]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
c++: dependence of member noexcept-spec [PR104079]
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2021 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 (unification_kind_t, tree*, tree*,
158 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 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_CLASS_SCOPE_P (t))
238 push_nested_class (DECL_CONTEXT (t));
239 else
240 push_to_top_level ();
241
242 if (TREE_CODE (t) == FUNCTION_DECL)
243 {
244 vec_safe_push (saved_access_scope, current_function_decl);
245 current_function_decl = t;
246 }
247 }
248
249 /* Restore the scope set up by push_access_scope. T is the node we
250 are processing. */
251
252 void
253 pop_access_scope (tree t)
254 {
255 if (TREE_CODE (t) == FUNCTION_DECL)
256 current_function_decl = saved_access_scope->pop();
257
258 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
259 pop_nested_class ();
260 else
261 pop_from_top_level ();
262 }
263
264 /* Do any processing required when DECL (a member template
265 declaration) is finished. Returns the TEMPLATE_DECL corresponding
266 to DECL, unless it is a specialization, in which case the DECL
267 itself is returned. */
268
269 tree
270 finish_member_template_decl (tree decl)
271 {
272 if (decl == error_mark_node)
273 return error_mark_node;
274
275 gcc_assert (DECL_P (decl));
276
277 if (TREE_CODE (decl) == TYPE_DECL)
278 {
279 tree type;
280
281 type = TREE_TYPE (decl);
282 if (type == error_mark_node)
283 return error_mark_node;
284 if (MAYBE_CLASS_TYPE_P (type)
285 && CLASSTYPE_TEMPLATE_INFO (type)
286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
287 {
288 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
289 check_member_template (tmpl);
290 return tmpl;
291 }
292 return NULL_TREE;
293 }
294 else if (TREE_CODE (decl) == FIELD_DECL)
295 error_at (DECL_SOURCE_LOCATION (decl),
296 "data member %qD cannot be a member template", decl);
297 else if (DECL_TEMPLATE_INFO (decl))
298 {
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
300 {
301 check_member_template (DECL_TI_TEMPLATE (decl));
302 return DECL_TI_TEMPLATE (decl);
303 }
304 else
305 return decl;
306 }
307 else
308 error_at (DECL_SOURCE_LOCATION (decl),
309 "invalid member template declaration %qD", decl);
310
311 return error_mark_node;
312 }
313
314 /* Create a template info node. */
315
316 tree
317 build_template_info (tree template_decl, tree template_args)
318 {
319 tree result = make_node (TEMPLATE_INFO);
320 TI_TEMPLATE (result) = template_decl;
321 TI_ARGS (result) = template_args;
322 return result;
323 }
324
325 /* Return the template info node corresponding to T, whatever T is. */
326
327 tree
328 get_template_info (const_tree t)
329 {
330 tree tinfo = NULL_TREE;
331
332 if (!t || t == error_mark_node)
333 return NULL;
334
335 if (TREE_CODE (t) == NAMESPACE_DECL
336 || TREE_CODE (t) == PARM_DECL)
337 return NULL;
338
339 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
340 tinfo = DECL_TEMPLATE_INFO (t);
341
342 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
343 t = TREE_TYPE (t);
344
345 if (OVERLOAD_TYPE_P (t))
346 tinfo = TYPE_TEMPLATE_INFO (t);
347 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
348 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
349
350 return tinfo;
351 }
352
353 /* Returns the template nesting level of the indicated class TYPE.
354
355 For example, in:
356 template <class T>
357 struct A
358 {
359 template <class U>
360 struct B {};
361 };
362
363 A<T>::B<U> has depth two, while A<T> has depth one.
364 Both A<T>::B<int> and A<int>::B<U> have depth one, if
365 they are instantiations, not specializations.
366
367 This function is guaranteed to return 0 if passed NULL_TREE so
368 that, for example, `template_class_depth (current_class_type)' is
369 always safe. */
370
371 int
372 template_class_depth (tree type)
373 {
374 int depth;
375
376 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
377 {
378 tree tinfo = get_template_info (type);
379
380 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
381 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
382 ++depth;
383
384 if (DECL_P (type))
385 {
386 if (tree fctx = DECL_FRIEND_CONTEXT (type))
387 type = fctx;
388 else
389 type = CP_DECL_CONTEXT (type);
390 }
391 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
395 }
396
397 return depth;
398 }
399
400 /* Return TRUE if NODE instantiates a template that has arguments of
401 its own, be it directly a primary template or indirectly through a
402 partial specializations. */
403 static bool
404 instantiates_primary_template_p (tree node)
405 {
406 tree tinfo = get_template_info (node);
407 if (!tinfo)
408 return false;
409
410 tree tmpl = TI_TEMPLATE (tinfo);
411 if (PRIMARY_TEMPLATE_P (tmpl))
412 return true;
413
414 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
415 return false;
416
417 /* So now we know we have a specialization, but it could be a full
418 or a partial specialization. To tell which, compare the depth of
419 its template arguments with those of its context. */
420
421 tree ctxt = DECL_CONTEXT (tmpl);
422 tree ctinfo = get_template_info (ctxt);
423 if (!ctinfo)
424 return true;
425
426 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
427 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
428 }
429
430 /* Subroutine of maybe_begin_member_template_processing.
431 Returns true if processing DECL needs us to push template parms. */
432
433 static bool
434 inline_needs_template_parms (tree decl, bool nsdmi)
435 {
436 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
437 return false;
438
439 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
440 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
441 }
442
443 /* Subroutine of maybe_begin_member_template_processing.
444 Push the template parms in PARMS, starting from LEVELS steps into the
445 chain, and ending at the beginning, since template parms are listed
446 innermost first. */
447
448 static void
449 push_inline_template_parms_recursive (tree parmlist, int levels)
450 {
451 tree parms = TREE_VALUE (parmlist);
452 int i;
453
454 if (levels > 1)
455 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
456
457 ++processing_template_decl;
458 current_template_parms
459 = tree_cons (size_int (processing_template_decl),
460 parms, current_template_parms);
461 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
462
463 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
464 NULL);
465 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
466 {
467 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
468
469 if (error_operand_p (parm))
470 continue;
471
472 gcc_assert (DECL_P (parm));
473
474 switch (TREE_CODE (parm))
475 {
476 case TYPE_DECL:
477 case TEMPLATE_DECL:
478 pushdecl (parm);
479 break;
480
481 case PARM_DECL:
482 /* Push the CONST_DECL. */
483 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
484 break;
485
486 default:
487 gcc_unreachable ();
488 }
489 }
490 }
491
492 /* Restore the template parameter context for a member template, a
493 friend template defined in a class definition, or a non-template
494 member of template class. */
495
496 void
497 maybe_begin_member_template_processing (tree decl)
498 {
499 tree parms;
500 int levels = 0;
501 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
502
503 if (nsdmi)
504 {
505 tree ctx = DECL_CONTEXT (decl);
506 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
507 /* Disregard full specializations (c++/60999). */
508 && uses_template_parms (ctx)
509 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
510 }
511
512 if (inline_needs_template_parms (decl, nsdmi))
513 {
514 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
515 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
516
517 if (DECL_TEMPLATE_SPECIALIZATION (decl))
518 {
519 --levels;
520 parms = TREE_CHAIN (parms);
521 }
522
523 push_inline_template_parms_recursive (parms, levels);
524 }
525
526 /* Remember how many levels of template parameters we pushed so that
527 we can pop them later. */
528 inline_parm_levels.safe_push (levels);
529 }
530
531 /* Undo the effects of maybe_begin_member_template_processing. */
532
533 void
534 maybe_end_member_template_processing (void)
535 {
536 int i;
537 int last;
538
539 if (inline_parm_levels.length () == 0)
540 return;
541
542 last = inline_parm_levels.pop ();
543 for (i = 0; i < last; ++i)
544 {
545 --processing_template_decl;
546 current_template_parms = TREE_CHAIN (current_template_parms);
547 poplevel (0, 0, 0);
548 }
549 }
550
551 /* Return a new template argument vector which contains all of ARGS,
552 but has as its innermost set of arguments the EXTRA_ARGS. */
553
554 tree
555 add_to_template_args (tree args, tree extra_args)
556 {
557 tree new_args;
558 int extra_depth;
559 int i;
560 int j;
561
562 if (args == NULL_TREE || extra_args == error_mark_node)
563 return extra_args;
564
565 extra_depth = TMPL_ARGS_DEPTH (extra_args);
566 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
567
568 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
569 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
570
571 for (j = 1; j <= extra_depth; ++j, ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
573
574 return new_args;
575 }
576
577 /* Like add_to_template_args, but only the outermost ARGS are added to
578 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
579 (EXTRA_ARGS) levels are added. This function is used to combine
580 the template arguments from a partial instantiation with the
581 template arguments used to attain the full instantiation from the
582 partial instantiation.
583
584 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
585
586 tree
587 add_outermost_template_args (tree args, tree extra_args)
588 {
589 tree new_args;
590
591 if (!args)
592 return extra_args;
593 if (TREE_CODE (args) == TEMPLATE_DECL)
594 {
595 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
596 args = TI_ARGS (ti);
597 }
598
599 /* If there are more levels of EXTRA_ARGS than there are ARGS,
600 something very fishy is going on. */
601 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
602
603 /* If *all* the new arguments will be the EXTRA_ARGS, just return
604 them. */
605 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
606 return extra_args;
607
608 /* For the moment, we make ARGS look like it contains fewer levels. */
609 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
610
611 new_args = add_to_template_args (args, extra_args);
612
613 /* Now, we restore ARGS to its full dimensions. */
614 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
615
616 return new_args;
617 }
618
619 /* Return the N levels of innermost template arguments from the ARGS. */
620
621 tree
622 get_innermost_template_args (tree args, int n)
623 {
624 tree new_args;
625 int extra_levels;
626 int i;
627
628 gcc_assert (n >= 0);
629
630 /* If N is 1, just return the innermost set of template arguments. */
631 if (n == 1)
632 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
633
634 /* If we're not removing anything, just return the arguments we were
635 given. */
636 extra_levels = TMPL_ARGS_DEPTH (args) - n;
637 gcc_assert (extra_levels >= 0);
638 if (extra_levels == 0)
639 return args;
640
641 /* Make a new set of arguments, not containing the outer arguments. */
642 new_args = make_tree_vec (n);
643 for (i = 1; i <= n; ++i)
644 SET_TMPL_ARGS_LEVEL (new_args, i,
645 TMPL_ARGS_LEVEL (args, i + extra_levels));
646
647 return new_args;
648 }
649
650 /* The inverse of get_innermost_template_args: Return all but the innermost
651 EXTRA_LEVELS levels of template arguments from the ARGS. */
652
653 static tree
654 strip_innermost_template_args (tree args, int extra_levels)
655 {
656 tree new_args;
657 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
658 int i;
659
660 gcc_assert (n >= 0);
661
662 /* If N is 1, just return the outermost set of template arguments. */
663 if (n == 1)
664 return TMPL_ARGS_LEVEL (args, 1);
665
666 /* If we're not removing anything, just return the arguments we were
667 given. */
668 gcc_assert (extra_levels >= 0);
669 if (extra_levels == 0)
670 return args;
671
672 /* Make a new set of arguments, not containing the inner arguments. */
673 new_args = make_tree_vec (n);
674 for (i = 1; i <= n; ++i)
675 SET_TMPL_ARGS_LEVEL (new_args, i,
676 TMPL_ARGS_LEVEL (args, i));
677
678 return new_args;
679 }
680
681 /* We've got a template header coming up; push to a new level for storing
682 the parms. */
683
684 void
685 begin_template_parm_list (void)
686 {
687 /* We use a non-tag-transparent scope here, which causes pushtag to
688 put tags in this scope, rather than in the enclosing class or
689 namespace scope. This is the right thing, since we want
690 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
691 global template class, push_template_decl handles putting the
692 TEMPLATE_DECL into top-level scope. For a nested template class,
693 e.g.:
694
695 template <class T> struct S1 {
696 template <class T> struct S2 {};
697 };
698
699 pushtag contains special code to insert the TEMPLATE_DECL for S2
700 at the right scope. */
701 begin_scope (sk_template_parms, NULL);
702 ++processing_template_decl;
703 ++processing_template_parmlist;
704 note_template_header (0);
705
706 /* Add a dummy parameter level while we process the parameter list. */
707 current_template_parms
708 = tree_cons (size_int (processing_template_decl),
709 make_tree_vec (0),
710 current_template_parms);
711 }
712
713 /* This routine is called when a specialization is declared. If it is
714 invalid to declare a specialization here, an error is reported and
715 false is returned, otherwise this routine will return true. */
716
717 static bool
718 check_specialization_scope (void)
719 {
720 tree scope = current_scope ();
721
722 /* [temp.expl.spec]
723
724 An explicit specialization shall be declared in the namespace of
725 which the template is a member, or, for member templates, in the
726 namespace of which the enclosing class or enclosing class
727 template is a member. An explicit specialization of a member
728 function, member class or static data member of a class template
729 shall be declared in the namespace of which the class template
730 is a member. */
731 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
732 {
733 error ("explicit specialization in non-namespace scope %qD", scope);
734 return false;
735 }
736
737 /* [temp.expl.spec]
738
739 In an explicit specialization declaration for a member of a class
740 template or a member template that appears in namespace scope,
741 the member template and some of its enclosing class templates may
742 remain unspecialized, except that the declaration shall not
743 explicitly specialize a class member template if its enclosing
744 class templates are not explicitly specialized as well. */
745 if (current_template_parms)
746 {
747 error ("enclosing class templates are not explicitly specialized");
748 return false;
749 }
750
751 return true;
752 }
753
754 /* We've just seen template <>. */
755
756 bool
757 begin_specialization (void)
758 {
759 begin_scope (sk_template_spec, NULL);
760 note_template_header (1);
761 return check_specialization_scope ();
762 }
763
764 /* Called at then end of processing a declaration preceded by
765 template<>. */
766
767 void
768 end_specialization (void)
769 {
770 finish_scope ();
771 reset_specialization ();
772 }
773
774 /* Any template <>'s that we have seen thus far are not referring to a
775 function specialization. */
776
777 void
778 reset_specialization (void)
779 {
780 processing_specialization = 0;
781 template_header_count = 0;
782 }
783
784 /* We've just seen a template header. If SPECIALIZATION is nonzero,
785 it was of the form template <>. */
786
787 static void
788 note_template_header (int specialization)
789 {
790 processing_specialization = specialization;
791 template_header_count++;
792 }
793
794 /* We're beginning an explicit instantiation. */
795
796 void
797 begin_explicit_instantiation (void)
798 {
799 gcc_assert (!processing_explicit_instantiation);
800 processing_explicit_instantiation = true;
801 }
802
803
804 void
805 end_explicit_instantiation (void)
806 {
807 gcc_assert (processing_explicit_instantiation);
808 processing_explicit_instantiation = false;
809 }
810
811 /* An explicit specialization or partial specialization of TMPL is being
812 declared. Check that the namespace in which the specialization is
813 occurring is permissible. Returns false iff it is invalid to
814 specialize TMPL in the current namespace. */
815
816 static bool
817 check_specialization_namespace (tree tmpl)
818 {
819 tree tpl_ns = decl_namespace_context (tmpl);
820
821 /* [tmpl.expl.spec]
822
823 An explicit specialization shall be declared in a namespace enclosing the
824 specialized template. An explicit specialization whose declarator-id is
825 not qualified shall be declared in the nearest enclosing namespace of the
826 template, or, if the namespace is inline (7.3.1), any namespace from its
827 enclosing namespace set. */
828 if (current_scope() != DECL_CONTEXT (tmpl)
829 && !at_namespace_scope_p ())
830 {
831 error ("specialization of %qD must appear at namespace scope", tmpl);
832 return false;
833 }
834
835 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
836 /* Same or enclosing namespace. */
837 return true;
838 else
839 {
840 auto_diagnostic_group d;
841 if (permerror (input_location,
842 "specialization of %qD in different namespace", tmpl))
843 inform (DECL_SOURCE_LOCATION (tmpl),
844 " from definition of %q#D", tmpl);
845 return false;
846 }
847 }
848
849 /* SPEC is an explicit instantiation. Check that it is valid to
850 perform this explicit instantiation in the current namespace. */
851
852 static void
853 check_explicit_instantiation_namespace (tree spec)
854 {
855 tree ns;
856
857 /* DR 275: An explicit instantiation shall appear in an enclosing
858 namespace of its template. */
859 ns = decl_namespace_context (spec);
860 if (!is_nested_namespace (current_namespace, ns))
861 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
862 "(which does not enclose namespace %qD)",
863 spec, current_namespace, ns);
864 }
865
866 /* Returns the type of a template specialization only if that
867 specialization needs to be defined. Otherwise (e.g., if the type has
868 already been defined), the function returns NULL_TREE. */
869
870 static tree
871 maybe_new_partial_specialization (tree type)
872 {
873 /* An implicit instantiation of an incomplete type implies
874 the definition of a new class template.
875
876 template<typename T>
877 struct S;
878
879 template<typename T>
880 struct S<T*>;
881
882 Here, S<T*> is an implicit instantiation of S whose type
883 is incomplete. */
884 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
885 return type;
886
887 /* It can also be the case that TYPE is a completed specialization.
888 Continuing the previous example, suppose we also declare:
889
890 template<typename T>
891 requires Integral<T>
892 struct S<T*>;
893
894 Here, S<T*> refers to the specialization S<T*> defined
895 above. However, we need to differentiate definitions because
896 we intend to define a new partial specialization. In this case,
897 we rely on the fact that the constraints are different for
898 this declaration than that above.
899
900 Note that we also get here for injected class names and
901 late-parsed template definitions. We must ensure that we
902 do not create new type declarations for those cases. */
903 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
904 {
905 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
906 tree args = CLASSTYPE_TI_ARGS (type);
907
908 /* If there are no template parameters, this cannot be a new
909 partial template specialization? */
910 if (!current_template_parms)
911 return NULL_TREE;
912
913 /* The injected-class-name is not a new partial specialization. */
914 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
915 return NULL_TREE;
916
917 /* If the constraints are not the same as those of the primary
918 then, we can probably create a new specialization. */
919 tree type_constr = current_template_constraints ();
920
921 if (type == TREE_TYPE (tmpl))
922 {
923 tree main_constr = get_constraints (tmpl);
924 if (equivalent_constraints (type_constr, main_constr))
925 return NULL_TREE;
926 }
927
928 /* Also, if there's a pre-existing specialization with matching
929 constraints, then this also isn't new. */
930 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
931 while (specs)
932 {
933 tree spec_tmpl = TREE_VALUE (specs);
934 tree spec_args = TREE_PURPOSE (specs);
935 tree spec_constr = get_constraints (spec_tmpl);
936 if (comp_template_args (args, spec_args)
937 && equivalent_constraints (type_constr, spec_constr))
938 return NULL_TREE;
939 specs = TREE_CHAIN (specs);
940 }
941
942 /* Create a new type node (and corresponding type decl)
943 for the newly declared specialization. */
944 tree t = make_class_type (TREE_CODE (type));
945 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
946 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
947
948 /* We only need a separate type node for storing the definition of this
949 partial specialization; uses of S<T*> are unconstrained, so all are
950 equivalent. So keep TYPE_CANONICAL the same. */
951 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
952
953 /* Build the corresponding type decl. */
954 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
955 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
956 DECL_SOURCE_LOCATION (d) = input_location;
957 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
958 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
959
960 set_instantiating_module (d);
961 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
962
963 return t;
964 }
965
966 return NULL_TREE;
967 }
968
969 /* The TYPE is being declared. If it is a template type, that means it
970 is a partial specialization. Do appropriate error-checking. */
971
972 tree
973 maybe_process_partial_specialization (tree type)
974 {
975 tree context;
976
977 if (type == error_mark_node)
978 return error_mark_node;
979
980 /* A lambda that appears in specialization context is not itself a
981 specialization. */
982 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
983 return type;
984
985 /* An injected-class-name is not a specialization. */
986 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
987 return type;
988
989 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
990 {
991 error ("name of class shadows template template parameter %qD",
992 TYPE_NAME (type));
993 return error_mark_node;
994 }
995
996 context = TYPE_CONTEXT (type);
997
998 if (TYPE_ALIAS_P (type))
999 {
1000 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1001
1002 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1003 error ("specialization of alias template %qD",
1004 TI_TEMPLATE (tinfo));
1005 else
1006 error ("explicit specialization of non-template %qT", type);
1007 return error_mark_node;
1008 }
1009 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1010 {
1011 /* This is for ordinary explicit specialization and partial
1012 specialization of a template class such as:
1013
1014 template <> class C<int>;
1015
1016 or:
1017
1018 template <class T> class C<T*>;
1019
1020 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1021
1022 if (tree t = maybe_new_partial_specialization (type))
1023 {
1024 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1025 && !at_namespace_scope_p ())
1026 return error_mark_node;
1027 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1028 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1029 if (processing_template_decl)
1030 {
1031 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1032 if (decl == error_mark_node)
1033 return error_mark_node;
1034 return TREE_TYPE (decl);
1035 }
1036 }
1037 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1038 error ("specialization of %qT after instantiation", type);
1039 else if (errorcount && !processing_specialization
1040 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1041 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1042 /* Trying to define a specialization either without a template<> header
1043 or in an inappropriate place. We've already given an error, so just
1044 bail now so we don't actually define the specialization. */
1045 return error_mark_node;
1046 }
1047 else if (CLASS_TYPE_P (type)
1048 && !CLASSTYPE_USE_TEMPLATE (type)
1049 && CLASSTYPE_TEMPLATE_INFO (type)
1050 && context && CLASS_TYPE_P (context)
1051 && CLASSTYPE_TEMPLATE_INFO (context))
1052 {
1053 /* This is for an explicit specialization of member class
1054 template according to [temp.expl.spec/18]:
1055
1056 template <> template <class U> class C<int>::D;
1057
1058 The context `C<int>' must be an implicit instantiation.
1059 Otherwise this is just a member class template declared
1060 earlier like:
1061
1062 template <> class C<int> { template <class U> class D; };
1063 template <> template <class U> class C<int>::D;
1064
1065 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1066 while in the second case, `C<int>::D' is a primary template
1067 and `C<T>::D' may not exist. */
1068
1069 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1070 && !COMPLETE_TYPE_P (type))
1071 {
1072 tree t;
1073 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1074
1075 if (current_namespace
1076 != decl_namespace_context (tmpl))
1077 {
1078 if (permerror (input_location,
1079 "specialization of %qD in different namespace",
1080 type))
1081 inform (DECL_SOURCE_LOCATION (tmpl),
1082 "from definition of %q#D", tmpl);
1083 }
1084
1085 /* Check for invalid specialization after instantiation:
1086
1087 template <> template <> class C<int>::D<int>;
1088 template <> template <class U> class C<int>::D; */
1089
1090 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1091 t; t = TREE_CHAIN (t))
1092 {
1093 tree inst = TREE_VALUE (t);
1094 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1095 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1096 {
1097 /* We already have a full specialization of this partial
1098 instantiation, or a full specialization has been
1099 looked up but not instantiated. Reassign it to the
1100 new member specialization template. */
1101 spec_entry elt;
1102 spec_entry *entry;
1103
1104 elt.tmpl = most_general_template (tmpl);
1105 elt.args = CLASSTYPE_TI_ARGS (inst);
1106 elt.spec = inst;
1107
1108 type_specializations->remove_elt (&elt);
1109
1110 elt.tmpl = tmpl;
1111 CLASSTYPE_TI_ARGS (inst)
1112 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1113
1114 spec_entry **slot
1115 = type_specializations->find_slot (&elt, INSERT);
1116 entry = ggc_alloc<spec_entry> ();
1117 *entry = elt;
1118 *slot = entry;
1119 }
1120 else
1121 /* But if we've had an implicit instantiation, that's a
1122 problem ([temp.expl.spec]/6). */
1123 error ("specialization %qT after instantiation %qT",
1124 type, inst);
1125 }
1126
1127 /* Mark TYPE as a specialization. And as a result, we only
1128 have one level of template argument for the innermost
1129 class template. */
1130 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1131 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1132 CLASSTYPE_TI_ARGS (type)
1133 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1134 }
1135 }
1136 else if (processing_specialization)
1137 {
1138 /* Someday C++0x may allow for enum template specialization. */
1139 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1140 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1141 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1142 "of %qD not allowed by ISO C++", type);
1143 else
1144 {
1145 error ("explicit specialization of non-template %qT", type);
1146 return error_mark_node;
1147 }
1148 }
1149
1150 return type;
1151 }
1152
1153 /* Returns nonzero if we can optimize the retrieval of specializations
1154 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1155 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1156
1157 static inline bool
1158 optimize_specialization_lookup_p (tree tmpl)
1159 {
1160 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1161 && DECL_CLASS_SCOPE_P (tmpl)
1162 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1163 parameter. */
1164 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1165 /* The optimized lookup depends on the fact that the
1166 template arguments for the member function template apply
1167 purely to the containing class, which is not true if the
1168 containing class is an explicit or partial
1169 specialization. */
1170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1171 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1172 && !DECL_CONV_FN_P (tmpl)
1173 /* It is possible to have a template that is not a member
1174 template and is not a member of a template class:
1175
1176 template <typename T>
1177 struct S { friend A::f(); };
1178
1179 Here, the friend function is a template, but the context does
1180 not have template information. The optimized lookup relies
1181 on having ARGS be the template arguments for both the class
1182 and the function template. */
1183 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1184 }
1185
1186 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1187 gone through coerce_template_parms by now. */
1188
1189 static void
1190 verify_unstripped_args_1 (tree inner)
1191 {
1192 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1193 {
1194 tree arg = TREE_VEC_ELT (inner, i);
1195 if (TREE_CODE (arg) == TEMPLATE_DECL)
1196 /* OK */;
1197 else if (TYPE_P (arg))
1198 gcc_assert (strip_typedefs (arg, NULL) == arg);
1199 else if (ARGUMENT_PACK_P (arg))
1200 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1201 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1202 /* Allow typedefs on the type of a non-type argument, since a
1203 parameter can have them. */;
1204 else
1205 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1206 }
1207 }
1208
1209 static void
1210 verify_unstripped_args (tree args)
1211 {
1212 ++processing_template_decl;
1213 if (!any_dependent_template_arguments_p (args))
1214 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1215 --processing_template_decl;
1216 }
1217
1218 /* Retrieve the specialization (in the sense of [temp.spec] - a
1219 specialization is either an instantiation or an explicit
1220 specialization) of TMPL for the given template ARGS. If there is
1221 no such specialization, return NULL_TREE. The ARGS are a vector of
1222 arguments, or a vector of vectors of arguments, in the case of
1223 templates with more than one level of parameters.
1224
1225 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1226 then we search for a partial specialization matching ARGS. This
1227 parameter is ignored if TMPL is not a class template.
1228
1229 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1230 result is a NONTYPE_ARGUMENT_PACK. */
1231
1232 static tree
1233 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1234 {
1235 if (tmpl == NULL_TREE)
1236 return NULL_TREE;
1237
1238 if (args == error_mark_node)
1239 return NULL_TREE;
1240
1241 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1242 || TREE_CODE (tmpl) == FIELD_DECL);
1243
1244 /* There should be as many levels of arguments as there are
1245 levels of parameters. */
1246 gcc_assert (TMPL_ARGS_DEPTH (args)
1247 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1248 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1249 : template_class_depth (DECL_CONTEXT (tmpl))));
1250
1251 if (flag_checking)
1252 verify_unstripped_args (args);
1253
1254 /* Lambda functions in templates aren't instantiated normally, but through
1255 tsubst_lambda_expr. */
1256 if (lambda_fn_in_template_p (tmpl))
1257 return NULL_TREE;
1258
1259 if (optimize_specialization_lookup_p (tmpl))
1260 {
1261 /* The template arguments actually apply to the containing
1262 class. Find the class specialization with those
1263 arguments. */
1264 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1265 tree class_specialization
1266 = retrieve_specialization (class_template, args, 0);
1267 if (!class_specialization)
1268 return NULL_TREE;
1269
1270 /* Find the instance of TMPL. */
1271 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1272 for (ovl_iterator iter (fns); iter; ++iter)
1273 {
1274 tree fn = *iter;
1275 if (tree ti = get_template_info (fn))
1276 if (TI_TEMPLATE (ti) == tmpl
1277 /* using-declarations can bring in a different
1278 instantiation of tmpl as a member of a different
1279 instantiation of tmpl's class. We don't want those
1280 here. */
1281 && DECL_CONTEXT (fn) == class_specialization)
1282 return fn;
1283 }
1284 return NULL_TREE;
1285 }
1286 else
1287 {
1288 spec_entry *found;
1289 spec_entry elt;
1290 spec_hash_table *specializations;
1291
1292 elt.tmpl = tmpl;
1293 elt.args = args;
1294 elt.spec = NULL_TREE;
1295
1296 if (DECL_CLASS_TEMPLATE_P (tmpl))
1297 specializations = type_specializations;
1298 else
1299 specializations = decl_specializations;
1300
1301 if (hash == 0)
1302 hash = spec_hasher::hash (&elt);
1303 found = specializations->find_with_hash (&elt, hash);
1304 if (found)
1305 return found->spec;
1306 }
1307
1308 return NULL_TREE;
1309 }
1310
1311 /* Like retrieve_specialization, but for local declarations. */
1312
1313 tree
1314 retrieve_local_specialization (tree tmpl)
1315 {
1316 if (local_specializations == NULL)
1317 return NULL_TREE;
1318
1319 tree *slot = local_specializations->get (tmpl);
1320 return slot ? *slot : NULL_TREE;
1321 }
1322
1323 /* Returns nonzero iff DECL is a specialization of TMPL. */
1324
1325 int
1326 is_specialization_of (tree decl, tree tmpl)
1327 {
1328 tree t;
1329
1330 if (TREE_CODE (decl) == FUNCTION_DECL)
1331 {
1332 for (t = decl;
1333 t != NULL_TREE;
1334 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1335 if (t == tmpl)
1336 return 1;
1337 }
1338 else
1339 {
1340 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1341
1342 for (t = TREE_TYPE (decl);
1343 t != NULL_TREE;
1344 t = CLASSTYPE_USE_TEMPLATE (t)
1345 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1346 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1347 return 1;
1348 }
1349
1350 return 0;
1351 }
1352
1353 /* Returns nonzero iff DECL is a specialization of friend declaration
1354 FRIEND_DECL according to [temp.friend]. */
1355
1356 bool
1357 is_specialization_of_friend (tree decl, tree friend_decl)
1358 {
1359 bool need_template = true;
1360 int template_depth;
1361
1362 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1363 || TREE_CODE (decl) == TYPE_DECL);
1364
1365 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1366 of a template class, we want to check if DECL is a specialization
1367 if this. */
1368 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1369 && DECL_TEMPLATE_INFO (friend_decl)
1370 && !DECL_USE_TEMPLATE (friend_decl))
1371 {
1372 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1373 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1374 need_template = false;
1375 }
1376 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1377 && !PRIMARY_TEMPLATE_P (friend_decl))
1378 need_template = false;
1379
1380 /* There is nothing to do if this is not a template friend. */
1381 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1382 return false;
1383
1384 if (is_specialization_of (decl, friend_decl))
1385 return true;
1386
1387 /* [temp.friend/6]
1388 A member of a class template may be declared to be a friend of a
1389 non-template class. In this case, the corresponding member of
1390 every specialization of the class template is a friend of the
1391 class granting friendship.
1392
1393 For example, given a template friend declaration
1394
1395 template <class T> friend void A<T>::f();
1396
1397 the member function below is considered a friend
1398
1399 template <> struct A<int> {
1400 void f();
1401 };
1402
1403 For this type of template friend, TEMPLATE_DEPTH below will be
1404 nonzero. To determine if DECL is a friend of FRIEND, we first
1405 check if the enclosing class is a specialization of another. */
1406
1407 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1408 if (template_depth
1409 && DECL_CLASS_SCOPE_P (decl)
1410 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1411 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1412 {
1413 /* Next, we check the members themselves. In order to handle
1414 a few tricky cases, such as when FRIEND_DECL's are
1415
1416 template <class T> friend void A<T>::g(T t);
1417 template <class T> template <T t> friend void A<T>::h();
1418
1419 and DECL's are
1420
1421 void A<int>::g(int);
1422 template <int> void A<int>::h();
1423
1424 we need to figure out ARGS, the template arguments from
1425 the context of DECL. This is required for template substitution
1426 of `T' in the function parameter of `g' and template parameter
1427 of `h' in the above examples. Here ARGS corresponds to `int'. */
1428
1429 tree context = DECL_CONTEXT (decl);
1430 tree args = NULL_TREE;
1431 int current_depth = 0;
1432
1433 while (current_depth < template_depth)
1434 {
1435 if (CLASSTYPE_TEMPLATE_INFO (context))
1436 {
1437 if (current_depth == 0)
1438 args = TYPE_TI_ARGS (context);
1439 else
1440 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1441 current_depth++;
1442 }
1443 context = TYPE_CONTEXT (context);
1444 }
1445
1446 if (TREE_CODE (decl) == FUNCTION_DECL)
1447 {
1448 bool is_template;
1449 tree friend_type;
1450 tree decl_type;
1451 tree friend_args_type;
1452 tree decl_args_type;
1453
1454 /* Make sure that both DECL and FRIEND_DECL are templates or
1455 non-templates. */
1456 is_template = DECL_TEMPLATE_INFO (decl)
1457 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1458 if (need_template ^ is_template)
1459 return false;
1460 else if (is_template)
1461 {
1462 /* If both are templates, check template parameter list. */
1463 tree friend_parms
1464 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1465 args, tf_none);
1466 if (!comp_template_parms
1467 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1468 friend_parms))
1469 return false;
1470
1471 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1472 }
1473 else
1474 decl_type = TREE_TYPE (decl);
1475
1476 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1477 tf_none, NULL_TREE);
1478 if (friend_type == error_mark_node)
1479 return false;
1480
1481 /* Check if return types match. */
1482 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1483 return false;
1484
1485 /* Check if function parameter types match, ignoring the
1486 `this' parameter. */
1487 friend_args_type = TYPE_ARG_TYPES (friend_type);
1488 decl_args_type = TYPE_ARG_TYPES (decl_type);
1489 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1490 friend_args_type = TREE_CHAIN (friend_args_type);
1491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1492 decl_args_type = TREE_CHAIN (decl_args_type);
1493
1494 return compparms (decl_args_type, friend_args_type);
1495 }
1496 else
1497 {
1498 /* DECL is a TYPE_DECL */
1499 bool is_template;
1500 tree decl_type = TREE_TYPE (decl);
1501
1502 /* Make sure that both DECL and FRIEND_DECL are templates or
1503 non-templates. */
1504 is_template
1505 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1506 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1507
1508 if (need_template ^ is_template)
1509 return false;
1510 else if (is_template)
1511 {
1512 tree friend_parms;
1513 /* If both are templates, check the name of the two
1514 TEMPLATE_DECL's first because is_friend didn't. */
1515 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1516 != DECL_NAME (friend_decl))
1517 return false;
1518
1519 /* Now check template parameter list. */
1520 friend_parms
1521 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1522 args, tf_none);
1523 return comp_template_parms
1524 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1525 friend_parms);
1526 }
1527 else
1528 return (DECL_NAME (decl)
1529 == DECL_NAME (friend_decl));
1530 }
1531 }
1532 return false;
1533 }
1534
1535 /* Register the specialization SPEC as a specialization of TMPL with
1536 the indicated ARGS. IS_FRIEND indicates whether the specialization
1537 is actually just a friend declaration. ATTRLIST is the list of
1538 attributes that the specialization is declared with or NULL when
1539 it isn't. Returns SPEC, or an equivalent prior declaration, if
1540 available.
1541
1542 We also store instantiations of field packs in the hash table, even
1543 though they are not themselves templates, to make lookup easier. */
1544
1545 static tree
1546 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1547 hashval_t hash)
1548 {
1549 tree fn;
1550 spec_entry **slot = NULL;
1551 spec_entry elt;
1552
1553 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1554 || (TREE_CODE (tmpl) == FIELD_DECL
1555 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1556
1557 if (TREE_CODE (spec) == FUNCTION_DECL
1558 && uses_template_parms (DECL_TI_ARGS (spec)))
1559 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1560 register it; we want the corresponding TEMPLATE_DECL instead.
1561 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1562 the more obvious `uses_template_parms (spec)' to avoid problems
1563 with default function arguments. In particular, given
1564 something like this:
1565
1566 template <class T> void f(T t1, T t = T())
1567
1568 the default argument expression is not substituted for in an
1569 instantiation unless and until it is actually needed. */
1570 return spec;
1571
1572 if (optimize_specialization_lookup_p (tmpl))
1573 /* We don't put these specializations in the hash table, but we might
1574 want to give an error about a mismatch. */
1575 fn = retrieve_specialization (tmpl, args, 0);
1576 else
1577 {
1578 elt.tmpl = tmpl;
1579 elt.args = args;
1580 elt.spec = spec;
1581
1582 if (hash == 0)
1583 hash = spec_hasher::hash (&elt);
1584
1585 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1586 if (*slot)
1587 fn = (*slot)->spec;
1588 else
1589 fn = NULL_TREE;
1590 }
1591
1592 /* We can sometimes try to re-register a specialization that we've
1593 already got. In particular, regenerate_decl_from_template calls
1594 duplicate_decls which will update the specialization list. But,
1595 we'll still get called again here anyhow. It's more convenient
1596 to simply allow this than to try to prevent it. */
1597 if (fn == spec)
1598 return spec;
1599 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1600 {
1601 if (DECL_TEMPLATE_INSTANTIATION (fn))
1602 {
1603 if (DECL_ODR_USED (fn)
1604 || DECL_EXPLICIT_INSTANTIATION (fn))
1605 {
1606 error ("specialization of %qD after instantiation",
1607 fn);
1608 return error_mark_node;
1609 }
1610 else
1611 {
1612 tree clone;
1613 /* This situation should occur only if the first
1614 specialization is an implicit instantiation, the
1615 second is an explicit specialization, and the
1616 implicit instantiation has not yet been used. That
1617 situation can occur if we have implicitly
1618 instantiated a member function and then specialized
1619 it later.
1620
1621 We can also wind up here if a friend declaration that
1622 looked like an instantiation turns out to be a
1623 specialization:
1624
1625 template <class T> void foo(T);
1626 class S { friend void foo<>(int) };
1627 template <> void foo(int);
1628
1629 We transform the existing DECL in place so that any
1630 pointers to it become pointers to the updated
1631 declaration.
1632
1633 If there was a definition for the template, but not
1634 for the specialization, we want this to look as if
1635 there were no definition, and vice versa. */
1636 DECL_INITIAL (fn) = NULL_TREE;
1637 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1638 /* The call to duplicate_decls will have applied
1639 [temp.expl.spec]:
1640
1641 An explicit specialization of a function template
1642 is inline only if it is explicitly declared to be,
1643 and independently of whether its function template
1644 is.
1645
1646 to the primary function; now copy the inline bits to
1647 the various clones. */
1648 FOR_EACH_CLONE (clone, fn)
1649 {
1650 DECL_DECLARED_INLINE_P (clone)
1651 = DECL_DECLARED_INLINE_P (fn);
1652 DECL_SOURCE_LOCATION (clone)
1653 = DECL_SOURCE_LOCATION (fn);
1654 DECL_DELETED_FN (clone)
1655 = DECL_DELETED_FN (fn);
1656 }
1657 check_specialization_namespace (tmpl);
1658
1659 return fn;
1660 }
1661 }
1662 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1663 {
1664 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1665 if (dd == error_mark_node)
1666 /* We've already complained in duplicate_decls. */
1667 return error_mark_node;
1668
1669 if (dd == NULL_TREE && DECL_INITIAL (spec))
1670 /* Dup decl failed, but this is a new definition. Set the
1671 line number so any errors match this new
1672 definition. */
1673 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1674
1675 return fn;
1676 }
1677 }
1678 else if (fn)
1679 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1680
1681 /* A specialization must be declared in the same namespace as the
1682 template it is specializing. */
1683 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1684 && !check_specialization_namespace (tmpl))
1685 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1686
1687 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1688 {
1689 spec_entry *entry = ggc_alloc<spec_entry> ();
1690 gcc_assert (tmpl && args && spec);
1691 *entry = elt;
1692 *slot = entry;
1693 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1694 && PRIMARY_TEMPLATE_P (tmpl)
1695 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1696 || variable_template_p (tmpl))
1697 /* If TMPL is a forward declaration of a template function, keep a list
1698 of all specializations in case we need to reassign them to a friend
1699 template later in tsubst_friend_function.
1700
1701 Also keep a list of all variable template instantiations so that
1702 process_partial_specialization can check whether a later partial
1703 specialization would have used it. */
1704 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1705 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1706 }
1707
1708 return spec;
1709 }
1710
1711 /* Restricts tree and type comparisons. */
1712 int comparing_specializations;
1713 int comparing_dependent_aliases;
1714
1715 /* Returns true iff two spec_entry nodes are equivalent. */
1716
1717 bool
1718 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1719 {
1720 int equal;
1721
1722 ++comparing_specializations;
1723 ++comparing_dependent_aliases;
1724 ++processing_template_decl;
1725 equal = (e1->tmpl == e2->tmpl
1726 && comp_template_args (e1->args, e2->args));
1727 if (equal && flag_concepts
1728 /* tmpl could be a FIELD_DECL for a capture pack. */
1729 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1730 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1731 && uses_template_parms (e1->args))
1732 {
1733 /* Partial specializations of a variable template can be distinguished by
1734 constraints. */
1735 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1736 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1737 equal = equivalent_constraints (c1, c2);
1738 }
1739 --processing_template_decl;
1740 --comparing_dependent_aliases;
1741 --comparing_specializations;
1742
1743 return equal;
1744 }
1745
1746 /* Returns a hash for a template TMPL and template arguments ARGS. */
1747
1748 static hashval_t
1749 hash_tmpl_and_args (tree tmpl, tree args)
1750 {
1751 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1752 return iterative_hash_template_arg (args, val);
1753 }
1754
1755 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1756 ignoring SPEC. */
1757
1758 hashval_t
1759 spec_hasher::hash (spec_entry *e)
1760 {
1761 return hash_tmpl_and_args (e->tmpl, e->args);
1762 }
1763
1764 /* Recursively calculate a hash value for a template argument ARG, for use
1765 in the hash tables of template specializations. We must be
1766 careful to (at least) skip the same entities template_args_equal
1767 does. */
1768
1769 hashval_t
1770 iterative_hash_template_arg (tree arg, hashval_t val)
1771 {
1772 if (arg == NULL_TREE)
1773 return iterative_hash_object (arg, val);
1774
1775 if (!TYPE_P (arg))
1776 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1777 while (CONVERT_EXPR_P (arg)
1778 || TREE_CODE (arg) == NON_LVALUE_EXPR
1779 || class_nttp_const_wrapper_p (arg))
1780 arg = TREE_OPERAND (arg, 0);
1781
1782 enum tree_code code = TREE_CODE (arg);
1783
1784 val = iterative_hash_object (code, val);
1785
1786 switch (code)
1787 {
1788 case ARGUMENT_PACK_SELECT:
1789 gcc_unreachable ();
1790
1791 case ERROR_MARK:
1792 return val;
1793
1794 case IDENTIFIER_NODE:
1795 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1796
1797 case TREE_VEC:
1798 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1799 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1800 return val;
1801
1802 case TYPE_PACK_EXPANSION:
1803 case EXPR_PACK_EXPANSION:
1804 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1805 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1806
1807 case TYPE_ARGUMENT_PACK:
1808 case NONTYPE_ARGUMENT_PACK:
1809 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1810
1811 case TREE_LIST:
1812 for (; arg; arg = TREE_CHAIN (arg))
1813 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1814 return val;
1815
1816 case OVERLOAD:
1817 for (lkp_iterator iter (arg); iter; ++iter)
1818 val = iterative_hash_template_arg (*iter, val);
1819 return val;
1820
1821 case CONSTRUCTOR:
1822 {
1823 tree field, value;
1824 unsigned i;
1825 iterative_hash_template_arg (TREE_TYPE (arg), val);
1826 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1827 {
1828 val = iterative_hash_template_arg (field, val);
1829 val = iterative_hash_template_arg (value, val);
1830 }
1831 return val;
1832 }
1833
1834 case PARM_DECL:
1835 if (!DECL_ARTIFICIAL (arg))
1836 {
1837 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1838 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1839 }
1840 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1841
1842 case TARGET_EXPR:
1843 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1844
1845 case PTRMEM_CST:
1846 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1847 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1848
1849 case TEMPLATE_PARM_INDEX:
1850 val = iterative_hash_template_arg
1851 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1852 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1853 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1854
1855 case TRAIT_EXPR:
1856 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1857 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1858 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1859
1860 case BASELINK:
1861 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1862 val);
1863 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1864 val);
1865
1866 case MODOP_EXPR:
1867 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1868 code = TREE_CODE (TREE_OPERAND (arg, 1));
1869 val = iterative_hash_object (code, val);
1870 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1871
1872 case LAMBDA_EXPR:
1873 /* [temp.over.link] Two lambda-expressions are never considered
1874 equivalent.
1875
1876 So just hash the closure type. */
1877 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1878
1879 case CAST_EXPR:
1880 case IMPLICIT_CONV_EXPR:
1881 case STATIC_CAST_EXPR:
1882 case REINTERPRET_CAST_EXPR:
1883 case CONST_CAST_EXPR:
1884 case DYNAMIC_CAST_EXPR:
1885 case NEW_EXPR:
1886 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1887 /* Now hash operands as usual. */
1888 break;
1889
1890 case CALL_EXPR:
1891 {
1892 tree fn = CALL_EXPR_FN (arg);
1893 if (tree name = dependent_name (fn))
1894 {
1895 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1896 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1897 fn = name;
1898 }
1899 val = iterative_hash_template_arg (fn, val);
1900 call_expr_arg_iterator ai;
1901 for (tree x = first_call_expr_arg (arg, &ai); x;
1902 x = next_call_expr_arg (&ai))
1903 val = iterative_hash_template_arg (x, val);
1904 return val;
1905 }
1906
1907 default:
1908 break;
1909 }
1910
1911 char tclass = TREE_CODE_CLASS (code);
1912 switch (tclass)
1913 {
1914 case tcc_type:
1915 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1916 {
1917 // We want an alias specialization that survived strip_typedefs
1918 // to hash differently from its TYPE_CANONICAL, to avoid hash
1919 // collisions that compare as different in template_args_equal.
1920 // These could be dependent specializations that strip_typedefs
1921 // left alone, or untouched specializations because
1922 // coerce_template_parms returns the unconverted template
1923 // arguments if it sees incomplete argument packs.
1924 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1925 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1926 }
1927
1928 switch (TREE_CODE (arg))
1929 {
1930 case TEMPLATE_TEMPLATE_PARM:
1931 {
1932 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1933
1934 /* Do not recurse with TPI directly, as that is unbounded
1935 recursion. */
1936 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1937 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1938 }
1939 break;
1940
1941 case DECLTYPE_TYPE:
1942 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1943 break;
1944
1945 default:
1946 if (tree canonical = TYPE_CANONICAL (arg))
1947 val = iterative_hash_object (TYPE_HASH (canonical), val);
1948 break;
1949 }
1950
1951 return val;
1952
1953 case tcc_declaration:
1954 case tcc_constant:
1955 return iterative_hash_expr (arg, val);
1956
1957 default:
1958 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1959 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1960 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1961 return val;
1962 }
1963
1964 gcc_unreachable ();
1965 return 0;
1966 }
1967
1968 /* Unregister the specialization SPEC as a specialization of TMPL.
1969 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1970 if the SPEC was listed as a specialization of TMPL.
1971
1972 Note that SPEC has been ggc_freed, so we can't look inside it. */
1973
1974 bool
1975 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1976 {
1977 spec_entry *entry;
1978 spec_entry elt;
1979
1980 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1981 elt.args = TI_ARGS (tinfo);
1982 elt.spec = NULL_TREE;
1983
1984 entry = decl_specializations->find (&elt);
1985 if (entry != NULL)
1986 {
1987 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1988 gcc_assert (new_spec != NULL_TREE);
1989 entry->spec = new_spec;
1990 return 1;
1991 }
1992
1993 return 0;
1994 }
1995
1996 /* Like register_specialization, but for local declarations. We are
1997 registering SPEC, an instantiation of TMPL. */
1998
1999 void
2000 register_local_specialization (tree spec, tree tmpl)
2001 {
2002 gcc_assert (tmpl != spec);
2003 local_specializations->put (tmpl, spec);
2004 }
2005
2006 /* TYPE is a class type. Returns true if TYPE is an explicitly
2007 specialized class. */
2008
2009 bool
2010 explicit_class_specialization_p (tree type)
2011 {
2012 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2013 return false;
2014 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2015 }
2016
2017 /* Print the list of functions at FNS, going through all the overloads
2018 for each element of the list. Alternatively, FNS cannot be a
2019 TREE_LIST, in which case it will be printed together with all the
2020 overloads.
2021
2022 MORE and *STR should respectively be FALSE and NULL when the function
2023 is called from the outside. They are used internally on recursive
2024 calls. print_candidates manages the two parameters and leaves NULL
2025 in *STR when it ends. */
2026
2027 static void
2028 print_candidates_1 (tree fns, char **str, bool more = false)
2029 {
2030 if (TREE_CODE (fns) == TREE_LIST)
2031 for (; fns; fns = TREE_CHAIN (fns))
2032 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2033 else
2034 for (lkp_iterator iter (fns); iter;)
2035 {
2036 tree cand = *iter;
2037 ++iter;
2038
2039 const char *pfx = *str;
2040 if (!pfx)
2041 {
2042 if (more || iter)
2043 pfx = _("candidates are:");
2044 else
2045 pfx = _("candidate is:");
2046 *str = get_spaces (pfx);
2047 }
2048 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2049 }
2050 }
2051
2052 /* Print the list of candidate FNS in an error message. FNS can also
2053 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2054
2055 void
2056 print_candidates (tree fns)
2057 {
2058 char *str = NULL;
2059 print_candidates_1 (fns, &str);
2060 free (str);
2061 }
2062
2063 /* Get a (possibly) constrained template declaration for the
2064 purpose of ordering candidates. */
2065 static tree
2066 get_template_for_ordering (tree list)
2067 {
2068 gcc_assert (TREE_CODE (list) == TREE_LIST);
2069 tree f = TREE_VALUE (list);
2070 if (tree ti = DECL_TEMPLATE_INFO (f))
2071 return TI_TEMPLATE (ti);
2072 return f;
2073 }
2074
2075 /* Among candidates having the same signature, return the
2076 most constrained or NULL_TREE if there is no best candidate.
2077 If the signatures of candidates vary (e.g., template
2078 specialization vs. member function), then there can be no
2079 most constrained.
2080
2081 Note that we don't compare constraints on the functions
2082 themselves, but rather those of their templates. */
2083 static tree
2084 most_constrained_function (tree candidates)
2085 {
2086 // Try to find the best candidate in a first pass.
2087 tree champ = candidates;
2088 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2089 {
2090 int winner = more_constrained (get_template_for_ordering (champ),
2091 get_template_for_ordering (c));
2092 if (winner == -1)
2093 champ = c; // The candidate is more constrained
2094 else if (winner == 0)
2095 return NULL_TREE; // Neither is more constrained
2096 }
2097
2098 // Verify that the champ is better than previous candidates.
2099 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2100 if (!more_constrained (get_template_for_ordering (champ),
2101 get_template_for_ordering (c)))
2102 return NULL_TREE;
2103 }
2104
2105 return champ;
2106 }
2107
2108
2109 /* Returns the template (one of the functions given by TEMPLATE_ID)
2110 which can be specialized to match the indicated DECL with the
2111 explicit template args given in TEMPLATE_ID. The DECL may be
2112 NULL_TREE if none is available. In that case, the functions in
2113 TEMPLATE_ID are non-members.
2114
2115 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2116 specialization of a member template.
2117
2118 The TEMPLATE_COUNT is the number of references to qualifying
2119 template classes that appeared in the name of the function. See
2120 check_explicit_specialization for a more accurate description.
2121
2122 TSK indicates what kind of template declaration (if any) is being
2123 declared. TSK_TEMPLATE indicates that the declaration given by
2124 DECL, though a FUNCTION_DECL, has template parameters, and is
2125 therefore a template function.
2126
2127 The template args (those explicitly specified and those deduced)
2128 are output in a newly created vector *TARGS_OUT.
2129
2130 If it is impossible to determine the result, an error message is
2131 issued. The error_mark_node is returned to indicate failure. */
2132
2133 static tree
2134 determine_specialization (tree template_id,
2135 tree decl,
2136 tree* targs_out,
2137 int need_member_template,
2138 int template_count,
2139 tmpl_spec_kind tsk)
2140 {
2141 tree fns;
2142 tree targs;
2143 tree explicit_targs;
2144 tree candidates = NULL_TREE;
2145
2146 /* A TREE_LIST of templates of which DECL may be a specialization.
2147 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2148 corresponding TREE_PURPOSE is the set of template arguments that,
2149 when used to instantiate the template, would produce a function
2150 with the signature of DECL. */
2151 tree templates = NULL_TREE;
2152 int header_count;
2153 cp_binding_level *b;
2154
2155 *targs_out = NULL_TREE;
2156
2157 if (template_id == error_mark_node || decl == error_mark_node)
2158 return error_mark_node;
2159
2160 /* We shouldn't be specializing a member template of an
2161 unspecialized class template; we already gave an error in
2162 check_specialization_scope, now avoid crashing. */
2163 if (!VAR_P (decl)
2164 && template_count && DECL_CLASS_SCOPE_P (decl)
2165 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2166 {
2167 gcc_assert (errorcount);
2168 return error_mark_node;
2169 }
2170
2171 fns = TREE_OPERAND (template_id, 0);
2172 explicit_targs = TREE_OPERAND (template_id, 1);
2173
2174 if (fns == error_mark_node)
2175 return error_mark_node;
2176
2177 /* Check for baselinks. */
2178 if (BASELINK_P (fns))
2179 fns = BASELINK_FUNCTIONS (fns);
2180
2181 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2182 {
2183 error_at (DECL_SOURCE_LOCATION (decl),
2184 "%qD is not a function template", fns);
2185 return error_mark_node;
2186 }
2187 else if (VAR_P (decl) && !variable_template_p (fns))
2188 {
2189 error ("%qD is not a variable template", fns);
2190 return error_mark_node;
2191 }
2192
2193 /* Count the number of template headers specified for this
2194 specialization. */
2195 header_count = 0;
2196 for (b = current_binding_level;
2197 b->kind == sk_template_parms;
2198 b = b->level_chain)
2199 ++header_count;
2200
2201 tree orig_fns = fns;
2202 bool header_mismatch = false;
2203
2204 if (variable_template_p (fns))
2205 {
2206 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2207 targs = coerce_template_parms (parms, explicit_targs, fns,
2208 tf_warning_or_error,
2209 /*req_all*/true, /*use_defarg*/true);
2210 if (targs != error_mark_node
2211 && constraints_satisfied_p (fns, targs))
2212 templates = tree_cons (targs, fns, templates);
2213 }
2214 else for (lkp_iterator iter (fns); iter; ++iter)
2215 {
2216 tree fn = *iter;
2217
2218 if (TREE_CODE (fn) == TEMPLATE_DECL)
2219 {
2220 tree decl_arg_types;
2221 tree fn_arg_types;
2222 tree insttype;
2223
2224 /* In case of explicit specialization, we need to check if
2225 the number of template headers appearing in the specialization
2226 is correct. This is usually done in check_explicit_specialization,
2227 but the check done there cannot be exhaustive when specializing
2228 member functions. Consider the following code:
2229
2230 template <> void A<int>::f(int);
2231 template <> template <> void A<int>::f(int);
2232
2233 Assuming that A<int> is not itself an explicit specialization
2234 already, the first line specializes "f" which is a non-template
2235 member function, whilst the second line specializes "f" which
2236 is a template member function. So both lines are syntactically
2237 correct, and check_explicit_specialization does not reject
2238 them.
2239
2240 Here, we can do better, as we are matching the specialization
2241 against the declarations. We count the number of template
2242 headers, and we check if they match TEMPLATE_COUNT + 1
2243 (TEMPLATE_COUNT is the number of qualifying template classes,
2244 plus there must be another header for the member template
2245 itself).
2246
2247 Notice that if header_count is zero, this is not a
2248 specialization but rather a template instantiation, so there
2249 is no check we can perform here. */
2250 if (header_count && header_count != template_count + 1)
2251 {
2252 header_mismatch = true;
2253 continue;
2254 }
2255
2256 /* Check that the number of template arguments at the
2257 innermost level for DECL is the same as for FN. */
2258 if (current_binding_level->kind == sk_template_parms
2259 && !current_binding_level->explicit_spec_p
2260 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2261 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2262 (current_template_parms))))
2263 continue;
2264
2265 /* DECL might be a specialization of FN. */
2266 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2267 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2268
2269 /* For a non-static member function, we need to make sure
2270 that the const qualification is the same. Since
2271 get_bindings does not try to merge the "this" parameter,
2272 we must do the comparison explicitly. */
2273 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2274 {
2275 if (!same_type_p (TREE_VALUE (fn_arg_types),
2276 TREE_VALUE (decl_arg_types)))
2277 continue;
2278
2279 /* And the ref-qualification. */
2280 if (type_memfn_rqual (TREE_TYPE (decl))
2281 != type_memfn_rqual (TREE_TYPE (fn)))
2282 continue;
2283 }
2284
2285 /* Skip the "this" parameter and, for constructors of
2286 classes with virtual bases, the VTT parameter. A
2287 full specialization of a constructor will have a VTT
2288 parameter, but a template never will. */
2289 decl_arg_types
2290 = skip_artificial_parms_for (decl, decl_arg_types);
2291 fn_arg_types
2292 = skip_artificial_parms_for (fn, fn_arg_types);
2293
2294 /* Function templates cannot be specializations; there are
2295 no partial specializations of functions. Therefore, if
2296 the type of DECL does not match FN, there is no
2297 match.
2298
2299 Note that it should never be the case that we have both
2300 candidates added here, and for regular member functions
2301 below. */
2302 if (tsk == tsk_template)
2303 {
2304 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2305 current_template_parms))
2306 continue;
2307 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2308 TREE_TYPE (TREE_TYPE (fn))))
2309 continue;
2310 if (!compparms (fn_arg_types, decl_arg_types))
2311 continue;
2312
2313 tree freq = get_trailing_function_requirements (fn);
2314 tree dreq = get_trailing_function_requirements (decl);
2315 if (!freq != !dreq)
2316 continue;
2317 if (freq)
2318 {
2319 tree fargs = DECL_TI_ARGS (fn);
2320 tsubst_flags_t complain = tf_none;
2321 freq = tsubst_constraint (freq, fargs, complain, fn);
2322 if (!cp_tree_equal (freq, dreq))
2323 continue;
2324 }
2325
2326 candidates = tree_cons (NULL_TREE, fn, candidates);
2327 continue;
2328 }
2329
2330 /* See whether this function might be a specialization of this
2331 template. Suppress access control because we might be trying
2332 to make this specialization a friend, and we have already done
2333 access control for the declaration of the specialization. */
2334 push_deferring_access_checks (dk_no_check);
2335 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2336 pop_deferring_access_checks ();
2337
2338 if (!targs)
2339 /* We cannot deduce template arguments that when used to
2340 specialize TMPL will produce DECL. */
2341 continue;
2342
2343 if (uses_template_parms (targs))
2344 /* We deduced something involving 'auto', which isn't a valid
2345 template argument. */
2346 continue;
2347
2348 /* Remove, from the set of candidates, all those functions
2349 whose constraints are not satisfied. */
2350 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2351 continue;
2352
2353 // Then, try to form the new function type.
2354 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2355 if (insttype == error_mark_node)
2356 continue;
2357 fn_arg_types
2358 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2359 if (!compparms (fn_arg_types, decl_arg_types))
2360 continue;
2361
2362 /* Save this template, and the arguments deduced. */
2363 templates = tree_cons (targs, fn, templates);
2364 }
2365 else if (need_member_template)
2366 /* FN is an ordinary member function, and we need a
2367 specialization of a member template. */
2368 ;
2369 else if (TREE_CODE (fn) != FUNCTION_DECL)
2370 /* We can get IDENTIFIER_NODEs here in certain erroneous
2371 cases. */
2372 ;
2373 else if (!DECL_FUNCTION_MEMBER_P (fn))
2374 /* This is just an ordinary non-member function. Nothing can
2375 be a specialization of that. */
2376 ;
2377 else if (DECL_ARTIFICIAL (fn))
2378 /* Cannot specialize functions that are created implicitly. */
2379 ;
2380 else
2381 {
2382 tree decl_arg_types;
2383
2384 /* This is an ordinary member function. However, since
2385 we're here, we can assume its enclosing class is a
2386 template class. For example,
2387
2388 template <typename T> struct S { void f(); };
2389 template <> void S<int>::f() {}
2390
2391 Here, S<int>::f is a non-template, but S<int> is a
2392 template class. If FN has the same type as DECL, we
2393 might be in business. */
2394
2395 if (!DECL_TEMPLATE_INFO (fn))
2396 /* Its enclosing class is an explicit specialization
2397 of a template class. This is not a candidate. */
2398 continue;
2399
2400 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2401 TREE_TYPE (TREE_TYPE (fn))))
2402 /* The return types differ. */
2403 continue;
2404
2405 /* Adjust the type of DECL in case FN is a static member. */
2406 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2407 if (DECL_STATIC_FUNCTION_P (fn)
2408 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2409 decl_arg_types = TREE_CHAIN (decl_arg_types);
2410
2411 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2412 decl_arg_types))
2413 continue;
2414
2415 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2416 && (type_memfn_rqual (TREE_TYPE (decl))
2417 != type_memfn_rqual (TREE_TYPE (fn))))
2418 continue;
2419
2420 // If the deduced arguments do not satisfy the constraints,
2421 // this is not a candidate.
2422 if (flag_concepts && !constraints_satisfied_p (fn))
2423 continue;
2424
2425 // Add the candidate.
2426 candidates = tree_cons (NULL_TREE, fn, candidates);
2427 }
2428 }
2429
2430 if (templates && TREE_CHAIN (templates))
2431 {
2432 /* We have:
2433
2434 [temp.expl.spec]
2435
2436 It is possible for a specialization with a given function
2437 signature to be instantiated from more than one function
2438 template. In such cases, explicit specification of the
2439 template arguments must be used to uniquely identify the
2440 function template specialization being specialized.
2441
2442 Note that here, there's no suggestion that we're supposed to
2443 determine which of the candidate templates is most
2444 specialized. However, we, also have:
2445
2446 [temp.func.order]
2447
2448 Partial ordering of overloaded function template
2449 declarations is used in the following contexts to select
2450 the function template to which a function template
2451 specialization refers:
2452
2453 -- when an explicit specialization refers to a function
2454 template.
2455
2456 So, we do use the partial ordering rules, at least for now.
2457 This extension can only serve to make invalid programs valid,
2458 so it's safe. And, there is strong anecdotal evidence that
2459 the committee intended the partial ordering rules to apply;
2460 the EDG front end has that behavior, and John Spicer claims
2461 that the committee simply forgot to delete the wording in
2462 [temp.expl.spec]. */
2463 tree tmpl = most_specialized_instantiation (templates);
2464 if (tmpl != error_mark_node)
2465 {
2466 templates = tmpl;
2467 TREE_CHAIN (templates) = NULL_TREE;
2468 }
2469 }
2470
2471 // Concepts allows multiple declarations of member functions
2472 // with the same signature. Like above, we need to rely on
2473 // on the partial ordering of those candidates to determine which
2474 // is the best.
2475 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2476 {
2477 if (tree cand = most_constrained_function (candidates))
2478 {
2479 candidates = cand;
2480 TREE_CHAIN (cand) = NULL_TREE;
2481 }
2482 }
2483
2484 if (templates == NULL_TREE && candidates == NULL_TREE)
2485 {
2486 error ("template-id %qD for %q+D does not match any template "
2487 "declaration", template_id, decl);
2488 if (header_mismatch)
2489 inform (DECL_SOURCE_LOCATION (decl),
2490 "saw %d %<template<>%>, need %d for "
2491 "specializing a member function template",
2492 header_count, template_count + 1);
2493 print_candidates (orig_fns);
2494 return error_mark_node;
2495 }
2496 else if ((templates && TREE_CHAIN (templates))
2497 || (candidates && TREE_CHAIN (candidates))
2498 || (templates && candidates))
2499 {
2500 error ("ambiguous template specialization %qD for %q+D",
2501 template_id, decl);
2502 candidates = chainon (candidates, templates);
2503 print_candidates (candidates);
2504 return error_mark_node;
2505 }
2506
2507 /* We have one, and exactly one, match. */
2508 if (candidates)
2509 {
2510 tree fn = TREE_VALUE (candidates);
2511 *targs_out = copy_node (DECL_TI_ARGS (fn));
2512
2513 /* Propagate the candidate's constraints to the declaration. */
2514 if (tsk != tsk_template)
2515 set_constraints (decl, get_constraints (fn));
2516
2517 /* DECL is a re-declaration or partial instantiation of a template
2518 function. */
2519 if (TREE_CODE (fn) == TEMPLATE_DECL)
2520 return fn;
2521 /* It was a specialization of an ordinary member function in a
2522 template class. */
2523 return DECL_TI_TEMPLATE (fn);
2524 }
2525
2526 /* It was a specialization of a template. */
2527 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2528 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2529 {
2530 *targs_out = copy_node (targs);
2531 SET_TMPL_ARGS_LEVEL (*targs_out,
2532 TMPL_ARGS_DEPTH (*targs_out),
2533 TREE_PURPOSE (templates));
2534 }
2535 else
2536 *targs_out = TREE_PURPOSE (templates);
2537 return TREE_VALUE (templates);
2538 }
2539
2540 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2541 but with the default argument values filled in from those in the
2542 TMPL_TYPES. */
2543
2544 static tree
2545 copy_default_args_to_explicit_spec_1 (tree spec_types,
2546 tree tmpl_types)
2547 {
2548 tree new_spec_types;
2549
2550 if (!spec_types)
2551 return NULL_TREE;
2552
2553 if (spec_types == void_list_node)
2554 return void_list_node;
2555
2556 /* Substitute into the rest of the list. */
2557 new_spec_types =
2558 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2559 TREE_CHAIN (tmpl_types));
2560
2561 /* Add the default argument for this parameter. */
2562 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2563 TREE_VALUE (spec_types),
2564 new_spec_types);
2565 }
2566
2567 /* DECL is an explicit specialization. Replicate default arguments
2568 from the template it specializes. (That way, code like:
2569
2570 template <class T> void f(T = 3);
2571 template <> void f(double);
2572 void g () { f (); }
2573
2574 works, as required.) An alternative approach would be to look up
2575 the correct default arguments at the call-site, but this approach
2576 is consistent with how implicit instantiations are handled. */
2577
2578 static void
2579 copy_default_args_to_explicit_spec (tree decl)
2580 {
2581 tree tmpl;
2582 tree spec_types;
2583 tree tmpl_types;
2584 tree new_spec_types;
2585 tree old_type;
2586 tree new_type;
2587 tree t;
2588 tree object_type = NULL_TREE;
2589 tree in_charge = NULL_TREE;
2590 tree vtt = NULL_TREE;
2591
2592 /* See if there's anything we need to do. */
2593 tmpl = DECL_TI_TEMPLATE (decl);
2594 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2595 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2596 if (TREE_PURPOSE (t))
2597 break;
2598 if (!t)
2599 return;
2600
2601 old_type = TREE_TYPE (decl);
2602 spec_types = TYPE_ARG_TYPES (old_type);
2603
2604 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2605 {
2606 /* Remove the this pointer, but remember the object's type for
2607 CV quals. */
2608 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2609 spec_types = TREE_CHAIN (spec_types);
2610 tmpl_types = TREE_CHAIN (tmpl_types);
2611
2612 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2613 {
2614 /* DECL may contain more parameters than TMPL due to the extra
2615 in-charge parameter in constructors and destructors. */
2616 in_charge = spec_types;
2617 spec_types = TREE_CHAIN (spec_types);
2618 }
2619 if (DECL_HAS_VTT_PARM_P (decl))
2620 {
2621 vtt = spec_types;
2622 spec_types = TREE_CHAIN (spec_types);
2623 }
2624 }
2625
2626 /* Compute the merged default arguments. */
2627 new_spec_types =
2628 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2629
2630 /* Compute the new FUNCTION_TYPE. */
2631 if (object_type)
2632 {
2633 if (vtt)
2634 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2635 TREE_VALUE (vtt),
2636 new_spec_types);
2637
2638 if (in_charge)
2639 /* Put the in-charge parameter back. */
2640 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2641 TREE_VALUE (in_charge),
2642 new_spec_types);
2643
2644 new_type = build_method_type_directly (object_type,
2645 TREE_TYPE (old_type),
2646 new_spec_types);
2647 }
2648 else
2649 new_type = build_function_type (TREE_TYPE (old_type),
2650 new_spec_types);
2651 new_type = cp_build_type_attribute_variant (new_type,
2652 TYPE_ATTRIBUTES (old_type));
2653 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2654
2655 TREE_TYPE (decl) = new_type;
2656 }
2657
2658 /* Return the number of template headers we expect to see for a definition
2659 or specialization of CTYPE or one of its non-template members. */
2660
2661 int
2662 num_template_headers_for_class (tree ctype)
2663 {
2664 int num_templates = 0;
2665
2666 while (ctype && CLASS_TYPE_P (ctype))
2667 {
2668 /* You're supposed to have one `template <...>' for every
2669 template class, but you don't need one for a full
2670 specialization. For example:
2671
2672 template <class T> struct S{};
2673 template <> struct S<int> { void f(); };
2674 void S<int>::f () {}
2675
2676 is correct; there shouldn't be a `template <>' for the
2677 definition of `S<int>::f'. */
2678 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2679 /* If CTYPE does not have template information of any
2680 kind, then it is not a template, nor is it nested
2681 within a template. */
2682 break;
2683 if (explicit_class_specialization_p (ctype))
2684 break;
2685 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2686 ++num_templates;
2687
2688 ctype = TYPE_CONTEXT (ctype);
2689 }
2690
2691 return num_templates;
2692 }
2693
2694 /* Do a simple sanity check on the template headers that precede the
2695 variable declaration DECL. */
2696
2697 void
2698 check_template_variable (tree decl)
2699 {
2700 tree ctx = CP_DECL_CONTEXT (decl);
2701 int wanted = num_template_headers_for_class (ctx);
2702 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2703 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2704 {
2705 if (cxx_dialect < cxx14)
2706 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2707 "variable templates only available with "
2708 "%<-std=c++14%> or %<-std=gnu++14%>");
2709
2710 // Namespace-scope variable templates should have a template header.
2711 ++wanted;
2712 }
2713 if (template_header_count > wanted)
2714 {
2715 auto_diagnostic_group d;
2716 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2717 "too many template headers for %qD "
2718 "(should be %d)",
2719 decl, wanted);
2720 if (warned && CLASS_TYPE_P (ctx)
2721 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2722 inform (DECL_SOURCE_LOCATION (decl),
2723 "members of an explicitly specialized class are defined "
2724 "without a template header");
2725 }
2726 }
2727
2728 /* An explicit specialization whose declarator-id or class-head-name is not
2729 qualified shall be declared in the nearest enclosing namespace of the
2730 template, or, if the namespace is inline (7.3.1), any namespace from its
2731 enclosing namespace set.
2732
2733 If the name declared in the explicit instantiation is an unqualified name,
2734 the explicit instantiation shall appear in the namespace where its template
2735 is declared or, if that namespace is inline (7.3.1), any namespace from its
2736 enclosing namespace set. */
2737
2738 void
2739 check_unqualified_spec_or_inst (tree t, location_t loc)
2740 {
2741 tree tmpl = most_general_template (t);
2742 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2743 && !is_nested_namespace (current_namespace,
2744 CP_DECL_CONTEXT (tmpl), true))
2745 {
2746 if (processing_specialization)
2747 permerror (loc, "explicit specialization of %qD outside its "
2748 "namespace must use a nested-name-specifier", tmpl);
2749 else if (processing_explicit_instantiation
2750 && cxx_dialect >= cxx11)
2751 /* This was allowed in C++98, so only pedwarn. */
2752 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2753 "outside its namespace must use a nested-name-"
2754 "specifier", tmpl);
2755 }
2756 }
2757
2758 /* Warn for a template specialization SPEC that is missing some of a set
2759 of function or type attributes that the template TEMPL is declared with.
2760 ATTRLIST is a list of additional attributes that SPEC should be taken
2761 to ultimately be declared with. */
2762
2763 static void
2764 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2765 {
2766 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2767 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2768
2769 /* Avoid warning if the difference between the primary and
2770 the specialization is not in one of the attributes below. */
2771 const char* const blacklist[] = {
2772 "alloc_align", "alloc_size", "assume_aligned", "format",
2773 "format_arg", "malloc", "nonnull", NULL
2774 };
2775
2776 /* Put together a list of the black listed attributes that the primary
2777 template is declared with that the specialization is not, in case
2778 it's not apparent from the most recent declaration of the primary. */
2779 pretty_printer str;
2780 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2781 blacklist, &str);
2782
2783 if (!nattrs)
2784 return;
2785
2786 auto_diagnostic_group d;
2787 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2788 "explicit specialization %q#D may be missing attributes",
2789 spec))
2790 inform (DECL_SOURCE_LOCATION (tmpl),
2791 nattrs > 1
2792 ? G_("missing primary template attributes %s")
2793 : G_("missing primary template attribute %s"),
2794 pp_formatted_text (&str));
2795 }
2796
2797 /* Check to see if the function just declared, as indicated in
2798 DECLARATOR, and in DECL, is a specialization of a function
2799 template. We may also discover that the declaration is an explicit
2800 instantiation at this point.
2801
2802 Returns DECL, or an equivalent declaration that should be used
2803 instead if all goes well. Issues an error message if something is
2804 amiss. Returns error_mark_node if the error is not easily
2805 recoverable.
2806
2807 FLAGS is a bitmask consisting of the following flags:
2808
2809 2: The function has a definition.
2810 4: The function is a friend.
2811
2812 The TEMPLATE_COUNT is the number of references to qualifying
2813 template classes that appeared in the name of the function. For
2814 example, in
2815
2816 template <class T> struct S { void f(); };
2817 void S<int>::f();
2818
2819 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2820 classes are not counted in the TEMPLATE_COUNT, so that in
2821
2822 template <class T> struct S {};
2823 template <> struct S<int> { void f(); }
2824 template <> void S<int>::f();
2825
2826 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2827 invalid; there should be no template <>.)
2828
2829 If the function is a specialization, it is marked as such via
2830 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2831 is set up correctly, and it is added to the list of specializations
2832 for that template. */
2833
2834 tree
2835 check_explicit_specialization (tree declarator,
2836 tree decl,
2837 int template_count,
2838 int flags,
2839 tree attrlist)
2840 {
2841 int have_def = flags & 2;
2842 int is_friend = flags & 4;
2843 bool is_concept = flags & 8;
2844 int specialization = 0;
2845 int explicit_instantiation = 0;
2846 int member_specialization = 0;
2847 tree ctype = DECL_CLASS_CONTEXT (decl);
2848 tree dname = DECL_NAME (decl);
2849 tmpl_spec_kind tsk;
2850
2851 if (is_friend)
2852 {
2853 if (!processing_specialization)
2854 tsk = tsk_none;
2855 else
2856 tsk = tsk_excessive_parms;
2857 }
2858 else
2859 tsk = current_tmpl_spec_kind (template_count);
2860
2861 switch (tsk)
2862 {
2863 case tsk_none:
2864 if (processing_specialization && !VAR_P (decl))
2865 {
2866 specialization = 1;
2867 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2868 }
2869 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2870 {
2871 if (is_friend)
2872 /* This could be something like:
2873
2874 template <class T> void f(T);
2875 class S { friend void f<>(int); } */
2876 specialization = 1;
2877 else
2878 {
2879 /* This case handles bogus declarations like template <>
2880 template <class T> void f<int>(); */
2881
2882 error_at (cp_expr_loc_or_input_loc (declarator),
2883 "template-id %qE in declaration of primary template",
2884 declarator);
2885 return decl;
2886 }
2887 }
2888 break;
2889
2890 case tsk_invalid_member_spec:
2891 /* The error has already been reported in
2892 check_specialization_scope. */
2893 return error_mark_node;
2894
2895 case tsk_invalid_expl_inst:
2896 error ("template parameter list used in explicit instantiation");
2897
2898 /* Fall through. */
2899
2900 case tsk_expl_inst:
2901 if (have_def)
2902 error ("definition provided for explicit instantiation");
2903
2904 explicit_instantiation = 1;
2905 break;
2906
2907 case tsk_excessive_parms:
2908 case tsk_insufficient_parms:
2909 if (tsk == tsk_excessive_parms)
2910 error ("too many template parameter lists in declaration of %qD",
2911 decl);
2912 else if (template_header_count)
2913 error("too few template parameter lists in declaration of %qD", decl);
2914 else
2915 error("explicit specialization of %qD must be introduced by "
2916 "%<template <>%>", decl);
2917
2918 /* Fall through. */
2919 case tsk_expl_spec:
2920 if (is_concept)
2921 error ("explicit specialization declared %<concept%>");
2922
2923 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2924 /* In cases like template<> constexpr bool v = true;
2925 We'll give an error in check_template_variable. */
2926 break;
2927
2928 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2929 if (ctype)
2930 member_specialization = 1;
2931 else
2932 specialization = 1;
2933 break;
2934
2935 case tsk_template:
2936 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2937 {
2938 /* This case handles bogus declarations like template <>
2939 template <class T> void f<int>(); */
2940
2941 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2942 error_at (cp_expr_loc_or_input_loc (declarator),
2943 "template-id %qE in declaration of primary template",
2944 declarator);
2945 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2946 {
2947 /* Partial specialization of variable template. */
2948 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2949 specialization = 1;
2950 goto ok;
2951 }
2952 else if (cxx_dialect < cxx14)
2953 error_at (cp_expr_loc_or_input_loc (declarator),
2954 "non-type partial specialization %qE "
2955 "is not allowed", declarator);
2956 else
2957 error_at (cp_expr_loc_or_input_loc (declarator),
2958 "non-class, non-variable partial specialization %qE "
2959 "is not allowed", declarator);
2960 return decl;
2961 ok:;
2962 }
2963
2964 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2965 /* This is a specialization of a member template, without
2966 specialization the containing class. Something like:
2967
2968 template <class T> struct S {
2969 template <class U> void f (U);
2970 };
2971 template <> template <class U> void S<int>::f(U) {}
2972
2973 That's a specialization -- but of the entire template. */
2974 specialization = 1;
2975 break;
2976
2977 default:
2978 gcc_unreachable ();
2979 }
2980
2981 if ((specialization || member_specialization)
2982 /* This doesn't apply to variable templates. */
2983 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2984 {
2985 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2986 for (; t; t = TREE_CHAIN (t))
2987 if (TREE_PURPOSE (t))
2988 {
2989 permerror (input_location,
2990 "default argument specified in explicit specialization");
2991 break;
2992 }
2993 }
2994
2995 if (specialization || member_specialization || explicit_instantiation)
2996 {
2997 tree tmpl = NULL_TREE;
2998 tree targs = NULL_TREE;
2999 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
3000 bool found_hidden = false;
3001
3002 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
3003 if (!was_template_id)
3004 {
3005 tree fns;
3006
3007 gcc_assert (identifier_p (declarator));
3008 if (ctype)
3009 fns = dname;
3010 else
3011 {
3012 /* If there is no class context, the explicit instantiation
3013 must be at namespace scope. */
3014 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3015
3016 /* Find the namespace binding, using the declaration
3017 context. */
3018 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3019 LOOK_want::NORMAL, true);
3020 if (fns == error_mark_node)
3021 {
3022 /* If lookup fails, look for a friend declaration so we can
3023 give a better diagnostic. */
3024 fns = (lookup_qualified_name
3025 (CP_DECL_CONTEXT (decl), dname,
3026 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3027 /*complain*/true));
3028 found_hidden = true;
3029 }
3030
3031 if (fns == error_mark_node || !is_overloaded_fn (fns))
3032 {
3033 error ("%qD is not a template function", dname);
3034 fns = error_mark_node;
3035 }
3036 }
3037
3038 declarator = lookup_template_function (fns, NULL_TREE);
3039 }
3040
3041 if (declarator == error_mark_node)
3042 return error_mark_node;
3043
3044 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3045 {
3046 if (!explicit_instantiation)
3047 /* A specialization in class scope. This is invalid,
3048 but the error will already have been flagged by
3049 check_specialization_scope. */
3050 return error_mark_node;
3051 else
3052 {
3053 /* It's not valid to write an explicit instantiation in
3054 class scope, e.g.:
3055
3056 class C { template void f(); }
3057
3058 This case is caught by the parser. However, on
3059 something like:
3060
3061 template class C { void f(); };
3062
3063 (which is invalid) we can get here. The error will be
3064 issued later. */
3065 ;
3066 }
3067
3068 return decl;
3069 }
3070 else if (ctype != NULL_TREE
3071 && (identifier_p (TREE_OPERAND (declarator, 0))))
3072 {
3073 // We'll match variable templates in start_decl.
3074 if (VAR_P (decl))
3075 return decl;
3076
3077 /* Find the list of functions in ctype that have the same
3078 name as the declared function. */
3079 tree name = TREE_OPERAND (declarator, 0);
3080
3081 if (constructor_name_p (name, ctype))
3082 {
3083 if (DECL_CONSTRUCTOR_P (decl)
3084 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3085 : !CLASSTYPE_DESTRUCTOR (ctype))
3086 {
3087 /* From [temp.expl.spec]:
3088
3089 If such an explicit specialization for the member
3090 of a class template names an implicitly-declared
3091 special member function (clause _special_), the
3092 program is ill-formed.
3093
3094 Similar language is found in [temp.explicit]. */
3095 error ("specialization of implicitly-declared special member function");
3096 return error_mark_node;
3097 }
3098
3099 name = DECL_NAME (decl);
3100 }
3101
3102 /* For a type-conversion operator, We might be looking for
3103 `operator int' which will be a specialization of
3104 `operator T'. Grab all the conversion operators, and
3105 then select from them. */
3106 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3107 ? conv_op_identifier : name);
3108
3109 if (fns == NULL_TREE)
3110 {
3111 error ("no member function %qD declared in %qT", name, ctype);
3112 return error_mark_node;
3113 }
3114 else
3115 TREE_OPERAND (declarator, 0) = fns;
3116 }
3117
3118 /* Figure out what exactly is being specialized at this point.
3119 Note that for an explicit instantiation, even one for a
3120 member function, we cannot tell a priori whether the
3121 instantiation is for a member template, or just a member
3122 function of a template class. Even if a member template is
3123 being instantiated, the member template arguments may be
3124 elided if they can be deduced from the rest of the
3125 declaration. */
3126 tmpl = determine_specialization (declarator, decl,
3127 &targs,
3128 member_specialization,
3129 template_count,
3130 tsk);
3131
3132 if (!tmpl || tmpl == error_mark_node)
3133 /* We couldn't figure out what this declaration was
3134 specializing. */
3135 return error_mark_node;
3136 else
3137 {
3138 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3139 {
3140 auto_diagnostic_group d;
3141 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3142 "friend declaration %qD is not visible to "
3143 "explicit specialization", tmpl))
3144 inform (DECL_SOURCE_LOCATION (tmpl),
3145 "friend declaration here");
3146 }
3147
3148 if (!ctype && !is_friend
3149 && CP_DECL_CONTEXT (decl) == current_namespace)
3150 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3151
3152 tree gen_tmpl = most_general_template (tmpl);
3153
3154 if (explicit_instantiation)
3155 {
3156 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3157 is done by do_decl_instantiation later. */
3158
3159 int arg_depth = TMPL_ARGS_DEPTH (targs);
3160 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3161
3162 if (arg_depth > parm_depth)
3163 {
3164 /* If TMPL is not the most general template (for
3165 example, if TMPL is a friend template that is
3166 injected into namespace scope), then there will
3167 be too many levels of TARGS. Remove some of them
3168 here. */
3169 int i;
3170 tree new_targs;
3171
3172 new_targs = make_tree_vec (parm_depth);
3173 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3174 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3175 = TREE_VEC_ELT (targs, i);
3176 targs = new_targs;
3177 }
3178
3179 return instantiate_template (tmpl, targs, tf_error);
3180 }
3181
3182 /* If we thought that the DECL was a member function, but it
3183 turns out to be specializing a static member function,
3184 make DECL a static member function as well. */
3185 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3186 && DECL_STATIC_FUNCTION_P (tmpl)
3187 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3188 revert_static_member_fn (decl);
3189
3190 /* If this is a specialization of a member template of a
3191 template class, we want to return the TEMPLATE_DECL, not
3192 the specialization of it. */
3193 if (tsk == tsk_template && !was_template_id)
3194 {
3195 tree result = DECL_TEMPLATE_RESULT (tmpl);
3196 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3197 DECL_INITIAL (result) = NULL_TREE;
3198 if (have_def)
3199 {
3200 tree parm;
3201 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3202 DECL_SOURCE_LOCATION (result)
3203 = DECL_SOURCE_LOCATION (decl);
3204 /* We want to use the argument list specified in the
3205 definition, not in the original declaration. */
3206 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3207 for (parm = DECL_ARGUMENTS (result); parm;
3208 parm = DECL_CHAIN (parm))
3209 DECL_CONTEXT (parm) = result;
3210 }
3211 return register_specialization (tmpl, gen_tmpl, targs,
3212 is_friend, 0);
3213 }
3214
3215 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3216 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3217
3218 if (was_template_id)
3219 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3220
3221 /* Inherit default function arguments from the template
3222 DECL is specializing. */
3223 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3224 copy_default_args_to_explicit_spec (decl);
3225
3226 /* This specialization has the same protection as the
3227 template it specializes. */
3228 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3229 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3230
3231 /* 7.1.1-1 [dcl.stc]
3232
3233 A storage-class-specifier shall not be specified in an
3234 explicit specialization...
3235
3236 The parser rejects these, so unless action is taken here,
3237 explicit function specializations will always appear with
3238 global linkage.
3239
3240 The action recommended by the C++ CWG in response to C++
3241 defect report 605 is to make the storage class and linkage
3242 of the explicit specialization match the templated function:
3243
3244 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3245 */
3246 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3247 {
3248 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3249 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3250
3251 /* A concept cannot be specialized. */
3252 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3253 {
3254 error ("explicit specialization of function concept %qD",
3255 gen_tmpl);
3256 return error_mark_node;
3257 }
3258
3259 /* This specialization has the same linkage and visibility as
3260 the function template it specializes. */
3261 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3262 if (! TREE_PUBLIC (decl))
3263 {
3264 DECL_INTERFACE_KNOWN (decl) = 1;
3265 DECL_NOT_REALLY_EXTERN (decl) = 1;
3266 }
3267 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3268 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3269 {
3270 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3271 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3272 }
3273 }
3274
3275 /* If DECL is a friend declaration, declared using an
3276 unqualified name, the namespace associated with DECL may
3277 have been set incorrectly. For example, in:
3278
3279 template <typename T> void f(T);
3280 namespace N {
3281 struct S { friend void f<int>(int); }
3282 }
3283
3284 we will have set the DECL_CONTEXT for the friend
3285 declaration to N, rather than to the global namespace. */
3286 if (DECL_NAMESPACE_SCOPE_P (decl))
3287 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3288
3289 if (is_friend && !have_def)
3290 /* This is not really a declaration of a specialization.
3291 It's just the name of an instantiation. But, it's not
3292 a request for an instantiation, either. */
3293 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3294 else if (TREE_CODE (decl) == FUNCTION_DECL)
3295 /* A specialization is not necessarily COMDAT. */
3296 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3297 && DECL_DECLARED_INLINE_P (decl));
3298 else if (VAR_P (decl))
3299 DECL_COMDAT (decl) = false;
3300
3301 /* If this is a full specialization, register it so that we can find
3302 it again. Partial specializations will be registered in
3303 process_partial_specialization. */
3304 if (!processing_template_decl)
3305 {
3306 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3307
3308 decl = register_specialization (decl, gen_tmpl, targs,
3309 is_friend, 0);
3310 }
3311
3312
3313 /* A 'structor should already have clones. */
3314 gcc_assert (decl == error_mark_node
3315 || variable_template_p (tmpl)
3316 || !(DECL_CONSTRUCTOR_P (decl)
3317 || DECL_DESTRUCTOR_P (decl))
3318 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3319 }
3320 }
3321
3322 return decl;
3323 }
3324
3325 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3326 parameters. These are represented in the same format used for
3327 DECL_TEMPLATE_PARMS. */
3328
3329 int
3330 comp_template_parms (const_tree parms1, const_tree parms2)
3331 {
3332 const_tree p1;
3333 const_tree p2;
3334
3335 if (parms1 == parms2)
3336 return 1;
3337
3338 for (p1 = parms1, p2 = parms2;
3339 p1 != NULL_TREE && p2 != NULL_TREE;
3340 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3341 {
3342 tree t1 = TREE_VALUE (p1);
3343 tree t2 = TREE_VALUE (p2);
3344 int i;
3345
3346 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3347 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3348
3349 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3350 return 0;
3351
3352 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3353 {
3354 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3355 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3356
3357 /* If either of the template parameters are invalid, assume
3358 they match for the sake of error recovery. */
3359 if (error_operand_p (parm1) || error_operand_p (parm2))
3360 return 1;
3361
3362 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3363 return 0;
3364
3365 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3366 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3367 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3368 continue;
3369 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3370 return 0;
3371 }
3372 }
3373
3374 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3375 /* One set of parameters has more parameters lists than the
3376 other. */
3377 return 0;
3378
3379 return 1;
3380 }
3381
3382 /* Returns true if two template parameters are declared with
3383 equivalent constraints. */
3384
3385 static bool
3386 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3387 {
3388 tree req1 = TREE_TYPE (parm1);
3389 tree req2 = TREE_TYPE (parm2);
3390 if (!req1 != !req2)
3391 return false;
3392 if (req1)
3393 return cp_tree_equal (req1, req2);
3394 return true;
3395 }
3396
3397 /* Returns true when two template parameters are equivalent. */
3398
3399 static bool
3400 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3401 {
3402 tree decl1 = TREE_VALUE (parm1);
3403 tree decl2 = TREE_VALUE (parm2);
3404
3405 /* If either of the template parameters are invalid, assume
3406 they match for the sake of error recovery. */
3407 if (error_operand_p (decl1) || error_operand_p (decl2))
3408 return true;
3409
3410 /* ... they declare parameters of the same kind. */
3411 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3412 return false;
3413
3414 /* ... one parameter was introduced by a parameter declaration, then
3415 both are. This case arises as a result of eagerly rewriting declarations
3416 during parsing. */
3417 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3418 return false;
3419
3420 /* ... if either declares a pack, they both do. */
3421 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3422 return false;
3423
3424 if (TREE_CODE (decl1) == PARM_DECL)
3425 {
3426 /* ... if they declare non-type parameters, the types are equivalent. */
3427 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3428 return false;
3429 }
3430 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3431 {
3432 /* ... if they declare template template parameters, their template
3433 parameter lists are equivalent. */
3434 if (!template_heads_equivalent_p (decl1, decl2))
3435 return false;
3436 }
3437
3438 /* ... if they are declared with a qualified-concept name, they both
3439 are, and those names are equivalent. */
3440 return template_parameter_constraints_equivalent_p (parm1, parm2);
3441 }
3442
3443 /* Returns true if two template parameters lists are equivalent.
3444 Two template parameter lists are equivalent if they have the
3445 same length and their corresponding parameters are equivalent.
3446
3447 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3448 data structure returned by DECL_TEMPLATE_PARMS.
3449
3450 This is generally the same implementation as comp_template_parms
3451 except that it also the concept names and arguments used to
3452 introduce parameters. */
3453
3454 static bool
3455 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3456 {
3457 if (parms1 == parms2)
3458 return true;
3459
3460 const_tree p1 = parms1;
3461 const_tree p2 = parms2;
3462 while (p1 != NULL_TREE && p2 != NULL_TREE)
3463 {
3464 tree list1 = TREE_VALUE (p1);
3465 tree list2 = TREE_VALUE (p2);
3466
3467 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3468 return 0;
3469
3470 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3471 {
3472 tree parm1 = TREE_VEC_ELT (list1, i);
3473 tree parm2 = TREE_VEC_ELT (list2, i);
3474 if (!template_parameters_equivalent_p (parm1, parm2))
3475 return false;
3476 }
3477
3478 p1 = TREE_CHAIN (p1);
3479 p2 = TREE_CHAIN (p2);
3480 }
3481
3482 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3483 return false;
3484
3485 return true;
3486 }
3487
3488 /* Return true if the requires-clause of the template parameter lists are
3489 equivalent and false otherwise. */
3490 static bool
3491 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3492 {
3493 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3494 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3495 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3496 return false;
3497 if (!cp_tree_equal (req1, req2))
3498 return false;
3499 return true;
3500 }
3501
3502 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3503 Two template heads are equivalent if their template parameter
3504 lists are equivalent and their requires clauses are equivalent.
3505
3506 In pre-C++20, this is equivalent to calling comp_template_parms
3507 for the template parameters of TMPL1 and TMPL2. */
3508
3509 bool
3510 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3511 {
3512 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3513 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3514
3515 /* Don't change the matching rules for pre-C++20. */
3516 if (cxx_dialect < cxx20)
3517 return comp_template_parms (parms1, parms2);
3518
3519 /* ... have the same number of template parameters, and their
3520 corresponding parameters are equivalent. */
3521 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3522 return false;
3523
3524 /* ... if either has a requires-clause, they both do and their
3525 corresponding constraint-expressions are equivalent. */
3526 return template_requirements_equivalent_p (parms1, parms2);
3527 }
3528
3529 /* Determine whether PARM is a parameter pack. */
3530
3531 bool
3532 template_parameter_pack_p (const_tree parm)
3533 {
3534 /* Determine if we have a non-type template parameter pack. */
3535 if (TREE_CODE (parm) == PARM_DECL)
3536 return (DECL_TEMPLATE_PARM_P (parm)
3537 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3538 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3539 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3540
3541 /* If this is a list of template parameters, we could get a
3542 TYPE_DECL or a TEMPLATE_DECL. */
3543 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3544 parm = TREE_TYPE (parm);
3545
3546 /* Otherwise it must be a type template parameter. */
3547 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3548 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3549 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3550 }
3551
3552 /* Determine if T is a function parameter pack. */
3553
3554 bool
3555 function_parameter_pack_p (const_tree t)
3556 {
3557 if (t && TREE_CODE (t) == PARM_DECL)
3558 return DECL_PACK_P (t);
3559 return false;
3560 }
3561
3562 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3563 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3564
3565 tree
3566 get_function_template_decl (const_tree primary_func_tmpl_inst)
3567 {
3568 if (! primary_func_tmpl_inst
3569 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3570 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3571 return NULL;
3572
3573 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3574 }
3575
3576 /* Return true iff the function parameter PARAM_DECL was expanded
3577 from the function parameter pack PACK. */
3578
3579 bool
3580 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3581 {
3582 if (DECL_ARTIFICIAL (param_decl)
3583 || !function_parameter_pack_p (pack))
3584 return false;
3585
3586 /* The parameter pack and its pack arguments have the same
3587 DECL_PARM_INDEX. */
3588 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3589 }
3590
3591 /* Determine whether ARGS describes a variadic template args list,
3592 i.e., one that is terminated by a template argument pack. */
3593
3594 static bool
3595 template_args_variadic_p (tree args)
3596 {
3597 int nargs;
3598 tree last_parm;
3599
3600 if (args == NULL_TREE)
3601 return false;
3602
3603 args = INNERMOST_TEMPLATE_ARGS (args);
3604 nargs = TREE_VEC_LENGTH (args);
3605
3606 if (nargs == 0)
3607 return false;
3608
3609 last_parm = TREE_VEC_ELT (args, nargs - 1);
3610
3611 return ARGUMENT_PACK_P (last_parm);
3612 }
3613
3614 /* Generate a new name for the parameter pack name NAME (an
3615 IDENTIFIER_NODE) that incorporates its */
3616
3617 static tree
3618 make_ith_pack_parameter_name (tree name, int i)
3619 {
3620 /* Munge the name to include the parameter index. */
3621 #define NUMBUF_LEN 128
3622 char numbuf[NUMBUF_LEN];
3623 char* newname;
3624 int newname_len;
3625
3626 if (name == NULL_TREE)
3627 return name;
3628 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3629 newname_len = IDENTIFIER_LENGTH (name)
3630 + strlen (numbuf) + 2;
3631 newname = (char*)alloca (newname_len);
3632 snprintf (newname, newname_len,
3633 "%s#%i", IDENTIFIER_POINTER (name), i);
3634 return get_identifier (newname);
3635 }
3636
3637 /* Return true if T is a primary function, class or alias template
3638 specialization, not including the template pattern. */
3639
3640 bool
3641 primary_template_specialization_p (const_tree t)
3642 {
3643 if (!t)
3644 return false;
3645
3646 if (VAR_OR_FUNCTION_DECL_P (t))
3647 return (DECL_LANG_SPECIFIC (t)
3648 && DECL_USE_TEMPLATE (t)
3649 && DECL_TEMPLATE_INFO (t)
3650 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3651 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3652 return (CLASSTYPE_TEMPLATE_INFO (t)
3653 && CLASSTYPE_USE_TEMPLATE (t)
3654 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3655 else if (alias_template_specialization_p (t, nt_transparent))
3656 return true;
3657 return false;
3658 }
3659
3660 /* Return true if PARM is a template template parameter. */
3661
3662 bool
3663 template_template_parameter_p (const_tree parm)
3664 {
3665 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3666 }
3667
3668 /* Return true iff PARM is a DECL representing a type template
3669 parameter. */
3670
3671 bool
3672 template_type_parameter_p (const_tree parm)
3673 {
3674 return (parm
3675 && (TREE_CODE (parm) == TYPE_DECL
3676 || TREE_CODE (parm) == TEMPLATE_DECL)
3677 && DECL_TEMPLATE_PARM_P (parm));
3678 }
3679
3680 /* Return the template parameters of T if T is a
3681 primary template instantiation, NULL otherwise. */
3682
3683 tree
3684 get_primary_template_innermost_parameters (const_tree t)
3685 {
3686 tree parms = NULL, template_info = NULL;
3687
3688 if ((template_info = get_template_info (t))
3689 && primary_template_specialization_p (t))
3690 parms = INNERMOST_TEMPLATE_PARMS
3691 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3692
3693 return parms;
3694 }
3695
3696 /* Return the template parameters of the LEVELth level from the full list
3697 of template parameters PARMS. */
3698
3699 tree
3700 get_template_parms_at_level (tree parms, int level)
3701 {
3702 tree p;
3703 if (!parms
3704 || TREE_CODE (parms) != TREE_LIST
3705 || level > TMPL_PARMS_DEPTH (parms))
3706 return NULL_TREE;
3707
3708 for (p = parms; p; p = TREE_CHAIN (p))
3709 if (TMPL_PARMS_DEPTH (p) == level)
3710 return p;
3711
3712 return NULL_TREE;
3713 }
3714
3715 /* Returns the template arguments of T if T is a template instantiation,
3716 NULL otherwise. */
3717
3718 tree
3719 get_template_innermost_arguments (const_tree t)
3720 {
3721 tree args = NULL, template_info = NULL;
3722
3723 if ((template_info = get_template_info (t))
3724 && TI_ARGS (template_info))
3725 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3726
3727 return args;
3728 }
3729
3730 /* Return the argument pack elements of T if T is a template argument pack,
3731 NULL otherwise. */
3732
3733 tree
3734 get_template_argument_pack_elems (const_tree t)
3735 {
3736 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3737 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3738 return NULL;
3739
3740 return ARGUMENT_PACK_ARGS (t);
3741 }
3742
3743 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3744 ARGUMENT_PACK_SELECT represents. */
3745
3746 static tree
3747 argument_pack_select_arg (tree t)
3748 {
3749 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3750 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3751
3752 /* If the selected argument is an expansion E, that most likely means we were
3753 called from gen_elem_of_pack_expansion_instantiation during the
3754 substituting of an argument pack (of which the Ith element is a pack
3755 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3756 In this case, the Ith element resulting from this substituting is going to
3757 be a pack expansion, which pattern is the pattern of E. Let's return the
3758 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3759 resulting pack expansion from it. */
3760 if (PACK_EXPANSION_P (arg))
3761 {
3762 /* Make sure we aren't throwing away arg info. */
3763 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3764 arg = PACK_EXPANSION_PATTERN (arg);
3765 }
3766
3767 return arg;
3768 }
3769
3770
3771 /* True iff FN is a function representing a built-in variadic parameter
3772 pack. */
3773
3774 bool
3775 builtin_pack_fn_p (tree fn)
3776 {
3777 if (!fn
3778 || TREE_CODE (fn) != FUNCTION_DECL
3779 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3780 return false;
3781
3782 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3783 return true;
3784
3785 return false;
3786 }
3787
3788 /* True iff CALL is a call to a function representing a built-in variadic
3789 parameter pack. */
3790
3791 static bool
3792 builtin_pack_call_p (tree call)
3793 {
3794 if (TREE_CODE (call) != CALL_EXPR)
3795 return false;
3796 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3797 }
3798
3799 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3800
3801 static tree
3802 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3803 tree in_decl)
3804 {
3805 tree ohi = CALL_EXPR_ARG (call, 0);
3806 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3807 false/*fn*/, true/*int_cst*/);
3808
3809 if (value_dependent_expression_p (hi))
3810 {
3811 if (hi != ohi)
3812 {
3813 call = copy_node (call);
3814 CALL_EXPR_ARG (call, 0) = hi;
3815 }
3816 tree ex = make_pack_expansion (call, complain);
3817 tree vec = make_tree_vec (1);
3818 TREE_VEC_ELT (vec, 0) = ex;
3819 return vec;
3820 }
3821 else
3822 {
3823 hi = instantiate_non_dependent_expr_sfinae (hi, complain);
3824 hi = cxx_constant_value (hi);
3825 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3826
3827 /* Calculate the largest value of len that won't make the size of the vec
3828 overflow an int. The compiler will exceed resource limits long before
3829 this, but it seems a decent place to diagnose. */
3830 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3831
3832 if (len < 0 || len > max)
3833 {
3834 if ((complain & tf_error)
3835 && hi != error_mark_node)
3836 error ("argument to %<__integer_pack%> must be between 0 and %d",
3837 max);
3838 return error_mark_node;
3839 }
3840
3841 tree vec = make_tree_vec (len);
3842
3843 for (int i = 0; i < len; ++i)
3844 TREE_VEC_ELT (vec, i) = size_int (i);
3845
3846 return vec;
3847 }
3848 }
3849
3850 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3851 CALL. */
3852
3853 static tree
3854 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3855 tree in_decl)
3856 {
3857 if (!builtin_pack_call_p (call))
3858 return NULL_TREE;
3859
3860 tree fn = CALL_EXPR_FN (call);
3861
3862 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3863 return expand_integer_pack (call, args, complain, in_decl);
3864
3865 return NULL_TREE;
3866 }
3867
3868 /* Structure used to track the progress of find_parameter_packs_r. */
3869 struct find_parameter_pack_data
3870 {
3871 /* TREE_LIST that will contain all of the parameter packs found by
3872 the traversal. */
3873 tree* parameter_packs;
3874
3875 /* Set of AST nodes that have been visited by the traversal. */
3876 hash_set<tree> *visited;
3877
3878 /* True iff we're making a type pack expansion. */
3879 bool type_pack_expansion_p;
3880 };
3881
3882 /* Identifies all of the argument packs that occur in a template
3883 argument and appends them to the TREE_LIST inside DATA, which is a
3884 find_parameter_pack_data structure. This is a subroutine of
3885 make_pack_expansion and uses_parameter_packs. */
3886 static tree
3887 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3888 {
3889 tree t = *tp;
3890 struct find_parameter_pack_data* ppd =
3891 (struct find_parameter_pack_data*)data;
3892 bool parameter_pack_p = false;
3893
3894 #define WALK_SUBTREE(NODE) \
3895 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3896 ppd, ppd->visited) \
3897
3898 /* Don't look through typedefs; we are interested in whether a
3899 parameter pack is actually written in the expression/type we're
3900 looking at, not the target type. */
3901 if (TYPE_P (t) && typedef_variant_p (t))
3902 {
3903 /* But do look at arguments for an alias template. */
3904 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3905 cp_walk_tree (&TI_ARGS (tinfo),
3906 &find_parameter_packs_r,
3907 ppd, ppd->visited);
3908 *walk_subtrees = 0;
3909 return NULL_TREE;
3910 }
3911
3912 /* Identify whether this is a parameter pack or not. */
3913 switch (TREE_CODE (t))
3914 {
3915 case TEMPLATE_PARM_INDEX:
3916 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3917 parameter_pack_p = true;
3918 break;
3919
3920 case TEMPLATE_TYPE_PARM:
3921 t = TYPE_MAIN_VARIANT (t);
3922 /* FALLTHRU */
3923 case TEMPLATE_TEMPLATE_PARM:
3924 /* If the placeholder appears in the decl-specifier-seq of a function
3925 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3926 is a pack expansion, the invented template parameter is a template
3927 parameter pack. */
3928 if (ppd->type_pack_expansion_p && is_auto (t))
3929 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3930 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3931 parameter_pack_p = true;
3932 break;
3933
3934 case FIELD_DECL:
3935 case PARM_DECL:
3936 if (DECL_PACK_P (t))
3937 {
3938 /* We don't want to walk into the type of a PARM_DECL,
3939 because we don't want to see the type parameter pack. */
3940 *walk_subtrees = 0;
3941 parameter_pack_p = true;
3942 }
3943 break;
3944
3945 case VAR_DECL:
3946 if (DECL_PACK_P (t))
3947 {
3948 /* We don't want to walk into the type of a variadic capture proxy,
3949 because we don't want to see the type parameter pack. */
3950 *walk_subtrees = 0;
3951 parameter_pack_p = true;
3952 }
3953 else if (variable_template_specialization_p (t))
3954 {
3955 cp_walk_tree (&DECL_TI_ARGS (t),
3956 find_parameter_packs_r,
3957 ppd, ppd->visited);
3958 *walk_subtrees = 0;
3959 }
3960 break;
3961
3962 case CALL_EXPR:
3963 if (builtin_pack_call_p (t))
3964 parameter_pack_p = true;
3965 break;
3966
3967 case BASES:
3968 parameter_pack_p = true;
3969 break;
3970 default:
3971 /* Not a parameter pack. */
3972 break;
3973 }
3974
3975 if (parameter_pack_p)
3976 {
3977 /* Add this parameter pack to the list. */
3978 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3979 }
3980
3981 if (TYPE_P (t))
3982 cp_walk_tree (&TYPE_CONTEXT (t),
3983 &find_parameter_packs_r, ppd, ppd->visited);
3984
3985 /* This switch statement will return immediately if we don't find a
3986 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3987 switch (TREE_CODE (t))
3988 {
3989 case BOUND_TEMPLATE_TEMPLATE_PARM:
3990 /* Check the template itself. */
3991 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3992 &find_parameter_packs_r, ppd, ppd->visited);
3993 return NULL_TREE;
3994
3995 case DECL_EXPR:
3996 {
3997 tree decl = DECL_EXPR_DECL (t);
3998 /* Ignore the declaration of a capture proxy for a parameter pack. */
3999 if (is_capture_proxy (decl))
4000 *walk_subtrees = 0;
4001 if (is_typedef_decl (decl))
4002 /* Since we stop at typedefs above, we need to look through them at
4003 the point of the DECL_EXPR. */
4004 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
4005 &find_parameter_packs_r, ppd, ppd->visited);
4006 return NULL_TREE;
4007 }
4008
4009 case TEMPLATE_DECL:
4010 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4011 return NULL_TREE;
4012 cp_walk_tree (&TREE_TYPE (t),
4013 &find_parameter_packs_r, ppd, ppd->visited);
4014 return NULL_TREE;
4015
4016 case TYPE_PACK_EXPANSION:
4017 case EXPR_PACK_EXPANSION:
4018 *walk_subtrees = 0;
4019 return NULL_TREE;
4020
4021 case INTEGER_TYPE:
4022 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4023 ppd, ppd->visited);
4024 *walk_subtrees = 0;
4025 return NULL_TREE;
4026
4027 case IDENTIFIER_NODE:
4028 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4029 ppd->visited);
4030 *walk_subtrees = 0;
4031 return NULL_TREE;
4032
4033 case LAMBDA_EXPR:
4034 {
4035 /* Since we defer implicit capture, look in the parms and body. */
4036 tree fn = lambda_function (t);
4037 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4038 ppd->visited);
4039 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4040 ppd->visited);
4041 return NULL_TREE;
4042 }
4043
4044 case DECLTYPE_TYPE:
4045 {
4046 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4047 type_pack_expansion_p to false so that any placeholders
4048 within the expression don't get marked as parameter packs. */
4049 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4050 ppd->type_pack_expansion_p = false;
4051 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4052 ppd, ppd->visited);
4053 ppd->type_pack_expansion_p = type_pack_expansion_p;
4054 *walk_subtrees = 0;
4055 return NULL_TREE;
4056 }
4057
4058 case IF_STMT:
4059 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4060 ppd, ppd->visited);
4061 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4062 ppd, ppd->visited);
4063 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4064 ppd, ppd->visited);
4065 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4066 *walk_subtrees = 0;
4067 return NULL_TREE;
4068
4069 case TAG_DEFN:
4070 /* Local class, need to look through the whole definition. */
4071 t = TREE_TYPE (t);
4072 if (CLASS_TYPE_P (t))
4073 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4074 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4075 ppd, ppd->visited);
4076 return NULL_TREE;
4077
4078 case FUNCTION_TYPE:
4079 case METHOD_TYPE:
4080 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4081 break;
4082
4083 default:
4084 return NULL_TREE;
4085 }
4086
4087 #undef WALK_SUBTREE
4088
4089 return NULL_TREE;
4090 }
4091
4092 /* Determines if the expression or type T uses any parameter packs. */
4093 tree
4094 uses_parameter_packs (tree t)
4095 {
4096 tree parameter_packs = NULL_TREE;
4097 struct find_parameter_pack_data ppd;
4098 ppd.parameter_packs = &parameter_packs;
4099 ppd.visited = new hash_set<tree>;
4100 ppd.type_pack_expansion_p = false;
4101 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4102 delete ppd.visited;
4103 return parameter_packs;
4104 }
4105
4106 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4107 representation a base-class initializer into a parameter pack
4108 expansion. If all goes well, the resulting node will be an
4109 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4110 respectively. */
4111 tree
4112 make_pack_expansion (tree arg, tsubst_flags_t complain)
4113 {
4114 tree result;
4115 tree parameter_packs = NULL_TREE;
4116 bool for_types = false;
4117 struct find_parameter_pack_data ppd;
4118
4119 if (!arg || arg == error_mark_node)
4120 return arg;
4121
4122 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4123 {
4124 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4125 class initializer. In this case, the TREE_PURPOSE will be a
4126 _TYPE node (representing the base class expansion we're
4127 initializing) and the TREE_VALUE will be a TREE_LIST
4128 containing the initialization arguments.
4129
4130 The resulting expansion looks somewhat different from most
4131 expansions. Rather than returning just one _EXPANSION, we
4132 return a TREE_LIST whose TREE_PURPOSE is a
4133 TYPE_PACK_EXPANSION containing the bases that will be
4134 initialized. The TREE_VALUE will be identical to the
4135 original TREE_VALUE, which is a list of arguments that will
4136 be passed to each base. We do not introduce any new pack
4137 expansion nodes into the TREE_VALUE (although it is possible
4138 that some already exist), because the TREE_PURPOSE and
4139 TREE_VALUE all need to be expanded together with the same
4140 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4141 resulting TREE_PURPOSE will mention the parameter packs in
4142 both the bases and the arguments to the bases. */
4143 tree purpose;
4144 tree value;
4145 tree parameter_packs = NULL_TREE;
4146
4147 /* Determine which parameter packs will be used by the base
4148 class expansion. */
4149 ppd.visited = new hash_set<tree>;
4150 ppd.parameter_packs = &parameter_packs;
4151 ppd.type_pack_expansion_p = false;
4152 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4153 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4154 &ppd, ppd.visited);
4155
4156 if (parameter_packs == NULL_TREE)
4157 {
4158 if (complain & tf_error)
4159 error ("base initializer expansion %qT contains no parameter packs",
4160 arg);
4161 delete ppd.visited;
4162 return error_mark_node;
4163 }
4164
4165 if (TREE_VALUE (arg) != void_type_node)
4166 {
4167 /* Collect the sets of parameter packs used in each of the
4168 initialization arguments. */
4169 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4170 {
4171 /* Determine which parameter packs will be expanded in this
4172 argument. */
4173 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4174 &ppd, ppd.visited);
4175 }
4176 }
4177
4178 delete ppd.visited;
4179
4180 /* Create the pack expansion type for the base type. */
4181 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4182 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4183 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4184 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4185
4186 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4187 they will rarely be compared to anything. */
4188 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4189
4190 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4191 }
4192
4193 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4194 for_types = true;
4195
4196 /* Build the PACK_EXPANSION_* node. */
4197 result = for_types
4198 ? cxx_make_type (TYPE_PACK_EXPANSION)
4199 : make_node (EXPR_PACK_EXPANSION);
4200 SET_PACK_EXPANSION_PATTERN (result, arg);
4201 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4202 {
4203 /* Propagate type and const-expression information. */
4204 TREE_TYPE (result) = TREE_TYPE (arg);
4205 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4206 /* Mark this read now, since the expansion might be length 0. */
4207 mark_exp_read (arg);
4208 }
4209 else
4210 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4211 they will rarely be compared to anything. */
4212 SET_TYPE_STRUCTURAL_EQUALITY (result);
4213
4214 /* Determine which parameter packs will be expanded. */
4215 ppd.parameter_packs = &parameter_packs;
4216 ppd.visited = new hash_set<tree>;
4217 ppd.type_pack_expansion_p = TYPE_P (arg);
4218 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4219 delete ppd.visited;
4220
4221 /* Make sure we found some parameter packs. */
4222 if (parameter_packs == NULL_TREE)
4223 {
4224 if (complain & tf_error)
4225 {
4226 if (TYPE_P (arg))
4227 error ("expansion pattern %qT contains no parameter packs", arg);
4228 else
4229 error ("expansion pattern %qE contains no parameter packs", arg);
4230 }
4231 return error_mark_node;
4232 }
4233 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4234
4235 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4236
4237 return result;
4238 }
4239
4240 /* Checks T for any "bare" parameter packs, which have not yet been
4241 expanded, and issues an error if any are found. This operation can
4242 only be done on full expressions or types (e.g., an expression
4243 statement, "if" condition, etc.), because we could have expressions like:
4244
4245 foo(f(g(h(args)))...)
4246
4247 where "args" is a parameter pack. check_for_bare_parameter_packs
4248 should not be called for the subexpressions args, h(args),
4249 g(h(args)), or f(g(h(args))), because we would produce erroneous
4250 error messages.
4251
4252 Returns TRUE and emits an error if there were bare parameter packs,
4253 returns FALSE otherwise. */
4254 bool
4255 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4256 {
4257 tree parameter_packs = NULL_TREE;
4258 struct find_parameter_pack_data ppd;
4259
4260 if (!processing_template_decl || !t || t == error_mark_node)
4261 return false;
4262
4263 if (TREE_CODE (t) == TYPE_DECL)
4264 t = TREE_TYPE (t);
4265
4266 ppd.parameter_packs = &parameter_packs;
4267 ppd.visited = new hash_set<tree>;
4268 ppd.type_pack_expansion_p = false;
4269 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4270 delete ppd.visited;
4271
4272 /* It's OK for a lambda to have an unexpanded parameter pack from the
4273 containing context, but do complain about unexpanded capture packs. */
4274 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4275 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4276 for (; parameter_packs;
4277 parameter_packs = TREE_CHAIN (parameter_packs))
4278 {
4279 tree pack = TREE_VALUE (parameter_packs);
4280 if (is_capture_proxy (pack))
4281 break;
4282 }
4283
4284 if (parameter_packs)
4285 {
4286 if (loc == UNKNOWN_LOCATION)
4287 loc = cp_expr_loc_or_input_loc (t);
4288 error_at (loc, "parameter packs not expanded with %<...%>:");
4289 while (parameter_packs)
4290 {
4291 tree pack = TREE_VALUE (parameter_packs);
4292 tree name = NULL_TREE;
4293
4294 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4295 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4296 name = TYPE_NAME (pack);
4297 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4298 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4299 else if (TREE_CODE (pack) == CALL_EXPR)
4300 name = DECL_NAME (CALL_EXPR_FN (pack));
4301 else
4302 name = DECL_NAME (pack);
4303
4304 if (name)
4305 inform (loc, " %qD", name);
4306 else
4307 inform (loc, " %s", "<anonymous>");
4308
4309 parameter_packs = TREE_CHAIN (parameter_packs);
4310 }
4311
4312 return true;
4313 }
4314
4315 return false;
4316 }
4317
4318 /* Expand any parameter packs that occur in the template arguments in
4319 ARGS. */
4320 tree
4321 expand_template_argument_pack (tree args)
4322 {
4323 if (args == error_mark_node)
4324 return error_mark_node;
4325
4326 tree result_args = NULL_TREE;
4327 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4328 int num_result_args = -1;
4329 int non_default_args_count = -1;
4330
4331 /* First, determine if we need to expand anything, and the number of
4332 slots we'll need. */
4333 for (in_arg = 0; in_arg < nargs; ++in_arg)
4334 {
4335 tree arg = TREE_VEC_ELT (args, in_arg);
4336 if (arg == NULL_TREE)
4337 return args;
4338 if (ARGUMENT_PACK_P (arg))
4339 {
4340 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4341 if (num_result_args < 0)
4342 num_result_args = in_arg + num_packed;
4343 else
4344 num_result_args += num_packed;
4345 }
4346 else
4347 {
4348 if (num_result_args >= 0)
4349 num_result_args++;
4350 }
4351 }
4352
4353 /* If no expansion is necessary, we're done. */
4354 if (num_result_args < 0)
4355 return args;
4356
4357 /* Expand arguments. */
4358 result_args = make_tree_vec (num_result_args);
4359 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4360 non_default_args_count =
4361 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4362 for (in_arg = 0; in_arg < nargs; ++in_arg)
4363 {
4364 tree arg = TREE_VEC_ELT (args, in_arg);
4365 if (ARGUMENT_PACK_P (arg))
4366 {
4367 tree packed = ARGUMENT_PACK_ARGS (arg);
4368 int i, num_packed = TREE_VEC_LENGTH (packed);
4369 for (i = 0; i < num_packed; ++i, ++out_arg)
4370 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4371 if (non_default_args_count > 0)
4372 non_default_args_count += num_packed - 1;
4373 }
4374 else
4375 {
4376 TREE_VEC_ELT (result_args, out_arg) = arg;
4377 ++out_arg;
4378 }
4379 }
4380 if (non_default_args_count >= 0)
4381 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4382 return result_args;
4383 }
4384
4385 /* Checks if DECL shadows a template parameter.
4386
4387 [temp.local]: A template-parameter shall not be redeclared within its
4388 scope (including nested scopes).
4389
4390 Emits an error and returns TRUE if the DECL shadows a parameter,
4391 returns FALSE otherwise. */
4392
4393 bool
4394 check_template_shadow (tree decl)
4395 {
4396 tree olddecl;
4397
4398 /* If we're not in a template, we can't possibly shadow a template
4399 parameter. */
4400 if (!current_template_parms)
4401 return true;
4402
4403 /* Figure out what we're shadowing. */
4404 decl = OVL_FIRST (decl);
4405 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4406
4407 /* If there's no previous binding for this name, we're not shadowing
4408 anything, let alone a template parameter. */
4409 if (!olddecl)
4410 return true;
4411
4412 /* If we're not shadowing a template parameter, we're done. Note
4413 that OLDDECL might be an OVERLOAD (or perhaps even an
4414 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4415 node. */
4416 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4417 return true;
4418
4419 /* We check for decl != olddecl to avoid bogus errors for using a
4420 name inside a class. We check TPFI to avoid duplicate errors for
4421 inline member templates. */
4422 if (decl == olddecl
4423 || (DECL_TEMPLATE_PARM_P (decl)
4424 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4425 return true;
4426
4427 /* Don't complain about the injected class name, as we've already
4428 complained about the class itself. */
4429 if (DECL_SELF_REFERENCE_P (decl))
4430 return false;
4431
4432 if (DECL_TEMPLATE_PARM_P (decl))
4433 error ("declaration of template parameter %q+D shadows "
4434 "template parameter", decl);
4435 else
4436 error ("declaration of %q+#D shadows template parameter", decl);
4437 inform (DECL_SOURCE_LOCATION (olddecl),
4438 "template parameter %qD declared here", olddecl);
4439 return false;
4440 }
4441
4442 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4443 ORIG_LEVEL, DECL, and TYPE. */
4444
4445 static tree
4446 build_template_parm_index (int index,
4447 int level,
4448 int orig_level,
4449 tree decl,
4450 tree type)
4451 {
4452 tree t = make_node (TEMPLATE_PARM_INDEX);
4453 TEMPLATE_PARM_IDX (t) = index;
4454 TEMPLATE_PARM_LEVEL (t) = level;
4455 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4456 TEMPLATE_PARM_DECL (t) = decl;
4457 TREE_TYPE (t) = type;
4458 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4459 TREE_READONLY (t) = TREE_READONLY (decl);
4460
4461 return t;
4462 }
4463
4464 /* Find the canonical type parameter for the given template type
4465 parameter. Returns the canonical type parameter, which may be TYPE
4466 if no such parameter existed. */
4467
4468 tree
4469 canonical_type_parameter (tree type)
4470 {
4471 int idx = TEMPLATE_TYPE_IDX (type);
4472
4473 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4474
4475 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4476 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4477
4478 for (tree list = (*canonical_template_parms)[idx];
4479 list; list = TREE_CHAIN (list))
4480 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4481 return TREE_VALUE (list);
4482
4483 (*canonical_template_parms)[idx]
4484 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4485 return type;
4486 }
4487
4488 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4489 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4490 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4491 new one is created. */
4492
4493 static tree
4494 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4495 tsubst_flags_t complain)
4496 {
4497 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4498 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4499 != TEMPLATE_PARM_LEVEL (index) - levels)
4500 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4501 {
4502 tree orig_decl = TEMPLATE_PARM_DECL (index);
4503
4504 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4505 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4506 type);
4507 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4508 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4509 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4510 DECL_ARTIFICIAL (decl) = 1;
4511 SET_DECL_TEMPLATE_PARM_P (decl);
4512
4513 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4514 TEMPLATE_PARM_LEVEL (index) - levels,
4515 TEMPLATE_PARM_ORIG_LEVEL (index),
4516 decl, type);
4517 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4518 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4519 = TEMPLATE_PARM_PARAMETER_PACK (index);
4520
4521 /* Template template parameters need this. */
4522 tree inner = decl;
4523 if (TREE_CODE (decl) == TEMPLATE_DECL)
4524 {
4525 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4526 TYPE_DECL, DECL_NAME (decl), type);
4527 DECL_TEMPLATE_RESULT (decl) = inner;
4528 DECL_ARTIFICIAL (inner) = true;
4529 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4530 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4531 }
4532
4533 /* Attach the TPI to the decl. */
4534 if (TREE_CODE (inner) == TYPE_DECL)
4535 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4536 else
4537 DECL_INITIAL (decl) = tpi;
4538 }
4539
4540 return TEMPLATE_PARM_DESCENDANTS (index);
4541 }
4542
4543 /* Process information from new template parameter PARM and append it
4544 to the LIST being built. This new parameter is a non-type
4545 parameter iff IS_NON_TYPE is true. This new parameter is a
4546 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4547 is in PARM_LOC. */
4548
4549 tree
4550 process_template_parm (tree list, location_t parm_loc, tree parm,
4551 bool is_non_type, bool is_parameter_pack)
4552 {
4553 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4554 tree prev = NULL_TREE;
4555 int idx = 0;
4556
4557 if (list)
4558 {
4559 prev = tree_last (list);
4560
4561 tree p = TREE_VALUE (prev);
4562 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4563 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4564 else if (TREE_CODE (p) == PARM_DECL)
4565 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4566
4567 ++idx;
4568 }
4569
4570 tree decl = NULL_TREE;
4571 tree defval = TREE_PURPOSE (parm);
4572 tree constr = TREE_TYPE (parm);
4573
4574 if (is_non_type)
4575 {
4576 parm = TREE_VALUE (parm);
4577
4578 SET_DECL_TEMPLATE_PARM_P (parm);
4579
4580 if (TREE_TYPE (parm) != error_mark_node)
4581 {
4582 /* [temp.param]
4583
4584 The top-level cv-qualifiers on the template-parameter are
4585 ignored when determining its type. */
4586 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4587 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4588 TREE_TYPE (parm) = error_mark_node;
4589 else if (uses_parameter_packs (TREE_TYPE (parm))
4590 && !is_parameter_pack
4591 /* If we're in a nested template parameter list, the template
4592 template parameter could be a parameter pack. */
4593 && processing_template_parmlist == 1)
4594 {
4595 /* This template parameter is not a parameter pack, but it
4596 should be. Complain about "bare" parameter packs. */
4597 check_for_bare_parameter_packs (TREE_TYPE (parm));
4598
4599 /* Recover by calling this a parameter pack. */
4600 is_parameter_pack = true;
4601 }
4602 }
4603
4604 /* A template parameter is not modifiable. */
4605 TREE_CONSTANT (parm) = 1;
4606 TREE_READONLY (parm) = 1;
4607 decl = build_decl (parm_loc,
4608 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4609 TREE_CONSTANT (decl) = 1;
4610 TREE_READONLY (decl) = 1;
4611 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4612 = build_template_parm_index (idx, processing_template_decl,
4613 processing_template_decl,
4614 decl, TREE_TYPE (parm));
4615
4616 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4617 = is_parameter_pack;
4618 }
4619 else
4620 {
4621 tree t;
4622 parm = TREE_VALUE (TREE_VALUE (parm));
4623
4624 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4625 {
4626 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4627 /* This is for distinguishing between real templates and template
4628 template parameters */
4629 TREE_TYPE (parm) = t;
4630
4631 /* any_template_parm_r expects to be able to get the targs of a
4632 DECL_TEMPLATE_RESULT. */
4633 tree result = DECL_TEMPLATE_RESULT (parm);
4634 TREE_TYPE (result) = t;
4635 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4636 tree tinfo = build_template_info (parm, args);
4637 retrofit_lang_decl (result);
4638 DECL_TEMPLATE_INFO (result) = tinfo;
4639
4640 decl = parm;
4641 }
4642 else
4643 {
4644 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4645 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4646 decl = build_decl (parm_loc,
4647 TYPE_DECL, parm, t);
4648 }
4649
4650 TYPE_NAME (t) = decl;
4651 TYPE_STUB_DECL (t) = decl;
4652 parm = decl;
4653 TEMPLATE_TYPE_PARM_INDEX (t)
4654 = build_template_parm_index (idx, processing_template_decl,
4655 processing_template_decl,
4656 decl, TREE_TYPE (parm));
4657 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4658 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4659 SET_TYPE_STRUCTURAL_EQUALITY (t);
4660 else
4661 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4662 }
4663 DECL_ARTIFICIAL (decl) = 1;
4664 SET_DECL_TEMPLATE_PARM_P (decl);
4665
4666 /* Build requirements for the type/template parameter.
4667 This must be done after SET_DECL_TEMPLATE_PARM_P or
4668 process_template_parm could fail. */
4669 tree reqs = finish_shorthand_constraint (parm, constr);
4670
4671 decl = pushdecl (decl);
4672 if (!is_non_type)
4673 parm = decl;
4674
4675 /* Build the parameter node linking the parameter declaration,
4676 its default argument (if any), and its constraints (if any). */
4677 parm = build_tree_list (defval, parm);
4678 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4679
4680 if (prev)
4681 TREE_CHAIN (prev) = parm;
4682 else
4683 list = parm;
4684
4685 return list;
4686 }
4687
4688 /* The end of a template parameter list has been reached. Process the
4689 tree list into a parameter vector, converting each parameter into a more
4690 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4691 as PARM_DECLs. */
4692
4693 tree
4694 end_template_parm_list (tree parms)
4695 {
4696 tree saved_parmlist = make_tree_vec (list_length (parms));
4697
4698 /* Pop the dummy parameter level and add the real one. We do not
4699 morph the dummy parameter in place, as it might have been
4700 captured by a (nested) template-template-parm. */
4701 current_template_parms = TREE_CHAIN (current_template_parms);
4702
4703 current_template_parms
4704 = tree_cons (size_int (processing_template_decl),
4705 saved_parmlist, current_template_parms);
4706
4707 for (unsigned ix = 0; parms; ix++)
4708 {
4709 tree parm = parms;
4710 parms = TREE_CHAIN (parms);
4711 TREE_CHAIN (parm) = NULL_TREE;
4712
4713 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4714 }
4715
4716 --processing_template_parmlist;
4717
4718 return saved_parmlist;
4719 }
4720
4721 // Explicitly indicate the end of the template parameter list. We assume
4722 // that the current template parameters have been constructed and/or
4723 // managed explicitly, as when creating new template template parameters
4724 // from a shorthand constraint.
4725 void
4726 end_template_parm_list ()
4727 {
4728 --processing_template_parmlist;
4729 }
4730
4731 /* end_template_decl is called after a template declaration is seen. */
4732
4733 void
4734 end_template_decl (void)
4735 {
4736 reset_specialization ();
4737
4738 if (! processing_template_decl)
4739 return;
4740
4741 /* This matches the pushlevel in begin_template_parm_list. */
4742 finish_scope ();
4743
4744 --processing_template_decl;
4745 current_template_parms = TREE_CHAIN (current_template_parms);
4746 }
4747
4748 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4749 thereof, and converts it into an argument suitable to be passed to
4750 the type substitution functions. Note that if the TREE_LIST contains
4751 an error_mark node, the returned argument is error_mark_node. */
4752
4753 tree
4754 template_parm_to_arg (tree t)
4755 {
4756 if (!t)
4757 return NULL_TREE;
4758
4759 if (TREE_CODE (t) == TREE_LIST)
4760 t = TREE_VALUE (t);
4761
4762 if (error_operand_p (t))
4763 return error_mark_node;
4764
4765 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4766 {
4767 if (TREE_CODE (t) == TYPE_DECL
4768 || TREE_CODE (t) == TEMPLATE_DECL)
4769 t = TREE_TYPE (t);
4770 else
4771 t = DECL_INITIAL (t);
4772 }
4773
4774 gcc_assert (TEMPLATE_PARM_P (t));
4775
4776 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4777 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4778 {
4779 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4780 {
4781 /* Turn this argument into a TYPE_ARGUMENT_PACK
4782 with a single element, which expands T. */
4783 tree vec = make_tree_vec (1);
4784 if (CHECKING_P)
4785 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4786
4787 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4788
4789 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4790 SET_ARGUMENT_PACK_ARGS (t, vec);
4791 }
4792 }
4793 else
4794 {
4795 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4796 {
4797 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4798 with a single element, which expands T. */
4799 tree vec = make_tree_vec (1);
4800 if (CHECKING_P)
4801 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4802
4803 t = convert_from_reference (t);
4804 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4805
4806 t = make_node (NONTYPE_ARGUMENT_PACK);
4807 SET_ARGUMENT_PACK_ARGS (t, vec);
4808 }
4809 else
4810 t = convert_from_reference (t);
4811 }
4812 return t;
4813 }
4814
4815 /* Given a single level of template parameters (a TREE_VEC), return it
4816 as a set of template arguments. */
4817
4818 tree
4819 template_parms_level_to_args (tree parms)
4820 {
4821 tree a = copy_node (parms);
4822 TREE_TYPE (a) = NULL_TREE;
4823 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4824 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4825
4826 if (CHECKING_P)
4827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4828
4829 return a;
4830 }
4831
4832 /* Given a set of template parameters, return them as a set of template
4833 arguments. The template parameters are represented as a TREE_VEC, in
4834 the form documented in cp-tree.h for template arguments. */
4835
4836 tree
4837 template_parms_to_args (tree parms)
4838 {
4839 tree header;
4840 tree args = NULL_TREE;
4841 int length = TMPL_PARMS_DEPTH (parms);
4842 int l = length;
4843
4844 /* If there is only one level of template parameters, we do not
4845 create a TREE_VEC of TREE_VECs. Instead, we return a single
4846 TREE_VEC containing the arguments. */
4847 if (length > 1)
4848 args = make_tree_vec (length);
4849
4850 for (header = parms; header; header = TREE_CHAIN (header))
4851 {
4852 tree a = template_parms_level_to_args (TREE_VALUE (header));
4853
4854 if (length > 1)
4855 TREE_VEC_ELT (args, --l) = a;
4856 else
4857 args = a;
4858 }
4859
4860 return args;
4861 }
4862
4863 /* Within the declaration of a template, return the currently active
4864 template parameters as an argument TREE_VEC. */
4865
4866 static tree
4867 current_template_args (void)
4868 {
4869 return template_parms_to_args (current_template_parms);
4870 }
4871
4872 /* Return the fully generic arguments for of TMPL, i.e. what
4873 current_template_args would be while parsing it. */
4874
4875 tree
4876 generic_targs_for (tree tmpl)
4877 {
4878 if (tmpl == NULL_TREE)
4879 return NULL_TREE;
4880 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4881 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4882 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4883 template parameter, it has no TEMPLATE_INFO; for a partial
4884 specialization, it has the arguments for the primary template, and we
4885 want the arguments for the partial specialization. */;
4886 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4887 if (tree ti = get_template_info (result))
4888 return TI_ARGS (ti);
4889 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4890 }
4891
4892 /* Update the declared TYPE by doing any lookups which were thought to be
4893 dependent, but are not now that we know the SCOPE of the declarator. */
4894
4895 tree
4896 maybe_update_decl_type (tree orig_type, tree scope)
4897 {
4898 tree type = orig_type;
4899
4900 if (type == NULL_TREE)
4901 return type;
4902
4903 if (TREE_CODE (orig_type) == TYPE_DECL)
4904 type = TREE_TYPE (type);
4905
4906 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4907 && dependent_type_p (type)
4908 /* Don't bother building up the args in this case. */
4909 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4910 {
4911 /* tsubst in the args corresponding to the template parameters,
4912 including auto if present. Most things will be unchanged, but
4913 make_typename_type and tsubst_qualified_id will resolve
4914 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4915 tree args = current_template_args ();
4916 tree auto_node = type_uses_auto (type);
4917 tree pushed;
4918 if (auto_node)
4919 {
4920 tree auto_vec = make_tree_vec (1);
4921 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4922 args = add_to_template_args (args, auto_vec);
4923 }
4924 pushed = push_scope (scope);
4925 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4926 if (pushed)
4927 pop_scope (scope);
4928 }
4929
4930 if (type == error_mark_node)
4931 return orig_type;
4932
4933 if (TREE_CODE (orig_type) == TYPE_DECL)
4934 {
4935 if (same_type_p (type, TREE_TYPE (orig_type)))
4936 type = orig_type;
4937 else
4938 type = TYPE_NAME (type);
4939 }
4940 return type;
4941 }
4942
4943 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4944 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4945 the new template is a member template. */
4946
4947 static tree
4948 build_template_decl (tree decl, tree parms, bool member_template_p)
4949 {
4950 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4951 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4952 DECL_TEMPLATE_PARMS (tmpl) = parms;
4953 DECL_TEMPLATE_RESULT (tmpl) = decl;
4954 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4955 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4956 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4957 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4958
4959 /* Propagate module information from the decl. */
4960 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
4961
4962 return tmpl;
4963 }
4964
4965 struct template_parm_data
4966 {
4967 /* The level of the template parameters we are currently
4968 processing. */
4969 int level;
4970
4971 /* The index of the specialization argument we are currently
4972 processing. */
4973 int current_arg;
4974
4975 /* An array whose size is the number of template parameters. The
4976 elements are nonzero if the parameter has been used in any one
4977 of the arguments processed so far. */
4978 int* parms;
4979
4980 /* An array whose size is the number of template arguments. The
4981 elements are nonzero if the argument makes use of template
4982 parameters of this level. */
4983 int* arg_uses_template_parms;
4984 };
4985
4986 /* Subroutine of push_template_decl used to see if each template
4987 parameter in a partial specialization is used in the explicit
4988 argument list. If T is of the LEVEL given in DATA (which is
4989 treated as a template_parm_data*), then DATA->PARMS is marked
4990 appropriately. */
4991
4992 static int
4993 mark_template_parm (tree t, void* data)
4994 {
4995 int level;
4996 int idx;
4997 struct template_parm_data* tpd = (struct template_parm_data*) data;
4998
4999 template_parm_level_and_index (t, &level, &idx);
5000
5001 if (level == tpd->level)
5002 {
5003 tpd->parms[idx] = 1;
5004 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5005 }
5006
5007 /* In C++17 the type of a non-type argument is a deduced context. */
5008 if (cxx_dialect >= cxx17
5009 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5010 for_each_template_parm (TREE_TYPE (t),
5011 &mark_template_parm,
5012 data,
5013 NULL,
5014 /*include_nondeduced_p=*/false);
5015
5016 /* Return zero so that for_each_template_parm will continue the
5017 traversal of the tree; we want to mark *every* template parm. */
5018 return 0;
5019 }
5020
5021 /* Process the partial specialization DECL. */
5022
5023 static tree
5024 process_partial_specialization (tree decl)
5025 {
5026 tree type = TREE_TYPE (decl);
5027 tree tinfo = get_template_info (decl);
5028 tree maintmpl = TI_TEMPLATE (tinfo);
5029 tree specargs = TI_ARGS (tinfo);
5030 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5031 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5032 tree inner_parms;
5033 tree inst;
5034 int nargs = TREE_VEC_LENGTH (inner_args);
5035 int ntparms;
5036 int i;
5037 bool did_error_intro = false;
5038 struct template_parm_data tpd;
5039 struct template_parm_data tpd2;
5040
5041 gcc_assert (current_template_parms);
5042
5043 /* A concept cannot be specialized. */
5044 if (flag_concepts && variable_concept_p (maintmpl))
5045 {
5046 error ("specialization of variable concept %q#D", maintmpl);
5047 return error_mark_node;
5048 }
5049
5050 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5051 ntparms = TREE_VEC_LENGTH (inner_parms);
5052
5053 /* We check that each of the template parameters given in the
5054 partial specialization is used in the argument list to the
5055 specialization. For example:
5056
5057 template <class T> struct S;
5058 template <class T> struct S<T*>;
5059
5060 The second declaration is OK because `T*' uses the template
5061 parameter T, whereas
5062
5063 template <class T> struct S<int>;
5064
5065 is no good. Even trickier is:
5066
5067 template <class T>
5068 struct S1
5069 {
5070 template <class U>
5071 struct S2;
5072 template <class U>
5073 struct S2<T>;
5074 };
5075
5076 The S2<T> declaration is actually invalid; it is a
5077 full-specialization. Of course,
5078
5079 template <class U>
5080 struct S2<T (*)(U)>;
5081
5082 or some such would have been OK. */
5083 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5084 tpd.parms = XALLOCAVEC (int, ntparms);
5085 memset (tpd.parms, 0, sizeof (int) * ntparms);
5086
5087 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5088 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5089 for (i = 0; i < nargs; ++i)
5090 {
5091 tpd.current_arg = i;
5092 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5093 &mark_template_parm,
5094 &tpd,
5095 NULL,
5096 /*include_nondeduced_p=*/false);
5097 }
5098 for (i = 0; i < ntparms; ++i)
5099 if (tpd.parms[i] == 0)
5100 {
5101 /* One of the template parms was not used in a deduced context in the
5102 specialization. */
5103 if (!did_error_intro)
5104 {
5105 error ("template parameters not deducible in "
5106 "partial specialization:");
5107 did_error_intro = true;
5108 }
5109
5110 inform (input_location, " %qD",
5111 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5112 }
5113
5114 if (did_error_intro)
5115 return error_mark_node;
5116
5117 /* [temp.class.spec]
5118
5119 The argument list of the specialization shall not be identical to
5120 the implicit argument list of the primary template. */
5121 tree main_args
5122 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5123 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5124 && (!flag_concepts
5125 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5126 {
5127 if (!flag_concepts)
5128 error ("partial specialization %q+D does not specialize "
5129 "any template arguments; to define the primary template, "
5130 "remove the template argument list", decl);
5131 else
5132 error ("partial specialization %q+D does not specialize any "
5133 "template arguments and is not more constrained than "
5134 "the primary template; to define the primary template, "
5135 "remove the template argument list", decl);
5136 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5137 }
5138
5139 /* A partial specialization that replaces multiple parameters of the
5140 primary template with a pack expansion is less specialized for those
5141 parameters. */
5142 if (nargs < DECL_NTPARMS (maintmpl))
5143 {
5144 error ("partial specialization is not more specialized than the "
5145 "primary template because it replaces multiple parameters "
5146 "with a pack expansion");
5147 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5148 /* Avoid crash in process_partial_specialization. */
5149 return decl;
5150 }
5151
5152 else if (nargs > DECL_NTPARMS (maintmpl))
5153 {
5154 error ("too many arguments for partial specialization %qT", type);
5155 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5156 /* Avoid crash below. */
5157 return decl;
5158 }
5159
5160 /* If we aren't in a dependent class, we can actually try deduction. */
5161 else if (tpd.level == 1
5162 /* FIXME we should be able to handle a partial specialization of a
5163 partial instantiation, but currently we can't (c++/41727). */
5164 && TMPL_ARGS_DEPTH (specargs) == 1
5165 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5166 {
5167 auto_diagnostic_group d;
5168 if (permerror (input_location, "partial specialization %qD is not "
5169 "more specialized than", decl))
5170 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5171 maintmpl);
5172 }
5173
5174 /* [temp.class.spec]
5175
5176 A partially specialized non-type argument expression shall not
5177 involve template parameters of the partial specialization except
5178 when the argument expression is a simple identifier.
5179
5180 The type of a template parameter corresponding to a specialized
5181 non-type argument shall not be dependent on a parameter of the
5182 specialization.
5183
5184 Also, we verify that pack expansions only occur at the
5185 end of the argument list. */
5186 tpd2.parms = 0;
5187 for (i = 0; i < nargs; ++i)
5188 {
5189 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5190 tree arg = TREE_VEC_ELT (inner_args, i);
5191 tree packed_args = NULL_TREE;
5192 int j, len = 1;
5193
5194 if (ARGUMENT_PACK_P (arg))
5195 {
5196 /* Extract the arguments from the argument pack. We'll be
5197 iterating over these in the following loop. */
5198 packed_args = ARGUMENT_PACK_ARGS (arg);
5199 len = TREE_VEC_LENGTH (packed_args);
5200 }
5201
5202 for (j = 0; j < len; j++)
5203 {
5204 if (packed_args)
5205 /* Get the Jth argument in the parameter pack. */
5206 arg = TREE_VEC_ELT (packed_args, j);
5207
5208 if (PACK_EXPANSION_P (arg))
5209 {
5210 /* Pack expansions must come at the end of the
5211 argument list. */
5212 if ((packed_args && j < len - 1)
5213 || (!packed_args && i < nargs - 1))
5214 {
5215 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5216 error ("parameter pack argument %qE must be at the "
5217 "end of the template argument list", arg);
5218 else
5219 error ("parameter pack argument %qT must be at the "
5220 "end of the template argument list", arg);
5221 }
5222 }
5223
5224 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5225 /* We only care about the pattern. */
5226 arg = PACK_EXPANSION_PATTERN (arg);
5227
5228 if (/* These first two lines are the `non-type' bit. */
5229 !TYPE_P (arg)
5230 && TREE_CODE (arg) != TEMPLATE_DECL
5231 /* This next two lines are the `argument expression is not just a
5232 simple identifier' condition and also the `specialized
5233 non-type argument' bit. */
5234 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5235 && !((REFERENCE_REF_P (arg)
5236 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5237 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5238 {
5239 if ((!packed_args && tpd.arg_uses_template_parms[i])
5240 || (packed_args && uses_template_parms (arg)))
5241 error_at (cp_expr_loc_or_input_loc (arg),
5242 "template argument %qE involves template "
5243 "parameter(s)", arg);
5244 else
5245 {
5246 /* Look at the corresponding template parameter,
5247 marking which template parameters its type depends
5248 upon. */
5249 tree type = TREE_TYPE (parm);
5250
5251 if (!tpd2.parms)
5252 {
5253 /* We haven't yet initialized TPD2. Do so now. */
5254 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5255 /* The number of parameters here is the number in the
5256 main template, which, as checked in the assertion
5257 above, is NARGS. */
5258 tpd2.parms = XALLOCAVEC (int, nargs);
5259 tpd2.level =
5260 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5261 }
5262
5263 /* Mark the template parameters. But this time, we're
5264 looking for the template parameters of the main
5265 template, not in the specialization. */
5266 tpd2.current_arg = i;
5267 tpd2.arg_uses_template_parms[i] = 0;
5268 memset (tpd2.parms, 0, sizeof (int) * nargs);
5269 for_each_template_parm (type,
5270 &mark_template_parm,
5271 &tpd2,
5272 NULL,
5273 /*include_nondeduced_p=*/false);
5274
5275 if (tpd2.arg_uses_template_parms [i])
5276 {
5277 /* The type depended on some template parameters.
5278 If they are fully specialized in the
5279 specialization, that's OK. */
5280 int j;
5281 int count = 0;
5282 for (j = 0; j < nargs; ++j)
5283 if (tpd2.parms[j] != 0
5284 && tpd.arg_uses_template_parms [j])
5285 ++count;
5286 if (count != 0)
5287 error_n (input_location, count,
5288 "type %qT of template argument %qE depends "
5289 "on a template parameter",
5290 "type %qT of template argument %qE depends "
5291 "on template parameters",
5292 type,
5293 arg);
5294 }
5295 }
5296 }
5297 }
5298 }
5299
5300 /* We should only get here once. */
5301 if (TREE_CODE (decl) == TYPE_DECL)
5302 gcc_assert (!COMPLETE_TYPE_P (type));
5303
5304 // Build the template decl.
5305 tree tmpl = build_template_decl (decl, current_template_parms,
5306 DECL_MEMBER_TEMPLATE_P (maintmpl));
5307 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5308 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5309 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5310
5311 /* Give template template parms a DECL_CONTEXT of the template
5312 for which they are a parameter. */
5313 for (i = 0; i < ntparms; ++i)
5314 {
5315 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5316 if (TREE_CODE (parm) == TEMPLATE_DECL)
5317 DECL_CONTEXT (parm) = tmpl;
5318 }
5319
5320 if (VAR_P (decl))
5321 /* We didn't register this in check_explicit_specialization so we could
5322 wait until the constraints were set. */
5323 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5324 else
5325 associate_classtype_constraints (type);
5326
5327 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5328 = tree_cons (specargs, tmpl,
5329 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5330 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5331
5332 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5333 inst = TREE_CHAIN (inst))
5334 {
5335 tree instance = TREE_VALUE (inst);
5336 if (TYPE_P (instance)
5337 ? (COMPLETE_TYPE_P (instance)
5338 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5339 : DECL_TEMPLATE_INSTANTIATION (instance))
5340 {
5341 tree spec = most_specialized_partial_spec (instance, tf_none);
5342 tree inst_decl = (DECL_P (instance)
5343 ? instance : TYPE_NAME (instance));
5344 if (!spec)
5345 /* OK */;
5346 else if (spec == error_mark_node)
5347 permerror (input_location,
5348 "declaration of %qD ambiguates earlier template "
5349 "instantiation for %qD", decl, inst_decl);
5350 else if (TREE_VALUE (spec) == tmpl)
5351 permerror (input_location,
5352 "partial specialization of %qD after instantiation "
5353 "of %qD", decl, inst_decl);
5354 }
5355 }
5356
5357 return decl;
5358 }
5359
5360 /* PARM is a template parameter of some form; return the corresponding
5361 TEMPLATE_PARM_INDEX. */
5362
5363 static tree
5364 get_template_parm_index (tree parm)
5365 {
5366 if (TREE_CODE (parm) == PARM_DECL
5367 || TREE_CODE (parm) == CONST_DECL)
5368 parm = DECL_INITIAL (parm);
5369 else if (TREE_CODE (parm) == TYPE_DECL
5370 || TREE_CODE (parm) == TEMPLATE_DECL)
5371 parm = TREE_TYPE (parm);
5372 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5373 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5374 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5375 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5376 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5377 return parm;
5378 }
5379
5380 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5381 parameter packs used by the template parameter PARM. */
5382
5383 static void
5384 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5385 {
5386 /* A type parm can't refer to another parm. */
5387 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5388 return;
5389 else if (TREE_CODE (parm) == PARM_DECL)
5390 {
5391 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5392 ppd, ppd->visited);
5393 return;
5394 }
5395
5396 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5397
5398 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5399 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5400 {
5401 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5402 if (template_parameter_pack_p (p))
5403 /* Any packs in the type are expanded by this parameter. */;
5404 else
5405 fixed_parameter_pack_p_1 (p, ppd);
5406 }
5407 }
5408
5409 /* PARM is a template parameter pack. Return any parameter packs used in
5410 its type or the type of any of its template parameters. If there are
5411 any such packs, it will be instantiated into a fixed template parameter
5412 list by partial instantiation rather than be fully deduced. */
5413
5414 tree
5415 fixed_parameter_pack_p (tree parm)
5416 {
5417 /* This can only be true in a member template. */
5418 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5419 return NULL_TREE;
5420 /* This can only be true for a parameter pack. */
5421 if (!template_parameter_pack_p (parm))
5422 return NULL_TREE;
5423 /* A type parm can't refer to another parm. */
5424 if (TREE_CODE (parm) == TYPE_DECL)
5425 return NULL_TREE;
5426
5427 tree parameter_packs = NULL_TREE;
5428 struct find_parameter_pack_data ppd;
5429 ppd.parameter_packs = &parameter_packs;
5430 ppd.visited = new hash_set<tree>;
5431 ppd.type_pack_expansion_p = false;
5432
5433 fixed_parameter_pack_p_1 (parm, &ppd);
5434
5435 delete ppd.visited;
5436 return parameter_packs;
5437 }
5438
5439 /* Check that a template declaration's use of default arguments and
5440 parameter packs is not invalid. Here, PARMS are the template
5441 parameters. IS_PRIMARY is true if DECL is the thing declared by
5442 a primary template. IS_PARTIAL is true if DECL is a partial
5443 specialization.
5444
5445 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5446 function template declaration or a friend class template
5447 declaration. In the function case, 1 indicates a declaration, 2
5448 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5449 emitted for extraneous default arguments.
5450
5451 Returns TRUE if there were no errors found, FALSE otherwise. */
5452
5453 bool
5454 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5455 bool is_partial, int is_friend_decl)
5456 {
5457 const char *msg;
5458 int last_level_to_check;
5459 tree parm_level;
5460 bool no_errors = true;
5461
5462 /* [temp.param]
5463
5464 A default template-argument shall not be specified in a
5465 function template declaration or a function template definition, nor
5466 in the template-parameter-list of the definition of a member of a
5467 class template. */
5468
5469 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5470 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5471 /* You can't have a function template declaration in a local
5472 scope, nor you can you define a member of a class template in a
5473 local scope. */
5474 return true;
5475
5476 if ((TREE_CODE (decl) == TYPE_DECL
5477 && TREE_TYPE (decl)
5478 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5479 || (TREE_CODE (decl) == FUNCTION_DECL
5480 && LAMBDA_FUNCTION_P (decl)))
5481 /* A lambda doesn't have an explicit declaration; don't complain
5482 about the parms of the enclosing class. */
5483 return true;
5484
5485 if (current_class_type
5486 && !TYPE_BEING_DEFINED (current_class_type)
5487 && DECL_LANG_SPECIFIC (decl)
5488 && DECL_DECLARES_FUNCTION_P (decl)
5489 /* If this is either a friend defined in the scope of the class
5490 or a member function. */
5491 && (DECL_FUNCTION_MEMBER_P (decl)
5492 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5493 : DECL_FRIEND_CONTEXT (decl)
5494 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5495 : false)
5496 /* And, if it was a member function, it really was defined in
5497 the scope of the class. */
5498 && (!DECL_FUNCTION_MEMBER_P (decl)
5499 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5500 /* We already checked these parameters when the template was
5501 declared, so there's no need to do it again now. This function
5502 was defined in class scope, but we're processing its body now
5503 that the class is complete. */
5504 return true;
5505
5506 /* Core issue 226 (C++0x only): the following only applies to class
5507 templates. */
5508 if (is_primary
5509 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5510 {
5511 /* [temp.param]
5512
5513 If a template-parameter has a default template-argument, all
5514 subsequent template-parameters shall have a default
5515 template-argument supplied. */
5516 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5517 {
5518 tree inner_parms = TREE_VALUE (parm_level);
5519 int ntparms = TREE_VEC_LENGTH (inner_parms);
5520 int seen_def_arg_p = 0;
5521 int i;
5522
5523 for (i = 0; i < ntparms; ++i)
5524 {
5525 tree parm = TREE_VEC_ELT (inner_parms, i);
5526
5527 if (parm == error_mark_node)
5528 continue;
5529
5530 if (TREE_PURPOSE (parm))
5531 seen_def_arg_p = 1;
5532 else if (seen_def_arg_p
5533 && !template_parameter_pack_p (TREE_VALUE (parm)))
5534 {
5535 error ("no default argument for %qD", TREE_VALUE (parm));
5536 /* For better subsequent error-recovery, we indicate that
5537 there should have been a default argument. */
5538 TREE_PURPOSE (parm) = error_mark_node;
5539 no_errors = false;
5540 }
5541 else if (!is_partial
5542 && !is_friend_decl
5543 /* Don't complain about an enclosing partial
5544 specialization. */
5545 && parm_level == parms
5546 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5547 && i < ntparms - 1
5548 && template_parameter_pack_p (TREE_VALUE (parm))
5549 /* A fixed parameter pack will be partially
5550 instantiated into a fixed length list. */
5551 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5552 {
5553 /* A primary class template, primary variable template
5554 (DR 2032), or alias template can only have one
5555 parameter pack, at the end of the template
5556 parameter list. */
5557
5558 error ("parameter pack %q+D must be at the end of the"
5559 " template parameter list", TREE_VALUE (parm));
5560
5561 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5562 = error_mark_node;
5563 no_errors = false;
5564 }
5565 }
5566 }
5567 }
5568
5569 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5570 || is_partial
5571 || !is_primary
5572 || is_friend_decl)
5573 /* For an ordinary class template, default template arguments are
5574 allowed at the innermost level, e.g.:
5575 template <class T = int>
5576 struct S {};
5577 but, in a partial specialization, they're not allowed even
5578 there, as we have in [temp.class.spec]:
5579
5580 The template parameter list of a specialization shall not
5581 contain default template argument values.
5582
5583 So, for a partial specialization, or for a function template
5584 (in C++98/C++03), we look at all of them. */
5585 ;
5586 else
5587 /* But, for a primary class template that is not a partial
5588 specialization we look at all template parameters except the
5589 innermost ones. */
5590 parms = TREE_CHAIN (parms);
5591
5592 /* Figure out what error message to issue. */
5593 if (is_friend_decl == 2)
5594 msg = G_("default template arguments may not be used in function template "
5595 "friend re-declaration");
5596 else if (is_friend_decl)
5597 msg = G_("default template arguments may not be used in template "
5598 "friend declarations");
5599 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5600 msg = G_("default template arguments may not be used in function templates "
5601 "without %<-std=c++11%> or %<-std=gnu++11%>");
5602 else if (is_partial)
5603 msg = G_("default template arguments may not be used in "
5604 "partial specializations");
5605 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5606 msg = G_("default argument for template parameter for class enclosing %qD");
5607 else
5608 /* Per [temp.param]/9, "A default template-argument shall not be
5609 specified in the template-parameter-lists of the definition of
5610 a member of a class template that appears outside of the member's
5611 class.", thus if we aren't handling a member of a class template
5612 there is no need to examine the parameters. */
5613 return true;
5614
5615 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5616 /* If we're inside a class definition, there's no need to
5617 examine the parameters to the class itself. On the one
5618 hand, they will be checked when the class is defined, and,
5619 on the other, default arguments are valid in things like:
5620 template <class T = double>
5621 struct S { template <class U> void f(U); };
5622 Here the default argument for `S' has no bearing on the
5623 declaration of `f'. */
5624 last_level_to_check = template_class_depth (current_class_type) + 1;
5625 else
5626 /* Check everything. */
5627 last_level_to_check = 0;
5628
5629 for (parm_level = parms;
5630 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5631 parm_level = TREE_CHAIN (parm_level))
5632 {
5633 tree inner_parms = TREE_VALUE (parm_level);
5634 int i;
5635 int ntparms;
5636
5637 ntparms = TREE_VEC_LENGTH (inner_parms);
5638 for (i = 0; i < ntparms; ++i)
5639 {
5640 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5641 continue;
5642
5643 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5644 {
5645 if (msg)
5646 {
5647 no_errors = false;
5648 if (is_friend_decl == 2)
5649 return no_errors;
5650
5651 error (msg, decl);
5652 msg = 0;
5653 }
5654
5655 /* Clear out the default argument so that we are not
5656 confused later. */
5657 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5658 }
5659 }
5660
5661 /* At this point, if we're still interested in issuing messages,
5662 they must apply to classes surrounding the object declared. */
5663 if (msg)
5664 msg = G_("default argument for template parameter for class "
5665 "enclosing %qD");
5666 }
5667
5668 return no_errors;
5669 }
5670
5671 /* Worker for push_template_decl_real, called via
5672 for_each_template_parm. DATA is really an int, indicating the
5673 level of the parameters we are interested in. If T is a template
5674 parameter of that level, return nonzero. */
5675
5676 static int
5677 template_parm_this_level_p (tree t, void* data)
5678 {
5679 int this_level = *(int *)data;
5680 int level;
5681
5682 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5683 level = TEMPLATE_PARM_LEVEL (t);
5684 else
5685 level = TEMPLATE_TYPE_LEVEL (t);
5686 return level == this_level;
5687 }
5688
5689 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5690 DATA is really an int, indicating the innermost outer level of parameters.
5691 If T is a template parameter of that level or further out, return
5692 nonzero. */
5693
5694 static int
5695 template_parm_outer_level (tree t, void *data)
5696 {
5697 int this_level = *(int *)data;
5698 int level;
5699
5700 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5701 level = TEMPLATE_PARM_LEVEL (t);
5702 else
5703 level = TEMPLATE_TYPE_LEVEL (t);
5704 return level <= this_level;
5705 }
5706
5707 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5708 parameters given by current_template_args, or reuses a
5709 previously existing one, if appropriate. Returns the DECL, or an
5710 equivalent one, if it is replaced via a call to duplicate_decls.
5711
5712 If IS_FRIEND is true, DECL is a friend declaration. */
5713
5714 tree
5715 push_template_decl (tree decl, bool is_friend)
5716 {
5717 if (decl == error_mark_node || !current_template_parms)
5718 return error_mark_node;
5719
5720 /* See if this is a partial specialization. */
5721 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5722 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5723 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5724 || (VAR_P (decl)
5725 && DECL_LANG_SPECIFIC (decl)
5726 && DECL_TEMPLATE_SPECIALIZATION (decl)
5727 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5728
5729 /* No surprising friend functions. */
5730 gcc_checking_assert (is_friend
5731 || !(TREE_CODE (decl) == FUNCTION_DECL
5732 && DECL_UNIQUE_FRIEND_P (decl)));
5733
5734 tree ctx;
5735 if (is_friend)
5736 /* For a friend, we want the context of the friend, not
5737 the type of which it is a friend. */
5738 ctx = CP_DECL_CONTEXT (decl);
5739 else if (CP_DECL_CONTEXT (decl)
5740 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5741 /* In the case of a virtual function, we want the class in which
5742 it is defined. */
5743 ctx = CP_DECL_CONTEXT (decl);
5744 else
5745 /* Otherwise, if we're currently defining some class, the DECL
5746 is assumed to be a member of the class. */
5747 ctx = current_scope ();
5748
5749 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5750 ctx = NULL_TREE;
5751
5752 if (!DECL_CONTEXT (decl))
5753 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5754
5755 /* See if this is a primary template. */
5756 bool is_primary = false;
5757 if (is_friend && ctx
5758 && uses_template_parms_level (ctx, processing_template_decl))
5759 /* A friend template that specifies a class context, i.e.
5760 template <typename T> friend void A<T>::f();
5761 is not primary. */
5762 ;
5763 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5764 /* Lambdas are not primary. */
5765 ;
5766 else
5767 is_primary = template_parm_scope_p ();
5768
5769 /* True if the template is a member template, in the sense of
5770 [temp.mem]. */
5771 bool member_template_p = false;
5772
5773 if (is_primary)
5774 {
5775 warning (OPT_Wtemplates, "template %qD declared", decl);
5776
5777 if (DECL_CLASS_SCOPE_P (decl))
5778 member_template_p = true;
5779
5780 if (TREE_CODE (decl) == TYPE_DECL
5781 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5782 {
5783 error ("template class without a name");
5784 return error_mark_node;
5785 }
5786 else if (TREE_CODE (decl) == FUNCTION_DECL)
5787 {
5788 if (member_template_p)
5789 {
5790 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5791 error ("member template %qD may not have virt-specifiers", decl);
5792 }
5793 if (DECL_DESTRUCTOR_P (decl))
5794 {
5795 /* [temp.mem]
5796
5797 A destructor shall not be a member template. */
5798 error_at (DECL_SOURCE_LOCATION (decl),
5799 "destructor %qD declared as member template", decl);
5800 return error_mark_node;
5801 }
5802 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5803 && (!prototype_p (TREE_TYPE (decl))
5804 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5805 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5806 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5807 == void_list_node)))
5808 {
5809 /* [basic.stc.dynamic.allocation]
5810
5811 An allocation function can be a function
5812 template. ... Template allocation functions shall
5813 have two or more parameters. */
5814 error ("invalid template declaration of %qD", decl);
5815 return error_mark_node;
5816 }
5817 }
5818 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5819 && CLASS_TYPE_P (TREE_TYPE (decl)))
5820 {
5821 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5822 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5823 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5824 {
5825 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5826 if (TREE_CODE (t) == TYPE_DECL)
5827 t = TREE_TYPE (t);
5828 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5829 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5830 }
5831 }
5832 else if (TREE_CODE (decl) == TYPE_DECL
5833 && TYPE_DECL_ALIAS_P (decl))
5834 /* alias-declaration */
5835 gcc_assert (!DECL_ARTIFICIAL (decl));
5836 else if (VAR_P (decl))
5837 /* C++14 variable template. */;
5838 else if (TREE_CODE (decl) == CONCEPT_DECL)
5839 /* C++20 concept definitions. */;
5840 else
5841 {
5842 error ("template declaration of %q#D", decl);
5843 return error_mark_node;
5844 }
5845 }
5846
5847 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5848 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5849 || (VAR_OR_FUNCTION_DECL_P (decl)
5850 && DECL_LOCAL_DECL_P (decl))));
5851
5852 /* Check to see that the rules regarding the use of default
5853 arguments are not being violated. We check args for a friend
5854 functions when we know whether it's a definition, introducing
5855 declaration or re-declaration. */
5856 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5857 check_default_tmpl_args (decl, current_template_parms,
5858 is_primary, is_partial, is_friend);
5859
5860 /* Ensure that there are no parameter packs in the type of this
5861 declaration that have not been expanded. */
5862 if (TREE_CODE (decl) == FUNCTION_DECL)
5863 {
5864 /* Check each of the arguments individually to see if there are
5865 any bare parameter packs. */
5866 tree type = TREE_TYPE (decl);
5867 tree arg = DECL_ARGUMENTS (decl);
5868 tree argtype = TYPE_ARG_TYPES (type);
5869
5870 while (arg && argtype)
5871 {
5872 if (!DECL_PACK_P (arg)
5873 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5874 {
5875 /* This is a PARM_DECL that contains unexpanded parameter
5876 packs. We have already complained about this in the
5877 check_for_bare_parameter_packs call, so just replace
5878 these types with ERROR_MARK_NODE. */
5879 TREE_TYPE (arg) = error_mark_node;
5880 TREE_VALUE (argtype) = error_mark_node;
5881 }
5882
5883 arg = DECL_CHAIN (arg);
5884 argtype = TREE_CHAIN (argtype);
5885 }
5886
5887 /* Check for bare parameter packs in the return type and the
5888 exception specifiers. */
5889 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5890 /* Errors were already issued, set return type to int
5891 as the frontend doesn't expect error_mark_node as
5892 the return type. */
5893 TREE_TYPE (type) = integer_type_node;
5894 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5895 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5896 }
5897 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5898 ? DECL_ORIGINAL_TYPE (decl)
5899 : TREE_TYPE (decl)))
5900 {
5901 TREE_TYPE (decl) = error_mark_node;
5902 return error_mark_node;
5903 }
5904
5905 if (is_partial)
5906 return process_partial_specialization (decl);
5907
5908 tree args = current_template_args ();
5909 tree tmpl = NULL_TREE;
5910 bool new_template_p = false;
5911 if (local_p)
5912 {
5913 /* Does not get a template head. */
5914 tmpl = NULL_TREE;
5915 gcc_checking_assert (!is_primary);
5916 }
5917 else if (!ctx
5918 || TREE_CODE (ctx) == FUNCTION_DECL
5919 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5920 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5921 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5922 && DECL_TEMPLATE_INFO (decl))))
5923 {
5924 if (DECL_LANG_SPECIFIC (decl)
5925 && DECL_TEMPLATE_INFO (decl)
5926 && DECL_TI_TEMPLATE (decl))
5927 tmpl = DECL_TI_TEMPLATE (decl);
5928 /* If DECL is a TYPE_DECL for a class-template, then there won't
5929 be DECL_LANG_SPECIFIC. The information equivalent to
5930 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5931 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5932 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5933 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5934 {
5935 /* Since a template declaration already existed for this
5936 class-type, we must be redeclaring it here. Make sure
5937 that the redeclaration is valid. */
5938 redeclare_class_template (TREE_TYPE (decl),
5939 current_template_parms,
5940 current_template_constraints ());
5941 /* We don't need to create a new TEMPLATE_DECL; just use the
5942 one we already had. */
5943 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5944 }
5945 else
5946 {
5947 tmpl = build_template_decl (decl, current_template_parms,
5948 member_template_p);
5949 new_template_p = true;
5950
5951 if (DECL_LANG_SPECIFIC (decl)
5952 && DECL_TEMPLATE_SPECIALIZATION (decl))
5953 {
5954 /* A specialization of a member template of a template
5955 class. */
5956 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5957 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5958 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5959 }
5960 }
5961 }
5962 else
5963 {
5964 tree a, t, current, parms;
5965 int i;
5966 tree tinfo = get_template_info (decl);
5967
5968 if (!tinfo)
5969 {
5970 error ("template definition of non-template %q#D", decl);
5971 return error_mark_node;
5972 }
5973
5974 tmpl = TI_TEMPLATE (tinfo);
5975
5976 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5977 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5978 && DECL_TEMPLATE_SPECIALIZATION (decl)
5979 && DECL_MEMBER_TEMPLATE_P (tmpl))
5980 {
5981 /* The declaration is a specialization of a member
5982 template, declared outside the class. Therefore, the
5983 innermost template arguments will be NULL, so we
5984 replace them with the arguments determined by the
5985 earlier call to check_explicit_specialization. */
5986 args = DECL_TI_ARGS (decl);
5987
5988 tree new_tmpl
5989 = build_template_decl (decl, current_template_parms,
5990 member_template_p);
5991 DECL_TI_TEMPLATE (decl) = new_tmpl;
5992 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5993 DECL_TEMPLATE_INFO (new_tmpl)
5994 = build_template_info (tmpl, args);
5995
5996 register_specialization (new_tmpl,
5997 most_general_template (tmpl),
5998 args,
5999 is_friend, 0);
6000 return decl;
6001 }
6002
6003 /* Make sure the template headers we got make sense. */
6004
6005 parms = DECL_TEMPLATE_PARMS (tmpl);
6006 i = TMPL_PARMS_DEPTH (parms);
6007 if (TMPL_ARGS_DEPTH (args) != i)
6008 {
6009 error ("expected %d levels of template parms for %q#D, got %d",
6010 i, decl, TMPL_ARGS_DEPTH (args));
6011 DECL_INTERFACE_KNOWN (decl) = 1;
6012 return error_mark_node;
6013 }
6014 else
6015 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
6016 {
6017 a = TMPL_ARGS_LEVEL (args, i);
6018 t = INNERMOST_TEMPLATE_PARMS (parms);
6019
6020 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6021 {
6022 if (current == decl)
6023 error ("got %d template parameters for %q#D",
6024 TREE_VEC_LENGTH (a), decl);
6025 else
6026 error ("got %d template parameters for %q#T",
6027 TREE_VEC_LENGTH (a), current);
6028 error (" but %d required", TREE_VEC_LENGTH (t));
6029 /* Avoid crash in import_export_decl. */
6030 DECL_INTERFACE_KNOWN (decl) = 1;
6031 return error_mark_node;
6032 }
6033
6034 if (current == decl)
6035 current = ctx;
6036 else if (current == NULL_TREE)
6037 /* Can happen in erroneous input. */
6038 break;
6039 else
6040 current = get_containing_scope (current);
6041 }
6042
6043 /* Check that the parms are used in the appropriate qualifying scopes
6044 in the declarator. */
6045 if (!comp_template_args
6046 (TI_ARGS (tinfo),
6047 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6048 {
6049 error ("template arguments to %qD do not match original "
6050 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6051 if (!uses_template_parms (TI_ARGS (tinfo)))
6052 inform (input_location, "use %<template<>%> for"
6053 " an explicit specialization");
6054 /* Avoid crash in import_export_decl. */
6055 DECL_INTERFACE_KNOWN (decl) = 1;
6056 return error_mark_node;
6057 }
6058 }
6059
6060 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6061
6062 if (new_template_p)
6063 {
6064 /* Push template declarations for global functions and types.
6065 Note that we do not try to push a global template friend
6066 declared in a template class; such a thing may well depend on
6067 the template parameters of the class and we'll push it when
6068 instantiating the befriending class. */
6069 if (!ctx
6070 && !(is_friend && template_class_depth (current_class_type) > 0))
6071 {
6072 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6073 if (pushed == error_mark_node)
6074 return error_mark_node;
6075
6076 /* pushdecl may have found an existing template. */
6077 if (pushed != tmpl)
6078 {
6079 decl = DECL_TEMPLATE_RESULT (pushed);
6080 tmpl = NULL_TREE;
6081 }
6082 }
6083 else if (is_friend)
6084 {
6085 /* Record this decl as belonging to the current class. It's
6086 not chained onto anything else. */
6087 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6088 gcc_checking_assert (!DECL_CHAIN (tmpl));
6089 DECL_CHAIN (tmpl) = current_scope ();
6090 }
6091 }
6092 else if (tmpl)
6093 /* The type may have been completed, or (erroneously) changed. */
6094 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6095
6096 if (tmpl)
6097 {
6098 if (is_primary)
6099 {
6100 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6101
6102 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6103
6104 /* Give template template parms a DECL_CONTEXT of the template
6105 for which they are a parameter. */
6106 parms = INNERMOST_TEMPLATE_PARMS (parms);
6107 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6108 {
6109 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6110 if (TREE_CODE (parm) == TEMPLATE_DECL)
6111 DECL_CONTEXT (parm) = tmpl;
6112 }
6113
6114 if (TREE_CODE (decl) == TYPE_DECL
6115 && TYPE_DECL_ALIAS_P (decl))
6116 {
6117 if (tree constr
6118 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6119 {
6120 /* ??? Why don't we do this here for all templates? */
6121 constr = build_constraints (constr, NULL_TREE);
6122 set_constraints (decl, constr);
6123 }
6124 if (complex_alias_template_p (tmpl))
6125 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6126 }
6127 }
6128
6129 /* The DECL_TI_ARGS of DECL contains full set of arguments
6130 referring wback to its most general template. If TMPL is a
6131 specialization, ARGS may only have the innermost set of
6132 arguments. Add the missing argument levels if necessary. */
6133 if (DECL_TEMPLATE_INFO (tmpl))
6134 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6135
6136 tree info = build_template_info (tmpl, args);
6137
6138 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6139 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6140 else
6141 {
6142 retrofit_lang_decl (decl);
6143 DECL_TEMPLATE_INFO (decl) = info;
6144 }
6145 }
6146
6147 if (flag_implicit_templates
6148 && !is_friend
6149 && TREE_PUBLIC (decl)
6150 && VAR_OR_FUNCTION_DECL_P (decl))
6151 /* Set DECL_COMDAT on template instantiations; if we force
6152 them to be emitted by explicit instantiation,
6153 mark_needed will tell cgraph to do the right thing. */
6154 DECL_COMDAT (decl) = true;
6155
6156 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6157
6158 return decl;
6159 }
6160
6161 /* FN is an inheriting constructor that inherits from the constructor
6162 template INHERITED; turn FN into a constructor template with a matching
6163 template header. */
6164
6165 tree
6166 add_inherited_template_parms (tree fn, tree inherited)
6167 {
6168 tree inner_parms
6169 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6170 inner_parms = copy_node (inner_parms);
6171 tree parms
6172 = tree_cons (size_int (processing_template_decl + 1),
6173 inner_parms, current_template_parms);
6174 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6175 tree args = template_parms_to_args (parms);
6176 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6177 DECL_ARTIFICIAL (tmpl) = true;
6178 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6179 return tmpl;
6180 }
6181
6182 /* Called when a class template TYPE is redeclared with the indicated
6183 template PARMS, e.g.:
6184
6185 template <class T> struct S;
6186 template <class T> struct S {}; */
6187
6188 bool
6189 redeclare_class_template (tree type, tree parms, tree cons)
6190 {
6191 tree tmpl;
6192 tree tmpl_parms;
6193 int i;
6194
6195 if (!TYPE_TEMPLATE_INFO (type))
6196 {
6197 error ("%qT is not a template type", type);
6198 return false;
6199 }
6200
6201 tmpl = TYPE_TI_TEMPLATE (type);
6202 if (!PRIMARY_TEMPLATE_P (tmpl))
6203 /* The type is nested in some template class. Nothing to worry
6204 about here; there are no new template parameters for the nested
6205 type. */
6206 return true;
6207
6208 if (!parms)
6209 {
6210 error ("template specifiers not specified in declaration of %qD",
6211 tmpl);
6212 return false;
6213 }
6214
6215 parms = INNERMOST_TEMPLATE_PARMS (parms);
6216 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6217
6218 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6219 {
6220 error_n (input_location, TREE_VEC_LENGTH (parms),
6221 "redeclared with %d template parameter",
6222 "redeclared with %d template parameters",
6223 TREE_VEC_LENGTH (parms));
6224 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6225 "previous declaration %qD used %d template parameter",
6226 "previous declaration %qD used %d template parameters",
6227 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6228 return false;
6229 }
6230
6231 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6232 {
6233 tree tmpl_parm;
6234 tree parm;
6235 tree tmpl_default;
6236 tree parm_default;
6237
6238 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6239 || TREE_VEC_ELT (parms, i) == error_mark_node)
6240 continue;
6241
6242 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6243 if (error_operand_p (tmpl_parm))
6244 return false;
6245
6246 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6247 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6248 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6249
6250 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6251 TEMPLATE_DECL. */
6252 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6253 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6254 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6255 || (TREE_CODE (tmpl_parm) != PARM_DECL
6256 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6257 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6258 || (TREE_CODE (tmpl_parm) == PARM_DECL
6259 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6260 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6261 {
6262 auto_diagnostic_group d;
6263 error ("template parameter %q+#D", tmpl_parm);
6264 inform (input_location, "redeclared here as %q#D", parm);
6265 return false;
6266 }
6267
6268 /* The parameters can be declared to introduce different
6269 constraints. */
6270 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6271 tree p2 = TREE_VEC_ELT (parms, i);
6272 if (!template_parameter_constraints_equivalent_p (p1, p2))
6273 {
6274 auto_diagnostic_group d;
6275 error ("declaration of template parameter %q+#D with different "
6276 "constraints", parm);
6277 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6278 "original declaration appeared here");
6279 return false;
6280 }
6281
6282 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6283 {
6284 /* We have in [temp.param]:
6285
6286 A template-parameter may not be given default arguments
6287 by two different declarations in the same scope. */
6288 auto_diagnostic_group d;
6289 error_at (input_location, "redefinition of default argument for %q#D", parm);
6290 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6291 "original definition appeared here");
6292 return false;
6293 }
6294
6295 if (parm_default != NULL_TREE)
6296 /* Update the previous template parameters (which are the ones
6297 that will really count) with the new default value. */
6298 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6299 else if (tmpl_default != NULL_TREE)
6300 /* Update the new parameters, too; they'll be used as the
6301 parameters for any members. */
6302 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6303
6304 /* Give each template template parm in this redeclaration a
6305 DECL_CONTEXT of the template for which they are a parameter. */
6306 if (TREE_CODE (parm) == TEMPLATE_DECL)
6307 {
6308 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6309 DECL_CONTEXT (parm) = tmpl;
6310 }
6311
6312 if (TREE_CODE (parm) == TYPE_DECL)
6313 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6314 }
6315
6316 tree ci = get_constraints (tmpl);
6317 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6318 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6319
6320 /* Two classes with different constraints declare different entities. */
6321 if (!cp_tree_equal (req1, req2))
6322 {
6323 auto_diagnostic_group d;
6324 error_at (input_location, "redeclaration %q#D with different "
6325 "constraints", tmpl);
6326 inform (DECL_SOURCE_LOCATION (tmpl),
6327 "original declaration appeared here");
6328 return false;
6329 }
6330
6331 return true;
6332 }
6333
6334 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6335 to be used when the caller has already checked
6336 (processing_template_decl
6337 && !instantiation_dependent_expression_p (expr)
6338 && potential_constant_expression (expr))
6339 and cleared processing_template_decl. */
6340
6341 tree
6342 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6343 {
6344 return tsubst_copy_and_build (expr,
6345 /*args=*/NULL_TREE,
6346 complain,
6347 /*in_decl=*/NULL_TREE,
6348 /*function_p=*/false,
6349 /*integral_constant_expression_p=*/true);
6350 }
6351
6352 /* Simplify EXPR if it is a non-dependent expression. Returns the
6353 (possibly simplified) expression. */
6354
6355 tree
6356 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6357 {
6358 if (expr == NULL_TREE)
6359 return NULL_TREE;
6360
6361 /* If we're in a template, but EXPR isn't value dependent, simplify
6362 it. We're supposed to treat:
6363
6364 template <typename T> void f(T[1 + 1]);
6365 template <typename T> void f(T[2]);
6366
6367 as two declarations of the same function, for example. */
6368 if (processing_template_decl
6369 && is_nondependent_constant_expression (expr))
6370 {
6371 processing_template_decl_sentinel s;
6372 expr = instantiate_non_dependent_expr_internal (expr, complain);
6373 }
6374 return expr;
6375 }
6376
6377 tree
6378 instantiate_non_dependent_expr (tree expr)
6379 {
6380 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6381 }
6382
6383 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6384 an uninstantiated expression. */
6385
6386 tree
6387 instantiate_non_dependent_or_null (tree expr)
6388 {
6389 if (expr == NULL_TREE)
6390 return NULL_TREE;
6391 if (processing_template_decl)
6392 {
6393 if (!is_nondependent_constant_expression (expr))
6394 expr = NULL_TREE;
6395 else
6396 {
6397 processing_template_decl_sentinel s;
6398 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6399 }
6400 }
6401 return expr;
6402 }
6403
6404 /* True iff T is a specialization of a variable template. */
6405
6406 bool
6407 variable_template_specialization_p (tree t)
6408 {
6409 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6410 return false;
6411 tree tmpl = DECL_TI_TEMPLATE (t);
6412 return variable_template_p (tmpl);
6413 }
6414
6415 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6416 template declaration, or a TYPE_DECL for an alias declaration. */
6417
6418 bool
6419 alias_type_or_template_p (tree t)
6420 {
6421 if (t == NULL_TREE)
6422 return false;
6423 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6424 || (TYPE_P (t)
6425 && TYPE_NAME (t)
6426 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6427 || DECL_ALIAS_TEMPLATE_P (t));
6428 }
6429
6430 /* If T is a specialization of an alias template, return it; otherwise return
6431 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6432
6433 tree
6434 alias_template_specialization_p (const_tree t,
6435 bool transparent_typedefs)
6436 {
6437 if (!TYPE_P (t))
6438 return NULL_TREE;
6439
6440 /* It's an alias template specialization if it's an alias and its
6441 TYPE_NAME is a specialization of a primary template. */
6442 if (typedef_variant_p (t))
6443 {
6444 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6445 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6446 return CONST_CAST_TREE (t);
6447 if (transparent_typedefs)
6448 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6449 (TYPE_NAME (t)),
6450 transparent_typedefs);
6451 }
6452
6453 return NULL_TREE;
6454 }
6455
6456 /* An alias template is complex from a SFINAE perspective if a template-id
6457 using that alias can be ill-formed when the expansion is not, as with
6458 the void_t template. We determine this by checking whether the
6459 expansion for the alias template uses all its template parameters. */
6460
6461 struct uses_all_template_parms_data
6462 {
6463 int level;
6464 bool *seen;
6465 };
6466
6467 static int
6468 uses_all_template_parms_r (tree t, void *data_)
6469 {
6470 struct uses_all_template_parms_data &data
6471 = *(struct uses_all_template_parms_data*)data_;
6472 tree idx = get_template_parm_index (t);
6473
6474 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6475 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6476 return 0;
6477 }
6478
6479 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6480
6481 static int
6482 complex_pack_expansion_r (tree t, void *data_)
6483 {
6484 /* An alias template with a pack expansion that expands a pack from the
6485 enclosing class needs to be considered complex, to avoid confusion with
6486 the same pack being used as an argument to the alias's own template
6487 parameter (91966). */
6488 if (!PACK_EXPANSION_P (t))
6489 return 0;
6490 struct uses_all_template_parms_data &data
6491 = *(struct uses_all_template_parms_data*)data_;
6492 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6493 pack = TREE_CHAIN (pack))
6494 {
6495 tree parm_pack = TREE_VALUE (pack);
6496 if (!TEMPLATE_PARM_P (parm_pack))
6497 continue;
6498 int idx, level;
6499 template_parm_level_and_index (parm_pack, &level, &idx);
6500 if (level < data.level)
6501 return 1;
6502 }
6503 return 0;
6504 }
6505
6506 static bool
6507 complex_alias_template_p (const_tree tmpl)
6508 {
6509 /* A renaming alias isn't complex. */
6510 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6511 return false;
6512
6513 /* Any other constrained alias is complex. */
6514 if (get_constraints (tmpl))
6515 return true;
6516
6517 struct uses_all_template_parms_data data;
6518 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6519 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6520 data.level = TMPL_PARMS_DEPTH (parms);
6521 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6522 data.seen = XALLOCAVEC (bool, len);
6523 for (int i = 0; i < len; ++i)
6524 data.seen[i] = false;
6525
6526 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6527 NULL, true, complex_pack_expansion_r))
6528 return true;
6529 for (int i = 0; i < len; ++i)
6530 if (!data.seen[i])
6531 return true;
6532 return false;
6533 }
6534
6535 /* If T is a specialization of a complex alias template with dependent
6536 template-arguments, return it; otherwise return NULL_TREE. If T is a
6537 typedef to such a specialization, return the specialization. */
6538
6539 tree
6540 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6541 {
6542 if (t == error_mark_node)
6543 return NULL_TREE;
6544 gcc_assert (TYPE_P (t));
6545
6546 if (!typedef_variant_p (t))
6547 return NULL_TREE;
6548
6549 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6550 if (tinfo
6551 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6552 && (any_dependent_template_arguments_p
6553 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6554 return CONST_CAST_TREE (t);
6555
6556 if (transparent_typedefs)
6557 {
6558 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6559 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6560 }
6561
6562 return NULL_TREE;
6563 }
6564
6565 /* Return the number of innermost template parameters in TMPL. */
6566
6567 static int
6568 num_innermost_template_parms (const_tree tmpl)
6569 {
6570 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6571 return TREE_VEC_LENGTH (parms);
6572 }
6573
6574 /* Return either TMPL or another template that it is equivalent to under DR
6575 1286: An alias that just changes the name of a template is equivalent to
6576 the other template. */
6577
6578 static tree
6579 get_underlying_template (tree tmpl)
6580 {
6581 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6582 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6583 {
6584 /* Determine if the alias is equivalent to an underlying template. */
6585 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6586 /* The underlying type may have been ill-formed. Don't proceed. */
6587 if (!orig_type)
6588 break;
6589 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6590 if (!tinfo)
6591 break;
6592
6593 tree underlying = TI_TEMPLATE (tinfo);
6594 if (!PRIMARY_TEMPLATE_P (underlying)
6595 || (num_innermost_template_parms (tmpl)
6596 != num_innermost_template_parms (underlying)))
6597 break;
6598
6599 /* Does the alias add cv-quals? */
6600 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6601 break;
6602
6603 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6604 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6605 break;
6606
6607 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6608 it's appropriate to treat a less-constrained alias as equivalent. */
6609 if (!at_least_as_constrained (underlying, tmpl))
6610 break;
6611
6612 /* Alias is equivalent. Strip it and repeat. */
6613 tmpl = underlying;
6614 }
6615
6616 return tmpl;
6617 }
6618
6619 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6620 must be a reference-to-function or a pointer-to-function type, as specified
6621 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6622 and check that the resulting function has external linkage. */
6623
6624 static tree
6625 convert_nontype_argument_function (tree type, tree expr,
6626 tsubst_flags_t complain)
6627 {
6628 tree fns = expr;
6629 tree fn, fn_no_ptr;
6630 linkage_kind linkage;
6631
6632 fn = instantiate_type (type, fns, tf_none);
6633 if (fn == error_mark_node)
6634 return error_mark_node;
6635
6636 if (value_dependent_expression_p (fn))
6637 goto accept;
6638
6639 fn_no_ptr = fn;
6640 if (REFERENCE_REF_P (fn_no_ptr))
6641 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6642 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6643 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6644 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6645 if (BASELINK_P (fn_no_ptr))
6646 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6647
6648 /* [temp.arg.nontype]/1
6649
6650 A template-argument for a non-type, non-template template-parameter
6651 shall be one of:
6652 [...]
6653 -- the address of an object or function with external [C++11: or
6654 internal] linkage. */
6655
6656 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6657 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6658 {
6659 if (complain & tf_error)
6660 {
6661 location_t loc = cp_expr_loc_or_input_loc (expr);
6662 error_at (loc, "%qE is not a valid template argument for type %qT",
6663 expr, type);
6664 if (TYPE_PTR_P (type))
6665 inform (loc, "it must be the address of a function "
6666 "with external linkage");
6667 else
6668 inform (loc, "it must be the name of a function with "
6669 "external linkage");
6670 }
6671 return NULL_TREE;
6672 }
6673
6674 linkage = decl_linkage (fn_no_ptr);
6675 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6676 {
6677 if (complain & tf_error)
6678 {
6679 location_t loc = cp_expr_loc_or_input_loc (expr);
6680 if (cxx_dialect >= cxx11)
6681 error_at (loc, "%qE is not a valid template argument for type "
6682 "%qT because %qD has no linkage",
6683 expr, type, fn_no_ptr);
6684 else
6685 error_at (loc, "%qE is not a valid template argument for type "
6686 "%qT because %qD does not have external linkage",
6687 expr, type, fn_no_ptr);
6688 }
6689 return NULL_TREE;
6690 }
6691
6692 accept:
6693 if (TYPE_REF_P (type))
6694 {
6695 if (REFERENCE_REF_P (fn))
6696 fn = TREE_OPERAND (fn, 0);
6697 else
6698 fn = build_address (fn);
6699 }
6700 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6701 fn = build_nop (type, fn);
6702
6703 return fn;
6704 }
6705
6706 /* Subroutine of convert_nontype_argument.
6707 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6708 Emit an error otherwise. */
6709
6710 static bool
6711 check_valid_ptrmem_cst_expr (tree type, tree expr,
6712 tsubst_flags_t complain)
6713 {
6714 tree orig_expr = expr;
6715 STRIP_NOPS (expr);
6716 if (null_ptr_cst_p (expr))
6717 return true;
6718 if (TREE_CODE (expr) == PTRMEM_CST
6719 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6720 PTRMEM_CST_CLASS (expr)))
6721 return true;
6722 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6723 return true;
6724 if (processing_template_decl
6725 && TREE_CODE (expr) == ADDR_EXPR
6726 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6727 return true;
6728 if (complain & tf_error)
6729 {
6730 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6731 error_at (loc, "%qE is not a valid template argument for type %qT",
6732 orig_expr, type);
6733 if (TREE_CODE (expr) != PTRMEM_CST)
6734 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6735 else
6736 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6737 }
6738 return false;
6739 }
6740
6741 /* Returns TRUE iff the address of OP is value-dependent.
6742
6743 14.6.2.4 [temp.dep.temp]:
6744 A non-integral non-type template-argument is dependent if its type is
6745 dependent or it has either of the following forms
6746 qualified-id
6747 & qualified-id
6748 and contains a nested-name-specifier which specifies a class-name that
6749 names a dependent type.
6750
6751 We generalize this to just say that the address of a member of a
6752 dependent class is value-dependent; the above doesn't cover the
6753 address of a static data member named with an unqualified-id. */
6754
6755 static bool
6756 has_value_dependent_address (tree op)
6757 {
6758 STRIP_ANY_LOCATION_WRAPPER (op);
6759
6760 /* We could use get_inner_reference here, but there's no need;
6761 this is only relevant for template non-type arguments, which
6762 can only be expressed as &id-expression. */
6763 if (DECL_P (op))
6764 {
6765 tree ctx = CP_DECL_CONTEXT (op);
6766 if (TYPE_P (ctx) && dependent_type_p (ctx))
6767 return true;
6768 }
6769
6770 return false;
6771 }
6772
6773 /* The next set of functions are used for providing helpful explanatory
6774 diagnostics for failed overload resolution. Their messages should be
6775 indented by two spaces for consistency with the messages in
6776 call.c */
6777
6778 static int
6779 unify_success (bool /*explain_p*/)
6780 {
6781 return 0;
6782 }
6783
6784 /* Other failure functions should call this one, to provide a single function
6785 for setting a breakpoint on. */
6786
6787 static int
6788 unify_invalid (bool /*explain_p*/)
6789 {
6790 return 1;
6791 }
6792
6793 static int
6794 unify_parameter_deduction_failure (bool explain_p, tree parm)
6795 {
6796 if (explain_p)
6797 inform (input_location,
6798 " couldn%'t deduce template parameter %qD", parm);
6799 return unify_invalid (explain_p);
6800 }
6801
6802 static int
6803 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6804 {
6805 if (explain_p)
6806 inform (input_location,
6807 " types %qT and %qT have incompatible cv-qualifiers",
6808 parm, arg);
6809 return unify_invalid (explain_p);
6810 }
6811
6812 static int
6813 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6814 {
6815 if (explain_p)
6816 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6817 return unify_invalid (explain_p);
6818 }
6819
6820 static int
6821 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6822 {
6823 if (explain_p)
6824 inform (input_location,
6825 " template parameter %qD is not a parameter pack, but "
6826 "argument %qD is",
6827 parm, arg);
6828 return unify_invalid (explain_p);
6829 }
6830
6831 static int
6832 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6833 {
6834 if (explain_p)
6835 inform (input_location,
6836 " template argument %qE does not match "
6837 "pointer-to-member constant %qE",
6838 arg, parm);
6839 return unify_invalid (explain_p);
6840 }
6841
6842 static int
6843 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6844 {
6845 if (explain_p)
6846 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6847 return unify_invalid (explain_p);
6848 }
6849
6850 static int
6851 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6852 {
6853 if (explain_p)
6854 inform (input_location,
6855 " inconsistent parameter pack deduction with %qT and %qT",
6856 old_arg, new_arg);
6857 return unify_invalid (explain_p);
6858 }
6859
6860 static int
6861 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6862 {
6863 if (explain_p)
6864 {
6865 if (TYPE_P (parm))
6866 inform (input_location,
6867 " deduced conflicting types for parameter %qT (%qT and %qT)",
6868 parm, first, second);
6869 else
6870 inform (input_location,
6871 " deduced conflicting values for non-type parameter "
6872 "%qE (%qE and %qE)", parm, first, second);
6873 }
6874 return unify_invalid (explain_p);
6875 }
6876
6877 static int
6878 unify_vla_arg (bool explain_p, tree arg)
6879 {
6880 if (explain_p)
6881 inform (input_location,
6882 " variable-sized array type %qT is not "
6883 "a valid template argument",
6884 arg);
6885 return unify_invalid (explain_p);
6886 }
6887
6888 static int
6889 unify_method_type_error (bool explain_p, tree arg)
6890 {
6891 if (explain_p)
6892 inform (input_location,
6893 " member function type %qT is not a valid template argument",
6894 arg);
6895 return unify_invalid (explain_p);
6896 }
6897
6898 static int
6899 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6900 {
6901 if (explain_p)
6902 {
6903 if (least_p)
6904 inform_n (input_location, wanted,
6905 " candidate expects at least %d argument, %d provided",
6906 " candidate expects at least %d arguments, %d provided",
6907 wanted, have);
6908 else
6909 inform_n (input_location, wanted,
6910 " candidate expects %d argument, %d provided",
6911 " candidate expects %d arguments, %d provided",
6912 wanted, have);
6913 }
6914 return unify_invalid (explain_p);
6915 }
6916
6917 static int
6918 unify_too_many_arguments (bool explain_p, int have, int wanted)
6919 {
6920 return unify_arity (explain_p, have, wanted);
6921 }
6922
6923 static int
6924 unify_too_few_arguments (bool explain_p, int have, int wanted,
6925 bool least_p = false)
6926 {
6927 return unify_arity (explain_p, have, wanted, least_p);
6928 }
6929
6930 static int
6931 unify_arg_conversion (bool explain_p, tree to_type,
6932 tree from_type, tree arg)
6933 {
6934 if (explain_p)
6935 inform (cp_expr_loc_or_input_loc (arg),
6936 " cannot convert %qE (type %qT) to type %qT",
6937 arg, from_type, to_type);
6938 return unify_invalid (explain_p);
6939 }
6940
6941 static int
6942 unify_no_common_base (bool explain_p, enum template_base_result r,
6943 tree parm, tree arg)
6944 {
6945 if (explain_p)
6946 switch (r)
6947 {
6948 case tbr_ambiguous_baseclass:
6949 inform (input_location, " %qT is an ambiguous base class of %qT",
6950 parm, arg);
6951 break;
6952 default:
6953 inform (input_location, " %qT is not derived from %qT", arg, parm);
6954 break;
6955 }
6956 return unify_invalid (explain_p);
6957 }
6958
6959 static int
6960 unify_inconsistent_template_template_parameters (bool explain_p)
6961 {
6962 if (explain_p)
6963 inform (input_location,
6964 " template parameters of a template template argument are "
6965 "inconsistent with other deduced template arguments");
6966 return unify_invalid (explain_p);
6967 }
6968
6969 static int
6970 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6971 {
6972 if (explain_p)
6973 inform (input_location,
6974 " cannot deduce a template for %qT from non-template type %qT",
6975 parm, arg);
6976 return unify_invalid (explain_p);
6977 }
6978
6979 static int
6980 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6981 {
6982 if (explain_p)
6983 inform (input_location,
6984 " template argument %qE does not match %qE", arg, parm);
6985 return unify_invalid (explain_p);
6986 }
6987
6988 /* True if T is a C++20 template parameter object to store the argument for a
6989 template parameter of class type. */
6990
6991 bool
6992 template_parm_object_p (const_tree t)
6993 {
6994 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6995 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6996 }
6997
6998 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6999 argument for TYPE, points to an unsuitable object.
7000
7001 Also adjust the type of the index in C++20 array subobject references. */
7002
7003 static bool
7004 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
7005 {
7006 switch (TREE_CODE (expr))
7007 {
7008 CASE_CONVERT:
7009 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
7010 complain);
7011
7012 case TARGET_EXPR:
7013 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
7014 complain);
7015
7016 case CONSTRUCTOR:
7017 {
7018 unsigned i; tree elt;
7019 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
7020 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
7021 return true;
7022 }
7023 break;
7024
7025 case ADDR_EXPR:
7026 {
7027 tree decl = TREE_OPERAND (expr, 0);
7028
7029 if (cxx_dialect >= cxx20)
7030 while (TREE_CODE (decl) == COMPONENT_REF
7031 || TREE_CODE (decl) == ARRAY_REF)
7032 {
7033 tree &op = TREE_OPERAND (decl, 1);
7034 if (TREE_CODE (decl) == ARRAY_REF
7035 && TREE_CODE (op) == INTEGER_CST)
7036 /* Canonicalize array offsets to ptrdiff_t; how they were
7037 written doesn't matter for subobject identity. */
7038 op = fold_convert (ptrdiff_type_node, op);
7039 decl = TREE_OPERAND (decl, 0);
7040 }
7041
7042 if (!VAR_P (decl))
7043 {
7044 if (complain & tf_error)
7045 error_at (cp_expr_loc_or_input_loc (expr),
7046 "%qE is not a valid template argument of type %qT "
7047 "because %qE is not a variable", expr, type, decl);
7048 return true;
7049 }
7050 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7051 {
7052 if (complain & tf_error)
7053 error_at (cp_expr_loc_or_input_loc (expr),
7054 "%qE is not a valid template argument of type %qT "
7055 "in C++98 because %qD does not have external linkage",
7056 expr, type, decl);
7057 return true;
7058 }
7059 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7060 && decl_linkage (decl) == lk_none)
7061 {
7062 if (complain & tf_error)
7063 error_at (cp_expr_loc_or_input_loc (expr),
7064 "%qE is not a valid template argument of type %qT "
7065 "because %qD has no linkage", expr, type, decl);
7066 return true;
7067 }
7068 /* C++17: For a non-type template-parameter of reference or pointer
7069 type, the value of the constant expression shall not refer to (or
7070 for a pointer type, shall not be the address of):
7071 * a subobject (4.5),
7072 * a temporary object (15.2),
7073 * a string literal (5.13.5),
7074 * the result of a typeid expression (8.2.8), or
7075 * a predefined __func__ variable (11.4.1). */
7076 else if (DECL_ARTIFICIAL (decl))
7077 {
7078 if (complain & tf_error)
7079 error ("the address of %qD is not a valid template argument",
7080 decl);
7081 return true;
7082 }
7083 else if (cxx_dialect < cxx20
7084 && !(same_type_ignoring_top_level_qualifiers_p
7085 (strip_array_types (TREE_TYPE (type)),
7086 strip_array_types (TREE_TYPE (decl)))))
7087 {
7088 if (complain & tf_error)
7089 error ("the address of the %qT subobject of %qD is not a "
7090 "valid template argument", TREE_TYPE (type), decl);
7091 return true;
7092 }
7093 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7094 {
7095 if (complain & tf_error)
7096 error ("the address of %qD is not a valid template argument "
7097 "because it does not have static storage duration",
7098 decl);
7099 return true;
7100 }
7101 }
7102 break;
7103
7104 default:
7105 if (!INDIRECT_TYPE_P (type))
7106 /* We're only concerned about pointers and references here. */;
7107 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7108 /* Null pointer values are OK in C++11. */;
7109 else
7110 {
7111 if (VAR_P (expr))
7112 {
7113 if (complain & tf_error)
7114 error ("%qD is not a valid template argument "
7115 "because %qD is a variable, not the address of "
7116 "a variable", expr, expr);
7117 return true;
7118 }
7119 else
7120 {
7121 if (complain & tf_error)
7122 error ("%qE is not a valid template argument for %qT "
7123 "because it is not the address of a variable",
7124 expr, type);
7125 return true;
7126 }
7127 }
7128 }
7129 return false;
7130
7131 }
7132
7133 /* The template arguments corresponding to template parameter objects of types
7134 that contain pointers to members. */
7135
7136 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7137
7138 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7139 template argument EXPR. */
7140
7141 static tree
7142 get_template_parm_object (tree expr, tsubst_flags_t complain)
7143 {
7144 if (TREE_CODE (expr) == TARGET_EXPR)
7145 expr = TARGET_EXPR_INITIAL (expr);
7146
7147 if (!TREE_CONSTANT (expr))
7148 {
7149 if ((complain & tf_error)
7150 && require_rvalue_constant_expression (expr))
7151 cxx_constant_value (expr);
7152 return error_mark_node;
7153 }
7154 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7155 return error_mark_node;
7156
7157 /* This is no longer a compound literal. */
7158 TREE_HAS_CONSTRUCTOR (expr) = 0;
7159
7160 tree name = mangle_template_parm_object (expr);
7161 tree decl = get_global_binding (name);
7162 if (decl)
7163 return decl;
7164
7165 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7166 decl = create_temporary_var (type);
7167 DECL_CONTEXT (decl) = NULL_TREE;
7168 TREE_STATIC (decl) = true;
7169 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7170 TREE_READONLY (decl) = true;
7171 DECL_NAME (decl) = name;
7172 SET_DECL_ASSEMBLER_NAME (decl, name);
7173 comdat_linkage (decl);
7174
7175 if (!zero_init_p (type))
7176 {
7177 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7178 lower_var_init before we're done mangling. So store the original
7179 value elsewhere. */
7180 tree copy = unshare_constructor (expr);
7181 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7182 }
7183
7184 pushdecl_top_level_and_finish (decl, expr);
7185
7186 return decl;
7187 }
7188
7189 /* Return the actual template argument corresponding to template parameter
7190 object VAR. */
7191
7192 tree
7193 tparm_object_argument (tree var)
7194 {
7195 if (zero_init_p (TREE_TYPE (var)))
7196 return DECL_INITIAL (var);
7197 return *(tparm_obj_values->get (var));
7198 }
7199
7200 /* Attempt to convert the non-type template parameter EXPR to the
7201 indicated TYPE. If the conversion is successful, return the
7202 converted value. If the conversion is unsuccessful, return
7203 NULL_TREE if we issued an error message, or error_mark_node if we
7204 did not. We issue error messages for out-and-out bad template
7205 parameters, but not simply because the conversion failed, since we
7206 might be just trying to do argument deduction. Both TYPE and EXPR
7207 must be non-dependent.
7208
7209 The conversion follows the special rules described in
7210 [temp.arg.nontype], and it is much more strict than an implicit
7211 conversion.
7212
7213 This function is called twice for each template argument (see
7214 lookup_template_class for a more accurate description of this
7215 problem). This means that we need to handle expressions which
7216 are not valid in a C++ source, but can be created from the
7217 first call (for instance, casts to perform conversions). These
7218 hacks can go away after we fix the double coercion problem. */
7219
7220 static tree
7221 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7222 {
7223 tree expr_type;
7224 location_t loc = cp_expr_loc_or_input_loc (expr);
7225
7226 /* Detect immediately string literals as invalid non-type argument.
7227 This special-case is not needed for correctness (we would easily
7228 catch this later), but only to provide better diagnostic for this
7229 common user mistake. As suggested by DR 100, we do not mention
7230 linkage issues in the diagnostic as this is not the point. */
7231 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7232 {
7233 if (complain & tf_error)
7234 error ("%qE is not a valid template argument for type %qT "
7235 "because string literals can never be used in this context",
7236 expr, type);
7237 return NULL_TREE;
7238 }
7239
7240 /* Add the ADDR_EXPR now for the benefit of
7241 value_dependent_expression_p. */
7242 if (TYPE_PTROBV_P (type)
7243 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7244 {
7245 expr = decay_conversion (expr, complain);
7246 if (expr == error_mark_node)
7247 return error_mark_node;
7248 }
7249
7250 /* If we are in a template, EXPR may be non-dependent, but still
7251 have a syntactic, rather than semantic, form. For example, EXPR
7252 might be a SCOPE_REF, rather than the VAR_DECL to which the
7253 SCOPE_REF refers. Preserving the qualifying scope is necessary
7254 so that access checking can be performed when the template is
7255 instantiated -- but here we need the resolved form so that we can
7256 convert the argument. */
7257 bool non_dep = false;
7258 if (TYPE_REF_OBJ_P (type)
7259 && has_value_dependent_address (expr))
7260 /* If we want the address and it's value-dependent, don't fold. */;
7261 else if (processing_template_decl
7262 && is_nondependent_constant_expression (expr))
7263 non_dep = true;
7264 if (error_operand_p (expr))
7265 return error_mark_node;
7266 expr_type = TREE_TYPE (expr);
7267
7268 /* If the argument is non-dependent, perform any conversions in
7269 non-dependent context as well. */
7270 processing_template_decl_sentinel s (non_dep);
7271 if (non_dep)
7272 expr = instantiate_non_dependent_expr_internal (expr, complain);
7273
7274 bool val_dep_p = value_dependent_expression_p (expr);
7275 if (val_dep_p)
7276 expr = canonicalize_expr_argument (expr, complain);
7277
7278 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7279 to a non-type argument of "nullptr". */
7280 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7281 expr = fold_simple (convert (type, expr));
7282
7283 /* In C++11, integral or enumeration non-type template arguments can be
7284 arbitrary constant expressions. Pointer and pointer to
7285 member arguments can be general constant expressions that evaluate
7286 to a null value, but otherwise still need to be of a specific form. */
7287 if (cxx_dialect >= cxx11)
7288 {
7289 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7290 /* A PTRMEM_CST is already constant, and a valid template
7291 argument for a parameter of pointer to member type, we just want
7292 to leave it in that form rather than lower it to a
7293 CONSTRUCTOR. */;
7294 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7295 || cxx_dialect >= cxx17)
7296 {
7297 /* C++17: A template-argument for a non-type template-parameter shall
7298 be a converted constant expression (8.20) of the type of the
7299 template-parameter. */
7300 expr = build_converted_constant_expr (type, expr, complain);
7301 if (expr == error_mark_node)
7302 /* Make sure we return NULL_TREE only if we have really issued
7303 an error, as described above. */
7304 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7305 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7306 {
7307 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7308 return expr;
7309 }
7310 expr = maybe_constant_value (expr, NULL_TREE,
7311 /*manifestly_const_eval=*/true);
7312 expr = convert_from_reference (expr);
7313 /* EXPR may have become value-dependent. */
7314 val_dep_p = value_dependent_expression_p (expr);
7315 }
7316 else if (TYPE_PTR_OR_PTRMEM_P (type))
7317 {
7318 tree folded = maybe_constant_value (expr, NULL_TREE,
7319 /*manifestly_const_eval=*/true);
7320 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7321 : null_member_pointer_value_p (folded))
7322 expr = folded;
7323 }
7324 }
7325
7326 if (TYPE_REF_P (type))
7327 expr = mark_lvalue_use (expr);
7328 else
7329 expr = mark_rvalue_use (expr);
7330
7331 /* HACK: Due to double coercion, we can get a
7332 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7333 which is the tree that we built on the first call (see
7334 below when coercing to reference to object or to reference to
7335 function). We just strip everything and get to the arg.
7336 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7337 for examples. */
7338 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7339 {
7340 tree probe_type, probe = expr;
7341 if (REFERENCE_REF_P (probe))
7342 probe = TREE_OPERAND (probe, 0);
7343 probe_type = TREE_TYPE (probe);
7344 if (TREE_CODE (probe) == NOP_EXPR)
7345 {
7346 /* ??? Maybe we could use convert_from_reference here, but we
7347 would need to relax its constraints because the NOP_EXPR
7348 could actually change the type to something more cv-qualified,
7349 and this is not folded by convert_from_reference. */
7350 tree addr = TREE_OPERAND (probe, 0);
7351 if (TYPE_REF_P (probe_type)
7352 && TREE_CODE (addr) == ADDR_EXPR
7353 && TYPE_PTR_P (TREE_TYPE (addr))
7354 && (same_type_ignoring_top_level_qualifiers_p
7355 (TREE_TYPE (probe_type),
7356 TREE_TYPE (TREE_TYPE (addr)))))
7357 {
7358 expr = TREE_OPERAND (addr, 0);
7359 expr_type = TREE_TYPE (probe_type);
7360 }
7361 }
7362 }
7363
7364 /* [temp.arg.nontype]/5, bullet 1
7365
7366 For a non-type template-parameter of integral or enumeration type,
7367 integral promotions (_conv.prom_) and integral conversions
7368 (_conv.integral_) are applied. */
7369 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7370 || TREE_CODE (type) == REAL_TYPE)
7371 {
7372 if (cxx_dialect < cxx11)
7373 {
7374 tree t = build_converted_constant_expr (type, expr, complain);
7375 t = maybe_constant_value (t);
7376 if (t != error_mark_node)
7377 expr = t;
7378 }
7379
7380 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7381 return error_mark_node;
7382
7383 /* Notice that there are constant expressions like '4 % 0' which
7384 do not fold into integer constants. */
7385 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7386 {
7387 if (complain & tf_error)
7388 {
7389 int errs = errorcount, warns = warningcount + werrorcount;
7390 if (!require_potential_constant_expression (expr))
7391 expr = error_mark_node;
7392 else
7393 expr = cxx_constant_value (expr);
7394 if (errorcount > errs || warningcount + werrorcount > warns)
7395 inform (loc, "in template argument for type %qT", type);
7396 if (expr == error_mark_node)
7397 return NULL_TREE;
7398 /* else cxx_constant_value complained but gave us
7399 a real constant, so go ahead. */
7400 if (!CONSTANT_CLASS_P (expr))
7401 {
7402 /* Some assemble time constant expressions like
7403 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7404 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7405 as we can emit them into .rodata initializers of
7406 variables, yet they can't fold into an INTEGER_CST at
7407 compile time. Refuse them here. */
7408 gcc_checking_assert (reduced_constant_expression_p (expr));
7409 error_at (loc, "template argument %qE for type %qT not "
7410 "a compile-time constant", expr, type);
7411 return NULL_TREE;
7412 }
7413 }
7414 else
7415 return NULL_TREE;
7416 }
7417
7418 /* Avoid typedef problems. */
7419 if (TREE_TYPE (expr) != type)
7420 expr = fold_convert (type, expr);
7421 }
7422 /* [temp.arg.nontype]/5, bullet 2
7423
7424 For a non-type template-parameter of type pointer to object,
7425 qualification conversions (_conv.qual_) and the array-to-pointer
7426 conversion (_conv.array_) are applied. */
7427 else if (TYPE_PTROBV_P (type))
7428 {
7429 tree decayed = expr;
7430
7431 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7432 decay_conversion or an explicit cast. If it's a problematic cast,
7433 we'll complain about it below. */
7434 if (TREE_CODE (expr) == NOP_EXPR)
7435 {
7436 tree probe = expr;
7437 STRIP_NOPS (probe);
7438 if (TREE_CODE (probe) == ADDR_EXPR
7439 && TYPE_PTR_P (TREE_TYPE (probe)))
7440 {
7441 expr = probe;
7442 expr_type = TREE_TYPE (expr);
7443 }
7444 }
7445
7446 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7447
7448 A template-argument for a non-type, non-template template-parameter
7449 shall be one of: [...]
7450
7451 -- the name of a non-type template-parameter;
7452 -- the address of an object or function with external linkage, [...]
7453 expressed as "& id-expression" where the & is optional if the name
7454 refers to a function or array, or if the corresponding
7455 template-parameter is a reference.
7456
7457 Here, we do not care about functions, as they are invalid anyway
7458 for a parameter of type pointer-to-object. */
7459
7460 if (val_dep_p)
7461 /* Non-type template parameters are OK. */
7462 ;
7463 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7464 /* Null pointer values are OK in C++11. */;
7465 else if (TREE_CODE (expr) != ADDR_EXPR
7466 && !INDIRECT_TYPE_P (expr_type))
7467 /* Other values, like integer constants, might be valid
7468 non-type arguments of some other type. */
7469 return error_mark_node;
7470 else if (invalid_tparm_referent_p (type, expr, complain))
7471 return NULL_TREE;
7472
7473 expr = decayed;
7474
7475 expr = perform_qualification_conversions (type, expr);
7476 if (expr == error_mark_node)
7477 return error_mark_node;
7478 }
7479 /* [temp.arg.nontype]/5, bullet 3
7480
7481 For a non-type template-parameter of type reference to object, no
7482 conversions apply. The type referred to by the reference may be more
7483 cv-qualified than the (otherwise identical) type of the
7484 template-argument. The template-parameter is bound directly to the
7485 template-argument, which must be an lvalue. */
7486 else if (TYPE_REF_OBJ_P (type))
7487 {
7488 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7489 expr_type))
7490 return error_mark_node;
7491
7492 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7493 {
7494 if (complain & tf_error)
7495 error ("%qE is not a valid template argument for type %qT "
7496 "because of conflicts in cv-qualification", expr, type);
7497 return NULL_TREE;
7498 }
7499
7500 if (!lvalue_p (expr))
7501 {
7502 if (complain & tf_error)
7503 error ("%qE is not a valid template argument for type %qT "
7504 "because it is not an lvalue", expr, type);
7505 return NULL_TREE;
7506 }
7507
7508 /* [temp.arg.nontype]/1
7509
7510 A template-argument for a non-type, non-template template-parameter
7511 shall be one of: [...]
7512
7513 -- the address of an object or function with external linkage. */
7514 if (INDIRECT_REF_P (expr)
7515 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7516 {
7517 expr = TREE_OPERAND (expr, 0);
7518 if (DECL_P (expr))
7519 {
7520 if (complain & tf_error)
7521 error ("%q#D is not a valid template argument for type %qT "
7522 "because a reference variable does not have a constant "
7523 "address", expr, type);
7524 return NULL_TREE;
7525 }
7526 }
7527
7528 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7529 /* OK, dependent reference. We don't want to ask whether a DECL is
7530 itself value-dependent, since what we want here is its address. */;
7531 else
7532 {
7533 expr = build_address (expr);
7534
7535 if (invalid_tparm_referent_p (type, expr, complain))
7536 return NULL_TREE;
7537 }
7538
7539 if (!same_type_p (type, TREE_TYPE (expr)))
7540 expr = build_nop (type, expr);
7541 }
7542 /* [temp.arg.nontype]/5, bullet 4
7543
7544 For a non-type template-parameter of type pointer to function, only
7545 the function-to-pointer conversion (_conv.func_) is applied. If the
7546 template-argument represents a set of overloaded functions (or a
7547 pointer to such), the matching function is selected from the set
7548 (_over.over_). */
7549 else if (TYPE_PTRFN_P (type))
7550 {
7551 /* If the argument is a template-id, we might not have enough
7552 context information to decay the pointer. */
7553 if (!type_unknown_p (expr_type))
7554 {
7555 expr = decay_conversion (expr, complain);
7556 if (expr == error_mark_node)
7557 return error_mark_node;
7558 }
7559
7560 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7561 /* Null pointer values are OK in C++11. */
7562 return perform_qualification_conversions (type, expr);
7563
7564 expr = convert_nontype_argument_function (type, expr, complain);
7565 if (!expr || expr == error_mark_node)
7566 return expr;
7567 }
7568 /* [temp.arg.nontype]/5, bullet 5
7569
7570 For a non-type template-parameter of type reference to function, no
7571 conversions apply. If the template-argument represents a set of
7572 overloaded functions, the matching function is selected from the set
7573 (_over.over_). */
7574 else if (TYPE_REFFN_P (type))
7575 {
7576 if (TREE_CODE (expr) == ADDR_EXPR)
7577 {
7578 if (complain & tf_error)
7579 {
7580 error ("%qE is not a valid template argument for type %qT "
7581 "because it is a pointer", expr, type);
7582 inform (input_location, "try using %qE instead",
7583 TREE_OPERAND (expr, 0));
7584 }
7585 return NULL_TREE;
7586 }
7587
7588 expr = convert_nontype_argument_function (type, expr, complain);
7589 if (!expr || expr == error_mark_node)
7590 return expr;
7591 }
7592 /* [temp.arg.nontype]/5, bullet 6
7593
7594 For a non-type template-parameter of type pointer to member function,
7595 no conversions apply. If the template-argument represents a set of
7596 overloaded member functions, the matching member function is selected
7597 from the set (_over.over_). */
7598 else if (TYPE_PTRMEMFUNC_P (type))
7599 {
7600 expr = instantiate_type (type, expr, tf_none);
7601 if (expr == error_mark_node)
7602 return error_mark_node;
7603
7604 /* [temp.arg.nontype] bullet 1 says the pointer to member
7605 expression must be a pointer-to-member constant. */
7606 if (!val_dep_p
7607 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7608 return NULL_TREE;
7609
7610 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7611 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7612 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7613 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7614 }
7615 /* [temp.arg.nontype]/5, bullet 7
7616
7617 For a non-type template-parameter of type pointer to data member,
7618 qualification conversions (_conv.qual_) are applied. */
7619 else if (TYPE_PTRDATAMEM_P (type))
7620 {
7621 /* [temp.arg.nontype] bullet 1 says the pointer to member
7622 expression must be a pointer-to-member constant. */
7623 if (!val_dep_p
7624 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7625 return NULL_TREE;
7626
7627 expr = perform_qualification_conversions (type, expr);
7628 if (expr == error_mark_node)
7629 return expr;
7630 }
7631 else if (NULLPTR_TYPE_P (type))
7632 {
7633 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7634 {
7635 if (complain & tf_error)
7636 error ("%qE is not a valid template argument for type %qT "
7637 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7638 return NULL_TREE;
7639 }
7640 return expr;
7641 }
7642 else if (CLASS_TYPE_P (type))
7643 {
7644 /* Replace the argument with a reference to the corresponding template
7645 parameter object. */
7646 if (!val_dep_p)
7647 expr = get_template_parm_object (expr, complain);
7648 if (expr == error_mark_node)
7649 return NULL_TREE;
7650 }
7651 /* A template non-type parameter must be one of the above. */
7652 else
7653 gcc_unreachable ();
7654
7655 /* Sanity check: did we actually convert the argument to the
7656 right type? */
7657 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7658 (type, TREE_TYPE (expr)));
7659 return convert_from_reference (expr);
7660 }
7661
7662 /* Subroutine of coerce_template_template_parms, which returns 1 if
7663 PARM_PARM and ARG_PARM match using the rule for the template
7664 parameters of template template parameters. Both PARM and ARG are
7665 template parameters; the rest of the arguments are the same as for
7666 coerce_template_template_parms.
7667 */
7668 static int
7669 coerce_template_template_parm (tree parm,
7670 tree arg,
7671 tsubst_flags_t complain,
7672 tree in_decl,
7673 tree outer_args)
7674 {
7675 if (arg == NULL_TREE || error_operand_p (arg)
7676 || parm == NULL_TREE || error_operand_p (parm))
7677 return 0;
7678
7679 if (TREE_CODE (arg) != TREE_CODE (parm))
7680 return 0;
7681
7682 switch (TREE_CODE (parm))
7683 {
7684 case TEMPLATE_DECL:
7685 /* We encounter instantiations of templates like
7686 template <template <template <class> class> class TT>
7687 class C; */
7688 {
7689 tree parmparm = DECL_TEMPLATE_PARMS (parm);
7690 tree argparm = DECL_TEMPLATE_PARMS (arg);
7691
7692 if (!coerce_template_template_parms
7693 (parmparm, argparm, complain, in_decl, outer_args))
7694 return 0;
7695 }
7696 /* Fall through. */
7697
7698 case TYPE_DECL:
7699 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7700 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7701 /* Argument is a parameter pack but parameter is not. */
7702 return 0;
7703 break;
7704
7705 case PARM_DECL:
7706 /* The tsubst call is used to handle cases such as
7707
7708 template <int> class C {};
7709 template <class T, template <T> class TT> class D {};
7710 D<int, C> d;
7711
7712 i.e. the parameter list of TT depends on earlier parameters. */
7713 if (!uses_template_parms (TREE_TYPE (arg)))
7714 {
7715 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7716 if (!uses_template_parms (t)
7717 && !same_type_p (t, TREE_TYPE (arg)))
7718 return 0;
7719 }
7720
7721 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7722 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7723 /* Argument is a parameter pack but parameter is not. */
7724 return 0;
7725
7726 break;
7727
7728 default:
7729 gcc_unreachable ();
7730 }
7731
7732 return 1;
7733 }
7734
7735 /* Coerce template argument list ARGLIST for use with template
7736 template-parameter TEMPL. */
7737
7738 static tree
7739 coerce_template_args_for_ttp (tree templ, tree arglist,
7740 tsubst_flags_t complain)
7741 {
7742 /* Consider an example where a template template parameter declared as
7743
7744 template <class T, class U = std::allocator<T> > class TT
7745
7746 The template parameter level of T and U are one level larger than
7747 of TT. To proper process the default argument of U, say when an
7748 instantiation `TT<int>' is seen, we need to build the full
7749 arguments containing {int} as the innermost level. Outer levels,
7750 available when not appearing as default template argument, can be
7751 obtained from the arguments of the enclosing template.
7752
7753 Suppose that TT is later substituted with std::vector. The above
7754 instantiation is `TT<int, std::allocator<T> >' with TT at
7755 level 1, and T at level 2, while the template arguments at level 1
7756 becomes {std::vector} and the inner level 2 is {int}. */
7757
7758 tree outer = DECL_CONTEXT (templ);
7759 if (outer)
7760 outer = generic_targs_for (outer);
7761 else if (current_template_parms)
7762 {
7763 /* This is an argument of the current template, so we haven't set
7764 DECL_CONTEXT yet. */
7765 tree relevant_template_parms;
7766
7767 /* Parameter levels that are greater than the level of the given
7768 template template parm are irrelevant. */
7769 relevant_template_parms = current_template_parms;
7770 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7771 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7772 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7773
7774 outer = template_parms_to_args (relevant_template_parms);
7775 }
7776
7777 if (outer)
7778 arglist = add_to_template_args (outer, arglist);
7779
7780 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7781 return coerce_template_parms (parmlist, arglist, templ,
7782 complain,
7783 /*require_all_args=*/true,
7784 /*use_default_args=*/true);
7785 }
7786
7787 /* A cache of template template parameters with match-all default
7788 arguments. */
7789 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7790
7791 /* T is a bound template template-parameter. Copy its arguments into default
7792 arguments of the template template-parameter's template parameters. */
7793
7794 static tree
7795 add_defaults_to_ttp (tree otmpl)
7796 {
7797 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7798 return *c;
7799
7800 tree ntmpl = copy_node (otmpl);
7801
7802 tree ntype = copy_node (TREE_TYPE (otmpl));
7803 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7804 TYPE_MAIN_VARIANT (ntype) = ntype;
7805 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7806 TYPE_NAME (ntype) = ntmpl;
7807 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7808
7809 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7810 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7811 TEMPLATE_PARM_DECL (idx) = ntmpl;
7812 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7813
7814 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7815 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7816 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7817 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7818 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7819 {
7820 tree o = TREE_VEC_ELT (vec, i);
7821 if (!template_parameter_pack_p (TREE_VALUE (o)))
7822 {
7823 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7824 TREE_PURPOSE (n) = any_targ_node;
7825 }
7826 }
7827
7828 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7829 return ntmpl;
7830 }
7831
7832 /* ARG is a bound potential template template-argument, and PARGS is a list
7833 of arguments for the corresponding template template-parameter. Adjust
7834 PARGS as appropriate for application to ARG's template, and if ARG is a
7835 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7836 arguments to the template template parameter. */
7837
7838 static tree
7839 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7840 {
7841 ++processing_template_decl;
7842 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7844 {
7845 /* When comparing two template template-parameters in partial ordering,
7846 rewrite the one currently being used as an argument to have default
7847 arguments for all parameters. */
7848 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7849 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7850 if (pargs != error_mark_node)
7851 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7852 TYPE_TI_ARGS (arg));
7853 }
7854 else
7855 {
7856 tree aparms
7857 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7858 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7859 /*require_all*/true,
7860 /*use_default*/true);
7861 }
7862 --processing_template_decl;
7863 return pargs;
7864 }
7865
7866 /* Subroutine of unify for the case when PARM is a
7867 BOUND_TEMPLATE_TEMPLATE_PARM. */
7868
7869 static int
7870 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7871 bool explain_p)
7872 {
7873 tree parmvec = TYPE_TI_ARGS (parm);
7874 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7875
7876 /* The template template parm might be variadic and the argument
7877 not, so flatten both argument lists. */
7878 parmvec = expand_template_argument_pack (parmvec);
7879 argvec = expand_template_argument_pack (argvec);
7880
7881 if (flag_new_ttp)
7882 {
7883 /* In keeping with P0522R0, adjust P's template arguments
7884 to apply to A's template; then flatten it again. */
7885 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7886 nparmvec = expand_template_argument_pack (nparmvec);
7887
7888 if (unify (tparms, targs, nparmvec, argvec,
7889 UNIFY_ALLOW_NONE, explain_p))
7890 return 1;
7891
7892 /* If the P0522 adjustment eliminated a pack expansion, deduce
7893 empty packs. */
7894 if (flag_new_ttp
7895 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7896 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7897 DEDUCE_EXACT, /*sub*/true, explain_p))
7898 return 1;
7899 }
7900 else
7901 {
7902 /* Deduce arguments T, i from TT<T> or TT<i>.
7903 We check each element of PARMVEC and ARGVEC individually
7904 rather than the whole TREE_VEC since they can have
7905 different number of elements, which is allowed under N2555. */
7906
7907 int len = TREE_VEC_LENGTH (parmvec);
7908
7909 /* Check if the parameters end in a pack, making them
7910 variadic. */
7911 int parm_variadic_p = 0;
7912 if (len > 0
7913 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7914 parm_variadic_p = 1;
7915
7916 for (int i = 0; i < len - parm_variadic_p; ++i)
7917 /* If the template argument list of P contains a pack
7918 expansion that is not the last template argument, the
7919 entire template argument list is a non-deduced
7920 context. */
7921 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7922 return unify_success (explain_p);
7923
7924 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7925 return unify_too_few_arguments (explain_p,
7926 TREE_VEC_LENGTH (argvec), len);
7927
7928 for (int i = 0; i < len - parm_variadic_p; ++i)
7929 if (unify (tparms, targs,
7930 TREE_VEC_ELT (parmvec, i),
7931 TREE_VEC_ELT (argvec, i),
7932 UNIFY_ALLOW_NONE, explain_p))
7933 return 1;
7934
7935 if (parm_variadic_p
7936 && unify_pack_expansion (tparms, targs,
7937 parmvec, argvec,
7938 DEDUCE_EXACT,
7939 /*subr=*/true, explain_p))
7940 return 1;
7941 }
7942
7943 return 0;
7944 }
7945
7946 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7947 template template parameters. Both PARM_PARMS and ARG_PARMS are
7948 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7949 or PARM_DECL.
7950
7951 Consider the example:
7952 template <class T> class A;
7953 template<template <class U> class TT> class B;
7954
7955 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7956 the parameters to A, and OUTER_ARGS contains A. */
7957
7958 static int
7959 coerce_template_template_parms (tree parm_parms_full,
7960 tree arg_parms_full,
7961 tsubst_flags_t complain,
7962 tree in_decl,
7963 tree outer_args)
7964 {
7965 int nparms, nargs, i;
7966 tree parm, arg;
7967 int variadic_p = 0;
7968
7969 tree parm_parms = INNERMOST_TEMPLATE_PARMS (parm_parms_full);
7970 tree arg_parms = INNERMOST_TEMPLATE_PARMS (arg_parms_full);
7971
7972 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7973 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7974
7975 nparms = TREE_VEC_LENGTH (parm_parms);
7976 nargs = TREE_VEC_LENGTH (arg_parms);
7977
7978 if (flag_new_ttp)
7979 {
7980 /* P0522R0: A template template-parameter P is at least as specialized as
7981 a template template-argument A if, given the following rewrite to two
7982 function templates, the function template corresponding to P is at
7983 least as specialized as the function template corresponding to A
7984 according to the partial ordering rules for function templates
7985 ([temp.func.order]). Given an invented class template X with the
7986 template parameter list of A (including default arguments):
7987
7988 * Each of the two function templates has the same template parameters,
7989 respectively, as P or A.
7990
7991 * Each function template has a single function parameter whose type is
7992 a specialization of X with template arguments corresponding to the
7993 template parameters from the respective function template where, for
7994 each template parameter PP in the template parameter list of the
7995 function template, a corresponding template argument AA is formed. If
7996 PP declares a parameter pack, then AA is the pack expansion
7997 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7998
7999 If the rewrite produces an invalid type, then P is not at least as
8000 specialized as A. */
8001
8002 /* So coerce P's args to apply to A's parms, and then deduce between A's
8003 args and the converted args. If that succeeds, A is at least as
8004 specialized as P, so they match.*/
8005 tree pargs = template_parms_level_to_args (parm_parms);
8006
8007 /* PARM, and thus the context in which we are passing ARG to it, may be
8008 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8009 want to provide the right number of levels, so we reduce the number of
8010 levels in OUTER_ARGS before prepending them. This is most important
8011 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8012
8013 ARG might also be deeper than PARM (ttp23). In that case, we include
8014 all of OUTER_ARGS. The missing levels seem potentially problematic,
8015 but I can't come up with a testcase that breaks. */
8016 if (int arg_outer_levs = TMPL_PARMS_DEPTH (arg_parms_full) - 1)
8017 {
8018 auto x = make_temp_override (TREE_VEC_LENGTH (outer_args));
8019 if (TMPL_ARGS_DEPTH (outer_args) > arg_outer_levs)
8020 TREE_VEC_LENGTH (outer_args) = arg_outer_levs;
8021 pargs = add_to_template_args (outer_args, pargs);
8022 }
8023
8024 ++processing_template_decl;
8025 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
8026 /*require_all*/true, /*use_default*/true);
8027 --processing_template_decl;
8028 if (pargs != error_mark_node)
8029 {
8030 tree targs = make_tree_vec (nargs);
8031 tree aargs = template_parms_level_to_args (arg_parms);
8032 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
8033 /*explain*/false))
8034 return 1;
8035 }
8036 }
8037
8038 /* Determine whether we have a parameter pack at the end of the
8039 template template parameter's template parameter list. */
8040 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
8041 {
8042 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8043
8044 if (error_operand_p (parm))
8045 return 0;
8046
8047 switch (TREE_CODE (parm))
8048 {
8049 case TEMPLATE_DECL:
8050 case TYPE_DECL:
8051 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8052 variadic_p = 1;
8053 break;
8054
8055 case PARM_DECL:
8056 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8057 variadic_p = 1;
8058 break;
8059
8060 default:
8061 gcc_unreachable ();
8062 }
8063 }
8064
8065 if (nargs != nparms
8066 && !(variadic_p && nargs >= nparms - 1))
8067 return 0;
8068
8069 /* Check all of the template parameters except the parameter pack at
8070 the end (if any). */
8071 for (i = 0; i < nparms - variadic_p; ++i)
8072 {
8073 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8074 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8075 continue;
8076
8077 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8078 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8079
8080 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8081 outer_args))
8082 return 0;
8083
8084 }
8085
8086 if (variadic_p)
8087 {
8088 /* Check each of the template parameters in the template
8089 argument against the template parameter pack at the end of
8090 the template template parameter. */
8091 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8092 return 0;
8093
8094 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8095
8096 for (; i < nargs; ++i)
8097 {
8098 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8099 continue;
8100
8101 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8102
8103 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8104 outer_args))
8105 return 0;
8106 }
8107 }
8108
8109 return 1;
8110 }
8111
8112 /* Verifies that the deduced template arguments (in TARGS) for the
8113 template template parameters (in TPARMS) represent valid bindings,
8114 by comparing the template parameter list of each template argument
8115 to the template parameter list of its corresponding template
8116 template parameter, in accordance with DR150. This
8117 routine can only be called after all template arguments have been
8118 deduced. It will return TRUE if all of the template template
8119 parameter bindings are okay, FALSE otherwise. */
8120 bool
8121 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8122 {
8123 int i, ntparms = TREE_VEC_LENGTH (tparms);
8124 bool ret = true;
8125
8126 /* We're dealing with template parms in this process. */
8127 ++processing_template_decl;
8128
8129 targs = INNERMOST_TEMPLATE_ARGS (targs);
8130
8131 for (i = 0; i < ntparms; ++i)
8132 {
8133 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8134 tree targ = TREE_VEC_ELT (targs, i);
8135
8136 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8137 {
8138 tree packed_args = NULL_TREE;
8139 int idx, len = 1;
8140
8141 if (ARGUMENT_PACK_P (targ))
8142 {
8143 /* Look inside the argument pack. */
8144 packed_args = ARGUMENT_PACK_ARGS (targ);
8145 len = TREE_VEC_LENGTH (packed_args);
8146 }
8147
8148 for (idx = 0; idx < len; ++idx)
8149 {
8150 tree targ_parms = NULL_TREE;
8151
8152 if (packed_args)
8153 /* Extract the next argument from the argument
8154 pack. */
8155 targ = TREE_VEC_ELT (packed_args, idx);
8156
8157 if (PACK_EXPANSION_P (targ))
8158 /* Look at the pattern of the pack expansion. */
8159 targ = PACK_EXPANSION_PATTERN (targ);
8160
8161 /* Extract the template parameters from the template
8162 argument. */
8163 if (TREE_CODE (targ) == TEMPLATE_DECL)
8164 targ_parms = DECL_TEMPLATE_PARMS (targ);
8165 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8166 targ_parms = DECL_TEMPLATE_PARMS (TYPE_NAME (targ));
8167
8168 /* Verify that we can coerce the template template
8169 parameters from the template argument to the template
8170 parameter. This requires an exact match. */
8171 if (targ_parms
8172 && !coerce_template_template_parms
8173 (DECL_TEMPLATE_PARMS (tparm),
8174 targ_parms,
8175 tf_none,
8176 tparm,
8177 targs))
8178 {
8179 ret = false;
8180 goto out;
8181 }
8182 }
8183 }
8184 }
8185
8186 out:
8187
8188 --processing_template_decl;
8189 return ret;
8190 }
8191
8192 /* Since type attributes aren't mangled, we need to strip them from
8193 template type arguments. */
8194
8195 tree
8196 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8197 {
8198 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8199 return arg;
8200 bool removed_attributes = false;
8201 tree canon = strip_typedefs (arg, &removed_attributes);
8202 if (removed_attributes
8203 && (complain & tf_warning))
8204 warning (OPT_Wignored_attributes,
8205 "ignoring attributes on template argument %qT", arg);
8206 return canon;
8207 }
8208
8209 /* And from inside dependent non-type arguments like sizeof(Type). */
8210
8211 static tree
8212 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8213 {
8214 if (!arg || arg == error_mark_node)
8215 return arg;
8216 bool removed_attributes = false;
8217 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8218 if (removed_attributes
8219 && (complain & tf_warning))
8220 warning (OPT_Wignored_attributes,
8221 "ignoring attributes in template argument %qE", arg);
8222 return canon;
8223 }
8224
8225 /* A template declaration can be substituted for a constrained
8226 template template parameter only when the argument is no more
8227 constrained than the parameter. */
8228
8229 static bool
8230 is_compatible_template_arg (tree parm, tree arg)
8231 {
8232 tree parm_cons = get_constraints (parm);
8233
8234 /* For now, allow constrained template template arguments
8235 and unconstrained template template parameters. */
8236 if (parm_cons == NULL_TREE)
8237 return true;
8238
8239 /* If the template parameter is constrained, we need to rewrite its
8240 constraints in terms of the ARG's template parameters. This ensures
8241 that all of the template parameter types will have the same depth.
8242
8243 Note that this is only valid when coerce_template_template_parm is
8244 true for the innermost template parameters of PARM and ARG. In other
8245 words, because coercion is successful, this conversion will be valid. */
8246 tree new_args = NULL_TREE;
8247 if (parm_cons)
8248 {
8249 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8250 new_args = template_parms_level_to_args (aparms);
8251 ++processing_template_decl;
8252 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8253 tf_none, NULL_TREE);
8254 --processing_template_decl;
8255 if (parm_cons == error_mark_node)
8256 return false;
8257 }
8258
8259 return weakly_subsumes (parm_cons, arg);
8260 }
8261
8262 // Convert a placeholder argument into a binding to the original
8263 // parameter. The original parameter is saved as the TREE_TYPE of
8264 // ARG.
8265 static inline tree
8266 convert_wildcard_argument (tree parm, tree arg)
8267 {
8268 TREE_TYPE (arg) = parm;
8269 return arg;
8270 }
8271
8272 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8273 because one of them is dependent. But we need to represent the
8274 conversion for the benefit of cp_tree_equal. */
8275
8276 static tree
8277 maybe_convert_nontype_argument (tree type, tree arg)
8278 {
8279 /* Auto parms get no conversion. */
8280 if (type_uses_auto (type))
8281 return arg;
8282 /* We don't need or want to add this conversion now if we're going to use the
8283 argument for deduction. */
8284 if (value_dependent_expression_p (arg))
8285 return arg;
8286
8287 type = cv_unqualified (type);
8288 tree argtype = TREE_TYPE (arg);
8289 if (same_type_p (type, argtype))
8290 return arg;
8291
8292 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8293 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8294 return arg;
8295 }
8296
8297 /* Convert the indicated template ARG as necessary to match the
8298 indicated template PARM. Returns the converted ARG, or
8299 error_mark_node if the conversion was unsuccessful. Error and
8300 warning messages are issued under control of COMPLAIN. This
8301 conversion is for the Ith parameter in the parameter list. ARGS is
8302 the full set of template arguments deduced so far. */
8303
8304 static tree
8305 convert_template_argument (tree parm,
8306 tree arg,
8307 tree args,
8308 tsubst_flags_t complain,
8309 int i,
8310 tree in_decl)
8311 {
8312 tree orig_arg;
8313 tree val;
8314 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8315
8316 if (parm == error_mark_node || error_operand_p (arg))
8317 return error_mark_node;
8318
8319 /* Trivially convert placeholders. */
8320 if (TREE_CODE (arg) == WILDCARD_DECL)
8321 return convert_wildcard_argument (parm, arg);
8322
8323 if (arg == any_targ_node)
8324 return arg;
8325
8326 if (TREE_CODE (arg) == TREE_LIST
8327 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8328 {
8329 /* The template argument was the name of some
8330 member function. That's usually
8331 invalid, but static members are OK. In any
8332 case, grab the underlying fields/functions
8333 and issue an error later if required. */
8334 TREE_TYPE (arg) = unknown_type_node;
8335 }
8336
8337 orig_arg = arg;
8338
8339 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8340 requires_type = (TREE_CODE (parm) == TYPE_DECL
8341 || requires_tmpl_type);
8342
8343 /* When determining whether an argument pack expansion is a template,
8344 look at the pattern. */
8345 if (PACK_EXPANSION_P (arg))
8346 arg = PACK_EXPANSION_PATTERN (arg);
8347
8348 /* Deal with an injected-class-name used as a template template arg. */
8349 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8350 {
8351 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8352 if (TREE_CODE (t) == TEMPLATE_DECL)
8353 {
8354 if (cxx_dialect >= cxx11)
8355 /* OK under DR 1004. */;
8356 else if (complain & tf_warning_or_error)
8357 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8358 " used as template template argument", TYPE_NAME (arg));
8359 else if (flag_pedantic_errors)
8360 t = arg;
8361
8362 arg = t;
8363 }
8364 }
8365
8366 is_tmpl_type =
8367 ((TREE_CODE (arg) == TEMPLATE_DECL
8368 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8369 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8370 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8371 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8372
8373 if (is_tmpl_type
8374 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8375 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8376 arg = TYPE_STUB_DECL (arg);
8377
8378 is_type = TYPE_P (arg) || is_tmpl_type;
8379
8380 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8381 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8382 {
8383 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8384 {
8385 if (complain & tf_error)
8386 error ("invalid use of destructor %qE as a type", orig_arg);
8387 return error_mark_node;
8388 }
8389
8390 permerror (input_location,
8391 "to refer to a type member of a template parameter, "
8392 "use %<typename %E%>", orig_arg);
8393
8394 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8395 TREE_OPERAND (arg, 1),
8396 typename_type,
8397 complain);
8398 arg = orig_arg;
8399 is_type = 1;
8400 }
8401 if (is_type != requires_type)
8402 {
8403 if (in_decl)
8404 {
8405 if (complain & tf_error)
8406 {
8407 error ("type/value mismatch at argument %d in template "
8408 "parameter list for %qD",
8409 i + 1, in_decl);
8410 if (is_type)
8411 {
8412 /* The template argument is a type, but we're expecting
8413 an expression. */
8414 inform (input_location,
8415 " expected a constant of type %qT, got %qT",
8416 TREE_TYPE (parm),
8417 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8418 /* [temp.arg]/2: "In a template-argument, an ambiguity
8419 between a type-id and an expression is resolved to a
8420 type-id, regardless of the form of the corresponding
8421 template-parameter." So give the user a clue. */
8422 if (TREE_CODE (arg) == FUNCTION_TYPE)
8423 inform (input_location, " ambiguous template argument "
8424 "for non-type template parameter is treated as "
8425 "function type");
8426 }
8427 else if (requires_tmpl_type)
8428 inform (input_location,
8429 " expected a class template, got %qE", orig_arg);
8430 else
8431 inform (input_location,
8432 " expected a type, got %qE", orig_arg);
8433 }
8434 }
8435 return error_mark_node;
8436 }
8437 if (is_tmpl_type ^ requires_tmpl_type)
8438 {
8439 if (in_decl && (complain & tf_error))
8440 {
8441 error ("type/value mismatch at argument %d in template "
8442 "parameter list for %qD",
8443 i + 1, in_decl);
8444 if (is_tmpl_type)
8445 inform (input_location,
8446 " expected a type, got %qT", DECL_NAME (arg));
8447 else
8448 inform (input_location,
8449 " expected a class template, got %qT", orig_arg);
8450 }
8451 return error_mark_node;
8452 }
8453
8454 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8455 /* We already did the appropriate conversion when packing args. */
8456 val = orig_arg;
8457 else if (is_type)
8458 {
8459 if (requires_tmpl_type)
8460 {
8461 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8462 /* The number of argument required is not known yet.
8463 Just accept it for now. */
8464 val = orig_arg;
8465 else
8466 {
8467 tree parmparm = DECL_TEMPLATE_PARMS (parm);
8468 tree argparm;
8469
8470 /* Strip alias templates that are equivalent to another
8471 template. */
8472 arg = get_underlying_template (arg);
8473 argparm = DECL_TEMPLATE_PARMS (arg);
8474
8475 if (coerce_template_template_parms (parmparm, argparm,
8476 complain, in_decl,
8477 args))
8478 {
8479 val = arg;
8480
8481 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8482 TEMPLATE_DECL. */
8483 if (val != error_mark_node)
8484 {
8485 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8486 val = TREE_TYPE (val);
8487 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8488 val = make_pack_expansion (val, complain);
8489 }
8490 }
8491 else
8492 {
8493 if (in_decl && (complain & tf_error))
8494 {
8495 error ("type/value mismatch at argument %d in "
8496 "template parameter list for %qD",
8497 i + 1, in_decl);
8498 inform (input_location,
8499 " expected a template of type %qD, got %qT",
8500 parm, orig_arg);
8501 }
8502
8503 val = error_mark_node;
8504 }
8505
8506 // Check that the constraints are compatible before allowing the
8507 // substitution.
8508 if (val != error_mark_node)
8509 if (!is_compatible_template_arg (parm, arg))
8510 {
8511 if (in_decl && (complain & tf_error))
8512 {
8513 error ("constraint mismatch at argument %d in "
8514 "template parameter list for %qD",
8515 i + 1, in_decl);
8516 inform (input_location, " expected %qD but got %qD",
8517 parm, arg);
8518 }
8519 val = error_mark_node;
8520 }
8521 }
8522 }
8523 else
8524 val = orig_arg;
8525 /* We only form one instance of each template specialization.
8526 Therefore, if we use a non-canonical variant (i.e., a
8527 typedef), any future messages referring to the type will use
8528 the typedef, which is confusing if those future uses do not
8529 themselves also use the typedef. */
8530 if (TYPE_P (val))
8531 val = canonicalize_type_argument (val, complain);
8532 }
8533 else
8534 {
8535 tree t = TREE_TYPE (parm);
8536
8537 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8538 > TMPL_ARGS_DEPTH (args))
8539 /* We don't have enough levels of args to do any substitution. This
8540 can happen in the context of -fnew-ttp-matching. */;
8541 else if (tree a = type_uses_auto (t))
8542 {
8543 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8544 LOOKUP_IMPLICIT);
8545 if (t == error_mark_node)
8546 return error_mark_node;
8547 }
8548 else
8549 t = tsubst (t, args, complain, in_decl);
8550
8551 if (invalid_nontype_parm_type_p (t, complain))
8552 return error_mark_node;
8553
8554 if (t != TREE_TYPE (parm))
8555 t = canonicalize_type_argument (t, complain);
8556
8557 if (!type_dependent_expression_p (orig_arg)
8558 && !uses_template_parms (t))
8559 /* We used to call digest_init here. However, digest_init
8560 will report errors, which we don't want when complain
8561 is zero. More importantly, digest_init will try too
8562 hard to convert things: for example, `0' should not be
8563 converted to pointer type at this point according to
8564 the standard. Accepting this is not merely an
8565 extension, since deciding whether or not these
8566 conversions can occur is part of determining which
8567 function template to call, or whether a given explicit
8568 argument specification is valid. */
8569 val = convert_nontype_argument (t, orig_arg, complain);
8570 else
8571 {
8572 val = canonicalize_expr_argument (orig_arg, complain);
8573 val = maybe_convert_nontype_argument (t, val);
8574 }
8575
8576
8577 if (val == NULL_TREE)
8578 val = error_mark_node;
8579 else if (val == error_mark_node && (complain & tf_error))
8580 error_at (cp_expr_loc_or_input_loc (orig_arg),
8581 "could not convert template argument %qE from %qT to %qT",
8582 orig_arg, TREE_TYPE (orig_arg), t);
8583
8584 if (INDIRECT_REF_P (val))
8585 {
8586 /* Reject template arguments that are references to built-in
8587 functions with no library fallbacks. */
8588 const_tree inner = TREE_OPERAND (val, 0);
8589 const_tree innertype = TREE_TYPE (inner);
8590 if (innertype
8591 && TYPE_REF_P (innertype)
8592 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8593 && TREE_OPERAND_LENGTH (inner) > 0
8594 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8595 return error_mark_node;
8596 }
8597
8598 if (TREE_CODE (val) == SCOPE_REF)
8599 {
8600 /* Strip typedefs from the SCOPE_REF. */
8601 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8602 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8603 complain);
8604 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8605 QUALIFIED_NAME_IS_TEMPLATE (val));
8606 }
8607 }
8608
8609 return val;
8610 }
8611
8612 /* Coerces the remaining template arguments in INNER_ARGS (from
8613 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8614 Returns the coerced argument pack. PARM_IDX is the position of this
8615 parameter in the template parameter list. ARGS is the original
8616 template argument list. */
8617 static tree
8618 coerce_template_parameter_pack (tree parms,
8619 int parm_idx,
8620 tree args,
8621 tree inner_args,
8622 int arg_idx,
8623 tree new_args,
8624 int* lost,
8625 tree in_decl,
8626 tsubst_flags_t complain)
8627 {
8628 tree parm = TREE_VEC_ELT (parms, parm_idx);
8629 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8630 tree packed_args;
8631 tree argument_pack;
8632 tree packed_parms = NULL_TREE;
8633
8634 if (arg_idx > nargs)
8635 arg_idx = nargs;
8636
8637 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8638 {
8639 /* When the template parameter is a non-type template parameter pack
8640 or template template parameter pack whose type or template
8641 parameters use parameter packs, we know exactly how many arguments
8642 we are looking for. Build a vector of the instantiated decls for
8643 these template parameters in PACKED_PARMS. */
8644 /* We can't use make_pack_expansion here because it would interpret a
8645 _DECL as a use rather than a declaration. */
8646 tree decl = TREE_VALUE (parm);
8647 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8648 SET_PACK_EXPANSION_PATTERN (exp, decl);
8649 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8650 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8651
8652 TREE_VEC_LENGTH (args)--;
8653 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8654 TREE_VEC_LENGTH (args)++;
8655
8656 if (packed_parms == error_mark_node)
8657 return error_mark_node;
8658
8659 /* If we're doing a partial instantiation of a member template,
8660 verify that all of the types used for the non-type
8661 template parameter pack are, in fact, valid for non-type
8662 template parameters. */
8663 if (arg_idx < nargs
8664 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8665 {
8666 int j, len = TREE_VEC_LENGTH (packed_parms);
8667 for (j = 0; j < len; ++j)
8668 {
8669 tree t = TREE_VEC_ELT (packed_parms, j);
8670 if (TREE_CODE (t) == PARM_DECL
8671 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8672 return error_mark_node;
8673 }
8674 /* We don't know how many args we have yet, just
8675 use the unconverted ones for now. */
8676 return NULL_TREE;
8677 }
8678
8679 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8680 }
8681 /* Check if we have a placeholder pack, which indicates we're
8682 in the context of a introduction list. In that case we want
8683 to match this pack to the single placeholder. */
8684 else if (arg_idx < nargs
8685 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8686 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8687 {
8688 nargs = arg_idx + 1;
8689 packed_args = make_tree_vec (1);
8690 }
8691 else
8692 packed_args = make_tree_vec (nargs - arg_idx);
8693
8694 /* Convert the remaining arguments, which will be a part of the
8695 parameter pack "parm". */
8696 int first_pack_arg = arg_idx;
8697 for (; arg_idx < nargs; ++arg_idx)
8698 {
8699 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8700 tree actual_parm = TREE_VALUE (parm);
8701 int pack_idx = arg_idx - first_pack_arg;
8702
8703 if (packed_parms)
8704 {
8705 /* Once we've packed as many args as we have types, stop. */
8706 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8707 break;
8708 else if (PACK_EXPANSION_P (arg))
8709 /* We don't know how many args we have yet, just
8710 use the unconverted ones for now. */
8711 return NULL_TREE;
8712 else
8713 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8714 }
8715
8716 if (arg == error_mark_node)
8717 {
8718 if (complain & tf_error)
8719 error ("template argument %d is invalid", arg_idx + 1);
8720 }
8721 else
8722 arg = convert_template_argument (actual_parm,
8723 arg, new_args, complain, parm_idx,
8724 in_decl);
8725 if (arg == error_mark_node)
8726 (*lost)++;
8727 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8728 }
8729
8730 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8731 && TREE_VEC_LENGTH (packed_args) > 0)
8732 {
8733 if (complain & tf_error)
8734 error ("wrong number of template arguments (%d, should be %d)",
8735 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8736 return error_mark_node;
8737 }
8738
8739 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8740 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8741 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8742 else
8743 {
8744 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8745 TREE_CONSTANT (argument_pack) = 1;
8746 }
8747
8748 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8749 if (CHECKING_P)
8750 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8751 TREE_VEC_LENGTH (packed_args));
8752 return argument_pack;
8753 }
8754
8755 /* Returns the number of pack expansions in the template argument vector
8756 ARGS. */
8757
8758 static int
8759 pack_expansion_args_count (tree args)
8760 {
8761 int i;
8762 int count = 0;
8763 if (args)
8764 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8765 {
8766 tree elt = TREE_VEC_ELT (args, i);
8767 if (elt && PACK_EXPANSION_P (elt))
8768 ++count;
8769 }
8770 return count;
8771 }
8772
8773 /* Convert all template arguments to their appropriate types, and
8774 return a vector containing the innermost resulting template
8775 arguments. If any error occurs, return error_mark_node. Error and
8776 warning messages are issued under control of COMPLAIN.
8777
8778 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8779 for arguments not specified in ARGS. Otherwise, if
8780 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8781 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8782 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8783 ARGS. */
8784
8785 static tree
8786 coerce_template_parms (tree parms,
8787 tree args,
8788 tree in_decl,
8789 tsubst_flags_t complain,
8790 bool require_all_args,
8791 bool use_default_args)
8792 {
8793 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8794 tree orig_inner_args;
8795 tree inner_args;
8796 tree new_args;
8797 tree new_inner_args;
8798
8799 /* When used as a boolean value, indicates whether this is a
8800 variadic template parameter list. Since it's an int, we can also
8801 subtract it from nparms to get the number of non-variadic
8802 parameters. */
8803 int variadic_p = 0;
8804 int variadic_args_p = 0;
8805 int post_variadic_parms = 0;
8806
8807 /* Adjustment to nparms for fixed parameter packs. */
8808 int fixed_pack_adjust = 0;
8809 int fixed_packs = 0;
8810 int missing = 0;
8811
8812 /* Likewise for parameters with default arguments. */
8813 int default_p = 0;
8814
8815 if (args == error_mark_node)
8816 return error_mark_node;
8817
8818 nparms = TREE_VEC_LENGTH (parms);
8819
8820 /* Determine if there are any parameter packs or default arguments. */
8821 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8822 {
8823 tree parm = TREE_VEC_ELT (parms, parm_idx);
8824 if (variadic_p)
8825 ++post_variadic_parms;
8826 if (template_parameter_pack_p (TREE_VALUE (parm)))
8827 ++variadic_p;
8828 if (TREE_PURPOSE (parm))
8829 ++default_p;
8830 }
8831
8832 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8833 /* If there are no parameters that follow a parameter pack, we need to
8834 expand any argument packs so that we can deduce a parameter pack from
8835 some non-packed args followed by an argument pack, as in variadic85.C.
8836 If there are such parameters, we need to leave argument packs intact
8837 so the arguments are assigned properly. This can happen when dealing
8838 with a nested class inside a partial specialization of a class
8839 template, as in variadic92.C, or when deducing a template parameter pack
8840 from a sub-declarator, as in variadic114.C. */
8841 if (!post_variadic_parms)
8842 inner_args = expand_template_argument_pack (inner_args);
8843
8844 /* Count any pack expansion args. */
8845 variadic_args_p = pack_expansion_args_count (inner_args);
8846
8847 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8848 if ((nargs - variadic_args_p > nparms && !variadic_p)
8849 || (nargs < nparms - variadic_p
8850 && require_all_args
8851 && !variadic_args_p
8852 && (!use_default_args
8853 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8854 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8855 {
8856 bad_nargs:
8857 if (complain & tf_error)
8858 {
8859 if (variadic_p || default_p)
8860 {
8861 nparms -= variadic_p + default_p;
8862 error ("wrong number of template arguments "
8863 "(%d, should be at least %d)", nargs, nparms);
8864 }
8865 else
8866 error ("wrong number of template arguments "
8867 "(%d, should be %d)", nargs, nparms);
8868
8869 if (in_decl)
8870 inform (DECL_SOURCE_LOCATION (in_decl),
8871 "provided for %qD", in_decl);
8872 }
8873
8874 return error_mark_node;
8875 }
8876 /* We can't pass a pack expansion to a non-pack parameter of an alias
8877 template (DR 1430). */
8878 else if (in_decl
8879 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8880 || concept_definition_p (in_decl))
8881 && variadic_args_p
8882 && nargs - variadic_args_p < nparms - variadic_p)
8883 {
8884 if (complain & tf_error)
8885 {
8886 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8887 {
8888 tree arg = TREE_VEC_ELT (inner_args, i);
8889 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8890
8891 if (PACK_EXPANSION_P (arg)
8892 && !template_parameter_pack_p (parm))
8893 {
8894 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8895 error_at (location_of (arg),
8896 "pack expansion argument for non-pack parameter "
8897 "%qD of alias template %qD", parm, in_decl);
8898 else
8899 error_at (location_of (arg),
8900 "pack expansion argument for non-pack parameter "
8901 "%qD of concept %qD", parm, in_decl);
8902 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8903 goto found;
8904 }
8905 }
8906 gcc_unreachable ();
8907 found:;
8908 }
8909 return error_mark_node;
8910 }
8911
8912 /* We need to evaluate the template arguments, even though this
8913 template-id may be nested within a "sizeof". */
8914 cp_evaluated ev;
8915
8916 new_inner_args = make_tree_vec (nparms);
8917 new_args = add_outermost_template_args (args, new_inner_args);
8918 int pack_adjust = 0;
8919 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8920 {
8921 tree arg;
8922 tree parm;
8923
8924 /* Get the Ith template parameter. */
8925 parm = TREE_VEC_ELT (parms, parm_idx);
8926
8927 if (parm == error_mark_node)
8928 {
8929 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8930 continue;
8931 }
8932
8933 /* Calculate the next argument. */
8934 if (arg_idx < nargs)
8935 arg = TREE_VEC_ELT (inner_args, arg_idx);
8936 else
8937 arg = NULL_TREE;
8938
8939 if (template_parameter_pack_p (TREE_VALUE (parm))
8940 && (arg || require_all_args || !(complain & tf_partial))
8941 && !(arg && ARGUMENT_PACK_P (arg)))
8942 {
8943 /* Some arguments will be placed in the
8944 template parameter pack PARM. */
8945 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8946 inner_args, arg_idx,
8947 new_args, &lost,
8948 in_decl, complain);
8949
8950 if (arg == NULL_TREE)
8951 {
8952 /* We don't know how many args we have yet, just use the
8953 unconverted (and still packed) ones for now. */
8954 new_inner_args = orig_inner_args;
8955 arg_idx = nargs;
8956 break;
8957 }
8958
8959 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8960
8961 /* Store this argument. */
8962 if (arg == error_mark_node)
8963 {
8964 lost++;
8965 /* We are done with all of the arguments. */
8966 arg_idx = nargs;
8967 break;
8968 }
8969 else
8970 {
8971 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8972 arg_idx += pack_adjust;
8973 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8974 {
8975 ++fixed_packs;
8976 fixed_pack_adjust += pack_adjust;
8977 }
8978 }
8979
8980 continue;
8981 }
8982 else if (arg)
8983 {
8984 if (PACK_EXPANSION_P (arg))
8985 {
8986 /* "If every valid specialization of a variadic template
8987 requires an empty template parameter pack, the template is
8988 ill-formed, no diagnostic required." So check that the
8989 pattern works with this parameter. */
8990 tree pattern = PACK_EXPANSION_PATTERN (arg);
8991 tree conv = convert_template_argument (TREE_VALUE (parm),
8992 pattern, new_args,
8993 complain, parm_idx,
8994 in_decl);
8995 if (conv == error_mark_node)
8996 {
8997 if (complain & tf_error)
8998 inform (input_location, "so any instantiation with a "
8999 "non-empty parameter pack would be ill-formed");
9000 ++lost;
9001 }
9002 else if (TYPE_P (conv) && !TYPE_P (pattern))
9003 /* Recover from missing typename. */
9004 TREE_VEC_ELT (inner_args, arg_idx)
9005 = make_pack_expansion (conv, complain);
9006
9007 /* We don't know how many args we have yet, just
9008 use the unconverted ones for now. */
9009 new_inner_args = inner_args;
9010 arg_idx = nargs;
9011 break;
9012 }
9013 }
9014 else if (require_all_args)
9015 {
9016 /* There must be a default arg in this case. */
9017 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
9018 complain, in_decl);
9019 /* The position of the first default template argument,
9020 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9021 Record that. */
9022 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9023 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9024 arg_idx - pack_adjust);
9025 }
9026 else
9027 break;
9028
9029 if (arg == error_mark_node)
9030 {
9031 if (complain & tf_error)
9032 error ("template argument %d is invalid", arg_idx + 1);
9033 }
9034 else if (!arg)
9035 {
9036 /* This can occur if there was an error in the template
9037 parameter list itself (which we would already have
9038 reported) that we are trying to recover from, e.g., a class
9039 template with a parameter list such as
9040 template<typename..., typename> (cpp0x/variadic150.C). */
9041 ++lost;
9042
9043 /* This can also happen with a fixed parameter pack (71834). */
9044 if (arg_idx >= nargs)
9045 ++missing;
9046 }
9047 else
9048 arg = convert_template_argument (TREE_VALUE (parm),
9049 arg, new_args, complain,
9050 parm_idx, in_decl);
9051
9052 if (arg == error_mark_node)
9053 lost++;
9054
9055 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9056 }
9057
9058 if (missing || arg_idx < nargs - variadic_args_p)
9059 {
9060 /* If we had fixed parameter packs, we didn't know how many arguments we
9061 actually needed earlier; now we do. */
9062 nparms += fixed_pack_adjust;
9063 variadic_p -= fixed_packs;
9064 goto bad_nargs;
9065 }
9066
9067 if (arg_idx < nargs)
9068 {
9069 /* We had some pack expansion arguments that will only work if the packs
9070 are empty, but wait until instantiation time to complain.
9071 See variadic-ttp3.C. */
9072
9073 /* Except that we can't provide empty packs to alias templates or
9074 concepts when there are no corresponding parameters. Basically,
9075 we can get here with this:
9076
9077 template<typename T> concept C = true;
9078
9079 template<typename... Args>
9080 requires C<Args...>
9081 void f();
9082
9083 When parsing C<Args...>, we try to form a concept check of
9084 C<?, Args...>. Without the extra check for substituting an empty
9085 pack past the last parameter, we can accept the check as valid.
9086
9087 FIXME: This may be valid for alias templates (but I doubt it).
9088
9089 FIXME: The error could be better also. */
9090 if (in_decl && concept_definition_p (in_decl))
9091 {
9092 if (complain & tf_error)
9093 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9094 "too many arguments");
9095 return error_mark_node;
9096 }
9097
9098 int len = nparms + (nargs - arg_idx);
9099 tree args = make_tree_vec (len);
9100 int i = 0;
9101 for (; i < nparms; ++i)
9102 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9103 for (; i < len; ++i, ++arg_idx)
9104 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9105 arg_idx - pack_adjust);
9106 new_inner_args = args;
9107 }
9108
9109 if (lost)
9110 {
9111 gcc_assert (!(complain & tf_error) || seen_error ());
9112 return error_mark_node;
9113 }
9114
9115 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9116 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9117 TREE_VEC_LENGTH (new_inner_args));
9118
9119 return new_inner_args;
9120 }
9121
9122 /* Convert all template arguments to their appropriate types, and
9123 return a vector containing the innermost resulting template
9124 arguments. If any error occurs, return error_mark_node. Error and
9125 warning messages are not issued.
9126
9127 Note that no function argument deduction is performed, and default
9128 arguments are used to fill in unspecified arguments. */
9129 tree
9130 coerce_template_parms (tree parms, tree args, tree in_decl)
9131 {
9132 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9133 }
9134
9135 /* Convert all template arguments to their appropriate type, and
9136 instantiate default arguments as needed. This returns a vector
9137 containing the innermost resulting template arguments, or
9138 error_mark_node if unsuccessful. */
9139 tree
9140 coerce_template_parms (tree parms, tree args, tree in_decl,
9141 tsubst_flags_t complain)
9142 {
9143 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9144 }
9145
9146 /* Like coerce_template_parms. If PARMS represents all template
9147 parameters levels, this function returns a vector of vectors
9148 representing all the resulting argument levels. Note that in this
9149 case, only the innermost arguments are coerced because the
9150 outermost ones are supposed to have been coerced already.
9151
9152 Otherwise, if PARMS represents only (the innermost) vector of
9153 parameters, this function returns a vector containing just the
9154 innermost resulting arguments. */
9155
9156 static tree
9157 coerce_innermost_template_parms (tree parms,
9158 tree args,
9159 tree in_decl,
9160 tsubst_flags_t complain,
9161 bool require_all_args,
9162 bool use_default_args)
9163 {
9164 int parms_depth = TMPL_PARMS_DEPTH (parms);
9165 int args_depth = TMPL_ARGS_DEPTH (args);
9166 tree coerced_args;
9167
9168 if (parms_depth > 1)
9169 {
9170 coerced_args = make_tree_vec (parms_depth);
9171 tree level;
9172 int cur_depth;
9173
9174 for (level = parms, cur_depth = parms_depth;
9175 parms_depth > 0 && level != NULL_TREE;
9176 level = TREE_CHAIN (level), --cur_depth)
9177 {
9178 tree l;
9179 if (cur_depth == args_depth)
9180 l = coerce_template_parms (TREE_VALUE (level),
9181 args, in_decl, complain,
9182 require_all_args,
9183 use_default_args);
9184 else
9185 l = TMPL_ARGS_LEVEL (args, cur_depth);
9186
9187 if (l == error_mark_node)
9188 return error_mark_node;
9189
9190 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9191 }
9192 }
9193 else
9194 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9195 args, in_decl, complain,
9196 require_all_args,
9197 use_default_args);
9198 return coerced_args;
9199 }
9200
9201 /* Returns true if T is a wrapper to make a C++20 template parameter
9202 object const. */
9203
9204 static bool
9205 class_nttp_const_wrapper_p (tree t)
9206 {
9207 if (cxx_dialect < cxx20)
9208 return false;
9209 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9210 && CP_TYPE_CONST_P (TREE_TYPE (t))
9211 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9212 }
9213
9214 /* Returns 1 if template args OT and NT are equivalent. */
9215
9216 int
9217 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9218 {
9219 if (nt == ot)
9220 return 1;
9221 if (nt == NULL_TREE || ot == NULL_TREE)
9222 return false;
9223 if (nt == any_targ_node || ot == any_targ_node)
9224 return true;
9225
9226 if (class_nttp_const_wrapper_p (nt))
9227 nt = TREE_OPERAND (nt, 0);
9228 if (class_nttp_const_wrapper_p (ot))
9229 ot = TREE_OPERAND (ot, 0);
9230
9231 /* DR 1558: Don't treat an alias template specialization with dependent
9232 arguments as equivalent to its underlying type when used as a template
9233 argument; we need them to be distinct so that we substitute into the
9234 specialization arguments at instantiation time. And aliases can't be
9235 equivalent without being ==, so we don't need to look any deeper.
9236
9237 During partial ordering, however, we need to treat them normally so we can
9238 order uses of the same alias with different cv-qualification (79960). */
9239 auto cso = make_temp_override (comparing_dependent_aliases);
9240 if (!partial_order)
9241 ++comparing_dependent_aliases;
9242
9243 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9244 /* For member templates */
9245 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9246 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9247 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9248 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9249 PACK_EXPANSION_PATTERN (nt))
9250 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9251 PACK_EXPANSION_EXTRA_ARGS (nt)));
9252 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9253 return cp_tree_equal (ot, nt);
9254 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9255 gcc_unreachable ();
9256 else if (TYPE_P (nt) || TYPE_P (ot))
9257 {
9258 if (!(TYPE_P (nt) && TYPE_P (ot)))
9259 return false;
9260 return same_type_p (ot, nt);
9261 }
9262 else
9263 {
9264 /* Try to treat a template non-type argument that has been converted
9265 to the parameter type as equivalent to one that hasn't yet. */
9266 for (enum tree_code code1 = TREE_CODE (ot);
9267 CONVERT_EXPR_CODE_P (code1)
9268 || code1 == NON_LVALUE_EXPR;
9269 code1 = TREE_CODE (ot))
9270 ot = TREE_OPERAND (ot, 0);
9271
9272 for (enum tree_code code2 = TREE_CODE (nt);
9273 CONVERT_EXPR_CODE_P (code2)
9274 || code2 == NON_LVALUE_EXPR;
9275 code2 = TREE_CODE (nt))
9276 nt = TREE_OPERAND (nt, 0);
9277
9278 return cp_tree_equal (ot, nt);
9279 }
9280 }
9281
9282 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9283 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9284 NEWARG_PTR with the offending arguments if they are non-NULL. */
9285
9286 int
9287 comp_template_args (tree oldargs, tree newargs,
9288 tree *oldarg_ptr, tree *newarg_ptr,
9289 bool partial_order)
9290 {
9291 int i;
9292
9293 if (oldargs == newargs)
9294 return 1;
9295
9296 if (!oldargs || !newargs)
9297 return 0;
9298
9299 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9300 return 0;
9301
9302 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9303 {
9304 tree nt = TREE_VEC_ELT (newargs, i);
9305 tree ot = TREE_VEC_ELT (oldargs, i);
9306
9307 if (! template_args_equal (ot, nt, partial_order))
9308 {
9309 if (oldarg_ptr != NULL)
9310 *oldarg_ptr = ot;
9311 if (newarg_ptr != NULL)
9312 *newarg_ptr = nt;
9313 return 0;
9314 }
9315 }
9316 return 1;
9317 }
9318
9319 inline bool
9320 comp_template_args_porder (tree oargs, tree nargs)
9321 {
9322 return comp_template_args (oargs, nargs, NULL, NULL, true);
9323 }
9324
9325 /* Implement a freelist interface for objects of type T.
9326
9327 Head is a separate object, rather than a regular member, so that we
9328 can define it as a GTY deletable pointer, which is highly
9329 desirable. A data member could be declared that way, but then the
9330 containing object would implicitly get GTY((user)), which would
9331 prevent us from instantiating freelists as global objects.
9332 Although this way we can create freelist global objects, they're
9333 such thin wrappers that instantiating temporaries at every use
9334 loses nothing and saves permanent storage for the freelist object.
9335
9336 Member functions next, anew, poison and reinit have default
9337 implementations that work for most of the types we're interested
9338 in, but if they don't work for some type, they should be explicitly
9339 specialized. See the comments before them for requirements, and
9340 the example specializations for the tree_list_freelist. */
9341 template <typename T>
9342 class freelist
9343 {
9344 /* Return the next object in a chain. We could just do type
9345 punning, but if we access the object with its underlying type, we
9346 avoid strict-aliasing trouble. This needs only work between
9347 poison and reinit. */
9348 static T *&next (T *obj) { return obj->next; }
9349
9350 /* Return a newly allocated, uninitialized or minimally-initialized
9351 object of type T. Any initialization performed by anew should
9352 either remain across the life of the object and the execution of
9353 poison, or be redone by reinit. */
9354 static T *anew () { return ggc_alloc<T> (); }
9355
9356 /* Optionally scribble all over the bits holding the object, so that
9357 they become (mostly?) uninitialized memory. This is called while
9358 preparing to make the object part of the free list. */
9359 static void poison (T *obj) {
9360 T *p ATTRIBUTE_UNUSED = obj;
9361 T **q ATTRIBUTE_UNUSED = &next (obj);
9362
9363 #ifdef ENABLE_GC_CHECKING
9364 /* Poison the data, to indicate the data is garbage. */
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9366 memset (p, 0xa5, sizeof (*p));
9367 #endif
9368 /* Let valgrind know the object is free. */
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9370
9371 /* Let valgrind know the next portion of the object is available,
9372 but uninitialized. */
9373 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9374 }
9375
9376 /* Bring an object that underwent at least one lifecycle after anew
9377 and before the most recent free and poison, back to a usable
9378 state, reinitializing whatever is needed for it to be
9379 functionally equivalent to an object just allocated and returned
9380 by anew. This may poison or clear the next field, used by
9381 freelist housekeeping after poison was called. */
9382 static void reinit (T *obj) {
9383 T **q ATTRIBUTE_UNUSED = &next (obj);
9384
9385 #ifdef ENABLE_GC_CHECKING
9386 memset (q, 0xa5, sizeof (*q));
9387 #endif
9388 /* Let valgrind know the entire object is available, but
9389 uninitialized. */
9390 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9391 }
9392
9393 /* Reference a GTY-deletable pointer that points to the first object
9394 in the free list proper. */
9395 T *&head;
9396 public:
9397 /* Construct a freelist object chaining objects off of HEAD. */
9398 freelist (T *&head) : head(head) {}
9399
9400 /* Add OBJ to the free object list. The former head becomes OBJ's
9401 successor. */
9402 void free (T *obj)
9403 {
9404 poison (obj);
9405 next (obj) = head;
9406 head = obj;
9407 }
9408
9409 /* Take an object from the free list, if one is available, or
9410 allocate a new one. Objects taken from the free list should be
9411 regarded as filled with garbage, except for bits that are
9412 configured to be preserved across free and alloc. */
9413 T *alloc ()
9414 {
9415 if (head)
9416 {
9417 T *obj = head;
9418 head = next (head);
9419 reinit (obj);
9420 return obj;
9421 }
9422 else
9423 return anew ();
9424 }
9425 };
9426
9427 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9428 want to allocate a TREE_LIST using the usual interface, and ensure
9429 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9430 build_tree_list logic in reinit, so this could go out of sync. */
9431 template <>
9432 inline tree &
9433 freelist<tree_node>::next (tree obj)
9434 {
9435 return TREE_CHAIN (obj);
9436 }
9437 template <>
9438 inline tree
9439 freelist<tree_node>::anew ()
9440 {
9441 return build_tree_list (NULL, NULL);
9442 }
9443 template <>
9444 inline void
9445 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9446 {
9447 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9448 tree p ATTRIBUTE_UNUSED = obj;
9449 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9450 tree *q ATTRIBUTE_UNUSED = &next (obj);
9451
9452 #ifdef ENABLE_GC_CHECKING
9453 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9454
9455 /* Poison the data, to indicate the data is garbage. */
9456 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9457 memset (p, 0xa5, size);
9458 #endif
9459 /* Let valgrind know the object is free. */
9460 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9461 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9462 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9463 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9464
9465 #ifdef ENABLE_GC_CHECKING
9466 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9467 /* Keep TREE_CHAIN functional. */
9468 TREE_SET_CODE (obj, TREE_LIST);
9469 #else
9470 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9471 #endif
9472 }
9473 template <>
9474 inline void
9475 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9476 {
9477 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9478
9479 #ifdef ENABLE_GC_CHECKING
9480 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9481 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9482 memset (obj, 0, sizeof (tree_list));
9483 #endif
9484
9485 /* Let valgrind know the entire object is available, but
9486 uninitialized. */
9487 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9488
9489 #ifdef ENABLE_GC_CHECKING
9490 TREE_SET_CODE (obj, TREE_LIST);
9491 #else
9492 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9493 #endif
9494 }
9495
9496 /* Point to the first object in the TREE_LIST freelist. */
9497 static GTY((deletable)) tree tree_list_freelist_head;
9498 /* Return the/an actual TREE_LIST freelist. */
9499 static inline freelist<tree_node>
9500 tree_list_freelist ()
9501 {
9502 return tree_list_freelist_head;
9503 }
9504
9505 /* Point to the first object in the tinst_level freelist. */
9506 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9507 /* Return the/an actual tinst_level freelist. */
9508 static inline freelist<tinst_level>
9509 tinst_level_freelist ()
9510 {
9511 return tinst_level_freelist_head;
9512 }
9513
9514 /* Point to the first object in the pending_template freelist. */
9515 static GTY((deletable)) pending_template *pending_template_freelist_head;
9516 /* Return the/an actual pending_template freelist. */
9517 static inline freelist<pending_template>
9518 pending_template_freelist ()
9519 {
9520 return pending_template_freelist_head;
9521 }
9522
9523 /* Build the TREE_LIST object out of a split list, store it
9524 permanently, and return it. */
9525 tree
9526 tinst_level::to_list ()
9527 {
9528 gcc_assert (split_list_p ());
9529 tree ret = tree_list_freelist ().alloc ();
9530 TREE_PURPOSE (ret) = tldcl;
9531 TREE_VALUE (ret) = targs;
9532 tldcl = ret;
9533 targs = NULL;
9534 gcc_assert (tree_list_p ());
9535 return ret;
9536 }
9537
9538 const unsigned short tinst_level::refcount_infinity;
9539
9540 /* Increment OBJ's refcount unless it is already infinite. */
9541 static tinst_level *
9542 inc_refcount_use (tinst_level *obj)
9543 {
9544 if (obj && obj->refcount != tinst_level::refcount_infinity)
9545 ++obj->refcount;
9546 return obj;
9547 }
9548
9549 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9550 void
9551 tinst_level::free (tinst_level *obj)
9552 {
9553 if (obj->tree_list_p ())
9554 tree_list_freelist ().free (obj->get_node ());
9555 tinst_level_freelist ().free (obj);
9556 }
9557
9558 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9559 OBJ's DECL and OBJ, and start over with the tinst_level object that
9560 used to be referenced by OBJ's NEXT. */
9561 static void
9562 dec_refcount_use (tinst_level *obj)
9563 {
9564 while (obj
9565 && obj->refcount != tinst_level::refcount_infinity
9566 && !--obj->refcount)
9567 {
9568 tinst_level *next = obj->next;
9569 tinst_level::free (obj);
9570 obj = next;
9571 }
9572 }
9573
9574 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9575 and of the former PTR. Omitting the second argument is equivalent
9576 to passing (T*)NULL; this is allowed because passing the
9577 zero-valued integral constant NULL confuses type deduction and/or
9578 overload resolution. */
9579 template <typename T>
9580 static void
9581 set_refcount_ptr (T *& ptr, T *obj = NULL)
9582 {
9583 T *save = ptr;
9584 ptr = inc_refcount_use (obj);
9585 dec_refcount_use (save);
9586 }
9587
9588 static void
9589 add_pending_template (tree d)
9590 {
9591 tree ti = (TYPE_P (d)
9592 ? CLASSTYPE_TEMPLATE_INFO (d)
9593 : DECL_TEMPLATE_INFO (d));
9594 struct pending_template *pt;
9595 int level;
9596
9597 if (TI_PENDING_TEMPLATE_FLAG (ti))
9598 return;
9599
9600 /* We are called both from instantiate_decl, where we've already had a
9601 tinst_level pushed, and instantiate_template, where we haven't.
9602 Compensate. */
9603 gcc_assert (TREE_CODE (d) != TREE_LIST);
9604 level = !current_tinst_level
9605 || current_tinst_level->maybe_get_node () != d;
9606
9607 if (level)
9608 push_tinst_level (d);
9609
9610 pt = pending_template_freelist ().alloc ();
9611 pt->next = NULL;
9612 pt->tinst = NULL;
9613 set_refcount_ptr (pt->tinst, current_tinst_level);
9614 if (last_pending_template)
9615 last_pending_template->next = pt;
9616 else
9617 pending_templates = pt;
9618
9619 last_pending_template = pt;
9620
9621 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9622
9623 if (level)
9624 pop_tinst_level ();
9625 }
9626
9627
9628 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9629 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9630 documentation for TEMPLATE_ID_EXPR. */
9631
9632 tree
9633 lookup_template_function (tree fns, tree arglist)
9634 {
9635 if (fns == error_mark_node || arglist == error_mark_node)
9636 return error_mark_node;
9637
9638 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9639
9640 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9641 {
9642 error ("%q#D is not a function template", fns);
9643 return error_mark_node;
9644 }
9645
9646 if (BASELINK_P (fns))
9647 {
9648 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9649 unknown_type_node,
9650 BASELINK_FUNCTIONS (fns),
9651 arglist);
9652 return fns;
9653 }
9654
9655 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9656 }
9657
9658 /* Within the scope of a template class S<T>, the name S gets bound
9659 (in build_self_reference) to a TYPE_DECL for the class, not a
9660 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9661 or one of its enclosing classes, and that type is a template,
9662 return the associated TEMPLATE_DECL. Otherwise, the original
9663 DECL is returned.
9664
9665 Also handle the case when DECL is a TREE_LIST of ambiguous
9666 injected-class-names from different bases. */
9667
9668 tree
9669 maybe_get_template_decl_from_type_decl (tree decl)
9670 {
9671 if (decl == NULL_TREE)
9672 return decl;
9673
9674 /* DR 176: A lookup that finds an injected-class-name (10.2
9675 [class.member.lookup]) can result in an ambiguity in certain cases
9676 (for example, if it is found in more than one base class). If all of
9677 the injected-class-names that are found refer to specializations of
9678 the same class template, and if the name is followed by a
9679 template-argument-list, the reference refers to the class template
9680 itself and not a specialization thereof, and is not ambiguous. */
9681 if (TREE_CODE (decl) == TREE_LIST)
9682 {
9683 tree t, tmpl = NULL_TREE;
9684 for (t = decl; t; t = TREE_CHAIN (t))
9685 {
9686 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9687 if (!tmpl)
9688 tmpl = elt;
9689 else if (tmpl != elt)
9690 break;
9691 }
9692 if (tmpl && t == NULL_TREE)
9693 return tmpl;
9694 else
9695 return decl;
9696 }
9697
9698 return (decl != NULL_TREE
9699 && DECL_SELF_REFERENCE_P (decl)
9700 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9701 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9702 }
9703
9704 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9705 parameters, find the desired type.
9706
9707 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9708
9709 IN_DECL, if non-NULL, is the template declaration we are trying to
9710 instantiate.
9711
9712 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9713 the class we are looking up.
9714
9715 Issue error and warning messages under control of COMPLAIN.
9716
9717 If the template class is really a local class in a template
9718 function, then the FUNCTION_CONTEXT is the function in which it is
9719 being instantiated.
9720
9721 ??? Note that this function is currently called *twice* for each
9722 template-id: the first time from the parser, while creating the
9723 incomplete type (finish_template_type), and the second type during the
9724 real instantiation (instantiate_template_class). This is surely something
9725 that we want to avoid. It also causes some problems with argument
9726 coercion (see convert_nontype_argument for more information on this). */
9727
9728 static tree
9729 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9730 int entering_scope, tsubst_flags_t complain)
9731 {
9732 tree templ = NULL_TREE, parmlist;
9733 tree t;
9734 spec_entry **slot;
9735 spec_entry *entry;
9736 spec_entry elt;
9737 hashval_t hash;
9738
9739 if (identifier_p (d1))
9740 {
9741 tree value = innermost_non_namespace_value (d1);
9742 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9743 templ = value;
9744 else
9745 {
9746 if (context)
9747 push_decl_namespace (context);
9748 templ = lookup_name (d1);
9749 templ = maybe_get_template_decl_from_type_decl (templ);
9750 if (context)
9751 pop_decl_namespace ();
9752 }
9753 if (templ)
9754 context = DECL_CONTEXT (templ);
9755 }
9756 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9757 {
9758 tree type = TREE_TYPE (d1);
9759
9760 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9761 an implicit typename for the second A. Deal with it. */
9762 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9763 type = TREE_TYPE (type);
9764
9765 if (CLASSTYPE_TEMPLATE_INFO (type))
9766 {
9767 templ = CLASSTYPE_TI_TEMPLATE (type);
9768 d1 = DECL_NAME (templ);
9769 }
9770 }
9771 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9772 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9773 {
9774 templ = TYPE_TI_TEMPLATE (d1);
9775 d1 = DECL_NAME (templ);
9776 }
9777 else if (DECL_TYPE_TEMPLATE_P (d1))
9778 {
9779 templ = d1;
9780 d1 = DECL_NAME (templ);
9781 context = DECL_CONTEXT (templ);
9782 }
9783 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9784 {
9785 templ = d1;
9786 d1 = DECL_NAME (templ);
9787 }
9788
9789 /* Issue an error message if we didn't find a template. */
9790 if (! templ)
9791 {
9792 if (complain & tf_error)
9793 error ("%qT is not a template", d1);
9794 return error_mark_node;
9795 }
9796
9797 if (TREE_CODE (templ) != TEMPLATE_DECL
9798 /* Make sure it's a user visible template, if it was named by
9799 the user. */
9800 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9801 && !PRIMARY_TEMPLATE_P (templ)))
9802 {
9803 if (complain & tf_error)
9804 {
9805 error ("non-template type %qT used as a template", d1);
9806 if (in_decl)
9807 error ("for template declaration %q+D", in_decl);
9808 }
9809 return error_mark_node;
9810 }
9811
9812 complain &= ~tf_user;
9813
9814 /* An alias that just changes the name of a template is equivalent to the
9815 other template, so if any of the arguments are pack expansions, strip
9816 the alias to avoid problems with a pack expansion passed to a non-pack
9817 alias template parameter (DR 1430). */
9818 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9819 templ = get_underlying_template (templ);
9820
9821 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9822 {
9823 tree parm;
9824 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9825 if (arglist2 == error_mark_node
9826 || (!uses_template_parms (arglist2)
9827 && check_instantiated_args (templ, arglist2, complain)))
9828 return error_mark_node;
9829
9830 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9831 return parm;
9832 }
9833 else
9834 {
9835 tree template_type = TREE_TYPE (templ);
9836 tree gen_tmpl;
9837 tree type_decl;
9838 tree found = NULL_TREE;
9839 int arg_depth;
9840 int parm_depth;
9841 int is_dependent_type;
9842 int use_partial_inst_tmpl = false;
9843
9844 if (template_type == error_mark_node)
9845 /* An error occurred while building the template TEMPL, and a
9846 diagnostic has most certainly been emitted for that
9847 already. Let's propagate that error. */
9848 return error_mark_node;
9849
9850 gen_tmpl = most_general_template (templ);
9851 if (modules_p ())
9852 lazy_load_pendings (gen_tmpl);
9853
9854 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9855 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9856 arg_depth = TMPL_ARGS_DEPTH (arglist);
9857
9858 if (arg_depth == 1 && parm_depth > 1)
9859 {
9860 /* We've been given an incomplete set of template arguments.
9861 For example, given:
9862
9863 template <class T> struct S1 {
9864 template <class U> struct S2 {};
9865 template <class U> struct S2<U*> {};
9866 };
9867
9868 we will be called with an ARGLIST of `U*', but the
9869 TEMPLATE will be `template <class T> template
9870 <class U> struct S1<T>::S2'. We must fill in the missing
9871 arguments. */
9872 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9873 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9874 arg_depth = TMPL_ARGS_DEPTH (arglist);
9875 }
9876
9877 /* Now we should have enough arguments. */
9878 gcc_assert (parm_depth == arg_depth);
9879
9880 /* From here on, we're only interested in the most general
9881 template. */
9882
9883 /* Calculate the BOUND_ARGS. These will be the args that are
9884 actually tsubst'd into the definition to create the
9885 instantiation. */
9886 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9887 complain,
9888 /*require_all_args=*/true,
9889 /*use_default_args=*/true);
9890
9891 if (arglist == error_mark_node)
9892 /* We were unable to bind the arguments. */
9893 return error_mark_node;
9894
9895 /* In the scope of a template class, explicit references to the
9896 template class refer to the type of the template, not any
9897 instantiation of it. For example, in:
9898
9899 template <class T> class C { void f(C<T>); }
9900
9901 the `C<T>' is just the same as `C'. Outside of the
9902 class, however, such a reference is an instantiation. */
9903 if (entering_scope
9904 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9905 || currently_open_class (template_type))
9906 {
9907 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9908
9909 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9910 return template_type;
9911 }
9912
9913 /* If we already have this specialization, return it. */
9914 elt.tmpl = gen_tmpl;
9915 elt.args = arglist;
9916 elt.spec = NULL_TREE;
9917 hash = spec_hasher::hash (&elt);
9918 entry = type_specializations->find_with_hash (&elt, hash);
9919
9920 if (entry)
9921 return entry->spec;
9922
9923 /* If the template's constraints are not satisfied,
9924 then we cannot form a valid type.
9925
9926 Note that the check is deferred until after the hash
9927 lookup. This prevents redundant checks on previously
9928 instantiated specializations. */
9929 if (flag_concepts
9930 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9931 && !constraints_satisfied_p (gen_tmpl, arglist))
9932 {
9933 if (complain & tf_error)
9934 {
9935 auto_diagnostic_group d;
9936 error ("template constraint failure for %qD", gen_tmpl);
9937 diagnose_constraints (input_location, gen_tmpl, arglist);
9938 }
9939 return error_mark_node;
9940 }
9941
9942 is_dependent_type = uses_template_parms (arglist);
9943
9944 /* If the deduced arguments are invalid, then the binding
9945 failed. */
9946 if (!is_dependent_type
9947 && check_instantiated_args (gen_tmpl,
9948 INNERMOST_TEMPLATE_ARGS (arglist),
9949 complain))
9950 return error_mark_node;
9951
9952 if (!is_dependent_type
9953 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9954 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9955 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9956 /* This occurs when the user has tried to define a tagged type
9957 in a scope that forbids it. We emitted an error during the
9958 parse. We didn't complete the bail out then, so here we
9959 are. */
9960 return error_mark_node;
9961
9962 context = DECL_CONTEXT (gen_tmpl);
9963 if (context && TYPE_P (context))
9964 {
9965 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9966 context = complete_type (context);
9967 }
9968 else
9969 context = tsubst (context, arglist, complain, in_decl);
9970
9971 if (context == error_mark_node)
9972 return error_mark_node;
9973
9974 if (!context)
9975 context = global_namespace;
9976
9977 /* Create the type. */
9978 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9979 {
9980 /* The user referred to a specialization of an alias
9981 template represented by GEN_TMPL.
9982
9983 [temp.alias]/2 says:
9984
9985 When a template-id refers to the specialization of an
9986 alias template, it is equivalent to the associated
9987 type obtained by substitution of its
9988 template-arguments for the template-parameters in the
9989 type-id of the alias template. */
9990
9991 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9992 /* Note that the call above (by indirectly calling
9993 register_specialization in tsubst_decl) registers the
9994 TYPE_DECL representing the specialization of the alias
9995 template. So next time someone substitutes ARGLIST for
9996 the template parms into the alias template (GEN_TMPL),
9997 she'll get that TYPE_DECL back. */
9998
9999 if (t == error_mark_node)
10000 return t;
10001 }
10002 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
10003 {
10004 if (!is_dependent_type)
10005 {
10006 set_current_access_from_decl (TYPE_NAME (template_type));
10007 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
10008 tsubst (ENUM_UNDERLYING_TYPE (template_type),
10009 arglist, complain, in_decl),
10010 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
10011 arglist, complain, in_decl),
10012 SCOPED_ENUM_P (template_type), NULL);
10013
10014 if (t == error_mark_node)
10015 return t;
10016 }
10017 else
10018 {
10019 /* We don't want to call start_enum for this type, since
10020 the values for the enumeration constants may involve
10021 template parameters. And, no one should be interested
10022 in the enumeration constants for such a type. */
10023 t = cxx_make_type (ENUMERAL_TYPE);
10024 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
10025 }
10026 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
10027 ENUM_FIXED_UNDERLYING_TYPE_P (t)
10028 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
10029 }
10030 else if (CLASS_TYPE_P (template_type))
10031 {
10032 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10033 instantiated here. */
10034 gcc_assert (!LAMBDA_TYPE_P (template_type));
10035
10036 t = make_class_type (TREE_CODE (template_type));
10037 CLASSTYPE_DECLARED_CLASS (t)
10038 = CLASSTYPE_DECLARED_CLASS (template_type);
10039 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10040
10041 /* A local class. Make sure the decl gets registered properly. */
10042 if (context == current_function_decl)
10043 if (pushtag (DECL_NAME (gen_tmpl), t)
10044 == error_mark_node)
10045 return error_mark_node;
10046
10047 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10048 /* This instantiation is another name for the primary
10049 template type. Set the TYPE_CANONICAL field
10050 appropriately. */
10051 TYPE_CANONICAL (t) = template_type;
10052 else if (any_template_arguments_need_structural_equality_p (arglist))
10053 /* Some of the template arguments require structural
10054 equality testing, so this template class requires
10055 structural equality testing. */
10056 SET_TYPE_STRUCTURAL_EQUALITY (t);
10057 }
10058 else
10059 gcc_unreachable ();
10060
10061 /* If we called start_enum or pushtag above, this information
10062 will already be set up. */
10063 type_decl = TYPE_NAME (t);
10064 if (!type_decl)
10065 {
10066 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10067
10068 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10069 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10070 DECL_SOURCE_LOCATION (type_decl)
10071 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10072 }
10073
10074 set_instantiating_module (type_decl);
10075 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10076 of export flag. We want to propagate this because it might
10077 be a friend declaration that pushes a new hidden binding. */
10078 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10079
10080 if (CLASS_TYPE_P (template_type))
10081 {
10082 TREE_PRIVATE (type_decl)
10083 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10084 TREE_PROTECTED (type_decl)
10085 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10086 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10087 {
10088 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10089 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10090 }
10091 }
10092
10093 if (OVERLOAD_TYPE_P (t)
10094 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10095 {
10096 static const char *tags[] = {"abi_tag", "may_alias"};
10097
10098 for (unsigned ix = 0; ix != 2; ix++)
10099 {
10100 tree attributes
10101 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10102
10103 if (attributes)
10104 TYPE_ATTRIBUTES (t)
10105 = tree_cons (TREE_PURPOSE (attributes),
10106 TREE_VALUE (attributes),
10107 TYPE_ATTRIBUTES (t));
10108 }
10109 }
10110
10111 /* Let's consider the explicit specialization of a member
10112 of a class template specialization that is implicitly instantiated,
10113 e.g.:
10114 template<class T>
10115 struct S
10116 {
10117 template<class U> struct M {}; //#0
10118 };
10119
10120 template<>
10121 template<>
10122 struct S<int>::M<char> //#1
10123 {
10124 int i;
10125 };
10126 [temp.expl.spec]/4 says this is valid.
10127
10128 In this case, when we write:
10129 S<int>::M<char> m;
10130
10131 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10132 the one of #0.
10133
10134 When we encounter #1, we want to store the partial instantiation
10135 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10136
10137 For all cases other than this "explicit specialization of member of a
10138 class template", we just want to store the most general template into
10139 the CLASSTYPE_TI_TEMPLATE of M.
10140
10141 This case of "explicit specialization of member of a class template"
10142 only happens when:
10143 1/ the enclosing class is an instantiation of, and therefore not
10144 the same as, the context of the most general template, and
10145 2/ we aren't looking at the partial instantiation itself, i.e.
10146 the innermost arguments are not the same as the innermost parms of
10147 the most general template.
10148
10149 So it's only when 1/ and 2/ happens that we want to use the partial
10150 instantiation of the member template in lieu of its most general
10151 template. */
10152
10153 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10154 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10155 /* the enclosing class must be an instantiation... */
10156 && CLASS_TYPE_P (context)
10157 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10158 {
10159 TREE_VEC_LENGTH (arglist)--;
10160 ++processing_template_decl;
10161 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10162 tree partial_inst_args =
10163 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10164 arglist, complain, NULL_TREE);
10165 --processing_template_decl;
10166 TREE_VEC_LENGTH (arglist)++;
10167 if (partial_inst_args == error_mark_node)
10168 return error_mark_node;
10169 use_partial_inst_tmpl =
10170 /*...and we must not be looking at the partial instantiation
10171 itself. */
10172 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10173 partial_inst_args);
10174 }
10175
10176 if (!use_partial_inst_tmpl)
10177 /* This case is easy; there are no member templates involved. */
10178 found = gen_tmpl;
10179 else
10180 {
10181 /* This is a full instantiation of a member template. Find
10182 the partial instantiation of which this is an instance. */
10183
10184 /* Temporarily reduce by one the number of levels in the ARGLIST
10185 so as to avoid comparing the last set of arguments. */
10186 TREE_VEC_LENGTH (arglist)--;
10187 /* We don't use COMPLAIN in the following call because this isn't
10188 the immediate context of deduction. For instance, tf_partial
10189 could be set here as we might be at the beginning of template
10190 argument deduction when any explicitly specified template
10191 arguments are substituted into the function type. tf_partial
10192 could lead into trouble because we wouldn't find the partial
10193 instantiation that might have been created outside tf_partial
10194 context, because the levels of template parameters wouldn't
10195 match, because in a tf_partial context, tsubst doesn't reduce
10196 TEMPLATE_PARM_LEVEL. */
10197 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10198 TREE_VEC_LENGTH (arglist)++;
10199 /* FOUND is either a proper class type, or an alias
10200 template specialization. In the later case, it's a
10201 TYPE_DECL, resulting from the substituting of arguments
10202 for parameters in the TYPE_DECL of the alias template
10203 done earlier. So be careful while getting the template
10204 of FOUND. */
10205 found = (TREE_CODE (found) == TEMPLATE_DECL
10206 ? found
10207 : (TREE_CODE (found) == TYPE_DECL
10208 ? DECL_TI_TEMPLATE (found)
10209 : CLASSTYPE_TI_TEMPLATE (found)));
10210
10211 if (DECL_CLASS_TEMPLATE_P (found)
10212 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10213 {
10214 /* If this partial instantiation is specialized, we want to
10215 use it for hash table lookup. */
10216 elt.tmpl = found;
10217 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10218 hash = spec_hasher::hash (&elt);
10219 }
10220 }
10221
10222 /* Build template info for the new specialization. */
10223 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10224
10225 elt.spec = t;
10226 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10227 gcc_checking_assert (*slot == NULL);
10228 entry = ggc_alloc<spec_entry> ();
10229 *entry = elt;
10230 *slot = entry;
10231
10232 /* Note this use of the partial instantiation so we can check it
10233 later in maybe_process_partial_specialization. */
10234 DECL_TEMPLATE_INSTANTIATIONS (found)
10235 = tree_cons (arglist, t,
10236 DECL_TEMPLATE_INSTANTIATIONS (found));
10237
10238 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10239 && !uses_template_parms (current_nonlambda_scope ())
10240 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10241 /* Now that the type has been registered on the instantiations
10242 list, we set up the enumerators. Because the enumeration
10243 constants may involve the enumeration type itself, we make
10244 sure to register the type first, and then create the
10245 constants. That way, doing tsubst_expr for the enumeration
10246 constants won't result in recursive calls here; we'll find
10247 the instantiation and exit above. */
10248 tsubst_enum (template_type, t, arglist);
10249
10250 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10251 /* If the type makes use of template parameters, the
10252 code that generates debugging information will crash. */
10253 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10254
10255 /* Possibly limit visibility based on template args. */
10256 TREE_PUBLIC (type_decl) = 1;
10257 determine_visibility (type_decl);
10258
10259 inherit_targ_abi_tags (t);
10260
10261 return t;
10262 }
10263 }
10264
10265 /* Wrapper for lookup_template_class_1. */
10266
10267 tree
10268 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10269 int entering_scope, tsubst_flags_t complain)
10270 {
10271 tree ret;
10272 timevar_push (TV_TEMPLATE_INST);
10273 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10274 entering_scope, complain);
10275 timevar_pop (TV_TEMPLATE_INST);
10276 return ret;
10277 }
10278
10279 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10280
10281 tree
10282 lookup_template_variable (tree templ, tree arglist)
10283 {
10284 if (flag_concepts && variable_concept_p (templ))
10285 return build_concept_check (templ, arglist, tf_none);
10286
10287 /* The type of the expression is NULL_TREE since the template-id could refer
10288 to an explicit or partial specialization. */
10289 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10290 }
10291
10292 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10293
10294 tree
10295 finish_template_variable (tree var, tsubst_flags_t complain)
10296 {
10297 tree templ = TREE_OPERAND (var, 0);
10298 tree arglist = TREE_OPERAND (var, 1);
10299
10300 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10301 arglist = add_outermost_template_args (tmpl_args, arglist);
10302
10303 templ = most_general_template (templ);
10304 tree parms = DECL_TEMPLATE_PARMS (templ);
10305 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10306 /*req_all*/true,
10307 /*use_default*/true);
10308 if (arglist == error_mark_node)
10309 return error_mark_node;
10310
10311 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10312 {
10313 if (complain & tf_error)
10314 {
10315 auto_diagnostic_group d;
10316 error ("use of invalid variable template %qE", var);
10317 diagnose_constraints (location_of (var), templ, arglist);
10318 }
10319 return error_mark_node;
10320 }
10321
10322 return instantiate_template (templ, arglist, complain);
10323 }
10324
10325 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10326 TARGS template args, and instantiate it if it's not dependent. */
10327
10328 tree
10329 lookup_and_finish_template_variable (tree templ, tree targs,
10330 tsubst_flags_t complain)
10331 {
10332 templ = lookup_template_variable (templ, targs);
10333 if (!any_dependent_template_arguments_p (targs))
10334 {
10335 templ = finish_template_variable (templ, complain);
10336 mark_used (templ);
10337 }
10338
10339 return convert_from_reference (templ);
10340 }
10341
10342 /* If the set of template parameters PARMS contains a template parameter
10343 at the given LEVEL and INDEX, then return this parameter. Otherwise
10344 return NULL_TREE. */
10345
10346 static tree
10347 corresponding_template_parameter (tree parms, int level, int index)
10348 {
10349 while (TMPL_PARMS_DEPTH (parms) > level)
10350 parms = TREE_CHAIN (parms);
10351
10352 if (TMPL_PARMS_DEPTH (parms) != level
10353 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10354 return NULL_TREE;
10355
10356 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10357 /* As in template_parm_to_arg. */
10358 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10359 t = TREE_TYPE (t);
10360 else
10361 t = DECL_INITIAL (t);
10362
10363 gcc_assert (TEMPLATE_PARM_P (t));
10364 return t;
10365 }
10366
10367 /* Return the template parameter from PARMS that positionally corresponds
10368 to the template parameter PARM, or else return NULL_TREE. */
10369
10370 static tree
10371 corresponding_template_parameter (tree parms, tree parm)
10372 {
10373 int level, index;
10374 template_parm_level_and_index (parm, &level, &index);
10375 return corresponding_template_parameter (parms, level, index);
10376 }
10377
10378 \f
10379 struct pair_fn_data
10380 {
10381 tree_fn_t fn;
10382 tree_fn_t any_fn;
10383 void *data;
10384 /* True when we should also visit template parameters that occur in
10385 non-deduced contexts. */
10386 bool include_nondeduced_p;
10387 hash_set<tree> *visited;
10388 };
10389
10390 /* Called from for_each_template_parm via walk_tree. */
10391
10392 static tree
10393 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10394 {
10395 tree t = *tp;
10396 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10397 tree_fn_t fn = pfd->fn;
10398 void *data = pfd->data;
10399 tree result = NULL_TREE;
10400
10401 #define WALK_SUBTREE(NODE) \
10402 do \
10403 { \
10404 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10405 pfd->include_nondeduced_p, \
10406 pfd->any_fn); \
10407 if (result) goto out; \
10408 } \
10409 while (0)
10410
10411 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10412 return t;
10413
10414 if (TYPE_P (t)
10415 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10416 WALK_SUBTREE (TYPE_CONTEXT (t));
10417
10418 switch (TREE_CODE (t))
10419 {
10420 case RECORD_TYPE:
10421 if (TYPE_PTRMEMFUNC_P (t))
10422 break;
10423 /* Fall through. */
10424
10425 case UNION_TYPE:
10426 case ENUMERAL_TYPE:
10427 if (!TYPE_TEMPLATE_INFO (t))
10428 *walk_subtrees = 0;
10429 else
10430 WALK_SUBTREE (TYPE_TI_ARGS (t));
10431 break;
10432
10433 case INTEGER_TYPE:
10434 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10435 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10436 break;
10437
10438 case METHOD_TYPE:
10439 /* Since we're not going to walk subtrees, we have to do this
10440 explicitly here. */
10441 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10442 /* Fall through. */
10443
10444 case FUNCTION_TYPE:
10445 /* Check the return type. */
10446 WALK_SUBTREE (TREE_TYPE (t));
10447
10448 /* Check the parameter types. Since default arguments are not
10449 instantiated until they are needed, the TYPE_ARG_TYPES may
10450 contain expressions that involve template parameters. But,
10451 no-one should be looking at them yet. And, once they're
10452 instantiated, they don't contain template parameters, so
10453 there's no point in looking at them then, either. */
10454 {
10455 tree parm;
10456
10457 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10458 WALK_SUBTREE (TREE_VALUE (parm));
10459
10460 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10461 want walk_tree walking into them itself. */
10462 *walk_subtrees = 0;
10463 }
10464
10465 if (flag_noexcept_type)
10466 {
10467 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10468 if (spec)
10469 WALK_SUBTREE (TREE_PURPOSE (spec));
10470 }
10471 break;
10472
10473 case TYPEOF_TYPE:
10474 case DECLTYPE_TYPE:
10475 case UNDERLYING_TYPE:
10476 if (pfd->include_nondeduced_p
10477 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10478 pfd->visited,
10479 pfd->include_nondeduced_p,
10480 pfd->any_fn))
10481 return error_mark_node;
10482 *walk_subtrees = false;
10483 break;
10484
10485 case FUNCTION_DECL:
10486 case VAR_DECL:
10487 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10488 WALK_SUBTREE (DECL_TI_ARGS (t));
10489 /* Fall through. */
10490
10491 case PARM_DECL:
10492 case CONST_DECL:
10493 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10494 WALK_SUBTREE (DECL_INITIAL (t));
10495 if (DECL_CONTEXT (t)
10496 && pfd->include_nondeduced_p)
10497 WALK_SUBTREE (DECL_CONTEXT (t));
10498 break;
10499
10500 case BOUND_TEMPLATE_TEMPLATE_PARM:
10501 /* Record template parameters such as `T' inside `TT<T>'. */
10502 WALK_SUBTREE (TYPE_TI_ARGS (t));
10503 /* Fall through. */
10504
10505 case TEMPLATE_TEMPLATE_PARM:
10506 case TEMPLATE_TYPE_PARM:
10507 case TEMPLATE_PARM_INDEX:
10508 if (fn && (*fn)(t, data))
10509 return t;
10510 else if (!fn)
10511 return t;
10512 break;
10513
10514 case TEMPLATE_DECL:
10515 /* A template template parameter is encountered. */
10516 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10517 WALK_SUBTREE (TREE_TYPE (t));
10518
10519 /* Already substituted template template parameter */
10520 *walk_subtrees = 0;
10521 break;
10522
10523 case TYPENAME_TYPE:
10524 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10525 partial instantiation. */
10526 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10527 *walk_subtrees = 0;
10528 break;
10529
10530 case CONSTRUCTOR:
10531 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10532 && pfd->include_nondeduced_p)
10533 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10534 break;
10535
10536 case INDIRECT_REF:
10537 case COMPONENT_REF:
10538 /* If there's no type, then this thing must be some expression
10539 involving template parameters. */
10540 if (!fn && !TREE_TYPE (t))
10541 return error_mark_node;
10542 break;
10543
10544 case MODOP_EXPR:
10545 case CAST_EXPR:
10546 case IMPLICIT_CONV_EXPR:
10547 case REINTERPRET_CAST_EXPR:
10548 case CONST_CAST_EXPR:
10549 case STATIC_CAST_EXPR:
10550 case DYNAMIC_CAST_EXPR:
10551 case ARROW_EXPR:
10552 case DOTSTAR_EXPR:
10553 case TYPEID_EXPR:
10554 case PSEUDO_DTOR_EXPR:
10555 if (!fn)
10556 return error_mark_node;
10557 break;
10558
10559 case SCOPE_REF:
10560 if (pfd->include_nondeduced_p)
10561 WALK_SUBTREE (TREE_OPERAND (t, 0));
10562 break;
10563
10564 case REQUIRES_EXPR:
10565 {
10566 if (!fn)
10567 return error_mark_node;
10568
10569 /* Recursively walk the type of each constraint variable. */
10570 tree p = TREE_OPERAND (t, 0);
10571 while (p)
10572 {
10573 WALK_SUBTREE (TREE_TYPE (p));
10574 p = TREE_CHAIN (p);
10575 }
10576 }
10577 break;
10578
10579 default:
10580 break;
10581 }
10582
10583 #undef WALK_SUBTREE
10584
10585 /* We didn't find any template parameters we liked. */
10586 out:
10587 return result;
10588 }
10589
10590 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10591 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10592 call FN with the parameter and the DATA.
10593 If FN returns nonzero, the iteration is terminated, and
10594 for_each_template_parm returns 1. Otherwise, the iteration
10595 continues. If FN never returns a nonzero value, the value
10596 returned by for_each_template_parm is 0. If FN is NULL, it is
10597 considered to be the function which always returns 1.
10598
10599 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10600 parameters that occur in non-deduced contexts. When false, only
10601 visits those template parameters that can be deduced. */
10602
10603 static tree
10604 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10605 hash_set<tree> *visited,
10606 bool include_nondeduced_p,
10607 tree_fn_t any_fn)
10608 {
10609 struct pair_fn_data pfd;
10610 tree result;
10611
10612 /* Set up. */
10613 pfd.fn = fn;
10614 pfd.any_fn = any_fn;
10615 pfd.data = data;
10616 pfd.include_nondeduced_p = include_nondeduced_p;
10617
10618 /* Walk the tree. (Conceptually, we would like to walk without
10619 duplicates, but for_each_template_parm_r recursively calls
10620 for_each_template_parm, so we would need to reorganize a fair
10621 bit to use walk_tree_without_duplicates, so we keep our own
10622 visited list.) */
10623 if (visited)
10624 pfd.visited = visited;
10625 else
10626 pfd.visited = new hash_set<tree>;
10627 result = cp_walk_tree (&t,
10628 for_each_template_parm_r,
10629 &pfd,
10630 pfd.visited);
10631
10632 /* Clean up. */
10633 if (!visited)
10634 {
10635 delete pfd.visited;
10636 pfd.visited = 0;
10637 }
10638
10639 return result;
10640 }
10641
10642 struct find_template_parameter_info
10643 {
10644 explicit find_template_parameter_info (tree ctx_parms)
10645 : parm_list (NULL_TREE),
10646 ctx_parms (ctx_parms),
10647 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10648 {}
10649
10650 hash_set<tree> visited;
10651 hash_set<tree> parms;
10652 tree parm_list;
10653 tree ctx_parms;
10654 int max_depth;
10655 };
10656
10657 /* Appends the declaration of T to the list in DATA. */
10658
10659 static int
10660 keep_template_parm (tree t, void* data)
10661 {
10662 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10663
10664 /* Template parameters declared within the expression are not part of
10665 the parameter mapping. For example, in this concept:
10666
10667 template<typename T>
10668 concept C = requires { <expr> } -> same_as<int>;
10669
10670 the return specifier same_as<int> declares a new decltype parameter
10671 that must not be part of the parameter mapping. The same is true
10672 for generic lambda parameters, lambda template parameters, etc. */
10673 int level;
10674 int index;
10675 template_parm_level_and_index (t, &level, &index);
10676 if (level > ftpi->max_depth)
10677 return 0;
10678
10679 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10680 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10681 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10682 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10683
10684 /* This template parameter might be an argument to a cached dependent
10685 specalization that was formed earlier inside some other template, in
10686 which case the parameter is not among the ones that are in-scope.
10687 Look in CTX_PARMS to find the corresponding in-scope template
10688 parameter, and use it instead. */
10689 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10690 t = in_scope;
10691
10692 /* Arguments like const T yield parameters like const T. This means that
10693 a template-id like X<T, const T> would yield two distinct parameters:
10694 T and const T. Adjust types to their unqualified versions. */
10695 if (TYPE_P (t))
10696 t = TYPE_MAIN_VARIANT (t);
10697 if (!ftpi->parms.add (t))
10698 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10699
10700 /* Verify the parameter we found has a valid index. */
10701 if (flag_checking)
10702 {
10703 tree parms = ftpi->ctx_parms;
10704 while (TMPL_PARMS_DEPTH (parms) > level)
10705 parms = TREE_CHAIN (parms);
10706 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10707 gcc_assert (index < len);
10708 }
10709
10710 return 0;
10711 }
10712
10713 /* Ensure that we recursively examine certain terms that are not normally
10714 visited in for_each_template_parm_r. */
10715
10716 static int
10717 any_template_parm_r (tree t, void *data)
10718 {
10719 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10720
10721 #define WALK_SUBTREE(NODE) \
10722 do \
10723 { \
10724 for_each_template_parm (NODE, keep_template_parm, data, \
10725 &ftpi->visited, true, \
10726 any_template_parm_r); \
10727 } \
10728 while (0)
10729
10730 /* A mention of a member alias/typedef is a use of all of its template
10731 arguments, including those from the enclosing class, so we don't use
10732 alias_template_specialization_p here. */
10733 if (TYPE_P (t) && typedef_variant_p (t))
10734 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10735 WALK_SUBTREE (TI_ARGS (tinfo));
10736
10737 switch (TREE_CODE (t))
10738 {
10739 case TEMPLATE_TYPE_PARM:
10740 /* Type constraints of a placeholder type may contain parameters. */
10741 if (is_auto (t))
10742 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10743 WALK_SUBTREE (constr);
10744 break;
10745
10746 case TEMPLATE_ID_EXPR:
10747 /* Search through references to variable templates. */
10748 WALK_SUBTREE (TREE_OPERAND (t, 0));
10749 WALK_SUBTREE (TREE_OPERAND (t, 1));
10750 break;
10751
10752 case TEMPLATE_PARM_INDEX:
10753 case PARM_DECL:
10754 /* A parameter or constraint variable may also depend on a template
10755 parameter without explicitly naming it. */
10756 WALK_SUBTREE (TREE_TYPE (t));
10757 break;
10758
10759 case TEMPLATE_DECL:
10760 /* If T is a member template that shares template parameters with
10761 ctx_parms, we need to mark all those parameters for mapping.
10762 To that end, it should suffice to just walk the DECL_CONTEXT of
10763 the template (assuming the template is not overly general). */
10764 WALK_SUBTREE (DECL_CONTEXT (t));
10765 break;
10766
10767 case LAMBDA_EXPR:
10768 {
10769 /* Look in the parms and body. */
10770 tree fn = lambda_function (t);
10771 WALK_SUBTREE (TREE_TYPE (fn));
10772 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10773 }
10774 break;
10775
10776 case IDENTIFIER_NODE:
10777 if (IDENTIFIER_CONV_OP_P (t))
10778 /* The conversion-type-id of a conversion operator may be dependent. */
10779 WALK_SUBTREE (TREE_TYPE (t));
10780 break;
10781
10782 case CONVERT_EXPR:
10783 if (is_dummy_object (t))
10784 WALK_SUBTREE (TREE_TYPE (t));
10785 break;
10786
10787 default:
10788 break;
10789 }
10790
10791 /* Keep walking. */
10792 return 0;
10793 }
10794
10795 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10796 are the template parameters in scope. */
10797
10798 tree
10799 find_template_parameters (tree t, tree ctx_parms)
10800 {
10801 if (!ctx_parms)
10802 return NULL_TREE;
10803
10804 find_template_parameter_info ftpi (ctx_parms);
10805 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10806 /*include_nondeduced*/true, any_template_parm_r);
10807 return ftpi.parm_list;
10808 }
10809
10810 /* Returns true if T depends on any template parameter. */
10811
10812 int
10813 uses_template_parms (tree t)
10814 {
10815 if (t == NULL_TREE)
10816 return false;
10817
10818 bool dependent_p;
10819 int saved_processing_template_decl;
10820
10821 saved_processing_template_decl = processing_template_decl;
10822 if (!saved_processing_template_decl)
10823 processing_template_decl = 1;
10824 if (TYPE_P (t))
10825 dependent_p = dependent_type_p (t);
10826 else if (TREE_CODE (t) == TREE_VEC)
10827 dependent_p = any_dependent_template_arguments_p (t);
10828 else if (TREE_CODE (t) == TREE_LIST)
10829 dependent_p = (uses_template_parms (TREE_VALUE (t))
10830 || uses_template_parms (TREE_CHAIN (t)));
10831 else if (TREE_CODE (t) == TYPE_DECL)
10832 dependent_p = dependent_type_p (TREE_TYPE (t));
10833 else if (t == error_mark_node)
10834 dependent_p = false;
10835 else
10836 dependent_p = instantiation_dependent_expression_p (t);
10837
10838 processing_template_decl = saved_processing_template_decl;
10839
10840 return dependent_p;
10841 }
10842
10843 /* Returns true iff we're processing an incompletely instantiated function
10844 template. Useful instead of processing_template_decl because the latter
10845 is set to 0 during instantiate_non_dependent_expr. */
10846
10847 bool
10848 in_template_function (void)
10849 {
10850 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10851 the latter might get set for e.g. access checking during satisfaction. */
10852 tree fn = cfun ? cfun->decl : NULL_TREE;
10853 bool ret;
10854 ++processing_template_decl;
10855 ret = (fn && DECL_LANG_SPECIFIC (fn)
10856 && DECL_TEMPLATE_INFO (fn)
10857 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10858 --processing_template_decl;
10859 return ret;
10860 }
10861
10862 /* Returns true if T depends on any template parameter with level LEVEL. */
10863
10864 bool
10865 uses_template_parms_level (tree t, int level)
10866 {
10867 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10868 /*include_nondeduced_p=*/true);
10869 }
10870
10871 /* Returns true if the signature of DECL depends on any template parameter from
10872 its enclosing class. */
10873
10874 bool
10875 uses_outer_template_parms (tree decl)
10876 {
10877 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10878 if (depth == 0)
10879 return false;
10880 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10881 &depth, NULL, /*include_nondeduced_p=*/true))
10882 return true;
10883 if (PRIMARY_TEMPLATE_P (decl)
10884 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
10885 {
10886 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
10887 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
10888 {
10889 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
10890 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
10891 if (TREE_CODE (parm) == PARM_DECL
10892 && for_each_template_parm (TREE_TYPE (parm),
10893 template_parm_outer_level,
10894 &depth, NULL, /*nondeduced*/true))
10895 return true;
10896 if (TREE_CODE (parm) == TEMPLATE_DECL
10897 && uses_outer_template_parms (parm))
10898 return true;
10899 if (defarg
10900 && for_each_template_parm (defarg, template_parm_outer_level,
10901 &depth, NULL, /*nondeduced*/true))
10902 return true;
10903 }
10904 }
10905 tree ci = get_constraints (decl);
10906 if (ci)
10907 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10908 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10909 &depth, NULL, /*nondeduced*/true))
10910 return true;
10911 return false;
10912 }
10913
10914 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10915 ill-formed translation unit, i.e. a variable or function that isn't
10916 usable in a constant expression. */
10917
10918 static inline bool
10919 neglectable_inst_p (tree d)
10920 {
10921 return (d && DECL_P (d)
10922 && !undeduced_auto_decl (d)
10923 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10924 : decl_maybe_constant_var_p (d)));
10925 }
10926
10927 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10928 neglectable and instantiated from within an erroneous instantiation. */
10929
10930 static bool
10931 limit_bad_template_recursion (tree decl)
10932 {
10933 struct tinst_level *lev = current_tinst_level;
10934 int errs = errorcount + sorrycount;
10935 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10936 return false;
10937
10938 for (; lev; lev = lev->next)
10939 if (neglectable_inst_p (lev->maybe_get_node ()))
10940 break;
10941
10942 return (lev && errs > lev->errors);
10943 }
10944
10945 static int tinst_depth;
10946 extern int max_tinst_depth;
10947 int depth_reached;
10948
10949 static GTY(()) struct tinst_level *last_error_tinst_level;
10950
10951 /* We're starting to instantiate D; record the template instantiation context
10952 at LOC for diagnostics and to restore it later. */
10953
10954 bool
10955 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10956 {
10957 struct tinst_level *new_level;
10958
10959 if (tinst_depth >= max_tinst_depth)
10960 {
10961 /* Tell error.c not to try to instantiate any templates. */
10962 at_eof = 2;
10963 fatal_error (input_location,
10964 "template instantiation depth exceeds maximum of %d"
10965 " (use %<-ftemplate-depth=%> to increase the maximum)",
10966 max_tinst_depth);
10967 return false;
10968 }
10969
10970 /* If the current instantiation caused problems, don't let it instantiate
10971 anything else. Do allow deduction substitution and decls usable in
10972 constant expressions. */
10973 if (!targs && limit_bad_template_recursion (tldcl))
10974 {
10975 /* Avoid no_linkage_errors and unused function warnings for this
10976 decl. */
10977 TREE_NO_WARNING (tldcl) = 1;
10978 return false;
10979 }
10980
10981 /* When not -quiet, dump template instantiations other than functions, since
10982 announce_function will take care of those. */
10983 if (!quiet_flag && !targs
10984 && TREE_CODE (tldcl) != TREE_LIST
10985 && TREE_CODE (tldcl) != FUNCTION_DECL)
10986 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10987
10988 new_level = tinst_level_freelist ().alloc ();
10989 new_level->tldcl = tldcl;
10990 new_level->targs = targs;
10991 new_level->locus = loc;
10992 new_level->errors = errorcount + sorrycount;
10993 new_level->next = NULL;
10994 new_level->refcount = 0;
10995 new_level->path = new_level->visible = nullptr;
10996 set_refcount_ptr (new_level->next, current_tinst_level);
10997 set_refcount_ptr (current_tinst_level, new_level);
10998
10999 ++tinst_depth;
11000 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
11001 depth_reached = tinst_depth;
11002
11003 return true;
11004 }
11005
11006 /* We're starting substitution of TMPL<ARGS>; record the template
11007 substitution context for diagnostics and to restore it later. */
11008
11009 bool
11010 push_tinst_level (tree tmpl, tree args)
11011 {
11012 return push_tinst_level_loc (tmpl, args, input_location);
11013 }
11014
11015 /* We're starting to instantiate D; record INPUT_LOCATION and the
11016 template instantiation context for diagnostics and to restore it
11017 later. */
11018
11019 bool
11020 push_tinst_level (tree d)
11021 {
11022 return push_tinst_level_loc (d, input_location);
11023 }
11024
11025 /* Likewise, but record LOC as the program location. */
11026
11027 bool
11028 push_tinst_level_loc (tree d, location_t loc)
11029 {
11030 gcc_assert (TREE_CODE (d) != TREE_LIST);
11031 return push_tinst_level_loc (d, NULL, loc);
11032 }
11033
11034 /* We're done instantiating this template; return to the instantiation
11035 context. */
11036
11037 void
11038 pop_tinst_level (void)
11039 {
11040 /* Restore the filename and line number stashed away when we started
11041 this instantiation. */
11042 input_location = current_tinst_level->locus;
11043 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
11044 --tinst_depth;
11045 }
11046
11047 /* We're instantiating a deferred template; restore the template
11048 instantiation context in which the instantiation was requested, which
11049 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11050
11051 static tree
11052 reopen_tinst_level (struct tinst_level *level)
11053 {
11054 struct tinst_level *t;
11055
11056 tinst_depth = 0;
11057 for (t = level; t; t = t->next)
11058 ++tinst_depth;
11059
11060 set_refcount_ptr (current_tinst_level, level);
11061 pop_tinst_level ();
11062 if (current_tinst_level)
11063 current_tinst_level->errors = errorcount+sorrycount;
11064 return level->maybe_get_node ();
11065 }
11066
11067 /* Returns the TINST_LEVEL which gives the original instantiation
11068 context. */
11069
11070 struct tinst_level *
11071 outermost_tinst_level (void)
11072 {
11073 struct tinst_level *level = current_tinst_level;
11074 if (level)
11075 while (level->next)
11076 level = level->next;
11077 return level;
11078 }
11079
11080 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11081 vector of template arguments, as for tsubst.
11082
11083 Returns an appropriate tsubst'd friend declaration. */
11084
11085 static tree
11086 tsubst_friend_function (tree decl, tree args)
11087 {
11088 tree new_friend;
11089
11090 if (TREE_CODE (decl) == FUNCTION_DECL
11091 && DECL_TEMPLATE_INSTANTIATION (decl)
11092 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11093 /* This was a friend declared with an explicit template
11094 argument list, e.g.:
11095
11096 friend void f<>(T);
11097
11098 to indicate that f was a template instantiation, not a new
11099 function declaration. Now, we have to figure out what
11100 instantiation of what template. */
11101 {
11102 tree template_id, arglist, fns;
11103 tree new_args;
11104 tree tmpl;
11105 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11106
11107 /* Friend functions are looked up in the containing namespace scope.
11108 We must enter that scope, to avoid finding member functions of the
11109 current class with same name. */
11110 push_nested_namespace (ns);
11111 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11112 tf_warning_or_error, NULL_TREE,
11113 /*integral_constant_expression_p=*/false);
11114 pop_nested_namespace (ns);
11115 arglist = tsubst (DECL_TI_ARGS (decl), args,
11116 tf_warning_or_error, NULL_TREE);
11117 template_id = lookup_template_function (fns, arglist);
11118
11119 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11120 tmpl = determine_specialization (template_id, new_friend,
11121 &new_args,
11122 /*need_member_template=*/0,
11123 TREE_VEC_LENGTH (args),
11124 tsk_none);
11125 return instantiate_template (tmpl, new_args, tf_error);
11126 }
11127
11128 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11129 if (new_friend == error_mark_node)
11130 return error_mark_node;
11131
11132 /* The NEW_FRIEND will look like an instantiation, to the
11133 compiler, but is not an instantiation from the point of view of
11134 the language. For example, we might have had:
11135
11136 template <class T> struct S {
11137 template <class U> friend void f(T, U);
11138 };
11139
11140 Then, in S<int>, template <class U> void f(int, U) is not an
11141 instantiation of anything. */
11142
11143 DECL_USE_TEMPLATE (new_friend) = 0;
11144 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11145 {
11146 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11147 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11148 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11149 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11150
11151 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11152 match in decls_match. */
11153 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11154 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11155 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11156 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11157 }
11158
11159 /* The mangled name for the NEW_FRIEND is incorrect. The function
11160 is not a template instantiation and should not be mangled like
11161 one. Therefore, we forget the mangling here; we'll recompute it
11162 later if we need it. */
11163 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11164 {
11165 SET_DECL_RTL (new_friend, NULL);
11166 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11167 }
11168
11169 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11170 {
11171 tree old_decl;
11172 tree ns;
11173
11174 /* We must save some information from NEW_FRIEND before calling
11175 duplicate decls since that function will free NEW_FRIEND if
11176 possible. */
11177 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11178 tree new_friend_result_template_info = NULL_TREE;
11179 bool new_friend_is_defn =
11180 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11181 (template_for_substitution (new_friend)))
11182 != NULL_TREE);
11183 tree not_tmpl = new_friend;
11184
11185 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11186 {
11187 /* This declaration is a `primary' template. */
11188 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11189
11190 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11191 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11192 }
11193
11194 /* Inside pushdecl_namespace_level, we will push into the
11195 current namespace. However, the friend function should go
11196 into the namespace of the template. */
11197 ns = decl_namespace_context (new_friend);
11198 push_nested_namespace (ns);
11199 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11200 pop_nested_namespace (ns);
11201
11202 if (old_decl == error_mark_node)
11203 return error_mark_node;
11204
11205 if (old_decl != new_friend)
11206 {
11207 /* This new friend declaration matched an existing
11208 declaration. For example, given:
11209
11210 template <class T> void f(T);
11211 template <class U> class C {
11212 template <class T> friend void f(T) {}
11213 };
11214
11215 the friend declaration actually provides the definition
11216 of `f', once C has been instantiated for some type. So,
11217 old_decl will be the out-of-class template declaration,
11218 while new_friend is the in-class definition.
11219
11220 But, if `f' was called before this point, the
11221 instantiation of `f' will have DECL_TI_ARGS corresponding
11222 to `T' but not to `U', references to which might appear
11223 in the definition of `f'. Previously, the most general
11224 template for an instantiation of `f' was the out-of-class
11225 version; now it is the in-class version. Therefore, we
11226 run through all specialization of `f', adding to their
11227 DECL_TI_ARGS appropriately. In particular, they need a
11228 new set of outer arguments, corresponding to the
11229 arguments for this class instantiation.
11230
11231 The same situation can arise with something like this:
11232
11233 friend void f(int);
11234 template <class T> class C {
11235 friend void f(T) {}
11236 };
11237
11238 when `C<int>' is instantiated. Now, `f(int)' is defined
11239 in the class. */
11240
11241 if (!new_friend_is_defn)
11242 /* On the other hand, if the in-class declaration does
11243 *not* provide a definition, then we don't want to alter
11244 existing definitions. We can just leave everything
11245 alone. */
11246 ;
11247 else
11248 {
11249 tree new_template = TI_TEMPLATE (new_friend_template_info);
11250 tree new_args = TI_ARGS (new_friend_template_info);
11251
11252 /* Overwrite whatever template info was there before, if
11253 any, with the new template information pertaining to
11254 the declaration. */
11255 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11256
11257 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11258 {
11259 /* We should have called reregister_specialization in
11260 duplicate_decls. */
11261 gcc_assert (retrieve_specialization (new_template,
11262 new_args, 0)
11263 == old_decl);
11264
11265 /* Instantiate it if the global has already been used. */
11266 if (DECL_ODR_USED (old_decl))
11267 instantiate_decl (old_decl, /*defer_ok=*/true,
11268 /*expl_inst_class_mem_p=*/false);
11269 }
11270 else
11271 {
11272 tree t;
11273
11274 /* Indicate that the old function template is a partial
11275 instantiation. */
11276 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11277 = new_friend_result_template_info;
11278
11279 gcc_assert (new_template
11280 == most_general_template (new_template));
11281 gcc_assert (new_template != old_decl);
11282
11283 /* Reassign any specializations already in the hash table
11284 to the new more general template, and add the
11285 additional template args. */
11286 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11287 t != NULL_TREE;
11288 t = TREE_CHAIN (t))
11289 {
11290 tree spec = TREE_VALUE (t);
11291 spec_entry elt;
11292
11293 elt.tmpl = old_decl;
11294 elt.args = DECL_TI_ARGS (spec);
11295 elt.spec = NULL_TREE;
11296
11297 decl_specializations->remove_elt (&elt);
11298
11299 DECL_TI_ARGS (spec)
11300 = add_outermost_template_args (new_args,
11301 DECL_TI_ARGS (spec));
11302
11303 register_specialization
11304 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11305
11306 }
11307 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11308 }
11309 }
11310
11311 /* The information from NEW_FRIEND has been merged into OLD_DECL
11312 by duplicate_decls. */
11313 new_friend = old_decl;
11314 }
11315 }
11316 else
11317 {
11318 tree context = DECL_CONTEXT (new_friend);
11319 bool dependent_p;
11320
11321 /* In the code
11322 template <class T> class C {
11323 template <class U> friend void C1<U>::f (); // case 1
11324 friend void C2<T>::f (); // case 2
11325 };
11326 we only need to make sure CONTEXT is a complete type for
11327 case 2. To distinguish between the two cases, we note that
11328 CONTEXT of case 1 remains dependent type after tsubst while
11329 this isn't true for case 2. */
11330 ++processing_template_decl;
11331 dependent_p = dependent_type_p (context);
11332 --processing_template_decl;
11333
11334 if (!dependent_p
11335 && !complete_type_or_else (context, NULL_TREE))
11336 return error_mark_node;
11337
11338 if (COMPLETE_TYPE_P (context))
11339 {
11340 tree fn = new_friend;
11341 /* do_friend adds the TEMPLATE_DECL for any member friend
11342 template even if it isn't a member template, i.e.
11343 template <class T> friend A<T>::f();
11344 Look through it in that case. */
11345 if (TREE_CODE (fn) == TEMPLATE_DECL
11346 && !PRIMARY_TEMPLATE_P (fn))
11347 fn = DECL_TEMPLATE_RESULT (fn);
11348 /* Check to see that the declaration is really present, and,
11349 possibly obtain an improved declaration. */
11350 fn = check_classfn (context, fn, NULL_TREE);
11351
11352 if (fn)
11353 new_friend = fn;
11354 }
11355 }
11356
11357 return new_friend;
11358 }
11359
11360 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11361 template arguments, as for tsubst.
11362
11363 Returns an appropriate tsubst'd friend type or error_mark_node on
11364 failure. */
11365
11366 static tree
11367 tsubst_friend_class (tree friend_tmpl, tree args)
11368 {
11369 tree tmpl;
11370
11371 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11372 {
11373 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11374 return TREE_TYPE (tmpl);
11375 }
11376
11377 tree context = CP_DECL_CONTEXT (friend_tmpl);
11378 if (TREE_CODE (context) == NAMESPACE_DECL)
11379 push_nested_namespace (context);
11380 else
11381 {
11382 context = tsubst (context, args, tf_error, NULL_TREE);
11383 push_nested_class (context);
11384 }
11385
11386 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11387 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11388
11389 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11390 {
11391 /* The friend template has already been declared. Just
11392 check to see that the declarations match, and install any new
11393 default parameters. We must tsubst the default parameters,
11394 of course. We only need the innermost template parameters
11395 because that is all that redeclare_class_template will look
11396 at. */
11397 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11398 > TMPL_ARGS_DEPTH (args))
11399 {
11400 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11401 args, tf_warning_or_error);
11402 location_t saved_input_location = input_location;
11403 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11404 tree cons = get_constraints (tmpl);
11405 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11406 input_location = saved_input_location;
11407 }
11408 }
11409 else
11410 {
11411 /* The friend template has not already been declared. In this
11412 case, the instantiation of the template class will cause the
11413 injection of this template into the namespace scope. */
11414 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11415
11416 if (tmpl != error_mark_node)
11417 {
11418 /* The new TMPL is not an instantiation of anything, so we
11419 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11420 for the new type because that is supposed to be the
11421 corresponding template decl, i.e., TMPL. */
11422 DECL_USE_TEMPLATE (tmpl) = 0;
11423 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11424 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11425 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11426 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11427
11428 /* Substitute into and set the constraints on the new declaration. */
11429 if (tree ci = get_constraints (friend_tmpl))
11430 {
11431 ++processing_template_decl;
11432 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11433 DECL_FRIEND_CONTEXT (friend_tmpl));
11434 --processing_template_decl;
11435 set_constraints (tmpl, ci);
11436 }
11437
11438 /* Inject this template into the enclosing namspace scope. */
11439 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11440 }
11441 }
11442
11443 if (TREE_CODE (context) == NAMESPACE_DECL)
11444 pop_nested_namespace (context);
11445 else
11446 pop_nested_class ();
11447
11448 return TREE_TYPE (tmpl);
11449 }
11450
11451 /* Returns zero if TYPE cannot be completed later due to circularity.
11452 Otherwise returns one. */
11453
11454 static int
11455 can_complete_type_without_circularity (tree type)
11456 {
11457 if (type == NULL_TREE || type == error_mark_node)
11458 return 0;
11459 else if (COMPLETE_TYPE_P (type))
11460 return 1;
11461 else if (TREE_CODE (type) == ARRAY_TYPE)
11462 return can_complete_type_without_circularity (TREE_TYPE (type));
11463 else if (CLASS_TYPE_P (type)
11464 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11465 return 0;
11466 else
11467 return 1;
11468 }
11469
11470 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11471 tsubst_flags_t, tree);
11472
11473 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11474 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11475
11476 static tree
11477 tsubst_attribute (tree t, tree *decl_p, tree args,
11478 tsubst_flags_t complain, tree in_decl)
11479 {
11480 gcc_assert (ATTR_IS_DEPENDENT (t));
11481
11482 tree val = TREE_VALUE (t);
11483 if (val == NULL_TREE)
11484 /* Nothing to do. */;
11485 else if ((flag_openmp || flag_openmp_simd)
11486 && is_attribute_p ("omp declare simd",
11487 get_attribute_name (t)))
11488 {
11489 tree clauses = TREE_VALUE (val);
11490 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11491 complain, in_decl);
11492 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11493 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11494 tree parms = DECL_ARGUMENTS (*decl_p);
11495 clauses
11496 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11497 if (clauses)
11498 val = build_tree_list (NULL_TREE, clauses);
11499 else
11500 val = NULL_TREE;
11501 }
11502 else if (flag_openmp
11503 && is_attribute_p ("omp declare variant base",
11504 get_attribute_name (t)))
11505 {
11506 ++cp_unevaluated_operand;
11507 tree varid
11508 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11509 in_decl, /*integral_constant_expression_p=*/false);
11510 --cp_unevaluated_operand;
11511 tree chain = TREE_CHAIN (val);
11512 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11513 tree ctx = copy_list (TREE_VALUE (val));
11514 tree simd = get_identifier ("simd");
11515 tree score = get_identifier (" score");
11516 tree condition = get_identifier ("condition");
11517 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11518 {
11519 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11520 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11521 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11522 {
11523 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11524 {
11525 tree clauses = TREE_VALUE (t2);
11526 clauses = tsubst_omp_clauses (clauses,
11527 C_ORT_OMP_DECLARE_SIMD, args,
11528 complain, in_decl);
11529 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11530 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11531 TREE_VALUE (t2) = clauses;
11532 }
11533 else
11534 {
11535 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11536 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11537 if (TREE_VALUE (t3))
11538 {
11539 bool allow_string
11540 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11541 && TREE_PURPOSE (t3) != score);
11542 tree v = TREE_VALUE (t3);
11543 if (TREE_CODE (v) == STRING_CST && allow_string)
11544 continue;
11545 v = tsubst_expr (v, args, complain, in_decl, true);
11546 v = fold_non_dependent_expr (v);
11547 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11548 || (TREE_PURPOSE (t3) == score
11549 ? TREE_CODE (v) != INTEGER_CST
11550 : !tree_fits_shwi_p (v)))
11551 {
11552 location_t loc
11553 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11554 match_loc);
11555 if (TREE_PURPOSE (t3) == score)
11556 error_at (loc, "score argument must be "
11557 "constant integer expression");
11558 else if (allow_string)
11559 error_at (loc, "property must be constant "
11560 "integer expression or string "
11561 "literal");
11562 else
11563 error_at (loc, "property must be constant "
11564 "integer expression");
11565 return NULL_TREE;
11566 }
11567 else if (TREE_PURPOSE (t3) == score
11568 && tree_int_cst_sgn (v) < 0)
11569 {
11570 location_t loc
11571 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11572 match_loc);
11573 error_at (loc, "score argument must be "
11574 "non-negative");
11575 return NULL_TREE;
11576 }
11577 TREE_VALUE (t3) = v;
11578 }
11579 }
11580 }
11581 }
11582 val = tree_cons (varid, ctx, chain);
11583 }
11584 /* If the first attribute argument is an identifier, don't
11585 pass it through tsubst. Attributes like mode, format,
11586 cleanup and several target specific attributes expect it
11587 unmodified. */
11588 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11589 {
11590 tree chain
11591 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11592 /*integral_constant_expression_p=*/false);
11593 if (chain != TREE_CHAIN (val))
11594 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11595 }
11596 else if (PACK_EXPANSION_P (val))
11597 {
11598 /* An attribute pack expansion. */
11599 tree purp = TREE_PURPOSE (t);
11600 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11601 if (pack == error_mark_node)
11602 return error_mark_node;
11603 int len = TREE_VEC_LENGTH (pack);
11604 tree list = NULL_TREE;
11605 tree *q = &list;
11606 for (int i = 0; i < len; ++i)
11607 {
11608 tree elt = TREE_VEC_ELT (pack, i);
11609 *q = build_tree_list (purp, elt);
11610 q = &TREE_CHAIN (*q);
11611 }
11612 return list;
11613 }
11614 else
11615 val = tsubst_expr (val, args, complain, in_decl,
11616 /*integral_constant_expression_p=*/false);
11617
11618 if (val == error_mark_node)
11619 return error_mark_node;
11620 if (val != TREE_VALUE (t))
11621 return build_tree_list (TREE_PURPOSE (t), val);
11622 return t;
11623 }
11624
11625 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11626 unchanged or a new TREE_LIST chain. */
11627
11628 static tree
11629 tsubst_attributes (tree attributes, tree args,
11630 tsubst_flags_t complain, tree in_decl)
11631 {
11632 tree last_dep = NULL_TREE;
11633
11634 for (tree t = attributes; t; t = TREE_CHAIN (t))
11635 if (ATTR_IS_DEPENDENT (t))
11636 {
11637 last_dep = t;
11638 attributes = copy_list (attributes);
11639 break;
11640 }
11641
11642 if (last_dep)
11643 for (tree *p = &attributes; *p; )
11644 {
11645 tree t = *p;
11646 if (ATTR_IS_DEPENDENT (t))
11647 {
11648 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11649 if (subst != t)
11650 {
11651 *p = subst;
11652 while (*p)
11653 p = &TREE_CHAIN (*p);
11654 *p = TREE_CHAIN (t);
11655 continue;
11656 }
11657 }
11658 p = &TREE_CHAIN (*p);
11659 }
11660
11661 return attributes;
11662 }
11663
11664 /* Apply any attributes which had to be deferred until instantiation
11665 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11666 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11667 false on error. */
11668
11669 static bool
11670 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11671 tree args, tsubst_flags_t complain, tree in_decl)
11672 {
11673 tree last_dep = NULL_TREE;
11674 tree t;
11675 tree *p;
11676
11677 if (attributes == NULL_TREE)
11678 return true;
11679
11680 if (DECL_P (*decl_p))
11681 {
11682 if (TREE_TYPE (*decl_p) == error_mark_node)
11683 return false;
11684 p = &DECL_ATTRIBUTES (*decl_p);
11685 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11686 to our attributes parameter. */
11687 gcc_assert (*p == attributes);
11688 }
11689 else
11690 {
11691 p = &TYPE_ATTRIBUTES (*decl_p);
11692 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11693 lookup_template_class_1, and should be preserved. */
11694 gcc_assert (*p != attributes);
11695 while (*p)
11696 p = &TREE_CHAIN (*p);
11697 }
11698
11699 for (t = attributes; t; t = TREE_CHAIN (t))
11700 if (ATTR_IS_DEPENDENT (t))
11701 {
11702 last_dep = t;
11703 attributes = copy_list (attributes);
11704 break;
11705 }
11706
11707 *p = attributes;
11708 if (last_dep)
11709 {
11710 tree late_attrs = NULL_TREE;
11711 tree *q = &late_attrs;
11712
11713 for (; *p; )
11714 {
11715 t = *p;
11716 if (ATTR_IS_DEPENDENT (t))
11717 {
11718 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11719 if (*q == error_mark_node)
11720 return false;
11721 *p = TREE_CHAIN (t);
11722 TREE_CHAIN (t) = NULL_TREE;
11723 while (*q)
11724 q = &TREE_CHAIN (*q);
11725 }
11726 else
11727 p = &TREE_CHAIN (t);
11728 }
11729
11730 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11731 }
11732 return true;
11733 }
11734
11735 /* The template TMPL is being instantiated with the template arguments TARGS.
11736 Perform the access checks that we deferred when parsing the template. */
11737
11738 static void
11739 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11740 {
11741 unsigned i;
11742 deferred_access_check *chk;
11743
11744 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11745 return;
11746
11747 if (vec<deferred_access_check, va_gc> *access_checks
11748 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11749 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11750 {
11751 tree decl = chk->decl;
11752 tree diag_decl = chk->diag_decl;
11753 tree type_scope = TREE_TYPE (chk->binfo);
11754
11755 if (uses_template_parms (type_scope))
11756 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11757
11758 /* Make access check error messages point to the location
11759 of the use of the typedef. */
11760 iloc_sentinel ils (chk->loc);
11761 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11762 decl, diag_decl, tf_warning_or_error);
11763 }
11764 }
11765
11766 static tree
11767 instantiate_class_template_1 (tree type)
11768 {
11769 tree templ, args, pattern, t, member;
11770 tree typedecl;
11771 tree pbinfo;
11772 tree base_list;
11773 unsigned int saved_maximum_field_alignment;
11774 tree fn_context;
11775
11776 if (type == error_mark_node)
11777 return error_mark_node;
11778
11779 if (COMPLETE_OR_OPEN_TYPE_P (type)
11780 || uses_template_parms (type))
11781 return type;
11782
11783 /* Figure out which template is being instantiated. */
11784 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11785 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11786
11787 /* Mark the type as in the process of being defined. */
11788 TYPE_BEING_DEFINED (type) = 1;
11789
11790 /* We may be in the middle of deferred access check. Disable
11791 it now. */
11792 deferring_access_check_sentinel acs (dk_no_deferred);
11793
11794 /* Determine what specialization of the original template to
11795 instantiate. */
11796 t = most_specialized_partial_spec (type, tf_warning_or_error);
11797 if (t == error_mark_node)
11798 return error_mark_node;
11799 else if (t)
11800 {
11801 /* This TYPE is actually an instantiation of a partial
11802 specialization. We replace the innermost set of ARGS with
11803 the arguments appropriate for substitution. For example,
11804 given:
11805
11806 template <class T> struct S {};
11807 template <class T> struct S<T*> {};
11808
11809 and supposing that we are instantiating S<int*>, ARGS will
11810 presently be {int*} -- but we need {int}. */
11811 pattern = TREE_TYPE (t);
11812 args = TREE_PURPOSE (t);
11813 }
11814 else
11815 {
11816 pattern = TREE_TYPE (templ);
11817 args = CLASSTYPE_TI_ARGS (type);
11818 }
11819
11820 /* If the template we're instantiating is incomplete, then clearly
11821 there's nothing we can do. */
11822 if (!COMPLETE_TYPE_P (pattern))
11823 {
11824 /* We can try again later. */
11825 TYPE_BEING_DEFINED (type) = 0;
11826 return type;
11827 }
11828
11829 /* If we've recursively instantiated too many templates, stop. */
11830 if (! push_tinst_level (type))
11831 return type;
11832
11833 int saved_unevaluated_operand = cp_unevaluated_operand;
11834 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11835
11836 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11837 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11838 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11839 fn_context = error_mark_node;
11840 if (!fn_context)
11841 push_to_top_level ();
11842 else
11843 {
11844 cp_unevaluated_operand = 0;
11845 c_inhibit_evaluation_warnings = 0;
11846 }
11847 /* Use #pragma pack from the template context. */
11848 saved_maximum_field_alignment = maximum_field_alignment;
11849 maximum_field_alignment = TYPE_PRECISION (pattern);
11850
11851 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11852
11853 /* Set the input location to the most specialized template definition.
11854 This is needed if tsubsting causes an error. */
11855 typedecl = TYPE_MAIN_DECL (pattern);
11856 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11857 DECL_SOURCE_LOCATION (typedecl);
11858
11859 set_instantiating_module (TYPE_NAME (type));
11860
11861 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11862 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11863 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11864 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11865 if (ANON_AGGR_TYPE_P (pattern))
11866 SET_ANON_AGGR_TYPE_P (type);
11867 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11868 {
11869 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11870 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11871 /* Adjust visibility for template arguments. */
11872 determine_visibility (TYPE_MAIN_DECL (type));
11873 }
11874 if (CLASS_TYPE_P (type))
11875 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11876
11877 pbinfo = TYPE_BINFO (pattern);
11878
11879 /* We should never instantiate a nested class before its enclosing
11880 class; we need to look up the nested class by name before we can
11881 instantiate it, and that lookup should instantiate the enclosing
11882 class. */
11883 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11884 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11885
11886 base_list = NULL_TREE;
11887 /* Defer access checking while we substitute into the types named in
11888 the base-clause. */
11889 push_deferring_access_checks (dk_deferred);
11890 if (BINFO_N_BASE_BINFOS (pbinfo))
11891 {
11892 tree pbase_binfo;
11893 int i;
11894
11895 /* Substitute into each of the bases to determine the actual
11896 basetypes. */
11897 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11898 {
11899 tree base;
11900 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11901 tree expanded_bases = NULL_TREE;
11902 int idx, len = 1;
11903
11904 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11905 {
11906 expanded_bases =
11907 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11908 args, tf_error, NULL_TREE);
11909 if (expanded_bases == error_mark_node)
11910 continue;
11911
11912 len = TREE_VEC_LENGTH (expanded_bases);
11913 }
11914
11915 for (idx = 0; idx < len; idx++)
11916 {
11917 if (expanded_bases)
11918 /* Extract the already-expanded base class. */
11919 base = TREE_VEC_ELT (expanded_bases, idx);
11920 else
11921 /* Substitute to figure out the base class. */
11922 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11923 NULL_TREE);
11924
11925 if (base == error_mark_node)
11926 continue;
11927
11928 base_list = tree_cons (access, base, base_list);
11929 if (BINFO_VIRTUAL_P (pbase_binfo))
11930 TREE_TYPE (base_list) = integer_type_node;
11931 }
11932 }
11933
11934 /* The list is now in reverse order; correct that. */
11935 base_list = nreverse (base_list);
11936 }
11937 /* Now call xref_basetypes to set up all the base-class
11938 information. */
11939 xref_basetypes (type, base_list);
11940
11941 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11942 (int) ATTR_FLAG_TYPE_IN_PLACE,
11943 args, tf_error, NULL_TREE);
11944 fixup_attribute_variants (type);
11945
11946 /* Now that our base classes are set up, enter the scope of the
11947 class, so that name lookups into base classes, etc. will work
11948 correctly. This is precisely analogous to what we do in
11949 begin_class_definition when defining an ordinary non-template
11950 class, except we also need to push the enclosing classes. */
11951 push_nested_class (type);
11952
11953 /* Now check accessibility of the types named in its base-clause,
11954 relative to the scope of the class. */
11955 pop_to_parent_deferring_access_checks ();
11956
11957 /* A vector to hold members marked with attribute used. */
11958 auto_vec<tree> used;
11959
11960 /* Now members are processed in the order of declaration. */
11961 for (member = CLASSTYPE_DECL_LIST (pattern);
11962 member; member = TREE_CHAIN (member))
11963 {
11964 tree t = TREE_VALUE (member);
11965
11966 if (TREE_PURPOSE (member))
11967 {
11968 if (TYPE_P (t))
11969 {
11970 if (LAMBDA_TYPE_P (t))
11971 /* A closure type for a lambda in an NSDMI or default argument.
11972 Ignore it; it will be regenerated when needed. */
11973 continue;
11974
11975 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11976 && TYPE_LANG_SPECIFIC (t)
11977 && CLASSTYPE_IS_TEMPLATE (t));
11978
11979 /* If the member is a class template, then -- even after
11980 substitution -- there may be dependent types in the
11981 template argument list for the class. We increment
11982 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11983 that function will assume that no types are dependent
11984 when outside of a template. */
11985 if (class_template_p)
11986 ++processing_template_decl;
11987 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11988 if (class_template_p)
11989 --processing_template_decl;
11990 if (newtag == error_mark_node)
11991 continue;
11992
11993 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11994 {
11995 tree name = TYPE_IDENTIFIER (t);
11996
11997 if (class_template_p)
11998 /* Unfortunately, lookup_template_class sets
11999 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12000 instantiation (i.e., for the type of a member
12001 template class nested within a template class.)
12002 This behavior is required for
12003 maybe_process_partial_specialization to work
12004 correctly, but is not accurate in this case;
12005 the TAG is not an instantiation of anything.
12006 (The corresponding TEMPLATE_DECL is an
12007 instantiation, but the TYPE is not.) */
12008 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
12009
12010 /* Now, install the tag. We don't use pushtag
12011 because that does too much work -- creating an
12012 implicit typedef, which we've already done. */
12013 set_identifier_type_value (name, TYPE_NAME (newtag));
12014 maybe_add_class_template_decl_list (type, newtag, false);
12015 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
12016 determine_visibility (TYPE_NAME (newtag));
12017 }
12018 }
12019 else if (DECL_DECLARES_FUNCTION_P (t))
12020 {
12021 tree r;
12022
12023 if (TREE_CODE (t) == TEMPLATE_DECL)
12024 ++processing_template_decl;
12025 r = tsubst (t, args, tf_error, NULL_TREE);
12026 if (TREE_CODE (t) == TEMPLATE_DECL)
12027 --processing_template_decl;
12028 set_current_access_from_decl (r);
12029 finish_member_declaration (r);
12030 /* Instantiate members marked with attribute used. */
12031 if (r != error_mark_node && DECL_PRESERVE_P (r))
12032 used.safe_push (r);
12033 if (TREE_CODE (r) == FUNCTION_DECL
12034 && DECL_OMP_DECLARE_REDUCTION_P (r))
12035 cp_check_omp_declare_reduction (r);
12036 }
12037 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12038 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12039 /* A closure type for a lambda in an NSDMI or default argument.
12040 Ignore it; it will be regenerated when needed. */;
12041 else
12042 {
12043 /* Build new TYPE_FIELDS. */
12044 if (TREE_CODE (t) == STATIC_ASSERT)
12045 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
12046 /*integral_constant_expression_p=*/true);
12047 else if (TREE_CODE (t) != CONST_DECL)
12048 {
12049 tree r;
12050 tree vec = NULL_TREE;
12051 int len = 1;
12052
12053 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12054 /* The file and line for this declaration, to
12055 assist in error message reporting. Since we
12056 called push_tinst_level above, we don't need to
12057 restore these. */
12058 input_location = DECL_SOURCE_LOCATION (t);
12059
12060 if (TREE_CODE (t) == TEMPLATE_DECL)
12061 ++processing_template_decl;
12062 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12063 if (TREE_CODE (t) == TEMPLATE_DECL)
12064 --processing_template_decl;
12065
12066 if (TREE_CODE (r) == TREE_VEC)
12067 {
12068 /* A capture pack became multiple fields. */
12069 vec = r;
12070 len = TREE_VEC_LENGTH (vec);
12071 }
12072
12073 for (int i = 0; i < len; ++i)
12074 {
12075 if (vec)
12076 r = TREE_VEC_ELT (vec, i);
12077 if (VAR_P (r))
12078 {
12079 /* In [temp.inst]:
12080
12081 [t]he initialization (and any associated
12082 side-effects) of a static data member does
12083 not occur unless the static data member is
12084 itself used in a way that requires the
12085 definition of the static data member to
12086 exist.
12087
12088 Therefore, we do not substitute into the
12089 initialized for the static data member here. */
12090 finish_static_data_member_decl
12091 (r,
12092 /*init=*/NULL_TREE,
12093 /*init_const_expr_p=*/false,
12094 /*asmspec_tree=*/NULL_TREE,
12095 /*flags=*/0);
12096 /* Instantiate members marked with attribute used. */
12097 if (r != error_mark_node && DECL_PRESERVE_P (r))
12098 used.safe_push (r);
12099 }
12100 else if (TREE_CODE (r) == FIELD_DECL)
12101 {
12102 /* Determine whether R has a valid type and can be
12103 completed later. If R is invalid, then its type
12104 is replaced by error_mark_node. */
12105 tree rtype = TREE_TYPE (r);
12106 if (can_complete_type_without_circularity (rtype))
12107 complete_type (rtype);
12108
12109 if (!complete_or_array_type_p (rtype))
12110 {
12111 /* If R's type couldn't be completed and
12112 it isn't a flexible array member (whose
12113 type is incomplete by definition) give
12114 an error. */
12115 cxx_incomplete_type_error (r, rtype);
12116 TREE_TYPE (r) = error_mark_node;
12117 }
12118 else if (TREE_CODE (rtype) == ARRAY_TYPE
12119 && TYPE_DOMAIN (rtype) == NULL_TREE
12120 && (TREE_CODE (type) == UNION_TYPE
12121 || TREE_CODE (type) == QUAL_UNION_TYPE))
12122 {
12123 error ("flexible array member %qD in union", r);
12124 TREE_TYPE (r) = error_mark_node;
12125 }
12126 else if (!verify_type_context (input_location,
12127 TCTX_FIELD, rtype))
12128 TREE_TYPE (r) = error_mark_node;
12129 }
12130
12131 /* If it is a TYPE_DECL for a class-scoped
12132 ENUMERAL_TYPE, such a thing will already have
12133 been added to the field list by tsubst_enum
12134 in finish_member_declaration case above. */
12135 if (!(TREE_CODE (r) == TYPE_DECL
12136 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12137 && DECL_ARTIFICIAL (r)))
12138 {
12139 set_current_access_from_decl (r);
12140 finish_member_declaration (r);
12141 }
12142 }
12143 }
12144 }
12145 }
12146 else
12147 {
12148 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12149 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12150 {
12151 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12152
12153 tree friend_type = t;
12154 bool adjust_processing_template_decl = false;
12155
12156 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12157 {
12158 /* template <class T> friend class C; */
12159 friend_type = tsubst_friend_class (friend_type, args);
12160 adjust_processing_template_decl = true;
12161 }
12162 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12163 {
12164 /* template <class T> friend class C::D; */
12165 friend_type = tsubst (friend_type, args,
12166 tf_warning_or_error, NULL_TREE);
12167 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12168 friend_type = TREE_TYPE (friend_type);
12169 adjust_processing_template_decl = true;
12170 }
12171 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12172 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12173 {
12174 /* This could be either
12175
12176 friend class T::C;
12177
12178 when dependent_type_p is false or
12179
12180 template <class U> friend class T::C;
12181
12182 otherwise. */
12183 /* Bump processing_template_decl in case this is something like
12184 template <class T> friend struct A<T>::B. */
12185 ++processing_template_decl;
12186 friend_type = tsubst (friend_type, args,
12187 tf_warning_or_error, NULL_TREE);
12188 if (dependent_type_p (friend_type))
12189 adjust_processing_template_decl = true;
12190 --processing_template_decl;
12191 }
12192 else if (uses_template_parms (friend_type))
12193 /* friend class C<T>; */
12194 friend_type = tsubst (friend_type, args,
12195 tf_warning_or_error, NULL_TREE);
12196
12197 /* Otherwise it's
12198
12199 friend class C;
12200
12201 where C is already declared or
12202
12203 friend class C<int>;
12204
12205 We don't have to do anything in these cases. */
12206
12207 if (adjust_processing_template_decl)
12208 /* Trick make_friend_class into realizing that the friend
12209 we're adding is a template, not an ordinary class. It's
12210 important that we use make_friend_class since it will
12211 perform some error-checking and output cross-reference
12212 information. */
12213 ++processing_template_decl;
12214
12215 if (friend_type != error_mark_node)
12216 make_friend_class (type, friend_type, /*complain=*/false);
12217
12218 if (adjust_processing_template_decl)
12219 --processing_template_decl;
12220 }
12221 else
12222 {
12223 /* Build new DECL_FRIENDLIST. */
12224 tree r;
12225
12226 /* The file and line for this declaration, to
12227 assist in error message reporting. Since we
12228 called push_tinst_level above, we don't need to
12229 restore these. */
12230 input_location = DECL_SOURCE_LOCATION (t);
12231
12232 if (TREE_CODE (t) == TEMPLATE_DECL)
12233 {
12234 ++processing_template_decl;
12235 push_deferring_access_checks (dk_no_check);
12236 }
12237
12238 r = tsubst_friend_function (t, args);
12239 add_friend (type, r, /*complain=*/false);
12240 if (TREE_CODE (t) == TEMPLATE_DECL)
12241 {
12242 pop_deferring_access_checks ();
12243 --processing_template_decl;
12244 }
12245 }
12246 }
12247 }
12248
12249 if (fn_context)
12250 {
12251 /* Restore these before substituting into the lambda capture
12252 initializers. */
12253 cp_unevaluated_operand = saved_unevaluated_operand;
12254 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12255 }
12256
12257 /* Set the file and line number information to whatever is given for
12258 the class itself. This puts error messages involving generated
12259 implicit functions at a predictable point, and the same point
12260 that would be used for non-template classes. */
12261 input_location = DECL_SOURCE_LOCATION (typedecl);
12262
12263 unreverse_member_declarations (type);
12264 finish_struct_1 (type);
12265 TYPE_BEING_DEFINED (type) = 0;
12266
12267 /* We don't instantiate default arguments for member functions. 14.7.1:
12268
12269 The implicit instantiation of a class template specialization causes
12270 the implicit instantiation of the declarations, but not of the
12271 definitions or default arguments, of the class member functions,
12272 member classes, static data members and member templates.... */
12273
12274 perform_instantiation_time_access_checks (pattern, args);
12275 perform_deferred_access_checks (tf_warning_or_error);
12276 pop_nested_class ();
12277 maximum_field_alignment = saved_maximum_field_alignment;
12278 if (!fn_context)
12279 pop_from_top_level ();
12280 pop_tinst_level ();
12281
12282 /* The vtable for a template class can be emitted in any translation
12283 unit in which the class is instantiated. When there is no key
12284 method, however, finish_struct_1 will already have added TYPE to
12285 the keyed_classes. */
12286 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12287 vec_safe_push (keyed_classes, type);
12288
12289 /* Now that we've gone through all the members, instantiate those
12290 marked with attribute used. */
12291 for (tree x : used)
12292 mark_used (x);
12293
12294 return type;
12295 }
12296
12297 /* Wrapper for instantiate_class_template_1. */
12298
12299 tree
12300 instantiate_class_template (tree type)
12301 {
12302 tree ret;
12303 timevar_push (TV_TEMPLATE_INST);
12304 ret = instantiate_class_template_1 (type);
12305 timevar_pop (TV_TEMPLATE_INST);
12306 return ret;
12307 }
12308
12309 tree
12310 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12311 {
12312 tree r;
12313
12314 if (!t)
12315 r = t;
12316 else if (TYPE_P (t))
12317 r = tsubst (t, args, complain, in_decl);
12318 else
12319 {
12320 if (!(complain & tf_warning))
12321 ++c_inhibit_evaluation_warnings;
12322 r = tsubst_expr (t, args, complain, in_decl,
12323 /*integral_constant_expression_p=*/true);
12324 if (!(complain & tf_warning))
12325 --c_inhibit_evaluation_warnings;
12326 }
12327
12328 return r;
12329 }
12330
12331 /* Given a function parameter pack TMPL_PARM and some function parameters
12332 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12333 and set *SPEC_P to point at the next point in the list. */
12334
12335 tree
12336 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12337 {
12338 /* Collect all of the extra "packed" parameters into an
12339 argument pack. */
12340 tree argpack;
12341 tree spec_parm = *spec_p;
12342 int len;
12343
12344 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12345 if (tmpl_parm
12346 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12347 break;
12348
12349 spec_parm = *spec_p;
12350 if (len == 1 && DECL_PACK_P (spec_parm))
12351 {
12352 /* The instantiation is still a parameter pack; don't wrap it in a
12353 NONTYPE_ARGUMENT_PACK. */
12354 argpack = spec_parm;
12355 spec_parm = DECL_CHAIN (spec_parm);
12356 }
12357 else
12358 {
12359 /* Fill in PARMVEC with all of the parameters. */
12360 tree parmvec = make_tree_vec (len);
12361 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12362 for (int i = 0; i < len; i++)
12363 {
12364 tree elt = spec_parm;
12365 if (DECL_PACK_P (elt))
12366 elt = make_pack_expansion (elt);
12367 TREE_VEC_ELT (parmvec, i) = elt;
12368 spec_parm = DECL_CHAIN (spec_parm);
12369 }
12370
12371 /* Build the argument packs. */
12372 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12373 }
12374 *spec_p = spec_parm;
12375
12376 return argpack;
12377 }
12378
12379 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12380 NONTYPE_ARGUMENT_PACK. */
12381
12382 static tree
12383 make_fnparm_pack (tree spec_parm)
12384 {
12385 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12386 }
12387
12388 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12389 pack expansion with no extra args, 2 if it has extra args, or 0
12390 if it is not a pack expansion. */
12391
12392 static int
12393 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12394 {
12395 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12396 /* We're being called before this happens in tsubst_pack_expansion. */
12397 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12398 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12399 if (i >= TREE_VEC_LENGTH (vec))
12400 return 0;
12401 tree elt = TREE_VEC_ELT (vec, i);
12402 if (DECL_P (elt))
12403 /* A decl pack is itself an expansion. */
12404 elt = TREE_TYPE (elt);
12405 if (!PACK_EXPANSION_P (elt))
12406 return 0;
12407 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12408 return 2;
12409 return 1;
12410 }
12411
12412
12413 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12414
12415 static tree
12416 make_argument_pack_select (tree arg_pack, unsigned index)
12417 {
12418 tree aps = make_node (ARGUMENT_PACK_SELECT);
12419
12420 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12421 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12422
12423 return aps;
12424 }
12425
12426 /* This is a subroutine of tsubst_pack_expansion.
12427
12428 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12429 mechanism to store the (non complete list of) arguments of the
12430 substitution and return a non substituted pack expansion, in order
12431 to wait for when we have enough arguments to really perform the
12432 substitution. */
12433
12434 static bool
12435 use_pack_expansion_extra_args_p (tree parm_packs,
12436 int arg_pack_len,
12437 bool has_empty_arg)
12438 {
12439 /* If one pack has an expansion and another pack has a normal
12440 argument or if one pack has an empty argument and an another
12441 one hasn't then tsubst_pack_expansion cannot perform the
12442 substitution and need to fall back on the
12443 PACK_EXPANSION_EXTRA mechanism. */
12444 if (parm_packs == NULL_TREE)
12445 return false;
12446 else if (has_empty_arg)
12447 {
12448 /* If all the actual packs are pack expansions, we can still
12449 subsitute directly. */
12450 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12451 {
12452 tree a = TREE_VALUE (p);
12453 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12454 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12455 a = ARGUMENT_PACK_ARGS (a);
12456 if (TREE_VEC_LENGTH (a) == 1)
12457 a = TREE_VEC_ELT (a, 0);
12458 if (PACK_EXPANSION_P (a))
12459 continue;
12460 return true;
12461 }
12462 return false;
12463 }
12464
12465 for (int i = 0 ; i < arg_pack_len; ++i)
12466 {
12467 bool has_expansion_arg = false;
12468 bool has_non_expansion_arg = false;
12469 for (tree parm_pack = parm_packs;
12470 parm_pack;
12471 parm_pack = TREE_CHAIN (parm_pack))
12472 {
12473 tree arg = TREE_VALUE (parm_pack);
12474
12475 int exp = argument_pack_element_is_expansion_p (arg, i);
12476 if (exp == 2)
12477 /* We can't substitute a pack expansion with extra args into
12478 our pattern. */
12479 return true;
12480 else if (exp)
12481 has_expansion_arg = true;
12482 else
12483 has_non_expansion_arg = true;
12484 }
12485
12486 if (has_expansion_arg && has_non_expansion_arg)
12487 {
12488 gcc_checking_assert (false);
12489 return true;
12490 }
12491 }
12492 return false;
12493 }
12494
12495 /* [temp.variadic]/6 says that:
12496
12497 The instantiation of a pack expansion [...]
12498 produces a list E1,E2, ..., En, where N is the number of elements
12499 in the pack expansion parameters.
12500
12501 This subroutine of tsubst_pack_expansion produces one of these Ei.
12502
12503 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12504 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12505 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12506 INDEX is the index 'i' of the element Ei to produce. ARGS,
12507 COMPLAIN, and IN_DECL are the same parameters as for the
12508 tsubst_pack_expansion function.
12509
12510 The function returns the resulting Ei upon successful completion,
12511 or error_mark_node.
12512
12513 Note that this function possibly modifies the ARGS parameter, so
12514 it's the responsibility of the caller to restore it. */
12515
12516 static tree
12517 gen_elem_of_pack_expansion_instantiation (tree pattern,
12518 tree parm_packs,
12519 unsigned index,
12520 tree args /* This parm gets
12521 modified. */,
12522 tsubst_flags_t complain,
12523 tree in_decl)
12524 {
12525 tree t;
12526 bool ith_elem_is_expansion = false;
12527
12528 /* For each parameter pack, change the substitution of the parameter
12529 pack to the ith argument in its argument pack, then expand the
12530 pattern. */
12531 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12532 {
12533 tree parm = TREE_PURPOSE (pack);
12534 tree arg_pack = TREE_VALUE (pack);
12535 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12536
12537 ith_elem_is_expansion |=
12538 argument_pack_element_is_expansion_p (arg_pack, index);
12539
12540 /* Select the Ith argument from the pack. */
12541 if (TREE_CODE (parm) == PARM_DECL
12542 || VAR_P (parm)
12543 || TREE_CODE (parm) == FIELD_DECL)
12544 {
12545 if (index == 0)
12546 {
12547 aps = make_argument_pack_select (arg_pack, index);
12548 if (!mark_used (parm, complain) && !(complain & tf_error))
12549 return error_mark_node;
12550 register_local_specialization (aps, parm);
12551 }
12552 else
12553 aps = retrieve_local_specialization (parm);
12554 }
12555 else
12556 {
12557 int idx, level;
12558 template_parm_level_and_index (parm, &level, &idx);
12559
12560 if (index == 0)
12561 {
12562 aps = make_argument_pack_select (arg_pack, index);
12563 /* Update the corresponding argument. */
12564 TMPL_ARG (args, level, idx) = aps;
12565 }
12566 else
12567 /* Re-use the ARGUMENT_PACK_SELECT. */
12568 aps = TMPL_ARG (args, level, idx);
12569 }
12570 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12571 }
12572
12573 /* Substitute into the PATTERN with the (possibly altered)
12574 arguments. */
12575 if (pattern == in_decl)
12576 /* Expanding a fixed parameter pack from
12577 coerce_template_parameter_pack. */
12578 t = tsubst_decl (pattern, args, complain);
12579 else if (pattern == error_mark_node)
12580 t = error_mark_node;
12581 else if (!TYPE_P (pattern))
12582 t = tsubst_expr (pattern, args, complain, in_decl,
12583 /*integral_constant_expression_p=*/false);
12584 else
12585 t = tsubst (pattern, args, complain, in_decl);
12586
12587 /* If the Ith argument pack element is a pack expansion, then
12588 the Ith element resulting from the substituting is going to
12589 be a pack expansion as well. */
12590 if (ith_elem_is_expansion)
12591 t = make_pack_expansion (t, complain);
12592
12593 return t;
12594 }
12595
12596 /* When the unexpanded parameter pack in a fold expression expands to an empty
12597 sequence, the value of the expression is as follows; the program is
12598 ill-formed if the operator is not listed in this table.
12599
12600 && true
12601 || false
12602 , void() */
12603
12604 tree
12605 expand_empty_fold (tree t, tsubst_flags_t complain)
12606 {
12607 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12608 if (!FOLD_EXPR_MODIFY_P (t))
12609 switch (code)
12610 {
12611 case TRUTH_ANDIF_EXPR:
12612 return boolean_true_node;
12613 case TRUTH_ORIF_EXPR:
12614 return boolean_false_node;
12615 case COMPOUND_EXPR:
12616 return void_node;
12617 default:
12618 break;
12619 }
12620
12621 if (complain & tf_error)
12622 error_at (location_of (t),
12623 "fold of empty expansion over %O", code);
12624 return error_mark_node;
12625 }
12626
12627 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12628 form an expression that combines the two terms using the
12629 operator of T. */
12630
12631 static tree
12632 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12633 {
12634 tree op = FOLD_EXPR_OP (t);
12635 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12636
12637 // Handle compound assignment operators.
12638 if (FOLD_EXPR_MODIFY_P (t))
12639 return build_x_modify_expr (input_location, left, code, right, complain);
12640
12641 warning_sentinel s(warn_parentheses);
12642 switch (code)
12643 {
12644 case COMPOUND_EXPR:
12645 return build_x_compound_expr (input_location, left, right, complain);
12646 default:
12647 return build_x_binary_op (input_location, code,
12648 left, TREE_CODE (left),
12649 right, TREE_CODE (right),
12650 /*overload=*/NULL,
12651 complain);
12652 }
12653 }
12654
12655 /* Substitute ARGS into the pack of a fold expression T. */
12656
12657 static inline tree
12658 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12659 {
12660 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12661 }
12662
12663 /* Substitute ARGS into the pack of a fold expression T. */
12664
12665 static inline tree
12666 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12667 {
12668 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12669 }
12670
12671 /* Expand a PACK of arguments into a grouped as left fold.
12672 Given a pack containing elements A0, A1, ..., An and an
12673 operator @, this builds the expression:
12674
12675 ((A0 @ A1) @ A2) ... @ An
12676
12677 Note that PACK must not be empty.
12678
12679 The operator is defined by the original fold expression T. */
12680
12681 static tree
12682 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12683 {
12684 tree left = TREE_VEC_ELT (pack, 0);
12685 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12686 {
12687 tree right = TREE_VEC_ELT (pack, i);
12688 left = fold_expression (t, left, right, complain);
12689 }
12690 return left;
12691 }
12692
12693 /* Substitute into a unary left fold expression. */
12694
12695 static tree
12696 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12697 tree in_decl)
12698 {
12699 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12700 if (pack == error_mark_node)
12701 return error_mark_node;
12702 if (PACK_EXPANSION_P (pack))
12703 {
12704 tree r = copy_node (t);
12705 FOLD_EXPR_PACK (r) = pack;
12706 return r;
12707 }
12708 if (TREE_VEC_LENGTH (pack) == 0)
12709 return expand_empty_fold (t, complain);
12710 else
12711 return expand_left_fold (t, pack, complain);
12712 }
12713
12714 /* Substitute into a binary left fold expression.
12715
12716 Do ths by building a single (non-empty) vector of argumnts and
12717 building the expression from those elements. */
12718
12719 static tree
12720 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12721 tree in_decl)
12722 {
12723 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12724 if (pack == error_mark_node)
12725 return error_mark_node;
12726 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12727 if (init == error_mark_node)
12728 return error_mark_node;
12729
12730 if (PACK_EXPANSION_P (pack))
12731 {
12732 tree r = copy_node (t);
12733 FOLD_EXPR_PACK (r) = pack;
12734 FOLD_EXPR_INIT (r) = init;
12735 return r;
12736 }
12737
12738 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12739 TREE_VEC_ELT (vec, 0) = init;
12740 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12741 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12742
12743 return expand_left_fold (t, vec, complain);
12744 }
12745
12746 /* Expand a PACK of arguments into a grouped as right fold.
12747 Given a pack containing elementns A0, A1, ..., and an
12748 operator @, this builds the expression:
12749
12750 A0@ ... (An-2 @ (An-1 @ An))
12751
12752 Note that PACK must not be empty.
12753
12754 The operator is defined by the original fold expression T. */
12755
12756 tree
12757 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12758 {
12759 // Build the expression.
12760 int n = TREE_VEC_LENGTH (pack);
12761 tree right = TREE_VEC_ELT (pack, n - 1);
12762 for (--n; n != 0; --n)
12763 {
12764 tree left = TREE_VEC_ELT (pack, n - 1);
12765 right = fold_expression (t, left, right, complain);
12766 }
12767 return right;
12768 }
12769
12770 /* Substitute into a unary right fold expression. */
12771
12772 static tree
12773 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12774 tree in_decl)
12775 {
12776 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12777 if (pack == error_mark_node)
12778 return error_mark_node;
12779 if (PACK_EXPANSION_P (pack))
12780 {
12781 tree r = copy_node (t);
12782 FOLD_EXPR_PACK (r) = pack;
12783 return r;
12784 }
12785 if (TREE_VEC_LENGTH (pack) == 0)
12786 return expand_empty_fold (t, complain);
12787 else
12788 return expand_right_fold (t, pack, complain);
12789 }
12790
12791 /* Substitute into a binary right fold expression.
12792
12793 Do ths by building a single (non-empty) vector of arguments and
12794 building the expression from those elements. */
12795
12796 static tree
12797 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12798 tree in_decl)
12799 {
12800 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12801 if (pack == error_mark_node)
12802 return error_mark_node;
12803 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12804 if (init == error_mark_node)
12805 return error_mark_node;
12806
12807 if (PACK_EXPANSION_P (pack))
12808 {
12809 tree r = copy_node (t);
12810 FOLD_EXPR_PACK (r) = pack;
12811 FOLD_EXPR_INIT (r) = init;
12812 return r;
12813 }
12814
12815 int n = TREE_VEC_LENGTH (pack);
12816 tree vec = make_tree_vec (n + 1);
12817 for (int i = 0; i < n; ++i)
12818 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12819 TREE_VEC_ELT (vec, n) = init;
12820
12821 return expand_right_fold (t, vec, complain);
12822 }
12823
12824 /* Walk through the pattern of a pack expansion, adding everything in
12825 local_specializations to a list. */
12826
12827 class el_data
12828 {
12829 public:
12830 /* Set of variables declared within the pattern. */
12831 hash_set<tree> internal;
12832 /* Set of AST nodes that have been visited by the traversal. */
12833 hash_set<tree> visited;
12834 /* List of local_specializations used within the pattern. */
12835 tree extra;
12836 tsubst_flags_t complain;
12837
12838 el_data (tsubst_flags_t c)
12839 : extra (NULL_TREE), complain (c) {}
12840 };
12841 static tree
12842 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12843 {
12844 el_data &data = *reinterpret_cast<el_data*>(data_);
12845 tree *extra = &data.extra;
12846 tsubst_flags_t complain = data.complain;
12847
12848 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12849 /* Remember local typedefs (85214). */
12850 tp = &TYPE_NAME (*tp);
12851
12852 if (TREE_CODE (*tp) == DECL_EXPR)
12853 {
12854 tree decl = DECL_EXPR_DECL (*tp);
12855 data.internal.add (decl);
12856 if (VAR_P (decl)
12857 && DECL_DECOMPOSITION_P (decl)
12858 && TREE_TYPE (decl) != error_mark_node)
12859 {
12860 gcc_assert (DECL_NAME (decl) == NULL_TREE);
12861 for (tree decl2 = DECL_CHAIN (decl);
12862 decl2
12863 && VAR_P (decl2)
12864 && DECL_DECOMPOSITION_P (decl2)
12865 && DECL_NAME (decl2)
12866 && TREE_TYPE (decl2) != error_mark_node;
12867 decl2 = DECL_CHAIN (decl2))
12868 {
12869 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
12870 data.internal.add (decl2);
12871 }
12872 }
12873 }
12874 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
12875 {
12876 /* Since we defer implicit capture, look in the parms and body. */
12877 tree fn = lambda_function (*tp);
12878 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
12879 &data.visited);
12880 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
12881 &data.visited);
12882 }
12883 else if (tree spec = retrieve_local_specialization (*tp))
12884 {
12885 if (data.internal.contains (*tp))
12886 /* Don't mess with variables declared within the pattern. */
12887 return NULL_TREE;
12888 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12889 {
12890 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12891 tree args = ARGUMENT_PACK_ARGS (spec);
12892 if (TREE_VEC_LENGTH (args) == 1)
12893 {
12894 tree elt = TREE_VEC_ELT (args, 0);
12895 if (PACK_EXPANSION_P (elt))
12896 elt = PACK_EXPANSION_PATTERN (elt);
12897 if (DECL_PACK_P (elt))
12898 spec = elt;
12899 }
12900 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12901 {
12902 /* Handle lambda capture here, since we aren't doing any
12903 substitution now, and so tsubst_copy won't call
12904 process_outer_var_ref. */
12905 tree args = ARGUMENT_PACK_ARGS (spec);
12906 int len = TREE_VEC_LENGTH (args);
12907 for (int i = 0; i < len; ++i)
12908 {
12909 tree arg = TREE_VEC_ELT (args, i);
12910 tree carg = arg;
12911 if (outer_automatic_var_p (arg))
12912 carg = process_outer_var_ref (arg, complain);
12913 if (carg != arg)
12914 {
12915 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12916 proxies. */
12917 if (i == 0)
12918 {
12919 spec = copy_node (spec);
12920 args = copy_node (args);
12921 SET_ARGUMENT_PACK_ARGS (spec, args);
12922 register_local_specialization (spec, *tp);
12923 }
12924 TREE_VEC_ELT (args, i) = carg;
12925 }
12926 }
12927 }
12928 }
12929 if (outer_automatic_var_p (spec))
12930 spec = process_outer_var_ref (spec, complain);
12931 *extra = tree_cons (*tp, spec, *extra);
12932 }
12933 return NULL_TREE;
12934 }
12935 static tree
12936 extract_local_specs (tree pattern, tsubst_flags_t complain)
12937 {
12938 el_data data (complain);
12939 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
12940 return data.extra;
12941 }
12942
12943 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12944 for use in PACK_EXPANSION_EXTRA_ARGS. */
12945
12946 tree
12947 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12948 {
12949 /* Make a copy of the extra arguments so that they won't get changed
12950 out from under us. */
12951 tree extra = copy_template_args (args);
12952 if (local_specializations)
12953 if (tree locals = extract_local_specs (pattern, complain))
12954 extra = tree_cons (NULL_TREE, extra, locals);
12955 return extra;
12956 }
12957
12958 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12959 normal template args to ARGS. */
12960
12961 tree
12962 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
12963 {
12964 if (extra && TREE_CODE (extra) == TREE_LIST)
12965 {
12966 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12967 {
12968 /* The partial instantiation involved local declarations collected in
12969 extract_local_specs; map from the general template to our local
12970 context. */
12971 tree gen = TREE_PURPOSE (elt);
12972 tree inst = TREE_VALUE (elt);
12973 if (DECL_P (inst))
12974 if (tree local = retrieve_local_specialization (inst))
12975 inst = local;
12976 /* else inst is already a full instantiation of the pack. */
12977 register_local_specialization (inst, gen);
12978 }
12979 gcc_assert (!TREE_PURPOSE (extra));
12980 extra = TREE_VALUE (extra);
12981 }
12982 if (uses_template_parms (extra))
12983 {
12984 /* This can happen after dependent substitution into a
12985 requires-expr or a lambda that uses constexpr if. */
12986 extra = tsubst_template_args (extra, args, complain, in_decl);
12987 args = add_outermost_template_args (args, extra);
12988 }
12989 else
12990 args = add_to_template_args (extra, args);
12991 return args;
12992 }
12993
12994 /* Substitute ARGS into T, which is an pack expansion
12995 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12996 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12997 (if only a partial substitution could be performed) or
12998 ERROR_MARK_NODE if there was an error. */
12999 tree
13000 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
13001 tree in_decl)
13002 {
13003 tree pattern;
13004 tree pack, packs = NULL_TREE;
13005 bool unsubstituted_packs = false;
13006 int i, len = -1;
13007 tree result;
13008 bool need_local_specializations = false;
13009 int levels;
13010
13011 gcc_assert (PACK_EXPANSION_P (t));
13012 pattern = PACK_EXPANSION_PATTERN (t);
13013
13014 /* Add in any args remembered from an earlier partial instantiation. */
13015 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
13016
13017 levels = TMPL_ARGS_DEPTH (args);
13018
13019 /* Determine the argument packs that will instantiate the parameter
13020 packs used in the expansion expression. While we're at it,
13021 compute the number of arguments to be expanded and make sure it
13022 is consistent. */
13023 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13024 pack = TREE_CHAIN (pack))
13025 {
13026 tree parm_pack = TREE_VALUE (pack);
13027 tree arg_pack = NULL_TREE;
13028 tree orig_arg = NULL_TREE;
13029 int level = 0;
13030
13031 if (TREE_CODE (parm_pack) == BASES)
13032 {
13033 gcc_assert (parm_pack == pattern);
13034 if (BASES_DIRECT (parm_pack))
13035 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
13036 args, complain,
13037 in_decl, false),
13038 complain);
13039 else
13040 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
13041 args, complain, in_decl,
13042 false), complain);
13043 }
13044 else if (builtin_pack_call_p (parm_pack))
13045 {
13046 if (parm_pack != pattern)
13047 {
13048 if (complain & tf_error)
13049 sorry ("%qE is not the entire pattern of the pack expansion",
13050 parm_pack);
13051 return error_mark_node;
13052 }
13053 return expand_builtin_pack_call (parm_pack, args,
13054 complain, in_decl);
13055 }
13056 else if (TREE_CODE (parm_pack) == PARM_DECL)
13057 {
13058 /* We know we have correct local_specializations if this
13059 expansion is at function scope, or if we're dealing with a
13060 local parameter in a requires expression; for the latter,
13061 tsubst_requires_expr set it up appropriately. */
13062 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13063 arg_pack = retrieve_local_specialization (parm_pack);
13064 else
13065 /* We can't rely on local_specializations for a parameter
13066 name used later in a function declaration (such as in a
13067 late-specified return type). Even if it exists, it might
13068 have the wrong value for a recursive call. */
13069 need_local_specializations = true;
13070
13071 if (!arg_pack)
13072 {
13073 /* This parameter pack was used in an unevaluated context. Just
13074 make a dummy decl, since it's only used for its type. */
13075 ++cp_unevaluated_operand;
13076 arg_pack = tsubst_decl (parm_pack, args, complain);
13077 --cp_unevaluated_operand;
13078 if (arg_pack && DECL_PACK_P (arg_pack))
13079 /* Partial instantiation of the parm_pack, we can't build
13080 up an argument pack yet. */
13081 arg_pack = NULL_TREE;
13082 else
13083 arg_pack = make_fnparm_pack (arg_pack);
13084 }
13085 else if (DECL_PACK_P (arg_pack))
13086 /* This argument pack isn't fully instantiated yet. */
13087 arg_pack = NULL_TREE;
13088 }
13089 else if (is_capture_proxy (parm_pack))
13090 {
13091 arg_pack = retrieve_local_specialization (parm_pack);
13092 if (DECL_PACK_P (arg_pack))
13093 arg_pack = NULL_TREE;
13094 }
13095 else
13096 {
13097 int idx;
13098 template_parm_level_and_index (parm_pack, &level, &idx);
13099 if (level <= levels)
13100 arg_pack = TMPL_ARG (args, level, idx);
13101
13102 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13103 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13104 arg_pack = NULL_TREE;
13105 }
13106
13107 orig_arg = arg_pack;
13108 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13109 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13110
13111 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13112 /* This can only happen if we forget to expand an argument
13113 pack somewhere else. Just return an error, silently. */
13114 {
13115 result = make_tree_vec (1);
13116 TREE_VEC_ELT (result, 0) = error_mark_node;
13117 return result;
13118 }
13119
13120 if (arg_pack)
13121 {
13122 int my_len =
13123 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13124
13125 /* Don't bother trying to do a partial substitution with
13126 incomplete packs; we'll try again after deduction. */
13127 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13128 return t;
13129
13130 if (len < 0)
13131 len = my_len;
13132 else if (len != my_len)
13133 {
13134 if (!(complain & tf_error))
13135 /* Fail quietly. */;
13136 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13137 error ("mismatched argument pack lengths while expanding %qT",
13138 pattern);
13139 else
13140 error ("mismatched argument pack lengths while expanding %qE",
13141 pattern);
13142 return error_mark_node;
13143 }
13144
13145 /* Keep track of the parameter packs and their corresponding
13146 argument packs. */
13147 packs = tree_cons (parm_pack, arg_pack, packs);
13148 TREE_TYPE (packs) = orig_arg;
13149 }
13150 else
13151 {
13152 /* We can't substitute for this parameter pack. We use a flag as
13153 well as the missing_level counter because function parameter
13154 packs don't have a level. */
13155 gcc_assert (processing_template_decl || is_auto (parm_pack));
13156 unsubstituted_packs = true;
13157 }
13158 }
13159
13160 /* If the expansion is just T..., return the matching argument pack, unless
13161 we need to call convert_from_reference on all the elements. This is an
13162 important optimization; see c++/68422. */
13163 if (!unsubstituted_packs
13164 && TREE_PURPOSE (packs) == pattern)
13165 {
13166 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13167
13168 /* If the argument pack is a single pack expansion, pull it out. */
13169 if (TREE_VEC_LENGTH (args) == 1
13170 && pack_expansion_args_count (args))
13171 return TREE_VEC_ELT (args, 0);
13172
13173 /* Types need no adjustment, nor does sizeof..., and if we still have
13174 some pack expansion args we won't do anything yet. */
13175 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13176 || PACK_EXPANSION_SIZEOF_P (t)
13177 || pack_expansion_args_count (args))
13178 return args;
13179 /* Also optimize expression pack expansions if we can tell that the
13180 elements won't have reference type. */
13181 tree type = TREE_TYPE (pattern);
13182 if (type && !TYPE_REF_P (type)
13183 && !PACK_EXPANSION_P (type)
13184 && !WILDCARD_TYPE_P (type))
13185 return args;
13186 /* Otherwise use the normal path so we get convert_from_reference. */
13187 }
13188
13189 /* We cannot expand this expansion expression, because we don't have
13190 all of the argument packs we need. */
13191 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13192 {
13193 /* We got some full packs, but we can't substitute them in until we
13194 have values for all the packs. So remember these until then. */
13195
13196 t = make_pack_expansion (pattern, complain);
13197 PACK_EXPANSION_EXTRA_ARGS (t)
13198 = build_extra_args (pattern, args, complain);
13199 return t;
13200 }
13201
13202 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13203 type, so create our own local specializations map; the current map is
13204 either NULL or (in the case of recursive unification) might have
13205 bindings that we don't want to use or alter. */
13206 local_specialization_stack lss (need_local_specializations
13207 ? lss_blank : lss_nop);
13208
13209 if (unsubstituted_packs)
13210 {
13211 /* There were no real arguments, we're just replacing a parameter
13212 pack with another version of itself. Substitute into the
13213 pattern and return a PACK_EXPANSION_*. The caller will need to
13214 deal with that. */
13215 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13216 result = tsubst_expr (pattern, args, complain, in_decl,
13217 /*integral_constant_expression_p=*/false);
13218 else
13219 result = tsubst (pattern, args, complain, in_decl);
13220 result = make_pack_expansion (result, complain);
13221 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13222 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13223 if (PACK_EXPANSION_AUTO_P (t))
13224 {
13225 /* This is a fake auto... pack expansion created in add_capture with
13226 _PACKS that don't appear in the pattern. Copy one over. */
13227 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13228 pack = retrieve_local_specialization (TREE_VALUE (packs));
13229 gcc_checking_assert (DECL_PACK_P (pack));
13230 PACK_EXPANSION_PARAMETER_PACKS (result)
13231 = build_tree_list (NULL_TREE, pack);
13232 PACK_EXPANSION_AUTO_P (result) = true;
13233 }
13234 return result;
13235 }
13236
13237 gcc_assert (len >= 0);
13238
13239 /* For each argument in each argument pack, substitute into the
13240 pattern. */
13241 result = make_tree_vec (len);
13242 tree elem_args = copy_template_args (args);
13243 for (i = 0; i < len; ++i)
13244 {
13245 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13246 i,
13247 elem_args, complain,
13248 in_decl);
13249 TREE_VEC_ELT (result, i) = t;
13250 if (t == error_mark_node)
13251 {
13252 result = error_mark_node;
13253 break;
13254 }
13255 }
13256
13257 /* Update ARGS to restore the substitution from parameter packs to
13258 their argument packs. */
13259 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13260 {
13261 tree parm = TREE_PURPOSE (pack);
13262
13263 if (TREE_CODE (parm) == PARM_DECL
13264 || VAR_P (parm)
13265 || TREE_CODE (parm) == FIELD_DECL)
13266 register_local_specialization (TREE_TYPE (pack), parm);
13267 else
13268 {
13269 int idx, level;
13270
13271 if (TREE_VALUE (pack) == NULL_TREE)
13272 continue;
13273
13274 template_parm_level_and_index (parm, &level, &idx);
13275
13276 /* Update the corresponding argument. */
13277 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13278 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13279 TREE_TYPE (pack);
13280 else
13281 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13282 }
13283 }
13284
13285 /* If the dependent pack arguments were such that we end up with only a
13286 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13287 if (len == 1 && TREE_CODE (result) == TREE_VEC
13288 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13289 return TREE_VEC_ELT (result, 0);
13290
13291 return result;
13292 }
13293
13294 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13295 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13296 parameter packs; all parms generated from a function parameter pack will
13297 have the same DECL_PARM_INDEX. */
13298
13299 tree
13300 get_pattern_parm (tree parm, tree tmpl)
13301 {
13302 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13303 tree patparm;
13304
13305 if (DECL_ARTIFICIAL (parm))
13306 {
13307 for (patparm = DECL_ARGUMENTS (pattern);
13308 patparm; patparm = DECL_CHAIN (patparm))
13309 if (DECL_ARTIFICIAL (patparm)
13310 && DECL_NAME (parm) == DECL_NAME (patparm))
13311 break;
13312 }
13313 else
13314 {
13315 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13316 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13317 gcc_assert (DECL_PARM_INDEX (patparm)
13318 == DECL_PARM_INDEX (parm));
13319 }
13320
13321 return patparm;
13322 }
13323
13324 /* Make an argument pack out of the TREE_VEC VEC. */
13325
13326 static tree
13327 make_argument_pack (tree vec)
13328 {
13329 tree pack;
13330
13331 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13332 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13333 else
13334 {
13335 pack = make_node (NONTYPE_ARGUMENT_PACK);
13336 TREE_CONSTANT (pack) = 1;
13337 }
13338 SET_ARGUMENT_PACK_ARGS (pack, vec);
13339 return pack;
13340 }
13341
13342 /* Return an exact copy of template args T that can be modified
13343 independently. */
13344
13345 static tree
13346 copy_template_args (tree t)
13347 {
13348 if (t == error_mark_node)
13349 return t;
13350
13351 int len = TREE_VEC_LENGTH (t);
13352 tree new_vec = make_tree_vec (len);
13353
13354 for (int i = 0; i < len; ++i)
13355 {
13356 tree elt = TREE_VEC_ELT (t, i);
13357 if (elt && TREE_CODE (elt) == TREE_VEC)
13358 elt = copy_template_args (elt);
13359 TREE_VEC_ELT (new_vec, i) = elt;
13360 }
13361
13362 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13363 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13364
13365 return new_vec;
13366 }
13367
13368 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13369
13370 tree
13371 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13372 tree in_decl)
13373 {
13374 /* Substitute into each of the arguments. */
13375 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13376 args, complain, in_decl);
13377 tree new_arg = error_mark_node;
13378 if (pack_args != error_mark_node)
13379 {
13380 if (TYPE_P (orig_arg))
13381 {
13382 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13383 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13384 }
13385 else
13386 {
13387 new_arg = make_node (TREE_CODE (orig_arg));
13388 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13389 }
13390
13391 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13392 }
13393
13394 return new_arg;
13395 }
13396
13397 /* Substitute ARGS into the vector or list of template arguments T. */
13398
13399 tree
13400 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13401 {
13402 tree orig_t = t;
13403 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13404 tree *elts;
13405
13406 if (t == error_mark_node)
13407 return error_mark_node;
13408
13409 len = TREE_VEC_LENGTH (t);
13410 elts = XALLOCAVEC (tree, len);
13411
13412 for (i = 0; i < len; i++)
13413 {
13414 tree orig_arg = TREE_VEC_ELT (t, i);
13415 tree new_arg;
13416
13417 if (!orig_arg)
13418 new_arg = NULL_TREE;
13419 else if (TREE_CODE (orig_arg) == TREE_VEC)
13420 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13421 else if (PACK_EXPANSION_P (orig_arg))
13422 {
13423 /* Substitute into an expansion expression. */
13424 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13425
13426 if (TREE_CODE (new_arg) == TREE_VEC)
13427 /* Add to the expanded length adjustment the number of
13428 expanded arguments. We subtract one from this
13429 measurement, because the argument pack expression
13430 itself is already counted as 1 in
13431 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13432 the argument pack is empty. */
13433 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13434 }
13435 else if (ARGUMENT_PACK_P (orig_arg))
13436 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13437 else
13438 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13439
13440 if (new_arg == error_mark_node)
13441 return error_mark_node;
13442
13443 elts[i] = new_arg;
13444 if (new_arg != orig_arg)
13445 need_new = 1;
13446 }
13447
13448 if (!need_new)
13449 return t;
13450
13451 /* Make space for the expanded arguments coming from template
13452 argument packs. */
13453 t = make_tree_vec (len + expanded_len_adjust);
13454 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13455 arguments for a member template.
13456 In that case each TREE_VEC in ORIG_T represents a level of template
13457 arguments, and ORIG_T won't carry any non defaulted argument count.
13458 It will rather be the nested TREE_VECs that will carry one.
13459 In other words, ORIG_T carries a non defaulted argument count only
13460 if it doesn't contain any nested TREE_VEC. */
13461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13462 {
13463 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13464 count += expanded_len_adjust;
13465 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13466 }
13467 for (i = 0, out = 0; i < len; i++)
13468 {
13469 tree orig_arg = TREE_VEC_ELT (orig_t, i);
13470 if (orig_arg
13471 && (PACK_EXPANSION_P (orig_arg) || ARGUMENT_PACK_P (orig_arg))
13472 && TREE_CODE (elts[i]) == TREE_VEC)
13473 {
13474 int idx;
13475
13476 /* Now expand the template argument pack "in place". */
13477 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13478 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13479 }
13480 else
13481 {
13482 TREE_VEC_ELT (t, out) = elts[i];
13483 out++;
13484 }
13485 }
13486
13487 return t;
13488 }
13489
13490 /* Substitute ARGS into one level PARMS of template parameters. */
13491
13492 static tree
13493 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13494 {
13495 if (parms == error_mark_node)
13496 return error_mark_node;
13497
13498 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13499
13500 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13501 {
13502 tree tuple = TREE_VEC_ELT (parms, i);
13503
13504 if (tuple == error_mark_node)
13505 continue;
13506
13507 TREE_VEC_ELT (new_vec, i) =
13508 tsubst_template_parm (tuple, args, complain);
13509 }
13510
13511 return new_vec;
13512 }
13513
13514 /* Return the result of substituting ARGS into the template parameters
13515 given by PARMS. If there are m levels of ARGS and m + n levels of
13516 PARMS, then the result will contain n levels of PARMS. For
13517 example, if PARMS is `template <class T> template <class U>
13518 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13519 result will be `template <int*, double, class V>'. */
13520
13521 static tree
13522 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13523 {
13524 tree r = NULL_TREE;
13525 tree* new_parms;
13526
13527 /* When substituting into a template, we must set
13528 PROCESSING_TEMPLATE_DECL as the template parameters may be
13529 dependent if they are based on one-another, and the dependency
13530 predicates are short-circuit outside of templates. */
13531 ++processing_template_decl;
13532
13533 for (new_parms = &r;
13534 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13535 new_parms = &(TREE_CHAIN (*new_parms)),
13536 parms = TREE_CHAIN (parms))
13537 {
13538 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13539 args, complain);
13540 *new_parms =
13541 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13542 - TMPL_ARGS_DEPTH (args)),
13543 new_vec, NULL_TREE);
13544 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13545 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13546 }
13547
13548 --processing_template_decl;
13549
13550 return r;
13551 }
13552
13553 /* Return the result of substituting ARGS into one template parameter
13554 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13555 parameter and which TREE_PURPOSE is the default argument of the
13556 template parameter. */
13557
13558 static tree
13559 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13560 {
13561 tree default_value, parm_decl;
13562
13563 if (args == NULL_TREE
13564 || t == NULL_TREE
13565 || t == error_mark_node)
13566 return t;
13567
13568 gcc_assert (TREE_CODE (t) == TREE_LIST);
13569
13570 default_value = TREE_PURPOSE (t);
13571 parm_decl = TREE_VALUE (t);
13572 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13573
13574 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13575 if (TREE_CODE (parm_decl) == PARM_DECL
13576 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13577 parm_decl = error_mark_node;
13578 default_value = tsubst_template_arg (default_value, args,
13579 complain, NULL_TREE);
13580 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13581
13582 tree r = build_tree_list (default_value, parm_decl);
13583 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13584 return r;
13585 }
13586
13587 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13588 type T. If T is not an aggregate or enumeration type, it is
13589 handled as if by tsubst. IN_DECL is as for tsubst. If
13590 ENTERING_SCOPE is nonzero, T is the context for a template which
13591 we are presently tsubst'ing. Return the substituted value. */
13592
13593 static tree
13594 tsubst_aggr_type (tree t,
13595 tree args,
13596 tsubst_flags_t complain,
13597 tree in_decl,
13598 int entering_scope)
13599 {
13600 if (t == NULL_TREE)
13601 return NULL_TREE;
13602
13603 /* If T is an alias template specialization, we want to substitute that
13604 rather than strip it, especially if it's dependent_alias_template_spec_p.
13605 It should be OK not to handle entering_scope in this case, since
13606 DECL_CONTEXT will never be an alias template specialization. We only get
13607 here with an alias when tsubst calls us for TYPENAME_TYPE. */
13608 if (alias_template_specialization_p (t, nt_transparent))
13609 return tsubst (t, args, complain, in_decl);
13610
13611 switch (TREE_CODE (t))
13612 {
13613 case RECORD_TYPE:
13614 if (TYPE_PTRMEMFUNC_P (t))
13615 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13616
13617 /* Fall through. */
13618 case ENUMERAL_TYPE:
13619 case UNION_TYPE:
13620 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13621 {
13622 tree argvec;
13623 tree context;
13624 tree r;
13625
13626 /* In "sizeof(X<I>)" we need to evaluate "I". */
13627 cp_evaluated ev;
13628
13629 /* First, determine the context for the type we are looking
13630 up. */
13631 context = TYPE_CONTEXT (t);
13632 if (context && TYPE_P (context))
13633 {
13634 context = tsubst_aggr_type (context, args, complain,
13635 in_decl, /*entering_scope=*/1);
13636 /* If context is a nested class inside a class template,
13637 it may still need to be instantiated (c++/33959). */
13638 context = complete_type (context);
13639 }
13640
13641 /* Then, figure out what arguments are appropriate for the
13642 type we are trying to find. For example, given:
13643
13644 template <class T> struct S;
13645 template <class T, class U> void f(T, U) { S<U> su; }
13646
13647 and supposing that we are instantiating f<int, double>,
13648 then our ARGS will be {int, double}, but, when looking up
13649 S we only want {double}. */
13650 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13651 complain, in_decl);
13652 if (argvec == error_mark_node)
13653 r = error_mark_node;
13654 else if (!entering_scope
13655 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13656 {
13657 /* See maybe_dependent_member_ref. */
13658 tree name = TYPE_IDENTIFIER (t);
13659 tree fullname = name;
13660 if (instantiates_primary_template_p (t))
13661 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13662 INNERMOST_TEMPLATE_ARGS (argvec));
13663 return build_typename_type (context, name, fullname,
13664 typename_type);
13665 }
13666 else
13667 {
13668 r = lookup_template_class (t, argvec, in_decl, context,
13669 entering_scope, complain);
13670 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13671 }
13672
13673 return r;
13674 }
13675 else
13676 /* This is not a template type, so there's nothing to do. */
13677 return t;
13678
13679 default:
13680 return tsubst (t, args, complain, in_decl);
13681 }
13682 }
13683
13684 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13685 indexed in reverse order of the parameters. */
13686
13687 static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
13688
13689 /* Return a reference to the vec* of defarg insts for FN. */
13690
13691 static vec<tree,va_gc> *&
13692 defarg_insts_for (tree fn)
13693 {
13694 if (!defarg_inst)
13695 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
13696 tree_vec_map in = { fn, nullptr };
13697 tree_vec_map **slot
13698 = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
13699 if (!*slot)
13700 {
13701 *slot = ggc_alloc<tree_vec_map> ();
13702 **slot = in;
13703 }
13704 return (*slot)->to;
13705 }
13706
13707 /* Substitute into the default argument ARG (a default argument for
13708 FN), which has the indicated TYPE. */
13709
13710 tree
13711 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13712 tsubst_flags_t complain)
13713 {
13714 int errs = errorcount + sorrycount;
13715
13716 /* This can happen in invalid code. */
13717 if (TREE_CODE (arg) == DEFERRED_PARSE)
13718 return arg;
13719
13720 /* Shortcut {}. */
13721 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13722 && CONSTRUCTOR_NELTS (arg) == 0)
13723 return arg;
13724
13725 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13726 parm = chain_index (parmnum, parm);
13727 tree parmtype = TREE_TYPE (parm);
13728 if (DECL_BY_REFERENCE (parm))
13729 parmtype = TREE_TYPE (parmtype);
13730 if (parmtype == error_mark_node)
13731 return error_mark_node;
13732
13733 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13734
13735 /* Remember the location of the pointer to the vec rather than the location
13736 of the particular element, in case the vec grows in tsubst_expr. */
13737 vec<tree,va_gc> *&defs = defarg_insts_for (fn);
13738 /* Index in reverse order to avoid allocating space for initial parameters
13739 that don't have default arguments. */
13740 unsigned ridx = list_length (parm);
13741 if (vec_safe_length (defs) < ridx)
13742 vec_safe_grow_cleared (defs, ridx);
13743 else if (tree inst = (*defs)[ridx - 1])
13744 return inst;
13745
13746 /* This default argument came from a template. Instantiate the
13747 default argument here, not in tsubst. In the case of
13748 something like:
13749
13750 template <class T>
13751 struct S {
13752 static T t();
13753 void f(T = t());
13754 };
13755
13756 we must be careful to do name lookup in the scope of S<T>,
13757 rather than in the current class. */
13758 push_to_top_level ();
13759 push_access_scope (fn);
13760 push_deferring_access_checks (dk_no_deferred);
13761 start_lambda_scope (parm);
13762
13763 /* The default argument expression may cause implicitly defined
13764 member functions to be synthesized, which will result in garbage
13765 collection. We must treat this situation as if we were within
13766 the body of function so as to avoid collecting live data on the
13767 stack. */
13768 ++function_depth;
13769 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13770 complain, NULL_TREE,
13771 /*integral_constant_expression_p=*/false);
13772 --function_depth;
13773
13774 finish_lambda_scope ();
13775
13776 /* Make sure the default argument is reasonable. */
13777 arg = check_default_argument (type, arg, complain);
13778
13779 if (errorcount+sorrycount > errs
13780 && (complain & tf_warning_or_error))
13781 inform (input_location,
13782 " when instantiating default argument for call to %qD", fn);
13783
13784 pop_deferring_access_checks ();
13785 pop_access_scope (fn);
13786 pop_from_top_level ();
13787
13788 if (arg != error_mark_node && !cp_unevaluated_operand)
13789 (*defs)[ridx - 1] = arg;
13790
13791 return arg;
13792 }
13793
13794 /* Substitute into all the default arguments for FN. */
13795
13796 static void
13797 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13798 {
13799 tree arg;
13800 tree tmpl_args;
13801
13802 tmpl_args = DECL_TI_ARGS (fn);
13803
13804 /* If this function is not yet instantiated, we certainly don't need
13805 its default arguments. */
13806 if (uses_template_parms (tmpl_args))
13807 return;
13808 /* Don't do this again for clones. */
13809 if (DECL_CLONED_FUNCTION_P (fn))
13810 return;
13811
13812 int i = 0;
13813 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13814 arg;
13815 arg = TREE_CHAIN (arg), ++i)
13816 if (TREE_PURPOSE (arg))
13817 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13818 TREE_VALUE (arg),
13819 TREE_PURPOSE (arg),
13820 complain);
13821 }
13822
13823 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13824 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13825
13826 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13827
13828 void
13829 store_explicit_specifier (tree v, tree t)
13830 {
13831 if (!explicit_specifier_map)
13832 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13833 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13834 explicit_specifier_map->put (v, t);
13835 }
13836
13837 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13838
13839 static tree
13840 lookup_explicit_specifier (tree v)
13841 {
13842 return *explicit_specifier_map->get (v);
13843 }
13844
13845 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13846 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13847 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13848 identical to T. */
13849
13850 static tree
13851 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13852 tree raises, tsubst_flags_t complain)
13853 {
13854 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13855
13856 tree new_type;
13857 if (TREE_CODE (t) == FUNCTION_TYPE)
13858 {
13859 new_type = build_function_type (return_type, arg_types);
13860 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13861 }
13862 else
13863 {
13864 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13865 /* Don't pick up extra function qualifiers from the basetype. */
13866 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13867 if (! MAYBE_CLASS_TYPE_P (r))
13868 {
13869 /* [temp.deduct]
13870
13871 Type deduction may fail for any of the following
13872 reasons:
13873
13874 -- Attempting to create "pointer to member of T" when T
13875 is not a class type. */
13876 if (complain & tf_error)
13877 error ("creating pointer to member function of non-class type %qT",
13878 r);
13879 return error_mark_node;
13880 }
13881
13882 new_type = build_method_type_directly (r, return_type,
13883 TREE_CHAIN (arg_types));
13884 }
13885 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13886
13887 cp_ref_qualifier rqual = type_memfn_rqual (t);
13888 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13889 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13890 }
13891
13892 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13893 each of its formal parameters. If there is a disagreement then rebuild
13894 DECL's function type according to its formal parameter types, as part of a
13895 resolution for Core issues 1001/1322. */
13896
13897 static void
13898 maybe_rebuild_function_decl_type (tree decl)
13899 {
13900 bool function_type_needs_rebuilding = false;
13901 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13902 {
13903 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13904 while (parm_type_list && parm_type_list != void_list_node)
13905 {
13906 tree parm_type = TREE_VALUE (parm_type_list);
13907 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13908 if (!same_type_p (parm_type, formal_parm_type_unqual))
13909 {
13910 function_type_needs_rebuilding = true;
13911 break;
13912 }
13913
13914 parm_list = DECL_CHAIN (parm_list);
13915 parm_type_list = TREE_CHAIN (parm_type_list);
13916 }
13917 }
13918
13919 if (!function_type_needs_rebuilding)
13920 return;
13921
13922 const tree fntype = TREE_TYPE (decl);
13923 tree parm_list = DECL_ARGUMENTS (decl);
13924 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13925 tree new_parm_type_list = NULL_TREE;
13926 tree *q = &new_parm_type_list;
13927 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13928 {
13929 *q = copy_node (old_parm_type_list);
13930 parm_list = DECL_CHAIN (parm_list);
13931 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13932 q = &TREE_CHAIN (*q);
13933 }
13934 while (old_parm_type_list && old_parm_type_list != void_list_node)
13935 {
13936 *q = copy_node (old_parm_type_list);
13937 tree *new_parm_type = &TREE_VALUE (*q);
13938 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13939 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13940 *new_parm_type = formal_parm_type_unqual;
13941
13942 parm_list = DECL_CHAIN (parm_list);
13943 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13944 q = &TREE_CHAIN (*q);
13945 }
13946 if (old_parm_type_list == void_list_node)
13947 *q = void_list_node;
13948
13949 TREE_TYPE (decl)
13950 = rebuild_function_or_method_type (fntype,
13951 TREE_TYPE (fntype), new_parm_type_list,
13952 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13953 }
13954
13955 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13956
13957 static tree
13958 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13959 tree lambda_fntype)
13960 {
13961 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13962 hashval_t hash = 0;
13963 tree in_decl = t;
13964
13965 /* Nobody should be tsubst'ing into non-template functions. */
13966 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13967 || DECL_LOCAL_DECL_P (t));
13968
13969 if (DECL_LOCAL_DECL_P (t))
13970 {
13971 if (tree spec = retrieve_local_specialization (t))
13972 return spec;
13973 }
13974 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13975 {
13976 /* If T is not dependent, just return it. */
13977 if (!uses_template_parms (DECL_TI_ARGS (t))
13978 && !LAMBDA_FUNCTION_P (t))
13979 return t;
13980
13981 /* Calculate the most general template of which R is a
13982 specialization. */
13983 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13984
13985 /* We're substituting a lambda function under tsubst_lambda_expr but not
13986 directly from it; find the matching function we're already inside.
13987 But don't do this if T is a generic lambda with a single level of
13988 template parms, as in that case we're doing a normal instantiation. */
13989 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13990 && (!generic_lambda_fn_p (t)
13991 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13992 return enclosing_instantiation_of (t);
13993
13994 /* Calculate the complete set of arguments used to
13995 specialize R. */
13996 argvec = tsubst_template_args (DECL_TI_ARGS
13997 (DECL_TEMPLATE_RESULT
13998 (DECL_TI_TEMPLATE (t))),
13999 args, complain, in_decl);
14000 if (argvec == error_mark_node)
14001 return error_mark_node;
14002
14003 /* Check to see if we already have this specialization. */
14004 if (!lambda_fntype)
14005 {
14006 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14007 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
14008 return spec;
14009 }
14010 }
14011 else
14012 {
14013 /* This special case arises when we have something like this:
14014
14015 template <class T> struct S {
14016 friend void f<int>(int, double);
14017 };
14018
14019 Here, the DECL_TI_TEMPLATE for the friend declaration
14020 will be an IDENTIFIER_NODE. We are being called from
14021 tsubst_friend_function, and we want only to create a
14022 new decl (R) with appropriate types so that we can call
14023 determine_specialization. */
14024 gen_tmpl = NULL_TREE;
14025 argvec = NULL_TREE;
14026 }
14027
14028 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
14029 : NULL_TREE);
14030 tree ctx = closure ? closure : DECL_CONTEXT (t);
14031 bool member = ctx && TYPE_P (ctx);
14032
14033 if (member && !closure)
14034 ctx = tsubst_aggr_type (ctx, args,
14035 complain, t, /*entering_scope=*/1);
14036
14037 tree type = (lambda_fntype ? lambda_fntype
14038 : tsubst (TREE_TYPE (t), args,
14039 complain | tf_fndecl_type, in_decl));
14040 if (type == error_mark_node)
14041 return error_mark_node;
14042
14043 /* If we hit excessive deduction depth, the type is bogus even if
14044 it isn't error_mark_node, so don't build a decl. */
14045 if (excessive_deduction_depth)
14046 return error_mark_node;
14047
14048 /* We do NOT check for matching decls pushed separately at this
14049 point, as they may not represent instantiations of this
14050 template, and in any case are considered separate under the
14051 discrete model. */
14052 tree r = copy_decl (t);
14053 DECL_USE_TEMPLATE (r) = 0;
14054 TREE_TYPE (r) = type;
14055 /* Clear out the mangled name and RTL for the instantiation. */
14056 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14057 SET_DECL_RTL (r, NULL);
14058 /* Leave DECL_INITIAL set on deleted instantiations. */
14059 if (!DECL_DELETED_FN (r))
14060 DECL_INITIAL (r) = NULL_TREE;
14061 DECL_CONTEXT (r) = ctx;
14062 set_instantiating_module (r);
14063
14064 /* Handle explicit(dependent-expr). */
14065 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
14066 {
14067 tree spec = lookup_explicit_specifier (t);
14068 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
14069 /*function_p=*/false,
14070 /*i_c_e_p=*/true);
14071 spec = build_explicit_specifier (spec, complain);
14072 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
14073 }
14074
14075 /* OpenMP UDRs have the only argument a reference to the declared
14076 type. We want to diagnose if the declared type is a reference,
14077 which is invalid, but as references to references are usually
14078 quietly merged, diagnose it here. */
14079 if (DECL_OMP_DECLARE_REDUCTION_P (t))
14080 {
14081 tree argtype
14082 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
14083 argtype = tsubst (argtype, args, complain, in_decl);
14084 if (TYPE_REF_P (argtype))
14085 error_at (DECL_SOURCE_LOCATION (t),
14086 "reference type %qT in "
14087 "%<#pragma omp declare reduction%>", argtype);
14088 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
14089 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14090 argtype);
14091 }
14092
14093 if (member && DECL_CONV_FN_P (r))
14094 /* Type-conversion operator. Reconstruct the name, in
14095 case it's the name of one of the template's parameters. */
14096 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14097
14098 tree parms = DECL_ARGUMENTS (t);
14099 if (closure)
14100 parms = DECL_CHAIN (parms);
14101 parms = tsubst (parms, args, complain, t);
14102 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14103 DECL_CONTEXT (parm) = r;
14104 if (closure)
14105 {
14106 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14107 DECL_NAME (tparm) = closure_identifier;
14108 DECL_CHAIN (tparm) = parms;
14109 parms = tparm;
14110 }
14111 DECL_ARGUMENTS (r) = parms;
14112 DECL_RESULT (r) = NULL_TREE;
14113
14114 maybe_rebuild_function_decl_type (r);
14115
14116 TREE_STATIC (r) = 0;
14117 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14118 DECL_EXTERNAL (r) = 1;
14119 /* If this is an instantiation of a function with internal
14120 linkage, we already know what object file linkage will be
14121 assigned to the instantiation. */
14122 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14123 DECL_DEFER_OUTPUT (r) = 0;
14124 DECL_CHAIN (r) = NULL_TREE;
14125 DECL_PENDING_INLINE_INFO (r) = 0;
14126 DECL_PENDING_INLINE_P (r) = 0;
14127 DECL_SAVED_TREE (r) = NULL_TREE;
14128 DECL_STRUCT_FUNCTION (r) = NULL;
14129 TREE_USED (r) = 0;
14130 /* We'll re-clone as appropriate in instantiate_template. */
14131 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14132
14133 /* If we aren't complaining now, return on error before we register
14134 the specialization so that we'll complain eventually. */
14135 if ((complain & tf_error) == 0
14136 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14137 && !grok_op_properties (r, /*complain=*/false))
14138 return error_mark_node;
14139
14140 /* Associate the constraints directly with the instantiation. We
14141 don't substitute through the constraints; that's only done when
14142 they are checked. */
14143 if (tree ci = get_constraints (t))
14144 set_constraints (r, ci);
14145
14146 if (DECL_FRIEND_CONTEXT (t))
14147 SET_DECL_FRIEND_CONTEXT (r,
14148 tsubst (DECL_FRIEND_CONTEXT (t),
14149 args, complain, in_decl));
14150
14151 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14152 args, complain, in_decl))
14153 return error_mark_node;
14154
14155 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14156 this in the special friend case mentioned above where
14157 GEN_TMPL is NULL. */
14158 if (gen_tmpl && !closure)
14159 {
14160 DECL_TEMPLATE_INFO (r)
14161 = build_template_info (gen_tmpl, argvec);
14162 SET_DECL_IMPLICIT_INSTANTIATION (r);
14163
14164 tree new_r
14165 = register_specialization (r, gen_tmpl, argvec, false, hash);
14166 if (new_r != r)
14167 /* We instantiated this while substituting into
14168 the type earlier (template/friend54.C). */
14169 return new_r;
14170
14171 /* We're not supposed to instantiate default arguments
14172 until they are called, for a template. But, for a
14173 declaration like:
14174
14175 template <class T> void f ()
14176 { extern void g(int i = T()); }
14177
14178 we should do the substitution when the template is
14179 instantiated. We handle the member function case in
14180 instantiate_class_template since the default arguments
14181 might refer to other members of the class. */
14182 if (!member
14183 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14184 && !uses_template_parms (argvec))
14185 tsubst_default_arguments (r, complain);
14186 }
14187 else if (DECL_LOCAL_DECL_P (r))
14188 {
14189 if (!cp_unevaluated_operand)
14190 register_local_specialization (r, t);
14191 }
14192 else
14193 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14194
14195 /* Copy the list of befriending classes. */
14196 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14197 *friends;
14198 friends = &TREE_CHAIN (*friends))
14199 {
14200 *friends = copy_node (*friends);
14201 TREE_VALUE (*friends)
14202 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14203 }
14204
14205 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14206 {
14207 maybe_retrofit_in_chrg (r);
14208 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14209 return error_mark_node;
14210 /* If this is an instantiation of a member template, clone it.
14211 If it isn't, that'll be handled by
14212 clone_constructors_and_destructors. */
14213 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14214 clone_cdtor (r, /*update_methods=*/false);
14215 }
14216 else if ((complain & tf_error) != 0
14217 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14218 && !grok_op_properties (r, /*complain=*/true))
14219 return error_mark_node;
14220
14221 /* Possibly limit visibility based on template args. */
14222 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14223 if (DECL_VISIBILITY_SPECIFIED (t))
14224 {
14225 DECL_VISIBILITY_SPECIFIED (r) = 0;
14226 DECL_ATTRIBUTES (r)
14227 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14228 }
14229 determine_visibility (r);
14230 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14231 && !processing_template_decl)
14232 defaulted_late_check (r);
14233
14234 if (flag_openmp)
14235 if (tree attr = lookup_attribute ("omp declare variant base",
14236 DECL_ATTRIBUTES (r)))
14237 omp_declare_variant_finalize (r, attr);
14238
14239 return r;
14240 }
14241
14242 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14243
14244 static tree
14245 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14246 tree lambda_fntype)
14247 {
14248 /* We can get here when processing a member function template,
14249 member class template, or template template parameter. */
14250 tree decl = DECL_TEMPLATE_RESULT (t);
14251 tree in_decl = t;
14252 tree spec;
14253 tree tmpl_args;
14254 tree full_args;
14255 tree r;
14256 hashval_t hash = 0;
14257
14258 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14259 {
14260 /* Template template parameter is treated here. */
14261 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14262 if (new_type == error_mark_node)
14263 r = error_mark_node;
14264 /* If we get a real template back, return it. This can happen in
14265 the context of most_specialized_partial_spec. */
14266 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14267 r = new_type;
14268 else
14269 /* The new TEMPLATE_DECL was built in
14270 reduce_template_parm_level. */
14271 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14272 return r;
14273 }
14274
14275 if (!lambda_fntype)
14276 {
14277 /* We might already have an instance of this template.
14278 The ARGS are for the surrounding class type, so the
14279 full args contain the tsubst'd args for the context,
14280 plus the innermost args from the template decl. */
14281 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14282 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14283 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14284 /* Because this is a template, the arguments will still be
14285 dependent, even after substitution. If
14286 PROCESSING_TEMPLATE_DECL is not set, the dependency
14287 predicates will short-circuit. */
14288 ++processing_template_decl;
14289 full_args = tsubst_template_args (tmpl_args, args,
14290 complain, in_decl);
14291 --processing_template_decl;
14292 if (full_args == error_mark_node)
14293 return error_mark_node;
14294
14295 /* If this is a default template template argument,
14296 tsubst might not have changed anything. */
14297 if (full_args == tmpl_args)
14298 return t;
14299
14300 hash = hash_tmpl_and_args (t, full_args);
14301 spec = retrieve_specialization (t, full_args, hash);
14302 if (spec != NULL_TREE)
14303 {
14304 if (TYPE_P (spec))
14305 /* Type partial instantiations are stored as the type by
14306 lookup_template_class_1, not here as the template. */
14307 spec = CLASSTYPE_TI_TEMPLATE (spec);
14308 return spec;
14309 }
14310 }
14311
14312 /* Make a new template decl. It will be similar to the
14313 original, but will record the current template arguments.
14314 We also create a new function declaration, which is just
14315 like the old one, but points to this new template, rather
14316 than the old one. */
14317 r = copy_decl (t);
14318 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14319 DECL_CHAIN (r) = NULL_TREE;
14320
14321 // Build new template info linking to the original template decl.
14322 if (!lambda_fntype)
14323 {
14324 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14325 SET_DECL_IMPLICIT_INSTANTIATION (r);
14326 }
14327 else
14328 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14329
14330 /* The template parameters for this new template are all the
14331 template parameters for the old template, except the
14332 outermost level of parameters. */
14333 auto tparm_guard = make_temp_override (current_template_parms);
14334 DECL_TEMPLATE_PARMS (r)
14335 = current_template_parms
14336 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14337 complain);
14338
14339 bool class_p = false;
14340 tree inner = decl;
14341 ++processing_template_decl;
14342 if (TREE_CODE (inner) == FUNCTION_DECL)
14343 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14344 else
14345 {
14346 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14347 {
14348 class_p = true;
14349 inner = TREE_TYPE (inner);
14350 }
14351 if (class_p)
14352 inner = tsubst_aggr_type (inner, args, complain,
14353 in_decl, /*entering*/1);
14354 else
14355 inner = tsubst (inner, args, complain, in_decl);
14356 }
14357 --processing_template_decl;
14358 if (inner == error_mark_node)
14359 return error_mark_node;
14360
14361 if (class_p)
14362 {
14363 /* For a partial specialization, we need to keep pointing to
14364 the primary template. */
14365 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14366 CLASSTYPE_TI_TEMPLATE (inner) = r;
14367
14368 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14369 inner = TYPE_MAIN_DECL (inner);
14370 }
14371 else if (lambda_fntype)
14372 {
14373 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14374 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14375 }
14376 else
14377 {
14378 DECL_TI_TEMPLATE (inner) = r;
14379 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14380 }
14381
14382 DECL_TEMPLATE_RESULT (r) = inner;
14383 TREE_TYPE (r) = TREE_TYPE (inner);
14384 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14385
14386 if (modules_p ())
14387 {
14388 /* Propagate module information from the decl. */
14389 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14390 if (DECL_LANG_SPECIFIC (inner))
14391 /* If this is a constrained template, the above tsubst of
14392 inner can find the unconstrained template, which may have
14393 come from an import. This is ok, because we don't
14394 register this instantiation (see below). */
14395 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14396 || (TEMPLATE_PARMS_CONSTRAINTS
14397 (DECL_TEMPLATE_PARMS (t))));
14398 }
14399
14400 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14401 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14402
14403 if (PRIMARY_TEMPLATE_P (t))
14404 DECL_PRIMARY_TEMPLATE (r) = r;
14405
14406 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14407 /* Record this non-type partial instantiation. */
14408 register_specialization (r, t,
14409 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14410 false, hash);
14411
14412 return r;
14413 }
14414
14415 /* True if FN is the op() for a lambda in an uninstantiated template. */
14416
14417 bool
14418 lambda_fn_in_template_p (tree fn)
14419 {
14420 if (!fn || !LAMBDA_FUNCTION_P (fn))
14421 return false;
14422 tree closure = DECL_CONTEXT (fn);
14423 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14424 }
14425
14426 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14427 which the above is true. */
14428
14429 bool
14430 regenerated_lambda_fn_p (tree fn)
14431 {
14432 if (!fn || !LAMBDA_FUNCTION_P (fn))
14433 return false;
14434 tree closure = DECL_CONTEXT (fn);
14435 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14436 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
14437 }
14438
14439 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14440 If T is not a regenerated LAMBDA_EXPR, return T. */
14441
14442 tree
14443 most_general_lambda (tree t)
14444 {
14445 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14446 t = TI_TEMPLATE (ti);
14447 return t;
14448 }
14449
14450 /* We're instantiating a variable from template function TCTX. Return the
14451 corresponding current enclosing scope. This gets complicated because lambda
14452 functions in templates are regenerated rather than instantiated, but generic
14453 lambda functions are subsequently instantiated. */
14454
14455 static tree
14456 enclosing_instantiation_of (tree otctx)
14457 {
14458 tree tctx = otctx;
14459 tree fn = current_function_decl;
14460 int lambda_count = 0;
14461
14462 for (; tctx && (lambda_fn_in_template_p (tctx)
14463 || regenerated_lambda_fn_p (tctx));
14464 tctx = decl_function_context (tctx))
14465 ++lambda_count;
14466
14467 if (!tctx)
14468 {
14469 /* Match using DECL_SOURCE_LOCATION, which is unique for all lambdas.
14470
14471 For GCC 11 the above condition limits this to the previously failing
14472 case where all enclosing functions are lambdas (95870). FIXME. */
14473 for (tree ofn = fn; ofn; ofn = decl_function_context (ofn))
14474 if (DECL_SOURCE_LOCATION (ofn) == DECL_SOURCE_LOCATION (otctx))
14475 return ofn;
14476 gcc_unreachable ();
14477 }
14478
14479 for (; fn; fn = decl_function_context (fn))
14480 {
14481 tree ofn = fn;
14482 int flambda_count = 0;
14483 for (; fn && regenerated_lambda_fn_p (fn);
14484 fn = decl_function_context (fn))
14485 ++flambda_count;
14486 if ((fn && DECL_TEMPLATE_INFO (fn))
14487 ? most_general_template (fn) != most_general_template (tctx)
14488 : fn != tctx)
14489 continue;
14490 if (flambda_count != lambda_count)
14491 {
14492 gcc_assert (flambda_count > lambda_count);
14493 for (; flambda_count > lambda_count; --flambda_count)
14494 ofn = decl_function_context (ofn);
14495 }
14496 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14497 || DECL_CONV_FN_P (ofn));
14498 return ofn;
14499 }
14500 gcc_unreachable ();
14501 }
14502
14503 /* Substitute the ARGS into the T, which is a _DECL. Return the
14504 result of the substitution. Issue error and warning messages under
14505 control of COMPLAIN. */
14506
14507 static tree
14508 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14509 {
14510 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14511 location_t saved_loc;
14512 tree r = NULL_TREE;
14513 tree in_decl = t;
14514 hashval_t hash = 0;
14515
14516 /* Set the filename and linenumber to improve error-reporting. */
14517 saved_loc = input_location;
14518 input_location = DECL_SOURCE_LOCATION (t);
14519
14520 switch (TREE_CODE (t))
14521 {
14522 case TEMPLATE_DECL:
14523 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14524 break;
14525
14526 case FUNCTION_DECL:
14527 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14528 break;
14529
14530 case PARM_DECL:
14531 {
14532 tree type = NULL_TREE;
14533 int i, len = 1;
14534 tree expanded_types = NULL_TREE;
14535 tree prev_r = NULL_TREE;
14536 tree first_r = NULL_TREE;
14537
14538 if (DECL_PACK_P (t))
14539 {
14540 /* If there is a local specialization that isn't a
14541 parameter pack, it means that we're doing a "simple"
14542 substitution from inside tsubst_pack_expansion. Just
14543 return the local specialization (which will be a single
14544 parm). */
14545 tree spec = retrieve_local_specialization (t);
14546 if (spec
14547 && TREE_CODE (spec) == PARM_DECL
14548 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14549 RETURN (spec);
14550
14551 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14552 the parameters in this function parameter pack. */
14553 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14554 complain, in_decl);
14555 if (TREE_CODE (expanded_types) == TREE_VEC)
14556 {
14557 len = TREE_VEC_LENGTH (expanded_types);
14558
14559 /* Zero-length parameter packs are boring. Just substitute
14560 into the chain. */
14561 if (len == 0 && !cp_unevaluated_operand)
14562 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14563 TREE_CHAIN (t)));
14564 }
14565 else
14566 {
14567 /* All we did was update the type. Make a note of that. */
14568 type = expanded_types;
14569 expanded_types = NULL_TREE;
14570 }
14571 }
14572
14573 /* Loop through all of the parameters we'll build. When T is
14574 a function parameter pack, LEN is the number of expanded
14575 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14576 r = NULL_TREE;
14577 for (i = 0; i < len; ++i)
14578 {
14579 prev_r = r;
14580 r = copy_node (t);
14581 if (DECL_TEMPLATE_PARM_P (t))
14582 SET_DECL_TEMPLATE_PARM_P (r);
14583
14584 if (expanded_types)
14585 /* We're on the Ith parameter of the function parameter
14586 pack. */
14587 {
14588 /* Get the Ith type. */
14589 type = TREE_VEC_ELT (expanded_types, i);
14590
14591 /* Rename the parameter to include the index. */
14592 DECL_NAME (r)
14593 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14594 }
14595 else if (!type)
14596 /* We're dealing with a normal parameter. */
14597 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14598
14599 type = type_decays_to (type);
14600 TREE_TYPE (r) = type;
14601 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14602
14603 if (DECL_INITIAL (r))
14604 {
14605 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14606 DECL_INITIAL (r) = TREE_TYPE (r);
14607 else
14608 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14609 complain, in_decl);
14610 }
14611
14612 DECL_CONTEXT (r) = NULL_TREE;
14613
14614 if (!DECL_TEMPLATE_PARM_P (r))
14615 DECL_ARG_TYPE (r) = type_passed_as (type);
14616
14617 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14618 args, complain, in_decl))
14619 return error_mark_node;
14620
14621 /* Keep track of the first new parameter we
14622 generate. That's what will be returned to the
14623 caller. */
14624 if (!first_r)
14625 first_r = r;
14626
14627 /* Build a proper chain of parameters when substituting
14628 into a function parameter pack. */
14629 if (prev_r)
14630 DECL_CHAIN (prev_r) = r;
14631 }
14632
14633 /* If cp_unevaluated_operand is set, we're just looking for a
14634 single dummy parameter, so don't keep going. */
14635 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14636 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14637 complain, DECL_CHAIN (t));
14638
14639 /* FIRST_R contains the start of the chain we've built. */
14640 r = first_r;
14641 }
14642 break;
14643
14644 case FIELD_DECL:
14645 {
14646 tree type = NULL_TREE;
14647 tree vec = NULL_TREE;
14648 tree expanded_types = NULL_TREE;
14649 int len = 1;
14650
14651 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14652 {
14653 /* This field is a lambda capture pack. Return a TREE_VEC of
14654 the expanded fields to instantiate_class_template_1. */
14655 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14656 complain, in_decl);
14657 if (TREE_CODE (expanded_types) == TREE_VEC)
14658 {
14659 len = TREE_VEC_LENGTH (expanded_types);
14660 vec = make_tree_vec (len);
14661 }
14662 else
14663 {
14664 /* All we did was update the type. Make a note of that. */
14665 type = expanded_types;
14666 expanded_types = NULL_TREE;
14667 }
14668 }
14669
14670 for (int i = 0; i < len; ++i)
14671 {
14672 r = copy_decl (t);
14673 if (expanded_types)
14674 {
14675 type = TREE_VEC_ELT (expanded_types, i);
14676 DECL_NAME (r)
14677 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14678 }
14679 else if (!type)
14680 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14681
14682 if (type == error_mark_node)
14683 RETURN (error_mark_node);
14684 TREE_TYPE (r) = type;
14685 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14686
14687 if (DECL_C_BIT_FIELD (r))
14688 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14689 number of bits. */
14690 DECL_BIT_FIELD_REPRESENTATIVE (r)
14691 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14692 complain, in_decl,
14693 /*integral_constant_expression_p=*/true);
14694 if (DECL_INITIAL (t))
14695 {
14696 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14697 NSDMI in perform_member_init. Still set DECL_INITIAL
14698 so that we know there is one. */
14699 DECL_INITIAL (r) = void_node;
14700 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14701 retrofit_lang_decl (r);
14702 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14703 }
14704 /* We don't have to set DECL_CONTEXT here; it is set by
14705 finish_member_declaration. */
14706 DECL_CHAIN (r) = NULL_TREE;
14707
14708 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14709 args, complain, in_decl))
14710 return error_mark_node;
14711
14712 if (vec)
14713 TREE_VEC_ELT (vec, i) = r;
14714 }
14715
14716 if (vec)
14717 r = vec;
14718 }
14719 break;
14720
14721 case USING_DECL:
14722 /* We reach here only for member using decls. We also need to check
14723 uses_template_parms because DECL_DEPENDENT_P is not set for a
14724 using-declaration that designates a member of the current
14725 instantiation (c++/53549). */
14726 if (DECL_DEPENDENT_P (t)
14727 || uses_template_parms (USING_DECL_SCOPE (t)))
14728 {
14729 tree scope = USING_DECL_SCOPE (t);
14730 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14731 if (PACK_EXPANSION_P (scope))
14732 {
14733 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14734 int len = TREE_VEC_LENGTH (vec);
14735 r = make_tree_vec (len);
14736 for (int i = 0; i < len; ++i)
14737 {
14738 tree escope = TREE_VEC_ELT (vec, i);
14739 tree elt = do_class_using_decl (escope, name);
14740 if (!elt)
14741 {
14742 r = error_mark_node;
14743 break;
14744 }
14745 else
14746 {
14747 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14748 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14749 }
14750 TREE_VEC_ELT (r, i) = elt;
14751 }
14752 }
14753 else
14754 {
14755 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14756 complain, in_decl);
14757 r = do_class_using_decl (inst_scope, name);
14758 if (!r)
14759 r = error_mark_node;
14760 else
14761 {
14762 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14763 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14764 }
14765 }
14766 }
14767 else
14768 {
14769 r = copy_node (t);
14770 DECL_CHAIN (r) = NULL_TREE;
14771 }
14772 break;
14773
14774 case TYPE_DECL:
14775 case VAR_DECL:
14776 {
14777 tree argvec = NULL_TREE;
14778 tree gen_tmpl = NULL_TREE;
14779 tree tmpl = NULL_TREE;
14780 tree type = NULL_TREE;
14781
14782 if (TREE_TYPE (t) == error_mark_node)
14783 RETURN (error_mark_node);
14784
14785 if (TREE_CODE (t) == TYPE_DECL
14786 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14787 {
14788 /* If this is the canonical decl, we don't have to
14789 mess with instantiations, and often we can't (for
14790 typename, template type parms and such). Note that
14791 TYPE_NAME is not correct for the above test if
14792 we've copied the type for a typedef. */
14793 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14794 if (type == error_mark_node)
14795 RETURN (error_mark_node);
14796 r = TYPE_NAME (type);
14797 break;
14798 }
14799
14800 /* Check to see if we already have the specialization we
14801 need. */
14802 tree spec = NULL_TREE;
14803 bool local_p = false;
14804 tree ctx = DECL_CONTEXT (t);
14805 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14806 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14807 {
14808 local_p = false;
14809 if (DECL_CLASS_SCOPE_P (t))
14810 {
14811 ctx = tsubst_aggr_type (ctx, args,
14812 complain,
14813 in_decl, /*entering_scope=*/1);
14814 /* If CTX is unchanged, then T is in fact the
14815 specialization we want. That situation occurs when
14816 referencing a static data member within in its own
14817 class. We can use pointer equality, rather than
14818 same_type_p, because DECL_CONTEXT is always
14819 canonical... */
14820 if (ctx == DECL_CONTEXT (t)
14821 /* ... unless T is a member template; in which
14822 case our caller can be willing to create a
14823 specialization of that template represented
14824 by T. */
14825 && !(DECL_TI_TEMPLATE (t)
14826 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14827 spec = t;
14828 }
14829
14830 if (!spec)
14831 {
14832 tmpl = DECL_TI_TEMPLATE (t);
14833 gen_tmpl = most_general_template (tmpl);
14834 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14835 if (argvec != error_mark_node)
14836 argvec = (coerce_innermost_template_parms
14837 (DECL_TEMPLATE_PARMS (gen_tmpl),
14838 argvec, t, complain,
14839 /*all*/true, /*defarg*/true));
14840 if (argvec == error_mark_node)
14841 RETURN (error_mark_node);
14842 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14843 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14844 }
14845 }
14846 else
14847 {
14848 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14849 /* Subsequent calls to pushdecl will fill this in. */
14850 ctx = NULL_TREE;
14851 /* A local variable. */
14852 local_p = true;
14853 /* Unless this is a reference to a static variable from an
14854 enclosing function, in which case we need to fill it in now. */
14855 if (TREE_STATIC (t))
14856 {
14857 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14858 if (fn != current_function_decl)
14859 ctx = fn;
14860 }
14861 spec = retrieve_local_specialization (t);
14862 }
14863 /* If we already have the specialization we need, there is
14864 nothing more to do. */
14865 if (spec)
14866 {
14867 r = spec;
14868 break;
14869 }
14870
14871 /* Create a new node for the specialization we need. */
14872 if (type == NULL_TREE)
14873 {
14874 if (is_typedef_decl (t))
14875 type = DECL_ORIGINAL_TYPE (t);
14876 else
14877 type = TREE_TYPE (t);
14878 if (VAR_P (t)
14879 && VAR_HAD_UNKNOWN_BOUND (t)
14880 && type != error_mark_node)
14881 type = strip_array_domain (type);
14882 tree sub_args = args;
14883 if (tree auto_node = type_uses_auto (type))
14884 {
14885 /* Mask off any template args past the variable's context so we
14886 don't replace the auto with an unrelated argument. */
14887 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14888 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14889 if (extra > 0)
14890 /* This should never happen with the new lambda instantiation
14891 model, but keep the handling just in case. */
14892 gcc_assert (!CHECKING_P),
14893 sub_args = strip_innermost_template_args (args, extra);
14894 }
14895 type = tsubst (type, sub_args, complain, in_decl);
14896 /* Substituting the type might have recursively instantiated this
14897 same alias (c++/86171). */
14898 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14899 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14900 {
14901 r = spec;
14902 break;
14903 }
14904 }
14905 r = copy_decl (t);
14906 if (VAR_P (r))
14907 {
14908 DECL_INITIALIZED_P (r) = 0;
14909 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14910 if (type == error_mark_node)
14911 RETURN (error_mark_node);
14912 if (TREE_CODE (type) == FUNCTION_TYPE)
14913 {
14914 /* It may seem that this case cannot occur, since:
14915
14916 typedef void f();
14917 void g() { f x; }
14918
14919 declares a function, not a variable. However:
14920
14921 typedef void f();
14922 template <typename T> void g() { T t; }
14923 template void g<f>();
14924
14925 is an attempt to declare a variable with function
14926 type. */
14927 error ("variable %qD has function type",
14928 /* R is not yet sufficiently initialized, so we
14929 just use its name. */
14930 DECL_NAME (r));
14931 RETURN (error_mark_node);
14932 }
14933 type = complete_type (type);
14934 /* Wait until cp_finish_decl to set this again, to handle
14935 circular dependency (template/instantiate6.C). */
14936 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14937 type = check_var_type (DECL_NAME (r), type,
14938 DECL_SOURCE_LOCATION (r));
14939 if (DECL_HAS_VALUE_EXPR_P (t))
14940 {
14941 tree ve = DECL_VALUE_EXPR (t);
14942 /* If the DECL_VALUE_EXPR is converted to the declared type,
14943 preserve the identity so that gimplify_type_sizes works. */
14944 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14945 if (nop)
14946 ve = TREE_OPERAND (ve, 0);
14947 ve = tsubst_expr (ve, args, complain, in_decl,
14948 /*constant_expression_p=*/false);
14949 if (REFERENCE_REF_P (ve))
14950 {
14951 gcc_assert (TYPE_REF_P (type));
14952 ve = TREE_OPERAND (ve, 0);
14953 }
14954 if (nop)
14955 ve = build_nop (type, ve);
14956 else if (DECL_LANG_SPECIFIC (t)
14957 && DECL_OMP_PRIVATIZED_MEMBER (t)
14958 && TREE_CODE (ve) == COMPONENT_REF
14959 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14960 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14961 type = TREE_TYPE (ve);
14962 else
14963 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14964 == TYPE_MAIN_VARIANT (type));
14965 SET_DECL_VALUE_EXPR (r, ve);
14966 }
14967 if (CP_DECL_THREAD_LOCAL_P (r)
14968 && !processing_template_decl)
14969 set_decl_tls_model (r, decl_default_tls_model (r));
14970 }
14971 else if (DECL_SELF_REFERENCE_P (t))
14972 SET_DECL_SELF_REFERENCE_P (r);
14973 TREE_TYPE (r) = type;
14974 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14975 DECL_CONTEXT (r) = ctx;
14976 /* Clear out the mangled name and RTL for the instantiation. */
14977 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14978 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14979 SET_DECL_RTL (r, NULL);
14980 set_instantiating_module (r);
14981
14982 /* The initializer must not be expanded until it is required;
14983 see [temp.inst]. */
14984 DECL_INITIAL (r) = NULL_TREE;
14985 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14986 if (VAR_P (r))
14987 {
14988 if (DECL_LANG_SPECIFIC (r))
14989 SET_DECL_DEPENDENT_INIT_P (r, false);
14990
14991 SET_DECL_MODE (r, VOIDmode);
14992
14993 /* Possibly limit visibility based on template args. */
14994 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14995 if (DECL_VISIBILITY_SPECIFIED (t))
14996 {
14997 DECL_VISIBILITY_SPECIFIED (r) = 0;
14998 DECL_ATTRIBUTES (r)
14999 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15000 }
15001 determine_visibility (r);
15002 }
15003
15004 if (!local_p)
15005 {
15006 /* A static data member declaration is always marked
15007 external when it is declared in-class, even if an
15008 initializer is present. We mimic the non-template
15009 processing here. */
15010 DECL_EXTERNAL (r) = 1;
15011 if (DECL_NAMESPACE_SCOPE_P (t))
15012 DECL_NOT_REALLY_EXTERN (r) = 1;
15013
15014 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
15015 SET_DECL_IMPLICIT_INSTANTIATION (r);
15016 if (!error_operand_p (r) || (complain & tf_error))
15017 register_specialization (r, gen_tmpl, argvec, false, hash);
15018 }
15019 else
15020 {
15021 if (DECL_LANG_SPECIFIC (r))
15022 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15023 if (!cp_unevaluated_operand)
15024 register_local_specialization (r, t);
15025 }
15026
15027 DECL_CHAIN (r) = NULL_TREE;
15028
15029 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
15030 /*flags=*/0,
15031 args, complain, in_decl))
15032 return error_mark_node;
15033
15034 /* Preserve a typedef that names a type. */
15035 if (is_typedef_decl (r) && type != error_mark_node)
15036 {
15037 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
15038 set_underlying_type (r);
15039 }
15040
15041 layout_decl (r, 0);
15042 }
15043 break;
15044
15045 default:
15046 gcc_unreachable ();
15047 }
15048 #undef RETURN
15049
15050 out:
15051 /* Restore the file and line information. */
15052 input_location = saved_loc;
15053
15054 return r;
15055 }
15056
15057 /* Substitute into the complete parameter type list PARMS. */
15058
15059 tree
15060 tsubst_function_parms (tree parms,
15061 tree args,
15062 tsubst_flags_t complain,
15063 tree in_decl)
15064 {
15065 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
15066 }
15067
15068 /* Substitute into the ARG_TYPES of a function type.
15069 If END is a TREE_CHAIN, leave it and any following types
15070 un-substituted. */
15071
15072 static tree
15073 tsubst_arg_types (tree arg_types,
15074 tree args,
15075 tree end,
15076 tsubst_flags_t complain,
15077 tree in_decl)
15078 {
15079 tree remaining_arg_types;
15080 tree type = NULL_TREE;
15081 int i = 1;
15082 tree expanded_args = NULL_TREE;
15083 tree default_arg;
15084
15085 if (!arg_types || arg_types == void_list_node || arg_types == end)
15086 return arg_types;
15087
15088 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
15089 args, end, complain, in_decl);
15090 if (remaining_arg_types == error_mark_node)
15091 return error_mark_node;
15092
15093 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
15094 {
15095 /* For a pack expansion, perform substitution on the
15096 entire expression. Later on, we'll handle the arguments
15097 one-by-one. */
15098 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
15099 args, complain, in_decl);
15100
15101 if (TREE_CODE (expanded_args) == TREE_VEC)
15102 /* So that we'll spin through the parameters, one by one. */
15103 i = TREE_VEC_LENGTH (expanded_args);
15104 else
15105 {
15106 /* We only partially substituted into the parameter
15107 pack. Our type is TYPE_PACK_EXPANSION. */
15108 type = expanded_args;
15109 expanded_args = NULL_TREE;
15110 }
15111 }
15112
15113 while (i > 0) {
15114 --i;
15115
15116 if (expanded_args)
15117 type = TREE_VEC_ELT (expanded_args, i);
15118 else if (!type)
15119 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
15120
15121 if (type == error_mark_node)
15122 return error_mark_node;
15123 if (VOID_TYPE_P (type))
15124 {
15125 if (complain & tf_error)
15126 {
15127 error ("invalid parameter type %qT", type);
15128 if (in_decl)
15129 error ("in declaration %q+D", in_decl);
15130 }
15131 return error_mark_node;
15132 }
15133
15134 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15135 top-level qualifiers as required. */
15136 type = cv_unqualified (type_decays_to (type));
15137
15138 /* We do not substitute into default arguments here. The standard
15139 mandates that they be instantiated only when needed, which is
15140 done in build_over_call. */
15141 default_arg = TREE_PURPOSE (arg_types);
15142
15143 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15144 since the new op() won't have any associated template arguments for us
15145 to refer to later. */
15146 if (lambda_fn_in_template_p (in_decl))
15147 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
15148 false/*fn*/, false/*constexpr*/);
15149
15150 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15151 {
15152 /* We've instantiated a template before its default arguments
15153 have been parsed. This can happen for a nested template
15154 class, and is not an error unless we require the default
15155 argument in a call of this function. */
15156 remaining_arg_types =
15157 tree_cons (default_arg, type, remaining_arg_types);
15158 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15159 remaining_arg_types);
15160 }
15161 else
15162 remaining_arg_types =
15163 hash_tree_cons (default_arg, type, remaining_arg_types);
15164 }
15165
15166 return remaining_arg_types;
15167 }
15168
15169 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15170 *not* handle the exception-specification for FNTYPE, because the
15171 initial substitution of explicitly provided template parameters
15172 during argument deduction forbids substitution into the
15173 exception-specification:
15174
15175 [temp.deduct]
15176
15177 All references in the function type of the function template to the
15178 corresponding template parameters are replaced by the specified tem-
15179 plate argument values. If a substitution in a template parameter or
15180 in the function type of the function template results in an invalid
15181 type, type deduction fails. [Note: The equivalent substitution in
15182 exception specifications is done only when the function is instanti-
15183 ated, at which point a program is ill-formed if the substitution
15184 results in an invalid type.] */
15185
15186 static tree
15187 tsubst_function_type (tree t,
15188 tree args,
15189 tsubst_flags_t complain,
15190 tree in_decl)
15191 {
15192 tree return_type;
15193 tree arg_types = NULL_TREE;
15194
15195 /* The TYPE_CONTEXT is not used for function/method types. */
15196 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15197
15198 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15199 failure. */
15200 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15201
15202 if (late_return_type_p)
15203 {
15204 /* Substitute the argument types. */
15205 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15206 complain, in_decl);
15207 if (arg_types == error_mark_node)
15208 return error_mark_node;
15209
15210 tree save_ccp = current_class_ptr;
15211 tree save_ccr = current_class_ref;
15212 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15213 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15214 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15215 if (do_inject)
15216 {
15217 /* DR 1207: 'this' is in scope in the trailing return type. */
15218 inject_this_parameter (this_type, cp_type_quals (this_type));
15219 }
15220
15221 /* Substitute the return type. */
15222 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15223
15224 if (do_inject)
15225 {
15226 current_class_ptr = save_ccp;
15227 current_class_ref = save_ccr;
15228 }
15229 }
15230 else
15231 /* Substitute the return type. */
15232 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15233
15234 if (return_type == error_mark_node)
15235 return error_mark_node;
15236 /* DR 486 clarifies that creation of a function type with an
15237 invalid return type is a deduction failure. */
15238 if (TREE_CODE (return_type) == ARRAY_TYPE
15239 || TREE_CODE (return_type) == FUNCTION_TYPE)
15240 {
15241 if (complain & tf_error)
15242 {
15243 if (TREE_CODE (return_type) == ARRAY_TYPE)
15244 error ("function returning an array");
15245 else
15246 error ("function returning a function");
15247 }
15248 return error_mark_node;
15249 }
15250
15251 if (!late_return_type_p)
15252 {
15253 /* Substitute the argument types. */
15254 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15255 complain, in_decl);
15256 if (arg_types == error_mark_node)
15257 return error_mark_node;
15258 }
15259
15260 /* Construct a new type node and return it. */
15261 return rebuild_function_or_method_type (t, return_type, arg_types,
15262 /*raises=*/NULL_TREE, complain);
15263 }
15264
15265 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15266 ARGS into that specification, and return the substituted
15267 specification. If there is no specification, return NULL_TREE. */
15268
15269 static tree
15270 tsubst_exception_specification (tree fntype,
15271 tree args,
15272 tsubst_flags_t complain,
15273 tree in_decl,
15274 bool defer_ok)
15275 {
15276 tree specs;
15277 tree new_specs;
15278
15279 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15280 new_specs = NULL_TREE;
15281 if (specs && TREE_PURPOSE (specs))
15282 {
15283 /* A noexcept-specifier. */
15284 tree expr = TREE_PURPOSE (specs);
15285 if (TREE_CODE (expr) == INTEGER_CST)
15286 new_specs = expr;
15287 else if (defer_ok)
15288 {
15289 /* Defer instantiation of noexcept-specifiers to avoid
15290 excessive instantiations (c++/49107). */
15291 new_specs = make_node (DEFERRED_NOEXCEPT);
15292 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15293 {
15294 /* We already partially instantiated this member template,
15295 so combine the new args with the old. */
15296 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15297 = DEFERRED_NOEXCEPT_PATTERN (expr);
15298 DEFERRED_NOEXCEPT_ARGS (new_specs)
15299 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15300 }
15301 else
15302 {
15303 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15304 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15305 }
15306 }
15307 else
15308 {
15309 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15310 {
15311 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15312 args);
15313 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15314 }
15315 new_specs = tsubst_copy_and_build
15316 (expr, args, complain, in_decl, /*function_p=*/false,
15317 /*integral_constant_expression_p=*/true);
15318 }
15319 new_specs = build_noexcept_spec (new_specs, complain);
15320 /* We've instantiated a template before a noexcept-specifier
15321 contained therein has been parsed. This can happen for
15322 a nested template class:
15323
15324 struct S {
15325 template<typename> struct B { B() noexcept(...); };
15326 struct A : B<int> { ... use B() ... };
15327 };
15328
15329 where completing B<int> will trigger instantiating the
15330 noexcept, even though we only parse it at the end of S. */
15331 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
15332 {
15333 gcc_checking_assert (defer_ok);
15334 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
15335 }
15336 }
15337 else if (specs)
15338 {
15339 if (! TREE_VALUE (specs))
15340 new_specs = specs;
15341 else
15342 while (specs)
15343 {
15344 tree spec;
15345 int i, len = 1;
15346 tree expanded_specs = NULL_TREE;
15347
15348 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15349 {
15350 /* Expand the pack expansion type. */
15351 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15352 args, complain,
15353 in_decl);
15354
15355 if (expanded_specs == error_mark_node)
15356 return error_mark_node;
15357 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15358 len = TREE_VEC_LENGTH (expanded_specs);
15359 else
15360 {
15361 /* We're substituting into a member template, so
15362 we got a TYPE_PACK_EXPANSION back. Add that
15363 expansion and move on. */
15364 gcc_assert (TREE_CODE (expanded_specs)
15365 == TYPE_PACK_EXPANSION);
15366 new_specs = add_exception_specifier (new_specs,
15367 expanded_specs,
15368 complain);
15369 specs = TREE_CHAIN (specs);
15370 continue;
15371 }
15372 }
15373
15374 for (i = 0; i < len; ++i)
15375 {
15376 if (expanded_specs)
15377 spec = TREE_VEC_ELT (expanded_specs, i);
15378 else
15379 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15380 if (spec == error_mark_node)
15381 return spec;
15382 new_specs = add_exception_specifier (new_specs, spec,
15383 complain);
15384 }
15385
15386 specs = TREE_CHAIN (specs);
15387 }
15388 }
15389 return new_specs;
15390 }
15391
15392 /* Substitute through a TREE_LIST of types or expressions, handling pack
15393 expansions. */
15394
15395 tree
15396 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15397 {
15398 if (t == void_list_node)
15399 return t;
15400
15401 tree purpose = TREE_PURPOSE (t);
15402 tree purposevec = NULL_TREE;
15403 if (!purpose)
15404 ;
15405 else if (PACK_EXPANSION_P (purpose))
15406 {
15407 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15408 if (TREE_CODE (purpose) == TREE_VEC)
15409 purposevec = purpose;
15410 }
15411 else if (TYPE_P (purpose))
15412 purpose = tsubst (purpose, args, complain, in_decl);
15413 else
15414 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15415 if (purpose == error_mark_node || purposevec == error_mark_node)
15416 return error_mark_node;
15417
15418 tree value = TREE_VALUE (t);
15419 tree valuevec = NULL_TREE;
15420 if (!value)
15421 ;
15422 else if (PACK_EXPANSION_P (value))
15423 {
15424 value = tsubst_pack_expansion (value, args, complain, in_decl);
15425 if (TREE_CODE (value) == TREE_VEC)
15426 valuevec = value;
15427 }
15428 else if (TYPE_P (value))
15429 value = tsubst (value, args, complain, in_decl);
15430 else
15431 value = tsubst_copy_and_build (value, args, complain, in_decl);
15432 if (value == error_mark_node || valuevec == error_mark_node)
15433 return error_mark_node;
15434
15435 tree chain = TREE_CHAIN (t);
15436 if (!chain)
15437 ;
15438 else if (TREE_CODE (chain) == TREE_LIST)
15439 chain = tsubst_tree_list (chain, args, complain, in_decl);
15440 else if (TYPE_P (chain))
15441 chain = tsubst (chain, args, complain, in_decl);
15442 else
15443 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15444 if (chain == error_mark_node)
15445 return error_mark_node;
15446
15447 if (purpose == TREE_PURPOSE (t)
15448 && value == TREE_VALUE (t)
15449 && chain == TREE_CHAIN (t))
15450 return t;
15451
15452 int len;
15453 /* Determine the number of arguments. */
15454 if (purposevec)
15455 {
15456 len = TREE_VEC_LENGTH (purposevec);
15457 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15458 }
15459 else if (valuevec)
15460 len = TREE_VEC_LENGTH (valuevec);
15461 else
15462 len = 1;
15463
15464 for (int i = len; i-- > 0; )
15465 {
15466 if (purposevec)
15467 purpose = TREE_VEC_ELT (purposevec, i);
15468 if (valuevec)
15469 value = TREE_VEC_ELT (valuevec, i);
15470
15471 if (value && TYPE_P (value))
15472 chain = hash_tree_cons (purpose, value, chain);
15473 else
15474 chain = tree_cons (purpose, value, chain);
15475 }
15476
15477 return chain;
15478 }
15479
15480 /* Take the tree structure T and replace template parameters used
15481 therein with the argument vector ARGS. IN_DECL is an associated
15482 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15483 Issue error and warning messages under control of COMPLAIN. Note
15484 that we must be relatively non-tolerant of extensions here, in
15485 order to preserve conformance; if we allow substitutions that
15486 should not be allowed, we may allow argument deductions that should
15487 not succeed, and therefore report ambiguous overload situations
15488 where there are none. In theory, we could allow the substitution,
15489 but indicate that it should have failed, and allow our caller to
15490 make sure that the right thing happens, but we don't try to do this
15491 yet.
15492
15493 This function is used for dealing with types, decls and the like;
15494 for expressions, use tsubst_expr or tsubst_copy. */
15495
15496 tree
15497 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15498 {
15499 enum tree_code code;
15500 tree type, r = NULL_TREE;
15501
15502 if (t == NULL_TREE || t == error_mark_node
15503 || t == integer_type_node
15504 || t == void_type_node
15505 || t == char_type_node
15506 || t == unknown_type_node
15507 || TREE_CODE (t) == NAMESPACE_DECL
15508 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15509 return t;
15510
15511 if (DECL_P (t))
15512 return tsubst_decl (t, args, complain);
15513
15514 if (args == NULL_TREE)
15515 return t;
15516
15517 code = TREE_CODE (t);
15518
15519 gcc_assert (code != IDENTIFIER_NODE);
15520 type = TREE_TYPE (t);
15521
15522 gcc_assert (type != unknown_type_node);
15523
15524 /* Reuse typedefs. We need to do this to handle dependent attributes,
15525 such as attribute aligned. */
15526 if (TYPE_P (t)
15527 && typedef_variant_p (t))
15528 {
15529 tree decl = TYPE_NAME (t);
15530
15531 if (alias_template_specialization_p (t, nt_opaque))
15532 {
15533 /* DECL represents an alias template and we want to
15534 instantiate it. */
15535 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15536 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15537 r = instantiate_alias_template (tmpl, gen_args, complain);
15538 }
15539 else if (DECL_CLASS_SCOPE_P (decl)
15540 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15541 && uses_template_parms (DECL_CONTEXT (decl)))
15542 {
15543 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15544 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15545 r = retrieve_specialization (tmpl, gen_args, 0);
15546 }
15547 else if (DECL_FUNCTION_SCOPE_P (decl)
15548 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15549 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15550 r = retrieve_local_specialization (decl);
15551 else
15552 /* The typedef is from a non-template context. */
15553 return t;
15554
15555 if (r)
15556 {
15557 r = TREE_TYPE (r);
15558 r = cp_build_qualified_type_real
15559 (r, cp_type_quals (t) | cp_type_quals (r),
15560 complain | tf_ignore_bad_quals);
15561 return r;
15562 }
15563 else
15564 {
15565 /* We don't have an instantiation yet, so drop the typedef. */
15566 int quals = cp_type_quals (t);
15567 t = DECL_ORIGINAL_TYPE (decl);
15568 t = cp_build_qualified_type_real (t, quals,
15569 complain | tf_ignore_bad_quals);
15570 }
15571 }
15572
15573 bool fndecl_type = (complain & tf_fndecl_type);
15574 complain &= ~tf_fndecl_type;
15575
15576 if (type
15577 && code != TYPENAME_TYPE
15578 && code != TEMPLATE_TYPE_PARM
15579 && code != TEMPLATE_PARM_INDEX
15580 && code != IDENTIFIER_NODE
15581 && code != FUNCTION_TYPE
15582 && code != METHOD_TYPE)
15583 type = tsubst (type, args, complain, in_decl);
15584 if (type == error_mark_node)
15585 return error_mark_node;
15586
15587 switch (code)
15588 {
15589 case RECORD_TYPE:
15590 case UNION_TYPE:
15591 case ENUMERAL_TYPE:
15592 return tsubst_aggr_type (t, args, complain, in_decl,
15593 /*entering_scope=*/0);
15594
15595 case ERROR_MARK:
15596 case IDENTIFIER_NODE:
15597 case VOID_TYPE:
15598 case OPAQUE_TYPE:
15599 case REAL_TYPE:
15600 case COMPLEX_TYPE:
15601 case VECTOR_TYPE:
15602 case BOOLEAN_TYPE:
15603 case NULLPTR_TYPE:
15604 case LANG_TYPE:
15605 return t;
15606
15607 case INTEGER_TYPE:
15608 if (t == integer_type_node)
15609 return t;
15610
15611 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15612 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15613 return t;
15614
15615 {
15616 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15617
15618 max = tsubst_expr (omax, args, complain, in_decl,
15619 /*integral_constant_expression_p=*/false);
15620
15621 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15622 needed. */
15623 if (TREE_CODE (max) == NOP_EXPR
15624 && TREE_SIDE_EFFECTS (omax)
15625 && !TREE_TYPE (max))
15626 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15627
15628 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15629 with TREE_SIDE_EFFECTS that indicates this is not an integral
15630 constant expression. */
15631 if (processing_template_decl
15632 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15633 {
15634 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15635 TREE_SIDE_EFFECTS (max) = 1;
15636 }
15637
15638 return compute_array_index_type (NULL_TREE, max, complain);
15639 }
15640
15641 case TEMPLATE_TYPE_PARM:
15642 case TEMPLATE_TEMPLATE_PARM:
15643 case BOUND_TEMPLATE_TEMPLATE_PARM:
15644 case TEMPLATE_PARM_INDEX:
15645 {
15646 int idx;
15647 int level;
15648 int levels;
15649 tree arg = NULL_TREE;
15650
15651 r = NULL_TREE;
15652
15653 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15654 template_parm_level_and_index (t, &level, &idx);
15655
15656 levels = TMPL_ARGS_DEPTH (args);
15657 if (level <= levels
15658 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15659 {
15660 arg = TMPL_ARG (args, level, idx);
15661
15662 /* See through ARGUMENT_PACK_SELECT arguments. */
15663 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15664 arg = argument_pack_select_arg (arg);
15665 }
15666
15667 if (arg == error_mark_node)
15668 return error_mark_node;
15669 else if (arg != NULL_TREE)
15670 {
15671 if (ARGUMENT_PACK_P (arg))
15672 /* If ARG is an argument pack, we don't actually want to
15673 perform a substitution here, because substitutions
15674 for argument packs are only done
15675 element-by-element. We can get to this point when
15676 substituting the type of a non-type template
15677 parameter pack, when that type actually contains
15678 template parameter packs from an outer template, e.g.,
15679
15680 template<typename... Types> struct A {
15681 template<Types... Values> struct B { };
15682 }; */
15683 return t;
15684
15685 if (code == TEMPLATE_TYPE_PARM)
15686 {
15687 int quals;
15688
15689 /* When building concept checks for the purpose of
15690 deducing placeholders, we can end up with wildcards
15691 where types are expected. Adjust this to the deduced
15692 value. */
15693 if (TREE_CODE (arg) == WILDCARD_DECL)
15694 arg = TREE_TYPE (TREE_TYPE (arg));
15695
15696 gcc_assert (TYPE_P (arg));
15697
15698 quals = cp_type_quals (arg) | cp_type_quals (t);
15699
15700 return cp_build_qualified_type_real
15701 (arg, quals, complain | tf_ignore_bad_quals);
15702 }
15703 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15704 {
15705 /* We are processing a type constructed from a
15706 template template parameter. */
15707 tree argvec = tsubst (TYPE_TI_ARGS (t),
15708 args, complain, in_decl);
15709 if (argvec == error_mark_node)
15710 return error_mark_node;
15711
15712 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15713 || TREE_CODE (arg) == TEMPLATE_DECL
15714 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15715
15716 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15717 /* Consider this code:
15718
15719 template <template <class> class Template>
15720 struct Internal {
15721 template <class Arg> using Bind = Template<Arg>;
15722 };
15723
15724 template <template <class> class Template, class Arg>
15725 using Instantiate = Template<Arg>; //#0
15726
15727 template <template <class> class Template,
15728 class Argument>
15729 using Bind =
15730 Instantiate<Internal<Template>::template Bind,
15731 Argument>; //#1
15732
15733 When #1 is parsed, the
15734 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15735 parameter `Template' in #0 matches the
15736 UNBOUND_CLASS_TEMPLATE representing the argument
15737 `Internal<Template>::template Bind'; We then want
15738 to assemble the type `Bind<Argument>' that can't
15739 be fully created right now, because
15740 `Internal<Template>' not being complete, the Bind
15741 template cannot be looked up in that context. So
15742 we need to "store" `Bind<Argument>' for later
15743 when the context of Bind becomes complete. Let's
15744 store that in a TYPENAME_TYPE. */
15745 return make_typename_type (TYPE_CONTEXT (arg),
15746 build_nt (TEMPLATE_ID_EXPR,
15747 TYPE_IDENTIFIER (arg),
15748 argvec),
15749 typename_type,
15750 complain);
15751
15752 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15753 are resolving nested-types in the signature of a
15754 member function templates. Otherwise ARG is a
15755 TEMPLATE_DECL and is the real template to be
15756 instantiated. */
15757 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15758 arg = TYPE_NAME (arg);
15759
15760 r = lookup_template_class (arg,
15761 argvec, in_decl,
15762 DECL_CONTEXT (arg),
15763 /*entering_scope=*/0,
15764 complain);
15765 return cp_build_qualified_type_real
15766 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15767 }
15768 else if (code == TEMPLATE_TEMPLATE_PARM)
15769 return arg;
15770 else
15771 /* TEMPLATE_PARM_INDEX. */
15772 return convert_from_reference (unshare_expr (arg));
15773 }
15774
15775 if (level == 1)
15776 /* This can happen during the attempted tsubst'ing in
15777 unify. This means that we don't yet have any information
15778 about the template parameter in question. */
15779 return t;
15780
15781 /* Early in template argument deduction substitution, we don't
15782 want to reduce the level of 'auto', or it will be confused
15783 with a normal template parm in subsequent deduction.
15784 Similarly, don't reduce the level of template parameters to
15785 avoid mismatches when deducing their types. */
15786 if (complain & tf_partial)
15787 return t;
15788
15789 /* If we get here, we must have been looking at a parm for a
15790 more deeply nested template. Make a new version of this
15791 template parameter, but with a lower level. */
15792 switch (code)
15793 {
15794 case TEMPLATE_TYPE_PARM:
15795 case TEMPLATE_TEMPLATE_PARM:
15796 case BOUND_TEMPLATE_TEMPLATE_PARM:
15797 if (cp_type_quals (t))
15798 {
15799 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15800 r = cp_build_qualified_type_real
15801 (r, cp_type_quals (t),
15802 complain | (code == TEMPLATE_TYPE_PARM
15803 ? tf_ignore_bad_quals : 0));
15804 }
15805 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15806 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15807 && (r = (TEMPLATE_PARM_DESCENDANTS
15808 (TEMPLATE_TYPE_PARM_INDEX (t))))
15809 && (r = TREE_TYPE (r))
15810 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r))
15811 /* Break infinite recursion when substituting the constraints
15812 of a constrained placeholder. */;
15813 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15814 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15815 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15816 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15817 r = TEMPLATE_PARM_DESCENDANTS (arg))
15818 && (TEMPLATE_PARM_LEVEL (r)
15819 == TEMPLATE_PARM_LEVEL (arg) - levels))
15820 /* Cache the simple case of lowering a type parameter. */
15821 r = TREE_TYPE (r);
15822 else
15823 {
15824 r = copy_type (t);
15825 TEMPLATE_TYPE_PARM_INDEX (r)
15826 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15827 r, levels, args, complain);
15828 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15829 TYPE_MAIN_VARIANT (r) = r;
15830 TYPE_POINTER_TO (r) = NULL_TREE;
15831 TYPE_REFERENCE_TO (r) = NULL_TREE;
15832
15833 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15834 {
15835 /* Propagate constraints on placeholders since they are
15836 only instantiated during satisfaction. */
15837 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
15838 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
15839 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15840 {
15841 pl = tsubst_copy (pl, args, complain, in_decl);
15842 if (TREE_CODE (pl) == TEMPLATE_TEMPLATE_PARM)
15843 pl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl);
15844 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15845 }
15846 }
15847
15848 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15849 /* We have reduced the level of the template
15850 template parameter, but not the levels of its
15851 template parameters, so canonical_type_parameter
15852 will not be able to find the canonical template
15853 template parameter for this level. Thus, we
15854 require structural equality checking to compare
15855 TEMPLATE_TEMPLATE_PARMs. */
15856 SET_TYPE_STRUCTURAL_EQUALITY (r);
15857 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15858 SET_TYPE_STRUCTURAL_EQUALITY (r);
15859 else
15860 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15861
15862 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15863 {
15864 tree tinfo = TYPE_TEMPLATE_INFO (t);
15865 /* We might need to substitute into the types of non-type
15866 template parameters. */
15867 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15868 complain, in_decl);
15869 if (tmpl == error_mark_node)
15870 return error_mark_node;
15871 tree argvec = tsubst (TI_ARGS (tinfo), args,
15872 complain, in_decl);
15873 if (argvec == error_mark_node)
15874 return error_mark_node;
15875
15876 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15877 = build_template_info (tmpl, argvec);
15878 }
15879 }
15880 break;
15881
15882 case TEMPLATE_PARM_INDEX:
15883 /* OK, now substitute the type of the non-type parameter. We
15884 couldn't do it earlier because it might be an auto parameter,
15885 and we wouldn't need to if we had an argument. */
15886 type = tsubst (type, args, complain, in_decl);
15887 if (type == error_mark_node)
15888 return error_mark_node;
15889 r = reduce_template_parm_level (t, type, levels, args, complain);
15890 break;
15891
15892 default:
15893 gcc_unreachable ();
15894 }
15895
15896 return r;
15897 }
15898
15899 case TREE_LIST:
15900 return tsubst_tree_list (t, args, complain, in_decl);
15901
15902 case TREE_BINFO:
15903 /* We should never be tsubsting a binfo. */
15904 gcc_unreachable ();
15905
15906 case TREE_VEC:
15907 /* A vector of template arguments. */
15908 gcc_assert (!type);
15909 return tsubst_template_args (t, args, complain, in_decl);
15910
15911 case POINTER_TYPE:
15912 case REFERENCE_TYPE:
15913 {
15914 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15915 return t;
15916
15917 /* [temp.deduct]
15918
15919 Type deduction may fail for any of the following
15920 reasons:
15921
15922 -- Attempting to create a pointer to reference type.
15923 -- Attempting to create a reference to a reference type or
15924 a reference to void.
15925
15926 Core issue 106 says that creating a reference to a reference
15927 during instantiation is no longer a cause for failure. We
15928 only enforce this check in strict C++98 mode. */
15929 if ((TYPE_REF_P (type)
15930 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15931 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15932 {
15933 static location_t last_loc;
15934
15935 /* We keep track of the last time we issued this error
15936 message to avoid spewing a ton of messages during a
15937 single bad template instantiation. */
15938 if (complain & tf_error
15939 && last_loc != input_location)
15940 {
15941 if (VOID_TYPE_P (type))
15942 error ("forming reference to void");
15943 else if (code == POINTER_TYPE)
15944 error ("forming pointer to reference type %qT", type);
15945 else
15946 error ("forming reference to reference type %qT", type);
15947 last_loc = input_location;
15948 }
15949
15950 return error_mark_node;
15951 }
15952 else if (TREE_CODE (type) == FUNCTION_TYPE
15953 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15954 || type_memfn_rqual (type) != REF_QUAL_NONE))
15955 {
15956 if (complain & tf_error)
15957 {
15958 if (code == POINTER_TYPE)
15959 error ("forming pointer to qualified function type %qT",
15960 type);
15961 else
15962 error ("forming reference to qualified function type %qT",
15963 type);
15964 }
15965 return error_mark_node;
15966 }
15967 else if (code == POINTER_TYPE)
15968 {
15969 r = build_pointer_type (type);
15970 if (TREE_CODE (type) == METHOD_TYPE)
15971 r = build_ptrmemfunc_type (r);
15972 }
15973 else if (TYPE_REF_P (type))
15974 /* In C++0x, during template argument substitution, when there is an
15975 attempt to create a reference to a reference type, reference
15976 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15977
15978 "If a template-argument for a template-parameter T names a type
15979 that is a reference to a type A, an attempt to create the type
15980 'lvalue reference to cv T' creates the type 'lvalue reference to
15981 A,' while an attempt to create the type type rvalue reference to
15982 cv T' creates the type T"
15983 */
15984 r = cp_build_reference_type
15985 (TREE_TYPE (type),
15986 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15987 else
15988 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15989 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15990
15991 if (r != error_mark_node)
15992 /* Will this ever be needed for TYPE_..._TO values? */
15993 layout_type (r);
15994
15995 return r;
15996 }
15997 case OFFSET_TYPE:
15998 {
15999 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
16000 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
16001 {
16002 /* [temp.deduct]
16003
16004 Type deduction may fail for any of the following
16005 reasons:
16006
16007 -- Attempting to create "pointer to member of T" when T
16008 is not a class type. */
16009 if (complain & tf_error)
16010 error ("creating pointer to member of non-class type %qT", r);
16011 return error_mark_node;
16012 }
16013 if (TYPE_REF_P (type))
16014 {
16015 if (complain & tf_error)
16016 error ("creating pointer to member reference type %qT", type);
16017 return error_mark_node;
16018 }
16019 if (VOID_TYPE_P (type))
16020 {
16021 if (complain & tf_error)
16022 error ("creating pointer to member of type void");
16023 return error_mark_node;
16024 }
16025 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
16026 if (TREE_CODE (type) == FUNCTION_TYPE)
16027 {
16028 /* The type of the implicit object parameter gets its
16029 cv-qualifiers from the FUNCTION_TYPE. */
16030 tree memptr;
16031 tree method_type
16032 = build_memfn_type (type, r, type_memfn_quals (type),
16033 type_memfn_rqual (type));
16034 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
16035 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
16036 complain);
16037 }
16038 else
16039 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
16040 cp_type_quals (t),
16041 complain);
16042 }
16043 case FUNCTION_TYPE:
16044 case METHOD_TYPE:
16045 {
16046 tree fntype;
16047 tree specs;
16048 fntype = tsubst_function_type (t, args, complain, in_decl);
16049 if (fntype == error_mark_node)
16050 return error_mark_node;
16051
16052 /* Substitute the exception specification. */
16053 specs = tsubst_exception_specification (t, args, complain, in_decl,
16054 /*defer_ok*/fndecl_type);
16055 if (specs == error_mark_node)
16056 return error_mark_node;
16057 if (specs)
16058 fntype = build_exception_variant (fntype, specs);
16059 return fntype;
16060 }
16061 case ARRAY_TYPE:
16062 {
16063 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
16064 if (domain == error_mark_node)
16065 return error_mark_node;
16066
16067 /* As an optimization, we avoid regenerating the array type if
16068 it will obviously be the same as T. */
16069 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
16070 return t;
16071
16072 /* These checks should match the ones in create_array_type_for_decl.
16073
16074 [temp.deduct]
16075
16076 The deduction may fail for any of the following reasons:
16077
16078 -- Attempting to create an array with an element type that
16079 is void, a function type, or a reference type, or [DR337]
16080 an abstract class type. */
16081 if (VOID_TYPE_P (type)
16082 || TREE_CODE (type) == FUNCTION_TYPE
16083 || (TREE_CODE (type) == ARRAY_TYPE
16084 && TYPE_DOMAIN (type) == NULL_TREE)
16085 || TYPE_REF_P (type))
16086 {
16087 if (complain & tf_error)
16088 error ("creating array of %qT", type);
16089 return error_mark_node;
16090 }
16091
16092 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
16093 !(complain & tf_error)))
16094 return error_mark_node;
16095
16096 r = build_cplus_array_type (type, domain);
16097
16098 if (!valid_array_size_p (input_location, r, in_decl,
16099 (complain & tf_error)))
16100 return error_mark_node;
16101
16102 if (TYPE_USER_ALIGN (t))
16103 {
16104 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
16105 TYPE_USER_ALIGN (r) = 1;
16106 }
16107
16108 return r;
16109 }
16110
16111 case TYPENAME_TYPE:
16112 {
16113 tree ctx = TYPE_CONTEXT (t);
16114 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
16115 {
16116 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
16117 if (ctx == error_mark_node
16118 || TREE_VEC_LENGTH (ctx) > 1)
16119 return error_mark_node;
16120 if (TREE_VEC_LENGTH (ctx) == 0)
16121 {
16122 if (complain & tf_error)
16123 error ("%qD is instantiated for an empty pack",
16124 TYPENAME_TYPE_FULLNAME (t));
16125 return error_mark_node;
16126 }
16127 ctx = TREE_VEC_ELT (ctx, 0);
16128 }
16129 else
16130 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
16131 /*entering_scope=*/1);
16132 if (ctx == error_mark_node)
16133 return error_mark_node;
16134
16135 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
16136 complain, in_decl);
16137 if (f == error_mark_node)
16138 return error_mark_node;
16139
16140 if (!MAYBE_CLASS_TYPE_P (ctx))
16141 {
16142 if (complain & tf_error)
16143 error ("%qT is not a class, struct, or union type", ctx);
16144 return error_mark_node;
16145 }
16146 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16147 {
16148 /* Normally, make_typename_type does not require that the CTX
16149 have complete type in order to allow things like:
16150
16151 template <class T> struct S { typename S<T>::X Y; };
16152
16153 But, such constructs have already been resolved by this
16154 point, so here CTX really should have complete type, unless
16155 it's a partial instantiation. */
16156 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16157 return error_mark_node;
16158 }
16159
16160 f = make_typename_type (ctx, f, typename_type,
16161 complain | tf_keep_type_decl);
16162 if (f == error_mark_node)
16163 return f;
16164 if (TREE_CODE (f) == TYPE_DECL)
16165 {
16166 complain |= tf_ignore_bad_quals;
16167 f = TREE_TYPE (f);
16168 }
16169
16170 if (TREE_CODE (f) != TYPENAME_TYPE)
16171 {
16172 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16173 {
16174 if (complain & tf_error)
16175 error ("%qT resolves to %qT, which is not an enumeration type",
16176 t, f);
16177 else
16178 return error_mark_node;
16179 }
16180 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16181 {
16182 if (complain & tf_error)
16183 error ("%qT resolves to %qT, which is not a class type",
16184 t, f);
16185 else
16186 return error_mark_node;
16187 }
16188 }
16189
16190 return cp_build_qualified_type_real
16191 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16192 }
16193
16194 case UNBOUND_CLASS_TEMPLATE:
16195 {
16196 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16197 in_decl, /*entering_scope=*/1);
16198 tree name = TYPE_IDENTIFIER (t);
16199 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16200
16201 if (ctx == error_mark_node || name == error_mark_node)
16202 return error_mark_node;
16203
16204 if (parm_list)
16205 parm_list = tsubst_template_parms (parm_list, args, complain);
16206 return make_unbound_class_template (ctx, name, parm_list, complain);
16207 }
16208
16209 case TYPEOF_TYPE:
16210 {
16211 tree type;
16212
16213 ++cp_unevaluated_operand;
16214 ++c_inhibit_evaluation_warnings;
16215
16216 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16217 complain, in_decl,
16218 /*integral_constant_expression_p=*/false);
16219
16220 --cp_unevaluated_operand;
16221 --c_inhibit_evaluation_warnings;
16222
16223 type = finish_typeof (type);
16224 return cp_build_qualified_type_real (type,
16225 cp_type_quals (t)
16226 | cp_type_quals (type),
16227 complain);
16228 }
16229
16230 case DECLTYPE_TYPE:
16231 {
16232 tree type;
16233
16234 ++cp_unevaluated_operand;
16235 ++c_inhibit_evaluation_warnings;
16236
16237 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16238 complain|tf_decltype, in_decl,
16239 /*function_p*/false,
16240 /*integral_constant_expression*/false);
16241
16242 --cp_unevaluated_operand;
16243 --c_inhibit_evaluation_warnings;
16244
16245 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16246 type = lambda_capture_field_type (type,
16247 false /*explicit_init*/,
16248 DECLTYPE_FOR_REF_CAPTURE (t));
16249 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16250 type = lambda_proxy_type (type);
16251 else
16252 {
16253 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16254 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16255 && EXPR_P (type))
16256 /* In a template ~id could be either a complement expression
16257 or an unqualified-id naming a destructor; if instantiating
16258 it produces an expression, it's not an id-expression or
16259 member access. */
16260 id = false;
16261 type = finish_decltype_type (type, id, complain);
16262 }
16263 return cp_build_qualified_type_real (type,
16264 cp_type_quals (t)
16265 | cp_type_quals (type),
16266 complain | tf_ignore_bad_quals);
16267 }
16268
16269 case UNDERLYING_TYPE:
16270 {
16271 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16272 complain, in_decl);
16273 return finish_underlying_type (type);
16274 }
16275
16276 case TYPE_ARGUMENT_PACK:
16277 case NONTYPE_ARGUMENT_PACK:
16278 return tsubst_argument_pack (t, args, complain, in_decl);
16279
16280 case VOID_CST:
16281 case INTEGER_CST:
16282 case REAL_CST:
16283 case STRING_CST:
16284 case PLUS_EXPR:
16285 case MINUS_EXPR:
16286 case NEGATE_EXPR:
16287 case NOP_EXPR:
16288 case INDIRECT_REF:
16289 case ADDR_EXPR:
16290 case CALL_EXPR:
16291 case ARRAY_REF:
16292 case SCOPE_REF:
16293 /* We should use one of the expression tsubsts for these codes. */
16294 gcc_unreachable ();
16295
16296 default:
16297 sorry ("use of %qs in template", get_tree_code_name (code));
16298 return error_mark_node;
16299 }
16300 }
16301
16302 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16303 expression on the left-hand side of the "." or "->" operator. We
16304 only do the lookup if we had a dependent BASELINK. Otherwise we
16305 adjust it onto the instantiated heirarchy. */
16306
16307 static tree
16308 tsubst_baselink (tree baselink, tree object_type,
16309 tree args, tsubst_flags_t complain, tree in_decl)
16310 {
16311 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16312 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16313 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16314
16315 tree optype = BASELINK_OPTYPE (baselink);
16316 optype = tsubst (optype, args, complain, in_decl);
16317
16318 tree template_args = NULL_TREE;
16319 bool template_id_p = false;
16320 tree fns = BASELINK_FUNCTIONS (baselink);
16321 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16322 {
16323 template_id_p = true;
16324 template_args = TREE_OPERAND (fns, 1);
16325 fns = TREE_OPERAND (fns, 0);
16326 if (template_args)
16327 template_args = tsubst_template_args (template_args, args,
16328 complain, in_decl);
16329 }
16330
16331 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16332 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16333 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16334
16335 if (dependent_p)
16336 {
16337 tree name = OVL_NAME (fns);
16338 if (IDENTIFIER_CONV_OP_P (name))
16339 name = make_conv_op_name (optype);
16340
16341 /* See maybe_dependent_member_ref. */
16342 if (dependent_scope_p (qualifying_scope))
16343 {
16344 if (template_id_p)
16345 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
16346 template_args);
16347 return build_qualified_name (NULL_TREE, qualifying_scope, name,
16348 /* ::template */false);
16349 }
16350
16351 if (name == complete_dtor_identifier)
16352 /* Treat as-if non-dependent below. */
16353 dependent_p = false;
16354
16355 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16356 complain);
16357 if (!baselink)
16358 {
16359 if ((complain & tf_error)
16360 && constructor_name_p (name, qualifying_scope))
16361 error ("cannot call constructor %<%T::%D%> directly",
16362 qualifying_scope, name);
16363 return error_mark_node;
16364 }
16365
16366 if (BASELINK_P (baselink))
16367 fns = BASELINK_FUNCTIONS (baselink);
16368 }
16369 else
16370 {
16371 /* We're going to overwrite pieces below, make a duplicate. */
16372 baselink = copy_node (baselink);
16373
16374 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
16375 {
16376 /* The decl we found was from non-dependent scope, but we still need
16377 to update the binfos for the instantiated qualifying_scope. */
16378 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
16379 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
16380 ba_unique, nullptr, complain);
16381 }
16382 }
16383
16384 /* If lookup found a single function, mark it as used at this point.
16385 (If lookup found multiple functions the one selected later by
16386 overload resolution will be marked as used at that point.) */
16387 if (!template_id_p && !really_overloaded_fn (fns))
16388 {
16389 tree fn = OVL_FIRST (fns);
16390 bool ok = mark_used (fn, complain);
16391 if (!ok && !(complain & tf_error))
16392 return error_mark_node;
16393 if (ok && BASELINK_P (baselink))
16394 /* We might have instantiated an auto function. */
16395 TREE_TYPE (baselink) = TREE_TYPE (fn);
16396 }
16397
16398 if (BASELINK_P (baselink))
16399 {
16400 /* Add back the template arguments, if present. */
16401 if (template_id_p)
16402 BASELINK_FUNCTIONS (baselink)
16403 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16404
16405 /* Update the conversion operator type. */
16406 BASELINK_OPTYPE (baselink) = optype;
16407 }
16408
16409 if (!object_type)
16410 object_type = current_class_type;
16411
16412 if (qualified_p || !dependent_p)
16413 {
16414 baselink = adjust_result_of_qualified_name_lookup (baselink,
16415 qualifying_scope,
16416 object_type);
16417 if (!qualified_p)
16418 /* We need to call adjust_result_of_qualified_name_lookup in case the
16419 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16420 so that we still get virtual function binding. */
16421 BASELINK_QUALIFIED_P (baselink) = false;
16422 }
16423
16424 return baselink;
16425 }
16426
16427 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16428 true if the qualified-id will be a postfix-expression in-and-of
16429 itself; false if more of the postfix-expression follows the
16430 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16431 of "&". */
16432
16433 static tree
16434 tsubst_qualified_id (tree qualified_id, tree args,
16435 tsubst_flags_t complain, tree in_decl,
16436 bool done, bool address_p)
16437 {
16438 tree expr;
16439 tree scope;
16440 tree name;
16441 bool is_template;
16442 tree template_args;
16443 location_t loc = EXPR_LOCATION (qualified_id);
16444
16445 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16446
16447 /* Figure out what name to look up. */
16448 name = TREE_OPERAND (qualified_id, 1);
16449 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16450 {
16451 is_template = true;
16452 template_args = TREE_OPERAND (name, 1);
16453 if (template_args)
16454 template_args = tsubst_template_args (template_args, args,
16455 complain, in_decl);
16456 if (template_args == error_mark_node)
16457 return error_mark_node;
16458 name = TREE_OPERAND (name, 0);
16459 }
16460 else
16461 {
16462 is_template = false;
16463 template_args = NULL_TREE;
16464 }
16465
16466 /* Substitute into the qualifying scope. When there are no ARGS, we
16467 are just trying to simplify a non-dependent expression. In that
16468 case the qualifying scope may be dependent, and, in any case,
16469 substituting will not help. */
16470 scope = TREE_OPERAND (qualified_id, 0);
16471 if (args)
16472 {
16473 scope = tsubst (scope, args, complain, in_decl);
16474 expr = tsubst_copy (name, args, complain, in_decl);
16475 }
16476 else
16477 expr = name;
16478
16479 if (dependent_scope_p (scope))
16480 {
16481 if (is_template)
16482 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16483 tree r = build_qualified_name (NULL_TREE, scope, expr,
16484 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16485 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16486 return r;
16487 }
16488
16489 if (!BASELINK_P (name) && !DECL_P (expr))
16490 {
16491 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16492 {
16493 /* A BIT_NOT_EXPR is used to represent a destructor. */
16494 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16495 {
16496 error ("qualifying type %qT does not match destructor name ~%qT",
16497 scope, TREE_OPERAND (expr, 0));
16498 expr = error_mark_node;
16499 }
16500 else
16501 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16502 LOOK_want::NORMAL, false);
16503 }
16504 else
16505 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16506 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16507 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16508 {
16509 if (complain & tf_error)
16510 {
16511 error ("dependent-name %qE is parsed as a non-type, but "
16512 "instantiation yields a type", qualified_id);
16513 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16514 }
16515 return error_mark_node;
16516 }
16517 }
16518
16519 if (DECL_P (expr))
16520 {
16521 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16522 scope, complain))
16523 return error_mark_node;
16524 /* Remember that there was a reference to this entity. */
16525 if (!mark_used (expr, complain) && !(complain & tf_error))
16526 return error_mark_node;
16527 }
16528
16529 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16530 {
16531 if (complain & tf_error)
16532 qualified_name_lookup_error (scope,
16533 TREE_OPERAND (qualified_id, 1),
16534 expr, input_location);
16535 return error_mark_node;
16536 }
16537
16538 if (is_template)
16539 {
16540 /* We may be repeating a check already done during parsing, but
16541 if it was well-formed and passed then, it will pass again
16542 now, and if it didn't, we wouldn't have got here. The case
16543 we want to catch is when we couldn't tell then, and can now,
16544 namely when templ prior to substitution was an
16545 identifier. */
16546 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16547 return error_mark_node;
16548
16549 if (variable_template_p (expr))
16550 expr = lookup_and_finish_template_variable (expr, template_args,
16551 complain);
16552 else
16553 expr = lookup_template_function (expr, template_args);
16554 }
16555
16556 if (expr == error_mark_node && complain & tf_error)
16557 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16558 expr, input_location);
16559 else if (TYPE_P (scope))
16560 {
16561 expr = (adjust_result_of_qualified_name_lookup
16562 (expr, scope, current_nonlambda_class_type ()));
16563 expr = (finish_qualified_id_expr
16564 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16565 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16566 /*template_arg_p=*/false, complain));
16567 }
16568
16569 /* Expressions do not generally have reference type. */
16570 if (TREE_CODE (expr) != SCOPE_REF
16571 /* However, if we're about to form a pointer-to-member, we just
16572 want the referenced member referenced. */
16573 && TREE_CODE (expr) != OFFSET_REF)
16574 expr = convert_from_reference (expr);
16575
16576 if (REF_PARENTHESIZED_P (qualified_id))
16577 expr = force_paren_expr (expr);
16578
16579 expr = maybe_wrap_with_location (expr, loc);
16580
16581 return expr;
16582 }
16583
16584 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16585 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16586 for tsubst. */
16587
16588 static tree
16589 tsubst_init (tree init, tree decl, tree args,
16590 tsubst_flags_t complain, tree in_decl)
16591 {
16592 if (!init)
16593 return NULL_TREE;
16594
16595 init = tsubst_expr (init, args, complain, in_decl, false);
16596
16597 tree type = TREE_TYPE (decl);
16598
16599 if (!init && type != error_mark_node)
16600 {
16601 if (tree auto_node = type_uses_auto (type))
16602 {
16603 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16604 {
16605 if (complain & tf_error)
16606 error ("initializer for %q#D expands to an empty list "
16607 "of expressions", decl);
16608 return error_mark_node;
16609 }
16610 }
16611 else if (!dependent_type_p (type))
16612 {
16613 /* If we had an initializer but it
16614 instantiated to nothing,
16615 value-initialize the object. This will
16616 only occur when the initializer was a
16617 pack expansion where the parameter packs
16618 used in that expansion were of length
16619 zero. */
16620 init = build_value_init (type, complain);
16621 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16622 init = get_target_expr_sfinae (init, complain);
16623 if (TREE_CODE (init) == TARGET_EXPR)
16624 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16625 }
16626 }
16627
16628 return init;
16629 }
16630
16631 /* If T is a reference to a dependent member of the current instantiation C and
16632 we are trying to refer to that member in a partial instantiation of C,
16633 return a SCOPE_REF; otherwise, return NULL_TREE.
16634
16635 This can happen when forming a C++17 deduction guide, as in PR96199. */
16636
16637 static tree
16638 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16639 tree in_decl)
16640 {
16641 if (cxx_dialect < cxx17)
16642 return NULL_TREE;
16643
16644 tree ctx = context_for_name_lookup (t);
16645 if (!CLASS_TYPE_P (ctx))
16646 return NULL_TREE;
16647
16648 ctx = tsubst (ctx, args, complain, in_decl);
16649 if (dependent_scope_p (ctx))
16650 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16651 /*template_p=*/false);
16652
16653 return NULL_TREE;
16654 }
16655
16656 /* Like tsubst, but deals with expressions. This function just replaces
16657 template parms; to finish processing the resultant expression, use
16658 tsubst_copy_and_build or tsubst_expr. */
16659
16660 static tree
16661 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16662 {
16663 enum tree_code code;
16664 tree r;
16665
16666 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16667 return t;
16668
16669 code = TREE_CODE (t);
16670
16671 switch (code)
16672 {
16673 case PARM_DECL:
16674 r = retrieve_local_specialization (t);
16675
16676 if (r == NULL_TREE)
16677 {
16678 /* We get here for a use of 'this' in an NSDMI. */
16679 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16680 return current_class_ptr;
16681
16682 /* This can happen for a parameter name used later in a function
16683 declaration (such as in a late-specified return type). Just
16684 make a dummy decl, since it's only used for its type. */
16685 gcc_assert (cp_unevaluated_operand != 0);
16686 r = tsubst_decl (t, args, complain);
16687 /* Give it the template pattern as its context; its true context
16688 hasn't been instantiated yet and this is good enough for
16689 mangling. */
16690 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16691 }
16692
16693 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16694 r = argument_pack_select_arg (r);
16695 if (!mark_used (r, complain) && !(complain & tf_error))
16696 return error_mark_node;
16697 return r;
16698
16699 case CONST_DECL:
16700 {
16701 tree enum_type;
16702 tree v;
16703
16704 if (DECL_TEMPLATE_PARM_P (t))
16705 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16706 /* There is no need to substitute into namespace-scope
16707 enumerators. */
16708 if (DECL_NAMESPACE_SCOPE_P (t))
16709 return t;
16710 /* If ARGS is NULL, then T is known to be non-dependent. */
16711 if (args == NULL_TREE)
16712 return scalar_constant_value (t);
16713
16714 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16715 return ref;
16716
16717 /* Unfortunately, we cannot just call lookup_name here.
16718 Consider:
16719
16720 template <int I> int f() {
16721 enum E { a = I };
16722 struct S { void g() { E e = a; } };
16723 };
16724
16725 When we instantiate f<7>::S::g(), say, lookup_name is not
16726 clever enough to find f<7>::a. */
16727 enum_type
16728 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16729 /*entering_scope=*/0);
16730
16731 for (v = TYPE_VALUES (enum_type);
16732 v != NULL_TREE;
16733 v = TREE_CHAIN (v))
16734 if (TREE_PURPOSE (v) == DECL_NAME (t))
16735 return TREE_VALUE (v);
16736
16737 /* We didn't find the name. That should never happen; if
16738 name-lookup found it during preliminary parsing, we
16739 should find it again here during instantiation. */
16740 gcc_unreachable ();
16741 }
16742 return t;
16743
16744 case FIELD_DECL:
16745 if (DECL_CONTEXT (t))
16746 {
16747 tree ctx;
16748
16749 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16750 /*entering_scope=*/1);
16751 if (ctx != DECL_CONTEXT (t))
16752 {
16753 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16754 if (!r)
16755 {
16756 if (complain & tf_error)
16757 error ("using invalid field %qD", t);
16758 return error_mark_node;
16759 }
16760 return r;
16761 }
16762 }
16763
16764 return t;
16765
16766 case VAR_DECL:
16767 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16768 return ref;
16769 gcc_fallthrough();
16770 case FUNCTION_DECL:
16771 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16772 r = tsubst (t, args, complain, in_decl);
16773 else if (DECL_LOCAL_DECL_P (t))
16774 {
16775 /* Local specialization will usually have been created when
16776 we instantiated the DECL_EXPR_DECL. */
16777 r = retrieve_local_specialization (t);
16778 if (!r)
16779 {
16780 /* We're in a generic lambda referencing a local extern
16781 from an outer block-scope of a non-template. */
16782 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
16783 r = t;
16784 }
16785 }
16786 else if (local_variable_p (t)
16787 && uses_template_parms (DECL_CONTEXT (t)))
16788 {
16789 r = retrieve_local_specialization (t);
16790 if (r == NULL_TREE)
16791 {
16792 /* First try name lookup to find the instantiation. */
16793 r = lookup_name (DECL_NAME (t));
16794 if (r)
16795 {
16796 if (!VAR_P (r))
16797 {
16798 /* During error-recovery we may find a non-variable,
16799 even an OVERLOAD: just bail out and avoid ICEs and
16800 duplicate diagnostics (c++/62207). */
16801 gcc_assert (seen_error ());
16802 return error_mark_node;
16803 }
16804 if (!is_capture_proxy (r))
16805 {
16806 /* Make sure the one we found is the one we want. */
16807 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16808 if (ctx != DECL_CONTEXT (r))
16809 r = NULL_TREE;
16810 }
16811 }
16812
16813 if (r)
16814 /* OK */;
16815 else
16816 {
16817 /* This can happen for a variable used in a
16818 late-specified return type of a local lambda, or for a
16819 local static or constant. Building a new VAR_DECL
16820 should be OK in all those cases. */
16821 r = tsubst_decl (t, args, complain);
16822 if (local_specializations)
16823 /* Avoid infinite recursion (79640). */
16824 register_local_specialization (r, t);
16825 if (decl_maybe_constant_var_p (r))
16826 {
16827 /* We can't call cp_finish_decl, so handle the
16828 initializer by hand. */
16829 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16830 complain, in_decl);
16831 if (!processing_template_decl)
16832 init = maybe_constant_init (init);
16833 if (processing_template_decl
16834 ? potential_constant_expression (init)
16835 : reduced_constant_expression_p (init))
16836 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16837 = TREE_CONSTANT (r) = true;
16838 DECL_INITIAL (r) = init;
16839 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16840 TREE_TYPE (r)
16841 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16842 complain, adc_variable_type);
16843 }
16844 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16845 || decl_constant_var_p (r)
16846 || seen_error ());
16847 if (!processing_template_decl
16848 && !TREE_STATIC (r))
16849 r = process_outer_var_ref (r, complain);
16850 }
16851 /* Remember this for subsequent uses. */
16852 if (local_specializations)
16853 register_local_specialization (r, t);
16854 }
16855 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16856 r = argument_pack_select_arg (r);
16857 }
16858 else
16859 r = t;
16860 if (!mark_used (r, complain))
16861 return error_mark_node;
16862 return r;
16863
16864 case NAMESPACE_DECL:
16865 return t;
16866
16867 case OVERLOAD:
16868 return t;
16869
16870 case BASELINK:
16871 return tsubst_baselink (t, current_nonlambda_class_type (),
16872 args, complain, in_decl);
16873
16874 case TEMPLATE_DECL:
16875 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16876 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16877 args, complain, in_decl);
16878 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16879 return tsubst (t, args, complain, in_decl);
16880 else if (DECL_CLASS_SCOPE_P (t)
16881 && uses_template_parms (DECL_CONTEXT (t)))
16882 {
16883 /* Template template argument like the following example need
16884 special treatment:
16885
16886 template <template <class> class TT> struct C {};
16887 template <class T> struct D {
16888 template <class U> struct E {};
16889 C<E> c; // #1
16890 };
16891 D<int> d; // #2
16892
16893 We are processing the template argument `E' in #1 for
16894 the template instantiation #2. Originally, `E' is a
16895 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16896 have to substitute this with one having context `D<int>'. */
16897
16898 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16899 if (dependent_scope_p (context))
16900 {
16901 /* When rewriting a constructor into a deduction guide, a
16902 non-dependent name can become dependent, so memtmpl<args>
16903 becomes context::template memtmpl<args>. */
16904 if (DECL_TYPE_TEMPLATE_P (t))
16905 return make_unbound_class_template (context, DECL_NAME (t),
16906 NULL_TREE, complain);
16907 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16908 return build_qualified_name (type, context, DECL_NAME (t),
16909 /*template*/true);
16910 }
16911 return lookup_field (context, DECL_NAME(t), 0, false);
16912 }
16913 else
16914 /* Ordinary template template argument. */
16915 return t;
16916
16917 case NON_LVALUE_EXPR:
16918 case VIEW_CONVERT_EXPR:
16919 {
16920 /* Handle location wrappers by substituting the wrapped node
16921 first, *then* reusing the resulting type. Doing the type
16922 first ensures that we handle template parameters and
16923 parameter pack expansions. */
16924 if (location_wrapper_p (t))
16925 {
16926 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16927 complain, in_decl);
16928 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16929 }
16930 tree op = TREE_OPERAND (t, 0);
16931 if (code == VIEW_CONVERT_EXPR
16932 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16933 {
16934 /* Wrapper to make a C++20 template parameter object const. */
16935 op = tsubst_copy (op, args, complain, in_decl);
16936 if (!CP_TYPE_CONST_P (TREE_TYPE (op)))
16937 {
16938 /* The template argument is not const, presumably because
16939 it is still dependent, and so not the const template parm
16940 object. */
16941 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16942 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
16943 (type, TREE_TYPE (op)));
16944 if (TREE_CODE (op) == CONSTRUCTOR
16945 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
16946 {
16947 /* Don't add a wrapper to these. */
16948 op = copy_node (op);
16949 TREE_TYPE (op) = type;
16950 }
16951 else
16952 /* Do add a wrapper otherwise (in particular, if op is
16953 another TEMPLATE_PARM_INDEX). */
16954 op = build1 (code, type, op);
16955 }
16956 return op;
16957 }
16958 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16959 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16960 {
16961 op = tsubst_copy (op, args, complain, in_decl);
16962 op = build1 (code, TREE_TYPE (op), op);
16963 REF_PARENTHESIZED_P (op) = true;
16964 return op;
16965 }
16966 /* We shouldn't see any other uses of these in templates. */
16967 gcc_unreachable ();
16968 }
16969
16970 case CAST_EXPR:
16971 case REINTERPRET_CAST_EXPR:
16972 case CONST_CAST_EXPR:
16973 case STATIC_CAST_EXPR:
16974 case DYNAMIC_CAST_EXPR:
16975 case IMPLICIT_CONV_EXPR:
16976 case CONVERT_EXPR:
16977 case NOP_EXPR:
16978 {
16979 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16980 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16981 return build1 (code, type, op0);
16982 }
16983
16984 case BIT_CAST_EXPR:
16985 {
16986 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16987 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16988 r = build_min (BIT_CAST_EXPR, type, op0);
16989 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16990 return r;
16991 }
16992
16993 case SIZEOF_EXPR:
16994 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16995 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16996 {
16997 tree expanded, op = TREE_OPERAND (t, 0);
16998 int len = 0;
16999
17000 if (SIZEOF_EXPR_TYPE_P (t))
17001 op = TREE_TYPE (op);
17002
17003 ++cp_unevaluated_operand;
17004 ++c_inhibit_evaluation_warnings;
17005 /* We only want to compute the number of arguments. */
17006 if (PACK_EXPANSION_P (op))
17007 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
17008 else
17009 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
17010 args, complain, in_decl);
17011 --cp_unevaluated_operand;
17012 --c_inhibit_evaluation_warnings;
17013
17014 if (TREE_CODE (expanded) == TREE_VEC)
17015 {
17016 len = TREE_VEC_LENGTH (expanded);
17017 /* Set TREE_USED for the benefit of -Wunused. */
17018 for (int i = 0; i < len; i++)
17019 if (DECL_P (TREE_VEC_ELT (expanded, i)))
17020 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
17021 }
17022
17023 if (expanded == error_mark_node)
17024 return error_mark_node;
17025 else if (PACK_EXPANSION_P (expanded)
17026 || (TREE_CODE (expanded) == TREE_VEC
17027 && pack_expansion_args_count (expanded)))
17028
17029 {
17030 if (PACK_EXPANSION_P (expanded))
17031 /* OK. */;
17032 else if (TREE_VEC_LENGTH (expanded) == 1)
17033 expanded = TREE_VEC_ELT (expanded, 0);
17034 else
17035 expanded = make_argument_pack (expanded);
17036
17037 if (TYPE_P (expanded))
17038 return cxx_sizeof_or_alignof_type (input_location,
17039 expanded, SIZEOF_EXPR,
17040 false,
17041 complain & tf_error);
17042 else
17043 return cxx_sizeof_or_alignof_expr (input_location,
17044 expanded, SIZEOF_EXPR,
17045 false,
17046 complain & tf_error);
17047 }
17048 else
17049 return build_int_cst (size_type_node, len);
17050 }
17051 if (SIZEOF_EXPR_TYPE_P (t))
17052 {
17053 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
17054 args, complain, in_decl);
17055 r = build1 (NOP_EXPR, r, error_mark_node);
17056 r = build1 (SIZEOF_EXPR,
17057 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
17058 SIZEOF_EXPR_TYPE_P (r) = 1;
17059 return r;
17060 }
17061 /* Fall through */
17062
17063 case INDIRECT_REF:
17064 case NEGATE_EXPR:
17065 case TRUTH_NOT_EXPR:
17066 case BIT_NOT_EXPR:
17067 case ADDR_EXPR:
17068 case UNARY_PLUS_EXPR: /* Unary + */
17069 case ALIGNOF_EXPR:
17070 case AT_ENCODE_EXPR:
17071 case ARROW_EXPR:
17072 case THROW_EXPR:
17073 case TYPEID_EXPR:
17074 case REALPART_EXPR:
17075 case IMAGPART_EXPR:
17076 case PAREN_EXPR:
17077 {
17078 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17079 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17080 r = build1 (code, type, op0);
17081 if (code == ALIGNOF_EXPR)
17082 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
17083 return r;
17084 }
17085
17086 case COMPONENT_REF:
17087 {
17088 tree object;
17089 tree name;
17090
17091 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17092 name = TREE_OPERAND (t, 1);
17093 if (TREE_CODE (name) == BIT_NOT_EXPR)
17094 {
17095 name = tsubst_copy (TREE_OPERAND (name, 0), args,
17096 complain, in_decl);
17097 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
17098 }
17099 else if (TREE_CODE (name) == SCOPE_REF
17100 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
17101 {
17102 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
17103 complain, in_decl);
17104 name = TREE_OPERAND (name, 1);
17105 name = tsubst_copy (TREE_OPERAND (name, 0), args,
17106 complain, in_decl);
17107 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
17108 name = build_qualified_name (/*type=*/NULL_TREE,
17109 base, name,
17110 /*template_p=*/false);
17111 }
17112 else if (BASELINK_P (name))
17113 name = tsubst_baselink (name,
17114 non_reference (TREE_TYPE (object)),
17115 args, complain,
17116 in_decl);
17117 else
17118 name = tsubst_copy (name, args, complain, in_decl);
17119 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
17120 }
17121
17122 case PLUS_EXPR:
17123 case MINUS_EXPR:
17124 case MULT_EXPR:
17125 case TRUNC_DIV_EXPR:
17126 case CEIL_DIV_EXPR:
17127 case FLOOR_DIV_EXPR:
17128 case ROUND_DIV_EXPR:
17129 case EXACT_DIV_EXPR:
17130 case BIT_AND_EXPR:
17131 case BIT_IOR_EXPR:
17132 case BIT_XOR_EXPR:
17133 case TRUNC_MOD_EXPR:
17134 case FLOOR_MOD_EXPR:
17135 case TRUTH_ANDIF_EXPR:
17136 case TRUTH_ORIF_EXPR:
17137 case TRUTH_AND_EXPR:
17138 case TRUTH_OR_EXPR:
17139 case RSHIFT_EXPR:
17140 case LSHIFT_EXPR:
17141 case EQ_EXPR:
17142 case NE_EXPR:
17143 case MAX_EXPR:
17144 case MIN_EXPR:
17145 case LE_EXPR:
17146 case GE_EXPR:
17147 case LT_EXPR:
17148 case GT_EXPR:
17149 case COMPOUND_EXPR:
17150 case DOTSTAR_EXPR:
17151 case MEMBER_REF:
17152 case PREDECREMENT_EXPR:
17153 case PREINCREMENT_EXPR:
17154 case POSTDECREMENT_EXPR:
17155 case POSTINCREMENT_EXPR:
17156 {
17157 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17158 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17159 return build_nt (code, op0, op1);
17160 }
17161
17162 case SCOPE_REF:
17163 {
17164 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17165 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17166 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
17167 QUALIFIED_NAME_IS_TEMPLATE (t));
17168 }
17169
17170 case ARRAY_REF:
17171 {
17172 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17173 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17174 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
17175 }
17176
17177 case CALL_EXPR:
17178 {
17179 int n = VL_EXP_OPERAND_LENGTH (t);
17180 tree result = build_vl_exp (CALL_EXPR, n);
17181 int i;
17182 for (i = 0; i < n; i++)
17183 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
17184 complain, in_decl);
17185 return result;
17186 }
17187
17188 case COND_EXPR:
17189 case MODOP_EXPR:
17190 case PSEUDO_DTOR_EXPR:
17191 case VEC_PERM_EXPR:
17192 {
17193 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17194 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17195 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17196 r = build_nt (code, op0, op1, op2);
17197 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17198 return r;
17199 }
17200
17201 case NEW_EXPR:
17202 {
17203 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17204 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17205 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17206 r = build_nt (code, op0, op1, op2);
17207 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17208 return r;
17209 }
17210
17211 case DELETE_EXPR:
17212 {
17213 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17214 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17215 r = build_nt (code, op0, op1);
17216 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17217 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17218 return r;
17219 }
17220
17221 case TEMPLATE_ID_EXPR:
17222 {
17223 /* Substituted template arguments */
17224 tree tmpl = TREE_OPERAND (t, 0);
17225 tree targs = TREE_OPERAND (t, 1);
17226
17227 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
17228 if (targs)
17229 targs = tsubst_template_args (targs, args, complain, in_decl);
17230
17231 if (variable_template_p (tmpl))
17232 return lookup_template_variable (tmpl, targs);
17233 else
17234 return lookup_template_function (tmpl, targs);
17235 }
17236
17237 case TREE_LIST:
17238 {
17239 tree purpose, value, chain;
17240
17241 if (t == void_list_node)
17242 return t;
17243
17244 purpose = TREE_PURPOSE (t);
17245 if (purpose)
17246 purpose = tsubst_copy (purpose, args, complain, in_decl);
17247 value = TREE_VALUE (t);
17248 if (value)
17249 value = tsubst_copy (value, args, complain, in_decl);
17250 chain = TREE_CHAIN (t);
17251 if (chain && chain != void_type_node)
17252 chain = tsubst_copy (chain, args, complain, in_decl);
17253 if (purpose == TREE_PURPOSE (t)
17254 && value == TREE_VALUE (t)
17255 && chain == TREE_CHAIN (t))
17256 return t;
17257 return tree_cons (purpose, value, chain);
17258 }
17259
17260 case RECORD_TYPE:
17261 case UNION_TYPE:
17262 case ENUMERAL_TYPE:
17263 case INTEGER_TYPE:
17264 case TEMPLATE_TYPE_PARM:
17265 case TEMPLATE_TEMPLATE_PARM:
17266 case BOUND_TEMPLATE_TEMPLATE_PARM:
17267 case TEMPLATE_PARM_INDEX:
17268 case POINTER_TYPE:
17269 case REFERENCE_TYPE:
17270 case OFFSET_TYPE:
17271 case FUNCTION_TYPE:
17272 case METHOD_TYPE:
17273 case ARRAY_TYPE:
17274 case TYPENAME_TYPE:
17275 case UNBOUND_CLASS_TEMPLATE:
17276 case TYPEOF_TYPE:
17277 case DECLTYPE_TYPE:
17278 case TYPE_DECL:
17279 return tsubst (t, args, complain, in_decl);
17280
17281 case USING_DECL:
17282 t = DECL_NAME (t);
17283 /* Fall through. */
17284 case IDENTIFIER_NODE:
17285 if (IDENTIFIER_CONV_OP_P (t))
17286 {
17287 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17288 return make_conv_op_name (new_type);
17289 }
17290 else
17291 return t;
17292
17293 case CONSTRUCTOR:
17294 /* This is handled by tsubst_copy_and_build. */
17295 gcc_unreachable ();
17296
17297 case VA_ARG_EXPR:
17298 {
17299 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17300 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17301 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17302 }
17303
17304 case CLEANUP_POINT_EXPR:
17305 /* We shouldn't have built any of these during initial template
17306 generation. Instead, they should be built during instantiation
17307 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17308 gcc_unreachable ();
17309
17310 case OFFSET_REF:
17311 {
17312 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17313 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17314 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17315 r = build2 (code, type, op0, op1);
17316 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17317 if (!mark_used (TREE_OPERAND (r, 1), complain)
17318 && !(complain & tf_error))
17319 return error_mark_node;
17320 return r;
17321 }
17322
17323 case EXPR_PACK_EXPANSION:
17324 error ("invalid use of pack expansion expression");
17325 return error_mark_node;
17326
17327 case NONTYPE_ARGUMENT_PACK:
17328 error ("use %<...%> to expand argument pack");
17329 return error_mark_node;
17330
17331 case VOID_CST:
17332 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17333 return t;
17334
17335 case INTEGER_CST:
17336 case REAL_CST:
17337 case COMPLEX_CST:
17338 {
17339 /* Instantiate any typedefs in the type. */
17340 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17341 r = fold_convert (type, t);
17342 gcc_assert (TREE_CODE (r) == code);
17343 return r;
17344 }
17345
17346 case STRING_CST:
17347 {
17348 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17349 r = t;
17350 if (type != TREE_TYPE (t))
17351 {
17352 r = copy_node (t);
17353 TREE_TYPE (r) = type;
17354 }
17355 return r;
17356 }
17357
17358 case PTRMEM_CST:
17359 /* These can sometimes show up in a partial instantiation, but never
17360 involve template parms. */
17361 gcc_assert (!uses_template_parms (t));
17362 return t;
17363
17364 case UNARY_LEFT_FOLD_EXPR:
17365 return tsubst_unary_left_fold (t, args, complain, in_decl);
17366 case UNARY_RIGHT_FOLD_EXPR:
17367 return tsubst_unary_right_fold (t, args, complain, in_decl);
17368 case BINARY_LEFT_FOLD_EXPR:
17369 return tsubst_binary_left_fold (t, args, complain, in_decl);
17370 case BINARY_RIGHT_FOLD_EXPR:
17371 return tsubst_binary_right_fold (t, args, complain, in_decl);
17372 case PREDICT_EXPR:
17373 return t;
17374
17375 case DEBUG_BEGIN_STMT:
17376 /* ??? There's no point in copying it for now, but maybe some
17377 day it will contain more information, such as a pointer back
17378 to the containing function, inlined copy or so. */
17379 return t;
17380
17381 case CO_AWAIT_EXPR:
17382 return tsubst_expr (t, args, complain, in_decl,
17383 /*integral_constant_expression_p=*/false);
17384 break;
17385
17386 default:
17387 /* We shouldn't get here, but keep going if !flag_checking. */
17388 if (flag_checking)
17389 gcc_unreachable ();
17390 return t;
17391 }
17392 }
17393
17394 /* Helper function for tsubst_omp_clauses, used for instantiation of
17395 OMP_CLAUSE_DECL of clauses. */
17396
17397 static tree
17398 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17399 tree in_decl, tree *iterator_cache)
17400 {
17401 if (decl == NULL_TREE)
17402 return NULL_TREE;
17403
17404 /* Handle OpenMP iterators. */
17405 if (TREE_CODE (decl) == TREE_LIST
17406 && TREE_PURPOSE (decl)
17407 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17408 {
17409 tree ret;
17410 if (iterator_cache[0] == TREE_PURPOSE (decl))
17411 ret = iterator_cache[1];
17412 else
17413 {
17414 tree *tp = &ret;
17415 begin_scope (sk_omp, NULL);
17416 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17417 {
17418 *tp = copy_node (it);
17419 TREE_VEC_ELT (*tp, 0)
17420 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17421 TREE_VEC_ELT (*tp, 1)
17422 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17423 /*integral_constant_expression_p=*/false);
17424 TREE_VEC_ELT (*tp, 2)
17425 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17426 /*integral_constant_expression_p=*/false);
17427 TREE_VEC_ELT (*tp, 3)
17428 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17429 /*integral_constant_expression_p=*/false);
17430 TREE_CHAIN (*tp) = NULL_TREE;
17431 tp = &TREE_CHAIN (*tp);
17432 }
17433 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17434 iterator_cache[0] = TREE_PURPOSE (decl);
17435 iterator_cache[1] = ret;
17436 }
17437 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17438 args, complain,
17439 in_decl, NULL));
17440 }
17441
17442 /* Handle an OpenMP array section represented as a TREE_LIST (or
17443 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17444 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17445 TREE_LIST. We can handle it exactly the same as an array section
17446 (purpose, value, and a chain), even though the nomenclature
17447 (low_bound, length, etc) is different. */
17448 if (TREE_CODE (decl) == TREE_LIST)
17449 {
17450 tree low_bound
17451 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17452 /*integral_constant_expression_p=*/false);
17453 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17454 /*integral_constant_expression_p=*/false);
17455 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17456 in_decl, NULL);
17457 if (TREE_PURPOSE (decl) == low_bound
17458 && TREE_VALUE (decl) == length
17459 && TREE_CHAIN (decl) == chain)
17460 return decl;
17461 tree ret = tree_cons (low_bound, length, chain);
17462 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17463 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17464 return ret;
17465 }
17466 tree ret = tsubst_expr (decl, args, complain, in_decl,
17467 /*integral_constant_expression_p=*/false);
17468 /* Undo convert_from_reference tsubst_expr could have called. */
17469 if (decl
17470 && REFERENCE_REF_P (ret)
17471 && !REFERENCE_REF_P (decl))
17472 ret = TREE_OPERAND (ret, 0);
17473 return ret;
17474 }
17475
17476 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17477
17478 static tree
17479 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17480 tree args, tsubst_flags_t complain, tree in_decl)
17481 {
17482 tree new_clauses = NULL_TREE, nc, oc;
17483 tree linear_no_step = NULL_TREE;
17484 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17485
17486 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17487 {
17488 nc = copy_node (oc);
17489 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17490 new_clauses = nc;
17491
17492 switch (OMP_CLAUSE_CODE (nc))
17493 {
17494 case OMP_CLAUSE_LASTPRIVATE:
17495 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17496 {
17497 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17498 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17499 in_decl, /*integral_constant_expression_p=*/false);
17500 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17501 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17502 }
17503 /* FALLTHRU */
17504 case OMP_CLAUSE_PRIVATE:
17505 case OMP_CLAUSE_SHARED:
17506 case OMP_CLAUSE_FIRSTPRIVATE:
17507 case OMP_CLAUSE_COPYIN:
17508 case OMP_CLAUSE_COPYPRIVATE:
17509 case OMP_CLAUSE_UNIFORM:
17510 case OMP_CLAUSE_DEPEND:
17511 case OMP_CLAUSE_FROM:
17512 case OMP_CLAUSE_TO:
17513 case OMP_CLAUSE_MAP:
17514 case OMP_CLAUSE__CACHE_:
17515 case OMP_CLAUSE_NONTEMPORAL:
17516 case OMP_CLAUSE_USE_DEVICE_PTR:
17517 case OMP_CLAUSE_USE_DEVICE_ADDR:
17518 case OMP_CLAUSE_IS_DEVICE_PTR:
17519 case OMP_CLAUSE_INCLUSIVE:
17520 case OMP_CLAUSE_EXCLUSIVE:
17521 OMP_CLAUSE_DECL (nc)
17522 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17523 in_decl, iterator_cache);
17524 break;
17525 case OMP_CLAUSE_TILE:
17526 case OMP_CLAUSE_IF:
17527 case OMP_CLAUSE_NUM_THREADS:
17528 case OMP_CLAUSE_SCHEDULE:
17529 case OMP_CLAUSE_COLLAPSE:
17530 case OMP_CLAUSE_FINAL:
17531 case OMP_CLAUSE_DEVICE:
17532 case OMP_CLAUSE_DIST_SCHEDULE:
17533 case OMP_CLAUSE_NUM_TEAMS:
17534 case OMP_CLAUSE_THREAD_LIMIT:
17535 case OMP_CLAUSE_SAFELEN:
17536 case OMP_CLAUSE_SIMDLEN:
17537 case OMP_CLAUSE_NUM_TASKS:
17538 case OMP_CLAUSE_GRAINSIZE:
17539 case OMP_CLAUSE_PRIORITY:
17540 case OMP_CLAUSE_ORDERED:
17541 case OMP_CLAUSE_HINT:
17542 case OMP_CLAUSE_NUM_GANGS:
17543 case OMP_CLAUSE_NUM_WORKERS:
17544 case OMP_CLAUSE_VECTOR_LENGTH:
17545 case OMP_CLAUSE_WORKER:
17546 case OMP_CLAUSE_VECTOR:
17547 case OMP_CLAUSE_ASYNC:
17548 case OMP_CLAUSE_WAIT:
17549 case OMP_CLAUSE_DETACH:
17550 OMP_CLAUSE_OPERAND (nc, 0)
17551 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17552 in_decl, /*integral_constant_expression_p=*/false);
17553 break;
17554 case OMP_CLAUSE_REDUCTION:
17555 case OMP_CLAUSE_IN_REDUCTION:
17556 case OMP_CLAUSE_TASK_REDUCTION:
17557 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17558 {
17559 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17560 if (TREE_CODE (placeholder) == SCOPE_REF)
17561 {
17562 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17563 complain, in_decl);
17564 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17565 = build_qualified_name (NULL_TREE, scope,
17566 TREE_OPERAND (placeholder, 1),
17567 false);
17568 }
17569 else
17570 gcc_assert (identifier_p (placeholder));
17571 }
17572 OMP_CLAUSE_DECL (nc)
17573 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17574 in_decl, NULL);
17575 break;
17576 case OMP_CLAUSE_GANG:
17577 case OMP_CLAUSE_ALIGNED:
17578 case OMP_CLAUSE_ALLOCATE:
17579 OMP_CLAUSE_DECL (nc)
17580 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17581 in_decl, NULL);
17582 OMP_CLAUSE_OPERAND (nc, 1)
17583 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17584 in_decl, /*integral_constant_expression_p=*/false);
17585 break;
17586 case OMP_CLAUSE_LINEAR:
17587 OMP_CLAUSE_DECL (nc)
17588 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17589 in_decl, NULL);
17590 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17591 {
17592 gcc_assert (!linear_no_step);
17593 linear_no_step = nc;
17594 }
17595 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17596 OMP_CLAUSE_LINEAR_STEP (nc)
17597 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17598 complain, in_decl, NULL);
17599 else
17600 OMP_CLAUSE_LINEAR_STEP (nc)
17601 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17602 in_decl,
17603 /*integral_constant_expression_p=*/false);
17604 break;
17605 case OMP_CLAUSE_NOWAIT:
17606 case OMP_CLAUSE_DEFAULT:
17607 case OMP_CLAUSE_UNTIED:
17608 case OMP_CLAUSE_MERGEABLE:
17609 case OMP_CLAUSE_INBRANCH:
17610 case OMP_CLAUSE_NOTINBRANCH:
17611 case OMP_CLAUSE_PROC_BIND:
17612 case OMP_CLAUSE_FOR:
17613 case OMP_CLAUSE_PARALLEL:
17614 case OMP_CLAUSE_SECTIONS:
17615 case OMP_CLAUSE_TASKGROUP:
17616 case OMP_CLAUSE_NOGROUP:
17617 case OMP_CLAUSE_THREADS:
17618 case OMP_CLAUSE_SIMD:
17619 case OMP_CLAUSE_DEFAULTMAP:
17620 case OMP_CLAUSE_ORDER:
17621 case OMP_CLAUSE_BIND:
17622 case OMP_CLAUSE_INDEPENDENT:
17623 case OMP_CLAUSE_AUTO:
17624 case OMP_CLAUSE_SEQ:
17625 case OMP_CLAUSE_IF_PRESENT:
17626 case OMP_CLAUSE_FINALIZE:
17627 break;
17628 default:
17629 gcc_unreachable ();
17630 }
17631 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17632 switch (OMP_CLAUSE_CODE (nc))
17633 {
17634 case OMP_CLAUSE_SHARED:
17635 case OMP_CLAUSE_PRIVATE:
17636 case OMP_CLAUSE_FIRSTPRIVATE:
17637 case OMP_CLAUSE_LASTPRIVATE:
17638 case OMP_CLAUSE_COPYPRIVATE:
17639 case OMP_CLAUSE_LINEAR:
17640 case OMP_CLAUSE_REDUCTION:
17641 case OMP_CLAUSE_IN_REDUCTION:
17642 case OMP_CLAUSE_TASK_REDUCTION:
17643 case OMP_CLAUSE_USE_DEVICE_PTR:
17644 case OMP_CLAUSE_USE_DEVICE_ADDR:
17645 case OMP_CLAUSE_IS_DEVICE_PTR:
17646 case OMP_CLAUSE_INCLUSIVE:
17647 case OMP_CLAUSE_EXCLUSIVE:
17648 case OMP_CLAUSE_ALLOCATE:
17649 /* tsubst_expr on SCOPE_REF results in returning
17650 finish_non_static_data_member result. Undo that here. */
17651 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17652 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17653 == IDENTIFIER_NODE))
17654 {
17655 tree t = OMP_CLAUSE_DECL (nc);
17656 tree v = t;
17657 while (v)
17658 switch (TREE_CODE (v))
17659 {
17660 case COMPONENT_REF:
17661 case MEM_REF:
17662 case INDIRECT_REF:
17663 CASE_CONVERT:
17664 case POINTER_PLUS_EXPR:
17665 v = TREE_OPERAND (v, 0);
17666 continue;
17667 case PARM_DECL:
17668 if (DECL_CONTEXT (v) == current_function_decl
17669 && DECL_ARTIFICIAL (v)
17670 && DECL_NAME (v) == this_identifier)
17671 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17672 /* FALLTHRU */
17673 default:
17674 v = NULL_TREE;
17675 break;
17676 }
17677 }
17678 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17679 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17680 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17681 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17682 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17683 {
17684 tree decl = OMP_CLAUSE_DECL (nc);
17685 if (VAR_P (decl))
17686 {
17687 retrofit_lang_decl (decl);
17688 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17689 }
17690 }
17691 break;
17692 default:
17693 break;
17694 }
17695 }
17696
17697 new_clauses = nreverse (new_clauses);
17698 if (ort != C_ORT_OMP_DECLARE_SIMD)
17699 {
17700 new_clauses = finish_omp_clauses (new_clauses, ort);
17701 if (linear_no_step)
17702 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17703 if (nc == linear_no_step)
17704 {
17705 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17706 break;
17707 }
17708 }
17709 return new_clauses;
17710 }
17711
17712 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17713
17714 static tree
17715 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17716 tree in_decl)
17717 {
17718 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17719
17720 tree purpose, value, chain;
17721
17722 if (t == NULL)
17723 return t;
17724
17725 if (TREE_CODE (t) != TREE_LIST)
17726 return tsubst_copy_and_build (t, args, complain, in_decl,
17727 /*function_p=*/false,
17728 /*integral_constant_expression_p=*/false);
17729
17730 if (t == void_list_node)
17731 return t;
17732
17733 purpose = TREE_PURPOSE (t);
17734 if (purpose)
17735 purpose = RECUR (purpose);
17736 value = TREE_VALUE (t);
17737 if (value)
17738 {
17739 if (TREE_CODE (value) != LABEL_DECL)
17740 value = RECUR (value);
17741 else
17742 {
17743 value = lookup_label (DECL_NAME (value));
17744 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17745 TREE_USED (value) = 1;
17746 }
17747 }
17748 chain = TREE_CHAIN (t);
17749 if (chain && chain != void_type_node)
17750 chain = RECUR (chain);
17751 return tree_cons (purpose, value, chain);
17752 #undef RECUR
17753 }
17754
17755 /* Used to temporarily communicate the list of #pragma omp parallel
17756 clauses to #pragma omp for instantiation if they are combined
17757 together. */
17758
17759 static tree *omp_parallel_combined_clauses;
17760
17761 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17762 tree *, unsigned int *);
17763
17764 /* Substitute one OMP_FOR iterator. */
17765
17766 static bool
17767 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17768 tree initv, tree condv, tree incrv, tree *clauses,
17769 tree args, tsubst_flags_t complain, tree in_decl,
17770 bool integral_constant_expression_p)
17771 {
17772 #define RECUR(NODE) \
17773 tsubst_expr ((NODE), args, complain, in_decl, \
17774 integral_constant_expression_p)
17775 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17776 bool ret = false;
17777
17778 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17779 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17780
17781 decl = TREE_OPERAND (init, 0);
17782 init = TREE_OPERAND (init, 1);
17783 tree decl_expr = NULL_TREE;
17784 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17785 if (range_for)
17786 {
17787 bool decomp = false;
17788 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17789 {
17790 tree v = DECL_VALUE_EXPR (decl);
17791 if (TREE_CODE (v) == ARRAY_REF
17792 && VAR_P (TREE_OPERAND (v, 0))
17793 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17794 {
17795 tree decomp_first = NULL_TREE;
17796 unsigned decomp_cnt = 0;
17797 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17798 maybe_push_decl (d);
17799 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17800 in_decl, &decomp_first, &decomp_cnt);
17801 decomp = true;
17802 if (d == error_mark_node)
17803 decl = error_mark_node;
17804 else
17805 for (unsigned int i = 0; i < decomp_cnt; i++)
17806 {
17807 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17808 {
17809 tree v = build_nt (ARRAY_REF, d,
17810 size_int (decomp_cnt - i - 1),
17811 NULL_TREE, NULL_TREE);
17812 SET_DECL_VALUE_EXPR (decomp_first, v);
17813 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17814 }
17815 fit_decomposition_lang_decl (decomp_first, d);
17816 decomp_first = DECL_CHAIN (decomp_first);
17817 }
17818 }
17819 }
17820 decl = tsubst_decl (decl, args, complain);
17821 if (!decomp)
17822 maybe_push_decl (decl);
17823 }
17824 else if (init && TREE_CODE (init) == DECL_EXPR)
17825 {
17826 /* We need to jump through some hoops to handle declarations in the
17827 init-statement, since we might need to handle auto deduction,
17828 but we need to keep control of initialization. */
17829 decl_expr = init;
17830 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17831 decl = tsubst_decl (decl, args, complain);
17832 }
17833 else
17834 {
17835 if (TREE_CODE (decl) == SCOPE_REF)
17836 {
17837 decl = RECUR (decl);
17838 if (TREE_CODE (decl) == COMPONENT_REF)
17839 {
17840 tree v = decl;
17841 while (v)
17842 switch (TREE_CODE (v))
17843 {
17844 case COMPONENT_REF:
17845 case MEM_REF:
17846 case INDIRECT_REF:
17847 CASE_CONVERT:
17848 case POINTER_PLUS_EXPR:
17849 v = TREE_OPERAND (v, 0);
17850 continue;
17851 case PARM_DECL:
17852 if (DECL_CONTEXT (v) == current_function_decl
17853 && DECL_ARTIFICIAL (v)
17854 && DECL_NAME (v) == this_identifier)
17855 {
17856 decl = TREE_OPERAND (decl, 1);
17857 decl = omp_privatize_field (decl, false);
17858 }
17859 /* FALLTHRU */
17860 default:
17861 v = NULL_TREE;
17862 break;
17863 }
17864 }
17865 }
17866 else
17867 decl = RECUR (decl);
17868 }
17869 if (init && TREE_CODE (init) == TREE_VEC)
17870 {
17871 init = copy_node (init);
17872 TREE_VEC_ELT (init, 0)
17873 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17874 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17875 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17876 }
17877 else
17878 init = RECUR (init);
17879
17880 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17881 {
17882 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17883 if (TREE_CODE (o) == TREE_LIST)
17884 TREE_VEC_ELT (orig_declv, i)
17885 = tree_cons (RECUR (TREE_PURPOSE (o)),
17886 RECUR (TREE_VALUE (o)),
17887 NULL_TREE);
17888 else
17889 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17890 }
17891
17892 if (range_for)
17893 {
17894 tree this_pre_body = NULL_TREE;
17895 tree orig_init = NULL_TREE;
17896 tree orig_decl = NULL_TREE;
17897 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17898 orig_init, cond, incr);
17899 if (orig_decl)
17900 {
17901 if (orig_declv == NULL_TREE)
17902 orig_declv = copy_node (declv);
17903 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17904 ret = true;
17905 }
17906 else if (orig_declv)
17907 TREE_VEC_ELT (orig_declv, i) = decl;
17908 }
17909
17910 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17911 if (!range_for && auto_node && init)
17912 TREE_TYPE (decl)
17913 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17914
17915 gcc_assert (!type_dependent_expression_p (decl));
17916
17917 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17918 {
17919 if (decl_expr)
17920 {
17921 /* Declare the variable, but don't let that initialize it. */
17922 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17923 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17924 RECUR (decl_expr);
17925 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17926 }
17927
17928 if (!range_for)
17929 {
17930 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17931 if (COMPARISON_CLASS_P (cond)
17932 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17933 {
17934 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17935 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17936 TREE_VEC_ELT (rhs, 0)
17937 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17938 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17939 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17940 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17941 lhs, rhs);
17942 }
17943 else
17944 cond = RECUR (cond);
17945 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17946 if (TREE_CODE (incr) == MODIFY_EXPR)
17947 {
17948 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17949 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17950 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17951 NOP_EXPR, rhs, complain);
17952 }
17953 else
17954 incr = RECUR (incr);
17955 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17956 TREE_VEC_ELT (orig_declv, i) = decl;
17957 }
17958 TREE_VEC_ELT (declv, i) = decl;
17959 TREE_VEC_ELT (initv, i) = init;
17960 TREE_VEC_ELT (condv, i) = cond;
17961 TREE_VEC_ELT (incrv, i) = incr;
17962 return ret;
17963 }
17964
17965 if (decl_expr)
17966 {
17967 /* Declare and initialize the variable. */
17968 RECUR (decl_expr);
17969 init = NULL_TREE;
17970 }
17971 else if (init)
17972 {
17973 tree *pc;
17974 int j;
17975 for (j = ((omp_parallel_combined_clauses == NULL
17976 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17977 {
17978 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17979 {
17980 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17981 && OMP_CLAUSE_DECL (*pc) == decl)
17982 break;
17983 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17984 && OMP_CLAUSE_DECL (*pc) == decl)
17985 {
17986 if (j)
17987 break;
17988 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17989 tree c = *pc;
17990 *pc = OMP_CLAUSE_CHAIN (c);
17991 OMP_CLAUSE_CHAIN (c) = *clauses;
17992 *clauses = c;
17993 }
17994 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17995 && OMP_CLAUSE_DECL (*pc) == decl)
17996 {
17997 error ("iteration variable %qD should not be firstprivate",
17998 decl);
17999 *pc = OMP_CLAUSE_CHAIN (*pc);
18000 }
18001 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
18002 && OMP_CLAUSE_DECL (*pc) == decl)
18003 {
18004 error ("iteration variable %qD should not be reduction",
18005 decl);
18006 *pc = OMP_CLAUSE_CHAIN (*pc);
18007 }
18008 else
18009 pc = &OMP_CLAUSE_CHAIN (*pc);
18010 }
18011 if (*pc)
18012 break;
18013 }
18014 if (*pc == NULL_TREE)
18015 {
18016 tree c = build_omp_clause (input_location,
18017 TREE_CODE (t) == OMP_LOOP
18018 ? OMP_CLAUSE_LASTPRIVATE
18019 : OMP_CLAUSE_PRIVATE);
18020 OMP_CLAUSE_DECL (c) = decl;
18021 c = finish_omp_clauses (c, C_ORT_OMP);
18022 if (c)
18023 {
18024 OMP_CLAUSE_CHAIN (c) = *clauses;
18025 *clauses = c;
18026 }
18027 }
18028 }
18029 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18030 if (COMPARISON_CLASS_P (cond))
18031 {
18032 tree op0 = RECUR (TREE_OPERAND (cond, 0));
18033 tree op1 = RECUR (TREE_OPERAND (cond, 1));
18034 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
18035 }
18036 else
18037 cond = RECUR (cond);
18038 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18039 switch (TREE_CODE (incr))
18040 {
18041 case PREINCREMENT_EXPR:
18042 case PREDECREMENT_EXPR:
18043 case POSTINCREMENT_EXPR:
18044 case POSTDECREMENT_EXPR:
18045 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
18046 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
18047 break;
18048 case MODIFY_EXPR:
18049 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18050 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18051 {
18052 tree rhs = TREE_OPERAND (incr, 1);
18053 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18054 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18055 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18056 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18057 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18058 rhs0, rhs1));
18059 }
18060 else
18061 incr = RECUR (incr);
18062 break;
18063 case MODOP_EXPR:
18064 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18065 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18066 {
18067 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18068 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18069 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
18070 TREE_TYPE (decl), lhs,
18071 RECUR (TREE_OPERAND (incr, 2))));
18072 }
18073 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
18074 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
18075 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
18076 {
18077 tree rhs = TREE_OPERAND (incr, 2);
18078 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18079 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18080 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18081 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18082 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18083 rhs0, rhs1));
18084 }
18085 else
18086 incr = RECUR (incr);
18087 break;
18088 default:
18089 incr = RECUR (incr);
18090 break;
18091 }
18092
18093 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18094 TREE_VEC_ELT (orig_declv, i) = decl;
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 false;
18100 #undef RECUR
18101 }
18102
18103 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18104 of OMP_TARGET's body. */
18105
18106 static tree
18107 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
18108 {
18109 *walk_subtrees = 0;
18110 switch (TREE_CODE (*tp))
18111 {
18112 case OMP_TEAMS:
18113 return *tp;
18114 case BIND_EXPR:
18115 case STATEMENT_LIST:
18116 *walk_subtrees = 1;
18117 break;
18118 default:
18119 break;
18120 }
18121 return NULL_TREE;
18122 }
18123
18124 /* Helper function for tsubst_expr. For decomposition declaration
18125 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18126 also the corresponding decls representing the identifiers
18127 of the decomposition declaration. Return DECL if successful
18128 or error_mark_node otherwise, set *FIRST to the first decl
18129 in the list chained through DECL_CHAIN and *CNT to the number
18130 of such decls. */
18131
18132 static tree
18133 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18134 tsubst_flags_t complain, tree in_decl, tree *first,
18135 unsigned int *cnt)
18136 {
18137 tree decl2, decl3, prev = decl;
18138 *cnt = 0;
18139 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18140 for (decl2 = DECL_CHAIN (pattern_decl);
18141 decl2
18142 && VAR_P (decl2)
18143 && DECL_DECOMPOSITION_P (decl2)
18144 && DECL_NAME (decl2);
18145 decl2 = DECL_CHAIN (decl2))
18146 {
18147 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
18148 {
18149 gcc_assert (errorcount);
18150 return error_mark_node;
18151 }
18152 (*cnt)++;
18153 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18154 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18155 tree v = DECL_VALUE_EXPR (decl2);
18156 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18157 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18158 decl3 = tsubst (decl2, args, complain, in_decl);
18159 SET_DECL_VALUE_EXPR (decl2, v);
18160 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18161 if (VAR_P (decl3))
18162 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18163 else
18164 {
18165 gcc_assert (errorcount);
18166 decl = error_mark_node;
18167 continue;
18168 }
18169 maybe_push_decl (decl3);
18170 if (error_operand_p (decl3))
18171 decl = error_mark_node;
18172 else if (decl != error_mark_node
18173 && DECL_CHAIN (decl3) != prev
18174 && decl != prev)
18175 {
18176 gcc_assert (errorcount);
18177 decl = error_mark_node;
18178 }
18179 else
18180 prev = decl3;
18181 }
18182 *first = prev;
18183 return decl;
18184 }
18185
18186 /* Return the proper local_specialization for init-capture pack DECL. */
18187
18188 static tree
18189 lookup_init_capture_pack (tree decl)
18190 {
18191 /* We handle normal pack captures by forwarding to the specialization of the
18192 captured parameter. We can't do that for pack init-captures; we need them
18193 to have their own local_specialization. We created the individual
18194 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18195 when we process the DECL_EXPR for the pack init-capture in the template.
18196 So, how do we find them? We don't know the capture proxy pack when
18197 building the individual resulting proxies, and we don't know the
18198 individual proxies when instantiating the pack. What we have in common is
18199 the FIELD_DECL.
18200
18201 So...when we instantiate the FIELD_DECL, we stick the result in
18202 local_specializations. Then at the DECL_EXPR we look up that result, see
18203 how many elements it has, synthesize the names, and look them up. */
18204
18205 tree cname = DECL_NAME (decl);
18206 tree val = DECL_VALUE_EXPR (decl);
18207 tree field = TREE_OPERAND (val, 1);
18208 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18209 tree fpack = retrieve_local_specialization (field);
18210 if (fpack == error_mark_node)
18211 return error_mark_node;
18212
18213 int len = 1;
18214 tree vec = NULL_TREE;
18215 tree r = NULL_TREE;
18216 if (TREE_CODE (fpack) == TREE_VEC)
18217 {
18218 len = TREE_VEC_LENGTH (fpack);
18219 vec = make_tree_vec (len);
18220 r = make_node (NONTYPE_ARGUMENT_PACK);
18221 SET_ARGUMENT_PACK_ARGS (r, vec);
18222 }
18223 for (int i = 0; i < len; ++i)
18224 {
18225 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18226 tree elt = lookup_name (ename);
18227 if (vec)
18228 TREE_VEC_ELT (vec, i) = elt;
18229 else
18230 r = elt;
18231 }
18232 return r;
18233 }
18234
18235 /* Like tsubst_copy for expressions, etc. but also does semantic
18236 processing. */
18237
18238 tree
18239 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18240 bool integral_constant_expression_p)
18241 {
18242 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18243 #define RECUR(NODE) \
18244 tsubst_expr ((NODE), args, complain, in_decl, \
18245 integral_constant_expression_p)
18246
18247 tree stmt, tmp;
18248 tree r;
18249 location_t loc;
18250
18251 if (t == NULL_TREE || t == error_mark_node)
18252 return t;
18253
18254 loc = input_location;
18255 if (location_t eloc = cp_expr_location (t))
18256 input_location = eloc;
18257 if (STATEMENT_CODE_P (TREE_CODE (t)))
18258 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18259
18260 switch (TREE_CODE (t))
18261 {
18262 case STATEMENT_LIST:
18263 {
18264 tree_stmt_iterator i;
18265 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
18266 RECUR (tsi_stmt (i));
18267 break;
18268 }
18269
18270 case CTOR_INITIALIZER:
18271 finish_mem_initializers (tsubst_initializer_list
18272 (TREE_OPERAND (t, 0), args));
18273 break;
18274
18275 case RETURN_EXPR:
18276 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18277 break;
18278
18279 case CO_RETURN_EXPR:
18280 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18281 break;
18282
18283 case CO_YIELD_EXPR:
18284 stmt = finish_co_yield_expr (input_location,
18285 RECUR (TREE_OPERAND (t, 0)));
18286 RETURN (stmt);
18287 break;
18288
18289 case CO_AWAIT_EXPR:
18290 stmt = finish_co_await_expr (input_location,
18291 RECUR (TREE_OPERAND (t, 0)));
18292 RETURN (stmt);
18293 break;
18294
18295 case EXPR_STMT:
18296 tmp = RECUR (EXPR_STMT_EXPR (t));
18297 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18298 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18299 else
18300 finish_expr_stmt (tmp);
18301 break;
18302
18303 case USING_STMT:
18304 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18305 break;
18306
18307 case DECL_EXPR:
18308 {
18309 tree decl, pattern_decl;
18310 tree init;
18311
18312 pattern_decl = decl = DECL_EXPR_DECL (t);
18313 if (TREE_CODE (decl) == LABEL_DECL)
18314 finish_label_decl (DECL_NAME (decl));
18315 else if (TREE_CODE (decl) == USING_DECL)
18316 {
18317 tree scope = USING_DECL_SCOPE (decl);
18318 if (DECL_DEPENDENT_P (decl))
18319 {
18320 scope = tsubst (scope, args, complain, in_decl);
18321 if (!MAYBE_CLASS_TYPE_P (scope)
18322 && TREE_CODE (scope) != ENUMERAL_TYPE)
18323 {
18324 if (complain & tf_error)
18325 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18326 "class, namespace, or enumeration", scope);
18327 return error_mark_node;
18328 }
18329 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18330 }
18331 else
18332 {
18333 /* This is a non-dependent using-decl, and we'll have
18334 used the names it found during template parsing. We do
18335 not want to do the lookup again, because we might not
18336 find the things we found then. */
18337 gcc_checking_assert (scope == tsubst (scope, args,
18338 complain, in_decl));
18339 /* We still need to push the bindings so that we can look up
18340 this name later. */
18341 push_using_decl_bindings (DECL_NAME (decl),
18342 USING_DECL_DECLS (decl));
18343 }
18344 }
18345 else if (is_capture_proxy (decl)
18346 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18347 {
18348 /* We're in tsubst_lambda_expr, we've already inserted a new
18349 capture proxy, so look it up and register it. */
18350 tree inst;
18351 if (!DECL_PACK_P (decl))
18352 {
18353 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18354 LOOK_want::HIDDEN_LAMBDA);
18355 gcc_assert (inst != decl && is_capture_proxy (inst));
18356 }
18357 else if (is_normal_capture_proxy (decl))
18358 {
18359 inst = (retrieve_local_specialization
18360 (DECL_CAPTURED_VARIABLE (decl)));
18361 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18362 || DECL_PACK_P (inst));
18363 }
18364 else
18365 inst = lookup_init_capture_pack (decl);
18366
18367 register_local_specialization (inst, decl);
18368 break;
18369 }
18370 else if (DECL_PRETTY_FUNCTION_P (decl))
18371 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18372 DECL_NAME (decl),
18373 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18374 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18375 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18376 /* Don't copy the old closure; we'll create a new one in
18377 tsubst_lambda_expr. */
18378 break;
18379 else
18380 {
18381 init = DECL_INITIAL (decl);
18382 decl = tsubst (decl, args, complain, in_decl);
18383 if (decl != error_mark_node)
18384 {
18385 /* By marking the declaration as instantiated, we avoid
18386 trying to instantiate it. Since instantiate_decl can't
18387 handle local variables, and since we've already done
18388 all that needs to be done, that's the right thing to
18389 do. */
18390 if (VAR_P (decl))
18391 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18392 if (VAR_P (decl) && !DECL_NAME (decl)
18393 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18394 /* Anonymous aggregates are a special case. */
18395 finish_anon_union (decl);
18396 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18397 {
18398 DECL_CONTEXT (decl) = current_function_decl;
18399 if (DECL_NAME (decl) == this_identifier)
18400 {
18401 tree lam = DECL_CONTEXT (current_function_decl);
18402 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18403 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18404 }
18405 insert_capture_proxy (decl);
18406 }
18407 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18408 /* We already did a pushtag. */;
18409 else if (VAR_OR_FUNCTION_DECL_P (decl)
18410 && DECL_LOCAL_DECL_P (decl))
18411 {
18412 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18413 DECL_CONTEXT (decl) = NULL_TREE;
18414 decl = pushdecl (decl);
18415 if (TREE_CODE (decl) == FUNCTION_DECL
18416 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18417 && cp_check_omp_declare_reduction (decl))
18418 instantiate_body (pattern_decl, args, decl, true);
18419 }
18420 else
18421 {
18422 bool const_init = false;
18423 unsigned int cnt = 0;
18424 tree first = NULL_TREE, ndecl = error_mark_node;
18425 tree asmspec_tree = NULL_TREE;
18426 maybe_push_decl (decl);
18427
18428 if (VAR_P (decl)
18429 && DECL_DECOMPOSITION_P (decl)
18430 && TREE_TYPE (pattern_decl) != error_mark_node)
18431 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18432 complain, in_decl, &first,
18433 &cnt);
18434
18435 init = tsubst_init (init, decl, args, complain, in_decl);
18436
18437 if (VAR_P (decl))
18438 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18439 (pattern_decl));
18440
18441 if (ndecl != error_mark_node)
18442 cp_maybe_mangle_decomp (ndecl, first, cnt);
18443
18444 /* In a non-template function, VLA type declarations are
18445 handled in grokdeclarator; for templates, handle them
18446 now. */
18447 predeclare_vla (decl);
18448
18449 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18450 {
18451 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18452 const char *asmspec = IDENTIFIER_POINTER (id);
18453 gcc_assert (asmspec[0] == '*');
18454 asmspec_tree
18455 = build_string (IDENTIFIER_LENGTH (id) - 1,
18456 asmspec + 1);
18457 TREE_TYPE (asmspec_tree) = char_array_type_node;
18458 }
18459
18460 cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
18461
18462 if (ndecl != error_mark_node)
18463 cp_finish_decomp (ndecl, first, cnt);
18464 }
18465 }
18466 }
18467
18468 break;
18469 }
18470
18471 case FOR_STMT:
18472 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18473 RECUR (FOR_INIT_STMT (t));
18474 finish_init_stmt (stmt);
18475 tmp = RECUR (FOR_COND (t));
18476 finish_for_cond (tmp, stmt, false, 0);
18477 tmp = RECUR (FOR_EXPR (t));
18478 finish_for_expr (tmp, stmt);
18479 {
18480 bool prev = note_iteration_stmt_body_start ();
18481 RECUR (FOR_BODY (t));
18482 note_iteration_stmt_body_end (prev);
18483 }
18484 finish_for_stmt (stmt);
18485 break;
18486
18487 case RANGE_FOR_STMT:
18488 {
18489 /* Construct another range_for, if this is not a final
18490 substitution (for inside a generic lambda of a
18491 template). Otherwise convert to a regular for. */
18492 tree decl, expr;
18493 stmt = (processing_template_decl
18494 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18495 : begin_for_stmt (NULL_TREE, NULL_TREE));
18496 RECUR (RANGE_FOR_INIT_STMT (t));
18497 decl = RANGE_FOR_DECL (t);
18498 decl = tsubst (decl, args, complain, in_decl);
18499 maybe_push_decl (decl);
18500 expr = RECUR (RANGE_FOR_EXPR (t));
18501
18502 tree decomp_first = NULL_TREE;
18503 unsigned decomp_cnt = 0;
18504 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18505 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18506 complain, in_decl,
18507 &decomp_first, &decomp_cnt);
18508
18509 if (processing_template_decl)
18510 {
18511 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18512 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18513 finish_range_for_decl (stmt, decl, expr);
18514 if (decomp_first && decl != error_mark_node)
18515 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18516 }
18517 else
18518 {
18519 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18520 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18521 stmt = cp_convert_range_for (stmt, decl, expr,
18522 decomp_first, decomp_cnt,
18523 RANGE_FOR_IVDEP (t), unroll);
18524 }
18525
18526 bool prev = note_iteration_stmt_body_start ();
18527 RECUR (RANGE_FOR_BODY (t));
18528 note_iteration_stmt_body_end (prev);
18529 finish_for_stmt (stmt);
18530 }
18531 break;
18532
18533 case WHILE_STMT:
18534 stmt = begin_while_stmt ();
18535 tmp = RECUR (WHILE_COND (t));
18536 finish_while_stmt_cond (tmp, stmt, false, 0);
18537 {
18538 bool prev = note_iteration_stmt_body_start ();
18539 RECUR (WHILE_BODY (t));
18540 note_iteration_stmt_body_end (prev);
18541 }
18542 finish_while_stmt (stmt);
18543 break;
18544
18545 case DO_STMT:
18546 stmt = begin_do_stmt ();
18547 {
18548 bool prev = note_iteration_stmt_body_start ();
18549 RECUR (DO_BODY (t));
18550 note_iteration_stmt_body_end (prev);
18551 }
18552 finish_do_body (stmt);
18553 tmp = RECUR (DO_COND (t));
18554 finish_do_stmt (tmp, stmt, false, 0);
18555 break;
18556
18557 case IF_STMT:
18558 stmt = begin_if_stmt ();
18559 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18560 if (IF_STMT_CONSTEXPR_P (t))
18561 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
18562 tmp = RECUR (IF_COND (t));
18563 tmp = finish_if_stmt_cond (tmp, stmt);
18564 if (IF_STMT_CONSTEXPR_P (t)
18565 && instantiation_dependent_expression_p (tmp))
18566 {
18567 /* We're partially instantiating a generic lambda, but the condition
18568 of the constexpr if is still dependent. Don't substitute into the
18569 branches now, just remember the template arguments. */
18570 do_poplevel (IF_SCOPE (stmt));
18571 IF_COND (stmt) = IF_COND (t);
18572 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18573 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18574 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18575 add_stmt (stmt);
18576 break;
18577 }
18578 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18579 /* Don't instantiate the THEN_CLAUSE. */;
18580 else
18581 {
18582 tree folded = fold_non_dependent_expr (tmp, complain);
18583 bool inhibit = integer_zerop (folded);
18584 if (inhibit)
18585 ++c_inhibit_evaluation_warnings;
18586 RECUR (THEN_CLAUSE (t));
18587 if (inhibit)
18588 --c_inhibit_evaluation_warnings;
18589 }
18590 finish_then_clause (stmt);
18591
18592 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18593 /* Don't instantiate the ELSE_CLAUSE. */;
18594 else if (ELSE_CLAUSE (t))
18595 {
18596 tree folded = fold_non_dependent_expr (tmp, complain);
18597 bool inhibit = integer_nonzerop (folded);
18598 begin_else_clause (stmt);
18599 if (inhibit)
18600 ++c_inhibit_evaluation_warnings;
18601 RECUR (ELSE_CLAUSE (t));
18602 if (inhibit)
18603 --c_inhibit_evaluation_warnings;
18604 finish_else_clause (stmt);
18605 }
18606
18607 finish_if_stmt (stmt);
18608 break;
18609
18610 case BIND_EXPR:
18611 if (BIND_EXPR_BODY_BLOCK (t))
18612 stmt = begin_function_body ();
18613 else
18614 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18615 ? BCS_TRY_BLOCK : 0);
18616
18617 RECUR (BIND_EXPR_BODY (t));
18618
18619 if (BIND_EXPR_BODY_BLOCK (t))
18620 finish_function_body (stmt);
18621 else
18622 finish_compound_stmt (stmt);
18623 break;
18624
18625 case BREAK_STMT:
18626 finish_break_stmt ();
18627 break;
18628
18629 case CONTINUE_STMT:
18630 finish_continue_stmt ();
18631 break;
18632
18633 case SWITCH_STMT:
18634 stmt = begin_switch_stmt ();
18635 tmp = RECUR (SWITCH_STMT_COND (t));
18636 finish_switch_cond (tmp, stmt);
18637 RECUR (SWITCH_STMT_BODY (t));
18638 finish_switch_stmt (stmt);
18639 break;
18640
18641 case CASE_LABEL_EXPR:
18642 {
18643 tree decl = CASE_LABEL (t);
18644 tree low = RECUR (CASE_LOW (t));
18645 tree high = RECUR (CASE_HIGH (t));
18646 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18647 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18648 {
18649 tree label = CASE_LABEL (l);
18650 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18651 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18652 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18653 }
18654 }
18655 break;
18656
18657 case LABEL_EXPR:
18658 {
18659 tree decl = LABEL_EXPR_LABEL (t);
18660 tree label;
18661
18662 label = finish_label_stmt (DECL_NAME (decl));
18663 if (TREE_CODE (label) == LABEL_DECL)
18664 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18665 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18666 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18667 }
18668 break;
18669
18670 case GOTO_EXPR:
18671 tmp = GOTO_DESTINATION (t);
18672 if (TREE_CODE (tmp) != LABEL_DECL)
18673 /* Computed goto's must be tsubst'd into. On the other hand,
18674 non-computed gotos must not be; the identifier in question
18675 will have no binding. */
18676 tmp = RECUR (tmp);
18677 else
18678 tmp = DECL_NAME (tmp);
18679 finish_goto_stmt (tmp);
18680 break;
18681
18682 case ASM_EXPR:
18683 {
18684 tree string = RECUR (ASM_STRING (t));
18685 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18686 complain, in_decl);
18687 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18688 complain, in_decl);
18689 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18690 complain, in_decl);
18691 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18692 complain, in_decl);
18693 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18694 outputs, inputs, clobbers, labels,
18695 ASM_INLINE_P (t));
18696 tree asm_expr = tmp;
18697 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18698 asm_expr = TREE_OPERAND (asm_expr, 0);
18699 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18700 }
18701 break;
18702
18703 case TRY_BLOCK:
18704 if (CLEANUP_P (t))
18705 {
18706 stmt = begin_try_block ();
18707 RECUR (TRY_STMTS (t));
18708 finish_cleanup_try_block (stmt);
18709 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18710 }
18711 else
18712 {
18713 tree compound_stmt = NULL_TREE;
18714
18715 if (FN_TRY_BLOCK_P (t))
18716 stmt = begin_function_try_block (&compound_stmt);
18717 else
18718 stmt = begin_try_block ();
18719
18720 RECUR (TRY_STMTS (t));
18721
18722 if (FN_TRY_BLOCK_P (t))
18723 finish_function_try_block (stmt);
18724 else
18725 finish_try_block (stmt);
18726
18727 RECUR (TRY_HANDLERS (t));
18728 if (FN_TRY_BLOCK_P (t))
18729 finish_function_handler_sequence (stmt, compound_stmt);
18730 else
18731 finish_handler_sequence (stmt);
18732 }
18733 break;
18734
18735 case HANDLER:
18736 {
18737 tree decl = HANDLER_PARMS (t);
18738
18739 if (decl)
18740 {
18741 decl = tsubst (decl, args, complain, in_decl);
18742 /* Prevent instantiate_decl from trying to instantiate
18743 this variable. We've already done all that needs to be
18744 done. */
18745 if (decl != error_mark_node)
18746 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18747 }
18748 stmt = begin_handler ();
18749 finish_handler_parms (decl, stmt);
18750 RECUR (HANDLER_BODY (t));
18751 finish_handler (stmt);
18752 }
18753 break;
18754
18755 case TAG_DEFN:
18756 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18757 if (CLASS_TYPE_P (tmp))
18758 {
18759 /* Local classes are not independent templates; they are
18760 instantiated along with their containing function. And this
18761 way we don't have to deal with pushing out of one local class
18762 to instantiate a member of another local class. */
18763 /* Closures are handled by the LAMBDA_EXPR. */
18764 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18765 complete_type (tmp);
18766 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18767 if ((VAR_P (fld)
18768 || (TREE_CODE (fld) == FUNCTION_DECL
18769 && !DECL_ARTIFICIAL (fld)))
18770 && DECL_TEMPLATE_INSTANTIATION (fld))
18771 instantiate_decl (fld, /*defer_ok=*/false,
18772 /*expl_inst_class=*/false);
18773 }
18774 break;
18775
18776 case STATIC_ASSERT:
18777 {
18778 tree condition;
18779
18780 ++c_inhibit_evaluation_warnings;
18781 condition =
18782 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18783 args,
18784 complain, in_decl,
18785 /*integral_constant_expression_p=*/true);
18786 --c_inhibit_evaluation_warnings;
18787
18788 finish_static_assert (condition,
18789 STATIC_ASSERT_MESSAGE (t),
18790 STATIC_ASSERT_SOURCE_LOCATION (t),
18791 /*member_p=*/false, /*show_expr_p=*/true);
18792 }
18793 break;
18794
18795 case OACC_KERNELS:
18796 case OACC_PARALLEL:
18797 case OACC_SERIAL:
18798 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18799 in_decl);
18800 stmt = begin_omp_parallel ();
18801 RECUR (OMP_BODY (t));
18802 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18803 break;
18804
18805 case OMP_PARALLEL:
18806 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18807 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18808 complain, in_decl);
18809 if (OMP_PARALLEL_COMBINED (t))
18810 omp_parallel_combined_clauses = &tmp;
18811 stmt = begin_omp_parallel ();
18812 RECUR (OMP_PARALLEL_BODY (t));
18813 gcc_assert (omp_parallel_combined_clauses == NULL);
18814 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18815 = OMP_PARALLEL_COMBINED (t);
18816 pop_omp_privatization_clauses (r);
18817 break;
18818
18819 case OMP_TASK:
18820 if (OMP_TASK_BODY (t) == NULL_TREE)
18821 {
18822 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18823 complain, in_decl);
18824 t = copy_node (t);
18825 OMP_TASK_CLAUSES (t) = tmp;
18826 add_stmt (t);
18827 break;
18828 }
18829 r = push_omp_privatization_clauses (false);
18830 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18831 complain, in_decl);
18832 stmt = begin_omp_task ();
18833 RECUR (OMP_TASK_BODY (t));
18834 finish_omp_task (tmp, stmt);
18835 pop_omp_privatization_clauses (r);
18836 break;
18837
18838 case OMP_FOR:
18839 case OMP_LOOP:
18840 case OMP_SIMD:
18841 case OMP_DISTRIBUTE:
18842 case OMP_TASKLOOP:
18843 case OACC_LOOP:
18844 {
18845 tree clauses, body, pre_body;
18846 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18847 tree orig_declv = NULL_TREE;
18848 tree incrv = NULL_TREE;
18849 enum c_omp_region_type ort = C_ORT_OMP;
18850 bool any_range_for = false;
18851 int i;
18852
18853 if (TREE_CODE (t) == OACC_LOOP)
18854 ort = C_ORT_ACC;
18855
18856 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18857 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18858 in_decl);
18859 if (OMP_FOR_INIT (t) != NULL_TREE)
18860 {
18861 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18862 if (OMP_FOR_ORIG_DECLS (t))
18863 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18864 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18865 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18866 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18867 }
18868
18869 keep_next_level (true);
18870 stmt = begin_omp_structured_block ();
18871
18872 pre_body = push_stmt_list ();
18873 RECUR (OMP_FOR_PRE_BODY (t));
18874 pre_body = pop_stmt_list (pre_body);
18875
18876 if (OMP_FOR_INIT (t) != NULL_TREE)
18877 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18878 any_range_for
18879 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18880 condv, incrv, &clauses, args,
18881 complain, in_decl,
18882 integral_constant_expression_p);
18883 omp_parallel_combined_clauses = NULL;
18884
18885 if (any_range_for)
18886 {
18887 gcc_assert (orig_declv);
18888 body = begin_omp_structured_block ();
18889 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18890 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18891 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18892 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18893 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18894 TREE_VEC_ELT (declv, i));
18895 }
18896 else
18897 body = push_stmt_list ();
18898 RECUR (OMP_FOR_BODY (t));
18899 if (any_range_for)
18900 body = finish_omp_structured_block (body);
18901 else
18902 body = pop_stmt_list (body);
18903
18904 if (OMP_FOR_INIT (t) != NULL_TREE)
18905 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18906 orig_declv, initv, condv, incrv, body, pre_body,
18907 NULL, clauses);
18908 else
18909 {
18910 t = make_node (TREE_CODE (t));
18911 TREE_TYPE (t) = void_type_node;
18912 OMP_FOR_BODY (t) = body;
18913 OMP_FOR_PRE_BODY (t) = pre_body;
18914 OMP_FOR_CLAUSES (t) = clauses;
18915 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18916 add_stmt (t);
18917 }
18918
18919 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18920 t));
18921 pop_omp_privatization_clauses (r);
18922 }
18923 break;
18924
18925 case OMP_SECTIONS:
18926 omp_parallel_combined_clauses = NULL;
18927 /* FALLTHRU */
18928 case OMP_SINGLE:
18929 case OMP_TEAMS:
18930 case OMP_CRITICAL:
18931 case OMP_TASKGROUP:
18932 case OMP_SCAN:
18933 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18934 && OMP_TEAMS_COMBINED (t));
18935 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18936 in_decl);
18937 if (TREE_CODE (t) == OMP_TEAMS)
18938 {
18939 keep_next_level (true);
18940 stmt = begin_omp_structured_block ();
18941 RECUR (OMP_BODY (t));
18942 stmt = finish_omp_structured_block (stmt);
18943 }
18944 else
18945 {
18946 stmt = push_stmt_list ();
18947 RECUR (OMP_BODY (t));
18948 stmt = pop_stmt_list (stmt);
18949 }
18950
18951 if (TREE_CODE (t) == OMP_CRITICAL
18952 && tmp != NULL_TREE
18953 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18954 {
18955 error_at (OMP_CLAUSE_LOCATION (tmp),
18956 "%<#pragma omp critical%> with %<hint%> clause requires "
18957 "a name, except when %<omp_sync_hint_none%> is used");
18958 RETURN (error_mark_node);
18959 }
18960 t = copy_node (t);
18961 OMP_BODY (t) = stmt;
18962 OMP_CLAUSES (t) = tmp;
18963 add_stmt (t);
18964 pop_omp_privatization_clauses (r);
18965 break;
18966
18967 case OMP_DEPOBJ:
18968 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18969 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18970 {
18971 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18972 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18973 {
18974 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18975 args, complain, in_decl);
18976 if (tmp == NULL_TREE)
18977 tmp = error_mark_node;
18978 }
18979 else
18980 {
18981 kind = (enum omp_clause_depend_kind)
18982 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18983 tmp = NULL_TREE;
18984 }
18985 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18986 }
18987 else
18988 finish_omp_depobj (EXPR_LOCATION (t), r,
18989 OMP_CLAUSE_DEPEND_SOURCE,
18990 OMP_DEPOBJ_CLAUSES (t));
18991 break;
18992
18993 case OACC_DATA:
18994 case OMP_TARGET_DATA:
18995 case OMP_TARGET:
18996 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18997 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18998 in_decl);
18999 keep_next_level (true);
19000 stmt = begin_omp_structured_block ();
19001
19002 RECUR (OMP_BODY (t));
19003 stmt = finish_omp_structured_block (stmt);
19004
19005 t = copy_node (t);
19006 OMP_BODY (t) = stmt;
19007 OMP_CLAUSES (t) = tmp;
19008 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
19009 {
19010 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
19011 if (teams)
19012 {
19013 /* For combined target teams, ensure the num_teams and
19014 thread_limit clause expressions are evaluated on the host,
19015 before entering the target construct. */
19016 tree c;
19017 for (c = OMP_TEAMS_CLAUSES (teams);
19018 c; c = OMP_CLAUSE_CHAIN (c))
19019 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19020 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19021 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
19022 {
19023 tree expr = OMP_CLAUSE_OPERAND (c, 0);
19024 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
19025 if (expr == error_mark_node)
19026 continue;
19027 tmp = TARGET_EXPR_SLOT (expr);
19028 add_stmt (expr);
19029 OMP_CLAUSE_OPERAND (c, 0) = expr;
19030 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19031 OMP_CLAUSE_FIRSTPRIVATE);
19032 OMP_CLAUSE_DECL (tc) = tmp;
19033 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
19034 OMP_TARGET_CLAUSES (t) = tc;
19035 }
19036 }
19037 }
19038 add_stmt (t);
19039 break;
19040
19041 case OACC_DECLARE:
19042 t = copy_node (t);
19043 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
19044 complain, in_decl);
19045 OACC_DECLARE_CLAUSES (t) = tmp;
19046 add_stmt (t);
19047 break;
19048
19049 case OMP_TARGET_UPDATE:
19050 case OMP_TARGET_ENTER_DATA:
19051 case OMP_TARGET_EXIT_DATA:
19052 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
19053 complain, in_decl);
19054 t = copy_node (t);
19055 OMP_STANDALONE_CLAUSES (t) = tmp;
19056 add_stmt (t);
19057 break;
19058
19059 case OACC_CACHE:
19060 case OACC_ENTER_DATA:
19061 case OACC_EXIT_DATA:
19062 case OACC_UPDATE:
19063 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
19064 complain, in_decl);
19065 t = copy_node (t);
19066 OMP_STANDALONE_CLAUSES (t) = tmp;
19067 add_stmt (t);
19068 break;
19069
19070 case OMP_ORDERED:
19071 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
19072 complain, in_decl);
19073 stmt = push_stmt_list ();
19074 RECUR (OMP_BODY (t));
19075 stmt = pop_stmt_list (stmt);
19076
19077 t = copy_node (t);
19078 OMP_BODY (t) = stmt;
19079 OMP_ORDERED_CLAUSES (t) = tmp;
19080 add_stmt (t);
19081 break;
19082
19083 case OMP_MASTER:
19084 omp_parallel_combined_clauses = NULL;
19085 /* FALLTHRU */
19086 case OMP_SECTION:
19087 stmt = push_stmt_list ();
19088 RECUR (OMP_BODY (t));
19089 stmt = pop_stmt_list (stmt);
19090
19091 t = copy_node (t);
19092 OMP_BODY (t) = stmt;
19093 add_stmt (t);
19094 break;
19095
19096 case OMP_ATOMIC:
19097 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
19098 tmp = NULL_TREE;
19099 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
19100 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
19101 complain, in_decl);
19102 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
19103 {
19104 tree op1 = TREE_OPERAND (t, 1);
19105 tree rhs1 = NULL_TREE;
19106 tree lhs, rhs;
19107 if (TREE_CODE (op1) == COMPOUND_EXPR)
19108 {
19109 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19110 op1 = TREE_OPERAND (op1, 1);
19111 }
19112 lhs = RECUR (TREE_OPERAND (op1, 0));
19113 rhs = RECUR (TREE_OPERAND (op1, 1));
19114 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19115 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
19116 OMP_ATOMIC_MEMORY_ORDER (t));
19117 }
19118 else
19119 {
19120 tree op1 = TREE_OPERAND (t, 1);
19121 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19122 tree rhs1 = NULL_TREE;
19123 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19124 enum tree_code opcode = NOP_EXPR;
19125 if (code == OMP_ATOMIC_READ)
19126 {
19127 v = RECUR (TREE_OPERAND (op1, 0));
19128 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19129 }
19130 else if (code == OMP_ATOMIC_CAPTURE_OLD
19131 || code == OMP_ATOMIC_CAPTURE_NEW)
19132 {
19133 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19134 v = RECUR (TREE_OPERAND (op1, 0));
19135 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19136 if (TREE_CODE (op11) == COMPOUND_EXPR)
19137 {
19138 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19139 op11 = TREE_OPERAND (op11, 1);
19140 }
19141 lhs = RECUR (TREE_OPERAND (op11, 0));
19142 rhs = RECUR (TREE_OPERAND (op11, 1));
19143 opcode = TREE_CODE (op11);
19144 if (opcode == MODIFY_EXPR)
19145 opcode = NOP_EXPR;
19146 }
19147 else
19148 {
19149 code = OMP_ATOMIC;
19150 lhs = RECUR (TREE_OPERAND (op1, 0));
19151 rhs = RECUR (TREE_OPERAND (op1, 1));
19152 }
19153 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19154 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
19155 }
19156 break;
19157
19158 case TRANSACTION_EXPR:
19159 {
19160 int flags = 0;
19161 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19162 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19163
19164 if (TRANSACTION_EXPR_IS_STMT (t))
19165 {
19166 tree body = TRANSACTION_EXPR_BODY (t);
19167 tree noex = NULL_TREE;
19168 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19169 {
19170 noex = MUST_NOT_THROW_COND (body);
19171 if (noex == NULL_TREE)
19172 noex = boolean_true_node;
19173 body = TREE_OPERAND (body, 0);
19174 }
19175 stmt = begin_transaction_stmt (input_location, NULL, flags);
19176 RECUR (body);
19177 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19178 }
19179 else
19180 {
19181 stmt = build_transaction_expr (EXPR_LOCATION (t),
19182 RECUR (TRANSACTION_EXPR_BODY (t)),
19183 flags, NULL_TREE);
19184 RETURN (stmt);
19185 }
19186 }
19187 break;
19188
19189 case MUST_NOT_THROW_EXPR:
19190 {
19191 tree op0 = RECUR (TREE_OPERAND (t, 0));
19192 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19193 RETURN (build_must_not_throw_expr (op0, cond));
19194 }
19195
19196 case EXPR_PACK_EXPANSION:
19197 error ("invalid use of pack expansion expression");
19198 RETURN (error_mark_node);
19199
19200 case NONTYPE_ARGUMENT_PACK:
19201 error ("use %<...%> to expand argument pack");
19202 RETURN (error_mark_node);
19203
19204 case COMPOUND_EXPR:
19205 tmp = RECUR (TREE_OPERAND (t, 0));
19206 if (tmp == NULL_TREE)
19207 /* If the first operand was a statement, we're done with it. */
19208 RETURN (RECUR (TREE_OPERAND (t, 1)));
19209 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19210 RECUR (TREE_OPERAND (t, 1)),
19211 complain));
19212
19213 case ANNOTATE_EXPR:
19214 tmp = RECUR (TREE_OPERAND (t, 0));
19215 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19216 TREE_TYPE (tmp), tmp,
19217 RECUR (TREE_OPERAND (t, 1)),
19218 RECUR (TREE_OPERAND (t, 2))));
19219
19220 case PREDICT_EXPR:
19221 RETURN (add_stmt (copy_node (t)));
19222
19223 default:
19224 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19225
19226 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19227 /*function_p=*/false,
19228 integral_constant_expression_p));
19229 }
19230
19231 RETURN (NULL_TREE);
19232 out:
19233 input_location = loc;
19234 return r;
19235 #undef RECUR
19236 #undef RETURN
19237 }
19238
19239 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19240 function. For description of the body see comment above
19241 cp_parser_omp_declare_reduction_exprs. */
19242
19243 static void
19244 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19245 {
19246 if (t == NULL_TREE || t == error_mark_node)
19247 return;
19248
19249 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19250
19251 tree_stmt_iterator tsi;
19252 int i;
19253 tree stmts[7];
19254 memset (stmts, 0, sizeof stmts);
19255 for (i = 0, tsi = tsi_start (t);
19256 i < 7 && !tsi_end_p (tsi);
19257 i++, tsi_next (&tsi))
19258 stmts[i] = tsi_stmt (tsi);
19259 gcc_assert (tsi_end_p (tsi));
19260
19261 if (i >= 3)
19262 {
19263 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19264 && TREE_CODE (stmts[1]) == DECL_EXPR);
19265 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19266 args, complain, in_decl);
19267 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19268 args, complain, in_decl);
19269 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19270 expect to be pushing it. */
19271 DECL_CONTEXT (omp_out) = current_function_decl;
19272 DECL_CONTEXT (omp_in) = current_function_decl;
19273 keep_next_level (true);
19274 tree block = begin_omp_structured_block ();
19275 tsubst_expr (stmts[2], args, complain, in_decl, false);
19276 block = finish_omp_structured_block (block);
19277 block = maybe_cleanup_point_expr_void (block);
19278 add_decl_expr (omp_out);
19279 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
19280 TREE_NO_WARNING (omp_out) = 1;
19281 add_decl_expr (omp_in);
19282 finish_expr_stmt (block);
19283 }
19284 if (i >= 6)
19285 {
19286 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19287 && TREE_CODE (stmts[4]) == DECL_EXPR);
19288 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19289 args, complain, in_decl);
19290 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19291 args, complain, in_decl);
19292 DECL_CONTEXT (omp_priv) = current_function_decl;
19293 DECL_CONTEXT (omp_orig) = current_function_decl;
19294 keep_next_level (true);
19295 tree block = begin_omp_structured_block ();
19296 tsubst_expr (stmts[5], args, complain, in_decl, false);
19297 block = finish_omp_structured_block (block);
19298 block = maybe_cleanup_point_expr_void (block);
19299 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19300 add_decl_expr (omp_priv);
19301 add_decl_expr (omp_orig);
19302 finish_expr_stmt (block);
19303 if (i == 7)
19304 add_decl_expr (omp_orig);
19305 }
19306 }
19307
19308 /* T is a postfix-expression that is not being used in a function
19309 call. Return the substituted version of T. */
19310
19311 static tree
19312 tsubst_non_call_postfix_expression (tree t, tree args,
19313 tsubst_flags_t complain,
19314 tree in_decl)
19315 {
19316 if (TREE_CODE (t) == SCOPE_REF)
19317 t = tsubst_qualified_id (t, args, complain, in_decl,
19318 /*done=*/false, /*address_p=*/false);
19319 else
19320 t = tsubst_copy_and_build (t, args, complain, in_decl,
19321 /*function_p=*/false,
19322 /*integral_constant_expression_p=*/false);
19323
19324 return t;
19325 }
19326
19327 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19328 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19329 dependent init-capture. */
19330
19331 static void
19332 prepend_one_capture (tree field, tree init, tree &list,
19333 tsubst_flags_t complain)
19334 {
19335 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19336 {
19337 tree type = NULL_TREE;
19338 if (!init)
19339 {
19340 if (complain & tf_error)
19341 error ("empty initializer in lambda init-capture");
19342 init = error_mark_node;
19343 }
19344 else if (TREE_CODE (init) == TREE_LIST)
19345 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19346 if (!type)
19347 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19348 TREE_TYPE (field) = type;
19349 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19350 }
19351 list = tree_cons (field, init, list);
19352 }
19353
19354 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19355 instantiation context. Instantiating a pack expansion containing a lambda
19356 might result in multiple lambdas all based on the same lambda in the
19357 template. */
19358
19359 tree
19360 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19361 {
19362 tree oldfn = lambda_function (t);
19363 in_decl = oldfn;
19364
19365 tree r = build_lambda_expr ();
19366
19367 LAMBDA_EXPR_LOCATION (r)
19368 = LAMBDA_EXPR_LOCATION (t);
19369 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19370 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19371 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19372 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19373 LAMBDA_EXPR_REGEN_INFO (r)
19374 = build_template_info (t, add_to_template_args (TI_ARGS (ti), args));
19375 else
19376 LAMBDA_EXPR_REGEN_INFO (r)
19377 = build_template_info (t, args);
19378
19379 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19380 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19381
19382 vec<tree,va_gc>* field_packs = NULL;
19383
19384 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19385 cap = TREE_CHAIN (cap))
19386 {
19387 tree ofield = TREE_PURPOSE (cap);
19388 tree init = TREE_VALUE (cap);
19389 if (PACK_EXPANSION_P (init))
19390 init = tsubst_pack_expansion (init, args, complain, in_decl);
19391 else
19392 init = tsubst_copy_and_build (init, args, complain, in_decl,
19393 /*fn*/false, /*constexpr*/false);
19394
19395 if (init == error_mark_node)
19396 return error_mark_node;
19397
19398 if (init && TREE_CODE (init) == TREE_LIST)
19399 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19400
19401 if (!processing_template_decl
19402 && init && TREE_CODE (init) != TREE_VEC
19403 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19404 {
19405 /* For a VLA, simply tsubsting the field type won't work, we need to
19406 go through add_capture again. XXX do we want to do this for all
19407 captures? */
19408 tree name = (get_identifier
19409 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19410 tree ftype = TREE_TYPE (ofield);
19411 bool by_ref = (TYPE_REF_P (ftype)
19412 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19413 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19414 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19415 continue;
19416 }
19417
19418 if (PACK_EXPANSION_P (ofield))
19419 ofield = PACK_EXPANSION_PATTERN (ofield);
19420 tree field = tsubst_decl (ofield, args, complain);
19421
19422 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19423 {
19424 /* Remember these for when we've pushed local_specializations. */
19425 vec_safe_push (field_packs, ofield);
19426 vec_safe_push (field_packs, field);
19427 }
19428
19429 if (field == error_mark_node)
19430 return error_mark_node;
19431
19432 if (TREE_CODE (field) == TREE_VEC)
19433 {
19434 int len = TREE_VEC_LENGTH (field);
19435 gcc_assert (TREE_CODE (init) == TREE_VEC
19436 && TREE_VEC_LENGTH (init) == len);
19437 for (int i = 0; i < len; ++i)
19438 prepend_one_capture (TREE_VEC_ELT (field, i),
19439 TREE_VEC_ELT (init, i),
19440 LAMBDA_EXPR_CAPTURE_LIST (r),
19441 complain);
19442 }
19443 else
19444 {
19445 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19446 complain);
19447
19448 if (id_equal (DECL_NAME (field), "__this"))
19449 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19450 }
19451 }
19452
19453 tree type = begin_lambda_type (r);
19454 if (type == error_mark_node)
19455 return error_mark_node;
19456
19457 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19458 /* A lambda in a default argument outside a class gets no
19459 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19460 tsubst_default_argument calls start_lambda_scope, so we need to
19461 specifically ignore it here, and use the global scope. */
19462 record_null_lambda_scope (r);
19463 else
19464 record_lambda_scope (r);
19465
19466 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19467 determine_visibility (TYPE_NAME (type));
19468
19469 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19470
19471 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19472 ? DECL_TI_TEMPLATE (oldfn)
19473 : NULL_TREE);
19474
19475 tree fntype = static_fn_type (oldfn);
19476 if (oldtmpl)
19477 ++processing_template_decl;
19478 fntype = tsubst (fntype, args, complain, in_decl);
19479 if (oldtmpl)
19480 --processing_template_decl;
19481
19482 if (fntype == error_mark_node)
19483 r = error_mark_node;
19484 else
19485 {
19486 /* The body of a lambda-expression is not a subexpression of the
19487 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19488 which would be skipped if cp_unevaluated_operand. */
19489 cp_evaluated ev;
19490
19491 /* Fix the type of 'this'. */
19492 fntype = build_memfn_type (fntype, type,
19493 type_memfn_quals (fntype),
19494 type_memfn_rqual (fntype));
19495 tree fn, tmpl;
19496 if (oldtmpl)
19497 {
19498 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19499 if (tmpl == error_mark_node)
19500 {
19501 r = error_mark_node;
19502 goto out;
19503 }
19504 fn = DECL_TEMPLATE_RESULT (tmpl);
19505 finish_member_declaration (tmpl);
19506 }
19507 else
19508 {
19509 tmpl = NULL_TREE;
19510 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19511 if (fn == error_mark_node)
19512 {
19513 r = error_mark_node;
19514 goto out;
19515 }
19516 finish_member_declaration (fn);
19517 }
19518
19519 /* Let finish_function set this. */
19520 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19521
19522 bool nested = cfun;
19523 if (nested)
19524 push_function_context ();
19525 else
19526 /* Still increment function_depth so that we don't GC in the
19527 middle of an expression. */
19528 ++function_depth;
19529
19530 local_specialization_stack s (lss_copy);
19531
19532 tree body = start_lambda_function (fn, r);
19533
19534 /* Now record them for lookup_init_capture_pack. */
19535 int fplen = vec_safe_length (field_packs);
19536 for (int i = 0; i < fplen; )
19537 {
19538 tree pack = (*field_packs)[i++];
19539 tree inst = (*field_packs)[i++];
19540 register_local_specialization (inst, pack);
19541 }
19542 release_tree_vector (field_packs);
19543
19544 register_parameter_specializations (oldfn, fn);
19545
19546 if (oldtmpl)
19547 {
19548 /* We might not partially instantiate some parts of the function, so
19549 copy these flags from the original template. */
19550 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19551 current_function_returns_value = ol->returns_value;
19552 current_function_returns_null = ol->returns_null;
19553 current_function_returns_abnormally = ol->returns_abnormally;
19554 current_function_infinite_loop = ol->infinite_loop;
19555 }
19556
19557 /* [temp.deduct] A lambda-expression appearing in a function type or a
19558 template parameter is not considered part of the immediate context for
19559 the purposes of template argument deduction. */
19560 complain = tf_warning_or_error;
19561
19562 tree saved = DECL_SAVED_TREE (oldfn);
19563 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19564 /* We already have a body block from start_lambda_function, we don't
19565 need another to confuse NRV (91217). */
19566 saved = BIND_EXPR_BODY (saved);
19567
19568 tsubst_expr (saved, args, complain, r, /*constexpr*/false);
19569
19570 finish_lambda_function (body);
19571
19572 if (nested)
19573 pop_function_context ();
19574 else
19575 --function_depth;
19576
19577 /* The capture list was built up in reverse order; fix that now. */
19578 LAMBDA_EXPR_CAPTURE_LIST (r)
19579 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19580
19581 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19582
19583 maybe_add_lambda_conv_op (type);
19584 }
19585
19586 out:
19587 finish_struct (type, /*attr*/NULL_TREE);
19588
19589 insert_pending_capture_proxies ();
19590
19591 return r;
19592 }
19593
19594 /* Like tsubst but deals with expressions and performs semantic
19595 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19596 "F<TARGS> (ARGS)". */
19597
19598 tree
19599 tsubst_copy_and_build (tree t,
19600 tree args,
19601 tsubst_flags_t complain,
19602 tree in_decl,
19603 bool function_p,
19604 bool integral_constant_expression_p)
19605 {
19606 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19607 #define RECUR(NODE) \
19608 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19609 /*function_p=*/false, \
19610 integral_constant_expression_p)
19611
19612 tree retval, op1;
19613 location_t save_loc;
19614
19615 if (t == NULL_TREE || t == error_mark_node)
19616 return t;
19617
19618 save_loc = input_location;
19619 if (location_t eloc = cp_expr_location (t))
19620 input_location = eloc;
19621
19622 /* N3276 decltype magic only applies to calls at the top level or on the
19623 right side of a comma. */
19624 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19625 complain &= ~tf_decltype;
19626
19627 switch (TREE_CODE (t))
19628 {
19629 case USING_DECL:
19630 t = DECL_NAME (t);
19631 /* Fall through. */
19632 case IDENTIFIER_NODE:
19633 {
19634 tree decl;
19635 cp_id_kind idk;
19636 bool non_integral_constant_expression_p;
19637 const char *error_msg;
19638
19639 if (IDENTIFIER_CONV_OP_P (t))
19640 {
19641 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19642 t = make_conv_op_name (new_type);
19643 }
19644
19645 /* Look up the name. */
19646 decl = lookup_name (t);
19647
19648 /* By convention, expressions use ERROR_MARK_NODE to indicate
19649 failure, not NULL_TREE. */
19650 if (decl == NULL_TREE)
19651 decl = error_mark_node;
19652
19653 decl = finish_id_expression (t, decl, NULL_TREE,
19654 &idk,
19655 integral_constant_expression_p,
19656 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19657 &non_integral_constant_expression_p,
19658 /*template_p=*/false,
19659 /*done=*/true,
19660 /*address_p=*/false,
19661 /*template_arg_p=*/false,
19662 &error_msg,
19663 input_location);
19664 if (error_msg)
19665 error (error_msg);
19666 if (!function_p && identifier_p (decl))
19667 {
19668 if (complain & tf_error)
19669 unqualified_name_lookup_error (decl);
19670 decl = error_mark_node;
19671 }
19672 RETURN (decl);
19673 }
19674
19675 case TEMPLATE_ID_EXPR:
19676 {
19677 tree object;
19678 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19679 complain, in_decl,
19680 function_p,
19681 integral_constant_expression_p);
19682 tree targs = TREE_OPERAND (t, 1);
19683
19684 if (targs)
19685 targs = tsubst_template_args (targs, args, complain, in_decl);
19686 if (targs == error_mark_node)
19687 RETURN (error_mark_node);
19688
19689 if (TREE_CODE (templ) == SCOPE_REF)
19690 {
19691 tree name = TREE_OPERAND (templ, 1);
19692 tree tid = lookup_template_function (name, targs);
19693 TREE_OPERAND (templ, 1) = tid;
19694 RETURN (templ);
19695 }
19696
19697 if (concept_definition_p (templ))
19698 {
19699 tree check = build_concept_check (templ, targs, complain);
19700 if (check == error_mark_node)
19701 RETURN (error_mark_node);
19702
19703 tree id = unpack_concept_check (check);
19704
19705 /* If we built a function concept check, return the underlying
19706 template-id. So we can evaluate it as a function call. */
19707 if (function_concept_p (TREE_OPERAND (id, 0)))
19708 RETURN (id);
19709
19710 RETURN (check);
19711 }
19712
19713 if (variable_template_p (templ))
19714 {
19715 tree r = lookup_and_finish_template_variable (templ, targs,
19716 complain);
19717 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19718 RETURN (r);
19719 }
19720
19721 if (TREE_CODE (templ) == COMPONENT_REF)
19722 {
19723 object = TREE_OPERAND (templ, 0);
19724 templ = TREE_OPERAND (templ, 1);
19725 }
19726 else
19727 object = NULL_TREE;
19728
19729 tree tid = lookup_template_function (templ, targs);
19730
19731 if (object)
19732 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19733 object, tid, NULL_TREE));
19734 else if (identifier_p (templ))
19735 {
19736 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19737 name lookup found nothing when parsing the template name. */
19738 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19739 RETURN (tid);
19740 }
19741 else
19742 RETURN (baselink_for_fns (tid));
19743 }
19744
19745 case INDIRECT_REF:
19746 {
19747 tree r = RECUR (TREE_OPERAND (t, 0));
19748
19749 if (REFERENCE_REF_P (t))
19750 {
19751 /* A type conversion to reference type will be enclosed in
19752 such an indirect ref, but the substitution of the cast
19753 will have also added such an indirect ref. */
19754 r = convert_from_reference (r);
19755 }
19756 else
19757 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19758 complain|decltype_flag);
19759
19760 if (REF_PARENTHESIZED_P (t))
19761 r = force_paren_expr (r);
19762
19763 RETURN (r);
19764 }
19765
19766 case NOP_EXPR:
19767 {
19768 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19769 tree op0 = RECUR (TREE_OPERAND (t, 0));
19770 RETURN (build_nop (type, op0));
19771 }
19772
19773 case IMPLICIT_CONV_EXPR:
19774 {
19775 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19776 tree expr = RECUR (TREE_OPERAND (t, 0));
19777 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19778 {
19779 retval = copy_node (t);
19780 TREE_TYPE (retval) = type;
19781 TREE_OPERAND (retval, 0) = expr;
19782 RETURN (retval);
19783 }
19784 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19785 /* We'll pass this to convert_nontype_argument again, we don't need
19786 to actually perform any conversion here. */
19787 RETURN (expr);
19788 int flags = LOOKUP_IMPLICIT;
19789 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19790 flags = LOOKUP_NORMAL;
19791 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19792 flags |= LOOKUP_NO_NARROWING;
19793 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19794 flags));
19795 }
19796
19797 case CONVERT_EXPR:
19798 {
19799 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19800 tree op0 = RECUR (TREE_OPERAND (t, 0));
19801 if (op0 == error_mark_node)
19802 RETURN (error_mark_node);
19803 RETURN (build1 (CONVERT_EXPR, type, op0));
19804 }
19805
19806 case CAST_EXPR:
19807 case REINTERPRET_CAST_EXPR:
19808 case CONST_CAST_EXPR:
19809 case DYNAMIC_CAST_EXPR:
19810 case STATIC_CAST_EXPR:
19811 {
19812 tree type;
19813 tree op, r = NULL_TREE;
19814
19815 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19816 if (integral_constant_expression_p
19817 && !cast_valid_in_integral_constant_expression_p (type))
19818 {
19819 if (complain & tf_error)
19820 error ("a cast to a type other than an integral or "
19821 "enumeration type cannot appear in a constant-expression");
19822 RETURN (error_mark_node);
19823 }
19824
19825 op = RECUR (TREE_OPERAND (t, 0));
19826
19827 warning_sentinel s(warn_useless_cast);
19828 warning_sentinel s2(warn_ignored_qualifiers);
19829 switch (TREE_CODE (t))
19830 {
19831 case CAST_EXPR:
19832 r = build_functional_cast (input_location, type, op, complain);
19833 break;
19834 case REINTERPRET_CAST_EXPR:
19835 r = build_reinterpret_cast (input_location, type, op, complain);
19836 break;
19837 case CONST_CAST_EXPR:
19838 r = build_const_cast (input_location, type, op, complain);
19839 break;
19840 case DYNAMIC_CAST_EXPR:
19841 r = build_dynamic_cast (input_location, type, op, complain);
19842 break;
19843 case STATIC_CAST_EXPR:
19844 r = build_static_cast (input_location, type, op, complain);
19845 if (IMPLICIT_RVALUE_P (t))
19846 set_implicit_rvalue_p (r);
19847 break;
19848 default:
19849 gcc_unreachable ();
19850 }
19851
19852 RETURN (r);
19853 }
19854
19855 case BIT_CAST_EXPR:
19856 {
19857 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19858 tree op0 = RECUR (TREE_OPERAND (t, 0));
19859 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
19860 }
19861
19862 case POSTDECREMENT_EXPR:
19863 case POSTINCREMENT_EXPR:
19864 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19865 args, complain, in_decl);
19866 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19867 complain|decltype_flag));
19868
19869 case PREDECREMENT_EXPR:
19870 case PREINCREMENT_EXPR:
19871 case NEGATE_EXPR:
19872 case BIT_NOT_EXPR:
19873 case ABS_EXPR:
19874 case TRUTH_NOT_EXPR:
19875 case UNARY_PLUS_EXPR: /* Unary + */
19876 case REALPART_EXPR:
19877 case IMAGPART_EXPR:
19878 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19879 RECUR (TREE_OPERAND (t, 0)),
19880 complain|decltype_flag));
19881
19882 case FIX_TRUNC_EXPR:
19883 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
19884 gcc_unreachable ();
19885
19886 case ADDR_EXPR:
19887 op1 = TREE_OPERAND (t, 0);
19888 if (TREE_CODE (op1) == LABEL_DECL)
19889 RETURN (finish_label_address_expr (DECL_NAME (op1),
19890 EXPR_LOCATION (op1)));
19891 if (TREE_CODE (op1) == SCOPE_REF)
19892 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19893 /*done=*/true, /*address_p=*/true);
19894 else
19895 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19896 in_decl);
19897 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19898 complain|decltype_flag));
19899
19900 case PLUS_EXPR:
19901 case MINUS_EXPR:
19902 case MULT_EXPR:
19903 case TRUNC_DIV_EXPR:
19904 case CEIL_DIV_EXPR:
19905 case FLOOR_DIV_EXPR:
19906 case ROUND_DIV_EXPR:
19907 case EXACT_DIV_EXPR:
19908 case BIT_AND_EXPR:
19909 case BIT_IOR_EXPR:
19910 case BIT_XOR_EXPR:
19911 case TRUNC_MOD_EXPR:
19912 case FLOOR_MOD_EXPR:
19913 case TRUTH_ANDIF_EXPR:
19914 case TRUTH_ORIF_EXPR:
19915 case TRUTH_AND_EXPR:
19916 case TRUTH_OR_EXPR:
19917 case RSHIFT_EXPR:
19918 case LSHIFT_EXPR:
19919 case EQ_EXPR:
19920 case NE_EXPR:
19921 case MAX_EXPR:
19922 case MIN_EXPR:
19923 case LE_EXPR:
19924 case GE_EXPR:
19925 case LT_EXPR:
19926 case GT_EXPR:
19927 case SPACESHIP_EXPR:
19928 case MEMBER_REF:
19929 case DOTSTAR_EXPR:
19930 {
19931 /* If either OP0 or OP1 was value- or type-dependent, suppress
19932 warnings that depend on the range of the types involved. */
19933 tree op0 = TREE_OPERAND (t, 0);
19934 tree op1 = TREE_OPERAND (t, 1);
19935 auto dep_p = [](tree t) {
19936 ++processing_template_decl;
19937 bool r = (potential_constant_expression (t)
19938 ? value_dependent_expression_p (t)
19939 : type_dependent_expression_p (t));
19940 --processing_template_decl;
19941 return r;
19942 };
19943 const bool was_dep = dep_p (op0) || dep_p (op1);
19944 op0 = RECUR (op0);
19945 op1 = RECUR (op1);
19946
19947 warning_sentinel s1(warn_type_limits, was_dep);
19948 warning_sentinel s2(warn_div_by_zero, was_dep);
19949 warning_sentinel s3(warn_logical_op, was_dep);
19950 warning_sentinel s4(warn_tautological_compare, was_dep);
19951
19952 tree r = build_x_binary_op
19953 (input_location, TREE_CODE (t),
19954 op0,
19955 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19956 ? ERROR_MARK
19957 : TREE_CODE (TREE_OPERAND (t, 0))),
19958 op1,
19959 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19960 ? ERROR_MARK
19961 : TREE_CODE (TREE_OPERAND (t, 1))),
19962 /*overload=*/NULL,
19963 complain|decltype_flag);
19964 if (EXPR_P (r) && TREE_NO_WARNING (t))
19965 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19966
19967 RETURN (r);
19968 }
19969
19970 case POINTER_PLUS_EXPR:
19971 {
19972 tree op0 = RECUR (TREE_OPERAND (t, 0));
19973 if (op0 == error_mark_node)
19974 RETURN (error_mark_node);
19975 tree op1 = RECUR (TREE_OPERAND (t, 1));
19976 if (op1 == error_mark_node)
19977 RETURN (error_mark_node);
19978 RETURN (fold_build_pointer_plus (op0, op1));
19979 }
19980
19981 case SCOPE_REF:
19982 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19983 /*address_p=*/false));
19984
19985 case BASELINK:
19986 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
19987 args, complain, in_decl));
19988
19989 case ARRAY_REF:
19990 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19991 args, complain, in_decl);
19992 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19993 RECUR (TREE_OPERAND (t, 1)),
19994 complain|decltype_flag));
19995
19996 case SIZEOF_EXPR:
19997 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19998 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19999 RETURN (tsubst_copy (t, args, complain, in_decl));
20000 /* Fall through */
20001
20002 case ALIGNOF_EXPR:
20003 {
20004 tree r;
20005
20006 op1 = TREE_OPERAND (t, 0);
20007 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
20008 op1 = TREE_TYPE (op1);
20009 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
20010 && ALIGNOF_EXPR_STD_P (t));
20011 if (!args)
20012 {
20013 /* When there are no ARGS, we are trying to evaluate a
20014 non-dependent expression from the parser. Trying to do
20015 the substitutions may not work. */
20016 if (!TYPE_P (op1))
20017 op1 = TREE_TYPE (op1);
20018 }
20019 else
20020 {
20021 ++cp_unevaluated_operand;
20022 ++c_inhibit_evaluation_warnings;
20023 if (TYPE_P (op1))
20024 op1 = tsubst (op1, args, complain, in_decl);
20025 else
20026 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
20027 /*function_p=*/false,
20028 /*integral_constant_expression_p=*/
20029 false);
20030 --cp_unevaluated_operand;
20031 --c_inhibit_evaluation_warnings;
20032 }
20033 if (TYPE_P (op1))
20034 r = cxx_sizeof_or_alignof_type (input_location,
20035 op1, TREE_CODE (t), std_alignof,
20036 complain & tf_error);
20037 else
20038 r = cxx_sizeof_or_alignof_expr (input_location,
20039 op1, TREE_CODE (t), std_alignof,
20040 complain & tf_error);
20041 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
20042 {
20043 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
20044 {
20045 if (!processing_template_decl && TYPE_P (op1))
20046 {
20047 r = build_min (SIZEOF_EXPR, size_type_node,
20048 build1 (NOP_EXPR, op1, error_mark_node));
20049 SIZEOF_EXPR_TYPE_P (r) = 1;
20050 }
20051 else
20052 r = build_min (SIZEOF_EXPR, size_type_node, op1);
20053 TREE_SIDE_EFFECTS (r) = 0;
20054 TREE_READONLY (r) = 1;
20055 }
20056 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
20057 }
20058 RETURN (r);
20059 }
20060
20061 case AT_ENCODE_EXPR:
20062 {
20063 op1 = TREE_OPERAND (t, 0);
20064 ++cp_unevaluated_operand;
20065 ++c_inhibit_evaluation_warnings;
20066 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
20067 /*function_p=*/false,
20068 /*integral_constant_expression_p=*/false);
20069 --cp_unevaluated_operand;
20070 --c_inhibit_evaluation_warnings;
20071 RETURN (objc_build_encode_expr (op1));
20072 }
20073
20074 case NOEXCEPT_EXPR:
20075 op1 = TREE_OPERAND (t, 0);
20076 ++cp_unevaluated_operand;
20077 ++c_inhibit_evaluation_warnings;
20078 ++cp_noexcept_operand;
20079 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
20080 /*function_p=*/false,
20081 /*integral_constant_expression_p=*/false);
20082 --cp_unevaluated_operand;
20083 --c_inhibit_evaluation_warnings;
20084 --cp_noexcept_operand;
20085 RETURN (finish_noexcept_expr (op1, complain));
20086
20087 case MODOP_EXPR:
20088 {
20089 warning_sentinel s(warn_div_by_zero);
20090 tree lhs = RECUR (TREE_OPERAND (t, 0));
20091 tree rhs = RECUR (TREE_OPERAND (t, 2));
20092 tree r = build_x_modify_expr
20093 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
20094 complain|decltype_flag);
20095 /* TREE_NO_WARNING must be set if either the expression was
20096 parenthesized or it uses an operator such as >>= rather
20097 than plain assignment. In the former case, it was already
20098 set and must be copied. In the latter case,
20099 build_x_modify_expr sets it and it must not be reset
20100 here. */
20101 if (TREE_NO_WARNING (t))
20102 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
20103
20104 RETURN (r);
20105 }
20106
20107 case ARROW_EXPR:
20108 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20109 args, complain, in_decl);
20110 /* Remember that there was a reference to this entity. */
20111 if (DECL_P (op1)
20112 && !mark_used (op1, complain) && !(complain & tf_error))
20113 RETURN (error_mark_node);
20114 RETURN (build_x_arrow (input_location, op1, complain));
20115
20116 case NEW_EXPR:
20117 {
20118 tree placement = RECUR (TREE_OPERAND (t, 0));
20119 tree init = RECUR (TREE_OPERAND (t, 3));
20120 vec<tree, va_gc> *placement_vec;
20121 vec<tree, va_gc> *init_vec;
20122 tree ret;
20123 location_t loc = EXPR_LOCATION (t);
20124
20125 if (placement == NULL_TREE)
20126 placement_vec = NULL;
20127 else if (placement == error_mark_node)
20128 RETURN (error_mark_node);
20129 else
20130 {
20131 placement_vec = make_tree_vector ();
20132 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20133 vec_safe_push (placement_vec, TREE_VALUE (placement));
20134 }
20135
20136 /* If there was an initializer in the original tree, but it
20137 instantiated to an empty list, then we should pass a
20138 non-NULL empty vector to tell build_new that it was an
20139 empty initializer() rather than no initializer. This can
20140 only happen when the initializer is a pack expansion whose
20141 parameter packs are of length zero. */
20142 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20143 init_vec = NULL;
20144 else if (init == error_mark_node)
20145 RETURN (error_mark_node);
20146 else
20147 {
20148 init_vec = make_tree_vector ();
20149 if (init == void_node)
20150 gcc_assert (init_vec != NULL);
20151 else
20152 {
20153 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20154 vec_safe_push (init_vec, TREE_VALUE (init));
20155 }
20156 }
20157
20158 /* Avoid passing an enclosing decl to valid_array_size_p. */
20159 in_decl = NULL_TREE;
20160
20161 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20162 tree op2 = RECUR (TREE_OPERAND (t, 2));
20163 ret = build_new (loc, &placement_vec, op1, op2,
20164 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20165 complain);
20166
20167 if (placement_vec != NULL)
20168 release_tree_vector (placement_vec);
20169 if (init_vec != NULL)
20170 release_tree_vector (init_vec);
20171
20172 RETURN (ret);
20173 }
20174
20175 case DELETE_EXPR:
20176 {
20177 tree op0 = RECUR (TREE_OPERAND (t, 0));
20178 tree op1 = RECUR (TREE_OPERAND (t, 1));
20179 RETURN (delete_sanity (input_location, op0, op1,
20180 DELETE_EXPR_USE_VEC (t),
20181 DELETE_EXPR_USE_GLOBAL (t),
20182 complain));
20183 }
20184
20185 case COMPOUND_EXPR:
20186 {
20187 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
20188 complain & ~tf_decltype, in_decl,
20189 /*function_p=*/false,
20190 integral_constant_expression_p);
20191 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20192 op0,
20193 RECUR (TREE_OPERAND (t, 1)),
20194 complain|decltype_flag));
20195 }
20196
20197 case CALL_EXPR:
20198 {
20199 tree function;
20200 unsigned int nargs, i;
20201 bool qualified_p;
20202 bool koenig_p;
20203 tree ret;
20204
20205 function = CALL_EXPR_FN (t);
20206 /* Internal function with no arguments. */
20207 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20208 RETURN (t);
20209
20210 /* When we parsed the expression, we determined whether or
20211 not Koenig lookup should be performed. */
20212 koenig_p = KOENIG_LOOKUP_P (t);
20213 if (function == NULL_TREE)
20214 {
20215 koenig_p = false;
20216 qualified_p = false;
20217 }
20218 else if (TREE_CODE (function) == SCOPE_REF)
20219 {
20220 qualified_p = true;
20221 function = tsubst_qualified_id (function, args, complain, in_decl,
20222 /*done=*/false,
20223 /*address_p=*/false);
20224 }
20225 else if (koenig_p && identifier_p (function))
20226 {
20227 /* Do nothing; calling tsubst_copy_and_build on an identifier
20228 would incorrectly perform unqualified lookup again.
20229
20230 Note that we can also have an IDENTIFIER_NODE if the earlier
20231 unqualified lookup found a member function; in that case
20232 koenig_p will be false and we do want to do the lookup
20233 again to find the instantiated member function.
20234
20235 FIXME but doing that causes c++/15272, so we need to stop
20236 using IDENTIFIER_NODE in that situation. */
20237 qualified_p = false;
20238 }
20239 else
20240 {
20241 if (TREE_CODE (function) == COMPONENT_REF)
20242 {
20243 tree op = TREE_OPERAND (function, 1);
20244
20245 qualified_p = (TREE_CODE (op) == SCOPE_REF
20246 || (BASELINK_P (op)
20247 && BASELINK_QUALIFIED_P (op)));
20248 }
20249 else
20250 qualified_p = false;
20251
20252 if (TREE_CODE (function) == ADDR_EXPR
20253 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20254 /* Avoid error about taking the address of a constructor. */
20255 function = TREE_OPERAND (function, 0);
20256
20257 function = tsubst_copy_and_build (function, args, complain,
20258 in_decl,
20259 !qualified_p,
20260 integral_constant_expression_p);
20261
20262 if (BASELINK_P (function))
20263 qualified_p = true;
20264 }
20265
20266 nargs = call_expr_nargs (t);
20267 releasing_vec call_args;
20268 for (i = 0; i < nargs; ++i)
20269 {
20270 tree arg = CALL_EXPR_ARG (t, i);
20271
20272 if (!PACK_EXPANSION_P (arg))
20273 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
20274 else
20275 {
20276 /* Expand the pack expansion and push each entry onto
20277 CALL_ARGS. */
20278 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20279 if (TREE_CODE (arg) == TREE_VEC)
20280 {
20281 unsigned int len, j;
20282
20283 len = TREE_VEC_LENGTH (arg);
20284 for (j = 0; j < len; ++j)
20285 {
20286 tree value = TREE_VEC_ELT (arg, j);
20287 if (value != NULL_TREE)
20288 value = convert_from_reference (value);
20289 vec_safe_push (call_args, value);
20290 }
20291 }
20292 else
20293 {
20294 /* A partial substitution. Add one entry. */
20295 vec_safe_push (call_args, arg);
20296 }
20297 }
20298 }
20299
20300 /* Stripped-down processing for a call in a thunk. Specifically, in
20301 the thunk template for a generic lambda. */
20302 if (call_from_lambda_thunk_p (t))
20303 {
20304 /* Now that we've expanded any packs, the number of call args
20305 might be different. */
20306 unsigned int cargs = call_args->length ();
20307 tree thisarg = NULL_TREE;
20308 if (TREE_CODE (function) == COMPONENT_REF)
20309 {
20310 thisarg = TREE_OPERAND (function, 0);
20311 if (TREE_CODE (thisarg) == INDIRECT_REF)
20312 thisarg = TREE_OPERAND (thisarg, 0);
20313 function = TREE_OPERAND (function, 1);
20314 if (TREE_CODE (function) == BASELINK)
20315 function = BASELINK_FUNCTIONS (function);
20316 }
20317 /* We aren't going to do normal overload resolution, so force the
20318 template-id to resolve. */
20319 function = resolve_nondeduced_context (function, complain);
20320 for (unsigned i = 0; i < cargs; ++i)
20321 {
20322 /* In a thunk, pass through args directly, without any
20323 conversions. */
20324 tree arg = (*call_args)[i];
20325 while (TREE_CODE (arg) != PARM_DECL)
20326 arg = TREE_OPERAND (arg, 0);
20327 (*call_args)[i] = arg;
20328 }
20329 if (thisarg)
20330 {
20331 /* If there are no other args, just push 'this'. */
20332 if (cargs == 0)
20333 vec_safe_push (call_args, thisarg);
20334 else
20335 {
20336 /* Otherwise, shift the other args over to make room. */
20337 tree last = (*call_args)[cargs - 1];
20338 vec_safe_push (call_args, last);
20339 for (int i = cargs - 1; i > 0; --i)
20340 (*call_args)[i] = (*call_args)[i - 1];
20341 (*call_args)[0] = thisarg;
20342 }
20343 }
20344 ret = build_call_a (function, call_args->length (),
20345 call_args->address ());
20346 /* The thunk location is not interesting. */
20347 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20348 CALL_FROM_THUNK_P (ret) = true;
20349 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20350 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20351
20352 RETURN (ret);
20353 }
20354
20355 /* We do not perform argument-dependent lookup if normal
20356 lookup finds a non-function, in accordance with the
20357 resolution of DR 218. */
20358 if (koenig_p
20359 && ((is_overloaded_fn (function)
20360 /* If lookup found a member function, the Koenig lookup is
20361 not appropriate, even if an unqualified-name was used
20362 to denote the function. */
20363 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20364 || identifier_p (function)
20365 /* C++20 P0846: Lookup found nothing. */
20366 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20367 && identifier_p (TREE_OPERAND (function, 0))))
20368 /* Only do this when substitution turns a dependent call
20369 into a non-dependent call. */
20370 && type_dependent_expression_p_push (t)
20371 && !any_type_dependent_arguments_p (call_args))
20372 function = perform_koenig_lookup (function, call_args, tf_none);
20373
20374 if (function != NULL_TREE
20375 && (identifier_p (function)
20376 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20377 && identifier_p (TREE_OPERAND (function, 0))
20378 && !any_dependent_template_arguments_p (TREE_OPERAND
20379 (function, 1))))
20380 && !any_type_dependent_arguments_p (call_args))
20381 {
20382 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20383 function = TREE_OPERAND (function, 0);
20384 if (koenig_p && (complain & tf_warning_or_error))
20385 {
20386 /* For backwards compatibility and good diagnostics, try
20387 the unqualified lookup again if we aren't in SFINAE
20388 context. */
20389 tree unq = (tsubst_copy_and_build
20390 (function, args, complain, in_decl, true,
20391 integral_constant_expression_p));
20392 if (unq == error_mark_node)
20393 RETURN (error_mark_node);
20394
20395 if (unq != function)
20396 {
20397 /* In a lambda fn, we have to be careful to not
20398 introduce new this captures. Legacy code can't
20399 be using lambdas anyway, so it's ok to be
20400 stricter. */
20401 bool in_lambda = (current_class_type
20402 && LAMBDA_TYPE_P (current_class_type));
20403 char const *const msg
20404 = G_("%qD was not declared in this scope, "
20405 "and no declarations were found by "
20406 "argument-dependent lookup at the point "
20407 "of instantiation");
20408
20409 bool diag = true;
20410 if (in_lambda)
20411 error_at (cp_expr_loc_or_input_loc (t),
20412 msg, function);
20413 else
20414 diag = permerror (cp_expr_loc_or_input_loc (t),
20415 msg, function);
20416 if (diag)
20417 {
20418 tree fn = unq;
20419
20420 if (INDIRECT_REF_P (fn))
20421 fn = TREE_OPERAND (fn, 0);
20422 if (is_overloaded_fn (fn))
20423 fn = get_first_fn (fn);
20424
20425 if (!DECL_P (fn))
20426 /* Can't say anything more. */;
20427 else if (DECL_CLASS_SCOPE_P (fn))
20428 {
20429 location_t loc = cp_expr_loc_or_input_loc (t);
20430 inform (loc,
20431 "declarations in dependent base %qT are "
20432 "not found by unqualified lookup",
20433 DECL_CLASS_CONTEXT (fn));
20434 if (current_class_ptr)
20435 inform (loc,
20436 "use %<this->%D%> instead", function);
20437 else
20438 inform (loc,
20439 "use %<%T::%D%> instead",
20440 current_class_name, function);
20441 }
20442 else
20443 inform (DECL_SOURCE_LOCATION (fn),
20444 "%qD declared here, later in the "
20445 "translation unit", fn);
20446 if (in_lambda)
20447 RETURN (error_mark_node);
20448 }
20449
20450 function = unq;
20451 }
20452 }
20453 if (identifier_p (function))
20454 {
20455 if (complain & tf_error)
20456 unqualified_name_lookup_error (function);
20457 RETURN (error_mark_node);
20458 }
20459 }
20460
20461 /* Remember that there was a reference to this entity. */
20462 if (function != NULL_TREE
20463 && DECL_P (function)
20464 && !mark_used (function, complain) && !(complain & tf_error))
20465 RETURN (error_mark_node);
20466
20467 /* Put back tf_decltype for the actual call. */
20468 complain |= decltype_flag;
20469
20470 if (function == NULL_TREE)
20471 switch (CALL_EXPR_IFN (t))
20472 {
20473 case IFN_LAUNDER:
20474 gcc_assert (nargs == 1);
20475 if (vec_safe_length (call_args) != 1)
20476 {
20477 error_at (cp_expr_loc_or_input_loc (t),
20478 "wrong number of arguments to "
20479 "%<__builtin_launder%>");
20480 ret = error_mark_node;
20481 }
20482 else
20483 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20484 (*call_args)[0], complain);
20485 break;
20486
20487 case IFN_VEC_CONVERT:
20488 gcc_assert (nargs == 1);
20489 if (vec_safe_length (call_args) != 1)
20490 {
20491 error_at (cp_expr_loc_or_input_loc (t),
20492 "wrong number of arguments to "
20493 "%<__builtin_convertvector%>");
20494 ret = error_mark_node;
20495 break;
20496 }
20497 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20498 tsubst (TREE_TYPE (t), args,
20499 complain, in_decl),
20500 complain);
20501 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20502 RETURN (ret);
20503 break;
20504
20505 default:
20506 /* Unsupported internal function with arguments. */
20507 gcc_unreachable ();
20508 }
20509 else if (TREE_CODE (function) == OFFSET_REF
20510 || TREE_CODE (function) == DOTSTAR_EXPR
20511 || TREE_CODE (function) == MEMBER_REF)
20512 ret = build_offset_ref_call_from_tree (function, &call_args,
20513 complain);
20514 else if (TREE_CODE (function) == COMPONENT_REF)
20515 {
20516 tree instance = TREE_OPERAND (function, 0);
20517 tree fn = TREE_OPERAND (function, 1);
20518
20519 if (processing_template_decl
20520 && (type_dependent_expression_p (instance)
20521 || (!BASELINK_P (fn)
20522 && TREE_CODE (fn) != FIELD_DECL)
20523 || type_dependent_expression_p (fn)
20524 || any_type_dependent_arguments_p (call_args)))
20525 ret = build_min_nt_call_vec (function, call_args);
20526 else if (!BASELINK_P (fn))
20527 ret = finish_call_expr (function, &call_args,
20528 /*disallow_virtual=*/false,
20529 /*koenig_p=*/false,
20530 complain);
20531 else
20532 ret = (build_new_method_call
20533 (instance, fn,
20534 &call_args, NULL_TREE,
20535 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20536 /*fn_p=*/NULL,
20537 complain));
20538 }
20539 else if (concept_check_p (function))
20540 {
20541 /* FUNCTION is a template-id referring to a concept definition. */
20542 tree id = unpack_concept_check (function);
20543 tree tmpl = TREE_OPERAND (id, 0);
20544 tree args = TREE_OPERAND (id, 1);
20545
20546 /* Calls to standard and variable concepts should have been
20547 previously diagnosed. */
20548 gcc_assert (function_concept_p (tmpl));
20549
20550 /* Ensure the result is wrapped as a call expression. */
20551 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20552 }
20553 else
20554 ret = finish_call_expr (function, &call_args,
20555 /*disallow_virtual=*/qualified_p,
20556 koenig_p,
20557 complain);
20558
20559 if (ret != error_mark_node)
20560 {
20561 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20562 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20563 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20564 if (op || ord || rev)
20565 {
20566 function = extract_call_expr (ret);
20567 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20568 CALL_EXPR_ORDERED_ARGS (function) = ord;
20569 CALL_EXPR_REVERSE_ARGS (function) = rev;
20570 }
20571 }
20572
20573 RETURN (ret);
20574 }
20575
20576 case COND_EXPR:
20577 {
20578 tree cond = RECUR (TREE_OPERAND (t, 0));
20579 cond = mark_rvalue_use (cond);
20580 tree folded_cond = fold_non_dependent_expr (cond, complain);
20581 tree exp1, exp2;
20582
20583 if (TREE_CODE (folded_cond) == INTEGER_CST)
20584 {
20585 if (integer_zerop (folded_cond))
20586 {
20587 ++c_inhibit_evaluation_warnings;
20588 exp1 = RECUR (TREE_OPERAND (t, 1));
20589 --c_inhibit_evaluation_warnings;
20590 exp2 = RECUR (TREE_OPERAND (t, 2));
20591 }
20592 else
20593 {
20594 exp1 = RECUR (TREE_OPERAND (t, 1));
20595 ++c_inhibit_evaluation_warnings;
20596 exp2 = RECUR (TREE_OPERAND (t, 2));
20597 --c_inhibit_evaluation_warnings;
20598 }
20599 cond = folded_cond;
20600 }
20601 else
20602 {
20603 exp1 = RECUR (TREE_OPERAND (t, 1));
20604 exp2 = RECUR (TREE_OPERAND (t, 2));
20605 }
20606
20607 warning_sentinel s(warn_duplicated_branches);
20608 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20609 cond, exp1, exp2, complain));
20610 }
20611
20612 case PSEUDO_DTOR_EXPR:
20613 {
20614 tree op0 = RECUR (TREE_OPERAND (t, 0));
20615 tree op1 = RECUR (TREE_OPERAND (t, 1));
20616 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20617 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20618 input_location));
20619 }
20620
20621 case TREE_LIST:
20622 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20623
20624 case COMPONENT_REF:
20625 {
20626 tree object;
20627 tree object_type;
20628 tree member;
20629 tree r;
20630
20631 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20632 args, complain, in_decl);
20633 /* Remember that there was a reference to this entity. */
20634 if (DECL_P (object)
20635 && !mark_used (object, complain) && !(complain & tf_error))
20636 RETURN (error_mark_node);
20637 object_type = TREE_TYPE (object);
20638
20639 member = TREE_OPERAND (t, 1);
20640 if (BASELINK_P (member))
20641 member = tsubst_baselink (member,
20642 non_reference (TREE_TYPE (object)),
20643 args, complain, in_decl);
20644 else
20645 member = tsubst_copy (member, args, complain, in_decl);
20646 if (member == error_mark_node)
20647 RETURN (error_mark_node);
20648
20649 if (TREE_CODE (member) == FIELD_DECL)
20650 {
20651 r = finish_non_static_data_member (member, object, NULL_TREE);
20652 if (TREE_CODE (r) == COMPONENT_REF)
20653 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20654 RETURN (r);
20655 }
20656 else if (type_dependent_expression_p (object))
20657 /* We can't do much here. */;
20658 else if (!CLASS_TYPE_P (object_type))
20659 {
20660 if (scalarish_type_p (object_type))
20661 {
20662 tree s = NULL_TREE;
20663 tree dtor = member;
20664
20665 if (TREE_CODE (dtor) == SCOPE_REF)
20666 {
20667 s = TREE_OPERAND (dtor, 0);
20668 dtor = TREE_OPERAND (dtor, 1);
20669 }
20670 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20671 {
20672 dtor = TREE_OPERAND (dtor, 0);
20673 if (TYPE_P (dtor))
20674 RETURN (finish_pseudo_destructor_expr
20675 (object, s, dtor, input_location));
20676 }
20677 }
20678 }
20679 else if (TREE_CODE (member) == SCOPE_REF
20680 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20681 {
20682 /* Lookup the template functions now that we know what the
20683 scope is. */
20684 tree scope = TREE_OPERAND (member, 0);
20685 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20686 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20687 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20688 /*complain=*/false);
20689 if (BASELINK_P (member))
20690 {
20691 BASELINK_FUNCTIONS (member)
20692 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20693 args);
20694 member = (adjust_result_of_qualified_name_lookup
20695 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20696 object_type));
20697 }
20698 else
20699 {
20700 qualified_name_lookup_error (scope, tmpl, member,
20701 input_location);
20702 RETURN (error_mark_node);
20703 }
20704 }
20705 else if (TREE_CODE (member) == SCOPE_REF
20706 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20707 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20708 {
20709 if (complain & tf_error)
20710 {
20711 if (TYPE_P (TREE_OPERAND (member, 0)))
20712 error ("%qT is not a class or namespace",
20713 TREE_OPERAND (member, 0));
20714 else
20715 error ("%qD is not a class or namespace",
20716 TREE_OPERAND (member, 0));
20717 }
20718 RETURN (error_mark_node);
20719 }
20720
20721 r = finish_class_member_access_expr (object, member,
20722 /*template_p=*/false,
20723 complain);
20724 if (TREE_CODE (r) == COMPONENT_REF)
20725 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20726 RETURN (r);
20727 }
20728
20729 case THROW_EXPR:
20730 RETURN (build_throw
20731 (input_location, RECUR (TREE_OPERAND (t, 0))));
20732
20733 case CONSTRUCTOR:
20734 {
20735 vec<constructor_elt, va_gc> *n;
20736 constructor_elt *ce;
20737 unsigned HOST_WIDE_INT idx;
20738 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20739 bool process_index_p;
20740 int newlen;
20741 bool need_copy_p = false;
20742 tree r;
20743
20744 if (type == error_mark_node)
20745 RETURN (error_mark_node);
20746
20747 /* We do not want to process the index of aggregate
20748 initializers as they are identifier nodes which will be
20749 looked up by digest_init. */
20750 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20751
20752 if (null_member_pointer_value_p (t))
20753 {
20754 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20755 RETURN (t);
20756 }
20757
20758 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20759 newlen = vec_safe_length (n);
20760 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20761 {
20762 if (ce->index && process_index_p
20763 /* An identifier index is looked up in the type
20764 being initialized, not the current scope. */
20765 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20766 ce->index = RECUR (ce->index);
20767
20768 if (PACK_EXPANSION_P (ce->value))
20769 {
20770 /* Substitute into the pack expansion. */
20771 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20772 in_decl);
20773
20774 if (ce->value == error_mark_node
20775 || PACK_EXPANSION_P (ce->value))
20776 ;
20777 else if (TREE_VEC_LENGTH (ce->value) == 1)
20778 /* Just move the argument into place. */
20779 ce->value = TREE_VEC_ELT (ce->value, 0);
20780 else
20781 {
20782 /* Update the length of the final CONSTRUCTOR
20783 arguments vector, and note that we will need to
20784 copy.*/
20785 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20786 need_copy_p = true;
20787 }
20788 }
20789 else
20790 ce->value = RECUR (ce->value);
20791 }
20792
20793 if (need_copy_p)
20794 {
20795 vec<constructor_elt, va_gc> *old_n = n;
20796
20797 vec_alloc (n, newlen);
20798 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20799 {
20800 if (TREE_CODE (ce->value) == TREE_VEC)
20801 {
20802 int i, len = TREE_VEC_LENGTH (ce->value);
20803 for (i = 0; i < len; ++i)
20804 CONSTRUCTOR_APPEND_ELT (n, 0,
20805 TREE_VEC_ELT (ce->value, i));
20806 }
20807 else
20808 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20809 }
20810 }
20811
20812 r = build_constructor (init_list_type_node, n);
20813 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20814 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20815 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20816
20817 if (TREE_HAS_CONSTRUCTOR (t))
20818 {
20819 fcl_t cl = fcl_functional;
20820 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20821 cl = fcl_c99;
20822 RETURN (finish_compound_literal (type, r, complain, cl));
20823 }
20824
20825 TREE_TYPE (r) = type;
20826 RETURN (r);
20827 }
20828
20829 case TYPEID_EXPR:
20830 {
20831 tree operand_0 = TREE_OPERAND (t, 0);
20832 if (TYPE_P (operand_0))
20833 {
20834 operand_0 = tsubst (operand_0, args, complain, in_decl);
20835 RETURN (get_typeid (operand_0, complain));
20836 }
20837 else
20838 {
20839 operand_0 = RECUR (operand_0);
20840 RETURN (build_typeid (operand_0, complain));
20841 }
20842 }
20843
20844 case VAR_DECL:
20845 if (!args)
20846 RETURN (t);
20847 /* Fall through */
20848
20849 case PARM_DECL:
20850 {
20851 tree r = tsubst_copy (t, args, complain, in_decl);
20852 /* ??? We're doing a subset of finish_id_expression here. */
20853 if (tree wrap = maybe_get_tls_wrapper_call (r))
20854 /* Replace an evaluated use of the thread_local variable with
20855 a call to its wrapper. */
20856 r = wrap;
20857 else if (outer_automatic_var_p (r))
20858 r = process_outer_var_ref (r, complain);
20859
20860 if (!TYPE_REF_P (TREE_TYPE (t)))
20861 /* If the original type was a reference, we'll be wrapped in
20862 the appropriate INDIRECT_REF. */
20863 r = convert_from_reference (r);
20864 RETURN (r);
20865 }
20866
20867 case VA_ARG_EXPR:
20868 {
20869 tree op0 = RECUR (TREE_OPERAND (t, 0));
20870 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20871 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20872 }
20873
20874 case OFFSETOF_EXPR:
20875 {
20876 tree object_ptr
20877 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20878 in_decl, /*function_p=*/false,
20879 /*integral_constant_expression_p=*/false);
20880 RETURN (finish_offsetof (object_ptr,
20881 RECUR (TREE_OPERAND (t, 0)),
20882 EXPR_LOCATION (t)));
20883 }
20884
20885 case ADDRESSOF_EXPR:
20886 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20887 RECUR (TREE_OPERAND (t, 0)), complain));
20888
20889 case TRAIT_EXPR:
20890 {
20891 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20892 complain, in_decl);
20893 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20894 complain, in_decl);
20895 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20896 TRAIT_EXPR_KIND (t), type1, type2));
20897 }
20898
20899 case STMT_EXPR:
20900 {
20901 tree old_stmt_expr = cur_stmt_expr;
20902 tree stmt_expr = begin_stmt_expr ();
20903
20904 cur_stmt_expr = stmt_expr;
20905 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20906 integral_constant_expression_p);
20907 stmt_expr = finish_stmt_expr (stmt_expr, false);
20908 cur_stmt_expr = old_stmt_expr;
20909
20910 /* If the resulting list of expression statement is empty,
20911 fold it further into void_node. */
20912 if (empty_expr_stmt_p (stmt_expr))
20913 stmt_expr = void_node;
20914
20915 RETURN (stmt_expr);
20916 }
20917
20918 case LAMBDA_EXPR:
20919 {
20920 if (complain & tf_partial)
20921 {
20922 /* We don't have a full set of template arguments yet; don't touch
20923 the lambda at all. */
20924 gcc_assert (processing_template_decl);
20925 return t;
20926 }
20927 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20928
20929 RETURN (build_lambda_object (r));
20930 }
20931
20932 case TARGET_EXPR:
20933 /* We can get here for a constant initializer of non-dependent type.
20934 FIXME stop folding in cp_parser_initializer_clause. */
20935 {
20936 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20937 complain);
20938 RETURN (r);
20939 }
20940
20941 case TRANSACTION_EXPR:
20942 RETURN (tsubst_expr(t, args, complain, in_decl,
20943 integral_constant_expression_p));
20944
20945 case PAREN_EXPR:
20946 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20947
20948 case VEC_PERM_EXPR:
20949 {
20950 tree op0 = RECUR (TREE_OPERAND (t, 0));
20951 tree op1 = RECUR (TREE_OPERAND (t, 1));
20952 tree op2 = RECUR (TREE_OPERAND (t, 2));
20953 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20954 complain));
20955 }
20956
20957 case REQUIRES_EXPR:
20958 {
20959 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20960 RETURN (r);
20961 }
20962
20963 case RANGE_EXPR:
20964 /* No need to substitute further, a RANGE_EXPR will always be built
20965 with constant operands. */
20966 RETURN (t);
20967
20968 case NON_LVALUE_EXPR:
20969 case VIEW_CONVERT_EXPR:
20970 if (location_wrapper_p (t))
20971 /* We need to do this here as well as in tsubst_copy so we get the
20972 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20973 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20974 EXPR_LOCATION (t)));
20975 /* fallthrough. */
20976
20977 default:
20978 /* Handle Objective-C++ constructs, if appropriate. */
20979 {
20980 tree subst
20981 = objcp_tsubst_copy_and_build (t, args, complain,
20982 in_decl, /*function_p=*/false);
20983 if (subst)
20984 RETURN (subst);
20985 }
20986 RETURN (tsubst_copy (t, args, complain, in_decl));
20987 }
20988
20989 #undef RECUR
20990 #undef RETURN
20991 out:
20992 input_location = save_loc;
20993 return retval;
20994 }
20995
20996 /* Verify that the instantiated ARGS are valid. For type arguments,
20997 make sure that the type's linkage is ok. For non-type arguments,
20998 make sure they are constants if they are integral or enumerations.
20999 Emit an error under control of COMPLAIN, and return TRUE on error. */
21000
21001 static bool
21002 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
21003 {
21004 if (dependent_template_arg_p (t))
21005 return false;
21006 if (ARGUMENT_PACK_P (t))
21007 {
21008 tree vec = ARGUMENT_PACK_ARGS (t);
21009 int len = TREE_VEC_LENGTH (vec);
21010 bool result = false;
21011 int i;
21012
21013 for (i = 0; i < len; ++i)
21014 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
21015 result = true;
21016 return result;
21017 }
21018 else if (TYPE_P (t))
21019 {
21020 /* [basic.link]: A name with no linkage (notably, the name
21021 of a class or enumeration declared in a local scope)
21022 shall not be used to declare an entity with linkage.
21023 This implies that names with no linkage cannot be used as
21024 template arguments
21025
21026 DR 757 relaxes this restriction for C++0x. */
21027 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
21028 : no_linkage_check (t, /*relaxed_p=*/false));
21029
21030 if (nt)
21031 {
21032 /* DR 488 makes use of a type with no linkage cause
21033 type deduction to fail. */
21034 if (complain & tf_error)
21035 {
21036 if (TYPE_UNNAMED_P (nt))
21037 error ("%qT is/uses unnamed type", t);
21038 else
21039 error ("template argument for %qD uses local type %qT",
21040 tmpl, t);
21041 }
21042 return true;
21043 }
21044 /* In order to avoid all sorts of complications, we do not
21045 allow variably-modified types as template arguments. */
21046 else if (variably_modified_type_p (t, NULL_TREE))
21047 {
21048 if (complain & tf_error)
21049 error ("%qT is a variably modified type", t);
21050 return true;
21051 }
21052 }
21053 /* Class template and alias template arguments should be OK. */
21054 else if (DECL_TYPE_TEMPLATE_P (t))
21055 ;
21056 /* A non-type argument of integral or enumerated type must be a
21057 constant. */
21058 else if (TREE_TYPE (t)
21059 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
21060 && !REFERENCE_REF_P (t)
21061 && !TREE_CONSTANT (t))
21062 {
21063 if (complain & tf_error)
21064 error ("integral expression %qE is not constant", t);
21065 return true;
21066 }
21067 return false;
21068 }
21069
21070 static bool
21071 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
21072 {
21073 int ix, len = DECL_NTPARMS (tmpl);
21074 bool result = false;
21075
21076 for (ix = 0; ix != len; ix++)
21077 {
21078 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
21079 result = true;
21080 }
21081 if (result && (complain & tf_error))
21082 error (" trying to instantiate %qD", tmpl);
21083 return result;
21084 }
21085
21086 /* We're out of SFINAE context now, so generate diagnostics for the access
21087 errors we saw earlier when instantiating D from TMPL and ARGS. */
21088
21089 static void
21090 recheck_decl_substitution (tree d, tree tmpl, tree args)
21091 {
21092 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
21093 tree type = TREE_TYPE (pattern);
21094 location_t loc = input_location;
21095
21096 push_access_scope (d);
21097 push_deferring_access_checks (dk_no_deferred);
21098 input_location = DECL_SOURCE_LOCATION (pattern);
21099 tsubst (type, args, tf_warning_or_error, d);
21100 input_location = loc;
21101 pop_deferring_access_checks ();
21102 pop_access_scope (d);
21103 }
21104
21105 /* Instantiate the indicated variable, function, or alias template TMPL with
21106 the template arguments in TARG_PTR. */
21107
21108 static tree
21109 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
21110 {
21111 tree targ_ptr = orig_args;
21112 tree fndecl;
21113 tree gen_tmpl;
21114 tree spec;
21115 bool access_ok = true;
21116
21117 if (tmpl == error_mark_node)
21118 return error_mark_node;
21119
21120 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
21121
21122 if (modules_p ())
21123 lazy_load_pendings (tmpl);
21124
21125 /* If this function is a clone, handle it specially. */
21126 if (DECL_CLONED_FUNCTION_P (tmpl))
21127 {
21128 tree spec;
21129 tree clone;
21130
21131 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21132 DECL_CLONED_FUNCTION. */
21133 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
21134 targ_ptr, complain);
21135 if (spec == error_mark_node)
21136 return error_mark_node;
21137
21138 /* Look for the clone. */
21139 FOR_EACH_CLONE (clone, spec)
21140 if (DECL_NAME (clone) == DECL_NAME (tmpl))
21141 return clone;
21142 /* We should always have found the clone by now. */
21143 gcc_unreachable ();
21144 return NULL_TREE;
21145 }
21146
21147 if (targ_ptr == error_mark_node)
21148 return error_mark_node;
21149
21150 /* Check to see if we already have this specialization. */
21151 gen_tmpl = most_general_template (tmpl);
21152 if (TMPL_ARGS_DEPTH (targ_ptr)
21153 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
21154 /* targ_ptr only has the innermost template args, so add the outer ones
21155 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21156 the case of a non-dependent call within a template definition). */
21157 targ_ptr = (add_outermost_template_args
21158 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
21159 targ_ptr));
21160
21161 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21162 but it doesn't seem to be on the hot path. */
21163 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
21164
21165 gcc_checking_assert (tmpl == gen_tmpl
21166 || ((fndecl
21167 = retrieve_specialization (tmpl, orig_args, 0))
21168 == spec)
21169 || fndecl == NULL_TREE);
21170
21171 if (spec != NULL_TREE)
21172 {
21173 if (FNDECL_HAS_ACCESS_ERRORS (spec))
21174 {
21175 if (complain & tf_error)
21176 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
21177 return error_mark_node;
21178 }
21179 return spec;
21180 }
21181
21182 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
21183 complain))
21184 return error_mark_node;
21185
21186 /* We are building a FUNCTION_DECL, during which the access of its
21187 parameters and return types have to be checked. However this
21188 FUNCTION_DECL which is the desired context for access checking
21189 is not built yet. We solve this chicken-and-egg problem by
21190 deferring all checks until we have the FUNCTION_DECL. */
21191 push_deferring_access_checks (dk_deferred);
21192
21193 /* Instantiation of the function happens in the context of the function
21194 template, not the context of the overload resolution we're doing. */
21195 push_to_top_level ();
21196 /* If there are dependent arguments, e.g. because we're doing partial
21197 ordering, make sure processing_template_decl stays set. */
21198 if (uses_template_parms (targ_ptr))
21199 ++processing_template_decl;
21200 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21201 {
21202 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
21203 complain, gen_tmpl, true);
21204 push_nested_class (ctx);
21205 }
21206
21207 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
21208
21209 fndecl = NULL_TREE;
21210 if (VAR_P (pattern))
21211 {
21212 /* We need to determine if we're using a partial or explicit
21213 specialization now, because the type of the variable could be
21214 different. */
21215 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
21216 tree elt = most_specialized_partial_spec (tid, complain);
21217 if (elt == error_mark_node)
21218 pattern = error_mark_node;
21219 else if (elt)
21220 {
21221 tree partial_tmpl = TREE_VALUE (elt);
21222 tree partial_args = TREE_PURPOSE (elt);
21223 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21224 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21225 }
21226 }
21227
21228 /* Substitute template parameters to obtain the specialization. */
21229 if (fndecl == NULL_TREE)
21230 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21231 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21232 pop_nested_class ();
21233 pop_from_top_level ();
21234
21235 if (fndecl == error_mark_node)
21236 {
21237 pop_deferring_access_checks ();
21238 return error_mark_node;
21239 }
21240
21241 /* The DECL_TI_TEMPLATE should always be the immediate parent
21242 template, not the most general template. */
21243 DECL_TI_TEMPLATE (fndecl) = tmpl;
21244 DECL_TI_ARGS (fndecl) = targ_ptr;
21245
21246 set_instantiating_module (fndecl);
21247
21248 /* Now we know the specialization, compute access previously
21249 deferred. Do no access control for inheriting constructors,
21250 as we already checked access for the inherited constructor. */
21251 if (!(flag_new_inheriting_ctors
21252 && DECL_INHERITED_CTOR (fndecl)))
21253 {
21254 push_access_scope (fndecl);
21255 if (!perform_deferred_access_checks (complain))
21256 access_ok = false;
21257 pop_access_scope (fndecl);
21258 }
21259 pop_deferring_access_checks ();
21260
21261 /* If we've just instantiated the main entry point for a function,
21262 instantiate all the alternate entry points as well. We do this
21263 by cloning the instantiation of the main entry point, not by
21264 instantiating the template clones. */
21265 if (tree chain = DECL_CHAIN (gen_tmpl))
21266 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21267 clone_cdtor (fndecl, /*update_methods=*/false);
21268
21269 if (!access_ok)
21270 {
21271 if (!(complain & tf_error))
21272 {
21273 /* Remember to reinstantiate when we're out of SFINAE so the user
21274 can see the errors. */
21275 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21276 }
21277 return error_mark_node;
21278 }
21279 return fndecl;
21280 }
21281
21282 /* Wrapper for instantiate_template_1. */
21283
21284 tree
21285 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21286 {
21287 tree ret;
21288 timevar_push (TV_TEMPLATE_INST);
21289 ret = instantiate_template_1 (tmpl, orig_args, complain);
21290 timevar_pop (TV_TEMPLATE_INST);
21291 return ret;
21292 }
21293
21294 /* Instantiate the alias template TMPL with ARGS. Also push a template
21295 instantiation level, which instantiate_template doesn't do because
21296 functions and variables have sufficient context established by the
21297 callers. */
21298
21299 static tree
21300 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21301 {
21302 if (tmpl == error_mark_node || args == error_mark_node)
21303 return error_mark_node;
21304
21305 args =
21306 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21307 args, tmpl, complain,
21308 /*require_all_args=*/true,
21309 /*use_default_args=*/true);
21310
21311 /* FIXME check for satisfaction in check_instantiated_args. */
21312 if (flag_concepts
21313 && !any_dependent_template_arguments_p (args)
21314 && !constraints_satisfied_p (tmpl, args))
21315 {
21316 if (complain & tf_error)
21317 {
21318 auto_diagnostic_group d;
21319 error ("template constraint failure for %qD", tmpl);
21320 diagnose_constraints (input_location, tmpl, args);
21321 }
21322 return error_mark_node;
21323 }
21324
21325 if (!push_tinst_level (tmpl, args))
21326 return error_mark_node;
21327 tree r = instantiate_template (tmpl, args, complain);
21328 pop_tinst_level ();
21329
21330 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
21331 {
21332 /* An alias template specialization can be dependent
21333 even if its underlying type is not. */
21334 TYPE_DEPENDENT_P (d) = true;
21335 TYPE_DEPENDENT_P_VALID (d) = true;
21336 /* Sometimes a dependent alias spec is equivalent to its expansion,
21337 sometimes not. So always use structural_comptypes. */
21338 SET_TYPE_STRUCTURAL_EQUALITY (d);
21339 }
21340
21341 return r;
21342 }
21343
21344 /* PARM is a template parameter pack for FN. Returns true iff
21345 PARM is used in a deducible way in the argument list of FN. */
21346
21347 static bool
21348 pack_deducible_p (tree parm, tree fn)
21349 {
21350 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21351 for (; t; t = TREE_CHAIN (t))
21352 {
21353 tree type = TREE_VALUE (t);
21354 tree packs;
21355 if (!PACK_EXPANSION_P (type))
21356 continue;
21357 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21358 packs; packs = TREE_CHAIN (packs))
21359 if (template_args_equal (TREE_VALUE (packs), parm))
21360 {
21361 /* The template parameter pack is used in a function parameter
21362 pack. If this is the end of the parameter list, the
21363 template parameter pack is deducible. */
21364 if (TREE_CHAIN (t) == void_list_node)
21365 return true;
21366 else
21367 /* Otherwise, not. Well, it could be deduced from
21368 a non-pack parameter, but doing so would end up with
21369 a deduction mismatch, so don't bother. */
21370 return false;
21371 }
21372 }
21373 /* The template parameter pack isn't used in any function parameter
21374 packs, but it might be used deeper, e.g. tuple<Args...>. */
21375 return true;
21376 }
21377
21378 /* Subroutine of fn_type_unification: check non-dependent parms for
21379 convertibility. */
21380
21381 static int
21382 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21383 tree fn, unification_kind_t strict, int flags,
21384 struct conversion **convs, bool explain_p)
21385 {
21386 /* Non-constructor methods need to leave a conversion for 'this', which
21387 isn't included in nargs here. */
21388 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21389 && !DECL_CONSTRUCTOR_P (fn));
21390
21391 for (unsigned ia = 0;
21392 parms && parms != void_list_node && ia < nargs; )
21393 {
21394 tree parm = TREE_VALUE (parms);
21395
21396 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21397 && (!TREE_CHAIN (parms)
21398 || TREE_CHAIN (parms) == void_list_node))
21399 /* For a function parameter pack that occurs at the end of the
21400 parameter-declaration-list, the type A of each remaining
21401 argument of the call is compared with the type P of the
21402 declarator-id of the function parameter pack. */
21403 break;
21404
21405 parms = TREE_CHAIN (parms);
21406
21407 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21408 /* For a function parameter pack that does not occur at the
21409 end of the parameter-declaration-list, the type of the
21410 parameter pack is a non-deduced context. */
21411 continue;
21412
21413 if (!uses_template_parms (parm))
21414 {
21415 tree arg = args[ia];
21416 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21417 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21418
21419 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21420 conv_p, explain_p))
21421 return 1;
21422 }
21423
21424 ++ia;
21425 }
21426
21427 return 0;
21428 }
21429
21430 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21431 NARGS elements of the arguments that are being used when calling
21432 it. TARGS is a vector into which the deduced template arguments
21433 are placed.
21434
21435 Returns either a FUNCTION_DECL for the matching specialization of FN or
21436 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21437 true, diagnostics will be printed to explain why it failed.
21438
21439 If FN is a conversion operator, or we are trying to produce a specific
21440 specialization, RETURN_TYPE is the return type desired.
21441
21442 The EXPLICIT_TARGS are explicit template arguments provided via a
21443 template-id.
21444
21445 The parameter STRICT is one of:
21446
21447 DEDUCE_CALL:
21448 We are deducing arguments for a function call, as in
21449 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21450 deducing arguments for a call to the result of a conversion
21451 function template, as in [over.call.object].
21452
21453 DEDUCE_CONV:
21454 We are deducing arguments for a conversion function, as in
21455 [temp.deduct.conv].
21456
21457 DEDUCE_EXACT:
21458 We are deducing arguments when doing an explicit instantiation
21459 as in [temp.explicit], when determining an explicit specialization
21460 as in [temp.expl.spec], or when taking the address of a function
21461 template, as in [temp.deduct.funcaddr]. */
21462
21463 tree
21464 fn_type_unification (tree fn,
21465 tree explicit_targs,
21466 tree targs,
21467 const tree *args,
21468 unsigned int nargs,
21469 tree return_type,
21470 unification_kind_t strict,
21471 int flags,
21472 struct conversion **convs,
21473 bool explain_p,
21474 bool decltype_p)
21475 {
21476 tree parms;
21477 tree fntype;
21478 tree decl = NULL_TREE;
21479 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21480 bool ok;
21481 static int deduction_depth;
21482 /* type_unification_real will pass back any access checks from default
21483 template argument substitution. */
21484 vec<deferred_access_check, va_gc> *checks = NULL;
21485 /* We don't have all the template args yet. */
21486 bool incomplete = true;
21487
21488 tree orig_fn = fn;
21489 if (flag_new_inheriting_ctors)
21490 fn = strip_inheriting_ctors (fn);
21491
21492 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21493 tree r = error_mark_node;
21494
21495 tree full_targs = targs;
21496 if (TMPL_ARGS_DEPTH (targs)
21497 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21498 full_targs = (add_outermost_template_args
21499 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21500 targs));
21501
21502 if (decltype_p)
21503 complain |= tf_decltype;
21504
21505 /* In C++0x, it's possible to have a function template whose type depends
21506 on itself recursively. This is most obvious with decltype, but can also
21507 occur with enumeration scope (c++/48969). So we need to catch infinite
21508 recursion and reject the substitution at deduction time; this function
21509 will return error_mark_node for any repeated substitution.
21510
21511 This also catches excessive recursion such as when f<N> depends on
21512 f<N-1> across all integers, and returns error_mark_node for all the
21513 substitutions back up to the initial one.
21514
21515 This is, of course, not reentrant. */
21516 if (excessive_deduction_depth)
21517 return error_mark_node;
21518 ++deduction_depth;
21519
21520 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21521
21522 fntype = TREE_TYPE (fn);
21523 if (explicit_targs)
21524 {
21525 /* [temp.deduct]
21526
21527 The specified template arguments must match the template
21528 parameters in kind (i.e., type, nontype, template), and there
21529 must not be more arguments than there are parameters;
21530 otherwise type deduction fails.
21531
21532 Nontype arguments must match the types of the corresponding
21533 nontype template parameters, or must be convertible to the
21534 types of the corresponding nontype parameters as specified in
21535 _temp.arg.nontype_, otherwise type deduction fails.
21536
21537 All references in the function type of the function template
21538 to the corresponding template parameters are replaced by the
21539 specified template argument values. If a substitution in a
21540 template parameter or in the function type of the function
21541 template results in an invalid type, type deduction fails. */
21542 int i, len = TREE_VEC_LENGTH (tparms);
21543 location_t loc = input_location;
21544 incomplete = false;
21545
21546 if (explicit_targs == error_mark_node)
21547 goto fail;
21548
21549 if (TMPL_ARGS_DEPTH (explicit_targs)
21550 < TMPL_ARGS_DEPTH (full_targs))
21551 explicit_targs = add_outermost_template_args (full_targs,
21552 explicit_targs);
21553
21554 /* Adjust any explicit template arguments before entering the
21555 substitution context. */
21556 explicit_targs
21557 = (coerce_template_parms (tparms, explicit_targs, fn,
21558 complain|tf_partial,
21559 /*require_all_args=*/false,
21560 /*use_default_args=*/false));
21561 if (explicit_targs == error_mark_node)
21562 goto fail;
21563
21564 /* Substitute the explicit args into the function type. This is
21565 necessary so that, for instance, explicitly declared function
21566 arguments can match null pointed constants. If we were given
21567 an incomplete set of explicit args, we must not do semantic
21568 processing during substitution as we could create partial
21569 instantiations. */
21570 for (i = 0; i < len; i++)
21571 {
21572 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21573 bool parameter_pack = false;
21574 tree targ = TREE_VEC_ELT (explicit_targs, i);
21575
21576 /* Dig out the actual parm. */
21577 if (TREE_CODE (parm) == TYPE_DECL
21578 || TREE_CODE (parm) == TEMPLATE_DECL)
21579 {
21580 parm = TREE_TYPE (parm);
21581 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21582 }
21583 else if (TREE_CODE (parm) == PARM_DECL)
21584 {
21585 parm = DECL_INITIAL (parm);
21586 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21587 }
21588
21589 if (targ == NULL_TREE)
21590 /* No explicit argument for this template parameter. */
21591 incomplete = true;
21592 else if (parameter_pack && pack_deducible_p (parm, fn))
21593 {
21594 /* Mark the argument pack as "incomplete". We could
21595 still deduce more arguments during unification.
21596 We remove this mark in type_unification_real. */
21597 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21598 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21599 = ARGUMENT_PACK_ARGS (targ);
21600
21601 /* We have some incomplete argument packs. */
21602 incomplete = true;
21603 }
21604 }
21605
21606 if (incomplete)
21607 {
21608 if (!push_tinst_level (fn, explicit_targs))
21609 {
21610 excessive_deduction_depth = true;
21611 goto fail;
21612 }
21613 ++processing_template_decl;
21614 input_location = DECL_SOURCE_LOCATION (fn);
21615 /* Ignore any access checks; we'll see them again in
21616 instantiate_template and they might have the wrong
21617 access path at this point. */
21618 push_deferring_access_checks (dk_deferred);
21619 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21620 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21621 pop_deferring_access_checks ();
21622 input_location = loc;
21623 --processing_template_decl;
21624 pop_tinst_level ();
21625
21626 if (fntype == error_mark_node)
21627 goto fail;
21628 }
21629
21630 /* Place the explicitly specified arguments in TARGS. */
21631 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21632 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21633 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21634 if (!incomplete && CHECKING_P
21635 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21636 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21637 (targs, NUM_TMPL_ARGS (explicit_targs));
21638 }
21639
21640 if (return_type && strict != DEDUCE_CALL)
21641 {
21642 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21643 new_args[0] = return_type;
21644 memcpy (new_args + 1, args, nargs * sizeof (tree));
21645 args = new_args;
21646 ++nargs;
21647 }
21648
21649 if (!incomplete)
21650 goto deduced;
21651
21652 /* Never do unification on the 'this' parameter. */
21653 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21654
21655 if (return_type && strict == DEDUCE_CALL)
21656 {
21657 /* We're deducing for a call to the result of a template conversion
21658 function. The parms we really want are in return_type. */
21659 if (INDIRECT_TYPE_P (return_type))
21660 return_type = TREE_TYPE (return_type);
21661 parms = TYPE_ARG_TYPES (return_type);
21662 }
21663 else if (return_type)
21664 {
21665 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21666 }
21667
21668 /* We allow incomplete unification without an error message here
21669 because the standard doesn't seem to explicitly prohibit it. Our
21670 callers must be ready to deal with unification failures in any
21671 event. */
21672
21673 /* If we aren't explaining yet, push tinst context so we can see where
21674 any errors (e.g. from class instantiations triggered by instantiation
21675 of default template arguments) come from. If we are explaining, this
21676 context is redundant. */
21677 if (!explain_p && !push_tinst_level (fn, targs))
21678 {
21679 excessive_deduction_depth = true;
21680 goto fail;
21681 }
21682
21683 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21684 full_targs, parms, args, nargs, /*subr=*/0,
21685 strict, &checks, explain_p);
21686 if (!explain_p)
21687 pop_tinst_level ();
21688 if (!ok)
21689 goto fail;
21690
21691 /* Now that we have bindings for all of the template arguments,
21692 ensure that the arguments deduced for the template template
21693 parameters have compatible template parameter lists. We cannot
21694 check this property before we have deduced all template
21695 arguments, because the template parameter types of a template
21696 template parameter might depend on prior template parameters
21697 deduced after the template template parameter. The following
21698 ill-formed example illustrates this issue:
21699
21700 template<typename T, template<T> class C> void f(C<5>, T);
21701
21702 template<int N> struct X {};
21703
21704 void g() {
21705 f(X<5>(), 5l); // error: template argument deduction fails
21706 }
21707
21708 The template parameter list of 'C' depends on the template type
21709 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21710 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21711 time that we deduce 'C'. */
21712 if (!template_template_parm_bindings_ok_p
21713 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21714 {
21715 unify_inconsistent_template_template_parameters (explain_p);
21716 goto fail;
21717 }
21718
21719 deduced:
21720
21721 /* CWG2369: Check satisfaction before non-deducible conversions. */
21722 if (!constraints_satisfied_p (fn, targs))
21723 {
21724 if (explain_p)
21725 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21726 goto fail;
21727 }
21728
21729 /* DR 1391: All parameters have args, now check non-dependent parms for
21730 convertibility. We don't do this if all args were explicitly specified,
21731 as the standard says that we substitute explicit args immediately. */
21732 if (incomplete
21733 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21734 convs, explain_p))
21735 goto fail;
21736
21737 /* All is well so far. Now, check:
21738
21739 [temp.deduct]
21740
21741 When all template arguments have been deduced, all uses of
21742 template parameters in nondeduced contexts are replaced with
21743 the corresponding deduced argument values. If the
21744 substitution results in an invalid type, as described above,
21745 type deduction fails. */
21746 if (!push_tinst_level (fn, targs))
21747 {
21748 excessive_deduction_depth = true;
21749 goto fail;
21750 }
21751
21752 /* Also collect access checks from the instantiation. */
21753 reopen_deferring_access_checks (checks);
21754
21755 decl = instantiate_template (fn, targs, complain);
21756
21757 checks = get_deferred_access_checks ();
21758 pop_deferring_access_checks ();
21759
21760 pop_tinst_level ();
21761
21762 if (decl == error_mark_node)
21763 goto fail;
21764
21765 /* Now perform any access checks encountered during substitution. */
21766 push_access_scope (decl);
21767 ok = perform_access_checks (checks, complain);
21768 pop_access_scope (decl);
21769 if (!ok)
21770 goto fail;
21771
21772 /* If we're looking for an exact match, check that what we got
21773 is indeed an exact match. It might not be if some template
21774 parameters are used in non-deduced contexts. But don't check
21775 for an exact match if we have dependent template arguments;
21776 in that case we're doing partial ordering, and we already know
21777 that we have two candidates that will provide the actual type. */
21778 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21779 {
21780 tree substed = TREE_TYPE (decl);
21781 unsigned int i;
21782
21783 tree sarg
21784 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21785 if (return_type)
21786 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21787 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21788 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21789 {
21790 unify_type_mismatch (explain_p, args[i],
21791 TREE_VALUE (sarg));
21792 goto fail;
21793 }
21794 }
21795
21796 /* After doing deduction with the inherited constructor, actually return an
21797 instantiation of the inheriting constructor. */
21798 if (orig_fn != fn)
21799 decl = instantiate_template (orig_fn, targs, complain);
21800
21801 r = decl;
21802
21803 fail:
21804 --deduction_depth;
21805 if (excessive_deduction_depth)
21806 {
21807 if (deduction_depth == 0)
21808 /* Reset once we're all the way out. */
21809 excessive_deduction_depth = false;
21810 }
21811
21812 return r;
21813 }
21814
21815 /* Adjust types before performing type deduction, as described in
21816 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21817 sections are symmetric. PARM is the type of a function parameter
21818 or the return type of the conversion function. ARG is the type of
21819 the argument passed to the call, or the type of the value
21820 initialized with the result of the conversion function.
21821 ARG_EXPR is the original argument expression, which may be null. */
21822
21823 static int
21824 maybe_adjust_types_for_deduction (unification_kind_t strict,
21825 tree* parm,
21826 tree* arg,
21827 tree arg_expr)
21828 {
21829 int result = 0;
21830
21831 switch (strict)
21832 {
21833 case DEDUCE_CALL:
21834 break;
21835
21836 case DEDUCE_CONV:
21837 /* Swap PARM and ARG throughout the remainder of this
21838 function; the handling is precisely symmetric since PARM
21839 will initialize ARG rather than vice versa. */
21840 std::swap (parm, arg);
21841 break;
21842
21843 case DEDUCE_EXACT:
21844 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21845 too, but here handle it by stripping the reference from PARM
21846 rather than by adding it to ARG. */
21847 if (TYPE_REF_P (*parm)
21848 && TYPE_REF_IS_RVALUE (*parm)
21849 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21850 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21851 && TYPE_REF_P (*arg)
21852 && !TYPE_REF_IS_RVALUE (*arg))
21853 *parm = TREE_TYPE (*parm);
21854 /* Nothing else to do in this case. */
21855 return 0;
21856
21857 default:
21858 gcc_unreachable ();
21859 }
21860
21861 if (!TYPE_REF_P (*parm))
21862 {
21863 /* [temp.deduct.call]
21864
21865 If P is not a reference type:
21866
21867 --If A is an array type, the pointer type produced by the
21868 array-to-pointer standard conversion (_conv.array_) is
21869 used in place of A for type deduction; otherwise,
21870
21871 --If A is a function type, the pointer type produced by
21872 the function-to-pointer standard conversion
21873 (_conv.func_) is used in place of A for type deduction;
21874 otherwise,
21875
21876 --If A is a cv-qualified type, the top level
21877 cv-qualifiers of A's type are ignored for type
21878 deduction. */
21879 if (TREE_CODE (*arg) == ARRAY_TYPE)
21880 *arg = build_pointer_type (TREE_TYPE (*arg));
21881 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21882 *arg = build_pointer_type (*arg);
21883 else
21884 *arg = TYPE_MAIN_VARIANT (*arg);
21885 }
21886
21887 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21888 reference to a cv-unqualified template parameter that does not represent a
21889 template parameter of a class template (during class template argument
21890 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21891 an lvalue, the type "lvalue reference to A" is used in place of A for type
21892 deduction. */
21893 if (TYPE_REF_P (*parm)
21894 && TYPE_REF_IS_RVALUE (*parm)
21895 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21896 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21897 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21898 && (arg_expr ? lvalue_p (arg_expr)
21899 /* try_one_overload doesn't provide an arg_expr, but
21900 functions are always lvalues. */
21901 : TREE_CODE (*arg) == FUNCTION_TYPE))
21902 *arg = build_reference_type (*arg);
21903
21904 /* [temp.deduct.call]
21905
21906 If P is a cv-qualified type, the top level cv-qualifiers
21907 of P's type are ignored for type deduction. If P is a
21908 reference type, the type referred to by P is used for
21909 type deduction. */
21910 *parm = TYPE_MAIN_VARIANT (*parm);
21911 if (TYPE_REF_P (*parm))
21912 {
21913 *parm = TREE_TYPE (*parm);
21914 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21915 }
21916
21917 /* DR 322. For conversion deduction, remove a reference type on parm
21918 too (which has been swapped into ARG). */
21919 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21920 *arg = TREE_TYPE (*arg);
21921
21922 return result;
21923 }
21924
21925 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21926 template which doesn't contain any deducible template parameters; check if
21927 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21928 unify_one_argument. */
21929
21930 static int
21931 check_non_deducible_conversion (tree parm, tree arg, int strict,
21932 int flags, struct conversion **conv_p,
21933 bool explain_p)
21934 {
21935 tree type;
21936
21937 if (!TYPE_P (arg))
21938 type = TREE_TYPE (arg);
21939 else
21940 type = arg;
21941
21942 if (same_type_p (parm, type))
21943 return unify_success (explain_p);
21944
21945 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21946 if (strict == DEDUCE_CONV)
21947 {
21948 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21949 return unify_success (explain_p);
21950 }
21951 else if (strict != DEDUCE_EXACT)
21952 {
21953 bool ok = false;
21954 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21955 if (conv_p)
21956 /* Avoid recalculating this in add_function_candidate. */
21957 ok = (*conv_p
21958 = good_conversion (parm, type, conv_arg, flags, complain));
21959 else
21960 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21961 if (ok)
21962 return unify_success (explain_p);
21963 }
21964
21965 if (strict == DEDUCE_EXACT)
21966 return unify_type_mismatch (explain_p, parm, arg);
21967 else
21968 return unify_arg_conversion (explain_p, parm, type, arg);
21969 }
21970
21971 static bool uses_deducible_template_parms (tree type);
21972
21973 /* Returns true iff the expression EXPR is one from which a template
21974 argument can be deduced. In other words, if it's an undecorated
21975 use of a template non-type parameter. */
21976
21977 static bool
21978 deducible_expression (tree expr)
21979 {
21980 /* Strip implicit conversions and implicit INDIRECT_REFs. */
21981 while (CONVERT_EXPR_P (expr)
21982 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
21983 || REFERENCE_REF_P (expr))
21984 expr = TREE_OPERAND (expr, 0);
21985 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21986 }
21987
21988 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21989 deducible way; that is, if it has a max value of <PARM> - 1. */
21990
21991 static bool
21992 deducible_array_bound (tree domain)
21993 {
21994 if (domain == NULL_TREE)
21995 return false;
21996
21997 tree max = TYPE_MAX_VALUE (domain);
21998 if (TREE_CODE (max) != MINUS_EXPR)
21999 return false;
22000
22001 return deducible_expression (TREE_OPERAND (max, 0));
22002 }
22003
22004 /* Returns true iff the template arguments ARGS use a template parameter
22005 in a deducible way. */
22006
22007 static bool
22008 deducible_template_args (tree args)
22009 {
22010 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
22011 {
22012 bool deducible;
22013 tree elt = TREE_VEC_ELT (args, i);
22014 if (ARGUMENT_PACK_P (elt))
22015 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
22016 else
22017 {
22018 if (PACK_EXPANSION_P (elt))
22019 elt = PACK_EXPANSION_PATTERN (elt);
22020 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
22021 deducible = true;
22022 else if (TYPE_P (elt))
22023 deducible = uses_deducible_template_parms (elt);
22024 else
22025 deducible = deducible_expression (elt);
22026 }
22027 if (deducible)
22028 return true;
22029 }
22030 return false;
22031 }
22032
22033 /* Returns true iff TYPE contains any deducible references to template
22034 parameters, as per 14.8.2.5. */
22035
22036 static bool
22037 uses_deducible_template_parms (tree type)
22038 {
22039 if (PACK_EXPANSION_P (type))
22040 type = PACK_EXPANSION_PATTERN (type);
22041
22042 /* T
22043 cv-list T
22044 TT<T>
22045 TT<i>
22046 TT<> */
22047 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22048 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22049 return true;
22050
22051 /* T*
22052 T&
22053 T&& */
22054 if (INDIRECT_TYPE_P (type))
22055 return uses_deducible_template_parms (TREE_TYPE (type));
22056
22057 /* T[integer-constant ]
22058 type [i] */
22059 if (TREE_CODE (type) == ARRAY_TYPE)
22060 return (uses_deducible_template_parms (TREE_TYPE (type))
22061 || deducible_array_bound (TYPE_DOMAIN (type)));
22062
22063 /* T type ::*
22064 type T::*
22065 T T::*
22066 T (type ::*)()
22067 type (T::*)()
22068 type (type ::*)(T)
22069 type (T::*)(T)
22070 T (type ::*)(T)
22071 T (T::*)()
22072 T (T::*)(T) */
22073 if (TYPE_PTRMEM_P (type))
22074 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
22075 || (uses_deducible_template_parms
22076 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
22077
22078 /* template-name <T> (where template-name refers to a class template)
22079 template-name <i> (where template-name refers to a class template) */
22080 if (CLASS_TYPE_P (type)
22081 && CLASSTYPE_TEMPLATE_INFO (type)
22082 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
22083 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22084 (CLASSTYPE_TI_ARGS (type)));
22085
22086 /* type (T)
22087 T()
22088 T(T) */
22089 if (FUNC_OR_METHOD_TYPE_P (type))
22090 {
22091 if (uses_deducible_template_parms (TREE_TYPE (type)))
22092 return true;
22093 tree parm = TYPE_ARG_TYPES (type);
22094 if (TREE_CODE (type) == METHOD_TYPE)
22095 parm = TREE_CHAIN (parm);
22096 for (; parm; parm = TREE_CHAIN (parm))
22097 if (uses_deducible_template_parms (TREE_VALUE (parm)))
22098 return true;
22099 }
22100
22101 return false;
22102 }
22103
22104 /* Subroutine of type_unification_real and unify_pack_expansion to
22105 handle unification of a single P/A pair. Parameters are as
22106 for those functions. */
22107
22108 static int
22109 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
22110 int subr, unification_kind_t strict,
22111 bool explain_p)
22112 {
22113 tree arg_expr = NULL_TREE;
22114 int arg_strict;
22115
22116 if (arg == error_mark_node || parm == error_mark_node)
22117 return unify_invalid (explain_p);
22118 if (arg == unknown_type_node)
22119 /* We can't deduce anything from this, but we might get all the
22120 template args from other function args. */
22121 return unify_success (explain_p);
22122
22123 /* Implicit conversions (Clause 4) will be performed on a function
22124 argument to convert it to the type of the corresponding function
22125 parameter if the parameter type contains no template-parameters that
22126 participate in template argument deduction. */
22127 if (strict != DEDUCE_EXACT
22128 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
22129 /* For function parameters with no deducible template parameters,
22130 just return. We'll check non-dependent conversions later. */
22131 return unify_success (explain_p);
22132
22133 switch (strict)
22134 {
22135 case DEDUCE_CALL:
22136 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
22137 | UNIFY_ALLOW_MORE_CV_QUAL
22138 | UNIFY_ALLOW_DERIVED);
22139 break;
22140
22141 case DEDUCE_CONV:
22142 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
22143 break;
22144
22145 case DEDUCE_EXACT:
22146 arg_strict = UNIFY_ALLOW_NONE;
22147 break;
22148
22149 default:
22150 gcc_unreachable ();
22151 }
22152
22153 /* We only do these transformations if this is the top-level
22154 parameter_type_list in a call or declaration matching; in other
22155 situations (nested function declarators, template argument lists) we
22156 won't be comparing a type to an expression, and we don't do any type
22157 adjustments. */
22158 if (!subr)
22159 {
22160 if (!TYPE_P (arg))
22161 {
22162 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
22163 if (type_unknown_p (arg))
22164 {
22165 /* [temp.deduct.type] A template-argument can be
22166 deduced from a pointer to function or pointer
22167 to member function argument if the set of
22168 overloaded functions does not contain function
22169 templates and at most one of a set of
22170 overloaded functions provides a unique
22171 match. */
22172 resolve_overloaded_unification (tparms, targs, parm,
22173 arg, strict,
22174 arg_strict, explain_p);
22175 /* If a unique match was not found, this is a
22176 non-deduced context, so we still succeed. */
22177 return unify_success (explain_p);
22178 }
22179
22180 arg_expr = arg;
22181 arg = unlowered_expr_type (arg);
22182 if (arg == error_mark_node)
22183 return unify_invalid (explain_p);
22184 }
22185
22186 arg_strict |=
22187 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
22188 }
22189 else
22190 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
22191 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
22192 return unify_template_argument_mismatch (explain_p, parm, arg);
22193
22194 /* For deduction from an init-list we need the actual list. */
22195 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
22196 arg = arg_expr;
22197 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
22198 }
22199
22200 /* for_each_template_parm callback that always returns 0. */
22201
22202 static int
22203 zero_r (tree, void *)
22204 {
22205 return 0;
22206 }
22207
22208 /* for_each_template_parm any_fn callback to handle deduction of a template
22209 type argument from the type of an array bound. */
22210
22211 static int
22212 array_deduction_r (tree t, void *data)
22213 {
22214 tree_pair_p d = (tree_pair_p)data;
22215 tree &tparms = d->purpose;
22216 tree &targs = d->value;
22217
22218 if (TREE_CODE (t) == ARRAY_TYPE)
22219 if (tree dom = TYPE_DOMAIN (t))
22220 if (tree max = TYPE_MAX_VALUE (dom))
22221 {
22222 if (TREE_CODE (max) == MINUS_EXPR)
22223 max = TREE_OPERAND (max, 0);
22224 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
22225 unify (tparms, targs, TREE_TYPE (max), size_type_node,
22226 UNIFY_ALLOW_NONE, /*explain*/false);
22227 }
22228
22229 /* Keep walking. */
22230 return 0;
22231 }
22232
22233 /* Try to deduce any not-yet-deduced template type arguments from the type of
22234 an array bound. This is handled separately from unify because 14.8.2.5 says
22235 "The type of a type parameter is only deduced from an array bound if it is
22236 not otherwise deduced." */
22237
22238 static void
22239 try_array_deduction (tree tparms, tree targs, tree parm)
22240 {
22241 tree_pair_s data = { tparms, targs };
22242 hash_set<tree> visited;
22243 for_each_template_parm (parm, zero_r, &data, &visited,
22244 /*nondeduced*/false, array_deduction_r);
22245 }
22246
22247 /* Most parms like fn_type_unification.
22248
22249 If SUBR is 1, we're being called recursively (to unify the
22250 arguments of a function or method parameter of a function
22251 template).
22252
22253 CHECKS is a pointer to a vector of access checks encountered while
22254 substituting default template arguments. */
22255
22256 static int
22257 type_unification_real (tree tparms,
22258 tree full_targs,
22259 tree xparms,
22260 const tree *xargs,
22261 unsigned int xnargs,
22262 int subr,
22263 unification_kind_t strict,
22264 vec<deferred_access_check, va_gc> **checks,
22265 bool explain_p)
22266 {
22267 tree parm, arg;
22268 int i;
22269 int ntparms = TREE_VEC_LENGTH (tparms);
22270 int saw_undeduced = 0;
22271 tree parms;
22272 const tree *args;
22273 unsigned int nargs;
22274 unsigned int ia;
22275
22276 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22277 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22278 gcc_assert (ntparms > 0);
22279
22280 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22281
22282 /* Reset the number of non-defaulted template arguments contained
22283 in TARGS. */
22284 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22285
22286 again:
22287 parms = xparms;
22288 args = xargs;
22289 nargs = xnargs;
22290
22291 ia = 0;
22292 while (parms && parms != void_list_node
22293 && ia < nargs)
22294 {
22295 parm = TREE_VALUE (parms);
22296
22297 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22298 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22299 /* For a function parameter pack that occurs at the end of the
22300 parameter-declaration-list, the type A of each remaining
22301 argument of the call is compared with the type P of the
22302 declarator-id of the function parameter pack. */
22303 break;
22304
22305 parms = TREE_CHAIN (parms);
22306
22307 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22308 /* For a function parameter pack that does not occur at the
22309 end of the parameter-declaration-list, the type of the
22310 parameter pack is a non-deduced context. */
22311 continue;
22312
22313 arg = args[ia];
22314 ++ia;
22315
22316 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22317 explain_p))
22318 return 1;
22319 }
22320
22321 if (parms
22322 && parms != void_list_node
22323 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22324 {
22325 /* Unify the remaining arguments with the pack expansion type. */
22326 tree argvec;
22327 tree parmvec = make_tree_vec (1);
22328
22329 /* Allocate a TREE_VEC and copy in all of the arguments */
22330 argvec = make_tree_vec (nargs - ia);
22331 for (i = 0; ia < nargs; ++ia, ++i)
22332 TREE_VEC_ELT (argvec, i) = args[ia];
22333
22334 /* Copy the parameter into parmvec. */
22335 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22336 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22337 /*subr=*/subr, explain_p))
22338 return 1;
22339
22340 /* Advance to the end of the list of parameters. */
22341 parms = TREE_CHAIN (parms);
22342 }
22343
22344 /* Fail if we've reached the end of the parm list, and more args
22345 are present, and the parm list isn't variadic. */
22346 if (ia < nargs && parms == void_list_node)
22347 return unify_too_many_arguments (explain_p, nargs, ia);
22348 /* Fail if parms are left and they don't have default values and
22349 they aren't all deduced as empty packs (c++/57397). This is
22350 consistent with sufficient_parms_p. */
22351 if (parms && parms != void_list_node
22352 && TREE_PURPOSE (parms) == NULL_TREE)
22353 {
22354 unsigned int count = nargs;
22355 tree p = parms;
22356 bool type_pack_p;
22357 do
22358 {
22359 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22360 if (!type_pack_p)
22361 count++;
22362 p = TREE_CHAIN (p);
22363 }
22364 while (p && p != void_list_node);
22365 if (count != nargs)
22366 return unify_too_few_arguments (explain_p, ia, count,
22367 type_pack_p);
22368 }
22369
22370 if (!subr)
22371 {
22372 tsubst_flags_t complain = (explain_p
22373 ? tf_warning_or_error
22374 : tf_none);
22375 bool tried_array_deduction = (cxx_dialect < cxx17);
22376
22377 for (i = 0; i < ntparms; i++)
22378 {
22379 tree targ = TREE_VEC_ELT (targs, i);
22380 tree tparm = TREE_VEC_ELT (tparms, i);
22381
22382 /* Clear the "incomplete" flags on all argument packs now so that
22383 substituting them into later default arguments works. */
22384 if (targ && ARGUMENT_PACK_P (targ))
22385 {
22386 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22387 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22388 }
22389
22390 if (targ || tparm == error_mark_node)
22391 continue;
22392 tparm = TREE_VALUE (tparm);
22393
22394 if (TREE_CODE (tparm) == TYPE_DECL
22395 && !tried_array_deduction)
22396 {
22397 try_array_deduction (tparms, targs, xparms);
22398 tried_array_deduction = true;
22399 if (TREE_VEC_ELT (targs, i))
22400 continue;
22401 }
22402
22403 /* If this is an undeduced nontype parameter that depends on
22404 a type parameter, try another pass; its type may have been
22405 deduced from a later argument than the one from which
22406 this parameter can be deduced. */
22407 if (TREE_CODE (tparm) == PARM_DECL
22408 && uses_template_parms (TREE_TYPE (tparm))
22409 && saw_undeduced < 2)
22410 {
22411 saw_undeduced = 1;
22412 continue;
22413 }
22414
22415 /* Core issue #226 (C++0x) [temp.deduct]:
22416
22417 If a template argument has not been deduced, its
22418 default template argument, if any, is used.
22419
22420 When we are in C++98 mode, TREE_PURPOSE will either
22421 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22422 to explicitly check cxx_dialect here. */
22423 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22424 /* OK, there is a default argument. Wait until after the
22425 conversion check to do substitution. */
22426 continue;
22427
22428 /* If the type parameter is a parameter pack, then it will
22429 be deduced to an empty parameter pack. */
22430 if (template_parameter_pack_p (tparm))
22431 {
22432 tree arg;
22433
22434 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22435 {
22436 arg = make_node (NONTYPE_ARGUMENT_PACK);
22437 TREE_CONSTANT (arg) = 1;
22438 }
22439 else
22440 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22441
22442 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22443
22444 TREE_VEC_ELT (targs, i) = arg;
22445 continue;
22446 }
22447
22448 return unify_parameter_deduction_failure (explain_p, tparm);
22449 }
22450
22451 /* Now substitute into the default template arguments. */
22452 for (i = 0; i < ntparms; i++)
22453 {
22454 tree targ = TREE_VEC_ELT (targs, i);
22455 tree tparm = TREE_VEC_ELT (tparms, i);
22456
22457 if (targ || tparm == error_mark_node)
22458 continue;
22459 tree parm = TREE_VALUE (tparm);
22460 tree arg = TREE_PURPOSE (tparm);
22461 reopen_deferring_access_checks (*checks);
22462 location_t save_loc = input_location;
22463 if (DECL_P (parm))
22464 input_location = DECL_SOURCE_LOCATION (parm);
22465
22466 if (saw_undeduced == 1
22467 && TREE_CODE (parm) == PARM_DECL
22468 && uses_template_parms (TREE_TYPE (parm)))
22469 {
22470 /* The type of this non-type parameter depends on undeduced
22471 parameters. Don't try to use its default argument yet,
22472 since we might deduce an argument for it on the next pass,
22473 but do check whether the arguments we already have cause
22474 substitution failure, so that that happens before we try
22475 later default arguments (78489). */
22476 ++processing_template_decl;
22477 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22478 NULL_TREE);
22479 --processing_template_decl;
22480 if (type == error_mark_node)
22481 arg = error_mark_node;
22482 else
22483 arg = NULL_TREE;
22484 }
22485 else
22486 {
22487 /* Even if the call is happening in template context, getting
22488 here means it's non-dependent, and a default argument is
22489 considered a separate definition under [temp.decls], so we can
22490 do this substitution without processing_template_decl. This
22491 is important if the default argument contains something that
22492 might be instantiation-dependent like access (87480). */
22493 processing_template_decl_sentinel s;
22494 tree substed = NULL_TREE;
22495 if (saw_undeduced == 1)
22496 {
22497 /* First instatiate in template context, in case we still
22498 depend on undeduced template parameters. */
22499 ++processing_template_decl;
22500 substed = tsubst_template_arg (arg, full_targs, complain,
22501 NULL_TREE);
22502 --processing_template_decl;
22503 if (substed != error_mark_node
22504 && !uses_template_parms (substed))
22505 /* We replaced all the tparms, substitute again out of
22506 template context. */
22507 substed = NULL_TREE;
22508 }
22509 if (!substed)
22510 substed = tsubst_template_arg (arg, full_targs, complain,
22511 NULL_TREE);
22512
22513 if (!uses_template_parms (substed))
22514 arg = convert_template_argument (parm, substed, full_targs,
22515 complain, i, NULL_TREE);
22516 else if (saw_undeduced == 1)
22517 arg = NULL_TREE;
22518 else
22519 arg = error_mark_node;
22520 }
22521
22522 input_location = save_loc;
22523 *checks = get_deferred_access_checks ();
22524 pop_deferring_access_checks ();
22525
22526 if (arg == error_mark_node)
22527 return 1;
22528 else if (arg)
22529 {
22530 TREE_VEC_ELT (targs, i) = arg;
22531 /* The position of the first default template argument,
22532 is also the number of non-defaulted arguments in TARGS.
22533 Record that. */
22534 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22535 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22536 }
22537 }
22538
22539 if (saw_undeduced++ == 1)
22540 goto again;
22541 }
22542
22543 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22544 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22545
22546 return unify_success (explain_p);
22547 }
22548
22549 /* Subroutine of type_unification_real. Args are like the variables
22550 at the call site. ARG is an overloaded function (or template-id);
22551 we try deducing template args from each of the overloads, and if
22552 only one succeeds, we go with that. Modifies TARGS and returns
22553 true on success. */
22554
22555 static bool
22556 resolve_overloaded_unification (tree tparms,
22557 tree targs,
22558 tree parm,
22559 tree arg,
22560 unification_kind_t strict,
22561 int sub_strict,
22562 bool explain_p)
22563 {
22564 tree tempargs = copy_node (targs);
22565 int good = 0;
22566 tree goodfn = NULL_TREE;
22567 bool addr_p;
22568
22569 if (TREE_CODE (arg) == ADDR_EXPR)
22570 {
22571 arg = TREE_OPERAND (arg, 0);
22572 addr_p = true;
22573 }
22574 else
22575 addr_p = false;
22576
22577 if (TREE_CODE (arg) == COMPONENT_REF)
22578 /* Handle `&x' where `x' is some static or non-static member
22579 function name. */
22580 arg = TREE_OPERAND (arg, 1);
22581
22582 if (TREE_CODE (arg) == OFFSET_REF)
22583 arg = TREE_OPERAND (arg, 1);
22584
22585 /* Strip baselink information. */
22586 if (BASELINK_P (arg))
22587 arg = BASELINK_FUNCTIONS (arg);
22588
22589 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22590 {
22591 /* If we got some explicit template args, we need to plug them into
22592 the affected templates before we try to unify, in case the
22593 explicit args will completely resolve the templates in question. */
22594
22595 int ok = 0;
22596 tree expl_subargs = TREE_OPERAND (arg, 1);
22597 arg = TREE_OPERAND (arg, 0);
22598
22599 for (lkp_iterator iter (arg); iter; ++iter)
22600 {
22601 tree fn = *iter;
22602 tree subargs, elem;
22603
22604 if (TREE_CODE (fn) != TEMPLATE_DECL)
22605 continue;
22606
22607 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22608 expl_subargs, NULL_TREE, tf_none,
22609 /*require_all_args=*/true,
22610 /*use_default_args=*/true);
22611 if (subargs != error_mark_node
22612 && !any_dependent_template_arguments_p (subargs))
22613 {
22614 fn = instantiate_template (fn, subargs, tf_none);
22615 if (!constraints_satisfied_p (fn))
22616 continue;
22617 if (undeduced_auto_decl (fn))
22618 {
22619 /* Instantiate the function to deduce its return type. */
22620 ++function_depth;
22621 instantiate_decl (fn, /*defer*/false, /*class*/false);
22622 --function_depth;
22623 }
22624
22625 if (flag_noexcept_type)
22626 maybe_instantiate_noexcept (fn, tf_none);
22627
22628 elem = TREE_TYPE (fn);
22629 if (try_one_overload (tparms, targs, tempargs, parm,
22630 elem, strict, sub_strict, addr_p, explain_p)
22631 && (!goodfn || !same_type_p (goodfn, elem)))
22632 {
22633 goodfn = elem;
22634 ++good;
22635 }
22636 }
22637 else if (subargs)
22638 ++ok;
22639 }
22640 /* If no templates (or more than one) are fully resolved by the
22641 explicit arguments, this template-id is a non-deduced context; it
22642 could still be OK if we deduce all template arguments for the
22643 enclosing call through other arguments. */
22644 if (good != 1)
22645 good = ok;
22646 }
22647 else if (!OVL_P (arg))
22648 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22649 -- but the deduction does not succeed because the expression is
22650 not just the function on its own. */
22651 return false;
22652 else
22653 for (lkp_iterator iter (arg); iter; ++iter)
22654 {
22655 tree fn = *iter;
22656 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22657 strict, sub_strict, addr_p, explain_p)
22658 && (!goodfn || !decls_match (goodfn, fn)))
22659 {
22660 goodfn = fn;
22661 ++good;
22662 }
22663 }
22664
22665 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22666 to function or pointer to member function argument if the set of
22667 overloaded functions does not contain function templates and at most
22668 one of a set of overloaded functions provides a unique match.
22669
22670 So if we found multiple possibilities, we return success but don't
22671 deduce anything. */
22672
22673 if (good == 1)
22674 {
22675 int i = TREE_VEC_LENGTH (targs);
22676 for (; i--; )
22677 if (TREE_VEC_ELT (tempargs, i))
22678 {
22679 tree old = TREE_VEC_ELT (targs, i);
22680 tree new_ = TREE_VEC_ELT (tempargs, i);
22681 if (new_ && old && ARGUMENT_PACK_P (old)
22682 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22683 /* Don't forget explicit template arguments in a pack. */
22684 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22685 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22686 TREE_VEC_ELT (targs, i) = new_;
22687 }
22688 }
22689 if (good)
22690 return true;
22691
22692 return false;
22693 }
22694
22695 /* Core DR 115: In contexts where deduction is done and fails, or in
22696 contexts where deduction is not done, if a template argument list is
22697 specified and it, along with any default template arguments, identifies
22698 a single function template specialization, then the template-id is an
22699 lvalue for the function template specialization. */
22700
22701 tree
22702 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22703 {
22704 tree expr, offset, baselink;
22705 bool addr;
22706
22707 if (!type_unknown_p (orig_expr))
22708 return orig_expr;
22709
22710 expr = orig_expr;
22711 addr = false;
22712 offset = NULL_TREE;
22713 baselink = NULL_TREE;
22714
22715 if (TREE_CODE (expr) == ADDR_EXPR)
22716 {
22717 expr = TREE_OPERAND (expr, 0);
22718 addr = true;
22719 }
22720 if (TREE_CODE (expr) == OFFSET_REF)
22721 {
22722 offset = expr;
22723 expr = TREE_OPERAND (expr, 1);
22724 }
22725 if (BASELINK_P (expr))
22726 {
22727 baselink = expr;
22728 expr = BASELINK_FUNCTIONS (expr);
22729 }
22730
22731 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22732 {
22733 int good = 0;
22734 tree goodfn = NULL_TREE;
22735
22736 /* If we got some explicit template args, we need to plug them into
22737 the affected templates before we try to unify, in case the
22738 explicit args will completely resolve the templates in question. */
22739
22740 tree expl_subargs = TREE_OPERAND (expr, 1);
22741 tree arg = TREE_OPERAND (expr, 0);
22742 tree badfn = NULL_TREE;
22743 tree badargs = NULL_TREE;
22744
22745 for (lkp_iterator iter (arg); iter; ++iter)
22746 {
22747 tree fn = *iter;
22748 tree subargs, elem;
22749
22750 if (TREE_CODE (fn) != TEMPLATE_DECL)
22751 continue;
22752
22753 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22754 expl_subargs, NULL_TREE, tf_none,
22755 /*require_all_args=*/true,
22756 /*use_default_args=*/true);
22757 if (subargs != error_mark_node
22758 && !any_dependent_template_arguments_p (subargs))
22759 {
22760 elem = instantiate_template (fn, subargs, tf_none);
22761 if (elem == error_mark_node)
22762 {
22763 badfn = fn;
22764 badargs = subargs;
22765 }
22766 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22767 && constraints_satisfied_p (elem))
22768 {
22769 goodfn = elem;
22770 ++good;
22771 }
22772 }
22773 }
22774 if (good == 1)
22775 {
22776 mark_used (goodfn);
22777 expr = goodfn;
22778 if (baselink)
22779 expr = build_baselink (BASELINK_BINFO (baselink),
22780 BASELINK_ACCESS_BINFO (baselink),
22781 expr, BASELINK_OPTYPE (baselink));
22782 if (offset)
22783 {
22784 tree base
22785 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22786 expr = build_offset_ref (base, expr, addr, complain);
22787 }
22788 if (addr)
22789 expr = cp_build_addr_expr (expr, complain);
22790 return expr;
22791 }
22792 else if (good == 0 && badargs && (complain & tf_error))
22793 /* There were no good options and at least one bad one, so let the
22794 user know what the problem is. */
22795 instantiate_template (badfn, badargs, complain);
22796 }
22797 return orig_expr;
22798 }
22799
22800 /* As above, but error out if the expression remains overloaded. */
22801
22802 tree
22803 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22804 {
22805 exp = resolve_nondeduced_context (exp, complain);
22806 if (type_unknown_p (exp))
22807 {
22808 if (complain & tf_error)
22809 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22810 return error_mark_node;
22811 }
22812 return exp;
22813 }
22814
22815 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22816 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22817 different overloads deduce different arguments for a given parm.
22818 ADDR_P is true if the expression for which deduction is being
22819 performed was of the form "& fn" rather than simply "fn".
22820
22821 Returns 1 on success. */
22822
22823 static int
22824 try_one_overload (tree tparms,
22825 tree orig_targs,
22826 tree targs,
22827 tree parm,
22828 tree arg,
22829 unification_kind_t strict,
22830 int sub_strict,
22831 bool addr_p,
22832 bool explain_p)
22833 {
22834 int nargs;
22835 tree tempargs;
22836 int i;
22837
22838 if (arg == error_mark_node)
22839 return 0;
22840
22841 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22842 to function or pointer to member function argument if the set of
22843 overloaded functions does not contain function templates and at most
22844 one of a set of overloaded functions provides a unique match.
22845
22846 So if this is a template, just return success. */
22847
22848 if (uses_template_parms (arg))
22849 return 1;
22850
22851 if (TREE_CODE (arg) == METHOD_TYPE)
22852 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22853 else if (addr_p)
22854 arg = build_pointer_type (arg);
22855
22856 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22857
22858 /* We don't copy orig_targs for this because if we have already deduced
22859 some template args from previous args, unify would complain when we
22860 try to deduce a template parameter for the same argument, even though
22861 there isn't really a conflict. */
22862 nargs = TREE_VEC_LENGTH (targs);
22863 tempargs = make_tree_vec (nargs);
22864
22865 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22866 return 0;
22867
22868 /* First make sure we didn't deduce anything that conflicts with
22869 explicitly specified args. */
22870 for (i = nargs; i--; )
22871 {
22872 tree elt = TREE_VEC_ELT (tempargs, i);
22873 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22874
22875 if (!elt)
22876 /*NOP*/;
22877 else if (uses_template_parms (elt))
22878 /* Since we're unifying against ourselves, we will fill in
22879 template args used in the function parm list with our own
22880 template parms. Discard them. */
22881 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22882 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22883 {
22884 /* Check that the argument at each index of the deduced argument pack
22885 is equivalent to the corresponding explicitly specified argument.
22886 We may have deduced more arguments than were explicitly specified,
22887 and that's OK. */
22888
22889 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22890 that's wrong if we deduce the same argument pack from multiple
22891 function arguments: it's only incomplete the first time. */
22892
22893 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22894 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22895
22896 if (TREE_VEC_LENGTH (deduced_pack)
22897 < TREE_VEC_LENGTH (explicit_pack))
22898 return 0;
22899
22900 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22901 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22902 TREE_VEC_ELT (deduced_pack, j)))
22903 return 0;
22904 }
22905 else if (oldelt && !template_args_equal (oldelt, elt))
22906 return 0;
22907 }
22908
22909 for (i = nargs; i--; )
22910 {
22911 tree elt = TREE_VEC_ELT (tempargs, i);
22912
22913 if (elt)
22914 TREE_VEC_ELT (targs, i) = elt;
22915 }
22916
22917 return 1;
22918 }
22919
22920 /* PARM is a template class (perhaps with unbound template
22921 parameters). ARG is a fully instantiated type. If ARG can be
22922 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22923 TARGS are as for unify. */
22924
22925 static tree
22926 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22927 bool explain_p)
22928 {
22929 tree copy_of_targs;
22930
22931 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22932 return NULL_TREE;
22933 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22934 /* Matches anything. */;
22935 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22936 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22937 return NULL_TREE;
22938
22939 /* We need to make a new template argument vector for the call to
22940 unify. If we used TARGS, we'd clutter it up with the result of
22941 the attempted unification, even if this class didn't work out.
22942 We also don't want to commit ourselves to all the unifications
22943 we've already done, since unification is supposed to be done on
22944 an argument-by-argument basis. In other words, consider the
22945 following pathological case:
22946
22947 template <int I, int J, int K>
22948 struct S {};
22949
22950 template <int I, int J>
22951 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22952
22953 template <int I, int J, int K>
22954 void f(S<I, J, K>, S<I, I, I>);
22955
22956 void g() {
22957 S<0, 0, 0> s0;
22958 S<0, 1, 2> s2;
22959
22960 f(s0, s2);
22961 }
22962
22963 Now, by the time we consider the unification involving `s2', we
22964 already know that we must have `f<0, 0, 0>'. But, even though
22965 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22966 because there are two ways to unify base classes of S<0, 1, 2>
22967 with S<I, I, I>. If we kept the already deduced knowledge, we
22968 would reject the possibility I=1. */
22969 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22970
22971 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22972 {
22973 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22974 return NULL_TREE;
22975 return arg;
22976 }
22977
22978 /* If unification failed, we're done. */
22979 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22980 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22981 return NULL_TREE;
22982
22983 return arg;
22984 }
22985
22986 /* Given a template type PARM and a class type ARG, find the unique
22987 base type in ARG that is an instance of PARM. We do not examine
22988 ARG itself; only its base-classes. If there is not exactly one
22989 appropriate base class, return NULL_TREE. PARM may be the type of
22990 a partial specialization, as well as a plain template type. Used
22991 by unify. */
22992
22993 static enum template_base_result
22994 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22995 bool explain_p, tree *result)
22996 {
22997 tree rval = NULL_TREE;
22998 tree binfo;
22999
23000 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
23001
23002 binfo = TYPE_BINFO (complete_type (arg));
23003 if (!binfo)
23004 {
23005 /* The type could not be completed. */
23006 *result = NULL_TREE;
23007 return tbr_incomplete_type;
23008 }
23009
23010 /* Walk in inheritance graph order. The search order is not
23011 important, and this avoids multiple walks of virtual bases. */
23012 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
23013 {
23014 tree r = try_class_unification (tparms, targs, parm,
23015 BINFO_TYPE (binfo), explain_p);
23016
23017 if (r)
23018 {
23019 /* If there is more than one satisfactory baseclass, then:
23020
23021 [temp.deduct.call]
23022
23023 If they yield more than one possible deduced A, the type
23024 deduction fails.
23025
23026 applies. */
23027 if (rval && !same_type_p (r, rval))
23028 {
23029 /* [temp.deduct.call]/4.3: If there is a class C that is a
23030 (direct or indirect) base class of D and derived (directly or
23031 indirectly) from a class B and that would be a valid deduced
23032 A, the deduced A cannot be B or pointer to B, respectively. */
23033 if (DERIVED_FROM_P (r, rval))
23034 /* Ignore r. */
23035 continue;
23036 else if (DERIVED_FROM_P (rval, r))
23037 /* Ignore rval. */;
23038 else
23039 {
23040 *result = NULL_TREE;
23041 return tbr_ambiguous_baseclass;
23042 }
23043 }
23044
23045 rval = r;
23046 }
23047 }
23048
23049 *result = rval;
23050 return tbr_success;
23051 }
23052
23053 /* Returns the level of DECL, which declares a template parameter. */
23054
23055 static int
23056 template_decl_level (tree decl)
23057 {
23058 switch (TREE_CODE (decl))
23059 {
23060 case TYPE_DECL:
23061 case TEMPLATE_DECL:
23062 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
23063
23064 case PARM_DECL:
23065 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
23066
23067 default:
23068 gcc_unreachable ();
23069 }
23070 return 0;
23071 }
23072
23073 /* Decide whether ARG can be unified with PARM, considering only the
23074 cv-qualifiers of each type, given STRICT as documented for unify.
23075 Returns nonzero iff the unification is OK on that basis. */
23076
23077 static int
23078 check_cv_quals_for_unify (int strict, tree arg, tree parm)
23079 {
23080 int arg_quals = cp_type_quals (arg);
23081 int parm_quals = cp_type_quals (parm);
23082
23083 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23084 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23085 {
23086 /* Although a CVR qualifier is ignored when being applied to a
23087 substituted template parameter ([8.3.2]/1 for example), that
23088 does not allow us to unify "const T" with "int&" because both
23089 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23090 It is ok when we're allowing additional CV qualifiers
23091 at the outer level [14.8.2.1]/3,1st bullet. */
23092 if ((TYPE_REF_P (arg)
23093 || FUNC_OR_METHOD_TYPE_P (arg))
23094 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
23095 return 0;
23096
23097 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
23098 && (parm_quals & TYPE_QUAL_RESTRICT))
23099 return 0;
23100 }
23101
23102 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23103 && (arg_quals & parm_quals) != parm_quals)
23104 return 0;
23105
23106 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
23107 && (parm_quals & arg_quals) != arg_quals)
23108 return 0;
23109
23110 return 1;
23111 }
23112
23113 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23114 void
23115 template_parm_level_and_index (tree parm, int* level, int* index)
23116 {
23117 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23118 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23119 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23120 {
23121 *index = TEMPLATE_TYPE_IDX (parm);
23122 *level = TEMPLATE_TYPE_LEVEL (parm);
23123 }
23124 else
23125 {
23126 *index = TEMPLATE_PARM_IDX (parm);
23127 *level = TEMPLATE_PARM_LEVEL (parm);
23128 }
23129 }
23130
23131 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23132 do { \
23133 if (unify (TP, TA, P, A, S, EP)) \
23134 return 1; \
23135 } while (0)
23136
23137 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23138 expansion at the end of PACKED_PARMS. Returns 0 if the type
23139 deduction succeeds, 1 otherwise. STRICT is the same as in
23140 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23141 function call argument list. We'll need to adjust the arguments to make them
23142 types. SUBR tells us if this is from a recursive call to
23143 type_unification_real, or for comparing two template argument
23144 lists. */
23145
23146 static int
23147 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
23148 tree packed_args, unification_kind_t strict,
23149 bool subr, bool explain_p)
23150 {
23151 tree parm
23152 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
23153 tree pattern = PACK_EXPANSION_PATTERN (parm);
23154 tree pack, packs = NULL_TREE;
23155 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
23156
23157 /* Add in any args remembered from an earlier partial instantiation. */
23158 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
23159 int levels = TMPL_ARGS_DEPTH (targs);
23160
23161 packed_args = expand_template_argument_pack (packed_args);
23162
23163 int len = TREE_VEC_LENGTH (packed_args);
23164
23165 /* Determine the parameter packs we will be deducing from the
23166 pattern, and record their current deductions. */
23167 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
23168 pack; pack = TREE_CHAIN (pack))
23169 {
23170 tree parm_pack = TREE_VALUE (pack);
23171 int idx, level;
23172
23173 /* Only template parameter packs can be deduced, not e.g. function
23174 parameter packs or __bases or __integer_pack. */
23175 if (!TEMPLATE_PARM_P (parm_pack))
23176 continue;
23177
23178 /* Determine the index and level of this parameter pack. */
23179 template_parm_level_and_index (parm_pack, &level, &idx);
23180 if (level < levels)
23181 continue;
23182
23183 /* Keep track of the parameter packs and their corresponding
23184 argument packs. */
23185 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
23186 TREE_TYPE (packs) = make_tree_vec (len - start);
23187 }
23188
23189 /* Loop through all of the arguments that have not yet been
23190 unified and unify each with the pattern. */
23191 for (i = start; i < len; i++)
23192 {
23193 tree parm;
23194 bool any_explicit = false;
23195 tree arg = TREE_VEC_ELT (packed_args, i);
23196
23197 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23198 or the element of its argument pack at the current index if
23199 this argument was explicitly specified. */
23200 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23201 {
23202 int idx, level;
23203 tree arg, pargs;
23204 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23205
23206 arg = NULL_TREE;
23207 if (TREE_VALUE (pack)
23208 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
23209 && (i - start < TREE_VEC_LENGTH (pargs)))
23210 {
23211 any_explicit = true;
23212 arg = TREE_VEC_ELT (pargs, i - start);
23213 }
23214 TMPL_ARG (targs, level, idx) = arg;
23215 }
23216
23217 /* If we had explicit template arguments, substitute them into the
23218 pattern before deduction. */
23219 if (any_explicit)
23220 {
23221 /* Some arguments might still be unspecified or dependent. */
23222 bool dependent;
23223 ++processing_template_decl;
23224 dependent = any_dependent_template_arguments_p (targs);
23225 if (!dependent)
23226 --processing_template_decl;
23227 parm = tsubst (pattern, targs,
23228 explain_p ? tf_warning_or_error : tf_none,
23229 NULL_TREE);
23230 if (dependent)
23231 --processing_template_decl;
23232 if (parm == error_mark_node)
23233 return 1;
23234 }
23235 else
23236 parm = pattern;
23237
23238 /* Unify the pattern with the current argument. */
23239 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23240 explain_p))
23241 return 1;
23242
23243 /* For each parameter pack, collect the deduced value. */
23244 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23245 {
23246 int idx, level;
23247 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23248
23249 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23250 TMPL_ARG (targs, level, idx);
23251 }
23252 }
23253
23254 /* Verify that the results of unification with the parameter packs
23255 produce results consistent with what we've seen before, and make
23256 the deduced argument packs available. */
23257 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23258 {
23259 tree old_pack = TREE_VALUE (pack);
23260 tree new_args = TREE_TYPE (pack);
23261 int i, len = TREE_VEC_LENGTH (new_args);
23262 int idx, level;
23263 bool nondeduced_p = false;
23264
23265 /* By default keep the original deduced argument pack.
23266 If necessary, more specific code is going to update the
23267 resulting deduced argument later down in this function. */
23268 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23269 TMPL_ARG (targs, level, idx) = old_pack;
23270
23271 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23272 actually deduce anything. */
23273 for (i = 0; i < len && !nondeduced_p; ++i)
23274 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23275 nondeduced_p = true;
23276 if (nondeduced_p)
23277 continue;
23278
23279 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23280 {
23281 /* If we had fewer function args than explicit template args,
23282 just use the explicits. */
23283 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23284 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23285 if (len < explicit_len)
23286 new_args = explicit_args;
23287 }
23288
23289 if (!old_pack)
23290 {
23291 tree result;
23292 /* Build the deduced *_ARGUMENT_PACK. */
23293 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23294 {
23295 result = make_node (NONTYPE_ARGUMENT_PACK);
23296 TREE_CONSTANT (result) = 1;
23297 }
23298 else
23299 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23300
23301 SET_ARGUMENT_PACK_ARGS (result, new_args);
23302
23303 /* Note the deduced argument packs for this parameter
23304 pack. */
23305 TMPL_ARG (targs, level, idx) = result;
23306 }
23307 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23308 && (ARGUMENT_PACK_ARGS (old_pack)
23309 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23310 {
23311 /* We only had the explicitly-provided arguments before, but
23312 now we have a complete set of arguments. */
23313 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23314
23315 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23316 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23317 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23318 }
23319 else
23320 {
23321 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23322 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23323 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23324 /* During template argument deduction for the aggregate deduction
23325 candidate, the number of elements in a trailing parameter pack
23326 is only deduced from the number of remaining function
23327 arguments if it is not otherwise deduced. */
23328 if (cxx_dialect >= cxx20
23329 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23330 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23331 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23332 if (!comp_template_args (old_args, new_args,
23333 &bad_old_arg, &bad_new_arg))
23334 /* Inconsistent unification of this parameter pack. */
23335 return unify_parameter_pack_inconsistent (explain_p,
23336 bad_old_arg,
23337 bad_new_arg);
23338 }
23339 }
23340
23341 return unify_success (explain_p);
23342 }
23343
23344 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23345 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23346 parameters and return value are as for unify. */
23347
23348 static int
23349 unify_array_domain (tree tparms, tree targs,
23350 tree parm_dom, tree arg_dom,
23351 bool explain_p)
23352 {
23353 tree parm_max;
23354 tree arg_max;
23355 bool parm_cst;
23356 bool arg_cst;
23357
23358 /* Our representation of array types uses "N - 1" as the
23359 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23360 not an integer constant. We cannot unify arbitrarily
23361 complex expressions, so we eliminate the MINUS_EXPRs
23362 here. */
23363 parm_max = TYPE_MAX_VALUE (parm_dom);
23364 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23365 if (!parm_cst)
23366 {
23367 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23368 parm_max = TREE_OPERAND (parm_max, 0);
23369 }
23370 arg_max = TYPE_MAX_VALUE (arg_dom);
23371 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23372 if (!arg_cst)
23373 {
23374 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23375 trying to unify the type of a variable with the type
23376 of a template parameter. For example:
23377
23378 template <unsigned int N>
23379 void f (char (&) [N]);
23380 int g();
23381 void h(int i) {
23382 char a[g(i)];
23383 f(a);
23384 }
23385
23386 Here, the type of the ARG will be "int [g(i)]", and
23387 may be a SAVE_EXPR, etc. */
23388 if (TREE_CODE (arg_max) != MINUS_EXPR)
23389 return unify_vla_arg (explain_p, arg_dom);
23390 arg_max = TREE_OPERAND (arg_max, 0);
23391 }
23392
23393 /* If only one of the bounds used a MINUS_EXPR, compensate
23394 by adding one to the other bound. */
23395 if (parm_cst && !arg_cst)
23396 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23397 integer_type_node,
23398 parm_max,
23399 integer_one_node);
23400 else if (arg_cst && !parm_cst)
23401 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23402 integer_type_node,
23403 arg_max,
23404 integer_one_node);
23405
23406 return unify (tparms, targs, parm_max, arg_max,
23407 UNIFY_ALLOW_INTEGER, explain_p);
23408 }
23409
23410 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23411
23412 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23413
23414 static pa_kind_t
23415 pa_kind (tree t)
23416 {
23417 if (PACK_EXPANSION_P (t))
23418 t = PACK_EXPANSION_PATTERN (t);
23419 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23420 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23421 || DECL_TYPE_TEMPLATE_P (t))
23422 return pa_tmpl;
23423 else if (TYPE_P (t))
23424 return pa_type;
23425 else
23426 return pa_expr;
23427 }
23428
23429 /* Deduce the value of template parameters. TPARMS is the (innermost)
23430 set of template parameters to a template. TARGS is the bindings
23431 for those template parameters, as determined thus far; TARGS may
23432 include template arguments for outer levels of template parameters
23433 as well. PARM is a parameter to a template function, or a
23434 subcomponent of that parameter; ARG is the corresponding argument.
23435 This function attempts to match PARM with ARG in a manner
23436 consistent with the existing assignments in TARGS. If more values
23437 are deduced, then TARGS is updated.
23438
23439 Returns 0 if the type deduction succeeds, 1 otherwise. The
23440 parameter STRICT is a bitwise or of the following flags:
23441
23442 UNIFY_ALLOW_NONE:
23443 Require an exact match between PARM and ARG.
23444 UNIFY_ALLOW_MORE_CV_QUAL:
23445 Allow the deduced ARG to be more cv-qualified (by qualification
23446 conversion) than ARG.
23447 UNIFY_ALLOW_LESS_CV_QUAL:
23448 Allow the deduced ARG to be less cv-qualified than ARG.
23449 UNIFY_ALLOW_DERIVED:
23450 Allow the deduced ARG to be a template base class of ARG,
23451 or a pointer to a template base class of the type pointed to by
23452 ARG.
23453 UNIFY_ALLOW_INTEGER:
23454 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23455 case for more information.
23456 UNIFY_ALLOW_OUTER_LEVEL:
23457 This is the outermost level of a deduction. Used to determine validity
23458 of qualification conversions. A valid qualification conversion must
23459 have const qualified pointers leading up to the inner type which
23460 requires additional CV quals, except at the outer level, where const
23461 is not required [conv.qual]. It would be normal to set this flag in
23462 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23463 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23464 This is the outermost level of a deduction, and PARM can be more CV
23465 qualified at this point.
23466 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23467 This is the outermost level of a deduction, and PARM can be less CV
23468 qualified at this point. */
23469
23470 static int
23471 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23472 bool explain_p)
23473 {
23474 int idx;
23475 tree targ;
23476 tree tparm;
23477 int strict_in = strict;
23478 tsubst_flags_t complain = (explain_p
23479 ? tf_warning_or_error
23480 : tf_none);
23481
23482 /* I don't think this will do the right thing with respect to types.
23483 But the only case I've seen it in so far has been array bounds, where
23484 signedness is the only information lost, and I think that will be
23485 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23486 finish_id_expression_1, and are also OK. */
23487 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23488 parm = TREE_OPERAND (parm, 0);
23489
23490 if (arg == error_mark_node)
23491 return unify_invalid (explain_p);
23492 if (arg == unknown_type_node
23493 || arg == init_list_type_node)
23494 /* We can't deduce anything from this, but we might get all the
23495 template args from other function args. */
23496 return unify_success (explain_p);
23497
23498 if (parm == any_targ_node || arg == any_targ_node)
23499 return unify_success (explain_p);
23500
23501 /* If PARM uses template parameters, then we can't bail out here,
23502 even if ARG == PARM, since we won't record unifications for the
23503 template parameters. We might need them if we're trying to
23504 figure out which of two things is more specialized. */
23505 if (arg == parm && !uses_template_parms (parm))
23506 return unify_success (explain_p);
23507
23508 /* Handle init lists early, so the rest of the function can assume
23509 we're dealing with a type. */
23510 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23511 {
23512 tree elt, elttype;
23513 unsigned i;
23514 tree orig_parm = parm;
23515
23516 if (!is_std_init_list (parm)
23517 && TREE_CODE (parm) != ARRAY_TYPE)
23518 /* We can only deduce from an initializer list argument if the
23519 parameter is std::initializer_list or an array; otherwise this
23520 is a non-deduced context. */
23521 return unify_success (explain_p);
23522
23523 if (TREE_CODE (parm) == ARRAY_TYPE)
23524 elttype = TREE_TYPE (parm);
23525 else
23526 {
23527 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23528 /* Deduction is defined in terms of a single type, so just punt
23529 on the (bizarre) std::initializer_list<T...>. */
23530 if (PACK_EXPANSION_P (elttype))
23531 return unify_success (explain_p);
23532 }
23533
23534 if (strict != DEDUCE_EXACT
23535 && TYPE_P (elttype)
23536 && !uses_deducible_template_parms (elttype))
23537 /* If ELTTYPE has no deducible template parms, skip deduction from
23538 the list elements. */;
23539 else
23540 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23541 {
23542 int elt_strict = strict;
23543
23544 if (elt == error_mark_node)
23545 return unify_invalid (explain_p);
23546
23547 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23548 {
23549 tree type = TREE_TYPE (elt);
23550 if (type == error_mark_node)
23551 return unify_invalid (explain_p);
23552 /* It should only be possible to get here for a call. */
23553 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23554 elt_strict |= maybe_adjust_types_for_deduction
23555 (DEDUCE_CALL, &elttype, &type, elt);
23556 elt = type;
23557 }
23558
23559 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23560 explain_p);
23561 }
23562
23563 if (TREE_CODE (parm) == ARRAY_TYPE
23564 && deducible_array_bound (TYPE_DOMAIN (parm)))
23565 {
23566 /* Also deduce from the length of the initializer list. */
23567 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23568 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23569 if (idx == error_mark_node)
23570 return unify_invalid (explain_p);
23571 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23572 idx, explain_p);
23573 }
23574
23575 /* If the std::initializer_list<T> deduction worked, replace the
23576 deduced A with std::initializer_list<A>. */
23577 if (orig_parm != parm)
23578 {
23579 idx = TEMPLATE_TYPE_IDX (orig_parm);
23580 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23581 targ = listify (targ);
23582 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23583 }
23584 return unify_success (explain_p);
23585 }
23586
23587 /* If parm and arg aren't the same kind of thing (template, type, or
23588 expression), fail early. */
23589 if (pa_kind (parm) != pa_kind (arg))
23590 return unify_invalid (explain_p);
23591
23592 /* Immediately reject some pairs that won't unify because of
23593 cv-qualification mismatches. */
23594 if (TREE_CODE (arg) == TREE_CODE (parm)
23595 && TYPE_P (arg)
23596 /* It is the elements of the array which hold the cv quals of an array
23597 type, and the elements might be template type parms. We'll check
23598 when we recurse. */
23599 && TREE_CODE (arg) != ARRAY_TYPE
23600 /* We check the cv-qualifiers when unifying with template type
23601 parameters below. We want to allow ARG `const T' to unify with
23602 PARM `T' for example, when computing which of two templates
23603 is more specialized, for example. */
23604 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23605 && !check_cv_quals_for_unify (strict_in, arg, parm))
23606 return unify_cv_qual_mismatch (explain_p, parm, arg);
23607
23608 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23609 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23610 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23611 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23612 strict &= ~UNIFY_ALLOW_DERIVED;
23613 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23614 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23615
23616 switch (TREE_CODE (parm))
23617 {
23618 case TYPENAME_TYPE:
23619 case SCOPE_REF:
23620 case UNBOUND_CLASS_TEMPLATE:
23621 /* In a type which contains a nested-name-specifier, template
23622 argument values cannot be deduced for template parameters used
23623 within the nested-name-specifier. */
23624 return unify_success (explain_p);
23625
23626 case TEMPLATE_TYPE_PARM:
23627 case TEMPLATE_TEMPLATE_PARM:
23628 case BOUND_TEMPLATE_TEMPLATE_PARM:
23629 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23630 if (error_operand_p (tparm))
23631 return unify_invalid (explain_p);
23632
23633 if (TEMPLATE_TYPE_LEVEL (parm)
23634 != template_decl_level (tparm))
23635 /* The PARM is not one we're trying to unify. Just check
23636 to see if it matches ARG. */
23637 {
23638 if (TREE_CODE (arg) == TREE_CODE (parm)
23639 && (is_auto (parm) ? is_auto (arg)
23640 : same_type_p (parm, arg)))
23641 return unify_success (explain_p);
23642 else
23643 return unify_type_mismatch (explain_p, parm, arg);
23644 }
23645 idx = TEMPLATE_TYPE_IDX (parm);
23646 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23647 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23648 if (error_operand_p (tparm))
23649 return unify_invalid (explain_p);
23650
23651 /* Check for mixed types and values. */
23652 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23653 && TREE_CODE (tparm) != TYPE_DECL)
23654 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23655 && TREE_CODE (tparm) != TEMPLATE_DECL))
23656 gcc_unreachable ();
23657
23658 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23659 {
23660 if ((strict_in & UNIFY_ALLOW_DERIVED)
23661 && CLASS_TYPE_P (arg))
23662 {
23663 /* First try to match ARG directly. */
23664 tree t = try_class_unification (tparms, targs, parm, arg,
23665 explain_p);
23666 if (!t)
23667 {
23668 /* Otherwise, look for a suitable base of ARG, as below. */
23669 enum template_base_result r;
23670 r = get_template_base (tparms, targs, parm, arg,
23671 explain_p, &t);
23672 if (!t)
23673 return unify_no_common_base (explain_p, r, parm, arg);
23674 arg = t;
23675 }
23676 }
23677 /* ARG must be constructed from a template class or a template
23678 template parameter. */
23679 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23680 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23681 return unify_template_deduction_failure (explain_p, parm, arg);
23682
23683 /* Deduce arguments T, i from TT<T> or TT<i>. */
23684 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23685 return 1;
23686
23687 arg = TYPE_TI_TEMPLATE (arg);
23688
23689 /* Fall through to deduce template name. */
23690 }
23691
23692 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23693 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23694 {
23695 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23696
23697 /* Simple cases: Value already set, does match or doesn't. */
23698 if (targ != NULL_TREE && template_args_equal (targ, arg))
23699 return unify_success (explain_p);
23700 else if (targ)
23701 return unify_inconsistency (explain_p, parm, targ, arg);
23702 }
23703 else
23704 {
23705 /* If PARM is `const T' and ARG is only `int', we don't have
23706 a match unless we are allowing additional qualification.
23707 If ARG is `const int' and PARM is just `T' that's OK;
23708 that binds `const int' to `T'. */
23709 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23710 arg, parm))
23711 return unify_cv_qual_mismatch (explain_p, parm, arg);
23712
23713 /* Consider the case where ARG is `const volatile int' and
23714 PARM is `const T'. Then, T should be `volatile int'. */
23715 arg = cp_build_qualified_type_real
23716 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23717 if (arg == error_mark_node)
23718 return unify_invalid (explain_p);
23719
23720 /* Simple cases: Value already set, does match or doesn't. */
23721 if (targ != NULL_TREE && same_type_p (targ, arg))
23722 return unify_success (explain_p);
23723 else if (targ)
23724 return unify_inconsistency (explain_p, parm, targ, arg);
23725
23726 /* Make sure that ARG is not a variable-sized array. (Note
23727 that were talking about variable-sized arrays (like
23728 `int[n]'), rather than arrays of unknown size (like
23729 `int[]').) We'll get very confused by such a type since
23730 the bound of the array is not constant, and therefore
23731 not mangleable. Besides, such types are not allowed in
23732 ISO C++, so we can do as we please here. We do allow
23733 them for 'auto' deduction, since that isn't ABI-exposed. */
23734 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23735 return unify_vla_arg (explain_p, arg);
23736
23737 /* Strip typedefs as in convert_template_argument. */
23738 arg = canonicalize_type_argument (arg, tf_none);
23739 }
23740
23741 /* If ARG is a parameter pack or an expansion, we cannot unify
23742 against it unless PARM is also a parameter pack. */
23743 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23744 && !template_parameter_pack_p (parm))
23745 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23746
23747 /* If the argument deduction results is a METHOD_TYPE,
23748 then there is a problem.
23749 METHOD_TYPE doesn't map to any real C++ type the result of
23750 the deduction cannot be of that type. */
23751 if (TREE_CODE (arg) == METHOD_TYPE)
23752 return unify_method_type_error (explain_p, arg);
23753
23754 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23755 return unify_success (explain_p);
23756
23757 case TEMPLATE_PARM_INDEX:
23758 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23759 if (error_operand_p (tparm))
23760 return unify_invalid (explain_p);
23761
23762 if (TEMPLATE_PARM_LEVEL (parm)
23763 != template_decl_level (tparm))
23764 {
23765 /* The PARM is not one we're trying to unify. Just check
23766 to see if it matches ARG. */
23767 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23768 && cp_tree_equal (parm, arg));
23769 if (result)
23770 unify_expression_unequal (explain_p, parm, arg);
23771 return result;
23772 }
23773
23774 idx = TEMPLATE_PARM_IDX (parm);
23775 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23776
23777 if (targ)
23778 {
23779 if ((strict & UNIFY_ALLOW_INTEGER)
23780 && TREE_TYPE (targ) && TREE_TYPE (arg)
23781 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23782 /* We're deducing from an array bound, the type doesn't matter. */
23783 arg = fold_convert (TREE_TYPE (targ), arg);
23784 int x = !cp_tree_equal (targ, arg);
23785 if (x)
23786 unify_inconsistency (explain_p, parm, targ, arg);
23787 return x;
23788 }
23789
23790 /* [temp.deduct.type] If, in the declaration of a function template
23791 with a non-type template-parameter, the non-type
23792 template-parameter is used in an expression in the function
23793 parameter-list and, if the corresponding template-argument is
23794 deduced, the template-argument type shall match the type of the
23795 template-parameter exactly, except that a template-argument
23796 deduced from an array bound may be of any integral type.
23797 The non-type parameter might use already deduced type parameters. */
23798 tparm = TREE_TYPE (parm);
23799 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23800 /* We don't have enough levels of args to do any substitution. This
23801 can happen in the context of -fnew-ttp-matching. */;
23802 else
23803 {
23804 ++processing_template_decl;
23805 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23806 --processing_template_decl;
23807
23808 if (tree a = type_uses_auto (tparm))
23809 {
23810 tparm = do_auto_deduction (tparm, arg, a,
23811 complain, adc_unify, targs);
23812 if (tparm == error_mark_node)
23813 return 1;
23814 }
23815 }
23816
23817 if (!TREE_TYPE (arg))
23818 /* Template-parameter dependent expression. Just accept it for now.
23819 It will later be processed in convert_template_argument. */
23820 ;
23821 else if (same_type_ignoring_top_level_qualifiers_p
23822 (non_reference (TREE_TYPE (arg)),
23823 non_reference (tparm)))
23824 /* OK. Ignore top-level quals here because a class-type template
23825 parameter object is const. */;
23826 else if ((strict & UNIFY_ALLOW_INTEGER)
23827 && CP_INTEGRAL_TYPE_P (tparm))
23828 /* Convert the ARG to the type of PARM; the deduced non-type
23829 template argument must exactly match the types of the
23830 corresponding parameter. */
23831 arg = fold (build_nop (tparm, arg));
23832 else if (uses_template_parms (tparm))
23833 {
23834 /* We haven't deduced the type of this parameter yet. */
23835 if (cxx_dialect >= cxx17
23836 /* We deduce from array bounds in try_array_deduction. */
23837 && !(strict & UNIFY_ALLOW_INTEGER)
23838 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
23839 {
23840 /* Deduce it from the non-type argument. */
23841 tree atype = TREE_TYPE (arg);
23842 RECUR_AND_CHECK_FAILURE (tparms, targs,
23843 tparm, atype,
23844 UNIFY_ALLOW_NONE, explain_p);
23845 /* Now check whether the type of this parameter is still
23846 dependent, and give up if so. */
23847 ++processing_template_decl;
23848 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23849 --processing_template_decl;
23850 if (uses_template_parms (tparm))
23851 return unify_success (explain_p);
23852 }
23853 else
23854 /* Try again later. */
23855 return unify_success (explain_p);
23856 }
23857 else
23858 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23859
23860 /* If ARG is a parameter pack or an expansion, we cannot unify
23861 against it unless PARM is also a parameter pack. */
23862 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23863 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23864 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23865
23866 {
23867 bool removed_attr = false;
23868 arg = strip_typedefs_expr (arg, &removed_attr);
23869 }
23870 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23871 return unify_success (explain_p);
23872
23873 case PTRMEM_CST:
23874 {
23875 /* A pointer-to-member constant can be unified only with
23876 another constant. */
23877 if (TREE_CODE (arg) != PTRMEM_CST)
23878 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23879
23880 /* Just unify the class member. It would be useless (and possibly
23881 wrong, depending on the strict flags) to unify also
23882 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23883 arg refer to the same variable, even if through different
23884 classes. For instance:
23885
23886 struct A { int x; };
23887 struct B : A { };
23888
23889 Unification of &A::x and &B::x must succeed. */
23890 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23891 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23892 }
23893
23894 case POINTER_TYPE:
23895 {
23896 if (!TYPE_PTR_P (arg))
23897 return unify_type_mismatch (explain_p, parm, arg);
23898
23899 /* [temp.deduct.call]
23900
23901 A can be another pointer or pointer to member type that can
23902 be converted to the deduced A via a qualification
23903 conversion (_conv.qual_).
23904
23905 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23906 This will allow for additional cv-qualification of the
23907 pointed-to types if appropriate. */
23908
23909 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23910 /* The derived-to-base conversion only persists through one
23911 level of pointers. */
23912 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23913
23914 return unify (tparms, targs, TREE_TYPE (parm),
23915 TREE_TYPE (arg), strict, explain_p);
23916 }
23917
23918 case REFERENCE_TYPE:
23919 if (!TYPE_REF_P (arg))
23920 return unify_type_mismatch (explain_p, parm, arg);
23921 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23922 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23923
23924 case ARRAY_TYPE:
23925 if (TREE_CODE (arg) != ARRAY_TYPE)
23926 return unify_type_mismatch (explain_p, parm, arg);
23927 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23928 != (TYPE_DOMAIN (arg) == NULL_TREE))
23929 return unify_type_mismatch (explain_p, parm, arg);
23930 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23931 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23932 if (TYPE_DOMAIN (parm) != NULL_TREE)
23933 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23934 TYPE_DOMAIN (arg), explain_p);
23935 return unify_success (explain_p);
23936
23937 case REAL_TYPE:
23938 case COMPLEX_TYPE:
23939 case VECTOR_TYPE:
23940 case INTEGER_TYPE:
23941 case BOOLEAN_TYPE:
23942 case ENUMERAL_TYPE:
23943 case VOID_TYPE:
23944 case OPAQUE_TYPE:
23945 case NULLPTR_TYPE:
23946 if (TREE_CODE (arg) != TREE_CODE (parm))
23947 return unify_type_mismatch (explain_p, parm, arg);
23948
23949 /* We have already checked cv-qualification at the top of the
23950 function. */
23951 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23952 return unify_type_mismatch (explain_p, parm, arg);
23953
23954 /* As far as unification is concerned, this wins. Later checks
23955 will invalidate it if necessary. */
23956 return unify_success (explain_p);
23957
23958 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23959 /* Type INTEGER_CST can come from ordinary constant template args. */
23960 case INTEGER_CST:
23961 while (CONVERT_EXPR_P (arg))
23962 arg = TREE_OPERAND (arg, 0);
23963
23964 if (TREE_CODE (arg) != INTEGER_CST)
23965 return unify_template_argument_mismatch (explain_p, parm, arg);
23966 return (tree_int_cst_equal (parm, arg)
23967 ? unify_success (explain_p)
23968 : unify_template_argument_mismatch (explain_p, parm, arg));
23969
23970 case TREE_VEC:
23971 {
23972 int i, len, argslen;
23973 int parm_variadic_p = 0;
23974
23975 if (TREE_CODE (arg) != TREE_VEC)
23976 return unify_template_argument_mismatch (explain_p, parm, arg);
23977
23978 len = TREE_VEC_LENGTH (parm);
23979 argslen = TREE_VEC_LENGTH (arg);
23980
23981 /* Check for pack expansions in the parameters. */
23982 for (i = 0; i < len; ++i)
23983 {
23984 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23985 {
23986 if (i == len - 1)
23987 /* We can unify against something with a trailing
23988 parameter pack. */
23989 parm_variadic_p = 1;
23990 else
23991 /* [temp.deduct.type]/9: If the template argument list of
23992 P contains a pack expansion that is not the last
23993 template argument, the entire template argument list
23994 is a non-deduced context. */
23995 return unify_success (explain_p);
23996 }
23997 }
23998
23999 /* If we don't have enough arguments to satisfy the parameters
24000 (not counting the pack expression at the end), or we have
24001 too many arguments for a parameter list that doesn't end in
24002 a pack expression, we can't unify. */
24003 if (parm_variadic_p
24004 ? argslen < len - parm_variadic_p
24005 : argslen != len)
24006 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
24007
24008 /* Unify all of the parameters that precede the (optional)
24009 pack expression. */
24010 for (i = 0; i < len - parm_variadic_p; ++i)
24011 {
24012 RECUR_AND_CHECK_FAILURE (tparms, targs,
24013 TREE_VEC_ELT (parm, i),
24014 TREE_VEC_ELT (arg, i),
24015 UNIFY_ALLOW_NONE, explain_p);
24016 }
24017 if (parm_variadic_p)
24018 return unify_pack_expansion (tparms, targs, parm, arg,
24019 DEDUCE_EXACT,
24020 /*subr=*/true, explain_p);
24021 return unify_success (explain_p);
24022 }
24023
24024 case RECORD_TYPE:
24025 case UNION_TYPE:
24026 if (TREE_CODE (arg) != TREE_CODE (parm))
24027 return unify_type_mismatch (explain_p, parm, arg);
24028
24029 if (TYPE_PTRMEMFUNC_P (parm))
24030 {
24031 if (!TYPE_PTRMEMFUNC_P (arg))
24032 return unify_type_mismatch (explain_p, parm, arg);
24033
24034 return unify (tparms, targs,
24035 TYPE_PTRMEMFUNC_FN_TYPE (parm),
24036 TYPE_PTRMEMFUNC_FN_TYPE (arg),
24037 strict, explain_p);
24038 }
24039 else if (TYPE_PTRMEMFUNC_P (arg))
24040 return unify_type_mismatch (explain_p, parm, arg);
24041
24042 if (CLASSTYPE_TEMPLATE_INFO (parm))
24043 {
24044 tree t = NULL_TREE;
24045
24046 if (strict_in & UNIFY_ALLOW_DERIVED)
24047 {
24048 /* First, we try to unify the PARM and ARG directly. */
24049 t = try_class_unification (tparms, targs,
24050 parm, arg, explain_p);
24051
24052 if (!t)
24053 {
24054 /* Fallback to the special case allowed in
24055 [temp.deduct.call]:
24056
24057 If P is a class, and P has the form
24058 template-id, then A can be a derived class of
24059 the deduced A. Likewise, if P is a pointer to
24060 a class of the form template-id, A can be a
24061 pointer to a derived class pointed to by the
24062 deduced A. */
24063 enum template_base_result r;
24064 r = get_template_base (tparms, targs, parm, arg,
24065 explain_p, &t);
24066
24067 if (!t)
24068 {
24069 /* Don't give the derived diagnostic if we're
24070 already dealing with the same template. */
24071 bool same_template
24072 = (CLASSTYPE_TEMPLATE_INFO (arg)
24073 && (CLASSTYPE_TI_TEMPLATE (parm)
24074 == CLASSTYPE_TI_TEMPLATE (arg)));
24075 return unify_no_common_base (explain_p && !same_template,
24076 r, parm, arg);
24077 }
24078 }
24079 }
24080 else if (CLASSTYPE_TEMPLATE_INFO (arg)
24081 && (CLASSTYPE_TI_TEMPLATE (parm)
24082 == CLASSTYPE_TI_TEMPLATE (arg)))
24083 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24084 Then, we should unify `int' and `U'. */
24085 t = arg;
24086 else
24087 /* There's no chance of unification succeeding. */
24088 return unify_type_mismatch (explain_p, parm, arg);
24089
24090 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
24091 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
24092 }
24093 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
24094 return unify_type_mismatch (explain_p, parm, arg);
24095 return unify_success (explain_p);
24096
24097 case METHOD_TYPE:
24098 case FUNCTION_TYPE:
24099 {
24100 unsigned int nargs;
24101 tree *args;
24102 tree a;
24103 unsigned int i;
24104
24105 if (TREE_CODE (arg) != TREE_CODE (parm))
24106 return unify_type_mismatch (explain_p, parm, arg);
24107
24108 /* CV qualifications for methods can never be deduced, they must
24109 match exactly. We need to check them explicitly here,
24110 because type_unification_real treats them as any other
24111 cv-qualified parameter. */
24112 if (TREE_CODE (parm) == METHOD_TYPE
24113 && (!check_cv_quals_for_unify
24114 (UNIFY_ALLOW_NONE,
24115 class_of_this_parm (arg),
24116 class_of_this_parm (parm))))
24117 return unify_cv_qual_mismatch (explain_p, parm, arg);
24118 if (TREE_CODE (arg) == FUNCTION_TYPE
24119 && type_memfn_quals (parm) != type_memfn_quals (arg))
24120 return unify_cv_qual_mismatch (explain_p, parm, arg);
24121 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
24122 return unify_type_mismatch (explain_p, parm, arg);
24123
24124 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
24125 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
24126
24127 nargs = list_length (TYPE_ARG_TYPES (arg));
24128 args = XALLOCAVEC (tree, nargs);
24129 for (a = TYPE_ARG_TYPES (arg), i = 0;
24130 a != NULL_TREE && a != void_list_node;
24131 a = TREE_CHAIN (a), ++i)
24132 args[i] = TREE_VALUE (a);
24133 nargs = i;
24134
24135 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
24136 args, nargs, 1, DEDUCE_EXACT,
24137 NULL, explain_p))
24138 return 1;
24139
24140 if (flag_noexcept_type)
24141 {
24142 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
24143 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
24144 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
24145 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
24146 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
24147 && uses_template_parms (TREE_PURPOSE (pspec)))
24148 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
24149 TREE_PURPOSE (aspec),
24150 UNIFY_ALLOW_NONE, explain_p);
24151 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
24152 return unify_type_mismatch (explain_p, parm, arg);
24153 }
24154
24155 return 0;
24156 }
24157
24158 case OFFSET_TYPE:
24159 /* Unify a pointer to member with a pointer to member function, which
24160 deduces the type of the member as a function type. */
24161 if (TYPE_PTRMEMFUNC_P (arg))
24162 {
24163 /* Check top-level cv qualifiers */
24164 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
24165 return unify_cv_qual_mismatch (explain_p, parm, arg);
24166
24167 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24168 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
24169 UNIFY_ALLOW_NONE, explain_p);
24170
24171 /* Determine the type of the function we are unifying against. */
24172 tree fntype = static_fn_type (arg);
24173
24174 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
24175 }
24176
24177 if (TREE_CODE (arg) != OFFSET_TYPE)
24178 return unify_type_mismatch (explain_p, parm, arg);
24179 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24180 TYPE_OFFSET_BASETYPE (arg),
24181 UNIFY_ALLOW_NONE, explain_p);
24182 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24183 strict, explain_p);
24184
24185 case CONST_DECL:
24186 if (DECL_TEMPLATE_PARM_P (parm))
24187 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
24188 if (arg != scalar_constant_value (parm))
24189 return unify_template_argument_mismatch (explain_p, parm, arg);
24190 return unify_success (explain_p);
24191
24192 case FIELD_DECL:
24193 case TEMPLATE_DECL:
24194 /* Matched cases are handled by the ARG == PARM test above. */
24195 return unify_template_argument_mismatch (explain_p, parm, arg);
24196
24197 case VAR_DECL:
24198 /* We might get a variable as a non-type template argument in parm if the
24199 corresponding parameter is type-dependent. Make any necessary
24200 adjustments based on whether arg is a reference. */
24201 if (CONSTANT_CLASS_P (arg))
24202 parm = fold_non_dependent_expr (parm, complain);
24203 else if (REFERENCE_REF_P (arg))
24204 {
24205 tree sub = TREE_OPERAND (arg, 0);
24206 STRIP_NOPS (sub);
24207 if (TREE_CODE (sub) == ADDR_EXPR)
24208 arg = TREE_OPERAND (sub, 0);
24209 }
24210 /* Now use the normal expression code to check whether they match. */
24211 goto expr;
24212
24213 case TYPE_ARGUMENT_PACK:
24214 case NONTYPE_ARGUMENT_PACK:
24215 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
24216 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
24217
24218 case TYPEOF_TYPE:
24219 case DECLTYPE_TYPE:
24220 case UNDERLYING_TYPE:
24221 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24222 or UNDERLYING_TYPE nodes. */
24223 return unify_success (explain_p);
24224
24225 case ERROR_MARK:
24226 /* Unification fails if we hit an error node. */
24227 return unify_invalid (explain_p);
24228
24229 case INDIRECT_REF:
24230 if (REFERENCE_REF_P (parm))
24231 {
24232 bool pexp = PACK_EXPANSION_P (arg);
24233 if (pexp)
24234 arg = PACK_EXPANSION_PATTERN (arg);
24235 if (REFERENCE_REF_P (arg))
24236 arg = TREE_OPERAND (arg, 0);
24237 if (pexp)
24238 arg = make_pack_expansion (arg, complain);
24239 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24240 strict, explain_p);
24241 }
24242 /* FALLTHRU */
24243
24244 default:
24245 /* An unresolved overload is a nondeduced context. */
24246 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24247 return unify_success (explain_p);
24248 gcc_assert (EXPR_P (parm)
24249 || COMPOUND_LITERAL_P (parm)
24250 || TREE_CODE (parm) == TRAIT_EXPR);
24251 expr:
24252 /* We must be looking at an expression. This can happen with
24253 something like:
24254
24255 template <int I>
24256 void foo(S<I>, S<I + 2>);
24257
24258 or
24259
24260 template<typename T>
24261 void foo(A<T, T{}>);
24262
24263 This is a "non-deduced context":
24264
24265 [deduct.type]
24266
24267 The non-deduced contexts are:
24268
24269 --A non-type template argument or an array bound in which
24270 a subexpression references a template parameter.
24271
24272 In these cases, we assume deduction succeeded, but don't
24273 actually infer any unifications. */
24274
24275 if (!uses_template_parms (parm)
24276 && !template_args_equal (parm, arg))
24277 return unify_expression_unequal (explain_p, parm, arg);
24278 else
24279 return unify_success (explain_p);
24280 }
24281 }
24282 #undef RECUR_AND_CHECK_FAILURE
24283 \f
24284 /* Note that DECL can be defined in this translation unit, if
24285 required. */
24286
24287 static void
24288 mark_definable (tree decl)
24289 {
24290 tree clone;
24291 DECL_NOT_REALLY_EXTERN (decl) = 1;
24292 FOR_EACH_CLONE (clone, decl)
24293 DECL_NOT_REALLY_EXTERN (clone) = 1;
24294 }
24295
24296 /* Called if RESULT is explicitly instantiated, or is a member of an
24297 explicitly instantiated class. */
24298
24299 void
24300 mark_decl_instantiated (tree result, int extern_p)
24301 {
24302 SET_DECL_EXPLICIT_INSTANTIATION (result);
24303
24304 /* If this entity has already been written out, it's too late to
24305 make any modifications. */
24306 if (TREE_ASM_WRITTEN (result))
24307 return;
24308
24309 /* consteval functions are never emitted. */
24310 if (TREE_CODE (result) == FUNCTION_DECL
24311 && DECL_IMMEDIATE_FUNCTION_P (result))
24312 return;
24313
24314 /* For anonymous namespace we don't need to do anything. */
24315 if (decl_anon_ns_mem_p (result))
24316 {
24317 gcc_assert (!TREE_PUBLIC (result));
24318 return;
24319 }
24320
24321 if (TREE_CODE (result) != FUNCTION_DECL)
24322 /* The TREE_PUBLIC flag for function declarations will have been
24323 set correctly by tsubst. */
24324 TREE_PUBLIC (result) = 1;
24325
24326 /* This might have been set by an earlier implicit instantiation. */
24327 DECL_COMDAT (result) = 0;
24328
24329 if (extern_p)
24330 {
24331 DECL_EXTERNAL (result) = 1;
24332 DECL_NOT_REALLY_EXTERN (result) = 0;
24333 }
24334 else
24335 {
24336 mark_definable (result);
24337 mark_needed (result);
24338 /* Always make artificials weak. */
24339 if (DECL_ARTIFICIAL (result) && flag_weak)
24340 comdat_linkage (result);
24341 /* For WIN32 we also want to put explicit instantiations in
24342 linkonce sections. */
24343 else if (TREE_PUBLIC (result))
24344 maybe_make_one_only (result);
24345 if (TREE_CODE (result) == FUNCTION_DECL
24346 && DECL_TEMPLATE_INSTANTIATED (result))
24347 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24348 since start_preparsed_function wouldn't have if we had an earlier
24349 extern explicit instantiation. */
24350 DECL_EXTERNAL (result) = 0;
24351 }
24352
24353 /* If EXTERN_P, then this function will not be emitted -- unless
24354 followed by an explicit instantiation, at which point its linkage
24355 will be adjusted. If !EXTERN_P, then this function will be
24356 emitted here. In neither circumstance do we want
24357 import_export_decl to adjust the linkage. */
24358 DECL_INTERFACE_KNOWN (result) = 1;
24359 }
24360
24361 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24362 important template arguments. If any are missing, we check whether
24363 they're important by using error_mark_node for substituting into any
24364 args that were used for partial ordering (the ones between ARGS and END)
24365 and seeing if it bubbles up. */
24366
24367 static bool
24368 check_undeduced_parms (tree targs, tree args, tree end)
24369 {
24370 bool found = false;
24371 int i;
24372 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24373 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24374 {
24375 found = true;
24376 TREE_VEC_ELT (targs, i) = error_mark_node;
24377 }
24378 if (found)
24379 {
24380 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24381 if (substed == error_mark_node)
24382 return true;
24383 }
24384 return false;
24385 }
24386
24387 /* Given two function templates PAT1 and PAT2, return:
24388
24389 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24390 -1 if PAT2 is more specialized than PAT1.
24391 0 if neither is more specialized.
24392
24393 LEN indicates the number of parameters we should consider
24394 (defaulted parameters should not be considered).
24395
24396 The 1998 std underspecified function template partial ordering, and
24397 DR214 addresses the issue. We take pairs of arguments, one from
24398 each of the templates, and deduce them against each other. One of
24399 the templates will be more specialized if all the *other*
24400 template's arguments deduce against its arguments and at least one
24401 of its arguments *does* *not* deduce against the other template's
24402 corresponding argument. Deduction is done as for class templates.
24403 The arguments used in deduction have reference and top level cv
24404 qualifiers removed. Iff both arguments were originally reference
24405 types *and* deduction succeeds in both directions, an lvalue reference
24406 wins against an rvalue reference and otherwise the template
24407 with the more cv-qualified argument wins for that pairing (if
24408 neither is more cv-qualified, they both are equal). Unlike regular
24409 deduction, after all the arguments have been deduced in this way,
24410 we do *not* verify the deduced template argument values can be
24411 substituted into non-deduced contexts.
24412
24413 The logic can be a bit confusing here, because we look at deduce1 and
24414 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24415 can find template arguments for pat1 to make arg1 look like arg2, that
24416 means that arg2 is at least as specialized as arg1. */
24417
24418 int
24419 more_specialized_fn (tree pat1, tree pat2, int len)
24420 {
24421 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24422 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24423 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24424 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24425 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24426 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24427 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24428 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24429 tree origs1, origs2;
24430 bool lose1 = false;
24431 bool lose2 = false;
24432
24433 /* Remove the this parameter from non-static member functions. If
24434 one is a non-static member function and the other is not a static
24435 member function, remove the first parameter from that function
24436 also. This situation occurs for operator functions where we
24437 locate both a member function (with this pointer) and non-member
24438 operator (with explicit first operand). */
24439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24440 {
24441 len--; /* LEN is the number of significant arguments for DECL1 */
24442 args1 = TREE_CHAIN (args1);
24443 if (!DECL_STATIC_FUNCTION_P (decl2))
24444 args2 = TREE_CHAIN (args2);
24445 }
24446 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24447 {
24448 args2 = TREE_CHAIN (args2);
24449 if (!DECL_STATIC_FUNCTION_P (decl1))
24450 {
24451 len--;
24452 args1 = TREE_CHAIN (args1);
24453 }
24454 }
24455
24456 /* If only one is a conversion operator, they are unordered. */
24457 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24458 return 0;
24459
24460 /* Consider the return type for a conversion function */
24461 if (DECL_CONV_FN_P (decl1))
24462 {
24463 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24464 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24465 len++;
24466 }
24467
24468 processing_template_decl++;
24469
24470 origs1 = args1;
24471 origs2 = args2;
24472
24473 while (len--
24474 /* Stop when an ellipsis is seen. */
24475 && args1 != NULL_TREE && args2 != NULL_TREE)
24476 {
24477 tree arg1 = TREE_VALUE (args1);
24478 tree arg2 = TREE_VALUE (args2);
24479 int deduce1, deduce2;
24480 int quals1 = -1;
24481 int quals2 = -1;
24482 int ref1 = 0;
24483 int ref2 = 0;
24484
24485 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24486 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24487 {
24488 /* When both arguments are pack expansions, we need only
24489 unify the patterns themselves. */
24490 arg1 = PACK_EXPANSION_PATTERN (arg1);
24491 arg2 = PACK_EXPANSION_PATTERN (arg2);
24492
24493 /* This is the last comparison we need to do. */
24494 len = 0;
24495 }
24496
24497 if (TYPE_REF_P (arg1))
24498 {
24499 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24500 arg1 = TREE_TYPE (arg1);
24501 quals1 = cp_type_quals (arg1);
24502 }
24503
24504 if (TYPE_REF_P (arg2))
24505 {
24506 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24507 arg2 = TREE_TYPE (arg2);
24508 quals2 = cp_type_quals (arg2);
24509 }
24510
24511 arg1 = TYPE_MAIN_VARIANT (arg1);
24512 arg2 = TYPE_MAIN_VARIANT (arg2);
24513
24514 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24515 {
24516 int i, len2 = remaining_arguments (args2);
24517 tree parmvec = make_tree_vec (1);
24518 tree argvec = make_tree_vec (len2);
24519 tree ta = args2;
24520
24521 /* Setup the parameter vector, which contains only ARG1. */
24522 TREE_VEC_ELT (parmvec, 0) = arg1;
24523
24524 /* Setup the argument vector, which contains the remaining
24525 arguments. */
24526 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24527 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24528
24529 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24530 argvec, DEDUCE_EXACT,
24531 /*subr=*/true, /*explain_p=*/false)
24532 == 0);
24533
24534 /* We cannot deduce in the other direction, because ARG1 is
24535 a pack expansion but ARG2 is not. */
24536 deduce2 = 0;
24537 }
24538 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24539 {
24540 int i, len1 = remaining_arguments (args1);
24541 tree parmvec = make_tree_vec (1);
24542 tree argvec = make_tree_vec (len1);
24543 tree ta = args1;
24544
24545 /* Setup the parameter vector, which contains only ARG1. */
24546 TREE_VEC_ELT (parmvec, 0) = arg2;
24547
24548 /* Setup the argument vector, which contains the remaining
24549 arguments. */
24550 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24551 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24552
24553 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24554 argvec, DEDUCE_EXACT,
24555 /*subr=*/true, /*explain_p=*/false)
24556 == 0);
24557
24558 /* We cannot deduce in the other direction, because ARG2 is
24559 a pack expansion but ARG1 is not.*/
24560 deduce1 = 0;
24561 }
24562
24563 else
24564 {
24565 /* The normal case, where neither argument is a pack
24566 expansion. */
24567 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24568 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24569 == 0);
24570 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24571 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24572 == 0);
24573 }
24574
24575 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24576 arg2, then arg2 is not as specialized as arg1. */
24577 if (!deduce1)
24578 lose2 = true;
24579 if (!deduce2)
24580 lose1 = true;
24581
24582 /* "If, for a given type, deduction succeeds in both directions
24583 (i.e., the types are identical after the transformations above)
24584 and both P and A were reference types (before being replaced with
24585 the type referred to above):
24586 - if the type from the argument template was an lvalue reference and
24587 the type from the parameter template was not, the argument type is
24588 considered to be more specialized than the other; otherwise,
24589 - if the type from the argument template is more cv-qualified
24590 than the type from the parameter template (as described above),
24591 the argument type is considered to be more specialized than the other;
24592 otherwise,
24593 - neither type is more specialized than the other." */
24594
24595 if (deduce1 && deduce2)
24596 {
24597 if (ref1 && ref2 && ref1 != ref2)
24598 {
24599 if (ref1 > ref2)
24600 lose1 = true;
24601 else
24602 lose2 = true;
24603 }
24604 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24605 {
24606 if ((quals1 & quals2) == quals2)
24607 lose2 = true;
24608 if ((quals1 & quals2) == quals1)
24609 lose1 = true;
24610 }
24611 }
24612
24613 if (lose1 && lose2)
24614 /* We've failed to deduce something in either direction.
24615 These must be unordered. */
24616 break;
24617
24618 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24619 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24620 /* We have already processed all of the arguments in our
24621 handing of the pack expansion type. */
24622 len = 0;
24623
24624 args1 = TREE_CHAIN (args1);
24625 args2 = TREE_CHAIN (args2);
24626 }
24627
24628 /* "In most cases, all template parameters must have values in order for
24629 deduction to succeed, but for partial ordering purposes a template
24630 parameter may remain without a value provided it is not used in the
24631 types being used for partial ordering."
24632
24633 Thus, if we are missing any of the targs1 we need to substitute into
24634 origs1, then pat2 is not as specialized as pat1. This can happen when
24635 there is a nondeduced context. */
24636 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24637 lose2 = true;
24638 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24639 lose1 = true;
24640
24641 processing_template_decl--;
24642
24643 /* If both deductions succeed, the partial ordering selects the more
24644 constrained template. */
24645 /* P2113: If the corresponding template-parameters of the
24646 template-parameter-lists are not equivalent ([temp.over.link]) or if
24647 the function parameters that positionally correspond between the two
24648 templates are not of the same type, neither template is more
24649 specialized than the other. */
24650 if (!lose1 && !lose2
24651 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24652 DECL_TEMPLATE_PARMS (pat2))
24653 && compparms (origs1, origs2))
24654 {
24655 int winner = more_constrained (decl1, decl2);
24656 if (winner > 0)
24657 lose2 = true;
24658 else if (winner < 0)
24659 lose1 = true;
24660 }
24661
24662 /* All things being equal, if the next argument is a pack expansion
24663 for one function but not for the other, prefer the
24664 non-variadic function. FIXME this is bogus; see c++/41958. */
24665 if (lose1 == lose2
24666 && args1 && TREE_VALUE (args1)
24667 && args2 && TREE_VALUE (args2))
24668 {
24669 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24670 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24671 }
24672
24673 if (lose1 == lose2)
24674 return 0;
24675 else if (!lose1)
24676 return 1;
24677 else
24678 return -1;
24679 }
24680
24681 /* Determine which of two partial specializations of TMPL is more
24682 specialized.
24683
24684 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24685 to the first partial specialization. The TREE_PURPOSE is the
24686 innermost set of template parameters for the partial
24687 specialization. PAT2 is similar, but for the second template.
24688
24689 Return 1 if the first partial specialization is more specialized;
24690 -1 if the second is more specialized; 0 if neither is more
24691 specialized.
24692
24693 See [temp.class.order] for information about determining which of
24694 two templates is more specialized. */
24695
24696 static int
24697 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24698 {
24699 tree targs;
24700 int winner = 0;
24701 bool any_deductions = false;
24702
24703 tree tmpl1 = TREE_VALUE (pat1);
24704 tree tmpl2 = TREE_VALUE (pat2);
24705 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24706 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24707
24708 /* Just like what happens for functions, if we are ordering between
24709 different template specializations, we may encounter dependent
24710 types in the arguments, and we need our dependency check functions
24711 to behave correctly. */
24712 ++processing_template_decl;
24713 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24714 if (targs)
24715 {
24716 --winner;
24717 any_deductions = true;
24718 }
24719
24720 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24721 if (targs)
24722 {
24723 ++winner;
24724 any_deductions = true;
24725 }
24726 --processing_template_decl;
24727
24728 /* If both deductions succeed, the partial ordering selects the more
24729 constrained template. */
24730 if (!winner && any_deductions)
24731 winner = more_constrained (tmpl1, tmpl2);
24732
24733 /* In the case of a tie where at least one of the templates
24734 has a parameter pack at the end, the template with the most
24735 non-packed parameters wins. */
24736 if (winner == 0
24737 && any_deductions
24738 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24739 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24740 {
24741 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24742 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24743 int len1 = TREE_VEC_LENGTH (args1);
24744 int len2 = TREE_VEC_LENGTH (args2);
24745
24746 /* We don't count the pack expansion at the end. */
24747 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24748 --len1;
24749 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24750 --len2;
24751
24752 if (len1 > len2)
24753 return 1;
24754 else if (len1 < len2)
24755 return -1;
24756 }
24757
24758 return winner;
24759 }
24760
24761 /* Return the template arguments that will produce the function signature
24762 DECL from the function template FN, with the explicit template
24763 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24764 also match. Return NULL_TREE if no satisfactory arguments could be
24765 found. */
24766
24767 static tree
24768 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24769 {
24770 int ntparms = DECL_NTPARMS (fn);
24771 tree targs = make_tree_vec (ntparms);
24772 tree decl_type = TREE_TYPE (decl);
24773 tree decl_arg_types;
24774 tree *args;
24775 unsigned int nargs, ix;
24776 tree arg;
24777
24778 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24779
24780 /* Never do unification on the 'this' parameter. */
24781 decl_arg_types = skip_artificial_parms_for (decl,
24782 TYPE_ARG_TYPES (decl_type));
24783
24784 nargs = list_length (decl_arg_types);
24785 args = XALLOCAVEC (tree, nargs);
24786 for (arg = decl_arg_types, ix = 0;
24787 arg != NULL_TREE && arg != void_list_node;
24788 arg = TREE_CHAIN (arg), ++ix)
24789 args[ix] = TREE_VALUE (arg);
24790
24791 if (fn_type_unification (fn, explicit_args, targs,
24792 args, ix,
24793 (check_rettype || DECL_CONV_FN_P (fn)
24794 ? TREE_TYPE (decl_type) : NULL_TREE),
24795 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24796 /*explain_p=*/false,
24797 /*decltype*/false)
24798 == error_mark_node)
24799 return NULL_TREE;
24800
24801 return targs;
24802 }
24803
24804 /* Return the innermost template arguments that, when applied to a partial
24805 specialization SPEC_TMPL of TMPL, yield the ARGS.
24806
24807 For example, suppose we have:
24808
24809 template <class T, class U> struct S {};
24810 template <class T> struct S<T*, int> {};
24811
24812 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24813 partial specialization and the ARGS will be {double*, int}. The resulting
24814 vector will be {double}, indicating that `T' is bound to `double'. */
24815
24816 static tree
24817 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24818 {
24819 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24820 tree spec_args
24821 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24822 int i, ntparms = TREE_VEC_LENGTH (tparms);
24823 tree deduced_args;
24824 tree innermost_deduced_args;
24825
24826 innermost_deduced_args = make_tree_vec (ntparms);
24827 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24828 {
24829 deduced_args = copy_node (args);
24830 SET_TMPL_ARGS_LEVEL (deduced_args,
24831 TMPL_ARGS_DEPTH (deduced_args),
24832 innermost_deduced_args);
24833 }
24834 else
24835 deduced_args = innermost_deduced_args;
24836
24837 bool tried_array_deduction = (cxx_dialect < cxx17);
24838 again:
24839 if (unify (tparms, deduced_args,
24840 INNERMOST_TEMPLATE_ARGS (spec_args),
24841 INNERMOST_TEMPLATE_ARGS (args),
24842 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24843 return NULL_TREE;
24844
24845 for (i = 0; i < ntparms; ++i)
24846 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24847 {
24848 if (!tried_array_deduction)
24849 {
24850 try_array_deduction (tparms, innermost_deduced_args,
24851 INNERMOST_TEMPLATE_ARGS (spec_args));
24852 tried_array_deduction = true;
24853 if (TREE_VEC_ELT (innermost_deduced_args, i))
24854 goto again;
24855 }
24856 return NULL_TREE;
24857 }
24858
24859 if (!push_tinst_level (spec_tmpl, deduced_args))
24860 {
24861 excessive_deduction_depth = true;
24862 return NULL_TREE;
24863 }
24864
24865 /* Verify that nondeduced template arguments agree with the type
24866 obtained from argument deduction.
24867
24868 For example:
24869
24870 struct A { typedef int X; };
24871 template <class T, class U> struct C {};
24872 template <class T> struct C<T, typename T::X> {};
24873
24874 Then with the instantiation `C<A, int>', we can deduce that
24875 `T' is `A' but unify () does not check whether `typename T::X'
24876 is `int'. */
24877 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24878
24879 if (spec_args != error_mark_node)
24880 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24881 INNERMOST_TEMPLATE_ARGS (spec_args),
24882 tmpl, tf_none, false, false);
24883
24884 pop_tinst_level ();
24885
24886 if (spec_args == error_mark_node
24887 /* We only need to check the innermost arguments; the other
24888 arguments will always agree. */
24889 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24890 INNERMOST_TEMPLATE_ARGS (args)))
24891 return NULL_TREE;
24892
24893 /* Now that we have bindings for all of the template arguments,
24894 ensure that the arguments deduced for the template template
24895 parameters have compatible template parameter lists. See the use
24896 of template_template_parm_bindings_ok_p in fn_type_unification
24897 for more information. */
24898 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24899 return NULL_TREE;
24900
24901 return deduced_args;
24902 }
24903
24904 // Compare two function templates T1 and T2 by deducing bindings
24905 // from one against the other. If both deductions succeed, compare
24906 // constraints to see which is more constrained.
24907 static int
24908 more_specialized_inst (tree t1, tree t2)
24909 {
24910 int fate = 0;
24911 int count = 0;
24912
24913 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24914 {
24915 --fate;
24916 ++count;
24917 }
24918
24919 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24920 {
24921 ++fate;
24922 ++count;
24923 }
24924
24925 // If both deductions succeed, then one may be more constrained.
24926 if (count == 2 && fate == 0)
24927 fate = more_constrained (t1, t2);
24928
24929 return fate;
24930 }
24931
24932 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24933 Return the TREE_LIST node with the most specialized template, if
24934 any. If there is no most specialized template, the error_mark_node
24935 is returned.
24936
24937 Note that this function does not look at, or modify, the
24938 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24939 returned is one of the elements of INSTANTIATIONS, callers may
24940 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24941 and retrieve it from the value returned. */
24942
24943 tree
24944 most_specialized_instantiation (tree templates)
24945 {
24946 tree fn, champ;
24947
24948 ++processing_template_decl;
24949
24950 champ = templates;
24951 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24952 {
24953 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24954 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24955 if (fate == -1)
24956 champ = fn;
24957 else if (!fate)
24958 {
24959 /* Equally specialized, move to next function. If there
24960 is no next function, nothing's most specialized. */
24961 fn = TREE_CHAIN (fn);
24962 champ = fn;
24963 if (!fn)
24964 break;
24965 }
24966 }
24967
24968 if (champ)
24969 /* Now verify that champ is better than everything earlier in the
24970 instantiation list. */
24971 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24972 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24973 {
24974 champ = NULL_TREE;
24975 break;
24976 }
24977 }
24978
24979 processing_template_decl--;
24980
24981 if (!champ)
24982 return error_mark_node;
24983
24984 return champ;
24985 }
24986
24987 /* If DECL is a specialization of some template, return the most
24988 general such template. Otherwise, returns NULL_TREE.
24989
24990 For example, given:
24991
24992 template <class T> struct S { template <class U> void f(U); };
24993
24994 if TMPL is `template <class U> void S<int>::f(U)' this will return
24995 the full template. This function will not trace past partial
24996 specializations, however. For example, given in addition:
24997
24998 template <class T> struct S<T*> { template <class U> void f(U); };
24999
25000 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25001 `template <class T> template <class U> S<T*>::f(U)'. */
25002
25003 tree
25004 most_general_template (tree decl)
25005 {
25006 if (TREE_CODE (decl) != TEMPLATE_DECL)
25007 {
25008 if (tree tinfo = get_template_info (decl))
25009 decl = TI_TEMPLATE (tinfo);
25010 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25011 template friend, or a FIELD_DECL for a capture pack. */
25012 if (TREE_CODE (decl) != TEMPLATE_DECL)
25013 return NULL_TREE;
25014 }
25015
25016 /* Look for more and more general templates. */
25017 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
25018 {
25019 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25020 (See cp-tree.h for details.) */
25021 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
25022 break;
25023
25024 if (CLASS_TYPE_P (TREE_TYPE (decl))
25025 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
25026 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
25027 break;
25028
25029 /* Stop if we run into an explicitly specialized class template. */
25030 if (!DECL_NAMESPACE_SCOPE_P (decl)
25031 && DECL_CONTEXT (decl)
25032 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
25033 break;
25034
25035 decl = DECL_TI_TEMPLATE (decl);
25036 }
25037
25038 return decl;
25039 }
25040
25041 /* Return the most specialized of the template partial specializations
25042 which can produce TARGET, a specialization of some class or variable
25043 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25044 a TEMPLATE_DECL node corresponding to the partial specialization, while
25045 the TREE_PURPOSE is the set of template arguments that must be
25046 substituted into the template pattern in order to generate TARGET.
25047
25048 If the choice of partial specialization is ambiguous, a diagnostic
25049 is issued, and the error_mark_node is returned. If there are no
25050 partial specializations matching TARGET, then NULL_TREE is
25051 returned, indicating that the primary template should be used. */
25052
25053 tree
25054 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
25055 {
25056 tree list = NULL_TREE;
25057 tree t;
25058 tree champ;
25059 int fate;
25060 bool ambiguous_p;
25061 tree outer_args = NULL_TREE;
25062 tree tmpl, args;
25063
25064 if (TYPE_P (target))
25065 {
25066 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
25067 tmpl = TI_TEMPLATE (tinfo);
25068 args = TI_ARGS (tinfo);
25069 }
25070 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
25071 {
25072 tmpl = TREE_OPERAND (target, 0);
25073 args = TREE_OPERAND (target, 1);
25074 }
25075 else if (VAR_P (target))
25076 {
25077 tree tinfo = DECL_TEMPLATE_INFO (target);
25078 tmpl = TI_TEMPLATE (tinfo);
25079 args = TI_ARGS (tinfo);
25080 }
25081 else
25082 gcc_unreachable ();
25083
25084 tree main_tmpl = most_general_template (tmpl);
25085
25086 /* For determining which partial specialization to use, only the
25087 innermost args are interesting. */
25088 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25089 {
25090 outer_args = strip_innermost_template_args (args, 1);
25091 args = INNERMOST_TEMPLATE_ARGS (args);
25092 }
25093
25094 /* The caller hasn't called push_to_top_level yet, but we need
25095 get_partial_spec_bindings to be done in non-template context so that we'll
25096 fully resolve everything. */
25097 processing_template_decl_sentinel ptds;
25098
25099 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
25100 {
25101 const tree ospec_tmpl = TREE_VALUE (t);
25102
25103 tree spec_tmpl;
25104 if (outer_args)
25105 {
25106 /* Substitute in the template args from the enclosing class. */
25107 ++processing_template_decl;
25108 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
25109 --processing_template_decl;
25110 if (spec_tmpl == error_mark_node)
25111 return error_mark_node;
25112 }
25113 else
25114 spec_tmpl = ospec_tmpl;
25115
25116 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
25117 if (spec_args)
25118 {
25119 if (outer_args)
25120 spec_args = add_to_template_args (outer_args, spec_args);
25121
25122 /* Keep the candidate only if the constraints are satisfied,
25123 or if we're not compiling with concepts. */
25124 if (!flag_concepts
25125 || constraints_satisfied_p (ospec_tmpl, spec_args))
25126 {
25127 list = tree_cons (spec_args, ospec_tmpl, list);
25128 TREE_TYPE (list) = TREE_TYPE (t);
25129 }
25130 }
25131 }
25132
25133 if (! list)
25134 return NULL_TREE;
25135
25136 ambiguous_p = false;
25137 t = list;
25138 champ = t;
25139 t = TREE_CHAIN (t);
25140 for (; t; t = TREE_CHAIN (t))
25141 {
25142 fate = more_specialized_partial_spec (tmpl, champ, t);
25143 if (fate == 1)
25144 ;
25145 else
25146 {
25147 if (fate == 0)
25148 {
25149 t = TREE_CHAIN (t);
25150 if (! t)
25151 {
25152 ambiguous_p = true;
25153 break;
25154 }
25155 }
25156 champ = t;
25157 }
25158 }
25159
25160 if (!ambiguous_p)
25161 for (t = list; t && t != champ; t = TREE_CHAIN (t))
25162 {
25163 fate = more_specialized_partial_spec (tmpl, champ, t);
25164 if (fate != 1)
25165 {
25166 ambiguous_p = true;
25167 break;
25168 }
25169 }
25170
25171 if (ambiguous_p)
25172 {
25173 const char *str;
25174 char *spaces = NULL;
25175 if (!(complain & tf_error))
25176 return error_mark_node;
25177 if (TYPE_P (target))
25178 error ("ambiguous template instantiation for %q#T", target);
25179 else
25180 error ("ambiguous template instantiation for %q#D", target);
25181 str = ngettext ("candidate is:", "candidates are:", list_length (list));
25182 for (t = list; t; t = TREE_CHAIN (t))
25183 {
25184 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
25185 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
25186 "%s %#qS", spaces ? spaces : str, subst);
25187 spaces = spaces ? spaces : get_spaces (str);
25188 }
25189 free (spaces);
25190 return error_mark_node;
25191 }
25192
25193 return champ;
25194 }
25195
25196 /* Explicitly instantiate DECL. */
25197
25198 void
25199 do_decl_instantiation (tree decl, tree storage)
25200 {
25201 tree result = NULL_TREE;
25202 int extern_p = 0;
25203
25204 if (!decl || decl == error_mark_node)
25205 /* An error occurred, for which grokdeclarator has already issued
25206 an appropriate message. */
25207 return;
25208 else if (! DECL_LANG_SPECIFIC (decl))
25209 {
25210 error ("explicit instantiation of non-template %q#D", decl);
25211 return;
25212 }
25213 else if (DECL_DECLARED_CONCEPT_P (decl))
25214 {
25215 if (VAR_P (decl))
25216 error ("explicit instantiation of variable concept %q#D", decl);
25217 else
25218 error ("explicit instantiation of function concept %q#D", decl);
25219 return;
25220 }
25221
25222 bool var_templ = (DECL_TEMPLATE_INFO (decl)
25223 && variable_template_p (DECL_TI_TEMPLATE (decl)));
25224
25225 if (VAR_P (decl) && !var_templ)
25226 {
25227 /* There is an asymmetry here in the way VAR_DECLs and
25228 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25229 the latter, the DECL we get back will be marked as a
25230 template instantiation, and the appropriate
25231 DECL_TEMPLATE_INFO will be set up. This does not happen for
25232 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25233 should handle VAR_DECLs as it currently handles
25234 FUNCTION_DECLs. */
25235 if (!DECL_CLASS_SCOPE_P (decl))
25236 {
25237 error ("%qD is not a static data member of a class template", decl);
25238 return;
25239 }
25240 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
25241 if (!result || !VAR_P (result))
25242 {
25243 error ("no matching template for %qD found", decl);
25244 return;
25245 }
25246 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25247 {
25248 error ("type %qT for explicit instantiation %qD does not match "
25249 "declared type %qT", TREE_TYPE (result), decl,
25250 TREE_TYPE (decl));
25251 return;
25252 }
25253 }
25254 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25255 {
25256 error ("explicit instantiation of %q#D", decl);
25257 return;
25258 }
25259 else
25260 result = decl;
25261
25262 /* Check for various error cases. Note that if the explicit
25263 instantiation is valid the RESULT will currently be marked as an
25264 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25265 until we get here. */
25266
25267 if (DECL_TEMPLATE_SPECIALIZATION (result))
25268 {
25269 /* DR 259 [temp.spec].
25270
25271 Both an explicit instantiation and a declaration of an explicit
25272 specialization shall not appear in a program unless the explicit
25273 instantiation follows a declaration of the explicit specialization.
25274
25275 For a given set of template parameters, if an explicit
25276 instantiation of a template appears after a declaration of an
25277 explicit specialization for that template, the explicit
25278 instantiation has no effect. */
25279 return;
25280 }
25281 else if (DECL_EXPLICIT_INSTANTIATION (result))
25282 {
25283 /* [temp.spec]
25284
25285 No program shall explicitly instantiate any template more
25286 than once.
25287
25288 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25289 the first instantiation was `extern' and the second is not,
25290 and EXTERN_P for the opposite case. */
25291 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25292 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25293 /* If an "extern" explicit instantiation follows an ordinary
25294 explicit instantiation, the template is instantiated. */
25295 if (extern_p)
25296 return;
25297 }
25298 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25299 {
25300 error ("no matching template for %qD found", result);
25301 return;
25302 }
25303 else if (!DECL_TEMPLATE_INFO (result))
25304 {
25305 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25306 return;
25307 }
25308
25309 if (storage == NULL_TREE)
25310 ;
25311 else if (storage == ridpointers[(int) RID_EXTERN])
25312 {
25313 if (cxx_dialect == cxx98)
25314 pedwarn (input_location, OPT_Wpedantic,
25315 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25316 "instantiations");
25317 extern_p = 1;
25318 }
25319 else
25320 error ("storage class %qD applied to template instantiation", storage);
25321
25322 check_explicit_instantiation_namespace (result);
25323 mark_decl_instantiated (result, extern_p);
25324 if (! extern_p)
25325 instantiate_decl (result, /*defer_ok=*/true,
25326 /*expl_inst_class_mem_p=*/false);
25327 }
25328
25329 static void
25330 mark_class_instantiated (tree t, int extern_p)
25331 {
25332 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25333 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25334 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25335 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25336 if (! extern_p)
25337 {
25338 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25339 rest_of_type_compilation (t, 1);
25340 }
25341 }
25342
25343 /* Perform an explicit instantiation of template class T. STORAGE, if
25344 non-null, is the RID for extern, inline or static. COMPLAIN is
25345 nonzero if this is called from the parser, zero if called recursively,
25346 since the standard is unclear (as detailed below). */
25347
25348 void
25349 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25350 {
25351 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25352 {
25353 if (tree ti = TYPE_TEMPLATE_INFO (t))
25354 error ("explicit instantiation of non-class template %qD",
25355 TI_TEMPLATE (ti));
25356 else
25357 error ("explicit instantiation of non-template type %qT", t);
25358 return;
25359 }
25360
25361 complete_type (t);
25362
25363 if (!COMPLETE_TYPE_P (t))
25364 {
25365 if (complain & tf_error)
25366 error ("explicit instantiation of %q#T before definition of template",
25367 t);
25368 return;
25369 }
25370
25371 /* At most one of these will be true. */
25372 bool extern_p = false;
25373 bool nomem_p = false;
25374 bool static_p = false;
25375
25376 if (storage != NULL_TREE)
25377 {
25378 if (storage == ridpointers[(int) RID_EXTERN])
25379 {
25380 if (cxx_dialect == cxx98)
25381 pedwarn (input_location, OPT_Wpedantic,
25382 "ISO C++ 1998 forbids the use of %<extern%> on "
25383 "explicit instantiations");
25384 }
25385 else
25386 pedwarn (input_location, OPT_Wpedantic,
25387 "ISO C++ forbids the use of %qE"
25388 " on explicit instantiations", storage);
25389
25390 if (storage == ridpointers[(int) RID_INLINE])
25391 nomem_p = true;
25392 else if (storage == ridpointers[(int) RID_EXTERN])
25393 extern_p = true;
25394 else if (storage == ridpointers[(int) RID_STATIC])
25395 static_p = true;
25396 else
25397 error ("storage class %qD applied to template instantiation",
25398 storage);
25399 }
25400
25401 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25402 /* DR 259 [temp.spec].
25403
25404 Both an explicit instantiation and a declaration of an explicit
25405 specialization shall not appear in a program unless the
25406 explicit instantiation follows a declaration of the explicit
25407 specialization.
25408
25409 For a given set of template parameters, if an explicit
25410 instantiation of a template appears after a declaration of an
25411 explicit specialization for that template, the explicit
25412 instantiation has no effect. */
25413 return;
25414
25415 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25416 {
25417 /* We've already instantiated the template. */
25418
25419 /* [temp.spec]
25420
25421 No program shall explicitly instantiate any template more
25422 than once.
25423
25424 If EXTERN_P then this is ok. */
25425 if (!extern_p && (complain & tf_error))
25426 permerror (input_location,
25427 "duplicate explicit instantiation of %q#T", t);
25428
25429 return;
25430 }
25431
25432 check_explicit_instantiation_namespace (TYPE_NAME (t));
25433 mark_class_instantiated (t, extern_p);
25434
25435 if (nomem_p)
25436 return;
25437
25438 /* In contrast to implicit instantiation, where only the
25439 declarations, and not the definitions, of members are
25440 instantiated, we have here:
25441
25442 [temp.explicit]
25443
25444 An explicit instantiation that names a class template
25445 specialization is also an explicit instantiation of the same
25446 kind (declaration or definition) of each of its members (not
25447 including members inherited from base classes and members
25448 that are templates) that has not been previously explicitly
25449 specialized in the translation unit containing the explicit
25450 instantiation, provided that the associated constraints, if
25451 any, of that member are satisfied by the template arguments
25452 of the explicit instantiation. */
25453 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25454 if ((VAR_P (fld)
25455 || (TREE_CODE (fld) == FUNCTION_DECL
25456 && !static_p
25457 && user_provided_p (fld)))
25458 && DECL_TEMPLATE_INSTANTIATION (fld)
25459 && constraints_satisfied_p (fld))
25460 {
25461 mark_decl_instantiated (fld, extern_p);
25462 if (! extern_p)
25463 instantiate_decl (fld, /*defer_ok=*/true,
25464 /*expl_inst_class_mem_p=*/true);
25465 }
25466 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25467 {
25468 tree type = TREE_TYPE (fld);
25469
25470 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25471 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25472 do_type_instantiation (type, storage, 0);
25473 }
25474 }
25475
25476 /* Given a function DECL, which is a specialization of TMPL, modify
25477 DECL to be a re-instantiation of TMPL with the same template
25478 arguments. TMPL should be the template into which tsubst'ing
25479 should occur for DECL, not the most general template.
25480
25481 One reason for doing this is a scenario like this:
25482
25483 template <class T>
25484 void f(const T&, int i);
25485
25486 void g() { f(3, 7); }
25487
25488 template <class T>
25489 void f(const T& t, const int i) { }
25490
25491 Note that when the template is first instantiated, with
25492 instantiate_template, the resulting DECL will have no name for the
25493 first parameter, and the wrong type for the second. So, when we go
25494 to instantiate the DECL, we regenerate it. */
25495
25496 static void
25497 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25498 {
25499 /* The arguments used to instantiate DECL, from the most general
25500 template. */
25501 tree code_pattern;
25502
25503 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25504
25505 /* Make sure that we can see identifiers, and compute access
25506 correctly. */
25507 push_access_scope (decl);
25508
25509 if (TREE_CODE (decl) == FUNCTION_DECL)
25510 {
25511 tree specs;
25512 int args_depth;
25513 int parms_depth;
25514
25515 args_depth = TMPL_ARGS_DEPTH (args);
25516 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25517 if (args_depth > parms_depth)
25518 args = get_innermost_template_args (args, parms_depth);
25519
25520 /* Instantiate a dynamic exception-specification. noexcept will be
25521 handled below. */
25522 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25523 if (TREE_VALUE (raises))
25524 {
25525 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25526 args, tf_error, NULL_TREE,
25527 /*defer_ok*/false);
25528 if (specs && specs != error_mark_node)
25529 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25530 specs);
25531 }
25532
25533 /* Merge parameter declarations. */
25534 if (tree pattern_parm
25535 = skip_artificial_parms_for (code_pattern,
25536 DECL_ARGUMENTS (code_pattern)))
25537 {
25538 tree *p = &DECL_ARGUMENTS (decl);
25539 for (int skip = num_artificial_parms_for (decl); skip; --skip)
25540 p = &DECL_CHAIN (*p);
25541 *p = tsubst_decl (pattern_parm, args, tf_error);
25542 for (tree t = *p; t; t = DECL_CHAIN (t))
25543 DECL_CONTEXT (t) = decl;
25544 }
25545
25546 /* Merge additional specifiers from the CODE_PATTERN. */
25547 if (DECL_DECLARED_INLINE_P (code_pattern)
25548 && !DECL_DECLARED_INLINE_P (decl))
25549 DECL_DECLARED_INLINE_P (decl) = 1;
25550
25551 maybe_instantiate_noexcept (decl, tf_error);
25552 }
25553 else if (VAR_P (decl))
25554 {
25555 start_lambda_scope (decl);
25556 DECL_INITIAL (decl) =
25557 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25558 tf_error, DECL_TI_TEMPLATE (decl));
25559 finish_lambda_scope ();
25560 if (VAR_HAD_UNKNOWN_BOUND (decl))
25561 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25562 tf_error, DECL_TI_TEMPLATE (decl));
25563 }
25564 else
25565 gcc_unreachable ();
25566
25567 pop_access_scope (decl);
25568 }
25569
25570 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25571 substituted to get DECL. */
25572
25573 tree
25574 template_for_substitution (tree decl)
25575 {
25576 tree tmpl = DECL_TI_TEMPLATE (decl);
25577
25578 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25579 for the instantiation. This is not always the most general
25580 template. Consider, for example:
25581
25582 template <class T>
25583 struct S { template <class U> void f();
25584 template <> void f<int>(); };
25585
25586 and an instantiation of S<double>::f<int>. We want TD to be the
25587 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25588 while (/* An instantiation cannot have a definition, so we need a
25589 more general template. */
25590 DECL_TEMPLATE_INSTANTIATION (tmpl)
25591 /* We must also deal with friend templates. Given:
25592
25593 template <class T> struct S {
25594 template <class U> friend void f() {};
25595 };
25596
25597 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25598 so far as the language is concerned, but that's still
25599 where we get the pattern for the instantiation from. On
25600 other hand, if the definition comes outside the class, say:
25601
25602 template <class T> struct S {
25603 template <class U> friend void f();
25604 };
25605 template <class U> friend void f() {}
25606
25607 we don't need to look any further. That's what the check for
25608 DECL_INITIAL is for. */
25609 || (TREE_CODE (decl) == FUNCTION_DECL
25610 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25611 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25612 {
25613 /* The present template, TD, should not be a definition. If it
25614 were a definition, we should be using it! Note that we
25615 cannot restructure the loop to just keep going until we find
25616 a template with a definition, since that might go too far if
25617 a specialization was declared, but not defined. */
25618
25619 /* Fetch the more general template. */
25620 tmpl = DECL_TI_TEMPLATE (tmpl);
25621 }
25622
25623 return tmpl;
25624 }
25625
25626 /* Returns true if we need to instantiate this template instance even if we
25627 know we aren't going to emit it. */
25628
25629 bool
25630 always_instantiate_p (tree decl)
25631 {
25632 /* We always instantiate inline functions so that we can inline them. An
25633 explicit instantiation declaration prohibits implicit instantiation of
25634 non-inline functions. With high levels of optimization, we would
25635 normally inline non-inline functions -- but we're not allowed to do
25636 that for "extern template" functions. Therefore, we check
25637 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25638 return ((TREE_CODE (decl) == FUNCTION_DECL
25639 && (DECL_DECLARED_INLINE_P (decl)
25640 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25641 /* And we need to instantiate static data members so that
25642 their initializers are available in integral constant
25643 expressions. */
25644 || (VAR_P (decl)
25645 && decl_maybe_constant_var_p (decl)));
25646 }
25647
25648 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25649 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25650 error, true otherwise. */
25651
25652 bool
25653 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25654 {
25655 if (fn == error_mark_node)
25656 return false;
25657
25658 /* Don't instantiate a noexcept-specification from template context. */
25659 if (processing_template_decl
25660 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25661 return true;
25662
25663 if (DECL_MAYBE_DELETED (fn))
25664 {
25665 if (fn == current_function_decl)
25666 /* We're in start_preparsed_function, keep going. */
25667 return true;
25668
25669 ++function_depth;
25670 synthesize_method (fn);
25671 --function_depth;
25672 return !DECL_MAYBE_DELETED (fn);
25673 }
25674
25675 tree fntype = TREE_TYPE (fn);
25676 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
25677
25678 if (!spec || !TREE_PURPOSE (spec))
25679 return true;
25680
25681 tree noex = TREE_PURPOSE (spec);
25682 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25683 && TREE_CODE (noex) != DEFERRED_PARSE)
25684 return true;
25685
25686 tree orig_fn = NULL_TREE;
25687 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25688 its FUNCTION_DECL for the rest of this function -- push_access_scope
25689 doesn't accept TEMPLATE_DECLs. */
25690 if (DECL_FUNCTION_TEMPLATE_P (fn))
25691 {
25692 orig_fn = fn;
25693 fn = DECL_TEMPLATE_RESULT (fn);
25694 }
25695
25696 if (DECL_CLONED_FUNCTION_P (fn))
25697 {
25698 tree prime = DECL_CLONED_FUNCTION (fn);
25699 if (!maybe_instantiate_noexcept (prime, complain))
25700 return false;
25701 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25702 }
25703 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25704 {
25705 static hash_set<tree>* fns = new hash_set<tree>;
25706 bool added = false;
25707 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25708 {
25709 spec = get_defaulted_eh_spec (fn, complain);
25710 if (spec == error_mark_node)
25711 /* This might have failed because of an unparsed DMI, so
25712 let's try again later. */
25713 return false;
25714 }
25715 else if (!(added = !fns->add (fn)))
25716 {
25717 /* If hash_set::add returns true, the element was already there. */
25718 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25719 DECL_SOURCE_LOCATION (fn));
25720 error_at (loc,
25721 "exception specification of %qD depends on itself",
25722 fn);
25723 spec = noexcept_false_spec;
25724 }
25725 else if (push_tinst_level (fn))
25726 {
25727 push_to_top_level ();
25728 push_access_scope (fn);
25729 push_deferring_access_checks (dk_no_deferred);
25730 input_location = DECL_SOURCE_LOCATION (fn);
25731
25732 if (!DECL_LOCAL_DECL_P (fn))
25733 {
25734 /* If needed, set current_class_ptr for the benefit of
25735 tsubst_copy/PARM_DECL. The exception pattern will
25736 refer to the parm of the template, not the
25737 instantiation. */
25738 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25739 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25740 {
25741 tree this_parm = DECL_ARGUMENTS (tdecl);
25742 current_class_ptr = NULL_TREE;
25743 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25744 current_class_ptr = this_parm;
25745 }
25746 }
25747
25748 /* If this function is represented by a TEMPLATE_DECL, then
25749 the deferred noexcept-specification might still contain
25750 dependent types, even after substitution. And we need the
25751 dependency check functions to work in build_noexcept_spec. */
25752 if (orig_fn)
25753 ++processing_template_decl;
25754
25755 /* Do deferred instantiation of the noexcept-specifier. */
25756 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25757 DEFERRED_NOEXCEPT_ARGS (noex),
25758 tf_warning_or_error, fn,
25759 /*function_p=*/false,
25760 /*i_c_e_p=*/true);
25761
25762 /* Build up the noexcept-specification. */
25763 spec = build_noexcept_spec (noex, tf_warning_or_error);
25764
25765 if (orig_fn)
25766 --processing_template_decl;
25767
25768 pop_deferring_access_checks ();
25769 pop_access_scope (fn);
25770 pop_tinst_level ();
25771 pop_from_top_level ();
25772 }
25773 else
25774 spec = noexcept_false_spec;
25775
25776 if (added)
25777 fns->remove (fn);
25778 }
25779
25780 if (spec == error_mark_node)
25781 {
25782 /* This failed with a hard error, so let's go with false. */
25783 gcc_assert (seen_error ());
25784 spec = noexcept_false_spec;
25785 }
25786
25787 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25788 if (orig_fn)
25789 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25790
25791 return true;
25792 }
25793
25794 /* We're starting to process the function INST, an instantiation of PATTERN;
25795 add their parameters to local_specializations. */
25796
25797 static void
25798 register_parameter_specializations (tree pattern, tree inst)
25799 {
25800 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25801 tree spec_parm = DECL_ARGUMENTS (inst);
25802 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25803 {
25804 register_local_specialization (spec_parm, tmpl_parm);
25805 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25806 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25807 }
25808 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25809 {
25810 if (!DECL_PACK_P (tmpl_parm))
25811 {
25812 register_local_specialization (spec_parm, tmpl_parm);
25813 spec_parm = DECL_CHAIN (spec_parm);
25814 }
25815 else
25816 {
25817 /* Register the (value) argument pack as a specialization of
25818 TMPL_PARM, then move on. */
25819 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25820 register_local_specialization (argpack, tmpl_parm);
25821 }
25822 }
25823 gcc_assert (!spec_parm);
25824 }
25825
25826 /* Instantiate the body of D using PATTERN with ARGS. We have
25827 already determined PATTERN is the correct template to use.
25828 NESTED_P is true if this is a nested function, in which case
25829 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25830
25831 static void
25832 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25833 {
25834 tree td = NULL_TREE;
25835 tree code_pattern = pattern;
25836
25837 if (!nested_p)
25838 {
25839 td = pattern;
25840 code_pattern = DECL_TEMPLATE_RESULT (td);
25841 }
25842 else
25843 /* Only OMP reductions are nested. */
25844 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25845
25846 vec<tree> omp_privatization_save;
25847 if (current_function_decl)
25848 save_omp_privatization_clauses (omp_privatization_save);
25849
25850 bool push_to_top
25851 = !(current_function_decl
25852 && !LAMBDA_FUNCTION_P (d)
25853 && decl_function_context (d) == current_function_decl);
25854
25855 if (push_to_top)
25856 push_to_top_level ();
25857 else
25858 {
25859 gcc_assert (!processing_template_decl);
25860 push_function_context ();
25861 cp_unevaluated_operand = 0;
25862 c_inhibit_evaluation_warnings = 0;
25863 }
25864
25865 if (VAR_P (d))
25866 {
25867 /* The variable might be a lambda's extra scope, and that
25868 lambda's visibility depends on D's. */
25869 maybe_commonize_var (d);
25870 determine_visibility (d);
25871 }
25872
25873 /* Mark D as instantiated so that recursive calls to
25874 instantiate_decl do not try to instantiate it again. */
25875 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25876
25877 if (td)
25878 /* Regenerate the declaration in case the template has been modified
25879 by a subsequent redeclaration. */
25880 regenerate_decl_from_template (d, td, args);
25881
25882 /* We already set the file and line above. Reset them now in case
25883 they changed as a result of calling regenerate_decl_from_template. */
25884 input_location = DECL_SOURCE_LOCATION (d);
25885
25886 if (VAR_P (d))
25887 {
25888 /* Clear out DECL_RTL; whatever was there before may not be right
25889 since we've reset the type of the declaration. */
25890 SET_DECL_RTL (d, NULL);
25891 DECL_IN_AGGR_P (d) = 0;
25892
25893 /* The initializer is placed in DECL_INITIAL by
25894 regenerate_decl_from_template so we don't need to
25895 push/pop_access_scope again here. Pull it out so that
25896 cp_finish_decl can process it. */
25897 bool const_init = false;
25898 tree init = DECL_INITIAL (d);
25899 DECL_INITIAL (d) = NULL_TREE;
25900 DECL_INITIALIZED_P (d) = 0;
25901
25902 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25903 initializer. That function will defer actual emission until
25904 we have a chance to determine linkage. */
25905 DECL_EXTERNAL (d) = 0;
25906
25907 /* Enter the scope of D so that access-checking works correctly. */
25908 bool enter_context = DECL_CLASS_SCOPE_P (d);
25909 if (enter_context)
25910 push_nested_class (DECL_CONTEXT (d));
25911
25912 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25913 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25914
25915 if (enter_context)
25916 pop_nested_class ();
25917 }
25918 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25919 synthesize_method (d);
25920 else if (TREE_CODE (d) == FUNCTION_DECL)
25921 {
25922 /* Set up the list of local specializations. */
25923 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25924 tree block = NULL_TREE;
25925
25926 /* Set up context. */
25927 if (nested_p)
25928 block = push_stmt_list ();
25929 else
25930 {
25931 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25932
25933 perform_instantiation_time_access_checks (code_pattern, args);
25934 }
25935
25936 /* Create substitution entries for the parameters. */
25937 register_parameter_specializations (code_pattern, d);
25938
25939 /* Substitute into the body of the function. */
25940 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25941 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25942 tf_warning_or_error, d);
25943 else
25944 {
25945 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25946 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25947 /*integral_constant_expression_p=*/false);
25948
25949 /* Set the current input_location to the end of the function
25950 so that finish_function knows where we are. */
25951 input_location
25952 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25953
25954 /* Remember if we saw an infinite loop in the template. */
25955 current_function_infinite_loop
25956 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25957 }
25958
25959 /* Finish the function. */
25960 if (nested_p)
25961 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25962 else
25963 {
25964 d = finish_function (/*inline_p=*/false);
25965 expand_or_defer_fn (d);
25966 }
25967
25968 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25969 cp_check_omp_declare_reduction (d);
25970 }
25971
25972 /* We're not deferring instantiation any more. */
25973 if (!nested_p)
25974 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25975
25976 if (push_to_top)
25977 pop_from_top_level ();
25978 else
25979 pop_function_context ();
25980
25981 if (current_function_decl)
25982 restore_omp_privatization_clauses (omp_privatization_save);
25983 }
25984
25985 /* Produce the definition of D, a _DECL generated from a template. If
25986 DEFER_OK is true, then we don't have to actually do the
25987 instantiation now; we just have to do it sometime. Normally it is
25988 an error if this is an explicit instantiation but D is undefined.
25989 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25990 instantiated class template. */
25991
25992 tree
25993 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25994 {
25995 tree tmpl = DECL_TI_TEMPLATE (d);
25996 tree gen_args;
25997 tree args;
25998 tree td;
25999 tree code_pattern;
26000 tree spec;
26001 tree gen_tmpl;
26002 bool pattern_defined;
26003 location_t saved_loc = input_location;
26004 int saved_unevaluated_operand = cp_unevaluated_operand;
26005 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26006 bool external_p;
26007 bool deleted_p;
26008
26009 /* This function should only be used to instantiate templates for
26010 functions and static member variables. */
26011 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
26012
26013 /* A concept is never instantiated. */
26014 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
26015
26016 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
26017
26018 if (modules_p ())
26019 /* We may have a pending instantiation of D itself. */
26020 lazy_load_pendings (d);
26021
26022 /* Variables are never deferred; if instantiation is required, they
26023 are instantiated right away. That allows for better code in the
26024 case that an expression refers to the value of the variable --
26025 if the variable has a constant value the referring expression can
26026 take advantage of that fact. */
26027 if (VAR_P (d))
26028 defer_ok = false;
26029
26030 /* Don't instantiate cloned functions. Instead, instantiate the
26031 functions they cloned. */
26032 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
26033 d = DECL_CLONED_FUNCTION (d);
26034
26035 if (DECL_TEMPLATE_INSTANTIATED (d)
26036 || TREE_TYPE (d) == error_mark_node
26037 || (TREE_CODE (d) == FUNCTION_DECL
26038 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
26039 || DECL_TEMPLATE_SPECIALIZATION (d))
26040 /* D has already been instantiated or explicitly specialized, so
26041 there's nothing for us to do here.
26042
26043 It might seem reasonable to check whether or not D is an explicit
26044 instantiation, and, if so, stop here. But when an explicit
26045 instantiation is deferred until the end of the compilation,
26046 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26047 the instantiation. */
26048 return d;
26049
26050 /* Check to see whether we know that this template will be
26051 instantiated in some other file, as with "extern template"
26052 extension. */
26053 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
26054
26055 /* In general, we do not instantiate such templates. */
26056 if (external_p && !always_instantiate_p (d))
26057 return d;
26058
26059 gen_tmpl = most_general_template (tmpl);
26060 gen_args = DECL_TI_ARGS (d);
26061
26062 /* We should already have the extra args. */
26063 gcc_checking_assert (tmpl == gen_tmpl
26064 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
26065 == TMPL_ARGS_DEPTH (gen_args)));
26066 /* And what's in the hash table should match D. */
26067 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
26068 == d
26069 || spec == NULL_TREE);
26070
26071 /* This needs to happen before any tsubsting. */
26072 if (! push_tinst_level (d))
26073 return d;
26074
26075 timevar_push (TV_TEMPLATE_INST);
26076
26077 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26078 for the instantiation. */
26079 td = template_for_substitution (d);
26080 args = gen_args;
26081
26082 if (VAR_P (d))
26083 {
26084 /* Look up an explicit specialization, if any. */
26085 tree tid = lookup_template_variable (gen_tmpl, gen_args);
26086 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
26087 if (elt && elt != error_mark_node)
26088 {
26089 td = TREE_VALUE (elt);
26090 args = TREE_PURPOSE (elt);
26091 }
26092 }
26093
26094 code_pattern = DECL_TEMPLATE_RESULT (td);
26095
26096 /* We should never be trying to instantiate a member of a class
26097 template or partial specialization. */
26098 gcc_assert (d != code_pattern);
26099
26100 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
26101 || DECL_TEMPLATE_SPECIALIZATION (td))
26102 /* In the case of a friend template whose definition is provided
26103 outside the class, we may have too many arguments. Drop the
26104 ones we don't need. The same is true for specializations. */
26105 args = get_innermost_template_args
26106 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
26107
26108 if (TREE_CODE (d) == FUNCTION_DECL)
26109 {
26110 deleted_p = DECL_DELETED_FN (code_pattern);
26111 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
26112 && DECL_INITIAL (code_pattern) != error_mark_node)
26113 || DECL_DEFAULTED_FN (code_pattern)
26114 || deleted_p);
26115 }
26116 else
26117 {
26118 deleted_p = false;
26119 if (DECL_CLASS_SCOPE_P (code_pattern))
26120 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
26121 else
26122 pattern_defined = ! DECL_EXTERNAL (code_pattern);
26123 }
26124
26125 /* We may be in the middle of deferred access check. Disable it now. */
26126 push_deferring_access_checks (dk_no_deferred);
26127
26128 /* Unless an explicit instantiation directive has already determined
26129 the linkage of D, remember that a definition is available for
26130 this entity. */
26131 if (pattern_defined
26132 && !DECL_INTERFACE_KNOWN (d)
26133 && !DECL_NOT_REALLY_EXTERN (d))
26134 mark_definable (d);
26135
26136 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
26137 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
26138 input_location = DECL_SOURCE_LOCATION (d);
26139
26140 /* If D is a member of an explicitly instantiated class template,
26141 and no definition is available, treat it like an implicit
26142 instantiation. */
26143 if (!pattern_defined && expl_inst_class_mem_p
26144 && DECL_EXPLICIT_INSTANTIATION (d))
26145 {
26146 /* Leave linkage flags alone on instantiations with anonymous
26147 visibility. */
26148 if (TREE_PUBLIC (d))
26149 {
26150 DECL_NOT_REALLY_EXTERN (d) = 0;
26151 DECL_INTERFACE_KNOWN (d) = 0;
26152 }
26153 SET_DECL_IMPLICIT_INSTANTIATION (d);
26154 }
26155
26156 /* Defer all other templates, unless we have been explicitly
26157 forbidden from doing so. */
26158 if (/* If there is no definition, we cannot instantiate the
26159 template. */
26160 ! pattern_defined
26161 /* If it's OK to postpone instantiation, do so. */
26162 || defer_ok
26163 /* If this is a static data member that will be defined
26164 elsewhere, we don't want to instantiate the entire data
26165 member, but we do want to instantiate the initializer so that
26166 we can substitute that elsewhere. */
26167 || (external_p && VAR_P (d))
26168 /* Handle here a deleted function too, avoid generating
26169 its body (c++/61080). */
26170 || deleted_p)
26171 {
26172 /* The definition of the static data member is now required so
26173 we must substitute the initializer. */
26174 if (VAR_P (d)
26175 && !DECL_INITIAL (d)
26176 && DECL_INITIAL (code_pattern))
26177 {
26178 tree ns;
26179 tree init;
26180 bool const_init = false;
26181 bool enter_context = DECL_CLASS_SCOPE_P (d);
26182
26183 ns = decl_namespace_context (d);
26184 push_nested_namespace (ns);
26185 if (enter_context)
26186 push_nested_class (DECL_CONTEXT (d));
26187 init = tsubst_expr (DECL_INITIAL (code_pattern),
26188 args,
26189 tf_warning_or_error, NULL_TREE,
26190 /*integral_constant_expression_p=*/false);
26191 /* If instantiating the initializer involved instantiating this
26192 again, don't call cp_finish_decl twice. */
26193 if (!DECL_INITIAL (d))
26194 {
26195 /* Make sure the initializer is still constant, in case of
26196 circular dependency (template/instantiate6.C). */
26197 const_init
26198 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26199 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26200 /*asmspec_tree=*/NULL_TREE,
26201 LOOKUP_ONLYCONVERTING);
26202 }
26203 if (enter_context)
26204 pop_nested_class ();
26205 pop_nested_namespace (ns);
26206 }
26207
26208 /* We restore the source position here because it's used by
26209 add_pending_template. */
26210 input_location = saved_loc;
26211
26212 if (at_eof && !pattern_defined
26213 && DECL_EXPLICIT_INSTANTIATION (d)
26214 && DECL_NOT_REALLY_EXTERN (d))
26215 /* [temp.explicit]
26216
26217 The definition of a non-exported function template, a
26218 non-exported member function template, or a non-exported
26219 member function or static data member of a class template
26220 shall be present in every translation unit in which it is
26221 explicitly instantiated. */
26222 permerror (input_location, "explicit instantiation of %qD "
26223 "but no definition available", d);
26224
26225 /* If we're in unevaluated context, we just wanted to get the
26226 constant value; this isn't an odr use, so don't queue
26227 a full instantiation. */
26228 if (!cp_unevaluated_operand
26229 /* ??? Historically, we have instantiated inline functions, even
26230 when marked as "extern template". */
26231 && !(external_p && VAR_P (d)))
26232 add_pending_template (d);
26233 }
26234 else
26235 {
26236 set_instantiating_module (d);
26237 if (variable_template_p (gen_tmpl))
26238 note_variable_template_instantiation (d);
26239 instantiate_body (td, args, d, false);
26240 }
26241
26242 pop_deferring_access_checks ();
26243 timevar_pop (TV_TEMPLATE_INST);
26244 pop_tinst_level ();
26245 input_location = saved_loc;
26246 cp_unevaluated_operand = saved_unevaluated_operand;
26247 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26248
26249 return d;
26250 }
26251
26252 /* Run through the list of templates that we wish we could
26253 instantiate, and instantiate any we can. RETRIES is the
26254 number of times we retry pending template instantiation. */
26255
26256 void
26257 instantiate_pending_templates (int retries)
26258 {
26259 int reconsider;
26260 location_t saved_loc = input_location;
26261
26262 /* Instantiating templates may trigger vtable generation. This in turn
26263 may require further template instantiations. We place a limit here
26264 to avoid infinite loop. */
26265 if (pending_templates && retries >= max_tinst_depth)
26266 {
26267 tree decl = pending_templates->tinst->maybe_get_node ();
26268
26269 fatal_error (input_location,
26270 "template instantiation depth exceeds maximum of %d"
26271 " instantiating %q+D, possibly from virtual table generation"
26272 " (use %<-ftemplate-depth=%> to increase the maximum)",
26273 max_tinst_depth, decl);
26274 if (TREE_CODE (decl) == FUNCTION_DECL)
26275 /* Pretend that we defined it. */
26276 DECL_INITIAL (decl) = error_mark_node;
26277 return;
26278 }
26279
26280 do
26281 {
26282 struct pending_template **t = &pending_templates;
26283 struct pending_template *last = NULL;
26284 reconsider = 0;
26285 while (*t)
26286 {
26287 tree instantiation = reopen_tinst_level ((*t)->tinst);
26288 bool complete = false;
26289
26290 if (TYPE_P (instantiation))
26291 {
26292 if (!COMPLETE_TYPE_P (instantiation))
26293 {
26294 instantiate_class_template (instantiation);
26295 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26296 for (tree fld = TYPE_FIELDS (instantiation);
26297 fld; fld = TREE_CHAIN (fld))
26298 if ((VAR_P (fld)
26299 || (TREE_CODE (fld) == FUNCTION_DECL
26300 && !DECL_ARTIFICIAL (fld)))
26301 && DECL_TEMPLATE_INSTANTIATION (fld))
26302 instantiate_decl (fld,
26303 /*defer_ok=*/false,
26304 /*expl_inst_class_mem_p=*/false);
26305
26306 if (COMPLETE_TYPE_P (instantiation))
26307 reconsider = 1;
26308 }
26309
26310 complete = COMPLETE_TYPE_P (instantiation);
26311 }
26312 else
26313 {
26314 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26315 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26316 {
26317 instantiation
26318 = instantiate_decl (instantiation,
26319 /*defer_ok=*/false,
26320 /*expl_inst_class_mem_p=*/false);
26321 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26322 reconsider = 1;
26323 }
26324
26325 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26326 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26327 }
26328
26329 if (complete)
26330 {
26331 /* If INSTANTIATION has been instantiated, then we don't
26332 need to consider it again in the future. */
26333 struct pending_template *drop = *t;
26334 *t = (*t)->next;
26335 set_refcount_ptr (drop->tinst);
26336 pending_template_freelist ().free (drop);
26337 }
26338 else
26339 {
26340 last = *t;
26341 t = &(*t)->next;
26342 }
26343 tinst_depth = 0;
26344 set_refcount_ptr (current_tinst_level);
26345 }
26346 last_pending_template = last;
26347 }
26348 while (reconsider);
26349
26350 input_location = saved_loc;
26351 }
26352
26353 /* Substitute ARGVEC into T, which is a list of initializers for
26354 either base class or a non-static data member. The TREE_PURPOSEs
26355 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26356 instantiate_decl. */
26357
26358 static tree
26359 tsubst_initializer_list (tree t, tree argvec)
26360 {
26361 tree inits = NULL_TREE;
26362 tree target_ctor = error_mark_node;
26363
26364 for (; t; t = TREE_CHAIN (t))
26365 {
26366 tree decl;
26367 tree init;
26368 tree expanded_bases = NULL_TREE;
26369 tree expanded_arguments = NULL_TREE;
26370 int i, len = 1;
26371
26372 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26373 {
26374 tree expr;
26375 tree arg;
26376
26377 /* Expand the base class expansion type into separate base
26378 classes. */
26379 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26380 tf_warning_or_error,
26381 NULL_TREE);
26382 if (expanded_bases == error_mark_node)
26383 continue;
26384
26385 /* We'll be building separate TREE_LISTs of arguments for
26386 each base. */
26387 len = TREE_VEC_LENGTH (expanded_bases);
26388 expanded_arguments = make_tree_vec (len);
26389 for (i = 0; i < len; i++)
26390 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26391
26392 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26393 expand each argument in the TREE_VALUE of t. */
26394 expr = make_node (EXPR_PACK_EXPANSION);
26395 PACK_EXPANSION_LOCAL_P (expr) = true;
26396 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26397 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26398
26399 if (TREE_VALUE (t) == void_type_node)
26400 /* VOID_TYPE_NODE is used to indicate
26401 value-initialization. */
26402 {
26403 for (i = 0; i < len; i++)
26404 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26405 }
26406 else
26407 {
26408 /* Substitute parameter packs into each argument in the
26409 TREE_LIST. */
26410 in_base_initializer = 1;
26411 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26412 {
26413 tree expanded_exprs;
26414
26415 /* Expand the argument. */
26416 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26417 expanded_exprs
26418 = tsubst_pack_expansion (expr, argvec,
26419 tf_warning_or_error,
26420 NULL_TREE);
26421 if (expanded_exprs == error_mark_node)
26422 continue;
26423
26424 /* Prepend each of the expanded expressions to the
26425 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26426 for (i = 0; i < len; i++)
26427 {
26428 TREE_VEC_ELT (expanded_arguments, i) =
26429 tree_cons (NULL_TREE,
26430 TREE_VEC_ELT (expanded_exprs, i),
26431 TREE_VEC_ELT (expanded_arguments, i));
26432 }
26433 }
26434 in_base_initializer = 0;
26435
26436 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26437 since we built them backwards. */
26438 for (i = 0; i < len; i++)
26439 {
26440 TREE_VEC_ELT (expanded_arguments, i) =
26441 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26442 }
26443 }
26444 }
26445
26446 for (i = 0; i < len; ++i)
26447 {
26448 if (expanded_bases)
26449 {
26450 decl = TREE_VEC_ELT (expanded_bases, i);
26451 decl = expand_member_init (decl);
26452 init = TREE_VEC_ELT (expanded_arguments, i);
26453 }
26454 else
26455 {
26456 tree tmp;
26457 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26458 tf_warning_or_error, NULL_TREE);
26459
26460 decl = expand_member_init (decl);
26461 if (decl && !DECL_P (decl))
26462 in_base_initializer = 1;
26463
26464 init = TREE_VALUE (t);
26465 tmp = init;
26466 if (init != void_type_node)
26467 init = tsubst_expr (init, argvec,
26468 tf_warning_or_error, NULL_TREE,
26469 /*integral_constant_expression_p=*/false);
26470 if (init == NULL_TREE && tmp != NULL_TREE)
26471 /* If we had an initializer but it instantiated to nothing,
26472 value-initialize the object. This will only occur when
26473 the initializer was a pack expansion where the parameter
26474 packs used in that expansion were of length zero. */
26475 init = void_type_node;
26476 in_base_initializer = 0;
26477 }
26478
26479 if (target_ctor != error_mark_node
26480 && init != error_mark_node)
26481 {
26482 error ("mem-initializer for %qD follows constructor delegation",
26483 decl);
26484 return inits;
26485 }
26486 /* Look for a target constructor. */
26487 if (init != error_mark_node
26488 && decl && CLASS_TYPE_P (decl)
26489 && same_type_p (decl, current_class_type))
26490 {
26491 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26492 if (inits)
26493 {
26494 error ("constructor delegation follows mem-initializer for %qD",
26495 TREE_PURPOSE (inits));
26496 continue;
26497 }
26498 target_ctor = init;
26499 }
26500
26501 if (decl)
26502 {
26503 init = build_tree_list (decl, init);
26504 /* Carry over the dummy TREE_TYPE node containing the source
26505 location. */
26506 TREE_TYPE (init) = TREE_TYPE (t);
26507 TREE_CHAIN (init) = inits;
26508 inits = init;
26509 }
26510 }
26511 }
26512 return inits;
26513 }
26514
26515 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26516 is the instantiation (which should have been created with
26517 start_enum) and ARGS are the template arguments to use. */
26518
26519 static void
26520 tsubst_enum (tree tag, tree newtag, tree args)
26521 {
26522 tree e;
26523
26524 if (SCOPED_ENUM_P (newtag))
26525 begin_scope (sk_scoped_enum, newtag);
26526
26527 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26528 {
26529 tree value;
26530 tree decl;
26531
26532 decl = TREE_VALUE (e);
26533 /* Note that in a template enum, the TREE_VALUE is the
26534 CONST_DECL, not the corresponding INTEGER_CST. */
26535 value = tsubst_expr (DECL_INITIAL (decl),
26536 args, tf_warning_or_error, NULL_TREE,
26537 /*integral_constant_expression_p=*/true);
26538
26539 /* Give this enumeration constant the correct access. */
26540 set_current_access_from_decl (decl);
26541
26542 /* Actually build the enumerator itself. Here we're assuming that
26543 enumerators can't have dependent attributes. */
26544 build_enumerator (DECL_NAME (decl), value, newtag,
26545 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26546 }
26547
26548 if (SCOPED_ENUM_P (newtag))
26549 finish_scope ();
26550
26551 finish_enum_value_list (newtag);
26552 finish_enum (newtag);
26553
26554 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26555 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26556 }
26557
26558 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26559 its type -- but without substituting the innermost set of template
26560 arguments. So, innermost set of template parameters will appear in
26561 the type. */
26562
26563 tree
26564 get_mostly_instantiated_function_type (tree decl)
26565 {
26566 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26567 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26568 }
26569
26570 /* Return truthvalue if we're processing a template different from
26571 the last one involved in diagnostics. */
26572 bool
26573 problematic_instantiation_changed (void)
26574 {
26575 return current_tinst_level != last_error_tinst_level;
26576 }
26577
26578 /* Remember current template involved in diagnostics. */
26579 void
26580 record_last_problematic_instantiation (void)
26581 {
26582 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26583 }
26584
26585 struct tinst_level *
26586 current_instantiation (void)
26587 {
26588 return current_tinst_level;
26589 }
26590
26591 /* Return TRUE if current_function_decl is being instantiated, false
26592 otherwise. */
26593
26594 bool
26595 instantiating_current_function_p (void)
26596 {
26597 return (current_instantiation ()
26598 && (current_instantiation ()->maybe_get_node ()
26599 == current_function_decl));
26600 }
26601
26602 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26603 type. Return false for ok, true for disallowed. Issue error and
26604 inform messages under control of COMPLAIN. */
26605
26606 static bool
26607 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26608 {
26609 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26610 return false;
26611 else if (TYPE_PTR_P (type))
26612 return false;
26613 else if (TYPE_REF_P (type)
26614 && !TYPE_REF_IS_RVALUE (type))
26615 return false;
26616 else if (TYPE_PTRMEM_P (type))
26617 return false;
26618 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26619 {
26620 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26621 {
26622 if (complain & tf_error)
26623 error ("non-type template parameters of deduced class type only "
26624 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26625 return true;
26626 }
26627 return false;
26628 }
26629 else if (TREE_CODE (type) == TYPENAME_TYPE)
26630 return false;
26631 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26632 return false;
26633 else if (TREE_CODE (type) == NULLPTR_TYPE)
26634 return false;
26635 /* A bound template template parm could later be instantiated to have a valid
26636 nontype parm type via an alias template. */
26637 else if (cxx_dialect >= cxx11
26638 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26639 return false;
26640 else if (TREE_CODE (type) == COMPLEX_TYPE)
26641 /* Fall through. */;
26642 else if (VOID_TYPE_P (type))
26643 /* Fall through. */;
26644 else if (cxx_dialect >= cxx20)
26645 {
26646 if (dependent_type_p (type))
26647 return false;
26648 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26649 return true;
26650 if (structural_type_p (type))
26651 return false;
26652 if (complain & tf_error)
26653 {
26654 auto_diagnostic_group d;
26655 error ("%qT is not a valid type for a template non-type "
26656 "parameter because it is not structural", type);
26657 structural_type_p (type, true);
26658 }
26659 return true;
26660 }
26661 else if (CLASS_TYPE_P (type))
26662 {
26663 if (complain & tf_error)
26664 error ("non-type template parameters of class type only available "
26665 "with %<-std=c++20%> or %<-std=gnu++20%>");
26666 return true;
26667 }
26668
26669 if (complain & tf_error)
26670 {
26671 if (type == error_mark_node)
26672 inform (input_location, "invalid template non-type parameter");
26673 else
26674 error ("%q#T is not a valid type for a template non-type parameter",
26675 type);
26676 }
26677 return true;
26678 }
26679
26680 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
26681
26682 static bool
26683 value_dependent_noexcept_spec_p (tree type)
26684 {
26685 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26686 if (tree noex = TREE_PURPOSE (spec))
26687 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26688 affect overload resolution and treating it as dependent breaks
26689 things. Same for an unparsed noexcept expression. */
26690 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26691 && TREE_CODE (noex) != DEFERRED_PARSE
26692 && value_dependent_expression_p (noex))
26693 return true;
26694
26695 return false;
26696 }
26697
26698 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26699 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26700
26701 static bool
26702 dependent_type_p_r (tree type)
26703 {
26704 tree scope;
26705
26706 /* [temp.dep.type]
26707
26708 A type is dependent if it is:
26709
26710 -- a template parameter. Template template parameters are types
26711 for us (since TYPE_P holds true for them) so we handle
26712 them here. */
26713 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26714 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26715 return true;
26716 /* -- a qualified-id with a nested-name-specifier which contains a
26717 class-name that names a dependent type or whose unqualified-id
26718 names a dependent type. */
26719 if (TREE_CODE (type) == TYPENAME_TYPE)
26720 return true;
26721
26722 /* An alias template specialization can be dependent even if the
26723 resulting type is not. */
26724 if (dependent_alias_template_spec_p (type, nt_transparent))
26725 return true;
26726
26727 /* -- a cv-qualified type where the cv-unqualified type is
26728 dependent.
26729 No code is necessary for this bullet; the code below handles
26730 cv-qualified types, and we don't want to strip aliases with
26731 TYPE_MAIN_VARIANT because of DR 1558. */
26732 /* -- a compound type constructed from any dependent type. */
26733 if (TYPE_PTRMEM_P (type))
26734 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26735 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26736 (type)));
26737 else if (INDIRECT_TYPE_P (type))
26738 return dependent_type_p (TREE_TYPE (type));
26739 else if (FUNC_OR_METHOD_TYPE_P (type))
26740 {
26741 tree arg_type;
26742
26743 if (dependent_type_p (TREE_TYPE (type)))
26744 return true;
26745 for (arg_type = TYPE_ARG_TYPES (type);
26746 arg_type;
26747 arg_type = TREE_CHAIN (arg_type))
26748 if (dependent_type_p (TREE_VALUE (arg_type)))
26749 return true;
26750 if (cxx_dialect >= cxx17
26751 && value_dependent_noexcept_spec_p (type))
26752 /* A value-dependent noexcept-specifier makes the type dependent. */
26753 return true;
26754 return false;
26755 }
26756 /* -- an array type constructed from any dependent type or whose
26757 size is specified by a constant expression that is
26758 value-dependent.
26759
26760 We checked for type- and value-dependence of the bounds in
26761 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26762 if (TREE_CODE (type) == ARRAY_TYPE)
26763 {
26764 if (TYPE_DOMAIN (type)
26765 && dependent_type_p (TYPE_DOMAIN (type)))
26766 return true;
26767 return dependent_type_p (TREE_TYPE (type));
26768 }
26769
26770 /* -- a template-id in which either the template name is a template
26771 parameter ... */
26772 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26773 return true;
26774 /* ... or any of the template arguments is a dependent type or
26775 an expression that is type-dependent or value-dependent. */
26776 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26777 && (any_dependent_template_arguments_p
26778 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26779 return true;
26780
26781 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26782 dependent; if the argument of the `typeof' expression is not
26783 type-dependent, then it should already been have resolved. */
26784 if (TREE_CODE (type) == TYPEOF_TYPE
26785 || TREE_CODE (type) == DECLTYPE_TYPE
26786 || TREE_CODE (type) == UNDERLYING_TYPE)
26787 return true;
26788
26789 /* A template argument pack is dependent if any of its packed
26790 arguments are. */
26791 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26792 {
26793 tree args = ARGUMENT_PACK_ARGS (type);
26794 int i, len = TREE_VEC_LENGTH (args);
26795 for (i = 0; i < len; ++i)
26796 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26797 return true;
26798 }
26799
26800 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26801 be template parameters. */
26802 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26803 return true;
26804
26805 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26806 return true;
26807
26808 /* The standard does not specifically mention types that are local
26809 to template functions or local classes, but they should be
26810 considered dependent too. For example:
26811
26812 template <int I> void f() {
26813 enum E { a = I };
26814 S<sizeof (E)> s;
26815 }
26816
26817 The size of `E' cannot be known until the value of `I' has been
26818 determined. Therefore, `E' must be considered dependent. */
26819 scope = TYPE_CONTEXT (type);
26820 if (scope && TYPE_P (scope))
26821 return dependent_type_p (scope);
26822 /* Don't use type_dependent_expression_p here, as it can lead
26823 to infinite recursion trying to determine whether a lambda
26824 nested in a lambda is dependent (c++/47687). */
26825 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26826 && DECL_LANG_SPECIFIC (scope)
26827 && DECL_TEMPLATE_INFO (scope)
26828 && (any_dependent_template_arguments_p
26829 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26830 return true;
26831
26832 /* Other types are non-dependent. */
26833 return false;
26834 }
26835
26836 /* Returns TRUE if TYPE is dependent, in the sense of
26837 [temp.dep.type]. Note that a NULL type is considered dependent. */
26838
26839 bool
26840 dependent_type_p (tree type)
26841 {
26842 /* If there are no template parameters in scope, then there can't be
26843 any dependent types. */
26844 if (!processing_template_decl)
26845 {
26846 /* If we are not processing a template, then nobody should be
26847 providing us with a dependent type. */
26848 gcc_assert (type);
26849 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26850 return false;
26851 }
26852
26853 /* If the type is NULL, we have not computed a type for the entity
26854 in question; in that case, the type is dependent. */
26855 if (!type)
26856 return true;
26857
26858 /* Erroneous types can be considered non-dependent. */
26859 if (type == error_mark_node)
26860 return false;
26861
26862 /* If we have not already computed the appropriate value for TYPE,
26863 do so now. */
26864 if (!TYPE_DEPENDENT_P_VALID (type))
26865 {
26866 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26867 TYPE_DEPENDENT_P_VALID (type) = 1;
26868 }
26869
26870 return TYPE_DEPENDENT_P (type);
26871 }
26872
26873 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26874 lookup. In other words, a dependent type that is not the current
26875 instantiation. */
26876
26877 bool
26878 dependent_scope_p (tree scope)
26879 {
26880 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26881 && !currently_open_class (scope));
26882 }
26883
26884 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26885 an unknown base of 'this' (and is therefore instantiation-dependent). */
26886
26887 static bool
26888 unknown_base_ref_p (tree t)
26889 {
26890 if (!current_class_ptr)
26891 return false;
26892
26893 tree mem = TREE_OPERAND (t, 1);
26894 if (shared_member_p (mem))
26895 return false;
26896
26897 tree cur = current_nonlambda_class_type ();
26898 if (!any_dependent_bases_p (cur))
26899 return false;
26900
26901 tree ctx = TREE_OPERAND (t, 0);
26902 if (DERIVED_FROM_P (ctx, cur))
26903 return false;
26904
26905 return true;
26906 }
26907
26908 /* T is a SCOPE_REF; return whether we need to consider it
26909 instantiation-dependent so that we can check access at instantiation
26910 time even though we know which member it resolves to. */
26911
26912 static bool
26913 instantiation_dependent_scope_ref_p (tree t)
26914 {
26915 if (DECL_P (TREE_OPERAND (t, 1))
26916 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26917 && !unknown_base_ref_p (t)
26918 && accessible_in_template_p (TREE_OPERAND (t, 0),
26919 TREE_OPERAND (t, 1)))
26920 return false;
26921 else
26922 return true;
26923 }
26924
26925 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26926 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26927 expression. */
26928
26929 /* Note that this predicate is not appropriate for general expressions;
26930 only constant expressions (that satisfy potential_constant_expression)
26931 can be tested for value dependence. */
26932
26933 bool
26934 value_dependent_expression_p (tree expression)
26935 {
26936 if (!processing_template_decl || expression == NULL_TREE)
26937 return false;
26938
26939 /* A type-dependent expression is also value-dependent. */
26940 if (type_dependent_expression_p (expression))
26941 return true;
26942
26943 switch (TREE_CODE (expression))
26944 {
26945 case BASELINK:
26946 /* A dependent member function of the current instantiation. */
26947 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26948
26949 case FUNCTION_DECL:
26950 /* A dependent member function of the current instantiation. */
26951 if (DECL_CLASS_SCOPE_P (expression)
26952 && dependent_type_p (DECL_CONTEXT (expression)))
26953 return true;
26954 break;
26955
26956 case IDENTIFIER_NODE:
26957 /* A name that has not been looked up -- must be dependent. */
26958 return true;
26959
26960 case TEMPLATE_PARM_INDEX:
26961 /* A non-type template parm. */
26962 return true;
26963
26964 case CONST_DECL:
26965 /* A non-type template parm. */
26966 if (DECL_TEMPLATE_PARM_P (expression))
26967 return true;
26968 return value_dependent_expression_p (DECL_INITIAL (expression));
26969
26970 case VAR_DECL:
26971 /* A constant with literal type and is initialized
26972 with an expression that is value-dependent. */
26973 if (DECL_DEPENDENT_INIT_P (expression)
26974 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26975 || TYPE_REF_P (TREE_TYPE (expression)))
26976 return true;
26977 if (DECL_HAS_VALUE_EXPR_P (expression))
26978 {
26979 tree value_expr = DECL_VALUE_EXPR (expression);
26980 if (value_dependent_expression_p (value_expr)
26981 /* __PRETTY_FUNCTION__ inside a template function is dependent
26982 on the name of the function. */
26983 || (DECL_PRETTY_FUNCTION_P (expression)
26984 /* It might be used in a template, but not a template
26985 function, in which case its DECL_VALUE_EXPR will be
26986 "top level". */
26987 && value_expr == error_mark_node))
26988 return true;
26989 }
26990 return false;
26991
26992 case DYNAMIC_CAST_EXPR:
26993 case STATIC_CAST_EXPR:
26994 case CONST_CAST_EXPR:
26995 case REINTERPRET_CAST_EXPR:
26996 case CAST_EXPR:
26997 case IMPLICIT_CONV_EXPR:
26998 /* These expressions are value-dependent if the type to which
26999 the cast occurs is dependent or the expression being casted
27000 is value-dependent. */
27001 {
27002 tree type = TREE_TYPE (expression);
27003
27004 if (dependent_type_p (type))
27005 return true;
27006
27007 /* A functional cast has a list of operands. */
27008 expression = TREE_OPERAND (expression, 0);
27009 if (!expression)
27010 {
27011 /* If there are no operands, it must be an expression such
27012 as "int()". This should not happen for aggregate types
27013 because it would form non-constant expressions. */
27014 gcc_assert (cxx_dialect >= cxx11
27015 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
27016
27017 return false;
27018 }
27019
27020 if (TREE_CODE (expression) == TREE_LIST)
27021 return any_value_dependent_elements_p (expression);
27022
27023 if (TREE_CODE (type) == REFERENCE_TYPE
27024 && has_value_dependent_address (expression))
27025 return true;
27026
27027 return value_dependent_expression_p (expression);
27028 }
27029
27030 case SIZEOF_EXPR:
27031 if (SIZEOF_EXPR_TYPE_P (expression))
27032 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
27033 /* FALLTHRU */
27034 case ALIGNOF_EXPR:
27035 case TYPEID_EXPR:
27036 /* A `sizeof' expression is value-dependent if the operand is
27037 type-dependent or is a pack expansion. */
27038 expression = TREE_OPERAND (expression, 0);
27039 if (PACK_EXPANSION_P (expression))
27040 return true;
27041 else if (TYPE_P (expression))
27042 return dependent_type_p (expression);
27043 return instantiation_dependent_uneval_expression_p (expression);
27044
27045 case AT_ENCODE_EXPR:
27046 /* An 'encode' expression is value-dependent if the operand is
27047 type-dependent. */
27048 expression = TREE_OPERAND (expression, 0);
27049 return dependent_type_p (expression);
27050
27051 case NOEXCEPT_EXPR:
27052 expression = TREE_OPERAND (expression, 0);
27053 return instantiation_dependent_uneval_expression_p (expression);
27054
27055 case SCOPE_REF:
27056 /* All instantiation-dependent expressions should also be considered
27057 value-dependent. */
27058 return instantiation_dependent_scope_ref_p (expression);
27059
27060 case COMPONENT_REF:
27061 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
27062 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
27063
27064 case NONTYPE_ARGUMENT_PACK:
27065 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27066 is value-dependent. */
27067 {
27068 tree values = ARGUMENT_PACK_ARGS (expression);
27069 int i, len = TREE_VEC_LENGTH (values);
27070
27071 for (i = 0; i < len; ++i)
27072 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
27073 return true;
27074
27075 return false;
27076 }
27077
27078 case TRAIT_EXPR:
27079 {
27080 tree type2 = TRAIT_EXPR_TYPE2 (expression);
27081
27082 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
27083 return true;
27084
27085 if (!type2)
27086 return false;
27087
27088 if (TREE_CODE (type2) != TREE_LIST)
27089 return dependent_type_p (type2);
27090
27091 for (; type2; type2 = TREE_CHAIN (type2))
27092 if (dependent_type_p (TREE_VALUE (type2)))
27093 return true;
27094
27095 return false;
27096 }
27097
27098 case MODOP_EXPR:
27099 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27100 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
27101
27102 case ARRAY_REF:
27103 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27104 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
27105
27106 case ADDR_EXPR:
27107 {
27108 tree op = TREE_OPERAND (expression, 0);
27109 return (value_dependent_expression_p (op)
27110 || has_value_dependent_address (op));
27111 }
27112
27113 case REQUIRES_EXPR:
27114 /* Treat all requires-expressions as value-dependent so
27115 we don't try to fold them. */
27116 return true;
27117
27118 case TYPE_REQ:
27119 return dependent_type_p (TREE_OPERAND (expression, 0));
27120
27121 case CALL_EXPR:
27122 {
27123 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
27124 return true;
27125 tree fn = get_callee_fndecl (expression);
27126 int i, nargs;
27127 nargs = call_expr_nargs (expression);
27128 for (i = 0; i < nargs; ++i)
27129 {
27130 tree op = CALL_EXPR_ARG (expression, i);
27131 /* In a call to a constexpr member function, look through the
27132 implicit ADDR_EXPR on the object argument so that it doesn't
27133 cause the call to be considered value-dependent. We also
27134 look through it in potential_constant_expression. */
27135 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
27136 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
27137 && TREE_CODE (op) == ADDR_EXPR)
27138 op = TREE_OPERAND (op, 0);
27139 if (value_dependent_expression_p (op))
27140 return true;
27141 }
27142 return false;
27143 }
27144
27145 case TEMPLATE_ID_EXPR:
27146 return concept_definition_p (TREE_OPERAND (expression, 0))
27147 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
27148
27149 case CONSTRUCTOR:
27150 {
27151 unsigned ix;
27152 tree val;
27153 if (dependent_type_p (TREE_TYPE (expression)))
27154 return true;
27155 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
27156 if (value_dependent_expression_p (val))
27157 return true;
27158 return false;
27159 }
27160
27161 case STMT_EXPR:
27162 /* Treat a GNU statement expression as dependent to avoid crashing
27163 under instantiate_non_dependent_expr; it can't be constant. */
27164 return true;
27165
27166 default:
27167 /* A constant expression is value-dependent if any subexpression is
27168 value-dependent. */
27169 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
27170 {
27171 case tcc_reference:
27172 case tcc_unary:
27173 case tcc_comparison:
27174 case tcc_binary:
27175 case tcc_expression:
27176 case tcc_vl_exp:
27177 {
27178 int i, len = cp_tree_operand_length (expression);
27179
27180 for (i = 0; i < len; i++)
27181 {
27182 tree t = TREE_OPERAND (expression, i);
27183
27184 /* In some cases, some of the operands may be missing.
27185 (For example, in the case of PREDECREMENT_EXPR, the
27186 amount to increment by may be missing.) That doesn't
27187 make the expression dependent. */
27188 if (t && value_dependent_expression_p (t))
27189 return true;
27190 }
27191 }
27192 break;
27193 default:
27194 break;
27195 }
27196 break;
27197 }
27198
27199 /* The expression is not value-dependent. */
27200 return false;
27201 }
27202
27203 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27204 [temp.dep.expr]. Note that an expression with no type is
27205 considered dependent. Other parts of the compiler arrange for an
27206 expression with type-dependent subexpressions to have no type, so
27207 this function doesn't have to be fully recursive. */
27208
27209 bool
27210 type_dependent_expression_p (tree expression)
27211 {
27212 if (!processing_template_decl)
27213 return false;
27214
27215 if (expression == NULL_TREE || expression == error_mark_node)
27216 return false;
27217
27218 STRIP_ANY_LOCATION_WRAPPER (expression);
27219
27220 /* An unresolved name is always dependent. */
27221 if (identifier_p (expression)
27222 || TREE_CODE (expression) == USING_DECL
27223 || TREE_CODE (expression) == WILDCARD_DECL)
27224 return true;
27225
27226 /* A lambda-expression in template context is dependent. dependent_type_p is
27227 true for a lambda in the scope of a class or function template, but that
27228 doesn't cover all template contexts, like a default template argument. */
27229 if (TREE_CODE (expression) == LAMBDA_EXPR)
27230 return true;
27231
27232 /* A fold expression is type-dependent. */
27233 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27234 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27235 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27236 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27237 return true;
27238
27239 /* Some expression forms are never type-dependent. */
27240 if (TREE_CODE (expression) == SIZEOF_EXPR
27241 || TREE_CODE (expression) == ALIGNOF_EXPR
27242 || TREE_CODE (expression) == AT_ENCODE_EXPR
27243 || TREE_CODE (expression) == NOEXCEPT_EXPR
27244 || TREE_CODE (expression) == TRAIT_EXPR
27245 || TREE_CODE (expression) == TYPEID_EXPR
27246 || TREE_CODE (expression) == DELETE_EXPR
27247 || TREE_CODE (expression) == VEC_DELETE_EXPR
27248 || TREE_CODE (expression) == THROW_EXPR
27249 || TREE_CODE (expression) == REQUIRES_EXPR)
27250 return false;
27251
27252 /* The types of these expressions depends only on the type to which
27253 the cast occurs. */
27254 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27255 || TREE_CODE (expression) == STATIC_CAST_EXPR
27256 || TREE_CODE (expression) == CONST_CAST_EXPR
27257 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27258 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27259 || TREE_CODE (expression) == CAST_EXPR)
27260 return dependent_type_p (TREE_TYPE (expression));
27261
27262 /* The types of these expressions depends only on the type created
27263 by the expression. */
27264 if (TREE_CODE (expression) == NEW_EXPR
27265 || TREE_CODE (expression) == VEC_NEW_EXPR)
27266 {
27267 /* For NEW_EXPR tree nodes created inside a template, either
27268 the object type itself or a TREE_LIST may appear as the
27269 operand 1. */
27270 tree type = TREE_OPERAND (expression, 1);
27271 if (TREE_CODE (type) == TREE_LIST)
27272 /* This is an array type. We need to check array dimensions
27273 as well. */
27274 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27275 || value_dependent_expression_p
27276 (TREE_OPERAND (TREE_VALUE (type), 1));
27277 /* Array type whose dimension has to be deduced. */
27278 else if (TREE_CODE (type) == ARRAY_TYPE
27279 && TREE_OPERAND (expression, 2) == NULL_TREE)
27280 return true;
27281 else
27282 return dependent_type_p (type);
27283 }
27284
27285 if (TREE_CODE (expression) == SCOPE_REF)
27286 {
27287 tree scope = TREE_OPERAND (expression, 0);
27288 tree name = TREE_OPERAND (expression, 1);
27289
27290 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27291 contains an identifier associated by name lookup with one or more
27292 declarations declared with a dependent type, or...a
27293 nested-name-specifier or qualified-id that names a member of an
27294 unknown specialization. */
27295 return (type_dependent_expression_p (name)
27296 || dependent_scope_p (scope));
27297 }
27298
27299 if (TREE_CODE (expression) == TEMPLATE_DECL
27300 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27301 return uses_outer_template_parms (expression);
27302
27303 if (TREE_CODE (expression) == STMT_EXPR)
27304 expression = stmt_expr_value_expr (expression);
27305
27306 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27307 {
27308 tree elt;
27309 unsigned i;
27310
27311 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27312 {
27313 if (type_dependent_expression_p (elt))
27314 return true;
27315 }
27316 return false;
27317 }
27318
27319 /* A static data member of the current instantiation with incomplete
27320 array type is type-dependent, as the definition and specializations
27321 can have different bounds. */
27322 if (VAR_P (expression)
27323 && DECL_CLASS_SCOPE_P (expression)
27324 && dependent_type_p (DECL_CONTEXT (expression))
27325 && VAR_HAD_UNKNOWN_BOUND (expression))
27326 return true;
27327
27328 /* An array of unknown bound depending on a variadic parameter, eg:
27329
27330 template<typename... Args>
27331 void foo (Args... args)
27332 {
27333 int arr[] = { args... };
27334 }
27335
27336 template<int... vals>
27337 void bar ()
27338 {
27339 int arr[] = { vals... };
27340 }
27341
27342 If the array has no length and has an initializer, it must be that
27343 we couldn't determine its length in cp_complete_array_type because
27344 it is dependent. */
27345 if (VAR_P (expression)
27346 && TREE_TYPE (expression) != NULL_TREE
27347 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27348 && !TYPE_DOMAIN (TREE_TYPE (expression))
27349 && DECL_INITIAL (expression))
27350 return true;
27351
27352 /* A function or variable template-id is type-dependent if it has any
27353 dependent template arguments. */
27354 if (VAR_OR_FUNCTION_DECL_P (expression)
27355 && DECL_LANG_SPECIFIC (expression)
27356 && DECL_TEMPLATE_INFO (expression))
27357 {
27358 /* Consider the innermost template arguments, since those are the ones
27359 that come from the template-id; the template arguments for the
27360 enclosing class do not make it type-dependent unless they are used in
27361 the type of the decl. */
27362 if (instantiates_primary_template_p (expression)
27363 && (any_dependent_template_arguments_p
27364 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27365 return true;
27366 }
27367
27368 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27369 type-dependent. Checking this is important for functions with auto return
27370 type, which looks like a dependent type. */
27371 if (TREE_CODE (expression) == FUNCTION_DECL
27372 && !(DECL_CLASS_SCOPE_P (expression)
27373 && dependent_type_p (DECL_CONTEXT (expression)))
27374 && !(DECL_LANG_SPECIFIC (expression)
27375 && DECL_UNIQUE_FRIEND_P (expression)
27376 && (!DECL_FRIEND_CONTEXT (expression)
27377 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27378 && !DECL_LOCAL_DECL_P (expression))
27379 {
27380 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27381 || undeduced_auto_decl (expression));
27382 return false;
27383 }
27384
27385 /* Always dependent, on the number of arguments if nothing else. */
27386 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27387 return true;
27388
27389 if (TREE_TYPE (expression) == unknown_type_node)
27390 {
27391 if (TREE_CODE (expression) == ADDR_EXPR)
27392 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27393 if (TREE_CODE (expression) == COMPONENT_REF
27394 || TREE_CODE (expression) == OFFSET_REF)
27395 {
27396 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27397 return true;
27398 expression = TREE_OPERAND (expression, 1);
27399 if (identifier_p (expression))
27400 return false;
27401 }
27402 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27403 if (TREE_CODE (expression) == SCOPE_REF)
27404 return false;
27405
27406 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27407 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27408 || TREE_CODE (expression) == CO_YIELD_EXPR)
27409 return true;
27410
27411 if (BASELINK_P (expression))
27412 {
27413 if (BASELINK_OPTYPE (expression)
27414 && dependent_type_p (BASELINK_OPTYPE (expression)))
27415 return true;
27416 expression = BASELINK_FUNCTIONS (expression);
27417 }
27418
27419 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27420 {
27421 if (any_dependent_template_arguments_p
27422 (TREE_OPERAND (expression, 1)))
27423 return true;
27424 expression = TREE_OPERAND (expression, 0);
27425 if (identifier_p (expression))
27426 return true;
27427 }
27428
27429 gcc_assert (OVL_P (expression));
27430
27431 for (lkp_iterator iter (expression); iter; ++iter)
27432 if (type_dependent_expression_p (*iter))
27433 return true;
27434
27435 return false;
27436 }
27437
27438 /* The type of a non-type template parm declared with a placeholder type
27439 depends on the corresponding template argument, even though
27440 placeholders are not normally considered dependent. */
27441 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27442 && is_auto (TREE_TYPE (expression)))
27443 return true;
27444
27445 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27446
27447 /* Dependent type attributes might not have made it from the decl to
27448 the type yet. */
27449 if (DECL_P (expression)
27450 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27451 return true;
27452
27453 return (dependent_type_p (TREE_TYPE (expression)));
27454 }
27455
27456 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27457 type-dependent if the expression refers to a member of the current
27458 instantiation and the type of the referenced member is dependent, or the
27459 class member access expression refers to a member of an unknown
27460 specialization.
27461
27462 This function returns true if the OBJECT in such a class member access
27463 expression is of an unknown specialization. */
27464
27465 bool
27466 type_dependent_object_expression_p (tree object)
27467 {
27468 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27469 dependent. */
27470 if (TREE_CODE (object) == IDENTIFIER_NODE)
27471 return true;
27472 tree scope = TREE_TYPE (object);
27473 return (!scope || dependent_scope_p (scope));
27474 }
27475
27476 /* walk_tree callback function for instantiation_dependent_expression_p,
27477 below. Returns non-zero if a dependent subexpression is found. */
27478
27479 static tree
27480 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27481 void * /*data*/)
27482 {
27483 if (TYPE_P (*tp))
27484 {
27485 /* We don't have to worry about decltype currently because decltype
27486 of an instantiation-dependent expr is a dependent type. This
27487 might change depending on the resolution of DR 1172. */
27488 *walk_subtrees = false;
27489 return NULL_TREE;
27490 }
27491 enum tree_code code = TREE_CODE (*tp);
27492 switch (code)
27493 {
27494 /* Don't treat an argument list as dependent just because it has no
27495 TREE_TYPE. */
27496 case TREE_LIST:
27497 case TREE_VEC:
27498 case NONTYPE_ARGUMENT_PACK:
27499 return NULL_TREE;
27500
27501 case TEMPLATE_PARM_INDEX:
27502 if (dependent_type_p (TREE_TYPE (*tp)))
27503 return *tp;
27504 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27505 return *tp;
27506 /* We'll check value-dependence separately. */
27507 return NULL_TREE;
27508
27509 /* Handle expressions with type operands. */
27510 case SIZEOF_EXPR:
27511 case ALIGNOF_EXPR:
27512 case TYPEID_EXPR:
27513 case AT_ENCODE_EXPR:
27514 {
27515 tree op = TREE_OPERAND (*tp, 0);
27516 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27517 op = TREE_TYPE (op);
27518 if (TYPE_P (op))
27519 {
27520 if (dependent_type_p (op))
27521 return *tp;
27522 else
27523 {
27524 *walk_subtrees = false;
27525 return NULL_TREE;
27526 }
27527 }
27528 break;
27529 }
27530
27531 case COMPONENT_REF:
27532 if (identifier_p (TREE_OPERAND (*tp, 1)))
27533 /* In a template, finish_class_member_access_expr creates a
27534 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27535 type-dependent, so that we can check access control at
27536 instantiation time (PR 42277). See also Core issue 1273. */
27537 return *tp;
27538 break;
27539
27540 case SCOPE_REF:
27541 if (instantiation_dependent_scope_ref_p (*tp))
27542 return *tp;
27543 else
27544 break;
27545
27546 /* Treat statement-expressions as dependent. */
27547 case BIND_EXPR:
27548 return *tp;
27549
27550 /* Treat requires-expressions as dependent. */
27551 case REQUIRES_EXPR:
27552 return *tp;
27553
27554 case CONSTRUCTOR:
27555 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27556 return *tp;
27557 break;
27558
27559 case TEMPLATE_DECL:
27560 case FUNCTION_DECL:
27561 /* Before C++17, a noexcept-specifier isn't part of the function type
27562 so it doesn't affect type dependence, but we still want to consider it
27563 for instantiation dependence. */
27564 if (cxx_dialect < cxx17
27565 && DECL_DECLARES_FUNCTION_P (*tp)
27566 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
27567 return *tp;
27568 break;
27569
27570 default:
27571 break;
27572 }
27573
27574 if (type_dependent_expression_p (*tp))
27575 return *tp;
27576 else
27577 return NULL_TREE;
27578 }
27579
27580 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27581 sense defined by the ABI:
27582
27583 "An expression is instantiation-dependent if it is type-dependent
27584 or value-dependent, or it has a subexpression that is type-dependent
27585 or value-dependent."
27586
27587 Except don't actually check value-dependence for unevaluated expressions,
27588 because in sizeof(i) we don't care about the value of i. Checking
27589 type-dependence will in turn check value-dependence of array bounds/template
27590 arguments as needed. */
27591
27592 bool
27593 instantiation_dependent_uneval_expression_p (tree expression)
27594 {
27595 tree result;
27596
27597 if (!processing_template_decl)
27598 return false;
27599
27600 if (expression == error_mark_node)
27601 return false;
27602
27603 result = cp_walk_tree_without_duplicates (&expression,
27604 instantiation_dependent_r, NULL);
27605 return result != NULL_TREE;
27606 }
27607
27608 /* As above, but also check value-dependence of the expression as a whole. */
27609
27610 bool
27611 instantiation_dependent_expression_p (tree expression)
27612 {
27613 return (instantiation_dependent_uneval_expression_p (expression)
27614 || (processing_template_decl
27615 && potential_constant_expression (expression)
27616 && value_dependent_expression_p (expression)));
27617 }
27618
27619 /* Like type_dependent_expression_p, but it also works while not processing
27620 a template definition, i.e. during substitution or mangling. */
27621
27622 bool
27623 type_dependent_expression_p_push (tree expr)
27624 {
27625 bool b;
27626 ++processing_template_decl;
27627 b = type_dependent_expression_p (expr);
27628 --processing_template_decl;
27629 return b;
27630 }
27631
27632 /* Returns TRUE if ARGS contains a type-dependent expression. */
27633
27634 bool
27635 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27636 {
27637 unsigned int i;
27638 tree arg;
27639
27640 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27641 {
27642 if (type_dependent_expression_p (arg))
27643 return true;
27644 }
27645 return false;
27646 }
27647
27648 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27649 expressions) contains any type-dependent expressions. */
27650
27651 bool
27652 any_type_dependent_elements_p (const_tree list)
27653 {
27654 for (; list; list = TREE_CHAIN (list))
27655 if (type_dependent_expression_p (TREE_VALUE (list)))
27656 return true;
27657
27658 return false;
27659 }
27660
27661 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27662 expressions) contains any value-dependent expressions. */
27663
27664 bool
27665 any_value_dependent_elements_p (const_tree list)
27666 {
27667 for (; list; list = TREE_CHAIN (list))
27668 if (value_dependent_expression_p (TREE_VALUE (list)))
27669 return true;
27670
27671 return false;
27672 }
27673
27674 /* Returns TRUE if the ARG (a template argument) is dependent. */
27675
27676 bool
27677 dependent_template_arg_p (tree arg)
27678 {
27679 if (!processing_template_decl)
27680 return false;
27681
27682 /* Assume a template argument that was wrongly written by the user
27683 is dependent. This is consistent with what
27684 any_dependent_template_arguments_p [that calls this function]
27685 does. */
27686 if (!arg || arg == error_mark_node)
27687 return true;
27688
27689 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27690 arg = argument_pack_select_arg (arg);
27691
27692 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27693 return true;
27694 if (TREE_CODE (arg) == TEMPLATE_DECL)
27695 {
27696 if (DECL_TEMPLATE_PARM_P (arg))
27697 return true;
27698 /* A member template of a dependent class is not necessarily
27699 type-dependent, but it is a dependent template argument because it
27700 will be a member of an unknown specialization to that template. */
27701 tree scope = CP_DECL_CONTEXT (arg);
27702 return TYPE_P (scope) && dependent_type_p (scope);
27703 }
27704 else if (ARGUMENT_PACK_P (arg))
27705 {
27706 tree args = ARGUMENT_PACK_ARGS (arg);
27707 int i, len = TREE_VEC_LENGTH (args);
27708 for (i = 0; i < len; ++i)
27709 {
27710 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27711 return true;
27712 }
27713
27714 return false;
27715 }
27716 else if (TYPE_P (arg))
27717 return dependent_type_p (arg);
27718 else
27719 return value_dependent_expression_p (arg);
27720 }
27721
27722 /* Returns true if ARGS (a collection of template arguments) contains
27723 any types that require structural equality testing. */
27724
27725 bool
27726 any_template_arguments_need_structural_equality_p (tree args)
27727 {
27728 int i;
27729 int j;
27730
27731 if (!args)
27732 return false;
27733 if (args == error_mark_node)
27734 return true;
27735
27736 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27737 {
27738 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27739 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27740 {
27741 tree arg = TREE_VEC_ELT (level, j);
27742 tree packed_args = NULL_TREE;
27743 int k, len = 1;
27744
27745 if (ARGUMENT_PACK_P (arg))
27746 {
27747 /* Look inside the argument pack. */
27748 packed_args = ARGUMENT_PACK_ARGS (arg);
27749 len = TREE_VEC_LENGTH (packed_args);
27750 }
27751
27752 for (k = 0; k < len; ++k)
27753 {
27754 if (packed_args)
27755 arg = TREE_VEC_ELT (packed_args, k);
27756
27757 if (error_operand_p (arg))
27758 return true;
27759 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27760 continue;
27761 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27762 return true;
27763 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27764 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27765 return true;
27766 }
27767 }
27768 }
27769
27770 return false;
27771 }
27772
27773 /* Returns true if ARGS (a collection of template arguments) contains
27774 any dependent arguments. */
27775
27776 bool
27777 any_dependent_template_arguments_p (const_tree args)
27778 {
27779 int i;
27780 int j;
27781
27782 if (!args)
27783 return false;
27784 if (args == error_mark_node)
27785 return true;
27786
27787 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27788 {
27789 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27790 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27791 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27792 return true;
27793 }
27794
27795 return false;
27796 }
27797
27798 /* Returns true if ARGS contains any errors. */
27799
27800 bool
27801 any_erroneous_template_args_p (const_tree args)
27802 {
27803 int i;
27804 int j;
27805
27806 if (args == error_mark_node)
27807 return true;
27808
27809 if (args && TREE_CODE (args) != TREE_VEC)
27810 {
27811 if (tree ti = get_template_info (args))
27812 args = TI_ARGS (ti);
27813 else
27814 args = NULL_TREE;
27815 }
27816
27817 if (!args)
27818 return false;
27819
27820 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27821 {
27822 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27823 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27824 if (error_operand_p (TREE_VEC_ELT (level, j)))
27825 return true;
27826 }
27827
27828 return false;
27829 }
27830
27831 /* Returns TRUE if the template TMPL is type-dependent. */
27832
27833 bool
27834 dependent_template_p (tree tmpl)
27835 {
27836 if (TREE_CODE (tmpl) == OVERLOAD)
27837 {
27838 for (lkp_iterator iter (tmpl); iter; ++iter)
27839 if (dependent_template_p (*iter))
27840 return true;
27841 return false;
27842 }
27843
27844 /* Template template parameters are dependent. */
27845 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27846 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27847 return true;
27848 /* So are names that have not been looked up. */
27849 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27850 return true;
27851 return false;
27852 }
27853
27854 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27855
27856 bool
27857 dependent_template_id_p (tree tmpl, tree args)
27858 {
27859 return (dependent_template_p (tmpl)
27860 || any_dependent_template_arguments_p (args));
27861 }
27862
27863 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27864 are dependent. */
27865
27866 bool
27867 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27868 {
27869 int i;
27870
27871 if (!processing_template_decl)
27872 return false;
27873
27874 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27875 {
27876 tree decl = TREE_VEC_ELT (declv, i);
27877 tree init = TREE_VEC_ELT (initv, i);
27878 tree cond = TREE_VEC_ELT (condv, i);
27879 tree incr = TREE_VEC_ELT (incrv, i);
27880
27881 if (type_dependent_expression_p (decl)
27882 || TREE_CODE (decl) == SCOPE_REF)
27883 return true;
27884
27885 if (init && type_dependent_expression_p (init))
27886 return true;
27887
27888 if (cond == global_namespace)
27889 return true;
27890
27891 if (type_dependent_expression_p (cond))
27892 return true;
27893
27894 if (COMPARISON_CLASS_P (cond)
27895 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27896 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27897 return true;
27898
27899 if (TREE_CODE (incr) == MODOP_EXPR)
27900 {
27901 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27902 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27903 return true;
27904 }
27905 else if (type_dependent_expression_p (incr))
27906 return true;
27907 else if (TREE_CODE (incr) == MODIFY_EXPR)
27908 {
27909 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27910 return true;
27911 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27912 {
27913 tree t = TREE_OPERAND (incr, 1);
27914 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27915 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27916 return true;
27917
27918 /* If this loop has a class iterator with != comparison
27919 with increment other than i++/++i/i--/--i, make sure the
27920 increment is constant. */
27921 if (CLASS_TYPE_P (TREE_TYPE (decl))
27922 && TREE_CODE (cond) == NE_EXPR)
27923 {
27924 if (TREE_OPERAND (t, 0) == decl)
27925 t = TREE_OPERAND (t, 1);
27926 else
27927 t = TREE_OPERAND (t, 0);
27928 if (TREE_CODE (t) != INTEGER_CST)
27929 return true;
27930 }
27931 }
27932 }
27933 }
27934
27935 return false;
27936 }
27937
27938 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27939 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27940 no such TYPE can be found. Note that this function peers inside
27941 uninstantiated templates and therefore should be used only in
27942 extremely limited situations. ONLY_CURRENT_P restricts this
27943 peering to the currently open classes hierarchy (which is required
27944 when comparing types). */
27945
27946 tree
27947 resolve_typename_type (tree type, bool only_current_p)
27948 {
27949 tree scope;
27950 tree name;
27951 tree decl;
27952 int quals;
27953 tree pushed_scope;
27954 tree result;
27955
27956 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27957
27958 scope = TYPE_CONTEXT (type);
27959 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27960 gcc_checking_assert (uses_template_parms (scope));
27961
27962 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27963 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27964 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27965 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27966 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27967 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27968 the TYPENAME_TYPE instead, we avoid messing up with a possible
27969 typedef variant case. */
27970 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27971
27972 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27973 it first before we can figure out what NAME refers to. */
27974 if (TREE_CODE (scope) == TYPENAME_TYPE)
27975 {
27976 if (TYPENAME_IS_RESOLVING_P (scope))
27977 /* Given a class template A with a dependent base with nested type C,
27978 typedef typename A::C::C C will land us here, as trying to resolve
27979 the initial A::C leads to the local C typedef, which leads back to
27980 A::C::C. So we break the recursion now. */
27981 return type;
27982 else
27983 scope = resolve_typename_type (scope, only_current_p);
27984 }
27985 /* If we don't know what SCOPE refers to, then we cannot resolve the
27986 TYPENAME_TYPE. */
27987 if (!CLASS_TYPE_P (scope))
27988 return type;
27989 /* If this is a typedef, we don't want to look inside (c++/11987). */
27990 if (typedef_variant_p (type))
27991 return type;
27992 /* If SCOPE isn't the template itself, it will not have a valid
27993 TYPE_FIELDS list. */
27994 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27995 /* scope is either the template itself or a compatible instantiation
27996 like X<T>, so look up the name in the original template. */
27997 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27998 /* If scope has no fields, it can't be a current instantiation. Check this
27999 before currently_open_class to avoid infinite recursion (71515). */
28000 if (!TYPE_FIELDS (scope))
28001 return type;
28002 /* If the SCOPE is not the current instantiation, there's no reason
28003 to look inside it. */
28004 if (only_current_p && !currently_open_class (scope))
28005 return type;
28006 /* Enter the SCOPE so that name lookup will be resolved as if we
28007 were in the class definition. In particular, SCOPE will no
28008 longer be considered a dependent type. */
28009 pushed_scope = push_scope (scope);
28010 /* Look up the declaration. */
28011 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
28012 tf_warning_or_error);
28013
28014 result = NULL_TREE;
28015
28016 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28017 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28018 tree fullname = TYPENAME_TYPE_FULLNAME (type);
28019 if (!decl)
28020 /*nop*/;
28021 else if (identifier_p (fullname)
28022 && TREE_CODE (decl) == TYPE_DECL)
28023 {
28024 result = TREE_TYPE (decl);
28025 if (result == error_mark_node)
28026 result = NULL_TREE;
28027 }
28028 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
28029 && DECL_CLASS_TEMPLATE_P (decl))
28030 {
28031 /* Obtain the template and the arguments. */
28032 tree tmpl = TREE_OPERAND (fullname, 0);
28033 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
28034 {
28035 /* We get here with a plain identifier because a previous tentative
28036 parse of the nested-name-specifier as part of a ptr-operator saw
28037 ::template X<A>. The use of ::template is necessary in a
28038 ptr-operator, but wrong in a declarator-id.
28039
28040 [temp.names]: In a qualified-id of a declarator-id, the keyword
28041 template shall not appear at the top level. */
28042 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
28043 "keyword %<template%> not allowed in declarator-id");
28044 tmpl = decl;
28045 }
28046 tree args = TREE_OPERAND (fullname, 1);
28047 /* Instantiate the template. */
28048 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
28049 /*entering_scope=*/true,
28050 tf_error | tf_user);
28051 if (result == error_mark_node)
28052 result = NULL_TREE;
28053 }
28054
28055 /* Leave the SCOPE. */
28056 if (pushed_scope)
28057 pop_scope (pushed_scope);
28058
28059 /* If we failed to resolve it, return the original typename. */
28060 if (!result)
28061 return type;
28062
28063 /* If lookup found a typename type, resolve that too. */
28064 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
28065 {
28066 /* Ill-formed programs can cause infinite recursion here, so we
28067 must catch that. */
28068 TYPENAME_IS_RESOLVING_P (result) = 1;
28069 result = resolve_typename_type (result, only_current_p);
28070 TYPENAME_IS_RESOLVING_P (result) = 0;
28071 }
28072
28073 /* Qualify the resulting type. */
28074 quals = cp_type_quals (type);
28075 if (quals)
28076 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
28077
28078 return result;
28079 }
28080
28081 /* EXPR is an expression which is not type-dependent. Return a proxy
28082 for EXPR that can be used to compute the types of larger
28083 expressions containing EXPR. */
28084
28085 tree
28086 build_non_dependent_expr (tree expr)
28087 {
28088 tree orig_expr = expr;
28089 tree inner_expr;
28090
28091 /* When checking, try to get a constant value for all non-dependent
28092 expressions in order to expose bugs in *_dependent_expression_p
28093 and constexpr. This can affect code generation, see PR70704, so
28094 only do this for -fchecking=2. */
28095 if (flag_checking > 1
28096 && cxx_dialect >= cxx11
28097 /* Don't do this during nsdmi parsing as it can lead to
28098 unexpected recursive instantiations. */
28099 && !parsing_nsdmi ()
28100 /* Don't do this during concept processing either and for
28101 the same reason. */
28102 && !processing_constraint_expression_p ())
28103 fold_non_dependent_expr (expr, tf_none);
28104
28105 STRIP_ANY_LOCATION_WRAPPER (expr);
28106
28107 /* Preserve OVERLOADs; the functions must be available to resolve
28108 types. */
28109 inner_expr = expr;
28110 if (TREE_CODE (inner_expr) == STMT_EXPR)
28111 inner_expr = stmt_expr_value_expr (inner_expr);
28112 if (TREE_CODE (inner_expr) == ADDR_EXPR)
28113 inner_expr = TREE_OPERAND (inner_expr, 0);
28114 if (TREE_CODE (inner_expr) == COMPONENT_REF)
28115 inner_expr = TREE_OPERAND (inner_expr, 1);
28116 if (is_overloaded_fn (inner_expr)
28117 || TREE_CODE (inner_expr) == OFFSET_REF)
28118 return orig_expr;
28119 /* There is no need to return a proxy for a variable or enumerator. */
28120 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
28121 return orig_expr;
28122 /* Preserve string constants; conversions from string constants to
28123 "char *" are allowed, even though normally a "const char *"
28124 cannot be used to initialize a "char *". */
28125 if (TREE_CODE (expr) == STRING_CST)
28126 return orig_expr;
28127 /* Preserve void and arithmetic constants, as an optimization -- there is no
28128 reason to create a new node. */
28129 if (TREE_CODE (expr) == VOID_CST
28130 || TREE_CODE (expr) == INTEGER_CST
28131 || TREE_CODE (expr) == REAL_CST)
28132 return orig_expr;
28133 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28134 There is at least one place where we want to know that a
28135 particular expression is a throw-expression: when checking a ?:
28136 expression, there are special rules if the second or third
28137 argument is a throw-expression. */
28138 if (TREE_CODE (expr) == THROW_EXPR)
28139 return orig_expr;
28140
28141 /* Don't wrap an initializer list, we need to be able to look inside. */
28142 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
28143 return orig_expr;
28144
28145 /* Don't wrap a dummy object, we need to be able to test for it. */
28146 if (is_dummy_object (expr))
28147 return orig_expr;
28148
28149 if (TREE_CODE (expr) == COND_EXPR)
28150 return build3 (COND_EXPR,
28151 TREE_TYPE (expr),
28152 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
28153 (TREE_OPERAND (expr, 1)
28154 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
28155 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
28156 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
28157 if (TREE_CODE (expr) == COMPOUND_EXPR
28158 && !COMPOUND_EXPR_OVERLOADED (expr))
28159 return build2 (COMPOUND_EXPR,
28160 TREE_TYPE (expr),
28161 TREE_OPERAND (expr, 0),
28162 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
28163
28164 /* If the type is unknown, it can't really be non-dependent */
28165 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
28166
28167 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28168 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
28169 TREE_TYPE (expr), expr);
28170 }
28171
28172 /* ARGS is a vector of expressions as arguments to a function call.
28173 Replace the arguments with equivalent non-dependent expressions.
28174 This modifies ARGS in place. */
28175
28176 void
28177 make_args_non_dependent (vec<tree, va_gc> *args)
28178 {
28179 unsigned int ix;
28180 tree arg;
28181
28182 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
28183 {
28184 tree newarg = build_non_dependent_expr (arg);
28185 if (newarg != arg)
28186 (*args)[ix] = newarg;
28187 }
28188 }
28189
28190 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28191 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
28192 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
28193
28194 static tree
28195 make_auto_1 (tree name, bool set_canonical)
28196 {
28197 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
28198 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
28199 TYPE_STUB_DECL (au) = TYPE_NAME (au);
28200 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
28201 (0, processing_template_decl + 1, processing_template_decl + 1,
28202 TYPE_NAME (au), NULL_TREE);
28203 if (set_canonical)
28204 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28205 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28206 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28207 if (name == decltype_auto_identifier)
28208 AUTO_IS_DECLTYPE (au) = true;
28209
28210 return au;
28211 }
28212
28213 tree
28214 make_decltype_auto (void)
28215 {
28216 return make_auto_1 (decltype_auto_identifier, true);
28217 }
28218
28219 tree
28220 make_auto (void)
28221 {
28222 return make_auto_1 (auto_identifier, true);
28223 }
28224
28225 /* Return a C++17 deduction placeholder for class template TMPL. */
28226
28227 tree
28228 make_template_placeholder (tree tmpl)
28229 {
28230 tree t = make_auto_1 (auto_identifier, false);
28231 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28232 /* Our canonical type depends on the placeholder. */
28233 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28234 return t;
28235 }
28236
28237 /* True iff T is a C++17 class template deduction placeholder. */
28238
28239 bool
28240 template_placeholder_p (tree t)
28241 {
28242 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28243 }
28244
28245 /* Make a "constrained auto" type-specifier. This is an auto or
28246 decltype(auto) type with constraints that must be associated after
28247 deduction. The constraint is formed from the given concept CON
28248 and its optional sequence of template arguments ARGS.
28249
28250 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28251
28252 static tree
28253 make_constrained_placeholder_type (tree type, tree con, tree args)
28254 {
28255 /* Build the constraint. */
28256 tree tmpl = DECL_TI_TEMPLATE (con);
28257 tree expr = tmpl;
28258 if (TREE_CODE (con) == FUNCTION_DECL)
28259 expr = ovl_make (tmpl);
28260 ++processing_template_decl;
28261 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28262 --processing_template_decl;
28263
28264 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
28265 = build_tree_list (current_template_parms, expr);
28266
28267 /* Our canonical type depends on the constraint. */
28268 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28269
28270 /* Attach the constraint to the type declaration. */
28271 return TYPE_NAME (type);
28272 }
28273
28274 /* Make a "constrained auto" type-specifier. */
28275
28276 tree
28277 make_constrained_auto (tree con, tree args)
28278 {
28279 tree type = make_auto_1 (auto_identifier, false);
28280 return make_constrained_placeholder_type (type, con, args);
28281 }
28282
28283 /* Make a "constrained decltype(auto)" type-specifier. */
28284
28285 tree
28286 make_constrained_decltype_auto (tree con, tree args)
28287 {
28288 tree type = make_auto_1 (decltype_auto_identifier, false);
28289 return make_constrained_placeholder_type (type, con, args);
28290 }
28291
28292 /* Returns true if the placeholder type constraint T has any dependent
28293 (explicit) template arguments. */
28294
28295 static bool
28296 placeholder_type_constraint_dependent_p (tree t)
28297 {
28298 tree id = unpack_concept_check (t);
28299 tree args = TREE_OPERAND (id, 1);
28300 tree first = TREE_VEC_ELT (args, 0);
28301 if (ARGUMENT_PACK_P (first))
28302 {
28303 args = expand_template_argument_pack (args);
28304 first = TREE_VEC_ELT (args, 0);
28305 }
28306 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
28307 || is_auto (first));
28308 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
28309 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
28310 return true;
28311 return false;
28312 }
28313
28314 /* Build and return a concept definition. Like other templates, the
28315 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28316 the TEMPLATE_DECL. */
28317
28318 tree
28319 finish_concept_definition (cp_expr id, tree init)
28320 {
28321 gcc_assert (identifier_p (id));
28322 gcc_assert (processing_template_decl);
28323
28324 location_t loc = id.get_location();
28325
28326 /* A concept-definition shall not have associated constraints. */
28327 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28328 {
28329 error_at (loc, "a concept cannot be constrained");
28330 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28331 }
28332
28333 /* A concept-definition shall appear in namespace scope. Templates
28334 aren't allowed in block scope, so we only need to check for class
28335 scope. */
28336 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28337 {
28338 error_at (loc, "concept %qE not in namespace scope", *id);
28339 return error_mark_node;
28340 }
28341
28342 /* Initially build the concept declaration; its type is bool. */
28343 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28344 DECL_CONTEXT (decl) = current_scope ();
28345 DECL_INITIAL (decl) = init;
28346
28347 set_originating_module (decl, false);
28348
28349 /* Push the enclosing template. */
28350 return push_template_decl (decl);
28351 }
28352
28353 /* Given type ARG, return std::initializer_list<ARG>. */
28354
28355 static tree
28356 listify (tree arg)
28357 {
28358 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28359
28360 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28361 {
28362 gcc_rich_location richloc (input_location);
28363 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28364 error_at (&richloc,
28365 "deducing from brace-enclosed initializer list"
28366 " requires %<#include <initializer_list>%>");
28367
28368 return error_mark_node;
28369 }
28370 tree argvec = make_tree_vec (1);
28371 TREE_VEC_ELT (argvec, 0) = arg;
28372
28373 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28374 NULL_TREE, 0, tf_warning_or_error);
28375 }
28376
28377 /* Replace auto in TYPE with std::initializer_list<auto>. */
28378
28379 static tree
28380 listify_autos (tree type, tree auto_node)
28381 {
28382 tree init_auto = listify (strip_top_quals (auto_node));
28383 tree argvec = make_tree_vec (1);
28384 TREE_VEC_ELT (argvec, 0) = init_auto;
28385 if (processing_template_decl)
28386 argvec = add_to_template_args (current_template_args (), argvec);
28387 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28388 }
28389
28390 /* Hash traits for hashing possibly constrained 'auto'
28391 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28392
28393 struct auto_hash : default_hash_traits<tree>
28394 {
28395 static inline hashval_t hash (tree);
28396 static inline bool equal (tree, tree);
28397 };
28398
28399 /* Hash the 'auto' T. */
28400
28401 inline hashval_t
28402 auto_hash::hash (tree t)
28403 {
28404 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28405 /* Matching constrained-type-specifiers denote the same template
28406 parameter, so hash the constraint. */
28407 return hash_placeholder_constraint (c);
28408 else
28409 /* But unconstrained autos are all separate, so just hash the pointer. */
28410 return iterative_hash_object (t, 0);
28411 }
28412
28413 /* Compare two 'auto's. */
28414
28415 inline bool
28416 auto_hash::equal (tree t1, tree t2)
28417 {
28418 if (t1 == t2)
28419 return true;
28420
28421 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28422 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28423
28424 /* Two unconstrained autos are distinct. */
28425 if (!c1 || !c2)
28426 return false;
28427
28428 return equivalent_placeholder_constraints (c1, c2);
28429 }
28430
28431 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28432 constrained) auto, add it to the vector. */
28433
28434 static int
28435 extract_autos_r (tree t, void *data)
28436 {
28437 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28438 if (is_auto (t) && !template_placeholder_p (t))
28439 {
28440 /* All the autos were built with index 0; fix that up now. */
28441 tree *p = hash.find_slot (t, INSERT);
28442 unsigned idx;
28443 if (*p)
28444 /* If this is a repeated constrained-type-specifier, use the index we
28445 chose before. */
28446 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28447 else
28448 {
28449 /* Otherwise this is new, so use the current count. */
28450 *p = t;
28451 idx = hash.elements () - 1;
28452 }
28453 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28454 }
28455
28456 /* Always keep walking. */
28457 return 0;
28458 }
28459
28460 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28461 says they can appear anywhere in the type. */
28462
28463 static tree
28464 extract_autos (tree type)
28465 {
28466 hash_set<tree> visited;
28467 hash_table<auto_hash> hash (2);
28468
28469 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28470
28471 tree tree_vec = make_tree_vec (hash.elements());
28472 for (tree elt : hash)
28473 {
28474 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28475 TREE_VEC_ELT (tree_vec, i)
28476 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28477 }
28478
28479 return tree_vec;
28480 }
28481
28482 /* The stem for deduction guide names. */
28483 const char *const dguide_base = "__dguide_";
28484
28485 /* Return the name for a deduction guide for class template TMPL. */
28486
28487 tree
28488 dguide_name (tree tmpl)
28489 {
28490 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28491 tree tname = TYPE_IDENTIFIER (type);
28492 char *buf = (char *) alloca (1 + strlen (dguide_base)
28493 + IDENTIFIER_LENGTH (tname));
28494 memcpy (buf, dguide_base, strlen (dguide_base));
28495 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28496 IDENTIFIER_LENGTH (tname) + 1);
28497 tree dname = get_identifier (buf);
28498 TREE_TYPE (dname) = type;
28499 return dname;
28500 }
28501
28502 /* True if NAME is the name of a deduction guide. */
28503
28504 bool
28505 dguide_name_p (tree name)
28506 {
28507 return (TREE_CODE (name) == IDENTIFIER_NODE
28508 && TREE_TYPE (name)
28509 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28510 strlen (dguide_base)));
28511 }
28512
28513 /* True if FN is a deduction guide. */
28514
28515 bool
28516 deduction_guide_p (const_tree fn)
28517 {
28518 if (DECL_P (fn))
28519 if (tree name = DECL_NAME (fn))
28520 return dguide_name_p (name);
28521 return false;
28522 }
28523
28524 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28525
28526 bool
28527 copy_guide_p (const_tree fn)
28528 {
28529 gcc_assert (deduction_guide_p (fn));
28530 if (!DECL_ARTIFICIAL (fn))
28531 return false;
28532 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28533 return (TREE_CHAIN (parms) == void_list_node
28534 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28535 }
28536
28537 /* True if FN is a guide generated from a constructor template. */
28538
28539 bool
28540 template_guide_p (const_tree fn)
28541 {
28542 gcc_assert (deduction_guide_p (fn));
28543 if (!DECL_ARTIFICIAL (fn))
28544 return false;
28545 tree tmpl = DECL_TI_TEMPLATE (fn);
28546 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28547 return PRIMARY_TEMPLATE_P (org);
28548 return false;
28549 }
28550
28551 /* True if FN is an aggregate initialization guide or the copy deduction
28552 guide. */
28553
28554 bool
28555 builtin_guide_p (const_tree fn)
28556 {
28557 if (!deduction_guide_p (fn))
28558 return false;
28559 if (!DECL_ARTIFICIAL (fn))
28560 /* Explicitly declared. */
28561 return false;
28562 if (DECL_ABSTRACT_ORIGIN (fn))
28563 /* Derived from a constructor. */
28564 return false;
28565 return true;
28566 }
28567
28568 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28569 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28570 template parameter types. Note that the handling of template template
28571 parameters relies on current_template_parms being set appropriately for the
28572 new template. */
28573
28574 static tree
28575 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28576 tree tsubst_args, tsubst_flags_t complain)
28577 {
28578 if (olddecl == error_mark_node)
28579 return error_mark_node;
28580
28581 tree oldidx = get_template_parm_index (olddecl);
28582
28583 tree newtype;
28584 if (TREE_CODE (olddecl) == TYPE_DECL
28585 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28586 {
28587 tree oldtype = TREE_TYPE (olddecl);
28588 newtype = cxx_make_type (TREE_CODE (oldtype));
28589 TYPE_MAIN_VARIANT (newtype) = newtype;
28590 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28591 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28592 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28593 }
28594 else
28595 {
28596 newtype = TREE_TYPE (olddecl);
28597 if (type_uses_auto (newtype))
28598 {
28599 // Substitute once to fix references to other template parameters.
28600 newtype = tsubst (newtype, tsubst_args,
28601 complain|tf_partial, NULL_TREE);
28602 // Now substitute again to reduce the level of the auto.
28603 newtype = tsubst (newtype, current_template_args (),
28604 complain, NULL_TREE);
28605 }
28606 else
28607 newtype = tsubst (newtype, tsubst_args,
28608 complain, NULL_TREE);
28609 }
28610
28611 tree newdecl
28612 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28613 DECL_NAME (olddecl), newtype);
28614 SET_DECL_TEMPLATE_PARM_P (newdecl);
28615
28616 tree newidx;
28617 if (TREE_CODE (olddecl) == TYPE_DECL
28618 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28619 {
28620 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28621 = build_template_parm_index (index, level, level,
28622 newdecl, newtype);
28623 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28624 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28625 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28626 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28627 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28628 else
28629 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28630
28631 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28632 {
28633 DECL_TEMPLATE_RESULT (newdecl)
28634 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28635 DECL_NAME (olddecl), newtype);
28636 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28637 // First create a copy (ttargs) of tsubst_args with an
28638 // additional level for the template template parameter's own
28639 // template parameters (ttparms).
28640 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28641 (DECL_TEMPLATE_PARMS (olddecl)));
28642 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28643 tree ttargs = make_tree_vec (depth + 1);
28644 for (int i = 0; i < depth; ++i)
28645 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28646 TREE_VEC_ELT (ttargs, depth)
28647 = template_parms_level_to_args (ttparms);
28648 // Substitute ttargs into ttparms to fix references to
28649 // other template parameters.
28650 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28651 complain|tf_partial);
28652 // Now substitute again with args based on tparms, to reduce
28653 // the level of the ttparms.
28654 ttargs = current_template_args ();
28655 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28656 complain);
28657 // Finally, tack the adjusted parms onto tparms.
28658 ttparms = tree_cons (size_int (depth), ttparms,
28659 current_template_parms);
28660 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28661 }
28662 }
28663 else
28664 {
28665 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28666 tree newconst
28667 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28668 TREE_CODE (oldconst),
28669 DECL_NAME (oldconst), newtype);
28670 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28671 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28672 SET_DECL_TEMPLATE_PARM_P (newconst);
28673 newidx = build_template_parm_index (index, level, level,
28674 newconst, newtype);
28675 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28676 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28677 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28678 }
28679
28680 return newdecl;
28681 }
28682
28683 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28684 template parameter. */
28685
28686 static tree
28687 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28688 tree targs, unsigned targs_index, tsubst_flags_t complain)
28689 {
28690 tree olddecl = TREE_VALUE (oldelt);
28691 tree newdecl = rewrite_template_parm (olddecl, index, level,
28692 targs, complain);
28693 if (newdecl == error_mark_node)
28694 return error_mark_node;
28695 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28696 targs, complain, NULL_TREE);
28697 tree list = build_tree_list (newdef, newdecl);
28698 TEMPLATE_PARM_CONSTRAINTS (list)
28699 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28700 targs, complain, NULL_TREE);
28701 int depth = TMPL_ARGS_DEPTH (targs);
28702 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28703 return list;
28704 }
28705
28706 /* Returns a C++17 class deduction guide template based on the constructor
28707 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28708 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28709 aggregate initialization guide. OUTER_ARGS are the template arguments
28710 for the enclosing scope of the class. */
28711
28712 static tree
28713 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28714 {
28715 tree tparms, targs, fparms, fargs, ci;
28716 bool memtmpl = false;
28717 bool explicit_p;
28718 location_t loc;
28719 tree fn_tmpl = NULL_TREE;
28720
28721 if (outer_args)
28722 {
28723 ++processing_template_decl;
28724 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28725 --processing_template_decl;
28726 }
28727
28728 if (!DECL_DECLARES_FUNCTION_P (ctor))
28729 {
28730 if (TYPE_P (ctor))
28731 {
28732 bool copy_p = TYPE_REF_P (ctor);
28733 if (copy_p)
28734 fparms = tree_cons (NULL_TREE, type, void_list_node);
28735 else
28736 fparms = void_list_node;
28737 }
28738 else if (TREE_CODE (ctor) == TREE_LIST)
28739 fparms = ctor;
28740 else
28741 gcc_unreachable ();
28742
28743 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28744 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28745 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
28746 ci = NULL_TREE;
28747 fargs = NULL_TREE;
28748 loc = DECL_SOURCE_LOCATION (ctmpl);
28749 explicit_p = false;
28750 }
28751 else
28752 {
28753 ++processing_template_decl;
28754 bool ok = true;
28755
28756 fn_tmpl
28757 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28758 : DECL_TI_TEMPLATE (ctor));
28759 if (outer_args)
28760 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28761 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28762
28763 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28764 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28765 fully specialized args for the enclosing class. Strip those off, as
28766 the deduction guide won't have those template parameters. */
28767 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28768 TMPL_PARMS_DEPTH (tparms));
28769 /* Discard the 'this' parameter. */
28770 fparms = FUNCTION_ARG_CHAIN (ctor);
28771 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28772 ci = get_constraints (ctor);
28773 loc = DECL_SOURCE_LOCATION (ctor);
28774 explicit_p = DECL_NONCONVERTING_P (ctor);
28775
28776 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28777 {
28778 memtmpl = true;
28779
28780 /* For a member template constructor, we need to flatten the two
28781 template parameter lists into one, and then adjust the function
28782 signature accordingly. This gets...complicated. */
28783 tree save_parms = current_template_parms;
28784
28785 /* For a member template we should have two levels of parms/args, one
28786 for the class and one for the constructor. We stripped
28787 specialized args for further enclosing classes above. */
28788 const int depth = 2;
28789 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28790
28791 /* Template args for translating references to the two-level template
28792 parameters into references to the one-level template parameters we
28793 are creating. */
28794 tree tsubst_args = copy_node (targs);
28795 TMPL_ARGS_LEVEL (tsubst_args, depth)
28796 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28797
28798 /* Template parms for the constructor template. */
28799 tree ftparms = TREE_VALUE (tparms);
28800 unsigned flen = TREE_VEC_LENGTH (ftparms);
28801 /* Template parms for the class template. */
28802 tparms = TREE_CHAIN (tparms);
28803 tree ctparms = TREE_VALUE (tparms);
28804 unsigned clen = TREE_VEC_LENGTH (ctparms);
28805 /* Template parms for the deduction guide start as a copy of the
28806 template parms for the class. We set current_template_parms for
28807 lookup_template_class_1. */
28808 current_template_parms = tparms = copy_node (tparms);
28809 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28810 for (unsigned i = 0; i < clen; ++i)
28811 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28812
28813 /* Now we need to rewrite the constructor parms to append them to the
28814 class parms. */
28815 for (unsigned i = 0; i < flen; ++i)
28816 {
28817 unsigned index = i + clen;
28818 unsigned level = 1;
28819 tree oldelt = TREE_VEC_ELT (ftparms, i);
28820 tree newelt
28821 = rewrite_tparm_list (oldelt, index, level,
28822 tsubst_args, i, complain);
28823 if (newelt == error_mark_node)
28824 ok = false;
28825 TREE_VEC_ELT (new_vec, index) = newelt;
28826 }
28827
28828 /* Now we have a final set of template parms to substitute into the
28829 function signature. */
28830 targs = template_parms_to_args (tparms);
28831 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28832 complain, ctor);
28833 if (fparms == error_mark_node)
28834 ok = false;
28835 if (ci)
28836 {
28837 if (outer_args)
28838 /* FIXME: We'd like to avoid substituting outer template
28839 arguments into the constraint ahead of time, but the
28840 construction of tsubst_args assumes that outer arguments
28841 are already substituted in. */
28842 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
28843 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28844 }
28845
28846 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28847 cp_unevaluated_operand. */
28848 cp_evaluated ev;
28849 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28850 current_template_parms = save_parms;
28851 }
28852 else
28853 {
28854 /* Substitute in the same arguments to rewrite class members into
28855 references to members of an unknown specialization. */
28856 cp_evaluated ev;
28857 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28858 fargs = tsubst (fargs, targs, complain, ctor);
28859 if (ci)
28860 {
28861 if (outer_args)
28862 /* FIXME: As above. */
28863 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
28864 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28865 }
28866 }
28867
28868 --processing_template_decl;
28869 if (!ok)
28870 return error_mark_node;
28871 }
28872
28873 if (!memtmpl)
28874 {
28875 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28876 tparms = copy_node (tparms);
28877 INNERMOST_TEMPLATE_PARMS (tparms)
28878 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28879 }
28880
28881 tree fntype = build_function_type (type, fparms);
28882 tree ded_fn = build_lang_decl_loc (loc,
28883 FUNCTION_DECL,
28884 dguide_name (type), fntype);
28885 DECL_ARGUMENTS (ded_fn) = fargs;
28886 DECL_ARTIFICIAL (ded_fn) = true;
28887 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28888 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28889 DECL_ARTIFICIAL (ded_tmpl) = true;
28890 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28891 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28892 if (DECL_P (ctor))
28893 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28894 if (ci)
28895 set_constraints (ded_tmpl, ci);
28896
28897 return ded_tmpl;
28898 }
28899
28900 /* Add to LIST the member types for the reshaped initializer CTOR. */
28901
28902 static tree
28903 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28904 {
28905 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28906 tree idx, val; unsigned i;
28907 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28908 {
28909 tree ftype = elt ? elt : TREE_TYPE (idx);
28910 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28911 && CONSTRUCTOR_BRACES_ELIDED_P (val))
28912 {
28913 tree subelt = NULL_TREE;
28914 if (TREE_CODE (ftype) == ARRAY_TYPE)
28915 subelt = TREE_TYPE (ftype);
28916 list = collect_ctor_idx_types (val, list, subelt);
28917 continue;
28918 }
28919 tree arg = NULL_TREE;
28920 if (i == v->length() - 1
28921 && PACK_EXPANSION_P (ftype))
28922 /* Give the trailing pack expansion parameter a default argument to
28923 match aggregate initialization behavior, even if we deduce the
28924 length of the pack separately to more than we have initializers. */
28925 arg = build_constructor (init_list_type_node, NULL);
28926 /* if ei is of array type and xi is a braced-init-list or string literal,
28927 Ti is an rvalue reference to the declared type of ei */
28928 STRIP_ANY_LOCATION_WRAPPER (val);
28929 if (TREE_CODE (ftype) == ARRAY_TYPE
28930 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28931 || TREE_CODE (val) == STRING_CST))
28932 {
28933 if (TREE_CODE (val) == STRING_CST)
28934 ftype = cp_build_qualified_type
28935 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28936 ftype = (cp_build_reference_type
28937 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28938 }
28939 list = tree_cons (arg, ftype, list);
28940 }
28941
28942 return list;
28943 }
28944
28945 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28946
28947 static bool
28948 is_spec_or_derived (tree etype, tree tmpl)
28949 {
28950 if (!etype || !CLASS_TYPE_P (etype))
28951 return false;
28952
28953 etype = cv_unqualified (etype);
28954 tree type = TREE_TYPE (tmpl);
28955 tree tparms = (INNERMOST_TEMPLATE_PARMS
28956 (DECL_TEMPLATE_PARMS (tmpl)));
28957 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28958 int err = unify (tparms, targs, type, etype,
28959 UNIFY_ALLOW_DERIVED, /*explain*/false);
28960 ggc_free (targs);
28961 return !err;
28962 }
28963
28964 static tree alias_ctad_tweaks (tree, tree);
28965
28966 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28967 INIT. */
28968
28969 static tree
28970 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28971 {
28972 if (cxx_dialect < cxx20)
28973 return NULL_TREE;
28974
28975 if (init == NULL_TREE)
28976 return NULL_TREE;
28977
28978 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28979 {
28980 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28981 tree tinfo = get_template_info (under);
28982 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
28983 return alias_ctad_tweaks (tmpl, guide);
28984 return NULL_TREE;
28985 }
28986
28987 /* We might be creating a guide for a class member template, e.g.,
28988
28989 template<typename U> struct A {
28990 template<typename T> struct B { T t; };
28991 };
28992
28993 At this point, A will have been instantiated. Below, we need to
28994 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
28995 const bool member_template_p
28996 = (DECL_TEMPLATE_INFO (tmpl)
28997 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
28998 tree type = TREE_TYPE (tmpl);
28999 tree template_type = (member_template_p
29000 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
29001 : type);
29002 if (!CP_AGGREGATE_TYPE_P (template_type))
29003 return NULL_TREE;
29004
29005 /* No aggregate candidate for copy-initialization. */
29006 if (args->length() == 1)
29007 {
29008 tree val = (*args)[0];
29009 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
29010 return NULL_TREE;
29011 }
29012
29013 /* If we encounter a problem, we just won't add the candidate. */
29014 tsubst_flags_t complain = tf_none;
29015
29016 tree parms = NULL_TREE;
29017 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29018 {
29019 init = reshape_init (template_type, init, complain);
29020 if (init == error_mark_node)
29021 return NULL_TREE;
29022 parms = collect_ctor_idx_types (init, parms);
29023 /* If we're creating a deduction guide for a member class template,
29024 we've used the original template pattern type for the reshape_init
29025 above; this is done because we want PARMS to be a template parameter
29026 type, something that can be deduced when used as a function template
29027 parameter. At this point the outer class template has already been
29028 partially instantiated (we deferred the deduction until the enclosing
29029 scope is non-dependent). Therefore we have to partially instantiate
29030 PARMS, so that its template level is properly reduced and we don't get
29031 mismatches when deducing types using the guide with PARMS. */
29032 if (member_template_p)
29033 parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
29034 }
29035 else if (TREE_CODE (init) == TREE_LIST)
29036 {
29037 int len = list_length (init);
29038 for (tree field = TYPE_FIELDS (type);
29039 len;
29040 --len, field = DECL_CHAIN (field))
29041 {
29042 field = next_initializable_field (field);
29043 if (!field)
29044 return NULL_TREE;
29045 tree ftype = finish_decltype_type (field, true, complain);
29046 parms = tree_cons (NULL_TREE, ftype, parms);
29047 }
29048 }
29049 else
29050 /* Aggregate initialization doesn't apply to an initializer expression. */
29051 return NULL_TREE;
29052
29053 if (parms)
29054 {
29055 tree last = parms;
29056 parms = nreverse (parms);
29057 TREE_CHAIN (last) = void_list_node;
29058 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
29059 return guide;
29060 }
29061
29062 return NULL_TREE;
29063 }
29064
29065 /* UGUIDES are the deduction guides for the underlying template of alias
29066 template TMPL; adjust them to be deduction guides for TMPL. */
29067
29068 static tree
29069 alias_ctad_tweaks (tree tmpl, tree uguides)
29070 {
29071 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29072 class type (9.2.8.2) where the template-name names an alias template A,
29073 the defining-type-id of A must be of the form
29074
29075 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29076
29077 as specified in 9.2.8.2. The guides of A are the set of functions or
29078 function templates formed as follows. For each function or function
29079 template f in the guides of the template named by the simple-template-id
29080 of the defining-type-id, the template arguments of the return type of f
29081 are deduced from the defining-type-id of A according to the process in
29082 13.10.2.5 with the exception that deduction does not fail if not all
29083 template arguments are deduced. Let g denote the result of substituting
29084 these deductions into f. If substitution succeeds, form a function or
29085 function template f' with the following properties and add it to the set
29086 of guides of A:
29087
29088 * The function type of f' is the function type of g.
29089
29090 * If f is a function template, f' is a function template whose template
29091 parameter list consists of all the template parameters of A (including
29092 their default template arguments) that appear in the above deductions or
29093 (recursively) in their default template arguments, followed by the
29094 template parameters of f that were not deduced (including their default
29095 template arguments), otherwise f' is not a function template.
29096
29097 * The associated constraints (13.5.2) are the conjunction of the
29098 associated constraints of g and a constraint that is satisfied if and only
29099 if the arguments of A are deducible (see below) from the return type.
29100
29101 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29102 be so as well.
29103
29104 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29105 considered to be so as well.
29106
29107 * The explicit-specifier of f' is the explicit-specifier of g (if
29108 any). */
29109
29110 /* This implementation differs from the above in two significant ways:
29111
29112 1) We include all template parameters of A, not just some.
29113 2) The added constraint is same_type instead of deducible.
29114
29115 I believe that while it's probably possible to construct a testcase that
29116 behaves differently with this simplification, it should have the same
29117 effect for real uses. Including all template parameters means that we
29118 deduce all parameters of A when resolving the call, so when we're in the
29119 constraint we don't need to deduce them again, we can just check whether
29120 the deduction produced the desired result. */
29121
29122 tsubst_flags_t complain = tf_warning_or_error;
29123 tree atype = TREE_TYPE (tmpl);
29124 tree aguides = NULL_TREE;
29125 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
29126 unsigned natparms = TREE_VEC_LENGTH (atparms);
29127 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29128 for (ovl_iterator iter (uguides); iter; ++iter)
29129 {
29130 tree f = *iter;
29131 tree in_decl = f;
29132 location_t loc = DECL_SOURCE_LOCATION (f);
29133 tree ret = TREE_TYPE (TREE_TYPE (f));
29134 tree fprime = f;
29135 if (TREE_CODE (f) == TEMPLATE_DECL)
29136 {
29137 processing_template_decl_sentinel ptds (/*reset*/false);
29138 ++processing_template_decl;
29139
29140 /* Deduce template arguments for f from the type-id of A. */
29141 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
29142 unsigned len = TREE_VEC_LENGTH (ftparms);
29143 tree targs = make_tree_vec (len);
29144 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
29145 if (err)
29146 continue;
29147
29148 /* The number of parms for f' is the number of parms for A plus
29149 non-deduced parms of f. */
29150 unsigned ndlen = 0;
29151 unsigned j;
29152 for (unsigned i = 0; i < len; ++i)
29153 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29154 ++ndlen;
29155 tree gtparms = make_tree_vec (natparms + ndlen);
29156
29157 /* First copy over the parms of A. */
29158 for (j = 0; j < natparms; ++j)
29159 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
29160 /* Now rewrite the non-deduced parms of f. */
29161 for (unsigned i = 0; ndlen && i < len; ++i)
29162 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29163 {
29164 --ndlen;
29165 unsigned index = j++;
29166 unsigned level = 1;
29167 tree oldlist = TREE_VEC_ELT (ftparms, i);
29168 tree list = rewrite_tparm_list (oldlist, index, level,
29169 targs, i, complain);
29170 TREE_VEC_ELT (gtparms, index) = list;
29171 }
29172 gtparms = build_tree_list (size_one_node, gtparms);
29173
29174 /* Substitute the deduced arguments plus the rewritten template
29175 parameters into f to get g. This covers the type, copyness,
29176 guideness, and explicit-specifier. */
29177 tree g;
29178 {
29179 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
29180 if cp_unevaluated_operand. */
29181 cp_evaluated ev;
29182 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
29183 }
29184 if (g == error_mark_node)
29185 continue;
29186 DECL_USE_TEMPLATE (g) = 0;
29187 fprime = build_template_decl (g, gtparms, false);
29188 DECL_TEMPLATE_RESULT (fprime) = g;
29189 TREE_TYPE (fprime) = TREE_TYPE (g);
29190 tree gtargs = template_parms_to_args (gtparms);
29191 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
29192 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
29193
29194 /* Substitute the associated constraints. */
29195 tree ci = get_constraints (f);
29196 if (ci)
29197 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
29198 if (ci == error_mark_node)
29199 continue;
29200
29201 /* Add a constraint that the return type matches the instantiation of
29202 A with the same template arguments. */
29203 ret = TREE_TYPE (TREE_TYPE (fprime));
29204 if (!same_type_p (atype, ret)
29205 /* FIXME this should mean they don't compare as equivalent. */
29206 || dependent_alias_template_spec_p (atype, nt_opaque))
29207 {
29208 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
29209 ci = append_constraint (ci, same);
29210 }
29211
29212 if (ci)
29213 {
29214 remove_constraints (fprime);
29215 set_constraints (fprime, ci);
29216 }
29217 }
29218 else
29219 {
29220 /* For a non-template deduction guide, if the arguments of A aren't
29221 deducible from the return type, don't add the candidate. */
29222 tree targs = make_tree_vec (natparms);
29223 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
29224 for (unsigned i = 0; !err && i < natparms; ++i)
29225 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29226 err = true;
29227 if (err)
29228 continue;
29229 }
29230
29231 aguides = lookup_add (fprime, aguides);
29232 }
29233
29234 return aguides;
29235 }
29236
29237 /* Return artificial deduction guides built from the constructors of class
29238 template TMPL. */
29239
29240 static tree
29241 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
29242 {
29243 tree type = TREE_TYPE (tmpl);
29244 tree outer_args = NULL_TREE;
29245 if (DECL_CLASS_SCOPE_P (tmpl)
29246 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
29247 {
29248 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
29249 type = TREE_TYPE (most_general_template (tmpl));
29250 }
29251
29252 tree cands = NULL_TREE;
29253
29254 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
29255 {
29256 /* Skip inherited constructors. */
29257 if (iter.using_p ())
29258 continue;
29259
29260 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
29261 cands = lookup_add (guide, cands);
29262 }
29263
29264 /* Add implicit default constructor deduction guide. */
29265 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
29266 {
29267 tree guide = build_deduction_guide (type, type, outer_args,
29268 complain);
29269 cands = lookup_add (guide, cands);
29270 }
29271
29272 /* Add copy guide. */
29273 {
29274 tree gtype = build_reference_type (type);
29275 tree guide = build_deduction_guide (type, gtype, outer_args,
29276 complain);
29277 cands = lookup_add (guide, cands);
29278 }
29279
29280 return cands;
29281 }
29282
29283 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29284
29285 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29286 aggregate candidate is added separately because it depends on the
29287 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29288 guide. */
29289
29290 static tree
29291 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29292 {
29293 tree guides = NULL_TREE;
29294 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29295 {
29296 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29297 tree tinfo = get_template_info (under);
29298 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29299 complain);
29300 }
29301 else
29302 {
29303 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29304 dguide_name (tmpl),
29305 LOOK_want::NORMAL, /*complain*/false);
29306 if (guides == error_mark_node)
29307 guides = NULL_TREE;
29308 else
29309 any_dguides_p = true;
29310 }
29311
29312 /* Cache the deduction guides for a template. We also remember the result of
29313 lookup, and rebuild everything if it changes; should be very rare. */
29314 tree_pair_p cache = NULL;
29315 if (tree_pair_p &r
29316 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29317 {
29318 cache = r;
29319 if (cache->purpose == guides)
29320 return cache->value;
29321 }
29322 else
29323 {
29324 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29325 cache->purpose = guides;
29326 }
29327
29328 tree cands = NULL_TREE;
29329 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29330 cands = alias_ctad_tweaks (tmpl, guides);
29331 else
29332 {
29333 cands = ctor_deduction_guides_for (tmpl, complain);
29334 for (ovl_iterator it (guides); it; ++it)
29335 cands = lookup_add (*it, cands);
29336 }
29337
29338 cache->value = cands;
29339 return cands;
29340 }
29341
29342 /* Return whether TMPL is a (class template argument-) deducible template. */
29343
29344 bool
29345 ctad_template_p (tree tmpl)
29346 {
29347 /* A deducible template is either a class template or is an alias template
29348 whose defining-type-id is of the form
29349
29350 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29351
29352 where the nested-name-specifier (if any) is non-dependent and the
29353 template-name of the simple-template-id names a deducible template. */
29354
29355 if (DECL_CLASS_TEMPLATE_P (tmpl)
29356 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29357 return true;
29358 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29359 return false;
29360 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29361 if (tree tinfo = get_template_info (orig))
29362 return ctad_template_p (TI_TEMPLATE (tinfo));
29363 return false;
29364 }
29365
29366 /* Deduce template arguments for the class template placeholder PTYPE for
29367 template TMPL based on the initializer INIT, and return the resulting
29368 type. */
29369
29370 static tree
29371 do_class_deduction (tree ptype, tree tmpl, tree init,
29372 int flags, tsubst_flags_t complain)
29373 {
29374 /* We should have handled this in the caller. */
29375 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29376 return ptype;
29377
29378 /* Wait until the enclosing scope is non-dependent. */
29379 if (DECL_CLASS_SCOPE_P (tmpl)
29380 && dependent_type_p (DECL_CONTEXT (tmpl)))
29381 return ptype;
29382
29383 /* Initializing one placeholder from another. */
29384 if (init
29385 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
29386 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
29387 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
29388 == TEMPLATE_PARM_INDEX)))
29389 && is_auto (TREE_TYPE (init))
29390 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29391 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29392
29393 /* Look through alias templates that just rename another template. */
29394 tmpl = get_underlying_template (tmpl);
29395 if (!ctad_template_p (tmpl))
29396 {
29397 if (complain & tf_error)
29398 error ("non-deducible template %qT used without template arguments", tmpl);
29399 return error_mark_node;
29400 }
29401 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29402 {
29403 if (complain & tf_error)
29404 error ("alias template deduction only available "
29405 "with %<-std=c++20%> or %<-std=gnu++20%>");
29406 return error_mark_node;
29407 }
29408
29409 /* Wait until the initializer is non-dependent. */
29410 if (type_dependent_expression_p (init))
29411 return ptype;
29412
29413 tree type = TREE_TYPE (tmpl);
29414
29415 bool try_list_ctor = false;
29416 bool list_init_p = false;
29417
29418 releasing_vec rv_args = NULL;
29419 vec<tree,va_gc> *&args = *&rv_args;
29420 if (init == NULL_TREE)
29421 args = make_tree_vector ();
29422 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29423 {
29424 list_init_p = true;
29425 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29426 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
29427 {
29428 /* As an exception, the first phase in 16.3.1.7 (considering the
29429 initializer list as a single argument) is omitted if the
29430 initializer list consists of a single expression of type cv U,
29431 where U is a specialization of C or a class derived from a
29432 specialization of C. */
29433 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29434 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29435 try_list_ctor = false;
29436 }
29437 if (try_list_ctor || is_std_init_list (type))
29438 args = make_tree_vector_single (init);
29439 else
29440 args = make_tree_vector_from_ctor (init);
29441 }
29442 else if (TREE_CODE (init) == TREE_LIST)
29443 args = make_tree_vector_from_list (init);
29444 else
29445 args = make_tree_vector_single (init);
29446
29447 /* Do this now to avoid problems with erroneous args later on. */
29448 args = resolve_args (args, complain);
29449 if (args == NULL)
29450 return error_mark_node;
29451
29452 bool any_dguides_p = false;
29453 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29454 if (cands == error_mark_node)
29455 return error_mark_node;
29456
29457 /* Prune explicit deduction guides in copy-initialization context (but
29458 not copy-list-initialization). */
29459 bool elided = false;
29460 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29461 {
29462 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29463 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29464 elided = true;
29465
29466 if (elided)
29467 {
29468 /* Found a nonconverting guide, prune the candidates. */
29469 tree pruned = NULL_TREE;
29470 for (lkp_iterator iter (cands); iter; ++iter)
29471 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29472 pruned = lookup_add (*iter, pruned);
29473
29474 cands = pruned;
29475 }
29476 }
29477
29478 if (!any_dguides_p)
29479 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29480 cands = lookup_add (guide, cands);
29481
29482 tree call = error_mark_node;
29483
29484 /* If this is list-initialization and the class has a list constructor, first
29485 try deducing from the list as a single argument, as [over.match.list]. */
29486 tree list_cands = NULL_TREE;
29487 if (try_list_ctor && cands)
29488 for (lkp_iterator iter (cands); iter; ++iter)
29489 {
29490 tree dg = *iter;
29491 if (is_list_ctor (dg))
29492 list_cands = lookup_add (dg, list_cands);
29493 }
29494 if (list_cands)
29495 {
29496 ++cp_unevaluated_operand;
29497 call = build_new_function_call (list_cands, &args, tf_decltype);
29498 --cp_unevaluated_operand;
29499
29500 if (call == error_mark_node)
29501 {
29502 /* That didn't work, now try treating the list as a sequence of
29503 arguments. */
29504 release_tree_vector (args);
29505 args = make_tree_vector_from_ctor (init);
29506 }
29507 }
29508
29509 if (elided && !cands)
29510 {
29511 error ("cannot deduce template arguments for copy-initialization"
29512 " of %qT, as it has no non-explicit deduction guides or "
29513 "user-declared constructors", type);
29514 return error_mark_node;
29515 }
29516 else if (!cands && call == error_mark_node)
29517 {
29518 error ("cannot deduce template arguments of %qT, as it has no viable "
29519 "deduction guides", type);
29520 return error_mark_node;
29521 }
29522
29523 if (call == error_mark_node)
29524 {
29525 ++cp_unevaluated_operand;
29526 call = build_new_function_call (cands, &args, tf_decltype);
29527 --cp_unevaluated_operand;
29528 }
29529
29530 if (call == error_mark_node)
29531 {
29532 if (complain & tf_warning_or_error)
29533 {
29534 error ("class template argument deduction failed:");
29535
29536 ++cp_unevaluated_operand;
29537 call = build_new_function_call (cands, &args,
29538 complain | tf_decltype);
29539 --cp_unevaluated_operand;
29540
29541 if (elided)
29542 inform (input_location, "explicit deduction guides not considered "
29543 "for copy-initialization");
29544 }
29545 return error_mark_node;
29546 }
29547 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29548 constructor is chosen, the initialization is ill-formed. */
29549 else if (flags & LOOKUP_ONLYCONVERTING)
29550 {
29551 tree fndecl = cp_get_callee_fndecl_nofold (call);
29552 if (fndecl && DECL_NONCONVERTING_P (fndecl))
29553 {
29554 if (complain & tf_warning_or_error)
29555 {
29556 // TODO: Pass down location from cp_finish_decl.
29557 error ("class template argument deduction for %qT failed: "
29558 "explicit deduction guide selected in "
29559 "copy-list-initialization", type);
29560 inform (DECL_SOURCE_LOCATION (fndecl),
29561 "explicit deduction guide declared here");
29562
29563 }
29564 return error_mark_node;
29565 }
29566 }
29567
29568 /* If CTAD succeeded but the type doesn't have any explicit deduction
29569 guides, this deduction might not be what the user intended. */
29570 if (call != error_mark_node && !any_dguides_p)
29571 {
29572 tree fndecl = cp_get_callee_fndecl_nofold (call);
29573 if (fndecl != NULL_TREE
29574 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29575 || global_dc->dc_warn_system_headers)
29576 && warning (OPT_Wctad_maybe_unsupported,
29577 "%qT may not intend to support class template argument "
29578 "deduction", type))
29579 inform (input_location, "add a deduction guide to suppress this "
29580 "warning");
29581 }
29582
29583 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29584 }
29585
29586 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29587 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29588 The CONTEXT determines the context in which auto deduction is performed
29589 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29590
29591 OUTER_TARGS is used during template argument deduction (context == adc_unify)
29592 to properly substitute the result. It's also used in the adc_unify and
29593 adc_requirement contexts to communicate the the necessary template arguments
29594 to satisfaction. OUTER_TARGS is ignored in other contexts.
29595
29596 For partial-concept-ids, extra args may be appended to the list of deduced
29597 template arguments prior to determining constraint satisfaction. */
29598
29599 tree
29600 do_auto_deduction (tree type, tree init, tree auto_node,
29601 tsubst_flags_t complain, auto_deduction_context context,
29602 tree outer_targs, int flags)
29603 {
29604 if (init == error_mark_node)
29605 return error_mark_node;
29606
29607 if (init && type_dependent_expression_p (init)
29608 && context != adc_unify)
29609 /* Defining a subset of type-dependent expressions that we can deduce
29610 from ahead of time isn't worth the trouble. */
29611 return type;
29612
29613 /* Similarly, we can't deduce from another undeduced decl. */
29614 if (init && undeduced_auto_decl (init))
29615 return type;
29616
29617 /* We may be doing a partial substitution, but we still want to replace
29618 auto_node. */
29619 complain &= ~tf_partial;
29620
29621 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29622 /* C++17 class template argument deduction. */
29623 return do_class_deduction (type, tmpl, init, flags, complain);
29624
29625 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29626 /* Nothing we can do with this, even in deduction context. */
29627 return type;
29628
29629 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29630 with either a new invented type template parameter U or, if the
29631 initializer is a braced-init-list (8.5.4), with
29632 std::initializer_list<U>. */
29633 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29634 {
29635 if (!DIRECT_LIST_INIT_P (init))
29636 type = listify_autos (type, auto_node);
29637 else if (CONSTRUCTOR_NELTS (init) == 1)
29638 init = CONSTRUCTOR_ELT (init, 0)->value;
29639 else
29640 {
29641 if (complain & tf_warning_or_error)
29642 {
29643 if (permerror (input_location, "direct-list-initialization of "
29644 "%<auto%> requires exactly one element"))
29645 inform (input_location,
29646 "for deduction to %<std::initializer_list%>, use copy-"
29647 "list-initialization (i.e. add %<=%> before the %<{%>)");
29648 }
29649 type = listify_autos (type, auto_node);
29650 }
29651 }
29652
29653 if (type == error_mark_node)
29654 return error_mark_node;
29655
29656 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29657 {
29658 /* We don't recurse here because we can't deduce from a nested
29659 initializer_list. */
29660 if (CONSTRUCTOR_ELTS (init))
29661 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
29662 elt.value = resolve_nondeduced_context (elt.value, complain);
29663 }
29664 else
29665 init = resolve_nondeduced_context (init, complain);
29666
29667 tree targs;
29668 if (context == adc_decomp_type
29669 && auto_node == type
29670 && init != error_mark_node
29671 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29672 {
29673 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
29674 and initializer has array type, deduce cv-qualified array type. */
29675 targs = make_tree_vec (1);
29676 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
29677 }
29678 else if (AUTO_IS_DECLTYPE (auto_node))
29679 {
29680 tree stripped_init = tree_strip_any_location_wrapper (init);
29681 if (REFERENCE_REF_P (stripped_init))
29682 stripped_init = TREE_OPERAND (stripped_init, 0);
29683 bool id = (DECL_P (stripped_init)
29684 || ((TREE_CODE (init) == COMPONENT_REF
29685 || TREE_CODE (init) == SCOPE_REF)
29686 && !REF_PARENTHESIZED_P (init)));
29687 tree deduced = finish_decltype_type (init, id, complain);
29688 deduced = canonicalize_type_argument (deduced, complain);
29689 if (deduced == error_mark_node)
29690 return error_mark_node;
29691 targs = make_tree_vec (1);
29692 TREE_VEC_ELT (targs, 0) = deduced;
29693 }
29694 else
29695 {
29696 if (error_operand_p (init))
29697 return error_mark_node;
29698
29699 tree parms = build_tree_list (NULL_TREE, type);
29700 tree tparms;
29701
29702 if (flag_concepts_ts)
29703 tparms = extract_autos (type);
29704 else
29705 {
29706 tparms = make_tree_vec (1);
29707 TREE_VEC_ELT (tparms, 0)
29708 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29709 }
29710
29711 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29712 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29713 DEDUCE_CALL,
29714 NULL, /*explain_p=*/false);
29715 if (val > 0)
29716 {
29717 if (processing_template_decl)
29718 /* Try again at instantiation time. */
29719 return type;
29720 if (type && type != error_mark_node
29721 && (complain & tf_error))
29722 /* If type is error_mark_node a diagnostic must have been
29723 emitted by now. Also, having a mention to '<type error>'
29724 in the diagnostic is not really useful to the user. */
29725 {
29726 if (cfun
29727 && FNDECL_USED_AUTO (current_function_decl)
29728 && (auto_node
29729 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29730 && LAMBDA_FUNCTION_P (current_function_decl))
29731 error ("unable to deduce lambda return type from %qE", init);
29732 else
29733 error ("unable to deduce %qT from %qE", type, init);
29734 type_unification_real (tparms, targs, parms, &init, 1, 0,
29735 DEDUCE_CALL,
29736 NULL, /*explain_p=*/true);
29737 }
29738 return error_mark_node;
29739 }
29740 }
29741
29742 /* Check any placeholder constraints against the deduced type. */
29743 if (processing_template_decl && context == adc_unify)
29744 /* Constraints will be checked after deduction. */;
29745 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29746 {
29747 if (processing_template_decl)
29748 {
29749 gcc_checking_assert (context == adc_variable_type
29750 || context == adc_return_type
29751 || context == adc_decomp_type);
29752 gcc_checking_assert (!type_dependent_expression_p (init));
29753 /* If the constraint is dependent, we need to wait until
29754 instantiation time to resolve the placeholder. */
29755 if (placeholder_type_constraint_dependent_p (constr))
29756 return type;
29757 }
29758
29759 if ((context == adc_return_type
29760 || context == adc_variable_type
29761 || context == adc_decomp_type)
29762 && current_function_decl
29763 && DECL_TEMPLATE_INFO (current_function_decl))
29764 outer_targs = DECL_TI_ARGS (current_function_decl);
29765
29766 tree full_targs = add_to_template_args (outer_targs, targs);
29767
29768 /* HACK: Compensate for callers not always communicating all levels of
29769 outer template arguments by filling in the outermost missing levels
29770 with dummy levels before checking satisfaction. We'll still crash
29771 if the constraint depends on a template argument belonging to one of
29772 these missing levels, but this hack otherwise allows us to handle a
29773 large subset of possible constraints (including all non-dependent
29774 constraints). */
29775 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
29776 - TMPL_ARGS_DEPTH (full_targs)))
29777 {
29778 tree dummy_levels = make_tree_vec (missing_levels);
29779 for (int i = 0; i < missing_levels; ++i)
29780 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
29781 full_targs = add_to_template_args (dummy_levels, full_targs);
29782 }
29783
29784 if (!constraints_satisfied_p (auto_node, full_targs))
29785 {
29786 if (complain & tf_warning_or_error)
29787 {
29788 auto_diagnostic_group d;
29789 switch (context)
29790 {
29791 case adc_unspecified:
29792 case adc_unify:
29793 error("placeholder constraints not satisfied");
29794 break;
29795 case adc_variable_type:
29796 case adc_decomp_type:
29797 error ("deduced initializer does not satisfy "
29798 "placeholder constraints");
29799 break;
29800 case adc_return_type:
29801 error ("deduced return type does not satisfy "
29802 "placeholder constraints");
29803 break;
29804 case adc_requirement:
29805 error ("deduced expression type does not satisfy "
29806 "placeholder constraints");
29807 break;
29808 }
29809 diagnose_constraints (input_location, auto_node, full_targs);
29810 }
29811 return error_mark_node;
29812 }
29813 }
29814
29815 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
29816 /* The outer template arguments are already substituted into type
29817 (but we still may have used them for constraint checking above). */;
29818 else if (context == adc_unify)
29819 targs = add_to_template_args (outer_targs, targs);
29820 else if (processing_template_decl)
29821 targs = add_to_template_args (current_template_args (), targs);
29822 return tsubst (type, targs, complain, NULL_TREE);
29823 }
29824
29825 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29826 result. */
29827
29828 tree
29829 splice_late_return_type (tree type, tree late_return_type)
29830 {
29831 if (late_return_type)
29832 {
29833 gcc_assert (is_auto (type) || seen_error ());
29834 return late_return_type;
29835 }
29836
29837 if (tree auto_node = find_type_usage (type, is_auto))
29838 if (TEMPLATE_TYPE_LEVEL (auto_node) <= processing_template_decl)
29839 {
29840 /* In an abbreviated function template we didn't know we were dealing
29841 with a function template when we saw the auto return type, so rebuild
29842 the return type using an auto with the correct level. */
29843 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
29844 tree auto_vec = make_tree_vec (1);
29845 TREE_VEC_ELT (auto_vec, 0) = new_auto;
29846 tree targs = add_outermost_template_args (current_template_args (),
29847 auto_vec);
29848 /* Also rebuild the constraint info in terms of the new auto. */
29849 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
29850 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
29851 = build_tree_list (current_template_parms,
29852 tsubst_constraint (TREE_VALUE (ci), targs,
29853 tf_none, NULL_TREE));
29854 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29855 return tsubst (type, targs, tf_none, NULL_TREE);
29856 }
29857 return type;
29858 }
29859
29860 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29861 'decltype(auto)' or a deduced class template. */
29862
29863 bool
29864 is_auto (const_tree type)
29865 {
29866 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29867 && (TYPE_IDENTIFIER (type) == auto_identifier
29868 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29869 return true;
29870 else
29871 return false;
29872 }
29873
29874 /* for_each_template_parm callback for type_uses_auto. */
29875
29876 int
29877 is_auto_r (tree tp, void */*data*/)
29878 {
29879 return is_auto (tp);
29880 }
29881
29882 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29883 a use of `auto'. Returns NULL_TREE otherwise. */
29884
29885 tree
29886 type_uses_auto (tree type)
29887 {
29888 if (type == NULL_TREE)
29889 return NULL_TREE;
29890 else if (flag_concepts_ts)
29891 {
29892 /* The Concepts TS allows multiple autos in one type-specifier; just
29893 return the first one we find, do_auto_deduction will collect all of
29894 them. */
29895 if (uses_template_parms (type))
29896 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29897 /*visited*/NULL, /*nondeduced*/false);
29898 else
29899 return NULL_TREE;
29900 }
29901 else
29902 return find_type_usage (type, is_auto);
29903 }
29904
29905 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29906 concepts are enabled, auto is acceptable in template arguments, but
29907 only when TEMPL identifies a template class. Return TRUE if any
29908 such errors were reported. */
29909
29910 bool
29911 check_auto_in_tmpl_args (tree tmpl, tree args)
29912 {
29913 if (!flag_concepts_ts)
29914 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
29915 have already been rejected by the parser more generally. */
29916 return false;
29917
29918 /* If there were previous errors, nevermind. */
29919 if (!args || TREE_CODE (args) != TREE_VEC)
29920 return false;
29921
29922 /* If TMPL is an identifier, we're parsing and we can't tell yet
29923 whether TMPL is supposed to be a type, a function or a variable.
29924 We'll only be able to tell during template substitution, so we
29925 expect to be called again then. If concepts are enabled and we
29926 know we have a type, we're ok. */
29927 if (identifier_p (tmpl)
29928 || (DECL_P (tmpl)
29929 && (DECL_TYPE_TEMPLATE_P (tmpl)
29930 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
29931 return false;
29932
29933 /* Quickly search for any occurrences of auto; usually there won't
29934 be any, and then we'll avoid allocating the vector. */
29935 if (!type_uses_auto (args))
29936 return false;
29937
29938 bool errors = false;
29939
29940 tree vec = extract_autos (args);
29941 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29942 {
29943 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29944 error_at (DECL_SOURCE_LOCATION (xauto),
29945 "invalid use of %qT in template argument", xauto);
29946 errors = true;
29947 }
29948
29949 return errors;
29950 }
29951
29952 /* Recursively walk over && expressions searching for EXPR. Return a reference
29953 to that expression. */
29954
29955 static tree *find_template_requirement (tree *t, tree key)
29956 {
29957 if (*t == key)
29958 return t;
29959 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29960 {
29961 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29962 return p;
29963 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29964 return p;
29965 }
29966 return 0;
29967 }
29968
29969 /* Convert the generic type parameters in PARM that match the types given in the
29970 range [START_IDX, END_IDX) from the current_template_parms into generic type
29971 packs. */
29972
29973 tree
29974 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29975 {
29976 tree current = current_template_parms;
29977 int depth = TMPL_PARMS_DEPTH (current);
29978 current = INNERMOST_TEMPLATE_PARMS (current);
29979 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29980
29981 for (int i = 0; i < start_idx; ++i)
29982 TREE_VEC_ELT (replacement, i)
29983 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29984
29985 for (int i = start_idx; i < end_idx; ++i)
29986 {
29987 /* Create a distinct parameter pack type from the current parm and add it
29988 to the replacement args to tsubst below into the generic function
29989 parameter. */
29990 tree node = TREE_VEC_ELT (current, i);
29991 tree o = TREE_TYPE (TREE_VALUE (node));
29992 tree t = copy_type (o);
29993 TEMPLATE_TYPE_PARM_INDEX (t)
29994 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29995 t, 0, 0, tf_none);
29996 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29997 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29998 TYPE_MAIN_VARIANT (t) = t;
29999 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
30000 TYPE_CANONICAL (t) = canonical_type_parameter (t);
30001 TREE_VEC_ELT (replacement, i) = t;
30002
30003 /* Replace the current template parameter with new pack. */
30004 TREE_VALUE (node) = TREE_CHAIN (t);
30005
30006 /* Surgically adjust the associated constraint of adjusted parameter
30007 and it's corresponding contribution to the current template
30008 requirements. */
30009 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
30010 {
30011 tree id = unpack_concept_check (constr);
30012 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
30013 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
30014 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
30015
30016 /* If there was a constraint, we also need to replace that in
30017 the template requirements, which we've already built. */
30018 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
30019 reqs = find_template_requirement (reqs, constr);
30020 *reqs = fold;
30021 }
30022 }
30023
30024 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
30025 TREE_VEC_ELT (replacement, i)
30026 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
30027
30028 /* If there are more levels then build up the replacement with the outer
30029 template parms. */
30030 if (depth > 1)
30031 replacement = add_to_template_args (template_parms_to_args
30032 (TREE_CHAIN (current_template_parms)),
30033 replacement);
30034
30035 return tsubst (parm, replacement, tf_none, NULL_TREE);
30036 }
30037
30038 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30039 0..N-1. */
30040
30041 void
30042 declare_integer_pack (void)
30043 {
30044 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
30045 build_function_type_list (integer_type_node,
30046 integer_type_node,
30047 NULL_TREE),
30048 NULL_TREE, ECF_CONST);
30049 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
30050 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
30051 CP_BUILT_IN_INTEGER_PACK);
30052 }
30053
30054 /* Walk the decl or type specialization table calling FN on each
30055 entry. */
30056
30057 void
30058 walk_specializations (bool decls_p,
30059 void (*fn) (bool decls_p, spec_entry *entry, void *data),
30060 void *data)
30061 {
30062 spec_hash_table *table = decls_p ? decl_specializations
30063 : type_specializations;
30064 spec_hash_table::iterator end (table->end ());
30065 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
30066 fn (decls_p, *iter, data);
30067 }
30068
30069 /* Lookup the specialization of *ELT, in the decl or type
30070 specialization table. Return the SPEC that's already there, or
30071 NULL if nothing. */
30072
30073 tree
30074 match_mergeable_specialization (bool decl_p, spec_entry *elt)
30075 {
30076 hash_table<spec_hasher> *specializations
30077 = decl_p ? decl_specializations : type_specializations;
30078 hashval_t hash = spec_hasher::hash (elt);
30079 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
30080
30081 if (slot)
30082 return (*slot)->spec;
30083
30084 return NULL_TREE;
30085 }
30086
30087 /* Return flags encoding whether SPEC is on the instantiation and/or
30088 specialization lists of TMPL. */
30089
30090 unsigned
30091 get_mergeable_specialization_flags (tree tmpl, tree decl)
30092 {
30093 unsigned flags = 0;
30094
30095 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
30096 inst; inst = TREE_CHAIN (inst))
30097 if (TREE_VALUE (inst) == decl)
30098 {
30099 flags |= 1;
30100 break;
30101 }
30102
30103 if (CLASS_TYPE_P (TREE_TYPE (decl))
30104 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
30105 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
30106 /* Only need to search if DECL is a partial specialization. */
30107 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
30108 part; part = TREE_CHAIN (part))
30109 if (TREE_VALUE (part) == decl)
30110 {
30111 flags |= 2;
30112 break;
30113 }
30114
30115 return flags;
30116 }
30117
30118 /* Add a new specialization described by SPEC. DECL is the
30119 maybe-template decl and FLAGS is as returned from
30120 get_mergeable_specialization_flags. */
30121
30122 void
30123 add_mergeable_specialization (bool decl_p, bool alias_p, spec_entry *elt,
30124 tree decl, unsigned flags)
30125 {
30126 hashval_t hash = spec_hasher::hash (elt);
30127 if (decl_p)
30128 {
30129 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
30130
30131 gcc_checking_assert (!*slot);
30132 auto entry = ggc_alloc<spec_entry> ();
30133 *entry = *elt;
30134 *slot = entry;
30135
30136 if (alias_p)
30137 {
30138 elt->spec = TREE_TYPE (elt->spec);
30139 gcc_checking_assert (elt->spec);
30140 }
30141 }
30142
30143 if (!decl_p || alias_p)
30144 {
30145 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
30146
30147 /* We don't distinguish different constrained partial type
30148 specializations, so there could be duplicates. Everything else
30149 must be new. */
30150 if (!(flags & 2 && *slot))
30151 {
30152 gcc_checking_assert (!*slot);
30153
30154 auto entry = ggc_alloc<spec_entry> ();
30155 *entry = *elt;
30156 *slot = entry;
30157 }
30158 }
30159
30160 if (flags & 1)
30161 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
30162 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
30163
30164 if (flags & 2)
30165 {
30166 /* A partial specialization. */
30167 tree cons = tree_cons (elt->args, decl,
30168 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
30169 TREE_TYPE (cons) = elt->spec;
30170 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
30171 }
30172 }
30173
30174 /* Set up the hash tables for template instantiations. */
30175
30176 void
30177 init_template_processing (void)
30178 {
30179 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
30180 type_specializations = hash_table<spec_hasher>::create_ggc (37);
30181
30182 if (cxx_dialect >= cxx11)
30183 declare_integer_pack ();
30184 }
30185
30186 /* Print stats about the template hash tables for -fstats. */
30187
30188 void
30189 print_template_statistics (void)
30190 {
30191 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
30192 "%f collisions\n", (long) decl_specializations->size (),
30193 (long) decl_specializations->elements (),
30194 decl_specializations->collisions ());
30195 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
30196 "%f collisions\n", (long) type_specializations->size (),
30197 (long) type_specializations->elements (),
30198 type_specializations->collisions ());
30199 }
30200
30201 #if CHECKING_P
30202
30203 namespace selftest {
30204
30205 /* Verify that build_non_dependent_expr () works, for various expressions,
30206 and that location wrappers don't affect the results. */
30207
30208 static void
30209 test_build_non_dependent_expr ()
30210 {
30211 location_t loc = BUILTINS_LOCATION;
30212
30213 /* Verify constants, without and with location wrappers. */
30214 tree int_cst = build_int_cst (integer_type_node, 42);
30215 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
30216
30217 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
30218 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
30219 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
30220
30221 tree string_lit = build_string (4, "foo");
30222 TREE_TYPE (string_lit) = char_array_type_node;
30223 string_lit = fix_string_type (string_lit);
30224 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
30225
30226 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
30227 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
30228 ASSERT_EQ (wrapped_string_lit,
30229 build_non_dependent_expr (wrapped_string_lit));
30230 }
30231
30232 /* Verify that type_dependent_expression_p () works correctly, even
30233 in the presence of location wrapper nodes. */
30234
30235 static void
30236 test_type_dependent_expression_p ()
30237 {
30238 location_t loc = BUILTINS_LOCATION;
30239
30240 tree name = get_identifier ("foo");
30241
30242 /* If no templates are involved, nothing is type-dependent. */
30243 gcc_assert (!processing_template_decl);
30244 ASSERT_FALSE (type_dependent_expression_p (name));
30245
30246 ++processing_template_decl;
30247
30248 /* Within a template, an unresolved name is always type-dependent. */
30249 ASSERT_TRUE (type_dependent_expression_p (name));
30250
30251 /* Ensure it copes with NULL_TREE and errors. */
30252 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
30253 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
30254
30255 /* A USING_DECL in a template should be type-dependent, even if wrapped
30256 with a location wrapper (PR c++/83799). */
30257 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
30258 TREE_TYPE (using_decl) = integer_type_node;
30259 ASSERT_TRUE (type_dependent_expression_p (using_decl));
30260 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
30261 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
30262 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
30263
30264 --processing_template_decl;
30265 }
30266
30267 /* Run all of the selftests within this file. */
30268
30269 void
30270 cp_pt_c_tests ()
30271 {
30272 test_build_non_dependent_expr ();
30273 test_type_dependent_expression_p ();
30274 }
30275
30276 } // namespace selftest
30277
30278 #endif /* #if CHECKING_P */
30279
30280 #include "gt-cp-pt.h"