12d084031b1c772ba3916e0b0b11e7b602d9e710
[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 GTY(()) tree saved_trees;
69 static vec<int> inline_parm_levels;
70
71 static GTY(()) struct tinst_level *current_tinst_level;
72
73 static GTY(()) vec<tree, va_gc> *saved_access_scope;
74
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
79
80 // -------------------------------------------------------------------------- //
81 // Local Specialization Stack
82 //
83 // Implementation of the RAII helper for creating new local
84 // specializations.
85 local_specialization_stack::local_specialization_stack (lss_policy policy)
86 : saved (local_specializations)
87 {
88 if (policy == lss_nop)
89 ;
90 else if (policy == lss_blank || !saved)
91 local_specializations = new hash_map<tree, tree>;
92 else
93 local_specializations = new hash_map<tree, tree>(*saved);
94 }
95
96 local_specialization_stack::~local_specialization_stack ()
97 {
98 if (local_specializations != saved)
99 {
100 delete local_specializations;
101 local_specializations = saved;
102 }
103 }
104
105 /* True if we've recursed into fn_type_unification too many times. */
106 static bool excessive_deduction_depth;
107
108 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 {
110 static hashval_t hash (spec_entry *);
111 static bool equal (spec_entry *, spec_entry *);
112 };
113
114 /* The general template is not in these tables. */
115 typedef hash_table<spec_hasher> spec_hash_table;
116 static GTY (()) spec_hash_table *decl_specializations;
117 static GTY (()) spec_hash_table *type_specializations;
118
119 /* Contains canonical template parameter types. The vector is indexed by
120 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
121 TREE_LIST, whose TREE_VALUEs contain the canonical template
122 parameters of various types and levels. */
123 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
124
125 #define UNIFY_ALLOW_NONE 0
126 #define UNIFY_ALLOW_MORE_CV_QUAL 1
127 #define UNIFY_ALLOW_LESS_CV_QUAL 2
128 #define UNIFY_ALLOW_DERIVED 4
129 #define UNIFY_ALLOW_INTEGER 8
130 #define UNIFY_ALLOW_OUTER_LEVEL 16
131 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
132 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133
134 enum template_base_result {
135 tbr_incomplete_type,
136 tbr_ambiguous_baseclass,
137 tbr_success
138 };
139
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int check_non_deducible_conversion (tree, tree, int, int,
158 struct conversion **, bool);
159 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
160 tree);
161 static int type_unification_real (tree, tree, tree, const tree *,
162 unsigned int, int, unification_kind_t,
163 vec<deferred_access_check, va_gc> **,
164 bool);
165 static void note_template_header (int);
166 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
167 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
168 static tree convert_template_argument (tree, tree, tree,
169 tsubst_flags_t, int, tree);
170 static tree for_each_template_parm (tree, tree_fn_t, void*,
171 hash_set<tree> *, bool, tree_fn_t = NULL);
172 static tree expand_template_argument_pack (tree);
173 static tree build_template_parm_index (int, int, int, tree, tree);
174 static bool inline_needs_template_parms (tree, bool);
175 static void push_inline_template_parms_recursive (tree, int);
176 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
177 static int mark_template_parm (tree, void *);
178 static int template_parm_this_level_p (tree, void *);
179 static tree tsubst_friend_function (tree, tree);
180 static tree tsubst_friend_class (tree, tree);
181 static int can_complete_type_without_circularity (tree);
182 static tree get_bindings (tree, tree, tree, bool);
183 static int template_decl_level (tree);
184 static int check_cv_quals_for_unify (int, tree, tree);
185 static int unify_pack_expansion (tree, tree, tree,
186 tree, unification_kind_t, bool, bool);
187 static tree copy_template_args (tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static bool class_nttp_const_wrapper_p (tree t);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_instantiation_time_access_checks (tree, tree);
214 static tree listify (tree);
215 static tree listify_autos (tree, tree);
216 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
217 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
218 static bool complex_alias_template_p (const_tree tmpl);
219 static tree get_underlying_template (tree);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
224 static tree enclosing_instantiation_of (tree tctx);
225 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
226
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
231
232 void
233 push_access_scope (tree t)
234 {
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
237
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
244
245 if (TREE_CODE (t) == FUNCTION_DECL)
246 {
247 vec_safe_push (saved_access_scope, current_function_decl);
248 current_function_decl = t;
249 }
250 }
251
252 /* Restore the scope set up by push_access_scope. T is the node we
253 are processing. */
254
255 void
256 pop_access_scope (tree t)
257 {
258 if (TREE_CODE (t) == FUNCTION_DECL)
259 current_function_decl = saved_access_scope->pop();
260
261 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
262 pop_nested_class ();
263 else
264 pop_from_top_level ();
265 }
266
267 /* Do any processing required when DECL (a member template
268 declaration) is finished. Returns the TEMPLATE_DECL corresponding
269 to DECL, unless it is a specialization, in which case the DECL
270 itself is returned. */
271
272 tree
273 finish_member_template_decl (tree decl)
274 {
275 if (decl == error_mark_node)
276 return error_mark_node;
277
278 gcc_assert (DECL_P (decl));
279
280 if (TREE_CODE (decl) == TYPE_DECL)
281 {
282 tree type;
283
284 type = TREE_TYPE (decl);
285 if (type == error_mark_node)
286 return error_mark_node;
287 if (MAYBE_CLASS_TYPE_P (type)
288 && CLASSTYPE_TEMPLATE_INFO (type)
289 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
290 {
291 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
292 check_member_template (tmpl);
293 return tmpl;
294 }
295 return NULL_TREE;
296 }
297 else if (TREE_CODE (decl) == FIELD_DECL)
298 error_at (DECL_SOURCE_LOCATION (decl),
299 "data member %qD cannot be a member template", decl);
300 else if (DECL_TEMPLATE_INFO (decl))
301 {
302 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
303 {
304 check_member_template (DECL_TI_TEMPLATE (decl));
305 return DECL_TI_TEMPLATE (decl);
306 }
307 else
308 return decl;
309 }
310 else
311 error_at (DECL_SOURCE_LOCATION (decl),
312 "invalid member template declaration %qD", decl);
313
314 return error_mark_node;
315 }
316
317 /* Create a template info node. */
318
319 tree
320 build_template_info (tree template_decl, tree template_args)
321 {
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
326 }
327
328 /* Return the template info node corresponding to T, whatever T is. */
329
330 tree
331 get_template_info (const_tree t)
332 {
333 tree tinfo = NULL_TREE;
334
335 if (!t || t == error_mark_node)
336 return NULL;
337
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
341
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
344
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
347
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
352
353 return tinfo;
354 }
355
356 /* Returns the template nesting level of the indicated class TYPE.
357
358 For example, in:
359 template <class T>
360 struct A
361 {
362 template <class U>
363 struct B {};
364 };
365
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
369
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
373
374 int
375 template_class_depth (tree type)
376 {
377 int depth;
378
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
380 {
381 tree tinfo = get_template_info (type);
382
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
386
387 if (DECL_P (type))
388 {
389 if (tree fctx = DECL_FRIEND_CONTEXT (type))
390 type = fctx;
391 else
392 type = CP_DECL_CONTEXT (type);
393 }
394 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
406 static bool
407 instantiates_primary_template_p (tree node)
408 {
409 tree tinfo = get_template_info (node);
410 if (!tinfo)
411 return false;
412
413 tree tmpl = TI_TEMPLATE (tinfo);
414 if (PRIMARY_TEMPLATE_P (tmpl))
415 return true;
416
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
418 return false;
419
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
423
424 tree ctxt = DECL_CONTEXT (tmpl);
425 tree ctinfo = get_template_info (ctxt);
426 if (!ctinfo)
427 return true;
428
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
431 }
432
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
435
436 static bool
437 inline_needs_template_parms (tree decl, bool nsdmi)
438 {
439 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
440 return false;
441
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
443 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
444 }
445
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
449 innermost first. */
450
451 static void
452 push_inline_template_parms_recursive (tree parmlist, int levels)
453 {
454 tree parms = TREE_VALUE (parmlist);
455 int i;
456
457 if (levels > 1)
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
459
460 ++processing_template_decl;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl),
463 parms, current_template_parms);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
465
466 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
467 NULL);
468 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
469 {
470 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
471
472 if (error_operand_p (parm))
473 continue;
474
475 gcc_assert (DECL_P (parm));
476
477 switch (TREE_CODE (parm))
478 {
479 case TYPE_DECL:
480 case TEMPLATE_DECL:
481 pushdecl (parm);
482 break;
483
484 case PARM_DECL:
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
487 break;
488
489 default:
490 gcc_unreachable ();
491 }
492 }
493 }
494
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
498
499 void
500 maybe_begin_member_template_processing (tree decl)
501 {
502 tree parms;
503 int levels = 0;
504 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
505
506 if (nsdmi)
507 {
508 tree ctx = DECL_CONTEXT (decl);
509 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx)
512 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
513 }
514
515 if (inline_needs_template_parms (decl, nsdmi))
516 {
517 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
518 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
519
520 if (DECL_TEMPLATE_SPECIALIZATION (decl))
521 {
522 --levels;
523 parms = TREE_CHAIN (parms);
524 }
525
526 push_inline_template_parms_recursive (parms, levels);
527 }
528
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels.safe_push (levels);
532 }
533
534 /* Undo the effects of maybe_begin_member_template_processing. */
535
536 void
537 maybe_end_member_template_processing (void)
538 {
539 int i;
540 int last;
541
542 if (inline_parm_levels.length () == 0)
543 return;
544
545 last = inline_parm_levels.pop ();
546 for (i = 0; i < last; ++i)
547 {
548 --processing_template_decl;
549 current_template_parms = TREE_CHAIN (current_template_parms);
550 poplevel (0, 0, 0);
551 }
552 }
553
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
556
557 static tree
558 add_to_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561 int extra_depth;
562 int i;
563 int j;
564
565 if (args == NULL_TREE || extra_args == error_mark_node)
566 return extra_args;
567
568 extra_depth = TMPL_ARGS_DEPTH (extra_args);
569 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
570
571 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
573
574 for (j = 1; j <= extra_depth; ++j, ++i)
575 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
576
577 return new_args;
578 }
579
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation.
586
587 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
588
589 tree
590 add_outermost_template_args (tree args, tree extra_args)
591 {
592 tree new_args;
593
594 if (!args)
595 return extra_args;
596 if (TREE_CODE (args) == TEMPLATE_DECL)
597 {
598 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
599 args = TI_ARGS (ti);
600 }
601
602 /* If there are more levels of EXTRA_ARGS than there are ARGS,
603 something very fishy is going on. */
604 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
605
606 /* If *all* the new arguments will be the EXTRA_ARGS, just return
607 them. */
608 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
609 return extra_args;
610
611 /* For the moment, we make ARGS look like it contains fewer levels. */
612 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
613
614 new_args = add_to_template_args (args, extra_args);
615
616 /* Now, we restore ARGS to its full dimensions. */
617 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
618
619 return new_args;
620 }
621
622 /* Return the N levels of innermost template arguments from the ARGS. */
623
624 tree
625 get_innermost_template_args (tree args, int n)
626 {
627 tree new_args;
628 int extra_levels;
629 int i;
630
631 gcc_assert (n >= 0);
632
633 /* If N is 1, just return the innermost set of template arguments. */
634 if (n == 1)
635 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
636
637 /* If we're not removing anything, just return the arguments we were
638 given. */
639 extra_levels = TMPL_ARGS_DEPTH (args) - n;
640 gcc_assert (extra_levels >= 0);
641 if (extra_levels == 0)
642 return args;
643
644 /* Make a new set of arguments, not containing the outer arguments. */
645 new_args = make_tree_vec (n);
646 for (i = 1; i <= n; ++i)
647 SET_TMPL_ARGS_LEVEL (new_args, i,
648 TMPL_ARGS_LEVEL (args, i + extra_levels));
649
650 return new_args;
651 }
652
653 /* The inverse of get_innermost_template_args: Return all but the innermost
654 EXTRA_LEVELS levels of template arguments from the ARGS. */
655
656 static tree
657 strip_innermost_template_args (tree args, int extra_levels)
658 {
659 tree new_args;
660 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
661 int i;
662
663 gcc_assert (n >= 0);
664
665 /* If N is 1, just return the outermost set of template arguments. */
666 if (n == 1)
667 return TMPL_ARGS_LEVEL (args, 1);
668
669 /* If we're not removing anything, just return the arguments we were
670 given. */
671 gcc_assert (extra_levels >= 0);
672 if (extra_levels == 0)
673 return args;
674
675 /* Make a new set of arguments, not containing the inner arguments. */
676 new_args = make_tree_vec (n);
677 for (i = 1; i <= n; ++i)
678 SET_TMPL_ARGS_LEVEL (new_args, i,
679 TMPL_ARGS_LEVEL (args, i));
680
681 return new_args;
682 }
683
684 /* We've got a template header coming up; push to a new level for storing
685 the parms. */
686
687 void
688 begin_template_parm_list (void)
689 {
690 /* We use a non-tag-transparent scope here, which causes pushtag to
691 put tags in this scope, rather than in the enclosing class or
692 namespace scope. This is the right thing, since we want
693 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
694 global template class, push_template_decl handles putting the
695 TEMPLATE_DECL into top-level scope. For a nested template class,
696 e.g.:
697
698 template <class T> struct S1 {
699 template <class T> struct S2 {};
700 };
701
702 pushtag contains special code to insert the TEMPLATE_DECL for S2
703 at the right scope. */
704 begin_scope (sk_template_parms, NULL);
705 ++processing_template_decl;
706 ++processing_template_parmlist;
707 note_template_header (0);
708
709 /* Add a dummy parameter level while we process the parameter list. */
710 current_template_parms
711 = tree_cons (size_int (processing_template_decl),
712 make_tree_vec (0),
713 current_template_parms);
714 }
715
716 /* This routine is called when a specialization is declared. If it is
717 invalid to declare a specialization here, an error is reported and
718 false is returned, otherwise this routine will return true. */
719
720 static bool
721 check_specialization_scope (void)
722 {
723 tree scope = current_scope ();
724
725 /* [temp.expl.spec]
726
727 An explicit specialization shall be declared in the namespace of
728 which the template is a member, or, for member templates, in the
729 namespace of which the enclosing class or enclosing class
730 template is a member. An explicit specialization of a member
731 function, member class or static data member of a class template
732 shall be declared in the namespace of which the class template
733 is a member. */
734 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
735 {
736 error ("explicit specialization in non-namespace scope %qD", scope);
737 return false;
738 }
739
740 /* [temp.expl.spec]
741
742 In an explicit specialization declaration for a member of a class
743 template or a member template that appears in namespace scope,
744 the member template and some of its enclosing class templates may
745 remain unspecialized, except that the declaration shall not
746 explicitly specialize a class member template if its enclosing
747 class templates are not explicitly specialized as well. */
748 if (current_template_parms)
749 {
750 error ("enclosing class templates are not explicitly specialized");
751 return false;
752 }
753
754 return true;
755 }
756
757 /* We've just seen template <>. */
758
759 bool
760 begin_specialization (void)
761 {
762 begin_scope (sk_template_spec, NULL);
763 note_template_header (1);
764 return check_specialization_scope ();
765 }
766
767 /* Called at then end of processing a declaration preceded by
768 template<>. */
769
770 void
771 end_specialization (void)
772 {
773 finish_scope ();
774 reset_specialization ();
775 }
776
777 /* Any template <>'s that we have seen thus far are not referring to a
778 function specialization. */
779
780 void
781 reset_specialization (void)
782 {
783 processing_specialization = 0;
784 template_header_count = 0;
785 }
786
787 /* We've just seen a template header. If SPECIALIZATION is nonzero,
788 it was of the form template <>. */
789
790 static void
791 note_template_header (int specialization)
792 {
793 processing_specialization = specialization;
794 template_header_count++;
795 }
796
797 /* We're beginning an explicit instantiation. */
798
799 void
800 begin_explicit_instantiation (void)
801 {
802 gcc_assert (!processing_explicit_instantiation);
803 processing_explicit_instantiation = true;
804 }
805
806
807 void
808 end_explicit_instantiation (void)
809 {
810 gcc_assert (processing_explicit_instantiation);
811 processing_explicit_instantiation = false;
812 }
813
814 /* An explicit specialization or partial specialization of TMPL is being
815 declared. Check that the namespace in which the specialization is
816 occurring is permissible. Returns false iff it is invalid to
817 specialize TMPL in the current namespace. */
818
819 static bool
820 check_specialization_namespace (tree tmpl)
821 {
822 tree tpl_ns = decl_namespace_context (tmpl);
823
824 /* [tmpl.expl.spec]
825
826 An explicit specialization shall be declared in a namespace enclosing the
827 specialized template. An explicit specialization whose declarator-id is
828 not qualified shall be declared in the nearest enclosing namespace of the
829 template, or, if the namespace is inline (7.3.1), any namespace from its
830 enclosing namespace set. */
831 if (current_scope() != DECL_CONTEXT (tmpl)
832 && !at_namespace_scope_p ())
833 {
834 error ("specialization of %qD must appear at namespace scope", tmpl);
835 return false;
836 }
837
838 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
839 /* Same or enclosing namespace. */
840 return true;
841 else
842 {
843 auto_diagnostic_group d;
844 if (permerror (input_location,
845 "specialization of %qD in different namespace", tmpl))
846 inform (DECL_SOURCE_LOCATION (tmpl),
847 " from definition of %q#D", tmpl);
848 return false;
849 }
850 }
851
852 /* SPEC is an explicit instantiation. Check that it is valid to
853 perform this explicit instantiation in the current namespace. */
854
855 static void
856 check_explicit_instantiation_namespace (tree spec)
857 {
858 tree ns;
859
860 /* DR 275: An explicit instantiation shall appear in an enclosing
861 namespace of its template. */
862 ns = decl_namespace_context (spec);
863 if (!is_nested_namespace (current_namespace, ns))
864 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
865 "(which does not enclose namespace %qD)",
866 spec, current_namespace, ns);
867 }
868
869 /* Returns the type of a template specialization only if that
870 specialization needs to be defined. Otherwise (e.g., if the type has
871 already been defined), the function returns NULL_TREE. */
872
873 static tree
874 maybe_new_partial_specialization (tree type)
875 {
876 /* An implicit instantiation of an incomplete type implies
877 the definition of a new class template.
878
879 template<typename T>
880 struct S;
881
882 template<typename T>
883 struct S<T*>;
884
885 Here, S<T*> is an implicit instantiation of S whose type
886 is incomplete. */
887 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
888 return type;
889
890 /* It can also be the case that TYPE is a completed specialization.
891 Continuing the previous example, suppose we also declare:
892
893 template<typename T>
894 requires Integral<T>
895 struct S<T*>;
896
897 Here, S<T*> refers to the specialization S<T*> defined
898 above. However, we need to differentiate definitions because
899 we intend to define a new partial specialization. In this case,
900 we rely on the fact that the constraints are different for
901 this declaration than that above.
902
903 Note that we also get here for injected class names and
904 late-parsed template definitions. We must ensure that we
905 do not create new type declarations for those cases. */
906 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
907 {
908 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
909 tree args = CLASSTYPE_TI_ARGS (type);
910
911 /* If there are no template parameters, this cannot be a new
912 partial template specialization? */
913 if (!current_template_parms)
914 return NULL_TREE;
915
916 /* The injected-class-name is not a new partial specialization. */
917 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
918 return NULL_TREE;
919
920 /* If the constraints are not the same as those of the primary
921 then, we can probably create a new specialization. */
922 tree type_constr = current_template_constraints ();
923
924 if (type == TREE_TYPE (tmpl))
925 {
926 tree main_constr = get_constraints (tmpl);
927 if (equivalent_constraints (type_constr, main_constr))
928 return NULL_TREE;
929 }
930
931 /* Also, if there's a pre-existing specialization with matching
932 constraints, then this also isn't new. */
933 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
934 while (specs)
935 {
936 tree spec_tmpl = TREE_VALUE (specs);
937 tree spec_args = TREE_PURPOSE (specs);
938 tree spec_constr = get_constraints (spec_tmpl);
939 if (comp_template_args (args, spec_args)
940 && equivalent_constraints (type_constr, spec_constr))
941 return NULL_TREE;
942 specs = TREE_CHAIN (specs);
943 }
944
945 /* Create a new type node (and corresponding type decl)
946 for the newly declared specialization. */
947 tree t = make_class_type (TREE_CODE (type));
948 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
949 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
950
951 /* We only need a separate type node for storing the definition of this
952 partial specialization; uses of S<T*> are unconstrained, so all are
953 equivalent. So keep TYPE_CANONICAL the same. */
954 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
955
956 /* Build the corresponding type decl. */
957 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
958 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
959 DECL_SOURCE_LOCATION (d) = input_location;
960 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
961 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
962
963 set_instantiating_module (d);
964 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
965
966 return t;
967 }
968
969 return NULL_TREE;
970 }
971
972 /* The TYPE is being declared. If it is a template type, that means it
973 is a partial specialization. Do appropriate error-checking. */
974
975 tree
976 maybe_process_partial_specialization (tree type)
977 {
978 tree context;
979
980 if (type == error_mark_node)
981 return error_mark_node;
982
983 /* A lambda that appears in specialization context is not itself a
984 specialization. */
985 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
986 return type;
987
988 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
989 {
990 error ("name of class shadows template template parameter %qD",
991 TYPE_NAME (type));
992 return error_mark_node;
993 }
994
995 context = TYPE_CONTEXT (type);
996
997 if (TYPE_ALIAS_P (type))
998 {
999 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1000
1001 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1002 error ("specialization of alias template %qD",
1003 TI_TEMPLATE (tinfo));
1004 else
1005 error ("explicit specialization of non-template %qT", type);
1006 return error_mark_node;
1007 }
1008 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1009 {
1010 /* This is for ordinary explicit specialization and partial
1011 specialization of a template class such as:
1012
1013 template <> class C<int>;
1014
1015 or:
1016
1017 template <class T> class C<T*>;
1018
1019 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1020
1021 if (tree t = maybe_new_partial_specialization (type))
1022 {
1023 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1024 && !at_namespace_scope_p ())
1025 return error_mark_node;
1026 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1027 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1028 if (processing_template_decl)
1029 {
1030 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1031 if (decl == error_mark_node)
1032 return error_mark_node;
1033 return TREE_TYPE (decl);
1034 }
1035 }
1036 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1037 error ("specialization of %qT after instantiation", type);
1038 else if (errorcount && !processing_specialization
1039 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1040 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1041 /* Trying to define a specialization either without a template<> header
1042 or in an inappropriate place. We've already given an error, so just
1043 bail now so we don't actually define the specialization. */
1044 return error_mark_node;
1045 }
1046 else if (CLASS_TYPE_P (type)
1047 && !CLASSTYPE_USE_TEMPLATE (type)
1048 && CLASSTYPE_TEMPLATE_INFO (type)
1049 && context && CLASS_TYPE_P (context)
1050 && CLASSTYPE_TEMPLATE_INFO (context))
1051 {
1052 /* This is for an explicit specialization of member class
1053 template according to [temp.expl.spec/18]:
1054
1055 template <> template <class U> class C<int>::D;
1056
1057 The context `C<int>' must be an implicit instantiation.
1058 Otherwise this is just a member class template declared
1059 earlier like:
1060
1061 template <> class C<int> { template <class U> class D; };
1062 template <> template <class U> class C<int>::D;
1063
1064 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1065 while in the second case, `C<int>::D' is a primary template
1066 and `C<T>::D' may not exist. */
1067
1068 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1069 && !COMPLETE_TYPE_P (type))
1070 {
1071 tree t;
1072 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1073
1074 if (current_namespace
1075 != decl_namespace_context (tmpl))
1076 {
1077 if (permerror (input_location,
1078 "specialization of %qD in different namespace",
1079 type))
1080 inform (DECL_SOURCE_LOCATION (tmpl),
1081 "from definition of %q#D", tmpl);
1082 }
1083
1084 /* Check for invalid specialization after instantiation:
1085
1086 template <> template <> class C<int>::D<int>;
1087 template <> template <class U> class C<int>::D; */
1088
1089 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1090 t; t = TREE_CHAIN (t))
1091 {
1092 tree inst = TREE_VALUE (t);
1093 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1094 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1095 {
1096 /* We already have a full specialization of this partial
1097 instantiation, or a full specialization has been
1098 looked up but not instantiated. Reassign it to the
1099 new member specialization template. */
1100 spec_entry elt;
1101 spec_entry *entry;
1102
1103 elt.tmpl = most_general_template (tmpl);
1104 elt.args = CLASSTYPE_TI_ARGS (inst);
1105 elt.spec = inst;
1106
1107 type_specializations->remove_elt (&elt);
1108
1109 elt.tmpl = tmpl;
1110 CLASSTYPE_TI_ARGS (inst)
1111 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1112
1113 spec_entry **slot
1114 = type_specializations->find_slot (&elt, INSERT);
1115 entry = ggc_alloc<spec_entry> ();
1116 *entry = elt;
1117 *slot = entry;
1118 }
1119 else
1120 /* But if we've had an implicit instantiation, that's a
1121 problem ([temp.expl.spec]/6). */
1122 error ("specialization %qT after instantiation %qT",
1123 type, inst);
1124 }
1125
1126 /* Mark TYPE as a specialization. And as a result, we only
1127 have one level of template argument for the innermost
1128 class template. */
1129 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1130 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1131 CLASSTYPE_TI_ARGS (type)
1132 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1133 }
1134 }
1135 else if (processing_specialization)
1136 {
1137 /* Someday C++0x may allow for enum template specialization. */
1138 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1139 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1140 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1141 "of %qD not allowed by ISO C++", type);
1142 else
1143 {
1144 error ("explicit specialization of non-template %qT", type);
1145 return error_mark_node;
1146 }
1147 }
1148
1149 return type;
1150 }
1151
1152 /* Returns nonzero if we can optimize the retrieval of specializations
1153 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1154 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1155
1156 static inline bool
1157 optimize_specialization_lookup_p (tree tmpl)
1158 {
1159 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1160 && DECL_CLASS_SCOPE_P (tmpl)
1161 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1162 parameter. */
1163 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1164 /* The optimized lookup depends on the fact that the
1165 template arguments for the member function template apply
1166 purely to the containing class, which is not true if the
1167 containing class is an explicit or partial
1168 specialization. */
1169 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1170 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1171 && !DECL_CONV_FN_P (tmpl)
1172 /* It is possible to have a template that is not a member
1173 template and is not a member of a template class:
1174
1175 template <typename T>
1176 struct S { friend A::f(); };
1177
1178 Here, the friend function is a template, but the context does
1179 not have template information. The optimized lookup relies
1180 on having ARGS be the template arguments for both the class
1181 and the function template. */
1182 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1183 }
1184
1185 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1186 gone through coerce_template_parms by now. */
1187
1188 static void
1189 verify_unstripped_args_1 (tree inner)
1190 {
1191 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1192 {
1193 tree arg = TREE_VEC_ELT (inner, i);
1194 if (TREE_CODE (arg) == TEMPLATE_DECL)
1195 /* OK */;
1196 else if (TYPE_P (arg))
1197 gcc_assert (strip_typedefs (arg, NULL) == arg);
1198 else if (ARGUMENT_PACK_P (arg))
1199 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1200 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1201 /* Allow typedefs on the type of a non-type argument, since a
1202 parameter can have them. */;
1203 else
1204 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1205 }
1206 }
1207
1208 static void
1209 verify_unstripped_args (tree args)
1210 {
1211 ++processing_template_decl;
1212 if (!any_dependent_template_arguments_p (args))
1213 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1214 --processing_template_decl;
1215 }
1216
1217 /* Retrieve the specialization (in the sense of [temp.spec] - a
1218 specialization is either an instantiation or an explicit
1219 specialization) of TMPL for the given template ARGS. If there is
1220 no such specialization, return NULL_TREE. The ARGS are a vector of
1221 arguments, or a vector of vectors of arguments, in the case of
1222 templates with more than one level of parameters.
1223
1224 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1225 then we search for a partial specialization matching ARGS. This
1226 parameter is ignored if TMPL is not a class template.
1227
1228 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1229 result is a NONTYPE_ARGUMENT_PACK. */
1230
1231 static tree
1232 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1233 {
1234 if (tmpl == NULL_TREE)
1235 return NULL_TREE;
1236
1237 if (args == error_mark_node)
1238 return NULL_TREE;
1239
1240 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1241 || TREE_CODE (tmpl) == FIELD_DECL);
1242
1243 /* There should be as many levels of arguments as there are
1244 levels of parameters. */
1245 gcc_assert (TMPL_ARGS_DEPTH (args)
1246 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1247 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1248 : template_class_depth (DECL_CONTEXT (tmpl))));
1249
1250 if (flag_checking)
1251 verify_unstripped_args (args);
1252
1253 /* Lambda functions in templates aren't instantiated normally, but through
1254 tsubst_lambda_expr. */
1255 if (lambda_fn_in_template_p (tmpl))
1256 return NULL_TREE;
1257
1258 if (optimize_specialization_lookup_p (tmpl))
1259 {
1260 /* The template arguments actually apply to the containing
1261 class. Find the class specialization with those
1262 arguments. */
1263 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1264 tree class_specialization
1265 = retrieve_specialization (class_template, args, 0);
1266 if (!class_specialization)
1267 return NULL_TREE;
1268
1269 /* Find the instance of TMPL. */
1270 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1271 for (ovl_iterator iter (fns); iter; ++iter)
1272 {
1273 tree fn = *iter;
1274 if (tree ti = get_template_info (fn))
1275 if (TI_TEMPLATE (ti) == tmpl
1276 /* using-declarations can bring in a different
1277 instantiation of tmpl as a member of a different
1278 instantiation of tmpl's class. We don't want those
1279 here. */
1280 && DECL_CONTEXT (fn) == class_specialization)
1281 return fn;
1282 }
1283 return NULL_TREE;
1284 }
1285 else
1286 {
1287 spec_entry *found;
1288 spec_entry elt;
1289 spec_hash_table *specializations;
1290
1291 elt.tmpl = tmpl;
1292 elt.args = args;
1293 elt.spec = NULL_TREE;
1294
1295 if (DECL_CLASS_TEMPLATE_P (tmpl))
1296 specializations = type_specializations;
1297 else
1298 specializations = decl_specializations;
1299
1300 if (hash == 0)
1301 hash = spec_hasher::hash (&elt);
1302 found = specializations->find_with_hash (&elt, hash);
1303 if (found)
1304 return found->spec;
1305 }
1306
1307 return NULL_TREE;
1308 }
1309
1310 /* Like retrieve_specialization, but for local declarations. */
1311
1312 tree
1313 retrieve_local_specialization (tree tmpl)
1314 {
1315 if (local_specializations == NULL)
1316 return NULL_TREE;
1317
1318 tree *slot = local_specializations->get (tmpl);
1319 return slot ? *slot : NULL_TREE;
1320 }
1321
1322 /* Returns nonzero iff DECL is a specialization of TMPL. */
1323
1324 int
1325 is_specialization_of (tree decl, tree tmpl)
1326 {
1327 tree t;
1328
1329 if (TREE_CODE (decl) == FUNCTION_DECL)
1330 {
1331 for (t = decl;
1332 t != NULL_TREE;
1333 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1334 if (t == tmpl)
1335 return 1;
1336 }
1337 else
1338 {
1339 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1340
1341 for (t = TREE_TYPE (decl);
1342 t != NULL_TREE;
1343 t = CLASSTYPE_USE_TEMPLATE (t)
1344 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1345 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1346 return 1;
1347 }
1348
1349 return 0;
1350 }
1351
1352 /* Returns nonzero iff DECL is a specialization of friend declaration
1353 FRIEND_DECL according to [temp.friend]. */
1354
1355 bool
1356 is_specialization_of_friend (tree decl, tree friend_decl)
1357 {
1358 bool need_template = true;
1359 int template_depth;
1360
1361 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1362 || TREE_CODE (decl) == TYPE_DECL);
1363
1364 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1365 of a template class, we want to check if DECL is a specialization
1366 if this. */
1367 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1368 && DECL_TEMPLATE_INFO (friend_decl)
1369 && !DECL_USE_TEMPLATE (friend_decl))
1370 {
1371 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1372 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1373 need_template = false;
1374 }
1375 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1376 && !PRIMARY_TEMPLATE_P (friend_decl))
1377 need_template = false;
1378
1379 /* There is nothing to do if this is not a template friend. */
1380 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1381 return false;
1382
1383 if (is_specialization_of (decl, friend_decl))
1384 return true;
1385
1386 /* [temp.friend/6]
1387 A member of a class template may be declared to be a friend of a
1388 non-template class. In this case, the corresponding member of
1389 every specialization of the class template is a friend of the
1390 class granting friendship.
1391
1392 For example, given a template friend declaration
1393
1394 template <class T> friend void A<T>::f();
1395
1396 the member function below is considered a friend
1397
1398 template <> struct A<int> {
1399 void f();
1400 };
1401
1402 For this type of template friend, TEMPLATE_DEPTH below will be
1403 nonzero. To determine if DECL is a friend of FRIEND, we first
1404 check if the enclosing class is a specialization of another. */
1405
1406 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1407 if (template_depth
1408 && DECL_CLASS_SCOPE_P (decl)
1409 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1410 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1411 {
1412 /* Next, we check the members themselves. In order to handle
1413 a few tricky cases, such as when FRIEND_DECL's are
1414
1415 template <class T> friend void A<T>::g(T t);
1416 template <class T> template <T t> friend void A<T>::h();
1417
1418 and DECL's are
1419
1420 void A<int>::g(int);
1421 template <int> void A<int>::h();
1422
1423 we need to figure out ARGS, the template arguments from
1424 the context of DECL. This is required for template substitution
1425 of `T' in the function parameter of `g' and template parameter
1426 of `h' in the above examples. Here ARGS corresponds to `int'. */
1427
1428 tree context = DECL_CONTEXT (decl);
1429 tree args = NULL_TREE;
1430 int current_depth = 0;
1431
1432 while (current_depth < template_depth)
1433 {
1434 if (CLASSTYPE_TEMPLATE_INFO (context))
1435 {
1436 if (current_depth == 0)
1437 args = TYPE_TI_ARGS (context);
1438 else
1439 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1440 current_depth++;
1441 }
1442 context = TYPE_CONTEXT (context);
1443 }
1444
1445 if (TREE_CODE (decl) == FUNCTION_DECL)
1446 {
1447 bool is_template;
1448 tree friend_type;
1449 tree decl_type;
1450 tree friend_args_type;
1451 tree decl_args_type;
1452
1453 /* Make sure that both DECL and FRIEND_DECL are templates or
1454 non-templates. */
1455 is_template = DECL_TEMPLATE_INFO (decl)
1456 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1457 if (need_template ^ is_template)
1458 return false;
1459 else if (is_template)
1460 {
1461 /* If both are templates, check template parameter list. */
1462 tree friend_parms
1463 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1464 args, tf_none);
1465 if (!comp_template_parms
1466 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1467 friend_parms))
1468 return false;
1469
1470 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1471 }
1472 else
1473 decl_type = TREE_TYPE (decl);
1474
1475 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1476 tf_none, NULL_TREE);
1477 if (friend_type == error_mark_node)
1478 return false;
1479
1480 /* Check if return types match. */
1481 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1482 return false;
1483
1484 /* Check if function parameter types match, ignoring the
1485 `this' parameter. */
1486 friend_args_type = TYPE_ARG_TYPES (friend_type);
1487 decl_args_type = TYPE_ARG_TYPES (decl_type);
1488 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1489 friend_args_type = TREE_CHAIN (friend_args_type);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1491 decl_args_type = TREE_CHAIN (decl_args_type);
1492
1493 return compparms (decl_args_type, friend_args_type);
1494 }
1495 else
1496 {
1497 /* DECL is a TYPE_DECL */
1498 bool is_template;
1499 tree decl_type = TREE_TYPE (decl);
1500
1501 /* Make sure that both DECL and FRIEND_DECL are templates or
1502 non-templates. */
1503 is_template
1504 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1505 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1506
1507 if (need_template ^ is_template)
1508 return false;
1509 else if (is_template)
1510 {
1511 tree friend_parms;
1512 /* If both are templates, check the name of the two
1513 TEMPLATE_DECL's first because is_friend didn't. */
1514 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1515 != DECL_NAME (friend_decl))
1516 return false;
1517
1518 /* Now check template parameter list. */
1519 friend_parms
1520 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1521 args, tf_none);
1522 return comp_template_parms
1523 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1524 friend_parms);
1525 }
1526 else
1527 return (DECL_NAME (decl)
1528 == DECL_NAME (friend_decl));
1529 }
1530 }
1531 return false;
1532 }
1533
1534 /* Register the specialization SPEC as a specialization of TMPL with
1535 the indicated ARGS. IS_FRIEND indicates whether the specialization
1536 is actually just a friend declaration. ATTRLIST is the list of
1537 attributes that the specialization is declared with or NULL when
1538 it isn't. Returns SPEC, or an equivalent prior declaration, if
1539 available.
1540
1541 We also store instantiations of field packs in the hash table, even
1542 though they are not themselves templates, to make lookup easier. */
1543
1544 static tree
1545 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1546 hashval_t hash)
1547 {
1548 tree fn;
1549 spec_entry **slot = NULL;
1550 spec_entry elt;
1551
1552 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1553 || (TREE_CODE (tmpl) == FIELD_DECL
1554 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1555
1556 if (TREE_CODE (spec) == FUNCTION_DECL
1557 && uses_template_parms (DECL_TI_ARGS (spec)))
1558 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1559 register it; we want the corresponding TEMPLATE_DECL instead.
1560 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1561 the more obvious `uses_template_parms (spec)' to avoid problems
1562 with default function arguments. In particular, given
1563 something like this:
1564
1565 template <class T> void f(T t1, T t = T())
1566
1567 the default argument expression is not substituted for in an
1568 instantiation unless and until it is actually needed. */
1569 return spec;
1570
1571 if (optimize_specialization_lookup_p (tmpl))
1572 /* We don't put these specializations in the hash table, but we might
1573 want to give an error about a mismatch. */
1574 fn = retrieve_specialization (tmpl, args, 0);
1575 else
1576 {
1577 elt.tmpl = tmpl;
1578 elt.args = args;
1579 elt.spec = spec;
1580
1581 if (hash == 0)
1582 hash = spec_hasher::hash (&elt);
1583
1584 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1585 if (*slot)
1586 fn = (*slot)->spec;
1587 else
1588 fn = NULL_TREE;
1589 }
1590
1591 /* We can sometimes try to re-register a specialization that we've
1592 already got. In particular, regenerate_decl_from_template calls
1593 duplicate_decls which will update the specialization list. But,
1594 we'll still get called again here anyhow. It's more convenient
1595 to simply allow this than to try to prevent it. */
1596 if (fn == spec)
1597 return spec;
1598 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1599 {
1600 if (DECL_TEMPLATE_INSTANTIATION (fn))
1601 {
1602 if (DECL_ODR_USED (fn)
1603 || DECL_EXPLICIT_INSTANTIATION (fn))
1604 {
1605 error ("specialization of %qD after instantiation",
1606 fn);
1607 return error_mark_node;
1608 }
1609 else
1610 {
1611 tree clone;
1612 /* This situation should occur only if the first
1613 specialization is an implicit instantiation, the
1614 second is an explicit specialization, and the
1615 implicit instantiation has not yet been used. That
1616 situation can occur if we have implicitly
1617 instantiated a member function and then specialized
1618 it later.
1619
1620 We can also wind up here if a friend declaration that
1621 looked like an instantiation turns out to be a
1622 specialization:
1623
1624 template <class T> void foo(T);
1625 class S { friend void foo<>(int) };
1626 template <> void foo(int);
1627
1628 We transform the existing DECL in place so that any
1629 pointers to it become pointers to the updated
1630 declaration.
1631
1632 If there was a definition for the template, but not
1633 for the specialization, we want this to look as if
1634 there were no definition, and vice versa. */
1635 DECL_INITIAL (fn) = NULL_TREE;
1636 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1637 /* The call to duplicate_decls will have applied
1638 [temp.expl.spec]:
1639
1640 An explicit specialization of a function template
1641 is inline only if it is explicitly declared to be,
1642 and independently of whether its function template
1643 is.
1644
1645 to the primary function; now copy the inline bits to
1646 the various clones. */
1647 FOR_EACH_CLONE (clone, fn)
1648 {
1649 DECL_DECLARED_INLINE_P (clone)
1650 = DECL_DECLARED_INLINE_P (fn);
1651 DECL_SOURCE_LOCATION (clone)
1652 = DECL_SOURCE_LOCATION (fn);
1653 DECL_DELETED_FN (clone)
1654 = DECL_DELETED_FN (fn);
1655 }
1656 check_specialization_namespace (tmpl);
1657
1658 return fn;
1659 }
1660 }
1661 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1662 {
1663 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1664 if (dd == error_mark_node)
1665 /* We've already complained in duplicate_decls. */
1666 return error_mark_node;
1667
1668 if (dd == NULL_TREE && DECL_INITIAL (spec))
1669 /* Dup decl failed, but this is a new definition. Set the
1670 line number so any errors match this new
1671 definition. */
1672 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1673
1674 return fn;
1675 }
1676 }
1677 else if (fn)
1678 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1679
1680 /* A specialization must be declared in the same namespace as the
1681 template it is specializing. */
1682 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1683 && !check_specialization_namespace (tmpl))
1684 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1685
1686 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1687 {
1688 spec_entry *entry = ggc_alloc<spec_entry> ();
1689 gcc_assert (tmpl && args && spec);
1690 *entry = elt;
1691 *slot = entry;
1692 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1693 && PRIMARY_TEMPLATE_P (tmpl)
1694 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1695 || variable_template_p (tmpl))
1696 /* If TMPL is a forward declaration of a template function, keep a list
1697 of all specializations in case we need to reassign them to a friend
1698 template later in tsubst_friend_function.
1699
1700 Also keep a list of all variable template instantiations so that
1701 process_partial_specialization can check whether a later partial
1702 specialization would have used it. */
1703 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1704 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1705 }
1706
1707 return spec;
1708 }
1709
1710 /* Restricts tree and type comparisons. */
1711 int comparing_specializations;
1712
1713 /* Returns true iff two spec_entry nodes are equivalent. */
1714
1715 bool
1716 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1717 {
1718 int equal;
1719
1720 ++comparing_specializations;
1721 equal = (e1->tmpl == e2->tmpl
1722 && comp_template_args (e1->args, e2->args));
1723 if (equal && flag_concepts
1724 /* tmpl could be a FIELD_DECL for a capture pack. */
1725 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1726 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1727 && uses_template_parms (e1->args))
1728 {
1729 /* Partial specializations of a variable template can be distinguished by
1730 constraints. */
1731 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1732 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1733 equal = equivalent_constraints (c1, c2);
1734 }
1735 --comparing_specializations;
1736
1737 return equal;
1738 }
1739
1740 /* Returns a hash for a template TMPL and template arguments ARGS. */
1741
1742 static hashval_t
1743 hash_tmpl_and_args (tree tmpl, tree args)
1744 {
1745 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1746 return iterative_hash_template_arg (args, val);
1747 }
1748
1749 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1750 ignoring SPEC. */
1751
1752 hashval_t
1753 spec_hasher::hash (spec_entry *e)
1754 {
1755 return hash_tmpl_and_args (e->tmpl, e->args);
1756 }
1757
1758 /* Recursively calculate a hash value for a template argument ARG, for use
1759 in the hash tables of template specializations. We must be
1760 careful to (at least) skip the same entities template_args_equal
1761 does. */
1762
1763 hashval_t
1764 iterative_hash_template_arg (tree arg, hashval_t val)
1765 {
1766 if (arg == NULL_TREE)
1767 return iterative_hash_object (arg, val);
1768
1769 if (!TYPE_P (arg))
1770 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1771 while (CONVERT_EXPR_P (arg)
1772 || TREE_CODE (arg) == NON_LVALUE_EXPR
1773 || class_nttp_const_wrapper_p (arg))
1774 arg = TREE_OPERAND (arg, 0);
1775
1776 enum tree_code code = TREE_CODE (arg);
1777
1778 val = iterative_hash_object (code, val);
1779
1780 switch (code)
1781 {
1782 case ARGUMENT_PACK_SELECT:
1783 gcc_unreachable ();
1784
1785 case ERROR_MARK:
1786 return val;
1787
1788 case IDENTIFIER_NODE:
1789 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1790
1791 case TREE_VEC:
1792 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1793 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1794 return val;
1795
1796 case TYPE_PACK_EXPANSION:
1797 case EXPR_PACK_EXPANSION:
1798 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1799 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1800
1801 case TYPE_ARGUMENT_PACK:
1802 case NONTYPE_ARGUMENT_PACK:
1803 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1804
1805 case TREE_LIST:
1806 for (; arg; arg = TREE_CHAIN (arg))
1807 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1808 return val;
1809
1810 case OVERLOAD:
1811 for (lkp_iterator iter (arg); iter; ++iter)
1812 val = iterative_hash_template_arg (*iter, val);
1813 return val;
1814
1815 case CONSTRUCTOR:
1816 {
1817 tree field, value;
1818 unsigned i;
1819 iterative_hash_template_arg (TREE_TYPE (arg), val);
1820 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1821 {
1822 val = iterative_hash_template_arg (field, val);
1823 val = iterative_hash_template_arg (value, val);
1824 }
1825 return val;
1826 }
1827
1828 case PARM_DECL:
1829 if (!DECL_ARTIFICIAL (arg))
1830 {
1831 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1832 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1833 }
1834 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1835
1836 case TARGET_EXPR:
1837 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1838
1839 case PTRMEM_CST:
1840 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1841 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1842
1843 case TEMPLATE_PARM_INDEX:
1844 val = iterative_hash_template_arg
1845 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1846 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1847 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1848
1849 case TRAIT_EXPR:
1850 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1851 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1852 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1853
1854 case BASELINK:
1855 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1856 val);
1857 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1858 val);
1859
1860 case MODOP_EXPR:
1861 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1862 code = TREE_CODE (TREE_OPERAND (arg, 1));
1863 val = iterative_hash_object (code, val);
1864 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1865
1866 case LAMBDA_EXPR:
1867 /* [temp.over.link] Two lambda-expressions are never considered
1868 equivalent.
1869
1870 So just hash the closure type. */
1871 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1872
1873 case CAST_EXPR:
1874 case IMPLICIT_CONV_EXPR:
1875 case STATIC_CAST_EXPR:
1876 case REINTERPRET_CAST_EXPR:
1877 case CONST_CAST_EXPR:
1878 case DYNAMIC_CAST_EXPR:
1879 case NEW_EXPR:
1880 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1881 /* Now hash operands as usual. */
1882 break;
1883
1884 case CALL_EXPR:
1885 {
1886 tree fn = CALL_EXPR_FN (arg);
1887 if (tree name = dependent_name (fn))
1888 {
1889 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1890 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1891 fn = name;
1892 }
1893 val = iterative_hash_template_arg (fn, val);
1894 call_expr_arg_iterator ai;
1895 for (tree x = first_call_expr_arg (arg, &ai); x;
1896 x = next_call_expr_arg (&ai))
1897 val = iterative_hash_template_arg (x, val);
1898 return val;
1899 }
1900
1901 default:
1902 break;
1903 }
1904
1905 char tclass = TREE_CODE_CLASS (code);
1906 switch (tclass)
1907 {
1908 case tcc_type:
1909 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1910 {
1911 // We want an alias specialization that survived strip_typedefs
1912 // to hash differently from its TYPE_CANONICAL, to avoid hash
1913 // collisions that compare as different in template_args_equal.
1914 // These could be dependent specializations that strip_typedefs
1915 // left alone, or untouched specializations because
1916 // coerce_template_parms returns the unconverted template
1917 // arguments if it sees incomplete argument packs.
1918 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1919 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1920 }
1921
1922 switch (TREE_CODE (arg))
1923 {
1924 case TEMPLATE_TEMPLATE_PARM:
1925 {
1926 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1927
1928 /* Do not recurse with TPI directly, as that is unbounded
1929 recursion. */
1930 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1931 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1932 }
1933 break;
1934
1935 case DECLTYPE_TYPE:
1936 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1937 break;
1938
1939 default:
1940 if (tree canonical = TYPE_CANONICAL (arg))
1941 val = iterative_hash_object (TYPE_HASH (canonical), val);
1942 break;
1943 }
1944
1945 return val;
1946
1947 case tcc_declaration:
1948 case tcc_constant:
1949 return iterative_hash_expr (arg, val);
1950
1951 default:
1952 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1953 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1954 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1955 return val;
1956 }
1957
1958 gcc_unreachable ();
1959 return 0;
1960 }
1961
1962 /* Unregister the specialization SPEC as a specialization of TMPL.
1963 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1964 if the SPEC was listed as a specialization of TMPL.
1965
1966 Note that SPEC has been ggc_freed, so we can't look inside it. */
1967
1968 bool
1969 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1970 {
1971 spec_entry *entry;
1972 spec_entry elt;
1973
1974 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1975 elt.args = TI_ARGS (tinfo);
1976 elt.spec = NULL_TREE;
1977
1978 entry = decl_specializations->find (&elt);
1979 if (entry != NULL)
1980 {
1981 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1982 gcc_assert (new_spec != NULL_TREE);
1983 entry->spec = new_spec;
1984 return 1;
1985 }
1986
1987 return 0;
1988 }
1989
1990 /* Like register_specialization, but for local declarations. We are
1991 registering SPEC, an instantiation of TMPL. */
1992
1993 void
1994 register_local_specialization (tree spec, tree tmpl)
1995 {
1996 gcc_assert (tmpl != spec);
1997 local_specializations->put (tmpl, spec);
1998 }
1999
2000 /* TYPE is a class type. Returns true if TYPE is an explicitly
2001 specialized class. */
2002
2003 bool
2004 explicit_class_specialization_p (tree type)
2005 {
2006 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2007 return false;
2008 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2009 }
2010
2011 /* Print the list of functions at FNS, going through all the overloads
2012 for each element of the list. Alternatively, FNS cannot be a
2013 TREE_LIST, in which case it will be printed together with all the
2014 overloads.
2015
2016 MORE and *STR should respectively be FALSE and NULL when the function
2017 is called from the outside. They are used internally on recursive
2018 calls. print_candidates manages the two parameters and leaves NULL
2019 in *STR when it ends. */
2020
2021 static void
2022 print_candidates_1 (tree fns, char **str, bool more = false)
2023 {
2024 if (TREE_CODE (fns) == TREE_LIST)
2025 for (; fns; fns = TREE_CHAIN (fns))
2026 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2027 else
2028 for (lkp_iterator iter (fns); iter;)
2029 {
2030 tree cand = *iter;
2031 ++iter;
2032
2033 const char *pfx = *str;
2034 if (!pfx)
2035 {
2036 if (more || iter)
2037 pfx = _("candidates are:");
2038 else
2039 pfx = _("candidate is:");
2040 *str = get_spaces (pfx);
2041 }
2042 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2043 }
2044 }
2045
2046 /* Print the list of candidate FNS in an error message. FNS can also
2047 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2048
2049 void
2050 print_candidates (tree fns)
2051 {
2052 char *str = NULL;
2053 print_candidates_1 (fns, &str);
2054 free (str);
2055 }
2056
2057 /* Get a (possibly) constrained template declaration for the
2058 purpose of ordering candidates. */
2059 static tree
2060 get_template_for_ordering (tree list)
2061 {
2062 gcc_assert (TREE_CODE (list) == TREE_LIST);
2063 tree f = TREE_VALUE (list);
2064 if (tree ti = DECL_TEMPLATE_INFO (f))
2065 return TI_TEMPLATE (ti);
2066 return f;
2067 }
2068
2069 /* Among candidates having the same signature, return the
2070 most constrained or NULL_TREE if there is no best candidate.
2071 If the signatures of candidates vary (e.g., template
2072 specialization vs. member function), then there can be no
2073 most constrained.
2074
2075 Note that we don't compare constraints on the functions
2076 themselves, but rather those of their templates. */
2077 static tree
2078 most_constrained_function (tree candidates)
2079 {
2080 // Try to find the best candidate in a first pass.
2081 tree champ = candidates;
2082 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2083 {
2084 int winner = more_constrained (get_template_for_ordering (champ),
2085 get_template_for_ordering (c));
2086 if (winner == -1)
2087 champ = c; // The candidate is more constrained
2088 else if (winner == 0)
2089 return NULL_TREE; // Neither is more constrained
2090 }
2091
2092 // Verify that the champ is better than previous candidates.
2093 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2094 if (!more_constrained (get_template_for_ordering (champ),
2095 get_template_for_ordering (c)))
2096 return NULL_TREE;
2097 }
2098
2099 return champ;
2100 }
2101
2102
2103 /* Returns the template (one of the functions given by TEMPLATE_ID)
2104 which can be specialized to match the indicated DECL with the
2105 explicit template args given in TEMPLATE_ID. The DECL may be
2106 NULL_TREE if none is available. In that case, the functions in
2107 TEMPLATE_ID are non-members.
2108
2109 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2110 specialization of a member template.
2111
2112 The TEMPLATE_COUNT is the number of references to qualifying
2113 template classes that appeared in the name of the function. See
2114 check_explicit_specialization for a more accurate description.
2115
2116 TSK indicates what kind of template declaration (if any) is being
2117 declared. TSK_TEMPLATE indicates that the declaration given by
2118 DECL, though a FUNCTION_DECL, has template parameters, and is
2119 therefore a template function.
2120
2121 The template args (those explicitly specified and those deduced)
2122 are output in a newly created vector *TARGS_OUT.
2123
2124 If it is impossible to determine the result, an error message is
2125 issued. The error_mark_node is returned to indicate failure. */
2126
2127 static tree
2128 determine_specialization (tree template_id,
2129 tree decl,
2130 tree* targs_out,
2131 int need_member_template,
2132 int template_count,
2133 tmpl_spec_kind tsk)
2134 {
2135 tree fns;
2136 tree targs;
2137 tree explicit_targs;
2138 tree candidates = NULL_TREE;
2139
2140 /* A TREE_LIST of templates of which DECL may be a specialization.
2141 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2142 corresponding TREE_PURPOSE is the set of template arguments that,
2143 when used to instantiate the template, would produce a function
2144 with the signature of DECL. */
2145 tree templates = NULL_TREE;
2146 int header_count;
2147 cp_binding_level *b;
2148
2149 *targs_out = NULL_TREE;
2150
2151 if (template_id == error_mark_node || decl == error_mark_node)
2152 return error_mark_node;
2153
2154 /* We shouldn't be specializing a member template of an
2155 unspecialized class template; we already gave an error in
2156 check_specialization_scope, now avoid crashing. */
2157 if (!VAR_P (decl)
2158 && template_count && DECL_CLASS_SCOPE_P (decl)
2159 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2160 {
2161 gcc_assert (errorcount);
2162 return error_mark_node;
2163 }
2164
2165 fns = TREE_OPERAND (template_id, 0);
2166 explicit_targs = TREE_OPERAND (template_id, 1);
2167
2168 if (fns == error_mark_node)
2169 return error_mark_node;
2170
2171 /* Check for baselinks. */
2172 if (BASELINK_P (fns))
2173 fns = BASELINK_FUNCTIONS (fns);
2174
2175 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2176 {
2177 error_at (DECL_SOURCE_LOCATION (decl),
2178 "%qD is not a function template", fns);
2179 return error_mark_node;
2180 }
2181 else if (VAR_P (decl) && !variable_template_p (fns))
2182 {
2183 error ("%qD is not a variable template", fns);
2184 return error_mark_node;
2185 }
2186
2187 /* Count the number of template headers specified for this
2188 specialization. */
2189 header_count = 0;
2190 for (b = current_binding_level;
2191 b->kind == sk_template_parms;
2192 b = b->level_chain)
2193 ++header_count;
2194
2195 tree orig_fns = fns;
2196
2197 if (variable_template_p (fns))
2198 {
2199 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2200 targs = coerce_template_parms (parms, explicit_targs, fns,
2201 tf_warning_or_error,
2202 /*req_all*/true, /*use_defarg*/true);
2203 if (targs != error_mark_node)
2204 templates = tree_cons (targs, fns, templates);
2205 }
2206 else for (lkp_iterator iter (fns); iter; ++iter)
2207 {
2208 tree fn = *iter;
2209
2210 if (TREE_CODE (fn) == TEMPLATE_DECL)
2211 {
2212 tree decl_arg_types;
2213 tree fn_arg_types;
2214 tree insttype;
2215
2216 /* In case of explicit specialization, we need to check if
2217 the number of template headers appearing in the specialization
2218 is correct. This is usually done in check_explicit_specialization,
2219 but the check done there cannot be exhaustive when specializing
2220 member functions. Consider the following code:
2221
2222 template <> void A<int>::f(int);
2223 template <> template <> void A<int>::f(int);
2224
2225 Assuming that A<int> is not itself an explicit specialization
2226 already, the first line specializes "f" which is a non-template
2227 member function, whilst the second line specializes "f" which
2228 is a template member function. So both lines are syntactically
2229 correct, and check_explicit_specialization does not reject
2230 them.
2231
2232 Here, we can do better, as we are matching the specialization
2233 against the declarations. We count the number of template
2234 headers, and we check if they match TEMPLATE_COUNT + 1
2235 (TEMPLATE_COUNT is the number of qualifying template classes,
2236 plus there must be another header for the member template
2237 itself).
2238
2239 Notice that if header_count is zero, this is not a
2240 specialization but rather a template instantiation, so there
2241 is no check we can perform here. */
2242 if (header_count && header_count != template_count + 1)
2243 continue;
2244
2245 /* Check that the number of template arguments at the
2246 innermost level for DECL is the same as for FN. */
2247 if (current_binding_level->kind == sk_template_parms
2248 && !current_binding_level->explicit_spec_p
2249 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2250 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2251 (current_template_parms))))
2252 continue;
2253
2254 /* DECL might be a specialization of FN. */
2255 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2256 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2257
2258 /* For a non-static member function, we need to make sure
2259 that the const qualification is the same. Since
2260 get_bindings does not try to merge the "this" parameter,
2261 we must do the comparison explicitly. */
2262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2263 {
2264 if (!same_type_p (TREE_VALUE (fn_arg_types),
2265 TREE_VALUE (decl_arg_types)))
2266 continue;
2267
2268 /* And the ref-qualification. */
2269 if (type_memfn_rqual (TREE_TYPE (decl))
2270 != type_memfn_rqual (TREE_TYPE (fn)))
2271 continue;
2272 }
2273
2274 /* Skip the "this" parameter and, for constructors of
2275 classes with virtual bases, the VTT parameter. A
2276 full specialization of a constructor will have a VTT
2277 parameter, but a template never will. */
2278 decl_arg_types
2279 = skip_artificial_parms_for (decl, decl_arg_types);
2280 fn_arg_types
2281 = skip_artificial_parms_for (fn, fn_arg_types);
2282
2283 /* Function templates cannot be specializations; there are
2284 no partial specializations of functions. Therefore, if
2285 the type of DECL does not match FN, there is no
2286 match.
2287
2288 Note that it should never be the case that we have both
2289 candidates added here, and for regular member functions
2290 below. */
2291 if (tsk == tsk_template)
2292 {
2293 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2294 current_template_parms))
2295 continue;
2296 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2297 TREE_TYPE (TREE_TYPE (fn))))
2298 continue;
2299 if (!compparms (fn_arg_types, decl_arg_types))
2300 continue;
2301
2302 tree freq = get_trailing_function_requirements (fn);
2303 tree dreq = get_trailing_function_requirements (decl);
2304 if (!freq != !dreq)
2305 continue;
2306 if (freq)
2307 {
2308 tree fargs = DECL_TI_ARGS (fn);
2309 tsubst_flags_t complain = tf_none;
2310 freq = tsubst_constraint (freq, fargs, complain, fn);
2311 if (!cp_tree_equal (freq, dreq))
2312 continue;
2313 }
2314
2315 candidates = tree_cons (NULL_TREE, fn, candidates);
2316 continue;
2317 }
2318
2319 /* See whether this function might be a specialization of this
2320 template. Suppress access control because we might be trying
2321 to make this specialization a friend, and we have already done
2322 access control for the declaration of the specialization. */
2323 push_deferring_access_checks (dk_no_check);
2324 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2325 pop_deferring_access_checks ();
2326
2327 if (!targs)
2328 /* We cannot deduce template arguments that when used to
2329 specialize TMPL will produce DECL. */
2330 continue;
2331
2332 if (uses_template_parms (targs))
2333 /* We deduced something involving 'auto', which isn't a valid
2334 template argument. */
2335 continue;
2336
2337 /* Remove, from the set of candidates, all those functions
2338 whose constraints are not satisfied. */
2339 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2340 continue;
2341
2342 // Then, try to form the new function type.
2343 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2344 if (insttype == error_mark_node)
2345 continue;
2346 fn_arg_types
2347 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2348 if (!compparms (fn_arg_types, decl_arg_types))
2349 continue;
2350
2351 /* Save this template, and the arguments deduced. */
2352 templates = tree_cons (targs, fn, templates);
2353 }
2354 else if (need_member_template)
2355 /* FN is an ordinary member function, and we need a
2356 specialization of a member template. */
2357 ;
2358 else if (TREE_CODE (fn) != FUNCTION_DECL)
2359 /* We can get IDENTIFIER_NODEs here in certain erroneous
2360 cases. */
2361 ;
2362 else if (!DECL_FUNCTION_MEMBER_P (fn))
2363 /* This is just an ordinary non-member function. Nothing can
2364 be a specialization of that. */
2365 ;
2366 else if (DECL_ARTIFICIAL (fn))
2367 /* Cannot specialize functions that are created implicitly. */
2368 ;
2369 else
2370 {
2371 tree decl_arg_types;
2372
2373 /* This is an ordinary member function. However, since
2374 we're here, we can assume its enclosing class is a
2375 template class. For example,
2376
2377 template <typename T> struct S { void f(); };
2378 template <> void S<int>::f() {}
2379
2380 Here, S<int>::f is a non-template, but S<int> is a
2381 template class. If FN has the same type as DECL, we
2382 might be in business. */
2383
2384 if (!DECL_TEMPLATE_INFO (fn))
2385 /* Its enclosing class is an explicit specialization
2386 of a template class. This is not a candidate. */
2387 continue;
2388
2389 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2390 TREE_TYPE (TREE_TYPE (fn))))
2391 /* The return types differ. */
2392 continue;
2393
2394 /* Adjust the type of DECL in case FN is a static member. */
2395 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2396 if (DECL_STATIC_FUNCTION_P (fn)
2397 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2398 decl_arg_types = TREE_CHAIN (decl_arg_types);
2399
2400 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2401 decl_arg_types))
2402 continue;
2403
2404 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2405 && (type_memfn_rqual (TREE_TYPE (decl))
2406 != type_memfn_rqual (TREE_TYPE (fn))))
2407 continue;
2408
2409 // If the deduced arguments do not satisfy the constraints,
2410 // this is not a candidate.
2411 if (flag_concepts && !constraints_satisfied_p (fn))
2412 continue;
2413
2414 // Add the candidate.
2415 candidates = tree_cons (NULL_TREE, fn, candidates);
2416 }
2417 }
2418
2419 if (templates && TREE_CHAIN (templates))
2420 {
2421 /* We have:
2422
2423 [temp.expl.spec]
2424
2425 It is possible for a specialization with a given function
2426 signature to be instantiated from more than one function
2427 template. In such cases, explicit specification of the
2428 template arguments must be used to uniquely identify the
2429 function template specialization being specialized.
2430
2431 Note that here, there's no suggestion that we're supposed to
2432 determine which of the candidate templates is most
2433 specialized. However, we, also have:
2434
2435 [temp.func.order]
2436
2437 Partial ordering of overloaded function template
2438 declarations is used in the following contexts to select
2439 the function template to which a function template
2440 specialization refers:
2441
2442 -- when an explicit specialization refers to a function
2443 template.
2444
2445 So, we do use the partial ordering rules, at least for now.
2446 This extension can only serve to make invalid programs valid,
2447 so it's safe. And, there is strong anecdotal evidence that
2448 the committee intended the partial ordering rules to apply;
2449 the EDG front end has that behavior, and John Spicer claims
2450 that the committee simply forgot to delete the wording in
2451 [temp.expl.spec]. */
2452 tree tmpl = most_specialized_instantiation (templates);
2453 if (tmpl != error_mark_node)
2454 {
2455 templates = tmpl;
2456 TREE_CHAIN (templates) = NULL_TREE;
2457 }
2458 }
2459
2460 // Concepts allows multiple declarations of member functions
2461 // with the same signature. Like above, we need to rely on
2462 // on the partial ordering of those candidates to determine which
2463 // is the best.
2464 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2465 {
2466 if (tree cand = most_constrained_function (candidates))
2467 {
2468 candidates = cand;
2469 TREE_CHAIN (cand) = NULL_TREE;
2470 }
2471 }
2472
2473 if (templates == NULL_TREE && candidates == NULL_TREE)
2474 {
2475 error ("template-id %qD for %q+D does not match any template "
2476 "declaration", template_id, decl);
2477 if (header_count && header_count != template_count + 1)
2478 inform (DECL_SOURCE_LOCATION (decl),
2479 "saw %d %<template<>%>, need %d for "
2480 "specializing a member function template",
2481 header_count, template_count + 1);
2482 else
2483 print_candidates (orig_fns);
2484 return error_mark_node;
2485 }
2486 else if ((templates && TREE_CHAIN (templates))
2487 || (candidates && TREE_CHAIN (candidates))
2488 || (templates && candidates))
2489 {
2490 error ("ambiguous template specialization %qD for %q+D",
2491 template_id, decl);
2492 candidates = chainon (candidates, templates);
2493 print_candidates (candidates);
2494 return error_mark_node;
2495 }
2496
2497 /* We have one, and exactly one, match. */
2498 if (candidates)
2499 {
2500 tree fn = TREE_VALUE (candidates);
2501 *targs_out = copy_node (DECL_TI_ARGS (fn));
2502
2503 /* Propagate the candidate's constraints to the declaration. */
2504 if (tsk != tsk_template)
2505 set_constraints (decl, get_constraints (fn));
2506
2507 /* DECL is a re-declaration or partial instantiation of a template
2508 function. */
2509 if (TREE_CODE (fn) == TEMPLATE_DECL)
2510 return fn;
2511 /* It was a specialization of an ordinary member function in a
2512 template class. */
2513 return DECL_TI_TEMPLATE (fn);
2514 }
2515
2516 /* It was a specialization of a template. */
2517 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2518 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2519 {
2520 *targs_out = copy_node (targs);
2521 SET_TMPL_ARGS_LEVEL (*targs_out,
2522 TMPL_ARGS_DEPTH (*targs_out),
2523 TREE_PURPOSE (templates));
2524 }
2525 else
2526 *targs_out = TREE_PURPOSE (templates);
2527 return TREE_VALUE (templates);
2528 }
2529
2530 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2531 but with the default argument values filled in from those in the
2532 TMPL_TYPES. */
2533
2534 static tree
2535 copy_default_args_to_explicit_spec_1 (tree spec_types,
2536 tree tmpl_types)
2537 {
2538 tree new_spec_types;
2539
2540 if (!spec_types)
2541 return NULL_TREE;
2542
2543 if (spec_types == void_list_node)
2544 return void_list_node;
2545
2546 /* Substitute into the rest of the list. */
2547 new_spec_types =
2548 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2549 TREE_CHAIN (tmpl_types));
2550
2551 /* Add the default argument for this parameter. */
2552 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2553 TREE_VALUE (spec_types),
2554 new_spec_types);
2555 }
2556
2557 /* DECL is an explicit specialization. Replicate default arguments
2558 from the template it specializes. (That way, code like:
2559
2560 template <class T> void f(T = 3);
2561 template <> void f(double);
2562 void g () { f (); }
2563
2564 works, as required.) An alternative approach would be to look up
2565 the correct default arguments at the call-site, but this approach
2566 is consistent with how implicit instantiations are handled. */
2567
2568 static void
2569 copy_default_args_to_explicit_spec (tree decl)
2570 {
2571 tree tmpl;
2572 tree spec_types;
2573 tree tmpl_types;
2574 tree new_spec_types;
2575 tree old_type;
2576 tree new_type;
2577 tree t;
2578 tree object_type = NULL_TREE;
2579 tree in_charge = NULL_TREE;
2580 tree vtt = NULL_TREE;
2581
2582 /* See if there's anything we need to do. */
2583 tmpl = DECL_TI_TEMPLATE (decl);
2584 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2585 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2586 if (TREE_PURPOSE (t))
2587 break;
2588 if (!t)
2589 return;
2590
2591 old_type = TREE_TYPE (decl);
2592 spec_types = TYPE_ARG_TYPES (old_type);
2593
2594 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2595 {
2596 /* Remove the this pointer, but remember the object's type for
2597 CV quals. */
2598 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2599 spec_types = TREE_CHAIN (spec_types);
2600 tmpl_types = TREE_CHAIN (tmpl_types);
2601
2602 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2603 {
2604 /* DECL may contain more parameters than TMPL due to the extra
2605 in-charge parameter in constructors and destructors. */
2606 in_charge = spec_types;
2607 spec_types = TREE_CHAIN (spec_types);
2608 }
2609 if (DECL_HAS_VTT_PARM_P (decl))
2610 {
2611 vtt = spec_types;
2612 spec_types = TREE_CHAIN (spec_types);
2613 }
2614 }
2615
2616 /* Compute the merged default arguments. */
2617 new_spec_types =
2618 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2619
2620 /* Compute the new FUNCTION_TYPE. */
2621 if (object_type)
2622 {
2623 if (vtt)
2624 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2625 TREE_VALUE (vtt),
2626 new_spec_types);
2627
2628 if (in_charge)
2629 /* Put the in-charge parameter back. */
2630 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2631 TREE_VALUE (in_charge),
2632 new_spec_types);
2633
2634 new_type = build_method_type_directly (object_type,
2635 TREE_TYPE (old_type),
2636 new_spec_types);
2637 }
2638 else
2639 new_type = build_function_type (TREE_TYPE (old_type),
2640 new_spec_types);
2641 new_type = cp_build_type_attribute_variant (new_type,
2642 TYPE_ATTRIBUTES (old_type));
2643 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2644
2645 TREE_TYPE (decl) = new_type;
2646 }
2647
2648 /* Return the number of template headers we expect to see for a definition
2649 or specialization of CTYPE or one of its non-template members. */
2650
2651 int
2652 num_template_headers_for_class (tree ctype)
2653 {
2654 int num_templates = 0;
2655
2656 while (ctype && CLASS_TYPE_P (ctype))
2657 {
2658 /* You're supposed to have one `template <...>' for every
2659 template class, but you don't need one for a full
2660 specialization. For example:
2661
2662 template <class T> struct S{};
2663 template <> struct S<int> { void f(); };
2664 void S<int>::f () {}
2665
2666 is correct; there shouldn't be a `template <>' for the
2667 definition of `S<int>::f'. */
2668 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2669 /* If CTYPE does not have template information of any
2670 kind, then it is not a template, nor is it nested
2671 within a template. */
2672 break;
2673 if (explicit_class_specialization_p (ctype))
2674 break;
2675 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2676 ++num_templates;
2677
2678 ctype = TYPE_CONTEXT (ctype);
2679 }
2680
2681 return num_templates;
2682 }
2683
2684 /* Do a simple sanity check on the template headers that precede the
2685 variable declaration DECL. */
2686
2687 void
2688 check_template_variable (tree decl)
2689 {
2690 tree ctx = CP_DECL_CONTEXT (decl);
2691 int wanted = num_template_headers_for_class (ctx);
2692 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2693 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2694 {
2695 if (cxx_dialect < cxx14)
2696 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2697 "variable templates only available with "
2698 "%<-std=c++14%> or %<-std=gnu++14%>");
2699
2700 // Namespace-scope variable templates should have a template header.
2701 ++wanted;
2702 }
2703 if (template_header_count > wanted)
2704 {
2705 auto_diagnostic_group d;
2706 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2707 "too many template headers for %qD "
2708 "(should be %d)",
2709 decl, wanted);
2710 if (warned && CLASS_TYPE_P (ctx)
2711 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2712 inform (DECL_SOURCE_LOCATION (decl),
2713 "members of an explicitly specialized class are defined "
2714 "without a template header");
2715 }
2716 }
2717
2718 /* An explicit specialization whose declarator-id or class-head-name is not
2719 qualified shall be declared in the nearest enclosing namespace of the
2720 template, or, if the namespace is inline (7.3.1), any namespace from its
2721 enclosing namespace set.
2722
2723 If the name declared in the explicit instantiation is an unqualified name,
2724 the explicit instantiation shall appear in the namespace where its template
2725 is declared or, if that namespace is inline (7.3.1), any namespace from its
2726 enclosing namespace set. */
2727
2728 void
2729 check_unqualified_spec_or_inst (tree t, location_t loc)
2730 {
2731 tree tmpl = most_general_template (t);
2732 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2733 && !is_nested_namespace (current_namespace,
2734 CP_DECL_CONTEXT (tmpl), true))
2735 {
2736 if (processing_specialization)
2737 permerror (loc, "explicit specialization of %qD outside its "
2738 "namespace must use a nested-name-specifier", tmpl);
2739 else if (processing_explicit_instantiation
2740 && cxx_dialect >= cxx11)
2741 /* This was allowed in C++98, so only pedwarn. */
2742 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2743 "outside its namespace must use a nested-name-"
2744 "specifier", tmpl);
2745 }
2746 }
2747
2748 /* Warn for a template specialization SPEC that is missing some of a set
2749 of function or type attributes that the template TEMPL is declared with.
2750 ATTRLIST is a list of additional attributes that SPEC should be taken
2751 to ultimately be declared with. */
2752
2753 static void
2754 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2755 {
2756 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2757 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2758
2759 /* Avoid warning if the difference between the primary and
2760 the specialization is not in one of the attributes below. */
2761 const char* const blacklist[] = {
2762 "alloc_align", "alloc_size", "assume_aligned", "format",
2763 "format_arg", "malloc", "nonnull", NULL
2764 };
2765
2766 /* Put together a list of the black listed attributes that the primary
2767 template is declared with that the specialization is not, in case
2768 it's not apparent from the most recent declaration of the primary. */
2769 pretty_printer str;
2770 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2771 blacklist, &str);
2772
2773 if (!nattrs)
2774 return;
2775
2776 auto_diagnostic_group d;
2777 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2778 "explicit specialization %q#D may be missing attributes",
2779 spec))
2780 inform (DECL_SOURCE_LOCATION (tmpl),
2781 nattrs > 1
2782 ? G_("missing primary template attributes %s")
2783 : G_("missing primary template attribute %s"),
2784 pp_formatted_text (&str));
2785 }
2786
2787 /* Check to see if the function just declared, as indicated in
2788 DECLARATOR, and in DECL, is a specialization of a function
2789 template. We may also discover that the declaration is an explicit
2790 instantiation at this point.
2791
2792 Returns DECL, or an equivalent declaration that should be used
2793 instead if all goes well. Issues an error message if something is
2794 amiss. Returns error_mark_node if the error is not easily
2795 recoverable.
2796
2797 FLAGS is a bitmask consisting of the following flags:
2798
2799 2: The function has a definition.
2800 4: The function is a friend.
2801
2802 The TEMPLATE_COUNT is the number of references to qualifying
2803 template classes that appeared in the name of the function. For
2804 example, in
2805
2806 template <class T> struct S { void f(); };
2807 void S<int>::f();
2808
2809 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2810 classes are not counted in the TEMPLATE_COUNT, so that in
2811
2812 template <class T> struct S {};
2813 template <> struct S<int> { void f(); }
2814 template <> void S<int>::f();
2815
2816 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2817 invalid; there should be no template <>.)
2818
2819 If the function is a specialization, it is marked as such via
2820 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2821 is set up correctly, and it is added to the list of specializations
2822 for that template. */
2823
2824 tree
2825 check_explicit_specialization (tree declarator,
2826 tree decl,
2827 int template_count,
2828 int flags,
2829 tree attrlist)
2830 {
2831 int have_def = flags & 2;
2832 int is_friend = flags & 4;
2833 bool is_concept = flags & 8;
2834 int specialization = 0;
2835 int explicit_instantiation = 0;
2836 int member_specialization = 0;
2837 tree ctype = DECL_CLASS_CONTEXT (decl);
2838 tree dname = DECL_NAME (decl);
2839 tmpl_spec_kind tsk;
2840
2841 if (is_friend)
2842 {
2843 if (!processing_specialization)
2844 tsk = tsk_none;
2845 else
2846 tsk = tsk_excessive_parms;
2847 }
2848 else
2849 tsk = current_tmpl_spec_kind (template_count);
2850
2851 switch (tsk)
2852 {
2853 case tsk_none:
2854 if (processing_specialization && !VAR_P (decl))
2855 {
2856 specialization = 1;
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 }
2859 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2860 {
2861 if (is_friend)
2862 /* This could be something like:
2863
2864 template <class T> void f(T);
2865 class S { friend void f<>(int); } */
2866 specialization = 1;
2867 else
2868 {
2869 /* This case handles bogus declarations like template <>
2870 template <class T> void f<int>(); */
2871
2872 error_at (cp_expr_loc_or_input_loc (declarator),
2873 "template-id %qE in declaration of primary template",
2874 declarator);
2875 return decl;
2876 }
2877 }
2878 break;
2879
2880 case tsk_invalid_member_spec:
2881 /* The error has already been reported in
2882 check_specialization_scope. */
2883 return error_mark_node;
2884
2885 case tsk_invalid_expl_inst:
2886 error ("template parameter list used in explicit instantiation");
2887
2888 /* Fall through. */
2889
2890 case tsk_expl_inst:
2891 if (have_def)
2892 error ("definition provided for explicit instantiation");
2893
2894 explicit_instantiation = 1;
2895 break;
2896
2897 case tsk_excessive_parms:
2898 case tsk_insufficient_parms:
2899 if (tsk == tsk_excessive_parms)
2900 error ("too many template parameter lists in declaration of %qD",
2901 decl);
2902 else if (template_header_count)
2903 error("too few template parameter lists in declaration of %qD", decl);
2904 else
2905 error("explicit specialization of %qD must be introduced by "
2906 "%<template <>%>", decl);
2907
2908 /* Fall through. */
2909 case tsk_expl_spec:
2910 if (is_concept)
2911 error ("explicit specialization declared %<concept%>");
2912
2913 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2914 /* In cases like template<> constexpr bool v = true;
2915 We'll give an error in check_template_variable. */
2916 break;
2917
2918 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2919 if (ctype)
2920 member_specialization = 1;
2921 else
2922 specialization = 1;
2923 break;
2924
2925 case tsk_template:
2926 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2927 {
2928 /* This case handles bogus declarations like template <>
2929 template <class T> void f<int>(); */
2930
2931 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2932 error_at (cp_expr_loc_or_input_loc (declarator),
2933 "template-id %qE in declaration of primary template",
2934 declarator);
2935 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2936 {
2937 /* Partial specialization of variable template. */
2938 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2939 specialization = 1;
2940 goto ok;
2941 }
2942 else if (cxx_dialect < cxx14)
2943 error_at (cp_expr_loc_or_input_loc (declarator),
2944 "non-type partial specialization %qE "
2945 "is not allowed", declarator);
2946 else
2947 error_at (cp_expr_loc_or_input_loc (declarator),
2948 "non-class, non-variable partial specialization %qE "
2949 "is not allowed", declarator);
2950 return decl;
2951 ok:;
2952 }
2953
2954 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2955 /* This is a specialization of a member template, without
2956 specialization the containing class. Something like:
2957
2958 template <class T> struct S {
2959 template <class U> void f (U);
2960 };
2961 template <> template <class U> void S<int>::f(U) {}
2962
2963 That's a specialization -- but of the entire template. */
2964 specialization = 1;
2965 break;
2966
2967 default:
2968 gcc_unreachable ();
2969 }
2970
2971 if ((specialization || member_specialization)
2972 /* This doesn't apply to variable templates. */
2973 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2974 {
2975 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2976 for (; t; t = TREE_CHAIN (t))
2977 if (TREE_PURPOSE (t))
2978 {
2979 permerror (input_location,
2980 "default argument specified in explicit specialization");
2981 break;
2982 }
2983 }
2984
2985 if (specialization || member_specialization || explicit_instantiation)
2986 {
2987 tree tmpl = NULL_TREE;
2988 tree targs = NULL_TREE;
2989 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2990 bool found_hidden = false;
2991
2992 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2993 if (!was_template_id)
2994 {
2995 tree fns;
2996
2997 gcc_assert (identifier_p (declarator));
2998 if (ctype)
2999 fns = dname;
3000 else
3001 {
3002 /* If there is no class context, the explicit instantiation
3003 must be at namespace scope. */
3004 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3005
3006 /* Find the namespace binding, using the declaration
3007 context. */
3008 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3009 LOOK_want::NORMAL, true);
3010 if (fns == error_mark_node)
3011 {
3012 /* If lookup fails, look for a friend declaration so we can
3013 give a better diagnostic. */
3014 fns = (lookup_qualified_name
3015 (CP_DECL_CONTEXT (decl), dname,
3016 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3017 /*complain*/true));
3018 found_hidden = true;
3019 }
3020
3021 if (fns == error_mark_node || !is_overloaded_fn (fns))
3022 {
3023 error ("%qD is not a template function", dname);
3024 fns = error_mark_node;
3025 }
3026 }
3027
3028 declarator = lookup_template_function (fns, NULL_TREE);
3029 }
3030
3031 if (declarator == error_mark_node)
3032 return error_mark_node;
3033
3034 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3035 {
3036 if (!explicit_instantiation)
3037 /* A specialization in class scope. This is invalid,
3038 but the error will already have been flagged by
3039 check_specialization_scope. */
3040 return error_mark_node;
3041 else
3042 {
3043 /* It's not valid to write an explicit instantiation in
3044 class scope, e.g.:
3045
3046 class C { template void f(); }
3047
3048 This case is caught by the parser. However, on
3049 something like:
3050
3051 template class C { void f(); };
3052
3053 (which is invalid) we can get here. The error will be
3054 issued later. */
3055 ;
3056 }
3057
3058 return decl;
3059 }
3060 else if (ctype != NULL_TREE
3061 && (identifier_p (TREE_OPERAND (declarator, 0))))
3062 {
3063 // We'll match variable templates in start_decl.
3064 if (VAR_P (decl))
3065 return decl;
3066
3067 /* Find the list of functions in ctype that have the same
3068 name as the declared function. */
3069 tree name = TREE_OPERAND (declarator, 0);
3070
3071 if (constructor_name_p (name, ctype))
3072 {
3073 if (DECL_CONSTRUCTOR_P (decl)
3074 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3075 : !CLASSTYPE_DESTRUCTOR (ctype))
3076 {
3077 /* From [temp.expl.spec]:
3078
3079 If such an explicit specialization for the member
3080 of a class template names an implicitly-declared
3081 special member function (clause _special_), the
3082 program is ill-formed.
3083
3084 Similar language is found in [temp.explicit]. */
3085 error ("specialization of implicitly-declared special member function");
3086 return error_mark_node;
3087 }
3088
3089 name = DECL_NAME (decl);
3090 }
3091
3092 /* For a type-conversion operator, We might be looking for
3093 `operator int' which will be a specialization of
3094 `operator T'. Grab all the conversion operators, and
3095 then select from them. */
3096 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3097 ? conv_op_identifier : name);
3098
3099 if (fns == NULL_TREE)
3100 {
3101 error ("no member function %qD declared in %qT", name, ctype);
3102 return error_mark_node;
3103 }
3104 else
3105 TREE_OPERAND (declarator, 0) = fns;
3106 }
3107
3108 /* Figure out what exactly is being specialized at this point.
3109 Note that for an explicit instantiation, even one for a
3110 member function, we cannot tell a priori whether the
3111 instantiation is for a member template, or just a member
3112 function of a template class. Even if a member template is
3113 being instantiated, the member template arguments may be
3114 elided if they can be deduced from the rest of the
3115 declaration. */
3116 tmpl = determine_specialization (declarator, decl,
3117 &targs,
3118 member_specialization,
3119 template_count,
3120 tsk);
3121
3122 if (!tmpl || tmpl == error_mark_node)
3123 /* We couldn't figure out what this declaration was
3124 specializing. */
3125 return error_mark_node;
3126 else
3127 {
3128 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3129 {
3130 auto_diagnostic_group d;
3131 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3132 "friend declaration %qD is not visible to "
3133 "explicit specialization", tmpl))
3134 inform (DECL_SOURCE_LOCATION (tmpl),
3135 "friend declaration here");
3136 }
3137
3138 if (!ctype && !is_friend
3139 && CP_DECL_CONTEXT (decl) == current_namespace)
3140 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3141
3142 tree gen_tmpl = most_general_template (tmpl);
3143
3144 if (explicit_instantiation)
3145 {
3146 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3147 is done by do_decl_instantiation later. */
3148
3149 int arg_depth = TMPL_ARGS_DEPTH (targs);
3150 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3151
3152 if (arg_depth > parm_depth)
3153 {
3154 /* If TMPL is not the most general template (for
3155 example, if TMPL is a friend template that is
3156 injected into namespace scope), then there will
3157 be too many levels of TARGS. Remove some of them
3158 here. */
3159 int i;
3160 tree new_targs;
3161
3162 new_targs = make_tree_vec (parm_depth);
3163 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3164 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3165 = TREE_VEC_ELT (targs, i);
3166 targs = new_targs;
3167 }
3168
3169 return instantiate_template (tmpl, targs, tf_error);
3170 }
3171
3172 /* If we thought that the DECL was a member function, but it
3173 turns out to be specializing a static member function,
3174 make DECL a static member function as well. */
3175 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3176 && DECL_STATIC_FUNCTION_P (tmpl)
3177 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3178 revert_static_member_fn (decl);
3179
3180 /* If this is a specialization of a member template of a
3181 template class, we want to return the TEMPLATE_DECL, not
3182 the specialization of it. */
3183 if (tsk == tsk_template && !was_template_id)
3184 {
3185 tree result = DECL_TEMPLATE_RESULT (tmpl);
3186 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3187 DECL_INITIAL (result) = NULL_TREE;
3188 if (have_def)
3189 {
3190 tree parm;
3191 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3192 DECL_SOURCE_LOCATION (result)
3193 = DECL_SOURCE_LOCATION (decl);
3194 /* We want to use the argument list specified in the
3195 definition, not in the original declaration. */
3196 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3197 for (parm = DECL_ARGUMENTS (result); parm;
3198 parm = DECL_CHAIN (parm))
3199 DECL_CONTEXT (parm) = result;
3200 }
3201 return register_specialization (tmpl, gen_tmpl, targs,
3202 is_friend, 0);
3203 }
3204
3205 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3206 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3207
3208 if (was_template_id)
3209 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3210
3211 /* Inherit default function arguments from the template
3212 DECL is specializing. */
3213 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3214 copy_default_args_to_explicit_spec (decl);
3215
3216 /* This specialization has the same protection as the
3217 template it specializes. */
3218 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3219 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3220
3221 /* 7.1.1-1 [dcl.stc]
3222
3223 A storage-class-specifier shall not be specified in an
3224 explicit specialization...
3225
3226 The parser rejects these, so unless action is taken here,
3227 explicit function specializations will always appear with
3228 global linkage.
3229
3230 The action recommended by the C++ CWG in response to C++
3231 defect report 605 is to make the storage class and linkage
3232 of the explicit specialization match the templated function:
3233
3234 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3235 */
3236 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3237 {
3238 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3239 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3240
3241 /* A concept cannot be specialized. */
3242 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3243 {
3244 error ("explicit specialization of function concept %qD",
3245 gen_tmpl);
3246 return error_mark_node;
3247 }
3248
3249 /* This specialization has the same linkage and visibility as
3250 the function template it specializes. */
3251 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3252 if (! TREE_PUBLIC (decl))
3253 {
3254 DECL_INTERFACE_KNOWN (decl) = 1;
3255 DECL_NOT_REALLY_EXTERN (decl) = 1;
3256 }
3257 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3258 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3259 {
3260 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3261 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3262 }
3263 }
3264
3265 /* If DECL is a friend declaration, declared using an
3266 unqualified name, the namespace associated with DECL may
3267 have been set incorrectly. For example, in:
3268
3269 template <typename T> void f(T);
3270 namespace N {
3271 struct S { friend void f<int>(int); }
3272 }
3273
3274 we will have set the DECL_CONTEXT for the friend
3275 declaration to N, rather than to the global namespace. */
3276 if (DECL_NAMESPACE_SCOPE_P (decl))
3277 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3278
3279 if (is_friend && !have_def)
3280 /* This is not really a declaration of a specialization.
3281 It's just the name of an instantiation. But, it's not
3282 a request for an instantiation, either. */
3283 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3284 else if (TREE_CODE (decl) == FUNCTION_DECL)
3285 /* A specialization is not necessarily COMDAT. */
3286 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3287 && DECL_DECLARED_INLINE_P (decl));
3288 else if (VAR_P (decl))
3289 DECL_COMDAT (decl) = false;
3290
3291 /* If this is a full specialization, register it so that we can find
3292 it again. Partial specializations will be registered in
3293 process_partial_specialization. */
3294 if (!processing_template_decl)
3295 {
3296 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3297
3298 decl = register_specialization (decl, gen_tmpl, targs,
3299 is_friend, 0);
3300 }
3301
3302
3303 /* A 'structor should already have clones. */
3304 gcc_assert (decl == error_mark_node
3305 || variable_template_p (tmpl)
3306 || !(DECL_CONSTRUCTOR_P (decl)
3307 || DECL_DESTRUCTOR_P (decl))
3308 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3309 }
3310 }
3311
3312 return decl;
3313 }
3314
3315 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3316 parameters. These are represented in the same format used for
3317 DECL_TEMPLATE_PARMS. */
3318
3319 int
3320 comp_template_parms (const_tree parms1, const_tree parms2)
3321 {
3322 const_tree p1;
3323 const_tree p2;
3324
3325 if (parms1 == parms2)
3326 return 1;
3327
3328 for (p1 = parms1, p2 = parms2;
3329 p1 != NULL_TREE && p2 != NULL_TREE;
3330 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3331 {
3332 tree t1 = TREE_VALUE (p1);
3333 tree t2 = TREE_VALUE (p2);
3334 int i;
3335
3336 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3337 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3338
3339 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3340 return 0;
3341
3342 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3343 {
3344 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3345 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3346
3347 /* If either of the template parameters are invalid, assume
3348 they match for the sake of error recovery. */
3349 if (error_operand_p (parm1) || error_operand_p (parm2))
3350 return 1;
3351
3352 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3353 return 0;
3354
3355 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3356 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3357 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3358 continue;
3359 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3360 return 0;
3361 }
3362 }
3363
3364 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3365 /* One set of parameters has more parameters lists than the
3366 other. */
3367 return 0;
3368
3369 return 1;
3370 }
3371
3372 /* Returns true if two template parameters are declared with
3373 equivalent constraints. */
3374
3375 static bool
3376 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3377 {
3378 tree req1 = TREE_TYPE (parm1);
3379 tree req2 = TREE_TYPE (parm2);
3380 if (!req1 != !req2)
3381 return false;
3382 if (req1)
3383 return cp_tree_equal (req1, req2);
3384 return true;
3385 }
3386
3387 /* Returns true when two template parameters are equivalent. */
3388
3389 static bool
3390 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3391 {
3392 tree decl1 = TREE_VALUE (parm1);
3393 tree decl2 = TREE_VALUE (parm2);
3394
3395 /* If either of the template parameters are invalid, assume
3396 they match for the sake of error recovery. */
3397 if (error_operand_p (decl1) || error_operand_p (decl2))
3398 return true;
3399
3400 /* ... they declare parameters of the same kind. */
3401 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3402 return false;
3403
3404 /* ... one parameter was introduced by a parameter declaration, then
3405 both are. This case arises as a result of eagerly rewriting declarations
3406 during parsing. */
3407 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3408 return false;
3409
3410 /* ... if either declares a pack, they both do. */
3411 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3412 return false;
3413
3414 if (TREE_CODE (decl1) == PARM_DECL)
3415 {
3416 /* ... if they declare non-type parameters, the types are equivalent. */
3417 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3418 return false;
3419 }
3420 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3421 {
3422 /* ... if they declare template template parameters, their template
3423 parameter lists are equivalent. */
3424 if (!template_heads_equivalent_p (decl1, decl2))
3425 return false;
3426 }
3427
3428 /* ... if they are declared with a qualified-concept name, they both
3429 are, and those names are equivalent. */
3430 return template_parameter_constraints_equivalent_p (parm1, parm2);
3431 }
3432
3433 /* Returns true if two template parameters lists are equivalent.
3434 Two template parameter lists are equivalent if they have the
3435 same length and their corresponding parameters are equivalent.
3436
3437 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3438 data structure returned by DECL_TEMPLATE_PARMS.
3439
3440 This is generally the same implementation as comp_template_parms
3441 except that it also the concept names and arguments used to
3442 introduce parameters. */
3443
3444 static bool
3445 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3446 {
3447 if (parms1 == parms2)
3448 return true;
3449
3450 const_tree p1 = parms1;
3451 const_tree p2 = parms2;
3452 while (p1 != NULL_TREE && p2 != NULL_TREE)
3453 {
3454 tree list1 = TREE_VALUE (p1);
3455 tree list2 = TREE_VALUE (p2);
3456
3457 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3458 return 0;
3459
3460 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3461 {
3462 tree parm1 = TREE_VEC_ELT (list1, i);
3463 tree parm2 = TREE_VEC_ELT (list2, i);
3464 if (!template_parameters_equivalent_p (parm1, parm2))
3465 return false;
3466 }
3467
3468 p1 = TREE_CHAIN (p1);
3469 p2 = TREE_CHAIN (p2);
3470 }
3471
3472 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3473 return false;
3474
3475 return true;
3476 }
3477
3478 /* Return true if the requires-clause of the template parameter lists are
3479 equivalent and false otherwise. */
3480 static bool
3481 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3482 {
3483 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3484 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3485 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3486 return false;
3487 if (!cp_tree_equal (req1, req2))
3488 return false;
3489 return true;
3490 }
3491
3492 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3493 Two template heads are equivalent if their template parameter
3494 lists are equivalent and their requires clauses are equivalent.
3495
3496 In pre-C++20, this is equivalent to calling comp_template_parms
3497 for the template parameters of TMPL1 and TMPL2. */
3498
3499 bool
3500 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3501 {
3502 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3503 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3504
3505 /* Don't change the matching rules for pre-C++20. */
3506 if (cxx_dialect < cxx20)
3507 return comp_template_parms (parms1, parms2);
3508
3509 /* ... have the same number of template parameters, and their
3510 corresponding parameters are equivalent. */
3511 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3512 return false;
3513
3514 /* ... if either has a requires-clause, they both do and their
3515 corresponding constraint-expressions are equivalent. */
3516 return template_requirements_equivalent_p (parms1, parms2);
3517 }
3518
3519 /* Determine whether PARM is a parameter pack. */
3520
3521 bool
3522 template_parameter_pack_p (const_tree parm)
3523 {
3524 /* Determine if we have a non-type template parameter pack. */
3525 if (TREE_CODE (parm) == PARM_DECL)
3526 return (DECL_TEMPLATE_PARM_P (parm)
3527 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3528 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3529 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3530
3531 /* If this is a list of template parameters, we could get a
3532 TYPE_DECL or a TEMPLATE_DECL. */
3533 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3534 parm = TREE_TYPE (parm);
3535
3536 /* Otherwise it must be a type template parameter. */
3537 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3538 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3539 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3540 }
3541
3542 /* Determine if T is a function parameter pack. */
3543
3544 bool
3545 function_parameter_pack_p (const_tree t)
3546 {
3547 if (t && TREE_CODE (t) == PARM_DECL)
3548 return DECL_PACK_P (t);
3549 return false;
3550 }
3551
3552 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3553 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3554
3555 tree
3556 get_function_template_decl (const_tree primary_func_tmpl_inst)
3557 {
3558 if (! primary_func_tmpl_inst
3559 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3560 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3561 return NULL;
3562
3563 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3564 }
3565
3566 /* Return true iff the function parameter PARAM_DECL was expanded
3567 from the function parameter pack PACK. */
3568
3569 bool
3570 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3571 {
3572 if (DECL_ARTIFICIAL (param_decl)
3573 || !function_parameter_pack_p (pack))
3574 return false;
3575
3576 /* The parameter pack and its pack arguments have the same
3577 DECL_PARM_INDEX. */
3578 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3579 }
3580
3581 /* Determine whether ARGS describes a variadic template args list,
3582 i.e., one that is terminated by a template argument pack. */
3583
3584 static bool
3585 template_args_variadic_p (tree args)
3586 {
3587 int nargs;
3588 tree last_parm;
3589
3590 if (args == NULL_TREE)
3591 return false;
3592
3593 args = INNERMOST_TEMPLATE_ARGS (args);
3594 nargs = TREE_VEC_LENGTH (args);
3595
3596 if (nargs == 0)
3597 return false;
3598
3599 last_parm = TREE_VEC_ELT (args, nargs - 1);
3600
3601 return ARGUMENT_PACK_P (last_parm);
3602 }
3603
3604 /* Generate a new name for the parameter pack name NAME (an
3605 IDENTIFIER_NODE) that incorporates its */
3606
3607 static tree
3608 make_ith_pack_parameter_name (tree name, int i)
3609 {
3610 /* Munge the name to include the parameter index. */
3611 #define NUMBUF_LEN 128
3612 char numbuf[NUMBUF_LEN];
3613 char* newname;
3614 int newname_len;
3615
3616 if (name == NULL_TREE)
3617 return name;
3618 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3619 newname_len = IDENTIFIER_LENGTH (name)
3620 + strlen (numbuf) + 2;
3621 newname = (char*)alloca (newname_len);
3622 snprintf (newname, newname_len,
3623 "%s#%i", IDENTIFIER_POINTER (name), i);
3624 return get_identifier (newname);
3625 }
3626
3627 /* Return true if T is a primary function, class or alias template
3628 specialization, not including the template pattern. */
3629
3630 bool
3631 primary_template_specialization_p (const_tree t)
3632 {
3633 if (!t)
3634 return false;
3635
3636 if (VAR_OR_FUNCTION_DECL_P (t))
3637 return (DECL_LANG_SPECIFIC (t)
3638 && DECL_USE_TEMPLATE (t)
3639 && DECL_TEMPLATE_INFO (t)
3640 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3641 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3642 return (CLASSTYPE_TEMPLATE_INFO (t)
3643 && CLASSTYPE_USE_TEMPLATE (t)
3644 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3645 else if (alias_template_specialization_p (t, nt_transparent))
3646 return true;
3647 return false;
3648 }
3649
3650 /* Return true if PARM is a template template parameter. */
3651
3652 bool
3653 template_template_parameter_p (const_tree parm)
3654 {
3655 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3656 }
3657
3658 /* Return true iff PARM is a DECL representing a type template
3659 parameter. */
3660
3661 bool
3662 template_type_parameter_p (const_tree parm)
3663 {
3664 return (parm
3665 && (TREE_CODE (parm) == TYPE_DECL
3666 || TREE_CODE (parm) == TEMPLATE_DECL)
3667 && DECL_TEMPLATE_PARM_P (parm));
3668 }
3669
3670 /* Return the template parameters of T if T is a
3671 primary template instantiation, NULL otherwise. */
3672
3673 tree
3674 get_primary_template_innermost_parameters (const_tree t)
3675 {
3676 tree parms = NULL, template_info = NULL;
3677
3678 if ((template_info = get_template_info (t))
3679 && primary_template_specialization_p (t))
3680 parms = INNERMOST_TEMPLATE_PARMS
3681 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3682
3683 return parms;
3684 }
3685
3686 /* Return the template parameters of the LEVELth level from the full list
3687 of template parameters PARMS. */
3688
3689 tree
3690 get_template_parms_at_level (tree parms, int level)
3691 {
3692 tree p;
3693 if (!parms
3694 || TREE_CODE (parms) != TREE_LIST
3695 || level > TMPL_PARMS_DEPTH (parms))
3696 return NULL_TREE;
3697
3698 for (p = parms; p; p = TREE_CHAIN (p))
3699 if (TMPL_PARMS_DEPTH (p) == level)
3700 return p;
3701
3702 return NULL_TREE;
3703 }
3704
3705 /* Returns the template arguments of T if T is a template instantiation,
3706 NULL otherwise. */
3707
3708 tree
3709 get_template_innermost_arguments (const_tree t)
3710 {
3711 tree args = NULL, template_info = NULL;
3712
3713 if ((template_info = get_template_info (t))
3714 && TI_ARGS (template_info))
3715 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3716
3717 return args;
3718 }
3719
3720 /* Return the argument pack elements of T if T is a template argument pack,
3721 NULL otherwise. */
3722
3723 tree
3724 get_template_argument_pack_elems (const_tree t)
3725 {
3726 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3727 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3728 return NULL;
3729
3730 return ARGUMENT_PACK_ARGS (t);
3731 }
3732
3733 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3734 ARGUMENT_PACK_SELECT represents. */
3735
3736 static tree
3737 argument_pack_select_arg (tree t)
3738 {
3739 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3740 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3741
3742 /* If the selected argument is an expansion E, that most likely means we were
3743 called from gen_elem_of_pack_expansion_instantiation during the
3744 substituting of an argument pack (of which the Ith element is a pack
3745 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3746 In this case, the Ith element resulting from this substituting is going to
3747 be a pack expansion, which pattern is the pattern of E. Let's return the
3748 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3749 resulting pack expansion from it. */
3750 if (PACK_EXPANSION_P (arg))
3751 {
3752 /* Make sure we aren't throwing away arg info. */
3753 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3754 arg = PACK_EXPANSION_PATTERN (arg);
3755 }
3756
3757 return arg;
3758 }
3759
3760
3761 /* True iff FN is a function representing a built-in variadic parameter
3762 pack. */
3763
3764 bool
3765 builtin_pack_fn_p (tree fn)
3766 {
3767 if (!fn
3768 || TREE_CODE (fn) != FUNCTION_DECL
3769 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3770 return false;
3771
3772 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3773 return true;
3774
3775 return false;
3776 }
3777
3778 /* True iff CALL is a call to a function representing a built-in variadic
3779 parameter pack. */
3780
3781 static bool
3782 builtin_pack_call_p (tree call)
3783 {
3784 if (TREE_CODE (call) != CALL_EXPR)
3785 return false;
3786 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3787 }
3788
3789 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3790
3791 static tree
3792 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3793 tree in_decl)
3794 {
3795 tree ohi = CALL_EXPR_ARG (call, 0);
3796 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3797 false/*fn*/, true/*int_cst*/);
3798
3799 if (value_dependent_expression_p (hi))
3800 {
3801 if (hi != ohi)
3802 {
3803 call = copy_node (call);
3804 CALL_EXPR_ARG (call, 0) = hi;
3805 }
3806 tree ex = make_pack_expansion (call, complain);
3807 tree vec = make_tree_vec (1);
3808 TREE_VEC_ELT (vec, 0) = ex;
3809 return vec;
3810 }
3811 else
3812 {
3813 hi = cxx_constant_value (hi);
3814 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3815
3816 /* Calculate the largest value of len that won't make the size of the vec
3817 overflow an int. The compiler will exceed resource limits long before
3818 this, but it seems a decent place to diagnose. */
3819 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3820
3821 if (len < 0 || len > max)
3822 {
3823 if ((complain & tf_error)
3824 && hi != error_mark_node)
3825 error ("argument to %<__integer_pack%> must be between 0 and %d",
3826 max);
3827 return error_mark_node;
3828 }
3829
3830 tree vec = make_tree_vec (len);
3831
3832 for (int i = 0; i < len; ++i)
3833 TREE_VEC_ELT (vec, i) = size_int (i);
3834
3835 return vec;
3836 }
3837 }
3838
3839 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3840 CALL. */
3841
3842 static tree
3843 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3844 tree in_decl)
3845 {
3846 if (!builtin_pack_call_p (call))
3847 return NULL_TREE;
3848
3849 tree fn = CALL_EXPR_FN (call);
3850
3851 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3852 return expand_integer_pack (call, args, complain, in_decl);
3853
3854 return NULL_TREE;
3855 }
3856
3857 /* Structure used to track the progress of find_parameter_packs_r. */
3858 struct find_parameter_pack_data
3859 {
3860 /* TREE_LIST that will contain all of the parameter packs found by
3861 the traversal. */
3862 tree* parameter_packs;
3863
3864 /* Set of AST nodes that have been visited by the traversal. */
3865 hash_set<tree> *visited;
3866
3867 /* True iff we're making a type pack expansion. */
3868 bool type_pack_expansion_p;
3869 };
3870
3871 /* Identifies all of the argument packs that occur in a template
3872 argument and appends them to the TREE_LIST inside DATA, which is a
3873 find_parameter_pack_data structure. This is a subroutine of
3874 make_pack_expansion and uses_parameter_packs. */
3875 static tree
3876 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3877 {
3878 tree t = *tp;
3879 struct find_parameter_pack_data* ppd =
3880 (struct find_parameter_pack_data*)data;
3881 bool parameter_pack_p = false;
3882
3883 /* Don't look through typedefs; we are interested in whether a
3884 parameter pack is actually written in the expression/type we're
3885 looking at, not the target type. */
3886 if (TYPE_P (t) && typedef_variant_p (t))
3887 {
3888 /* But do look at arguments for an alias template. */
3889 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3890 cp_walk_tree (&TI_ARGS (tinfo),
3891 &find_parameter_packs_r,
3892 ppd, ppd->visited);
3893 *walk_subtrees = 0;
3894 return NULL_TREE;
3895 }
3896
3897 /* Identify whether this is a parameter pack or not. */
3898 switch (TREE_CODE (t))
3899 {
3900 case TEMPLATE_PARM_INDEX:
3901 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3902 parameter_pack_p = true;
3903 break;
3904
3905 case TEMPLATE_TYPE_PARM:
3906 t = TYPE_MAIN_VARIANT (t);
3907 /* FALLTHRU */
3908 case TEMPLATE_TEMPLATE_PARM:
3909 /* If the placeholder appears in the decl-specifier-seq of a function
3910 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3911 is a pack expansion, the invented template parameter is a template
3912 parameter pack. */
3913 if (ppd->type_pack_expansion_p && is_auto (t))
3914 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3915 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3916 parameter_pack_p = true;
3917 break;
3918
3919 case FIELD_DECL:
3920 case PARM_DECL:
3921 if (DECL_PACK_P (t))
3922 {
3923 /* We don't want to walk into the type of a PARM_DECL,
3924 because we don't want to see the type parameter pack. */
3925 *walk_subtrees = 0;
3926 parameter_pack_p = true;
3927 }
3928 break;
3929
3930 case VAR_DECL:
3931 if (DECL_PACK_P (t))
3932 {
3933 /* We don't want to walk into the type of a variadic capture proxy,
3934 because we don't want to see the type parameter pack. */
3935 *walk_subtrees = 0;
3936 parameter_pack_p = true;
3937 }
3938 else if (variable_template_specialization_p (t))
3939 {
3940 cp_walk_tree (&DECL_TI_ARGS (t),
3941 find_parameter_packs_r,
3942 ppd, ppd->visited);
3943 *walk_subtrees = 0;
3944 }
3945 break;
3946
3947 case CALL_EXPR:
3948 if (builtin_pack_call_p (t))
3949 parameter_pack_p = true;
3950 break;
3951
3952 case BASES:
3953 parameter_pack_p = true;
3954 break;
3955 default:
3956 /* Not a parameter pack. */
3957 break;
3958 }
3959
3960 if (parameter_pack_p)
3961 {
3962 /* Add this parameter pack to the list. */
3963 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3964 }
3965
3966 if (TYPE_P (t))
3967 cp_walk_tree (&TYPE_CONTEXT (t),
3968 &find_parameter_packs_r, ppd, ppd->visited);
3969
3970 /* This switch statement will return immediately if we don't find a
3971 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3972 switch (TREE_CODE (t))
3973 {
3974 case BOUND_TEMPLATE_TEMPLATE_PARM:
3975 /* Check the template itself. */
3976 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3977 &find_parameter_packs_r, ppd, ppd->visited);
3978 return NULL_TREE;
3979
3980 case DECL_EXPR:
3981 {
3982 tree decl = DECL_EXPR_DECL (t);
3983 /* Ignore the declaration of a capture proxy for a parameter pack. */
3984 if (is_capture_proxy (decl))
3985 *walk_subtrees = 0;
3986 if (is_typedef_decl (decl))
3987 /* Since we stop at typedefs above, we need to look through them at
3988 the point of the DECL_EXPR. */
3989 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3990 &find_parameter_packs_r, ppd, ppd->visited);
3991 return NULL_TREE;
3992 }
3993
3994 case TEMPLATE_DECL:
3995 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3996 return NULL_TREE;
3997 cp_walk_tree (&TREE_TYPE (t),
3998 &find_parameter_packs_r, ppd, ppd->visited);
3999 return NULL_TREE;
4000
4001 case TYPE_PACK_EXPANSION:
4002 case EXPR_PACK_EXPANSION:
4003 *walk_subtrees = 0;
4004 return NULL_TREE;
4005
4006 case INTEGER_TYPE:
4007 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4008 ppd, ppd->visited);
4009 *walk_subtrees = 0;
4010 return NULL_TREE;
4011
4012 case IDENTIFIER_NODE:
4013 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4014 ppd->visited);
4015 *walk_subtrees = 0;
4016 return NULL_TREE;
4017
4018 case LAMBDA_EXPR:
4019 {
4020 /* Since we defer implicit capture, look in the parms and body. */
4021 tree fn = lambda_function (t);
4022 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4023 ppd->visited);
4024 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4025 ppd->visited);
4026 return NULL_TREE;
4027 }
4028
4029 case DECLTYPE_TYPE:
4030 {
4031 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4032 type_pack_expansion_p to false so that any placeholders
4033 within the expression don't get marked as parameter packs. */
4034 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4035 ppd->type_pack_expansion_p = false;
4036 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4037 ppd, ppd->visited);
4038 ppd->type_pack_expansion_p = type_pack_expansion_p;
4039 *walk_subtrees = 0;
4040 return NULL_TREE;
4041 }
4042
4043 case IF_STMT:
4044 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4045 ppd, ppd->visited);
4046 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4047 ppd, ppd->visited);
4048 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4049 ppd, ppd->visited);
4050 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4051 *walk_subtrees = 0;
4052 return NULL_TREE;
4053
4054 default:
4055 return NULL_TREE;
4056 }
4057
4058 return NULL_TREE;
4059 }
4060
4061 /* Determines if the expression or type T uses any parameter packs. */
4062 tree
4063 uses_parameter_packs (tree t)
4064 {
4065 tree parameter_packs = NULL_TREE;
4066 struct find_parameter_pack_data ppd;
4067 ppd.parameter_packs = &parameter_packs;
4068 ppd.visited = new hash_set<tree>;
4069 ppd.type_pack_expansion_p = false;
4070 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4071 delete ppd.visited;
4072 return parameter_packs;
4073 }
4074
4075 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4076 representation a base-class initializer into a parameter pack
4077 expansion. If all goes well, the resulting node will be an
4078 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4079 respectively. */
4080 tree
4081 make_pack_expansion (tree arg, tsubst_flags_t complain)
4082 {
4083 tree result;
4084 tree parameter_packs = NULL_TREE;
4085 bool for_types = false;
4086 struct find_parameter_pack_data ppd;
4087
4088 if (!arg || arg == error_mark_node)
4089 return arg;
4090
4091 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4092 {
4093 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4094 class initializer. In this case, the TREE_PURPOSE will be a
4095 _TYPE node (representing the base class expansion we're
4096 initializing) and the TREE_VALUE will be a TREE_LIST
4097 containing the initialization arguments.
4098
4099 The resulting expansion looks somewhat different from most
4100 expansions. Rather than returning just one _EXPANSION, we
4101 return a TREE_LIST whose TREE_PURPOSE is a
4102 TYPE_PACK_EXPANSION containing the bases that will be
4103 initialized. The TREE_VALUE will be identical to the
4104 original TREE_VALUE, which is a list of arguments that will
4105 be passed to each base. We do not introduce any new pack
4106 expansion nodes into the TREE_VALUE (although it is possible
4107 that some already exist), because the TREE_PURPOSE and
4108 TREE_VALUE all need to be expanded together with the same
4109 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4110 resulting TREE_PURPOSE will mention the parameter packs in
4111 both the bases and the arguments to the bases. */
4112 tree purpose;
4113 tree value;
4114 tree parameter_packs = NULL_TREE;
4115
4116 /* Determine which parameter packs will be used by the base
4117 class expansion. */
4118 ppd.visited = new hash_set<tree>;
4119 ppd.parameter_packs = &parameter_packs;
4120 ppd.type_pack_expansion_p = false;
4121 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4122 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4123 &ppd, ppd.visited);
4124
4125 if (parameter_packs == NULL_TREE)
4126 {
4127 if (complain & tf_error)
4128 error ("base initializer expansion %qT contains no parameter packs",
4129 arg);
4130 delete ppd.visited;
4131 return error_mark_node;
4132 }
4133
4134 if (TREE_VALUE (arg) != void_type_node)
4135 {
4136 /* Collect the sets of parameter packs used in each of the
4137 initialization arguments. */
4138 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4139 {
4140 /* Determine which parameter packs will be expanded in this
4141 argument. */
4142 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4143 &ppd, ppd.visited);
4144 }
4145 }
4146
4147 delete ppd.visited;
4148
4149 /* Create the pack expansion type for the base type. */
4150 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4151 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4152 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4153 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4154
4155 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4156 they will rarely be compared to anything. */
4157 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4158
4159 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4160 }
4161
4162 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4163 for_types = true;
4164
4165 /* Build the PACK_EXPANSION_* node. */
4166 result = for_types
4167 ? cxx_make_type (TYPE_PACK_EXPANSION)
4168 : make_node (EXPR_PACK_EXPANSION);
4169 SET_PACK_EXPANSION_PATTERN (result, arg);
4170 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4171 {
4172 /* Propagate type and const-expression information. */
4173 TREE_TYPE (result) = TREE_TYPE (arg);
4174 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4175 /* Mark this read now, since the expansion might be length 0. */
4176 mark_exp_read (arg);
4177 }
4178 else
4179 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4180 they will rarely be compared to anything. */
4181 SET_TYPE_STRUCTURAL_EQUALITY (result);
4182
4183 /* Determine which parameter packs will be expanded. */
4184 ppd.parameter_packs = &parameter_packs;
4185 ppd.visited = new hash_set<tree>;
4186 ppd.type_pack_expansion_p = TYPE_P (arg);
4187 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4188 delete ppd.visited;
4189
4190 /* Make sure we found some parameter packs. */
4191 if (parameter_packs == NULL_TREE)
4192 {
4193 if (complain & tf_error)
4194 {
4195 if (TYPE_P (arg))
4196 error ("expansion pattern %qT contains no parameter packs", arg);
4197 else
4198 error ("expansion pattern %qE contains no parameter packs", arg);
4199 }
4200 return error_mark_node;
4201 }
4202 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4203
4204 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4205
4206 return result;
4207 }
4208
4209 /* Checks T for any "bare" parameter packs, which have not yet been
4210 expanded, and issues an error if any are found. This operation can
4211 only be done on full expressions or types (e.g., an expression
4212 statement, "if" condition, etc.), because we could have expressions like:
4213
4214 foo(f(g(h(args)))...)
4215
4216 where "args" is a parameter pack. check_for_bare_parameter_packs
4217 should not be called for the subexpressions args, h(args),
4218 g(h(args)), or f(g(h(args))), because we would produce erroneous
4219 error messages.
4220
4221 Returns TRUE and emits an error if there were bare parameter packs,
4222 returns FALSE otherwise. */
4223 bool
4224 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4225 {
4226 tree parameter_packs = NULL_TREE;
4227 struct find_parameter_pack_data ppd;
4228
4229 if (!processing_template_decl || !t || t == error_mark_node)
4230 return false;
4231
4232 if (TREE_CODE (t) == TYPE_DECL)
4233 t = TREE_TYPE (t);
4234
4235 ppd.parameter_packs = &parameter_packs;
4236 ppd.visited = new hash_set<tree>;
4237 ppd.type_pack_expansion_p = false;
4238 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4239 delete ppd.visited;
4240
4241 /* It's OK for a lambda to have an unexpanded parameter pack from the
4242 containing context, but do complain about unexpanded capture packs. */
4243 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4244 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4245 for (; parameter_packs;
4246 parameter_packs = TREE_CHAIN (parameter_packs))
4247 {
4248 tree pack = TREE_VALUE (parameter_packs);
4249 if (is_capture_proxy (pack))
4250 break;
4251 }
4252
4253 if (parameter_packs)
4254 {
4255 if (loc == UNKNOWN_LOCATION)
4256 loc = cp_expr_loc_or_input_loc (t);
4257 error_at (loc, "parameter packs not expanded with %<...%>:");
4258 while (parameter_packs)
4259 {
4260 tree pack = TREE_VALUE (parameter_packs);
4261 tree name = NULL_TREE;
4262
4263 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4264 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4265 name = TYPE_NAME (pack);
4266 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4267 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4268 else if (TREE_CODE (pack) == CALL_EXPR)
4269 name = DECL_NAME (CALL_EXPR_FN (pack));
4270 else
4271 name = DECL_NAME (pack);
4272
4273 if (name)
4274 inform (loc, " %qD", name);
4275 else
4276 inform (loc, " %s", "<anonymous>");
4277
4278 parameter_packs = TREE_CHAIN (parameter_packs);
4279 }
4280
4281 return true;
4282 }
4283
4284 return false;
4285 }
4286
4287 /* Expand any parameter packs that occur in the template arguments in
4288 ARGS. */
4289 tree
4290 expand_template_argument_pack (tree args)
4291 {
4292 if (args == error_mark_node)
4293 return error_mark_node;
4294
4295 tree result_args = NULL_TREE;
4296 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4297 int num_result_args = -1;
4298 int non_default_args_count = -1;
4299
4300 /* First, determine if we need to expand anything, and the number of
4301 slots we'll need. */
4302 for (in_arg = 0; in_arg < nargs; ++in_arg)
4303 {
4304 tree arg = TREE_VEC_ELT (args, in_arg);
4305 if (arg == NULL_TREE)
4306 return args;
4307 if (ARGUMENT_PACK_P (arg))
4308 {
4309 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4310 if (num_result_args < 0)
4311 num_result_args = in_arg + num_packed;
4312 else
4313 num_result_args += num_packed;
4314 }
4315 else
4316 {
4317 if (num_result_args >= 0)
4318 num_result_args++;
4319 }
4320 }
4321
4322 /* If no expansion is necessary, we're done. */
4323 if (num_result_args < 0)
4324 return args;
4325
4326 /* Expand arguments. */
4327 result_args = make_tree_vec (num_result_args);
4328 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4329 non_default_args_count =
4330 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4331 for (in_arg = 0; in_arg < nargs; ++in_arg)
4332 {
4333 tree arg = TREE_VEC_ELT (args, in_arg);
4334 if (ARGUMENT_PACK_P (arg))
4335 {
4336 tree packed = ARGUMENT_PACK_ARGS (arg);
4337 int i, num_packed = TREE_VEC_LENGTH (packed);
4338 for (i = 0; i < num_packed; ++i, ++out_arg)
4339 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4340 if (non_default_args_count > 0)
4341 non_default_args_count += num_packed - 1;
4342 }
4343 else
4344 {
4345 TREE_VEC_ELT (result_args, out_arg) = arg;
4346 ++out_arg;
4347 }
4348 }
4349 if (non_default_args_count >= 0)
4350 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4351 return result_args;
4352 }
4353
4354 /* Checks if DECL shadows a template parameter.
4355
4356 [temp.local]: A template-parameter shall not be redeclared within its
4357 scope (including nested scopes).
4358
4359 Emits an error and returns TRUE if the DECL shadows a parameter,
4360 returns FALSE otherwise. */
4361
4362 bool
4363 check_template_shadow (tree decl)
4364 {
4365 tree olddecl;
4366
4367 /* If we're not in a template, we can't possibly shadow a template
4368 parameter. */
4369 if (!current_template_parms)
4370 return true;
4371
4372 /* Figure out what we're shadowing. */
4373 decl = OVL_FIRST (decl);
4374 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4375
4376 /* If there's no previous binding for this name, we're not shadowing
4377 anything, let alone a template parameter. */
4378 if (!olddecl)
4379 return true;
4380
4381 /* If we're not shadowing a template parameter, we're done. Note
4382 that OLDDECL might be an OVERLOAD (or perhaps even an
4383 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4384 node. */
4385 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4386 return true;
4387
4388 /* We check for decl != olddecl to avoid bogus errors for using a
4389 name inside a class. We check TPFI to avoid duplicate errors for
4390 inline member templates. */
4391 if (decl == olddecl
4392 || (DECL_TEMPLATE_PARM_P (decl)
4393 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4394 return true;
4395
4396 /* Don't complain about the injected class name, as we've already
4397 complained about the class itself. */
4398 if (DECL_SELF_REFERENCE_P (decl))
4399 return false;
4400
4401 if (DECL_TEMPLATE_PARM_P (decl))
4402 error ("declaration of template parameter %q+D shadows "
4403 "template parameter", decl);
4404 else
4405 error ("declaration of %q+#D shadows template parameter", decl);
4406 inform (DECL_SOURCE_LOCATION (olddecl),
4407 "template parameter %qD declared here", olddecl);
4408 return false;
4409 }
4410
4411 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4412 ORIG_LEVEL, DECL, and TYPE. */
4413
4414 static tree
4415 build_template_parm_index (int index,
4416 int level,
4417 int orig_level,
4418 tree decl,
4419 tree type)
4420 {
4421 tree t = make_node (TEMPLATE_PARM_INDEX);
4422 TEMPLATE_PARM_IDX (t) = index;
4423 TEMPLATE_PARM_LEVEL (t) = level;
4424 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4425 TEMPLATE_PARM_DECL (t) = decl;
4426 TREE_TYPE (t) = type;
4427 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4428 TREE_READONLY (t) = TREE_READONLY (decl);
4429
4430 return t;
4431 }
4432
4433 /* Find the canonical type parameter for the given template type
4434 parameter. Returns the canonical type parameter, which may be TYPE
4435 if no such parameter existed. */
4436
4437 tree
4438 canonical_type_parameter (tree type)
4439 {
4440 int idx = TEMPLATE_TYPE_IDX (type);
4441
4442 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4443
4444 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4445 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4446
4447 for (tree list = (*canonical_template_parms)[idx];
4448 list; list = TREE_CHAIN (list))
4449 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4450 return TREE_VALUE (list);
4451
4452 (*canonical_template_parms)[idx]
4453 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4454 return type;
4455 }
4456
4457 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4458 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4459 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4460 new one is created. */
4461
4462 static tree
4463 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4464 tsubst_flags_t complain)
4465 {
4466 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4467 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4468 != TEMPLATE_PARM_LEVEL (index) - levels)
4469 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4470 {
4471 tree orig_decl = TEMPLATE_PARM_DECL (index);
4472
4473 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4474 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4475 type);
4476 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4477 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4478 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4479 DECL_ARTIFICIAL (decl) = 1;
4480 SET_DECL_TEMPLATE_PARM_P (decl);
4481
4482 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4483 TEMPLATE_PARM_LEVEL (index) - levels,
4484 TEMPLATE_PARM_ORIG_LEVEL (index),
4485 decl, type);
4486 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4487 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4488 = TEMPLATE_PARM_PARAMETER_PACK (index);
4489
4490 /* Template template parameters need this. */
4491 tree inner = decl;
4492 if (TREE_CODE (decl) == TEMPLATE_DECL)
4493 {
4494 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4495 TYPE_DECL, DECL_NAME (decl), type);
4496 DECL_TEMPLATE_RESULT (decl) = inner;
4497 DECL_ARTIFICIAL (inner) = true;
4498 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4499 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4500 }
4501
4502 /* Attach the TPI to the decl. */
4503 if (TREE_CODE (inner) == TYPE_DECL)
4504 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4505 else
4506 DECL_INITIAL (decl) = tpi;
4507 }
4508
4509 return TEMPLATE_PARM_DESCENDANTS (index);
4510 }
4511
4512 /* Process information from new template parameter PARM and append it
4513 to the LIST being built. This new parameter is a non-type
4514 parameter iff IS_NON_TYPE is true. This new parameter is a
4515 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4516 is in PARM_LOC. */
4517
4518 tree
4519 process_template_parm (tree list, location_t parm_loc, tree parm,
4520 bool is_non_type, bool is_parameter_pack)
4521 {
4522 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4523 tree prev = NULL_TREE;
4524 int idx = 0;
4525
4526 if (list)
4527 {
4528 prev = tree_last (list);
4529
4530 tree p = TREE_VALUE (prev);
4531 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4532 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4533 else if (TREE_CODE (p) == PARM_DECL)
4534 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4535
4536 ++idx;
4537 }
4538
4539 tree decl = NULL_TREE;
4540 tree defval = TREE_PURPOSE (parm);
4541 tree constr = TREE_TYPE (parm);
4542
4543 if (is_non_type)
4544 {
4545 parm = TREE_VALUE (parm);
4546
4547 SET_DECL_TEMPLATE_PARM_P (parm);
4548
4549 if (TREE_TYPE (parm) != error_mark_node)
4550 {
4551 /* [temp.param]
4552
4553 The top-level cv-qualifiers on the template-parameter are
4554 ignored when determining its type. */
4555 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4556 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4557 TREE_TYPE (parm) = error_mark_node;
4558 else if (uses_parameter_packs (TREE_TYPE (parm))
4559 && !is_parameter_pack
4560 /* If we're in a nested template parameter list, the template
4561 template parameter could be a parameter pack. */
4562 && processing_template_parmlist == 1)
4563 {
4564 /* This template parameter is not a parameter pack, but it
4565 should be. Complain about "bare" parameter packs. */
4566 check_for_bare_parameter_packs (TREE_TYPE (parm));
4567
4568 /* Recover by calling this a parameter pack. */
4569 is_parameter_pack = true;
4570 }
4571 }
4572
4573 /* A template parameter is not modifiable. */
4574 TREE_CONSTANT (parm) = 1;
4575 TREE_READONLY (parm) = 1;
4576 decl = build_decl (parm_loc,
4577 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4578 TREE_CONSTANT (decl) = 1;
4579 TREE_READONLY (decl) = 1;
4580 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4581 = build_template_parm_index (idx, processing_template_decl,
4582 processing_template_decl,
4583 decl, TREE_TYPE (parm));
4584
4585 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4586 = is_parameter_pack;
4587 }
4588 else
4589 {
4590 tree t;
4591 parm = TREE_VALUE (TREE_VALUE (parm));
4592
4593 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4594 {
4595 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4596 /* This is for distinguishing between real templates and template
4597 template parameters */
4598 TREE_TYPE (parm) = t;
4599
4600 /* any_template_parm_r expects to be able to get the targs of a
4601 DECL_TEMPLATE_RESULT. */
4602 tree result = DECL_TEMPLATE_RESULT (parm);
4603 TREE_TYPE (result) = t;
4604 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4605 tree tinfo = build_template_info (parm, args);
4606 retrofit_lang_decl (result);
4607 DECL_TEMPLATE_INFO (result) = tinfo;
4608
4609 decl = parm;
4610 }
4611 else
4612 {
4613 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4614 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4615 decl = build_decl (parm_loc,
4616 TYPE_DECL, parm, t);
4617 }
4618
4619 TYPE_NAME (t) = decl;
4620 TYPE_STUB_DECL (t) = decl;
4621 parm = decl;
4622 TEMPLATE_TYPE_PARM_INDEX (t)
4623 = build_template_parm_index (idx, processing_template_decl,
4624 processing_template_decl,
4625 decl, TREE_TYPE (parm));
4626 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4627 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4628 SET_TYPE_STRUCTURAL_EQUALITY (t);
4629 else
4630 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4631 }
4632 DECL_ARTIFICIAL (decl) = 1;
4633 SET_DECL_TEMPLATE_PARM_P (decl);
4634
4635 /* Build requirements for the type/template parameter.
4636 This must be done after SET_DECL_TEMPLATE_PARM_P or
4637 process_template_parm could fail. */
4638 tree reqs = finish_shorthand_constraint (parm, constr);
4639
4640 decl = pushdecl (decl);
4641 if (!is_non_type)
4642 parm = decl;
4643
4644 /* Build the parameter node linking the parameter declaration,
4645 its default argument (if any), and its constraints (if any). */
4646 parm = build_tree_list (defval, parm);
4647 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4648
4649 if (prev)
4650 TREE_CHAIN (prev) = parm;
4651 else
4652 list = parm;
4653
4654 return list;
4655 }
4656
4657 /* The end of a template parameter list has been reached. Process the
4658 tree list into a parameter vector, converting each parameter into a more
4659 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4660 as PARM_DECLs. */
4661
4662 tree
4663 end_template_parm_list (tree parms)
4664 {
4665 tree saved_parmlist = make_tree_vec (list_length (parms));
4666
4667 /* Pop the dummy parameter level and add the real one. We do not
4668 morph the dummy parameter in place, as it might have been
4669 captured by a (nested) template-template-parm. */
4670 current_template_parms = TREE_CHAIN (current_template_parms);
4671
4672 current_template_parms
4673 = tree_cons (size_int (processing_template_decl),
4674 saved_parmlist, current_template_parms);
4675
4676 for (unsigned ix = 0; parms; ix++)
4677 {
4678 tree parm = parms;
4679 parms = TREE_CHAIN (parms);
4680 TREE_CHAIN (parm) = NULL_TREE;
4681
4682 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4683 }
4684
4685 --processing_template_parmlist;
4686
4687 return saved_parmlist;
4688 }
4689
4690 // Explicitly indicate the end of the template parameter list. We assume
4691 // that the current template parameters have been constructed and/or
4692 // managed explicitly, as when creating new template template parameters
4693 // from a shorthand constraint.
4694 void
4695 end_template_parm_list ()
4696 {
4697 --processing_template_parmlist;
4698 }
4699
4700 /* end_template_decl is called after a template declaration is seen. */
4701
4702 void
4703 end_template_decl (void)
4704 {
4705 reset_specialization ();
4706
4707 if (! processing_template_decl)
4708 return;
4709
4710 /* This matches the pushlevel in begin_template_parm_list. */
4711 finish_scope ();
4712
4713 --processing_template_decl;
4714 current_template_parms = TREE_CHAIN (current_template_parms);
4715 }
4716
4717 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4718 thereof, and converts it into an argument suitable to be passed to
4719 the type substitution functions. Note that if the TREE_LIST contains
4720 an error_mark node, the returned argument is error_mark_node. */
4721
4722 tree
4723 template_parm_to_arg (tree t)
4724 {
4725 if (!t)
4726 return NULL_TREE;
4727
4728 if (TREE_CODE (t) == TREE_LIST)
4729 t = TREE_VALUE (t);
4730
4731 if (error_operand_p (t))
4732 return error_mark_node;
4733
4734 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4735 {
4736 if (TREE_CODE (t) == TYPE_DECL
4737 || TREE_CODE (t) == TEMPLATE_DECL)
4738 t = TREE_TYPE (t);
4739 else
4740 t = DECL_INITIAL (t);
4741 }
4742
4743 gcc_assert (TEMPLATE_PARM_P (t));
4744
4745 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4746 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4747 {
4748 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4749 {
4750 /* Turn this argument into a TYPE_ARGUMENT_PACK
4751 with a single element, which expands T. */
4752 tree vec = make_tree_vec (1);
4753 if (CHECKING_P)
4754 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4755
4756 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4757
4758 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4759 SET_ARGUMENT_PACK_ARGS (t, vec);
4760 }
4761 }
4762 else
4763 {
4764 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4765 {
4766 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4767 with a single element, which expands T. */
4768 tree vec = make_tree_vec (1);
4769 if (CHECKING_P)
4770 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4771
4772 t = convert_from_reference (t);
4773 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4774
4775 t = make_node (NONTYPE_ARGUMENT_PACK);
4776 SET_ARGUMENT_PACK_ARGS (t, vec);
4777 }
4778 else
4779 t = convert_from_reference (t);
4780 }
4781 return t;
4782 }
4783
4784 /* Given a single level of template parameters (a TREE_VEC), return it
4785 as a set of template arguments. */
4786
4787 tree
4788 template_parms_level_to_args (tree parms)
4789 {
4790 tree a = copy_node (parms);
4791 TREE_TYPE (a) = NULL_TREE;
4792 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4793 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4794
4795 if (CHECKING_P)
4796 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4797
4798 return a;
4799 }
4800
4801 /* Given a set of template parameters, return them as a set of template
4802 arguments. The template parameters are represented as a TREE_VEC, in
4803 the form documented in cp-tree.h for template arguments. */
4804
4805 tree
4806 template_parms_to_args (tree parms)
4807 {
4808 tree header;
4809 tree args = NULL_TREE;
4810 int length = TMPL_PARMS_DEPTH (parms);
4811 int l = length;
4812
4813 /* If there is only one level of template parameters, we do not
4814 create a TREE_VEC of TREE_VECs. Instead, we return a single
4815 TREE_VEC containing the arguments. */
4816 if (length > 1)
4817 args = make_tree_vec (length);
4818
4819 for (header = parms; header; header = TREE_CHAIN (header))
4820 {
4821 tree a = template_parms_level_to_args (TREE_VALUE (header));
4822
4823 if (length > 1)
4824 TREE_VEC_ELT (args, --l) = a;
4825 else
4826 args = a;
4827 }
4828
4829 return args;
4830 }
4831
4832 /* Within the declaration of a template, return the currently active
4833 template parameters as an argument TREE_VEC. */
4834
4835 static tree
4836 current_template_args (void)
4837 {
4838 return template_parms_to_args (current_template_parms);
4839 }
4840
4841 /* Return the fully generic arguments for of TMPL, i.e. what
4842 current_template_args would be while parsing it. */
4843
4844 tree
4845 generic_targs_for (tree tmpl)
4846 {
4847 if (tmpl == NULL_TREE)
4848 return NULL_TREE;
4849 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4850 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4851 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4852 template parameter, it has no TEMPLATE_INFO; for a partial
4853 specialization, it has the arguments for the primary template, and we
4854 want the arguments for the partial specialization. */;
4855 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4856 if (tree ti = get_template_info (result))
4857 return TI_ARGS (ti);
4858 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4859 }
4860
4861 /* Update the declared TYPE by doing any lookups which were thought to be
4862 dependent, but are not now that we know the SCOPE of the declarator. */
4863
4864 tree
4865 maybe_update_decl_type (tree orig_type, tree scope)
4866 {
4867 tree type = orig_type;
4868
4869 if (type == NULL_TREE)
4870 return type;
4871
4872 if (TREE_CODE (orig_type) == TYPE_DECL)
4873 type = TREE_TYPE (type);
4874
4875 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4876 && dependent_type_p (type)
4877 /* Don't bother building up the args in this case. */
4878 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4879 {
4880 /* tsubst in the args corresponding to the template parameters,
4881 including auto if present. Most things will be unchanged, but
4882 make_typename_type and tsubst_qualified_id will resolve
4883 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4884 tree args = current_template_args ();
4885 tree auto_node = type_uses_auto (type);
4886 tree pushed;
4887 if (auto_node)
4888 {
4889 tree auto_vec = make_tree_vec (1);
4890 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4891 args = add_to_template_args (args, auto_vec);
4892 }
4893 pushed = push_scope (scope);
4894 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4895 if (pushed)
4896 pop_scope (scope);
4897 }
4898
4899 if (type == error_mark_node)
4900 return orig_type;
4901
4902 if (TREE_CODE (orig_type) == TYPE_DECL)
4903 {
4904 if (same_type_p (type, TREE_TYPE (orig_type)))
4905 type = orig_type;
4906 else
4907 type = TYPE_NAME (type);
4908 }
4909 return type;
4910 }
4911
4912 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4913 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4914 the new template is a member template. */
4915
4916 static tree
4917 build_template_decl (tree decl, tree parms, bool member_template_p)
4918 {
4919 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4920 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4921 DECL_TEMPLATE_PARMS (tmpl) = parms;
4922 DECL_TEMPLATE_RESULT (tmpl) = decl;
4923 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4924 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4925 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4926 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4927
4928 if (modules_p ())
4929 {
4930 /* Propagate module information from the decl. */
4931 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
4932 if (DECL_LANG_SPECIFIC (decl))
4933 {
4934 DECL_MODULE_PURVIEW_P (tmpl) = DECL_MODULE_PURVIEW_P (decl);
4935 gcc_checking_assert (!DECL_MODULE_IMPORT_P (decl));
4936 }
4937 }
4938
4939 return tmpl;
4940 }
4941
4942 struct template_parm_data
4943 {
4944 /* The level of the template parameters we are currently
4945 processing. */
4946 int level;
4947
4948 /* The index of the specialization argument we are currently
4949 processing. */
4950 int current_arg;
4951
4952 /* An array whose size is the number of template parameters. The
4953 elements are nonzero if the parameter has been used in any one
4954 of the arguments processed so far. */
4955 int* parms;
4956
4957 /* An array whose size is the number of template arguments. The
4958 elements are nonzero if the argument makes use of template
4959 parameters of this level. */
4960 int* arg_uses_template_parms;
4961 };
4962
4963 /* Subroutine of push_template_decl used to see if each template
4964 parameter in a partial specialization is used in the explicit
4965 argument list. If T is of the LEVEL given in DATA (which is
4966 treated as a template_parm_data*), then DATA->PARMS is marked
4967 appropriately. */
4968
4969 static int
4970 mark_template_parm (tree t, void* data)
4971 {
4972 int level;
4973 int idx;
4974 struct template_parm_data* tpd = (struct template_parm_data*) data;
4975
4976 template_parm_level_and_index (t, &level, &idx);
4977
4978 if (level == tpd->level)
4979 {
4980 tpd->parms[idx] = 1;
4981 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4982 }
4983
4984 /* In C++17 the type of a non-type argument is a deduced context. */
4985 if (cxx_dialect >= cxx17
4986 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4987 for_each_template_parm (TREE_TYPE (t),
4988 &mark_template_parm,
4989 data,
4990 NULL,
4991 /*include_nondeduced_p=*/false);
4992
4993 /* Return zero so that for_each_template_parm will continue the
4994 traversal of the tree; we want to mark *every* template parm. */
4995 return 0;
4996 }
4997
4998 /* Process the partial specialization DECL. */
4999
5000 static tree
5001 process_partial_specialization (tree decl)
5002 {
5003 tree type = TREE_TYPE (decl);
5004 tree tinfo = get_template_info (decl);
5005 tree maintmpl = TI_TEMPLATE (tinfo);
5006 tree specargs = TI_ARGS (tinfo);
5007 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5008 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5009 tree inner_parms;
5010 tree inst;
5011 int nargs = TREE_VEC_LENGTH (inner_args);
5012 int ntparms;
5013 int i;
5014 bool did_error_intro = false;
5015 struct template_parm_data tpd;
5016 struct template_parm_data tpd2;
5017
5018 gcc_assert (current_template_parms);
5019
5020 /* A concept cannot be specialized. */
5021 if (flag_concepts && variable_concept_p (maintmpl))
5022 {
5023 error ("specialization of variable concept %q#D", maintmpl);
5024 return error_mark_node;
5025 }
5026
5027 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5028 ntparms = TREE_VEC_LENGTH (inner_parms);
5029
5030 /* We check that each of the template parameters given in the
5031 partial specialization is used in the argument list to the
5032 specialization. For example:
5033
5034 template <class T> struct S;
5035 template <class T> struct S<T*>;
5036
5037 The second declaration is OK because `T*' uses the template
5038 parameter T, whereas
5039
5040 template <class T> struct S<int>;
5041
5042 is no good. Even trickier is:
5043
5044 template <class T>
5045 struct S1
5046 {
5047 template <class U>
5048 struct S2;
5049 template <class U>
5050 struct S2<T>;
5051 };
5052
5053 The S2<T> declaration is actually invalid; it is a
5054 full-specialization. Of course,
5055
5056 template <class U>
5057 struct S2<T (*)(U)>;
5058
5059 or some such would have been OK. */
5060 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5061 tpd.parms = XALLOCAVEC (int, ntparms);
5062 memset (tpd.parms, 0, sizeof (int) * ntparms);
5063
5064 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5065 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5066 for (i = 0; i < nargs; ++i)
5067 {
5068 tpd.current_arg = i;
5069 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5070 &mark_template_parm,
5071 &tpd,
5072 NULL,
5073 /*include_nondeduced_p=*/false);
5074 }
5075 for (i = 0; i < ntparms; ++i)
5076 if (tpd.parms[i] == 0)
5077 {
5078 /* One of the template parms was not used in a deduced context in the
5079 specialization. */
5080 if (!did_error_intro)
5081 {
5082 error ("template parameters not deducible in "
5083 "partial specialization:");
5084 did_error_intro = true;
5085 }
5086
5087 inform (input_location, " %qD",
5088 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5089 }
5090
5091 if (did_error_intro)
5092 return error_mark_node;
5093
5094 /* [temp.class.spec]
5095
5096 The argument list of the specialization shall not be identical to
5097 the implicit argument list of the primary template. */
5098 tree main_args
5099 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5100 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5101 && (!flag_concepts
5102 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5103 {
5104 if (!flag_concepts)
5105 error ("partial specialization %q+D does not specialize "
5106 "any template arguments; to define the primary template, "
5107 "remove the template argument list", decl);
5108 else
5109 error ("partial specialization %q+D does not specialize any "
5110 "template arguments and is not more constrained than "
5111 "the primary template; to define the primary template, "
5112 "remove the template argument list", decl);
5113 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5114 }
5115
5116 /* A partial specialization that replaces multiple parameters of the
5117 primary template with a pack expansion is less specialized for those
5118 parameters. */
5119 if (nargs < DECL_NTPARMS (maintmpl))
5120 {
5121 error ("partial specialization is not more specialized than the "
5122 "primary template because it replaces multiple parameters "
5123 "with a pack expansion");
5124 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5125 /* Avoid crash in process_partial_specialization. */
5126 return decl;
5127 }
5128
5129 else if (nargs > DECL_NTPARMS (maintmpl))
5130 {
5131 error ("too many arguments for partial specialization %qT", type);
5132 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5133 /* Avoid crash below. */
5134 return decl;
5135 }
5136
5137 /* If we aren't in a dependent class, we can actually try deduction. */
5138 else if (tpd.level == 1
5139 /* FIXME we should be able to handle a partial specialization of a
5140 partial instantiation, but currently we can't (c++/41727). */
5141 && TMPL_ARGS_DEPTH (specargs) == 1
5142 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5143 {
5144 auto_diagnostic_group d;
5145 if (permerror (input_location, "partial specialization %qD is not "
5146 "more specialized than", decl))
5147 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5148 maintmpl);
5149 }
5150
5151 /* [temp.class.spec]
5152
5153 A partially specialized non-type argument expression shall not
5154 involve template parameters of the partial specialization except
5155 when the argument expression is a simple identifier.
5156
5157 The type of a template parameter corresponding to a specialized
5158 non-type argument shall not be dependent on a parameter of the
5159 specialization.
5160
5161 Also, we verify that pack expansions only occur at the
5162 end of the argument list. */
5163 tpd2.parms = 0;
5164 for (i = 0; i < nargs; ++i)
5165 {
5166 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5167 tree arg = TREE_VEC_ELT (inner_args, i);
5168 tree packed_args = NULL_TREE;
5169 int j, len = 1;
5170
5171 if (ARGUMENT_PACK_P (arg))
5172 {
5173 /* Extract the arguments from the argument pack. We'll be
5174 iterating over these in the following loop. */
5175 packed_args = ARGUMENT_PACK_ARGS (arg);
5176 len = TREE_VEC_LENGTH (packed_args);
5177 }
5178
5179 for (j = 0; j < len; j++)
5180 {
5181 if (packed_args)
5182 /* Get the Jth argument in the parameter pack. */
5183 arg = TREE_VEC_ELT (packed_args, j);
5184
5185 if (PACK_EXPANSION_P (arg))
5186 {
5187 /* Pack expansions must come at the end of the
5188 argument list. */
5189 if ((packed_args && j < len - 1)
5190 || (!packed_args && i < nargs - 1))
5191 {
5192 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5193 error ("parameter pack argument %qE must be at the "
5194 "end of the template argument list", arg);
5195 else
5196 error ("parameter pack argument %qT must be at the "
5197 "end of the template argument list", arg);
5198 }
5199 }
5200
5201 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5202 /* We only care about the pattern. */
5203 arg = PACK_EXPANSION_PATTERN (arg);
5204
5205 if (/* These first two lines are the `non-type' bit. */
5206 !TYPE_P (arg)
5207 && TREE_CODE (arg) != TEMPLATE_DECL
5208 /* This next two lines are the `argument expression is not just a
5209 simple identifier' condition and also the `specialized
5210 non-type argument' bit. */
5211 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5212 && !((REFERENCE_REF_P (arg)
5213 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5214 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5215 {
5216 if ((!packed_args && tpd.arg_uses_template_parms[i])
5217 || (packed_args && uses_template_parms (arg)))
5218 error_at (cp_expr_loc_or_input_loc (arg),
5219 "template argument %qE involves template "
5220 "parameter(s)", arg);
5221 else
5222 {
5223 /* Look at the corresponding template parameter,
5224 marking which template parameters its type depends
5225 upon. */
5226 tree type = TREE_TYPE (parm);
5227
5228 if (!tpd2.parms)
5229 {
5230 /* We haven't yet initialized TPD2. Do so now. */
5231 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5232 /* The number of parameters here is the number in the
5233 main template, which, as checked in the assertion
5234 above, is NARGS. */
5235 tpd2.parms = XALLOCAVEC (int, nargs);
5236 tpd2.level =
5237 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5238 }
5239
5240 /* Mark the template parameters. But this time, we're
5241 looking for the template parameters of the main
5242 template, not in the specialization. */
5243 tpd2.current_arg = i;
5244 tpd2.arg_uses_template_parms[i] = 0;
5245 memset (tpd2.parms, 0, sizeof (int) * nargs);
5246 for_each_template_parm (type,
5247 &mark_template_parm,
5248 &tpd2,
5249 NULL,
5250 /*include_nondeduced_p=*/false);
5251
5252 if (tpd2.arg_uses_template_parms [i])
5253 {
5254 /* The type depended on some template parameters.
5255 If they are fully specialized in the
5256 specialization, that's OK. */
5257 int j;
5258 int count = 0;
5259 for (j = 0; j < nargs; ++j)
5260 if (tpd2.parms[j] != 0
5261 && tpd.arg_uses_template_parms [j])
5262 ++count;
5263 if (count != 0)
5264 error_n (input_location, count,
5265 "type %qT of template argument %qE depends "
5266 "on a template parameter",
5267 "type %qT of template argument %qE depends "
5268 "on template parameters",
5269 type,
5270 arg);
5271 }
5272 }
5273 }
5274 }
5275 }
5276
5277 /* We should only get here once. */
5278 if (TREE_CODE (decl) == TYPE_DECL)
5279 gcc_assert (!COMPLETE_TYPE_P (type));
5280
5281 // Build the template decl.
5282 tree tmpl = build_template_decl (decl, current_template_parms,
5283 DECL_MEMBER_TEMPLATE_P (maintmpl));
5284 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5285 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5286 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5287
5288 /* Give template template parms a DECL_CONTEXT of the template
5289 for which they are a parameter. */
5290 for (i = 0; i < ntparms; ++i)
5291 {
5292 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5293 if (TREE_CODE (parm) == TEMPLATE_DECL)
5294 DECL_CONTEXT (parm) = tmpl;
5295 }
5296
5297 if (VAR_P (decl))
5298 /* We didn't register this in check_explicit_specialization so we could
5299 wait until the constraints were set. */
5300 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5301 else
5302 associate_classtype_constraints (type);
5303
5304 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5305 = tree_cons (specargs, tmpl,
5306 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5307 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5308
5309 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5310 inst = TREE_CHAIN (inst))
5311 {
5312 tree instance = TREE_VALUE (inst);
5313 if (TYPE_P (instance)
5314 ? (COMPLETE_TYPE_P (instance)
5315 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5316 : DECL_TEMPLATE_INSTANTIATION (instance))
5317 {
5318 tree spec = most_specialized_partial_spec (instance, tf_none);
5319 tree inst_decl = (DECL_P (instance)
5320 ? instance : TYPE_NAME (instance));
5321 if (!spec)
5322 /* OK */;
5323 else if (spec == error_mark_node)
5324 permerror (input_location,
5325 "declaration of %qD ambiguates earlier template "
5326 "instantiation for %qD", decl, inst_decl);
5327 else if (TREE_VALUE (spec) == tmpl)
5328 permerror (input_location,
5329 "partial specialization of %qD after instantiation "
5330 "of %qD", decl, inst_decl);
5331 }
5332 }
5333
5334 return decl;
5335 }
5336
5337 /* PARM is a template parameter of some form; return the corresponding
5338 TEMPLATE_PARM_INDEX. */
5339
5340 static tree
5341 get_template_parm_index (tree parm)
5342 {
5343 if (TREE_CODE (parm) == PARM_DECL
5344 || TREE_CODE (parm) == CONST_DECL)
5345 parm = DECL_INITIAL (parm);
5346 else if (TREE_CODE (parm) == TYPE_DECL
5347 || TREE_CODE (parm) == TEMPLATE_DECL)
5348 parm = TREE_TYPE (parm);
5349 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5350 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5351 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5352 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5353 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5354 return parm;
5355 }
5356
5357 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5358 parameter packs used by the template parameter PARM. */
5359
5360 static void
5361 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5362 {
5363 /* A type parm can't refer to another parm. */
5364 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5365 return;
5366 else if (TREE_CODE (parm) == PARM_DECL)
5367 {
5368 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5369 ppd, ppd->visited);
5370 return;
5371 }
5372
5373 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5374
5375 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5376 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5377 {
5378 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5379 if (template_parameter_pack_p (p))
5380 /* Any packs in the type are expanded by this parameter. */;
5381 else
5382 fixed_parameter_pack_p_1 (p, ppd);
5383 }
5384 }
5385
5386 /* PARM is a template parameter pack. Return any parameter packs used in
5387 its type or the type of any of its template parameters. If there are
5388 any such packs, it will be instantiated into a fixed template parameter
5389 list by partial instantiation rather than be fully deduced. */
5390
5391 tree
5392 fixed_parameter_pack_p (tree parm)
5393 {
5394 /* This can only be true in a member template. */
5395 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5396 return NULL_TREE;
5397 /* This can only be true for a parameter pack. */
5398 if (!template_parameter_pack_p (parm))
5399 return NULL_TREE;
5400 /* A type parm can't refer to another parm. */
5401 if (TREE_CODE (parm) == TYPE_DECL)
5402 return NULL_TREE;
5403
5404 tree parameter_packs = NULL_TREE;
5405 struct find_parameter_pack_data ppd;
5406 ppd.parameter_packs = &parameter_packs;
5407 ppd.visited = new hash_set<tree>;
5408 ppd.type_pack_expansion_p = false;
5409
5410 fixed_parameter_pack_p_1 (parm, &ppd);
5411
5412 delete ppd.visited;
5413 return parameter_packs;
5414 }
5415
5416 /* Check that a template declaration's use of default arguments and
5417 parameter packs is not invalid. Here, PARMS are the template
5418 parameters. IS_PRIMARY is true if DECL is the thing declared by
5419 a primary template. IS_PARTIAL is true if DECL is a partial
5420 specialization.
5421
5422 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5423 function template declaration or a friend class template
5424 declaration. In the function case, 1 indicates a declaration, 2
5425 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5426 emitted for extraneous default arguments.
5427
5428 Returns TRUE if there were no errors found, FALSE otherwise. */
5429
5430 bool
5431 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5432 bool is_partial, int is_friend_decl)
5433 {
5434 const char *msg;
5435 int last_level_to_check;
5436 tree parm_level;
5437 bool no_errors = true;
5438
5439 /* [temp.param]
5440
5441 A default template-argument shall not be specified in a
5442 function template declaration or a function template definition, nor
5443 in the template-parameter-list of the definition of a member of a
5444 class template. */
5445
5446 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5447 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5448 /* You can't have a function template declaration in a local
5449 scope, nor you can you define a member of a class template in a
5450 local scope. */
5451 return true;
5452
5453 if ((TREE_CODE (decl) == TYPE_DECL
5454 && TREE_TYPE (decl)
5455 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5456 || (TREE_CODE (decl) == FUNCTION_DECL
5457 && LAMBDA_FUNCTION_P (decl)))
5458 /* A lambda doesn't have an explicit declaration; don't complain
5459 about the parms of the enclosing class. */
5460 return true;
5461
5462 if (current_class_type
5463 && !TYPE_BEING_DEFINED (current_class_type)
5464 && DECL_LANG_SPECIFIC (decl)
5465 && DECL_DECLARES_FUNCTION_P (decl)
5466 /* If this is either a friend defined in the scope of the class
5467 or a member function. */
5468 && (DECL_FUNCTION_MEMBER_P (decl)
5469 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5470 : DECL_FRIEND_CONTEXT (decl)
5471 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5472 : false)
5473 /* And, if it was a member function, it really was defined in
5474 the scope of the class. */
5475 && (!DECL_FUNCTION_MEMBER_P (decl)
5476 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5477 /* We already checked these parameters when the template was
5478 declared, so there's no need to do it again now. This function
5479 was defined in class scope, but we're processing its body now
5480 that the class is complete. */
5481 return true;
5482
5483 /* Core issue 226 (C++0x only): the following only applies to class
5484 templates. */
5485 if (is_primary
5486 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5487 {
5488 /* [temp.param]
5489
5490 If a template-parameter has a default template-argument, all
5491 subsequent template-parameters shall have a default
5492 template-argument supplied. */
5493 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5494 {
5495 tree inner_parms = TREE_VALUE (parm_level);
5496 int ntparms = TREE_VEC_LENGTH (inner_parms);
5497 int seen_def_arg_p = 0;
5498 int i;
5499
5500 for (i = 0; i < ntparms; ++i)
5501 {
5502 tree parm = TREE_VEC_ELT (inner_parms, i);
5503
5504 if (parm == error_mark_node)
5505 continue;
5506
5507 if (TREE_PURPOSE (parm))
5508 seen_def_arg_p = 1;
5509 else if (seen_def_arg_p
5510 && !template_parameter_pack_p (TREE_VALUE (parm)))
5511 {
5512 error ("no default argument for %qD", TREE_VALUE (parm));
5513 /* For better subsequent error-recovery, we indicate that
5514 there should have been a default argument. */
5515 TREE_PURPOSE (parm) = error_mark_node;
5516 no_errors = false;
5517 }
5518 else if (!is_partial
5519 && !is_friend_decl
5520 /* Don't complain about an enclosing partial
5521 specialization. */
5522 && parm_level == parms
5523 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5524 && i < ntparms - 1
5525 && template_parameter_pack_p (TREE_VALUE (parm))
5526 /* A fixed parameter pack will be partially
5527 instantiated into a fixed length list. */
5528 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5529 {
5530 /* A primary class template, primary variable template
5531 (DR 2032), or alias template can only have one
5532 parameter pack, at the end of the template
5533 parameter list. */
5534
5535 error ("parameter pack %q+D must be at the end of the"
5536 " template parameter list", TREE_VALUE (parm));
5537
5538 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5539 = error_mark_node;
5540 no_errors = false;
5541 }
5542 }
5543 }
5544 }
5545
5546 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5547 || is_partial
5548 || !is_primary
5549 || is_friend_decl)
5550 /* For an ordinary class template, default template arguments are
5551 allowed at the innermost level, e.g.:
5552 template <class T = int>
5553 struct S {};
5554 but, in a partial specialization, they're not allowed even
5555 there, as we have in [temp.class.spec]:
5556
5557 The template parameter list of a specialization shall not
5558 contain default template argument values.
5559
5560 So, for a partial specialization, or for a function template
5561 (in C++98/C++03), we look at all of them. */
5562 ;
5563 else
5564 /* But, for a primary class template that is not a partial
5565 specialization we look at all template parameters except the
5566 innermost ones. */
5567 parms = TREE_CHAIN (parms);
5568
5569 /* Figure out what error message to issue. */
5570 if (is_friend_decl == 2)
5571 msg = G_("default template arguments may not be used in function template "
5572 "friend re-declaration");
5573 else if (is_friend_decl)
5574 msg = G_("default template arguments may not be used in template "
5575 "friend declarations");
5576 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5577 msg = G_("default template arguments may not be used in function templates "
5578 "without %<-std=c++11%> or %<-std=gnu++11%>");
5579 else if (is_partial)
5580 msg = G_("default template arguments may not be used in "
5581 "partial specializations");
5582 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5583 msg = G_("default argument for template parameter for class enclosing %qD");
5584 else
5585 /* Per [temp.param]/9, "A default template-argument shall not be
5586 specified in the template-parameter-lists of the definition of
5587 a member of a class template that appears outside of the member's
5588 class.", thus if we aren't handling a member of a class template
5589 there is no need to examine the parameters. */
5590 return true;
5591
5592 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5593 /* If we're inside a class definition, there's no need to
5594 examine the parameters to the class itself. On the one
5595 hand, they will be checked when the class is defined, and,
5596 on the other, default arguments are valid in things like:
5597 template <class T = double>
5598 struct S { template <class U> void f(U); };
5599 Here the default argument for `S' has no bearing on the
5600 declaration of `f'. */
5601 last_level_to_check = template_class_depth (current_class_type) + 1;
5602 else
5603 /* Check everything. */
5604 last_level_to_check = 0;
5605
5606 for (parm_level = parms;
5607 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5608 parm_level = TREE_CHAIN (parm_level))
5609 {
5610 tree inner_parms = TREE_VALUE (parm_level);
5611 int i;
5612 int ntparms;
5613
5614 ntparms = TREE_VEC_LENGTH (inner_parms);
5615 for (i = 0; i < ntparms; ++i)
5616 {
5617 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5618 continue;
5619
5620 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5621 {
5622 if (msg)
5623 {
5624 no_errors = false;
5625 if (is_friend_decl == 2)
5626 return no_errors;
5627
5628 error (msg, decl);
5629 msg = 0;
5630 }
5631
5632 /* Clear out the default argument so that we are not
5633 confused later. */
5634 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5635 }
5636 }
5637
5638 /* At this point, if we're still interested in issuing messages,
5639 they must apply to classes surrounding the object declared. */
5640 if (msg)
5641 msg = G_("default argument for template parameter for class "
5642 "enclosing %qD");
5643 }
5644
5645 return no_errors;
5646 }
5647
5648 /* Worker for push_template_decl_real, called via
5649 for_each_template_parm. DATA is really an int, indicating the
5650 level of the parameters we are interested in. If T is a template
5651 parameter of that level, return nonzero. */
5652
5653 static int
5654 template_parm_this_level_p (tree t, void* data)
5655 {
5656 int this_level = *(int *)data;
5657 int level;
5658
5659 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5660 level = TEMPLATE_PARM_LEVEL (t);
5661 else
5662 level = TEMPLATE_TYPE_LEVEL (t);
5663 return level == this_level;
5664 }
5665
5666 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5667 DATA is really an int, indicating the innermost outer level of parameters.
5668 If T is a template parameter of that level or further out, return
5669 nonzero. */
5670
5671 static int
5672 template_parm_outer_level (tree t, void *data)
5673 {
5674 int this_level = *(int *)data;
5675 int level;
5676
5677 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5678 level = TEMPLATE_PARM_LEVEL (t);
5679 else
5680 level = TEMPLATE_TYPE_LEVEL (t);
5681 return level <= this_level;
5682 }
5683
5684 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5685 parameters given by current_template_args, or reuses a
5686 previously existing one, if appropriate. Returns the DECL, or an
5687 equivalent one, if it is replaced via a call to duplicate_decls.
5688
5689 If IS_FRIEND is true, DECL is a friend declaration. */
5690
5691 tree
5692 push_template_decl (tree decl, bool is_friend)
5693 {
5694 if (decl == error_mark_node || !current_template_parms)
5695 return error_mark_node;
5696
5697 /* See if this is a partial specialization. */
5698 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5699 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5700 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5701 || (VAR_P (decl)
5702 && DECL_LANG_SPECIFIC (decl)
5703 && DECL_TEMPLATE_SPECIALIZATION (decl)
5704 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5705
5706 /* No surprising friend functions. */
5707 gcc_checking_assert (is_friend
5708 || !(TREE_CODE (decl) == FUNCTION_DECL
5709 && DECL_UNIQUE_FRIEND_P (decl)));
5710
5711 tree ctx;
5712 if (is_friend)
5713 /* For a friend, we want the context of the friend, not
5714 the type of which it is a friend. */
5715 ctx = CP_DECL_CONTEXT (decl);
5716 else if (CP_DECL_CONTEXT (decl)
5717 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5718 /* In the case of a virtual function, we want the class in which
5719 it is defined. */
5720 ctx = CP_DECL_CONTEXT (decl);
5721 else
5722 /* Otherwise, if we're currently defining some class, the DECL
5723 is assumed to be a member of the class. */
5724 ctx = current_scope ();
5725
5726 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5727 ctx = NULL_TREE;
5728
5729 if (!DECL_CONTEXT (decl))
5730 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5731
5732 /* See if this is a primary template. */
5733 bool is_primary = false;
5734 if (is_friend && ctx
5735 && uses_template_parms_level (ctx, processing_template_decl))
5736 /* A friend template that specifies a class context, i.e.
5737 template <typename T> friend void A<T>::f();
5738 is not primary. */
5739 ;
5740 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5741 /* Lambdas are not primary. */
5742 ;
5743 else
5744 is_primary = template_parm_scope_p ();
5745
5746 /* True if the template is a member template, in the sense of
5747 [temp.mem]. */
5748 bool member_template_p = false;
5749
5750 if (is_primary)
5751 {
5752 warning (OPT_Wtemplates, "template %qD declared", decl);
5753
5754 if (DECL_CLASS_SCOPE_P (decl))
5755 member_template_p = true;
5756
5757 if (TREE_CODE (decl) == TYPE_DECL
5758 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5759 {
5760 error ("template class without a name");
5761 return error_mark_node;
5762 }
5763 else if (TREE_CODE (decl) == FUNCTION_DECL)
5764 {
5765 if (member_template_p)
5766 {
5767 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5768 error ("member template %qD may not have virt-specifiers", decl);
5769 }
5770 if (DECL_DESTRUCTOR_P (decl))
5771 {
5772 /* [temp.mem]
5773
5774 A destructor shall not be a member template. */
5775 error_at (DECL_SOURCE_LOCATION (decl),
5776 "destructor %qD declared as member template", decl);
5777 return error_mark_node;
5778 }
5779 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5780 && (!prototype_p (TREE_TYPE (decl))
5781 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5782 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5783 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5784 == void_list_node)))
5785 {
5786 /* [basic.stc.dynamic.allocation]
5787
5788 An allocation function can be a function
5789 template. ... Template allocation functions shall
5790 have two or more parameters. */
5791 error ("invalid template declaration of %qD", decl);
5792 return error_mark_node;
5793 }
5794 }
5795 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5796 && CLASS_TYPE_P (TREE_TYPE (decl)))
5797 {
5798 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5799 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5800 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5801 {
5802 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5803 if (TREE_CODE (t) == TYPE_DECL)
5804 t = TREE_TYPE (t);
5805 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5806 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5807 }
5808 }
5809 else if (TREE_CODE (decl) == TYPE_DECL
5810 && TYPE_DECL_ALIAS_P (decl))
5811 /* alias-declaration */
5812 gcc_assert (!DECL_ARTIFICIAL (decl));
5813 else if (VAR_P (decl))
5814 /* C++14 variable template. */;
5815 else if (TREE_CODE (decl) == CONCEPT_DECL)
5816 /* C++20 concept definitions. */;
5817 else
5818 {
5819 error ("template declaration of %q#D", decl);
5820 return error_mark_node;
5821 }
5822 }
5823
5824 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5825 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5826 || (VAR_OR_FUNCTION_DECL_P (decl)
5827 && DECL_LOCAL_DECL_P (decl))));
5828
5829 /* Check to see that the rules regarding the use of default
5830 arguments are not being violated. We check args for a friend
5831 functions when we know whether it's a definition, introducing
5832 declaration or re-declaration. */
5833 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5834 check_default_tmpl_args (decl, current_template_parms,
5835 is_primary, is_partial, is_friend);
5836
5837 /* Ensure that there are no parameter packs in the type of this
5838 declaration that have not been expanded. */
5839 if (TREE_CODE (decl) == FUNCTION_DECL)
5840 {
5841 /* Check each of the arguments individually to see if there are
5842 any bare parameter packs. */
5843 tree type = TREE_TYPE (decl);
5844 tree arg = DECL_ARGUMENTS (decl);
5845 tree argtype = TYPE_ARG_TYPES (type);
5846
5847 while (arg && argtype)
5848 {
5849 if (!DECL_PACK_P (arg)
5850 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5851 {
5852 /* This is a PARM_DECL that contains unexpanded parameter
5853 packs. We have already complained about this in the
5854 check_for_bare_parameter_packs call, so just replace
5855 these types with ERROR_MARK_NODE. */
5856 TREE_TYPE (arg) = error_mark_node;
5857 TREE_VALUE (argtype) = error_mark_node;
5858 }
5859
5860 arg = DECL_CHAIN (arg);
5861 argtype = TREE_CHAIN (argtype);
5862 }
5863
5864 /* Check for bare parameter packs in the return type and the
5865 exception specifiers. */
5866 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5867 /* Errors were already issued, set return type to int
5868 as the frontend doesn't expect error_mark_node as
5869 the return type. */
5870 TREE_TYPE (type) = integer_type_node;
5871 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5872 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5873 }
5874 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5875 ? DECL_ORIGINAL_TYPE (decl)
5876 : TREE_TYPE (decl)))
5877 {
5878 TREE_TYPE (decl) = error_mark_node;
5879 return error_mark_node;
5880 }
5881
5882 if (is_partial)
5883 return process_partial_specialization (decl);
5884
5885 tree args = current_template_args ();
5886 tree tmpl = NULL_TREE;
5887 bool new_template_p = false;
5888 if (local_p)
5889 {
5890 /* Does not get a template head. */
5891 tmpl = NULL_TREE;
5892 gcc_checking_assert (!is_primary);
5893 }
5894 else if (!ctx
5895 || TREE_CODE (ctx) == FUNCTION_DECL
5896 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5897 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5898 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5899 && DECL_TEMPLATE_INFO (decl))))
5900 {
5901 if (DECL_LANG_SPECIFIC (decl)
5902 && DECL_TEMPLATE_INFO (decl)
5903 && DECL_TI_TEMPLATE (decl))
5904 tmpl = DECL_TI_TEMPLATE (decl);
5905 /* If DECL is a TYPE_DECL for a class-template, then there won't
5906 be DECL_LANG_SPECIFIC. The information equivalent to
5907 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5908 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5909 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5910 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5911 {
5912 /* Since a template declaration already existed for this
5913 class-type, we must be redeclaring it here. Make sure
5914 that the redeclaration is valid. */
5915 redeclare_class_template (TREE_TYPE (decl),
5916 current_template_parms,
5917 current_template_constraints ());
5918 /* We don't need to create a new TEMPLATE_DECL; just use the
5919 one we already had. */
5920 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5921 }
5922 else
5923 {
5924 tmpl = build_template_decl (decl, current_template_parms,
5925 member_template_p);
5926 new_template_p = true;
5927
5928 if (DECL_LANG_SPECIFIC (decl)
5929 && DECL_TEMPLATE_SPECIALIZATION (decl))
5930 {
5931 /* A specialization of a member template of a template
5932 class. */
5933 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5934 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5935 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5936 }
5937 }
5938 }
5939 else
5940 {
5941 tree a, t, current, parms;
5942 int i;
5943 tree tinfo = get_template_info (decl);
5944
5945 if (!tinfo)
5946 {
5947 error ("template definition of non-template %q#D", decl);
5948 return error_mark_node;
5949 }
5950
5951 tmpl = TI_TEMPLATE (tinfo);
5952
5953 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5954 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5955 && DECL_TEMPLATE_SPECIALIZATION (decl)
5956 && DECL_MEMBER_TEMPLATE_P (tmpl))
5957 {
5958 /* The declaration is a specialization of a member
5959 template, declared outside the class. Therefore, the
5960 innermost template arguments will be NULL, so we
5961 replace them with the arguments determined by the
5962 earlier call to check_explicit_specialization. */
5963 args = DECL_TI_ARGS (decl);
5964
5965 tree new_tmpl
5966 = build_template_decl (decl, current_template_parms,
5967 member_template_p);
5968 DECL_TI_TEMPLATE (decl) = new_tmpl;
5969 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5970 DECL_TEMPLATE_INFO (new_tmpl)
5971 = build_template_info (tmpl, args);
5972
5973 register_specialization (new_tmpl,
5974 most_general_template (tmpl),
5975 args,
5976 is_friend, 0);
5977 return decl;
5978 }
5979
5980 /* Make sure the template headers we got make sense. */
5981
5982 parms = DECL_TEMPLATE_PARMS (tmpl);
5983 i = TMPL_PARMS_DEPTH (parms);
5984 if (TMPL_ARGS_DEPTH (args) != i)
5985 {
5986 error ("expected %d levels of template parms for %q#D, got %d",
5987 i, decl, TMPL_ARGS_DEPTH (args));
5988 DECL_INTERFACE_KNOWN (decl) = 1;
5989 return error_mark_node;
5990 }
5991 else
5992 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5993 {
5994 a = TMPL_ARGS_LEVEL (args, i);
5995 t = INNERMOST_TEMPLATE_PARMS (parms);
5996
5997 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5998 {
5999 if (current == decl)
6000 error ("got %d template parameters for %q#D",
6001 TREE_VEC_LENGTH (a), decl);
6002 else
6003 error ("got %d template parameters for %q#T",
6004 TREE_VEC_LENGTH (a), current);
6005 error (" but %d required", TREE_VEC_LENGTH (t));
6006 /* Avoid crash in import_export_decl. */
6007 DECL_INTERFACE_KNOWN (decl) = 1;
6008 return error_mark_node;
6009 }
6010
6011 if (current == decl)
6012 current = ctx;
6013 else if (current == NULL_TREE)
6014 /* Can happen in erroneous input. */
6015 break;
6016 else
6017 current = get_containing_scope (current);
6018 }
6019
6020 /* Check that the parms are used in the appropriate qualifying scopes
6021 in the declarator. */
6022 if (!comp_template_args
6023 (TI_ARGS (tinfo),
6024 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6025 {
6026 error ("template arguments to %qD do not match original "
6027 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6028 if (!uses_template_parms (TI_ARGS (tinfo)))
6029 inform (input_location, "use %<template<>%> for"
6030 " an explicit specialization");
6031 /* Avoid crash in import_export_decl. */
6032 DECL_INTERFACE_KNOWN (decl) = 1;
6033 return error_mark_node;
6034 }
6035 }
6036
6037 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6038
6039 if (new_template_p)
6040 {
6041 /* Push template declarations for global functions and types.
6042 Note that we do not try to push a global template friend
6043 declared in a template class; such a thing may well depend on
6044 the template parameters of the class and we'll push it when
6045 instantiating the befriending class. */
6046 if (!ctx
6047 && !(is_friend && template_class_depth (current_class_type) > 0))
6048 {
6049 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6050 if (pushed == error_mark_node)
6051 return error_mark_node;
6052
6053 /* pushdecl may have found an existing template. */
6054 if (pushed != tmpl)
6055 {
6056 decl = DECL_TEMPLATE_RESULT (pushed);
6057 tmpl = NULL_TREE;
6058 }
6059 }
6060 else if (is_friend)
6061 {
6062 /* Record this decl as belonging to the current class. It's
6063 not chained onto anything else. */
6064 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6065 gcc_checking_assert (!DECL_CHAIN (tmpl));
6066 DECL_CHAIN (tmpl) = current_scope ();
6067 }
6068 }
6069 else if (tmpl)
6070 /* The type may have been completed, or (erroneously) changed. */
6071 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6072
6073 if (tmpl)
6074 {
6075 if (is_primary)
6076 {
6077 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6078
6079 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6080
6081 /* Give template template parms a DECL_CONTEXT of the template
6082 for which they are a parameter. */
6083 parms = INNERMOST_TEMPLATE_PARMS (parms);
6084 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6085 {
6086 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6087 if (TREE_CODE (parm) == TEMPLATE_DECL)
6088 DECL_CONTEXT (parm) = tmpl;
6089 }
6090
6091 if (TREE_CODE (decl) == TYPE_DECL
6092 && TYPE_DECL_ALIAS_P (decl))
6093 {
6094 if (tree constr
6095 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6096 {
6097 /* ??? Why don't we do this here for all templates? */
6098 constr = build_constraints (constr, NULL_TREE);
6099 set_constraints (decl, constr);
6100 }
6101 if (complex_alias_template_p (tmpl))
6102 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6103 }
6104 }
6105
6106 /* The DECL_TI_ARGS of DECL contains full set of arguments
6107 referring wback to its most general template. If TMPL is a
6108 specialization, ARGS may only have the innermost set of
6109 arguments. Add the missing argument levels if necessary. */
6110 if (DECL_TEMPLATE_INFO (tmpl))
6111 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6112
6113 tree info = build_template_info (tmpl, args);
6114
6115 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6116 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6117 else
6118 {
6119 retrofit_lang_decl (decl);
6120 DECL_TEMPLATE_INFO (decl) = info;
6121 }
6122 }
6123
6124 if (flag_implicit_templates
6125 && !is_friend
6126 && TREE_PUBLIC (decl)
6127 && VAR_OR_FUNCTION_DECL_P (decl))
6128 /* Set DECL_COMDAT on template instantiations; if we force
6129 them to be emitted by explicit instantiation,
6130 mark_needed will tell cgraph to do the right thing. */
6131 DECL_COMDAT (decl) = true;
6132
6133 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6134
6135 return decl;
6136 }
6137
6138 /* FN is an inheriting constructor that inherits from the constructor
6139 template INHERITED; turn FN into a constructor template with a matching
6140 template header. */
6141
6142 tree
6143 add_inherited_template_parms (tree fn, tree inherited)
6144 {
6145 tree inner_parms
6146 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6147 inner_parms = copy_node (inner_parms);
6148 tree parms
6149 = tree_cons (size_int (processing_template_decl + 1),
6150 inner_parms, current_template_parms);
6151 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6152 tree args = template_parms_to_args (parms);
6153 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6154 DECL_ARTIFICIAL (tmpl) = true;
6155 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6156 return tmpl;
6157 }
6158
6159 /* Called when a class template TYPE is redeclared with the indicated
6160 template PARMS, e.g.:
6161
6162 template <class T> struct S;
6163 template <class T> struct S {}; */
6164
6165 bool
6166 redeclare_class_template (tree type, tree parms, tree cons)
6167 {
6168 tree tmpl;
6169 tree tmpl_parms;
6170 int i;
6171
6172 if (!TYPE_TEMPLATE_INFO (type))
6173 {
6174 error ("%qT is not a template type", type);
6175 return false;
6176 }
6177
6178 tmpl = TYPE_TI_TEMPLATE (type);
6179 if (!PRIMARY_TEMPLATE_P (tmpl))
6180 /* The type is nested in some template class. Nothing to worry
6181 about here; there are no new template parameters for the nested
6182 type. */
6183 return true;
6184
6185 if (!parms)
6186 {
6187 error ("template specifiers not specified in declaration of %qD",
6188 tmpl);
6189 return false;
6190 }
6191
6192 parms = INNERMOST_TEMPLATE_PARMS (parms);
6193 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6194
6195 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6196 {
6197 error_n (input_location, TREE_VEC_LENGTH (parms),
6198 "redeclared with %d template parameter",
6199 "redeclared with %d template parameters",
6200 TREE_VEC_LENGTH (parms));
6201 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6202 "previous declaration %qD used %d template parameter",
6203 "previous declaration %qD used %d template parameters",
6204 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6205 return false;
6206 }
6207
6208 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6209 {
6210 tree tmpl_parm;
6211 tree parm;
6212 tree tmpl_default;
6213 tree parm_default;
6214
6215 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6216 || TREE_VEC_ELT (parms, i) == error_mark_node)
6217 continue;
6218
6219 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6220 if (error_operand_p (tmpl_parm))
6221 return false;
6222
6223 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6224 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6225 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6226
6227 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6228 TEMPLATE_DECL. */
6229 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6230 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6231 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6232 || (TREE_CODE (tmpl_parm) != PARM_DECL
6233 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6234 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6235 || (TREE_CODE (tmpl_parm) == PARM_DECL
6236 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6237 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6238 {
6239 auto_diagnostic_group d;
6240 error ("template parameter %q+#D", tmpl_parm);
6241 inform (input_location, "redeclared here as %q#D", parm);
6242 return false;
6243 }
6244
6245 /* The parameters can be declared to introduce different
6246 constraints. */
6247 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6248 tree p2 = TREE_VEC_ELT (parms, i);
6249 if (!template_parameter_constraints_equivalent_p (p1, p2))
6250 {
6251 auto_diagnostic_group d;
6252 error ("declaration of template parameter %q+#D with different "
6253 "constraints", parm);
6254 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6255 "original declaration appeared here");
6256 return false;
6257 }
6258
6259 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6260 {
6261 /* We have in [temp.param]:
6262
6263 A template-parameter may not be given default arguments
6264 by two different declarations in the same scope. */
6265 auto_diagnostic_group d;
6266 error_at (input_location, "redefinition of default argument for %q#D", parm);
6267 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6268 "original definition appeared here");
6269 return false;
6270 }
6271
6272 if (parm_default != NULL_TREE)
6273 /* Update the previous template parameters (which are the ones
6274 that will really count) with the new default value. */
6275 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6276 else if (tmpl_default != NULL_TREE)
6277 /* Update the new parameters, too; they'll be used as the
6278 parameters for any members. */
6279 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6280
6281 /* Give each template template parm in this redeclaration a
6282 DECL_CONTEXT of the template for which they are a parameter. */
6283 if (TREE_CODE (parm) == TEMPLATE_DECL)
6284 {
6285 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6286 DECL_CONTEXT (parm) = tmpl;
6287 }
6288
6289 if (TREE_CODE (parm) == TYPE_DECL)
6290 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6291 }
6292
6293 tree ci = get_constraints (tmpl);
6294 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6295 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6296
6297 /* Two classes with different constraints declare different entities. */
6298 if (!cp_tree_equal (req1, req2))
6299 {
6300 auto_diagnostic_group d;
6301 error_at (input_location, "redeclaration %q#D with different "
6302 "constraints", tmpl);
6303 inform (DECL_SOURCE_LOCATION (tmpl),
6304 "original declaration appeared here");
6305 return false;
6306 }
6307
6308 return true;
6309 }
6310
6311 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6312 to be used when the caller has already checked
6313 (processing_template_decl
6314 && !instantiation_dependent_expression_p (expr)
6315 && potential_constant_expression (expr))
6316 and cleared processing_template_decl. */
6317
6318 tree
6319 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6320 {
6321 return tsubst_copy_and_build (expr,
6322 /*args=*/NULL_TREE,
6323 complain,
6324 /*in_decl=*/NULL_TREE,
6325 /*function_p=*/false,
6326 /*integral_constant_expression_p=*/true);
6327 }
6328
6329 /* Simplify EXPR if it is a non-dependent expression. Returns the
6330 (possibly simplified) expression. */
6331
6332 tree
6333 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6334 {
6335 if (expr == NULL_TREE)
6336 return NULL_TREE;
6337
6338 /* If we're in a template, but EXPR isn't value dependent, simplify
6339 it. We're supposed to treat:
6340
6341 template <typename T> void f(T[1 + 1]);
6342 template <typename T> void f(T[2]);
6343
6344 as two declarations of the same function, for example. */
6345 if (processing_template_decl
6346 && is_nondependent_constant_expression (expr))
6347 {
6348 processing_template_decl_sentinel s;
6349 expr = instantiate_non_dependent_expr_internal (expr, complain);
6350 }
6351 return expr;
6352 }
6353
6354 tree
6355 instantiate_non_dependent_expr (tree expr)
6356 {
6357 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6358 }
6359
6360 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6361 an uninstantiated expression. */
6362
6363 tree
6364 instantiate_non_dependent_or_null (tree expr)
6365 {
6366 if (expr == NULL_TREE)
6367 return NULL_TREE;
6368 if (processing_template_decl)
6369 {
6370 if (!is_nondependent_constant_expression (expr))
6371 expr = NULL_TREE;
6372 else
6373 {
6374 processing_template_decl_sentinel s;
6375 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6376 }
6377 }
6378 return expr;
6379 }
6380
6381 /* True iff T is a specialization of a variable template. */
6382
6383 bool
6384 variable_template_specialization_p (tree t)
6385 {
6386 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6387 return false;
6388 tree tmpl = DECL_TI_TEMPLATE (t);
6389 return variable_template_p (tmpl);
6390 }
6391
6392 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6393 template declaration, or a TYPE_DECL for an alias declaration. */
6394
6395 bool
6396 alias_type_or_template_p (tree t)
6397 {
6398 if (t == NULL_TREE)
6399 return false;
6400 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6401 || (TYPE_P (t)
6402 && TYPE_NAME (t)
6403 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6404 || DECL_ALIAS_TEMPLATE_P (t));
6405 }
6406
6407 /* If T is a specialization of an alias template, return it; otherwise return
6408 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6409
6410 tree
6411 alias_template_specialization_p (const_tree t,
6412 bool transparent_typedefs)
6413 {
6414 if (!TYPE_P (t))
6415 return NULL_TREE;
6416
6417 /* It's an alias template specialization if it's an alias and its
6418 TYPE_NAME is a specialization of a primary template. */
6419 if (typedef_variant_p (t))
6420 {
6421 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6422 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6423 return CONST_CAST_TREE (t);
6424 if (transparent_typedefs)
6425 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6426 (TYPE_NAME (t)),
6427 transparent_typedefs);
6428 }
6429
6430 return NULL_TREE;
6431 }
6432
6433 /* An alias template is complex from a SFINAE perspective if a template-id
6434 using that alias can be ill-formed when the expansion is not, as with
6435 the void_t template. We determine this by checking whether the
6436 expansion for the alias template uses all its template parameters. */
6437
6438 struct uses_all_template_parms_data
6439 {
6440 int level;
6441 bool *seen;
6442 };
6443
6444 static int
6445 uses_all_template_parms_r (tree t, void *data_)
6446 {
6447 struct uses_all_template_parms_data &data
6448 = *(struct uses_all_template_parms_data*)data_;
6449 tree idx = get_template_parm_index (t);
6450
6451 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6452 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6453 return 0;
6454 }
6455
6456 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6457
6458 static int
6459 complex_pack_expansion_r (tree t, void *data_)
6460 {
6461 /* An alias template with a pack expansion that expands a pack from the
6462 enclosing class needs to be considered complex, to avoid confusion with
6463 the same pack being used as an argument to the alias's own template
6464 parameter (91966). */
6465 if (!PACK_EXPANSION_P (t))
6466 return 0;
6467 struct uses_all_template_parms_data &data
6468 = *(struct uses_all_template_parms_data*)data_;
6469 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6470 pack = TREE_CHAIN (pack))
6471 {
6472 tree parm_pack = TREE_VALUE (pack);
6473 if (!TEMPLATE_PARM_P (parm_pack))
6474 continue;
6475 int idx, level;
6476 template_parm_level_and_index (parm_pack, &level, &idx);
6477 if (level < data.level)
6478 return 1;
6479 }
6480 return 0;
6481 }
6482
6483 static bool
6484 complex_alias_template_p (const_tree tmpl)
6485 {
6486 /* A renaming alias isn't complex. */
6487 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6488 return false;
6489
6490 /* Any other constrained alias is complex. */
6491 if (get_constraints (tmpl))
6492 return true;
6493
6494 struct uses_all_template_parms_data data;
6495 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6496 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6497 data.level = TMPL_PARMS_DEPTH (parms);
6498 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6499 data.seen = XALLOCAVEC (bool, len);
6500 for (int i = 0; i < len; ++i)
6501 data.seen[i] = false;
6502
6503 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6504 NULL, true, complex_pack_expansion_r))
6505 return true;
6506 for (int i = 0; i < len; ++i)
6507 if (!data.seen[i])
6508 return true;
6509 return false;
6510 }
6511
6512 /* If T is a specialization of a complex alias template with dependent
6513 template-arguments, return it; otherwise return NULL_TREE. If T is a
6514 typedef to such a specialization, return the specialization. */
6515
6516 tree
6517 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6518 {
6519 if (!TYPE_P (t) || !typedef_variant_p (t))
6520 return NULL_TREE;
6521
6522 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6523 if (tinfo
6524 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6525 && (any_dependent_template_arguments_p
6526 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6527 return CONST_CAST_TREE (t);
6528
6529 if (transparent_typedefs)
6530 {
6531 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6532 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6533 }
6534
6535 return NULL_TREE;
6536 }
6537
6538 /* Return the number of innermost template parameters in TMPL. */
6539
6540 static int
6541 num_innermost_template_parms (const_tree tmpl)
6542 {
6543 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6544 return TREE_VEC_LENGTH (parms);
6545 }
6546
6547 /* Return either TMPL or another template that it is equivalent to under DR
6548 1286: An alias that just changes the name of a template is equivalent to
6549 the other template. */
6550
6551 static tree
6552 get_underlying_template (tree tmpl)
6553 {
6554 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6555 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6556 {
6557 /* Determine if the alias is equivalent to an underlying template. */
6558 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6559 /* The underlying type may have been ill-formed. Don't proceed. */
6560 if (!orig_type)
6561 break;
6562 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6563 if (!tinfo)
6564 break;
6565
6566 tree underlying = TI_TEMPLATE (tinfo);
6567 if (!PRIMARY_TEMPLATE_P (underlying)
6568 || (num_innermost_template_parms (tmpl)
6569 != num_innermost_template_parms (underlying)))
6570 break;
6571
6572 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6573 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6574 break;
6575
6576 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6577 it's appropriate to treat a less-constrained alias as equivalent. */
6578 if (!at_least_as_constrained (underlying, tmpl))
6579 break;
6580
6581 /* Alias is equivalent. Strip it and repeat. */
6582 tmpl = underlying;
6583 }
6584
6585 return tmpl;
6586 }
6587
6588 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6589 must be a reference-to-function or a pointer-to-function type, as specified
6590 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6591 and check that the resulting function has external linkage. */
6592
6593 static tree
6594 convert_nontype_argument_function (tree type, tree expr,
6595 tsubst_flags_t complain)
6596 {
6597 tree fns = expr;
6598 tree fn, fn_no_ptr;
6599 linkage_kind linkage;
6600
6601 fn = instantiate_type (type, fns, tf_none);
6602 if (fn == error_mark_node)
6603 return error_mark_node;
6604
6605 if (value_dependent_expression_p (fn))
6606 goto accept;
6607
6608 fn_no_ptr = strip_fnptr_conv (fn);
6609 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6610 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6611 if (BASELINK_P (fn_no_ptr))
6612 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6613
6614 /* [temp.arg.nontype]/1
6615
6616 A template-argument for a non-type, non-template template-parameter
6617 shall be one of:
6618 [...]
6619 -- the address of an object or function with external [C++11: or
6620 internal] linkage. */
6621
6622 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6623 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6624 {
6625 if (complain & tf_error)
6626 {
6627 location_t loc = cp_expr_loc_or_input_loc (expr);
6628 error_at (loc, "%qE is not a valid template argument for type %qT",
6629 expr, type);
6630 if (TYPE_PTR_P (type))
6631 inform (loc, "it must be the address of a function "
6632 "with external linkage");
6633 else
6634 inform (loc, "it must be the name of a function with "
6635 "external linkage");
6636 }
6637 return NULL_TREE;
6638 }
6639
6640 linkage = decl_linkage (fn_no_ptr);
6641 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6642 {
6643 if (complain & tf_error)
6644 {
6645 location_t loc = cp_expr_loc_or_input_loc (expr);
6646 if (cxx_dialect >= cxx11)
6647 error_at (loc, "%qE is not a valid template argument for type "
6648 "%qT because %qD has no linkage",
6649 expr, type, fn_no_ptr);
6650 else
6651 error_at (loc, "%qE is not a valid template argument for type "
6652 "%qT because %qD does not have external linkage",
6653 expr, type, fn_no_ptr);
6654 }
6655 return NULL_TREE;
6656 }
6657
6658 accept:
6659 if (TYPE_REF_P (type))
6660 {
6661 if (REFERENCE_REF_P (fn))
6662 fn = TREE_OPERAND (fn, 0);
6663 else
6664 fn = build_address (fn);
6665 }
6666 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6667 fn = build_nop (type, fn);
6668
6669 return fn;
6670 }
6671
6672 /* Subroutine of convert_nontype_argument.
6673 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6674 Emit an error otherwise. */
6675
6676 static bool
6677 check_valid_ptrmem_cst_expr (tree type, tree expr,
6678 tsubst_flags_t complain)
6679 {
6680 tree orig_expr = expr;
6681 STRIP_NOPS (expr);
6682 if (null_ptr_cst_p (expr))
6683 return true;
6684 if (TREE_CODE (expr) == PTRMEM_CST
6685 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6686 PTRMEM_CST_CLASS (expr)))
6687 return true;
6688 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6689 return true;
6690 if (processing_template_decl
6691 && TREE_CODE (expr) == ADDR_EXPR
6692 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6693 return true;
6694 if (complain & tf_error)
6695 {
6696 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6697 error_at (loc, "%qE is not a valid template argument for type %qT",
6698 orig_expr, type);
6699 if (TREE_CODE (expr) != PTRMEM_CST)
6700 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6701 else
6702 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6703 }
6704 return false;
6705 }
6706
6707 /* Returns TRUE iff the address of OP is value-dependent.
6708
6709 14.6.2.4 [temp.dep.temp]:
6710 A non-integral non-type template-argument is dependent if its type is
6711 dependent or it has either of the following forms
6712 qualified-id
6713 & qualified-id
6714 and contains a nested-name-specifier which specifies a class-name that
6715 names a dependent type.
6716
6717 We generalize this to just say that the address of a member of a
6718 dependent class is value-dependent; the above doesn't cover the
6719 address of a static data member named with an unqualified-id. */
6720
6721 static bool
6722 has_value_dependent_address (tree op)
6723 {
6724 STRIP_ANY_LOCATION_WRAPPER (op);
6725
6726 /* We could use get_inner_reference here, but there's no need;
6727 this is only relevant for template non-type arguments, which
6728 can only be expressed as &id-expression. */
6729 if (DECL_P (op))
6730 {
6731 tree ctx = CP_DECL_CONTEXT (op);
6732 if (TYPE_P (ctx) && dependent_type_p (ctx))
6733 return true;
6734 }
6735
6736 return false;
6737 }
6738
6739 /* The next set of functions are used for providing helpful explanatory
6740 diagnostics for failed overload resolution. Their messages should be
6741 indented by two spaces for consistency with the messages in
6742 call.c */
6743
6744 static int
6745 unify_success (bool /*explain_p*/)
6746 {
6747 return 0;
6748 }
6749
6750 /* Other failure functions should call this one, to provide a single function
6751 for setting a breakpoint on. */
6752
6753 static int
6754 unify_invalid (bool /*explain_p*/)
6755 {
6756 return 1;
6757 }
6758
6759 static int
6760 unify_parameter_deduction_failure (bool explain_p, tree parm)
6761 {
6762 if (explain_p)
6763 inform (input_location,
6764 " couldn%'t deduce template parameter %qD", parm);
6765 return unify_invalid (explain_p);
6766 }
6767
6768 static int
6769 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6770 {
6771 if (explain_p)
6772 inform (input_location,
6773 " types %qT and %qT have incompatible cv-qualifiers",
6774 parm, arg);
6775 return unify_invalid (explain_p);
6776 }
6777
6778 static int
6779 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6780 {
6781 if (explain_p)
6782 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6783 return unify_invalid (explain_p);
6784 }
6785
6786 static int
6787 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6788 {
6789 if (explain_p)
6790 inform (input_location,
6791 " template parameter %qD is not a parameter pack, but "
6792 "argument %qD is",
6793 parm, arg);
6794 return unify_invalid (explain_p);
6795 }
6796
6797 static int
6798 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6799 {
6800 if (explain_p)
6801 inform (input_location,
6802 " template argument %qE does not match "
6803 "pointer-to-member constant %qE",
6804 arg, parm);
6805 return unify_invalid (explain_p);
6806 }
6807
6808 static int
6809 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6810 {
6811 if (explain_p)
6812 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6813 return unify_invalid (explain_p);
6814 }
6815
6816 static int
6817 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6818 {
6819 if (explain_p)
6820 inform (input_location,
6821 " inconsistent parameter pack deduction with %qT and %qT",
6822 old_arg, new_arg);
6823 return unify_invalid (explain_p);
6824 }
6825
6826 static int
6827 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6828 {
6829 if (explain_p)
6830 {
6831 if (TYPE_P (parm))
6832 inform (input_location,
6833 " deduced conflicting types for parameter %qT (%qT and %qT)",
6834 parm, first, second);
6835 else
6836 inform (input_location,
6837 " deduced conflicting values for non-type parameter "
6838 "%qE (%qE and %qE)", parm, first, second);
6839 }
6840 return unify_invalid (explain_p);
6841 }
6842
6843 static int
6844 unify_vla_arg (bool explain_p, tree arg)
6845 {
6846 if (explain_p)
6847 inform (input_location,
6848 " variable-sized array type %qT is not "
6849 "a valid template argument",
6850 arg);
6851 return unify_invalid (explain_p);
6852 }
6853
6854 static int
6855 unify_method_type_error (bool explain_p, tree arg)
6856 {
6857 if (explain_p)
6858 inform (input_location,
6859 " member function type %qT is not a valid template argument",
6860 arg);
6861 return unify_invalid (explain_p);
6862 }
6863
6864 static int
6865 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6866 {
6867 if (explain_p)
6868 {
6869 if (least_p)
6870 inform_n (input_location, wanted,
6871 " candidate expects at least %d argument, %d provided",
6872 " candidate expects at least %d arguments, %d provided",
6873 wanted, have);
6874 else
6875 inform_n (input_location, wanted,
6876 " candidate expects %d argument, %d provided",
6877 " candidate expects %d arguments, %d provided",
6878 wanted, have);
6879 }
6880 return unify_invalid (explain_p);
6881 }
6882
6883 static int
6884 unify_too_many_arguments (bool explain_p, int have, int wanted)
6885 {
6886 return unify_arity (explain_p, have, wanted);
6887 }
6888
6889 static int
6890 unify_too_few_arguments (bool explain_p, int have, int wanted,
6891 bool least_p = false)
6892 {
6893 return unify_arity (explain_p, have, wanted, least_p);
6894 }
6895
6896 static int
6897 unify_arg_conversion (bool explain_p, tree to_type,
6898 tree from_type, tree arg)
6899 {
6900 if (explain_p)
6901 inform (cp_expr_loc_or_input_loc (arg),
6902 " cannot convert %qE (type %qT) to type %qT",
6903 arg, from_type, to_type);
6904 return unify_invalid (explain_p);
6905 }
6906
6907 static int
6908 unify_no_common_base (bool explain_p, enum template_base_result r,
6909 tree parm, tree arg)
6910 {
6911 if (explain_p)
6912 switch (r)
6913 {
6914 case tbr_ambiguous_baseclass:
6915 inform (input_location, " %qT is an ambiguous base class of %qT",
6916 parm, arg);
6917 break;
6918 default:
6919 inform (input_location, " %qT is not derived from %qT", arg, parm);
6920 break;
6921 }
6922 return unify_invalid (explain_p);
6923 }
6924
6925 static int
6926 unify_inconsistent_template_template_parameters (bool explain_p)
6927 {
6928 if (explain_p)
6929 inform (input_location,
6930 " template parameters of a template template argument are "
6931 "inconsistent with other deduced template arguments");
6932 return unify_invalid (explain_p);
6933 }
6934
6935 static int
6936 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6937 {
6938 if (explain_p)
6939 inform (input_location,
6940 " cannot deduce a template for %qT from non-template type %qT",
6941 parm, arg);
6942 return unify_invalid (explain_p);
6943 }
6944
6945 static int
6946 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6947 {
6948 if (explain_p)
6949 inform (input_location,
6950 " template argument %qE does not match %qE", arg, parm);
6951 return unify_invalid (explain_p);
6952 }
6953
6954 /* True if T is a C++20 template parameter object to store the argument for a
6955 template parameter of class type. */
6956
6957 bool
6958 template_parm_object_p (const_tree t)
6959 {
6960 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6961 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6962 }
6963
6964 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6965 argument for TYPE, points to an unsuitable object.
6966
6967 Also adjust the type of the index in C++20 array subobject references. */
6968
6969 static bool
6970 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6971 {
6972 switch (TREE_CODE (expr))
6973 {
6974 CASE_CONVERT:
6975 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6976 complain);
6977
6978 case TARGET_EXPR:
6979 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6980 complain);
6981
6982 case CONSTRUCTOR:
6983 {
6984 unsigned i; tree elt;
6985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6986 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6987 return true;
6988 }
6989 break;
6990
6991 case ADDR_EXPR:
6992 {
6993 tree decl = TREE_OPERAND (expr, 0);
6994
6995 if (cxx_dialect >= cxx20)
6996 while (TREE_CODE (decl) == COMPONENT_REF
6997 || TREE_CODE (decl) == ARRAY_REF)
6998 {
6999 tree &op = TREE_OPERAND (decl, 1);
7000 if (TREE_CODE (decl) == ARRAY_REF
7001 && TREE_CODE (op) == INTEGER_CST)
7002 /* Canonicalize array offsets to ptrdiff_t; how they were
7003 written doesn't matter for subobject identity. */
7004 op = fold_convert (ptrdiff_type_node, op);
7005 decl = TREE_OPERAND (decl, 0);
7006 }
7007
7008 if (!VAR_P (decl))
7009 {
7010 if (complain & tf_error)
7011 error_at (cp_expr_loc_or_input_loc (expr),
7012 "%qE is not a valid template argument of type %qT "
7013 "because %qE is not a variable", expr, type, decl);
7014 return true;
7015 }
7016 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7017 {
7018 if (complain & tf_error)
7019 error_at (cp_expr_loc_or_input_loc (expr),
7020 "%qE is not a valid template argument of type %qT "
7021 "in C++98 because %qD does not have external linkage",
7022 expr, type, decl);
7023 return true;
7024 }
7025 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7026 && decl_linkage (decl) == lk_none)
7027 {
7028 if (complain & tf_error)
7029 error_at (cp_expr_loc_or_input_loc (expr),
7030 "%qE is not a valid template argument of type %qT "
7031 "because %qD has no linkage", expr, type, decl);
7032 return true;
7033 }
7034 /* C++17: For a non-type template-parameter of reference or pointer
7035 type, the value of the constant expression shall not refer to (or
7036 for a pointer type, shall not be the address of):
7037 * a subobject (4.5),
7038 * a temporary object (15.2),
7039 * a string literal (5.13.5),
7040 * the result of a typeid expression (8.2.8), or
7041 * a predefined __func__ variable (11.4.1). */
7042 else if (DECL_ARTIFICIAL (decl))
7043 {
7044 if (complain & tf_error)
7045 error ("the address of %qD is not a valid template argument",
7046 decl);
7047 return true;
7048 }
7049 else if (cxx_dialect < cxx20
7050 && !(same_type_ignoring_top_level_qualifiers_p
7051 (strip_array_types (TREE_TYPE (type)),
7052 strip_array_types (TREE_TYPE (decl)))))
7053 {
7054 if (complain & tf_error)
7055 error ("the address of the %qT subobject of %qD is not a "
7056 "valid template argument", TREE_TYPE (type), decl);
7057 return true;
7058 }
7059 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7060 {
7061 if (complain & tf_error)
7062 error ("the address of %qD is not a valid template argument "
7063 "because it does not have static storage duration",
7064 decl);
7065 return true;
7066 }
7067 }
7068 break;
7069
7070 default:
7071 if (!INDIRECT_TYPE_P (type))
7072 /* We're only concerned about pointers and references here. */;
7073 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7074 /* Null pointer values are OK in C++11. */;
7075 else
7076 {
7077 if (VAR_P (expr))
7078 {
7079 if (complain & tf_error)
7080 error ("%qD is not a valid template argument "
7081 "because %qD is a variable, not the address of "
7082 "a variable", expr, expr);
7083 return true;
7084 }
7085 else
7086 {
7087 if (complain & tf_error)
7088 error ("%qE is not a valid template argument for %qT "
7089 "because it is not the address of a variable",
7090 expr, type);
7091 return true;
7092 }
7093 }
7094 }
7095 return false;
7096
7097 }
7098
7099 /* The template arguments corresponding to template parameter objects of types
7100 that contain pointers to members. */
7101
7102 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7103
7104 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7105 template argument EXPR. */
7106
7107 static tree
7108 get_template_parm_object (tree expr, tsubst_flags_t complain)
7109 {
7110 if (TREE_CODE (expr) == TARGET_EXPR)
7111 expr = TARGET_EXPR_INITIAL (expr);
7112
7113 if (!TREE_CONSTANT (expr))
7114 {
7115 if ((complain & tf_error)
7116 && require_rvalue_constant_expression (expr))
7117 cxx_constant_value (expr);
7118 return error_mark_node;
7119 }
7120 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7121 return error_mark_node;
7122
7123 tree name = mangle_template_parm_object (expr);
7124 tree decl = get_global_binding (name);
7125 if (decl)
7126 return decl;
7127
7128 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7129 decl = create_temporary_var (type);
7130 DECL_CONTEXT (decl) = NULL_TREE;
7131 TREE_STATIC (decl) = true;
7132 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7133 TREE_READONLY (decl) = true;
7134 DECL_NAME (decl) = name;
7135 SET_DECL_ASSEMBLER_NAME (decl, name);
7136 comdat_linkage (decl);
7137
7138 if (!zero_init_p (type))
7139 {
7140 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7141 lower_var_init before we're done mangling. So store the original
7142 value elsewhere. */
7143 tree copy = unshare_constructor (expr);
7144 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7145 }
7146
7147 pushdecl_top_level_and_finish (decl, expr);
7148
7149 return decl;
7150 }
7151
7152 /* Return the actual template argument corresponding to template parameter
7153 object VAR. */
7154
7155 tree
7156 tparm_object_argument (tree var)
7157 {
7158 if (zero_init_p (TREE_TYPE (var)))
7159 return DECL_INITIAL (var);
7160 return *(tparm_obj_values->get (var));
7161 }
7162
7163 /* Attempt to convert the non-type template parameter EXPR to the
7164 indicated TYPE. If the conversion is successful, return the
7165 converted value. If the conversion is unsuccessful, return
7166 NULL_TREE if we issued an error message, or error_mark_node if we
7167 did not. We issue error messages for out-and-out bad template
7168 parameters, but not simply because the conversion failed, since we
7169 might be just trying to do argument deduction. Both TYPE and EXPR
7170 must be non-dependent.
7171
7172 The conversion follows the special rules described in
7173 [temp.arg.nontype], and it is much more strict than an implicit
7174 conversion.
7175
7176 This function is called twice for each template argument (see
7177 lookup_template_class for a more accurate description of this
7178 problem). This means that we need to handle expressions which
7179 are not valid in a C++ source, but can be created from the
7180 first call (for instance, casts to perform conversions). These
7181 hacks can go away after we fix the double coercion problem. */
7182
7183 static tree
7184 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7185 {
7186 tree expr_type;
7187 location_t loc = cp_expr_loc_or_input_loc (expr);
7188
7189 /* Detect immediately string literals as invalid non-type argument.
7190 This special-case is not needed for correctness (we would easily
7191 catch this later), but only to provide better diagnostic for this
7192 common user mistake. As suggested by DR 100, we do not mention
7193 linkage issues in the diagnostic as this is not the point. */
7194 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7195 {
7196 if (complain & tf_error)
7197 error ("%qE is not a valid template argument for type %qT "
7198 "because string literals can never be used in this context",
7199 expr, type);
7200 return NULL_TREE;
7201 }
7202
7203 /* Add the ADDR_EXPR now for the benefit of
7204 value_dependent_expression_p. */
7205 if (TYPE_PTROBV_P (type)
7206 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7207 {
7208 expr = decay_conversion (expr, complain);
7209 if (expr == error_mark_node)
7210 return error_mark_node;
7211 }
7212
7213 /* If we are in a template, EXPR may be non-dependent, but still
7214 have a syntactic, rather than semantic, form. For example, EXPR
7215 might be a SCOPE_REF, rather than the VAR_DECL to which the
7216 SCOPE_REF refers. Preserving the qualifying scope is necessary
7217 so that access checking can be performed when the template is
7218 instantiated -- but here we need the resolved form so that we can
7219 convert the argument. */
7220 bool non_dep = false;
7221 if (TYPE_REF_OBJ_P (type)
7222 && has_value_dependent_address (expr))
7223 /* If we want the address and it's value-dependent, don't fold. */;
7224 else if (processing_template_decl
7225 && is_nondependent_constant_expression (expr))
7226 non_dep = true;
7227 if (error_operand_p (expr))
7228 return error_mark_node;
7229 expr_type = TREE_TYPE (expr);
7230
7231 /* If the argument is non-dependent, perform any conversions in
7232 non-dependent context as well. */
7233 processing_template_decl_sentinel s (non_dep);
7234 if (non_dep)
7235 expr = instantiate_non_dependent_expr_internal (expr, complain);
7236
7237 const bool val_dep_p = value_dependent_expression_p (expr);
7238 if (val_dep_p)
7239 expr = canonicalize_expr_argument (expr, complain);
7240
7241 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7242 to a non-type argument of "nullptr". */
7243 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7244 expr = fold_simple (convert (type, expr));
7245
7246 /* In C++11, integral or enumeration non-type template arguments can be
7247 arbitrary constant expressions. Pointer and pointer to
7248 member arguments can be general constant expressions that evaluate
7249 to a null value, but otherwise still need to be of a specific form. */
7250 if (cxx_dialect >= cxx11)
7251 {
7252 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7253 /* A PTRMEM_CST is already constant, and a valid template
7254 argument for a parameter of pointer to member type, we just want
7255 to leave it in that form rather than lower it to a
7256 CONSTRUCTOR. */;
7257 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7258 || cxx_dialect >= cxx17)
7259 {
7260 /* C++17: A template-argument for a non-type template-parameter shall
7261 be a converted constant expression (8.20) of the type of the
7262 template-parameter. */
7263 expr = build_converted_constant_expr (type, expr, complain);
7264 if (expr == error_mark_node)
7265 /* Make sure we return NULL_TREE only if we have really issued
7266 an error, as described above. */
7267 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7268 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7269 {
7270 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7271 return expr;
7272 }
7273 expr = maybe_constant_value (expr, NULL_TREE,
7274 /*manifestly_const_eval=*/true);
7275 expr = convert_from_reference (expr);
7276 }
7277 else if (TYPE_PTR_OR_PTRMEM_P (type))
7278 {
7279 tree folded = maybe_constant_value (expr, NULL_TREE,
7280 /*manifestly_const_eval=*/true);
7281 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7282 : null_member_pointer_value_p (folded))
7283 expr = folded;
7284 }
7285 }
7286
7287 if (TYPE_REF_P (type))
7288 expr = mark_lvalue_use (expr);
7289 else
7290 expr = mark_rvalue_use (expr);
7291
7292 /* HACK: Due to double coercion, we can get a
7293 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7294 which is the tree that we built on the first call (see
7295 below when coercing to reference to object or to reference to
7296 function). We just strip everything and get to the arg.
7297 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7298 for examples. */
7299 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7300 {
7301 tree probe_type, probe = expr;
7302 if (REFERENCE_REF_P (probe))
7303 probe = TREE_OPERAND (probe, 0);
7304 probe_type = TREE_TYPE (probe);
7305 if (TREE_CODE (probe) == NOP_EXPR)
7306 {
7307 /* ??? Maybe we could use convert_from_reference here, but we
7308 would need to relax its constraints because the NOP_EXPR
7309 could actually change the type to something more cv-qualified,
7310 and this is not folded by convert_from_reference. */
7311 tree addr = TREE_OPERAND (probe, 0);
7312 if (TYPE_REF_P (probe_type)
7313 && TREE_CODE (addr) == ADDR_EXPR
7314 && TYPE_PTR_P (TREE_TYPE (addr))
7315 && (same_type_ignoring_top_level_qualifiers_p
7316 (TREE_TYPE (probe_type),
7317 TREE_TYPE (TREE_TYPE (addr)))))
7318 {
7319 expr = TREE_OPERAND (addr, 0);
7320 expr_type = TREE_TYPE (probe_type);
7321 }
7322 }
7323 }
7324
7325 /* [temp.arg.nontype]/5, bullet 1
7326
7327 For a non-type template-parameter of integral or enumeration type,
7328 integral promotions (_conv.prom_) and integral conversions
7329 (_conv.integral_) are applied. */
7330 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7331 || TREE_CODE (type) == REAL_TYPE)
7332 {
7333 if (cxx_dialect < cxx11)
7334 {
7335 tree t = build_converted_constant_expr (type, expr, complain);
7336 t = maybe_constant_value (t);
7337 if (t != error_mark_node)
7338 expr = t;
7339 }
7340
7341 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7342 return error_mark_node;
7343
7344 /* Notice that there are constant expressions like '4 % 0' which
7345 do not fold into integer constants. */
7346 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7347 {
7348 if (complain & tf_error)
7349 {
7350 int errs = errorcount, warns = warningcount + werrorcount;
7351 if (!require_potential_constant_expression (expr))
7352 expr = error_mark_node;
7353 else
7354 expr = cxx_constant_value (expr);
7355 if (errorcount > errs || warningcount + werrorcount > warns)
7356 inform (loc, "in template argument for type %qT", type);
7357 if (expr == error_mark_node)
7358 return NULL_TREE;
7359 /* else cxx_constant_value complained but gave us
7360 a real constant, so go ahead. */
7361 if (!CONSTANT_CLASS_P (expr))
7362 {
7363 /* Some assemble time constant expressions like
7364 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7365 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7366 as we can emit them into .rodata initializers of
7367 variables, yet they can't fold into an INTEGER_CST at
7368 compile time. Refuse them here. */
7369 gcc_checking_assert (reduced_constant_expression_p (expr));
7370 error_at (loc, "template argument %qE for type %qT not "
7371 "a compile-time constant", expr, type);
7372 return NULL_TREE;
7373 }
7374 }
7375 else
7376 return NULL_TREE;
7377 }
7378
7379 /* Avoid typedef problems. */
7380 if (TREE_TYPE (expr) != type)
7381 expr = fold_convert (type, expr);
7382 }
7383 /* [temp.arg.nontype]/5, bullet 2
7384
7385 For a non-type template-parameter of type pointer to object,
7386 qualification conversions (_conv.qual_) and the array-to-pointer
7387 conversion (_conv.array_) are applied. */
7388 else if (TYPE_PTROBV_P (type))
7389 {
7390 tree decayed = expr;
7391
7392 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7393 decay_conversion or an explicit cast. If it's a problematic cast,
7394 we'll complain about it below. */
7395 if (TREE_CODE (expr) == NOP_EXPR)
7396 {
7397 tree probe = expr;
7398 STRIP_NOPS (probe);
7399 if (TREE_CODE (probe) == ADDR_EXPR
7400 && TYPE_PTR_P (TREE_TYPE (probe)))
7401 {
7402 expr = probe;
7403 expr_type = TREE_TYPE (expr);
7404 }
7405 }
7406
7407 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7408
7409 A template-argument for a non-type, non-template template-parameter
7410 shall be one of: [...]
7411
7412 -- the name of a non-type template-parameter;
7413 -- the address of an object or function with external linkage, [...]
7414 expressed as "& id-expression" where the & is optional if the name
7415 refers to a function or array, or if the corresponding
7416 template-parameter is a reference.
7417
7418 Here, we do not care about functions, as they are invalid anyway
7419 for a parameter of type pointer-to-object. */
7420
7421 if (val_dep_p)
7422 /* Non-type template parameters are OK. */
7423 ;
7424 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7425 /* Null pointer values are OK in C++11. */;
7426 else if (TREE_CODE (expr) != ADDR_EXPR
7427 && !INDIRECT_TYPE_P (expr_type))
7428 /* Other values, like integer constants, might be valid
7429 non-type arguments of some other type. */
7430 return error_mark_node;
7431 else if (invalid_tparm_referent_p (type, expr, complain))
7432 return NULL_TREE;
7433
7434 expr = decayed;
7435
7436 expr = perform_qualification_conversions (type, expr);
7437 if (expr == error_mark_node)
7438 return error_mark_node;
7439 }
7440 /* [temp.arg.nontype]/5, bullet 3
7441
7442 For a non-type template-parameter of type reference to object, no
7443 conversions apply. The type referred to by the reference may be more
7444 cv-qualified than the (otherwise identical) type of the
7445 template-argument. The template-parameter is bound directly to the
7446 template-argument, which must be an lvalue. */
7447 else if (TYPE_REF_OBJ_P (type))
7448 {
7449 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7450 expr_type))
7451 return error_mark_node;
7452
7453 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7454 {
7455 if (complain & tf_error)
7456 error ("%qE is not a valid template argument for type %qT "
7457 "because of conflicts in cv-qualification", expr, type);
7458 return NULL_TREE;
7459 }
7460
7461 if (!lvalue_p (expr))
7462 {
7463 if (complain & tf_error)
7464 error ("%qE is not a valid template argument for type %qT "
7465 "because it is not an lvalue", expr, type);
7466 return NULL_TREE;
7467 }
7468
7469 /* [temp.arg.nontype]/1
7470
7471 A template-argument for a non-type, non-template template-parameter
7472 shall be one of: [...]
7473
7474 -- the address of an object or function with external linkage. */
7475 if (INDIRECT_REF_P (expr)
7476 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7477 {
7478 expr = TREE_OPERAND (expr, 0);
7479 if (DECL_P (expr))
7480 {
7481 if (complain & tf_error)
7482 error ("%q#D is not a valid template argument for type %qT "
7483 "because a reference variable does not have a constant "
7484 "address", expr, type);
7485 return NULL_TREE;
7486 }
7487 }
7488
7489 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7490 /* OK, dependent reference. We don't want to ask whether a DECL is
7491 itself value-dependent, since what we want here is its address. */;
7492 else
7493 {
7494 expr = build_address (expr);
7495
7496 if (invalid_tparm_referent_p (type, expr, complain))
7497 return NULL_TREE;
7498 }
7499
7500 if (!same_type_p (type, TREE_TYPE (expr)))
7501 expr = build_nop (type, expr);
7502 }
7503 /* [temp.arg.nontype]/5, bullet 4
7504
7505 For a non-type template-parameter of type pointer to function, only
7506 the function-to-pointer conversion (_conv.func_) is applied. If the
7507 template-argument represents a set of overloaded functions (or a
7508 pointer to such), the matching function is selected from the set
7509 (_over.over_). */
7510 else if (TYPE_PTRFN_P (type))
7511 {
7512 /* If the argument is a template-id, we might not have enough
7513 context information to decay the pointer. */
7514 if (!type_unknown_p (expr_type))
7515 {
7516 expr = decay_conversion (expr, complain);
7517 if (expr == error_mark_node)
7518 return error_mark_node;
7519 }
7520
7521 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7522 /* Null pointer values are OK in C++11. */
7523 return perform_qualification_conversions (type, expr);
7524
7525 expr = convert_nontype_argument_function (type, expr, complain);
7526 if (!expr || expr == error_mark_node)
7527 return expr;
7528 }
7529 /* [temp.arg.nontype]/5, bullet 5
7530
7531 For a non-type template-parameter of type reference to function, no
7532 conversions apply. If the template-argument represents a set of
7533 overloaded functions, the matching function is selected from the set
7534 (_over.over_). */
7535 else if (TYPE_REFFN_P (type))
7536 {
7537 if (TREE_CODE (expr) == ADDR_EXPR)
7538 {
7539 if (complain & tf_error)
7540 {
7541 error ("%qE is not a valid template argument for type %qT "
7542 "because it is a pointer", expr, type);
7543 inform (input_location, "try using %qE instead",
7544 TREE_OPERAND (expr, 0));
7545 }
7546 return NULL_TREE;
7547 }
7548
7549 expr = convert_nontype_argument_function (type, expr, complain);
7550 if (!expr || expr == error_mark_node)
7551 return expr;
7552 }
7553 /* [temp.arg.nontype]/5, bullet 6
7554
7555 For a non-type template-parameter of type pointer to member function,
7556 no conversions apply. If the template-argument represents a set of
7557 overloaded member functions, the matching member function is selected
7558 from the set (_over.over_). */
7559 else if (TYPE_PTRMEMFUNC_P (type))
7560 {
7561 expr = instantiate_type (type, expr, tf_none);
7562 if (expr == error_mark_node)
7563 return error_mark_node;
7564
7565 /* [temp.arg.nontype] bullet 1 says the pointer to member
7566 expression must be a pointer-to-member constant. */
7567 if (!val_dep_p
7568 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7569 return NULL_TREE;
7570
7571 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7572 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7573 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7574 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7575 }
7576 /* [temp.arg.nontype]/5, bullet 7
7577
7578 For a non-type template-parameter of type pointer to data member,
7579 qualification conversions (_conv.qual_) are applied. */
7580 else if (TYPE_PTRDATAMEM_P (type))
7581 {
7582 /* [temp.arg.nontype] bullet 1 says the pointer to member
7583 expression must be a pointer-to-member constant. */
7584 if (!val_dep_p
7585 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7586 return NULL_TREE;
7587
7588 expr = perform_qualification_conversions (type, expr);
7589 if (expr == error_mark_node)
7590 return expr;
7591 }
7592 else if (NULLPTR_TYPE_P (type))
7593 {
7594 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7595 {
7596 if (complain & tf_error)
7597 error ("%qE is not a valid template argument for type %qT "
7598 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7599 return NULL_TREE;
7600 }
7601 return expr;
7602 }
7603 else if (CLASS_TYPE_P (type))
7604 {
7605 /* Replace the argument with a reference to the corresponding template
7606 parameter object. */
7607 if (!val_dep_p)
7608 expr = get_template_parm_object (expr, complain);
7609 if (expr == error_mark_node)
7610 return NULL_TREE;
7611 }
7612 /* A template non-type parameter must be one of the above. */
7613 else
7614 gcc_unreachable ();
7615
7616 /* Sanity check: did we actually convert the argument to the
7617 right type? */
7618 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7619 (type, TREE_TYPE (expr)));
7620 return convert_from_reference (expr);
7621 }
7622
7623 /* Subroutine of coerce_template_template_parms, which returns 1 if
7624 PARM_PARM and ARG_PARM match using the rule for the template
7625 parameters of template template parameters. Both PARM and ARG are
7626 template parameters; the rest of the arguments are the same as for
7627 coerce_template_template_parms.
7628 */
7629 static int
7630 coerce_template_template_parm (tree parm,
7631 tree arg,
7632 tsubst_flags_t complain,
7633 tree in_decl,
7634 tree outer_args)
7635 {
7636 if (arg == NULL_TREE || error_operand_p (arg)
7637 || parm == NULL_TREE || error_operand_p (parm))
7638 return 0;
7639
7640 if (TREE_CODE (arg) != TREE_CODE (parm))
7641 return 0;
7642
7643 switch (TREE_CODE (parm))
7644 {
7645 case TEMPLATE_DECL:
7646 /* We encounter instantiations of templates like
7647 template <template <template <class> class> class TT>
7648 class C; */
7649 {
7650 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7651 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7652
7653 if (!coerce_template_template_parms
7654 (parmparm, argparm, complain, in_decl, outer_args))
7655 return 0;
7656 }
7657 /* Fall through. */
7658
7659 case TYPE_DECL:
7660 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7661 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7662 /* Argument is a parameter pack but parameter is not. */
7663 return 0;
7664 break;
7665
7666 case PARM_DECL:
7667 /* The tsubst call is used to handle cases such as
7668
7669 template <int> class C {};
7670 template <class T, template <T> class TT> class D {};
7671 D<int, C> d;
7672
7673 i.e. the parameter list of TT depends on earlier parameters. */
7674 if (!uses_template_parms (TREE_TYPE (arg)))
7675 {
7676 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7677 if (!uses_template_parms (t)
7678 && !same_type_p (t, TREE_TYPE (arg)))
7679 return 0;
7680 }
7681
7682 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7683 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7684 /* Argument is a parameter pack but parameter is not. */
7685 return 0;
7686
7687 break;
7688
7689 default:
7690 gcc_unreachable ();
7691 }
7692
7693 return 1;
7694 }
7695
7696 /* Coerce template argument list ARGLIST for use with template
7697 template-parameter TEMPL. */
7698
7699 static tree
7700 coerce_template_args_for_ttp (tree templ, tree arglist,
7701 tsubst_flags_t complain)
7702 {
7703 /* Consider an example where a template template parameter declared as
7704
7705 template <class T, class U = std::allocator<T> > class TT
7706
7707 The template parameter level of T and U are one level larger than
7708 of TT. To proper process the default argument of U, say when an
7709 instantiation `TT<int>' is seen, we need to build the full
7710 arguments containing {int} as the innermost level. Outer levels,
7711 available when not appearing as default template argument, can be
7712 obtained from the arguments of the enclosing template.
7713
7714 Suppose that TT is later substituted with std::vector. The above
7715 instantiation is `TT<int, std::allocator<T> >' with TT at
7716 level 1, and T at level 2, while the template arguments at level 1
7717 becomes {std::vector} and the inner level 2 is {int}. */
7718
7719 tree outer = DECL_CONTEXT (templ);
7720 if (outer)
7721 outer = generic_targs_for (outer);
7722 else if (current_template_parms)
7723 {
7724 /* This is an argument of the current template, so we haven't set
7725 DECL_CONTEXT yet. */
7726 tree relevant_template_parms;
7727
7728 /* Parameter levels that are greater than the level of the given
7729 template template parm are irrelevant. */
7730 relevant_template_parms = current_template_parms;
7731 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7732 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7733 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7734
7735 outer = template_parms_to_args (relevant_template_parms);
7736 }
7737
7738 if (outer)
7739 arglist = add_to_template_args (outer, arglist);
7740
7741 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7742 return coerce_template_parms (parmlist, arglist, templ,
7743 complain,
7744 /*require_all_args=*/true,
7745 /*use_default_args=*/true);
7746 }
7747
7748 /* A cache of template template parameters with match-all default
7749 arguments. */
7750 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7751
7752 /* T is a bound template template-parameter. Copy its arguments into default
7753 arguments of the template template-parameter's template parameters. */
7754
7755 static tree
7756 add_defaults_to_ttp (tree otmpl)
7757 {
7758 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7759 return *c;
7760
7761 tree ntmpl = copy_node (otmpl);
7762
7763 tree ntype = copy_node (TREE_TYPE (otmpl));
7764 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7765 TYPE_MAIN_VARIANT (ntype) = ntype;
7766 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7767 TYPE_NAME (ntype) = ntmpl;
7768 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7769
7770 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7771 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7772 TEMPLATE_PARM_DECL (idx) = ntmpl;
7773 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7774
7775 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7776 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7777 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7778 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7779 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7780 {
7781 tree o = TREE_VEC_ELT (vec, i);
7782 if (!template_parameter_pack_p (TREE_VALUE (o)))
7783 {
7784 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7785 TREE_PURPOSE (n) = any_targ_node;
7786 }
7787 }
7788
7789 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7790 return ntmpl;
7791 }
7792
7793 /* ARG is a bound potential template template-argument, and PARGS is a list
7794 of arguments for the corresponding template template-parameter. Adjust
7795 PARGS as appropriate for application to ARG's template, and if ARG is a
7796 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7797 arguments to the template template parameter. */
7798
7799 static tree
7800 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7801 {
7802 ++processing_template_decl;
7803 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7804 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7805 {
7806 /* When comparing two template template-parameters in partial ordering,
7807 rewrite the one currently being used as an argument to have default
7808 arguments for all parameters. */
7809 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7810 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7811 if (pargs != error_mark_node)
7812 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7813 TYPE_TI_ARGS (arg));
7814 }
7815 else
7816 {
7817 tree aparms
7818 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7819 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7820 /*require_all*/true,
7821 /*use_default*/true);
7822 }
7823 --processing_template_decl;
7824 return pargs;
7825 }
7826
7827 /* Subroutine of unify for the case when PARM is a
7828 BOUND_TEMPLATE_TEMPLATE_PARM. */
7829
7830 static int
7831 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7832 bool explain_p)
7833 {
7834 tree parmvec = TYPE_TI_ARGS (parm);
7835 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7836
7837 /* The template template parm might be variadic and the argument
7838 not, so flatten both argument lists. */
7839 parmvec = expand_template_argument_pack (parmvec);
7840 argvec = expand_template_argument_pack (argvec);
7841
7842 if (flag_new_ttp)
7843 {
7844 /* In keeping with P0522R0, adjust P's template arguments
7845 to apply to A's template; then flatten it again. */
7846 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7847 nparmvec = expand_template_argument_pack (nparmvec);
7848
7849 if (unify (tparms, targs, nparmvec, argvec,
7850 UNIFY_ALLOW_NONE, explain_p))
7851 return 1;
7852
7853 /* If the P0522 adjustment eliminated a pack expansion, deduce
7854 empty packs. */
7855 if (flag_new_ttp
7856 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7857 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7858 DEDUCE_EXACT, /*sub*/true, explain_p))
7859 return 1;
7860 }
7861 else
7862 {
7863 /* Deduce arguments T, i from TT<T> or TT<i>.
7864 We check each element of PARMVEC and ARGVEC individually
7865 rather than the whole TREE_VEC since they can have
7866 different number of elements, which is allowed under N2555. */
7867
7868 int len = TREE_VEC_LENGTH (parmvec);
7869
7870 /* Check if the parameters end in a pack, making them
7871 variadic. */
7872 int parm_variadic_p = 0;
7873 if (len > 0
7874 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7875 parm_variadic_p = 1;
7876
7877 for (int i = 0; i < len - parm_variadic_p; ++i)
7878 /* If the template argument list of P contains a pack
7879 expansion that is not the last template argument, the
7880 entire template argument list is a non-deduced
7881 context. */
7882 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7883 return unify_success (explain_p);
7884
7885 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7886 return unify_too_few_arguments (explain_p,
7887 TREE_VEC_LENGTH (argvec), len);
7888
7889 for (int i = 0; i < len - parm_variadic_p; ++i)
7890 if (unify (tparms, targs,
7891 TREE_VEC_ELT (parmvec, i),
7892 TREE_VEC_ELT (argvec, i),
7893 UNIFY_ALLOW_NONE, explain_p))
7894 return 1;
7895
7896 if (parm_variadic_p
7897 && unify_pack_expansion (tparms, targs,
7898 parmvec, argvec,
7899 DEDUCE_EXACT,
7900 /*subr=*/true, explain_p))
7901 return 1;
7902 }
7903
7904 return 0;
7905 }
7906
7907 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7908 template template parameters. Both PARM_PARMS and ARG_PARMS are
7909 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7910 or PARM_DECL.
7911
7912 Consider the example:
7913 template <class T> class A;
7914 template<template <class U> class TT> class B;
7915
7916 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7917 the parameters to A, and OUTER_ARGS contains A. */
7918
7919 static int
7920 coerce_template_template_parms (tree parm_parms,
7921 tree arg_parms,
7922 tsubst_flags_t complain,
7923 tree in_decl,
7924 tree outer_args)
7925 {
7926 int nparms, nargs, i;
7927 tree parm, arg;
7928 int variadic_p = 0;
7929
7930 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7931 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7932
7933 nparms = TREE_VEC_LENGTH (parm_parms);
7934 nargs = TREE_VEC_LENGTH (arg_parms);
7935
7936 if (flag_new_ttp)
7937 {
7938 /* P0522R0: A template template-parameter P is at least as specialized as
7939 a template template-argument A if, given the following rewrite to two
7940 function templates, the function template corresponding to P is at
7941 least as specialized as the function template corresponding to A
7942 according to the partial ordering rules for function templates
7943 ([temp.func.order]). Given an invented class template X with the
7944 template parameter list of A (including default arguments):
7945
7946 * Each of the two function templates has the same template parameters,
7947 respectively, as P or A.
7948
7949 * Each function template has a single function parameter whose type is
7950 a specialization of X with template arguments corresponding to the
7951 template parameters from the respective function template where, for
7952 each template parameter PP in the template parameter list of the
7953 function template, a corresponding template argument AA is formed. If
7954 PP declares a parameter pack, then AA is the pack expansion
7955 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7956
7957 If the rewrite produces an invalid type, then P is not at least as
7958 specialized as A. */
7959
7960 /* So coerce P's args to apply to A's parms, and then deduce between A's
7961 args and the converted args. If that succeeds, A is at least as
7962 specialized as P, so they match.*/
7963 tree pargs = template_parms_level_to_args (parm_parms);
7964 pargs = add_outermost_template_args (outer_args, pargs);
7965 ++processing_template_decl;
7966 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7967 /*require_all*/true, /*use_default*/true);
7968 --processing_template_decl;
7969 if (pargs != error_mark_node)
7970 {
7971 tree targs = make_tree_vec (nargs);
7972 tree aargs = template_parms_level_to_args (arg_parms);
7973 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7974 /*explain*/false))
7975 return 1;
7976 }
7977 }
7978
7979 /* Determine whether we have a parameter pack at the end of the
7980 template template parameter's template parameter list. */
7981 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7982 {
7983 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7984
7985 if (error_operand_p (parm))
7986 return 0;
7987
7988 switch (TREE_CODE (parm))
7989 {
7990 case TEMPLATE_DECL:
7991 case TYPE_DECL:
7992 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7993 variadic_p = 1;
7994 break;
7995
7996 case PARM_DECL:
7997 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7998 variadic_p = 1;
7999 break;
8000
8001 default:
8002 gcc_unreachable ();
8003 }
8004 }
8005
8006 if (nargs != nparms
8007 && !(variadic_p && nargs >= nparms - 1))
8008 return 0;
8009
8010 /* Check all of the template parameters except the parameter pack at
8011 the end (if any). */
8012 for (i = 0; i < nparms - variadic_p; ++i)
8013 {
8014 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8015 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8016 continue;
8017
8018 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8019 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8020
8021 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8022 outer_args))
8023 return 0;
8024
8025 }
8026
8027 if (variadic_p)
8028 {
8029 /* Check each of the template parameters in the template
8030 argument against the template parameter pack at the end of
8031 the template template parameter. */
8032 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8033 return 0;
8034
8035 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8036
8037 for (; i < nargs; ++i)
8038 {
8039 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8040 continue;
8041
8042 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8043
8044 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8045 outer_args))
8046 return 0;
8047 }
8048 }
8049
8050 return 1;
8051 }
8052
8053 /* Verifies that the deduced template arguments (in TARGS) for the
8054 template template parameters (in TPARMS) represent valid bindings,
8055 by comparing the template parameter list of each template argument
8056 to the template parameter list of its corresponding template
8057 template parameter, in accordance with DR150. This
8058 routine can only be called after all template arguments have been
8059 deduced. It will return TRUE if all of the template template
8060 parameter bindings are okay, FALSE otherwise. */
8061 bool
8062 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8063 {
8064 int i, ntparms = TREE_VEC_LENGTH (tparms);
8065 bool ret = true;
8066
8067 /* We're dealing with template parms in this process. */
8068 ++processing_template_decl;
8069
8070 targs = INNERMOST_TEMPLATE_ARGS (targs);
8071
8072 for (i = 0; i < ntparms; ++i)
8073 {
8074 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8075 tree targ = TREE_VEC_ELT (targs, i);
8076
8077 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8078 {
8079 tree packed_args = NULL_TREE;
8080 int idx, len = 1;
8081
8082 if (ARGUMENT_PACK_P (targ))
8083 {
8084 /* Look inside the argument pack. */
8085 packed_args = ARGUMENT_PACK_ARGS (targ);
8086 len = TREE_VEC_LENGTH (packed_args);
8087 }
8088
8089 for (idx = 0; idx < len; ++idx)
8090 {
8091 tree targ_parms = NULL_TREE;
8092
8093 if (packed_args)
8094 /* Extract the next argument from the argument
8095 pack. */
8096 targ = TREE_VEC_ELT (packed_args, idx);
8097
8098 if (PACK_EXPANSION_P (targ))
8099 /* Look at the pattern of the pack expansion. */
8100 targ = PACK_EXPANSION_PATTERN (targ);
8101
8102 /* Extract the template parameters from the template
8103 argument. */
8104 if (TREE_CODE (targ) == TEMPLATE_DECL)
8105 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8106 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8107 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8108
8109 /* Verify that we can coerce the template template
8110 parameters from the template argument to the template
8111 parameter. This requires an exact match. */
8112 if (targ_parms
8113 && !coerce_template_template_parms
8114 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8115 targ_parms,
8116 tf_none,
8117 tparm,
8118 targs))
8119 {
8120 ret = false;
8121 goto out;
8122 }
8123 }
8124 }
8125 }
8126
8127 out:
8128
8129 --processing_template_decl;
8130 return ret;
8131 }
8132
8133 /* Since type attributes aren't mangled, we need to strip them from
8134 template type arguments. */
8135
8136 tree
8137 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8138 {
8139 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8140 return arg;
8141 bool removed_attributes = false;
8142 tree canon = strip_typedefs (arg, &removed_attributes);
8143 if (removed_attributes
8144 && (complain & tf_warning))
8145 warning (OPT_Wignored_attributes,
8146 "ignoring attributes on template argument %qT", arg);
8147 return canon;
8148 }
8149
8150 /* And from inside dependent non-type arguments like sizeof(Type). */
8151
8152 static tree
8153 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8154 {
8155 if (!arg || arg == error_mark_node)
8156 return arg;
8157 bool removed_attributes = false;
8158 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8159 if (removed_attributes
8160 && (complain & tf_warning))
8161 warning (OPT_Wignored_attributes,
8162 "ignoring attributes in template argument %qE", arg);
8163 return canon;
8164 }
8165
8166 /* A template declaration can be substituted for a constrained
8167 template template parameter only when the argument is no more
8168 constrained than the parameter. */
8169
8170 static bool
8171 is_compatible_template_arg (tree parm, tree arg)
8172 {
8173 tree parm_cons = get_constraints (parm);
8174
8175 /* For now, allow constrained template template arguments
8176 and unconstrained template template parameters. */
8177 if (parm_cons == NULL_TREE)
8178 return true;
8179
8180 /* If the template parameter is constrained, we need to rewrite its
8181 constraints in terms of the ARG's template parameters. This ensures
8182 that all of the template parameter types will have the same depth.
8183
8184 Note that this is only valid when coerce_template_template_parm is
8185 true for the innermost template parameters of PARM and ARG. In other
8186 words, because coercion is successful, this conversion will be valid. */
8187 tree new_args = NULL_TREE;
8188 if (parm_cons)
8189 {
8190 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8191 new_args = template_parms_level_to_args (aparms);
8192 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8193 tf_none, NULL_TREE);
8194 if (parm_cons == error_mark_node)
8195 return false;
8196 }
8197
8198 return weakly_subsumes (parm_cons, arg);
8199 }
8200
8201 // Convert a placeholder argument into a binding to the original
8202 // parameter. The original parameter is saved as the TREE_TYPE of
8203 // ARG.
8204 static inline tree
8205 convert_wildcard_argument (tree parm, tree arg)
8206 {
8207 TREE_TYPE (arg) = parm;
8208 return arg;
8209 }
8210
8211 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8212 because one of them is dependent. But we need to represent the
8213 conversion for the benefit of cp_tree_equal. */
8214
8215 static tree
8216 maybe_convert_nontype_argument (tree type, tree arg)
8217 {
8218 /* Auto parms get no conversion. */
8219 if (type_uses_auto (type))
8220 return arg;
8221 /* We don't need or want to add this conversion now if we're going to use the
8222 argument for deduction. */
8223 if (value_dependent_expression_p (arg))
8224 return arg;
8225
8226 type = cv_unqualified (type);
8227 tree argtype = TREE_TYPE (arg);
8228 if (same_type_p (type, argtype))
8229 return arg;
8230
8231 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8232 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8233 return arg;
8234 }
8235
8236 /* Convert the indicated template ARG as necessary to match the
8237 indicated template PARM. Returns the converted ARG, or
8238 error_mark_node if the conversion was unsuccessful. Error and
8239 warning messages are issued under control of COMPLAIN. This
8240 conversion is for the Ith parameter in the parameter list. ARGS is
8241 the full set of template arguments deduced so far. */
8242
8243 static tree
8244 convert_template_argument (tree parm,
8245 tree arg,
8246 tree args,
8247 tsubst_flags_t complain,
8248 int i,
8249 tree in_decl)
8250 {
8251 tree orig_arg;
8252 tree val;
8253 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8254
8255 if (parm == error_mark_node || error_operand_p (arg))
8256 return error_mark_node;
8257
8258 /* Trivially convert placeholders. */
8259 if (TREE_CODE (arg) == WILDCARD_DECL)
8260 return convert_wildcard_argument (parm, arg);
8261
8262 if (arg == any_targ_node)
8263 return arg;
8264
8265 if (TREE_CODE (arg) == TREE_LIST
8266 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8267 {
8268 /* The template argument was the name of some
8269 member function. That's usually
8270 invalid, but static members are OK. In any
8271 case, grab the underlying fields/functions
8272 and issue an error later if required. */
8273 TREE_TYPE (arg) = unknown_type_node;
8274 }
8275
8276 orig_arg = arg;
8277
8278 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8279 requires_type = (TREE_CODE (parm) == TYPE_DECL
8280 || requires_tmpl_type);
8281
8282 /* When determining whether an argument pack expansion is a template,
8283 look at the pattern. */
8284 if (PACK_EXPANSION_P (arg))
8285 arg = PACK_EXPANSION_PATTERN (arg);
8286
8287 /* Deal with an injected-class-name used as a template template arg. */
8288 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8289 {
8290 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8291 if (TREE_CODE (t) == TEMPLATE_DECL)
8292 {
8293 if (cxx_dialect >= cxx11)
8294 /* OK under DR 1004. */;
8295 else if (complain & tf_warning_or_error)
8296 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8297 " used as template template argument", TYPE_NAME (arg));
8298 else if (flag_pedantic_errors)
8299 t = arg;
8300
8301 arg = t;
8302 }
8303 }
8304
8305 is_tmpl_type =
8306 ((TREE_CODE (arg) == TEMPLATE_DECL
8307 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8308 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8309 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8310 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8311
8312 if (is_tmpl_type
8313 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8314 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8315 arg = TYPE_STUB_DECL (arg);
8316
8317 is_type = TYPE_P (arg) || is_tmpl_type;
8318
8319 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8320 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8321 {
8322 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8323 {
8324 if (complain & tf_error)
8325 error ("invalid use of destructor %qE as a type", orig_arg);
8326 return error_mark_node;
8327 }
8328
8329 permerror (input_location,
8330 "to refer to a type member of a template parameter, "
8331 "use %<typename %E%>", orig_arg);
8332
8333 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8334 TREE_OPERAND (arg, 1),
8335 typename_type,
8336 complain);
8337 arg = orig_arg;
8338 is_type = 1;
8339 }
8340 if (is_type != requires_type)
8341 {
8342 if (in_decl)
8343 {
8344 if (complain & tf_error)
8345 {
8346 error ("type/value mismatch at argument %d in template "
8347 "parameter list for %qD",
8348 i + 1, in_decl);
8349 if (is_type)
8350 {
8351 /* The template argument is a type, but we're expecting
8352 an expression. */
8353 inform (input_location,
8354 " expected a constant of type %qT, got %qT",
8355 TREE_TYPE (parm),
8356 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8357 /* [temp.arg]/2: "In a template-argument, an ambiguity
8358 between a type-id and an expression is resolved to a
8359 type-id, regardless of the form of the corresponding
8360 template-parameter." So give the user a clue. */
8361 if (TREE_CODE (arg) == FUNCTION_TYPE)
8362 inform (input_location, " ambiguous template argument "
8363 "for non-type template parameter is treated as "
8364 "function type");
8365 }
8366 else if (requires_tmpl_type)
8367 inform (input_location,
8368 " expected a class template, got %qE", orig_arg);
8369 else
8370 inform (input_location,
8371 " expected a type, got %qE", orig_arg);
8372 }
8373 }
8374 return error_mark_node;
8375 }
8376 if (is_tmpl_type ^ requires_tmpl_type)
8377 {
8378 if (in_decl && (complain & tf_error))
8379 {
8380 error ("type/value mismatch at argument %d in template "
8381 "parameter list for %qD",
8382 i + 1, in_decl);
8383 if (is_tmpl_type)
8384 inform (input_location,
8385 " expected a type, got %qT", DECL_NAME (arg));
8386 else
8387 inform (input_location,
8388 " expected a class template, got %qT", orig_arg);
8389 }
8390 return error_mark_node;
8391 }
8392
8393 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8394 /* We already did the appropriate conversion when packing args. */
8395 val = orig_arg;
8396 else if (is_type)
8397 {
8398 if (requires_tmpl_type)
8399 {
8400 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8401 /* The number of argument required is not known yet.
8402 Just accept it for now. */
8403 val = orig_arg;
8404 else
8405 {
8406 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8407 tree argparm;
8408
8409 /* Strip alias templates that are equivalent to another
8410 template. */
8411 arg = get_underlying_template (arg);
8412 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8413
8414 if (coerce_template_template_parms (parmparm, argparm,
8415 complain, in_decl,
8416 args))
8417 {
8418 val = arg;
8419
8420 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8421 TEMPLATE_DECL. */
8422 if (val != error_mark_node)
8423 {
8424 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8425 val = TREE_TYPE (val);
8426 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8427 val = make_pack_expansion (val, complain);
8428 }
8429 }
8430 else
8431 {
8432 if (in_decl && (complain & tf_error))
8433 {
8434 error ("type/value mismatch at argument %d in "
8435 "template parameter list for %qD",
8436 i + 1, in_decl);
8437 inform (input_location,
8438 " expected a template of type %qD, got %qT",
8439 parm, orig_arg);
8440 }
8441
8442 val = error_mark_node;
8443 }
8444
8445 // Check that the constraints are compatible before allowing the
8446 // substitution.
8447 if (val != error_mark_node)
8448 if (!is_compatible_template_arg (parm, arg))
8449 {
8450 if (in_decl && (complain & tf_error))
8451 {
8452 error ("constraint mismatch at argument %d in "
8453 "template parameter list for %qD",
8454 i + 1, in_decl);
8455 inform (input_location, " expected %qD but got %qD",
8456 parm, arg);
8457 }
8458 val = error_mark_node;
8459 }
8460 }
8461 }
8462 else
8463 val = orig_arg;
8464 /* We only form one instance of each template specialization.
8465 Therefore, if we use a non-canonical variant (i.e., a
8466 typedef), any future messages referring to the type will use
8467 the typedef, which is confusing if those future uses do not
8468 themselves also use the typedef. */
8469 if (TYPE_P (val))
8470 val = canonicalize_type_argument (val, complain);
8471 }
8472 else
8473 {
8474 tree t = TREE_TYPE (parm);
8475
8476 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8477 > TMPL_ARGS_DEPTH (args))
8478 /* We don't have enough levels of args to do any substitution. This
8479 can happen in the context of -fnew-ttp-matching. */;
8480 else if (tree a = type_uses_auto (t))
8481 {
8482 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8483 if (t == error_mark_node)
8484 return error_mark_node;
8485 }
8486 else
8487 t = tsubst (t, args, complain, in_decl);
8488
8489 if (invalid_nontype_parm_type_p (t, complain))
8490 return error_mark_node;
8491
8492 if (t != TREE_TYPE (parm))
8493 t = canonicalize_type_argument (t, complain);
8494
8495 if (!type_dependent_expression_p (orig_arg)
8496 && !uses_template_parms (t))
8497 /* We used to call digest_init here. However, digest_init
8498 will report errors, which we don't want when complain
8499 is zero. More importantly, digest_init will try too
8500 hard to convert things: for example, `0' should not be
8501 converted to pointer type at this point according to
8502 the standard. Accepting this is not merely an
8503 extension, since deciding whether or not these
8504 conversions can occur is part of determining which
8505 function template to call, or whether a given explicit
8506 argument specification is valid. */
8507 val = convert_nontype_argument (t, orig_arg, complain);
8508 else
8509 {
8510 val = canonicalize_expr_argument (orig_arg, complain);
8511 val = maybe_convert_nontype_argument (t, val);
8512 }
8513
8514
8515 if (val == NULL_TREE)
8516 val = error_mark_node;
8517 else if (val == error_mark_node && (complain & tf_error))
8518 error_at (cp_expr_loc_or_input_loc (orig_arg),
8519 "could not convert template argument %qE from %qT to %qT",
8520 orig_arg, TREE_TYPE (orig_arg), t);
8521
8522 if (INDIRECT_REF_P (val))
8523 {
8524 /* Reject template arguments that are references to built-in
8525 functions with no library fallbacks. */
8526 const_tree inner = TREE_OPERAND (val, 0);
8527 const_tree innertype = TREE_TYPE (inner);
8528 if (innertype
8529 && TYPE_REF_P (innertype)
8530 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8531 && TREE_OPERAND_LENGTH (inner) > 0
8532 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8533 return error_mark_node;
8534 }
8535
8536 if (TREE_CODE (val) == SCOPE_REF)
8537 {
8538 /* Strip typedefs from the SCOPE_REF. */
8539 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8540 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8541 complain);
8542 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8543 QUALIFIED_NAME_IS_TEMPLATE (val));
8544 }
8545 }
8546
8547 return val;
8548 }
8549
8550 /* Coerces the remaining template arguments in INNER_ARGS (from
8551 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8552 Returns the coerced argument pack. PARM_IDX is the position of this
8553 parameter in the template parameter list. ARGS is the original
8554 template argument list. */
8555 static tree
8556 coerce_template_parameter_pack (tree parms,
8557 int parm_idx,
8558 tree args,
8559 tree inner_args,
8560 int arg_idx,
8561 tree new_args,
8562 int* lost,
8563 tree in_decl,
8564 tsubst_flags_t complain)
8565 {
8566 tree parm = TREE_VEC_ELT (parms, parm_idx);
8567 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8568 tree packed_args;
8569 tree argument_pack;
8570 tree packed_parms = NULL_TREE;
8571
8572 if (arg_idx > nargs)
8573 arg_idx = nargs;
8574
8575 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8576 {
8577 /* When the template parameter is a non-type template parameter pack
8578 or template template parameter pack whose type or template
8579 parameters use parameter packs, we know exactly how many arguments
8580 we are looking for. Build a vector of the instantiated decls for
8581 these template parameters in PACKED_PARMS. */
8582 /* We can't use make_pack_expansion here because it would interpret a
8583 _DECL as a use rather than a declaration. */
8584 tree decl = TREE_VALUE (parm);
8585 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8586 SET_PACK_EXPANSION_PATTERN (exp, decl);
8587 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8588 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8589
8590 TREE_VEC_LENGTH (args)--;
8591 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8592 TREE_VEC_LENGTH (args)++;
8593
8594 if (packed_parms == error_mark_node)
8595 return error_mark_node;
8596
8597 /* If we're doing a partial instantiation of a member template,
8598 verify that all of the types used for the non-type
8599 template parameter pack are, in fact, valid for non-type
8600 template parameters. */
8601 if (arg_idx < nargs
8602 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8603 {
8604 int j, len = TREE_VEC_LENGTH (packed_parms);
8605 for (j = 0; j < len; ++j)
8606 {
8607 tree t = TREE_VEC_ELT (packed_parms, j);
8608 if (TREE_CODE (t) == PARM_DECL
8609 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8610 return error_mark_node;
8611 }
8612 /* We don't know how many args we have yet, just
8613 use the unconverted ones for now. */
8614 return NULL_TREE;
8615 }
8616
8617 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8618 }
8619 /* Check if we have a placeholder pack, which indicates we're
8620 in the context of a introduction list. In that case we want
8621 to match this pack to the single placeholder. */
8622 else if (arg_idx < nargs
8623 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8624 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8625 {
8626 nargs = arg_idx + 1;
8627 packed_args = make_tree_vec (1);
8628 }
8629 else
8630 packed_args = make_tree_vec (nargs - arg_idx);
8631
8632 /* Convert the remaining arguments, which will be a part of the
8633 parameter pack "parm". */
8634 int first_pack_arg = arg_idx;
8635 for (; arg_idx < nargs; ++arg_idx)
8636 {
8637 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8638 tree actual_parm = TREE_VALUE (parm);
8639 int pack_idx = arg_idx - first_pack_arg;
8640
8641 if (packed_parms)
8642 {
8643 /* Once we've packed as many args as we have types, stop. */
8644 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8645 break;
8646 else if (PACK_EXPANSION_P (arg))
8647 /* We don't know how many args we have yet, just
8648 use the unconverted ones for now. */
8649 return NULL_TREE;
8650 else
8651 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8652 }
8653
8654 if (arg == error_mark_node)
8655 {
8656 if (complain & tf_error)
8657 error ("template argument %d is invalid", arg_idx + 1);
8658 }
8659 else
8660 arg = convert_template_argument (actual_parm,
8661 arg, new_args, complain, parm_idx,
8662 in_decl);
8663 if (arg == error_mark_node)
8664 (*lost)++;
8665 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8666 }
8667
8668 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8669 && TREE_VEC_LENGTH (packed_args) > 0)
8670 {
8671 if (complain & tf_error)
8672 error ("wrong number of template arguments (%d, should be %d)",
8673 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8674 return error_mark_node;
8675 }
8676
8677 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8678 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8679 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8680 else
8681 {
8682 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8683 TREE_CONSTANT (argument_pack) = 1;
8684 }
8685
8686 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8687 if (CHECKING_P)
8688 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8689 TREE_VEC_LENGTH (packed_args));
8690 return argument_pack;
8691 }
8692
8693 /* Returns the number of pack expansions in the template argument vector
8694 ARGS. */
8695
8696 static int
8697 pack_expansion_args_count (tree args)
8698 {
8699 int i;
8700 int count = 0;
8701 if (args)
8702 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8703 {
8704 tree elt = TREE_VEC_ELT (args, i);
8705 if (elt && PACK_EXPANSION_P (elt))
8706 ++count;
8707 }
8708 return count;
8709 }
8710
8711 /* Convert all template arguments to their appropriate types, and
8712 return a vector containing the innermost resulting template
8713 arguments. If any error occurs, return error_mark_node. Error and
8714 warning messages are issued under control of COMPLAIN.
8715
8716 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8717 for arguments not specified in ARGS. Otherwise, if
8718 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8719 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8720 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8721 ARGS. */
8722
8723 static tree
8724 coerce_template_parms (tree parms,
8725 tree args,
8726 tree in_decl,
8727 tsubst_flags_t complain,
8728 bool require_all_args,
8729 bool use_default_args)
8730 {
8731 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8732 tree orig_inner_args;
8733 tree inner_args;
8734 tree new_args;
8735 tree new_inner_args;
8736
8737 /* When used as a boolean value, indicates whether this is a
8738 variadic template parameter list. Since it's an int, we can also
8739 subtract it from nparms to get the number of non-variadic
8740 parameters. */
8741 int variadic_p = 0;
8742 int variadic_args_p = 0;
8743 int post_variadic_parms = 0;
8744
8745 /* Adjustment to nparms for fixed parameter packs. */
8746 int fixed_pack_adjust = 0;
8747 int fixed_packs = 0;
8748 int missing = 0;
8749
8750 /* Likewise for parameters with default arguments. */
8751 int default_p = 0;
8752
8753 if (args == error_mark_node)
8754 return error_mark_node;
8755
8756 nparms = TREE_VEC_LENGTH (parms);
8757
8758 /* Determine if there are any parameter packs or default arguments. */
8759 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8760 {
8761 tree parm = TREE_VEC_ELT (parms, parm_idx);
8762 if (variadic_p)
8763 ++post_variadic_parms;
8764 if (template_parameter_pack_p (TREE_VALUE (parm)))
8765 ++variadic_p;
8766 if (TREE_PURPOSE (parm))
8767 ++default_p;
8768 }
8769
8770 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8771 /* If there are no parameters that follow a parameter pack, we need to
8772 expand any argument packs so that we can deduce a parameter pack from
8773 some non-packed args followed by an argument pack, as in variadic85.C.
8774 If there are such parameters, we need to leave argument packs intact
8775 so the arguments are assigned properly. This can happen when dealing
8776 with a nested class inside a partial specialization of a class
8777 template, as in variadic92.C, or when deducing a template parameter pack
8778 from a sub-declarator, as in variadic114.C. */
8779 if (!post_variadic_parms)
8780 inner_args = expand_template_argument_pack (inner_args);
8781
8782 /* Count any pack expansion args. */
8783 variadic_args_p = pack_expansion_args_count (inner_args);
8784
8785 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8786 if ((nargs - variadic_args_p > nparms && !variadic_p)
8787 || (nargs < nparms - variadic_p
8788 && require_all_args
8789 && !variadic_args_p
8790 && (!use_default_args
8791 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8792 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8793 {
8794 bad_nargs:
8795 if (complain & tf_error)
8796 {
8797 if (variadic_p || default_p)
8798 {
8799 nparms -= variadic_p + default_p;
8800 error ("wrong number of template arguments "
8801 "(%d, should be at least %d)", nargs, nparms);
8802 }
8803 else
8804 error ("wrong number of template arguments "
8805 "(%d, should be %d)", nargs, nparms);
8806
8807 if (in_decl)
8808 inform (DECL_SOURCE_LOCATION (in_decl),
8809 "provided for %qD", in_decl);
8810 }
8811
8812 return error_mark_node;
8813 }
8814 /* We can't pass a pack expansion to a non-pack parameter of an alias
8815 template (DR 1430). */
8816 else if (in_decl
8817 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8818 || concept_definition_p (in_decl))
8819 && variadic_args_p
8820 && nargs - variadic_args_p < nparms - variadic_p)
8821 {
8822 if (complain & tf_error)
8823 {
8824 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8825 {
8826 tree arg = TREE_VEC_ELT (inner_args, i);
8827 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8828
8829 if (PACK_EXPANSION_P (arg)
8830 && !template_parameter_pack_p (parm))
8831 {
8832 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8833 error_at (location_of (arg),
8834 "pack expansion argument for non-pack parameter "
8835 "%qD of alias template %qD", parm, in_decl);
8836 else
8837 error_at (location_of (arg),
8838 "pack expansion argument for non-pack parameter "
8839 "%qD of concept %qD", parm, in_decl);
8840 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8841 goto found;
8842 }
8843 }
8844 gcc_unreachable ();
8845 found:;
8846 }
8847 return error_mark_node;
8848 }
8849
8850 /* We need to evaluate the template arguments, even though this
8851 template-id may be nested within a "sizeof". */
8852 cp_evaluated ev;
8853
8854 new_inner_args = make_tree_vec (nparms);
8855 new_args = add_outermost_template_args (args, new_inner_args);
8856 int pack_adjust = 0;
8857 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8858 {
8859 tree arg;
8860 tree parm;
8861
8862 /* Get the Ith template parameter. */
8863 parm = TREE_VEC_ELT (parms, parm_idx);
8864
8865 if (parm == error_mark_node)
8866 {
8867 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8868 continue;
8869 }
8870
8871 /* Calculate the next argument. */
8872 if (arg_idx < nargs)
8873 arg = TREE_VEC_ELT (inner_args, arg_idx);
8874 else
8875 arg = NULL_TREE;
8876
8877 if (template_parameter_pack_p (TREE_VALUE (parm))
8878 && (arg || require_all_args || !(complain & tf_partial))
8879 && !(arg && ARGUMENT_PACK_P (arg)))
8880 {
8881 /* Some arguments will be placed in the
8882 template parameter pack PARM. */
8883 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8884 inner_args, arg_idx,
8885 new_args, &lost,
8886 in_decl, complain);
8887
8888 if (arg == NULL_TREE)
8889 {
8890 /* We don't know how many args we have yet, just use the
8891 unconverted (and still packed) ones for now. */
8892 new_inner_args = orig_inner_args;
8893 arg_idx = nargs;
8894 break;
8895 }
8896
8897 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8898
8899 /* Store this argument. */
8900 if (arg == error_mark_node)
8901 {
8902 lost++;
8903 /* We are done with all of the arguments. */
8904 arg_idx = nargs;
8905 break;
8906 }
8907 else
8908 {
8909 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8910 arg_idx += pack_adjust;
8911 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8912 {
8913 ++fixed_packs;
8914 fixed_pack_adjust += pack_adjust;
8915 }
8916 }
8917
8918 continue;
8919 }
8920 else if (arg)
8921 {
8922 if (PACK_EXPANSION_P (arg))
8923 {
8924 /* "If every valid specialization of a variadic template
8925 requires an empty template parameter pack, the template is
8926 ill-formed, no diagnostic required." So check that the
8927 pattern works with this parameter. */
8928 tree pattern = PACK_EXPANSION_PATTERN (arg);
8929 tree conv = convert_template_argument (TREE_VALUE (parm),
8930 pattern, new_args,
8931 complain, parm_idx,
8932 in_decl);
8933 if (conv == error_mark_node)
8934 {
8935 if (complain & tf_error)
8936 inform (input_location, "so any instantiation with a "
8937 "non-empty parameter pack would be ill-formed");
8938 ++lost;
8939 }
8940 else if (TYPE_P (conv) && !TYPE_P (pattern))
8941 /* Recover from missing typename. */
8942 TREE_VEC_ELT (inner_args, arg_idx)
8943 = make_pack_expansion (conv, complain);
8944
8945 /* We don't know how many args we have yet, just
8946 use the unconverted ones for now. */
8947 new_inner_args = inner_args;
8948 arg_idx = nargs;
8949 break;
8950 }
8951 }
8952 else if (require_all_args)
8953 {
8954 /* There must be a default arg in this case. */
8955 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8956 complain, in_decl);
8957 /* The position of the first default template argument,
8958 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8959 Record that. */
8960 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8961 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8962 arg_idx - pack_adjust);
8963 }
8964 else
8965 break;
8966
8967 if (arg == error_mark_node)
8968 {
8969 if (complain & tf_error)
8970 error ("template argument %d is invalid", arg_idx + 1);
8971 }
8972 else if (!arg)
8973 {
8974 /* This can occur if there was an error in the template
8975 parameter list itself (which we would already have
8976 reported) that we are trying to recover from, e.g., a class
8977 template with a parameter list such as
8978 template<typename..., typename> (cpp0x/variadic150.C). */
8979 ++lost;
8980
8981 /* This can also happen with a fixed parameter pack (71834). */
8982 if (arg_idx >= nargs)
8983 ++missing;
8984 }
8985 else
8986 arg = convert_template_argument (TREE_VALUE (parm),
8987 arg, new_args, complain,
8988 parm_idx, in_decl);
8989
8990 if (arg == error_mark_node)
8991 lost++;
8992
8993 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8994 }
8995
8996 if (missing || arg_idx < nargs - variadic_args_p)
8997 {
8998 /* If we had fixed parameter packs, we didn't know how many arguments we
8999 actually needed earlier; now we do. */
9000 nparms += fixed_pack_adjust;
9001 variadic_p -= fixed_packs;
9002 goto bad_nargs;
9003 }
9004
9005 if (arg_idx < nargs)
9006 {
9007 /* We had some pack expansion arguments that will only work if the packs
9008 are empty, but wait until instantiation time to complain.
9009 See variadic-ttp3.C. */
9010
9011 /* Except that we can't provide empty packs to alias templates or
9012 concepts when there are no corresponding parameters. Basically,
9013 we can get here with this:
9014
9015 template<typename T> concept C = true;
9016
9017 template<typename... Args>
9018 requires C<Args...>
9019 void f();
9020
9021 When parsing C<Args...>, we try to form a concept check of
9022 C<?, Args...>. Without the extra check for substituting an empty
9023 pack past the last parameter, we can accept the check as valid.
9024
9025 FIXME: This may be valid for alias templates (but I doubt it).
9026
9027 FIXME: The error could be better also. */
9028 if (in_decl && concept_definition_p (in_decl))
9029 {
9030 if (complain & tf_error)
9031 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9032 "too many arguments");
9033 return error_mark_node;
9034 }
9035
9036 int len = nparms + (nargs - arg_idx);
9037 tree args = make_tree_vec (len);
9038 int i = 0;
9039 for (; i < nparms; ++i)
9040 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9041 for (; i < len; ++i, ++arg_idx)
9042 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9043 arg_idx - pack_adjust);
9044 new_inner_args = args;
9045 }
9046
9047 if (lost)
9048 {
9049 gcc_assert (!(complain & tf_error) || seen_error ());
9050 return error_mark_node;
9051 }
9052
9053 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9054 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9055 TREE_VEC_LENGTH (new_inner_args));
9056
9057 return new_inner_args;
9058 }
9059
9060 /* Convert all template arguments to their appropriate types, and
9061 return a vector containing the innermost resulting template
9062 arguments. If any error occurs, return error_mark_node. Error and
9063 warning messages are not issued.
9064
9065 Note that no function argument deduction is performed, and default
9066 arguments are used to fill in unspecified arguments. */
9067 tree
9068 coerce_template_parms (tree parms, tree args, tree in_decl)
9069 {
9070 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9071 }
9072
9073 /* Convert all template arguments to their appropriate type, and
9074 instantiate default arguments as needed. This returns a vector
9075 containing the innermost resulting template arguments, or
9076 error_mark_node if unsuccessful. */
9077 tree
9078 coerce_template_parms (tree parms, tree args, tree in_decl,
9079 tsubst_flags_t complain)
9080 {
9081 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9082 }
9083
9084 /* Like coerce_template_parms. If PARMS represents all template
9085 parameters levels, this function returns a vector of vectors
9086 representing all the resulting argument levels. Note that in this
9087 case, only the innermost arguments are coerced because the
9088 outermost ones are supposed to have been coerced already.
9089
9090 Otherwise, if PARMS represents only (the innermost) vector of
9091 parameters, this function returns a vector containing just the
9092 innermost resulting arguments. */
9093
9094 static tree
9095 coerce_innermost_template_parms (tree parms,
9096 tree args,
9097 tree in_decl,
9098 tsubst_flags_t complain,
9099 bool require_all_args,
9100 bool use_default_args)
9101 {
9102 int parms_depth = TMPL_PARMS_DEPTH (parms);
9103 int args_depth = TMPL_ARGS_DEPTH (args);
9104 tree coerced_args;
9105
9106 if (parms_depth > 1)
9107 {
9108 coerced_args = make_tree_vec (parms_depth);
9109 tree level;
9110 int cur_depth;
9111
9112 for (level = parms, cur_depth = parms_depth;
9113 parms_depth > 0 && level != NULL_TREE;
9114 level = TREE_CHAIN (level), --cur_depth)
9115 {
9116 tree l;
9117 if (cur_depth == args_depth)
9118 l = coerce_template_parms (TREE_VALUE (level),
9119 args, in_decl, complain,
9120 require_all_args,
9121 use_default_args);
9122 else
9123 l = TMPL_ARGS_LEVEL (args, cur_depth);
9124
9125 if (l == error_mark_node)
9126 return error_mark_node;
9127
9128 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9129 }
9130 }
9131 else
9132 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9133 args, in_decl, complain,
9134 require_all_args,
9135 use_default_args);
9136 return coerced_args;
9137 }
9138
9139 /* Returns true if T is a wrapper to make a C++20 template parameter
9140 object const. */
9141
9142 static bool
9143 class_nttp_const_wrapper_p (tree t)
9144 {
9145 if (cxx_dialect < cxx20)
9146 return false;
9147 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9148 && CP_TYPE_CONST_P (TREE_TYPE (t))
9149 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9150 }
9151
9152 /* Returns 1 if template args OT and NT are equivalent. */
9153
9154 int
9155 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9156 {
9157 if (nt == ot)
9158 return 1;
9159 if (nt == NULL_TREE || ot == NULL_TREE)
9160 return false;
9161 if (nt == any_targ_node || ot == any_targ_node)
9162 return true;
9163
9164 if (class_nttp_const_wrapper_p (nt))
9165 nt = TREE_OPERAND (nt, 0);
9166 if (class_nttp_const_wrapper_p (ot))
9167 ot = TREE_OPERAND (ot, 0);
9168
9169 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9170 /* For member templates */
9171 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9172 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9173 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9174 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9175 PACK_EXPANSION_PATTERN (nt))
9176 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9177 PACK_EXPANSION_EXTRA_ARGS (nt)));
9178 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9179 return cp_tree_equal (ot, nt);
9180 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9181 gcc_unreachable ();
9182 else if (TYPE_P (nt) || TYPE_P (ot))
9183 {
9184 if (!(TYPE_P (nt) && TYPE_P (ot)))
9185 return false;
9186 /* Don't treat an alias template specialization with dependent
9187 arguments as equivalent to its underlying type when used as a
9188 template argument; we need them to be distinct so that we
9189 substitute into the specialization arguments at instantiation
9190 time. And aliases can't be equivalent without being ==, so
9191 we don't need to look any deeper.
9192
9193 During partial ordering, however, we need to treat them normally so
9194 that we can order uses of the same alias with different
9195 cv-qualification (79960). */
9196 if (!partial_order
9197 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9198 return false;
9199 else
9200 return same_type_p (ot, nt);
9201 }
9202 else
9203 {
9204 /* Try to treat a template non-type argument that has been converted
9205 to the parameter type as equivalent to one that hasn't yet. */
9206 for (enum tree_code code1 = TREE_CODE (ot);
9207 CONVERT_EXPR_CODE_P (code1)
9208 || code1 == NON_LVALUE_EXPR;
9209 code1 = TREE_CODE (ot))
9210 ot = TREE_OPERAND (ot, 0);
9211
9212 for (enum tree_code code2 = TREE_CODE (nt);
9213 CONVERT_EXPR_CODE_P (code2)
9214 || code2 == NON_LVALUE_EXPR;
9215 code2 = TREE_CODE (nt))
9216 nt = TREE_OPERAND (nt, 0);
9217
9218 return cp_tree_equal (ot, nt);
9219 }
9220 }
9221
9222 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9223 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9224 NEWARG_PTR with the offending arguments if they are non-NULL. */
9225
9226 int
9227 comp_template_args (tree oldargs, tree newargs,
9228 tree *oldarg_ptr, tree *newarg_ptr,
9229 bool partial_order)
9230 {
9231 int i;
9232
9233 if (oldargs == newargs)
9234 return 1;
9235
9236 if (!oldargs || !newargs)
9237 return 0;
9238
9239 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9240 return 0;
9241
9242 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9243 {
9244 tree nt = TREE_VEC_ELT (newargs, i);
9245 tree ot = TREE_VEC_ELT (oldargs, i);
9246
9247 if (! template_args_equal (ot, nt, partial_order))
9248 {
9249 if (oldarg_ptr != NULL)
9250 *oldarg_ptr = ot;
9251 if (newarg_ptr != NULL)
9252 *newarg_ptr = nt;
9253 return 0;
9254 }
9255 }
9256 return 1;
9257 }
9258
9259 inline bool
9260 comp_template_args_porder (tree oargs, tree nargs)
9261 {
9262 return comp_template_args (oargs, nargs, NULL, NULL, true);
9263 }
9264
9265 /* Implement a freelist interface for objects of type T.
9266
9267 Head is a separate object, rather than a regular member, so that we
9268 can define it as a GTY deletable pointer, which is highly
9269 desirable. A data member could be declared that way, but then the
9270 containing object would implicitly get GTY((user)), which would
9271 prevent us from instantiating freelists as global objects.
9272 Although this way we can create freelist global objects, they're
9273 such thin wrappers that instantiating temporaries at every use
9274 loses nothing and saves permanent storage for the freelist object.
9275
9276 Member functions next, anew, poison and reinit have default
9277 implementations that work for most of the types we're interested
9278 in, but if they don't work for some type, they should be explicitly
9279 specialized. See the comments before them for requirements, and
9280 the example specializations for the tree_list_freelist. */
9281 template <typename T>
9282 class freelist
9283 {
9284 /* Return the next object in a chain. We could just do type
9285 punning, but if we access the object with its underlying type, we
9286 avoid strict-aliasing trouble. This needs only work between
9287 poison and reinit. */
9288 static T *&next (T *obj) { return obj->next; }
9289
9290 /* Return a newly allocated, uninitialized or minimally-initialized
9291 object of type T. Any initialization performed by anew should
9292 either remain across the life of the object and the execution of
9293 poison, or be redone by reinit. */
9294 static T *anew () { return ggc_alloc<T> (); }
9295
9296 /* Optionally scribble all over the bits holding the object, so that
9297 they become (mostly?) uninitialized memory. This is called while
9298 preparing to make the object part of the free list. */
9299 static void poison (T *obj) {
9300 T *p ATTRIBUTE_UNUSED = obj;
9301 T **q ATTRIBUTE_UNUSED = &next (obj);
9302
9303 #ifdef ENABLE_GC_CHECKING
9304 /* Poison the data, to indicate the data is garbage. */
9305 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9306 memset (p, 0xa5, sizeof (*p));
9307 #endif
9308 /* Let valgrind know the object is free. */
9309 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9310
9311 /* Let valgrind know the next portion of the object is available,
9312 but uninitialized. */
9313 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9314 }
9315
9316 /* Bring an object that underwent at least one lifecycle after anew
9317 and before the most recent free and poison, back to a usable
9318 state, reinitializing whatever is needed for it to be
9319 functionally equivalent to an object just allocated and returned
9320 by anew. This may poison or clear the next field, used by
9321 freelist housekeeping after poison was called. */
9322 static void reinit (T *obj) {
9323 T **q ATTRIBUTE_UNUSED = &next (obj);
9324
9325 #ifdef ENABLE_GC_CHECKING
9326 memset (q, 0xa5, sizeof (*q));
9327 #endif
9328 /* Let valgrind know the entire object is available, but
9329 uninitialized. */
9330 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9331 }
9332
9333 /* Reference a GTY-deletable pointer that points to the first object
9334 in the free list proper. */
9335 T *&head;
9336 public:
9337 /* Construct a freelist object chaining objects off of HEAD. */
9338 freelist (T *&head) : head(head) {}
9339
9340 /* Add OBJ to the free object list. The former head becomes OBJ's
9341 successor. */
9342 void free (T *obj)
9343 {
9344 poison (obj);
9345 next (obj) = head;
9346 head = obj;
9347 }
9348
9349 /* Take an object from the free list, if one is available, or
9350 allocate a new one. Objects taken from the free list should be
9351 regarded as filled with garbage, except for bits that are
9352 configured to be preserved across free and alloc. */
9353 T *alloc ()
9354 {
9355 if (head)
9356 {
9357 T *obj = head;
9358 head = next (head);
9359 reinit (obj);
9360 return obj;
9361 }
9362 else
9363 return anew ();
9364 }
9365 };
9366
9367 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9368 want to allocate a TREE_LIST using the usual interface, and ensure
9369 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9370 build_tree_list logic in reinit, so this could go out of sync. */
9371 template <>
9372 inline tree &
9373 freelist<tree_node>::next (tree obj)
9374 {
9375 return TREE_CHAIN (obj);
9376 }
9377 template <>
9378 inline tree
9379 freelist<tree_node>::anew ()
9380 {
9381 return build_tree_list (NULL, NULL);
9382 }
9383 template <>
9384 inline void
9385 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9386 {
9387 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9388 tree p ATTRIBUTE_UNUSED = obj;
9389 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9390 tree *q ATTRIBUTE_UNUSED = &next (obj);
9391
9392 #ifdef ENABLE_GC_CHECKING
9393 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9394
9395 /* Poison the data, to indicate the data is garbage. */
9396 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9397 memset (p, 0xa5, size);
9398 #endif
9399 /* Let valgrind know the object is free. */
9400 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9401 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9402 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9403 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9404
9405 #ifdef ENABLE_GC_CHECKING
9406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9407 /* Keep TREE_CHAIN functional. */
9408 TREE_SET_CODE (obj, TREE_LIST);
9409 #else
9410 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9411 #endif
9412 }
9413 template <>
9414 inline void
9415 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9416 {
9417 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9418
9419 #ifdef ENABLE_GC_CHECKING
9420 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9421 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9422 memset (obj, 0, sizeof (tree_list));
9423 #endif
9424
9425 /* Let valgrind know the entire object is available, but
9426 uninitialized. */
9427 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9428
9429 #ifdef ENABLE_GC_CHECKING
9430 TREE_SET_CODE (obj, TREE_LIST);
9431 #else
9432 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9433 #endif
9434 }
9435
9436 /* Point to the first object in the TREE_LIST freelist. */
9437 static GTY((deletable)) tree tree_list_freelist_head;
9438 /* Return the/an actual TREE_LIST freelist. */
9439 static inline freelist<tree_node>
9440 tree_list_freelist ()
9441 {
9442 return tree_list_freelist_head;
9443 }
9444
9445 /* Point to the first object in the tinst_level freelist. */
9446 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9447 /* Return the/an actual tinst_level freelist. */
9448 static inline freelist<tinst_level>
9449 tinst_level_freelist ()
9450 {
9451 return tinst_level_freelist_head;
9452 }
9453
9454 /* Point to the first object in the pending_template freelist. */
9455 static GTY((deletable)) pending_template *pending_template_freelist_head;
9456 /* Return the/an actual pending_template freelist. */
9457 static inline freelist<pending_template>
9458 pending_template_freelist ()
9459 {
9460 return pending_template_freelist_head;
9461 }
9462
9463 /* Build the TREE_LIST object out of a split list, store it
9464 permanently, and return it. */
9465 tree
9466 tinst_level::to_list ()
9467 {
9468 gcc_assert (split_list_p ());
9469 tree ret = tree_list_freelist ().alloc ();
9470 TREE_PURPOSE (ret) = tldcl;
9471 TREE_VALUE (ret) = targs;
9472 tldcl = ret;
9473 targs = NULL;
9474 gcc_assert (tree_list_p ());
9475 return ret;
9476 }
9477
9478 const unsigned short tinst_level::refcount_infinity;
9479
9480 /* Increment OBJ's refcount unless it is already infinite. */
9481 static tinst_level *
9482 inc_refcount_use (tinst_level *obj)
9483 {
9484 if (obj && obj->refcount != tinst_level::refcount_infinity)
9485 ++obj->refcount;
9486 return obj;
9487 }
9488
9489 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9490 void
9491 tinst_level::free (tinst_level *obj)
9492 {
9493 if (obj->tree_list_p ())
9494 tree_list_freelist ().free (obj->get_node ());
9495 tinst_level_freelist ().free (obj);
9496 }
9497
9498 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9499 OBJ's DECL and OBJ, and start over with the tinst_level object that
9500 used to be referenced by OBJ's NEXT. */
9501 static void
9502 dec_refcount_use (tinst_level *obj)
9503 {
9504 while (obj
9505 && obj->refcount != tinst_level::refcount_infinity
9506 && !--obj->refcount)
9507 {
9508 tinst_level *next = obj->next;
9509 tinst_level::free (obj);
9510 obj = next;
9511 }
9512 }
9513
9514 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9515 and of the former PTR. Omitting the second argument is equivalent
9516 to passing (T*)NULL; this is allowed because passing the
9517 zero-valued integral constant NULL confuses type deduction and/or
9518 overload resolution. */
9519 template <typename T>
9520 static void
9521 set_refcount_ptr (T *& ptr, T *obj = NULL)
9522 {
9523 T *save = ptr;
9524 ptr = inc_refcount_use (obj);
9525 dec_refcount_use (save);
9526 }
9527
9528 static void
9529 add_pending_template (tree d)
9530 {
9531 tree ti = (TYPE_P (d)
9532 ? CLASSTYPE_TEMPLATE_INFO (d)
9533 : DECL_TEMPLATE_INFO (d));
9534 struct pending_template *pt;
9535 int level;
9536
9537 if (TI_PENDING_TEMPLATE_FLAG (ti))
9538 return;
9539
9540 /* We are called both from instantiate_decl, where we've already had a
9541 tinst_level pushed, and instantiate_template, where we haven't.
9542 Compensate. */
9543 gcc_assert (TREE_CODE (d) != TREE_LIST);
9544 level = !current_tinst_level
9545 || current_tinst_level->maybe_get_node () != d;
9546
9547 if (level)
9548 push_tinst_level (d);
9549
9550 pt = pending_template_freelist ().alloc ();
9551 pt->next = NULL;
9552 pt->tinst = NULL;
9553 set_refcount_ptr (pt->tinst, current_tinst_level);
9554 if (last_pending_template)
9555 last_pending_template->next = pt;
9556 else
9557 pending_templates = pt;
9558
9559 last_pending_template = pt;
9560
9561 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9562
9563 if (level)
9564 pop_tinst_level ();
9565 }
9566
9567
9568 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9569 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9570 documentation for TEMPLATE_ID_EXPR. */
9571
9572 tree
9573 lookup_template_function (tree fns, tree arglist)
9574 {
9575 if (fns == error_mark_node || arglist == error_mark_node)
9576 return error_mark_node;
9577
9578 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9579
9580 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9581 {
9582 error ("%q#D is not a function template", fns);
9583 return error_mark_node;
9584 }
9585
9586 if (BASELINK_P (fns))
9587 {
9588 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9589 unknown_type_node,
9590 BASELINK_FUNCTIONS (fns),
9591 arglist);
9592 return fns;
9593 }
9594
9595 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9596 }
9597
9598 /* Within the scope of a template class S<T>, the name S gets bound
9599 (in build_self_reference) to a TYPE_DECL for the class, not a
9600 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9601 or one of its enclosing classes, and that type is a template,
9602 return the associated TEMPLATE_DECL. Otherwise, the original
9603 DECL is returned.
9604
9605 Also handle the case when DECL is a TREE_LIST of ambiguous
9606 injected-class-names from different bases. */
9607
9608 tree
9609 maybe_get_template_decl_from_type_decl (tree decl)
9610 {
9611 if (decl == NULL_TREE)
9612 return decl;
9613
9614 /* DR 176: A lookup that finds an injected-class-name (10.2
9615 [class.member.lookup]) can result in an ambiguity in certain cases
9616 (for example, if it is found in more than one base class). If all of
9617 the injected-class-names that are found refer to specializations of
9618 the same class template, and if the name is followed by a
9619 template-argument-list, the reference refers to the class template
9620 itself and not a specialization thereof, and is not ambiguous. */
9621 if (TREE_CODE (decl) == TREE_LIST)
9622 {
9623 tree t, tmpl = NULL_TREE;
9624 for (t = decl; t; t = TREE_CHAIN (t))
9625 {
9626 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9627 if (!tmpl)
9628 tmpl = elt;
9629 else if (tmpl != elt)
9630 break;
9631 }
9632 if (tmpl && t == NULL_TREE)
9633 return tmpl;
9634 else
9635 return decl;
9636 }
9637
9638 return (decl != NULL_TREE
9639 && DECL_SELF_REFERENCE_P (decl)
9640 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9641 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9642 }
9643
9644 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9645 parameters, find the desired type.
9646
9647 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9648
9649 IN_DECL, if non-NULL, is the template declaration we are trying to
9650 instantiate.
9651
9652 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9653 the class we are looking up.
9654
9655 Issue error and warning messages under control of COMPLAIN.
9656
9657 If the template class is really a local class in a template
9658 function, then the FUNCTION_CONTEXT is the function in which it is
9659 being instantiated.
9660
9661 ??? Note that this function is currently called *twice* for each
9662 template-id: the first time from the parser, while creating the
9663 incomplete type (finish_template_type), and the second type during the
9664 real instantiation (instantiate_template_class). This is surely something
9665 that we want to avoid. It also causes some problems with argument
9666 coercion (see convert_nontype_argument for more information on this). */
9667
9668 static tree
9669 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9670 int entering_scope, tsubst_flags_t complain)
9671 {
9672 tree templ = NULL_TREE, parmlist;
9673 tree t;
9674 spec_entry **slot;
9675 spec_entry *entry;
9676 spec_entry elt;
9677 hashval_t hash;
9678
9679 if (identifier_p (d1))
9680 {
9681 tree value = innermost_non_namespace_value (d1);
9682 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9683 templ = value;
9684 else
9685 {
9686 if (context)
9687 push_decl_namespace (context);
9688 templ = lookup_name (d1);
9689 templ = maybe_get_template_decl_from_type_decl (templ);
9690 if (context)
9691 pop_decl_namespace ();
9692 }
9693 if (templ)
9694 context = DECL_CONTEXT (templ);
9695 }
9696 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9697 {
9698 tree type = TREE_TYPE (d1);
9699
9700 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9701 an implicit typename for the second A. Deal with it. */
9702 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9703 type = TREE_TYPE (type);
9704
9705 if (CLASSTYPE_TEMPLATE_INFO (type))
9706 {
9707 templ = CLASSTYPE_TI_TEMPLATE (type);
9708 d1 = DECL_NAME (templ);
9709 }
9710 }
9711 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9712 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9713 {
9714 templ = TYPE_TI_TEMPLATE (d1);
9715 d1 = DECL_NAME (templ);
9716 }
9717 else if (DECL_TYPE_TEMPLATE_P (d1))
9718 {
9719 templ = d1;
9720 d1 = DECL_NAME (templ);
9721 context = DECL_CONTEXT (templ);
9722 }
9723 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9724 {
9725 templ = d1;
9726 d1 = DECL_NAME (templ);
9727 }
9728
9729 /* Issue an error message if we didn't find a template. */
9730 if (! templ)
9731 {
9732 if (complain & tf_error)
9733 error ("%qT is not a template", d1);
9734 return error_mark_node;
9735 }
9736
9737 if (TREE_CODE (templ) != TEMPLATE_DECL
9738 /* Make sure it's a user visible template, if it was named by
9739 the user. */
9740 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9741 && !PRIMARY_TEMPLATE_P (templ)))
9742 {
9743 if (complain & tf_error)
9744 {
9745 error ("non-template type %qT used as a template", d1);
9746 if (in_decl)
9747 error ("for template declaration %q+D", in_decl);
9748 }
9749 return error_mark_node;
9750 }
9751
9752 complain &= ~tf_user;
9753
9754 /* An alias that just changes the name of a template is equivalent to the
9755 other template, so if any of the arguments are pack expansions, strip
9756 the alias to avoid problems with a pack expansion passed to a non-pack
9757 alias template parameter (DR 1430). */
9758 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9759 templ = get_underlying_template (templ);
9760
9761 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9762 {
9763 tree parm;
9764 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9765 if (arglist2 == error_mark_node
9766 || (!uses_template_parms (arglist2)
9767 && check_instantiated_args (templ, arglist2, complain)))
9768 return error_mark_node;
9769
9770 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9771 return parm;
9772 }
9773 else
9774 {
9775 tree template_type = TREE_TYPE (templ);
9776 tree gen_tmpl;
9777 tree type_decl;
9778 tree found = NULL_TREE;
9779 int arg_depth;
9780 int parm_depth;
9781 int is_dependent_type;
9782 int use_partial_inst_tmpl = false;
9783
9784 if (template_type == error_mark_node)
9785 /* An error occurred while building the template TEMPL, and a
9786 diagnostic has most certainly been emitted for that
9787 already. Let's propagate that error. */
9788 return error_mark_node;
9789
9790 gen_tmpl = most_general_template (templ);
9791 if (modules_p ())
9792 {
9793 tree origin = get_originating_module_decl (gen_tmpl);
9794 load_pending_specializations (CP_DECL_CONTEXT (origin),
9795 DECL_NAME (origin));
9796 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl))
9797 lazy_load_specializations (gen_tmpl);
9798 }
9799
9800 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9801 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9802 arg_depth = TMPL_ARGS_DEPTH (arglist);
9803
9804 if (arg_depth == 1 && parm_depth > 1)
9805 {
9806 /* We've been given an incomplete set of template arguments.
9807 For example, given:
9808
9809 template <class T> struct S1 {
9810 template <class U> struct S2 {};
9811 template <class U> struct S2<U*> {};
9812 };
9813
9814 we will be called with an ARGLIST of `U*', but the
9815 TEMPLATE will be `template <class T> template
9816 <class U> struct S1<T>::S2'. We must fill in the missing
9817 arguments. */
9818 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9819 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9820 arg_depth = TMPL_ARGS_DEPTH (arglist);
9821 }
9822
9823 /* Now we should have enough arguments. */
9824 gcc_assert (parm_depth == arg_depth);
9825
9826 /* From here on, we're only interested in the most general
9827 template. */
9828
9829 /* Calculate the BOUND_ARGS. These will be the args that are
9830 actually tsubst'd into the definition to create the
9831 instantiation. */
9832 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9833 complain,
9834 /*require_all_args=*/true,
9835 /*use_default_args=*/true);
9836
9837 if (arglist == error_mark_node)
9838 /* We were unable to bind the arguments. */
9839 return error_mark_node;
9840
9841 /* In the scope of a template class, explicit references to the
9842 template class refer to the type of the template, not any
9843 instantiation of it. For example, in:
9844
9845 template <class T> class C { void f(C<T>); }
9846
9847 the `C<T>' is just the same as `C'. Outside of the
9848 class, however, such a reference is an instantiation. */
9849 if (entering_scope
9850 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9851 || currently_open_class (template_type))
9852 {
9853 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9854
9855 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9856 return template_type;
9857 }
9858
9859 /* If we already have this specialization, return it. */
9860 elt.tmpl = gen_tmpl;
9861 elt.args = arglist;
9862 elt.spec = NULL_TREE;
9863 hash = spec_hasher::hash (&elt);
9864 entry = type_specializations->find_with_hash (&elt, hash);
9865
9866 if (entry)
9867 return entry->spec;
9868
9869 /* If the template's constraints are not satisfied,
9870 then we cannot form a valid type.
9871
9872 Note that the check is deferred until after the hash
9873 lookup. This prevents redundant checks on previously
9874 instantiated specializations. */
9875 if (flag_concepts
9876 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9877 && !constraints_satisfied_p (gen_tmpl, arglist))
9878 {
9879 if (complain & tf_error)
9880 {
9881 auto_diagnostic_group d;
9882 error ("template constraint failure for %qD", gen_tmpl);
9883 diagnose_constraints (input_location, gen_tmpl, arglist);
9884 }
9885 return error_mark_node;
9886 }
9887
9888 is_dependent_type = uses_template_parms (arglist);
9889
9890 /* If the deduced arguments are invalid, then the binding
9891 failed. */
9892 if (!is_dependent_type
9893 && check_instantiated_args (gen_tmpl,
9894 INNERMOST_TEMPLATE_ARGS (arglist),
9895 complain))
9896 return error_mark_node;
9897
9898 if (!is_dependent_type
9899 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9900 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9901 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9902 /* This occurs when the user has tried to define a tagged type
9903 in a scope that forbids it. We emitted an error during the
9904 parse. We didn't complete the bail out then, so here we
9905 are. */
9906 return error_mark_node;
9907
9908 context = DECL_CONTEXT (gen_tmpl);
9909 if (context && TYPE_P (context))
9910 {
9911 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9912 context = complete_type (context);
9913 }
9914 else
9915 context = tsubst (context, arglist, complain, in_decl);
9916
9917 if (context == error_mark_node)
9918 return error_mark_node;
9919
9920 if (!context)
9921 context = global_namespace;
9922
9923 /* Create the type. */
9924 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9925 {
9926 /* The user referred to a specialization of an alias
9927 template represented by GEN_TMPL.
9928
9929 [temp.alias]/2 says:
9930
9931 When a template-id refers to the specialization of an
9932 alias template, it is equivalent to the associated
9933 type obtained by substitution of its
9934 template-arguments for the template-parameters in the
9935 type-id of the alias template. */
9936
9937 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9938 /* Note that the call above (by indirectly calling
9939 register_specialization in tsubst_decl) registers the
9940 TYPE_DECL representing the specialization of the alias
9941 template. So next time someone substitutes ARGLIST for
9942 the template parms into the alias template (GEN_TMPL),
9943 she'll get that TYPE_DECL back. */
9944
9945 if (t == error_mark_node)
9946 return t;
9947 }
9948 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9949 {
9950 if (!is_dependent_type)
9951 {
9952 set_current_access_from_decl (TYPE_NAME (template_type));
9953 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9954 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9955 arglist, complain, in_decl),
9956 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9957 arglist, complain, in_decl),
9958 SCOPED_ENUM_P (template_type), NULL);
9959
9960 if (t == error_mark_node)
9961 return t;
9962 }
9963 else
9964 {
9965 /* We don't want to call start_enum for this type, since
9966 the values for the enumeration constants may involve
9967 template parameters. And, no one should be interested
9968 in the enumeration constants for such a type. */
9969 t = cxx_make_type (ENUMERAL_TYPE);
9970 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9971 }
9972 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9973 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9974 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9975 }
9976 else if (CLASS_TYPE_P (template_type))
9977 {
9978 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9979 instantiated here. */
9980 gcc_assert (!LAMBDA_TYPE_P (template_type));
9981
9982 t = make_class_type (TREE_CODE (template_type));
9983 CLASSTYPE_DECLARED_CLASS (t)
9984 = CLASSTYPE_DECLARED_CLASS (template_type);
9985 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9986
9987 /* A local class. Make sure the decl gets registered properly. */
9988 if (context == current_function_decl)
9989 if (pushtag (DECL_NAME (gen_tmpl), t)
9990 == error_mark_node)
9991 return error_mark_node;
9992
9993 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9994 /* This instantiation is another name for the primary
9995 template type. Set the TYPE_CANONICAL field
9996 appropriately. */
9997 TYPE_CANONICAL (t) = template_type;
9998 else if (any_template_arguments_need_structural_equality_p (arglist))
9999 /* Some of the template arguments require structural
10000 equality testing, so this template class requires
10001 structural equality testing. */
10002 SET_TYPE_STRUCTURAL_EQUALITY (t);
10003 }
10004 else
10005 gcc_unreachable ();
10006
10007 /* If we called start_enum or pushtag above, this information
10008 will already be set up. */
10009 type_decl = TYPE_NAME (t);
10010 if (!type_decl)
10011 {
10012 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10013
10014 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10015 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10016 DECL_SOURCE_LOCATION (type_decl)
10017 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10018 }
10019
10020 set_instantiating_module (type_decl);
10021 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10022 of export flag. We want to propagate this because it might
10023 be a friend declaration that pushes a new hidden binding. */
10024 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10025
10026 if (CLASS_TYPE_P (template_type))
10027 {
10028 TREE_PRIVATE (type_decl)
10029 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10030 TREE_PROTECTED (type_decl)
10031 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10032 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10033 {
10034 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10035 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10036 }
10037 }
10038
10039 if (OVERLOAD_TYPE_P (t)
10040 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10041 {
10042 static const char *tags[] = {"abi_tag", "may_alias"};
10043
10044 for (unsigned ix = 0; ix != 2; ix++)
10045 {
10046 tree attributes
10047 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10048
10049 if (attributes)
10050 TYPE_ATTRIBUTES (t)
10051 = tree_cons (TREE_PURPOSE (attributes),
10052 TREE_VALUE (attributes),
10053 TYPE_ATTRIBUTES (t));
10054 }
10055 }
10056
10057 /* Let's consider the explicit specialization of a member
10058 of a class template specialization that is implicitly instantiated,
10059 e.g.:
10060 template<class T>
10061 struct S
10062 {
10063 template<class U> struct M {}; //#0
10064 };
10065
10066 template<>
10067 template<>
10068 struct S<int>::M<char> //#1
10069 {
10070 int i;
10071 };
10072 [temp.expl.spec]/4 says this is valid.
10073
10074 In this case, when we write:
10075 S<int>::M<char> m;
10076
10077 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10078 the one of #0.
10079
10080 When we encounter #1, we want to store the partial instantiation
10081 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10082
10083 For all cases other than this "explicit specialization of member of a
10084 class template", we just want to store the most general template into
10085 the CLASSTYPE_TI_TEMPLATE of M.
10086
10087 This case of "explicit specialization of member of a class template"
10088 only happens when:
10089 1/ the enclosing class is an instantiation of, and therefore not
10090 the same as, the context of the most general template, and
10091 2/ we aren't looking at the partial instantiation itself, i.e.
10092 the innermost arguments are not the same as the innermost parms of
10093 the most general template.
10094
10095 So it's only when 1/ and 2/ happens that we want to use the partial
10096 instantiation of the member template in lieu of its most general
10097 template. */
10098
10099 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10100 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10101 /* the enclosing class must be an instantiation... */
10102 && CLASS_TYPE_P (context)
10103 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10104 {
10105 TREE_VEC_LENGTH (arglist)--;
10106 ++processing_template_decl;
10107 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10108 tree partial_inst_args =
10109 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10110 arglist, complain, NULL_TREE);
10111 --processing_template_decl;
10112 TREE_VEC_LENGTH (arglist)++;
10113 if (partial_inst_args == error_mark_node)
10114 return error_mark_node;
10115 use_partial_inst_tmpl =
10116 /*...and we must not be looking at the partial instantiation
10117 itself. */
10118 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10119 partial_inst_args);
10120 }
10121
10122 if (!use_partial_inst_tmpl)
10123 /* This case is easy; there are no member templates involved. */
10124 found = gen_tmpl;
10125 else
10126 {
10127 /* This is a full instantiation of a member template. Find
10128 the partial instantiation of which this is an instance. */
10129
10130 /* Temporarily reduce by one the number of levels in the ARGLIST
10131 so as to avoid comparing the last set of arguments. */
10132 TREE_VEC_LENGTH (arglist)--;
10133 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10134 TREE_VEC_LENGTH (arglist)++;
10135 /* FOUND is either a proper class type, or an alias
10136 template specialization. In the later case, it's a
10137 TYPE_DECL, resulting from the substituting of arguments
10138 for parameters in the TYPE_DECL of the alias template
10139 done earlier. So be careful while getting the template
10140 of FOUND. */
10141 found = (TREE_CODE (found) == TEMPLATE_DECL
10142 ? found
10143 : (TREE_CODE (found) == TYPE_DECL
10144 ? DECL_TI_TEMPLATE (found)
10145 : CLASSTYPE_TI_TEMPLATE (found)));
10146
10147 if (DECL_CLASS_TEMPLATE_P (found)
10148 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10149 {
10150 /* If this partial instantiation is specialized, we want to
10151 use it for hash table lookup. */
10152 elt.tmpl = found;
10153 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10154 hash = spec_hasher::hash (&elt);
10155 }
10156 }
10157
10158 /* Build template info for the new specialization. */
10159 if (TYPE_ALIAS_P (t))
10160 {
10161 /* This is constructed during instantiation of the alias
10162 decl. But for member templates of template classes, that
10163 is not correct as we need to refer to the partially
10164 instantiated template, not the most general template.
10165 The incorrect knowledge will not have escaped this
10166 instantiation process, so we're good just updating the
10167 template_info we made then. */
10168 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10169 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10170 if (TI_TEMPLATE (ti) != found)
10171 {
10172 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10173 TI_TEMPLATE (ti) = found;
10174 }
10175 }
10176 else
10177 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10178
10179 elt.spec = t;
10180 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10181 gcc_checking_assert (*slot == NULL);
10182 entry = ggc_alloc<spec_entry> ();
10183 *entry = elt;
10184 *slot = entry;
10185
10186 /* Note this use of the partial instantiation so we can check it
10187 later in maybe_process_partial_specialization. */
10188 DECL_TEMPLATE_INSTANTIATIONS (found)
10189 = tree_cons (arglist, t,
10190 DECL_TEMPLATE_INSTANTIATIONS (found));
10191
10192 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10193 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10194 /* Now that the type has been registered on the instantiations
10195 list, we set up the enumerators. Because the enumeration
10196 constants may involve the enumeration type itself, we make
10197 sure to register the type first, and then create the
10198 constants. That way, doing tsubst_expr for the enumeration
10199 constants won't result in recursive calls here; we'll find
10200 the instantiation and exit above. */
10201 tsubst_enum (template_type, t, arglist);
10202
10203 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10204 /* If the type makes use of template parameters, the
10205 code that generates debugging information will crash. */
10206 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10207
10208 /* Possibly limit visibility based on template args. */
10209 TREE_PUBLIC (type_decl) = 1;
10210 determine_visibility (type_decl);
10211
10212 inherit_targ_abi_tags (t);
10213
10214 return t;
10215 }
10216 }
10217
10218 /* Wrapper for lookup_template_class_1. */
10219
10220 tree
10221 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10222 int entering_scope, tsubst_flags_t complain)
10223 {
10224 tree ret;
10225 timevar_push (TV_TEMPLATE_INST);
10226 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10227 entering_scope, complain);
10228 timevar_pop (TV_TEMPLATE_INST);
10229 return ret;
10230 }
10231
10232 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10233
10234 tree
10235 lookup_template_variable (tree templ, tree arglist)
10236 {
10237 if (flag_concepts && variable_concept_p (templ))
10238 return build_concept_check (templ, arglist, tf_none);
10239
10240 /* The type of the expression is NULL_TREE since the template-id could refer
10241 to an explicit or partial specialization. */
10242 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10243 }
10244
10245 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10246
10247 tree
10248 finish_template_variable (tree var, tsubst_flags_t complain)
10249 {
10250 tree templ = TREE_OPERAND (var, 0);
10251 tree arglist = TREE_OPERAND (var, 1);
10252
10253 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10254 arglist = add_outermost_template_args (tmpl_args, arglist);
10255
10256 templ = most_general_template (templ);
10257 tree parms = DECL_TEMPLATE_PARMS (templ);
10258 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10259 /*req_all*/true,
10260 /*use_default*/true);
10261 if (arglist == error_mark_node)
10262 return error_mark_node;
10263
10264 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10265 {
10266 if (complain & tf_error)
10267 {
10268 auto_diagnostic_group d;
10269 error ("use of invalid variable template %qE", var);
10270 diagnose_constraints (location_of (var), templ, arglist);
10271 }
10272 return error_mark_node;
10273 }
10274
10275 return instantiate_template (templ, arglist, complain);
10276 }
10277
10278 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10279 TARGS template args, and instantiate it if it's not dependent. */
10280
10281 tree
10282 lookup_and_finish_template_variable (tree templ, tree targs,
10283 tsubst_flags_t complain)
10284 {
10285 templ = lookup_template_variable (templ, targs);
10286 if (!any_dependent_template_arguments_p (targs))
10287 {
10288 templ = finish_template_variable (templ, complain);
10289 mark_used (templ);
10290 }
10291
10292 return convert_from_reference (templ);
10293 }
10294
10295 /* If the set of template parameters PARMS contains a template parameter
10296 at the given LEVEL and INDEX, then return this parameter. Otherwise
10297 return NULL_TREE. */
10298
10299 static tree
10300 corresponding_template_parameter (tree parms, int level, int index)
10301 {
10302 while (TMPL_PARMS_DEPTH (parms) > level)
10303 parms = TREE_CHAIN (parms);
10304
10305 if (TMPL_PARMS_DEPTH (parms) != level
10306 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10307 return NULL_TREE;
10308
10309 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10310 /* As in template_parm_to_arg. */
10311 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10312 t = TREE_TYPE (t);
10313 else
10314 t = DECL_INITIAL (t);
10315
10316 gcc_assert (TEMPLATE_PARM_P (t));
10317 return t;
10318 }
10319
10320 /* Return the template parameter from PARMS that positionally corresponds
10321 to the template parameter PARM, or else return NULL_TREE. */
10322
10323 static tree
10324 corresponding_template_parameter (tree parms, tree parm)
10325 {
10326 int level, index;
10327 template_parm_level_and_index (parm, &level, &index);
10328 return corresponding_template_parameter (parms, level, index);
10329 }
10330
10331 \f
10332 struct pair_fn_data
10333 {
10334 tree_fn_t fn;
10335 tree_fn_t any_fn;
10336 void *data;
10337 /* True when we should also visit template parameters that occur in
10338 non-deduced contexts. */
10339 bool include_nondeduced_p;
10340 hash_set<tree> *visited;
10341 };
10342
10343 /* Called from for_each_template_parm via walk_tree. */
10344
10345 static tree
10346 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10347 {
10348 tree t = *tp;
10349 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10350 tree_fn_t fn = pfd->fn;
10351 void *data = pfd->data;
10352 tree result = NULL_TREE;
10353
10354 #define WALK_SUBTREE(NODE) \
10355 do \
10356 { \
10357 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10358 pfd->include_nondeduced_p, \
10359 pfd->any_fn); \
10360 if (result) goto out; \
10361 } \
10362 while (0)
10363
10364 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10365 return t;
10366
10367 if (TYPE_P (t)
10368 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10369 WALK_SUBTREE (TYPE_CONTEXT (t));
10370
10371 switch (TREE_CODE (t))
10372 {
10373 case RECORD_TYPE:
10374 if (TYPE_PTRMEMFUNC_P (t))
10375 break;
10376 /* Fall through. */
10377
10378 case UNION_TYPE:
10379 case ENUMERAL_TYPE:
10380 if (!TYPE_TEMPLATE_INFO (t))
10381 *walk_subtrees = 0;
10382 else
10383 WALK_SUBTREE (TYPE_TI_ARGS (t));
10384 break;
10385
10386 case INTEGER_TYPE:
10387 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10388 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10389 break;
10390
10391 case METHOD_TYPE:
10392 /* Since we're not going to walk subtrees, we have to do this
10393 explicitly here. */
10394 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10395 /* Fall through. */
10396
10397 case FUNCTION_TYPE:
10398 /* Check the return type. */
10399 WALK_SUBTREE (TREE_TYPE (t));
10400
10401 /* Check the parameter types. Since default arguments are not
10402 instantiated until they are needed, the TYPE_ARG_TYPES may
10403 contain expressions that involve template parameters. But,
10404 no-one should be looking at them yet. And, once they're
10405 instantiated, they don't contain template parameters, so
10406 there's no point in looking at them then, either. */
10407 {
10408 tree parm;
10409
10410 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10411 WALK_SUBTREE (TREE_VALUE (parm));
10412
10413 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10414 want walk_tree walking into them itself. */
10415 *walk_subtrees = 0;
10416 }
10417
10418 if (flag_noexcept_type)
10419 {
10420 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10421 if (spec)
10422 WALK_SUBTREE (TREE_PURPOSE (spec));
10423 }
10424 break;
10425
10426 case TYPEOF_TYPE:
10427 case DECLTYPE_TYPE:
10428 case UNDERLYING_TYPE:
10429 if (pfd->include_nondeduced_p
10430 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10431 pfd->visited,
10432 pfd->include_nondeduced_p,
10433 pfd->any_fn))
10434 return error_mark_node;
10435 *walk_subtrees = false;
10436 break;
10437
10438 case FUNCTION_DECL:
10439 case VAR_DECL:
10440 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10441 WALK_SUBTREE (DECL_TI_ARGS (t));
10442 /* Fall through. */
10443
10444 case PARM_DECL:
10445 case CONST_DECL:
10446 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10447 WALK_SUBTREE (DECL_INITIAL (t));
10448 if (DECL_CONTEXT (t)
10449 && pfd->include_nondeduced_p)
10450 WALK_SUBTREE (DECL_CONTEXT (t));
10451 break;
10452
10453 case BOUND_TEMPLATE_TEMPLATE_PARM:
10454 /* Record template parameters such as `T' inside `TT<T>'. */
10455 WALK_SUBTREE (TYPE_TI_ARGS (t));
10456 /* Fall through. */
10457
10458 case TEMPLATE_TEMPLATE_PARM:
10459 case TEMPLATE_TYPE_PARM:
10460 case TEMPLATE_PARM_INDEX:
10461 if (fn && (*fn)(t, data))
10462 return t;
10463 else if (!fn)
10464 return t;
10465 break;
10466
10467 case TEMPLATE_DECL:
10468 /* A template template parameter is encountered. */
10469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10470 WALK_SUBTREE (TREE_TYPE (t));
10471
10472 /* Already substituted template template parameter */
10473 *walk_subtrees = 0;
10474 break;
10475
10476 case TYPENAME_TYPE:
10477 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10478 partial instantiation. */
10479 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10480 *walk_subtrees = 0;
10481 break;
10482
10483 case CONSTRUCTOR:
10484 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10485 && pfd->include_nondeduced_p)
10486 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10487 break;
10488
10489 case INDIRECT_REF:
10490 case COMPONENT_REF:
10491 /* If there's no type, then this thing must be some expression
10492 involving template parameters. */
10493 if (!fn && !TREE_TYPE (t))
10494 return error_mark_node;
10495 break;
10496
10497 case MODOP_EXPR:
10498 case CAST_EXPR:
10499 case IMPLICIT_CONV_EXPR:
10500 case REINTERPRET_CAST_EXPR:
10501 case CONST_CAST_EXPR:
10502 case STATIC_CAST_EXPR:
10503 case DYNAMIC_CAST_EXPR:
10504 case ARROW_EXPR:
10505 case DOTSTAR_EXPR:
10506 case TYPEID_EXPR:
10507 case PSEUDO_DTOR_EXPR:
10508 if (!fn)
10509 return error_mark_node;
10510 break;
10511
10512 case SCOPE_REF:
10513 if (pfd->include_nondeduced_p)
10514 WALK_SUBTREE (TREE_OPERAND (t, 0));
10515 break;
10516
10517 case REQUIRES_EXPR:
10518 {
10519 if (!fn)
10520 return error_mark_node;
10521
10522 /* Recursively walk the type of each constraint variable. */
10523 tree p = TREE_OPERAND (t, 0);
10524 while (p)
10525 {
10526 WALK_SUBTREE (TREE_TYPE (p));
10527 p = TREE_CHAIN (p);
10528 }
10529 }
10530 break;
10531
10532 default:
10533 break;
10534 }
10535
10536 #undef WALK_SUBTREE
10537
10538 /* We didn't find any template parameters we liked. */
10539 out:
10540 return result;
10541 }
10542
10543 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10544 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10545 call FN with the parameter and the DATA.
10546 If FN returns nonzero, the iteration is terminated, and
10547 for_each_template_parm returns 1. Otherwise, the iteration
10548 continues. If FN never returns a nonzero value, the value
10549 returned by for_each_template_parm is 0. If FN is NULL, it is
10550 considered to be the function which always returns 1.
10551
10552 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10553 parameters that occur in non-deduced contexts. When false, only
10554 visits those template parameters that can be deduced. */
10555
10556 static tree
10557 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10558 hash_set<tree> *visited,
10559 bool include_nondeduced_p,
10560 tree_fn_t any_fn)
10561 {
10562 struct pair_fn_data pfd;
10563 tree result;
10564
10565 /* Set up. */
10566 pfd.fn = fn;
10567 pfd.any_fn = any_fn;
10568 pfd.data = data;
10569 pfd.include_nondeduced_p = include_nondeduced_p;
10570
10571 /* Walk the tree. (Conceptually, we would like to walk without
10572 duplicates, but for_each_template_parm_r recursively calls
10573 for_each_template_parm, so we would need to reorganize a fair
10574 bit to use walk_tree_without_duplicates, so we keep our own
10575 visited list.) */
10576 if (visited)
10577 pfd.visited = visited;
10578 else
10579 pfd.visited = new hash_set<tree>;
10580 result = cp_walk_tree (&t,
10581 for_each_template_parm_r,
10582 &pfd,
10583 pfd.visited);
10584
10585 /* Clean up. */
10586 if (!visited)
10587 {
10588 delete pfd.visited;
10589 pfd.visited = 0;
10590 }
10591
10592 return result;
10593 }
10594
10595 struct find_template_parameter_info
10596 {
10597 explicit find_template_parameter_info (tree ctx_parms)
10598 : parm_list (NULL_TREE),
10599 ctx_parms (ctx_parms),
10600 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10601 {}
10602
10603 hash_set<tree> visited;
10604 hash_set<tree> parms;
10605 tree parm_list;
10606 tree ctx_parms;
10607 int max_depth;
10608 };
10609
10610 /* Appends the declaration of T to the list in DATA. */
10611
10612 static int
10613 keep_template_parm (tree t, void* data)
10614 {
10615 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10616
10617 /* Template parameters declared within the expression are not part of
10618 the parameter mapping. For example, in this concept:
10619
10620 template<typename T>
10621 concept C = requires { <expr> } -> same_as<int>;
10622
10623 the return specifier same_as<int> declares a new decltype parameter
10624 that must not be part of the parameter mapping. The same is true
10625 for generic lambda parameters, lambda template parameters, etc. */
10626 int level;
10627 int index;
10628 template_parm_level_and_index (t, &level, &index);
10629 if (level > ftpi->max_depth)
10630 return 0;
10631
10632 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10633 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10634 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10635 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10636
10637 /* This template parameter might be an argument to a cached dependent
10638 specalization that was formed earlier inside some other template, in
10639 which case the parameter is not among the ones that are in-scope.
10640 Look in CTX_PARMS to find the corresponding in-scope template
10641 parameter, and use it instead. */
10642 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10643 t = in_scope;
10644
10645 /* Arguments like const T yield parameters like const T. This means that
10646 a template-id like X<T, const T> would yield two distinct parameters:
10647 T and const T. Adjust types to their unqualified versions. */
10648 if (TYPE_P (t))
10649 t = TYPE_MAIN_VARIANT (t);
10650 if (!ftpi->parms.add (t))
10651 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10652
10653 /* Verify the parameter we found has a valid index. */
10654 if (flag_checking)
10655 {
10656 tree parms = ftpi->ctx_parms;
10657 while (TMPL_PARMS_DEPTH (parms) > level)
10658 parms = TREE_CHAIN (parms);
10659 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10660 gcc_assert (index < len);
10661 }
10662
10663 return 0;
10664 }
10665
10666 /* Ensure that we recursively examine certain terms that are not normally
10667 visited in for_each_template_parm_r. */
10668
10669 static int
10670 any_template_parm_r (tree t, void *data)
10671 {
10672 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10673
10674 #define WALK_SUBTREE(NODE) \
10675 do \
10676 { \
10677 for_each_template_parm (NODE, keep_template_parm, data, \
10678 &ftpi->visited, true, \
10679 any_template_parm_r); \
10680 } \
10681 while (0)
10682
10683 /* A mention of a member alias/typedef is a use of all of its template
10684 arguments, including those from the enclosing class, so we don't use
10685 alias_template_specialization_p here. */
10686 if (TYPE_P (t) && typedef_variant_p (t))
10687 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10688 WALK_SUBTREE (TI_ARGS (tinfo));
10689
10690 switch (TREE_CODE (t))
10691 {
10692 case TEMPLATE_TYPE_PARM:
10693 /* Type constraints of a placeholder type may contain parameters. */
10694 if (is_auto (t))
10695 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10696 WALK_SUBTREE (constr);
10697 break;
10698
10699 case TEMPLATE_ID_EXPR:
10700 /* Search through references to variable templates. */
10701 WALK_SUBTREE (TREE_OPERAND (t, 0));
10702 WALK_SUBTREE (TREE_OPERAND (t, 1));
10703 break;
10704
10705 case TEMPLATE_PARM_INDEX:
10706 case PARM_DECL:
10707 /* A parameter or constraint variable may also depend on a template
10708 parameter without explicitly naming it. */
10709 WALK_SUBTREE (TREE_TYPE (t));
10710 break;
10711
10712 case TEMPLATE_DECL:
10713 {
10714 /* If T is a member template that shares template parameters with
10715 ctx_parms, we need to mark all those parameters for mapping. */
10716 tree dparms = DECL_TEMPLATE_PARMS (t);
10717 tree cparms = ftpi->ctx_parms;
10718 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10719 dparms = TREE_CHAIN (dparms);
10720 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10721 cparms = TREE_CHAIN (cparms);
10722 while (dparms
10723 && (TREE_TYPE (TREE_VALUE (dparms))
10724 != TREE_TYPE (TREE_VALUE (cparms))))
10725 dparms = TREE_CHAIN (dparms),
10726 cparms = TREE_CHAIN (cparms);
10727 if (dparms)
10728 {
10729 int ddepth = TMPL_PARMS_DEPTH (dparms);
10730 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10731 for (int i = 0; i < ddepth; ++i)
10732 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10733 }
10734 }
10735 break;
10736
10737 case LAMBDA_EXPR:
10738 {
10739 /* Look in the parms and body. */
10740 tree fn = lambda_function (t);
10741 WALK_SUBTREE (TREE_TYPE (fn));
10742 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10743 }
10744 break;
10745
10746 case IDENTIFIER_NODE:
10747 if (IDENTIFIER_CONV_OP_P (t))
10748 /* The conversion-type-id of a conversion operator may be dependent. */
10749 WALK_SUBTREE (TREE_TYPE (t));
10750 break;
10751
10752 default:
10753 break;
10754 }
10755
10756 /* Keep walking. */
10757 return 0;
10758 }
10759
10760 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10761 are the template parameters in scope. */
10762
10763 tree
10764 find_template_parameters (tree t, tree ctx_parms)
10765 {
10766 if (!ctx_parms)
10767 return NULL_TREE;
10768
10769 find_template_parameter_info ftpi (ctx_parms);
10770 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10771 /*include_nondeduced*/true, any_template_parm_r);
10772 return ftpi.parm_list;
10773 }
10774
10775 /* Returns true if T depends on any template parameter. */
10776
10777 int
10778 uses_template_parms (tree t)
10779 {
10780 if (t == NULL_TREE)
10781 return false;
10782
10783 bool dependent_p;
10784 int saved_processing_template_decl;
10785
10786 saved_processing_template_decl = processing_template_decl;
10787 if (!saved_processing_template_decl)
10788 processing_template_decl = 1;
10789 if (TYPE_P (t))
10790 dependent_p = dependent_type_p (t);
10791 else if (TREE_CODE (t) == TREE_VEC)
10792 dependent_p = any_dependent_template_arguments_p (t);
10793 else if (TREE_CODE (t) == TREE_LIST)
10794 dependent_p = (uses_template_parms (TREE_VALUE (t))
10795 || uses_template_parms (TREE_CHAIN (t)));
10796 else if (TREE_CODE (t) == TYPE_DECL)
10797 dependent_p = dependent_type_p (TREE_TYPE (t));
10798 else if (t == error_mark_node)
10799 dependent_p = false;
10800 else
10801 dependent_p = instantiation_dependent_expression_p (t);
10802
10803 processing_template_decl = saved_processing_template_decl;
10804
10805 return dependent_p;
10806 }
10807
10808 /* Returns true iff we're processing an incompletely instantiated function
10809 template. Useful instead of processing_template_decl because the latter
10810 is set to 0 during instantiate_non_dependent_expr. */
10811
10812 bool
10813 in_template_function (void)
10814 {
10815 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10816 the latter might get set for e.g. access checking during satisfaction. */
10817 tree fn = cfun ? cfun->decl : NULL_TREE;
10818 bool ret;
10819 ++processing_template_decl;
10820 ret = (fn && DECL_LANG_SPECIFIC (fn)
10821 && DECL_TEMPLATE_INFO (fn)
10822 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10823 --processing_template_decl;
10824 return ret;
10825 }
10826
10827 /* Returns true if T depends on any template parameter with level LEVEL. */
10828
10829 bool
10830 uses_template_parms_level (tree t, int level)
10831 {
10832 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10833 /*include_nondeduced_p=*/true);
10834 }
10835
10836 /* Returns true if the signature of DECL depends on any template parameter from
10837 its enclosing class. */
10838
10839 bool
10840 uses_outer_template_parms (tree decl)
10841 {
10842 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10843 if (depth == 0)
10844 return false;
10845 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10846 &depth, NULL, /*include_nondeduced_p=*/true))
10847 return true;
10848 if (PRIMARY_TEMPLATE_P (decl)
10849 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10850 (DECL_TEMPLATE_PARMS (decl)),
10851 template_parm_outer_level,
10852 &depth, NULL, /*include_nondeduced_p=*/true))
10853 return true;
10854 tree ci = get_constraints (decl);
10855 if (ci)
10856 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10857 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10858 &depth, NULL, /*nondeduced*/true))
10859 return true;
10860 return false;
10861 }
10862
10863 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10864 ill-formed translation unit, i.e. a variable or function that isn't
10865 usable in a constant expression. */
10866
10867 static inline bool
10868 neglectable_inst_p (tree d)
10869 {
10870 return (d && DECL_P (d)
10871 && !undeduced_auto_decl (d)
10872 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10873 : decl_maybe_constant_var_p (d)));
10874 }
10875
10876 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10877 neglectable and instantiated from within an erroneous instantiation. */
10878
10879 static bool
10880 limit_bad_template_recursion (tree decl)
10881 {
10882 struct tinst_level *lev = current_tinst_level;
10883 int errs = errorcount + sorrycount;
10884 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10885 return false;
10886
10887 for (; lev; lev = lev->next)
10888 if (neglectable_inst_p (lev->maybe_get_node ()))
10889 break;
10890
10891 return (lev && errs > lev->errors);
10892 }
10893
10894 static int tinst_depth;
10895 extern int max_tinst_depth;
10896 int depth_reached;
10897
10898 static GTY(()) struct tinst_level *last_error_tinst_level;
10899
10900 /* We're starting to instantiate D; record the template instantiation context
10901 at LOC for diagnostics and to restore it later. */
10902
10903 bool
10904 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10905 {
10906 struct tinst_level *new_level;
10907
10908 if (tinst_depth >= max_tinst_depth)
10909 {
10910 /* Tell error.c not to try to instantiate any templates. */
10911 at_eof = 2;
10912 fatal_error (input_location,
10913 "template instantiation depth exceeds maximum of %d"
10914 " (use %<-ftemplate-depth=%> to increase the maximum)",
10915 max_tinst_depth);
10916 return false;
10917 }
10918
10919 /* If the current instantiation caused problems, don't let it instantiate
10920 anything else. Do allow deduction substitution and decls usable in
10921 constant expressions. */
10922 if (!targs && limit_bad_template_recursion (tldcl))
10923 {
10924 /* Avoid no_linkage_errors and unused function warnings for this
10925 decl. */
10926 TREE_NO_WARNING (tldcl) = 1;
10927 return false;
10928 }
10929
10930 /* When not -quiet, dump template instantiations other than functions, since
10931 announce_function will take care of those. */
10932 if (!quiet_flag && !targs
10933 && TREE_CODE (tldcl) != TREE_LIST
10934 && TREE_CODE (tldcl) != FUNCTION_DECL)
10935 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10936
10937 new_level = tinst_level_freelist ().alloc ();
10938 new_level->tldcl = tldcl;
10939 new_level->targs = targs;
10940 new_level->locus = loc;
10941 new_level->errors = errorcount + sorrycount;
10942 new_level->next = NULL;
10943 new_level->refcount = 0;
10944 new_level->path = new_level->visible = nullptr;
10945 set_refcount_ptr (new_level->next, current_tinst_level);
10946 set_refcount_ptr (current_tinst_level, new_level);
10947
10948 ++tinst_depth;
10949 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10950 depth_reached = tinst_depth;
10951
10952 return true;
10953 }
10954
10955 /* We're starting substitution of TMPL<ARGS>; record the template
10956 substitution context for diagnostics and to restore it later. */
10957
10958 bool
10959 push_tinst_level (tree tmpl, tree args)
10960 {
10961 return push_tinst_level_loc (tmpl, args, input_location);
10962 }
10963
10964 /* We're starting to instantiate D; record INPUT_LOCATION and the
10965 template instantiation context for diagnostics and to restore it
10966 later. */
10967
10968 bool
10969 push_tinst_level (tree d)
10970 {
10971 return push_tinst_level_loc (d, input_location);
10972 }
10973
10974 /* Likewise, but record LOC as the program location. */
10975
10976 bool
10977 push_tinst_level_loc (tree d, location_t loc)
10978 {
10979 gcc_assert (TREE_CODE (d) != TREE_LIST);
10980 return push_tinst_level_loc (d, NULL, loc);
10981 }
10982
10983 /* We're done instantiating this template; return to the instantiation
10984 context. */
10985
10986 void
10987 pop_tinst_level (void)
10988 {
10989 /* Restore the filename and line number stashed away when we started
10990 this instantiation. */
10991 input_location = current_tinst_level->locus;
10992 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10993 --tinst_depth;
10994 }
10995
10996 /* We're instantiating a deferred template; restore the template
10997 instantiation context in which the instantiation was requested, which
10998 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10999
11000 static tree
11001 reopen_tinst_level (struct tinst_level *level)
11002 {
11003 struct tinst_level *t;
11004
11005 tinst_depth = 0;
11006 for (t = level; t; t = t->next)
11007 ++tinst_depth;
11008
11009 set_refcount_ptr (current_tinst_level, level);
11010 pop_tinst_level ();
11011 if (current_tinst_level)
11012 current_tinst_level->errors = errorcount+sorrycount;
11013 return level->maybe_get_node ();
11014 }
11015
11016 /* Returns the TINST_LEVEL which gives the original instantiation
11017 context. */
11018
11019 struct tinst_level *
11020 outermost_tinst_level (void)
11021 {
11022 struct tinst_level *level = current_tinst_level;
11023 if (level)
11024 while (level->next)
11025 level = level->next;
11026 return level;
11027 }
11028
11029 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11030 vector of template arguments, as for tsubst.
11031
11032 Returns an appropriate tsubst'd friend declaration. */
11033
11034 static tree
11035 tsubst_friend_function (tree decl, tree args)
11036 {
11037 tree new_friend;
11038
11039 if (TREE_CODE (decl) == FUNCTION_DECL
11040 && DECL_TEMPLATE_INSTANTIATION (decl)
11041 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11042 /* This was a friend declared with an explicit template
11043 argument list, e.g.:
11044
11045 friend void f<>(T);
11046
11047 to indicate that f was a template instantiation, not a new
11048 function declaration. Now, we have to figure out what
11049 instantiation of what template. */
11050 {
11051 tree template_id, arglist, fns;
11052 tree new_args;
11053 tree tmpl;
11054 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11055
11056 /* Friend functions are looked up in the containing namespace scope.
11057 We must enter that scope, to avoid finding member functions of the
11058 current class with same name. */
11059 push_nested_namespace (ns);
11060 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11061 tf_warning_or_error, NULL_TREE,
11062 /*integral_constant_expression_p=*/false);
11063 pop_nested_namespace (ns);
11064 arglist = tsubst (DECL_TI_ARGS (decl), args,
11065 tf_warning_or_error, NULL_TREE);
11066 template_id = lookup_template_function (fns, arglist);
11067
11068 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11069 tmpl = determine_specialization (template_id, new_friend,
11070 &new_args,
11071 /*need_member_template=*/0,
11072 TREE_VEC_LENGTH (args),
11073 tsk_none);
11074 return instantiate_template (tmpl, new_args, tf_error);
11075 }
11076
11077 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11078 if (new_friend == error_mark_node)
11079 return error_mark_node;
11080
11081 /* The NEW_FRIEND will look like an instantiation, to the
11082 compiler, but is not an instantiation from the point of view of
11083 the language. For example, we might have had:
11084
11085 template <class T> struct S {
11086 template <class U> friend void f(T, U);
11087 };
11088
11089 Then, in S<int>, template <class U> void f(int, U) is not an
11090 instantiation of anything. */
11091
11092 DECL_USE_TEMPLATE (new_friend) = 0;
11093 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11094 {
11095 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11096 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11097 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11098 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11099
11100 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11101 match in decls_match. */
11102 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11103 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11104 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11105 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11106 }
11107
11108 /* The mangled name for the NEW_FRIEND is incorrect. The function
11109 is not a template instantiation and should not be mangled like
11110 one. Therefore, we forget the mangling here; we'll recompute it
11111 later if we need it. */
11112 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11113 {
11114 SET_DECL_RTL (new_friend, NULL);
11115 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11116 }
11117
11118 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11119 {
11120 tree old_decl;
11121 tree ns;
11122
11123 /* We must save some information from NEW_FRIEND before calling
11124 duplicate decls since that function will free NEW_FRIEND if
11125 possible. */
11126 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11127 tree new_friend_result_template_info = NULL_TREE;
11128 bool new_friend_is_defn =
11129 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11130 (template_for_substitution (new_friend)))
11131 != NULL_TREE);
11132 tree not_tmpl = new_friend;
11133
11134 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11135 {
11136 /* This declaration is a `primary' template. */
11137 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11138
11139 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11140 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11141 }
11142
11143 /* Inside pushdecl_namespace_level, we will push into the
11144 current namespace. However, the friend function should go
11145 into the namespace of the template. */
11146 ns = decl_namespace_context (new_friend);
11147 push_nested_namespace (ns);
11148 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11149 pop_nested_namespace (ns);
11150
11151 if (old_decl == error_mark_node)
11152 return error_mark_node;
11153
11154 if (old_decl != new_friend)
11155 {
11156 /* This new friend declaration matched an existing
11157 declaration. For example, given:
11158
11159 template <class T> void f(T);
11160 template <class U> class C {
11161 template <class T> friend void f(T) {}
11162 };
11163
11164 the friend declaration actually provides the definition
11165 of `f', once C has been instantiated for some type. So,
11166 old_decl will be the out-of-class template declaration,
11167 while new_friend is the in-class definition.
11168
11169 But, if `f' was called before this point, the
11170 instantiation of `f' will have DECL_TI_ARGS corresponding
11171 to `T' but not to `U', references to which might appear
11172 in the definition of `f'. Previously, the most general
11173 template for an instantiation of `f' was the out-of-class
11174 version; now it is the in-class version. Therefore, we
11175 run through all specialization of `f', adding to their
11176 DECL_TI_ARGS appropriately. In particular, they need a
11177 new set of outer arguments, corresponding to the
11178 arguments for this class instantiation.
11179
11180 The same situation can arise with something like this:
11181
11182 friend void f(int);
11183 template <class T> class C {
11184 friend void f(T) {}
11185 };
11186
11187 when `C<int>' is instantiated. Now, `f(int)' is defined
11188 in the class. */
11189
11190 if (!new_friend_is_defn)
11191 /* On the other hand, if the in-class declaration does
11192 *not* provide a definition, then we don't want to alter
11193 existing definitions. We can just leave everything
11194 alone. */
11195 ;
11196 else
11197 {
11198 tree new_template = TI_TEMPLATE (new_friend_template_info);
11199 tree new_args = TI_ARGS (new_friend_template_info);
11200
11201 /* Overwrite whatever template info was there before, if
11202 any, with the new template information pertaining to
11203 the declaration. */
11204 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11205
11206 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11207 {
11208 /* We should have called reregister_specialization in
11209 duplicate_decls. */
11210 gcc_assert (retrieve_specialization (new_template,
11211 new_args, 0)
11212 == old_decl);
11213
11214 /* Instantiate it if the global has already been used. */
11215 if (DECL_ODR_USED (old_decl))
11216 instantiate_decl (old_decl, /*defer_ok=*/true,
11217 /*expl_inst_class_mem_p=*/false);
11218 }
11219 else
11220 {
11221 tree t;
11222
11223 /* Indicate that the old function template is a partial
11224 instantiation. */
11225 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11226 = new_friend_result_template_info;
11227
11228 gcc_assert (new_template
11229 == most_general_template (new_template));
11230 gcc_assert (new_template != old_decl);
11231
11232 /* Reassign any specializations already in the hash table
11233 to the new more general template, and add the
11234 additional template args. */
11235 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11236 t != NULL_TREE;
11237 t = TREE_CHAIN (t))
11238 {
11239 tree spec = TREE_VALUE (t);
11240 spec_entry elt;
11241
11242 elt.tmpl = old_decl;
11243 elt.args = DECL_TI_ARGS (spec);
11244 elt.spec = NULL_TREE;
11245
11246 decl_specializations->remove_elt (&elt);
11247
11248 DECL_TI_ARGS (spec)
11249 = add_outermost_template_args (new_args,
11250 DECL_TI_ARGS (spec));
11251
11252 register_specialization
11253 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11254
11255 }
11256 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11257 }
11258 }
11259
11260 /* The information from NEW_FRIEND has been merged into OLD_DECL
11261 by duplicate_decls. */
11262 new_friend = old_decl;
11263 }
11264 }
11265 else
11266 {
11267 tree context = DECL_CONTEXT (new_friend);
11268 bool dependent_p;
11269
11270 /* In the code
11271 template <class T> class C {
11272 template <class U> friend void C1<U>::f (); // case 1
11273 friend void C2<T>::f (); // case 2
11274 };
11275 we only need to make sure CONTEXT is a complete type for
11276 case 2. To distinguish between the two cases, we note that
11277 CONTEXT of case 1 remains dependent type after tsubst while
11278 this isn't true for case 2. */
11279 ++processing_template_decl;
11280 dependent_p = dependent_type_p (context);
11281 --processing_template_decl;
11282
11283 if (!dependent_p
11284 && !complete_type_or_else (context, NULL_TREE))
11285 return error_mark_node;
11286
11287 if (COMPLETE_TYPE_P (context))
11288 {
11289 tree fn = new_friend;
11290 /* do_friend adds the TEMPLATE_DECL for any member friend
11291 template even if it isn't a member template, i.e.
11292 template <class T> friend A<T>::f();
11293 Look through it in that case. */
11294 if (TREE_CODE (fn) == TEMPLATE_DECL
11295 && !PRIMARY_TEMPLATE_P (fn))
11296 fn = DECL_TEMPLATE_RESULT (fn);
11297 /* Check to see that the declaration is really present, and,
11298 possibly obtain an improved declaration. */
11299 fn = check_classfn (context, fn, NULL_TREE);
11300
11301 if (fn)
11302 new_friend = fn;
11303 }
11304 }
11305
11306 return new_friend;
11307 }
11308
11309 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11310 template arguments, as for tsubst.
11311
11312 Returns an appropriate tsubst'd friend type or error_mark_node on
11313 failure. */
11314
11315 static tree
11316 tsubst_friend_class (tree friend_tmpl, tree args)
11317 {
11318 tree tmpl;
11319
11320 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11321 {
11322 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11323 return TREE_TYPE (tmpl);
11324 }
11325
11326 tree context = CP_DECL_CONTEXT (friend_tmpl);
11327 if (TREE_CODE (context) == NAMESPACE_DECL)
11328 push_nested_namespace (context);
11329 else
11330 {
11331 context = tsubst (context, args, tf_error, NULL_TREE);
11332 push_nested_class (context);
11333 }
11334
11335 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11336 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11337
11338 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11339 {
11340 /* The friend template has already been declared. Just
11341 check to see that the declarations match, and install any new
11342 default parameters. We must tsubst the default parameters,
11343 of course. We only need the innermost template parameters
11344 because that is all that redeclare_class_template will look
11345 at. */
11346 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11347 > TMPL_ARGS_DEPTH (args))
11348 {
11349 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11350 args, tf_warning_or_error);
11351 location_t saved_input_location = input_location;
11352 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11353 tree cons = get_constraints (tmpl);
11354 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11355 input_location = saved_input_location;
11356 }
11357 }
11358 else
11359 {
11360 /* The friend template has not already been declared. In this
11361 case, the instantiation of the template class will cause the
11362 injection of this template into the namespace scope. */
11363 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11364
11365 if (tmpl != error_mark_node)
11366 {
11367 /* The new TMPL is not an instantiation of anything, so we
11368 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11369 for the new type because that is supposed to be the
11370 corresponding template decl, i.e., TMPL. */
11371 DECL_USE_TEMPLATE (tmpl) = 0;
11372 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11373 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11374 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11375 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11376
11377 /* Substitute into and set the constraints on the new declaration. */
11378 if (tree ci = get_constraints (friend_tmpl))
11379 {
11380 ++processing_template_decl;
11381 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11382 DECL_FRIEND_CONTEXT (friend_tmpl));
11383 --processing_template_decl;
11384 set_constraints (tmpl, ci);
11385 }
11386
11387 /* Inject this template into the enclosing namspace scope. */
11388 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11389 }
11390 }
11391
11392 if (TREE_CODE (context) == NAMESPACE_DECL)
11393 pop_nested_namespace (context);
11394 else
11395 pop_nested_class ();
11396
11397 return TREE_TYPE (tmpl);
11398 }
11399
11400 /* Returns zero if TYPE cannot be completed later due to circularity.
11401 Otherwise returns one. */
11402
11403 static int
11404 can_complete_type_without_circularity (tree type)
11405 {
11406 if (type == NULL_TREE || type == error_mark_node)
11407 return 0;
11408 else if (COMPLETE_TYPE_P (type))
11409 return 1;
11410 else if (TREE_CODE (type) == ARRAY_TYPE)
11411 return can_complete_type_without_circularity (TREE_TYPE (type));
11412 else if (CLASS_TYPE_P (type)
11413 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11414 return 0;
11415 else
11416 return 1;
11417 }
11418
11419 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11420 tsubst_flags_t, tree);
11421
11422 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11423 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11424
11425 static tree
11426 tsubst_attribute (tree t, tree *decl_p, tree args,
11427 tsubst_flags_t complain, tree in_decl)
11428 {
11429 gcc_assert (ATTR_IS_DEPENDENT (t));
11430
11431 tree val = TREE_VALUE (t);
11432 if (val == NULL_TREE)
11433 /* Nothing to do. */;
11434 else if ((flag_openmp || flag_openmp_simd)
11435 && is_attribute_p ("omp declare simd",
11436 get_attribute_name (t)))
11437 {
11438 tree clauses = TREE_VALUE (val);
11439 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11440 complain, in_decl);
11441 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11442 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11443 tree parms = DECL_ARGUMENTS (*decl_p);
11444 clauses
11445 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11446 if (clauses)
11447 val = build_tree_list (NULL_TREE, clauses);
11448 else
11449 val = NULL_TREE;
11450 }
11451 else if (flag_openmp
11452 && is_attribute_p ("omp declare variant base",
11453 get_attribute_name (t)))
11454 {
11455 ++cp_unevaluated_operand;
11456 tree varid
11457 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11458 in_decl, /*integral_constant_expression_p=*/false);
11459 --cp_unevaluated_operand;
11460 tree chain = TREE_CHAIN (val);
11461 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11462 tree ctx = copy_list (TREE_VALUE (val));
11463 tree simd = get_identifier ("simd");
11464 tree score = get_identifier (" score");
11465 tree condition = get_identifier ("condition");
11466 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11467 {
11468 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11469 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11470 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11471 {
11472 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11473 {
11474 tree clauses = TREE_VALUE (t2);
11475 clauses = tsubst_omp_clauses (clauses,
11476 C_ORT_OMP_DECLARE_SIMD, args,
11477 complain, in_decl);
11478 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11479 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11480 TREE_VALUE (t2) = clauses;
11481 }
11482 else
11483 {
11484 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11485 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11486 if (TREE_VALUE (t3))
11487 {
11488 bool allow_string
11489 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11490 && TREE_PURPOSE (t3) != score);
11491 tree v = TREE_VALUE (t3);
11492 if (TREE_CODE (v) == STRING_CST && allow_string)
11493 continue;
11494 v = tsubst_expr (v, args, complain, in_decl, true);
11495 v = fold_non_dependent_expr (v);
11496 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11497 || (TREE_PURPOSE (t3) == score
11498 ? TREE_CODE (v) != INTEGER_CST
11499 : !tree_fits_shwi_p (v)))
11500 {
11501 location_t loc
11502 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11503 match_loc);
11504 if (TREE_PURPOSE (t3) == score)
11505 error_at (loc, "score argument must be "
11506 "constant integer expression");
11507 else if (allow_string)
11508 error_at (loc, "property must be constant "
11509 "integer expression or string "
11510 "literal");
11511 else
11512 error_at (loc, "property must be constant "
11513 "integer expression");
11514 return NULL_TREE;
11515 }
11516 else if (TREE_PURPOSE (t3) == score
11517 && tree_int_cst_sgn (v) < 0)
11518 {
11519 location_t loc
11520 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11521 match_loc);
11522 error_at (loc, "score argument must be "
11523 "non-negative");
11524 return NULL_TREE;
11525 }
11526 TREE_VALUE (t3) = v;
11527 }
11528 }
11529 }
11530 }
11531 val = tree_cons (varid, ctx, chain);
11532 }
11533 /* If the first attribute argument is an identifier, don't
11534 pass it through tsubst. Attributes like mode, format,
11535 cleanup and several target specific attributes expect it
11536 unmodified. */
11537 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11538 {
11539 tree chain
11540 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11541 /*integral_constant_expression_p=*/false);
11542 if (chain != TREE_CHAIN (val))
11543 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11544 }
11545 else if (PACK_EXPANSION_P (val))
11546 {
11547 /* An attribute pack expansion. */
11548 tree purp = TREE_PURPOSE (t);
11549 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11550 if (pack == error_mark_node)
11551 return error_mark_node;
11552 int len = TREE_VEC_LENGTH (pack);
11553 tree list = NULL_TREE;
11554 tree *q = &list;
11555 for (int i = 0; i < len; ++i)
11556 {
11557 tree elt = TREE_VEC_ELT (pack, i);
11558 *q = build_tree_list (purp, elt);
11559 q = &TREE_CHAIN (*q);
11560 }
11561 return list;
11562 }
11563 else
11564 val = tsubst_expr (val, args, complain, in_decl,
11565 /*integral_constant_expression_p=*/false);
11566
11567 if (val != TREE_VALUE (t))
11568 return build_tree_list (TREE_PURPOSE (t), val);
11569 return t;
11570 }
11571
11572 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11573 unchanged or a new TREE_LIST chain. */
11574
11575 static tree
11576 tsubst_attributes (tree attributes, tree args,
11577 tsubst_flags_t complain, tree in_decl)
11578 {
11579 tree last_dep = NULL_TREE;
11580
11581 for (tree t = attributes; t; t = TREE_CHAIN (t))
11582 if (ATTR_IS_DEPENDENT (t))
11583 {
11584 last_dep = t;
11585 attributes = copy_list (attributes);
11586 break;
11587 }
11588
11589 if (last_dep)
11590 for (tree *p = &attributes; *p; )
11591 {
11592 tree t = *p;
11593 if (ATTR_IS_DEPENDENT (t))
11594 {
11595 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11596 if (subst != t)
11597 {
11598 *p = subst;
11599 while (*p)
11600 p = &TREE_CHAIN (*p);
11601 *p = TREE_CHAIN (t);
11602 continue;
11603 }
11604 }
11605 p = &TREE_CHAIN (*p);
11606 }
11607
11608 return attributes;
11609 }
11610
11611 /* Apply any attributes which had to be deferred until instantiation
11612 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11613 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11614
11615 static void
11616 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11617 tree args, tsubst_flags_t complain, tree in_decl)
11618 {
11619 tree last_dep = NULL_TREE;
11620 tree t;
11621 tree *p;
11622
11623 if (attributes == NULL_TREE)
11624 return;
11625
11626 if (DECL_P (*decl_p))
11627 {
11628 if (TREE_TYPE (*decl_p) == error_mark_node)
11629 return;
11630 p = &DECL_ATTRIBUTES (*decl_p);
11631 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11632 to our attributes parameter. */
11633 gcc_assert (*p == attributes);
11634 }
11635 else
11636 {
11637 p = &TYPE_ATTRIBUTES (*decl_p);
11638 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11639 lookup_template_class_1, and should be preserved. */
11640 gcc_assert (*p != attributes);
11641 while (*p)
11642 p = &TREE_CHAIN (*p);
11643 }
11644
11645 for (t = attributes; t; t = TREE_CHAIN (t))
11646 if (ATTR_IS_DEPENDENT (t))
11647 {
11648 last_dep = t;
11649 attributes = copy_list (attributes);
11650 break;
11651 }
11652
11653 *p = attributes;
11654 if (last_dep)
11655 {
11656 tree late_attrs = NULL_TREE;
11657 tree *q = &late_attrs;
11658
11659 for (; *p; )
11660 {
11661 t = *p;
11662 if (ATTR_IS_DEPENDENT (t))
11663 {
11664 *p = TREE_CHAIN (t);
11665 TREE_CHAIN (t) = NULL_TREE;
11666 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11667 while (*q)
11668 q = &TREE_CHAIN (*q);
11669 }
11670 else
11671 p = &TREE_CHAIN (t);
11672 }
11673
11674 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11675 }
11676 }
11677
11678 /* The template TMPL is being instantiated with the template arguments TARGS.
11679 Perform the access checks that we deferred when parsing the template. */
11680
11681 static void
11682 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11683 {
11684 unsigned i;
11685 deferred_access_check *chk;
11686
11687 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11688 return;
11689
11690 if (vec<deferred_access_check, va_gc> *access_checks
11691 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11692 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11693 {
11694 tree decl = chk->decl;
11695 tree diag_decl = chk->diag_decl;
11696 tree type_scope = TREE_TYPE (chk->binfo);
11697
11698 if (uses_template_parms (type_scope))
11699 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11700
11701 /* Make access check error messages point to the location
11702 of the use of the typedef. */
11703 iloc_sentinel ils (chk->loc);
11704 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11705 decl, diag_decl, tf_warning_or_error);
11706 }
11707 }
11708
11709 static tree
11710 instantiate_class_template_1 (tree type)
11711 {
11712 tree templ, args, pattern, t, member;
11713 tree typedecl;
11714 tree pbinfo;
11715 tree base_list;
11716 unsigned int saved_maximum_field_alignment;
11717 tree fn_context;
11718
11719 if (type == error_mark_node)
11720 return error_mark_node;
11721
11722 if (COMPLETE_OR_OPEN_TYPE_P (type)
11723 || uses_template_parms (type))
11724 return type;
11725
11726 /* Figure out which template is being instantiated. */
11727 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11728 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11729
11730 /* Mark the type as in the process of being defined. */
11731 TYPE_BEING_DEFINED (type) = 1;
11732
11733 /* We may be in the middle of deferred access check. Disable
11734 it now. */
11735 deferring_access_check_sentinel acs (dk_no_deferred);
11736
11737 /* Determine what specialization of the original template to
11738 instantiate. */
11739 t = most_specialized_partial_spec (type, tf_warning_or_error);
11740 if (t == error_mark_node)
11741 return error_mark_node;
11742 else if (t)
11743 {
11744 /* This TYPE is actually an instantiation of a partial
11745 specialization. We replace the innermost set of ARGS with
11746 the arguments appropriate for substitution. For example,
11747 given:
11748
11749 template <class T> struct S {};
11750 template <class T> struct S<T*> {};
11751
11752 and supposing that we are instantiating S<int*>, ARGS will
11753 presently be {int*} -- but we need {int}. */
11754 pattern = TREE_TYPE (t);
11755 args = TREE_PURPOSE (t);
11756 }
11757 else
11758 {
11759 pattern = TREE_TYPE (templ);
11760 args = CLASSTYPE_TI_ARGS (type);
11761 }
11762
11763 /* If the template we're instantiating is incomplete, then clearly
11764 there's nothing we can do. */
11765 if (!COMPLETE_TYPE_P (pattern))
11766 {
11767 /* We can try again later. */
11768 TYPE_BEING_DEFINED (type) = 0;
11769 return type;
11770 }
11771
11772 /* If we've recursively instantiated too many templates, stop. */
11773 if (! push_tinst_level (type))
11774 return type;
11775
11776 int saved_unevaluated_operand = cp_unevaluated_operand;
11777 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11778
11779 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11780 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11781 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11782 fn_context = error_mark_node;
11783 if (!fn_context)
11784 push_to_top_level ();
11785 else
11786 {
11787 cp_unevaluated_operand = 0;
11788 c_inhibit_evaluation_warnings = 0;
11789 }
11790 /* Use #pragma pack from the template context. */
11791 saved_maximum_field_alignment = maximum_field_alignment;
11792 maximum_field_alignment = TYPE_PRECISION (pattern);
11793
11794 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11795
11796 /* Set the input location to the most specialized template definition.
11797 This is needed if tsubsting causes an error. */
11798 typedecl = TYPE_MAIN_DECL (pattern);
11799 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11800 DECL_SOURCE_LOCATION (typedecl);
11801
11802 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11803 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11804 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11805 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11806 if (ANON_AGGR_TYPE_P (pattern))
11807 SET_ANON_AGGR_TYPE_P (type);
11808 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11809 {
11810 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11811 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11812 /* Adjust visibility for template arguments. */
11813 determine_visibility (TYPE_MAIN_DECL (type));
11814 }
11815 if (CLASS_TYPE_P (type))
11816 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11817
11818 pbinfo = TYPE_BINFO (pattern);
11819
11820 /* We should never instantiate a nested class before its enclosing
11821 class; we need to look up the nested class by name before we can
11822 instantiate it, and that lookup should instantiate the enclosing
11823 class. */
11824 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11825 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11826
11827 base_list = NULL_TREE;
11828 if (BINFO_N_BASE_BINFOS (pbinfo))
11829 {
11830 tree pbase_binfo;
11831 tree pushed_scope;
11832 int i;
11833
11834 /* We must enter the scope containing the type, as that is where
11835 the accessibility of types named in dependent bases are
11836 looked up from. */
11837 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11838
11839 /* Substitute into each of the bases to determine the actual
11840 basetypes. */
11841 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11842 {
11843 tree base;
11844 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11845 tree expanded_bases = NULL_TREE;
11846 int idx, len = 1;
11847
11848 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11849 {
11850 expanded_bases =
11851 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11852 args, tf_error, NULL_TREE);
11853 if (expanded_bases == error_mark_node)
11854 continue;
11855
11856 len = TREE_VEC_LENGTH (expanded_bases);
11857 }
11858
11859 for (idx = 0; idx < len; idx++)
11860 {
11861 if (expanded_bases)
11862 /* Extract the already-expanded base class. */
11863 base = TREE_VEC_ELT (expanded_bases, idx);
11864 else
11865 /* Substitute to figure out the base class. */
11866 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11867 NULL_TREE);
11868
11869 if (base == error_mark_node)
11870 continue;
11871
11872 base_list = tree_cons (access, base, base_list);
11873 if (BINFO_VIRTUAL_P (pbase_binfo))
11874 TREE_TYPE (base_list) = integer_type_node;
11875 }
11876 }
11877
11878 /* The list is now in reverse order; correct that. */
11879 base_list = nreverse (base_list);
11880
11881 if (pushed_scope)
11882 pop_scope (pushed_scope);
11883 }
11884 /* Now call xref_basetypes to set up all the base-class
11885 information. */
11886 xref_basetypes (type, base_list);
11887
11888 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11889 (int) ATTR_FLAG_TYPE_IN_PLACE,
11890 args, tf_error, NULL_TREE);
11891 fixup_attribute_variants (type);
11892
11893 /* Now that our base classes are set up, enter the scope of the
11894 class, so that name lookups into base classes, etc. will work
11895 correctly. This is precisely analogous to what we do in
11896 begin_class_definition when defining an ordinary non-template
11897 class, except we also need to push the enclosing classes. */
11898 push_nested_class (type);
11899
11900 /* Now members are processed in the order of declaration. */
11901 for (member = CLASSTYPE_DECL_LIST (pattern);
11902 member; member = TREE_CHAIN (member))
11903 {
11904 tree t = TREE_VALUE (member);
11905
11906 if (TREE_PURPOSE (member))
11907 {
11908 if (TYPE_P (t))
11909 {
11910 if (LAMBDA_TYPE_P (t))
11911 /* A closure type for a lambda in an NSDMI or default argument.
11912 Ignore it; it will be regenerated when needed. */
11913 continue;
11914
11915 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11916 && TYPE_LANG_SPECIFIC (t)
11917 && CLASSTYPE_IS_TEMPLATE (t));
11918
11919 /* If the member is a class template, then -- even after
11920 substitution -- there may be dependent types in the
11921 template argument list for the class. We increment
11922 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11923 that function will assume that no types are dependent
11924 when outside of a template. */
11925 if (class_template_p)
11926 ++processing_template_decl;
11927 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11928 if (class_template_p)
11929 --processing_template_decl;
11930 if (newtag == error_mark_node)
11931 continue;
11932
11933 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11934 {
11935 tree name = TYPE_IDENTIFIER (t);
11936
11937 if (class_template_p)
11938 /* Unfortunately, lookup_template_class sets
11939 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11940 instantiation (i.e., for the type of a member
11941 template class nested within a template class.)
11942 This behavior is required for
11943 maybe_process_partial_specialization to work
11944 correctly, but is not accurate in this case;
11945 the TAG is not an instantiation of anything.
11946 (The corresponding TEMPLATE_DECL is an
11947 instantiation, but the TYPE is not.) */
11948 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11949
11950 /* Now, we call pushtag to put this NEWTAG into the scope of
11951 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11952 pushtag calling push_template_decl. We don't have to do
11953 this for enums because it will already have been done in
11954 tsubst_enum. */
11955 if (name)
11956 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11957 pushtag (name, newtag);
11958 }
11959 }
11960 else if (DECL_DECLARES_FUNCTION_P (t))
11961 {
11962 tree r;
11963
11964 if (TREE_CODE (t) == TEMPLATE_DECL)
11965 ++processing_template_decl;
11966 r = tsubst (t, args, tf_error, NULL_TREE);
11967 if (TREE_CODE (t) == TEMPLATE_DECL)
11968 --processing_template_decl;
11969 set_current_access_from_decl (r);
11970 finish_member_declaration (r);
11971 /* Instantiate members marked with attribute used. */
11972 if (r != error_mark_node && DECL_PRESERVE_P (r))
11973 mark_used (r);
11974 if (TREE_CODE (r) == FUNCTION_DECL
11975 && DECL_OMP_DECLARE_REDUCTION_P (r))
11976 cp_check_omp_declare_reduction (r);
11977 }
11978 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11979 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11980 /* A closure type for a lambda in an NSDMI or default argument.
11981 Ignore it; it will be regenerated when needed. */;
11982 else
11983 {
11984 /* Build new TYPE_FIELDS. */
11985 if (TREE_CODE (t) == STATIC_ASSERT)
11986 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11987 /*integral_constant_expression_p=*/true);
11988 else if (TREE_CODE (t) != CONST_DECL)
11989 {
11990 tree r;
11991 tree vec = NULL_TREE;
11992 int len = 1;
11993
11994 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11995 /* The file and line for this declaration, to
11996 assist in error message reporting. Since we
11997 called push_tinst_level above, we don't need to
11998 restore these. */
11999 input_location = DECL_SOURCE_LOCATION (t);
12000
12001 if (TREE_CODE (t) == TEMPLATE_DECL)
12002 ++processing_template_decl;
12003 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12004 if (TREE_CODE (t) == TEMPLATE_DECL)
12005 --processing_template_decl;
12006
12007 if (TREE_CODE (r) == TREE_VEC)
12008 {
12009 /* A capture pack became multiple fields. */
12010 vec = r;
12011 len = TREE_VEC_LENGTH (vec);
12012 }
12013
12014 for (int i = 0; i < len; ++i)
12015 {
12016 if (vec)
12017 r = TREE_VEC_ELT (vec, i);
12018 if (VAR_P (r))
12019 {
12020 /* In [temp.inst]:
12021
12022 [t]he initialization (and any associated
12023 side-effects) of a static data member does
12024 not occur unless the static data member is
12025 itself used in a way that requires the
12026 definition of the static data member to
12027 exist.
12028
12029 Therefore, we do not substitute into the
12030 initialized for the static data member here. */
12031 finish_static_data_member_decl
12032 (r,
12033 /*init=*/NULL_TREE,
12034 /*init_const_expr_p=*/false,
12035 /*asmspec_tree=*/NULL_TREE,
12036 /*flags=*/0);
12037 /* Instantiate members marked with attribute used. */
12038 if (r != error_mark_node && DECL_PRESERVE_P (r))
12039 mark_used (r);
12040 }
12041 else if (TREE_CODE (r) == FIELD_DECL)
12042 {
12043 /* Determine whether R has a valid type and can be
12044 completed later. If R is invalid, then its type
12045 is replaced by error_mark_node. */
12046 tree rtype = TREE_TYPE (r);
12047 if (can_complete_type_without_circularity (rtype))
12048 complete_type (rtype);
12049
12050 if (!complete_or_array_type_p (rtype))
12051 {
12052 /* If R's type couldn't be completed and
12053 it isn't a flexible array member (whose
12054 type is incomplete by definition) give
12055 an error. */
12056 cxx_incomplete_type_error (r, rtype);
12057 TREE_TYPE (r) = error_mark_node;
12058 }
12059 else if (TREE_CODE (rtype) == ARRAY_TYPE
12060 && TYPE_DOMAIN (rtype) == NULL_TREE
12061 && (TREE_CODE (type) == UNION_TYPE
12062 || TREE_CODE (type) == QUAL_UNION_TYPE))
12063 {
12064 error ("flexible array member %qD in union", r);
12065 TREE_TYPE (r) = error_mark_node;
12066 }
12067 else if (!verify_type_context (input_location,
12068 TCTX_FIELD, rtype))
12069 TREE_TYPE (r) = error_mark_node;
12070 }
12071
12072 /* If it is a TYPE_DECL for a class-scoped
12073 ENUMERAL_TYPE, such a thing will already have
12074 been added to the field list by tsubst_enum
12075 in finish_member_declaration case above. */
12076 if (!(TREE_CODE (r) == TYPE_DECL
12077 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12078 && DECL_ARTIFICIAL (r)))
12079 {
12080 set_current_access_from_decl (r);
12081 finish_member_declaration (r);
12082 }
12083 }
12084 }
12085 }
12086 }
12087 else
12088 {
12089 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12090 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12091 {
12092 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12093
12094 tree friend_type = t;
12095 bool adjust_processing_template_decl = false;
12096
12097 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12098 {
12099 /* template <class T> friend class C; */
12100 friend_type = tsubst_friend_class (friend_type, args);
12101 adjust_processing_template_decl = true;
12102 }
12103 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12104 {
12105 /* template <class T> friend class C::D; */
12106 friend_type = tsubst (friend_type, args,
12107 tf_warning_or_error, NULL_TREE);
12108 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12109 friend_type = TREE_TYPE (friend_type);
12110 adjust_processing_template_decl = true;
12111 }
12112 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12113 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12114 {
12115 /* This could be either
12116
12117 friend class T::C;
12118
12119 when dependent_type_p is false or
12120
12121 template <class U> friend class T::C;
12122
12123 otherwise. */
12124 /* Bump processing_template_decl in case this is something like
12125 template <class T> friend struct A<T>::B. */
12126 ++processing_template_decl;
12127 friend_type = tsubst (friend_type, args,
12128 tf_warning_or_error, NULL_TREE);
12129 if (dependent_type_p (friend_type))
12130 adjust_processing_template_decl = true;
12131 --processing_template_decl;
12132 }
12133 else if (uses_template_parms (friend_type))
12134 /* friend class C<T>; */
12135 friend_type = tsubst (friend_type, args,
12136 tf_warning_or_error, NULL_TREE);
12137
12138 /* Otherwise it's
12139
12140 friend class C;
12141
12142 where C is already declared or
12143
12144 friend class C<int>;
12145
12146 We don't have to do anything in these cases. */
12147
12148 if (adjust_processing_template_decl)
12149 /* Trick make_friend_class into realizing that the friend
12150 we're adding is a template, not an ordinary class. It's
12151 important that we use make_friend_class since it will
12152 perform some error-checking and output cross-reference
12153 information. */
12154 ++processing_template_decl;
12155
12156 if (friend_type != error_mark_node)
12157 make_friend_class (type, friend_type, /*complain=*/false);
12158
12159 if (adjust_processing_template_decl)
12160 --processing_template_decl;
12161 }
12162 else
12163 {
12164 /* Build new DECL_FRIENDLIST. */
12165 tree r;
12166
12167 /* The file and line for this declaration, to
12168 assist in error message reporting. Since we
12169 called push_tinst_level above, we don't need to
12170 restore these. */
12171 input_location = DECL_SOURCE_LOCATION (t);
12172
12173 if (TREE_CODE (t) == TEMPLATE_DECL)
12174 {
12175 ++processing_template_decl;
12176 push_deferring_access_checks (dk_no_check);
12177 }
12178
12179 r = tsubst_friend_function (t, args);
12180 add_friend (type, r, /*complain=*/false);
12181 if (TREE_CODE (t) == TEMPLATE_DECL)
12182 {
12183 pop_deferring_access_checks ();
12184 --processing_template_decl;
12185 }
12186 }
12187 }
12188 }
12189
12190 if (fn_context)
12191 {
12192 /* Restore these before substituting into the lambda capture
12193 initializers. */
12194 cp_unevaluated_operand = saved_unevaluated_operand;
12195 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12196 }
12197
12198 /* Set the file and line number information to whatever is given for
12199 the class itself. This puts error messages involving generated
12200 implicit functions at a predictable point, and the same point
12201 that would be used for non-template classes. */
12202 input_location = DECL_SOURCE_LOCATION (typedecl);
12203
12204 unreverse_member_declarations (type);
12205 finish_struct_1 (type);
12206 TYPE_BEING_DEFINED (type) = 0;
12207
12208 /* We don't instantiate default arguments for member functions. 14.7.1:
12209
12210 The implicit instantiation of a class template specialization causes
12211 the implicit instantiation of the declarations, but not of the
12212 definitions or default arguments, of the class member functions,
12213 member classes, static data members and member templates.... */
12214
12215 perform_instantiation_time_access_checks (pattern, args);
12216 perform_deferred_access_checks (tf_warning_or_error);
12217 pop_nested_class ();
12218 maximum_field_alignment = saved_maximum_field_alignment;
12219 if (!fn_context)
12220 pop_from_top_level ();
12221 pop_tinst_level ();
12222
12223 /* The vtable for a template class can be emitted in any translation
12224 unit in which the class is instantiated. When there is no key
12225 method, however, finish_struct_1 will already have added TYPE to
12226 the keyed_classes. */
12227 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12228 vec_safe_push (keyed_classes, type);
12229
12230 return type;
12231 }
12232
12233 /* Wrapper for instantiate_class_template_1. */
12234
12235 tree
12236 instantiate_class_template (tree type)
12237 {
12238 tree ret;
12239 timevar_push (TV_TEMPLATE_INST);
12240 ret = instantiate_class_template_1 (type);
12241 timevar_pop (TV_TEMPLATE_INST);
12242 return ret;
12243 }
12244
12245 tree
12246 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12247 {
12248 tree r;
12249
12250 if (!t)
12251 r = t;
12252 else if (TYPE_P (t))
12253 r = tsubst (t, args, complain, in_decl);
12254 else
12255 {
12256 if (!(complain & tf_warning))
12257 ++c_inhibit_evaluation_warnings;
12258 r = tsubst_expr (t, args, complain, in_decl,
12259 /*integral_constant_expression_p=*/true);
12260 if (!(complain & tf_warning))
12261 --c_inhibit_evaluation_warnings;
12262 }
12263
12264 return r;
12265 }
12266
12267 /* Given a function parameter pack TMPL_PARM and some function parameters
12268 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12269 and set *SPEC_P to point at the next point in the list. */
12270
12271 tree
12272 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12273 {
12274 /* Collect all of the extra "packed" parameters into an
12275 argument pack. */
12276 tree parmvec;
12277 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12278 tree spec_parm = *spec_p;
12279 int i, len;
12280
12281 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12282 if (tmpl_parm
12283 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12284 break;
12285
12286 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12287 parmvec = make_tree_vec (len);
12288 spec_parm = *spec_p;
12289 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12290 {
12291 tree elt = spec_parm;
12292 if (DECL_PACK_P (elt))
12293 elt = make_pack_expansion (elt);
12294 TREE_VEC_ELT (parmvec, i) = elt;
12295 }
12296
12297 /* Build the argument packs. */
12298 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12299 *spec_p = spec_parm;
12300
12301 return argpack;
12302 }
12303
12304 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12305 NONTYPE_ARGUMENT_PACK. */
12306
12307 static tree
12308 make_fnparm_pack (tree spec_parm)
12309 {
12310 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12311 }
12312
12313 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12314 pack expansion with no extra args, 2 if it has extra args, or 0
12315 if it is not a pack expansion. */
12316
12317 static int
12318 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12319 {
12320 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12321 /* We're being called before this happens in tsubst_pack_expansion. */
12322 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12323 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12324 if (i >= TREE_VEC_LENGTH (vec))
12325 return 0;
12326 tree elt = TREE_VEC_ELT (vec, i);
12327 if (DECL_P (elt))
12328 /* A decl pack is itself an expansion. */
12329 elt = TREE_TYPE (elt);
12330 if (!PACK_EXPANSION_P (elt))
12331 return 0;
12332 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12333 return 2;
12334 return 1;
12335 }
12336
12337
12338 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12339
12340 static tree
12341 make_argument_pack_select (tree arg_pack, unsigned index)
12342 {
12343 tree aps = make_node (ARGUMENT_PACK_SELECT);
12344
12345 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12346 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12347
12348 return aps;
12349 }
12350
12351 /* This is a subroutine of tsubst_pack_expansion.
12352
12353 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12354 mechanism to store the (non complete list of) arguments of the
12355 substitution and return a non substituted pack expansion, in order
12356 to wait for when we have enough arguments to really perform the
12357 substitution. */
12358
12359 static bool
12360 use_pack_expansion_extra_args_p (tree parm_packs,
12361 int arg_pack_len,
12362 bool has_empty_arg)
12363 {
12364 /* If one pack has an expansion and another pack has a normal
12365 argument or if one pack has an empty argument and an another
12366 one hasn't then tsubst_pack_expansion cannot perform the
12367 substitution and need to fall back on the
12368 PACK_EXPANSION_EXTRA mechanism. */
12369 if (parm_packs == NULL_TREE)
12370 return false;
12371 else if (has_empty_arg)
12372 {
12373 /* If all the actual packs are pack expansions, we can still
12374 subsitute directly. */
12375 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12376 {
12377 tree a = TREE_VALUE (p);
12378 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12379 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12380 a = ARGUMENT_PACK_ARGS (a);
12381 if (TREE_VEC_LENGTH (a) == 1)
12382 a = TREE_VEC_ELT (a, 0);
12383 if (PACK_EXPANSION_P (a))
12384 continue;
12385 return true;
12386 }
12387 return false;
12388 }
12389
12390 bool has_expansion_arg = false;
12391 for (int i = 0 ; i < arg_pack_len; ++i)
12392 {
12393 bool has_non_expansion_arg = false;
12394 for (tree parm_pack = parm_packs;
12395 parm_pack;
12396 parm_pack = TREE_CHAIN (parm_pack))
12397 {
12398 tree arg = TREE_VALUE (parm_pack);
12399
12400 int exp = argument_pack_element_is_expansion_p (arg, i);
12401 if (exp == 2)
12402 /* We can't substitute a pack expansion with extra args into
12403 our pattern. */
12404 return true;
12405 else if (exp)
12406 has_expansion_arg = true;
12407 else
12408 has_non_expansion_arg = true;
12409 }
12410
12411 if (has_expansion_arg && has_non_expansion_arg)
12412 return true;
12413 }
12414 return false;
12415 }
12416
12417 /* [temp.variadic]/6 says that:
12418
12419 The instantiation of a pack expansion [...]
12420 produces a list E1,E2, ..., En, where N is the number of elements
12421 in the pack expansion parameters.
12422
12423 This subroutine of tsubst_pack_expansion produces one of these Ei.
12424
12425 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12426 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12427 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12428 INDEX is the index 'i' of the element Ei to produce. ARGS,
12429 COMPLAIN, and IN_DECL are the same parameters as for the
12430 tsubst_pack_expansion function.
12431
12432 The function returns the resulting Ei upon successful completion,
12433 or error_mark_node.
12434
12435 Note that this function possibly modifies the ARGS parameter, so
12436 it's the responsibility of the caller to restore it. */
12437
12438 static tree
12439 gen_elem_of_pack_expansion_instantiation (tree pattern,
12440 tree parm_packs,
12441 unsigned index,
12442 tree args /* This parm gets
12443 modified. */,
12444 tsubst_flags_t complain,
12445 tree in_decl)
12446 {
12447 tree t;
12448 bool ith_elem_is_expansion = false;
12449
12450 /* For each parameter pack, change the substitution of the parameter
12451 pack to the ith argument in its argument pack, then expand the
12452 pattern. */
12453 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12454 {
12455 tree parm = TREE_PURPOSE (pack);
12456 tree arg_pack = TREE_VALUE (pack);
12457 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12458
12459 ith_elem_is_expansion |=
12460 argument_pack_element_is_expansion_p (arg_pack, index);
12461
12462 /* Select the Ith argument from the pack. */
12463 if (TREE_CODE (parm) == PARM_DECL
12464 || VAR_P (parm)
12465 || TREE_CODE (parm) == FIELD_DECL)
12466 {
12467 if (index == 0)
12468 {
12469 aps = make_argument_pack_select (arg_pack, index);
12470 if (!mark_used (parm, complain) && !(complain & tf_error))
12471 return error_mark_node;
12472 register_local_specialization (aps, parm);
12473 }
12474 else
12475 aps = retrieve_local_specialization (parm);
12476 }
12477 else
12478 {
12479 int idx, level;
12480 template_parm_level_and_index (parm, &level, &idx);
12481
12482 if (index == 0)
12483 {
12484 aps = make_argument_pack_select (arg_pack, index);
12485 /* Update the corresponding argument. */
12486 TMPL_ARG (args, level, idx) = aps;
12487 }
12488 else
12489 /* Re-use the ARGUMENT_PACK_SELECT. */
12490 aps = TMPL_ARG (args, level, idx);
12491 }
12492 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12493 }
12494
12495 /* Substitute into the PATTERN with the (possibly altered)
12496 arguments. */
12497 if (pattern == in_decl)
12498 /* Expanding a fixed parameter pack from
12499 coerce_template_parameter_pack. */
12500 t = tsubst_decl (pattern, args, complain);
12501 else if (pattern == error_mark_node)
12502 t = error_mark_node;
12503 else if (!TYPE_P (pattern))
12504 t = tsubst_expr (pattern, args, complain, in_decl,
12505 /*integral_constant_expression_p=*/false);
12506 else
12507 t = tsubst (pattern, args, complain, in_decl);
12508
12509 /* If the Ith argument pack element is a pack expansion, then
12510 the Ith element resulting from the substituting is going to
12511 be a pack expansion as well. */
12512 if (ith_elem_is_expansion)
12513 t = make_pack_expansion (t, complain);
12514
12515 return t;
12516 }
12517
12518 /* When the unexpanded parameter pack in a fold expression expands to an empty
12519 sequence, the value of the expression is as follows; the program is
12520 ill-formed if the operator is not listed in this table.
12521
12522 && true
12523 || false
12524 , void() */
12525
12526 tree
12527 expand_empty_fold (tree t, tsubst_flags_t complain)
12528 {
12529 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12530 if (!FOLD_EXPR_MODIFY_P (t))
12531 switch (code)
12532 {
12533 case TRUTH_ANDIF_EXPR:
12534 return boolean_true_node;
12535 case TRUTH_ORIF_EXPR:
12536 return boolean_false_node;
12537 case COMPOUND_EXPR:
12538 return void_node;
12539 default:
12540 break;
12541 }
12542
12543 if (complain & tf_error)
12544 error_at (location_of (t),
12545 "fold of empty expansion over %O", code);
12546 return error_mark_node;
12547 }
12548
12549 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12550 form an expression that combines the two terms using the
12551 operator of T. */
12552
12553 static tree
12554 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12555 {
12556 tree op = FOLD_EXPR_OP (t);
12557 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12558
12559 // Handle compound assignment operators.
12560 if (FOLD_EXPR_MODIFY_P (t))
12561 return build_x_modify_expr (input_location, left, code, right, complain);
12562
12563 warning_sentinel s(warn_parentheses);
12564 switch (code)
12565 {
12566 case COMPOUND_EXPR:
12567 return build_x_compound_expr (input_location, left, right, complain);
12568 default:
12569 return build_x_binary_op (input_location, code,
12570 left, TREE_CODE (left),
12571 right, TREE_CODE (right),
12572 /*overload=*/NULL,
12573 complain);
12574 }
12575 }
12576
12577 /* Substitute ARGS into the pack of a fold expression T. */
12578
12579 static inline tree
12580 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12581 {
12582 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12583 }
12584
12585 /* Substitute ARGS into the pack of a fold expression T. */
12586
12587 static inline tree
12588 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12589 {
12590 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12591 }
12592
12593 /* Expand a PACK of arguments into a grouped as left fold.
12594 Given a pack containing elements A0, A1, ..., An and an
12595 operator @, this builds the expression:
12596
12597 ((A0 @ A1) @ A2) ... @ An
12598
12599 Note that PACK must not be empty.
12600
12601 The operator is defined by the original fold expression T. */
12602
12603 static tree
12604 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12605 {
12606 tree left = TREE_VEC_ELT (pack, 0);
12607 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12608 {
12609 tree right = TREE_VEC_ELT (pack, i);
12610 left = fold_expression (t, left, right, complain);
12611 }
12612 return left;
12613 }
12614
12615 /* Substitute into a unary left fold expression. */
12616
12617 static tree
12618 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12619 tree in_decl)
12620 {
12621 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12622 if (pack == error_mark_node)
12623 return error_mark_node;
12624 if (PACK_EXPANSION_P (pack))
12625 {
12626 tree r = copy_node (t);
12627 FOLD_EXPR_PACK (r) = pack;
12628 return r;
12629 }
12630 if (TREE_VEC_LENGTH (pack) == 0)
12631 return expand_empty_fold (t, complain);
12632 else
12633 return expand_left_fold (t, pack, complain);
12634 }
12635
12636 /* Substitute into a binary left fold expression.
12637
12638 Do ths by building a single (non-empty) vector of argumnts and
12639 building the expression from those elements. */
12640
12641 static tree
12642 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12643 tree in_decl)
12644 {
12645 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12646 if (pack == error_mark_node)
12647 return error_mark_node;
12648 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12649 if (init == error_mark_node)
12650 return error_mark_node;
12651
12652 if (PACK_EXPANSION_P (pack))
12653 {
12654 tree r = copy_node (t);
12655 FOLD_EXPR_PACK (r) = pack;
12656 FOLD_EXPR_INIT (r) = init;
12657 return r;
12658 }
12659
12660 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12661 TREE_VEC_ELT (vec, 0) = init;
12662 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12663 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12664
12665 return expand_left_fold (t, vec, complain);
12666 }
12667
12668 /* Expand a PACK of arguments into a grouped as right fold.
12669 Given a pack containing elementns A0, A1, ..., and an
12670 operator @, this builds the expression:
12671
12672 A0@ ... (An-2 @ (An-1 @ An))
12673
12674 Note that PACK must not be empty.
12675
12676 The operator is defined by the original fold expression T. */
12677
12678 tree
12679 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12680 {
12681 // Build the expression.
12682 int n = TREE_VEC_LENGTH (pack);
12683 tree right = TREE_VEC_ELT (pack, n - 1);
12684 for (--n; n != 0; --n)
12685 {
12686 tree left = TREE_VEC_ELT (pack, n - 1);
12687 right = fold_expression (t, left, right, complain);
12688 }
12689 return right;
12690 }
12691
12692 /* Substitute into a unary right fold expression. */
12693
12694 static tree
12695 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12696 tree in_decl)
12697 {
12698 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12699 if (pack == error_mark_node)
12700 return error_mark_node;
12701 if (PACK_EXPANSION_P (pack))
12702 {
12703 tree r = copy_node (t);
12704 FOLD_EXPR_PACK (r) = pack;
12705 return r;
12706 }
12707 if (TREE_VEC_LENGTH (pack) == 0)
12708 return expand_empty_fold (t, complain);
12709 else
12710 return expand_right_fold (t, pack, complain);
12711 }
12712
12713 /* Substitute into a binary right fold expression.
12714
12715 Do ths by building a single (non-empty) vector of arguments and
12716 building the expression from those elements. */
12717
12718 static tree
12719 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12720 tree in_decl)
12721 {
12722 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12723 if (pack == error_mark_node)
12724 return error_mark_node;
12725 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12726 if (init == error_mark_node)
12727 return error_mark_node;
12728
12729 if (PACK_EXPANSION_P (pack))
12730 {
12731 tree r = copy_node (t);
12732 FOLD_EXPR_PACK (r) = pack;
12733 FOLD_EXPR_INIT (r) = init;
12734 return r;
12735 }
12736
12737 int n = TREE_VEC_LENGTH (pack);
12738 tree vec = make_tree_vec (n + 1);
12739 for (int i = 0; i < n; ++i)
12740 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12741 TREE_VEC_ELT (vec, n) = init;
12742
12743 return expand_right_fold (t, vec, complain);
12744 }
12745
12746 /* Walk through the pattern of a pack expansion, adding everything in
12747 local_specializations to a list. */
12748
12749 class el_data
12750 {
12751 public:
12752 hash_set<tree> internal;
12753 tree extra;
12754 tsubst_flags_t complain;
12755
12756 el_data (tsubst_flags_t c)
12757 : extra (NULL_TREE), complain (c) {}
12758 };
12759 static tree
12760 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12761 {
12762 el_data &data = *reinterpret_cast<el_data*>(data_);
12763 tree *extra = &data.extra;
12764 tsubst_flags_t complain = data.complain;
12765
12766 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12767 /* Remember local typedefs (85214). */
12768 tp = &TYPE_NAME (*tp);
12769
12770 if (TREE_CODE (*tp) == DECL_EXPR)
12771 data.internal.add (DECL_EXPR_DECL (*tp));
12772 else if (tree spec = retrieve_local_specialization (*tp))
12773 {
12774 if (data.internal.contains (*tp))
12775 /* Don't mess with variables declared within the pattern. */
12776 return NULL_TREE;
12777 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12778 {
12779 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12780 tree args = ARGUMENT_PACK_ARGS (spec);
12781 if (TREE_VEC_LENGTH (args) == 1)
12782 {
12783 tree elt = TREE_VEC_ELT (args, 0);
12784 if (PACK_EXPANSION_P (elt))
12785 elt = PACK_EXPANSION_PATTERN (elt);
12786 if (DECL_PACK_P (elt))
12787 spec = elt;
12788 }
12789 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12790 {
12791 /* Handle lambda capture here, since we aren't doing any
12792 substitution now, and so tsubst_copy won't call
12793 process_outer_var_ref. */
12794 tree args = ARGUMENT_PACK_ARGS (spec);
12795 int len = TREE_VEC_LENGTH (args);
12796 for (int i = 0; i < len; ++i)
12797 {
12798 tree arg = TREE_VEC_ELT (args, i);
12799 tree carg = arg;
12800 if (outer_automatic_var_p (arg))
12801 carg = process_outer_var_ref (arg, complain);
12802 if (carg != arg)
12803 {
12804 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12805 proxies. */
12806 if (i == 0)
12807 {
12808 spec = copy_node (spec);
12809 args = copy_node (args);
12810 SET_ARGUMENT_PACK_ARGS (spec, args);
12811 register_local_specialization (spec, *tp);
12812 }
12813 TREE_VEC_ELT (args, i) = carg;
12814 }
12815 }
12816 }
12817 }
12818 if (outer_automatic_var_p (spec))
12819 spec = process_outer_var_ref (spec, complain);
12820 *extra = tree_cons (*tp, spec, *extra);
12821 }
12822 return NULL_TREE;
12823 }
12824 static tree
12825 extract_local_specs (tree pattern, tsubst_flags_t complain)
12826 {
12827 el_data data (complain);
12828 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12829 return data.extra;
12830 }
12831
12832 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12833 for use in PACK_EXPANSION_EXTRA_ARGS. */
12834
12835 tree
12836 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12837 {
12838 tree extra = args;
12839 if (local_specializations)
12840 if (tree locals = extract_local_specs (pattern, complain))
12841 extra = tree_cons (NULL_TREE, extra, locals);
12842 return extra;
12843 }
12844
12845 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12846 normal template args to ARGS. */
12847
12848 tree
12849 add_extra_args (tree extra, tree args)
12850 {
12851 if (extra && TREE_CODE (extra) == TREE_LIST)
12852 {
12853 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12854 {
12855 /* The partial instantiation involved local declarations collected in
12856 extract_local_specs; map from the general template to our local
12857 context. */
12858 tree gen = TREE_PURPOSE (elt);
12859 tree inst = TREE_VALUE (elt);
12860 if (DECL_P (inst))
12861 if (tree local = retrieve_local_specialization (inst))
12862 inst = local;
12863 /* else inst is already a full instantiation of the pack. */
12864 register_local_specialization (inst, gen);
12865 }
12866 gcc_assert (!TREE_PURPOSE (extra));
12867 extra = TREE_VALUE (extra);
12868 }
12869 #if 1
12870 /* I think we should always be able to substitute dependent args into the
12871 pattern. If that turns out to be incorrect in some cases, enable the
12872 alternate code (and add complain/in_decl parms to this function). */
12873 gcc_checking_assert (!uses_template_parms (extra));
12874 #else
12875 if (!uses_template_parms (extra))
12876 {
12877 gcc_unreachable ();
12878 extra = tsubst_template_args (extra, args, complain, in_decl);
12879 args = add_outermost_template_args (args, extra);
12880 }
12881 else
12882 #endif
12883 args = add_to_template_args (extra, args);
12884 return args;
12885 }
12886
12887 /* Substitute ARGS into T, which is an pack expansion
12888 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12889 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12890 (if only a partial substitution could be performed) or
12891 ERROR_MARK_NODE if there was an error. */
12892 tree
12893 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12894 tree in_decl)
12895 {
12896 tree pattern;
12897 tree pack, packs = NULL_TREE;
12898 bool unsubstituted_packs = false;
12899 int i, len = -1;
12900 tree result;
12901 bool need_local_specializations = false;
12902 int levels;
12903
12904 gcc_assert (PACK_EXPANSION_P (t));
12905 pattern = PACK_EXPANSION_PATTERN (t);
12906
12907 /* Add in any args remembered from an earlier partial instantiation. */
12908 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12909
12910 levels = TMPL_ARGS_DEPTH (args);
12911
12912 /* Determine the argument packs that will instantiate the parameter
12913 packs used in the expansion expression. While we're at it,
12914 compute the number of arguments to be expanded and make sure it
12915 is consistent. */
12916 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12917 pack = TREE_CHAIN (pack))
12918 {
12919 tree parm_pack = TREE_VALUE (pack);
12920 tree arg_pack = NULL_TREE;
12921 tree orig_arg = NULL_TREE;
12922 int level = 0;
12923
12924 if (TREE_CODE (parm_pack) == BASES)
12925 {
12926 gcc_assert (parm_pack == pattern);
12927 if (BASES_DIRECT (parm_pack))
12928 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12929 args, complain,
12930 in_decl, false),
12931 complain);
12932 else
12933 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12934 args, complain, in_decl,
12935 false), complain);
12936 }
12937 else if (builtin_pack_call_p (parm_pack))
12938 {
12939 if (parm_pack != pattern)
12940 {
12941 if (complain & tf_error)
12942 sorry ("%qE is not the entire pattern of the pack expansion",
12943 parm_pack);
12944 return error_mark_node;
12945 }
12946 return expand_builtin_pack_call (parm_pack, args,
12947 complain, in_decl);
12948 }
12949 else if (TREE_CODE (parm_pack) == PARM_DECL)
12950 {
12951 /* We know we have correct local_specializations if this
12952 expansion is at function scope, or if we're dealing with a
12953 local parameter in a requires expression; for the latter,
12954 tsubst_requires_expr set it up appropriately. */
12955 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12956 arg_pack = retrieve_local_specialization (parm_pack);
12957 else
12958 /* We can't rely on local_specializations for a parameter
12959 name used later in a function declaration (such as in a
12960 late-specified return type). Even if it exists, it might
12961 have the wrong value for a recursive call. */
12962 need_local_specializations = true;
12963
12964 if (!arg_pack)
12965 {
12966 /* This parameter pack was used in an unevaluated context. Just
12967 make a dummy decl, since it's only used for its type. */
12968 ++cp_unevaluated_operand;
12969 arg_pack = tsubst_decl (parm_pack, args, complain);
12970 --cp_unevaluated_operand;
12971 if (arg_pack && DECL_PACK_P (arg_pack))
12972 /* Partial instantiation of the parm_pack, we can't build
12973 up an argument pack yet. */
12974 arg_pack = NULL_TREE;
12975 else
12976 arg_pack = make_fnparm_pack (arg_pack);
12977 }
12978 else if (DECL_PACK_P (arg_pack))
12979 /* This argument pack isn't fully instantiated yet. */
12980 arg_pack = NULL_TREE;
12981 }
12982 else if (is_capture_proxy (parm_pack))
12983 {
12984 arg_pack = retrieve_local_specialization (parm_pack);
12985 if (DECL_PACK_P (arg_pack))
12986 arg_pack = NULL_TREE;
12987 }
12988 else
12989 {
12990 int idx;
12991 template_parm_level_and_index (parm_pack, &level, &idx);
12992 if (level <= levels)
12993 arg_pack = TMPL_ARG (args, level, idx);
12994
12995 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12996 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12997 arg_pack = NULL_TREE;
12998 }
12999
13000 orig_arg = arg_pack;
13001 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13002 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13003
13004 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13005 /* This can only happen if we forget to expand an argument
13006 pack somewhere else. Just return an error, silently. */
13007 {
13008 result = make_tree_vec (1);
13009 TREE_VEC_ELT (result, 0) = error_mark_node;
13010 return result;
13011 }
13012
13013 if (arg_pack)
13014 {
13015 int my_len =
13016 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13017
13018 /* Don't bother trying to do a partial substitution with
13019 incomplete packs; we'll try again after deduction. */
13020 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13021 return t;
13022
13023 if (len < 0)
13024 len = my_len;
13025 else if (len != my_len)
13026 {
13027 if (!(complain & tf_error))
13028 /* Fail quietly. */;
13029 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13030 error ("mismatched argument pack lengths while expanding %qT",
13031 pattern);
13032 else
13033 error ("mismatched argument pack lengths while expanding %qE",
13034 pattern);
13035 return error_mark_node;
13036 }
13037
13038 /* Keep track of the parameter packs and their corresponding
13039 argument packs. */
13040 packs = tree_cons (parm_pack, arg_pack, packs);
13041 TREE_TYPE (packs) = orig_arg;
13042 }
13043 else
13044 {
13045 /* We can't substitute for this parameter pack. We use a flag as
13046 well as the missing_level counter because function parameter
13047 packs don't have a level. */
13048 gcc_assert (processing_template_decl || is_auto (parm_pack));
13049 unsubstituted_packs = true;
13050 }
13051 }
13052
13053 /* If the expansion is just T..., return the matching argument pack, unless
13054 we need to call convert_from_reference on all the elements. This is an
13055 important optimization; see c++/68422. */
13056 if (!unsubstituted_packs
13057 && TREE_PURPOSE (packs) == pattern)
13058 {
13059 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13060
13061 /* If the argument pack is a single pack expansion, pull it out. */
13062 if (TREE_VEC_LENGTH (args) == 1
13063 && pack_expansion_args_count (args))
13064 return TREE_VEC_ELT (args, 0);
13065
13066 /* Types need no adjustment, nor does sizeof..., and if we still have
13067 some pack expansion args we won't do anything yet. */
13068 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13069 || PACK_EXPANSION_SIZEOF_P (t)
13070 || pack_expansion_args_count (args))
13071 return args;
13072 /* Also optimize expression pack expansions if we can tell that the
13073 elements won't have reference type. */
13074 tree type = TREE_TYPE (pattern);
13075 if (type && !TYPE_REF_P (type)
13076 && !PACK_EXPANSION_P (type)
13077 && !WILDCARD_TYPE_P (type))
13078 return args;
13079 /* Otherwise use the normal path so we get convert_from_reference. */
13080 }
13081
13082 /* We cannot expand this expansion expression, because we don't have
13083 all of the argument packs we need. */
13084 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13085 {
13086 /* We got some full packs, but we can't substitute them in until we
13087 have values for all the packs. So remember these until then. */
13088
13089 t = make_pack_expansion (pattern, complain);
13090 PACK_EXPANSION_EXTRA_ARGS (t)
13091 = build_extra_args (pattern, args, complain);
13092 return t;
13093 }
13094
13095 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13096 type, so create our own local specializations map; the current map is
13097 either NULL or (in the case of recursive unification) might have
13098 bindings that we don't want to use or alter. */
13099 local_specialization_stack lss (need_local_specializations
13100 ? lss_blank : lss_nop);
13101
13102 if (unsubstituted_packs)
13103 {
13104 /* There were no real arguments, we're just replacing a parameter
13105 pack with another version of itself. Substitute into the
13106 pattern and return a PACK_EXPANSION_*. The caller will need to
13107 deal with that. */
13108 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13109 t = tsubst_expr (pattern, args, complain, in_decl,
13110 /*integral_constant_expression_p=*/false);
13111 else
13112 t = tsubst (pattern, args, complain, in_decl);
13113 t = make_pack_expansion (t, complain);
13114 return t;
13115 }
13116
13117 gcc_assert (len >= 0);
13118
13119 /* For each argument in each argument pack, substitute into the
13120 pattern. */
13121 result = make_tree_vec (len);
13122 tree elem_args = copy_template_args (args);
13123 for (i = 0; i < len; ++i)
13124 {
13125 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13126 i,
13127 elem_args, complain,
13128 in_decl);
13129 TREE_VEC_ELT (result, i) = t;
13130 if (t == error_mark_node)
13131 {
13132 result = error_mark_node;
13133 break;
13134 }
13135 }
13136
13137 /* Update ARGS to restore the substitution from parameter packs to
13138 their argument packs. */
13139 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13140 {
13141 tree parm = TREE_PURPOSE (pack);
13142
13143 if (TREE_CODE (parm) == PARM_DECL
13144 || VAR_P (parm)
13145 || TREE_CODE (parm) == FIELD_DECL)
13146 register_local_specialization (TREE_TYPE (pack), parm);
13147 else
13148 {
13149 int idx, level;
13150
13151 if (TREE_VALUE (pack) == NULL_TREE)
13152 continue;
13153
13154 template_parm_level_and_index (parm, &level, &idx);
13155
13156 /* Update the corresponding argument. */
13157 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13158 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13159 TREE_TYPE (pack);
13160 else
13161 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13162 }
13163 }
13164
13165 /* If the dependent pack arguments were such that we end up with only a
13166 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13167 if (len == 1 && TREE_CODE (result) == TREE_VEC
13168 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13169 return TREE_VEC_ELT (result, 0);
13170
13171 return result;
13172 }
13173
13174 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13175 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13176 parameter packs; all parms generated from a function parameter pack will
13177 have the same DECL_PARM_INDEX. */
13178
13179 tree
13180 get_pattern_parm (tree parm, tree tmpl)
13181 {
13182 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13183 tree patparm;
13184
13185 if (DECL_ARTIFICIAL (parm))
13186 {
13187 for (patparm = DECL_ARGUMENTS (pattern);
13188 patparm; patparm = DECL_CHAIN (patparm))
13189 if (DECL_ARTIFICIAL (patparm)
13190 && DECL_NAME (parm) == DECL_NAME (patparm))
13191 break;
13192 }
13193 else
13194 {
13195 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13196 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13197 gcc_assert (DECL_PARM_INDEX (patparm)
13198 == DECL_PARM_INDEX (parm));
13199 }
13200
13201 return patparm;
13202 }
13203
13204 /* Make an argument pack out of the TREE_VEC VEC. */
13205
13206 static tree
13207 make_argument_pack (tree vec)
13208 {
13209 tree pack;
13210
13211 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13212 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13213 else
13214 {
13215 pack = make_node (NONTYPE_ARGUMENT_PACK);
13216 TREE_CONSTANT (pack) = 1;
13217 }
13218 SET_ARGUMENT_PACK_ARGS (pack, vec);
13219 return pack;
13220 }
13221
13222 /* Return an exact copy of template args T that can be modified
13223 independently. */
13224
13225 static tree
13226 copy_template_args (tree t)
13227 {
13228 if (t == error_mark_node)
13229 return t;
13230
13231 int len = TREE_VEC_LENGTH (t);
13232 tree new_vec = make_tree_vec (len);
13233
13234 for (int i = 0; i < len; ++i)
13235 {
13236 tree elt = TREE_VEC_ELT (t, i);
13237 if (elt && TREE_CODE (elt) == TREE_VEC)
13238 elt = copy_template_args (elt);
13239 TREE_VEC_ELT (new_vec, i) = elt;
13240 }
13241
13242 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13243 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13244
13245 return new_vec;
13246 }
13247
13248 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13249
13250 tree
13251 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13252 tree in_decl)
13253 {
13254 /* Substitute into each of the arguments. */
13255 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13256 args, complain, in_decl);
13257 tree new_arg = error_mark_node;
13258 if (pack_args != error_mark_node)
13259 {
13260 if (TYPE_P (orig_arg))
13261 {
13262 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13263 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13264 }
13265 else
13266 {
13267 new_arg = make_node (TREE_CODE (orig_arg));
13268 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13269 }
13270
13271 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13272 }
13273
13274 return new_arg;
13275 }
13276
13277 /* Substitute ARGS into the vector or list of template arguments T. */
13278
13279 tree
13280 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13281 {
13282 tree orig_t = t;
13283 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13284 tree *elts;
13285
13286 if (t == error_mark_node)
13287 return error_mark_node;
13288
13289 len = TREE_VEC_LENGTH (t);
13290 elts = XALLOCAVEC (tree, len);
13291
13292 for (i = 0; i < len; i++)
13293 {
13294 tree orig_arg = TREE_VEC_ELT (t, i);
13295 tree new_arg;
13296
13297 if (TREE_CODE (orig_arg) == TREE_VEC)
13298 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13299 else if (PACK_EXPANSION_P (orig_arg))
13300 {
13301 /* Substitute into an expansion expression. */
13302 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13303
13304 if (TREE_CODE (new_arg) == TREE_VEC)
13305 /* Add to the expanded length adjustment the number of
13306 expanded arguments. We subtract one from this
13307 measurement, because the argument pack expression
13308 itself is already counted as 1 in
13309 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13310 the argument pack is empty. */
13311 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13312 }
13313 else if (ARGUMENT_PACK_P (orig_arg))
13314 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13315 else
13316 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13317
13318 if (new_arg == error_mark_node)
13319 return error_mark_node;
13320
13321 elts[i] = new_arg;
13322 if (new_arg != orig_arg)
13323 need_new = 1;
13324 }
13325
13326 if (!need_new)
13327 return t;
13328
13329 /* Make space for the expanded arguments coming from template
13330 argument packs. */
13331 t = make_tree_vec (len + expanded_len_adjust);
13332 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13333 arguments for a member template.
13334 In that case each TREE_VEC in ORIG_T represents a level of template
13335 arguments, and ORIG_T won't carry any non defaulted argument count.
13336 It will rather be the nested TREE_VECs that will carry one.
13337 In other words, ORIG_T carries a non defaulted argument count only
13338 if it doesn't contain any nested TREE_VEC. */
13339 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13340 {
13341 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13342 count += expanded_len_adjust;
13343 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13344 }
13345 for (i = 0, out = 0; i < len; i++)
13346 {
13347 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13348 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13349 && TREE_CODE (elts[i]) == TREE_VEC)
13350 {
13351 int idx;
13352
13353 /* Now expand the template argument pack "in place". */
13354 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13355 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13356 }
13357 else
13358 {
13359 TREE_VEC_ELT (t, out) = elts[i];
13360 out++;
13361 }
13362 }
13363
13364 return t;
13365 }
13366
13367 /* Substitute ARGS into one level PARMS of template parameters. */
13368
13369 static tree
13370 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13371 {
13372 if (parms == error_mark_node)
13373 return error_mark_node;
13374
13375 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13376
13377 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13378 {
13379 tree tuple = TREE_VEC_ELT (parms, i);
13380
13381 if (tuple == error_mark_node)
13382 continue;
13383
13384 TREE_VEC_ELT (new_vec, i) =
13385 tsubst_template_parm (tuple, args, complain);
13386 }
13387
13388 return new_vec;
13389 }
13390
13391 /* Return the result of substituting ARGS into the template parameters
13392 given by PARMS. If there are m levels of ARGS and m + n levels of
13393 PARMS, then the result will contain n levels of PARMS. For
13394 example, if PARMS is `template <class T> template <class U>
13395 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13396 result will be `template <int*, double, class V>'. */
13397
13398 static tree
13399 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13400 {
13401 tree r = NULL_TREE;
13402 tree* new_parms;
13403
13404 /* When substituting into a template, we must set
13405 PROCESSING_TEMPLATE_DECL as the template parameters may be
13406 dependent if they are based on one-another, and the dependency
13407 predicates are short-circuit outside of templates. */
13408 ++processing_template_decl;
13409
13410 for (new_parms = &r;
13411 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13412 new_parms = &(TREE_CHAIN (*new_parms)),
13413 parms = TREE_CHAIN (parms))
13414 {
13415 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13416 args, complain);
13417 *new_parms =
13418 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13419 - TMPL_ARGS_DEPTH (args)),
13420 new_vec, NULL_TREE);
13421 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13422 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13423 }
13424
13425 --processing_template_decl;
13426
13427 return r;
13428 }
13429
13430 /* Return the result of substituting ARGS into one template parameter
13431 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13432 parameter and which TREE_PURPOSE is the default argument of the
13433 template parameter. */
13434
13435 static tree
13436 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13437 {
13438 tree default_value, parm_decl;
13439
13440 if (args == NULL_TREE
13441 || t == NULL_TREE
13442 || t == error_mark_node)
13443 return t;
13444
13445 gcc_assert (TREE_CODE (t) == TREE_LIST);
13446
13447 default_value = TREE_PURPOSE (t);
13448 parm_decl = TREE_VALUE (t);
13449 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13450
13451 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13452 if (TREE_CODE (parm_decl) == PARM_DECL
13453 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13454 parm_decl = error_mark_node;
13455 default_value = tsubst_template_arg (default_value, args,
13456 complain, NULL_TREE);
13457 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13458
13459 tree r = build_tree_list (default_value, parm_decl);
13460 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13461 return r;
13462 }
13463
13464 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13465 type T. If T is not an aggregate or enumeration type, it is
13466 handled as if by tsubst. IN_DECL is as for tsubst. If
13467 ENTERING_SCOPE is nonzero, T is the context for a template which
13468 we are presently tsubst'ing. Return the substituted value. */
13469
13470 static tree
13471 tsubst_aggr_type (tree t,
13472 tree args,
13473 tsubst_flags_t complain,
13474 tree in_decl,
13475 int entering_scope)
13476 {
13477 if (t == NULL_TREE)
13478 return NULL_TREE;
13479
13480 switch (TREE_CODE (t))
13481 {
13482 case RECORD_TYPE:
13483 if (TYPE_PTRMEMFUNC_P (t))
13484 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13485
13486 /* Fall through. */
13487 case ENUMERAL_TYPE:
13488 case UNION_TYPE:
13489 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13490 {
13491 tree argvec;
13492 tree context;
13493 tree r;
13494
13495 /* In "sizeof(X<I>)" we need to evaluate "I". */
13496 cp_evaluated ev;
13497
13498 /* First, determine the context for the type we are looking
13499 up. */
13500 context = TYPE_CONTEXT (t);
13501 if (context && TYPE_P (context))
13502 {
13503 context = tsubst_aggr_type (context, args, complain,
13504 in_decl, /*entering_scope=*/1);
13505 /* If context is a nested class inside a class template,
13506 it may still need to be instantiated (c++/33959). */
13507 context = complete_type (context);
13508 }
13509
13510 /* Then, figure out what arguments are appropriate for the
13511 type we are trying to find. For example, given:
13512
13513 template <class T> struct S;
13514 template <class T, class U> void f(T, U) { S<U> su; }
13515
13516 and supposing that we are instantiating f<int, double>,
13517 then our ARGS will be {int, double}, but, when looking up
13518 S we only want {double}. */
13519 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13520 complain, in_decl);
13521 if (argvec == error_mark_node)
13522 r = error_mark_node;
13523 else if (!entering_scope
13524 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13525 {
13526 /* See maybe_dependent_member_ref. */
13527 tree name = TYPE_IDENTIFIER (t);
13528 tree fullname = name;
13529 if (instantiates_primary_template_p (t))
13530 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13531 INNERMOST_TEMPLATE_ARGS (argvec));
13532 return build_typename_type (context, name, fullname,
13533 typename_type);
13534 }
13535 else
13536 {
13537 r = lookup_template_class (t, argvec, in_decl, context,
13538 entering_scope, complain);
13539 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13540 }
13541
13542 return r;
13543 }
13544 else
13545 /* This is not a template type, so there's nothing to do. */
13546 return t;
13547
13548 default:
13549 return tsubst (t, args, complain, in_decl);
13550 }
13551 }
13552
13553 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13554
13555 /* Substitute into the default argument ARG (a default argument for
13556 FN), which has the indicated TYPE. */
13557
13558 tree
13559 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13560 tsubst_flags_t complain)
13561 {
13562 int errs = errorcount + sorrycount;
13563
13564 /* This can happen in invalid code. */
13565 if (TREE_CODE (arg) == DEFERRED_PARSE)
13566 return arg;
13567
13568 /* Shortcut {}. */
13569 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13570 && CONSTRUCTOR_NELTS (arg) == 0)
13571 return arg;
13572
13573 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13574 parm = chain_index (parmnum, parm);
13575 tree parmtype = TREE_TYPE (parm);
13576 if (DECL_BY_REFERENCE (parm))
13577 parmtype = TREE_TYPE (parmtype);
13578 if (parmtype == error_mark_node)
13579 return error_mark_node;
13580
13581 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13582
13583 tree *slot;
13584 if (defarg_inst && (slot = defarg_inst->get (parm)))
13585 return *slot;
13586
13587 /* This default argument came from a template. Instantiate the
13588 default argument here, not in tsubst. In the case of
13589 something like:
13590
13591 template <class T>
13592 struct S {
13593 static T t();
13594 void f(T = t());
13595 };
13596
13597 we must be careful to do name lookup in the scope of S<T>,
13598 rather than in the current class. */
13599 push_to_top_level ();
13600 push_access_scope (fn);
13601 push_deferring_access_checks (dk_no_deferred);
13602 start_lambda_scope (parm);
13603
13604 /* The default argument expression may cause implicitly defined
13605 member functions to be synthesized, which will result in garbage
13606 collection. We must treat this situation as if we were within
13607 the body of function so as to avoid collecting live data on the
13608 stack. */
13609 ++function_depth;
13610 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13611 complain, NULL_TREE,
13612 /*integral_constant_expression_p=*/false);
13613 --function_depth;
13614
13615 finish_lambda_scope ();
13616
13617 /* Make sure the default argument is reasonable. */
13618 arg = check_default_argument (type, arg, complain);
13619
13620 if (errorcount+sorrycount > errs
13621 && (complain & tf_warning_or_error))
13622 inform (input_location,
13623 " when instantiating default argument for call to %qD", fn);
13624
13625 pop_deferring_access_checks ();
13626 pop_access_scope (fn);
13627 pop_from_top_level ();
13628
13629 if (arg != error_mark_node && !cp_unevaluated_operand)
13630 {
13631 if (!defarg_inst)
13632 defarg_inst = decl_tree_cache_map::create_ggc (37);
13633 defarg_inst->put (parm, arg);
13634 }
13635
13636 return arg;
13637 }
13638
13639 /* Substitute into all the default arguments for FN. */
13640
13641 static void
13642 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13643 {
13644 tree arg;
13645 tree tmpl_args;
13646
13647 tmpl_args = DECL_TI_ARGS (fn);
13648
13649 /* If this function is not yet instantiated, we certainly don't need
13650 its default arguments. */
13651 if (uses_template_parms (tmpl_args))
13652 return;
13653 /* Don't do this again for clones. */
13654 if (DECL_CLONED_FUNCTION_P (fn))
13655 return;
13656
13657 int i = 0;
13658 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13659 arg;
13660 arg = TREE_CHAIN (arg), ++i)
13661 if (TREE_PURPOSE (arg))
13662 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13663 TREE_VALUE (arg),
13664 TREE_PURPOSE (arg),
13665 complain);
13666 }
13667
13668 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13669 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13670
13671 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13672
13673 void
13674 store_explicit_specifier (tree v, tree t)
13675 {
13676 if (!explicit_specifier_map)
13677 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13678 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13679 explicit_specifier_map->put (v, t);
13680 }
13681
13682 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13683
13684 static tree
13685 lookup_explicit_specifier (tree v)
13686 {
13687 return *explicit_specifier_map->get (v);
13688 }
13689
13690 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13691 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13692 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13693 identical to T. */
13694
13695 static tree
13696 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13697 tree raises, tsubst_flags_t complain)
13698 {
13699 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13700
13701 tree new_type;
13702 if (TREE_CODE (t) == FUNCTION_TYPE)
13703 {
13704 new_type = build_function_type (return_type, arg_types);
13705 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13706 }
13707 else
13708 {
13709 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13710 /* Don't pick up extra function qualifiers from the basetype. */
13711 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13712 if (! MAYBE_CLASS_TYPE_P (r))
13713 {
13714 /* [temp.deduct]
13715
13716 Type deduction may fail for any of the following
13717 reasons:
13718
13719 -- Attempting to create "pointer to member of T" when T
13720 is not a class type. */
13721 if (complain & tf_error)
13722 error ("creating pointer to member function of non-class type %qT",
13723 r);
13724 return error_mark_node;
13725 }
13726
13727 new_type = build_method_type_directly (r, return_type,
13728 TREE_CHAIN (arg_types));
13729 }
13730 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13731
13732 cp_ref_qualifier rqual = type_memfn_rqual (t);
13733 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13734 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13735 }
13736
13737 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13738 each of its formal parameters. If there is a disagreement then rebuild
13739 DECL's function type according to its formal parameter types, as part of a
13740 resolution for Core issues 1001/1322. */
13741
13742 static void
13743 maybe_rebuild_function_decl_type (tree decl)
13744 {
13745 bool function_type_needs_rebuilding = false;
13746 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13747 {
13748 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13749 while (parm_type_list && parm_type_list != void_list_node)
13750 {
13751 tree parm_type = TREE_VALUE (parm_type_list);
13752 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13753 if (!same_type_p (parm_type, formal_parm_type_unqual))
13754 {
13755 function_type_needs_rebuilding = true;
13756 break;
13757 }
13758
13759 parm_list = DECL_CHAIN (parm_list);
13760 parm_type_list = TREE_CHAIN (parm_type_list);
13761 }
13762 }
13763
13764 if (!function_type_needs_rebuilding)
13765 return;
13766
13767 const tree fntype = TREE_TYPE (decl);
13768 tree parm_list = DECL_ARGUMENTS (decl);
13769 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13770 tree new_parm_type_list = NULL_TREE;
13771 tree *q = &new_parm_type_list;
13772 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13773 {
13774 *q = copy_node (old_parm_type_list);
13775 parm_list = DECL_CHAIN (parm_list);
13776 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13777 q = &TREE_CHAIN (*q);
13778 }
13779 while (old_parm_type_list && old_parm_type_list != void_list_node)
13780 {
13781 *q = copy_node (old_parm_type_list);
13782 tree *new_parm_type = &TREE_VALUE (*q);
13783 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13784 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13785 *new_parm_type = formal_parm_type_unqual;
13786
13787 parm_list = DECL_CHAIN (parm_list);
13788 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13789 q = &TREE_CHAIN (*q);
13790 }
13791 if (old_parm_type_list == void_list_node)
13792 *q = void_list_node;
13793
13794 TREE_TYPE (decl)
13795 = rebuild_function_or_method_type (fntype,
13796 TREE_TYPE (fntype), new_parm_type_list,
13797 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13798 }
13799
13800 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13801
13802 static tree
13803 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13804 tree lambda_fntype)
13805 {
13806 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13807 hashval_t hash = 0;
13808 tree in_decl = t;
13809
13810 /* Nobody should be tsubst'ing into non-template functions. */
13811 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13812 || DECL_LOCAL_DECL_P (t));
13813
13814 if (DECL_LOCAL_DECL_P (t))
13815 {
13816 if (tree spec = retrieve_local_specialization (t))
13817 return spec;
13818 }
13819 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13820 {
13821 /* If T is not dependent, just return it. */
13822 if (!uses_template_parms (DECL_TI_ARGS (t))
13823 && !LAMBDA_FUNCTION_P (t))
13824 return t;
13825
13826 /* Calculate the most general template of which R is a
13827 specialization. */
13828 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13829
13830 /* We're substituting a lambda function under tsubst_lambda_expr but not
13831 directly from it; find the matching function we're already inside.
13832 But don't do this if T is a generic lambda with a single level of
13833 template parms, as in that case we're doing a normal instantiation. */
13834 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13835 && (!generic_lambda_fn_p (t)
13836 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13837 return enclosing_instantiation_of (t);
13838
13839 /* Calculate the complete set of arguments used to
13840 specialize R. */
13841 argvec = tsubst_template_args (DECL_TI_ARGS
13842 (DECL_TEMPLATE_RESULT
13843 (DECL_TI_TEMPLATE (t))),
13844 args, complain, in_decl);
13845 if (argvec == error_mark_node)
13846 return error_mark_node;
13847
13848 /* Check to see if we already have this specialization. */
13849 if (!lambda_fntype)
13850 {
13851 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13852 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13853 return spec;
13854 }
13855
13856 /* We can see more levels of arguments than parameters if
13857 there was a specialization of a member template, like
13858 this:
13859
13860 template <class T> struct S { template <class U> void f(); }
13861 template <> template <class U> void S<int>::f(U);
13862
13863 Here, we'll be substituting into the specialization,
13864 because that's where we can find the code we actually
13865 want to generate, but we'll have enough arguments for
13866 the most general template.
13867
13868 We also deal with the peculiar case:
13869
13870 template <class T> struct S {
13871 template <class U> friend void f();
13872 };
13873 template <class U> void f() {}
13874 template S<int>;
13875 template void f<double>();
13876
13877 Here, the ARGS for the instantiation of will be {int,
13878 double}. But, we only need as many ARGS as there are
13879 levels of template parameters in CODE_PATTERN. We are
13880 careful not to get fooled into reducing the ARGS in
13881 situations like:
13882
13883 template <class T> struct S { template <class U> void f(U); }
13884 template <class T> template <> void S<T>::f(int) {}
13885
13886 which we can spot because the pattern will be a
13887 specialization in this case. */
13888 int args_depth = TMPL_ARGS_DEPTH (args);
13889 int parms_depth =
13890 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13891
13892 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13893 args = get_innermost_template_args (args, parms_depth);
13894 }
13895 else
13896 {
13897 /* This special case arises when we have something like this:
13898
13899 template <class T> struct S {
13900 friend void f<int>(int, double);
13901 };
13902
13903 Here, the DECL_TI_TEMPLATE for the friend declaration
13904 will be an IDENTIFIER_NODE. We are being called from
13905 tsubst_friend_function, and we want only to create a
13906 new decl (R) with appropriate types so that we can call
13907 determine_specialization. */
13908 gen_tmpl = NULL_TREE;
13909 argvec = NULL_TREE;
13910 }
13911
13912 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13913 : NULL_TREE);
13914 tree ctx = closure ? closure : DECL_CONTEXT (t);
13915 bool member = ctx && TYPE_P (ctx);
13916
13917 if (member && !closure)
13918 ctx = tsubst_aggr_type (ctx, args,
13919 complain, t, /*entering_scope=*/1);
13920
13921 tree type = (lambda_fntype ? lambda_fntype
13922 : tsubst (TREE_TYPE (t), args,
13923 complain | tf_fndecl_type, in_decl));
13924 if (type == error_mark_node)
13925 return error_mark_node;
13926
13927 /* If we hit excessive deduction depth, the type is bogus even if
13928 it isn't error_mark_node, so don't build a decl. */
13929 if (excessive_deduction_depth)
13930 return error_mark_node;
13931
13932 /* We do NOT check for matching decls pushed separately at this
13933 point, as they may not represent instantiations of this
13934 template, and in any case are considered separate under the
13935 discrete model. */
13936 tree r = copy_decl (t);
13937 DECL_USE_TEMPLATE (r) = 0;
13938 TREE_TYPE (r) = type;
13939 /* Clear out the mangled name and RTL for the instantiation. */
13940 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13941 SET_DECL_RTL (r, NULL);
13942 /* Leave DECL_INITIAL set on deleted instantiations. */
13943 if (!DECL_DELETED_FN (r))
13944 DECL_INITIAL (r) = NULL_TREE;
13945 DECL_CONTEXT (r) = ctx;
13946 set_instantiating_module (r);
13947
13948 /* Handle explicit(dependent-expr). */
13949 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13950 {
13951 tree spec = lookup_explicit_specifier (t);
13952 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13953 /*function_p=*/false,
13954 /*i_c_e_p=*/true);
13955 spec = build_explicit_specifier (spec, complain);
13956 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13957 }
13958
13959 /* OpenMP UDRs have the only argument a reference to the declared
13960 type. We want to diagnose if the declared type is a reference,
13961 which is invalid, but as references to references are usually
13962 quietly merged, diagnose it here. */
13963 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13964 {
13965 tree argtype
13966 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13967 argtype = tsubst (argtype, args, complain, in_decl);
13968 if (TYPE_REF_P (argtype))
13969 error_at (DECL_SOURCE_LOCATION (t),
13970 "reference type %qT in "
13971 "%<#pragma omp declare reduction%>", argtype);
13972 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13973 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13974 argtype);
13975 }
13976
13977 if (member && DECL_CONV_FN_P (r))
13978 /* Type-conversion operator. Reconstruct the name, in
13979 case it's the name of one of the template's parameters. */
13980 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13981
13982 tree parms = DECL_ARGUMENTS (t);
13983 if (closure)
13984 parms = DECL_CHAIN (parms);
13985 parms = tsubst (parms, args, complain, t);
13986 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13987 DECL_CONTEXT (parm) = r;
13988 if (closure)
13989 {
13990 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13991 DECL_NAME (tparm) = closure_identifier;
13992 DECL_CHAIN (tparm) = parms;
13993 parms = tparm;
13994 }
13995 DECL_ARGUMENTS (r) = parms;
13996 DECL_RESULT (r) = NULL_TREE;
13997
13998 maybe_rebuild_function_decl_type (r);
13999
14000 TREE_STATIC (r) = 0;
14001 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14002 DECL_EXTERNAL (r) = 1;
14003 /* If this is an instantiation of a function with internal
14004 linkage, we already know what object file linkage will be
14005 assigned to the instantiation. */
14006 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14007 DECL_DEFER_OUTPUT (r) = 0;
14008 DECL_CHAIN (r) = NULL_TREE;
14009 DECL_PENDING_INLINE_INFO (r) = 0;
14010 DECL_PENDING_INLINE_P (r) = 0;
14011 DECL_SAVED_TREE (r) = NULL_TREE;
14012 DECL_STRUCT_FUNCTION (r) = NULL;
14013 TREE_USED (r) = 0;
14014 /* We'll re-clone as appropriate in instantiate_template. */
14015 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14016
14017 /* If we aren't complaining now, return on error before we register
14018 the specialization so that we'll complain eventually. */
14019 if ((complain & tf_error) == 0
14020 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14021 && !grok_op_properties (r, /*complain=*/false))
14022 return error_mark_node;
14023
14024 /* Associate the constraints directly with the instantiation. We
14025 don't substitute through the constraints; that's only done when
14026 they are checked. */
14027 if (tree ci = get_constraints (t))
14028 /* Unless we're regenerating a lambda, in which case we'll set the
14029 lambda's constraints in tsubst_lambda_expr. */
14030 if (!lambda_fntype)
14031 set_constraints (r, ci);
14032
14033 if (DECL_FRIEND_CONTEXT (t))
14034 SET_DECL_FRIEND_CONTEXT (r,
14035 tsubst (DECL_FRIEND_CONTEXT (t),
14036 args, complain, in_decl));
14037
14038 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14039 this in the special friend case mentioned above where
14040 GEN_TMPL is NULL. */
14041 if (gen_tmpl && !closure)
14042 {
14043 DECL_TEMPLATE_INFO (r)
14044 = build_template_info (gen_tmpl, argvec);
14045 SET_DECL_IMPLICIT_INSTANTIATION (r);
14046
14047 tree new_r
14048 = register_specialization (r, gen_tmpl, argvec, false, hash);
14049 if (new_r != r)
14050 /* We instantiated this while substituting into
14051 the type earlier (template/friend54.C). */
14052 return new_r;
14053
14054 /* We're not supposed to instantiate default arguments
14055 until they are called, for a template. But, for a
14056 declaration like:
14057
14058 template <class T> void f ()
14059 { extern void g(int i = T()); }
14060
14061 we should do the substitution when the template is
14062 instantiated. We handle the member function case in
14063 instantiate_class_template since the default arguments
14064 might refer to other members of the class. */
14065 if (!member
14066 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14067 && !uses_template_parms (argvec))
14068 tsubst_default_arguments (r, complain);
14069 }
14070 else if (DECL_LOCAL_DECL_P (r))
14071 {
14072 if (!cp_unevaluated_operand)
14073 register_local_specialization (r, t);
14074 }
14075 else
14076 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14077
14078 /* Copy the list of befriending classes. */
14079 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14080 *friends;
14081 friends = &TREE_CHAIN (*friends))
14082 {
14083 *friends = copy_node (*friends);
14084 TREE_VALUE (*friends)
14085 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14086 }
14087
14088 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14089 {
14090 maybe_retrofit_in_chrg (r);
14091 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14092 return error_mark_node;
14093 /* If this is an instantiation of a member template, clone it.
14094 If it isn't, that'll be handled by
14095 clone_constructors_and_destructors. */
14096 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14097 clone_cdtor (r, /*update_methods=*/false);
14098 }
14099 else if ((complain & tf_error) != 0
14100 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14101 && !grok_op_properties (r, /*complain=*/true))
14102 return error_mark_node;
14103
14104 /* Possibly limit visibility based on template args. */
14105 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14106 if (DECL_VISIBILITY_SPECIFIED (t))
14107 {
14108 DECL_VISIBILITY_SPECIFIED (r) = 0;
14109 DECL_ATTRIBUTES (r)
14110 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14111 }
14112 determine_visibility (r);
14113 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14114 && !processing_template_decl)
14115 defaulted_late_check (r);
14116
14117 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14118 args, complain, in_decl);
14119 if (flag_openmp)
14120 if (tree attr = lookup_attribute ("omp declare variant base",
14121 DECL_ATTRIBUTES (r)))
14122 omp_declare_variant_finalize (r, attr);
14123
14124 return r;
14125 }
14126
14127 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14128
14129 static tree
14130 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14131 tree lambda_fntype)
14132 {
14133 /* We can get here when processing a member function template,
14134 member class template, or template template parameter. */
14135 tree decl = DECL_TEMPLATE_RESULT (t);
14136 tree in_decl = t;
14137 tree spec;
14138 tree tmpl_args;
14139 tree full_args;
14140 tree r;
14141 hashval_t hash = 0;
14142
14143 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14144 {
14145 /* Template template parameter is treated here. */
14146 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14147 if (new_type == error_mark_node)
14148 r = error_mark_node;
14149 /* If we get a real template back, return it. This can happen in
14150 the context of most_specialized_partial_spec. */
14151 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14152 r = new_type;
14153 else
14154 /* The new TEMPLATE_DECL was built in
14155 reduce_template_parm_level. */
14156 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14157 return r;
14158 }
14159
14160 if (!lambda_fntype)
14161 {
14162 /* We might already have an instance of this template.
14163 The ARGS are for the surrounding class type, so the
14164 full args contain the tsubst'd args for the context,
14165 plus the innermost args from the template decl. */
14166 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14167 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14168 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14169 /* Because this is a template, the arguments will still be
14170 dependent, even after substitution. If
14171 PROCESSING_TEMPLATE_DECL is not set, the dependency
14172 predicates will short-circuit. */
14173 ++processing_template_decl;
14174 full_args = tsubst_template_args (tmpl_args, args,
14175 complain, in_decl);
14176 --processing_template_decl;
14177 if (full_args == error_mark_node)
14178 return error_mark_node;
14179
14180 /* If this is a default template template argument,
14181 tsubst might not have changed anything. */
14182 if (full_args == tmpl_args)
14183 return t;
14184
14185 hash = hash_tmpl_and_args (t, full_args);
14186 spec = retrieve_specialization (t, full_args, hash);
14187 if (spec != NULL_TREE)
14188 {
14189 if (TYPE_P (spec))
14190 /* Type partial instantiations are stored as the type by
14191 lookup_template_class_1, not here as the template. */
14192 spec = CLASSTYPE_TI_TEMPLATE (spec);
14193 return spec;
14194 }
14195 }
14196
14197 /* Make a new template decl. It will be similar to the
14198 original, but will record the current template arguments.
14199 We also create a new function declaration, which is just
14200 like the old one, but points to this new template, rather
14201 than the old one. */
14202 r = copy_decl (t);
14203 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14204 DECL_CHAIN (r) = NULL_TREE;
14205
14206 // Build new template info linking to the original template decl.
14207 if (!lambda_fntype)
14208 {
14209 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14210 SET_DECL_IMPLICIT_INSTANTIATION (r);
14211 }
14212 else
14213 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14214
14215 /* The template parameters for this new template are all the
14216 template parameters for the old template, except the
14217 outermost level of parameters. */
14218 DECL_TEMPLATE_PARMS (r)
14219 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14220 complain);
14221
14222 bool class_p = false;
14223 tree inner = decl;
14224 ++processing_template_decl;
14225 if (TREE_CODE (inner) == FUNCTION_DECL)
14226 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14227 else
14228 {
14229 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14230 {
14231 class_p = true;
14232 inner = TREE_TYPE (inner);
14233 }
14234 if (class_p)
14235 inner = tsubst_aggr_type (inner, args, complain,
14236 in_decl, /*entering*/1);
14237 else
14238 inner = tsubst (inner, args, complain, in_decl);
14239 }
14240 --processing_template_decl;
14241 if (inner == error_mark_node)
14242 return error_mark_node;
14243
14244 if (class_p)
14245 {
14246 /* For a partial specialization, we need to keep pointing to
14247 the primary template. */
14248 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14249 CLASSTYPE_TI_TEMPLATE (inner) = r;
14250
14251 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14252 inner = TYPE_MAIN_DECL (inner);
14253 }
14254 else if (lambda_fntype)
14255 {
14256 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14257 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14258 }
14259 else
14260 {
14261 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14262 DECL_TI_TEMPLATE (inner) = r;
14263 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14264 }
14265
14266 DECL_TEMPLATE_RESULT (r) = inner;
14267 TREE_TYPE (r) = TREE_TYPE (inner);
14268 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14269
14270 if (modules_p ())
14271 {
14272 /* Propagate module information from the decl. */
14273 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14274 if (DECL_LANG_SPECIFIC (inner))
14275 {
14276 DECL_MODULE_PURVIEW_P (r) = DECL_MODULE_PURVIEW_P (inner);
14277 /* If this is a constrained template, the above tsubst of
14278 inner can find the unconstrained template, which may have
14279 come from an import. This is ok, because we don't
14280 register this instantiation (see below). */
14281 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14282 || (TEMPLATE_PARMS_CONSTRAINTS
14283 (DECL_TEMPLATE_PARMS (t))));
14284 DECL_MODULE_IMPORT_P (r) = false;
14285 }
14286 }
14287
14288 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14289 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14290
14291 if (PRIMARY_TEMPLATE_P (t))
14292 DECL_PRIMARY_TEMPLATE (r) = r;
14293
14294 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14295 /* Record this non-type partial instantiation. */
14296 register_specialization (r, t,
14297 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14298 false, hash);
14299
14300 return r;
14301 }
14302
14303 /* True if FN is the op() for a lambda in an uninstantiated template. */
14304
14305 bool
14306 lambda_fn_in_template_p (tree fn)
14307 {
14308 if (!fn || !LAMBDA_FUNCTION_P (fn))
14309 return false;
14310 tree closure = DECL_CONTEXT (fn);
14311 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14312 }
14313
14314 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14315 which the above is true. */
14316
14317 bool
14318 instantiated_lambda_fn_p (tree fn)
14319 {
14320 if (!fn || !LAMBDA_FUNCTION_P (fn))
14321 return false;
14322 tree closure = DECL_CONTEXT (fn);
14323 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14324 return LAMBDA_EXPR_INSTANTIATED (lam);
14325 }
14326
14327 /* We're instantiating a variable from template function TCTX. Return the
14328 corresponding current enclosing scope. This gets complicated because lambda
14329 functions in templates are regenerated rather than instantiated, but generic
14330 lambda functions are subsequently instantiated. */
14331
14332 static tree
14333 enclosing_instantiation_of (tree otctx)
14334 {
14335 tree tctx = otctx;
14336 tree fn = current_function_decl;
14337 int lambda_count = 0;
14338
14339 for (; tctx && (lambda_fn_in_template_p (tctx)
14340 || instantiated_lambda_fn_p (tctx));
14341 tctx = decl_function_context (tctx))
14342 ++lambda_count;
14343 for (; fn; fn = decl_function_context (fn))
14344 {
14345 tree ofn = fn;
14346 int flambda_count = 0;
14347 for (; fn && instantiated_lambda_fn_p (fn);
14348 fn = decl_function_context (fn))
14349 ++flambda_count;
14350 if ((fn && DECL_TEMPLATE_INFO (fn))
14351 ? most_general_template (fn) != most_general_template (tctx)
14352 : fn != tctx)
14353 continue;
14354 if (flambda_count != lambda_count)
14355 {
14356 gcc_assert (flambda_count > lambda_count);
14357 for (; flambda_count > lambda_count; --flambda_count)
14358 ofn = decl_function_context (ofn);
14359 }
14360 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14361 || DECL_CONV_FN_P (ofn));
14362 return ofn;
14363 }
14364 gcc_unreachable ();
14365 }
14366
14367 /* Substitute the ARGS into the T, which is a _DECL. Return the
14368 result of the substitution. Issue error and warning messages under
14369 control of COMPLAIN. */
14370
14371 static tree
14372 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14373 {
14374 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14375 location_t saved_loc;
14376 tree r = NULL_TREE;
14377 tree in_decl = t;
14378 hashval_t hash = 0;
14379
14380 /* Set the filename and linenumber to improve error-reporting. */
14381 saved_loc = input_location;
14382 input_location = DECL_SOURCE_LOCATION (t);
14383
14384 switch (TREE_CODE (t))
14385 {
14386 case TEMPLATE_DECL:
14387 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14388 break;
14389
14390 case FUNCTION_DECL:
14391 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14392 break;
14393
14394 case PARM_DECL:
14395 {
14396 tree type = NULL_TREE;
14397 int i, len = 1;
14398 tree expanded_types = NULL_TREE;
14399 tree prev_r = NULL_TREE;
14400 tree first_r = NULL_TREE;
14401
14402 if (DECL_PACK_P (t))
14403 {
14404 /* If there is a local specialization that isn't a
14405 parameter pack, it means that we're doing a "simple"
14406 substitution from inside tsubst_pack_expansion. Just
14407 return the local specialization (which will be a single
14408 parm). */
14409 tree spec = retrieve_local_specialization (t);
14410 if (spec
14411 && TREE_CODE (spec) == PARM_DECL
14412 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14413 RETURN (spec);
14414
14415 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14416 the parameters in this function parameter pack. */
14417 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14418 complain, in_decl);
14419 if (TREE_CODE (expanded_types) == TREE_VEC)
14420 {
14421 len = TREE_VEC_LENGTH (expanded_types);
14422
14423 /* Zero-length parameter packs are boring. Just substitute
14424 into the chain. */
14425 if (len == 0 && !cp_unevaluated_operand)
14426 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14427 TREE_CHAIN (t)));
14428 }
14429 else
14430 {
14431 /* All we did was update the type. Make a note of that. */
14432 type = expanded_types;
14433 expanded_types = NULL_TREE;
14434 }
14435 }
14436
14437 /* Loop through all of the parameters we'll build. When T is
14438 a function parameter pack, LEN is the number of expanded
14439 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14440 r = NULL_TREE;
14441 for (i = 0; i < len; ++i)
14442 {
14443 prev_r = r;
14444 r = copy_node (t);
14445 if (DECL_TEMPLATE_PARM_P (t))
14446 SET_DECL_TEMPLATE_PARM_P (r);
14447
14448 if (expanded_types)
14449 /* We're on the Ith parameter of the function parameter
14450 pack. */
14451 {
14452 /* Get the Ith type. */
14453 type = TREE_VEC_ELT (expanded_types, i);
14454
14455 /* Rename the parameter to include the index. */
14456 DECL_NAME (r)
14457 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14458 }
14459 else if (!type)
14460 /* We're dealing with a normal parameter. */
14461 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14462
14463 type = type_decays_to (type);
14464 TREE_TYPE (r) = type;
14465 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14466
14467 if (DECL_INITIAL (r))
14468 {
14469 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14470 DECL_INITIAL (r) = TREE_TYPE (r);
14471 else
14472 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14473 complain, in_decl);
14474 }
14475
14476 DECL_CONTEXT (r) = NULL_TREE;
14477
14478 if (!DECL_TEMPLATE_PARM_P (r))
14479 DECL_ARG_TYPE (r) = type_passed_as (type);
14480
14481 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14482 args, complain, in_decl);
14483
14484 /* Keep track of the first new parameter we
14485 generate. That's what will be returned to the
14486 caller. */
14487 if (!first_r)
14488 first_r = r;
14489
14490 /* Build a proper chain of parameters when substituting
14491 into a function parameter pack. */
14492 if (prev_r)
14493 DECL_CHAIN (prev_r) = r;
14494 }
14495
14496 /* If cp_unevaluated_operand is set, we're just looking for a
14497 single dummy parameter, so don't keep going. */
14498 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14499 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14500 complain, DECL_CHAIN (t));
14501
14502 /* FIRST_R contains the start of the chain we've built. */
14503 r = first_r;
14504 }
14505 break;
14506
14507 case FIELD_DECL:
14508 {
14509 tree type = NULL_TREE;
14510 tree vec = NULL_TREE;
14511 tree expanded_types = NULL_TREE;
14512 int len = 1;
14513
14514 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14515 {
14516 /* This field is a lambda capture pack. Return a TREE_VEC of
14517 the expanded fields to instantiate_class_template_1. */
14518 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14519 complain, in_decl);
14520 if (TREE_CODE (expanded_types) == TREE_VEC)
14521 {
14522 len = TREE_VEC_LENGTH (expanded_types);
14523 vec = make_tree_vec (len);
14524 }
14525 else
14526 {
14527 /* All we did was update the type. Make a note of that. */
14528 type = expanded_types;
14529 expanded_types = NULL_TREE;
14530 }
14531 }
14532
14533 for (int i = 0; i < len; ++i)
14534 {
14535 r = copy_decl (t);
14536 if (expanded_types)
14537 {
14538 type = TREE_VEC_ELT (expanded_types, i);
14539 DECL_NAME (r)
14540 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14541 }
14542 else if (!type)
14543 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14544
14545 if (type == error_mark_node)
14546 RETURN (error_mark_node);
14547 TREE_TYPE (r) = type;
14548 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14549
14550 if (DECL_C_BIT_FIELD (r))
14551 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14552 number of bits. */
14553 DECL_BIT_FIELD_REPRESENTATIVE (r)
14554 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14555 complain, in_decl,
14556 /*integral_constant_expression_p=*/true);
14557 if (DECL_INITIAL (t))
14558 {
14559 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14560 NSDMI in perform_member_init. Still set DECL_INITIAL
14561 so that we know there is one. */
14562 DECL_INITIAL (r) = void_node;
14563 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14564 retrofit_lang_decl (r);
14565 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14566 }
14567 /* We don't have to set DECL_CONTEXT here; it is set by
14568 finish_member_declaration. */
14569 DECL_CHAIN (r) = NULL_TREE;
14570
14571 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14572 args, complain, in_decl);
14573
14574 if (vec)
14575 TREE_VEC_ELT (vec, i) = r;
14576 }
14577
14578 if (vec)
14579 r = vec;
14580 }
14581 break;
14582
14583 case USING_DECL:
14584 /* We reach here only for member using decls. We also need to check
14585 uses_template_parms because DECL_DEPENDENT_P is not set for a
14586 using-declaration that designates a member of the current
14587 instantiation (c++/53549). */
14588 if (DECL_DEPENDENT_P (t)
14589 || uses_template_parms (USING_DECL_SCOPE (t)))
14590 {
14591 tree scope = USING_DECL_SCOPE (t);
14592 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14593 if (PACK_EXPANSION_P (scope))
14594 {
14595 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14596 int len = TREE_VEC_LENGTH (vec);
14597 r = make_tree_vec (len);
14598 for (int i = 0; i < len; ++i)
14599 {
14600 tree escope = TREE_VEC_ELT (vec, i);
14601 tree elt = do_class_using_decl (escope, name);
14602 if (!elt)
14603 {
14604 r = error_mark_node;
14605 break;
14606 }
14607 else
14608 {
14609 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14610 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14611 }
14612 TREE_VEC_ELT (r, i) = elt;
14613 }
14614 }
14615 else
14616 {
14617 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14618 complain, in_decl);
14619 r = do_class_using_decl (inst_scope, name);
14620 if (!r)
14621 r = error_mark_node;
14622 else
14623 {
14624 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14625 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14626 }
14627 }
14628 }
14629 else
14630 {
14631 r = copy_node (t);
14632 DECL_CHAIN (r) = NULL_TREE;
14633 }
14634 break;
14635
14636 case TYPE_DECL:
14637 case VAR_DECL:
14638 {
14639 tree argvec = NULL_TREE;
14640 tree gen_tmpl = NULL_TREE;
14641 tree tmpl = NULL_TREE;
14642 tree type = NULL_TREE;
14643
14644 if (TREE_TYPE (t) == error_mark_node)
14645 RETURN (error_mark_node);
14646
14647 if (TREE_CODE (t) == TYPE_DECL
14648 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14649 {
14650 /* If this is the canonical decl, we don't have to
14651 mess with instantiations, and often we can't (for
14652 typename, template type parms and such). Note that
14653 TYPE_NAME is not correct for the above test if
14654 we've copied the type for a typedef. */
14655 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14656 if (type == error_mark_node)
14657 RETURN (error_mark_node);
14658 r = TYPE_NAME (type);
14659 break;
14660 }
14661
14662 /* Check to see if we already have the specialization we
14663 need. */
14664 tree spec = NULL_TREE;
14665 bool local_p = false;
14666 tree ctx = DECL_CONTEXT (t);
14667 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14668 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14669 {
14670 local_p = false;
14671 if (DECL_CLASS_SCOPE_P (t))
14672 {
14673 ctx = tsubst_aggr_type (ctx, args,
14674 complain,
14675 in_decl, /*entering_scope=*/1);
14676 /* If CTX is unchanged, then T is in fact the
14677 specialization we want. That situation occurs when
14678 referencing a static data member within in its own
14679 class. We can use pointer equality, rather than
14680 same_type_p, because DECL_CONTEXT is always
14681 canonical... */
14682 if (ctx == DECL_CONTEXT (t)
14683 /* ... unless T is a member template; in which
14684 case our caller can be willing to create a
14685 specialization of that template represented
14686 by T. */
14687 && !(DECL_TI_TEMPLATE (t)
14688 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14689 spec = t;
14690 }
14691
14692 if (!spec)
14693 {
14694 tmpl = DECL_TI_TEMPLATE (t);
14695 gen_tmpl = most_general_template (tmpl);
14696 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14697 if (argvec != error_mark_node)
14698 argvec = (coerce_innermost_template_parms
14699 (DECL_TEMPLATE_PARMS (gen_tmpl),
14700 argvec, t, complain,
14701 /*all*/true, /*defarg*/true));
14702 if (argvec == error_mark_node)
14703 RETURN (error_mark_node);
14704 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14705 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14706 }
14707 }
14708 else
14709 {
14710 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14711 /* Subsequent calls to pushdecl will fill this in. */
14712 ctx = NULL_TREE;
14713 /* A local variable. */
14714 local_p = true;
14715 /* Unless this is a reference to a static variable from an
14716 enclosing function, in which case we need to fill it in now. */
14717 if (TREE_STATIC (t))
14718 {
14719 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14720 if (fn != current_function_decl)
14721 ctx = fn;
14722 }
14723 spec = retrieve_local_specialization (t);
14724 }
14725 /* If we already have the specialization we need, there is
14726 nothing more to do. */
14727 if (spec)
14728 {
14729 r = spec;
14730 break;
14731 }
14732
14733 /* Create a new node for the specialization we need. */
14734 if (type == NULL_TREE)
14735 {
14736 if (is_typedef_decl (t))
14737 type = DECL_ORIGINAL_TYPE (t);
14738 else
14739 type = TREE_TYPE (t);
14740 if (VAR_P (t)
14741 && VAR_HAD_UNKNOWN_BOUND (t)
14742 && type != error_mark_node)
14743 type = strip_array_domain (type);
14744 tree sub_args = args;
14745 if (tree auto_node = type_uses_auto (type))
14746 {
14747 /* Mask off any template args past the variable's context so we
14748 don't replace the auto with an unrelated argument. */
14749 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14750 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14751 if (extra > 0)
14752 /* This should never happen with the new lambda instantiation
14753 model, but keep the handling just in case. */
14754 gcc_assert (!CHECKING_P),
14755 sub_args = strip_innermost_template_args (args, extra);
14756 }
14757 type = tsubst (type, sub_args, complain, in_decl);
14758 /* Substituting the type might have recursively instantiated this
14759 same alias (c++/86171). */
14760 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14761 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14762 {
14763 r = spec;
14764 break;
14765 }
14766 }
14767 r = copy_decl (t);
14768 if (VAR_P (r))
14769 {
14770 DECL_INITIALIZED_P (r) = 0;
14771 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14772 if (type == error_mark_node)
14773 RETURN (error_mark_node);
14774 if (TREE_CODE (type) == FUNCTION_TYPE)
14775 {
14776 /* It may seem that this case cannot occur, since:
14777
14778 typedef void f();
14779 void g() { f x; }
14780
14781 declares a function, not a variable. However:
14782
14783 typedef void f();
14784 template <typename T> void g() { T t; }
14785 template void g<f>();
14786
14787 is an attempt to declare a variable with function
14788 type. */
14789 error ("variable %qD has function type",
14790 /* R is not yet sufficiently initialized, so we
14791 just use its name. */
14792 DECL_NAME (r));
14793 RETURN (error_mark_node);
14794 }
14795 type = complete_type (type);
14796 /* Wait until cp_finish_decl to set this again, to handle
14797 circular dependency (template/instantiate6.C). */
14798 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14799 type = check_var_type (DECL_NAME (r), type,
14800 DECL_SOURCE_LOCATION (r));
14801 if (DECL_HAS_VALUE_EXPR_P (t))
14802 {
14803 tree ve = DECL_VALUE_EXPR (t);
14804 /* If the DECL_VALUE_EXPR is converted to the declared type,
14805 preserve the identity so that gimplify_type_sizes works. */
14806 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14807 if (nop)
14808 ve = TREE_OPERAND (ve, 0);
14809 ve = tsubst_expr (ve, args, complain, in_decl,
14810 /*constant_expression_p=*/false);
14811 if (REFERENCE_REF_P (ve))
14812 {
14813 gcc_assert (TYPE_REF_P (type));
14814 ve = TREE_OPERAND (ve, 0);
14815 }
14816 if (nop)
14817 ve = build_nop (type, ve);
14818 else if (DECL_LANG_SPECIFIC (t)
14819 && DECL_OMP_PRIVATIZED_MEMBER (t)
14820 && TREE_CODE (ve) == COMPONENT_REF
14821 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14822 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14823 type = TREE_TYPE (ve);
14824 else
14825 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14826 == TYPE_MAIN_VARIANT (type));
14827 SET_DECL_VALUE_EXPR (r, ve);
14828 }
14829 if (CP_DECL_THREAD_LOCAL_P (r)
14830 && !processing_template_decl)
14831 set_decl_tls_model (r, decl_default_tls_model (r));
14832 }
14833 else if (DECL_SELF_REFERENCE_P (t))
14834 SET_DECL_SELF_REFERENCE_P (r);
14835 TREE_TYPE (r) = type;
14836 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14837 DECL_CONTEXT (r) = ctx;
14838 /* Clear out the mangled name and RTL for the instantiation. */
14839 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14840 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14841 SET_DECL_RTL (r, NULL);
14842 set_instantiating_module (r);
14843
14844 /* The initializer must not be expanded until it is required;
14845 see [temp.inst]. */
14846 DECL_INITIAL (r) = NULL_TREE;
14847 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14848 if (VAR_P (r))
14849 {
14850 if (DECL_LANG_SPECIFIC (r))
14851 SET_DECL_DEPENDENT_INIT_P (r, false);
14852
14853 SET_DECL_MODE (r, VOIDmode);
14854
14855 /* Possibly limit visibility based on template args. */
14856 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14857 if (DECL_VISIBILITY_SPECIFIED (t))
14858 {
14859 DECL_VISIBILITY_SPECIFIED (r) = 0;
14860 DECL_ATTRIBUTES (r)
14861 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14862 }
14863 determine_visibility (r);
14864 }
14865
14866 if (!local_p)
14867 {
14868 /* A static data member declaration is always marked
14869 external when it is declared in-class, even if an
14870 initializer is present. We mimic the non-template
14871 processing here. */
14872 DECL_EXTERNAL (r) = 1;
14873 if (DECL_NAMESPACE_SCOPE_P (t))
14874 DECL_NOT_REALLY_EXTERN (r) = 1;
14875
14876 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14877 SET_DECL_IMPLICIT_INSTANTIATION (r);
14878 if (!error_operand_p (r) || (complain & tf_error))
14879 register_specialization (r, gen_tmpl, argvec, false, hash);
14880 }
14881 else
14882 {
14883 if (DECL_LANG_SPECIFIC (r))
14884 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14885 if (!cp_unevaluated_operand)
14886 register_local_specialization (r, t);
14887 }
14888
14889 DECL_CHAIN (r) = NULL_TREE;
14890
14891 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14892 /*flags=*/0,
14893 args, complain, in_decl);
14894
14895 /* Preserve a typedef that names a type. */
14896 if (is_typedef_decl (r) && type != error_mark_node)
14897 {
14898 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14899 set_underlying_type (r);
14900 if (TYPE_DECL_ALIAS_P (r))
14901 /* An alias template specialization can be dependent
14902 even if its underlying type is not. */
14903 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14904 }
14905
14906 layout_decl (r, 0);
14907 }
14908 break;
14909
14910 default:
14911 gcc_unreachable ();
14912 }
14913 #undef RETURN
14914
14915 out:
14916 /* Restore the file and line information. */
14917 input_location = saved_loc;
14918
14919 return r;
14920 }
14921
14922 /* Substitute into the complete parameter type list PARMS. */
14923
14924 tree
14925 tsubst_function_parms (tree parms,
14926 tree args,
14927 tsubst_flags_t complain,
14928 tree in_decl)
14929 {
14930 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14931 }
14932
14933 /* Substitute into the ARG_TYPES of a function type.
14934 If END is a TREE_CHAIN, leave it and any following types
14935 un-substituted. */
14936
14937 static tree
14938 tsubst_arg_types (tree arg_types,
14939 tree args,
14940 tree end,
14941 tsubst_flags_t complain,
14942 tree in_decl)
14943 {
14944 tree remaining_arg_types;
14945 tree type = NULL_TREE;
14946 int i = 1;
14947 tree expanded_args = NULL_TREE;
14948 tree default_arg;
14949
14950 if (!arg_types || arg_types == void_list_node || arg_types == end)
14951 return arg_types;
14952
14953 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14954 args, end, complain, in_decl);
14955 if (remaining_arg_types == error_mark_node)
14956 return error_mark_node;
14957
14958 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14959 {
14960 /* For a pack expansion, perform substitution on the
14961 entire expression. Later on, we'll handle the arguments
14962 one-by-one. */
14963 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14964 args, complain, in_decl);
14965
14966 if (TREE_CODE (expanded_args) == TREE_VEC)
14967 /* So that we'll spin through the parameters, one by one. */
14968 i = TREE_VEC_LENGTH (expanded_args);
14969 else
14970 {
14971 /* We only partially substituted into the parameter
14972 pack. Our type is TYPE_PACK_EXPANSION. */
14973 type = expanded_args;
14974 expanded_args = NULL_TREE;
14975 }
14976 }
14977
14978 while (i > 0) {
14979 --i;
14980
14981 if (expanded_args)
14982 type = TREE_VEC_ELT (expanded_args, i);
14983 else if (!type)
14984 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14985
14986 if (type == error_mark_node)
14987 return error_mark_node;
14988 if (VOID_TYPE_P (type))
14989 {
14990 if (complain & tf_error)
14991 {
14992 error ("invalid parameter type %qT", type);
14993 if (in_decl)
14994 error ("in declaration %q+D", in_decl);
14995 }
14996 return error_mark_node;
14997 }
14998
14999 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15000 top-level qualifiers as required. */
15001 type = cv_unqualified (type_decays_to (type));
15002
15003 /* We do not substitute into default arguments here. The standard
15004 mandates that they be instantiated only when needed, which is
15005 done in build_over_call. */
15006 default_arg = TREE_PURPOSE (arg_types);
15007
15008 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15009 since the new op() won't have any associated template arguments for us
15010 to refer to later. */
15011 if (lambda_fn_in_template_p (in_decl))
15012 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
15013 false/*fn*/, false/*constexpr*/);
15014
15015 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15016 {
15017 /* We've instantiated a template before its default arguments
15018 have been parsed. This can happen for a nested template
15019 class, and is not an error unless we require the default
15020 argument in a call of this function. */
15021 remaining_arg_types =
15022 tree_cons (default_arg, type, remaining_arg_types);
15023 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15024 remaining_arg_types);
15025 }
15026 else
15027 remaining_arg_types =
15028 hash_tree_cons (default_arg, type, remaining_arg_types);
15029 }
15030
15031 return remaining_arg_types;
15032 }
15033
15034 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15035 *not* handle the exception-specification for FNTYPE, because the
15036 initial substitution of explicitly provided template parameters
15037 during argument deduction forbids substitution into the
15038 exception-specification:
15039
15040 [temp.deduct]
15041
15042 All references in the function type of the function template to the
15043 corresponding template parameters are replaced by the specified tem-
15044 plate argument values. If a substitution in a template parameter or
15045 in the function type of the function template results in an invalid
15046 type, type deduction fails. [Note: The equivalent substitution in
15047 exception specifications is done only when the function is instanti-
15048 ated, at which point a program is ill-formed if the substitution
15049 results in an invalid type.] */
15050
15051 static tree
15052 tsubst_function_type (tree t,
15053 tree args,
15054 tsubst_flags_t complain,
15055 tree in_decl)
15056 {
15057 tree return_type;
15058 tree arg_types = NULL_TREE;
15059
15060 /* The TYPE_CONTEXT is not used for function/method types. */
15061 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15062
15063 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15064 failure. */
15065 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15066
15067 if (late_return_type_p)
15068 {
15069 /* Substitute the argument types. */
15070 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15071 complain, in_decl);
15072 if (arg_types == error_mark_node)
15073 return error_mark_node;
15074
15075 tree save_ccp = current_class_ptr;
15076 tree save_ccr = current_class_ref;
15077 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15078 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15079 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15080 if (do_inject)
15081 {
15082 /* DR 1207: 'this' is in scope in the trailing return type. */
15083 inject_this_parameter (this_type, cp_type_quals (this_type));
15084 }
15085
15086 /* Substitute the return type. */
15087 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15088
15089 if (do_inject)
15090 {
15091 current_class_ptr = save_ccp;
15092 current_class_ref = save_ccr;
15093 }
15094 }
15095 else
15096 /* Substitute the return type. */
15097 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15098
15099 if (return_type == error_mark_node)
15100 return error_mark_node;
15101 /* DR 486 clarifies that creation of a function type with an
15102 invalid return type is a deduction failure. */
15103 if (TREE_CODE (return_type) == ARRAY_TYPE
15104 || TREE_CODE (return_type) == FUNCTION_TYPE)
15105 {
15106 if (complain & tf_error)
15107 {
15108 if (TREE_CODE (return_type) == ARRAY_TYPE)
15109 error ("function returning an array");
15110 else
15111 error ("function returning a function");
15112 }
15113 return error_mark_node;
15114 }
15115
15116 if (!late_return_type_p)
15117 {
15118 /* Substitute the argument types. */
15119 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15120 complain, in_decl);
15121 if (arg_types == error_mark_node)
15122 return error_mark_node;
15123 }
15124
15125 /* Construct a new type node and return it. */
15126 return rebuild_function_or_method_type (t, return_type, arg_types,
15127 /*raises=*/NULL_TREE, complain);
15128 }
15129
15130 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15131 ARGS into that specification, and return the substituted
15132 specification. If there is no specification, return NULL_TREE. */
15133
15134 static tree
15135 tsubst_exception_specification (tree fntype,
15136 tree args,
15137 tsubst_flags_t complain,
15138 tree in_decl,
15139 bool defer_ok)
15140 {
15141 tree specs;
15142 tree new_specs;
15143
15144 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15145 new_specs = NULL_TREE;
15146 if (specs && TREE_PURPOSE (specs))
15147 {
15148 /* A noexcept-specifier. */
15149 tree expr = TREE_PURPOSE (specs);
15150 if (TREE_CODE (expr) == INTEGER_CST)
15151 new_specs = expr;
15152 else if (defer_ok)
15153 {
15154 /* Defer instantiation of noexcept-specifiers to avoid
15155 excessive instantiations (c++/49107). */
15156 new_specs = make_node (DEFERRED_NOEXCEPT);
15157 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15158 {
15159 /* We already partially instantiated this member template,
15160 so combine the new args with the old. */
15161 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15162 = DEFERRED_NOEXCEPT_PATTERN (expr);
15163 DEFERRED_NOEXCEPT_ARGS (new_specs)
15164 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15165 }
15166 else
15167 {
15168 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15169 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15170 }
15171 }
15172 else
15173 {
15174 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15175 {
15176 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15177 args);
15178 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15179 }
15180 new_specs = tsubst_copy_and_build
15181 (expr, args, complain, in_decl, /*function_p=*/false,
15182 /*integral_constant_expression_p=*/true);
15183 }
15184 new_specs = build_noexcept_spec (new_specs, complain);
15185 }
15186 else if (specs)
15187 {
15188 if (! TREE_VALUE (specs))
15189 new_specs = specs;
15190 else
15191 while (specs)
15192 {
15193 tree spec;
15194 int i, len = 1;
15195 tree expanded_specs = NULL_TREE;
15196
15197 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15198 {
15199 /* Expand the pack expansion type. */
15200 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15201 args, complain,
15202 in_decl);
15203
15204 if (expanded_specs == error_mark_node)
15205 return error_mark_node;
15206 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15207 len = TREE_VEC_LENGTH (expanded_specs);
15208 else
15209 {
15210 /* We're substituting into a member template, so
15211 we got a TYPE_PACK_EXPANSION back. Add that
15212 expansion and move on. */
15213 gcc_assert (TREE_CODE (expanded_specs)
15214 == TYPE_PACK_EXPANSION);
15215 new_specs = add_exception_specifier (new_specs,
15216 expanded_specs,
15217 complain);
15218 specs = TREE_CHAIN (specs);
15219 continue;
15220 }
15221 }
15222
15223 for (i = 0; i < len; ++i)
15224 {
15225 if (expanded_specs)
15226 spec = TREE_VEC_ELT (expanded_specs, i);
15227 else
15228 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15229 if (spec == error_mark_node)
15230 return spec;
15231 new_specs = add_exception_specifier (new_specs, spec,
15232 complain);
15233 }
15234
15235 specs = TREE_CHAIN (specs);
15236 }
15237 }
15238 return new_specs;
15239 }
15240
15241 /* Substitute through a TREE_LIST of types or expressions, handling pack
15242 expansions. */
15243
15244 tree
15245 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15246 {
15247 if (t == void_list_node)
15248 return t;
15249
15250 tree purpose = TREE_PURPOSE (t);
15251 tree purposevec = NULL_TREE;
15252 if (!purpose)
15253 ;
15254 else if (PACK_EXPANSION_P (purpose))
15255 {
15256 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15257 if (TREE_CODE (purpose) == TREE_VEC)
15258 purposevec = purpose;
15259 }
15260 else if (TYPE_P (purpose))
15261 purpose = tsubst (purpose, args, complain, in_decl);
15262 else
15263 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15264 if (purpose == error_mark_node || purposevec == error_mark_node)
15265 return error_mark_node;
15266
15267 tree value = TREE_VALUE (t);
15268 tree valuevec = NULL_TREE;
15269 if (!value)
15270 ;
15271 else if (PACK_EXPANSION_P (value))
15272 {
15273 value = tsubst_pack_expansion (value, args, complain, in_decl);
15274 if (TREE_CODE (value) == TREE_VEC)
15275 valuevec = value;
15276 }
15277 else if (TYPE_P (value))
15278 value = tsubst (value, args, complain, in_decl);
15279 else
15280 value = tsubst_copy_and_build (value, args, complain, in_decl);
15281 if (value == error_mark_node || valuevec == error_mark_node)
15282 return error_mark_node;
15283
15284 tree chain = TREE_CHAIN (t);
15285 if (!chain)
15286 ;
15287 else if (TREE_CODE (chain) == TREE_LIST)
15288 chain = tsubst_tree_list (chain, args, complain, in_decl);
15289 else if (TYPE_P (chain))
15290 chain = tsubst (chain, args, complain, in_decl);
15291 else
15292 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15293 if (chain == error_mark_node)
15294 return error_mark_node;
15295
15296 if (purpose == TREE_PURPOSE (t)
15297 && value == TREE_VALUE (t)
15298 && chain == TREE_CHAIN (t))
15299 return t;
15300
15301 int len;
15302 /* Determine the number of arguments. */
15303 if (purposevec)
15304 {
15305 len = TREE_VEC_LENGTH (purposevec);
15306 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15307 }
15308 else if (valuevec)
15309 len = TREE_VEC_LENGTH (valuevec);
15310 else
15311 len = 1;
15312
15313 for (int i = len; i-- > 0; )
15314 {
15315 if (purposevec)
15316 purpose = TREE_VEC_ELT (purposevec, i);
15317 if (valuevec)
15318 value = TREE_VEC_ELT (valuevec, i);
15319
15320 if (value && TYPE_P (value))
15321 chain = hash_tree_cons (purpose, value, chain);
15322 else
15323 chain = tree_cons (purpose, value, chain);
15324 }
15325
15326 return chain;
15327 }
15328
15329 /* Take the tree structure T and replace template parameters used
15330 therein with the argument vector ARGS. IN_DECL is an associated
15331 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15332 Issue error and warning messages under control of COMPLAIN. Note
15333 that we must be relatively non-tolerant of extensions here, in
15334 order to preserve conformance; if we allow substitutions that
15335 should not be allowed, we may allow argument deductions that should
15336 not succeed, and therefore report ambiguous overload situations
15337 where there are none. In theory, we could allow the substitution,
15338 but indicate that it should have failed, and allow our caller to
15339 make sure that the right thing happens, but we don't try to do this
15340 yet.
15341
15342 This function is used for dealing with types, decls and the like;
15343 for expressions, use tsubst_expr or tsubst_copy. */
15344
15345 tree
15346 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15347 {
15348 enum tree_code code;
15349 tree type, r = NULL_TREE;
15350
15351 if (t == NULL_TREE || t == error_mark_node
15352 || t == integer_type_node
15353 || t == void_type_node
15354 || t == char_type_node
15355 || t == unknown_type_node
15356 || TREE_CODE (t) == NAMESPACE_DECL
15357 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15358 return t;
15359
15360 if (DECL_P (t))
15361 return tsubst_decl (t, args, complain);
15362
15363 if (args == NULL_TREE)
15364 return t;
15365
15366 code = TREE_CODE (t);
15367
15368 if (code == IDENTIFIER_NODE)
15369 type = IDENTIFIER_TYPE_VALUE (t);
15370 else
15371 type = TREE_TYPE (t);
15372
15373 gcc_assert (type != unknown_type_node);
15374
15375 /* Reuse typedefs. We need to do this to handle dependent attributes,
15376 such as attribute aligned. */
15377 if (TYPE_P (t)
15378 && typedef_variant_p (t))
15379 {
15380 tree decl = TYPE_NAME (t);
15381
15382 if (alias_template_specialization_p (t, nt_opaque))
15383 {
15384 /* DECL represents an alias template and we want to
15385 instantiate it. */
15386 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15387 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15388 r = instantiate_alias_template (tmpl, gen_args, complain);
15389 }
15390 else if (DECL_CLASS_SCOPE_P (decl)
15391 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15392 && uses_template_parms (DECL_CONTEXT (decl)))
15393 {
15394 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15395 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15396 r = retrieve_specialization (tmpl, gen_args, 0);
15397 }
15398 else if (DECL_FUNCTION_SCOPE_P (decl)
15399 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15400 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15401 r = retrieve_local_specialization (decl);
15402 else
15403 /* The typedef is from a non-template context. */
15404 return t;
15405
15406 if (r)
15407 {
15408 r = TREE_TYPE (r);
15409 r = cp_build_qualified_type_real
15410 (r, cp_type_quals (t) | cp_type_quals (r),
15411 complain | tf_ignore_bad_quals);
15412 return r;
15413 }
15414 else
15415 {
15416 /* We don't have an instantiation yet, so drop the typedef. */
15417 int quals = cp_type_quals (t);
15418 t = DECL_ORIGINAL_TYPE (decl);
15419 t = cp_build_qualified_type_real (t, quals,
15420 complain | tf_ignore_bad_quals);
15421 }
15422 }
15423
15424 bool fndecl_type = (complain & tf_fndecl_type);
15425 complain &= ~tf_fndecl_type;
15426
15427 if (type
15428 && code != TYPENAME_TYPE
15429 && code != TEMPLATE_TYPE_PARM
15430 && code != TEMPLATE_PARM_INDEX
15431 && code != IDENTIFIER_NODE
15432 && code != FUNCTION_TYPE
15433 && code != METHOD_TYPE)
15434 type = tsubst (type, args, complain, in_decl);
15435 if (type == error_mark_node)
15436 return error_mark_node;
15437
15438 switch (code)
15439 {
15440 case RECORD_TYPE:
15441 case UNION_TYPE:
15442 case ENUMERAL_TYPE:
15443 return tsubst_aggr_type (t, args, complain, in_decl,
15444 /*entering_scope=*/0);
15445
15446 case ERROR_MARK:
15447 case IDENTIFIER_NODE:
15448 case VOID_TYPE:
15449 case OPAQUE_TYPE:
15450 case REAL_TYPE:
15451 case COMPLEX_TYPE:
15452 case VECTOR_TYPE:
15453 case BOOLEAN_TYPE:
15454 case NULLPTR_TYPE:
15455 case LANG_TYPE:
15456 return t;
15457
15458 case INTEGER_TYPE:
15459 if (t == integer_type_node)
15460 return t;
15461
15462 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15463 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15464 return t;
15465
15466 {
15467 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15468
15469 max = tsubst_expr (omax, args, complain, in_decl,
15470 /*integral_constant_expression_p=*/false);
15471
15472 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15473 needed. */
15474 if (TREE_CODE (max) == NOP_EXPR
15475 && TREE_SIDE_EFFECTS (omax)
15476 && !TREE_TYPE (max))
15477 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15478
15479 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15480 with TREE_SIDE_EFFECTS that indicates this is not an integral
15481 constant expression. */
15482 if (processing_template_decl
15483 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15484 {
15485 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15486 TREE_SIDE_EFFECTS (max) = 1;
15487 }
15488
15489 return compute_array_index_type (NULL_TREE, max, complain);
15490 }
15491
15492 case TEMPLATE_TYPE_PARM:
15493 case TEMPLATE_TEMPLATE_PARM:
15494 case BOUND_TEMPLATE_TEMPLATE_PARM:
15495 case TEMPLATE_PARM_INDEX:
15496 {
15497 int idx;
15498 int level;
15499 int levels;
15500 tree arg = NULL_TREE;
15501
15502 r = NULL_TREE;
15503
15504 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15505 template_parm_level_and_index (t, &level, &idx);
15506
15507 levels = TMPL_ARGS_DEPTH (args);
15508 if (level <= levels
15509 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15510 {
15511 arg = TMPL_ARG (args, level, idx);
15512
15513 /* See through ARGUMENT_PACK_SELECT arguments. */
15514 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15515 arg = argument_pack_select_arg (arg);
15516 }
15517
15518 if (arg == error_mark_node)
15519 return error_mark_node;
15520 else if (arg != NULL_TREE)
15521 {
15522 if (ARGUMENT_PACK_P (arg))
15523 /* If ARG is an argument pack, we don't actually want to
15524 perform a substitution here, because substitutions
15525 for argument packs are only done
15526 element-by-element. We can get to this point when
15527 substituting the type of a non-type template
15528 parameter pack, when that type actually contains
15529 template parameter packs from an outer template, e.g.,
15530
15531 template<typename... Types> struct A {
15532 template<Types... Values> struct B { };
15533 }; */
15534 return t;
15535
15536 if (code == TEMPLATE_TYPE_PARM)
15537 {
15538 int quals;
15539
15540 /* When building concept checks for the purpose of
15541 deducing placeholders, we can end up with wildcards
15542 where types are expected. Adjust this to the deduced
15543 value. */
15544 if (TREE_CODE (arg) == WILDCARD_DECL)
15545 arg = TREE_TYPE (TREE_TYPE (arg));
15546
15547 gcc_assert (TYPE_P (arg));
15548
15549 quals = cp_type_quals (arg) | cp_type_quals (t);
15550
15551 return cp_build_qualified_type_real
15552 (arg, quals, complain | tf_ignore_bad_quals);
15553 }
15554 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15555 {
15556 /* We are processing a type constructed from a
15557 template template parameter. */
15558 tree argvec = tsubst (TYPE_TI_ARGS (t),
15559 args, complain, in_decl);
15560 if (argvec == error_mark_node)
15561 return error_mark_node;
15562
15563 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15564 || TREE_CODE (arg) == TEMPLATE_DECL
15565 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15566
15567 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15568 /* Consider this code:
15569
15570 template <template <class> class Template>
15571 struct Internal {
15572 template <class Arg> using Bind = Template<Arg>;
15573 };
15574
15575 template <template <class> class Template, class Arg>
15576 using Instantiate = Template<Arg>; //#0
15577
15578 template <template <class> class Template,
15579 class Argument>
15580 using Bind =
15581 Instantiate<Internal<Template>::template Bind,
15582 Argument>; //#1
15583
15584 When #1 is parsed, the
15585 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15586 parameter `Template' in #0 matches the
15587 UNBOUND_CLASS_TEMPLATE representing the argument
15588 `Internal<Template>::template Bind'; We then want
15589 to assemble the type `Bind<Argument>' that can't
15590 be fully created right now, because
15591 `Internal<Template>' not being complete, the Bind
15592 template cannot be looked up in that context. So
15593 we need to "store" `Bind<Argument>' for later
15594 when the context of Bind becomes complete. Let's
15595 store that in a TYPENAME_TYPE. */
15596 return make_typename_type (TYPE_CONTEXT (arg),
15597 build_nt (TEMPLATE_ID_EXPR,
15598 TYPE_IDENTIFIER (arg),
15599 argvec),
15600 typename_type,
15601 complain);
15602
15603 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15604 are resolving nested-types in the signature of a
15605 member function templates. Otherwise ARG is a
15606 TEMPLATE_DECL and is the real template to be
15607 instantiated. */
15608 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15609 arg = TYPE_NAME (arg);
15610
15611 r = lookup_template_class (arg,
15612 argvec, in_decl,
15613 DECL_CONTEXT (arg),
15614 /*entering_scope=*/0,
15615 complain);
15616 return cp_build_qualified_type_real
15617 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15618 }
15619 else if (code == TEMPLATE_TEMPLATE_PARM)
15620 return arg;
15621 else
15622 /* TEMPLATE_PARM_INDEX. */
15623 return convert_from_reference (unshare_expr (arg));
15624 }
15625
15626 if (level == 1)
15627 /* This can happen during the attempted tsubst'ing in
15628 unify. This means that we don't yet have any information
15629 about the template parameter in question. */
15630 return t;
15631
15632 /* Early in template argument deduction substitution, we don't
15633 want to reduce the level of 'auto', or it will be confused
15634 with a normal template parm in subsequent deduction.
15635 Similarly, don't reduce the level of template parameters to
15636 avoid mismatches when deducing their types. */
15637 if (complain & tf_partial)
15638 return t;
15639
15640 /* If we get here, we must have been looking at a parm for a
15641 more deeply nested template. Make a new version of this
15642 template parameter, but with a lower level. */
15643 switch (code)
15644 {
15645 case TEMPLATE_TYPE_PARM:
15646 case TEMPLATE_TEMPLATE_PARM:
15647 case BOUND_TEMPLATE_TEMPLATE_PARM:
15648 if (cp_type_quals (t))
15649 {
15650 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15651 r = cp_build_qualified_type_real
15652 (r, cp_type_quals (t),
15653 complain | (code == TEMPLATE_TYPE_PARM
15654 ? tf_ignore_bad_quals : 0));
15655 }
15656 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15657 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15658 && (r = (TEMPLATE_PARM_DESCENDANTS
15659 (TEMPLATE_TYPE_PARM_INDEX (t))))
15660 && (r = TREE_TYPE (r))
15661 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15662 /* Break infinite recursion when substituting the constraints
15663 of a constrained placeholder. */;
15664 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15665 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15666 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15667 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15668 r = TEMPLATE_PARM_DESCENDANTS (arg))
15669 && (TEMPLATE_PARM_LEVEL (r)
15670 == TEMPLATE_PARM_LEVEL (arg) - levels))
15671 /* Cache the simple case of lowering a type parameter. */
15672 r = TREE_TYPE (r);
15673 else
15674 {
15675 r = copy_type (t);
15676 TEMPLATE_TYPE_PARM_INDEX (r)
15677 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15678 r, levels, args, complain);
15679 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15680 TYPE_MAIN_VARIANT (r) = r;
15681 TYPE_POINTER_TO (r) = NULL_TREE;
15682 TYPE_REFERENCE_TO (r) = NULL_TREE;
15683
15684 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15685 {
15686 /* Propagate constraints on placeholders since they are
15687 only instantiated during satisfaction. */
15688 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15689 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15690 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15691 {
15692 pl = tsubst_copy (pl, args, complain, in_decl);
15693 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15694 }
15695 }
15696
15697 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15698 /* We have reduced the level of the template
15699 template parameter, but not the levels of its
15700 template parameters, so canonical_type_parameter
15701 will not be able to find the canonical template
15702 template parameter for this level. Thus, we
15703 require structural equality checking to compare
15704 TEMPLATE_TEMPLATE_PARMs. */
15705 SET_TYPE_STRUCTURAL_EQUALITY (r);
15706 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15707 SET_TYPE_STRUCTURAL_EQUALITY (r);
15708 else
15709 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15710
15711 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15712 {
15713 tree tinfo = TYPE_TEMPLATE_INFO (t);
15714 /* We might need to substitute into the types of non-type
15715 template parameters. */
15716 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15717 complain, in_decl);
15718 if (tmpl == error_mark_node)
15719 return error_mark_node;
15720 tree argvec = tsubst (TI_ARGS (tinfo), args,
15721 complain, in_decl);
15722 if (argvec == error_mark_node)
15723 return error_mark_node;
15724
15725 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15726 = build_template_info (tmpl, argvec);
15727 }
15728 }
15729 break;
15730
15731 case TEMPLATE_PARM_INDEX:
15732 /* OK, now substitute the type of the non-type parameter. We
15733 couldn't do it earlier because it might be an auto parameter,
15734 and we wouldn't need to if we had an argument. */
15735 type = tsubst (type, args, complain, in_decl);
15736 if (type == error_mark_node)
15737 return error_mark_node;
15738 r = reduce_template_parm_level (t, type, levels, args, complain);
15739 break;
15740
15741 default:
15742 gcc_unreachable ();
15743 }
15744
15745 return r;
15746 }
15747
15748 case TREE_LIST:
15749 return tsubst_tree_list (t, args, complain, in_decl);
15750
15751 case TREE_BINFO:
15752 /* We should never be tsubsting a binfo. */
15753 gcc_unreachable ();
15754
15755 case TREE_VEC:
15756 /* A vector of template arguments. */
15757 gcc_assert (!type);
15758 return tsubst_template_args (t, args, complain, in_decl);
15759
15760 case POINTER_TYPE:
15761 case REFERENCE_TYPE:
15762 {
15763 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15764 return t;
15765
15766 /* [temp.deduct]
15767
15768 Type deduction may fail for any of the following
15769 reasons:
15770
15771 -- Attempting to create a pointer to reference type.
15772 -- Attempting to create a reference to a reference type or
15773 a reference to void.
15774
15775 Core issue 106 says that creating a reference to a reference
15776 during instantiation is no longer a cause for failure. We
15777 only enforce this check in strict C++98 mode. */
15778 if ((TYPE_REF_P (type)
15779 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15780 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15781 {
15782 static location_t last_loc;
15783
15784 /* We keep track of the last time we issued this error
15785 message to avoid spewing a ton of messages during a
15786 single bad template instantiation. */
15787 if (complain & tf_error
15788 && last_loc != input_location)
15789 {
15790 if (VOID_TYPE_P (type))
15791 error ("forming reference to void");
15792 else if (code == POINTER_TYPE)
15793 error ("forming pointer to reference type %qT", type);
15794 else
15795 error ("forming reference to reference type %qT", type);
15796 last_loc = input_location;
15797 }
15798
15799 return error_mark_node;
15800 }
15801 else if (TREE_CODE (type) == FUNCTION_TYPE
15802 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15803 || type_memfn_rqual (type) != REF_QUAL_NONE))
15804 {
15805 if (complain & tf_error)
15806 {
15807 if (code == POINTER_TYPE)
15808 error ("forming pointer to qualified function type %qT",
15809 type);
15810 else
15811 error ("forming reference to qualified function type %qT",
15812 type);
15813 }
15814 return error_mark_node;
15815 }
15816 else if (code == POINTER_TYPE)
15817 {
15818 r = build_pointer_type (type);
15819 if (TREE_CODE (type) == METHOD_TYPE)
15820 r = build_ptrmemfunc_type (r);
15821 }
15822 else if (TYPE_REF_P (type))
15823 /* In C++0x, during template argument substitution, when there is an
15824 attempt to create a reference to a reference type, reference
15825 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15826
15827 "If a template-argument for a template-parameter T names a type
15828 that is a reference to a type A, an attempt to create the type
15829 'lvalue reference to cv T' creates the type 'lvalue reference to
15830 A,' while an attempt to create the type type rvalue reference to
15831 cv T' creates the type T"
15832 */
15833 r = cp_build_reference_type
15834 (TREE_TYPE (type),
15835 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15836 else
15837 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15838 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15839
15840 if (r != error_mark_node)
15841 /* Will this ever be needed for TYPE_..._TO values? */
15842 layout_type (r);
15843
15844 return r;
15845 }
15846 case OFFSET_TYPE:
15847 {
15848 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15849 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15850 {
15851 /* [temp.deduct]
15852
15853 Type deduction may fail for any of the following
15854 reasons:
15855
15856 -- Attempting to create "pointer to member of T" when T
15857 is not a class type. */
15858 if (complain & tf_error)
15859 error ("creating pointer to member of non-class type %qT", r);
15860 return error_mark_node;
15861 }
15862 if (TYPE_REF_P (type))
15863 {
15864 if (complain & tf_error)
15865 error ("creating pointer to member reference type %qT", type);
15866 return error_mark_node;
15867 }
15868 if (VOID_TYPE_P (type))
15869 {
15870 if (complain & tf_error)
15871 error ("creating pointer to member of type void");
15872 return error_mark_node;
15873 }
15874 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15875 if (TREE_CODE (type) == FUNCTION_TYPE)
15876 {
15877 /* The type of the implicit object parameter gets its
15878 cv-qualifiers from the FUNCTION_TYPE. */
15879 tree memptr;
15880 tree method_type
15881 = build_memfn_type (type, r, type_memfn_quals (type),
15882 type_memfn_rqual (type));
15883 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15884 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15885 complain);
15886 }
15887 else
15888 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15889 cp_type_quals (t),
15890 complain);
15891 }
15892 case FUNCTION_TYPE:
15893 case METHOD_TYPE:
15894 {
15895 tree fntype;
15896 tree specs;
15897 fntype = tsubst_function_type (t, args, complain, in_decl);
15898 if (fntype == error_mark_node)
15899 return error_mark_node;
15900
15901 /* Substitute the exception specification. */
15902 specs = tsubst_exception_specification (t, args, complain, in_decl,
15903 /*defer_ok*/fndecl_type);
15904 if (specs == error_mark_node)
15905 return error_mark_node;
15906 if (specs)
15907 fntype = build_exception_variant (fntype, specs);
15908 return fntype;
15909 }
15910 case ARRAY_TYPE:
15911 {
15912 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15913 if (domain == error_mark_node)
15914 return error_mark_node;
15915
15916 /* As an optimization, we avoid regenerating the array type if
15917 it will obviously be the same as T. */
15918 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15919 return t;
15920
15921 /* These checks should match the ones in create_array_type_for_decl.
15922
15923 [temp.deduct]
15924
15925 The deduction may fail for any of the following reasons:
15926
15927 -- Attempting to create an array with an element type that
15928 is void, a function type, or a reference type, or [DR337]
15929 an abstract class type. */
15930 if (VOID_TYPE_P (type)
15931 || TREE_CODE (type) == FUNCTION_TYPE
15932 || (TREE_CODE (type) == ARRAY_TYPE
15933 && TYPE_DOMAIN (type) == NULL_TREE)
15934 || TYPE_REF_P (type))
15935 {
15936 if (complain & tf_error)
15937 error ("creating array of %qT", type);
15938 return error_mark_node;
15939 }
15940
15941 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
15942 !(complain & tf_error)))
15943 return error_mark_node;
15944
15945 r = build_cplus_array_type (type, domain);
15946
15947 if (!valid_array_size_p (input_location, r, in_decl,
15948 (complain & tf_error)))
15949 return error_mark_node;
15950
15951 if (TYPE_USER_ALIGN (t))
15952 {
15953 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15954 TYPE_USER_ALIGN (r) = 1;
15955 }
15956
15957 return r;
15958 }
15959
15960 case TYPENAME_TYPE:
15961 {
15962 tree ctx = TYPE_CONTEXT (t);
15963 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15964 {
15965 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15966 if (ctx == error_mark_node
15967 || TREE_VEC_LENGTH (ctx) > 1)
15968 return error_mark_node;
15969 if (TREE_VEC_LENGTH (ctx) == 0)
15970 {
15971 if (complain & tf_error)
15972 error ("%qD is instantiated for an empty pack",
15973 TYPENAME_TYPE_FULLNAME (t));
15974 return error_mark_node;
15975 }
15976 ctx = TREE_VEC_ELT (ctx, 0);
15977 }
15978 else
15979 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15980 /*entering_scope=*/1);
15981 if (ctx == error_mark_node)
15982 return error_mark_node;
15983
15984 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15985 complain, in_decl);
15986 if (f == error_mark_node)
15987 return error_mark_node;
15988
15989 if (!MAYBE_CLASS_TYPE_P (ctx))
15990 {
15991 if (complain & tf_error)
15992 error ("%qT is not a class, struct, or union type", ctx);
15993 return error_mark_node;
15994 }
15995 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15996 {
15997 /* Normally, make_typename_type does not require that the CTX
15998 have complete type in order to allow things like:
15999
16000 template <class T> struct S { typename S<T>::X Y; };
16001
16002 But, such constructs have already been resolved by this
16003 point, so here CTX really should have complete type, unless
16004 it's a partial instantiation. */
16005 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16006 return error_mark_node;
16007 }
16008
16009 f = make_typename_type (ctx, f, typename_type,
16010 complain | tf_keep_type_decl);
16011 if (f == error_mark_node)
16012 return f;
16013 if (TREE_CODE (f) == TYPE_DECL)
16014 {
16015 complain |= tf_ignore_bad_quals;
16016 f = TREE_TYPE (f);
16017 }
16018
16019 if (TREE_CODE (f) != TYPENAME_TYPE)
16020 {
16021 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16022 {
16023 if (complain & tf_error)
16024 error ("%qT resolves to %qT, which is not an enumeration type",
16025 t, f);
16026 else
16027 return error_mark_node;
16028 }
16029 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16030 {
16031 if (complain & tf_error)
16032 error ("%qT resolves to %qT, which is not a class type",
16033 t, f);
16034 else
16035 return error_mark_node;
16036 }
16037 }
16038
16039 return cp_build_qualified_type_real
16040 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16041 }
16042
16043 case UNBOUND_CLASS_TEMPLATE:
16044 {
16045 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16046 in_decl, /*entering_scope=*/1);
16047 tree name = TYPE_IDENTIFIER (t);
16048 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16049
16050 if (ctx == error_mark_node || name == error_mark_node)
16051 return error_mark_node;
16052
16053 if (parm_list)
16054 parm_list = tsubst_template_parms (parm_list, args, complain);
16055 return make_unbound_class_template (ctx, name, parm_list, complain);
16056 }
16057
16058 case TYPEOF_TYPE:
16059 {
16060 tree type;
16061
16062 ++cp_unevaluated_operand;
16063 ++c_inhibit_evaluation_warnings;
16064
16065 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16066 complain, in_decl,
16067 /*integral_constant_expression_p=*/false);
16068
16069 --cp_unevaluated_operand;
16070 --c_inhibit_evaluation_warnings;
16071
16072 type = finish_typeof (type);
16073 return cp_build_qualified_type_real (type,
16074 cp_type_quals (t)
16075 | cp_type_quals (type),
16076 complain);
16077 }
16078
16079 case DECLTYPE_TYPE:
16080 {
16081 tree type;
16082
16083 ++cp_unevaluated_operand;
16084 ++c_inhibit_evaluation_warnings;
16085
16086 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16087 complain|tf_decltype, in_decl,
16088 /*function_p*/false,
16089 /*integral_constant_expression*/false);
16090
16091 --cp_unevaluated_operand;
16092 --c_inhibit_evaluation_warnings;
16093
16094 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16095 type = lambda_capture_field_type (type,
16096 false /*explicit_init*/,
16097 DECLTYPE_FOR_REF_CAPTURE (t));
16098 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16099 type = lambda_proxy_type (type);
16100 else
16101 {
16102 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16103 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16104 && EXPR_P (type))
16105 /* In a template ~id could be either a complement expression
16106 or an unqualified-id naming a destructor; if instantiating
16107 it produces an expression, it's not an id-expression or
16108 member access. */
16109 id = false;
16110 type = finish_decltype_type (type, id, complain);
16111 }
16112 return cp_build_qualified_type_real (type,
16113 cp_type_quals (t)
16114 | cp_type_quals (type),
16115 complain | tf_ignore_bad_quals);
16116 }
16117
16118 case UNDERLYING_TYPE:
16119 {
16120 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16121 complain, in_decl);
16122 return finish_underlying_type (type);
16123 }
16124
16125 case TYPE_ARGUMENT_PACK:
16126 case NONTYPE_ARGUMENT_PACK:
16127 return tsubst_argument_pack (t, args, complain, in_decl);
16128
16129 case VOID_CST:
16130 case INTEGER_CST:
16131 case REAL_CST:
16132 case STRING_CST:
16133 case PLUS_EXPR:
16134 case MINUS_EXPR:
16135 case NEGATE_EXPR:
16136 case NOP_EXPR:
16137 case INDIRECT_REF:
16138 case ADDR_EXPR:
16139 case CALL_EXPR:
16140 case ARRAY_REF:
16141 case SCOPE_REF:
16142 /* We should use one of the expression tsubsts for these codes. */
16143 gcc_unreachable ();
16144
16145 default:
16146 sorry ("use of %qs in template", get_tree_code_name (code));
16147 return error_mark_node;
16148 }
16149 }
16150
16151 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16152 expression on the left-hand side of the "." or "->" operator. We
16153 only do the lookup if we had a dependent BASELINK. Otherwise we
16154 adjust it onto the instantiated heirarchy. */
16155
16156 static tree
16157 tsubst_baselink (tree baselink, tree object_type,
16158 tree args, tsubst_flags_t complain, tree in_decl)
16159 {
16160 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16161 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16162 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16163
16164 tree optype = BASELINK_OPTYPE (baselink);
16165 optype = tsubst (optype, args, complain, in_decl);
16166
16167 tree template_args = NULL_TREE;
16168 bool template_id_p = false;
16169 tree fns = BASELINK_FUNCTIONS (baselink);
16170 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16171 {
16172 template_id_p = true;
16173 template_args = TREE_OPERAND (fns, 1);
16174 fns = TREE_OPERAND (fns, 0);
16175 if (template_args)
16176 template_args = tsubst_template_args (template_args, args,
16177 complain, in_decl);
16178 }
16179
16180 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16181 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16182 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16183
16184 if (dependent_p)
16185 {
16186 tree name = OVL_NAME (fns);
16187 if (IDENTIFIER_CONV_OP_P (name))
16188 name = make_conv_op_name (optype);
16189
16190 if (name == complete_dtor_identifier)
16191 /* Treat as-if non-dependent below. */
16192 dependent_p = false;
16193
16194 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16195 complain);
16196 if (!baselink)
16197 {
16198 if ((complain & tf_error)
16199 && constructor_name_p (name, qualifying_scope))
16200 error ("cannot call constructor %<%T::%D%> directly",
16201 qualifying_scope, name);
16202 return error_mark_node;
16203 }
16204
16205 if (BASELINK_P (baselink))
16206 fns = BASELINK_FUNCTIONS (baselink);
16207 }
16208 else
16209 /* We're going to overwrite pieces below, make a duplicate. */
16210 baselink = copy_node (baselink);
16211
16212 /* If lookup found a single function, mark it as used at this point.
16213 (If lookup found multiple functions the one selected later by
16214 overload resolution will be marked as used at that point.) */
16215 if (!template_id_p && !really_overloaded_fn (fns))
16216 {
16217 tree fn = OVL_FIRST (fns);
16218 bool ok = mark_used (fn, complain);
16219 if (!ok && !(complain & tf_error))
16220 return error_mark_node;
16221 if (ok && BASELINK_P (baselink))
16222 /* We might have instantiated an auto function. */
16223 TREE_TYPE (baselink) = TREE_TYPE (fn);
16224 }
16225
16226 if (BASELINK_P (baselink))
16227 {
16228 /* Add back the template arguments, if present. */
16229 if (template_id_p)
16230 BASELINK_FUNCTIONS (baselink)
16231 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16232
16233 /* Update the conversion operator type. */
16234 BASELINK_OPTYPE (baselink) = optype;
16235 }
16236
16237 if (!object_type)
16238 object_type = current_class_type;
16239
16240 if (qualified_p || !dependent_p)
16241 {
16242 baselink = adjust_result_of_qualified_name_lookup (baselink,
16243 qualifying_scope,
16244 object_type);
16245 if (!qualified_p)
16246 /* We need to call adjust_result_of_qualified_name_lookup in case the
16247 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16248 so that we still get virtual function binding. */
16249 BASELINK_QUALIFIED_P (baselink) = false;
16250 }
16251
16252 return baselink;
16253 }
16254
16255 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16256 true if the qualified-id will be a postfix-expression in-and-of
16257 itself; false if more of the postfix-expression follows the
16258 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16259 of "&". */
16260
16261 static tree
16262 tsubst_qualified_id (tree qualified_id, tree args,
16263 tsubst_flags_t complain, tree in_decl,
16264 bool done, bool address_p)
16265 {
16266 tree expr;
16267 tree scope;
16268 tree name;
16269 bool is_template;
16270 tree template_args;
16271 location_t loc = EXPR_LOCATION (qualified_id);
16272
16273 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16274
16275 /* Figure out what name to look up. */
16276 name = TREE_OPERAND (qualified_id, 1);
16277 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16278 {
16279 is_template = true;
16280 template_args = TREE_OPERAND (name, 1);
16281 if (template_args)
16282 template_args = tsubst_template_args (template_args, args,
16283 complain, in_decl);
16284 if (template_args == error_mark_node)
16285 return error_mark_node;
16286 name = TREE_OPERAND (name, 0);
16287 }
16288 else
16289 {
16290 is_template = false;
16291 template_args = NULL_TREE;
16292 }
16293
16294 /* Substitute into the qualifying scope. When there are no ARGS, we
16295 are just trying to simplify a non-dependent expression. In that
16296 case the qualifying scope may be dependent, and, in any case,
16297 substituting will not help. */
16298 scope = TREE_OPERAND (qualified_id, 0);
16299 if (args)
16300 {
16301 scope = tsubst (scope, args, complain, in_decl);
16302 expr = tsubst_copy (name, args, complain, in_decl);
16303 }
16304 else
16305 expr = name;
16306
16307 if (dependent_scope_p (scope))
16308 {
16309 if (is_template)
16310 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16311 tree r = build_qualified_name (NULL_TREE, scope, expr,
16312 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16313 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16314 return r;
16315 }
16316
16317 if (!BASELINK_P (name) && !DECL_P (expr))
16318 {
16319 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16320 {
16321 /* A BIT_NOT_EXPR is used to represent a destructor. */
16322 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16323 {
16324 error ("qualifying type %qT does not match destructor name ~%qT",
16325 scope, TREE_OPERAND (expr, 0));
16326 expr = error_mark_node;
16327 }
16328 else
16329 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16330 LOOK_want::NORMAL, false);
16331 }
16332 else
16333 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16334 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16335 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16336 {
16337 if (complain & tf_error)
16338 {
16339 error ("dependent-name %qE is parsed as a non-type, but "
16340 "instantiation yields a type", qualified_id);
16341 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16342 }
16343 return error_mark_node;
16344 }
16345 }
16346
16347 if (DECL_P (expr))
16348 {
16349 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16350 scope, complain))
16351 return error_mark_node;
16352 /* Remember that there was a reference to this entity. */
16353 if (!mark_used (expr, complain) && !(complain & tf_error))
16354 return error_mark_node;
16355 }
16356
16357 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16358 {
16359 if (complain & tf_error)
16360 qualified_name_lookup_error (scope,
16361 TREE_OPERAND (qualified_id, 1),
16362 expr, input_location);
16363 return error_mark_node;
16364 }
16365
16366 if (is_template)
16367 {
16368 /* We may be repeating a check already done during parsing, but
16369 if it was well-formed and passed then, it will pass again
16370 now, and if it didn't, we wouldn't have got here. The case
16371 we want to catch is when we couldn't tell then, and can now,
16372 namely when templ prior to substitution was an
16373 identifier. */
16374 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16375 return error_mark_node;
16376
16377 if (variable_template_p (expr))
16378 expr = lookup_and_finish_template_variable (expr, template_args,
16379 complain);
16380 else
16381 expr = lookup_template_function (expr, template_args);
16382 }
16383
16384 if (expr == error_mark_node && complain & tf_error)
16385 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16386 expr, input_location);
16387 else if (TYPE_P (scope))
16388 {
16389 expr = (adjust_result_of_qualified_name_lookup
16390 (expr, scope, current_nonlambda_class_type ()));
16391 expr = (finish_qualified_id_expr
16392 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16393 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16394 /*template_arg_p=*/false, complain));
16395 }
16396
16397 /* Expressions do not generally have reference type. */
16398 if (TREE_CODE (expr) != SCOPE_REF
16399 /* However, if we're about to form a pointer-to-member, we just
16400 want the referenced member referenced. */
16401 && TREE_CODE (expr) != OFFSET_REF)
16402 expr = convert_from_reference (expr);
16403
16404 if (REF_PARENTHESIZED_P (qualified_id))
16405 expr = force_paren_expr (expr);
16406
16407 expr = maybe_wrap_with_location (expr, loc);
16408
16409 return expr;
16410 }
16411
16412 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16413 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16414 for tsubst. */
16415
16416 static tree
16417 tsubst_init (tree init, tree decl, tree args,
16418 tsubst_flags_t complain, tree in_decl)
16419 {
16420 if (!init)
16421 return NULL_TREE;
16422
16423 init = tsubst_expr (init, args, complain, in_decl, false);
16424
16425 tree type = TREE_TYPE (decl);
16426
16427 if (!init && type != error_mark_node)
16428 {
16429 if (tree auto_node = type_uses_auto (type))
16430 {
16431 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16432 {
16433 if (complain & tf_error)
16434 error ("initializer for %q#D expands to an empty list "
16435 "of expressions", decl);
16436 return error_mark_node;
16437 }
16438 }
16439 else if (!dependent_type_p (type))
16440 {
16441 /* If we had an initializer but it
16442 instantiated to nothing,
16443 value-initialize the object. This will
16444 only occur when the initializer was a
16445 pack expansion where the parameter packs
16446 used in that expansion were of length
16447 zero. */
16448 init = build_value_init (type, complain);
16449 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16450 init = get_target_expr_sfinae (init, complain);
16451 if (TREE_CODE (init) == TARGET_EXPR)
16452 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16453 }
16454 }
16455
16456 return init;
16457 }
16458
16459 /* If T is a reference to a dependent member of the current instantiation C and
16460 we are trying to refer to that member in a partial instantiation of C,
16461 return a SCOPE_REF; otherwise, return NULL_TREE.
16462
16463 This can happen when forming a C++17 deduction guide, as in PR96199. */
16464
16465 static tree
16466 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16467 tree in_decl)
16468 {
16469 if (cxx_dialect < cxx17)
16470 return NULL_TREE;
16471
16472 tree ctx = context_for_name_lookup (t);
16473 if (!CLASS_TYPE_P (ctx))
16474 return NULL_TREE;
16475
16476 ctx = tsubst (ctx, args, complain, in_decl);
16477 if (dependent_scope_p (ctx))
16478 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16479 /*template_p=*/false);
16480
16481 return NULL_TREE;
16482 }
16483
16484 /* Like tsubst, but deals with expressions. This function just replaces
16485 template parms; to finish processing the resultant expression, use
16486 tsubst_copy_and_build or tsubst_expr. */
16487
16488 static tree
16489 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16490 {
16491 enum tree_code code;
16492 tree r;
16493
16494 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16495 return t;
16496
16497 code = TREE_CODE (t);
16498
16499 switch (code)
16500 {
16501 case PARM_DECL:
16502 r = retrieve_local_specialization (t);
16503
16504 if (r == NULL_TREE)
16505 {
16506 /* We get here for a use of 'this' in an NSDMI. */
16507 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16508 return current_class_ptr;
16509
16510 /* This can happen for a parameter name used later in a function
16511 declaration (such as in a late-specified return type). Just
16512 make a dummy decl, since it's only used for its type. */
16513 gcc_assert (cp_unevaluated_operand != 0);
16514 r = tsubst_decl (t, args, complain);
16515 /* Give it the template pattern as its context; its true context
16516 hasn't been instantiated yet and this is good enough for
16517 mangling. */
16518 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16519 }
16520
16521 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16522 r = argument_pack_select_arg (r);
16523 if (!mark_used (r, complain) && !(complain & tf_error))
16524 return error_mark_node;
16525 return r;
16526
16527 case CONST_DECL:
16528 {
16529 tree enum_type;
16530 tree v;
16531
16532 if (DECL_TEMPLATE_PARM_P (t))
16533 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16534 /* There is no need to substitute into namespace-scope
16535 enumerators. */
16536 if (DECL_NAMESPACE_SCOPE_P (t))
16537 return t;
16538 /* If ARGS is NULL, then T is known to be non-dependent. */
16539 if (args == NULL_TREE)
16540 return scalar_constant_value (t);
16541
16542 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16543 return ref;
16544
16545 /* Unfortunately, we cannot just call lookup_name here.
16546 Consider:
16547
16548 template <int I> int f() {
16549 enum E { a = I };
16550 struct S { void g() { E e = a; } };
16551 };
16552
16553 When we instantiate f<7>::S::g(), say, lookup_name is not
16554 clever enough to find f<7>::a. */
16555 enum_type
16556 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16557 /*entering_scope=*/0);
16558
16559 for (v = TYPE_VALUES (enum_type);
16560 v != NULL_TREE;
16561 v = TREE_CHAIN (v))
16562 if (TREE_PURPOSE (v) == DECL_NAME (t))
16563 return TREE_VALUE (v);
16564
16565 /* We didn't find the name. That should never happen; if
16566 name-lookup found it during preliminary parsing, we
16567 should find it again here during instantiation. */
16568 gcc_unreachable ();
16569 }
16570 return t;
16571
16572 case FIELD_DECL:
16573 if (DECL_CONTEXT (t))
16574 {
16575 tree ctx;
16576
16577 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16578 /*entering_scope=*/1);
16579 if (ctx != DECL_CONTEXT (t))
16580 {
16581 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16582 if (!r)
16583 {
16584 if (complain & tf_error)
16585 error ("using invalid field %qD", t);
16586 return error_mark_node;
16587 }
16588 return r;
16589 }
16590 }
16591
16592 return t;
16593
16594 case VAR_DECL:
16595 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16596 return ref;
16597 gcc_fallthrough();
16598 case FUNCTION_DECL:
16599 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16600 r = tsubst (t, args, complain, in_decl);
16601 else if (DECL_LOCAL_DECL_P (t))
16602 {
16603 /* Local specialization will have been created when we
16604 instantiated the DECL_EXPR_DECL. */
16605 r = retrieve_local_specialization (t);
16606 if (!r)
16607 r = error_mark_node;
16608 }
16609 else if (local_variable_p (t)
16610 && uses_template_parms (DECL_CONTEXT (t)))
16611 {
16612 r = retrieve_local_specialization (t);
16613 if (r == NULL_TREE)
16614 {
16615 /* First try name lookup to find the instantiation. */
16616 r = lookup_name (DECL_NAME (t));
16617 if (r)
16618 {
16619 if (!VAR_P (r))
16620 {
16621 /* During error-recovery we may find a non-variable,
16622 even an OVERLOAD: just bail out and avoid ICEs and
16623 duplicate diagnostics (c++/62207). */
16624 gcc_assert (seen_error ());
16625 return error_mark_node;
16626 }
16627 if (!is_capture_proxy (r))
16628 {
16629 /* Make sure the one we found is the one we want. */
16630 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16631 if (ctx != DECL_CONTEXT (r))
16632 r = NULL_TREE;
16633 }
16634 }
16635
16636 if (r)
16637 /* OK */;
16638 else
16639 {
16640 /* This can happen for a variable used in a
16641 late-specified return type of a local lambda, or for a
16642 local static or constant. Building a new VAR_DECL
16643 should be OK in all those cases. */
16644 r = tsubst_decl (t, args, complain);
16645 if (local_specializations)
16646 /* Avoid infinite recursion (79640). */
16647 register_local_specialization (r, t);
16648 if (decl_maybe_constant_var_p (r))
16649 {
16650 /* We can't call cp_finish_decl, so handle the
16651 initializer by hand. */
16652 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16653 complain, in_decl);
16654 if (!processing_template_decl)
16655 init = maybe_constant_init (init);
16656 if (processing_template_decl
16657 ? potential_constant_expression (init)
16658 : reduced_constant_expression_p (init))
16659 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16660 = TREE_CONSTANT (r) = true;
16661 DECL_INITIAL (r) = init;
16662 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16663 TREE_TYPE (r)
16664 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16665 complain, adc_variable_type);
16666 }
16667 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16668 || decl_constant_var_p (r)
16669 || seen_error ());
16670 if (!processing_template_decl
16671 && !TREE_STATIC (r))
16672 r = process_outer_var_ref (r, complain);
16673 }
16674 /* Remember this for subsequent uses. */
16675 if (local_specializations)
16676 register_local_specialization (r, t);
16677 }
16678 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16679 r = argument_pack_select_arg (r);
16680 }
16681 else
16682 r = t;
16683 if (!mark_used (r, complain))
16684 return error_mark_node;
16685 return r;
16686
16687 case NAMESPACE_DECL:
16688 return t;
16689
16690 case OVERLOAD:
16691 return t;
16692
16693 case BASELINK:
16694 return tsubst_baselink (t, current_nonlambda_class_type (),
16695 args, complain, in_decl);
16696
16697 case TEMPLATE_DECL:
16698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16699 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16700 args, complain, in_decl);
16701 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16702 return tsubst (t, args, complain, in_decl);
16703 else if (DECL_CLASS_SCOPE_P (t)
16704 && uses_template_parms (DECL_CONTEXT (t)))
16705 {
16706 /* Template template argument like the following example need
16707 special treatment:
16708
16709 template <template <class> class TT> struct C {};
16710 template <class T> struct D {
16711 template <class U> struct E {};
16712 C<E> c; // #1
16713 };
16714 D<int> d; // #2
16715
16716 We are processing the template argument `E' in #1 for
16717 the template instantiation #2. Originally, `E' is a
16718 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16719 have to substitute this with one having context `D<int>'. */
16720
16721 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16722 if (dependent_scope_p (context))
16723 {
16724 /* When rewriting a constructor into a deduction guide, a
16725 non-dependent name can become dependent, so memtmpl<args>
16726 becomes context::template memtmpl<args>. */
16727 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16728 return build_qualified_name (type, context, DECL_NAME (t),
16729 /*template*/true);
16730 }
16731 return lookup_field (context, DECL_NAME(t), 0, false);
16732 }
16733 else
16734 /* Ordinary template template argument. */
16735 return t;
16736
16737 case NON_LVALUE_EXPR:
16738 case VIEW_CONVERT_EXPR:
16739 {
16740 /* Handle location wrappers by substituting the wrapped node
16741 first, *then* reusing the resulting type. Doing the type
16742 first ensures that we handle template parameters and
16743 parameter pack expansions. */
16744 if (location_wrapper_p (t))
16745 {
16746 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16747 complain, in_decl);
16748 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16749 }
16750 tree op = TREE_OPERAND (t, 0);
16751 if (code == VIEW_CONVERT_EXPR
16752 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16753 {
16754 /* Wrapper to make a C++20 template parameter object const. */
16755 op = tsubst_copy (op, args, complain, in_decl);
16756 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16757 {
16758 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16759 return build1 (code, type, op);
16760 }
16761 else
16762 {
16763 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16764 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16765 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16766 return op;
16767 }
16768 }
16769 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16770 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16771 {
16772 op = tsubst_copy (op, args, complain, in_decl);
16773 op = build1 (code, TREE_TYPE (op), op);
16774 REF_PARENTHESIZED_P (op) = true;
16775 return op;
16776 }
16777 /* We shouldn't see any other uses of these in templates. */
16778 gcc_unreachable ();
16779 }
16780
16781 case CAST_EXPR:
16782 case REINTERPRET_CAST_EXPR:
16783 case CONST_CAST_EXPR:
16784 case STATIC_CAST_EXPR:
16785 case DYNAMIC_CAST_EXPR:
16786 case IMPLICIT_CONV_EXPR:
16787 case CONVERT_EXPR:
16788 case NOP_EXPR:
16789 {
16790 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16791 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16792 return build1 (code, type, op0);
16793 }
16794
16795 case BIT_CAST_EXPR:
16796 {
16797 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16798 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16799 r = build_min (BIT_CAST_EXPR, type, op0);
16800 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16801 return r;
16802 }
16803
16804 case SIZEOF_EXPR:
16805 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16806 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16807 {
16808 tree expanded, op = TREE_OPERAND (t, 0);
16809 int len = 0;
16810
16811 if (SIZEOF_EXPR_TYPE_P (t))
16812 op = TREE_TYPE (op);
16813
16814 ++cp_unevaluated_operand;
16815 ++c_inhibit_evaluation_warnings;
16816 /* We only want to compute the number of arguments. */
16817 if (PACK_EXPANSION_P (op))
16818 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16819 else
16820 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16821 args, complain, in_decl);
16822 --cp_unevaluated_operand;
16823 --c_inhibit_evaluation_warnings;
16824
16825 if (TREE_CODE (expanded) == TREE_VEC)
16826 {
16827 len = TREE_VEC_LENGTH (expanded);
16828 /* Set TREE_USED for the benefit of -Wunused. */
16829 for (int i = 0; i < len; i++)
16830 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16831 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16832 }
16833
16834 if (expanded == error_mark_node)
16835 return error_mark_node;
16836 else if (PACK_EXPANSION_P (expanded)
16837 || (TREE_CODE (expanded) == TREE_VEC
16838 && pack_expansion_args_count (expanded)))
16839
16840 {
16841 if (PACK_EXPANSION_P (expanded))
16842 /* OK. */;
16843 else if (TREE_VEC_LENGTH (expanded) == 1)
16844 expanded = TREE_VEC_ELT (expanded, 0);
16845 else
16846 expanded = make_argument_pack (expanded);
16847
16848 if (TYPE_P (expanded))
16849 return cxx_sizeof_or_alignof_type (input_location,
16850 expanded, SIZEOF_EXPR,
16851 false,
16852 complain & tf_error);
16853 else
16854 return cxx_sizeof_or_alignof_expr (input_location,
16855 expanded, SIZEOF_EXPR,
16856 false,
16857 complain & tf_error);
16858 }
16859 else
16860 return build_int_cst (size_type_node, len);
16861 }
16862 if (SIZEOF_EXPR_TYPE_P (t))
16863 {
16864 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16865 args, complain, in_decl);
16866 r = build1 (NOP_EXPR, r, error_mark_node);
16867 r = build1 (SIZEOF_EXPR,
16868 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16869 SIZEOF_EXPR_TYPE_P (r) = 1;
16870 return r;
16871 }
16872 /* Fall through */
16873
16874 case INDIRECT_REF:
16875 case NEGATE_EXPR:
16876 case TRUTH_NOT_EXPR:
16877 case BIT_NOT_EXPR:
16878 case ADDR_EXPR:
16879 case UNARY_PLUS_EXPR: /* Unary + */
16880 case ALIGNOF_EXPR:
16881 case AT_ENCODE_EXPR:
16882 case ARROW_EXPR:
16883 case THROW_EXPR:
16884 case TYPEID_EXPR:
16885 case REALPART_EXPR:
16886 case IMAGPART_EXPR:
16887 case PAREN_EXPR:
16888 {
16889 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16890 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16891 r = build1 (code, type, op0);
16892 if (code == ALIGNOF_EXPR)
16893 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16894 return r;
16895 }
16896
16897 case COMPONENT_REF:
16898 {
16899 tree object;
16900 tree name;
16901
16902 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16903 name = TREE_OPERAND (t, 1);
16904 if (TREE_CODE (name) == BIT_NOT_EXPR)
16905 {
16906 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16907 complain, in_decl);
16908 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16909 }
16910 else if (TREE_CODE (name) == SCOPE_REF
16911 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16912 {
16913 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16914 complain, in_decl);
16915 name = TREE_OPERAND (name, 1);
16916 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16917 complain, in_decl);
16918 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16919 name = build_qualified_name (/*type=*/NULL_TREE,
16920 base, name,
16921 /*template_p=*/false);
16922 }
16923 else if (BASELINK_P (name))
16924 name = tsubst_baselink (name,
16925 non_reference (TREE_TYPE (object)),
16926 args, complain,
16927 in_decl);
16928 else
16929 name = tsubst_copy (name, args, complain, in_decl);
16930 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16931 }
16932
16933 case PLUS_EXPR:
16934 case MINUS_EXPR:
16935 case MULT_EXPR:
16936 case TRUNC_DIV_EXPR:
16937 case CEIL_DIV_EXPR:
16938 case FLOOR_DIV_EXPR:
16939 case ROUND_DIV_EXPR:
16940 case EXACT_DIV_EXPR:
16941 case BIT_AND_EXPR:
16942 case BIT_IOR_EXPR:
16943 case BIT_XOR_EXPR:
16944 case TRUNC_MOD_EXPR:
16945 case FLOOR_MOD_EXPR:
16946 case TRUTH_ANDIF_EXPR:
16947 case TRUTH_ORIF_EXPR:
16948 case TRUTH_AND_EXPR:
16949 case TRUTH_OR_EXPR:
16950 case RSHIFT_EXPR:
16951 case LSHIFT_EXPR:
16952 case EQ_EXPR:
16953 case NE_EXPR:
16954 case MAX_EXPR:
16955 case MIN_EXPR:
16956 case LE_EXPR:
16957 case GE_EXPR:
16958 case LT_EXPR:
16959 case GT_EXPR:
16960 case COMPOUND_EXPR:
16961 case DOTSTAR_EXPR:
16962 case MEMBER_REF:
16963 case PREDECREMENT_EXPR:
16964 case PREINCREMENT_EXPR:
16965 case POSTDECREMENT_EXPR:
16966 case POSTINCREMENT_EXPR:
16967 {
16968 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16969 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16970 return build_nt (code, op0, op1);
16971 }
16972
16973 case SCOPE_REF:
16974 {
16975 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16976 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16977 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16978 QUALIFIED_NAME_IS_TEMPLATE (t));
16979 }
16980
16981 case ARRAY_REF:
16982 {
16983 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16984 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16985 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16986 }
16987
16988 case CALL_EXPR:
16989 {
16990 int n = VL_EXP_OPERAND_LENGTH (t);
16991 tree result = build_vl_exp (CALL_EXPR, n);
16992 int i;
16993 for (i = 0; i < n; i++)
16994 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16995 complain, in_decl);
16996 return result;
16997 }
16998
16999 case COND_EXPR:
17000 case MODOP_EXPR:
17001 case PSEUDO_DTOR_EXPR:
17002 case VEC_PERM_EXPR:
17003 {
17004 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17005 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17006 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17007 r = build_nt (code, op0, op1, op2);
17008 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17009 return r;
17010 }
17011
17012 case NEW_EXPR:
17013 {
17014 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17015 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17016 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17017 r = build_nt (code, op0, op1, op2);
17018 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17019 return r;
17020 }
17021
17022 case DELETE_EXPR:
17023 {
17024 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17025 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17026 r = build_nt (code, op0, op1);
17027 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17028 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17029 return r;
17030 }
17031
17032 case TEMPLATE_ID_EXPR:
17033 {
17034 /* Substituted template arguments */
17035 tree fn = TREE_OPERAND (t, 0);
17036 tree targs = TREE_OPERAND (t, 1);
17037
17038 fn = tsubst_copy (fn, args, complain, in_decl);
17039 if (targs)
17040 targs = tsubst_template_args (targs, args, complain, in_decl);
17041
17042 return lookup_template_function (fn, targs);
17043 }
17044
17045 case TREE_LIST:
17046 {
17047 tree purpose, value, chain;
17048
17049 if (t == void_list_node)
17050 return t;
17051
17052 purpose = TREE_PURPOSE (t);
17053 if (purpose)
17054 purpose = tsubst_copy (purpose, args, complain, in_decl);
17055 value = TREE_VALUE (t);
17056 if (value)
17057 value = tsubst_copy (value, args, complain, in_decl);
17058 chain = TREE_CHAIN (t);
17059 if (chain && chain != void_type_node)
17060 chain = tsubst_copy (chain, args, complain, in_decl);
17061 if (purpose == TREE_PURPOSE (t)
17062 && value == TREE_VALUE (t)
17063 && chain == TREE_CHAIN (t))
17064 return t;
17065 return tree_cons (purpose, value, chain);
17066 }
17067
17068 case RECORD_TYPE:
17069 case UNION_TYPE:
17070 case ENUMERAL_TYPE:
17071 case INTEGER_TYPE:
17072 case TEMPLATE_TYPE_PARM:
17073 case TEMPLATE_TEMPLATE_PARM:
17074 case BOUND_TEMPLATE_TEMPLATE_PARM:
17075 case TEMPLATE_PARM_INDEX:
17076 case POINTER_TYPE:
17077 case REFERENCE_TYPE:
17078 case OFFSET_TYPE:
17079 case FUNCTION_TYPE:
17080 case METHOD_TYPE:
17081 case ARRAY_TYPE:
17082 case TYPENAME_TYPE:
17083 case UNBOUND_CLASS_TEMPLATE:
17084 case TYPEOF_TYPE:
17085 case DECLTYPE_TYPE:
17086 case TYPE_DECL:
17087 return tsubst (t, args, complain, in_decl);
17088
17089 case USING_DECL:
17090 t = DECL_NAME (t);
17091 /* Fall through. */
17092 case IDENTIFIER_NODE:
17093 if (IDENTIFIER_CONV_OP_P (t))
17094 {
17095 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17096 return make_conv_op_name (new_type);
17097 }
17098 else
17099 return t;
17100
17101 case CONSTRUCTOR:
17102 /* This is handled by tsubst_copy_and_build. */
17103 gcc_unreachable ();
17104
17105 case VA_ARG_EXPR:
17106 {
17107 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17108 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17109 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17110 }
17111
17112 case CLEANUP_POINT_EXPR:
17113 /* We shouldn't have built any of these during initial template
17114 generation. Instead, they should be built during instantiation
17115 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17116 gcc_unreachable ();
17117
17118 case OFFSET_REF:
17119 {
17120 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17121 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17122 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17123 r = build2 (code, type, op0, op1);
17124 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17125 if (!mark_used (TREE_OPERAND (r, 1), complain)
17126 && !(complain & tf_error))
17127 return error_mark_node;
17128 return r;
17129 }
17130
17131 case EXPR_PACK_EXPANSION:
17132 error ("invalid use of pack expansion expression");
17133 return error_mark_node;
17134
17135 case NONTYPE_ARGUMENT_PACK:
17136 error ("use %<...%> to expand argument pack");
17137 return error_mark_node;
17138
17139 case VOID_CST:
17140 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17141 return t;
17142
17143 case INTEGER_CST:
17144 case REAL_CST:
17145 case COMPLEX_CST:
17146 {
17147 /* Instantiate any typedefs in the type. */
17148 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17149 r = fold_convert (type, t);
17150 gcc_assert (TREE_CODE (r) == code);
17151 return r;
17152 }
17153
17154 case STRING_CST:
17155 {
17156 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17157 r = t;
17158 if (type != TREE_TYPE (t))
17159 {
17160 r = copy_node (t);
17161 TREE_TYPE (r) = type;
17162 }
17163 return r;
17164 }
17165
17166 case PTRMEM_CST:
17167 /* These can sometimes show up in a partial instantiation, but never
17168 involve template parms. */
17169 gcc_assert (!uses_template_parms (t));
17170 return t;
17171
17172 case UNARY_LEFT_FOLD_EXPR:
17173 return tsubst_unary_left_fold (t, args, complain, in_decl);
17174 case UNARY_RIGHT_FOLD_EXPR:
17175 return tsubst_unary_right_fold (t, args, complain, in_decl);
17176 case BINARY_LEFT_FOLD_EXPR:
17177 return tsubst_binary_left_fold (t, args, complain, in_decl);
17178 case BINARY_RIGHT_FOLD_EXPR:
17179 return tsubst_binary_right_fold (t, args, complain, in_decl);
17180 case PREDICT_EXPR:
17181 return t;
17182
17183 case DEBUG_BEGIN_STMT:
17184 /* ??? There's no point in copying it for now, but maybe some
17185 day it will contain more information, such as a pointer back
17186 to the containing function, inlined copy or so. */
17187 return t;
17188
17189 case CO_AWAIT_EXPR:
17190 return tsubst_expr (t, args, complain, in_decl,
17191 /*integral_constant_expression_p=*/false);
17192 break;
17193
17194 default:
17195 /* We shouldn't get here, but keep going if !flag_checking. */
17196 if (flag_checking)
17197 gcc_unreachable ();
17198 return t;
17199 }
17200 }
17201
17202 /* Helper function for tsubst_omp_clauses, used for instantiation of
17203 OMP_CLAUSE_DECL of clauses. */
17204
17205 static tree
17206 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17207 tree in_decl, tree *iterator_cache)
17208 {
17209 if (decl == NULL_TREE)
17210 return NULL_TREE;
17211
17212 /* Handle OpenMP iterators. */
17213 if (TREE_CODE (decl) == TREE_LIST
17214 && TREE_PURPOSE (decl)
17215 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17216 {
17217 tree ret;
17218 if (iterator_cache[0] == TREE_PURPOSE (decl))
17219 ret = iterator_cache[1];
17220 else
17221 {
17222 tree *tp = &ret;
17223 begin_scope (sk_omp, NULL);
17224 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17225 {
17226 *tp = copy_node (it);
17227 TREE_VEC_ELT (*tp, 0)
17228 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17229 TREE_VEC_ELT (*tp, 1)
17230 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17231 /*integral_constant_expression_p=*/false);
17232 TREE_VEC_ELT (*tp, 2)
17233 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17234 /*integral_constant_expression_p=*/false);
17235 TREE_VEC_ELT (*tp, 3)
17236 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17237 /*integral_constant_expression_p=*/false);
17238 TREE_CHAIN (*tp) = NULL_TREE;
17239 tp = &TREE_CHAIN (*tp);
17240 }
17241 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17242 iterator_cache[0] = TREE_PURPOSE (decl);
17243 iterator_cache[1] = ret;
17244 }
17245 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17246 args, complain,
17247 in_decl, NULL));
17248 }
17249
17250 /* Handle an OpenMP array section represented as a TREE_LIST (or
17251 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17252 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17253 TREE_LIST. We can handle it exactly the same as an array section
17254 (purpose, value, and a chain), even though the nomenclature
17255 (low_bound, length, etc) is different. */
17256 if (TREE_CODE (decl) == TREE_LIST)
17257 {
17258 tree low_bound
17259 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17260 /*integral_constant_expression_p=*/false);
17261 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17262 /*integral_constant_expression_p=*/false);
17263 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17264 in_decl, NULL);
17265 if (TREE_PURPOSE (decl) == low_bound
17266 && TREE_VALUE (decl) == length
17267 && TREE_CHAIN (decl) == chain)
17268 return decl;
17269 tree ret = tree_cons (low_bound, length, chain);
17270 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17271 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17272 return ret;
17273 }
17274 tree ret = tsubst_expr (decl, args, complain, in_decl,
17275 /*integral_constant_expression_p=*/false);
17276 /* Undo convert_from_reference tsubst_expr could have called. */
17277 if (decl
17278 && REFERENCE_REF_P (ret)
17279 && !REFERENCE_REF_P (decl))
17280 ret = TREE_OPERAND (ret, 0);
17281 return ret;
17282 }
17283
17284 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17285
17286 static tree
17287 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17288 tree args, tsubst_flags_t complain, tree in_decl)
17289 {
17290 tree new_clauses = NULL_TREE, nc, oc;
17291 tree linear_no_step = NULL_TREE;
17292 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17293
17294 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17295 {
17296 nc = copy_node (oc);
17297 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17298 new_clauses = nc;
17299
17300 switch (OMP_CLAUSE_CODE (nc))
17301 {
17302 case OMP_CLAUSE_LASTPRIVATE:
17303 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17304 {
17305 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17306 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17307 in_decl, /*integral_constant_expression_p=*/false);
17308 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17309 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17310 }
17311 /* FALLTHRU */
17312 case OMP_CLAUSE_PRIVATE:
17313 case OMP_CLAUSE_SHARED:
17314 case OMP_CLAUSE_FIRSTPRIVATE:
17315 case OMP_CLAUSE_COPYIN:
17316 case OMP_CLAUSE_COPYPRIVATE:
17317 case OMP_CLAUSE_UNIFORM:
17318 case OMP_CLAUSE_DEPEND:
17319 case OMP_CLAUSE_FROM:
17320 case OMP_CLAUSE_TO:
17321 case OMP_CLAUSE_MAP:
17322 case OMP_CLAUSE__CACHE_:
17323 case OMP_CLAUSE_NONTEMPORAL:
17324 case OMP_CLAUSE_USE_DEVICE_PTR:
17325 case OMP_CLAUSE_USE_DEVICE_ADDR:
17326 case OMP_CLAUSE_IS_DEVICE_PTR:
17327 case OMP_CLAUSE_INCLUSIVE:
17328 case OMP_CLAUSE_EXCLUSIVE:
17329 OMP_CLAUSE_DECL (nc)
17330 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17331 in_decl, iterator_cache);
17332 break;
17333 case OMP_CLAUSE_TILE:
17334 case OMP_CLAUSE_IF:
17335 case OMP_CLAUSE_NUM_THREADS:
17336 case OMP_CLAUSE_SCHEDULE:
17337 case OMP_CLAUSE_COLLAPSE:
17338 case OMP_CLAUSE_FINAL:
17339 case OMP_CLAUSE_DEVICE:
17340 case OMP_CLAUSE_DIST_SCHEDULE:
17341 case OMP_CLAUSE_NUM_TEAMS:
17342 case OMP_CLAUSE_THREAD_LIMIT:
17343 case OMP_CLAUSE_SAFELEN:
17344 case OMP_CLAUSE_SIMDLEN:
17345 case OMP_CLAUSE_NUM_TASKS:
17346 case OMP_CLAUSE_GRAINSIZE:
17347 case OMP_CLAUSE_PRIORITY:
17348 case OMP_CLAUSE_ORDERED:
17349 case OMP_CLAUSE_HINT:
17350 case OMP_CLAUSE_NUM_GANGS:
17351 case OMP_CLAUSE_NUM_WORKERS:
17352 case OMP_CLAUSE_VECTOR_LENGTH:
17353 case OMP_CLAUSE_WORKER:
17354 case OMP_CLAUSE_VECTOR:
17355 case OMP_CLAUSE_ASYNC:
17356 case OMP_CLAUSE_WAIT:
17357 case OMP_CLAUSE_DETACH:
17358 OMP_CLAUSE_OPERAND (nc, 0)
17359 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17360 in_decl, /*integral_constant_expression_p=*/false);
17361 break;
17362 case OMP_CLAUSE_REDUCTION:
17363 case OMP_CLAUSE_IN_REDUCTION:
17364 case OMP_CLAUSE_TASK_REDUCTION:
17365 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17366 {
17367 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17368 if (TREE_CODE (placeholder) == SCOPE_REF)
17369 {
17370 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17371 complain, in_decl);
17372 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17373 = build_qualified_name (NULL_TREE, scope,
17374 TREE_OPERAND (placeholder, 1),
17375 false);
17376 }
17377 else
17378 gcc_assert (identifier_p (placeholder));
17379 }
17380 OMP_CLAUSE_DECL (nc)
17381 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17382 in_decl, NULL);
17383 break;
17384 case OMP_CLAUSE_GANG:
17385 case OMP_CLAUSE_ALIGNED:
17386 case OMP_CLAUSE_ALLOCATE:
17387 OMP_CLAUSE_DECL (nc)
17388 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17389 in_decl, NULL);
17390 OMP_CLAUSE_OPERAND (nc, 1)
17391 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17392 in_decl, /*integral_constant_expression_p=*/false);
17393 break;
17394 case OMP_CLAUSE_LINEAR:
17395 OMP_CLAUSE_DECL (nc)
17396 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17397 in_decl, NULL);
17398 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17399 {
17400 gcc_assert (!linear_no_step);
17401 linear_no_step = nc;
17402 }
17403 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17404 OMP_CLAUSE_LINEAR_STEP (nc)
17405 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17406 complain, in_decl, NULL);
17407 else
17408 OMP_CLAUSE_LINEAR_STEP (nc)
17409 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17410 in_decl,
17411 /*integral_constant_expression_p=*/false);
17412 break;
17413 case OMP_CLAUSE_NOWAIT:
17414 case OMP_CLAUSE_DEFAULT:
17415 case OMP_CLAUSE_UNTIED:
17416 case OMP_CLAUSE_MERGEABLE:
17417 case OMP_CLAUSE_INBRANCH:
17418 case OMP_CLAUSE_NOTINBRANCH:
17419 case OMP_CLAUSE_PROC_BIND:
17420 case OMP_CLAUSE_FOR:
17421 case OMP_CLAUSE_PARALLEL:
17422 case OMP_CLAUSE_SECTIONS:
17423 case OMP_CLAUSE_TASKGROUP:
17424 case OMP_CLAUSE_NOGROUP:
17425 case OMP_CLAUSE_THREADS:
17426 case OMP_CLAUSE_SIMD:
17427 case OMP_CLAUSE_DEFAULTMAP:
17428 case OMP_CLAUSE_ORDER:
17429 case OMP_CLAUSE_BIND:
17430 case OMP_CLAUSE_INDEPENDENT:
17431 case OMP_CLAUSE_AUTO:
17432 case OMP_CLAUSE_SEQ:
17433 case OMP_CLAUSE_IF_PRESENT:
17434 case OMP_CLAUSE_FINALIZE:
17435 break;
17436 default:
17437 gcc_unreachable ();
17438 }
17439 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17440 switch (OMP_CLAUSE_CODE (nc))
17441 {
17442 case OMP_CLAUSE_SHARED:
17443 case OMP_CLAUSE_PRIVATE:
17444 case OMP_CLAUSE_FIRSTPRIVATE:
17445 case OMP_CLAUSE_LASTPRIVATE:
17446 case OMP_CLAUSE_COPYPRIVATE:
17447 case OMP_CLAUSE_LINEAR:
17448 case OMP_CLAUSE_REDUCTION:
17449 case OMP_CLAUSE_IN_REDUCTION:
17450 case OMP_CLAUSE_TASK_REDUCTION:
17451 case OMP_CLAUSE_USE_DEVICE_PTR:
17452 case OMP_CLAUSE_USE_DEVICE_ADDR:
17453 case OMP_CLAUSE_IS_DEVICE_PTR:
17454 case OMP_CLAUSE_INCLUSIVE:
17455 case OMP_CLAUSE_EXCLUSIVE:
17456 case OMP_CLAUSE_ALLOCATE:
17457 /* tsubst_expr on SCOPE_REF results in returning
17458 finish_non_static_data_member result. Undo that here. */
17459 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17460 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17461 == IDENTIFIER_NODE))
17462 {
17463 tree t = OMP_CLAUSE_DECL (nc);
17464 tree v = t;
17465 while (v)
17466 switch (TREE_CODE (v))
17467 {
17468 case COMPONENT_REF:
17469 case MEM_REF:
17470 case INDIRECT_REF:
17471 CASE_CONVERT:
17472 case POINTER_PLUS_EXPR:
17473 v = TREE_OPERAND (v, 0);
17474 continue;
17475 case PARM_DECL:
17476 if (DECL_CONTEXT (v) == current_function_decl
17477 && DECL_ARTIFICIAL (v)
17478 && DECL_NAME (v) == this_identifier)
17479 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17480 /* FALLTHRU */
17481 default:
17482 v = NULL_TREE;
17483 break;
17484 }
17485 }
17486 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17487 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17488 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17489 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17490 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17491 {
17492 tree decl = OMP_CLAUSE_DECL (nc);
17493 if (VAR_P (decl))
17494 {
17495 retrofit_lang_decl (decl);
17496 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17497 }
17498 }
17499 break;
17500 default:
17501 break;
17502 }
17503 }
17504
17505 new_clauses = nreverse (new_clauses);
17506 if (ort != C_ORT_OMP_DECLARE_SIMD)
17507 {
17508 new_clauses = finish_omp_clauses (new_clauses, ort);
17509 if (linear_no_step)
17510 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17511 if (nc == linear_no_step)
17512 {
17513 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17514 break;
17515 }
17516 }
17517 return new_clauses;
17518 }
17519
17520 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17521
17522 static tree
17523 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17524 tree in_decl)
17525 {
17526 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17527
17528 tree purpose, value, chain;
17529
17530 if (t == NULL)
17531 return t;
17532
17533 if (TREE_CODE (t) != TREE_LIST)
17534 return tsubst_copy_and_build (t, args, complain, in_decl,
17535 /*function_p=*/false,
17536 /*integral_constant_expression_p=*/false);
17537
17538 if (t == void_list_node)
17539 return t;
17540
17541 purpose = TREE_PURPOSE (t);
17542 if (purpose)
17543 purpose = RECUR (purpose);
17544 value = TREE_VALUE (t);
17545 if (value)
17546 {
17547 if (TREE_CODE (value) != LABEL_DECL)
17548 value = RECUR (value);
17549 else
17550 {
17551 value = lookup_label (DECL_NAME (value));
17552 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17553 TREE_USED (value) = 1;
17554 }
17555 }
17556 chain = TREE_CHAIN (t);
17557 if (chain && chain != void_type_node)
17558 chain = RECUR (chain);
17559 return tree_cons (purpose, value, chain);
17560 #undef RECUR
17561 }
17562
17563 /* Used to temporarily communicate the list of #pragma omp parallel
17564 clauses to #pragma omp for instantiation if they are combined
17565 together. */
17566
17567 static tree *omp_parallel_combined_clauses;
17568
17569 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17570 tree *, unsigned int *);
17571
17572 /* Substitute one OMP_FOR iterator. */
17573
17574 static bool
17575 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17576 tree initv, tree condv, tree incrv, tree *clauses,
17577 tree args, tsubst_flags_t complain, tree in_decl,
17578 bool integral_constant_expression_p)
17579 {
17580 #define RECUR(NODE) \
17581 tsubst_expr ((NODE), args, complain, in_decl, \
17582 integral_constant_expression_p)
17583 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17584 bool ret = false;
17585
17586 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17587 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17588
17589 decl = TREE_OPERAND (init, 0);
17590 init = TREE_OPERAND (init, 1);
17591 tree decl_expr = NULL_TREE;
17592 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17593 if (range_for)
17594 {
17595 bool decomp = false;
17596 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17597 {
17598 tree v = DECL_VALUE_EXPR (decl);
17599 if (TREE_CODE (v) == ARRAY_REF
17600 && VAR_P (TREE_OPERAND (v, 0))
17601 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17602 {
17603 tree decomp_first = NULL_TREE;
17604 unsigned decomp_cnt = 0;
17605 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17606 maybe_push_decl (d);
17607 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17608 in_decl, &decomp_first, &decomp_cnt);
17609 decomp = true;
17610 if (d == error_mark_node)
17611 decl = error_mark_node;
17612 else
17613 for (unsigned int i = 0; i < decomp_cnt; i++)
17614 {
17615 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17616 {
17617 tree v = build_nt (ARRAY_REF, d,
17618 size_int (decomp_cnt - i - 1),
17619 NULL_TREE, NULL_TREE);
17620 SET_DECL_VALUE_EXPR (decomp_first, v);
17621 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17622 }
17623 fit_decomposition_lang_decl (decomp_first, d);
17624 decomp_first = DECL_CHAIN (decomp_first);
17625 }
17626 }
17627 }
17628 decl = tsubst_decl (decl, args, complain);
17629 if (!decomp)
17630 maybe_push_decl (decl);
17631 }
17632 else if (init && TREE_CODE (init) == DECL_EXPR)
17633 {
17634 /* We need to jump through some hoops to handle declarations in the
17635 init-statement, since we might need to handle auto deduction,
17636 but we need to keep control of initialization. */
17637 decl_expr = init;
17638 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17639 decl = tsubst_decl (decl, args, complain);
17640 }
17641 else
17642 {
17643 if (TREE_CODE (decl) == SCOPE_REF)
17644 {
17645 decl = RECUR (decl);
17646 if (TREE_CODE (decl) == COMPONENT_REF)
17647 {
17648 tree v = decl;
17649 while (v)
17650 switch (TREE_CODE (v))
17651 {
17652 case COMPONENT_REF:
17653 case MEM_REF:
17654 case INDIRECT_REF:
17655 CASE_CONVERT:
17656 case POINTER_PLUS_EXPR:
17657 v = TREE_OPERAND (v, 0);
17658 continue;
17659 case PARM_DECL:
17660 if (DECL_CONTEXT (v) == current_function_decl
17661 && DECL_ARTIFICIAL (v)
17662 && DECL_NAME (v) == this_identifier)
17663 {
17664 decl = TREE_OPERAND (decl, 1);
17665 decl = omp_privatize_field (decl, false);
17666 }
17667 /* FALLTHRU */
17668 default:
17669 v = NULL_TREE;
17670 break;
17671 }
17672 }
17673 }
17674 else
17675 decl = RECUR (decl);
17676 }
17677 if (init && TREE_CODE (init) == TREE_VEC)
17678 {
17679 init = copy_node (init);
17680 TREE_VEC_ELT (init, 0)
17681 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17682 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17683 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17684 }
17685 else
17686 init = RECUR (init);
17687
17688 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17689 {
17690 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17691 if (TREE_CODE (o) == TREE_LIST)
17692 TREE_VEC_ELT (orig_declv, i)
17693 = tree_cons (RECUR (TREE_PURPOSE (o)),
17694 RECUR (TREE_VALUE (o)),
17695 NULL_TREE);
17696 else
17697 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17698 }
17699
17700 if (range_for)
17701 {
17702 tree this_pre_body = NULL_TREE;
17703 tree orig_init = NULL_TREE;
17704 tree orig_decl = NULL_TREE;
17705 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17706 orig_init, cond, incr);
17707 if (orig_decl)
17708 {
17709 if (orig_declv == NULL_TREE)
17710 orig_declv = copy_node (declv);
17711 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17712 ret = true;
17713 }
17714 else if (orig_declv)
17715 TREE_VEC_ELT (orig_declv, i) = decl;
17716 }
17717
17718 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17719 if (!range_for && auto_node && init)
17720 TREE_TYPE (decl)
17721 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17722
17723 gcc_assert (!type_dependent_expression_p (decl));
17724
17725 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17726 {
17727 if (decl_expr)
17728 {
17729 /* Declare the variable, but don't let that initialize it. */
17730 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17731 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17732 RECUR (decl_expr);
17733 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17734 }
17735
17736 if (!range_for)
17737 {
17738 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17739 if (COMPARISON_CLASS_P (cond)
17740 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17741 {
17742 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17743 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17744 TREE_VEC_ELT (rhs, 0)
17745 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17746 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17747 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17748 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17749 lhs, rhs);
17750 }
17751 else
17752 cond = RECUR (cond);
17753 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17754 if (TREE_CODE (incr) == MODIFY_EXPR)
17755 {
17756 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17757 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17758 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17759 NOP_EXPR, rhs, complain);
17760 }
17761 else
17762 incr = RECUR (incr);
17763 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17764 TREE_VEC_ELT (orig_declv, i) = decl;
17765 }
17766 TREE_VEC_ELT (declv, i) = decl;
17767 TREE_VEC_ELT (initv, i) = init;
17768 TREE_VEC_ELT (condv, i) = cond;
17769 TREE_VEC_ELT (incrv, i) = incr;
17770 return ret;
17771 }
17772
17773 if (decl_expr)
17774 {
17775 /* Declare and initialize the variable. */
17776 RECUR (decl_expr);
17777 init = NULL_TREE;
17778 }
17779 else if (init)
17780 {
17781 tree *pc;
17782 int j;
17783 for (j = ((omp_parallel_combined_clauses == NULL
17784 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17785 {
17786 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17787 {
17788 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17789 && OMP_CLAUSE_DECL (*pc) == decl)
17790 break;
17791 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17792 && OMP_CLAUSE_DECL (*pc) == decl)
17793 {
17794 if (j)
17795 break;
17796 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17797 tree c = *pc;
17798 *pc = OMP_CLAUSE_CHAIN (c);
17799 OMP_CLAUSE_CHAIN (c) = *clauses;
17800 *clauses = c;
17801 }
17802 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17803 && OMP_CLAUSE_DECL (*pc) == decl)
17804 {
17805 error ("iteration variable %qD should not be firstprivate",
17806 decl);
17807 *pc = OMP_CLAUSE_CHAIN (*pc);
17808 }
17809 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17810 && OMP_CLAUSE_DECL (*pc) == decl)
17811 {
17812 error ("iteration variable %qD should not be reduction",
17813 decl);
17814 *pc = OMP_CLAUSE_CHAIN (*pc);
17815 }
17816 else
17817 pc = &OMP_CLAUSE_CHAIN (*pc);
17818 }
17819 if (*pc)
17820 break;
17821 }
17822 if (*pc == NULL_TREE)
17823 {
17824 tree c = build_omp_clause (input_location,
17825 TREE_CODE (t) == OMP_LOOP
17826 ? OMP_CLAUSE_LASTPRIVATE
17827 : OMP_CLAUSE_PRIVATE);
17828 OMP_CLAUSE_DECL (c) = decl;
17829 c = finish_omp_clauses (c, C_ORT_OMP);
17830 if (c)
17831 {
17832 OMP_CLAUSE_CHAIN (c) = *clauses;
17833 *clauses = c;
17834 }
17835 }
17836 }
17837 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17838 if (COMPARISON_CLASS_P (cond))
17839 {
17840 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17841 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17842 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17843 }
17844 else
17845 cond = RECUR (cond);
17846 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17847 switch (TREE_CODE (incr))
17848 {
17849 case PREINCREMENT_EXPR:
17850 case PREDECREMENT_EXPR:
17851 case POSTINCREMENT_EXPR:
17852 case POSTDECREMENT_EXPR:
17853 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17854 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17855 break;
17856 case MODIFY_EXPR:
17857 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17858 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17859 {
17860 tree rhs = TREE_OPERAND (incr, 1);
17861 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17862 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17863 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17864 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17865 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17866 rhs0, rhs1));
17867 }
17868 else
17869 incr = RECUR (incr);
17870 break;
17871 case MODOP_EXPR:
17872 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17873 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17874 {
17875 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17876 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17877 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17878 TREE_TYPE (decl), lhs,
17879 RECUR (TREE_OPERAND (incr, 2))));
17880 }
17881 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17882 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17883 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17884 {
17885 tree rhs = TREE_OPERAND (incr, 2);
17886 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17887 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17888 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17889 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17890 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17891 rhs0, rhs1));
17892 }
17893 else
17894 incr = RECUR (incr);
17895 break;
17896 default:
17897 incr = RECUR (incr);
17898 break;
17899 }
17900
17901 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17902 TREE_VEC_ELT (orig_declv, i) = decl;
17903 TREE_VEC_ELT (declv, i) = decl;
17904 TREE_VEC_ELT (initv, i) = init;
17905 TREE_VEC_ELT (condv, i) = cond;
17906 TREE_VEC_ELT (incrv, i) = incr;
17907 return false;
17908 #undef RECUR
17909 }
17910
17911 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17912 of OMP_TARGET's body. */
17913
17914 static tree
17915 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17916 {
17917 *walk_subtrees = 0;
17918 switch (TREE_CODE (*tp))
17919 {
17920 case OMP_TEAMS:
17921 return *tp;
17922 case BIND_EXPR:
17923 case STATEMENT_LIST:
17924 *walk_subtrees = 1;
17925 break;
17926 default:
17927 break;
17928 }
17929 return NULL_TREE;
17930 }
17931
17932 /* Helper function for tsubst_expr. For decomposition declaration
17933 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17934 also the corresponding decls representing the identifiers
17935 of the decomposition declaration. Return DECL if successful
17936 or error_mark_node otherwise, set *FIRST to the first decl
17937 in the list chained through DECL_CHAIN and *CNT to the number
17938 of such decls. */
17939
17940 static tree
17941 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17942 tsubst_flags_t complain, tree in_decl, tree *first,
17943 unsigned int *cnt)
17944 {
17945 tree decl2, decl3, prev = decl;
17946 *cnt = 0;
17947 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17948 for (decl2 = DECL_CHAIN (pattern_decl);
17949 decl2
17950 && VAR_P (decl2)
17951 && DECL_DECOMPOSITION_P (decl2)
17952 && DECL_NAME (decl2);
17953 decl2 = DECL_CHAIN (decl2))
17954 {
17955 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17956 {
17957 gcc_assert (errorcount);
17958 return error_mark_node;
17959 }
17960 (*cnt)++;
17961 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17962 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17963 tree v = DECL_VALUE_EXPR (decl2);
17964 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17965 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17966 decl3 = tsubst (decl2, args, complain, in_decl);
17967 SET_DECL_VALUE_EXPR (decl2, v);
17968 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17969 if (VAR_P (decl3))
17970 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17971 else
17972 {
17973 gcc_assert (errorcount);
17974 decl = error_mark_node;
17975 continue;
17976 }
17977 maybe_push_decl (decl3);
17978 if (error_operand_p (decl3))
17979 decl = error_mark_node;
17980 else if (decl != error_mark_node
17981 && DECL_CHAIN (decl3) != prev
17982 && decl != prev)
17983 {
17984 gcc_assert (errorcount);
17985 decl = error_mark_node;
17986 }
17987 else
17988 prev = decl3;
17989 }
17990 *first = prev;
17991 return decl;
17992 }
17993
17994 /* Return the proper local_specialization for init-capture pack DECL. */
17995
17996 static tree
17997 lookup_init_capture_pack (tree decl)
17998 {
17999 /* We handle normal pack captures by forwarding to the specialization of the
18000 captured parameter. We can't do that for pack init-captures; we need them
18001 to have their own local_specialization. We created the individual
18002 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18003 when we process the DECL_EXPR for the pack init-capture in the template.
18004 So, how do we find them? We don't know the capture proxy pack when
18005 building the individual resulting proxies, and we don't know the
18006 individual proxies when instantiating the pack. What we have in common is
18007 the FIELD_DECL.
18008
18009 So...when we instantiate the FIELD_DECL, we stick the result in
18010 local_specializations. Then at the DECL_EXPR we look up that result, see
18011 how many elements it has, synthesize the names, and look them up. */
18012
18013 tree cname = DECL_NAME (decl);
18014 tree val = DECL_VALUE_EXPR (decl);
18015 tree field = TREE_OPERAND (val, 1);
18016 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18017 tree fpack = retrieve_local_specialization (field);
18018 if (fpack == error_mark_node)
18019 return error_mark_node;
18020
18021 int len = 1;
18022 tree vec = NULL_TREE;
18023 tree r = NULL_TREE;
18024 if (TREE_CODE (fpack) == TREE_VEC)
18025 {
18026 len = TREE_VEC_LENGTH (fpack);
18027 vec = make_tree_vec (len);
18028 r = make_node (NONTYPE_ARGUMENT_PACK);
18029 SET_ARGUMENT_PACK_ARGS (r, vec);
18030 }
18031 for (int i = 0; i < len; ++i)
18032 {
18033 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18034 tree elt = lookup_name (ename);
18035 if (vec)
18036 TREE_VEC_ELT (vec, i) = elt;
18037 else
18038 r = elt;
18039 }
18040 return r;
18041 }
18042
18043 /* Like tsubst_copy for expressions, etc. but also does semantic
18044 processing. */
18045
18046 tree
18047 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18048 bool integral_constant_expression_p)
18049 {
18050 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18051 #define RECUR(NODE) \
18052 tsubst_expr ((NODE), args, complain, in_decl, \
18053 integral_constant_expression_p)
18054
18055 tree stmt, tmp;
18056 tree r;
18057 location_t loc;
18058
18059 if (t == NULL_TREE || t == error_mark_node)
18060 return t;
18061
18062 loc = input_location;
18063 if (location_t eloc = cp_expr_location (t))
18064 input_location = eloc;
18065 if (STATEMENT_CODE_P (TREE_CODE (t)))
18066 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18067
18068 switch (TREE_CODE (t))
18069 {
18070 case STATEMENT_LIST:
18071 {
18072 tree_stmt_iterator i;
18073 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
18074 RECUR (tsi_stmt (i));
18075 break;
18076 }
18077
18078 case CTOR_INITIALIZER:
18079 finish_mem_initializers (tsubst_initializer_list
18080 (TREE_OPERAND (t, 0), args));
18081 break;
18082
18083 case RETURN_EXPR:
18084 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18085 break;
18086
18087 case CO_RETURN_EXPR:
18088 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18089 break;
18090
18091 case CO_YIELD_EXPR:
18092 stmt = finish_co_yield_expr (input_location,
18093 RECUR (TREE_OPERAND (t, 0)));
18094 RETURN (stmt);
18095 break;
18096
18097 case CO_AWAIT_EXPR:
18098 stmt = finish_co_await_expr (input_location,
18099 RECUR (TREE_OPERAND (t, 0)));
18100 RETURN (stmt);
18101 break;
18102
18103 case EXPR_STMT:
18104 tmp = RECUR (EXPR_STMT_EXPR (t));
18105 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18106 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18107 else
18108 finish_expr_stmt (tmp);
18109 break;
18110
18111 case USING_STMT:
18112 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18113 break;
18114
18115 case DECL_EXPR:
18116 {
18117 tree decl, pattern_decl;
18118 tree init;
18119
18120 pattern_decl = decl = DECL_EXPR_DECL (t);
18121 if (TREE_CODE (decl) == LABEL_DECL)
18122 finish_label_decl (DECL_NAME (decl));
18123 else if (TREE_CODE (decl) == USING_DECL)
18124 {
18125 /* We cannot have a member-using decl here (until 'using
18126 enum T' is a thing). */
18127 gcc_checking_assert (!DECL_DEPENDENT_P (decl));
18128
18129 /* This must be a non-dependent using-decl, and we'll have
18130 used the names it found during template parsing. We do
18131 not want to do the lookup again, because we might not
18132 find the things we found then. (Again, using enum T
18133 might mean we have to do things here.) */
18134 tree scope = USING_DECL_SCOPE (decl);
18135 gcc_checking_assert (scope
18136 == tsubst (scope, args, complain, in_decl));
18137 /* We still need to push the bindings so that we can look up
18138 this name later. */
18139 push_using_decl_bindings (DECL_NAME (decl),
18140 USING_DECL_DECLS (decl));
18141 }
18142 else if (is_capture_proxy (decl)
18143 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18144 {
18145 /* We're in tsubst_lambda_expr, we've already inserted a new
18146 capture proxy, so look it up and register it. */
18147 tree inst;
18148 if (!DECL_PACK_P (decl))
18149 {
18150 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18151 LOOK_want::HIDDEN_LAMBDA);
18152 gcc_assert (inst != decl && is_capture_proxy (inst));
18153 }
18154 else if (is_normal_capture_proxy (decl))
18155 {
18156 inst = (retrieve_local_specialization
18157 (DECL_CAPTURED_VARIABLE (decl)));
18158 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18159 || DECL_PACK_P (inst));
18160 }
18161 else
18162 inst = lookup_init_capture_pack (decl);
18163
18164 register_local_specialization (inst, decl);
18165 break;
18166 }
18167 else if (DECL_PRETTY_FUNCTION_P (decl))
18168 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18169 DECL_NAME (decl),
18170 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18171 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18172 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18173 /* Don't copy the old closure; we'll create a new one in
18174 tsubst_lambda_expr. */
18175 break;
18176 else
18177 {
18178 init = DECL_INITIAL (decl);
18179 decl = tsubst (decl, args, complain, in_decl);
18180 if (decl != error_mark_node)
18181 {
18182 /* By marking the declaration as instantiated, we avoid
18183 trying to instantiate it. Since instantiate_decl can't
18184 handle local variables, and since we've already done
18185 all that needs to be done, that's the right thing to
18186 do. */
18187 if (VAR_P (decl))
18188 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18189 if (VAR_P (decl) && !DECL_NAME (decl)
18190 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18191 /* Anonymous aggregates are a special case. */
18192 finish_anon_union (decl);
18193 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18194 {
18195 DECL_CONTEXT (decl) = current_function_decl;
18196 if (DECL_NAME (decl) == this_identifier)
18197 {
18198 tree lam = DECL_CONTEXT (current_function_decl);
18199 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18200 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18201 }
18202 insert_capture_proxy (decl);
18203 }
18204 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18205 /* We already did a pushtag. */;
18206 else if (VAR_OR_FUNCTION_DECL_P (decl)
18207 && DECL_LOCAL_DECL_P (decl))
18208 {
18209 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18210 DECL_CONTEXT (decl) = NULL_TREE;
18211 decl = pushdecl (decl);
18212 if (TREE_CODE (decl) == FUNCTION_DECL
18213 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18214 && cp_check_omp_declare_reduction (decl))
18215 instantiate_body (pattern_decl, args, decl, true);
18216 }
18217 else
18218 {
18219 bool const_init = false;
18220 unsigned int cnt = 0;
18221 tree first = NULL_TREE, ndecl = error_mark_node;
18222 maybe_push_decl (decl);
18223
18224 if (VAR_P (decl)
18225 && DECL_DECOMPOSITION_P (decl)
18226 && TREE_TYPE (pattern_decl) != error_mark_node)
18227 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18228 complain, in_decl, &first,
18229 &cnt);
18230
18231 init = tsubst_init (init, decl, args, complain, in_decl);
18232
18233 if (VAR_P (decl))
18234 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18235 (pattern_decl));
18236
18237 if (ndecl != error_mark_node)
18238 cp_maybe_mangle_decomp (ndecl, first, cnt);
18239
18240 /* In a non-template function, VLA type declarations are
18241 handled in grokdeclarator; for templates, handle them
18242 now. */
18243 predeclare_vla (decl);
18244
18245 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18246
18247 if (ndecl != error_mark_node)
18248 cp_finish_decomp (ndecl, first, cnt);
18249 }
18250 }
18251 }
18252
18253 break;
18254 }
18255
18256 case FOR_STMT:
18257 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18258 RECUR (FOR_INIT_STMT (t));
18259 finish_init_stmt (stmt);
18260 tmp = RECUR (FOR_COND (t));
18261 finish_for_cond (tmp, stmt, false, 0);
18262 tmp = RECUR (FOR_EXPR (t));
18263 finish_for_expr (tmp, stmt);
18264 {
18265 bool prev = note_iteration_stmt_body_start ();
18266 RECUR (FOR_BODY (t));
18267 note_iteration_stmt_body_end (prev);
18268 }
18269 finish_for_stmt (stmt);
18270 break;
18271
18272 case RANGE_FOR_STMT:
18273 {
18274 /* Construct another range_for, if this is not a final
18275 substitution (for inside a generic lambda of a
18276 template). Otherwise convert to a regular for. */
18277 tree decl, expr;
18278 stmt = (processing_template_decl
18279 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18280 : begin_for_stmt (NULL_TREE, NULL_TREE));
18281 RECUR (RANGE_FOR_INIT_STMT (t));
18282 decl = RANGE_FOR_DECL (t);
18283 decl = tsubst (decl, args, complain, in_decl);
18284 maybe_push_decl (decl);
18285 expr = RECUR (RANGE_FOR_EXPR (t));
18286
18287 tree decomp_first = NULL_TREE;
18288 unsigned decomp_cnt = 0;
18289 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18290 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18291 complain, in_decl,
18292 &decomp_first, &decomp_cnt);
18293
18294 if (processing_template_decl)
18295 {
18296 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18297 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18298 finish_range_for_decl (stmt, decl, expr);
18299 if (decomp_first && decl != error_mark_node)
18300 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18301 }
18302 else
18303 {
18304 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18305 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18306 stmt = cp_convert_range_for (stmt, decl, expr,
18307 decomp_first, decomp_cnt,
18308 RANGE_FOR_IVDEP (t), unroll);
18309 }
18310
18311 bool prev = note_iteration_stmt_body_start ();
18312 RECUR (RANGE_FOR_BODY (t));
18313 note_iteration_stmt_body_end (prev);
18314 finish_for_stmt (stmt);
18315 }
18316 break;
18317
18318 case WHILE_STMT:
18319 stmt = begin_while_stmt ();
18320 tmp = RECUR (WHILE_COND (t));
18321 finish_while_stmt_cond (tmp, stmt, false, 0);
18322 {
18323 bool prev = note_iteration_stmt_body_start ();
18324 RECUR (WHILE_BODY (t));
18325 note_iteration_stmt_body_end (prev);
18326 }
18327 finish_while_stmt (stmt);
18328 break;
18329
18330 case DO_STMT:
18331 stmt = begin_do_stmt ();
18332 {
18333 bool prev = note_iteration_stmt_body_start ();
18334 RECUR (DO_BODY (t));
18335 note_iteration_stmt_body_end (prev);
18336 }
18337 finish_do_body (stmt);
18338 tmp = RECUR (DO_COND (t));
18339 finish_do_stmt (tmp, stmt, false, 0);
18340 break;
18341
18342 case IF_STMT:
18343 stmt = begin_if_stmt ();
18344 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18345 if (IF_STMT_CONSTEXPR_P (t))
18346 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18347 tmp = RECUR (IF_COND (t));
18348 tmp = finish_if_stmt_cond (tmp, stmt);
18349 if (IF_STMT_CONSTEXPR_P (t)
18350 && instantiation_dependent_expression_p (tmp))
18351 {
18352 /* We're partially instantiating a generic lambda, but the condition
18353 of the constexpr if is still dependent. Don't substitute into the
18354 branches now, just remember the template arguments. */
18355 do_poplevel (IF_SCOPE (stmt));
18356 IF_COND (stmt) = IF_COND (t);
18357 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18358 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18359 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18360 add_stmt (stmt);
18361 break;
18362 }
18363 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18364 /* Don't instantiate the THEN_CLAUSE. */;
18365 else
18366 {
18367 tree folded = fold_non_dependent_expr (tmp, complain);
18368 bool inhibit = integer_zerop (folded);
18369 if (inhibit)
18370 ++c_inhibit_evaluation_warnings;
18371 RECUR (THEN_CLAUSE (t));
18372 if (inhibit)
18373 --c_inhibit_evaluation_warnings;
18374 }
18375 finish_then_clause (stmt);
18376
18377 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18378 /* Don't instantiate the ELSE_CLAUSE. */;
18379 else if (ELSE_CLAUSE (t))
18380 {
18381 tree folded = fold_non_dependent_expr (tmp, complain);
18382 bool inhibit = integer_nonzerop (folded);
18383 begin_else_clause (stmt);
18384 if (inhibit)
18385 ++c_inhibit_evaluation_warnings;
18386 RECUR (ELSE_CLAUSE (t));
18387 if (inhibit)
18388 --c_inhibit_evaluation_warnings;
18389 finish_else_clause (stmt);
18390 }
18391
18392 finish_if_stmt (stmt);
18393 break;
18394
18395 case BIND_EXPR:
18396 if (BIND_EXPR_BODY_BLOCK (t))
18397 stmt = begin_function_body ();
18398 else
18399 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18400 ? BCS_TRY_BLOCK : 0);
18401
18402 RECUR (BIND_EXPR_BODY (t));
18403
18404 if (BIND_EXPR_BODY_BLOCK (t))
18405 finish_function_body (stmt);
18406 else
18407 finish_compound_stmt (stmt);
18408 break;
18409
18410 case BREAK_STMT:
18411 finish_break_stmt ();
18412 break;
18413
18414 case CONTINUE_STMT:
18415 finish_continue_stmt ();
18416 break;
18417
18418 case SWITCH_STMT:
18419 stmt = begin_switch_stmt ();
18420 tmp = RECUR (SWITCH_STMT_COND (t));
18421 finish_switch_cond (tmp, stmt);
18422 RECUR (SWITCH_STMT_BODY (t));
18423 finish_switch_stmt (stmt);
18424 break;
18425
18426 case CASE_LABEL_EXPR:
18427 {
18428 tree decl = CASE_LABEL (t);
18429 tree low = RECUR (CASE_LOW (t));
18430 tree high = RECUR (CASE_HIGH (t));
18431 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18432 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18433 {
18434 tree label = CASE_LABEL (l);
18435 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18436 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18437 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18438 }
18439 }
18440 break;
18441
18442 case LABEL_EXPR:
18443 {
18444 tree decl = LABEL_EXPR_LABEL (t);
18445 tree label;
18446
18447 label = finish_label_stmt (DECL_NAME (decl));
18448 if (TREE_CODE (label) == LABEL_DECL)
18449 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18450 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18451 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18452 }
18453 break;
18454
18455 case GOTO_EXPR:
18456 tmp = GOTO_DESTINATION (t);
18457 if (TREE_CODE (tmp) != LABEL_DECL)
18458 /* Computed goto's must be tsubst'd into. On the other hand,
18459 non-computed gotos must not be; the identifier in question
18460 will have no binding. */
18461 tmp = RECUR (tmp);
18462 else
18463 tmp = DECL_NAME (tmp);
18464 finish_goto_stmt (tmp);
18465 break;
18466
18467 case ASM_EXPR:
18468 {
18469 tree string = RECUR (ASM_STRING (t));
18470 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18471 complain, in_decl);
18472 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18473 complain, in_decl);
18474 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18475 complain, in_decl);
18476 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18477 complain, in_decl);
18478 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18479 outputs, inputs, clobbers, labels,
18480 ASM_INLINE_P (t));
18481 tree asm_expr = tmp;
18482 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18483 asm_expr = TREE_OPERAND (asm_expr, 0);
18484 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18485 }
18486 break;
18487
18488 case TRY_BLOCK:
18489 if (CLEANUP_P (t))
18490 {
18491 stmt = begin_try_block ();
18492 RECUR (TRY_STMTS (t));
18493 finish_cleanup_try_block (stmt);
18494 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18495 }
18496 else
18497 {
18498 tree compound_stmt = NULL_TREE;
18499
18500 if (FN_TRY_BLOCK_P (t))
18501 stmt = begin_function_try_block (&compound_stmt);
18502 else
18503 stmt = begin_try_block ();
18504
18505 RECUR (TRY_STMTS (t));
18506
18507 if (FN_TRY_BLOCK_P (t))
18508 finish_function_try_block (stmt);
18509 else
18510 finish_try_block (stmt);
18511
18512 RECUR (TRY_HANDLERS (t));
18513 if (FN_TRY_BLOCK_P (t))
18514 finish_function_handler_sequence (stmt, compound_stmt);
18515 else
18516 finish_handler_sequence (stmt);
18517 }
18518 break;
18519
18520 case HANDLER:
18521 {
18522 tree decl = HANDLER_PARMS (t);
18523
18524 if (decl)
18525 {
18526 decl = tsubst (decl, args, complain, in_decl);
18527 /* Prevent instantiate_decl from trying to instantiate
18528 this variable. We've already done all that needs to be
18529 done. */
18530 if (decl != error_mark_node)
18531 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18532 }
18533 stmt = begin_handler ();
18534 finish_handler_parms (decl, stmt);
18535 RECUR (HANDLER_BODY (t));
18536 finish_handler (stmt);
18537 }
18538 break;
18539
18540 case TAG_DEFN:
18541 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18542 if (CLASS_TYPE_P (tmp))
18543 {
18544 /* Local classes are not independent templates; they are
18545 instantiated along with their containing function. And this
18546 way we don't have to deal with pushing out of one local class
18547 to instantiate a member of another local class. */
18548 /* Closures are handled by the LAMBDA_EXPR. */
18549 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18550 complete_type (tmp);
18551 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18552 if ((VAR_P (fld)
18553 || (TREE_CODE (fld) == FUNCTION_DECL
18554 && !DECL_ARTIFICIAL (fld)))
18555 && DECL_TEMPLATE_INSTANTIATION (fld))
18556 instantiate_decl (fld, /*defer_ok=*/false,
18557 /*expl_inst_class=*/false);
18558 }
18559 break;
18560
18561 case STATIC_ASSERT:
18562 {
18563 tree condition;
18564
18565 ++c_inhibit_evaluation_warnings;
18566 condition =
18567 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18568 args,
18569 complain, in_decl,
18570 /*integral_constant_expression_p=*/true);
18571 --c_inhibit_evaluation_warnings;
18572
18573 finish_static_assert (condition,
18574 STATIC_ASSERT_MESSAGE (t),
18575 STATIC_ASSERT_SOURCE_LOCATION (t),
18576 /*member_p=*/false, /*show_expr_p=*/true);
18577 }
18578 break;
18579
18580 case OACC_KERNELS:
18581 case OACC_PARALLEL:
18582 case OACC_SERIAL:
18583 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18584 in_decl);
18585 stmt = begin_omp_parallel ();
18586 RECUR (OMP_BODY (t));
18587 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18588 break;
18589
18590 case OMP_PARALLEL:
18591 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18592 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18593 complain, in_decl);
18594 if (OMP_PARALLEL_COMBINED (t))
18595 omp_parallel_combined_clauses = &tmp;
18596 stmt = begin_omp_parallel ();
18597 RECUR (OMP_PARALLEL_BODY (t));
18598 gcc_assert (omp_parallel_combined_clauses == NULL);
18599 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18600 = OMP_PARALLEL_COMBINED (t);
18601 pop_omp_privatization_clauses (r);
18602 break;
18603
18604 case OMP_TASK:
18605 if (OMP_TASK_BODY (t) == NULL_TREE)
18606 {
18607 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18608 complain, in_decl);
18609 t = copy_node (t);
18610 OMP_TASK_CLAUSES (t) = tmp;
18611 add_stmt (t);
18612 break;
18613 }
18614 r = push_omp_privatization_clauses (false);
18615 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18616 complain, in_decl);
18617 stmt = begin_omp_task ();
18618 RECUR (OMP_TASK_BODY (t));
18619 finish_omp_task (tmp, stmt);
18620 pop_omp_privatization_clauses (r);
18621 break;
18622
18623 case OMP_FOR:
18624 case OMP_LOOP:
18625 case OMP_SIMD:
18626 case OMP_DISTRIBUTE:
18627 case OMP_TASKLOOP:
18628 case OACC_LOOP:
18629 {
18630 tree clauses, body, pre_body;
18631 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18632 tree orig_declv = NULL_TREE;
18633 tree incrv = NULL_TREE;
18634 enum c_omp_region_type ort = C_ORT_OMP;
18635 bool any_range_for = false;
18636 int i;
18637
18638 if (TREE_CODE (t) == OACC_LOOP)
18639 ort = C_ORT_ACC;
18640
18641 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18642 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18643 in_decl);
18644 if (OMP_FOR_INIT (t) != NULL_TREE)
18645 {
18646 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18647 if (OMP_FOR_ORIG_DECLS (t))
18648 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18649 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18650 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18651 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18652 }
18653
18654 keep_next_level (true);
18655 stmt = begin_omp_structured_block ();
18656
18657 pre_body = push_stmt_list ();
18658 RECUR (OMP_FOR_PRE_BODY (t));
18659 pre_body = pop_stmt_list (pre_body);
18660
18661 if (OMP_FOR_INIT (t) != NULL_TREE)
18662 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18663 any_range_for
18664 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18665 condv, incrv, &clauses, args,
18666 complain, in_decl,
18667 integral_constant_expression_p);
18668 omp_parallel_combined_clauses = NULL;
18669
18670 if (any_range_for)
18671 {
18672 gcc_assert (orig_declv);
18673 body = begin_omp_structured_block ();
18674 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18675 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18676 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18677 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18678 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18679 TREE_VEC_ELT (declv, i));
18680 }
18681 else
18682 body = push_stmt_list ();
18683 RECUR (OMP_FOR_BODY (t));
18684 if (any_range_for)
18685 body = finish_omp_structured_block (body);
18686 else
18687 body = pop_stmt_list (body);
18688
18689 if (OMP_FOR_INIT (t) != NULL_TREE)
18690 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18691 orig_declv, initv, condv, incrv, body, pre_body,
18692 NULL, clauses);
18693 else
18694 {
18695 t = make_node (TREE_CODE (t));
18696 TREE_TYPE (t) = void_type_node;
18697 OMP_FOR_BODY (t) = body;
18698 OMP_FOR_PRE_BODY (t) = pre_body;
18699 OMP_FOR_CLAUSES (t) = clauses;
18700 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18701 add_stmt (t);
18702 }
18703
18704 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18705 t));
18706 pop_omp_privatization_clauses (r);
18707 }
18708 break;
18709
18710 case OMP_SECTIONS:
18711 omp_parallel_combined_clauses = NULL;
18712 /* FALLTHRU */
18713 case OMP_SINGLE:
18714 case OMP_TEAMS:
18715 case OMP_CRITICAL:
18716 case OMP_TASKGROUP:
18717 case OMP_SCAN:
18718 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18719 && OMP_TEAMS_COMBINED (t));
18720 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18721 in_decl);
18722 if (TREE_CODE (t) == OMP_TEAMS)
18723 {
18724 keep_next_level (true);
18725 stmt = begin_omp_structured_block ();
18726 RECUR (OMP_BODY (t));
18727 stmt = finish_omp_structured_block (stmt);
18728 }
18729 else
18730 {
18731 stmt = push_stmt_list ();
18732 RECUR (OMP_BODY (t));
18733 stmt = pop_stmt_list (stmt);
18734 }
18735
18736 if (TREE_CODE (t) == OMP_CRITICAL
18737 && tmp != NULL_TREE
18738 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18739 {
18740 error_at (OMP_CLAUSE_LOCATION (tmp),
18741 "%<#pragma omp critical%> with %<hint%> clause requires "
18742 "a name, except when %<omp_sync_hint_none%> is used");
18743 RETURN (error_mark_node);
18744 }
18745 t = copy_node (t);
18746 OMP_BODY (t) = stmt;
18747 OMP_CLAUSES (t) = tmp;
18748 add_stmt (t);
18749 pop_omp_privatization_clauses (r);
18750 break;
18751
18752 case OMP_DEPOBJ:
18753 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18754 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18755 {
18756 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18757 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18758 {
18759 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18760 args, complain, in_decl);
18761 if (tmp == NULL_TREE)
18762 tmp = error_mark_node;
18763 }
18764 else
18765 {
18766 kind = (enum omp_clause_depend_kind)
18767 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18768 tmp = NULL_TREE;
18769 }
18770 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18771 }
18772 else
18773 finish_omp_depobj (EXPR_LOCATION (t), r,
18774 OMP_CLAUSE_DEPEND_SOURCE,
18775 OMP_DEPOBJ_CLAUSES (t));
18776 break;
18777
18778 case OACC_DATA:
18779 case OMP_TARGET_DATA:
18780 case OMP_TARGET:
18781 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18782 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18783 in_decl);
18784 keep_next_level (true);
18785 stmt = begin_omp_structured_block ();
18786
18787 RECUR (OMP_BODY (t));
18788 stmt = finish_omp_structured_block (stmt);
18789
18790 t = copy_node (t);
18791 OMP_BODY (t) = stmt;
18792 OMP_CLAUSES (t) = tmp;
18793 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18794 {
18795 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18796 if (teams)
18797 {
18798 /* For combined target teams, ensure the num_teams and
18799 thread_limit clause expressions are evaluated on the host,
18800 before entering the target construct. */
18801 tree c;
18802 for (c = OMP_TEAMS_CLAUSES (teams);
18803 c; c = OMP_CLAUSE_CHAIN (c))
18804 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18805 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18806 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18807 {
18808 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18809 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18810 if (expr == error_mark_node)
18811 continue;
18812 tmp = TARGET_EXPR_SLOT (expr);
18813 add_stmt (expr);
18814 OMP_CLAUSE_OPERAND (c, 0) = expr;
18815 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18816 OMP_CLAUSE_FIRSTPRIVATE);
18817 OMP_CLAUSE_DECL (tc) = tmp;
18818 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18819 OMP_TARGET_CLAUSES (t) = tc;
18820 }
18821 }
18822 }
18823 add_stmt (t);
18824 break;
18825
18826 case OACC_DECLARE:
18827 t = copy_node (t);
18828 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18829 complain, in_decl);
18830 OACC_DECLARE_CLAUSES (t) = tmp;
18831 add_stmt (t);
18832 break;
18833
18834 case OMP_TARGET_UPDATE:
18835 case OMP_TARGET_ENTER_DATA:
18836 case OMP_TARGET_EXIT_DATA:
18837 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18838 complain, in_decl);
18839 t = copy_node (t);
18840 OMP_STANDALONE_CLAUSES (t) = tmp;
18841 add_stmt (t);
18842 break;
18843
18844 case OACC_CACHE:
18845 case OACC_ENTER_DATA:
18846 case OACC_EXIT_DATA:
18847 case OACC_UPDATE:
18848 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18849 complain, in_decl);
18850 t = copy_node (t);
18851 OMP_STANDALONE_CLAUSES (t) = tmp;
18852 add_stmt (t);
18853 break;
18854
18855 case OMP_ORDERED:
18856 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18857 complain, in_decl);
18858 stmt = push_stmt_list ();
18859 RECUR (OMP_BODY (t));
18860 stmt = pop_stmt_list (stmt);
18861
18862 t = copy_node (t);
18863 OMP_BODY (t) = stmt;
18864 OMP_ORDERED_CLAUSES (t) = tmp;
18865 add_stmt (t);
18866 break;
18867
18868 case OMP_MASTER:
18869 omp_parallel_combined_clauses = NULL;
18870 /* FALLTHRU */
18871 case OMP_SECTION:
18872 stmt = push_stmt_list ();
18873 RECUR (OMP_BODY (t));
18874 stmt = pop_stmt_list (stmt);
18875
18876 t = copy_node (t);
18877 OMP_BODY (t) = stmt;
18878 add_stmt (t);
18879 break;
18880
18881 case OMP_ATOMIC:
18882 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18883 tmp = NULL_TREE;
18884 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18885 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18886 complain, in_decl);
18887 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18888 {
18889 tree op1 = TREE_OPERAND (t, 1);
18890 tree rhs1 = NULL_TREE;
18891 tree lhs, rhs;
18892 if (TREE_CODE (op1) == COMPOUND_EXPR)
18893 {
18894 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18895 op1 = TREE_OPERAND (op1, 1);
18896 }
18897 lhs = RECUR (TREE_OPERAND (op1, 0));
18898 rhs = RECUR (TREE_OPERAND (op1, 1));
18899 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18900 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18901 OMP_ATOMIC_MEMORY_ORDER (t));
18902 }
18903 else
18904 {
18905 tree op1 = TREE_OPERAND (t, 1);
18906 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18907 tree rhs1 = NULL_TREE;
18908 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18909 enum tree_code opcode = NOP_EXPR;
18910 if (code == OMP_ATOMIC_READ)
18911 {
18912 v = RECUR (TREE_OPERAND (op1, 0));
18913 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18914 }
18915 else if (code == OMP_ATOMIC_CAPTURE_OLD
18916 || code == OMP_ATOMIC_CAPTURE_NEW)
18917 {
18918 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18919 v = RECUR (TREE_OPERAND (op1, 0));
18920 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18921 if (TREE_CODE (op11) == COMPOUND_EXPR)
18922 {
18923 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18924 op11 = TREE_OPERAND (op11, 1);
18925 }
18926 lhs = RECUR (TREE_OPERAND (op11, 0));
18927 rhs = RECUR (TREE_OPERAND (op11, 1));
18928 opcode = TREE_CODE (op11);
18929 if (opcode == MODIFY_EXPR)
18930 opcode = NOP_EXPR;
18931 }
18932 else
18933 {
18934 code = OMP_ATOMIC;
18935 lhs = RECUR (TREE_OPERAND (op1, 0));
18936 rhs = RECUR (TREE_OPERAND (op1, 1));
18937 }
18938 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18939 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18940 }
18941 break;
18942
18943 case TRANSACTION_EXPR:
18944 {
18945 int flags = 0;
18946 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18947 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18948
18949 if (TRANSACTION_EXPR_IS_STMT (t))
18950 {
18951 tree body = TRANSACTION_EXPR_BODY (t);
18952 tree noex = NULL_TREE;
18953 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18954 {
18955 noex = MUST_NOT_THROW_COND (body);
18956 if (noex == NULL_TREE)
18957 noex = boolean_true_node;
18958 body = TREE_OPERAND (body, 0);
18959 }
18960 stmt = begin_transaction_stmt (input_location, NULL, flags);
18961 RECUR (body);
18962 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18963 }
18964 else
18965 {
18966 stmt = build_transaction_expr (EXPR_LOCATION (t),
18967 RECUR (TRANSACTION_EXPR_BODY (t)),
18968 flags, NULL_TREE);
18969 RETURN (stmt);
18970 }
18971 }
18972 break;
18973
18974 case MUST_NOT_THROW_EXPR:
18975 {
18976 tree op0 = RECUR (TREE_OPERAND (t, 0));
18977 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18978 RETURN (build_must_not_throw_expr (op0, cond));
18979 }
18980
18981 case EXPR_PACK_EXPANSION:
18982 error ("invalid use of pack expansion expression");
18983 RETURN (error_mark_node);
18984
18985 case NONTYPE_ARGUMENT_PACK:
18986 error ("use %<...%> to expand argument pack");
18987 RETURN (error_mark_node);
18988
18989 case COMPOUND_EXPR:
18990 tmp = RECUR (TREE_OPERAND (t, 0));
18991 if (tmp == NULL_TREE)
18992 /* If the first operand was a statement, we're done with it. */
18993 RETURN (RECUR (TREE_OPERAND (t, 1)));
18994 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18995 RECUR (TREE_OPERAND (t, 1)),
18996 complain));
18997
18998 case ANNOTATE_EXPR:
18999 tmp = RECUR (TREE_OPERAND (t, 0));
19000 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19001 TREE_TYPE (tmp), tmp,
19002 RECUR (TREE_OPERAND (t, 1)),
19003 RECUR (TREE_OPERAND (t, 2))));
19004
19005 case PREDICT_EXPR:
19006 RETURN (add_stmt (copy_node (t)));
19007
19008 default:
19009 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19010
19011 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19012 /*function_p=*/false,
19013 integral_constant_expression_p));
19014 }
19015
19016 RETURN (NULL_TREE);
19017 out:
19018 input_location = loc;
19019 return r;
19020 #undef RECUR
19021 #undef RETURN
19022 }
19023
19024 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19025 function. For description of the body see comment above
19026 cp_parser_omp_declare_reduction_exprs. */
19027
19028 static void
19029 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19030 {
19031 if (t == NULL_TREE || t == error_mark_node)
19032 return;
19033
19034 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19035
19036 tree_stmt_iterator tsi;
19037 int i;
19038 tree stmts[7];
19039 memset (stmts, 0, sizeof stmts);
19040 for (i = 0, tsi = tsi_start (t);
19041 i < 7 && !tsi_end_p (tsi);
19042 i++, tsi_next (&tsi))
19043 stmts[i] = tsi_stmt (tsi);
19044 gcc_assert (tsi_end_p (tsi));
19045
19046 if (i >= 3)
19047 {
19048 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19049 && TREE_CODE (stmts[1]) == DECL_EXPR);
19050 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19051 args, complain, in_decl);
19052 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19053 args, complain, in_decl);
19054 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19055 expect to be pushing it. */
19056 DECL_CONTEXT (omp_out) = current_function_decl;
19057 DECL_CONTEXT (omp_in) = current_function_decl;
19058 keep_next_level (true);
19059 tree block = begin_omp_structured_block ();
19060 tsubst_expr (stmts[2], args, complain, in_decl, false);
19061 block = finish_omp_structured_block (block);
19062 block = maybe_cleanup_point_expr_void (block);
19063 add_decl_expr (omp_out);
19064 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
19065 TREE_NO_WARNING (omp_out) = 1;
19066 add_decl_expr (omp_in);
19067 finish_expr_stmt (block);
19068 }
19069 if (i >= 6)
19070 {
19071 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19072 && TREE_CODE (stmts[4]) == DECL_EXPR);
19073 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19074 args, complain, in_decl);
19075 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19076 args, complain, in_decl);
19077 DECL_CONTEXT (omp_priv) = current_function_decl;
19078 DECL_CONTEXT (omp_orig) = current_function_decl;
19079 keep_next_level (true);
19080 tree block = begin_omp_structured_block ();
19081 tsubst_expr (stmts[5], args, complain, in_decl, false);
19082 block = finish_omp_structured_block (block);
19083 block = maybe_cleanup_point_expr_void (block);
19084 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19085 add_decl_expr (omp_priv);
19086 add_decl_expr (omp_orig);
19087 finish_expr_stmt (block);
19088 if (i == 7)
19089 add_decl_expr (omp_orig);
19090 }
19091 }
19092
19093 /* T is a postfix-expression that is not being used in a function
19094 call. Return the substituted version of T. */
19095
19096 static tree
19097 tsubst_non_call_postfix_expression (tree t, tree args,
19098 tsubst_flags_t complain,
19099 tree in_decl)
19100 {
19101 if (TREE_CODE (t) == SCOPE_REF)
19102 t = tsubst_qualified_id (t, args, complain, in_decl,
19103 /*done=*/false, /*address_p=*/false);
19104 else
19105 t = tsubst_copy_and_build (t, args, complain, in_decl,
19106 /*function_p=*/false,
19107 /*integral_constant_expression_p=*/false);
19108
19109 return t;
19110 }
19111
19112 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19113 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19114 dependent init-capture. */
19115
19116 static void
19117 prepend_one_capture (tree field, tree init, tree &list,
19118 tsubst_flags_t complain)
19119 {
19120 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19121 {
19122 tree type = NULL_TREE;
19123 if (!init)
19124 {
19125 if (complain & tf_error)
19126 error ("empty initializer in lambda init-capture");
19127 init = error_mark_node;
19128 }
19129 else if (TREE_CODE (init) == TREE_LIST)
19130 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19131 if (!type)
19132 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19133 TREE_TYPE (field) = type;
19134 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19135 }
19136 list = tree_cons (field, init, list);
19137 }
19138
19139 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19140 instantiation context. Instantiating a pack expansion containing a lambda
19141 might result in multiple lambdas all based on the same lambda in the
19142 template. */
19143
19144 tree
19145 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19146 {
19147 tree oldfn = lambda_function (t);
19148 in_decl = oldfn;
19149
19150 tree r = build_lambda_expr ();
19151
19152 LAMBDA_EXPR_LOCATION (r)
19153 = LAMBDA_EXPR_LOCATION (t);
19154 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19155 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19156 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19157 LAMBDA_EXPR_INSTANTIATED (r) = true;
19158
19159 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19160 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19161
19162 vec<tree,va_gc>* field_packs = NULL;
19163
19164 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19165 cap = TREE_CHAIN (cap))
19166 {
19167 tree ofield = TREE_PURPOSE (cap);
19168 tree init = TREE_VALUE (cap);
19169 if (PACK_EXPANSION_P (init))
19170 init = tsubst_pack_expansion (init, args, complain, in_decl);
19171 else
19172 init = tsubst_copy_and_build (init, args, complain, in_decl,
19173 /*fn*/false, /*constexpr*/false);
19174
19175 if (init == error_mark_node)
19176 return error_mark_node;
19177
19178 if (init && TREE_CODE (init) == TREE_LIST)
19179 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19180
19181 if (!processing_template_decl
19182 && init && TREE_CODE (init) != TREE_VEC
19183 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19184 {
19185 /* For a VLA, simply tsubsting the field type won't work, we need to
19186 go through add_capture again. XXX do we want to do this for all
19187 captures? */
19188 tree name = (get_identifier
19189 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19190 tree ftype = TREE_TYPE (ofield);
19191 bool by_ref = (TYPE_REF_P (ftype)
19192 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19193 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19194 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19195 continue;
19196 }
19197
19198 if (PACK_EXPANSION_P (ofield))
19199 ofield = PACK_EXPANSION_PATTERN (ofield);
19200 tree field = tsubst_decl (ofield, args, complain);
19201
19202 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19203 {
19204 /* Remember these for when we've pushed local_specializations. */
19205 vec_safe_push (field_packs, ofield);
19206 vec_safe_push (field_packs, field);
19207 }
19208
19209 if (field == error_mark_node)
19210 return error_mark_node;
19211
19212 if (TREE_CODE (field) == TREE_VEC)
19213 {
19214 int len = TREE_VEC_LENGTH (field);
19215 gcc_assert (TREE_CODE (init) == TREE_VEC
19216 && TREE_VEC_LENGTH (init) == len);
19217 for (int i = 0; i < len; ++i)
19218 prepend_one_capture (TREE_VEC_ELT (field, i),
19219 TREE_VEC_ELT (init, i),
19220 LAMBDA_EXPR_CAPTURE_LIST (r),
19221 complain);
19222 }
19223 else
19224 {
19225 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19226 complain);
19227
19228 if (id_equal (DECL_NAME (field), "__this"))
19229 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19230 }
19231 }
19232
19233 tree type = begin_lambda_type (r);
19234 if (type == error_mark_node)
19235 return error_mark_node;
19236
19237 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19238 /* A lambda in a default argument outside a class gets no
19239 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19240 tsubst_default_argument calls start_lambda_scope, so we need to
19241 specifically ignore it here, and use the global scope. */
19242 record_null_lambda_scope (r);
19243 else
19244 record_lambda_scope (r);
19245
19246 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19247 determine_visibility (TYPE_NAME (type));
19248
19249 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19250
19251 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19252 ? DECL_TI_TEMPLATE (oldfn)
19253 : NULL_TREE);
19254
19255 tree fntype = static_fn_type (oldfn);
19256 if (oldtmpl)
19257 ++processing_template_decl;
19258 fntype = tsubst (fntype, args, complain, in_decl);
19259 if (oldtmpl)
19260 --processing_template_decl;
19261
19262 if (fntype == error_mark_node)
19263 r = error_mark_node;
19264 else
19265 {
19266 /* The body of a lambda-expression is not a subexpression of the
19267 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19268 which would be skipped if cp_unevaluated_operand. */
19269 cp_evaluated ev;
19270
19271 /* Fix the type of 'this'. */
19272 fntype = build_memfn_type (fntype, type,
19273 type_memfn_quals (fntype),
19274 type_memfn_rqual (fntype));
19275 tree fn, tmpl;
19276 if (oldtmpl)
19277 {
19278 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19279 if (tmpl == error_mark_node)
19280 {
19281 r = error_mark_node;
19282 goto out;
19283 }
19284 fn = DECL_TEMPLATE_RESULT (tmpl);
19285 finish_member_declaration (tmpl);
19286 }
19287 else
19288 {
19289 tmpl = NULL_TREE;
19290 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19291 if (fn == error_mark_node)
19292 {
19293 r = error_mark_node;
19294 goto out;
19295 }
19296 finish_member_declaration (fn);
19297 }
19298
19299 if (tree ci = get_constraints (oldfn))
19300 {
19301 /* Substitute into the lambda's constraints. */
19302 if (oldtmpl)
19303 ++processing_template_decl;
19304 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19305 if (oldtmpl)
19306 --processing_template_decl;
19307 set_constraints (fn, ci);
19308 }
19309
19310 /* Let finish_function set this. */
19311 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19312
19313 bool nested = cfun;
19314 if (nested)
19315 push_function_context ();
19316 else
19317 /* Still increment function_depth so that we don't GC in the
19318 middle of an expression. */
19319 ++function_depth;
19320
19321 local_specialization_stack s (lss_copy);
19322
19323 tree body = start_lambda_function (fn, r);
19324
19325 /* Now record them for lookup_init_capture_pack. */
19326 int fplen = vec_safe_length (field_packs);
19327 for (int i = 0; i < fplen; )
19328 {
19329 tree pack = (*field_packs)[i++];
19330 tree inst = (*field_packs)[i++];
19331 register_local_specialization (inst, pack);
19332 }
19333 release_tree_vector (field_packs);
19334
19335 register_parameter_specializations (oldfn, fn);
19336
19337 if (oldtmpl)
19338 {
19339 /* We might not partially instantiate some parts of the function, so
19340 copy these flags from the original template. */
19341 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19342 current_function_returns_value = ol->returns_value;
19343 current_function_returns_null = ol->returns_null;
19344 current_function_returns_abnormally = ol->returns_abnormally;
19345 current_function_infinite_loop = ol->infinite_loop;
19346 }
19347
19348 /* [temp.deduct] A lambda-expression appearing in a function type or a
19349 template parameter is not considered part of the immediate context for
19350 the purposes of template argument deduction. */
19351 complain = tf_warning_or_error;
19352
19353 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19354 /*constexpr*/false);
19355
19356 finish_lambda_function (body);
19357
19358 if (nested)
19359 pop_function_context ();
19360 else
19361 --function_depth;
19362
19363 /* The capture list was built up in reverse order; fix that now. */
19364 LAMBDA_EXPR_CAPTURE_LIST (r)
19365 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19366
19367 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19368
19369 maybe_add_lambda_conv_op (type);
19370 }
19371
19372 out:
19373 finish_struct (type, /*attr*/NULL_TREE);
19374
19375 insert_pending_capture_proxies ();
19376
19377 return r;
19378 }
19379
19380 /* Like tsubst but deals with expressions and performs semantic
19381 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19382 "F<TARGS> (ARGS)". */
19383
19384 tree
19385 tsubst_copy_and_build (tree t,
19386 tree args,
19387 tsubst_flags_t complain,
19388 tree in_decl,
19389 bool function_p,
19390 bool integral_constant_expression_p)
19391 {
19392 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19393 #define RECUR(NODE) \
19394 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19395 /*function_p=*/false, \
19396 integral_constant_expression_p)
19397
19398 tree retval, op1;
19399 location_t save_loc;
19400
19401 if (t == NULL_TREE || t == error_mark_node)
19402 return t;
19403
19404 save_loc = input_location;
19405 if (location_t eloc = cp_expr_location (t))
19406 input_location = eloc;
19407
19408 /* N3276 decltype magic only applies to calls at the top level or on the
19409 right side of a comma. */
19410 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19411 complain &= ~tf_decltype;
19412
19413 switch (TREE_CODE (t))
19414 {
19415 case USING_DECL:
19416 t = DECL_NAME (t);
19417 /* Fall through. */
19418 case IDENTIFIER_NODE:
19419 {
19420 tree decl;
19421 cp_id_kind idk;
19422 bool non_integral_constant_expression_p;
19423 const char *error_msg;
19424
19425 if (IDENTIFIER_CONV_OP_P (t))
19426 {
19427 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19428 t = make_conv_op_name (new_type);
19429 }
19430
19431 /* Look up the name. */
19432 decl = lookup_name (t);
19433
19434 /* By convention, expressions use ERROR_MARK_NODE to indicate
19435 failure, not NULL_TREE. */
19436 if (decl == NULL_TREE)
19437 decl = error_mark_node;
19438
19439 decl = finish_id_expression (t, decl, NULL_TREE,
19440 &idk,
19441 integral_constant_expression_p,
19442 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19443 &non_integral_constant_expression_p,
19444 /*template_p=*/false,
19445 /*done=*/true,
19446 /*address_p=*/false,
19447 /*template_arg_p=*/false,
19448 &error_msg,
19449 input_location);
19450 if (error_msg)
19451 error (error_msg);
19452 if (!function_p && identifier_p (decl))
19453 {
19454 if (complain & tf_error)
19455 unqualified_name_lookup_error (decl);
19456 decl = error_mark_node;
19457 }
19458 RETURN (decl);
19459 }
19460
19461 case TEMPLATE_ID_EXPR:
19462 {
19463 tree object;
19464 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19465 complain, in_decl,
19466 function_p,
19467 integral_constant_expression_p);
19468 tree targs = TREE_OPERAND (t, 1);
19469
19470 if (targs)
19471 targs = tsubst_template_args (targs, args, complain, in_decl);
19472 if (targs == error_mark_node)
19473 RETURN (error_mark_node);
19474
19475 if (TREE_CODE (templ) == SCOPE_REF)
19476 {
19477 tree name = TREE_OPERAND (templ, 1);
19478 tree tid = lookup_template_function (name, targs);
19479 TREE_OPERAND (templ, 1) = tid;
19480 RETURN (templ);
19481 }
19482
19483 if (concept_definition_p (templ))
19484 {
19485 tree check = build_concept_check (templ, targs, complain);
19486 if (check == error_mark_node)
19487 RETURN (error_mark_node);
19488
19489 tree id = unpack_concept_check (check);
19490
19491 /* If we built a function concept check, return the underlying
19492 template-id. So we can evaluate it as a function call. */
19493 if (function_concept_p (TREE_OPERAND (id, 0)))
19494 RETURN (id);
19495
19496 RETURN (check);
19497 }
19498
19499 if (variable_template_p (templ))
19500 {
19501 tree r = lookup_and_finish_template_variable (templ, targs,
19502 complain);
19503 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19504 RETURN (r);
19505 }
19506
19507 if (TREE_CODE (templ) == COMPONENT_REF)
19508 {
19509 object = TREE_OPERAND (templ, 0);
19510 templ = TREE_OPERAND (templ, 1);
19511 }
19512 else
19513 object = NULL_TREE;
19514
19515 tree tid = lookup_template_function (templ, targs);
19516
19517 if (object)
19518 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19519 object, tid, NULL_TREE));
19520 else if (identifier_p (templ))
19521 {
19522 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19523 name lookup found nothing when parsing the template name. */
19524 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19525 RETURN (tid);
19526 }
19527 else
19528 RETURN (baselink_for_fns (tid));
19529 }
19530
19531 case INDIRECT_REF:
19532 {
19533 tree r = RECUR (TREE_OPERAND (t, 0));
19534
19535 if (REFERENCE_REF_P (t))
19536 {
19537 /* A type conversion to reference type will be enclosed in
19538 such an indirect ref, but the substitution of the cast
19539 will have also added such an indirect ref. */
19540 r = convert_from_reference (r);
19541 }
19542 else
19543 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19544 complain|decltype_flag);
19545
19546 if (REF_PARENTHESIZED_P (t))
19547 r = force_paren_expr (r);
19548
19549 RETURN (r);
19550 }
19551
19552 case NOP_EXPR:
19553 {
19554 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19555 tree op0 = RECUR (TREE_OPERAND (t, 0));
19556 RETURN (build_nop (type, op0));
19557 }
19558
19559 case IMPLICIT_CONV_EXPR:
19560 {
19561 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19562 tree expr = RECUR (TREE_OPERAND (t, 0));
19563 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19564 {
19565 retval = copy_node (t);
19566 TREE_TYPE (retval) = type;
19567 TREE_OPERAND (retval, 0) = expr;
19568 RETURN (retval);
19569 }
19570 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19571 /* We'll pass this to convert_nontype_argument again, we don't need
19572 to actually perform any conversion here. */
19573 RETURN (expr);
19574 int flags = LOOKUP_IMPLICIT;
19575 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19576 flags = LOOKUP_NORMAL;
19577 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19578 flags |= LOOKUP_NO_NARROWING;
19579 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19580 flags));
19581 }
19582
19583 case CONVERT_EXPR:
19584 {
19585 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19586 tree op0 = RECUR (TREE_OPERAND (t, 0));
19587 if (op0 == error_mark_node)
19588 RETURN (error_mark_node);
19589 RETURN (build1 (CONVERT_EXPR, type, op0));
19590 }
19591
19592 case CAST_EXPR:
19593 case REINTERPRET_CAST_EXPR:
19594 case CONST_CAST_EXPR:
19595 case DYNAMIC_CAST_EXPR:
19596 case STATIC_CAST_EXPR:
19597 {
19598 tree type;
19599 tree op, r = NULL_TREE;
19600
19601 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19602 if (integral_constant_expression_p
19603 && !cast_valid_in_integral_constant_expression_p (type))
19604 {
19605 if (complain & tf_error)
19606 error ("a cast to a type other than an integral or "
19607 "enumeration type cannot appear in a constant-expression");
19608 RETURN (error_mark_node);
19609 }
19610
19611 op = RECUR (TREE_OPERAND (t, 0));
19612
19613 warning_sentinel s(warn_useless_cast);
19614 warning_sentinel s2(warn_ignored_qualifiers);
19615 switch (TREE_CODE (t))
19616 {
19617 case CAST_EXPR:
19618 r = build_functional_cast (input_location, type, op, complain);
19619 break;
19620 case REINTERPRET_CAST_EXPR:
19621 r = build_reinterpret_cast (input_location, type, op, complain);
19622 break;
19623 case CONST_CAST_EXPR:
19624 r = build_const_cast (input_location, type, op, complain);
19625 break;
19626 case DYNAMIC_CAST_EXPR:
19627 r = build_dynamic_cast (input_location, type, op, complain);
19628 break;
19629 case STATIC_CAST_EXPR:
19630 r = build_static_cast (input_location, type, op, complain);
19631 if (IMPLICIT_RVALUE_P (t))
19632 set_implicit_rvalue_p (r);
19633 break;
19634 default:
19635 gcc_unreachable ();
19636 }
19637
19638 RETURN (r);
19639 }
19640
19641 case BIT_CAST_EXPR:
19642 {
19643 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19644 tree op0 = RECUR (TREE_OPERAND (t, 0));
19645 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
19646 }
19647
19648 case POSTDECREMENT_EXPR:
19649 case POSTINCREMENT_EXPR:
19650 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19651 args, complain, in_decl);
19652 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19653 complain|decltype_flag));
19654
19655 case PREDECREMENT_EXPR:
19656 case PREINCREMENT_EXPR:
19657 case NEGATE_EXPR:
19658 case BIT_NOT_EXPR:
19659 case ABS_EXPR:
19660 case TRUTH_NOT_EXPR:
19661 case UNARY_PLUS_EXPR: /* Unary + */
19662 case REALPART_EXPR:
19663 case IMAGPART_EXPR:
19664 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19665 RECUR (TREE_OPERAND (t, 0)),
19666 complain|decltype_flag));
19667
19668 case FIX_TRUNC_EXPR:
19669 gcc_unreachable ();
19670
19671 case ADDR_EXPR:
19672 op1 = TREE_OPERAND (t, 0);
19673 if (TREE_CODE (op1) == LABEL_DECL)
19674 RETURN (finish_label_address_expr (DECL_NAME (op1),
19675 EXPR_LOCATION (op1)));
19676 if (TREE_CODE (op1) == SCOPE_REF)
19677 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19678 /*done=*/true, /*address_p=*/true);
19679 else
19680 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19681 in_decl);
19682 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19683 complain|decltype_flag));
19684
19685 case PLUS_EXPR:
19686 case MINUS_EXPR:
19687 case MULT_EXPR:
19688 case TRUNC_DIV_EXPR:
19689 case CEIL_DIV_EXPR:
19690 case FLOOR_DIV_EXPR:
19691 case ROUND_DIV_EXPR:
19692 case EXACT_DIV_EXPR:
19693 case BIT_AND_EXPR:
19694 case BIT_IOR_EXPR:
19695 case BIT_XOR_EXPR:
19696 case TRUNC_MOD_EXPR:
19697 case FLOOR_MOD_EXPR:
19698 case TRUTH_ANDIF_EXPR:
19699 case TRUTH_ORIF_EXPR:
19700 case TRUTH_AND_EXPR:
19701 case TRUTH_OR_EXPR:
19702 case RSHIFT_EXPR:
19703 case LSHIFT_EXPR:
19704 case EQ_EXPR:
19705 case NE_EXPR:
19706 case MAX_EXPR:
19707 case MIN_EXPR:
19708 case LE_EXPR:
19709 case GE_EXPR:
19710 case LT_EXPR:
19711 case GT_EXPR:
19712 case SPACESHIP_EXPR:
19713 case MEMBER_REF:
19714 case DOTSTAR_EXPR:
19715 {
19716 /* If T was type-dependent, suppress warnings that depend on the range
19717 of the types involved. */
19718 ++processing_template_decl;
19719 const bool was_dep = (potential_constant_expression (t)
19720 ? value_dependent_expression_p (t)
19721 : type_dependent_expression_p (t));
19722 --processing_template_decl;
19723 tree op0 = RECUR (TREE_OPERAND (t, 0));
19724 tree op1 = RECUR (TREE_OPERAND (t, 1));
19725
19726 warning_sentinel s1(warn_type_limits, was_dep);
19727 warning_sentinel s2(warn_div_by_zero, was_dep);
19728 warning_sentinel s3(warn_logical_op, was_dep);
19729 warning_sentinel s4(warn_tautological_compare, was_dep);
19730
19731 tree r = build_x_binary_op
19732 (input_location, TREE_CODE (t),
19733 op0,
19734 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19735 ? ERROR_MARK
19736 : TREE_CODE (TREE_OPERAND (t, 0))),
19737 op1,
19738 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19739 ? ERROR_MARK
19740 : TREE_CODE (TREE_OPERAND (t, 1))),
19741 /*overload=*/NULL,
19742 complain|decltype_flag);
19743 if (EXPR_P (r) && TREE_NO_WARNING (t))
19744 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19745
19746 RETURN (r);
19747 }
19748
19749 case POINTER_PLUS_EXPR:
19750 {
19751 tree op0 = RECUR (TREE_OPERAND (t, 0));
19752 if (op0 == error_mark_node)
19753 RETURN (error_mark_node);
19754 tree op1 = RECUR (TREE_OPERAND (t, 1));
19755 if (op1 == error_mark_node)
19756 RETURN (error_mark_node);
19757 RETURN (fold_build_pointer_plus (op0, op1));
19758 }
19759
19760 case SCOPE_REF:
19761 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19762 /*address_p=*/false));
19763 case ARRAY_REF:
19764 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19765 args, complain, in_decl);
19766 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19767 RECUR (TREE_OPERAND (t, 1)),
19768 complain|decltype_flag));
19769
19770 case SIZEOF_EXPR:
19771 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19772 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19773 RETURN (tsubst_copy (t, args, complain, in_decl));
19774 /* Fall through */
19775
19776 case ALIGNOF_EXPR:
19777 {
19778 tree r;
19779
19780 op1 = TREE_OPERAND (t, 0);
19781 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19782 op1 = TREE_TYPE (op1);
19783 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19784 && ALIGNOF_EXPR_STD_P (t));
19785 if (!args)
19786 {
19787 /* When there are no ARGS, we are trying to evaluate a
19788 non-dependent expression from the parser. Trying to do
19789 the substitutions may not work. */
19790 if (!TYPE_P (op1))
19791 op1 = TREE_TYPE (op1);
19792 }
19793 else
19794 {
19795 ++cp_unevaluated_operand;
19796 ++c_inhibit_evaluation_warnings;
19797 if (TYPE_P (op1))
19798 op1 = tsubst (op1, args, complain, in_decl);
19799 else
19800 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19801 /*function_p=*/false,
19802 /*integral_constant_expression_p=*/
19803 false);
19804 --cp_unevaluated_operand;
19805 --c_inhibit_evaluation_warnings;
19806 }
19807 if (TYPE_P (op1))
19808 r = cxx_sizeof_or_alignof_type (input_location,
19809 op1, TREE_CODE (t), std_alignof,
19810 complain & tf_error);
19811 else
19812 r = cxx_sizeof_or_alignof_expr (input_location,
19813 op1, TREE_CODE (t), std_alignof,
19814 complain & tf_error);
19815 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19816 {
19817 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19818 {
19819 if (!processing_template_decl && TYPE_P (op1))
19820 {
19821 r = build_min (SIZEOF_EXPR, size_type_node,
19822 build1 (NOP_EXPR, op1, error_mark_node));
19823 SIZEOF_EXPR_TYPE_P (r) = 1;
19824 }
19825 else
19826 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19827 TREE_SIDE_EFFECTS (r) = 0;
19828 TREE_READONLY (r) = 1;
19829 }
19830 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19831 }
19832 RETURN (r);
19833 }
19834
19835 case AT_ENCODE_EXPR:
19836 {
19837 op1 = TREE_OPERAND (t, 0);
19838 ++cp_unevaluated_operand;
19839 ++c_inhibit_evaluation_warnings;
19840 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19841 /*function_p=*/false,
19842 /*integral_constant_expression_p=*/false);
19843 --cp_unevaluated_operand;
19844 --c_inhibit_evaluation_warnings;
19845 RETURN (objc_build_encode_expr (op1));
19846 }
19847
19848 case NOEXCEPT_EXPR:
19849 op1 = TREE_OPERAND (t, 0);
19850 ++cp_unevaluated_operand;
19851 ++c_inhibit_evaluation_warnings;
19852 ++cp_noexcept_operand;
19853 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19854 /*function_p=*/false,
19855 /*integral_constant_expression_p=*/false);
19856 --cp_unevaluated_operand;
19857 --c_inhibit_evaluation_warnings;
19858 --cp_noexcept_operand;
19859 RETURN (finish_noexcept_expr (op1, complain));
19860
19861 case MODOP_EXPR:
19862 {
19863 warning_sentinel s(warn_div_by_zero);
19864 tree lhs = RECUR (TREE_OPERAND (t, 0));
19865 tree rhs = RECUR (TREE_OPERAND (t, 2));
19866 tree r = build_x_modify_expr
19867 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19868 complain|decltype_flag);
19869 /* TREE_NO_WARNING must be set if either the expression was
19870 parenthesized or it uses an operator such as >>= rather
19871 than plain assignment. In the former case, it was already
19872 set and must be copied. In the latter case,
19873 build_x_modify_expr sets it and it must not be reset
19874 here. */
19875 if (TREE_NO_WARNING (t))
19876 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19877
19878 RETURN (r);
19879 }
19880
19881 case ARROW_EXPR:
19882 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19883 args, complain, in_decl);
19884 /* Remember that there was a reference to this entity. */
19885 if (DECL_P (op1)
19886 && !mark_used (op1, complain) && !(complain & tf_error))
19887 RETURN (error_mark_node);
19888 RETURN (build_x_arrow (input_location, op1, complain));
19889
19890 case NEW_EXPR:
19891 {
19892 tree placement = RECUR (TREE_OPERAND (t, 0));
19893 tree init = RECUR (TREE_OPERAND (t, 3));
19894 vec<tree, va_gc> *placement_vec;
19895 vec<tree, va_gc> *init_vec;
19896 tree ret;
19897 location_t loc = EXPR_LOCATION (t);
19898
19899 if (placement == NULL_TREE)
19900 placement_vec = NULL;
19901 else if (placement == error_mark_node)
19902 RETURN (error_mark_node);
19903 else
19904 {
19905 placement_vec = make_tree_vector ();
19906 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19907 vec_safe_push (placement_vec, TREE_VALUE (placement));
19908 }
19909
19910 /* If there was an initializer in the original tree, but it
19911 instantiated to an empty list, then we should pass a
19912 non-NULL empty vector to tell build_new that it was an
19913 empty initializer() rather than no initializer. This can
19914 only happen when the initializer is a pack expansion whose
19915 parameter packs are of length zero. */
19916 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19917 init_vec = NULL;
19918 else if (init == error_mark_node)
19919 RETURN (error_mark_node);
19920 else
19921 {
19922 init_vec = make_tree_vector ();
19923 if (init == void_node)
19924 gcc_assert (init_vec != NULL);
19925 else
19926 {
19927 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19928 vec_safe_push (init_vec, TREE_VALUE (init));
19929 }
19930 }
19931
19932 /* Avoid passing an enclosing decl to valid_array_size_p. */
19933 in_decl = NULL_TREE;
19934
19935 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19936 tree op2 = RECUR (TREE_OPERAND (t, 2));
19937 ret = build_new (loc, &placement_vec, op1, op2,
19938 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19939 complain);
19940
19941 if (placement_vec != NULL)
19942 release_tree_vector (placement_vec);
19943 if (init_vec != NULL)
19944 release_tree_vector (init_vec);
19945
19946 RETURN (ret);
19947 }
19948
19949 case DELETE_EXPR:
19950 {
19951 tree op0 = RECUR (TREE_OPERAND (t, 0));
19952 tree op1 = RECUR (TREE_OPERAND (t, 1));
19953 RETURN (delete_sanity (input_location, op0, op1,
19954 DELETE_EXPR_USE_VEC (t),
19955 DELETE_EXPR_USE_GLOBAL (t),
19956 complain));
19957 }
19958
19959 case COMPOUND_EXPR:
19960 {
19961 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19962 complain & ~tf_decltype, in_decl,
19963 /*function_p=*/false,
19964 integral_constant_expression_p);
19965 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19966 op0,
19967 RECUR (TREE_OPERAND (t, 1)),
19968 complain|decltype_flag));
19969 }
19970
19971 case CALL_EXPR:
19972 {
19973 tree function;
19974 unsigned int nargs, i;
19975 bool qualified_p;
19976 bool koenig_p;
19977 tree ret;
19978
19979 function = CALL_EXPR_FN (t);
19980 /* Internal function with no arguments. */
19981 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19982 RETURN (t);
19983
19984 /* When we parsed the expression, we determined whether or
19985 not Koenig lookup should be performed. */
19986 koenig_p = KOENIG_LOOKUP_P (t);
19987 if (function == NULL_TREE)
19988 {
19989 koenig_p = false;
19990 qualified_p = false;
19991 }
19992 else if (TREE_CODE (function) == SCOPE_REF)
19993 {
19994 qualified_p = true;
19995 function = tsubst_qualified_id (function, args, complain, in_decl,
19996 /*done=*/false,
19997 /*address_p=*/false);
19998 }
19999 else if (koenig_p && identifier_p (function))
20000 {
20001 /* Do nothing; calling tsubst_copy_and_build on an identifier
20002 would incorrectly perform unqualified lookup again.
20003
20004 Note that we can also have an IDENTIFIER_NODE if the earlier
20005 unqualified lookup found a member function; in that case
20006 koenig_p will be false and we do want to do the lookup
20007 again to find the instantiated member function.
20008
20009 FIXME but doing that causes c++/15272, so we need to stop
20010 using IDENTIFIER_NODE in that situation. */
20011 qualified_p = false;
20012 }
20013 else
20014 {
20015 if (TREE_CODE (function) == COMPONENT_REF)
20016 {
20017 tree op = TREE_OPERAND (function, 1);
20018
20019 qualified_p = (TREE_CODE (op) == SCOPE_REF
20020 || (BASELINK_P (op)
20021 && BASELINK_QUALIFIED_P (op)));
20022 }
20023 else
20024 qualified_p = false;
20025
20026 if (TREE_CODE (function) == ADDR_EXPR
20027 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20028 /* Avoid error about taking the address of a constructor. */
20029 function = TREE_OPERAND (function, 0);
20030
20031 function = tsubst_copy_and_build (function, args, complain,
20032 in_decl,
20033 !qualified_p,
20034 integral_constant_expression_p);
20035
20036 if (BASELINK_P (function))
20037 qualified_p = true;
20038 }
20039
20040 nargs = call_expr_nargs (t);
20041 releasing_vec call_args;
20042 for (i = 0; i < nargs; ++i)
20043 {
20044 tree arg = CALL_EXPR_ARG (t, i);
20045
20046 if (!PACK_EXPANSION_P (arg))
20047 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
20048 else
20049 {
20050 /* Expand the pack expansion and push each entry onto
20051 CALL_ARGS. */
20052 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20053 if (TREE_CODE (arg) == TREE_VEC)
20054 {
20055 unsigned int len, j;
20056
20057 len = TREE_VEC_LENGTH (arg);
20058 for (j = 0; j < len; ++j)
20059 {
20060 tree value = TREE_VEC_ELT (arg, j);
20061 if (value != NULL_TREE)
20062 value = convert_from_reference (value);
20063 vec_safe_push (call_args, value);
20064 }
20065 }
20066 else
20067 {
20068 /* A partial substitution. Add one entry. */
20069 vec_safe_push (call_args, arg);
20070 }
20071 }
20072 }
20073
20074 /* Stripped-down processing for a call in a thunk. Specifically, in
20075 the thunk template for a generic lambda. */
20076 if (call_from_lambda_thunk_p (t))
20077 {
20078 /* Now that we've expanded any packs, the number of call args
20079 might be different. */
20080 unsigned int cargs = call_args->length ();
20081 tree thisarg = NULL_TREE;
20082 if (TREE_CODE (function) == COMPONENT_REF)
20083 {
20084 thisarg = TREE_OPERAND (function, 0);
20085 if (TREE_CODE (thisarg) == INDIRECT_REF)
20086 thisarg = TREE_OPERAND (thisarg, 0);
20087 function = TREE_OPERAND (function, 1);
20088 if (TREE_CODE (function) == BASELINK)
20089 function = BASELINK_FUNCTIONS (function);
20090 }
20091 /* We aren't going to do normal overload resolution, so force the
20092 template-id to resolve. */
20093 function = resolve_nondeduced_context (function, complain);
20094 for (unsigned i = 0; i < cargs; ++i)
20095 {
20096 /* In a thunk, pass through args directly, without any
20097 conversions. */
20098 tree arg = (*call_args)[i];
20099 while (TREE_CODE (arg) != PARM_DECL)
20100 arg = TREE_OPERAND (arg, 0);
20101 (*call_args)[i] = arg;
20102 }
20103 if (thisarg)
20104 {
20105 /* If there are no other args, just push 'this'. */
20106 if (cargs == 0)
20107 vec_safe_push (call_args, thisarg);
20108 else
20109 {
20110 /* Otherwise, shift the other args over to make room. */
20111 tree last = (*call_args)[cargs - 1];
20112 vec_safe_push (call_args, last);
20113 for (int i = cargs - 1; i > 0; --i)
20114 (*call_args)[i] = (*call_args)[i - 1];
20115 (*call_args)[0] = thisarg;
20116 }
20117 }
20118 ret = build_call_a (function, call_args->length (),
20119 call_args->address ());
20120 /* The thunk location is not interesting. */
20121 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20122 CALL_FROM_THUNK_P (ret) = true;
20123 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20124 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20125
20126 RETURN (ret);
20127 }
20128
20129 /* We do not perform argument-dependent lookup if normal
20130 lookup finds a non-function, in accordance with the
20131 resolution of DR 218. */
20132 if (koenig_p
20133 && ((is_overloaded_fn (function)
20134 /* If lookup found a member function, the Koenig lookup is
20135 not appropriate, even if an unqualified-name was used
20136 to denote the function. */
20137 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20138 || identifier_p (function)
20139 /* C++20 P0846: Lookup found nothing. */
20140 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20141 && identifier_p (TREE_OPERAND (function, 0))))
20142 /* Only do this when substitution turns a dependent call
20143 into a non-dependent call. */
20144 && type_dependent_expression_p_push (t)
20145 && !any_type_dependent_arguments_p (call_args))
20146 function = perform_koenig_lookup (function, call_args, tf_none);
20147
20148 if (function != NULL_TREE
20149 && (identifier_p (function)
20150 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20151 && identifier_p (TREE_OPERAND (function, 0))))
20152 && !any_type_dependent_arguments_p (call_args))
20153 {
20154 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20155 function = TREE_OPERAND (function, 0);
20156 if (koenig_p && (complain & tf_warning_or_error))
20157 {
20158 /* For backwards compatibility and good diagnostics, try
20159 the unqualified lookup again if we aren't in SFINAE
20160 context. */
20161 tree unq = (tsubst_copy_and_build
20162 (function, args, complain, in_decl, true,
20163 integral_constant_expression_p));
20164 if (unq == error_mark_node)
20165 RETURN (error_mark_node);
20166
20167 if (unq != function)
20168 {
20169 /* In a lambda fn, we have to be careful to not
20170 introduce new this captures. Legacy code can't
20171 be using lambdas anyway, so it's ok to be
20172 stricter. */
20173 bool in_lambda = (current_class_type
20174 && LAMBDA_TYPE_P (current_class_type));
20175 char const *const msg
20176 = G_("%qD was not declared in this scope, "
20177 "and no declarations were found by "
20178 "argument-dependent lookup at the point "
20179 "of instantiation");
20180
20181 bool diag = true;
20182 if (in_lambda)
20183 error_at (cp_expr_loc_or_input_loc (t),
20184 msg, function);
20185 else
20186 diag = permerror (cp_expr_loc_or_input_loc (t),
20187 msg, function);
20188 if (diag)
20189 {
20190 tree fn = unq;
20191
20192 if (INDIRECT_REF_P (fn))
20193 fn = TREE_OPERAND (fn, 0);
20194 if (is_overloaded_fn (fn))
20195 fn = get_first_fn (fn);
20196
20197 if (!DECL_P (fn))
20198 /* Can't say anything more. */;
20199 else if (DECL_CLASS_SCOPE_P (fn))
20200 {
20201 location_t loc = cp_expr_loc_or_input_loc (t);
20202 inform (loc,
20203 "declarations in dependent base %qT are "
20204 "not found by unqualified lookup",
20205 DECL_CLASS_CONTEXT (fn));
20206 if (current_class_ptr)
20207 inform (loc,
20208 "use %<this->%D%> instead", function);
20209 else
20210 inform (loc,
20211 "use %<%T::%D%> instead",
20212 current_class_name, function);
20213 }
20214 else
20215 inform (DECL_SOURCE_LOCATION (fn),
20216 "%qD declared here, later in the "
20217 "translation unit", fn);
20218 if (in_lambda)
20219 RETURN (error_mark_node);
20220 }
20221
20222 function = unq;
20223 }
20224 }
20225 if (identifier_p (function))
20226 {
20227 if (complain & tf_error)
20228 unqualified_name_lookup_error (function);
20229 RETURN (error_mark_node);
20230 }
20231 }
20232
20233 /* Remember that there was a reference to this entity. */
20234 if (function != NULL_TREE
20235 && DECL_P (function)
20236 && !mark_used (function, complain) && !(complain & tf_error))
20237 RETURN (error_mark_node);
20238
20239 /* Put back tf_decltype for the actual call. */
20240 complain |= decltype_flag;
20241
20242 if (function == NULL_TREE)
20243 switch (CALL_EXPR_IFN (t))
20244 {
20245 case IFN_LAUNDER:
20246 gcc_assert (nargs == 1);
20247 if (vec_safe_length (call_args) != 1)
20248 {
20249 error_at (cp_expr_loc_or_input_loc (t),
20250 "wrong number of arguments to "
20251 "%<__builtin_launder%>");
20252 ret = error_mark_node;
20253 }
20254 else
20255 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20256 (*call_args)[0], complain);
20257 break;
20258
20259 case IFN_VEC_CONVERT:
20260 gcc_assert (nargs == 1);
20261 if (vec_safe_length (call_args) != 1)
20262 {
20263 error_at (cp_expr_loc_or_input_loc (t),
20264 "wrong number of arguments to "
20265 "%<__builtin_convertvector%>");
20266 ret = error_mark_node;
20267 break;
20268 }
20269 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20270 tsubst (TREE_TYPE (t), args,
20271 complain, in_decl),
20272 complain);
20273 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20274 RETURN (ret);
20275 break;
20276
20277 default:
20278 /* Unsupported internal function with arguments. */
20279 gcc_unreachable ();
20280 }
20281 else if (TREE_CODE (function) == OFFSET_REF
20282 || TREE_CODE (function) == DOTSTAR_EXPR
20283 || TREE_CODE (function) == MEMBER_REF)
20284 ret = build_offset_ref_call_from_tree (function, &call_args,
20285 complain);
20286 else if (TREE_CODE (function) == COMPONENT_REF)
20287 {
20288 tree instance = TREE_OPERAND (function, 0);
20289 tree fn = TREE_OPERAND (function, 1);
20290
20291 if (processing_template_decl
20292 && (type_dependent_expression_p (instance)
20293 || (!BASELINK_P (fn)
20294 && TREE_CODE (fn) != FIELD_DECL)
20295 || type_dependent_expression_p (fn)
20296 || any_type_dependent_arguments_p (call_args)))
20297 ret = build_min_nt_call_vec (function, call_args);
20298 else if (!BASELINK_P (fn))
20299 ret = finish_call_expr (function, &call_args,
20300 /*disallow_virtual=*/false,
20301 /*koenig_p=*/false,
20302 complain);
20303 else
20304 ret = (build_new_method_call
20305 (instance, fn,
20306 &call_args, NULL_TREE,
20307 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20308 /*fn_p=*/NULL,
20309 complain));
20310 }
20311 else if (concept_check_p (function))
20312 {
20313 /* FUNCTION is a template-id referring to a concept definition. */
20314 tree id = unpack_concept_check (function);
20315 tree tmpl = TREE_OPERAND (id, 0);
20316 tree args = TREE_OPERAND (id, 1);
20317
20318 /* Calls to standard and variable concepts should have been
20319 previously diagnosed. */
20320 gcc_assert (function_concept_p (tmpl));
20321
20322 /* Ensure the result is wrapped as a call expression. */
20323 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20324 }
20325 else
20326 ret = finish_call_expr (function, &call_args,
20327 /*disallow_virtual=*/qualified_p,
20328 koenig_p,
20329 complain);
20330
20331 if (ret != error_mark_node)
20332 {
20333 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20334 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20335 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20336 if (op || ord || rev)
20337 {
20338 function = extract_call_expr (ret);
20339 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20340 CALL_EXPR_ORDERED_ARGS (function) = ord;
20341 CALL_EXPR_REVERSE_ARGS (function) = rev;
20342 }
20343 }
20344
20345 RETURN (ret);
20346 }
20347
20348 case COND_EXPR:
20349 {
20350 tree cond = RECUR (TREE_OPERAND (t, 0));
20351 cond = mark_rvalue_use (cond);
20352 tree folded_cond = fold_non_dependent_expr (cond, complain);
20353 tree exp1, exp2;
20354
20355 if (TREE_CODE (folded_cond) == INTEGER_CST)
20356 {
20357 if (integer_zerop (folded_cond))
20358 {
20359 ++c_inhibit_evaluation_warnings;
20360 exp1 = RECUR (TREE_OPERAND (t, 1));
20361 --c_inhibit_evaluation_warnings;
20362 exp2 = RECUR (TREE_OPERAND (t, 2));
20363 }
20364 else
20365 {
20366 exp1 = RECUR (TREE_OPERAND (t, 1));
20367 ++c_inhibit_evaluation_warnings;
20368 exp2 = RECUR (TREE_OPERAND (t, 2));
20369 --c_inhibit_evaluation_warnings;
20370 }
20371 cond = folded_cond;
20372 }
20373 else
20374 {
20375 exp1 = RECUR (TREE_OPERAND (t, 1));
20376 exp2 = RECUR (TREE_OPERAND (t, 2));
20377 }
20378
20379 warning_sentinel s(warn_duplicated_branches);
20380 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20381 cond, exp1, exp2, complain));
20382 }
20383
20384 case PSEUDO_DTOR_EXPR:
20385 {
20386 tree op0 = RECUR (TREE_OPERAND (t, 0));
20387 tree op1 = RECUR (TREE_OPERAND (t, 1));
20388 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20389 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20390 input_location));
20391 }
20392
20393 case TREE_LIST:
20394 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20395
20396 case COMPONENT_REF:
20397 {
20398 tree object;
20399 tree object_type;
20400 tree member;
20401 tree r;
20402
20403 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20404 args, complain, in_decl);
20405 /* Remember that there was a reference to this entity. */
20406 if (DECL_P (object)
20407 && !mark_used (object, complain) && !(complain & tf_error))
20408 RETURN (error_mark_node);
20409 object_type = TREE_TYPE (object);
20410
20411 member = TREE_OPERAND (t, 1);
20412 if (BASELINK_P (member))
20413 member = tsubst_baselink (member,
20414 non_reference (TREE_TYPE (object)),
20415 args, complain, in_decl);
20416 else
20417 member = tsubst_copy (member, args, complain, in_decl);
20418 if (member == error_mark_node)
20419 RETURN (error_mark_node);
20420
20421 if (TREE_CODE (member) == FIELD_DECL)
20422 {
20423 r = finish_non_static_data_member (member, object, NULL_TREE);
20424 if (TREE_CODE (r) == COMPONENT_REF)
20425 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20426 RETURN (r);
20427 }
20428 else if (type_dependent_expression_p (object))
20429 /* We can't do much here. */;
20430 else if (!CLASS_TYPE_P (object_type))
20431 {
20432 if (scalarish_type_p (object_type))
20433 {
20434 tree s = NULL_TREE;
20435 tree dtor = member;
20436
20437 if (TREE_CODE (dtor) == SCOPE_REF)
20438 {
20439 s = TREE_OPERAND (dtor, 0);
20440 dtor = TREE_OPERAND (dtor, 1);
20441 }
20442 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20443 {
20444 dtor = TREE_OPERAND (dtor, 0);
20445 if (TYPE_P (dtor))
20446 RETURN (finish_pseudo_destructor_expr
20447 (object, s, dtor, input_location));
20448 }
20449 }
20450 }
20451 else if (TREE_CODE (member) == SCOPE_REF
20452 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20453 {
20454 /* Lookup the template functions now that we know what the
20455 scope is. */
20456 tree scope = TREE_OPERAND (member, 0);
20457 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20458 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20459 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20460 /*complain=*/false);
20461 if (BASELINK_P (member))
20462 {
20463 BASELINK_FUNCTIONS (member)
20464 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20465 args);
20466 member = (adjust_result_of_qualified_name_lookup
20467 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20468 object_type));
20469 }
20470 else
20471 {
20472 qualified_name_lookup_error (scope, tmpl, member,
20473 input_location);
20474 RETURN (error_mark_node);
20475 }
20476 }
20477 else if (TREE_CODE (member) == SCOPE_REF
20478 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20479 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20480 {
20481 if (complain & tf_error)
20482 {
20483 if (TYPE_P (TREE_OPERAND (member, 0)))
20484 error ("%qT is not a class or namespace",
20485 TREE_OPERAND (member, 0));
20486 else
20487 error ("%qD is not a class or namespace",
20488 TREE_OPERAND (member, 0));
20489 }
20490 RETURN (error_mark_node);
20491 }
20492
20493 r = finish_class_member_access_expr (object, member,
20494 /*template_p=*/false,
20495 complain);
20496 if (TREE_CODE (r) == COMPONENT_REF)
20497 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20498 RETURN (r);
20499 }
20500
20501 case THROW_EXPR:
20502 RETURN (build_throw
20503 (input_location, RECUR (TREE_OPERAND (t, 0))));
20504
20505 case CONSTRUCTOR:
20506 {
20507 vec<constructor_elt, va_gc> *n;
20508 constructor_elt *ce;
20509 unsigned HOST_WIDE_INT idx;
20510 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20511 bool process_index_p;
20512 int newlen;
20513 bool need_copy_p = false;
20514 tree r;
20515
20516 if (type == error_mark_node)
20517 RETURN (error_mark_node);
20518
20519 /* We do not want to process the index of aggregate
20520 initializers as they are identifier nodes which will be
20521 looked up by digest_init. */
20522 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20523
20524 if (null_member_pointer_value_p (t))
20525 {
20526 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20527 RETURN (t);
20528 }
20529
20530 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20531 newlen = vec_safe_length (n);
20532 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20533 {
20534 if (ce->index && process_index_p
20535 /* An identifier index is looked up in the type
20536 being initialized, not the current scope. */
20537 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20538 ce->index = RECUR (ce->index);
20539
20540 if (PACK_EXPANSION_P (ce->value))
20541 {
20542 /* Substitute into the pack expansion. */
20543 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20544 in_decl);
20545
20546 if (ce->value == error_mark_node
20547 || PACK_EXPANSION_P (ce->value))
20548 ;
20549 else if (TREE_VEC_LENGTH (ce->value) == 1)
20550 /* Just move the argument into place. */
20551 ce->value = TREE_VEC_ELT (ce->value, 0);
20552 else
20553 {
20554 /* Update the length of the final CONSTRUCTOR
20555 arguments vector, and note that we will need to
20556 copy.*/
20557 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20558 need_copy_p = true;
20559 }
20560 }
20561 else
20562 ce->value = RECUR (ce->value);
20563 }
20564
20565 if (need_copy_p)
20566 {
20567 vec<constructor_elt, va_gc> *old_n = n;
20568
20569 vec_alloc (n, newlen);
20570 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20571 {
20572 if (TREE_CODE (ce->value) == TREE_VEC)
20573 {
20574 int i, len = TREE_VEC_LENGTH (ce->value);
20575 for (i = 0; i < len; ++i)
20576 CONSTRUCTOR_APPEND_ELT (n, 0,
20577 TREE_VEC_ELT (ce->value, i));
20578 }
20579 else
20580 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20581 }
20582 }
20583
20584 r = build_constructor (init_list_type_node, n);
20585 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20586 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20587 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20588
20589 if (TREE_HAS_CONSTRUCTOR (t))
20590 {
20591 fcl_t cl = fcl_functional;
20592 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20593 cl = fcl_c99;
20594 RETURN (finish_compound_literal (type, r, complain, cl));
20595 }
20596
20597 TREE_TYPE (r) = type;
20598 RETURN (r);
20599 }
20600
20601 case TYPEID_EXPR:
20602 {
20603 tree operand_0 = TREE_OPERAND (t, 0);
20604 if (TYPE_P (operand_0))
20605 {
20606 operand_0 = tsubst (operand_0, args, complain, in_decl);
20607 RETURN (get_typeid (operand_0, complain));
20608 }
20609 else
20610 {
20611 operand_0 = RECUR (operand_0);
20612 RETURN (build_typeid (operand_0, complain));
20613 }
20614 }
20615
20616 case VAR_DECL:
20617 if (!args)
20618 RETURN (t);
20619 /* Fall through */
20620
20621 case PARM_DECL:
20622 {
20623 tree r = tsubst_copy (t, args, complain, in_decl);
20624 /* ??? We're doing a subset of finish_id_expression here. */
20625 if (tree wrap = maybe_get_tls_wrapper_call (r))
20626 /* Replace an evaluated use of the thread_local variable with
20627 a call to its wrapper. */
20628 r = wrap;
20629 else if (outer_automatic_var_p (r))
20630 r = process_outer_var_ref (r, complain);
20631
20632 if (!TYPE_REF_P (TREE_TYPE (t)))
20633 /* If the original type was a reference, we'll be wrapped in
20634 the appropriate INDIRECT_REF. */
20635 r = convert_from_reference (r);
20636 RETURN (r);
20637 }
20638
20639 case VA_ARG_EXPR:
20640 {
20641 tree op0 = RECUR (TREE_OPERAND (t, 0));
20642 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20643 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20644 }
20645
20646 case OFFSETOF_EXPR:
20647 {
20648 tree object_ptr
20649 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20650 in_decl, /*function_p=*/false,
20651 /*integral_constant_expression_p=*/false);
20652 RETURN (finish_offsetof (object_ptr,
20653 RECUR (TREE_OPERAND (t, 0)),
20654 EXPR_LOCATION (t)));
20655 }
20656
20657 case ADDRESSOF_EXPR:
20658 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20659 RECUR (TREE_OPERAND (t, 0)), complain));
20660
20661 case TRAIT_EXPR:
20662 {
20663 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20664 complain, in_decl);
20665 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20666 complain, in_decl);
20667 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20668 TRAIT_EXPR_KIND (t), type1, type2));
20669 }
20670
20671 case STMT_EXPR:
20672 {
20673 tree old_stmt_expr = cur_stmt_expr;
20674 tree stmt_expr = begin_stmt_expr ();
20675
20676 cur_stmt_expr = stmt_expr;
20677 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20678 integral_constant_expression_p);
20679 stmt_expr = finish_stmt_expr (stmt_expr, false);
20680 cur_stmt_expr = old_stmt_expr;
20681
20682 /* If the resulting list of expression statement is empty,
20683 fold it further into void_node. */
20684 if (empty_expr_stmt_p (stmt_expr))
20685 stmt_expr = void_node;
20686
20687 RETURN (stmt_expr);
20688 }
20689
20690 case LAMBDA_EXPR:
20691 {
20692 if (complain & tf_partial)
20693 {
20694 /* We don't have a full set of template arguments yet; don't touch
20695 the lambda at all. */
20696 gcc_assert (processing_template_decl);
20697 return t;
20698 }
20699 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20700
20701 RETURN (build_lambda_object (r));
20702 }
20703
20704 case TARGET_EXPR:
20705 /* We can get here for a constant initializer of non-dependent type.
20706 FIXME stop folding in cp_parser_initializer_clause. */
20707 {
20708 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20709 complain);
20710 RETURN (r);
20711 }
20712
20713 case TRANSACTION_EXPR:
20714 RETURN (tsubst_expr(t, args, complain, in_decl,
20715 integral_constant_expression_p));
20716
20717 case PAREN_EXPR:
20718 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20719
20720 case VEC_PERM_EXPR:
20721 {
20722 tree op0 = RECUR (TREE_OPERAND (t, 0));
20723 tree op1 = RECUR (TREE_OPERAND (t, 1));
20724 tree op2 = RECUR (TREE_OPERAND (t, 2));
20725 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20726 complain));
20727 }
20728
20729 case REQUIRES_EXPR:
20730 {
20731 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20732 RETURN (r);
20733 }
20734
20735 case RANGE_EXPR:
20736 /* No need to substitute further, a RANGE_EXPR will always be built
20737 with constant operands. */
20738 RETURN (t);
20739
20740 case NON_LVALUE_EXPR:
20741 case VIEW_CONVERT_EXPR:
20742 if (location_wrapper_p (t))
20743 /* We need to do this here as well as in tsubst_copy so we get the
20744 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20745 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20746 EXPR_LOCATION (t)));
20747 /* fallthrough. */
20748
20749 default:
20750 /* Handle Objective-C++ constructs, if appropriate. */
20751 {
20752 tree subst
20753 = objcp_tsubst_copy_and_build (t, args, complain,
20754 in_decl, /*function_p=*/false);
20755 if (subst)
20756 RETURN (subst);
20757 }
20758 RETURN (tsubst_copy (t, args, complain, in_decl));
20759 }
20760
20761 #undef RECUR
20762 #undef RETURN
20763 out:
20764 input_location = save_loc;
20765 return retval;
20766 }
20767
20768 /* Verify that the instantiated ARGS are valid. For type arguments,
20769 make sure that the type's linkage is ok. For non-type arguments,
20770 make sure they are constants if they are integral or enumerations.
20771 Emit an error under control of COMPLAIN, and return TRUE on error. */
20772
20773 static bool
20774 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20775 {
20776 if (dependent_template_arg_p (t))
20777 return false;
20778 if (ARGUMENT_PACK_P (t))
20779 {
20780 tree vec = ARGUMENT_PACK_ARGS (t);
20781 int len = TREE_VEC_LENGTH (vec);
20782 bool result = false;
20783 int i;
20784
20785 for (i = 0; i < len; ++i)
20786 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20787 result = true;
20788 return result;
20789 }
20790 else if (TYPE_P (t))
20791 {
20792 /* [basic.link]: A name with no linkage (notably, the name
20793 of a class or enumeration declared in a local scope)
20794 shall not be used to declare an entity with linkage.
20795 This implies that names with no linkage cannot be used as
20796 template arguments
20797
20798 DR 757 relaxes this restriction for C++0x. */
20799 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20800 : no_linkage_check (t, /*relaxed_p=*/false));
20801
20802 if (nt)
20803 {
20804 /* DR 488 makes use of a type with no linkage cause
20805 type deduction to fail. */
20806 if (complain & tf_error)
20807 {
20808 if (TYPE_UNNAMED_P (nt))
20809 error ("%qT is/uses unnamed type", t);
20810 else
20811 error ("template argument for %qD uses local type %qT",
20812 tmpl, t);
20813 }
20814 return true;
20815 }
20816 /* In order to avoid all sorts of complications, we do not
20817 allow variably-modified types as template arguments. */
20818 else if (variably_modified_type_p (t, NULL_TREE))
20819 {
20820 if (complain & tf_error)
20821 error ("%qT is a variably modified type", t);
20822 return true;
20823 }
20824 }
20825 /* Class template and alias template arguments should be OK. */
20826 else if (DECL_TYPE_TEMPLATE_P (t))
20827 ;
20828 /* A non-type argument of integral or enumerated type must be a
20829 constant. */
20830 else if (TREE_TYPE (t)
20831 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20832 && !REFERENCE_REF_P (t)
20833 && !TREE_CONSTANT (t))
20834 {
20835 if (complain & tf_error)
20836 error ("integral expression %qE is not constant", t);
20837 return true;
20838 }
20839 return false;
20840 }
20841
20842 static bool
20843 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20844 {
20845 int ix, len = DECL_NTPARMS (tmpl);
20846 bool result = false;
20847
20848 for (ix = 0; ix != len; ix++)
20849 {
20850 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20851 result = true;
20852 }
20853 if (result && (complain & tf_error))
20854 error (" trying to instantiate %qD", tmpl);
20855 return result;
20856 }
20857
20858 /* We're out of SFINAE context now, so generate diagnostics for the access
20859 errors we saw earlier when instantiating D from TMPL and ARGS. */
20860
20861 static void
20862 recheck_decl_substitution (tree d, tree tmpl, tree args)
20863 {
20864 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20865 tree type = TREE_TYPE (pattern);
20866 location_t loc = input_location;
20867
20868 push_access_scope (d);
20869 push_deferring_access_checks (dk_no_deferred);
20870 input_location = DECL_SOURCE_LOCATION (pattern);
20871 tsubst (type, args, tf_warning_or_error, d);
20872 input_location = loc;
20873 pop_deferring_access_checks ();
20874 pop_access_scope (d);
20875 }
20876
20877 /* Instantiate the indicated variable, function, or alias template TMPL with
20878 the template arguments in TARG_PTR. */
20879
20880 static tree
20881 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20882 {
20883 tree targ_ptr = orig_args;
20884 tree fndecl;
20885 tree gen_tmpl;
20886 tree spec;
20887 bool access_ok = true;
20888
20889 if (tmpl == error_mark_node)
20890 return error_mark_node;
20891
20892 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20893
20894 /* If this function is a clone, handle it specially. */
20895 if (DECL_CLONED_FUNCTION_P (tmpl))
20896 {
20897 tree spec;
20898 tree clone;
20899
20900 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20901 DECL_CLONED_FUNCTION. */
20902 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20903 targ_ptr, complain);
20904 if (spec == error_mark_node)
20905 return error_mark_node;
20906
20907 /* Look for the clone. */
20908 FOR_EACH_CLONE (clone, spec)
20909 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20910 return clone;
20911 /* We should always have found the clone by now. */
20912 gcc_unreachable ();
20913 return NULL_TREE;
20914 }
20915
20916 if (targ_ptr == error_mark_node)
20917 return error_mark_node;
20918
20919 /* Check to see if we already have this specialization. */
20920 gen_tmpl = most_general_template (tmpl);
20921 if (TMPL_ARGS_DEPTH (targ_ptr)
20922 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20923 /* targ_ptr only has the innermost template args, so add the outer ones
20924 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20925 the case of a non-dependent call within a template definition). */
20926 targ_ptr = (add_outermost_template_args
20927 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20928 targ_ptr));
20929
20930 if (modules_p ())
20931 {
20932 tree origin = get_originating_module_decl (gen_tmpl);
20933 load_pending_specializations (CP_DECL_CONTEXT (origin),
20934 DECL_NAME (origin));
20935 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl))
20936 lazy_load_specializations (gen_tmpl);
20937 }
20938
20939 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20940 but it doesn't seem to be on the hot path. */
20941 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20942
20943 gcc_checking_assert (tmpl == gen_tmpl
20944 || ((fndecl
20945 = retrieve_specialization (tmpl, orig_args, 0))
20946 == spec)
20947 || fndecl == NULL_TREE);
20948
20949 if (spec != NULL_TREE)
20950 {
20951 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20952 {
20953 if (complain & tf_error)
20954 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20955 return error_mark_node;
20956 }
20957 return spec;
20958 }
20959
20960 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20961 complain))
20962 return error_mark_node;
20963
20964 /* We are building a FUNCTION_DECL, during which the access of its
20965 parameters and return types have to be checked. However this
20966 FUNCTION_DECL which is the desired context for access checking
20967 is not built yet. We solve this chicken-and-egg problem by
20968 deferring all checks until we have the FUNCTION_DECL. */
20969 push_deferring_access_checks (dk_deferred);
20970
20971 /* Instantiation of the function happens in the context of the function
20972 template, not the context of the overload resolution we're doing. */
20973 push_to_top_level ();
20974 /* If there are dependent arguments, e.g. because we're doing partial
20975 ordering, make sure processing_template_decl stays set. */
20976 if (uses_template_parms (targ_ptr))
20977 ++processing_template_decl;
20978 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20979 {
20980 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20981 complain, gen_tmpl, true);
20982 push_nested_class (ctx);
20983 }
20984
20985 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20986
20987 fndecl = NULL_TREE;
20988 if (VAR_P (pattern))
20989 {
20990 /* We need to determine if we're using a partial or explicit
20991 specialization now, because the type of the variable could be
20992 different. */
20993 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20994 tree elt = most_specialized_partial_spec (tid, complain);
20995 if (elt == error_mark_node)
20996 pattern = error_mark_node;
20997 else if (elt)
20998 {
20999 tree partial_tmpl = TREE_VALUE (elt);
21000 tree partial_args = TREE_PURPOSE (elt);
21001 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21002 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21003 }
21004 }
21005
21006 /* Substitute template parameters to obtain the specialization. */
21007 if (fndecl == NULL_TREE)
21008 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21009 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21010 pop_nested_class ();
21011 pop_from_top_level ();
21012
21013 if (fndecl == error_mark_node)
21014 {
21015 pop_deferring_access_checks ();
21016 return error_mark_node;
21017 }
21018
21019 /* The DECL_TI_TEMPLATE should always be the immediate parent
21020 template, not the most general template. */
21021 DECL_TI_TEMPLATE (fndecl) = tmpl;
21022 DECL_TI_ARGS (fndecl) = targ_ptr;
21023
21024 set_instantiating_module (fndecl);
21025
21026 /* Now we know the specialization, compute access previously
21027 deferred. Do no access control for inheriting constructors,
21028 as we already checked access for the inherited constructor. */
21029 if (!(flag_new_inheriting_ctors
21030 && DECL_INHERITED_CTOR (fndecl)))
21031 {
21032 push_access_scope (fndecl);
21033 if (!perform_deferred_access_checks (complain))
21034 access_ok = false;
21035 pop_access_scope (fndecl);
21036 }
21037 pop_deferring_access_checks ();
21038
21039 /* If we've just instantiated the main entry point for a function,
21040 instantiate all the alternate entry points as well. We do this
21041 by cloning the instantiation of the main entry point, not by
21042 instantiating the template clones. */
21043 if (tree chain = DECL_CHAIN (gen_tmpl))
21044 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21045 clone_cdtor (fndecl, /*update_methods=*/false);
21046
21047 if (!access_ok)
21048 {
21049 if (!(complain & tf_error))
21050 {
21051 /* Remember to reinstantiate when we're out of SFINAE so the user
21052 can see the errors. */
21053 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21054 }
21055 return error_mark_node;
21056 }
21057 return fndecl;
21058 }
21059
21060 /* Wrapper for instantiate_template_1. */
21061
21062 tree
21063 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21064 {
21065 tree ret;
21066 timevar_push (TV_TEMPLATE_INST);
21067 ret = instantiate_template_1 (tmpl, orig_args, complain);
21068 timevar_pop (TV_TEMPLATE_INST);
21069 return ret;
21070 }
21071
21072 /* Instantiate the alias template TMPL with ARGS. Also push a template
21073 instantiation level, which instantiate_template doesn't do because
21074 functions and variables have sufficient context established by the
21075 callers. */
21076
21077 static tree
21078 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21079 {
21080 if (tmpl == error_mark_node || args == error_mark_node)
21081 return error_mark_node;
21082
21083 args =
21084 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21085 args, tmpl, complain,
21086 /*require_all_args=*/true,
21087 /*use_default_args=*/true);
21088
21089 /* FIXME check for satisfaction in check_instantiated_args. */
21090 if (flag_concepts
21091 && !any_dependent_template_arguments_p (args)
21092 && !constraints_satisfied_p (tmpl, args))
21093 {
21094 if (complain & tf_error)
21095 {
21096 auto_diagnostic_group d;
21097 error ("template constraint failure for %qD", tmpl);
21098 diagnose_constraints (input_location, tmpl, args);
21099 }
21100 return error_mark_node;
21101 }
21102
21103 if (!push_tinst_level (tmpl, args))
21104 return error_mark_node;
21105 tree r = instantiate_template (tmpl, args, complain);
21106 pop_tinst_level ();
21107
21108 return r;
21109 }
21110
21111 /* PARM is a template parameter pack for FN. Returns true iff
21112 PARM is used in a deducible way in the argument list of FN. */
21113
21114 static bool
21115 pack_deducible_p (tree parm, tree fn)
21116 {
21117 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21118 for (; t; t = TREE_CHAIN (t))
21119 {
21120 tree type = TREE_VALUE (t);
21121 tree packs;
21122 if (!PACK_EXPANSION_P (type))
21123 continue;
21124 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21125 packs; packs = TREE_CHAIN (packs))
21126 if (template_args_equal (TREE_VALUE (packs), parm))
21127 {
21128 /* The template parameter pack is used in a function parameter
21129 pack. If this is the end of the parameter list, the
21130 template parameter pack is deducible. */
21131 if (TREE_CHAIN (t) == void_list_node)
21132 return true;
21133 else
21134 /* Otherwise, not. Well, it could be deduced from
21135 a non-pack parameter, but doing so would end up with
21136 a deduction mismatch, so don't bother. */
21137 return false;
21138 }
21139 }
21140 /* The template parameter pack isn't used in any function parameter
21141 packs, but it might be used deeper, e.g. tuple<Args...>. */
21142 return true;
21143 }
21144
21145 /* Subroutine of fn_type_unification: check non-dependent parms for
21146 convertibility. */
21147
21148 static int
21149 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21150 tree fn, unification_kind_t strict, int flags,
21151 struct conversion **convs, bool explain_p)
21152 {
21153 /* Non-constructor methods need to leave a conversion for 'this', which
21154 isn't included in nargs here. */
21155 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21156 && !DECL_CONSTRUCTOR_P (fn));
21157
21158 for (unsigned ia = 0;
21159 parms && parms != void_list_node && ia < nargs; )
21160 {
21161 tree parm = TREE_VALUE (parms);
21162
21163 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21164 && (!TREE_CHAIN (parms)
21165 || TREE_CHAIN (parms) == void_list_node))
21166 /* For a function parameter pack that occurs at the end of the
21167 parameter-declaration-list, the type A of each remaining
21168 argument of the call is compared with the type P of the
21169 declarator-id of the function parameter pack. */
21170 break;
21171
21172 parms = TREE_CHAIN (parms);
21173
21174 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21175 /* For a function parameter pack that does not occur at the
21176 end of the parameter-declaration-list, the type of the
21177 parameter pack is a non-deduced context. */
21178 continue;
21179
21180 if (!uses_template_parms (parm))
21181 {
21182 tree arg = args[ia];
21183 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21184 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21185
21186 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21187 conv_p, explain_p))
21188 return 1;
21189 }
21190
21191 ++ia;
21192 }
21193
21194 return 0;
21195 }
21196
21197 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21198 NARGS elements of the arguments that are being used when calling
21199 it. TARGS is a vector into which the deduced template arguments
21200 are placed.
21201
21202 Returns either a FUNCTION_DECL for the matching specialization of FN or
21203 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21204 true, diagnostics will be printed to explain why it failed.
21205
21206 If FN is a conversion operator, or we are trying to produce a specific
21207 specialization, RETURN_TYPE is the return type desired.
21208
21209 The EXPLICIT_TARGS are explicit template arguments provided via a
21210 template-id.
21211
21212 The parameter STRICT is one of:
21213
21214 DEDUCE_CALL:
21215 We are deducing arguments for a function call, as in
21216 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21217 deducing arguments for a call to the result of a conversion
21218 function template, as in [over.call.object].
21219
21220 DEDUCE_CONV:
21221 We are deducing arguments for a conversion function, as in
21222 [temp.deduct.conv].
21223
21224 DEDUCE_EXACT:
21225 We are deducing arguments when doing an explicit instantiation
21226 as in [temp.explicit], when determining an explicit specialization
21227 as in [temp.expl.spec], or when taking the address of a function
21228 template, as in [temp.deduct.funcaddr]. */
21229
21230 tree
21231 fn_type_unification (tree fn,
21232 tree explicit_targs,
21233 tree targs,
21234 const tree *args,
21235 unsigned int nargs,
21236 tree return_type,
21237 unification_kind_t strict,
21238 int flags,
21239 struct conversion **convs,
21240 bool explain_p,
21241 bool decltype_p)
21242 {
21243 tree parms;
21244 tree fntype;
21245 tree decl = NULL_TREE;
21246 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21247 bool ok;
21248 static int deduction_depth;
21249 /* type_unification_real will pass back any access checks from default
21250 template argument substitution. */
21251 vec<deferred_access_check, va_gc> *checks = NULL;
21252 /* We don't have all the template args yet. */
21253 bool incomplete = true;
21254
21255 tree orig_fn = fn;
21256 if (flag_new_inheriting_ctors)
21257 fn = strip_inheriting_ctors (fn);
21258
21259 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21260 tree r = error_mark_node;
21261
21262 tree full_targs = targs;
21263 if (TMPL_ARGS_DEPTH (targs)
21264 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21265 full_targs = (add_outermost_template_args
21266 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21267 targs));
21268
21269 if (decltype_p)
21270 complain |= tf_decltype;
21271
21272 /* In C++0x, it's possible to have a function template whose type depends
21273 on itself recursively. This is most obvious with decltype, but can also
21274 occur with enumeration scope (c++/48969). So we need to catch infinite
21275 recursion and reject the substitution at deduction time; this function
21276 will return error_mark_node for any repeated substitution.
21277
21278 This also catches excessive recursion such as when f<N> depends on
21279 f<N-1> across all integers, and returns error_mark_node for all the
21280 substitutions back up to the initial one.
21281
21282 This is, of course, not reentrant. */
21283 if (excessive_deduction_depth)
21284 return error_mark_node;
21285 ++deduction_depth;
21286
21287 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21288
21289 fntype = TREE_TYPE (fn);
21290 if (explicit_targs)
21291 {
21292 /* [temp.deduct]
21293
21294 The specified template arguments must match the template
21295 parameters in kind (i.e., type, nontype, template), and there
21296 must not be more arguments than there are parameters;
21297 otherwise type deduction fails.
21298
21299 Nontype arguments must match the types of the corresponding
21300 nontype template parameters, or must be convertible to the
21301 types of the corresponding nontype parameters as specified in
21302 _temp.arg.nontype_, otherwise type deduction fails.
21303
21304 All references in the function type of the function template
21305 to the corresponding template parameters are replaced by the
21306 specified template argument values. If a substitution in a
21307 template parameter or in the function type of the function
21308 template results in an invalid type, type deduction fails. */
21309 int i, len = TREE_VEC_LENGTH (tparms);
21310 location_t loc = input_location;
21311 incomplete = false;
21312
21313 if (explicit_targs == error_mark_node)
21314 goto fail;
21315
21316 if (TMPL_ARGS_DEPTH (explicit_targs)
21317 < TMPL_ARGS_DEPTH (full_targs))
21318 explicit_targs = add_outermost_template_args (full_targs,
21319 explicit_targs);
21320
21321 /* Adjust any explicit template arguments before entering the
21322 substitution context. */
21323 explicit_targs
21324 = (coerce_template_parms (tparms, explicit_targs, fn,
21325 complain|tf_partial,
21326 /*require_all_args=*/false,
21327 /*use_default_args=*/false));
21328 if (explicit_targs == error_mark_node)
21329 goto fail;
21330
21331 /* Substitute the explicit args into the function type. This is
21332 necessary so that, for instance, explicitly declared function
21333 arguments can match null pointed constants. If we were given
21334 an incomplete set of explicit args, we must not do semantic
21335 processing during substitution as we could create partial
21336 instantiations. */
21337 for (i = 0; i < len; i++)
21338 {
21339 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21340 bool parameter_pack = false;
21341 tree targ = TREE_VEC_ELT (explicit_targs, i);
21342
21343 /* Dig out the actual parm. */
21344 if (TREE_CODE (parm) == TYPE_DECL
21345 || TREE_CODE (parm) == TEMPLATE_DECL)
21346 {
21347 parm = TREE_TYPE (parm);
21348 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21349 }
21350 else if (TREE_CODE (parm) == PARM_DECL)
21351 {
21352 parm = DECL_INITIAL (parm);
21353 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21354 }
21355
21356 if (targ == NULL_TREE)
21357 /* No explicit argument for this template parameter. */
21358 incomplete = true;
21359 else if (parameter_pack && pack_deducible_p (parm, fn))
21360 {
21361 /* Mark the argument pack as "incomplete". We could
21362 still deduce more arguments during unification.
21363 We remove this mark in type_unification_real. */
21364 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21365 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21366 = ARGUMENT_PACK_ARGS (targ);
21367
21368 /* We have some incomplete argument packs. */
21369 incomplete = true;
21370 }
21371 }
21372
21373 if (incomplete)
21374 {
21375 if (!push_tinst_level (fn, explicit_targs))
21376 {
21377 excessive_deduction_depth = true;
21378 goto fail;
21379 }
21380 ++processing_template_decl;
21381 input_location = DECL_SOURCE_LOCATION (fn);
21382 /* Ignore any access checks; we'll see them again in
21383 instantiate_template and they might have the wrong
21384 access path at this point. */
21385 push_deferring_access_checks (dk_deferred);
21386 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21387 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21388 pop_deferring_access_checks ();
21389 input_location = loc;
21390 --processing_template_decl;
21391 pop_tinst_level ();
21392
21393 if (fntype == error_mark_node)
21394 goto fail;
21395 }
21396
21397 /* Place the explicitly specified arguments in TARGS. */
21398 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21399 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21400 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21401 if (!incomplete && CHECKING_P
21402 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21403 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21404 (targs, NUM_TMPL_ARGS (explicit_targs));
21405 }
21406
21407 if (return_type && strict != DEDUCE_CALL)
21408 {
21409 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21410 new_args[0] = return_type;
21411 memcpy (new_args + 1, args, nargs * sizeof (tree));
21412 args = new_args;
21413 ++nargs;
21414 }
21415
21416 if (!incomplete)
21417 goto deduced;
21418
21419 /* Never do unification on the 'this' parameter. */
21420 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21421
21422 if (return_type && strict == DEDUCE_CALL)
21423 {
21424 /* We're deducing for a call to the result of a template conversion
21425 function. The parms we really want are in return_type. */
21426 if (INDIRECT_TYPE_P (return_type))
21427 return_type = TREE_TYPE (return_type);
21428 parms = TYPE_ARG_TYPES (return_type);
21429 }
21430 else if (return_type)
21431 {
21432 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21433 }
21434
21435 /* We allow incomplete unification without an error message here
21436 because the standard doesn't seem to explicitly prohibit it. Our
21437 callers must be ready to deal with unification failures in any
21438 event. */
21439
21440 /* If we aren't explaining yet, push tinst context so we can see where
21441 any errors (e.g. from class instantiations triggered by instantiation
21442 of default template arguments) come from. If we are explaining, this
21443 context is redundant. */
21444 if (!explain_p && !push_tinst_level (fn, targs))
21445 {
21446 excessive_deduction_depth = true;
21447 goto fail;
21448 }
21449
21450 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21451 full_targs, parms, args, nargs, /*subr=*/0,
21452 strict, &checks, explain_p);
21453 if (!explain_p)
21454 pop_tinst_level ();
21455 if (!ok)
21456 goto fail;
21457
21458 /* Now that we have bindings for all of the template arguments,
21459 ensure that the arguments deduced for the template template
21460 parameters have compatible template parameter lists. We cannot
21461 check this property before we have deduced all template
21462 arguments, because the template parameter types of a template
21463 template parameter might depend on prior template parameters
21464 deduced after the template template parameter. The following
21465 ill-formed example illustrates this issue:
21466
21467 template<typename T, template<T> class C> void f(C<5>, T);
21468
21469 template<int N> struct X {};
21470
21471 void g() {
21472 f(X<5>(), 5l); // error: template argument deduction fails
21473 }
21474
21475 The template parameter list of 'C' depends on the template type
21476 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21477 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21478 time that we deduce 'C'. */
21479 if (!template_template_parm_bindings_ok_p
21480 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21481 {
21482 unify_inconsistent_template_template_parameters (explain_p);
21483 goto fail;
21484 }
21485
21486 deduced:
21487
21488 /* CWG2369: Check satisfaction before non-deducible conversions. */
21489 if (!constraints_satisfied_p (fn, targs))
21490 {
21491 if (explain_p)
21492 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21493 goto fail;
21494 }
21495
21496 /* DR 1391: All parameters have args, now check non-dependent parms for
21497 convertibility. We don't do this if all args were explicitly specified,
21498 as the standard says that we substitute explicit args immediately. */
21499 if (incomplete
21500 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21501 convs, explain_p))
21502 goto fail;
21503
21504 /* All is well so far. Now, check:
21505
21506 [temp.deduct]
21507
21508 When all template arguments have been deduced, all uses of
21509 template parameters in nondeduced contexts are replaced with
21510 the corresponding deduced argument values. If the
21511 substitution results in an invalid type, as described above,
21512 type deduction fails. */
21513 if (!push_tinst_level (fn, targs))
21514 {
21515 excessive_deduction_depth = true;
21516 goto fail;
21517 }
21518
21519 /* Also collect access checks from the instantiation. */
21520 reopen_deferring_access_checks (checks);
21521
21522 decl = instantiate_template (fn, targs, complain);
21523
21524 checks = get_deferred_access_checks ();
21525 pop_deferring_access_checks ();
21526
21527 pop_tinst_level ();
21528
21529 if (decl == error_mark_node)
21530 goto fail;
21531
21532 /* Now perform any access checks encountered during substitution. */
21533 push_access_scope (decl);
21534 ok = perform_access_checks (checks, complain);
21535 pop_access_scope (decl);
21536 if (!ok)
21537 goto fail;
21538
21539 /* If we're looking for an exact match, check that what we got
21540 is indeed an exact match. It might not be if some template
21541 parameters are used in non-deduced contexts. But don't check
21542 for an exact match if we have dependent template arguments;
21543 in that case we're doing partial ordering, and we already know
21544 that we have two candidates that will provide the actual type. */
21545 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21546 {
21547 tree substed = TREE_TYPE (decl);
21548 unsigned int i;
21549
21550 tree sarg
21551 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21552 if (return_type)
21553 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21554 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21555 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21556 {
21557 unify_type_mismatch (explain_p, args[i],
21558 TREE_VALUE (sarg));
21559 goto fail;
21560 }
21561 }
21562
21563 /* After doing deduction with the inherited constructor, actually return an
21564 instantiation of the inheriting constructor. */
21565 if (orig_fn != fn)
21566 decl = instantiate_template (orig_fn, targs, complain);
21567
21568 r = decl;
21569
21570 fail:
21571 --deduction_depth;
21572 if (excessive_deduction_depth)
21573 {
21574 if (deduction_depth == 0)
21575 /* Reset once we're all the way out. */
21576 excessive_deduction_depth = false;
21577 }
21578
21579 return r;
21580 }
21581
21582 /* Adjust types before performing type deduction, as described in
21583 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21584 sections are symmetric. PARM is the type of a function parameter
21585 or the return type of the conversion function. ARG is the type of
21586 the argument passed to the call, or the type of the value
21587 initialized with the result of the conversion function.
21588 ARG_EXPR is the original argument expression, which may be null. */
21589
21590 static int
21591 maybe_adjust_types_for_deduction (unification_kind_t strict,
21592 tree* parm,
21593 tree* arg,
21594 tree arg_expr)
21595 {
21596 int result = 0;
21597
21598 switch (strict)
21599 {
21600 case DEDUCE_CALL:
21601 break;
21602
21603 case DEDUCE_CONV:
21604 /* Swap PARM and ARG throughout the remainder of this
21605 function; the handling is precisely symmetric since PARM
21606 will initialize ARG rather than vice versa. */
21607 std::swap (parm, arg);
21608 break;
21609
21610 case DEDUCE_EXACT:
21611 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21612 too, but here handle it by stripping the reference from PARM
21613 rather than by adding it to ARG. */
21614 if (TYPE_REF_P (*parm)
21615 && TYPE_REF_IS_RVALUE (*parm)
21616 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21617 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21618 && TYPE_REF_P (*arg)
21619 && !TYPE_REF_IS_RVALUE (*arg))
21620 *parm = TREE_TYPE (*parm);
21621 /* Nothing else to do in this case. */
21622 return 0;
21623
21624 default:
21625 gcc_unreachable ();
21626 }
21627
21628 if (!TYPE_REF_P (*parm))
21629 {
21630 /* [temp.deduct.call]
21631
21632 If P is not a reference type:
21633
21634 --If A is an array type, the pointer type produced by the
21635 array-to-pointer standard conversion (_conv.array_) is
21636 used in place of A for type deduction; otherwise,
21637
21638 --If A is a function type, the pointer type produced by
21639 the function-to-pointer standard conversion
21640 (_conv.func_) is used in place of A for type deduction;
21641 otherwise,
21642
21643 --If A is a cv-qualified type, the top level
21644 cv-qualifiers of A's type are ignored for type
21645 deduction. */
21646 if (TREE_CODE (*arg) == ARRAY_TYPE)
21647 *arg = build_pointer_type (TREE_TYPE (*arg));
21648 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21649 *arg = build_pointer_type (*arg);
21650 else
21651 *arg = TYPE_MAIN_VARIANT (*arg);
21652 }
21653
21654 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21655 reference to a cv-unqualified template parameter that does not represent a
21656 template parameter of a class template (during class template argument
21657 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21658 an lvalue, the type "lvalue reference to A" is used in place of A for type
21659 deduction. */
21660 if (TYPE_REF_P (*parm)
21661 && TYPE_REF_IS_RVALUE (*parm)
21662 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21663 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21664 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21665 && (arg_expr ? lvalue_p (arg_expr)
21666 /* try_one_overload doesn't provide an arg_expr, but
21667 functions are always lvalues. */
21668 : TREE_CODE (*arg) == FUNCTION_TYPE))
21669 *arg = build_reference_type (*arg);
21670
21671 /* [temp.deduct.call]
21672
21673 If P is a cv-qualified type, the top level cv-qualifiers
21674 of P's type are ignored for type deduction. If P is a
21675 reference type, the type referred to by P is used for
21676 type deduction. */
21677 *parm = TYPE_MAIN_VARIANT (*parm);
21678 if (TYPE_REF_P (*parm))
21679 {
21680 *parm = TREE_TYPE (*parm);
21681 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21682 }
21683
21684 /* DR 322. For conversion deduction, remove a reference type on parm
21685 too (which has been swapped into ARG). */
21686 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21687 *arg = TREE_TYPE (*arg);
21688
21689 return result;
21690 }
21691
21692 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21693 template which doesn't contain any deducible template parameters; check if
21694 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21695 unify_one_argument. */
21696
21697 static int
21698 check_non_deducible_conversion (tree parm, tree arg, int strict,
21699 int flags, struct conversion **conv_p,
21700 bool explain_p)
21701 {
21702 tree type;
21703
21704 if (!TYPE_P (arg))
21705 type = TREE_TYPE (arg);
21706 else
21707 type = arg;
21708
21709 if (same_type_p (parm, type))
21710 return unify_success (explain_p);
21711
21712 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21713 if (strict == DEDUCE_CONV)
21714 {
21715 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21716 return unify_success (explain_p);
21717 }
21718 else if (strict != DEDUCE_EXACT)
21719 {
21720 bool ok = false;
21721 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21722 if (conv_p)
21723 /* Avoid recalculating this in add_function_candidate. */
21724 ok = (*conv_p
21725 = good_conversion (parm, type, conv_arg, flags, complain));
21726 else
21727 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21728 if (ok)
21729 return unify_success (explain_p);
21730 }
21731
21732 if (strict == DEDUCE_EXACT)
21733 return unify_type_mismatch (explain_p, parm, arg);
21734 else
21735 return unify_arg_conversion (explain_p, parm, type, arg);
21736 }
21737
21738 static bool uses_deducible_template_parms (tree type);
21739
21740 /* Returns true iff the expression EXPR is one from which a template
21741 argument can be deduced. In other words, if it's an undecorated
21742 use of a template non-type parameter. */
21743
21744 static bool
21745 deducible_expression (tree expr)
21746 {
21747 /* Strip implicit conversions. */
21748 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21749 expr = TREE_OPERAND (expr, 0);
21750 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21751 }
21752
21753 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21754 deducible way; that is, if it has a max value of <PARM> - 1. */
21755
21756 static bool
21757 deducible_array_bound (tree domain)
21758 {
21759 if (domain == NULL_TREE)
21760 return false;
21761
21762 tree max = TYPE_MAX_VALUE (domain);
21763 if (TREE_CODE (max) != MINUS_EXPR)
21764 return false;
21765
21766 return deducible_expression (TREE_OPERAND (max, 0));
21767 }
21768
21769 /* Returns true iff the template arguments ARGS use a template parameter
21770 in a deducible way. */
21771
21772 static bool
21773 deducible_template_args (tree args)
21774 {
21775 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21776 {
21777 bool deducible;
21778 tree elt = TREE_VEC_ELT (args, i);
21779 if (ARGUMENT_PACK_P (elt))
21780 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21781 else
21782 {
21783 if (PACK_EXPANSION_P (elt))
21784 elt = PACK_EXPANSION_PATTERN (elt);
21785 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21786 deducible = true;
21787 else if (TYPE_P (elt))
21788 deducible = uses_deducible_template_parms (elt);
21789 else
21790 deducible = deducible_expression (elt);
21791 }
21792 if (deducible)
21793 return true;
21794 }
21795 return false;
21796 }
21797
21798 /* Returns true iff TYPE contains any deducible references to template
21799 parameters, as per 14.8.2.5. */
21800
21801 static bool
21802 uses_deducible_template_parms (tree type)
21803 {
21804 if (PACK_EXPANSION_P (type))
21805 type = PACK_EXPANSION_PATTERN (type);
21806
21807 /* T
21808 cv-list T
21809 TT<T>
21810 TT<i>
21811 TT<> */
21812 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21813 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21814 return true;
21815
21816 /* T*
21817 T&
21818 T&& */
21819 if (INDIRECT_TYPE_P (type))
21820 return uses_deducible_template_parms (TREE_TYPE (type));
21821
21822 /* T[integer-constant ]
21823 type [i] */
21824 if (TREE_CODE (type) == ARRAY_TYPE)
21825 return (uses_deducible_template_parms (TREE_TYPE (type))
21826 || deducible_array_bound (TYPE_DOMAIN (type)));
21827
21828 /* T type ::*
21829 type T::*
21830 T T::*
21831 T (type ::*)()
21832 type (T::*)()
21833 type (type ::*)(T)
21834 type (T::*)(T)
21835 T (type ::*)(T)
21836 T (T::*)()
21837 T (T::*)(T) */
21838 if (TYPE_PTRMEM_P (type))
21839 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21840 || (uses_deducible_template_parms
21841 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21842
21843 /* template-name <T> (where template-name refers to a class template)
21844 template-name <i> (where template-name refers to a class template) */
21845 if (CLASS_TYPE_P (type)
21846 && CLASSTYPE_TEMPLATE_INFO (type)
21847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21848 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21849 (CLASSTYPE_TI_ARGS (type)));
21850
21851 /* type (T)
21852 T()
21853 T(T) */
21854 if (FUNC_OR_METHOD_TYPE_P (type))
21855 {
21856 if (uses_deducible_template_parms (TREE_TYPE (type)))
21857 return true;
21858 tree parm = TYPE_ARG_TYPES (type);
21859 if (TREE_CODE (type) == METHOD_TYPE)
21860 parm = TREE_CHAIN (parm);
21861 for (; parm; parm = TREE_CHAIN (parm))
21862 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21863 return true;
21864 }
21865
21866 return false;
21867 }
21868
21869 /* Subroutine of type_unification_real and unify_pack_expansion to
21870 handle unification of a single P/A pair. Parameters are as
21871 for those functions. */
21872
21873 static int
21874 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21875 int subr, unification_kind_t strict,
21876 bool explain_p)
21877 {
21878 tree arg_expr = NULL_TREE;
21879 int arg_strict;
21880
21881 if (arg == error_mark_node || parm == error_mark_node)
21882 return unify_invalid (explain_p);
21883 if (arg == unknown_type_node)
21884 /* We can't deduce anything from this, but we might get all the
21885 template args from other function args. */
21886 return unify_success (explain_p);
21887
21888 /* Implicit conversions (Clause 4) will be performed on a function
21889 argument to convert it to the type of the corresponding function
21890 parameter if the parameter type contains no template-parameters that
21891 participate in template argument deduction. */
21892 if (strict != DEDUCE_EXACT
21893 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21894 /* For function parameters with no deducible template parameters,
21895 just return. We'll check non-dependent conversions later. */
21896 return unify_success (explain_p);
21897
21898 switch (strict)
21899 {
21900 case DEDUCE_CALL:
21901 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21902 | UNIFY_ALLOW_MORE_CV_QUAL
21903 | UNIFY_ALLOW_DERIVED);
21904 break;
21905
21906 case DEDUCE_CONV:
21907 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21908 break;
21909
21910 case DEDUCE_EXACT:
21911 arg_strict = UNIFY_ALLOW_NONE;
21912 break;
21913
21914 default:
21915 gcc_unreachable ();
21916 }
21917
21918 /* We only do these transformations if this is the top-level
21919 parameter_type_list in a call or declaration matching; in other
21920 situations (nested function declarators, template argument lists) we
21921 won't be comparing a type to an expression, and we don't do any type
21922 adjustments. */
21923 if (!subr)
21924 {
21925 if (!TYPE_P (arg))
21926 {
21927 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21928 if (type_unknown_p (arg))
21929 {
21930 /* [temp.deduct.type] A template-argument can be
21931 deduced from a pointer to function or pointer
21932 to member function argument if the set of
21933 overloaded functions does not contain function
21934 templates and at most one of a set of
21935 overloaded functions provides a unique
21936 match. */
21937 resolve_overloaded_unification (tparms, targs, parm,
21938 arg, strict,
21939 arg_strict, explain_p);
21940 /* If a unique match was not found, this is a
21941 non-deduced context, so we still succeed. */
21942 return unify_success (explain_p);
21943 }
21944
21945 arg_expr = arg;
21946 arg = unlowered_expr_type (arg);
21947 if (arg == error_mark_node)
21948 return unify_invalid (explain_p);
21949 }
21950
21951 arg_strict |=
21952 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21953 }
21954 else
21955 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21956 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21957 return unify_template_argument_mismatch (explain_p, parm, arg);
21958
21959 /* For deduction from an init-list we need the actual list. */
21960 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21961 arg = arg_expr;
21962 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21963 }
21964
21965 /* for_each_template_parm callback that always returns 0. */
21966
21967 static int
21968 zero_r (tree, void *)
21969 {
21970 return 0;
21971 }
21972
21973 /* for_each_template_parm any_fn callback to handle deduction of a template
21974 type argument from the type of an array bound. */
21975
21976 static int
21977 array_deduction_r (tree t, void *data)
21978 {
21979 tree_pair_p d = (tree_pair_p)data;
21980 tree &tparms = d->purpose;
21981 tree &targs = d->value;
21982
21983 if (TREE_CODE (t) == ARRAY_TYPE)
21984 if (tree dom = TYPE_DOMAIN (t))
21985 if (tree max = TYPE_MAX_VALUE (dom))
21986 {
21987 if (TREE_CODE (max) == MINUS_EXPR)
21988 max = TREE_OPERAND (max, 0);
21989 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21990 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21991 UNIFY_ALLOW_NONE, /*explain*/false);
21992 }
21993
21994 /* Keep walking. */
21995 return 0;
21996 }
21997
21998 /* Try to deduce any not-yet-deduced template type arguments from the type of
21999 an array bound. This is handled separately from unify because 14.8.2.5 says
22000 "The type of a type parameter is only deduced from an array bound if it is
22001 not otherwise deduced." */
22002
22003 static void
22004 try_array_deduction (tree tparms, tree targs, tree parm)
22005 {
22006 tree_pair_s data = { tparms, targs };
22007 hash_set<tree> visited;
22008 for_each_template_parm (parm, zero_r, &data, &visited,
22009 /*nondeduced*/false, array_deduction_r);
22010 }
22011
22012 /* Most parms like fn_type_unification.
22013
22014 If SUBR is 1, we're being called recursively (to unify the
22015 arguments of a function or method parameter of a function
22016 template).
22017
22018 CHECKS is a pointer to a vector of access checks encountered while
22019 substituting default template arguments. */
22020
22021 static int
22022 type_unification_real (tree tparms,
22023 tree full_targs,
22024 tree xparms,
22025 const tree *xargs,
22026 unsigned int xnargs,
22027 int subr,
22028 unification_kind_t strict,
22029 vec<deferred_access_check, va_gc> **checks,
22030 bool explain_p)
22031 {
22032 tree parm, arg;
22033 int i;
22034 int ntparms = TREE_VEC_LENGTH (tparms);
22035 int saw_undeduced = 0;
22036 tree parms;
22037 const tree *args;
22038 unsigned int nargs;
22039 unsigned int ia;
22040
22041 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22042 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22043 gcc_assert (ntparms > 0);
22044
22045 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22046
22047 /* Reset the number of non-defaulted template arguments contained
22048 in TARGS. */
22049 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22050
22051 again:
22052 parms = xparms;
22053 args = xargs;
22054 nargs = xnargs;
22055
22056 ia = 0;
22057 while (parms && parms != void_list_node
22058 && ia < nargs)
22059 {
22060 parm = TREE_VALUE (parms);
22061
22062 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22063 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22064 /* For a function parameter pack that occurs at the end of the
22065 parameter-declaration-list, the type A of each remaining
22066 argument of the call is compared with the type P of the
22067 declarator-id of the function parameter pack. */
22068 break;
22069
22070 parms = TREE_CHAIN (parms);
22071
22072 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22073 /* For a function parameter pack that does not occur at the
22074 end of the parameter-declaration-list, the type of the
22075 parameter pack is a non-deduced context. */
22076 continue;
22077
22078 arg = args[ia];
22079 ++ia;
22080
22081 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22082 explain_p))
22083 return 1;
22084 }
22085
22086 if (parms
22087 && parms != void_list_node
22088 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22089 {
22090 /* Unify the remaining arguments with the pack expansion type. */
22091 tree argvec;
22092 tree parmvec = make_tree_vec (1);
22093
22094 /* Allocate a TREE_VEC and copy in all of the arguments */
22095 argvec = make_tree_vec (nargs - ia);
22096 for (i = 0; ia < nargs; ++ia, ++i)
22097 TREE_VEC_ELT (argvec, i) = args[ia];
22098
22099 /* Copy the parameter into parmvec. */
22100 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22101 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22102 /*subr=*/subr, explain_p))
22103 return 1;
22104
22105 /* Advance to the end of the list of parameters. */
22106 parms = TREE_CHAIN (parms);
22107 }
22108
22109 /* Fail if we've reached the end of the parm list, and more args
22110 are present, and the parm list isn't variadic. */
22111 if (ia < nargs && parms == void_list_node)
22112 return unify_too_many_arguments (explain_p, nargs, ia);
22113 /* Fail if parms are left and they don't have default values and
22114 they aren't all deduced as empty packs (c++/57397). This is
22115 consistent with sufficient_parms_p. */
22116 if (parms && parms != void_list_node
22117 && TREE_PURPOSE (parms) == NULL_TREE)
22118 {
22119 unsigned int count = nargs;
22120 tree p = parms;
22121 bool type_pack_p;
22122 do
22123 {
22124 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22125 if (!type_pack_p)
22126 count++;
22127 p = TREE_CHAIN (p);
22128 }
22129 while (p && p != void_list_node);
22130 if (count != nargs)
22131 return unify_too_few_arguments (explain_p, ia, count,
22132 type_pack_p);
22133 }
22134
22135 if (!subr)
22136 {
22137 tsubst_flags_t complain = (explain_p
22138 ? tf_warning_or_error
22139 : tf_none);
22140 bool tried_array_deduction = (cxx_dialect < cxx17);
22141
22142 for (i = 0; i < ntparms; i++)
22143 {
22144 tree targ = TREE_VEC_ELT (targs, i);
22145 tree tparm = TREE_VEC_ELT (tparms, i);
22146
22147 /* Clear the "incomplete" flags on all argument packs now so that
22148 substituting them into later default arguments works. */
22149 if (targ && ARGUMENT_PACK_P (targ))
22150 {
22151 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22152 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22153 }
22154
22155 if (targ || tparm == error_mark_node)
22156 continue;
22157 tparm = TREE_VALUE (tparm);
22158
22159 if (TREE_CODE (tparm) == TYPE_DECL
22160 && !tried_array_deduction)
22161 {
22162 try_array_deduction (tparms, targs, xparms);
22163 tried_array_deduction = true;
22164 if (TREE_VEC_ELT (targs, i))
22165 continue;
22166 }
22167
22168 /* If this is an undeduced nontype parameter that depends on
22169 a type parameter, try another pass; its type may have been
22170 deduced from a later argument than the one from which
22171 this parameter can be deduced. */
22172 if (TREE_CODE (tparm) == PARM_DECL
22173 && uses_template_parms (TREE_TYPE (tparm))
22174 && saw_undeduced < 2)
22175 {
22176 saw_undeduced = 1;
22177 continue;
22178 }
22179
22180 /* Core issue #226 (C++0x) [temp.deduct]:
22181
22182 If a template argument has not been deduced, its
22183 default template argument, if any, is used.
22184
22185 When we are in C++98 mode, TREE_PURPOSE will either
22186 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22187 to explicitly check cxx_dialect here. */
22188 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22189 /* OK, there is a default argument. Wait until after the
22190 conversion check to do substitution. */
22191 continue;
22192
22193 /* If the type parameter is a parameter pack, then it will
22194 be deduced to an empty parameter pack. */
22195 if (template_parameter_pack_p (tparm))
22196 {
22197 tree arg;
22198
22199 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22200 {
22201 arg = make_node (NONTYPE_ARGUMENT_PACK);
22202 TREE_CONSTANT (arg) = 1;
22203 }
22204 else
22205 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22206
22207 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22208
22209 TREE_VEC_ELT (targs, i) = arg;
22210 continue;
22211 }
22212
22213 return unify_parameter_deduction_failure (explain_p, tparm);
22214 }
22215
22216 /* Now substitute into the default template arguments. */
22217 for (i = 0; i < ntparms; i++)
22218 {
22219 tree targ = TREE_VEC_ELT (targs, i);
22220 tree tparm = TREE_VEC_ELT (tparms, i);
22221
22222 if (targ || tparm == error_mark_node)
22223 continue;
22224 tree parm = TREE_VALUE (tparm);
22225 tree arg = TREE_PURPOSE (tparm);
22226 reopen_deferring_access_checks (*checks);
22227 location_t save_loc = input_location;
22228 if (DECL_P (parm))
22229 input_location = DECL_SOURCE_LOCATION (parm);
22230
22231 if (saw_undeduced == 1
22232 && TREE_CODE (parm) == PARM_DECL
22233 && uses_template_parms (TREE_TYPE (parm)))
22234 {
22235 /* The type of this non-type parameter depends on undeduced
22236 parameters. Don't try to use its default argument yet,
22237 since we might deduce an argument for it on the next pass,
22238 but do check whether the arguments we already have cause
22239 substitution failure, so that that happens before we try
22240 later default arguments (78489). */
22241 ++processing_template_decl;
22242 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22243 NULL_TREE);
22244 --processing_template_decl;
22245 if (type == error_mark_node)
22246 arg = error_mark_node;
22247 else
22248 arg = NULL_TREE;
22249 }
22250 else
22251 {
22252 /* Even if the call is happening in template context, getting
22253 here means it's non-dependent, and a default argument is
22254 considered a separate definition under [temp.decls], so we can
22255 do this substitution without processing_template_decl. This
22256 is important if the default argument contains something that
22257 might be instantiation-dependent like access (87480). */
22258 processing_template_decl_sentinel s;
22259 tree substed = NULL_TREE;
22260 if (saw_undeduced == 1)
22261 {
22262 /* First instatiate in template context, in case we still
22263 depend on undeduced template parameters. */
22264 ++processing_template_decl;
22265 substed = tsubst_template_arg (arg, full_targs, complain,
22266 NULL_TREE);
22267 --processing_template_decl;
22268 if (substed != error_mark_node
22269 && !uses_template_parms (substed))
22270 /* We replaced all the tparms, substitute again out of
22271 template context. */
22272 substed = NULL_TREE;
22273 }
22274 if (!substed)
22275 substed = tsubst_template_arg (arg, full_targs, complain,
22276 NULL_TREE);
22277
22278 if (!uses_template_parms (substed))
22279 arg = convert_template_argument (parm, substed, full_targs,
22280 complain, i, NULL_TREE);
22281 else if (saw_undeduced == 1)
22282 arg = NULL_TREE;
22283 else
22284 arg = error_mark_node;
22285 }
22286
22287 input_location = save_loc;
22288 *checks = get_deferred_access_checks ();
22289 pop_deferring_access_checks ();
22290
22291 if (arg == error_mark_node)
22292 return 1;
22293 else if (arg)
22294 {
22295 TREE_VEC_ELT (targs, i) = arg;
22296 /* The position of the first default template argument,
22297 is also the number of non-defaulted arguments in TARGS.
22298 Record that. */
22299 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22300 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22301 }
22302 }
22303
22304 if (saw_undeduced++ == 1)
22305 goto again;
22306 }
22307
22308 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22310
22311 return unify_success (explain_p);
22312 }
22313
22314 /* Subroutine of type_unification_real. Args are like the variables
22315 at the call site. ARG is an overloaded function (or template-id);
22316 we try deducing template args from each of the overloads, and if
22317 only one succeeds, we go with that. Modifies TARGS and returns
22318 true on success. */
22319
22320 static bool
22321 resolve_overloaded_unification (tree tparms,
22322 tree targs,
22323 tree parm,
22324 tree arg,
22325 unification_kind_t strict,
22326 int sub_strict,
22327 bool explain_p)
22328 {
22329 tree tempargs = copy_node (targs);
22330 int good = 0;
22331 tree goodfn = NULL_TREE;
22332 bool addr_p;
22333
22334 if (TREE_CODE (arg) == ADDR_EXPR)
22335 {
22336 arg = TREE_OPERAND (arg, 0);
22337 addr_p = true;
22338 }
22339 else
22340 addr_p = false;
22341
22342 if (TREE_CODE (arg) == COMPONENT_REF)
22343 /* Handle `&x' where `x' is some static or non-static member
22344 function name. */
22345 arg = TREE_OPERAND (arg, 1);
22346
22347 if (TREE_CODE (arg) == OFFSET_REF)
22348 arg = TREE_OPERAND (arg, 1);
22349
22350 /* Strip baselink information. */
22351 if (BASELINK_P (arg))
22352 arg = BASELINK_FUNCTIONS (arg);
22353
22354 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22355 {
22356 /* If we got some explicit template args, we need to plug them into
22357 the affected templates before we try to unify, in case the
22358 explicit args will completely resolve the templates in question. */
22359
22360 int ok = 0;
22361 tree expl_subargs = TREE_OPERAND (arg, 1);
22362 arg = TREE_OPERAND (arg, 0);
22363
22364 for (lkp_iterator iter (arg); iter; ++iter)
22365 {
22366 tree fn = *iter;
22367 tree subargs, elem;
22368
22369 if (TREE_CODE (fn) != TEMPLATE_DECL)
22370 continue;
22371
22372 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22373 expl_subargs, NULL_TREE, tf_none,
22374 /*require_all_args=*/true,
22375 /*use_default_args=*/true);
22376 if (subargs != error_mark_node
22377 && !any_dependent_template_arguments_p (subargs))
22378 {
22379 fn = instantiate_template (fn, subargs, tf_none);
22380 if (!constraints_satisfied_p (fn))
22381 continue;
22382 if (undeduced_auto_decl (fn))
22383 {
22384 /* Instantiate the function to deduce its return type. */
22385 ++function_depth;
22386 instantiate_decl (fn, /*defer*/false, /*class*/false);
22387 --function_depth;
22388 }
22389
22390 if (flag_noexcept_type)
22391 maybe_instantiate_noexcept (fn, tf_none);
22392
22393 elem = TREE_TYPE (fn);
22394 if (try_one_overload (tparms, targs, tempargs, parm,
22395 elem, strict, sub_strict, addr_p, explain_p)
22396 && (!goodfn || !same_type_p (goodfn, elem)))
22397 {
22398 goodfn = elem;
22399 ++good;
22400 }
22401 }
22402 else if (subargs)
22403 ++ok;
22404 }
22405 /* If no templates (or more than one) are fully resolved by the
22406 explicit arguments, this template-id is a non-deduced context; it
22407 could still be OK if we deduce all template arguments for the
22408 enclosing call through other arguments. */
22409 if (good != 1)
22410 good = ok;
22411 }
22412 else if (!OVL_P (arg))
22413 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22414 -- but the deduction does not succeed because the expression is
22415 not just the function on its own. */
22416 return false;
22417 else
22418 for (lkp_iterator iter (arg); iter; ++iter)
22419 {
22420 tree fn = *iter;
22421 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22422 strict, sub_strict, addr_p, explain_p)
22423 && (!goodfn || !decls_match (goodfn, fn)))
22424 {
22425 goodfn = fn;
22426 ++good;
22427 }
22428 }
22429
22430 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22431 to function or pointer to member function argument if the set of
22432 overloaded functions does not contain function templates and at most
22433 one of a set of overloaded functions provides a unique match.
22434
22435 So if we found multiple possibilities, we return success but don't
22436 deduce anything. */
22437
22438 if (good == 1)
22439 {
22440 int i = TREE_VEC_LENGTH (targs);
22441 for (; i--; )
22442 if (TREE_VEC_ELT (tempargs, i))
22443 {
22444 tree old = TREE_VEC_ELT (targs, i);
22445 tree new_ = TREE_VEC_ELT (tempargs, i);
22446 if (new_ && old && ARGUMENT_PACK_P (old)
22447 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22448 /* Don't forget explicit template arguments in a pack. */
22449 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22450 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22451 TREE_VEC_ELT (targs, i) = new_;
22452 }
22453 }
22454 if (good)
22455 return true;
22456
22457 return false;
22458 }
22459
22460 /* Core DR 115: In contexts where deduction is done and fails, or in
22461 contexts where deduction is not done, if a template argument list is
22462 specified and it, along with any default template arguments, identifies
22463 a single function template specialization, then the template-id is an
22464 lvalue for the function template specialization. */
22465
22466 tree
22467 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22468 {
22469 tree expr, offset, baselink;
22470 bool addr;
22471
22472 if (!type_unknown_p (orig_expr))
22473 return orig_expr;
22474
22475 expr = orig_expr;
22476 addr = false;
22477 offset = NULL_TREE;
22478 baselink = NULL_TREE;
22479
22480 if (TREE_CODE (expr) == ADDR_EXPR)
22481 {
22482 expr = TREE_OPERAND (expr, 0);
22483 addr = true;
22484 }
22485 if (TREE_CODE (expr) == OFFSET_REF)
22486 {
22487 offset = expr;
22488 expr = TREE_OPERAND (expr, 1);
22489 }
22490 if (BASELINK_P (expr))
22491 {
22492 baselink = expr;
22493 expr = BASELINK_FUNCTIONS (expr);
22494 }
22495
22496 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22497 {
22498 int good = 0;
22499 tree goodfn = NULL_TREE;
22500
22501 /* If we got some explicit template args, we need to plug them into
22502 the affected templates before we try to unify, in case the
22503 explicit args will completely resolve the templates in question. */
22504
22505 tree expl_subargs = TREE_OPERAND (expr, 1);
22506 tree arg = TREE_OPERAND (expr, 0);
22507 tree badfn = NULL_TREE;
22508 tree badargs = NULL_TREE;
22509
22510 for (lkp_iterator iter (arg); iter; ++iter)
22511 {
22512 tree fn = *iter;
22513 tree subargs, elem;
22514
22515 if (TREE_CODE (fn) != TEMPLATE_DECL)
22516 continue;
22517
22518 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22519 expl_subargs, NULL_TREE, tf_none,
22520 /*require_all_args=*/true,
22521 /*use_default_args=*/true);
22522 if (subargs != error_mark_node
22523 && !any_dependent_template_arguments_p (subargs))
22524 {
22525 elem = instantiate_template (fn, subargs, tf_none);
22526 if (elem == error_mark_node)
22527 {
22528 badfn = fn;
22529 badargs = subargs;
22530 }
22531 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22532 && constraints_satisfied_p (elem))
22533 {
22534 goodfn = elem;
22535 ++good;
22536 }
22537 }
22538 }
22539 if (good == 1)
22540 {
22541 mark_used (goodfn);
22542 expr = goodfn;
22543 if (baselink)
22544 expr = build_baselink (BASELINK_BINFO (baselink),
22545 BASELINK_ACCESS_BINFO (baselink),
22546 expr, BASELINK_OPTYPE (baselink));
22547 if (offset)
22548 {
22549 tree base
22550 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22551 expr = build_offset_ref (base, expr, addr, complain);
22552 }
22553 if (addr)
22554 expr = cp_build_addr_expr (expr, complain);
22555 return expr;
22556 }
22557 else if (good == 0 && badargs && (complain & tf_error))
22558 /* There were no good options and at least one bad one, so let the
22559 user know what the problem is. */
22560 instantiate_template (badfn, badargs, complain);
22561 }
22562 return orig_expr;
22563 }
22564
22565 /* As above, but error out if the expression remains overloaded. */
22566
22567 tree
22568 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22569 {
22570 exp = resolve_nondeduced_context (exp, complain);
22571 if (type_unknown_p (exp))
22572 {
22573 if (complain & tf_error)
22574 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22575 return error_mark_node;
22576 }
22577 return exp;
22578 }
22579
22580 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22581 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22582 different overloads deduce different arguments for a given parm.
22583 ADDR_P is true if the expression for which deduction is being
22584 performed was of the form "& fn" rather than simply "fn".
22585
22586 Returns 1 on success. */
22587
22588 static int
22589 try_one_overload (tree tparms,
22590 tree orig_targs,
22591 tree targs,
22592 tree parm,
22593 tree arg,
22594 unification_kind_t strict,
22595 int sub_strict,
22596 bool addr_p,
22597 bool explain_p)
22598 {
22599 int nargs;
22600 tree tempargs;
22601 int i;
22602
22603 if (arg == error_mark_node)
22604 return 0;
22605
22606 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22607 to function or pointer to member function argument if the set of
22608 overloaded functions does not contain function templates and at most
22609 one of a set of overloaded functions provides a unique match.
22610
22611 So if this is a template, just return success. */
22612
22613 if (uses_template_parms (arg))
22614 return 1;
22615
22616 if (TREE_CODE (arg) == METHOD_TYPE)
22617 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22618 else if (addr_p)
22619 arg = build_pointer_type (arg);
22620
22621 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22622
22623 /* We don't copy orig_targs for this because if we have already deduced
22624 some template args from previous args, unify would complain when we
22625 try to deduce a template parameter for the same argument, even though
22626 there isn't really a conflict. */
22627 nargs = TREE_VEC_LENGTH (targs);
22628 tempargs = make_tree_vec (nargs);
22629
22630 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22631 return 0;
22632
22633 /* First make sure we didn't deduce anything that conflicts with
22634 explicitly specified args. */
22635 for (i = nargs; i--; )
22636 {
22637 tree elt = TREE_VEC_ELT (tempargs, i);
22638 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22639
22640 if (!elt)
22641 /*NOP*/;
22642 else if (uses_template_parms (elt))
22643 /* Since we're unifying against ourselves, we will fill in
22644 template args used in the function parm list with our own
22645 template parms. Discard them. */
22646 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22647 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22648 {
22649 /* Check that the argument at each index of the deduced argument pack
22650 is equivalent to the corresponding explicitly specified argument.
22651 We may have deduced more arguments than were explicitly specified,
22652 and that's OK. */
22653
22654 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22655 that's wrong if we deduce the same argument pack from multiple
22656 function arguments: it's only incomplete the first time. */
22657
22658 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22659 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22660
22661 if (TREE_VEC_LENGTH (deduced_pack)
22662 < TREE_VEC_LENGTH (explicit_pack))
22663 return 0;
22664
22665 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22666 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22667 TREE_VEC_ELT (deduced_pack, j)))
22668 return 0;
22669 }
22670 else if (oldelt && !template_args_equal (oldelt, elt))
22671 return 0;
22672 }
22673
22674 for (i = nargs; i--; )
22675 {
22676 tree elt = TREE_VEC_ELT (tempargs, i);
22677
22678 if (elt)
22679 TREE_VEC_ELT (targs, i) = elt;
22680 }
22681
22682 return 1;
22683 }
22684
22685 /* PARM is a template class (perhaps with unbound template
22686 parameters). ARG is a fully instantiated type. If ARG can be
22687 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22688 TARGS are as for unify. */
22689
22690 static tree
22691 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22692 bool explain_p)
22693 {
22694 tree copy_of_targs;
22695
22696 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22697 return NULL_TREE;
22698 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22699 /* Matches anything. */;
22700 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22701 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22702 return NULL_TREE;
22703
22704 /* We need to make a new template argument vector for the call to
22705 unify. If we used TARGS, we'd clutter it up with the result of
22706 the attempted unification, even if this class didn't work out.
22707 We also don't want to commit ourselves to all the unifications
22708 we've already done, since unification is supposed to be done on
22709 an argument-by-argument basis. In other words, consider the
22710 following pathological case:
22711
22712 template <int I, int J, int K>
22713 struct S {};
22714
22715 template <int I, int J>
22716 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22717
22718 template <int I, int J, int K>
22719 void f(S<I, J, K>, S<I, I, I>);
22720
22721 void g() {
22722 S<0, 0, 0> s0;
22723 S<0, 1, 2> s2;
22724
22725 f(s0, s2);
22726 }
22727
22728 Now, by the time we consider the unification involving `s2', we
22729 already know that we must have `f<0, 0, 0>'. But, even though
22730 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22731 because there are two ways to unify base classes of S<0, 1, 2>
22732 with S<I, I, I>. If we kept the already deduced knowledge, we
22733 would reject the possibility I=1. */
22734 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22735
22736 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22737 {
22738 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22739 return NULL_TREE;
22740 return arg;
22741 }
22742
22743 /* If unification failed, we're done. */
22744 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22745 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22746 return NULL_TREE;
22747
22748 return arg;
22749 }
22750
22751 /* Given a template type PARM and a class type ARG, find the unique
22752 base type in ARG that is an instance of PARM. We do not examine
22753 ARG itself; only its base-classes. If there is not exactly one
22754 appropriate base class, return NULL_TREE. PARM may be the type of
22755 a partial specialization, as well as a plain template type. Used
22756 by unify. */
22757
22758 static enum template_base_result
22759 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22760 bool explain_p, tree *result)
22761 {
22762 tree rval = NULL_TREE;
22763 tree binfo;
22764
22765 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22766
22767 binfo = TYPE_BINFO (complete_type (arg));
22768 if (!binfo)
22769 {
22770 /* The type could not be completed. */
22771 *result = NULL_TREE;
22772 return tbr_incomplete_type;
22773 }
22774
22775 /* Walk in inheritance graph order. The search order is not
22776 important, and this avoids multiple walks of virtual bases. */
22777 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22778 {
22779 tree r = try_class_unification (tparms, targs, parm,
22780 BINFO_TYPE (binfo), explain_p);
22781
22782 if (r)
22783 {
22784 /* If there is more than one satisfactory baseclass, then:
22785
22786 [temp.deduct.call]
22787
22788 If they yield more than one possible deduced A, the type
22789 deduction fails.
22790
22791 applies. */
22792 if (rval && !same_type_p (r, rval))
22793 {
22794 /* [temp.deduct.call]/4.3: If there is a class C that is a
22795 (direct or indirect) base class of D and derived (directly or
22796 indirectly) from a class B and that would be a valid deduced
22797 A, the deduced A cannot be B or pointer to B, respectively. */
22798 if (DERIVED_FROM_P (r, rval))
22799 /* Ignore r. */
22800 continue;
22801 else if (DERIVED_FROM_P (rval, r))
22802 /* Ignore rval. */;
22803 else
22804 {
22805 *result = NULL_TREE;
22806 return tbr_ambiguous_baseclass;
22807 }
22808 }
22809
22810 rval = r;
22811 }
22812 }
22813
22814 *result = rval;
22815 return tbr_success;
22816 }
22817
22818 /* Returns the level of DECL, which declares a template parameter. */
22819
22820 static int
22821 template_decl_level (tree decl)
22822 {
22823 switch (TREE_CODE (decl))
22824 {
22825 case TYPE_DECL:
22826 case TEMPLATE_DECL:
22827 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22828
22829 case PARM_DECL:
22830 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22831
22832 default:
22833 gcc_unreachable ();
22834 }
22835 return 0;
22836 }
22837
22838 /* Decide whether ARG can be unified with PARM, considering only the
22839 cv-qualifiers of each type, given STRICT as documented for unify.
22840 Returns nonzero iff the unification is OK on that basis. */
22841
22842 static int
22843 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22844 {
22845 int arg_quals = cp_type_quals (arg);
22846 int parm_quals = cp_type_quals (parm);
22847
22848 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22849 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22850 {
22851 /* Although a CVR qualifier is ignored when being applied to a
22852 substituted template parameter ([8.3.2]/1 for example), that
22853 does not allow us to unify "const T" with "int&" because both
22854 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22855 It is ok when we're allowing additional CV qualifiers
22856 at the outer level [14.8.2.1]/3,1st bullet. */
22857 if ((TYPE_REF_P (arg)
22858 || FUNC_OR_METHOD_TYPE_P (arg))
22859 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22860 return 0;
22861
22862 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22863 && (parm_quals & TYPE_QUAL_RESTRICT))
22864 return 0;
22865 }
22866
22867 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22868 && (arg_quals & parm_quals) != parm_quals)
22869 return 0;
22870
22871 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22872 && (parm_quals & arg_quals) != arg_quals)
22873 return 0;
22874
22875 return 1;
22876 }
22877
22878 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22879 void
22880 template_parm_level_and_index (tree parm, int* level, int* index)
22881 {
22882 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22883 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22884 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22885 {
22886 *index = TEMPLATE_TYPE_IDX (parm);
22887 *level = TEMPLATE_TYPE_LEVEL (parm);
22888 }
22889 else
22890 {
22891 *index = TEMPLATE_PARM_IDX (parm);
22892 *level = TEMPLATE_PARM_LEVEL (parm);
22893 }
22894 }
22895
22896 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22897 do { \
22898 if (unify (TP, TA, P, A, S, EP)) \
22899 return 1; \
22900 } while (0)
22901
22902 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22903 expansion at the end of PACKED_PARMS. Returns 0 if the type
22904 deduction succeeds, 1 otherwise. STRICT is the same as in
22905 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22906 function call argument list. We'll need to adjust the arguments to make them
22907 types. SUBR tells us if this is from a recursive call to
22908 type_unification_real, or for comparing two template argument
22909 lists. */
22910
22911 static int
22912 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22913 tree packed_args, unification_kind_t strict,
22914 bool subr, bool explain_p)
22915 {
22916 tree parm
22917 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22918 tree pattern = PACK_EXPANSION_PATTERN (parm);
22919 tree pack, packs = NULL_TREE;
22920 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22921
22922 /* Add in any args remembered from an earlier partial instantiation. */
22923 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22924 int levels = TMPL_ARGS_DEPTH (targs);
22925
22926 packed_args = expand_template_argument_pack (packed_args);
22927
22928 int len = TREE_VEC_LENGTH (packed_args);
22929
22930 /* Determine the parameter packs we will be deducing from the
22931 pattern, and record their current deductions. */
22932 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22933 pack; pack = TREE_CHAIN (pack))
22934 {
22935 tree parm_pack = TREE_VALUE (pack);
22936 int idx, level;
22937
22938 /* Only template parameter packs can be deduced, not e.g. function
22939 parameter packs or __bases or __integer_pack. */
22940 if (!TEMPLATE_PARM_P (parm_pack))
22941 continue;
22942
22943 /* Determine the index and level of this parameter pack. */
22944 template_parm_level_and_index (parm_pack, &level, &idx);
22945 if (level < levels)
22946 continue;
22947
22948 /* Keep track of the parameter packs and their corresponding
22949 argument packs. */
22950 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22951 TREE_TYPE (packs) = make_tree_vec (len - start);
22952 }
22953
22954 /* Loop through all of the arguments that have not yet been
22955 unified and unify each with the pattern. */
22956 for (i = start; i < len; i++)
22957 {
22958 tree parm;
22959 bool any_explicit = false;
22960 tree arg = TREE_VEC_ELT (packed_args, i);
22961
22962 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22963 or the element of its argument pack at the current index if
22964 this argument was explicitly specified. */
22965 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22966 {
22967 int idx, level;
22968 tree arg, pargs;
22969 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22970
22971 arg = NULL_TREE;
22972 if (TREE_VALUE (pack)
22973 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22974 && (i - start < TREE_VEC_LENGTH (pargs)))
22975 {
22976 any_explicit = true;
22977 arg = TREE_VEC_ELT (pargs, i - start);
22978 }
22979 TMPL_ARG (targs, level, idx) = arg;
22980 }
22981
22982 /* If we had explicit template arguments, substitute them into the
22983 pattern before deduction. */
22984 if (any_explicit)
22985 {
22986 /* Some arguments might still be unspecified or dependent. */
22987 bool dependent;
22988 ++processing_template_decl;
22989 dependent = any_dependent_template_arguments_p (targs);
22990 if (!dependent)
22991 --processing_template_decl;
22992 parm = tsubst (pattern, targs,
22993 explain_p ? tf_warning_or_error : tf_none,
22994 NULL_TREE);
22995 if (dependent)
22996 --processing_template_decl;
22997 if (parm == error_mark_node)
22998 return 1;
22999 }
23000 else
23001 parm = pattern;
23002
23003 /* Unify the pattern with the current argument. */
23004 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23005 explain_p))
23006 return 1;
23007
23008 /* For each parameter pack, collect the deduced value. */
23009 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23010 {
23011 int idx, level;
23012 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23013
23014 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23015 TMPL_ARG (targs, level, idx);
23016 }
23017 }
23018
23019 /* Verify that the results of unification with the parameter packs
23020 produce results consistent with what we've seen before, and make
23021 the deduced argument packs available. */
23022 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23023 {
23024 tree old_pack = TREE_VALUE (pack);
23025 tree new_args = TREE_TYPE (pack);
23026 int i, len = TREE_VEC_LENGTH (new_args);
23027 int idx, level;
23028 bool nondeduced_p = false;
23029
23030 /* By default keep the original deduced argument pack.
23031 If necessary, more specific code is going to update the
23032 resulting deduced argument later down in this function. */
23033 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23034 TMPL_ARG (targs, level, idx) = old_pack;
23035
23036 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23037 actually deduce anything. */
23038 for (i = 0; i < len && !nondeduced_p; ++i)
23039 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23040 nondeduced_p = true;
23041 if (nondeduced_p)
23042 continue;
23043
23044 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23045 {
23046 /* If we had fewer function args than explicit template args,
23047 just use the explicits. */
23048 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23049 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23050 if (len < explicit_len)
23051 new_args = explicit_args;
23052 }
23053
23054 if (!old_pack)
23055 {
23056 tree result;
23057 /* Build the deduced *_ARGUMENT_PACK. */
23058 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23059 {
23060 result = make_node (NONTYPE_ARGUMENT_PACK);
23061 TREE_CONSTANT (result) = 1;
23062 }
23063 else
23064 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23065
23066 SET_ARGUMENT_PACK_ARGS (result, new_args);
23067
23068 /* Note the deduced argument packs for this parameter
23069 pack. */
23070 TMPL_ARG (targs, level, idx) = result;
23071 }
23072 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23073 && (ARGUMENT_PACK_ARGS (old_pack)
23074 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23075 {
23076 /* We only had the explicitly-provided arguments before, but
23077 now we have a complete set of arguments. */
23078 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23079
23080 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23081 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23082 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23083 }
23084 else
23085 {
23086 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23087 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23088 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23089 /* During template argument deduction for the aggregate deduction
23090 candidate, the number of elements in a trailing parameter pack
23091 is only deduced from the number of remaining function
23092 arguments if it is not otherwise deduced. */
23093 if (cxx_dialect >= cxx20
23094 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23095 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23096 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23097 if (!comp_template_args (old_args, new_args,
23098 &bad_old_arg, &bad_new_arg))
23099 /* Inconsistent unification of this parameter pack. */
23100 return unify_parameter_pack_inconsistent (explain_p,
23101 bad_old_arg,
23102 bad_new_arg);
23103 }
23104 }
23105
23106 return unify_success (explain_p);
23107 }
23108
23109 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23110 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23111 parameters and return value are as for unify. */
23112
23113 static int
23114 unify_array_domain (tree tparms, tree targs,
23115 tree parm_dom, tree arg_dom,
23116 bool explain_p)
23117 {
23118 tree parm_max;
23119 tree arg_max;
23120 bool parm_cst;
23121 bool arg_cst;
23122
23123 /* Our representation of array types uses "N - 1" as the
23124 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23125 not an integer constant. We cannot unify arbitrarily
23126 complex expressions, so we eliminate the MINUS_EXPRs
23127 here. */
23128 parm_max = TYPE_MAX_VALUE (parm_dom);
23129 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23130 if (!parm_cst)
23131 {
23132 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23133 parm_max = TREE_OPERAND (parm_max, 0);
23134 }
23135 arg_max = TYPE_MAX_VALUE (arg_dom);
23136 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23137 if (!arg_cst)
23138 {
23139 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23140 trying to unify the type of a variable with the type
23141 of a template parameter. For example:
23142
23143 template <unsigned int N>
23144 void f (char (&) [N]);
23145 int g();
23146 void h(int i) {
23147 char a[g(i)];
23148 f(a);
23149 }
23150
23151 Here, the type of the ARG will be "int [g(i)]", and
23152 may be a SAVE_EXPR, etc. */
23153 if (TREE_CODE (arg_max) != MINUS_EXPR)
23154 return unify_vla_arg (explain_p, arg_dom);
23155 arg_max = TREE_OPERAND (arg_max, 0);
23156 }
23157
23158 /* If only one of the bounds used a MINUS_EXPR, compensate
23159 by adding one to the other bound. */
23160 if (parm_cst && !arg_cst)
23161 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23162 integer_type_node,
23163 parm_max,
23164 integer_one_node);
23165 else if (arg_cst && !parm_cst)
23166 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23167 integer_type_node,
23168 arg_max,
23169 integer_one_node);
23170
23171 return unify (tparms, targs, parm_max, arg_max,
23172 UNIFY_ALLOW_INTEGER, explain_p);
23173 }
23174
23175 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23176
23177 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23178
23179 static pa_kind_t
23180 pa_kind (tree t)
23181 {
23182 if (PACK_EXPANSION_P (t))
23183 t = PACK_EXPANSION_PATTERN (t);
23184 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23185 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23186 || DECL_TYPE_TEMPLATE_P (t))
23187 return pa_tmpl;
23188 else if (TYPE_P (t))
23189 return pa_type;
23190 else
23191 return pa_expr;
23192 }
23193
23194 /* Deduce the value of template parameters. TPARMS is the (innermost)
23195 set of template parameters to a template. TARGS is the bindings
23196 for those template parameters, as determined thus far; TARGS may
23197 include template arguments for outer levels of template parameters
23198 as well. PARM is a parameter to a template function, or a
23199 subcomponent of that parameter; ARG is the corresponding argument.
23200 This function attempts to match PARM with ARG in a manner
23201 consistent with the existing assignments in TARGS. If more values
23202 are deduced, then TARGS is updated.
23203
23204 Returns 0 if the type deduction succeeds, 1 otherwise. The
23205 parameter STRICT is a bitwise or of the following flags:
23206
23207 UNIFY_ALLOW_NONE:
23208 Require an exact match between PARM and ARG.
23209 UNIFY_ALLOW_MORE_CV_QUAL:
23210 Allow the deduced ARG to be more cv-qualified (by qualification
23211 conversion) than ARG.
23212 UNIFY_ALLOW_LESS_CV_QUAL:
23213 Allow the deduced ARG to be less cv-qualified than ARG.
23214 UNIFY_ALLOW_DERIVED:
23215 Allow the deduced ARG to be a template base class of ARG,
23216 or a pointer to a template base class of the type pointed to by
23217 ARG.
23218 UNIFY_ALLOW_INTEGER:
23219 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23220 case for more information.
23221 UNIFY_ALLOW_OUTER_LEVEL:
23222 This is the outermost level of a deduction. Used to determine validity
23223 of qualification conversions. A valid qualification conversion must
23224 have const qualified pointers leading up to the inner type which
23225 requires additional CV quals, except at the outer level, where const
23226 is not required [conv.qual]. It would be normal to set this flag in
23227 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23228 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23229 This is the outermost level of a deduction, and PARM can be more CV
23230 qualified at this point.
23231 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23232 This is the outermost level of a deduction, and PARM can be less CV
23233 qualified at this point. */
23234
23235 static int
23236 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23237 bool explain_p)
23238 {
23239 int idx;
23240 tree targ;
23241 tree tparm;
23242 int strict_in = strict;
23243 tsubst_flags_t complain = (explain_p
23244 ? tf_warning_or_error
23245 : tf_none);
23246
23247 /* I don't think this will do the right thing with respect to types.
23248 But the only case I've seen it in so far has been array bounds, where
23249 signedness is the only information lost, and I think that will be
23250 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23251 finish_id_expression_1, and are also OK. */
23252 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23253 parm = TREE_OPERAND (parm, 0);
23254
23255 if (arg == error_mark_node)
23256 return unify_invalid (explain_p);
23257 if (arg == unknown_type_node
23258 || arg == init_list_type_node)
23259 /* We can't deduce anything from this, but we might get all the
23260 template args from other function args. */
23261 return unify_success (explain_p);
23262
23263 if (parm == any_targ_node || arg == any_targ_node)
23264 return unify_success (explain_p);
23265
23266 /* If PARM uses template parameters, then we can't bail out here,
23267 even if ARG == PARM, since we won't record unifications for the
23268 template parameters. We might need them if we're trying to
23269 figure out which of two things is more specialized. */
23270 if (arg == parm && !uses_template_parms (parm))
23271 return unify_success (explain_p);
23272
23273 /* Handle init lists early, so the rest of the function can assume
23274 we're dealing with a type. */
23275 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23276 {
23277 tree elt, elttype;
23278 unsigned i;
23279 tree orig_parm = parm;
23280
23281 if (!is_std_init_list (parm)
23282 && TREE_CODE (parm) != ARRAY_TYPE)
23283 /* We can only deduce from an initializer list argument if the
23284 parameter is std::initializer_list or an array; otherwise this
23285 is a non-deduced context. */
23286 return unify_success (explain_p);
23287
23288 if (TREE_CODE (parm) == ARRAY_TYPE)
23289 elttype = TREE_TYPE (parm);
23290 else
23291 {
23292 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23293 /* Deduction is defined in terms of a single type, so just punt
23294 on the (bizarre) std::initializer_list<T...>. */
23295 if (PACK_EXPANSION_P (elttype))
23296 return unify_success (explain_p);
23297 }
23298
23299 if (strict != DEDUCE_EXACT
23300 && TYPE_P (elttype)
23301 && !uses_deducible_template_parms (elttype))
23302 /* If ELTTYPE has no deducible template parms, skip deduction from
23303 the list elements. */;
23304 else
23305 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23306 {
23307 int elt_strict = strict;
23308
23309 if (elt == error_mark_node)
23310 return unify_invalid (explain_p);
23311
23312 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23313 {
23314 tree type = TREE_TYPE (elt);
23315 if (type == error_mark_node)
23316 return unify_invalid (explain_p);
23317 /* It should only be possible to get here for a call. */
23318 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23319 elt_strict |= maybe_adjust_types_for_deduction
23320 (DEDUCE_CALL, &elttype, &type, elt);
23321 elt = type;
23322 }
23323
23324 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23325 explain_p);
23326 }
23327
23328 if (TREE_CODE (parm) == ARRAY_TYPE
23329 && deducible_array_bound (TYPE_DOMAIN (parm)))
23330 {
23331 /* Also deduce from the length of the initializer list. */
23332 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23333 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23334 if (idx == error_mark_node)
23335 return unify_invalid (explain_p);
23336 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23337 idx, explain_p);
23338 }
23339
23340 /* If the std::initializer_list<T> deduction worked, replace the
23341 deduced A with std::initializer_list<A>. */
23342 if (orig_parm != parm)
23343 {
23344 idx = TEMPLATE_TYPE_IDX (orig_parm);
23345 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23346 targ = listify (targ);
23347 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23348 }
23349 return unify_success (explain_p);
23350 }
23351
23352 /* If parm and arg aren't the same kind of thing (template, type, or
23353 expression), fail early. */
23354 if (pa_kind (parm) != pa_kind (arg))
23355 return unify_invalid (explain_p);
23356
23357 /* Immediately reject some pairs that won't unify because of
23358 cv-qualification mismatches. */
23359 if (TREE_CODE (arg) == TREE_CODE (parm)
23360 && TYPE_P (arg)
23361 /* It is the elements of the array which hold the cv quals of an array
23362 type, and the elements might be template type parms. We'll check
23363 when we recurse. */
23364 && TREE_CODE (arg) != ARRAY_TYPE
23365 /* We check the cv-qualifiers when unifying with template type
23366 parameters below. We want to allow ARG `const T' to unify with
23367 PARM `T' for example, when computing which of two templates
23368 is more specialized, for example. */
23369 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23370 && !check_cv_quals_for_unify (strict_in, arg, parm))
23371 return unify_cv_qual_mismatch (explain_p, parm, arg);
23372
23373 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23374 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23375 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23376 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23377 strict &= ~UNIFY_ALLOW_DERIVED;
23378 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23379 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23380
23381 switch (TREE_CODE (parm))
23382 {
23383 case TYPENAME_TYPE:
23384 case SCOPE_REF:
23385 case UNBOUND_CLASS_TEMPLATE:
23386 /* In a type which contains a nested-name-specifier, template
23387 argument values cannot be deduced for template parameters used
23388 within the nested-name-specifier. */
23389 return unify_success (explain_p);
23390
23391 case TEMPLATE_TYPE_PARM:
23392 case TEMPLATE_TEMPLATE_PARM:
23393 case BOUND_TEMPLATE_TEMPLATE_PARM:
23394 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23395 if (error_operand_p (tparm))
23396 return unify_invalid (explain_p);
23397
23398 if (TEMPLATE_TYPE_LEVEL (parm)
23399 != template_decl_level (tparm))
23400 /* The PARM is not one we're trying to unify. Just check
23401 to see if it matches ARG. */
23402 {
23403 if (TREE_CODE (arg) == TREE_CODE (parm)
23404 && (is_auto (parm) ? is_auto (arg)
23405 : same_type_p (parm, arg)))
23406 return unify_success (explain_p);
23407 else
23408 return unify_type_mismatch (explain_p, parm, arg);
23409 }
23410 idx = TEMPLATE_TYPE_IDX (parm);
23411 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23412 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23413 if (error_operand_p (tparm))
23414 return unify_invalid (explain_p);
23415
23416 /* Check for mixed types and values. */
23417 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23418 && TREE_CODE (tparm) != TYPE_DECL)
23419 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23420 && TREE_CODE (tparm) != TEMPLATE_DECL))
23421 gcc_unreachable ();
23422
23423 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23424 {
23425 if ((strict_in & UNIFY_ALLOW_DERIVED)
23426 && CLASS_TYPE_P (arg))
23427 {
23428 /* First try to match ARG directly. */
23429 tree t = try_class_unification (tparms, targs, parm, arg,
23430 explain_p);
23431 if (!t)
23432 {
23433 /* Otherwise, look for a suitable base of ARG, as below. */
23434 enum template_base_result r;
23435 r = get_template_base (tparms, targs, parm, arg,
23436 explain_p, &t);
23437 if (!t)
23438 return unify_no_common_base (explain_p, r, parm, arg);
23439 arg = t;
23440 }
23441 }
23442 /* ARG must be constructed from a template class or a template
23443 template parameter. */
23444 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23445 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23446 return unify_template_deduction_failure (explain_p, parm, arg);
23447
23448 /* Deduce arguments T, i from TT<T> or TT<i>. */
23449 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23450 return 1;
23451
23452 arg = TYPE_TI_TEMPLATE (arg);
23453
23454 /* Fall through to deduce template name. */
23455 }
23456
23457 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23458 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23459 {
23460 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23461
23462 /* Simple cases: Value already set, does match or doesn't. */
23463 if (targ != NULL_TREE && template_args_equal (targ, arg))
23464 return unify_success (explain_p);
23465 else if (targ)
23466 return unify_inconsistency (explain_p, parm, targ, arg);
23467 }
23468 else
23469 {
23470 /* If PARM is `const T' and ARG is only `int', we don't have
23471 a match unless we are allowing additional qualification.
23472 If ARG is `const int' and PARM is just `T' that's OK;
23473 that binds `const int' to `T'. */
23474 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23475 arg, parm))
23476 return unify_cv_qual_mismatch (explain_p, parm, arg);
23477
23478 /* Consider the case where ARG is `const volatile int' and
23479 PARM is `const T'. Then, T should be `volatile int'. */
23480 arg = cp_build_qualified_type_real
23481 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23482 if (arg == error_mark_node)
23483 return unify_invalid (explain_p);
23484
23485 /* Simple cases: Value already set, does match or doesn't. */
23486 if (targ != NULL_TREE && same_type_p (targ, arg))
23487 return unify_success (explain_p);
23488 else if (targ)
23489 return unify_inconsistency (explain_p, parm, targ, arg);
23490
23491 /* Make sure that ARG is not a variable-sized array. (Note
23492 that were talking about variable-sized arrays (like
23493 `int[n]'), rather than arrays of unknown size (like
23494 `int[]').) We'll get very confused by such a type since
23495 the bound of the array is not constant, and therefore
23496 not mangleable. Besides, such types are not allowed in
23497 ISO C++, so we can do as we please here. We do allow
23498 them for 'auto' deduction, since that isn't ABI-exposed. */
23499 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23500 return unify_vla_arg (explain_p, arg);
23501
23502 /* Strip typedefs as in convert_template_argument. */
23503 arg = canonicalize_type_argument (arg, tf_none);
23504 }
23505
23506 /* If ARG is a parameter pack or an expansion, we cannot unify
23507 against it unless PARM is also a parameter pack. */
23508 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23509 && !template_parameter_pack_p (parm))
23510 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23511
23512 /* If the argument deduction results is a METHOD_TYPE,
23513 then there is a problem.
23514 METHOD_TYPE doesn't map to any real C++ type the result of
23515 the deduction cannot be of that type. */
23516 if (TREE_CODE (arg) == METHOD_TYPE)
23517 return unify_method_type_error (explain_p, arg);
23518
23519 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23520 return unify_success (explain_p);
23521
23522 case TEMPLATE_PARM_INDEX:
23523 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23524 if (error_operand_p (tparm))
23525 return unify_invalid (explain_p);
23526
23527 if (TEMPLATE_PARM_LEVEL (parm)
23528 != template_decl_level (tparm))
23529 {
23530 /* The PARM is not one we're trying to unify. Just check
23531 to see if it matches ARG. */
23532 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23533 && cp_tree_equal (parm, arg));
23534 if (result)
23535 unify_expression_unequal (explain_p, parm, arg);
23536 return result;
23537 }
23538
23539 idx = TEMPLATE_PARM_IDX (parm);
23540 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23541
23542 if (targ)
23543 {
23544 if ((strict & UNIFY_ALLOW_INTEGER)
23545 && TREE_TYPE (targ) && TREE_TYPE (arg)
23546 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23547 /* We're deducing from an array bound, the type doesn't matter. */
23548 arg = fold_convert (TREE_TYPE (targ), arg);
23549 int x = !cp_tree_equal (targ, arg);
23550 if (x)
23551 unify_inconsistency (explain_p, parm, targ, arg);
23552 return x;
23553 }
23554
23555 /* [temp.deduct.type] If, in the declaration of a function template
23556 with a non-type template-parameter, the non-type
23557 template-parameter is used in an expression in the function
23558 parameter-list and, if the corresponding template-argument is
23559 deduced, the template-argument type shall match the type of the
23560 template-parameter exactly, except that a template-argument
23561 deduced from an array bound may be of any integral type.
23562 The non-type parameter might use already deduced type parameters. */
23563 tparm = TREE_TYPE (parm);
23564 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23565 /* We don't have enough levels of args to do any substitution. This
23566 can happen in the context of -fnew-ttp-matching. */;
23567 else
23568 {
23569 ++processing_template_decl;
23570 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23571 --processing_template_decl;
23572
23573 if (tree a = type_uses_auto (tparm))
23574 {
23575 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23576 if (tparm == error_mark_node)
23577 return 1;
23578 }
23579 }
23580
23581 if (!TREE_TYPE (arg))
23582 /* Template-parameter dependent expression. Just accept it for now.
23583 It will later be processed in convert_template_argument. */
23584 ;
23585 else if (same_type_ignoring_top_level_qualifiers_p
23586 (non_reference (TREE_TYPE (arg)),
23587 non_reference (tparm)))
23588 /* OK. Ignore top-level quals here because a class-type template
23589 parameter object is const. */;
23590 else if ((strict & UNIFY_ALLOW_INTEGER)
23591 && CP_INTEGRAL_TYPE_P (tparm))
23592 /* Convert the ARG to the type of PARM; the deduced non-type
23593 template argument must exactly match the types of the
23594 corresponding parameter. */
23595 arg = fold (build_nop (tparm, arg));
23596 else if (uses_template_parms (tparm))
23597 {
23598 /* We haven't deduced the type of this parameter yet. */
23599 if (cxx_dialect >= cxx17
23600 /* We deduce from array bounds in try_array_deduction. */
23601 && !(strict & UNIFY_ALLOW_INTEGER)
23602 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
23603 {
23604 /* Deduce it from the non-type argument. */
23605 tree atype = TREE_TYPE (arg);
23606 RECUR_AND_CHECK_FAILURE (tparms, targs,
23607 tparm, atype,
23608 UNIFY_ALLOW_NONE, explain_p);
23609 /* Now check whether the type of this parameter is still
23610 dependent, and give up if so. */
23611 ++processing_template_decl;
23612 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23613 --processing_template_decl;
23614 if (uses_template_parms (tparm))
23615 return unify_success (explain_p);
23616 }
23617 else
23618 /* Try again later. */
23619 return unify_success (explain_p);
23620 }
23621 else
23622 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23623
23624 /* If ARG is a parameter pack or an expansion, we cannot unify
23625 against it unless PARM is also a parameter pack. */
23626 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23627 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23628 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23629
23630 {
23631 bool removed_attr = false;
23632 arg = strip_typedefs_expr (arg, &removed_attr);
23633 }
23634 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23635 return unify_success (explain_p);
23636
23637 case PTRMEM_CST:
23638 {
23639 /* A pointer-to-member constant can be unified only with
23640 another constant. */
23641 if (TREE_CODE (arg) != PTRMEM_CST)
23642 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23643
23644 /* Just unify the class member. It would be useless (and possibly
23645 wrong, depending on the strict flags) to unify also
23646 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23647 arg refer to the same variable, even if through different
23648 classes. For instance:
23649
23650 struct A { int x; };
23651 struct B : A { };
23652
23653 Unification of &A::x and &B::x must succeed. */
23654 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23655 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23656 }
23657
23658 case POINTER_TYPE:
23659 {
23660 if (!TYPE_PTR_P (arg))
23661 return unify_type_mismatch (explain_p, parm, arg);
23662
23663 /* [temp.deduct.call]
23664
23665 A can be another pointer or pointer to member type that can
23666 be converted to the deduced A via a qualification
23667 conversion (_conv.qual_).
23668
23669 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23670 This will allow for additional cv-qualification of the
23671 pointed-to types if appropriate. */
23672
23673 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23674 /* The derived-to-base conversion only persists through one
23675 level of pointers. */
23676 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23677
23678 return unify (tparms, targs, TREE_TYPE (parm),
23679 TREE_TYPE (arg), strict, explain_p);
23680 }
23681
23682 case REFERENCE_TYPE:
23683 if (!TYPE_REF_P (arg))
23684 return unify_type_mismatch (explain_p, parm, arg);
23685 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23686 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23687
23688 case ARRAY_TYPE:
23689 if (TREE_CODE (arg) != ARRAY_TYPE)
23690 return unify_type_mismatch (explain_p, parm, arg);
23691 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23692 != (TYPE_DOMAIN (arg) == NULL_TREE))
23693 return unify_type_mismatch (explain_p, parm, arg);
23694 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23695 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23696 if (TYPE_DOMAIN (parm) != NULL_TREE)
23697 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23698 TYPE_DOMAIN (arg), explain_p);
23699 return unify_success (explain_p);
23700
23701 case REAL_TYPE:
23702 case COMPLEX_TYPE:
23703 case VECTOR_TYPE:
23704 case INTEGER_TYPE:
23705 case BOOLEAN_TYPE:
23706 case ENUMERAL_TYPE:
23707 case VOID_TYPE:
23708 case OPAQUE_TYPE:
23709 case NULLPTR_TYPE:
23710 if (TREE_CODE (arg) != TREE_CODE (parm))
23711 return unify_type_mismatch (explain_p, parm, arg);
23712
23713 /* We have already checked cv-qualification at the top of the
23714 function. */
23715 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23716 return unify_type_mismatch (explain_p, parm, arg);
23717
23718 /* As far as unification is concerned, this wins. Later checks
23719 will invalidate it if necessary. */
23720 return unify_success (explain_p);
23721
23722 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23723 /* Type INTEGER_CST can come from ordinary constant template args. */
23724 case INTEGER_CST:
23725 while (CONVERT_EXPR_P (arg))
23726 arg = TREE_OPERAND (arg, 0);
23727
23728 if (TREE_CODE (arg) != INTEGER_CST)
23729 return unify_template_argument_mismatch (explain_p, parm, arg);
23730 return (tree_int_cst_equal (parm, arg)
23731 ? unify_success (explain_p)
23732 : unify_template_argument_mismatch (explain_p, parm, arg));
23733
23734 case TREE_VEC:
23735 {
23736 int i, len, argslen;
23737 int parm_variadic_p = 0;
23738
23739 if (TREE_CODE (arg) != TREE_VEC)
23740 return unify_template_argument_mismatch (explain_p, parm, arg);
23741
23742 len = TREE_VEC_LENGTH (parm);
23743 argslen = TREE_VEC_LENGTH (arg);
23744
23745 /* Check for pack expansions in the parameters. */
23746 for (i = 0; i < len; ++i)
23747 {
23748 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23749 {
23750 if (i == len - 1)
23751 /* We can unify against something with a trailing
23752 parameter pack. */
23753 parm_variadic_p = 1;
23754 else
23755 /* [temp.deduct.type]/9: If the template argument list of
23756 P contains a pack expansion that is not the last
23757 template argument, the entire template argument list
23758 is a non-deduced context. */
23759 return unify_success (explain_p);
23760 }
23761 }
23762
23763 /* If we don't have enough arguments to satisfy the parameters
23764 (not counting the pack expression at the end), or we have
23765 too many arguments for a parameter list that doesn't end in
23766 a pack expression, we can't unify. */
23767 if (parm_variadic_p
23768 ? argslen < len - parm_variadic_p
23769 : argslen != len)
23770 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23771
23772 /* Unify all of the parameters that precede the (optional)
23773 pack expression. */
23774 for (i = 0; i < len - parm_variadic_p; ++i)
23775 {
23776 RECUR_AND_CHECK_FAILURE (tparms, targs,
23777 TREE_VEC_ELT (parm, i),
23778 TREE_VEC_ELT (arg, i),
23779 UNIFY_ALLOW_NONE, explain_p);
23780 }
23781 if (parm_variadic_p)
23782 return unify_pack_expansion (tparms, targs, parm, arg,
23783 DEDUCE_EXACT,
23784 /*subr=*/true, explain_p);
23785 return unify_success (explain_p);
23786 }
23787
23788 case RECORD_TYPE:
23789 case UNION_TYPE:
23790 if (TREE_CODE (arg) != TREE_CODE (parm))
23791 return unify_type_mismatch (explain_p, parm, arg);
23792
23793 if (TYPE_PTRMEMFUNC_P (parm))
23794 {
23795 if (!TYPE_PTRMEMFUNC_P (arg))
23796 return unify_type_mismatch (explain_p, parm, arg);
23797
23798 return unify (tparms, targs,
23799 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23800 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23801 strict, explain_p);
23802 }
23803 else if (TYPE_PTRMEMFUNC_P (arg))
23804 return unify_type_mismatch (explain_p, parm, arg);
23805
23806 if (CLASSTYPE_TEMPLATE_INFO (parm))
23807 {
23808 tree t = NULL_TREE;
23809
23810 if (strict_in & UNIFY_ALLOW_DERIVED)
23811 {
23812 /* First, we try to unify the PARM and ARG directly. */
23813 t = try_class_unification (tparms, targs,
23814 parm, arg, explain_p);
23815
23816 if (!t)
23817 {
23818 /* Fallback to the special case allowed in
23819 [temp.deduct.call]:
23820
23821 If P is a class, and P has the form
23822 template-id, then A can be a derived class of
23823 the deduced A. Likewise, if P is a pointer to
23824 a class of the form template-id, A can be a
23825 pointer to a derived class pointed to by the
23826 deduced A. */
23827 enum template_base_result r;
23828 r = get_template_base (tparms, targs, parm, arg,
23829 explain_p, &t);
23830
23831 if (!t)
23832 {
23833 /* Don't give the derived diagnostic if we're
23834 already dealing with the same template. */
23835 bool same_template
23836 = (CLASSTYPE_TEMPLATE_INFO (arg)
23837 && (CLASSTYPE_TI_TEMPLATE (parm)
23838 == CLASSTYPE_TI_TEMPLATE (arg)));
23839 return unify_no_common_base (explain_p && !same_template,
23840 r, parm, arg);
23841 }
23842 }
23843 }
23844 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23845 && (CLASSTYPE_TI_TEMPLATE (parm)
23846 == CLASSTYPE_TI_TEMPLATE (arg)))
23847 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23848 Then, we should unify `int' and `U'. */
23849 t = arg;
23850 else
23851 /* There's no chance of unification succeeding. */
23852 return unify_type_mismatch (explain_p, parm, arg);
23853
23854 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23855 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23856 }
23857 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23858 return unify_type_mismatch (explain_p, parm, arg);
23859 return unify_success (explain_p);
23860
23861 case METHOD_TYPE:
23862 case FUNCTION_TYPE:
23863 {
23864 unsigned int nargs;
23865 tree *args;
23866 tree a;
23867 unsigned int i;
23868
23869 if (TREE_CODE (arg) != TREE_CODE (parm))
23870 return unify_type_mismatch (explain_p, parm, arg);
23871
23872 /* CV qualifications for methods can never be deduced, they must
23873 match exactly. We need to check them explicitly here,
23874 because type_unification_real treats them as any other
23875 cv-qualified parameter. */
23876 if (TREE_CODE (parm) == METHOD_TYPE
23877 && (!check_cv_quals_for_unify
23878 (UNIFY_ALLOW_NONE,
23879 class_of_this_parm (arg),
23880 class_of_this_parm (parm))))
23881 return unify_cv_qual_mismatch (explain_p, parm, arg);
23882 if (TREE_CODE (arg) == FUNCTION_TYPE
23883 && type_memfn_quals (parm) != type_memfn_quals (arg))
23884 return unify_cv_qual_mismatch (explain_p, parm, arg);
23885 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23886 return unify_type_mismatch (explain_p, parm, arg);
23887
23888 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23889 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23890
23891 nargs = list_length (TYPE_ARG_TYPES (arg));
23892 args = XALLOCAVEC (tree, nargs);
23893 for (a = TYPE_ARG_TYPES (arg), i = 0;
23894 a != NULL_TREE && a != void_list_node;
23895 a = TREE_CHAIN (a), ++i)
23896 args[i] = TREE_VALUE (a);
23897 nargs = i;
23898
23899 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23900 args, nargs, 1, DEDUCE_EXACT,
23901 NULL, explain_p))
23902 return 1;
23903
23904 if (flag_noexcept_type)
23905 {
23906 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23907 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23908 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23909 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23910 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23911 && uses_template_parms (TREE_PURPOSE (pspec)))
23912 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23913 TREE_PURPOSE (aspec),
23914 UNIFY_ALLOW_NONE, explain_p);
23915 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23916 return unify_type_mismatch (explain_p, parm, arg);
23917 }
23918
23919 return 0;
23920 }
23921
23922 case OFFSET_TYPE:
23923 /* Unify a pointer to member with a pointer to member function, which
23924 deduces the type of the member as a function type. */
23925 if (TYPE_PTRMEMFUNC_P (arg))
23926 {
23927 /* Check top-level cv qualifiers */
23928 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23929 return unify_cv_qual_mismatch (explain_p, parm, arg);
23930
23931 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23932 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23933 UNIFY_ALLOW_NONE, explain_p);
23934
23935 /* Determine the type of the function we are unifying against. */
23936 tree fntype = static_fn_type (arg);
23937
23938 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23939 }
23940
23941 if (TREE_CODE (arg) != OFFSET_TYPE)
23942 return unify_type_mismatch (explain_p, parm, arg);
23943 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23944 TYPE_OFFSET_BASETYPE (arg),
23945 UNIFY_ALLOW_NONE, explain_p);
23946 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23947 strict, explain_p);
23948
23949 case CONST_DECL:
23950 if (DECL_TEMPLATE_PARM_P (parm))
23951 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23952 if (arg != scalar_constant_value (parm))
23953 return unify_template_argument_mismatch (explain_p, parm, arg);
23954 return unify_success (explain_p);
23955
23956 case FIELD_DECL:
23957 case TEMPLATE_DECL:
23958 /* Matched cases are handled by the ARG == PARM test above. */
23959 return unify_template_argument_mismatch (explain_p, parm, arg);
23960
23961 case VAR_DECL:
23962 /* We might get a variable as a non-type template argument in parm if the
23963 corresponding parameter is type-dependent. Make any necessary
23964 adjustments based on whether arg is a reference. */
23965 if (CONSTANT_CLASS_P (arg))
23966 parm = fold_non_dependent_expr (parm, complain);
23967 else if (REFERENCE_REF_P (arg))
23968 {
23969 tree sub = TREE_OPERAND (arg, 0);
23970 STRIP_NOPS (sub);
23971 if (TREE_CODE (sub) == ADDR_EXPR)
23972 arg = TREE_OPERAND (sub, 0);
23973 }
23974 /* Now use the normal expression code to check whether they match. */
23975 goto expr;
23976
23977 case TYPE_ARGUMENT_PACK:
23978 case NONTYPE_ARGUMENT_PACK:
23979 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23980 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23981
23982 case TYPEOF_TYPE:
23983 case DECLTYPE_TYPE:
23984 case UNDERLYING_TYPE:
23985 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23986 or UNDERLYING_TYPE nodes. */
23987 return unify_success (explain_p);
23988
23989 case ERROR_MARK:
23990 /* Unification fails if we hit an error node. */
23991 return unify_invalid (explain_p);
23992
23993 case INDIRECT_REF:
23994 if (REFERENCE_REF_P (parm))
23995 {
23996 bool pexp = PACK_EXPANSION_P (arg);
23997 if (pexp)
23998 arg = PACK_EXPANSION_PATTERN (arg);
23999 if (REFERENCE_REF_P (arg))
24000 arg = TREE_OPERAND (arg, 0);
24001 if (pexp)
24002 arg = make_pack_expansion (arg, complain);
24003 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24004 strict, explain_p);
24005 }
24006 /* FALLTHRU */
24007
24008 default:
24009 /* An unresolved overload is a nondeduced context. */
24010 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24011 return unify_success (explain_p);
24012 gcc_assert (EXPR_P (parm)
24013 || COMPOUND_LITERAL_P (parm)
24014 || TREE_CODE (parm) == TRAIT_EXPR);
24015 expr:
24016 /* We must be looking at an expression. This can happen with
24017 something like:
24018
24019 template <int I>
24020 void foo(S<I>, S<I + 2>);
24021
24022 or
24023
24024 template<typename T>
24025 void foo(A<T, T{}>);
24026
24027 This is a "non-deduced context":
24028
24029 [deduct.type]
24030
24031 The non-deduced contexts are:
24032
24033 --A non-type template argument or an array bound in which
24034 a subexpression references a template parameter.
24035
24036 In these cases, we assume deduction succeeded, but don't
24037 actually infer any unifications. */
24038
24039 if (!uses_template_parms (parm)
24040 && !template_args_equal (parm, arg))
24041 return unify_expression_unequal (explain_p, parm, arg);
24042 else
24043 return unify_success (explain_p);
24044 }
24045 }
24046 #undef RECUR_AND_CHECK_FAILURE
24047 \f
24048 /* Note that DECL can be defined in this translation unit, if
24049 required. */
24050
24051 static void
24052 mark_definable (tree decl)
24053 {
24054 tree clone;
24055 DECL_NOT_REALLY_EXTERN (decl) = 1;
24056 FOR_EACH_CLONE (clone, decl)
24057 DECL_NOT_REALLY_EXTERN (clone) = 1;
24058 }
24059
24060 /* Called if RESULT is explicitly instantiated, or is a member of an
24061 explicitly instantiated class. */
24062
24063 void
24064 mark_decl_instantiated (tree result, int extern_p)
24065 {
24066 SET_DECL_EXPLICIT_INSTANTIATION (result);
24067
24068 /* If this entity has already been written out, it's too late to
24069 make any modifications. */
24070 if (TREE_ASM_WRITTEN (result))
24071 return;
24072
24073 /* For anonymous namespace we don't need to do anything. */
24074 if (decl_anon_ns_mem_p (result))
24075 {
24076 gcc_assert (!TREE_PUBLIC (result));
24077 return;
24078 }
24079
24080 if (TREE_CODE (result) != FUNCTION_DECL)
24081 /* The TREE_PUBLIC flag for function declarations will have been
24082 set correctly by tsubst. */
24083 TREE_PUBLIC (result) = 1;
24084
24085 /* This might have been set by an earlier implicit instantiation. */
24086 DECL_COMDAT (result) = 0;
24087
24088 if (extern_p)
24089 DECL_NOT_REALLY_EXTERN (result) = 0;
24090 else
24091 {
24092 mark_definable (result);
24093 mark_needed (result);
24094 /* Always make artificials weak. */
24095 if (DECL_ARTIFICIAL (result) && flag_weak)
24096 comdat_linkage (result);
24097 /* For WIN32 we also want to put explicit instantiations in
24098 linkonce sections. */
24099 else if (TREE_PUBLIC (result))
24100 maybe_make_one_only (result);
24101 if (TREE_CODE (result) == FUNCTION_DECL
24102 && DECL_TEMPLATE_INSTANTIATED (result))
24103 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24104 since start_preparsed_function wouldn't have if we had an earlier
24105 extern explicit instantiation. */
24106 DECL_EXTERNAL (result) = 0;
24107 }
24108
24109 /* If EXTERN_P, then this function will not be emitted -- unless
24110 followed by an explicit instantiation, at which point its linkage
24111 will be adjusted. If !EXTERN_P, then this function will be
24112 emitted here. In neither circumstance do we want
24113 import_export_decl to adjust the linkage. */
24114 DECL_INTERFACE_KNOWN (result) = 1;
24115 }
24116
24117 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24118 important template arguments. If any are missing, we check whether
24119 they're important by using error_mark_node for substituting into any
24120 args that were used for partial ordering (the ones between ARGS and END)
24121 and seeing if it bubbles up. */
24122
24123 static bool
24124 check_undeduced_parms (tree targs, tree args, tree end)
24125 {
24126 bool found = false;
24127 int i;
24128 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24129 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24130 {
24131 found = true;
24132 TREE_VEC_ELT (targs, i) = error_mark_node;
24133 }
24134 if (found)
24135 {
24136 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24137 if (substed == error_mark_node)
24138 return true;
24139 }
24140 return false;
24141 }
24142
24143 /* Given two function templates PAT1 and PAT2, return:
24144
24145 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24146 -1 if PAT2 is more specialized than PAT1.
24147 0 if neither is more specialized.
24148
24149 LEN indicates the number of parameters we should consider
24150 (defaulted parameters should not be considered).
24151
24152 The 1998 std underspecified function template partial ordering, and
24153 DR214 addresses the issue. We take pairs of arguments, one from
24154 each of the templates, and deduce them against each other. One of
24155 the templates will be more specialized if all the *other*
24156 template's arguments deduce against its arguments and at least one
24157 of its arguments *does* *not* deduce against the other template's
24158 corresponding argument. Deduction is done as for class templates.
24159 The arguments used in deduction have reference and top level cv
24160 qualifiers removed. Iff both arguments were originally reference
24161 types *and* deduction succeeds in both directions, an lvalue reference
24162 wins against an rvalue reference and otherwise the template
24163 with the more cv-qualified argument wins for that pairing (if
24164 neither is more cv-qualified, they both are equal). Unlike regular
24165 deduction, after all the arguments have been deduced in this way,
24166 we do *not* verify the deduced template argument values can be
24167 substituted into non-deduced contexts.
24168
24169 The logic can be a bit confusing here, because we look at deduce1 and
24170 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24171 can find template arguments for pat1 to make arg1 look like arg2, that
24172 means that arg2 is at least as specialized as arg1. */
24173
24174 int
24175 more_specialized_fn (tree pat1, tree pat2, int len)
24176 {
24177 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24178 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24179 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24180 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24181 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24182 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24183 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24184 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24185 tree origs1, origs2;
24186 bool lose1 = false;
24187 bool lose2 = false;
24188
24189 /* Remove the this parameter from non-static member functions. If
24190 one is a non-static member function and the other is not a static
24191 member function, remove the first parameter from that function
24192 also. This situation occurs for operator functions where we
24193 locate both a member function (with this pointer) and non-member
24194 operator (with explicit first operand). */
24195 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24196 {
24197 len--; /* LEN is the number of significant arguments for DECL1 */
24198 args1 = TREE_CHAIN (args1);
24199 if (!DECL_STATIC_FUNCTION_P (decl2))
24200 args2 = TREE_CHAIN (args2);
24201 }
24202 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24203 {
24204 args2 = TREE_CHAIN (args2);
24205 if (!DECL_STATIC_FUNCTION_P (decl1))
24206 {
24207 len--;
24208 args1 = TREE_CHAIN (args1);
24209 }
24210 }
24211
24212 /* If only one is a conversion operator, they are unordered. */
24213 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24214 return 0;
24215
24216 /* Consider the return type for a conversion function */
24217 if (DECL_CONV_FN_P (decl1))
24218 {
24219 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24220 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24221 len++;
24222 }
24223
24224 processing_template_decl++;
24225
24226 origs1 = args1;
24227 origs2 = args2;
24228
24229 while (len--
24230 /* Stop when an ellipsis is seen. */
24231 && args1 != NULL_TREE && args2 != NULL_TREE)
24232 {
24233 tree arg1 = TREE_VALUE (args1);
24234 tree arg2 = TREE_VALUE (args2);
24235 int deduce1, deduce2;
24236 int quals1 = -1;
24237 int quals2 = -1;
24238 int ref1 = 0;
24239 int ref2 = 0;
24240
24241 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24242 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24243 {
24244 /* When both arguments are pack expansions, we need only
24245 unify the patterns themselves. */
24246 arg1 = PACK_EXPANSION_PATTERN (arg1);
24247 arg2 = PACK_EXPANSION_PATTERN (arg2);
24248
24249 /* This is the last comparison we need to do. */
24250 len = 0;
24251 }
24252
24253 if (TYPE_REF_P (arg1))
24254 {
24255 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24256 arg1 = TREE_TYPE (arg1);
24257 quals1 = cp_type_quals (arg1);
24258 }
24259
24260 if (TYPE_REF_P (arg2))
24261 {
24262 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24263 arg2 = TREE_TYPE (arg2);
24264 quals2 = cp_type_quals (arg2);
24265 }
24266
24267 arg1 = TYPE_MAIN_VARIANT (arg1);
24268 arg2 = TYPE_MAIN_VARIANT (arg2);
24269
24270 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24271 {
24272 int i, len2 = remaining_arguments (args2);
24273 tree parmvec = make_tree_vec (1);
24274 tree argvec = make_tree_vec (len2);
24275 tree ta = args2;
24276
24277 /* Setup the parameter vector, which contains only ARG1. */
24278 TREE_VEC_ELT (parmvec, 0) = arg1;
24279
24280 /* Setup the argument vector, which contains the remaining
24281 arguments. */
24282 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24283 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24284
24285 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24286 argvec, DEDUCE_EXACT,
24287 /*subr=*/true, /*explain_p=*/false)
24288 == 0);
24289
24290 /* We cannot deduce in the other direction, because ARG1 is
24291 a pack expansion but ARG2 is not. */
24292 deduce2 = 0;
24293 }
24294 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24295 {
24296 int i, len1 = remaining_arguments (args1);
24297 tree parmvec = make_tree_vec (1);
24298 tree argvec = make_tree_vec (len1);
24299 tree ta = args1;
24300
24301 /* Setup the parameter vector, which contains only ARG1. */
24302 TREE_VEC_ELT (parmvec, 0) = arg2;
24303
24304 /* Setup the argument vector, which contains the remaining
24305 arguments. */
24306 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24307 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24308
24309 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24310 argvec, DEDUCE_EXACT,
24311 /*subr=*/true, /*explain_p=*/false)
24312 == 0);
24313
24314 /* We cannot deduce in the other direction, because ARG2 is
24315 a pack expansion but ARG1 is not.*/
24316 deduce1 = 0;
24317 }
24318
24319 else
24320 {
24321 /* The normal case, where neither argument is a pack
24322 expansion. */
24323 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24324 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24325 == 0);
24326 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24327 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24328 == 0);
24329 }
24330
24331 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24332 arg2, then arg2 is not as specialized as arg1. */
24333 if (!deduce1)
24334 lose2 = true;
24335 if (!deduce2)
24336 lose1 = true;
24337
24338 /* "If, for a given type, deduction succeeds in both directions
24339 (i.e., the types are identical after the transformations above)
24340 and both P and A were reference types (before being replaced with
24341 the type referred to above):
24342 - if the type from the argument template was an lvalue reference and
24343 the type from the parameter template was not, the argument type is
24344 considered to be more specialized than the other; otherwise,
24345 - if the type from the argument template is more cv-qualified
24346 than the type from the parameter template (as described above),
24347 the argument type is considered to be more specialized than the other;
24348 otherwise,
24349 - neither type is more specialized than the other." */
24350
24351 if (deduce1 && deduce2)
24352 {
24353 if (ref1 && ref2 && ref1 != ref2)
24354 {
24355 if (ref1 > ref2)
24356 lose1 = true;
24357 else
24358 lose2 = true;
24359 }
24360 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24361 {
24362 if ((quals1 & quals2) == quals2)
24363 lose2 = true;
24364 if ((quals1 & quals2) == quals1)
24365 lose1 = true;
24366 }
24367 }
24368
24369 if (lose1 && lose2)
24370 /* We've failed to deduce something in either direction.
24371 These must be unordered. */
24372 break;
24373
24374 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24375 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24376 /* We have already processed all of the arguments in our
24377 handing of the pack expansion type. */
24378 len = 0;
24379
24380 args1 = TREE_CHAIN (args1);
24381 args2 = TREE_CHAIN (args2);
24382 }
24383
24384 /* "In most cases, all template parameters must have values in order for
24385 deduction to succeed, but for partial ordering purposes a template
24386 parameter may remain without a value provided it is not used in the
24387 types being used for partial ordering."
24388
24389 Thus, if we are missing any of the targs1 we need to substitute into
24390 origs1, then pat2 is not as specialized as pat1. This can happen when
24391 there is a nondeduced context. */
24392 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24393 lose2 = true;
24394 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24395 lose1 = true;
24396
24397 processing_template_decl--;
24398
24399 /* If both deductions succeed, the partial ordering selects the more
24400 constrained template. */
24401 /* P2113: If the corresponding template-parameters of the
24402 template-parameter-lists are not equivalent ([temp.over.link]) or if
24403 the function parameters that positionally correspond between the two
24404 templates are not of the same type, neither template is more
24405 specialized than the other. */
24406 if (!lose1 && !lose2
24407 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24408 DECL_TEMPLATE_PARMS (pat2))
24409 && compparms (origs1, origs2))
24410 {
24411 int winner = more_constrained (decl1, decl2);
24412 if (winner > 0)
24413 lose2 = true;
24414 else if (winner < 0)
24415 lose1 = true;
24416 }
24417
24418 /* All things being equal, if the next argument is a pack expansion
24419 for one function but not for the other, prefer the
24420 non-variadic function. FIXME this is bogus; see c++/41958. */
24421 if (lose1 == lose2
24422 && args1 && TREE_VALUE (args1)
24423 && args2 && TREE_VALUE (args2))
24424 {
24425 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24426 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24427 }
24428
24429 if (lose1 == lose2)
24430 return 0;
24431 else if (!lose1)
24432 return 1;
24433 else
24434 return -1;
24435 }
24436
24437 /* Determine which of two partial specializations of TMPL is more
24438 specialized.
24439
24440 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24441 to the first partial specialization. The TREE_PURPOSE is the
24442 innermost set of template parameters for the partial
24443 specialization. PAT2 is similar, but for the second template.
24444
24445 Return 1 if the first partial specialization is more specialized;
24446 -1 if the second is more specialized; 0 if neither is more
24447 specialized.
24448
24449 See [temp.class.order] for information about determining which of
24450 two templates is more specialized. */
24451
24452 static int
24453 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24454 {
24455 tree targs;
24456 int winner = 0;
24457 bool any_deductions = false;
24458
24459 tree tmpl1 = TREE_VALUE (pat1);
24460 tree tmpl2 = TREE_VALUE (pat2);
24461 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24462 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24463
24464 /* Just like what happens for functions, if we are ordering between
24465 different template specializations, we may encounter dependent
24466 types in the arguments, and we need our dependency check functions
24467 to behave correctly. */
24468 ++processing_template_decl;
24469 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24470 if (targs)
24471 {
24472 --winner;
24473 any_deductions = true;
24474 }
24475
24476 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24477 if (targs)
24478 {
24479 ++winner;
24480 any_deductions = true;
24481 }
24482 --processing_template_decl;
24483
24484 /* If both deductions succeed, the partial ordering selects the more
24485 constrained template. */
24486 if (!winner && any_deductions)
24487 winner = more_constrained (tmpl1, tmpl2);
24488
24489 /* In the case of a tie where at least one of the templates
24490 has a parameter pack at the end, the template with the most
24491 non-packed parameters wins. */
24492 if (winner == 0
24493 && any_deductions
24494 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24495 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24496 {
24497 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24498 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24499 int len1 = TREE_VEC_LENGTH (args1);
24500 int len2 = TREE_VEC_LENGTH (args2);
24501
24502 /* We don't count the pack expansion at the end. */
24503 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24504 --len1;
24505 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24506 --len2;
24507
24508 if (len1 > len2)
24509 return 1;
24510 else if (len1 < len2)
24511 return -1;
24512 }
24513
24514 return winner;
24515 }
24516
24517 /* Return the template arguments that will produce the function signature
24518 DECL from the function template FN, with the explicit template
24519 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24520 also match. Return NULL_TREE if no satisfactory arguments could be
24521 found. */
24522
24523 static tree
24524 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24525 {
24526 int ntparms = DECL_NTPARMS (fn);
24527 tree targs = make_tree_vec (ntparms);
24528 tree decl_type = TREE_TYPE (decl);
24529 tree decl_arg_types;
24530 tree *args;
24531 unsigned int nargs, ix;
24532 tree arg;
24533
24534 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24535
24536 /* Never do unification on the 'this' parameter. */
24537 decl_arg_types = skip_artificial_parms_for (decl,
24538 TYPE_ARG_TYPES (decl_type));
24539
24540 nargs = list_length (decl_arg_types);
24541 args = XALLOCAVEC (tree, nargs);
24542 for (arg = decl_arg_types, ix = 0;
24543 arg != NULL_TREE && arg != void_list_node;
24544 arg = TREE_CHAIN (arg), ++ix)
24545 args[ix] = TREE_VALUE (arg);
24546
24547 if (fn_type_unification (fn, explicit_args, targs,
24548 args, ix,
24549 (check_rettype || DECL_CONV_FN_P (fn)
24550 ? TREE_TYPE (decl_type) : NULL_TREE),
24551 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24552 /*explain_p=*/false,
24553 /*decltype*/false)
24554 == error_mark_node)
24555 return NULL_TREE;
24556
24557 return targs;
24558 }
24559
24560 /* Return the innermost template arguments that, when applied to a partial
24561 specialization SPEC_TMPL of TMPL, yield the ARGS.
24562
24563 For example, suppose we have:
24564
24565 template <class T, class U> struct S {};
24566 template <class T> struct S<T*, int> {};
24567
24568 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24569 partial specialization and the ARGS will be {double*, int}. The resulting
24570 vector will be {double}, indicating that `T' is bound to `double'. */
24571
24572 static tree
24573 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24574 {
24575 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24576 tree spec_args
24577 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24578 int i, ntparms = TREE_VEC_LENGTH (tparms);
24579 tree deduced_args;
24580 tree innermost_deduced_args;
24581
24582 innermost_deduced_args = make_tree_vec (ntparms);
24583 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24584 {
24585 deduced_args = copy_node (args);
24586 SET_TMPL_ARGS_LEVEL (deduced_args,
24587 TMPL_ARGS_DEPTH (deduced_args),
24588 innermost_deduced_args);
24589 }
24590 else
24591 deduced_args = innermost_deduced_args;
24592
24593 bool tried_array_deduction = (cxx_dialect < cxx17);
24594 again:
24595 if (unify (tparms, deduced_args,
24596 INNERMOST_TEMPLATE_ARGS (spec_args),
24597 INNERMOST_TEMPLATE_ARGS (args),
24598 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24599 return NULL_TREE;
24600
24601 for (i = 0; i < ntparms; ++i)
24602 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24603 {
24604 if (!tried_array_deduction)
24605 {
24606 try_array_deduction (tparms, innermost_deduced_args,
24607 INNERMOST_TEMPLATE_ARGS (spec_args));
24608 tried_array_deduction = true;
24609 if (TREE_VEC_ELT (innermost_deduced_args, i))
24610 goto again;
24611 }
24612 return NULL_TREE;
24613 }
24614
24615 if (!push_tinst_level (spec_tmpl, deduced_args))
24616 {
24617 excessive_deduction_depth = true;
24618 return NULL_TREE;
24619 }
24620
24621 /* Verify that nondeduced template arguments agree with the type
24622 obtained from argument deduction.
24623
24624 For example:
24625
24626 struct A { typedef int X; };
24627 template <class T, class U> struct C {};
24628 template <class T> struct C<T, typename T::X> {};
24629
24630 Then with the instantiation `C<A, int>', we can deduce that
24631 `T' is `A' but unify () does not check whether `typename T::X'
24632 is `int'. */
24633 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24634
24635 if (spec_args != error_mark_node)
24636 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24637 INNERMOST_TEMPLATE_ARGS (spec_args),
24638 tmpl, tf_none, false, false);
24639
24640 pop_tinst_level ();
24641
24642 if (spec_args == error_mark_node
24643 /* We only need to check the innermost arguments; the other
24644 arguments will always agree. */
24645 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24646 INNERMOST_TEMPLATE_ARGS (args)))
24647 return NULL_TREE;
24648
24649 /* Now that we have bindings for all of the template arguments,
24650 ensure that the arguments deduced for the template template
24651 parameters have compatible template parameter lists. See the use
24652 of template_template_parm_bindings_ok_p in fn_type_unification
24653 for more information. */
24654 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24655 return NULL_TREE;
24656
24657 return deduced_args;
24658 }
24659
24660 // Compare two function templates T1 and T2 by deducing bindings
24661 // from one against the other. If both deductions succeed, compare
24662 // constraints to see which is more constrained.
24663 static int
24664 more_specialized_inst (tree t1, tree t2)
24665 {
24666 int fate = 0;
24667 int count = 0;
24668
24669 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24670 {
24671 --fate;
24672 ++count;
24673 }
24674
24675 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24676 {
24677 ++fate;
24678 ++count;
24679 }
24680
24681 // If both deductions succeed, then one may be more constrained.
24682 if (count == 2 && fate == 0)
24683 fate = more_constrained (t1, t2);
24684
24685 return fate;
24686 }
24687
24688 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24689 Return the TREE_LIST node with the most specialized template, if
24690 any. If there is no most specialized template, the error_mark_node
24691 is returned.
24692
24693 Note that this function does not look at, or modify, the
24694 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24695 returned is one of the elements of INSTANTIATIONS, callers may
24696 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24697 and retrieve it from the value returned. */
24698
24699 tree
24700 most_specialized_instantiation (tree templates)
24701 {
24702 tree fn, champ;
24703
24704 ++processing_template_decl;
24705
24706 champ = templates;
24707 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24708 {
24709 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24710 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24711 if (fate == -1)
24712 champ = fn;
24713 else if (!fate)
24714 {
24715 /* Equally specialized, move to next function. If there
24716 is no next function, nothing's most specialized. */
24717 fn = TREE_CHAIN (fn);
24718 champ = fn;
24719 if (!fn)
24720 break;
24721 }
24722 }
24723
24724 if (champ)
24725 /* Now verify that champ is better than everything earlier in the
24726 instantiation list. */
24727 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24728 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24729 {
24730 champ = NULL_TREE;
24731 break;
24732 }
24733 }
24734
24735 processing_template_decl--;
24736
24737 if (!champ)
24738 return error_mark_node;
24739
24740 return champ;
24741 }
24742
24743 /* If DECL is a specialization of some template, return the most
24744 general such template. Otherwise, returns NULL_TREE.
24745
24746 For example, given:
24747
24748 template <class T> struct S { template <class U> void f(U); };
24749
24750 if TMPL is `template <class U> void S<int>::f(U)' this will return
24751 the full template. This function will not trace past partial
24752 specializations, however. For example, given in addition:
24753
24754 template <class T> struct S<T*> { template <class U> void f(U); };
24755
24756 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24757 `template <class T> template <class U> S<T*>::f(U)'. */
24758
24759 tree
24760 most_general_template (tree decl)
24761 {
24762 if (TREE_CODE (decl) != TEMPLATE_DECL)
24763 {
24764 if (tree tinfo = get_template_info (decl))
24765 decl = TI_TEMPLATE (tinfo);
24766 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24767 template friend, or a FIELD_DECL for a capture pack. */
24768 if (TREE_CODE (decl) != TEMPLATE_DECL)
24769 return NULL_TREE;
24770 }
24771
24772 /* Look for more and more general templates. */
24773 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24774 {
24775 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24776 (See cp-tree.h for details.) */
24777 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24778 break;
24779
24780 if (CLASS_TYPE_P (TREE_TYPE (decl))
24781 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24782 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24783 break;
24784
24785 /* Stop if we run into an explicitly specialized class template. */
24786 if (!DECL_NAMESPACE_SCOPE_P (decl)
24787 && DECL_CONTEXT (decl)
24788 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24789 break;
24790
24791 decl = DECL_TI_TEMPLATE (decl);
24792 }
24793
24794 return decl;
24795 }
24796
24797 /* Return the most specialized of the template partial specializations
24798 which can produce TARGET, a specialization of some class or variable
24799 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24800 a TEMPLATE_DECL node corresponding to the partial specialization, while
24801 the TREE_PURPOSE is the set of template arguments that must be
24802 substituted into the template pattern in order to generate TARGET.
24803
24804 If the choice of partial specialization is ambiguous, a diagnostic
24805 is issued, and the error_mark_node is returned. If there are no
24806 partial specializations matching TARGET, then NULL_TREE is
24807 returned, indicating that the primary template should be used. */
24808
24809 tree
24810 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24811 {
24812 tree list = NULL_TREE;
24813 tree t;
24814 tree champ;
24815 int fate;
24816 bool ambiguous_p;
24817 tree outer_args = NULL_TREE;
24818 tree tmpl, args;
24819
24820 if (TYPE_P (target))
24821 {
24822 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24823 tmpl = TI_TEMPLATE (tinfo);
24824 args = TI_ARGS (tinfo);
24825 }
24826 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24827 {
24828 tmpl = TREE_OPERAND (target, 0);
24829 args = TREE_OPERAND (target, 1);
24830 }
24831 else if (VAR_P (target))
24832 {
24833 tree tinfo = DECL_TEMPLATE_INFO (target);
24834 tmpl = TI_TEMPLATE (tinfo);
24835 args = TI_ARGS (tinfo);
24836 }
24837 else
24838 gcc_unreachable ();
24839
24840 tree main_tmpl = most_general_template (tmpl);
24841
24842 /* For determining which partial specialization to use, only the
24843 innermost args are interesting. */
24844 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24845 {
24846 outer_args = strip_innermost_template_args (args, 1);
24847 args = INNERMOST_TEMPLATE_ARGS (args);
24848 }
24849
24850 /* The caller hasn't called push_to_top_level yet, but we need
24851 get_partial_spec_bindings to be done in non-template context so that we'll
24852 fully resolve everything. */
24853 processing_template_decl_sentinel ptds;
24854
24855 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24856 {
24857 const tree ospec_tmpl = TREE_VALUE (t);
24858
24859 tree spec_tmpl;
24860 if (outer_args)
24861 {
24862 /* Substitute in the template args from the enclosing class. */
24863 ++processing_template_decl;
24864 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24865 --processing_template_decl;
24866 if (spec_tmpl == error_mark_node)
24867 return error_mark_node;
24868 }
24869 else
24870 spec_tmpl = ospec_tmpl;
24871
24872 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24873 if (spec_args)
24874 {
24875 if (outer_args)
24876 spec_args = add_to_template_args (outer_args, spec_args);
24877
24878 /* Keep the candidate only if the constraints are satisfied,
24879 or if we're not compiling with concepts. */
24880 if (!flag_concepts
24881 || constraints_satisfied_p (ospec_tmpl, spec_args))
24882 {
24883 list = tree_cons (spec_args, ospec_tmpl, list);
24884 TREE_TYPE (list) = TREE_TYPE (t);
24885 }
24886 }
24887 }
24888
24889 if (! list)
24890 return NULL_TREE;
24891
24892 ambiguous_p = false;
24893 t = list;
24894 champ = t;
24895 t = TREE_CHAIN (t);
24896 for (; t; t = TREE_CHAIN (t))
24897 {
24898 fate = more_specialized_partial_spec (tmpl, champ, t);
24899 if (fate == 1)
24900 ;
24901 else
24902 {
24903 if (fate == 0)
24904 {
24905 t = TREE_CHAIN (t);
24906 if (! t)
24907 {
24908 ambiguous_p = true;
24909 break;
24910 }
24911 }
24912 champ = t;
24913 }
24914 }
24915
24916 if (!ambiguous_p)
24917 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24918 {
24919 fate = more_specialized_partial_spec (tmpl, champ, t);
24920 if (fate != 1)
24921 {
24922 ambiguous_p = true;
24923 break;
24924 }
24925 }
24926
24927 if (ambiguous_p)
24928 {
24929 const char *str;
24930 char *spaces = NULL;
24931 if (!(complain & tf_error))
24932 return error_mark_node;
24933 if (TYPE_P (target))
24934 error ("ambiguous template instantiation for %q#T", target);
24935 else
24936 error ("ambiguous template instantiation for %q#D", target);
24937 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24938 for (t = list; t; t = TREE_CHAIN (t))
24939 {
24940 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24941 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24942 "%s %#qS", spaces ? spaces : str, subst);
24943 spaces = spaces ? spaces : get_spaces (str);
24944 }
24945 free (spaces);
24946 return error_mark_node;
24947 }
24948
24949 return champ;
24950 }
24951
24952 /* Explicitly instantiate DECL. */
24953
24954 void
24955 do_decl_instantiation (tree decl, tree storage)
24956 {
24957 tree result = NULL_TREE;
24958 int extern_p = 0;
24959
24960 if (!decl || decl == error_mark_node)
24961 /* An error occurred, for which grokdeclarator has already issued
24962 an appropriate message. */
24963 return;
24964 else if (! DECL_LANG_SPECIFIC (decl))
24965 {
24966 error ("explicit instantiation of non-template %q#D", decl);
24967 return;
24968 }
24969 else if (DECL_DECLARED_CONCEPT_P (decl))
24970 {
24971 if (VAR_P (decl))
24972 error ("explicit instantiation of variable concept %q#D", decl);
24973 else
24974 error ("explicit instantiation of function concept %q#D", decl);
24975 return;
24976 }
24977
24978 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24979 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24980
24981 if (VAR_P (decl) && !var_templ)
24982 {
24983 /* There is an asymmetry here in the way VAR_DECLs and
24984 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24985 the latter, the DECL we get back will be marked as a
24986 template instantiation, and the appropriate
24987 DECL_TEMPLATE_INFO will be set up. This does not happen for
24988 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24989 should handle VAR_DECLs as it currently handles
24990 FUNCTION_DECLs. */
24991 if (!DECL_CLASS_SCOPE_P (decl))
24992 {
24993 error ("%qD is not a static data member of a class template", decl);
24994 return;
24995 }
24996 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24997 if (!result || !VAR_P (result))
24998 {
24999 error ("no matching template for %qD found", decl);
25000 return;
25001 }
25002 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25003 {
25004 error ("type %qT for explicit instantiation %qD does not match "
25005 "declared type %qT", TREE_TYPE (result), decl,
25006 TREE_TYPE (decl));
25007 return;
25008 }
25009 }
25010 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25011 {
25012 error ("explicit instantiation of %q#D", decl);
25013 return;
25014 }
25015 else
25016 result = decl;
25017
25018 /* Check for various error cases. Note that if the explicit
25019 instantiation is valid the RESULT will currently be marked as an
25020 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25021 until we get here. */
25022
25023 if (DECL_TEMPLATE_SPECIALIZATION (result))
25024 {
25025 /* DR 259 [temp.spec].
25026
25027 Both an explicit instantiation and a declaration of an explicit
25028 specialization shall not appear in a program unless the explicit
25029 instantiation follows a declaration of the explicit specialization.
25030
25031 For a given set of template parameters, if an explicit
25032 instantiation of a template appears after a declaration of an
25033 explicit specialization for that template, the explicit
25034 instantiation has no effect. */
25035 return;
25036 }
25037 else if (DECL_EXPLICIT_INSTANTIATION (result))
25038 {
25039 /* [temp.spec]
25040
25041 No program shall explicitly instantiate any template more
25042 than once.
25043
25044 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25045 the first instantiation was `extern' and the second is not,
25046 and EXTERN_P for the opposite case. */
25047 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25048 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25049 /* If an "extern" explicit instantiation follows an ordinary
25050 explicit instantiation, the template is instantiated. */
25051 if (extern_p)
25052 return;
25053 }
25054 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25055 {
25056 error ("no matching template for %qD found", result);
25057 return;
25058 }
25059 else if (!DECL_TEMPLATE_INFO (result))
25060 {
25061 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25062 return;
25063 }
25064
25065 if (storage == NULL_TREE)
25066 ;
25067 else if (storage == ridpointers[(int) RID_EXTERN])
25068 {
25069 if (cxx_dialect == cxx98)
25070 pedwarn (input_location, OPT_Wpedantic,
25071 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25072 "instantiations");
25073 extern_p = 1;
25074 }
25075 else
25076 error ("storage class %qD applied to template instantiation", storage);
25077
25078 check_explicit_instantiation_namespace (result);
25079 mark_decl_instantiated (result, extern_p);
25080 if (! extern_p)
25081 instantiate_decl (result, /*defer_ok=*/true,
25082 /*expl_inst_class_mem_p=*/false);
25083 }
25084
25085 static void
25086 mark_class_instantiated (tree t, int extern_p)
25087 {
25088 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25089 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25090 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25091 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25092 if (! extern_p)
25093 {
25094 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25095 rest_of_type_compilation (t, 1);
25096 }
25097 }
25098
25099 /* Perform an explicit instantiation of template class T. STORAGE, if
25100 non-null, is the RID for extern, inline or static. COMPLAIN is
25101 nonzero if this is called from the parser, zero if called recursively,
25102 since the standard is unclear (as detailed below). */
25103
25104 void
25105 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25106 {
25107 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25108 {
25109 if (tree ti = TYPE_TEMPLATE_INFO (t))
25110 error ("explicit instantiation of non-class template %qD",
25111 TI_TEMPLATE (ti));
25112 else
25113 error ("explicit instantiation of non-template type %qT", t);
25114 return;
25115 }
25116
25117 complete_type (t);
25118
25119 if (!COMPLETE_TYPE_P (t))
25120 {
25121 if (complain & tf_error)
25122 error ("explicit instantiation of %q#T before definition of template",
25123 t);
25124 return;
25125 }
25126
25127 /* At most one of these will be true. */
25128 bool extern_p = false;
25129 bool nomem_p = false;
25130 bool static_p = false;
25131
25132 if (storage != NULL_TREE)
25133 {
25134 if (storage == ridpointers[(int) RID_EXTERN])
25135 {
25136 if (cxx_dialect == cxx98)
25137 pedwarn (input_location, OPT_Wpedantic,
25138 "ISO C++ 1998 forbids the use of %<extern%> on "
25139 "explicit instantiations");
25140 }
25141 else
25142 pedwarn (input_location, OPT_Wpedantic,
25143 "ISO C++ forbids the use of %qE"
25144 " on explicit instantiations", storage);
25145
25146 if (storage == ridpointers[(int) RID_INLINE])
25147 nomem_p = true;
25148 else if (storage == ridpointers[(int) RID_EXTERN])
25149 extern_p = true;
25150 else if (storage == ridpointers[(int) RID_STATIC])
25151 static_p = true;
25152 else
25153 error ("storage class %qD applied to template instantiation",
25154 storage);
25155 }
25156
25157 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25158 /* DR 259 [temp.spec].
25159
25160 Both an explicit instantiation and a declaration of an explicit
25161 specialization shall not appear in a program unless the
25162 explicit instantiation follows a declaration of the explicit
25163 specialization.
25164
25165 For a given set of template parameters, if an explicit
25166 instantiation of a template appears after a declaration of an
25167 explicit specialization for that template, the explicit
25168 instantiation has no effect. */
25169 return;
25170
25171 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25172 {
25173 /* We've already instantiated the template. */
25174
25175 /* [temp.spec]
25176
25177 No program shall explicitly instantiate any template more
25178 than once.
25179
25180 If EXTERN_P then this is ok. */
25181 if (!extern_p && (complain & tf_error))
25182 permerror (input_location,
25183 "duplicate explicit instantiation of %q#T", t);
25184
25185 return;
25186 }
25187
25188 check_explicit_instantiation_namespace (TYPE_NAME (t));
25189 mark_class_instantiated (t, extern_p);
25190
25191 if (nomem_p)
25192 return;
25193
25194 /* In contrast to implicit instantiation, where only the
25195 declarations, and not the definitions, of members are
25196 instantiated, we have here:
25197
25198 [temp.explicit]
25199
25200 An explicit instantiation that names a class template
25201 specialization is also an explicit instantiation of the same
25202 kind (declaration or definition) of each of its members (not
25203 including members inherited from base classes and members
25204 that are templates) that has not been previously explicitly
25205 specialized in the translation unit containing the explicit
25206 instantiation, provided that the associated constraints, if
25207 any, of that member are satisfied by the template arguments
25208 of the explicit instantiation. */
25209 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25210 if ((VAR_P (fld)
25211 || (TREE_CODE (fld) == FUNCTION_DECL
25212 && !static_p
25213 && user_provided_p (fld)))
25214 && DECL_TEMPLATE_INSTANTIATION (fld)
25215 && constraints_satisfied_p (fld))
25216 {
25217 mark_decl_instantiated (fld, extern_p);
25218 if (! extern_p)
25219 instantiate_decl (fld, /*defer_ok=*/true,
25220 /*expl_inst_class_mem_p=*/true);
25221 }
25222 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25223 {
25224 tree type = TREE_TYPE (fld);
25225
25226 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25227 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25228 do_type_instantiation (type, storage, 0);
25229 }
25230 }
25231
25232 /* Given a function DECL, which is a specialization of TMPL, modify
25233 DECL to be a re-instantiation of TMPL with the same template
25234 arguments. TMPL should be the template into which tsubst'ing
25235 should occur for DECL, not the most general template.
25236
25237 One reason for doing this is a scenario like this:
25238
25239 template <class T>
25240 void f(const T&, int i);
25241
25242 void g() { f(3, 7); }
25243
25244 template <class T>
25245 void f(const T& t, const int i) { }
25246
25247 Note that when the template is first instantiated, with
25248 instantiate_template, the resulting DECL will have no name for the
25249 first parameter, and the wrong type for the second. So, when we go
25250 to instantiate the DECL, we regenerate it. */
25251
25252 static void
25253 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25254 {
25255 /* The arguments used to instantiate DECL, from the most general
25256 template. */
25257 tree code_pattern;
25258
25259 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25260
25261 /* Make sure that we can see identifiers, and compute access
25262 correctly. */
25263 push_access_scope (decl);
25264
25265 if (TREE_CODE (decl) == FUNCTION_DECL)
25266 {
25267 tree decl_parm;
25268 tree pattern_parm;
25269 tree specs;
25270 int args_depth;
25271 int parms_depth;
25272
25273 args_depth = TMPL_ARGS_DEPTH (args);
25274 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25275 if (args_depth > parms_depth)
25276 args = get_innermost_template_args (args, parms_depth);
25277
25278 /* Instantiate a dynamic exception-specification. noexcept will be
25279 handled below. */
25280 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25281 if (TREE_VALUE (raises))
25282 {
25283 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25284 args, tf_error, NULL_TREE,
25285 /*defer_ok*/false);
25286 if (specs && specs != error_mark_node)
25287 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25288 specs);
25289 }
25290
25291 /* Merge parameter declarations. */
25292 decl_parm = skip_artificial_parms_for (decl,
25293 DECL_ARGUMENTS (decl));
25294 pattern_parm
25295 = skip_artificial_parms_for (code_pattern,
25296 DECL_ARGUMENTS (code_pattern));
25297 while (decl_parm && !DECL_PACK_P (pattern_parm))
25298 {
25299 tree parm_type;
25300 tree attributes;
25301
25302 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25303 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25304 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25305 NULL_TREE);
25306 parm_type = type_decays_to (parm_type);
25307 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25308 TREE_TYPE (decl_parm) = parm_type;
25309 attributes = DECL_ATTRIBUTES (pattern_parm);
25310 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25311 {
25312 DECL_ATTRIBUTES (decl_parm) = attributes;
25313 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25314 }
25315 decl_parm = DECL_CHAIN (decl_parm);
25316 pattern_parm = DECL_CHAIN (pattern_parm);
25317 }
25318 /* Merge any parameters that match with the function parameter
25319 pack. */
25320 if (pattern_parm && DECL_PACK_P (pattern_parm))
25321 {
25322 int i, len;
25323 tree expanded_types;
25324 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25325 the parameters in this function parameter pack. */
25326 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25327 args, tf_error, NULL_TREE);
25328 len = TREE_VEC_LENGTH (expanded_types);
25329 for (i = 0; i < len; i++)
25330 {
25331 tree parm_type;
25332 tree attributes;
25333
25334 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25335 /* Rename the parameter to include the index. */
25336 DECL_NAME (decl_parm) =
25337 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25338 parm_type = TREE_VEC_ELT (expanded_types, i);
25339 parm_type = type_decays_to (parm_type);
25340 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25341 TREE_TYPE (decl_parm) = parm_type;
25342 attributes = DECL_ATTRIBUTES (pattern_parm);
25343 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25344 {
25345 DECL_ATTRIBUTES (decl_parm) = attributes;
25346 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25347 }
25348 decl_parm = DECL_CHAIN (decl_parm);
25349 }
25350 }
25351 /* Merge additional specifiers from the CODE_PATTERN. */
25352 if (DECL_DECLARED_INLINE_P (code_pattern)
25353 && !DECL_DECLARED_INLINE_P (decl))
25354 DECL_DECLARED_INLINE_P (decl) = 1;
25355
25356 maybe_instantiate_noexcept (decl, tf_error);
25357 }
25358 else if (VAR_P (decl))
25359 {
25360 start_lambda_scope (decl);
25361 DECL_INITIAL (decl) =
25362 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25363 tf_error, DECL_TI_TEMPLATE (decl));
25364 finish_lambda_scope ();
25365 if (VAR_HAD_UNKNOWN_BOUND (decl))
25366 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25367 tf_error, DECL_TI_TEMPLATE (decl));
25368 }
25369 else
25370 gcc_unreachable ();
25371
25372 pop_access_scope (decl);
25373 }
25374
25375 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25376 substituted to get DECL. */
25377
25378 tree
25379 template_for_substitution (tree decl)
25380 {
25381 tree tmpl = DECL_TI_TEMPLATE (decl);
25382
25383 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25384 for the instantiation. This is not always the most general
25385 template. Consider, for example:
25386
25387 template <class T>
25388 struct S { template <class U> void f();
25389 template <> void f<int>(); };
25390
25391 and an instantiation of S<double>::f<int>. We want TD to be the
25392 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25393 while (/* An instantiation cannot have a definition, so we need a
25394 more general template. */
25395 DECL_TEMPLATE_INSTANTIATION (tmpl)
25396 /* We must also deal with friend templates. Given:
25397
25398 template <class T> struct S {
25399 template <class U> friend void f() {};
25400 };
25401
25402 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25403 so far as the language is concerned, but that's still
25404 where we get the pattern for the instantiation from. On
25405 other hand, if the definition comes outside the class, say:
25406
25407 template <class T> struct S {
25408 template <class U> friend void f();
25409 };
25410 template <class U> friend void f() {}
25411
25412 we don't need to look any further. That's what the check for
25413 DECL_INITIAL is for. */
25414 || (TREE_CODE (decl) == FUNCTION_DECL
25415 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25416 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25417 {
25418 /* The present template, TD, should not be a definition. If it
25419 were a definition, we should be using it! Note that we
25420 cannot restructure the loop to just keep going until we find
25421 a template with a definition, since that might go too far if
25422 a specialization was declared, but not defined. */
25423
25424 /* Fetch the more general template. */
25425 tmpl = DECL_TI_TEMPLATE (tmpl);
25426 }
25427
25428 return tmpl;
25429 }
25430
25431 /* Returns true if we need to instantiate this template instance even if we
25432 know we aren't going to emit it. */
25433
25434 bool
25435 always_instantiate_p (tree decl)
25436 {
25437 /* We always instantiate inline functions so that we can inline them. An
25438 explicit instantiation declaration prohibits implicit instantiation of
25439 non-inline functions. With high levels of optimization, we would
25440 normally inline non-inline functions -- but we're not allowed to do
25441 that for "extern template" functions. Therefore, we check
25442 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25443 return ((TREE_CODE (decl) == FUNCTION_DECL
25444 && (DECL_DECLARED_INLINE_P (decl)
25445 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25446 /* And we need to instantiate static data members so that
25447 their initializers are available in integral constant
25448 expressions. */
25449 || (VAR_P (decl)
25450 && decl_maybe_constant_var_p (decl)));
25451 }
25452
25453 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25454 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25455 error, true otherwise. */
25456
25457 bool
25458 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25459 {
25460 tree fntype, spec, noex;
25461
25462 /* Don't instantiate a noexcept-specification from template context. */
25463 if (processing_template_decl
25464 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25465 return true;
25466
25467 if (DECL_MAYBE_DELETED (fn))
25468 {
25469 if (fn == current_function_decl)
25470 /* We're in start_preparsed_function, keep going. */
25471 return true;
25472
25473 ++function_depth;
25474 synthesize_method (fn);
25475 --function_depth;
25476 return !DECL_MAYBE_DELETED (fn);
25477 }
25478
25479 fntype = TREE_TYPE (fn);
25480 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25481
25482 if (!spec || !TREE_PURPOSE (spec))
25483 return true;
25484
25485 noex = TREE_PURPOSE (spec);
25486 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25487 && TREE_CODE (noex) != DEFERRED_PARSE)
25488 return true;
25489
25490 tree orig_fn = NULL_TREE;
25491 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25492 its FUNCTION_DECL for the rest of this function -- push_access_scope
25493 doesn't accept TEMPLATE_DECLs. */
25494 if (DECL_FUNCTION_TEMPLATE_P (fn))
25495 {
25496 orig_fn = fn;
25497 fn = DECL_TEMPLATE_RESULT (fn);
25498 }
25499
25500 if (DECL_CLONED_FUNCTION_P (fn))
25501 {
25502 tree prime = DECL_CLONED_FUNCTION (fn);
25503 if (!maybe_instantiate_noexcept (prime, complain))
25504 return false;
25505 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25506 }
25507 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25508 {
25509 static hash_set<tree>* fns = new hash_set<tree>;
25510 bool added = false;
25511 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25512 {
25513 spec = get_defaulted_eh_spec (fn, complain);
25514 if (spec == error_mark_node)
25515 /* This might have failed because of an unparsed DMI, so
25516 let's try again later. */
25517 return false;
25518 }
25519 else if (!(added = !fns->add (fn)))
25520 {
25521 /* If hash_set::add returns true, the element was already there. */
25522 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25523 DECL_SOURCE_LOCATION (fn));
25524 error_at (loc,
25525 "exception specification of %qD depends on itself",
25526 fn);
25527 spec = noexcept_false_spec;
25528 }
25529 else if (push_tinst_level (fn))
25530 {
25531 push_to_top_level ();
25532 push_access_scope (fn);
25533 push_deferring_access_checks (dk_no_deferred);
25534 input_location = DECL_SOURCE_LOCATION (fn);
25535
25536 if (!DECL_LOCAL_DECL_P (fn))
25537 {
25538 /* If needed, set current_class_ptr for the benefit of
25539 tsubst_copy/PARM_DECL. The exception pattern will
25540 refer to the parm of the template, not the
25541 instantiation. */
25542 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25543 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25544 {
25545 tree this_parm = DECL_ARGUMENTS (tdecl);
25546 current_class_ptr = NULL_TREE;
25547 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25548 current_class_ptr = this_parm;
25549 }
25550 }
25551
25552 /* If this function is represented by a TEMPLATE_DECL, then
25553 the deferred noexcept-specification might still contain
25554 dependent types, even after substitution. And we need the
25555 dependency check functions to work in build_noexcept_spec. */
25556 if (orig_fn)
25557 ++processing_template_decl;
25558
25559 /* Do deferred instantiation of the noexcept-specifier. */
25560 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25561 DEFERRED_NOEXCEPT_ARGS (noex),
25562 tf_warning_or_error, fn,
25563 /*function_p=*/false,
25564 /*i_c_e_p=*/true);
25565
25566 /* Build up the noexcept-specification. */
25567 spec = build_noexcept_spec (noex, tf_warning_or_error);
25568
25569 if (orig_fn)
25570 --processing_template_decl;
25571
25572 pop_deferring_access_checks ();
25573 pop_access_scope (fn);
25574 pop_tinst_level ();
25575 pop_from_top_level ();
25576 }
25577 else
25578 spec = noexcept_false_spec;
25579
25580 if (added)
25581 fns->remove (fn);
25582 }
25583
25584 if (spec == error_mark_node)
25585 {
25586 /* This failed with a hard error, so let's go with false. */
25587 gcc_assert (seen_error ());
25588 spec = noexcept_false_spec;
25589 }
25590
25591 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25592 if (orig_fn)
25593 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25594
25595 return true;
25596 }
25597
25598 /* We're starting to process the function INST, an instantiation of PATTERN;
25599 add their parameters to local_specializations. */
25600
25601 static void
25602 register_parameter_specializations (tree pattern, tree inst)
25603 {
25604 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25605 tree spec_parm = DECL_ARGUMENTS (inst);
25606 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25607 {
25608 register_local_specialization (spec_parm, tmpl_parm);
25609 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25610 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25611 }
25612 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25613 {
25614 if (!DECL_PACK_P (tmpl_parm)
25615 || (spec_parm && DECL_PACK_P (spec_parm)))
25616 {
25617 register_local_specialization (spec_parm, tmpl_parm);
25618 spec_parm = DECL_CHAIN (spec_parm);
25619 }
25620 else
25621 {
25622 /* Register the (value) argument pack as a specialization of
25623 TMPL_PARM, then move on. */
25624 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25625 register_local_specialization (argpack, tmpl_parm);
25626 }
25627 }
25628 gcc_assert (!spec_parm);
25629 }
25630
25631 /* Instantiate the body of D using PATTERN with ARGS. We have
25632 already determined PATTERN is the correct template to use.
25633 NESTED_P is true if this is a nested function, in which case
25634 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25635
25636 static void
25637 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25638 {
25639 tree td = NULL_TREE;
25640 tree code_pattern = pattern;
25641
25642 if (!nested_p)
25643 {
25644 td = pattern;
25645 code_pattern = DECL_TEMPLATE_RESULT (td);
25646 }
25647 else
25648 /* Only OMP reductions are nested. */
25649 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25650
25651 vec<tree> omp_privatization_save;
25652 if (current_function_decl)
25653 save_omp_privatization_clauses (omp_privatization_save);
25654
25655 bool push_to_top
25656 = !(current_function_decl
25657 && !LAMBDA_FUNCTION_P (d)
25658 && decl_function_context (d) == current_function_decl);
25659
25660 if (push_to_top)
25661 push_to_top_level ();
25662 else
25663 {
25664 gcc_assert (!processing_template_decl);
25665 push_function_context ();
25666 cp_unevaluated_operand = 0;
25667 c_inhibit_evaluation_warnings = 0;
25668 }
25669
25670 if (VAR_P (d))
25671 {
25672 /* The variable might be a lambda's extra scope, and that
25673 lambda's visibility depends on D's. */
25674 maybe_commonize_var (d);
25675 determine_visibility (d);
25676 }
25677
25678 /* Mark D as instantiated so that recursive calls to
25679 instantiate_decl do not try to instantiate it again. */
25680 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25681
25682 if (td)
25683 /* Regenerate the declaration in case the template has been modified
25684 by a subsequent redeclaration. */
25685 regenerate_decl_from_template (d, td, args);
25686
25687 /* We already set the file and line above. Reset them now in case
25688 they changed as a result of calling regenerate_decl_from_template. */
25689 input_location = DECL_SOURCE_LOCATION (d);
25690
25691 if (VAR_P (d))
25692 {
25693 /* Clear out DECL_RTL; whatever was there before may not be right
25694 since we've reset the type of the declaration. */
25695 SET_DECL_RTL (d, NULL);
25696 DECL_IN_AGGR_P (d) = 0;
25697
25698 /* The initializer is placed in DECL_INITIAL by
25699 regenerate_decl_from_template so we don't need to
25700 push/pop_access_scope again here. Pull it out so that
25701 cp_finish_decl can process it. */
25702 bool const_init = false;
25703 tree init = DECL_INITIAL (d);
25704 DECL_INITIAL (d) = NULL_TREE;
25705 DECL_INITIALIZED_P (d) = 0;
25706
25707 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25708 initializer. That function will defer actual emission until
25709 we have a chance to determine linkage. */
25710 DECL_EXTERNAL (d) = 0;
25711
25712 /* Enter the scope of D so that access-checking works correctly. */
25713 bool enter_context = DECL_CLASS_SCOPE_P (d);
25714 if (enter_context)
25715 push_nested_class (DECL_CONTEXT (d));
25716
25717 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25718 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25719
25720 if (enter_context)
25721 pop_nested_class ();
25722 }
25723 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25724 synthesize_method (d);
25725 else if (TREE_CODE (d) == FUNCTION_DECL)
25726 {
25727 /* Set up the list of local specializations. */
25728 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25729 tree block = NULL_TREE;
25730
25731 /* Set up context. */
25732 if (nested_p)
25733 block = push_stmt_list ();
25734 else
25735 {
25736 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25737
25738 perform_instantiation_time_access_checks (code_pattern, args);
25739 }
25740
25741 /* Create substitution entries for the parameters. */
25742 register_parameter_specializations (code_pattern, d);
25743
25744 /* Substitute into the body of the function. */
25745 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25746 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25747 tf_warning_or_error, d);
25748 else
25749 {
25750 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25751 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25752 /*integral_constant_expression_p=*/false);
25753
25754 /* Set the current input_location to the end of the function
25755 so that finish_function knows where we are. */
25756 input_location
25757 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25758
25759 /* Remember if we saw an infinite loop in the template. */
25760 current_function_infinite_loop
25761 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25762 }
25763
25764 /* Finish the function. */
25765 if (nested_p)
25766 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25767 else
25768 {
25769 d = finish_function (/*inline_p=*/false);
25770 expand_or_defer_fn (d);
25771 }
25772
25773 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25774 cp_check_omp_declare_reduction (d);
25775 }
25776
25777 /* We're not deferring instantiation any more. */
25778 if (!nested_p)
25779 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25780
25781 if (push_to_top)
25782 pop_from_top_level ();
25783 else
25784 pop_function_context ();
25785
25786 if (current_function_decl)
25787 restore_omp_privatization_clauses (omp_privatization_save);
25788 }
25789
25790 /* Produce the definition of D, a _DECL generated from a template. If
25791 DEFER_OK is true, then we don't have to actually do the
25792 instantiation now; we just have to do it sometime. Normally it is
25793 an error if this is an explicit instantiation but D is undefined.
25794 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25795 instantiated class template. */
25796
25797 tree
25798 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25799 {
25800 tree tmpl = DECL_TI_TEMPLATE (d);
25801 tree gen_args;
25802 tree args;
25803 tree td;
25804 tree code_pattern;
25805 tree spec;
25806 tree gen_tmpl;
25807 bool pattern_defined;
25808 location_t saved_loc = input_location;
25809 int saved_unevaluated_operand = cp_unevaluated_operand;
25810 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25811 bool external_p;
25812 bool deleted_p;
25813
25814 /* This function should only be used to instantiate templates for
25815 functions and static member variables. */
25816 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25817
25818 /* A concept is never instantiated. */
25819 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25820
25821 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25822
25823 /* Variables are never deferred; if instantiation is required, they
25824 are instantiated right away. That allows for better code in the
25825 case that an expression refers to the value of the variable --
25826 if the variable has a constant value the referring expression can
25827 take advantage of that fact. */
25828 if (VAR_P (d))
25829 defer_ok = false;
25830
25831 /* Don't instantiate cloned functions. Instead, instantiate the
25832 functions they cloned. */
25833 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25834 d = DECL_CLONED_FUNCTION (d);
25835
25836 if (DECL_TEMPLATE_INSTANTIATED (d)
25837 || TREE_TYPE (d) == error_mark_node
25838 || (TREE_CODE (d) == FUNCTION_DECL
25839 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25840 || DECL_TEMPLATE_SPECIALIZATION (d))
25841 /* D has already been instantiated or explicitly specialized, so
25842 there's nothing for us to do here.
25843
25844 It might seem reasonable to check whether or not D is an explicit
25845 instantiation, and, if so, stop here. But when an explicit
25846 instantiation is deferred until the end of the compilation,
25847 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25848 the instantiation. */
25849 return d;
25850
25851 /* Check to see whether we know that this template will be
25852 instantiated in some other file, as with "extern template"
25853 extension. */
25854 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25855
25856 /* In general, we do not instantiate such templates. */
25857 if (external_p && !always_instantiate_p (d))
25858 return d;
25859
25860 gen_tmpl = most_general_template (tmpl);
25861 gen_args = DECL_TI_ARGS (d);
25862
25863 /* We should already have the extra args. */
25864 gcc_checking_assert (tmpl == gen_tmpl
25865 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25866 == TMPL_ARGS_DEPTH (gen_args)));
25867 /* And what's in the hash table should match D. */
25868 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25869 == d
25870 || spec == NULL_TREE);
25871
25872 /* This needs to happen before any tsubsting. */
25873 if (! push_tinst_level (d))
25874 return d;
25875
25876 timevar_push (TV_TEMPLATE_INST);
25877
25878 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25879 for the instantiation. */
25880 td = template_for_substitution (d);
25881 args = gen_args;
25882
25883 if (VAR_P (d))
25884 {
25885 /* Look up an explicit specialization, if any. */
25886 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25887 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25888 if (elt && elt != error_mark_node)
25889 {
25890 td = TREE_VALUE (elt);
25891 args = TREE_PURPOSE (elt);
25892 }
25893 }
25894
25895 code_pattern = DECL_TEMPLATE_RESULT (td);
25896
25897 /* We should never be trying to instantiate a member of a class
25898 template or partial specialization. */
25899 gcc_assert (d != code_pattern);
25900
25901 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25902 || DECL_TEMPLATE_SPECIALIZATION (td))
25903 /* In the case of a friend template whose definition is provided
25904 outside the class, we may have too many arguments. Drop the
25905 ones we don't need. The same is true for specializations. */
25906 args = get_innermost_template_args
25907 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25908
25909 if (TREE_CODE (d) == FUNCTION_DECL)
25910 {
25911 deleted_p = DECL_DELETED_FN (code_pattern);
25912 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25913 && DECL_INITIAL (code_pattern) != error_mark_node)
25914 || DECL_DEFAULTED_FN (code_pattern)
25915 || deleted_p);
25916 }
25917 else
25918 {
25919 deleted_p = false;
25920 if (DECL_CLASS_SCOPE_P (code_pattern))
25921 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25922 else
25923 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25924 }
25925
25926 /* We may be in the middle of deferred access check. Disable it now. */
25927 push_deferring_access_checks (dk_no_deferred);
25928
25929 /* Unless an explicit instantiation directive has already determined
25930 the linkage of D, remember that a definition is available for
25931 this entity. */
25932 if (pattern_defined
25933 && !DECL_INTERFACE_KNOWN (d)
25934 && !DECL_NOT_REALLY_EXTERN (d))
25935 mark_definable (d);
25936
25937 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25938 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25939 input_location = DECL_SOURCE_LOCATION (d);
25940
25941 /* If D is a member of an explicitly instantiated class template,
25942 and no definition is available, treat it like an implicit
25943 instantiation. */
25944 if (!pattern_defined && expl_inst_class_mem_p
25945 && DECL_EXPLICIT_INSTANTIATION (d))
25946 {
25947 /* Leave linkage flags alone on instantiations with anonymous
25948 visibility. */
25949 if (TREE_PUBLIC (d))
25950 {
25951 DECL_NOT_REALLY_EXTERN (d) = 0;
25952 DECL_INTERFACE_KNOWN (d) = 0;
25953 }
25954 SET_DECL_IMPLICIT_INSTANTIATION (d);
25955 }
25956
25957 /* Defer all other templates, unless we have been explicitly
25958 forbidden from doing so. */
25959 if (/* If there is no definition, we cannot instantiate the
25960 template. */
25961 ! pattern_defined
25962 /* If it's OK to postpone instantiation, do so. */
25963 || defer_ok
25964 /* If this is a static data member that will be defined
25965 elsewhere, we don't want to instantiate the entire data
25966 member, but we do want to instantiate the initializer so that
25967 we can substitute that elsewhere. */
25968 || (external_p && VAR_P (d))
25969 /* Handle here a deleted function too, avoid generating
25970 its body (c++/61080). */
25971 || deleted_p)
25972 {
25973 /* The definition of the static data member is now required so
25974 we must substitute the initializer. */
25975 if (VAR_P (d)
25976 && !DECL_INITIAL (d)
25977 && DECL_INITIAL (code_pattern))
25978 {
25979 tree ns;
25980 tree init;
25981 bool const_init = false;
25982 bool enter_context = DECL_CLASS_SCOPE_P (d);
25983
25984 ns = decl_namespace_context (d);
25985 push_nested_namespace (ns);
25986 if (enter_context)
25987 push_nested_class (DECL_CONTEXT (d));
25988 init = tsubst_expr (DECL_INITIAL (code_pattern),
25989 args,
25990 tf_warning_or_error, NULL_TREE,
25991 /*integral_constant_expression_p=*/false);
25992 /* If instantiating the initializer involved instantiating this
25993 again, don't call cp_finish_decl twice. */
25994 if (!DECL_INITIAL (d))
25995 {
25996 /* Make sure the initializer is still constant, in case of
25997 circular dependency (template/instantiate6.C). */
25998 const_init
25999 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26000 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26001 /*asmspec_tree=*/NULL_TREE,
26002 LOOKUP_ONLYCONVERTING);
26003 }
26004 if (enter_context)
26005 pop_nested_class ();
26006 pop_nested_namespace (ns);
26007 }
26008
26009 /* We restore the source position here because it's used by
26010 add_pending_template. */
26011 input_location = saved_loc;
26012
26013 if (at_eof && !pattern_defined
26014 && DECL_EXPLICIT_INSTANTIATION (d)
26015 && DECL_NOT_REALLY_EXTERN (d))
26016 /* [temp.explicit]
26017
26018 The definition of a non-exported function template, a
26019 non-exported member function template, or a non-exported
26020 member function or static data member of a class template
26021 shall be present in every translation unit in which it is
26022 explicitly instantiated. */
26023 permerror (input_location, "explicit instantiation of %qD "
26024 "but no definition available", d);
26025
26026 /* If we're in unevaluated context, we just wanted to get the
26027 constant value; this isn't an odr use, so don't queue
26028 a full instantiation. */
26029 if (!cp_unevaluated_operand
26030 /* ??? Historically, we have instantiated inline functions, even
26031 when marked as "extern template". */
26032 && !(external_p && VAR_P (d)))
26033 add_pending_template (d);
26034 }
26035 else
26036 {
26037 if (variable_template_p (gen_tmpl))
26038 note_variable_template_instantiation (d);
26039 instantiate_body (td, args, d, false);
26040 }
26041
26042 pop_deferring_access_checks ();
26043 timevar_pop (TV_TEMPLATE_INST);
26044 pop_tinst_level ();
26045 input_location = saved_loc;
26046 cp_unevaluated_operand = saved_unevaluated_operand;
26047 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26048
26049 return d;
26050 }
26051
26052 /* Run through the list of templates that we wish we could
26053 instantiate, and instantiate any we can. RETRIES is the
26054 number of times we retry pending template instantiation. */
26055
26056 void
26057 instantiate_pending_templates (int retries)
26058 {
26059 int reconsider;
26060 location_t saved_loc = input_location;
26061
26062 /* Instantiating templates may trigger vtable generation. This in turn
26063 may require further template instantiations. We place a limit here
26064 to avoid infinite loop. */
26065 if (pending_templates && retries >= max_tinst_depth)
26066 {
26067 tree decl = pending_templates->tinst->maybe_get_node ();
26068
26069 fatal_error (input_location,
26070 "template instantiation depth exceeds maximum of %d"
26071 " instantiating %q+D, possibly from virtual table generation"
26072 " (use %<-ftemplate-depth=%> to increase the maximum)",
26073 max_tinst_depth, decl);
26074 if (TREE_CODE (decl) == FUNCTION_DECL)
26075 /* Pretend that we defined it. */
26076 DECL_INITIAL (decl) = error_mark_node;
26077 return;
26078 }
26079
26080 do
26081 {
26082 struct pending_template **t = &pending_templates;
26083 struct pending_template *last = NULL;
26084 reconsider = 0;
26085 while (*t)
26086 {
26087 tree instantiation = reopen_tinst_level ((*t)->tinst);
26088 bool complete = false;
26089
26090 if (TYPE_P (instantiation))
26091 {
26092 if (!COMPLETE_TYPE_P (instantiation))
26093 {
26094 instantiate_class_template (instantiation);
26095 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26096 for (tree fld = TYPE_FIELDS (instantiation);
26097 fld; fld = TREE_CHAIN (fld))
26098 if ((VAR_P (fld)
26099 || (TREE_CODE (fld) == FUNCTION_DECL
26100 && !DECL_ARTIFICIAL (fld)))
26101 && DECL_TEMPLATE_INSTANTIATION (fld))
26102 instantiate_decl (fld,
26103 /*defer_ok=*/false,
26104 /*expl_inst_class_mem_p=*/false);
26105
26106 if (COMPLETE_TYPE_P (instantiation))
26107 reconsider = 1;
26108 }
26109
26110 complete = COMPLETE_TYPE_P (instantiation);
26111 }
26112 else
26113 {
26114 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26115 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26116 {
26117 instantiation
26118 = instantiate_decl (instantiation,
26119 /*defer_ok=*/false,
26120 /*expl_inst_class_mem_p=*/false);
26121 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26122 reconsider = 1;
26123 }
26124
26125 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26126 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26127 }
26128
26129 if (complete)
26130 {
26131 /* If INSTANTIATION has been instantiated, then we don't
26132 need to consider it again in the future. */
26133 struct pending_template *drop = *t;
26134 *t = (*t)->next;
26135 set_refcount_ptr (drop->tinst);
26136 pending_template_freelist ().free (drop);
26137 }
26138 else
26139 {
26140 last = *t;
26141 t = &(*t)->next;
26142 }
26143 tinst_depth = 0;
26144 set_refcount_ptr (current_tinst_level);
26145 }
26146 last_pending_template = last;
26147 }
26148 while (reconsider);
26149
26150 input_location = saved_loc;
26151 }
26152
26153 /* Substitute ARGVEC into T, which is a list of initializers for
26154 either base class or a non-static data member. The TREE_PURPOSEs
26155 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26156 instantiate_decl. */
26157
26158 static tree
26159 tsubst_initializer_list (tree t, tree argvec)
26160 {
26161 tree inits = NULL_TREE;
26162 tree target_ctor = error_mark_node;
26163
26164 for (; t; t = TREE_CHAIN (t))
26165 {
26166 tree decl;
26167 tree init;
26168 tree expanded_bases = NULL_TREE;
26169 tree expanded_arguments = NULL_TREE;
26170 int i, len = 1;
26171
26172 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26173 {
26174 tree expr;
26175 tree arg;
26176
26177 /* Expand the base class expansion type into separate base
26178 classes. */
26179 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26180 tf_warning_or_error,
26181 NULL_TREE);
26182 if (expanded_bases == error_mark_node)
26183 continue;
26184
26185 /* We'll be building separate TREE_LISTs of arguments for
26186 each base. */
26187 len = TREE_VEC_LENGTH (expanded_bases);
26188 expanded_arguments = make_tree_vec (len);
26189 for (i = 0; i < len; i++)
26190 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26191
26192 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26193 expand each argument in the TREE_VALUE of t. */
26194 expr = make_node (EXPR_PACK_EXPANSION);
26195 PACK_EXPANSION_LOCAL_P (expr) = true;
26196 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26197 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26198
26199 if (TREE_VALUE (t) == void_type_node)
26200 /* VOID_TYPE_NODE is used to indicate
26201 value-initialization. */
26202 {
26203 for (i = 0; i < len; i++)
26204 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26205 }
26206 else
26207 {
26208 /* Substitute parameter packs into each argument in the
26209 TREE_LIST. */
26210 in_base_initializer = 1;
26211 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26212 {
26213 tree expanded_exprs;
26214
26215 /* Expand the argument. */
26216 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26217 expanded_exprs
26218 = tsubst_pack_expansion (expr, argvec,
26219 tf_warning_or_error,
26220 NULL_TREE);
26221 if (expanded_exprs == error_mark_node)
26222 continue;
26223
26224 /* Prepend each of the expanded expressions to the
26225 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26226 for (i = 0; i < len; i++)
26227 {
26228 TREE_VEC_ELT (expanded_arguments, i) =
26229 tree_cons (NULL_TREE,
26230 TREE_VEC_ELT (expanded_exprs, i),
26231 TREE_VEC_ELT (expanded_arguments, i));
26232 }
26233 }
26234 in_base_initializer = 0;
26235
26236 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26237 since we built them backwards. */
26238 for (i = 0; i < len; i++)
26239 {
26240 TREE_VEC_ELT (expanded_arguments, i) =
26241 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26242 }
26243 }
26244 }
26245
26246 for (i = 0; i < len; ++i)
26247 {
26248 if (expanded_bases)
26249 {
26250 decl = TREE_VEC_ELT (expanded_bases, i);
26251 decl = expand_member_init (decl);
26252 init = TREE_VEC_ELT (expanded_arguments, i);
26253 }
26254 else
26255 {
26256 tree tmp;
26257 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26258 tf_warning_or_error, NULL_TREE);
26259
26260 decl = expand_member_init (decl);
26261 if (decl && !DECL_P (decl))
26262 in_base_initializer = 1;
26263
26264 init = TREE_VALUE (t);
26265 tmp = init;
26266 if (init != void_type_node)
26267 init = tsubst_expr (init, argvec,
26268 tf_warning_or_error, NULL_TREE,
26269 /*integral_constant_expression_p=*/false);
26270 if (init == NULL_TREE && tmp != NULL_TREE)
26271 /* If we had an initializer but it instantiated to nothing,
26272 value-initialize the object. This will only occur when
26273 the initializer was a pack expansion where the parameter
26274 packs used in that expansion were of length zero. */
26275 init = void_type_node;
26276 in_base_initializer = 0;
26277 }
26278
26279 if (target_ctor != error_mark_node
26280 && init != error_mark_node)
26281 {
26282 error ("mem-initializer for %qD follows constructor delegation",
26283 decl);
26284 return inits;
26285 }
26286 /* Look for a target constructor. */
26287 if (init != error_mark_node
26288 && decl && CLASS_TYPE_P (decl)
26289 && same_type_p (decl, current_class_type))
26290 {
26291 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26292 if (inits)
26293 {
26294 error ("constructor delegation follows mem-initializer for %qD",
26295 TREE_PURPOSE (inits));
26296 continue;
26297 }
26298 target_ctor = init;
26299 }
26300
26301 if (decl)
26302 {
26303 init = build_tree_list (decl, init);
26304 /* Carry over the dummy TREE_TYPE node containing the source
26305 location. */
26306 TREE_TYPE (init) = TREE_TYPE (t);
26307 TREE_CHAIN (init) = inits;
26308 inits = init;
26309 }
26310 }
26311 }
26312 return inits;
26313 }
26314
26315 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26316
26317 static void
26318 set_current_access_from_decl (tree decl)
26319 {
26320 if (TREE_PRIVATE (decl))
26321 current_access_specifier = access_private_node;
26322 else if (TREE_PROTECTED (decl))
26323 current_access_specifier = access_protected_node;
26324 else
26325 current_access_specifier = access_public_node;
26326 }
26327
26328 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26329 is the instantiation (which should have been created with
26330 start_enum) and ARGS are the template arguments to use. */
26331
26332 static void
26333 tsubst_enum (tree tag, tree newtag, tree args)
26334 {
26335 tree e;
26336
26337 if (SCOPED_ENUM_P (newtag))
26338 begin_scope (sk_scoped_enum, newtag);
26339
26340 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26341 {
26342 tree value;
26343 tree decl;
26344
26345 decl = TREE_VALUE (e);
26346 /* Note that in a template enum, the TREE_VALUE is the
26347 CONST_DECL, not the corresponding INTEGER_CST. */
26348 value = tsubst_expr (DECL_INITIAL (decl),
26349 args, tf_warning_or_error, NULL_TREE,
26350 /*integral_constant_expression_p=*/true);
26351
26352 /* Give this enumeration constant the correct access. */
26353 set_current_access_from_decl (decl);
26354
26355 /* Actually build the enumerator itself. Here we're assuming that
26356 enumerators can't have dependent attributes. */
26357 build_enumerator (DECL_NAME (decl), value, newtag,
26358 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26359 }
26360
26361 if (SCOPED_ENUM_P (newtag))
26362 finish_scope ();
26363
26364 finish_enum_value_list (newtag);
26365 finish_enum (newtag);
26366
26367 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26368 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26369 }
26370
26371 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26372 its type -- but without substituting the innermost set of template
26373 arguments. So, innermost set of template parameters will appear in
26374 the type. */
26375
26376 tree
26377 get_mostly_instantiated_function_type (tree decl)
26378 {
26379 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26380 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26381 }
26382
26383 /* Return truthvalue if we're processing a template different from
26384 the last one involved in diagnostics. */
26385 bool
26386 problematic_instantiation_changed (void)
26387 {
26388 return current_tinst_level != last_error_tinst_level;
26389 }
26390
26391 /* Remember current template involved in diagnostics. */
26392 void
26393 record_last_problematic_instantiation (void)
26394 {
26395 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26396 }
26397
26398 struct tinst_level *
26399 current_instantiation (void)
26400 {
26401 return current_tinst_level;
26402 }
26403
26404 /* Return TRUE if current_function_decl is being instantiated, false
26405 otherwise. */
26406
26407 bool
26408 instantiating_current_function_p (void)
26409 {
26410 return (current_instantiation ()
26411 && (current_instantiation ()->maybe_get_node ()
26412 == current_function_decl));
26413 }
26414
26415 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26416 type. Return false for ok, true for disallowed. Issue error and
26417 inform messages under control of COMPLAIN. */
26418
26419 static bool
26420 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26421 {
26422 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26423 return false;
26424 else if (TYPE_PTR_P (type))
26425 return false;
26426 else if (TYPE_REF_P (type)
26427 && !TYPE_REF_IS_RVALUE (type))
26428 return false;
26429 else if (TYPE_PTRMEM_P (type))
26430 return false;
26431 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26432 {
26433 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26434 {
26435 if (complain & tf_error)
26436 error ("non-type template parameters of deduced class type only "
26437 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26438 return true;
26439 }
26440 return false;
26441 }
26442 else if (TREE_CODE (type) == TYPENAME_TYPE)
26443 return false;
26444 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26445 return false;
26446 else if (TREE_CODE (type) == NULLPTR_TYPE)
26447 return false;
26448 /* A bound template template parm could later be instantiated to have a valid
26449 nontype parm type via an alias template. */
26450 else if (cxx_dialect >= cxx11
26451 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26452 return false;
26453 else if (VOID_TYPE_P (type))
26454 /* Fall through. */;
26455 else if (cxx_dialect >= cxx20)
26456 {
26457 if (dependent_type_p (type))
26458 return false;
26459 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26460 return true;
26461 if (structural_type_p (type))
26462 return false;
26463 if (complain & tf_error)
26464 {
26465 auto_diagnostic_group d;
26466 error ("%qT is not a valid type for a template non-type "
26467 "parameter because it is not structural", type);
26468 structural_type_p (type, true);
26469 }
26470 return true;
26471 }
26472 else if (CLASS_TYPE_P (type))
26473 {
26474 if (complain & tf_error)
26475 error ("non-type template parameters of class type only available "
26476 "with %<-std=c++20%> or %<-std=gnu++20%>");
26477 return true;
26478 }
26479
26480 if (complain & tf_error)
26481 {
26482 if (type == error_mark_node)
26483 inform (input_location, "invalid template non-type parameter");
26484 else
26485 error ("%q#T is not a valid type for a template non-type parameter",
26486 type);
26487 }
26488 return true;
26489 }
26490
26491 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26492 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26493
26494 static bool
26495 dependent_type_p_r (tree type)
26496 {
26497 tree scope;
26498
26499 /* [temp.dep.type]
26500
26501 A type is dependent if it is:
26502
26503 -- a template parameter. Template template parameters are types
26504 for us (since TYPE_P holds true for them) so we handle
26505 them here. */
26506 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26507 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26508 return true;
26509 /* -- a qualified-id with a nested-name-specifier which contains a
26510 class-name that names a dependent type or whose unqualified-id
26511 names a dependent type. */
26512 if (TREE_CODE (type) == TYPENAME_TYPE)
26513 return true;
26514
26515 /* An alias template specialization can be dependent even if the
26516 resulting type is not. */
26517 if (dependent_alias_template_spec_p (type, nt_transparent))
26518 return true;
26519
26520 /* -- a cv-qualified type where the cv-unqualified type is
26521 dependent.
26522 No code is necessary for this bullet; the code below handles
26523 cv-qualified types, and we don't want to strip aliases with
26524 TYPE_MAIN_VARIANT because of DR 1558. */
26525 /* -- a compound type constructed from any dependent type. */
26526 if (TYPE_PTRMEM_P (type))
26527 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26528 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26529 (type)));
26530 else if (INDIRECT_TYPE_P (type))
26531 return dependent_type_p (TREE_TYPE (type));
26532 else if (FUNC_OR_METHOD_TYPE_P (type))
26533 {
26534 tree arg_type;
26535
26536 if (dependent_type_p (TREE_TYPE (type)))
26537 return true;
26538 for (arg_type = TYPE_ARG_TYPES (type);
26539 arg_type;
26540 arg_type = TREE_CHAIN (arg_type))
26541 if (dependent_type_p (TREE_VALUE (arg_type)))
26542 return true;
26543 if (cxx_dialect >= cxx17)
26544 /* A value-dependent noexcept-specifier makes the type dependent. */
26545 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26546 if (tree noex = TREE_PURPOSE (spec))
26547 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26548 affect overload resolution and treating it as dependent breaks
26549 things. Same for an unparsed noexcept expression. */
26550 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26551 && TREE_CODE (noex) != DEFERRED_PARSE
26552 && value_dependent_expression_p (noex))
26553 return true;
26554 return false;
26555 }
26556 /* -- an array type constructed from any dependent type or whose
26557 size is specified by a constant expression that is
26558 value-dependent.
26559
26560 We checked for type- and value-dependence of the bounds in
26561 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26562 if (TREE_CODE (type) == ARRAY_TYPE)
26563 {
26564 if (TYPE_DOMAIN (type)
26565 && dependent_type_p (TYPE_DOMAIN (type)))
26566 return true;
26567 return dependent_type_p (TREE_TYPE (type));
26568 }
26569
26570 /* -- a template-id in which either the template name is a template
26571 parameter ... */
26572 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26573 return true;
26574 /* ... or any of the template arguments is a dependent type or
26575 an expression that is type-dependent or value-dependent. */
26576 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26577 && (any_dependent_template_arguments_p
26578 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26579 return true;
26580
26581 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26582 dependent; if the argument of the `typeof' expression is not
26583 type-dependent, then it should already been have resolved. */
26584 if (TREE_CODE (type) == TYPEOF_TYPE
26585 || TREE_CODE (type) == DECLTYPE_TYPE
26586 || TREE_CODE (type) == UNDERLYING_TYPE)
26587 return true;
26588
26589 /* A template argument pack is dependent if any of its packed
26590 arguments are. */
26591 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26592 {
26593 tree args = ARGUMENT_PACK_ARGS (type);
26594 int i, len = TREE_VEC_LENGTH (args);
26595 for (i = 0; i < len; ++i)
26596 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26597 return true;
26598 }
26599
26600 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26601 be template parameters. */
26602 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26603 return true;
26604
26605 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26606 return true;
26607
26608 /* The standard does not specifically mention types that are local
26609 to template functions or local classes, but they should be
26610 considered dependent too. For example:
26611
26612 template <int I> void f() {
26613 enum E { a = I };
26614 S<sizeof (E)> s;
26615 }
26616
26617 The size of `E' cannot be known until the value of `I' has been
26618 determined. Therefore, `E' must be considered dependent. */
26619 scope = TYPE_CONTEXT (type);
26620 if (scope && TYPE_P (scope))
26621 return dependent_type_p (scope);
26622 /* Don't use type_dependent_expression_p here, as it can lead
26623 to infinite recursion trying to determine whether a lambda
26624 nested in a lambda is dependent (c++/47687). */
26625 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26626 && DECL_LANG_SPECIFIC (scope)
26627 && DECL_TEMPLATE_INFO (scope)
26628 && (any_dependent_template_arguments_p
26629 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26630 return true;
26631
26632 /* Other types are non-dependent. */
26633 return false;
26634 }
26635
26636 /* Returns TRUE if TYPE is dependent, in the sense of
26637 [temp.dep.type]. Note that a NULL type is considered dependent. */
26638
26639 bool
26640 dependent_type_p (tree type)
26641 {
26642 /* If there are no template parameters in scope, then there can't be
26643 any dependent types. */
26644 if (!processing_template_decl)
26645 {
26646 /* If we are not processing a template, then nobody should be
26647 providing us with a dependent type. */
26648 gcc_assert (type);
26649 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26650 return false;
26651 }
26652
26653 /* If the type is NULL, we have not computed a type for the entity
26654 in question; in that case, the type is dependent. */
26655 if (!type)
26656 return true;
26657
26658 /* Erroneous types can be considered non-dependent. */
26659 if (type == error_mark_node)
26660 return false;
26661
26662 /* Getting here with global_type_node means we improperly called this
26663 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26664 gcc_checking_assert (type != global_type_node);
26665
26666 /* If we have not already computed the appropriate value for TYPE,
26667 do so now. */
26668 if (!TYPE_DEPENDENT_P_VALID (type))
26669 {
26670 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26671 TYPE_DEPENDENT_P_VALID (type) = 1;
26672 }
26673
26674 return TYPE_DEPENDENT_P (type);
26675 }
26676
26677 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26678 lookup. In other words, a dependent type that is not the current
26679 instantiation. */
26680
26681 bool
26682 dependent_scope_p (tree scope)
26683 {
26684 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26685 && !currently_open_class (scope));
26686 }
26687
26688 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26689 an unknown base of 'this' (and is therefore instantiation-dependent). */
26690
26691 static bool
26692 unknown_base_ref_p (tree t)
26693 {
26694 if (!current_class_ptr)
26695 return false;
26696
26697 tree mem = TREE_OPERAND (t, 1);
26698 if (shared_member_p (mem))
26699 return false;
26700
26701 tree cur = current_nonlambda_class_type ();
26702 if (!any_dependent_bases_p (cur))
26703 return false;
26704
26705 tree ctx = TREE_OPERAND (t, 0);
26706 if (DERIVED_FROM_P (ctx, cur))
26707 return false;
26708
26709 return true;
26710 }
26711
26712 /* T is a SCOPE_REF; return whether we need to consider it
26713 instantiation-dependent so that we can check access at instantiation
26714 time even though we know which member it resolves to. */
26715
26716 static bool
26717 instantiation_dependent_scope_ref_p (tree t)
26718 {
26719 if (DECL_P (TREE_OPERAND (t, 1))
26720 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26721 && !unknown_base_ref_p (t)
26722 && accessible_in_template_p (TREE_OPERAND (t, 0),
26723 TREE_OPERAND (t, 1)))
26724 return false;
26725 else
26726 return true;
26727 }
26728
26729 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26730 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26731 expression. */
26732
26733 /* Note that this predicate is not appropriate for general expressions;
26734 only constant expressions (that satisfy potential_constant_expression)
26735 can be tested for value dependence. */
26736
26737 bool
26738 value_dependent_expression_p (tree expression)
26739 {
26740 if (!processing_template_decl || expression == NULL_TREE)
26741 return false;
26742
26743 /* A type-dependent expression is also value-dependent. */
26744 if (type_dependent_expression_p (expression))
26745 return true;
26746
26747 switch (TREE_CODE (expression))
26748 {
26749 case BASELINK:
26750 /* A dependent member function of the current instantiation. */
26751 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26752
26753 case FUNCTION_DECL:
26754 /* A dependent member function of the current instantiation. */
26755 if (DECL_CLASS_SCOPE_P (expression)
26756 && dependent_type_p (DECL_CONTEXT (expression)))
26757 return true;
26758 break;
26759
26760 case IDENTIFIER_NODE:
26761 /* A name that has not been looked up -- must be dependent. */
26762 return true;
26763
26764 case TEMPLATE_PARM_INDEX:
26765 /* A non-type template parm. */
26766 return true;
26767
26768 case CONST_DECL:
26769 /* A non-type template parm. */
26770 if (DECL_TEMPLATE_PARM_P (expression))
26771 return true;
26772 return value_dependent_expression_p (DECL_INITIAL (expression));
26773
26774 case VAR_DECL:
26775 /* A constant with literal type and is initialized
26776 with an expression that is value-dependent. */
26777 if (DECL_DEPENDENT_INIT_P (expression)
26778 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26779 || TYPE_REF_P (TREE_TYPE (expression)))
26780 return true;
26781 if (DECL_HAS_VALUE_EXPR_P (expression))
26782 {
26783 tree value_expr = DECL_VALUE_EXPR (expression);
26784 if (value_dependent_expression_p (value_expr)
26785 /* __PRETTY_FUNCTION__ inside a template function is dependent
26786 on the name of the function. */
26787 || (DECL_PRETTY_FUNCTION_P (expression)
26788 /* It might be used in a template, but not a template
26789 function, in which case its DECL_VALUE_EXPR will be
26790 "top level". */
26791 && value_expr == error_mark_node))
26792 return true;
26793 }
26794 return false;
26795
26796 case DYNAMIC_CAST_EXPR:
26797 case STATIC_CAST_EXPR:
26798 case CONST_CAST_EXPR:
26799 case REINTERPRET_CAST_EXPR:
26800 case CAST_EXPR:
26801 case IMPLICIT_CONV_EXPR:
26802 /* These expressions are value-dependent if the type to which
26803 the cast occurs is dependent or the expression being casted
26804 is value-dependent. */
26805 {
26806 tree type = TREE_TYPE (expression);
26807
26808 if (dependent_type_p (type))
26809 return true;
26810
26811 /* A functional cast has a list of operands. */
26812 expression = TREE_OPERAND (expression, 0);
26813 if (!expression)
26814 {
26815 /* If there are no operands, it must be an expression such
26816 as "int()". This should not happen for aggregate types
26817 because it would form non-constant expressions. */
26818 gcc_assert (cxx_dialect >= cxx11
26819 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26820
26821 return false;
26822 }
26823
26824 if (TREE_CODE (expression) == TREE_LIST)
26825 return any_value_dependent_elements_p (expression);
26826
26827 return value_dependent_expression_p (expression);
26828 }
26829
26830 case SIZEOF_EXPR:
26831 if (SIZEOF_EXPR_TYPE_P (expression))
26832 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26833 /* FALLTHRU */
26834 case ALIGNOF_EXPR:
26835 case TYPEID_EXPR:
26836 /* A `sizeof' expression is value-dependent if the operand is
26837 type-dependent or is a pack expansion. */
26838 expression = TREE_OPERAND (expression, 0);
26839 if (PACK_EXPANSION_P (expression))
26840 return true;
26841 else if (TYPE_P (expression))
26842 return dependent_type_p (expression);
26843 return instantiation_dependent_uneval_expression_p (expression);
26844
26845 case AT_ENCODE_EXPR:
26846 /* An 'encode' expression is value-dependent if the operand is
26847 type-dependent. */
26848 expression = TREE_OPERAND (expression, 0);
26849 return dependent_type_p (expression);
26850
26851 case NOEXCEPT_EXPR:
26852 expression = TREE_OPERAND (expression, 0);
26853 return instantiation_dependent_uneval_expression_p (expression);
26854
26855 case SCOPE_REF:
26856 /* All instantiation-dependent expressions should also be considered
26857 value-dependent. */
26858 return instantiation_dependent_scope_ref_p (expression);
26859
26860 case COMPONENT_REF:
26861 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26862 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26863
26864 case NONTYPE_ARGUMENT_PACK:
26865 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26866 is value-dependent. */
26867 {
26868 tree values = ARGUMENT_PACK_ARGS (expression);
26869 int i, len = TREE_VEC_LENGTH (values);
26870
26871 for (i = 0; i < len; ++i)
26872 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26873 return true;
26874
26875 return false;
26876 }
26877
26878 case TRAIT_EXPR:
26879 {
26880 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26881
26882 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26883 return true;
26884
26885 if (!type2)
26886 return false;
26887
26888 if (TREE_CODE (type2) != TREE_LIST)
26889 return dependent_type_p (type2);
26890
26891 for (; type2; type2 = TREE_CHAIN (type2))
26892 if (dependent_type_p (TREE_VALUE (type2)))
26893 return true;
26894
26895 return false;
26896 }
26897
26898 case MODOP_EXPR:
26899 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26900 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26901
26902 case ARRAY_REF:
26903 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26904 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26905
26906 case ADDR_EXPR:
26907 {
26908 tree op = TREE_OPERAND (expression, 0);
26909 return (value_dependent_expression_p (op)
26910 || has_value_dependent_address (op));
26911 }
26912
26913 case REQUIRES_EXPR:
26914 /* Treat all requires-expressions as value-dependent so
26915 we don't try to fold them. */
26916 return true;
26917
26918 case TYPE_REQ:
26919 return dependent_type_p (TREE_OPERAND (expression, 0));
26920
26921 case CALL_EXPR:
26922 {
26923 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26924 return true;
26925 tree fn = get_callee_fndecl (expression);
26926 int i, nargs;
26927 nargs = call_expr_nargs (expression);
26928 for (i = 0; i < nargs; ++i)
26929 {
26930 tree op = CALL_EXPR_ARG (expression, i);
26931 /* In a call to a constexpr member function, look through the
26932 implicit ADDR_EXPR on the object argument so that it doesn't
26933 cause the call to be considered value-dependent. We also
26934 look through it in potential_constant_expression. */
26935 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26936 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26937 && TREE_CODE (op) == ADDR_EXPR)
26938 op = TREE_OPERAND (op, 0);
26939 if (value_dependent_expression_p (op))
26940 return true;
26941 }
26942 return false;
26943 }
26944
26945 case TEMPLATE_ID_EXPR:
26946 return concept_definition_p (TREE_OPERAND (expression, 0));
26947
26948 case CONSTRUCTOR:
26949 {
26950 unsigned ix;
26951 tree val;
26952 if (dependent_type_p (TREE_TYPE (expression)))
26953 return true;
26954 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26955 if (value_dependent_expression_p (val))
26956 return true;
26957 return false;
26958 }
26959
26960 case STMT_EXPR:
26961 /* Treat a GNU statement expression as dependent to avoid crashing
26962 under instantiate_non_dependent_expr; it can't be constant. */
26963 return true;
26964
26965 default:
26966 /* A constant expression is value-dependent if any subexpression is
26967 value-dependent. */
26968 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26969 {
26970 case tcc_reference:
26971 case tcc_unary:
26972 case tcc_comparison:
26973 case tcc_binary:
26974 case tcc_expression:
26975 case tcc_vl_exp:
26976 {
26977 int i, len = cp_tree_operand_length (expression);
26978
26979 for (i = 0; i < len; i++)
26980 {
26981 tree t = TREE_OPERAND (expression, i);
26982
26983 /* In some cases, some of the operands may be missing.
26984 (For example, in the case of PREDECREMENT_EXPR, the
26985 amount to increment by may be missing.) That doesn't
26986 make the expression dependent. */
26987 if (t && value_dependent_expression_p (t))
26988 return true;
26989 }
26990 }
26991 break;
26992 default:
26993 break;
26994 }
26995 break;
26996 }
26997
26998 /* The expression is not value-dependent. */
26999 return false;
27000 }
27001
27002 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27003 [temp.dep.expr]. Note that an expression with no type is
27004 considered dependent. Other parts of the compiler arrange for an
27005 expression with type-dependent subexpressions to have no type, so
27006 this function doesn't have to be fully recursive. */
27007
27008 bool
27009 type_dependent_expression_p (tree expression)
27010 {
27011 if (!processing_template_decl)
27012 return false;
27013
27014 if (expression == NULL_TREE || expression == error_mark_node)
27015 return false;
27016
27017 STRIP_ANY_LOCATION_WRAPPER (expression);
27018
27019 /* An unresolved name is always dependent. */
27020 if (identifier_p (expression)
27021 || TREE_CODE (expression) == USING_DECL
27022 || TREE_CODE (expression) == WILDCARD_DECL)
27023 return true;
27024
27025 /* A lambda-expression in template context is dependent. dependent_type_p is
27026 true for a lambda in the scope of a class or function template, but that
27027 doesn't cover all template contexts, like a default template argument. */
27028 if (TREE_CODE (expression) == LAMBDA_EXPR)
27029 return true;
27030
27031 /* A fold expression is type-dependent. */
27032 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27033 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27034 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27035 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27036 return true;
27037
27038 /* Some expression forms are never type-dependent. */
27039 if (TREE_CODE (expression) == SIZEOF_EXPR
27040 || TREE_CODE (expression) == ALIGNOF_EXPR
27041 || TREE_CODE (expression) == AT_ENCODE_EXPR
27042 || TREE_CODE (expression) == NOEXCEPT_EXPR
27043 || TREE_CODE (expression) == TRAIT_EXPR
27044 || TREE_CODE (expression) == TYPEID_EXPR
27045 || TREE_CODE (expression) == DELETE_EXPR
27046 || TREE_CODE (expression) == VEC_DELETE_EXPR
27047 || TREE_CODE (expression) == THROW_EXPR
27048 || TREE_CODE (expression) == REQUIRES_EXPR)
27049 return false;
27050
27051 /* The types of these expressions depends only on the type to which
27052 the cast occurs. */
27053 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27054 || TREE_CODE (expression) == STATIC_CAST_EXPR
27055 || TREE_CODE (expression) == CONST_CAST_EXPR
27056 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27057 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27058 || TREE_CODE (expression) == CAST_EXPR)
27059 return dependent_type_p (TREE_TYPE (expression));
27060
27061 /* The types of these expressions depends only on the type created
27062 by the expression. */
27063 if (TREE_CODE (expression) == NEW_EXPR
27064 || TREE_CODE (expression) == VEC_NEW_EXPR)
27065 {
27066 /* For NEW_EXPR tree nodes created inside a template, either
27067 the object type itself or a TREE_LIST may appear as the
27068 operand 1. */
27069 tree type = TREE_OPERAND (expression, 1);
27070 if (TREE_CODE (type) == TREE_LIST)
27071 /* This is an array type. We need to check array dimensions
27072 as well. */
27073 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27074 || value_dependent_expression_p
27075 (TREE_OPERAND (TREE_VALUE (type), 1));
27076 /* Array type whose dimension has to be deduced. */
27077 else if (TREE_CODE (type) == ARRAY_TYPE
27078 && TREE_OPERAND (expression, 2) == NULL_TREE)
27079 return true;
27080 else
27081 return dependent_type_p (type);
27082 }
27083
27084 if (TREE_CODE (expression) == SCOPE_REF)
27085 {
27086 tree scope = TREE_OPERAND (expression, 0);
27087 tree name = TREE_OPERAND (expression, 1);
27088
27089 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27090 contains an identifier associated by name lookup with one or more
27091 declarations declared with a dependent type, or...a
27092 nested-name-specifier or qualified-id that names a member of an
27093 unknown specialization. */
27094 return (type_dependent_expression_p (name)
27095 || dependent_scope_p (scope));
27096 }
27097
27098 if (TREE_CODE (expression) == TEMPLATE_DECL
27099 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27100 return uses_outer_template_parms (expression);
27101
27102 if (TREE_CODE (expression) == STMT_EXPR)
27103 expression = stmt_expr_value_expr (expression);
27104
27105 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27106 {
27107 tree elt;
27108 unsigned i;
27109
27110 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27111 {
27112 if (type_dependent_expression_p (elt))
27113 return true;
27114 }
27115 return false;
27116 }
27117
27118 /* A static data member of the current instantiation with incomplete
27119 array type is type-dependent, as the definition and specializations
27120 can have different bounds. */
27121 if (VAR_P (expression)
27122 && DECL_CLASS_SCOPE_P (expression)
27123 && dependent_type_p (DECL_CONTEXT (expression))
27124 && VAR_HAD_UNKNOWN_BOUND (expression))
27125 return true;
27126
27127 /* An array of unknown bound depending on a variadic parameter, eg:
27128
27129 template<typename... Args>
27130 void foo (Args... args)
27131 {
27132 int arr[] = { args... };
27133 }
27134
27135 template<int... vals>
27136 void bar ()
27137 {
27138 int arr[] = { vals... };
27139 }
27140
27141 If the array has no length and has an initializer, it must be that
27142 we couldn't determine its length in cp_complete_array_type because
27143 it is dependent. */
27144 if (VAR_P (expression)
27145 && TREE_TYPE (expression) != NULL_TREE
27146 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27147 && !TYPE_DOMAIN (TREE_TYPE (expression))
27148 && DECL_INITIAL (expression))
27149 return true;
27150
27151 /* A function or variable template-id is type-dependent if it has any
27152 dependent template arguments. */
27153 if (VAR_OR_FUNCTION_DECL_P (expression)
27154 && DECL_LANG_SPECIFIC (expression)
27155 && DECL_TEMPLATE_INFO (expression))
27156 {
27157 /* Consider the innermost template arguments, since those are the ones
27158 that come from the template-id; the template arguments for the
27159 enclosing class do not make it type-dependent unless they are used in
27160 the type of the decl. */
27161 if (instantiates_primary_template_p (expression)
27162 && (any_dependent_template_arguments_p
27163 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27164 return true;
27165 }
27166
27167 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27168 type-dependent. Checking this is important for functions with auto return
27169 type, which looks like a dependent type. */
27170 if (TREE_CODE (expression) == FUNCTION_DECL
27171 && !(DECL_CLASS_SCOPE_P (expression)
27172 && dependent_type_p (DECL_CONTEXT (expression)))
27173 && !(DECL_LANG_SPECIFIC (expression)
27174 && DECL_UNIQUE_FRIEND_P (expression)
27175 && (!DECL_FRIEND_CONTEXT (expression)
27176 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27177 && !DECL_LOCAL_DECL_P (expression))
27178 {
27179 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27180 || undeduced_auto_decl (expression));
27181 return false;
27182 }
27183
27184 /* Always dependent, on the number of arguments if nothing else. */
27185 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27186 return true;
27187
27188 if (TREE_TYPE (expression) == unknown_type_node)
27189 {
27190 if (TREE_CODE (expression) == ADDR_EXPR)
27191 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27192 if (TREE_CODE (expression) == COMPONENT_REF
27193 || TREE_CODE (expression) == OFFSET_REF)
27194 {
27195 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27196 return true;
27197 expression = TREE_OPERAND (expression, 1);
27198 if (identifier_p (expression))
27199 return false;
27200 }
27201 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27202 if (TREE_CODE (expression) == SCOPE_REF)
27203 return false;
27204
27205 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27206 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27207 || TREE_CODE (expression) == CO_YIELD_EXPR)
27208 return true;
27209
27210 if (BASELINK_P (expression))
27211 {
27212 if (BASELINK_OPTYPE (expression)
27213 && dependent_type_p (BASELINK_OPTYPE (expression)))
27214 return true;
27215 expression = BASELINK_FUNCTIONS (expression);
27216 }
27217
27218 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27219 {
27220 if (any_dependent_template_arguments_p
27221 (TREE_OPERAND (expression, 1)))
27222 return true;
27223 expression = TREE_OPERAND (expression, 0);
27224 if (identifier_p (expression))
27225 return true;
27226 }
27227
27228 gcc_assert (OVL_P (expression));
27229
27230 for (lkp_iterator iter (expression); iter; ++iter)
27231 if (type_dependent_expression_p (*iter))
27232 return true;
27233
27234 return false;
27235 }
27236
27237 /* The type of a non-type template parm declared with a placeholder type
27238 depends on the corresponding template argument, even though
27239 placeholders are not normally considered dependent. */
27240 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27241 && is_auto (TREE_TYPE (expression)))
27242 return true;
27243
27244 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27245
27246 /* Dependent type attributes might not have made it from the decl to
27247 the type yet. */
27248 if (DECL_P (expression)
27249 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27250 return true;
27251
27252 return (dependent_type_p (TREE_TYPE (expression)));
27253 }
27254
27255 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27256 type-dependent if the expression refers to a member of the current
27257 instantiation and the type of the referenced member is dependent, or the
27258 class member access expression refers to a member of an unknown
27259 specialization.
27260
27261 This function returns true if the OBJECT in such a class member access
27262 expression is of an unknown specialization. */
27263
27264 bool
27265 type_dependent_object_expression_p (tree object)
27266 {
27267 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27268 dependent. */
27269 if (TREE_CODE (object) == IDENTIFIER_NODE)
27270 return true;
27271 tree scope = TREE_TYPE (object);
27272 return (!scope || dependent_scope_p (scope));
27273 }
27274
27275 /* walk_tree callback function for instantiation_dependent_expression_p,
27276 below. Returns non-zero if a dependent subexpression is found. */
27277
27278 static tree
27279 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27280 void * /*data*/)
27281 {
27282 if (TYPE_P (*tp))
27283 {
27284 /* We don't have to worry about decltype currently because decltype
27285 of an instantiation-dependent expr is a dependent type. This
27286 might change depending on the resolution of DR 1172. */
27287 *walk_subtrees = false;
27288 return NULL_TREE;
27289 }
27290 enum tree_code code = TREE_CODE (*tp);
27291 switch (code)
27292 {
27293 /* Don't treat an argument list as dependent just because it has no
27294 TREE_TYPE. */
27295 case TREE_LIST:
27296 case TREE_VEC:
27297 case NONTYPE_ARGUMENT_PACK:
27298 return NULL_TREE;
27299
27300 case TEMPLATE_PARM_INDEX:
27301 if (dependent_type_p (TREE_TYPE (*tp)))
27302 return *tp;
27303 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27304 return *tp;
27305 /* We'll check value-dependence separately. */
27306 return NULL_TREE;
27307
27308 /* Handle expressions with type operands. */
27309 case SIZEOF_EXPR:
27310 case ALIGNOF_EXPR:
27311 case TYPEID_EXPR:
27312 case AT_ENCODE_EXPR:
27313 {
27314 tree op = TREE_OPERAND (*tp, 0);
27315 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27316 op = TREE_TYPE (op);
27317 if (TYPE_P (op))
27318 {
27319 if (dependent_type_p (op))
27320 return *tp;
27321 else
27322 {
27323 *walk_subtrees = false;
27324 return NULL_TREE;
27325 }
27326 }
27327 break;
27328 }
27329
27330 case COMPONENT_REF:
27331 if (identifier_p (TREE_OPERAND (*tp, 1)))
27332 /* In a template, finish_class_member_access_expr creates a
27333 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27334 type-dependent, so that we can check access control at
27335 instantiation time (PR 42277). See also Core issue 1273. */
27336 return *tp;
27337 break;
27338
27339 case SCOPE_REF:
27340 if (instantiation_dependent_scope_ref_p (*tp))
27341 return *tp;
27342 else
27343 break;
27344
27345 /* Treat statement-expressions as dependent. */
27346 case BIND_EXPR:
27347 return *tp;
27348
27349 /* Treat requires-expressions as dependent. */
27350 case REQUIRES_EXPR:
27351 return *tp;
27352
27353 case CALL_EXPR:
27354 /* Treat concept checks as dependent. */
27355 if (concept_check_p (*tp))
27356 return *tp;
27357 break;
27358
27359 case TEMPLATE_ID_EXPR:
27360 /* Treat concept checks as dependent. */
27361 if (concept_check_p (*tp))
27362 return *tp;
27363 break;
27364
27365 case CONSTRUCTOR:
27366 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27367 return *tp;
27368 break;
27369
27370 default:
27371 break;
27372 }
27373
27374 if (type_dependent_expression_p (*tp))
27375 return *tp;
27376 else
27377 return NULL_TREE;
27378 }
27379
27380 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27381 sense defined by the ABI:
27382
27383 "An expression is instantiation-dependent if it is type-dependent
27384 or value-dependent, or it has a subexpression that is type-dependent
27385 or value-dependent."
27386
27387 Except don't actually check value-dependence for unevaluated expressions,
27388 because in sizeof(i) we don't care about the value of i. Checking
27389 type-dependence will in turn check value-dependence of array bounds/template
27390 arguments as needed. */
27391
27392 bool
27393 instantiation_dependent_uneval_expression_p (tree expression)
27394 {
27395 tree result;
27396
27397 if (!processing_template_decl)
27398 return false;
27399
27400 if (expression == error_mark_node)
27401 return false;
27402
27403 result = cp_walk_tree_without_duplicates (&expression,
27404 instantiation_dependent_r, NULL);
27405 return result != NULL_TREE;
27406 }
27407
27408 /* As above, but also check value-dependence of the expression as a whole. */
27409
27410 bool
27411 instantiation_dependent_expression_p (tree expression)
27412 {
27413 return (instantiation_dependent_uneval_expression_p (expression)
27414 || (potential_constant_expression (expression)
27415 && value_dependent_expression_p (expression)));
27416 }
27417
27418 /* Like type_dependent_expression_p, but it also works while not processing
27419 a template definition, i.e. during substitution or mangling. */
27420
27421 bool
27422 type_dependent_expression_p_push (tree expr)
27423 {
27424 bool b;
27425 ++processing_template_decl;
27426 b = type_dependent_expression_p (expr);
27427 --processing_template_decl;
27428 return b;
27429 }
27430
27431 /* Returns TRUE if ARGS contains a type-dependent expression. */
27432
27433 bool
27434 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27435 {
27436 unsigned int i;
27437 tree arg;
27438
27439 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27440 {
27441 if (type_dependent_expression_p (arg))
27442 return true;
27443 }
27444 return false;
27445 }
27446
27447 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27448 expressions) contains any type-dependent expressions. */
27449
27450 bool
27451 any_type_dependent_elements_p (const_tree list)
27452 {
27453 for (; list; list = TREE_CHAIN (list))
27454 if (type_dependent_expression_p (TREE_VALUE (list)))
27455 return true;
27456
27457 return false;
27458 }
27459
27460 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27461 expressions) contains any value-dependent expressions. */
27462
27463 bool
27464 any_value_dependent_elements_p (const_tree list)
27465 {
27466 for (; list; list = TREE_CHAIN (list))
27467 if (value_dependent_expression_p (TREE_VALUE (list)))
27468 return true;
27469
27470 return false;
27471 }
27472
27473 /* Returns TRUE if the ARG (a template argument) is dependent. */
27474
27475 bool
27476 dependent_template_arg_p (tree arg)
27477 {
27478 if (!processing_template_decl)
27479 return false;
27480
27481 /* Assume a template argument that was wrongly written by the user
27482 is dependent. This is consistent with what
27483 any_dependent_template_arguments_p [that calls this function]
27484 does. */
27485 if (!arg || arg == error_mark_node)
27486 return true;
27487
27488 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27489 arg = argument_pack_select_arg (arg);
27490
27491 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27492 return true;
27493 if (TREE_CODE (arg) == TEMPLATE_DECL)
27494 {
27495 if (DECL_TEMPLATE_PARM_P (arg))
27496 return true;
27497 /* A member template of a dependent class is not necessarily
27498 type-dependent, but it is a dependent template argument because it
27499 will be a member of an unknown specialization to that template. */
27500 tree scope = CP_DECL_CONTEXT (arg);
27501 return TYPE_P (scope) && dependent_type_p (scope);
27502 }
27503 else if (ARGUMENT_PACK_P (arg))
27504 {
27505 tree args = ARGUMENT_PACK_ARGS (arg);
27506 int i, len = TREE_VEC_LENGTH (args);
27507 for (i = 0; i < len; ++i)
27508 {
27509 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27510 return true;
27511 }
27512
27513 return false;
27514 }
27515 else if (TYPE_P (arg))
27516 return dependent_type_p (arg);
27517 else
27518 return value_dependent_expression_p (arg);
27519 }
27520
27521 /* Returns true if ARGS (a collection of template arguments) contains
27522 any types that require structural equality testing. */
27523
27524 bool
27525 any_template_arguments_need_structural_equality_p (tree args)
27526 {
27527 int i;
27528 int j;
27529
27530 if (!args)
27531 return false;
27532 if (args == error_mark_node)
27533 return true;
27534
27535 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27536 {
27537 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27538 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27539 {
27540 tree arg = TREE_VEC_ELT (level, j);
27541 tree packed_args = NULL_TREE;
27542 int k, len = 1;
27543
27544 if (ARGUMENT_PACK_P (arg))
27545 {
27546 /* Look inside the argument pack. */
27547 packed_args = ARGUMENT_PACK_ARGS (arg);
27548 len = TREE_VEC_LENGTH (packed_args);
27549 }
27550
27551 for (k = 0; k < len; ++k)
27552 {
27553 if (packed_args)
27554 arg = TREE_VEC_ELT (packed_args, k);
27555
27556 if (error_operand_p (arg))
27557 return true;
27558 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27559 continue;
27560 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27561 return true;
27562 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27563 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27564 return true;
27565 }
27566 }
27567 }
27568
27569 return false;
27570 }
27571
27572 /* Returns true if ARGS (a collection of template arguments) contains
27573 any dependent arguments. */
27574
27575 bool
27576 any_dependent_template_arguments_p (const_tree args)
27577 {
27578 int i;
27579 int j;
27580
27581 if (!args)
27582 return false;
27583 if (args == error_mark_node)
27584 return true;
27585
27586 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27587 {
27588 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27589 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27590 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27591 return true;
27592 }
27593
27594 return false;
27595 }
27596
27597 /* Returns true if ARGS contains any errors. */
27598
27599 bool
27600 any_erroneous_template_args_p (const_tree args)
27601 {
27602 int i;
27603 int j;
27604
27605 if (args == error_mark_node)
27606 return true;
27607
27608 if (args && TREE_CODE (args) != TREE_VEC)
27609 {
27610 if (tree ti = get_template_info (args))
27611 args = TI_ARGS (ti);
27612 else
27613 args = NULL_TREE;
27614 }
27615
27616 if (!args)
27617 return false;
27618
27619 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27620 {
27621 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27622 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27623 if (error_operand_p (TREE_VEC_ELT (level, j)))
27624 return true;
27625 }
27626
27627 return false;
27628 }
27629
27630 /* Returns TRUE if the template TMPL is type-dependent. */
27631
27632 bool
27633 dependent_template_p (tree tmpl)
27634 {
27635 if (TREE_CODE (tmpl) == OVERLOAD)
27636 {
27637 for (lkp_iterator iter (tmpl); iter; ++iter)
27638 if (dependent_template_p (*iter))
27639 return true;
27640 return false;
27641 }
27642
27643 /* Template template parameters are dependent. */
27644 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27645 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27646 return true;
27647 /* So are names that have not been looked up. */
27648 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27649 return true;
27650 return false;
27651 }
27652
27653 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27654
27655 bool
27656 dependent_template_id_p (tree tmpl, tree args)
27657 {
27658 return (dependent_template_p (tmpl)
27659 || any_dependent_template_arguments_p (args));
27660 }
27661
27662 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27663 are dependent. */
27664
27665 bool
27666 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27667 {
27668 int i;
27669
27670 if (!processing_template_decl)
27671 return false;
27672
27673 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27674 {
27675 tree decl = TREE_VEC_ELT (declv, i);
27676 tree init = TREE_VEC_ELT (initv, i);
27677 tree cond = TREE_VEC_ELT (condv, i);
27678 tree incr = TREE_VEC_ELT (incrv, i);
27679
27680 if (type_dependent_expression_p (decl)
27681 || TREE_CODE (decl) == SCOPE_REF)
27682 return true;
27683
27684 if (init && type_dependent_expression_p (init))
27685 return true;
27686
27687 if (cond == global_namespace)
27688 return true;
27689
27690 if (type_dependent_expression_p (cond))
27691 return true;
27692
27693 if (COMPARISON_CLASS_P (cond)
27694 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27695 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27696 return true;
27697
27698 if (TREE_CODE (incr) == MODOP_EXPR)
27699 {
27700 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27701 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27702 return true;
27703 }
27704 else if (type_dependent_expression_p (incr))
27705 return true;
27706 else if (TREE_CODE (incr) == MODIFY_EXPR)
27707 {
27708 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27709 return true;
27710 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27711 {
27712 tree t = TREE_OPERAND (incr, 1);
27713 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27714 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27715 return true;
27716
27717 /* If this loop has a class iterator with != comparison
27718 with increment other than i++/++i/i--/--i, make sure the
27719 increment is constant. */
27720 if (CLASS_TYPE_P (TREE_TYPE (decl))
27721 && TREE_CODE (cond) == NE_EXPR)
27722 {
27723 if (TREE_OPERAND (t, 0) == decl)
27724 t = TREE_OPERAND (t, 1);
27725 else
27726 t = TREE_OPERAND (t, 0);
27727 if (TREE_CODE (t) != INTEGER_CST)
27728 return true;
27729 }
27730 }
27731 }
27732 }
27733
27734 return false;
27735 }
27736
27737 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27738 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27739 no such TYPE can be found. Note that this function peers inside
27740 uninstantiated templates and therefore should be used only in
27741 extremely limited situations. ONLY_CURRENT_P restricts this
27742 peering to the currently open classes hierarchy (which is required
27743 when comparing types). */
27744
27745 tree
27746 resolve_typename_type (tree type, bool only_current_p)
27747 {
27748 tree scope;
27749 tree name;
27750 tree decl;
27751 int quals;
27752 tree pushed_scope;
27753 tree result;
27754
27755 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27756
27757 scope = TYPE_CONTEXT (type);
27758 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27759 gcc_checking_assert (uses_template_parms (scope));
27760
27761 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27762 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27763 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27764 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27765 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27766 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27767 the TYPENAME_TYPE instead, we avoid messing up with a possible
27768 typedef variant case. */
27769 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27770
27771 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27772 it first before we can figure out what NAME refers to. */
27773 if (TREE_CODE (scope) == TYPENAME_TYPE)
27774 {
27775 if (TYPENAME_IS_RESOLVING_P (scope))
27776 /* Given a class template A with a dependent base with nested type C,
27777 typedef typename A::C::C C will land us here, as trying to resolve
27778 the initial A::C leads to the local C typedef, which leads back to
27779 A::C::C. So we break the recursion now. */
27780 return type;
27781 else
27782 scope = resolve_typename_type (scope, only_current_p);
27783 }
27784 /* If we don't know what SCOPE refers to, then we cannot resolve the
27785 TYPENAME_TYPE. */
27786 if (!CLASS_TYPE_P (scope))
27787 return type;
27788 /* If this is a typedef, we don't want to look inside (c++/11987). */
27789 if (typedef_variant_p (type))
27790 return type;
27791 /* If SCOPE isn't the template itself, it will not have a valid
27792 TYPE_FIELDS list. */
27793 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27794 /* scope is either the template itself or a compatible instantiation
27795 like X<T>, so look up the name in the original template. */
27796 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27797 /* If scope has no fields, it can't be a current instantiation. Check this
27798 before currently_open_class to avoid infinite recursion (71515). */
27799 if (!TYPE_FIELDS (scope))
27800 return type;
27801 /* If the SCOPE is not the current instantiation, there's no reason
27802 to look inside it. */
27803 if (only_current_p && !currently_open_class (scope))
27804 return type;
27805 /* Enter the SCOPE so that name lookup will be resolved as if we
27806 were in the class definition. In particular, SCOPE will no
27807 longer be considered a dependent type. */
27808 pushed_scope = push_scope (scope);
27809 /* Look up the declaration. */
27810 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27811 tf_warning_or_error);
27812
27813 result = NULL_TREE;
27814
27815 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27816 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27817 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27818 if (!decl)
27819 /*nop*/;
27820 else if (identifier_p (fullname)
27821 && TREE_CODE (decl) == TYPE_DECL)
27822 {
27823 result = TREE_TYPE (decl);
27824 if (result == error_mark_node)
27825 result = NULL_TREE;
27826 }
27827 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27828 && DECL_CLASS_TEMPLATE_P (decl))
27829 {
27830 /* Obtain the template and the arguments. */
27831 tree tmpl = TREE_OPERAND (fullname, 0);
27832 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27833 {
27834 /* We get here with a plain identifier because a previous tentative
27835 parse of the nested-name-specifier as part of a ptr-operator saw
27836 ::template X<A>. The use of ::template is necessary in a
27837 ptr-operator, but wrong in a declarator-id.
27838
27839 [temp.names]: In a qualified-id of a declarator-id, the keyword
27840 template shall not appear at the top level. */
27841 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27842 "keyword %<template%> not allowed in declarator-id");
27843 tmpl = decl;
27844 }
27845 tree args = TREE_OPERAND (fullname, 1);
27846 /* Instantiate the template. */
27847 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27848 /*entering_scope=*/true,
27849 tf_error | tf_user);
27850 if (result == error_mark_node)
27851 result = NULL_TREE;
27852 }
27853
27854 /* Leave the SCOPE. */
27855 if (pushed_scope)
27856 pop_scope (pushed_scope);
27857
27858 /* If we failed to resolve it, return the original typename. */
27859 if (!result)
27860 return type;
27861
27862 /* If lookup found a typename type, resolve that too. */
27863 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27864 {
27865 /* Ill-formed programs can cause infinite recursion here, so we
27866 must catch that. */
27867 TYPENAME_IS_RESOLVING_P (result) = 1;
27868 result = resolve_typename_type (result, only_current_p);
27869 TYPENAME_IS_RESOLVING_P (result) = 0;
27870 }
27871
27872 /* Qualify the resulting type. */
27873 quals = cp_type_quals (type);
27874 if (quals)
27875 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27876
27877 return result;
27878 }
27879
27880 /* EXPR is an expression which is not type-dependent. Return a proxy
27881 for EXPR that can be used to compute the types of larger
27882 expressions containing EXPR. */
27883
27884 tree
27885 build_non_dependent_expr (tree expr)
27886 {
27887 tree orig_expr = expr;
27888 tree inner_expr;
27889
27890 /* When checking, try to get a constant value for all non-dependent
27891 expressions in order to expose bugs in *_dependent_expression_p
27892 and constexpr. This can affect code generation, see PR70704, so
27893 only do this for -fchecking=2. */
27894 if (flag_checking > 1
27895 && cxx_dialect >= cxx11
27896 /* Don't do this during nsdmi parsing as it can lead to
27897 unexpected recursive instantiations. */
27898 && !parsing_nsdmi ()
27899 /* Don't do this during concept processing either and for
27900 the same reason. */
27901 && !processing_constraint_expression_p ())
27902 fold_non_dependent_expr (expr, tf_none);
27903
27904 STRIP_ANY_LOCATION_WRAPPER (expr);
27905
27906 /* Preserve OVERLOADs; the functions must be available to resolve
27907 types. */
27908 inner_expr = expr;
27909 if (TREE_CODE (inner_expr) == STMT_EXPR)
27910 inner_expr = stmt_expr_value_expr (inner_expr);
27911 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27912 inner_expr = TREE_OPERAND (inner_expr, 0);
27913 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27914 inner_expr = TREE_OPERAND (inner_expr, 1);
27915 if (is_overloaded_fn (inner_expr)
27916 || TREE_CODE (inner_expr) == OFFSET_REF)
27917 return orig_expr;
27918 /* There is no need to return a proxy for a variable or enumerator. */
27919 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27920 return orig_expr;
27921 /* Preserve string constants; conversions from string constants to
27922 "char *" are allowed, even though normally a "const char *"
27923 cannot be used to initialize a "char *". */
27924 if (TREE_CODE (expr) == STRING_CST)
27925 return orig_expr;
27926 /* Preserve void and arithmetic constants, as an optimization -- there is no
27927 reason to create a new node. */
27928 if (TREE_CODE (expr) == VOID_CST
27929 || TREE_CODE (expr) == INTEGER_CST
27930 || TREE_CODE (expr) == REAL_CST)
27931 return orig_expr;
27932 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27933 There is at least one place where we want to know that a
27934 particular expression is a throw-expression: when checking a ?:
27935 expression, there are special rules if the second or third
27936 argument is a throw-expression. */
27937 if (TREE_CODE (expr) == THROW_EXPR)
27938 return orig_expr;
27939
27940 /* Don't wrap an initializer list, we need to be able to look inside. */
27941 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27942 return orig_expr;
27943
27944 /* Don't wrap a dummy object, we need to be able to test for it. */
27945 if (is_dummy_object (expr))
27946 return orig_expr;
27947
27948 if (TREE_CODE (expr) == COND_EXPR)
27949 return build3 (COND_EXPR,
27950 TREE_TYPE (expr),
27951 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27952 (TREE_OPERAND (expr, 1)
27953 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27954 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27955 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27956 if (TREE_CODE (expr) == COMPOUND_EXPR
27957 && !COMPOUND_EXPR_OVERLOADED (expr))
27958 return build2 (COMPOUND_EXPR,
27959 TREE_TYPE (expr),
27960 TREE_OPERAND (expr, 0),
27961 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27962
27963 /* If the type is unknown, it can't really be non-dependent */
27964 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27965
27966 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27967 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27968 TREE_TYPE (expr), expr);
27969 }
27970
27971 /* ARGS is a vector of expressions as arguments to a function call.
27972 Replace the arguments with equivalent non-dependent expressions.
27973 This modifies ARGS in place. */
27974
27975 void
27976 make_args_non_dependent (vec<tree, va_gc> *args)
27977 {
27978 unsigned int ix;
27979 tree arg;
27980
27981 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27982 {
27983 tree newarg = build_non_dependent_expr (arg);
27984 if (newarg != arg)
27985 (*args)[ix] = newarg;
27986 }
27987 }
27988
27989 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27990 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27991 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27992
27993 static tree
27994 make_auto_1 (tree name, bool set_canonical)
27995 {
27996 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27997 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27998 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27999 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
28000 (0, processing_template_decl + 1, processing_template_decl + 1,
28001 TYPE_NAME (au), NULL_TREE);
28002 if (set_canonical)
28003 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28004 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28005 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28006 if (name == decltype_auto_identifier)
28007 AUTO_IS_DECLTYPE (au) = true;
28008
28009 return au;
28010 }
28011
28012 tree
28013 make_decltype_auto (void)
28014 {
28015 return make_auto_1 (decltype_auto_identifier, true);
28016 }
28017
28018 tree
28019 make_auto (void)
28020 {
28021 return make_auto_1 (auto_identifier, true);
28022 }
28023
28024 /* Return a C++17 deduction placeholder for class template TMPL. */
28025
28026 tree
28027 make_template_placeholder (tree tmpl)
28028 {
28029 tree t = make_auto_1 (auto_identifier, false);
28030 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28031 /* Our canonical type depends on the placeholder. */
28032 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28033 return t;
28034 }
28035
28036 /* True iff T is a C++17 class template deduction placeholder. */
28037
28038 bool
28039 template_placeholder_p (tree t)
28040 {
28041 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28042 }
28043
28044 /* Make a "constrained auto" type-specifier. This is an auto or
28045 decltype(auto) type with constraints that must be associated after
28046 deduction. The constraint is formed from the given concept CON
28047 and its optional sequence of template arguments ARGS.
28048
28049 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28050
28051 static tree
28052 make_constrained_placeholder_type (tree type, tree con, tree args)
28053 {
28054 /* Build the constraint. */
28055 tree tmpl = DECL_TI_TEMPLATE (con);
28056 tree expr = tmpl;
28057 if (TREE_CODE (con) == FUNCTION_DECL)
28058 expr = ovl_make (tmpl);
28059 ++processing_template_decl;
28060 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28061 --processing_template_decl;
28062
28063 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
28064
28065 /* Our canonical type depends on the constraint. */
28066 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28067
28068 /* Attach the constraint to the type declaration. */
28069 return TYPE_NAME (type);
28070 }
28071
28072 /* Make a "constrained auto" type-specifier. */
28073
28074 tree
28075 make_constrained_auto (tree con, tree args)
28076 {
28077 tree type = make_auto_1 (auto_identifier, false);
28078 return make_constrained_placeholder_type (type, con, args);
28079 }
28080
28081 /* Make a "constrained decltype(auto)" type-specifier. */
28082
28083 tree
28084 make_constrained_decltype_auto (tree con, tree args)
28085 {
28086 tree type = make_auto_1 (decltype_auto_identifier, false);
28087 return make_constrained_placeholder_type (type, con, args);
28088 }
28089
28090 /* Build and return a concept definition. Like other templates, the
28091 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28092 the TEMPLATE_DECL. */
28093
28094 tree
28095 finish_concept_definition (cp_expr id, tree init)
28096 {
28097 gcc_assert (identifier_p (id));
28098 gcc_assert (processing_template_decl);
28099
28100 location_t loc = id.get_location();
28101
28102 /* A concept-definition shall not have associated constraints. */
28103 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28104 {
28105 error_at (loc, "a concept cannot be constrained");
28106 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28107 }
28108
28109 /* A concept-definition shall appear in namespace scope. Templates
28110 aren't allowed in block scope, so we only need to check for class
28111 scope. */
28112 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28113 {
28114 error_at (loc, "concept %qE not in namespace scope", *id);
28115 return error_mark_node;
28116 }
28117
28118 /* Initially build the concept declaration; its type is bool. */
28119 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28120 DECL_CONTEXT (decl) = current_scope ();
28121 DECL_INITIAL (decl) = init;
28122
28123 set_originating_module (decl, false);
28124
28125 /* Push the enclosing template. */
28126 return push_template_decl (decl);
28127 }
28128
28129 /* Given type ARG, return std::initializer_list<ARG>. */
28130
28131 static tree
28132 listify (tree arg)
28133 {
28134 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28135
28136 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28137 {
28138 gcc_rich_location richloc (input_location);
28139 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28140 error_at (&richloc,
28141 "deducing from brace-enclosed initializer list"
28142 " requires %<#include <initializer_list>%>");
28143
28144 return error_mark_node;
28145 }
28146 tree argvec = make_tree_vec (1);
28147 TREE_VEC_ELT (argvec, 0) = arg;
28148
28149 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28150 NULL_TREE, 0, tf_warning_or_error);
28151 }
28152
28153 /* Replace auto in TYPE with std::initializer_list<auto>. */
28154
28155 static tree
28156 listify_autos (tree type, tree auto_node)
28157 {
28158 tree init_auto = listify (strip_top_quals (auto_node));
28159 tree argvec = make_tree_vec (1);
28160 TREE_VEC_ELT (argvec, 0) = init_auto;
28161 if (processing_template_decl)
28162 argvec = add_to_template_args (current_template_args (), argvec);
28163 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28164 }
28165
28166 /* Hash traits for hashing possibly constrained 'auto'
28167 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28168
28169 struct auto_hash : default_hash_traits<tree>
28170 {
28171 static inline hashval_t hash (tree);
28172 static inline bool equal (tree, tree);
28173 };
28174
28175 /* Hash the 'auto' T. */
28176
28177 inline hashval_t
28178 auto_hash::hash (tree t)
28179 {
28180 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28181 /* Matching constrained-type-specifiers denote the same template
28182 parameter, so hash the constraint. */
28183 return hash_placeholder_constraint (c);
28184 else
28185 /* But unconstrained autos are all separate, so just hash the pointer. */
28186 return iterative_hash_object (t, 0);
28187 }
28188
28189 /* Compare two 'auto's. */
28190
28191 inline bool
28192 auto_hash::equal (tree t1, tree t2)
28193 {
28194 if (t1 == t2)
28195 return true;
28196
28197 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28198 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28199
28200 /* Two unconstrained autos are distinct. */
28201 if (!c1 || !c2)
28202 return false;
28203
28204 return equivalent_placeholder_constraints (c1, c2);
28205 }
28206
28207 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28208 constrained) auto, add it to the vector. */
28209
28210 static int
28211 extract_autos_r (tree t, void *data)
28212 {
28213 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28214 if (is_auto (t))
28215 {
28216 /* All the autos were built with index 0; fix that up now. */
28217 tree *p = hash.find_slot (t, INSERT);
28218 unsigned idx;
28219 if (*p)
28220 /* If this is a repeated constrained-type-specifier, use the index we
28221 chose before. */
28222 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28223 else
28224 {
28225 /* Otherwise this is new, so use the current count. */
28226 *p = t;
28227 idx = hash.elements () - 1;
28228 }
28229 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28230 }
28231
28232 /* Always keep walking. */
28233 return 0;
28234 }
28235
28236 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28237 says they can appear anywhere in the type. */
28238
28239 static tree
28240 extract_autos (tree type)
28241 {
28242 hash_set<tree> visited;
28243 hash_table<auto_hash> hash (2);
28244
28245 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28246
28247 tree tree_vec = make_tree_vec (hash.elements());
28248 for (hash_table<auto_hash>::iterator iter = hash.begin();
28249 iter != hash.end(); ++iter)
28250 {
28251 tree elt = *iter;
28252 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28253 TREE_VEC_ELT (tree_vec, i)
28254 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28255 }
28256
28257 return tree_vec;
28258 }
28259
28260 /* The stem for deduction guide names. */
28261 const char *const dguide_base = "__dguide_";
28262
28263 /* Return the name for a deduction guide for class template TMPL. */
28264
28265 tree
28266 dguide_name (tree tmpl)
28267 {
28268 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28269 tree tname = TYPE_IDENTIFIER (type);
28270 char *buf = (char *) alloca (1 + strlen (dguide_base)
28271 + IDENTIFIER_LENGTH (tname));
28272 memcpy (buf, dguide_base, strlen (dguide_base));
28273 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28274 IDENTIFIER_LENGTH (tname) + 1);
28275 tree dname = get_identifier (buf);
28276 TREE_TYPE (dname) = type;
28277 return dname;
28278 }
28279
28280 /* True if NAME is the name of a deduction guide. */
28281
28282 bool
28283 dguide_name_p (tree name)
28284 {
28285 return (TREE_CODE (name) == IDENTIFIER_NODE
28286 && TREE_TYPE (name)
28287 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28288 strlen (dguide_base)));
28289 }
28290
28291 /* True if FN is a deduction guide. */
28292
28293 bool
28294 deduction_guide_p (const_tree fn)
28295 {
28296 if (DECL_P (fn))
28297 if (tree name = DECL_NAME (fn))
28298 return dguide_name_p (name);
28299 return false;
28300 }
28301
28302 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28303
28304 bool
28305 copy_guide_p (const_tree fn)
28306 {
28307 gcc_assert (deduction_guide_p (fn));
28308 if (!DECL_ARTIFICIAL (fn))
28309 return false;
28310 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28311 return (TREE_CHAIN (parms) == void_list_node
28312 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28313 }
28314
28315 /* True if FN is a guide generated from a constructor template. */
28316
28317 bool
28318 template_guide_p (const_tree fn)
28319 {
28320 gcc_assert (deduction_guide_p (fn));
28321 if (!DECL_ARTIFICIAL (fn))
28322 return false;
28323 tree tmpl = DECL_TI_TEMPLATE (fn);
28324 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28325 return PRIMARY_TEMPLATE_P (org);
28326 return false;
28327 }
28328
28329 /* True if FN is an aggregate initialization guide or the copy deduction
28330 guide. */
28331
28332 bool
28333 builtin_guide_p (const_tree fn)
28334 {
28335 if (!deduction_guide_p (fn))
28336 return false;
28337 if (!DECL_ARTIFICIAL (fn))
28338 /* Explicitly declared. */
28339 return false;
28340 if (DECL_ABSTRACT_ORIGIN (fn))
28341 /* Derived from a constructor. */
28342 return false;
28343 return true;
28344 }
28345
28346 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28347 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28348 template parameter types. Note that the handling of template template
28349 parameters relies on current_template_parms being set appropriately for the
28350 new template. */
28351
28352 static tree
28353 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28354 tree tsubst_args, tsubst_flags_t complain)
28355 {
28356 if (olddecl == error_mark_node)
28357 return error_mark_node;
28358
28359 tree oldidx = get_template_parm_index (olddecl);
28360
28361 tree newtype;
28362 if (TREE_CODE (olddecl) == TYPE_DECL
28363 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28364 {
28365 tree oldtype = TREE_TYPE (olddecl);
28366 newtype = cxx_make_type (TREE_CODE (oldtype));
28367 TYPE_MAIN_VARIANT (newtype) = newtype;
28368 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28369 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28370 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28371 }
28372 else
28373 {
28374 newtype = TREE_TYPE (olddecl);
28375 if (type_uses_auto (newtype))
28376 {
28377 // Substitute once to fix references to other template parameters.
28378 newtype = tsubst (newtype, tsubst_args,
28379 complain|tf_partial, NULL_TREE);
28380 // Now substitute again to reduce the level of the auto.
28381 newtype = tsubst (newtype, current_template_args (),
28382 complain, NULL_TREE);
28383 }
28384 else
28385 newtype = tsubst (newtype, tsubst_args,
28386 complain, NULL_TREE);
28387 }
28388
28389 tree newdecl
28390 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28391 DECL_NAME (olddecl), newtype);
28392 SET_DECL_TEMPLATE_PARM_P (newdecl);
28393
28394 tree newidx;
28395 if (TREE_CODE (olddecl) == TYPE_DECL
28396 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28397 {
28398 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28399 = build_template_parm_index (index, level, level,
28400 newdecl, newtype);
28401 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28402 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28403 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28404 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28405 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28406 else
28407 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28408
28409 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28410 {
28411 DECL_TEMPLATE_RESULT (newdecl)
28412 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28413 DECL_NAME (olddecl), newtype);
28414 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28415 // First create a copy (ttargs) of tsubst_args with an
28416 // additional level for the template template parameter's own
28417 // template parameters (ttparms).
28418 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28419 (DECL_TEMPLATE_PARMS (olddecl)));
28420 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28421 tree ttargs = make_tree_vec (depth + 1);
28422 for (int i = 0; i < depth; ++i)
28423 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28424 TREE_VEC_ELT (ttargs, depth)
28425 = template_parms_level_to_args (ttparms);
28426 // Substitute ttargs into ttparms to fix references to
28427 // other template parameters.
28428 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28429 complain|tf_partial);
28430 // Now substitute again with args based on tparms, to reduce
28431 // the level of the ttparms.
28432 ttargs = current_template_args ();
28433 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28434 complain);
28435 // Finally, tack the adjusted parms onto tparms.
28436 ttparms = tree_cons (size_int (depth), ttparms,
28437 current_template_parms);
28438 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28439 }
28440 }
28441 else
28442 {
28443 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28444 tree newconst
28445 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28446 TREE_CODE (oldconst),
28447 DECL_NAME (oldconst), newtype);
28448 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28449 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28450 SET_DECL_TEMPLATE_PARM_P (newconst);
28451 newidx = build_template_parm_index (index, level, level,
28452 newconst, newtype);
28453 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28454 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28455 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28456 }
28457
28458 return newdecl;
28459 }
28460
28461 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28462 template parameter. */
28463
28464 static tree
28465 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28466 tree targs, unsigned targs_index, tsubst_flags_t complain)
28467 {
28468 tree olddecl = TREE_VALUE (oldelt);
28469 tree newdecl = rewrite_template_parm (olddecl, index, level,
28470 targs, complain);
28471 if (newdecl == error_mark_node)
28472 return error_mark_node;
28473 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28474 targs, complain, NULL_TREE);
28475 tree list = build_tree_list (newdef, newdecl);
28476 TEMPLATE_PARM_CONSTRAINTS (list)
28477 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28478 targs, complain, NULL_TREE);
28479 int depth = TMPL_ARGS_DEPTH (targs);
28480 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28481 return list;
28482 }
28483
28484 /* Returns a C++17 class deduction guide template based on the constructor
28485 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28486 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28487 aggregate initialization guide. */
28488
28489 static tree
28490 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28491 {
28492 tree tparms, targs, fparms, fargs, ci;
28493 bool memtmpl = false;
28494 bool explicit_p;
28495 location_t loc;
28496 tree fn_tmpl = NULL_TREE;
28497
28498 if (outer_args)
28499 {
28500 ++processing_template_decl;
28501 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28502 --processing_template_decl;
28503 }
28504
28505 if (!DECL_DECLARES_FUNCTION_P (ctor))
28506 {
28507 if (TYPE_P (ctor))
28508 {
28509 bool copy_p = TYPE_REF_P (ctor);
28510 if (copy_p)
28511 fparms = tree_cons (NULL_TREE, type, void_list_node);
28512 else
28513 fparms = void_list_node;
28514 }
28515 else if (TREE_CODE (ctor) == TREE_LIST)
28516 fparms = ctor;
28517 else
28518 gcc_unreachable ();
28519
28520 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28521 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28522 targs = CLASSTYPE_TI_ARGS (type);
28523 ci = NULL_TREE;
28524 fargs = NULL_TREE;
28525 loc = DECL_SOURCE_LOCATION (ctmpl);
28526 explicit_p = false;
28527 }
28528 else
28529 {
28530 ++processing_template_decl;
28531 bool ok = true;
28532
28533 fn_tmpl
28534 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28535 : DECL_TI_TEMPLATE (ctor));
28536 if (outer_args)
28537 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28538 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28539
28540 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28541 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28542 fully specialized args for the enclosing class. Strip those off, as
28543 the deduction guide won't have those template parameters. */
28544 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28545 TMPL_PARMS_DEPTH (tparms));
28546 /* Discard the 'this' parameter. */
28547 fparms = FUNCTION_ARG_CHAIN (ctor);
28548 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28549 ci = get_constraints (ctor);
28550 loc = DECL_SOURCE_LOCATION (ctor);
28551 explicit_p = DECL_NONCONVERTING_P (ctor);
28552
28553 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28554 {
28555 memtmpl = true;
28556
28557 /* For a member template constructor, we need to flatten the two
28558 template parameter lists into one, and then adjust the function
28559 signature accordingly. This gets...complicated. */
28560 tree save_parms = current_template_parms;
28561
28562 /* For a member template we should have two levels of parms/args, one
28563 for the class and one for the constructor. We stripped
28564 specialized args for further enclosing classes above. */
28565 const int depth = 2;
28566 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28567
28568 /* Template args for translating references to the two-level template
28569 parameters into references to the one-level template parameters we
28570 are creating. */
28571 tree tsubst_args = copy_node (targs);
28572 TMPL_ARGS_LEVEL (tsubst_args, depth)
28573 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28574
28575 /* Template parms for the constructor template. */
28576 tree ftparms = TREE_VALUE (tparms);
28577 unsigned flen = TREE_VEC_LENGTH (ftparms);
28578 /* Template parms for the class template. */
28579 tparms = TREE_CHAIN (tparms);
28580 tree ctparms = TREE_VALUE (tparms);
28581 unsigned clen = TREE_VEC_LENGTH (ctparms);
28582 /* Template parms for the deduction guide start as a copy of the
28583 template parms for the class. We set current_template_parms for
28584 lookup_template_class_1. */
28585 current_template_parms = tparms = copy_node (tparms);
28586 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28587 for (unsigned i = 0; i < clen; ++i)
28588 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28589
28590 /* Now we need to rewrite the constructor parms to append them to the
28591 class parms. */
28592 for (unsigned i = 0; i < flen; ++i)
28593 {
28594 unsigned index = i + clen;
28595 unsigned level = 1;
28596 tree oldelt = TREE_VEC_ELT (ftparms, i);
28597 tree newelt
28598 = rewrite_tparm_list (oldelt, index, level,
28599 tsubst_args, i, complain);
28600 if (newelt == error_mark_node)
28601 ok = false;
28602 TREE_VEC_ELT (new_vec, index) = newelt;
28603 }
28604
28605 /* Now we have a final set of template parms to substitute into the
28606 function signature. */
28607 targs = template_parms_to_args (tparms);
28608 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28609 complain, ctor);
28610 if (fparms == error_mark_node)
28611 ok = false;
28612 if (ci)
28613 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28614
28615 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28616 cp_unevaluated_operand. */
28617 cp_evaluated ev;
28618 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28619 current_template_parms = save_parms;
28620 }
28621 else
28622 {
28623 /* Substitute in the same arguments to rewrite class members into
28624 references to members of an unknown specialization. */
28625 cp_evaluated ev;
28626 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28627 fargs = tsubst (fargs, targs, complain, ctor);
28628 if (ci)
28629 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28630 }
28631
28632 --processing_template_decl;
28633 if (!ok)
28634 return error_mark_node;
28635 }
28636
28637 if (!memtmpl)
28638 {
28639 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28640 tparms = copy_node (tparms);
28641 INNERMOST_TEMPLATE_PARMS (tparms)
28642 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28643 }
28644
28645 tree fntype = build_function_type (type, fparms);
28646 tree ded_fn = build_lang_decl_loc (loc,
28647 FUNCTION_DECL,
28648 dguide_name (type), fntype);
28649 DECL_ARGUMENTS (ded_fn) = fargs;
28650 DECL_ARTIFICIAL (ded_fn) = true;
28651 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28652 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28653 DECL_ARTIFICIAL (ded_tmpl) = true;
28654 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28655 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28656 if (DECL_P (ctor))
28657 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28658 if (ci)
28659 set_constraints (ded_tmpl, ci);
28660
28661 return ded_tmpl;
28662 }
28663
28664 /* Add to LIST the member types for the reshaped initializer CTOR. */
28665
28666 static tree
28667 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28668 {
28669 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28670 tree idx, val; unsigned i;
28671 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28672 {
28673 tree ftype = elt ? elt : TREE_TYPE (idx);
28674 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28675 && CONSTRUCTOR_NELTS (val)
28676 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28677 type gets a single initializer. */
28678 && CP_AGGREGATE_TYPE_P (ftype)
28679 && !(TREE_CODE (ftype) == ARRAY_TYPE
28680 && uses_template_parms (TYPE_DOMAIN (ftype))))
28681 {
28682 tree subelt = NULL_TREE;
28683 if (TREE_CODE (ftype) == ARRAY_TYPE)
28684 subelt = TREE_TYPE (ftype);
28685 list = collect_ctor_idx_types (val, list, subelt);
28686 continue;
28687 }
28688 tree arg = NULL_TREE;
28689 if (i == v->length() - 1
28690 && PACK_EXPANSION_P (ftype))
28691 /* Give the trailing pack expansion parameter a default argument to
28692 match aggregate initialization behavior, even if we deduce the
28693 length of the pack separately to more than we have initializers. */
28694 arg = build_constructor (init_list_type_node, NULL);
28695 /* if ei is of array type and xi is a braced-init-list or string literal,
28696 Ti is an rvalue reference to the declared type of ei */
28697 STRIP_ANY_LOCATION_WRAPPER (val);
28698 if (TREE_CODE (ftype) == ARRAY_TYPE
28699 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28700 || TREE_CODE (val) == STRING_CST))
28701 {
28702 if (TREE_CODE (val) == STRING_CST)
28703 ftype = cp_build_qualified_type
28704 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28705 ftype = (cp_build_reference_type
28706 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28707 }
28708 list = tree_cons (arg, ftype, list);
28709 }
28710
28711 return list;
28712 }
28713
28714 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28715
28716 static bool
28717 is_spec_or_derived (tree etype, tree tmpl)
28718 {
28719 if (!etype || !CLASS_TYPE_P (etype))
28720 return false;
28721
28722 tree type = TREE_TYPE (tmpl);
28723 tree tparms = (INNERMOST_TEMPLATE_PARMS
28724 (DECL_TEMPLATE_PARMS (tmpl)));
28725 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28726 int err = unify (tparms, targs, type, etype,
28727 UNIFY_ALLOW_DERIVED, /*explain*/false);
28728 ggc_free (targs);
28729 return !err;
28730 }
28731
28732 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28733 INIT. */
28734
28735 static tree
28736 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28737 {
28738 if (cxx_dialect < cxx20)
28739 return NULL_TREE;
28740
28741 if (init == NULL_TREE)
28742 return NULL_TREE;
28743
28744 tree type = TREE_TYPE (tmpl);
28745 if (!CP_AGGREGATE_TYPE_P (type))
28746 return NULL_TREE;
28747
28748 /* No aggregate candidate for copy-initialization. */
28749 if (args->length() == 1)
28750 {
28751 tree val = (*args)[0];
28752 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28753 return NULL_TREE;
28754 }
28755
28756 /* If we encounter a problem, we just won't add the candidate. */
28757 tsubst_flags_t complain = tf_none;
28758
28759 tree parms = NULL_TREE;
28760 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28761 {
28762 init = reshape_init (type, init, complain);
28763 if (init == error_mark_node)
28764 return NULL_TREE;
28765 parms = collect_ctor_idx_types (init, parms);
28766 }
28767 else if (TREE_CODE (init) == TREE_LIST)
28768 {
28769 int len = list_length (init);
28770 for (tree field = TYPE_FIELDS (type);
28771 len;
28772 --len, field = DECL_CHAIN (field))
28773 {
28774 field = next_initializable_field (field);
28775 if (!field)
28776 return NULL_TREE;
28777 tree ftype = finish_decltype_type (field, true, complain);
28778 parms = tree_cons (NULL_TREE, ftype, parms);
28779 }
28780 }
28781 else
28782 /* Aggregate initialization doesn't apply to an initializer expression. */
28783 return NULL_TREE;
28784
28785 if (parms)
28786 {
28787 tree last = parms;
28788 parms = nreverse (parms);
28789 TREE_CHAIN (last) = void_list_node;
28790 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28791 return guide;
28792 }
28793
28794 return NULL_TREE;
28795 }
28796
28797 /* UGUIDES are the deduction guides for the underlying template of alias
28798 template TMPL; adjust them to be deduction guides for TMPL. */
28799
28800 static tree
28801 alias_ctad_tweaks (tree tmpl, tree uguides)
28802 {
28803 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28804 class type (9.2.8.2) where the template-name names an alias template A,
28805 the defining-type-id of A must be of the form
28806
28807 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28808
28809 as specified in 9.2.8.2. The guides of A are the set of functions or
28810 function templates formed as follows. For each function or function
28811 template f in the guides of the template named by the simple-template-id
28812 of the defining-type-id, the template arguments of the return type of f
28813 are deduced from the defining-type-id of A according to the process in
28814 13.10.2.5 with the exception that deduction does not fail if not all
28815 template arguments are deduced. Let g denote the result of substituting
28816 these deductions into f. If substitution succeeds, form a function or
28817 function template f' with the following properties and add it to the set
28818 of guides of A:
28819
28820 * The function type of f' is the function type of g.
28821
28822 * If f is a function template, f' is a function template whose template
28823 parameter list consists of all the template parameters of A (including
28824 their default template arguments) that appear in the above deductions or
28825 (recursively) in their default template arguments, followed by the
28826 template parameters of f that were not deduced (including their default
28827 template arguments), otherwise f' is not a function template.
28828
28829 * The associated constraints (13.5.2) are the conjunction of the
28830 associated constraints of g and a constraint that is satisfied if and only
28831 if the arguments of A are deducible (see below) from the return type.
28832
28833 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28834 be so as well.
28835
28836 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28837 considered to be so as well.
28838
28839 * The explicit-specifier of f' is the explicit-specifier of g (if
28840 any). */
28841
28842 /* This implementation differs from the above in two significant ways:
28843
28844 1) We include all template parameters of A, not just some.
28845 2) The added constraint is same_type instead of deducible.
28846
28847 I believe that while it's probably possible to construct a testcase that
28848 behaves differently with this simplification, it should have the same
28849 effect for real uses. Including all template parameters means that we
28850 deduce all parameters of A when resolving the call, so when we're in the
28851 constraint we don't need to deduce them again, we can just check whether
28852 the deduction produced the desired result. */
28853
28854 tsubst_flags_t complain = tf_warning_or_error;
28855 tree atype = TREE_TYPE (tmpl);
28856 tree aguides = NULL_TREE;
28857 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28858 unsigned natparms = TREE_VEC_LENGTH (atparms);
28859 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28860 for (ovl_iterator iter (uguides); iter; ++iter)
28861 {
28862 tree f = *iter;
28863 tree in_decl = f;
28864 location_t loc = DECL_SOURCE_LOCATION (f);
28865 tree ret = TREE_TYPE (TREE_TYPE (f));
28866 tree fprime = f;
28867 if (TREE_CODE (f) == TEMPLATE_DECL)
28868 {
28869 processing_template_decl_sentinel ptds (/*reset*/false);
28870 ++processing_template_decl;
28871
28872 /* Deduce template arguments for f from the type-id of A. */
28873 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28874 unsigned len = TREE_VEC_LENGTH (ftparms);
28875 tree targs = make_tree_vec (len);
28876 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28877 gcc_assert (!err);
28878
28879 /* The number of parms for f' is the number of parms for A plus
28880 non-deduced parms of f. */
28881 unsigned ndlen = 0;
28882 unsigned j;
28883 for (unsigned i = 0; i < len; ++i)
28884 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28885 ++ndlen;
28886 tree gtparms = make_tree_vec (natparms + ndlen);
28887
28888 /* First copy over the parms of A. */
28889 for (j = 0; j < natparms; ++j)
28890 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28891 /* Now rewrite the non-deduced parms of f. */
28892 for (unsigned i = 0; ndlen && i < len; ++i)
28893 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28894 {
28895 --ndlen;
28896 unsigned index = j++;
28897 unsigned level = 1;
28898 tree oldlist = TREE_VEC_ELT (ftparms, i);
28899 tree list = rewrite_tparm_list (oldlist, index, level,
28900 targs, i, complain);
28901 TREE_VEC_ELT (gtparms, index) = list;
28902 }
28903 gtparms = build_tree_list (size_one_node, gtparms);
28904
28905 /* Substitute the deduced arguments plus the rewritten template
28906 parameters into f to get g. This covers the type, copyness,
28907 guideness, and explicit-specifier. */
28908 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28909 if (g == error_mark_node)
28910 return error_mark_node;
28911 DECL_USE_TEMPLATE (g) = 0;
28912 fprime = build_template_decl (g, gtparms, false);
28913 DECL_TEMPLATE_RESULT (fprime) = g;
28914 TREE_TYPE (fprime) = TREE_TYPE (g);
28915 tree gtargs = template_parms_to_args (gtparms);
28916 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28917 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28918
28919 /* Substitute the associated constraints. */
28920 tree ci = get_constraints (f);
28921 if (ci)
28922 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28923 if (ci == error_mark_node)
28924 return error_mark_node;
28925
28926 /* Add a constraint that the return type matches the instantiation of
28927 A with the same template arguments. */
28928 ret = TREE_TYPE (TREE_TYPE (fprime));
28929 if (!same_type_p (atype, ret)
28930 /* FIXME this should mean they don't compare as equivalent. */
28931 || dependent_alias_template_spec_p (atype, nt_opaque))
28932 {
28933 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28934 ci = append_constraint (ci, same);
28935 }
28936
28937 if (ci)
28938 {
28939 remove_constraints (fprime);
28940 set_constraints (fprime, ci);
28941 }
28942 }
28943 else
28944 {
28945 /* For a non-template deduction guide, if the arguments of A aren't
28946 deducible from the return type, don't add the candidate. */
28947 tree targs = make_tree_vec (natparms);
28948 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28949 for (unsigned i = 0; !err && i < natparms; ++i)
28950 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28951 err = true;
28952 if (err)
28953 continue;
28954 }
28955
28956 aguides = lookup_add (fprime, aguides);
28957 }
28958
28959 return aguides;
28960 }
28961
28962 /* Return artificial deduction guides built from the constructors of class
28963 template TMPL. */
28964
28965 static tree
28966 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28967 {
28968 tree type = TREE_TYPE (tmpl);
28969 tree outer_args = NULL_TREE;
28970 if (DECL_CLASS_SCOPE_P (tmpl)
28971 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28972 {
28973 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28974 type = TREE_TYPE (most_general_template (tmpl));
28975 }
28976
28977 tree cands = NULL_TREE;
28978
28979 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28980 {
28981 /* Skip inherited constructors. */
28982 if (iter.using_p ())
28983 continue;
28984
28985 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28986 cands = lookup_add (guide, cands);
28987 }
28988
28989 /* Add implicit default constructor deduction guide. */
28990 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28991 {
28992 tree guide = build_deduction_guide (type, type, outer_args,
28993 complain);
28994 cands = lookup_add (guide, cands);
28995 }
28996
28997 /* Add copy guide. */
28998 {
28999 tree gtype = build_reference_type (type);
29000 tree guide = build_deduction_guide (type, gtype, outer_args,
29001 complain);
29002 cands = lookup_add (guide, cands);
29003 }
29004
29005 return cands;
29006 }
29007
29008 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29009
29010 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29011 aggregate candidate is added separately because it depends on the
29012 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29013 guide. */
29014
29015 static tree
29016 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29017 {
29018 tree guides = NULL_TREE;
29019 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29020 {
29021 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29022 tree tinfo = get_template_info (under);
29023 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29024 complain);
29025 }
29026 else
29027 {
29028 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29029 dguide_name (tmpl),
29030 LOOK_want::NORMAL, /*complain*/false);
29031 if (guides == error_mark_node)
29032 guides = NULL_TREE;
29033 else
29034 any_dguides_p = true;
29035 }
29036
29037 /* Cache the deduction guides for a template. We also remember the result of
29038 lookup, and rebuild everything if it changes; should be very rare. */
29039 tree_pair_p cache = NULL;
29040 if (tree_pair_p &r
29041 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29042 {
29043 cache = r;
29044 if (cache->purpose == guides)
29045 return cache->value;
29046 }
29047 else
29048 {
29049 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29050 cache->purpose = guides;
29051 }
29052
29053 tree cands = NULL_TREE;
29054 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29055 cands = alias_ctad_tweaks (tmpl, guides);
29056 else
29057 {
29058 cands = ctor_deduction_guides_for (tmpl, complain);
29059 for (ovl_iterator it (guides); it; ++it)
29060 cands = lookup_add (*it, cands);
29061 }
29062
29063 cache->value = cands;
29064 return cands;
29065 }
29066
29067 /* Return whether TMPL is a (class template argument-) deducible template. */
29068
29069 bool
29070 ctad_template_p (tree tmpl)
29071 {
29072 /* A deducible template is either a class template or is an alias template
29073 whose defining-type-id is of the form
29074
29075 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29076
29077 where the nested-name-specifier (if any) is non-dependent and the
29078 template-name of the simple-template-id names a deducible template. */
29079
29080 if (DECL_CLASS_TEMPLATE_P (tmpl)
29081 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29082 return true;
29083 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29084 return false;
29085 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29086 if (tree tinfo = get_template_info (orig))
29087 return ctad_template_p (TI_TEMPLATE (tinfo));
29088 return false;
29089 }
29090
29091 /* Deduce template arguments for the class template placeholder PTYPE for
29092 template TMPL based on the initializer INIT, and return the resulting
29093 type. */
29094
29095 static tree
29096 do_class_deduction (tree ptype, tree tmpl, tree init,
29097 int flags, tsubst_flags_t complain)
29098 {
29099 /* We should have handled this in the caller. */
29100 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29101 return ptype;
29102
29103 /* Initializing one placeholder from another. */
29104 if (init && TREE_CODE (init) == TEMPLATE_PARM_INDEX
29105 && is_auto (TREE_TYPE (init))
29106 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29107 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29108
29109 /* Look through alias templates that just rename another template. */
29110 tmpl = get_underlying_template (tmpl);
29111 if (!ctad_template_p (tmpl))
29112 {
29113 if (complain & tf_error)
29114 error ("non-deducible template %qT used without template arguments", tmpl);
29115 return error_mark_node;
29116 }
29117 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29118 {
29119 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29120 if (complain & tf_warning_or_error)
29121 pedwarn (input_location, 0, "alias template deduction only available "
29122 "with %<-std=c++20%> or %<-std=gnu++20%>");
29123 }
29124
29125 tree type = TREE_TYPE (tmpl);
29126
29127 bool try_list_ctor = false;
29128 bool list_init_p = false;
29129
29130 releasing_vec rv_args = NULL;
29131 vec<tree,va_gc> *&args = *&rv_args;
29132 if (init == NULL_TREE)
29133 args = make_tree_vector ();
29134 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29135 {
29136 list_init_p = true;
29137 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29138 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
29139 {
29140 /* As an exception, the first phase in 16.3.1.7 (considering the
29141 initializer list as a single argument) is omitted if the
29142 initializer list consists of a single expression of type cv U,
29143 where U is a specialization of C or a class derived from a
29144 specialization of C. */
29145 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29146 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29147 try_list_ctor = false;
29148 }
29149 if (try_list_ctor || is_std_init_list (type))
29150 args = make_tree_vector_single (init);
29151 else
29152 args = make_tree_vector_from_ctor (init);
29153 }
29154 else if (TREE_CODE (init) == TREE_LIST)
29155 args = make_tree_vector_from_list (init);
29156 else
29157 args = make_tree_vector_single (init);
29158
29159 /* Do this now to avoid problems with erroneous args later on. */
29160 args = resolve_args (args, complain);
29161 if (args == NULL)
29162 return error_mark_node;
29163
29164 bool any_dguides_p = false;
29165 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29166 if (cands == error_mark_node)
29167 return error_mark_node;
29168
29169 /* Prune explicit deduction guides in copy-initialization context (but
29170 not copy-list-initialization). */
29171 bool elided = false;
29172 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29173 {
29174 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29175 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29176 elided = true;
29177
29178 if (elided)
29179 {
29180 /* Found a nonconverting guide, prune the candidates. */
29181 tree pruned = NULL_TREE;
29182 for (lkp_iterator iter (cands); iter; ++iter)
29183 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29184 pruned = lookup_add (*iter, pruned);
29185
29186 cands = pruned;
29187 }
29188 }
29189
29190 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29191 cands = lookup_add (guide, cands);
29192
29193 tree call = error_mark_node;
29194
29195 /* If this is list-initialization and the class has a list constructor, first
29196 try deducing from the list as a single argument, as [over.match.list]. */
29197 tree list_cands = NULL_TREE;
29198 if (try_list_ctor && cands)
29199 for (lkp_iterator iter (cands); iter; ++iter)
29200 {
29201 tree dg = *iter;
29202 if (is_list_ctor (dg))
29203 list_cands = lookup_add (dg, list_cands);
29204 }
29205 if (list_cands)
29206 {
29207 ++cp_unevaluated_operand;
29208 call = build_new_function_call (list_cands, &args, tf_decltype);
29209 --cp_unevaluated_operand;
29210
29211 if (call == error_mark_node)
29212 {
29213 /* That didn't work, now try treating the list as a sequence of
29214 arguments. */
29215 release_tree_vector (args);
29216 args = make_tree_vector_from_ctor (init);
29217 }
29218 }
29219
29220 if (elided && !cands)
29221 {
29222 error ("cannot deduce template arguments for copy-initialization"
29223 " of %qT, as it has no non-explicit deduction guides or "
29224 "user-declared constructors", type);
29225 return error_mark_node;
29226 }
29227 else if (!cands && call == error_mark_node)
29228 {
29229 error ("cannot deduce template arguments of %qT, as it has no viable "
29230 "deduction guides", type);
29231 return error_mark_node;
29232 }
29233
29234 if (call == error_mark_node)
29235 {
29236 ++cp_unevaluated_operand;
29237 call = build_new_function_call (cands, &args, tf_decltype);
29238 --cp_unevaluated_operand;
29239 }
29240
29241 if (call == error_mark_node)
29242 {
29243 if (complain & tf_warning_or_error)
29244 {
29245 error ("class template argument deduction failed:");
29246
29247 ++cp_unevaluated_operand;
29248 call = build_new_function_call (cands, &args,
29249 complain | tf_decltype);
29250 --cp_unevaluated_operand;
29251
29252 if (elided)
29253 inform (input_location, "explicit deduction guides not considered "
29254 "for copy-initialization");
29255 }
29256 return error_mark_node;
29257 }
29258 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29259 constructor is chosen, the initialization is ill-formed. */
29260 else if (flags & LOOKUP_ONLYCONVERTING)
29261 {
29262 tree fndecl = cp_get_callee_fndecl_nofold (call);
29263 if (fndecl && DECL_NONCONVERTING_P (fndecl))
29264 {
29265 if (complain & tf_warning_or_error)
29266 {
29267 // TODO: Pass down location from cp_finish_decl.
29268 error ("class template argument deduction for %qT failed: "
29269 "explicit deduction guide selected in "
29270 "copy-list-initialization", type);
29271 inform (DECL_SOURCE_LOCATION (fndecl),
29272 "explicit deduction guide declared here");
29273
29274 }
29275 return error_mark_node;
29276 }
29277 }
29278
29279 /* If CTAD succeeded but the type doesn't have any explicit deduction
29280 guides, this deduction might not be what the user intended. */
29281 if (call != error_mark_node && !any_dguides_p)
29282 {
29283 tree fndecl = cp_get_callee_fndecl_nofold (call);
29284 if (fndecl != NULL_TREE
29285 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29286 || global_dc->dc_warn_system_headers)
29287 && warning (OPT_Wctad_maybe_unsupported,
29288 "%qT may not intend to support class template argument "
29289 "deduction", type))
29290 inform (input_location, "add a deduction guide to suppress this "
29291 "warning");
29292 }
29293
29294 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29295 }
29296
29297 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29298 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29299 The CONTEXT determines the context in which auto deduction is performed
29300 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29301 OUTER_TARGS are used during template argument deduction
29302 (context == adc_unify) to properly substitute the result, and is ignored
29303 in other contexts.
29304
29305 For partial-concept-ids, extra args may be appended to the list of deduced
29306 template arguments prior to determining constraint satisfaction. */
29307
29308 tree
29309 do_auto_deduction (tree type, tree init, tree auto_node,
29310 tsubst_flags_t complain, auto_deduction_context context,
29311 tree outer_targs, int flags)
29312 {
29313 tree targs;
29314
29315 if (init == error_mark_node)
29316 return error_mark_node;
29317
29318 if (init && type_dependent_expression_p (init)
29319 && context != adc_unify)
29320 /* Defining a subset of type-dependent expressions that we can deduce
29321 from ahead of time isn't worth the trouble. */
29322 return type;
29323
29324 /* Similarly, we can't deduce from another undeduced decl. */
29325 if (init && undeduced_auto_decl (init))
29326 return type;
29327
29328 /* We may be doing a partial substitution, but we still want to replace
29329 auto_node. */
29330 complain &= ~tf_partial;
29331
29332 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29333 /* C++17 class template argument deduction. */
29334 return do_class_deduction (type, tmpl, init, flags, complain);
29335
29336 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29337 /* Nothing we can do with this, even in deduction context. */
29338 return type;
29339
29340 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29341 with either a new invented type template parameter U or, if the
29342 initializer is a braced-init-list (8.5.4), with
29343 std::initializer_list<U>. */
29344 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29345 {
29346 if (!DIRECT_LIST_INIT_P (init))
29347 type = listify_autos (type, auto_node);
29348 else if (CONSTRUCTOR_NELTS (init) == 1)
29349 init = CONSTRUCTOR_ELT (init, 0)->value;
29350 else
29351 {
29352 if (complain & tf_warning_or_error)
29353 {
29354 if (permerror (input_location, "direct-list-initialization of "
29355 "%<auto%> requires exactly one element"))
29356 inform (input_location,
29357 "for deduction to %<std::initializer_list%>, use copy-"
29358 "list-initialization (i.e. add %<=%> before the %<{%>)");
29359 }
29360 type = listify_autos (type, auto_node);
29361 }
29362 }
29363
29364 if (type == error_mark_node)
29365 return error_mark_node;
29366
29367 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29368 {
29369 /* We don't recurse here because we can't deduce from a nested
29370 initializer_list. */
29371 if (CONSTRUCTOR_ELTS (init))
29372 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
29373 elt.value = resolve_nondeduced_context (elt.value, complain);
29374 }
29375 else
29376 init = resolve_nondeduced_context (init, complain);
29377
29378 if (context == adc_decomp_type
29379 && auto_node == type
29380 && init != error_mark_node
29381 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29382 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29383 and initializer has array type, deduce cv-qualified array type. */
29384 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29385 complain);
29386 else if (AUTO_IS_DECLTYPE (auto_node))
29387 {
29388 tree stripped_init = tree_strip_any_location_wrapper (init);
29389 if (REFERENCE_REF_P (stripped_init))
29390 stripped_init = TREE_OPERAND (stripped_init, 0);
29391 bool id = (DECL_P (stripped_init)
29392 || ((TREE_CODE (init) == COMPONENT_REF
29393 || TREE_CODE (init) == SCOPE_REF)
29394 && !REF_PARENTHESIZED_P (init)));
29395 targs = make_tree_vec (1);
29396 TREE_VEC_ELT (targs, 0)
29397 = finish_decltype_type (init, id, tf_warning_or_error);
29398 if (type != auto_node)
29399 {
29400 if (complain & tf_error)
29401 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29402 return error_mark_node;
29403 }
29404 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29405 {
29406 if (complain & tf_error)
29407 error ("%<decltype(auto)%> cannot be cv-qualified");
29408 return error_mark_node;
29409 }
29410 }
29411 else
29412 {
29413 if (error_operand_p (init))
29414 return error_mark_node;
29415
29416 tree parms = build_tree_list (NULL_TREE, type);
29417 tree tparms;
29418
29419 if (flag_concepts)
29420 tparms = extract_autos (type);
29421 else
29422 {
29423 tparms = make_tree_vec (1);
29424 TREE_VEC_ELT (tparms, 0)
29425 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29426 }
29427
29428 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29429 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29430 DEDUCE_CALL,
29431 NULL, /*explain_p=*/false);
29432 if (val > 0)
29433 {
29434 if (processing_template_decl)
29435 /* Try again at instantiation time. */
29436 return type;
29437 if (type && type != error_mark_node
29438 && (complain & tf_error))
29439 /* If type is error_mark_node a diagnostic must have been
29440 emitted by now. Also, having a mention to '<type error>'
29441 in the diagnostic is not really useful to the user. */
29442 {
29443 if (cfun
29444 && FNDECL_USED_AUTO (current_function_decl)
29445 && (auto_node
29446 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29447 && LAMBDA_FUNCTION_P (current_function_decl))
29448 error ("unable to deduce lambda return type from %qE", init);
29449 else
29450 error ("unable to deduce %qT from %qE", type, init);
29451 type_unification_real (tparms, targs, parms, &init, 1, 0,
29452 DEDUCE_CALL,
29453 NULL, /*explain_p=*/true);
29454 }
29455 return error_mark_node;
29456 }
29457 }
29458
29459 /* Check any placeholder constraints against the deduced type. */
29460 if (flag_concepts && !processing_template_decl)
29461 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29462 {
29463 /* Use the deduced type to check the associated constraints. If we
29464 have a partial-concept-id, rebuild the argument list so that
29465 we check using the extra arguments. */
29466 check = unpack_concept_check (check);
29467 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29468 tree cdecl = TREE_OPERAND (check, 0);
29469 if (OVL_P (cdecl))
29470 cdecl = OVL_FIRST (cdecl);
29471 tree cargs = TREE_OPERAND (check, 1);
29472 if (TREE_VEC_LENGTH (cargs) > 1)
29473 {
29474 cargs = copy_node (cargs);
29475 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29476 }
29477 else
29478 cargs = targs;
29479
29480 /* Rebuild the check using the deduced arguments. */
29481 check = build_concept_check (cdecl, cargs, tf_none);
29482
29483 if (!constraints_satisfied_p (check))
29484 {
29485 if (complain & tf_warning_or_error)
29486 {
29487 auto_diagnostic_group d;
29488 switch (context)
29489 {
29490 case adc_unspecified:
29491 case adc_unify:
29492 error("placeholder constraints not satisfied");
29493 break;
29494 case adc_variable_type:
29495 case adc_decomp_type:
29496 error ("deduced initializer does not satisfy "
29497 "placeholder constraints");
29498 break;
29499 case adc_return_type:
29500 error ("deduced return type does not satisfy "
29501 "placeholder constraints");
29502 break;
29503 case adc_requirement:
29504 error ("deduced expression type does not satisfy "
29505 "placeholder constraints");
29506 break;
29507 }
29508 diagnose_constraints (input_location, check, targs);
29509 }
29510 return error_mark_node;
29511 }
29512 }
29513
29514 if (processing_template_decl && context != adc_unify)
29515 outer_targs = current_template_args ();
29516 targs = add_to_template_args (outer_targs, targs);
29517 return tsubst (type, targs, complain, NULL_TREE);
29518 }
29519
29520 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29521 result. */
29522
29523 tree
29524 splice_late_return_type (tree type, tree late_return_type)
29525 {
29526 if (late_return_type)
29527 {
29528 gcc_assert (is_auto (type) || seen_error ());
29529 return late_return_type;
29530 }
29531
29532 if (tree *auto_node = find_type_usage (&type, is_auto))
29533 {
29534 tree idx = get_template_parm_index (*auto_node);
29535 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29536 {
29537 /* In an abbreviated function template we didn't know we were dealing
29538 with a function template when we saw the auto return type, so update
29539 it to have the correct level. */
29540 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29541 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29542 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29543 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29544 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29545 *auto_node = new_auto;
29546 }
29547 }
29548 return type;
29549 }
29550
29551 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29552 'decltype(auto)' or a deduced class template. */
29553
29554 bool
29555 is_auto (const_tree type)
29556 {
29557 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29558 && (TYPE_IDENTIFIER (type) == auto_identifier
29559 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29560 return true;
29561 else
29562 return false;
29563 }
29564
29565 /* for_each_template_parm callback for type_uses_auto. */
29566
29567 int
29568 is_auto_r (tree tp, void */*data*/)
29569 {
29570 return is_auto (tp);
29571 }
29572
29573 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29574 a use of `auto'. Returns NULL_TREE otherwise. */
29575
29576 tree
29577 type_uses_auto (tree type)
29578 {
29579 if (type == NULL_TREE)
29580 return NULL_TREE;
29581 else if (flag_concepts)
29582 {
29583 /* The Concepts TS allows multiple autos in one type-specifier; just
29584 return the first one we find, do_auto_deduction will collect all of
29585 them. */
29586 if (uses_template_parms (type))
29587 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29588 /*visited*/NULL, /*nondeduced*/false);
29589 else
29590 return NULL_TREE;
29591 }
29592 else if (tree *tp = find_type_usage (&type, is_auto))
29593 return *tp;
29594 else
29595 return NULL_TREE;
29596 }
29597
29598 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29599 concepts are enabled, auto is acceptable in template arguments, but
29600 only when TEMPL identifies a template class. Return TRUE if any
29601 such errors were reported. */
29602
29603 bool
29604 check_auto_in_tmpl_args (tree tmpl, tree args)
29605 {
29606 /* If there were previous errors, nevermind. */
29607 if (!args || TREE_CODE (args) != TREE_VEC)
29608 return false;
29609
29610 /* If TMPL is an identifier, we're parsing and we can't tell yet
29611 whether TMPL is supposed to be a type, a function or a variable.
29612 We'll only be able to tell during template substitution, so we
29613 expect to be called again then. If concepts are enabled and we
29614 know we have a type, we're ok. */
29615 if (flag_concepts
29616 && (identifier_p (tmpl)
29617 || (DECL_P (tmpl)
29618 && (DECL_TYPE_TEMPLATE_P (tmpl)
29619 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29620 return false;
29621
29622 /* Quickly search for any occurrences of auto; usually there won't
29623 be any, and then we'll avoid allocating the vector. */
29624 if (!type_uses_auto (args))
29625 return false;
29626
29627 bool errors = false;
29628
29629 tree vec = extract_autos (args);
29630 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29631 {
29632 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29633 error_at (DECL_SOURCE_LOCATION (xauto),
29634 "invalid use of %qT in template argument", xauto);
29635 errors = true;
29636 }
29637
29638 return errors;
29639 }
29640
29641 /* Recursively walk over && expressions searching for EXPR. Return a reference
29642 to that expression. */
29643
29644 static tree *find_template_requirement (tree *t, tree key)
29645 {
29646 if (*t == key)
29647 return t;
29648 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29649 {
29650 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29651 return p;
29652 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29653 return p;
29654 }
29655 return 0;
29656 }
29657
29658 /* Convert the generic type parameters in PARM that match the types given in the
29659 range [START_IDX, END_IDX) from the current_template_parms into generic type
29660 packs. */
29661
29662 tree
29663 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29664 {
29665 tree current = current_template_parms;
29666 int depth = TMPL_PARMS_DEPTH (current);
29667 current = INNERMOST_TEMPLATE_PARMS (current);
29668 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29669
29670 for (int i = 0; i < start_idx; ++i)
29671 TREE_VEC_ELT (replacement, i)
29672 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29673
29674 for (int i = start_idx; i < end_idx; ++i)
29675 {
29676 /* Create a distinct parameter pack type from the current parm and add it
29677 to the replacement args to tsubst below into the generic function
29678 parameter. */
29679 tree node = TREE_VEC_ELT (current, i);
29680 tree o = TREE_TYPE (TREE_VALUE (node));
29681 tree t = copy_type (o);
29682 TEMPLATE_TYPE_PARM_INDEX (t)
29683 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29684 t, 0, 0, tf_none);
29685 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29686 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29687 TYPE_MAIN_VARIANT (t) = t;
29688 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29689 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29690 TREE_VEC_ELT (replacement, i) = t;
29691
29692 /* Replace the current template parameter with new pack. */
29693 TREE_VALUE (node) = TREE_CHAIN (t);
29694
29695 /* Surgically adjust the associated constraint of adjusted parameter
29696 and it's corresponding contribution to the current template
29697 requirements. */
29698 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29699 {
29700 tree id = unpack_concept_check (constr);
29701 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29702 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29703 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29704
29705 /* If there was a constraint, we also need to replace that in
29706 the template requirements, which we've already built. */
29707 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29708 reqs = find_template_requirement (reqs, constr);
29709 *reqs = fold;
29710 }
29711 }
29712
29713 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29714 TREE_VEC_ELT (replacement, i)
29715 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29716
29717 /* If there are more levels then build up the replacement with the outer
29718 template parms. */
29719 if (depth > 1)
29720 replacement = add_to_template_args (template_parms_to_args
29721 (TREE_CHAIN (current_template_parms)),
29722 replacement);
29723
29724 return tsubst (parm, replacement, tf_none, NULL_TREE);
29725 }
29726
29727 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29728 0..N-1. */
29729
29730 void
29731 declare_integer_pack (void)
29732 {
29733 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29734 build_function_type_list (integer_type_node,
29735 integer_type_node,
29736 NULL_TREE),
29737 NULL_TREE, ECF_CONST);
29738 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29739 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29740 CP_BUILT_IN_INTEGER_PACK);
29741 }
29742
29743 /* Walk the decl or type specialization table calling FN on each
29744 entry. */
29745
29746 void
29747 walk_specializations (bool decls_p,
29748 void (*fn) (bool decls_p, spec_entry *entry, void *data),
29749 void *data)
29750 {
29751 spec_hash_table *table = decls_p ? decl_specializations
29752 : type_specializations;
29753 spec_hash_table::iterator end (table->end ());
29754 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
29755 fn (decls_p, *iter, data);
29756 }
29757
29758 /* Lookup the specialization of *ELT, in the decl or type
29759 specialization table. Return the SPEC that's already there (NULL if
29760 nothing). If INSERT is true, and there was nothing, add the new
29761 spec. */
29762
29763 tree
29764 match_mergeable_specialization (bool decl_p, spec_entry *elt, bool insert)
29765 {
29766 hash_table<spec_hasher> *specializations
29767 = decl_p ? decl_specializations : type_specializations;
29768 hashval_t hash = spec_hasher::hash (elt);
29769 spec_entry **slot
29770 = specializations->find_slot_with_hash (elt, hash,
29771 insert ? INSERT : NO_INSERT);
29772 if (slot && *slot)
29773 return (*slot)->spec;
29774
29775 if (insert)
29776 {
29777 auto entry = ggc_alloc<spec_entry> ();
29778 *entry = *elt;
29779 *slot = entry;
29780 }
29781
29782 return NULL_TREE;
29783 }
29784
29785 /* Return flags encoding whether SPEC is on the instantiation and/or
29786 specialization lists of TMPL. */
29787
29788 unsigned
29789 get_mergeable_specialization_flags (tree tmpl, tree decl)
29790 {
29791 unsigned flags = 0;
29792
29793 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
29794 inst; inst = TREE_CHAIN (inst))
29795 if (TREE_VALUE (inst) == decl)
29796 {
29797 flags |= 1;
29798 break;
29799 }
29800
29801 if (CLASS_TYPE_P (TREE_TYPE (decl))
29802 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
29803 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
29804 /* Only need to search if DECL is a partial specialization. */
29805 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
29806 part; part = TREE_CHAIN (part))
29807 if (TREE_VALUE (part) == decl)
29808 {
29809 flags |= 2;
29810 break;
29811 }
29812
29813 return flags;
29814 }
29815
29816 /* Add a new specialization of TMPL. FLAGS is as returned from
29817 get_mergeable_specialization_flags. */
29818
29819 void
29820 add_mergeable_specialization (tree tmpl, tree args, tree decl, unsigned flags)
29821 {
29822 if (flags & 1)
29823 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
29824 = tree_cons (args, decl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
29825
29826 if (flags & 2)
29827 {
29828 /* A partial specialization. */
29829 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
29830 = tree_cons (args, decl, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
29831 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (tmpl))
29832 = TREE_TYPE (DECL_TEMPLATE_RESULT (decl));
29833 }
29834 }
29835
29836 /* Set up the hash tables for template instantiations. */
29837
29838 void
29839 init_template_processing (void)
29840 {
29841 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29842 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29843
29844 if (cxx_dialect >= cxx11)
29845 declare_integer_pack ();
29846 }
29847
29848 /* Print stats about the template hash tables for -fstats. */
29849
29850 void
29851 print_template_statistics (void)
29852 {
29853 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29854 "%f collisions\n", (long) decl_specializations->size (),
29855 (long) decl_specializations->elements (),
29856 decl_specializations->collisions ());
29857 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29858 "%f collisions\n", (long) type_specializations->size (),
29859 (long) type_specializations->elements (),
29860 type_specializations->collisions ());
29861 }
29862
29863 #if CHECKING_P
29864
29865 namespace selftest {
29866
29867 /* Verify that build_non_dependent_expr () works, for various expressions,
29868 and that location wrappers don't affect the results. */
29869
29870 static void
29871 test_build_non_dependent_expr ()
29872 {
29873 location_t loc = BUILTINS_LOCATION;
29874
29875 /* Verify constants, without and with location wrappers. */
29876 tree int_cst = build_int_cst (integer_type_node, 42);
29877 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29878
29879 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29880 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29881 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29882
29883 tree string_lit = build_string (4, "foo");
29884 TREE_TYPE (string_lit) = char_array_type_node;
29885 string_lit = fix_string_type (string_lit);
29886 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29887
29888 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29889 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29890 ASSERT_EQ (wrapped_string_lit,
29891 build_non_dependent_expr (wrapped_string_lit));
29892 }
29893
29894 /* Verify that type_dependent_expression_p () works correctly, even
29895 in the presence of location wrapper nodes. */
29896
29897 static void
29898 test_type_dependent_expression_p ()
29899 {
29900 location_t loc = BUILTINS_LOCATION;
29901
29902 tree name = get_identifier ("foo");
29903
29904 /* If no templates are involved, nothing is type-dependent. */
29905 gcc_assert (!processing_template_decl);
29906 ASSERT_FALSE (type_dependent_expression_p (name));
29907
29908 ++processing_template_decl;
29909
29910 /* Within a template, an unresolved name is always type-dependent. */
29911 ASSERT_TRUE (type_dependent_expression_p (name));
29912
29913 /* Ensure it copes with NULL_TREE and errors. */
29914 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29915 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29916
29917 /* A USING_DECL in a template should be type-dependent, even if wrapped
29918 with a location wrapper (PR c++/83799). */
29919 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29920 TREE_TYPE (using_decl) = integer_type_node;
29921 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29922 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29923 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29924 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29925
29926 --processing_template_decl;
29927 }
29928
29929 /* Run all of the selftests within this file. */
29930
29931 void
29932 cp_pt_c_tests ()
29933 {
29934 test_build_non_dependent_expr ();
29935 test_type_dependent_expression_p ();
29936 }
29937
29938 } // namespace selftest
29939
29940 #endif /* #if CHECKING_P */
29941
29942 #include "gt-cp-pt.h"