]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.cc
c++: merge default targs for function templates [PR65396]
[gcc.git] / gcc / cp / pt.cc
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2022 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 #include "system.h"
31 #include "coretypes.h"
32 #include "cp-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "intl.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "toplev.h"
42 #include "tree-iterator.h"
43 #include "type-utils.h"
44 #include "gimplify.h"
45 #include "gcc-rich-location.h"
46 #include "selftest.h"
47 #include "target.h"
48
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
52
53 /* The PENDING_TEMPLATES is a list of templates whose instantiations
54 have been deferred, either because their definitions were not yet
55 available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
57 {
58 struct pending_template *next;
59 struct tinst_level *tinst;
60 };
61
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
64
65 int processing_template_parmlist;
66 static int template_header_count;
67
68 static vec<int> inline_parm_levels;
69
70 static GTY(()) struct tinst_level *current_tinst_level;
71
72 static GTY(()) vec<tree, va_gc> *saved_access_scope;
73
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
78
79 // -------------------------------------------------------------------------- //
80 // Local Specialization Stack
81 //
82 // Implementation of the RAII helper for creating new local
83 // specializations.
84 local_specialization_stack::local_specialization_stack (lss_policy policy)
85 : saved (local_specializations)
86 {
87 if (policy == lss_nop)
88 ;
89 else if (policy == lss_blank || !saved)
90 local_specializations = new hash_map<tree, tree>;
91 else
92 local_specializations = new hash_map<tree, tree>(*saved);
93 }
94
95 local_specialization_stack::~local_specialization_stack ()
96 {
97 if (local_specializations != saved)
98 {
99 delete local_specializations;
100 local_specializations = saved;
101 }
102 }
103
104 /* True if we've recursed into fn_type_unification too many times. */
105 static bool excessive_deduction_depth;
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 /* The general template is not in these tables. */
114 typedef hash_table<spec_hasher> spec_hash_table;
115 static GTY (()) spec_hash_table *decl_specializations;
116 static GTY (()) spec_hash_table *type_specializations;
117
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
123
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132
133 enum template_base_result {
134 tbr_incomplete_type,
135 tbr_ambiguous_baseclass,
136 tbr_success
137 };
138
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155 static int check_non_deducible_conversion (tree, tree, int, int,
156 struct conversion **, bool);
157 static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
158 tree*, tree*, tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
187 tree most_specialized_partial_spec (tree, tsubst_flags_t);
188 static tree tsubst_aggr_type (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 any_template_arguments_need_structural_equality_p (tree);
207 static bool dependent_type_p_r (tree);
208 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
209 static tree tsubst_decl (tree, tree, tsubst_flags_t);
210 static void perform_instantiation_time_access_checks (tree, tree);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree get_underlying_template (tree);
217 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
218 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 static tree make_argument_pack (tree);
220 static void register_parameter_specializations (tree, tree);
221 static tree enclosing_instantiation_of (tree tctx);
222 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
223
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 for a class or alias template (needed by instantiate_decl). */
228
229 void
230 push_access_scope (tree t)
231 {
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
233 || TREE_CODE (t) == TYPE_DECL);
234
235 if (DECL_FRIEND_CONTEXT (t))
236 push_nested_class (DECL_FRIEND_CONTEXT (t));
237 else if (DECL_IMPLICIT_TYPEDEF_P (t)
238 && CLASS_TYPE_P (TREE_TYPE (t)))
239 push_nested_class (TREE_TYPE (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
243 /* An artificial deduction guide should have the same access as
244 the constructor. */
245 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
246 else
247 push_to_top_level ();
248
249 if (TREE_CODE (t) == FUNCTION_DECL)
250 {
251 vec_safe_push (saved_access_scope, current_function_decl);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 current_function_decl = saved_access_scope->pop();
264
265 if (DECL_FRIEND_CONTEXT (t)
266 || (DECL_IMPLICIT_TYPEDEF_P (t)
267 && CLASS_TYPE_P (TREE_TYPE (t)))
268 || DECL_CLASS_SCOPE_P (t)
269 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
270 pop_nested_class ();
271 else
272 pop_from_top_level ();
273 }
274
275 /* Do any processing required when DECL (a member template
276 declaration) is finished. Returns the TEMPLATE_DECL corresponding
277 to DECL, unless it is a specialization, in which case the DECL
278 itself is returned. */
279
280 tree
281 finish_member_template_decl (tree decl)
282 {
283 if (decl == error_mark_node)
284 return error_mark_node;
285
286 gcc_assert (DECL_P (decl));
287
288 if (TREE_CODE (decl) == TYPE_DECL)
289 {
290 tree type;
291
292 type = TREE_TYPE (decl);
293 if (type == error_mark_node)
294 return error_mark_node;
295 if (MAYBE_CLASS_TYPE_P (type)
296 && CLASSTYPE_TEMPLATE_INFO (type)
297 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
298 {
299 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
300 check_member_template (tmpl);
301 return tmpl;
302 }
303 return NULL_TREE;
304 }
305 else if (TREE_CODE (decl) == FIELD_DECL)
306 error_at (DECL_SOURCE_LOCATION (decl),
307 "data member %qD cannot be a member template", decl);
308 else if (DECL_TEMPLATE_INFO (decl))
309 {
310 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
311 {
312 check_member_template (DECL_TI_TEMPLATE (decl));
313 return DECL_TI_TEMPLATE (decl);
314 }
315 else
316 return decl;
317 }
318 else
319 error_at (DECL_SOURCE_LOCATION (decl),
320 "invalid member template declaration %qD", decl);
321
322 return error_mark_node;
323 }
324
325 /* Create a template info node. */
326
327 tree
328 build_template_info (tree template_decl, tree template_args)
329 {
330 tree result = make_node (TEMPLATE_INFO);
331 TI_TEMPLATE (result) = template_decl;
332 TI_ARGS (result) = template_args;
333 return result;
334 }
335
336 /* Return the template info node corresponding to T, whatever T is. */
337
338 tree
339 get_template_info (const_tree t)
340 {
341 tree tinfo = NULL_TREE;
342
343 if (!t || t == error_mark_node)
344 return NULL;
345
346 if (TREE_CODE (t) == NAMESPACE_DECL
347 || TREE_CODE (t) == PARM_DECL)
348 return NULL;
349
350 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
351 tinfo = DECL_TEMPLATE_INFO (t);
352
353 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
354 t = TREE_TYPE (t);
355
356 if (OVERLOAD_TYPE_P (t))
357 tinfo = TYPE_TEMPLATE_INFO (t);
358 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
359 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
360
361 return tinfo;
362 }
363
364 /* Returns the template nesting level of the indicated class TYPE.
365
366 For example, in:
367 template <class T>
368 struct A
369 {
370 template <class U>
371 struct B {};
372 };
373
374 A<T>::B<U> has depth two, while A<T> has depth one.
375 Both A<T>::B<int> and A<int>::B<U> have depth one, if
376 they are instantiations, not specializations.
377
378 This function is guaranteed to return 0 if passed NULL_TREE so
379 that, for example, `template_class_depth (current_class_type)' is
380 always safe. */
381
382 int
383 template_class_depth (tree type)
384 {
385 int depth;
386
387 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
388 {
389 tree tinfo = get_template_info (type);
390
391 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
392 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
393 ++depth;
394
395 if (DECL_P (type))
396 {
397 if (tree fctx = DECL_FRIEND_CONTEXT (type))
398 type = fctx;
399 else
400 type = CP_DECL_CONTEXT (type);
401 }
402 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
403 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
404 else
405 type = CP_TYPE_CONTEXT (type);
406 }
407
408 return depth;
409 }
410
411 /* Return TRUE if NODE instantiates a template that has arguments of
412 its own, be it directly a primary template or indirectly through a
413 partial specializations. */
414 static bool
415 instantiates_primary_template_p (tree node)
416 {
417 tree tinfo = get_template_info (node);
418 if (!tinfo)
419 return false;
420
421 tree tmpl = TI_TEMPLATE (tinfo);
422 if (PRIMARY_TEMPLATE_P (tmpl))
423 return true;
424
425 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
426 return false;
427
428 /* So now we know we have a specialization, but it could be a full
429 or a partial specialization. To tell which, compare the depth of
430 its template arguments with those of its context. */
431
432 tree ctxt = DECL_CONTEXT (tmpl);
433 tree ctinfo = get_template_info (ctxt);
434 if (!ctinfo)
435 return true;
436
437 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
438 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
439 }
440
441 /* Subroutine of maybe_begin_member_template_processing.
442 Returns true if processing DECL needs us to push template parms. */
443
444 static bool
445 inline_needs_template_parms (tree decl, bool nsdmi)
446 {
447 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
448 return false;
449
450 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
451 > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
452 }
453
454 /* Subroutine of maybe_begin_member_template_processing.
455 Push the template parms in PARMS, starting from LEVELS steps into the
456 chain, and ending at the beginning, since template parms are listed
457 innermost first. */
458
459 static void
460 push_inline_template_parms_recursive (tree parmlist, int levels)
461 {
462 tree parms = TREE_VALUE (parmlist);
463 int i;
464
465 if (levels > 1)
466 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
467
468 ++processing_template_decl;
469 current_template_parms
470 = tree_cons (size_int (current_template_depth + 1),
471 parms, current_template_parms);
472 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
473
474 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
475 NULL);
476 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
477 {
478 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
479
480 if (error_operand_p (parm))
481 continue;
482
483 gcc_assert (DECL_P (parm));
484
485 switch (TREE_CODE (parm))
486 {
487 case TYPE_DECL:
488 case TEMPLATE_DECL:
489 pushdecl (parm);
490 break;
491
492 case PARM_DECL:
493 /* Push the CONST_DECL. */
494 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
495 break;
496
497 default:
498 gcc_unreachable ();
499 }
500 }
501 }
502
503 /* Restore the template parameter context for a member template, a
504 friend template defined in a class definition, or a non-template
505 member of template class. */
506
507 void
508 maybe_begin_member_template_processing (tree decl)
509 {
510 tree parms;
511 int levels = 0;
512 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
513
514 if (nsdmi)
515 {
516 tree ctx = DECL_CONTEXT (decl);
517 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
518 /* Disregard full specializations (c++/60999). */
519 && uses_template_parms (ctx)
520 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
521 }
522
523 if (inline_needs_template_parms (decl, nsdmi))
524 {
525 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
526 levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
527
528 if (DECL_TEMPLATE_SPECIALIZATION (decl))
529 {
530 --levels;
531 parms = TREE_CHAIN (parms);
532 }
533
534 push_inline_template_parms_recursive (parms, levels);
535 }
536
537 /* Remember how many levels of template parameters we pushed so that
538 we can pop them later. */
539 inline_parm_levels.safe_push (levels);
540 }
541
542 /* Undo the effects of maybe_begin_member_template_processing. */
543
544 void
545 maybe_end_member_template_processing (void)
546 {
547 int i;
548 int last;
549
550 if (inline_parm_levels.length () == 0)
551 return;
552
553 last = inline_parm_levels.pop ();
554 for (i = 0; i < last; ++i)
555 {
556 --processing_template_decl;
557 current_template_parms = TREE_CHAIN (current_template_parms);
558 poplevel (0, 0, 0);
559 }
560 }
561
562 /* Return a new template argument vector which contains all of ARGS,
563 but has as its innermost set of arguments the EXTRA_ARGS. */
564
565 tree
566 add_to_template_args (tree args, tree extra_args)
567 {
568 tree new_args;
569 int extra_depth;
570 int i;
571 int j;
572
573 if (args == NULL_TREE || extra_args == error_mark_node)
574 return extra_args;
575
576 extra_depth = TMPL_ARGS_DEPTH (extra_args);
577 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
578
579 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
581
582 for (j = 1; j <= extra_depth; ++j, ++i)
583 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
584
585 return new_args;
586 }
587
588 /* Like add_to_template_args, but only the outermost ARGS are added to
589 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
590 (EXTRA_ARGS) levels are added. This function is used to combine
591 the template arguments from a partial instantiation with the
592 template arguments used to attain the full instantiation from the
593 partial instantiation.
594
595 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
596
597 tree
598 add_outermost_template_args (tree args, tree extra_args)
599 {
600 tree new_args;
601
602 if (!args)
603 return extra_args;
604 if (TREE_CODE (args) == TEMPLATE_DECL)
605 {
606 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
607 args = TI_ARGS (ti);
608 }
609
610 /* If there are more levels of EXTRA_ARGS than there are ARGS,
611 something very fishy is going on. */
612 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
613
614 /* If *all* the new arguments will be the EXTRA_ARGS, just return
615 them. */
616 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
617 return extra_args;
618
619 /* For the moment, we make ARGS look like it contains fewer levels. */
620 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
621
622 new_args = add_to_template_args (args, extra_args);
623
624 /* Now, we restore ARGS to its full dimensions. */
625 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
626
627 return new_args;
628 }
629
630 /* Return the N levels of innermost template arguments from the ARGS. */
631
632 tree
633 get_innermost_template_args (tree args, int n)
634 {
635 tree new_args;
636 int extra_levels;
637 int i;
638
639 gcc_assert (n >= 0);
640
641 /* If N is 1, just return the innermost set of template arguments. */
642 if (n == 1)
643 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
644
645 /* If we're not removing anything, just return the arguments we were
646 given. */
647 extra_levels = TMPL_ARGS_DEPTH (args) - n;
648 gcc_assert (extra_levels >= 0);
649 if (extra_levels == 0)
650 return args;
651
652 /* Make a new set of arguments, not containing the outer arguments. */
653 new_args = make_tree_vec (n);
654 for (i = 1; i <= n; ++i)
655 SET_TMPL_ARGS_LEVEL (new_args, i,
656 TMPL_ARGS_LEVEL (args, i + extra_levels));
657
658 return new_args;
659 }
660
661 /* The inverse of get_innermost_template_args: Return all but the innermost
662 EXTRA_LEVELS levels of template arguments from the ARGS. */
663
664 static tree
665 strip_innermost_template_args (tree args, int extra_levels)
666 {
667 tree new_args;
668 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
669 int i;
670
671 gcc_assert (n >= 0);
672
673 /* If N is 1, just return the outermost set of template arguments. */
674 if (n == 1)
675 return TMPL_ARGS_LEVEL (args, 1);
676
677 /* If we're not removing anything, just return the arguments we were
678 given. */
679 gcc_assert (extra_levels >= 0);
680 if (extra_levels == 0)
681 return args;
682
683 /* Make a new set of arguments, not containing the inner arguments. */
684 new_args = make_tree_vec (n);
685 for (i = 1; i <= n; ++i)
686 SET_TMPL_ARGS_LEVEL (new_args, i,
687 TMPL_ARGS_LEVEL (args, i));
688
689 return new_args;
690 }
691
692 /* We've got a template header coming up; push to a new level for storing
693 the parms. */
694
695 void
696 begin_template_parm_list (void)
697 {
698 /* We use a non-tag-transparent scope here, which causes pushtag to
699 put tags in this scope, rather than in the enclosing class or
700 namespace scope. This is the right thing, since we want
701 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
702 global template class, push_template_decl handles putting the
703 TEMPLATE_DECL into top-level scope. For a nested template class,
704 e.g.:
705
706 template <class T> struct S1 {
707 template <class T> struct S2 {};
708 };
709
710 pushtag contains special code to insert the TEMPLATE_DECL for S2
711 at the right scope. */
712 begin_scope (sk_template_parms, NULL);
713 ++processing_template_decl;
714 ++processing_template_parmlist;
715 note_template_header (0);
716
717 /* Add a dummy parameter level while we process the parameter list. */
718 current_template_parms
719 = tree_cons (size_int (current_template_depth + 1),
720 make_tree_vec (0),
721 current_template_parms);
722 }
723
724 /* This routine is called when a specialization is declared. If it is
725 invalid to declare a specialization here, an error is reported and
726 false is returned, otherwise this routine will return true. */
727
728 static bool
729 check_specialization_scope (void)
730 {
731 tree scope = current_scope ();
732
733 /* [temp.expl.spec]
734
735 An explicit specialization shall be declared in the namespace of
736 which the template is a member, or, for member templates, in the
737 namespace of which the enclosing class or enclosing class
738 template is a member. An explicit specialization of a member
739 function, member class or static data member of a class template
740 shall be declared in the namespace of which the class template
741 is a member. */
742 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
743 {
744 error ("explicit specialization in non-namespace scope %qD", scope);
745 return false;
746 }
747
748 /* [temp.expl.spec]
749
750 In an explicit specialization declaration for a member of a class
751 template or a member template that appears in namespace scope,
752 the member template and some of its enclosing class templates may
753 remain unspecialized, except that the declaration shall not
754 explicitly specialize a class member template if its enclosing
755 class templates are not explicitly specialized as well. */
756 if (current_template_parms)
757 {
758 error ("enclosing class templates are not explicitly specialized");
759 return false;
760 }
761
762 return true;
763 }
764
765 /* We've just seen template <>. */
766
767 bool
768 begin_specialization (void)
769 {
770 begin_scope (sk_template_spec, NULL);
771 note_template_header (1);
772 return check_specialization_scope ();
773 }
774
775 /* Called at then end of processing a declaration preceded by
776 template<>. */
777
778 void
779 end_specialization (void)
780 {
781 finish_scope ();
782 reset_specialization ();
783 }
784
785 /* Any template <>'s that we have seen thus far are not referring to a
786 function specialization. */
787
788 void
789 reset_specialization (void)
790 {
791 processing_specialization = 0;
792 template_header_count = 0;
793 }
794
795 /* We've just seen a template header. If SPECIALIZATION is nonzero,
796 it was of the form template <>. */
797
798 static void
799 note_template_header (int specialization)
800 {
801 processing_specialization = specialization;
802 template_header_count++;
803 }
804
805 /* We're beginning an explicit instantiation. */
806
807 void
808 begin_explicit_instantiation (void)
809 {
810 gcc_assert (!processing_explicit_instantiation);
811 processing_explicit_instantiation = true;
812 }
813
814
815 void
816 end_explicit_instantiation (void)
817 {
818 gcc_assert (processing_explicit_instantiation);
819 processing_explicit_instantiation = false;
820 }
821
822 /* An explicit specialization or partial specialization of TMPL is being
823 declared. Check that the namespace in which the specialization is
824 occurring is permissible. Returns false iff it is invalid to
825 specialize TMPL in the current namespace. */
826
827 static bool
828 check_specialization_namespace (tree tmpl)
829 {
830 tree tpl_ns = decl_namespace_context (tmpl);
831
832 /* [tmpl.expl.spec]
833
834 An explicit specialization shall be declared in a namespace enclosing the
835 specialized template. An explicit specialization whose declarator-id is
836 not qualified shall be declared in the nearest enclosing namespace of the
837 template, or, if the namespace is inline (7.3.1), any namespace from its
838 enclosing namespace set. */
839 if (current_scope() != DECL_CONTEXT (tmpl)
840 && !at_namespace_scope_p ())
841 {
842 error ("specialization of %qD must appear at namespace scope", tmpl);
843 return false;
844 }
845
846 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
847 /* Same or enclosing namespace. */
848 return true;
849 else
850 {
851 auto_diagnostic_group d;
852 if (permerror (input_location,
853 "specialization of %qD in different namespace", tmpl))
854 inform (DECL_SOURCE_LOCATION (tmpl),
855 " from definition of %q#D", tmpl);
856 return false;
857 }
858 }
859
860 /* SPEC is an explicit instantiation. Check that it is valid to
861 perform this explicit instantiation in the current namespace. */
862
863 static void
864 check_explicit_instantiation_namespace (tree spec)
865 {
866 tree ns;
867
868 /* DR 275: An explicit instantiation shall appear in an enclosing
869 namespace of its template. */
870 ns = decl_namespace_context (spec);
871 if (!is_nested_namespace (current_namespace, ns))
872 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
873 "(which does not enclose namespace %qD)",
874 spec, current_namespace, ns);
875 }
876
877 /* Returns the type of a template specialization only if that
878 specialization needs to be defined. Otherwise (e.g., if the type has
879 already been defined), the function returns NULL_TREE. */
880
881 static tree
882 maybe_new_partial_specialization (tree type)
883 {
884 /* An implicit instantiation of an incomplete type implies
885 the definition of a new class template.
886
887 template<typename T>
888 struct S;
889
890 template<typename T>
891 struct S<T*>;
892
893 Here, S<T*> is an implicit instantiation of S whose type
894 is incomplete. */
895 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
896 return type;
897
898 /* It can also be the case that TYPE is a completed specialization.
899 Continuing the previous example, suppose we also declare:
900
901 template<typename T>
902 requires Integral<T>
903 struct S<T*>;
904
905 Here, S<T*> refers to the specialization S<T*> defined
906 above. However, we need to differentiate definitions because
907 we intend to define a new partial specialization. In this case,
908 we rely on the fact that the constraints are different for
909 this declaration than that above.
910
911 Note that we also get here for injected class names and
912 late-parsed template definitions. We must ensure that we
913 do not create new type declarations for those cases. */
914 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
915 {
916 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
917 tree args = CLASSTYPE_TI_ARGS (type);
918
919 /* If there are no template parameters, this cannot be a new
920 partial template specialization? */
921 if (!current_template_parms)
922 return NULL_TREE;
923
924 /* The injected-class-name is not a new partial specialization. */
925 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
926 return NULL_TREE;
927
928 /* If the constraints are not the same as those of the primary
929 then, we can probably create a new specialization. */
930 tree type_constr = current_template_constraints ();
931
932 if (type == TREE_TYPE (tmpl))
933 {
934 tree main_constr = get_constraints (tmpl);
935 if (equivalent_constraints (type_constr, main_constr))
936 return NULL_TREE;
937 }
938
939 /* Also, if there's a pre-existing specialization with matching
940 constraints, then this also isn't new. */
941 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
942 while (specs)
943 {
944 tree spec_tmpl = TREE_VALUE (specs);
945 tree spec_args = TREE_PURPOSE (specs);
946 tree spec_constr = get_constraints (spec_tmpl);
947 if (comp_template_args (args, spec_args)
948 && equivalent_constraints (type_constr, spec_constr))
949 return NULL_TREE;
950 specs = TREE_CHAIN (specs);
951 }
952
953 /* Create a new type node (and corresponding type decl)
954 for the newly declared specialization. */
955 tree t = make_class_type (TREE_CODE (type));
956 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
957 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
958
959 /* We only need a separate type node for storing the definition of this
960 partial specialization; uses of S<T*> are unconstrained, so all are
961 equivalent. So keep TYPE_CANONICAL the same. */
962 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
963
964 /* Build the corresponding type decl. */
965 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
966 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
967 DECL_SOURCE_LOCATION (d) = input_location;
968 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
969 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
970
971 set_instantiating_module (d);
972 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
973
974 return t;
975 }
976
977 return NULL_TREE;
978 }
979
980 /* The TYPE is being declared. If it is a template type, that means it
981 is a partial specialization. Do appropriate error-checking. */
982
983 tree
984 maybe_process_partial_specialization (tree type)
985 {
986 tree context;
987
988 if (type == error_mark_node)
989 return error_mark_node;
990
991 /* A lambda that appears in specialization context is not itself a
992 specialization. */
993 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
994 return type;
995
996 /* An injected-class-name is not a specialization. */
997 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
998 return type;
999
1000 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1001 {
1002 error ("name of class shadows template template parameter %qD",
1003 TYPE_NAME (type));
1004 return error_mark_node;
1005 }
1006
1007 context = TYPE_CONTEXT (type);
1008
1009 if (TYPE_ALIAS_P (type))
1010 {
1011 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1012
1013 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1014 error ("specialization of alias template %qD",
1015 TI_TEMPLATE (tinfo));
1016 else
1017 error ("explicit specialization of non-template %qT", type);
1018 return error_mark_node;
1019 }
1020 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1021 {
1022 /* This is for ordinary explicit specialization and partial
1023 specialization of a template class such as:
1024
1025 template <> class C<int>;
1026
1027 or:
1028
1029 template <class T> class C<T*>;
1030
1031 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1032
1033 if (tree t = maybe_new_partial_specialization (type))
1034 {
1035 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1036 && !at_namespace_scope_p ())
1037 return error_mark_node;
1038 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1039 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1040 if (processing_template_decl)
1041 {
1042 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1043 if (decl == error_mark_node)
1044 return error_mark_node;
1045 return TREE_TYPE (decl);
1046 }
1047 }
1048 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1049 error ("specialization of %qT after instantiation", type);
1050 else if (errorcount && !processing_specialization
1051 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1052 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1053 /* Trying to define a specialization either without a template<> header
1054 or in an inappropriate place. We've already given an error, so just
1055 bail now so we don't actually define the specialization. */
1056 return error_mark_node;
1057 }
1058 else if (CLASS_TYPE_P (type)
1059 && !CLASSTYPE_USE_TEMPLATE (type)
1060 && CLASSTYPE_TEMPLATE_INFO (type)
1061 && context && CLASS_TYPE_P (context)
1062 && CLASSTYPE_TEMPLATE_INFO (context))
1063 {
1064 /* This is for an explicit specialization of member class
1065 template according to [temp.expl.spec/18]:
1066
1067 template <> template <class U> class C<int>::D;
1068
1069 The context `C<int>' must be an implicit instantiation.
1070 Otherwise this is just a member class template declared
1071 earlier like:
1072
1073 template <> class C<int> { template <class U> class D; };
1074 template <> template <class U> class C<int>::D;
1075
1076 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1077 while in the second case, `C<int>::D' is a primary template
1078 and `C<T>::D' may not exist. */
1079
1080 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1081 && !COMPLETE_TYPE_P (type))
1082 {
1083 tree t;
1084 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1085
1086 if (current_namespace
1087 != decl_namespace_context (tmpl))
1088 {
1089 if (permerror (input_location,
1090 "specialization of %qD in different namespace",
1091 type))
1092 inform (DECL_SOURCE_LOCATION (tmpl),
1093 "from definition of %q#D", tmpl);
1094 }
1095
1096 /* Check for invalid specialization after instantiation:
1097
1098 template <> template <> class C<int>::D<int>;
1099 template <> template <class U> class C<int>::D; */
1100
1101 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1102 t; t = TREE_CHAIN (t))
1103 {
1104 tree inst = TREE_VALUE (t);
1105 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1106 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1107 {
1108 /* We already have a full specialization of this partial
1109 instantiation, or a full specialization has been
1110 looked up but not instantiated. Reassign it to the
1111 new member specialization template. */
1112 spec_entry elt;
1113 spec_entry *entry;
1114
1115 elt.tmpl = most_general_template (tmpl);
1116 elt.args = CLASSTYPE_TI_ARGS (inst);
1117 elt.spec = inst;
1118
1119 type_specializations->remove_elt (&elt);
1120
1121 elt.tmpl = tmpl;
1122 CLASSTYPE_TI_ARGS (inst)
1123 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1124
1125 spec_entry **slot
1126 = type_specializations->find_slot (&elt, INSERT);
1127 entry = ggc_alloc<spec_entry> ();
1128 *entry = elt;
1129 *slot = entry;
1130 }
1131 else
1132 /* But if we've had an implicit instantiation, that's a
1133 problem ([temp.expl.spec]/6). */
1134 error ("specialization %qT after instantiation %qT",
1135 type, inst);
1136 }
1137
1138 /* Mark TYPE as a specialization. And as a result, we only
1139 have one level of template argument for the innermost
1140 class template. */
1141 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1142 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1143 CLASSTYPE_TI_ARGS (type)
1144 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1145 }
1146 }
1147 else if (processing_specialization)
1148 {
1149 /* Someday C++0x may allow for enum template specialization. */
1150 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1151 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1152 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1153 "of %qD not allowed by ISO C++", type);
1154 else
1155 {
1156 error ("explicit specialization of non-template %qT", type);
1157 return error_mark_node;
1158 }
1159 }
1160
1161 return type;
1162 }
1163
1164 /* Returns nonzero if we can optimize the retrieval of specializations
1165 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1166 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1167
1168 static inline bool
1169 optimize_specialization_lookup_p (tree tmpl)
1170 {
1171 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1172 && DECL_CLASS_SCOPE_P (tmpl)
1173 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1174 parameter. */
1175 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1176 /* The optimized lookup depends on the fact that the
1177 template arguments for the member function template apply
1178 purely to the containing class, which is not true if the
1179 containing class is an explicit or partial
1180 specialization. */
1181 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1182 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1183 && !DECL_CONV_FN_P (tmpl)
1184 /* It is possible to have a template that is not a member
1185 template and is not a member of a template class:
1186
1187 template <typename T>
1188 struct S { friend A::f(); };
1189
1190 Here, the friend function is a template, but the context does
1191 not have template information. The optimized lookup relies
1192 on having ARGS be the template arguments for both the class
1193 and the function template. */
1194 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1195 }
1196
1197 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1198 gone through coerce_template_parms by now. */
1199
1200 static void
1201 verify_unstripped_args_1 (tree inner)
1202 {
1203 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1204 {
1205 tree arg = TREE_VEC_ELT (inner, i);
1206 if (TREE_CODE (arg) == TEMPLATE_DECL)
1207 /* OK */;
1208 else if (TYPE_P (arg))
1209 gcc_assert (strip_typedefs (arg, NULL) == arg);
1210 else if (ARGUMENT_PACK_P (arg))
1211 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1212 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1213 /* Allow typedefs on the type of a non-type argument, since a
1214 parameter can have them. */;
1215 else
1216 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1217 }
1218 }
1219
1220 static void
1221 verify_unstripped_args (tree args)
1222 {
1223 ++processing_template_decl;
1224 if (!any_dependent_template_arguments_p (args))
1225 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1226 --processing_template_decl;
1227 }
1228
1229 /* Retrieve the specialization (in the sense of [temp.spec] - a
1230 specialization is either an instantiation or an explicit
1231 specialization) of TMPL for the given template ARGS. If there is
1232 no such specialization, return NULL_TREE. The ARGS are a vector of
1233 arguments, or a vector of vectors of arguments, in the case of
1234 templates with more than one level of parameters.
1235
1236 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1237 then we search for a partial specialization matching ARGS. This
1238 parameter is ignored if TMPL is not a class template.
1239
1240 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1241 result is a NONTYPE_ARGUMENT_PACK. */
1242
1243 static tree
1244 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1245 {
1246 if (tmpl == NULL_TREE)
1247 return NULL_TREE;
1248
1249 if (args == error_mark_node)
1250 return NULL_TREE;
1251
1252 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1253 || TREE_CODE (tmpl) == FIELD_DECL);
1254
1255 /* There should be as many levels of arguments as there are
1256 levels of parameters. */
1257 gcc_assert (TMPL_ARGS_DEPTH (args)
1258 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1259 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1260 : template_class_depth (DECL_CONTEXT (tmpl))));
1261
1262 if (flag_checking)
1263 verify_unstripped_args (args);
1264
1265 /* Lambda functions in templates aren't instantiated normally, but through
1266 tsubst_lambda_expr. */
1267 if (lambda_fn_in_template_p (tmpl))
1268 return NULL_TREE;
1269
1270 if (optimize_specialization_lookup_p (tmpl))
1271 {
1272 /* The template arguments actually apply to the containing
1273 class. Find the class specialization with those
1274 arguments. */
1275 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1276 tree class_specialization
1277 = retrieve_specialization (class_template, args, 0);
1278 if (!class_specialization)
1279 return NULL_TREE;
1280
1281 /* Find the instance of TMPL. */
1282 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1283 for (ovl_iterator iter (fns); iter; ++iter)
1284 {
1285 tree fn = *iter;
1286 if (tree ti = get_template_info (fn))
1287 if (TI_TEMPLATE (ti) == tmpl
1288 /* using-declarations can bring in a different
1289 instantiation of tmpl as a member of a different
1290 instantiation of tmpl's class. We don't want those
1291 here. */
1292 && DECL_CONTEXT (fn) == class_specialization)
1293 return fn;
1294 }
1295 return NULL_TREE;
1296 }
1297 else
1298 {
1299 spec_entry *found;
1300 spec_entry elt;
1301 spec_hash_table *specializations;
1302
1303 elt.tmpl = tmpl;
1304 elt.args = args;
1305 elt.spec = NULL_TREE;
1306
1307 if (DECL_CLASS_TEMPLATE_P (tmpl))
1308 specializations = type_specializations;
1309 else
1310 specializations = decl_specializations;
1311
1312 if (hash == 0)
1313 hash = spec_hasher::hash (&elt);
1314 found = specializations->find_with_hash (&elt, hash);
1315 if (found)
1316 return found->spec;
1317 }
1318
1319 return NULL_TREE;
1320 }
1321
1322 /* Like retrieve_specialization, but for local declarations. */
1323
1324 tree
1325 retrieve_local_specialization (tree tmpl)
1326 {
1327 if (local_specializations == NULL)
1328 return NULL_TREE;
1329
1330 tree *slot = local_specializations->get (tmpl);
1331 return slot ? *slot : NULL_TREE;
1332 }
1333
1334 /* Returns nonzero iff DECL is a specialization of TMPL. */
1335
1336 int
1337 is_specialization_of (tree decl, tree tmpl)
1338 {
1339 tree t;
1340
1341 if (TREE_CODE (decl) == FUNCTION_DECL)
1342 {
1343 for (t = decl;
1344 t != NULL_TREE;
1345 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1346 if (t == tmpl)
1347 return 1;
1348 }
1349 else
1350 {
1351 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1352
1353 for (t = TREE_TYPE (decl);
1354 t != NULL_TREE;
1355 t = CLASSTYPE_USE_TEMPLATE (t)
1356 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1357 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1358 return 1;
1359 }
1360
1361 return 0;
1362 }
1363
1364 /* Returns nonzero iff DECL is a specialization of friend declaration
1365 FRIEND_DECL according to [temp.friend]. */
1366
1367 bool
1368 is_specialization_of_friend (tree decl, tree friend_decl)
1369 {
1370 bool need_template = true;
1371 int template_depth;
1372
1373 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1374 || TREE_CODE (decl) == TYPE_DECL);
1375
1376 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1377 of a template class, we want to check if DECL is a specialization
1378 if this. */
1379 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1380 && DECL_TEMPLATE_INFO (friend_decl)
1381 && !DECL_USE_TEMPLATE (friend_decl))
1382 {
1383 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1384 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1385 need_template = false;
1386 }
1387 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1388 && !PRIMARY_TEMPLATE_P (friend_decl))
1389 need_template = false;
1390
1391 /* There is nothing to do if this is not a template friend. */
1392 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1393 return false;
1394
1395 if (is_specialization_of (decl, friend_decl))
1396 return true;
1397
1398 /* [temp.friend/6]
1399 A member of a class template may be declared to be a friend of a
1400 non-template class. In this case, the corresponding member of
1401 every specialization of the class template is a friend of the
1402 class granting friendship.
1403
1404 For example, given a template friend declaration
1405
1406 template <class T> friend void A<T>::f();
1407
1408 the member function below is considered a friend
1409
1410 template <> struct A<int> {
1411 void f();
1412 };
1413
1414 For this type of template friend, TEMPLATE_DEPTH below will be
1415 nonzero. To determine if DECL is a friend of FRIEND, we first
1416 check if the enclosing class is a specialization of another. */
1417
1418 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1419 if (template_depth
1420 && DECL_CLASS_SCOPE_P (decl)
1421 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1422 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1423 {
1424 /* Next, we check the members themselves. In order to handle
1425 a few tricky cases, such as when FRIEND_DECL's are
1426
1427 template <class T> friend void A<T>::g(T t);
1428 template <class T> template <T t> friend void A<T>::h();
1429
1430 and DECL's are
1431
1432 void A<int>::g(int);
1433 template <int> void A<int>::h();
1434
1435 we need to figure out ARGS, the template arguments from
1436 the context of DECL. This is required for template substitution
1437 of `T' in the function parameter of `g' and template parameter
1438 of `h' in the above examples. Here ARGS corresponds to `int'. */
1439
1440 tree context = DECL_CONTEXT (decl);
1441 tree args = NULL_TREE;
1442 int current_depth = 0;
1443
1444 while (current_depth < template_depth)
1445 {
1446 if (CLASSTYPE_TEMPLATE_INFO (context))
1447 {
1448 if (current_depth == 0)
1449 args = TYPE_TI_ARGS (context);
1450 else
1451 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1452 current_depth++;
1453 }
1454 context = TYPE_CONTEXT (context);
1455 }
1456
1457 if (TREE_CODE (decl) == FUNCTION_DECL)
1458 {
1459 bool is_template;
1460 tree friend_type;
1461 tree decl_type;
1462 tree friend_args_type;
1463 tree decl_args_type;
1464
1465 /* Make sure that both DECL and FRIEND_DECL are templates or
1466 non-templates. */
1467 is_template = DECL_TEMPLATE_INFO (decl)
1468 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1469 if (need_template ^ is_template)
1470 return false;
1471 else if (is_template)
1472 {
1473 /* If both are templates, check template parameter list. */
1474 tree friend_parms
1475 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1476 args, tf_none);
1477 if (!comp_template_parms
1478 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1479 friend_parms))
1480 return false;
1481
1482 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1483 }
1484 else
1485 decl_type = TREE_TYPE (decl);
1486
1487 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1488 tf_none, NULL_TREE);
1489 if (friend_type == error_mark_node)
1490 return false;
1491
1492 /* Check if return types match. */
1493 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1494 return false;
1495
1496 /* Check if function parameter types match, ignoring the
1497 `this' parameter. */
1498 friend_args_type = TYPE_ARG_TYPES (friend_type);
1499 decl_args_type = TYPE_ARG_TYPES (decl_type);
1500 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1501 friend_args_type = TREE_CHAIN (friend_args_type);
1502 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1503 decl_args_type = TREE_CHAIN (decl_args_type);
1504
1505 return compparms (decl_args_type, friend_args_type);
1506 }
1507 else
1508 {
1509 /* DECL is a TYPE_DECL */
1510 bool is_template;
1511 tree decl_type = TREE_TYPE (decl);
1512
1513 /* Make sure that both DECL and FRIEND_DECL are templates or
1514 non-templates. */
1515 is_template
1516 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1517 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1518
1519 if (need_template ^ is_template)
1520 return false;
1521 else if (is_template)
1522 {
1523 tree friend_parms;
1524 /* If both are templates, check the name of the two
1525 TEMPLATE_DECL's first because is_friend didn't. */
1526 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1527 != DECL_NAME (friend_decl))
1528 return false;
1529
1530 /* Now check template parameter list. */
1531 friend_parms
1532 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1533 args, tf_none);
1534 return comp_template_parms
1535 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1536 friend_parms);
1537 }
1538 else
1539 return (DECL_NAME (decl)
1540 == DECL_NAME (friend_decl));
1541 }
1542 }
1543 return false;
1544 }
1545
1546 /* Register the specialization SPEC as a specialization of TMPL with
1547 the indicated ARGS. IS_FRIEND indicates whether the specialization
1548 is actually just a friend declaration. ATTRLIST is the list of
1549 attributes that the specialization is declared with or NULL when
1550 it isn't. Returns SPEC, or an equivalent prior declaration, if
1551 available.
1552
1553 We also store instantiations of field packs in the hash table, even
1554 though they are not themselves templates, to make lookup easier. */
1555
1556 static tree
1557 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1558 hashval_t hash)
1559 {
1560 tree fn;
1561 spec_entry **slot = NULL;
1562 spec_entry elt;
1563
1564 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1565 || (TREE_CODE (tmpl) == FIELD_DECL
1566 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1567
1568 if (TREE_CODE (spec) == FUNCTION_DECL
1569 && uses_template_parms (DECL_TI_ARGS (spec)))
1570 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1571 register it; we want the corresponding TEMPLATE_DECL instead.
1572 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1573 the more obvious `uses_template_parms (spec)' to avoid problems
1574 with default function arguments. In particular, given
1575 something like this:
1576
1577 template <class T> void f(T t1, T t = T())
1578
1579 the default argument expression is not substituted for in an
1580 instantiation unless and until it is actually needed. */
1581 return spec;
1582
1583 if (optimize_specialization_lookup_p (tmpl))
1584 /* We don't put these specializations in the hash table, but we might
1585 want to give an error about a mismatch. */
1586 fn = retrieve_specialization (tmpl, args, 0);
1587 else
1588 {
1589 elt.tmpl = tmpl;
1590 elt.args = args;
1591 elt.spec = spec;
1592
1593 if (hash == 0)
1594 hash = spec_hasher::hash (&elt);
1595
1596 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1597 if (*slot)
1598 fn = (*slot)->spec;
1599 else
1600 fn = NULL_TREE;
1601 }
1602
1603 /* We can sometimes try to re-register a specialization that we've
1604 already got. In particular, regenerate_decl_from_template calls
1605 duplicate_decls which will update the specialization list. But,
1606 we'll still get called again here anyhow. It's more convenient
1607 to simply allow this than to try to prevent it. */
1608 if (fn == spec)
1609 return spec;
1610 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1611 {
1612 if (DECL_TEMPLATE_INSTANTIATION (fn))
1613 {
1614 if (DECL_ODR_USED (fn)
1615 || DECL_EXPLICIT_INSTANTIATION (fn))
1616 {
1617 error ("specialization of %qD after instantiation",
1618 fn);
1619 return error_mark_node;
1620 }
1621 else
1622 {
1623 tree clone;
1624 /* This situation should occur only if the first
1625 specialization is an implicit instantiation, the
1626 second is an explicit specialization, and the
1627 implicit instantiation has not yet been used. That
1628 situation can occur if we have implicitly
1629 instantiated a member function and then specialized
1630 it later.
1631
1632 We can also wind up here if a friend declaration that
1633 looked like an instantiation turns out to be a
1634 specialization:
1635
1636 template <class T> void foo(T);
1637 class S { friend void foo<>(int) };
1638 template <> void foo(int);
1639
1640 We transform the existing DECL in place so that any
1641 pointers to it become pointers to the updated
1642 declaration.
1643
1644 If there was a definition for the template, but not
1645 for the specialization, we want this to look as if
1646 there were no definition, and vice versa. */
1647 DECL_INITIAL (fn) = NULL_TREE;
1648 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1649 /* The call to duplicate_decls will have applied
1650 [temp.expl.spec]:
1651
1652 An explicit specialization of a function template
1653 is inline only if it is explicitly declared to be,
1654 and independently of whether its function template
1655 is.
1656
1657 to the primary function; now copy the inline bits to
1658 the various clones. */
1659 FOR_EACH_CLONE (clone, fn)
1660 {
1661 DECL_DECLARED_INLINE_P (clone)
1662 = DECL_DECLARED_INLINE_P (fn);
1663 DECL_SOURCE_LOCATION (clone)
1664 = DECL_SOURCE_LOCATION (fn);
1665 DECL_DELETED_FN (clone)
1666 = DECL_DELETED_FN (fn);
1667 }
1668 check_specialization_namespace (tmpl);
1669
1670 return fn;
1671 }
1672 }
1673 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1674 {
1675 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1676 if (dd == error_mark_node)
1677 /* We've already complained in duplicate_decls. */
1678 return error_mark_node;
1679
1680 if (dd == NULL_TREE && DECL_INITIAL (spec))
1681 /* Dup decl failed, but this is a new definition. Set the
1682 line number so any errors match this new
1683 definition. */
1684 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1685
1686 return fn;
1687 }
1688 }
1689 else if (fn)
1690 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1691
1692 /* A specialization must be declared in the same namespace as the
1693 template it is specializing. */
1694 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1695 && !check_specialization_namespace (tmpl))
1696 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1697
1698 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1699 {
1700 spec_entry *entry = ggc_alloc<spec_entry> ();
1701 gcc_assert (tmpl && args && spec);
1702 *entry = elt;
1703 *slot = entry;
1704 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1705 && PRIMARY_TEMPLATE_P (tmpl)
1706 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1707 || variable_template_p (tmpl))
1708 /* If TMPL is a forward declaration of a template function, keep a list
1709 of all specializations in case we need to reassign them to a friend
1710 template later in tsubst_friend_function.
1711
1712 Also keep a list of all variable template instantiations so that
1713 process_partial_specialization can check whether a later partial
1714 specialization would have used it. */
1715 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1716 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1717 }
1718
1719 return spec;
1720 }
1721
1722 /* Restricts tree and type comparisons. */
1723 int comparing_specializations;
1724 int comparing_dependent_aliases;
1725
1726 /* Returns true iff two spec_entry nodes are equivalent. */
1727
1728 bool
1729 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1730 {
1731 int equal;
1732
1733 ++comparing_specializations;
1734 ++comparing_dependent_aliases;
1735 ++processing_template_decl;
1736 equal = (e1->tmpl == e2->tmpl
1737 && comp_template_args (e1->args, e2->args));
1738 if (equal && flag_concepts
1739 /* tmpl could be a FIELD_DECL for a capture pack. */
1740 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1741 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1742 && uses_template_parms (e1->args))
1743 {
1744 /* Partial specializations of a variable template can be distinguished by
1745 constraints. */
1746 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1747 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1748 equal = equivalent_constraints (c1, c2);
1749 }
1750 --processing_template_decl;
1751 --comparing_dependent_aliases;
1752 --comparing_specializations;
1753
1754 return equal;
1755 }
1756
1757 /* Returns a hash for a template TMPL and template arguments ARGS. */
1758
1759 static hashval_t
1760 hash_tmpl_and_args (tree tmpl, tree args)
1761 {
1762 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1763 return iterative_hash_template_arg (args, val);
1764 }
1765
1766 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1767 ignoring SPEC. */
1768
1769 hashval_t
1770 spec_hasher::hash (spec_entry *e)
1771 {
1772 return hash_tmpl_and_args (e->tmpl, e->args);
1773 }
1774
1775 /* Recursively calculate a hash value for a template argument ARG, for use
1776 in the hash tables of template specializations. We must be
1777 careful to (at least) skip the same entities template_args_equal
1778 does. */
1779
1780 hashval_t
1781 iterative_hash_template_arg (tree arg, hashval_t val)
1782 {
1783 if (arg == NULL_TREE)
1784 return iterative_hash_object (arg, val);
1785
1786 if (!TYPE_P (arg))
1787 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1788 while (CONVERT_EXPR_P (arg)
1789 || TREE_CODE (arg) == NON_LVALUE_EXPR
1790 || class_nttp_const_wrapper_p (arg))
1791 arg = TREE_OPERAND (arg, 0);
1792
1793 enum tree_code code = TREE_CODE (arg);
1794
1795 val = iterative_hash_object (code, val);
1796
1797 switch (code)
1798 {
1799 case ARGUMENT_PACK_SELECT:
1800 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1801 preserving it in a hash table, which is bad because it will change
1802 meaning when gen_elem_of_pack_expansion_instantiation changes the
1803 ARGUMENT_PACK_SELECT_INDEX. */
1804 gcc_unreachable ();
1805
1806 case ERROR_MARK:
1807 return val;
1808
1809 case IDENTIFIER_NODE:
1810 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1811
1812 case TREE_VEC:
1813 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1814 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1815 return val;
1816
1817 case TYPE_PACK_EXPANSION:
1818 case EXPR_PACK_EXPANSION:
1819 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1820 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1821
1822 case TYPE_ARGUMENT_PACK:
1823 case NONTYPE_ARGUMENT_PACK:
1824 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1825
1826 case TREE_LIST:
1827 for (; arg; arg = TREE_CHAIN (arg))
1828 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1829 return val;
1830
1831 case OVERLOAD:
1832 for (lkp_iterator iter (arg); iter; ++iter)
1833 val = iterative_hash_template_arg (*iter, val);
1834 return val;
1835
1836 case CONSTRUCTOR:
1837 {
1838 iterative_hash_template_arg (TREE_TYPE (arg), val);
1839 for (auto &e: CONSTRUCTOR_ELTS (arg))
1840 {
1841 val = iterative_hash_template_arg (e.index, val);
1842 val = iterative_hash_template_arg (e.value, val);
1843 }
1844 return val;
1845 }
1846
1847 case PARM_DECL:
1848 if (!DECL_ARTIFICIAL (arg))
1849 {
1850 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1851 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1852 }
1853 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1854
1855 case TARGET_EXPR:
1856 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1857
1858 case PTRMEM_CST:
1859 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1860 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1861
1862 case TEMPLATE_PARM_INDEX:
1863 val = iterative_hash_template_arg
1864 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1865 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1866 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1867
1868 case TRAIT_EXPR:
1869 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1870 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1871 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1872
1873 case BASELINK:
1874 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1875 val);
1876 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1877 val);
1878
1879 case MODOP_EXPR:
1880 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1881 code = TREE_CODE (TREE_OPERAND (arg, 1));
1882 val = iterative_hash_object (code, val);
1883 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1884
1885 case LAMBDA_EXPR:
1886 /* [temp.over.link] Two lambda-expressions are never considered
1887 equivalent.
1888
1889 So just hash the closure type. */
1890 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1891
1892 case CAST_EXPR:
1893 case IMPLICIT_CONV_EXPR:
1894 case STATIC_CAST_EXPR:
1895 case REINTERPRET_CAST_EXPR:
1896 case CONST_CAST_EXPR:
1897 case DYNAMIC_CAST_EXPR:
1898 case NEW_EXPR:
1899 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1900 /* Now hash operands as usual. */
1901 break;
1902
1903 case CALL_EXPR:
1904 {
1905 tree fn = CALL_EXPR_FN (arg);
1906 if (tree name = dependent_name (fn))
1907 {
1908 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1909 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1910 fn = name;
1911 }
1912 val = iterative_hash_template_arg (fn, val);
1913 call_expr_arg_iterator ai;
1914 for (tree x = first_call_expr_arg (arg, &ai); x;
1915 x = next_call_expr_arg (&ai))
1916 val = iterative_hash_template_arg (x, val);
1917 return val;
1918 }
1919
1920 default:
1921 break;
1922 }
1923
1924 char tclass = TREE_CODE_CLASS (code);
1925 switch (tclass)
1926 {
1927 case tcc_type:
1928 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1929 {
1930 // We want an alias specialization that survived strip_typedefs
1931 // to hash differently from its TYPE_CANONICAL, to avoid hash
1932 // collisions that compare as different in template_args_equal.
1933 // These could be dependent specializations that strip_typedefs
1934 // left alone, or untouched specializations because
1935 // coerce_template_parms returns the unconverted template
1936 // arguments if it sees incomplete argument packs.
1937 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1938 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1939 }
1940
1941 switch (TREE_CODE (arg))
1942 {
1943 case TEMPLATE_TEMPLATE_PARM:
1944 {
1945 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1946
1947 /* Do not recurse with TPI directly, as that is unbounded
1948 recursion. */
1949 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1950 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1951 }
1952 break;
1953
1954 case DECLTYPE_TYPE:
1955 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1956 break;
1957
1958 default:
1959 if (tree canonical = TYPE_CANONICAL (arg))
1960 val = iterative_hash_object (TYPE_HASH (canonical), val);
1961 break;
1962 }
1963
1964 return val;
1965
1966 case tcc_declaration:
1967 case tcc_constant:
1968 return iterative_hash_expr (arg, val);
1969
1970 default:
1971 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1972 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1973 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1974 return val;
1975 }
1976 }
1977
1978 /* Unregister the specialization SPEC as a specialization of TMPL.
1979 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1980 if the SPEC was listed as a specialization of TMPL.
1981
1982 Note that SPEC has been ggc_freed, so we can't look inside it. */
1983
1984 bool
1985 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1986 {
1987 spec_entry *entry;
1988 spec_entry elt;
1989
1990 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1991 elt.args = TI_ARGS (tinfo);
1992 elt.spec = NULL_TREE;
1993
1994 entry = decl_specializations->find (&elt);
1995 if (entry != NULL)
1996 {
1997 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1998 gcc_assert (new_spec != NULL_TREE);
1999 entry->spec = new_spec;
2000 return 1;
2001 }
2002
2003 return 0;
2004 }
2005
2006 /* Like register_specialization, but for local declarations. We are
2007 registering SPEC, an instantiation of TMPL. */
2008
2009 void
2010 register_local_specialization (tree spec, tree tmpl)
2011 {
2012 gcc_assert (tmpl != spec);
2013 local_specializations->put (tmpl, spec);
2014 }
2015
2016 /* TYPE is a class type. Returns true if TYPE is an explicitly
2017 specialized class. */
2018
2019 bool
2020 explicit_class_specialization_p (tree type)
2021 {
2022 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2023 return false;
2024 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2025 }
2026
2027 /* Print the list of functions at FNS, going through all the overloads
2028 for each element of the list. Alternatively, FNS cannot be a
2029 TREE_LIST, in which case it will be printed together with all the
2030 overloads.
2031
2032 MORE and *STR should respectively be FALSE and NULL when the function
2033 is called from the outside. They are used internally on recursive
2034 calls. print_candidates manages the two parameters and leaves NULL
2035 in *STR when it ends. */
2036
2037 static void
2038 print_candidates_1 (tree fns, char **str, bool more = false)
2039 {
2040 if (TREE_CODE (fns) == TREE_LIST)
2041 for (; fns; fns = TREE_CHAIN (fns))
2042 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2043 else
2044 for (lkp_iterator iter (fns); iter;)
2045 {
2046 tree cand = *iter;
2047 ++iter;
2048
2049 const char *pfx = *str;
2050 if (!pfx)
2051 {
2052 if (more || iter)
2053 pfx = _("candidates are:");
2054 else
2055 pfx = _("candidate is:");
2056 *str = get_spaces (pfx);
2057 }
2058 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2059 }
2060 }
2061
2062 /* Print the list of candidate FNS in an error message. FNS can also
2063 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2064
2065 void
2066 print_candidates (tree fns)
2067 {
2068 char *str = NULL;
2069 print_candidates_1 (fns, &str);
2070 free (str);
2071 }
2072
2073 /* Get a (possibly) constrained template declaration for the
2074 purpose of ordering candidates. */
2075 static tree
2076 get_template_for_ordering (tree list)
2077 {
2078 gcc_assert (TREE_CODE (list) == TREE_LIST);
2079 tree f = TREE_VALUE (list);
2080 if (tree ti = DECL_TEMPLATE_INFO (f))
2081 return TI_TEMPLATE (ti);
2082 return f;
2083 }
2084
2085 /* Among candidates having the same signature, return the
2086 most constrained or NULL_TREE if there is no best candidate.
2087 If the signatures of candidates vary (e.g., template
2088 specialization vs. member function), then there can be no
2089 most constrained.
2090
2091 Note that we don't compare constraints on the functions
2092 themselves, but rather those of their templates. */
2093 static tree
2094 most_constrained_function (tree candidates)
2095 {
2096 // Try to find the best candidate in a first pass.
2097 tree champ = candidates;
2098 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2099 {
2100 int winner = more_constrained (get_template_for_ordering (champ),
2101 get_template_for_ordering (c));
2102 if (winner == -1)
2103 champ = c; // The candidate is more constrained
2104 else if (winner == 0)
2105 return NULL_TREE; // Neither is more constrained
2106 }
2107
2108 // Verify that the champ is better than previous candidates.
2109 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2110 if (!more_constrained (get_template_for_ordering (champ),
2111 get_template_for_ordering (c)))
2112 return NULL_TREE;
2113 }
2114
2115 return champ;
2116 }
2117
2118
2119 /* Returns the template (one of the functions given by TEMPLATE_ID)
2120 which can be specialized to match the indicated DECL with the
2121 explicit template args given in TEMPLATE_ID. The DECL may be
2122 NULL_TREE if none is available. In that case, the functions in
2123 TEMPLATE_ID are non-members.
2124
2125 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2126 specialization of a member template.
2127
2128 The TEMPLATE_COUNT is the number of references to qualifying
2129 template classes that appeared in the name of the function. See
2130 check_explicit_specialization for a more accurate description.
2131
2132 TSK indicates what kind of template declaration (if any) is being
2133 declared. TSK_TEMPLATE indicates that the declaration given by
2134 DECL, though a FUNCTION_DECL, has template parameters, and is
2135 therefore a template function.
2136
2137 The template args (those explicitly specified and those deduced)
2138 are output in a newly created vector *TARGS_OUT.
2139
2140 If it is impossible to determine the result, an error message is
2141 issued. The error_mark_node is returned to indicate failure. */
2142
2143 static tree
2144 determine_specialization (tree template_id,
2145 tree decl,
2146 tree* targs_out,
2147 int need_member_template,
2148 int template_count,
2149 tmpl_spec_kind tsk)
2150 {
2151 tree fns;
2152 tree targs;
2153 tree explicit_targs;
2154 tree candidates = NULL_TREE;
2155
2156 /* A TREE_LIST of templates of which DECL may be a specialization.
2157 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2158 corresponding TREE_PURPOSE is the set of template arguments that,
2159 when used to instantiate the template, would produce a function
2160 with the signature of DECL. */
2161 tree templates = NULL_TREE;
2162 int header_count;
2163 cp_binding_level *b;
2164
2165 *targs_out = NULL_TREE;
2166
2167 if (template_id == error_mark_node || decl == error_mark_node)
2168 return error_mark_node;
2169
2170 /* We shouldn't be specializing a member template of an
2171 unspecialized class template; we already gave an error in
2172 check_specialization_scope, now avoid crashing. */
2173 if (!VAR_P (decl)
2174 && template_count && DECL_CLASS_SCOPE_P (decl)
2175 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2176 {
2177 gcc_assert (errorcount);
2178 return error_mark_node;
2179 }
2180
2181 fns = TREE_OPERAND (template_id, 0);
2182 explicit_targs = TREE_OPERAND (template_id, 1);
2183
2184 if (fns == error_mark_node)
2185 return error_mark_node;
2186
2187 /* Check for baselinks. */
2188 if (BASELINK_P (fns))
2189 fns = BASELINK_FUNCTIONS (fns);
2190
2191 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2192 {
2193 error_at (DECL_SOURCE_LOCATION (decl),
2194 "%qD is not a function template", fns);
2195 return error_mark_node;
2196 }
2197 else if (VAR_P (decl) && !variable_template_p (fns))
2198 {
2199 error ("%qD is not a variable template", fns);
2200 return error_mark_node;
2201 }
2202
2203 /* Count the number of template headers specified for this
2204 specialization. */
2205 header_count = 0;
2206 for (b = current_binding_level;
2207 b->kind == sk_template_parms;
2208 b = b->level_chain)
2209 ++header_count;
2210
2211 tree orig_fns = fns;
2212 bool header_mismatch = false;
2213
2214 if (variable_template_p (fns))
2215 {
2216 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2217 targs = coerce_template_parms (parms, explicit_targs, fns,
2218 tf_warning_or_error,
2219 /*req_all*/true, /*use_defarg*/true);
2220 if (targs != error_mark_node
2221 && constraints_satisfied_p (fns, targs))
2222 templates = tree_cons (targs, fns, templates);
2223 }
2224 else for (lkp_iterator iter (fns); iter; ++iter)
2225 {
2226 tree fn = *iter;
2227
2228 if (TREE_CODE (fn) == TEMPLATE_DECL)
2229 {
2230 tree decl_arg_types;
2231 tree fn_arg_types;
2232
2233 /* In case of explicit specialization, we need to check if
2234 the number of template headers appearing in the specialization
2235 is correct. This is usually done in check_explicit_specialization,
2236 but the check done there cannot be exhaustive when specializing
2237 member functions. Consider the following code:
2238
2239 template <> void A<int>::f(int);
2240 template <> template <> void A<int>::f(int);
2241
2242 Assuming that A<int> is not itself an explicit specialization
2243 already, the first line specializes "f" which is a non-template
2244 member function, whilst the second line specializes "f" which
2245 is a template member function. So both lines are syntactically
2246 correct, and check_explicit_specialization does not reject
2247 them.
2248
2249 Here, we can do better, as we are matching the specialization
2250 against the declarations. We count the number of template
2251 headers, and we check if they match TEMPLATE_COUNT + 1
2252 (TEMPLATE_COUNT is the number of qualifying template classes,
2253 plus there must be another header for the member template
2254 itself).
2255
2256 Notice that if header_count is zero, this is not a
2257 specialization but rather a template instantiation, so there
2258 is no check we can perform here. */
2259 if (header_count && header_count != template_count + 1)
2260 {
2261 header_mismatch = true;
2262 continue;
2263 }
2264
2265 /* Check that the number of template arguments at the
2266 innermost level for DECL is the same as for FN. */
2267 if (current_binding_level->kind == sk_template_parms
2268 && !current_binding_level->explicit_spec_p
2269 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2270 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2271 (current_template_parms))))
2272 continue;
2273
2274 /* DECL might be a specialization of FN. */
2275 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2276 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2277
2278 /* For a non-static member function, we need to make sure
2279 that the const qualification is the same. Since
2280 get_bindings does not try to merge the "this" parameter,
2281 we must do the comparison explicitly. */
2282 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2283 {
2284 if (!same_type_p (TREE_VALUE (fn_arg_types),
2285 TREE_VALUE (decl_arg_types)))
2286 continue;
2287
2288 /* And the ref-qualification. */
2289 if (type_memfn_rqual (TREE_TYPE (decl))
2290 != type_memfn_rqual (TREE_TYPE (fn)))
2291 continue;
2292 }
2293
2294 /* Skip the "this" parameter and, for constructors of
2295 classes with virtual bases, the VTT parameter. A
2296 full specialization of a constructor will have a VTT
2297 parameter, but a template never will. */
2298 decl_arg_types
2299 = skip_artificial_parms_for (decl, decl_arg_types);
2300 fn_arg_types
2301 = skip_artificial_parms_for (fn, fn_arg_types);
2302
2303 /* Function templates cannot be specializations; there are
2304 no partial specializations of functions. Therefore, if
2305 the type of DECL does not match FN, there is no
2306 match.
2307
2308 Note that it should never be the case that we have both
2309 candidates added here, and for regular member functions
2310 below. */
2311 if (tsk == tsk_template)
2312 {
2313 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2314 current_template_parms))
2315 continue;
2316 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2317 TREE_TYPE (TREE_TYPE (fn))))
2318 continue;
2319 if (!compparms (fn_arg_types, decl_arg_types))
2320 continue;
2321
2322 tree freq = get_trailing_function_requirements (fn);
2323 tree dreq = get_trailing_function_requirements (decl);
2324 if (!freq != !dreq)
2325 continue;
2326 if (freq)
2327 {
2328 tree fargs = DECL_TI_ARGS (fn);
2329 tsubst_flags_t complain = tf_none;
2330 freq = tsubst_constraint (freq, fargs, complain, fn);
2331 if (!cp_tree_equal (freq, dreq))
2332 continue;
2333 }
2334
2335 candidates = tree_cons (NULL_TREE, fn, candidates);
2336 continue;
2337 }
2338
2339 /* See whether this function might be a specialization of this
2340 template. Suppress access control because we might be trying
2341 to make this specialization a friend, and we have already done
2342 access control for the declaration of the specialization. */
2343 push_deferring_access_checks (dk_no_check);
2344 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2345 pop_deferring_access_checks ();
2346
2347 if (!targs)
2348 /* We cannot deduce template arguments that when used to
2349 specialize TMPL will produce DECL. */
2350 continue;
2351
2352 if (uses_template_parms (targs))
2353 /* We deduced something involving 'auto', which isn't a valid
2354 template argument. */
2355 continue;
2356
2357 /* Save this template, and the arguments deduced. */
2358 templates = tree_cons (targs, fn, templates);
2359 }
2360 else if (need_member_template)
2361 /* FN is an ordinary member function, and we need a
2362 specialization of a member template. */
2363 ;
2364 else if (TREE_CODE (fn) != FUNCTION_DECL)
2365 /* We can get IDENTIFIER_NODEs here in certain erroneous
2366 cases. */
2367 ;
2368 else if (!DECL_FUNCTION_MEMBER_P (fn))
2369 /* This is just an ordinary non-member function. Nothing can
2370 be a specialization of that. */
2371 ;
2372 else if (DECL_ARTIFICIAL (fn))
2373 /* Cannot specialize functions that are created implicitly. */
2374 ;
2375 else
2376 {
2377 tree decl_arg_types;
2378
2379 /* This is an ordinary member function. However, since
2380 we're here, we can assume its enclosing class is a
2381 template class. For example,
2382
2383 template <typename T> struct S { void f(); };
2384 template <> void S<int>::f() {}
2385
2386 Here, S<int>::f is a non-template, but S<int> is a
2387 template class. If FN has the same type as DECL, we
2388 might be in business. */
2389
2390 if (!DECL_TEMPLATE_INFO (fn))
2391 /* Its enclosing class is an explicit specialization
2392 of a template class. This is not a candidate. */
2393 continue;
2394
2395 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2396 TREE_TYPE (TREE_TYPE (fn))))
2397 /* The return types differ. */
2398 continue;
2399
2400 /* Adjust the type of DECL in case FN is a static member. */
2401 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2402 if (DECL_STATIC_FUNCTION_P (fn)
2403 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2404 decl_arg_types = TREE_CHAIN (decl_arg_types);
2405
2406 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2407 decl_arg_types))
2408 continue;
2409
2410 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2411 && (type_memfn_rqual (TREE_TYPE (decl))
2412 != type_memfn_rqual (TREE_TYPE (fn))))
2413 continue;
2414
2415 // If the deduced arguments do not satisfy the constraints,
2416 // this is not a candidate.
2417 if (flag_concepts && !constraints_satisfied_p (fn))
2418 continue;
2419
2420 // Add the candidate.
2421 candidates = tree_cons (NULL_TREE, fn, candidates);
2422 }
2423 }
2424
2425 if (templates && TREE_CHAIN (templates))
2426 {
2427 /* We have:
2428
2429 [temp.expl.spec]
2430
2431 It is possible for a specialization with a given function
2432 signature to be instantiated from more than one function
2433 template. In such cases, explicit specification of the
2434 template arguments must be used to uniquely identify the
2435 function template specialization being specialized.
2436
2437 Note that here, there's no suggestion that we're supposed to
2438 determine which of the candidate templates is most
2439 specialized. However, we, also have:
2440
2441 [temp.func.order]
2442
2443 Partial ordering of overloaded function template
2444 declarations is used in the following contexts to select
2445 the function template to which a function template
2446 specialization refers:
2447
2448 -- when an explicit specialization refers to a function
2449 template.
2450
2451 So, we do use the partial ordering rules, at least for now.
2452 This extension can only serve to make invalid programs valid,
2453 so it's safe. And, there is strong anecdotal evidence that
2454 the committee intended the partial ordering rules to apply;
2455 the EDG front end has that behavior, and John Spicer claims
2456 that the committee simply forgot to delete the wording in
2457 [temp.expl.spec]. */
2458 tree tmpl = most_specialized_instantiation (templates);
2459 if (tmpl != error_mark_node)
2460 {
2461 templates = tmpl;
2462 TREE_CHAIN (templates) = NULL_TREE;
2463 }
2464 }
2465
2466 // Concepts allows multiple declarations of member functions
2467 // with the same signature. Like above, we need to rely on
2468 // on the partial ordering of those candidates to determine which
2469 // is the best.
2470 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2471 {
2472 if (tree cand = most_constrained_function (candidates))
2473 {
2474 candidates = cand;
2475 TREE_CHAIN (cand) = NULL_TREE;
2476 }
2477 }
2478
2479 if (templates == NULL_TREE && candidates == NULL_TREE)
2480 {
2481 error ("template-id %qD for %q+D does not match any template "
2482 "declaration", template_id, decl);
2483 if (header_mismatch)
2484 inform (DECL_SOURCE_LOCATION (decl),
2485 "saw %d %<template<>%>, need %d for "
2486 "specializing a member function template",
2487 header_count, template_count + 1);
2488 print_candidates (orig_fns);
2489 return error_mark_node;
2490 }
2491 else if ((templates && TREE_CHAIN (templates))
2492 || (candidates && TREE_CHAIN (candidates))
2493 || (templates && candidates))
2494 {
2495 error ("ambiguous template specialization %qD for %q+D",
2496 template_id, decl);
2497 candidates = chainon (candidates, templates);
2498 print_candidates (candidates);
2499 return error_mark_node;
2500 }
2501
2502 /* We have one, and exactly one, match. */
2503 if (candidates)
2504 {
2505 tree fn = TREE_VALUE (candidates);
2506 *targs_out = copy_node (DECL_TI_ARGS (fn));
2507
2508 /* Propagate the candidate's constraints to the declaration. */
2509 if (tsk != tsk_template)
2510 set_constraints (decl, get_constraints (fn));
2511
2512 /* DECL is a re-declaration or partial instantiation of a template
2513 function. */
2514 if (TREE_CODE (fn) == TEMPLATE_DECL)
2515 return fn;
2516 /* It was a specialization of an ordinary member function in a
2517 template class. */
2518 return DECL_TI_TEMPLATE (fn);
2519 }
2520
2521 /* It was a specialization of a template. */
2522 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2523 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2524 {
2525 *targs_out = copy_node (targs);
2526 SET_TMPL_ARGS_LEVEL (*targs_out,
2527 TMPL_ARGS_DEPTH (*targs_out),
2528 TREE_PURPOSE (templates));
2529 }
2530 else
2531 *targs_out = TREE_PURPOSE (templates);
2532 return TREE_VALUE (templates);
2533 }
2534
2535 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2536 but with the default argument values filled in from those in the
2537 TMPL_TYPES. */
2538
2539 static tree
2540 copy_default_args_to_explicit_spec_1 (tree spec_types,
2541 tree tmpl_types)
2542 {
2543 tree new_spec_types;
2544
2545 if (!spec_types)
2546 return NULL_TREE;
2547
2548 if (spec_types == void_list_node)
2549 return void_list_node;
2550
2551 /* Substitute into the rest of the list. */
2552 new_spec_types =
2553 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2554 TREE_CHAIN (tmpl_types));
2555
2556 /* Add the default argument for this parameter. */
2557 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2558 TREE_VALUE (spec_types),
2559 new_spec_types);
2560 }
2561
2562 /* DECL is an explicit specialization. Replicate default arguments
2563 from the template it specializes. (That way, code like:
2564
2565 template <class T> void f(T = 3);
2566 template <> void f(double);
2567 void g () { f (); }
2568
2569 works, as required.) An alternative approach would be to look up
2570 the correct default arguments at the call-site, but this approach
2571 is consistent with how implicit instantiations are handled. */
2572
2573 static void
2574 copy_default_args_to_explicit_spec (tree decl)
2575 {
2576 tree tmpl;
2577 tree spec_types;
2578 tree tmpl_types;
2579 tree new_spec_types;
2580 tree old_type;
2581 tree new_type;
2582 tree t;
2583 tree object_type = NULL_TREE;
2584 tree in_charge = NULL_TREE;
2585 tree vtt = NULL_TREE;
2586
2587 /* See if there's anything we need to do. */
2588 tmpl = DECL_TI_TEMPLATE (decl);
2589 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2590 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2591 if (TREE_PURPOSE (t))
2592 break;
2593 if (!t)
2594 return;
2595
2596 old_type = TREE_TYPE (decl);
2597 spec_types = TYPE_ARG_TYPES (old_type);
2598
2599 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2600 {
2601 /* Remove the this pointer, but remember the object's type for
2602 CV quals. */
2603 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2604 spec_types = TREE_CHAIN (spec_types);
2605 tmpl_types = TREE_CHAIN (tmpl_types);
2606
2607 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2608 {
2609 /* DECL may contain more parameters than TMPL due to the extra
2610 in-charge parameter in constructors and destructors. */
2611 in_charge = spec_types;
2612 spec_types = TREE_CHAIN (spec_types);
2613 }
2614 if (DECL_HAS_VTT_PARM_P (decl))
2615 {
2616 vtt = spec_types;
2617 spec_types = TREE_CHAIN (spec_types);
2618 }
2619 }
2620
2621 /* Compute the merged default arguments. */
2622 new_spec_types =
2623 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2624
2625 /* Compute the new FUNCTION_TYPE. */
2626 if (object_type)
2627 {
2628 if (vtt)
2629 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2630 TREE_VALUE (vtt),
2631 new_spec_types);
2632
2633 if (in_charge)
2634 /* Put the in-charge parameter back. */
2635 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2636 TREE_VALUE (in_charge),
2637 new_spec_types);
2638
2639 new_type = build_method_type_directly (object_type,
2640 TREE_TYPE (old_type),
2641 new_spec_types);
2642 }
2643 else
2644 new_type = build_function_type (TREE_TYPE (old_type),
2645 new_spec_types);
2646 new_type = cp_build_type_attribute_variant (new_type,
2647 TYPE_ATTRIBUTES (old_type));
2648 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2649
2650 TREE_TYPE (decl) = new_type;
2651 }
2652
2653 /* Return the number of template headers we expect to see for a definition
2654 or specialization of CTYPE or one of its non-template members. */
2655
2656 int
2657 num_template_headers_for_class (tree ctype)
2658 {
2659 int num_templates = 0;
2660
2661 while (ctype && CLASS_TYPE_P (ctype))
2662 {
2663 /* You're supposed to have one `template <...>' for every
2664 template class, but you don't need one for a full
2665 specialization. For example:
2666
2667 template <class T> struct S{};
2668 template <> struct S<int> { void f(); };
2669 void S<int>::f () {}
2670
2671 is correct; there shouldn't be a `template <>' for the
2672 definition of `S<int>::f'. */
2673 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2674 /* If CTYPE does not have template information of any
2675 kind, then it is not a template, nor is it nested
2676 within a template. */
2677 break;
2678 if (explicit_class_specialization_p (ctype))
2679 break;
2680 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2681 ++num_templates;
2682
2683 ctype = TYPE_CONTEXT (ctype);
2684 }
2685
2686 return num_templates;
2687 }
2688
2689 /* Do a simple sanity check on the template headers that precede the
2690 variable declaration DECL. */
2691
2692 void
2693 check_template_variable (tree decl)
2694 {
2695 tree ctx = CP_DECL_CONTEXT (decl);
2696 int wanted = num_template_headers_for_class (ctx);
2697 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2698 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2699 {
2700 if (cxx_dialect < cxx14)
2701 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2702 "variable templates only available with "
2703 "%<-std=c++14%> or %<-std=gnu++14%>");
2704
2705 // Namespace-scope variable templates should have a template header.
2706 ++wanted;
2707 }
2708 if (template_header_count > wanted)
2709 {
2710 auto_diagnostic_group d;
2711 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2712 "too many template headers for %qD "
2713 "(should be %d)",
2714 decl, wanted);
2715 if (warned && CLASS_TYPE_P (ctx)
2716 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2717 inform (DECL_SOURCE_LOCATION (decl),
2718 "members of an explicitly specialized class are defined "
2719 "without a template header");
2720 }
2721 }
2722
2723 /* An explicit specialization whose declarator-id or class-head-name is not
2724 qualified shall be declared in the nearest enclosing namespace of the
2725 template, or, if the namespace is inline (7.3.1), any namespace from its
2726 enclosing namespace set.
2727
2728 If the name declared in the explicit instantiation is an unqualified name,
2729 the explicit instantiation shall appear in the namespace where its template
2730 is declared or, if that namespace is inline (7.3.1), any namespace from its
2731 enclosing namespace set. */
2732
2733 void
2734 check_unqualified_spec_or_inst (tree t, location_t loc)
2735 {
2736 tree tmpl = most_general_template (t);
2737 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2738 && !is_nested_namespace (current_namespace,
2739 CP_DECL_CONTEXT (tmpl), true))
2740 {
2741 if (processing_specialization)
2742 permerror (loc, "explicit specialization of %qD outside its "
2743 "namespace must use a nested-name-specifier", tmpl);
2744 else if (processing_explicit_instantiation
2745 && cxx_dialect >= cxx11)
2746 /* This was allowed in C++98, so only pedwarn. */
2747 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2748 "outside its namespace must use a nested-name-"
2749 "specifier", tmpl);
2750 }
2751 }
2752
2753 /* Warn for a template specialization SPEC that is missing some of a set
2754 of function or type attributes that the template TEMPL is declared with.
2755 ATTRLIST is a list of additional attributes that SPEC should be taken
2756 to ultimately be declared with. */
2757
2758 static void
2759 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2760 {
2761 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2762 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2763
2764 /* Avoid warning if the difference between the primary and
2765 the specialization is not in one of the attributes below. */
2766 const char* const blacklist[] = {
2767 "alloc_align", "alloc_size", "assume_aligned", "format",
2768 "format_arg", "malloc", "nonnull", NULL
2769 };
2770
2771 /* Put together a list of the black listed attributes that the primary
2772 template is declared with that the specialization is not, in case
2773 it's not apparent from the most recent declaration of the primary. */
2774 pretty_printer str;
2775 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2776 blacklist, &str);
2777
2778 if (!nattrs)
2779 return;
2780
2781 auto_diagnostic_group d;
2782 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2783 "explicit specialization %q#D may be missing attributes",
2784 spec))
2785 inform (DECL_SOURCE_LOCATION (tmpl),
2786 nattrs > 1
2787 ? G_("missing primary template attributes %s")
2788 : G_("missing primary template attribute %s"),
2789 pp_formatted_text (&str));
2790 }
2791
2792 /* Check to see if the function just declared, as indicated in
2793 DECLARATOR, and in DECL, is a specialization of a function
2794 template. We may also discover that the declaration is an explicit
2795 instantiation at this point.
2796
2797 Returns DECL, or an equivalent declaration that should be used
2798 instead if all goes well. Issues an error message if something is
2799 amiss. Returns error_mark_node if the error is not easily
2800 recoverable.
2801
2802 FLAGS is a bitmask consisting of the following flags:
2803
2804 2: The function has a definition.
2805 4: The function is a friend.
2806
2807 The TEMPLATE_COUNT is the number of references to qualifying
2808 template classes that appeared in the name of the function. For
2809 example, in
2810
2811 template <class T> struct S { void f(); };
2812 void S<int>::f();
2813
2814 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2815 classes are not counted in the TEMPLATE_COUNT, so that in
2816
2817 template <class T> struct S {};
2818 template <> struct S<int> { void f(); }
2819 template <> void S<int>::f();
2820
2821 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2822 invalid; there should be no template <>.)
2823
2824 If the function is a specialization, it is marked as such via
2825 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2826 is set up correctly, and it is added to the list of specializations
2827 for that template. */
2828
2829 tree
2830 check_explicit_specialization (tree declarator,
2831 tree decl,
2832 int template_count,
2833 int flags,
2834 tree attrlist)
2835 {
2836 int have_def = flags & 2;
2837 int is_friend = flags & 4;
2838 bool is_concept = flags & 8;
2839 int specialization = 0;
2840 int explicit_instantiation = 0;
2841 int member_specialization = 0;
2842 tree ctype = DECL_CLASS_CONTEXT (decl);
2843 tree dname = DECL_NAME (decl);
2844 tmpl_spec_kind tsk;
2845
2846 if (is_friend)
2847 {
2848 if (!processing_specialization)
2849 tsk = tsk_none;
2850 else
2851 tsk = tsk_excessive_parms;
2852 }
2853 else
2854 tsk = current_tmpl_spec_kind (template_count);
2855
2856 switch (tsk)
2857 {
2858 case tsk_none:
2859 if (processing_specialization && !VAR_P (decl))
2860 {
2861 specialization = 1;
2862 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2863 }
2864 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2865 {
2866 if (is_friend)
2867 /* This could be something like:
2868
2869 template <class T> void f(T);
2870 class S { friend void f<>(int); } */
2871 specialization = 1;
2872 else
2873 {
2874 /* This case handles bogus declarations like template <>
2875 template <class T> void f<int>(); */
2876
2877 error_at (cp_expr_loc_or_input_loc (declarator),
2878 "template-id %qE in declaration of primary template",
2879 declarator);
2880 return decl;
2881 }
2882 }
2883 break;
2884
2885 case tsk_invalid_member_spec:
2886 /* The error has already been reported in
2887 check_specialization_scope. */
2888 return error_mark_node;
2889
2890 case tsk_invalid_expl_inst:
2891 error ("template parameter list used in explicit instantiation");
2892
2893 /* Fall through. */
2894
2895 case tsk_expl_inst:
2896 if (have_def)
2897 error ("definition provided for explicit instantiation");
2898
2899 explicit_instantiation = 1;
2900 break;
2901
2902 case tsk_excessive_parms:
2903 case tsk_insufficient_parms:
2904 if (tsk == tsk_excessive_parms)
2905 error ("too many template parameter lists in declaration of %qD",
2906 decl);
2907 else if (template_header_count)
2908 error("too few template parameter lists in declaration of %qD", decl);
2909 else
2910 error("explicit specialization of %qD must be introduced by "
2911 "%<template <>%>", decl);
2912
2913 /* Fall through. */
2914 case tsk_expl_spec:
2915 if (is_concept)
2916 error ("explicit specialization declared %<concept%>");
2917
2918 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2919 /* In cases like template<> constexpr bool v = true;
2920 We'll give an error in check_template_variable. */
2921 break;
2922
2923 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2924 if (ctype)
2925 member_specialization = 1;
2926 else
2927 specialization = 1;
2928 break;
2929
2930 case tsk_template:
2931 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2932 {
2933 /* This case handles bogus declarations like template <>
2934 template <class T> void f<int>(); */
2935
2936 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2937 error_at (cp_expr_loc_or_input_loc (declarator),
2938 "template-id %qE in declaration of primary template",
2939 declarator);
2940 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2941 {
2942 /* Partial specialization of variable template. */
2943 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2944 specialization = 1;
2945 goto ok;
2946 }
2947 else if (cxx_dialect < cxx14)
2948 error_at (cp_expr_loc_or_input_loc (declarator),
2949 "non-type partial specialization %qE "
2950 "is not allowed", declarator);
2951 else
2952 error_at (cp_expr_loc_or_input_loc (declarator),
2953 "non-class, non-variable partial specialization %qE "
2954 "is not allowed", declarator);
2955 return decl;
2956 ok:;
2957 }
2958
2959 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2960 /* This is a specialization of a member template, without
2961 specialization the containing class. Something like:
2962
2963 template <class T> struct S {
2964 template <class U> void f (U);
2965 };
2966 template <> template <class U> void S<int>::f(U) {}
2967
2968 That's a specialization -- but of the entire template. */
2969 specialization = 1;
2970 break;
2971
2972 default:
2973 gcc_unreachable ();
2974 }
2975
2976 if ((specialization || member_specialization)
2977 /* This doesn't apply to variable templates. */
2978 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2979 {
2980 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2981 for (; t; t = TREE_CHAIN (t))
2982 if (TREE_PURPOSE (t))
2983 {
2984 permerror (input_location,
2985 "default argument specified in explicit specialization");
2986 break;
2987 }
2988 }
2989
2990 if (specialization || member_specialization || explicit_instantiation)
2991 {
2992 tree tmpl = NULL_TREE;
2993 tree targs = NULL_TREE;
2994 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2995 bool found_hidden = false;
2996
2997 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2998 if (!was_template_id)
2999 {
3000 tree fns;
3001
3002 gcc_assert (identifier_p (declarator));
3003 if (ctype)
3004 fns = dname;
3005 else
3006 {
3007 /* If there is no class context, the explicit instantiation
3008 must be at namespace scope. */
3009 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3010
3011 /* Find the namespace binding, using the declaration
3012 context. */
3013 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3014 LOOK_want::NORMAL, true);
3015 if (fns == error_mark_node)
3016 {
3017 /* If lookup fails, look for a friend declaration so we can
3018 give a better diagnostic. */
3019 fns = (lookup_qualified_name
3020 (CP_DECL_CONTEXT (decl), dname,
3021 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3022 /*complain*/true));
3023 found_hidden = true;
3024 }
3025
3026 if (fns == error_mark_node || !is_overloaded_fn (fns))
3027 {
3028 error ("%qD is not a template function", dname);
3029 fns = error_mark_node;
3030 }
3031 }
3032
3033 declarator = lookup_template_function (fns, NULL_TREE);
3034 }
3035
3036 if (declarator == error_mark_node)
3037 return error_mark_node;
3038
3039 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3040 {
3041 if (!explicit_instantiation)
3042 /* A specialization in class scope. This is invalid,
3043 but the error will already have been flagged by
3044 check_specialization_scope. */
3045 return error_mark_node;
3046 else
3047 {
3048 /* It's not valid to write an explicit instantiation in
3049 class scope, e.g.:
3050
3051 class C { template void f(); }
3052
3053 This case is caught by the parser. However, on
3054 something like:
3055
3056 template class C { void f(); };
3057
3058 (which is invalid) we can get here. The error will be
3059 issued later. */
3060 ;
3061 }
3062
3063 return decl;
3064 }
3065 else if (ctype != NULL_TREE
3066 && (identifier_p (TREE_OPERAND (declarator, 0))))
3067 {
3068 // We'll match variable templates in start_decl.
3069 if (VAR_P (decl))
3070 return decl;
3071
3072 /* Find the list of functions in ctype that have the same
3073 name as the declared function. */
3074 tree name = TREE_OPERAND (declarator, 0);
3075
3076 if (constructor_name_p (name, ctype))
3077 {
3078 if (DECL_CONSTRUCTOR_P (decl)
3079 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3080 : !CLASSTYPE_DESTRUCTOR (ctype))
3081 {
3082 /* From [temp.expl.spec]:
3083
3084 If such an explicit specialization for the member
3085 of a class template names an implicitly-declared
3086 special member function (clause _special_), the
3087 program is ill-formed.
3088
3089 Similar language is found in [temp.explicit]. */
3090 error ("specialization of implicitly-declared special member function");
3091 return error_mark_node;
3092 }
3093
3094 name = DECL_NAME (decl);
3095 }
3096
3097 /* For a type-conversion operator, We might be looking for
3098 `operator int' which will be a specialization of
3099 `operator T'. Grab all the conversion operators, and
3100 then select from them. */
3101 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3102 ? conv_op_identifier : name);
3103
3104 if (fns == NULL_TREE)
3105 {
3106 error ("no member function %qD declared in %qT", name, ctype);
3107 return error_mark_node;
3108 }
3109 else
3110 TREE_OPERAND (declarator, 0) = fns;
3111 }
3112
3113 /* Figure out what exactly is being specialized at this point.
3114 Note that for an explicit instantiation, even one for a
3115 member function, we cannot tell a priori whether the
3116 instantiation is for a member template, or just a member
3117 function of a template class. Even if a member template is
3118 being instantiated, the member template arguments may be
3119 elided if they can be deduced from the rest of the
3120 declaration. */
3121 tmpl = determine_specialization (declarator, decl,
3122 &targs,
3123 member_specialization,
3124 template_count,
3125 tsk);
3126
3127 if (!tmpl || tmpl == error_mark_node)
3128 /* We couldn't figure out what this declaration was
3129 specializing. */
3130 return error_mark_node;
3131 else
3132 {
3133 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3134 {
3135 auto_diagnostic_group d;
3136 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3137 "friend declaration %qD is not visible to "
3138 "explicit specialization", tmpl))
3139 inform (DECL_SOURCE_LOCATION (tmpl),
3140 "friend declaration here");
3141 }
3142
3143 if (!ctype && !is_friend
3144 && CP_DECL_CONTEXT (decl) == current_namespace)
3145 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3146
3147 tree gen_tmpl = most_general_template (tmpl);
3148
3149 if (explicit_instantiation)
3150 {
3151 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3152 is done by do_decl_instantiation later. */
3153
3154 int arg_depth = TMPL_ARGS_DEPTH (targs);
3155 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3156
3157 if (arg_depth > parm_depth)
3158 {
3159 /* If TMPL is not the most general template (for
3160 example, if TMPL is a friend template that is
3161 injected into namespace scope), then there will
3162 be too many levels of TARGS. Remove some of them
3163 here. */
3164 int i;
3165 tree new_targs;
3166
3167 new_targs = make_tree_vec (parm_depth);
3168 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3169 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3170 = TREE_VEC_ELT (targs, i);
3171 targs = new_targs;
3172 }
3173
3174 return instantiate_template (tmpl, targs, tf_error);
3175 }
3176
3177 /* If we thought that the DECL was a member function, but it
3178 turns out to be specializing a static member function,
3179 make DECL a static member function as well. */
3180 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3181 && DECL_STATIC_FUNCTION_P (tmpl)
3182 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3183 revert_static_member_fn (decl);
3184
3185 /* If this is a specialization of a member template of a
3186 template class, we want to return the TEMPLATE_DECL, not
3187 the specialization of it. */
3188 if (tsk == tsk_template && !was_template_id)
3189 {
3190 tree result = DECL_TEMPLATE_RESULT (tmpl);
3191 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3192 DECL_INITIAL (result) = NULL_TREE;
3193 if (have_def)
3194 {
3195 tree parm;
3196 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3197 DECL_SOURCE_LOCATION (result)
3198 = DECL_SOURCE_LOCATION (decl);
3199 /* We want to use the argument list specified in the
3200 definition, not in the original declaration. */
3201 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3202 for (parm = DECL_ARGUMENTS (result); parm;
3203 parm = DECL_CHAIN (parm))
3204 DECL_CONTEXT (parm) = result;
3205 }
3206 return register_specialization (tmpl, gen_tmpl, targs,
3207 is_friend, 0);
3208 }
3209
3210 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3211 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3212
3213 if (was_template_id)
3214 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3215
3216 /* Inherit default function arguments from the template
3217 DECL is specializing. */
3218 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3219 copy_default_args_to_explicit_spec (decl);
3220
3221 /* This specialization has the same protection as the
3222 template it specializes. */
3223 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3224 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3225
3226 /* 7.1.1-1 [dcl.stc]
3227
3228 A storage-class-specifier shall not be specified in an
3229 explicit specialization...
3230
3231 The parser rejects these, so unless action is taken here,
3232 explicit function specializations will always appear with
3233 global linkage.
3234
3235 The action recommended by the C++ CWG in response to C++
3236 defect report 605 is to make the storage class and linkage
3237 of the explicit specialization match the templated function:
3238
3239 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3240 */
3241 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3242 {
3243 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3244 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3245
3246 /* A concept cannot be specialized. */
3247 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3248 {
3249 error ("explicit specialization of function concept %qD",
3250 gen_tmpl);
3251 return error_mark_node;
3252 }
3253
3254 /* This specialization has the same linkage and visibility as
3255 the function template it specializes. */
3256 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3257 if (! TREE_PUBLIC (decl))
3258 {
3259 DECL_INTERFACE_KNOWN (decl) = 1;
3260 DECL_NOT_REALLY_EXTERN (decl) = 1;
3261 }
3262 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3263 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3264 {
3265 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3266 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3267 }
3268 }
3269
3270 /* If DECL is a friend declaration, declared using an
3271 unqualified name, the namespace associated with DECL may
3272 have been set incorrectly. For example, in:
3273
3274 template <typename T> void f(T);
3275 namespace N {
3276 struct S { friend void f<int>(int); }
3277 }
3278
3279 we will have set the DECL_CONTEXT for the friend
3280 declaration to N, rather than to the global namespace. */
3281 if (DECL_NAMESPACE_SCOPE_P (decl))
3282 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3283
3284 if (is_friend && !have_def)
3285 /* This is not really a declaration of a specialization.
3286 It's just the name of an instantiation. But, it's not
3287 a request for an instantiation, either. */
3288 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3289 else if (TREE_CODE (decl) == FUNCTION_DECL)
3290 /* A specialization is not necessarily COMDAT. */
3291 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3292 && DECL_DECLARED_INLINE_P (decl));
3293 else if (VAR_P (decl))
3294 DECL_COMDAT (decl) = false;
3295
3296 /* If this is a full specialization, register it so that we can find
3297 it again. Partial specializations will be registered in
3298 process_partial_specialization. */
3299 if (!processing_template_decl)
3300 {
3301 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3302
3303 decl = register_specialization (decl, gen_tmpl, targs,
3304 is_friend, 0);
3305 }
3306
3307
3308 /* A 'structor should already have clones. */
3309 gcc_assert (decl == error_mark_node
3310 || variable_template_p (tmpl)
3311 || !(DECL_CONSTRUCTOR_P (decl)
3312 || DECL_DESTRUCTOR_P (decl))
3313 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3314 }
3315 }
3316
3317 return decl;
3318 }
3319
3320 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3321 parameters. These are represented in the same format used for
3322 DECL_TEMPLATE_PARMS. */
3323
3324 int
3325 comp_template_parms (const_tree parms1, const_tree parms2)
3326 {
3327 const_tree p1;
3328 const_tree p2;
3329
3330 if (parms1 == parms2)
3331 return 1;
3332
3333 for (p1 = parms1, p2 = parms2;
3334 p1 != NULL_TREE && p2 != NULL_TREE;
3335 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3336 {
3337 tree t1 = TREE_VALUE (p1);
3338 tree t2 = TREE_VALUE (p2);
3339 int i;
3340
3341 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3342 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3343
3344 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3345 return 0;
3346
3347 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3348 {
3349 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3350 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3351
3352 /* If either of the template parameters are invalid, assume
3353 they match for the sake of error recovery. */
3354 if (error_operand_p (parm1) || error_operand_p (parm2))
3355 return 1;
3356
3357 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3358 return 0;
3359
3360 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3361 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3362 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3363 continue;
3364 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3365 return 0;
3366 }
3367 }
3368
3369 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3370 /* One set of parameters has more parameters lists than the
3371 other. */
3372 return 0;
3373
3374 return 1;
3375 }
3376
3377 /* Returns true if two template parameters are declared with
3378 equivalent constraints. */
3379
3380 static bool
3381 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3382 {
3383 tree req1 = TREE_TYPE (parm1);
3384 tree req2 = TREE_TYPE (parm2);
3385 if (!req1 != !req2)
3386 return false;
3387 if (req1)
3388 return cp_tree_equal (req1, req2);
3389 return true;
3390 }
3391
3392 /* Returns true when two template parameters are equivalent. */
3393
3394 static bool
3395 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3396 {
3397 tree decl1 = TREE_VALUE (parm1);
3398 tree decl2 = TREE_VALUE (parm2);
3399
3400 /* If either of the template parameters are invalid, assume
3401 they match for the sake of error recovery. */
3402 if (error_operand_p (decl1) || error_operand_p (decl2))
3403 return true;
3404
3405 /* ... they declare parameters of the same kind. */
3406 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3407 return false;
3408
3409 /* ... one parameter was introduced by a parameter declaration, then
3410 both are. This case arises as a result of eagerly rewriting declarations
3411 during parsing. */
3412 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3413 return false;
3414
3415 /* ... if either declares a pack, they both do. */
3416 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3417 return false;
3418
3419 if (TREE_CODE (decl1) == PARM_DECL)
3420 {
3421 /* ... if they declare non-type parameters, the types are equivalent. */
3422 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3423 return false;
3424 }
3425 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3426 {
3427 /* ... if they declare template template parameters, their template
3428 parameter lists are equivalent. */
3429 if (!template_heads_equivalent_p (decl1, decl2))
3430 return false;
3431 }
3432
3433 /* ... if they are declared with a qualified-concept name, they both
3434 are, and those names are equivalent. */
3435 return template_parameter_constraints_equivalent_p (parm1, parm2);
3436 }
3437
3438 /* Returns true if two template parameters lists are equivalent.
3439 Two template parameter lists are equivalent if they have the
3440 same length and their corresponding parameters are equivalent.
3441
3442 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3443 data structure returned by DECL_TEMPLATE_PARMS.
3444
3445 This is generally the same implementation as comp_template_parms
3446 except that it also the concept names and arguments used to
3447 introduce parameters. */
3448
3449 static bool
3450 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3451 {
3452 if (parms1 == parms2)
3453 return true;
3454
3455 const_tree p1 = parms1;
3456 const_tree p2 = parms2;
3457 while (p1 != NULL_TREE && p2 != NULL_TREE)
3458 {
3459 tree list1 = TREE_VALUE (p1);
3460 tree list2 = TREE_VALUE (p2);
3461
3462 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3463 return 0;
3464
3465 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3466 {
3467 tree parm1 = TREE_VEC_ELT (list1, i);
3468 tree parm2 = TREE_VEC_ELT (list2, i);
3469 if (!template_parameters_equivalent_p (parm1, parm2))
3470 return false;
3471 }
3472
3473 p1 = TREE_CHAIN (p1);
3474 p2 = TREE_CHAIN (p2);
3475 }
3476
3477 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3478 return false;
3479
3480 return true;
3481 }
3482
3483 /* Return true if the requires-clause of the template parameter lists are
3484 equivalent and false otherwise. */
3485 static bool
3486 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3487 {
3488 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3489 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3490 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3491 return false;
3492 if (!cp_tree_equal (req1, req2))
3493 return false;
3494 return true;
3495 }
3496
3497 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3498 Two template heads are equivalent if their template parameter
3499 lists are equivalent and their requires clauses are equivalent.
3500
3501 In pre-C++20, this is equivalent to calling comp_template_parms
3502 for the template parameters of TMPL1 and TMPL2. */
3503
3504 bool
3505 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3506 {
3507 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3508 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3509
3510 /* Don't change the matching rules for pre-C++20. */
3511 if (cxx_dialect < cxx20)
3512 return comp_template_parms (parms1, parms2);
3513
3514 /* ... have the same number of template parameters, and their
3515 corresponding parameters are equivalent. */
3516 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3517 return false;
3518
3519 /* ... if either has a requires-clause, they both do and their
3520 corresponding constraint-expressions are equivalent. */
3521 return template_requirements_equivalent_p (parms1, parms2);
3522 }
3523
3524 /* Determine whether PARM is a parameter pack. */
3525
3526 bool
3527 template_parameter_pack_p (const_tree parm)
3528 {
3529 /* Determine if we have a non-type template parameter pack. */
3530 if (TREE_CODE (parm) == PARM_DECL)
3531 return (DECL_TEMPLATE_PARM_P (parm)
3532 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3533 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3534 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3535
3536 /* If this is a list of template parameters, we could get a
3537 TYPE_DECL or a TEMPLATE_DECL. */
3538 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3539 parm = TREE_TYPE (parm);
3540
3541 /* Otherwise it must be a type template parameter. */
3542 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3543 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3544 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3545 }
3546
3547 /* Determine if T is a function parameter pack. */
3548
3549 bool
3550 function_parameter_pack_p (const_tree t)
3551 {
3552 if (t && TREE_CODE (t) == PARM_DECL)
3553 return DECL_PACK_P (t);
3554 return false;
3555 }
3556
3557 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3558 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3559
3560 tree
3561 get_function_template_decl (const_tree primary_func_tmpl_inst)
3562 {
3563 if (! primary_func_tmpl_inst
3564 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3565 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3566 return NULL;
3567
3568 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3569 }
3570
3571 /* Return true iff the function parameter PARAM_DECL was expanded
3572 from the function parameter pack PACK. */
3573
3574 bool
3575 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3576 {
3577 if (DECL_ARTIFICIAL (param_decl)
3578 || !function_parameter_pack_p (pack))
3579 return false;
3580
3581 /* The parameter pack and its pack arguments have the same
3582 DECL_PARM_INDEX. */
3583 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3584 }
3585
3586 /* Determine whether ARGS describes a variadic template args list,
3587 i.e., one that is terminated by a template argument pack. */
3588
3589 static bool
3590 template_args_variadic_p (tree args)
3591 {
3592 int nargs;
3593 tree last_parm;
3594
3595 if (args == NULL_TREE)
3596 return false;
3597
3598 args = INNERMOST_TEMPLATE_ARGS (args);
3599 nargs = TREE_VEC_LENGTH (args);
3600
3601 if (nargs == 0)
3602 return false;
3603
3604 last_parm = TREE_VEC_ELT (args, nargs - 1);
3605
3606 return ARGUMENT_PACK_P (last_parm);
3607 }
3608
3609 /* Generate a new name for the parameter pack name NAME (an
3610 IDENTIFIER_NODE) that incorporates its */
3611
3612 static tree
3613 make_ith_pack_parameter_name (tree name, int i)
3614 {
3615 /* Munge the name to include the parameter index. */
3616 #define NUMBUF_LEN 128
3617 char numbuf[NUMBUF_LEN];
3618 char* newname;
3619 int newname_len;
3620
3621 if (name == NULL_TREE)
3622 return name;
3623 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3624 newname_len = IDENTIFIER_LENGTH (name)
3625 + strlen (numbuf) + 2;
3626 newname = (char*)alloca (newname_len);
3627 snprintf (newname, newname_len,
3628 "%s#%i", IDENTIFIER_POINTER (name), i);
3629 return get_identifier (newname);
3630 }
3631
3632 /* Return true if T is a primary function, class or alias template
3633 specialization, not including the template pattern. */
3634
3635 bool
3636 primary_template_specialization_p (const_tree t)
3637 {
3638 if (!t)
3639 return false;
3640
3641 if (VAR_OR_FUNCTION_DECL_P (t))
3642 return (DECL_LANG_SPECIFIC (t)
3643 && DECL_USE_TEMPLATE (t)
3644 && DECL_TEMPLATE_INFO (t)
3645 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3646 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3647 return (CLASSTYPE_TEMPLATE_INFO (t)
3648 && CLASSTYPE_USE_TEMPLATE (t)
3649 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3650 else if (alias_template_specialization_p (t, nt_transparent))
3651 return true;
3652 return false;
3653 }
3654
3655 /* Return true if PARM is a template template parameter. */
3656
3657 bool
3658 template_template_parameter_p (const_tree parm)
3659 {
3660 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3661 }
3662
3663 /* Return true iff PARM is a DECL representing a type template
3664 parameter. */
3665
3666 bool
3667 template_type_parameter_p (const_tree parm)
3668 {
3669 return (parm
3670 && (TREE_CODE (parm) == TYPE_DECL
3671 || TREE_CODE (parm) == TEMPLATE_DECL)
3672 && DECL_TEMPLATE_PARM_P (parm));
3673 }
3674
3675 /* Return the template parameters of T if T is a
3676 primary template instantiation, NULL otherwise. */
3677
3678 tree
3679 get_primary_template_innermost_parameters (const_tree t)
3680 {
3681 tree parms = NULL, template_info = NULL;
3682
3683 if ((template_info = get_template_info (t))
3684 && primary_template_specialization_p (t))
3685 parms = INNERMOST_TEMPLATE_PARMS
3686 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3687
3688 return parms;
3689 }
3690
3691 /* Returns the template arguments of T if T is a template instantiation,
3692 NULL otherwise. */
3693
3694 tree
3695 get_template_innermost_arguments (const_tree t)
3696 {
3697 tree args = NULL, template_info = NULL;
3698
3699 if ((template_info = get_template_info (t))
3700 && TI_ARGS (template_info))
3701 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3702
3703 return args;
3704 }
3705
3706 /* Return the argument pack elements of T if T is a template argument pack,
3707 NULL otherwise. */
3708
3709 tree
3710 get_template_argument_pack_elems (const_tree t)
3711 {
3712 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3713 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3714 return NULL;
3715
3716 return ARGUMENT_PACK_ARGS (t);
3717 }
3718
3719 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3720 ARGUMENT_PACK_SELECT represents. */
3721
3722 static tree
3723 argument_pack_select_arg (tree t)
3724 {
3725 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3726 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3727
3728 /* If the selected argument is an expansion E, that most likely means we were
3729 called from gen_elem_of_pack_expansion_instantiation during the
3730 substituting of an argument pack (of which the Ith element is a pack
3731 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3732 In this case, the Ith element resulting from this substituting is going to
3733 be a pack expansion, which pattern is the pattern of E. Let's return the
3734 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3735 resulting pack expansion from it. */
3736 if (PACK_EXPANSION_P (arg))
3737 {
3738 /* Make sure we aren't throwing away arg info. */
3739 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3740 arg = PACK_EXPANSION_PATTERN (arg);
3741 }
3742
3743 return arg;
3744 }
3745
3746 /* Return a modification of ARGS that's suitable for preserving inside a hash
3747 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3748 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3749
3750 static tree
3751 preserve_args (tree args, bool cow_p = true)
3752 {
3753 if (!args)
3754 return NULL_TREE;
3755
3756 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
3757 {
3758 tree t = TREE_VEC_ELT (args, i);
3759 tree r;
3760 if (!t)
3761 r = NULL_TREE;
3762 else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
3763 r = argument_pack_select_arg (t);
3764 else if (TREE_CODE (t) == TREE_VEC)
3765 r = preserve_args (t, cow_p);
3766 else
3767 r = t;
3768 if (r != t)
3769 {
3770 if (cow_p)
3771 {
3772 args = copy_template_args (args);
3773 cow_p = false;
3774 }
3775 TREE_VEC_ELT (args, i) = r;
3776 }
3777 }
3778
3779 return args;
3780 }
3781
3782 /* True iff FN is a function representing a built-in variadic parameter
3783 pack. */
3784
3785 bool
3786 builtin_pack_fn_p (tree fn)
3787 {
3788 if (!fn
3789 || TREE_CODE (fn) != FUNCTION_DECL
3790 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3791 return false;
3792
3793 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3794 return true;
3795
3796 return false;
3797 }
3798
3799 /* True iff CALL is a call to a function representing a built-in variadic
3800 parameter pack. */
3801
3802 static bool
3803 builtin_pack_call_p (tree call)
3804 {
3805 if (TREE_CODE (call) != CALL_EXPR)
3806 return false;
3807 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3808 }
3809
3810 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3811
3812 static tree
3813 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3814 tree in_decl)
3815 {
3816 tree ohi = CALL_EXPR_ARG (call, 0);
3817 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3818 false/*fn*/, true/*int_cst*/);
3819
3820 if (value_dependent_expression_p (hi))
3821 {
3822 if (hi != ohi)
3823 {
3824 call = copy_node (call);
3825 CALL_EXPR_ARG (call, 0) = hi;
3826 }
3827 tree ex = make_pack_expansion (call, complain);
3828 tree vec = make_tree_vec (1);
3829 TREE_VEC_ELT (vec, 0) = ex;
3830 return vec;
3831 }
3832 else
3833 {
3834 hi = instantiate_non_dependent_expr_sfinae (hi, complain);
3835 hi = cxx_constant_value (hi);
3836 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3837
3838 /* Calculate the largest value of len that won't make the size of the vec
3839 overflow an int. The compiler will exceed resource limits long before
3840 this, but it seems a decent place to diagnose. */
3841 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3842
3843 if (len < 0 || len > max)
3844 {
3845 if ((complain & tf_error)
3846 && hi != error_mark_node)
3847 error ("argument to %<__integer_pack%> must be between 0 and %d",
3848 max);
3849 return error_mark_node;
3850 }
3851
3852 tree vec = make_tree_vec (len);
3853
3854 for (int i = 0; i < len; ++i)
3855 TREE_VEC_ELT (vec, i) = size_int (i);
3856
3857 return vec;
3858 }
3859 }
3860
3861 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3862 CALL. */
3863
3864 static tree
3865 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3866 tree in_decl)
3867 {
3868 if (!builtin_pack_call_p (call))
3869 return NULL_TREE;
3870
3871 tree fn = CALL_EXPR_FN (call);
3872
3873 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3874 return expand_integer_pack (call, args, complain, in_decl);
3875
3876 return NULL_TREE;
3877 }
3878
3879 /* Return true if the tree T has the extra args mechanism for
3880 avoiding partial instantiation. */
3881
3882 static bool
3883 has_extra_args_mechanism_p (const_tree t)
3884 {
3885 return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */
3886 || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */
3887 || (TREE_CODE (t) == IF_STMT
3888 && IF_STMT_CONSTEXPR_P (t))); /* IF_STMT_EXTRA_ARGS */
3889 }
3890
3891 /* Structure used to track the progress of find_parameter_packs_r. */
3892 struct find_parameter_pack_data
3893 {
3894 /* TREE_LIST that will contain all of the parameter packs found by
3895 the traversal. */
3896 tree* parameter_packs;
3897
3898 /* Set of AST nodes that have been visited by the traversal. */
3899 hash_set<tree> *visited;
3900
3901 /* True iff we're making a type pack expansion. */
3902 bool type_pack_expansion_p;
3903
3904 /* True iff we found a subtree that has the extra args mechanism. */
3905 bool found_extra_args_tree_p = false;
3906 };
3907
3908 /* Identifies all of the argument packs that occur in a template
3909 argument and appends them to the TREE_LIST inside DATA, which is a
3910 find_parameter_pack_data structure. This is a subroutine of
3911 make_pack_expansion and uses_parameter_packs. */
3912 static tree
3913 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3914 {
3915 tree t = *tp;
3916 struct find_parameter_pack_data* ppd =
3917 (struct find_parameter_pack_data*)data;
3918 bool parameter_pack_p = false;
3919
3920 #define WALK_SUBTREE(NODE) \
3921 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3922 ppd, ppd->visited) \
3923
3924 /* Don't look through typedefs; we are interested in whether a
3925 parameter pack is actually written in the expression/type we're
3926 looking at, not the target type. */
3927 if (TYPE_P (t) && typedef_variant_p (t))
3928 {
3929 /* But do look at arguments for an alias template. */
3930 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3931 cp_walk_tree (&TI_ARGS (tinfo),
3932 &find_parameter_packs_r,
3933 ppd, ppd->visited);
3934 *walk_subtrees = 0;
3935 return NULL_TREE;
3936 }
3937
3938 /* Identify whether this is a parameter pack or not. */
3939 switch (TREE_CODE (t))
3940 {
3941 case TEMPLATE_PARM_INDEX:
3942 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3943 parameter_pack_p = true;
3944 break;
3945
3946 case TEMPLATE_TYPE_PARM:
3947 t = TYPE_MAIN_VARIANT (t);
3948 /* FALLTHRU */
3949 case TEMPLATE_TEMPLATE_PARM:
3950 /* If the placeholder appears in the decl-specifier-seq of a function
3951 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3952 is a pack expansion, the invented template parameter is a template
3953 parameter pack. */
3954 if (ppd->type_pack_expansion_p && is_auto (t))
3955 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3956 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3957 parameter_pack_p = true;
3958 break;
3959
3960 case FIELD_DECL:
3961 case PARM_DECL:
3962 if (DECL_PACK_P (t))
3963 {
3964 /* We don't want to walk into the type of a PARM_DECL,
3965 because we don't want to see the type parameter pack. */
3966 *walk_subtrees = 0;
3967 parameter_pack_p = true;
3968 }
3969 break;
3970
3971 case VAR_DECL:
3972 if (DECL_PACK_P (t))
3973 {
3974 /* We don't want to walk into the type of a variadic capture proxy,
3975 because we don't want to see the type parameter pack. */
3976 *walk_subtrees = 0;
3977 parameter_pack_p = true;
3978 }
3979 else if (variable_template_specialization_p (t))
3980 {
3981 cp_walk_tree (&DECL_TI_ARGS (t),
3982 find_parameter_packs_r,
3983 ppd, ppd->visited);
3984 *walk_subtrees = 0;
3985 }
3986 break;
3987
3988 case CALL_EXPR:
3989 if (builtin_pack_call_p (t))
3990 parameter_pack_p = true;
3991 break;
3992
3993 case BASES:
3994 parameter_pack_p = true;
3995 break;
3996 default:
3997 /* Not a parameter pack. */
3998 break;
3999 }
4000
4001 if (parameter_pack_p)
4002 {
4003 /* Add this parameter pack to the list. */
4004 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
4005 }
4006
4007 if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
4008 ppd->found_extra_args_tree_p = true;
4009
4010 if (TYPE_P (t))
4011 cp_walk_tree (&TYPE_CONTEXT (t),
4012 &find_parameter_packs_r, ppd, ppd->visited);
4013
4014 /* This switch statement will return immediately if we don't find a
4015 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4016 switch (TREE_CODE (t))
4017 {
4018 case BOUND_TEMPLATE_TEMPLATE_PARM:
4019 /* Check the template itself. */
4020 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
4021 &find_parameter_packs_r, ppd, ppd->visited);
4022 return NULL_TREE;
4023
4024 case DECL_EXPR:
4025 {
4026 tree decl = DECL_EXPR_DECL (t);
4027 /* Ignore the declaration of a capture proxy for a parameter pack. */
4028 if (is_capture_proxy (decl))
4029 *walk_subtrees = 0;
4030 if (is_typedef_decl (decl))
4031 /* Since we stop at typedefs above, we need to look through them at
4032 the point of the DECL_EXPR. */
4033 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
4034 &find_parameter_packs_r, ppd, ppd->visited);
4035 return NULL_TREE;
4036 }
4037
4038 case TEMPLATE_DECL:
4039 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4040 return NULL_TREE;
4041 cp_walk_tree (&TREE_TYPE (t),
4042 &find_parameter_packs_r, ppd, ppd->visited);
4043 return NULL_TREE;
4044
4045 case TYPE_PACK_EXPANSION:
4046 case EXPR_PACK_EXPANSION:
4047 *walk_subtrees = 0;
4048 return NULL_TREE;
4049
4050 case INTEGER_TYPE:
4051 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4052 ppd, ppd->visited);
4053 *walk_subtrees = 0;
4054 return NULL_TREE;
4055
4056 case IDENTIFIER_NODE:
4057 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4058 ppd->visited);
4059 *walk_subtrees = 0;
4060 return NULL_TREE;
4061
4062 case LAMBDA_EXPR:
4063 {
4064 /* Since we defer implicit capture, look in the parms and body. */
4065 tree fn = lambda_function (t);
4066 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4067 ppd->visited);
4068 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4069 ppd->visited);
4070 return NULL_TREE;
4071 }
4072
4073 case DECLTYPE_TYPE:
4074 {
4075 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4076 type_pack_expansion_p to false so that any placeholders
4077 within the expression don't get marked as parameter packs. */
4078 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4079 ppd->type_pack_expansion_p = false;
4080 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4081 ppd, ppd->visited);
4082 ppd->type_pack_expansion_p = type_pack_expansion_p;
4083 *walk_subtrees = 0;
4084 return NULL_TREE;
4085 }
4086
4087 case IF_STMT:
4088 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4089 ppd, ppd->visited);
4090 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4091 ppd, ppd->visited);
4092 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4093 ppd, ppd->visited);
4094 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4095 *walk_subtrees = 0;
4096 return NULL_TREE;
4097
4098 case TAG_DEFN:
4099 t = TREE_TYPE (t);
4100 if (CLASS_TYPE_P (t))
4101 /* Local class, need to look through the whole definition. */
4102 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4103 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4104 ppd, ppd->visited);
4105 else
4106 /* Enum, look at the values. */
4107 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4108 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4109 &find_parameter_packs_r,
4110 ppd, ppd->visited);
4111 return NULL_TREE;
4112
4113 case FUNCTION_TYPE:
4114 case METHOD_TYPE:
4115 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4116 break;
4117
4118 default:
4119 return NULL_TREE;
4120 }
4121
4122 #undef WALK_SUBTREE
4123
4124 return NULL_TREE;
4125 }
4126
4127 /* Determines if the expression or type T uses any parameter packs. */
4128 tree
4129 uses_parameter_packs (tree t)
4130 {
4131 tree parameter_packs = NULL_TREE;
4132 struct find_parameter_pack_data ppd;
4133 ppd.parameter_packs = &parameter_packs;
4134 ppd.visited = new hash_set<tree>;
4135 ppd.type_pack_expansion_p = false;
4136 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4137 delete ppd.visited;
4138 return parameter_packs;
4139 }
4140
4141 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4142 representation a base-class initializer into a parameter pack
4143 expansion. If all goes well, the resulting node will be an
4144 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4145 respectively. */
4146 tree
4147 make_pack_expansion (tree arg, tsubst_flags_t complain)
4148 {
4149 tree result;
4150 tree parameter_packs = NULL_TREE;
4151 bool for_types = false;
4152 struct find_parameter_pack_data ppd;
4153
4154 if (!arg || arg == error_mark_node)
4155 return arg;
4156
4157 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4158 {
4159 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4160 class initializer. In this case, the TREE_PURPOSE will be a
4161 _TYPE node (representing the base class expansion we're
4162 initializing) and the TREE_VALUE will be a TREE_LIST
4163 containing the initialization arguments.
4164
4165 The resulting expansion looks somewhat different from most
4166 expansions. Rather than returning just one _EXPANSION, we
4167 return a TREE_LIST whose TREE_PURPOSE is a
4168 TYPE_PACK_EXPANSION containing the bases that will be
4169 initialized. The TREE_VALUE will be identical to the
4170 original TREE_VALUE, which is a list of arguments that will
4171 be passed to each base. We do not introduce any new pack
4172 expansion nodes into the TREE_VALUE (although it is possible
4173 that some already exist), because the TREE_PURPOSE and
4174 TREE_VALUE all need to be expanded together with the same
4175 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4176 resulting TREE_PURPOSE will mention the parameter packs in
4177 both the bases and the arguments to the bases. */
4178 tree purpose;
4179 tree value;
4180 tree parameter_packs = NULL_TREE;
4181
4182 /* Determine which parameter packs will be used by the base
4183 class expansion. */
4184 ppd.visited = new hash_set<tree>;
4185 ppd.parameter_packs = &parameter_packs;
4186 ppd.type_pack_expansion_p = false;
4187 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4188 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4189 &ppd, ppd.visited);
4190
4191 if (parameter_packs == NULL_TREE)
4192 {
4193 if (complain & tf_error)
4194 error ("base initializer expansion %qT contains no parameter packs",
4195 arg);
4196 delete ppd.visited;
4197 return error_mark_node;
4198 }
4199
4200 if (TREE_VALUE (arg) != void_type_node)
4201 {
4202 /* Collect the sets of parameter packs used in each of the
4203 initialization arguments. */
4204 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4205 {
4206 /* Determine which parameter packs will be expanded in this
4207 argument. */
4208 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4209 &ppd, ppd.visited);
4210 }
4211 }
4212
4213 delete ppd.visited;
4214
4215 /* Create the pack expansion type for the base type. */
4216 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4217 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4218 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4219 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4220
4221 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4222 they will rarely be compared to anything. */
4223 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4224
4225 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4226 }
4227
4228 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4229 for_types = true;
4230
4231 /* Build the PACK_EXPANSION_* node. */
4232 result = for_types
4233 ? cxx_make_type (TYPE_PACK_EXPANSION)
4234 : make_node (EXPR_PACK_EXPANSION);
4235 SET_PACK_EXPANSION_PATTERN (result, arg);
4236 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4237 {
4238 /* Propagate type and const-expression information. */
4239 TREE_TYPE (result) = TREE_TYPE (arg);
4240 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4241 /* Mark this read now, since the expansion might be length 0. */
4242 mark_exp_read (arg);
4243 }
4244 else
4245 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4246 they will rarely be compared to anything. */
4247 SET_TYPE_STRUCTURAL_EQUALITY (result);
4248
4249 /* Determine which parameter packs will be expanded. */
4250 ppd.parameter_packs = &parameter_packs;
4251 ppd.visited = new hash_set<tree>;
4252 ppd.type_pack_expansion_p = TYPE_P (arg);
4253 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4254 delete ppd.visited;
4255
4256 /* Make sure we found some parameter packs. */
4257 if (parameter_packs == NULL_TREE)
4258 {
4259 if (complain & tf_error)
4260 {
4261 if (TYPE_P (arg))
4262 error ("expansion pattern %qT contains no parameter packs", arg);
4263 else
4264 error ("expansion pattern %qE contains no parameter packs", arg);
4265 }
4266 return error_mark_node;
4267 }
4268 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4269
4270 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4271 if (ppd.found_extra_args_tree_p)
4272 /* If the pattern of this pack expansion contains a subtree that has
4273 the extra args mechanism for avoiding partial instantiation, then
4274 force this pack expansion to also use extra args. Otherwise
4275 partial instantiation of this pack expansion may not lower the
4276 level of some parameter packs within the pattern, which would
4277 confuse tsubst_pack_expansion later (PR101764). */
4278 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
4279
4280 return result;
4281 }
4282
4283 /* Checks T for any "bare" parameter packs, which have not yet been
4284 expanded, and issues an error if any are found. This operation can
4285 only be done on full expressions or types (e.g., an expression
4286 statement, "if" condition, etc.), because we could have expressions like:
4287
4288 foo(f(g(h(args)))...)
4289
4290 where "args" is a parameter pack. check_for_bare_parameter_packs
4291 should not be called for the subexpressions args, h(args),
4292 g(h(args)), or f(g(h(args))), because we would produce erroneous
4293 error messages.
4294
4295 Returns TRUE and emits an error if there were bare parameter packs,
4296 returns FALSE otherwise. */
4297 bool
4298 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4299 {
4300 tree parameter_packs = NULL_TREE;
4301 struct find_parameter_pack_data ppd;
4302
4303 if (!processing_template_decl || !t || t == error_mark_node)
4304 return false;
4305
4306 if (TREE_CODE (t) == TYPE_DECL)
4307 t = TREE_TYPE (t);
4308
4309 ppd.parameter_packs = &parameter_packs;
4310 ppd.visited = new hash_set<tree>;
4311 ppd.type_pack_expansion_p = false;
4312 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4313 delete ppd.visited;
4314
4315 if (!parameter_packs)
4316 return false;
4317
4318 if (loc == UNKNOWN_LOCATION)
4319 loc = cp_expr_loc_or_input_loc (t);
4320
4321 /* It's OK for a lambda to have an unexpanded parameter pack from the
4322 containing context, but do complain about unexpanded capture packs. */
4323 tree lam = current_lambda_expr ();
4324 if (lam)
4325 lam = TREE_TYPE (lam);
4326
4327 if (lam && lam != current_class_type)
4328 {
4329 /* We're in a lambda, but it isn't the innermost class.
4330 This should work, but currently doesn't. */
4331 sorry_at (loc, "unexpanded parameter pack in local class in lambda");
4332 return true;
4333 }
4334
4335 if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
4336 for (; parameter_packs;
4337 parameter_packs = TREE_CHAIN (parameter_packs))
4338 {
4339 tree pack = TREE_VALUE (parameter_packs);
4340 if (is_capture_proxy (pack))
4341 break;
4342 }
4343
4344 if (parameter_packs)
4345 {
4346 error_at (loc, "parameter packs not expanded with %<...%>:");
4347 while (parameter_packs)
4348 {
4349 tree pack = TREE_VALUE (parameter_packs);
4350 tree name = NULL_TREE;
4351
4352 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4353 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4354 name = TYPE_NAME (pack);
4355 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4356 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4357 else if (TREE_CODE (pack) == CALL_EXPR)
4358 name = DECL_NAME (CALL_EXPR_FN (pack));
4359 else
4360 name = DECL_NAME (pack);
4361
4362 if (name)
4363 inform (loc, " %qD", name);
4364 else
4365 inform (loc, " %s", "<anonymous>");
4366
4367 parameter_packs = TREE_CHAIN (parameter_packs);
4368 }
4369
4370 return true;
4371 }
4372
4373 return false;
4374 }
4375
4376 /* Expand any parameter packs that occur in the template arguments in
4377 ARGS. */
4378 tree
4379 expand_template_argument_pack (tree args)
4380 {
4381 if (args == error_mark_node)
4382 return error_mark_node;
4383
4384 tree result_args = NULL_TREE;
4385 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4386 int num_result_args = -1;
4387 int non_default_args_count = -1;
4388
4389 /* First, determine if we need to expand anything, and the number of
4390 slots we'll need. */
4391 for (in_arg = 0; in_arg < nargs; ++in_arg)
4392 {
4393 tree arg = TREE_VEC_ELT (args, in_arg);
4394 if (arg == NULL_TREE)
4395 return args;
4396 if (ARGUMENT_PACK_P (arg))
4397 {
4398 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4399 if (num_result_args < 0)
4400 num_result_args = in_arg + num_packed;
4401 else
4402 num_result_args += num_packed;
4403 }
4404 else
4405 {
4406 if (num_result_args >= 0)
4407 num_result_args++;
4408 }
4409 }
4410
4411 /* If no expansion is necessary, we're done. */
4412 if (num_result_args < 0)
4413 return args;
4414
4415 /* Expand arguments. */
4416 result_args = make_tree_vec (num_result_args);
4417 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4418 non_default_args_count =
4419 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4420 for (in_arg = 0; in_arg < nargs; ++in_arg)
4421 {
4422 tree arg = TREE_VEC_ELT (args, in_arg);
4423 if (ARGUMENT_PACK_P (arg))
4424 {
4425 tree packed = ARGUMENT_PACK_ARGS (arg);
4426 int i, num_packed = TREE_VEC_LENGTH (packed);
4427 for (i = 0; i < num_packed; ++i, ++out_arg)
4428 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4429 if (non_default_args_count > 0)
4430 non_default_args_count += num_packed - 1;
4431 }
4432 else
4433 {
4434 TREE_VEC_ELT (result_args, out_arg) = arg;
4435 ++out_arg;
4436 }
4437 }
4438 if (non_default_args_count >= 0)
4439 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4440 return result_args;
4441 }
4442
4443 /* Checks if DECL shadows a template parameter.
4444
4445 [temp.local]: A template-parameter shall not be redeclared within its
4446 scope (including nested scopes).
4447
4448 Emits an error and returns TRUE if the DECL shadows a parameter,
4449 returns FALSE otherwise. */
4450
4451 bool
4452 check_template_shadow (tree decl)
4453 {
4454 tree olddecl;
4455
4456 /* If we're not in a template, we can't possibly shadow a template
4457 parameter. */
4458 if (!current_template_parms)
4459 return true;
4460
4461 /* Figure out what we're shadowing. */
4462 decl = OVL_FIRST (decl);
4463 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4464
4465 /* If there's no previous binding for this name, we're not shadowing
4466 anything, let alone a template parameter. */
4467 if (!olddecl)
4468 return true;
4469
4470 /* If we're not shadowing a template parameter, we're done. Note
4471 that OLDDECL might be an OVERLOAD (or perhaps even an
4472 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4473 node. */
4474 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4475 return true;
4476
4477 /* We check for decl != olddecl to avoid bogus errors for using a
4478 name inside a class. We check TPFI to avoid duplicate errors for
4479 inline member templates. */
4480 if (decl == olddecl
4481 || (DECL_TEMPLATE_PARM_P (decl)
4482 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4483 return true;
4484
4485 /* Don't complain about the injected class name, as we've already
4486 complained about the class itself. */
4487 if (DECL_SELF_REFERENCE_P (decl))
4488 return false;
4489
4490 if (DECL_TEMPLATE_PARM_P (decl))
4491 error ("declaration of template parameter %q+D shadows "
4492 "template parameter", decl);
4493 else
4494 error ("declaration of %q+#D shadows template parameter", decl);
4495 inform (DECL_SOURCE_LOCATION (olddecl),
4496 "template parameter %qD declared here", olddecl);
4497 return false;
4498 }
4499
4500 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4501 ORIG_LEVEL, DECL, and TYPE. */
4502
4503 static tree
4504 build_template_parm_index (int index,
4505 int level,
4506 int orig_level,
4507 tree decl,
4508 tree type)
4509 {
4510 tree t = make_node (TEMPLATE_PARM_INDEX);
4511 TEMPLATE_PARM_IDX (t) = index;
4512 TEMPLATE_PARM_LEVEL (t) = level;
4513 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4514 TEMPLATE_PARM_DECL (t) = decl;
4515 TREE_TYPE (t) = type;
4516 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4517 TREE_READONLY (t) = TREE_READONLY (decl);
4518
4519 return t;
4520 }
4521
4522 /* Find the canonical type parameter for the given template type
4523 parameter. Returns the canonical type parameter, which may be TYPE
4524 if no such parameter existed. */
4525
4526 tree
4527 canonical_type_parameter (tree type)
4528 {
4529 int idx = TEMPLATE_TYPE_IDX (type);
4530
4531 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4532
4533 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4534 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4535
4536 for (tree list = (*canonical_template_parms)[idx];
4537 list; list = TREE_CHAIN (list))
4538 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4539 return TREE_VALUE (list);
4540
4541 (*canonical_template_parms)[idx]
4542 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4543 return type;
4544 }
4545
4546 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4547 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4548 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4549 new one is created. */
4550
4551 static tree
4552 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4553 tsubst_flags_t complain)
4554 {
4555 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4556 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4557 != TEMPLATE_PARM_LEVEL (index) - levels)
4558 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4559 {
4560 tree orig_decl = TEMPLATE_PARM_DECL (index);
4561
4562 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4563 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4564 type);
4565 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4566 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4567 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4568 DECL_ARTIFICIAL (decl) = 1;
4569 SET_DECL_TEMPLATE_PARM_P (decl);
4570
4571 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4572 TEMPLATE_PARM_LEVEL (index) - levels,
4573 TEMPLATE_PARM_ORIG_LEVEL (index),
4574 decl, type);
4575 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4576 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4577 = TEMPLATE_PARM_PARAMETER_PACK (index);
4578
4579 /* Template template parameters need this. */
4580 tree inner = decl;
4581 if (TREE_CODE (decl) == TEMPLATE_DECL)
4582 {
4583 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4584 TYPE_DECL, DECL_NAME (decl), type);
4585 DECL_TEMPLATE_RESULT (decl) = inner;
4586 DECL_ARTIFICIAL (inner) = true;
4587 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4588 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4589 }
4590
4591 /* Attach the TPI to the decl. */
4592 if (TREE_CODE (inner) == TYPE_DECL)
4593 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4594 else
4595 DECL_INITIAL (decl) = tpi;
4596 }
4597
4598 return TEMPLATE_PARM_DESCENDANTS (index);
4599 }
4600
4601 /* Process information from new template parameter PARM and append it
4602 to the LIST being built. This new parameter is a non-type
4603 parameter iff IS_NON_TYPE is true. This new parameter is a
4604 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4605 is in PARM_LOC. */
4606
4607 tree
4608 process_template_parm (tree list, location_t parm_loc, tree parm,
4609 bool is_non_type, bool is_parameter_pack)
4610 {
4611 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4612 tree prev = NULL_TREE;
4613 int idx = 0;
4614
4615 if (list)
4616 {
4617 prev = tree_last (list);
4618
4619 tree p = TREE_VALUE (prev);
4620 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4621 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4622 else if (TREE_CODE (p) == PARM_DECL)
4623 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4624
4625 ++idx;
4626 }
4627
4628 tree decl = NULL_TREE;
4629 tree defval = TREE_PURPOSE (parm);
4630 tree constr = TREE_TYPE (parm);
4631
4632 if (is_non_type)
4633 {
4634 parm = TREE_VALUE (parm);
4635
4636 SET_DECL_TEMPLATE_PARM_P (parm);
4637
4638 if (TREE_TYPE (parm) != error_mark_node)
4639 {
4640 /* [temp.param]
4641
4642 The top-level cv-qualifiers on the template-parameter are
4643 ignored when determining its type. */
4644 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4645 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4646 TREE_TYPE (parm) = error_mark_node;
4647 else if (uses_parameter_packs (TREE_TYPE (parm))
4648 && !is_parameter_pack
4649 /* If we're in a nested template parameter list, the template
4650 template parameter could be a parameter pack. */
4651 && processing_template_parmlist == 1)
4652 {
4653 /* This template parameter is not a parameter pack, but it
4654 should be. Complain about "bare" parameter packs. */
4655 check_for_bare_parameter_packs (TREE_TYPE (parm));
4656
4657 /* Recover by calling this a parameter pack. */
4658 is_parameter_pack = true;
4659 }
4660 }
4661
4662 /* A template parameter is not modifiable. */
4663 TREE_CONSTANT (parm) = 1;
4664 TREE_READONLY (parm) = 1;
4665 decl = build_decl (parm_loc,
4666 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4667 TREE_CONSTANT (decl) = 1;
4668 TREE_READONLY (decl) = 1;
4669 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4670 = build_template_parm_index (idx, current_template_depth,
4671 current_template_depth,
4672 decl, TREE_TYPE (parm));
4673
4674 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4675 = is_parameter_pack;
4676 }
4677 else
4678 {
4679 tree t;
4680 parm = TREE_VALUE (TREE_VALUE (parm));
4681
4682 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4683 {
4684 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4685 /* This is for distinguishing between real templates and template
4686 template parameters */
4687 TREE_TYPE (parm) = t;
4688
4689 /* any_template_parm_r expects to be able to get the targs of a
4690 DECL_TEMPLATE_RESULT. */
4691 tree result = DECL_TEMPLATE_RESULT (parm);
4692 TREE_TYPE (result) = t;
4693 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4694 tree tinfo = build_template_info (parm, args);
4695 retrofit_lang_decl (result);
4696 DECL_TEMPLATE_INFO (result) = tinfo;
4697
4698 decl = parm;
4699 }
4700 else
4701 {
4702 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4703 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4704 decl = build_decl (parm_loc,
4705 TYPE_DECL, parm, t);
4706 }
4707
4708 TYPE_NAME (t) = decl;
4709 TYPE_STUB_DECL (t) = decl;
4710 parm = decl;
4711 TEMPLATE_TYPE_PARM_INDEX (t)
4712 = build_template_parm_index (idx, current_template_depth,
4713 current_template_depth,
4714 decl, TREE_TYPE (parm));
4715 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4716 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4717 SET_TYPE_STRUCTURAL_EQUALITY (t);
4718 else
4719 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4720 }
4721 DECL_ARTIFICIAL (decl) = 1;
4722 SET_DECL_TEMPLATE_PARM_P (decl);
4723
4724 /* Build requirements for the type/template parameter.
4725 This must be done after SET_DECL_TEMPLATE_PARM_P or
4726 process_template_parm could fail. */
4727 tree reqs = finish_shorthand_constraint (parm, constr);
4728
4729 decl = pushdecl (decl);
4730 if (!is_non_type)
4731 parm = decl;
4732
4733 /* Build the parameter node linking the parameter declaration,
4734 its default argument (if any), and its constraints (if any). */
4735 parm = build_tree_list (defval, parm);
4736 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4737
4738 if (prev)
4739 TREE_CHAIN (prev) = parm;
4740 else
4741 list = parm;
4742
4743 return list;
4744 }
4745
4746 /* The end of a template parameter list has been reached. Process the
4747 tree list into a parameter vector, converting each parameter into a more
4748 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4749 as PARM_DECLs. */
4750
4751 tree
4752 end_template_parm_list (tree parms)
4753 {
4754 tree saved_parmlist = make_tree_vec (list_length (parms));
4755
4756 /* Pop the dummy parameter level and add the real one. We do not
4757 morph the dummy parameter in place, as it might have been
4758 captured by a (nested) template-template-parm. */
4759 current_template_parms = TREE_CHAIN (current_template_parms);
4760
4761 current_template_parms
4762 = tree_cons (size_int (current_template_depth + 1),
4763 saved_parmlist, current_template_parms);
4764
4765 for (unsigned ix = 0; parms; ix++)
4766 {
4767 tree parm = parms;
4768 parms = TREE_CHAIN (parms);
4769 TREE_CHAIN (parm) = NULL_TREE;
4770
4771 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4772 }
4773
4774 --processing_template_parmlist;
4775
4776 return saved_parmlist;
4777 }
4778
4779 // Explicitly indicate the end of the template parameter list. We assume
4780 // that the current template parameters have been constructed and/or
4781 // managed explicitly, as when creating new template template parameters
4782 // from a shorthand constraint.
4783 void
4784 end_template_parm_list ()
4785 {
4786 --processing_template_parmlist;
4787 }
4788
4789 /* end_template_decl is called after a template declaration is seen. */
4790
4791 void
4792 end_template_decl (void)
4793 {
4794 reset_specialization ();
4795
4796 if (! processing_template_decl)
4797 return;
4798
4799 /* This matches the pushlevel in begin_template_parm_list. */
4800 finish_scope ();
4801
4802 --processing_template_decl;
4803 current_template_parms = TREE_CHAIN (current_template_parms);
4804 }
4805
4806 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4807 thereof, and converts it into an argument suitable to be passed to
4808 the type substitution functions. Note that if the TREE_LIST contains
4809 an error_mark node, the returned argument is error_mark_node. */
4810
4811 tree
4812 template_parm_to_arg (tree t)
4813 {
4814 if (!t)
4815 return NULL_TREE;
4816
4817 if (TREE_CODE (t) == TREE_LIST)
4818 t = TREE_VALUE (t);
4819
4820 if (error_operand_p (t))
4821 return error_mark_node;
4822
4823 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4824 {
4825 if (TREE_CODE (t) == TYPE_DECL
4826 || TREE_CODE (t) == TEMPLATE_DECL)
4827 t = TREE_TYPE (t);
4828 else
4829 t = DECL_INITIAL (t);
4830 }
4831
4832 gcc_assert (TEMPLATE_PARM_P (t));
4833
4834 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4835 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4836 {
4837 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4838 {
4839 /* Turn this argument into a TYPE_ARGUMENT_PACK
4840 with a single element, which expands T. */
4841 tree vec = make_tree_vec (1);
4842 if (CHECKING_P)
4843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4844
4845 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4846
4847 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4848 SET_ARGUMENT_PACK_ARGS (t, vec);
4849 }
4850 }
4851 else
4852 {
4853 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4854 {
4855 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4856 with a single element, which expands T. */
4857 tree vec = make_tree_vec (1);
4858 if (CHECKING_P)
4859 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4860
4861 t = convert_from_reference (t);
4862 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4863
4864 t = make_node (NONTYPE_ARGUMENT_PACK);
4865 SET_ARGUMENT_PACK_ARGS (t, vec);
4866 }
4867 else
4868 t = convert_from_reference (t);
4869 }
4870 return t;
4871 }
4872
4873 /* Given a single level of template parameters (a TREE_VEC), return it
4874 as a set of template arguments. */
4875
4876 tree
4877 template_parms_level_to_args (tree parms)
4878 {
4879 tree a = copy_node (parms);
4880 TREE_TYPE (a) = NULL_TREE;
4881 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4882 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4883
4884 if (CHECKING_P)
4885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4886
4887 return a;
4888 }
4889
4890 /* Given a set of template parameters, return them as a set of template
4891 arguments. The template parameters are represented as a TREE_VEC, in
4892 the form documented in cp-tree.h for template arguments. */
4893
4894 tree
4895 template_parms_to_args (tree parms)
4896 {
4897 tree header;
4898 tree args = NULL_TREE;
4899 int length = TMPL_PARMS_DEPTH (parms);
4900 int l = length;
4901
4902 /* If there is only one level of template parameters, we do not
4903 create a TREE_VEC of TREE_VECs. Instead, we return a single
4904 TREE_VEC containing the arguments. */
4905 if (length > 1)
4906 args = make_tree_vec (length);
4907
4908 for (header = parms; header; header = TREE_CHAIN (header))
4909 {
4910 tree a = template_parms_level_to_args (TREE_VALUE (header));
4911
4912 if (length > 1)
4913 TREE_VEC_ELT (args, --l) = a;
4914 else
4915 args = a;
4916 }
4917
4918 return args;
4919 }
4920
4921 /* Within the declaration of a template, return the currently active
4922 template parameters as an argument TREE_VEC. */
4923
4924 static tree
4925 current_template_args (void)
4926 {
4927 return template_parms_to_args (current_template_parms);
4928 }
4929
4930 /* Return the fully generic arguments for of TMPL, i.e. what
4931 current_template_args would be while parsing it. */
4932
4933 tree
4934 generic_targs_for (tree tmpl)
4935 {
4936 if (tmpl == NULL_TREE)
4937 return NULL_TREE;
4938 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4939 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4940 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4941 template parameter, it has no TEMPLATE_INFO; for a partial
4942 specialization, it has the arguments for the primary template, and we
4943 want the arguments for the partial specialization. */;
4944 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4945 if (tree ti = get_template_info (result))
4946 return TI_ARGS (ti);
4947 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4948 }
4949
4950 /* Update the declared TYPE by doing any lookups which were thought to be
4951 dependent, but are not now that we know the SCOPE of the declarator. */
4952
4953 tree
4954 maybe_update_decl_type (tree orig_type, tree scope)
4955 {
4956 tree type = orig_type;
4957
4958 if (type == NULL_TREE)
4959 return type;
4960
4961 if (TREE_CODE (orig_type) == TYPE_DECL)
4962 type = TREE_TYPE (type);
4963
4964 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4965 && dependent_type_p (type)
4966 /* Don't bother building up the args in this case. */
4967 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4968 {
4969 /* tsubst in the args corresponding to the template parameters,
4970 including auto if present. Most things will be unchanged, but
4971 make_typename_type and tsubst_qualified_id will resolve
4972 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4973 tree args = current_template_args ();
4974 tree auto_node = type_uses_auto (type);
4975 tree pushed;
4976 if (auto_node)
4977 {
4978 tree auto_vec = make_tree_vec (1);
4979 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4980 args = add_to_template_args (args, auto_vec);
4981 }
4982 pushed = push_scope (scope);
4983 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4984 if (pushed)
4985 pop_scope (scope);
4986 }
4987
4988 if (type == error_mark_node)
4989 return orig_type;
4990
4991 if (TREE_CODE (orig_type) == TYPE_DECL)
4992 {
4993 if (same_type_p (type, TREE_TYPE (orig_type)))
4994 type = orig_type;
4995 else
4996 type = TYPE_NAME (type);
4997 }
4998 return type;
4999 }
5000
5001 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5002 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5003 the new template is a member template. */
5004
5005 static tree
5006 build_template_decl (tree decl, tree parms, bool member_template_p)
5007 {
5008 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
5009 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
5010 DECL_TEMPLATE_PARMS (tmpl) = parms;
5011 DECL_TEMPLATE_RESULT (tmpl) = decl;
5012 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
5013 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5014 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
5015 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
5016
5017 /* Propagate module information from the decl. */
5018 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
5019
5020 return tmpl;
5021 }
5022
5023 struct template_parm_data
5024 {
5025 /* The level of the template parameters we are currently
5026 processing. */
5027 int level;
5028
5029 /* The index of the specialization argument we are currently
5030 processing. */
5031 int current_arg;
5032
5033 /* An array whose size is the number of template parameters. The
5034 elements are nonzero if the parameter has been used in any one
5035 of the arguments processed so far. */
5036 int* parms;
5037
5038 /* An array whose size is the number of template arguments. The
5039 elements are nonzero if the argument makes use of template
5040 parameters of this level. */
5041 int* arg_uses_template_parms;
5042 };
5043
5044 /* Subroutine of push_template_decl used to see if each template
5045 parameter in a partial specialization is used in the explicit
5046 argument list. If T is of the LEVEL given in DATA (which is
5047 treated as a template_parm_data*), then DATA->PARMS is marked
5048 appropriately. */
5049
5050 static int
5051 mark_template_parm (tree t, void* data)
5052 {
5053 int level;
5054 int idx;
5055 struct template_parm_data* tpd = (struct template_parm_data*) data;
5056
5057 template_parm_level_and_index (t, &level, &idx);
5058
5059 if (level == tpd->level)
5060 {
5061 tpd->parms[idx] = 1;
5062 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5063 }
5064
5065 /* In C++17 the type of a non-type argument is a deduced context. */
5066 if (cxx_dialect >= cxx17
5067 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5068 for_each_template_parm (TREE_TYPE (t),
5069 &mark_template_parm,
5070 data,
5071 NULL,
5072 /*include_nondeduced_p=*/false);
5073
5074 /* Return zero so that for_each_template_parm will continue the
5075 traversal of the tree; we want to mark *every* template parm. */
5076 return 0;
5077 }
5078
5079 /* Process the partial specialization DECL. */
5080
5081 static tree
5082 process_partial_specialization (tree decl)
5083 {
5084 tree type = TREE_TYPE (decl);
5085 tree tinfo = get_template_info (decl);
5086 tree maintmpl = TI_TEMPLATE (tinfo);
5087 tree specargs = TI_ARGS (tinfo);
5088 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5089 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5090 tree inner_parms;
5091 tree inst;
5092 int nargs = TREE_VEC_LENGTH (inner_args);
5093 int ntparms;
5094 int i;
5095 bool did_error_intro = false;
5096 struct template_parm_data tpd;
5097 struct template_parm_data tpd2;
5098
5099 gcc_assert (current_template_parms);
5100
5101 /* A concept cannot be specialized. */
5102 if (flag_concepts && variable_concept_p (maintmpl))
5103 {
5104 error ("specialization of variable concept %q#D", maintmpl);
5105 return error_mark_node;
5106 }
5107
5108 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5109 ntparms = TREE_VEC_LENGTH (inner_parms);
5110
5111 /* We check that each of the template parameters given in the
5112 partial specialization is used in the argument list to the
5113 specialization. For example:
5114
5115 template <class T> struct S;
5116 template <class T> struct S<T*>;
5117
5118 The second declaration is OK because `T*' uses the template
5119 parameter T, whereas
5120
5121 template <class T> struct S<int>;
5122
5123 is no good. Even trickier is:
5124
5125 template <class T>
5126 struct S1
5127 {
5128 template <class U>
5129 struct S2;
5130 template <class U>
5131 struct S2<T>;
5132 };
5133
5134 The S2<T> declaration is actually invalid; it is a
5135 full-specialization. Of course,
5136
5137 template <class U>
5138 struct S2<T (*)(U)>;
5139
5140 or some such would have been OK. */
5141 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5142 tpd.parms = XALLOCAVEC (int, ntparms);
5143 memset (tpd.parms, 0, sizeof (int) * ntparms);
5144
5145 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5146 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5147 for (i = 0; i < nargs; ++i)
5148 {
5149 tpd.current_arg = i;
5150 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5151 &mark_template_parm,
5152 &tpd,
5153 NULL,
5154 /*include_nondeduced_p=*/false);
5155 }
5156 for (i = 0; i < ntparms; ++i)
5157 if (tpd.parms[i] == 0)
5158 {
5159 /* One of the template parms was not used in a deduced context in the
5160 specialization. */
5161 if (!did_error_intro)
5162 {
5163 error ("template parameters not deducible in "
5164 "partial specialization:");
5165 did_error_intro = true;
5166 }
5167
5168 inform (input_location, " %qD",
5169 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5170 }
5171
5172 if (did_error_intro)
5173 return error_mark_node;
5174
5175 /* [temp.class.spec]
5176
5177 The argument list of the specialization shall not be identical to
5178 the implicit argument list of the primary template. */
5179 tree main_args
5180 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5181 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5182 && (!flag_concepts
5183 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5184 {
5185 if (!flag_concepts)
5186 error ("partial specialization %q+D does not specialize "
5187 "any template arguments; to define the primary template, "
5188 "remove the template argument list", decl);
5189 else
5190 error ("partial specialization %q+D does not specialize any "
5191 "template arguments and is not more constrained than "
5192 "the primary template; to define the primary template, "
5193 "remove the template argument list", decl);
5194 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5195 }
5196
5197 /* A partial specialization that replaces multiple parameters of the
5198 primary template with a pack expansion is less specialized for those
5199 parameters. */
5200 if (nargs < DECL_NTPARMS (maintmpl))
5201 {
5202 error ("partial specialization is not more specialized than the "
5203 "primary template because it replaces multiple parameters "
5204 "with a pack expansion");
5205 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5206 /* Avoid crash in process_partial_specialization. */
5207 return decl;
5208 }
5209
5210 else if (nargs > DECL_NTPARMS (maintmpl))
5211 {
5212 error ("too many arguments for partial specialization %qT", type);
5213 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5214 /* Avoid crash below. */
5215 return decl;
5216 }
5217
5218 /* If we aren't in a dependent class, we can actually try deduction. */
5219 else if (tpd.level == 1
5220 /* FIXME we should be able to handle a partial specialization of a
5221 partial instantiation, but currently we can't (c++/41727). */
5222 && TMPL_ARGS_DEPTH (specargs) == 1
5223 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5224 {
5225 auto_diagnostic_group d;
5226 if (permerror (input_location, "partial specialization %qD is not "
5227 "more specialized than", decl))
5228 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5229 maintmpl);
5230 }
5231
5232 /* [temp.spec.partial]
5233
5234 The type of a template parameter corresponding to a specialized
5235 non-type argument shall not be dependent on a parameter of the
5236 specialization.
5237
5238 Also, we verify that pack expansions only occur at the
5239 end of the argument list. */
5240 tpd2.parms = 0;
5241 for (i = 0; i < nargs; ++i)
5242 {
5243 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5244 tree arg = TREE_VEC_ELT (inner_args, i);
5245 tree packed_args = NULL_TREE;
5246 int j, len = 1;
5247
5248 if (ARGUMENT_PACK_P (arg))
5249 {
5250 /* Extract the arguments from the argument pack. We'll be
5251 iterating over these in the following loop. */
5252 packed_args = ARGUMENT_PACK_ARGS (arg);
5253 len = TREE_VEC_LENGTH (packed_args);
5254 }
5255
5256 for (j = 0; j < len; j++)
5257 {
5258 if (packed_args)
5259 /* Get the Jth argument in the parameter pack. */
5260 arg = TREE_VEC_ELT (packed_args, j);
5261
5262 if (PACK_EXPANSION_P (arg))
5263 {
5264 /* Pack expansions must come at the end of the
5265 argument list. */
5266 if ((packed_args && j < len - 1)
5267 || (!packed_args && i < nargs - 1))
5268 {
5269 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5270 error ("parameter pack argument %qE must be at the "
5271 "end of the template argument list", arg);
5272 else
5273 error ("parameter pack argument %qT must be at the "
5274 "end of the template argument list", arg);
5275 }
5276 }
5277
5278 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5279 /* We only care about the pattern. */
5280 arg = PACK_EXPANSION_PATTERN (arg);
5281
5282 if (/* These first two lines are the `non-type' bit. */
5283 !TYPE_P (arg)
5284 && TREE_CODE (arg) != TEMPLATE_DECL
5285 /* This next two lines are the `argument expression is not just a
5286 simple identifier' condition and also the `specialized
5287 non-type argument' bit. */
5288 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5289 && !((REFERENCE_REF_P (arg)
5290 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5291 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5292 {
5293 /* Look at the corresponding template parameter,
5294 marking which template parameters its type depends
5295 upon. */
5296 tree type = TREE_TYPE (parm);
5297
5298 if (!tpd2.parms)
5299 {
5300 /* We haven't yet initialized TPD2. Do so now. */
5301 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5302 /* The number of parameters here is the number in the
5303 main template, which, as checked in the assertion
5304 above, is NARGS. */
5305 tpd2.parms = XALLOCAVEC (int, nargs);
5306 tpd2.level =
5307 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5308 }
5309
5310 /* Mark the template parameters. But this time, we're
5311 looking for the template parameters of the main
5312 template, not in the specialization. */
5313 tpd2.current_arg = i;
5314 tpd2.arg_uses_template_parms[i] = 0;
5315 memset (tpd2.parms, 0, sizeof (int) * nargs);
5316 for_each_template_parm (type,
5317 &mark_template_parm,
5318 &tpd2,
5319 NULL,
5320 /*include_nondeduced_p=*/false);
5321
5322 if (tpd2.arg_uses_template_parms [i])
5323 {
5324 /* The type depended on some template parameters.
5325 If they are fully specialized in the
5326 specialization, that's OK. */
5327 int j;
5328 int count = 0;
5329 for (j = 0; j < nargs; ++j)
5330 if (tpd2.parms[j] != 0
5331 && tpd.arg_uses_template_parms [j])
5332 ++count;
5333 if (count != 0)
5334 error_n (input_location, count,
5335 "type %qT of template argument %qE depends "
5336 "on a template parameter",
5337 "type %qT of template argument %qE depends "
5338 "on template parameters",
5339 type,
5340 arg);
5341 }
5342 }
5343 }
5344 }
5345
5346 /* We should only get here once. */
5347 if (TREE_CODE (decl) == TYPE_DECL)
5348 gcc_assert (!COMPLETE_TYPE_P (type));
5349
5350 // Build the template decl.
5351 tree tmpl = build_template_decl (decl, current_template_parms,
5352 DECL_MEMBER_TEMPLATE_P (maintmpl));
5353 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5354 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5355 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5356
5357 /* Give template template parms a DECL_CONTEXT of the template
5358 for which they are a parameter. */
5359 for (i = 0; i < ntparms; ++i)
5360 {
5361 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5362 if (TREE_CODE (parm) == TEMPLATE_DECL)
5363 DECL_CONTEXT (parm) = tmpl;
5364 }
5365
5366 if (VAR_P (decl))
5367 /* We didn't register this in check_explicit_specialization so we could
5368 wait until the constraints were set. */
5369 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5370 else
5371 associate_classtype_constraints (type);
5372
5373 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5374 = tree_cons (specargs, tmpl,
5375 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5376 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5377
5378 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5379 inst = TREE_CHAIN (inst))
5380 {
5381 tree instance = TREE_VALUE (inst);
5382 if (TYPE_P (instance)
5383 ? (COMPLETE_TYPE_P (instance)
5384 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5385 : DECL_TEMPLATE_INSTANTIATION (instance))
5386 {
5387 tree spec = most_specialized_partial_spec (instance, tf_none);
5388 tree inst_decl = (DECL_P (instance)
5389 ? instance : TYPE_NAME (instance));
5390 if (!spec)
5391 /* OK */;
5392 else if (spec == error_mark_node)
5393 permerror (input_location,
5394 "declaration of %qD ambiguates earlier template "
5395 "instantiation for %qD", decl, inst_decl);
5396 else if (TREE_VALUE (spec) == tmpl)
5397 permerror (input_location,
5398 "partial specialization of %qD after instantiation "
5399 "of %qD", decl, inst_decl);
5400 }
5401 }
5402
5403 return decl;
5404 }
5405
5406 /* PARM is a template parameter of some form; return the corresponding
5407 TEMPLATE_PARM_INDEX. */
5408
5409 static tree
5410 get_template_parm_index (tree parm)
5411 {
5412 if (TREE_CODE (parm) == PARM_DECL
5413 || TREE_CODE (parm) == CONST_DECL)
5414 parm = DECL_INITIAL (parm);
5415 else if (TREE_CODE (parm) == TYPE_DECL
5416 || TREE_CODE (parm) == TEMPLATE_DECL)
5417 parm = TREE_TYPE (parm);
5418 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5419 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5420 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5421 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5422 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5423 return parm;
5424 }
5425
5426 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5427 parameter packs used by the template parameter PARM. */
5428
5429 static void
5430 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5431 {
5432 /* A type parm can't refer to another parm. */
5433 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5434 return;
5435 else if (TREE_CODE (parm) == PARM_DECL)
5436 {
5437 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5438 ppd, ppd->visited);
5439 return;
5440 }
5441
5442 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5443
5444 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5445 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5446 {
5447 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5448 if (template_parameter_pack_p (p))
5449 /* Any packs in the type are expanded by this parameter. */;
5450 else
5451 fixed_parameter_pack_p_1 (p, ppd);
5452 }
5453 }
5454
5455 /* PARM is a template parameter pack. Return any parameter packs used in
5456 its type or the type of any of its template parameters. If there are
5457 any such packs, it will be instantiated into a fixed template parameter
5458 list by partial instantiation rather than be fully deduced. */
5459
5460 tree
5461 fixed_parameter_pack_p (tree parm)
5462 {
5463 /* This can only be true in a member template. */
5464 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5465 return NULL_TREE;
5466 /* This can only be true for a parameter pack. */
5467 if (!template_parameter_pack_p (parm))
5468 return NULL_TREE;
5469 /* A type parm can't refer to another parm. */
5470 if (TREE_CODE (parm) == TYPE_DECL)
5471 return NULL_TREE;
5472
5473 tree parameter_packs = NULL_TREE;
5474 struct find_parameter_pack_data ppd;
5475 ppd.parameter_packs = &parameter_packs;
5476 ppd.visited = new hash_set<tree>;
5477 ppd.type_pack_expansion_p = false;
5478
5479 fixed_parameter_pack_p_1 (parm, &ppd);
5480
5481 delete ppd.visited;
5482 return parameter_packs;
5483 }
5484
5485 /* Check that a template declaration's use of default arguments and
5486 parameter packs is not invalid. Here, PARMS are the template
5487 parameters. IS_PRIMARY is true if DECL is the thing declared by
5488 a primary template. IS_PARTIAL is true if DECL is a partial
5489 specialization.
5490
5491 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5492 function template declaration or a friend class template
5493 declaration. In the function case, 1 indicates a declaration, 2
5494 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5495 emitted for extraneous default arguments.
5496
5497 Returns TRUE if there were no errors found, FALSE otherwise. */
5498
5499 bool
5500 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5501 bool is_partial, int is_friend_decl)
5502 {
5503 const char *msg;
5504 int last_level_to_check;
5505 tree parm_level;
5506 bool no_errors = true;
5507
5508 /* [temp.param]
5509
5510 A default template-argument shall not be specified in a
5511 function template declaration or a function template definition, nor
5512 in the template-parameter-list of the definition of a member of a
5513 class template. */
5514
5515 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5516 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5517 /* You can't have a function template declaration in a local
5518 scope, nor you can you define a member of a class template in a
5519 local scope. */
5520 return true;
5521
5522 if ((TREE_CODE (decl) == TYPE_DECL
5523 && TREE_TYPE (decl)
5524 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5525 || (TREE_CODE (decl) == FUNCTION_DECL
5526 && LAMBDA_FUNCTION_P (decl)))
5527 /* A lambda doesn't have an explicit declaration; don't complain
5528 about the parms of the enclosing class. */
5529 return true;
5530
5531 if (current_class_type
5532 && !TYPE_BEING_DEFINED (current_class_type)
5533 && DECL_LANG_SPECIFIC (decl)
5534 && DECL_DECLARES_FUNCTION_P (decl)
5535 /* If this is either a friend defined in the scope of the class
5536 or a member function. */
5537 && (DECL_FUNCTION_MEMBER_P (decl)
5538 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5539 : DECL_FRIEND_CONTEXT (decl)
5540 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5541 : false)
5542 /* And, if it was a member function, it really was defined in
5543 the scope of the class. */
5544 && (!DECL_FUNCTION_MEMBER_P (decl)
5545 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5546 /* We already checked these parameters when the template was
5547 declared, so there's no need to do it again now. This function
5548 was defined in class scope, but we're processing its body now
5549 that the class is complete. */
5550 return true;
5551
5552 /* Core issue 226 (C++0x only): the following only applies to class
5553 templates. */
5554 if (is_primary
5555 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5556 {
5557 /* [temp.param]
5558
5559 If a template-parameter has a default template-argument, all
5560 subsequent template-parameters shall have a default
5561 template-argument supplied. */
5562 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5563 {
5564 tree inner_parms = TREE_VALUE (parm_level);
5565 int ntparms = TREE_VEC_LENGTH (inner_parms);
5566 int seen_def_arg_p = 0;
5567 int i;
5568
5569 for (i = 0; i < ntparms; ++i)
5570 {
5571 tree parm = TREE_VEC_ELT (inner_parms, i);
5572
5573 if (parm == error_mark_node)
5574 continue;
5575
5576 if (TREE_PURPOSE (parm))
5577 seen_def_arg_p = 1;
5578 else if (seen_def_arg_p
5579 && !template_parameter_pack_p (TREE_VALUE (parm)))
5580 {
5581 error ("no default argument for %qD", TREE_VALUE (parm));
5582 /* For better subsequent error-recovery, we indicate that
5583 there should have been a default argument. */
5584 TREE_PURPOSE (parm) = error_mark_node;
5585 no_errors = false;
5586 }
5587 else if (!is_partial
5588 && !is_friend_decl
5589 /* Don't complain about an enclosing partial
5590 specialization. */
5591 && parm_level == parms
5592 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5593 && i < ntparms - 1
5594 && template_parameter_pack_p (TREE_VALUE (parm))
5595 /* A fixed parameter pack will be partially
5596 instantiated into a fixed length list. */
5597 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5598 {
5599 /* A primary class template, primary variable template
5600 (DR 2032), or alias template can only have one
5601 parameter pack, at the end of the template
5602 parameter list. */
5603
5604 error ("parameter pack %q+D must be at the end of the"
5605 " template parameter list", TREE_VALUE (parm));
5606
5607 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5608 = error_mark_node;
5609 no_errors = false;
5610 }
5611 }
5612 }
5613 }
5614
5615 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5616 || is_partial
5617 || !is_primary
5618 || is_friend_decl)
5619 /* For an ordinary class template, default template arguments are
5620 allowed at the innermost level, e.g.:
5621 template <class T = int>
5622 struct S {};
5623 but, in a partial specialization, they're not allowed even
5624 there, as we have in [temp.class.spec]:
5625
5626 The template parameter list of a specialization shall not
5627 contain default template argument values.
5628
5629 So, for a partial specialization, or for a function template
5630 (in C++98/C++03), we look at all of them. */
5631 ;
5632 else
5633 /* But, for a primary class template that is not a partial
5634 specialization we look at all template parameters except the
5635 innermost ones. */
5636 parms = TREE_CHAIN (parms);
5637
5638 /* Figure out what error message to issue. */
5639 if (is_friend_decl == 2)
5640 msg = G_("default template arguments may not be used in function template "
5641 "friend re-declaration");
5642 else if (is_friend_decl)
5643 msg = G_("default template arguments may not be used in template "
5644 "friend declarations");
5645 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5646 msg = G_("default template arguments may not be used in function templates "
5647 "without %<-std=c++11%> or %<-std=gnu++11%>");
5648 else if (is_partial)
5649 msg = G_("default template arguments may not be used in "
5650 "partial specializations");
5651 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5652 msg = G_("default argument for template parameter for class enclosing %qD");
5653 else
5654 /* Per [temp.param]/9, "A default template-argument shall not be
5655 specified in the template-parameter-lists of the definition of
5656 a member of a class template that appears outside of the member's
5657 class.", thus if we aren't handling a member of a class template
5658 there is no need to examine the parameters. */
5659 return true;
5660
5661 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5662 /* If we're inside a class definition, there's no need to
5663 examine the parameters to the class itself. On the one
5664 hand, they will be checked when the class is defined, and,
5665 on the other, default arguments are valid in things like:
5666 template <class T = double>
5667 struct S { template <class U> void f(U); };
5668 Here the default argument for `S' has no bearing on the
5669 declaration of `f'. */
5670 last_level_to_check = template_class_depth (current_class_type) + 1;
5671 else
5672 /* Check everything. */
5673 last_level_to_check = 0;
5674
5675 for (parm_level = parms;
5676 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5677 parm_level = TREE_CHAIN (parm_level))
5678 {
5679 tree inner_parms = TREE_VALUE (parm_level);
5680 int i;
5681 int ntparms;
5682
5683 ntparms = TREE_VEC_LENGTH (inner_parms);
5684 for (i = 0; i < ntparms; ++i)
5685 {
5686 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5687 continue;
5688
5689 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5690 {
5691 if (msg)
5692 {
5693 no_errors = false;
5694 if (is_friend_decl == 2)
5695 return no_errors;
5696
5697 error (msg, decl);
5698 msg = 0;
5699 }
5700
5701 /* Clear out the default argument so that we are not
5702 confused later. */
5703 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5704 }
5705 }
5706
5707 /* At this point, if we're still interested in issuing messages,
5708 they must apply to classes surrounding the object declared. */
5709 if (msg)
5710 msg = G_("default argument for template parameter for class "
5711 "enclosing %qD");
5712 }
5713
5714 return no_errors;
5715 }
5716
5717 /* Worker for push_template_decl_real, called via
5718 for_each_template_parm. DATA is really an int, indicating the
5719 level of the parameters we are interested in. If T is a template
5720 parameter of that level, return nonzero. */
5721
5722 static int
5723 template_parm_this_level_p (tree t, void* data)
5724 {
5725 int this_level = *(int *)data;
5726 int level;
5727
5728 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5729 level = TEMPLATE_PARM_LEVEL (t);
5730 else
5731 level = TEMPLATE_TYPE_LEVEL (t);
5732 return level == this_level;
5733 }
5734
5735 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5736 DATA is really an int, indicating the innermost outer level of parameters.
5737 If T is a template parameter of that level or further out, return
5738 nonzero. */
5739
5740 static int
5741 template_parm_outer_level (tree t, void *data)
5742 {
5743 int this_level = *(int *)data;
5744 int level;
5745
5746 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5747 level = TEMPLATE_PARM_LEVEL (t);
5748 else
5749 level = TEMPLATE_TYPE_LEVEL (t);
5750 return level <= this_level;
5751 }
5752
5753 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5754 parameters given by current_template_args, or reuses a
5755 previously existing one, if appropriate. Returns the DECL, or an
5756 equivalent one, if it is replaced via a call to duplicate_decls.
5757
5758 If IS_FRIEND is true, DECL is a friend declaration. */
5759
5760 tree
5761 push_template_decl (tree decl, bool is_friend)
5762 {
5763 if (decl == error_mark_node || !current_template_parms)
5764 return error_mark_node;
5765
5766 /* See if this is a partial specialization. */
5767 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5768 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5769 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5770 || (VAR_P (decl)
5771 && DECL_LANG_SPECIFIC (decl)
5772 && DECL_TEMPLATE_SPECIALIZATION (decl)
5773 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5774
5775 /* No surprising friend functions. */
5776 gcc_checking_assert (is_friend
5777 || !(TREE_CODE (decl) == FUNCTION_DECL
5778 && DECL_UNIQUE_FRIEND_P (decl)));
5779
5780 tree ctx;
5781 if (is_friend)
5782 /* For a friend, we want the context of the friend, not
5783 the type of which it is a friend. */
5784 ctx = CP_DECL_CONTEXT (decl);
5785 else if (CP_DECL_CONTEXT (decl)
5786 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5787 /* In the case of a virtual function, we want the class in which
5788 it is defined. */
5789 ctx = CP_DECL_CONTEXT (decl);
5790 else
5791 /* Otherwise, if we're currently defining some class, the DECL
5792 is assumed to be a member of the class. */
5793 ctx = current_scope ();
5794
5795 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5796 ctx = NULL_TREE;
5797
5798 if (!DECL_CONTEXT (decl))
5799 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5800
5801 /* See if this is a primary template. */
5802 bool is_primary = false;
5803 if (is_friend && ctx
5804 && uses_template_parms_level (ctx, current_template_depth))
5805 /* A friend template that specifies a class context, i.e.
5806 template <typename T> friend void A<T>::f();
5807 is not primary. */
5808 ;
5809 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5810 /* Lambdas are not primary. */
5811 ;
5812 else
5813 is_primary = template_parm_scope_p ();
5814
5815 /* True if the template is a member template, in the sense of
5816 [temp.mem]. */
5817 bool member_template_p = false;
5818
5819 if (is_primary)
5820 {
5821 warning (OPT_Wtemplates, "template %qD declared", decl);
5822
5823 if (DECL_CLASS_SCOPE_P (decl))
5824 member_template_p = true;
5825
5826 if (TREE_CODE (decl) == TYPE_DECL
5827 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5828 {
5829 error ("template class without a name");
5830 return error_mark_node;
5831 }
5832 else if (TREE_CODE (decl) == FUNCTION_DECL)
5833 {
5834 if (member_template_p)
5835 {
5836 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5837 error ("member template %qD may not have virt-specifiers", decl);
5838 }
5839 if (DECL_DESTRUCTOR_P (decl))
5840 {
5841 /* [temp.mem]
5842
5843 A destructor shall not be a member template. */
5844 error_at (DECL_SOURCE_LOCATION (decl),
5845 "destructor %qD declared as member template", decl);
5846 return error_mark_node;
5847 }
5848 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5849 && (!prototype_p (TREE_TYPE (decl))
5850 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5851 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5852 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5853 == void_list_node)))
5854 {
5855 /* [basic.stc.dynamic.allocation]
5856
5857 An allocation function can be a function
5858 template. ... Template allocation functions shall
5859 have two or more parameters. */
5860 error ("invalid template declaration of %qD", decl);
5861 return error_mark_node;
5862 }
5863 }
5864 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5865 && CLASS_TYPE_P (TREE_TYPE (decl)))
5866 /* Class template. */;
5867 else if (TREE_CODE (decl) == TYPE_DECL
5868 && TYPE_DECL_ALIAS_P (decl))
5869 /* alias-declaration */
5870 gcc_assert (!DECL_ARTIFICIAL (decl));
5871 else if (VAR_P (decl))
5872 /* C++14 variable template. */;
5873 else if (TREE_CODE (decl) == CONCEPT_DECL)
5874 /* C++20 concept definitions. */;
5875 else
5876 {
5877 error ("template declaration of %q#D", decl);
5878 return error_mark_node;
5879 }
5880 }
5881
5882 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5883 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5884 || (VAR_OR_FUNCTION_DECL_P (decl)
5885 && DECL_LOCAL_DECL_P (decl))));
5886
5887 /* Check to see that the rules regarding the use of default
5888 arguments are not being violated. We check args for a friend
5889 functions when we know whether it's a definition, introducing
5890 declaration or re-declaration. */
5891 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5892 check_default_tmpl_args (decl, current_template_parms,
5893 is_primary, is_partial, is_friend);
5894
5895 /* Ensure that there are no parameter packs in the type of this
5896 declaration that have not been expanded. */
5897 if (TREE_CODE (decl) == FUNCTION_DECL)
5898 {
5899 /* Check each of the arguments individually to see if there are
5900 any bare parameter packs. */
5901 tree type = TREE_TYPE (decl);
5902 tree arg = DECL_ARGUMENTS (decl);
5903 tree argtype = TYPE_ARG_TYPES (type);
5904
5905 while (arg && argtype)
5906 {
5907 if (!DECL_PACK_P (arg)
5908 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5909 {
5910 /* This is a PARM_DECL that contains unexpanded parameter
5911 packs. We have already complained about this in the
5912 check_for_bare_parameter_packs call, so just replace
5913 these types with ERROR_MARK_NODE. */
5914 TREE_TYPE (arg) = error_mark_node;
5915 TREE_VALUE (argtype) = error_mark_node;
5916 }
5917
5918 arg = DECL_CHAIN (arg);
5919 argtype = TREE_CHAIN (argtype);
5920 }
5921
5922 /* Check for bare parameter packs in the return type and the
5923 exception specifiers. */
5924 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5925 /* Errors were already issued, set return type to int
5926 as the frontend doesn't expect error_mark_node as
5927 the return type. */
5928 TREE_TYPE (type) = integer_type_node;
5929 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5930 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5931 }
5932 else
5933 {
5934 if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5935 ? DECL_ORIGINAL_TYPE (decl)
5936 : TREE_TYPE (decl)))
5937 {
5938 TREE_TYPE (decl) = error_mark_node;
5939 return error_mark_node;
5940 }
5941
5942 if (is_partial && VAR_P (decl)
5943 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
5944 return error_mark_node;
5945 }
5946
5947 if (is_partial)
5948 return process_partial_specialization (decl);
5949
5950 tree args = current_template_args ();
5951 tree tmpl = NULL_TREE;
5952 bool new_template_p = false;
5953 if (local_p)
5954 {
5955 /* Does not get a template head. */
5956 tmpl = NULL_TREE;
5957 gcc_checking_assert (!is_primary);
5958 }
5959 else if (!ctx
5960 || TREE_CODE (ctx) == FUNCTION_DECL
5961 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5962 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5963 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5964 && DECL_TEMPLATE_INFO (decl))))
5965 {
5966 if (DECL_LANG_SPECIFIC (decl)
5967 && DECL_TEMPLATE_INFO (decl)
5968 && DECL_TI_TEMPLATE (decl))
5969 tmpl = DECL_TI_TEMPLATE (decl);
5970 /* If DECL is a TYPE_DECL for a class-template, then there won't
5971 be DECL_LANG_SPECIFIC. The information equivalent to
5972 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5973 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5974 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5975 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5976 {
5977 /* Since a template declaration already existed for this
5978 class-type, we must be redeclaring it here. Make sure
5979 that the redeclaration is valid. */
5980 redeclare_class_template (TREE_TYPE (decl),
5981 current_template_parms,
5982 current_template_constraints ());
5983 /* We don't need to create a new TEMPLATE_DECL; just use the
5984 one we already had. */
5985 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5986 }
5987 else
5988 {
5989 tmpl = build_template_decl (decl, current_template_parms,
5990 member_template_p);
5991 new_template_p = true;
5992
5993 if (DECL_LANG_SPECIFIC (decl)
5994 && DECL_TEMPLATE_SPECIALIZATION (decl))
5995 {
5996 /* A specialization of a member template of a template
5997 class. */
5998 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5999 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
6000 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
6001 }
6002 }
6003 }
6004 else
6005 {
6006 tree a, t, current, parms;
6007 int i;
6008 tree tinfo = get_template_info (decl);
6009
6010 if (!tinfo)
6011 {
6012 error ("template definition of non-template %q#D", decl);
6013 return error_mark_node;
6014 }
6015
6016 tmpl = TI_TEMPLATE (tinfo);
6017
6018 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
6019 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
6020 && DECL_TEMPLATE_SPECIALIZATION (decl)
6021 && DECL_MEMBER_TEMPLATE_P (tmpl))
6022 {
6023 /* The declaration is a specialization of a member
6024 template, declared outside the class. Therefore, the
6025 innermost template arguments will be NULL, so we
6026 replace them with the arguments determined by the
6027 earlier call to check_explicit_specialization. */
6028 args = DECL_TI_ARGS (decl);
6029
6030 tree new_tmpl
6031 = build_template_decl (decl, current_template_parms,
6032 member_template_p);
6033 DECL_TI_TEMPLATE (decl) = new_tmpl;
6034 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
6035 DECL_TEMPLATE_INFO (new_tmpl)
6036 = build_template_info (tmpl, args);
6037
6038 register_specialization (new_tmpl,
6039 most_general_template (tmpl),
6040 args,
6041 is_friend, 0);
6042 return decl;
6043 }
6044
6045 /* Make sure the template headers we got make sense. */
6046
6047 parms = DECL_TEMPLATE_PARMS (tmpl);
6048 i = TMPL_PARMS_DEPTH (parms);
6049 if (TMPL_ARGS_DEPTH (args) != i)
6050 {
6051 error ("expected %d levels of template parms for %q#D, got %d",
6052 i, decl, TMPL_ARGS_DEPTH (args));
6053 DECL_INTERFACE_KNOWN (decl) = 1;
6054 return error_mark_node;
6055 }
6056 else
6057 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
6058 {
6059 a = TMPL_ARGS_LEVEL (args, i);
6060 t = INNERMOST_TEMPLATE_PARMS (parms);
6061
6062 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6063 {
6064 if (current == decl)
6065 error ("got %d template parameters for %q#D",
6066 TREE_VEC_LENGTH (a), decl);
6067 else
6068 error ("got %d template parameters for %q#T",
6069 TREE_VEC_LENGTH (a), current);
6070 error (" but %d required", TREE_VEC_LENGTH (t));
6071 /* Avoid crash in import_export_decl. */
6072 DECL_INTERFACE_KNOWN (decl) = 1;
6073 return error_mark_node;
6074 }
6075
6076 if (current == decl)
6077 current = ctx;
6078 else if (current == NULL_TREE)
6079 /* Can happen in erroneous input. */
6080 break;
6081 else
6082 current = get_containing_scope (current);
6083 }
6084
6085 /* Check that the parms are used in the appropriate qualifying scopes
6086 in the declarator. */
6087 if (!comp_template_args
6088 (TI_ARGS (tinfo),
6089 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6090 {
6091 error ("template arguments to %qD do not match original "
6092 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6093 if (!uses_template_parms (TI_ARGS (tinfo)))
6094 inform (input_location, "use %<template<>%> for"
6095 " an explicit specialization");
6096 /* Avoid crash in import_export_decl. */
6097 DECL_INTERFACE_KNOWN (decl) = 1;
6098 return error_mark_node;
6099 }
6100 }
6101
6102 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6103
6104 if (new_template_p)
6105 {
6106 /* Push template declarations for global functions and types.
6107 Note that we do not try to push a global template friend
6108 declared in a template class; such a thing may well depend on
6109 the template parameters of the class and we'll push it when
6110 instantiating the befriending class. */
6111 if (!ctx
6112 && !(is_friend && template_class_depth (current_class_type) > 0))
6113 {
6114 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6115 if (pushed == error_mark_node)
6116 return error_mark_node;
6117
6118 /* pushdecl may have found an existing template. */
6119 if (pushed != tmpl)
6120 {
6121 decl = DECL_TEMPLATE_RESULT (pushed);
6122 tmpl = NULL_TREE;
6123 }
6124 }
6125 else if (is_friend)
6126 {
6127 /* Record this decl as belonging to the current class. It's
6128 not chained onto anything else. */
6129 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6130 gcc_checking_assert (!DECL_CHAIN (tmpl));
6131 DECL_CHAIN (tmpl) = current_scope ();
6132 }
6133 }
6134 else if (tmpl)
6135 /* The type may have been completed, or (erroneously) changed. */
6136 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6137
6138 if (tmpl)
6139 {
6140 if (is_primary)
6141 {
6142 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6143
6144 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6145
6146 /* Give template template parms a DECL_CONTEXT of the template
6147 for which they are a parameter. */
6148 parms = INNERMOST_TEMPLATE_PARMS (parms);
6149 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6150 {
6151 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6152 if (TREE_CODE (parm) == TEMPLATE_DECL)
6153 DECL_CONTEXT (parm) = tmpl;
6154 }
6155
6156 if (TREE_CODE (decl) == TYPE_DECL
6157 && TYPE_DECL_ALIAS_P (decl))
6158 {
6159 if (tree constr
6160 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6161 {
6162 /* ??? Why don't we do this here for all templates? */
6163 constr = build_constraints (constr, NULL_TREE);
6164 set_constraints (decl, constr);
6165 }
6166 if (complex_alias_template_p (tmpl))
6167 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6168 }
6169 }
6170
6171 /* The DECL_TI_ARGS of DECL contains full set of arguments
6172 referring wback to its most general template. If TMPL is a
6173 specialization, ARGS may only have the innermost set of
6174 arguments. Add the missing argument levels if necessary. */
6175 if (DECL_TEMPLATE_INFO (tmpl))
6176 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6177
6178 tree info = build_template_info (tmpl, args);
6179
6180 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6181 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6182 else
6183 {
6184 retrofit_lang_decl (decl);
6185 DECL_TEMPLATE_INFO (decl) = info;
6186 }
6187 }
6188
6189 if (flag_implicit_templates
6190 && !is_friend
6191 && TREE_PUBLIC (decl)
6192 && VAR_OR_FUNCTION_DECL_P (decl))
6193 /* Set DECL_COMDAT on template instantiations; if we force
6194 them to be emitted by explicit instantiation,
6195 mark_needed will tell cgraph to do the right thing. */
6196 DECL_COMDAT (decl) = true;
6197
6198 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6199
6200 return decl;
6201 }
6202
6203 /* FN is an inheriting constructor that inherits from the constructor
6204 template INHERITED; turn FN into a constructor template with a matching
6205 template header. */
6206
6207 tree
6208 add_inherited_template_parms (tree fn, tree inherited)
6209 {
6210 tree inner_parms
6211 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6212 inner_parms = copy_node (inner_parms);
6213 tree parms
6214 = tree_cons (size_int (current_template_depth + 1),
6215 inner_parms, current_template_parms);
6216 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6217 tree args = template_parms_to_args (parms);
6218 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6219 DECL_ARTIFICIAL (tmpl) = true;
6220 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6221 return tmpl;
6222 }
6223
6224 /* Called when a class template TYPE is redeclared with the indicated
6225 template PARMS, e.g.:
6226
6227 template <class T> struct S;
6228 template <class T> struct S {}; */
6229
6230 bool
6231 redeclare_class_template (tree type, tree parms, tree cons)
6232 {
6233 tree tmpl;
6234 tree tmpl_parms;
6235 int i;
6236
6237 if (!TYPE_TEMPLATE_INFO (type))
6238 {
6239 error ("%qT is not a template type", type);
6240 return false;
6241 }
6242
6243 tmpl = TYPE_TI_TEMPLATE (type);
6244 if (!PRIMARY_TEMPLATE_P (tmpl))
6245 /* The type is nested in some template class. Nothing to worry
6246 about here; there are no new template parameters for the nested
6247 type. */
6248 return true;
6249
6250 if (!parms)
6251 {
6252 error ("template specifiers not specified in declaration of %qD",
6253 tmpl);
6254 return false;
6255 }
6256
6257 parms = INNERMOST_TEMPLATE_PARMS (parms);
6258 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6259
6260 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6261 {
6262 error_n (input_location, TREE_VEC_LENGTH (parms),
6263 "redeclared with %d template parameter",
6264 "redeclared with %d template parameters",
6265 TREE_VEC_LENGTH (parms));
6266 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6267 "previous declaration %qD used %d template parameter",
6268 "previous declaration %qD used %d template parameters",
6269 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6270 return false;
6271 }
6272
6273 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6274 {
6275 tree tmpl_parm;
6276 tree parm;
6277
6278 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6279 || TREE_VEC_ELT (parms, i) == error_mark_node)
6280 continue;
6281
6282 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6283 if (error_operand_p (tmpl_parm))
6284 return false;
6285
6286 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6287
6288 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6289 TEMPLATE_DECL. */
6290 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6291 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6292 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6293 || (TREE_CODE (tmpl_parm) != PARM_DECL
6294 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6295 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6296 || (TREE_CODE (tmpl_parm) == PARM_DECL
6297 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6298 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6299 {
6300 auto_diagnostic_group d;
6301 error ("template parameter %q+#D", tmpl_parm);
6302 inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
6303 return false;
6304 }
6305
6306 /* The parameters can be declared to introduce different
6307 constraints. */
6308 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6309 tree p2 = TREE_VEC_ELT (parms, i);
6310 if (!template_parameter_constraints_equivalent_p (p1, p2))
6311 {
6312 auto_diagnostic_group d;
6313 error ("declaration of template parameter %q+#D with different "
6314 "constraints", parm);
6315 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6316 "original declaration appeared here");
6317 return false;
6318 }
6319
6320 /* Give each template template parm in this redeclaration a
6321 DECL_CONTEXT of the template for which they are a parameter. */
6322 if (TREE_CODE (parm) == TEMPLATE_DECL)
6323 {
6324 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6325 DECL_CONTEXT (parm) = tmpl;
6326 }
6327 }
6328
6329 if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
6330 return false;
6331
6332 tree ci = get_constraints (tmpl);
6333 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6334 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6335
6336 /* Two classes with different constraints declare different entities. */
6337 if (!cp_tree_equal (req1, req2))
6338 {
6339 auto_diagnostic_group d;
6340 error_at (input_location, "redeclaration %q#D with different "
6341 "constraints", tmpl);
6342 inform (DECL_SOURCE_LOCATION (tmpl),
6343 "original declaration appeared here");
6344 return false;
6345 }
6346
6347 return true;
6348 }
6349
6350 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6351 to be used when the caller has already checked
6352 (processing_template_decl
6353 && !instantiation_dependent_expression_p (expr)
6354 && potential_constant_expression (expr))
6355 and cleared processing_template_decl. */
6356
6357 tree
6358 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6359 {
6360 return tsubst_copy_and_build (expr,
6361 /*args=*/NULL_TREE,
6362 complain,
6363 /*in_decl=*/NULL_TREE,
6364 /*function_p=*/false,
6365 /*integral_constant_expression_p=*/true);
6366 }
6367
6368 /* Simplify EXPR if it is a non-dependent expression. Returns the
6369 (possibly simplified) expression. */
6370
6371 tree
6372 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6373 {
6374 if (expr == NULL_TREE)
6375 return NULL_TREE;
6376
6377 /* If we're in a template, but EXPR isn't value dependent, simplify
6378 it. We're supposed to treat:
6379
6380 template <typename T> void f(T[1 + 1]);
6381 template <typename T> void f(T[2]);
6382
6383 as two declarations of the same function, for example. */
6384 if (processing_template_decl
6385 && is_nondependent_constant_expression (expr))
6386 {
6387 processing_template_decl_sentinel s;
6388 expr = instantiate_non_dependent_expr_internal (expr, complain);
6389 }
6390 return expr;
6391 }
6392
6393 tree
6394 instantiate_non_dependent_expr (tree expr)
6395 {
6396 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6397 }
6398
6399 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6400 an uninstantiated expression. */
6401
6402 tree
6403 instantiate_non_dependent_or_null (tree expr)
6404 {
6405 if (expr == NULL_TREE)
6406 return NULL_TREE;
6407 if (processing_template_decl)
6408 {
6409 if (!is_nondependent_constant_expression (expr))
6410 expr = NULL_TREE;
6411 else
6412 {
6413 processing_template_decl_sentinel s;
6414 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6415 }
6416 }
6417 return expr;
6418 }
6419
6420 /* True iff T is a specialization of a variable template. */
6421
6422 bool
6423 variable_template_specialization_p (tree t)
6424 {
6425 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6426 return false;
6427 tree tmpl = DECL_TI_TEMPLATE (t);
6428 return variable_template_p (tmpl);
6429 }
6430
6431 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6432 template declaration, or a TYPE_DECL for an alias declaration. */
6433
6434 bool
6435 alias_type_or_template_p (tree t)
6436 {
6437 if (t == NULL_TREE)
6438 return false;
6439 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6440 || (TYPE_P (t)
6441 && TYPE_NAME (t)
6442 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6443 || DECL_ALIAS_TEMPLATE_P (t));
6444 }
6445
6446 /* If T is a specialization of an alias template, return it; otherwise return
6447 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6448
6449 tree
6450 alias_template_specialization_p (const_tree t,
6451 bool transparent_typedefs)
6452 {
6453 if (!TYPE_P (t))
6454 return NULL_TREE;
6455
6456 /* It's an alias template specialization if it's an alias and its
6457 TYPE_NAME is a specialization of a primary template. */
6458 if (typedef_variant_p (t))
6459 {
6460 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6461 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6462 return CONST_CAST_TREE (t);
6463 if (transparent_typedefs)
6464 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6465 (TYPE_NAME (t)),
6466 transparent_typedefs);
6467 }
6468
6469 return NULL_TREE;
6470 }
6471
6472 /* An alias template is complex from a SFINAE perspective if a template-id
6473 using that alias can be ill-formed when the expansion is not, as with
6474 the void_t template. We determine this by checking whether the
6475 expansion for the alias template uses all its template parameters. */
6476
6477 struct uses_all_template_parms_data
6478 {
6479 int level;
6480 bool *seen;
6481 };
6482
6483 static int
6484 uses_all_template_parms_r (tree t, void *data_)
6485 {
6486 struct uses_all_template_parms_data &data
6487 = *(struct uses_all_template_parms_data*)data_;
6488 tree idx = get_template_parm_index (t);
6489
6490 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6491 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6492 return 0;
6493 }
6494
6495 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6496
6497 static int
6498 complex_pack_expansion_r (tree t, void *data_)
6499 {
6500 /* An alias template with a pack expansion that expands a pack from the
6501 enclosing class needs to be considered complex, to avoid confusion with
6502 the same pack being used as an argument to the alias's own template
6503 parameter (91966). */
6504 if (!PACK_EXPANSION_P (t))
6505 return 0;
6506 struct uses_all_template_parms_data &data
6507 = *(struct uses_all_template_parms_data*)data_;
6508 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6509 pack = TREE_CHAIN (pack))
6510 {
6511 tree parm_pack = TREE_VALUE (pack);
6512 if (!TEMPLATE_PARM_P (parm_pack))
6513 continue;
6514 int idx, level;
6515 template_parm_level_and_index (parm_pack, &level, &idx);
6516 if (level < data.level)
6517 return 1;
6518 }
6519 return 0;
6520 }
6521
6522 static bool
6523 complex_alias_template_p (const_tree tmpl)
6524 {
6525 /* A renaming alias isn't complex. */
6526 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6527 return false;
6528
6529 /* Any other constrained alias is complex. */
6530 if (get_constraints (tmpl))
6531 return true;
6532
6533 struct uses_all_template_parms_data data;
6534 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6535 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6536 data.level = TMPL_PARMS_DEPTH (parms);
6537 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6538 data.seen = XALLOCAVEC (bool, len);
6539 for (int i = 0; i < len; ++i)
6540 data.seen[i] = false;
6541
6542 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6543 NULL, true, complex_pack_expansion_r))
6544 return true;
6545 for (int i = 0; i < len; ++i)
6546 if (!data.seen[i])
6547 return true;
6548 return false;
6549 }
6550
6551 /* If T is a specialization of a complex alias template with dependent
6552 template-arguments, return it; otherwise return NULL_TREE. If T is a
6553 typedef to such a specialization, return the specialization. */
6554
6555 tree
6556 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6557 {
6558 if (t == error_mark_node)
6559 return NULL_TREE;
6560 gcc_assert (TYPE_P (t));
6561
6562 if (!typedef_variant_p (t))
6563 return NULL_TREE;
6564
6565 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6566 if (tinfo
6567 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6568 && (any_dependent_template_arguments_p
6569 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6570 return CONST_CAST_TREE (t);
6571
6572 if (transparent_typedefs)
6573 {
6574 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6575 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6576 }
6577
6578 return NULL_TREE;
6579 }
6580
6581 /* Return the number of innermost template parameters in TMPL. */
6582
6583 static int
6584 num_innermost_template_parms (const_tree tmpl)
6585 {
6586 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6587 return TREE_VEC_LENGTH (parms);
6588 }
6589
6590 /* Return either TMPL or another template that it is equivalent to under DR
6591 1286: An alias that just changes the name of a template is equivalent to
6592 the other template. */
6593
6594 static tree
6595 get_underlying_template (tree tmpl)
6596 {
6597 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6598 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6599 {
6600 /* Determine if the alias is equivalent to an underlying template. */
6601 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6602 /* The underlying type may have been ill-formed. Don't proceed. */
6603 if (!orig_type)
6604 break;
6605 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6606 if (!tinfo)
6607 break;
6608
6609 tree underlying = TI_TEMPLATE (tinfo);
6610 if (!PRIMARY_TEMPLATE_P (underlying)
6611 || (num_innermost_template_parms (tmpl)
6612 != num_innermost_template_parms (underlying)))
6613 break;
6614
6615 /* Does the alias add cv-quals? */
6616 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6617 break;
6618
6619 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6620 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6621 break;
6622
6623 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6624 it's appropriate to treat a less-constrained alias as equivalent. */
6625 if (!at_least_as_constrained (underlying, tmpl))
6626 break;
6627
6628 /* Alias is equivalent. Strip it and repeat. */
6629 tmpl = underlying;
6630 }
6631
6632 return tmpl;
6633 }
6634
6635 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6636 must be a reference-to-function or a pointer-to-function type, as specified
6637 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6638 and check that the resulting function has external linkage. */
6639
6640 static tree
6641 convert_nontype_argument_function (tree type, tree expr,
6642 tsubst_flags_t complain)
6643 {
6644 tree fns = expr;
6645 tree fn, fn_no_ptr;
6646 linkage_kind linkage;
6647
6648 fn = instantiate_type (type, fns, tf_none);
6649 if (fn == error_mark_node)
6650 return error_mark_node;
6651
6652 if (value_dependent_expression_p (fn))
6653 goto accept;
6654
6655 fn_no_ptr = fn;
6656 if (REFERENCE_REF_P (fn_no_ptr))
6657 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6658 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6659 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6660 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6661 if (BASELINK_P (fn_no_ptr))
6662 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6663
6664 /* [temp.arg.nontype]/1
6665
6666 A template-argument for a non-type, non-template template-parameter
6667 shall be one of:
6668 [...]
6669 -- the address of an object or function with external [C++11: or
6670 internal] linkage. */
6671
6672 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6673 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6674 {
6675 if (complain & tf_error)
6676 {
6677 location_t loc = cp_expr_loc_or_input_loc (expr);
6678 error_at (loc, "%qE is not a valid template argument for type %qT",
6679 expr, type);
6680 if (TYPE_PTR_P (type))
6681 inform (loc, "it must be the address of a function "
6682 "with external linkage");
6683 else
6684 inform (loc, "it must be the name of a function with "
6685 "external linkage");
6686 }
6687 return NULL_TREE;
6688 }
6689
6690 linkage = decl_linkage (fn_no_ptr);
6691 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6692 {
6693 if (complain & tf_error)
6694 {
6695 location_t loc = cp_expr_loc_or_input_loc (expr);
6696 if (cxx_dialect >= cxx11)
6697 error_at (loc, "%qE is not a valid template argument for type "
6698 "%qT because %qD has no linkage",
6699 expr, type, fn_no_ptr);
6700 else
6701 error_at (loc, "%qE is not a valid template argument for type "
6702 "%qT because %qD does not have external linkage",
6703 expr, type, fn_no_ptr);
6704 }
6705 return NULL_TREE;
6706 }
6707
6708 accept:
6709 if (TYPE_REF_P (type))
6710 {
6711 if (REFERENCE_REF_P (fn))
6712 fn = TREE_OPERAND (fn, 0);
6713 else
6714 fn = build_address (fn);
6715 }
6716 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6717 fn = build_nop (type, fn);
6718
6719 return fn;
6720 }
6721
6722 /* Subroutine of convert_nontype_argument.
6723 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6724 Emit an error otherwise. */
6725
6726 static bool
6727 check_valid_ptrmem_cst_expr (tree type, tree expr,
6728 tsubst_flags_t complain)
6729 {
6730 tree orig_expr = expr;
6731 STRIP_NOPS (expr);
6732 if (null_ptr_cst_p (expr))
6733 return true;
6734 if (TREE_CODE (expr) == PTRMEM_CST
6735 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6736 PTRMEM_CST_CLASS (expr)))
6737 return true;
6738 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6739 return true;
6740 if (processing_template_decl
6741 && TREE_CODE (expr) == ADDR_EXPR
6742 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6743 return true;
6744 if (complain & tf_error)
6745 {
6746 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6747 error_at (loc, "%qE is not a valid template argument for type %qT",
6748 orig_expr, type);
6749 if (TREE_CODE (expr) != PTRMEM_CST)
6750 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6751 else
6752 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6753 }
6754 return false;
6755 }
6756
6757 /* Returns TRUE iff the address of OP is value-dependent.
6758
6759 14.6.2.4 [temp.dep.temp]:
6760 A non-integral non-type template-argument is dependent if its type is
6761 dependent or it has either of the following forms
6762 qualified-id
6763 & qualified-id
6764 and contains a nested-name-specifier which specifies a class-name that
6765 names a dependent type.
6766
6767 We generalize this to just say that the address of a member of a
6768 dependent class is value-dependent; the above doesn't cover the
6769 address of a static data member named with an unqualified-id. */
6770
6771 static bool
6772 has_value_dependent_address (tree op)
6773 {
6774 STRIP_ANY_LOCATION_WRAPPER (op);
6775
6776 /* We could use get_inner_reference here, but there's no need;
6777 this is only relevant for template non-type arguments, which
6778 can only be expressed as &id-expression. */
6779 if (DECL_P (op))
6780 {
6781 tree ctx = CP_DECL_CONTEXT (op);
6782
6783 if (TYPE_P (ctx) && dependent_type_p (ctx))
6784 return true;
6785
6786 if (VAR_P (op)
6787 && TREE_STATIC (op)
6788 && TREE_CODE (ctx) == FUNCTION_DECL
6789 && type_dependent_expression_p (ctx))
6790 return true;
6791 }
6792
6793 return false;
6794 }
6795
6796 /* The next set of functions are used for providing helpful explanatory
6797 diagnostics for failed overload resolution. Their messages should be
6798 indented by two spaces for consistency with the messages in
6799 call.cc */
6800
6801 static int
6802 unify_success (bool /*explain_p*/)
6803 {
6804 return 0;
6805 }
6806
6807 /* Other failure functions should call this one, to provide a single function
6808 for setting a breakpoint on. */
6809
6810 static int
6811 unify_invalid (bool /*explain_p*/)
6812 {
6813 return 1;
6814 }
6815
6816 static int
6817 unify_parameter_deduction_failure (bool explain_p, tree parm)
6818 {
6819 if (explain_p)
6820 inform (input_location,
6821 " couldn%'t deduce template parameter %qD", parm);
6822 return unify_invalid (explain_p);
6823 }
6824
6825 static int
6826 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6827 {
6828 if (explain_p)
6829 inform (input_location,
6830 " types %qT and %qT have incompatible cv-qualifiers",
6831 parm, arg);
6832 return unify_invalid (explain_p);
6833 }
6834
6835 static int
6836 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6837 {
6838 if (explain_p)
6839 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6840 return unify_invalid (explain_p);
6841 }
6842
6843 static int
6844 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6845 {
6846 if (explain_p)
6847 inform (input_location,
6848 " template parameter %qD is not a parameter pack, but "
6849 "argument %qD is",
6850 parm, arg);
6851 return unify_invalid (explain_p);
6852 }
6853
6854 static int
6855 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6856 {
6857 if (explain_p)
6858 inform (input_location,
6859 " template argument %qE does not match "
6860 "pointer-to-member constant %qE",
6861 arg, parm);
6862 return unify_invalid (explain_p);
6863 }
6864
6865 static int
6866 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6867 {
6868 if (explain_p)
6869 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6870 return unify_invalid (explain_p);
6871 }
6872
6873 static int
6874 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6875 {
6876 if (explain_p)
6877 inform (input_location,
6878 " inconsistent parameter pack deduction with %qT and %qT",
6879 old_arg, new_arg);
6880 return unify_invalid (explain_p);
6881 }
6882
6883 static int
6884 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6885 {
6886 if (explain_p)
6887 {
6888 if (TYPE_P (parm))
6889 inform (input_location,
6890 " deduced conflicting types for parameter %qT (%qT and %qT)",
6891 parm, first, second);
6892 else
6893 inform (input_location,
6894 " deduced conflicting values for non-type parameter "
6895 "%qE (%qE and %qE)", parm, first, second);
6896 }
6897 return unify_invalid (explain_p);
6898 }
6899
6900 static int
6901 unify_vla_arg (bool explain_p, tree arg)
6902 {
6903 if (explain_p)
6904 inform (input_location,
6905 " variable-sized array type %qT is not "
6906 "a valid template argument",
6907 arg);
6908 return unify_invalid (explain_p);
6909 }
6910
6911 static int
6912 unify_method_type_error (bool explain_p, tree arg)
6913 {
6914 if (explain_p)
6915 inform (input_location,
6916 " member function type %qT is not a valid template argument",
6917 arg);
6918 return unify_invalid (explain_p);
6919 }
6920
6921 static int
6922 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6923 {
6924 if (explain_p)
6925 {
6926 if (least_p)
6927 inform_n (input_location, wanted,
6928 " candidate expects at least %d argument, %d provided",
6929 " candidate expects at least %d arguments, %d provided",
6930 wanted, have);
6931 else
6932 inform_n (input_location, wanted,
6933 " candidate expects %d argument, %d provided",
6934 " candidate expects %d arguments, %d provided",
6935 wanted, have);
6936 }
6937 return unify_invalid (explain_p);
6938 }
6939
6940 static int
6941 unify_too_many_arguments (bool explain_p, int have, int wanted)
6942 {
6943 return unify_arity (explain_p, have, wanted);
6944 }
6945
6946 static int
6947 unify_too_few_arguments (bool explain_p, int have, int wanted,
6948 bool least_p = false)
6949 {
6950 return unify_arity (explain_p, have, wanted, least_p);
6951 }
6952
6953 static int
6954 unify_arg_conversion (bool explain_p, tree to_type,
6955 tree from_type, tree arg)
6956 {
6957 if (explain_p)
6958 inform (cp_expr_loc_or_input_loc (arg),
6959 " cannot convert %qE (type %qT) to type %qT",
6960 arg, from_type, to_type);
6961 return unify_invalid (explain_p);
6962 }
6963
6964 static int
6965 unify_no_common_base (bool explain_p, enum template_base_result r,
6966 tree parm, tree arg)
6967 {
6968 if (explain_p)
6969 switch (r)
6970 {
6971 case tbr_ambiguous_baseclass:
6972 inform (input_location, " %qT is an ambiguous base class of %qT",
6973 parm, arg);
6974 break;
6975 default:
6976 inform (input_location, " %qT is not derived from %qT", arg, parm);
6977 break;
6978 }
6979 return unify_invalid (explain_p);
6980 }
6981
6982 static int
6983 unify_inconsistent_template_template_parameters (bool explain_p)
6984 {
6985 if (explain_p)
6986 inform (input_location,
6987 " template parameters of a template template argument are "
6988 "inconsistent with other deduced template arguments");
6989 return unify_invalid (explain_p);
6990 }
6991
6992 static int
6993 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6994 {
6995 if (explain_p)
6996 inform (input_location,
6997 " cannot deduce a template for %qT from non-template type %qT",
6998 parm, arg);
6999 return unify_invalid (explain_p);
7000 }
7001
7002 static int
7003 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
7004 {
7005 if (explain_p)
7006 inform (input_location,
7007 " template argument %qE does not match %qE", arg, parm);
7008 return unify_invalid (explain_p);
7009 }
7010
7011 /* True if T is a C++20 template parameter object to store the argument for a
7012 template parameter of class type. */
7013
7014 bool
7015 template_parm_object_p (const_tree t)
7016 {
7017 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
7018 && startswith (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA"));
7019 }
7020
7021 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7022 argument for TYPE, points to an unsuitable object.
7023
7024 Also adjust the type of the index in C++20 array subobject references. */
7025
7026 static bool
7027 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
7028 {
7029 switch (TREE_CODE (expr))
7030 {
7031 CASE_CONVERT:
7032 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
7033 complain);
7034
7035 case TARGET_EXPR:
7036 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
7037 complain);
7038
7039 case CONSTRUCTOR:
7040 {
7041 for (auto &e: CONSTRUCTOR_ELTS (expr))
7042 if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
7043 return true;
7044 }
7045 break;
7046
7047 case ADDR_EXPR:
7048 {
7049 tree decl = TREE_OPERAND (expr, 0);
7050
7051 if (cxx_dialect >= cxx20)
7052 while (TREE_CODE (decl) == COMPONENT_REF
7053 || TREE_CODE (decl) == ARRAY_REF)
7054 {
7055 tree &op = TREE_OPERAND (decl, 1);
7056 if (TREE_CODE (decl) == ARRAY_REF
7057 && TREE_CODE (op) == INTEGER_CST)
7058 /* Canonicalize array offsets to ptrdiff_t; how they were
7059 written doesn't matter for subobject identity. */
7060 op = fold_convert (ptrdiff_type_node, op);
7061 decl = TREE_OPERAND (decl, 0);
7062 }
7063
7064 if (!VAR_P (decl))
7065 {
7066 if (complain & tf_error)
7067 error_at (cp_expr_loc_or_input_loc (expr),
7068 "%qE is not a valid template argument of type %qT "
7069 "because %qE is not a variable", expr, type, decl);
7070 return true;
7071 }
7072 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7073 {
7074 if (complain & tf_error)
7075 error_at (cp_expr_loc_or_input_loc (expr),
7076 "%qE is not a valid template argument of type %qT "
7077 "in C++98 because %qD does not have external linkage",
7078 expr, type, decl);
7079 return true;
7080 }
7081 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7082 && decl_linkage (decl) == lk_none)
7083 {
7084 if (complain & tf_error)
7085 error_at (cp_expr_loc_or_input_loc (expr),
7086 "%qE is not a valid template argument of type %qT "
7087 "because %qD has no linkage", expr, type, decl);
7088 return true;
7089 }
7090 /* C++17: For a non-type template-parameter of reference or pointer
7091 type, the value of the constant expression shall not refer to (or
7092 for a pointer type, shall not be the address of):
7093 * a subobject (4.5),
7094 * a temporary object (15.2),
7095 * a string literal (5.13.5),
7096 * the result of a typeid expression (8.2.8), or
7097 * a predefined __func__ variable (11.4.1). */
7098 else if (DECL_ARTIFICIAL (decl))
7099 {
7100 if (complain & tf_error)
7101 error ("the address of %qD is not a valid template argument",
7102 decl);
7103 return true;
7104 }
7105 else if (cxx_dialect < cxx20
7106 && !(same_type_ignoring_top_level_qualifiers_p
7107 (strip_array_types (TREE_TYPE (type)),
7108 strip_array_types (TREE_TYPE (decl)))))
7109 {
7110 if (complain & tf_error)
7111 error ("the address of the %qT subobject of %qD is not a "
7112 "valid template argument", TREE_TYPE (type), decl);
7113 return true;
7114 }
7115 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7116 {
7117 if (complain & tf_error)
7118 error ("the address of %qD is not a valid template argument "
7119 "because it does not have static storage duration",
7120 decl);
7121 return true;
7122 }
7123 }
7124 break;
7125
7126 default:
7127 if (!INDIRECT_TYPE_P (type))
7128 /* We're only concerned about pointers and references here. */;
7129 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7130 /* Null pointer values are OK in C++11. */;
7131 else
7132 {
7133 if (VAR_P (expr))
7134 {
7135 if (complain & tf_error)
7136 error ("%qD is not a valid template argument "
7137 "because %qD is a variable, not the address of "
7138 "a variable", expr, expr);
7139 return true;
7140 }
7141 else
7142 {
7143 if (complain & tf_error)
7144 error ("%qE is not a valid template argument for %qT "
7145 "because it is not the address of a variable",
7146 expr, type);
7147 return true;
7148 }
7149 }
7150 }
7151 return false;
7152
7153 }
7154
7155 /* The template arguments corresponding to template parameter objects of types
7156 that contain pointers to members. */
7157
7158 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7159
7160 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7161 template argument EXPR. */
7162
7163 static tree
7164 get_template_parm_object (tree expr, tsubst_flags_t complain)
7165 {
7166 if (TREE_CODE (expr) == TARGET_EXPR)
7167 expr = TARGET_EXPR_INITIAL (expr);
7168
7169 if (!TREE_CONSTANT (expr))
7170 {
7171 if ((complain & tf_error)
7172 && require_rvalue_constant_expression (expr))
7173 cxx_constant_value (expr);
7174 return error_mark_node;
7175 }
7176 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7177 return error_mark_node;
7178
7179 /* This is no longer a compound literal. */
7180 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7181
7182 tree name = mangle_template_parm_object (expr);
7183 tree decl = get_global_binding (name);
7184 if (decl)
7185 return decl;
7186
7187 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7188 decl = create_temporary_var (type);
7189 DECL_CONTEXT (decl) = NULL_TREE;
7190 TREE_STATIC (decl) = true;
7191 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7192 TREE_READONLY (decl) = true;
7193 DECL_NAME (decl) = name;
7194 SET_DECL_ASSEMBLER_NAME (decl, name);
7195 comdat_linkage (decl);
7196
7197 if (!zero_init_p (type))
7198 {
7199 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7200 lower_var_init before we're done mangling. So store the original
7201 value elsewhere. */
7202 tree copy = unshare_constructor (expr);
7203 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7204 }
7205
7206 pushdecl_top_level_and_finish (decl, expr);
7207
7208 return decl;
7209 }
7210
7211 /* Return the actual template argument corresponding to template parameter
7212 object VAR. */
7213
7214 tree
7215 tparm_object_argument (tree var)
7216 {
7217 if (zero_init_p (TREE_TYPE (var)))
7218 return DECL_INITIAL (var);
7219 return *(tparm_obj_values->get (var));
7220 }
7221
7222 /* Attempt to convert the non-type template parameter EXPR to the
7223 indicated TYPE. If the conversion is successful, return the
7224 converted value. If the conversion is unsuccessful, return
7225 NULL_TREE if we issued an error message, or error_mark_node if we
7226 did not. We issue error messages for out-and-out bad template
7227 parameters, but not simply because the conversion failed, since we
7228 might be just trying to do argument deduction. Both TYPE and EXPR
7229 must be non-dependent.
7230
7231 The conversion follows the special rules described in
7232 [temp.arg.nontype], and it is much more strict than an implicit
7233 conversion.
7234
7235 This function is called twice for each template argument (see
7236 lookup_template_class for a more accurate description of this
7237 problem). This means that we need to handle expressions which
7238 are not valid in a C++ source, but can be created from the
7239 first call (for instance, casts to perform conversions). These
7240 hacks can go away after we fix the double coercion problem. */
7241
7242 static tree
7243 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7244 {
7245 tree expr_type;
7246 location_t loc = cp_expr_loc_or_input_loc (expr);
7247
7248 /* Detect immediately string literals as invalid non-type argument.
7249 This special-case is not needed for correctness (we would easily
7250 catch this later), but only to provide better diagnostic for this
7251 common user mistake. As suggested by DR 100, we do not mention
7252 linkage issues in the diagnostic as this is not the point. */
7253 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7254 {
7255 if (complain & tf_error)
7256 error ("%qE is not a valid template argument for type %qT "
7257 "because string literals can never be used in this context",
7258 expr, type);
7259 return NULL_TREE;
7260 }
7261
7262 /* Add the ADDR_EXPR now for the benefit of
7263 value_dependent_expression_p. */
7264 if (TYPE_PTROBV_P (type)
7265 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7266 {
7267 expr = decay_conversion (expr, complain);
7268 if (expr == error_mark_node)
7269 return error_mark_node;
7270 }
7271
7272 /* If we are in a template, EXPR may be non-dependent, but still
7273 have a syntactic, rather than semantic, form. For example, EXPR
7274 might be a SCOPE_REF, rather than the VAR_DECL to which the
7275 SCOPE_REF refers. Preserving the qualifying scope is necessary
7276 so that access checking can be performed when the template is
7277 instantiated -- but here we need the resolved form so that we can
7278 convert the argument. */
7279 bool non_dep = false;
7280 if (TYPE_REF_OBJ_P (type)
7281 && has_value_dependent_address (expr))
7282 /* If we want the address and it's value-dependent, don't fold. */;
7283 else if (processing_template_decl
7284 && is_nondependent_constant_expression (expr))
7285 non_dep = true;
7286 if (error_operand_p (expr))
7287 return error_mark_node;
7288 expr_type = TREE_TYPE (expr);
7289
7290 /* If the argument is non-dependent, perform any conversions in
7291 non-dependent context as well. */
7292 processing_template_decl_sentinel s (non_dep);
7293 if (non_dep)
7294 expr = instantiate_non_dependent_expr_internal (expr, complain);
7295
7296 bool val_dep_p = value_dependent_expression_p (expr);
7297 if (val_dep_p)
7298 expr = canonicalize_expr_argument (expr, complain);
7299 else
7300 STRIP_ANY_LOCATION_WRAPPER (expr);
7301
7302 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7303 to a non-type argument of "nullptr". */
7304 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7305 expr = fold_simple (convert (type, expr));
7306
7307 /* In C++11, integral or enumeration non-type template arguments can be
7308 arbitrary constant expressions. Pointer and pointer to
7309 member arguments can be general constant expressions that evaluate
7310 to a null value, but otherwise still need to be of a specific form. */
7311 if (cxx_dialect >= cxx11)
7312 {
7313 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7314 /* A PTRMEM_CST is already constant, and a valid template
7315 argument for a parameter of pointer to member type, we just want
7316 to leave it in that form rather than lower it to a
7317 CONSTRUCTOR. */;
7318 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7319 || cxx_dialect >= cxx17)
7320 {
7321 /* C++17: A template-argument for a non-type template-parameter shall
7322 be a converted constant expression (8.20) of the type of the
7323 template-parameter. */
7324 expr = build_converted_constant_expr (type, expr, complain);
7325 if (expr == error_mark_node)
7326 /* Make sure we return NULL_TREE only if we have really issued
7327 an error, as described above. */
7328 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7329 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7330 {
7331 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7332 return expr;
7333 }
7334 expr = maybe_constant_value (expr, NULL_TREE,
7335 /*manifestly_const_eval=*/true);
7336 expr = convert_from_reference (expr);
7337 /* EXPR may have become value-dependent. */
7338 val_dep_p = value_dependent_expression_p (expr);
7339 }
7340 else if (TYPE_PTR_OR_PTRMEM_P (type))
7341 {
7342 tree folded = maybe_constant_value (expr, NULL_TREE,
7343 /*manifestly_const_eval=*/true);
7344 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7345 : null_member_pointer_value_p (folded))
7346 expr = folded;
7347 }
7348 }
7349
7350 if (TYPE_REF_P (type))
7351 expr = mark_lvalue_use (expr);
7352 else
7353 expr = mark_rvalue_use (expr);
7354
7355 /* HACK: Due to double coercion, we can get a
7356 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7357 which is the tree that we built on the first call (see
7358 below when coercing to reference to object or to reference to
7359 function). We just strip everything and get to the arg.
7360 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7361 for examples. */
7362 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7363 {
7364 /* Check this before we strip *& to avoid redundancy. */
7365 if (!mark_single_function (expr, complain))
7366 return error_mark_node;
7367
7368 tree probe_type, probe = expr;
7369 if (REFERENCE_REF_P (probe))
7370 probe = TREE_OPERAND (probe, 0);
7371 probe_type = TREE_TYPE (probe);
7372 if (TREE_CODE (probe) == NOP_EXPR)
7373 {
7374 /* ??? Maybe we could use convert_from_reference here, but we
7375 would need to relax its constraints because the NOP_EXPR
7376 could actually change the type to something more cv-qualified,
7377 and this is not folded by convert_from_reference. */
7378 tree addr = TREE_OPERAND (probe, 0);
7379 if (TYPE_REF_P (probe_type)
7380 && TREE_CODE (addr) == ADDR_EXPR
7381 && TYPE_PTR_P (TREE_TYPE (addr))
7382 && (same_type_ignoring_top_level_qualifiers_p
7383 (TREE_TYPE (probe_type),
7384 TREE_TYPE (TREE_TYPE (addr)))))
7385 {
7386 expr = TREE_OPERAND (addr, 0);
7387 expr_type = TREE_TYPE (probe_type);
7388 }
7389 }
7390 }
7391
7392 /* [temp.arg.nontype]/5, bullet 1
7393
7394 For a non-type template-parameter of integral or enumeration type,
7395 integral promotions (_conv.prom_) and integral conversions
7396 (_conv.integral_) are applied. */
7397 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7398 || TREE_CODE (type) == REAL_TYPE)
7399 {
7400 if (cxx_dialect < cxx11)
7401 {
7402 tree t = build_converted_constant_expr (type, expr, complain);
7403 t = maybe_constant_value (t);
7404 if (t != error_mark_node)
7405 expr = t;
7406 }
7407
7408 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7409 return error_mark_node;
7410
7411 /* Notice that there are constant expressions like '4 % 0' which
7412 do not fold into integer constants. */
7413 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7414 {
7415 if (complain & tf_error)
7416 {
7417 int errs = errorcount, warns = warningcount + werrorcount;
7418 if (!require_potential_constant_expression (expr))
7419 expr = error_mark_node;
7420 else
7421 expr = cxx_constant_value (expr);
7422 if (errorcount > errs || warningcount + werrorcount > warns)
7423 inform (loc, "in template argument for type %qT", type);
7424 if (expr == error_mark_node)
7425 return NULL_TREE;
7426 /* else cxx_constant_value complained but gave us
7427 a real constant, so go ahead. */
7428 if (!CONSTANT_CLASS_P (expr))
7429 {
7430 /* Some assemble time constant expressions like
7431 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7432 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7433 as we can emit them into .rodata initializers of
7434 variables, yet they can't fold into an INTEGER_CST at
7435 compile time. Refuse them here. */
7436 gcc_checking_assert (reduced_constant_expression_p (expr));
7437 error_at (loc, "template argument %qE for type %qT not "
7438 "a compile-time constant", expr, type);
7439 return NULL_TREE;
7440 }
7441 }
7442 else
7443 return NULL_TREE;
7444 }
7445
7446 /* Avoid typedef problems. */
7447 if (TREE_TYPE (expr) != type)
7448 expr = fold_convert (type, expr);
7449 }
7450 /* [temp.arg.nontype]/5, bullet 2
7451
7452 For a non-type template-parameter of type pointer to object,
7453 qualification conversions (_conv.qual_) and the array-to-pointer
7454 conversion (_conv.array_) are applied. */
7455 else if (TYPE_PTROBV_P (type))
7456 {
7457 tree decayed = expr;
7458
7459 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7460 decay_conversion or an explicit cast. If it's a problematic cast,
7461 we'll complain about it below. */
7462 if (TREE_CODE (expr) == NOP_EXPR)
7463 {
7464 tree probe = expr;
7465 STRIP_NOPS (probe);
7466 if (TREE_CODE (probe) == ADDR_EXPR
7467 && TYPE_PTR_P (TREE_TYPE (probe)))
7468 {
7469 expr = probe;
7470 expr_type = TREE_TYPE (expr);
7471 }
7472 }
7473
7474 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7475
7476 A template-argument for a non-type, non-template template-parameter
7477 shall be one of: [...]
7478
7479 -- the name of a non-type template-parameter;
7480 -- the address of an object or function with external linkage, [...]
7481 expressed as "& id-expression" where the & is optional if the name
7482 refers to a function or array, or if the corresponding
7483 template-parameter is a reference.
7484
7485 Here, we do not care about functions, as they are invalid anyway
7486 for a parameter of type pointer-to-object. */
7487
7488 if (val_dep_p)
7489 /* Non-type template parameters are OK. */
7490 ;
7491 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7492 /* Null pointer values are OK in C++11. */;
7493 else if (TREE_CODE (expr) != ADDR_EXPR
7494 && !INDIRECT_TYPE_P (expr_type))
7495 /* Other values, like integer constants, might be valid
7496 non-type arguments of some other type. */
7497 return error_mark_node;
7498 else if (invalid_tparm_referent_p (type, expr, complain))
7499 return NULL_TREE;
7500
7501 expr = decayed;
7502
7503 expr = perform_qualification_conversions (type, expr);
7504 if (expr == error_mark_node)
7505 return error_mark_node;
7506 }
7507 /* [temp.arg.nontype]/5, bullet 3
7508
7509 For a non-type template-parameter of type reference to object, no
7510 conversions apply. The type referred to by the reference may be more
7511 cv-qualified than the (otherwise identical) type of the
7512 template-argument. The template-parameter is bound directly to the
7513 template-argument, which must be an lvalue. */
7514 else if (TYPE_REF_OBJ_P (type))
7515 {
7516 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7517 expr_type))
7518 return error_mark_node;
7519
7520 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7521 {
7522 if (complain & tf_error)
7523 error ("%qE is not a valid template argument for type %qT "
7524 "because of conflicts in cv-qualification", expr, type);
7525 return NULL_TREE;
7526 }
7527
7528 if (!lvalue_p (expr))
7529 {
7530 if (complain & tf_error)
7531 error ("%qE is not a valid template argument for type %qT "
7532 "because it is not an lvalue", expr, type);
7533 return NULL_TREE;
7534 }
7535
7536 /* [temp.arg.nontype]/1
7537
7538 A template-argument for a non-type, non-template template-parameter
7539 shall be one of: [...]
7540
7541 -- the address of an object or function with external linkage. */
7542 if (INDIRECT_REF_P (expr)
7543 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7544 {
7545 expr = TREE_OPERAND (expr, 0);
7546 if (DECL_P (expr))
7547 {
7548 if (complain & tf_error)
7549 error ("%q#D is not a valid template argument for type %qT "
7550 "because a reference variable does not have a constant "
7551 "address", expr, type);
7552 return NULL_TREE;
7553 }
7554 }
7555
7556 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7557 /* OK, dependent reference. We don't want to ask whether a DECL is
7558 itself value-dependent, since what we want here is its address. */;
7559 else
7560 {
7561 expr = build_address (expr);
7562
7563 if (invalid_tparm_referent_p (type, expr, complain))
7564 return NULL_TREE;
7565 }
7566
7567 if (!same_type_p (type, TREE_TYPE (expr)))
7568 expr = build_nop (type, expr);
7569 }
7570 /* [temp.arg.nontype]/5, bullet 4
7571
7572 For a non-type template-parameter of type pointer to function, only
7573 the function-to-pointer conversion (_conv.func_) is applied. If the
7574 template-argument represents a set of overloaded functions (or a
7575 pointer to such), the matching function is selected from the set
7576 (_over.over_). */
7577 else if (TYPE_PTRFN_P (type))
7578 {
7579 /* If the argument is a template-id, we might not have enough
7580 context information to decay the pointer. */
7581 if (!type_unknown_p (expr_type))
7582 {
7583 expr = decay_conversion (expr, complain);
7584 if (expr == error_mark_node)
7585 return error_mark_node;
7586 }
7587
7588 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7589 /* Null pointer values are OK in C++11. */
7590 return perform_qualification_conversions (type, expr);
7591
7592 expr = convert_nontype_argument_function (type, expr, complain);
7593 if (!expr || expr == error_mark_node)
7594 return expr;
7595 }
7596 /* [temp.arg.nontype]/5, bullet 5
7597
7598 For a non-type template-parameter of type reference to function, no
7599 conversions apply. If the template-argument represents a set of
7600 overloaded functions, the matching function is selected from the set
7601 (_over.over_). */
7602 else if (TYPE_REFFN_P (type))
7603 {
7604 if (TREE_CODE (expr) == ADDR_EXPR)
7605 {
7606 if (complain & tf_error)
7607 {
7608 error ("%qE is not a valid template argument for type %qT "
7609 "because it is a pointer", expr, type);
7610 inform (input_location, "try using %qE instead",
7611 TREE_OPERAND (expr, 0));
7612 }
7613 return NULL_TREE;
7614 }
7615
7616 expr = convert_nontype_argument_function (type, expr, complain);
7617 if (!expr || expr == error_mark_node)
7618 return expr;
7619 }
7620 /* [temp.arg.nontype]/5, bullet 6
7621
7622 For a non-type template-parameter of type pointer to member function,
7623 no conversions apply. If the template-argument represents a set of
7624 overloaded member functions, the matching member function is selected
7625 from the set (_over.over_). */
7626 else if (TYPE_PTRMEMFUNC_P (type))
7627 {
7628 expr = instantiate_type (type, expr, tf_none);
7629 if (expr == error_mark_node)
7630 return error_mark_node;
7631
7632 /* [temp.arg.nontype] bullet 1 says the pointer to member
7633 expression must be a pointer-to-member constant. */
7634 if (!val_dep_p
7635 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7636 return NULL_TREE;
7637
7638 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7639 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7640 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7641 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7642 }
7643 /* [temp.arg.nontype]/5, bullet 7
7644
7645 For a non-type template-parameter of type pointer to data member,
7646 qualification conversions (_conv.qual_) are applied. */
7647 else if (TYPE_PTRDATAMEM_P (type))
7648 {
7649 /* [temp.arg.nontype] bullet 1 says the pointer to member
7650 expression must be a pointer-to-member constant. */
7651 if (!val_dep_p
7652 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7653 return NULL_TREE;
7654
7655 expr = perform_qualification_conversions (type, expr);
7656 if (expr == error_mark_node)
7657 return expr;
7658 }
7659 else if (NULLPTR_TYPE_P (type))
7660 {
7661 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7662 {
7663 if (complain & tf_error)
7664 error ("%qE is not a valid template argument for type %qT "
7665 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7666 return NULL_TREE;
7667 }
7668 return expr;
7669 }
7670 else if (CLASS_TYPE_P (type))
7671 {
7672 /* Replace the argument with a reference to the corresponding template
7673 parameter object. */
7674 if (!val_dep_p)
7675 expr = get_template_parm_object (expr, complain);
7676 if (expr == error_mark_node)
7677 return NULL_TREE;
7678 }
7679 /* A template non-type parameter must be one of the above. */
7680 else
7681 gcc_unreachable ();
7682
7683 /* Sanity check: did we actually convert the argument to the
7684 right type? */
7685 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7686 (type, TREE_TYPE (expr)));
7687 return convert_from_reference (expr);
7688 }
7689
7690 /* Subroutine of coerce_template_template_parms, which returns 1 if
7691 PARM_PARM and ARG_PARM match using the rule for the template
7692 parameters of template template parameters. Both PARM and ARG are
7693 template parameters; the rest of the arguments are the same as for
7694 coerce_template_template_parms.
7695 */
7696 static int
7697 coerce_template_template_parm (tree parm,
7698 tree arg,
7699 tsubst_flags_t complain,
7700 tree in_decl,
7701 tree outer_args)
7702 {
7703 if (arg == NULL_TREE || error_operand_p (arg)
7704 || parm == NULL_TREE || error_operand_p (parm))
7705 return 0;
7706
7707 if (TREE_CODE (arg) != TREE_CODE (parm))
7708 return 0;
7709
7710 switch (TREE_CODE (parm))
7711 {
7712 case TEMPLATE_DECL:
7713 /* We encounter instantiations of templates like
7714 template <template <template <class> class> class TT>
7715 class C; */
7716 {
7717 tree parmparm = DECL_TEMPLATE_PARMS (parm);
7718 tree argparm = DECL_TEMPLATE_PARMS (arg);
7719
7720 if (!coerce_template_template_parms
7721 (parmparm, argparm, complain, in_decl, outer_args))
7722 return 0;
7723 }
7724 /* Fall through. */
7725
7726 case TYPE_DECL:
7727 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7728 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7729 /* Argument is a parameter pack but parameter is not. */
7730 return 0;
7731 break;
7732
7733 case PARM_DECL:
7734 /* The tsubst call is used to handle cases such as
7735
7736 template <int> class C {};
7737 template <class T, template <T> class TT> class D {};
7738 D<int, C> d;
7739
7740 i.e. the parameter list of TT depends on earlier parameters. */
7741 if (!uses_template_parms (TREE_TYPE (arg)))
7742 {
7743 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7744 if (!uses_template_parms (t)
7745 && !same_type_p (t, TREE_TYPE (arg)))
7746 return 0;
7747 }
7748
7749 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7750 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7751 /* Argument is a parameter pack but parameter is not. */
7752 return 0;
7753
7754 break;
7755
7756 default:
7757 gcc_unreachable ();
7758 }
7759
7760 return 1;
7761 }
7762
7763 /* Coerce template argument list ARGLIST for use with template
7764 template-parameter TEMPL. */
7765
7766 static tree
7767 coerce_template_args_for_ttp (tree templ, tree arglist,
7768 tsubst_flags_t complain)
7769 {
7770 /* Consider an example where a template template parameter declared as
7771
7772 template <class T, class U = std::allocator<T> > class TT
7773
7774 The template parameter level of T and U are one level larger than
7775 of TT. To proper process the default argument of U, say when an
7776 instantiation `TT<int>' is seen, we need to build the full
7777 arguments containing {int} as the innermost level. Outer levels,
7778 available when not appearing as default template argument, can be
7779 obtained from the arguments of the enclosing template.
7780
7781 Suppose that TT is later substituted with std::vector. The above
7782 instantiation is `TT<int, std::allocator<T> >' with TT at
7783 level 1, and T at level 2, while the template arguments at level 1
7784 becomes {std::vector} and the inner level 2 is {int}. */
7785
7786 tree outer = DECL_CONTEXT (templ);
7787 if (outer)
7788 outer = generic_targs_for (outer);
7789 else if (current_template_parms)
7790 {
7791 /* This is an argument of the current template, so we haven't set
7792 DECL_CONTEXT yet. */
7793 tree relevant_template_parms;
7794
7795 /* Parameter levels that are greater than the level of the given
7796 template template parm are irrelevant. */
7797 relevant_template_parms = current_template_parms;
7798 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7799 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7800 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7801
7802 outer = template_parms_to_args (relevant_template_parms);
7803 }
7804
7805 if (outer)
7806 arglist = add_to_template_args (outer, arglist);
7807
7808 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7809 return coerce_template_parms (parmlist, arglist, templ,
7810 complain,
7811 /*require_all_args=*/true,
7812 /*use_default_args=*/true);
7813 }
7814
7815 /* A cache of template template parameters with match-all default
7816 arguments. */
7817 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7818
7819 /* T is a bound template template-parameter. Copy its arguments into default
7820 arguments of the template template-parameter's template parameters. */
7821
7822 static tree
7823 add_defaults_to_ttp (tree otmpl)
7824 {
7825 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7826 return *c;
7827
7828 tree ntmpl = copy_node (otmpl);
7829
7830 tree ntype = copy_node (TREE_TYPE (otmpl));
7831 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7832 TYPE_MAIN_VARIANT (ntype) = ntype;
7833 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7834 TYPE_NAME (ntype) = ntmpl;
7835 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7836
7837 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7838 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7839 TEMPLATE_PARM_DECL (idx) = ntmpl;
7840 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7841
7842 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7843 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7844 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7845 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7846 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7847 {
7848 tree o = TREE_VEC_ELT (vec, i);
7849 if (!template_parameter_pack_p (TREE_VALUE (o)))
7850 {
7851 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7852 TREE_PURPOSE (n) = any_targ_node;
7853 }
7854 }
7855
7856 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7857 return ntmpl;
7858 }
7859
7860 /* ARG is a bound potential template template-argument, and PARGS is a list
7861 of arguments for the corresponding template template-parameter. Adjust
7862 PARGS as appropriate for application to ARG's template, and if ARG is a
7863 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7864 arguments to the template template parameter. */
7865
7866 static tree
7867 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7868 {
7869 ++processing_template_decl;
7870 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7871 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7872 {
7873 /* When comparing two template template-parameters in partial ordering,
7874 rewrite the one currently being used as an argument to have default
7875 arguments for all parameters. */
7876 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7877 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7878 if (pargs != error_mark_node)
7879 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7880 TYPE_TI_ARGS (arg));
7881 }
7882 else
7883 {
7884 tree aparms
7885 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7886 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7887 /*require_all*/true,
7888 /*use_default*/true);
7889 }
7890 --processing_template_decl;
7891 return pargs;
7892 }
7893
7894 /* Subroutine of unify for the case when PARM is a
7895 BOUND_TEMPLATE_TEMPLATE_PARM. */
7896
7897 static int
7898 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7899 bool explain_p)
7900 {
7901 tree parmvec = TYPE_TI_ARGS (parm);
7902 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7903
7904 /* The template template parm might be variadic and the argument
7905 not, so flatten both argument lists. */
7906 parmvec = expand_template_argument_pack (parmvec);
7907 argvec = expand_template_argument_pack (argvec);
7908
7909 if (flag_new_ttp)
7910 {
7911 /* In keeping with P0522R0, adjust P's template arguments
7912 to apply to A's template; then flatten it again. */
7913 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7914 nparmvec = expand_template_argument_pack (nparmvec);
7915
7916 if (unify (tparms, targs, nparmvec, argvec,
7917 UNIFY_ALLOW_NONE, explain_p))
7918 return 1;
7919
7920 /* If the P0522 adjustment eliminated a pack expansion, deduce
7921 empty packs. */
7922 if (flag_new_ttp
7923 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7924 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7925 DEDUCE_EXACT, /*sub*/true, explain_p))
7926 return 1;
7927 }
7928 else
7929 {
7930 /* Deduce arguments T, i from TT<T> or TT<i>.
7931 We check each element of PARMVEC and ARGVEC individually
7932 rather than the whole TREE_VEC since they can have
7933 different number of elements, which is allowed under N2555. */
7934
7935 int len = TREE_VEC_LENGTH (parmvec);
7936
7937 /* Check if the parameters end in a pack, making them
7938 variadic. */
7939 int parm_variadic_p = 0;
7940 if (len > 0
7941 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7942 parm_variadic_p = 1;
7943
7944 for (int i = 0; i < len - parm_variadic_p; ++i)
7945 /* If the template argument list of P contains a pack
7946 expansion that is not the last template argument, the
7947 entire template argument list is a non-deduced
7948 context. */
7949 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7950 return unify_success (explain_p);
7951
7952 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7953 return unify_too_few_arguments (explain_p,
7954 TREE_VEC_LENGTH (argvec), len);
7955
7956 for (int i = 0; i < len - parm_variadic_p; ++i)
7957 if (unify (tparms, targs,
7958 TREE_VEC_ELT (parmvec, i),
7959 TREE_VEC_ELT (argvec, i),
7960 UNIFY_ALLOW_NONE, explain_p))
7961 return 1;
7962
7963 if (parm_variadic_p
7964 && unify_pack_expansion (tparms, targs,
7965 parmvec, argvec,
7966 DEDUCE_EXACT,
7967 /*subr=*/true, explain_p))
7968 return 1;
7969 }
7970
7971 return 0;
7972 }
7973
7974 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7975 template template parameters. Both PARM_PARMS and ARG_PARMS are
7976 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7977 or PARM_DECL.
7978
7979 Consider the example:
7980 template <class T> class A;
7981 template<template <class U> class TT> class B;
7982
7983 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7984 the parameters to A, and OUTER_ARGS contains A. */
7985
7986 static int
7987 coerce_template_template_parms (tree parm_parms_full,
7988 tree arg_parms_full,
7989 tsubst_flags_t complain,
7990 tree in_decl,
7991 tree outer_args)
7992 {
7993 int nparms, nargs, i;
7994 tree parm, arg;
7995 int variadic_p = 0;
7996
7997 tree parm_parms = INNERMOST_TEMPLATE_PARMS (parm_parms_full);
7998 tree arg_parms = INNERMOST_TEMPLATE_PARMS (arg_parms_full);
7999
8000 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
8001 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
8002
8003 nparms = TREE_VEC_LENGTH (parm_parms);
8004 nargs = TREE_VEC_LENGTH (arg_parms);
8005
8006 if (flag_new_ttp)
8007 {
8008 /* P0522R0: A template template-parameter P is at least as specialized as
8009 a template template-argument A if, given the following rewrite to two
8010 function templates, the function template corresponding to P is at
8011 least as specialized as the function template corresponding to A
8012 according to the partial ordering rules for function templates
8013 ([temp.func.order]). Given an invented class template X with the
8014 template parameter list of A (including default arguments):
8015
8016 * Each of the two function templates has the same template parameters,
8017 respectively, as P or A.
8018
8019 * Each function template has a single function parameter whose type is
8020 a specialization of X with template arguments corresponding to the
8021 template parameters from the respective function template where, for
8022 each template parameter PP in the template parameter list of the
8023 function template, a corresponding template argument AA is formed. If
8024 PP declares a parameter pack, then AA is the pack expansion
8025 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8026
8027 If the rewrite produces an invalid type, then P is not at least as
8028 specialized as A. */
8029
8030 /* So coerce P's args to apply to A's parms, and then deduce between A's
8031 args and the converted args. If that succeeds, A is at least as
8032 specialized as P, so they match.*/
8033 processing_template_decl_sentinel ptds (/*reset*/false);
8034 ++processing_template_decl;
8035
8036 tree pargs = template_parms_level_to_args (parm_parms);
8037
8038 /* PARM, and thus the context in which we are passing ARG to it, may be
8039 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8040 want to provide the right number of levels, so we reduce the number of
8041 levels in OUTER_ARGS before prepending them. This is most important
8042 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8043
8044 ARG might also be deeper than PARM (ttp23). In that case, we include
8045 all of OUTER_ARGS. The missing levels seem potentially problematic,
8046 but I can't come up with a testcase that breaks. */
8047 if (int arg_outer_levs = TMPL_PARMS_DEPTH (arg_parms_full) - 1)
8048 {
8049 auto x = make_temp_override (TREE_VEC_LENGTH (outer_args));
8050 if (TMPL_ARGS_DEPTH (outer_args) > arg_outer_levs)
8051 TREE_VEC_LENGTH (outer_args) = arg_outer_levs;
8052 pargs = add_to_template_args (outer_args, pargs);
8053 }
8054
8055 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
8056 /*require_all*/true, /*use_default*/true);
8057 if (pargs != error_mark_node)
8058 {
8059 tree targs = make_tree_vec (nargs);
8060 tree aargs = template_parms_level_to_args (arg_parms);
8061 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
8062 /*explain*/false))
8063 return 1;
8064 }
8065 }
8066
8067 /* Determine whether we have a parameter pack at the end of the
8068 template template parameter's template parameter list. */
8069 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
8070 {
8071 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8072
8073 if (error_operand_p (parm))
8074 return 0;
8075
8076 switch (TREE_CODE (parm))
8077 {
8078 case TEMPLATE_DECL:
8079 case TYPE_DECL:
8080 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8081 variadic_p = 1;
8082 break;
8083
8084 case PARM_DECL:
8085 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8086 variadic_p = 1;
8087 break;
8088
8089 default:
8090 gcc_unreachable ();
8091 }
8092 }
8093
8094 if (nargs != nparms
8095 && !(variadic_p && nargs >= nparms - 1))
8096 return 0;
8097
8098 /* Check all of the template parameters except the parameter pack at
8099 the end (if any). */
8100 for (i = 0; i < nparms - variadic_p; ++i)
8101 {
8102 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8103 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8104 continue;
8105
8106 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8107 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8108
8109 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8110 outer_args))
8111 return 0;
8112
8113 }
8114
8115 if (variadic_p)
8116 {
8117 /* Check each of the template parameters in the template
8118 argument against the template parameter pack at the end of
8119 the template template parameter. */
8120 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8121 return 0;
8122
8123 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8124
8125 for (; i < nargs; ++i)
8126 {
8127 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8128 continue;
8129
8130 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8131
8132 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8133 outer_args))
8134 return 0;
8135 }
8136 }
8137
8138 return 1;
8139 }
8140
8141 /* Verifies that the deduced template arguments (in TARGS) for the
8142 template template parameters (in TPARMS) represent valid bindings,
8143 by comparing the template parameter list of each template argument
8144 to the template parameter list of its corresponding template
8145 template parameter, in accordance with DR150. This
8146 routine can only be called after all template arguments have been
8147 deduced. It will return TRUE if all of the template template
8148 parameter bindings are okay, FALSE otherwise. */
8149 bool
8150 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8151 {
8152 int i, ntparms = TREE_VEC_LENGTH (tparms);
8153 bool ret = true;
8154
8155 /* We're dealing with template parms in this process. */
8156 ++processing_template_decl;
8157
8158 targs = INNERMOST_TEMPLATE_ARGS (targs);
8159
8160 for (i = 0; i < ntparms; ++i)
8161 {
8162 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8163 tree targ = TREE_VEC_ELT (targs, i);
8164
8165 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8166 {
8167 tree packed_args = NULL_TREE;
8168 int idx, len = 1;
8169
8170 if (ARGUMENT_PACK_P (targ))
8171 {
8172 /* Look inside the argument pack. */
8173 packed_args = ARGUMENT_PACK_ARGS (targ);
8174 len = TREE_VEC_LENGTH (packed_args);
8175 }
8176
8177 for (idx = 0; idx < len; ++idx)
8178 {
8179 tree targ_parms = NULL_TREE;
8180
8181 if (packed_args)
8182 /* Extract the next argument from the argument
8183 pack. */
8184 targ = TREE_VEC_ELT (packed_args, idx);
8185
8186 if (PACK_EXPANSION_P (targ))
8187 /* Look at the pattern of the pack expansion. */
8188 targ = PACK_EXPANSION_PATTERN (targ);
8189
8190 /* Extract the template parameters from the template
8191 argument. */
8192 if (TREE_CODE (targ) == TEMPLATE_DECL)
8193 targ_parms = DECL_TEMPLATE_PARMS (targ);
8194 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8195 targ_parms = DECL_TEMPLATE_PARMS (TYPE_NAME (targ));
8196
8197 /* Verify that we can coerce the template template
8198 parameters from the template argument to the template
8199 parameter. This requires an exact match. */
8200 if (targ_parms
8201 && !coerce_template_template_parms
8202 (DECL_TEMPLATE_PARMS (tparm),
8203 targ_parms,
8204 tf_none,
8205 tparm,
8206 targs))
8207 {
8208 ret = false;
8209 goto out;
8210 }
8211 }
8212 }
8213 }
8214
8215 out:
8216
8217 --processing_template_decl;
8218 return ret;
8219 }
8220
8221 /* Since type attributes aren't mangled, we need to strip them from
8222 template type arguments. */
8223
8224 tree
8225 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8226 {
8227 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8228 return arg;
8229 bool removed_attributes = false;
8230 tree canon = strip_typedefs (arg, &removed_attributes);
8231 if (removed_attributes
8232 && (complain & tf_warning))
8233 warning (OPT_Wignored_attributes,
8234 "ignoring attributes on template argument %qT", arg);
8235 return canon;
8236 }
8237
8238 /* And from inside dependent non-type arguments like sizeof(Type). */
8239
8240 static tree
8241 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8242 {
8243 if (!arg || arg == error_mark_node)
8244 return arg;
8245 bool removed_attributes = false;
8246 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8247 if (removed_attributes
8248 && (complain & tf_warning))
8249 warning (OPT_Wignored_attributes,
8250 "ignoring attributes in template argument %qE", arg);
8251 return canon;
8252 }
8253
8254 /* A template declaration can be substituted for a constrained
8255 template template parameter only when the argument is no more
8256 constrained than the parameter. */
8257
8258 static bool
8259 is_compatible_template_arg (tree parm, tree arg)
8260 {
8261 tree parm_cons = get_constraints (parm);
8262
8263 /* For now, allow constrained template template arguments
8264 and unconstrained template template parameters. */
8265 if (parm_cons == NULL_TREE)
8266 return true;
8267
8268 /* If the template parameter is constrained, we need to rewrite its
8269 constraints in terms of the ARG's template parameters. This ensures
8270 that all of the template parameter types will have the same depth.
8271
8272 Note that this is only valid when coerce_template_template_parm is
8273 true for the innermost template parameters of PARM and ARG. In other
8274 words, because coercion is successful, this conversion will be valid. */
8275 tree new_args = NULL_TREE;
8276 if (parm_cons)
8277 {
8278 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8279 new_args = template_parms_level_to_args (aparms);
8280 ++processing_template_decl;
8281 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8282 tf_none, NULL_TREE);
8283 --processing_template_decl;
8284 if (parm_cons == error_mark_node)
8285 return false;
8286 }
8287
8288 return weakly_subsumes (parm_cons, arg);
8289 }
8290
8291 // Convert a placeholder argument into a binding to the original
8292 // parameter. The original parameter is saved as the TREE_TYPE of
8293 // ARG.
8294 static inline tree
8295 convert_wildcard_argument (tree parm, tree arg)
8296 {
8297 TREE_TYPE (arg) = parm;
8298 return arg;
8299 }
8300
8301 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8302 because one of them is dependent. But we need to represent the
8303 conversion for the benefit of cp_tree_equal. */
8304
8305 static tree
8306 maybe_convert_nontype_argument (tree type, tree arg)
8307 {
8308 /* Auto parms get no conversion. */
8309 if (type_uses_auto (type))
8310 return arg;
8311 /* We don't need or want to add this conversion now if we're going to use the
8312 argument for deduction. */
8313 if (value_dependent_expression_p (arg))
8314 return arg;
8315
8316 type = cv_unqualified (type);
8317 tree argtype = TREE_TYPE (arg);
8318 if (same_type_p (type, argtype))
8319 return arg;
8320
8321 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8322 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8323 return arg;
8324 }
8325
8326 /* Convert the indicated template ARG as necessary to match the
8327 indicated template PARM. Returns the converted ARG, or
8328 error_mark_node if the conversion was unsuccessful. Error and
8329 warning messages are issued under control of COMPLAIN. This
8330 conversion is for the Ith parameter in the parameter list. ARGS is
8331 the full set of template arguments deduced so far. */
8332
8333 static tree
8334 convert_template_argument (tree parm,
8335 tree arg,
8336 tree args,
8337 tsubst_flags_t complain,
8338 int i,
8339 tree in_decl)
8340 {
8341 tree orig_arg;
8342 tree val;
8343 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8344
8345 if (parm == error_mark_node || error_operand_p (arg))
8346 return error_mark_node;
8347
8348 /* Trivially convert placeholders. */
8349 if (TREE_CODE (arg) == WILDCARD_DECL)
8350 return convert_wildcard_argument (parm, arg);
8351
8352 if (arg == any_targ_node)
8353 return arg;
8354
8355 if (TREE_CODE (arg) == TREE_LIST
8356 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8357 {
8358 /* The template argument was the name of some
8359 member function. That's usually
8360 invalid, but static members are OK. In any
8361 case, grab the underlying fields/functions
8362 and issue an error later if required. */
8363 TREE_TYPE (arg) = unknown_type_node;
8364 }
8365
8366 orig_arg = arg;
8367
8368 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8369 requires_type = (TREE_CODE (parm) == TYPE_DECL
8370 || requires_tmpl_type);
8371
8372 /* When determining whether an argument pack expansion is a template,
8373 look at the pattern. */
8374 if (PACK_EXPANSION_P (arg))
8375 arg = PACK_EXPANSION_PATTERN (arg);
8376
8377 /* Deal with an injected-class-name used as a template template arg. */
8378 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8379 {
8380 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8381 if (TREE_CODE (t) == TEMPLATE_DECL)
8382 {
8383 if (cxx_dialect >= cxx11)
8384 /* OK under DR 1004. */;
8385 else if (complain & tf_warning_or_error)
8386 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8387 " used as template template argument", TYPE_NAME (arg));
8388 else if (flag_pedantic_errors)
8389 t = arg;
8390
8391 arg = t;
8392 }
8393 }
8394
8395 is_tmpl_type =
8396 ((TREE_CODE (arg) == TEMPLATE_DECL
8397 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8398 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8399 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8400 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8401
8402 if (is_tmpl_type
8403 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8404 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8405 arg = TYPE_STUB_DECL (arg);
8406
8407 is_type = TYPE_P (arg) || is_tmpl_type;
8408
8409 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8410 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8411 {
8412 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8413 {
8414 if (complain & tf_error)
8415 error ("invalid use of destructor %qE as a type", orig_arg);
8416 return error_mark_node;
8417 }
8418
8419 permerror (input_location,
8420 "to refer to a type member of a template parameter, "
8421 "use %<typename %E%>", orig_arg);
8422
8423 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8424 TREE_OPERAND (arg, 1),
8425 typename_type,
8426 complain);
8427 arg = orig_arg;
8428 is_type = 1;
8429 }
8430 if (is_type != requires_type)
8431 {
8432 if (in_decl)
8433 {
8434 if (complain & tf_error)
8435 {
8436 error ("type/value mismatch at argument %d in template "
8437 "parameter list for %qD",
8438 i + 1, in_decl);
8439 if (is_type)
8440 {
8441 /* The template argument is a type, but we're expecting
8442 an expression. */
8443 inform (input_location,
8444 " expected a constant of type %qT, got %qT",
8445 TREE_TYPE (parm),
8446 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8447 /* [temp.arg]/2: "In a template-argument, an ambiguity
8448 between a type-id and an expression is resolved to a
8449 type-id, regardless of the form of the corresponding
8450 template-parameter." So give the user a clue. */
8451 if (TREE_CODE (arg) == FUNCTION_TYPE)
8452 inform (input_location, " ambiguous template argument "
8453 "for non-type template parameter is treated as "
8454 "function type");
8455 }
8456 else if (requires_tmpl_type)
8457 inform (input_location,
8458 " expected a class template, got %qE", orig_arg);
8459 else
8460 inform (input_location,
8461 " expected a type, got %qE", orig_arg);
8462 }
8463 }
8464 return error_mark_node;
8465 }
8466 if (is_tmpl_type ^ requires_tmpl_type)
8467 {
8468 if (in_decl && (complain & tf_error))
8469 {
8470 error ("type/value mismatch at argument %d in template "
8471 "parameter list for %qD",
8472 i + 1, in_decl);
8473 if (is_tmpl_type)
8474 inform (input_location,
8475 " expected a type, got %qT", DECL_NAME (arg));
8476 else
8477 inform (input_location,
8478 " expected a class template, got %qT", orig_arg);
8479 }
8480 return error_mark_node;
8481 }
8482
8483 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8484 /* We already did the appropriate conversion when packing args. */
8485 val = orig_arg;
8486 else if (is_type)
8487 {
8488 if (requires_tmpl_type)
8489 {
8490 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8491 /* The number of argument required is not known yet.
8492 Just accept it for now. */
8493 val = orig_arg;
8494 else
8495 {
8496 tree parmparm = DECL_TEMPLATE_PARMS (parm);
8497 tree argparm;
8498
8499 /* Strip alias templates that are equivalent to another
8500 template. */
8501 arg = get_underlying_template (arg);
8502 argparm = DECL_TEMPLATE_PARMS (arg);
8503
8504 if (coerce_template_template_parms (parmparm, argparm,
8505 complain, in_decl,
8506 args))
8507 {
8508 val = arg;
8509
8510 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8511 TEMPLATE_DECL. */
8512 if (val != error_mark_node)
8513 {
8514 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8515 val = TREE_TYPE (val);
8516 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8517 val = make_pack_expansion (val, complain);
8518 }
8519 }
8520 else
8521 {
8522 if (in_decl && (complain & tf_error))
8523 {
8524 error ("type/value mismatch at argument %d in "
8525 "template parameter list for %qD",
8526 i + 1, in_decl);
8527 inform (input_location,
8528 " expected a template of type %qD, got %qT",
8529 parm, orig_arg);
8530 }
8531
8532 val = error_mark_node;
8533 }
8534
8535 // Check that the constraints are compatible before allowing the
8536 // substitution.
8537 if (val != error_mark_node)
8538 if (!is_compatible_template_arg (parm, arg))
8539 {
8540 if (in_decl && (complain & tf_error))
8541 {
8542 error ("constraint mismatch at argument %d in "
8543 "template parameter list for %qD",
8544 i + 1, in_decl);
8545 inform (input_location, " expected %qD but got %qD",
8546 parm, arg);
8547 }
8548 val = error_mark_node;
8549 }
8550 }
8551 }
8552 else
8553 val = orig_arg;
8554 /* We only form one instance of each template specialization.
8555 Therefore, if we use a non-canonical variant (i.e., a
8556 typedef), any future messages referring to the type will use
8557 the typedef, which is confusing if those future uses do not
8558 themselves also use the typedef. */
8559 if (TYPE_P (val))
8560 val = canonicalize_type_argument (val, complain);
8561 }
8562 else
8563 {
8564 tree t = TREE_TYPE (parm);
8565
8566 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8567 > TMPL_ARGS_DEPTH (args))
8568 /* We don't have enough levels of args to do any substitution. This
8569 can happen in the context of -fnew-ttp-matching. */;
8570 else if (tree a = type_uses_auto (t))
8571 {
8572 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8573 LOOKUP_IMPLICIT);
8574 if (t == error_mark_node)
8575 return error_mark_node;
8576 }
8577 else
8578 t = tsubst (t, args, complain, in_decl);
8579
8580 /* Perform array-to-pointer and function-to-pointer conversion
8581 as per [temp.param]/10. */
8582 t = type_decays_to (t);
8583
8584 if (invalid_nontype_parm_type_p (t, complain))
8585 return error_mark_node;
8586
8587 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8588 this non-type template parameter, as per [temp.param]/6. */
8589 t = cv_unqualified (t);
8590
8591 if (t != TREE_TYPE (parm))
8592 t = canonicalize_type_argument (t, complain);
8593
8594 if (!type_dependent_expression_p (orig_arg)
8595 && !uses_template_parms (t))
8596 /* We used to call digest_init here. However, digest_init
8597 will report errors, which we don't want when complain
8598 is zero. More importantly, digest_init will try too
8599 hard to convert things: for example, `0' should not be
8600 converted to pointer type at this point according to
8601 the standard. Accepting this is not merely an
8602 extension, since deciding whether or not these
8603 conversions can occur is part of determining which
8604 function template to call, or whether a given explicit
8605 argument specification is valid. */
8606 val = convert_nontype_argument (t, orig_arg, complain);
8607 else
8608 {
8609 val = canonicalize_expr_argument (orig_arg, complain);
8610 val = maybe_convert_nontype_argument (t, val);
8611 }
8612
8613
8614 if (val == NULL_TREE)
8615 val = error_mark_node;
8616 else if (val == error_mark_node && (complain & tf_error))
8617 error_at (cp_expr_loc_or_input_loc (orig_arg),
8618 "could not convert template argument %qE from %qT to %qT",
8619 orig_arg, TREE_TYPE (orig_arg), t);
8620
8621 if (INDIRECT_REF_P (val))
8622 {
8623 /* Reject template arguments that are references to built-in
8624 functions with no library fallbacks. */
8625 const_tree inner = TREE_OPERAND (val, 0);
8626 const_tree innertype = TREE_TYPE (inner);
8627 if (innertype
8628 && TYPE_REF_P (innertype)
8629 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8630 && TREE_OPERAND_LENGTH (inner) > 0
8631 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8632 return error_mark_node;
8633 }
8634
8635 if (TREE_CODE (val) == SCOPE_REF)
8636 {
8637 /* Strip typedefs from the SCOPE_REF. */
8638 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8639 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8640 complain);
8641 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8642 QUALIFIED_NAME_IS_TEMPLATE (val));
8643 }
8644 }
8645
8646 return val;
8647 }
8648
8649 /* Coerces the remaining template arguments in INNER_ARGS (from
8650 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8651 Returns the coerced argument pack. PARM_IDX is the position of this
8652 parameter in the template parameter list. ARGS is the original
8653 template argument list. */
8654 static tree
8655 coerce_template_parameter_pack (tree parms,
8656 int parm_idx,
8657 tree args,
8658 tree inner_args,
8659 int arg_idx,
8660 tree new_args,
8661 int* lost,
8662 tree in_decl,
8663 tsubst_flags_t complain)
8664 {
8665 tree parm = TREE_VEC_ELT (parms, parm_idx);
8666 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8667 tree packed_args;
8668 tree argument_pack;
8669 tree packed_parms = NULL_TREE;
8670
8671 if (arg_idx > nargs)
8672 arg_idx = nargs;
8673
8674 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8675 {
8676 /* When the template parameter is a non-type template parameter pack
8677 or template template parameter pack whose type or template
8678 parameters use parameter packs, we know exactly how many arguments
8679 we are looking for. Build a vector of the instantiated decls for
8680 these template parameters in PACKED_PARMS. */
8681 /* We can't use make_pack_expansion here because it would interpret a
8682 _DECL as a use rather than a declaration. */
8683 tree decl = TREE_VALUE (parm);
8684 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8685 SET_PACK_EXPANSION_PATTERN (exp, decl);
8686 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8687 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8688
8689 TREE_VEC_LENGTH (args)--;
8690 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8691 TREE_VEC_LENGTH (args)++;
8692
8693 if (packed_parms == error_mark_node)
8694 return error_mark_node;
8695
8696 /* If we're doing a partial instantiation of a member template,
8697 verify that all of the types used for the non-type
8698 template parameter pack are, in fact, valid for non-type
8699 template parameters. */
8700 if (arg_idx < nargs
8701 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8702 {
8703 int j, len = TREE_VEC_LENGTH (packed_parms);
8704 for (j = 0; j < len; ++j)
8705 {
8706 tree t = TREE_VEC_ELT (packed_parms, j);
8707 if (TREE_CODE (t) == PARM_DECL
8708 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8709 return error_mark_node;
8710 }
8711 /* We don't know how many args we have yet, just
8712 use the unconverted ones for now. */
8713 return NULL_TREE;
8714 }
8715
8716 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8717 }
8718 /* Check if we have a placeholder pack, which indicates we're
8719 in the context of a introduction list. In that case we want
8720 to match this pack to the single placeholder. */
8721 else if (arg_idx < nargs
8722 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8723 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8724 {
8725 nargs = arg_idx + 1;
8726 packed_args = make_tree_vec (1);
8727 }
8728 else
8729 packed_args = make_tree_vec (nargs - arg_idx);
8730
8731 /* Convert the remaining arguments, which will be a part of the
8732 parameter pack "parm". */
8733 int first_pack_arg = arg_idx;
8734 for (; arg_idx < nargs; ++arg_idx)
8735 {
8736 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8737 tree actual_parm = TREE_VALUE (parm);
8738 int pack_idx = arg_idx - first_pack_arg;
8739
8740 if (packed_parms)
8741 {
8742 /* Once we've packed as many args as we have types, stop. */
8743 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8744 break;
8745 else if (PACK_EXPANSION_P (arg))
8746 /* We don't know how many args we have yet, just
8747 use the unconverted ones for now. */
8748 return NULL_TREE;
8749 else
8750 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8751 }
8752
8753 if (arg == error_mark_node)
8754 {
8755 if (complain & tf_error)
8756 error ("template argument %d is invalid", arg_idx + 1);
8757 }
8758 else
8759 arg = convert_template_argument (actual_parm,
8760 arg, new_args, complain, parm_idx,
8761 in_decl);
8762 if (arg == error_mark_node)
8763 (*lost)++;
8764 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8765 }
8766
8767 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8768 && TREE_VEC_LENGTH (packed_args) > 0)
8769 {
8770 if (complain & tf_error)
8771 error ("wrong number of template arguments (%d, should be %d)",
8772 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8773 return error_mark_node;
8774 }
8775
8776 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8777 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8778 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8779 else
8780 {
8781 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8782 TREE_CONSTANT (argument_pack) = 1;
8783 }
8784
8785 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8786 if (CHECKING_P)
8787 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8788 TREE_VEC_LENGTH (packed_args));
8789 return argument_pack;
8790 }
8791
8792 /* Returns the number of pack expansions in the template argument vector
8793 ARGS. */
8794
8795 static int
8796 pack_expansion_args_count (tree args)
8797 {
8798 int i;
8799 int count = 0;
8800 if (args)
8801 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8802 {
8803 tree elt = TREE_VEC_ELT (args, i);
8804 if (elt && PACK_EXPANSION_P (elt))
8805 ++count;
8806 }
8807 return count;
8808 }
8809
8810 /* Convert all template arguments to their appropriate types, and
8811 return a vector containing the innermost resulting template
8812 arguments. If any error occurs, return error_mark_node. Error and
8813 warning messages are issued under control of COMPLAIN.
8814
8815 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8816 for arguments not specified in ARGS. Otherwise, if
8817 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8818 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8819 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8820 ARGS. */
8821
8822 static tree
8823 coerce_template_parms (tree parms,
8824 tree args,
8825 tree in_decl,
8826 tsubst_flags_t complain,
8827 bool require_all_args,
8828 bool use_default_args)
8829 {
8830 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8831 tree orig_inner_args;
8832 tree inner_args;
8833 tree new_args;
8834 tree new_inner_args;
8835
8836 /* When used as a boolean value, indicates whether this is a
8837 variadic template parameter list. Since it's an int, we can also
8838 subtract it from nparms to get the number of non-variadic
8839 parameters. */
8840 int variadic_p = 0;
8841 int variadic_args_p = 0;
8842 int post_variadic_parms = 0;
8843
8844 /* Adjustment to nparms for fixed parameter packs. */
8845 int fixed_pack_adjust = 0;
8846 int fixed_packs = 0;
8847 int missing = 0;
8848
8849 /* Likewise for parameters with default arguments. */
8850 int default_p = 0;
8851
8852 if (args == error_mark_node)
8853 return error_mark_node;
8854
8855 nparms = TREE_VEC_LENGTH (parms);
8856
8857 /* Determine if there are any parameter packs or default arguments. */
8858 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8859 {
8860 tree parm = TREE_VEC_ELT (parms, parm_idx);
8861 if (variadic_p)
8862 ++post_variadic_parms;
8863 if (template_parameter_pack_p (TREE_VALUE (parm)))
8864 ++variadic_p;
8865 if (TREE_PURPOSE (parm))
8866 ++default_p;
8867 }
8868
8869 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8870 /* If there are no parameters that follow a parameter pack, we need to
8871 expand any argument packs so that we can deduce a parameter pack from
8872 some non-packed args followed by an argument pack, as in variadic85.C.
8873 If there are such parameters, we need to leave argument packs intact
8874 so the arguments are assigned properly. This can happen when dealing
8875 with a nested class inside a partial specialization of a class
8876 template, as in variadic92.C, or when deducing a template parameter pack
8877 from a sub-declarator, as in variadic114.C. */
8878 if (!post_variadic_parms)
8879 inner_args = expand_template_argument_pack (inner_args);
8880
8881 /* Count any pack expansion args. */
8882 variadic_args_p = pack_expansion_args_count (inner_args);
8883
8884 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8885 if ((nargs - variadic_args_p > nparms && !variadic_p)
8886 || (nargs < nparms - variadic_p
8887 && require_all_args
8888 && !variadic_args_p
8889 && (!use_default_args
8890 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8891 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8892 {
8893 bad_nargs:
8894 if (complain & tf_error)
8895 {
8896 if (variadic_p || default_p)
8897 {
8898 nparms -= variadic_p + default_p;
8899 error ("wrong number of template arguments "
8900 "(%d, should be at least %d)", nargs, nparms);
8901 }
8902 else
8903 error ("wrong number of template arguments "
8904 "(%d, should be %d)", nargs, nparms);
8905
8906 if (in_decl)
8907 inform (DECL_SOURCE_LOCATION (in_decl),
8908 "provided for %qD", in_decl);
8909 }
8910
8911 return error_mark_node;
8912 }
8913 /* We can't pass a pack expansion to a non-pack parameter of an alias
8914 template (DR 1430). */
8915 else if (in_decl
8916 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8917 || concept_definition_p (in_decl))
8918 && variadic_args_p
8919 && nargs - variadic_args_p < nparms - variadic_p)
8920 {
8921 if (complain & tf_error)
8922 {
8923 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8924 {
8925 tree arg = TREE_VEC_ELT (inner_args, i);
8926 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8927
8928 if (PACK_EXPANSION_P (arg)
8929 && !template_parameter_pack_p (parm))
8930 {
8931 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8932 error_at (location_of (arg),
8933 "pack expansion argument for non-pack parameter "
8934 "%qD of alias template %qD", parm, in_decl);
8935 else
8936 error_at (location_of (arg),
8937 "pack expansion argument for non-pack parameter "
8938 "%qD of concept %qD", parm, in_decl);
8939 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8940 goto found;
8941 }
8942 }
8943 gcc_unreachable ();
8944 found:;
8945 }
8946 return error_mark_node;
8947 }
8948
8949 /* We need to evaluate the template arguments, even though this
8950 template-id may be nested within a "sizeof". */
8951 cp_evaluated ev;
8952
8953 new_inner_args = make_tree_vec (nparms);
8954 new_args = add_outermost_template_args (args, new_inner_args);
8955 int pack_adjust = 0;
8956 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8957 {
8958 tree arg;
8959 tree parm;
8960
8961 /* Get the Ith template parameter. */
8962 parm = TREE_VEC_ELT (parms, parm_idx);
8963
8964 if (parm == error_mark_node)
8965 {
8966 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8967 continue;
8968 }
8969
8970 /* Calculate the next argument. */
8971 if (arg_idx < nargs)
8972 arg = TREE_VEC_ELT (inner_args, arg_idx);
8973 else
8974 arg = NULL_TREE;
8975
8976 if (template_parameter_pack_p (TREE_VALUE (parm))
8977 && (arg || require_all_args || !(complain & tf_partial))
8978 && !(arg && ARGUMENT_PACK_P (arg)))
8979 {
8980 /* Some arguments will be placed in the
8981 template parameter pack PARM. */
8982 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8983 inner_args, arg_idx,
8984 new_args, &lost,
8985 in_decl, complain);
8986
8987 if (arg == NULL_TREE)
8988 {
8989 /* We don't know how many args we have yet, just use the
8990 unconverted (and still packed) ones for now. */
8991 new_inner_args = orig_inner_args;
8992 arg_idx = nargs;
8993 break;
8994 }
8995
8996 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8997
8998 /* Store this argument. */
8999 if (arg == error_mark_node)
9000 {
9001 lost++;
9002 /* We are done with all of the arguments. */
9003 arg_idx = nargs;
9004 break;
9005 }
9006 else
9007 {
9008 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
9009 arg_idx += pack_adjust;
9010 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
9011 {
9012 ++fixed_packs;
9013 fixed_pack_adjust += pack_adjust;
9014 }
9015 }
9016
9017 continue;
9018 }
9019 else if (arg)
9020 {
9021 if (PACK_EXPANSION_P (arg))
9022 {
9023 /* "If every valid specialization of a variadic template
9024 requires an empty template parameter pack, the template is
9025 ill-formed, no diagnostic required." So check that the
9026 pattern works with this parameter. */
9027 tree pattern = PACK_EXPANSION_PATTERN (arg);
9028 tree conv = convert_template_argument (TREE_VALUE (parm),
9029 pattern, new_args,
9030 complain, parm_idx,
9031 in_decl);
9032 if (conv == error_mark_node)
9033 {
9034 if (complain & tf_error)
9035 inform (input_location, "so any instantiation with a "
9036 "non-empty parameter pack would be ill-formed");
9037 ++lost;
9038 }
9039 else if (TYPE_P (conv) && !TYPE_P (pattern))
9040 /* Recover from missing typename. */
9041 TREE_VEC_ELT (inner_args, arg_idx)
9042 = make_pack_expansion (conv, complain);
9043
9044 /* We don't know how many args we have yet, just
9045 use the unconverted ones for now. */
9046 new_inner_args = inner_args;
9047 arg_idx = nargs;
9048 break;
9049 }
9050 }
9051 else if (require_all_args)
9052 {
9053 /* There must be a default arg in this case. */
9054 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
9055 complain, in_decl);
9056 /* The position of the first default template argument,
9057 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9058 Record that. */
9059 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9060 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9061 arg_idx - pack_adjust);
9062 }
9063 else
9064 break;
9065
9066 if (arg == error_mark_node)
9067 {
9068 if (complain & tf_error)
9069 error ("template argument %d is invalid", arg_idx + 1);
9070 }
9071 else if (!arg)
9072 {
9073 /* This can occur if there was an error in the template
9074 parameter list itself (which we would already have
9075 reported) that we are trying to recover from, e.g., a class
9076 template with a parameter list such as
9077 template<typename..., typename> (cpp0x/variadic150.C). */
9078 ++lost;
9079
9080 /* This can also happen with a fixed parameter pack (71834). */
9081 if (arg_idx >= nargs)
9082 ++missing;
9083 }
9084 else
9085 arg = convert_template_argument (TREE_VALUE (parm),
9086 arg, new_args, complain,
9087 parm_idx, in_decl);
9088
9089 if (arg == error_mark_node)
9090 lost++;
9091
9092 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9093 }
9094
9095 if (missing || arg_idx < nargs - variadic_args_p)
9096 {
9097 /* If we had fixed parameter packs, we didn't know how many arguments we
9098 actually needed earlier; now we do. */
9099 nparms += fixed_pack_adjust;
9100 variadic_p -= fixed_packs;
9101 goto bad_nargs;
9102 }
9103
9104 if (arg_idx < nargs)
9105 {
9106 /* We had some pack expansion arguments that will only work if the packs
9107 are empty, but wait until instantiation time to complain.
9108 See variadic-ttp3.C. */
9109
9110 /* Except that we can't provide empty packs to alias templates or
9111 concepts when there are no corresponding parameters. Basically,
9112 we can get here with this:
9113
9114 template<typename T> concept C = true;
9115
9116 template<typename... Args>
9117 requires C<Args...>
9118 void f();
9119
9120 When parsing C<Args...>, we try to form a concept check of
9121 C<?, Args...>. Without the extra check for substituting an empty
9122 pack past the last parameter, we can accept the check as valid.
9123
9124 FIXME: This may be valid for alias templates (but I doubt it).
9125
9126 FIXME: The error could be better also. */
9127 if (in_decl && concept_definition_p (in_decl))
9128 {
9129 if (complain & tf_error)
9130 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9131 "too many arguments");
9132 return error_mark_node;
9133 }
9134
9135 int len = nparms + (nargs - arg_idx);
9136 tree args = make_tree_vec (len);
9137 int i = 0;
9138 for (; i < nparms; ++i)
9139 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9140 for (; i < len; ++i, ++arg_idx)
9141 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9142 arg_idx - pack_adjust);
9143 new_inner_args = args;
9144 }
9145
9146 if (lost)
9147 {
9148 gcc_assert (!(complain & tf_error) || seen_error ());
9149 return error_mark_node;
9150 }
9151
9152 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9153 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9154 TREE_VEC_LENGTH (new_inner_args));
9155
9156 return new_inner_args;
9157 }
9158
9159 /* Convert all template arguments to their appropriate types, and
9160 return a vector containing the innermost resulting template
9161 arguments. If any error occurs, return error_mark_node. Error and
9162 warning messages are not issued.
9163
9164 Note that no function argument deduction is performed, and default
9165 arguments are used to fill in unspecified arguments. */
9166 tree
9167 coerce_template_parms (tree parms, tree args, tree in_decl)
9168 {
9169 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9170 }
9171
9172 /* Convert all template arguments to their appropriate type, and
9173 instantiate default arguments as needed. This returns a vector
9174 containing the innermost resulting template arguments, or
9175 error_mark_node if unsuccessful. */
9176 tree
9177 coerce_template_parms (tree parms, tree args, tree in_decl,
9178 tsubst_flags_t complain)
9179 {
9180 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9181 }
9182
9183 /* Like coerce_template_parms. If PARMS represents all template
9184 parameters levels, this function returns a vector of vectors
9185 representing all the resulting argument levels. Note that in this
9186 case, only the innermost arguments are coerced because the
9187 outermost ones are supposed to have been coerced already.
9188
9189 Otherwise, if PARMS represents only (the innermost) vector of
9190 parameters, this function returns a vector containing just the
9191 innermost resulting arguments. */
9192
9193 static tree
9194 coerce_innermost_template_parms (tree parms,
9195 tree args,
9196 tree in_decl,
9197 tsubst_flags_t complain,
9198 bool require_all_args,
9199 bool use_default_args)
9200 {
9201 int parms_depth = TMPL_PARMS_DEPTH (parms);
9202 int args_depth = TMPL_ARGS_DEPTH (args);
9203 tree coerced_args;
9204
9205 if (parms_depth > 1)
9206 {
9207 coerced_args = make_tree_vec (parms_depth);
9208 tree level;
9209 int cur_depth;
9210
9211 for (level = parms, cur_depth = parms_depth;
9212 parms_depth > 0 && level != NULL_TREE;
9213 level = TREE_CHAIN (level), --cur_depth)
9214 {
9215 tree l;
9216 if (cur_depth == args_depth)
9217 l = coerce_template_parms (TREE_VALUE (level),
9218 args, in_decl, complain,
9219 require_all_args,
9220 use_default_args);
9221 else
9222 l = TMPL_ARGS_LEVEL (args, cur_depth);
9223
9224 if (l == error_mark_node)
9225 return error_mark_node;
9226
9227 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9228 }
9229 }
9230 else
9231 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9232 args, in_decl, complain,
9233 require_all_args,
9234 use_default_args);
9235 return coerced_args;
9236 }
9237
9238 /* Returns true if T is a wrapper to make a C++20 template parameter
9239 object const. */
9240
9241 static bool
9242 class_nttp_const_wrapper_p (tree t)
9243 {
9244 if (cxx_dialect < cxx20)
9245 return false;
9246 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9247 && CP_TYPE_CONST_P (TREE_TYPE (t))
9248 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9249 }
9250
9251 /* Returns 1 if template args OT and NT are equivalent. */
9252
9253 int
9254 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9255 {
9256 if (nt == ot)
9257 return 1;
9258 if (nt == NULL_TREE || ot == NULL_TREE)
9259 return false;
9260 if (nt == any_targ_node || ot == any_targ_node)
9261 return true;
9262
9263 if (class_nttp_const_wrapper_p (nt))
9264 nt = TREE_OPERAND (nt, 0);
9265 if (class_nttp_const_wrapper_p (ot))
9266 ot = TREE_OPERAND (ot, 0);
9267
9268 /* DR 1558: Don't treat an alias template specialization with dependent
9269 arguments as equivalent to its underlying type when used as a template
9270 argument; we need them to be distinct so that we substitute into the
9271 specialization arguments at instantiation time. And aliases can't be
9272 equivalent without being ==, so we don't need to look any deeper.
9273
9274 During partial ordering, however, we need to treat them normally so we can
9275 order uses of the same alias with different cv-qualification (79960). */
9276 auto cso = make_temp_override (comparing_dependent_aliases);
9277 if (!partial_order)
9278 ++comparing_dependent_aliases;
9279
9280 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9281 /* For member templates */
9282 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9283 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9284 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9285 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9286 PACK_EXPANSION_PATTERN (nt))
9287 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9288 PACK_EXPANSION_EXTRA_ARGS (nt)));
9289 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9290 return cp_tree_equal (ot, nt);
9291 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9292 gcc_unreachable ();
9293 else if (TYPE_P (nt) || TYPE_P (ot))
9294 {
9295 if (!(TYPE_P (nt) && TYPE_P (ot)))
9296 return false;
9297 return same_type_p (ot, nt);
9298 }
9299 else
9300 {
9301 /* Try to treat a template non-type argument that has been converted
9302 to the parameter type as equivalent to one that hasn't yet. */
9303 for (enum tree_code code1 = TREE_CODE (ot);
9304 CONVERT_EXPR_CODE_P (code1)
9305 || code1 == NON_LVALUE_EXPR;
9306 code1 = TREE_CODE (ot))
9307 ot = TREE_OPERAND (ot, 0);
9308
9309 for (enum tree_code code2 = TREE_CODE (nt);
9310 CONVERT_EXPR_CODE_P (code2)
9311 || code2 == NON_LVALUE_EXPR;
9312 code2 = TREE_CODE (nt))
9313 nt = TREE_OPERAND (nt, 0);
9314
9315 return cp_tree_equal (ot, nt);
9316 }
9317 }
9318
9319 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9320 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9321 NEWARG_PTR with the offending arguments if they are non-NULL. */
9322
9323 int
9324 comp_template_args (tree oldargs, tree newargs,
9325 tree *oldarg_ptr, tree *newarg_ptr,
9326 bool partial_order)
9327 {
9328 int i;
9329
9330 if (oldargs == newargs)
9331 return 1;
9332
9333 if (!oldargs || !newargs)
9334 return 0;
9335
9336 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9337 return 0;
9338
9339 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9340 {
9341 tree nt = TREE_VEC_ELT (newargs, i);
9342 tree ot = TREE_VEC_ELT (oldargs, i);
9343
9344 if (! template_args_equal (ot, nt, partial_order))
9345 {
9346 if (oldarg_ptr != NULL)
9347 *oldarg_ptr = ot;
9348 if (newarg_ptr != NULL)
9349 *newarg_ptr = nt;
9350 return 0;
9351 }
9352 }
9353 return 1;
9354 }
9355
9356 inline bool
9357 comp_template_args_porder (tree oargs, tree nargs)
9358 {
9359 return comp_template_args (oargs, nargs, NULL, NULL, true);
9360 }
9361
9362 /* Implement a freelist interface for objects of type T.
9363
9364 Head is a separate object, rather than a regular member, so that we
9365 can define it as a GTY deletable pointer, which is highly
9366 desirable. A data member could be declared that way, but then the
9367 containing object would implicitly get GTY((user)), which would
9368 prevent us from instantiating freelists as global objects.
9369 Although this way we can create freelist global objects, they're
9370 such thin wrappers that instantiating temporaries at every use
9371 loses nothing and saves permanent storage for the freelist object.
9372
9373 Member functions next, anew, poison and reinit have default
9374 implementations that work for most of the types we're interested
9375 in, but if they don't work for some type, they should be explicitly
9376 specialized. See the comments before them for requirements, and
9377 the example specializations for the tree_list_freelist. */
9378 template <typename T>
9379 class freelist
9380 {
9381 /* Return the next object in a chain. We could just do type
9382 punning, but if we access the object with its underlying type, we
9383 avoid strict-aliasing trouble. This needs only work between
9384 poison and reinit. */
9385 static T *&next (T *obj) { return obj->next; }
9386
9387 /* Return a newly allocated, uninitialized or minimally-initialized
9388 object of type T. Any initialization performed by anew should
9389 either remain across the life of the object and the execution of
9390 poison, or be redone by reinit. */
9391 static T *anew () { return ggc_alloc<T> (); }
9392
9393 /* Optionally scribble all over the bits holding the object, so that
9394 they become (mostly?) uninitialized memory. This is called while
9395 preparing to make the object part of the free list. */
9396 static void poison (T *obj) {
9397 T *p ATTRIBUTE_UNUSED = obj;
9398 T **q ATTRIBUTE_UNUSED = &next (obj);
9399
9400 #ifdef ENABLE_GC_CHECKING
9401 /* Poison the data, to indicate the data is garbage. */
9402 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9403 memset (p, 0xa5, sizeof (*p));
9404 #endif
9405 /* Let valgrind know the object is free. */
9406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9407
9408 /* Let valgrind know the next portion of the object is available,
9409 but uninitialized. */
9410 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9411 }
9412
9413 /* Bring an object that underwent at least one lifecycle after anew
9414 and before the most recent free and poison, back to a usable
9415 state, reinitializing whatever is needed for it to be
9416 functionally equivalent to an object just allocated and returned
9417 by anew. This may poison or clear the next field, used by
9418 freelist housekeeping after poison was called. */
9419 static void reinit (T *obj) {
9420 T **q ATTRIBUTE_UNUSED = &next (obj);
9421
9422 #ifdef ENABLE_GC_CHECKING
9423 memset (q, 0xa5, sizeof (*q));
9424 #endif
9425 /* Let valgrind know the entire object is available, but
9426 uninitialized. */
9427 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9428 }
9429
9430 /* Reference a GTY-deletable pointer that points to the first object
9431 in the free list proper. */
9432 T *&head;
9433 public:
9434 /* Construct a freelist object chaining objects off of HEAD. */
9435 freelist (T *&head) : head(head) {}
9436
9437 /* Add OBJ to the free object list. The former head becomes OBJ's
9438 successor. */
9439 void free (T *obj)
9440 {
9441 poison (obj);
9442 next (obj) = head;
9443 head = obj;
9444 }
9445
9446 /* Take an object from the free list, if one is available, or
9447 allocate a new one. Objects taken from the free list should be
9448 regarded as filled with garbage, except for bits that are
9449 configured to be preserved across free and alloc. */
9450 T *alloc ()
9451 {
9452 if (head)
9453 {
9454 T *obj = head;
9455 head = next (head);
9456 reinit (obj);
9457 return obj;
9458 }
9459 else
9460 return anew ();
9461 }
9462 };
9463
9464 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9465 want to allocate a TREE_LIST using the usual interface, and ensure
9466 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9467 build_tree_list logic in reinit, so this could go out of sync. */
9468 template <>
9469 inline tree &
9470 freelist<tree_node>::next (tree obj)
9471 {
9472 return TREE_CHAIN (obj);
9473 }
9474 template <>
9475 inline tree
9476 freelist<tree_node>::anew ()
9477 {
9478 return build_tree_list (NULL, NULL);
9479 }
9480 template <>
9481 inline void
9482 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9483 {
9484 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9485 tree p ATTRIBUTE_UNUSED = obj;
9486 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9487 tree *q ATTRIBUTE_UNUSED = &next (obj);
9488
9489 #ifdef ENABLE_GC_CHECKING
9490 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9491
9492 /* Poison the data, to indicate the data is garbage. */
9493 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9494 memset (p, 0xa5, size);
9495 #endif
9496 /* Let valgrind know the object is free. */
9497 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9498 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9499 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9500 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9501
9502 #ifdef ENABLE_GC_CHECKING
9503 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9504 /* Keep TREE_CHAIN functional. */
9505 TREE_SET_CODE (obj, TREE_LIST);
9506 #else
9507 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9508 #endif
9509 }
9510 template <>
9511 inline void
9512 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9513 {
9514 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9515
9516 #ifdef ENABLE_GC_CHECKING
9517 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9518 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9519 memset (obj, 0, sizeof (tree_list));
9520 #endif
9521
9522 /* Let valgrind know the entire object is available, but
9523 uninitialized. */
9524 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9525
9526 #ifdef ENABLE_GC_CHECKING
9527 TREE_SET_CODE (obj, TREE_LIST);
9528 #else
9529 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9530 #endif
9531 }
9532
9533 /* Point to the first object in the TREE_LIST freelist. */
9534 static GTY((deletable)) tree tree_list_freelist_head;
9535 /* Return the/an actual TREE_LIST freelist. */
9536 static inline freelist<tree_node>
9537 tree_list_freelist ()
9538 {
9539 return tree_list_freelist_head;
9540 }
9541
9542 /* Point to the first object in the tinst_level freelist. */
9543 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9544 /* Return the/an actual tinst_level freelist. */
9545 static inline freelist<tinst_level>
9546 tinst_level_freelist ()
9547 {
9548 return tinst_level_freelist_head;
9549 }
9550
9551 /* Point to the first object in the pending_template freelist. */
9552 static GTY((deletable)) pending_template *pending_template_freelist_head;
9553 /* Return the/an actual pending_template freelist. */
9554 static inline freelist<pending_template>
9555 pending_template_freelist ()
9556 {
9557 return pending_template_freelist_head;
9558 }
9559
9560 /* Build the TREE_LIST object out of a split list, store it
9561 permanently, and return it. */
9562 tree
9563 tinst_level::to_list ()
9564 {
9565 gcc_assert (split_list_p ());
9566 tree ret = tree_list_freelist ().alloc ();
9567 TREE_PURPOSE (ret) = tldcl;
9568 TREE_VALUE (ret) = targs;
9569 tldcl = ret;
9570 targs = NULL;
9571 gcc_assert (tree_list_p ());
9572 return ret;
9573 }
9574
9575 const unsigned short tinst_level::refcount_infinity;
9576
9577 /* Increment OBJ's refcount unless it is already infinite. */
9578 static tinst_level *
9579 inc_refcount_use (tinst_level *obj)
9580 {
9581 if (obj && obj->refcount != tinst_level::refcount_infinity)
9582 ++obj->refcount;
9583 return obj;
9584 }
9585
9586 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9587 void
9588 tinst_level::free (tinst_level *obj)
9589 {
9590 if (obj->tree_list_p ())
9591 tree_list_freelist ().free (obj->get_node ());
9592 tinst_level_freelist ().free (obj);
9593 }
9594
9595 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9596 OBJ's DECL and OBJ, and start over with the tinst_level object that
9597 used to be referenced by OBJ's NEXT. */
9598 static void
9599 dec_refcount_use (tinst_level *obj)
9600 {
9601 while (obj
9602 && obj->refcount != tinst_level::refcount_infinity
9603 && !--obj->refcount)
9604 {
9605 tinst_level *next = obj->next;
9606 tinst_level::free (obj);
9607 obj = next;
9608 }
9609 }
9610
9611 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9612 and of the former PTR. Omitting the second argument is equivalent
9613 to passing (T*)NULL; this is allowed because passing the
9614 zero-valued integral constant NULL confuses type deduction and/or
9615 overload resolution. */
9616 template <typename T>
9617 static void
9618 set_refcount_ptr (T *& ptr, T *obj = NULL)
9619 {
9620 T *save = ptr;
9621 ptr = inc_refcount_use (obj);
9622 dec_refcount_use (save);
9623 }
9624
9625 static void
9626 add_pending_template (tree d)
9627 {
9628 tree ti = (TYPE_P (d)
9629 ? CLASSTYPE_TEMPLATE_INFO (d)
9630 : DECL_TEMPLATE_INFO (d));
9631 struct pending_template *pt;
9632 int level;
9633
9634 if (TI_PENDING_TEMPLATE_FLAG (ti))
9635 return;
9636
9637 /* We are called both from instantiate_decl, where we've already had a
9638 tinst_level pushed, and instantiate_template, where we haven't.
9639 Compensate. */
9640 gcc_assert (TREE_CODE (d) != TREE_LIST);
9641 level = !current_tinst_level
9642 || current_tinst_level->maybe_get_node () != d;
9643
9644 if (level)
9645 push_tinst_level (d);
9646
9647 pt = pending_template_freelist ().alloc ();
9648 pt->next = NULL;
9649 pt->tinst = NULL;
9650 set_refcount_ptr (pt->tinst, current_tinst_level);
9651 if (last_pending_template)
9652 last_pending_template->next = pt;
9653 else
9654 pending_templates = pt;
9655
9656 last_pending_template = pt;
9657
9658 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9659
9660 if (level)
9661 pop_tinst_level ();
9662 }
9663
9664
9665 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9666 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9667 documentation for TEMPLATE_ID_EXPR. */
9668
9669 tree
9670 lookup_template_function (tree fns, tree arglist)
9671 {
9672 if (fns == error_mark_node || arglist == error_mark_node)
9673 return error_mark_node;
9674
9675 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9676
9677 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9678 {
9679 error ("%q#D is not a function template", fns);
9680 return error_mark_node;
9681 }
9682
9683 if (BASELINK_P (fns))
9684 {
9685 fns = copy_node (fns);
9686 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9687 unknown_type_node,
9688 BASELINK_FUNCTIONS (fns),
9689 arglist);
9690 return fns;
9691 }
9692
9693 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9694 }
9695
9696 /* Within the scope of a template class S<T>, the name S gets bound
9697 (in build_self_reference) to a TYPE_DECL for the class, not a
9698 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9699 or one of its enclosing classes, and that type is a template,
9700 return the associated TEMPLATE_DECL. Otherwise, the original
9701 DECL is returned.
9702
9703 Also handle the case when DECL is a TREE_LIST of ambiguous
9704 injected-class-names from different bases. */
9705
9706 tree
9707 maybe_get_template_decl_from_type_decl (tree decl)
9708 {
9709 if (decl == NULL_TREE)
9710 return decl;
9711
9712 /* DR 176: A lookup that finds an injected-class-name (10.2
9713 [class.member.lookup]) can result in an ambiguity in certain cases
9714 (for example, if it is found in more than one base class). If all of
9715 the injected-class-names that are found refer to specializations of
9716 the same class template, and if the name is followed by a
9717 template-argument-list, the reference refers to the class template
9718 itself and not a specialization thereof, and is not ambiguous. */
9719 if (TREE_CODE (decl) == TREE_LIST)
9720 {
9721 tree t, tmpl = NULL_TREE;
9722 for (t = decl; t; t = TREE_CHAIN (t))
9723 {
9724 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9725 if (!tmpl)
9726 tmpl = elt;
9727 else if (tmpl != elt)
9728 break;
9729 }
9730 if (tmpl && t == NULL_TREE)
9731 return tmpl;
9732 else
9733 return decl;
9734 }
9735
9736 return (decl != NULL_TREE
9737 && DECL_SELF_REFERENCE_P (decl)
9738 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9739 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9740 }
9741
9742 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9743 parameters, find the desired type.
9744
9745 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9746
9747 IN_DECL, if non-NULL, is the template declaration we are trying to
9748 instantiate.
9749
9750 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9751 the class we are looking up.
9752
9753 Issue error and warning messages under control of COMPLAIN.
9754
9755 If the template class is really a local class in a template
9756 function, then the FUNCTION_CONTEXT is the function in which it is
9757 being instantiated.
9758
9759 ??? Note that this function is currently called *twice* for each
9760 template-id: the first time from the parser, while creating the
9761 incomplete type (finish_template_type), and the second type during the
9762 real instantiation (instantiate_template_class). This is surely something
9763 that we want to avoid. It also causes some problems with argument
9764 coercion (see convert_nontype_argument for more information on this). */
9765
9766 static tree
9767 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9768 int entering_scope, tsubst_flags_t complain)
9769 {
9770 tree templ = NULL_TREE, parmlist;
9771 tree t;
9772 spec_entry **slot;
9773 spec_entry *entry;
9774 spec_entry elt;
9775 hashval_t hash;
9776
9777 if (identifier_p (d1))
9778 {
9779 tree value = innermost_non_namespace_value (d1);
9780 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9781 templ = value;
9782 else
9783 {
9784 if (context)
9785 push_decl_namespace (context);
9786 templ = lookup_name (d1);
9787 templ = maybe_get_template_decl_from_type_decl (templ);
9788 if (context)
9789 pop_decl_namespace ();
9790 }
9791 if (templ)
9792 context = DECL_CONTEXT (templ);
9793 }
9794 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9795 {
9796 tree type = TREE_TYPE (d1);
9797
9798 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9799 an implicit typename for the second A. Deal with it. */
9800 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9801 type = TREE_TYPE (type);
9802
9803 if (CLASSTYPE_TEMPLATE_INFO (type))
9804 {
9805 templ = CLASSTYPE_TI_TEMPLATE (type);
9806 d1 = DECL_NAME (templ);
9807 }
9808 }
9809 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9810 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9811 {
9812 templ = TYPE_TI_TEMPLATE (d1);
9813 d1 = DECL_NAME (templ);
9814 }
9815 else if (DECL_TYPE_TEMPLATE_P (d1))
9816 {
9817 templ = d1;
9818 d1 = DECL_NAME (templ);
9819 context = DECL_CONTEXT (templ);
9820 }
9821 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9822 {
9823 templ = d1;
9824 d1 = DECL_NAME (templ);
9825 }
9826
9827 /* Issue an error message if we didn't find a template. */
9828 if (! templ)
9829 {
9830 if (complain & tf_error)
9831 error ("%qT is not a template", d1);
9832 return error_mark_node;
9833 }
9834
9835 if (TREE_CODE (templ) != TEMPLATE_DECL
9836 /* Make sure it's a user visible template, if it was named by
9837 the user. */
9838 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9839 && !PRIMARY_TEMPLATE_P (templ)))
9840 {
9841 if (complain & tf_error)
9842 {
9843 error ("non-template type %qT used as a template", d1);
9844 if (in_decl)
9845 error ("for template declaration %q+D", in_decl);
9846 }
9847 return error_mark_node;
9848 }
9849
9850 complain &= ~tf_user;
9851
9852 /* An alias that just changes the name of a template is equivalent to the
9853 other template, so if any of the arguments are pack expansions, strip
9854 the alias to avoid problems with a pack expansion passed to a non-pack
9855 alias template parameter (DR 1430). */
9856 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9857 templ = get_underlying_template (templ);
9858
9859 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9860 {
9861 tree parm;
9862 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9863 if (arglist2 == error_mark_node
9864 || (!uses_template_parms (arglist2)
9865 && check_instantiated_args (templ, arglist2, complain)))
9866 return error_mark_node;
9867
9868 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9869 return parm;
9870 }
9871 else
9872 {
9873 tree template_type = TREE_TYPE (templ);
9874 tree gen_tmpl;
9875 tree type_decl;
9876 tree found = NULL_TREE;
9877 int arg_depth;
9878 int parm_depth;
9879 int is_dependent_type;
9880 int use_partial_inst_tmpl = false;
9881
9882 if (template_type == error_mark_node)
9883 /* An error occurred while building the template TEMPL, and a
9884 diagnostic has most certainly been emitted for that
9885 already. Let's propagate that error. */
9886 return error_mark_node;
9887
9888 gen_tmpl = most_general_template (templ);
9889 if (modules_p ())
9890 lazy_load_pendings (gen_tmpl);
9891
9892 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9893 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9894 arg_depth = TMPL_ARGS_DEPTH (arglist);
9895
9896 if (arg_depth == 1 && parm_depth > 1)
9897 {
9898 /* We've been given an incomplete set of template arguments.
9899 For example, given:
9900
9901 template <class T> struct S1 {
9902 template <class U> struct S2 {};
9903 template <class U> struct S2<U*> {};
9904 };
9905
9906 we will be called with an ARGLIST of `U*', but the
9907 TEMPLATE will be `template <class T> template
9908 <class U> struct S1<T>::S2'. We must fill in the missing
9909 arguments. */
9910 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9911 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9912 arg_depth = TMPL_ARGS_DEPTH (arglist);
9913 }
9914
9915 /* Now we should have enough arguments. */
9916 gcc_assert (parm_depth == arg_depth);
9917
9918 /* From here on, we're only interested in the most general
9919 template. */
9920
9921 /* Shortcut looking up the current class scope again. */
9922 if (current_class_type)
9923 if (tree ti = CLASSTYPE_TEMPLATE_INFO (current_class_type))
9924 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
9925 && comp_template_args (arglist, TI_ARGS (ti)))
9926 return current_class_type;
9927
9928 /* Calculate the BOUND_ARGS. These will be the args that are
9929 actually tsubst'd into the definition to create the
9930 instantiation. */
9931 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9932 complain,
9933 /*require_all_args=*/true,
9934 /*use_default_args=*/true);
9935
9936 if (arglist == error_mark_node)
9937 /* We were unable to bind the arguments. */
9938 return error_mark_node;
9939
9940 /* In the scope of a template class, explicit references to the
9941 template class refer to the type of the template, not any
9942 instantiation of it. For example, in:
9943
9944 template <class T> class C { void f(C<T>); }
9945
9946 the `C<T>' is just the same as `C'. Outside of the
9947 class, however, such a reference is an instantiation. */
9948 if (entering_scope
9949 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9950 || currently_open_class (template_type))
9951 {
9952 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9953
9954 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9955 return template_type;
9956 }
9957
9958 /* If we already have this specialization, return it. */
9959 elt.tmpl = gen_tmpl;
9960 elt.args = arglist;
9961 elt.spec = NULL_TREE;
9962 hash = spec_hasher::hash (&elt);
9963 entry = type_specializations->find_with_hash (&elt, hash);
9964
9965 if (entry)
9966 return entry->spec;
9967
9968 /* If the template's constraints are not satisfied,
9969 then we cannot form a valid type.
9970
9971 Note that the check is deferred until after the hash
9972 lookup. This prevents redundant checks on previously
9973 instantiated specializations. */
9974 if (flag_concepts
9975 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9976 && !constraints_satisfied_p (gen_tmpl, arglist))
9977 {
9978 if (complain & tf_error)
9979 {
9980 auto_diagnostic_group d;
9981 error ("template constraint failure for %qD", gen_tmpl);
9982 diagnose_constraints (input_location, gen_tmpl, arglist);
9983 }
9984 return error_mark_node;
9985 }
9986
9987 is_dependent_type = uses_template_parms (arglist);
9988
9989 /* If the deduced arguments are invalid, then the binding
9990 failed. */
9991 if (!is_dependent_type
9992 && check_instantiated_args (gen_tmpl,
9993 INNERMOST_TEMPLATE_ARGS (arglist),
9994 complain))
9995 return error_mark_node;
9996
9997 if (!is_dependent_type
9998 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9999 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
10000 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
10001 /* This occurs when the user has tried to define a tagged type
10002 in a scope that forbids it. We emitted an error during the
10003 parse. We didn't complete the bail out then, so here we
10004 are. */
10005 return error_mark_node;
10006
10007 context = DECL_CONTEXT (gen_tmpl);
10008 if (context && TYPE_P (context))
10009 {
10010 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
10011 context = complete_type (context);
10012 }
10013 else
10014 context = tsubst (context, arglist, complain, in_decl);
10015
10016 if (context == error_mark_node)
10017 return error_mark_node;
10018
10019 if (!context)
10020 context = global_namespace;
10021
10022 /* Create the type. */
10023 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10024 {
10025 /* The user referred to a specialization of an alias
10026 template represented by GEN_TMPL.
10027
10028 [temp.alias]/2 says:
10029
10030 When a template-id refers to the specialization of an
10031 alias template, it is equivalent to the associated
10032 type obtained by substitution of its
10033 template-arguments for the template-parameters in the
10034 type-id of the alias template. */
10035
10036 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
10037 /* Note that the call above (by indirectly calling
10038 register_specialization in tsubst_decl) registers the
10039 TYPE_DECL representing the specialization of the alias
10040 template. So next time someone substitutes ARGLIST for
10041 the template parms into the alias template (GEN_TMPL),
10042 she'll get that TYPE_DECL back. */
10043
10044 if (t == error_mark_node)
10045 return t;
10046 }
10047 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
10048 {
10049 if (!is_dependent_type)
10050 {
10051 set_current_access_from_decl (TYPE_NAME (template_type));
10052 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
10053 tsubst (ENUM_UNDERLYING_TYPE (template_type),
10054 arglist, complain, in_decl),
10055 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
10056 arglist, complain, in_decl),
10057 SCOPED_ENUM_P (template_type), NULL);
10058
10059 if (t == error_mark_node)
10060 return t;
10061 }
10062 else
10063 {
10064 /* We don't want to call start_enum for this type, since
10065 the values for the enumeration constants may involve
10066 template parameters. And, no one should be interested
10067 in the enumeration constants for such a type. */
10068 t = cxx_make_type (ENUMERAL_TYPE);
10069 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
10070 }
10071 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
10072 ENUM_FIXED_UNDERLYING_TYPE_P (t)
10073 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
10074 }
10075 else if (CLASS_TYPE_P (template_type))
10076 {
10077 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10078 instantiated here. */
10079 gcc_assert (!LAMBDA_TYPE_P (template_type));
10080
10081 t = make_class_type (TREE_CODE (template_type));
10082 CLASSTYPE_DECLARED_CLASS (t)
10083 = CLASSTYPE_DECLARED_CLASS (template_type);
10084 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10085
10086 /* A local class. Make sure the decl gets registered properly. */
10087 if (context == current_function_decl)
10088 if (pushtag (DECL_NAME (gen_tmpl), t)
10089 == error_mark_node)
10090 return error_mark_node;
10091
10092 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10093 /* This instantiation is another name for the primary
10094 template type. Set the TYPE_CANONICAL field
10095 appropriately. */
10096 TYPE_CANONICAL (t) = template_type;
10097 else if (any_template_arguments_need_structural_equality_p (arglist))
10098 /* Some of the template arguments require structural
10099 equality testing, so this template class requires
10100 structural equality testing. */
10101 SET_TYPE_STRUCTURAL_EQUALITY (t);
10102 }
10103 else
10104 gcc_unreachable ();
10105
10106 /* If we called start_enum or pushtag above, this information
10107 will already be set up. */
10108 type_decl = TYPE_NAME (t);
10109 if (!type_decl)
10110 {
10111 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10112
10113 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10114 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10115 DECL_SOURCE_LOCATION (type_decl)
10116 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10117 }
10118
10119 set_instantiating_module (type_decl);
10120 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10121 of export flag. We want to propagate this because it might
10122 be a friend declaration that pushes a new hidden binding. */
10123 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10124
10125 if (CLASS_TYPE_P (template_type))
10126 {
10127 TREE_PRIVATE (type_decl)
10128 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10129 TREE_PROTECTED (type_decl)
10130 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10131 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10132 {
10133 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10134 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10135 }
10136 }
10137
10138 if (OVERLOAD_TYPE_P (t)
10139 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10140 {
10141 static const char *tags[] = {"abi_tag", "may_alias"};
10142
10143 for (unsigned ix = 0; ix != 2; ix++)
10144 {
10145 tree attributes
10146 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10147
10148 if (attributes)
10149 TYPE_ATTRIBUTES (t)
10150 = tree_cons (TREE_PURPOSE (attributes),
10151 TREE_VALUE (attributes),
10152 TYPE_ATTRIBUTES (t));
10153 }
10154 }
10155
10156 /* Let's consider the explicit specialization of a member
10157 of a class template specialization that is implicitly instantiated,
10158 e.g.:
10159 template<class T>
10160 struct S
10161 {
10162 template<class U> struct M {}; //#0
10163 };
10164
10165 template<>
10166 template<>
10167 struct S<int>::M<char> //#1
10168 {
10169 int i;
10170 };
10171 [temp.expl.spec]/4 says this is valid.
10172
10173 In this case, when we write:
10174 S<int>::M<char> m;
10175
10176 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10177 the one of #0.
10178
10179 When we encounter #1, we want to store the partial instantiation
10180 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10181
10182 For all cases other than this "explicit specialization of member of a
10183 class template", we just want to store the most general template into
10184 the CLASSTYPE_TI_TEMPLATE of M.
10185
10186 This case of "explicit specialization of member of a class template"
10187 only happens when:
10188 1/ the enclosing class is an instantiation of, and therefore not
10189 the same as, the context of the most general template, and
10190 2/ we aren't looking at the partial instantiation itself, i.e.
10191 the innermost arguments are not the same as the innermost parms of
10192 the most general template.
10193
10194 So it's only when 1/ and 2/ happens that we want to use the partial
10195 instantiation of the member template in lieu of its most general
10196 template. */
10197
10198 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10199 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10200 /* the enclosing class must be an instantiation... */
10201 && CLASS_TYPE_P (context)
10202 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10203 {
10204 TREE_VEC_LENGTH (arglist)--;
10205 ++processing_template_decl;
10206 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10207 tree partial_inst_args =
10208 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10209 arglist, complain, NULL_TREE);
10210 --processing_template_decl;
10211 TREE_VEC_LENGTH (arglist)++;
10212 if (partial_inst_args == error_mark_node)
10213 return error_mark_node;
10214 use_partial_inst_tmpl =
10215 /*...and we must not be looking at the partial instantiation
10216 itself. */
10217 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10218 partial_inst_args);
10219 }
10220
10221 if (!use_partial_inst_tmpl)
10222 /* This case is easy; there are no member templates involved. */
10223 found = gen_tmpl;
10224 else
10225 {
10226 /* This is a full instantiation of a member template. Find
10227 the partial instantiation of which this is an instance. */
10228
10229 /* Temporarily reduce by one the number of levels in the ARGLIST
10230 so as to avoid comparing the last set of arguments. */
10231 TREE_VEC_LENGTH (arglist)--;
10232 /* We don't use COMPLAIN in the following call because this isn't
10233 the immediate context of deduction. For instance, tf_partial
10234 could be set here as we might be at the beginning of template
10235 argument deduction when any explicitly specified template
10236 arguments are substituted into the function type. tf_partial
10237 could lead into trouble because we wouldn't find the partial
10238 instantiation that might have been created outside tf_partial
10239 context, because the levels of template parameters wouldn't
10240 match, because in a tf_partial context, tsubst doesn't reduce
10241 TEMPLATE_PARM_LEVEL. */
10242 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10243 TREE_VEC_LENGTH (arglist)++;
10244 /* FOUND is either a proper class type, or an alias
10245 template specialization. In the later case, it's a
10246 TYPE_DECL, resulting from the substituting of arguments
10247 for parameters in the TYPE_DECL of the alias template
10248 done earlier. So be careful while getting the template
10249 of FOUND. */
10250 found = (TREE_CODE (found) == TEMPLATE_DECL
10251 ? found
10252 : (TREE_CODE (found) == TYPE_DECL
10253 ? DECL_TI_TEMPLATE (found)
10254 : CLASSTYPE_TI_TEMPLATE (found)));
10255
10256 if (DECL_CLASS_TEMPLATE_P (found)
10257 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10258 {
10259 /* If this partial instantiation is specialized, we want to
10260 use it for hash table lookup. */
10261 elt.tmpl = found;
10262 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10263 hash = spec_hasher::hash (&elt);
10264 }
10265 }
10266
10267 /* Build template info for the new specialization. */
10268 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10269
10270 elt.spec = t;
10271 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10272 gcc_checking_assert (*slot == NULL);
10273 entry = ggc_alloc<spec_entry> ();
10274 *entry = elt;
10275 *slot = entry;
10276
10277 /* Note this use of the partial instantiation so we can check it
10278 later in maybe_process_partial_specialization. */
10279 DECL_TEMPLATE_INSTANTIATIONS (found)
10280 = tree_cons (arglist, t,
10281 DECL_TEMPLATE_INSTANTIATIONS (found));
10282
10283 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10284 && !uses_template_parms (current_nonlambda_scope ())
10285 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10286 /* Now that the type has been registered on the instantiations
10287 list, we set up the enumerators. Because the enumeration
10288 constants may involve the enumeration type itself, we make
10289 sure to register the type first, and then create the
10290 constants. That way, doing tsubst_expr for the enumeration
10291 constants won't result in recursive calls here; we'll find
10292 the instantiation and exit above. */
10293 tsubst_enum (template_type, t, arglist);
10294
10295 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10296 /* If the type makes use of template parameters, the
10297 code that generates debugging information will crash. */
10298 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10299
10300 /* Possibly limit visibility based on template args. */
10301 TREE_PUBLIC (type_decl) = 1;
10302 determine_visibility (type_decl);
10303
10304 inherit_targ_abi_tags (t);
10305
10306 return t;
10307 }
10308 }
10309
10310 /* Wrapper for lookup_template_class_1. */
10311
10312 tree
10313 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10314 int entering_scope, tsubst_flags_t complain)
10315 {
10316 tree ret;
10317 timevar_push (TV_TEMPLATE_INST);
10318 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10319 entering_scope, complain);
10320 timevar_pop (TV_TEMPLATE_INST);
10321 return ret;
10322 }
10323
10324 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10325
10326 tree
10327 lookup_template_variable (tree templ, tree arglist)
10328 {
10329 if (flag_concepts && variable_concept_p (templ))
10330 return build_concept_check (templ, arglist, tf_none);
10331
10332 /* The type of the expression is NULL_TREE since the template-id could refer
10333 to an explicit or partial specialization. */
10334 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10335 }
10336
10337 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10338
10339 tree
10340 finish_template_variable (tree var, tsubst_flags_t complain)
10341 {
10342 tree templ = TREE_OPERAND (var, 0);
10343 tree arglist = TREE_OPERAND (var, 1);
10344
10345 tree parms = DECL_TEMPLATE_PARMS (templ);
10346 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10347 /*req_all*/true,
10348 /*use_default*/true);
10349 if (arglist == error_mark_node)
10350 return error_mark_node;
10351
10352 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10353 {
10354 if (complain & tf_error)
10355 {
10356 auto_diagnostic_group d;
10357 error ("use of invalid variable template %qE", var);
10358 diagnose_constraints (location_of (var), templ, arglist);
10359 }
10360 return error_mark_node;
10361 }
10362
10363 return instantiate_template (templ, arglist, complain);
10364 }
10365
10366 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10367 TARGS template args, and instantiate it if it's not dependent. */
10368
10369 tree
10370 lookup_and_finish_template_variable (tree templ, tree targs,
10371 tsubst_flags_t complain)
10372 {
10373 templ = lookup_template_variable (templ, targs);
10374 if (!any_dependent_template_arguments_p (targs))
10375 {
10376 templ = finish_template_variable (templ, complain);
10377 mark_used (templ);
10378 }
10379
10380 return convert_from_reference (templ);
10381 }
10382
10383 /* If the set of template parameters PARMS contains a template parameter
10384 at the given LEVEL and INDEX, then return this parameter. Otherwise
10385 return NULL_TREE. */
10386
10387 static tree
10388 corresponding_template_parameter (tree parms, int level, int index)
10389 {
10390 while (TMPL_PARMS_DEPTH (parms) > level)
10391 parms = TREE_CHAIN (parms);
10392
10393 if (TMPL_PARMS_DEPTH (parms) != level
10394 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10395 return NULL_TREE;
10396
10397 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10398 /* As in template_parm_to_arg. */
10399 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10400 t = TREE_TYPE (t);
10401 else
10402 t = DECL_INITIAL (t);
10403
10404 gcc_assert (TEMPLATE_PARM_P (t));
10405 return t;
10406 }
10407
10408 /* Return the template parameter from PARMS that positionally corresponds
10409 to the template parameter PARM, or else return NULL_TREE. */
10410
10411 static tree
10412 corresponding_template_parameter (tree parms, tree parm)
10413 {
10414 int level, index;
10415 template_parm_level_and_index (parm, &level, &index);
10416 return corresponding_template_parameter (parms, level, index);
10417 }
10418
10419 \f
10420 struct pair_fn_data
10421 {
10422 tree_fn_t fn;
10423 tree_fn_t any_fn;
10424 void *data;
10425 /* True when we should also visit template parameters that occur in
10426 non-deduced contexts. */
10427 bool include_nondeduced_p;
10428 hash_set<tree> *visited;
10429 };
10430
10431 /* Called from for_each_template_parm via walk_tree. */
10432
10433 static tree
10434 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10435 {
10436 tree t = *tp;
10437 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10438 tree_fn_t fn = pfd->fn;
10439 void *data = pfd->data;
10440 tree result = NULL_TREE;
10441
10442 #define WALK_SUBTREE(NODE) \
10443 do \
10444 { \
10445 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10446 pfd->include_nondeduced_p, \
10447 pfd->any_fn); \
10448 if (result) goto out; \
10449 } \
10450 while (0)
10451
10452 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10453 return t;
10454
10455 if (TYPE_P (t)
10456 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10457 WALK_SUBTREE (TYPE_CONTEXT (t));
10458
10459 switch (TREE_CODE (t))
10460 {
10461 case RECORD_TYPE:
10462 if (TYPE_PTRMEMFUNC_P (t))
10463 break;
10464 /* Fall through. */
10465
10466 case UNION_TYPE:
10467 case ENUMERAL_TYPE:
10468 if (!TYPE_TEMPLATE_INFO (t))
10469 *walk_subtrees = 0;
10470 else
10471 WALK_SUBTREE (TYPE_TI_ARGS (t));
10472 break;
10473
10474 case INTEGER_TYPE:
10475 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10476 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10477 break;
10478
10479 case METHOD_TYPE:
10480 /* Since we're not going to walk subtrees, we have to do this
10481 explicitly here. */
10482 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10483 /* Fall through. */
10484
10485 case FUNCTION_TYPE:
10486 /* Check the return type. */
10487 WALK_SUBTREE (TREE_TYPE (t));
10488
10489 /* Check the parameter types. Since default arguments are not
10490 instantiated until they are needed, the TYPE_ARG_TYPES may
10491 contain expressions that involve template parameters. But,
10492 no-one should be looking at them yet. And, once they're
10493 instantiated, they don't contain template parameters, so
10494 there's no point in looking at them then, either. */
10495 {
10496 tree parm;
10497
10498 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10499 WALK_SUBTREE (TREE_VALUE (parm));
10500
10501 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10502 want walk_tree walking into them itself. */
10503 *walk_subtrees = 0;
10504 }
10505
10506 if (flag_noexcept_type)
10507 {
10508 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10509 if (spec)
10510 WALK_SUBTREE (TREE_PURPOSE (spec));
10511 }
10512 break;
10513
10514 case TYPEOF_TYPE:
10515 case DECLTYPE_TYPE:
10516 case UNDERLYING_TYPE:
10517 if (pfd->include_nondeduced_p
10518 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10519 pfd->visited,
10520 pfd->include_nondeduced_p,
10521 pfd->any_fn))
10522 return error_mark_node;
10523 *walk_subtrees = false;
10524 break;
10525
10526 case FUNCTION_DECL:
10527 case VAR_DECL:
10528 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10529 WALK_SUBTREE (DECL_TI_ARGS (t));
10530 /* Fall through. */
10531
10532 case PARM_DECL:
10533 case CONST_DECL:
10534 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10535 WALK_SUBTREE (DECL_INITIAL (t));
10536 if (DECL_CONTEXT (t)
10537 && pfd->include_nondeduced_p)
10538 WALK_SUBTREE (DECL_CONTEXT (t));
10539 break;
10540
10541 case BOUND_TEMPLATE_TEMPLATE_PARM:
10542 /* Record template parameters such as `T' inside `TT<T>'. */
10543 WALK_SUBTREE (TYPE_TI_ARGS (t));
10544 /* Fall through. */
10545
10546 case TEMPLATE_TEMPLATE_PARM:
10547 case TEMPLATE_TYPE_PARM:
10548 case TEMPLATE_PARM_INDEX:
10549 if (fn && (*fn)(t, data))
10550 return t;
10551 else if (!fn)
10552 return t;
10553 break;
10554
10555 case TEMPLATE_DECL:
10556 /* A template template parameter is encountered. */
10557 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10558 WALK_SUBTREE (TREE_TYPE (t));
10559
10560 /* Already substituted template template parameter */
10561 *walk_subtrees = 0;
10562 break;
10563
10564 case TYPENAME_TYPE:
10565 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10566 partial instantiation. */
10567 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10568 *walk_subtrees = 0;
10569 break;
10570
10571 case INDIRECT_REF:
10572 case COMPONENT_REF:
10573 /* If there's no type, then this thing must be some expression
10574 involving template parameters. */
10575 if (!fn && !TREE_TYPE (t))
10576 return error_mark_node;
10577 break;
10578
10579 case CONSTRUCTOR:
10580 case TRAIT_EXPR:
10581 case PLUS_EXPR:
10582 case MULT_EXPR:
10583 case SCOPE_REF:
10584 /* These are non-deduced contexts. */
10585 if (!pfd->include_nondeduced_p)
10586 *walk_subtrees = 0;
10587 break;
10588
10589 case MODOP_EXPR:
10590 case CAST_EXPR:
10591 case IMPLICIT_CONV_EXPR:
10592 case REINTERPRET_CAST_EXPR:
10593 case CONST_CAST_EXPR:
10594 case STATIC_CAST_EXPR:
10595 case DYNAMIC_CAST_EXPR:
10596 case ARROW_EXPR:
10597 case DOTSTAR_EXPR:
10598 case TYPEID_EXPR:
10599 case PSEUDO_DTOR_EXPR:
10600 if (!fn)
10601 return error_mark_node;
10602 break;
10603
10604 case REQUIRES_EXPR:
10605 {
10606 if (!fn)
10607 return error_mark_node;
10608
10609 /* Recursively walk the type of each constraint variable. */
10610 tree p = TREE_OPERAND (t, 0);
10611 while (p)
10612 {
10613 WALK_SUBTREE (TREE_TYPE (p));
10614 p = TREE_CHAIN (p);
10615 }
10616 }
10617 break;
10618
10619 default:
10620 break;
10621 }
10622
10623 #undef WALK_SUBTREE
10624
10625 /* We didn't find any template parameters we liked. */
10626 out:
10627 return result;
10628 }
10629
10630 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10631 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10632 call FN with the parameter and the DATA.
10633 If FN returns nonzero, the iteration is terminated, and
10634 for_each_template_parm returns 1. Otherwise, the iteration
10635 continues. If FN never returns a nonzero value, the value
10636 returned by for_each_template_parm is 0. If FN is NULL, it is
10637 considered to be the function which always returns 1.
10638
10639 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10640 parameters that occur in non-deduced contexts. When false, only
10641 visits those template parameters that can be deduced. */
10642
10643 static tree
10644 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10645 hash_set<tree> *visited,
10646 bool include_nondeduced_p,
10647 tree_fn_t any_fn)
10648 {
10649 struct pair_fn_data pfd;
10650 tree result;
10651
10652 /* Set up. */
10653 pfd.fn = fn;
10654 pfd.any_fn = any_fn;
10655 pfd.data = data;
10656 pfd.include_nondeduced_p = include_nondeduced_p;
10657
10658 /* Walk the tree. (Conceptually, we would like to walk without
10659 duplicates, but for_each_template_parm_r recursively calls
10660 for_each_template_parm, so we would need to reorganize a fair
10661 bit to use walk_tree_without_duplicates, so we keep our own
10662 visited list.) */
10663 if (visited)
10664 pfd.visited = visited;
10665 else
10666 pfd.visited = new hash_set<tree>;
10667 result = cp_walk_tree (&t,
10668 for_each_template_parm_r,
10669 &pfd,
10670 pfd.visited);
10671
10672 /* Clean up. */
10673 if (!visited)
10674 {
10675 delete pfd.visited;
10676 pfd.visited = 0;
10677 }
10678
10679 return result;
10680 }
10681
10682 struct find_template_parameter_info
10683 {
10684 explicit find_template_parameter_info (tree ctx_parms)
10685 : parm_list (NULL_TREE),
10686 ctx_parms (ctx_parms),
10687 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10688 {}
10689
10690 hash_set<tree> visited;
10691 hash_set<tree> parms;
10692 tree parm_list;
10693 tree ctx_parms;
10694 int max_depth;
10695 };
10696
10697 /* Appends the declaration of T to the list in DATA. */
10698
10699 static int
10700 keep_template_parm (tree t, void* data)
10701 {
10702 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10703
10704 /* Template parameters declared within the expression are not part of
10705 the parameter mapping. For example, in this concept:
10706
10707 template<typename T>
10708 concept C = requires { <expr> } -> same_as<int>;
10709
10710 the return specifier same_as<int> declares a new decltype parameter
10711 that must not be part of the parameter mapping. The same is true
10712 for generic lambda parameters, lambda template parameters, etc. */
10713 int level;
10714 int index;
10715 template_parm_level_and_index (t, &level, &index);
10716 if (level == 0 || level > ftpi->max_depth)
10717 return 0;
10718
10719 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10720 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10721 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10722 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10723
10724 /* This template parameter might be an argument to a cached dependent
10725 specalization that was formed earlier inside some other template, in
10726 which case the parameter is not among the ones that are in-scope.
10727 Look in CTX_PARMS to find the corresponding in-scope template
10728 parameter, and use it instead. */
10729 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10730 t = in_scope;
10731
10732 /* Arguments like const T yield parameters like const T. This means that
10733 a template-id like X<T, const T> would yield two distinct parameters:
10734 T and const T. Adjust types to their unqualified versions. */
10735 if (TYPE_P (t))
10736 t = TYPE_MAIN_VARIANT (t);
10737 if (!ftpi->parms.add (t))
10738 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10739
10740 /* Verify the parameter we found has a valid index. */
10741 if (flag_checking)
10742 {
10743 tree parms = ftpi->ctx_parms;
10744 while (TMPL_PARMS_DEPTH (parms) > level)
10745 parms = TREE_CHAIN (parms);
10746 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10747 gcc_assert (index < len);
10748 }
10749
10750 return 0;
10751 }
10752
10753 /* Ensure that we recursively examine certain terms that are not normally
10754 visited in for_each_template_parm_r. */
10755
10756 static int
10757 any_template_parm_r (tree t, void *data)
10758 {
10759 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10760
10761 #define WALK_SUBTREE(NODE) \
10762 do \
10763 { \
10764 for_each_template_parm (NODE, keep_template_parm, data, \
10765 &ftpi->visited, true, \
10766 any_template_parm_r); \
10767 } \
10768 while (0)
10769
10770 /* A mention of a member alias/typedef is a use of all of its template
10771 arguments, including those from the enclosing class, so we don't use
10772 alias_template_specialization_p here. */
10773 if (TYPE_P (t) && typedef_variant_p (t))
10774 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10775 WALK_SUBTREE (TI_ARGS (tinfo));
10776
10777 switch (TREE_CODE (t))
10778 {
10779 case TEMPLATE_TYPE_PARM:
10780 /* Type constraints of a placeholder type may contain parameters. */
10781 if (is_auto (t))
10782 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10783 WALK_SUBTREE (constr);
10784 break;
10785
10786 case TEMPLATE_ID_EXPR:
10787 /* Search through references to variable templates. */
10788 WALK_SUBTREE (TREE_OPERAND (t, 0));
10789 WALK_SUBTREE (TREE_OPERAND (t, 1));
10790 break;
10791
10792 case TEMPLATE_PARM_INDEX:
10793 case PARM_DECL:
10794 /* A parameter or constraint variable may also depend on a template
10795 parameter without explicitly naming it. */
10796 WALK_SUBTREE (TREE_TYPE (t));
10797 break;
10798
10799 case TEMPLATE_DECL:
10800 /* If T is a member template that shares template parameters with
10801 ctx_parms, we need to mark all those parameters for mapping.
10802 To that end, it should suffice to just walk the DECL_CONTEXT of
10803 the template (assuming the template is not overly general). */
10804 WALK_SUBTREE (DECL_CONTEXT (t));
10805 break;
10806
10807 case LAMBDA_EXPR:
10808 {
10809 /* Look in the parms and body. */
10810 tree fn = lambda_function (t);
10811 WALK_SUBTREE (TREE_TYPE (fn));
10812 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10813 }
10814 break;
10815
10816 case IDENTIFIER_NODE:
10817 if (IDENTIFIER_CONV_OP_P (t))
10818 /* The conversion-type-id of a conversion operator may be dependent. */
10819 WALK_SUBTREE (TREE_TYPE (t));
10820 break;
10821
10822 case CONVERT_EXPR:
10823 if (is_dummy_object (t))
10824 WALK_SUBTREE (TREE_TYPE (t));
10825 break;
10826
10827 default:
10828 break;
10829 }
10830
10831 /* Keep walking. */
10832 return 0;
10833 }
10834
10835 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10836 are the template parameters in scope. */
10837
10838 tree
10839 find_template_parameters (tree t, tree ctx_parms)
10840 {
10841 if (!ctx_parms)
10842 return NULL_TREE;
10843
10844 find_template_parameter_info ftpi (ctx_parms);
10845 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10846 /*include_nondeduced*/true, any_template_parm_r);
10847 return ftpi.parm_list;
10848 }
10849
10850 /* Returns true if T depends on any template parameter. */
10851
10852 int
10853 uses_template_parms (tree t)
10854 {
10855 if (t == NULL_TREE)
10856 return false;
10857
10858 bool dependent_p;
10859 int saved_processing_template_decl;
10860
10861 saved_processing_template_decl = processing_template_decl;
10862 if (!saved_processing_template_decl)
10863 processing_template_decl = 1;
10864 if (TYPE_P (t))
10865 dependent_p = dependent_type_p (t);
10866 else if (TREE_CODE (t) == TREE_VEC)
10867 dependent_p = any_dependent_template_arguments_p (t);
10868 else if (TREE_CODE (t) == TREE_LIST)
10869 dependent_p = (uses_template_parms (TREE_VALUE (t))
10870 || uses_template_parms (TREE_CHAIN (t)));
10871 else if (TREE_CODE (t) == TYPE_DECL)
10872 dependent_p = dependent_type_p (TREE_TYPE (t));
10873 else if (t == error_mark_node)
10874 dependent_p = false;
10875 else
10876 dependent_p = instantiation_dependent_expression_p (t);
10877
10878 processing_template_decl = saved_processing_template_decl;
10879
10880 return dependent_p;
10881 }
10882
10883 /* Returns true iff we're processing an incompletely instantiated function
10884 template. Useful instead of processing_template_decl because the latter
10885 is set to 0 during instantiate_non_dependent_expr. */
10886
10887 bool
10888 in_template_function (void)
10889 {
10890 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10891 the latter might get set for e.g. access checking during satisfaction. */
10892 tree fn = cfun ? cfun->decl : NULL_TREE;
10893 bool ret;
10894 ++processing_template_decl;
10895 ret = (fn && DECL_LANG_SPECIFIC (fn)
10896 && DECL_TEMPLATE_INFO (fn)
10897 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10898 --processing_template_decl;
10899 return ret;
10900 }
10901
10902 /* Returns true if T depends on any template parameter with level LEVEL. */
10903
10904 bool
10905 uses_template_parms_level (tree t, int level)
10906 {
10907 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10908 /*include_nondeduced_p=*/true);
10909 }
10910
10911 /* Returns true if the signature of DECL depends on any template parameter from
10912 its enclosing class. */
10913
10914 bool
10915 uses_outer_template_parms (tree decl)
10916 {
10917 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10918 if (depth == 0)
10919 return false;
10920 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10921 &depth, NULL, /*include_nondeduced_p=*/true))
10922 return true;
10923 if (PRIMARY_TEMPLATE_P (decl)
10924 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
10925 {
10926 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
10927 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
10928 {
10929 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
10930 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
10931 if (TREE_CODE (parm) == PARM_DECL
10932 && for_each_template_parm (TREE_TYPE (parm),
10933 template_parm_outer_level,
10934 &depth, NULL, /*nondeduced*/true))
10935 return true;
10936 if (TREE_CODE (parm) == TEMPLATE_DECL
10937 && uses_outer_template_parms (parm))
10938 return true;
10939 if (defarg
10940 && for_each_template_parm (defarg, template_parm_outer_level,
10941 &depth, NULL, /*nondeduced*/true))
10942 return true;
10943 }
10944 }
10945 tree ci = get_constraints (decl);
10946 if (ci)
10947 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10948 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10949 &depth, NULL, /*nondeduced*/true))
10950 return true;
10951 return false;
10952 }
10953
10954 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10955 ill-formed translation unit, i.e. a variable or function that isn't
10956 usable in a constant expression. */
10957
10958 static inline bool
10959 neglectable_inst_p (tree d)
10960 {
10961 return (d && DECL_P (d)
10962 && !undeduced_auto_decl (d)
10963 && !(TREE_CODE (d) == FUNCTION_DECL
10964 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
10965 : decl_maybe_constant_var_p (d)));
10966 }
10967
10968 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10969 neglectable and instantiated from within an erroneous instantiation. */
10970
10971 static bool
10972 limit_bad_template_recursion (tree decl)
10973 {
10974 struct tinst_level *lev = current_tinst_level;
10975 int errs = errorcount + sorrycount;
10976 if (errs == 0 || !neglectable_inst_p (decl))
10977 return false;
10978
10979 /* Avoid instantiating members of an ill-formed class. */
10980 bool refuse
10981 = (DECL_CLASS_SCOPE_P (decl)
10982 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
10983
10984 if (!refuse)
10985 {
10986 for (; lev; lev = lev->next)
10987 if (neglectable_inst_p (lev->maybe_get_node ()))
10988 break;
10989 refuse = (lev && errs > lev->errors);
10990 }
10991
10992 if (refuse)
10993 {
10994 /* Don't warn about it not being defined. */
10995 suppress_warning (decl, OPT_Wunused);
10996 tree clone;
10997 FOR_EACH_CLONE (clone, decl)
10998 suppress_warning (clone, OPT_Wunused);
10999 }
11000 return refuse;
11001 }
11002
11003 static int tinst_depth;
11004 extern int max_tinst_depth;
11005 int depth_reached;
11006
11007 static GTY(()) struct tinst_level *last_error_tinst_level;
11008
11009 /* We're starting to instantiate D; record the template instantiation context
11010 at LOC for diagnostics and to restore it later. */
11011
11012 bool
11013 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
11014 {
11015 struct tinst_level *new_level;
11016
11017 if (tinst_depth >= max_tinst_depth)
11018 {
11019 /* Tell error.cc not to try to instantiate any templates. */
11020 at_eof = 2;
11021 fatal_error (input_location,
11022 "template instantiation depth exceeds maximum of %d"
11023 " (use %<-ftemplate-depth=%> to increase the maximum)",
11024 max_tinst_depth);
11025 return false;
11026 }
11027
11028 /* If the current instantiation caused problems, don't let it instantiate
11029 anything else. Do allow deduction substitution and decls usable in
11030 constant expressions. */
11031 if (!targs && limit_bad_template_recursion (tldcl))
11032 {
11033 /* Avoid no_linkage_errors and unused function (and all other)
11034 warnings for this decl. */
11035 suppress_warning (tldcl);
11036 return false;
11037 }
11038
11039 /* When not -quiet, dump template instantiations other than functions, since
11040 announce_function will take care of those. */
11041 if (!quiet_flag && !targs
11042 && TREE_CODE (tldcl) != TREE_LIST
11043 && TREE_CODE (tldcl) != FUNCTION_DECL)
11044 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
11045
11046 new_level = tinst_level_freelist ().alloc ();
11047 new_level->tldcl = tldcl;
11048 new_level->targs = targs;
11049 new_level->locus = loc;
11050 new_level->errors = errorcount + sorrycount;
11051 new_level->next = NULL;
11052 new_level->refcount = 0;
11053 new_level->path = new_level->visible = nullptr;
11054 set_refcount_ptr (new_level->next, current_tinst_level);
11055 set_refcount_ptr (current_tinst_level, new_level);
11056
11057 ++tinst_depth;
11058 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
11059 depth_reached = tinst_depth;
11060
11061 return true;
11062 }
11063
11064 /* We're starting substitution of TMPL<ARGS>; record the template
11065 substitution context for diagnostics and to restore it later. */
11066
11067 bool
11068 push_tinst_level (tree tmpl, tree args)
11069 {
11070 return push_tinst_level_loc (tmpl, args, input_location);
11071 }
11072
11073 /* We're starting to instantiate D; record INPUT_LOCATION and the
11074 template instantiation context for diagnostics and to restore it
11075 later. */
11076
11077 bool
11078 push_tinst_level (tree d)
11079 {
11080 return push_tinst_level_loc (d, input_location);
11081 }
11082
11083 /* Likewise, but record LOC as the program location. */
11084
11085 bool
11086 push_tinst_level_loc (tree d, location_t loc)
11087 {
11088 gcc_assert (TREE_CODE (d) != TREE_LIST);
11089 return push_tinst_level_loc (d, NULL, loc);
11090 }
11091
11092 /* We're done instantiating this template; return to the instantiation
11093 context. */
11094
11095 void
11096 pop_tinst_level (void)
11097 {
11098 /* Restore the filename and line number stashed away when we started
11099 this instantiation. */
11100 input_location = current_tinst_level->locus;
11101 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
11102 --tinst_depth;
11103 }
11104
11105 /* We're instantiating a deferred template; restore the template
11106 instantiation context in which the instantiation was requested, which
11107 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11108
11109 static tree
11110 reopen_tinst_level (struct tinst_level *level)
11111 {
11112 struct tinst_level *t;
11113
11114 tinst_depth = 0;
11115 for (t = level; t; t = t->next)
11116 ++tinst_depth;
11117
11118 set_refcount_ptr (current_tinst_level, level);
11119 pop_tinst_level ();
11120 if (current_tinst_level)
11121 current_tinst_level->errors = errorcount+sorrycount;
11122 return level->maybe_get_node ();
11123 }
11124
11125 /* Returns the TINST_LEVEL which gives the original instantiation
11126 context. */
11127
11128 struct tinst_level *
11129 outermost_tinst_level (void)
11130 {
11131 struct tinst_level *level = current_tinst_level;
11132 if (level)
11133 while (level->next)
11134 level = level->next;
11135 return level;
11136 }
11137
11138 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11139 vector of template arguments, as for tsubst.
11140
11141 Returns an appropriate tsubst'd friend declaration. */
11142
11143 static tree
11144 tsubst_friend_function (tree decl, tree args)
11145 {
11146 tree new_friend;
11147
11148 if (TREE_CODE (decl) == FUNCTION_DECL
11149 && DECL_TEMPLATE_INSTANTIATION (decl)
11150 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11151 /* This was a friend declared with an explicit template
11152 argument list, e.g.:
11153
11154 friend void f<>(T);
11155
11156 to indicate that f was a template instantiation, not a new
11157 function declaration. Now, we have to figure out what
11158 instantiation of what template. */
11159 {
11160 tree template_id, arglist, fns;
11161 tree new_args;
11162 tree tmpl;
11163 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11164
11165 /* Friend functions are looked up in the containing namespace scope.
11166 We must enter that scope, to avoid finding member functions of the
11167 current class with same name. */
11168 push_nested_namespace (ns);
11169 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11170 tf_warning_or_error, NULL_TREE,
11171 /*integral_constant_expression_p=*/false);
11172 pop_nested_namespace (ns);
11173 arglist = tsubst (DECL_TI_ARGS (decl), args,
11174 tf_warning_or_error, NULL_TREE);
11175 template_id = lookup_template_function (fns, arglist);
11176
11177 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11178 tmpl = determine_specialization (template_id, new_friend,
11179 &new_args,
11180 /*need_member_template=*/0,
11181 TREE_VEC_LENGTH (args),
11182 tsk_none);
11183 return instantiate_template (tmpl, new_args, tf_error);
11184 }
11185
11186 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11187 if (new_friend == error_mark_node)
11188 return error_mark_node;
11189
11190 /* The NEW_FRIEND will look like an instantiation, to the
11191 compiler, but is not an instantiation from the point of view of
11192 the language. For example, we might have had:
11193
11194 template <class T> struct S {
11195 template <class U> friend void f(T, U);
11196 };
11197
11198 Then, in S<int>, template <class U> void f(int, U) is not an
11199 instantiation of anything. */
11200
11201 DECL_USE_TEMPLATE (new_friend) = 0;
11202 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11203 {
11204 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11205 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11206 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11207 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11208
11209 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11210 match in decls_match. */
11211 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11212 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11213 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11214 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11215 }
11216
11217 /* The mangled name for the NEW_FRIEND is incorrect. The function
11218 is not a template instantiation and should not be mangled like
11219 one. Therefore, we forget the mangling here; we'll recompute it
11220 later if we need it. */
11221 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11222 {
11223 SET_DECL_RTL (new_friend, NULL);
11224 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11225 }
11226
11227 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11228 {
11229 tree old_decl;
11230 tree ns;
11231
11232 /* We must save some information from NEW_FRIEND before calling
11233 duplicate decls since that function will free NEW_FRIEND if
11234 possible. */
11235 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11236 tree new_friend_result_template_info = NULL_TREE;
11237 bool new_friend_is_defn =
11238 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11239 (template_for_substitution (new_friend)))
11240 != NULL_TREE);
11241 tree not_tmpl = new_friend;
11242
11243 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11244 {
11245 /* This declaration is a `primary' template. */
11246 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11247
11248 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11249 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11250 }
11251
11252 /* Inside pushdecl_namespace_level, we will push into the
11253 current namespace. However, the friend function should go
11254 into the namespace of the template. */
11255 ns = decl_namespace_context (new_friend);
11256 push_nested_namespace (ns);
11257 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11258 pop_nested_namespace (ns);
11259
11260 if (old_decl == error_mark_node)
11261 return error_mark_node;
11262
11263 if (old_decl != new_friend)
11264 {
11265 /* This new friend declaration matched an existing
11266 declaration. For example, given:
11267
11268 template <class T> void f(T);
11269 template <class U> class C {
11270 template <class T> friend void f(T) {}
11271 };
11272
11273 the friend declaration actually provides the definition
11274 of `f', once C has been instantiated for some type. So,
11275 old_decl will be the out-of-class template declaration,
11276 while new_friend is the in-class definition.
11277
11278 But, if `f' was called before this point, the
11279 instantiation of `f' will have DECL_TI_ARGS corresponding
11280 to `T' but not to `U', references to which might appear
11281 in the definition of `f'. Previously, the most general
11282 template for an instantiation of `f' was the out-of-class
11283 version; now it is the in-class version. Therefore, we
11284 run through all specialization of `f', adding to their
11285 DECL_TI_ARGS appropriately. In particular, they need a
11286 new set of outer arguments, corresponding to the
11287 arguments for this class instantiation.
11288
11289 The same situation can arise with something like this:
11290
11291 friend void f(int);
11292 template <class T> class C {
11293 friend void f(T) {}
11294 };
11295
11296 when `C<int>' is instantiated. Now, `f(int)' is defined
11297 in the class. */
11298
11299 if (!new_friend_is_defn)
11300 /* On the other hand, if the in-class declaration does
11301 *not* provide a definition, then we don't want to alter
11302 existing definitions. We can just leave everything
11303 alone. */
11304 ;
11305 else
11306 {
11307 tree new_template = TI_TEMPLATE (new_friend_template_info);
11308 tree new_args = TI_ARGS (new_friend_template_info);
11309
11310 /* Overwrite whatever template info was there before, if
11311 any, with the new template information pertaining to
11312 the declaration. */
11313 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11314
11315 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11316 {
11317 /* We should have called reregister_specialization in
11318 duplicate_decls. */
11319 gcc_assert (retrieve_specialization (new_template,
11320 new_args, 0)
11321 == old_decl);
11322
11323 /* Instantiate it if the global has already been used. */
11324 if (DECL_ODR_USED (old_decl))
11325 instantiate_decl (old_decl, /*defer_ok=*/true,
11326 /*expl_inst_class_mem_p=*/false);
11327 }
11328 else
11329 {
11330 tree t;
11331
11332 /* Indicate that the old function template is a partial
11333 instantiation. */
11334 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11335 = new_friend_result_template_info;
11336
11337 gcc_assert (new_template
11338 == most_general_template (new_template));
11339 gcc_assert (new_template != old_decl);
11340
11341 /* Reassign any specializations already in the hash table
11342 to the new more general template, and add the
11343 additional template args. */
11344 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11345 t != NULL_TREE;
11346 t = TREE_CHAIN (t))
11347 {
11348 tree spec = TREE_VALUE (t);
11349 spec_entry elt;
11350
11351 elt.tmpl = old_decl;
11352 elt.args = DECL_TI_ARGS (spec);
11353 elt.spec = NULL_TREE;
11354
11355 decl_specializations->remove_elt (&elt);
11356
11357 DECL_TI_ARGS (spec)
11358 = add_outermost_template_args (new_args,
11359 DECL_TI_ARGS (spec));
11360
11361 register_specialization
11362 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11363
11364 }
11365 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11366 }
11367 }
11368
11369 /* The information from NEW_FRIEND has been merged into OLD_DECL
11370 by duplicate_decls. */
11371 new_friend = old_decl;
11372 }
11373 }
11374 else
11375 {
11376 tree context = DECL_CONTEXT (new_friend);
11377 bool dependent_p;
11378
11379 /* In the code
11380 template <class T> class C {
11381 template <class U> friend void C1<U>::f (); // case 1
11382 friend void C2<T>::f (); // case 2
11383 };
11384 we only need to make sure CONTEXT is a complete type for
11385 case 2. To distinguish between the two cases, we note that
11386 CONTEXT of case 1 remains dependent type after tsubst while
11387 this isn't true for case 2. */
11388 ++processing_template_decl;
11389 dependent_p = dependent_type_p (context);
11390 --processing_template_decl;
11391
11392 if (!dependent_p
11393 && !complete_type_or_else (context, NULL_TREE))
11394 return error_mark_node;
11395
11396 if (COMPLETE_TYPE_P (context))
11397 {
11398 tree fn = new_friend;
11399 /* do_friend adds the TEMPLATE_DECL for any member friend
11400 template even if it isn't a member template, i.e.
11401 template <class T> friend A<T>::f();
11402 Look through it in that case. */
11403 if (TREE_CODE (fn) == TEMPLATE_DECL
11404 && !PRIMARY_TEMPLATE_P (fn))
11405 fn = DECL_TEMPLATE_RESULT (fn);
11406 /* Check to see that the declaration is really present, and,
11407 possibly obtain an improved declaration. */
11408 fn = check_classfn (context, fn, NULL_TREE);
11409
11410 if (fn)
11411 new_friend = fn;
11412 }
11413 }
11414
11415 return new_friend;
11416 }
11417
11418 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11419 template arguments, as for tsubst.
11420
11421 Returns an appropriate tsubst'd friend type or error_mark_node on
11422 failure. */
11423
11424 static tree
11425 tsubst_friend_class (tree friend_tmpl, tree args)
11426 {
11427 tree tmpl;
11428
11429 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11430 {
11431 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11432 return TREE_TYPE (tmpl);
11433 }
11434
11435 tree context = CP_DECL_CONTEXT (friend_tmpl);
11436 if (TREE_CODE (context) == NAMESPACE_DECL)
11437 push_nested_namespace (context);
11438 else
11439 {
11440 context = tsubst (context, args, tf_error, NULL_TREE);
11441 push_nested_class (context);
11442 }
11443
11444 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11445 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11446
11447 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11448 {
11449 /* The friend template has already been declared. Just
11450 check to see that the declarations match, and install any new
11451 default parameters. We must tsubst the default parameters,
11452 of course. We only need the innermost template parameters
11453 because that is all that redeclare_class_template will look
11454 at. */
11455 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11456 > TMPL_ARGS_DEPTH (args))
11457 {
11458 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11459 args, tf_warning_or_error);
11460 location_t saved_input_location = input_location;
11461 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11462 tree cons = get_constraints (tmpl);
11463 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11464 input_location = saved_input_location;
11465 }
11466 }
11467 else
11468 {
11469 /* The friend template has not already been declared. In this
11470 case, the instantiation of the template class will cause the
11471 injection of this template into the namespace scope. */
11472 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11473
11474 if (tmpl != error_mark_node)
11475 {
11476 /* The new TMPL is not an instantiation of anything, so we
11477 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11478 for the new type because that is supposed to be the
11479 corresponding template decl, i.e., TMPL. */
11480 DECL_USE_TEMPLATE (tmpl) = 0;
11481 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11482 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11483 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11484 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11485
11486 /* Substitute into and set the constraints on the new declaration. */
11487 if (tree ci = get_constraints (friend_tmpl))
11488 {
11489 ++processing_template_decl;
11490 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11491 DECL_FRIEND_CONTEXT (friend_tmpl));
11492 --processing_template_decl;
11493 set_constraints (tmpl, ci);
11494 }
11495
11496 /* Inject this template into the enclosing namspace scope. */
11497 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11498 }
11499 }
11500
11501 if (TREE_CODE (context) == NAMESPACE_DECL)
11502 pop_nested_namespace (context);
11503 else
11504 pop_nested_class ();
11505
11506 return TREE_TYPE (tmpl);
11507 }
11508
11509 /* Returns zero if TYPE cannot be completed later due to circularity.
11510 Otherwise returns one. */
11511
11512 static int
11513 can_complete_type_without_circularity (tree type)
11514 {
11515 if (type == NULL_TREE || type == error_mark_node)
11516 return 0;
11517 else if (COMPLETE_TYPE_P (type))
11518 return 1;
11519 else if (TREE_CODE (type) == ARRAY_TYPE)
11520 return can_complete_type_without_circularity (TREE_TYPE (type));
11521 else if (CLASS_TYPE_P (type)
11522 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11523 return 0;
11524 else
11525 return 1;
11526 }
11527
11528 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11529 tsubst_flags_t, tree);
11530
11531 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11532 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11533
11534 static tree
11535 tsubst_attribute (tree t, tree *decl_p, tree args,
11536 tsubst_flags_t complain, tree in_decl)
11537 {
11538 gcc_assert (ATTR_IS_DEPENDENT (t));
11539
11540 tree val = TREE_VALUE (t);
11541 if (val == NULL_TREE)
11542 /* Nothing to do. */;
11543 else if ((flag_openmp || flag_openmp_simd)
11544 && is_attribute_p ("omp declare simd",
11545 get_attribute_name (t)))
11546 {
11547 tree clauses = TREE_VALUE (val);
11548 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11549 complain, in_decl);
11550 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11551 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11552 tree parms = DECL_ARGUMENTS (*decl_p);
11553 clauses
11554 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11555 if (clauses)
11556 val = build_tree_list (NULL_TREE, clauses);
11557 else
11558 val = NULL_TREE;
11559 }
11560 else if (flag_openmp
11561 && is_attribute_p ("omp declare variant base",
11562 get_attribute_name (t)))
11563 {
11564 ++cp_unevaluated_operand;
11565 tree varid
11566 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11567 in_decl, /*integral_constant_expression_p=*/false);
11568 --cp_unevaluated_operand;
11569 tree chain = TREE_CHAIN (val);
11570 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11571 tree ctx = copy_list (TREE_VALUE (val));
11572 tree simd = get_identifier ("simd");
11573 tree score = get_identifier (" score");
11574 tree condition = get_identifier ("condition");
11575 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11576 {
11577 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11578 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11579 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11580 {
11581 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11582 {
11583 tree clauses = TREE_VALUE (t2);
11584 clauses = tsubst_omp_clauses (clauses,
11585 C_ORT_OMP_DECLARE_SIMD, args,
11586 complain, in_decl);
11587 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11588 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11589 TREE_VALUE (t2) = clauses;
11590 }
11591 else
11592 {
11593 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11594 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11595 if (TREE_VALUE (t3))
11596 {
11597 bool allow_string
11598 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11599 && TREE_PURPOSE (t3) != score);
11600 tree v = TREE_VALUE (t3);
11601 if (TREE_CODE (v) == STRING_CST && allow_string)
11602 continue;
11603 v = tsubst_expr (v, args, complain, in_decl, true);
11604 v = fold_non_dependent_expr (v);
11605 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11606 || (TREE_PURPOSE (t3) == score
11607 ? TREE_CODE (v) != INTEGER_CST
11608 : !tree_fits_shwi_p (v)))
11609 {
11610 location_t loc
11611 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11612 match_loc);
11613 if (TREE_PURPOSE (t3) == score)
11614 error_at (loc, "score argument must be "
11615 "constant integer expression");
11616 else if (allow_string)
11617 error_at (loc, "property must be constant "
11618 "integer expression or string "
11619 "literal");
11620 else
11621 error_at (loc, "property must be constant "
11622 "integer expression");
11623 return NULL_TREE;
11624 }
11625 else if (TREE_PURPOSE (t3) == score
11626 && tree_int_cst_sgn (v) < 0)
11627 {
11628 location_t loc
11629 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11630 match_loc);
11631 error_at (loc, "score argument must be "
11632 "non-negative");
11633 return NULL_TREE;
11634 }
11635 TREE_VALUE (t3) = v;
11636 }
11637 }
11638 }
11639 }
11640 val = tree_cons (varid, ctx, chain);
11641 }
11642 /* If the first attribute argument is an identifier, don't
11643 pass it through tsubst. Attributes like mode, format,
11644 cleanup and several target specific attributes expect it
11645 unmodified. */
11646 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11647 {
11648 tree chain
11649 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11650 /*integral_constant_expression_p=*/false);
11651 if (chain != TREE_CHAIN (val))
11652 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11653 }
11654 else if (PACK_EXPANSION_P (val))
11655 {
11656 /* An attribute pack expansion. */
11657 tree purp = TREE_PURPOSE (t);
11658 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11659 if (pack == error_mark_node)
11660 return error_mark_node;
11661 int len = TREE_VEC_LENGTH (pack);
11662 tree list = NULL_TREE;
11663 tree *q = &list;
11664 for (int i = 0; i < len; ++i)
11665 {
11666 tree elt = TREE_VEC_ELT (pack, i);
11667 *q = build_tree_list (purp, elt);
11668 q = &TREE_CHAIN (*q);
11669 }
11670 return list;
11671 }
11672 else
11673 val = tsubst_expr (val, args, complain, in_decl,
11674 /*integral_constant_expression_p=*/false);
11675
11676 if (val == error_mark_node)
11677 return error_mark_node;
11678 if (val != TREE_VALUE (t))
11679 return build_tree_list (TREE_PURPOSE (t), val);
11680 return t;
11681 }
11682
11683 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11684 unchanged or a new TREE_LIST chain. */
11685
11686 static tree
11687 tsubst_attributes (tree attributes, tree args,
11688 tsubst_flags_t complain, tree in_decl)
11689 {
11690 tree last_dep = NULL_TREE;
11691
11692 for (tree t = attributes; t; t = TREE_CHAIN (t))
11693 if (ATTR_IS_DEPENDENT (t))
11694 {
11695 last_dep = t;
11696 attributes = copy_list (attributes);
11697 break;
11698 }
11699
11700 if (last_dep)
11701 for (tree *p = &attributes; *p; )
11702 {
11703 tree t = *p;
11704 if (ATTR_IS_DEPENDENT (t))
11705 {
11706 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11707 if (subst != t)
11708 {
11709 *p = subst;
11710 while (*p)
11711 p = &TREE_CHAIN (*p);
11712 *p = TREE_CHAIN (t);
11713 continue;
11714 }
11715 }
11716 p = &TREE_CHAIN (*p);
11717 }
11718
11719 return attributes;
11720 }
11721
11722 /* Apply any attributes which had to be deferred until instantiation
11723 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11724 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11725 false on error. */
11726
11727 static bool
11728 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11729 tree args, tsubst_flags_t complain, tree in_decl)
11730 {
11731 tree t;
11732 tree *p;
11733
11734 if (attributes == NULL_TREE)
11735 return true;
11736
11737 if (DECL_P (*decl_p))
11738 {
11739 if (TREE_TYPE (*decl_p) == error_mark_node)
11740 return false;
11741 p = &DECL_ATTRIBUTES (*decl_p);
11742 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11743 to our attributes parameter. */
11744 gcc_assert (*p == attributes);
11745 }
11746 else
11747 {
11748 p = &TYPE_ATTRIBUTES (*decl_p);
11749 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11750 lookup_template_class_1, and should be preserved. */
11751 gcc_assert (*p != attributes);
11752 while (*p)
11753 p = &TREE_CHAIN (*p);
11754 }
11755
11756 /* save_template_attributes puts the dependent attributes at the beginning of
11757 the list; find the non-dependent ones. */
11758 for (t = attributes; t; t = TREE_CHAIN (t))
11759 if (!ATTR_IS_DEPENDENT (t))
11760 break;
11761 tree nondep = t;
11762
11763 /* Apply any non-dependent attributes. */
11764 *p = nondep;
11765
11766 if (nondep == attributes)
11767 return true;
11768
11769 /* And then any dependent ones. */
11770 tree late_attrs = NULL_TREE;
11771 tree *q = &late_attrs;
11772 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
11773 {
11774 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11775 if (*q == error_mark_node)
11776 return false;
11777 if (*q == t)
11778 {
11779 *q = copy_node (t);
11780 TREE_CHAIN (*q) = NULL_TREE;
11781 }
11782 while (*q)
11783 q = &TREE_CHAIN (*q);
11784 }
11785
11786 /* cplus_decl_attributes can add some attributes implicitly. For templates,
11787 those attributes should have been added already when those templates were
11788 parsed, and shouldn't be added based on from which context they are
11789 first time instantiated. */
11790 auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
11791 auto o2 = make_temp_override (optimization_current_node,
11792 optimization_default_node);
11793 auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
11794 auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
11795 NULL);
11796
11797 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11798
11799 return true;
11800 }
11801
11802 /* The template TMPL is being instantiated with the template arguments TARGS.
11803 Perform the access checks that we deferred when parsing the template. */
11804
11805 static void
11806 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11807 {
11808 unsigned i;
11809 deferred_access_check *chk;
11810
11811 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11812 return;
11813
11814 if (vec<deferred_access_check, va_gc> *access_checks
11815 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11816 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11817 {
11818 tree decl = chk->decl;
11819 tree diag_decl = chk->diag_decl;
11820 tree type_scope = TREE_TYPE (chk->binfo);
11821
11822 if (uses_template_parms (type_scope))
11823 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11824
11825 /* Make access check error messages point to the location
11826 of the use of the typedef. */
11827 iloc_sentinel ils (chk->loc);
11828 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11829 decl, diag_decl, tf_warning_or_error);
11830 }
11831 }
11832
11833 static tree
11834 instantiate_class_template_1 (tree type)
11835 {
11836 tree templ, args, pattern, t, member;
11837 tree typedecl;
11838 tree pbinfo;
11839 tree base_list;
11840 unsigned int saved_maximum_field_alignment;
11841 tree fn_context;
11842
11843 if (type == error_mark_node)
11844 return error_mark_node;
11845
11846 if (COMPLETE_OR_OPEN_TYPE_P (type)
11847 || uses_template_parms (type))
11848 return type;
11849
11850 /* Figure out which template is being instantiated. */
11851 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11852 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11853
11854 /* Mark the type as in the process of being defined. */
11855 TYPE_BEING_DEFINED (type) = 1;
11856
11857 /* We may be in the middle of deferred access check. Disable
11858 it now. */
11859 deferring_access_check_sentinel acs (dk_no_deferred);
11860
11861 /* Determine what specialization of the original template to
11862 instantiate. */
11863 t = most_specialized_partial_spec (type, tf_warning_or_error);
11864 if (t == error_mark_node)
11865 return error_mark_node;
11866 else if (t)
11867 {
11868 /* This TYPE is actually an instantiation of a partial
11869 specialization. We replace the innermost set of ARGS with
11870 the arguments appropriate for substitution. For example,
11871 given:
11872
11873 template <class T> struct S {};
11874 template <class T> struct S<T*> {};
11875
11876 and supposing that we are instantiating S<int*>, ARGS will
11877 presently be {int*} -- but we need {int}. */
11878 pattern = TREE_TYPE (t);
11879 args = TREE_PURPOSE (t);
11880 }
11881 else
11882 {
11883 pattern = TREE_TYPE (templ);
11884 args = CLASSTYPE_TI_ARGS (type);
11885 }
11886
11887 /* If the template we're instantiating is incomplete, then clearly
11888 there's nothing we can do. */
11889 if (!COMPLETE_TYPE_P (pattern))
11890 {
11891 /* We can try again later. */
11892 TYPE_BEING_DEFINED (type) = 0;
11893 return type;
11894 }
11895
11896 /* If we've recursively instantiated too many templates, stop. */
11897 if (! push_tinst_level (type))
11898 return type;
11899
11900 int saved_unevaluated_operand = cp_unevaluated_operand;
11901 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11902
11903 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11904 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11905 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11906 fn_context = error_mark_node;
11907 if (!fn_context)
11908 push_to_top_level ();
11909 else
11910 {
11911 cp_unevaluated_operand = 0;
11912 c_inhibit_evaluation_warnings = 0;
11913 }
11914 /* Use #pragma pack from the template context. */
11915 saved_maximum_field_alignment = maximum_field_alignment;
11916 maximum_field_alignment = TYPE_PRECISION (pattern);
11917
11918 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11919
11920 /* Set the input location to the most specialized template definition.
11921 This is needed if tsubsting causes an error. */
11922 typedecl = TYPE_MAIN_DECL (pattern);
11923 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11924 DECL_SOURCE_LOCATION (typedecl);
11925
11926 set_instantiating_module (TYPE_NAME (type));
11927
11928 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11929 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11930 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11931 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11932 if (ANON_AGGR_TYPE_P (pattern))
11933 SET_ANON_AGGR_TYPE_P (type);
11934 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11935 {
11936 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11937 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11938 /* Adjust visibility for template arguments. */
11939 determine_visibility (TYPE_MAIN_DECL (type));
11940 }
11941 if (CLASS_TYPE_P (type))
11942 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11943
11944 pbinfo = TYPE_BINFO (pattern);
11945
11946 /* We should never instantiate a nested class before its enclosing
11947 class; we need to look up the nested class by name before we can
11948 instantiate it, and that lookup should instantiate the enclosing
11949 class. */
11950 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11951 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11952
11953 base_list = NULL_TREE;
11954 /* Defer access checking while we substitute into the types named in
11955 the base-clause. */
11956 push_deferring_access_checks (dk_deferred);
11957 if (BINFO_N_BASE_BINFOS (pbinfo))
11958 {
11959 tree pbase_binfo;
11960 int i;
11961
11962 /* Substitute into each of the bases to determine the actual
11963 basetypes. */
11964 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11965 {
11966 tree base;
11967 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11968 tree expanded_bases = NULL_TREE;
11969 int idx, len = 1;
11970
11971 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11972 {
11973 expanded_bases =
11974 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11975 args, tf_error, NULL_TREE);
11976 if (expanded_bases == error_mark_node)
11977 continue;
11978
11979 len = TREE_VEC_LENGTH (expanded_bases);
11980 }
11981
11982 for (idx = 0; idx < len; idx++)
11983 {
11984 if (expanded_bases)
11985 /* Extract the already-expanded base class. */
11986 base = TREE_VEC_ELT (expanded_bases, idx);
11987 else
11988 /* Substitute to figure out the base class. */
11989 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11990 NULL_TREE);
11991
11992 if (base == error_mark_node)
11993 continue;
11994
11995 base_list = tree_cons (access, base, base_list);
11996 if (BINFO_VIRTUAL_P (pbase_binfo))
11997 TREE_TYPE (base_list) = integer_type_node;
11998 }
11999 }
12000
12001 /* The list is now in reverse order; correct that. */
12002 base_list = nreverse (base_list);
12003 }
12004 /* Now call xref_basetypes to set up all the base-class
12005 information. */
12006 xref_basetypes (type, base_list);
12007
12008 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
12009 (int) ATTR_FLAG_TYPE_IN_PLACE,
12010 args, tf_error, NULL_TREE);
12011 fixup_attribute_variants (type);
12012
12013 /* Now that our base classes are set up, enter the scope of the
12014 class, so that name lookups into base classes, etc. will work
12015 correctly. This is precisely analogous to what we do in
12016 begin_class_definition when defining an ordinary non-template
12017 class, except we also need to push the enclosing classes. */
12018 push_nested_class (type);
12019
12020 /* Now check accessibility of the types named in its base-clause,
12021 relative to the scope of the class. */
12022 pop_to_parent_deferring_access_checks ();
12023
12024 /* A vector to hold members marked with attribute used. */
12025 auto_vec<tree> used;
12026
12027 /* Now members are processed in the order of declaration. */
12028 for (member = CLASSTYPE_DECL_LIST (pattern);
12029 member; member = TREE_CHAIN (member))
12030 {
12031 tree t = TREE_VALUE (member);
12032
12033 if (TREE_PURPOSE (member))
12034 {
12035 if (TYPE_P (t))
12036 {
12037 if (LAMBDA_TYPE_P (t))
12038 /* A closure type for a lambda in an NSDMI or default argument.
12039 Ignore it; it will be regenerated when needed. */
12040 continue;
12041
12042 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
12043 && TYPE_LANG_SPECIFIC (t)
12044 && CLASSTYPE_IS_TEMPLATE (t));
12045
12046 /* If the member is a class template, then -- even after
12047 substitution -- there may be dependent types in the
12048 template argument list for the class. We increment
12049 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
12050 that function will assume that no types are dependent
12051 when outside of a template. */
12052 if (class_template_p)
12053 ++processing_template_decl;
12054 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
12055 if (class_template_p)
12056 --processing_template_decl;
12057 if (newtag == error_mark_node)
12058 continue;
12059
12060 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
12061 {
12062 tree name = TYPE_IDENTIFIER (t);
12063
12064 if (class_template_p)
12065 /* Unfortunately, lookup_template_class sets
12066 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12067 instantiation (i.e., for the type of a member
12068 template class nested within a template class.)
12069 This behavior is required for
12070 maybe_process_partial_specialization to work
12071 correctly, but is not accurate in this case;
12072 the TAG is not an instantiation of anything.
12073 (The corresponding TEMPLATE_DECL is an
12074 instantiation, but the TYPE is not.) */
12075 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
12076
12077 /* Now, install the tag. We don't use pushtag
12078 because that does too much work -- creating an
12079 implicit typedef, which we've already done. */
12080 set_identifier_type_value (name, TYPE_NAME (newtag));
12081 maybe_add_class_template_decl_list (type, newtag, false);
12082 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
12083 determine_visibility (TYPE_NAME (newtag));
12084 }
12085 }
12086 else if (DECL_DECLARES_FUNCTION_P (t))
12087 {
12088 tree r;
12089
12090 if (TREE_CODE (t) == TEMPLATE_DECL)
12091 ++processing_template_decl;
12092 r = tsubst (t, args, tf_error, NULL_TREE);
12093 if (TREE_CODE (t) == TEMPLATE_DECL)
12094 --processing_template_decl;
12095 set_current_access_from_decl (r);
12096 finish_member_declaration (r);
12097 /* Instantiate members marked with attribute used. */
12098 if (r != error_mark_node && DECL_PRESERVE_P (r))
12099 used.safe_push (r);
12100 if (TREE_CODE (r) == FUNCTION_DECL
12101 && DECL_OMP_DECLARE_REDUCTION_P (r))
12102 cp_check_omp_declare_reduction (r);
12103 }
12104 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12105 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12106 /* A closure type for a lambda in an NSDMI or default argument.
12107 Ignore it; it will be regenerated when needed. */;
12108 else
12109 {
12110 /* Build new TYPE_FIELDS. */
12111 if (TREE_CODE (t) == STATIC_ASSERT)
12112 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
12113 /*integral_constant_expression_p=*/true);
12114 else if (TREE_CODE (t) != CONST_DECL)
12115 {
12116 tree r;
12117 tree vec = NULL_TREE;
12118 int len = 1;
12119
12120 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12121 /* The file and line for this declaration, to
12122 assist in error message reporting. Since we
12123 called push_tinst_level above, we don't need to
12124 restore these. */
12125 input_location = DECL_SOURCE_LOCATION (t);
12126
12127 if (TREE_CODE (t) == TEMPLATE_DECL)
12128 ++processing_template_decl;
12129 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12130 if (TREE_CODE (t) == TEMPLATE_DECL)
12131 --processing_template_decl;
12132
12133 if (TREE_CODE (r) == TREE_VEC)
12134 {
12135 /* A capture pack became multiple fields. */
12136 vec = r;
12137 len = TREE_VEC_LENGTH (vec);
12138 }
12139
12140 for (int i = 0; i < len; ++i)
12141 {
12142 if (vec)
12143 r = TREE_VEC_ELT (vec, i);
12144 if (VAR_P (r))
12145 {
12146 /* In [temp.inst]:
12147
12148 [t]he initialization (and any associated
12149 side-effects) of a static data member does
12150 not occur unless the static data member is
12151 itself used in a way that requires the
12152 definition of the static data member to
12153 exist.
12154
12155 Therefore, we do not substitute into the
12156 initialized for the static data member here. */
12157 finish_static_data_member_decl
12158 (r,
12159 /*init=*/NULL_TREE,
12160 /*init_const_expr_p=*/false,
12161 /*asmspec_tree=*/NULL_TREE,
12162 /*flags=*/0);
12163 /* Instantiate members marked with attribute used. */
12164 if (r != error_mark_node && DECL_PRESERVE_P (r))
12165 used.safe_push (r);
12166 }
12167 else if (TREE_CODE (r) == FIELD_DECL)
12168 {
12169 /* Determine whether R has a valid type and can be
12170 completed later. If R is invalid, then its type
12171 is replaced by error_mark_node. */
12172 tree rtype = TREE_TYPE (r);
12173 if (can_complete_type_without_circularity (rtype))
12174 complete_type (rtype);
12175
12176 if (!complete_or_array_type_p (rtype))
12177 {
12178 /* If R's type couldn't be completed and
12179 it isn't a flexible array member (whose
12180 type is incomplete by definition) give
12181 an error. */
12182 cxx_incomplete_type_error (r, rtype);
12183 TREE_TYPE (r) = error_mark_node;
12184 }
12185 else if (TREE_CODE (rtype) == ARRAY_TYPE
12186 && TYPE_DOMAIN (rtype) == NULL_TREE
12187 && (TREE_CODE (type) == UNION_TYPE
12188 || TREE_CODE (type) == QUAL_UNION_TYPE))
12189 {
12190 error ("flexible array member %qD in union", r);
12191 TREE_TYPE (r) = error_mark_node;
12192 }
12193 else if (!verify_type_context (input_location,
12194 TCTX_FIELD, rtype))
12195 TREE_TYPE (r) = error_mark_node;
12196 }
12197
12198 /* If it is a TYPE_DECL for a class-scoped
12199 ENUMERAL_TYPE, such a thing will already have
12200 been added to the field list by tsubst_enum
12201 in finish_member_declaration case above. */
12202 if (!(TREE_CODE (r) == TYPE_DECL
12203 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12204 && DECL_ARTIFICIAL (r)))
12205 {
12206 set_current_access_from_decl (r);
12207 finish_member_declaration (r);
12208 }
12209 }
12210 }
12211 }
12212 }
12213 else
12214 {
12215 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12216 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12217 {
12218 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12219
12220 tree friend_type = t;
12221 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12222 {
12223 /* template <class T> friend class C; */
12224 friend_type = tsubst_friend_class (friend_type, args);
12225 }
12226 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12227 {
12228 /* template <class T> friend class C::D; */
12229 friend_type = tsubst (friend_type, args,
12230 tf_warning_or_error, NULL_TREE);
12231 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12232 friend_type = TREE_TYPE (friend_type);
12233 }
12234 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12235 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12236 {
12237 /* This could be either
12238
12239 friend class T::C;
12240
12241 when dependent_type_p is false or
12242
12243 template <class U> friend class T::C;
12244
12245 otherwise. */
12246 /* Bump processing_template_decl in case this is something like
12247 template <class T> friend struct A<T>::B. */
12248 ++processing_template_decl;
12249 friend_type = tsubst (friend_type, args,
12250 tf_warning_or_error, NULL_TREE);
12251 --processing_template_decl;
12252 }
12253 else if (uses_template_parms (friend_type))
12254 /* friend class C<T>; */
12255 friend_type = tsubst (friend_type, args,
12256 tf_warning_or_error, NULL_TREE);
12257
12258 /* Otherwise it's
12259
12260 friend class C;
12261
12262 where C is already declared or
12263
12264 friend class C<int>;
12265
12266 We don't have to do anything in these cases. */
12267
12268 if (friend_type != error_mark_node)
12269 make_friend_class (type, friend_type, /*complain=*/false);
12270 }
12271 else
12272 {
12273 /* Build new DECL_FRIENDLIST. */
12274 tree r;
12275
12276 /* The file and line for this declaration, to
12277 assist in error message reporting. Since we
12278 called push_tinst_level above, we don't need to
12279 restore these. */
12280 input_location = DECL_SOURCE_LOCATION (t);
12281
12282 if (TREE_CODE (t) == TEMPLATE_DECL)
12283 {
12284 ++processing_template_decl;
12285 push_deferring_access_checks (dk_no_check);
12286 }
12287
12288 r = tsubst_friend_function (t, args);
12289 add_friend (type, r, /*complain=*/false);
12290 if (TREE_CODE (t) == TEMPLATE_DECL)
12291 {
12292 pop_deferring_access_checks ();
12293 --processing_template_decl;
12294 }
12295 }
12296 }
12297 }
12298
12299 if (fn_context)
12300 {
12301 /* Restore these before substituting into the lambda capture
12302 initializers. */
12303 cp_unevaluated_operand = saved_unevaluated_operand;
12304 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12305 }
12306
12307 /* Set the file and line number information to whatever is given for
12308 the class itself. This puts error messages involving generated
12309 implicit functions at a predictable point, and the same point
12310 that would be used for non-template classes. */
12311 input_location = DECL_SOURCE_LOCATION (typedecl);
12312
12313 unreverse_member_declarations (type);
12314 finish_struct_1 (type);
12315 TYPE_BEING_DEFINED (type) = 0;
12316
12317 /* Remember if instantiating this class ran into errors, so we can avoid
12318 instantiating member functions in limit_bad_template_recursion. We set
12319 this flag even if the problem was in another instantiation triggered by
12320 this one, as that will likely also cause trouble for member functions. */
12321 if (errorcount + sorrycount > current_tinst_level->errors)
12322 CLASSTYPE_ERRONEOUS (type) = true;
12323
12324 /* We don't instantiate default arguments for member functions. 14.7.1:
12325
12326 The implicit instantiation of a class template specialization causes
12327 the implicit instantiation of the declarations, but not of the
12328 definitions or default arguments, of the class member functions,
12329 member classes, static data members and member templates.... */
12330
12331 perform_instantiation_time_access_checks (pattern, args);
12332 perform_deferred_access_checks (tf_warning_or_error);
12333
12334 /* Now that we've gone through all the members, instantiate those
12335 marked with attribute used. We must do this in the context of
12336 the class -- not the context we pushed from, as that might be
12337 inside a template and change the behaviour of mark_used. */
12338 for (tree x : used)
12339 mark_used (x);
12340
12341 pop_nested_class ();
12342 maximum_field_alignment = saved_maximum_field_alignment;
12343 if (!fn_context)
12344 pop_from_top_level ();
12345 pop_tinst_level ();
12346
12347 /* The vtable for a template class can be emitted in any translation
12348 unit in which the class is instantiated. When there is no key
12349 method, however, finish_struct_1 will already have added TYPE to
12350 the keyed_classes. */
12351 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12352 vec_safe_push (keyed_classes, type);
12353
12354 return type;
12355 }
12356
12357 /* Wrapper for instantiate_class_template_1. */
12358
12359 tree
12360 instantiate_class_template (tree type)
12361 {
12362 tree ret;
12363 timevar_push (TV_TEMPLATE_INST);
12364 ret = instantiate_class_template_1 (type);
12365 timevar_pop (TV_TEMPLATE_INST);
12366 return ret;
12367 }
12368
12369 tree
12370 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12371 {
12372 tree r;
12373
12374 if (!t)
12375 r = t;
12376 else if (TYPE_P (t))
12377 r = tsubst (t, args, complain, in_decl);
12378 else
12379 {
12380 if (!(complain & tf_warning))
12381 ++c_inhibit_evaluation_warnings;
12382 r = tsubst_expr (t, args, complain, in_decl,
12383 /*integral_constant_expression_p=*/true);
12384 if (!(complain & tf_warning))
12385 --c_inhibit_evaluation_warnings;
12386 }
12387
12388 return r;
12389 }
12390
12391 /* Given a function parameter pack TMPL_PARM and some function parameters
12392 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12393 and set *SPEC_P to point at the next point in the list. */
12394
12395 tree
12396 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12397 {
12398 /* Collect all of the extra "packed" parameters into an
12399 argument pack. */
12400 tree argpack;
12401 tree spec_parm = *spec_p;
12402 int len;
12403
12404 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12405 if (tmpl_parm
12406 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12407 break;
12408
12409 spec_parm = *spec_p;
12410 if (len == 1 && DECL_PACK_P (spec_parm))
12411 {
12412 /* The instantiation is still a parameter pack; don't wrap it in a
12413 NONTYPE_ARGUMENT_PACK. */
12414 argpack = spec_parm;
12415 spec_parm = DECL_CHAIN (spec_parm);
12416 }
12417 else
12418 {
12419 /* Fill in PARMVEC with all of the parameters. */
12420 tree parmvec = make_tree_vec (len);
12421 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12422 for (int i = 0; i < len; i++)
12423 {
12424 tree elt = spec_parm;
12425 if (DECL_PACK_P (elt))
12426 elt = make_pack_expansion (elt);
12427 TREE_VEC_ELT (parmvec, i) = elt;
12428 spec_parm = DECL_CHAIN (spec_parm);
12429 }
12430
12431 /* Build the argument packs. */
12432 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12433 }
12434 *spec_p = spec_parm;
12435
12436 return argpack;
12437 }
12438
12439 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12440 NONTYPE_ARGUMENT_PACK. */
12441
12442 static tree
12443 make_fnparm_pack (tree spec_parm)
12444 {
12445 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12446 }
12447
12448 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12449 pack expansion with no extra args, 2 if it has extra args, or 0
12450 if it is not a pack expansion. */
12451
12452 static int
12453 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12454 {
12455 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12456 /* We're being called before this happens in tsubst_pack_expansion. */
12457 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12458 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12459 if (i >= TREE_VEC_LENGTH (vec))
12460 return 0;
12461 tree elt = TREE_VEC_ELT (vec, i);
12462 if (DECL_P (elt))
12463 /* A decl pack is itself an expansion. */
12464 elt = TREE_TYPE (elt);
12465 if (!PACK_EXPANSION_P (elt))
12466 return 0;
12467 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12468 return 2;
12469 return 1;
12470 }
12471
12472
12473 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12474
12475 static tree
12476 make_argument_pack_select (tree arg_pack, unsigned index)
12477 {
12478 tree aps = make_node (ARGUMENT_PACK_SELECT);
12479
12480 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12481 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12482
12483 return aps;
12484 }
12485
12486 /* This is a subroutine of tsubst_pack_expansion.
12487
12488 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12489 mechanism to store the (non complete list of) arguments of the
12490 substitution and return a non substituted pack expansion, in order
12491 to wait for when we have enough arguments to really perform the
12492 substitution. */
12493
12494 static bool
12495 use_pack_expansion_extra_args_p (tree t,
12496 tree parm_packs,
12497 int arg_pack_len,
12498 bool has_empty_arg)
12499 {
12500 if (has_empty_arg
12501 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
12502 return true;
12503
12504 /* If one pack has an expansion and another pack has a normal
12505 argument or if one pack has an empty argument and an another
12506 one hasn't then tsubst_pack_expansion cannot perform the
12507 substitution and need to fall back on the
12508 PACK_EXPANSION_EXTRA mechanism. */
12509 if (parm_packs == NULL_TREE)
12510 return false;
12511 else if (has_empty_arg)
12512 {
12513 /* If all the actual packs are pack expansions, we can still
12514 subsitute directly. */
12515 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12516 {
12517 tree a = TREE_VALUE (p);
12518 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12519 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12520 a = ARGUMENT_PACK_ARGS (a);
12521 if (TREE_VEC_LENGTH (a) == 1)
12522 a = TREE_VEC_ELT (a, 0);
12523 if (PACK_EXPANSION_P (a))
12524 continue;
12525 return true;
12526 }
12527 return false;
12528 }
12529
12530 for (int i = 0 ; i < arg_pack_len; ++i)
12531 {
12532 bool has_expansion_arg = false;
12533 bool has_non_expansion_arg = false;
12534 for (tree parm_pack = parm_packs;
12535 parm_pack;
12536 parm_pack = TREE_CHAIN (parm_pack))
12537 {
12538 tree arg = TREE_VALUE (parm_pack);
12539
12540 int exp = argument_pack_element_is_expansion_p (arg, i);
12541 if (exp == 2)
12542 /* We can't substitute a pack expansion with extra args into
12543 our pattern. */
12544 return true;
12545 else if (exp)
12546 has_expansion_arg = true;
12547 else
12548 has_non_expansion_arg = true;
12549 }
12550
12551 if (has_expansion_arg && has_non_expansion_arg)
12552 {
12553 gcc_checking_assert (false);
12554 return true;
12555 }
12556 }
12557 return false;
12558 }
12559
12560 /* [temp.variadic]/6 says that:
12561
12562 The instantiation of a pack expansion [...]
12563 produces a list E1,E2, ..., En, where N is the number of elements
12564 in the pack expansion parameters.
12565
12566 This subroutine of tsubst_pack_expansion produces one of these Ei.
12567
12568 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12569 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12570 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12571 INDEX is the index 'i' of the element Ei to produce. ARGS,
12572 COMPLAIN, and IN_DECL are the same parameters as for the
12573 tsubst_pack_expansion function.
12574
12575 The function returns the resulting Ei upon successful completion,
12576 or error_mark_node.
12577
12578 Note that this function possibly modifies the ARGS parameter, so
12579 it's the responsibility of the caller to restore it. */
12580
12581 static tree
12582 gen_elem_of_pack_expansion_instantiation (tree pattern,
12583 tree parm_packs,
12584 unsigned index,
12585 tree args /* This parm gets
12586 modified. */,
12587 tsubst_flags_t complain,
12588 tree in_decl)
12589 {
12590 tree t;
12591 bool ith_elem_is_expansion = false;
12592
12593 /* For each parameter pack, change the substitution of the parameter
12594 pack to the ith argument in its argument pack, then expand the
12595 pattern. */
12596 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12597 {
12598 tree parm = TREE_PURPOSE (pack);
12599 tree arg_pack = TREE_VALUE (pack);
12600 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12601
12602 ith_elem_is_expansion |=
12603 argument_pack_element_is_expansion_p (arg_pack, index);
12604
12605 /* Select the Ith argument from the pack. */
12606 if (TREE_CODE (parm) == PARM_DECL
12607 || VAR_P (parm)
12608 || TREE_CODE (parm) == FIELD_DECL)
12609 {
12610 if (index == 0)
12611 {
12612 aps = make_argument_pack_select (arg_pack, index);
12613 if (!mark_used (parm, complain) && !(complain & tf_error))
12614 return error_mark_node;
12615 register_local_specialization (aps, parm);
12616 }
12617 else
12618 aps = retrieve_local_specialization (parm);
12619 }
12620 else
12621 {
12622 int idx, level;
12623 template_parm_level_and_index (parm, &level, &idx);
12624
12625 if (index == 0)
12626 {
12627 aps = make_argument_pack_select (arg_pack, index);
12628 /* Update the corresponding argument. */
12629 TMPL_ARG (args, level, idx) = aps;
12630 }
12631 else
12632 /* Re-use the ARGUMENT_PACK_SELECT. */
12633 aps = TMPL_ARG (args, level, idx);
12634 }
12635 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12636 }
12637
12638 /* Substitute into the PATTERN with the (possibly altered)
12639 arguments. */
12640 if (pattern == in_decl)
12641 /* Expanding a fixed parameter pack from
12642 coerce_template_parameter_pack. */
12643 t = tsubst_decl (pattern, args, complain);
12644 else if (pattern == error_mark_node)
12645 t = error_mark_node;
12646 else if (!TYPE_P (pattern))
12647 t = tsubst_expr (pattern, args, complain, in_decl,
12648 /*integral_constant_expression_p=*/false);
12649 else
12650 t = tsubst (pattern, args, complain, in_decl);
12651
12652 /* If the Ith argument pack element is a pack expansion, then
12653 the Ith element resulting from the substituting is going to
12654 be a pack expansion as well. */
12655 if (ith_elem_is_expansion)
12656 t = make_pack_expansion (t, complain);
12657
12658 return t;
12659 }
12660
12661 /* When the unexpanded parameter pack in a fold expression expands to an empty
12662 sequence, the value of the expression is as follows; the program is
12663 ill-formed if the operator is not listed in this table.
12664
12665 && true
12666 || false
12667 , void() */
12668
12669 tree
12670 expand_empty_fold (tree t, tsubst_flags_t complain)
12671 {
12672 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12673 if (!FOLD_EXPR_MODIFY_P (t))
12674 switch (code)
12675 {
12676 case TRUTH_ANDIF_EXPR:
12677 return boolean_true_node;
12678 case TRUTH_ORIF_EXPR:
12679 return boolean_false_node;
12680 case COMPOUND_EXPR:
12681 return void_node;
12682 default:
12683 break;
12684 }
12685
12686 if (complain & tf_error)
12687 error_at (location_of (t),
12688 "fold of empty expansion over %O", code);
12689 return error_mark_node;
12690 }
12691
12692 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12693 form an expression that combines the two terms using the
12694 operator of T. */
12695
12696 static tree
12697 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12698 {
12699 tree_code code = FOLD_EXPR_OP (t);
12700
12701 tree lookups = templated_operator_saved_lookups (t);
12702
12703 // Handle compound assignment operators.
12704 if (FOLD_EXPR_MODIFY_P (t))
12705 return build_x_modify_expr (input_location, left, code, right,
12706 lookups, complain);
12707
12708 warning_sentinel s(warn_parentheses);
12709 switch (code)
12710 {
12711 case COMPOUND_EXPR:
12712 return build_x_compound_expr (input_location, left, right,
12713 lookups, complain);
12714 default:
12715 return build_x_binary_op (input_location, code,
12716 left, TREE_CODE (left),
12717 right, TREE_CODE (right),
12718 lookups, /*overload=*/NULL,
12719 complain);
12720 }
12721 }
12722
12723 /* Substitute ARGS into the pack of a fold expression T. */
12724
12725 static inline tree
12726 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12727 {
12728 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12729 }
12730
12731 /* Substitute ARGS into the pack of a fold expression T. */
12732
12733 static inline tree
12734 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12735 {
12736 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12737 }
12738
12739 /* Expand a PACK of arguments into a grouped as left fold.
12740 Given a pack containing elements A0, A1, ..., An and an
12741 operator @, this builds the expression:
12742
12743 ((A0 @ A1) @ A2) ... @ An
12744
12745 Note that PACK must not be empty.
12746
12747 The operator is defined by the original fold expression T. */
12748
12749 static tree
12750 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12751 {
12752 tree left = TREE_VEC_ELT (pack, 0);
12753 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12754 {
12755 tree right = TREE_VEC_ELT (pack, i);
12756 left = fold_expression (t, left, right, complain);
12757 }
12758 return left;
12759 }
12760
12761 /* Substitute into a unary left fold expression. */
12762
12763 static tree
12764 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12765 tree in_decl)
12766 {
12767 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12768 if (pack == error_mark_node)
12769 return error_mark_node;
12770 if (PACK_EXPANSION_P (pack))
12771 {
12772 tree r = copy_node (t);
12773 FOLD_EXPR_PACK (r) = pack;
12774 return r;
12775 }
12776 if (TREE_VEC_LENGTH (pack) == 0)
12777 return expand_empty_fold (t, complain);
12778 else
12779 return expand_left_fold (t, pack, complain);
12780 }
12781
12782 /* Substitute into a binary left fold expression.
12783
12784 Do ths by building a single (non-empty) vector of argumnts and
12785 building the expression from those elements. */
12786
12787 static tree
12788 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12789 tree in_decl)
12790 {
12791 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12792 if (pack == error_mark_node)
12793 return error_mark_node;
12794 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12795 if (init == error_mark_node)
12796 return error_mark_node;
12797
12798 if (PACK_EXPANSION_P (pack))
12799 {
12800 tree r = copy_node (t);
12801 FOLD_EXPR_PACK (r) = pack;
12802 FOLD_EXPR_INIT (r) = init;
12803 return r;
12804 }
12805
12806 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12807 TREE_VEC_ELT (vec, 0) = init;
12808 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12809 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12810
12811 return expand_left_fold (t, vec, complain);
12812 }
12813
12814 /* Expand a PACK of arguments into a grouped as right fold.
12815 Given a pack containing elementns A0, A1, ..., and an
12816 operator @, this builds the expression:
12817
12818 A0@ ... (An-2 @ (An-1 @ An))
12819
12820 Note that PACK must not be empty.
12821
12822 The operator is defined by the original fold expression T. */
12823
12824 tree
12825 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12826 {
12827 // Build the expression.
12828 int n = TREE_VEC_LENGTH (pack);
12829 tree right = TREE_VEC_ELT (pack, n - 1);
12830 for (--n; n != 0; --n)
12831 {
12832 tree left = TREE_VEC_ELT (pack, n - 1);
12833 right = fold_expression (t, left, right, complain);
12834 }
12835 return right;
12836 }
12837
12838 /* Substitute into a unary right fold expression. */
12839
12840 static tree
12841 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12842 tree in_decl)
12843 {
12844 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12845 if (pack == error_mark_node)
12846 return error_mark_node;
12847 if (PACK_EXPANSION_P (pack))
12848 {
12849 tree r = copy_node (t);
12850 FOLD_EXPR_PACK (r) = pack;
12851 return r;
12852 }
12853 if (TREE_VEC_LENGTH (pack) == 0)
12854 return expand_empty_fold (t, complain);
12855 else
12856 return expand_right_fold (t, pack, complain);
12857 }
12858
12859 /* Substitute into a binary right fold expression.
12860
12861 Do ths by building a single (non-empty) vector of arguments and
12862 building the expression from those elements. */
12863
12864 static tree
12865 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12866 tree in_decl)
12867 {
12868 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12869 if (pack == error_mark_node)
12870 return error_mark_node;
12871 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12872 if (init == error_mark_node)
12873 return error_mark_node;
12874
12875 if (PACK_EXPANSION_P (pack))
12876 {
12877 tree r = copy_node (t);
12878 FOLD_EXPR_PACK (r) = pack;
12879 FOLD_EXPR_INIT (r) = init;
12880 return r;
12881 }
12882
12883 int n = TREE_VEC_LENGTH (pack);
12884 tree vec = make_tree_vec (n + 1);
12885 for (int i = 0; i < n; ++i)
12886 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12887 TREE_VEC_ELT (vec, n) = init;
12888
12889 return expand_right_fold (t, vec, complain);
12890 }
12891
12892 /* Walk through the pattern of a pack expansion, adding everything in
12893 local_specializations to a list. */
12894
12895 class el_data
12896 {
12897 public:
12898 /* Set of variables declared within the pattern. */
12899 hash_set<tree> internal;
12900 /* Set of AST nodes that have been visited by the traversal. */
12901 hash_set<tree> visited;
12902 /* List of local_specializations used within the pattern. */
12903 tree extra;
12904 tsubst_flags_t complain;
12905
12906 el_data (tsubst_flags_t c)
12907 : extra (NULL_TREE), complain (c) {}
12908 };
12909 static tree
12910 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12911 {
12912 el_data &data = *reinterpret_cast<el_data*>(data_);
12913 tree *extra = &data.extra;
12914 tsubst_flags_t complain = data.complain;
12915
12916 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12917 /* Remember local typedefs (85214). */
12918 tp = &TYPE_NAME (*tp);
12919
12920 if (TREE_CODE (*tp) == DECL_EXPR)
12921 {
12922 tree decl = DECL_EXPR_DECL (*tp);
12923 data.internal.add (decl);
12924 if (VAR_P (decl)
12925 && DECL_DECOMPOSITION_P (decl)
12926 && TREE_TYPE (decl) != error_mark_node)
12927 {
12928 gcc_assert (DECL_NAME (decl) == NULL_TREE);
12929 for (tree decl2 = DECL_CHAIN (decl);
12930 decl2
12931 && VAR_P (decl2)
12932 && DECL_DECOMPOSITION_P (decl2)
12933 && DECL_NAME (decl2)
12934 && TREE_TYPE (decl2) != error_mark_node;
12935 decl2 = DECL_CHAIN (decl2))
12936 {
12937 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
12938 data.internal.add (decl2);
12939 }
12940 }
12941 }
12942 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
12943 {
12944 /* Since we defer implicit capture, look in the parms and body. */
12945 tree fn = lambda_function (*tp);
12946 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
12947 &data.visited);
12948 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
12949 &data.visited);
12950 }
12951 else if (tree spec = retrieve_local_specialization (*tp))
12952 {
12953 if (data.internal.contains (*tp))
12954 /* Don't mess with variables declared within the pattern. */
12955 return NULL_TREE;
12956 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12957 {
12958 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12959 tree args = ARGUMENT_PACK_ARGS (spec);
12960 if (TREE_VEC_LENGTH (args) == 1)
12961 {
12962 tree elt = TREE_VEC_ELT (args, 0);
12963 if (PACK_EXPANSION_P (elt))
12964 elt = PACK_EXPANSION_PATTERN (elt);
12965 if (DECL_PACK_P (elt))
12966 spec = elt;
12967 }
12968 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12969 {
12970 /* Handle lambda capture here, since we aren't doing any
12971 substitution now, and so tsubst_copy won't call
12972 process_outer_var_ref. */
12973 tree args = ARGUMENT_PACK_ARGS (spec);
12974 int len = TREE_VEC_LENGTH (args);
12975 for (int i = 0; i < len; ++i)
12976 {
12977 tree arg = TREE_VEC_ELT (args, i);
12978 tree carg = arg;
12979 if (outer_automatic_var_p (arg))
12980 carg = process_outer_var_ref (arg, complain);
12981 if (carg != arg)
12982 {
12983 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12984 proxies. */
12985 if (i == 0)
12986 {
12987 spec = copy_node (spec);
12988 args = copy_node (args);
12989 SET_ARGUMENT_PACK_ARGS (spec, args);
12990 register_local_specialization (spec, *tp);
12991 }
12992 TREE_VEC_ELT (args, i) = carg;
12993 }
12994 }
12995 }
12996 }
12997 if (outer_automatic_var_p (spec))
12998 spec = process_outer_var_ref (spec, complain);
12999 *extra = tree_cons (*tp, spec, *extra);
13000 }
13001 return NULL_TREE;
13002 }
13003 static tree
13004 extract_local_specs (tree pattern, tsubst_flags_t complain)
13005 {
13006 el_data data (complain);
13007 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
13008 return data.extra;
13009 }
13010
13011 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13012 for use in PACK_EXPANSION_EXTRA_ARGS. */
13013
13014 tree
13015 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
13016 {
13017 /* Make a copy of the extra arguments so that they won't get changed
13018 out from under us. */
13019 tree extra = copy_template_args (args);
13020 if (local_specializations)
13021 if (tree locals = extract_local_specs (pattern, complain))
13022 extra = tree_cons (NULL_TREE, extra, locals);
13023 return extra;
13024 }
13025
13026 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13027 normal template args to ARGS. */
13028
13029 tree
13030 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
13031 {
13032 if (extra && TREE_CODE (extra) == TREE_LIST)
13033 {
13034 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
13035 {
13036 /* The partial instantiation involved local declarations collected in
13037 extract_local_specs; map from the general template to our local
13038 context. */
13039 tree gen = TREE_PURPOSE (elt);
13040 tree inst = TREE_VALUE (elt);
13041 if (DECL_P (inst))
13042 if (tree local = retrieve_local_specialization (inst))
13043 inst = local;
13044 /* else inst is already a full instantiation of the pack. */
13045 register_local_specialization (inst, gen);
13046 }
13047 gcc_assert (!TREE_PURPOSE (extra));
13048 extra = TREE_VALUE (extra);
13049 }
13050 if (uses_template_parms (extra))
13051 {
13052 /* This can happen after dependent substitution into a
13053 requires-expr or a lambda that uses constexpr if. */
13054 extra = tsubst_template_args (extra, args, complain, in_decl);
13055 args = add_outermost_template_args (args, extra);
13056 }
13057 else
13058 args = add_to_template_args (extra, args);
13059 return args;
13060 }
13061
13062 /* Substitute ARGS into T, which is an pack expansion
13063 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13064 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13065 (if only a partial substitution could be performed) or
13066 ERROR_MARK_NODE if there was an error. */
13067 tree
13068 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
13069 tree in_decl)
13070 {
13071 tree pattern;
13072 tree pack, packs = NULL_TREE;
13073 bool unsubstituted_packs = false;
13074 int i, len = -1;
13075 tree result;
13076 bool need_local_specializations = false;
13077 int levels;
13078
13079 gcc_assert (PACK_EXPANSION_P (t));
13080 pattern = PACK_EXPANSION_PATTERN (t);
13081
13082 /* Add in any args remembered from an earlier partial instantiation. */
13083 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
13084
13085 levels = TMPL_ARGS_DEPTH (args);
13086
13087 /* Determine the argument packs that will instantiate the parameter
13088 packs used in the expansion expression. While we're at it,
13089 compute the number of arguments to be expanded and make sure it
13090 is consistent. */
13091 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13092 pack = TREE_CHAIN (pack))
13093 {
13094 tree parm_pack = TREE_VALUE (pack);
13095 tree arg_pack = NULL_TREE;
13096 tree orig_arg = NULL_TREE;
13097 int level = 0;
13098
13099 if (TREE_CODE (parm_pack) == BASES)
13100 {
13101 gcc_assert (parm_pack == pattern);
13102 if (BASES_DIRECT (parm_pack))
13103 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
13104 args, complain,
13105 in_decl, false),
13106 complain);
13107 else
13108 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
13109 args, complain, in_decl,
13110 false), complain);
13111 }
13112 else if (builtin_pack_call_p (parm_pack))
13113 {
13114 if (parm_pack != pattern)
13115 {
13116 if (complain & tf_error)
13117 sorry ("%qE is not the entire pattern of the pack expansion",
13118 parm_pack);
13119 return error_mark_node;
13120 }
13121 return expand_builtin_pack_call (parm_pack, args,
13122 complain, in_decl);
13123 }
13124 else if (TREE_CODE (parm_pack) == PARM_DECL)
13125 {
13126 /* We know we have correct local_specializations if this
13127 expansion is at function scope, or if we're dealing with a
13128 local parameter in a requires expression; for the latter,
13129 tsubst_requires_expr set it up appropriately. */
13130 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13131 arg_pack = retrieve_local_specialization (parm_pack);
13132 else
13133 /* We can't rely on local_specializations for a parameter
13134 name used later in a function declaration (such as in a
13135 late-specified return type). Even if it exists, it might
13136 have the wrong value for a recursive call. */
13137 need_local_specializations = true;
13138
13139 if (!arg_pack)
13140 {
13141 /* This parameter pack was used in an unevaluated context. Just
13142 make a dummy decl, since it's only used for its type. */
13143 ++cp_unevaluated_operand;
13144 arg_pack = tsubst_decl (parm_pack, args, complain);
13145 --cp_unevaluated_operand;
13146 if (arg_pack && DECL_PACK_P (arg_pack))
13147 /* Partial instantiation of the parm_pack, we can't build
13148 up an argument pack yet. */
13149 arg_pack = NULL_TREE;
13150 else
13151 arg_pack = make_fnparm_pack (arg_pack);
13152 }
13153 else if (DECL_PACK_P (arg_pack))
13154 /* This argument pack isn't fully instantiated yet. */
13155 arg_pack = NULL_TREE;
13156 }
13157 else if (is_capture_proxy (parm_pack))
13158 {
13159 arg_pack = retrieve_local_specialization (parm_pack);
13160 if (DECL_PACK_P (arg_pack))
13161 arg_pack = NULL_TREE;
13162 }
13163 else
13164 {
13165 int idx;
13166 template_parm_level_and_index (parm_pack, &level, &idx);
13167 if (level <= levels)
13168 arg_pack = TMPL_ARG (args, level, idx);
13169
13170 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13171 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13172 arg_pack = NULL_TREE;
13173 }
13174
13175 orig_arg = arg_pack;
13176 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13177 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13178
13179 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13180 /* This can only happen if we forget to expand an argument
13181 pack somewhere else. Just return an error, silently. */
13182 {
13183 result = make_tree_vec (1);
13184 TREE_VEC_ELT (result, 0) = error_mark_node;
13185 return result;
13186 }
13187
13188 if (arg_pack)
13189 {
13190 int my_len =
13191 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13192
13193 /* Don't bother trying to do a partial substitution with
13194 incomplete packs; we'll try again after deduction. */
13195 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13196 return t;
13197
13198 if (len < 0)
13199 len = my_len;
13200 else if (len != my_len)
13201 {
13202 if (!(complain & tf_error))
13203 /* Fail quietly. */;
13204 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13205 error ("mismatched argument pack lengths while expanding %qT",
13206 pattern);
13207 else
13208 error ("mismatched argument pack lengths while expanding %qE",
13209 pattern);
13210 return error_mark_node;
13211 }
13212
13213 /* Keep track of the parameter packs and their corresponding
13214 argument packs. */
13215 packs = tree_cons (parm_pack, arg_pack, packs);
13216 TREE_TYPE (packs) = orig_arg;
13217 }
13218 else
13219 {
13220 /* We can't substitute for this parameter pack. We use a flag as
13221 well as the missing_level counter because function parameter
13222 packs don't have a level. */
13223 gcc_assert (processing_template_decl || is_auto (parm_pack));
13224 unsubstituted_packs = true;
13225 }
13226 }
13227
13228 /* If the expansion is just T..., return the matching argument pack, unless
13229 we need to call convert_from_reference on all the elements. This is an
13230 important optimization; see c++/68422. */
13231 if (!unsubstituted_packs
13232 && TREE_PURPOSE (packs) == pattern)
13233 {
13234 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13235
13236 /* If the argument pack is a single pack expansion, pull it out. */
13237 if (TREE_VEC_LENGTH (args) == 1
13238 && pack_expansion_args_count (args))
13239 return TREE_VEC_ELT (args, 0);
13240
13241 /* Types need no adjustment, nor does sizeof..., and if we still have
13242 some pack expansion args we won't do anything yet. */
13243 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13244 || PACK_EXPANSION_SIZEOF_P (t)
13245 || pack_expansion_args_count (args))
13246 return args;
13247 /* Also optimize expression pack expansions if we can tell that the
13248 elements won't have reference type. */
13249 tree type = TREE_TYPE (pattern);
13250 if (type && !TYPE_REF_P (type)
13251 && !PACK_EXPANSION_P (type)
13252 && !WILDCARD_TYPE_P (type))
13253 return args;
13254 /* Otherwise use the normal path so we get convert_from_reference. */
13255 }
13256
13257 /* We cannot expand this expansion expression, because we don't have
13258 all of the argument packs we need. */
13259 if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
13260 {
13261 /* We got some full packs, but we can't substitute them in until we
13262 have values for all the packs. So remember these until then. */
13263
13264 t = make_pack_expansion (pattern, complain);
13265 PACK_EXPANSION_EXTRA_ARGS (t)
13266 = build_extra_args (pattern, args, complain);
13267 return t;
13268 }
13269
13270 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13271 type, so create our own local specializations map; the current map is
13272 either NULL or (in the case of recursive unification) might have
13273 bindings that we don't want to use or alter. */
13274 local_specialization_stack lss (need_local_specializations
13275 ? lss_blank : lss_nop);
13276
13277 if (unsubstituted_packs)
13278 {
13279 /* There were no real arguments, we're just replacing a parameter
13280 pack with another version of itself. Substitute into the
13281 pattern and return a PACK_EXPANSION_*. The caller will need to
13282 deal with that. */
13283 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13284 result = tsubst_expr (pattern, args, complain, in_decl,
13285 /*integral_constant_expression_p=*/false);
13286 else
13287 result = tsubst (pattern, args, complain, in_decl);
13288 result = make_pack_expansion (result, complain);
13289 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13290 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13291 if (PACK_EXPANSION_AUTO_P (t))
13292 {
13293 /* This is a fake auto... pack expansion created in add_capture with
13294 _PACKS that don't appear in the pattern. Copy one over. */
13295 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13296 pack = retrieve_local_specialization (TREE_VALUE (packs));
13297 gcc_checking_assert (DECL_PACK_P (pack));
13298 PACK_EXPANSION_PARAMETER_PACKS (result)
13299 = build_tree_list (NULL_TREE, pack);
13300 PACK_EXPANSION_AUTO_P (result) = true;
13301 }
13302 return result;
13303 }
13304
13305 gcc_assert (len >= 0);
13306
13307 /* For each argument in each argument pack, substitute into the
13308 pattern. */
13309 result = make_tree_vec (len);
13310 tree elem_args = copy_template_args (args);
13311 for (i = 0; i < len; ++i)
13312 {
13313 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13314 i,
13315 elem_args, complain,
13316 in_decl);
13317 TREE_VEC_ELT (result, i) = t;
13318 if (t == error_mark_node)
13319 {
13320 result = error_mark_node;
13321 break;
13322 }
13323 }
13324
13325 /* Update ARGS to restore the substitution from parameter packs to
13326 their argument packs. */
13327 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13328 {
13329 tree parm = TREE_PURPOSE (pack);
13330
13331 if (TREE_CODE (parm) == PARM_DECL
13332 || VAR_P (parm)
13333 || TREE_CODE (parm) == FIELD_DECL)
13334 register_local_specialization (TREE_TYPE (pack), parm);
13335 else
13336 {
13337 int idx, level;
13338
13339 if (TREE_VALUE (pack) == NULL_TREE)
13340 continue;
13341
13342 template_parm_level_and_index (parm, &level, &idx);
13343
13344 /* Update the corresponding argument. */
13345 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13346 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13347 TREE_TYPE (pack);
13348 else
13349 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13350 }
13351 }
13352
13353 /* If the dependent pack arguments were such that we end up with only a
13354 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13355 if (len == 1 && TREE_CODE (result) == TREE_VEC
13356 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13357 return TREE_VEC_ELT (result, 0);
13358
13359 return result;
13360 }
13361
13362 /* Make an argument pack out of the TREE_VEC VEC. */
13363
13364 static tree
13365 make_argument_pack (tree vec)
13366 {
13367 tree pack;
13368
13369 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13370 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13371 else
13372 {
13373 pack = make_node (NONTYPE_ARGUMENT_PACK);
13374 TREE_CONSTANT (pack) = 1;
13375 }
13376 SET_ARGUMENT_PACK_ARGS (pack, vec);
13377 return pack;
13378 }
13379
13380 /* Return an exact copy of template args T that can be modified
13381 independently. */
13382
13383 static tree
13384 copy_template_args (tree t)
13385 {
13386 if (t == error_mark_node)
13387 return t;
13388
13389 int len = TREE_VEC_LENGTH (t);
13390 tree new_vec = make_tree_vec (len);
13391
13392 for (int i = 0; i < len; ++i)
13393 {
13394 tree elt = TREE_VEC_ELT (t, i);
13395 if (elt && TREE_CODE (elt) == TREE_VEC)
13396 elt = copy_template_args (elt);
13397 TREE_VEC_ELT (new_vec, i) = elt;
13398 }
13399
13400 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13401 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13402
13403 return new_vec;
13404 }
13405
13406 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13407
13408 tree
13409 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13410 tree in_decl)
13411 {
13412 /* Substitute into each of the arguments. */
13413 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13414 args, complain, in_decl);
13415 tree new_arg = error_mark_node;
13416 if (pack_args != error_mark_node)
13417 {
13418 if (TYPE_P (orig_arg))
13419 {
13420 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13421 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13422 }
13423 else
13424 {
13425 new_arg = make_node (TREE_CODE (orig_arg));
13426 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13427 }
13428
13429 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13430 }
13431
13432 return new_arg;
13433 }
13434
13435 /* Substitute ARGS into the vector or list of template arguments T. */
13436
13437 tree
13438 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13439 {
13440 tree orig_t = t;
13441 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13442 tree *elts;
13443
13444 if (t == error_mark_node)
13445 return error_mark_node;
13446
13447 len = TREE_VEC_LENGTH (t);
13448 elts = XALLOCAVEC (tree, len);
13449
13450 for (i = 0; i < len; i++)
13451 {
13452 tree orig_arg = TREE_VEC_ELT (t, i);
13453 tree new_arg;
13454
13455 if (!orig_arg)
13456 new_arg = NULL_TREE;
13457 else if (TREE_CODE (orig_arg) == TREE_VEC)
13458 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13459 else if (PACK_EXPANSION_P (orig_arg))
13460 {
13461 /* Substitute into an expansion expression. */
13462 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13463
13464 if (TREE_CODE (new_arg) == TREE_VEC)
13465 /* Add to the expanded length adjustment the number of
13466 expanded arguments. We subtract one from this
13467 measurement, because the argument pack expression
13468 itself is already counted as 1 in
13469 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13470 the argument pack is empty. */
13471 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13472 }
13473 else if (ARGUMENT_PACK_P (orig_arg))
13474 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13475 else
13476 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13477
13478 if (new_arg == error_mark_node)
13479 return error_mark_node;
13480
13481 elts[i] = new_arg;
13482 if (new_arg != orig_arg)
13483 need_new = 1;
13484 }
13485
13486 if (!need_new)
13487 return t;
13488
13489 /* Make space for the expanded arguments coming from template
13490 argument packs. */
13491 t = make_tree_vec (len + expanded_len_adjust);
13492 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13493 arguments for a member template.
13494 In that case each TREE_VEC in ORIG_T represents a level of template
13495 arguments, and ORIG_T won't carry any non defaulted argument count.
13496 It will rather be the nested TREE_VECs that will carry one.
13497 In other words, ORIG_T carries a non defaulted argument count only
13498 if it doesn't contain any nested TREE_VEC. */
13499 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13500 {
13501 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13502 count += expanded_len_adjust;
13503 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13504 }
13505 for (i = 0, out = 0; i < len; i++)
13506 {
13507 tree orig_arg = TREE_VEC_ELT (orig_t, i);
13508 if (orig_arg
13509 && (PACK_EXPANSION_P (orig_arg) || ARGUMENT_PACK_P (orig_arg))
13510 && TREE_CODE (elts[i]) == TREE_VEC)
13511 {
13512 int idx;
13513
13514 /* Now expand the template argument pack "in place". */
13515 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13516 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13517 }
13518 else
13519 {
13520 TREE_VEC_ELT (t, out) = elts[i];
13521 out++;
13522 }
13523 }
13524
13525 return t;
13526 }
13527
13528 /* Substitute ARGS into one level PARMS of template parameters. */
13529
13530 static tree
13531 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13532 {
13533 if (parms == error_mark_node)
13534 return error_mark_node;
13535
13536 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13537
13538 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13539 {
13540 tree tuple = TREE_VEC_ELT (parms, i);
13541
13542 if (tuple == error_mark_node)
13543 continue;
13544
13545 TREE_VEC_ELT (new_vec, i) =
13546 tsubst_template_parm (tuple, args, complain);
13547 }
13548
13549 return new_vec;
13550 }
13551
13552 /* Return the result of substituting ARGS into the template parameters
13553 given by PARMS. If there are m levels of ARGS and m + n levels of
13554 PARMS, then the result will contain n levels of PARMS. For
13555 example, if PARMS is `template <class T> template <class U>
13556 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13557 result will be `template <int*, double, class V>'. */
13558
13559 static tree
13560 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13561 {
13562 tree r = NULL_TREE;
13563 tree* new_parms;
13564
13565 /* When substituting into a template, we must set
13566 PROCESSING_TEMPLATE_DECL as the template parameters may be
13567 dependent if they are based on one-another, and the dependency
13568 predicates are short-circuit outside of templates. */
13569 ++processing_template_decl;
13570
13571 for (new_parms = &r;
13572 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13573 new_parms = &(TREE_CHAIN (*new_parms)),
13574 parms = TREE_CHAIN (parms))
13575 {
13576 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13577 args, complain);
13578 *new_parms =
13579 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13580 - TMPL_ARGS_DEPTH (args)),
13581 new_vec, NULL_TREE);
13582 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13583 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13584 }
13585
13586 --processing_template_decl;
13587
13588 return r;
13589 }
13590
13591 /* Return the result of substituting ARGS into one template parameter
13592 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13593 parameter and which TREE_PURPOSE is the default argument of the
13594 template parameter. */
13595
13596 static tree
13597 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13598 {
13599 tree default_value, parm_decl;
13600
13601 if (args == NULL_TREE
13602 || t == NULL_TREE
13603 || t == error_mark_node)
13604 return t;
13605
13606 gcc_assert (TREE_CODE (t) == TREE_LIST);
13607
13608 default_value = TREE_PURPOSE (t);
13609 parm_decl = TREE_VALUE (t);
13610 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13611
13612 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13613 if (TREE_CODE (parm_decl) == PARM_DECL
13614 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13615 parm_decl = error_mark_node;
13616 default_value = tsubst_template_arg (default_value, args,
13617 complain, NULL_TREE);
13618 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13619
13620 tree r = build_tree_list (default_value, parm_decl);
13621 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13622 return r;
13623 }
13624
13625 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13626 type T. If T is not an aggregate or enumeration type, it is
13627 handled as if by tsubst. IN_DECL is as for tsubst. If
13628 ENTERING_SCOPE is nonzero, T is the context for a template which
13629 we are presently tsubst'ing. Return the substituted value. */
13630
13631 static tree
13632 tsubst_aggr_type (tree t,
13633 tree args,
13634 tsubst_flags_t complain,
13635 tree in_decl,
13636 int entering_scope)
13637 {
13638 if (t == NULL_TREE)
13639 return NULL_TREE;
13640
13641 /* If T is an alias template specialization, we want to substitute that
13642 rather than strip it, especially if it's dependent_alias_template_spec_p.
13643 It should be OK not to handle entering_scope in this case, since
13644 DECL_CONTEXT will never be an alias template specialization. We only get
13645 here with an alias when tsubst calls us for TYPENAME_TYPE. */
13646 if (alias_template_specialization_p (t, nt_transparent))
13647 return tsubst (t, args, complain, in_decl);
13648
13649 switch (TREE_CODE (t))
13650 {
13651 case RECORD_TYPE:
13652 if (TYPE_PTRMEMFUNC_P (t))
13653 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13654
13655 /* Fall through. */
13656 case ENUMERAL_TYPE:
13657 case UNION_TYPE:
13658 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13659 {
13660 tree argvec;
13661 tree context;
13662 tree r;
13663
13664 /* In "sizeof(X<I>)" we need to evaluate "I". */
13665 cp_evaluated ev;
13666
13667 /* First, determine the context for the type we are looking
13668 up. */
13669 context = TYPE_CONTEXT (t);
13670 if (context && TYPE_P (context))
13671 {
13672 context = tsubst_aggr_type (context, args, complain,
13673 in_decl, /*entering_scope=*/1);
13674 /* If context is a nested class inside a class template,
13675 it may still need to be instantiated (c++/33959). */
13676 context = complete_type (context);
13677 }
13678
13679 /* Then, figure out what arguments are appropriate for the
13680 type we are trying to find. For example, given:
13681
13682 template <class T> struct S;
13683 template <class T, class U> void f(T, U) { S<U> su; }
13684
13685 and supposing that we are instantiating f<int, double>,
13686 then our ARGS will be {int, double}, but, when looking up
13687 S we only want {double}. */
13688 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13689 complain, in_decl);
13690 if (argvec == error_mark_node)
13691 r = error_mark_node;
13692 else if (!entering_scope
13693 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13694 {
13695 /* See maybe_dependent_member_ref. */
13696 tree name = TYPE_IDENTIFIER (t);
13697 tree fullname = name;
13698 if (instantiates_primary_template_p (t))
13699 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13700 INNERMOST_TEMPLATE_ARGS (argvec));
13701 return build_typename_type (context, name, fullname,
13702 typename_type);
13703 }
13704 else
13705 {
13706 r = lookup_template_class (t, argvec, in_decl, context,
13707 entering_scope, complain);
13708 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13709 }
13710
13711 return r;
13712 }
13713 else
13714 /* This is not a template type, so there's nothing to do. */
13715 return t;
13716
13717 default:
13718 return tsubst (t, args, complain, in_decl);
13719 }
13720 }
13721
13722 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13723 indexed in reverse order of the parameters. */
13724
13725 static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
13726
13727 /* Return a reference to the vec* of defarg insts for FN. */
13728
13729 static vec<tree,va_gc> *&
13730 defarg_insts_for (tree fn)
13731 {
13732 if (!defarg_inst)
13733 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
13734 tree_vec_map in = { { fn }, nullptr };
13735 tree_vec_map **slot
13736 = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
13737 if (!*slot)
13738 {
13739 *slot = ggc_alloc<tree_vec_map> ();
13740 **slot = in;
13741 }
13742 return (*slot)->to;
13743 }
13744
13745 /* Substitute into the default argument ARG (a default argument for
13746 FN), which has the indicated TYPE. */
13747
13748 tree
13749 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13750 tsubst_flags_t complain)
13751 {
13752 int errs = errorcount + sorrycount;
13753
13754 /* This can happen in invalid code. */
13755 if (TREE_CODE (arg) == DEFERRED_PARSE)
13756 return arg;
13757
13758 /* Shortcut {}. */
13759 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13760 && CONSTRUCTOR_NELTS (arg) == 0)
13761 return arg;
13762
13763 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13764 parm = chain_index (parmnum, parm);
13765 tree parmtype = TREE_TYPE (parm);
13766 if (DECL_BY_REFERENCE (parm))
13767 parmtype = TREE_TYPE (parmtype);
13768 if (parmtype == error_mark_node)
13769 return error_mark_node;
13770
13771 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13772
13773 /* Remember the location of the pointer to the vec rather than the location
13774 of the particular element, in case the vec grows in tsubst_expr. */
13775 vec<tree,va_gc> *&defs = defarg_insts_for (fn);
13776 /* Index in reverse order to avoid allocating space for initial parameters
13777 that don't have default arguments. */
13778 unsigned ridx = list_length (parm);
13779 if (vec_safe_length (defs) < ridx)
13780 vec_safe_grow_cleared (defs, ridx);
13781 else if (tree inst = (*defs)[ridx - 1])
13782 return inst;
13783
13784 /* This default argument came from a template. Instantiate the
13785 default argument here, not in tsubst. In the case of
13786 something like:
13787
13788 template <class T>
13789 struct S {
13790 static T t();
13791 void f(T = t());
13792 };
13793
13794 we must be careful to do name lookup in the scope of S<T>,
13795 rather than in the current class. */
13796 push_to_top_level ();
13797 push_access_scope (fn);
13798 push_deferring_access_checks (dk_no_deferred);
13799 start_lambda_scope (parm);
13800
13801 /* The default argument expression may cause implicitly defined
13802 member functions to be synthesized, which will result in garbage
13803 collection. We must treat this situation as if we were within
13804 the body of function so as to avoid collecting live data on the
13805 stack. */
13806 ++function_depth;
13807 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13808 complain, NULL_TREE,
13809 /*integral_constant_expression_p=*/false);
13810 --function_depth;
13811
13812 finish_lambda_scope ();
13813
13814 /* Make sure the default argument is reasonable. */
13815 arg = check_default_argument (type, arg, complain);
13816
13817 if (errorcount+sorrycount > errs
13818 && (complain & tf_warning_or_error))
13819 inform (input_location,
13820 " when instantiating default argument for call to %qD", fn);
13821
13822 pop_deferring_access_checks ();
13823 pop_access_scope (fn);
13824 pop_from_top_level ();
13825
13826 if (arg != error_mark_node && !cp_unevaluated_operand)
13827 (*defs)[ridx - 1] = arg;
13828
13829 return arg;
13830 }
13831
13832 /* Substitute into all the default arguments for FN. */
13833
13834 static void
13835 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13836 {
13837 tree arg;
13838 tree tmpl_args;
13839
13840 tmpl_args = DECL_TI_ARGS (fn);
13841
13842 /* If this function is not yet instantiated, we certainly don't need
13843 its default arguments. */
13844 if (uses_template_parms (tmpl_args))
13845 return;
13846 /* Don't do this again for clones. */
13847 if (DECL_CLONED_FUNCTION_P (fn))
13848 return;
13849
13850 int i = 0;
13851 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13852 arg;
13853 arg = TREE_CHAIN (arg), ++i)
13854 if (TREE_PURPOSE (arg))
13855 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13856 TREE_VALUE (arg),
13857 TREE_PURPOSE (arg),
13858 complain);
13859 }
13860
13861 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13862 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13863
13864 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13865
13866 void
13867 store_explicit_specifier (tree v, tree t)
13868 {
13869 if (!explicit_specifier_map)
13870 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13871 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13872 explicit_specifier_map->put (v, t);
13873 }
13874
13875 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13876
13877 tree
13878 lookup_explicit_specifier (tree v)
13879 {
13880 return *explicit_specifier_map->get (v);
13881 }
13882
13883 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13884 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13885 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13886 identical to T. */
13887
13888 static tree
13889 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13890 tree raises, tsubst_flags_t complain)
13891 {
13892 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13893
13894 tree new_type;
13895 if (TREE_CODE (t) == FUNCTION_TYPE)
13896 {
13897 new_type = build_function_type (return_type, arg_types);
13898 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13899 }
13900 else
13901 {
13902 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13903 /* Don't pick up extra function qualifiers from the basetype. */
13904 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13905 if (! MAYBE_CLASS_TYPE_P (r))
13906 {
13907 /* [temp.deduct]
13908
13909 Type deduction may fail for any of the following
13910 reasons:
13911
13912 -- Attempting to create "pointer to member of T" when T
13913 is not a class type. */
13914 if (complain & tf_error)
13915 error ("creating pointer to member function of non-class type %qT",
13916 r);
13917 return error_mark_node;
13918 }
13919
13920 new_type = build_method_type_directly (r, return_type,
13921 TREE_CHAIN (arg_types));
13922 }
13923 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13924
13925 cp_ref_qualifier rqual = type_memfn_rqual (t);
13926 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13927 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13928 }
13929
13930 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13931 each of its formal parameters. If there is a disagreement then rebuild
13932 DECL's function type according to its formal parameter types, as part of a
13933 resolution for Core issues 1001/1322. */
13934
13935 static void
13936 maybe_rebuild_function_decl_type (tree decl)
13937 {
13938 bool function_type_needs_rebuilding = false;
13939 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13940 {
13941 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13942 while (parm_type_list && parm_type_list != void_list_node)
13943 {
13944 tree parm_type = TREE_VALUE (parm_type_list);
13945 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13946 if (!same_type_p (parm_type, formal_parm_type_unqual))
13947 {
13948 function_type_needs_rebuilding = true;
13949 break;
13950 }
13951
13952 parm_list = DECL_CHAIN (parm_list);
13953 parm_type_list = TREE_CHAIN (parm_type_list);
13954 }
13955 }
13956
13957 if (!function_type_needs_rebuilding)
13958 return;
13959
13960 const tree fntype = TREE_TYPE (decl);
13961 tree parm_list = DECL_ARGUMENTS (decl);
13962 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13963 tree new_parm_type_list = NULL_TREE;
13964 tree *q = &new_parm_type_list;
13965 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13966 {
13967 *q = copy_node (old_parm_type_list);
13968 parm_list = DECL_CHAIN (parm_list);
13969 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13970 q = &TREE_CHAIN (*q);
13971 }
13972 while (old_parm_type_list && old_parm_type_list != void_list_node)
13973 {
13974 *q = copy_node (old_parm_type_list);
13975 tree *new_parm_type = &TREE_VALUE (*q);
13976 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13977 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13978 *new_parm_type = formal_parm_type_unqual;
13979
13980 parm_list = DECL_CHAIN (parm_list);
13981 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13982 q = &TREE_CHAIN (*q);
13983 }
13984 if (old_parm_type_list == void_list_node)
13985 *q = void_list_node;
13986
13987 TREE_TYPE (decl)
13988 = rebuild_function_or_method_type (fntype,
13989 TREE_TYPE (fntype), new_parm_type_list,
13990 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13991 }
13992
13993 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13994
13995 static tree
13996 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13997 tree lambda_fntype)
13998 {
13999 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
14000 hashval_t hash = 0;
14001 tree in_decl = t;
14002
14003 /* Nobody should be tsubst'ing into non-template functions. */
14004 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
14005 || DECL_LOCAL_DECL_P (t));
14006
14007 if (DECL_LOCAL_DECL_P (t))
14008 {
14009 if (tree spec = retrieve_local_specialization (t))
14010 return spec;
14011 }
14012 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
14013 {
14014 /* If T is not dependent, just return it. */
14015 if (!uses_template_parms (DECL_TI_ARGS (t))
14016 && !LAMBDA_FUNCTION_P (t))
14017 return t;
14018
14019 /* Calculate the most general template of which R is a
14020 specialization. */
14021 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
14022
14023 /* We're substituting a lambda function under tsubst_lambda_expr but not
14024 directly from it; find the matching function we're already inside.
14025 But don't do this if T is a generic lambda with a single level of
14026 template parms, as in that case we're doing a normal instantiation. */
14027 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
14028 && (!generic_lambda_fn_p (t)
14029 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
14030 return enclosing_instantiation_of (t);
14031
14032 /* Calculate the complete set of arguments used to
14033 specialize R. */
14034 argvec = tsubst_template_args (DECL_TI_ARGS
14035 (DECL_TEMPLATE_RESULT
14036 (DECL_TI_TEMPLATE (t))),
14037 args, complain, in_decl);
14038 if (argvec == error_mark_node)
14039 return error_mark_node;
14040
14041 /* Check to see if we already have this specialization. */
14042 if (!lambda_fntype)
14043 {
14044 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14045 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
14046 return spec;
14047 }
14048 }
14049 else
14050 {
14051 /* This special case arises when we have something like this:
14052
14053 template <class T> struct S {
14054 friend void f<int>(int, double);
14055 };
14056
14057 Here, the DECL_TI_TEMPLATE for the friend declaration
14058 will be an IDENTIFIER_NODE. We are being called from
14059 tsubst_friend_function, and we want only to create a
14060 new decl (R) with appropriate types so that we can call
14061 determine_specialization. */
14062 gen_tmpl = NULL_TREE;
14063 argvec = NULL_TREE;
14064 }
14065
14066 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
14067 : NULL_TREE);
14068 tree ctx = closure ? closure : DECL_CONTEXT (t);
14069 bool member = ctx && TYPE_P (ctx);
14070
14071 if (member && !closure)
14072 ctx = tsubst_aggr_type (ctx, args,
14073 complain, t, /*entering_scope=*/1);
14074
14075 tree type = (lambda_fntype ? lambda_fntype
14076 : tsubst (TREE_TYPE (t), args,
14077 complain | tf_fndecl_type, in_decl));
14078 if (type == error_mark_node)
14079 return error_mark_node;
14080
14081 /* If we hit excessive deduction depth, the type is bogus even if
14082 it isn't error_mark_node, so don't build a decl. */
14083 if (excessive_deduction_depth)
14084 return error_mark_node;
14085
14086 /* We do NOT check for matching decls pushed separately at this
14087 point, as they may not represent instantiations of this
14088 template, and in any case are considered separate under the
14089 discrete model. */
14090 tree r = copy_decl (t);
14091 DECL_USE_TEMPLATE (r) = 0;
14092 TREE_TYPE (r) = type;
14093 /* Clear out the mangled name and RTL for the instantiation. */
14094 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14095 SET_DECL_RTL (r, NULL);
14096 /* Leave DECL_INITIAL set on deleted instantiations. */
14097 if (!DECL_DELETED_FN (r))
14098 DECL_INITIAL (r) = NULL_TREE;
14099 DECL_CONTEXT (r) = ctx;
14100 set_instantiating_module (r);
14101
14102 /* Handle explicit(dependent-expr). */
14103 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
14104 {
14105 tree spec = lookup_explicit_specifier (t);
14106 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
14107 /*function_p=*/false,
14108 /*i_c_e_p=*/true);
14109 spec = build_explicit_specifier (spec, complain);
14110 if (instantiation_dependent_expression_p (spec))
14111 store_explicit_specifier (r, spec);
14112 else
14113 {
14114 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
14115 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
14116 }
14117 }
14118
14119 /* OpenMP UDRs have the only argument a reference to the declared
14120 type. We want to diagnose if the declared type is a reference,
14121 which is invalid, but as references to references are usually
14122 quietly merged, diagnose it here. */
14123 if (DECL_OMP_DECLARE_REDUCTION_P (t))
14124 {
14125 tree argtype
14126 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
14127 argtype = tsubst (argtype, args, complain, in_decl);
14128 if (TYPE_REF_P (argtype))
14129 error_at (DECL_SOURCE_LOCATION (t),
14130 "reference type %qT in "
14131 "%<#pragma omp declare reduction%>", argtype);
14132 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
14133 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14134 argtype);
14135 }
14136
14137 if (member && DECL_CONV_FN_P (r))
14138 /* Type-conversion operator. Reconstruct the name, in
14139 case it's the name of one of the template's parameters. */
14140 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14141
14142 tree parms = DECL_ARGUMENTS (t);
14143 if (closure)
14144 parms = DECL_CHAIN (parms);
14145 parms = tsubst (parms, args, complain, t);
14146 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14147 DECL_CONTEXT (parm) = r;
14148 if (closure)
14149 {
14150 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14151 DECL_NAME (tparm) = closure_identifier;
14152 DECL_CHAIN (tparm) = parms;
14153 parms = tparm;
14154 }
14155 DECL_ARGUMENTS (r) = parms;
14156 DECL_RESULT (r) = NULL_TREE;
14157
14158 maybe_rebuild_function_decl_type (r);
14159
14160 TREE_STATIC (r) = 0;
14161 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14162 DECL_EXTERNAL (r) = 1;
14163 /* If this is an instantiation of a function with internal
14164 linkage, we already know what object file linkage will be
14165 assigned to the instantiation. */
14166 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14167 DECL_DEFER_OUTPUT (r) = 0;
14168 DECL_CHAIN (r) = NULL_TREE;
14169 DECL_PENDING_INLINE_INFO (r) = 0;
14170 DECL_PENDING_INLINE_P (r) = 0;
14171 DECL_SAVED_TREE (r) = NULL_TREE;
14172 DECL_STRUCT_FUNCTION (r) = NULL;
14173 TREE_USED (r) = 0;
14174 /* We'll re-clone as appropriate in instantiate_template. */
14175 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14176
14177 /* If we aren't complaining now, return on error before we register
14178 the specialization so that we'll complain eventually. */
14179 if ((complain & tf_error) == 0
14180 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14181 && !grok_op_properties (r, /*complain=*/false))
14182 return error_mark_node;
14183
14184 /* Associate the constraints directly with the instantiation. We
14185 don't substitute through the constraints; that's only done when
14186 they are checked. */
14187 if (tree ci = get_constraints (t))
14188 set_constraints (r, ci);
14189
14190 if (DECL_FRIEND_CONTEXT (t))
14191 SET_DECL_FRIEND_CONTEXT (r,
14192 tsubst (DECL_FRIEND_CONTEXT (t),
14193 args, complain, in_decl));
14194
14195 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14196 args, complain, in_decl))
14197 return error_mark_node;
14198
14199 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14200 this in the special friend case mentioned above where
14201 GEN_TMPL is NULL. */
14202 if (gen_tmpl && !closure)
14203 {
14204 DECL_TEMPLATE_INFO (r)
14205 = build_template_info (gen_tmpl, argvec);
14206 SET_DECL_IMPLICIT_INSTANTIATION (r);
14207
14208 tree new_r
14209 = register_specialization (r, gen_tmpl, argvec, false, hash);
14210 if (new_r != r)
14211 /* We instantiated this while substituting into
14212 the type earlier (template/friend54.C). */
14213 return new_r;
14214
14215 /* We're not supposed to instantiate default arguments
14216 until they are called, for a template. But, for a
14217 declaration like:
14218
14219 template <class T> void f ()
14220 { extern void g(int i = T()); }
14221
14222 we should do the substitution when the template is
14223 instantiated. We handle the member function case in
14224 instantiate_class_template since the default arguments
14225 might refer to other members of the class. */
14226 if (!member
14227 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14228 && !uses_template_parms (argvec))
14229 tsubst_default_arguments (r, complain);
14230 }
14231 else if (DECL_LOCAL_DECL_P (r))
14232 {
14233 if (!cp_unevaluated_operand)
14234 register_local_specialization (r, t);
14235 }
14236 else
14237 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14238
14239 /* Copy the list of befriending classes. */
14240 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14241 *friends;
14242 friends = &TREE_CHAIN (*friends))
14243 {
14244 *friends = copy_node (*friends);
14245 TREE_VALUE (*friends)
14246 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14247 }
14248
14249 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14250 {
14251 maybe_retrofit_in_chrg (r);
14252 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14253 return error_mark_node;
14254 /* If this is an instantiation of a member template, clone it.
14255 If it isn't, that'll be handled by
14256 clone_constructors_and_destructors. */
14257 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14258 clone_cdtor (r, /*update_methods=*/false);
14259 }
14260 else if ((complain & tf_error) != 0
14261 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14262 && !grok_op_properties (r, /*complain=*/true))
14263 return error_mark_node;
14264
14265 /* Possibly limit visibility based on template args. */
14266 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14267 if (DECL_VISIBILITY_SPECIFIED (t))
14268 {
14269 DECL_VISIBILITY_SPECIFIED (r) = 0;
14270 DECL_ATTRIBUTES (r)
14271 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14272 }
14273 determine_visibility (r);
14274 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14275 && !processing_template_decl)
14276 defaulted_late_check (r);
14277
14278 if (flag_openmp)
14279 if (tree attr = lookup_attribute ("omp declare variant base",
14280 DECL_ATTRIBUTES (r)))
14281 omp_declare_variant_finalize (r, attr);
14282
14283 return r;
14284 }
14285
14286 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14287
14288 static tree
14289 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14290 tree lambda_fntype)
14291 {
14292 /* We can get here when processing a member function template,
14293 member class template, or template template parameter. */
14294 tree decl = DECL_TEMPLATE_RESULT (t);
14295 tree in_decl = t;
14296 tree spec;
14297 tree tmpl_args;
14298 tree full_args;
14299 tree r;
14300 hashval_t hash = 0;
14301
14302 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14303 {
14304 /* Template template parameter is treated here. */
14305 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14306 if (new_type == error_mark_node)
14307 r = error_mark_node;
14308 /* If we get a real template back, return it. This can happen in
14309 the context of most_specialized_partial_spec. */
14310 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14311 r = new_type;
14312 else
14313 /* The new TEMPLATE_DECL was built in
14314 reduce_template_parm_level. */
14315 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14316 return r;
14317 }
14318
14319 if (!lambda_fntype)
14320 {
14321 /* We might already have an instance of this template.
14322 The ARGS are for the surrounding class type, so the
14323 full args contain the tsubst'd args for the context,
14324 plus the innermost args from the template decl. */
14325 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14326 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14327 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14328 /* Because this is a template, the arguments will still be
14329 dependent, even after substitution. If
14330 PROCESSING_TEMPLATE_DECL is not set, the dependency
14331 predicates will short-circuit. */
14332 ++processing_template_decl;
14333 full_args = tsubst_template_args (tmpl_args, args,
14334 complain, in_decl);
14335 --processing_template_decl;
14336 if (full_args == error_mark_node)
14337 return error_mark_node;
14338
14339 /* If this is a default template template argument,
14340 tsubst might not have changed anything. */
14341 if (full_args == tmpl_args)
14342 return t;
14343
14344 hash = hash_tmpl_and_args (t, full_args);
14345 spec = retrieve_specialization (t, full_args, hash);
14346 if (spec != NULL_TREE)
14347 {
14348 if (TYPE_P (spec))
14349 /* Type partial instantiations are stored as the type by
14350 lookup_template_class_1, not here as the template. */
14351 spec = CLASSTYPE_TI_TEMPLATE (spec);
14352 return spec;
14353 }
14354 }
14355
14356 /* Make a new template decl. It will be similar to the
14357 original, but will record the current template arguments.
14358 We also create a new function declaration, which is just
14359 like the old one, but points to this new template, rather
14360 than the old one. */
14361 r = copy_decl (t);
14362 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14363 DECL_CHAIN (r) = NULL_TREE;
14364
14365 // Build new template info linking to the original template decl.
14366 if (!lambda_fntype)
14367 {
14368 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14369 SET_DECL_IMPLICIT_INSTANTIATION (r);
14370 }
14371 else
14372 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14373
14374 /* The template parameters for this new template are all the
14375 template parameters for the old template, except the
14376 outermost level of parameters. */
14377 auto tparm_guard = make_temp_override (current_template_parms);
14378 DECL_TEMPLATE_PARMS (r)
14379 = current_template_parms
14380 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14381 complain);
14382
14383 bool class_p = false;
14384 tree inner = decl;
14385 ++processing_template_decl;
14386 if (TREE_CODE (inner) == FUNCTION_DECL)
14387 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14388 else
14389 {
14390 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14391 {
14392 class_p = true;
14393 inner = TREE_TYPE (inner);
14394 }
14395 if (class_p)
14396 inner = tsubst_aggr_type (inner, args, complain,
14397 in_decl, /*entering*/1);
14398 else
14399 inner = tsubst (inner, args, complain, in_decl);
14400 }
14401 --processing_template_decl;
14402 if (inner == error_mark_node)
14403 return error_mark_node;
14404
14405 if (class_p)
14406 {
14407 /* For a partial specialization, we need to keep pointing to
14408 the primary template. */
14409 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14410 CLASSTYPE_TI_TEMPLATE (inner) = r;
14411
14412 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14413 inner = TYPE_MAIN_DECL (inner);
14414 }
14415 else if (lambda_fntype)
14416 {
14417 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14418 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14419 }
14420 else
14421 {
14422 DECL_TI_TEMPLATE (inner) = r;
14423 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14424 }
14425
14426 DECL_TEMPLATE_RESULT (r) = inner;
14427 TREE_TYPE (r) = TREE_TYPE (inner);
14428 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14429
14430 if (modules_p ())
14431 {
14432 /* Propagate module information from the decl. */
14433 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14434 if (DECL_LANG_SPECIFIC (inner))
14435 /* If this is a constrained template, the above tsubst of
14436 inner can find the unconstrained template, which may have
14437 come from an import. This is ok, because we don't
14438 register this instantiation (see below). */
14439 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14440 || (TEMPLATE_PARMS_CONSTRAINTS
14441 (DECL_TEMPLATE_PARMS (t))));
14442 }
14443
14444 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14445 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14446
14447 if (PRIMARY_TEMPLATE_P (t))
14448 DECL_PRIMARY_TEMPLATE (r) = r;
14449
14450 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14451 /* Record this non-type partial instantiation. */
14452 register_specialization (r, t,
14453 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14454 false, hash);
14455
14456 return r;
14457 }
14458
14459 /* True if FN is the op() for a lambda in an uninstantiated template. */
14460
14461 bool
14462 lambda_fn_in_template_p (tree fn)
14463 {
14464 if (!fn || !LAMBDA_FUNCTION_P (fn))
14465 return false;
14466 tree closure = DECL_CONTEXT (fn);
14467 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14468 }
14469
14470 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14471 which the above is true. */
14472
14473 bool
14474 regenerated_lambda_fn_p (tree fn)
14475 {
14476 if (!fn || !LAMBDA_FUNCTION_P (fn))
14477 return false;
14478 tree closure = DECL_CONTEXT (fn);
14479 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14480 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
14481 }
14482
14483 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14484 If T is not a regenerated LAMBDA_EXPR, return T. */
14485
14486 tree
14487 most_general_lambda (tree t)
14488 {
14489 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14490 t = TI_TEMPLATE (ti);
14491 return t;
14492 }
14493
14494 /* Return the set of template arguments used to regenerate the lambda T
14495 from its most general lambda. */
14496
14497 tree
14498 lambda_regenerating_args (tree t)
14499 {
14500 if (LAMBDA_FUNCTION_P (t))
14501 t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
14502 gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
14503 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14504 return TI_ARGS (ti);
14505 else
14506 return NULL_TREE;
14507 }
14508
14509 /* We're instantiating a variable from template function TCTX. Return the
14510 corresponding current enclosing scope. We can match them up using
14511 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14512 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14513 the template definition in regenerate_decl_from_template. */
14514
14515 static tree
14516 enclosing_instantiation_of (tree tctx)
14517 {
14518 tree fn = current_function_decl;
14519
14520 /* We shouldn't ever need to do this for other artificial functions. */
14521 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
14522
14523 for (; fn; fn = decl_function_context (fn))
14524 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
14525 return fn;
14526 gcc_unreachable ();
14527 }
14528
14529 /* Substitute the ARGS into the T, which is a _DECL. Return the
14530 result of the substitution. Issue error and warning messages under
14531 control of COMPLAIN. */
14532
14533 static tree
14534 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14535 {
14536 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14537 location_t saved_loc;
14538 tree r = NULL_TREE;
14539 tree in_decl = t;
14540 hashval_t hash = 0;
14541
14542 /* Set the filename and linenumber to improve error-reporting. */
14543 saved_loc = input_location;
14544 input_location = DECL_SOURCE_LOCATION (t);
14545
14546 switch (TREE_CODE (t))
14547 {
14548 case TEMPLATE_DECL:
14549 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14550 break;
14551
14552 case FUNCTION_DECL:
14553 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14554 break;
14555
14556 case PARM_DECL:
14557 {
14558 tree type = NULL_TREE;
14559 int i, len = 1;
14560 tree expanded_types = NULL_TREE;
14561 tree prev_r = NULL_TREE;
14562 tree first_r = NULL_TREE;
14563
14564 if (DECL_PACK_P (t))
14565 {
14566 /* If there is a local specialization that isn't a
14567 parameter pack, it means that we're doing a "simple"
14568 substitution from inside tsubst_pack_expansion. Just
14569 return the local specialization (which will be a single
14570 parm). */
14571 tree spec = retrieve_local_specialization (t);
14572 if (spec
14573 && TREE_CODE (spec) == PARM_DECL
14574 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14575 RETURN (spec);
14576
14577 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14578 the parameters in this function parameter pack. */
14579 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14580 complain, in_decl);
14581 if (TREE_CODE (expanded_types) == TREE_VEC)
14582 {
14583 len = TREE_VEC_LENGTH (expanded_types);
14584
14585 /* Zero-length parameter packs are boring. Just substitute
14586 into the chain. */
14587 if (len == 0 && !cp_unevaluated_operand)
14588 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14589 TREE_CHAIN (t)));
14590 }
14591 else
14592 {
14593 /* All we did was update the type. Make a note of that. */
14594 type = expanded_types;
14595 expanded_types = NULL_TREE;
14596 }
14597 }
14598
14599 /* Loop through all of the parameters we'll build. When T is
14600 a function parameter pack, LEN is the number of expanded
14601 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14602 r = NULL_TREE;
14603 for (i = 0; i < len; ++i)
14604 {
14605 prev_r = r;
14606 r = copy_node (t);
14607 if (DECL_TEMPLATE_PARM_P (t))
14608 SET_DECL_TEMPLATE_PARM_P (r);
14609
14610 if (expanded_types)
14611 /* We're on the Ith parameter of the function parameter
14612 pack. */
14613 {
14614 /* Get the Ith type. */
14615 type = TREE_VEC_ELT (expanded_types, i);
14616
14617 /* Rename the parameter to include the index. */
14618 DECL_NAME (r)
14619 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14620 }
14621 else if (!type)
14622 /* We're dealing with a normal parameter. */
14623 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14624
14625 type = type_decays_to (type);
14626 TREE_TYPE (r) = type;
14627 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14628
14629 if (DECL_INITIAL (r))
14630 {
14631 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14632 DECL_INITIAL (r) = TREE_TYPE (r);
14633 else
14634 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14635 complain, in_decl);
14636 }
14637
14638 DECL_CONTEXT (r) = NULL_TREE;
14639
14640 if (!DECL_TEMPLATE_PARM_P (r))
14641 DECL_ARG_TYPE (r) = type_passed_as (type);
14642
14643 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14644 args, complain, in_decl))
14645 return error_mark_node;
14646
14647 /* Keep track of the first new parameter we
14648 generate. That's what will be returned to the
14649 caller. */
14650 if (!first_r)
14651 first_r = r;
14652
14653 /* Build a proper chain of parameters when substituting
14654 into a function parameter pack. */
14655 if (prev_r)
14656 DECL_CHAIN (prev_r) = r;
14657 }
14658
14659 /* If cp_unevaluated_operand is set, we're just looking for a
14660 single dummy parameter, so don't keep going. */
14661 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14662 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14663 complain, DECL_CHAIN (t));
14664
14665 /* FIRST_R contains the start of the chain we've built. */
14666 r = first_r;
14667 }
14668 break;
14669
14670 case FIELD_DECL:
14671 {
14672 tree type = NULL_TREE;
14673 tree vec = NULL_TREE;
14674 tree expanded_types = NULL_TREE;
14675 int len = 1;
14676
14677 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14678 {
14679 /* This field is a lambda capture pack. Return a TREE_VEC of
14680 the expanded fields to instantiate_class_template_1. */
14681 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14682 complain, in_decl);
14683 if (TREE_CODE (expanded_types) == TREE_VEC)
14684 {
14685 len = TREE_VEC_LENGTH (expanded_types);
14686 vec = make_tree_vec (len);
14687 }
14688 else
14689 {
14690 /* All we did was update the type. Make a note of that. */
14691 type = expanded_types;
14692 expanded_types = NULL_TREE;
14693 }
14694 }
14695
14696 for (int i = 0; i < len; ++i)
14697 {
14698 r = copy_decl (t);
14699 if (expanded_types)
14700 {
14701 type = TREE_VEC_ELT (expanded_types, i);
14702 DECL_NAME (r)
14703 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14704 }
14705 else if (!type)
14706 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14707
14708 if (type == error_mark_node)
14709 RETURN (error_mark_node);
14710 TREE_TYPE (r) = type;
14711 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14712
14713 if (DECL_C_BIT_FIELD (r))
14714 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14715 number of bits. */
14716 DECL_BIT_FIELD_REPRESENTATIVE (r)
14717 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14718 complain, in_decl,
14719 /*integral_constant_expression_p=*/true);
14720 if (DECL_INITIAL (t))
14721 {
14722 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14723 NSDMI in perform_member_init. Still set DECL_INITIAL
14724 so that we know there is one. */
14725 DECL_INITIAL (r) = void_node;
14726 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14727 retrofit_lang_decl (r);
14728 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14729 }
14730 /* We don't have to set DECL_CONTEXT here; it is set by
14731 finish_member_declaration. */
14732 DECL_CHAIN (r) = NULL_TREE;
14733
14734 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14735 args, complain, in_decl))
14736 return error_mark_node;
14737
14738 if (vec)
14739 TREE_VEC_ELT (vec, i) = r;
14740 }
14741
14742 if (vec)
14743 r = vec;
14744 }
14745 break;
14746
14747 case USING_DECL:
14748 /* We reach here only for member using decls. We also need to check
14749 uses_template_parms because DECL_DEPENDENT_P is not set for a
14750 using-declaration that designates a member of the current
14751 instantiation (c++/53549). */
14752 if (DECL_DEPENDENT_P (t)
14753 || uses_template_parms (USING_DECL_SCOPE (t)))
14754 {
14755 tree scope = USING_DECL_SCOPE (t);
14756 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14757 if (PACK_EXPANSION_P (scope))
14758 {
14759 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14760 int len = TREE_VEC_LENGTH (vec);
14761 r = make_tree_vec (len);
14762 for (int i = 0; i < len; ++i)
14763 {
14764 tree escope = TREE_VEC_ELT (vec, i);
14765 tree elt = do_class_using_decl (escope, name);
14766 if (!elt)
14767 {
14768 r = error_mark_node;
14769 break;
14770 }
14771 else
14772 {
14773 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14774 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14775 }
14776 TREE_VEC_ELT (r, i) = elt;
14777 }
14778 }
14779 else
14780 {
14781 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14782 complain, in_decl);
14783 r = do_class_using_decl (inst_scope, name);
14784 if (!r)
14785 r = error_mark_node;
14786 else
14787 {
14788 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14789 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14790 }
14791 }
14792 }
14793 else
14794 {
14795 r = copy_node (t);
14796 DECL_CHAIN (r) = NULL_TREE;
14797 }
14798 break;
14799
14800 case TYPE_DECL:
14801 case VAR_DECL:
14802 {
14803 tree argvec = NULL_TREE;
14804 tree gen_tmpl = NULL_TREE;
14805 tree tmpl = NULL_TREE;
14806 tree type = NULL_TREE;
14807
14808 if (TREE_TYPE (t) == error_mark_node)
14809 RETURN (error_mark_node);
14810
14811 if (TREE_CODE (t) == TYPE_DECL
14812 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14813 {
14814 /* If this is the canonical decl, we don't have to
14815 mess with instantiations, and often we can't (for
14816 typename, template type parms and such). Note that
14817 TYPE_NAME is not correct for the above test if
14818 we've copied the type for a typedef. */
14819 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14820 if (type == error_mark_node)
14821 RETURN (error_mark_node);
14822 r = TYPE_NAME (type);
14823 break;
14824 }
14825
14826 /* Check to see if we already have the specialization we
14827 need. */
14828 tree spec = NULL_TREE;
14829 bool local_p = false;
14830 tree ctx = DECL_CONTEXT (t);
14831 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14832 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14833 {
14834 local_p = false;
14835 if (DECL_CLASS_SCOPE_P (t))
14836 {
14837 ctx = tsubst_aggr_type (ctx, args,
14838 complain,
14839 in_decl, /*entering_scope=*/1);
14840 /* If CTX is unchanged, then T is in fact the
14841 specialization we want. That situation occurs when
14842 referencing a static data member within in its own
14843 class. We can use pointer equality, rather than
14844 same_type_p, because DECL_CONTEXT is always
14845 canonical... */
14846 if (ctx == DECL_CONTEXT (t)
14847 /* ... unless T is a member template; in which
14848 case our caller can be willing to create a
14849 specialization of that template represented
14850 by T. */
14851 && !(DECL_TI_TEMPLATE (t)
14852 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14853 spec = t;
14854 }
14855
14856 if (!spec)
14857 {
14858 tmpl = DECL_TI_TEMPLATE (t);
14859 gen_tmpl = most_general_template (tmpl);
14860 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14861 if (argvec != error_mark_node)
14862 argvec = (coerce_innermost_template_parms
14863 (DECL_TEMPLATE_PARMS (gen_tmpl),
14864 argvec, t, complain,
14865 /*all*/true, /*defarg*/true));
14866 if (argvec == error_mark_node)
14867 RETURN (error_mark_node);
14868 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14869 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14870 }
14871 }
14872 else
14873 {
14874 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14875 /* Subsequent calls to pushdecl will fill this in. */
14876 ctx = NULL_TREE;
14877 /* A local variable. */
14878 local_p = true;
14879 /* Unless this is a reference to a static variable from an
14880 enclosing function, in which case we need to fill it in now. */
14881 if (TREE_STATIC (t))
14882 {
14883 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14884 if (fn != current_function_decl)
14885 ctx = fn;
14886 }
14887 spec = retrieve_local_specialization (t);
14888 }
14889 /* If we already have the specialization we need, there is
14890 nothing more to do. */
14891 if (spec)
14892 {
14893 r = spec;
14894 break;
14895 }
14896
14897 /* Create a new node for the specialization we need. */
14898 if (type == NULL_TREE)
14899 {
14900 if (is_typedef_decl (t))
14901 type = DECL_ORIGINAL_TYPE (t);
14902 else
14903 type = TREE_TYPE (t);
14904 if (VAR_P (t)
14905 && VAR_HAD_UNKNOWN_BOUND (t)
14906 && type != error_mark_node)
14907 type = strip_array_domain (type);
14908 type = tsubst (type, args, complain, in_decl);
14909 /* Substituting the type might have recursively instantiated this
14910 same alias (c++/86171). */
14911 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14912 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14913 {
14914 r = spec;
14915 break;
14916 }
14917 }
14918 r = copy_decl (t);
14919 if (VAR_P (r))
14920 {
14921 DECL_INITIALIZED_P (r) = 0;
14922 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14923 if (type == error_mark_node)
14924 RETURN (error_mark_node);
14925 if (TREE_CODE (type) == FUNCTION_TYPE)
14926 {
14927 /* It may seem that this case cannot occur, since:
14928
14929 typedef void f();
14930 void g() { f x; }
14931
14932 declares a function, not a variable. However:
14933
14934 typedef void f();
14935 template <typename T> void g() { T t; }
14936 template void g<f>();
14937
14938 is an attempt to declare a variable with function
14939 type. */
14940 error ("variable %qD has function type",
14941 /* R is not yet sufficiently initialized, so we
14942 just use its name. */
14943 DECL_NAME (r));
14944 RETURN (error_mark_node);
14945 }
14946 type = complete_type (type);
14947 /* Wait until cp_finish_decl to set this again, to handle
14948 circular dependency (template/instantiate6.C). */
14949 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14950 type = check_var_type (DECL_NAME (r), type,
14951 DECL_SOURCE_LOCATION (r));
14952 if (DECL_HAS_VALUE_EXPR_P (t))
14953 {
14954 tree ve = DECL_VALUE_EXPR (t);
14955 /* If the DECL_VALUE_EXPR is converted to the declared type,
14956 preserve the identity so that gimplify_type_sizes works. */
14957 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14958 if (nop)
14959 ve = TREE_OPERAND (ve, 0);
14960 ve = tsubst_expr (ve, args, complain, in_decl,
14961 /*constant_expression_p=*/false);
14962 if (REFERENCE_REF_P (ve))
14963 {
14964 gcc_assert (TYPE_REF_P (type));
14965 ve = TREE_OPERAND (ve, 0);
14966 }
14967 if (nop)
14968 ve = build_nop (type, ve);
14969 else if (DECL_LANG_SPECIFIC (t)
14970 && DECL_OMP_PRIVATIZED_MEMBER (t)
14971 && TREE_CODE (ve) == COMPONENT_REF
14972 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14973 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14974 type = TREE_TYPE (ve);
14975 else
14976 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14977 == TYPE_MAIN_VARIANT (type));
14978 SET_DECL_VALUE_EXPR (r, ve);
14979 }
14980 if (CP_DECL_THREAD_LOCAL_P (r)
14981 && !processing_template_decl)
14982 set_decl_tls_model (r, decl_default_tls_model (r));
14983 }
14984 else if (DECL_SELF_REFERENCE_P (t))
14985 SET_DECL_SELF_REFERENCE_P (r);
14986 TREE_TYPE (r) = type;
14987 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14988 DECL_CONTEXT (r) = ctx;
14989 /* Clear out the mangled name and RTL for the instantiation. */
14990 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14991 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14992 SET_DECL_RTL (r, NULL);
14993 set_instantiating_module (r);
14994
14995 /* The initializer must not be expanded until it is required;
14996 see [temp.inst]. */
14997 DECL_INITIAL (r) = NULL_TREE;
14998 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14999 if (VAR_P (r))
15000 {
15001 if (DECL_LANG_SPECIFIC (r))
15002 SET_DECL_DEPENDENT_INIT_P (r, false);
15003
15004 SET_DECL_MODE (r, VOIDmode);
15005
15006 /* Possibly limit visibility based on template args. */
15007 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
15008 if (DECL_VISIBILITY_SPECIFIED (t))
15009 {
15010 DECL_VISIBILITY_SPECIFIED (r) = 0;
15011 DECL_ATTRIBUTES (r)
15012 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15013 }
15014 determine_visibility (r);
15015 }
15016
15017 if (!local_p)
15018 {
15019 /* A static data member declaration is always marked
15020 external when it is declared in-class, even if an
15021 initializer is present. We mimic the non-template
15022 processing here. */
15023 DECL_EXTERNAL (r) = 1;
15024 if (DECL_NAMESPACE_SCOPE_P (t))
15025 DECL_NOT_REALLY_EXTERN (r) = 1;
15026
15027 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
15028 SET_DECL_IMPLICIT_INSTANTIATION (r);
15029 if (!error_operand_p (r) || (complain & tf_error))
15030 register_specialization (r, gen_tmpl, argvec, false, hash);
15031 }
15032 else
15033 {
15034 if (DECL_LANG_SPECIFIC (r))
15035 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15036 if (!cp_unevaluated_operand)
15037 register_local_specialization (r, t);
15038 }
15039
15040 DECL_CHAIN (r) = NULL_TREE;
15041
15042 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
15043 /*flags=*/0,
15044 args, complain, in_decl))
15045 return error_mark_node;
15046
15047 /* Preserve a typedef that names a type. */
15048 if (is_typedef_decl (r) && type != error_mark_node)
15049 {
15050 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
15051 set_underlying_type (r);
15052 }
15053
15054 layout_decl (r, 0);
15055 }
15056 break;
15057
15058 default:
15059 gcc_unreachable ();
15060 }
15061 #undef RETURN
15062
15063 out:
15064 /* Restore the file and line information. */
15065 input_location = saved_loc;
15066
15067 return r;
15068 }
15069
15070 /* Substitute into the complete parameter type list PARMS. */
15071
15072 tree
15073 tsubst_function_parms (tree parms,
15074 tree args,
15075 tsubst_flags_t complain,
15076 tree in_decl)
15077 {
15078 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
15079 }
15080
15081 /* Substitute into the ARG_TYPES of a function type.
15082 If END is a TREE_CHAIN, leave it and any following types
15083 un-substituted. */
15084
15085 static tree
15086 tsubst_arg_types (tree arg_types,
15087 tree args,
15088 tree end,
15089 tsubst_flags_t complain,
15090 tree in_decl)
15091 {
15092 tree type = NULL_TREE;
15093 int len = 1;
15094 tree expanded_args = NULL_TREE;
15095
15096 if (!arg_types || arg_types == void_list_node || arg_types == end)
15097 return arg_types;
15098
15099 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
15100 {
15101 /* For a pack expansion, perform substitution on the
15102 entire expression. Later on, we'll handle the arguments
15103 one-by-one. */
15104 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
15105 args, complain, in_decl);
15106
15107 if (TREE_CODE (expanded_args) == TREE_VEC)
15108 /* So that we'll spin through the parameters, one by one. */
15109 len = TREE_VEC_LENGTH (expanded_args);
15110 else
15111 {
15112 /* We only partially substituted into the parameter
15113 pack. Our type is TYPE_PACK_EXPANSION. */
15114 type = expanded_args;
15115 expanded_args = NULL_TREE;
15116 }
15117 }
15118 else
15119 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
15120
15121 /* Check if a substituted type is erroneous before substituting into
15122 the rest of the chain. */
15123 for (int i = 0; i < len; i++)
15124 {
15125 if (expanded_args)
15126 type = TREE_VEC_ELT (expanded_args, i);
15127
15128 if (type == error_mark_node)
15129 return error_mark_node;
15130 if (VOID_TYPE_P (type))
15131 {
15132 if (complain & tf_error)
15133 {
15134 error ("invalid parameter type %qT", type);
15135 if (in_decl)
15136 error ("in declaration %q+D", in_decl);
15137 }
15138 return error_mark_node;
15139 }
15140 }
15141
15142 /* We do not substitute into default arguments here. The standard
15143 mandates that they be instantiated only when needed, which is
15144 done in build_over_call. */
15145 tree default_arg = TREE_PURPOSE (arg_types);
15146
15147 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15148 since the new op() won't have any associated template arguments for us
15149 to refer to later. */
15150 if (lambda_fn_in_template_p (in_decl))
15151 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
15152 false/*fn*/, false/*constexpr*/);
15153
15154 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
15155 args, end, complain, in_decl);
15156 if (remaining_arg_types == error_mark_node)
15157 return error_mark_node;
15158
15159 for (int i = len-1; i >= 0; i--)
15160 {
15161 if (expanded_args)
15162 type = TREE_VEC_ELT (expanded_args, i);
15163
15164 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15165 top-level qualifiers as required. */
15166 type = cv_unqualified (type_decays_to (type));
15167
15168 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15169 {
15170 /* We've instantiated a template before its default arguments
15171 have been parsed. This can happen for a nested template
15172 class, and is not an error unless we require the default
15173 argument in a call of this function. */
15174 remaining_arg_types
15175 = tree_cons (default_arg, type, remaining_arg_types);
15176 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15177 remaining_arg_types);
15178 }
15179 else
15180 remaining_arg_types
15181 = hash_tree_cons (default_arg, type, remaining_arg_types);
15182 }
15183
15184 return remaining_arg_types;
15185 }
15186
15187 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15188 *not* handle the exception-specification for FNTYPE, because the
15189 initial substitution of explicitly provided template parameters
15190 during argument deduction forbids substitution into the
15191 exception-specification:
15192
15193 [temp.deduct]
15194
15195 All references in the function type of the function template to the
15196 corresponding template parameters are replaced by the specified tem-
15197 plate argument values. If a substitution in a template parameter or
15198 in the function type of the function template results in an invalid
15199 type, type deduction fails. [Note: The equivalent substitution in
15200 exception specifications is done only when the function is instanti-
15201 ated, at which point a program is ill-formed if the substitution
15202 results in an invalid type.] */
15203
15204 static tree
15205 tsubst_function_type (tree t,
15206 tree args,
15207 tsubst_flags_t complain,
15208 tree in_decl)
15209 {
15210 tree return_type;
15211 tree arg_types = NULL_TREE;
15212
15213 /* The TYPE_CONTEXT is not used for function/method types. */
15214 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15215
15216 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15217 failure. */
15218 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15219
15220 if (late_return_type_p)
15221 {
15222 /* Substitute the argument types. */
15223 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15224 complain, in_decl);
15225 if (arg_types == error_mark_node)
15226 return error_mark_node;
15227
15228 tree save_ccp = current_class_ptr;
15229 tree save_ccr = current_class_ref;
15230 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15231 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15232 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15233 if (do_inject)
15234 {
15235 /* DR 1207: 'this' is in scope in the trailing return type. */
15236 inject_this_parameter (this_type, cp_type_quals (this_type));
15237 }
15238
15239 /* Substitute the return type. */
15240 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15241
15242 if (do_inject)
15243 {
15244 current_class_ptr = save_ccp;
15245 current_class_ref = save_ccr;
15246 }
15247 }
15248 else
15249 /* Substitute the return type. */
15250 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15251
15252 if (return_type == error_mark_node)
15253 return error_mark_node;
15254 /* DR 486 clarifies that creation of a function type with an
15255 invalid return type is a deduction failure. */
15256 if (TREE_CODE (return_type) == ARRAY_TYPE
15257 || TREE_CODE (return_type) == FUNCTION_TYPE)
15258 {
15259 if (complain & tf_error)
15260 {
15261 if (TREE_CODE (return_type) == ARRAY_TYPE)
15262 error ("function returning an array");
15263 else
15264 error ("function returning a function");
15265 }
15266 return error_mark_node;
15267 }
15268
15269 if (!late_return_type_p)
15270 {
15271 /* Substitute the argument types. */
15272 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15273 complain, in_decl);
15274 if (arg_types == error_mark_node)
15275 return error_mark_node;
15276 }
15277
15278 /* Construct a new type node and return it. */
15279 return rebuild_function_or_method_type (t, return_type, arg_types,
15280 /*raises=*/NULL_TREE, complain);
15281 }
15282
15283 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15284 ARGS into that specification, and return the substituted
15285 specification. If there is no specification, return NULL_TREE. */
15286
15287 static tree
15288 tsubst_exception_specification (tree fntype,
15289 tree args,
15290 tsubst_flags_t complain,
15291 tree in_decl,
15292 bool defer_ok)
15293 {
15294 tree specs;
15295 tree new_specs;
15296
15297 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15298 new_specs = NULL_TREE;
15299 if (specs && TREE_PURPOSE (specs))
15300 {
15301 /* A noexcept-specifier. */
15302 tree expr = TREE_PURPOSE (specs);
15303 if (TREE_CODE (expr) == INTEGER_CST)
15304 new_specs = expr;
15305 else if (defer_ok)
15306 {
15307 /* Defer instantiation of noexcept-specifiers to avoid
15308 excessive instantiations (c++/49107). */
15309 new_specs = make_node (DEFERRED_NOEXCEPT);
15310 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15311 {
15312 /* We already partially instantiated this member template,
15313 so combine the new args with the old. */
15314 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15315 = DEFERRED_NOEXCEPT_PATTERN (expr);
15316 DEFERRED_NOEXCEPT_ARGS (new_specs)
15317 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15318 }
15319 else
15320 {
15321 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15322 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15323 }
15324 }
15325 else
15326 {
15327 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15328 {
15329 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15330 args);
15331 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15332 }
15333 new_specs = tsubst_copy_and_build
15334 (expr, args, complain, in_decl, /*function_p=*/false,
15335 /*integral_constant_expression_p=*/true);
15336 }
15337 new_specs = build_noexcept_spec (new_specs, complain);
15338 /* We've instantiated a template before a noexcept-specifier
15339 contained therein has been parsed. This can happen for
15340 a nested template class:
15341
15342 struct S {
15343 template<typename> struct B { B() noexcept(...); };
15344 struct A : B<int> { ... use B() ... };
15345 };
15346
15347 where completing B<int> will trigger instantiating the
15348 noexcept, even though we only parse it at the end of S. */
15349 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
15350 {
15351 gcc_checking_assert (defer_ok);
15352 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
15353 }
15354 }
15355 else if (specs)
15356 {
15357 if (! TREE_VALUE (specs))
15358 new_specs = specs;
15359 else
15360 while (specs)
15361 {
15362 tree spec;
15363 int i, len = 1;
15364 tree expanded_specs = NULL_TREE;
15365
15366 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15367 {
15368 /* Expand the pack expansion type. */
15369 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15370 args, complain,
15371 in_decl);
15372
15373 if (expanded_specs == error_mark_node)
15374 return error_mark_node;
15375 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15376 len = TREE_VEC_LENGTH (expanded_specs);
15377 else
15378 {
15379 /* We're substituting into a member template, so
15380 we got a TYPE_PACK_EXPANSION back. Add that
15381 expansion and move on. */
15382 gcc_assert (TREE_CODE (expanded_specs)
15383 == TYPE_PACK_EXPANSION);
15384 new_specs = add_exception_specifier (new_specs,
15385 expanded_specs,
15386 complain);
15387 specs = TREE_CHAIN (specs);
15388 continue;
15389 }
15390 }
15391
15392 for (i = 0; i < len; ++i)
15393 {
15394 if (expanded_specs)
15395 spec = TREE_VEC_ELT (expanded_specs, i);
15396 else
15397 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15398 if (spec == error_mark_node)
15399 return spec;
15400 new_specs = add_exception_specifier (new_specs, spec,
15401 complain);
15402 }
15403
15404 specs = TREE_CHAIN (specs);
15405 }
15406 }
15407 return new_specs;
15408 }
15409
15410 /* Substitute through a TREE_LIST of types or expressions, handling pack
15411 expansions. */
15412
15413 tree
15414 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15415 {
15416 if (t == void_list_node)
15417 return t;
15418
15419 tree purpose = TREE_PURPOSE (t);
15420 tree purposevec = NULL_TREE;
15421 if (!purpose)
15422 ;
15423 else if (PACK_EXPANSION_P (purpose))
15424 {
15425 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15426 if (TREE_CODE (purpose) == TREE_VEC)
15427 purposevec = purpose;
15428 }
15429 else if (TYPE_P (purpose))
15430 purpose = tsubst (purpose, args, complain, in_decl);
15431 else
15432 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15433 if (purpose == error_mark_node || purposevec == error_mark_node)
15434 return error_mark_node;
15435
15436 tree value = TREE_VALUE (t);
15437 tree valuevec = NULL_TREE;
15438 if (!value)
15439 ;
15440 else if (PACK_EXPANSION_P (value))
15441 {
15442 value = tsubst_pack_expansion (value, args, complain, in_decl);
15443 if (TREE_CODE (value) == TREE_VEC)
15444 valuevec = value;
15445 }
15446 else if (TYPE_P (value))
15447 value = tsubst (value, args, complain, in_decl);
15448 else
15449 value = tsubst_copy_and_build (value, args, complain, in_decl);
15450 if (value == error_mark_node || valuevec == error_mark_node)
15451 return error_mark_node;
15452
15453 tree chain = TREE_CHAIN (t);
15454 if (!chain)
15455 ;
15456 else if (TREE_CODE (chain) == TREE_LIST)
15457 chain = tsubst_tree_list (chain, args, complain, in_decl);
15458 else if (TYPE_P (chain))
15459 chain = tsubst (chain, args, complain, in_decl);
15460 else
15461 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15462 if (chain == error_mark_node)
15463 return error_mark_node;
15464
15465 if (purpose == TREE_PURPOSE (t)
15466 && value == TREE_VALUE (t)
15467 && chain == TREE_CHAIN (t))
15468 return t;
15469
15470 int len;
15471 /* Determine the number of arguments. */
15472 if (purposevec)
15473 {
15474 len = TREE_VEC_LENGTH (purposevec);
15475 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15476 }
15477 else if (valuevec)
15478 len = TREE_VEC_LENGTH (valuevec);
15479 else
15480 len = 1;
15481
15482 for (int i = len; i-- > 0; )
15483 {
15484 if (purposevec)
15485 purpose = TREE_VEC_ELT (purposevec, i);
15486 if (valuevec)
15487 value = TREE_VEC_ELT (valuevec, i);
15488
15489 if (value && TYPE_P (value))
15490 chain = hash_tree_cons (purpose, value, chain);
15491 else
15492 chain = tree_cons (purpose, value, chain);
15493 }
15494
15495 return chain;
15496 }
15497
15498 /* Take the tree structure T and replace template parameters used
15499 therein with the argument vector ARGS. IN_DECL is an associated
15500 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15501 Issue error and warning messages under control of COMPLAIN. Note
15502 that we must be relatively non-tolerant of extensions here, in
15503 order to preserve conformance; if we allow substitutions that
15504 should not be allowed, we may allow argument deductions that should
15505 not succeed, and therefore report ambiguous overload situations
15506 where there are none. In theory, we could allow the substitution,
15507 but indicate that it should have failed, and allow our caller to
15508 make sure that the right thing happens, but we don't try to do this
15509 yet.
15510
15511 This function is used for dealing with types, decls and the like;
15512 for expressions, use tsubst_expr or tsubst_copy. */
15513
15514 tree
15515 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15516 {
15517 enum tree_code code;
15518 tree type, r = NULL_TREE;
15519
15520 if (t == NULL_TREE || t == error_mark_node
15521 || t == integer_type_node
15522 || t == void_type_node
15523 || t == char_type_node
15524 || t == unknown_type_node
15525 || TREE_CODE (t) == NAMESPACE_DECL
15526 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15527 return t;
15528
15529 if (DECL_P (t))
15530 return tsubst_decl (t, args, complain);
15531
15532 if (args == NULL_TREE)
15533 return t;
15534
15535 code = TREE_CODE (t);
15536
15537 gcc_assert (code != IDENTIFIER_NODE);
15538 type = TREE_TYPE (t);
15539
15540 gcc_assert (type != unknown_type_node);
15541
15542 /* Reuse typedefs. We need to do this to handle dependent attributes,
15543 such as attribute aligned. */
15544 if (TYPE_P (t)
15545 && typedef_variant_p (t))
15546 {
15547 tree decl = TYPE_NAME (t);
15548
15549 if (alias_template_specialization_p (t, nt_opaque))
15550 {
15551 /* DECL represents an alias template and we want to
15552 instantiate it. */
15553 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15554 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15555 r = instantiate_alias_template (tmpl, gen_args, complain);
15556 }
15557 else if (DECL_CLASS_SCOPE_P (decl)
15558 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15559 && uses_template_parms (DECL_CONTEXT (decl)))
15560 {
15561 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15562 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15563 r = retrieve_specialization (tmpl, gen_args, 0);
15564 }
15565 else if (DECL_FUNCTION_SCOPE_P (decl)
15566 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15567 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15568 r = retrieve_local_specialization (decl);
15569 else
15570 /* The typedef is from a non-template context. */
15571 return t;
15572
15573 if (r)
15574 {
15575 r = TREE_TYPE (r);
15576 r = cp_build_qualified_type_real
15577 (r, cp_type_quals (t) | cp_type_quals (r),
15578 complain | tf_ignore_bad_quals);
15579 return r;
15580 }
15581 else
15582 {
15583 /* We don't have an instantiation yet, so drop the typedef. */
15584 int quals = cp_type_quals (t);
15585 t = DECL_ORIGINAL_TYPE (decl);
15586 t = cp_build_qualified_type_real (t, quals,
15587 complain | tf_ignore_bad_quals);
15588 }
15589 }
15590
15591 bool fndecl_type = (complain & tf_fndecl_type);
15592 complain &= ~tf_fndecl_type;
15593
15594 if (type
15595 && code != TYPENAME_TYPE
15596 && code != TEMPLATE_TYPE_PARM
15597 && code != TEMPLATE_PARM_INDEX
15598 && code != IDENTIFIER_NODE
15599 && code != FUNCTION_TYPE
15600 && code != METHOD_TYPE)
15601 type = tsubst (type, args, complain, in_decl);
15602 if (type == error_mark_node)
15603 return error_mark_node;
15604
15605 switch (code)
15606 {
15607 case RECORD_TYPE:
15608 case UNION_TYPE:
15609 case ENUMERAL_TYPE:
15610 return tsubst_aggr_type (t, args, complain, in_decl,
15611 /*entering_scope=*/0);
15612
15613 case ERROR_MARK:
15614 case IDENTIFIER_NODE:
15615 case VOID_TYPE:
15616 case OPAQUE_TYPE:
15617 case REAL_TYPE:
15618 case COMPLEX_TYPE:
15619 case VECTOR_TYPE:
15620 case BOOLEAN_TYPE:
15621 case NULLPTR_TYPE:
15622 case LANG_TYPE:
15623 return t;
15624
15625 case INTEGER_TYPE:
15626 if (t == integer_type_node)
15627 return t;
15628
15629 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15630 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15631 return t;
15632
15633 {
15634 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15635
15636 max = tsubst_expr (omax, args, complain, in_decl,
15637 /*integral_constant_expression_p=*/false);
15638
15639 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15640 needed. */
15641 if (TREE_CODE (max) == NOP_EXPR
15642 && TREE_SIDE_EFFECTS (omax)
15643 && !TREE_TYPE (max))
15644 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15645
15646 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15647 with TREE_SIDE_EFFECTS that indicates this is not an integral
15648 constant expression. */
15649 if (processing_template_decl
15650 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15651 {
15652 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15653 TREE_SIDE_EFFECTS (max) = 1;
15654 }
15655
15656 return compute_array_index_type (NULL_TREE, max, complain);
15657 }
15658
15659 case TEMPLATE_TYPE_PARM:
15660 if (template_placeholder_p (t))
15661 {
15662 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
15663 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
15664 if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
15665 tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
15666
15667 if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
15668 return make_template_placeholder (tmpl);
15669 else
15670 return t;
15671 }
15672 /* Fall through. */
15673 case TEMPLATE_TEMPLATE_PARM:
15674 case BOUND_TEMPLATE_TEMPLATE_PARM:
15675 case TEMPLATE_PARM_INDEX:
15676 {
15677 int idx;
15678 int level;
15679 int levels;
15680 tree arg = NULL_TREE;
15681
15682 r = NULL_TREE;
15683
15684 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15685 template_parm_level_and_index (t, &level, &idx);
15686
15687 levels = TMPL_ARGS_DEPTH (args);
15688 if (level <= levels
15689 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15690 {
15691 arg = TMPL_ARG (args, level, idx);
15692
15693 /* See through ARGUMENT_PACK_SELECT arguments. */
15694 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15695 arg = argument_pack_select_arg (arg);
15696 }
15697
15698 if (arg == error_mark_node)
15699 return error_mark_node;
15700 else if (arg != NULL_TREE)
15701 {
15702 if (ARGUMENT_PACK_P (arg))
15703 /* If ARG is an argument pack, we don't actually want to
15704 perform a substitution here, because substitutions
15705 for argument packs are only done
15706 element-by-element. We can get to this point when
15707 substituting the type of a non-type template
15708 parameter pack, when that type actually contains
15709 template parameter packs from an outer template, e.g.,
15710
15711 template<typename... Types> struct A {
15712 template<Types... Values> struct B { };
15713 }; */
15714 return t;
15715
15716 if (code == TEMPLATE_TYPE_PARM)
15717 {
15718 int quals;
15719
15720 /* When building concept checks for the purpose of
15721 deducing placeholders, we can end up with wildcards
15722 where types are expected. Adjust this to the deduced
15723 value. */
15724 if (TREE_CODE (arg) == WILDCARD_DECL)
15725 arg = TREE_TYPE (TREE_TYPE (arg));
15726
15727 gcc_assert (TYPE_P (arg));
15728
15729 quals = cp_type_quals (arg) | cp_type_quals (t);
15730
15731 return cp_build_qualified_type_real
15732 (arg, quals, complain | tf_ignore_bad_quals);
15733 }
15734 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15735 {
15736 /* We are processing a type constructed from a
15737 template template parameter. */
15738 tree argvec = tsubst (TYPE_TI_ARGS (t),
15739 args, complain, in_decl);
15740 if (argvec == error_mark_node)
15741 return error_mark_node;
15742
15743 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15744 || TREE_CODE (arg) == TEMPLATE_DECL
15745 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15746
15747 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15748 /* Consider this code:
15749
15750 template <template <class> class Template>
15751 struct Internal {
15752 template <class Arg> using Bind = Template<Arg>;
15753 };
15754
15755 template <template <class> class Template, class Arg>
15756 using Instantiate = Template<Arg>; //#0
15757
15758 template <template <class> class Template,
15759 class Argument>
15760 using Bind =
15761 Instantiate<Internal<Template>::template Bind,
15762 Argument>; //#1
15763
15764 When #1 is parsed, the
15765 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15766 parameter `Template' in #0 matches the
15767 UNBOUND_CLASS_TEMPLATE representing the argument
15768 `Internal<Template>::template Bind'; We then want
15769 to assemble the type `Bind<Argument>' that can't
15770 be fully created right now, because
15771 `Internal<Template>' not being complete, the Bind
15772 template cannot be looked up in that context. So
15773 we need to "store" `Bind<Argument>' for later
15774 when the context of Bind becomes complete. Let's
15775 store that in a TYPENAME_TYPE. */
15776 return make_typename_type (TYPE_CONTEXT (arg),
15777 build_nt (TEMPLATE_ID_EXPR,
15778 TYPE_IDENTIFIER (arg),
15779 argvec),
15780 typename_type,
15781 complain);
15782
15783 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15784 are resolving nested-types in the signature of a
15785 member function templates. Otherwise ARG is a
15786 TEMPLATE_DECL and is the real template to be
15787 instantiated. */
15788 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15789 arg = TYPE_NAME (arg);
15790
15791 r = lookup_template_class (arg,
15792 argvec, in_decl,
15793 DECL_CONTEXT (arg),
15794 /*entering_scope=*/0,
15795 complain);
15796 return cp_build_qualified_type_real
15797 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15798 }
15799 else if (code == TEMPLATE_TEMPLATE_PARM)
15800 return arg;
15801 else
15802 /* TEMPLATE_PARM_INDEX. */
15803 return convert_from_reference (unshare_expr (arg));
15804 }
15805
15806 if (level == 1)
15807 /* This can happen during the attempted tsubst'ing in
15808 unify. This means that we don't yet have any information
15809 about the template parameter in question. */
15810 return t;
15811
15812 /* Early in template argument deduction substitution, we don't
15813 want to reduce the level of 'auto', or it will be confused
15814 with a normal template parm in subsequent deduction.
15815 Similarly, don't reduce the level of template parameters to
15816 avoid mismatches when deducing their types. */
15817 if (complain & tf_partial)
15818 return t;
15819
15820 /* If we get here, we must have been looking at a parm for a
15821 more deeply nested template. Make a new version of this
15822 template parameter, but with a lower level. */
15823 switch (code)
15824 {
15825 case TEMPLATE_TYPE_PARM:
15826 case TEMPLATE_TEMPLATE_PARM:
15827 case BOUND_TEMPLATE_TEMPLATE_PARM:
15828 if (cp_type_quals (t))
15829 {
15830 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15831 r = cp_build_qualified_type_real
15832 (r, cp_type_quals (t),
15833 complain | (code == TEMPLATE_TYPE_PARM
15834 ? tf_ignore_bad_quals : 0));
15835 }
15836 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15837 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15838 && (r = (TEMPLATE_PARM_DESCENDANTS
15839 (TEMPLATE_TYPE_PARM_INDEX (t))))
15840 && (r = TREE_TYPE (r))
15841 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r))
15842 /* Break infinite recursion when substituting the constraints
15843 of a constrained placeholder. */;
15844 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15845 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15846 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15847 r = TEMPLATE_PARM_DESCENDANTS (arg))
15848 && (TEMPLATE_PARM_LEVEL (r)
15849 == TEMPLATE_PARM_LEVEL (arg) - levels))
15850 /* Cache the simple case of lowering a type parameter. */
15851 r = TREE_TYPE (r);
15852 else
15853 {
15854 r = copy_type (t);
15855 TEMPLATE_TYPE_PARM_INDEX (r)
15856 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15857 r, levels, args, complain);
15858 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15859 TYPE_MAIN_VARIANT (r) = r;
15860 TYPE_POINTER_TO (r) = NULL_TREE;
15861 TYPE_REFERENCE_TO (r) = NULL_TREE;
15862
15863 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15864 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
15865 /* Propagate constraints on placeholders since they are
15866 only instantiated during satisfaction. */
15867 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
15868
15869 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15870 /* We have reduced the level of the template
15871 template parameter, but not the levels of its
15872 template parameters, so canonical_type_parameter
15873 will not be able to find the canonical template
15874 template parameter for this level. Thus, we
15875 require structural equality checking to compare
15876 TEMPLATE_TEMPLATE_PARMs. */
15877 SET_TYPE_STRUCTURAL_EQUALITY (r);
15878 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15879 SET_TYPE_STRUCTURAL_EQUALITY (r);
15880 else
15881 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15882
15883 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15884 {
15885 tree tinfo = TYPE_TEMPLATE_INFO (t);
15886 /* We might need to substitute into the types of non-type
15887 template parameters. */
15888 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15889 complain, in_decl);
15890 if (tmpl == error_mark_node)
15891 return error_mark_node;
15892 tree argvec = tsubst (TI_ARGS (tinfo), args,
15893 complain, in_decl);
15894 if (argvec == error_mark_node)
15895 return error_mark_node;
15896
15897 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15898 = build_template_info (tmpl, argvec);
15899 }
15900 }
15901 break;
15902
15903 case TEMPLATE_PARM_INDEX:
15904 /* OK, now substitute the type of the non-type parameter. We
15905 couldn't do it earlier because it might be an auto parameter,
15906 and we wouldn't need to if we had an argument. */
15907 type = tsubst (type, args, complain, in_decl);
15908 if (type == error_mark_node)
15909 return error_mark_node;
15910 r = reduce_template_parm_level (t, type, levels, args, complain);
15911 break;
15912
15913 default:
15914 gcc_unreachable ();
15915 }
15916
15917 return r;
15918 }
15919
15920 case TREE_LIST:
15921 return tsubst_tree_list (t, args, complain, in_decl);
15922
15923 case TREE_BINFO:
15924 /* We should never be tsubsting a binfo. */
15925 gcc_unreachable ();
15926
15927 case TREE_VEC:
15928 /* A vector of template arguments. */
15929 gcc_assert (!type);
15930 return tsubst_template_args (t, args, complain, in_decl);
15931
15932 case POINTER_TYPE:
15933 case REFERENCE_TYPE:
15934 {
15935 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15936 return t;
15937
15938 /* [temp.deduct]
15939
15940 Type deduction may fail for any of the following
15941 reasons:
15942
15943 -- Attempting to create a pointer to reference type.
15944 -- Attempting to create a reference to a reference type or
15945 a reference to void.
15946
15947 Core issue 106 says that creating a reference to a reference
15948 during instantiation is no longer a cause for failure. We
15949 only enforce this check in strict C++98 mode. */
15950 if ((TYPE_REF_P (type)
15951 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15952 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15953 {
15954 static location_t last_loc;
15955
15956 /* We keep track of the last time we issued this error
15957 message to avoid spewing a ton of messages during a
15958 single bad template instantiation. */
15959 if (complain & tf_error
15960 && last_loc != input_location)
15961 {
15962 if (VOID_TYPE_P (type))
15963 error ("forming reference to void");
15964 else if (code == POINTER_TYPE)
15965 error ("forming pointer to reference type %qT", type);
15966 else
15967 error ("forming reference to reference type %qT", type);
15968 last_loc = input_location;
15969 }
15970
15971 return error_mark_node;
15972 }
15973 else if (TREE_CODE (type) == FUNCTION_TYPE
15974 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15975 || type_memfn_rqual (type) != REF_QUAL_NONE))
15976 {
15977 if (complain & tf_error)
15978 {
15979 if (code == POINTER_TYPE)
15980 error ("forming pointer to qualified function type %qT",
15981 type);
15982 else
15983 error ("forming reference to qualified function type %qT",
15984 type);
15985 }
15986 return error_mark_node;
15987 }
15988 else if (code == POINTER_TYPE)
15989 {
15990 r = build_pointer_type (type);
15991 if (TREE_CODE (type) == METHOD_TYPE)
15992 r = build_ptrmemfunc_type (r);
15993 }
15994 else if (TYPE_REF_P (type))
15995 /* In C++0x, during template argument substitution, when there is an
15996 attempt to create a reference to a reference type, reference
15997 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15998
15999 "If a template-argument for a template-parameter T names a type
16000 that is a reference to a type A, an attempt to create the type
16001 'lvalue reference to cv T' creates the type 'lvalue reference to
16002 A,' while an attempt to create the type type rvalue reference to
16003 cv T' creates the type T"
16004 */
16005 r = cp_build_reference_type
16006 (TREE_TYPE (type),
16007 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
16008 else
16009 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
16010 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
16011
16012 if (r != error_mark_node)
16013 /* Will this ever be needed for TYPE_..._TO values? */
16014 layout_type (r);
16015
16016 return r;
16017 }
16018 case OFFSET_TYPE:
16019 {
16020 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
16021 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
16022 {
16023 /* [temp.deduct]
16024
16025 Type deduction may fail for any of the following
16026 reasons:
16027
16028 -- Attempting to create "pointer to member of T" when T
16029 is not a class type. */
16030 if (complain & tf_error)
16031 error ("creating pointer to member of non-class type %qT", r);
16032 return error_mark_node;
16033 }
16034 if (TYPE_REF_P (type))
16035 {
16036 if (complain & tf_error)
16037 error ("creating pointer to member reference type %qT", type);
16038 return error_mark_node;
16039 }
16040 if (VOID_TYPE_P (type))
16041 {
16042 if (complain & tf_error)
16043 error ("creating pointer to member of type void");
16044 return error_mark_node;
16045 }
16046 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
16047 if (TREE_CODE (type) == FUNCTION_TYPE)
16048 {
16049 /* The type of the implicit object parameter gets its
16050 cv-qualifiers from the FUNCTION_TYPE. */
16051 tree memptr;
16052 tree method_type
16053 = build_memfn_type (type, r, type_memfn_quals (type),
16054 type_memfn_rqual (type));
16055 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
16056 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
16057 complain);
16058 }
16059 else
16060 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
16061 cp_type_quals (t),
16062 complain);
16063 }
16064 case FUNCTION_TYPE:
16065 case METHOD_TYPE:
16066 {
16067 tree fntype;
16068 tree specs;
16069 fntype = tsubst_function_type (t, args, complain, in_decl);
16070 if (fntype == error_mark_node)
16071 return error_mark_node;
16072
16073 /* Substitute the exception specification. */
16074 specs = tsubst_exception_specification (t, args, complain, in_decl,
16075 /*defer_ok*/fndecl_type);
16076 if (specs == error_mark_node)
16077 return error_mark_node;
16078 if (specs)
16079 fntype = build_exception_variant (fntype, specs);
16080 return fntype;
16081 }
16082 case ARRAY_TYPE:
16083 {
16084 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
16085 if (domain == error_mark_node)
16086 return error_mark_node;
16087
16088 /* As an optimization, we avoid regenerating the array type if
16089 it will obviously be the same as T. */
16090 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
16091 return t;
16092
16093 /* These checks should match the ones in create_array_type_for_decl.
16094
16095 [temp.deduct]
16096
16097 The deduction may fail for any of the following reasons:
16098
16099 -- Attempting to create an array with an element type that
16100 is void, a function type, or a reference type, or [DR337]
16101 an abstract class type. */
16102 if (VOID_TYPE_P (type)
16103 || TREE_CODE (type) == FUNCTION_TYPE
16104 || (TREE_CODE (type) == ARRAY_TYPE
16105 && TYPE_DOMAIN (type) == NULL_TREE)
16106 || TYPE_REF_P (type))
16107 {
16108 if (complain & tf_error)
16109 error ("creating array of %qT", type);
16110 return error_mark_node;
16111 }
16112
16113 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
16114 !(complain & tf_error)))
16115 return error_mark_node;
16116
16117 r = build_cplus_array_type (type, domain);
16118
16119 if (!valid_array_size_p (input_location, r, in_decl,
16120 (complain & tf_error)))
16121 return error_mark_node;
16122
16123 if (TYPE_USER_ALIGN (t))
16124 {
16125 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
16126 TYPE_USER_ALIGN (r) = 1;
16127 }
16128
16129 return r;
16130 }
16131
16132 case TYPENAME_TYPE:
16133 {
16134 tree ctx = TYPE_CONTEXT (t);
16135 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
16136 {
16137 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
16138 if (ctx == error_mark_node
16139 || TREE_VEC_LENGTH (ctx) > 1)
16140 return error_mark_node;
16141 if (TREE_VEC_LENGTH (ctx) == 0)
16142 {
16143 if (complain & tf_error)
16144 error ("%qD is instantiated for an empty pack",
16145 TYPENAME_TYPE_FULLNAME (t));
16146 return error_mark_node;
16147 }
16148 ctx = TREE_VEC_ELT (ctx, 0);
16149 }
16150 else
16151 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
16152 /*entering_scope=*/1);
16153 if (ctx == error_mark_node)
16154 return error_mark_node;
16155
16156 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
16157 complain, in_decl);
16158 if (f == error_mark_node)
16159 return error_mark_node;
16160
16161 if (!MAYBE_CLASS_TYPE_P (ctx))
16162 {
16163 if (complain & tf_error)
16164 error ("%qT is not a class, struct, or union type", ctx);
16165 return error_mark_node;
16166 }
16167 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16168 {
16169 /* Normally, make_typename_type does not require that the CTX
16170 have complete type in order to allow things like:
16171
16172 template <class T> struct S { typename S<T>::X Y; };
16173
16174 But, such constructs have already been resolved by this
16175 point, so here CTX really should have complete type, unless
16176 it's a partial instantiation. */
16177 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16178 return error_mark_node;
16179 }
16180
16181 f = make_typename_type (ctx, f, typename_type,
16182 complain | tf_keep_type_decl);
16183 if (f == error_mark_node)
16184 return f;
16185 if (TREE_CODE (f) == TYPE_DECL)
16186 {
16187 complain |= tf_ignore_bad_quals;
16188 f = TREE_TYPE (f);
16189 }
16190
16191 if (TREE_CODE (f) != TYPENAME_TYPE)
16192 {
16193 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16194 {
16195 if (complain & tf_error)
16196 error ("%qT resolves to %qT, which is not an enumeration type",
16197 t, f);
16198 else
16199 return error_mark_node;
16200 }
16201 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16202 {
16203 if (complain & tf_error)
16204 error ("%qT resolves to %qT, which is not a class type",
16205 t, f);
16206 else
16207 return error_mark_node;
16208 }
16209 }
16210
16211 return cp_build_qualified_type_real
16212 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16213 }
16214
16215 case UNBOUND_CLASS_TEMPLATE:
16216 {
16217 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16218 in_decl, /*entering_scope=*/1);
16219 tree name = TYPE_IDENTIFIER (t);
16220 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16221
16222 if (ctx == error_mark_node || name == error_mark_node)
16223 return error_mark_node;
16224
16225 if (parm_list)
16226 parm_list = tsubst_template_parms (parm_list, args, complain);
16227 return make_unbound_class_template (ctx, name, parm_list, complain);
16228 }
16229
16230 case TYPEOF_TYPE:
16231 {
16232 tree type;
16233
16234 ++cp_unevaluated_operand;
16235 ++c_inhibit_evaluation_warnings;
16236
16237 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16238 complain, in_decl,
16239 /*integral_constant_expression_p=*/false);
16240
16241 --cp_unevaluated_operand;
16242 --c_inhibit_evaluation_warnings;
16243
16244 type = finish_typeof (type);
16245 return cp_build_qualified_type_real (type,
16246 cp_type_quals (t)
16247 | cp_type_quals (type),
16248 complain);
16249 }
16250
16251 case DECLTYPE_TYPE:
16252 {
16253 tree type;
16254
16255 ++cp_unevaluated_operand;
16256 ++c_inhibit_evaluation_warnings;
16257
16258 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16259 complain|tf_decltype, in_decl,
16260 /*function_p*/false,
16261 /*integral_constant_expression*/false);
16262
16263 --cp_unevaluated_operand;
16264 --c_inhibit_evaluation_warnings;
16265
16266 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16267 type = lambda_capture_field_type (type,
16268 false /*explicit_init*/,
16269 DECLTYPE_FOR_REF_CAPTURE (t));
16270 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16271 type = lambda_proxy_type (type);
16272 else
16273 {
16274 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16275 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16276 && EXPR_P (type))
16277 /* In a template ~id could be either a complement expression
16278 or an unqualified-id naming a destructor; if instantiating
16279 it produces an expression, it's not an id-expression or
16280 member access. */
16281 id = false;
16282 type = finish_decltype_type (type, id, complain);
16283 }
16284 return cp_build_qualified_type_real (type,
16285 cp_type_quals (t)
16286 | cp_type_quals (type),
16287 complain | tf_ignore_bad_quals);
16288 }
16289
16290 case UNDERLYING_TYPE:
16291 {
16292 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16293 complain, in_decl);
16294 return finish_underlying_type (type);
16295 }
16296
16297 case TYPE_ARGUMENT_PACK:
16298 case NONTYPE_ARGUMENT_PACK:
16299 return tsubst_argument_pack (t, args, complain, in_decl);
16300
16301 case VOID_CST:
16302 case INTEGER_CST:
16303 case REAL_CST:
16304 case STRING_CST:
16305 case PLUS_EXPR:
16306 case MINUS_EXPR:
16307 case NEGATE_EXPR:
16308 case NOP_EXPR:
16309 case INDIRECT_REF:
16310 case ADDR_EXPR:
16311 case CALL_EXPR:
16312 case ARRAY_REF:
16313 case SCOPE_REF:
16314 /* We should use one of the expression tsubsts for these codes. */
16315 gcc_unreachable ();
16316
16317 default:
16318 sorry ("use of %qs in template", get_tree_code_name (code));
16319 return error_mark_node;
16320 }
16321 }
16322
16323 /* OLDFNS is a lookup set of member functions from some class template, and
16324 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
16325 of that class template. Return the subset of NEWFNS which are
16326 specializations of a function from OLDFNS. */
16327
16328 static tree
16329 filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
16330 {
16331 /* Record all member functions from the old lookup set OLDFNS into
16332 VISIBLE_SET. */
16333 hash_set<tree> visible_set;
16334 bool seen_dep_using = false;
16335 for (tree fn : lkp_range (oldfns))
16336 {
16337 if (TREE_CODE (fn) == USING_DECL)
16338 {
16339 /* Imprecisely handle dependent using-decl by keeping all members
16340 in the new lookup set that are defined in a base class, i.e.
16341 members that could plausibly have been introduced by this
16342 dependent using-decl.
16343 FIXME: Track which members are introduced by a dependent
16344 using-decl precisely, perhaps by performing another lookup
16345 from the substituted USING_DECL_SCOPE. */
16346 gcc_checking_assert (DECL_DEPENDENT_P (fn));
16347 seen_dep_using = true;
16348 }
16349 else
16350 visible_set.add (fn);
16351 }
16352
16353 /* Returns true iff (a less specialized version of) FN appeared in
16354 the old lookup set OLDFNS. */
16355 auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
16356 if (DECL_CONTEXT (fn) != newtype)
16357 /* FN is a member function from a base class, introduced via a
16358 using-decl; if it might have been introduced by a dependent
16359 using-decl then just conservatively keep it, otherwise look
16360 in the old lookup set for FN exactly. */
16361 return seen_dep_using || visible_set.contains (fn);
16362 else if (TREE_CODE (fn) == TEMPLATE_DECL)
16363 /* FN is a member function template from the current class;
16364 look in the old lookup set for the TEMPLATE_DECL from which
16365 it was specialized. */
16366 return visible_set.contains (DECL_TI_TEMPLATE (fn));
16367 else
16368 /* FN is a non-template member function from the current class;
16369 look in the old lookup set for the FUNCTION_DECL from which
16370 it was specialized. */
16371 return visible_set.contains (DECL_TEMPLATE_RESULT
16372 (DECL_TI_TEMPLATE (fn)));
16373 };
16374
16375 bool lookup_changed_p = false;
16376 for (tree fn : lkp_range (newfns))
16377 if (!visible_p (fn))
16378 {
16379 lookup_changed_p = true;
16380 break;
16381 }
16382 if (!lookup_changed_p)
16383 return newfns;
16384
16385 /* Filter out from NEWFNS the member functions that weren't
16386 previously visible according to OLDFNS. */
16387 tree filtered_fns = NULL_TREE;
16388 unsigned filtered_size = 0;
16389 for (tree fn : lkp_range (newfns))
16390 if (visible_p (fn))
16391 {
16392 filtered_fns = lookup_add (fn, filtered_fns);
16393 filtered_size++;
16394 }
16395 gcc_checking_assert (seen_dep_using
16396 ? filtered_size >= visible_set.elements ()
16397 : filtered_size == visible_set.elements ());
16398
16399 return filtered_fns;
16400 }
16401
16402 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16403 expression on the left-hand side of the "." or "->" operator. We
16404 only do the lookup if we had a dependent BASELINK. Otherwise we
16405 adjust it onto the instantiated heirarchy. */
16406
16407 static tree
16408 tsubst_baselink (tree baselink, tree object_type,
16409 tree args, tsubst_flags_t complain, tree in_decl)
16410 {
16411 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16412 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16413 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16414
16415 tree optype = BASELINK_OPTYPE (baselink);
16416 optype = tsubst (optype, args, complain, in_decl);
16417
16418 tree template_args = NULL_TREE;
16419 bool template_id_p = false;
16420 tree fns = BASELINK_FUNCTIONS (baselink);
16421 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16422 {
16423 template_id_p = true;
16424 template_args = TREE_OPERAND (fns, 1);
16425 fns = TREE_OPERAND (fns, 0);
16426 if (template_args)
16427 template_args = tsubst_template_args (template_args, args,
16428 complain, in_decl);
16429 }
16430
16431 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16432 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16433 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16434
16435 if (dependent_p)
16436 {
16437 tree name = OVL_NAME (fns);
16438 if (IDENTIFIER_CONV_OP_P (name))
16439 name = make_conv_op_name (optype);
16440
16441 /* See maybe_dependent_member_ref. */
16442 if (dependent_scope_p (qualifying_scope))
16443 {
16444 if (template_id_p)
16445 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
16446 template_args);
16447 return build_qualified_name (NULL_TREE, qualifying_scope, name,
16448 /* ::template */false);
16449 }
16450
16451 if (name == complete_dtor_identifier)
16452 /* Treat as-if non-dependent below. */
16453 dependent_p = false;
16454
16455 bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
16456 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16457 complain);
16458 if (maybe_incomplete)
16459 {
16460 /* Filter out from the new lookup set those functions which didn't
16461 appear in the original lookup set (in a less specialized form).
16462 This is needed to preserve the consistency of member lookup
16463 performed in an incomplete-class context, within which
16464 later-declared members ought to remain invisible. */
16465 BASELINK_FUNCTIONS (baselink)
16466 = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
16467 binfo_type);
16468 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
16469 }
16470
16471 if (!baselink)
16472 {
16473 if ((complain & tf_error)
16474 && constructor_name_p (name, qualifying_scope))
16475 error ("cannot call constructor %<%T::%D%> directly",
16476 qualifying_scope, name);
16477 return error_mark_node;
16478 }
16479
16480 fns = BASELINK_FUNCTIONS (baselink);
16481 }
16482 else
16483 {
16484 /* We're going to overwrite pieces below, make a duplicate. */
16485 baselink = copy_node (baselink);
16486
16487 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
16488 {
16489 /* The decl we found was from non-dependent scope, but we still need
16490 to update the binfos for the instantiated qualifying_scope. */
16491 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
16492 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
16493 ba_unique, nullptr, complain);
16494 }
16495 }
16496
16497 /* If lookup found a single function, mark it as used at this point.
16498 (If lookup found multiple functions the one selected later by
16499 overload resolution will be marked as used at that point.) */
16500 if (!template_id_p && !really_overloaded_fn (fns))
16501 {
16502 tree fn = OVL_FIRST (fns);
16503 bool ok = mark_used (fn, complain);
16504 if (!ok && !(complain & tf_error))
16505 return error_mark_node;
16506 if (ok && BASELINK_P (baselink))
16507 /* We might have instantiated an auto function. */
16508 TREE_TYPE (baselink) = TREE_TYPE (fn);
16509 }
16510
16511 if (BASELINK_P (baselink))
16512 {
16513 /* Add back the template arguments, if present. */
16514 if (template_id_p)
16515 BASELINK_FUNCTIONS (baselink)
16516 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16517
16518 /* Update the conversion operator type. */
16519 BASELINK_OPTYPE (baselink) = optype;
16520 }
16521
16522 if (!object_type)
16523 object_type = current_class_type;
16524
16525 if (qualified_p || !dependent_p)
16526 {
16527 baselink = adjust_result_of_qualified_name_lookup (baselink,
16528 qualifying_scope,
16529 object_type);
16530 if (!qualified_p)
16531 /* We need to call adjust_result_of_qualified_name_lookup in case the
16532 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16533 so that we still get virtual function binding. */
16534 BASELINK_QUALIFIED_P (baselink) = false;
16535 }
16536
16537 return baselink;
16538 }
16539
16540 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16541 true if the qualified-id will be a postfix-expression in-and-of
16542 itself; false if more of the postfix-expression follows the
16543 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16544 of "&". */
16545
16546 static tree
16547 tsubst_qualified_id (tree qualified_id, tree args,
16548 tsubst_flags_t complain, tree in_decl,
16549 bool done, bool address_p)
16550 {
16551 tree expr;
16552 tree scope;
16553 tree name;
16554 bool is_template;
16555 tree template_args;
16556 location_t loc = EXPR_LOCATION (qualified_id);
16557
16558 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16559
16560 /* Figure out what name to look up. */
16561 name = TREE_OPERAND (qualified_id, 1);
16562 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16563 {
16564 is_template = true;
16565 template_args = TREE_OPERAND (name, 1);
16566 if (template_args)
16567 template_args = tsubst_template_args (template_args, args,
16568 complain, in_decl);
16569 if (template_args == error_mark_node)
16570 return error_mark_node;
16571 name = TREE_OPERAND (name, 0);
16572 }
16573 else
16574 {
16575 is_template = false;
16576 template_args = NULL_TREE;
16577 }
16578
16579 /* Substitute into the qualifying scope. When there are no ARGS, we
16580 are just trying to simplify a non-dependent expression. In that
16581 case the qualifying scope may be dependent, and, in any case,
16582 substituting will not help. */
16583 scope = TREE_OPERAND (qualified_id, 0);
16584 if (args)
16585 {
16586 scope = tsubst (scope, args, complain, in_decl);
16587 expr = tsubst_copy (name, args, complain, in_decl);
16588 }
16589 else
16590 expr = name;
16591
16592 if (dependent_scope_p (scope))
16593 {
16594 if (is_template)
16595 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16596 tree r = build_qualified_name (NULL_TREE, scope, expr,
16597 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16598 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16599 return r;
16600 }
16601
16602 if (!BASELINK_P (name) && !DECL_P (expr))
16603 {
16604 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16605 {
16606 /* A BIT_NOT_EXPR is used to represent a destructor. */
16607 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16608 {
16609 error ("qualifying type %qT does not match destructor name ~%qT",
16610 scope, TREE_OPERAND (expr, 0));
16611 expr = error_mark_node;
16612 }
16613 else
16614 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16615 LOOK_want::NORMAL, false);
16616 }
16617 else
16618 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16619 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16620 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16621 {
16622 if (complain & tf_error)
16623 {
16624 error ("dependent-name %qE is parsed as a non-type, but "
16625 "instantiation yields a type", qualified_id);
16626 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16627 }
16628 return error_mark_node;
16629 }
16630 }
16631
16632 if (DECL_P (expr))
16633 {
16634 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16635 scope, complain))
16636 return error_mark_node;
16637 /* Remember that there was a reference to this entity. */
16638 if (!mark_used (expr, complain) && !(complain & tf_error))
16639 return error_mark_node;
16640 }
16641
16642 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16643 {
16644 if (complain & tf_error)
16645 qualified_name_lookup_error (scope,
16646 TREE_OPERAND (qualified_id, 1),
16647 expr, input_location);
16648 return error_mark_node;
16649 }
16650
16651 if (is_template)
16652 {
16653 /* We may be repeating a check already done during parsing, but
16654 if it was well-formed and passed then, it will pass again
16655 now, and if it didn't, we wouldn't have got here. The case
16656 we want to catch is when we couldn't tell then, and can now,
16657 namely when templ prior to substitution was an
16658 identifier. */
16659 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16660 return error_mark_node;
16661
16662 if (variable_template_p (expr))
16663 expr = lookup_and_finish_template_variable (expr, template_args,
16664 complain);
16665 else
16666 expr = lookup_template_function (expr, template_args);
16667 }
16668
16669 if (expr == error_mark_node && complain & tf_error)
16670 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16671 expr, input_location);
16672 else if (TYPE_P (scope))
16673 {
16674 expr = (adjust_result_of_qualified_name_lookup
16675 (expr, scope, current_nonlambda_class_type ()));
16676 expr = (finish_qualified_id_expr
16677 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16678 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16679 /*template_arg_p=*/false, complain));
16680 }
16681
16682 /* Expressions do not generally have reference type. */
16683 if (TREE_CODE (expr) != SCOPE_REF
16684 /* However, if we're about to form a pointer-to-member, we just
16685 want the referenced member referenced. */
16686 && TREE_CODE (expr) != OFFSET_REF)
16687 expr = convert_from_reference (expr);
16688
16689 if (REF_PARENTHESIZED_P (qualified_id))
16690 expr = force_paren_expr (expr);
16691
16692 expr = maybe_wrap_with_location (expr, loc);
16693
16694 return expr;
16695 }
16696
16697 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16698 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16699 for tsubst. */
16700
16701 static tree
16702 tsubst_init (tree init, tree decl, tree args,
16703 tsubst_flags_t complain, tree in_decl)
16704 {
16705 if (!init)
16706 return NULL_TREE;
16707
16708 init = tsubst_expr (init, args, complain, in_decl, false);
16709
16710 tree type = TREE_TYPE (decl);
16711
16712 if (!init && type != error_mark_node)
16713 {
16714 if (tree auto_node = type_uses_auto (type))
16715 {
16716 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16717 {
16718 if (complain & tf_error)
16719 error ("initializer for %q#D expands to an empty list "
16720 "of expressions", decl);
16721 return error_mark_node;
16722 }
16723 }
16724 else if (!dependent_type_p (type))
16725 {
16726 /* If we had an initializer but it
16727 instantiated to nothing,
16728 value-initialize the object. This will
16729 only occur when the initializer was a
16730 pack expansion where the parameter packs
16731 used in that expansion were of length
16732 zero. */
16733 init = build_value_init (type, complain);
16734 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16735 init = get_target_expr_sfinae (init, complain);
16736 if (TREE_CODE (init) == TARGET_EXPR)
16737 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16738 }
16739 }
16740
16741 return init;
16742 }
16743
16744 /* If T is a reference to a dependent member of the current instantiation C and
16745 we are trying to refer to that member in a partial instantiation of C,
16746 return a SCOPE_REF; otherwise, return NULL_TREE.
16747
16748 This can happen when forming a C++17 deduction guide, as in PR96199. */
16749
16750 static tree
16751 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16752 tree in_decl)
16753 {
16754 if (cxx_dialect < cxx17)
16755 return NULL_TREE;
16756
16757 tree ctx = context_for_name_lookup (t);
16758 if (!CLASS_TYPE_P (ctx))
16759 return NULL_TREE;
16760
16761 ctx = tsubst (ctx, args, complain, in_decl);
16762 if (dependent_scope_p (ctx))
16763 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16764 /*template_p=*/false);
16765
16766 return NULL_TREE;
16767 }
16768
16769 /* Like tsubst, but deals with expressions. This function just replaces
16770 template parms; to finish processing the resultant expression, use
16771 tsubst_copy_and_build or tsubst_expr. */
16772
16773 static tree
16774 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16775 {
16776 enum tree_code code;
16777 tree r;
16778
16779 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16780 return t;
16781
16782 code = TREE_CODE (t);
16783
16784 switch (code)
16785 {
16786 case PARM_DECL:
16787 r = retrieve_local_specialization (t);
16788
16789 if (r == NULL_TREE)
16790 {
16791 /* We get here for a use of 'this' in an NSDMI. */
16792 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16793 return current_class_ptr;
16794
16795 /* This can happen for a parameter name used later in a function
16796 declaration (such as in a late-specified return type). Just
16797 make a dummy decl, since it's only used for its type. */
16798 gcc_assert (cp_unevaluated_operand != 0);
16799 r = tsubst_decl (t, args, complain);
16800 /* Give it the template pattern as its context; its true context
16801 hasn't been instantiated yet and this is good enough for
16802 mangling. */
16803 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16804 }
16805
16806 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16807 r = argument_pack_select_arg (r);
16808 if (!mark_used (r, complain) && !(complain & tf_error))
16809 return error_mark_node;
16810 return r;
16811
16812 case CONST_DECL:
16813 {
16814 tree enum_type;
16815 tree v;
16816
16817 if (DECL_TEMPLATE_PARM_P (t))
16818 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16819 /* There is no need to substitute into namespace-scope
16820 enumerators. */
16821 if (DECL_NAMESPACE_SCOPE_P (t))
16822 return t;
16823 /* If ARGS is NULL, then T is known to be non-dependent. */
16824 if (args == NULL_TREE)
16825 return scalar_constant_value (t);
16826
16827 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16828 return ref;
16829
16830 /* Unfortunately, we cannot just call lookup_name here.
16831 Consider:
16832
16833 template <int I> int f() {
16834 enum E { a = I };
16835 struct S { void g() { E e = a; } };
16836 };
16837
16838 When we instantiate f<7>::S::g(), say, lookup_name is not
16839 clever enough to find f<7>::a. */
16840 enum_type
16841 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16842 /*entering_scope=*/0);
16843
16844 for (v = TYPE_VALUES (enum_type);
16845 v != NULL_TREE;
16846 v = TREE_CHAIN (v))
16847 if (TREE_PURPOSE (v) == DECL_NAME (t))
16848 return TREE_VALUE (v);
16849
16850 /* We didn't find the name. That should never happen; if
16851 name-lookup found it during preliminary parsing, we
16852 should find it again here during instantiation. */
16853 gcc_unreachable ();
16854 }
16855 return t;
16856
16857 case FIELD_DECL:
16858 if (DECL_CONTEXT (t))
16859 {
16860 tree ctx;
16861
16862 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16863 /*entering_scope=*/1);
16864 if (ctx != DECL_CONTEXT (t))
16865 {
16866 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16867 if (!r)
16868 {
16869 if (complain & tf_error)
16870 error ("using invalid field %qD", t);
16871 return error_mark_node;
16872 }
16873 return r;
16874 }
16875 }
16876
16877 return t;
16878
16879 case VAR_DECL:
16880 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16881 return ref;
16882 gcc_fallthrough();
16883 case FUNCTION_DECL:
16884 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16885 r = tsubst (t, args, complain, in_decl);
16886 else if (DECL_LOCAL_DECL_P (t))
16887 {
16888 /* Local specialization will usually have been created when
16889 we instantiated the DECL_EXPR_DECL. */
16890 r = retrieve_local_specialization (t);
16891 if (!r)
16892 {
16893 /* We're in a generic lambda referencing a local extern
16894 from an outer block-scope of a non-template. */
16895 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
16896 r = t;
16897 }
16898 }
16899 else if (local_variable_p (t)
16900 && uses_template_parms (DECL_CONTEXT (t)))
16901 {
16902 r = retrieve_local_specialization (t);
16903 if (r == NULL_TREE)
16904 {
16905 /* First try name lookup to find the instantiation. */
16906 r = lookup_name (DECL_NAME (t));
16907 if (r)
16908 {
16909 if (!VAR_P (r))
16910 {
16911 /* During error-recovery we may find a non-variable,
16912 even an OVERLOAD: just bail out and avoid ICEs and
16913 duplicate diagnostics (c++/62207). */
16914 gcc_assert (seen_error ());
16915 return error_mark_node;
16916 }
16917 if (!is_capture_proxy (r))
16918 {
16919 /* Make sure the one we found is the one we want. */
16920 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16921 if (ctx != DECL_CONTEXT (r))
16922 r = NULL_TREE;
16923 }
16924 }
16925
16926 if (r)
16927 /* OK */;
16928 else
16929 {
16930 /* This can happen for a variable used in a
16931 late-specified return type of a local lambda, or for a
16932 local static or constant. Building a new VAR_DECL
16933 should be OK in all those cases. */
16934 r = tsubst_decl (t, args, complain);
16935 if (local_specializations)
16936 /* Avoid infinite recursion (79640). */
16937 register_local_specialization (r, t);
16938 if (decl_maybe_constant_var_p (r))
16939 {
16940 /* We can't call cp_finish_decl, so handle the
16941 initializer by hand. */
16942 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16943 complain, in_decl);
16944 if (!processing_template_decl)
16945 init = maybe_constant_init (init);
16946 if (processing_template_decl
16947 ? potential_constant_expression (init)
16948 : reduced_constant_expression_p (init))
16949 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16950 = TREE_CONSTANT (r) = true;
16951 DECL_INITIAL (r) = init;
16952 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16953 TREE_TYPE (r)
16954 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16955 complain, adc_variable_type);
16956 }
16957 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16958 || decl_constant_var_p (r)
16959 || seen_error ());
16960 if (!processing_template_decl
16961 && !TREE_STATIC (r))
16962 r = process_outer_var_ref (r, complain);
16963 }
16964 /* Remember this for subsequent uses. */
16965 if (local_specializations)
16966 register_local_specialization (r, t);
16967 }
16968 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16969 r = argument_pack_select_arg (r);
16970 }
16971 else
16972 r = t;
16973 if (!mark_used (r, complain))
16974 return error_mark_node;
16975 return r;
16976
16977 case NAMESPACE_DECL:
16978 return t;
16979
16980 case OVERLOAD:
16981 return t;
16982
16983 case BASELINK:
16984 return tsubst_baselink (t, current_nonlambda_class_type (),
16985 args, complain, in_decl);
16986
16987 case TEMPLATE_DECL:
16988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16989 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16990 args, complain, in_decl);
16991 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16992 return tsubst (t, args, complain, in_decl);
16993 else if (DECL_CLASS_SCOPE_P (t)
16994 && uses_template_parms (DECL_CONTEXT (t)))
16995 {
16996 /* Template template argument like the following example need
16997 special treatment:
16998
16999 template <template <class> class TT> struct C {};
17000 template <class T> struct D {
17001 template <class U> struct E {};
17002 C<E> c; // #1
17003 };
17004 D<int> d; // #2
17005
17006 We are processing the template argument `E' in #1 for
17007 the template instantiation #2. Originally, `E' is a
17008 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
17009 have to substitute this with one having context `D<int>'. */
17010
17011 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
17012 if (dependent_scope_p (context))
17013 {
17014 /* When rewriting a constructor into a deduction guide, a
17015 non-dependent name can become dependent, so memtmpl<args>
17016 becomes context::template memtmpl<args>. */
17017 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17018 return build_qualified_name (type, context, DECL_NAME (t),
17019 /*template*/true);
17020 }
17021 return lookup_field (context, DECL_NAME(t), 0, false);
17022 }
17023 else
17024 /* Ordinary template template argument. */
17025 return t;
17026
17027 case NON_LVALUE_EXPR:
17028 case VIEW_CONVERT_EXPR:
17029 {
17030 /* Handle location wrappers by substituting the wrapped node
17031 first, *then* reusing the resulting type. Doing the type
17032 first ensures that we handle template parameters and
17033 parameter pack expansions. */
17034 if (location_wrapper_p (t))
17035 {
17036 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
17037 complain, in_decl);
17038 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
17039 }
17040 tree op = TREE_OPERAND (t, 0);
17041 if (code == VIEW_CONVERT_EXPR
17042 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
17043 {
17044 /* Wrapper to make a C++20 template parameter object const. */
17045 op = tsubst_copy (op, args, complain, in_decl);
17046 if (!CP_TYPE_CONST_P (TREE_TYPE (op)))
17047 {
17048 /* The template argument is not const, presumably because
17049 it is still dependent, and so not the const template parm
17050 object. */
17051 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17052 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
17053 (type, TREE_TYPE (op)));
17054 if (TREE_CODE (op) == CONSTRUCTOR
17055 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
17056 {
17057 /* Don't add a wrapper to these. */
17058 op = copy_node (op);
17059 TREE_TYPE (op) = type;
17060 }
17061 else
17062 /* Do add a wrapper otherwise (in particular, if op is
17063 another TEMPLATE_PARM_INDEX). */
17064 op = build1 (code, type, op);
17065 }
17066 return op;
17067 }
17068 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
17069 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
17070 {
17071 op = tsubst_copy (op, args, complain, in_decl);
17072 op = build1 (code, TREE_TYPE (op), op);
17073 REF_PARENTHESIZED_P (op) = true;
17074 return op;
17075 }
17076 /* We shouldn't see any other uses of these in templates. */
17077 gcc_unreachable ();
17078 }
17079
17080 case CAST_EXPR:
17081 case REINTERPRET_CAST_EXPR:
17082 case CONST_CAST_EXPR:
17083 case STATIC_CAST_EXPR:
17084 case DYNAMIC_CAST_EXPR:
17085 case IMPLICIT_CONV_EXPR:
17086 case CONVERT_EXPR:
17087 case NOP_EXPR:
17088 {
17089 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17090 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17091 return build1 (code, type, op0);
17092 }
17093
17094 case BIT_CAST_EXPR:
17095 {
17096 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17097 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17098 r = build_min (BIT_CAST_EXPR, type, op0);
17099 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17100 return r;
17101 }
17102
17103 case SIZEOF_EXPR:
17104 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17105 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17106 {
17107 tree expanded, op = TREE_OPERAND (t, 0);
17108 int len = 0;
17109
17110 if (SIZEOF_EXPR_TYPE_P (t))
17111 op = TREE_TYPE (op);
17112
17113 ++cp_unevaluated_operand;
17114 ++c_inhibit_evaluation_warnings;
17115 /* We only want to compute the number of arguments. */
17116 if (PACK_EXPANSION_P (op))
17117 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
17118 else
17119 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
17120 args, complain, in_decl);
17121 --cp_unevaluated_operand;
17122 --c_inhibit_evaluation_warnings;
17123
17124 if (TREE_CODE (expanded) == TREE_VEC)
17125 {
17126 len = TREE_VEC_LENGTH (expanded);
17127 /* Set TREE_USED for the benefit of -Wunused. */
17128 for (int i = 0; i < len; i++)
17129 if (DECL_P (TREE_VEC_ELT (expanded, i)))
17130 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
17131 }
17132
17133 if (expanded == error_mark_node)
17134 return error_mark_node;
17135 else if (PACK_EXPANSION_P (expanded)
17136 || (TREE_CODE (expanded) == TREE_VEC
17137 && pack_expansion_args_count (expanded)))
17138
17139 {
17140 if (PACK_EXPANSION_P (expanded))
17141 /* OK. */;
17142 else if (TREE_VEC_LENGTH (expanded) == 1)
17143 expanded = TREE_VEC_ELT (expanded, 0);
17144 else
17145 expanded = make_argument_pack (expanded);
17146
17147 if (TYPE_P (expanded))
17148 return cxx_sizeof_or_alignof_type (input_location,
17149 expanded, SIZEOF_EXPR,
17150 false,
17151 complain & tf_error);
17152 else
17153 return cxx_sizeof_or_alignof_expr (input_location,
17154 expanded, SIZEOF_EXPR,
17155 false,
17156 complain & tf_error);
17157 }
17158 else
17159 return build_int_cst (size_type_node, len);
17160 }
17161 if (SIZEOF_EXPR_TYPE_P (t))
17162 {
17163 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
17164 args, complain, in_decl);
17165 r = build1 (NOP_EXPR, r, error_mark_node);
17166 r = build1 (SIZEOF_EXPR,
17167 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
17168 SIZEOF_EXPR_TYPE_P (r) = 1;
17169 return r;
17170 }
17171 /* Fall through */
17172
17173 case INDIRECT_REF:
17174 case NEGATE_EXPR:
17175 case TRUTH_NOT_EXPR:
17176 case BIT_NOT_EXPR:
17177 case ADDR_EXPR:
17178 case UNARY_PLUS_EXPR: /* Unary + */
17179 case ALIGNOF_EXPR:
17180 case AT_ENCODE_EXPR:
17181 case ARROW_EXPR:
17182 case THROW_EXPR:
17183 case TYPEID_EXPR:
17184 case REALPART_EXPR:
17185 case IMAGPART_EXPR:
17186 case PAREN_EXPR:
17187 {
17188 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17189 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17190 r = build1_loc (EXPR_LOCATION (t), code, type, op0);
17191 if (code == ALIGNOF_EXPR)
17192 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
17193 /* For addresses of immediate functions ensure we have EXPR_LOCATION
17194 set for possible later diagnostics. */
17195 if (code == ADDR_EXPR
17196 && EXPR_LOCATION (r) == UNKNOWN_LOCATION
17197 && TREE_CODE (op0) == FUNCTION_DECL
17198 && DECL_IMMEDIATE_FUNCTION_P (op0))
17199 SET_EXPR_LOCATION (r, input_location);
17200 return r;
17201 }
17202
17203 case COMPONENT_REF:
17204 {
17205 tree object;
17206 tree name;
17207
17208 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17209 name = TREE_OPERAND (t, 1);
17210 if (TREE_CODE (name) == BIT_NOT_EXPR)
17211 {
17212 name = tsubst_copy (TREE_OPERAND (name, 0), args,
17213 complain, in_decl);
17214 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
17215 }
17216 else if (TREE_CODE (name) == SCOPE_REF
17217 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
17218 {
17219 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
17220 complain, in_decl);
17221 name = TREE_OPERAND (name, 1);
17222 name = tsubst_copy (TREE_OPERAND (name, 0), args,
17223 complain, in_decl);
17224 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
17225 name = build_qualified_name (/*type=*/NULL_TREE,
17226 base, name,
17227 /*template_p=*/false);
17228 }
17229 else if (BASELINK_P (name))
17230 name = tsubst_baselink (name,
17231 non_reference (TREE_TYPE (object)),
17232 args, complain,
17233 in_decl);
17234 else
17235 name = tsubst_copy (name, args, complain, in_decl);
17236 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
17237 }
17238
17239 case PLUS_EXPR:
17240 case MINUS_EXPR:
17241 case MULT_EXPR:
17242 case TRUNC_DIV_EXPR:
17243 case CEIL_DIV_EXPR:
17244 case FLOOR_DIV_EXPR:
17245 case ROUND_DIV_EXPR:
17246 case EXACT_DIV_EXPR:
17247 case BIT_AND_EXPR:
17248 case BIT_IOR_EXPR:
17249 case BIT_XOR_EXPR:
17250 case TRUNC_MOD_EXPR:
17251 case FLOOR_MOD_EXPR:
17252 case TRUTH_ANDIF_EXPR:
17253 case TRUTH_ORIF_EXPR:
17254 case TRUTH_AND_EXPR:
17255 case TRUTH_OR_EXPR:
17256 case RSHIFT_EXPR:
17257 case LSHIFT_EXPR:
17258 case EQ_EXPR:
17259 case NE_EXPR:
17260 case MAX_EXPR:
17261 case MIN_EXPR:
17262 case LE_EXPR:
17263 case GE_EXPR:
17264 case LT_EXPR:
17265 case GT_EXPR:
17266 case COMPOUND_EXPR:
17267 case DOTSTAR_EXPR:
17268 case MEMBER_REF:
17269 case PREDECREMENT_EXPR:
17270 case PREINCREMENT_EXPR:
17271 case POSTDECREMENT_EXPR:
17272 case POSTINCREMENT_EXPR:
17273 {
17274 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17275 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17276 return build_nt (code, op0, op1);
17277 }
17278
17279 case SCOPE_REF:
17280 {
17281 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17282 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17283 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
17284 QUALIFIED_NAME_IS_TEMPLATE (t));
17285 }
17286
17287 case ARRAY_REF:
17288 {
17289 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17290 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17291 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
17292 }
17293
17294 case CALL_EXPR:
17295 {
17296 int n = VL_EXP_OPERAND_LENGTH (t);
17297 tree result = build_vl_exp (CALL_EXPR, n);
17298 int i;
17299 for (i = 0; i < n; i++)
17300 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
17301 complain, in_decl);
17302 return result;
17303 }
17304
17305 case COND_EXPR:
17306 case MODOP_EXPR:
17307 case PSEUDO_DTOR_EXPR:
17308 case VEC_PERM_EXPR:
17309 {
17310 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17311 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17312 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17313 r = build_nt (code, op0, op1, op2);
17314 copy_warning (r, t);
17315 return r;
17316 }
17317
17318 case NEW_EXPR:
17319 {
17320 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17321 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17322 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17323 r = build_nt (code, op0, op1, op2);
17324 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17325 return r;
17326 }
17327
17328 case DELETE_EXPR:
17329 {
17330 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17331 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17332 r = build_nt (code, op0, op1);
17333 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17334 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17335 return r;
17336 }
17337
17338 case TEMPLATE_ID_EXPR:
17339 {
17340 /* Substituted template arguments */
17341 tree tmpl = TREE_OPERAND (t, 0);
17342 tree targs = TREE_OPERAND (t, 1);
17343
17344 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
17345 if (targs)
17346 targs = tsubst_template_args (targs, args, complain, in_decl);
17347
17348 if (variable_template_p (tmpl))
17349 return lookup_template_variable (tmpl, targs);
17350 else
17351 return lookup_template_function (tmpl, targs);
17352 }
17353
17354 case TREE_LIST:
17355 {
17356 tree purpose, value, chain;
17357
17358 if (t == void_list_node)
17359 return t;
17360
17361 purpose = TREE_PURPOSE (t);
17362 if (purpose)
17363 purpose = tsubst_copy (purpose, args, complain, in_decl);
17364 value = TREE_VALUE (t);
17365 if (value)
17366 value = tsubst_copy (value, args, complain, in_decl);
17367 chain = TREE_CHAIN (t);
17368 if (chain && chain != void_type_node)
17369 chain = tsubst_copy (chain, args, complain, in_decl);
17370 if (purpose == TREE_PURPOSE (t)
17371 && value == TREE_VALUE (t)
17372 && chain == TREE_CHAIN (t))
17373 return t;
17374 return tree_cons (purpose, value, chain);
17375 }
17376
17377 case RECORD_TYPE:
17378 case UNION_TYPE:
17379 case ENUMERAL_TYPE:
17380 case INTEGER_TYPE:
17381 case TEMPLATE_TYPE_PARM:
17382 case TEMPLATE_TEMPLATE_PARM:
17383 case BOUND_TEMPLATE_TEMPLATE_PARM:
17384 case TEMPLATE_PARM_INDEX:
17385 case POINTER_TYPE:
17386 case REFERENCE_TYPE:
17387 case OFFSET_TYPE:
17388 case FUNCTION_TYPE:
17389 case METHOD_TYPE:
17390 case ARRAY_TYPE:
17391 case TYPENAME_TYPE:
17392 case UNBOUND_CLASS_TEMPLATE:
17393 case TYPEOF_TYPE:
17394 case DECLTYPE_TYPE:
17395 case TYPE_DECL:
17396 return tsubst (t, args, complain, in_decl);
17397
17398 case USING_DECL:
17399 t = DECL_NAME (t);
17400 /* Fall through. */
17401 case IDENTIFIER_NODE:
17402 if (IDENTIFIER_CONV_OP_P (t))
17403 {
17404 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17405 return make_conv_op_name (new_type);
17406 }
17407 else
17408 return t;
17409
17410 case CONSTRUCTOR:
17411 /* This is handled by tsubst_copy_and_build. */
17412 gcc_unreachable ();
17413
17414 case VA_ARG_EXPR:
17415 {
17416 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17417 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17418 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17419 }
17420
17421 case CLEANUP_POINT_EXPR:
17422 /* We shouldn't have built any of these during initial template
17423 generation. Instead, they should be built during instantiation
17424 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17425 gcc_unreachable ();
17426
17427 case OFFSET_REF:
17428 {
17429 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17430 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17431 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17432 r = build2 (code, type, op0, op1);
17433 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17434 if (!mark_used (TREE_OPERAND (r, 1), complain)
17435 && !(complain & tf_error))
17436 return error_mark_node;
17437 return r;
17438 }
17439
17440 case EXPR_PACK_EXPANSION:
17441 error ("invalid use of pack expansion expression");
17442 return error_mark_node;
17443
17444 case NONTYPE_ARGUMENT_PACK:
17445 error ("use %<...%> to expand argument pack");
17446 return error_mark_node;
17447
17448 case VOID_CST:
17449 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17450 return t;
17451
17452 case INTEGER_CST:
17453 case REAL_CST:
17454 case COMPLEX_CST:
17455 case VECTOR_CST:
17456 {
17457 /* Instantiate any typedefs in the type. */
17458 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17459 r = fold_convert (type, t);
17460 gcc_assert (TREE_CODE (r) == code);
17461 return r;
17462 }
17463
17464 case STRING_CST:
17465 {
17466 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17467 r = t;
17468 if (type != TREE_TYPE (t))
17469 {
17470 r = copy_node (t);
17471 TREE_TYPE (r) = type;
17472 }
17473 return r;
17474 }
17475
17476 case PTRMEM_CST:
17477 /* These can sometimes show up in a partial instantiation, but never
17478 involve template parms. */
17479 gcc_assert (!uses_template_parms (t));
17480 return t;
17481
17482 case UNARY_LEFT_FOLD_EXPR:
17483 return tsubst_unary_left_fold (t, args, complain, in_decl);
17484 case UNARY_RIGHT_FOLD_EXPR:
17485 return tsubst_unary_right_fold (t, args, complain, in_decl);
17486 case BINARY_LEFT_FOLD_EXPR:
17487 return tsubst_binary_left_fold (t, args, complain, in_decl);
17488 case BINARY_RIGHT_FOLD_EXPR:
17489 return tsubst_binary_right_fold (t, args, complain, in_decl);
17490 case PREDICT_EXPR:
17491 return t;
17492
17493 case DEBUG_BEGIN_STMT:
17494 /* ??? There's no point in copying it for now, but maybe some
17495 day it will contain more information, such as a pointer back
17496 to the containing function, inlined copy or so. */
17497 return t;
17498
17499 case CO_AWAIT_EXPR:
17500 return tsubst_expr (t, args, complain, in_decl,
17501 /*integral_constant_expression_p=*/false);
17502 break;
17503
17504 default:
17505 /* We shouldn't get here, but keep going if !flag_checking. */
17506 if (flag_checking)
17507 gcc_unreachable ();
17508 return t;
17509 }
17510 }
17511
17512 /* Helper function for tsubst_omp_clauses, used for instantiation of
17513 OMP_CLAUSE_DECL of clauses. */
17514
17515 static tree
17516 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17517 tree in_decl, tree *iterator_cache)
17518 {
17519 if (decl == NULL_TREE)
17520 return NULL_TREE;
17521
17522 /* Handle OpenMP iterators. */
17523 if (TREE_CODE (decl) == TREE_LIST
17524 && TREE_PURPOSE (decl)
17525 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17526 {
17527 tree ret;
17528 if (iterator_cache[0] == TREE_PURPOSE (decl))
17529 ret = iterator_cache[1];
17530 else
17531 {
17532 tree *tp = &ret;
17533 begin_scope (sk_omp, NULL);
17534 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17535 {
17536 *tp = copy_node (it);
17537 TREE_VEC_ELT (*tp, 0)
17538 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17539 TREE_VEC_ELT (*tp, 1)
17540 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17541 /*integral_constant_expression_p=*/false);
17542 TREE_VEC_ELT (*tp, 2)
17543 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17544 /*integral_constant_expression_p=*/false);
17545 TREE_VEC_ELT (*tp, 3)
17546 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17547 /*integral_constant_expression_p=*/false);
17548 TREE_CHAIN (*tp) = NULL_TREE;
17549 tp = &TREE_CHAIN (*tp);
17550 }
17551 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17552 iterator_cache[0] = TREE_PURPOSE (decl);
17553 iterator_cache[1] = ret;
17554 }
17555 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17556 args, complain,
17557 in_decl, NULL));
17558 }
17559
17560 /* Handle an OpenMP array section represented as a TREE_LIST (or
17561 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17562 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17563 TREE_LIST. We can handle it exactly the same as an array section
17564 (purpose, value, and a chain), even though the nomenclature
17565 (low_bound, length, etc) is different. */
17566 if (TREE_CODE (decl) == TREE_LIST)
17567 {
17568 tree low_bound
17569 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17570 /*integral_constant_expression_p=*/false);
17571 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17572 /*integral_constant_expression_p=*/false);
17573 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17574 in_decl, NULL);
17575 if (TREE_PURPOSE (decl) == low_bound
17576 && TREE_VALUE (decl) == length
17577 && TREE_CHAIN (decl) == chain)
17578 return decl;
17579 tree ret = tree_cons (low_bound, length, chain);
17580 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17581 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17582 return ret;
17583 }
17584 tree ret = tsubst_expr (decl, args, complain, in_decl,
17585 /*integral_constant_expression_p=*/false);
17586 /* Undo convert_from_reference tsubst_expr could have called. */
17587 if (decl
17588 && REFERENCE_REF_P (ret)
17589 && !REFERENCE_REF_P (decl))
17590 ret = TREE_OPERAND (ret, 0);
17591 return ret;
17592 }
17593
17594 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17595
17596 static tree
17597 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17598 tree args, tsubst_flags_t complain, tree in_decl)
17599 {
17600 tree new_clauses = NULL_TREE, nc, oc;
17601 tree linear_no_step = NULL_TREE;
17602 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17603
17604 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17605 {
17606 nc = copy_node (oc);
17607 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17608 new_clauses = nc;
17609
17610 switch (OMP_CLAUSE_CODE (nc))
17611 {
17612 case OMP_CLAUSE_LASTPRIVATE:
17613 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17614 {
17615 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17616 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17617 in_decl, /*integral_constant_expression_p=*/false);
17618 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17619 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17620 }
17621 /* FALLTHRU */
17622 case OMP_CLAUSE_PRIVATE:
17623 case OMP_CLAUSE_SHARED:
17624 case OMP_CLAUSE_FIRSTPRIVATE:
17625 case OMP_CLAUSE_COPYIN:
17626 case OMP_CLAUSE_COPYPRIVATE:
17627 case OMP_CLAUSE_UNIFORM:
17628 case OMP_CLAUSE_DEPEND:
17629 case OMP_CLAUSE_AFFINITY:
17630 case OMP_CLAUSE_FROM:
17631 case OMP_CLAUSE_TO:
17632 case OMP_CLAUSE_MAP:
17633 case OMP_CLAUSE__CACHE_:
17634 case OMP_CLAUSE_NONTEMPORAL:
17635 case OMP_CLAUSE_USE_DEVICE_PTR:
17636 case OMP_CLAUSE_USE_DEVICE_ADDR:
17637 case OMP_CLAUSE_IS_DEVICE_PTR:
17638 case OMP_CLAUSE_INCLUSIVE:
17639 case OMP_CLAUSE_EXCLUSIVE:
17640 OMP_CLAUSE_DECL (nc)
17641 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17642 in_decl, iterator_cache);
17643 break;
17644 case OMP_CLAUSE_NUM_TEAMS:
17645 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
17646 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
17647 = tsubst_expr (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
17648 complain, in_decl,
17649 /*integral_constant_expression_p=*/false);
17650 /* FALLTHRU */
17651 case OMP_CLAUSE_TILE:
17652 case OMP_CLAUSE_IF:
17653 case OMP_CLAUSE_NUM_THREADS:
17654 case OMP_CLAUSE_SCHEDULE:
17655 case OMP_CLAUSE_COLLAPSE:
17656 case OMP_CLAUSE_FINAL:
17657 case OMP_CLAUSE_DEVICE:
17658 case OMP_CLAUSE_DIST_SCHEDULE:
17659 case OMP_CLAUSE_THREAD_LIMIT:
17660 case OMP_CLAUSE_SAFELEN:
17661 case OMP_CLAUSE_SIMDLEN:
17662 case OMP_CLAUSE_NUM_TASKS:
17663 case OMP_CLAUSE_GRAINSIZE:
17664 case OMP_CLAUSE_PRIORITY:
17665 case OMP_CLAUSE_ORDERED:
17666 case OMP_CLAUSE_HINT:
17667 case OMP_CLAUSE_FILTER:
17668 case OMP_CLAUSE_NUM_GANGS:
17669 case OMP_CLAUSE_NUM_WORKERS:
17670 case OMP_CLAUSE_VECTOR_LENGTH:
17671 case OMP_CLAUSE_WORKER:
17672 case OMP_CLAUSE_VECTOR:
17673 case OMP_CLAUSE_ASYNC:
17674 case OMP_CLAUSE_WAIT:
17675 case OMP_CLAUSE_DETACH:
17676 OMP_CLAUSE_OPERAND (nc, 0)
17677 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17678 in_decl, /*integral_constant_expression_p=*/false);
17679 break;
17680 case OMP_CLAUSE_REDUCTION:
17681 case OMP_CLAUSE_IN_REDUCTION:
17682 case OMP_CLAUSE_TASK_REDUCTION:
17683 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17684 {
17685 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17686 if (TREE_CODE (placeholder) == SCOPE_REF)
17687 {
17688 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17689 complain, in_decl);
17690 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17691 = build_qualified_name (NULL_TREE, scope,
17692 TREE_OPERAND (placeholder, 1),
17693 false);
17694 }
17695 else
17696 gcc_assert (identifier_p (placeholder));
17697 }
17698 OMP_CLAUSE_DECL (nc)
17699 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17700 in_decl, NULL);
17701 break;
17702 case OMP_CLAUSE_GANG:
17703 case OMP_CLAUSE_ALIGNED:
17704 OMP_CLAUSE_DECL (nc)
17705 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17706 in_decl, NULL);
17707 OMP_CLAUSE_OPERAND (nc, 1)
17708 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17709 in_decl, /*integral_constant_expression_p=*/false);
17710 break;
17711 case OMP_CLAUSE_ALLOCATE:
17712 OMP_CLAUSE_DECL (nc)
17713 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17714 in_decl, NULL);
17715 OMP_CLAUSE_OPERAND (nc, 1)
17716 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17717 in_decl, /*integral_constant_expression_p=*/false);
17718 OMP_CLAUSE_OPERAND (nc, 2)
17719 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 2), args, complain,
17720 in_decl, /*integral_constant_expression_p=*/false);
17721 break;
17722 case OMP_CLAUSE_LINEAR:
17723 OMP_CLAUSE_DECL (nc)
17724 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17725 in_decl, NULL);
17726 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17727 {
17728 gcc_assert (!linear_no_step);
17729 linear_no_step = nc;
17730 }
17731 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17732 OMP_CLAUSE_LINEAR_STEP (nc)
17733 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17734 complain, in_decl, NULL);
17735 else
17736 OMP_CLAUSE_LINEAR_STEP (nc)
17737 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17738 in_decl,
17739 /*integral_constant_expression_p=*/false);
17740 break;
17741 case OMP_CLAUSE_NOWAIT:
17742 case OMP_CLAUSE_DEFAULT:
17743 case OMP_CLAUSE_UNTIED:
17744 case OMP_CLAUSE_MERGEABLE:
17745 case OMP_CLAUSE_INBRANCH:
17746 case OMP_CLAUSE_NOTINBRANCH:
17747 case OMP_CLAUSE_PROC_BIND:
17748 case OMP_CLAUSE_FOR:
17749 case OMP_CLAUSE_PARALLEL:
17750 case OMP_CLAUSE_SECTIONS:
17751 case OMP_CLAUSE_TASKGROUP:
17752 case OMP_CLAUSE_NOGROUP:
17753 case OMP_CLAUSE_THREADS:
17754 case OMP_CLAUSE_SIMD:
17755 case OMP_CLAUSE_DEFAULTMAP:
17756 case OMP_CLAUSE_ORDER:
17757 case OMP_CLAUSE_BIND:
17758 case OMP_CLAUSE_INDEPENDENT:
17759 case OMP_CLAUSE_AUTO:
17760 case OMP_CLAUSE_SEQ:
17761 case OMP_CLAUSE_IF_PRESENT:
17762 case OMP_CLAUSE_FINALIZE:
17763 case OMP_CLAUSE_NOHOST:
17764 break;
17765 default:
17766 gcc_unreachable ();
17767 }
17768 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17769 switch (OMP_CLAUSE_CODE (nc))
17770 {
17771 case OMP_CLAUSE_SHARED:
17772 case OMP_CLAUSE_PRIVATE:
17773 case OMP_CLAUSE_FIRSTPRIVATE:
17774 case OMP_CLAUSE_LASTPRIVATE:
17775 case OMP_CLAUSE_COPYPRIVATE:
17776 case OMP_CLAUSE_LINEAR:
17777 case OMP_CLAUSE_REDUCTION:
17778 case OMP_CLAUSE_IN_REDUCTION:
17779 case OMP_CLAUSE_TASK_REDUCTION:
17780 case OMP_CLAUSE_USE_DEVICE_PTR:
17781 case OMP_CLAUSE_USE_DEVICE_ADDR:
17782 case OMP_CLAUSE_IS_DEVICE_PTR:
17783 case OMP_CLAUSE_INCLUSIVE:
17784 case OMP_CLAUSE_EXCLUSIVE:
17785 case OMP_CLAUSE_ALLOCATE:
17786 /* tsubst_expr on SCOPE_REF results in returning
17787 finish_non_static_data_member result. Undo that here. */
17788 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17789 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17790 == IDENTIFIER_NODE))
17791 {
17792 tree t = OMP_CLAUSE_DECL (nc);
17793 tree v = t;
17794 while (v)
17795 switch (TREE_CODE (v))
17796 {
17797 case COMPONENT_REF:
17798 case MEM_REF:
17799 case INDIRECT_REF:
17800 CASE_CONVERT:
17801 case POINTER_PLUS_EXPR:
17802 v = TREE_OPERAND (v, 0);
17803 continue;
17804 case PARM_DECL:
17805 if (DECL_CONTEXT (v) == current_function_decl
17806 && DECL_ARTIFICIAL (v)
17807 && DECL_NAME (v) == this_identifier)
17808 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17809 /* FALLTHRU */
17810 default:
17811 v = NULL_TREE;
17812 break;
17813 }
17814 }
17815 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17816 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17817 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17818 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17819 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17820 {
17821 tree decl = OMP_CLAUSE_DECL (nc);
17822 if (VAR_P (decl))
17823 {
17824 retrofit_lang_decl (decl);
17825 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17826 }
17827 }
17828 break;
17829 default:
17830 break;
17831 }
17832 }
17833
17834 new_clauses = nreverse (new_clauses);
17835 if (ort != C_ORT_OMP_DECLARE_SIMD)
17836 {
17837 new_clauses = finish_omp_clauses (new_clauses, ort);
17838 if (linear_no_step)
17839 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17840 if (nc == linear_no_step)
17841 {
17842 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17843 break;
17844 }
17845 }
17846 return new_clauses;
17847 }
17848
17849 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17850
17851 static tree
17852 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17853 tree in_decl)
17854 {
17855 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17856
17857 tree purpose, value, chain;
17858
17859 if (t == NULL)
17860 return t;
17861
17862 if (TREE_CODE (t) != TREE_LIST)
17863 return tsubst_copy_and_build (t, args, complain, in_decl,
17864 /*function_p=*/false,
17865 /*integral_constant_expression_p=*/false);
17866
17867 if (t == void_list_node)
17868 return t;
17869
17870 purpose = TREE_PURPOSE (t);
17871 if (purpose)
17872 purpose = RECUR (purpose);
17873 value = TREE_VALUE (t);
17874 if (value)
17875 {
17876 if (TREE_CODE (value) != LABEL_DECL)
17877 value = RECUR (value);
17878 else
17879 {
17880 value = lookup_label (DECL_NAME (value));
17881 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17882 TREE_USED (value) = 1;
17883 }
17884 }
17885 chain = TREE_CHAIN (t);
17886 if (chain && chain != void_type_node)
17887 chain = RECUR (chain);
17888 return tree_cons (purpose, value, chain);
17889 #undef RECUR
17890 }
17891
17892 /* Used to temporarily communicate the list of #pragma omp parallel
17893 clauses to #pragma omp for instantiation if they are combined
17894 together. */
17895
17896 static tree *omp_parallel_combined_clauses;
17897
17898 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17899 tree *, unsigned int *);
17900
17901 /* Substitute one OMP_FOR iterator. */
17902
17903 static bool
17904 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17905 tree initv, tree condv, tree incrv, tree *clauses,
17906 tree args, tsubst_flags_t complain, tree in_decl,
17907 bool integral_constant_expression_p)
17908 {
17909 #define RECUR(NODE) \
17910 tsubst_expr ((NODE), args, complain, in_decl, \
17911 integral_constant_expression_p)
17912 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17913 bool ret = false;
17914
17915 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17916 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17917
17918 decl = TREE_OPERAND (init, 0);
17919 init = TREE_OPERAND (init, 1);
17920 tree decl_expr = NULL_TREE;
17921 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17922 if (range_for)
17923 {
17924 bool decomp = false;
17925 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17926 {
17927 tree v = DECL_VALUE_EXPR (decl);
17928 if (TREE_CODE (v) == ARRAY_REF
17929 && VAR_P (TREE_OPERAND (v, 0))
17930 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17931 {
17932 tree decomp_first = NULL_TREE;
17933 unsigned decomp_cnt = 0;
17934 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17935 maybe_push_decl (d);
17936 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17937 in_decl, &decomp_first, &decomp_cnt);
17938 decomp = true;
17939 if (d == error_mark_node)
17940 decl = error_mark_node;
17941 else
17942 for (unsigned int i = 0; i < decomp_cnt; i++)
17943 {
17944 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17945 {
17946 tree v = build_nt (ARRAY_REF, d,
17947 size_int (decomp_cnt - i - 1),
17948 NULL_TREE, NULL_TREE);
17949 SET_DECL_VALUE_EXPR (decomp_first, v);
17950 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17951 }
17952 fit_decomposition_lang_decl (decomp_first, d);
17953 decomp_first = DECL_CHAIN (decomp_first);
17954 }
17955 }
17956 }
17957 decl = tsubst_decl (decl, args, complain);
17958 if (!decomp)
17959 maybe_push_decl (decl);
17960 }
17961 else if (init && TREE_CODE (init) == DECL_EXPR)
17962 {
17963 /* We need to jump through some hoops to handle declarations in the
17964 init-statement, since we might need to handle auto deduction,
17965 but we need to keep control of initialization. */
17966 decl_expr = init;
17967 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17968 decl = tsubst_decl (decl, args, complain);
17969 }
17970 else
17971 {
17972 if (TREE_CODE (decl) == SCOPE_REF)
17973 {
17974 decl = RECUR (decl);
17975 if (TREE_CODE (decl) == COMPONENT_REF)
17976 {
17977 tree v = decl;
17978 while (v)
17979 switch (TREE_CODE (v))
17980 {
17981 case COMPONENT_REF:
17982 case MEM_REF:
17983 case INDIRECT_REF:
17984 CASE_CONVERT:
17985 case POINTER_PLUS_EXPR:
17986 v = TREE_OPERAND (v, 0);
17987 continue;
17988 case PARM_DECL:
17989 if (DECL_CONTEXT (v) == current_function_decl
17990 && DECL_ARTIFICIAL (v)
17991 && DECL_NAME (v) == this_identifier)
17992 {
17993 decl = TREE_OPERAND (decl, 1);
17994 decl = omp_privatize_field (decl, false);
17995 }
17996 /* FALLTHRU */
17997 default:
17998 v = NULL_TREE;
17999 break;
18000 }
18001 }
18002 }
18003 else
18004 decl = RECUR (decl);
18005 }
18006 if (init && TREE_CODE (init) == TREE_VEC)
18007 {
18008 init = copy_node (init);
18009 TREE_VEC_ELT (init, 0)
18010 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
18011 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
18012 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
18013 }
18014 else
18015 init = RECUR (init);
18016
18017 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
18018 {
18019 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
18020 if (TREE_CODE (o) == TREE_LIST)
18021 TREE_VEC_ELT (orig_declv, i)
18022 = tree_cons (RECUR (TREE_PURPOSE (o)),
18023 RECUR (TREE_VALUE (o)),
18024 NULL_TREE);
18025 else
18026 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
18027 }
18028
18029 if (range_for)
18030 {
18031 tree this_pre_body = NULL_TREE;
18032 tree orig_init = NULL_TREE;
18033 tree orig_decl = NULL_TREE;
18034 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
18035 orig_init, cond, incr);
18036 if (orig_decl)
18037 {
18038 if (orig_declv == NULL_TREE)
18039 orig_declv = copy_node (declv);
18040 TREE_VEC_ELT (orig_declv, i) = orig_decl;
18041 ret = true;
18042 }
18043 else if (orig_declv)
18044 TREE_VEC_ELT (orig_declv, i) = decl;
18045 }
18046
18047 tree auto_node = type_uses_auto (TREE_TYPE (decl));
18048 if (!range_for && auto_node && init)
18049 TREE_TYPE (decl)
18050 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
18051
18052 gcc_assert (!type_dependent_expression_p (decl));
18053
18054 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
18055 {
18056 if (decl_expr)
18057 {
18058 /* Declare the variable, but don't let that initialize it. */
18059 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
18060 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
18061 RECUR (decl_expr);
18062 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
18063 }
18064
18065 if (!range_for)
18066 {
18067 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18068 if (COMPARISON_CLASS_P (cond)
18069 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
18070 {
18071 tree lhs = RECUR (TREE_OPERAND (cond, 0));
18072 tree rhs = copy_node (TREE_OPERAND (cond, 1));
18073 TREE_VEC_ELT (rhs, 0)
18074 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
18075 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
18076 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
18077 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
18078 lhs, rhs);
18079 }
18080 else
18081 cond = RECUR (cond);
18082 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18083 if (TREE_CODE (incr) == MODIFY_EXPR)
18084 {
18085 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18086 tree rhs = RECUR (TREE_OPERAND (incr, 1));
18087 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
18088 NOP_EXPR, rhs, NULL_TREE, complain);
18089 }
18090 else
18091 incr = RECUR (incr);
18092 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18093 TREE_VEC_ELT (orig_declv, i) = decl;
18094 }
18095 TREE_VEC_ELT (declv, i) = decl;
18096 TREE_VEC_ELT (initv, i) = init;
18097 TREE_VEC_ELT (condv, i) = cond;
18098 TREE_VEC_ELT (incrv, i) = incr;
18099 return ret;
18100 }
18101
18102 if (decl_expr)
18103 {
18104 /* Declare and initialize the variable. */
18105 RECUR (decl_expr);
18106 init = NULL_TREE;
18107 }
18108 else if (init)
18109 {
18110 tree *pc;
18111 int j;
18112 for (j = ((omp_parallel_combined_clauses == NULL
18113 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
18114 {
18115 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
18116 {
18117 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
18118 && OMP_CLAUSE_DECL (*pc) == decl)
18119 break;
18120 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
18121 && OMP_CLAUSE_DECL (*pc) == decl)
18122 {
18123 if (j)
18124 break;
18125 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18126 tree c = *pc;
18127 *pc = OMP_CLAUSE_CHAIN (c);
18128 OMP_CLAUSE_CHAIN (c) = *clauses;
18129 *clauses = c;
18130 }
18131 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
18132 && OMP_CLAUSE_DECL (*pc) == decl)
18133 {
18134 error ("iteration variable %qD should not be firstprivate",
18135 decl);
18136 *pc = OMP_CLAUSE_CHAIN (*pc);
18137 }
18138 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
18139 && OMP_CLAUSE_DECL (*pc) == decl)
18140 {
18141 error ("iteration variable %qD should not be reduction",
18142 decl);
18143 *pc = OMP_CLAUSE_CHAIN (*pc);
18144 }
18145 else
18146 pc = &OMP_CLAUSE_CHAIN (*pc);
18147 }
18148 if (*pc)
18149 break;
18150 }
18151 if (*pc == NULL_TREE)
18152 {
18153 tree c = build_omp_clause (input_location,
18154 TREE_CODE (t) == OMP_LOOP
18155 ? OMP_CLAUSE_LASTPRIVATE
18156 : OMP_CLAUSE_PRIVATE);
18157 OMP_CLAUSE_DECL (c) = decl;
18158 c = finish_omp_clauses (c, C_ORT_OMP);
18159 if (c)
18160 {
18161 OMP_CLAUSE_CHAIN (c) = *clauses;
18162 *clauses = c;
18163 }
18164 }
18165 }
18166 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18167 if (COMPARISON_CLASS_P (cond))
18168 {
18169 tree op0 = RECUR (TREE_OPERAND (cond, 0));
18170 tree op1 = RECUR (TREE_OPERAND (cond, 1));
18171 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
18172 }
18173 else
18174 cond = RECUR (cond);
18175 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18176 switch (TREE_CODE (incr))
18177 {
18178 case PREINCREMENT_EXPR:
18179 case PREDECREMENT_EXPR:
18180 case POSTINCREMENT_EXPR:
18181 case POSTDECREMENT_EXPR:
18182 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
18183 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
18184 break;
18185 case MODIFY_EXPR:
18186 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18187 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18188 {
18189 tree rhs = TREE_OPERAND (incr, 1);
18190 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18191 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18192 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18193 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18194 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18195 rhs0, rhs1));
18196 }
18197 else
18198 incr = RECUR (incr);
18199 break;
18200 case MODOP_EXPR:
18201 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18202 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18203 {
18204 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18205 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18206 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
18207 TREE_TYPE (decl), lhs,
18208 RECUR (TREE_OPERAND (incr, 2))));
18209 }
18210 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
18211 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
18212 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
18213 {
18214 tree rhs = TREE_OPERAND (incr, 2);
18215 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18216 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18217 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18218 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18219 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18220 rhs0, rhs1));
18221 }
18222 else
18223 incr = RECUR (incr);
18224 break;
18225 default:
18226 incr = RECUR (incr);
18227 break;
18228 }
18229
18230 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18231 TREE_VEC_ELT (orig_declv, i) = decl;
18232 TREE_VEC_ELT (declv, i) = decl;
18233 TREE_VEC_ELT (initv, i) = init;
18234 TREE_VEC_ELT (condv, i) = cond;
18235 TREE_VEC_ELT (incrv, i) = incr;
18236 return false;
18237 #undef RECUR
18238 }
18239
18240 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18241 of OMP_TARGET's body. */
18242
18243 static tree
18244 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
18245 {
18246 *walk_subtrees = 0;
18247 switch (TREE_CODE (*tp))
18248 {
18249 case OMP_TEAMS:
18250 return *tp;
18251 case BIND_EXPR:
18252 case STATEMENT_LIST:
18253 *walk_subtrees = 1;
18254 break;
18255 default:
18256 break;
18257 }
18258 return NULL_TREE;
18259 }
18260
18261 /* Helper function for tsubst_expr. For decomposition declaration
18262 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18263 also the corresponding decls representing the identifiers
18264 of the decomposition declaration. Return DECL if successful
18265 or error_mark_node otherwise, set *FIRST to the first decl
18266 in the list chained through DECL_CHAIN and *CNT to the number
18267 of such decls. */
18268
18269 static tree
18270 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18271 tsubst_flags_t complain, tree in_decl, tree *first,
18272 unsigned int *cnt)
18273 {
18274 tree decl2, decl3, prev = decl;
18275 *cnt = 0;
18276 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18277 for (decl2 = DECL_CHAIN (pattern_decl);
18278 decl2
18279 && VAR_P (decl2)
18280 && DECL_DECOMPOSITION_P (decl2)
18281 && DECL_NAME (decl2);
18282 decl2 = DECL_CHAIN (decl2))
18283 {
18284 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
18285 {
18286 gcc_assert (errorcount);
18287 return error_mark_node;
18288 }
18289 (*cnt)++;
18290 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18291 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18292 tree v = DECL_VALUE_EXPR (decl2);
18293 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18294 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18295 decl3 = tsubst (decl2, args, complain, in_decl);
18296 SET_DECL_VALUE_EXPR (decl2, v);
18297 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18298 if (VAR_P (decl3))
18299 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18300 else
18301 {
18302 gcc_assert (errorcount);
18303 decl = error_mark_node;
18304 continue;
18305 }
18306 maybe_push_decl (decl3);
18307 if (error_operand_p (decl3))
18308 decl = error_mark_node;
18309 else if (decl != error_mark_node
18310 && DECL_CHAIN (decl3) != prev
18311 && decl != prev)
18312 {
18313 gcc_assert (errorcount);
18314 decl = error_mark_node;
18315 }
18316 else
18317 prev = decl3;
18318 }
18319 *first = prev;
18320 return decl;
18321 }
18322
18323 /* Return the proper local_specialization for init-capture pack DECL. */
18324
18325 static tree
18326 lookup_init_capture_pack (tree decl)
18327 {
18328 /* We handle normal pack captures by forwarding to the specialization of the
18329 captured parameter. We can't do that for pack init-captures; we need them
18330 to have their own local_specialization. We created the individual
18331 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18332 when we process the DECL_EXPR for the pack init-capture in the template.
18333 So, how do we find them? We don't know the capture proxy pack when
18334 building the individual resulting proxies, and we don't know the
18335 individual proxies when instantiating the pack. What we have in common is
18336 the FIELD_DECL.
18337
18338 So...when we instantiate the FIELD_DECL, we stick the result in
18339 local_specializations. Then at the DECL_EXPR we look up that result, see
18340 how many elements it has, synthesize the names, and look them up. */
18341
18342 tree cname = DECL_NAME (decl);
18343 tree val = DECL_VALUE_EXPR (decl);
18344 tree field = TREE_OPERAND (val, 1);
18345 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18346 tree fpack = retrieve_local_specialization (field);
18347 if (fpack == error_mark_node)
18348 return error_mark_node;
18349
18350 int len = 1;
18351 tree vec = NULL_TREE;
18352 tree r = NULL_TREE;
18353 if (TREE_CODE (fpack) == TREE_VEC)
18354 {
18355 len = TREE_VEC_LENGTH (fpack);
18356 vec = make_tree_vec (len);
18357 r = make_node (NONTYPE_ARGUMENT_PACK);
18358 SET_ARGUMENT_PACK_ARGS (r, vec);
18359 }
18360 for (int i = 0; i < len; ++i)
18361 {
18362 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18363 tree elt = lookup_name (ename);
18364 if (vec)
18365 TREE_VEC_ELT (vec, i) = elt;
18366 else
18367 r = elt;
18368 }
18369 return r;
18370 }
18371
18372 /* Like tsubst_copy for expressions, etc. but also does semantic
18373 processing. */
18374
18375 tree
18376 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18377 bool integral_constant_expression_p)
18378 {
18379 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18380 #define RECUR(NODE) \
18381 tsubst_expr ((NODE), args, complain, in_decl, \
18382 integral_constant_expression_p)
18383
18384 tree stmt, tmp;
18385 tree r;
18386 location_t loc;
18387
18388 if (t == NULL_TREE || t == error_mark_node)
18389 return t;
18390
18391 loc = input_location;
18392 if (location_t eloc = cp_expr_location (t))
18393 input_location = eloc;
18394 if (STATEMENT_CODE_P (TREE_CODE (t)))
18395 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18396
18397 switch (TREE_CODE (t))
18398 {
18399 case STATEMENT_LIST:
18400 {
18401 for (tree stmt : tsi_range (t))
18402 RECUR (stmt);
18403 break;
18404 }
18405
18406 case CTOR_INITIALIZER:
18407 finish_mem_initializers (tsubst_initializer_list
18408 (TREE_OPERAND (t, 0), args));
18409 break;
18410
18411 case RETURN_EXPR:
18412 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18413 break;
18414
18415 case CO_RETURN_EXPR:
18416 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18417 break;
18418
18419 case CO_YIELD_EXPR:
18420 stmt = finish_co_yield_expr (input_location,
18421 RECUR (TREE_OPERAND (t, 0)));
18422 RETURN (stmt);
18423
18424 case CO_AWAIT_EXPR:
18425 stmt = finish_co_await_expr (input_location,
18426 RECUR (TREE_OPERAND (t, 0)));
18427 RETURN (stmt);
18428
18429 case EXPR_STMT:
18430 tmp = RECUR (EXPR_STMT_EXPR (t));
18431 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18432 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18433 else
18434 finish_expr_stmt (tmp);
18435 break;
18436
18437 case USING_STMT:
18438 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18439 break;
18440
18441 case DECL_EXPR:
18442 {
18443 tree decl, pattern_decl;
18444 tree init;
18445
18446 pattern_decl = decl = DECL_EXPR_DECL (t);
18447 if (TREE_CODE (decl) == LABEL_DECL)
18448 finish_label_decl (DECL_NAME (decl));
18449 else if (TREE_CODE (decl) == USING_DECL)
18450 {
18451 tree scope = USING_DECL_SCOPE (decl);
18452 if (DECL_DEPENDENT_P (decl))
18453 {
18454 scope = tsubst (scope, args, complain, in_decl);
18455 if (!MAYBE_CLASS_TYPE_P (scope)
18456 && TREE_CODE (scope) != ENUMERAL_TYPE)
18457 {
18458 if (complain & tf_error)
18459 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18460 "class, namespace, or enumeration", scope);
18461 return error_mark_node;
18462 }
18463 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18464 }
18465 else
18466 {
18467 /* This is a non-dependent using-decl, and we'll have
18468 used the names it found during template parsing. We do
18469 not want to do the lookup again, because we might not
18470 find the things we found then. */
18471 gcc_checking_assert (scope == tsubst (scope, args,
18472 complain, in_decl));
18473 /* We still need to push the bindings so that we can look up
18474 this name later. */
18475 push_using_decl_bindings (DECL_NAME (decl),
18476 USING_DECL_DECLS (decl));
18477 }
18478 }
18479 else if (is_capture_proxy (decl)
18480 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18481 {
18482 /* We're in tsubst_lambda_expr, we've already inserted a new
18483 capture proxy, so look it up and register it. */
18484 tree inst;
18485 if (!DECL_PACK_P (decl))
18486 {
18487 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18488 LOOK_want::HIDDEN_LAMBDA);
18489 gcc_assert (inst != decl && is_capture_proxy (inst));
18490 }
18491 else if (is_normal_capture_proxy (decl))
18492 {
18493 inst = (retrieve_local_specialization
18494 (DECL_CAPTURED_VARIABLE (decl)));
18495 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18496 || DECL_PACK_P (inst));
18497 }
18498 else
18499 inst = lookup_init_capture_pack (decl);
18500
18501 register_local_specialization (inst, decl);
18502 break;
18503 }
18504 else if (DECL_PRETTY_FUNCTION_P (decl))
18505 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18506 DECL_NAME (decl),
18507 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18508 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18509 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18510 /* Don't copy the old closure; we'll create a new one in
18511 tsubst_lambda_expr. */
18512 break;
18513 else
18514 {
18515 init = DECL_INITIAL (decl);
18516 decl = tsubst (decl, args, complain, in_decl);
18517 if (decl != error_mark_node)
18518 {
18519 /* By marking the declaration as instantiated, we avoid
18520 trying to instantiate it. Since instantiate_decl can't
18521 handle local variables, and since we've already done
18522 all that needs to be done, that's the right thing to
18523 do. */
18524 if (VAR_P (decl))
18525 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18526 if (VAR_P (decl) && !DECL_NAME (decl)
18527 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18528 /* Anonymous aggregates are a special case. */
18529 finish_anon_union (decl);
18530 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18531 {
18532 DECL_CONTEXT (decl) = current_function_decl;
18533 if (DECL_NAME (decl) == this_identifier)
18534 {
18535 tree lam = DECL_CONTEXT (current_function_decl);
18536 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18537 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18538 }
18539 insert_capture_proxy (decl);
18540 }
18541 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18542 /* We already did a pushtag. */;
18543 else if (VAR_OR_FUNCTION_DECL_P (decl)
18544 && DECL_LOCAL_DECL_P (decl))
18545 {
18546 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18547 DECL_CONTEXT (decl) = NULL_TREE;
18548 decl = pushdecl (decl);
18549 if (TREE_CODE (decl) == FUNCTION_DECL
18550 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18551 && cp_check_omp_declare_reduction (decl))
18552 instantiate_body (pattern_decl, args, decl, true);
18553 }
18554 else
18555 {
18556 bool const_init = false;
18557 unsigned int cnt = 0;
18558 tree first = NULL_TREE, ndecl = error_mark_node;
18559 tree asmspec_tree = NULL_TREE;
18560 maybe_push_decl (decl);
18561
18562 if (VAR_P (decl)
18563 && DECL_DECOMPOSITION_P (decl)
18564 && TREE_TYPE (pattern_decl) != error_mark_node)
18565 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18566 complain, in_decl, &first,
18567 &cnt);
18568
18569 init = tsubst_init (init, decl, args, complain, in_decl);
18570
18571 if (VAR_P (decl))
18572 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18573 (pattern_decl));
18574
18575 if (ndecl != error_mark_node)
18576 cp_maybe_mangle_decomp (ndecl, first, cnt);
18577
18578 /* In a non-template function, VLA type declarations are
18579 handled in grokdeclarator; for templates, handle them
18580 now. */
18581 predeclare_vla (decl);
18582
18583 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18584 {
18585 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18586 const char *asmspec = IDENTIFIER_POINTER (id);
18587 gcc_assert (asmspec[0] == '*');
18588 asmspec_tree
18589 = build_string (IDENTIFIER_LENGTH (id) - 1,
18590 asmspec + 1);
18591 TREE_TYPE (asmspec_tree) = char_array_type_node;
18592 }
18593
18594 cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
18595
18596 if (ndecl != error_mark_node)
18597 cp_finish_decomp (ndecl, first, cnt);
18598 }
18599 }
18600 }
18601
18602 break;
18603 }
18604
18605 case FOR_STMT:
18606 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18607 RECUR (FOR_INIT_STMT (t));
18608 finish_init_stmt (stmt);
18609 tmp = RECUR (FOR_COND (t));
18610 finish_for_cond (tmp, stmt, false, 0);
18611 tmp = RECUR (FOR_EXPR (t));
18612 finish_for_expr (tmp, stmt);
18613 {
18614 bool prev = note_iteration_stmt_body_start ();
18615 RECUR (FOR_BODY (t));
18616 note_iteration_stmt_body_end (prev);
18617 }
18618 finish_for_stmt (stmt);
18619 break;
18620
18621 case RANGE_FOR_STMT:
18622 {
18623 /* Construct another range_for, if this is not a final
18624 substitution (for inside a generic lambda of a
18625 template). Otherwise convert to a regular for. */
18626 tree decl, expr;
18627 stmt = (processing_template_decl
18628 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18629 : begin_for_stmt (NULL_TREE, NULL_TREE));
18630 RECUR (RANGE_FOR_INIT_STMT (t));
18631 decl = RANGE_FOR_DECL (t);
18632 decl = tsubst (decl, args, complain, in_decl);
18633 maybe_push_decl (decl);
18634 expr = RECUR (RANGE_FOR_EXPR (t));
18635
18636 tree decomp_first = NULL_TREE;
18637 unsigned decomp_cnt = 0;
18638 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18639 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18640 complain, in_decl,
18641 &decomp_first, &decomp_cnt);
18642
18643 if (processing_template_decl)
18644 {
18645 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18646 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18647 finish_range_for_decl (stmt, decl, expr);
18648 if (decomp_first && decl != error_mark_node)
18649 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18650 }
18651 else
18652 {
18653 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18654 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18655 stmt = cp_convert_range_for (stmt, decl, expr,
18656 decomp_first, decomp_cnt,
18657 RANGE_FOR_IVDEP (t), unroll);
18658 }
18659
18660 bool prev = note_iteration_stmt_body_start ();
18661 RECUR (RANGE_FOR_BODY (t));
18662 note_iteration_stmt_body_end (prev);
18663 finish_for_stmt (stmt);
18664 }
18665 break;
18666
18667 case WHILE_STMT:
18668 stmt = begin_while_stmt ();
18669 tmp = RECUR (WHILE_COND (t));
18670 finish_while_stmt_cond (tmp, stmt, false, 0);
18671 {
18672 bool prev = note_iteration_stmt_body_start ();
18673 RECUR (WHILE_BODY (t));
18674 note_iteration_stmt_body_end (prev);
18675 }
18676 finish_while_stmt (stmt);
18677 break;
18678
18679 case DO_STMT:
18680 stmt = begin_do_stmt ();
18681 {
18682 bool prev = note_iteration_stmt_body_start ();
18683 RECUR (DO_BODY (t));
18684 note_iteration_stmt_body_end (prev);
18685 }
18686 finish_do_body (stmt);
18687 tmp = RECUR (DO_COND (t));
18688 finish_do_stmt (tmp, stmt, false, 0);
18689 break;
18690
18691 case IF_STMT:
18692 stmt = begin_if_stmt ();
18693 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18694 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
18695 if (IF_STMT_CONSTEXPR_P (t))
18696 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
18697 tmp = RECUR (IF_COND (t));
18698 tmp = finish_if_stmt_cond (tmp, stmt);
18699 if (IF_STMT_CONSTEXPR_P (t)
18700 && instantiation_dependent_expression_p (tmp))
18701 {
18702 /* We're partially instantiating a generic lambda, but the condition
18703 of the constexpr if is still dependent. Don't substitute into the
18704 branches now, just remember the template arguments. */
18705 do_poplevel (IF_SCOPE (stmt));
18706 IF_COND (stmt) = IF_COND (t);
18707 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18708 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18709 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18710 add_stmt (stmt);
18711 break;
18712 }
18713 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18714 /* Don't instantiate the THEN_CLAUSE. */;
18715 else if (IF_STMT_CONSTEVAL_P (t))
18716 {
18717 bool save_in_consteval_if_p = in_consteval_if_p;
18718 in_consteval_if_p = true;
18719 RECUR (THEN_CLAUSE (t));
18720 in_consteval_if_p = save_in_consteval_if_p;
18721 }
18722 else
18723 {
18724 tree folded = fold_non_dependent_expr (tmp, complain);
18725 bool inhibit = integer_zerop (folded);
18726 if (inhibit)
18727 ++c_inhibit_evaluation_warnings;
18728 RECUR (THEN_CLAUSE (t));
18729 if (inhibit)
18730 --c_inhibit_evaluation_warnings;
18731 }
18732 finish_then_clause (stmt);
18733
18734 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18735 /* Don't instantiate the ELSE_CLAUSE. */;
18736 else if (ELSE_CLAUSE (t))
18737 {
18738 tree folded = fold_non_dependent_expr (tmp, complain);
18739 bool inhibit = integer_nonzerop (folded);
18740 begin_else_clause (stmt);
18741 if (inhibit)
18742 ++c_inhibit_evaluation_warnings;
18743 RECUR (ELSE_CLAUSE (t));
18744 if (inhibit)
18745 --c_inhibit_evaluation_warnings;
18746 finish_else_clause (stmt);
18747 }
18748
18749 finish_if_stmt (stmt);
18750 break;
18751
18752 case BIND_EXPR:
18753 if (BIND_EXPR_BODY_BLOCK (t))
18754 stmt = begin_function_body ();
18755 else
18756 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18757 ? BCS_TRY_BLOCK : 0);
18758
18759 RECUR (BIND_EXPR_BODY (t));
18760
18761 if (BIND_EXPR_BODY_BLOCK (t))
18762 finish_function_body (stmt);
18763 else
18764 finish_compound_stmt (stmt);
18765 break;
18766
18767 case BREAK_STMT:
18768 finish_break_stmt ();
18769 break;
18770
18771 case CONTINUE_STMT:
18772 finish_continue_stmt ();
18773 break;
18774
18775 case SWITCH_STMT:
18776 stmt = begin_switch_stmt ();
18777 tmp = RECUR (SWITCH_STMT_COND (t));
18778 finish_switch_cond (tmp, stmt);
18779 RECUR (SWITCH_STMT_BODY (t));
18780 finish_switch_stmt (stmt);
18781 break;
18782
18783 case CASE_LABEL_EXPR:
18784 {
18785 tree decl = CASE_LABEL (t);
18786 tree low = RECUR (CASE_LOW (t));
18787 tree high = RECUR (CASE_HIGH (t));
18788 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18789 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18790 {
18791 tree label = CASE_LABEL (l);
18792 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18793 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18794 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18795 }
18796 }
18797 break;
18798
18799 case LABEL_EXPR:
18800 {
18801 tree decl = LABEL_EXPR_LABEL (t);
18802 tree label;
18803
18804 label = finish_label_stmt (DECL_NAME (decl));
18805 if (TREE_CODE (label) == LABEL_DECL)
18806 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18807 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18808 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18809 }
18810 break;
18811
18812 case GOTO_EXPR:
18813 tmp = GOTO_DESTINATION (t);
18814 if (TREE_CODE (tmp) != LABEL_DECL)
18815 /* Computed goto's must be tsubst'd into. On the other hand,
18816 non-computed gotos must not be; the identifier in question
18817 will have no binding. */
18818 tmp = RECUR (tmp);
18819 else
18820 tmp = DECL_NAME (tmp);
18821 finish_goto_stmt (tmp);
18822 break;
18823
18824 case ASM_EXPR:
18825 {
18826 tree string = RECUR (ASM_STRING (t));
18827 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18828 complain, in_decl);
18829 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18830 complain, in_decl);
18831 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18832 complain, in_decl);
18833 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18834 complain, in_decl);
18835 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18836 outputs, inputs, clobbers, labels,
18837 ASM_INLINE_P (t));
18838 tree asm_expr = tmp;
18839 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18840 asm_expr = TREE_OPERAND (asm_expr, 0);
18841 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18842 }
18843 break;
18844
18845 case TRY_BLOCK:
18846 if (CLEANUP_P (t))
18847 {
18848 stmt = begin_try_block ();
18849 RECUR (TRY_STMTS (t));
18850 finish_cleanup_try_block (stmt);
18851 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18852 }
18853 else
18854 {
18855 tree compound_stmt = NULL_TREE;
18856
18857 if (FN_TRY_BLOCK_P (t))
18858 stmt = begin_function_try_block (&compound_stmt);
18859 else
18860 stmt = begin_try_block ();
18861
18862 RECUR (TRY_STMTS (t));
18863
18864 if (FN_TRY_BLOCK_P (t))
18865 finish_function_try_block (stmt);
18866 else
18867 finish_try_block (stmt);
18868
18869 RECUR (TRY_HANDLERS (t));
18870 if (FN_TRY_BLOCK_P (t))
18871 finish_function_handler_sequence (stmt, compound_stmt);
18872 else
18873 finish_handler_sequence (stmt);
18874 }
18875 break;
18876
18877 case HANDLER:
18878 {
18879 tree decl = HANDLER_PARMS (t);
18880
18881 if (decl)
18882 {
18883 decl = tsubst (decl, args, complain, in_decl);
18884 /* Prevent instantiate_decl from trying to instantiate
18885 this variable. We've already done all that needs to be
18886 done. */
18887 if (decl != error_mark_node)
18888 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18889 }
18890 stmt = begin_handler ();
18891 finish_handler_parms (decl, stmt);
18892 RECUR (HANDLER_BODY (t));
18893 finish_handler (stmt);
18894 }
18895 break;
18896
18897 case TAG_DEFN:
18898 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18899 if (CLASS_TYPE_P (tmp))
18900 {
18901 /* Local classes are not independent templates; they are
18902 instantiated along with their containing function. And this
18903 way we don't have to deal with pushing out of one local class
18904 to instantiate a member of another local class. */
18905 /* Closures are handled by the LAMBDA_EXPR. */
18906 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18907 complete_type (tmp);
18908 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18909 if ((VAR_P (fld)
18910 || (TREE_CODE (fld) == FUNCTION_DECL
18911 && !DECL_ARTIFICIAL (fld)))
18912 && DECL_TEMPLATE_INSTANTIATION (fld))
18913 instantiate_decl (fld, /*defer_ok=*/false,
18914 /*expl_inst_class=*/false);
18915 }
18916 break;
18917
18918 case STATIC_ASSERT:
18919 {
18920 tree condition;
18921
18922 ++c_inhibit_evaluation_warnings;
18923 condition =
18924 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18925 args,
18926 complain, in_decl,
18927 /*integral_constant_expression_p=*/true);
18928 --c_inhibit_evaluation_warnings;
18929
18930 finish_static_assert (condition,
18931 STATIC_ASSERT_MESSAGE (t),
18932 STATIC_ASSERT_SOURCE_LOCATION (t),
18933 /*member_p=*/false, /*show_expr_p=*/true);
18934 }
18935 break;
18936
18937 case OACC_KERNELS:
18938 case OACC_PARALLEL:
18939 case OACC_SERIAL:
18940 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18941 in_decl);
18942 stmt = begin_omp_parallel ();
18943 RECUR (OMP_BODY (t));
18944 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18945 break;
18946
18947 case OMP_PARALLEL:
18948 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18949 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18950 complain, in_decl);
18951 if (OMP_PARALLEL_COMBINED (t))
18952 omp_parallel_combined_clauses = &tmp;
18953 stmt = begin_omp_parallel ();
18954 RECUR (OMP_PARALLEL_BODY (t));
18955 gcc_assert (omp_parallel_combined_clauses == NULL);
18956 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18957 = OMP_PARALLEL_COMBINED (t);
18958 pop_omp_privatization_clauses (r);
18959 break;
18960
18961 case OMP_TASK:
18962 if (OMP_TASK_BODY (t) == NULL_TREE)
18963 {
18964 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18965 complain, in_decl);
18966 t = copy_node (t);
18967 OMP_TASK_CLAUSES (t) = tmp;
18968 add_stmt (t);
18969 break;
18970 }
18971 r = push_omp_privatization_clauses (false);
18972 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18973 complain, in_decl);
18974 stmt = begin_omp_task ();
18975 RECUR (OMP_TASK_BODY (t));
18976 finish_omp_task (tmp, stmt);
18977 pop_omp_privatization_clauses (r);
18978 break;
18979
18980 case OMP_FOR:
18981 case OMP_LOOP:
18982 case OMP_SIMD:
18983 case OMP_DISTRIBUTE:
18984 case OMP_TASKLOOP:
18985 case OACC_LOOP:
18986 {
18987 tree clauses, body, pre_body;
18988 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18989 tree orig_declv = NULL_TREE;
18990 tree incrv = NULL_TREE;
18991 enum c_omp_region_type ort = C_ORT_OMP;
18992 bool any_range_for = false;
18993 int i;
18994
18995 if (TREE_CODE (t) == OACC_LOOP)
18996 ort = C_ORT_ACC;
18997
18998 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18999 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
19000 in_decl);
19001 if (OMP_FOR_INIT (t) != NULL_TREE)
19002 {
19003 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19004 if (OMP_FOR_ORIG_DECLS (t))
19005 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19006 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19007 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19008 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19009 }
19010
19011 keep_next_level (true);
19012 stmt = begin_omp_structured_block ();
19013
19014 pre_body = push_stmt_list ();
19015 RECUR (OMP_FOR_PRE_BODY (t));
19016 pre_body = pop_stmt_list (pre_body);
19017
19018 if (OMP_FOR_INIT (t) != NULL_TREE)
19019 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19020 any_range_for
19021 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
19022 condv, incrv, &clauses, args,
19023 complain, in_decl,
19024 integral_constant_expression_p);
19025 omp_parallel_combined_clauses = NULL;
19026
19027 if (any_range_for)
19028 {
19029 gcc_assert (orig_declv);
19030 body = begin_omp_structured_block ();
19031 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19032 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
19033 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
19034 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
19035 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
19036 TREE_VEC_ELT (declv, i));
19037 }
19038 else
19039 body = push_stmt_list ();
19040 RECUR (OMP_FOR_BODY (t));
19041 if (any_range_for)
19042 body = finish_omp_structured_block (body);
19043 else
19044 body = pop_stmt_list (body);
19045
19046 if (OMP_FOR_INIT (t) != NULL_TREE)
19047 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
19048 orig_declv, initv, condv, incrv, body, pre_body,
19049 NULL, clauses);
19050 else
19051 {
19052 t = make_node (TREE_CODE (t));
19053 TREE_TYPE (t) = void_type_node;
19054 OMP_FOR_BODY (t) = body;
19055 OMP_FOR_PRE_BODY (t) = pre_body;
19056 OMP_FOR_CLAUSES (t) = clauses;
19057 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
19058 add_stmt (t);
19059 }
19060
19061 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
19062 t));
19063 pop_omp_privatization_clauses (r);
19064 }
19065 break;
19066
19067 case OMP_SECTIONS:
19068 case OMP_MASKED:
19069 omp_parallel_combined_clauses = NULL;
19070 /* FALLTHRU */
19071 case OMP_SINGLE:
19072 case OMP_SCOPE:
19073 case OMP_TEAMS:
19074 case OMP_CRITICAL:
19075 case OMP_TASKGROUP:
19076 case OMP_SCAN:
19077 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
19078 && OMP_TEAMS_COMBINED (t));
19079 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
19080 in_decl);
19081 if (TREE_CODE (t) == OMP_TEAMS)
19082 {
19083 keep_next_level (true);
19084 stmt = begin_omp_structured_block ();
19085 RECUR (OMP_BODY (t));
19086 stmt = finish_omp_structured_block (stmt);
19087 }
19088 else
19089 {
19090 stmt = push_stmt_list ();
19091 RECUR (OMP_BODY (t));
19092 stmt = pop_stmt_list (stmt);
19093 }
19094
19095 if (TREE_CODE (t) == OMP_CRITICAL
19096 && tmp != NULL_TREE
19097 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
19098 {
19099 error_at (OMP_CLAUSE_LOCATION (tmp),
19100 "%<#pragma omp critical%> with %<hint%> clause requires "
19101 "a name, except when %<omp_sync_hint_none%> is used");
19102 RETURN (error_mark_node);
19103 }
19104 t = copy_node (t);
19105 OMP_BODY (t) = stmt;
19106 OMP_CLAUSES (t) = tmp;
19107 add_stmt (t);
19108 pop_omp_privatization_clauses (r);
19109 break;
19110
19111 case OMP_DEPOBJ:
19112 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
19113 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
19114 {
19115 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
19116 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
19117 {
19118 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
19119 args, complain, in_decl);
19120 if (tmp == NULL_TREE)
19121 tmp = error_mark_node;
19122 }
19123 else
19124 {
19125 kind = (enum omp_clause_depend_kind)
19126 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
19127 tmp = NULL_TREE;
19128 }
19129 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
19130 }
19131 else
19132 finish_omp_depobj (EXPR_LOCATION (t), r,
19133 OMP_CLAUSE_DEPEND_SOURCE,
19134 OMP_DEPOBJ_CLAUSES (t));
19135 break;
19136
19137 case OACC_DATA:
19138 case OMP_TARGET_DATA:
19139 case OMP_TARGET:
19140 tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
19141 TREE_CODE (t) == OACC_DATA
19142 ? C_ORT_ACC
19143 : TREE_CODE (t) == OMP_TARGET
19144 ? C_ORT_OMP_TARGET : C_ORT_OMP,
19145 args, complain, in_decl);
19146 keep_next_level (true);
19147 stmt = begin_omp_structured_block ();
19148
19149 RECUR (OMP_BODY (t));
19150 stmt = finish_omp_structured_block (stmt);
19151
19152 t = copy_node (t);
19153 OMP_BODY (t) = stmt;
19154 OMP_CLAUSES (t) = tmp;
19155
19156 if (TREE_CODE (t) == OMP_TARGET)
19157 finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
19158 &OMP_CLAUSES (t));
19159
19160 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
19161 {
19162 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
19163 if (teams)
19164 /* For combined target teams, ensure the num_teams and
19165 thread_limit clause expressions are evaluated on the host,
19166 before entering the target construct. */
19167 for (tree c = OMP_TEAMS_CLAUSES (teams);
19168 c; c = OMP_CLAUSE_CHAIN (c))
19169 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19170 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19171 for (int i = 0;
19172 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
19173 if (OMP_CLAUSE_OPERAND (c, i)
19174 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
19175 {
19176 tree expr = OMP_CLAUSE_OPERAND (c, i);
19177 expr = force_target_expr (TREE_TYPE (expr), expr,
19178 tf_none);
19179 if (expr == error_mark_node)
19180 continue;
19181 tmp = TARGET_EXPR_SLOT (expr);
19182 add_stmt (expr);
19183 OMP_CLAUSE_OPERAND (c, i) = expr;
19184 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19185 OMP_CLAUSE_FIRSTPRIVATE);
19186 OMP_CLAUSE_DECL (tc) = tmp;
19187 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
19188 OMP_TARGET_CLAUSES (t) = tc;
19189 }
19190 }
19191 add_stmt (t);
19192 break;
19193
19194 case OACC_DECLARE:
19195 t = copy_node (t);
19196 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
19197 complain, in_decl);
19198 OACC_DECLARE_CLAUSES (t) = tmp;
19199 add_stmt (t);
19200 break;
19201
19202 case OMP_TARGET_UPDATE:
19203 case OMP_TARGET_ENTER_DATA:
19204 case OMP_TARGET_EXIT_DATA:
19205 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
19206 complain, in_decl);
19207 t = copy_node (t);
19208 OMP_STANDALONE_CLAUSES (t) = tmp;
19209 add_stmt (t);
19210 break;
19211
19212 case OACC_CACHE:
19213 case OACC_ENTER_DATA:
19214 case OACC_EXIT_DATA:
19215 case OACC_UPDATE:
19216 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
19217 complain, in_decl);
19218 t = copy_node (t);
19219 OMP_STANDALONE_CLAUSES (t) = tmp;
19220 add_stmt (t);
19221 break;
19222
19223 case OMP_ORDERED:
19224 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
19225 complain, in_decl);
19226 stmt = push_stmt_list ();
19227 RECUR (OMP_BODY (t));
19228 stmt = pop_stmt_list (stmt);
19229
19230 t = copy_node (t);
19231 OMP_BODY (t) = stmt;
19232 OMP_ORDERED_CLAUSES (t) = tmp;
19233 add_stmt (t);
19234 break;
19235
19236 case OMP_MASTER:
19237 omp_parallel_combined_clauses = NULL;
19238 /* FALLTHRU */
19239 case OMP_SECTION:
19240 stmt = push_stmt_list ();
19241 RECUR (OMP_BODY (t));
19242 stmt = pop_stmt_list (stmt);
19243
19244 t = copy_node (t);
19245 OMP_BODY (t) = stmt;
19246 add_stmt (t);
19247 break;
19248
19249 case OMP_ATOMIC:
19250 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
19251 tmp = NULL_TREE;
19252 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
19253 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
19254 complain, in_decl);
19255 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
19256 {
19257 tree op1 = TREE_OPERAND (t, 1);
19258 tree rhs1 = NULL_TREE;
19259 tree r = NULL_TREE;
19260 tree lhs, rhs;
19261 if (TREE_CODE (op1) == COMPOUND_EXPR)
19262 {
19263 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19264 op1 = TREE_OPERAND (op1, 1);
19265 }
19266 if (TREE_CODE (op1) == COND_EXPR)
19267 {
19268 gcc_assert (rhs1 == NULL_TREE);
19269 tree c = TREE_OPERAND (op1, 0);
19270 if (TREE_CODE (c) == MODIFY_EXPR)
19271 {
19272 r = RECUR (TREE_OPERAND (c, 0));
19273 c = TREE_OPERAND (c, 1);
19274 }
19275 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19276 rhs = RECUR (TREE_OPERAND (c, 1));
19277 lhs = RECUR (TREE_OPERAND (op1, 2));
19278 rhs1 = RECUR (TREE_OPERAND (op1, 1));
19279 }
19280 else
19281 {
19282 lhs = RECUR (TREE_OPERAND (op1, 0));
19283 rhs = RECUR (TREE_OPERAND (op1, 1));
19284 }
19285 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19286 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
19287 tmp, OMP_ATOMIC_MEMORY_ORDER (t),
19288 OMP_ATOMIC_WEAK (t));
19289 }
19290 else
19291 {
19292 tree op1 = TREE_OPERAND (t, 1);
19293 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19294 tree rhs1 = NULL_TREE, r = NULL_TREE;
19295 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19296 enum tree_code opcode = NOP_EXPR;
19297 if (code == OMP_ATOMIC_READ)
19298 {
19299 v = RECUR (TREE_OPERAND (op1, 0));
19300 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19301 }
19302 else if (code == OMP_ATOMIC_CAPTURE_OLD
19303 || code == OMP_ATOMIC_CAPTURE_NEW)
19304 {
19305 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19306 v = RECUR (TREE_OPERAND (op1, 0));
19307 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19308 if (TREE_CODE (op11) == COMPOUND_EXPR)
19309 {
19310 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19311 op11 = TREE_OPERAND (op11, 1);
19312 }
19313 if (TREE_CODE (op11) == COND_EXPR)
19314 {
19315 gcc_assert (rhs1 == NULL_TREE);
19316 tree c = TREE_OPERAND (op11, 0);
19317 if (TREE_CODE (c) == MODIFY_EXPR)
19318 {
19319 r = RECUR (TREE_OPERAND (c, 0));
19320 c = TREE_OPERAND (c, 1);
19321 }
19322 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19323 rhs = RECUR (TREE_OPERAND (c, 1));
19324 lhs = RECUR (TREE_OPERAND (op11, 2));
19325 rhs1 = RECUR (TREE_OPERAND (op11, 1));
19326 }
19327 else
19328 {
19329 lhs = RECUR (TREE_OPERAND (op11, 0));
19330 rhs = RECUR (TREE_OPERAND (op11, 1));
19331 }
19332 opcode = TREE_CODE (op11);
19333 if (opcode == MODIFY_EXPR)
19334 opcode = NOP_EXPR;
19335 }
19336 else
19337 {
19338 code = OMP_ATOMIC;
19339 lhs = RECUR (TREE_OPERAND (op1, 0));
19340 rhs = RECUR (TREE_OPERAND (op1, 1));
19341 }
19342 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19343 lhs1, rhs1, r, tmp,
19344 OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
19345 }
19346 break;
19347
19348 case TRANSACTION_EXPR:
19349 {
19350 int flags = 0;
19351 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19352 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19353
19354 if (TRANSACTION_EXPR_IS_STMT (t))
19355 {
19356 tree body = TRANSACTION_EXPR_BODY (t);
19357 tree noex = NULL_TREE;
19358 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19359 {
19360 noex = MUST_NOT_THROW_COND (body);
19361 if (noex == NULL_TREE)
19362 noex = boolean_true_node;
19363 body = TREE_OPERAND (body, 0);
19364 }
19365 stmt = begin_transaction_stmt (input_location, NULL, flags);
19366 RECUR (body);
19367 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19368 }
19369 else
19370 {
19371 stmt = build_transaction_expr (EXPR_LOCATION (t),
19372 RECUR (TRANSACTION_EXPR_BODY (t)),
19373 flags, NULL_TREE);
19374 RETURN (stmt);
19375 }
19376 }
19377 break;
19378
19379 case MUST_NOT_THROW_EXPR:
19380 {
19381 tree op0 = RECUR (TREE_OPERAND (t, 0));
19382 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19383 RETURN (build_must_not_throw_expr (op0, cond));
19384 }
19385
19386 case EXPR_PACK_EXPANSION:
19387 error ("invalid use of pack expansion expression");
19388 RETURN (error_mark_node);
19389
19390 case NONTYPE_ARGUMENT_PACK:
19391 error ("use %<...%> to expand argument pack");
19392 RETURN (error_mark_node);
19393
19394 case COMPOUND_EXPR:
19395 tmp = RECUR (TREE_OPERAND (t, 0));
19396 if (tmp == NULL_TREE)
19397 /* If the first operand was a statement, we're done with it. */
19398 RETURN (RECUR (TREE_OPERAND (t, 1)));
19399 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19400 RECUR (TREE_OPERAND (t, 1)),
19401 templated_operator_saved_lookups (t),
19402 complain));
19403
19404 case ANNOTATE_EXPR:
19405 tmp = RECUR (TREE_OPERAND (t, 0));
19406 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19407 TREE_TYPE (tmp), tmp,
19408 RECUR (TREE_OPERAND (t, 1)),
19409 RECUR (TREE_OPERAND (t, 2))));
19410
19411 case PREDICT_EXPR:
19412 RETURN (add_stmt (copy_node (t)));
19413
19414 default:
19415 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19416
19417 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19418 /*function_p=*/false,
19419 integral_constant_expression_p));
19420 }
19421
19422 RETURN (NULL_TREE);
19423 out:
19424 input_location = loc;
19425 return r;
19426 #undef RECUR
19427 #undef RETURN
19428 }
19429
19430 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19431 function. For description of the body see comment above
19432 cp_parser_omp_declare_reduction_exprs. */
19433
19434 static void
19435 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19436 {
19437 if (t == NULL_TREE || t == error_mark_node)
19438 return;
19439
19440 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19441
19442 tree_stmt_iterator tsi;
19443 int i;
19444 tree stmts[7];
19445 memset (stmts, 0, sizeof stmts);
19446 for (i = 0, tsi = tsi_start (t);
19447 i < 7 && !tsi_end_p (tsi);
19448 i++, tsi_next (&tsi))
19449 stmts[i] = tsi_stmt (tsi);
19450 gcc_assert (tsi_end_p (tsi));
19451
19452 if (i >= 3)
19453 {
19454 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19455 && TREE_CODE (stmts[1]) == DECL_EXPR);
19456 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19457 args, complain, in_decl);
19458 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19459 args, complain, in_decl);
19460 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19461 expect to be pushing it. */
19462 DECL_CONTEXT (omp_out) = current_function_decl;
19463 DECL_CONTEXT (omp_in) = current_function_decl;
19464 keep_next_level (true);
19465 tree block = begin_omp_structured_block ();
19466 tsubst_expr (stmts[2], args, complain, in_decl, false);
19467 block = finish_omp_structured_block (block);
19468 block = maybe_cleanup_point_expr_void (block);
19469 add_decl_expr (omp_out);
19470 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
19471 add_decl_expr (omp_in);
19472 finish_expr_stmt (block);
19473 }
19474 if (i >= 6)
19475 {
19476 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19477 && TREE_CODE (stmts[4]) == DECL_EXPR);
19478 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19479 args, complain, in_decl);
19480 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19481 args, complain, in_decl);
19482 DECL_CONTEXT (omp_priv) = current_function_decl;
19483 DECL_CONTEXT (omp_orig) = current_function_decl;
19484 keep_next_level (true);
19485 tree block = begin_omp_structured_block ();
19486 tsubst_expr (stmts[5], args, complain, in_decl, false);
19487 block = finish_omp_structured_block (block);
19488 block = maybe_cleanup_point_expr_void (block);
19489 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19490 add_decl_expr (omp_priv);
19491 add_decl_expr (omp_orig);
19492 finish_expr_stmt (block);
19493 if (i == 7)
19494 add_decl_expr (omp_orig);
19495 }
19496 }
19497
19498 /* T is a postfix-expression that is not being used in a function
19499 call. Return the substituted version of T. */
19500
19501 static tree
19502 tsubst_non_call_postfix_expression (tree t, tree args,
19503 tsubst_flags_t complain,
19504 tree in_decl)
19505 {
19506 if (TREE_CODE (t) == SCOPE_REF)
19507 t = tsubst_qualified_id (t, args, complain, in_decl,
19508 /*done=*/false, /*address_p=*/false);
19509 else
19510 t = tsubst_copy_and_build (t, args, complain, in_decl,
19511 /*function_p=*/false,
19512 /*integral_constant_expression_p=*/false);
19513
19514 return t;
19515 }
19516
19517 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19518 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19519 dependent init-capture. */
19520
19521 static void
19522 prepend_one_capture (tree field, tree init, tree &list,
19523 tsubst_flags_t complain)
19524 {
19525 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19526 {
19527 tree type = NULL_TREE;
19528 if (!init)
19529 {
19530 if (complain & tf_error)
19531 error ("empty initializer in lambda init-capture");
19532 init = error_mark_node;
19533 }
19534 else if (TREE_CODE (init) == TREE_LIST)
19535 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19536 if (!type)
19537 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19538 TREE_TYPE (field) = type;
19539 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19540 }
19541 list = tree_cons (field, init, list);
19542 }
19543
19544 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19545 instantiation context. Instantiating a pack expansion containing a lambda
19546 might result in multiple lambdas all based on the same lambda in the
19547 template. */
19548
19549 tree
19550 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19551 {
19552 tree oldfn = lambda_function (t);
19553 in_decl = oldfn;
19554
19555 tree r = build_lambda_expr ();
19556
19557 LAMBDA_EXPR_LOCATION (r)
19558 = LAMBDA_EXPR_LOCATION (t);
19559 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19560 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19561 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19562 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19563 LAMBDA_EXPR_REGEN_INFO (r)
19564 = build_template_info (t, add_to_template_args (TI_ARGS (ti),
19565 preserve_args (args)));
19566 else
19567 LAMBDA_EXPR_REGEN_INFO (r)
19568 = build_template_info (t, preserve_args (args));
19569
19570 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19571 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19572
19573 vec<tree,va_gc>* field_packs = NULL;
19574
19575 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19576 cap = TREE_CHAIN (cap))
19577 {
19578 tree ofield = TREE_PURPOSE (cap);
19579 tree init = TREE_VALUE (cap);
19580 if (PACK_EXPANSION_P (init))
19581 init = tsubst_pack_expansion (init, args, complain, in_decl);
19582 else
19583 init = tsubst_copy_and_build (init, args, complain, in_decl,
19584 /*fn*/false, /*constexpr*/false);
19585
19586 if (init == error_mark_node)
19587 return error_mark_node;
19588
19589 if (init && TREE_CODE (init) == TREE_LIST)
19590 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19591
19592 if (!processing_template_decl
19593 && init && TREE_CODE (init) != TREE_VEC
19594 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19595 {
19596 /* For a VLA, simply tsubsting the field type won't work, we need to
19597 go through add_capture again. XXX do we want to do this for all
19598 captures? */
19599 tree name = (get_identifier
19600 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19601 tree ftype = TREE_TYPE (ofield);
19602 bool by_ref = (TYPE_REF_P (ftype)
19603 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19604 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19605 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19606 continue;
19607 }
19608
19609 if (PACK_EXPANSION_P (ofield))
19610 ofield = PACK_EXPANSION_PATTERN (ofield);
19611 tree field = tsubst_decl (ofield, args, complain);
19612
19613 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19614 {
19615 /* Remember these for when we've pushed local_specializations. */
19616 vec_safe_push (field_packs, ofield);
19617 vec_safe_push (field_packs, field);
19618 }
19619
19620 if (field == error_mark_node)
19621 return error_mark_node;
19622
19623 if (TREE_CODE (field) == TREE_VEC)
19624 {
19625 int len = TREE_VEC_LENGTH (field);
19626 gcc_assert (TREE_CODE (init) == TREE_VEC
19627 && TREE_VEC_LENGTH (init) == len);
19628 for (int i = 0; i < len; ++i)
19629 prepend_one_capture (TREE_VEC_ELT (field, i),
19630 TREE_VEC_ELT (init, i),
19631 LAMBDA_EXPR_CAPTURE_LIST (r),
19632 complain);
19633 }
19634 else
19635 {
19636 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19637 complain);
19638
19639 if (id_equal (DECL_NAME (field), "__this"))
19640 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19641 }
19642 }
19643
19644 tree type = begin_lambda_type (r);
19645 if (type == error_mark_node)
19646 return error_mark_node;
19647
19648 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19649 /* A lambda in a default argument outside a class gets no
19650 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19651 tsubst_default_argument calls start_lambda_scope, so we need to
19652 specifically ignore it here, and use the global scope. */
19653 record_null_lambda_scope (r);
19654 else
19655 record_lambda_scope (r);
19656
19657 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19658 determine_visibility (TYPE_NAME (type));
19659
19660 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19661
19662 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19663 ? DECL_TI_TEMPLATE (oldfn)
19664 : NULL_TREE);
19665
19666 tree fntype = static_fn_type (oldfn);
19667 if (oldtmpl)
19668 ++processing_template_decl;
19669 fntype = tsubst (fntype, args, complain, in_decl);
19670 if (oldtmpl)
19671 --processing_template_decl;
19672
19673 if (fntype == error_mark_node)
19674 r = error_mark_node;
19675 else
19676 {
19677 /* The body of a lambda-expression is not a subexpression of the
19678 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19679 which would be skipped if cp_unevaluated_operand. */
19680 cp_evaluated ev;
19681
19682 /* Fix the type of 'this'. */
19683 fntype = build_memfn_type (fntype, type,
19684 type_memfn_quals (fntype),
19685 type_memfn_rqual (fntype));
19686 tree fn, tmpl;
19687 if (oldtmpl)
19688 {
19689 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19690 if (tmpl == error_mark_node)
19691 {
19692 r = error_mark_node;
19693 goto out;
19694 }
19695 fn = DECL_TEMPLATE_RESULT (tmpl);
19696 finish_member_declaration (tmpl);
19697 }
19698 else
19699 {
19700 tmpl = NULL_TREE;
19701 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19702 if (fn == error_mark_node)
19703 {
19704 r = error_mark_node;
19705 goto out;
19706 }
19707 finish_member_declaration (fn);
19708 }
19709
19710 /* Let finish_function set this. */
19711 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19712
19713 bool nested = cfun;
19714 if (nested)
19715 push_function_context ();
19716 else
19717 /* Still increment function_depth so that we don't GC in the
19718 middle of an expression. */
19719 ++function_depth;
19720
19721 local_specialization_stack s (lss_copy);
19722
19723 bool save_in_consteval_if_p = in_consteval_if_p;
19724 in_consteval_if_p = false;
19725
19726 tree body = start_lambda_function (fn, r);
19727
19728 /* Now record them for lookup_init_capture_pack. */
19729 int fplen = vec_safe_length (field_packs);
19730 for (int i = 0; i < fplen; )
19731 {
19732 tree pack = (*field_packs)[i++];
19733 tree inst = (*field_packs)[i++];
19734 register_local_specialization (inst, pack);
19735 }
19736 release_tree_vector (field_packs);
19737
19738 register_parameter_specializations (oldfn, fn);
19739
19740 if (oldtmpl)
19741 {
19742 /* We might not partially instantiate some parts of the function, so
19743 copy these flags from the original template. */
19744 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19745 current_function_returns_value = ol->returns_value;
19746 current_function_returns_null = ol->returns_null;
19747 current_function_returns_abnormally = ol->returns_abnormally;
19748 current_function_infinite_loop = ol->infinite_loop;
19749 }
19750
19751 /* [temp.deduct] A lambda-expression appearing in a function type or a
19752 template parameter is not considered part of the immediate context for
19753 the purposes of template argument deduction. */
19754 complain = tf_warning_or_error;
19755
19756 tree saved = DECL_SAVED_TREE (oldfn);
19757 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19758 /* We already have a body block from start_lambda_function, we don't
19759 need another to confuse NRV (91217). */
19760 saved = BIND_EXPR_BODY (saved);
19761
19762 tsubst_expr (saved, args, complain, r, /*constexpr*/false);
19763
19764 finish_lambda_function (body);
19765
19766 in_consteval_if_p = save_in_consteval_if_p;
19767
19768 if (nested)
19769 pop_function_context ();
19770 else
19771 --function_depth;
19772
19773 /* The capture list was built up in reverse order; fix that now. */
19774 LAMBDA_EXPR_CAPTURE_LIST (r)
19775 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19776
19777 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19778
19779 maybe_add_lambda_conv_op (type);
19780 }
19781
19782 out:
19783 finish_struct (type, /*attr*/NULL_TREE);
19784
19785 insert_pending_capture_proxies ();
19786
19787 return r;
19788 }
19789
19790 /* Subroutine of maybe_fold_fn_template_args. */
19791
19792 static bool
19793 fold_targs_r (tree targs, tsubst_flags_t complain)
19794 {
19795 int len = TREE_VEC_LENGTH (targs);
19796 for (int i = 0; i < len; ++i)
19797 {
19798 tree &elt = TREE_VEC_ELT (targs, i);
19799 if (!elt || TYPE_P (elt)
19800 || TREE_CODE (elt) == TEMPLATE_DECL)
19801 continue;
19802 if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
19803 {
19804 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
19805 return false;
19806 }
19807 else if (/* We can only safely preevaluate scalar prvalues. */
19808 SCALAR_TYPE_P (TREE_TYPE (elt))
19809 && !glvalue_p (elt)
19810 && !TREE_CONSTANT (elt))
19811 {
19812 elt = cxx_constant_value_sfinae (elt, complain);
19813 if (elt == error_mark_node)
19814 return false;
19815 }
19816 }
19817
19818 return true;
19819 }
19820
19821 /* Try to do constant evaluation of any explicit template arguments in FN
19822 before overload resolution, to get any errors only once. Return true iff
19823 we didn't have any problems folding. */
19824
19825 static bool
19826 maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
19827 {
19828 if (processing_template_decl || fn == NULL_TREE)
19829 return true;
19830 if (fn == error_mark_node)
19831 return false;
19832 if (TREE_CODE (fn) == OFFSET_REF
19833 || TREE_CODE (fn) == COMPONENT_REF)
19834 fn = TREE_OPERAND (fn, 1);
19835 if (BASELINK_P (fn))
19836 fn = BASELINK_FUNCTIONS (fn);
19837 if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
19838 return true;
19839 tree targs = TREE_OPERAND (fn, 1);
19840 if (targs == NULL_TREE)
19841 return true;
19842 if (targs == error_mark_node)
19843 return false;
19844 return fold_targs_r (targs, complain);
19845 }
19846
19847 /* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
19848 handling. */
19849
19850 static void
19851 tsubst_copy_and_build_call_args (tree t, tree args, tsubst_flags_t complain,
19852 tree in_decl,
19853 bool integral_constant_expression_p,
19854 releasing_vec &call_args)
19855 {
19856 unsigned int nargs = call_expr_nargs (t);
19857 for (unsigned int i = 0; i < nargs; ++i)
19858 {
19859 tree arg = CALL_EXPR_ARG (t, i);
19860
19861 if (!PACK_EXPANSION_P (arg))
19862 vec_safe_push (call_args,
19863 tsubst_copy_and_build (arg, args, complain, in_decl,
19864 /*function_p=*/false,
19865 integral_constant_expression_p));
19866 else
19867 {
19868 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19869 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19870 if (TREE_CODE (arg) == TREE_VEC)
19871 {
19872 unsigned int len, j;
19873
19874 len = TREE_VEC_LENGTH (arg);
19875 for (j = 0; j < len; ++j)
19876 {
19877 tree value = TREE_VEC_ELT (arg, j);
19878 if (value != NULL_TREE)
19879 value = convert_from_reference (value);
19880 vec_safe_push (call_args, value);
19881 }
19882 }
19883 else
19884 /* A partial substitution. Add one entry. */
19885 vec_safe_push (call_args, arg);
19886 }
19887 }
19888 }
19889
19890 /* Like tsubst but deals with expressions and performs semantic
19891 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19892 "F<TARGS> (ARGS)". */
19893
19894 tree
19895 tsubst_copy_and_build (tree t,
19896 tree args,
19897 tsubst_flags_t complain,
19898 tree in_decl,
19899 bool function_p,
19900 bool integral_constant_expression_p)
19901 {
19902 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19903 #define RECUR(NODE) \
19904 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19905 /*function_p=*/false, \
19906 integral_constant_expression_p)
19907
19908 tree retval, op1;
19909 location_t save_loc;
19910
19911 if (t == NULL_TREE || t == error_mark_node)
19912 return t;
19913
19914 save_loc = input_location;
19915 if (location_t eloc = cp_expr_location (t))
19916 input_location = eloc;
19917
19918 /* N3276 decltype magic only applies to calls at the top level or on the
19919 right side of a comma. */
19920 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19921 complain &= ~tf_decltype;
19922
19923 switch (TREE_CODE (t))
19924 {
19925 case USING_DECL:
19926 t = DECL_NAME (t);
19927 /* Fall through. */
19928 case IDENTIFIER_NODE:
19929 {
19930 tree decl;
19931 cp_id_kind idk;
19932 bool non_integral_constant_expression_p;
19933 const char *error_msg;
19934
19935 if (IDENTIFIER_CONV_OP_P (t))
19936 {
19937 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19938 t = make_conv_op_name (new_type);
19939 }
19940
19941 /* Look up the name. */
19942 decl = lookup_name (t);
19943
19944 /* By convention, expressions use ERROR_MARK_NODE to indicate
19945 failure, not NULL_TREE. */
19946 if (decl == NULL_TREE)
19947 decl = error_mark_node;
19948
19949 decl = finish_id_expression (t, decl, NULL_TREE,
19950 &idk,
19951 integral_constant_expression_p,
19952 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19953 &non_integral_constant_expression_p,
19954 /*template_p=*/false,
19955 /*done=*/true,
19956 /*address_p=*/false,
19957 /*template_arg_p=*/false,
19958 &error_msg,
19959 input_location);
19960 if (error_msg)
19961 error (error_msg);
19962 if (!function_p && identifier_p (decl))
19963 {
19964 if (complain & tf_error)
19965 unqualified_name_lookup_error (decl);
19966 decl = error_mark_node;
19967 }
19968 RETURN (decl);
19969 }
19970
19971 case TEMPLATE_ID_EXPR:
19972 {
19973 tree object;
19974 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19975 complain, in_decl,
19976 function_p,
19977 integral_constant_expression_p);
19978 tree targs = TREE_OPERAND (t, 1);
19979
19980 if (targs)
19981 targs = tsubst_template_args (targs, args, complain, in_decl);
19982 if (targs == error_mark_node)
19983 RETURN (error_mark_node);
19984
19985 if (TREE_CODE (templ) == SCOPE_REF)
19986 {
19987 tree name = TREE_OPERAND (templ, 1);
19988 tree tid = lookup_template_function (name, targs);
19989 TREE_OPERAND (templ, 1) = tid;
19990 RETURN (templ);
19991 }
19992
19993 if (concept_definition_p (templ))
19994 {
19995 tree check = build_concept_check (templ, targs, complain);
19996 if (check == error_mark_node)
19997 RETURN (error_mark_node);
19998
19999 tree id = unpack_concept_check (check);
20000
20001 /* If we built a function concept check, return the underlying
20002 template-id. So we can evaluate it as a function call. */
20003 if (function_concept_p (TREE_OPERAND (id, 0)))
20004 RETURN (id);
20005
20006 RETURN (check);
20007 }
20008
20009 if (variable_template_p (templ))
20010 {
20011 tree r = lookup_and_finish_template_variable (templ, targs,
20012 complain);
20013 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
20014 RETURN (r);
20015 }
20016
20017 if (TREE_CODE (templ) == COMPONENT_REF)
20018 {
20019 object = TREE_OPERAND (templ, 0);
20020 templ = TREE_OPERAND (templ, 1);
20021 }
20022 else
20023 object = NULL_TREE;
20024
20025 tree tid = lookup_template_function (templ, targs);
20026
20027 if (object)
20028 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
20029 object, tid, NULL_TREE));
20030 else if (identifier_p (templ))
20031 {
20032 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20033 name lookup found nothing when parsing the template name. */
20034 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
20035 RETURN (tid);
20036 }
20037 else
20038 RETURN (baselink_for_fns (tid));
20039 }
20040
20041 case INDIRECT_REF:
20042 {
20043 tree r = RECUR (TREE_OPERAND (t, 0));
20044
20045 if (REFERENCE_REF_P (t))
20046 {
20047 /* A type conversion to reference type will be enclosed in
20048 such an indirect ref, but the substitution of the cast
20049 will have also added such an indirect ref. */
20050 r = convert_from_reference (r);
20051 }
20052 else
20053 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
20054 templated_operator_saved_lookups (t),
20055 complain|decltype_flag);
20056
20057 if (REF_PARENTHESIZED_P (t))
20058 r = force_paren_expr (r);
20059
20060 RETURN (r);
20061 }
20062
20063 case NOP_EXPR:
20064 {
20065 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20066 tree op0 = RECUR (TREE_OPERAND (t, 0));
20067 RETURN (build_nop (type, op0));
20068 }
20069
20070 case IMPLICIT_CONV_EXPR:
20071 {
20072 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20073 tree expr = RECUR (TREE_OPERAND (t, 0));
20074 if (dependent_type_p (type) || type_dependent_expression_p (expr))
20075 {
20076 retval = copy_node (t);
20077 TREE_TYPE (retval) = type;
20078 TREE_OPERAND (retval, 0) = expr;
20079 RETURN (retval);
20080 }
20081 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
20082 /* We'll pass this to convert_nontype_argument again, we don't need
20083 to actually perform any conversion here. */
20084 RETURN (expr);
20085 int flags = LOOKUP_IMPLICIT;
20086 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
20087 flags = LOOKUP_NORMAL;
20088 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
20089 flags |= LOOKUP_NO_NARROWING;
20090 RETURN (perform_implicit_conversion_flags (type, expr, complain,
20091 flags));
20092 }
20093
20094 case CONVERT_EXPR:
20095 {
20096 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20097 tree op0 = RECUR (TREE_OPERAND (t, 0));
20098 if (op0 == error_mark_node)
20099 RETURN (error_mark_node);
20100 RETURN (build1 (CONVERT_EXPR, type, op0));
20101 }
20102
20103 case CAST_EXPR:
20104 case REINTERPRET_CAST_EXPR:
20105 case CONST_CAST_EXPR:
20106 case DYNAMIC_CAST_EXPR:
20107 case STATIC_CAST_EXPR:
20108 {
20109 tree type;
20110 tree op, r = NULL_TREE;
20111
20112 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20113 if (integral_constant_expression_p
20114 && !cast_valid_in_integral_constant_expression_p (type))
20115 {
20116 if (complain & tf_error)
20117 error ("a cast to a type other than an integral or "
20118 "enumeration type cannot appear in a constant-expression");
20119 RETURN (error_mark_node);
20120 }
20121
20122 op = RECUR (TREE_OPERAND (t, 0));
20123
20124 warning_sentinel s(warn_useless_cast);
20125 warning_sentinel s2(warn_ignored_qualifiers);
20126 warning_sentinel s3(warn_int_in_bool_context);
20127 switch (TREE_CODE (t))
20128 {
20129 case CAST_EXPR:
20130 r = build_functional_cast (input_location, type, op, complain);
20131 break;
20132 case REINTERPRET_CAST_EXPR:
20133 r = build_reinterpret_cast (input_location, type, op, complain);
20134 break;
20135 case CONST_CAST_EXPR:
20136 r = build_const_cast (input_location, type, op, complain);
20137 break;
20138 case DYNAMIC_CAST_EXPR:
20139 r = build_dynamic_cast (input_location, type, op, complain);
20140 break;
20141 case STATIC_CAST_EXPR:
20142 r = build_static_cast (input_location, type, op, complain);
20143 if (IMPLICIT_RVALUE_P (t))
20144 set_implicit_rvalue_p (r);
20145 break;
20146 default:
20147 gcc_unreachable ();
20148 }
20149
20150 RETURN (r);
20151 }
20152
20153 case BIT_CAST_EXPR:
20154 {
20155 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20156 tree op0 = RECUR (TREE_OPERAND (t, 0));
20157 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
20158 }
20159
20160 case POSTDECREMENT_EXPR:
20161 case POSTINCREMENT_EXPR:
20162 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20163 args, complain, in_decl);
20164 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
20165 templated_operator_saved_lookups (t),
20166 complain|decltype_flag));
20167
20168 case PREDECREMENT_EXPR:
20169 case PREINCREMENT_EXPR:
20170 case NEGATE_EXPR:
20171 case BIT_NOT_EXPR:
20172 case ABS_EXPR:
20173 case TRUTH_NOT_EXPR:
20174 case UNARY_PLUS_EXPR: /* Unary + */
20175 case REALPART_EXPR:
20176 case IMAGPART_EXPR:
20177 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
20178 RECUR (TREE_OPERAND (t, 0)),
20179 templated_operator_saved_lookups (t),
20180 complain|decltype_flag));
20181
20182 case FIX_TRUNC_EXPR:
20183 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20184 gcc_unreachable ();
20185
20186 case ADDR_EXPR:
20187 op1 = TREE_OPERAND (t, 0);
20188 if (TREE_CODE (op1) == LABEL_DECL)
20189 RETURN (finish_label_address_expr (DECL_NAME (op1),
20190 EXPR_LOCATION (op1)));
20191 if (TREE_CODE (op1) == SCOPE_REF)
20192 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
20193 /*done=*/true, /*address_p=*/true);
20194 else
20195 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
20196 in_decl);
20197 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
20198 templated_operator_saved_lookups (t),
20199 complain|decltype_flag));
20200
20201 case PLUS_EXPR:
20202 case MINUS_EXPR:
20203 case MULT_EXPR:
20204 case TRUNC_DIV_EXPR:
20205 case CEIL_DIV_EXPR:
20206 case FLOOR_DIV_EXPR:
20207 case ROUND_DIV_EXPR:
20208 case EXACT_DIV_EXPR:
20209 case BIT_AND_EXPR:
20210 case BIT_IOR_EXPR:
20211 case BIT_XOR_EXPR:
20212 case TRUNC_MOD_EXPR:
20213 case FLOOR_MOD_EXPR:
20214 case TRUTH_ANDIF_EXPR:
20215 case TRUTH_ORIF_EXPR:
20216 case TRUTH_AND_EXPR:
20217 case TRUTH_OR_EXPR:
20218 case RSHIFT_EXPR:
20219 case LSHIFT_EXPR:
20220 case EQ_EXPR:
20221 case NE_EXPR:
20222 case MAX_EXPR:
20223 case MIN_EXPR:
20224 case LE_EXPR:
20225 case GE_EXPR:
20226 case LT_EXPR:
20227 case GT_EXPR:
20228 case SPACESHIP_EXPR:
20229 case MEMBER_REF:
20230 case DOTSTAR_EXPR:
20231 {
20232 /* If either OP0 or OP1 was value- or type-dependent, suppress
20233 warnings that depend on the range of the types involved. */
20234 tree op0 = TREE_OPERAND (t, 0);
20235 tree op1 = TREE_OPERAND (t, 1);
20236 auto dep_p = [](tree t) {
20237 ++processing_template_decl;
20238 bool r = (potential_constant_expression (t)
20239 ? value_dependent_expression_p (t)
20240 : type_dependent_expression_p (t));
20241 --processing_template_decl;
20242 return r;
20243 };
20244 const bool was_dep = dep_p (op0) || dep_p (op1);
20245 op0 = RECUR (op0);
20246 op1 = RECUR (op1);
20247
20248 warning_sentinel s1(warn_type_limits, was_dep);
20249 warning_sentinel s2(warn_div_by_zero, was_dep);
20250 warning_sentinel s3(warn_logical_op, was_dep);
20251 warning_sentinel s4(warn_tautological_compare, was_dep);
20252
20253 tree r = build_x_binary_op
20254 (input_location, TREE_CODE (t),
20255 op0,
20256 (warning_suppressed_p (TREE_OPERAND (t, 0))
20257 ? ERROR_MARK
20258 : TREE_CODE (TREE_OPERAND (t, 0))),
20259 op1,
20260 (warning_suppressed_p (TREE_OPERAND (t, 1))
20261 ? ERROR_MARK
20262 : TREE_CODE (TREE_OPERAND (t, 1))),
20263 templated_operator_saved_lookups (t),
20264 /*overload=*/NULL,
20265 complain|decltype_flag);
20266 if (EXPR_P (r))
20267 copy_warning (r, t);
20268
20269 RETURN (r);
20270 }
20271
20272 case POINTER_PLUS_EXPR:
20273 {
20274 tree op0 = RECUR (TREE_OPERAND (t, 0));
20275 if (op0 == error_mark_node)
20276 RETURN (error_mark_node);
20277 tree op1 = RECUR (TREE_OPERAND (t, 1));
20278 if (op1 == error_mark_node)
20279 RETURN (error_mark_node);
20280 RETURN (fold_build_pointer_plus (op0, op1));
20281 }
20282
20283 case SCOPE_REF:
20284 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
20285 /*address_p=*/false));
20286
20287 case BASELINK:
20288 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
20289 args, complain, in_decl));
20290
20291 case ARRAY_REF:
20292 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20293 args, complain, in_decl);
20294 if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
20295 && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
20296 == ovl_op_identifier (ARRAY_REF)))
20297 {
20298 tree c = TREE_OPERAND (t, 1);
20299 releasing_vec index_exp_list;
20300 tsubst_copy_and_build_call_args (c, args, complain, in_decl,
20301 integral_constant_expression_p,
20302 index_exp_list);
20303
20304 tree r;
20305 if (vec_safe_length (index_exp_list) == 1
20306 && !PACK_EXPANSION_P (index_exp_list[0]))
20307 r = grok_array_decl (EXPR_LOCATION (t), op1,
20308 index_exp_list[0], NULL,
20309 complain | decltype_flag);
20310 else
20311 r = grok_array_decl (EXPR_LOCATION (t), op1,
20312 NULL_TREE, &index_exp_list,
20313 complain | decltype_flag);
20314 RETURN (r);
20315 }
20316 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
20317 RECUR (TREE_OPERAND (t, 1)),
20318 complain|decltype_flag));
20319
20320 case SIZEOF_EXPR:
20321 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
20322 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
20323 RETURN (tsubst_copy (t, args, complain, in_decl));
20324 /* Fall through */
20325
20326 case ALIGNOF_EXPR:
20327 {
20328 tree r;
20329
20330 op1 = TREE_OPERAND (t, 0);
20331 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
20332 op1 = TREE_TYPE (op1);
20333 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
20334 && ALIGNOF_EXPR_STD_P (t));
20335 if (!args)
20336 {
20337 /* When there are no ARGS, we are trying to evaluate a
20338 non-dependent expression from the parser. Trying to do
20339 the substitutions may not work. */
20340 if (!TYPE_P (op1))
20341 op1 = TREE_TYPE (op1);
20342 }
20343 else
20344 {
20345 ++cp_unevaluated_operand;
20346 ++c_inhibit_evaluation_warnings;
20347 if (TYPE_P (op1))
20348 op1 = tsubst (op1, args, complain, in_decl);
20349 else
20350 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
20351 /*function_p=*/false,
20352 /*integral_constant_expression_p=*/
20353 false);
20354 --cp_unevaluated_operand;
20355 --c_inhibit_evaluation_warnings;
20356 }
20357 if (TYPE_P (op1))
20358 r = cxx_sizeof_or_alignof_type (input_location,
20359 op1, TREE_CODE (t), std_alignof,
20360 complain & tf_error);
20361 else
20362 r = cxx_sizeof_or_alignof_expr (input_location,
20363 op1, TREE_CODE (t), std_alignof,
20364 complain & tf_error);
20365 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
20366 {
20367 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
20368 {
20369 if (!processing_template_decl && TYPE_P (op1))
20370 {
20371 r = build_min (SIZEOF_EXPR, size_type_node,
20372 build1 (NOP_EXPR, op1, error_mark_node));
20373 SIZEOF_EXPR_TYPE_P (r) = 1;
20374 }
20375 else
20376 r = build_min (SIZEOF_EXPR, size_type_node, op1);
20377 TREE_SIDE_EFFECTS (r) = 0;
20378 TREE_READONLY (r) = 1;
20379 }
20380 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
20381 }
20382 RETURN (r);
20383 }
20384
20385 case AT_ENCODE_EXPR:
20386 {
20387 op1 = TREE_OPERAND (t, 0);
20388 ++cp_unevaluated_operand;
20389 ++c_inhibit_evaluation_warnings;
20390 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
20391 /*function_p=*/false,
20392 /*integral_constant_expression_p=*/false);
20393 --cp_unevaluated_operand;
20394 --c_inhibit_evaluation_warnings;
20395 RETURN (objc_build_encode_expr (op1));
20396 }
20397
20398 case NOEXCEPT_EXPR:
20399 op1 = TREE_OPERAND (t, 0);
20400 ++cp_unevaluated_operand;
20401 ++c_inhibit_evaluation_warnings;
20402 ++cp_noexcept_operand;
20403 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
20404 /*function_p=*/false,
20405 /*integral_constant_expression_p=*/false);
20406 --cp_unevaluated_operand;
20407 --c_inhibit_evaluation_warnings;
20408 --cp_noexcept_operand;
20409 RETURN (finish_noexcept_expr (op1, complain));
20410
20411 case MODOP_EXPR:
20412 {
20413 warning_sentinel s(warn_div_by_zero);
20414 tree lhs = RECUR (TREE_OPERAND (t, 0));
20415 tree rhs = RECUR (TREE_OPERAND (t, 2));
20416
20417 tree r = build_x_modify_expr
20418 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
20419 templated_operator_saved_lookups (t),
20420 complain|decltype_flag);
20421 /* TREE_NO_WARNING must be set if either the expression was
20422 parenthesized or it uses an operator such as >>= rather
20423 than plain assignment. In the former case, it was already
20424 set and must be copied. In the latter case,
20425 build_x_modify_expr sets it and it must not be reset
20426 here. */
20427 if (warning_suppressed_p (t, OPT_Wparentheses))
20428 suppress_warning (r, OPT_Wparentheses);
20429
20430 RETURN (r);
20431 }
20432
20433 case ARROW_EXPR:
20434 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20435 args, complain, in_decl);
20436 /* Remember that there was a reference to this entity. */
20437 if (DECL_P (op1)
20438 && !mark_used (op1, complain) && !(complain & tf_error))
20439 RETURN (error_mark_node);
20440 RETURN (build_x_arrow (input_location, op1, complain));
20441
20442 case NEW_EXPR:
20443 {
20444 tree placement = RECUR (TREE_OPERAND (t, 0));
20445 tree init = RECUR (TREE_OPERAND (t, 3));
20446 vec<tree, va_gc> *placement_vec;
20447 vec<tree, va_gc> *init_vec;
20448 tree ret;
20449 location_t loc = EXPR_LOCATION (t);
20450
20451 if (placement == NULL_TREE)
20452 placement_vec = NULL;
20453 else if (placement == error_mark_node)
20454 RETURN (error_mark_node);
20455 else
20456 {
20457 placement_vec = make_tree_vector ();
20458 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20459 vec_safe_push (placement_vec, TREE_VALUE (placement));
20460 }
20461
20462 /* If there was an initializer in the original tree, but it
20463 instantiated to an empty list, then we should pass a
20464 non-NULL empty vector to tell build_new that it was an
20465 empty initializer() rather than no initializer. This can
20466 only happen when the initializer is a pack expansion whose
20467 parameter packs are of length zero. */
20468 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20469 init_vec = NULL;
20470 else if (init == error_mark_node)
20471 RETURN (error_mark_node);
20472 else
20473 {
20474 init_vec = make_tree_vector ();
20475 if (init == void_node)
20476 gcc_assert (init_vec != NULL);
20477 else
20478 {
20479 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20480 vec_safe_push (init_vec, TREE_VALUE (init));
20481 }
20482 }
20483
20484 /* Avoid passing an enclosing decl to valid_array_size_p. */
20485 in_decl = NULL_TREE;
20486
20487 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20488 tree op2 = RECUR (TREE_OPERAND (t, 2));
20489 ret = build_new (loc, &placement_vec, op1, op2,
20490 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20491 complain);
20492
20493 if (placement_vec != NULL)
20494 release_tree_vector (placement_vec);
20495 if (init_vec != NULL)
20496 release_tree_vector (init_vec);
20497
20498 RETURN (ret);
20499 }
20500
20501 case DELETE_EXPR:
20502 {
20503 tree op0 = RECUR (TREE_OPERAND (t, 0));
20504 tree op1 = RECUR (TREE_OPERAND (t, 1));
20505 RETURN (delete_sanity (input_location, op0, op1,
20506 DELETE_EXPR_USE_VEC (t),
20507 DELETE_EXPR_USE_GLOBAL (t),
20508 complain));
20509 }
20510
20511 case COMPOUND_EXPR:
20512 {
20513 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
20514 complain & ~tf_decltype, in_decl,
20515 /*function_p=*/false,
20516 integral_constant_expression_p);
20517 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20518 op0,
20519 RECUR (TREE_OPERAND (t, 1)),
20520 templated_operator_saved_lookups (t),
20521 complain|decltype_flag));
20522 }
20523
20524 case CALL_EXPR:
20525 {
20526 tree function;
20527 unsigned int nargs;
20528 bool qualified_p;
20529 bool koenig_p;
20530 tree ret;
20531
20532 function = CALL_EXPR_FN (t);
20533 /* Internal function with no arguments. */
20534 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20535 RETURN (t);
20536
20537 /* When we parsed the expression, we determined whether or
20538 not Koenig lookup should be performed. */
20539 koenig_p = KOENIG_LOOKUP_P (t);
20540 if (function == NULL_TREE)
20541 {
20542 koenig_p = false;
20543 qualified_p = false;
20544 }
20545 else if (TREE_CODE (function) == SCOPE_REF)
20546 {
20547 qualified_p = true;
20548 function = tsubst_qualified_id (function, args, complain, in_decl,
20549 /*done=*/false,
20550 /*address_p=*/false);
20551 }
20552 else if (koenig_p
20553 && (identifier_p (function)
20554 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20555 && identifier_p (TREE_OPERAND (function, 0)))))
20556 {
20557 /* Do nothing; calling tsubst_copy_and_build on an identifier
20558 would incorrectly perform unqualified lookup again.
20559
20560 Note that we can also have an IDENTIFIER_NODE if the earlier
20561 unqualified lookup found a member function; in that case
20562 koenig_p will be false and we do want to do the lookup
20563 again to find the instantiated member function.
20564
20565 FIXME but doing that causes c++/15272, so we need to stop
20566 using IDENTIFIER_NODE in that situation. */
20567 qualified_p = false;
20568
20569 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20570 /* Use tsubst_copy to substitute through the template arguments
20571 of the template-id without performing unqualified lookup of
20572 the template name. */
20573 function = tsubst_copy (function, args, complain, in_decl);
20574 }
20575 else
20576 {
20577 if (TREE_CODE (function) == COMPONENT_REF)
20578 {
20579 tree op = TREE_OPERAND (function, 1);
20580
20581 qualified_p = (TREE_CODE (op) == SCOPE_REF
20582 || (BASELINK_P (op)
20583 && BASELINK_QUALIFIED_P (op)));
20584 }
20585 else
20586 qualified_p = false;
20587
20588 if (TREE_CODE (function) == ADDR_EXPR
20589 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20590 /* Avoid error about taking the address of a constructor. */
20591 function = TREE_OPERAND (function, 0);
20592
20593 tsubst_flags_t subcomplain = complain;
20594 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
20595 /* When KOENIG_P, we don't want to mark_used the callee before
20596 augmenting the overload set via ADL, so during this initial
20597 substitution we disable mark_used by setting tf_conv (68942). */
20598 subcomplain |= tf_conv;
20599 function = tsubst_copy_and_build (function, args, subcomplain,
20600 in_decl,
20601 !qualified_p,
20602 integral_constant_expression_p);
20603
20604 if (BASELINK_P (function))
20605 qualified_p = true;
20606 }
20607
20608 nargs = call_expr_nargs (t);
20609 releasing_vec call_args;
20610 tsubst_copy_and_build_call_args (t, args, complain, in_decl,
20611 integral_constant_expression_p,
20612 call_args);
20613
20614 /* Stripped-down processing for a call in a thunk. Specifically, in
20615 the thunk template for a generic lambda. */
20616 if (call_from_lambda_thunk_p (t))
20617 {
20618 /* Now that we've expanded any packs, the number of call args
20619 might be different. */
20620 unsigned int cargs = call_args->length ();
20621 tree thisarg = NULL_TREE;
20622 if (TREE_CODE (function) == COMPONENT_REF)
20623 {
20624 thisarg = TREE_OPERAND (function, 0);
20625 if (TREE_CODE (thisarg) == INDIRECT_REF)
20626 thisarg = TREE_OPERAND (thisarg, 0);
20627 function = TREE_OPERAND (function, 1);
20628 if (TREE_CODE (function) == BASELINK)
20629 function = BASELINK_FUNCTIONS (function);
20630 }
20631 /* We aren't going to do normal overload resolution, so force the
20632 template-id to resolve. */
20633 function = resolve_nondeduced_context (function, complain);
20634 for (unsigned i = 0; i < cargs; ++i)
20635 {
20636 /* In a thunk, pass through args directly, without any
20637 conversions. */
20638 tree arg = (*call_args)[i];
20639 while (TREE_CODE (arg) != PARM_DECL)
20640 arg = TREE_OPERAND (arg, 0);
20641 (*call_args)[i] = arg;
20642 }
20643 if (thisarg)
20644 {
20645 /* If there are no other args, just push 'this'. */
20646 if (cargs == 0)
20647 vec_safe_push (call_args, thisarg);
20648 else
20649 {
20650 /* Otherwise, shift the other args over to make room. */
20651 tree last = (*call_args)[cargs - 1];
20652 vec_safe_push (call_args, last);
20653 for (int i = cargs - 1; i > 0; --i)
20654 (*call_args)[i] = (*call_args)[i - 1];
20655 (*call_args)[0] = thisarg;
20656 }
20657 }
20658 ret = build_call_a (function, call_args->length (),
20659 call_args->address ());
20660 /* The thunk location is not interesting. */
20661 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20662 CALL_FROM_THUNK_P (ret) = true;
20663 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20664 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20665
20666 RETURN (ret);
20667 }
20668
20669 /* We do not perform argument-dependent lookup if normal
20670 lookup finds a non-function, in accordance with the
20671 resolution of DR 218. */
20672 if (koenig_p
20673 && ((is_overloaded_fn (function)
20674 /* If lookup found a member function, the Koenig lookup is
20675 not appropriate, even if an unqualified-name was used
20676 to denote the function. */
20677 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20678 || identifier_p (function)
20679 /* C++20 P0846: Lookup found nothing. */
20680 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20681 && identifier_p (TREE_OPERAND (function, 0))))
20682 /* Only do this when substitution turns a dependent call
20683 into a non-dependent call. */
20684 && type_dependent_expression_p_push (t)
20685 && !any_type_dependent_arguments_p (call_args))
20686 function = perform_koenig_lookup (function, call_args, tf_none);
20687
20688 if (function != NULL_TREE
20689 && (identifier_p (function)
20690 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20691 && identifier_p (TREE_OPERAND (function, 0))
20692 && !any_dependent_template_arguments_p (TREE_OPERAND
20693 (function, 1))))
20694 && !any_type_dependent_arguments_p (call_args))
20695 {
20696 bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
20697 if (template_id_p)
20698 function = TREE_OPERAND (function, 0);
20699 if (koenig_p && (complain & tf_warning_or_error))
20700 {
20701 /* For backwards compatibility and good diagnostics, try
20702 the unqualified lookup again if we aren't in SFINAE
20703 context. */
20704 tree unq = (tsubst_copy_and_build
20705 (function, args, complain, in_decl, true,
20706 integral_constant_expression_p));
20707 if (unq == error_mark_node)
20708 RETURN (error_mark_node);
20709
20710 if (unq != function)
20711 {
20712 char const *const msg
20713 = G_("%qD was not declared in this scope, "
20714 "and no declarations were found by "
20715 "argument-dependent lookup at the point "
20716 "of instantiation");
20717
20718 bool in_lambda = (current_class_type
20719 && LAMBDA_TYPE_P (current_class_type));
20720 /* In a lambda fn, we have to be careful to not
20721 introduce new this captures. Legacy code can't
20722 be using lambdas anyway, so it's ok to be
20723 stricter. Be strict with C++20 template-id ADL too. */
20724 bool strict = in_lambda || template_id_p;
20725 bool diag = true;
20726 if (strict)
20727 error_at (cp_expr_loc_or_input_loc (t),
20728 msg, function);
20729 else
20730 diag = permerror (cp_expr_loc_or_input_loc (t),
20731 msg, function);
20732 if (diag)
20733 {
20734 tree fn = unq;
20735
20736 if (INDIRECT_REF_P (fn))
20737 fn = TREE_OPERAND (fn, 0);
20738 if (is_overloaded_fn (fn))
20739 fn = get_first_fn (fn);
20740
20741 if (!DECL_P (fn))
20742 /* Can't say anything more. */;
20743 else if (DECL_CLASS_SCOPE_P (fn))
20744 {
20745 location_t loc = cp_expr_loc_or_input_loc (t);
20746 inform (loc,
20747 "declarations in dependent base %qT are "
20748 "not found by unqualified lookup",
20749 DECL_CLASS_CONTEXT (fn));
20750 if (current_class_ptr)
20751 inform (loc,
20752 "use %<this->%D%> instead", function);
20753 else
20754 inform (loc,
20755 "use %<%T::%D%> instead",
20756 current_class_name, function);
20757 }
20758 else
20759 inform (DECL_SOURCE_LOCATION (fn),
20760 "%qD declared here, later in the "
20761 "translation unit", fn);
20762 if (strict)
20763 RETURN (error_mark_node);
20764 }
20765
20766 function = unq;
20767 }
20768 }
20769 if (identifier_p (function))
20770 {
20771 if (complain & tf_error)
20772 unqualified_name_lookup_error (function);
20773 RETURN (error_mark_node);
20774 }
20775 }
20776
20777 /* Remember that there was a reference to this entity. */
20778 if (function != NULL_TREE
20779 && DECL_P (function)
20780 && !mark_used (function, complain) && !(complain & tf_error))
20781 RETURN (error_mark_node);
20782
20783 if (!maybe_fold_fn_template_args (function, complain))
20784 return error_mark_node;
20785
20786 /* Put back tf_decltype for the actual call. */
20787 complain |= decltype_flag;
20788
20789 if (function == NULL_TREE)
20790 switch (CALL_EXPR_IFN (t))
20791 {
20792 case IFN_LAUNDER:
20793 gcc_assert (nargs == 1);
20794 if (vec_safe_length (call_args) != 1)
20795 {
20796 error_at (cp_expr_loc_or_input_loc (t),
20797 "wrong number of arguments to "
20798 "%<__builtin_launder%>");
20799 ret = error_mark_node;
20800 }
20801 else
20802 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20803 (*call_args)[0], complain);
20804 break;
20805
20806 case IFN_VEC_CONVERT:
20807 gcc_assert (nargs == 1);
20808 if (vec_safe_length (call_args) != 1)
20809 {
20810 error_at (cp_expr_loc_or_input_loc (t),
20811 "wrong number of arguments to "
20812 "%<__builtin_convertvector%>");
20813 ret = error_mark_node;
20814 break;
20815 }
20816 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20817 tsubst (TREE_TYPE (t), args,
20818 complain, in_decl),
20819 complain);
20820 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20821 RETURN (ret);
20822 break;
20823
20824 case IFN_SHUFFLEVECTOR:
20825 {
20826 ret = build_x_shufflevector (input_location, call_args,
20827 complain);
20828 if (ret != error_mark_node)
20829 RETURN (ret);
20830 break;
20831 }
20832
20833 default:
20834 /* Unsupported internal function with arguments. */
20835 gcc_unreachable ();
20836 }
20837 else if (TREE_CODE (function) == OFFSET_REF
20838 || TREE_CODE (function) == DOTSTAR_EXPR
20839 || TREE_CODE (function) == MEMBER_REF)
20840 ret = build_offset_ref_call_from_tree (function, &call_args,
20841 complain);
20842 else if (TREE_CODE (function) == COMPONENT_REF)
20843 {
20844 tree instance = TREE_OPERAND (function, 0);
20845 tree fn = TREE_OPERAND (function, 1);
20846
20847 if (processing_template_decl
20848 && (type_dependent_expression_p (instance)
20849 || (!BASELINK_P (fn)
20850 && TREE_CODE (fn) != FIELD_DECL)
20851 || type_dependent_expression_p (fn)
20852 || any_type_dependent_arguments_p (call_args)))
20853 ret = build_min_nt_call_vec (function, call_args);
20854 else if (!BASELINK_P (fn))
20855 ret = finish_call_expr (function, &call_args,
20856 /*disallow_virtual=*/false,
20857 /*koenig_p=*/false,
20858 complain);
20859 else
20860 ret = (build_new_method_call
20861 (instance, fn,
20862 &call_args, NULL_TREE,
20863 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20864 /*fn_p=*/NULL,
20865 complain));
20866 }
20867 else if (concept_check_p (function))
20868 {
20869 /* FUNCTION is a template-id referring to a concept definition. */
20870 tree id = unpack_concept_check (function);
20871 tree tmpl = TREE_OPERAND (id, 0);
20872 tree args = TREE_OPERAND (id, 1);
20873
20874 /* Calls to standard and variable concepts should have been
20875 previously diagnosed. */
20876 gcc_assert (function_concept_p (tmpl));
20877
20878 /* Ensure the result is wrapped as a call expression. */
20879 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20880 }
20881 else
20882 ret = finish_call_expr (function, &call_args,
20883 /*disallow_virtual=*/qualified_p,
20884 koenig_p,
20885 complain);
20886
20887 if (ret != error_mark_node)
20888 {
20889 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20890 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20891 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20892 if (op || ord || rev)
20893 {
20894 function = extract_call_expr (ret);
20895 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20896 CALL_EXPR_ORDERED_ARGS (function) = ord;
20897 CALL_EXPR_REVERSE_ARGS (function) = rev;
20898 }
20899 }
20900
20901 RETURN (ret);
20902 }
20903
20904 case COND_EXPR:
20905 {
20906 tree cond = RECUR (TREE_OPERAND (t, 0));
20907 cond = mark_rvalue_use (cond);
20908 tree folded_cond = fold_non_dependent_expr (cond, complain);
20909 tree exp1, exp2;
20910
20911 if (TREE_CODE (folded_cond) == INTEGER_CST)
20912 {
20913 if (integer_zerop (folded_cond))
20914 {
20915 ++c_inhibit_evaluation_warnings;
20916 exp1 = RECUR (TREE_OPERAND (t, 1));
20917 --c_inhibit_evaluation_warnings;
20918 exp2 = RECUR (TREE_OPERAND (t, 2));
20919 }
20920 else
20921 {
20922 exp1 = RECUR (TREE_OPERAND (t, 1));
20923 ++c_inhibit_evaluation_warnings;
20924 exp2 = RECUR (TREE_OPERAND (t, 2));
20925 --c_inhibit_evaluation_warnings;
20926 }
20927 cond = folded_cond;
20928 }
20929 else
20930 {
20931 exp1 = RECUR (TREE_OPERAND (t, 1));
20932 exp2 = RECUR (TREE_OPERAND (t, 2));
20933 }
20934
20935 warning_sentinel s(warn_duplicated_branches);
20936 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20937 cond, exp1, exp2, complain));
20938 }
20939
20940 case PSEUDO_DTOR_EXPR:
20941 {
20942 tree op0 = RECUR (TREE_OPERAND (t, 0));
20943 tree op1 = RECUR (TREE_OPERAND (t, 1));
20944 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20945 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20946 input_location));
20947 }
20948
20949 case TREE_LIST:
20950 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20951
20952 case COMPONENT_REF:
20953 {
20954 tree object;
20955 tree object_type;
20956 tree member;
20957 tree r;
20958
20959 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20960 args, complain, in_decl);
20961 /* Remember that there was a reference to this entity. */
20962 if (DECL_P (object)
20963 && !mark_used (object, complain) && !(complain & tf_error))
20964 RETURN (error_mark_node);
20965 object_type = TREE_TYPE (object);
20966
20967 member = TREE_OPERAND (t, 1);
20968 if (BASELINK_P (member))
20969 member = tsubst_baselink (member,
20970 non_reference (TREE_TYPE (object)),
20971 args, complain, in_decl);
20972 else
20973 member = tsubst_copy (member, args, complain, in_decl);
20974 if (member == error_mark_node)
20975 RETURN (error_mark_node);
20976
20977 if (object_type && TYPE_PTRMEMFUNC_P (object_type)
20978 && TREE_CODE (member) == FIELD_DECL)
20979 {
20980 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
20981 RETURN (r);
20982 }
20983 else if (TREE_CODE (member) == FIELD_DECL)
20984 {
20985 r = finish_non_static_data_member (member, object, NULL_TREE);
20986 if (TREE_CODE (r) == COMPONENT_REF)
20987 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20988 RETURN (r);
20989 }
20990 else if (type_dependent_expression_p (object))
20991 /* We can't do much here. */;
20992 else if (!CLASS_TYPE_P (object_type))
20993 {
20994 if (scalarish_type_p (object_type))
20995 {
20996 tree s = NULL_TREE;
20997 tree dtor = member;
20998
20999 if (TREE_CODE (dtor) == SCOPE_REF)
21000 {
21001 s = TREE_OPERAND (dtor, 0);
21002 dtor = TREE_OPERAND (dtor, 1);
21003 }
21004 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
21005 {
21006 dtor = TREE_OPERAND (dtor, 0);
21007 if (TYPE_P (dtor))
21008 RETURN (finish_pseudo_destructor_expr
21009 (object, s, dtor, input_location));
21010 }
21011 }
21012 }
21013 else if (TREE_CODE (member) == SCOPE_REF
21014 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
21015 {
21016 /* Lookup the template functions now that we know what the
21017 scope is. */
21018 tree scope = TREE_OPERAND (member, 0);
21019 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
21020 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
21021 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
21022 /*complain=*/false);
21023 if (BASELINK_P (member))
21024 {
21025 BASELINK_FUNCTIONS (member)
21026 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
21027 args);
21028 member = (adjust_result_of_qualified_name_lookup
21029 (member, BINFO_TYPE (BASELINK_BINFO (member)),
21030 object_type));
21031 }
21032 else
21033 {
21034 qualified_name_lookup_error (scope, tmpl, member,
21035 input_location);
21036 RETURN (error_mark_node);
21037 }
21038 }
21039 else if (TREE_CODE (member) == SCOPE_REF
21040 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
21041 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
21042 {
21043 if (complain & tf_error)
21044 {
21045 if (TYPE_P (TREE_OPERAND (member, 0)))
21046 error ("%qT is not a class or namespace",
21047 TREE_OPERAND (member, 0));
21048 else
21049 error ("%qD is not a class or namespace",
21050 TREE_OPERAND (member, 0));
21051 }
21052 RETURN (error_mark_node);
21053 }
21054
21055 r = finish_class_member_access_expr (object, member,
21056 /*template_p=*/false,
21057 complain);
21058 if (TREE_CODE (r) == COMPONENT_REF)
21059 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
21060 RETURN (r);
21061 }
21062
21063 case THROW_EXPR:
21064 RETURN (build_throw
21065 (input_location, RECUR (TREE_OPERAND (t, 0))));
21066
21067 case CONSTRUCTOR:
21068 {
21069 vec<constructor_elt, va_gc> *n;
21070 constructor_elt *ce;
21071 unsigned HOST_WIDE_INT idx;
21072 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21073 bool process_index_p;
21074 int newlen;
21075 bool need_copy_p = false;
21076 tree r;
21077
21078 if (type == error_mark_node)
21079 RETURN (error_mark_node);
21080
21081 /* We do not want to process the index of aggregate
21082 initializers as they are identifier nodes which will be
21083 looked up by digest_init. */
21084 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
21085
21086 if (null_member_pointer_value_p (t))
21087 {
21088 gcc_assert (same_type_p (type, TREE_TYPE (t)));
21089 RETURN (t);
21090 }
21091
21092 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
21093 newlen = vec_safe_length (n);
21094 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
21095 {
21096 if (ce->index && process_index_p
21097 /* An identifier index is looked up in the type
21098 being initialized, not the current scope. */
21099 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
21100 ce->index = RECUR (ce->index);
21101
21102 if (PACK_EXPANSION_P (ce->value))
21103 {
21104 /* Substitute into the pack expansion. */
21105 ce->value = tsubst_pack_expansion (ce->value, args, complain,
21106 in_decl);
21107
21108 if (ce->value == error_mark_node
21109 || PACK_EXPANSION_P (ce->value))
21110 ;
21111 else if (TREE_VEC_LENGTH (ce->value) == 1)
21112 /* Just move the argument into place. */
21113 ce->value = TREE_VEC_ELT (ce->value, 0);
21114 else
21115 {
21116 /* Update the length of the final CONSTRUCTOR
21117 arguments vector, and note that we will need to
21118 copy.*/
21119 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
21120 need_copy_p = true;
21121 }
21122 }
21123 else
21124 ce->value = RECUR (ce->value);
21125 }
21126
21127 if (need_copy_p)
21128 {
21129 vec<constructor_elt, va_gc> *old_n = n;
21130
21131 vec_alloc (n, newlen);
21132 FOR_EACH_VEC_ELT (*old_n, idx, ce)
21133 {
21134 if (TREE_CODE (ce->value) == TREE_VEC)
21135 {
21136 int i, len = TREE_VEC_LENGTH (ce->value);
21137 for (i = 0; i < len; ++i)
21138 CONSTRUCTOR_APPEND_ELT (n, 0,
21139 TREE_VEC_ELT (ce->value, i));
21140 }
21141 else
21142 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
21143 }
21144 }
21145
21146 r = build_constructor (init_list_type_node, n);
21147 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
21148 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
21149 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
21150
21151 if (TREE_HAS_CONSTRUCTOR (t))
21152 {
21153 fcl_t cl = fcl_functional;
21154 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
21155 cl = fcl_c99;
21156 RETURN (finish_compound_literal (type, r, complain, cl));
21157 }
21158
21159 TREE_TYPE (r) = type;
21160 RETURN (r);
21161 }
21162
21163 case TYPEID_EXPR:
21164 {
21165 tree operand_0 = TREE_OPERAND (t, 0);
21166 if (TYPE_P (operand_0))
21167 {
21168 operand_0 = tsubst (operand_0, args, complain, in_decl);
21169 RETURN (get_typeid (operand_0, complain));
21170 }
21171 else
21172 {
21173 operand_0 = RECUR (operand_0);
21174 RETURN (build_typeid (operand_0, complain));
21175 }
21176 }
21177
21178 case VAR_DECL:
21179 if (!args)
21180 RETURN (t);
21181 /* Fall through */
21182
21183 case PARM_DECL:
21184 {
21185 tree r = tsubst_copy (t, args, complain, in_decl);
21186 /* ??? We're doing a subset of finish_id_expression here. */
21187 if (tree wrap = maybe_get_tls_wrapper_call (r))
21188 /* Replace an evaluated use of the thread_local variable with
21189 a call to its wrapper. */
21190 r = wrap;
21191 else if (outer_automatic_var_p (r))
21192 r = process_outer_var_ref (r, complain);
21193
21194 if (!TYPE_REF_P (TREE_TYPE (t)))
21195 /* If the original type was a reference, we'll be wrapped in
21196 the appropriate INDIRECT_REF. */
21197 r = convert_from_reference (r);
21198 RETURN (r);
21199 }
21200
21201 case VA_ARG_EXPR:
21202 {
21203 tree op0 = RECUR (TREE_OPERAND (t, 0));
21204 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21205 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
21206 }
21207
21208 case OFFSETOF_EXPR:
21209 {
21210 tree object_ptr
21211 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
21212 in_decl, /*function_p=*/false,
21213 /*integral_constant_expression_p=*/false);
21214 RETURN (finish_offsetof (object_ptr,
21215 RECUR (TREE_OPERAND (t, 0)),
21216 EXPR_LOCATION (t)));
21217 }
21218
21219 case ADDRESSOF_EXPR:
21220 RETURN (cp_build_addressof (EXPR_LOCATION (t),
21221 RECUR (TREE_OPERAND (t, 0)), complain));
21222
21223 case TRAIT_EXPR:
21224 {
21225 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
21226 complain, in_decl);
21227 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
21228 complain, in_decl);
21229 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
21230 TRAIT_EXPR_KIND (t), type1, type2));
21231 }
21232
21233 case STMT_EXPR:
21234 {
21235 tree old_stmt_expr = cur_stmt_expr;
21236 tree stmt_expr = begin_stmt_expr ();
21237
21238 cur_stmt_expr = stmt_expr;
21239 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
21240 integral_constant_expression_p);
21241 stmt_expr = finish_stmt_expr (stmt_expr, false);
21242 cur_stmt_expr = old_stmt_expr;
21243
21244 /* If the resulting list of expression statement is empty,
21245 fold it further into void_node. */
21246 if (empty_expr_stmt_p (stmt_expr))
21247 stmt_expr = void_node;
21248
21249 RETURN (stmt_expr);
21250 }
21251
21252 case LAMBDA_EXPR:
21253 {
21254 if (complain & tf_partial)
21255 {
21256 /* We don't have a full set of template arguments yet; don't touch
21257 the lambda at all. */
21258 gcc_assert (processing_template_decl);
21259 return t;
21260 }
21261 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
21262
21263 RETURN (build_lambda_object (r));
21264 }
21265
21266 case TRANSACTION_EXPR:
21267 RETURN (tsubst_expr(t, args, complain, in_decl,
21268 integral_constant_expression_p));
21269
21270 case PAREN_EXPR:
21271 if (REF_PARENTHESIZED_P (t))
21272 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
21273 else
21274 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21275 {
21276 tree op0 = RECUR (TREE_OPERAND (t, 0));
21277 RETURN (build1_loc (input_location, PAREN_EXPR,
21278 TREE_TYPE (op0), op0));
21279 }
21280
21281 case VEC_PERM_EXPR:
21282 {
21283 tree op0 = RECUR (TREE_OPERAND (t, 0));
21284 tree op1 = RECUR (TREE_OPERAND (t, 1));
21285 tree op2 = RECUR (TREE_OPERAND (t, 2));
21286 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
21287 complain));
21288 }
21289
21290 case REQUIRES_EXPR:
21291 {
21292 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
21293 RETURN (r);
21294 }
21295
21296 case RANGE_EXPR:
21297 /* No need to substitute further, a RANGE_EXPR will always be built
21298 with constant operands. */
21299 RETURN (t);
21300
21301 case NON_LVALUE_EXPR:
21302 case VIEW_CONVERT_EXPR:
21303 if (location_wrapper_p (t))
21304 /* We need to do this here as well as in tsubst_copy so we get the
21305 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21306 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
21307 EXPR_LOCATION (t)));
21308 /* fallthrough. */
21309
21310 default:
21311 /* Handle Objective-C++ constructs, if appropriate. */
21312 {
21313 tree subst
21314 = objcp_tsubst_copy_and_build (t, args, complain,
21315 in_decl, /*function_p=*/false);
21316 if (subst)
21317 RETURN (subst);
21318 }
21319 RETURN (tsubst_copy (t, args, complain, in_decl));
21320 }
21321
21322 #undef RECUR
21323 #undef RETURN
21324 out:
21325 input_location = save_loc;
21326 return retval;
21327 }
21328
21329 /* Verify that the instantiated ARGS are valid. For type arguments,
21330 make sure that the type's linkage is ok. For non-type arguments,
21331 make sure they are constants if they are integral or enumerations.
21332 Emit an error under control of COMPLAIN, and return TRUE on error. */
21333
21334 static bool
21335 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
21336 {
21337 if (dependent_template_arg_p (t))
21338 return false;
21339 if (ARGUMENT_PACK_P (t))
21340 {
21341 tree vec = ARGUMENT_PACK_ARGS (t);
21342 int len = TREE_VEC_LENGTH (vec);
21343 bool result = false;
21344 int i;
21345
21346 for (i = 0; i < len; ++i)
21347 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
21348 result = true;
21349 return result;
21350 }
21351 else if (TYPE_P (t))
21352 {
21353 /* [basic.link]: A name with no linkage (notably, the name
21354 of a class or enumeration declared in a local scope)
21355 shall not be used to declare an entity with linkage.
21356 This implies that names with no linkage cannot be used as
21357 template arguments
21358
21359 DR 757 relaxes this restriction for C++0x. */
21360 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
21361 : no_linkage_check (t, /*relaxed_p=*/false));
21362
21363 if (nt)
21364 {
21365 /* DR 488 makes use of a type with no linkage cause
21366 type deduction to fail. */
21367 if (complain & tf_error)
21368 {
21369 if (TYPE_UNNAMED_P (nt))
21370 error ("%qT is/uses unnamed type", t);
21371 else
21372 error ("template argument for %qD uses local type %qT",
21373 tmpl, t);
21374 }
21375 return true;
21376 }
21377 /* In order to avoid all sorts of complications, we do not
21378 allow variably-modified types as template arguments. */
21379 else if (variably_modified_type_p (t, NULL_TREE))
21380 {
21381 if (complain & tf_error)
21382 error ("%qT is a variably modified type", t);
21383 return true;
21384 }
21385 }
21386 /* Class template and alias template arguments should be OK. */
21387 else if (DECL_TYPE_TEMPLATE_P (t))
21388 ;
21389 /* A non-type argument of integral or enumerated type must be a
21390 constant. */
21391 else if (TREE_TYPE (t)
21392 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
21393 && !REFERENCE_REF_P (t)
21394 && !TREE_CONSTANT (t))
21395 {
21396 if (complain & tf_error)
21397 error ("integral expression %qE is not constant", t);
21398 return true;
21399 }
21400 return false;
21401 }
21402
21403 static bool
21404 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
21405 {
21406 int ix, len = DECL_NTPARMS (tmpl);
21407 bool result = false;
21408
21409 for (ix = 0; ix != len; ix++)
21410 {
21411 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
21412 result = true;
21413 }
21414 if (result && (complain & tf_error))
21415 error (" trying to instantiate %qD", tmpl);
21416 return result;
21417 }
21418
21419 /* We're out of SFINAE context now, so generate diagnostics for the access
21420 errors we saw earlier when instantiating D from TMPL and ARGS. */
21421
21422 static void
21423 recheck_decl_substitution (tree d, tree tmpl, tree args)
21424 {
21425 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
21426 tree type = TREE_TYPE (pattern);
21427 location_t loc = input_location;
21428
21429 push_access_scope (d);
21430 push_deferring_access_checks (dk_no_deferred);
21431 input_location = DECL_SOURCE_LOCATION (pattern);
21432 tsubst (type, args, tf_warning_or_error, d);
21433 input_location = loc;
21434 pop_deferring_access_checks ();
21435 pop_access_scope (d);
21436 }
21437
21438 /* Instantiate the indicated variable, function, or alias template TMPL with
21439 the template arguments in TARG_PTR. */
21440
21441 static tree
21442 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
21443 {
21444 tree targ_ptr = orig_args;
21445 tree fndecl;
21446 tree gen_tmpl;
21447 tree spec;
21448 bool access_ok = true;
21449
21450 if (tmpl == error_mark_node)
21451 return error_mark_node;
21452
21453 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
21454
21455 if (modules_p ())
21456 lazy_load_pendings (tmpl);
21457
21458 /* If this function is a clone, handle it specially. */
21459 if (DECL_CLONED_FUNCTION_P (tmpl))
21460 {
21461 tree spec;
21462 tree clone;
21463
21464 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21465 DECL_CLONED_FUNCTION. */
21466 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
21467 targ_ptr, complain);
21468 if (spec == error_mark_node)
21469 return error_mark_node;
21470
21471 /* Look for the clone. */
21472 FOR_EACH_CLONE (clone, spec)
21473 if (DECL_NAME (clone) == DECL_NAME (tmpl))
21474 return clone;
21475 /* We should always have found the clone by now. */
21476 gcc_unreachable ();
21477 return NULL_TREE;
21478 }
21479
21480 if (targ_ptr == error_mark_node)
21481 return error_mark_node;
21482
21483 /* Check to see if we already have this specialization. */
21484 gen_tmpl = most_general_template (tmpl);
21485 if (TMPL_ARGS_DEPTH (targ_ptr)
21486 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
21487 /* targ_ptr only has the innermost template args, so add the outer ones
21488 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21489 the case of a non-dependent call within a template definition). */
21490 targ_ptr = (add_outermost_template_args
21491 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
21492 targ_ptr));
21493
21494 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21495 but it doesn't seem to be on the hot path. */
21496 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
21497
21498 gcc_checking_assert (tmpl == gen_tmpl
21499 || ((fndecl
21500 = retrieve_specialization (tmpl, orig_args, 0))
21501 == spec)
21502 || fndecl == NULL_TREE);
21503
21504 if (spec != NULL_TREE)
21505 {
21506 if (FNDECL_HAS_ACCESS_ERRORS (spec))
21507 {
21508 if (complain & tf_error)
21509 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
21510 return error_mark_node;
21511 }
21512 return spec;
21513 }
21514
21515 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
21516 complain))
21517 return error_mark_node;
21518
21519 /* We are building a FUNCTION_DECL, during which the access of its
21520 parameters and return types have to be checked. However this
21521 FUNCTION_DECL which is the desired context for access checking
21522 is not built yet. We solve this chicken-and-egg problem by
21523 deferring all checks until we have the FUNCTION_DECL. */
21524 push_deferring_access_checks (dk_deferred);
21525
21526 /* Instantiation of the function happens in the context of the function
21527 template, not the context of the overload resolution we're doing. */
21528 push_to_top_level ();
21529 /* If there are dependent arguments, e.g. because we're doing partial
21530 ordering, make sure processing_template_decl stays set. */
21531 if (uses_template_parms (targ_ptr))
21532 ++processing_template_decl;
21533 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21534 {
21535 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
21536 complain, gen_tmpl, true);
21537 push_nested_class (ctx);
21538 }
21539
21540 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
21541
21542 fndecl = NULL_TREE;
21543 if (VAR_P (pattern))
21544 {
21545 /* We need to determine if we're using a partial or explicit
21546 specialization now, because the type of the variable could be
21547 different. */
21548 tree tid = lookup_template_variable (tmpl, targ_ptr);
21549 tree elt = most_specialized_partial_spec (tid, complain);
21550 if (elt == error_mark_node)
21551 pattern = error_mark_node;
21552 else if (elt)
21553 {
21554 tree partial_tmpl = TREE_VALUE (elt);
21555 tree partial_args = TREE_PURPOSE (elt);
21556 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21557 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21558 }
21559 }
21560
21561 /* Substitute template parameters to obtain the specialization. */
21562 if (fndecl == NULL_TREE)
21563 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21564 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21565 pop_nested_class ();
21566 pop_from_top_level ();
21567
21568 if (fndecl == error_mark_node)
21569 {
21570 pop_deferring_access_checks ();
21571 return error_mark_node;
21572 }
21573
21574 /* The DECL_TI_TEMPLATE should always be the immediate parent
21575 template, not the most general template. */
21576 DECL_TI_TEMPLATE (fndecl) = tmpl;
21577 DECL_TI_ARGS (fndecl) = targ_ptr;
21578
21579 set_instantiating_module (fndecl);
21580
21581 /* Now we know the specialization, compute access previously
21582 deferred. Do no access control for inheriting constructors,
21583 as we already checked access for the inherited constructor. */
21584 if (!(flag_new_inheriting_ctors
21585 && DECL_INHERITED_CTOR (fndecl)))
21586 {
21587 push_access_scope (fndecl);
21588 if (!perform_deferred_access_checks (complain))
21589 access_ok = false;
21590 pop_access_scope (fndecl);
21591 }
21592 pop_deferring_access_checks ();
21593
21594 /* If we've just instantiated the main entry point for a function,
21595 instantiate all the alternate entry points as well. We do this
21596 by cloning the instantiation of the main entry point, not by
21597 instantiating the template clones. */
21598 if (tree chain = DECL_CHAIN (gen_tmpl))
21599 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21600 clone_cdtor (fndecl, /*update_methods=*/false);
21601
21602 if (!access_ok)
21603 {
21604 if (!(complain & tf_error))
21605 {
21606 /* Remember to reinstantiate when we're out of SFINAE so the user
21607 can see the errors. */
21608 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21609 }
21610 return error_mark_node;
21611 }
21612 return fndecl;
21613 }
21614
21615 /* Wrapper for instantiate_template_1. */
21616
21617 tree
21618 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21619 {
21620 tree ret;
21621 timevar_push (TV_TEMPLATE_INST);
21622 ret = instantiate_template_1 (tmpl, orig_args, complain);
21623 timevar_pop (TV_TEMPLATE_INST);
21624 return ret;
21625 }
21626
21627 /* Instantiate the alias template TMPL with ARGS. Also push a template
21628 instantiation level, which instantiate_template doesn't do because
21629 functions and variables have sufficient context established by the
21630 callers. */
21631
21632 static tree
21633 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21634 {
21635 if (tmpl == error_mark_node || args == error_mark_node)
21636 return error_mark_node;
21637
21638 args =
21639 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21640 args, tmpl, complain,
21641 /*require_all_args=*/true,
21642 /*use_default_args=*/true);
21643
21644 /* FIXME check for satisfaction in check_instantiated_args. */
21645 if (flag_concepts
21646 && !any_dependent_template_arguments_p (args)
21647 && !constraints_satisfied_p (tmpl, args))
21648 {
21649 if (complain & tf_error)
21650 {
21651 auto_diagnostic_group d;
21652 error ("template constraint failure for %qD", tmpl);
21653 diagnose_constraints (input_location, tmpl, args);
21654 }
21655 return error_mark_node;
21656 }
21657
21658 if (!push_tinst_level (tmpl, args))
21659 return error_mark_node;
21660 tree r = instantiate_template (tmpl, args, complain);
21661 pop_tinst_level ();
21662
21663 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
21664 {
21665 /* An alias template specialization can be dependent
21666 even if its underlying type is not. */
21667 TYPE_DEPENDENT_P (d) = true;
21668 TYPE_DEPENDENT_P_VALID (d) = true;
21669 /* Sometimes a dependent alias spec is equivalent to its expansion,
21670 sometimes not. So always use structural_comptypes. */
21671 SET_TYPE_STRUCTURAL_EQUALITY (d);
21672 }
21673
21674 return r;
21675 }
21676
21677 /* PARM is a template parameter pack for FN. Returns true iff
21678 PARM is used in a deducible way in the argument list of FN. */
21679
21680 static bool
21681 pack_deducible_p (tree parm, tree fn)
21682 {
21683 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21684 for (; t; t = TREE_CHAIN (t))
21685 {
21686 tree type = TREE_VALUE (t);
21687 tree packs;
21688 if (!PACK_EXPANSION_P (type))
21689 continue;
21690 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21691 packs; packs = TREE_CHAIN (packs))
21692 if (template_args_equal (TREE_VALUE (packs), parm))
21693 {
21694 /* The template parameter pack is used in a function parameter
21695 pack. If this is the end of the parameter list, the
21696 template parameter pack is deducible. */
21697 if (TREE_CHAIN (t) == void_list_node)
21698 return true;
21699 else
21700 /* Otherwise, not. Well, it could be deduced from
21701 a non-pack parameter, but doing so would end up with
21702 a deduction mismatch, so don't bother. */
21703 return false;
21704 }
21705 }
21706 /* The template parameter pack isn't used in any function parameter
21707 packs, but it might be used deeper, e.g. tuple<Args...>. */
21708 return true;
21709 }
21710
21711 /* Subroutine of fn_type_unification: check non-dependent parms for
21712 convertibility. */
21713
21714 static int
21715 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21716 tree fn, unification_kind_t strict, int flags,
21717 struct conversion **convs, bool explain_p)
21718 {
21719 /* Non-constructor methods need to leave a conversion for 'this', which
21720 isn't included in nargs here. */
21721 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21722 && !DECL_CONSTRUCTOR_P (fn));
21723
21724 for (unsigned ia = 0;
21725 parms && parms != void_list_node && ia < nargs; )
21726 {
21727 tree parm = TREE_VALUE (parms);
21728
21729 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21730 && (!TREE_CHAIN (parms)
21731 || TREE_CHAIN (parms) == void_list_node))
21732 /* For a function parameter pack that occurs at the end of the
21733 parameter-declaration-list, the type A of each remaining
21734 argument of the call is compared with the type P of the
21735 declarator-id of the function parameter pack. */
21736 break;
21737
21738 parms = TREE_CHAIN (parms);
21739
21740 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21741 /* For a function parameter pack that does not occur at the
21742 end of the parameter-declaration-list, the type of the
21743 parameter pack is a non-deduced context. */
21744 continue;
21745
21746 if (!uses_template_parms (parm))
21747 {
21748 tree arg = args[ia];
21749 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21750 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21751
21752 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21753 conv_p, explain_p))
21754 return 1;
21755 }
21756
21757 ++ia;
21758 }
21759
21760 return 0;
21761 }
21762
21763 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21764 NARGS elements of the arguments that are being used when calling
21765 it. TARGS is a vector into which the deduced template arguments
21766 are placed.
21767
21768 Returns either a FUNCTION_DECL for the matching specialization of FN or
21769 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21770 true, diagnostics will be printed to explain why it failed.
21771
21772 If FN is a conversion operator, or we are trying to produce a specific
21773 specialization, RETURN_TYPE is the return type desired.
21774
21775 The EXPLICIT_TARGS are explicit template arguments provided via a
21776 template-id.
21777
21778 The parameter STRICT is one of:
21779
21780 DEDUCE_CALL:
21781 We are deducing arguments for a function call, as in
21782 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21783 deducing arguments for a call to the result of a conversion
21784 function template, as in [over.call.object].
21785
21786 DEDUCE_CONV:
21787 We are deducing arguments for a conversion function, as in
21788 [temp.deduct.conv].
21789
21790 DEDUCE_EXACT:
21791 We are deducing arguments when doing an explicit instantiation
21792 as in [temp.explicit], when determining an explicit specialization
21793 as in [temp.expl.spec], or when taking the address of a function
21794 template, as in [temp.deduct.funcaddr]. */
21795
21796 tree
21797 fn_type_unification (tree fn,
21798 tree explicit_targs,
21799 tree targs,
21800 const tree *args,
21801 unsigned int nargs,
21802 tree return_type,
21803 unification_kind_t strict,
21804 int flags,
21805 struct conversion **convs,
21806 bool explain_p,
21807 bool decltype_p)
21808 {
21809 tree parms;
21810 tree fntype;
21811 tree decl = NULL_TREE;
21812 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21813 bool ok;
21814 static int deduction_depth;
21815 /* type_unification_real will pass back any access checks from default
21816 template argument substitution. */
21817 vec<deferred_access_check, va_gc> *checks = NULL;
21818 /* We don't have all the template args yet. */
21819 bool incomplete = true;
21820
21821 tree orig_fn = fn;
21822 if (flag_new_inheriting_ctors)
21823 fn = strip_inheriting_ctors (fn);
21824
21825 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21826 tree r = error_mark_node;
21827
21828 tree full_targs = targs;
21829 if (TMPL_ARGS_DEPTH (targs)
21830 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21831 full_targs = (add_outermost_template_args
21832 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21833 targs));
21834
21835 if (decltype_p)
21836 complain |= tf_decltype;
21837
21838 /* In C++0x, it's possible to have a function template whose type depends
21839 on itself recursively. This is most obvious with decltype, but can also
21840 occur with enumeration scope (c++/48969). So we need to catch infinite
21841 recursion and reject the substitution at deduction time; this function
21842 will return error_mark_node for any repeated substitution.
21843
21844 This also catches excessive recursion such as when f<N> depends on
21845 f<N-1> across all integers, and returns error_mark_node for all the
21846 substitutions back up to the initial one.
21847
21848 This is, of course, not reentrant. */
21849 if (excessive_deduction_depth)
21850 return error_mark_node;
21851 ++deduction_depth;
21852
21853 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21854
21855 fntype = TREE_TYPE (fn);
21856 if (explicit_targs)
21857 {
21858 /* [temp.deduct]
21859
21860 The specified template arguments must match the template
21861 parameters in kind (i.e., type, nontype, template), and there
21862 must not be more arguments than there are parameters;
21863 otherwise type deduction fails.
21864
21865 Nontype arguments must match the types of the corresponding
21866 nontype template parameters, or must be convertible to the
21867 types of the corresponding nontype parameters as specified in
21868 _temp.arg.nontype_, otherwise type deduction fails.
21869
21870 All references in the function type of the function template
21871 to the corresponding template parameters are replaced by the
21872 specified template argument values. If a substitution in a
21873 template parameter or in the function type of the function
21874 template results in an invalid type, type deduction fails. */
21875 int i, len = TREE_VEC_LENGTH (tparms);
21876 location_t loc = input_location;
21877 incomplete = false;
21878
21879 if (explicit_targs == error_mark_node)
21880 goto fail;
21881
21882 if (TMPL_ARGS_DEPTH (explicit_targs)
21883 < TMPL_ARGS_DEPTH (full_targs))
21884 explicit_targs = add_outermost_template_args (full_targs,
21885 explicit_targs);
21886
21887 /* Adjust any explicit template arguments before entering the
21888 substitution context. */
21889 explicit_targs
21890 = (coerce_template_parms (tparms, explicit_targs, fn,
21891 complain|tf_partial,
21892 /*require_all_args=*/false,
21893 /*use_default_args=*/false));
21894 if (explicit_targs == error_mark_node)
21895 goto fail;
21896
21897 /* Substitute the explicit args into the function type. This is
21898 necessary so that, for instance, explicitly declared function
21899 arguments can match null pointed constants. If we were given
21900 an incomplete set of explicit args, we must not do semantic
21901 processing during substitution as we could create partial
21902 instantiations. */
21903 for (i = 0; i < len; i++)
21904 {
21905 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21906 bool parameter_pack = false;
21907 tree targ = TREE_VEC_ELT (explicit_targs, i);
21908
21909 /* Dig out the actual parm. */
21910 if (TREE_CODE (parm) == TYPE_DECL
21911 || TREE_CODE (parm) == TEMPLATE_DECL)
21912 {
21913 parm = TREE_TYPE (parm);
21914 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21915 }
21916 else if (TREE_CODE (parm) == PARM_DECL)
21917 {
21918 parm = DECL_INITIAL (parm);
21919 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21920 }
21921
21922 if (targ == NULL_TREE)
21923 /* No explicit argument for this template parameter. */
21924 incomplete = true;
21925 else if (parameter_pack && pack_deducible_p (parm, fn))
21926 {
21927 /* Mark the argument pack as "incomplete". We could
21928 still deduce more arguments during unification.
21929 We remove this mark in type_unification_real. */
21930 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21931 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21932 = ARGUMENT_PACK_ARGS (targ);
21933
21934 /* We have some incomplete argument packs. */
21935 incomplete = true;
21936 }
21937 }
21938
21939 if (incomplete)
21940 {
21941 if (!push_tinst_level (fn, explicit_targs))
21942 {
21943 excessive_deduction_depth = true;
21944 goto fail;
21945 }
21946 ++processing_template_decl;
21947 input_location = DECL_SOURCE_LOCATION (fn);
21948 /* Ignore any access checks; we'll see them again in
21949 instantiate_template and they might have the wrong
21950 access path at this point. */
21951 push_deferring_access_checks (dk_deferred);
21952 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21953 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21954 pop_deferring_access_checks ();
21955 input_location = loc;
21956 --processing_template_decl;
21957 pop_tinst_level ();
21958
21959 if (fntype == error_mark_node)
21960 goto fail;
21961 }
21962
21963 /* Place the explicitly specified arguments in TARGS. */
21964 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21965 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21966 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21967 if (!incomplete && CHECKING_P
21968 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21969 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21970 (targs, NUM_TMPL_ARGS (explicit_targs));
21971 }
21972
21973 if (return_type && strict != DEDUCE_CALL)
21974 {
21975 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21976 new_args[0] = return_type;
21977 memcpy (new_args + 1, args, nargs * sizeof (tree));
21978 args = new_args;
21979 ++nargs;
21980 }
21981
21982 if (!incomplete)
21983 goto deduced;
21984
21985 /* Never do unification on the 'this' parameter. */
21986 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21987
21988 if (return_type && strict == DEDUCE_CALL)
21989 {
21990 /* We're deducing for a call to the result of a template conversion
21991 function. The parms we really want are in return_type. */
21992 if (INDIRECT_TYPE_P (return_type))
21993 return_type = TREE_TYPE (return_type);
21994 parms = TYPE_ARG_TYPES (return_type);
21995 }
21996 else if (return_type)
21997 {
21998 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21999 }
22000
22001 /* We allow incomplete unification without an error message here
22002 because the standard doesn't seem to explicitly prohibit it. Our
22003 callers must be ready to deal with unification failures in any
22004 event. */
22005
22006 /* If we aren't explaining yet, push tinst context so we can see where
22007 any errors (e.g. from class instantiations triggered by instantiation
22008 of default template arguments) come from. If we are explaining, this
22009 context is redundant. */
22010 if (!explain_p && !push_tinst_level (fn, targs))
22011 {
22012 excessive_deduction_depth = true;
22013 goto fail;
22014 }
22015
22016 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22017 full_targs, parms, args, nargs, /*subr=*/0,
22018 strict, &checks, explain_p);
22019 if (!explain_p)
22020 pop_tinst_level ();
22021 if (!ok)
22022 goto fail;
22023
22024 /* Now that we have bindings for all of the template arguments,
22025 ensure that the arguments deduced for the template template
22026 parameters have compatible template parameter lists. We cannot
22027 check this property before we have deduced all template
22028 arguments, because the template parameter types of a template
22029 template parameter might depend on prior template parameters
22030 deduced after the template template parameter. The following
22031 ill-formed example illustrates this issue:
22032
22033 template<typename T, template<T> class C> void f(C<5>, T);
22034
22035 template<int N> struct X {};
22036
22037 void g() {
22038 f(X<5>(), 5l); // error: template argument deduction fails
22039 }
22040
22041 The template parameter list of 'C' depends on the template type
22042 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22043 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22044 time that we deduce 'C'. */
22045 if (!template_template_parm_bindings_ok_p
22046 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
22047 {
22048 unify_inconsistent_template_template_parameters (explain_p);
22049 goto fail;
22050 }
22051
22052 deduced:
22053
22054 /* CWG2369: Check satisfaction before non-deducible conversions. */
22055 if (!constraints_satisfied_p (fn, targs))
22056 {
22057 if (explain_p)
22058 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
22059 goto fail;
22060 }
22061
22062 /* DR 1391: All parameters have args, now check non-dependent parms for
22063 convertibility. We don't do this if all args were explicitly specified,
22064 as the standard says that we substitute explicit args immediately. */
22065 if (incomplete
22066 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
22067 convs, explain_p))
22068 goto fail;
22069
22070 /* All is well so far. Now, check:
22071
22072 [temp.deduct]
22073
22074 When all template arguments have been deduced, all uses of
22075 template parameters in nondeduced contexts are replaced with
22076 the corresponding deduced argument values. If the
22077 substitution results in an invalid type, as described above,
22078 type deduction fails. */
22079 if (!push_tinst_level (fn, targs))
22080 {
22081 excessive_deduction_depth = true;
22082 goto fail;
22083 }
22084
22085 /* Also collect access checks from the instantiation. */
22086 reopen_deferring_access_checks (checks);
22087
22088 decl = instantiate_template (fn, targs, complain);
22089
22090 checks = get_deferred_access_checks ();
22091 pop_deferring_access_checks ();
22092
22093 pop_tinst_level ();
22094
22095 if (decl == error_mark_node)
22096 goto fail;
22097
22098 /* Now perform any access checks encountered during substitution. */
22099 push_access_scope (decl);
22100 ok = perform_access_checks (checks, complain);
22101 pop_access_scope (decl);
22102 if (!ok)
22103 goto fail;
22104
22105 /* If we're looking for an exact match, check that what we got
22106 is indeed an exact match. It might not be if some template
22107 parameters are used in non-deduced contexts. But don't check
22108 for an exact match if we have dependent template arguments;
22109 in that case we're doing partial ordering, and we already know
22110 that we have two candidates that will provide the actual type. */
22111 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
22112 {
22113 tree substed = TREE_TYPE (decl);
22114 unsigned int i;
22115
22116 tree sarg
22117 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
22118 if (return_type)
22119 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
22120 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
22121 if (!same_type_p (args[i], TREE_VALUE (sarg)))
22122 {
22123 unify_type_mismatch (explain_p, args[i],
22124 TREE_VALUE (sarg));
22125 goto fail;
22126 }
22127 if ((i < nargs || sarg)
22128 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22129 doesn't contain the trailing void, and conv fns are always (). */
22130 && !DECL_CONV_FN_P (decl))
22131 {
22132 unsigned nsargs = i + list_length (sarg);
22133 unify_arity (explain_p, nargs, nsargs);
22134 goto fail;
22135 }
22136 }
22137
22138 /* After doing deduction with the inherited constructor, actually return an
22139 instantiation of the inheriting constructor. */
22140 if (orig_fn != fn)
22141 decl = instantiate_template (orig_fn, targs, complain);
22142
22143 r = decl;
22144
22145 fail:
22146 --deduction_depth;
22147 if (excessive_deduction_depth)
22148 {
22149 if (deduction_depth == 0)
22150 /* Reset once we're all the way out. */
22151 excessive_deduction_depth = false;
22152 }
22153
22154 return r;
22155 }
22156
22157 /* Returns true iff PARM is a forwarding reference in the context of
22158 template argument deduction for TMPL. */
22159
22160 static bool
22161 forwarding_reference_p (tree parm, tree tmpl)
22162 {
22163 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22164 cv-unqualified template parameter ..." */
22165 if (TYPE_REF_P (parm)
22166 && TYPE_REF_IS_RVALUE (parm)
22167 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
22168 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
22169 {
22170 parm = TREE_TYPE (parm);
22171 /* [temp.deduct.call], "... that does not represent a template parameter
22172 of a class template (during class template argument deduction)." */
22173 if (tmpl
22174 && deduction_guide_p (tmpl)
22175 && DECL_ARTIFICIAL (tmpl))
22176 {
22177 /* Since the template parameters of a synthesized guide consist of
22178 the template parameters of the class template followed by those of
22179 the constructor (if any), we can tell if PARM represents a template
22180 parameter of the class template by comparing its index with the
22181 arity of the class template. */
22182 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
22183 if (TEMPLATE_TYPE_IDX (parm)
22184 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
22185 return false;
22186 }
22187 return true;
22188 }
22189 return false;
22190 }
22191
22192 /* Adjust types before performing type deduction, as described in
22193 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22194 sections are symmetric. PARM is the type of a function parameter
22195 or the return type of the conversion function. ARG is the type of
22196 the argument passed to the call, or the type of the value
22197 initialized with the result of the conversion function.
22198 ARG_EXPR is the original argument expression, which may be null. */
22199
22200 static int
22201 maybe_adjust_types_for_deduction (tree tparms,
22202 unification_kind_t strict,
22203 tree* parm,
22204 tree* arg,
22205 tree arg_expr)
22206 {
22207 int result = 0;
22208
22209 switch (strict)
22210 {
22211 case DEDUCE_CALL:
22212 break;
22213
22214 case DEDUCE_CONV:
22215 /* Swap PARM and ARG throughout the remainder of this
22216 function; the handling is precisely symmetric since PARM
22217 will initialize ARG rather than vice versa. */
22218 std::swap (parm, arg);
22219 break;
22220
22221 case DEDUCE_EXACT:
22222 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22223 too, but here handle it by stripping the reference from PARM
22224 rather than by adding it to ARG. */
22225 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22226 && TYPE_REF_P (*arg)
22227 && !TYPE_REF_IS_RVALUE (*arg))
22228 *parm = TREE_TYPE (*parm);
22229 /* Nothing else to do in this case. */
22230 return 0;
22231
22232 default:
22233 gcc_unreachable ();
22234 }
22235
22236 if (!TYPE_REF_P (*parm))
22237 {
22238 /* [temp.deduct.call]
22239
22240 If P is not a reference type:
22241
22242 --If A is an array type, the pointer type produced by the
22243 array-to-pointer standard conversion (_conv.array_) is
22244 used in place of A for type deduction; otherwise,
22245
22246 --If A is a function type, the pointer type produced by
22247 the function-to-pointer standard conversion
22248 (_conv.func_) is used in place of A for type deduction;
22249 otherwise,
22250
22251 --If A is a cv-qualified type, the top level
22252 cv-qualifiers of A's type are ignored for type
22253 deduction. */
22254 if (TREE_CODE (*arg) == ARRAY_TYPE)
22255 *arg = build_pointer_type (TREE_TYPE (*arg));
22256 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
22257 *arg = build_pointer_type (*arg);
22258 else
22259 *arg = TYPE_MAIN_VARIANT (*arg);
22260 }
22261
22262 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22263 an lvalue, the type 'lvalue reference to A' is used in place of A for
22264 type deduction." */
22265 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22266 && (arg_expr ? lvalue_p (arg_expr)
22267 /* try_one_overload doesn't provide an arg_expr, but
22268 functions are always lvalues. */
22269 : TREE_CODE (*arg) == FUNCTION_TYPE))
22270 *arg = build_reference_type (*arg);
22271
22272 /* [temp.deduct.call]
22273
22274 If P is a cv-qualified type, the top level cv-qualifiers
22275 of P's type are ignored for type deduction. If P is a
22276 reference type, the type referred to by P is used for
22277 type deduction. */
22278 *parm = TYPE_MAIN_VARIANT (*parm);
22279 if (TYPE_REF_P (*parm))
22280 {
22281 *parm = TREE_TYPE (*parm);
22282 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22283 }
22284
22285 /* DR 322. For conversion deduction, remove a reference type on parm
22286 too (which has been swapped into ARG). */
22287 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
22288 *arg = TREE_TYPE (*arg);
22289
22290 return result;
22291 }
22292
22293 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22294 template which doesn't contain any deducible template parameters; check if
22295 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22296 unify_one_argument. */
22297
22298 static int
22299 check_non_deducible_conversion (tree parm, tree arg, int strict,
22300 int flags, struct conversion **conv_p,
22301 bool explain_p)
22302 {
22303 tree type;
22304
22305 if (!TYPE_P (arg))
22306 type = TREE_TYPE (arg);
22307 else
22308 type = arg;
22309
22310 if (same_type_p (parm, type))
22311 return unify_success (explain_p);
22312
22313 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22314 if (strict == DEDUCE_CONV)
22315 {
22316 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
22317 return unify_success (explain_p);
22318 }
22319 else if (strict != DEDUCE_EXACT)
22320 {
22321 bool ok = false;
22322 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
22323 if (conv_p)
22324 /* Avoid recalculating this in add_function_candidate. */
22325 ok = (*conv_p
22326 = good_conversion (parm, type, conv_arg, flags, complain));
22327 else
22328 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
22329 if (ok)
22330 return unify_success (explain_p);
22331 }
22332
22333 if (strict == DEDUCE_EXACT)
22334 return unify_type_mismatch (explain_p, parm, arg);
22335 else
22336 return unify_arg_conversion (explain_p, parm, type, arg);
22337 }
22338
22339 static bool uses_deducible_template_parms (tree type);
22340
22341 /* Returns true iff the expression EXPR is one from which a template
22342 argument can be deduced. In other words, if it's an undecorated
22343 use of a template non-type parameter. */
22344
22345 static bool
22346 deducible_expression (tree expr)
22347 {
22348 /* Strip implicit conversions and implicit INDIRECT_REFs. */
22349 while (CONVERT_EXPR_P (expr)
22350 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
22351 || REFERENCE_REF_P (expr))
22352 expr = TREE_OPERAND (expr, 0);
22353 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
22354 }
22355
22356 /* Returns true iff the array domain DOMAIN uses a template parameter in a
22357 deducible way; that is, if it has a max value of <PARM> - 1. */
22358
22359 static bool
22360 deducible_array_bound (tree domain)
22361 {
22362 if (domain == NULL_TREE)
22363 return false;
22364
22365 tree max = TYPE_MAX_VALUE (domain);
22366 if (TREE_CODE (max) != MINUS_EXPR)
22367 return false;
22368
22369 return deducible_expression (TREE_OPERAND (max, 0));
22370 }
22371
22372 /* Returns true iff the template arguments ARGS use a template parameter
22373 in a deducible way. */
22374
22375 static bool
22376 deducible_template_args (tree args)
22377 {
22378 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
22379 {
22380 bool deducible;
22381 tree elt = TREE_VEC_ELT (args, i);
22382 if (ARGUMENT_PACK_P (elt))
22383 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
22384 else
22385 {
22386 if (PACK_EXPANSION_P (elt))
22387 elt = PACK_EXPANSION_PATTERN (elt);
22388 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
22389 deducible = true;
22390 else if (TYPE_P (elt))
22391 deducible = uses_deducible_template_parms (elt);
22392 else
22393 deducible = deducible_expression (elt);
22394 }
22395 if (deducible)
22396 return true;
22397 }
22398 return false;
22399 }
22400
22401 /* Returns true iff TYPE contains any deducible references to template
22402 parameters, as per 14.8.2.5. */
22403
22404 static bool
22405 uses_deducible_template_parms (tree type)
22406 {
22407 if (PACK_EXPANSION_P (type))
22408 type = PACK_EXPANSION_PATTERN (type);
22409
22410 /* T
22411 cv-list T
22412 TT<T>
22413 TT<i>
22414 TT<> */
22415 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22416 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22417 return true;
22418
22419 /* T*
22420 T&
22421 T&& */
22422 if (INDIRECT_TYPE_P (type))
22423 return uses_deducible_template_parms (TREE_TYPE (type));
22424
22425 /* T[integer-constant ]
22426 type [i] */
22427 if (TREE_CODE (type) == ARRAY_TYPE)
22428 return (uses_deducible_template_parms (TREE_TYPE (type))
22429 || deducible_array_bound (TYPE_DOMAIN (type)));
22430
22431 /* T type ::*
22432 type T::*
22433 T T::*
22434 T (type ::*)()
22435 type (T::*)()
22436 type (type ::*)(T)
22437 type (T::*)(T)
22438 T (type ::*)(T)
22439 T (T::*)()
22440 T (T::*)(T) */
22441 if (TYPE_PTRMEM_P (type))
22442 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
22443 || (uses_deducible_template_parms
22444 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
22445
22446 /* template-name <T> (where template-name refers to a class template)
22447 template-name <i> (where template-name refers to a class template) */
22448 if (CLASS_TYPE_P (type)
22449 && CLASSTYPE_TEMPLATE_INFO (type)
22450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
22451 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22452 (CLASSTYPE_TI_ARGS (type)));
22453
22454 /* type (T)
22455 T()
22456 T(T) */
22457 if (FUNC_OR_METHOD_TYPE_P (type))
22458 {
22459 if (uses_deducible_template_parms (TREE_TYPE (type)))
22460 return true;
22461 tree parm = TYPE_ARG_TYPES (type);
22462 if (TREE_CODE (type) == METHOD_TYPE)
22463 parm = TREE_CHAIN (parm);
22464 for (; parm; parm = TREE_CHAIN (parm))
22465 if (uses_deducible_template_parms (TREE_VALUE (parm)))
22466 return true;
22467 if (flag_noexcept_type
22468 && TYPE_RAISES_EXCEPTIONS (type)
22469 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
22470 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
22471 return true;
22472 }
22473
22474 return false;
22475 }
22476
22477 /* Subroutine of type_unification_real and unify_pack_expansion to
22478 handle unification of a single P/A pair. Parameters are as
22479 for those functions. */
22480
22481 static int
22482 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
22483 int subr, unification_kind_t strict,
22484 bool explain_p)
22485 {
22486 tree arg_expr = NULL_TREE;
22487 int arg_strict;
22488
22489 if (arg == error_mark_node || parm == error_mark_node)
22490 return unify_invalid (explain_p);
22491 if (arg == unknown_type_node)
22492 /* We can't deduce anything from this, but we might get all the
22493 template args from other function args. */
22494 return unify_success (explain_p);
22495
22496 /* Implicit conversions (Clause 4) will be performed on a function
22497 argument to convert it to the type of the corresponding function
22498 parameter if the parameter type contains no template-parameters that
22499 participate in template argument deduction. */
22500 if (strict != DEDUCE_EXACT
22501 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
22502 /* For function parameters with no deducible template parameters,
22503 just return. We'll check non-dependent conversions later. */
22504 return unify_success (explain_p);
22505
22506 switch (strict)
22507 {
22508 case DEDUCE_CALL:
22509 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
22510 | UNIFY_ALLOW_MORE_CV_QUAL
22511 | UNIFY_ALLOW_DERIVED);
22512 break;
22513
22514 case DEDUCE_CONV:
22515 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
22516 break;
22517
22518 case DEDUCE_EXACT:
22519 arg_strict = UNIFY_ALLOW_NONE;
22520 break;
22521
22522 default:
22523 gcc_unreachable ();
22524 }
22525
22526 /* We only do these transformations if this is the top-level
22527 parameter_type_list in a call or declaration matching; in other
22528 situations (nested function declarators, template argument lists) we
22529 won't be comparing a type to an expression, and we don't do any type
22530 adjustments. */
22531 if (!subr)
22532 {
22533 if (!TYPE_P (arg))
22534 {
22535 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
22536 if (type_unknown_p (arg))
22537 {
22538 /* [temp.deduct.type] A template-argument can be
22539 deduced from a pointer to function or pointer
22540 to member function argument if the set of
22541 overloaded functions does not contain function
22542 templates and at most one of a set of
22543 overloaded functions provides a unique
22544 match. */
22545 resolve_overloaded_unification (tparms, targs, parm,
22546 arg, strict,
22547 arg_strict, explain_p);
22548 /* If a unique match was not found, this is a
22549 non-deduced context, so we still succeed. */
22550 return unify_success (explain_p);
22551 }
22552
22553 arg_expr = arg;
22554 arg = unlowered_expr_type (arg);
22555 if (arg == error_mark_node)
22556 return unify_invalid (explain_p);
22557 }
22558
22559 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
22560 &parm, &arg, arg_expr);
22561 }
22562 else
22563 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
22564 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
22565 return unify_template_argument_mismatch (explain_p, parm, arg);
22566
22567 /* For deduction from an init-list we need the actual list. */
22568 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
22569 arg = arg_expr;
22570 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
22571 }
22572
22573 /* for_each_template_parm callback that always returns 0. */
22574
22575 static int
22576 zero_r (tree, void *)
22577 {
22578 return 0;
22579 }
22580
22581 /* for_each_template_parm any_fn callback to handle deduction of a template
22582 type argument from the type of an array bound. */
22583
22584 static int
22585 array_deduction_r (tree t, void *data)
22586 {
22587 tree_pair_p d = (tree_pair_p)data;
22588 tree &tparms = d->purpose;
22589 tree &targs = d->value;
22590
22591 if (TREE_CODE (t) == ARRAY_TYPE)
22592 if (tree dom = TYPE_DOMAIN (t))
22593 if (tree max = TYPE_MAX_VALUE (dom))
22594 {
22595 if (TREE_CODE (max) == MINUS_EXPR)
22596 max = TREE_OPERAND (max, 0);
22597 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
22598 unify (tparms, targs, TREE_TYPE (max), size_type_node,
22599 UNIFY_ALLOW_NONE, /*explain*/false);
22600 }
22601
22602 /* Keep walking. */
22603 return 0;
22604 }
22605
22606 /* Try to deduce any not-yet-deduced template type arguments from the type of
22607 an array bound. This is handled separately from unify because 14.8.2.5 says
22608 "The type of a type parameter is only deduced from an array bound if it is
22609 not otherwise deduced." */
22610
22611 static void
22612 try_array_deduction (tree tparms, tree targs, tree parm)
22613 {
22614 tree_pair_s data = { tparms, targs };
22615 hash_set<tree> visited;
22616 for_each_template_parm (parm, zero_r, &data, &visited,
22617 /*nondeduced*/false, array_deduction_r);
22618 }
22619
22620 /* Most parms like fn_type_unification.
22621
22622 If SUBR is 1, we're being called recursively (to unify the
22623 arguments of a function or method parameter of a function
22624 template).
22625
22626 CHECKS is a pointer to a vector of access checks encountered while
22627 substituting default template arguments. */
22628
22629 static int
22630 type_unification_real (tree tparms,
22631 tree full_targs,
22632 tree xparms,
22633 const tree *xargs,
22634 unsigned int xnargs,
22635 int subr,
22636 unification_kind_t strict,
22637 vec<deferred_access_check, va_gc> **checks,
22638 bool explain_p)
22639 {
22640 tree parm, arg;
22641 int i;
22642 int ntparms = TREE_VEC_LENGTH (tparms);
22643 int saw_undeduced = 0;
22644 tree parms;
22645 const tree *args;
22646 unsigned int nargs;
22647 unsigned int ia;
22648
22649 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22650 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22651 gcc_assert (ntparms > 0);
22652
22653 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22654
22655 /* Reset the number of non-defaulted template arguments contained
22656 in TARGS. */
22657 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22658
22659 again:
22660 parms = xparms;
22661 args = xargs;
22662 nargs = xnargs;
22663
22664 /* Only fn_type_unification cares about terminal void. */
22665 if (nargs && args[nargs-1] == void_type_node)
22666 --nargs;
22667
22668 ia = 0;
22669 while (parms && parms != void_list_node
22670 && ia < nargs)
22671 {
22672 parm = TREE_VALUE (parms);
22673
22674 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22675 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22676 /* For a function parameter pack that occurs at the end of the
22677 parameter-declaration-list, the type A of each remaining
22678 argument of the call is compared with the type P of the
22679 declarator-id of the function parameter pack. */
22680 break;
22681
22682 parms = TREE_CHAIN (parms);
22683
22684 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22685 /* For a function parameter pack that does not occur at the
22686 end of the parameter-declaration-list, the type of the
22687 parameter pack is a non-deduced context. */
22688 continue;
22689
22690 arg = args[ia];
22691 ++ia;
22692
22693 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22694 explain_p))
22695 return 1;
22696 }
22697
22698 if (parms
22699 && parms != void_list_node
22700 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22701 {
22702 /* Unify the remaining arguments with the pack expansion type. */
22703 tree argvec;
22704 tree parmvec = make_tree_vec (1);
22705
22706 /* Allocate a TREE_VEC and copy in all of the arguments */
22707 argvec = make_tree_vec (nargs - ia);
22708 for (i = 0; ia < nargs; ++ia, ++i)
22709 TREE_VEC_ELT (argvec, i) = args[ia];
22710
22711 /* Copy the parameter into parmvec. */
22712 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22713 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22714 /*subr=*/subr, explain_p))
22715 return 1;
22716
22717 /* Advance to the end of the list of parameters. */
22718 parms = TREE_CHAIN (parms);
22719 }
22720
22721 /* Fail if we've reached the end of the parm list, and more args
22722 are present, and the parm list isn't variadic. */
22723 if (ia < nargs && parms == void_list_node)
22724 return unify_too_many_arguments (explain_p, nargs, ia);
22725 /* Fail if parms are left and they don't have default values and
22726 they aren't all deduced as empty packs (c++/57397). This is
22727 consistent with sufficient_parms_p. */
22728 if (parms && parms != void_list_node
22729 && TREE_PURPOSE (parms) == NULL_TREE)
22730 {
22731 unsigned int count = nargs;
22732 tree p = parms;
22733 bool type_pack_p;
22734 do
22735 {
22736 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22737 if (!type_pack_p)
22738 count++;
22739 p = TREE_CHAIN (p);
22740 }
22741 while (p && p != void_list_node);
22742 if (count != nargs)
22743 return unify_too_few_arguments (explain_p, ia, count,
22744 type_pack_p);
22745 }
22746
22747 if (!subr)
22748 {
22749 tsubst_flags_t complain = (explain_p
22750 ? tf_warning_or_error
22751 : tf_none);
22752 bool tried_array_deduction = (cxx_dialect < cxx17);
22753
22754 for (i = 0; i < ntparms; i++)
22755 {
22756 tree targ = TREE_VEC_ELT (targs, i);
22757 tree tparm = TREE_VEC_ELT (tparms, i);
22758
22759 /* Clear the "incomplete" flags on all argument packs now so that
22760 substituting them into later default arguments works. */
22761 if (targ && ARGUMENT_PACK_P (targ))
22762 {
22763 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22764 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22765 }
22766
22767 if (targ || tparm == error_mark_node)
22768 continue;
22769 tparm = TREE_VALUE (tparm);
22770
22771 if (TREE_CODE (tparm) == TYPE_DECL
22772 && !tried_array_deduction)
22773 {
22774 try_array_deduction (tparms, targs, xparms);
22775 tried_array_deduction = true;
22776 if (TREE_VEC_ELT (targs, i))
22777 continue;
22778 }
22779
22780 /* If this is an undeduced nontype parameter that depends on
22781 a type parameter, try another pass; its type may have been
22782 deduced from a later argument than the one from which
22783 this parameter can be deduced. */
22784 if (TREE_CODE (tparm) == PARM_DECL
22785 && uses_template_parms (TREE_TYPE (tparm))
22786 && saw_undeduced < 2)
22787 {
22788 saw_undeduced = 1;
22789 continue;
22790 }
22791
22792 /* Core issue #226 (C++0x) [temp.deduct]:
22793
22794 If a template argument has not been deduced, its
22795 default template argument, if any, is used.
22796
22797 When we are in C++98 mode, TREE_PURPOSE will either
22798 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22799 to explicitly check cxx_dialect here. */
22800 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22801 /* OK, there is a default argument. Wait until after the
22802 conversion check to do substitution. */
22803 continue;
22804
22805 /* If the type parameter is a parameter pack, then it will
22806 be deduced to an empty parameter pack. */
22807 if (template_parameter_pack_p (tparm))
22808 {
22809 tree arg;
22810
22811 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22812 {
22813 arg = make_node (NONTYPE_ARGUMENT_PACK);
22814 TREE_CONSTANT (arg) = 1;
22815 }
22816 else
22817 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22818
22819 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22820
22821 TREE_VEC_ELT (targs, i) = arg;
22822 continue;
22823 }
22824
22825 return unify_parameter_deduction_failure (explain_p, tparm);
22826 }
22827
22828 /* Now substitute into the default template arguments. */
22829 for (i = 0; i < ntparms; i++)
22830 {
22831 tree targ = TREE_VEC_ELT (targs, i);
22832 tree tparm = TREE_VEC_ELT (tparms, i);
22833
22834 if (targ || tparm == error_mark_node)
22835 continue;
22836 tree parm = TREE_VALUE (tparm);
22837 tree arg = TREE_PURPOSE (tparm);
22838 reopen_deferring_access_checks (*checks);
22839 location_t save_loc = input_location;
22840 if (DECL_P (parm))
22841 input_location = DECL_SOURCE_LOCATION (parm);
22842
22843 if (saw_undeduced == 1
22844 && TREE_CODE (parm) == PARM_DECL
22845 && uses_template_parms (TREE_TYPE (parm)))
22846 {
22847 /* The type of this non-type parameter depends on undeduced
22848 parameters. Don't try to use its default argument yet,
22849 since we might deduce an argument for it on the next pass,
22850 but do check whether the arguments we already have cause
22851 substitution failure, so that that happens before we try
22852 later default arguments (78489). */
22853 ++processing_template_decl;
22854 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22855 NULL_TREE);
22856 --processing_template_decl;
22857 if (type == error_mark_node)
22858 arg = error_mark_node;
22859 else
22860 arg = NULL_TREE;
22861 }
22862 else
22863 {
22864 /* Even if the call is happening in template context, getting
22865 here means it's non-dependent, and a default argument is
22866 considered a separate definition under [temp.decls], so we can
22867 do this substitution without processing_template_decl. This
22868 is important if the default argument contains something that
22869 might be instantiation-dependent like access (87480). */
22870 processing_template_decl_sentinel s;
22871 tree substed = NULL_TREE;
22872 if (saw_undeduced == 1)
22873 {
22874 /* First instatiate in template context, in case we still
22875 depend on undeduced template parameters. */
22876 ++processing_template_decl;
22877 substed = tsubst_template_arg (arg, full_targs, complain,
22878 NULL_TREE);
22879 --processing_template_decl;
22880 if (substed != error_mark_node
22881 && !uses_template_parms (substed))
22882 /* We replaced all the tparms, substitute again out of
22883 template context. */
22884 substed = NULL_TREE;
22885 }
22886 if (!substed)
22887 substed = tsubst_template_arg (arg, full_targs, complain,
22888 NULL_TREE);
22889
22890 if (!uses_template_parms (substed))
22891 arg = convert_template_argument (parm, substed, full_targs,
22892 complain, i, NULL_TREE);
22893 else if (saw_undeduced == 1)
22894 arg = NULL_TREE;
22895 else
22896 arg = error_mark_node;
22897 }
22898
22899 input_location = save_loc;
22900 *checks = get_deferred_access_checks ();
22901 pop_deferring_access_checks ();
22902
22903 if (arg == error_mark_node)
22904 return 1;
22905 else if (arg)
22906 {
22907 TREE_VEC_ELT (targs, i) = arg;
22908 /* The position of the first default template argument,
22909 is also the number of non-defaulted arguments in TARGS.
22910 Record that. */
22911 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22912 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22913 }
22914 }
22915
22916 if (saw_undeduced++ == 1)
22917 goto again;
22918 }
22919
22920 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22921 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22922
22923 return unify_success (explain_p);
22924 }
22925
22926 /* Subroutine of type_unification_real. Args are like the variables
22927 at the call site. ARG is an overloaded function (or template-id);
22928 we try deducing template args from each of the overloads, and if
22929 only one succeeds, we go with that. Modifies TARGS and returns
22930 true on success. */
22931
22932 static bool
22933 resolve_overloaded_unification (tree tparms,
22934 tree targs,
22935 tree parm,
22936 tree arg,
22937 unification_kind_t strict,
22938 int sub_strict,
22939 bool explain_p)
22940 {
22941 tree tempargs = copy_node (targs);
22942 int good = 0;
22943 tree goodfn = NULL_TREE;
22944 bool addr_p;
22945
22946 if (TREE_CODE (arg) == ADDR_EXPR)
22947 {
22948 arg = TREE_OPERAND (arg, 0);
22949 addr_p = true;
22950 }
22951 else
22952 addr_p = false;
22953
22954 if (TREE_CODE (arg) == COMPONENT_REF)
22955 /* Handle `&x' where `x' is some static or non-static member
22956 function name. */
22957 arg = TREE_OPERAND (arg, 1);
22958
22959 if (TREE_CODE (arg) == OFFSET_REF)
22960 arg = TREE_OPERAND (arg, 1);
22961
22962 /* Strip baselink information. */
22963 if (BASELINK_P (arg))
22964 arg = BASELINK_FUNCTIONS (arg);
22965
22966 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22967 {
22968 /* If we got some explicit template args, we need to plug them into
22969 the affected templates before we try to unify, in case the
22970 explicit args will completely resolve the templates in question. */
22971
22972 int ok = 0;
22973 tree expl_subargs = TREE_OPERAND (arg, 1);
22974 arg = TREE_OPERAND (arg, 0);
22975
22976 for (lkp_iterator iter (arg); iter; ++iter)
22977 {
22978 tree fn = *iter;
22979 tree subargs, elem;
22980
22981 if (TREE_CODE (fn) != TEMPLATE_DECL)
22982 continue;
22983
22984 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22985 expl_subargs, NULL_TREE, tf_none,
22986 /*require_all_args=*/true,
22987 /*use_default_args=*/true);
22988 if (subargs != error_mark_node
22989 && !any_dependent_template_arguments_p (subargs))
22990 {
22991 fn = instantiate_template (fn, subargs, tf_none);
22992 if (!constraints_satisfied_p (fn))
22993 continue;
22994 if (undeduced_auto_decl (fn))
22995 {
22996 /* Instantiate the function to deduce its return type. */
22997 ++function_depth;
22998 instantiate_decl (fn, /*defer*/false, /*class*/false);
22999 --function_depth;
23000 }
23001
23002 if (flag_noexcept_type)
23003 maybe_instantiate_noexcept (fn, tf_none);
23004
23005 elem = TREE_TYPE (fn);
23006 if (try_one_overload (tparms, targs, tempargs, parm,
23007 elem, strict, sub_strict, addr_p, explain_p)
23008 && (!goodfn || !same_type_p (goodfn, elem)))
23009 {
23010 goodfn = elem;
23011 ++good;
23012 }
23013 }
23014 else if (subargs)
23015 ++ok;
23016 }
23017 /* If no templates (or more than one) are fully resolved by the
23018 explicit arguments, this template-id is a non-deduced context; it
23019 could still be OK if we deduce all template arguments for the
23020 enclosing call through other arguments. */
23021 if (good != 1)
23022 good = ok;
23023 }
23024 else if (!OVL_P (arg))
23025 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23026 -- but the deduction does not succeed because the expression is
23027 not just the function on its own. */
23028 return false;
23029 else
23030 for (lkp_iterator iter (arg); iter; ++iter)
23031 {
23032 tree fn = *iter;
23033 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
23034 strict, sub_strict, addr_p, explain_p)
23035 && (!goodfn || !decls_match (goodfn, fn)))
23036 {
23037 goodfn = fn;
23038 ++good;
23039 }
23040 }
23041
23042 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23043 to function or pointer to member function argument if the set of
23044 overloaded functions does not contain function templates and at most
23045 one of a set of overloaded functions provides a unique match.
23046
23047 So if we found multiple possibilities, we return success but don't
23048 deduce anything. */
23049
23050 if (good == 1)
23051 {
23052 int i = TREE_VEC_LENGTH (targs);
23053 for (; i--; )
23054 if (TREE_VEC_ELT (tempargs, i))
23055 {
23056 tree old = TREE_VEC_ELT (targs, i);
23057 tree new_ = TREE_VEC_ELT (tempargs, i);
23058 if (new_ && old && ARGUMENT_PACK_P (old)
23059 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
23060 /* Don't forget explicit template arguments in a pack. */
23061 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
23062 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
23063 TREE_VEC_ELT (targs, i) = new_;
23064 }
23065 }
23066 if (good)
23067 return true;
23068
23069 return false;
23070 }
23071
23072 /* Core DR 115: In contexts where deduction is done and fails, or in
23073 contexts where deduction is not done, if a template argument list is
23074 specified and it, along with any default template arguments, identifies
23075 a single function template specialization, then the template-id is an
23076 lvalue for the function template specialization. */
23077
23078 tree
23079 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
23080 {
23081 tree expr, offset, baselink;
23082 bool addr;
23083
23084 if (!type_unknown_p (orig_expr))
23085 return orig_expr;
23086
23087 expr = orig_expr;
23088 addr = false;
23089 offset = NULL_TREE;
23090 baselink = NULL_TREE;
23091
23092 if (TREE_CODE (expr) == ADDR_EXPR)
23093 {
23094 expr = TREE_OPERAND (expr, 0);
23095 addr = true;
23096 }
23097 if (TREE_CODE (expr) == OFFSET_REF)
23098 {
23099 offset = expr;
23100 expr = TREE_OPERAND (expr, 1);
23101 }
23102 if (BASELINK_P (expr))
23103 {
23104 baselink = expr;
23105 expr = BASELINK_FUNCTIONS (expr);
23106 }
23107
23108 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
23109 {
23110 int good = 0;
23111 tree goodfn = NULL_TREE;
23112
23113 /* If we got some explicit template args, we need to plug them into
23114 the affected templates before we try to unify, in case the
23115 explicit args will completely resolve the templates in question. */
23116
23117 tree expl_subargs = TREE_OPERAND (expr, 1);
23118 tree arg = TREE_OPERAND (expr, 0);
23119 tree badfn = NULL_TREE;
23120 tree badargs = NULL_TREE;
23121
23122 for (lkp_iterator iter (arg); iter; ++iter)
23123 {
23124 tree fn = *iter;
23125 tree subargs, elem;
23126
23127 if (TREE_CODE (fn) != TEMPLATE_DECL)
23128 continue;
23129
23130 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23131 expl_subargs, NULL_TREE, tf_none,
23132 /*require_all_args=*/true,
23133 /*use_default_args=*/true);
23134 if (subargs != error_mark_node
23135 && !any_dependent_template_arguments_p (subargs))
23136 {
23137 elem = instantiate_template (fn, subargs, tf_none);
23138 if (elem == error_mark_node)
23139 {
23140 badfn = fn;
23141 badargs = subargs;
23142 }
23143 else if (elem && (!goodfn || !decls_match (goodfn, elem))
23144 && constraints_satisfied_p (elem))
23145 {
23146 goodfn = elem;
23147 ++good;
23148 }
23149 }
23150 }
23151 if (good == 1)
23152 {
23153 mark_used (goodfn);
23154 expr = goodfn;
23155 if (baselink)
23156 expr = build_baselink (BASELINK_BINFO (baselink),
23157 BASELINK_ACCESS_BINFO (baselink),
23158 expr, BASELINK_OPTYPE (baselink));
23159 if (offset)
23160 {
23161 tree base
23162 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
23163 expr = build_offset_ref (base, expr, addr, complain);
23164 }
23165 if (addr)
23166 expr = cp_build_addr_expr (expr, complain);
23167 return expr;
23168 }
23169 else if (good == 0 && badargs && (complain & tf_error))
23170 /* There were no good options and at least one bad one, so let the
23171 user know what the problem is. */
23172 instantiate_template (badfn, badargs, complain);
23173 }
23174 return orig_expr;
23175 }
23176
23177 /* As above, but error out if the expression remains overloaded. */
23178
23179 tree
23180 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
23181 {
23182 exp = resolve_nondeduced_context (exp, complain);
23183 if (type_unknown_p (exp))
23184 {
23185 if (complain & tf_error)
23186 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
23187 return error_mark_node;
23188 }
23189 return exp;
23190 }
23191
23192 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23193 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23194 different overloads deduce different arguments for a given parm.
23195 ADDR_P is true if the expression for which deduction is being
23196 performed was of the form "& fn" rather than simply "fn".
23197
23198 Returns 1 on success. */
23199
23200 static int
23201 try_one_overload (tree tparms,
23202 tree orig_targs,
23203 tree targs,
23204 tree parm,
23205 tree arg,
23206 unification_kind_t strict,
23207 int sub_strict,
23208 bool addr_p,
23209 bool explain_p)
23210 {
23211 int nargs;
23212 tree tempargs;
23213 int i;
23214
23215 if (arg == error_mark_node)
23216 return 0;
23217
23218 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23219 to function or pointer to member function argument if the set of
23220 overloaded functions does not contain function templates and at most
23221 one of a set of overloaded functions provides a unique match.
23222
23223 So if this is a template, just return success. */
23224
23225 if (uses_template_parms (arg))
23226 return 1;
23227
23228 if (TREE_CODE (arg) == METHOD_TYPE)
23229 arg = build_ptrmemfunc_type (build_pointer_type (arg));
23230 else if (addr_p)
23231 arg = build_pointer_type (arg);
23232
23233 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23234 &parm, &arg, NULL_TREE);
23235
23236 /* We don't copy orig_targs for this because if we have already deduced
23237 some template args from previous args, unify would complain when we
23238 try to deduce a template parameter for the same argument, even though
23239 there isn't really a conflict. */
23240 nargs = TREE_VEC_LENGTH (targs);
23241 tempargs = make_tree_vec (nargs);
23242
23243 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
23244 return 0;
23245
23246 /* First make sure we didn't deduce anything that conflicts with
23247 explicitly specified args. */
23248 for (i = nargs; i--; )
23249 {
23250 tree elt = TREE_VEC_ELT (tempargs, i);
23251 tree oldelt = TREE_VEC_ELT (orig_targs, i);
23252
23253 if (!elt)
23254 /*NOP*/;
23255 else if (uses_template_parms (elt))
23256 /* Since we're unifying against ourselves, we will fill in
23257 template args used in the function parm list with our own
23258 template parms. Discard them. */
23259 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
23260 else if (oldelt && ARGUMENT_PACK_P (oldelt))
23261 {
23262 /* Check that the argument at each index of the deduced argument pack
23263 is equivalent to the corresponding explicitly specified argument.
23264 We may have deduced more arguments than were explicitly specified,
23265 and that's OK. */
23266
23267 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23268 that's wrong if we deduce the same argument pack from multiple
23269 function arguments: it's only incomplete the first time. */
23270
23271 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
23272 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
23273
23274 if (TREE_VEC_LENGTH (deduced_pack)
23275 < TREE_VEC_LENGTH (explicit_pack))
23276 return 0;
23277
23278 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
23279 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
23280 TREE_VEC_ELT (deduced_pack, j)))
23281 return 0;
23282 }
23283 else if (oldelt && !template_args_equal (oldelt, elt))
23284 return 0;
23285 }
23286
23287 for (i = nargs; i--; )
23288 {
23289 tree elt = TREE_VEC_ELT (tempargs, i);
23290
23291 if (elt)
23292 TREE_VEC_ELT (targs, i) = elt;
23293 }
23294
23295 return 1;
23296 }
23297
23298 /* PARM is a template class (perhaps with unbound template
23299 parameters). ARG is a fully instantiated type. If ARG can be
23300 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23301 TARGS are as for unify. */
23302
23303 static tree
23304 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
23305 bool explain_p)
23306 {
23307 tree copy_of_targs;
23308
23309 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23310 return NULL_TREE;
23311 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23312 /* Matches anything. */;
23313 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
23314 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
23315 return NULL_TREE;
23316
23317 /* We need to make a new template argument vector for the call to
23318 unify. If we used TARGS, we'd clutter it up with the result of
23319 the attempted unification, even if this class didn't work out.
23320 We also don't want to commit ourselves to all the unifications
23321 we've already done, since unification is supposed to be done on
23322 an argument-by-argument basis. In other words, consider the
23323 following pathological case:
23324
23325 template <int I, int J, int K>
23326 struct S {};
23327
23328 template <int I, int J>
23329 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
23330
23331 template <int I, int J, int K>
23332 void f(S<I, J, K>, S<I, I, I>);
23333
23334 void g() {
23335 S<0, 0, 0> s0;
23336 S<0, 1, 2> s2;
23337
23338 f(s0, s2);
23339 }
23340
23341 Now, by the time we consider the unification involving `s2', we
23342 already know that we must have `f<0, 0, 0>'. But, even though
23343 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
23344 because there are two ways to unify base classes of S<0, 1, 2>
23345 with S<I, I, I>. If we kept the already deduced knowledge, we
23346 would reject the possibility I=1. */
23347 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
23348
23349 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23350 {
23351 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
23352 return NULL_TREE;
23353 return arg;
23354 }
23355
23356 /* If unification failed, we're done. */
23357 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
23358 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
23359 return NULL_TREE;
23360
23361 return arg;
23362 }
23363
23364 /* Given a template type PARM and a class type ARG, find the unique
23365 base type in ARG that is an instance of PARM. We do not examine
23366 ARG itself; only its base-classes. If there is not exactly one
23367 appropriate base class, return NULL_TREE. PARM may be the type of
23368 a partial specialization, as well as a plain template type. Used
23369 by unify. */
23370
23371 static enum template_base_result
23372 get_template_base (tree tparms, tree targs, tree parm, tree arg,
23373 bool explain_p, tree *result)
23374 {
23375 tree rval = NULL_TREE;
23376 tree binfo;
23377
23378 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
23379
23380 binfo = TYPE_BINFO (complete_type (arg));
23381 if (!binfo)
23382 {
23383 /* The type could not be completed. */
23384 *result = NULL_TREE;
23385 return tbr_incomplete_type;
23386 }
23387
23388 /* Walk in inheritance graph order. The search order is not
23389 important, and this avoids multiple walks of virtual bases. */
23390 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
23391 {
23392 tree r = try_class_unification (tparms, targs, parm,
23393 BINFO_TYPE (binfo), explain_p);
23394
23395 if (r)
23396 {
23397 /* If there is more than one satisfactory baseclass, then:
23398
23399 [temp.deduct.call]
23400
23401 If they yield more than one possible deduced A, the type
23402 deduction fails.
23403
23404 applies. */
23405 if (rval && !same_type_p (r, rval))
23406 {
23407 /* [temp.deduct.call]/4.3: If there is a class C that is a
23408 (direct or indirect) base class of D and derived (directly or
23409 indirectly) from a class B and that would be a valid deduced
23410 A, the deduced A cannot be B or pointer to B, respectively. */
23411 if (DERIVED_FROM_P (r, rval))
23412 /* Ignore r. */
23413 continue;
23414 else if (DERIVED_FROM_P (rval, r))
23415 /* Ignore rval. */;
23416 else
23417 {
23418 *result = NULL_TREE;
23419 return tbr_ambiguous_baseclass;
23420 }
23421 }
23422
23423 rval = r;
23424 }
23425 }
23426
23427 *result = rval;
23428 return tbr_success;
23429 }
23430
23431 /* Returns the level of DECL, which declares a template parameter. */
23432
23433 static int
23434 template_decl_level (tree decl)
23435 {
23436 switch (TREE_CODE (decl))
23437 {
23438 case TYPE_DECL:
23439 case TEMPLATE_DECL:
23440 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
23441
23442 case PARM_DECL:
23443 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
23444
23445 default:
23446 gcc_unreachable ();
23447 }
23448 return 0;
23449 }
23450
23451 /* Decide whether ARG can be unified with PARM, considering only the
23452 cv-qualifiers of each type, given STRICT as documented for unify.
23453 Returns nonzero iff the unification is OK on that basis. */
23454
23455 static int
23456 check_cv_quals_for_unify (int strict, tree arg, tree parm)
23457 {
23458 int arg_quals = cp_type_quals (arg);
23459 int parm_quals = cp_type_quals (parm);
23460
23461 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23462 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23463 {
23464 /* Although a CVR qualifier is ignored when being applied to a
23465 substituted template parameter ([8.3.2]/1 for example), that
23466 does not allow us to unify "const T" with "int&" because both
23467 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23468 It is ok when we're allowing additional CV qualifiers
23469 at the outer level [14.8.2.1]/3,1st bullet. */
23470 if ((TYPE_REF_P (arg)
23471 || FUNC_OR_METHOD_TYPE_P (arg))
23472 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
23473 return 0;
23474
23475 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
23476 && (parm_quals & TYPE_QUAL_RESTRICT))
23477 return 0;
23478 }
23479
23480 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23481 && (arg_quals & parm_quals) != parm_quals)
23482 return 0;
23483
23484 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
23485 && (parm_quals & arg_quals) != arg_quals)
23486 return 0;
23487
23488 return 1;
23489 }
23490
23491 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23492 void
23493 template_parm_level_and_index (tree parm, int* level, int* index)
23494 {
23495 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23496 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23497 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23498 {
23499 *index = TEMPLATE_TYPE_IDX (parm);
23500 *level = TEMPLATE_TYPE_LEVEL (parm);
23501 }
23502 else
23503 {
23504 *index = TEMPLATE_PARM_IDX (parm);
23505 *level = TEMPLATE_PARM_LEVEL (parm);
23506 }
23507 }
23508
23509 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23510 do { \
23511 if (unify (TP, TA, P, A, S, EP)) \
23512 return 1; \
23513 } while (0)
23514
23515 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23516 expansion at the end of PACKED_PARMS. Returns 0 if the type
23517 deduction succeeds, 1 otherwise. STRICT is the same as in
23518 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23519 function call argument list. We'll need to adjust the arguments to make them
23520 types. SUBR tells us if this is from a recursive call to
23521 type_unification_real, or for comparing two template argument
23522 lists. */
23523
23524 static int
23525 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
23526 tree packed_args, unification_kind_t strict,
23527 bool subr, bool explain_p)
23528 {
23529 tree parm
23530 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
23531 tree pattern = PACK_EXPANSION_PATTERN (parm);
23532 tree pack, packs = NULL_TREE;
23533 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
23534
23535 /* Add in any args remembered from an earlier partial instantiation. */
23536 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
23537 int levels = TMPL_ARGS_DEPTH (targs);
23538
23539 packed_args = expand_template_argument_pack (packed_args);
23540
23541 int len = TREE_VEC_LENGTH (packed_args);
23542
23543 /* Determine the parameter packs we will be deducing from the
23544 pattern, and record their current deductions. */
23545 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
23546 pack; pack = TREE_CHAIN (pack))
23547 {
23548 tree parm_pack = TREE_VALUE (pack);
23549 int idx, level;
23550
23551 /* Only template parameter packs can be deduced, not e.g. function
23552 parameter packs or __bases or __integer_pack. */
23553 if (!TEMPLATE_PARM_P (parm_pack))
23554 continue;
23555
23556 /* Determine the index and level of this parameter pack. */
23557 template_parm_level_and_index (parm_pack, &level, &idx);
23558 if (level < levels)
23559 continue;
23560
23561 /* Keep track of the parameter packs and their corresponding
23562 argument packs. */
23563 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
23564 TREE_TYPE (packs) = make_tree_vec (len - start);
23565 }
23566
23567 /* Loop through all of the arguments that have not yet been
23568 unified and unify each with the pattern. */
23569 for (i = start; i < len; i++)
23570 {
23571 tree parm;
23572 bool any_explicit = false;
23573 tree arg = TREE_VEC_ELT (packed_args, i);
23574
23575 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23576 or the element of its argument pack at the current index if
23577 this argument was explicitly specified. */
23578 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23579 {
23580 int idx, level;
23581 tree arg, pargs;
23582 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23583
23584 arg = NULL_TREE;
23585 if (TREE_VALUE (pack)
23586 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
23587 && (i - start < TREE_VEC_LENGTH (pargs)))
23588 {
23589 any_explicit = true;
23590 arg = TREE_VEC_ELT (pargs, i - start);
23591 }
23592 TMPL_ARG (targs, level, idx) = arg;
23593 }
23594
23595 /* If we had explicit template arguments, substitute them into the
23596 pattern before deduction. */
23597 if (any_explicit)
23598 {
23599 /* Some arguments might still be unspecified or dependent. */
23600 bool dependent;
23601 ++processing_template_decl;
23602 dependent = any_dependent_template_arguments_p (targs);
23603 if (!dependent)
23604 --processing_template_decl;
23605 parm = tsubst (pattern, targs,
23606 explain_p ? tf_warning_or_error : tf_none,
23607 NULL_TREE);
23608 if (dependent)
23609 --processing_template_decl;
23610 if (parm == error_mark_node)
23611 return 1;
23612 }
23613 else
23614 parm = pattern;
23615
23616 /* Unify the pattern with the current argument. */
23617 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23618 explain_p))
23619 return 1;
23620
23621 /* For each parameter pack, collect the deduced value. */
23622 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23623 {
23624 int idx, level;
23625 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23626
23627 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23628 TMPL_ARG (targs, level, idx);
23629 }
23630 }
23631
23632 /* Verify that the results of unification with the parameter packs
23633 produce results consistent with what we've seen before, and make
23634 the deduced argument packs available. */
23635 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23636 {
23637 tree old_pack = TREE_VALUE (pack);
23638 tree new_args = TREE_TYPE (pack);
23639 int i, len = TREE_VEC_LENGTH (new_args);
23640 int idx, level;
23641 bool nondeduced_p = false;
23642
23643 /* By default keep the original deduced argument pack.
23644 If necessary, more specific code is going to update the
23645 resulting deduced argument later down in this function. */
23646 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23647 TMPL_ARG (targs, level, idx) = old_pack;
23648
23649 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23650 actually deduce anything. */
23651 for (i = 0; i < len && !nondeduced_p; ++i)
23652 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23653 nondeduced_p = true;
23654 if (nondeduced_p)
23655 continue;
23656
23657 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23658 {
23659 /* If we had fewer function args than explicit template args,
23660 just use the explicits. */
23661 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23662 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23663 if (len < explicit_len)
23664 new_args = explicit_args;
23665 }
23666
23667 if (!old_pack)
23668 {
23669 tree result;
23670 /* Build the deduced *_ARGUMENT_PACK. */
23671 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23672 {
23673 result = make_node (NONTYPE_ARGUMENT_PACK);
23674 TREE_CONSTANT (result) = 1;
23675 }
23676 else
23677 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23678
23679 SET_ARGUMENT_PACK_ARGS (result, new_args);
23680
23681 /* Note the deduced argument packs for this parameter
23682 pack. */
23683 TMPL_ARG (targs, level, idx) = result;
23684 }
23685 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23686 && (ARGUMENT_PACK_ARGS (old_pack)
23687 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23688 {
23689 /* We only had the explicitly-provided arguments before, but
23690 now we have a complete set of arguments. */
23691 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23692
23693 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23694 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23695 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23696 }
23697 else
23698 {
23699 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23700 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23701 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23702 /* During template argument deduction for the aggregate deduction
23703 candidate, the number of elements in a trailing parameter pack
23704 is only deduced from the number of remaining function
23705 arguments if it is not otherwise deduced. */
23706 if (cxx_dialect >= cxx20
23707 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23708 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23709 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23710 if (!comp_template_args (old_args, new_args,
23711 &bad_old_arg, &bad_new_arg))
23712 /* Inconsistent unification of this parameter pack. */
23713 return unify_parameter_pack_inconsistent (explain_p,
23714 bad_old_arg,
23715 bad_new_arg);
23716 }
23717 }
23718
23719 return unify_success (explain_p);
23720 }
23721
23722 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23723 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23724 parameters and return value are as for unify. */
23725
23726 static int
23727 unify_array_domain (tree tparms, tree targs,
23728 tree parm_dom, tree arg_dom,
23729 bool explain_p)
23730 {
23731 tree parm_max;
23732 tree arg_max;
23733 bool parm_cst;
23734 bool arg_cst;
23735
23736 /* Our representation of array types uses "N - 1" as the
23737 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23738 not an integer constant. We cannot unify arbitrarily
23739 complex expressions, so we eliminate the MINUS_EXPRs
23740 here. */
23741 parm_max = TYPE_MAX_VALUE (parm_dom);
23742 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23743 if (!parm_cst)
23744 {
23745 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23746 parm_max = TREE_OPERAND (parm_max, 0);
23747 }
23748 arg_max = TYPE_MAX_VALUE (arg_dom);
23749 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23750 if (!arg_cst)
23751 {
23752 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23753 trying to unify the type of a variable with the type
23754 of a template parameter. For example:
23755
23756 template <unsigned int N>
23757 void f (char (&) [N]);
23758 int g();
23759 void h(int i) {
23760 char a[g(i)];
23761 f(a);
23762 }
23763
23764 Here, the type of the ARG will be "int [g(i)]", and
23765 may be a SAVE_EXPR, etc. */
23766 if (TREE_CODE (arg_max) != MINUS_EXPR)
23767 return unify_vla_arg (explain_p, arg_dom);
23768 arg_max = TREE_OPERAND (arg_max, 0);
23769 }
23770
23771 /* If only one of the bounds used a MINUS_EXPR, compensate
23772 by adding one to the other bound. */
23773 if (parm_cst && !arg_cst)
23774 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23775 integer_type_node,
23776 parm_max,
23777 integer_one_node);
23778 else if (arg_cst && !parm_cst)
23779 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23780 integer_type_node,
23781 arg_max,
23782 integer_one_node);
23783
23784 return unify (tparms, targs, parm_max, arg_max,
23785 UNIFY_ALLOW_INTEGER, explain_p);
23786 }
23787
23788 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23789
23790 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23791
23792 static pa_kind_t
23793 pa_kind (tree t)
23794 {
23795 if (PACK_EXPANSION_P (t))
23796 t = PACK_EXPANSION_PATTERN (t);
23797 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23798 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23799 || DECL_TYPE_TEMPLATE_P (t))
23800 return pa_tmpl;
23801 else if (TYPE_P (t))
23802 return pa_type;
23803 else
23804 return pa_expr;
23805 }
23806
23807 /* Deduce the value of template parameters. TPARMS is the (innermost)
23808 set of template parameters to a template. TARGS is the bindings
23809 for those template parameters, as determined thus far; TARGS may
23810 include template arguments for outer levels of template parameters
23811 as well. PARM is a parameter to a template function, or a
23812 subcomponent of that parameter; ARG is the corresponding argument.
23813 This function attempts to match PARM with ARG in a manner
23814 consistent with the existing assignments in TARGS. If more values
23815 are deduced, then TARGS is updated.
23816
23817 Returns 0 if the type deduction succeeds, 1 otherwise. The
23818 parameter STRICT is a bitwise or of the following flags:
23819
23820 UNIFY_ALLOW_NONE:
23821 Require an exact match between PARM and ARG.
23822 UNIFY_ALLOW_MORE_CV_QUAL:
23823 Allow the deduced ARG to be more cv-qualified (by qualification
23824 conversion) than ARG.
23825 UNIFY_ALLOW_LESS_CV_QUAL:
23826 Allow the deduced ARG to be less cv-qualified than ARG.
23827 UNIFY_ALLOW_DERIVED:
23828 Allow the deduced ARG to be a template base class of ARG,
23829 or a pointer to a template base class of the type pointed to by
23830 ARG.
23831 UNIFY_ALLOW_INTEGER:
23832 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23833 case for more information.
23834 UNIFY_ALLOW_OUTER_LEVEL:
23835 This is the outermost level of a deduction. Used to determine validity
23836 of qualification conversions. A valid qualification conversion must
23837 have const qualified pointers leading up to the inner type which
23838 requires additional CV quals, except at the outer level, where const
23839 is not required [conv.qual]. It would be normal to set this flag in
23840 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23841 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23842 This is the outermost level of a deduction, and PARM can be more CV
23843 qualified at this point.
23844 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23845 This is the outermost level of a deduction, and PARM can be less CV
23846 qualified at this point. */
23847
23848 static int
23849 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23850 bool explain_p)
23851 {
23852 int idx;
23853 tree targ;
23854 tree tparm;
23855 int strict_in = strict;
23856 tsubst_flags_t complain = (explain_p
23857 ? tf_warning_or_error
23858 : tf_none);
23859
23860 /* I don't think this will do the right thing with respect to types.
23861 But the only case I've seen it in so far has been array bounds, where
23862 signedness is the only information lost, and I think that will be
23863 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23864 finish_id_expression_1, and are also OK. */
23865 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23866 parm = TREE_OPERAND (parm, 0);
23867
23868 if (arg == error_mark_node)
23869 return unify_invalid (explain_p);
23870 if (arg == unknown_type_node
23871 || arg == init_list_type_node)
23872 /* We can't deduce anything from this, but we might get all the
23873 template args from other function args. */
23874 return unify_success (explain_p);
23875
23876 if (parm == any_targ_node || arg == any_targ_node)
23877 return unify_success (explain_p);
23878
23879 /* If PARM uses template parameters, then we can't bail out here,
23880 even if ARG == PARM, since we won't record unifications for the
23881 template parameters. We might need them if we're trying to
23882 figure out which of two things is more specialized. */
23883 if (arg == parm && !uses_template_parms (parm))
23884 return unify_success (explain_p);
23885
23886 /* Handle init lists early, so the rest of the function can assume
23887 we're dealing with a type. */
23888 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23889 {
23890 tree elttype;
23891 tree orig_parm = parm;
23892
23893 if (!is_std_init_list (parm)
23894 && TREE_CODE (parm) != ARRAY_TYPE)
23895 /* We can only deduce from an initializer list argument if the
23896 parameter is std::initializer_list or an array; otherwise this
23897 is a non-deduced context. */
23898 return unify_success (explain_p);
23899
23900 if (TREE_CODE (parm) == ARRAY_TYPE)
23901 elttype = TREE_TYPE (parm);
23902 else
23903 {
23904 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23905 /* Deduction is defined in terms of a single type, so just punt
23906 on the (bizarre) std::initializer_list<T...>. */
23907 if (PACK_EXPANSION_P (elttype))
23908 return unify_success (explain_p);
23909 }
23910
23911 if (strict != DEDUCE_EXACT
23912 && TYPE_P (elttype)
23913 && !uses_deducible_template_parms (elttype))
23914 /* If ELTTYPE has no deducible template parms, skip deduction from
23915 the list elements. */;
23916 else
23917 for (auto &e: CONSTRUCTOR_ELTS (arg))
23918 {
23919 tree elt = e.value;
23920 int elt_strict = strict;
23921
23922 if (elt == error_mark_node)
23923 return unify_invalid (explain_p);
23924
23925 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23926 {
23927 tree type = TREE_TYPE (elt);
23928 if (type == error_mark_node)
23929 return unify_invalid (explain_p);
23930 /* It should only be possible to get here for a call. */
23931 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23932 elt_strict |= maybe_adjust_types_for_deduction
23933 (tparms, DEDUCE_CALL, &elttype, &type, elt);
23934 elt = type;
23935 }
23936
23937 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23938 explain_p);
23939 }
23940
23941 if (TREE_CODE (parm) == ARRAY_TYPE
23942 && deducible_array_bound (TYPE_DOMAIN (parm)))
23943 {
23944 /* Also deduce from the length of the initializer list. */
23945 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23946 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23947 if (idx == error_mark_node)
23948 return unify_invalid (explain_p);
23949 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23950 idx, explain_p);
23951 }
23952
23953 /* If the std::initializer_list<T> deduction worked, replace the
23954 deduced A with std::initializer_list<A>. */
23955 if (orig_parm != parm)
23956 {
23957 idx = TEMPLATE_TYPE_IDX (orig_parm);
23958 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23959 targ = listify (targ);
23960 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23961 }
23962 return unify_success (explain_p);
23963 }
23964
23965 /* If parm and arg aren't the same kind of thing (template, type, or
23966 expression), fail early. */
23967 if (pa_kind (parm) != pa_kind (arg))
23968 return unify_invalid (explain_p);
23969
23970 /* Immediately reject some pairs that won't unify because of
23971 cv-qualification mismatches. */
23972 if (TREE_CODE (arg) == TREE_CODE (parm)
23973 && TYPE_P (arg)
23974 /* It is the elements of the array which hold the cv quals of an array
23975 type, and the elements might be template type parms. We'll check
23976 when we recurse. */
23977 && TREE_CODE (arg) != ARRAY_TYPE
23978 /* We check the cv-qualifiers when unifying with template type
23979 parameters below. We want to allow ARG `const T' to unify with
23980 PARM `T' for example, when computing which of two templates
23981 is more specialized, for example. */
23982 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23983 && !check_cv_quals_for_unify (strict_in, arg, parm))
23984 return unify_cv_qual_mismatch (explain_p, parm, arg);
23985
23986 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23987 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23988 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23989 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23990 strict &= ~UNIFY_ALLOW_DERIVED;
23991 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23992 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23993
23994 switch (TREE_CODE (parm))
23995 {
23996 case TYPENAME_TYPE:
23997 case SCOPE_REF:
23998 case UNBOUND_CLASS_TEMPLATE:
23999 /* In a type which contains a nested-name-specifier, template
24000 argument values cannot be deduced for template parameters used
24001 within the nested-name-specifier. */
24002 return unify_success (explain_p);
24003
24004 case TEMPLATE_TYPE_PARM:
24005 case TEMPLATE_TEMPLATE_PARM:
24006 case BOUND_TEMPLATE_TEMPLATE_PARM:
24007 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24008 if (error_operand_p (tparm))
24009 return unify_invalid (explain_p);
24010
24011 if (TEMPLATE_TYPE_LEVEL (parm)
24012 != template_decl_level (tparm))
24013 /* The PARM is not one we're trying to unify. Just check
24014 to see if it matches ARG. */
24015 {
24016 if (TREE_CODE (arg) == TREE_CODE (parm)
24017 && (is_auto (parm) ? is_auto (arg)
24018 : same_type_p (parm, arg)))
24019 return unify_success (explain_p);
24020 else
24021 return unify_type_mismatch (explain_p, parm, arg);
24022 }
24023 idx = TEMPLATE_TYPE_IDX (parm);
24024 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24025 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
24026 if (error_operand_p (tparm))
24027 return unify_invalid (explain_p);
24028
24029 /* Check for mixed types and values. */
24030 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24031 && TREE_CODE (tparm) != TYPE_DECL)
24032 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24033 && TREE_CODE (tparm) != TEMPLATE_DECL))
24034 gcc_unreachable ();
24035
24036 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24037 {
24038 if ((strict_in & UNIFY_ALLOW_DERIVED)
24039 && CLASS_TYPE_P (arg))
24040 {
24041 /* First try to match ARG directly. */
24042 tree t = try_class_unification (tparms, targs, parm, arg,
24043 explain_p);
24044 if (!t)
24045 {
24046 /* Otherwise, look for a suitable base of ARG, as below. */
24047 enum template_base_result r;
24048 r = get_template_base (tparms, targs, parm, arg,
24049 explain_p, &t);
24050 if (!t)
24051 return unify_no_common_base (explain_p, r, parm, arg);
24052 arg = t;
24053 }
24054 }
24055 /* ARG must be constructed from a template class or a template
24056 template parameter. */
24057 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
24058 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24059 return unify_template_deduction_failure (explain_p, parm, arg);
24060
24061 /* Deduce arguments T, i from TT<T> or TT<i>. */
24062 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
24063 return 1;
24064
24065 arg = TYPE_TI_TEMPLATE (arg);
24066 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
24067 /* If the template is a template template parameter, use the
24068 TEMPLATE_TEMPLATE_PARM for matching. */
24069 arg = TREE_TYPE (arg);
24070
24071 /* Fall through to deduce template name. */
24072 }
24073
24074 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24075 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24076 {
24077 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24078
24079 /* Simple cases: Value already set, does match or doesn't. */
24080 if (targ != NULL_TREE && template_args_equal (targ, arg))
24081 return unify_success (explain_p);
24082 else if (targ)
24083 return unify_inconsistency (explain_p, parm, targ, arg);
24084 }
24085 else
24086 {
24087 /* If PARM is `const T' and ARG is only `int', we don't have
24088 a match unless we are allowing additional qualification.
24089 If ARG is `const int' and PARM is just `T' that's OK;
24090 that binds `const int' to `T'. */
24091 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
24092 arg, parm))
24093 return unify_cv_qual_mismatch (explain_p, parm, arg);
24094
24095 /* Consider the case where ARG is `const volatile int' and
24096 PARM is `const T'. Then, T should be `volatile int'. */
24097 arg = cp_build_qualified_type_real
24098 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
24099 if (arg == error_mark_node)
24100 return unify_invalid (explain_p);
24101
24102 /* Simple cases: Value already set, does match or doesn't. */
24103 if (targ != NULL_TREE && same_type_p (targ, arg))
24104 return unify_success (explain_p);
24105 else if (targ)
24106 return unify_inconsistency (explain_p, parm, targ, arg);
24107
24108 /* Make sure that ARG is not a variable-sized array. (Note
24109 that were talking about variable-sized arrays (like
24110 `int[n]'), rather than arrays of unknown size (like
24111 `int[]').) We'll get very confused by such a type since
24112 the bound of the array is not constant, and therefore
24113 not mangleable. Besides, such types are not allowed in
24114 ISO C++, so we can do as we please here. We do allow
24115 them for 'auto' deduction, since that isn't ABI-exposed. */
24116 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
24117 return unify_vla_arg (explain_p, arg);
24118
24119 /* Strip typedefs as in convert_template_argument. */
24120 arg = canonicalize_type_argument (arg, tf_none);
24121 }
24122
24123 /* If ARG is a parameter pack or an expansion, we cannot unify
24124 against it unless PARM is also a parameter pack. */
24125 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
24126 && !template_parameter_pack_p (parm))
24127 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24128
24129 /* If the argument deduction results is a METHOD_TYPE,
24130 then there is a problem.
24131 METHOD_TYPE doesn't map to any real C++ type the result of
24132 the deduction cannot be of that type. */
24133 if (TREE_CODE (arg) == METHOD_TYPE)
24134 return unify_method_type_error (explain_p, arg);
24135
24136 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24137 return unify_success (explain_p);
24138
24139 case TEMPLATE_PARM_INDEX:
24140 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24141 if (error_operand_p (tparm))
24142 return unify_invalid (explain_p);
24143
24144 if (TEMPLATE_PARM_LEVEL (parm)
24145 != template_decl_level (tparm))
24146 {
24147 /* The PARM is not one we're trying to unify. Just check
24148 to see if it matches ARG. */
24149 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
24150 && cp_tree_equal (parm, arg));
24151 if (result)
24152 unify_expression_unequal (explain_p, parm, arg);
24153 return result;
24154 }
24155
24156 idx = TEMPLATE_PARM_IDX (parm);
24157 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24158
24159 if (targ)
24160 {
24161 if ((strict & UNIFY_ALLOW_INTEGER)
24162 && TREE_TYPE (targ) && TREE_TYPE (arg)
24163 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
24164 /* We're deducing from an array bound, the type doesn't matter. */
24165 arg = fold_convert (TREE_TYPE (targ), arg);
24166 int x = !cp_tree_equal (targ, arg);
24167 if (x)
24168 unify_inconsistency (explain_p, parm, targ, arg);
24169 return x;
24170 }
24171
24172 /* [temp.deduct.type] If, in the declaration of a function template
24173 with a non-type template-parameter, the non-type
24174 template-parameter is used in an expression in the function
24175 parameter-list and, if the corresponding template-argument is
24176 deduced, the template-argument type shall match the type of the
24177 template-parameter exactly, except that a template-argument
24178 deduced from an array bound may be of any integral type.
24179 The non-type parameter might use already deduced type parameters. */
24180 tparm = TREE_TYPE (parm);
24181 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
24182 /* We don't have enough levels of args to do any substitution. This
24183 can happen in the context of -fnew-ttp-matching. */;
24184 else
24185 {
24186 ++processing_template_decl;
24187 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
24188 --processing_template_decl;
24189
24190 if (tree a = type_uses_auto (tparm))
24191 {
24192 tparm = do_auto_deduction (tparm, arg, a,
24193 complain, adc_unify, targs);
24194 if (tparm == error_mark_node)
24195 return 1;
24196 }
24197 }
24198
24199 if (!TREE_TYPE (arg))
24200 /* Template-parameter dependent expression. Just accept it for now.
24201 It will later be processed in convert_template_argument. */
24202 ;
24203 else if (same_type_ignoring_top_level_qualifiers_p
24204 (non_reference (TREE_TYPE (arg)),
24205 non_reference (tparm)))
24206 /* OK. Ignore top-level quals here because a class-type template
24207 parameter object is const. */;
24208 else if ((strict & UNIFY_ALLOW_INTEGER)
24209 && CP_INTEGRAL_TYPE_P (tparm))
24210 /* Convert the ARG to the type of PARM; the deduced non-type
24211 template argument must exactly match the types of the
24212 corresponding parameter. */
24213 arg = fold (build_nop (tparm, arg));
24214 else if (uses_template_parms (tparm))
24215 {
24216 /* We haven't deduced the type of this parameter yet. */
24217 if (cxx_dialect >= cxx17
24218 /* We deduce from array bounds in try_array_deduction. */
24219 && !(strict & UNIFY_ALLOW_INTEGER)
24220 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
24221 {
24222 /* Deduce it from the non-type argument. */
24223 tree atype = TREE_TYPE (arg);
24224 RECUR_AND_CHECK_FAILURE (tparms, targs,
24225 tparm, atype,
24226 UNIFY_ALLOW_NONE, explain_p);
24227 /* Now check whether the type of this parameter is still
24228 dependent, and give up if so. */
24229 ++processing_template_decl;
24230 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
24231 --processing_template_decl;
24232 if (uses_template_parms (tparm))
24233 return unify_success (explain_p);
24234 }
24235 else
24236 /* Try again later. */
24237 return unify_success (explain_p);
24238 }
24239 else
24240 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
24241
24242 /* If ARG is a parameter pack or an expansion, we cannot unify
24243 against it unless PARM is also a parameter pack. */
24244 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
24245 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
24246 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24247
24248 {
24249 bool removed_attr = false;
24250 arg = strip_typedefs_expr (arg, &removed_attr);
24251 }
24252 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24253 return unify_success (explain_p);
24254
24255 case PTRMEM_CST:
24256 {
24257 /* A pointer-to-member constant can be unified only with
24258 another constant. */
24259 if (TREE_CODE (arg) != PTRMEM_CST)
24260 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
24261
24262 /* Just unify the class member. It would be useless (and possibly
24263 wrong, depending on the strict flags) to unify also
24264 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24265 arg refer to the same variable, even if through different
24266 classes. For instance:
24267
24268 struct A { int x; };
24269 struct B : A { };
24270
24271 Unification of &A::x and &B::x must succeed. */
24272 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
24273 PTRMEM_CST_MEMBER (arg), strict, explain_p);
24274 }
24275
24276 case POINTER_TYPE:
24277 {
24278 if (!TYPE_PTR_P (arg))
24279 return unify_type_mismatch (explain_p, parm, arg);
24280
24281 /* [temp.deduct.call]
24282
24283 A can be another pointer or pointer to member type that can
24284 be converted to the deduced A via a qualification
24285 conversion (_conv.qual_).
24286
24287 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24288 This will allow for additional cv-qualification of the
24289 pointed-to types if appropriate. */
24290
24291 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
24292 /* The derived-to-base conversion only persists through one
24293 level of pointers. */
24294 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
24295
24296 return unify (tparms, targs, TREE_TYPE (parm),
24297 TREE_TYPE (arg), strict, explain_p);
24298 }
24299
24300 case REFERENCE_TYPE:
24301 if (!TYPE_REF_P (arg))
24302 return unify_type_mismatch (explain_p, parm, arg);
24303 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24304 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
24305
24306 case ARRAY_TYPE:
24307 if (TREE_CODE (arg) != ARRAY_TYPE)
24308 return unify_type_mismatch (explain_p, parm, arg);
24309 if ((TYPE_DOMAIN (parm) == NULL_TREE)
24310 != (TYPE_DOMAIN (arg) == NULL_TREE))
24311 return unify_type_mismatch (explain_p, parm, arg);
24312 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24313 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
24314 if (TYPE_DOMAIN (parm) != NULL_TREE)
24315 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
24316 TYPE_DOMAIN (arg), explain_p);
24317 return unify_success (explain_p);
24318
24319 case REAL_TYPE:
24320 case COMPLEX_TYPE:
24321 case VECTOR_TYPE:
24322 case INTEGER_TYPE:
24323 case BOOLEAN_TYPE:
24324 case ENUMERAL_TYPE:
24325 case VOID_TYPE:
24326 case OPAQUE_TYPE:
24327 case NULLPTR_TYPE:
24328 if (TREE_CODE (arg) != TREE_CODE (parm))
24329 return unify_type_mismatch (explain_p, parm, arg);
24330
24331 /* We have already checked cv-qualification at the top of the
24332 function. */
24333 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
24334 return unify_type_mismatch (explain_p, parm, arg);
24335
24336 /* As far as unification is concerned, this wins. Later checks
24337 will invalidate it if necessary. */
24338 return unify_success (explain_p);
24339
24340 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
24341 /* Type INTEGER_CST can come from ordinary constant template args. */
24342 case INTEGER_CST:
24343 while (CONVERT_EXPR_P (arg))
24344 arg = TREE_OPERAND (arg, 0);
24345
24346 if (TREE_CODE (arg) != INTEGER_CST)
24347 return unify_template_argument_mismatch (explain_p, parm, arg);
24348 return (tree_int_cst_equal (parm, arg)
24349 ? unify_success (explain_p)
24350 : unify_template_argument_mismatch (explain_p, parm, arg));
24351
24352 case TREE_VEC:
24353 {
24354 int i, len, argslen;
24355 int parm_variadic_p = 0;
24356
24357 if (TREE_CODE (arg) != TREE_VEC)
24358 return unify_template_argument_mismatch (explain_p, parm, arg);
24359
24360 len = TREE_VEC_LENGTH (parm);
24361 argslen = TREE_VEC_LENGTH (arg);
24362
24363 /* Check for pack expansions in the parameters. */
24364 for (i = 0; i < len; ++i)
24365 {
24366 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
24367 {
24368 if (i == len - 1)
24369 /* We can unify against something with a trailing
24370 parameter pack. */
24371 parm_variadic_p = 1;
24372 else
24373 /* [temp.deduct.type]/9: If the template argument list of
24374 P contains a pack expansion that is not the last
24375 template argument, the entire template argument list
24376 is a non-deduced context. */
24377 return unify_success (explain_p);
24378 }
24379 }
24380
24381 /* If we don't have enough arguments to satisfy the parameters
24382 (not counting the pack expression at the end), or we have
24383 too many arguments for a parameter list that doesn't end in
24384 a pack expression, we can't unify. */
24385 if (parm_variadic_p
24386 ? argslen < len - parm_variadic_p
24387 : argslen != len)
24388 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
24389
24390 /* Unify all of the parameters that precede the (optional)
24391 pack expression. */
24392 for (i = 0; i < len - parm_variadic_p; ++i)
24393 {
24394 RECUR_AND_CHECK_FAILURE (tparms, targs,
24395 TREE_VEC_ELT (parm, i),
24396 TREE_VEC_ELT (arg, i),
24397 UNIFY_ALLOW_NONE, explain_p);
24398 }
24399 if (parm_variadic_p)
24400 return unify_pack_expansion (tparms, targs, parm, arg,
24401 DEDUCE_EXACT,
24402 /*subr=*/true, explain_p);
24403 return unify_success (explain_p);
24404 }
24405
24406 case RECORD_TYPE:
24407 case UNION_TYPE:
24408 if (TREE_CODE (arg) != TREE_CODE (parm))
24409 return unify_type_mismatch (explain_p, parm, arg);
24410
24411 if (TYPE_PTRMEMFUNC_P (parm))
24412 {
24413 if (!TYPE_PTRMEMFUNC_P (arg))
24414 return unify_type_mismatch (explain_p, parm, arg);
24415
24416 return unify (tparms, targs,
24417 TYPE_PTRMEMFUNC_FN_TYPE (parm),
24418 TYPE_PTRMEMFUNC_FN_TYPE (arg),
24419 strict, explain_p);
24420 }
24421 else if (TYPE_PTRMEMFUNC_P (arg))
24422 return unify_type_mismatch (explain_p, parm, arg);
24423
24424 if (CLASSTYPE_TEMPLATE_INFO (parm))
24425 {
24426 tree t = NULL_TREE;
24427
24428 if (strict_in & UNIFY_ALLOW_DERIVED)
24429 {
24430 /* First, we try to unify the PARM and ARG directly. */
24431 t = try_class_unification (tparms, targs,
24432 parm, arg, explain_p);
24433
24434 if (!t)
24435 {
24436 /* Fallback to the special case allowed in
24437 [temp.deduct.call]:
24438
24439 If P is a class, and P has the form
24440 template-id, then A can be a derived class of
24441 the deduced A. Likewise, if P is a pointer to
24442 a class of the form template-id, A can be a
24443 pointer to a derived class pointed to by the
24444 deduced A. */
24445 enum template_base_result r;
24446 r = get_template_base (tparms, targs, parm, arg,
24447 explain_p, &t);
24448
24449 if (!t)
24450 {
24451 /* Don't give the derived diagnostic if we're
24452 already dealing with the same template. */
24453 bool same_template
24454 = (CLASSTYPE_TEMPLATE_INFO (arg)
24455 && (CLASSTYPE_TI_TEMPLATE (parm)
24456 == CLASSTYPE_TI_TEMPLATE (arg)));
24457 return unify_no_common_base (explain_p && !same_template,
24458 r, parm, arg);
24459 }
24460 }
24461 }
24462 else if (CLASSTYPE_TEMPLATE_INFO (arg)
24463 && (CLASSTYPE_TI_TEMPLATE (parm)
24464 == CLASSTYPE_TI_TEMPLATE (arg)))
24465 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24466 Then, we should unify `int' and `U'. */
24467 t = arg;
24468 else
24469 /* There's no chance of unification succeeding. */
24470 return unify_type_mismatch (explain_p, parm, arg);
24471
24472 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
24473 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
24474 }
24475 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
24476 return unify_type_mismatch (explain_p, parm, arg);
24477 return unify_success (explain_p);
24478
24479 case METHOD_TYPE:
24480 case FUNCTION_TYPE:
24481 {
24482 unsigned int nargs;
24483 tree *args;
24484 tree a;
24485 unsigned int i;
24486
24487 if (TREE_CODE (arg) != TREE_CODE (parm))
24488 return unify_type_mismatch (explain_p, parm, arg);
24489
24490 /* CV qualifications for methods can never be deduced, they must
24491 match exactly. We need to check them explicitly here,
24492 because type_unification_real treats them as any other
24493 cv-qualified parameter. */
24494 if (TREE_CODE (parm) == METHOD_TYPE
24495 && (!check_cv_quals_for_unify
24496 (UNIFY_ALLOW_NONE,
24497 class_of_this_parm (arg),
24498 class_of_this_parm (parm))))
24499 return unify_cv_qual_mismatch (explain_p, parm, arg);
24500 if (TREE_CODE (arg) == FUNCTION_TYPE
24501 && type_memfn_quals (parm) != type_memfn_quals (arg))
24502 return unify_cv_qual_mismatch (explain_p, parm, arg);
24503 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
24504 return unify_type_mismatch (explain_p, parm, arg);
24505
24506 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
24507 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
24508
24509 nargs = list_length (TYPE_ARG_TYPES (arg));
24510 args = XALLOCAVEC (tree, nargs);
24511 for (a = TYPE_ARG_TYPES (arg), i = 0;
24512 a != NULL_TREE && a != void_list_node;
24513 a = TREE_CHAIN (a), ++i)
24514 args[i] = TREE_VALUE (a);
24515 nargs = i;
24516
24517 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
24518 args, nargs, 1, DEDUCE_EXACT,
24519 NULL, explain_p))
24520 return 1;
24521
24522 if (flag_noexcept_type)
24523 {
24524 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
24525 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
24526 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
24527 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
24528 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
24529 && uses_template_parms (TREE_PURPOSE (pspec)))
24530 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
24531 TREE_PURPOSE (aspec),
24532 UNIFY_ALLOW_NONE, explain_p);
24533 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
24534 return unify_type_mismatch (explain_p, parm, arg);
24535 }
24536
24537 return 0;
24538 }
24539
24540 case OFFSET_TYPE:
24541 /* Unify a pointer to member with a pointer to member function, which
24542 deduces the type of the member as a function type. */
24543 if (TYPE_PTRMEMFUNC_P (arg))
24544 {
24545 /* Check top-level cv qualifiers */
24546 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
24547 return unify_cv_qual_mismatch (explain_p, parm, arg);
24548
24549 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24550 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
24551 UNIFY_ALLOW_NONE, explain_p);
24552
24553 /* Determine the type of the function we are unifying against. */
24554 tree fntype = static_fn_type (arg);
24555
24556 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
24557 }
24558
24559 if (TREE_CODE (arg) != OFFSET_TYPE)
24560 return unify_type_mismatch (explain_p, parm, arg);
24561 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24562 TYPE_OFFSET_BASETYPE (arg),
24563 UNIFY_ALLOW_NONE, explain_p);
24564 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24565 strict, explain_p);
24566
24567 case CONST_DECL:
24568 if (DECL_TEMPLATE_PARM_P (parm))
24569 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
24570 if (arg != scalar_constant_value (parm))
24571 return unify_template_argument_mismatch (explain_p, parm, arg);
24572 return unify_success (explain_p);
24573
24574 case FIELD_DECL:
24575 case TEMPLATE_DECL:
24576 /* Matched cases are handled by the ARG == PARM test above. */
24577 return unify_template_argument_mismatch (explain_p, parm, arg);
24578
24579 case VAR_DECL:
24580 /* We might get a variable as a non-type template argument in parm if the
24581 corresponding parameter is type-dependent. Make any necessary
24582 adjustments based on whether arg is a reference. */
24583 if (CONSTANT_CLASS_P (arg))
24584 parm = fold_non_dependent_expr (parm, complain);
24585 else if (REFERENCE_REF_P (arg))
24586 {
24587 tree sub = TREE_OPERAND (arg, 0);
24588 STRIP_NOPS (sub);
24589 if (TREE_CODE (sub) == ADDR_EXPR)
24590 arg = TREE_OPERAND (sub, 0);
24591 }
24592 /* Now use the normal expression code to check whether they match. */
24593 goto expr;
24594
24595 case TYPE_ARGUMENT_PACK:
24596 case NONTYPE_ARGUMENT_PACK:
24597 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
24598 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
24599
24600 case TYPEOF_TYPE:
24601 case DECLTYPE_TYPE:
24602 case UNDERLYING_TYPE:
24603 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24604 or UNDERLYING_TYPE nodes. */
24605 return unify_success (explain_p);
24606
24607 case ERROR_MARK:
24608 /* Unification fails if we hit an error node. */
24609 return unify_invalid (explain_p);
24610
24611 case INDIRECT_REF:
24612 if (REFERENCE_REF_P (parm))
24613 {
24614 bool pexp = PACK_EXPANSION_P (arg);
24615 if (pexp)
24616 arg = PACK_EXPANSION_PATTERN (arg);
24617 if (REFERENCE_REF_P (arg))
24618 arg = TREE_OPERAND (arg, 0);
24619 if (pexp)
24620 arg = make_pack_expansion (arg, complain);
24621 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24622 strict, explain_p);
24623 }
24624 /* FALLTHRU */
24625
24626 default:
24627 /* An unresolved overload is a nondeduced context. */
24628 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24629 return unify_success (explain_p);
24630 gcc_assert (EXPR_P (parm)
24631 || COMPOUND_LITERAL_P (parm)
24632 || TREE_CODE (parm) == TRAIT_EXPR);
24633 expr:
24634 /* We must be looking at an expression. This can happen with
24635 something like:
24636
24637 template <int I>
24638 void foo(S<I>, S<I + 2>);
24639
24640 or
24641
24642 template<typename T>
24643 void foo(A<T, T{}>);
24644
24645 This is a "non-deduced context":
24646
24647 [deduct.type]
24648
24649 The non-deduced contexts are:
24650
24651 --A non-type template argument or an array bound in which
24652 a subexpression references a template parameter.
24653
24654 In these cases, we assume deduction succeeded, but don't
24655 actually infer any unifications. */
24656
24657 if (!uses_template_parms (parm)
24658 && !template_args_equal (parm, arg))
24659 return unify_expression_unequal (explain_p, parm, arg);
24660 else
24661 return unify_success (explain_p);
24662 }
24663 }
24664 #undef RECUR_AND_CHECK_FAILURE
24665 \f
24666 /* Note that DECL can be defined in this translation unit, if
24667 required. */
24668
24669 static void
24670 mark_definable (tree decl)
24671 {
24672 tree clone;
24673 DECL_NOT_REALLY_EXTERN (decl) = 1;
24674 FOR_EACH_CLONE (clone, decl)
24675 DECL_NOT_REALLY_EXTERN (clone) = 1;
24676 }
24677
24678 /* Called if RESULT is explicitly instantiated, or is a member of an
24679 explicitly instantiated class. */
24680
24681 void
24682 mark_decl_instantiated (tree result, int extern_p)
24683 {
24684 SET_DECL_EXPLICIT_INSTANTIATION (result);
24685
24686 /* If this entity has already been written out, it's too late to
24687 make any modifications. */
24688 if (TREE_ASM_WRITTEN (result))
24689 return;
24690
24691 /* consteval functions are never emitted. */
24692 if (TREE_CODE (result) == FUNCTION_DECL
24693 && DECL_IMMEDIATE_FUNCTION_P (result))
24694 return;
24695
24696 /* For anonymous namespace we don't need to do anything. */
24697 if (decl_anon_ns_mem_p (result))
24698 {
24699 gcc_assert (!TREE_PUBLIC (result));
24700 return;
24701 }
24702
24703 if (TREE_CODE (result) != FUNCTION_DECL)
24704 /* The TREE_PUBLIC flag for function declarations will have been
24705 set correctly by tsubst. */
24706 TREE_PUBLIC (result) = 1;
24707
24708 if (extern_p)
24709 {
24710 DECL_EXTERNAL (result) = 1;
24711 DECL_NOT_REALLY_EXTERN (result) = 0;
24712 }
24713 else
24714 {
24715 mark_definable (result);
24716 mark_needed (result);
24717 /* Always make artificials weak. */
24718 if (DECL_ARTIFICIAL (result) && flag_weak)
24719 comdat_linkage (result);
24720 /* For WIN32 we also want to put explicit instantiations in
24721 linkonce sections. */
24722 else if (TREE_PUBLIC (result))
24723 maybe_make_one_only (result);
24724 if (TREE_CODE (result) == FUNCTION_DECL
24725 && DECL_TEMPLATE_INSTANTIATED (result))
24726 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24727 since start_preparsed_function wouldn't have if we had an earlier
24728 extern explicit instantiation. */
24729 DECL_EXTERNAL (result) = 0;
24730 }
24731
24732 /* If EXTERN_P, then this function will not be emitted -- unless
24733 followed by an explicit instantiation, at which point its linkage
24734 will be adjusted. If !EXTERN_P, then this function will be
24735 emitted here. In neither circumstance do we want
24736 import_export_decl to adjust the linkage. */
24737 DECL_INTERFACE_KNOWN (result) = 1;
24738 }
24739
24740 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24741 important template arguments. If any are missing, we check whether
24742 they're important by using error_mark_node for substituting into any
24743 args that were used for partial ordering (the ones between ARGS and END)
24744 and seeing if it bubbles up. */
24745
24746 static bool
24747 check_undeduced_parms (tree targs, tree args, tree end)
24748 {
24749 bool found = false;
24750 int i;
24751 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24752 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24753 {
24754 found = true;
24755 TREE_VEC_ELT (targs, i) = error_mark_node;
24756 }
24757 if (found)
24758 {
24759 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24760 if (substed == error_mark_node)
24761 return true;
24762 }
24763 return false;
24764 }
24765
24766 /* Given two function templates PAT1 and PAT2, return:
24767
24768 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24769 -1 if PAT2 is more specialized than PAT1.
24770 0 if neither is more specialized.
24771
24772 LEN indicates the number of parameters we should consider
24773 (defaulted parameters should not be considered).
24774
24775 The 1998 std underspecified function template partial ordering, and
24776 DR214 addresses the issue. We take pairs of arguments, one from
24777 each of the templates, and deduce them against each other. One of
24778 the templates will be more specialized if all the *other*
24779 template's arguments deduce against its arguments and at least one
24780 of its arguments *does* *not* deduce against the other template's
24781 corresponding argument. Deduction is done as for class templates.
24782 The arguments used in deduction have reference and top level cv
24783 qualifiers removed. Iff both arguments were originally reference
24784 types *and* deduction succeeds in both directions, an lvalue reference
24785 wins against an rvalue reference and otherwise the template
24786 with the more cv-qualified argument wins for that pairing (if
24787 neither is more cv-qualified, they both are equal). Unlike regular
24788 deduction, after all the arguments have been deduced in this way,
24789 we do *not* verify the deduced template argument values can be
24790 substituted into non-deduced contexts.
24791
24792 The logic can be a bit confusing here, because we look at deduce1 and
24793 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24794 can find template arguments for pat1 to make arg1 look like arg2, that
24795 means that arg2 is at least as specialized as arg1. */
24796
24797 int
24798 more_specialized_fn (tree pat1, tree pat2, int len)
24799 {
24800 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24801 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24802 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24803 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24804 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24805 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24806 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24807 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24808 tree origs1, origs2;
24809 bool lose1 = false;
24810 bool lose2 = false;
24811
24812 /* Remove the this parameter from non-static member functions. If
24813 one is a non-static member function and the other is not a static
24814 member function, remove the first parameter from that function
24815 also. This situation occurs for operator functions where we
24816 locate both a member function (with this pointer) and non-member
24817 operator (with explicit first operand). */
24818 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24819 {
24820 len--; /* LEN is the number of significant arguments for DECL1 */
24821 args1 = TREE_CHAIN (args1);
24822 if (!DECL_STATIC_FUNCTION_P (decl2))
24823 args2 = TREE_CHAIN (args2);
24824 }
24825 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24826 {
24827 args2 = TREE_CHAIN (args2);
24828 if (!DECL_STATIC_FUNCTION_P (decl1))
24829 {
24830 len--;
24831 args1 = TREE_CHAIN (args1);
24832 }
24833 }
24834
24835 /* If only one is a conversion operator, they are unordered. */
24836 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24837 return 0;
24838
24839 /* Consider the return type for a conversion function */
24840 if (DECL_CONV_FN_P (decl1))
24841 {
24842 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24843 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24844 len++;
24845 }
24846
24847 processing_template_decl++;
24848
24849 origs1 = args1;
24850 origs2 = args2;
24851
24852 while (len--
24853 /* Stop when an ellipsis is seen. */
24854 && args1 != NULL_TREE && args2 != NULL_TREE)
24855 {
24856 tree arg1 = TREE_VALUE (args1);
24857 tree arg2 = TREE_VALUE (args2);
24858 int deduce1, deduce2;
24859 int quals1 = -1;
24860 int quals2 = -1;
24861 int ref1 = 0;
24862 int ref2 = 0;
24863
24864 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24865 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24866 {
24867 /* When both arguments are pack expansions, we need only
24868 unify the patterns themselves. */
24869 arg1 = PACK_EXPANSION_PATTERN (arg1);
24870 arg2 = PACK_EXPANSION_PATTERN (arg2);
24871
24872 /* This is the last comparison we need to do. */
24873 len = 0;
24874 }
24875
24876 if (TYPE_REF_P (arg1))
24877 {
24878 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24879 arg1 = TREE_TYPE (arg1);
24880 quals1 = cp_type_quals (arg1);
24881 }
24882
24883 if (TYPE_REF_P (arg2))
24884 {
24885 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24886 arg2 = TREE_TYPE (arg2);
24887 quals2 = cp_type_quals (arg2);
24888 }
24889
24890 arg1 = TYPE_MAIN_VARIANT (arg1);
24891 arg2 = TYPE_MAIN_VARIANT (arg2);
24892
24893 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24894 {
24895 int i, len2 = remaining_arguments (args2);
24896 tree parmvec = make_tree_vec (1);
24897 tree argvec = make_tree_vec (len2);
24898 tree ta = args2;
24899
24900 /* Setup the parameter vector, which contains only ARG1. */
24901 TREE_VEC_ELT (parmvec, 0) = arg1;
24902
24903 /* Setup the argument vector, which contains the remaining
24904 arguments. */
24905 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24906 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24907
24908 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24909 argvec, DEDUCE_EXACT,
24910 /*subr=*/true, /*explain_p=*/false)
24911 == 0);
24912
24913 /* We cannot deduce in the other direction, because ARG1 is
24914 a pack expansion but ARG2 is not. */
24915 deduce2 = 0;
24916 }
24917 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24918 {
24919 int i, len1 = remaining_arguments (args1);
24920 tree parmvec = make_tree_vec (1);
24921 tree argvec = make_tree_vec (len1);
24922 tree ta = args1;
24923
24924 /* Setup the parameter vector, which contains only ARG1. */
24925 TREE_VEC_ELT (parmvec, 0) = arg2;
24926
24927 /* Setup the argument vector, which contains the remaining
24928 arguments. */
24929 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24930 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24931
24932 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24933 argvec, DEDUCE_EXACT,
24934 /*subr=*/true, /*explain_p=*/false)
24935 == 0);
24936
24937 /* We cannot deduce in the other direction, because ARG2 is
24938 a pack expansion but ARG1 is not.*/
24939 deduce1 = 0;
24940 }
24941
24942 else
24943 {
24944 /* The normal case, where neither argument is a pack
24945 expansion. */
24946 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24947 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24948 == 0);
24949 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24950 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24951 == 0);
24952 }
24953
24954 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24955 arg2, then arg2 is not as specialized as arg1. */
24956 if (!deduce1)
24957 lose2 = true;
24958 if (!deduce2)
24959 lose1 = true;
24960
24961 /* "If, for a given type, deduction succeeds in both directions
24962 (i.e., the types are identical after the transformations above)
24963 and both P and A were reference types (before being replaced with
24964 the type referred to above):
24965 - if the type from the argument template was an lvalue reference and
24966 the type from the parameter template was not, the argument type is
24967 considered to be more specialized than the other; otherwise,
24968 - if the type from the argument template is more cv-qualified
24969 than the type from the parameter template (as described above),
24970 the argument type is considered to be more specialized than the other;
24971 otherwise,
24972 - neither type is more specialized than the other." */
24973
24974 if (deduce1 && deduce2)
24975 {
24976 if (ref1 && ref2 && ref1 != ref2)
24977 {
24978 if (ref1 > ref2)
24979 lose1 = true;
24980 else
24981 lose2 = true;
24982 }
24983 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24984 {
24985 if ((quals1 & quals2) == quals2)
24986 lose2 = true;
24987 if ((quals1 & quals2) == quals1)
24988 lose1 = true;
24989 }
24990 }
24991
24992 if (lose1 && lose2)
24993 /* We've failed to deduce something in either direction.
24994 These must be unordered. */
24995 break;
24996
24997 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24998 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24999 /* We have already processed all of the arguments in our
25000 handing of the pack expansion type. */
25001 len = 0;
25002
25003 args1 = TREE_CHAIN (args1);
25004 args2 = TREE_CHAIN (args2);
25005 }
25006
25007 /* "In most cases, all template parameters must have values in order for
25008 deduction to succeed, but for partial ordering purposes a template
25009 parameter may remain without a value provided it is not used in the
25010 types being used for partial ordering."
25011
25012 Thus, if we are missing any of the targs1 we need to substitute into
25013 origs1, then pat2 is not as specialized as pat1. This can happen when
25014 there is a nondeduced context. */
25015 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
25016 lose2 = true;
25017 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
25018 lose1 = true;
25019
25020 processing_template_decl--;
25021
25022 /* If both deductions succeed, the partial ordering selects the more
25023 constrained template. */
25024 /* P2113: If the corresponding template-parameters of the
25025 template-parameter-lists are not equivalent ([temp.over.link]) or if
25026 the function parameters that positionally correspond between the two
25027 templates are not of the same type, neither template is more
25028 specialized than the other. */
25029 if (!lose1 && !lose2
25030 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
25031 DECL_TEMPLATE_PARMS (pat2))
25032 && compparms (origs1, origs2))
25033 {
25034 int winner = more_constrained (decl1, decl2);
25035 if (winner > 0)
25036 lose2 = true;
25037 else if (winner < 0)
25038 lose1 = true;
25039 }
25040
25041 /* All things being equal, if the next argument is a pack expansion
25042 for one function but not for the other, prefer the
25043 non-variadic function. FIXME this is bogus; see c++/41958. */
25044 if (lose1 == lose2
25045 && args1 && TREE_VALUE (args1)
25046 && args2 && TREE_VALUE (args2))
25047 {
25048 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
25049 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
25050 }
25051
25052 if (lose1 == lose2)
25053 return 0;
25054 else if (!lose1)
25055 return 1;
25056 else
25057 return -1;
25058 }
25059
25060 /* Determine which of two partial specializations of TMPL is more
25061 specialized.
25062
25063 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25064 to the first partial specialization. The TREE_PURPOSE is the
25065 innermost set of template parameters for the partial
25066 specialization. PAT2 is similar, but for the second template.
25067
25068 Return 1 if the first partial specialization is more specialized;
25069 -1 if the second is more specialized; 0 if neither is more
25070 specialized.
25071
25072 See [temp.class.order] for information about determining which of
25073 two templates is more specialized. */
25074
25075 static int
25076 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
25077 {
25078 tree targs;
25079 int winner = 0;
25080 bool any_deductions = false;
25081
25082 tree tmpl1 = TREE_VALUE (pat1);
25083 tree tmpl2 = TREE_VALUE (pat2);
25084 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
25085 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
25086
25087 /* Just like what happens for functions, if we are ordering between
25088 different template specializations, we may encounter dependent
25089 types in the arguments, and we need our dependency check functions
25090 to behave correctly. */
25091 ++processing_template_decl;
25092 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
25093 if (targs)
25094 {
25095 --winner;
25096 any_deductions = true;
25097 }
25098
25099 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
25100 if (targs)
25101 {
25102 ++winner;
25103 any_deductions = true;
25104 }
25105 --processing_template_decl;
25106
25107 /* If both deductions succeed, the partial ordering selects the more
25108 constrained template. */
25109 if (!winner && any_deductions)
25110 winner = more_constrained (tmpl1, tmpl2);
25111
25112 /* In the case of a tie where at least one of the templates
25113 has a parameter pack at the end, the template with the most
25114 non-packed parameters wins. */
25115 if (winner == 0
25116 && any_deductions
25117 && (template_args_variadic_p (TREE_PURPOSE (pat1))
25118 || template_args_variadic_p (TREE_PURPOSE (pat2))))
25119 {
25120 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
25121 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
25122 int len1 = TREE_VEC_LENGTH (args1);
25123 int len2 = TREE_VEC_LENGTH (args2);
25124
25125 /* We don't count the pack expansion at the end. */
25126 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
25127 --len1;
25128 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
25129 --len2;
25130
25131 if (len1 > len2)
25132 return 1;
25133 else if (len1 < len2)
25134 return -1;
25135 }
25136
25137 return winner;
25138 }
25139
25140 /* Return the template arguments that will produce the function signature
25141 DECL from the function template FN, with the explicit template
25142 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25143 also match. Return NULL_TREE if no satisfactory arguments could be
25144 found. */
25145
25146 static tree
25147 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
25148 {
25149 int ntparms = DECL_NTPARMS (fn);
25150 tree targs = make_tree_vec (ntparms);
25151 tree decl_type = TREE_TYPE (decl);
25152 tree decl_arg_types;
25153 tree *args;
25154 unsigned int nargs, ix;
25155 tree arg;
25156
25157 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
25158
25159 /* Never do unification on the 'this' parameter. */
25160 decl_arg_types = skip_artificial_parms_for (decl,
25161 TYPE_ARG_TYPES (decl_type));
25162
25163 nargs = list_length (decl_arg_types);
25164 args = XALLOCAVEC (tree, nargs);
25165 for (arg = decl_arg_types, ix = 0;
25166 arg != NULL_TREE;
25167 arg = TREE_CHAIN (arg), ++ix)
25168 args[ix] = TREE_VALUE (arg);
25169
25170 if (fn_type_unification (fn, explicit_args, targs,
25171 args, ix,
25172 (check_rettype || DECL_CONV_FN_P (fn)
25173 ? TREE_TYPE (decl_type) : NULL_TREE),
25174 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
25175 /*explain_p=*/false,
25176 /*decltype*/false)
25177 == error_mark_node)
25178 return NULL_TREE;
25179
25180 return targs;
25181 }
25182
25183 /* Return the innermost template arguments that, when applied to a partial
25184 specialization SPEC_TMPL of TMPL, yield the ARGS.
25185
25186 For example, suppose we have:
25187
25188 template <class T, class U> struct S {};
25189 template <class T> struct S<T*, int> {};
25190
25191 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25192 partial specialization and the ARGS will be {double*, int}. The resulting
25193 vector will be {double}, indicating that `T' is bound to `double'. */
25194
25195 static tree
25196 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
25197 {
25198 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
25199 tree spec_args
25200 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
25201 int i, ntparms = TREE_VEC_LENGTH (tparms);
25202 tree deduced_args;
25203 tree innermost_deduced_args;
25204
25205 innermost_deduced_args = make_tree_vec (ntparms);
25206 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25207 {
25208 deduced_args = copy_node (args);
25209 SET_TMPL_ARGS_LEVEL (deduced_args,
25210 TMPL_ARGS_DEPTH (deduced_args),
25211 innermost_deduced_args);
25212 }
25213 else
25214 deduced_args = innermost_deduced_args;
25215
25216 bool tried_array_deduction = (cxx_dialect < cxx17);
25217 again:
25218 if (unify (tparms, deduced_args,
25219 INNERMOST_TEMPLATE_ARGS (spec_args),
25220 INNERMOST_TEMPLATE_ARGS (args),
25221 UNIFY_ALLOW_NONE, /*explain_p=*/false))
25222 return NULL_TREE;
25223
25224 for (i = 0; i < ntparms; ++i)
25225 if (! TREE_VEC_ELT (innermost_deduced_args, i))
25226 {
25227 if (!tried_array_deduction)
25228 {
25229 try_array_deduction (tparms, innermost_deduced_args,
25230 INNERMOST_TEMPLATE_ARGS (spec_args));
25231 tried_array_deduction = true;
25232 if (TREE_VEC_ELT (innermost_deduced_args, i))
25233 goto again;
25234 }
25235 return NULL_TREE;
25236 }
25237
25238 if (!push_tinst_level (spec_tmpl, deduced_args))
25239 {
25240 excessive_deduction_depth = true;
25241 return NULL_TREE;
25242 }
25243
25244 /* Verify that nondeduced template arguments agree with the type
25245 obtained from argument deduction.
25246
25247 For example:
25248
25249 struct A { typedef int X; };
25250 template <class T, class U> struct C {};
25251 template <class T> struct C<T, typename T::X> {};
25252
25253 Then with the instantiation `C<A, int>', we can deduce that
25254 `T' is `A' but unify () does not check whether `typename T::X'
25255 is `int'. */
25256 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
25257
25258 if (spec_args != error_mark_node)
25259 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
25260 INNERMOST_TEMPLATE_ARGS (spec_args),
25261 tmpl, tf_none, false, false);
25262
25263 pop_tinst_level ();
25264
25265 if (spec_args == error_mark_node
25266 /* We only need to check the innermost arguments; the other
25267 arguments will always agree. */
25268 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
25269 INNERMOST_TEMPLATE_ARGS (args)))
25270 return NULL_TREE;
25271
25272 /* Now that we have bindings for all of the template arguments,
25273 ensure that the arguments deduced for the template template
25274 parameters have compatible template parameter lists. See the use
25275 of template_template_parm_bindings_ok_p in fn_type_unification
25276 for more information. */
25277 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
25278 return NULL_TREE;
25279
25280 return deduced_args;
25281 }
25282
25283 // Compare two function templates T1 and T2 by deducing bindings
25284 // from one against the other. If both deductions succeed, compare
25285 // constraints to see which is more constrained.
25286 static int
25287 more_specialized_inst (tree t1, tree t2)
25288 {
25289 int fate = 0;
25290 int count = 0;
25291
25292 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
25293 {
25294 --fate;
25295 ++count;
25296 }
25297
25298 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
25299 {
25300 ++fate;
25301 ++count;
25302 }
25303
25304 // If both deductions succeed, then one may be more constrained.
25305 if (count == 2 && fate == 0)
25306 fate = more_constrained (t1, t2);
25307
25308 return fate;
25309 }
25310
25311 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
25312 Return the TREE_LIST node with the most specialized template, if
25313 any. If there is no most specialized template, the error_mark_node
25314 is returned.
25315
25316 Note that this function does not look at, or modify, the
25317 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
25318 returned is one of the elements of INSTANTIATIONS, callers may
25319 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
25320 and retrieve it from the value returned. */
25321
25322 tree
25323 most_specialized_instantiation (tree templates)
25324 {
25325 tree fn, champ;
25326
25327 ++processing_template_decl;
25328
25329 champ = templates;
25330 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
25331 {
25332 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
25333 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
25334 if (fate == -1)
25335 champ = fn;
25336 else if (!fate)
25337 {
25338 /* Equally specialized, move to next function. If there
25339 is no next function, nothing's most specialized. */
25340 fn = TREE_CHAIN (fn);
25341 champ = fn;
25342 if (!fn)
25343 break;
25344 }
25345 }
25346
25347 if (champ)
25348 /* Now verify that champ is better than everything earlier in the
25349 instantiation list. */
25350 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
25351 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
25352 {
25353 champ = NULL_TREE;
25354 break;
25355 }
25356 }
25357
25358 processing_template_decl--;
25359
25360 if (!champ)
25361 return error_mark_node;
25362
25363 return champ;
25364 }
25365
25366 /* If DECL is a specialization of some template, return the most
25367 general such template. Otherwise, returns NULL_TREE.
25368
25369 For example, given:
25370
25371 template <class T> struct S { template <class U> void f(U); };
25372
25373 if TMPL is `template <class U> void S<int>::f(U)' this will return
25374 the full template. This function will not trace past partial
25375 specializations, however. For example, given in addition:
25376
25377 template <class T> struct S<T*> { template <class U> void f(U); };
25378
25379 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25380 `template <class T> template <class U> S<T*>::f(U)'. */
25381
25382 tree
25383 most_general_template (tree decl)
25384 {
25385 if (TREE_CODE (decl) != TEMPLATE_DECL)
25386 {
25387 if (tree tinfo = get_template_info (decl))
25388 decl = TI_TEMPLATE (tinfo);
25389 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25390 template friend, or a FIELD_DECL for a capture pack. */
25391 if (TREE_CODE (decl) != TEMPLATE_DECL)
25392 return NULL_TREE;
25393 }
25394
25395 /* Look for more and more general templates. */
25396 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
25397 {
25398 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25399 (See cp-tree.h for details.) */
25400 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
25401 break;
25402
25403 if (CLASS_TYPE_P (TREE_TYPE (decl))
25404 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
25405 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
25406 break;
25407
25408 /* Stop if we run into an explicitly specialized class template. */
25409 if (!DECL_NAMESPACE_SCOPE_P (decl)
25410 && DECL_CONTEXT (decl)
25411 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
25412 break;
25413
25414 decl = DECL_TI_TEMPLATE (decl);
25415 }
25416
25417 return decl;
25418 }
25419
25420 /* Return the most specialized of the template partial specializations
25421 which can produce TARGET, a specialization of some class or variable
25422 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25423 a TEMPLATE_DECL node corresponding to the partial specialization, while
25424 the TREE_PURPOSE is the set of template arguments that must be
25425 substituted into the template pattern in order to generate TARGET.
25426
25427 If the choice of partial specialization is ambiguous, a diagnostic
25428 is issued, and the error_mark_node is returned. If there are no
25429 partial specializations matching TARGET, then NULL_TREE is
25430 returned, indicating that the primary template should be used. */
25431
25432 tree
25433 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
25434 {
25435 tree list = NULL_TREE;
25436 tree t;
25437 tree champ;
25438 int fate;
25439 bool ambiguous_p;
25440 tree outer_args = NULL_TREE;
25441 tree tmpl, args;
25442
25443 tree decl;
25444 if (TYPE_P (target))
25445 {
25446 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
25447 tmpl = TI_TEMPLATE (tinfo);
25448 args = TI_ARGS (tinfo);
25449 decl = TYPE_NAME (target);
25450 }
25451 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
25452 {
25453 tmpl = TREE_OPERAND (target, 0);
25454 args = TREE_OPERAND (target, 1);
25455 decl = DECL_TEMPLATE_RESULT (tmpl);
25456 }
25457 else if (VAR_P (target))
25458 {
25459 tree tinfo = DECL_TEMPLATE_INFO (target);
25460 tmpl = TI_TEMPLATE (tinfo);
25461 args = TI_ARGS (tinfo);
25462 decl = target;
25463 }
25464 else
25465 gcc_unreachable ();
25466
25467 push_access_scope_guard pas (decl);
25468 deferring_access_check_sentinel acs (dk_no_deferred);
25469
25470 tree main_tmpl = most_general_template (tmpl);
25471
25472 /* For determining which partial specialization to use, only the
25473 innermost args are interesting. */
25474 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25475 {
25476 outer_args = strip_innermost_template_args (args, 1);
25477 args = INNERMOST_TEMPLATE_ARGS (args);
25478 }
25479
25480 /* The caller hasn't called push_to_top_level yet, but we need
25481 get_partial_spec_bindings to be done in non-template context so that we'll
25482 fully resolve everything. */
25483 processing_template_decl_sentinel ptds;
25484
25485 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
25486 {
25487 const tree ospec_tmpl = TREE_VALUE (t);
25488
25489 tree spec_tmpl;
25490 if (outer_args)
25491 {
25492 /* Substitute in the template args from the enclosing class. */
25493 ++processing_template_decl;
25494 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
25495 --processing_template_decl;
25496 if (spec_tmpl == error_mark_node)
25497 return error_mark_node;
25498 }
25499 else
25500 spec_tmpl = ospec_tmpl;
25501
25502 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
25503 if (spec_args)
25504 {
25505 if (outer_args)
25506 spec_args = add_to_template_args (outer_args, spec_args);
25507
25508 /* Keep the candidate only if the constraints are satisfied,
25509 or if we're not compiling with concepts. */
25510 if (!flag_concepts
25511 || constraints_satisfied_p (ospec_tmpl, spec_args))
25512 {
25513 list = tree_cons (spec_args, ospec_tmpl, list);
25514 TREE_TYPE (list) = TREE_TYPE (t);
25515 }
25516 }
25517 }
25518
25519 if (! list)
25520 return NULL_TREE;
25521
25522 ambiguous_p = false;
25523 t = list;
25524 champ = t;
25525 t = TREE_CHAIN (t);
25526 for (; t; t = TREE_CHAIN (t))
25527 {
25528 fate = more_specialized_partial_spec (tmpl, champ, t);
25529 if (fate == 1)
25530 ;
25531 else
25532 {
25533 if (fate == 0)
25534 {
25535 t = TREE_CHAIN (t);
25536 if (! t)
25537 {
25538 ambiguous_p = true;
25539 break;
25540 }
25541 }
25542 champ = t;
25543 }
25544 }
25545
25546 if (!ambiguous_p)
25547 for (t = list; t && t != champ; t = TREE_CHAIN (t))
25548 {
25549 fate = more_specialized_partial_spec (tmpl, champ, t);
25550 if (fate != 1)
25551 {
25552 ambiguous_p = true;
25553 break;
25554 }
25555 }
25556
25557 if (ambiguous_p)
25558 {
25559 const char *str;
25560 char *spaces = NULL;
25561 if (!(complain & tf_error))
25562 return error_mark_node;
25563 if (TYPE_P (target))
25564 error ("ambiguous template instantiation for %q#T", target);
25565 else
25566 error ("ambiguous template instantiation for %q#D", target);
25567 str = ngettext ("candidate is:", "candidates are:", list_length (list));
25568 for (t = list; t; t = TREE_CHAIN (t))
25569 {
25570 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
25571 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
25572 "%s %#qS", spaces ? spaces : str, subst);
25573 spaces = spaces ? spaces : get_spaces (str);
25574 }
25575 free (spaces);
25576 return error_mark_node;
25577 }
25578
25579 return champ;
25580 }
25581
25582 /* Explicitly instantiate DECL. */
25583
25584 void
25585 do_decl_instantiation (tree decl, tree storage)
25586 {
25587 tree result = NULL_TREE;
25588 int extern_p = 0;
25589
25590 if (!decl || decl == error_mark_node)
25591 /* An error occurred, for which grokdeclarator has already issued
25592 an appropriate message. */
25593 return;
25594 else if (! DECL_LANG_SPECIFIC (decl))
25595 {
25596 error ("explicit instantiation of non-template %q#D", decl);
25597 return;
25598 }
25599 else if (DECL_DECLARED_CONCEPT_P (decl))
25600 {
25601 if (VAR_P (decl))
25602 error ("explicit instantiation of variable concept %q#D", decl);
25603 else
25604 error ("explicit instantiation of function concept %q#D", decl);
25605 return;
25606 }
25607
25608 bool var_templ = (DECL_TEMPLATE_INFO (decl)
25609 && variable_template_p (DECL_TI_TEMPLATE (decl)));
25610
25611 if (VAR_P (decl) && !var_templ)
25612 {
25613 /* There is an asymmetry here in the way VAR_DECLs and
25614 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25615 the latter, the DECL we get back will be marked as a
25616 template instantiation, and the appropriate
25617 DECL_TEMPLATE_INFO will be set up. This does not happen for
25618 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25619 should handle VAR_DECLs as it currently handles
25620 FUNCTION_DECLs. */
25621 if (!DECL_CLASS_SCOPE_P (decl))
25622 {
25623 error ("%qD is not a static data member of a class template", decl);
25624 return;
25625 }
25626 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
25627 if (!result || !VAR_P (result))
25628 {
25629 error ("no matching template for %qD found", decl);
25630 return;
25631 }
25632 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25633 {
25634 error ("type %qT for explicit instantiation %qD does not match "
25635 "declared type %qT", TREE_TYPE (result), decl,
25636 TREE_TYPE (decl));
25637 return;
25638 }
25639 }
25640 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25641 {
25642 error ("explicit instantiation of %q#D", decl);
25643 return;
25644 }
25645 else
25646 result = decl;
25647
25648 /* Check for various error cases. Note that if the explicit
25649 instantiation is valid the RESULT will currently be marked as an
25650 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25651 until we get here. */
25652
25653 if (DECL_TEMPLATE_SPECIALIZATION (result))
25654 {
25655 /* DR 259 [temp.spec].
25656
25657 Both an explicit instantiation and a declaration of an explicit
25658 specialization shall not appear in a program unless the explicit
25659 instantiation follows a declaration of the explicit specialization.
25660
25661 For a given set of template parameters, if an explicit
25662 instantiation of a template appears after a declaration of an
25663 explicit specialization for that template, the explicit
25664 instantiation has no effect. */
25665 return;
25666 }
25667 else if (DECL_EXPLICIT_INSTANTIATION (result))
25668 {
25669 /* [temp.spec]
25670
25671 No program shall explicitly instantiate any template more
25672 than once.
25673
25674 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25675 the first instantiation was `extern' and the second is not,
25676 and EXTERN_P for the opposite case. */
25677 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25678 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25679 /* If an "extern" explicit instantiation follows an ordinary
25680 explicit instantiation, the template is instantiated. */
25681 if (extern_p)
25682 return;
25683 }
25684 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25685 {
25686 error ("no matching template for %qD found", result);
25687 return;
25688 }
25689 else if (!DECL_TEMPLATE_INFO (result))
25690 {
25691 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25692 return;
25693 }
25694
25695 if (storage == NULL_TREE)
25696 ;
25697 else if (storage == ridpointers[(int) RID_EXTERN])
25698 {
25699 if (cxx_dialect == cxx98)
25700 pedwarn (input_location, OPT_Wpedantic,
25701 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25702 "instantiations");
25703 extern_p = 1;
25704 }
25705 else
25706 error ("storage class %qD applied to template instantiation", storage);
25707
25708 check_explicit_instantiation_namespace (result);
25709 mark_decl_instantiated (result, extern_p);
25710 if (! extern_p)
25711 instantiate_decl (result, /*defer_ok=*/true,
25712 /*expl_inst_class_mem_p=*/false);
25713 }
25714
25715 static void
25716 mark_class_instantiated (tree t, int extern_p)
25717 {
25718 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25719 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25720 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25721 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25722 if (! extern_p)
25723 {
25724 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25725 rest_of_type_compilation (t, 1);
25726 }
25727 }
25728
25729 /* Perform an explicit instantiation of template class T. STORAGE, if
25730 non-null, is the RID for extern, inline or static. COMPLAIN is
25731 nonzero if this is called from the parser, zero if called recursively,
25732 since the standard is unclear (as detailed below). */
25733
25734 void
25735 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25736 {
25737 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25738 {
25739 if (tree ti = TYPE_TEMPLATE_INFO (t))
25740 error ("explicit instantiation of non-class template %qD",
25741 TI_TEMPLATE (ti));
25742 else
25743 error ("explicit instantiation of non-template type %qT", t);
25744 return;
25745 }
25746
25747 complete_type (t);
25748
25749 if (!COMPLETE_TYPE_P (t))
25750 {
25751 if (complain & tf_error)
25752 error ("explicit instantiation of %q#T before definition of template",
25753 t);
25754 return;
25755 }
25756
25757 /* At most one of these will be true. */
25758 bool extern_p = false;
25759 bool nomem_p = false;
25760 bool static_p = false;
25761
25762 if (storage != NULL_TREE)
25763 {
25764 if (storage == ridpointers[(int) RID_EXTERN])
25765 {
25766 if (cxx_dialect == cxx98)
25767 pedwarn (input_location, OPT_Wpedantic,
25768 "ISO C++ 1998 forbids the use of %<extern%> on "
25769 "explicit instantiations");
25770 }
25771 else
25772 pedwarn (input_location, OPT_Wpedantic,
25773 "ISO C++ forbids the use of %qE"
25774 " on explicit instantiations", storage);
25775
25776 if (storage == ridpointers[(int) RID_INLINE])
25777 nomem_p = true;
25778 else if (storage == ridpointers[(int) RID_EXTERN])
25779 extern_p = true;
25780 else if (storage == ridpointers[(int) RID_STATIC])
25781 static_p = true;
25782 else
25783 error ("storage class %qD applied to template instantiation",
25784 storage);
25785 }
25786
25787 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25788 /* DR 259 [temp.spec].
25789
25790 Both an explicit instantiation and a declaration of an explicit
25791 specialization shall not appear in a program unless the
25792 explicit instantiation follows a declaration of the explicit
25793 specialization.
25794
25795 For a given set of template parameters, if an explicit
25796 instantiation of a template appears after a declaration of an
25797 explicit specialization for that template, the explicit
25798 instantiation has no effect. */
25799 return;
25800
25801 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25802 {
25803 /* We've already instantiated the template. */
25804
25805 /* [temp.spec]
25806
25807 No program shall explicitly instantiate any template more
25808 than once.
25809
25810 If EXTERN_P then this is ok. */
25811 if (!extern_p && (complain & tf_error))
25812 permerror (input_location,
25813 "duplicate explicit instantiation of %q#T", t);
25814
25815 return;
25816 }
25817
25818 check_explicit_instantiation_namespace (TYPE_NAME (t));
25819 mark_class_instantiated (t, extern_p);
25820
25821 if (nomem_p)
25822 return;
25823
25824 /* In contrast to implicit instantiation, where only the
25825 declarations, and not the definitions, of members are
25826 instantiated, we have here:
25827
25828 [temp.explicit]
25829
25830 An explicit instantiation that names a class template
25831 specialization is also an explicit instantiation of the same
25832 kind (declaration or definition) of each of its members (not
25833 including members inherited from base classes and members
25834 that are templates) that has not been previously explicitly
25835 specialized in the translation unit containing the explicit
25836 instantiation, provided that the associated constraints, if
25837 any, of that member are satisfied by the template arguments
25838 of the explicit instantiation. */
25839 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25840 if ((VAR_P (fld)
25841 || (TREE_CODE (fld) == FUNCTION_DECL
25842 && !static_p
25843 && user_provided_p (fld)))
25844 && DECL_TEMPLATE_INSTANTIATION (fld)
25845 && constraints_satisfied_p (fld))
25846 {
25847 mark_decl_instantiated (fld, extern_p);
25848 if (! extern_p)
25849 instantiate_decl (fld, /*defer_ok=*/true,
25850 /*expl_inst_class_mem_p=*/true);
25851 }
25852 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25853 {
25854 tree type = TREE_TYPE (fld);
25855
25856 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25857 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25858 do_type_instantiation (type, storage, 0);
25859 }
25860 }
25861
25862 /* Given a function DECL, which is a specialization of TMPL, modify
25863 DECL to be a re-instantiation of TMPL with the same template
25864 arguments. TMPL should be the template into which tsubst'ing
25865 should occur for DECL, not the most general template.
25866
25867 One reason for doing this is a scenario like this:
25868
25869 template <class T>
25870 void f(const T&, int i);
25871
25872 void g() { f(3, 7); }
25873
25874 template <class T>
25875 void f(const T& t, const int i) { }
25876
25877 Note that when the template is first instantiated, with
25878 instantiate_template, the resulting DECL will have no name for the
25879 first parameter, and the wrong type for the second. So, when we go
25880 to instantiate the DECL, we regenerate it. */
25881
25882 static void
25883 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25884 {
25885 /* The arguments used to instantiate DECL, from the most general
25886 template. */
25887 tree code_pattern;
25888
25889 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25890
25891 /* Make sure that we can see identifiers, and compute access
25892 correctly. */
25893 push_access_scope (decl);
25894
25895 if (TREE_CODE (decl) == FUNCTION_DECL)
25896 {
25897 tree specs;
25898 int args_depth;
25899 int parms_depth;
25900
25901 /* Use the source location of the definition. */
25902 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
25903
25904 args_depth = TMPL_ARGS_DEPTH (args);
25905 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25906 if (args_depth > parms_depth)
25907 args = get_innermost_template_args (args, parms_depth);
25908
25909 /* Instantiate a dynamic exception-specification. noexcept will be
25910 handled below. */
25911 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25912 if (TREE_VALUE (raises))
25913 {
25914 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25915 args, tf_error, NULL_TREE,
25916 /*defer_ok*/false);
25917 if (specs && specs != error_mark_node)
25918 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25919 specs);
25920 }
25921
25922 /* Merge parameter declarations. */
25923 if (tree pattern_parm
25924 = skip_artificial_parms_for (code_pattern,
25925 DECL_ARGUMENTS (code_pattern)))
25926 {
25927 tree *p = &DECL_ARGUMENTS (decl);
25928 for (int skip = num_artificial_parms_for (decl); skip; --skip)
25929 p = &DECL_CHAIN (*p);
25930 *p = tsubst_decl (pattern_parm, args, tf_error);
25931 for (tree t = *p; t; t = DECL_CHAIN (t))
25932 DECL_CONTEXT (t) = decl;
25933 }
25934
25935 /* Merge additional specifiers from the CODE_PATTERN. */
25936 if (DECL_DECLARED_INLINE_P (code_pattern)
25937 && !DECL_DECLARED_INLINE_P (decl))
25938 DECL_DECLARED_INLINE_P (decl) = 1;
25939
25940 maybe_instantiate_noexcept (decl, tf_error);
25941 }
25942 else if (VAR_P (decl))
25943 {
25944 start_lambda_scope (decl);
25945 DECL_INITIAL (decl) =
25946 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25947 tf_error, DECL_TI_TEMPLATE (decl));
25948 finish_lambda_scope ();
25949 if (VAR_HAD_UNKNOWN_BOUND (decl))
25950 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25951 tf_error, DECL_TI_TEMPLATE (decl));
25952 }
25953 else
25954 gcc_unreachable ();
25955
25956 pop_access_scope (decl);
25957 }
25958
25959 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25960 substituted to get DECL. */
25961
25962 tree
25963 template_for_substitution (tree decl)
25964 {
25965 tree tmpl = DECL_TI_TEMPLATE (decl);
25966
25967 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25968 for the instantiation. This is not always the most general
25969 template. Consider, for example:
25970
25971 template <class T>
25972 struct S { template <class U> void f();
25973 template <> void f<int>(); };
25974
25975 and an instantiation of S<double>::f<int>. We want TD to be the
25976 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25977 while (/* An instantiation cannot have a definition, so we need a
25978 more general template. */
25979 DECL_TEMPLATE_INSTANTIATION (tmpl)
25980 /* We must also deal with friend templates. Given:
25981
25982 template <class T> struct S {
25983 template <class U> friend void f() {};
25984 };
25985
25986 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25987 so far as the language is concerned, but that's still
25988 where we get the pattern for the instantiation from. On
25989 other hand, if the definition comes outside the class, say:
25990
25991 template <class T> struct S {
25992 template <class U> friend void f();
25993 };
25994 template <class U> friend void f() {}
25995
25996 we don't need to look any further. That's what the check for
25997 DECL_INITIAL is for. */
25998 || (TREE_CODE (decl) == FUNCTION_DECL
25999 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
26000 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
26001 {
26002 /* The present template, TD, should not be a definition. If it
26003 were a definition, we should be using it! Note that we
26004 cannot restructure the loop to just keep going until we find
26005 a template with a definition, since that might go too far if
26006 a specialization was declared, but not defined. */
26007
26008 /* Fetch the more general template. */
26009 tmpl = DECL_TI_TEMPLATE (tmpl);
26010 }
26011
26012 return tmpl;
26013 }
26014
26015 /* Returns true if we need to instantiate this template instance even if we
26016 know we aren't going to emit it. */
26017
26018 bool
26019 always_instantiate_p (tree decl)
26020 {
26021 /* We always instantiate inline functions so that we can inline them. An
26022 explicit instantiation declaration prohibits implicit instantiation of
26023 non-inline functions. With high levels of optimization, we would
26024 normally inline non-inline functions -- but we're not allowed to do
26025 that for "extern template" functions. Therefore, we check
26026 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26027 return ((TREE_CODE (decl) == FUNCTION_DECL
26028 && (DECL_DECLARED_INLINE_P (decl)
26029 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
26030 /* And we need to instantiate static data members so that
26031 their initializers are available in integral constant
26032 expressions. */
26033 || (VAR_P (decl)
26034 && decl_maybe_constant_var_p (decl)));
26035 }
26036
26037 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26038 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26039 error, true otherwise. */
26040
26041 bool
26042 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
26043 {
26044 if (fn == error_mark_node)
26045 return false;
26046
26047 /* Don't instantiate a noexcept-specification from template context. */
26048 if (processing_template_decl
26049 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
26050 return true;
26051
26052 tree fntype = TREE_TYPE (fn);
26053 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
26054
26055 if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
26056 && DECL_MAYBE_DELETED (fn))
26057 {
26058 if (fn == current_function_decl)
26059 /* We're in start_preparsed_function, keep going. */
26060 return true;
26061
26062 ++function_depth;
26063 maybe_synthesize_method (fn);
26064 --function_depth;
26065 return !DECL_DELETED_FN (fn);
26066 }
26067
26068 if (!spec || !TREE_PURPOSE (spec))
26069 return true;
26070
26071 tree noex = TREE_PURPOSE (spec);
26072 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26073 && TREE_CODE (noex) != DEFERRED_PARSE)
26074 return true;
26075
26076 tree orig_fn = NULL_TREE;
26077 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26078 its FUNCTION_DECL for the rest of this function -- push_access_scope
26079 doesn't accept TEMPLATE_DECLs. */
26080 if (DECL_FUNCTION_TEMPLATE_P (fn))
26081 {
26082 orig_fn = fn;
26083 fn = DECL_TEMPLATE_RESULT (fn);
26084 }
26085
26086 if (DECL_CLONED_FUNCTION_P (fn))
26087 {
26088 tree prime = DECL_CLONED_FUNCTION (fn);
26089 if (!maybe_instantiate_noexcept (prime, complain))
26090 return false;
26091 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
26092 }
26093 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
26094 {
26095 static hash_set<tree>* fns = new hash_set<tree>;
26096 bool added = false;
26097 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
26098 {
26099 spec = get_defaulted_eh_spec (fn, complain);
26100 if (spec == error_mark_node)
26101 /* This might have failed because of an unparsed DMI, so
26102 let's try again later. */
26103 return false;
26104 }
26105 else if (!(added = !fns->add (fn)))
26106 {
26107 /* If hash_set::add returns true, the element was already there. */
26108 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
26109 DECL_SOURCE_LOCATION (fn));
26110 error_at (loc,
26111 "exception specification of %qD depends on itself",
26112 fn);
26113 spec = noexcept_false_spec;
26114 }
26115 else if (push_tinst_level (fn))
26116 {
26117 push_to_top_level ();
26118 push_access_scope (fn);
26119 push_deferring_access_checks (dk_no_deferred);
26120 input_location = DECL_SOURCE_LOCATION (fn);
26121
26122 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26123 && !DECL_LOCAL_DECL_P (fn))
26124 {
26125 /* If needed, set current_class_ptr for the benefit of
26126 tsubst_copy/PARM_DECL. */
26127 tree this_parm = DECL_ARGUMENTS (fn);
26128 current_class_ptr = NULL_TREE;
26129 current_class_ref = cp_build_fold_indirect_ref (this_parm);
26130 current_class_ptr = this_parm;
26131 }
26132
26133 /* If this function is represented by a TEMPLATE_DECL, then
26134 the deferred noexcept-specification might still contain
26135 dependent types, even after substitution. And we need the
26136 dependency check functions to work in build_noexcept_spec. */
26137 if (orig_fn)
26138 ++processing_template_decl;
26139
26140 /* Do deferred instantiation of the noexcept-specifier. */
26141 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
26142 DEFERRED_NOEXCEPT_ARGS (noex),
26143 tf_warning_or_error, fn,
26144 /*function_p=*/false,
26145 /*i_c_e_p=*/true);
26146
26147 /* Build up the noexcept-specification. */
26148 spec = build_noexcept_spec (noex, tf_warning_or_error);
26149
26150 if (orig_fn)
26151 --processing_template_decl;
26152
26153 pop_deferring_access_checks ();
26154 pop_access_scope (fn);
26155 pop_tinst_level ();
26156 pop_from_top_level ();
26157 }
26158 else
26159 spec = noexcept_false_spec;
26160
26161 if (added)
26162 fns->remove (fn);
26163 }
26164
26165 if (spec == error_mark_node)
26166 {
26167 /* This failed with a hard error, so let's go with false. */
26168 gcc_assert (seen_error ());
26169 spec = noexcept_false_spec;
26170 }
26171
26172 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
26173 if (orig_fn)
26174 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
26175
26176 return true;
26177 }
26178
26179 /* We're starting to process the function INST, an instantiation of PATTERN;
26180 add their parameters to local_specializations. */
26181
26182 static void
26183 register_parameter_specializations (tree pattern, tree inst)
26184 {
26185 tree tmpl_parm = DECL_ARGUMENTS (pattern);
26186 tree spec_parm = DECL_ARGUMENTS (inst);
26187 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
26188 {
26189 register_local_specialization (spec_parm, tmpl_parm);
26190 spec_parm = skip_artificial_parms_for (inst, spec_parm);
26191 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
26192 }
26193 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
26194 {
26195 if (!DECL_PACK_P (tmpl_parm))
26196 {
26197 register_local_specialization (spec_parm, tmpl_parm);
26198 spec_parm = DECL_CHAIN (spec_parm);
26199 }
26200 else
26201 {
26202 /* Register the (value) argument pack as a specialization of
26203 TMPL_PARM, then move on. */
26204 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
26205 register_local_specialization (argpack, tmpl_parm);
26206 }
26207 }
26208 gcc_assert (!spec_parm);
26209 }
26210
26211 /* Instantiate the body of D using PATTERN with ARGS. We have
26212 already determined PATTERN is the correct template to use.
26213 NESTED_P is true if this is a nested function, in which case
26214 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
26215
26216 static void
26217 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
26218 {
26219 tree td = NULL_TREE;
26220 tree code_pattern = pattern;
26221
26222 if (!nested_p)
26223 {
26224 td = pattern;
26225 code_pattern = DECL_TEMPLATE_RESULT (td);
26226 }
26227 else
26228 /* Only OMP reductions are nested. */
26229 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
26230
26231 vec<tree> omp_privatization_save;
26232 if (current_function_decl)
26233 save_omp_privatization_clauses (omp_privatization_save);
26234
26235 bool push_to_top
26236 = !(current_function_decl
26237 && !LAMBDA_FUNCTION_P (d)
26238 && decl_function_context (d) == current_function_decl);
26239
26240 if (push_to_top)
26241 push_to_top_level ();
26242 else
26243 {
26244 gcc_assert (!processing_template_decl);
26245 push_function_context ();
26246 cp_unevaluated_operand = 0;
26247 c_inhibit_evaluation_warnings = 0;
26248 }
26249
26250 if (VAR_P (d))
26251 {
26252 /* The variable might be a lambda's extra scope, and that
26253 lambda's visibility depends on D's. */
26254 maybe_commonize_var (d);
26255 determine_visibility (d);
26256 }
26257
26258 /* Mark D as instantiated so that recursive calls to
26259 instantiate_decl do not try to instantiate it again. */
26260 DECL_TEMPLATE_INSTANTIATED (d) = 1;
26261
26262 if (td)
26263 /* Regenerate the declaration in case the template has been modified
26264 by a subsequent redeclaration. */
26265 regenerate_decl_from_template (d, td, args);
26266
26267 /* We already set the file and line above. Reset them now in case
26268 they changed as a result of calling regenerate_decl_from_template. */
26269 input_location = DECL_SOURCE_LOCATION (d);
26270
26271 if (VAR_P (d))
26272 {
26273 /* Clear out DECL_RTL; whatever was there before may not be right
26274 since we've reset the type of the declaration. */
26275 SET_DECL_RTL (d, NULL);
26276 DECL_IN_AGGR_P (d) = 0;
26277
26278 /* The initializer is placed in DECL_INITIAL by
26279 regenerate_decl_from_template so we don't need to
26280 push/pop_access_scope again here. Pull it out so that
26281 cp_finish_decl can process it. */
26282 bool const_init = false;
26283 tree init = DECL_INITIAL (d);
26284 DECL_INITIAL (d) = NULL_TREE;
26285 DECL_INITIALIZED_P (d) = 0;
26286
26287 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
26288 initializer. That function will defer actual emission until
26289 we have a chance to determine linkage. */
26290 DECL_EXTERNAL (d) = 0;
26291
26292 /* Enter the scope of D so that access-checking works correctly. */
26293 bool enter_context = DECL_CLASS_SCOPE_P (d);
26294 if (enter_context)
26295 push_nested_class (DECL_CONTEXT (d));
26296
26297 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26298 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
26299
26300 if (enter_context)
26301 pop_nested_class ();
26302 }
26303 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
26304 synthesize_method (d);
26305 else if (TREE_CODE (d) == FUNCTION_DECL)
26306 {
26307 /* Set up the list of local specializations. */
26308 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
26309 tree block = NULL_TREE;
26310
26311 /* Set up context. */
26312 if (nested_p)
26313 block = push_stmt_list ();
26314 else
26315 {
26316 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
26317
26318 perform_instantiation_time_access_checks (code_pattern, args);
26319 }
26320
26321 /* Create substitution entries for the parameters. */
26322 register_parameter_specializations (code_pattern, d);
26323
26324 /* Substitute into the body of the function. */
26325 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
26326 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
26327 tf_warning_or_error, d);
26328 else
26329 {
26330 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
26331 tf_warning_or_error, DECL_TI_TEMPLATE (d),
26332 /*integral_constant_expression_p=*/false);
26333
26334 /* Set the current input_location to the end of the function
26335 so that finish_function knows where we are. */
26336 input_location
26337 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
26338
26339 /* Remember if we saw an infinite loop in the template. */
26340 current_function_infinite_loop
26341 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
26342 }
26343
26344 /* Finish the function. */
26345 if (nested_p)
26346 DECL_SAVED_TREE (d) = pop_stmt_list (block);
26347 else
26348 {
26349 d = finish_function (/*inline_p=*/false);
26350 expand_or_defer_fn (d);
26351 }
26352
26353 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
26354 cp_check_omp_declare_reduction (d);
26355 }
26356
26357 /* We're not deferring instantiation any more. */
26358 if (!nested_p)
26359 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
26360
26361 if (push_to_top)
26362 pop_from_top_level ();
26363 else
26364 pop_function_context ();
26365
26366 if (current_function_decl)
26367 restore_omp_privatization_clauses (omp_privatization_save);
26368 }
26369
26370 /* Produce the definition of D, a _DECL generated from a template. If
26371 DEFER_OK is true, then we don't have to actually do the
26372 instantiation now; we just have to do it sometime. Normally it is
26373 an error if this is an explicit instantiation but D is undefined.
26374 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
26375 instantiated class template. */
26376
26377 tree
26378 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
26379 {
26380 tree tmpl = DECL_TI_TEMPLATE (d);
26381 tree gen_args;
26382 tree args;
26383 tree td;
26384 tree code_pattern;
26385 tree spec;
26386 tree gen_tmpl;
26387 bool pattern_defined;
26388 location_t saved_loc = input_location;
26389 int saved_unevaluated_operand = cp_unevaluated_operand;
26390 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26391 bool external_p;
26392 bool deleted_p;
26393
26394 /* This function should only be used to instantiate templates for
26395 functions and static member variables. */
26396 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
26397
26398 /* A concept is never instantiated. */
26399 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
26400
26401 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
26402
26403 if (modules_p ())
26404 /* We may have a pending instantiation of D itself. */
26405 lazy_load_pendings (d);
26406
26407 /* Variables are never deferred; if instantiation is required, they
26408 are instantiated right away. That allows for better code in the
26409 case that an expression refers to the value of the variable --
26410 if the variable has a constant value the referring expression can
26411 take advantage of that fact. */
26412 if (VAR_P (d))
26413 defer_ok = false;
26414
26415 /* Don't instantiate cloned functions. Instead, instantiate the
26416 functions they cloned. */
26417 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
26418 d = DECL_CLONED_FUNCTION (d);
26419
26420 if (DECL_TEMPLATE_INSTANTIATED (d)
26421 || TREE_TYPE (d) == error_mark_node
26422 || (TREE_CODE (d) == FUNCTION_DECL
26423 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
26424 || DECL_TEMPLATE_SPECIALIZATION (d))
26425 /* D has already been instantiated or explicitly specialized, so
26426 there's nothing for us to do here.
26427
26428 It might seem reasonable to check whether or not D is an explicit
26429 instantiation, and, if so, stop here. But when an explicit
26430 instantiation is deferred until the end of the compilation,
26431 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26432 the instantiation. */
26433 return d;
26434
26435 /* Check to see whether we know that this template will be
26436 instantiated in some other file, as with "extern template"
26437 extension. */
26438 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
26439
26440 /* In general, we do not instantiate such templates. */
26441 if (external_p && !always_instantiate_p (d))
26442 return d;
26443
26444 gen_tmpl = most_general_template (tmpl);
26445 gen_args = DECL_TI_ARGS (d);
26446
26447 /* We should already have the extra args. */
26448 gcc_checking_assert (tmpl == gen_tmpl
26449 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
26450 == TMPL_ARGS_DEPTH (gen_args)));
26451 /* And what's in the hash table should match D. */
26452 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
26453 == d
26454 || spec == NULL_TREE);
26455
26456 /* This needs to happen before any tsubsting. */
26457 if (! push_tinst_level (d))
26458 return d;
26459
26460 timevar_push (TV_TEMPLATE_INST);
26461
26462 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26463 for the instantiation. */
26464 td = template_for_substitution (d);
26465 args = gen_args;
26466
26467 if (variable_template_specialization_p (d))
26468 {
26469 /* Look up an explicit specialization, if any. */
26470 tree elt = most_specialized_partial_spec (d, tf_warning_or_error);
26471 if (elt && elt != error_mark_node)
26472 {
26473 td = TREE_VALUE (elt);
26474 args = TREE_PURPOSE (elt);
26475 }
26476 }
26477
26478 code_pattern = DECL_TEMPLATE_RESULT (td);
26479
26480 /* We should never be trying to instantiate a member of a class
26481 template or partial specialization. */
26482 gcc_assert (d != code_pattern);
26483
26484 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
26485 || DECL_TEMPLATE_SPECIALIZATION (td))
26486 /* In the case of a friend template whose definition is provided
26487 outside the class, we may have too many arguments. Drop the
26488 ones we don't need. The same is true for specializations. */
26489 args = get_innermost_template_args
26490 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
26491
26492 if (TREE_CODE (d) == FUNCTION_DECL)
26493 {
26494 deleted_p = DECL_DELETED_FN (code_pattern);
26495 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
26496 && DECL_INITIAL (code_pattern) != error_mark_node)
26497 || DECL_DEFAULTED_FN (code_pattern)
26498 || deleted_p);
26499 }
26500 else
26501 {
26502 deleted_p = false;
26503 if (DECL_CLASS_SCOPE_P (code_pattern))
26504 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
26505 else
26506 pattern_defined = ! DECL_EXTERNAL (code_pattern);
26507 }
26508
26509 /* We may be in the middle of deferred access check. Disable it now. */
26510 push_deferring_access_checks (dk_no_deferred);
26511
26512 /* Unless an explicit instantiation directive has already determined
26513 the linkage of D, remember that a definition is available for
26514 this entity. */
26515 if (pattern_defined
26516 && !DECL_INTERFACE_KNOWN (d)
26517 && !DECL_NOT_REALLY_EXTERN (d))
26518 mark_definable (d);
26519
26520 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
26521 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
26522 input_location = DECL_SOURCE_LOCATION (d);
26523
26524 /* If D is a member of an explicitly instantiated class template,
26525 and no definition is available, treat it like an implicit
26526 instantiation. */
26527 if (!pattern_defined && expl_inst_class_mem_p
26528 && DECL_EXPLICIT_INSTANTIATION (d))
26529 {
26530 /* Leave linkage flags alone on instantiations with anonymous
26531 visibility. */
26532 if (TREE_PUBLIC (d))
26533 {
26534 DECL_NOT_REALLY_EXTERN (d) = 0;
26535 DECL_INTERFACE_KNOWN (d) = 0;
26536 }
26537 SET_DECL_IMPLICIT_INSTANTIATION (d);
26538 }
26539
26540 /* Defer all other templates, unless we have been explicitly
26541 forbidden from doing so. */
26542 if (/* If there is no definition, we cannot instantiate the
26543 template. */
26544 ! pattern_defined
26545 /* If it's OK to postpone instantiation, do so. */
26546 || defer_ok
26547 /* If this is a static data member that will be defined
26548 elsewhere, we don't want to instantiate the entire data
26549 member, but we do want to instantiate the initializer so that
26550 we can substitute that elsewhere. */
26551 || (external_p && VAR_P (d))
26552 /* Handle here a deleted function too, avoid generating
26553 its body (c++/61080). */
26554 || deleted_p)
26555 {
26556 /* The definition of the static data member is now required so
26557 we must substitute the initializer. */
26558 if (VAR_P (d)
26559 && !DECL_INITIAL (d)
26560 && DECL_INITIAL (code_pattern))
26561 {
26562 tree ns;
26563 tree init;
26564 bool const_init = false;
26565 bool enter_context = DECL_CLASS_SCOPE_P (d);
26566
26567 ns = decl_namespace_context (d);
26568 push_nested_namespace (ns);
26569 if (enter_context)
26570 push_nested_class (DECL_CONTEXT (d));
26571 init = tsubst_expr (DECL_INITIAL (code_pattern),
26572 args,
26573 tf_warning_or_error, NULL_TREE,
26574 /*integral_constant_expression_p=*/false);
26575 /* If instantiating the initializer involved instantiating this
26576 again, don't call cp_finish_decl twice. */
26577 if (!DECL_INITIAL (d))
26578 {
26579 /* Make sure the initializer is still constant, in case of
26580 circular dependency (template/instantiate6.C). */
26581 const_init
26582 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26583 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26584 /*asmspec_tree=*/NULL_TREE,
26585 LOOKUP_ONLYCONVERTING);
26586 }
26587 if (enter_context)
26588 pop_nested_class ();
26589 pop_nested_namespace (ns);
26590 }
26591
26592 /* We restore the source position here because it's used by
26593 add_pending_template. */
26594 input_location = saved_loc;
26595
26596 if (at_eof && !pattern_defined
26597 && DECL_EXPLICIT_INSTANTIATION (d)
26598 && DECL_NOT_REALLY_EXTERN (d))
26599 /* [temp.explicit]
26600
26601 The definition of a non-exported function template, a
26602 non-exported member function template, or a non-exported
26603 member function or static data member of a class template
26604 shall be present in every translation unit in which it is
26605 explicitly instantiated. */
26606 permerror (input_location, "explicit instantiation of %qD "
26607 "but no definition available", d);
26608
26609 /* If we're in unevaluated context, we just wanted to get the
26610 constant value; this isn't an odr use, so don't queue
26611 a full instantiation. */
26612 if (!cp_unevaluated_operand
26613 /* ??? Historically, we have instantiated inline functions, even
26614 when marked as "extern template". */
26615 && !(external_p && VAR_P (d)))
26616 add_pending_template (d);
26617 }
26618 else
26619 {
26620 set_instantiating_module (d);
26621 if (variable_template_p (gen_tmpl))
26622 note_variable_template_instantiation (d);
26623 instantiate_body (td, args, d, false);
26624 }
26625
26626 pop_deferring_access_checks ();
26627 timevar_pop (TV_TEMPLATE_INST);
26628 pop_tinst_level ();
26629 input_location = saved_loc;
26630 cp_unevaluated_operand = saved_unevaluated_operand;
26631 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26632
26633 return d;
26634 }
26635
26636 /* Run through the list of templates that we wish we could
26637 instantiate, and instantiate any we can. RETRIES is the
26638 number of times we retry pending template instantiation. */
26639
26640 void
26641 instantiate_pending_templates (int retries)
26642 {
26643 int reconsider;
26644 location_t saved_loc = input_location;
26645
26646 /* Instantiating templates may trigger vtable generation. This in turn
26647 may require further template instantiations. We place a limit here
26648 to avoid infinite loop. */
26649 if (pending_templates && retries >= max_tinst_depth)
26650 {
26651 tree decl = pending_templates->tinst->maybe_get_node ();
26652
26653 fatal_error (input_location,
26654 "template instantiation depth exceeds maximum of %d"
26655 " instantiating %q+D, possibly from virtual table generation"
26656 " (use %<-ftemplate-depth=%> to increase the maximum)",
26657 max_tinst_depth, decl);
26658 if (TREE_CODE (decl) == FUNCTION_DECL)
26659 /* Pretend that we defined it. */
26660 DECL_INITIAL (decl) = error_mark_node;
26661 return;
26662 }
26663
26664 do
26665 {
26666 struct pending_template **t = &pending_templates;
26667 struct pending_template *last = NULL;
26668 reconsider = 0;
26669 while (*t)
26670 {
26671 tree instantiation = reopen_tinst_level ((*t)->tinst);
26672 bool complete = false;
26673
26674 if (TYPE_P (instantiation))
26675 {
26676 if (!COMPLETE_TYPE_P (instantiation))
26677 {
26678 instantiate_class_template (instantiation);
26679 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26680 for (tree fld = TYPE_FIELDS (instantiation);
26681 fld; fld = TREE_CHAIN (fld))
26682 if ((VAR_P (fld)
26683 || (TREE_CODE (fld) == FUNCTION_DECL
26684 && !DECL_ARTIFICIAL (fld)))
26685 && DECL_TEMPLATE_INSTANTIATION (fld))
26686 instantiate_decl (fld,
26687 /*defer_ok=*/false,
26688 /*expl_inst_class_mem_p=*/false);
26689
26690 if (COMPLETE_TYPE_P (instantiation))
26691 reconsider = 1;
26692 }
26693
26694 complete = COMPLETE_TYPE_P (instantiation);
26695 }
26696 else
26697 {
26698 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26699 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26700 {
26701 instantiation
26702 = instantiate_decl (instantiation,
26703 /*defer_ok=*/false,
26704 /*expl_inst_class_mem_p=*/false);
26705 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26706 reconsider = 1;
26707 }
26708
26709 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26710 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26711 }
26712
26713 if (complete)
26714 {
26715 /* If INSTANTIATION has been instantiated, then we don't
26716 need to consider it again in the future. */
26717 struct pending_template *drop = *t;
26718 *t = (*t)->next;
26719 set_refcount_ptr (drop->tinst);
26720 pending_template_freelist ().free (drop);
26721 }
26722 else
26723 {
26724 last = *t;
26725 t = &(*t)->next;
26726 }
26727 tinst_depth = 0;
26728 set_refcount_ptr (current_tinst_level);
26729 }
26730 last_pending_template = last;
26731 }
26732 while (reconsider);
26733
26734 input_location = saved_loc;
26735 }
26736
26737 /* Substitute ARGVEC into T, which is a list of initializers for
26738 either base class or a non-static data member. The TREE_PURPOSEs
26739 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26740 instantiate_decl. */
26741
26742 static tree
26743 tsubst_initializer_list (tree t, tree argvec)
26744 {
26745 tree inits = NULL_TREE;
26746 tree target_ctor = error_mark_node;
26747
26748 for (; t; t = TREE_CHAIN (t))
26749 {
26750 tree decl;
26751 tree init;
26752 tree expanded_bases = NULL_TREE;
26753 tree expanded_arguments = NULL_TREE;
26754 int i, len = 1;
26755
26756 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26757 {
26758 tree expr;
26759 tree arg;
26760
26761 /* Expand the base class expansion type into separate base
26762 classes. */
26763 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26764 tf_warning_or_error,
26765 NULL_TREE);
26766 if (expanded_bases == error_mark_node)
26767 continue;
26768
26769 /* We'll be building separate TREE_LISTs of arguments for
26770 each base. */
26771 len = TREE_VEC_LENGTH (expanded_bases);
26772 expanded_arguments = make_tree_vec (len);
26773 for (i = 0; i < len; i++)
26774 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26775
26776 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26777 expand each argument in the TREE_VALUE of t. */
26778 expr = make_node (EXPR_PACK_EXPANSION);
26779 PACK_EXPANSION_LOCAL_P (expr) = true;
26780 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26781 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26782
26783 if (TREE_VALUE (t) == void_type_node)
26784 /* VOID_TYPE_NODE is used to indicate
26785 value-initialization. */
26786 {
26787 for (i = 0; i < len; i++)
26788 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26789 }
26790 else
26791 {
26792 /* Substitute parameter packs into each argument in the
26793 TREE_LIST. */
26794 in_base_initializer = 1;
26795 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26796 {
26797 tree expanded_exprs;
26798
26799 /* Expand the argument. */
26800 tree value;
26801 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
26802 value = TREE_VALUE (arg);
26803 else
26804 {
26805 value = expr;
26806 SET_PACK_EXPANSION_PATTERN (value, TREE_VALUE (arg));
26807 }
26808 expanded_exprs
26809 = tsubst_pack_expansion (value, argvec,
26810 tf_warning_or_error,
26811 NULL_TREE);
26812 if (expanded_exprs == error_mark_node)
26813 continue;
26814
26815 /* Prepend each of the expanded expressions to the
26816 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26817 for (i = 0; i < len; i++)
26818 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
26819 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
26820 TREE_VEC_ELT (expanded_arguments, i)
26821 = tree_cons (NULL_TREE,
26822 TREE_VEC_ELT (expanded_exprs, j),
26823 TREE_VEC_ELT (expanded_arguments, i));
26824 else
26825 TREE_VEC_ELT (expanded_arguments, i)
26826 = tree_cons (NULL_TREE,
26827 TREE_VEC_ELT (expanded_exprs, i),
26828 TREE_VEC_ELT (expanded_arguments, i));
26829 }
26830 in_base_initializer = 0;
26831
26832 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26833 since we built them backwards. */
26834 for (i = 0; i < len; i++)
26835 {
26836 TREE_VEC_ELT (expanded_arguments, i) =
26837 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26838 }
26839 }
26840 }
26841
26842 for (i = 0; i < len; ++i)
26843 {
26844 if (expanded_bases)
26845 {
26846 decl = TREE_VEC_ELT (expanded_bases, i);
26847 decl = expand_member_init (decl);
26848 init = TREE_VEC_ELT (expanded_arguments, i);
26849 }
26850 else
26851 {
26852 tree tmp;
26853 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26854 tf_warning_or_error, NULL_TREE);
26855
26856 decl = expand_member_init (decl);
26857 if (decl && !DECL_P (decl))
26858 in_base_initializer = 1;
26859
26860 init = TREE_VALUE (t);
26861 tmp = init;
26862 if (init != void_type_node)
26863 init = tsubst_expr (init, argvec,
26864 tf_warning_or_error, NULL_TREE,
26865 /*integral_constant_expression_p=*/false);
26866 if (init == NULL_TREE && tmp != NULL_TREE)
26867 /* If we had an initializer but it instantiated to nothing,
26868 value-initialize the object. This will only occur when
26869 the initializer was a pack expansion where the parameter
26870 packs used in that expansion were of length zero. */
26871 init = void_type_node;
26872 in_base_initializer = 0;
26873 }
26874
26875 if (target_ctor != error_mark_node
26876 && init != error_mark_node)
26877 {
26878 error ("mem-initializer for %qD follows constructor delegation",
26879 decl);
26880 return inits;
26881 }
26882 /* Look for a target constructor. */
26883 if (init != error_mark_node
26884 && decl && CLASS_TYPE_P (decl)
26885 && same_type_p (decl, current_class_type))
26886 {
26887 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26888 if (inits)
26889 {
26890 error ("constructor delegation follows mem-initializer for %qD",
26891 TREE_PURPOSE (inits));
26892 continue;
26893 }
26894 target_ctor = init;
26895 }
26896
26897 if (decl)
26898 {
26899 init = build_tree_list (decl, init);
26900 /* Carry over the dummy TREE_TYPE node containing the source
26901 location. */
26902 TREE_TYPE (init) = TREE_TYPE (t);
26903 TREE_CHAIN (init) = inits;
26904 inits = init;
26905 }
26906 }
26907 }
26908 return inits;
26909 }
26910
26911 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26912 is the instantiation (which should have been created with
26913 start_enum) and ARGS are the template arguments to use. */
26914
26915 static void
26916 tsubst_enum (tree tag, tree newtag, tree args)
26917 {
26918 tree e;
26919
26920 if (SCOPED_ENUM_P (newtag))
26921 begin_scope (sk_scoped_enum, newtag);
26922
26923 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26924 {
26925 tree value;
26926 tree decl = TREE_VALUE (e);
26927
26928 /* Note that in a template enum, the TREE_VALUE is the
26929 CONST_DECL, not the corresponding INTEGER_CST. */
26930 value = tsubst_expr (DECL_INITIAL (decl),
26931 args, tf_warning_or_error, NULL_TREE,
26932 /*integral_constant_expression_p=*/true);
26933
26934 /* Give this enumeration constant the correct access. */
26935 set_current_access_from_decl (decl);
26936
26937 /* Actually build the enumerator itself. Here we're assuming that
26938 enumerators can't have dependent attributes. */
26939 tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
26940 DECL_ATTRIBUTES (decl),
26941 DECL_SOURCE_LOCATION (decl));
26942 /* Attribute deprecated without an argument isn't sticky: it'll
26943 melt into a tree flag, so we need to propagate the flag here,
26944 since we just created a new enumerator. */
26945 TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
26946 TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
26947 }
26948
26949 if (SCOPED_ENUM_P (newtag))
26950 finish_scope ();
26951
26952 finish_enum_value_list (newtag);
26953 finish_enum (newtag);
26954
26955 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26956 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26957 TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
26958 TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
26959 }
26960
26961 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26962 its type -- but without substituting the innermost set of template
26963 arguments. So, innermost set of template parameters will appear in
26964 the type. */
26965
26966 tree
26967 get_mostly_instantiated_function_type (tree decl)
26968 {
26969 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26970 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26971 }
26972
26973 /* Return truthvalue if we're processing a template different from
26974 the last one involved in diagnostics. */
26975 bool
26976 problematic_instantiation_changed (void)
26977 {
26978 return current_tinst_level != last_error_tinst_level;
26979 }
26980
26981 /* Remember current template involved in diagnostics. */
26982 void
26983 record_last_problematic_instantiation (void)
26984 {
26985 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26986 }
26987
26988 struct tinst_level *
26989 current_instantiation (void)
26990 {
26991 return current_tinst_level;
26992 }
26993
26994 /* Return TRUE if current_function_decl is being instantiated, false
26995 otherwise. */
26996
26997 bool
26998 instantiating_current_function_p (void)
26999 {
27000 return (current_instantiation ()
27001 && (current_instantiation ()->maybe_get_node ()
27002 == current_function_decl));
27003 }
27004
27005 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27006 type. Return false for ok, true for disallowed. Issue error and
27007 inform messages under control of COMPLAIN. */
27008
27009 static bool
27010 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
27011 {
27012 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
27013 return false;
27014 else if (TYPE_PTR_P (type))
27015 return false;
27016 else if (TYPE_REF_P (type)
27017 && !TYPE_REF_IS_RVALUE (type))
27018 return false;
27019 else if (TYPE_PTRMEM_P (type))
27020 return false;
27021 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
27022 {
27023 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
27024 {
27025 if (complain & tf_error)
27026 error ("non-type template parameters of deduced class type only "
27027 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27028 return true;
27029 }
27030 return false;
27031 }
27032 else if (TREE_CODE (type) == NULLPTR_TYPE)
27033 return false;
27034 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
27035 && cxx_dialect < cxx11)
27036 /* Fall through; before C++11 alias templates, a bound ttp
27037 always instantiates into a class type. */;
27038 else if (WILDCARD_TYPE_P (type))
27039 /* Any other wildcard type not already handled above is allowed. */
27040 return false;
27041 else if (TREE_CODE (type) == COMPLEX_TYPE)
27042 /* Fall through. */;
27043 else if (VOID_TYPE_P (type))
27044 /* Fall through. */;
27045 else if (cxx_dialect >= cxx20)
27046 {
27047 if (dependent_type_p (type))
27048 return false;
27049 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
27050 return true;
27051 if (structural_type_p (type))
27052 return false;
27053 if (complain & tf_error)
27054 {
27055 auto_diagnostic_group d;
27056 error ("%qT is not a valid type for a template non-type "
27057 "parameter because it is not structural", type);
27058 structural_type_p (type, true);
27059 }
27060 return true;
27061 }
27062 else if (CLASS_TYPE_P (type))
27063 {
27064 if (complain & tf_error)
27065 error ("non-type template parameters of class type only available "
27066 "with %<-std=c++20%> or %<-std=gnu++20%>");
27067 return true;
27068 }
27069
27070 if (complain & tf_error)
27071 {
27072 if (type == error_mark_node)
27073 inform (input_location, "invalid template non-type parameter");
27074 else
27075 error ("%q#T is not a valid type for a template non-type parameter",
27076 type);
27077 }
27078 return true;
27079 }
27080
27081 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27082
27083 static bool
27084 value_dependent_noexcept_spec_p (tree type)
27085 {
27086 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
27087 if (tree noex = TREE_PURPOSE (spec))
27088 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27089 affect overload resolution and treating it as dependent breaks
27090 things. Same for an unparsed noexcept expression. */
27091 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
27092 && TREE_CODE (noex) != DEFERRED_PARSE
27093 && value_dependent_expression_p (noex))
27094 return true;
27095
27096 return false;
27097 }
27098
27099 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27100 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27101
27102 static bool
27103 dependent_type_p_r (tree type)
27104 {
27105 tree scope;
27106
27107 /* [temp.dep.type]
27108
27109 A type is dependent if it is:
27110
27111 -- a template parameter. Template template parameters are types
27112 for us (since TYPE_P holds true for them) so we handle
27113 them here. */
27114 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27115 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
27116 return true;
27117 /* -- a qualified-id with a nested-name-specifier which contains a
27118 class-name that names a dependent type or whose unqualified-id
27119 names a dependent type. */
27120 if (TREE_CODE (type) == TYPENAME_TYPE)
27121 return true;
27122
27123 /* An alias template specialization can be dependent even if the
27124 resulting type is not. */
27125 if (dependent_alias_template_spec_p (type, nt_transparent))
27126 return true;
27127
27128 /* -- a cv-qualified type where the cv-unqualified type is
27129 dependent.
27130 No code is necessary for this bullet; the code below handles
27131 cv-qualified types, and we don't want to strip aliases with
27132 TYPE_MAIN_VARIANT because of DR 1558. */
27133 /* -- a compound type constructed from any dependent type. */
27134 if (TYPE_PTRMEM_P (type))
27135 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
27136 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27137 (type)));
27138 else if (INDIRECT_TYPE_P (type))
27139 return dependent_type_p (TREE_TYPE (type));
27140 else if (FUNC_OR_METHOD_TYPE_P (type))
27141 {
27142 tree arg_type;
27143
27144 if (dependent_type_p (TREE_TYPE (type)))
27145 return true;
27146 for (arg_type = TYPE_ARG_TYPES (type);
27147 arg_type;
27148 arg_type = TREE_CHAIN (arg_type))
27149 if (dependent_type_p (TREE_VALUE (arg_type)))
27150 return true;
27151 if (cxx_dialect >= cxx17
27152 && value_dependent_noexcept_spec_p (type))
27153 /* A value-dependent noexcept-specifier makes the type dependent. */
27154 return true;
27155 return false;
27156 }
27157 /* -- an array type constructed from any dependent type or whose
27158 size is specified by a constant expression that is
27159 value-dependent.
27160
27161 We checked for type- and value-dependence of the bounds in
27162 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27163 if (TREE_CODE (type) == ARRAY_TYPE)
27164 {
27165 if (TYPE_DOMAIN (type)
27166 && dependent_type_p (TYPE_DOMAIN (type)))
27167 return true;
27168 return dependent_type_p (TREE_TYPE (type));
27169 }
27170
27171 /* -- a template-id in which either the template name is a template
27172 parameter ... */
27173 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
27174 return true;
27175 /* ... or any of the template arguments is a dependent type or
27176 an expression that is type-dependent or value-dependent. */
27177 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
27178 && (any_dependent_template_arguments_p
27179 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
27180 return true;
27181
27182 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
27183 dependent; if the argument of the `typeof' expression is not
27184 type-dependent, then it should already been have resolved. */
27185 if (TREE_CODE (type) == TYPEOF_TYPE
27186 || TREE_CODE (type) == DECLTYPE_TYPE
27187 || TREE_CODE (type) == UNDERLYING_TYPE)
27188 return true;
27189
27190 /* A template argument pack is dependent if any of its packed
27191 arguments are. */
27192 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
27193 {
27194 tree args = ARGUMENT_PACK_ARGS (type);
27195 int i, len = TREE_VEC_LENGTH (args);
27196 for (i = 0; i < len; ++i)
27197 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27198 return true;
27199 }
27200
27201 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27202 be template parameters. */
27203 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
27204 return true;
27205
27206 if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
27207 return true;
27208
27209 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
27210 return true;
27211
27212 /* The standard does not specifically mention types that are local
27213 to template functions or local classes, but they should be
27214 considered dependent too. For example:
27215
27216 template <int I> void f() {
27217 enum E { a = I };
27218 S<sizeof (E)> s;
27219 }
27220
27221 The size of `E' cannot be known until the value of `I' has been
27222 determined. Therefore, `E' must be considered dependent. */
27223 scope = TYPE_CONTEXT (type);
27224 if (scope && TYPE_P (scope))
27225 return dependent_type_p (scope);
27226 /* Don't use type_dependent_expression_p here, as it can lead
27227 to infinite recursion trying to determine whether a lambda
27228 nested in a lambda is dependent (c++/47687). */
27229 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
27230 && DECL_LANG_SPECIFIC (scope)
27231 && DECL_TEMPLATE_INFO (scope)
27232 && (any_dependent_template_arguments_p
27233 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
27234 return true;
27235
27236 /* Other types are non-dependent. */
27237 return false;
27238 }
27239
27240 /* Returns TRUE if TYPE is dependent, in the sense of
27241 [temp.dep.type]. Note that a NULL type is considered dependent. */
27242
27243 bool
27244 dependent_type_p (tree type)
27245 {
27246 /* If there are no template parameters in scope, then there can't be
27247 any dependent types. */
27248 if (!processing_template_decl)
27249 {
27250 /* If we are not processing a template, then nobody should be
27251 providing us with a dependent type. */
27252 gcc_assert (type);
27253 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
27254 return false;
27255 }
27256
27257 /* If the type is NULL, we have not computed a type for the entity
27258 in question; in that case, the type is dependent. */
27259 if (!type)
27260 return true;
27261
27262 /* Erroneous types can be considered non-dependent. */
27263 if (type == error_mark_node)
27264 return false;
27265
27266 /* If we have not already computed the appropriate value for TYPE,
27267 do so now. */
27268 if (!TYPE_DEPENDENT_P_VALID (type))
27269 {
27270 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
27271 TYPE_DEPENDENT_P_VALID (type) = 1;
27272 }
27273
27274 return TYPE_DEPENDENT_P (type);
27275 }
27276
27277 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
27278 lookup. In other words, a dependent type that is not the current
27279 instantiation. */
27280
27281 bool
27282 dependent_scope_p (tree scope)
27283 {
27284 return (scope && TYPE_P (scope) && dependent_type_p (scope)
27285 && !currently_open_class (scope));
27286 }
27287
27288 /* True if we might find more declarations in SCOPE during instantiation than
27289 we can when parsing the template. */
27290
27291 bool
27292 dependentish_scope_p (tree scope)
27293 {
27294 return dependent_scope_p (scope) || any_dependent_bases_p (scope);
27295 }
27296
27297 /* T is a SCOPE_REF. Return whether it represents a non-static member of
27298 an unknown base of 'this' (and is therefore instantiation-dependent). */
27299
27300 static bool
27301 unknown_base_ref_p (tree t)
27302 {
27303 if (!current_class_ptr)
27304 return false;
27305
27306 tree mem = TREE_OPERAND (t, 1);
27307 if (shared_member_p (mem))
27308 return false;
27309
27310 tree cur = current_nonlambda_class_type ();
27311 if (!any_dependent_bases_p (cur))
27312 return false;
27313
27314 tree ctx = TREE_OPERAND (t, 0);
27315 if (DERIVED_FROM_P (ctx, cur))
27316 return false;
27317
27318 return true;
27319 }
27320
27321 /* T is a SCOPE_REF; return whether we need to consider it
27322 instantiation-dependent so that we can check access at instantiation
27323 time even though we know which member it resolves to. */
27324
27325 static bool
27326 instantiation_dependent_scope_ref_p (tree t)
27327 {
27328 if (DECL_P (TREE_OPERAND (t, 1))
27329 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
27330 && !unknown_base_ref_p (t)
27331 && accessible_in_template_p (TREE_OPERAND (t, 0),
27332 TREE_OPERAND (t, 1)))
27333 return false;
27334 else
27335 return true;
27336 }
27337
27338 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
27339 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
27340 expression. */
27341
27342 /* Note that this predicate is not appropriate for general expressions;
27343 only constant expressions (that satisfy potential_constant_expression)
27344 can be tested for value dependence. */
27345
27346 bool
27347 value_dependent_expression_p (tree expression)
27348 {
27349 if (!processing_template_decl || expression == NULL_TREE)
27350 return false;
27351
27352 /* A type-dependent expression is also value-dependent. */
27353 if (type_dependent_expression_p (expression))
27354 return true;
27355
27356 switch (TREE_CODE (expression))
27357 {
27358 case BASELINK:
27359 /* A dependent member function of the current instantiation. */
27360 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
27361
27362 case FUNCTION_DECL:
27363 /* A dependent member function of the current instantiation. */
27364 if (DECL_CLASS_SCOPE_P (expression)
27365 && dependent_type_p (DECL_CONTEXT (expression)))
27366 return true;
27367 break;
27368
27369 case IDENTIFIER_NODE:
27370 /* A name that has not been looked up -- must be dependent. */
27371 return true;
27372
27373 case TEMPLATE_PARM_INDEX:
27374 /* A non-type template parm. */
27375 return true;
27376
27377 case CONST_DECL:
27378 /* A non-type template parm. */
27379 if (DECL_TEMPLATE_PARM_P (expression))
27380 return true;
27381 return value_dependent_expression_p (DECL_INITIAL (expression));
27382
27383 case VAR_DECL:
27384 /* A constant with literal type and is initialized
27385 with an expression that is value-dependent. */
27386 if (DECL_DEPENDENT_INIT_P (expression)
27387 /* FIXME cp_finish_decl doesn't fold reference initializers. */
27388 || TYPE_REF_P (TREE_TYPE (expression)))
27389 return true;
27390 if (DECL_HAS_VALUE_EXPR_P (expression))
27391 {
27392 tree value_expr = DECL_VALUE_EXPR (expression);
27393 if (value_dependent_expression_p (value_expr)
27394 /* __PRETTY_FUNCTION__ inside a template function is dependent
27395 on the name of the function. */
27396 || (DECL_PRETTY_FUNCTION_P (expression)
27397 /* It might be used in a template, but not a template
27398 function, in which case its DECL_VALUE_EXPR will be
27399 "top level". */
27400 && value_expr == error_mark_node))
27401 return true;
27402 }
27403 return false;
27404
27405 case DYNAMIC_CAST_EXPR:
27406 case STATIC_CAST_EXPR:
27407 case CONST_CAST_EXPR:
27408 case REINTERPRET_CAST_EXPR:
27409 case CAST_EXPR:
27410 case IMPLICIT_CONV_EXPR:
27411 /* These expressions are value-dependent if the type to which
27412 the cast occurs is dependent or the expression being casted
27413 is value-dependent. */
27414 {
27415 tree type = TREE_TYPE (expression);
27416
27417 if (dependent_type_p (type))
27418 return true;
27419
27420 /* A functional cast has a list of operands. */
27421 expression = TREE_OPERAND (expression, 0);
27422 if (!expression)
27423 {
27424 /* If there are no operands, it must be an expression such
27425 as "int()". This should not happen for aggregate types
27426 because it would form non-constant expressions. */
27427 gcc_assert (cxx_dialect >= cxx11
27428 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
27429
27430 return false;
27431 }
27432
27433 if (TREE_CODE (expression) == TREE_LIST)
27434 return any_value_dependent_elements_p (expression);
27435
27436 return value_dependent_expression_p (expression);
27437 }
27438
27439 case SIZEOF_EXPR:
27440 if (SIZEOF_EXPR_TYPE_P (expression))
27441 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
27442 /* FALLTHRU */
27443 case ALIGNOF_EXPR:
27444 case TYPEID_EXPR:
27445 /* A `sizeof' expression is value-dependent if the operand is
27446 type-dependent or is a pack expansion. */
27447 expression = TREE_OPERAND (expression, 0);
27448 if (PACK_EXPANSION_P (expression))
27449 return true;
27450 else if (TYPE_P (expression))
27451 return dependent_type_p (expression);
27452 return instantiation_dependent_uneval_expression_p (expression);
27453
27454 case AT_ENCODE_EXPR:
27455 /* An 'encode' expression is value-dependent if the operand is
27456 type-dependent. */
27457 expression = TREE_OPERAND (expression, 0);
27458 return dependent_type_p (expression);
27459
27460 case NOEXCEPT_EXPR:
27461 expression = TREE_OPERAND (expression, 0);
27462 return instantiation_dependent_uneval_expression_p (expression);
27463
27464 case SCOPE_REF:
27465 /* All instantiation-dependent expressions should also be considered
27466 value-dependent. */
27467 return instantiation_dependent_scope_ref_p (expression);
27468
27469 case COMPONENT_REF:
27470 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
27471 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
27472
27473 case NONTYPE_ARGUMENT_PACK:
27474 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27475 is value-dependent. */
27476 {
27477 tree values = ARGUMENT_PACK_ARGS (expression);
27478 int i, len = TREE_VEC_LENGTH (values);
27479
27480 for (i = 0; i < len; ++i)
27481 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
27482 return true;
27483
27484 return false;
27485 }
27486
27487 case TRAIT_EXPR:
27488 {
27489 tree type2 = TRAIT_EXPR_TYPE2 (expression);
27490
27491 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
27492 return true;
27493
27494 if (!type2)
27495 return false;
27496
27497 if (TREE_CODE (type2) != TREE_LIST)
27498 return dependent_type_p (type2);
27499
27500 for (; type2; type2 = TREE_CHAIN (type2))
27501 if (dependent_type_p (TREE_VALUE (type2)))
27502 return true;
27503
27504 return false;
27505 }
27506
27507 case MODOP_EXPR:
27508 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27509 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
27510
27511 case ARRAY_REF:
27512 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27513 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
27514
27515 case ADDR_EXPR:
27516 {
27517 tree op = TREE_OPERAND (expression, 0);
27518 return (value_dependent_expression_p (op)
27519 || has_value_dependent_address (op));
27520 }
27521
27522 case REQUIRES_EXPR:
27523 /* Treat all requires-expressions as value-dependent so
27524 we don't try to fold them. */
27525 return true;
27526
27527 case TYPE_REQ:
27528 return dependent_type_p (TREE_OPERAND (expression, 0));
27529
27530 case CALL_EXPR:
27531 {
27532 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
27533 return true;
27534 tree fn = get_callee_fndecl (expression);
27535 int i, nargs;
27536 nargs = call_expr_nargs (expression);
27537 for (i = 0; i < nargs; ++i)
27538 {
27539 tree op = CALL_EXPR_ARG (expression, i);
27540 /* In a call to a constexpr member function, look through the
27541 implicit ADDR_EXPR on the object argument so that it doesn't
27542 cause the call to be considered value-dependent. We also
27543 look through it in potential_constant_expression. */
27544 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
27545 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
27546 && TREE_CODE (op) == ADDR_EXPR)
27547 op = TREE_OPERAND (op, 0);
27548 if (value_dependent_expression_p (op))
27549 return true;
27550 }
27551 return false;
27552 }
27553
27554 case TEMPLATE_ID_EXPR:
27555 return concept_definition_p (TREE_OPERAND (expression, 0))
27556 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
27557
27558 case CONSTRUCTOR:
27559 {
27560 unsigned ix;
27561 tree val;
27562 if (dependent_type_p (TREE_TYPE (expression)))
27563 return true;
27564 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
27565 if (value_dependent_expression_p (val))
27566 return true;
27567 return false;
27568 }
27569
27570 case STMT_EXPR:
27571 /* Treat a GNU statement expression as dependent to avoid crashing
27572 under instantiate_non_dependent_expr; it can't be constant. */
27573 return true;
27574
27575 default:
27576 /* A constant expression is value-dependent if any subexpression is
27577 value-dependent. */
27578 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
27579 {
27580 case tcc_reference:
27581 case tcc_unary:
27582 case tcc_comparison:
27583 case tcc_binary:
27584 case tcc_expression:
27585 case tcc_vl_exp:
27586 {
27587 int i, len = cp_tree_operand_length (expression);
27588
27589 for (i = 0; i < len; i++)
27590 {
27591 tree t = TREE_OPERAND (expression, i);
27592
27593 /* In some cases, some of the operands may be missing.
27594 (For example, in the case of PREDECREMENT_EXPR, the
27595 amount to increment by may be missing.) That doesn't
27596 make the expression dependent. */
27597 if (t && value_dependent_expression_p (t))
27598 return true;
27599 }
27600 }
27601 break;
27602 default:
27603 break;
27604 }
27605 break;
27606 }
27607
27608 /* The expression is not value-dependent. */
27609 return false;
27610 }
27611
27612 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27613 [temp.dep.expr]. Note that an expression with no type is
27614 considered dependent. Other parts of the compiler arrange for an
27615 expression with type-dependent subexpressions to have no type, so
27616 this function doesn't have to be fully recursive. */
27617
27618 bool
27619 type_dependent_expression_p (tree expression)
27620 {
27621 if (!processing_template_decl)
27622 return false;
27623
27624 if (expression == NULL_TREE || expression == error_mark_node)
27625 return false;
27626
27627 STRIP_ANY_LOCATION_WRAPPER (expression);
27628
27629 /* An unresolved name is always dependent. */
27630 if (identifier_p (expression)
27631 || TREE_CODE (expression) == USING_DECL
27632 || TREE_CODE (expression) == WILDCARD_DECL)
27633 return true;
27634
27635 /* A lambda-expression in template context is dependent. dependent_type_p is
27636 true for a lambda in the scope of a class or function template, but that
27637 doesn't cover all template contexts, like a default template argument. */
27638 if (TREE_CODE (expression) == LAMBDA_EXPR)
27639 return true;
27640
27641 /* A fold expression is type-dependent. */
27642 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27643 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27644 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27645 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27646 return true;
27647
27648 /* Some expression forms are never type-dependent. */
27649 if (TREE_CODE (expression) == SIZEOF_EXPR
27650 || TREE_CODE (expression) == ALIGNOF_EXPR
27651 || TREE_CODE (expression) == AT_ENCODE_EXPR
27652 || TREE_CODE (expression) == NOEXCEPT_EXPR
27653 || TREE_CODE (expression) == TRAIT_EXPR
27654 || TREE_CODE (expression) == TYPEID_EXPR
27655 || TREE_CODE (expression) == DELETE_EXPR
27656 || TREE_CODE (expression) == VEC_DELETE_EXPR
27657 || TREE_CODE (expression) == THROW_EXPR
27658 || TREE_CODE (expression) == REQUIRES_EXPR)
27659 return false;
27660
27661 /* The types of these expressions depends only on the type to which
27662 the cast occurs. */
27663 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27664 || TREE_CODE (expression) == STATIC_CAST_EXPR
27665 || TREE_CODE (expression) == CONST_CAST_EXPR
27666 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27667 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27668 || TREE_CODE (expression) == CAST_EXPR)
27669 return dependent_type_p (TREE_TYPE (expression));
27670
27671 /* The types of these expressions depends only on the type created
27672 by the expression. */
27673 if (TREE_CODE (expression) == NEW_EXPR
27674 || TREE_CODE (expression) == VEC_NEW_EXPR)
27675 {
27676 /* For NEW_EXPR tree nodes created inside a template, either
27677 the object type itself or a TREE_LIST may appear as the
27678 operand 1. */
27679 tree type = TREE_OPERAND (expression, 1);
27680 if (TREE_CODE (type) == TREE_LIST)
27681 /* This is an array type. We need to check array dimensions
27682 as well. */
27683 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27684 || value_dependent_expression_p
27685 (TREE_OPERAND (TREE_VALUE (type), 1));
27686 /* Array type whose dimension has to be deduced. */
27687 else if (TREE_CODE (type) == ARRAY_TYPE
27688 && TREE_OPERAND (expression, 2) == NULL_TREE)
27689 return true;
27690 else
27691 return dependent_type_p (type);
27692 }
27693
27694 if (TREE_CODE (expression) == SCOPE_REF)
27695 {
27696 tree scope = TREE_OPERAND (expression, 0);
27697 tree name = TREE_OPERAND (expression, 1);
27698
27699 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27700 contains an identifier associated by name lookup with one or more
27701 declarations declared with a dependent type, or...a
27702 nested-name-specifier or qualified-id that names a member of an
27703 unknown specialization. */
27704 return (type_dependent_expression_p (name)
27705 || dependent_scope_p (scope));
27706 }
27707
27708 if (TREE_CODE (expression) == TEMPLATE_DECL
27709 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27710 return uses_outer_template_parms (expression);
27711
27712 if (TREE_CODE (expression) == STMT_EXPR)
27713 expression = stmt_expr_value_expr (expression);
27714
27715 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27716 {
27717 for (auto &elt : CONSTRUCTOR_ELTS (expression))
27718 if (type_dependent_expression_p (elt.value))
27719 return true;
27720 return false;
27721 }
27722
27723 /* A static data member of the current instantiation with incomplete
27724 array type is type-dependent, as the definition and specializations
27725 can have different bounds. */
27726 if (VAR_P (expression)
27727 && DECL_CLASS_SCOPE_P (expression)
27728 && dependent_type_p (DECL_CONTEXT (expression))
27729 && VAR_HAD_UNKNOWN_BOUND (expression))
27730 return true;
27731
27732 /* An array of unknown bound depending on a variadic parameter, eg:
27733
27734 template<typename... Args>
27735 void foo (Args... args)
27736 {
27737 int arr[] = { args... };
27738 }
27739
27740 template<int... vals>
27741 void bar ()
27742 {
27743 int arr[] = { vals... };
27744 }
27745
27746 If the array has no length and has an initializer, it must be that
27747 we couldn't determine its length in cp_complete_array_type because
27748 it is dependent. */
27749 if (VAR_P (expression)
27750 && TREE_TYPE (expression) != NULL_TREE
27751 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27752 && !TYPE_DOMAIN (TREE_TYPE (expression))
27753 && DECL_INITIAL (expression))
27754 return true;
27755
27756 /* A function or variable template-id is type-dependent if it has any
27757 dependent template arguments. */
27758 if (VAR_OR_FUNCTION_DECL_P (expression)
27759 && DECL_LANG_SPECIFIC (expression)
27760 && DECL_TEMPLATE_INFO (expression))
27761 {
27762 /* Consider the innermost template arguments, since those are the ones
27763 that come from the template-id; the template arguments for the
27764 enclosing class do not make it type-dependent unless they are used in
27765 the type of the decl. */
27766 if (instantiates_primary_template_p (expression)
27767 && (any_dependent_template_arguments_p
27768 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27769 return true;
27770 }
27771
27772 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27773 type-dependent. Checking this is important for functions with auto return
27774 type, which looks like a dependent type. */
27775 if (TREE_CODE (expression) == FUNCTION_DECL
27776 && !(DECL_CLASS_SCOPE_P (expression)
27777 && dependent_type_p (DECL_CONTEXT (expression)))
27778 && !(DECL_LANG_SPECIFIC (expression)
27779 && DECL_UNIQUE_FRIEND_P (expression)
27780 && (!DECL_FRIEND_CONTEXT (expression)
27781 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27782 && !DECL_LOCAL_DECL_P (expression))
27783 {
27784 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27785 || undeduced_auto_decl (expression));
27786 return false;
27787 }
27788
27789 /* Always dependent, on the number of arguments if nothing else. */
27790 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27791 return true;
27792
27793 if (TREE_TYPE (expression) == unknown_type_node)
27794 {
27795 if (TREE_CODE (expression) == ADDR_EXPR)
27796 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27797 if (TREE_CODE (expression) == COMPONENT_REF
27798 || TREE_CODE (expression) == OFFSET_REF)
27799 {
27800 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27801 return true;
27802 expression = TREE_OPERAND (expression, 1);
27803 if (identifier_p (expression))
27804 return false;
27805 }
27806 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27807 if (TREE_CODE (expression) == SCOPE_REF)
27808 return false;
27809
27810 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27811 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27812 || TREE_CODE (expression) == CO_YIELD_EXPR)
27813 return true;
27814
27815 if (BASELINK_P (expression))
27816 {
27817 if (BASELINK_OPTYPE (expression)
27818 && dependent_type_p (BASELINK_OPTYPE (expression)))
27819 return true;
27820 expression = BASELINK_FUNCTIONS (expression);
27821 }
27822
27823 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27824 {
27825 if (any_dependent_template_arguments_p
27826 (TREE_OPERAND (expression, 1)))
27827 return true;
27828 expression = TREE_OPERAND (expression, 0);
27829 if (identifier_p (expression))
27830 return true;
27831 }
27832
27833 gcc_assert (OVL_P (expression));
27834
27835 for (lkp_iterator iter (expression); iter; ++iter)
27836 if (type_dependent_expression_p (*iter))
27837 return true;
27838
27839 return false;
27840 }
27841
27842 /* The type of a non-type template parm declared with a placeholder type
27843 depends on the corresponding template argument, even though
27844 placeholders are not normally considered dependent. */
27845 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27846 && is_auto (TREE_TYPE (expression)))
27847 return true;
27848
27849 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27850
27851 /* Dependent type attributes might not have made it from the decl to
27852 the type yet. */
27853 if (DECL_P (expression)
27854 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27855 return true;
27856
27857 return (dependent_type_p (TREE_TYPE (expression)));
27858 }
27859
27860 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27861 type-dependent if the expression refers to a member of the current
27862 instantiation and the type of the referenced member is dependent, or the
27863 class member access expression refers to a member of an unknown
27864 specialization.
27865
27866 This function returns true if the OBJECT in such a class member access
27867 expression is of an unknown specialization. */
27868
27869 bool
27870 type_dependent_object_expression_p (tree object)
27871 {
27872 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27873 dependent. */
27874 if (TREE_CODE (object) == IDENTIFIER_NODE)
27875 return true;
27876 tree scope = TREE_TYPE (object);
27877 return (!scope || dependent_scope_p (scope));
27878 }
27879
27880 /* walk_tree callback function for instantiation_dependent_expression_p,
27881 below. Returns non-zero if a dependent subexpression is found. */
27882
27883 static tree
27884 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27885 void * /*data*/)
27886 {
27887 if (TYPE_P (*tp))
27888 {
27889 /* We don't have to worry about decltype currently because decltype
27890 of an instantiation-dependent expr is a dependent type. This
27891 might change depending on the resolution of DR 1172. */
27892 *walk_subtrees = false;
27893 return NULL_TREE;
27894 }
27895 enum tree_code code = TREE_CODE (*tp);
27896 switch (code)
27897 {
27898 /* Don't treat an argument list as dependent just because it has no
27899 TREE_TYPE. */
27900 case TREE_LIST:
27901 case TREE_VEC:
27902 case NONTYPE_ARGUMENT_PACK:
27903 return NULL_TREE;
27904
27905 case TEMPLATE_PARM_INDEX:
27906 if (dependent_type_p (TREE_TYPE (*tp)))
27907 return *tp;
27908 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27909 return *tp;
27910 /* We'll check value-dependence separately. */
27911 return NULL_TREE;
27912
27913 /* Handle expressions with type operands. */
27914 case SIZEOF_EXPR:
27915 case ALIGNOF_EXPR:
27916 case TYPEID_EXPR:
27917 case AT_ENCODE_EXPR:
27918 {
27919 tree op = TREE_OPERAND (*tp, 0);
27920 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27921 op = TREE_TYPE (op);
27922 if (TYPE_P (op))
27923 {
27924 if (dependent_type_p (op))
27925 return *tp;
27926 else
27927 {
27928 *walk_subtrees = false;
27929 return NULL_TREE;
27930 }
27931 }
27932 break;
27933 }
27934
27935 case COMPONENT_REF:
27936 if (identifier_p (TREE_OPERAND (*tp, 1)))
27937 /* In a template, finish_class_member_access_expr creates a
27938 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27939 type-dependent, so that we can check access control at
27940 instantiation time (PR 42277). See also Core issue 1273. */
27941 return *tp;
27942 break;
27943
27944 case SCOPE_REF:
27945 if (instantiation_dependent_scope_ref_p (*tp))
27946 return *tp;
27947 else
27948 break;
27949
27950 /* Treat statement-expressions as dependent. */
27951 case BIND_EXPR:
27952 return *tp;
27953
27954 /* Treat requires-expressions as dependent. */
27955 case REQUIRES_EXPR:
27956 return *tp;
27957
27958 case CONSTRUCTOR:
27959 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27960 return *tp;
27961 break;
27962
27963 case TEMPLATE_DECL:
27964 case FUNCTION_DECL:
27965 /* Before C++17, a noexcept-specifier isn't part of the function type
27966 so it doesn't affect type dependence, but we still want to consider it
27967 for instantiation dependence. */
27968 if (cxx_dialect < cxx17
27969 && DECL_DECLARES_FUNCTION_P (*tp)
27970 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
27971 return *tp;
27972 break;
27973
27974 default:
27975 break;
27976 }
27977
27978 if (type_dependent_expression_p (*tp))
27979 return *tp;
27980 else
27981 return NULL_TREE;
27982 }
27983
27984 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27985 sense defined by the ABI:
27986
27987 "An expression is instantiation-dependent if it is type-dependent
27988 or value-dependent, or it has a subexpression that is type-dependent
27989 or value-dependent."
27990
27991 Except don't actually check value-dependence for unevaluated expressions,
27992 because in sizeof(i) we don't care about the value of i. Checking
27993 type-dependence will in turn check value-dependence of array bounds/template
27994 arguments as needed. */
27995
27996 bool
27997 instantiation_dependent_uneval_expression_p (tree expression)
27998 {
27999 tree result;
28000
28001 if (!processing_template_decl)
28002 return false;
28003
28004 if (expression == error_mark_node)
28005 return false;
28006
28007 result = cp_walk_tree_without_duplicates (&expression,
28008 instantiation_dependent_r, NULL);
28009 return result != NULL_TREE;
28010 }
28011
28012 /* As above, but also check value-dependence of the expression as a whole. */
28013
28014 bool
28015 instantiation_dependent_expression_p (tree expression)
28016 {
28017 return (instantiation_dependent_uneval_expression_p (expression)
28018 || (processing_template_decl
28019 && potential_constant_expression (expression)
28020 && value_dependent_expression_p (expression)));
28021 }
28022
28023 /* Like type_dependent_expression_p, but it also works while not processing
28024 a template definition, i.e. during substitution or mangling. */
28025
28026 bool
28027 type_dependent_expression_p_push (tree expr)
28028 {
28029 bool b;
28030 ++processing_template_decl;
28031 b = type_dependent_expression_p (expr);
28032 --processing_template_decl;
28033 return b;
28034 }
28035
28036 /* Returns TRUE if ARGS contains a type-dependent expression. */
28037
28038 bool
28039 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
28040 {
28041 unsigned int i;
28042 tree arg;
28043
28044 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
28045 {
28046 if (type_dependent_expression_p (arg))
28047 return true;
28048 }
28049 return false;
28050 }
28051
28052 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28053 expressions) contains any type-dependent expressions. */
28054
28055 bool
28056 any_type_dependent_elements_p (const_tree list)
28057 {
28058 for (; list; list = TREE_CHAIN (list))
28059 if (type_dependent_expression_p (TREE_VALUE (list)))
28060 return true;
28061
28062 return false;
28063 }
28064
28065 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28066 expressions) contains any value-dependent expressions. */
28067
28068 bool
28069 any_value_dependent_elements_p (const_tree list)
28070 {
28071 for (; list; list = TREE_CHAIN (list))
28072 if (value_dependent_expression_p (TREE_VALUE (list)))
28073 return true;
28074
28075 return false;
28076 }
28077
28078 /* Returns TRUE if the ARG (a template argument) is dependent. */
28079
28080 bool
28081 dependent_template_arg_p (tree arg)
28082 {
28083 if (!processing_template_decl)
28084 return false;
28085
28086 /* Assume a template argument that was wrongly written by the user
28087 is dependent. This is consistent with what
28088 any_dependent_template_arguments_p [that calls this function]
28089 does. */
28090 if (!arg || arg == error_mark_node)
28091 return true;
28092
28093 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
28094 arg = argument_pack_select_arg (arg);
28095
28096 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
28097 return true;
28098 if (TREE_CODE (arg) == TEMPLATE_DECL)
28099 {
28100 if (DECL_TEMPLATE_PARM_P (arg))
28101 return true;
28102 /* A member template of a dependent class is not necessarily
28103 type-dependent, but it is a dependent template argument because it
28104 will be a member of an unknown specialization to that template. */
28105 tree scope = CP_DECL_CONTEXT (arg);
28106 return TYPE_P (scope) && dependent_type_p (scope);
28107 }
28108 else if (ARGUMENT_PACK_P (arg))
28109 {
28110 tree args = ARGUMENT_PACK_ARGS (arg);
28111 int i, len = TREE_VEC_LENGTH (args);
28112 for (i = 0; i < len; ++i)
28113 {
28114 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
28115 return true;
28116 }
28117
28118 return false;
28119 }
28120 else if (TYPE_P (arg))
28121 return dependent_type_p (arg);
28122 else
28123 return value_dependent_expression_p (arg);
28124 }
28125
28126 /* Identify any expressions that use function parms. */
28127
28128 static tree
28129 find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
28130 {
28131 tree t = *tp;
28132 if (TREE_CODE (t) == PARM_DECL)
28133 {
28134 *walk_subtrees = 0;
28135 return t;
28136 }
28137 return NULL_TREE;
28138 }
28139
28140 /* Returns true if ARGS (a collection of template arguments) contains
28141 any types that require structural equality testing. */
28142
28143 bool
28144 any_template_arguments_need_structural_equality_p (tree args)
28145 {
28146 int i;
28147 int j;
28148
28149 if (!args)
28150 return false;
28151 if (args == error_mark_node)
28152 return true;
28153
28154 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28155 {
28156 tree level = TMPL_ARGS_LEVEL (args, i + 1);
28157 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28158 {
28159 tree arg = TREE_VEC_ELT (level, j);
28160 tree packed_args = NULL_TREE;
28161 int k, len = 1;
28162
28163 if (ARGUMENT_PACK_P (arg))
28164 {
28165 /* Look inside the argument pack. */
28166 packed_args = ARGUMENT_PACK_ARGS (arg);
28167 len = TREE_VEC_LENGTH (packed_args);
28168 }
28169
28170 for (k = 0; k < len; ++k)
28171 {
28172 if (packed_args)
28173 arg = TREE_VEC_ELT (packed_args, k);
28174
28175 if (error_operand_p (arg))
28176 return true;
28177 else if (TREE_CODE (arg) == TEMPLATE_DECL)
28178 continue;
28179 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
28180 return true;
28181 else if (!TYPE_P (arg) && TREE_TYPE (arg)
28182 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
28183 return true;
28184 /* Checking current_function_decl because this structural
28185 comparison is only necessary for redeclaration. */
28186 else if (!current_function_decl
28187 && dependent_template_arg_p (arg)
28188 && (cp_walk_tree_without_duplicates
28189 (&arg, find_parm_usage_r, NULL)))
28190 return true;
28191 }
28192 }
28193 }
28194
28195 return false;
28196 }
28197
28198 /* Returns true if ARGS (a collection of template arguments) contains
28199 any dependent arguments. */
28200
28201 bool
28202 any_dependent_template_arguments_p (const_tree args)
28203 {
28204 int i;
28205 int j;
28206
28207 if (!args)
28208 return false;
28209 if (args == error_mark_node)
28210 return true;
28211
28212 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28213 {
28214 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
28215 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28216 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
28217 return true;
28218 }
28219
28220 return false;
28221 }
28222
28223 /* Returns true if ARGS contains any errors. */
28224
28225 bool
28226 any_erroneous_template_args_p (const_tree args)
28227 {
28228 int i;
28229 int j;
28230
28231 if (args == error_mark_node)
28232 return true;
28233
28234 if (args && TREE_CODE (args) != TREE_VEC)
28235 {
28236 if (tree ti = get_template_info (args))
28237 args = TI_ARGS (ti);
28238 else
28239 args = NULL_TREE;
28240 }
28241
28242 if (!args)
28243 return false;
28244
28245 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28246 {
28247 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
28248 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28249 if (error_operand_p (TREE_VEC_ELT (level, j)))
28250 return true;
28251 }
28252
28253 return false;
28254 }
28255
28256 /* Returns TRUE if the template TMPL is type-dependent. */
28257
28258 bool
28259 dependent_template_p (tree tmpl)
28260 {
28261 if (TREE_CODE (tmpl) == OVERLOAD)
28262 {
28263 for (lkp_iterator iter (tmpl); iter; ++iter)
28264 if (dependent_template_p (*iter))
28265 return true;
28266 return false;
28267 }
28268
28269 /* Template template parameters are dependent. */
28270 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
28271 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
28272 return true;
28273 /* So are names that have not been looked up. */
28274 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
28275 return true;
28276 return false;
28277 }
28278
28279 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
28280
28281 bool
28282 dependent_template_id_p (tree tmpl, tree args)
28283 {
28284 return (dependent_template_p (tmpl)
28285 || any_dependent_template_arguments_p (args));
28286 }
28287
28288 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
28289 are dependent. */
28290
28291 bool
28292 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
28293 {
28294 int i;
28295
28296 if (!processing_template_decl)
28297 return false;
28298
28299 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
28300 {
28301 tree decl = TREE_VEC_ELT (declv, i);
28302 tree init = TREE_VEC_ELT (initv, i);
28303 tree cond = TREE_VEC_ELT (condv, i);
28304 tree incr = TREE_VEC_ELT (incrv, i);
28305
28306 if (type_dependent_expression_p (decl)
28307 || TREE_CODE (decl) == SCOPE_REF)
28308 return true;
28309
28310 if (init && type_dependent_expression_p (init))
28311 return true;
28312
28313 if (cond == global_namespace)
28314 return true;
28315
28316 if (type_dependent_expression_p (cond))
28317 return true;
28318
28319 if (COMPARISON_CLASS_P (cond)
28320 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
28321 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
28322 return true;
28323
28324 if (TREE_CODE (incr) == MODOP_EXPR)
28325 {
28326 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
28327 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
28328 return true;
28329 }
28330 else if (type_dependent_expression_p (incr))
28331 return true;
28332 else if (TREE_CODE (incr) == MODIFY_EXPR)
28333 {
28334 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
28335 return true;
28336 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
28337 {
28338 tree t = TREE_OPERAND (incr, 1);
28339 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
28340 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
28341 return true;
28342
28343 /* If this loop has a class iterator with != comparison
28344 with increment other than i++/++i/i--/--i, make sure the
28345 increment is constant. */
28346 if (CLASS_TYPE_P (TREE_TYPE (decl))
28347 && TREE_CODE (cond) == NE_EXPR)
28348 {
28349 if (TREE_OPERAND (t, 0) == decl)
28350 t = TREE_OPERAND (t, 1);
28351 else
28352 t = TREE_OPERAND (t, 0);
28353 if (TREE_CODE (t) != INTEGER_CST)
28354 return true;
28355 }
28356 }
28357 }
28358 }
28359
28360 return false;
28361 }
28362
28363 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
28364 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
28365 no such TYPE can be found. Note that this function peers inside
28366 uninstantiated templates and therefore should be used only in
28367 extremely limited situations. ONLY_CURRENT_P restricts this
28368 peering to the currently open classes hierarchy (which is required
28369 when comparing types). */
28370
28371 tree
28372 resolve_typename_type (tree type, bool only_current_p)
28373 {
28374 tree scope;
28375 tree name;
28376 tree decl;
28377 int quals;
28378 tree pushed_scope;
28379 tree result;
28380
28381 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
28382
28383 scope = TYPE_CONTEXT (type);
28384 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
28385 gcc_checking_assert (uses_template_parms (scope));
28386
28387 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
28388 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
28389 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
28390 representing the typedef. In that case TYPE_IDENTIFIER (type) is
28391 not the non-qualified identifier of the TYPENAME_TYPE anymore.
28392 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
28393 the TYPENAME_TYPE instead, we avoid messing up with a possible
28394 typedef variant case. */
28395 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
28396
28397 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
28398 it first before we can figure out what NAME refers to. */
28399 if (TREE_CODE (scope) == TYPENAME_TYPE)
28400 {
28401 if (TYPENAME_IS_RESOLVING_P (scope))
28402 /* Given a class template A with a dependent base with nested type C,
28403 typedef typename A::C::C C will land us here, as trying to resolve
28404 the initial A::C leads to the local C typedef, which leads back to
28405 A::C::C. So we break the recursion now. */
28406 return type;
28407 else
28408 scope = resolve_typename_type (scope, only_current_p);
28409 }
28410 /* If we don't know what SCOPE refers to, then we cannot resolve the
28411 TYPENAME_TYPE. */
28412 if (!CLASS_TYPE_P (scope))
28413 return type;
28414 /* If this is a typedef, we don't want to look inside (c++/11987). */
28415 if (typedef_variant_p (type))
28416 return type;
28417 /* If SCOPE isn't the template itself, it will not have a valid
28418 TYPE_FIELDS list. */
28419 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
28420 /* scope is either the template itself or a compatible instantiation
28421 like X<T>, so look up the name in the original template. */
28422 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
28423 /* If scope has no fields, it can't be a current instantiation. Check this
28424 before currently_open_class to avoid infinite recursion (71515). */
28425 if (!TYPE_FIELDS (scope))
28426 return type;
28427 /* If the SCOPE is not the current instantiation, there's no reason
28428 to look inside it. */
28429 if (only_current_p && !currently_open_class (scope))
28430 return type;
28431 /* Enter the SCOPE so that name lookup will be resolved as if we
28432 were in the class definition. In particular, SCOPE will no
28433 longer be considered a dependent type. */
28434 pushed_scope = push_scope (scope);
28435 /* Look up the declaration. */
28436 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
28437 tf_warning_or_error);
28438
28439 result = NULL_TREE;
28440
28441 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28442 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28443 tree fullname = TYPENAME_TYPE_FULLNAME (type);
28444 if (!decl)
28445 /*nop*/;
28446 else if (identifier_p (fullname)
28447 && TREE_CODE (decl) == TYPE_DECL)
28448 {
28449 result = TREE_TYPE (decl);
28450 if (result == error_mark_node)
28451 result = NULL_TREE;
28452 }
28453 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
28454 && DECL_CLASS_TEMPLATE_P (decl))
28455 {
28456 /* Obtain the template and the arguments. */
28457 tree tmpl = TREE_OPERAND (fullname, 0);
28458 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
28459 {
28460 /* We get here with a plain identifier because a previous tentative
28461 parse of the nested-name-specifier as part of a ptr-operator saw
28462 ::template X<A>. The use of ::template is necessary in a
28463 ptr-operator, but wrong in a declarator-id.
28464
28465 [temp.names]: In a qualified-id of a declarator-id, the keyword
28466 template shall not appear at the top level. */
28467 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
28468 "keyword %<template%> not allowed in declarator-id");
28469 tmpl = decl;
28470 }
28471 tree args = TREE_OPERAND (fullname, 1);
28472 /* Instantiate the template. */
28473 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
28474 /*entering_scope=*/true,
28475 tf_error | tf_user);
28476 if (result == error_mark_node)
28477 result = NULL_TREE;
28478 }
28479
28480 /* Leave the SCOPE. */
28481 if (pushed_scope)
28482 pop_scope (pushed_scope);
28483
28484 /* If we failed to resolve it, return the original typename. */
28485 if (!result)
28486 return type;
28487
28488 /* If lookup found a typename type, resolve that too. */
28489 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
28490 {
28491 /* Ill-formed programs can cause infinite recursion here, so we
28492 must catch that. */
28493 TYPENAME_IS_RESOLVING_P (result) = 1;
28494 result = resolve_typename_type (result, only_current_p);
28495 TYPENAME_IS_RESOLVING_P (result) = 0;
28496 }
28497
28498 /* Qualify the resulting type. */
28499 quals = cp_type_quals (type);
28500 if (quals)
28501 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
28502
28503 return result;
28504 }
28505
28506 /* EXPR is an expression which is not type-dependent. Return a proxy
28507 for EXPR that can be used to compute the types of larger
28508 expressions containing EXPR. */
28509
28510 tree
28511 build_non_dependent_expr (tree expr)
28512 {
28513 tree orig_expr = expr;
28514 tree inner_expr;
28515
28516 /* When checking, try to get a constant value for all non-dependent
28517 expressions in order to expose bugs in *_dependent_expression_p
28518 and constexpr. This can affect code generation, see PR70704, so
28519 only do this for -fchecking=2. */
28520 if (flag_checking > 1
28521 && cxx_dialect >= cxx11
28522 /* Don't do this during nsdmi parsing as it can lead to
28523 unexpected recursive instantiations. */
28524 && !parsing_nsdmi ()
28525 /* Don't do this during concept processing either and for
28526 the same reason. */
28527 && !processing_constraint_expression_p ())
28528 fold_non_dependent_expr (expr, tf_none);
28529
28530 STRIP_ANY_LOCATION_WRAPPER (expr);
28531
28532 /* Preserve OVERLOADs; the functions must be available to resolve
28533 types. */
28534 inner_expr = expr;
28535 if (TREE_CODE (inner_expr) == STMT_EXPR)
28536 inner_expr = stmt_expr_value_expr (inner_expr);
28537 if (TREE_CODE (inner_expr) == ADDR_EXPR)
28538 inner_expr = TREE_OPERAND (inner_expr, 0);
28539 if (TREE_CODE (inner_expr) == COMPONENT_REF)
28540 inner_expr = TREE_OPERAND (inner_expr, 1);
28541 if (is_overloaded_fn (inner_expr)
28542 || TREE_CODE (inner_expr) == OFFSET_REF)
28543 return orig_expr;
28544 /* There is no need to return a proxy for a variable, parameter
28545 or enumerator. */
28546 if (VAR_P (expr) || TREE_CODE (expr) == PARM_DECL
28547 || TREE_CODE (expr) == CONST_DECL)
28548 return orig_expr;
28549 /* Preserve string constants; conversions from string constants to
28550 "char *" are allowed, even though normally a "const char *"
28551 cannot be used to initialize a "char *". */
28552 if (TREE_CODE (expr) == STRING_CST)
28553 return orig_expr;
28554 /* Preserve void and arithmetic constants, as an optimization -- there is no
28555 reason to create a new node. */
28556 if (TREE_CODE (expr) == VOID_CST
28557 || TREE_CODE (expr) == INTEGER_CST
28558 || TREE_CODE (expr) == REAL_CST)
28559 return orig_expr;
28560 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28561 There is at least one place where we want to know that a
28562 particular expression is a throw-expression: when checking a ?:
28563 expression, there are special rules if the second or third
28564 argument is a throw-expression. */
28565 if (TREE_CODE (expr) == THROW_EXPR)
28566 return orig_expr;
28567
28568 /* Don't wrap an initializer list, we need to be able to look inside. */
28569 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
28570 return orig_expr;
28571
28572 /* Don't wrap a dummy object, we need to be able to test for it. */
28573 if (is_dummy_object (expr))
28574 return orig_expr;
28575
28576 if (TREE_CODE (expr) == COND_EXPR)
28577 return build3 (COND_EXPR,
28578 TREE_TYPE (expr),
28579 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
28580 (TREE_OPERAND (expr, 1)
28581 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
28582 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
28583 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
28584 if (TREE_CODE (expr) == COMPOUND_EXPR)
28585 return build2 (COMPOUND_EXPR,
28586 TREE_TYPE (expr),
28587 TREE_OPERAND (expr, 0),
28588 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
28589
28590 /* If the type is unknown, it can't really be non-dependent */
28591 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
28592
28593 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28594 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
28595 TREE_TYPE (expr), expr);
28596 }
28597
28598 /* ARGS is a vector of expressions as arguments to a function call.
28599 Replace the arguments with equivalent non-dependent expressions.
28600 This modifies ARGS in place. */
28601
28602 void
28603 make_args_non_dependent (vec<tree, va_gc> *args)
28604 {
28605 unsigned int ix;
28606 tree arg;
28607
28608 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
28609 {
28610 tree newarg = build_non_dependent_expr (arg);
28611 if (newarg != arg)
28612 (*args)[ix] = newarg;
28613 }
28614 }
28615
28616 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28617 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
28618 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
28619
28620 static tree
28621 make_auto_1 (tree name, bool set_canonical, int level = -1)
28622 {
28623 if (level == -1)
28624 level = current_template_depth + 1;
28625 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
28626 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
28627 TYPE_STUB_DECL (au) = TYPE_NAME (au);
28628 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
28629 (0, level, level, TYPE_NAME (au), NULL_TREE);
28630 if (set_canonical)
28631 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28632 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28633 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28634 if (name == decltype_auto_identifier)
28635 AUTO_IS_DECLTYPE (au) = true;
28636
28637 return au;
28638 }
28639
28640 tree
28641 make_decltype_auto (void)
28642 {
28643 return make_auto_1 (decltype_auto_identifier, true);
28644 }
28645
28646 tree
28647 make_auto (void)
28648 {
28649 return make_auto_1 (auto_identifier, true);
28650 }
28651
28652 /* Return a C++17 deduction placeholder for class template TMPL.
28653 There are represented as an 'auto' with the special level 0 and
28654 CLASS_PLACEHOLDER_TEMPLATE set. */
28655
28656 tree
28657 make_template_placeholder (tree tmpl)
28658 {
28659 tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
28660 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28661 /* Our canonical type depends on the placeholder. */
28662 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28663 return t;
28664 }
28665
28666 /* True iff T is a C++17 class template deduction placeholder. */
28667
28668 bool
28669 template_placeholder_p (tree t)
28670 {
28671 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28672 }
28673
28674 /* Make a "constrained auto" type-specifier. This is an auto or
28675 decltype(auto) type with constraints that must be associated after
28676 deduction. The constraint is formed from the given concept CON
28677 and its optional sequence of template arguments ARGS.
28678
28679 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28680
28681 static tree
28682 make_constrained_placeholder_type (tree type, tree con, tree args)
28683 {
28684 /* Build the constraint. */
28685 tree tmpl = DECL_TI_TEMPLATE (con);
28686 tree expr = tmpl;
28687 if (TREE_CODE (con) == FUNCTION_DECL)
28688 expr = ovl_make (tmpl);
28689 ++processing_template_decl;
28690 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28691 --processing_template_decl;
28692
28693 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
28694 = build_tree_list (current_template_parms, expr);
28695
28696 /* Our canonical type depends on the constraint. */
28697 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28698
28699 /* Attach the constraint to the type declaration. */
28700 return TYPE_NAME (type);
28701 }
28702
28703 /* Make a "constrained auto" type-specifier. */
28704
28705 tree
28706 make_constrained_auto (tree con, tree args)
28707 {
28708 tree type = make_auto_1 (auto_identifier, false);
28709 return make_constrained_placeholder_type (type, con, args);
28710 }
28711
28712 /* Make a "constrained decltype(auto)" type-specifier. */
28713
28714 tree
28715 make_constrained_decltype_auto (tree con, tree args)
28716 {
28717 tree type = make_auto_1 (decltype_auto_identifier, false);
28718 return make_constrained_placeholder_type (type, con, args);
28719 }
28720
28721 /* Returns true if the placeholder type constraint T has any dependent
28722 (explicit) template arguments. */
28723
28724 static bool
28725 placeholder_type_constraint_dependent_p (tree t)
28726 {
28727 tree id = unpack_concept_check (t);
28728 tree args = TREE_OPERAND (id, 1);
28729 tree first = TREE_VEC_ELT (args, 0);
28730 if (ARGUMENT_PACK_P (first))
28731 {
28732 args = expand_template_argument_pack (args);
28733 first = TREE_VEC_ELT (args, 0);
28734 }
28735 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
28736 || is_auto (first));
28737 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
28738 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
28739 return true;
28740 return false;
28741 }
28742
28743 /* Build and return a concept definition. Like other templates, the
28744 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28745 the TEMPLATE_DECL. */
28746
28747 tree
28748 finish_concept_definition (cp_expr id, tree init)
28749 {
28750 gcc_assert (identifier_p (id));
28751 gcc_assert (processing_template_decl);
28752
28753 location_t loc = id.get_location();
28754
28755 /* A concept-definition shall not have associated constraints. */
28756 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28757 {
28758 error_at (loc, "a concept cannot be constrained");
28759 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28760 }
28761
28762 /* A concept-definition shall appear in namespace scope. Templates
28763 aren't allowed in block scope, so we only need to check for class
28764 scope. */
28765 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28766 {
28767 error_at (loc, "concept %qE not in namespace scope", *id);
28768 return error_mark_node;
28769 }
28770
28771 /* Initially build the concept declaration; its type is bool. */
28772 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28773 DECL_CONTEXT (decl) = current_scope ();
28774 DECL_INITIAL (decl) = init;
28775
28776 set_originating_module (decl, false);
28777
28778 /* Push the enclosing template. */
28779 return push_template_decl (decl);
28780 }
28781
28782 /* Given type ARG, return std::initializer_list<ARG>. */
28783
28784 static tree
28785 listify (tree arg)
28786 {
28787 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28788
28789 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28790 {
28791 gcc_rich_location richloc (input_location);
28792 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28793 error_at (&richloc,
28794 "deducing from brace-enclosed initializer list"
28795 " requires %<#include <initializer_list>%>");
28796
28797 return error_mark_node;
28798 }
28799 tree argvec = make_tree_vec (1);
28800 TREE_VEC_ELT (argvec, 0) = arg;
28801
28802 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28803 NULL_TREE, 0, tf_warning_or_error);
28804 }
28805
28806 /* Replace auto in TYPE with std::initializer_list<auto>. */
28807
28808 static tree
28809 listify_autos (tree type, tree auto_node)
28810 {
28811 tree init_auto = listify (strip_top_quals (auto_node));
28812 tree argvec = make_tree_vec (1);
28813 TREE_VEC_ELT (argvec, 0) = init_auto;
28814 if (processing_template_decl)
28815 argvec = add_to_template_args (current_template_args (), argvec);
28816 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28817 }
28818
28819 /* Hash traits for hashing possibly constrained 'auto'
28820 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28821
28822 struct auto_hash : default_hash_traits<tree>
28823 {
28824 static inline hashval_t hash (tree);
28825 static inline bool equal (tree, tree);
28826 };
28827
28828 /* Hash the 'auto' T. */
28829
28830 inline hashval_t
28831 auto_hash::hash (tree t)
28832 {
28833 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28834 /* Matching constrained-type-specifiers denote the same template
28835 parameter, so hash the constraint. */
28836 return hash_placeholder_constraint (c);
28837 else
28838 /* But unconstrained autos are all separate, so just hash the pointer. */
28839 return iterative_hash_object (t, 0);
28840 }
28841
28842 /* Compare two 'auto's. */
28843
28844 inline bool
28845 auto_hash::equal (tree t1, tree t2)
28846 {
28847 if (t1 == t2)
28848 return true;
28849
28850 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28851 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28852
28853 /* Two unconstrained autos are distinct. */
28854 if (!c1 || !c2)
28855 return false;
28856
28857 return equivalent_placeholder_constraints (c1, c2);
28858 }
28859
28860 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28861 constrained) auto, add it to the vector. */
28862
28863 static int
28864 extract_autos_r (tree t, void *data)
28865 {
28866 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28867 if (is_auto (t) && !template_placeholder_p (t))
28868 {
28869 /* All the autos were built with index 0; fix that up now. */
28870 tree *p = hash.find_slot (t, INSERT);
28871 unsigned idx;
28872 if (*p)
28873 /* If this is a repeated constrained-type-specifier, use the index we
28874 chose before. */
28875 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28876 else
28877 {
28878 /* Otherwise this is new, so use the current count. */
28879 *p = t;
28880 idx = hash.elements () - 1;
28881 }
28882 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28883 }
28884
28885 /* Always keep walking. */
28886 return 0;
28887 }
28888
28889 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28890 says they can appear anywhere in the type. */
28891
28892 static tree
28893 extract_autos (tree type)
28894 {
28895 hash_set<tree> visited;
28896 hash_table<auto_hash> hash (2);
28897
28898 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28899
28900 tree tree_vec = make_tree_vec (hash.elements());
28901 for (tree elt : hash)
28902 {
28903 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28904 TREE_VEC_ELT (tree_vec, i)
28905 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28906 }
28907
28908 return tree_vec;
28909 }
28910
28911 /* The stem for deduction guide names. */
28912 const char *const dguide_base = "__dguide_";
28913
28914 /* Return the name for a deduction guide for class template TMPL. */
28915
28916 tree
28917 dguide_name (tree tmpl)
28918 {
28919 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28920 tree tname = TYPE_IDENTIFIER (type);
28921 char *buf = (char *) alloca (1 + strlen (dguide_base)
28922 + IDENTIFIER_LENGTH (tname));
28923 memcpy (buf, dguide_base, strlen (dguide_base));
28924 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28925 IDENTIFIER_LENGTH (tname) + 1);
28926 tree dname = get_identifier (buf);
28927 TREE_TYPE (dname) = type;
28928 return dname;
28929 }
28930
28931 /* True if NAME is the name of a deduction guide. */
28932
28933 bool
28934 dguide_name_p (tree name)
28935 {
28936 return (TREE_CODE (name) == IDENTIFIER_NODE
28937 && TREE_TYPE (name)
28938 && startswith (IDENTIFIER_POINTER (name), dguide_base));
28939 }
28940
28941 /* True if FN is a deduction guide. */
28942
28943 bool
28944 deduction_guide_p (const_tree fn)
28945 {
28946 if (DECL_P (fn))
28947 if (tree name = DECL_NAME (fn))
28948 return dguide_name_p (name);
28949 return false;
28950 }
28951
28952 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28953
28954 bool
28955 copy_guide_p (const_tree fn)
28956 {
28957 gcc_assert (deduction_guide_p (fn));
28958 if (!DECL_ARTIFICIAL (fn))
28959 return false;
28960 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28961 return (TREE_CHAIN (parms) == void_list_node
28962 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28963 }
28964
28965 /* True if FN is a guide generated from a constructor template. */
28966
28967 bool
28968 template_guide_p (const_tree fn)
28969 {
28970 gcc_assert (deduction_guide_p (fn));
28971 if (!DECL_ARTIFICIAL (fn))
28972 return false;
28973 tree tmpl = DECL_TI_TEMPLATE (fn);
28974 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28975 return PRIMARY_TEMPLATE_P (org);
28976 return false;
28977 }
28978
28979 /* True if FN is an aggregate initialization guide or the copy deduction
28980 guide. */
28981
28982 bool
28983 builtin_guide_p (const_tree fn)
28984 {
28985 if (!deduction_guide_p (fn))
28986 return false;
28987 if (!DECL_ARTIFICIAL (fn))
28988 /* Explicitly declared. */
28989 return false;
28990 if (DECL_ABSTRACT_ORIGIN (fn))
28991 /* Derived from a constructor. */
28992 return false;
28993 return true;
28994 }
28995
28996 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28997 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28998 template parameter types. Note that the handling of template template
28999 parameters relies on current_template_parms being set appropriately for the
29000 new template. */
29001
29002 static tree
29003 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
29004 tree tsubst_args, tsubst_flags_t complain)
29005 {
29006 if (olddecl == error_mark_node)
29007 return error_mark_node;
29008
29009 tree oldidx = get_template_parm_index (olddecl);
29010
29011 tree newtype;
29012 if (TREE_CODE (olddecl) == TYPE_DECL
29013 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29014 {
29015 tree oldtype = TREE_TYPE (olddecl);
29016 newtype = cxx_make_type (TREE_CODE (oldtype));
29017 TYPE_MAIN_VARIANT (newtype) = newtype;
29018 }
29019 else
29020 {
29021 newtype = TREE_TYPE (olddecl);
29022 if (type_uses_auto (newtype))
29023 {
29024 // Substitute once to fix references to other template parameters.
29025 newtype = tsubst (newtype, tsubst_args,
29026 complain|tf_partial, NULL_TREE);
29027 // Now substitute again to reduce the level of the auto.
29028 newtype = tsubst (newtype, current_template_args (),
29029 complain, NULL_TREE);
29030 }
29031 else
29032 newtype = tsubst (newtype, tsubst_args,
29033 complain, NULL_TREE);
29034 }
29035
29036 tree newdecl
29037 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
29038 DECL_NAME (olddecl), newtype);
29039 SET_DECL_TEMPLATE_PARM_P (newdecl);
29040
29041 tree newidx;
29042 if (TREE_CODE (olddecl) == TYPE_DECL
29043 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29044 {
29045 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
29046 = build_template_parm_index (index, level, level,
29047 newdecl, newtype);
29048 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29049 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29050 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
29051 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
29052 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
29053 else
29054 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
29055
29056 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
29057 {
29058 DECL_TEMPLATE_RESULT (newdecl)
29059 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
29060 DECL_NAME (olddecl), newtype);
29061 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
29062 // First create a copy (ttargs) of tsubst_args with an
29063 // additional level for the template template parameter's own
29064 // template parameters (ttparms).
29065 tree ttparms = (INNERMOST_TEMPLATE_PARMS
29066 (DECL_TEMPLATE_PARMS (olddecl)));
29067 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
29068 tree ttargs = make_tree_vec (depth + 1);
29069 for (int i = 0; i < depth; ++i)
29070 TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
29071 TREE_VEC_ELT (ttargs, depth)
29072 = template_parms_level_to_args (ttparms);
29073 // Substitute ttargs into ttparms to fix references to
29074 // other template parameters.
29075 ttparms = tsubst_template_parms_level (ttparms, ttargs,
29076 complain|tf_partial);
29077 // Now substitute again with args based on tparms, to reduce
29078 // the level of the ttparms.
29079 ttargs = current_template_args ();
29080 ttparms = tsubst_template_parms_level (ttparms, ttargs,
29081 complain);
29082 // Finally, tack the adjusted parms onto tparms.
29083 ttparms = tree_cons (size_int (level + 1), ttparms,
29084 copy_node (current_template_parms));
29085 // As with all template template parms, the parameter list captured
29086 // by this template template parm that corresponds to its own level
29087 // should be empty. This avoids infinite recursion when structurally
29088 // comparing two such rewritten template template parms (PR102479).
29089 gcc_assert (!TREE_VEC_LENGTH
29090 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
29091 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
29092 TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
29093 // All done.
29094 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
29095 }
29096 }
29097 else
29098 {
29099 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
29100 tree newconst
29101 = build_decl (DECL_SOURCE_LOCATION (oldconst),
29102 TREE_CODE (oldconst),
29103 DECL_NAME (oldconst), newtype);
29104 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
29105 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
29106 SET_DECL_TEMPLATE_PARM_P (newconst);
29107 newidx = build_template_parm_index (index, level, level,
29108 newconst, newtype);
29109 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29110 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29111 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
29112 }
29113
29114 return newdecl;
29115 }
29116
29117 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29118 template parameter. */
29119
29120 static tree
29121 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
29122 tree targs, unsigned targs_index, tsubst_flags_t complain)
29123 {
29124 tree olddecl = TREE_VALUE (oldelt);
29125 tree newdecl = rewrite_template_parm (olddecl, index, level,
29126 targs, complain);
29127 if (newdecl == error_mark_node)
29128 return error_mark_node;
29129 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
29130 targs, complain, NULL_TREE);
29131 tree list = build_tree_list (newdef, newdecl);
29132 TEMPLATE_PARM_CONSTRAINTS (list)
29133 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
29134 targs, complain, NULL_TREE);
29135 int depth = TMPL_ARGS_DEPTH (targs);
29136 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
29137 return list;
29138 }
29139
29140 /* Returns a C++17 class deduction guide template based on the constructor
29141 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29142 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29143 aggregate initialization guide. OUTER_ARGS are the template arguments
29144 for the enclosing scope of the class. */
29145
29146 static tree
29147 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
29148 {
29149 tree tparms, targs, fparms, fargs, ci;
29150 bool memtmpl = false;
29151 bool explicit_p;
29152 location_t loc;
29153 tree fn_tmpl = NULL_TREE;
29154
29155 if (outer_args)
29156 {
29157 ++processing_template_decl;
29158 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
29159 --processing_template_decl;
29160 }
29161
29162 if (!DECL_DECLARES_FUNCTION_P (ctor))
29163 {
29164 if (TYPE_P (ctor))
29165 {
29166 bool copy_p = TYPE_REF_P (ctor);
29167 if (copy_p)
29168 fparms = tree_cons (NULL_TREE, type, void_list_node);
29169 else
29170 fparms = void_list_node;
29171 }
29172 else if (TREE_CODE (ctor) == TREE_LIST)
29173 fparms = ctor;
29174 else
29175 gcc_unreachable ();
29176
29177 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
29178 tparms = DECL_TEMPLATE_PARMS (ctmpl);
29179 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
29180 ci = NULL_TREE;
29181 fargs = NULL_TREE;
29182 loc = DECL_SOURCE_LOCATION (ctmpl);
29183 explicit_p = false;
29184 }
29185 else
29186 {
29187 ++processing_template_decl;
29188 bool ok = true;
29189
29190 fn_tmpl
29191 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
29192 : DECL_TI_TEMPLATE (ctor));
29193 if (outer_args)
29194 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
29195 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
29196
29197 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
29198 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29199 fully specialized args for the enclosing class. Strip those off, as
29200 the deduction guide won't have those template parameters. */
29201 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
29202 TMPL_PARMS_DEPTH (tparms));
29203 /* Discard the 'this' parameter. */
29204 fparms = FUNCTION_ARG_CHAIN (ctor);
29205 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
29206 ci = get_constraints (ctor);
29207 loc = DECL_SOURCE_LOCATION (ctor);
29208 explicit_p = DECL_NONCONVERTING_P (ctor);
29209
29210 if (PRIMARY_TEMPLATE_P (fn_tmpl))
29211 {
29212 memtmpl = true;
29213
29214 /* For a member template constructor, we need to flatten the two
29215 template parameter lists into one, and then adjust the function
29216 signature accordingly. This gets...complicated. */
29217 tree save_parms = current_template_parms;
29218
29219 /* For a member template we should have two levels of parms/args, one
29220 for the class and one for the constructor. We stripped
29221 specialized args for further enclosing classes above. */
29222 const int depth = 2;
29223 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
29224
29225 /* Template args for translating references to the two-level template
29226 parameters into references to the one-level template parameters we
29227 are creating. */
29228 tree tsubst_args = copy_node (targs);
29229 TMPL_ARGS_LEVEL (tsubst_args, depth)
29230 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
29231
29232 /* Template parms for the constructor template. */
29233 tree ftparms = TREE_VALUE (tparms);
29234 unsigned flen = TREE_VEC_LENGTH (ftparms);
29235 /* Template parms for the class template. */
29236 tparms = TREE_CHAIN (tparms);
29237 tree ctparms = TREE_VALUE (tparms);
29238 unsigned clen = TREE_VEC_LENGTH (ctparms);
29239 /* Template parms for the deduction guide start as a copy of the
29240 template parms for the class. We set current_template_parms for
29241 lookup_template_class_1. */
29242 current_template_parms = tparms = copy_node (tparms);
29243 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
29244 for (unsigned i = 0; i < clen; ++i)
29245 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
29246
29247 /* Now we need to rewrite the constructor parms to append them to the
29248 class parms. */
29249 for (unsigned i = 0; i < flen; ++i)
29250 {
29251 unsigned index = i + clen;
29252 unsigned level = 1;
29253 tree oldelt = TREE_VEC_ELT (ftparms, i);
29254 tree newelt
29255 = rewrite_tparm_list (oldelt, index, level,
29256 tsubst_args, i, complain);
29257 if (newelt == error_mark_node)
29258 ok = false;
29259 TREE_VEC_ELT (new_vec, index) = newelt;
29260 }
29261
29262 /* Now we have a final set of template parms to substitute into the
29263 function signature. */
29264 targs = template_parms_to_args (tparms);
29265 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
29266 complain, ctor);
29267 if (fparms == error_mark_node)
29268 ok = false;
29269 if (ci)
29270 {
29271 if (outer_args)
29272 /* FIXME: We'd like to avoid substituting outer template
29273 arguments into the constraint ahead of time, but the
29274 construction of tsubst_args assumes that outer arguments
29275 are already substituted in. */
29276 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
29277 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
29278 }
29279
29280 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
29281 cp_unevaluated_operand. */
29282 cp_evaluated ev;
29283 fargs = tsubst (fargs, tsubst_args, complain, ctor);
29284 current_template_parms = save_parms;
29285 }
29286 else
29287 {
29288 /* Substitute in the same arguments to rewrite class members into
29289 references to members of an unknown specialization. */
29290 cp_evaluated ev;
29291 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
29292 fargs = tsubst (fargs, targs, complain, ctor);
29293 if (ci)
29294 {
29295 if (outer_args)
29296 /* FIXME: As above. */
29297 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
29298 ci = tsubst_constraint_info (ci, targs, complain, ctor);
29299 }
29300 }
29301
29302 --processing_template_decl;
29303 if (!ok)
29304 return error_mark_node;
29305 }
29306
29307 if (!memtmpl)
29308 {
29309 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
29310 tparms = copy_node (tparms);
29311 INNERMOST_TEMPLATE_PARMS (tparms)
29312 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
29313 }
29314
29315 tree fntype = build_function_type (type, fparms);
29316 tree ded_fn = build_lang_decl_loc (loc,
29317 FUNCTION_DECL,
29318 dguide_name (type), fntype);
29319 DECL_ARGUMENTS (ded_fn) = fargs;
29320 DECL_ARTIFICIAL (ded_fn) = true;
29321 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
29322 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
29323 DECL_ARTIFICIAL (ded_tmpl) = true;
29324 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
29325 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
29326 if (DECL_P (ctor))
29327 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
29328 if (ci)
29329 set_constraints (ded_tmpl, ci);
29330
29331 return ded_tmpl;
29332 }
29333
29334 /* Add to LIST the member types for the reshaped initializer CTOR. */
29335
29336 static tree
29337 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
29338 {
29339 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
29340 tree idx, val; unsigned i;
29341 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
29342 {
29343 tree ftype = elt ? elt : TREE_TYPE (idx);
29344 if (BRACE_ENCLOSED_INITIALIZER_P (val)
29345 && CONSTRUCTOR_BRACES_ELIDED_P (val))
29346 {
29347 tree subelt = NULL_TREE;
29348 if (TREE_CODE (ftype) == ARRAY_TYPE)
29349 subelt = TREE_TYPE (ftype);
29350 list = collect_ctor_idx_types (val, list, subelt);
29351 continue;
29352 }
29353 tree arg = NULL_TREE;
29354 if (i == v->length() - 1
29355 && PACK_EXPANSION_P (ftype))
29356 /* Give the trailing pack expansion parameter a default argument to
29357 match aggregate initialization behavior, even if we deduce the
29358 length of the pack separately to more than we have initializers. */
29359 arg = build_constructor (init_list_type_node, NULL);
29360 /* if ei is of array type and xi is a braced-init-list or string literal,
29361 Ti is an rvalue reference to the declared type of ei */
29362 STRIP_ANY_LOCATION_WRAPPER (val);
29363 if (TREE_CODE (ftype) == ARRAY_TYPE
29364 && (BRACE_ENCLOSED_INITIALIZER_P (val)
29365 || TREE_CODE (val) == STRING_CST))
29366 {
29367 if (TREE_CODE (val) == STRING_CST)
29368 ftype = cp_build_qualified_type
29369 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
29370 ftype = (cp_build_reference_type
29371 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
29372 }
29373 list = tree_cons (arg, ftype, list);
29374 }
29375
29376 return list;
29377 }
29378
29379 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
29380
29381 static bool
29382 is_spec_or_derived (tree etype, tree tmpl)
29383 {
29384 if (!etype || !CLASS_TYPE_P (etype))
29385 return false;
29386
29387 etype = cv_unqualified (etype);
29388 tree type = TREE_TYPE (tmpl);
29389 tree tparms = (INNERMOST_TEMPLATE_PARMS
29390 (DECL_TEMPLATE_PARMS (tmpl)));
29391 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29392 int err = unify (tparms, targs, type, etype,
29393 UNIFY_ALLOW_DERIVED, /*explain*/false);
29394 ggc_free (targs);
29395 return !err;
29396 }
29397
29398 static tree alias_ctad_tweaks (tree, tree);
29399
29400 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
29401 INIT. */
29402
29403 static tree
29404 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
29405 {
29406 if (cxx_dialect < cxx20)
29407 return NULL_TREE;
29408
29409 if (init == NULL_TREE)
29410 return NULL_TREE;
29411
29412 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29413 {
29414 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29415 tree tinfo = get_template_info (under);
29416 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
29417 return alias_ctad_tweaks (tmpl, guide);
29418 return NULL_TREE;
29419 }
29420
29421 /* We might be creating a guide for a class member template, e.g.,
29422
29423 template<typename U> struct A {
29424 template<typename T> struct B { T t; };
29425 };
29426
29427 At this point, A will have been instantiated. Below, we need to
29428 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
29429 const bool member_template_p
29430 = (DECL_TEMPLATE_INFO (tmpl)
29431 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
29432 tree type = TREE_TYPE (tmpl);
29433 tree template_type = (member_template_p
29434 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
29435 : type);
29436 if (!CP_AGGREGATE_TYPE_P (template_type))
29437 return NULL_TREE;
29438
29439 /* No aggregate candidate for copy-initialization. */
29440 if (args->length() == 1)
29441 {
29442 tree val = (*args)[0];
29443 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
29444 return NULL_TREE;
29445 }
29446
29447 /* If we encounter a problem, we just won't add the candidate. */
29448 tsubst_flags_t complain = tf_none;
29449
29450 tree parms = NULL_TREE;
29451 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29452 {
29453 init = reshape_init (template_type, init, complain);
29454 if (init == error_mark_node)
29455 return NULL_TREE;
29456 parms = collect_ctor_idx_types (init, parms);
29457 /* If we're creating a deduction guide for a member class template,
29458 we've used the original template pattern type for the reshape_init
29459 above; this is done because we want PARMS to be a template parameter
29460 type, something that can be deduced when used as a function template
29461 parameter. At this point the outer class template has already been
29462 partially instantiated (we deferred the deduction until the enclosing
29463 scope is non-dependent). Therefore we have to partially instantiate
29464 PARMS, so that its template level is properly reduced and we don't get
29465 mismatches when deducing types using the guide with PARMS. */
29466 if (member_template_p)
29467 parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
29468 }
29469 else if (TREE_CODE (init) == TREE_LIST)
29470 {
29471 int len = list_length (init);
29472 for (tree field = TYPE_FIELDS (type);
29473 len;
29474 --len, field = DECL_CHAIN (field))
29475 {
29476 field = next_initializable_field (field);
29477 if (!field)
29478 return NULL_TREE;
29479 tree ftype = finish_decltype_type (field, true, complain);
29480 parms = tree_cons (NULL_TREE, ftype, parms);
29481 }
29482 }
29483 else
29484 /* Aggregate initialization doesn't apply to an initializer expression. */
29485 return NULL_TREE;
29486
29487 if (parms)
29488 {
29489 tree last = parms;
29490 parms = nreverse (parms);
29491 TREE_CHAIN (last) = void_list_node;
29492 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
29493 return guide;
29494 }
29495
29496 return NULL_TREE;
29497 }
29498
29499 /* UGUIDES are the deduction guides for the underlying template of alias
29500 template TMPL; adjust them to be deduction guides for TMPL. */
29501
29502 static tree
29503 alias_ctad_tweaks (tree tmpl, tree uguides)
29504 {
29505 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29506 class type (9.2.8.2) where the template-name names an alias template A,
29507 the defining-type-id of A must be of the form
29508
29509 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29510
29511 as specified in 9.2.8.2. The guides of A are the set of functions or
29512 function templates formed as follows. For each function or function
29513 template f in the guides of the template named by the simple-template-id
29514 of the defining-type-id, the template arguments of the return type of f
29515 are deduced from the defining-type-id of A according to the process in
29516 13.10.2.5 with the exception that deduction does not fail if not all
29517 template arguments are deduced. Let g denote the result of substituting
29518 these deductions into f. If substitution succeeds, form a function or
29519 function template f' with the following properties and add it to the set
29520 of guides of A:
29521
29522 * The function type of f' is the function type of g.
29523
29524 * If f is a function template, f' is a function template whose template
29525 parameter list consists of all the template parameters of A (including
29526 their default template arguments) that appear in the above deductions or
29527 (recursively) in their default template arguments, followed by the
29528 template parameters of f that were not deduced (including their default
29529 template arguments), otherwise f' is not a function template.
29530
29531 * The associated constraints (13.5.2) are the conjunction of the
29532 associated constraints of g and a constraint that is satisfied if and only
29533 if the arguments of A are deducible (see below) from the return type.
29534
29535 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29536 be so as well.
29537
29538 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29539 considered to be so as well.
29540
29541 * The explicit-specifier of f' is the explicit-specifier of g (if
29542 any). */
29543
29544 /* This implementation differs from the above in two significant ways:
29545
29546 1) We include all template parameters of A, not just some.
29547 2) The added constraint is same_type instead of deducible.
29548
29549 I believe that while it's probably possible to construct a testcase that
29550 behaves differently with this simplification, it should have the same
29551 effect for real uses. Including all template parameters means that we
29552 deduce all parameters of A when resolving the call, so when we're in the
29553 constraint we don't need to deduce them again, we can just check whether
29554 the deduction produced the desired result. */
29555
29556 tsubst_flags_t complain = tf_warning_or_error;
29557 tree atype = TREE_TYPE (tmpl);
29558 tree aguides = NULL_TREE;
29559 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
29560 unsigned natparms = TREE_VEC_LENGTH (atparms);
29561 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29562 for (ovl_iterator iter (uguides); iter; ++iter)
29563 {
29564 tree f = *iter;
29565 tree in_decl = f;
29566 location_t loc = DECL_SOURCE_LOCATION (f);
29567 tree ret = TREE_TYPE (TREE_TYPE (f));
29568 tree fprime = f;
29569 if (TREE_CODE (f) == TEMPLATE_DECL)
29570 {
29571 processing_template_decl_sentinel ptds (/*reset*/false);
29572 ++processing_template_decl;
29573
29574 /* Deduce template arguments for f from the type-id of A. */
29575 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
29576 unsigned len = TREE_VEC_LENGTH (ftparms);
29577 tree targs = make_tree_vec (len);
29578 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
29579 if (err)
29580 continue;
29581
29582 /* The number of parms for f' is the number of parms for A plus
29583 non-deduced parms of f. */
29584 unsigned ndlen = 0;
29585 unsigned j;
29586 for (unsigned i = 0; i < len; ++i)
29587 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29588 ++ndlen;
29589 tree gtparms = make_tree_vec (natparms + ndlen);
29590
29591 /* Set current_template_parms as in build_deduction_guide. */
29592 auto ctp = make_temp_override (current_template_parms);
29593 current_template_parms = copy_node (DECL_TEMPLATE_PARMS (tmpl));
29594 TREE_VALUE (current_template_parms) = gtparms;
29595
29596 /* First copy over the parms of A. */
29597 for (j = 0; j < natparms; ++j)
29598 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
29599 /* Now rewrite the non-deduced parms of f. */
29600 for (unsigned i = 0; ndlen && i < len; ++i)
29601 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29602 {
29603 --ndlen;
29604 unsigned index = j++;
29605 unsigned level = 1;
29606 tree oldlist = TREE_VEC_ELT (ftparms, i);
29607 tree list = rewrite_tparm_list (oldlist, index, level,
29608 targs, i, complain);
29609 TREE_VEC_ELT (gtparms, index) = list;
29610 }
29611 gtparms = build_tree_list (size_one_node, gtparms);
29612
29613 /* Substitute the deduced arguments plus the rewritten template
29614 parameters into f to get g. This covers the type, copyness,
29615 guideness, and explicit-specifier. */
29616 tree g;
29617 {
29618 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
29619 if cp_unevaluated_operand. */
29620 cp_evaluated ev;
29621 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
29622 }
29623 if (g == error_mark_node)
29624 continue;
29625 DECL_USE_TEMPLATE (g) = 0;
29626 fprime = build_template_decl (g, gtparms, false);
29627 DECL_TEMPLATE_RESULT (fprime) = g;
29628 TREE_TYPE (fprime) = TREE_TYPE (g);
29629 tree gtargs = template_parms_to_args (gtparms);
29630 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
29631 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
29632
29633 /* Substitute the associated constraints. */
29634 tree ci = get_constraints (f);
29635 if (ci)
29636 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
29637 if (ci == error_mark_node)
29638 continue;
29639
29640 /* Add a constraint that the return type matches the instantiation of
29641 A with the same template arguments. */
29642 ret = TREE_TYPE (TREE_TYPE (fprime));
29643 if (!same_type_p (atype, ret)
29644 /* FIXME this should mean they don't compare as equivalent. */
29645 || dependent_alias_template_spec_p (atype, nt_opaque))
29646 {
29647 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
29648 ci = append_constraint (ci, same);
29649 }
29650
29651 if (ci)
29652 {
29653 remove_constraints (fprime);
29654 set_constraints (fprime, ci);
29655 }
29656 }
29657 else
29658 {
29659 /* For a non-template deduction guide, if the arguments of A aren't
29660 deducible from the return type, don't add the candidate. */
29661 tree targs = make_tree_vec (natparms);
29662 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
29663 for (unsigned i = 0; !err && i < natparms; ++i)
29664 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29665 err = true;
29666 if (err)
29667 continue;
29668 }
29669
29670 aguides = lookup_add (fprime, aguides);
29671 }
29672
29673 return aguides;
29674 }
29675
29676 /* Return artificial deduction guides built from the constructors of class
29677 template TMPL. */
29678
29679 static tree
29680 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
29681 {
29682 tree type = TREE_TYPE (tmpl);
29683 tree outer_args = NULL_TREE;
29684 if (DECL_CLASS_SCOPE_P (tmpl)
29685 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
29686 {
29687 outer_args = copy_node (CLASSTYPE_TI_ARGS (type));
29688 gcc_assert (TMPL_ARGS_DEPTH (outer_args) > 1);
29689 --TREE_VEC_LENGTH (outer_args);
29690 type = TREE_TYPE (most_general_template (tmpl));
29691 }
29692
29693 tree cands = NULL_TREE;
29694
29695 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
29696 {
29697 /* Skip inherited constructors. */
29698 if (iter.using_p ())
29699 continue;
29700
29701 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
29702 cands = lookup_add (guide, cands);
29703 }
29704
29705 /* Add implicit default constructor deduction guide. */
29706 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
29707 {
29708 tree guide = build_deduction_guide (type, type, outer_args,
29709 complain);
29710 cands = lookup_add (guide, cands);
29711 }
29712
29713 /* Add copy guide. */
29714 {
29715 tree gtype = build_reference_type (type);
29716 tree guide = build_deduction_guide (type, gtype, outer_args,
29717 complain);
29718 cands = lookup_add (guide, cands);
29719 }
29720
29721 return cands;
29722 }
29723
29724 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29725
29726 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29727 aggregate candidate is added separately because it depends on the
29728 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29729 guide. */
29730
29731 static tree
29732 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29733 {
29734 tree guides = NULL_TREE;
29735 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29736 {
29737 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29738 tree tinfo = get_template_info (under);
29739 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29740 complain);
29741 }
29742 else
29743 {
29744 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29745 dguide_name (tmpl),
29746 LOOK_want::NORMAL, /*complain*/false);
29747 if (guides == error_mark_node)
29748 guides = NULL_TREE;
29749 else
29750 any_dguides_p = true;
29751 }
29752
29753 /* Cache the deduction guides for a template. We also remember the result of
29754 lookup, and rebuild everything if it changes; should be very rare. */
29755 tree_pair_p cache = NULL;
29756 if (tree_pair_p &r
29757 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29758 {
29759 cache = r;
29760 if (cache->purpose == guides)
29761 return cache->value;
29762 }
29763 else
29764 {
29765 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29766 cache->purpose = guides;
29767 }
29768
29769 tree cands = NULL_TREE;
29770 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29771 cands = alias_ctad_tweaks (tmpl, guides);
29772 else
29773 {
29774 cands = ctor_deduction_guides_for (tmpl, complain);
29775 for (ovl_iterator it (guides); it; ++it)
29776 cands = lookup_add (*it, cands);
29777 }
29778
29779 cache->value = cands;
29780 return cands;
29781 }
29782
29783 /* Return whether TMPL is a (class template argument-) deducible template. */
29784
29785 bool
29786 ctad_template_p (tree tmpl)
29787 {
29788 /* A deducible template is either a class template or is an alias template
29789 whose defining-type-id is of the form
29790
29791 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29792
29793 where the nested-name-specifier (if any) is non-dependent and the
29794 template-name of the simple-template-id names a deducible template. */
29795
29796 if (DECL_CLASS_TEMPLATE_P (tmpl)
29797 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29798 return true;
29799 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29800 return false;
29801 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29802 if (tree tinfo = get_template_info (orig))
29803 return ctad_template_p (TI_TEMPLATE (tinfo));
29804 return false;
29805 }
29806
29807 /* Deduce template arguments for the class template placeholder PTYPE for
29808 template TMPL based on the initializer INIT, and return the resulting
29809 type. */
29810
29811 static tree
29812 do_class_deduction (tree ptype, tree tmpl, tree init,
29813 int flags, tsubst_flags_t complain)
29814 {
29815 /* We should have handled this in the caller. */
29816 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29817 return ptype;
29818
29819 /* If the class was erroneous, don't try to deduce, because that
29820 can generate a lot of diagnostic. */
29821 if (TREE_TYPE (tmpl)
29822 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
29823 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
29824 return ptype;
29825
29826 /* Wait until the enclosing scope is non-dependent. */
29827 if (DECL_CLASS_SCOPE_P (tmpl)
29828 && dependent_type_p (DECL_CONTEXT (tmpl)))
29829 return ptype;
29830
29831 /* Initializing one placeholder from another. */
29832 if (init
29833 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
29834 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
29835 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
29836 == TEMPLATE_PARM_INDEX)))
29837 && is_auto (TREE_TYPE (init))
29838 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29839 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29840
29841 /* Look through alias templates that just rename another template. */
29842 tmpl = get_underlying_template (tmpl);
29843 if (!ctad_template_p (tmpl))
29844 {
29845 if (complain & tf_error)
29846 error ("non-deducible template %qT used without template arguments", tmpl);
29847 return error_mark_node;
29848 }
29849 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29850 {
29851 if (complain & tf_error)
29852 error ("alias template deduction only available "
29853 "with %<-std=c++20%> or %<-std=gnu++20%>");
29854 return error_mark_node;
29855 }
29856
29857 /* Wait until the initializer is non-dependent. */
29858 if (type_dependent_expression_p (init))
29859 return ptype;
29860
29861 tree type = TREE_TYPE (tmpl);
29862
29863 bool try_list_ctor = false;
29864 bool list_init_p = false;
29865
29866 releasing_vec rv_args = NULL;
29867 vec<tree,va_gc> *&args = *&rv_args;
29868 if (init == NULL_TREE)
29869 args = make_tree_vector ();
29870 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29871 {
29872 list_init_p = true;
29873 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29874 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
29875 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
29876 {
29877 /* As an exception, the first phase in 16.3.1.7 (considering the
29878 initializer list as a single argument) is omitted if the
29879 initializer list consists of a single expression of type cv U,
29880 where U is a specialization of C or a class derived from a
29881 specialization of C. */
29882 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29883 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29884 try_list_ctor = false;
29885 }
29886 if (try_list_ctor || is_std_init_list (type))
29887 args = make_tree_vector_single (init);
29888 else
29889 args = make_tree_vector_from_ctor (init);
29890 }
29891 else if (TREE_CODE (init) == TREE_LIST)
29892 args = make_tree_vector_from_list (init);
29893 else
29894 args = make_tree_vector_single (init);
29895
29896 /* Do this now to avoid problems with erroneous args later on. */
29897 args = resolve_args (args, complain);
29898 if (args == NULL)
29899 return error_mark_node;
29900
29901 bool any_dguides_p = false;
29902 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29903 if (cands == error_mark_node)
29904 return error_mark_node;
29905
29906 /* Prune explicit deduction guides in copy-initialization context (but
29907 not copy-list-initialization). */
29908 bool elided = false;
29909 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29910 {
29911 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29912 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29913 elided = true;
29914
29915 if (elided)
29916 {
29917 /* Found a nonconverting guide, prune the candidates. */
29918 tree pruned = NULL_TREE;
29919 for (lkp_iterator iter (cands); iter; ++iter)
29920 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29921 pruned = lookup_add (*iter, pruned);
29922
29923 cands = pruned;
29924 }
29925 }
29926
29927 if (!any_dguides_p)
29928 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29929 cands = lookup_add (guide, cands);
29930
29931 tree fndecl = error_mark_node;
29932
29933 /* If this is list-initialization and the class has a list constructor, first
29934 try deducing from the list as a single argument, as [over.match.list]. */
29935 tree list_cands = NULL_TREE;
29936 if (try_list_ctor && cands)
29937 for (lkp_iterator iter (cands); iter; ++iter)
29938 {
29939 tree dg = *iter;
29940 if (is_list_ctor (dg))
29941 list_cands = lookup_add (dg, list_cands);
29942 }
29943 if (list_cands)
29944 {
29945 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
29946
29947 if (fndecl == error_mark_node)
29948 {
29949 /* That didn't work, now try treating the list as a sequence of
29950 arguments. */
29951 release_tree_vector (args);
29952 args = make_tree_vector_from_ctor (init);
29953 }
29954 }
29955
29956 if (elided && !cands)
29957 {
29958 error ("cannot deduce template arguments for copy-initialization"
29959 " of %qT, as it has no non-explicit deduction guides or "
29960 "user-declared constructors", type);
29961 return error_mark_node;
29962 }
29963 else if (!cands && fndecl == error_mark_node)
29964 {
29965 error ("cannot deduce template arguments of %qT, as it has no viable "
29966 "deduction guides", type);
29967 return error_mark_node;
29968 }
29969
29970 if (fndecl == error_mark_node)
29971 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
29972
29973 if (fndecl == error_mark_node)
29974 {
29975 if (complain & tf_warning_or_error)
29976 {
29977 error ("class template argument deduction failed:");
29978 perform_dguide_overload_resolution (cands, args, complain);
29979 if (elided)
29980 inform (input_location, "explicit deduction guides not considered "
29981 "for copy-initialization");
29982 }
29983 return error_mark_node;
29984 }
29985 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29986 constructor is chosen, the initialization is ill-formed. */
29987 else if (flags & LOOKUP_ONLYCONVERTING)
29988 {
29989 if (DECL_NONCONVERTING_P (fndecl))
29990 {
29991 if (complain & tf_warning_or_error)
29992 {
29993 // TODO: Pass down location from cp_finish_decl.
29994 error ("class template argument deduction for %qT failed: "
29995 "explicit deduction guide selected in "
29996 "copy-list-initialization", type);
29997 inform (DECL_SOURCE_LOCATION (fndecl),
29998 "explicit deduction guide declared here");
29999
30000 }
30001 return error_mark_node;
30002 }
30003 }
30004
30005 /* If CTAD succeeded but the type doesn't have any explicit deduction
30006 guides, this deduction might not be what the user intended. */
30007 if (fndecl != error_mark_node && !any_dguides_p)
30008 {
30009 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
30010 || global_dc->dc_warn_system_headers)
30011 && warning (OPT_Wctad_maybe_unsupported,
30012 "%qT may not intend to support class template argument "
30013 "deduction", type))
30014 inform (input_location, "add a deduction guide to suppress this "
30015 "warning");
30016 }
30017
30018 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
30019 cp_type_quals (ptype));
30020 }
30021
30022 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
30023 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
30024 The CONTEXT determines the context in which auto deduction is performed
30025 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
30026
30027 OUTER_TARGS is used during template argument deduction (context == adc_unify)
30028 to properly substitute the result. It's also used in the adc_unify and
30029 adc_requirement contexts to communicate the necessary template arguments
30030 to satisfaction. OUTER_TARGS is ignored in other contexts.
30031
30032 For partial-concept-ids, extra args may be appended to the list of deduced
30033 template arguments prior to determining constraint satisfaction. */
30034
30035 tree
30036 do_auto_deduction (tree type, tree init, tree auto_node,
30037 tsubst_flags_t complain, auto_deduction_context context,
30038 tree outer_targs, int flags)
30039 {
30040 if (init == error_mark_node)
30041 return error_mark_node;
30042
30043 if (init && type_dependent_expression_p (init)
30044 && context != adc_unify)
30045 /* Defining a subset of type-dependent expressions that we can deduce
30046 from ahead of time isn't worth the trouble. */
30047 return type;
30048
30049 /* Similarly, we can't deduce from another undeduced decl. */
30050 if (init && undeduced_auto_decl (init))
30051 return type;
30052
30053 /* We may be doing a partial substitution, but we still want to replace
30054 auto_node. */
30055 complain &= ~tf_partial;
30056
30057 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
30058 /* C++17 class template argument deduction. */
30059 return do_class_deduction (type, tmpl, init, flags, complain);
30060
30061 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
30062 /* Nothing we can do with this, even in deduction context. */
30063 return type;
30064
30065 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
30066 with either a new invented type template parameter U or, if the
30067 initializer is a braced-init-list (8.5.4), with
30068 std::initializer_list<U>. */
30069 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30070 {
30071 if (!DIRECT_LIST_INIT_P (init))
30072 type = listify_autos (type, auto_node);
30073 else if (CONSTRUCTOR_NELTS (init) == 1)
30074 init = CONSTRUCTOR_ELT (init, 0)->value;
30075 else
30076 {
30077 if (complain & tf_warning_or_error)
30078 {
30079 if (permerror (input_location, "direct-list-initialization of "
30080 "%<auto%> requires exactly one element"))
30081 inform (input_location,
30082 "for deduction to %<std::initializer_list%>, use copy-"
30083 "list-initialization (i.e. add %<=%> before the %<{%>)");
30084 }
30085 type = listify_autos (type, auto_node);
30086 }
30087 }
30088
30089 if (type == error_mark_node)
30090 return error_mark_node;
30091
30092 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30093 {
30094 /* We don't recurse here because we can't deduce from a nested
30095 initializer_list. */
30096 if (CONSTRUCTOR_ELTS (init))
30097 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
30098 elt.value = resolve_nondeduced_context (elt.value, complain);
30099 }
30100 else
30101 init = resolve_nondeduced_context (init, complain);
30102
30103 tree targs;
30104 if (context == adc_decomp_type
30105 && auto_node == type
30106 && init != error_mark_node
30107 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
30108 {
30109 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
30110 and initializer has array type, deduce cv-qualified array type. */
30111 targs = make_tree_vec (1);
30112 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
30113 }
30114 else if (AUTO_IS_DECLTYPE (auto_node))
30115 {
30116 /* Figure out if INIT is an unparenthesized id-expression or an
30117 unparenthesized class member access. */
30118 tree stripped_init = tree_strip_any_location_wrapper (init);
30119 /* We need to be able to tell '(r)' and 'r' apart (when it's of
30120 reference type). Only the latter is an id-expression. */
30121 if (REFERENCE_REF_P (stripped_init)
30122 && !REF_PARENTHESIZED_P (stripped_init))
30123 stripped_init = TREE_OPERAND (stripped_init, 0);
30124 const bool id = (DECL_P (stripped_init)
30125 || ((TREE_CODE (stripped_init) == COMPONENT_REF
30126 || TREE_CODE (stripped_init) == SCOPE_REF)
30127 && !REF_PARENTHESIZED_P (stripped_init)));
30128 tree deduced = finish_decltype_type (init, id, complain);
30129 deduced = canonicalize_type_argument (deduced, complain);
30130 if (deduced == error_mark_node)
30131 return error_mark_node;
30132 targs = make_tree_vec (1);
30133 TREE_VEC_ELT (targs, 0) = deduced;
30134 }
30135 else
30136 {
30137 if (error_operand_p (init))
30138 return error_mark_node;
30139
30140 tree parms = build_tree_list (NULL_TREE, type);
30141 tree tparms;
30142
30143 if (flag_concepts_ts)
30144 tparms = extract_autos (type);
30145 else
30146 {
30147 tparms = make_tree_vec (1);
30148 TREE_VEC_ELT (tparms, 0)
30149 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
30150 }
30151
30152 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
30153 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
30154 DEDUCE_CALL,
30155 NULL, /*explain_p=*/false);
30156 if (val > 0)
30157 {
30158 if (processing_template_decl)
30159 /* Try again at instantiation time. */
30160 return type;
30161 if (type && type != error_mark_node
30162 && (complain & tf_error))
30163 /* If type is error_mark_node a diagnostic must have been
30164 emitted by now. Also, having a mention to '<type error>'
30165 in the diagnostic is not really useful to the user. */
30166 {
30167 if (cfun
30168 && FNDECL_USED_AUTO (current_function_decl)
30169 && (auto_node
30170 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
30171 && LAMBDA_FUNCTION_P (current_function_decl))
30172 error ("unable to deduce lambda return type from %qE", init);
30173 else
30174 error ("unable to deduce %qT from %qE", type, init);
30175 type_unification_real (tparms, targs, parms, &init, 1, 0,
30176 DEDUCE_CALL,
30177 NULL, /*explain_p=*/true);
30178 }
30179 return error_mark_node;
30180 }
30181 }
30182
30183 /* Check any placeholder constraints against the deduced type. */
30184 if (processing_template_decl && context == adc_unify)
30185 /* Constraints will be checked after deduction. */;
30186 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
30187 {
30188 if (processing_template_decl)
30189 {
30190 gcc_checking_assert (context == adc_variable_type
30191 || context == adc_return_type
30192 || context == adc_decomp_type);
30193 gcc_checking_assert (!type_dependent_expression_p (init));
30194 /* If the constraint is dependent, we need to wait until
30195 instantiation time to resolve the placeholder. */
30196 if (placeholder_type_constraint_dependent_p (constr))
30197 return type;
30198 }
30199
30200 if (context == adc_return_type
30201 || context == adc_variable_type
30202 || context == adc_decomp_type)
30203 if (tree fn = current_function_decl)
30204 if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
30205 {
30206 outer_targs = DECL_TEMPLATE_INFO (fn)
30207 ? DECL_TI_ARGS (fn) : NULL_TREE;
30208 if (LAMBDA_FUNCTION_P (fn))
30209 {
30210 /* As in satisfy_declaration_constraints. */
30211 tree regen_args = lambda_regenerating_args (fn);
30212 if (outer_targs)
30213 outer_targs = add_to_template_args (regen_args, outer_targs);
30214 else
30215 outer_targs = regen_args;
30216 }
30217 }
30218
30219 tree full_targs = add_to_template_args (outer_targs, targs);
30220
30221 /* HACK: Compensate for callers not always communicating all levels of
30222 outer template arguments by filling in the outermost missing levels
30223 with dummy levels before checking satisfaction. We'll still crash
30224 if the constraint depends on a template argument belonging to one of
30225 these missing levels, but this hack otherwise allows us to handle a
30226 large subset of possible constraints (including all non-dependent
30227 constraints). */
30228 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
30229 - TMPL_ARGS_DEPTH (full_targs)))
30230 {
30231 tree dummy_levels = make_tree_vec (missing_levels);
30232 for (int i = 0; i < missing_levels; ++i)
30233 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
30234 full_targs = add_to_template_args (dummy_levels, full_targs);
30235 }
30236
30237 if (!constraints_satisfied_p (auto_node, full_targs))
30238 {
30239 if (complain & tf_warning_or_error)
30240 {
30241 auto_diagnostic_group d;
30242 switch (context)
30243 {
30244 case adc_unspecified:
30245 case adc_unify:
30246 error("placeholder constraints not satisfied");
30247 break;
30248 case adc_variable_type:
30249 case adc_decomp_type:
30250 error ("deduced initializer does not satisfy "
30251 "placeholder constraints");
30252 break;
30253 case adc_return_type:
30254 error ("deduced return type does not satisfy "
30255 "placeholder constraints");
30256 break;
30257 case adc_requirement:
30258 error ("deduced expression type does not satisfy "
30259 "placeholder constraints");
30260 break;
30261 }
30262 diagnose_constraints (input_location, auto_node, full_targs);
30263 }
30264 return error_mark_node;
30265 }
30266 }
30267
30268 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
30269 /* The outer template arguments are already substituted into type
30270 (but we still may have used them for constraint checking above). */;
30271 else if (context == adc_unify)
30272 targs = add_to_template_args (outer_targs, targs);
30273 else if (processing_template_decl)
30274 targs = add_to_template_args (current_template_args (), targs);
30275 return tsubst (type, targs, complain, NULL_TREE);
30276 }
30277
30278 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
30279 result. */
30280
30281 tree
30282 splice_late_return_type (tree type, tree late_return_type)
30283 {
30284 if (late_return_type)
30285 {
30286 gcc_assert (is_auto (type) || seen_error ());
30287 return late_return_type;
30288 }
30289
30290 if (tree auto_node = find_type_usage (type, is_auto))
30291 if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
30292 {
30293 /* In an abbreviated function template we didn't know we were dealing
30294 with a function template when we saw the auto return type, so rebuild
30295 the return type using an auto with the correct level. */
30296 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
30297 tree auto_vec = make_tree_vec (1);
30298 TREE_VEC_ELT (auto_vec, 0) = new_auto;
30299 tree targs = add_outermost_template_args (current_template_args (),
30300 auto_vec);
30301 /* Also rebuild the constraint info in terms of the new auto. */
30302 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
30303 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
30304 = build_tree_list (current_template_parms,
30305 tsubst_constraint (TREE_VALUE (ci), targs,
30306 tf_none, NULL_TREE));
30307 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
30308 return tsubst (type, targs, tf_none, NULL_TREE);
30309 }
30310 return type;
30311 }
30312
30313 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
30314 'decltype(auto)' or a deduced class template. */
30315
30316 bool
30317 is_auto (const_tree type)
30318 {
30319 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
30320 && (TYPE_IDENTIFIER (type) == auto_identifier
30321 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
30322 return true;
30323 else
30324 return false;
30325 }
30326
30327 /* for_each_template_parm callback for type_uses_auto. */
30328
30329 int
30330 is_auto_r (tree tp, void */*data*/)
30331 {
30332 return is_auto (tp);
30333 }
30334
30335 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
30336 a use of `auto'. Returns NULL_TREE otherwise. */
30337
30338 tree
30339 type_uses_auto (tree type)
30340 {
30341 if (type == NULL_TREE)
30342 return NULL_TREE;
30343 else if (flag_concepts_ts)
30344 {
30345 /* The Concepts TS allows multiple autos in one type-specifier; just
30346 return the first one we find, do_auto_deduction will collect all of
30347 them. */
30348 if (uses_template_parms (type))
30349 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
30350 /*visited*/NULL, /*nondeduced*/false);
30351 else
30352 return NULL_TREE;
30353 }
30354 else
30355 return find_type_usage (type, is_auto);
30356 }
30357
30358 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
30359 concepts are enabled, auto is acceptable in template arguments, but
30360 only when TEMPL identifies a template class. Return TRUE if any
30361 such errors were reported. */
30362
30363 bool
30364 check_auto_in_tmpl_args (tree tmpl, tree args)
30365 {
30366 if (!flag_concepts_ts)
30367 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
30368 have already been rejected by the parser more generally. */
30369 return false;
30370
30371 /* If there were previous errors, nevermind. */
30372 if (!args || TREE_CODE (args) != TREE_VEC)
30373 return false;
30374
30375 /* If TMPL is an identifier, we're parsing and we can't tell yet
30376 whether TMPL is supposed to be a type, a function or a variable.
30377 We'll only be able to tell during template substitution, so we
30378 expect to be called again then. If concepts are enabled and we
30379 know we have a type, we're ok. */
30380 if (identifier_p (tmpl)
30381 || (DECL_P (tmpl)
30382 && (DECL_TYPE_TEMPLATE_P (tmpl)
30383 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
30384 return false;
30385
30386 /* Quickly search for any occurrences of auto; usually there won't
30387 be any, and then we'll avoid allocating the vector. */
30388 if (!type_uses_auto (args))
30389 return false;
30390
30391 bool errors = false;
30392
30393 tree vec = extract_autos (args);
30394 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
30395 {
30396 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
30397 error_at (DECL_SOURCE_LOCATION (xauto),
30398 "invalid use of %qT in template argument", xauto);
30399 errors = true;
30400 }
30401
30402 return errors;
30403 }
30404
30405 /* Recursively walk over && expressions searching for EXPR. Return a reference
30406 to that expression. */
30407
30408 static tree *find_template_requirement (tree *t, tree key)
30409 {
30410 if (*t == key)
30411 return t;
30412 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
30413 {
30414 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
30415 return p;
30416 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
30417 return p;
30418 }
30419 return 0;
30420 }
30421
30422 /* Convert the generic type parameters in PARM that match the types given in the
30423 range [START_IDX, END_IDX) from the current_template_parms into generic type
30424 packs. */
30425
30426 tree
30427 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
30428 {
30429 tree current = current_template_parms;
30430 int depth = TMPL_PARMS_DEPTH (current);
30431 current = INNERMOST_TEMPLATE_PARMS (current);
30432 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
30433
30434 for (int i = 0; i < start_idx; ++i)
30435 TREE_VEC_ELT (replacement, i)
30436 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
30437
30438 for (int i = start_idx; i < end_idx; ++i)
30439 {
30440 /* Create a distinct parameter pack type from the current parm and add it
30441 to the replacement args to tsubst below into the generic function
30442 parameter. */
30443 tree node = TREE_VEC_ELT (current, i);
30444 tree o = TREE_TYPE (TREE_VALUE (node));
30445 tree t = copy_type (o);
30446 TEMPLATE_TYPE_PARM_INDEX (t)
30447 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
30448 t, 0, 0, tf_none);
30449 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
30450 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
30451 TYPE_MAIN_VARIANT (t) = t;
30452 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
30453 TYPE_CANONICAL (t) = canonical_type_parameter (t);
30454 TREE_VEC_ELT (replacement, i) = t;
30455
30456 /* Replace the current template parameter with new pack. */
30457 TREE_VALUE (node) = TREE_CHAIN (t);
30458
30459 /* Surgically adjust the associated constraint of adjusted parameter
30460 and it's corresponding contribution to the current template
30461 requirements. */
30462 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
30463 {
30464 tree id = unpack_concept_check (constr);
30465 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
30466 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
30467 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
30468
30469 /* If there was a constraint, we also need to replace that in
30470 the template requirements, which we've already built. */
30471 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
30472 reqs = find_template_requirement (reqs, constr);
30473 *reqs = fold;
30474 }
30475 }
30476
30477 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
30478 TREE_VEC_ELT (replacement, i)
30479 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
30480
30481 /* If there are more levels then build up the replacement with the outer
30482 template parms. */
30483 if (depth > 1)
30484 replacement = add_to_template_args (template_parms_to_args
30485 (TREE_CHAIN (current_template_parms)),
30486 replacement);
30487
30488 return tsubst (parm, replacement, tf_none, NULL_TREE);
30489 }
30490
30491 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30492 0..N-1. */
30493
30494 void
30495 declare_integer_pack (void)
30496 {
30497 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
30498 build_function_type_list (integer_type_node,
30499 integer_type_node,
30500 NULL_TREE),
30501 NULL_TREE, ECF_CONST);
30502 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
30503 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
30504 CP_BUILT_IN_INTEGER_PACK);
30505 }
30506
30507 /* Walk the decl or type specialization table calling FN on each
30508 entry. */
30509
30510 void
30511 walk_specializations (bool decls_p,
30512 void (*fn) (bool decls_p, spec_entry *entry, void *data),
30513 void *data)
30514 {
30515 spec_hash_table *table = decls_p ? decl_specializations
30516 : type_specializations;
30517 spec_hash_table::iterator end (table->end ());
30518 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
30519 fn (decls_p, *iter, data);
30520 }
30521
30522 /* Lookup the specialization of *ELT, in the decl or type
30523 specialization table. Return the SPEC that's already there, or
30524 NULL if nothing. */
30525
30526 tree
30527 match_mergeable_specialization (bool decl_p, spec_entry *elt)
30528 {
30529 hash_table<spec_hasher> *specializations
30530 = decl_p ? decl_specializations : type_specializations;
30531 hashval_t hash = spec_hasher::hash (elt);
30532 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
30533
30534 if (slot)
30535 return (*slot)->spec;
30536
30537 return NULL_TREE;
30538 }
30539
30540 /* Return flags encoding whether SPEC is on the instantiation and/or
30541 specialization lists of TMPL. */
30542
30543 unsigned
30544 get_mergeable_specialization_flags (tree tmpl, tree decl)
30545 {
30546 unsigned flags = 0;
30547
30548 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
30549 inst; inst = TREE_CHAIN (inst))
30550 if (TREE_VALUE (inst) == decl)
30551 {
30552 flags |= 1;
30553 break;
30554 }
30555
30556 if (CLASS_TYPE_P (TREE_TYPE (decl))
30557 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
30558 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
30559 /* Only need to search if DECL is a partial specialization. */
30560 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
30561 part; part = TREE_CHAIN (part))
30562 if (TREE_VALUE (part) == decl)
30563 {
30564 flags |= 2;
30565 break;
30566 }
30567
30568 return flags;
30569 }
30570
30571 /* Add a new specialization described by SPEC. DECL is the
30572 maybe-template decl and FLAGS is as returned from
30573 get_mergeable_specialization_flags. */
30574
30575 void
30576 add_mergeable_specialization (bool decl_p, bool alias_p, spec_entry *elt,
30577 tree decl, unsigned flags)
30578 {
30579 hashval_t hash = spec_hasher::hash (elt);
30580 if (decl_p)
30581 {
30582 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
30583
30584 gcc_checking_assert (!*slot);
30585 auto entry = ggc_alloc<spec_entry> ();
30586 *entry = *elt;
30587 *slot = entry;
30588
30589 if (alias_p)
30590 {
30591 elt->spec = TREE_TYPE (elt->spec);
30592 gcc_checking_assert (elt->spec);
30593 }
30594 }
30595
30596 if (!decl_p || alias_p)
30597 {
30598 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
30599
30600 /* We don't distinguish different constrained partial type
30601 specializations, so there could be duplicates. Everything else
30602 must be new. */
30603 if (!(flags & 2 && *slot))
30604 {
30605 gcc_checking_assert (!*slot);
30606
30607 auto entry = ggc_alloc<spec_entry> ();
30608 *entry = *elt;
30609 *slot = entry;
30610 }
30611 }
30612
30613 if (flags & 1)
30614 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
30615 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
30616
30617 if (flags & 2)
30618 {
30619 /* A partial specialization. */
30620 tree cons = tree_cons (elt->args, decl,
30621 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
30622 TREE_TYPE (cons) = elt->spec;
30623 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
30624 }
30625 }
30626
30627 /* Set up the hash tables for template instantiations. */
30628
30629 void
30630 init_template_processing (void)
30631 {
30632 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
30633 type_specializations = hash_table<spec_hasher>::create_ggc (37);
30634
30635 if (cxx_dialect >= cxx11)
30636 declare_integer_pack ();
30637 }
30638
30639 /* Print stats about the template hash tables for -fstats. */
30640
30641 void
30642 print_template_statistics (void)
30643 {
30644 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
30645 "%f collisions\n", (long) decl_specializations->size (),
30646 (long) decl_specializations->elements (),
30647 decl_specializations->collisions ());
30648 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
30649 "%f collisions\n", (long) type_specializations->size (),
30650 (long) type_specializations->elements (),
30651 type_specializations->collisions ());
30652 }
30653
30654 #if CHECKING_P
30655
30656 namespace selftest {
30657
30658 /* Verify that build_non_dependent_expr () works, for various expressions,
30659 and that location wrappers don't affect the results. */
30660
30661 static void
30662 test_build_non_dependent_expr ()
30663 {
30664 location_t loc = BUILTINS_LOCATION;
30665
30666 /* Verify constants, without and with location wrappers. */
30667 tree int_cst = build_int_cst (integer_type_node, 42);
30668 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
30669
30670 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
30671 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
30672 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
30673
30674 tree string_lit = build_string (4, "foo");
30675 TREE_TYPE (string_lit) = char_array_type_node;
30676 string_lit = fix_string_type (string_lit);
30677 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
30678
30679 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
30680 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
30681 ASSERT_EQ (wrapped_string_lit,
30682 build_non_dependent_expr (wrapped_string_lit));
30683 }
30684
30685 /* Verify that type_dependent_expression_p () works correctly, even
30686 in the presence of location wrapper nodes. */
30687
30688 static void
30689 test_type_dependent_expression_p ()
30690 {
30691 location_t loc = BUILTINS_LOCATION;
30692
30693 tree name = get_identifier ("foo");
30694
30695 /* If no templates are involved, nothing is type-dependent. */
30696 gcc_assert (!processing_template_decl);
30697 ASSERT_FALSE (type_dependent_expression_p (name));
30698
30699 ++processing_template_decl;
30700
30701 /* Within a template, an unresolved name is always type-dependent. */
30702 ASSERT_TRUE (type_dependent_expression_p (name));
30703
30704 /* Ensure it copes with NULL_TREE and errors. */
30705 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
30706 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
30707
30708 /* A USING_DECL in a template should be type-dependent, even if wrapped
30709 with a location wrapper (PR c++/83799). */
30710 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
30711 TREE_TYPE (using_decl) = integer_type_node;
30712 ASSERT_TRUE (type_dependent_expression_p (using_decl));
30713 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
30714 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
30715 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
30716
30717 --processing_template_decl;
30718 }
30719
30720 /* Run all of the selftests within this file. */
30721
30722 void
30723 cp_pt_cc_tests ()
30724 {
30725 test_build_non_dependent_expr ();
30726 test_type_dependent_expression_p ();
30727 }
30728
30729 } // namespace selftest
30730
30731 #endif /* #if CHECKING_P */
30732
30733 #include "gt-cp-pt.h"
This page took 1.46819 seconds and 5 git commands to generate.