c++: ICE with delayed noexcept and attribute used [PR97966]
[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 /* Defer access checking while we substitute into the types named in
11829 the base-clause. */
11830 push_deferring_access_checks (dk_deferred);
11831 if (BINFO_N_BASE_BINFOS (pbinfo))
11832 {
11833 tree pbase_binfo;
11834 int i;
11835
11836 /* Substitute into each of the bases to determine the actual
11837 basetypes. */
11838 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11839 {
11840 tree base;
11841 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11842 tree expanded_bases = NULL_TREE;
11843 int idx, len = 1;
11844
11845 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11846 {
11847 expanded_bases =
11848 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11849 args, tf_error, NULL_TREE);
11850 if (expanded_bases == error_mark_node)
11851 continue;
11852
11853 len = TREE_VEC_LENGTH (expanded_bases);
11854 }
11855
11856 for (idx = 0; idx < len; idx++)
11857 {
11858 if (expanded_bases)
11859 /* Extract the already-expanded base class. */
11860 base = TREE_VEC_ELT (expanded_bases, idx);
11861 else
11862 /* Substitute to figure out the base class. */
11863 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11864 NULL_TREE);
11865
11866 if (base == error_mark_node)
11867 continue;
11868
11869 base_list = tree_cons (access, base, base_list);
11870 if (BINFO_VIRTUAL_P (pbase_binfo))
11871 TREE_TYPE (base_list) = integer_type_node;
11872 }
11873 }
11874
11875 /* The list is now in reverse order; correct that. */
11876 base_list = nreverse (base_list);
11877 }
11878 /* Now call xref_basetypes to set up all the base-class
11879 information. */
11880 xref_basetypes (type, base_list);
11881
11882 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11883 (int) ATTR_FLAG_TYPE_IN_PLACE,
11884 args, tf_error, NULL_TREE);
11885 fixup_attribute_variants (type);
11886
11887 /* Now that our base classes are set up, enter the scope of the
11888 class, so that name lookups into base classes, etc. will work
11889 correctly. This is precisely analogous to what we do in
11890 begin_class_definition when defining an ordinary non-template
11891 class, except we also need to push the enclosing classes. */
11892 push_nested_class (type);
11893
11894 /* Now check accessibility of the types named in its base-clause,
11895 relative to the scope of the class. */
11896 pop_to_parent_deferring_access_checks ();
11897
11898 /* A vector to hold members marked with attribute used. */
11899 auto_vec<tree> used;
11900
11901 /* Now members are processed in the order of declaration. */
11902 for (member = CLASSTYPE_DECL_LIST (pattern);
11903 member; member = TREE_CHAIN (member))
11904 {
11905 tree t = TREE_VALUE (member);
11906
11907 if (TREE_PURPOSE (member))
11908 {
11909 if (TYPE_P (t))
11910 {
11911 if (LAMBDA_TYPE_P (t))
11912 /* A closure type for a lambda in an NSDMI or default argument.
11913 Ignore it; it will be regenerated when needed. */
11914 continue;
11915
11916 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11917 && TYPE_LANG_SPECIFIC (t)
11918 && CLASSTYPE_IS_TEMPLATE (t));
11919
11920 /* If the member is a class template, then -- even after
11921 substitution -- there may be dependent types in the
11922 template argument list for the class. We increment
11923 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11924 that function will assume that no types are dependent
11925 when outside of a template. */
11926 if (class_template_p)
11927 ++processing_template_decl;
11928 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11929 if (class_template_p)
11930 --processing_template_decl;
11931 if (newtag == error_mark_node)
11932 continue;
11933
11934 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11935 {
11936 tree name = TYPE_IDENTIFIER (t);
11937
11938 if (class_template_p)
11939 /* Unfortunately, lookup_template_class sets
11940 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11941 instantiation (i.e., for the type of a member
11942 template class nested within a template class.)
11943 This behavior is required for
11944 maybe_process_partial_specialization to work
11945 correctly, but is not accurate in this case;
11946 the TAG is not an instantiation of anything.
11947 (The corresponding TEMPLATE_DECL is an
11948 instantiation, but the TYPE is not.) */
11949 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11950
11951 /* Now, we call pushtag to put this NEWTAG into the scope of
11952 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11953 pushtag calling push_template_decl. We don't have to do
11954 this for enums because it will already have been done in
11955 tsubst_enum. */
11956 if (name)
11957 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11958 pushtag (name, newtag);
11959 }
11960 }
11961 else if (DECL_DECLARES_FUNCTION_P (t))
11962 {
11963 tree r;
11964
11965 if (TREE_CODE (t) == TEMPLATE_DECL)
11966 ++processing_template_decl;
11967 r = tsubst (t, args, tf_error, NULL_TREE);
11968 if (TREE_CODE (t) == TEMPLATE_DECL)
11969 --processing_template_decl;
11970 set_current_access_from_decl (r);
11971 finish_member_declaration (r);
11972 /* Instantiate members marked with attribute used. */
11973 if (r != error_mark_node && DECL_PRESERVE_P (r))
11974 used.safe_push (r);
11975 if (TREE_CODE (r) == FUNCTION_DECL
11976 && DECL_OMP_DECLARE_REDUCTION_P (r))
11977 cp_check_omp_declare_reduction (r);
11978 }
11979 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11980 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11981 /* A closure type for a lambda in an NSDMI or default argument.
11982 Ignore it; it will be regenerated when needed. */;
11983 else
11984 {
11985 /* Build new TYPE_FIELDS. */
11986 if (TREE_CODE (t) == STATIC_ASSERT)
11987 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11988 /*integral_constant_expression_p=*/true);
11989 else if (TREE_CODE (t) != CONST_DECL)
11990 {
11991 tree r;
11992 tree vec = NULL_TREE;
11993 int len = 1;
11994
11995 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11996 /* The file and line for this declaration, to
11997 assist in error message reporting. Since we
11998 called push_tinst_level above, we don't need to
11999 restore these. */
12000 input_location = DECL_SOURCE_LOCATION (t);
12001
12002 if (TREE_CODE (t) == TEMPLATE_DECL)
12003 ++processing_template_decl;
12004 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12005 if (TREE_CODE (t) == TEMPLATE_DECL)
12006 --processing_template_decl;
12007
12008 if (TREE_CODE (r) == TREE_VEC)
12009 {
12010 /* A capture pack became multiple fields. */
12011 vec = r;
12012 len = TREE_VEC_LENGTH (vec);
12013 }
12014
12015 for (int i = 0; i < len; ++i)
12016 {
12017 if (vec)
12018 r = TREE_VEC_ELT (vec, i);
12019 if (VAR_P (r))
12020 {
12021 /* In [temp.inst]:
12022
12023 [t]he initialization (and any associated
12024 side-effects) of a static data member does
12025 not occur unless the static data member is
12026 itself used in a way that requires the
12027 definition of the static data member to
12028 exist.
12029
12030 Therefore, we do not substitute into the
12031 initialized for the static data member here. */
12032 finish_static_data_member_decl
12033 (r,
12034 /*init=*/NULL_TREE,
12035 /*init_const_expr_p=*/false,
12036 /*asmspec_tree=*/NULL_TREE,
12037 /*flags=*/0);
12038 /* Instantiate members marked with attribute used. */
12039 if (r != error_mark_node && DECL_PRESERVE_P (r))
12040 used.safe_push (r);
12041 }
12042 else if (TREE_CODE (r) == FIELD_DECL)
12043 {
12044 /* Determine whether R has a valid type and can be
12045 completed later. If R is invalid, then its type
12046 is replaced by error_mark_node. */
12047 tree rtype = TREE_TYPE (r);
12048 if (can_complete_type_without_circularity (rtype))
12049 complete_type (rtype);
12050
12051 if (!complete_or_array_type_p (rtype))
12052 {
12053 /* If R's type couldn't be completed and
12054 it isn't a flexible array member (whose
12055 type is incomplete by definition) give
12056 an error. */
12057 cxx_incomplete_type_error (r, rtype);
12058 TREE_TYPE (r) = error_mark_node;
12059 }
12060 else if (TREE_CODE (rtype) == ARRAY_TYPE
12061 && TYPE_DOMAIN (rtype) == NULL_TREE
12062 && (TREE_CODE (type) == UNION_TYPE
12063 || TREE_CODE (type) == QUAL_UNION_TYPE))
12064 {
12065 error ("flexible array member %qD in union", r);
12066 TREE_TYPE (r) = error_mark_node;
12067 }
12068 else if (!verify_type_context (input_location,
12069 TCTX_FIELD, rtype))
12070 TREE_TYPE (r) = error_mark_node;
12071 }
12072
12073 /* If it is a TYPE_DECL for a class-scoped
12074 ENUMERAL_TYPE, such a thing will already have
12075 been added to the field list by tsubst_enum
12076 in finish_member_declaration case above. */
12077 if (!(TREE_CODE (r) == TYPE_DECL
12078 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12079 && DECL_ARTIFICIAL (r)))
12080 {
12081 set_current_access_from_decl (r);
12082 finish_member_declaration (r);
12083 }
12084 }
12085 }
12086 }
12087 }
12088 else
12089 {
12090 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12091 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12092 {
12093 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12094
12095 tree friend_type = t;
12096 bool adjust_processing_template_decl = false;
12097
12098 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12099 {
12100 /* template <class T> friend class C; */
12101 friend_type = tsubst_friend_class (friend_type, args);
12102 adjust_processing_template_decl = true;
12103 }
12104 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12105 {
12106 /* template <class T> friend class C::D; */
12107 friend_type = tsubst (friend_type, args,
12108 tf_warning_or_error, NULL_TREE);
12109 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12110 friend_type = TREE_TYPE (friend_type);
12111 adjust_processing_template_decl = true;
12112 }
12113 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12114 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12115 {
12116 /* This could be either
12117
12118 friend class T::C;
12119
12120 when dependent_type_p is false or
12121
12122 template <class U> friend class T::C;
12123
12124 otherwise. */
12125 /* Bump processing_template_decl in case this is something like
12126 template <class T> friend struct A<T>::B. */
12127 ++processing_template_decl;
12128 friend_type = tsubst (friend_type, args,
12129 tf_warning_or_error, NULL_TREE);
12130 if (dependent_type_p (friend_type))
12131 adjust_processing_template_decl = true;
12132 --processing_template_decl;
12133 }
12134 else if (uses_template_parms (friend_type))
12135 /* friend class C<T>; */
12136 friend_type = tsubst (friend_type, args,
12137 tf_warning_or_error, NULL_TREE);
12138
12139 /* Otherwise it's
12140
12141 friend class C;
12142
12143 where C is already declared or
12144
12145 friend class C<int>;
12146
12147 We don't have to do anything in these cases. */
12148
12149 if (adjust_processing_template_decl)
12150 /* Trick make_friend_class into realizing that the friend
12151 we're adding is a template, not an ordinary class. It's
12152 important that we use make_friend_class since it will
12153 perform some error-checking and output cross-reference
12154 information. */
12155 ++processing_template_decl;
12156
12157 if (friend_type != error_mark_node)
12158 make_friend_class (type, friend_type, /*complain=*/false);
12159
12160 if (adjust_processing_template_decl)
12161 --processing_template_decl;
12162 }
12163 else
12164 {
12165 /* Build new DECL_FRIENDLIST. */
12166 tree r;
12167
12168 /* The file and line for this declaration, to
12169 assist in error message reporting. Since we
12170 called push_tinst_level above, we don't need to
12171 restore these. */
12172 input_location = DECL_SOURCE_LOCATION (t);
12173
12174 if (TREE_CODE (t) == TEMPLATE_DECL)
12175 {
12176 ++processing_template_decl;
12177 push_deferring_access_checks (dk_no_check);
12178 }
12179
12180 r = tsubst_friend_function (t, args);
12181 add_friend (type, r, /*complain=*/false);
12182 if (TREE_CODE (t) == TEMPLATE_DECL)
12183 {
12184 pop_deferring_access_checks ();
12185 --processing_template_decl;
12186 }
12187 }
12188 }
12189 }
12190
12191 if (fn_context)
12192 {
12193 /* Restore these before substituting into the lambda capture
12194 initializers. */
12195 cp_unevaluated_operand = saved_unevaluated_operand;
12196 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12197 }
12198
12199 /* Set the file and line number information to whatever is given for
12200 the class itself. This puts error messages involving generated
12201 implicit functions at a predictable point, and the same point
12202 that would be used for non-template classes. */
12203 input_location = DECL_SOURCE_LOCATION (typedecl);
12204
12205 unreverse_member_declarations (type);
12206 finish_struct_1 (type);
12207 TYPE_BEING_DEFINED (type) = 0;
12208
12209 /* We don't instantiate default arguments for member functions. 14.7.1:
12210
12211 The implicit instantiation of a class template specialization causes
12212 the implicit instantiation of the declarations, but not of the
12213 definitions or default arguments, of the class member functions,
12214 member classes, static data members and member templates.... */
12215
12216 perform_instantiation_time_access_checks (pattern, args);
12217 perform_deferred_access_checks (tf_warning_or_error);
12218 pop_nested_class ();
12219 maximum_field_alignment = saved_maximum_field_alignment;
12220 if (!fn_context)
12221 pop_from_top_level ();
12222 pop_tinst_level ();
12223
12224 /* The vtable for a template class can be emitted in any translation
12225 unit in which the class is instantiated. When there is no key
12226 method, however, finish_struct_1 will already have added TYPE to
12227 the keyed_classes. */
12228 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12229 vec_safe_push (keyed_classes, type);
12230
12231 /* Now that we've gone through all the members, instantiate those
12232 marked with attribute used. */
12233 for (tree x : used)
12234 mark_used (x);
12235
12236 return type;
12237 }
12238
12239 /* Wrapper for instantiate_class_template_1. */
12240
12241 tree
12242 instantiate_class_template (tree type)
12243 {
12244 tree ret;
12245 timevar_push (TV_TEMPLATE_INST);
12246 ret = instantiate_class_template_1 (type);
12247 timevar_pop (TV_TEMPLATE_INST);
12248 return ret;
12249 }
12250
12251 tree
12252 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12253 {
12254 tree r;
12255
12256 if (!t)
12257 r = t;
12258 else if (TYPE_P (t))
12259 r = tsubst (t, args, complain, in_decl);
12260 else
12261 {
12262 if (!(complain & tf_warning))
12263 ++c_inhibit_evaluation_warnings;
12264 r = tsubst_expr (t, args, complain, in_decl,
12265 /*integral_constant_expression_p=*/true);
12266 if (!(complain & tf_warning))
12267 --c_inhibit_evaluation_warnings;
12268 }
12269
12270 return r;
12271 }
12272
12273 /* Given a function parameter pack TMPL_PARM and some function parameters
12274 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12275 and set *SPEC_P to point at the next point in the list. */
12276
12277 tree
12278 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12279 {
12280 /* Collect all of the extra "packed" parameters into an
12281 argument pack. */
12282 tree parmvec;
12283 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12284 tree spec_parm = *spec_p;
12285 int i, len;
12286
12287 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12288 if (tmpl_parm
12289 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12290 break;
12291
12292 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12293 parmvec = make_tree_vec (len);
12294 spec_parm = *spec_p;
12295 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12296 {
12297 tree elt = spec_parm;
12298 if (DECL_PACK_P (elt))
12299 elt = make_pack_expansion (elt);
12300 TREE_VEC_ELT (parmvec, i) = elt;
12301 }
12302
12303 /* Build the argument packs. */
12304 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12305 *spec_p = spec_parm;
12306
12307 return argpack;
12308 }
12309
12310 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12311 NONTYPE_ARGUMENT_PACK. */
12312
12313 static tree
12314 make_fnparm_pack (tree spec_parm)
12315 {
12316 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12317 }
12318
12319 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12320 pack expansion with no extra args, 2 if it has extra args, or 0
12321 if it is not a pack expansion. */
12322
12323 static int
12324 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12325 {
12326 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12327 /* We're being called before this happens in tsubst_pack_expansion. */
12328 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12329 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12330 if (i >= TREE_VEC_LENGTH (vec))
12331 return 0;
12332 tree elt = TREE_VEC_ELT (vec, i);
12333 if (DECL_P (elt))
12334 /* A decl pack is itself an expansion. */
12335 elt = TREE_TYPE (elt);
12336 if (!PACK_EXPANSION_P (elt))
12337 return 0;
12338 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12339 return 2;
12340 return 1;
12341 }
12342
12343
12344 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12345
12346 static tree
12347 make_argument_pack_select (tree arg_pack, unsigned index)
12348 {
12349 tree aps = make_node (ARGUMENT_PACK_SELECT);
12350
12351 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12352 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12353
12354 return aps;
12355 }
12356
12357 /* This is a subroutine of tsubst_pack_expansion.
12358
12359 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12360 mechanism to store the (non complete list of) arguments of the
12361 substitution and return a non substituted pack expansion, in order
12362 to wait for when we have enough arguments to really perform the
12363 substitution. */
12364
12365 static bool
12366 use_pack_expansion_extra_args_p (tree parm_packs,
12367 int arg_pack_len,
12368 bool has_empty_arg)
12369 {
12370 /* If one pack has an expansion and another pack has a normal
12371 argument or if one pack has an empty argument and an another
12372 one hasn't then tsubst_pack_expansion cannot perform the
12373 substitution and need to fall back on the
12374 PACK_EXPANSION_EXTRA mechanism. */
12375 if (parm_packs == NULL_TREE)
12376 return false;
12377 else if (has_empty_arg)
12378 {
12379 /* If all the actual packs are pack expansions, we can still
12380 subsitute directly. */
12381 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12382 {
12383 tree a = TREE_VALUE (p);
12384 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12385 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12386 a = ARGUMENT_PACK_ARGS (a);
12387 if (TREE_VEC_LENGTH (a) == 1)
12388 a = TREE_VEC_ELT (a, 0);
12389 if (PACK_EXPANSION_P (a))
12390 continue;
12391 return true;
12392 }
12393 return false;
12394 }
12395
12396 bool has_expansion_arg = false;
12397 for (int i = 0 ; i < arg_pack_len; ++i)
12398 {
12399 bool has_non_expansion_arg = false;
12400 for (tree parm_pack = parm_packs;
12401 parm_pack;
12402 parm_pack = TREE_CHAIN (parm_pack))
12403 {
12404 tree arg = TREE_VALUE (parm_pack);
12405
12406 int exp = argument_pack_element_is_expansion_p (arg, i);
12407 if (exp == 2)
12408 /* We can't substitute a pack expansion with extra args into
12409 our pattern. */
12410 return true;
12411 else if (exp)
12412 has_expansion_arg = true;
12413 else
12414 has_non_expansion_arg = true;
12415 }
12416
12417 if (has_expansion_arg && has_non_expansion_arg)
12418 return true;
12419 }
12420 return false;
12421 }
12422
12423 /* [temp.variadic]/6 says that:
12424
12425 The instantiation of a pack expansion [...]
12426 produces a list E1,E2, ..., En, where N is the number of elements
12427 in the pack expansion parameters.
12428
12429 This subroutine of tsubst_pack_expansion produces one of these Ei.
12430
12431 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12432 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12433 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12434 INDEX is the index 'i' of the element Ei to produce. ARGS,
12435 COMPLAIN, and IN_DECL are the same parameters as for the
12436 tsubst_pack_expansion function.
12437
12438 The function returns the resulting Ei upon successful completion,
12439 or error_mark_node.
12440
12441 Note that this function possibly modifies the ARGS parameter, so
12442 it's the responsibility of the caller to restore it. */
12443
12444 static tree
12445 gen_elem_of_pack_expansion_instantiation (tree pattern,
12446 tree parm_packs,
12447 unsigned index,
12448 tree args /* This parm gets
12449 modified. */,
12450 tsubst_flags_t complain,
12451 tree in_decl)
12452 {
12453 tree t;
12454 bool ith_elem_is_expansion = false;
12455
12456 /* For each parameter pack, change the substitution of the parameter
12457 pack to the ith argument in its argument pack, then expand the
12458 pattern. */
12459 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12460 {
12461 tree parm = TREE_PURPOSE (pack);
12462 tree arg_pack = TREE_VALUE (pack);
12463 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12464
12465 ith_elem_is_expansion |=
12466 argument_pack_element_is_expansion_p (arg_pack, index);
12467
12468 /* Select the Ith argument from the pack. */
12469 if (TREE_CODE (parm) == PARM_DECL
12470 || VAR_P (parm)
12471 || TREE_CODE (parm) == FIELD_DECL)
12472 {
12473 if (index == 0)
12474 {
12475 aps = make_argument_pack_select (arg_pack, index);
12476 if (!mark_used (parm, complain) && !(complain & tf_error))
12477 return error_mark_node;
12478 register_local_specialization (aps, parm);
12479 }
12480 else
12481 aps = retrieve_local_specialization (parm);
12482 }
12483 else
12484 {
12485 int idx, level;
12486 template_parm_level_and_index (parm, &level, &idx);
12487
12488 if (index == 0)
12489 {
12490 aps = make_argument_pack_select (arg_pack, index);
12491 /* Update the corresponding argument. */
12492 TMPL_ARG (args, level, idx) = aps;
12493 }
12494 else
12495 /* Re-use the ARGUMENT_PACK_SELECT. */
12496 aps = TMPL_ARG (args, level, idx);
12497 }
12498 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12499 }
12500
12501 /* Substitute into the PATTERN with the (possibly altered)
12502 arguments. */
12503 if (pattern == in_decl)
12504 /* Expanding a fixed parameter pack from
12505 coerce_template_parameter_pack. */
12506 t = tsubst_decl (pattern, args, complain);
12507 else if (pattern == error_mark_node)
12508 t = error_mark_node;
12509 else if (!TYPE_P (pattern))
12510 t = tsubst_expr (pattern, args, complain, in_decl,
12511 /*integral_constant_expression_p=*/false);
12512 else
12513 t = tsubst (pattern, args, complain, in_decl);
12514
12515 /* If the Ith argument pack element is a pack expansion, then
12516 the Ith element resulting from the substituting is going to
12517 be a pack expansion as well. */
12518 if (ith_elem_is_expansion)
12519 t = make_pack_expansion (t, complain);
12520
12521 return t;
12522 }
12523
12524 /* When the unexpanded parameter pack in a fold expression expands to an empty
12525 sequence, the value of the expression is as follows; the program is
12526 ill-formed if the operator is not listed in this table.
12527
12528 && true
12529 || false
12530 , void() */
12531
12532 tree
12533 expand_empty_fold (tree t, tsubst_flags_t complain)
12534 {
12535 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12536 if (!FOLD_EXPR_MODIFY_P (t))
12537 switch (code)
12538 {
12539 case TRUTH_ANDIF_EXPR:
12540 return boolean_true_node;
12541 case TRUTH_ORIF_EXPR:
12542 return boolean_false_node;
12543 case COMPOUND_EXPR:
12544 return void_node;
12545 default:
12546 break;
12547 }
12548
12549 if (complain & tf_error)
12550 error_at (location_of (t),
12551 "fold of empty expansion over %O", code);
12552 return error_mark_node;
12553 }
12554
12555 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12556 form an expression that combines the two terms using the
12557 operator of T. */
12558
12559 static tree
12560 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12561 {
12562 tree op = FOLD_EXPR_OP (t);
12563 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12564
12565 // Handle compound assignment operators.
12566 if (FOLD_EXPR_MODIFY_P (t))
12567 return build_x_modify_expr (input_location, left, code, right, complain);
12568
12569 warning_sentinel s(warn_parentheses);
12570 switch (code)
12571 {
12572 case COMPOUND_EXPR:
12573 return build_x_compound_expr (input_location, left, right, complain);
12574 default:
12575 return build_x_binary_op (input_location, code,
12576 left, TREE_CODE (left),
12577 right, TREE_CODE (right),
12578 /*overload=*/NULL,
12579 complain);
12580 }
12581 }
12582
12583 /* Substitute ARGS into the pack of a fold expression T. */
12584
12585 static inline tree
12586 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12587 {
12588 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12589 }
12590
12591 /* Substitute ARGS into the pack of a fold expression T. */
12592
12593 static inline tree
12594 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12595 {
12596 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12597 }
12598
12599 /* Expand a PACK of arguments into a grouped as left fold.
12600 Given a pack containing elements A0, A1, ..., An and an
12601 operator @, this builds the expression:
12602
12603 ((A0 @ A1) @ A2) ... @ An
12604
12605 Note that PACK must not be empty.
12606
12607 The operator is defined by the original fold expression T. */
12608
12609 static tree
12610 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12611 {
12612 tree left = TREE_VEC_ELT (pack, 0);
12613 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12614 {
12615 tree right = TREE_VEC_ELT (pack, i);
12616 left = fold_expression (t, left, right, complain);
12617 }
12618 return left;
12619 }
12620
12621 /* Substitute into a unary left fold expression. */
12622
12623 static tree
12624 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12625 tree in_decl)
12626 {
12627 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12628 if (pack == error_mark_node)
12629 return error_mark_node;
12630 if (PACK_EXPANSION_P (pack))
12631 {
12632 tree r = copy_node (t);
12633 FOLD_EXPR_PACK (r) = pack;
12634 return r;
12635 }
12636 if (TREE_VEC_LENGTH (pack) == 0)
12637 return expand_empty_fold (t, complain);
12638 else
12639 return expand_left_fold (t, pack, complain);
12640 }
12641
12642 /* Substitute into a binary left fold expression.
12643
12644 Do ths by building a single (non-empty) vector of argumnts and
12645 building the expression from those elements. */
12646
12647 static tree
12648 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12649 tree in_decl)
12650 {
12651 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12652 if (pack == error_mark_node)
12653 return error_mark_node;
12654 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12655 if (init == error_mark_node)
12656 return error_mark_node;
12657
12658 if (PACK_EXPANSION_P (pack))
12659 {
12660 tree r = copy_node (t);
12661 FOLD_EXPR_PACK (r) = pack;
12662 FOLD_EXPR_INIT (r) = init;
12663 return r;
12664 }
12665
12666 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12667 TREE_VEC_ELT (vec, 0) = init;
12668 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12669 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12670
12671 return expand_left_fold (t, vec, complain);
12672 }
12673
12674 /* Expand a PACK of arguments into a grouped as right fold.
12675 Given a pack containing elementns A0, A1, ..., and an
12676 operator @, this builds the expression:
12677
12678 A0@ ... (An-2 @ (An-1 @ An))
12679
12680 Note that PACK must not be empty.
12681
12682 The operator is defined by the original fold expression T. */
12683
12684 tree
12685 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12686 {
12687 // Build the expression.
12688 int n = TREE_VEC_LENGTH (pack);
12689 tree right = TREE_VEC_ELT (pack, n - 1);
12690 for (--n; n != 0; --n)
12691 {
12692 tree left = TREE_VEC_ELT (pack, n - 1);
12693 right = fold_expression (t, left, right, complain);
12694 }
12695 return right;
12696 }
12697
12698 /* Substitute into a unary right fold expression. */
12699
12700 static tree
12701 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12702 tree in_decl)
12703 {
12704 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12705 if (pack == error_mark_node)
12706 return error_mark_node;
12707 if (PACK_EXPANSION_P (pack))
12708 {
12709 tree r = copy_node (t);
12710 FOLD_EXPR_PACK (r) = pack;
12711 return r;
12712 }
12713 if (TREE_VEC_LENGTH (pack) == 0)
12714 return expand_empty_fold (t, complain);
12715 else
12716 return expand_right_fold (t, pack, complain);
12717 }
12718
12719 /* Substitute into a binary right fold expression.
12720
12721 Do ths by building a single (non-empty) vector of arguments and
12722 building the expression from those elements. */
12723
12724 static tree
12725 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12726 tree in_decl)
12727 {
12728 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12729 if (pack == error_mark_node)
12730 return error_mark_node;
12731 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12732 if (init == error_mark_node)
12733 return error_mark_node;
12734
12735 if (PACK_EXPANSION_P (pack))
12736 {
12737 tree r = copy_node (t);
12738 FOLD_EXPR_PACK (r) = pack;
12739 FOLD_EXPR_INIT (r) = init;
12740 return r;
12741 }
12742
12743 int n = TREE_VEC_LENGTH (pack);
12744 tree vec = make_tree_vec (n + 1);
12745 for (int i = 0; i < n; ++i)
12746 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12747 TREE_VEC_ELT (vec, n) = init;
12748
12749 return expand_right_fold (t, vec, complain);
12750 }
12751
12752 /* Walk through the pattern of a pack expansion, adding everything in
12753 local_specializations to a list. */
12754
12755 class el_data
12756 {
12757 public:
12758 hash_set<tree> internal;
12759 tree extra;
12760 tsubst_flags_t complain;
12761
12762 el_data (tsubst_flags_t c)
12763 : extra (NULL_TREE), complain (c) {}
12764 };
12765 static tree
12766 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12767 {
12768 el_data &data = *reinterpret_cast<el_data*>(data_);
12769 tree *extra = &data.extra;
12770 tsubst_flags_t complain = data.complain;
12771
12772 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12773 /* Remember local typedefs (85214). */
12774 tp = &TYPE_NAME (*tp);
12775
12776 if (TREE_CODE (*tp) == DECL_EXPR)
12777 data.internal.add (DECL_EXPR_DECL (*tp));
12778 else if (tree spec = retrieve_local_specialization (*tp))
12779 {
12780 if (data.internal.contains (*tp))
12781 /* Don't mess with variables declared within the pattern. */
12782 return NULL_TREE;
12783 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12784 {
12785 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12786 tree args = ARGUMENT_PACK_ARGS (spec);
12787 if (TREE_VEC_LENGTH (args) == 1)
12788 {
12789 tree elt = TREE_VEC_ELT (args, 0);
12790 if (PACK_EXPANSION_P (elt))
12791 elt = PACK_EXPANSION_PATTERN (elt);
12792 if (DECL_PACK_P (elt))
12793 spec = elt;
12794 }
12795 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12796 {
12797 /* Handle lambda capture here, since we aren't doing any
12798 substitution now, and so tsubst_copy won't call
12799 process_outer_var_ref. */
12800 tree args = ARGUMENT_PACK_ARGS (spec);
12801 int len = TREE_VEC_LENGTH (args);
12802 for (int i = 0; i < len; ++i)
12803 {
12804 tree arg = TREE_VEC_ELT (args, i);
12805 tree carg = arg;
12806 if (outer_automatic_var_p (arg))
12807 carg = process_outer_var_ref (arg, complain);
12808 if (carg != arg)
12809 {
12810 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12811 proxies. */
12812 if (i == 0)
12813 {
12814 spec = copy_node (spec);
12815 args = copy_node (args);
12816 SET_ARGUMENT_PACK_ARGS (spec, args);
12817 register_local_specialization (spec, *tp);
12818 }
12819 TREE_VEC_ELT (args, i) = carg;
12820 }
12821 }
12822 }
12823 }
12824 if (outer_automatic_var_p (spec))
12825 spec = process_outer_var_ref (spec, complain);
12826 *extra = tree_cons (*tp, spec, *extra);
12827 }
12828 return NULL_TREE;
12829 }
12830 static tree
12831 extract_local_specs (tree pattern, tsubst_flags_t complain)
12832 {
12833 el_data data (complain);
12834 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12835 return data.extra;
12836 }
12837
12838 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12839 for use in PACK_EXPANSION_EXTRA_ARGS. */
12840
12841 tree
12842 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12843 {
12844 tree extra = args;
12845 if (local_specializations)
12846 if (tree locals = extract_local_specs (pattern, complain))
12847 extra = tree_cons (NULL_TREE, extra, locals);
12848 return extra;
12849 }
12850
12851 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12852 normal template args to ARGS. */
12853
12854 tree
12855 add_extra_args (tree extra, tree args)
12856 {
12857 if (extra && TREE_CODE (extra) == TREE_LIST)
12858 {
12859 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12860 {
12861 /* The partial instantiation involved local declarations collected in
12862 extract_local_specs; map from the general template to our local
12863 context. */
12864 tree gen = TREE_PURPOSE (elt);
12865 tree inst = TREE_VALUE (elt);
12866 if (DECL_P (inst))
12867 if (tree local = retrieve_local_specialization (inst))
12868 inst = local;
12869 /* else inst is already a full instantiation of the pack. */
12870 register_local_specialization (inst, gen);
12871 }
12872 gcc_assert (!TREE_PURPOSE (extra));
12873 extra = TREE_VALUE (extra);
12874 }
12875 #if 1
12876 /* I think we should always be able to substitute dependent args into the
12877 pattern. If that turns out to be incorrect in some cases, enable the
12878 alternate code (and add complain/in_decl parms to this function). */
12879 gcc_checking_assert (!uses_template_parms (extra));
12880 #else
12881 if (!uses_template_parms (extra))
12882 {
12883 gcc_unreachable ();
12884 extra = tsubst_template_args (extra, args, complain, in_decl);
12885 args = add_outermost_template_args (args, extra);
12886 }
12887 else
12888 #endif
12889 args = add_to_template_args (extra, args);
12890 return args;
12891 }
12892
12893 /* Substitute ARGS into T, which is an pack expansion
12894 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12895 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12896 (if only a partial substitution could be performed) or
12897 ERROR_MARK_NODE if there was an error. */
12898 tree
12899 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12900 tree in_decl)
12901 {
12902 tree pattern;
12903 tree pack, packs = NULL_TREE;
12904 bool unsubstituted_packs = false;
12905 int i, len = -1;
12906 tree result;
12907 bool need_local_specializations = false;
12908 int levels;
12909
12910 gcc_assert (PACK_EXPANSION_P (t));
12911 pattern = PACK_EXPANSION_PATTERN (t);
12912
12913 /* Add in any args remembered from an earlier partial instantiation. */
12914 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12915
12916 levels = TMPL_ARGS_DEPTH (args);
12917
12918 /* Determine the argument packs that will instantiate the parameter
12919 packs used in the expansion expression. While we're at it,
12920 compute the number of arguments to be expanded and make sure it
12921 is consistent. */
12922 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12923 pack = TREE_CHAIN (pack))
12924 {
12925 tree parm_pack = TREE_VALUE (pack);
12926 tree arg_pack = NULL_TREE;
12927 tree orig_arg = NULL_TREE;
12928 int level = 0;
12929
12930 if (TREE_CODE (parm_pack) == BASES)
12931 {
12932 gcc_assert (parm_pack == pattern);
12933 if (BASES_DIRECT (parm_pack))
12934 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12935 args, complain,
12936 in_decl, false),
12937 complain);
12938 else
12939 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12940 args, complain, in_decl,
12941 false), complain);
12942 }
12943 else if (builtin_pack_call_p (parm_pack))
12944 {
12945 if (parm_pack != pattern)
12946 {
12947 if (complain & tf_error)
12948 sorry ("%qE is not the entire pattern of the pack expansion",
12949 parm_pack);
12950 return error_mark_node;
12951 }
12952 return expand_builtin_pack_call (parm_pack, args,
12953 complain, in_decl);
12954 }
12955 else if (TREE_CODE (parm_pack) == PARM_DECL)
12956 {
12957 /* We know we have correct local_specializations if this
12958 expansion is at function scope, or if we're dealing with a
12959 local parameter in a requires expression; for the latter,
12960 tsubst_requires_expr set it up appropriately. */
12961 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12962 arg_pack = retrieve_local_specialization (parm_pack);
12963 else
12964 /* We can't rely on local_specializations for a parameter
12965 name used later in a function declaration (such as in a
12966 late-specified return type). Even if it exists, it might
12967 have the wrong value for a recursive call. */
12968 need_local_specializations = true;
12969
12970 if (!arg_pack)
12971 {
12972 /* This parameter pack was used in an unevaluated context. Just
12973 make a dummy decl, since it's only used for its type. */
12974 ++cp_unevaluated_operand;
12975 arg_pack = tsubst_decl (parm_pack, args, complain);
12976 --cp_unevaluated_operand;
12977 if (arg_pack && DECL_PACK_P (arg_pack))
12978 /* Partial instantiation of the parm_pack, we can't build
12979 up an argument pack yet. */
12980 arg_pack = NULL_TREE;
12981 else
12982 arg_pack = make_fnparm_pack (arg_pack);
12983 }
12984 else if (DECL_PACK_P (arg_pack))
12985 /* This argument pack isn't fully instantiated yet. */
12986 arg_pack = NULL_TREE;
12987 }
12988 else if (is_capture_proxy (parm_pack))
12989 {
12990 arg_pack = retrieve_local_specialization (parm_pack);
12991 if (DECL_PACK_P (arg_pack))
12992 arg_pack = NULL_TREE;
12993 }
12994 else
12995 {
12996 int idx;
12997 template_parm_level_and_index (parm_pack, &level, &idx);
12998 if (level <= levels)
12999 arg_pack = TMPL_ARG (args, level, idx);
13000
13001 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13002 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13003 arg_pack = NULL_TREE;
13004 }
13005
13006 orig_arg = arg_pack;
13007 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13008 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13009
13010 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13011 /* This can only happen if we forget to expand an argument
13012 pack somewhere else. Just return an error, silently. */
13013 {
13014 result = make_tree_vec (1);
13015 TREE_VEC_ELT (result, 0) = error_mark_node;
13016 return result;
13017 }
13018
13019 if (arg_pack)
13020 {
13021 int my_len =
13022 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13023
13024 /* Don't bother trying to do a partial substitution with
13025 incomplete packs; we'll try again after deduction. */
13026 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13027 return t;
13028
13029 if (len < 0)
13030 len = my_len;
13031 else if (len != my_len)
13032 {
13033 if (!(complain & tf_error))
13034 /* Fail quietly. */;
13035 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13036 error ("mismatched argument pack lengths while expanding %qT",
13037 pattern);
13038 else
13039 error ("mismatched argument pack lengths while expanding %qE",
13040 pattern);
13041 return error_mark_node;
13042 }
13043
13044 /* Keep track of the parameter packs and their corresponding
13045 argument packs. */
13046 packs = tree_cons (parm_pack, arg_pack, packs);
13047 TREE_TYPE (packs) = orig_arg;
13048 }
13049 else
13050 {
13051 /* We can't substitute for this parameter pack. We use a flag as
13052 well as the missing_level counter because function parameter
13053 packs don't have a level. */
13054 gcc_assert (processing_template_decl || is_auto (parm_pack));
13055 unsubstituted_packs = true;
13056 }
13057 }
13058
13059 /* If the expansion is just T..., return the matching argument pack, unless
13060 we need to call convert_from_reference on all the elements. This is an
13061 important optimization; see c++/68422. */
13062 if (!unsubstituted_packs
13063 && TREE_PURPOSE (packs) == pattern)
13064 {
13065 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13066
13067 /* If the argument pack is a single pack expansion, pull it out. */
13068 if (TREE_VEC_LENGTH (args) == 1
13069 && pack_expansion_args_count (args))
13070 return TREE_VEC_ELT (args, 0);
13071
13072 /* Types need no adjustment, nor does sizeof..., and if we still have
13073 some pack expansion args we won't do anything yet. */
13074 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13075 || PACK_EXPANSION_SIZEOF_P (t)
13076 || pack_expansion_args_count (args))
13077 return args;
13078 /* Also optimize expression pack expansions if we can tell that the
13079 elements won't have reference type. */
13080 tree type = TREE_TYPE (pattern);
13081 if (type && !TYPE_REF_P (type)
13082 && !PACK_EXPANSION_P (type)
13083 && !WILDCARD_TYPE_P (type))
13084 return args;
13085 /* Otherwise use the normal path so we get convert_from_reference. */
13086 }
13087
13088 /* We cannot expand this expansion expression, because we don't have
13089 all of the argument packs we need. */
13090 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13091 {
13092 /* We got some full packs, but we can't substitute them in until we
13093 have values for all the packs. So remember these until then. */
13094
13095 t = make_pack_expansion (pattern, complain);
13096 PACK_EXPANSION_EXTRA_ARGS (t)
13097 = build_extra_args (pattern, args, complain);
13098 return t;
13099 }
13100
13101 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13102 type, so create our own local specializations map; the current map is
13103 either NULL or (in the case of recursive unification) might have
13104 bindings that we don't want to use or alter. */
13105 local_specialization_stack lss (need_local_specializations
13106 ? lss_blank : lss_nop);
13107
13108 if (unsubstituted_packs)
13109 {
13110 /* There were no real arguments, we're just replacing a parameter
13111 pack with another version of itself. Substitute into the
13112 pattern and return a PACK_EXPANSION_*. The caller will need to
13113 deal with that. */
13114 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13115 t = tsubst_expr (pattern, args, complain, in_decl,
13116 /*integral_constant_expression_p=*/false);
13117 else
13118 t = tsubst (pattern, args, complain, in_decl);
13119 t = make_pack_expansion (t, complain);
13120 return t;
13121 }
13122
13123 gcc_assert (len >= 0);
13124
13125 /* For each argument in each argument pack, substitute into the
13126 pattern. */
13127 result = make_tree_vec (len);
13128 tree elem_args = copy_template_args (args);
13129 for (i = 0; i < len; ++i)
13130 {
13131 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13132 i,
13133 elem_args, complain,
13134 in_decl);
13135 TREE_VEC_ELT (result, i) = t;
13136 if (t == error_mark_node)
13137 {
13138 result = error_mark_node;
13139 break;
13140 }
13141 }
13142
13143 /* Update ARGS to restore the substitution from parameter packs to
13144 their argument packs. */
13145 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13146 {
13147 tree parm = TREE_PURPOSE (pack);
13148
13149 if (TREE_CODE (parm) == PARM_DECL
13150 || VAR_P (parm)
13151 || TREE_CODE (parm) == FIELD_DECL)
13152 register_local_specialization (TREE_TYPE (pack), parm);
13153 else
13154 {
13155 int idx, level;
13156
13157 if (TREE_VALUE (pack) == NULL_TREE)
13158 continue;
13159
13160 template_parm_level_and_index (parm, &level, &idx);
13161
13162 /* Update the corresponding argument. */
13163 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13164 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13165 TREE_TYPE (pack);
13166 else
13167 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13168 }
13169 }
13170
13171 /* If the dependent pack arguments were such that we end up with only a
13172 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13173 if (len == 1 && TREE_CODE (result) == TREE_VEC
13174 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13175 return TREE_VEC_ELT (result, 0);
13176
13177 return result;
13178 }
13179
13180 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13181 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13182 parameter packs; all parms generated from a function parameter pack will
13183 have the same DECL_PARM_INDEX. */
13184
13185 tree
13186 get_pattern_parm (tree parm, tree tmpl)
13187 {
13188 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13189 tree patparm;
13190
13191 if (DECL_ARTIFICIAL (parm))
13192 {
13193 for (patparm = DECL_ARGUMENTS (pattern);
13194 patparm; patparm = DECL_CHAIN (patparm))
13195 if (DECL_ARTIFICIAL (patparm)
13196 && DECL_NAME (parm) == DECL_NAME (patparm))
13197 break;
13198 }
13199 else
13200 {
13201 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13202 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13203 gcc_assert (DECL_PARM_INDEX (patparm)
13204 == DECL_PARM_INDEX (parm));
13205 }
13206
13207 return patparm;
13208 }
13209
13210 /* Make an argument pack out of the TREE_VEC VEC. */
13211
13212 static tree
13213 make_argument_pack (tree vec)
13214 {
13215 tree pack;
13216
13217 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13218 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13219 else
13220 {
13221 pack = make_node (NONTYPE_ARGUMENT_PACK);
13222 TREE_CONSTANT (pack) = 1;
13223 }
13224 SET_ARGUMENT_PACK_ARGS (pack, vec);
13225 return pack;
13226 }
13227
13228 /* Return an exact copy of template args T that can be modified
13229 independently. */
13230
13231 static tree
13232 copy_template_args (tree t)
13233 {
13234 if (t == error_mark_node)
13235 return t;
13236
13237 int len = TREE_VEC_LENGTH (t);
13238 tree new_vec = make_tree_vec (len);
13239
13240 for (int i = 0; i < len; ++i)
13241 {
13242 tree elt = TREE_VEC_ELT (t, i);
13243 if (elt && TREE_CODE (elt) == TREE_VEC)
13244 elt = copy_template_args (elt);
13245 TREE_VEC_ELT (new_vec, i) = elt;
13246 }
13247
13248 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13249 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13250
13251 return new_vec;
13252 }
13253
13254 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13255
13256 tree
13257 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13258 tree in_decl)
13259 {
13260 /* Substitute into each of the arguments. */
13261 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13262 args, complain, in_decl);
13263 tree new_arg = error_mark_node;
13264 if (pack_args != error_mark_node)
13265 {
13266 if (TYPE_P (orig_arg))
13267 {
13268 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13269 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13270 }
13271 else
13272 {
13273 new_arg = make_node (TREE_CODE (orig_arg));
13274 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13275 }
13276
13277 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13278 }
13279
13280 return new_arg;
13281 }
13282
13283 /* Substitute ARGS into the vector or list of template arguments T. */
13284
13285 tree
13286 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13287 {
13288 tree orig_t = t;
13289 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13290 tree *elts;
13291
13292 if (t == error_mark_node)
13293 return error_mark_node;
13294
13295 len = TREE_VEC_LENGTH (t);
13296 elts = XALLOCAVEC (tree, len);
13297
13298 for (i = 0; i < len; i++)
13299 {
13300 tree orig_arg = TREE_VEC_ELT (t, i);
13301 tree new_arg;
13302
13303 if (TREE_CODE (orig_arg) == TREE_VEC)
13304 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13305 else if (PACK_EXPANSION_P (orig_arg))
13306 {
13307 /* Substitute into an expansion expression. */
13308 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13309
13310 if (TREE_CODE (new_arg) == TREE_VEC)
13311 /* Add to the expanded length adjustment the number of
13312 expanded arguments. We subtract one from this
13313 measurement, because the argument pack expression
13314 itself is already counted as 1 in
13315 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13316 the argument pack is empty. */
13317 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13318 }
13319 else if (ARGUMENT_PACK_P (orig_arg))
13320 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13321 else
13322 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13323
13324 if (new_arg == error_mark_node)
13325 return error_mark_node;
13326
13327 elts[i] = new_arg;
13328 if (new_arg != orig_arg)
13329 need_new = 1;
13330 }
13331
13332 if (!need_new)
13333 return t;
13334
13335 /* Make space for the expanded arguments coming from template
13336 argument packs. */
13337 t = make_tree_vec (len + expanded_len_adjust);
13338 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13339 arguments for a member template.
13340 In that case each TREE_VEC in ORIG_T represents a level of template
13341 arguments, and ORIG_T won't carry any non defaulted argument count.
13342 It will rather be the nested TREE_VECs that will carry one.
13343 In other words, ORIG_T carries a non defaulted argument count only
13344 if it doesn't contain any nested TREE_VEC. */
13345 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13346 {
13347 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13348 count += expanded_len_adjust;
13349 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13350 }
13351 for (i = 0, out = 0; i < len; i++)
13352 {
13353 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13354 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13355 && TREE_CODE (elts[i]) == TREE_VEC)
13356 {
13357 int idx;
13358
13359 /* Now expand the template argument pack "in place". */
13360 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13361 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13362 }
13363 else
13364 {
13365 TREE_VEC_ELT (t, out) = elts[i];
13366 out++;
13367 }
13368 }
13369
13370 return t;
13371 }
13372
13373 /* Substitute ARGS into one level PARMS of template parameters. */
13374
13375 static tree
13376 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13377 {
13378 if (parms == error_mark_node)
13379 return error_mark_node;
13380
13381 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13382
13383 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13384 {
13385 tree tuple = TREE_VEC_ELT (parms, i);
13386
13387 if (tuple == error_mark_node)
13388 continue;
13389
13390 TREE_VEC_ELT (new_vec, i) =
13391 tsubst_template_parm (tuple, args, complain);
13392 }
13393
13394 return new_vec;
13395 }
13396
13397 /* Return the result of substituting ARGS into the template parameters
13398 given by PARMS. If there are m levels of ARGS and m + n levels of
13399 PARMS, then the result will contain n levels of PARMS. For
13400 example, if PARMS is `template <class T> template <class U>
13401 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13402 result will be `template <int*, double, class V>'. */
13403
13404 static tree
13405 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13406 {
13407 tree r = NULL_TREE;
13408 tree* new_parms;
13409
13410 /* When substituting into a template, we must set
13411 PROCESSING_TEMPLATE_DECL as the template parameters may be
13412 dependent if they are based on one-another, and the dependency
13413 predicates are short-circuit outside of templates. */
13414 ++processing_template_decl;
13415
13416 for (new_parms = &r;
13417 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13418 new_parms = &(TREE_CHAIN (*new_parms)),
13419 parms = TREE_CHAIN (parms))
13420 {
13421 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13422 args, complain);
13423 *new_parms =
13424 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13425 - TMPL_ARGS_DEPTH (args)),
13426 new_vec, NULL_TREE);
13427 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13428 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13429 }
13430
13431 --processing_template_decl;
13432
13433 return r;
13434 }
13435
13436 /* Return the result of substituting ARGS into one template parameter
13437 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13438 parameter and which TREE_PURPOSE is the default argument of the
13439 template parameter. */
13440
13441 static tree
13442 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13443 {
13444 tree default_value, parm_decl;
13445
13446 if (args == NULL_TREE
13447 || t == NULL_TREE
13448 || t == error_mark_node)
13449 return t;
13450
13451 gcc_assert (TREE_CODE (t) == TREE_LIST);
13452
13453 default_value = TREE_PURPOSE (t);
13454 parm_decl = TREE_VALUE (t);
13455 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13456
13457 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13458 if (TREE_CODE (parm_decl) == PARM_DECL
13459 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13460 parm_decl = error_mark_node;
13461 default_value = tsubst_template_arg (default_value, args,
13462 complain, NULL_TREE);
13463 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13464
13465 tree r = build_tree_list (default_value, parm_decl);
13466 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13467 return r;
13468 }
13469
13470 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13471 type T. If T is not an aggregate or enumeration type, it is
13472 handled as if by tsubst. IN_DECL is as for tsubst. If
13473 ENTERING_SCOPE is nonzero, T is the context for a template which
13474 we are presently tsubst'ing. Return the substituted value. */
13475
13476 static tree
13477 tsubst_aggr_type (tree t,
13478 tree args,
13479 tsubst_flags_t complain,
13480 tree in_decl,
13481 int entering_scope)
13482 {
13483 if (t == NULL_TREE)
13484 return NULL_TREE;
13485
13486 switch (TREE_CODE (t))
13487 {
13488 case RECORD_TYPE:
13489 if (TYPE_PTRMEMFUNC_P (t))
13490 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13491
13492 /* Fall through. */
13493 case ENUMERAL_TYPE:
13494 case UNION_TYPE:
13495 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13496 {
13497 tree argvec;
13498 tree context;
13499 tree r;
13500
13501 /* In "sizeof(X<I>)" we need to evaluate "I". */
13502 cp_evaluated ev;
13503
13504 /* First, determine the context for the type we are looking
13505 up. */
13506 context = TYPE_CONTEXT (t);
13507 if (context && TYPE_P (context))
13508 {
13509 context = tsubst_aggr_type (context, args, complain,
13510 in_decl, /*entering_scope=*/1);
13511 /* If context is a nested class inside a class template,
13512 it may still need to be instantiated (c++/33959). */
13513 context = complete_type (context);
13514 }
13515
13516 /* Then, figure out what arguments are appropriate for the
13517 type we are trying to find. For example, given:
13518
13519 template <class T> struct S;
13520 template <class T, class U> void f(T, U) { S<U> su; }
13521
13522 and supposing that we are instantiating f<int, double>,
13523 then our ARGS will be {int, double}, but, when looking up
13524 S we only want {double}. */
13525 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13526 complain, in_decl);
13527 if (argvec == error_mark_node)
13528 r = error_mark_node;
13529 else if (!entering_scope
13530 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13531 {
13532 /* See maybe_dependent_member_ref. */
13533 tree name = TYPE_IDENTIFIER (t);
13534 tree fullname = name;
13535 if (instantiates_primary_template_p (t))
13536 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13537 INNERMOST_TEMPLATE_ARGS (argvec));
13538 return build_typename_type (context, name, fullname,
13539 typename_type);
13540 }
13541 else
13542 {
13543 r = lookup_template_class (t, argvec, in_decl, context,
13544 entering_scope, complain);
13545 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13546 }
13547
13548 return r;
13549 }
13550 else
13551 /* This is not a template type, so there's nothing to do. */
13552 return t;
13553
13554 default:
13555 return tsubst (t, args, complain, in_decl);
13556 }
13557 }
13558
13559 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13560
13561 /* Substitute into the default argument ARG (a default argument for
13562 FN), which has the indicated TYPE. */
13563
13564 tree
13565 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13566 tsubst_flags_t complain)
13567 {
13568 int errs = errorcount + sorrycount;
13569
13570 /* This can happen in invalid code. */
13571 if (TREE_CODE (arg) == DEFERRED_PARSE)
13572 return arg;
13573
13574 /* Shortcut {}. */
13575 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13576 && CONSTRUCTOR_NELTS (arg) == 0)
13577 return arg;
13578
13579 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13580 parm = chain_index (parmnum, parm);
13581 tree parmtype = TREE_TYPE (parm);
13582 if (DECL_BY_REFERENCE (parm))
13583 parmtype = TREE_TYPE (parmtype);
13584 if (parmtype == error_mark_node)
13585 return error_mark_node;
13586
13587 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13588
13589 tree *slot;
13590 if (defarg_inst && (slot = defarg_inst->get (parm)))
13591 return *slot;
13592
13593 /* This default argument came from a template. Instantiate the
13594 default argument here, not in tsubst. In the case of
13595 something like:
13596
13597 template <class T>
13598 struct S {
13599 static T t();
13600 void f(T = t());
13601 };
13602
13603 we must be careful to do name lookup in the scope of S<T>,
13604 rather than in the current class. */
13605 push_to_top_level ();
13606 push_access_scope (fn);
13607 push_deferring_access_checks (dk_no_deferred);
13608 start_lambda_scope (parm);
13609
13610 /* The default argument expression may cause implicitly defined
13611 member functions to be synthesized, which will result in garbage
13612 collection. We must treat this situation as if we were within
13613 the body of function so as to avoid collecting live data on the
13614 stack. */
13615 ++function_depth;
13616 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13617 complain, NULL_TREE,
13618 /*integral_constant_expression_p=*/false);
13619 --function_depth;
13620
13621 finish_lambda_scope ();
13622
13623 /* Make sure the default argument is reasonable. */
13624 arg = check_default_argument (type, arg, complain);
13625
13626 if (errorcount+sorrycount > errs
13627 && (complain & tf_warning_or_error))
13628 inform (input_location,
13629 " when instantiating default argument for call to %qD", fn);
13630
13631 pop_deferring_access_checks ();
13632 pop_access_scope (fn);
13633 pop_from_top_level ();
13634
13635 if (arg != error_mark_node && !cp_unevaluated_operand)
13636 {
13637 if (!defarg_inst)
13638 defarg_inst = decl_tree_cache_map::create_ggc (37);
13639 defarg_inst->put (parm, arg);
13640 }
13641
13642 return arg;
13643 }
13644
13645 /* Substitute into all the default arguments for FN. */
13646
13647 static void
13648 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13649 {
13650 tree arg;
13651 tree tmpl_args;
13652
13653 tmpl_args = DECL_TI_ARGS (fn);
13654
13655 /* If this function is not yet instantiated, we certainly don't need
13656 its default arguments. */
13657 if (uses_template_parms (tmpl_args))
13658 return;
13659 /* Don't do this again for clones. */
13660 if (DECL_CLONED_FUNCTION_P (fn))
13661 return;
13662
13663 int i = 0;
13664 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13665 arg;
13666 arg = TREE_CHAIN (arg), ++i)
13667 if (TREE_PURPOSE (arg))
13668 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13669 TREE_VALUE (arg),
13670 TREE_PURPOSE (arg),
13671 complain);
13672 }
13673
13674 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13675 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13676
13677 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13678
13679 void
13680 store_explicit_specifier (tree v, tree t)
13681 {
13682 if (!explicit_specifier_map)
13683 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13684 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13685 explicit_specifier_map->put (v, t);
13686 }
13687
13688 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13689
13690 static tree
13691 lookup_explicit_specifier (tree v)
13692 {
13693 return *explicit_specifier_map->get (v);
13694 }
13695
13696 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13697 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13698 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13699 identical to T. */
13700
13701 static tree
13702 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13703 tree raises, tsubst_flags_t complain)
13704 {
13705 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13706
13707 tree new_type;
13708 if (TREE_CODE (t) == FUNCTION_TYPE)
13709 {
13710 new_type = build_function_type (return_type, arg_types);
13711 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13712 }
13713 else
13714 {
13715 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13716 /* Don't pick up extra function qualifiers from the basetype. */
13717 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13718 if (! MAYBE_CLASS_TYPE_P (r))
13719 {
13720 /* [temp.deduct]
13721
13722 Type deduction may fail for any of the following
13723 reasons:
13724
13725 -- Attempting to create "pointer to member of T" when T
13726 is not a class type. */
13727 if (complain & tf_error)
13728 error ("creating pointer to member function of non-class type %qT",
13729 r);
13730 return error_mark_node;
13731 }
13732
13733 new_type = build_method_type_directly (r, return_type,
13734 TREE_CHAIN (arg_types));
13735 }
13736 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13737
13738 cp_ref_qualifier rqual = type_memfn_rqual (t);
13739 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13740 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13741 }
13742
13743 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13744 each of its formal parameters. If there is a disagreement then rebuild
13745 DECL's function type according to its formal parameter types, as part of a
13746 resolution for Core issues 1001/1322. */
13747
13748 static void
13749 maybe_rebuild_function_decl_type (tree decl)
13750 {
13751 bool function_type_needs_rebuilding = false;
13752 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13753 {
13754 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13755 while (parm_type_list && parm_type_list != void_list_node)
13756 {
13757 tree parm_type = TREE_VALUE (parm_type_list);
13758 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13759 if (!same_type_p (parm_type, formal_parm_type_unqual))
13760 {
13761 function_type_needs_rebuilding = true;
13762 break;
13763 }
13764
13765 parm_list = DECL_CHAIN (parm_list);
13766 parm_type_list = TREE_CHAIN (parm_type_list);
13767 }
13768 }
13769
13770 if (!function_type_needs_rebuilding)
13771 return;
13772
13773 const tree fntype = TREE_TYPE (decl);
13774 tree parm_list = DECL_ARGUMENTS (decl);
13775 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13776 tree new_parm_type_list = NULL_TREE;
13777 tree *q = &new_parm_type_list;
13778 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13779 {
13780 *q = copy_node (old_parm_type_list);
13781 parm_list = DECL_CHAIN (parm_list);
13782 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13783 q = &TREE_CHAIN (*q);
13784 }
13785 while (old_parm_type_list && old_parm_type_list != void_list_node)
13786 {
13787 *q = copy_node (old_parm_type_list);
13788 tree *new_parm_type = &TREE_VALUE (*q);
13789 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13790 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13791 *new_parm_type = formal_parm_type_unqual;
13792
13793 parm_list = DECL_CHAIN (parm_list);
13794 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13795 q = &TREE_CHAIN (*q);
13796 }
13797 if (old_parm_type_list == void_list_node)
13798 *q = void_list_node;
13799
13800 TREE_TYPE (decl)
13801 = rebuild_function_or_method_type (fntype,
13802 TREE_TYPE (fntype), new_parm_type_list,
13803 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13804 }
13805
13806 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13807
13808 static tree
13809 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13810 tree lambda_fntype)
13811 {
13812 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13813 hashval_t hash = 0;
13814 tree in_decl = t;
13815
13816 /* Nobody should be tsubst'ing into non-template functions. */
13817 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13818 || DECL_LOCAL_DECL_P (t));
13819
13820 if (DECL_LOCAL_DECL_P (t))
13821 {
13822 if (tree spec = retrieve_local_specialization (t))
13823 return spec;
13824 }
13825 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13826 {
13827 /* If T is not dependent, just return it. */
13828 if (!uses_template_parms (DECL_TI_ARGS (t))
13829 && !LAMBDA_FUNCTION_P (t))
13830 return t;
13831
13832 /* Calculate the most general template of which R is a
13833 specialization. */
13834 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13835
13836 /* We're substituting a lambda function under tsubst_lambda_expr but not
13837 directly from it; find the matching function we're already inside.
13838 But don't do this if T is a generic lambda with a single level of
13839 template parms, as in that case we're doing a normal instantiation. */
13840 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13841 && (!generic_lambda_fn_p (t)
13842 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13843 return enclosing_instantiation_of (t);
13844
13845 /* Calculate the complete set of arguments used to
13846 specialize R. */
13847 argvec = tsubst_template_args (DECL_TI_ARGS
13848 (DECL_TEMPLATE_RESULT
13849 (DECL_TI_TEMPLATE (t))),
13850 args, complain, in_decl);
13851 if (argvec == error_mark_node)
13852 return error_mark_node;
13853
13854 /* Check to see if we already have this specialization. */
13855 if (!lambda_fntype)
13856 {
13857 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13858 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13859 return spec;
13860 }
13861
13862 /* We can see more levels of arguments than parameters if
13863 there was a specialization of a member template, like
13864 this:
13865
13866 template <class T> struct S { template <class U> void f(); }
13867 template <> template <class U> void S<int>::f(U);
13868
13869 Here, we'll be substituting into the specialization,
13870 because that's where we can find the code we actually
13871 want to generate, but we'll have enough arguments for
13872 the most general template.
13873
13874 We also deal with the peculiar case:
13875
13876 template <class T> struct S {
13877 template <class U> friend void f();
13878 };
13879 template <class U> void f() {}
13880 template S<int>;
13881 template void f<double>();
13882
13883 Here, the ARGS for the instantiation of will be {int,
13884 double}. But, we only need as many ARGS as there are
13885 levels of template parameters in CODE_PATTERN. We are
13886 careful not to get fooled into reducing the ARGS in
13887 situations like:
13888
13889 template <class T> struct S { template <class U> void f(U); }
13890 template <class T> template <> void S<T>::f(int) {}
13891
13892 which we can spot because the pattern will be a
13893 specialization in this case. */
13894 int args_depth = TMPL_ARGS_DEPTH (args);
13895 int parms_depth =
13896 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13897
13898 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13899 args = get_innermost_template_args (args, parms_depth);
13900 }
13901 else
13902 {
13903 /* This special case arises when we have something like this:
13904
13905 template <class T> struct S {
13906 friend void f<int>(int, double);
13907 };
13908
13909 Here, the DECL_TI_TEMPLATE for the friend declaration
13910 will be an IDENTIFIER_NODE. We are being called from
13911 tsubst_friend_function, and we want only to create a
13912 new decl (R) with appropriate types so that we can call
13913 determine_specialization. */
13914 gen_tmpl = NULL_TREE;
13915 argvec = NULL_TREE;
13916 }
13917
13918 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13919 : NULL_TREE);
13920 tree ctx = closure ? closure : DECL_CONTEXT (t);
13921 bool member = ctx && TYPE_P (ctx);
13922
13923 if (member && !closure)
13924 ctx = tsubst_aggr_type (ctx, args,
13925 complain, t, /*entering_scope=*/1);
13926
13927 tree type = (lambda_fntype ? lambda_fntype
13928 : tsubst (TREE_TYPE (t), args,
13929 complain | tf_fndecl_type, in_decl));
13930 if (type == error_mark_node)
13931 return error_mark_node;
13932
13933 /* If we hit excessive deduction depth, the type is bogus even if
13934 it isn't error_mark_node, so don't build a decl. */
13935 if (excessive_deduction_depth)
13936 return error_mark_node;
13937
13938 /* We do NOT check for matching decls pushed separately at this
13939 point, as they may not represent instantiations of this
13940 template, and in any case are considered separate under the
13941 discrete model. */
13942 tree r = copy_decl (t);
13943 DECL_USE_TEMPLATE (r) = 0;
13944 TREE_TYPE (r) = type;
13945 /* Clear out the mangled name and RTL for the instantiation. */
13946 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13947 SET_DECL_RTL (r, NULL);
13948 /* Leave DECL_INITIAL set on deleted instantiations. */
13949 if (!DECL_DELETED_FN (r))
13950 DECL_INITIAL (r) = NULL_TREE;
13951 DECL_CONTEXT (r) = ctx;
13952 set_instantiating_module (r);
13953
13954 /* Handle explicit(dependent-expr). */
13955 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13956 {
13957 tree spec = lookup_explicit_specifier (t);
13958 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13959 /*function_p=*/false,
13960 /*i_c_e_p=*/true);
13961 spec = build_explicit_specifier (spec, complain);
13962 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13963 }
13964
13965 /* OpenMP UDRs have the only argument a reference to the declared
13966 type. We want to diagnose if the declared type is a reference,
13967 which is invalid, but as references to references are usually
13968 quietly merged, diagnose it here. */
13969 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13970 {
13971 tree argtype
13972 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13973 argtype = tsubst (argtype, args, complain, in_decl);
13974 if (TYPE_REF_P (argtype))
13975 error_at (DECL_SOURCE_LOCATION (t),
13976 "reference type %qT in "
13977 "%<#pragma omp declare reduction%>", argtype);
13978 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13979 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13980 argtype);
13981 }
13982
13983 if (member && DECL_CONV_FN_P (r))
13984 /* Type-conversion operator. Reconstruct the name, in
13985 case it's the name of one of the template's parameters. */
13986 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13987
13988 tree parms = DECL_ARGUMENTS (t);
13989 if (closure)
13990 parms = DECL_CHAIN (parms);
13991 parms = tsubst (parms, args, complain, t);
13992 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13993 DECL_CONTEXT (parm) = r;
13994 if (closure)
13995 {
13996 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13997 DECL_NAME (tparm) = closure_identifier;
13998 DECL_CHAIN (tparm) = parms;
13999 parms = tparm;
14000 }
14001 DECL_ARGUMENTS (r) = parms;
14002 DECL_RESULT (r) = NULL_TREE;
14003
14004 maybe_rebuild_function_decl_type (r);
14005
14006 TREE_STATIC (r) = 0;
14007 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14008 DECL_EXTERNAL (r) = 1;
14009 /* If this is an instantiation of a function with internal
14010 linkage, we already know what object file linkage will be
14011 assigned to the instantiation. */
14012 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14013 DECL_DEFER_OUTPUT (r) = 0;
14014 DECL_CHAIN (r) = NULL_TREE;
14015 DECL_PENDING_INLINE_INFO (r) = 0;
14016 DECL_PENDING_INLINE_P (r) = 0;
14017 DECL_SAVED_TREE (r) = NULL_TREE;
14018 DECL_STRUCT_FUNCTION (r) = NULL;
14019 TREE_USED (r) = 0;
14020 /* We'll re-clone as appropriate in instantiate_template. */
14021 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14022
14023 /* If we aren't complaining now, return on error before we register
14024 the specialization so that we'll complain eventually. */
14025 if ((complain & tf_error) == 0
14026 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14027 && !grok_op_properties (r, /*complain=*/false))
14028 return error_mark_node;
14029
14030 /* Associate the constraints directly with the instantiation. We
14031 don't substitute through the constraints; that's only done when
14032 they are checked. */
14033 if (tree ci = get_constraints (t))
14034 /* Unless we're regenerating a lambda, in which case we'll set the
14035 lambda's constraints in tsubst_lambda_expr. */
14036 if (!lambda_fntype)
14037 set_constraints (r, ci);
14038
14039 if (DECL_FRIEND_CONTEXT (t))
14040 SET_DECL_FRIEND_CONTEXT (r,
14041 tsubst (DECL_FRIEND_CONTEXT (t),
14042 args, complain, in_decl));
14043
14044 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14045 this in the special friend case mentioned above where
14046 GEN_TMPL is NULL. */
14047 if (gen_tmpl && !closure)
14048 {
14049 DECL_TEMPLATE_INFO (r)
14050 = build_template_info (gen_tmpl, argvec);
14051 SET_DECL_IMPLICIT_INSTANTIATION (r);
14052
14053 tree new_r
14054 = register_specialization (r, gen_tmpl, argvec, false, hash);
14055 if (new_r != r)
14056 /* We instantiated this while substituting into
14057 the type earlier (template/friend54.C). */
14058 return new_r;
14059
14060 /* We're not supposed to instantiate default arguments
14061 until they are called, for a template. But, for a
14062 declaration like:
14063
14064 template <class T> void f ()
14065 { extern void g(int i = T()); }
14066
14067 we should do the substitution when the template is
14068 instantiated. We handle the member function case in
14069 instantiate_class_template since the default arguments
14070 might refer to other members of the class. */
14071 if (!member
14072 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14073 && !uses_template_parms (argvec))
14074 tsubst_default_arguments (r, complain);
14075 }
14076 else if (DECL_LOCAL_DECL_P (r))
14077 {
14078 if (!cp_unevaluated_operand)
14079 register_local_specialization (r, t);
14080 }
14081 else
14082 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14083
14084 /* Copy the list of befriending classes. */
14085 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14086 *friends;
14087 friends = &TREE_CHAIN (*friends))
14088 {
14089 *friends = copy_node (*friends);
14090 TREE_VALUE (*friends)
14091 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14092 }
14093
14094 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14095 {
14096 maybe_retrofit_in_chrg (r);
14097 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14098 return error_mark_node;
14099 /* If this is an instantiation of a member template, clone it.
14100 If it isn't, that'll be handled by
14101 clone_constructors_and_destructors. */
14102 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14103 clone_cdtor (r, /*update_methods=*/false);
14104 }
14105 else if ((complain & tf_error) != 0
14106 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14107 && !grok_op_properties (r, /*complain=*/true))
14108 return error_mark_node;
14109
14110 /* Possibly limit visibility based on template args. */
14111 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14112 if (DECL_VISIBILITY_SPECIFIED (t))
14113 {
14114 DECL_VISIBILITY_SPECIFIED (r) = 0;
14115 DECL_ATTRIBUTES (r)
14116 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14117 }
14118 determine_visibility (r);
14119 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14120 && !processing_template_decl)
14121 defaulted_late_check (r);
14122
14123 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14124 args, complain, in_decl);
14125 if (flag_openmp)
14126 if (tree attr = lookup_attribute ("omp declare variant base",
14127 DECL_ATTRIBUTES (r)))
14128 omp_declare_variant_finalize (r, attr);
14129
14130 return r;
14131 }
14132
14133 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14134
14135 static tree
14136 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14137 tree lambda_fntype)
14138 {
14139 /* We can get here when processing a member function template,
14140 member class template, or template template parameter. */
14141 tree decl = DECL_TEMPLATE_RESULT (t);
14142 tree in_decl = t;
14143 tree spec;
14144 tree tmpl_args;
14145 tree full_args;
14146 tree r;
14147 hashval_t hash = 0;
14148
14149 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14150 {
14151 /* Template template parameter is treated here. */
14152 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14153 if (new_type == error_mark_node)
14154 r = error_mark_node;
14155 /* If we get a real template back, return it. This can happen in
14156 the context of most_specialized_partial_spec. */
14157 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14158 r = new_type;
14159 else
14160 /* The new TEMPLATE_DECL was built in
14161 reduce_template_parm_level. */
14162 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14163 return r;
14164 }
14165
14166 if (!lambda_fntype)
14167 {
14168 /* We might already have an instance of this template.
14169 The ARGS are for the surrounding class type, so the
14170 full args contain the tsubst'd args for the context,
14171 plus the innermost args from the template decl. */
14172 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14173 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14174 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14175 /* Because this is a template, the arguments will still be
14176 dependent, even after substitution. If
14177 PROCESSING_TEMPLATE_DECL is not set, the dependency
14178 predicates will short-circuit. */
14179 ++processing_template_decl;
14180 full_args = tsubst_template_args (tmpl_args, args,
14181 complain, in_decl);
14182 --processing_template_decl;
14183 if (full_args == error_mark_node)
14184 return error_mark_node;
14185
14186 /* If this is a default template template argument,
14187 tsubst might not have changed anything. */
14188 if (full_args == tmpl_args)
14189 return t;
14190
14191 hash = hash_tmpl_and_args (t, full_args);
14192 spec = retrieve_specialization (t, full_args, hash);
14193 if (spec != NULL_TREE)
14194 {
14195 if (TYPE_P (spec))
14196 /* Type partial instantiations are stored as the type by
14197 lookup_template_class_1, not here as the template. */
14198 spec = CLASSTYPE_TI_TEMPLATE (spec);
14199 return spec;
14200 }
14201 }
14202
14203 /* Make a new template decl. It will be similar to the
14204 original, but will record the current template arguments.
14205 We also create a new function declaration, which is just
14206 like the old one, but points to this new template, rather
14207 than the old one. */
14208 r = copy_decl (t);
14209 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14210 DECL_CHAIN (r) = NULL_TREE;
14211
14212 // Build new template info linking to the original template decl.
14213 if (!lambda_fntype)
14214 {
14215 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14216 SET_DECL_IMPLICIT_INSTANTIATION (r);
14217 }
14218 else
14219 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14220
14221 /* The template parameters for this new template are all the
14222 template parameters for the old template, except the
14223 outermost level of parameters. */
14224 DECL_TEMPLATE_PARMS (r)
14225 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14226 complain);
14227
14228 bool class_p = false;
14229 tree inner = decl;
14230 ++processing_template_decl;
14231 if (TREE_CODE (inner) == FUNCTION_DECL)
14232 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14233 else
14234 {
14235 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14236 {
14237 class_p = true;
14238 inner = TREE_TYPE (inner);
14239 }
14240 if (class_p)
14241 inner = tsubst_aggr_type (inner, args, complain,
14242 in_decl, /*entering*/1);
14243 else
14244 inner = tsubst (inner, args, complain, in_decl);
14245 }
14246 --processing_template_decl;
14247 if (inner == error_mark_node)
14248 return error_mark_node;
14249
14250 if (class_p)
14251 {
14252 /* For a partial specialization, we need to keep pointing to
14253 the primary template. */
14254 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14255 CLASSTYPE_TI_TEMPLATE (inner) = r;
14256
14257 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14258 inner = TYPE_MAIN_DECL (inner);
14259 }
14260 else if (lambda_fntype)
14261 {
14262 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14263 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14264 }
14265 else
14266 {
14267 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14268 DECL_TI_TEMPLATE (inner) = r;
14269 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14270 }
14271
14272 DECL_TEMPLATE_RESULT (r) = inner;
14273 TREE_TYPE (r) = TREE_TYPE (inner);
14274 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14275
14276 if (modules_p ())
14277 {
14278 /* Propagate module information from the decl. */
14279 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14280 if (DECL_LANG_SPECIFIC (inner))
14281 {
14282 DECL_MODULE_PURVIEW_P (r) = DECL_MODULE_PURVIEW_P (inner);
14283 /* If this is a constrained template, the above tsubst of
14284 inner can find the unconstrained template, which may have
14285 come from an import. This is ok, because we don't
14286 register this instantiation (see below). */
14287 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14288 || (TEMPLATE_PARMS_CONSTRAINTS
14289 (DECL_TEMPLATE_PARMS (t))));
14290 DECL_MODULE_IMPORT_P (r) = false;
14291 }
14292 }
14293
14294 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14295 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14296
14297 if (PRIMARY_TEMPLATE_P (t))
14298 DECL_PRIMARY_TEMPLATE (r) = r;
14299
14300 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14301 /* Record this non-type partial instantiation. */
14302 register_specialization (r, t,
14303 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14304 false, hash);
14305
14306 return r;
14307 }
14308
14309 /* True if FN is the op() for a lambda in an uninstantiated template. */
14310
14311 bool
14312 lambda_fn_in_template_p (tree fn)
14313 {
14314 if (!fn || !LAMBDA_FUNCTION_P (fn))
14315 return false;
14316 tree closure = DECL_CONTEXT (fn);
14317 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14318 }
14319
14320 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14321 which the above is true. */
14322
14323 bool
14324 instantiated_lambda_fn_p (tree fn)
14325 {
14326 if (!fn || !LAMBDA_FUNCTION_P (fn))
14327 return false;
14328 tree closure = DECL_CONTEXT (fn);
14329 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14330 return LAMBDA_EXPR_INSTANTIATED (lam);
14331 }
14332
14333 /* We're instantiating a variable from template function TCTX. Return the
14334 corresponding current enclosing scope. This gets complicated because lambda
14335 functions in templates are regenerated rather than instantiated, but generic
14336 lambda functions are subsequently instantiated. */
14337
14338 static tree
14339 enclosing_instantiation_of (tree otctx)
14340 {
14341 tree tctx = otctx;
14342 tree fn = current_function_decl;
14343 int lambda_count = 0;
14344
14345 for (; tctx && (lambda_fn_in_template_p (tctx)
14346 || instantiated_lambda_fn_p (tctx));
14347 tctx = decl_function_context (tctx))
14348 ++lambda_count;
14349 for (; fn; fn = decl_function_context (fn))
14350 {
14351 tree ofn = fn;
14352 int flambda_count = 0;
14353 for (; fn && instantiated_lambda_fn_p (fn);
14354 fn = decl_function_context (fn))
14355 ++flambda_count;
14356 if ((fn && DECL_TEMPLATE_INFO (fn))
14357 ? most_general_template (fn) != most_general_template (tctx)
14358 : fn != tctx)
14359 continue;
14360 if (flambda_count != lambda_count)
14361 {
14362 gcc_assert (flambda_count > lambda_count);
14363 for (; flambda_count > lambda_count; --flambda_count)
14364 ofn = decl_function_context (ofn);
14365 }
14366 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14367 || DECL_CONV_FN_P (ofn));
14368 return ofn;
14369 }
14370 gcc_unreachable ();
14371 }
14372
14373 /* Substitute the ARGS into the T, which is a _DECL. Return the
14374 result of the substitution. Issue error and warning messages under
14375 control of COMPLAIN. */
14376
14377 static tree
14378 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14379 {
14380 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14381 location_t saved_loc;
14382 tree r = NULL_TREE;
14383 tree in_decl = t;
14384 hashval_t hash = 0;
14385
14386 /* Set the filename and linenumber to improve error-reporting. */
14387 saved_loc = input_location;
14388 input_location = DECL_SOURCE_LOCATION (t);
14389
14390 switch (TREE_CODE (t))
14391 {
14392 case TEMPLATE_DECL:
14393 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14394 break;
14395
14396 case FUNCTION_DECL:
14397 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14398 break;
14399
14400 case PARM_DECL:
14401 {
14402 tree type = NULL_TREE;
14403 int i, len = 1;
14404 tree expanded_types = NULL_TREE;
14405 tree prev_r = NULL_TREE;
14406 tree first_r = NULL_TREE;
14407
14408 if (DECL_PACK_P (t))
14409 {
14410 /* If there is a local specialization that isn't a
14411 parameter pack, it means that we're doing a "simple"
14412 substitution from inside tsubst_pack_expansion. Just
14413 return the local specialization (which will be a single
14414 parm). */
14415 tree spec = retrieve_local_specialization (t);
14416 if (spec
14417 && TREE_CODE (spec) == PARM_DECL
14418 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14419 RETURN (spec);
14420
14421 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14422 the parameters in this function parameter pack. */
14423 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14424 complain, in_decl);
14425 if (TREE_CODE (expanded_types) == TREE_VEC)
14426 {
14427 len = TREE_VEC_LENGTH (expanded_types);
14428
14429 /* Zero-length parameter packs are boring. Just substitute
14430 into the chain. */
14431 if (len == 0 && !cp_unevaluated_operand)
14432 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14433 TREE_CHAIN (t)));
14434 }
14435 else
14436 {
14437 /* All we did was update the type. Make a note of that. */
14438 type = expanded_types;
14439 expanded_types = NULL_TREE;
14440 }
14441 }
14442
14443 /* Loop through all of the parameters we'll build. When T is
14444 a function parameter pack, LEN is the number of expanded
14445 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14446 r = NULL_TREE;
14447 for (i = 0; i < len; ++i)
14448 {
14449 prev_r = r;
14450 r = copy_node (t);
14451 if (DECL_TEMPLATE_PARM_P (t))
14452 SET_DECL_TEMPLATE_PARM_P (r);
14453
14454 if (expanded_types)
14455 /* We're on the Ith parameter of the function parameter
14456 pack. */
14457 {
14458 /* Get the Ith type. */
14459 type = TREE_VEC_ELT (expanded_types, i);
14460
14461 /* Rename the parameter to include the index. */
14462 DECL_NAME (r)
14463 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14464 }
14465 else if (!type)
14466 /* We're dealing with a normal parameter. */
14467 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14468
14469 type = type_decays_to (type);
14470 TREE_TYPE (r) = type;
14471 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14472
14473 if (DECL_INITIAL (r))
14474 {
14475 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14476 DECL_INITIAL (r) = TREE_TYPE (r);
14477 else
14478 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14479 complain, in_decl);
14480 }
14481
14482 DECL_CONTEXT (r) = NULL_TREE;
14483
14484 if (!DECL_TEMPLATE_PARM_P (r))
14485 DECL_ARG_TYPE (r) = type_passed_as (type);
14486
14487 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14488 args, complain, in_decl);
14489
14490 /* Keep track of the first new parameter we
14491 generate. That's what will be returned to the
14492 caller. */
14493 if (!first_r)
14494 first_r = r;
14495
14496 /* Build a proper chain of parameters when substituting
14497 into a function parameter pack. */
14498 if (prev_r)
14499 DECL_CHAIN (prev_r) = r;
14500 }
14501
14502 /* If cp_unevaluated_operand is set, we're just looking for a
14503 single dummy parameter, so don't keep going. */
14504 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14505 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14506 complain, DECL_CHAIN (t));
14507
14508 /* FIRST_R contains the start of the chain we've built. */
14509 r = first_r;
14510 }
14511 break;
14512
14513 case FIELD_DECL:
14514 {
14515 tree type = NULL_TREE;
14516 tree vec = NULL_TREE;
14517 tree expanded_types = NULL_TREE;
14518 int len = 1;
14519
14520 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14521 {
14522 /* This field is a lambda capture pack. Return a TREE_VEC of
14523 the expanded fields to instantiate_class_template_1. */
14524 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14525 complain, in_decl);
14526 if (TREE_CODE (expanded_types) == TREE_VEC)
14527 {
14528 len = TREE_VEC_LENGTH (expanded_types);
14529 vec = make_tree_vec (len);
14530 }
14531 else
14532 {
14533 /* All we did was update the type. Make a note of that. */
14534 type = expanded_types;
14535 expanded_types = NULL_TREE;
14536 }
14537 }
14538
14539 for (int i = 0; i < len; ++i)
14540 {
14541 r = copy_decl (t);
14542 if (expanded_types)
14543 {
14544 type = TREE_VEC_ELT (expanded_types, i);
14545 DECL_NAME (r)
14546 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14547 }
14548 else if (!type)
14549 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14550
14551 if (type == error_mark_node)
14552 RETURN (error_mark_node);
14553 TREE_TYPE (r) = type;
14554 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14555
14556 if (DECL_C_BIT_FIELD (r))
14557 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14558 number of bits. */
14559 DECL_BIT_FIELD_REPRESENTATIVE (r)
14560 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14561 complain, in_decl,
14562 /*integral_constant_expression_p=*/true);
14563 if (DECL_INITIAL (t))
14564 {
14565 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14566 NSDMI in perform_member_init. Still set DECL_INITIAL
14567 so that we know there is one. */
14568 DECL_INITIAL (r) = void_node;
14569 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14570 retrofit_lang_decl (r);
14571 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14572 }
14573 /* We don't have to set DECL_CONTEXT here; it is set by
14574 finish_member_declaration. */
14575 DECL_CHAIN (r) = NULL_TREE;
14576
14577 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14578 args, complain, in_decl);
14579
14580 if (vec)
14581 TREE_VEC_ELT (vec, i) = r;
14582 }
14583
14584 if (vec)
14585 r = vec;
14586 }
14587 break;
14588
14589 case USING_DECL:
14590 /* We reach here only for member using decls. We also need to check
14591 uses_template_parms because DECL_DEPENDENT_P is not set for a
14592 using-declaration that designates a member of the current
14593 instantiation (c++/53549). */
14594 if (DECL_DEPENDENT_P (t)
14595 || uses_template_parms (USING_DECL_SCOPE (t)))
14596 {
14597 tree scope = USING_DECL_SCOPE (t);
14598 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14599 if (PACK_EXPANSION_P (scope))
14600 {
14601 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14602 int len = TREE_VEC_LENGTH (vec);
14603 r = make_tree_vec (len);
14604 for (int i = 0; i < len; ++i)
14605 {
14606 tree escope = TREE_VEC_ELT (vec, i);
14607 tree elt = do_class_using_decl (escope, name);
14608 if (!elt)
14609 {
14610 r = error_mark_node;
14611 break;
14612 }
14613 else
14614 {
14615 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14616 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14617 }
14618 TREE_VEC_ELT (r, i) = elt;
14619 }
14620 }
14621 else
14622 {
14623 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14624 complain, in_decl);
14625 r = do_class_using_decl (inst_scope, name);
14626 if (!r)
14627 r = error_mark_node;
14628 else
14629 {
14630 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14631 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14632 }
14633 }
14634 }
14635 else
14636 {
14637 r = copy_node (t);
14638 DECL_CHAIN (r) = NULL_TREE;
14639 }
14640 break;
14641
14642 case TYPE_DECL:
14643 case VAR_DECL:
14644 {
14645 tree argvec = NULL_TREE;
14646 tree gen_tmpl = NULL_TREE;
14647 tree tmpl = NULL_TREE;
14648 tree type = NULL_TREE;
14649
14650 if (TREE_TYPE (t) == error_mark_node)
14651 RETURN (error_mark_node);
14652
14653 if (TREE_CODE (t) == TYPE_DECL
14654 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14655 {
14656 /* If this is the canonical decl, we don't have to
14657 mess with instantiations, and often we can't (for
14658 typename, template type parms and such). Note that
14659 TYPE_NAME is not correct for the above test if
14660 we've copied the type for a typedef. */
14661 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14662 if (type == error_mark_node)
14663 RETURN (error_mark_node);
14664 r = TYPE_NAME (type);
14665 break;
14666 }
14667
14668 /* Check to see if we already have the specialization we
14669 need. */
14670 tree spec = NULL_TREE;
14671 bool local_p = false;
14672 tree ctx = DECL_CONTEXT (t);
14673 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14674 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14675 {
14676 local_p = false;
14677 if (DECL_CLASS_SCOPE_P (t))
14678 {
14679 ctx = tsubst_aggr_type (ctx, args,
14680 complain,
14681 in_decl, /*entering_scope=*/1);
14682 /* If CTX is unchanged, then T is in fact the
14683 specialization we want. That situation occurs when
14684 referencing a static data member within in its own
14685 class. We can use pointer equality, rather than
14686 same_type_p, because DECL_CONTEXT is always
14687 canonical... */
14688 if (ctx == DECL_CONTEXT (t)
14689 /* ... unless T is a member template; in which
14690 case our caller can be willing to create a
14691 specialization of that template represented
14692 by T. */
14693 && !(DECL_TI_TEMPLATE (t)
14694 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14695 spec = t;
14696 }
14697
14698 if (!spec)
14699 {
14700 tmpl = DECL_TI_TEMPLATE (t);
14701 gen_tmpl = most_general_template (tmpl);
14702 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14703 if (argvec != error_mark_node)
14704 argvec = (coerce_innermost_template_parms
14705 (DECL_TEMPLATE_PARMS (gen_tmpl),
14706 argvec, t, complain,
14707 /*all*/true, /*defarg*/true));
14708 if (argvec == error_mark_node)
14709 RETURN (error_mark_node);
14710 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14711 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14712 }
14713 }
14714 else
14715 {
14716 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14717 /* Subsequent calls to pushdecl will fill this in. */
14718 ctx = NULL_TREE;
14719 /* A local variable. */
14720 local_p = true;
14721 /* Unless this is a reference to a static variable from an
14722 enclosing function, in which case we need to fill it in now. */
14723 if (TREE_STATIC (t))
14724 {
14725 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14726 if (fn != current_function_decl)
14727 ctx = fn;
14728 }
14729 spec = retrieve_local_specialization (t);
14730 }
14731 /* If we already have the specialization we need, there is
14732 nothing more to do. */
14733 if (spec)
14734 {
14735 r = spec;
14736 break;
14737 }
14738
14739 /* Create a new node for the specialization we need. */
14740 if (type == NULL_TREE)
14741 {
14742 if (is_typedef_decl (t))
14743 type = DECL_ORIGINAL_TYPE (t);
14744 else
14745 type = TREE_TYPE (t);
14746 if (VAR_P (t)
14747 && VAR_HAD_UNKNOWN_BOUND (t)
14748 && type != error_mark_node)
14749 type = strip_array_domain (type);
14750 tree sub_args = args;
14751 if (tree auto_node = type_uses_auto (type))
14752 {
14753 /* Mask off any template args past the variable's context so we
14754 don't replace the auto with an unrelated argument. */
14755 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14756 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14757 if (extra > 0)
14758 /* This should never happen with the new lambda instantiation
14759 model, but keep the handling just in case. */
14760 gcc_assert (!CHECKING_P),
14761 sub_args = strip_innermost_template_args (args, extra);
14762 }
14763 type = tsubst (type, sub_args, complain, in_decl);
14764 /* Substituting the type might have recursively instantiated this
14765 same alias (c++/86171). */
14766 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14767 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14768 {
14769 r = spec;
14770 break;
14771 }
14772 }
14773 r = copy_decl (t);
14774 if (VAR_P (r))
14775 {
14776 DECL_INITIALIZED_P (r) = 0;
14777 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14778 if (type == error_mark_node)
14779 RETURN (error_mark_node);
14780 if (TREE_CODE (type) == FUNCTION_TYPE)
14781 {
14782 /* It may seem that this case cannot occur, since:
14783
14784 typedef void f();
14785 void g() { f x; }
14786
14787 declares a function, not a variable. However:
14788
14789 typedef void f();
14790 template <typename T> void g() { T t; }
14791 template void g<f>();
14792
14793 is an attempt to declare a variable with function
14794 type. */
14795 error ("variable %qD has function type",
14796 /* R is not yet sufficiently initialized, so we
14797 just use its name. */
14798 DECL_NAME (r));
14799 RETURN (error_mark_node);
14800 }
14801 type = complete_type (type);
14802 /* Wait until cp_finish_decl to set this again, to handle
14803 circular dependency (template/instantiate6.C). */
14804 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14805 type = check_var_type (DECL_NAME (r), type,
14806 DECL_SOURCE_LOCATION (r));
14807 if (DECL_HAS_VALUE_EXPR_P (t))
14808 {
14809 tree ve = DECL_VALUE_EXPR (t);
14810 /* If the DECL_VALUE_EXPR is converted to the declared type,
14811 preserve the identity so that gimplify_type_sizes works. */
14812 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14813 if (nop)
14814 ve = TREE_OPERAND (ve, 0);
14815 ve = tsubst_expr (ve, args, complain, in_decl,
14816 /*constant_expression_p=*/false);
14817 if (REFERENCE_REF_P (ve))
14818 {
14819 gcc_assert (TYPE_REF_P (type));
14820 ve = TREE_OPERAND (ve, 0);
14821 }
14822 if (nop)
14823 ve = build_nop (type, ve);
14824 else if (DECL_LANG_SPECIFIC (t)
14825 && DECL_OMP_PRIVATIZED_MEMBER (t)
14826 && TREE_CODE (ve) == COMPONENT_REF
14827 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14828 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14829 type = TREE_TYPE (ve);
14830 else
14831 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14832 == TYPE_MAIN_VARIANT (type));
14833 SET_DECL_VALUE_EXPR (r, ve);
14834 }
14835 if (CP_DECL_THREAD_LOCAL_P (r)
14836 && !processing_template_decl)
14837 set_decl_tls_model (r, decl_default_tls_model (r));
14838 }
14839 else if (DECL_SELF_REFERENCE_P (t))
14840 SET_DECL_SELF_REFERENCE_P (r);
14841 TREE_TYPE (r) = type;
14842 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14843 DECL_CONTEXT (r) = ctx;
14844 /* Clear out the mangled name and RTL for the instantiation. */
14845 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14846 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14847 SET_DECL_RTL (r, NULL);
14848 set_instantiating_module (r);
14849
14850 /* The initializer must not be expanded until it is required;
14851 see [temp.inst]. */
14852 DECL_INITIAL (r) = NULL_TREE;
14853 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14854 if (VAR_P (r))
14855 {
14856 if (DECL_LANG_SPECIFIC (r))
14857 SET_DECL_DEPENDENT_INIT_P (r, false);
14858
14859 SET_DECL_MODE (r, VOIDmode);
14860
14861 /* Possibly limit visibility based on template args. */
14862 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14863 if (DECL_VISIBILITY_SPECIFIED (t))
14864 {
14865 DECL_VISIBILITY_SPECIFIED (r) = 0;
14866 DECL_ATTRIBUTES (r)
14867 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14868 }
14869 determine_visibility (r);
14870 }
14871
14872 if (!local_p)
14873 {
14874 /* A static data member declaration is always marked
14875 external when it is declared in-class, even if an
14876 initializer is present. We mimic the non-template
14877 processing here. */
14878 DECL_EXTERNAL (r) = 1;
14879 if (DECL_NAMESPACE_SCOPE_P (t))
14880 DECL_NOT_REALLY_EXTERN (r) = 1;
14881
14882 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14883 SET_DECL_IMPLICIT_INSTANTIATION (r);
14884 if (!error_operand_p (r) || (complain & tf_error))
14885 register_specialization (r, gen_tmpl, argvec, false, hash);
14886 }
14887 else
14888 {
14889 if (DECL_LANG_SPECIFIC (r))
14890 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14891 if (!cp_unevaluated_operand)
14892 register_local_specialization (r, t);
14893 }
14894
14895 DECL_CHAIN (r) = NULL_TREE;
14896
14897 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14898 /*flags=*/0,
14899 args, complain, in_decl);
14900
14901 /* Preserve a typedef that names a type. */
14902 if (is_typedef_decl (r) && type != error_mark_node)
14903 {
14904 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14905 set_underlying_type (r);
14906 if (TYPE_DECL_ALIAS_P (r))
14907 /* An alias template specialization can be dependent
14908 even if its underlying type is not. */
14909 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14910 }
14911
14912 layout_decl (r, 0);
14913 }
14914 break;
14915
14916 default:
14917 gcc_unreachable ();
14918 }
14919 #undef RETURN
14920
14921 out:
14922 /* Restore the file and line information. */
14923 input_location = saved_loc;
14924
14925 return r;
14926 }
14927
14928 /* Substitute into the complete parameter type list PARMS. */
14929
14930 tree
14931 tsubst_function_parms (tree parms,
14932 tree args,
14933 tsubst_flags_t complain,
14934 tree in_decl)
14935 {
14936 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14937 }
14938
14939 /* Substitute into the ARG_TYPES of a function type.
14940 If END is a TREE_CHAIN, leave it and any following types
14941 un-substituted. */
14942
14943 static tree
14944 tsubst_arg_types (tree arg_types,
14945 tree args,
14946 tree end,
14947 tsubst_flags_t complain,
14948 tree in_decl)
14949 {
14950 tree remaining_arg_types;
14951 tree type = NULL_TREE;
14952 int i = 1;
14953 tree expanded_args = NULL_TREE;
14954 tree default_arg;
14955
14956 if (!arg_types || arg_types == void_list_node || arg_types == end)
14957 return arg_types;
14958
14959 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14960 args, end, complain, in_decl);
14961 if (remaining_arg_types == error_mark_node)
14962 return error_mark_node;
14963
14964 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14965 {
14966 /* For a pack expansion, perform substitution on the
14967 entire expression. Later on, we'll handle the arguments
14968 one-by-one. */
14969 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14970 args, complain, in_decl);
14971
14972 if (TREE_CODE (expanded_args) == TREE_VEC)
14973 /* So that we'll spin through the parameters, one by one. */
14974 i = TREE_VEC_LENGTH (expanded_args);
14975 else
14976 {
14977 /* We only partially substituted into the parameter
14978 pack. Our type is TYPE_PACK_EXPANSION. */
14979 type = expanded_args;
14980 expanded_args = NULL_TREE;
14981 }
14982 }
14983
14984 while (i > 0) {
14985 --i;
14986
14987 if (expanded_args)
14988 type = TREE_VEC_ELT (expanded_args, i);
14989 else if (!type)
14990 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14991
14992 if (type == error_mark_node)
14993 return error_mark_node;
14994 if (VOID_TYPE_P (type))
14995 {
14996 if (complain & tf_error)
14997 {
14998 error ("invalid parameter type %qT", type);
14999 if (in_decl)
15000 error ("in declaration %q+D", in_decl);
15001 }
15002 return error_mark_node;
15003 }
15004
15005 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15006 top-level qualifiers as required. */
15007 type = cv_unqualified (type_decays_to (type));
15008
15009 /* We do not substitute into default arguments here. The standard
15010 mandates that they be instantiated only when needed, which is
15011 done in build_over_call. */
15012 default_arg = TREE_PURPOSE (arg_types);
15013
15014 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15015 since the new op() won't have any associated template arguments for us
15016 to refer to later. */
15017 if (lambda_fn_in_template_p (in_decl))
15018 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
15019 false/*fn*/, false/*constexpr*/);
15020
15021 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15022 {
15023 /* We've instantiated a template before its default arguments
15024 have been parsed. This can happen for a nested template
15025 class, and is not an error unless we require the default
15026 argument in a call of this function. */
15027 remaining_arg_types =
15028 tree_cons (default_arg, type, remaining_arg_types);
15029 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15030 remaining_arg_types);
15031 }
15032 else
15033 remaining_arg_types =
15034 hash_tree_cons (default_arg, type, remaining_arg_types);
15035 }
15036
15037 return remaining_arg_types;
15038 }
15039
15040 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15041 *not* handle the exception-specification for FNTYPE, because the
15042 initial substitution of explicitly provided template parameters
15043 during argument deduction forbids substitution into the
15044 exception-specification:
15045
15046 [temp.deduct]
15047
15048 All references in the function type of the function template to the
15049 corresponding template parameters are replaced by the specified tem-
15050 plate argument values. If a substitution in a template parameter or
15051 in the function type of the function template results in an invalid
15052 type, type deduction fails. [Note: The equivalent substitution in
15053 exception specifications is done only when the function is instanti-
15054 ated, at which point a program is ill-formed if the substitution
15055 results in an invalid type.] */
15056
15057 static tree
15058 tsubst_function_type (tree t,
15059 tree args,
15060 tsubst_flags_t complain,
15061 tree in_decl)
15062 {
15063 tree return_type;
15064 tree arg_types = NULL_TREE;
15065
15066 /* The TYPE_CONTEXT is not used for function/method types. */
15067 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15068
15069 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15070 failure. */
15071 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15072
15073 if (late_return_type_p)
15074 {
15075 /* Substitute the argument types. */
15076 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15077 complain, in_decl);
15078 if (arg_types == error_mark_node)
15079 return error_mark_node;
15080
15081 tree save_ccp = current_class_ptr;
15082 tree save_ccr = current_class_ref;
15083 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15084 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15085 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15086 if (do_inject)
15087 {
15088 /* DR 1207: 'this' is in scope in the trailing return type. */
15089 inject_this_parameter (this_type, cp_type_quals (this_type));
15090 }
15091
15092 /* Substitute the return type. */
15093 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15094
15095 if (do_inject)
15096 {
15097 current_class_ptr = save_ccp;
15098 current_class_ref = save_ccr;
15099 }
15100 }
15101 else
15102 /* Substitute the return type. */
15103 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15104
15105 if (return_type == error_mark_node)
15106 return error_mark_node;
15107 /* DR 486 clarifies that creation of a function type with an
15108 invalid return type is a deduction failure. */
15109 if (TREE_CODE (return_type) == ARRAY_TYPE
15110 || TREE_CODE (return_type) == FUNCTION_TYPE)
15111 {
15112 if (complain & tf_error)
15113 {
15114 if (TREE_CODE (return_type) == ARRAY_TYPE)
15115 error ("function returning an array");
15116 else
15117 error ("function returning a function");
15118 }
15119 return error_mark_node;
15120 }
15121
15122 if (!late_return_type_p)
15123 {
15124 /* Substitute the argument types. */
15125 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15126 complain, in_decl);
15127 if (arg_types == error_mark_node)
15128 return error_mark_node;
15129 }
15130
15131 /* Construct a new type node and return it. */
15132 return rebuild_function_or_method_type (t, return_type, arg_types,
15133 /*raises=*/NULL_TREE, complain);
15134 }
15135
15136 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15137 ARGS into that specification, and return the substituted
15138 specification. If there is no specification, return NULL_TREE. */
15139
15140 static tree
15141 tsubst_exception_specification (tree fntype,
15142 tree args,
15143 tsubst_flags_t complain,
15144 tree in_decl,
15145 bool defer_ok)
15146 {
15147 tree specs;
15148 tree new_specs;
15149
15150 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15151 new_specs = NULL_TREE;
15152 if (specs && TREE_PURPOSE (specs))
15153 {
15154 /* A noexcept-specifier. */
15155 tree expr = TREE_PURPOSE (specs);
15156 if (TREE_CODE (expr) == INTEGER_CST)
15157 new_specs = expr;
15158 else if (defer_ok)
15159 {
15160 /* Defer instantiation of noexcept-specifiers to avoid
15161 excessive instantiations (c++/49107). */
15162 new_specs = make_node (DEFERRED_NOEXCEPT);
15163 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15164 {
15165 /* We already partially instantiated this member template,
15166 so combine the new args with the old. */
15167 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15168 = DEFERRED_NOEXCEPT_PATTERN (expr);
15169 DEFERRED_NOEXCEPT_ARGS (new_specs)
15170 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15171 }
15172 else
15173 {
15174 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15175 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15176 }
15177 }
15178 else
15179 {
15180 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15181 {
15182 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15183 args);
15184 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15185 }
15186 new_specs = tsubst_copy_and_build
15187 (expr, args, complain, in_decl, /*function_p=*/false,
15188 /*integral_constant_expression_p=*/true);
15189 }
15190 new_specs = build_noexcept_spec (new_specs, complain);
15191 }
15192 else if (specs)
15193 {
15194 if (! TREE_VALUE (specs))
15195 new_specs = specs;
15196 else
15197 while (specs)
15198 {
15199 tree spec;
15200 int i, len = 1;
15201 tree expanded_specs = NULL_TREE;
15202
15203 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15204 {
15205 /* Expand the pack expansion type. */
15206 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15207 args, complain,
15208 in_decl);
15209
15210 if (expanded_specs == error_mark_node)
15211 return error_mark_node;
15212 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15213 len = TREE_VEC_LENGTH (expanded_specs);
15214 else
15215 {
15216 /* We're substituting into a member template, so
15217 we got a TYPE_PACK_EXPANSION back. Add that
15218 expansion and move on. */
15219 gcc_assert (TREE_CODE (expanded_specs)
15220 == TYPE_PACK_EXPANSION);
15221 new_specs = add_exception_specifier (new_specs,
15222 expanded_specs,
15223 complain);
15224 specs = TREE_CHAIN (specs);
15225 continue;
15226 }
15227 }
15228
15229 for (i = 0; i < len; ++i)
15230 {
15231 if (expanded_specs)
15232 spec = TREE_VEC_ELT (expanded_specs, i);
15233 else
15234 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15235 if (spec == error_mark_node)
15236 return spec;
15237 new_specs = add_exception_specifier (new_specs, spec,
15238 complain);
15239 }
15240
15241 specs = TREE_CHAIN (specs);
15242 }
15243 }
15244 return new_specs;
15245 }
15246
15247 /* Substitute through a TREE_LIST of types or expressions, handling pack
15248 expansions. */
15249
15250 tree
15251 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15252 {
15253 if (t == void_list_node)
15254 return t;
15255
15256 tree purpose = TREE_PURPOSE (t);
15257 tree purposevec = NULL_TREE;
15258 if (!purpose)
15259 ;
15260 else if (PACK_EXPANSION_P (purpose))
15261 {
15262 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15263 if (TREE_CODE (purpose) == TREE_VEC)
15264 purposevec = purpose;
15265 }
15266 else if (TYPE_P (purpose))
15267 purpose = tsubst (purpose, args, complain, in_decl);
15268 else
15269 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15270 if (purpose == error_mark_node || purposevec == error_mark_node)
15271 return error_mark_node;
15272
15273 tree value = TREE_VALUE (t);
15274 tree valuevec = NULL_TREE;
15275 if (!value)
15276 ;
15277 else if (PACK_EXPANSION_P (value))
15278 {
15279 value = tsubst_pack_expansion (value, args, complain, in_decl);
15280 if (TREE_CODE (value) == TREE_VEC)
15281 valuevec = value;
15282 }
15283 else if (TYPE_P (value))
15284 value = tsubst (value, args, complain, in_decl);
15285 else
15286 value = tsubst_copy_and_build (value, args, complain, in_decl);
15287 if (value == error_mark_node || valuevec == error_mark_node)
15288 return error_mark_node;
15289
15290 tree chain = TREE_CHAIN (t);
15291 if (!chain)
15292 ;
15293 else if (TREE_CODE (chain) == TREE_LIST)
15294 chain = tsubst_tree_list (chain, args, complain, in_decl);
15295 else if (TYPE_P (chain))
15296 chain = tsubst (chain, args, complain, in_decl);
15297 else
15298 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15299 if (chain == error_mark_node)
15300 return error_mark_node;
15301
15302 if (purpose == TREE_PURPOSE (t)
15303 && value == TREE_VALUE (t)
15304 && chain == TREE_CHAIN (t))
15305 return t;
15306
15307 int len;
15308 /* Determine the number of arguments. */
15309 if (purposevec)
15310 {
15311 len = TREE_VEC_LENGTH (purposevec);
15312 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15313 }
15314 else if (valuevec)
15315 len = TREE_VEC_LENGTH (valuevec);
15316 else
15317 len = 1;
15318
15319 for (int i = len; i-- > 0; )
15320 {
15321 if (purposevec)
15322 purpose = TREE_VEC_ELT (purposevec, i);
15323 if (valuevec)
15324 value = TREE_VEC_ELT (valuevec, i);
15325
15326 if (value && TYPE_P (value))
15327 chain = hash_tree_cons (purpose, value, chain);
15328 else
15329 chain = tree_cons (purpose, value, chain);
15330 }
15331
15332 return chain;
15333 }
15334
15335 /* Take the tree structure T and replace template parameters used
15336 therein with the argument vector ARGS. IN_DECL is an associated
15337 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15338 Issue error and warning messages under control of COMPLAIN. Note
15339 that we must be relatively non-tolerant of extensions here, in
15340 order to preserve conformance; if we allow substitutions that
15341 should not be allowed, we may allow argument deductions that should
15342 not succeed, and therefore report ambiguous overload situations
15343 where there are none. In theory, we could allow the substitution,
15344 but indicate that it should have failed, and allow our caller to
15345 make sure that the right thing happens, but we don't try to do this
15346 yet.
15347
15348 This function is used for dealing with types, decls and the like;
15349 for expressions, use tsubst_expr or tsubst_copy. */
15350
15351 tree
15352 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15353 {
15354 enum tree_code code;
15355 tree type, r = NULL_TREE;
15356
15357 if (t == NULL_TREE || t == error_mark_node
15358 || t == integer_type_node
15359 || t == void_type_node
15360 || t == char_type_node
15361 || t == unknown_type_node
15362 || TREE_CODE (t) == NAMESPACE_DECL
15363 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15364 return t;
15365
15366 if (DECL_P (t))
15367 return tsubst_decl (t, args, complain);
15368
15369 if (args == NULL_TREE)
15370 return t;
15371
15372 code = TREE_CODE (t);
15373
15374 if (code == IDENTIFIER_NODE)
15375 type = IDENTIFIER_TYPE_VALUE (t);
15376 else
15377 type = TREE_TYPE (t);
15378
15379 gcc_assert (type != unknown_type_node);
15380
15381 /* Reuse typedefs. We need to do this to handle dependent attributes,
15382 such as attribute aligned. */
15383 if (TYPE_P (t)
15384 && typedef_variant_p (t))
15385 {
15386 tree decl = TYPE_NAME (t);
15387
15388 if (alias_template_specialization_p (t, nt_opaque))
15389 {
15390 /* DECL represents an alias template and we want to
15391 instantiate it. */
15392 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15393 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15394 r = instantiate_alias_template (tmpl, gen_args, complain);
15395 }
15396 else if (DECL_CLASS_SCOPE_P (decl)
15397 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15398 && uses_template_parms (DECL_CONTEXT (decl)))
15399 {
15400 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15401 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15402 r = retrieve_specialization (tmpl, gen_args, 0);
15403 }
15404 else if (DECL_FUNCTION_SCOPE_P (decl)
15405 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15406 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15407 r = retrieve_local_specialization (decl);
15408 else
15409 /* The typedef is from a non-template context. */
15410 return t;
15411
15412 if (r)
15413 {
15414 r = TREE_TYPE (r);
15415 r = cp_build_qualified_type_real
15416 (r, cp_type_quals (t) | cp_type_quals (r),
15417 complain | tf_ignore_bad_quals);
15418 return r;
15419 }
15420 else
15421 {
15422 /* We don't have an instantiation yet, so drop the typedef. */
15423 int quals = cp_type_quals (t);
15424 t = DECL_ORIGINAL_TYPE (decl);
15425 t = cp_build_qualified_type_real (t, quals,
15426 complain | tf_ignore_bad_quals);
15427 }
15428 }
15429
15430 bool fndecl_type = (complain & tf_fndecl_type);
15431 complain &= ~tf_fndecl_type;
15432
15433 if (type
15434 && code != TYPENAME_TYPE
15435 && code != TEMPLATE_TYPE_PARM
15436 && code != TEMPLATE_PARM_INDEX
15437 && code != IDENTIFIER_NODE
15438 && code != FUNCTION_TYPE
15439 && code != METHOD_TYPE)
15440 type = tsubst (type, args, complain, in_decl);
15441 if (type == error_mark_node)
15442 return error_mark_node;
15443
15444 switch (code)
15445 {
15446 case RECORD_TYPE:
15447 case UNION_TYPE:
15448 case ENUMERAL_TYPE:
15449 return tsubst_aggr_type (t, args, complain, in_decl,
15450 /*entering_scope=*/0);
15451
15452 case ERROR_MARK:
15453 case IDENTIFIER_NODE:
15454 case VOID_TYPE:
15455 case OPAQUE_TYPE:
15456 case REAL_TYPE:
15457 case COMPLEX_TYPE:
15458 case VECTOR_TYPE:
15459 case BOOLEAN_TYPE:
15460 case NULLPTR_TYPE:
15461 case LANG_TYPE:
15462 return t;
15463
15464 case INTEGER_TYPE:
15465 if (t == integer_type_node)
15466 return t;
15467
15468 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15469 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15470 return t;
15471
15472 {
15473 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15474
15475 max = tsubst_expr (omax, args, complain, in_decl,
15476 /*integral_constant_expression_p=*/false);
15477
15478 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15479 needed. */
15480 if (TREE_CODE (max) == NOP_EXPR
15481 && TREE_SIDE_EFFECTS (omax)
15482 && !TREE_TYPE (max))
15483 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15484
15485 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15486 with TREE_SIDE_EFFECTS that indicates this is not an integral
15487 constant expression. */
15488 if (processing_template_decl
15489 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15490 {
15491 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15492 TREE_SIDE_EFFECTS (max) = 1;
15493 }
15494
15495 return compute_array_index_type (NULL_TREE, max, complain);
15496 }
15497
15498 case TEMPLATE_TYPE_PARM:
15499 case TEMPLATE_TEMPLATE_PARM:
15500 case BOUND_TEMPLATE_TEMPLATE_PARM:
15501 case TEMPLATE_PARM_INDEX:
15502 {
15503 int idx;
15504 int level;
15505 int levels;
15506 tree arg = NULL_TREE;
15507
15508 r = NULL_TREE;
15509
15510 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15511 template_parm_level_and_index (t, &level, &idx);
15512
15513 levels = TMPL_ARGS_DEPTH (args);
15514 if (level <= levels
15515 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15516 {
15517 arg = TMPL_ARG (args, level, idx);
15518
15519 /* See through ARGUMENT_PACK_SELECT arguments. */
15520 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15521 arg = argument_pack_select_arg (arg);
15522 }
15523
15524 if (arg == error_mark_node)
15525 return error_mark_node;
15526 else if (arg != NULL_TREE)
15527 {
15528 if (ARGUMENT_PACK_P (arg))
15529 /* If ARG is an argument pack, we don't actually want to
15530 perform a substitution here, because substitutions
15531 for argument packs are only done
15532 element-by-element. We can get to this point when
15533 substituting the type of a non-type template
15534 parameter pack, when that type actually contains
15535 template parameter packs from an outer template, e.g.,
15536
15537 template<typename... Types> struct A {
15538 template<Types... Values> struct B { };
15539 }; */
15540 return t;
15541
15542 if (code == TEMPLATE_TYPE_PARM)
15543 {
15544 int quals;
15545
15546 /* When building concept checks for the purpose of
15547 deducing placeholders, we can end up with wildcards
15548 where types are expected. Adjust this to the deduced
15549 value. */
15550 if (TREE_CODE (arg) == WILDCARD_DECL)
15551 arg = TREE_TYPE (TREE_TYPE (arg));
15552
15553 gcc_assert (TYPE_P (arg));
15554
15555 quals = cp_type_quals (arg) | cp_type_quals (t);
15556
15557 return cp_build_qualified_type_real
15558 (arg, quals, complain | tf_ignore_bad_quals);
15559 }
15560 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15561 {
15562 /* We are processing a type constructed from a
15563 template template parameter. */
15564 tree argvec = tsubst (TYPE_TI_ARGS (t),
15565 args, complain, in_decl);
15566 if (argvec == error_mark_node)
15567 return error_mark_node;
15568
15569 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15570 || TREE_CODE (arg) == TEMPLATE_DECL
15571 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15572
15573 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15574 /* Consider this code:
15575
15576 template <template <class> class Template>
15577 struct Internal {
15578 template <class Arg> using Bind = Template<Arg>;
15579 };
15580
15581 template <template <class> class Template, class Arg>
15582 using Instantiate = Template<Arg>; //#0
15583
15584 template <template <class> class Template,
15585 class Argument>
15586 using Bind =
15587 Instantiate<Internal<Template>::template Bind,
15588 Argument>; //#1
15589
15590 When #1 is parsed, the
15591 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15592 parameter `Template' in #0 matches the
15593 UNBOUND_CLASS_TEMPLATE representing the argument
15594 `Internal<Template>::template Bind'; We then want
15595 to assemble the type `Bind<Argument>' that can't
15596 be fully created right now, because
15597 `Internal<Template>' not being complete, the Bind
15598 template cannot be looked up in that context. So
15599 we need to "store" `Bind<Argument>' for later
15600 when the context of Bind becomes complete. Let's
15601 store that in a TYPENAME_TYPE. */
15602 return make_typename_type (TYPE_CONTEXT (arg),
15603 build_nt (TEMPLATE_ID_EXPR,
15604 TYPE_IDENTIFIER (arg),
15605 argvec),
15606 typename_type,
15607 complain);
15608
15609 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15610 are resolving nested-types in the signature of a
15611 member function templates. Otherwise ARG is a
15612 TEMPLATE_DECL and is the real template to be
15613 instantiated. */
15614 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15615 arg = TYPE_NAME (arg);
15616
15617 r = lookup_template_class (arg,
15618 argvec, in_decl,
15619 DECL_CONTEXT (arg),
15620 /*entering_scope=*/0,
15621 complain);
15622 return cp_build_qualified_type_real
15623 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15624 }
15625 else if (code == TEMPLATE_TEMPLATE_PARM)
15626 return arg;
15627 else
15628 /* TEMPLATE_PARM_INDEX. */
15629 return convert_from_reference (unshare_expr (arg));
15630 }
15631
15632 if (level == 1)
15633 /* This can happen during the attempted tsubst'ing in
15634 unify. This means that we don't yet have any information
15635 about the template parameter in question. */
15636 return t;
15637
15638 /* Early in template argument deduction substitution, we don't
15639 want to reduce the level of 'auto', or it will be confused
15640 with a normal template parm in subsequent deduction.
15641 Similarly, don't reduce the level of template parameters to
15642 avoid mismatches when deducing their types. */
15643 if (complain & tf_partial)
15644 return t;
15645
15646 /* If we get here, we must have been looking at a parm for a
15647 more deeply nested template. Make a new version of this
15648 template parameter, but with a lower level. */
15649 switch (code)
15650 {
15651 case TEMPLATE_TYPE_PARM:
15652 case TEMPLATE_TEMPLATE_PARM:
15653 case BOUND_TEMPLATE_TEMPLATE_PARM:
15654 if (cp_type_quals (t))
15655 {
15656 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15657 r = cp_build_qualified_type_real
15658 (r, cp_type_quals (t),
15659 complain | (code == TEMPLATE_TYPE_PARM
15660 ? tf_ignore_bad_quals : 0));
15661 }
15662 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15663 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15664 && (r = (TEMPLATE_PARM_DESCENDANTS
15665 (TEMPLATE_TYPE_PARM_INDEX (t))))
15666 && (r = TREE_TYPE (r))
15667 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15668 /* Break infinite recursion when substituting the constraints
15669 of a constrained placeholder. */;
15670 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15671 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15672 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15673 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15674 r = TEMPLATE_PARM_DESCENDANTS (arg))
15675 && (TEMPLATE_PARM_LEVEL (r)
15676 == TEMPLATE_PARM_LEVEL (arg) - levels))
15677 /* Cache the simple case of lowering a type parameter. */
15678 r = TREE_TYPE (r);
15679 else
15680 {
15681 r = copy_type (t);
15682 TEMPLATE_TYPE_PARM_INDEX (r)
15683 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15684 r, levels, args, complain);
15685 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15686 TYPE_MAIN_VARIANT (r) = r;
15687 TYPE_POINTER_TO (r) = NULL_TREE;
15688 TYPE_REFERENCE_TO (r) = NULL_TREE;
15689
15690 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15691 {
15692 /* Propagate constraints on placeholders since they are
15693 only instantiated during satisfaction. */
15694 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15695 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15696 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15697 {
15698 pl = tsubst_copy (pl, args, complain, in_decl);
15699 if (TREE_CODE (pl) == TEMPLATE_TEMPLATE_PARM)
15700 pl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl);
15701 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15702 }
15703 }
15704
15705 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15706 /* We have reduced the level of the template
15707 template parameter, but not the levels of its
15708 template parameters, so canonical_type_parameter
15709 will not be able to find the canonical template
15710 template parameter for this level. Thus, we
15711 require structural equality checking to compare
15712 TEMPLATE_TEMPLATE_PARMs. */
15713 SET_TYPE_STRUCTURAL_EQUALITY (r);
15714 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15715 SET_TYPE_STRUCTURAL_EQUALITY (r);
15716 else
15717 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15718
15719 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15720 {
15721 tree tinfo = TYPE_TEMPLATE_INFO (t);
15722 /* We might need to substitute into the types of non-type
15723 template parameters. */
15724 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15725 complain, in_decl);
15726 if (tmpl == error_mark_node)
15727 return error_mark_node;
15728 tree argvec = tsubst (TI_ARGS (tinfo), args,
15729 complain, in_decl);
15730 if (argvec == error_mark_node)
15731 return error_mark_node;
15732
15733 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15734 = build_template_info (tmpl, argvec);
15735 }
15736 }
15737 break;
15738
15739 case TEMPLATE_PARM_INDEX:
15740 /* OK, now substitute the type of the non-type parameter. We
15741 couldn't do it earlier because it might be an auto parameter,
15742 and we wouldn't need to if we had an argument. */
15743 type = tsubst (type, args, complain, in_decl);
15744 if (type == error_mark_node)
15745 return error_mark_node;
15746 r = reduce_template_parm_level (t, type, levels, args, complain);
15747 break;
15748
15749 default:
15750 gcc_unreachable ();
15751 }
15752
15753 return r;
15754 }
15755
15756 case TREE_LIST:
15757 return tsubst_tree_list (t, args, complain, in_decl);
15758
15759 case TREE_BINFO:
15760 /* We should never be tsubsting a binfo. */
15761 gcc_unreachable ();
15762
15763 case TREE_VEC:
15764 /* A vector of template arguments. */
15765 gcc_assert (!type);
15766 return tsubst_template_args (t, args, complain, in_decl);
15767
15768 case POINTER_TYPE:
15769 case REFERENCE_TYPE:
15770 {
15771 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15772 return t;
15773
15774 /* [temp.deduct]
15775
15776 Type deduction may fail for any of the following
15777 reasons:
15778
15779 -- Attempting to create a pointer to reference type.
15780 -- Attempting to create a reference to a reference type or
15781 a reference to void.
15782
15783 Core issue 106 says that creating a reference to a reference
15784 during instantiation is no longer a cause for failure. We
15785 only enforce this check in strict C++98 mode. */
15786 if ((TYPE_REF_P (type)
15787 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15788 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15789 {
15790 static location_t last_loc;
15791
15792 /* We keep track of the last time we issued this error
15793 message to avoid spewing a ton of messages during a
15794 single bad template instantiation. */
15795 if (complain & tf_error
15796 && last_loc != input_location)
15797 {
15798 if (VOID_TYPE_P (type))
15799 error ("forming reference to void");
15800 else if (code == POINTER_TYPE)
15801 error ("forming pointer to reference type %qT", type);
15802 else
15803 error ("forming reference to reference type %qT", type);
15804 last_loc = input_location;
15805 }
15806
15807 return error_mark_node;
15808 }
15809 else if (TREE_CODE (type) == FUNCTION_TYPE
15810 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15811 || type_memfn_rqual (type) != REF_QUAL_NONE))
15812 {
15813 if (complain & tf_error)
15814 {
15815 if (code == POINTER_TYPE)
15816 error ("forming pointer to qualified function type %qT",
15817 type);
15818 else
15819 error ("forming reference to qualified function type %qT",
15820 type);
15821 }
15822 return error_mark_node;
15823 }
15824 else if (code == POINTER_TYPE)
15825 {
15826 r = build_pointer_type (type);
15827 if (TREE_CODE (type) == METHOD_TYPE)
15828 r = build_ptrmemfunc_type (r);
15829 }
15830 else if (TYPE_REF_P (type))
15831 /* In C++0x, during template argument substitution, when there is an
15832 attempt to create a reference to a reference type, reference
15833 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15834
15835 "If a template-argument for a template-parameter T names a type
15836 that is a reference to a type A, an attempt to create the type
15837 'lvalue reference to cv T' creates the type 'lvalue reference to
15838 A,' while an attempt to create the type type rvalue reference to
15839 cv T' creates the type T"
15840 */
15841 r = cp_build_reference_type
15842 (TREE_TYPE (type),
15843 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15844 else
15845 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15846 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15847
15848 if (r != error_mark_node)
15849 /* Will this ever be needed for TYPE_..._TO values? */
15850 layout_type (r);
15851
15852 return r;
15853 }
15854 case OFFSET_TYPE:
15855 {
15856 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15857 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15858 {
15859 /* [temp.deduct]
15860
15861 Type deduction may fail for any of the following
15862 reasons:
15863
15864 -- Attempting to create "pointer to member of T" when T
15865 is not a class type. */
15866 if (complain & tf_error)
15867 error ("creating pointer to member of non-class type %qT", r);
15868 return error_mark_node;
15869 }
15870 if (TYPE_REF_P (type))
15871 {
15872 if (complain & tf_error)
15873 error ("creating pointer to member reference type %qT", type);
15874 return error_mark_node;
15875 }
15876 if (VOID_TYPE_P (type))
15877 {
15878 if (complain & tf_error)
15879 error ("creating pointer to member of type void");
15880 return error_mark_node;
15881 }
15882 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15883 if (TREE_CODE (type) == FUNCTION_TYPE)
15884 {
15885 /* The type of the implicit object parameter gets its
15886 cv-qualifiers from the FUNCTION_TYPE. */
15887 tree memptr;
15888 tree method_type
15889 = build_memfn_type (type, r, type_memfn_quals (type),
15890 type_memfn_rqual (type));
15891 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15892 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15893 complain);
15894 }
15895 else
15896 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15897 cp_type_quals (t),
15898 complain);
15899 }
15900 case FUNCTION_TYPE:
15901 case METHOD_TYPE:
15902 {
15903 tree fntype;
15904 tree specs;
15905 fntype = tsubst_function_type (t, args, complain, in_decl);
15906 if (fntype == error_mark_node)
15907 return error_mark_node;
15908
15909 /* Substitute the exception specification. */
15910 specs = tsubst_exception_specification (t, args, complain, in_decl,
15911 /*defer_ok*/fndecl_type);
15912 if (specs == error_mark_node)
15913 return error_mark_node;
15914 if (specs)
15915 fntype = build_exception_variant (fntype, specs);
15916 return fntype;
15917 }
15918 case ARRAY_TYPE:
15919 {
15920 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15921 if (domain == error_mark_node)
15922 return error_mark_node;
15923
15924 /* As an optimization, we avoid regenerating the array type if
15925 it will obviously be the same as T. */
15926 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15927 return t;
15928
15929 /* These checks should match the ones in create_array_type_for_decl.
15930
15931 [temp.deduct]
15932
15933 The deduction may fail for any of the following reasons:
15934
15935 -- Attempting to create an array with an element type that
15936 is void, a function type, or a reference type, or [DR337]
15937 an abstract class type. */
15938 if (VOID_TYPE_P (type)
15939 || TREE_CODE (type) == FUNCTION_TYPE
15940 || (TREE_CODE (type) == ARRAY_TYPE
15941 && TYPE_DOMAIN (type) == NULL_TREE)
15942 || TYPE_REF_P (type))
15943 {
15944 if (complain & tf_error)
15945 error ("creating array of %qT", type);
15946 return error_mark_node;
15947 }
15948
15949 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
15950 !(complain & tf_error)))
15951 return error_mark_node;
15952
15953 r = build_cplus_array_type (type, domain);
15954
15955 if (!valid_array_size_p (input_location, r, in_decl,
15956 (complain & tf_error)))
15957 return error_mark_node;
15958
15959 if (TYPE_USER_ALIGN (t))
15960 {
15961 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15962 TYPE_USER_ALIGN (r) = 1;
15963 }
15964
15965 return r;
15966 }
15967
15968 case TYPENAME_TYPE:
15969 {
15970 tree ctx = TYPE_CONTEXT (t);
15971 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15972 {
15973 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15974 if (ctx == error_mark_node
15975 || TREE_VEC_LENGTH (ctx) > 1)
15976 return error_mark_node;
15977 if (TREE_VEC_LENGTH (ctx) == 0)
15978 {
15979 if (complain & tf_error)
15980 error ("%qD is instantiated for an empty pack",
15981 TYPENAME_TYPE_FULLNAME (t));
15982 return error_mark_node;
15983 }
15984 ctx = TREE_VEC_ELT (ctx, 0);
15985 }
15986 else
15987 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15988 /*entering_scope=*/1);
15989 if (ctx == error_mark_node)
15990 return error_mark_node;
15991
15992 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15993 complain, in_decl);
15994 if (f == error_mark_node)
15995 return error_mark_node;
15996
15997 if (!MAYBE_CLASS_TYPE_P (ctx))
15998 {
15999 if (complain & tf_error)
16000 error ("%qT is not a class, struct, or union type", ctx);
16001 return error_mark_node;
16002 }
16003 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16004 {
16005 /* Normally, make_typename_type does not require that the CTX
16006 have complete type in order to allow things like:
16007
16008 template <class T> struct S { typename S<T>::X Y; };
16009
16010 But, such constructs have already been resolved by this
16011 point, so here CTX really should have complete type, unless
16012 it's a partial instantiation. */
16013 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16014 return error_mark_node;
16015 }
16016
16017 f = make_typename_type (ctx, f, typename_type,
16018 complain | tf_keep_type_decl);
16019 if (f == error_mark_node)
16020 return f;
16021 if (TREE_CODE (f) == TYPE_DECL)
16022 {
16023 complain |= tf_ignore_bad_quals;
16024 f = TREE_TYPE (f);
16025 }
16026
16027 if (TREE_CODE (f) != TYPENAME_TYPE)
16028 {
16029 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16030 {
16031 if (complain & tf_error)
16032 error ("%qT resolves to %qT, which is not an enumeration type",
16033 t, f);
16034 else
16035 return error_mark_node;
16036 }
16037 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16038 {
16039 if (complain & tf_error)
16040 error ("%qT resolves to %qT, which is not a class type",
16041 t, f);
16042 else
16043 return error_mark_node;
16044 }
16045 }
16046
16047 return cp_build_qualified_type_real
16048 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16049 }
16050
16051 case UNBOUND_CLASS_TEMPLATE:
16052 {
16053 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16054 in_decl, /*entering_scope=*/1);
16055 tree name = TYPE_IDENTIFIER (t);
16056 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16057
16058 if (ctx == error_mark_node || name == error_mark_node)
16059 return error_mark_node;
16060
16061 if (parm_list)
16062 parm_list = tsubst_template_parms (parm_list, args, complain);
16063 return make_unbound_class_template (ctx, name, parm_list, complain);
16064 }
16065
16066 case TYPEOF_TYPE:
16067 {
16068 tree type;
16069
16070 ++cp_unevaluated_operand;
16071 ++c_inhibit_evaluation_warnings;
16072
16073 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16074 complain, in_decl,
16075 /*integral_constant_expression_p=*/false);
16076
16077 --cp_unevaluated_operand;
16078 --c_inhibit_evaluation_warnings;
16079
16080 type = finish_typeof (type);
16081 return cp_build_qualified_type_real (type,
16082 cp_type_quals (t)
16083 | cp_type_quals (type),
16084 complain);
16085 }
16086
16087 case DECLTYPE_TYPE:
16088 {
16089 tree type;
16090
16091 ++cp_unevaluated_operand;
16092 ++c_inhibit_evaluation_warnings;
16093
16094 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16095 complain|tf_decltype, in_decl,
16096 /*function_p*/false,
16097 /*integral_constant_expression*/false);
16098
16099 --cp_unevaluated_operand;
16100 --c_inhibit_evaluation_warnings;
16101
16102 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16103 type = lambda_capture_field_type (type,
16104 false /*explicit_init*/,
16105 DECLTYPE_FOR_REF_CAPTURE (t));
16106 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16107 type = lambda_proxy_type (type);
16108 else
16109 {
16110 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16111 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16112 && EXPR_P (type))
16113 /* In a template ~id could be either a complement expression
16114 or an unqualified-id naming a destructor; if instantiating
16115 it produces an expression, it's not an id-expression or
16116 member access. */
16117 id = false;
16118 type = finish_decltype_type (type, id, complain);
16119 }
16120 return cp_build_qualified_type_real (type,
16121 cp_type_quals (t)
16122 | cp_type_quals (type),
16123 complain | tf_ignore_bad_quals);
16124 }
16125
16126 case UNDERLYING_TYPE:
16127 {
16128 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16129 complain, in_decl);
16130 return finish_underlying_type (type);
16131 }
16132
16133 case TYPE_ARGUMENT_PACK:
16134 case NONTYPE_ARGUMENT_PACK:
16135 return tsubst_argument_pack (t, args, complain, in_decl);
16136
16137 case VOID_CST:
16138 case INTEGER_CST:
16139 case REAL_CST:
16140 case STRING_CST:
16141 case PLUS_EXPR:
16142 case MINUS_EXPR:
16143 case NEGATE_EXPR:
16144 case NOP_EXPR:
16145 case INDIRECT_REF:
16146 case ADDR_EXPR:
16147 case CALL_EXPR:
16148 case ARRAY_REF:
16149 case SCOPE_REF:
16150 /* We should use one of the expression tsubsts for these codes. */
16151 gcc_unreachable ();
16152
16153 default:
16154 sorry ("use of %qs in template", get_tree_code_name (code));
16155 return error_mark_node;
16156 }
16157 }
16158
16159 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16160 expression on the left-hand side of the "." or "->" operator. We
16161 only do the lookup if we had a dependent BASELINK. Otherwise we
16162 adjust it onto the instantiated heirarchy. */
16163
16164 static tree
16165 tsubst_baselink (tree baselink, tree object_type,
16166 tree args, tsubst_flags_t complain, tree in_decl)
16167 {
16168 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16169 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16170 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16171
16172 tree optype = BASELINK_OPTYPE (baselink);
16173 optype = tsubst (optype, args, complain, in_decl);
16174
16175 tree template_args = NULL_TREE;
16176 bool template_id_p = false;
16177 tree fns = BASELINK_FUNCTIONS (baselink);
16178 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16179 {
16180 template_id_p = true;
16181 template_args = TREE_OPERAND (fns, 1);
16182 fns = TREE_OPERAND (fns, 0);
16183 if (template_args)
16184 template_args = tsubst_template_args (template_args, args,
16185 complain, in_decl);
16186 }
16187
16188 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16189 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16190 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16191
16192 if (dependent_p)
16193 {
16194 tree name = OVL_NAME (fns);
16195 if (IDENTIFIER_CONV_OP_P (name))
16196 name = make_conv_op_name (optype);
16197
16198 if (name == complete_dtor_identifier)
16199 /* Treat as-if non-dependent below. */
16200 dependent_p = false;
16201
16202 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16203 complain);
16204 if (!baselink)
16205 {
16206 if ((complain & tf_error)
16207 && constructor_name_p (name, qualifying_scope))
16208 error ("cannot call constructor %<%T::%D%> directly",
16209 qualifying_scope, name);
16210 return error_mark_node;
16211 }
16212
16213 if (BASELINK_P (baselink))
16214 fns = BASELINK_FUNCTIONS (baselink);
16215 }
16216 else
16217 /* We're going to overwrite pieces below, make a duplicate. */
16218 baselink = copy_node (baselink);
16219
16220 /* If lookup found a single function, mark it as used at this point.
16221 (If lookup found multiple functions the one selected later by
16222 overload resolution will be marked as used at that point.) */
16223 if (!template_id_p && !really_overloaded_fn (fns))
16224 {
16225 tree fn = OVL_FIRST (fns);
16226 bool ok = mark_used (fn, complain);
16227 if (!ok && !(complain & tf_error))
16228 return error_mark_node;
16229 if (ok && BASELINK_P (baselink))
16230 /* We might have instantiated an auto function. */
16231 TREE_TYPE (baselink) = TREE_TYPE (fn);
16232 }
16233
16234 if (BASELINK_P (baselink))
16235 {
16236 /* Add back the template arguments, if present. */
16237 if (template_id_p)
16238 BASELINK_FUNCTIONS (baselink)
16239 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16240
16241 /* Update the conversion operator type. */
16242 BASELINK_OPTYPE (baselink) = optype;
16243 }
16244
16245 if (!object_type)
16246 object_type = current_class_type;
16247
16248 if (qualified_p || !dependent_p)
16249 {
16250 baselink = adjust_result_of_qualified_name_lookup (baselink,
16251 qualifying_scope,
16252 object_type);
16253 if (!qualified_p)
16254 /* We need to call adjust_result_of_qualified_name_lookup in case the
16255 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16256 so that we still get virtual function binding. */
16257 BASELINK_QUALIFIED_P (baselink) = false;
16258 }
16259
16260 return baselink;
16261 }
16262
16263 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16264 true if the qualified-id will be a postfix-expression in-and-of
16265 itself; false if more of the postfix-expression follows the
16266 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16267 of "&". */
16268
16269 static tree
16270 tsubst_qualified_id (tree qualified_id, tree args,
16271 tsubst_flags_t complain, tree in_decl,
16272 bool done, bool address_p)
16273 {
16274 tree expr;
16275 tree scope;
16276 tree name;
16277 bool is_template;
16278 tree template_args;
16279 location_t loc = EXPR_LOCATION (qualified_id);
16280
16281 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16282
16283 /* Figure out what name to look up. */
16284 name = TREE_OPERAND (qualified_id, 1);
16285 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16286 {
16287 is_template = true;
16288 template_args = TREE_OPERAND (name, 1);
16289 if (template_args)
16290 template_args = tsubst_template_args (template_args, args,
16291 complain, in_decl);
16292 if (template_args == error_mark_node)
16293 return error_mark_node;
16294 name = TREE_OPERAND (name, 0);
16295 }
16296 else
16297 {
16298 is_template = false;
16299 template_args = NULL_TREE;
16300 }
16301
16302 /* Substitute into the qualifying scope. When there are no ARGS, we
16303 are just trying to simplify a non-dependent expression. In that
16304 case the qualifying scope may be dependent, and, in any case,
16305 substituting will not help. */
16306 scope = TREE_OPERAND (qualified_id, 0);
16307 if (args)
16308 {
16309 scope = tsubst (scope, args, complain, in_decl);
16310 expr = tsubst_copy (name, args, complain, in_decl);
16311 }
16312 else
16313 expr = name;
16314
16315 if (dependent_scope_p (scope))
16316 {
16317 if (is_template)
16318 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16319 tree r = build_qualified_name (NULL_TREE, scope, expr,
16320 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16321 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16322 return r;
16323 }
16324
16325 if (!BASELINK_P (name) && !DECL_P (expr))
16326 {
16327 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16328 {
16329 /* A BIT_NOT_EXPR is used to represent a destructor. */
16330 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16331 {
16332 error ("qualifying type %qT does not match destructor name ~%qT",
16333 scope, TREE_OPERAND (expr, 0));
16334 expr = error_mark_node;
16335 }
16336 else
16337 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16338 LOOK_want::NORMAL, false);
16339 }
16340 else
16341 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16342 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16343 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16344 {
16345 if (complain & tf_error)
16346 {
16347 error ("dependent-name %qE is parsed as a non-type, but "
16348 "instantiation yields a type", qualified_id);
16349 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16350 }
16351 return error_mark_node;
16352 }
16353 }
16354
16355 if (DECL_P (expr))
16356 {
16357 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16358 scope, complain))
16359 return error_mark_node;
16360 /* Remember that there was a reference to this entity. */
16361 if (!mark_used (expr, complain) && !(complain & tf_error))
16362 return error_mark_node;
16363 }
16364
16365 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16366 {
16367 if (complain & tf_error)
16368 qualified_name_lookup_error (scope,
16369 TREE_OPERAND (qualified_id, 1),
16370 expr, input_location);
16371 return error_mark_node;
16372 }
16373
16374 if (is_template)
16375 {
16376 /* We may be repeating a check already done during parsing, but
16377 if it was well-formed and passed then, it will pass again
16378 now, and if it didn't, we wouldn't have got here. The case
16379 we want to catch is when we couldn't tell then, and can now,
16380 namely when templ prior to substitution was an
16381 identifier. */
16382 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16383 return error_mark_node;
16384
16385 if (variable_template_p (expr))
16386 expr = lookup_and_finish_template_variable (expr, template_args,
16387 complain);
16388 else
16389 expr = lookup_template_function (expr, template_args);
16390 }
16391
16392 if (expr == error_mark_node && complain & tf_error)
16393 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16394 expr, input_location);
16395 else if (TYPE_P (scope))
16396 {
16397 expr = (adjust_result_of_qualified_name_lookup
16398 (expr, scope, current_nonlambda_class_type ()));
16399 expr = (finish_qualified_id_expr
16400 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16401 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16402 /*template_arg_p=*/false, complain));
16403 }
16404
16405 /* Expressions do not generally have reference type. */
16406 if (TREE_CODE (expr) != SCOPE_REF
16407 /* However, if we're about to form a pointer-to-member, we just
16408 want the referenced member referenced. */
16409 && TREE_CODE (expr) != OFFSET_REF)
16410 expr = convert_from_reference (expr);
16411
16412 if (REF_PARENTHESIZED_P (qualified_id))
16413 expr = force_paren_expr (expr);
16414
16415 expr = maybe_wrap_with_location (expr, loc);
16416
16417 return expr;
16418 }
16419
16420 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16421 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16422 for tsubst. */
16423
16424 static tree
16425 tsubst_init (tree init, tree decl, tree args,
16426 tsubst_flags_t complain, tree in_decl)
16427 {
16428 if (!init)
16429 return NULL_TREE;
16430
16431 init = tsubst_expr (init, args, complain, in_decl, false);
16432
16433 tree type = TREE_TYPE (decl);
16434
16435 if (!init && type != error_mark_node)
16436 {
16437 if (tree auto_node = type_uses_auto (type))
16438 {
16439 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16440 {
16441 if (complain & tf_error)
16442 error ("initializer for %q#D expands to an empty list "
16443 "of expressions", decl);
16444 return error_mark_node;
16445 }
16446 }
16447 else if (!dependent_type_p (type))
16448 {
16449 /* If we had an initializer but it
16450 instantiated to nothing,
16451 value-initialize the object. This will
16452 only occur when the initializer was a
16453 pack expansion where the parameter packs
16454 used in that expansion were of length
16455 zero. */
16456 init = build_value_init (type, complain);
16457 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16458 init = get_target_expr_sfinae (init, complain);
16459 if (TREE_CODE (init) == TARGET_EXPR)
16460 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16461 }
16462 }
16463
16464 return init;
16465 }
16466
16467 /* If T is a reference to a dependent member of the current instantiation C and
16468 we are trying to refer to that member in a partial instantiation of C,
16469 return a SCOPE_REF; otherwise, return NULL_TREE.
16470
16471 This can happen when forming a C++17 deduction guide, as in PR96199. */
16472
16473 static tree
16474 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16475 tree in_decl)
16476 {
16477 if (cxx_dialect < cxx17)
16478 return NULL_TREE;
16479
16480 tree ctx = context_for_name_lookup (t);
16481 if (!CLASS_TYPE_P (ctx))
16482 return NULL_TREE;
16483
16484 ctx = tsubst (ctx, args, complain, in_decl);
16485 if (dependent_scope_p (ctx))
16486 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16487 /*template_p=*/false);
16488
16489 return NULL_TREE;
16490 }
16491
16492 /* Like tsubst, but deals with expressions. This function just replaces
16493 template parms; to finish processing the resultant expression, use
16494 tsubst_copy_and_build or tsubst_expr. */
16495
16496 static tree
16497 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16498 {
16499 enum tree_code code;
16500 tree r;
16501
16502 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16503 return t;
16504
16505 code = TREE_CODE (t);
16506
16507 switch (code)
16508 {
16509 case PARM_DECL:
16510 r = retrieve_local_specialization (t);
16511
16512 if (r == NULL_TREE)
16513 {
16514 /* We get here for a use of 'this' in an NSDMI. */
16515 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16516 return current_class_ptr;
16517
16518 /* This can happen for a parameter name used later in a function
16519 declaration (such as in a late-specified return type). Just
16520 make a dummy decl, since it's only used for its type. */
16521 gcc_assert (cp_unevaluated_operand != 0);
16522 r = tsubst_decl (t, args, complain);
16523 /* Give it the template pattern as its context; its true context
16524 hasn't been instantiated yet and this is good enough for
16525 mangling. */
16526 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16527 }
16528
16529 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16530 r = argument_pack_select_arg (r);
16531 if (!mark_used (r, complain) && !(complain & tf_error))
16532 return error_mark_node;
16533 return r;
16534
16535 case CONST_DECL:
16536 {
16537 tree enum_type;
16538 tree v;
16539
16540 if (DECL_TEMPLATE_PARM_P (t))
16541 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16542 /* There is no need to substitute into namespace-scope
16543 enumerators. */
16544 if (DECL_NAMESPACE_SCOPE_P (t))
16545 return t;
16546 /* If ARGS is NULL, then T is known to be non-dependent. */
16547 if (args == NULL_TREE)
16548 return scalar_constant_value (t);
16549
16550 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16551 return ref;
16552
16553 /* Unfortunately, we cannot just call lookup_name here.
16554 Consider:
16555
16556 template <int I> int f() {
16557 enum E { a = I };
16558 struct S { void g() { E e = a; } };
16559 };
16560
16561 When we instantiate f<7>::S::g(), say, lookup_name is not
16562 clever enough to find f<7>::a. */
16563 enum_type
16564 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16565 /*entering_scope=*/0);
16566
16567 for (v = TYPE_VALUES (enum_type);
16568 v != NULL_TREE;
16569 v = TREE_CHAIN (v))
16570 if (TREE_PURPOSE (v) == DECL_NAME (t))
16571 return TREE_VALUE (v);
16572
16573 /* We didn't find the name. That should never happen; if
16574 name-lookup found it during preliminary parsing, we
16575 should find it again here during instantiation. */
16576 gcc_unreachable ();
16577 }
16578 return t;
16579
16580 case FIELD_DECL:
16581 if (DECL_CONTEXT (t))
16582 {
16583 tree ctx;
16584
16585 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16586 /*entering_scope=*/1);
16587 if (ctx != DECL_CONTEXT (t))
16588 {
16589 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16590 if (!r)
16591 {
16592 if (complain & tf_error)
16593 error ("using invalid field %qD", t);
16594 return error_mark_node;
16595 }
16596 return r;
16597 }
16598 }
16599
16600 return t;
16601
16602 case VAR_DECL:
16603 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16604 return ref;
16605 gcc_fallthrough();
16606 case FUNCTION_DECL:
16607 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16608 r = tsubst (t, args, complain, in_decl);
16609 else if (DECL_LOCAL_DECL_P (t))
16610 {
16611 /* Local specialization will have been created when we
16612 instantiated the DECL_EXPR_DECL. */
16613 r = retrieve_local_specialization (t);
16614 if (!r)
16615 r = error_mark_node;
16616 }
16617 else if (local_variable_p (t)
16618 && uses_template_parms (DECL_CONTEXT (t)))
16619 {
16620 r = retrieve_local_specialization (t);
16621 if (r == NULL_TREE)
16622 {
16623 /* First try name lookup to find the instantiation. */
16624 r = lookup_name (DECL_NAME (t));
16625 if (r)
16626 {
16627 if (!VAR_P (r))
16628 {
16629 /* During error-recovery we may find a non-variable,
16630 even an OVERLOAD: just bail out and avoid ICEs and
16631 duplicate diagnostics (c++/62207). */
16632 gcc_assert (seen_error ());
16633 return error_mark_node;
16634 }
16635 if (!is_capture_proxy (r))
16636 {
16637 /* Make sure the one we found is the one we want. */
16638 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16639 if (ctx != DECL_CONTEXT (r))
16640 r = NULL_TREE;
16641 }
16642 }
16643
16644 if (r)
16645 /* OK */;
16646 else
16647 {
16648 /* This can happen for a variable used in a
16649 late-specified return type of a local lambda, or for a
16650 local static or constant. Building a new VAR_DECL
16651 should be OK in all those cases. */
16652 r = tsubst_decl (t, args, complain);
16653 if (local_specializations)
16654 /* Avoid infinite recursion (79640). */
16655 register_local_specialization (r, t);
16656 if (decl_maybe_constant_var_p (r))
16657 {
16658 /* We can't call cp_finish_decl, so handle the
16659 initializer by hand. */
16660 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16661 complain, in_decl);
16662 if (!processing_template_decl)
16663 init = maybe_constant_init (init);
16664 if (processing_template_decl
16665 ? potential_constant_expression (init)
16666 : reduced_constant_expression_p (init))
16667 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16668 = TREE_CONSTANT (r) = true;
16669 DECL_INITIAL (r) = init;
16670 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16671 TREE_TYPE (r)
16672 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16673 complain, adc_variable_type);
16674 }
16675 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16676 || decl_constant_var_p (r)
16677 || seen_error ());
16678 if (!processing_template_decl
16679 && !TREE_STATIC (r))
16680 r = process_outer_var_ref (r, complain);
16681 }
16682 /* Remember this for subsequent uses. */
16683 if (local_specializations)
16684 register_local_specialization (r, t);
16685 }
16686 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16687 r = argument_pack_select_arg (r);
16688 }
16689 else
16690 r = t;
16691 if (!mark_used (r, complain))
16692 return error_mark_node;
16693 return r;
16694
16695 case NAMESPACE_DECL:
16696 return t;
16697
16698 case OVERLOAD:
16699 return t;
16700
16701 case BASELINK:
16702 return tsubst_baselink (t, current_nonlambda_class_type (),
16703 args, complain, in_decl);
16704
16705 case TEMPLATE_DECL:
16706 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16707 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16708 args, complain, in_decl);
16709 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16710 return tsubst (t, args, complain, in_decl);
16711 else if (DECL_CLASS_SCOPE_P (t)
16712 && uses_template_parms (DECL_CONTEXT (t)))
16713 {
16714 /* Template template argument like the following example need
16715 special treatment:
16716
16717 template <template <class> class TT> struct C {};
16718 template <class T> struct D {
16719 template <class U> struct E {};
16720 C<E> c; // #1
16721 };
16722 D<int> d; // #2
16723
16724 We are processing the template argument `E' in #1 for
16725 the template instantiation #2. Originally, `E' is a
16726 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16727 have to substitute this with one having context `D<int>'. */
16728
16729 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16730 if (dependent_scope_p (context))
16731 {
16732 /* When rewriting a constructor into a deduction guide, a
16733 non-dependent name can become dependent, so memtmpl<args>
16734 becomes context::template memtmpl<args>. */
16735 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16736 return build_qualified_name (type, context, DECL_NAME (t),
16737 /*template*/true);
16738 }
16739 return lookup_field (context, DECL_NAME(t), 0, false);
16740 }
16741 else
16742 /* Ordinary template template argument. */
16743 return t;
16744
16745 case NON_LVALUE_EXPR:
16746 case VIEW_CONVERT_EXPR:
16747 {
16748 /* Handle location wrappers by substituting the wrapped node
16749 first, *then* reusing the resulting type. Doing the type
16750 first ensures that we handle template parameters and
16751 parameter pack expansions. */
16752 if (location_wrapper_p (t))
16753 {
16754 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16755 complain, in_decl);
16756 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16757 }
16758 tree op = TREE_OPERAND (t, 0);
16759 if (code == VIEW_CONVERT_EXPR
16760 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16761 {
16762 /* Wrapper to make a C++20 template parameter object const. */
16763 op = tsubst_copy (op, args, complain, in_decl);
16764 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16765 {
16766 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16767 return build1 (code, type, op);
16768 }
16769 else
16770 {
16771 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16772 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16773 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16774 return op;
16775 }
16776 }
16777 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16778 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16779 {
16780 op = tsubst_copy (op, args, complain, in_decl);
16781 op = build1 (code, TREE_TYPE (op), op);
16782 REF_PARENTHESIZED_P (op) = true;
16783 return op;
16784 }
16785 /* We shouldn't see any other uses of these in templates. */
16786 gcc_unreachable ();
16787 }
16788
16789 case CAST_EXPR:
16790 case REINTERPRET_CAST_EXPR:
16791 case CONST_CAST_EXPR:
16792 case STATIC_CAST_EXPR:
16793 case DYNAMIC_CAST_EXPR:
16794 case IMPLICIT_CONV_EXPR:
16795 case CONVERT_EXPR:
16796 case NOP_EXPR:
16797 {
16798 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16799 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16800 return build1 (code, type, op0);
16801 }
16802
16803 case BIT_CAST_EXPR:
16804 {
16805 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16806 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16807 r = build_min (BIT_CAST_EXPR, type, op0);
16808 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16809 return r;
16810 }
16811
16812 case SIZEOF_EXPR:
16813 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16814 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16815 {
16816 tree expanded, op = TREE_OPERAND (t, 0);
16817 int len = 0;
16818
16819 if (SIZEOF_EXPR_TYPE_P (t))
16820 op = TREE_TYPE (op);
16821
16822 ++cp_unevaluated_operand;
16823 ++c_inhibit_evaluation_warnings;
16824 /* We only want to compute the number of arguments. */
16825 if (PACK_EXPANSION_P (op))
16826 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16827 else
16828 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16829 args, complain, in_decl);
16830 --cp_unevaluated_operand;
16831 --c_inhibit_evaluation_warnings;
16832
16833 if (TREE_CODE (expanded) == TREE_VEC)
16834 {
16835 len = TREE_VEC_LENGTH (expanded);
16836 /* Set TREE_USED for the benefit of -Wunused. */
16837 for (int i = 0; i < len; i++)
16838 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16839 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16840 }
16841
16842 if (expanded == error_mark_node)
16843 return error_mark_node;
16844 else if (PACK_EXPANSION_P (expanded)
16845 || (TREE_CODE (expanded) == TREE_VEC
16846 && pack_expansion_args_count (expanded)))
16847
16848 {
16849 if (PACK_EXPANSION_P (expanded))
16850 /* OK. */;
16851 else if (TREE_VEC_LENGTH (expanded) == 1)
16852 expanded = TREE_VEC_ELT (expanded, 0);
16853 else
16854 expanded = make_argument_pack (expanded);
16855
16856 if (TYPE_P (expanded))
16857 return cxx_sizeof_or_alignof_type (input_location,
16858 expanded, SIZEOF_EXPR,
16859 false,
16860 complain & tf_error);
16861 else
16862 return cxx_sizeof_or_alignof_expr (input_location,
16863 expanded, SIZEOF_EXPR,
16864 false,
16865 complain & tf_error);
16866 }
16867 else
16868 return build_int_cst (size_type_node, len);
16869 }
16870 if (SIZEOF_EXPR_TYPE_P (t))
16871 {
16872 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16873 args, complain, in_decl);
16874 r = build1 (NOP_EXPR, r, error_mark_node);
16875 r = build1 (SIZEOF_EXPR,
16876 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16877 SIZEOF_EXPR_TYPE_P (r) = 1;
16878 return r;
16879 }
16880 /* Fall through */
16881
16882 case INDIRECT_REF:
16883 case NEGATE_EXPR:
16884 case TRUTH_NOT_EXPR:
16885 case BIT_NOT_EXPR:
16886 case ADDR_EXPR:
16887 case UNARY_PLUS_EXPR: /* Unary + */
16888 case ALIGNOF_EXPR:
16889 case AT_ENCODE_EXPR:
16890 case ARROW_EXPR:
16891 case THROW_EXPR:
16892 case TYPEID_EXPR:
16893 case REALPART_EXPR:
16894 case IMAGPART_EXPR:
16895 case PAREN_EXPR:
16896 {
16897 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16898 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16899 r = build1 (code, type, op0);
16900 if (code == ALIGNOF_EXPR)
16901 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16902 return r;
16903 }
16904
16905 case COMPONENT_REF:
16906 {
16907 tree object;
16908 tree name;
16909
16910 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16911 name = TREE_OPERAND (t, 1);
16912 if (TREE_CODE (name) == BIT_NOT_EXPR)
16913 {
16914 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16915 complain, in_decl);
16916 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16917 }
16918 else if (TREE_CODE (name) == SCOPE_REF
16919 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16920 {
16921 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16922 complain, in_decl);
16923 name = TREE_OPERAND (name, 1);
16924 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16925 complain, in_decl);
16926 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16927 name = build_qualified_name (/*type=*/NULL_TREE,
16928 base, name,
16929 /*template_p=*/false);
16930 }
16931 else if (BASELINK_P (name))
16932 name = tsubst_baselink (name,
16933 non_reference (TREE_TYPE (object)),
16934 args, complain,
16935 in_decl);
16936 else
16937 name = tsubst_copy (name, args, complain, in_decl);
16938 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16939 }
16940
16941 case PLUS_EXPR:
16942 case MINUS_EXPR:
16943 case MULT_EXPR:
16944 case TRUNC_DIV_EXPR:
16945 case CEIL_DIV_EXPR:
16946 case FLOOR_DIV_EXPR:
16947 case ROUND_DIV_EXPR:
16948 case EXACT_DIV_EXPR:
16949 case BIT_AND_EXPR:
16950 case BIT_IOR_EXPR:
16951 case BIT_XOR_EXPR:
16952 case TRUNC_MOD_EXPR:
16953 case FLOOR_MOD_EXPR:
16954 case TRUTH_ANDIF_EXPR:
16955 case TRUTH_ORIF_EXPR:
16956 case TRUTH_AND_EXPR:
16957 case TRUTH_OR_EXPR:
16958 case RSHIFT_EXPR:
16959 case LSHIFT_EXPR:
16960 case EQ_EXPR:
16961 case NE_EXPR:
16962 case MAX_EXPR:
16963 case MIN_EXPR:
16964 case LE_EXPR:
16965 case GE_EXPR:
16966 case LT_EXPR:
16967 case GT_EXPR:
16968 case COMPOUND_EXPR:
16969 case DOTSTAR_EXPR:
16970 case MEMBER_REF:
16971 case PREDECREMENT_EXPR:
16972 case PREINCREMENT_EXPR:
16973 case POSTDECREMENT_EXPR:
16974 case POSTINCREMENT_EXPR:
16975 {
16976 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16977 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16978 return build_nt (code, op0, op1);
16979 }
16980
16981 case SCOPE_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_qualified_name (/*type=*/NULL_TREE, op0, op1,
16986 QUALIFIED_NAME_IS_TEMPLATE (t));
16987 }
16988
16989 case ARRAY_REF:
16990 {
16991 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16992 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16993 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16994 }
16995
16996 case CALL_EXPR:
16997 {
16998 int n = VL_EXP_OPERAND_LENGTH (t);
16999 tree result = build_vl_exp (CALL_EXPR, n);
17000 int i;
17001 for (i = 0; i < n; i++)
17002 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
17003 complain, in_decl);
17004 return result;
17005 }
17006
17007 case COND_EXPR:
17008 case MODOP_EXPR:
17009 case PSEUDO_DTOR_EXPR:
17010 case VEC_PERM_EXPR:
17011 {
17012 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17013 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17014 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17015 r = build_nt (code, op0, op1, op2);
17016 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17017 return r;
17018 }
17019
17020 case NEW_EXPR:
17021 {
17022 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17023 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17024 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17025 r = build_nt (code, op0, op1, op2);
17026 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17027 return r;
17028 }
17029
17030 case DELETE_EXPR:
17031 {
17032 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17033 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17034 r = build_nt (code, op0, op1);
17035 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17036 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17037 return r;
17038 }
17039
17040 case TEMPLATE_ID_EXPR:
17041 {
17042 /* Substituted template arguments */
17043 tree fn = TREE_OPERAND (t, 0);
17044 tree targs = TREE_OPERAND (t, 1);
17045
17046 fn = tsubst_copy (fn, args, complain, in_decl);
17047 if (targs)
17048 targs = tsubst_template_args (targs, args, complain, in_decl);
17049
17050 return lookup_template_function (fn, targs);
17051 }
17052
17053 case TREE_LIST:
17054 {
17055 tree purpose, value, chain;
17056
17057 if (t == void_list_node)
17058 return t;
17059
17060 purpose = TREE_PURPOSE (t);
17061 if (purpose)
17062 purpose = tsubst_copy (purpose, args, complain, in_decl);
17063 value = TREE_VALUE (t);
17064 if (value)
17065 value = tsubst_copy (value, args, complain, in_decl);
17066 chain = TREE_CHAIN (t);
17067 if (chain && chain != void_type_node)
17068 chain = tsubst_copy (chain, args, complain, in_decl);
17069 if (purpose == TREE_PURPOSE (t)
17070 && value == TREE_VALUE (t)
17071 && chain == TREE_CHAIN (t))
17072 return t;
17073 return tree_cons (purpose, value, chain);
17074 }
17075
17076 case RECORD_TYPE:
17077 case UNION_TYPE:
17078 case ENUMERAL_TYPE:
17079 case INTEGER_TYPE:
17080 case TEMPLATE_TYPE_PARM:
17081 case TEMPLATE_TEMPLATE_PARM:
17082 case BOUND_TEMPLATE_TEMPLATE_PARM:
17083 case TEMPLATE_PARM_INDEX:
17084 case POINTER_TYPE:
17085 case REFERENCE_TYPE:
17086 case OFFSET_TYPE:
17087 case FUNCTION_TYPE:
17088 case METHOD_TYPE:
17089 case ARRAY_TYPE:
17090 case TYPENAME_TYPE:
17091 case UNBOUND_CLASS_TEMPLATE:
17092 case TYPEOF_TYPE:
17093 case DECLTYPE_TYPE:
17094 case TYPE_DECL:
17095 return tsubst (t, args, complain, in_decl);
17096
17097 case USING_DECL:
17098 t = DECL_NAME (t);
17099 /* Fall through. */
17100 case IDENTIFIER_NODE:
17101 if (IDENTIFIER_CONV_OP_P (t))
17102 {
17103 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17104 return make_conv_op_name (new_type);
17105 }
17106 else
17107 return t;
17108
17109 case CONSTRUCTOR:
17110 /* This is handled by tsubst_copy_and_build. */
17111 gcc_unreachable ();
17112
17113 case VA_ARG_EXPR:
17114 {
17115 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17116 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17117 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17118 }
17119
17120 case CLEANUP_POINT_EXPR:
17121 /* We shouldn't have built any of these during initial template
17122 generation. Instead, they should be built during instantiation
17123 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17124 gcc_unreachable ();
17125
17126 case OFFSET_REF:
17127 {
17128 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17129 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17130 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17131 r = build2 (code, type, op0, op1);
17132 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17133 if (!mark_used (TREE_OPERAND (r, 1), complain)
17134 && !(complain & tf_error))
17135 return error_mark_node;
17136 return r;
17137 }
17138
17139 case EXPR_PACK_EXPANSION:
17140 error ("invalid use of pack expansion expression");
17141 return error_mark_node;
17142
17143 case NONTYPE_ARGUMENT_PACK:
17144 error ("use %<...%> to expand argument pack");
17145 return error_mark_node;
17146
17147 case VOID_CST:
17148 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17149 return t;
17150
17151 case INTEGER_CST:
17152 case REAL_CST:
17153 case COMPLEX_CST:
17154 {
17155 /* Instantiate any typedefs in the type. */
17156 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17157 r = fold_convert (type, t);
17158 gcc_assert (TREE_CODE (r) == code);
17159 return r;
17160 }
17161
17162 case STRING_CST:
17163 {
17164 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17165 r = t;
17166 if (type != TREE_TYPE (t))
17167 {
17168 r = copy_node (t);
17169 TREE_TYPE (r) = type;
17170 }
17171 return r;
17172 }
17173
17174 case PTRMEM_CST:
17175 /* These can sometimes show up in a partial instantiation, but never
17176 involve template parms. */
17177 gcc_assert (!uses_template_parms (t));
17178 return t;
17179
17180 case UNARY_LEFT_FOLD_EXPR:
17181 return tsubst_unary_left_fold (t, args, complain, in_decl);
17182 case UNARY_RIGHT_FOLD_EXPR:
17183 return tsubst_unary_right_fold (t, args, complain, in_decl);
17184 case BINARY_LEFT_FOLD_EXPR:
17185 return tsubst_binary_left_fold (t, args, complain, in_decl);
17186 case BINARY_RIGHT_FOLD_EXPR:
17187 return tsubst_binary_right_fold (t, args, complain, in_decl);
17188 case PREDICT_EXPR:
17189 return t;
17190
17191 case DEBUG_BEGIN_STMT:
17192 /* ??? There's no point in copying it for now, but maybe some
17193 day it will contain more information, such as a pointer back
17194 to the containing function, inlined copy or so. */
17195 return t;
17196
17197 case CO_AWAIT_EXPR:
17198 return tsubst_expr (t, args, complain, in_decl,
17199 /*integral_constant_expression_p=*/false);
17200 break;
17201
17202 default:
17203 /* We shouldn't get here, but keep going if !flag_checking. */
17204 if (flag_checking)
17205 gcc_unreachable ();
17206 return t;
17207 }
17208 }
17209
17210 /* Helper function for tsubst_omp_clauses, used for instantiation of
17211 OMP_CLAUSE_DECL of clauses. */
17212
17213 static tree
17214 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17215 tree in_decl, tree *iterator_cache)
17216 {
17217 if (decl == NULL_TREE)
17218 return NULL_TREE;
17219
17220 /* Handle OpenMP iterators. */
17221 if (TREE_CODE (decl) == TREE_LIST
17222 && TREE_PURPOSE (decl)
17223 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17224 {
17225 tree ret;
17226 if (iterator_cache[0] == TREE_PURPOSE (decl))
17227 ret = iterator_cache[1];
17228 else
17229 {
17230 tree *tp = &ret;
17231 begin_scope (sk_omp, NULL);
17232 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17233 {
17234 *tp = copy_node (it);
17235 TREE_VEC_ELT (*tp, 0)
17236 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17237 TREE_VEC_ELT (*tp, 1)
17238 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17239 /*integral_constant_expression_p=*/false);
17240 TREE_VEC_ELT (*tp, 2)
17241 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17242 /*integral_constant_expression_p=*/false);
17243 TREE_VEC_ELT (*tp, 3)
17244 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17245 /*integral_constant_expression_p=*/false);
17246 TREE_CHAIN (*tp) = NULL_TREE;
17247 tp = &TREE_CHAIN (*tp);
17248 }
17249 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17250 iterator_cache[0] = TREE_PURPOSE (decl);
17251 iterator_cache[1] = ret;
17252 }
17253 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17254 args, complain,
17255 in_decl, NULL));
17256 }
17257
17258 /* Handle an OpenMP array section represented as a TREE_LIST (or
17259 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17260 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17261 TREE_LIST. We can handle it exactly the same as an array section
17262 (purpose, value, and a chain), even though the nomenclature
17263 (low_bound, length, etc) is different. */
17264 if (TREE_CODE (decl) == TREE_LIST)
17265 {
17266 tree low_bound
17267 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17268 /*integral_constant_expression_p=*/false);
17269 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17270 /*integral_constant_expression_p=*/false);
17271 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17272 in_decl, NULL);
17273 if (TREE_PURPOSE (decl) == low_bound
17274 && TREE_VALUE (decl) == length
17275 && TREE_CHAIN (decl) == chain)
17276 return decl;
17277 tree ret = tree_cons (low_bound, length, chain);
17278 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17279 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17280 return ret;
17281 }
17282 tree ret = tsubst_expr (decl, args, complain, in_decl,
17283 /*integral_constant_expression_p=*/false);
17284 /* Undo convert_from_reference tsubst_expr could have called. */
17285 if (decl
17286 && REFERENCE_REF_P (ret)
17287 && !REFERENCE_REF_P (decl))
17288 ret = TREE_OPERAND (ret, 0);
17289 return ret;
17290 }
17291
17292 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17293
17294 static tree
17295 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17296 tree args, tsubst_flags_t complain, tree in_decl)
17297 {
17298 tree new_clauses = NULL_TREE, nc, oc;
17299 tree linear_no_step = NULL_TREE;
17300 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17301
17302 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17303 {
17304 nc = copy_node (oc);
17305 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17306 new_clauses = nc;
17307
17308 switch (OMP_CLAUSE_CODE (nc))
17309 {
17310 case OMP_CLAUSE_LASTPRIVATE:
17311 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17312 {
17313 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17314 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17315 in_decl, /*integral_constant_expression_p=*/false);
17316 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17317 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17318 }
17319 /* FALLTHRU */
17320 case OMP_CLAUSE_PRIVATE:
17321 case OMP_CLAUSE_SHARED:
17322 case OMP_CLAUSE_FIRSTPRIVATE:
17323 case OMP_CLAUSE_COPYIN:
17324 case OMP_CLAUSE_COPYPRIVATE:
17325 case OMP_CLAUSE_UNIFORM:
17326 case OMP_CLAUSE_DEPEND:
17327 case OMP_CLAUSE_FROM:
17328 case OMP_CLAUSE_TO:
17329 case OMP_CLAUSE_MAP:
17330 case OMP_CLAUSE__CACHE_:
17331 case OMP_CLAUSE_NONTEMPORAL:
17332 case OMP_CLAUSE_USE_DEVICE_PTR:
17333 case OMP_CLAUSE_USE_DEVICE_ADDR:
17334 case OMP_CLAUSE_IS_DEVICE_PTR:
17335 case OMP_CLAUSE_INCLUSIVE:
17336 case OMP_CLAUSE_EXCLUSIVE:
17337 OMP_CLAUSE_DECL (nc)
17338 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17339 in_decl, iterator_cache);
17340 break;
17341 case OMP_CLAUSE_TILE:
17342 case OMP_CLAUSE_IF:
17343 case OMP_CLAUSE_NUM_THREADS:
17344 case OMP_CLAUSE_SCHEDULE:
17345 case OMP_CLAUSE_COLLAPSE:
17346 case OMP_CLAUSE_FINAL:
17347 case OMP_CLAUSE_DEVICE:
17348 case OMP_CLAUSE_DIST_SCHEDULE:
17349 case OMP_CLAUSE_NUM_TEAMS:
17350 case OMP_CLAUSE_THREAD_LIMIT:
17351 case OMP_CLAUSE_SAFELEN:
17352 case OMP_CLAUSE_SIMDLEN:
17353 case OMP_CLAUSE_NUM_TASKS:
17354 case OMP_CLAUSE_GRAINSIZE:
17355 case OMP_CLAUSE_PRIORITY:
17356 case OMP_CLAUSE_ORDERED:
17357 case OMP_CLAUSE_HINT:
17358 case OMP_CLAUSE_NUM_GANGS:
17359 case OMP_CLAUSE_NUM_WORKERS:
17360 case OMP_CLAUSE_VECTOR_LENGTH:
17361 case OMP_CLAUSE_WORKER:
17362 case OMP_CLAUSE_VECTOR:
17363 case OMP_CLAUSE_ASYNC:
17364 case OMP_CLAUSE_WAIT:
17365 case OMP_CLAUSE_DETACH:
17366 OMP_CLAUSE_OPERAND (nc, 0)
17367 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17368 in_decl, /*integral_constant_expression_p=*/false);
17369 break;
17370 case OMP_CLAUSE_REDUCTION:
17371 case OMP_CLAUSE_IN_REDUCTION:
17372 case OMP_CLAUSE_TASK_REDUCTION:
17373 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17374 {
17375 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17376 if (TREE_CODE (placeholder) == SCOPE_REF)
17377 {
17378 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17379 complain, in_decl);
17380 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17381 = build_qualified_name (NULL_TREE, scope,
17382 TREE_OPERAND (placeholder, 1),
17383 false);
17384 }
17385 else
17386 gcc_assert (identifier_p (placeholder));
17387 }
17388 OMP_CLAUSE_DECL (nc)
17389 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17390 in_decl, NULL);
17391 break;
17392 case OMP_CLAUSE_GANG:
17393 case OMP_CLAUSE_ALIGNED:
17394 case OMP_CLAUSE_ALLOCATE:
17395 OMP_CLAUSE_DECL (nc)
17396 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17397 in_decl, NULL);
17398 OMP_CLAUSE_OPERAND (nc, 1)
17399 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17400 in_decl, /*integral_constant_expression_p=*/false);
17401 break;
17402 case OMP_CLAUSE_LINEAR:
17403 OMP_CLAUSE_DECL (nc)
17404 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17405 in_decl, NULL);
17406 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17407 {
17408 gcc_assert (!linear_no_step);
17409 linear_no_step = nc;
17410 }
17411 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17412 OMP_CLAUSE_LINEAR_STEP (nc)
17413 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17414 complain, in_decl, NULL);
17415 else
17416 OMP_CLAUSE_LINEAR_STEP (nc)
17417 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17418 in_decl,
17419 /*integral_constant_expression_p=*/false);
17420 break;
17421 case OMP_CLAUSE_NOWAIT:
17422 case OMP_CLAUSE_DEFAULT:
17423 case OMP_CLAUSE_UNTIED:
17424 case OMP_CLAUSE_MERGEABLE:
17425 case OMP_CLAUSE_INBRANCH:
17426 case OMP_CLAUSE_NOTINBRANCH:
17427 case OMP_CLAUSE_PROC_BIND:
17428 case OMP_CLAUSE_FOR:
17429 case OMP_CLAUSE_PARALLEL:
17430 case OMP_CLAUSE_SECTIONS:
17431 case OMP_CLAUSE_TASKGROUP:
17432 case OMP_CLAUSE_NOGROUP:
17433 case OMP_CLAUSE_THREADS:
17434 case OMP_CLAUSE_SIMD:
17435 case OMP_CLAUSE_DEFAULTMAP:
17436 case OMP_CLAUSE_ORDER:
17437 case OMP_CLAUSE_BIND:
17438 case OMP_CLAUSE_INDEPENDENT:
17439 case OMP_CLAUSE_AUTO:
17440 case OMP_CLAUSE_SEQ:
17441 case OMP_CLAUSE_IF_PRESENT:
17442 case OMP_CLAUSE_FINALIZE:
17443 break;
17444 default:
17445 gcc_unreachable ();
17446 }
17447 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17448 switch (OMP_CLAUSE_CODE (nc))
17449 {
17450 case OMP_CLAUSE_SHARED:
17451 case OMP_CLAUSE_PRIVATE:
17452 case OMP_CLAUSE_FIRSTPRIVATE:
17453 case OMP_CLAUSE_LASTPRIVATE:
17454 case OMP_CLAUSE_COPYPRIVATE:
17455 case OMP_CLAUSE_LINEAR:
17456 case OMP_CLAUSE_REDUCTION:
17457 case OMP_CLAUSE_IN_REDUCTION:
17458 case OMP_CLAUSE_TASK_REDUCTION:
17459 case OMP_CLAUSE_USE_DEVICE_PTR:
17460 case OMP_CLAUSE_USE_DEVICE_ADDR:
17461 case OMP_CLAUSE_IS_DEVICE_PTR:
17462 case OMP_CLAUSE_INCLUSIVE:
17463 case OMP_CLAUSE_EXCLUSIVE:
17464 case OMP_CLAUSE_ALLOCATE:
17465 /* tsubst_expr on SCOPE_REF results in returning
17466 finish_non_static_data_member result. Undo that here. */
17467 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17468 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17469 == IDENTIFIER_NODE))
17470 {
17471 tree t = OMP_CLAUSE_DECL (nc);
17472 tree v = t;
17473 while (v)
17474 switch (TREE_CODE (v))
17475 {
17476 case COMPONENT_REF:
17477 case MEM_REF:
17478 case INDIRECT_REF:
17479 CASE_CONVERT:
17480 case POINTER_PLUS_EXPR:
17481 v = TREE_OPERAND (v, 0);
17482 continue;
17483 case PARM_DECL:
17484 if (DECL_CONTEXT (v) == current_function_decl
17485 && DECL_ARTIFICIAL (v)
17486 && DECL_NAME (v) == this_identifier)
17487 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17488 /* FALLTHRU */
17489 default:
17490 v = NULL_TREE;
17491 break;
17492 }
17493 }
17494 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17495 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17496 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17497 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17498 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17499 {
17500 tree decl = OMP_CLAUSE_DECL (nc);
17501 if (VAR_P (decl))
17502 {
17503 retrofit_lang_decl (decl);
17504 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17505 }
17506 }
17507 break;
17508 default:
17509 break;
17510 }
17511 }
17512
17513 new_clauses = nreverse (new_clauses);
17514 if (ort != C_ORT_OMP_DECLARE_SIMD)
17515 {
17516 new_clauses = finish_omp_clauses (new_clauses, ort);
17517 if (linear_no_step)
17518 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17519 if (nc == linear_no_step)
17520 {
17521 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17522 break;
17523 }
17524 }
17525 return new_clauses;
17526 }
17527
17528 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17529
17530 static tree
17531 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17532 tree in_decl)
17533 {
17534 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17535
17536 tree purpose, value, chain;
17537
17538 if (t == NULL)
17539 return t;
17540
17541 if (TREE_CODE (t) != TREE_LIST)
17542 return tsubst_copy_and_build (t, args, complain, in_decl,
17543 /*function_p=*/false,
17544 /*integral_constant_expression_p=*/false);
17545
17546 if (t == void_list_node)
17547 return t;
17548
17549 purpose = TREE_PURPOSE (t);
17550 if (purpose)
17551 purpose = RECUR (purpose);
17552 value = TREE_VALUE (t);
17553 if (value)
17554 {
17555 if (TREE_CODE (value) != LABEL_DECL)
17556 value = RECUR (value);
17557 else
17558 {
17559 value = lookup_label (DECL_NAME (value));
17560 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17561 TREE_USED (value) = 1;
17562 }
17563 }
17564 chain = TREE_CHAIN (t);
17565 if (chain && chain != void_type_node)
17566 chain = RECUR (chain);
17567 return tree_cons (purpose, value, chain);
17568 #undef RECUR
17569 }
17570
17571 /* Used to temporarily communicate the list of #pragma omp parallel
17572 clauses to #pragma omp for instantiation if they are combined
17573 together. */
17574
17575 static tree *omp_parallel_combined_clauses;
17576
17577 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17578 tree *, unsigned int *);
17579
17580 /* Substitute one OMP_FOR iterator. */
17581
17582 static bool
17583 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17584 tree initv, tree condv, tree incrv, tree *clauses,
17585 tree args, tsubst_flags_t complain, tree in_decl,
17586 bool integral_constant_expression_p)
17587 {
17588 #define RECUR(NODE) \
17589 tsubst_expr ((NODE), args, complain, in_decl, \
17590 integral_constant_expression_p)
17591 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17592 bool ret = false;
17593
17594 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17595 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17596
17597 decl = TREE_OPERAND (init, 0);
17598 init = TREE_OPERAND (init, 1);
17599 tree decl_expr = NULL_TREE;
17600 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17601 if (range_for)
17602 {
17603 bool decomp = false;
17604 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17605 {
17606 tree v = DECL_VALUE_EXPR (decl);
17607 if (TREE_CODE (v) == ARRAY_REF
17608 && VAR_P (TREE_OPERAND (v, 0))
17609 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17610 {
17611 tree decomp_first = NULL_TREE;
17612 unsigned decomp_cnt = 0;
17613 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17614 maybe_push_decl (d);
17615 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17616 in_decl, &decomp_first, &decomp_cnt);
17617 decomp = true;
17618 if (d == error_mark_node)
17619 decl = error_mark_node;
17620 else
17621 for (unsigned int i = 0; i < decomp_cnt; i++)
17622 {
17623 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17624 {
17625 tree v = build_nt (ARRAY_REF, d,
17626 size_int (decomp_cnt - i - 1),
17627 NULL_TREE, NULL_TREE);
17628 SET_DECL_VALUE_EXPR (decomp_first, v);
17629 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17630 }
17631 fit_decomposition_lang_decl (decomp_first, d);
17632 decomp_first = DECL_CHAIN (decomp_first);
17633 }
17634 }
17635 }
17636 decl = tsubst_decl (decl, args, complain);
17637 if (!decomp)
17638 maybe_push_decl (decl);
17639 }
17640 else if (init && TREE_CODE (init) == DECL_EXPR)
17641 {
17642 /* We need to jump through some hoops to handle declarations in the
17643 init-statement, since we might need to handle auto deduction,
17644 but we need to keep control of initialization. */
17645 decl_expr = init;
17646 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17647 decl = tsubst_decl (decl, args, complain);
17648 }
17649 else
17650 {
17651 if (TREE_CODE (decl) == SCOPE_REF)
17652 {
17653 decl = RECUR (decl);
17654 if (TREE_CODE (decl) == COMPONENT_REF)
17655 {
17656 tree v = decl;
17657 while (v)
17658 switch (TREE_CODE (v))
17659 {
17660 case COMPONENT_REF:
17661 case MEM_REF:
17662 case INDIRECT_REF:
17663 CASE_CONVERT:
17664 case POINTER_PLUS_EXPR:
17665 v = TREE_OPERAND (v, 0);
17666 continue;
17667 case PARM_DECL:
17668 if (DECL_CONTEXT (v) == current_function_decl
17669 && DECL_ARTIFICIAL (v)
17670 && DECL_NAME (v) == this_identifier)
17671 {
17672 decl = TREE_OPERAND (decl, 1);
17673 decl = omp_privatize_field (decl, false);
17674 }
17675 /* FALLTHRU */
17676 default:
17677 v = NULL_TREE;
17678 break;
17679 }
17680 }
17681 }
17682 else
17683 decl = RECUR (decl);
17684 }
17685 if (init && TREE_CODE (init) == TREE_VEC)
17686 {
17687 init = copy_node (init);
17688 TREE_VEC_ELT (init, 0)
17689 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17690 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17691 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17692 }
17693 else
17694 init = RECUR (init);
17695
17696 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17697 {
17698 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17699 if (TREE_CODE (o) == TREE_LIST)
17700 TREE_VEC_ELT (orig_declv, i)
17701 = tree_cons (RECUR (TREE_PURPOSE (o)),
17702 RECUR (TREE_VALUE (o)),
17703 NULL_TREE);
17704 else
17705 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17706 }
17707
17708 if (range_for)
17709 {
17710 tree this_pre_body = NULL_TREE;
17711 tree orig_init = NULL_TREE;
17712 tree orig_decl = NULL_TREE;
17713 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17714 orig_init, cond, incr);
17715 if (orig_decl)
17716 {
17717 if (orig_declv == NULL_TREE)
17718 orig_declv = copy_node (declv);
17719 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17720 ret = true;
17721 }
17722 else if (orig_declv)
17723 TREE_VEC_ELT (orig_declv, i) = decl;
17724 }
17725
17726 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17727 if (!range_for && auto_node && init)
17728 TREE_TYPE (decl)
17729 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17730
17731 gcc_assert (!type_dependent_expression_p (decl));
17732
17733 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17734 {
17735 if (decl_expr)
17736 {
17737 /* Declare the variable, but don't let that initialize it. */
17738 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17739 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17740 RECUR (decl_expr);
17741 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17742 }
17743
17744 if (!range_for)
17745 {
17746 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17747 if (COMPARISON_CLASS_P (cond)
17748 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17749 {
17750 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17751 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17752 TREE_VEC_ELT (rhs, 0)
17753 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17754 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17755 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17756 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17757 lhs, rhs);
17758 }
17759 else
17760 cond = RECUR (cond);
17761 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17762 if (TREE_CODE (incr) == MODIFY_EXPR)
17763 {
17764 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17765 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17766 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17767 NOP_EXPR, rhs, complain);
17768 }
17769 else
17770 incr = RECUR (incr);
17771 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17772 TREE_VEC_ELT (orig_declv, i) = decl;
17773 }
17774 TREE_VEC_ELT (declv, i) = decl;
17775 TREE_VEC_ELT (initv, i) = init;
17776 TREE_VEC_ELT (condv, i) = cond;
17777 TREE_VEC_ELT (incrv, i) = incr;
17778 return ret;
17779 }
17780
17781 if (decl_expr)
17782 {
17783 /* Declare and initialize the variable. */
17784 RECUR (decl_expr);
17785 init = NULL_TREE;
17786 }
17787 else if (init)
17788 {
17789 tree *pc;
17790 int j;
17791 for (j = ((omp_parallel_combined_clauses == NULL
17792 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17793 {
17794 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17795 {
17796 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17797 && OMP_CLAUSE_DECL (*pc) == decl)
17798 break;
17799 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17800 && OMP_CLAUSE_DECL (*pc) == decl)
17801 {
17802 if (j)
17803 break;
17804 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17805 tree c = *pc;
17806 *pc = OMP_CLAUSE_CHAIN (c);
17807 OMP_CLAUSE_CHAIN (c) = *clauses;
17808 *clauses = c;
17809 }
17810 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17811 && OMP_CLAUSE_DECL (*pc) == decl)
17812 {
17813 error ("iteration variable %qD should not be firstprivate",
17814 decl);
17815 *pc = OMP_CLAUSE_CHAIN (*pc);
17816 }
17817 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17818 && OMP_CLAUSE_DECL (*pc) == decl)
17819 {
17820 error ("iteration variable %qD should not be reduction",
17821 decl);
17822 *pc = OMP_CLAUSE_CHAIN (*pc);
17823 }
17824 else
17825 pc = &OMP_CLAUSE_CHAIN (*pc);
17826 }
17827 if (*pc)
17828 break;
17829 }
17830 if (*pc == NULL_TREE)
17831 {
17832 tree c = build_omp_clause (input_location,
17833 TREE_CODE (t) == OMP_LOOP
17834 ? OMP_CLAUSE_LASTPRIVATE
17835 : OMP_CLAUSE_PRIVATE);
17836 OMP_CLAUSE_DECL (c) = decl;
17837 c = finish_omp_clauses (c, C_ORT_OMP);
17838 if (c)
17839 {
17840 OMP_CLAUSE_CHAIN (c) = *clauses;
17841 *clauses = c;
17842 }
17843 }
17844 }
17845 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17846 if (COMPARISON_CLASS_P (cond))
17847 {
17848 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17849 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17850 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17851 }
17852 else
17853 cond = RECUR (cond);
17854 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17855 switch (TREE_CODE (incr))
17856 {
17857 case PREINCREMENT_EXPR:
17858 case PREDECREMENT_EXPR:
17859 case POSTINCREMENT_EXPR:
17860 case POSTDECREMENT_EXPR:
17861 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17862 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17863 break;
17864 case MODIFY_EXPR:
17865 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17866 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17867 {
17868 tree rhs = TREE_OPERAND (incr, 1);
17869 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17870 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17871 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17872 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17873 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17874 rhs0, rhs1));
17875 }
17876 else
17877 incr = RECUR (incr);
17878 break;
17879 case MODOP_EXPR:
17880 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17881 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17882 {
17883 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17884 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17885 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17886 TREE_TYPE (decl), lhs,
17887 RECUR (TREE_OPERAND (incr, 2))));
17888 }
17889 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17890 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17891 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17892 {
17893 tree rhs = TREE_OPERAND (incr, 2);
17894 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17895 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17896 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17897 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17898 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17899 rhs0, rhs1));
17900 }
17901 else
17902 incr = RECUR (incr);
17903 break;
17904 default:
17905 incr = RECUR (incr);
17906 break;
17907 }
17908
17909 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17910 TREE_VEC_ELT (orig_declv, i) = decl;
17911 TREE_VEC_ELT (declv, i) = decl;
17912 TREE_VEC_ELT (initv, i) = init;
17913 TREE_VEC_ELT (condv, i) = cond;
17914 TREE_VEC_ELT (incrv, i) = incr;
17915 return false;
17916 #undef RECUR
17917 }
17918
17919 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17920 of OMP_TARGET's body. */
17921
17922 static tree
17923 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17924 {
17925 *walk_subtrees = 0;
17926 switch (TREE_CODE (*tp))
17927 {
17928 case OMP_TEAMS:
17929 return *tp;
17930 case BIND_EXPR:
17931 case STATEMENT_LIST:
17932 *walk_subtrees = 1;
17933 break;
17934 default:
17935 break;
17936 }
17937 return NULL_TREE;
17938 }
17939
17940 /* Helper function for tsubst_expr. For decomposition declaration
17941 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17942 also the corresponding decls representing the identifiers
17943 of the decomposition declaration. Return DECL if successful
17944 or error_mark_node otherwise, set *FIRST to the first decl
17945 in the list chained through DECL_CHAIN and *CNT to the number
17946 of such decls. */
17947
17948 static tree
17949 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17950 tsubst_flags_t complain, tree in_decl, tree *first,
17951 unsigned int *cnt)
17952 {
17953 tree decl2, decl3, prev = decl;
17954 *cnt = 0;
17955 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17956 for (decl2 = DECL_CHAIN (pattern_decl);
17957 decl2
17958 && VAR_P (decl2)
17959 && DECL_DECOMPOSITION_P (decl2)
17960 && DECL_NAME (decl2);
17961 decl2 = DECL_CHAIN (decl2))
17962 {
17963 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17964 {
17965 gcc_assert (errorcount);
17966 return error_mark_node;
17967 }
17968 (*cnt)++;
17969 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17970 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17971 tree v = DECL_VALUE_EXPR (decl2);
17972 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17973 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17974 decl3 = tsubst (decl2, args, complain, in_decl);
17975 SET_DECL_VALUE_EXPR (decl2, v);
17976 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17977 if (VAR_P (decl3))
17978 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17979 else
17980 {
17981 gcc_assert (errorcount);
17982 decl = error_mark_node;
17983 continue;
17984 }
17985 maybe_push_decl (decl3);
17986 if (error_operand_p (decl3))
17987 decl = error_mark_node;
17988 else if (decl != error_mark_node
17989 && DECL_CHAIN (decl3) != prev
17990 && decl != prev)
17991 {
17992 gcc_assert (errorcount);
17993 decl = error_mark_node;
17994 }
17995 else
17996 prev = decl3;
17997 }
17998 *first = prev;
17999 return decl;
18000 }
18001
18002 /* Return the proper local_specialization for init-capture pack DECL. */
18003
18004 static tree
18005 lookup_init_capture_pack (tree decl)
18006 {
18007 /* We handle normal pack captures by forwarding to the specialization of the
18008 captured parameter. We can't do that for pack init-captures; we need them
18009 to have their own local_specialization. We created the individual
18010 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18011 when we process the DECL_EXPR for the pack init-capture in the template.
18012 So, how do we find them? We don't know the capture proxy pack when
18013 building the individual resulting proxies, and we don't know the
18014 individual proxies when instantiating the pack. What we have in common is
18015 the FIELD_DECL.
18016
18017 So...when we instantiate the FIELD_DECL, we stick the result in
18018 local_specializations. Then at the DECL_EXPR we look up that result, see
18019 how many elements it has, synthesize the names, and look them up. */
18020
18021 tree cname = DECL_NAME (decl);
18022 tree val = DECL_VALUE_EXPR (decl);
18023 tree field = TREE_OPERAND (val, 1);
18024 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18025 tree fpack = retrieve_local_specialization (field);
18026 if (fpack == error_mark_node)
18027 return error_mark_node;
18028
18029 int len = 1;
18030 tree vec = NULL_TREE;
18031 tree r = NULL_TREE;
18032 if (TREE_CODE (fpack) == TREE_VEC)
18033 {
18034 len = TREE_VEC_LENGTH (fpack);
18035 vec = make_tree_vec (len);
18036 r = make_node (NONTYPE_ARGUMENT_PACK);
18037 SET_ARGUMENT_PACK_ARGS (r, vec);
18038 }
18039 for (int i = 0; i < len; ++i)
18040 {
18041 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18042 tree elt = lookup_name (ename);
18043 if (vec)
18044 TREE_VEC_ELT (vec, i) = elt;
18045 else
18046 r = elt;
18047 }
18048 return r;
18049 }
18050
18051 /* Like tsubst_copy for expressions, etc. but also does semantic
18052 processing. */
18053
18054 tree
18055 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18056 bool integral_constant_expression_p)
18057 {
18058 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18059 #define RECUR(NODE) \
18060 tsubst_expr ((NODE), args, complain, in_decl, \
18061 integral_constant_expression_p)
18062
18063 tree stmt, tmp;
18064 tree r;
18065 location_t loc;
18066
18067 if (t == NULL_TREE || t == error_mark_node)
18068 return t;
18069
18070 loc = input_location;
18071 if (location_t eloc = cp_expr_location (t))
18072 input_location = eloc;
18073 if (STATEMENT_CODE_P (TREE_CODE (t)))
18074 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18075
18076 switch (TREE_CODE (t))
18077 {
18078 case STATEMENT_LIST:
18079 {
18080 tree_stmt_iterator i;
18081 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
18082 RECUR (tsi_stmt (i));
18083 break;
18084 }
18085
18086 case CTOR_INITIALIZER:
18087 finish_mem_initializers (tsubst_initializer_list
18088 (TREE_OPERAND (t, 0), args));
18089 break;
18090
18091 case RETURN_EXPR:
18092 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18093 break;
18094
18095 case CO_RETURN_EXPR:
18096 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18097 break;
18098
18099 case CO_YIELD_EXPR:
18100 stmt = finish_co_yield_expr (input_location,
18101 RECUR (TREE_OPERAND (t, 0)));
18102 RETURN (stmt);
18103 break;
18104
18105 case CO_AWAIT_EXPR:
18106 stmt = finish_co_await_expr (input_location,
18107 RECUR (TREE_OPERAND (t, 0)));
18108 RETURN (stmt);
18109 break;
18110
18111 case EXPR_STMT:
18112 tmp = RECUR (EXPR_STMT_EXPR (t));
18113 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18114 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18115 else
18116 finish_expr_stmt (tmp);
18117 break;
18118
18119 case USING_STMT:
18120 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18121 break;
18122
18123 case DECL_EXPR:
18124 {
18125 tree decl, pattern_decl;
18126 tree init;
18127
18128 pattern_decl = decl = DECL_EXPR_DECL (t);
18129 if (TREE_CODE (decl) == LABEL_DECL)
18130 finish_label_decl (DECL_NAME (decl));
18131 else if (TREE_CODE (decl) == USING_DECL)
18132 {
18133 /* We cannot have a member-using decl here (until 'using
18134 enum T' is a thing). */
18135 gcc_checking_assert (!DECL_DEPENDENT_P (decl));
18136
18137 /* This must be a non-dependent using-decl, and we'll have
18138 used the names it found during template parsing. We do
18139 not want to do the lookup again, because we might not
18140 find the things we found then. (Again, using enum T
18141 might mean we have to do things here.) */
18142 tree scope = USING_DECL_SCOPE (decl);
18143 gcc_checking_assert (scope
18144 == tsubst (scope, args, complain, in_decl));
18145 /* We still need to push the bindings so that we can look up
18146 this name later. */
18147 push_using_decl_bindings (DECL_NAME (decl),
18148 USING_DECL_DECLS (decl));
18149 }
18150 else if (is_capture_proxy (decl)
18151 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18152 {
18153 /* We're in tsubst_lambda_expr, we've already inserted a new
18154 capture proxy, so look it up and register it. */
18155 tree inst;
18156 if (!DECL_PACK_P (decl))
18157 {
18158 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18159 LOOK_want::HIDDEN_LAMBDA);
18160 gcc_assert (inst != decl && is_capture_proxy (inst));
18161 }
18162 else if (is_normal_capture_proxy (decl))
18163 {
18164 inst = (retrieve_local_specialization
18165 (DECL_CAPTURED_VARIABLE (decl)));
18166 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18167 || DECL_PACK_P (inst));
18168 }
18169 else
18170 inst = lookup_init_capture_pack (decl);
18171
18172 register_local_specialization (inst, decl);
18173 break;
18174 }
18175 else if (DECL_PRETTY_FUNCTION_P (decl))
18176 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18177 DECL_NAME (decl),
18178 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18179 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18180 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18181 /* Don't copy the old closure; we'll create a new one in
18182 tsubst_lambda_expr. */
18183 break;
18184 else
18185 {
18186 init = DECL_INITIAL (decl);
18187 decl = tsubst (decl, args, complain, in_decl);
18188 if (decl != error_mark_node)
18189 {
18190 /* By marking the declaration as instantiated, we avoid
18191 trying to instantiate it. Since instantiate_decl can't
18192 handle local variables, and since we've already done
18193 all that needs to be done, that's the right thing to
18194 do. */
18195 if (VAR_P (decl))
18196 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18197 if (VAR_P (decl) && !DECL_NAME (decl)
18198 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18199 /* Anonymous aggregates are a special case. */
18200 finish_anon_union (decl);
18201 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18202 {
18203 DECL_CONTEXT (decl) = current_function_decl;
18204 if (DECL_NAME (decl) == this_identifier)
18205 {
18206 tree lam = DECL_CONTEXT (current_function_decl);
18207 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18208 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18209 }
18210 insert_capture_proxy (decl);
18211 }
18212 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18213 /* We already did a pushtag. */;
18214 else if (VAR_OR_FUNCTION_DECL_P (decl)
18215 && DECL_LOCAL_DECL_P (decl))
18216 {
18217 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18218 DECL_CONTEXT (decl) = NULL_TREE;
18219 decl = pushdecl (decl);
18220 if (TREE_CODE (decl) == FUNCTION_DECL
18221 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18222 && cp_check_omp_declare_reduction (decl))
18223 instantiate_body (pattern_decl, args, decl, true);
18224 }
18225 else
18226 {
18227 bool const_init = false;
18228 unsigned int cnt = 0;
18229 tree first = NULL_TREE, ndecl = error_mark_node;
18230 maybe_push_decl (decl);
18231
18232 if (VAR_P (decl)
18233 && DECL_DECOMPOSITION_P (decl)
18234 && TREE_TYPE (pattern_decl) != error_mark_node)
18235 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18236 complain, in_decl, &first,
18237 &cnt);
18238
18239 init = tsubst_init (init, decl, args, complain, in_decl);
18240
18241 if (VAR_P (decl))
18242 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18243 (pattern_decl));
18244
18245 if (ndecl != error_mark_node)
18246 cp_maybe_mangle_decomp (ndecl, first, cnt);
18247
18248 /* In a non-template function, VLA type declarations are
18249 handled in grokdeclarator; for templates, handle them
18250 now. */
18251 predeclare_vla (decl);
18252
18253 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18254
18255 if (ndecl != error_mark_node)
18256 cp_finish_decomp (ndecl, first, cnt);
18257 }
18258 }
18259 }
18260
18261 break;
18262 }
18263
18264 case FOR_STMT:
18265 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18266 RECUR (FOR_INIT_STMT (t));
18267 finish_init_stmt (stmt);
18268 tmp = RECUR (FOR_COND (t));
18269 finish_for_cond (tmp, stmt, false, 0);
18270 tmp = RECUR (FOR_EXPR (t));
18271 finish_for_expr (tmp, stmt);
18272 {
18273 bool prev = note_iteration_stmt_body_start ();
18274 RECUR (FOR_BODY (t));
18275 note_iteration_stmt_body_end (prev);
18276 }
18277 finish_for_stmt (stmt);
18278 break;
18279
18280 case RANGE_FOR_STMT:
18281 {
18282 /* Construct another range_for, if this is not a final
18283 substitution (for inside a generic lambda of a
18284 template). Otherwise convert to a regular for. */
18285 tree decl, expr;
18286 stmt = (processing_template_decl
18287 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18288 : begin_for_stmt (NULL_TREE, NULL_TREE));
18289 RECUR (RANGE_FOR_INIT_STMT (t));
18290 decl = RANGE_FOR_DECL (t);
18291 decl = tsubst (decl, args, complain, in_decl);
18292 maybe_push_decl (decl);
18293 expr = RECUR (RANGE_FOR_EXPR (t));
18294
18295 tree decomp_first = NULL_TREE;
18296 unsigned decomp_cnt = 0;
18297 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18298 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18299 complain, in_decl,
18300 &decomp_first, &decomp_cnt);
18301
18302 if (processing_template_decl)
18303 {
18304 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18305 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18306 finish_range_for_decl (stmt, decl, expr);
18307 if (decomp_first && decl != error_mark_node)
18308 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18309 }
18310 else
18311 {
18312 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18313 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18314 stmt = cp_convert_range_for (stmt, decl, expr,
18315 decomp_first, decomp_cnt,
18316 RANGE_FOR_IVDEP (t), unroll);
18317 }
18318
18319 bool prev = note_iteration_stmt_body_start ();
18320 RECUR (RANGE_FOR_BODY (t));
18321 note_iteration_stmt_body_end (prev);
18322 finish_for_stmt (stmt);
18323 }
18324 break;
18325
18326 case WHILE_STMT:
18327 stmt = begin_while_stmt ();
18328 tmp = RECUR (WHILE_COND (t));
18329 finish_while_stmt_cond (tmp, stmt, false, 0);
18330 {
18331 bool prev = note_iteration_stmt_body_start ();
18332 RECUR (WHILE_BODY (t));
18333 note_iteration_stmt_body_end (prev);
18334 }
18335 finish_while_stmt (stmt);
18336 break;
18337
18338 case DO_STMT:
18339 stmt = begin_do_stmt ();
18340 {
18341 bool prev = note_iteration_stmt_body_start ();
18342 RECUR (DO_BODY (t));
18343 note_iteration_stmt_body_end (prev);
18344 }
18345 finish_do_body (stmt);
18346 tmp = RECUR (DO_COND (t));
18347 finish_do_stmt (tmp, stmt, false, 0);
18348 break;
18349
18350 case IF_STMT:
18351 stmt = begin_if_stmt ();
18352 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18353 if (IF_STMT_CONSTEXPR_P (t))
18354 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18355 tmp = RECUR (IF_COND (t));
18356 tmp = finish_if_stmt_cond (tmp, stmt);
18357 if (IF_STMT_CONSTEXPR_P (t)
18358 && instantiation_dependent_expression_p (tmp))
18359 {
18360 /* We're partially instantiating a generic lambda, but the condition
18361 of the constexpr if is still dependent. Don't substitute into the
18362 branches now, just remember the template arguments. */
18363 do_poplevel (IF_SCOPE (stmt));
18364 IF_COND (stmt) = IF_COND (t);
18365 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18366 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18367 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18368 add_stmt (stmt);
18369 break;
18370 }
18371 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18372 /* Don't instantiate the THEN_CLAUSE. */;
18373 else
18374 {
18375 tree folded = fold_non_dependent_expr (tmp, complain);
18376 bool inhibit = integer_zerop (folded);
18377 if (inhibit)
18378 ++c_inhibit_evaluation_warnings;
18379 RECUR (THEN_CLAUSE (t));
18380 if (inhibit)
18381 --c_inhibit_evaluation_warnings;
18382 }
18383 finish_then_clause (stmt);
18384
18385 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18386 /* Don't instantiate the ELSE_CLAUSE. */;
18387 else if (ELSE_CLAUSE (t))
18388 {
18389 tree folded = fold_non_dependent_expr (tmp, complain);
18390 bool inhibit = integer_nonzerop (folded);
18391 begin_else_clause (stmt);
18392 if (inhibit)
18393 ++c_inhibit_evaluation_warnings;
18394 RECUR (ELSE_CLAUSE (t));
18395 if (inhibit)
18396 --c_inhibit_evaluation_warnings;
18397 finish_else_clause (stmt);
18398 }
18399
18400 finish_if_stmt (stmt);
18401 break;
18402
18403 case BIND_EXPR:
18404 if (BIND_EXPR_BODY_BLOCK (t))
18405 stmt = begin_function_body ();
18406 else
18407 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18408 ? BCS_TRY_BLOCK : 0);
18409
18410 RECUR (BIND_EXPR_BODY (t));
18411
18412 if (BIND_EXPR_BODY_BLOCK (t))
18413 finish_function_body (stmt);
18414 else
18415 finish_compound_stmt (stmt);
18416 break;
18417
18418 case BREAK_STMT:
18419 finish_break_stmt ();
18420 break;
18421
18422 case CONTINUE_STMT:
18423 finish_continue_stmt ();
18424 break;
18425
18426 case SWITCH_STMT:
18427 stmt = begin_switch_stmt ();
18428 tmp = RECUR (SWITCH_STMT_COND (t));
18429 finish_switch_cond (tmp, stmt);
18430 RECUR (SWITCH_STMT_BODY (t));
18431 finish_switch_stmt (stmt);
18432 break;
18433
18434 case CASE_LABEL_EXPR:
18435 {
18436 tree decl = CASE_LABEL (t);
18437 tree low = RECUR (CASE_LOW (t));
18438 tree high = RECUR (CASE_HIGH (t));
18439 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18440 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18441 {
18442 tree label = CASE_LABEL (l);
18443 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18444 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18445 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18446 }
18447 }
18448 break;
18449
18450 case LABEL_EXPR:
18451 {
18452 tree decl = LABEL_EXPR_LABEL (t);
18453 tree label;
18454
18455 label = finish_label_stmt (DECL_NAME (decl));
18456 if (TREE_CODE (label) == LABEL_DECL)
18457 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18458 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18459 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18460 }
18461 break;
18462
18463 case GOTO_EXPR:
18464 tmp = GOTO_DESTINATION (t);
18465 if (TREE_CODE (tmp) != LABEL_DECL)
18466 /* Computed goto's must be tsubst'd into. On the other hand,
18467 non-computed gotos must not be; the identifier in question
18468 will have no binding. */
18469 tmp = RECUR (tmp);
18470 else
18471 tmp = DECL_NAME (tmp);
18472 finish_goto_stmt (tmp);
18473 break;
18474
18475 case ASM_EXPR:
18476 {
18477 tree string = RECUR (ASM_STRING (t));
18478 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18479 complain, in_decl);
18480 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18481 complain, in_decl);
18482 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18483 complain, in_decl);
18484 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18485 complain, in_decl);
18486 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18487 outputs, inputs, clobbers, labels,
18488 ASM_INLINE_P (t));
18489 tree asm_expr = tmp;
18490 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18491 asm_expr = TREE_OPERAND (asm_expr, 0);
18492 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18493 }
18494 break;
18495
18496 case TRY_BLOCK:
18497 if (CLEANUP_P (t))
18498 {
18499 stmt = begin_try_block ();
18500 RECUR (TRY_STMTS (t));
18501 finish_cleanup_try_block (stmt);
18502 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18503 }
18504 else
18505 {
18506 tree compound_stmt = NULL_TREE;
18507
18508 if (FN_TRY_BLOCK_P (t))
18509 stmt = begin_function_try_block (&compound_stmt);
18510 else
18511 stmt = begin_try_block ();
18512
18513 RECUR (TRY_STMTS (t));
18514
18515 if (FN_TRY_BLOCK_P (t))
18516 finish_function_try_block (stmt);
18517 else
18518 finish_try_block (stmt);
18519
18520 RECUR (TRY_HANDLERS (t));
18521 if (FN_TRY_BLOCK_P (t))
18522 finish_function_handler_sequence (stmt, compound_stmt);
18523 else
18524 finish_handler_sequence (stmt);
18525 }
18526 break;
18527
18528 case HANDLER:
18529 {
18530 tree decl = HANDLER_PARMS (t);
18531
18532 if (decl)
18533 {
18534 decl = tsubst (decl, args, complain, in_decl);
18535 /* Prevent instantiate_decl from trying to instantiate
18536 this variable. We've already done all that needs to be
18537 done. */
18538 if (decl != error_mark_node)
18539 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18540 }
18541 stmt = begin_handler ();
18542 finish_handler_parms (decl, stmt);
18543 RECUR (HANDLER_BODY (t));
18544 finish_handler (stmt);
18545 }
18546 break;
18547
18548 case TAG_DEFN:
18549 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18550 if (CLASS_TYPE_P (tmp))
18551 {
18552 /* Local classes are not independent templates; they are
18553 instantiated along with their containing function. And this
18554 way we don't have to deal with pushing out of one local class
18555 to instantiate a member of another local class. */
18556 /* Closures are handled by the LAMBDA_EXPR. */
18557 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18558 complete_type (tmp);
18559 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18560 if ((VAR_P (fld)
18561 || (TREE_CODE (fld) == FUNCTION_DECL
18562 && !DECL_ARTIFICIAL (fld)))
18563 && DECL_TEMPLATE_INSTANTIATION (fld))
18564 instantiate_decl (fld, /*defer_ok=*/false,
18565 /*expl_inst_class=*/false);
18566 }
18567 break;
18568
18569 case STATIC_ASSERT:
18570 {
18571 tree condition;
18572
18573 ++c_inhibit_evaluation_warnings;
18574 condition =
18575 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18576 args,
18577 complain, in_decl,
18578 /*integral_constant_expression_p=*/true);
18579 --c_inhibit_evaluation_warnings;
18580
18581 finish_static_assert (condition,
18582 STATIC_ASSERT_MESSAGE (t),
18583 STATIC_ASSERT_SOURCE_LOCATION (t),
18584 /*member_p=*/false, /*show_expr_p=*/true);
18585 }
18586 break;
18587
18588 case OACC_KERNELS:
18589 case OACC_PARALLEL:
18590 case OACC_SERIAL:
18591 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18592 in_decl);
18593 stmt = begin_omp_parallel ();
18594 RECUR (OMP_BODY (t));
18595 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18596 break;
18597
18598 case OMP_PARALLEL:
18599 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18600 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18601 complain, in_decl);
18602 if (OMP_PARALLEL_COMBINED (t))
18603 omp_parallel_combined_clauses = &tmp;
18604 stmt = begin_omp_parallel ();
18605 RECUR (OMP_PARALLEL_BODY (t));
18606 gcc_assert (omp_parallel_combined_clauses == NULL);
18607 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18608 = OMP_PARALLEL_COMBINED (t);
18609 pop_omp_privatization_clauses (r);
18610 break;
18611
18612 case OMP_TASK:
18613 if (OMP_TASK_BODY (t) == NULL_TREE)
18614 {
18615 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18616 complain, in_decl);
18617 t = copy_node (t);
18618 OMP_TASK_CLAUSES (t) = tmp;
18619 add_stmt (t);
18620 break;
18621 }
18622 r = push_omp_privatization_clauses (false);
18623 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18624 complain, in_decl);
18625 stmt = begin_omp_task ();
18626 RECUR (OMP_TASK_BODY (t));
18627 finish_omp_task (tmp, stmt);
18628 pop_omp_privatization_clauses (r);
18629 break;
18630
18631 case OMP_FOR:
18632 case OMP_LOOP:
18633 case OMP_SIMD:
18634 case OMP_DISTRIBUTE:
18635 case OMP_TASKLOOP:
18636 case OACC_LOOP:
18637 {
18638 tree clauses, body, pre_body;
18639 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18640 tree orig_declv = NULL_TREE;
18641 tree incrv = NULL_TREE;
18642 enum c_omp_region_type ort = C_ORT_OMP;
18643 bool any_range_for = false;
18644 int i;
18645
18646 if (TREE_CODE (t) == OACC_LOOP)
18647 ort = C_ORT_ACC;
18648
18649 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18650 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18651 in_decl);
18652 if (OMP_FOR_INIT (t) != NULL_TREE)
18653 {
18654 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18655 if (OMP_FOR_ORIG_DECLS (t))
18656 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18657 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18658 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18659 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18660 }
18661
18662 keep_next_level (true);
18663 stmt = begin_omp_structured_block ();
18664
18665 pre_body = push_stmt_list ();
18666 RECUR (OMP_FOR_PRE_BODY (t));
18667 pre_body = pop_stmt_list (pre_body);
18668
18669 if (OMP_FOR_INIT (t) != NULL_TREE)
18670 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18671 any_range_for
18672 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18673 condv, incrv, &clauses, args,
18674 complain, in_decl,
18675 integral_constant_expression_p);
18676 omp_parallel_combined_clauses = NULL;
18677
18678 if (any_range_for)
18679 {
18680 gcc_assert (orig_declv);
18681 body = begin_omp_structured_block ();
18682 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18683 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18684 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18685 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18686 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18687 TREE_VEC_ELT (declv, i));
18688 }
18689 else
18690 body = push_stmt_list ();
18691 RECUR (OMP_FOR_BODY (t));
18692 if (any_range_for)
18693 body = finish_omp_structured_block (body);
18694 else
18695 body = pop_stmt_list (body);
18696
18697 if (OMP_FOR_INIT (t) != NULL_TREE)
18698 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18699 orig_declv, initv, condv, incrv, body, pre_body,
18700 NULL, clauses);
18701 else
18702 {
18703 t = make_node (TREE_CODE (t));
18704 TREE_TYPE (t) = void_type_node;
18705 OMP_FOR_BODY (t) = body;
18706 OMP_FOR_PRE_BODY (t) = pre_body;
18707 OMP_FOR_CLAUSES (t) = clauses;
18708 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18709 add_stmt (t);
18710 }
18711
18712 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18713 t));
18714 pop_omp_privatization_clauses (r);
18715 }
18716 break;
18717
18718 case OMP_SECTIONS:
18719 omp_parallel_combined_clauses = NULL;
18720 /* FALLTHRU */
18721 case OMP_SINGLE:
18722 case OMP_TEAMS:
18723 case OMP_CRITICAL:
18724 case OMP_TASKGROUP:
18725 case OMP_SCAN:
18726 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18727 && OMP_TEAMS_COMBINED (t));
18728 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18729 in_decl);
18730 if (TREE_CODE (t) == OMP_TEAMS)
18731 {
18732 keep_next_level (true);
18733 stmt = begin_omp_structured_block ();
18734 RECUR (OMP_BODY (t));
18735 stmt = finish_omp_structured_block (stmt);
18736 }
18737 else
18738 {
18739 stmt = push_stmt_list ();
18740 RECUR (OMP_BODY (t));
18741 stmt = pop_stmt_list (stmt);
18742 }
18743
18744 if (TREE_CODE (t) == OMP_CRITICAL
18745 && tmp != NULL_TREE
18746 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18747 {
18748 error_at (OMP_CLAUSE_LOCATION (tmp),
18749 "%<#pragma omp critical%> with %<hint%> clause requires "
18750 "a name, except when %<omp_sync_hint_none%> is used");
18751 RETURN (error_mark_node);
18752 }
18753 t = copy_node (t);
18754 OMP_BODY (t) = stmt;
18755 OMP_CLAUSES (t) = tmp;
18756 add_stmt (t);
18757 pop_omp_privatization_clauses (r);
18758 break;
18759
18760 case OMP_DEPOBJ:
18761 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18762 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18763 {
18764 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18765 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18766 {
18767 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18768 args, complain, in_decl);
18769 if (tmp == NULL_TREE)
18770 tmp = error_mark_node;
18771 }
18772 else
18773 {
18774 kind = (enum omp_clause_depend_kind)
18775 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18776 tmp = NULL_TREE;
18777 }
18778 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18779 }
18780 else
18781 finish_omp_depobj (EXPR_LOCATION (t), r,
18782 OMP_CLAUSE_DEPEND_SOURCE,
18783 OMP_DEPOBJ_CLAUSES (t));
18784 break;
18785
18786 case OACC_DATA:
18787 case OMP_TARGET_DATA:
18788 case OMP_TARGET:
18789 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18790 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18791 in_decl);
18792 keep_next_level (true);
18793 stmt = begin_omp_structured_block ();
18794
18795 RECUR (OMP_BODY (t));
18796 stmt = finish_omp_structured_block (stmt);
18797
18798 t = copy_node (t);
18799 OMP_BODY (t) = stmt;
18800 OMP_CLAUSES (t) = tmp;
18801 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18802 {
18803 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18804 if (teams)
18805 {
18806 /* For combined target teams, ensure the num_teams and
18807 thread_limit clause expressions are evaluated on the host,
18808 before entering the target construct. */
18809 tree c;
18810 for (c = OMP_TEAMS_CLAUSES (teams);
18811 c; c = OMP_CLAUSE_CHAIN (c))
18812 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18813 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18814 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18815 {
18816 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18817 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18818 if (expr == error_mark_node)
18819 continue;
18820 tmp = TARGET_EXPR_SLOT (expr);
18821 add_stmt (expr);
18822 OMP_CLAUSE_OPERAND (c, 0) = expr;
18823 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18824 OMP_CLAUSE_FIRSTPRIVATE);
18825 OMP_CLAUSE_DECL (tc) = tmp;
18826 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18827 OMP_TARGET_CLAUSES (t) = tc;
18828 }
18829 }
18830 }
18831 add_stmt (t);
18832 break;
18833
18834 case OACC_DECLARE:
18835 t = copy_node (t);
18836 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18837 complain, in_decl);
18838 OACC_DECLARE_CLAUSES (t) = tmp;
18839 add_stmt (t);
18840 break;
18841
18842 case OMP_TARGET_UPDATE:
18843 case OMP_TARGET_ENTER_DATA:
18844 case OMP_TARGET_EXIT_DATA:
18845 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18846 complain, in_decl);
18847 t = copy_node (t);
18848 OMP_STANDALONE_CLAUSES (t) = tmp;
18849 add_stmt (t);
18850 break;
18851
18852 case OACC_CACHE:
18853 case OACC_ENTER_DATA:
18854 case OACC_EXIT_DATA:
18855 case OACC_UPDATE:
18856 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18857 complain, in_decl);
18858 t = copy_node (t);
18859 OMP_STANDALONE_CLAUSES (t) = tmp;
18860 add_stmt (t);
18861 break;
18862
18863 case OMP_ORDERED:
18864 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18865 complain, in_decl);
18866 stmt = push_stmt_list ();
18867 RECUR (OMP_BODY (t));
18868 stmt = pop_stmt_list (stmt);
18869
18870 t = copy_node (t);
18871 OMP_BODY (t) = stmt;
18872 OMP_ORDERED_CLAUSES (t) = tmp;
18873 add_stmt (t);
18874 break;
18875
18876 case OMP_MASTER:
18877 omp_parallel_combined_clauses = NULL;
18878 /* FALLTHRU */
18879 case OMP_SECTION:
18880 stmt = push_stmt_list ();
18881 RECUR (OMP_BODY (t));
18882 stmt = pop_stmt_list (stmt);
18883
18884 t = copy_node (t);
18885 OMP_BODY (t) = stmt;
18886 add_stmt (t);
18887 break;
18888
18889 case OMP_ATOMIC:
18890 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18891 tmp = NULL_TREE;
18892 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18893 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18894 complain, in_decl);
18895 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18896 {
18897 tree op1 = TREE_OPERAND (t, 1);
18898 tree rhs1 = NULL_TREE;
18899 tree lhs, rhs;
18900 if (TREE_CODE (op1) == COMPOUND_EXPR)
18901 {
18902 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18903 op1 = TREE_OPERAND (op1, 1);
18904 }
18905 lhs = RECUR (TREE_OPERAND (op1, 0));
18906 rhs = RECUR (TREE_OPERAND (op1, 1));
18907 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18908 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18909 OMP_ATOMIC_MEMORY_ORDER (t));
18910 }
18911 else
18912 {
18913 tree op1 = TREE_OPERAND (t, 1);
18914 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18915 tree rhs1 = NULL_TREE;
18916 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18917 enum tree_code opcode = NOP_EXPR;
18918 if (code == OMP_ATOMIC_READ)
18919 {
18920 v = RECUR (TREE_OPERAND (op1, 0));
18921 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18922 }
18923 else if (code == OMP_ATOMIC_CAPTURE_OLD
18924 || code == OMP_ATOMIC_CAPTURE_NEW)
18925 {
18926 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18927 v = RECUR (TREE_OPERAND (op1, 0));
18928 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18929 if (TREE_CODE (op11) == COMPOUND_EXPR)
18930 {
18931 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18932 op11 = TREE_OPERAND (op11, 1);
18933 }
18934 lhs = RECUR (TREE_OPERAND (op11, 0));
18935 rhs = RECUR (TREE_OPERAND (op11, 1));
18936 opcode = TREE_CODE (op11);
18937 if (opcode == MODIFY_EXPR)
18938 opcode = NOP_EXPR;
18939 }
18940 else
18941 {
18942 code = OMP_ATOMIC;
18943 lhs = RECUR (TREE_OPERAND (op1, 0));
18944 rhs = RECUR (TREE_OPERAND (op1, 1));
18945 }
18946 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18947 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18948 }
18949 break;
18950
18951 case TRANSACTION_EXPR:
18952 {
18953 int flags = 0;
18954 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18955 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18956
18957 if (TRANSACTION_EXPR_IS_STMT (t))
18958 {
18959 tree body = TRANSACTION_EXPR_BODY (t);
18960 tree noex = NULL_TREE;
18961 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18962 {
18963 noex = MUST_NOT_THROW_COND (body);
18964 if (noex == NULL_TREE)
18965 noex = boolean_true_node;
18966 body = TREE_OPERAND (body, 0);
18967 }
18968 stmt = begin_transaction_stmt (input_location, NULL, flags);
18969 RECUR (body);
18970 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18971 }
18972 else
18973 {
18974 stmt = build_transaction_expr (EXPR_LOCATION (t),
18975 RECUR (TRANSACTION_EXPR_BODY (t)),
18976 flags, NULL_TREE);
18977 RETURN (stmt);
18978 }
18979 }
18980 break;
18981
18982 case MUST_NOT_THROW_EXPR:
18983 {
18984 tree op0 = RECUR (TREE_OPERAND (t, 0));
18985 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18986 RETURN (build_must_not_throw_expr (op0, cond));
18987 }
18988
18989 case EXPR_PACK_EXPANSION:
18990 error ("invalid use of pack expansion expression");
18991 RETURN (error_mark_node);
18992
18993 case NONTYPE_ARGUMENT_PACK:
18994 error ("use %<...%> to expand argument pack");
18995 RETURN (error_mark_node);
18996
18997 case COMPOUND_EXPR:
18998 tmp = RECUR (TREE_OPERAND (t, 0));
18999 if (tmp == NULL_TREE)
19000 /* If the first operand was a statement, we're done with it. */
19001 RETURN (RECUR (TREE_OPERAND (t, 1)));
19002 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19003 RECUR (TREE_OPERAND (t, 1)),
19004 complain));
19005
19006 case ANNOTATE_EXPR:
19007 tmp = RECUR (TREE_OPERAND (t, 0));
19008 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19009 TREE_TYPE (tmp), tmp,
19010 RECUR (TREE_OPERAND (t, 1)),
19011 RECUR (TREE_OPERAND (t, 2))));
19012
19013 case PREDICT_EXPR:
19014 RETURN (add_stmt (copy_node (t)));
19015
19016 default:
19017 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19018
19019 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19020 /*function_p=*/false,
19021 integral_constant_expression_p));
19022 }
19023
19024 RETURN (NULL_TREE);
19025 out:
19026 input_location = loc;
19027 return r;
19028 #undef RECUR
19029 #undef RETURN
19030 }
19031
19032 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19033 function. For description of the body see comment above
19034 cp_parser_omp_declare_reduction_exprs. */
19035
19036 static void
19037 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19038 {
19039 if (t == NULL_TREE || t == error_mark_node)
19040 return;
19041
19042 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19043
19044 tree_stmt_iterator tsi;
19045 int i;
19046 tree stmts[7];
19047 memset (stmts, 0, sizeof stmts);
19048 for (i = 0, tsi = tsi_start (t);
19049 i < 7 && !tsi_end_p (tsi);
19050 i++, tsi_next (&tsi))
19051 stmts[i] = tsi_stmt (tsi);
19052 gcc_assert (tsi_end_p (tsi));
19053
19054 if (i >= 3)
19055 {
19056 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19057 && TREE_CODE (stmts[1]) == DECL_EXPR);
19058 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19059 args, complain, in_decl);
19060 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19061 args, complain, in_decl);
19062 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19063 expect to be pushing it. */
19064 DECL_CONTEXT (omp_out) = current_function_decl;
19065 DECL_CONTEXT (omp_in) = current_function_decl;
19066 keep_next_level (true);
19067 tree block = begin_omp_structured_block ();
19068 tsubst_expr (stmts[2], args, complain, in_decl, false);
19069 block = finish_omp_structured_block (block);
19070 block = maybe_cleanup_point_expr_void (block);
19071 add_decl_expr (omp_out);
19072 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
19073 TREE_NO_WARNING (omp_out) = 1;
19074 add_decl_expr (omp_in);
19075 finish_expr_stmt (block);
19076 }
19077 if (i >= 6)
19078 {
19079 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19080 && TREE_CODE (stmts[4]) == DECL_EXPR);
19081 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19082 args, complain, in_decl);
19083 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19084 args, complain, in_decl);
19085 DECL_CONTEXT (omp_priv) = current_function_decl;
19086 DECL_CONTEXT (omp_orig) = current_function_decl;
19087 keep_next_level (true);
19088 tree block = begin_omp_structured_block ();
19089 tsubst_expr (stmts[5], args, complain, in_decl, false);
19090 block = finish_omp_structured_block (block);
19091 block = maybe_cleanup_point_expr_void (block);
19092 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19093 add_decl_expr (omp_priv);
19094 add_decl_expr (omp_orig);
19095 finish_expr_stmt (block);
19096 if (i == 7)
19097 add_decl_expr (omp_orig);
19098 }
19099 }
19100
19101 /* T is a postfix-expression that is not being used in a function
19102 call. Return the substituted version of T. */
19103
19104 static tree
19105 tsubst_non_call_postfix_expression (tree t, tree args,
19106 tsubst_flags_t complain,
19107 tree in_decl)
19108 {
19109 if (TREE_CODE (t) == SCOPE_REF)
19110 t = tsubst_qualified_id (t, args, complain, in_decl,
19111 /*done=*/false, /*address_p=*/false);
19112 else
19113 t = tsubst_copy_and_build (t, args, complain, in_decl,
19114 /*function_p=*/false,
19115 /*integral_constant_expression_p=*/false);
19116
19117 return t;
19118 }
19119
19120 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19121 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19122 dependent init-capture. */
19123
19124 static void
19125 prepend_one_capture (tree field, tree init, tree &list,
19126 tsubst_flags_t complain)
19127 {
19128 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19129 {
19130 tree type = NULL_TREE;
19131 if (!init)
19132 {
19133 if (complain & tf_error)
19134 error ("empty initializer in lambda init-capture");
19135 init = error_mark_node;
19136 }
19137 else if (TREE_CODE (init) == TREE_LIST)
19138 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19139 if (!type)
19140 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19141 TREE_TYPE (field) = type;
19142 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19143 }
19144 list = tree_cons (field, init, list);
19145 }
19146
19147 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19148 instantiation context. Instantiating a pack expansion containing a lambda
19149 might result in multiple lambdas all based on the same lambda in the
19150 template. */
19151
19152 tree
19153 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19154 {
19155 tree oldfn = lambda_function (t);
19156 in_decl = oldfn;
19157
19158 tree r = build_lambda_expr ();
19159
19160 LAMBDA_EXPR_LOCATION (r)
19161 = LAMBDA_EXPR_LOCATION (t);
19162 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19163 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19164 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19165 LAMBDA_EXPR_INSTANTIATED (r) = true;
19166
19167 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19168 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19169
19170 vec<tree,va_gc>* field_packs = NULL;
19171
19172 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19173 cap = TREE_CHAIN (cap))
19174 {
19175 tree ofield = TREE_PURPOSE (cap);
19176 tree init = TREE_VALUE (cap);
19177 if (PACK_EXPANSION_P (init))
19178 init = tsubst_pack_expansion (init, args, complain, in_decl);
19179 else
19180 init = tsubst_copy_and_build (init, args, complain, in_decl,
19181 /*fn*/false, /*constexpr*/false);
19182
19183 if (init == error_mark_node)
19184 return error_mark_node;
19185
19186 if (init && TREE_CODE (init) == TREE_LIST)
19187 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19188
19189 if (!processing_template_decl
19190 && init && TREE_CODE (init) != TREE_VEC
19191 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19192 {
19193 /* For a VLA, simply tsubsting the field type won't work, we need to
19194 go through add_capture again. XXX do we want to do this for all
19195 captures? */
19196 tree name = (get_identifier
19197 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19198 tree ftype = TREE_TYPE (ofield);
19199 bool by_ref = (TYPE_REF_P (ftype)
19200 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19201 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19202 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19203 continue;
19204 }
19205
19206 if (PACK_EXPANSION_P (ofield))
19207 ofield = PACK_EXPANSION_PATTERN (ofield);
19208 tree field = tsubst_decl (ofield, args, complain);
19209
19210 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19211 {
19212 /* Remember these for when we've pushed local_specializations. */
19213 vec_safe_push (field_packs, ofield);
19214 vec_safe_push (field_packs, field);
19215 }
19216
19217 if (field == error_mark_node)
19218 return error_mark_node;
19219
19220 if (TREE_CODE (field) == TREE_VEC)
19221 {
19222 int len = TREE_VEC_LENGTH (field);
19223 gcc_assert (TREE_CODE (init) == TREE_VEC
19224 && TREE_VEC_LENGTH (init) == len);
19225 for (int i = 0; i < len; ++i)
19226 prepend_one_capture (TREE_VEC_ELT (field, i),
19227 TREE_VEC_ELT (init, i),
19228 LAMBDA_EXPR_CAPTURE_LIST (r),
19229 complain);
19230 }
19231 else
19232 {
19233 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19234 complain);
19235
19236 if (id_equal (DECL_NAME (field), "__this"))
19237 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19238 }
19239 }
19240
19241 tree type = begin_lambda_type (r);
19242 if (type == error_mark_node)
19243 return error_mark_node;
19244
19245 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19246 /* A lambda in a default argument outside a class gets no
19247 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19248 tsubst_default_argument calls start_lambda_scope, so we need to
19249 specifically ignore it here, and use the global scope. */
19250 record_null_lambda_scope (r);
19251 else
19252 record_lambda_scope (r);
19253
19254 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19255 determine_visibility (TYPE_NAME (type));
19256
19257 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19258
19259 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19260 ? DECL_TI_TEMPLATE (oldfn)
19261 : NULL_TREE);
19262
19263 tree fntype = static_fn_type (oldfn);
19264 if (oldtmpl)
19265 ++processing_template_decl;
19266 fntype = tsubst (fntype, args, complain, in_decl);
19267 if (oldtmpl)
19268 --processing_template_decl;
19269
19270 if (fntype == error_mark_node)
19271 r = error_mark_node;
19272 else
19273 {
19274 /* The body of a lambda-expression is not a subexpression of the
19275 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19276 which would be skipped if cp_unevaluated_operand. */
19277 cp_evaluated ev;
19278
19279 /* Fix the type of 'this'. */
19280 fntype = build_memfn_type (fntype, type,
19281 type_memfn_quals (fntype),
19282 type_memfn_rqual (fntype));
19283 tree fn, tmpl;
19284 if (oldtmpl)
19285 {
19286 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19287 if (tmpl == error_mark_node)
19288 {
19289 r = error_mark_node;
19290 goto out;
19291 }
19292 fn = DECL_TEMPLATE_RESULT (tmpl);
19293 finish_member_declaration (tmpl);
19294 }
19295 else
19296 {
19297 tmpl = NULL_TREE;
19298 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19299 if (fn == error_mark_node)
19300 {
19301 r = error_mark_node;
19302 goto out;
19303 }
19304 finish_member_declaration (fn);
19305 }
19306
19307 if (tree ci = get_constraints (oldfn))
19308 {
19309 /* Substitute into the lambda's constraints. */
19310 if (oldtmpl)
19311 ++processing_template_decl;
19312 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19313 if (oldtmpl)
19314 --processing_template_decl;
19315 set_constraints (fn, ci);
19316 }
19317
19318 /* Let finish_function set this. */
19319 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19320
19321 bool nested = cfun;
19322 if (nested)
19323 push_function_context ();
19324 else
19325 /* Still increment function_depth so that we don't GC in the
19326 middle of an expression. */
19327 ++function_depth;
19328
19329 local_specialization_stack s (lss_copy);
19330
19331 tree body = start_lambda_function (fn, r);
19332
19333 /* Now record them for lookup_init_capture_pack. */
19334 int fplen = vec_safe_length (field_packs);
19335 for (int i = 0; i < fplen; )
19336 {
19337 tree pack = (*field_packs)[i++];
19338 tree inst = (*field_packs)[i++];
19339 register_local_specialization (inst, pack);
19340 }
19341 release_tree_vector (field_packs);
19342
19343 register_parameter_specializations (oldfn, fn);
19344
19345 if (oldtmpl)
19346 {
19347 /* We might not partially instantiate some parts of the function, so
19348 copy these flags from the original template. */
19349 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19350 current_function_returns_value = ol->returns_value;
19351 current_function_returns_null = ol->returns_null;
19352 current_function_returns_abnormally = ol->returns_abnormally;
19353 current_function_infinite_loop = ol->infinite_loop;
19354 }
19355
19356 /* [temp.deduct] A lambda-expression appearing in a function type or a
19357 template parameter is not considered part of the immediate context for
19358 the purposes of template argument deduction. */
19359 complain = tf_warning_or_error;
19360
19361 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19362 /*constexpr*/false);
19363
19364 finish_lambda_function (body);
19365
19366 if (nested)
19367 pop_function_context ();
19368 else
19369 --function_depth;
19370
19371 /* The capture list was built up in reverse order; fix that now. */
19372 LAMBDA_EXPR_CAPTURE_LIST (r)
19373 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19374
19375 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19376
19377 maybe_add_lambda_conv_op (type);
19378 }
19379
19380 out:
19381 finish_struct (type, /*attr*/NULL_TREE);
19382
19383 insert_pending_capture_proxies ();
19384
19385 return r;
19386 }
19387
19388 /* Like tsubst but deals with expressions and performs semantic
19389 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19390 "F<TARGS> (ARGS)". */
19391
19392 tree
19393 tsubst_copy_and_build (tree t,
19394 tree args,
19395 tsubst_flags_t complain,
19396 tree in_decl,
19397 bool function_p,
19398 bool integral_constant_expression_p)
19399 {
19400 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19401 #define RECUR(NODE) \
19402 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19403 /*function_p=*/false, \
19404 integral_constant_expression_p)
19405
19406 tree retval, op1;
19407 location_t save_loc;
19408
19409 if (t == NULL_TREE || t == error_mark_node)
19410 return t;
19411
19412 save_loc = input_location;
19413 if (location_t eloc = cp_expr_location (t))
19414 input_location = eloc;
19415
19416 /* N3276 decltype magic only applies to calls at the top level or on the
19417 right side of a comma. */
19418 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19419 complain &= ~tf_decltype;
19420
19421 switch (TREE_CODE (t))
19422 {
19423 case USING_DECL:
19424 t = DECL_NAME (t);
19425 /* Fall through. */
19426 case IDENTIFIER_NODE:
19427 {
19428 tree decl;
19429 cp_id_kind idk;
19430 bool non_integral_constant_expression_p;
19431 const char *error_msg;
19432
19433 if (IDENTIFIER_CONV_OP_P (t))
19434 {
19435 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19436 t = make_conv_op_name (new_type);
19437 }
19438
19439 /* Look up the name. */
19440 decl = lookup_name (t);
19441
19442 /* By convention, expressions use ERROR_MARK_NODE to indicate
19443 failure, not NULL_TREE. */
19444 if (decl == NULL_TREE)
19445 decl = error_mark_node;
19446
19447 decl = finish_id_expression (t, decl, NULL_TREE,
19448 &idk,
19449 integral_constant_expression_p,
19450 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19451 &non_integral_constant_expression_p,
19452 /*template_p=*/false,
19453 /*done=*/true,
19454 /*address_p=*/false,
19455 /*template_arg_p=*/false,
19456 &error_msg,
19457 input_location);
19458 if (error_msg)
19459 error (error_msg);
19460 if (!function_p && identifier_p (decl))
19461 {
19462 if (complain & tf_error)
19463 unqualified_name_lookup_error (decl);
19464 decl = error_mark_node;
19465 }
19466 RETURN (decl);
19467 }
19468
19469 case TEMPLATE_ID_EXPR:
19470 {
19471 tree object;
19472 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19473 complain, in_decl,
19474 function_p,
19475 integral_constant_expression_p);
19476 tree targs = TREE_OPERAND (t, 1);
19477
19478 if (targs)
19479 targs = tsubst_template_args (targs, args, complain, in_decl);
19480 if (targs == error_mark_node)
19481 RETURN (error_mark_node);
19482
19483 if (TREE_CODE (templ) == SCOPE_REF)
19484 {
19485 tree name = TREE_OPERAND (templ, 1);
19486 tree tid = lookup_template_function (name, targs);
19487 TREE_OPERAND (templ, 1) = tid;
19488 RETURN (templ);
19489 }
19490
19491 if (concept_definition_p (templ))
19492 {
19493 tree check = build_concept_check (templ, targs, complain);
19494 if (check == error_mark_node)
19495 RETURN (error_mark_node);
19496
19497 tree id = unpack_concept_check (check);
19498
19499 /* If we built a function concept check, return the underlying
19500 template-id. So we can evaluate it as a function call. */
19501 if (function_concept_p (TREE_OPERAND (id, 0)))
19502 RETURN (id);
19503
19504 RETURN (check);
19505 }
19506
19507 if (variable_template_p (templ))
19508 {
19509 tree r = lookup_and_finish_template_variable (templ, targs,
19510 complain);
19511 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19512 RETURN (r);
19513 }
19514
19515 if (TREE_CODE (templ) == COMPONENT_REF)
19516 {
19517 object = TREE_OPERAND (templ, 0);
19518 templ = TREE_OPERAND (templ, 1);
19519 }
19520 else
19521 object = NULL_TREE;
19522
19523 tree tid = lookup_template_function (templ, targs);
19524
19525 if (object)
19526 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19527 object, tid, NULL_TREE));
19528 else if (identifier_p (templ))
19529 {
19530 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19531 name lookup found nothing when parsing the template name. */
19532 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19533 RETURN (tid);
19534 }
19535 else
19536 RETURN (baselink_for_fns (tid));
19537 }
19538
19539 case INDIRECT_REF:
19540 {
19541 tree r = RECUR (TREE_OPERAND (t, 0));
19542
19543 if (REFERENCE_REF_P (t))
19544 {
19545 /* A type conversion to reference type will be enclosed in
19546 such an indirect ref, but the substitution of the cast
19547 will have also added such an indirect ref. */
19548 r = convert_from_reference (r);
19549 }
19550 else
19551 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19552 complain|decltype_flag);
19553
19554 if (REF_PARENTHESIZED_P (t))
19555 r = force_paren_expr (r);
19556
19557 RETURN (r);
19558 }
19559
19560 case NOP_EXPR:
19561 {
19562 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19563 tree op0 = RECUR (TREE_OPERAND (t, 0));
19564 RETURN (build_nop (type, op0));
19565 }
19566
19567 case IMPLICIT_CONV_EXPR:
19568 {
19569 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19570 tree expr = RECUR (TREE_OPERAND (t, 0));
19571 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19572 {
19573 retval = copy_node (t);
19574 TREE_TYPE (retval) = type;
19575 TREE_OPERAND (retval, 0) = expr;
19576 RETURN (retval);
19577 }
19578 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19579 /* We'll pass this to convert_nontype_argument again, we don't need
19580 to actually perform any conversion here. */
19581 RETURN (expr);
19582 int flags = LOOKUP_IMPLICIT;
19583 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19584 flags = LOOKUP_NORMAL;
19585 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19586 flags |= LOOKUP_NO_NARROWING;
19587 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19588 flags));
19589 }
19590
19591 case CONVERT_EXPR:
19592 {
19593 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19594 tree op0 = RECUR (TREE_OPERAND (t, 0));
19595 if (op0 == error_mark_node)
19596 RETURN (error_mark_node);
19597 RETURN (build1 (CONVERT_EXPR, type, op0));
19598 }
19599
19600 case CAST_EXPR:
19601 case REINTERPRET_CAST_EXPR:
19602 case CONST_CAST_EXPR:
19603 case DYNAMIC_CAST_EXPR:
19604 case STATIC_CAST_EXPR:
19605 {
19606 tree type;
19607 tree op, r = NULL_TREE;
19608
19609 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19610 if (integral_constant_expression_p
19611 && !cast_valid_in_integral_constant_expression_p (type))
19612 {
19613 if (complain & tf_error)
19614 error ("a cast to a type other than an integral or "
19615 "enumeration type cannot appear in a constant-expression");
19616 RETURN (error_mark_node);
19617 }
19618
19619 op = RECUR (TREE_OPERAND (t, 0));
19620
19621 warning_sentinel s(warn_useless_cast);
19622 warning_sentinel s2(warn_ignored_qualifiers);
19623 switch (TREE_CODE (t))
19624 {
19625 case CAST_EXPR:
19626 r = build_functional_cast (input_location, type, op, complain);
19627 break;
19628 case REINTERPRET_CAST_EXPR:
19629 r = build_reinterpret_cast (input_location, type, op, complain);
19630 break;
19631 case CONST_CAST_EXPR:
19632 r = build_const_cast (input_location, type, op, complain);
19633 break;
19634 case DYNAMIC_CAST_EXPR:
19635 r = build_dynamic_cast (input_location, type, op, complain);
19636 break;
19637 case STATIC_CAST_EXPR:
19638 r = build_static_cast (input_location, type, op, complain);
19639 if (IMPLICIT_RVALUE_P (t))
19640 set_implicit_rvalue_p (r);
19641 break;
19642 default:
19643 gcc_unreachable ();
19644 }
19645
19646 RETURN (r);
19647 }
19648
19649 case BIT_CAST_EXPR:
19650 {
19651 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19652 tree op0 = RECUR (TREE_OPERAND (t, 0));
19653 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
19654 }
19655
19656 case POSTDECREMENT_EXPR:
19657 case POSTINCREMENT_EXPR:
19658 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19659 args, complain, in_decl);
19660 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19661 complain|decltype_flag));
19662
19663 case PREDECREMENT_EXPR:
19664 case PREINCREMENT_EXPR:
19665 case NEGATE_EXPR:
19666 case BIT_NOT_EXPR:
19667 case ABS_EXPR:
19668 case TRUTH_NOT_EXPR:
19669 case UNARY_PLUS_EXPR: /* Unary + */
19670 case REALPART_EXPR:
19671 case IMAGPART_EXPR:
19672 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19673 RECUR (TREE_OPERAND (t, 0)),
19674 complain|decltype_flag));
19675
19676 case FIX_TRUNC_EXPR:
19677 gcc_unreachable ();
19678
19679 case ADDR_EXPR:
19680 op1 = TREE_OPERAND (t, 0);
19681 if (TREE_CODE (op1) == LABEL_DECL)
19682 RETURN (finish_label_address_expr (DECL_NAME (op1),
19683 EXPR_LOCATION (op1)));
19684 if (TREE_CODE (op1) == SCOPE_REF)
19685 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19686 /*done=*/true, /*address_p=*/true);
19687 else
19688 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19689 in_decl);
19690 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19691 complain|decltype_flag));
19692
19693 case PLUS_EXPR:
19694 case MINUS_EXPR:
19695 case MULT_EXPR:
19696 case TRUNC_DIV_EXPR:
19697 case CEIL_DIV_EXPR:
19698 case FLOOR_DIV_EXPR:
19699 case ROUND_DIV_EXPR:
19700 case EXACT_DIV_EXPR:
19701 case BIT_AND_EXPR:
19702 case BIT_IOR_EXPR:
19703 case BIT_XOR_EXPR:
19704 case TRUNC_MOD_EXPR:
19705 case FLOOR_MOD_EXPR:
19706 case TRUTH_ANDIF_EXPR:
19707 case TRUTH_ORIF_EXPR:
19708 case TRUTH_AND_EXPR:
19709 case TRUTH_OR_EXPR:
19710 case RSHIFT_EXPR:
19711 case LSHIFT_EXPR:
19712 case EQ_EXPR:
19713 case NE_EXPR:
19714 case MAX_EXPR:
19715 case MIN_EXPR:
19716 case LE_EXPR:
19717 case GE_EXPR:
19718 case LT_EXPR:
19719 case GT_EXPR:
19720 case SPACESHIP_EXPR:
19721 case MEMBER_REF:
19722 case DOTSTAR_EXPR:
19723 {
19724 /* If T was type-dependent, suppress warnings that depend on the range
19725 of the types involved. */
19726 ++processing_template_decl;
19727 const bool was_dep = (potential_constant_expression (t)
19728 ? value_dependent_expression_p (t)
19729 : type_dependent_expression_p (t));
19730 --processing_template_decl;
19731 tree op0 = RECUR (TREE_OPERAND (t, 0));
19732 tree op1 = RECUR (TREE_OPERAND (t, 1));
19733
19734 warning_sentinel s1(warn_type_limits, was_dep);
19735 warning_sentinel s2(warn_div_by_zero, was_dep);
19736 warning_sentinel s3(warn_logical_op, was_dep);
19737 warning_sentinel s4(warn_tautological_compare, was_dep);
19738
19739 tree r = build_x_binary_op
19740 (input_location, TREE_CODE (t),
19741 op0,
19742 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19743 ? ERROR_MARK
19744 : TREE_CODE (TREE_OPERAND (t, 0))),
19745 op1,
19746 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19747 ? ERROR_MARK
19748 : TREE_CODE (TREE_OPERAND (t, 1))),
19749 /*overload=*/NULL,
19750 complain|decltype_flag);
19751 if (EXPR_P (r) && TREE_NO_WARNING (t))
19752 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19753
19754 RETURN (r);
19755 }
19756
19757 case POINTER_PLUS_EXPR:
19758 {
19759 tree op0 = RECUR (TREE_OPERAND (t, 0));
19760 if (op0 == error_mark_node)
19761 RETURN (error_mark_node);
19762 tree op1 = RECUR (TREE_OPERAND (t, 1));
19763 if (op1 == error_mark_node)
19764 RETURN (error_mark_node);
19765 RETURN (fold_build_pointer_plus (op0, op1));
19766 }
19767
19768 case SCOPE_REF:
19769 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19770 /*address_p=*/false));
19771 case ARRAY_REF:
19772 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19773 args, complain, in_decl);
19774 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19775 RECUR (TREE_OPERAND (t, 1)),
19776 complain|decltype_flag));
19777
19778 case SIZEOF_EXPR:
19779 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19780 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19781 RETURN (tsubst_copy (t, args, complain, in_decl));
19782 /* Fall through */
19783
19784 case ALIGNOF_EXPR:
19785 {
19786 tree r;
19787
19788 op1 = TREE_OPERAND (t, 0);
19789 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19790 op1 = TREE_TYPE (op1);
19791 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19792 && ALIGNOF_EXPR_STD_P (t));
19793 if (!args)
19794 {
19795 /* When there are no ARGS, we are trying to evaluate a
19796 non-dependent expression from the parser. Trying to do
19797 the substitutions may not work. */
19798 if (!TYPE_P (op1))
19799 op1 = TREE_TYPE (op1);
19800 }
19801 else
19802 {
19803 ++cp_unevaluated_operand;
19804 ++c_inhibit_evaluation_warnings;
19805 if (TYPE_P (op1))
19806 op1 = tsubst (op1, args, complain, in_decl);
19807 else
19808 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19809 /*function_p=*/false,
19810 /*integral_constant_expression_p=*/
19811 false);
19812 --cp_unevaluated_operand;
19813 --c_inhibit_evaluation_warnings;
19814 }
19815 if (TYPE_P (op1))
19816 r = cxx_sizeof_or_alignof_type (input_location,
19817 op1, TREE_CODE (t), std_alignof,
19818 complain & tf_error);
19819 else
19820 r = cxx_sizeof_or_alignof_expr (input_location,
19821 op1, TREE_CODE (t), std_alignof,
19822 complain & tf_error);
19823 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19824 {
19825 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19826 {
19827 if (!processing_template_decl && TYPE_P (op1))
19828 {
19829 r = build_min (SIZEOF_EXPR, size_type_node,
19830 build1 (NOP_EXPR, op1, error_mark_node));
19831 SIZEOF_EXPR_TYPE_P (r) = 1;
19832 }
19833 else
19834 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19835 TREE_SIDE_EFFECTS (r) = 0;
19836 TREE_READONLY (r) = 1;
19837 }
19838 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19839 }
19840 RETURN (r);
19841 }
19842
19843 case AT_ENCODE_EXPR:
19844 {
19845 op1 = TREE_OPERAND (t, 0);
19846 ++cp_unevaluated_operand;
19847 ++c_inhibit_evaluation_warnings;
19848 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19849 /*function_p=*/false,
19850 /*integral_constant_expression_p=*/false);
19851 --cp_unevaluated_operand;
19852 --c_inhibit_evaluation_warnings;
19853 RETURN (objc_build_encode_expr (op1));
19854 }
19855
19856 case NOEXCEPT_EXPR:
19857 op1 = TREE_OPERAND (t, 0);
19858 ++cp_unevaluated_operand;
19859 ++c_inhibit_evaluation_warnings;
19860 ++cp_noexcept_operand;
19861 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19862 /*function_p=*/false,
19863 /*integral_constant_expression_p=*/false);
19864 --cp_unevaluated_operand;
19865 --c_inhibit_evaluation_warnings;
19866 --cp_noexcept_operand;
19867 RETURN (finish_noexcept_expr (op1, complain));
19868
19869 case MODOP_EXPR:
19870 {
19871 warning_sentinel s(warn_div_by_zero);
19872 tree lhs = RECUR (TREE_OPERAND (t, 0));
19873 tree rhs = RECUR (TREE_OPERAND (t, 2));
19874 tree r = build_x_modify_expr
19875 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19876 complain|decltype_flag);
19877 /* TREE_NO_WARNING must be set if either the expression was
19878 parenthesized or it uses an operator such as >>= rather
19879 than plain assignment. In the former case, it was already
19880 set and must be copied. In the latter case,
19881 build_x_modify_expr sets it and it must not be reset
19882 here. */
19883 if (TREE_NO_WARNING (t))
19884 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19885
19886 RETURN (r);
19887 }
19888
19889 case ARROW_EXPR:
19890 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19891 args, complain, in_decl);
19892 /* Remember that there was a reference to this entity. */
19893 if (DECL_P (op1)
19894 && !mark_used (op1, complain) && !(complain & tf_error))
19895 RETURN (error_mark_node);
19896 RETURN (build_x_arrow (input_location, op1, complain));
19897
19898 case NEW_EXPR:
19899 {
19900 tree placement = RECUR (TREE_OPERAND (t, 0));
19901 tree init = RECUR (TREE_OPERAND (t, 3));
19902 vec<tree, va_gc> *placement_vec;
19903 vec<tree, va_gc> *init_vec;
19904 tree ret;
19905 location_t loc = EXPR_LOCATION (t);
19906
19907 if (placement == NULL_TREE)
19908 placement_vec = NULL;
19909 else if (placement == error_mark_node)
19910 RETURN (error_mark_node);
19911 else
19912 {
19913 placement_vec = make_tree_vector ();
19914 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19915 vec_safe_push (placement_vec, TREE_VALUE (placement));
19916 }
19917
19918 /* If there was an initializer in the original tree, but it
19919 instantiated to an empty list, then we should pass a
19920 non-NULL empty vector to tell build_new that it was an
19921 empty initializer() rather than no initializer. This can
19922 only happen when the initializer is a pack expansion whose
19923 parameter packs are of length zero. */
19924 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19925 init_vec = NULL;
19926 else if (init == error_mark_node)
19927 RETURN (error_mark_node);
19928 else
19929 {
19930 init_vec = make_tree_vector ();
19931 if (init == void_node)
19932 gcc_assert (init_vec != NULL);
19933 else
19934 {
19935 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19936 vec_safe_push (init_vec, TREE_VALUE (init));
19937 }
19938 }
19939
19940 /* Avoid passing an enclosing decl to valid_array_size_p. */
19941 in_decl = NULL_TREE;
19942
19943 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19944 tree op2 = RECUR (TREE_OPERAND (t, 2));
19945 ret = build_new (loc, &placement_vec, op1, op2,
19946 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19947 complain);
19948
19949 if (placement_vec != NULL)
19950 release_tree_vector (placement_vec);
19951 if (init_vec != NULL)
19952 release_tree_vector (init_vec);
19953
19954 RETURN (ret);
19955 }
19956
19957 case DELETE_EXPR:
19958 {
19959 tree op0 = RECUR (TREE_OPERAND (t, 0));
19960 tree op1 = RECUR (TREE_OPERAND (t, 1));
19961 RETURN (delete_sanity (input_location, op0, op1,
19962 DELETE_EXPR_USE_VEC (t),
19963 DELETE_EXPR_USE_GLOBAL (t),
19964 complain));
19965 }
19966
19967 case COMPOUND_EXPR:
19968 {
19969 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19970 complain & ~tf_decltype, in_decl,
19971 /*function_p=*/false,
19972 integral_constant_expression_p);
19973 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19974 op0,
19975 RECUR (TREE_OPERAND (t, 1)),
19976 complain|decltype_flag));
19977 }
19978
19979 case CALL_EXPR:
19980 {
19981 tree function;
19982 unsigned int nargs, i;
19983 bool qualified_p;
19984 bool koenig_p;
19985 tree ret;
19986
19987 function = CALL_EXPR_FN (t);
19988 /* Internal function with no arguments. */
19989 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19990 RETURN (t);
19991
19992 /* When we parsed the expression, we determined whether or
19993 not Koenig lookup should be performed. */
19994 koenig_p = KOENIG_LOOKUP_P (t);
19995 if (function == NULL_TREE)
19996 {
19997 koenig_p = false;
19998 qualified_p = false;
19999 }
20000 else if (TREE_CODE (function) == SCOPE_REF)
20001 {
20002 qualified_p = true;
20003 function = tsubst_qualified_id (function, args, complain, in_decl,
20004 /*done=*/false,
20005 /*address_p=*/false);
20006 }
20007 else if (koenig_p && identifier_p (function))
20008 {
20009 /* Do nothing; calling tsubst_copy_and_build on an identifier
20010 would incorrectly perform unqualified lookup again.
20011
20012 Note that we can also have an IDENTIFIER_NODE if the earlier
20013 unqualified lookup found a member function; in that case
20014 koenig_p will be false and we do want to do the lookup
20015 again to find the instantiated member function.
20016
20017 FIXME but doing that causes c++/15272, so we need to stop
20018 using IDENTIFIER_NODE in that situation. */
20019 qualified_p = false;
20020 }
20021 else
20022 {
20023 if (TREE_CODE (function) == COMPONENT_REF)
20024 {
20025 tree op = TREE_OPERAND (function, 1);
20026
20027 qualified_p = (TREE_CODE (op) == SCOPE_REF
20028 || (BASELINK_P (op)
20029 && BASELINK_QUALIFIED_P (op)));
20030 }
20031 else
20032 qualified_p = false;
20033
20034 if (TREE_CODE (function) == ADDR_EXPR
20035 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20036 /* Avoid error about taking the address of a constructor. */
20037 function = TREE_OPERAND (function, 0);
20038
20039 function = tsubst_copy_and_build (function, args, complain,
20040 in_decl,
20041 !qualified_p,
20042 integral_constant_expression_p);
20043
20044 if (BASELINK_P (function))
20045 qualified_p = true;
20046 }
20047
20048 nargs = call_expr_nargs (t);
20049 releasing_vec call_args;
20050 for (i = 0; i < nargs; ++i)
20051 {
20052 tree arg = CALL_EXPR_ARG (t, i);
20053
20054 if (!PACK_EXPANSION_P (arg))
20055 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
20056 else
20057 {
20058 /* Expand the pack expansion and push each entry onto
20059 CALL_ARGS. */
20060 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20061 if (TREE_CODE (arg) == TREE_VEC)
20062 {
20063 unsigned int len, j;
20064
20065 len = TREE_VEC_LENGTH (arg);
20066 for (j = 0; j < len; ++j)
20067 {
20068 tree value = TREE_VEC_ELT (arg, j);
20069 if (value != NULL_TREE)
20070 value = convert_from_reference (value);
20071 vec_safe_push (call_args, value);
20072 }
20073 }
20074 else
20075 {
20076 /* A partial substitution. Add one entry. */
20077 vec_safe_push (call_args, arg);
20078 }
20079 }
20080 }
20081
20082 /* Stripped-down processing for a call in a thunk. Specifically, in
20083 the thunk template for a generic lambda. */
20084 if (call_from_lambda_thunk_p (t))
20085 {
20086 /* Now that we've expanded any packs, the number of call args
20087 might be different. */
20088 unsigned int cargs = call_args->length ();
20089 tree thisarg = NULL_TREE;
20090 if (TREE_CODE (function) == COMPONENT_REF)
20091 {
20092 thisarg = TREE_OPERAND (function, 0);
20093 if (TREE_CODE (thisarg) == INDIRECT_REF)
20094 thisarg = TREE_OPERAND (thisarg, 0);
20095 function = TREE_OPERAND (function, 1);
20096 if (TREE_CODE (function) == BASELINK)
20097 function = BASELINK_FUNCTIONS (function);
20098 }
20099 /* We aren't going to do normal overload resolution, so force the
20100 template-id to resolve. */
20101 function = resolve_nondeduced_context (function, complain);
20102 for (unsigned i = 0; i < cargs; ++i)
20103 {
20104 /* In a thunk, pass through args directly, without any
20105 conversions. */
20106 tree arg = (*call_args)[i];
20107 while (TREE_CODE (arg) != PARM_DECL)
20108 arg = TREE_OPERAND (arg, 0);
20109 (*call_args)[i] = arg;
20110 }
20111 if (thisarg)
20112 {
20113 /* If there are no other args, just push 'this'. */
20114 if (cargs == 0)
20115 vec_safe_push (call_args, thisarg);
20116 else
20117 {
20118 /* Otherwise, shift the other args over to make room. */
20119 tree last = (*call_args)[cargs - 1];
20120 vec_safe_push (call_args, last);
20121 for (int i = cargs - 1; i > 0; --i)
20122 (*call_args)[i] = (*call_args)[i - 1];
20123 (*call_args)[0] = thisarg;
20124 }
20125 }
20126 ret = build_call_a (function, call_args->length (),
20127 call_args->address ());
20128 /* The thunk location is not interesting. */
20129 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20130 CALL_FROM_THUNK_P (ret) = true;
20131 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20132 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20133
20134 RETURN (ret);
20135 }
20136
20137 /* We do not perform argument-dependent lookup if normal
20138 lookup finds a non-function, in accordance with the
20139 resolution of DR 218. */
20140 if (koenig_p
20141 && ((is_overloaded_fn (function)
20142 /* If lookup found a member function, the Koenig lookup is
20143 not appropriate, even if an unqualified-name was used
20144 to denote the function. */
20145 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20146 || identifier_p (function)
20147 /* C++20 P0846: Lookup found nothing. */
20148 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20149 && identifier_p (TREE_OPERAND (function, 0))))
20150 /* Only do this when substitution turns a dependent call
20151 into a non-dependent call. */
20152 && type_dependent_expression_p_push (t)
20153 && !any_type_dependent_arguments_p (call_args))
20154 function = perform_koenig_lookup (function, call_args, tf_none);
20155
20156 if (function != NULL_TREE
20157 && (identifier_p (function)
20158 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20159 && identifier_p (TREE_OPERAND (function, 0))))
20160 && !any_type_dependent_arguments_p (call_args))
20161 {
20162 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20163 function = TREE_OPERAND (function, 0);
20164 if (koenig_p && (complain & tf_warning_or_error))
20165 {
20166 /* For backwards compatibility and good diagnostics, try
20167 the unqualified lookup again if we aren't in SFINAE
20168 context. */
20169 tree unq = (tsubst_copy_and_build
20170 (function, args, complain, in_decl, true,
20171 integral_constant_expression_p));
20172 if (unq == error_mark_node)
20173 RETURN (error_mark_node);
20174
20175 if (unq != function)
20176 {
20177 /* In a lambda fn, we have to be careful to not
20178 introduce new this captures. Legacy code can't
20179 be using lambdas anyway, so it's ok to be
20180 stricter. */
20181 bool in_lambda = (current_class_type
20182 && LAMBDA_TYPE_P (current_class_type));
20183 char const *const msg
20184 = G_("%qD was not declared in this scope, "
20185 "and no declarations were found by "
20186 "argument-dependent lookup at the point "
20187 "of instantiation");
20188
20189 bool diag = true;
20190 if (in_lambda)
20191 error_at (cp_expr_loc_or_input_loc (t),
20192 msg, function);
20193 else
20194 diag = permerror (cp_expr_loc_or_input_loc (t),
20195 msg, function);
20196 if (diag)
20197 {
20198 tree fn = unq;
20199
20200 if (INDIRECT_REF_P (fn))
20201 fn = TREE_OPERAND (fn, 0);
20202 if (is_overloaded_fn (fn))
20203 fn = get_first_fn (fn);
20204
20205 if (!DECL_P (fn))
20206 /* Can't say anything more. */;
20207 else if (DECL_CLASS_SCOPE_P (fn))
20208 {
20209 location_t loc = cp_expr_loc_or_input_loc (t);
20210 inform (loc,
20211 "declarations in dependent base %qT are "
20212 "not found by unqualified lookup",
20213 DECL_CLASS_CONTEXT (fn));
20214 if (current_class_ptr)
20215 inform (loc,
20216 "use %<this->%D%> instead", function);
20217 else
20218 inform (loc,
20219 "use %<%T::%D%> instead",
20220 current_class_name, function);
20221 }
20222 else
20223 inform (DECL_SOURCE_LOCATION (fn),
20224 "%qD declared here, later in the "
20225 "translation unit", fn);
20226 if (in_lambda)
20227 RETURN (error_mark_node);
20228 }
20229
20230 function = unq;
20231 }
20232 }
20233 if (identifier_p (function))
20234 {
20235 if (complain & tf_error)
20236 unqualified_name_lookup_error (function);
20237 RETURN (error_mark_node);
20238 }
20239 }
20240
20241 /* Remember that there was a reference to this entity. */
20242 if (function != NULL_TREE
20243 && DECL_P (function)
20244 && !mark_used (function, complain) && !(complain & tf_error))
20245 RETURN (error_mark_node);
20246
20247 /* Put back tf_decltype for the actual call. */
20248 complain |= decltype_flag;
20249
20250 if (function == NULL_TREE)
20251 switch (CALL_EXPR_IFN (t))
20252 {
20253 case IFN_LAUNDER:
20254 gcc_assert (nargs == 1);
20255 if (vec_safe_length (call_args) != 1)
20256 {
20257 error_at (cp_expr_loc_or_input_loc (t),
20258 "wrong number of arguments to "
20259 "%<__builtin_launder%>");
20260 ret = error_mark_node;
20261 }
20262 else
20263 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20264 (*call_args)[0], complain);
20265 break;
20266
20267 case IFN_VEC_CONVERT:
20268 gcc_assert (nargs == 1);
20269 if (vec_safe_length (call_args) != 1)
20270 {
20271 error_at (cp_expr_loc_or_input_loc (t),
20272 "wrong number of arguments to "
20273 "%<__builtin_convertvector%>");
20274 ret = error_mark_node;
20275 break;
20276 }
20277 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20278 tsubst (TREE_TYPE (t), args,
20279 complain, in_decl),
20280 complain);
20281 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20282 RETURN (ret);
20283 break;
20284
20285 default:
20286 /* Unsupported internal function with arguments. */
20287 gcc_unreachable ();
20288 }
20289 else if (TREE_CODE (function) == OFFSET_REF
20290 || TREE_CODE (function) == DOTSTAR_EXPR
20291 || TREE_CODE (function) == MEMBER_REF)
20292 ret = build_offset_ref_call_from_tree (function, &call_args,
20293 complain);
20294 else if (TREE_CODE (function) == COMPONENT_REF)
20295 {
20296 tree instance = TREE_OPERAND (function, 0);
20297 tree fn = TREE_OPERAND (function, 1);
20298
20299 if (processing_template_decl
20300 && (type_dependent_expression_p (instance)
20301 || (!BASELINK_P (fn)
20302 && TREE_CODE (fn) != FIELD_DECL)
20303 || type_dependent_expression_p (fn)
20304 || any_type_dependent_arguments_p (call_args)))
20305 ret = build_min_nt_call_vec (function, call_args);
20306 else if (!BASELINK_P (fn))
20307 ret = finish_call_expr (function, &call_args,
20308 /*disallow_virtual=*/false,
20309 /*koenig_p=*/false,
20310 complain);
20311 else
20312 ret = (build_new_method_call
20313 (instance, fn,
20314 &call_args, NULL_TREE,
20315 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20316 /*fn_p=*/NULL,
20317 complain));
20318 }
20319 else if (concept_check_p (function))
20320 {
20321 /* FUNCTION is a template-id referring to a concept definition. */
20322 tree id = unpack_concept_check (function);
20323 tree tmpl = TREE_OPERAND (id, 0);
20324 tree args = TREE_OPERAND (id, 1);
20325
20326 /* Calls to standard and variable concepts should have been
20327 previously diagnosed. */
20328 gcc_assert (function_concept_p (tmpl));
20329
20330 /* Ensure the result is wrapped as a call expression. */
20331 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20332 }
20333 else
20334 ret = finish_call_expr (function, &call_args,
20335 /*disallow_virtual=*/qualified_p,
20336 koenig_p,
20337 complain);
20338
20339 if (ret != error_mark_node)
20340 {
20341 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20342 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20343 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20344 if (op || ord || rev)
20345 {
20346 function = extract_call_expr (ret);
20347 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20348 CALL_EXPR_ORDERED_ARGS (function) = ord;
20349 CALL_EXPR_REVERSE_ARGS (function) = rev;
20350 }
20351 }
20352
20353 RETURN (ret);
20354 }
20355
20356 case COND_EXPR:
20357 {
20358 tree cond = RECUR (TREE_OPERAND (t, 0));
20359 cond = mark_rvalue_use (cond);
20360 tree folded_cond = fold_non_dependent_expr (cond, complain);
20361 tree exp1, exp2;
20362
20363 if (TREE_CODE (folded_cond) == INTEGER_CST)
20364 {
20365 if (integer_zerop (folded_cond))
20366 {
20367 ++c_inhibit_evaluation_warnings;
20368 exp1 = RECUR (TREE_OPERAND (t, 1));
20369 --c_inhibit_evaluation_warnings;
20370 exp2 = RECUR (TREE_OPERAND (t, 2));
20371 }
20372 else
20373 {
20374 exp1 = RECUR (TREE_OPERAND (t, 1));
20375 ++c_inhibit_evaluation_warnings;
20376 exp2 = RECUR (TREE_OPERAND (t, 2));
20377 --c_inhibit_evaluation_warnings;
20378 }
20379 cond = folded_cond;
20380 }
20381 else
20382 {
20383 exp1 = RECUR (TREE_OPERAND (t, 1));
20384 exp2 = RECUR (TREE_OPERAND (t, 2));
20385 }
20386
20387 warning_sentinel s(warn_duplicated_branches);
20388 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20389 cond, exp1, exp2, complain));
20390 }
20391
20392 case PSEUDO_DTOR_EXPR:
20393 {
20394 tree op0 = RECUR (TREE_OPERAND (t, 0));
20395 tree op1 = RECUR (TREE_OPERAND (t, 1));
20396 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20397 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20398 input_location));
20399 }
20400
20401 case TREE_LIST:
20402 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20403
20404 case COMPONENT_REF:
20405 {
20406 tree object;
20407 tree object_type;
20408 tree member;
20409 tree r;
20410
20411 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20412 args, complain, in_decl);
20413 /* Remember that there was a reference to this entity. */
20414 if (DECL_P (object)
20415 && !mark_used (object, complain) && !(complain & tf_error))
20416 RETURN (error_mark_node);
20417 object_type = TREE_TYPE (object);
20418
20419 member = TREE_OPERAND (t, 1);
20420 if (BASELINK_P (member))
20421 member = tsubst_baselink (member,
20422 non_reference (TREE_TYPE (object)),
20423 args, complain, in_decl);
20424 else
20425 member = tsubst_copy (member, args, complain, in_decl);
20426 if (member == error_mark_node)
20427 RETURN (error_mark_node);
20428
20429 if (TREE_CODE (member) == FIELD_DECL)
20430 {
20431 r = finish_non_static_data_member (member, object, NULL_TREE);
20432 if (TREE_CODE (r) == COMPONENT_REF)
20433 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20434 RETURN (r);
20435 }
20436 else if (type_dependent_expression_p (object))
20437 /* We can't do much here. */;
20438 else if (!CLASS_TYPE_P (object_type))
20439 {
20440 if (scalarish_type_p (object_type))
20441 {
20442 tree s = NULL_TREE;
20443 tree dtor = member;
20444
20445 if (TREE_CODE (dtor) == SCOPE_REF)
20446 {
20447 s = TREE_OPERAND (dtor, 0);
20448 dtor = TREE_OPERAND (dtor, 1);
20449 }
20450 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20451 {
20452 dtor = TREE_OPERAND (dtor, 0);
20453 if (TYPE_P (dtor))
20454 RETURN (finish_pseudo_destructor_expr
20455 (object, s, dtor, input_location));
20456 }
20457 }
20458 }
20459 else if (TREE_CODE (member) == SCOPE_REF
20460 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20461 {
20462 /* Lookup the template functions now that we know what the
20463 scope is. */
20464 tree scope = TREE_OPERAND (member, 0);
20465 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20466 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20467 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20468 /*complain=*/false);
20469 if (BASELINK_P (member))
20470 {
20471 BASELINK_FUNCTIONS (member)
20472 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20473 args);
20474 member = (adjust_result_of_qualified_name_lookup
20475 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20476 object_type));
20477 }
20478 else
20479 {
20480 qualified_name_lookup_error (scope, tmpl, member,
20481 input_location);
20482 RETURN (error_mark_node);
20483 }
20484 }
20485 else if (TREE_CODE (member) == SCOPE_REF
20486 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20487 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20488 {
20489 if (complain & tf_error)
20490 {
20491 if (TYPE_P (TREE_OPERAND (member, 0)))
20492 error ("%qT is not a class or namespace",
20493 TREE_OPERAND (member, 0));
20494 else
20495 error ("%qD is not a class or namespace",
20496 TREE_OPERAND (member, 0));
20497 }
20498 RETURN (error_mark_node);
20499 }
20500
20501 r = finish_class_member_access_expr (object, member,
20502 /*template_p=*/false,
20503 complain);
20504 if (TREE_CODE (r) == COMPONENT_REF)
20505 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20506 RETURN (r);
20507 }
20508
20509 case THROW_EXPR:
20510 RETURN (build_throw
20511 (input_location, RECUR (TREE_OPERAND (t, 0))));
20512
20513 case CONSTRUCTOR:
20514 {
20515 vec<constructor_elt, va_gc> *n;
20516 constructor_elt *ce;
20517 unsigned HOST_WIDE_INT idx;
20518 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20519 bool process_index_p;
20520 int newlen;
20521 bool need_copy_p = false;
20522 tree r;
20523
20524 if (type == error_mark_node)
20525 RETURN (error_mark_node);
20526
20527 /* We do not want to process the index of aggregate
20528 initializers as they are identifier nodes which will be
20529 looked up by digest_init. */
20530 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20531
20532 if (null_member_pointer_value_p (t))
20533 {
20534 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20535 RETURN (t);
20536 }
20537
20538 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20539 newlen = vec_safe_length (n);
20540 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20541 {
20542 if (ce->index && process_index_p
20543 /* An identifier index is looked up in the type
20544 being initialized, not the current scope. */
20545 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20546 ce->index = RECUR (ce->index);
20547
20548 if (PACK_EXPANSION_P (ce->value))
20549 {
20550 /* Substitute into the pack expansion. */
20551 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20552 in_decl);
20553
20554 if (ce->value == error_mark_node
20555 || PACK_EXPANSION_P (ce->value))
20556 ;
20557 else if (TREE_VEC_LENGTH (ce->value) == 1)
20558 /* Just move the argument into place. */
20559 ce->value = TREE_VEC_ELT (ce->value, 0);
20560 else
20561 {
20562 /* Update the length of the final CONSTRUCTOR
20563 arguments vector, and note that we will need to
20564 copy.*/
20565 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20566 need_copy_p = true;
20567 }
20568 }
20569 else
20570 ce->value = RECUR (ce->value);
20571 }
20572
20573 if (need_copy_p)
20574 {
20575 vec<constructor_elt, va_gc> *old_n = n;
20576
20577 vec_alloc (n, newlen);
20578 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20579 {
20580 if (TREE_CODE (ce->value) == TREE_VEC)
20581 {
20582 int i, len = TREE_VEC_LENGTH (ce->value);
20583 for (i = 0; i < len; ++i)
20584 CONSTRUCTOR_APPEND_ELT (n, 0,
20585 TREE_VEC_ELT (ce->value, i));
20586 }
20587 else
20588 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20589 }
20590 }
20591
20592 r = build_constructor (init_list_type_node, n);
20593 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20594 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20595 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20596
20597 if (TREE_HAS_CONSTRUCTOR (t))
20598 {
20599 fcl_t cl = fcl_functional;
20600 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20601 cl = fcl_c99;
20602 RETURN (finish_compound_literal (type, r, complain, cl));
20603 }
20604
20605 TREE_TYPE (r) = type;
20606 RETURN (r);
20607 }
20608
20609 case TYPEID_EXPR:
20610 {
20611 tree operand_0 = TREE_OPERAND (t, 0);
20612 if (TYPE_P (operand_0))
20613 {
20614 operand_0 = tsubst (operand_0, args, complain, in_decl);
20615 RETURN (get_typeid (operand_0, complain));
20616 }
20617 else
20618 {
20619 operand_0 = RECUR (operand_0);
20620 RETURN (build_typeid (operand_0, complain));
20621 }
20622 }
20623
20624 case VAR_DECL:
20625 if (!args)
20626 RETURN (t);
20627 /* Fall through */
20628
20629 case PARM_DECL:
20630 {
20631 tree r = tsubst_copy (t, args, complain, in_decl);
20632 /* ??? We're doing a subset of finish_id_expression here. */
20633 if (tree wrap = maybe_get_tls_wrapper_call (r))
20634 /* Replace an evaluated use of the thread_local variable with
20635 a call to its wrapper. */
20636 r = wrap;
20637 else if (outer_automatic_var_p (r))
20638 r = process_outer_var_ref (r, complain);
20639
20640 if (!TYPE_REF_P (TREE_TYPE (t)))
20641 /* If the original type was a reference, we'll be wrapped in
20642 the appropriate INDIRECT_REF. */
20643 r = convert_from_reference (r);
20644 RETURN (r);
20645 }
20646
20647 case VA_ARG_EXPR:
20648 {
20649 tree op0 = RECUR (TREE_OPERAND (t, 0));
20650 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20651 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20652 }
20653
20654 case OFFSETOF_EXPR:
20655 {
20656 tree object_ptr
20657 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20658 in_decl, /*function_p=*/false,
20659 /*integral_constant_expression_p=*/false);
20660 RETURN (finish_offsetof (object_ptr,
20661 RECUR (TREE_OPERAND (t, 0)),
20662 EXPR_LOCATION (t)));
20663 }
20664
20665 case ADDRESSOF_EXPR:
20666 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20667 RECUR (TREE_OPERAND (t, 0)), complain));
20668
20669 case TRAIT_EXPR:
20670 {
20671 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20672 complain, in_decl);
20673 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20674 complain, in_decl);
20675 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20676 TRAIT_EXPR_KIND (t), type1, type2));
20677 }
20678
20679 case STMT_EXPR:
20680 {
20681 tree old_stmt_expr = cur_stmt_expr;
20682 tree stmt_expr = begin_stmt_expr ();
20683
20684 cur_stmt_expr = stmt_expr;
20685 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20686 integral_constant_expression_p);
20687 stmt_expr = finish_stmt_expr (stmt_expr, false);
20688 cur_stmt_expr = old_stmt_expr;
20689
20690 /* If the resulting list of expression statement is empty,
20691 fold it further into void_node. */
20692 if (empty_expr_stmt_p (stmt_expr))
20693 stmt_expr = void_node;
20694
20695 RETURN (stmt_expr);
20696 }
20697
20698 case LAMBDA_EXPR:
20699 {
20700 if (complain & tf_partial)
20701 {
20702 /* We don't have a full set of template arguments yet; don't touch
20703 the lambda at all. */
20704 gcc_assert (processing_template_decl);
20705 return t;
20706 }
20707 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20708
20709 RETURN (build_lambda_object (r));
20710 }
20711
20712 case TARGET_EXPR:
20713 /* We can get here for a constant initializer of non-dependent type.
20714 FIXME stop folding in cp_parser_initializer_clause. */
20715 {
20716 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20717 complain);
20718 RETURN (r);
20719 }
20720
20721 case TRANSACTION_EXPR:
20722 RETURN (tsubst_expr(t, args, complain, in_decl,
20723 integral_constant_expression_p));
20724
20725 case PAREN_EXPR:
20726 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20727
20728 case VEC_PERM_EXPR:
20729 {
20730 tree op0 = RECUR (TREE_OPERAND (t, 0));
20731 tree op1 = RECUR (TREE_OPERAND (t, 1));
20732 tree op2 = RECUR (TREE_OPERAND (t, 2));
20733 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20734 complain));
20735 }
20736
20737 case REQUIRES_EXPR:
20738 {
20739 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20740 RETURN (r);
20741 }
20742
20743 case RANGE_EXPR:
20744 /* No need to substitute further, a RANGE_EXPR will always be built
20745 with constant operands. */
20746 RETURN (t);
20747
20748 case NON_LVALUE_EXPR:
20749 case VIEW_CONVERT_EXPR:
20750 if (location_wrapper_p (t))
20751 /* We need to do this here as well as in tsubst_copy so we get the
20752 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20753 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20754 EXPR_LOCATION (t)));
20755 /* fallthrough. */
20756
20757 default:
20758 /* Handle Objective-C++ constructs, if appropriate. */
20759 {
20760 tree subst
20761 = objcp_tsubst_copy_and_build (t, args, complain,
20762 in_decl, /*function_p=*/false);
20763 if (subst)
20764 RETURN (subst);
20765 }
20766 RETURN (tsubst_copy (t, args, complain, in_decl));
20767 }
20768
20769 #undef RECUR
20770 #undef RETURN
20771 out:
20772 input_location = save_loc;
20773 return retval;
20774 }
20775
20776 /* Verify that the instantiated ARGS are valid. For type arguments,
20777 make sure that the type's linkage is ok. For non-type arguments,
20778 make sure they are constants if they are integral or enumerations.
20779 Emit an error under control of COMPLAIN, and return TRUE on error. */
20780
20781 static bool
20782 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20783 {
20784 if (dependent_template_arg_p (t))
20785 return false;
20786 if (ARGUMENT_PACK_P (t))
20787 {
20788 tree vec = ARGUMENT_PACK_ARGS (t);
20789 int len = TREE_VEC_LENGTH (vec);
20790 bool result = false;
20791 int i;
20792
20793 for (i = 0; i < len; ++i)
20794 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20795 result = true;
20796 return result;
20797 }
20798 else if (TYPE_P (t))
20799 {
20800 /* [basic.link]: A name with no linkage (notably, the name
20801 of a class or enumeration declared in a local scope)
20802 shall not be used to declare an entity with linkage.
20803 This implies that names with no linkage cannot be used as
20804 template arguments
20805
20806 DR 757 relaxes this restriction for C++0x. */
20807 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20808 : no_linkage_check (t, /*relaxed_p=*/false));
20809
20810 if (nt)
20811 {
20812 /* DR 488 makes use of a type with no linkage cause
20813 type deduction to fail. */
20814 if (complain & tf_error)
20815 {
20816 if (TYPE_UNNAMED_P (nt))
20817 error ("%qT is/uses unnamed type", t);
20818 else
20819 error ("template argument for %qD uses local type %qT",
20820 tmpl, t);
20821 }
20822 return true;
20823 }
20824 /* In order to avoid all sorts of complications, we do not
20825 allow variably-modified types as template arguments. */
20826 else if (variably_modified_type_p (t, NULL_TREE))
20827 {
20828 if (complain & tf_error)
20829 error ("%qT is a variably modified type", t);
20830 return true;
20831 }
20832 }
20833 /* Class template and alias template arguments should be OK. */
20834 else if (DECL_TYPE_TEMPLATE_P (t))
20835 ;
20836 /* A non-type argument of integral or enumerated type must be a
20837 constant. */
20838 else if (TREE_TYPE (t)
20839 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20840 && !REFERENCE_REF_P (t)
20841 && !TREE_CONSTANT (t))
20842 {
20843 if (complain & tf_error)
20844 error ("integral expression %qE is not constant", t);
20845 return true;
20846 }
20847 return false;
20848 }
20849
20850 static bool
20851 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20852 {
20853 int ix, len = DECL_NTPARMS (tmpl);
20854 bool result = false;
20855
20856 for (ix = 0; ix != len; ix++)
20857 {
20858 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20859 result = true;
20860 }
20861 if (result && (complain & tf_error))
20862 error (" trying to instantiate %qD", tmpl);
20863 return result;
20864 }
20865
20866 /* We're out of SFINAE context now, so generate diagnostics for the access
20867 errors we saw earlier when instantiating D from TMPL and ARGS. */
20868
20869 static void
20870 recheck_decl_substitution (tree d, tree tmpl, tree args)
20871 {
20872 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20873 tree type = TREE_TYPE (pattern);
20874 location_t loc = input_location;
20875
20876 push_access_scope (d);
20877 push_deferring_access_checks (dk_no_deferred);
20878 input_location = DECL_SOURCE_LOCATION (pattern);
20879 tsubst (type, args, tf_warning_or_error, d);
20880 input_location = loc;
20881 pop_deferring_access_checks ();
20882 pop_access_scope (d);
20883 }
20884
20885 /* Instantiate the indicated variable, function, or alias template TMPL with
20886 the template arguments in TARG_PTR. */
20887
20888 static tree
20889 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20890 {
20891 tree targ_ptr = orig_args;
20892 tree fndecl;
20893 tree gen_tmpl;
20894 tree spec;
20895 bool access_ok = true;
20896
20897 if (tmpl == error_mark_node)
20898 return error_mark_node;
20899
20900 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20901
20902 /* If this function is a clone, handle it specially. */
20903 if (DECL_CLONED_FUNCTION_P (tmpl))
20904 {
20905 tree spec;
20906 tree clone;
20907
20908 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20909 DECL_CLONED_FUNCTION. */
20910 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20911 targ_ptr, complain);
20912 if (spec == error_mark_node)
20913 return error_mark_node;
20914
20915 /* Look for the clone. */
20916 FOR_EACH_CLONE (clone, spec)
20917 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20918 return clone;
20919 /* We should always have found the clone by now. */
20920 gcc_unreachable ();
20921 return NULL_TREE;
20922 }
20923
20924 if (targ_ptr == error_mark_node)
20925 return error_mark_node;
20926
20927 /* Check to see if we already have this specialization. */
20928 gen_tmpl = most_general_template (tmpl);
20929 if (TMPL_ARGS_DEPTH (targ_ptr)
20930 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20931 /* targ_ptr only has the innermost template args, so add the outer ones
20932 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20933 the case of a non-dependent call within a template definition). */
20934 targ_ptr = (add_outermost_template_args
20935 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20936 targ_ptr));
20937
20938 if (modules_p ())
20939 {
20940 tree origin = get_originating_module_decl (gen_tmpl);
20941 load_pending_specializations (CP_DECL_CONTEXT (origin),
20942 DECL_NAME (origin));
20943 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl))
20944 lazy_load_specializations (gen_tmpl);
20945 }
20946
20947 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20948 but it doesn't seem to be on the hot path. */
20949 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20950
20951 gcc_checking_assert (tmpl == gen_tmpl
20952 || ((fndecl
20953 = retrieve_specialization (tmpl, orig_args, 0))
20954 == spec)
20955 || fndecl == NULL_TREE);
20956
20957 if (spec != NULL_TREE)
20958 {
20959 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20960 {
20961 if (complain & tf_error)
20962 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20963 return error_mark_node;
20964 }
20965 return spec;
20966 }
20967
20968 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20969 complain))
20970 return error_mark_node;
20971
20972 /* We are building a FUNCTION_DECL, during which the access of its
20973 parameters and return types have to be checked. However this
20974 FUNCTION_DECL which is the desired context for access checking
20975 is not built yet. We solve this chicken-and-egg problem by
20976 deferring all checks until we have the FUNCTION_DECL. */
20977 push_deferring_access_checks (dk_deferred);
20978
20979 /* Instantiation of the function happens in the context of the function
20980 template, not the context of the overload resolution we're doing. */
20981 push_to_top_level ();
20982 /* If there are dependent arguments, e.g. because we're doing partial
20983 ordering, make sure processing_template_decl stays set. */
20984 if (uses_template_parms (targ_ptr))
20985 ++processing_template_decl;
20986 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20987 {
20988 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20989 complain, gen_tmpl, true);
20990 push_nested_class (ctx);
20991 }
20992
20993 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20994
20995 fndecl = NULL_TREE;
20996 if (VAR_P (pattern))
20997 {
20998 /* We need to determine if we're using a partial or explicit
20999 specialization now, because the type of the variable could be
21000 different. */
21001 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
21002 tree elt = most_specialized_partial_spec (tid, complain);
21003 if (elt == error_mark_node)
21004 pattern = error_mark_node;
21005 else if (elt)
21006 {
21007 tree partial_tmpl = TREE_VALUE (elt);
21008 tree partial_args = TREE_PURPOSE (elt);
21009 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21010 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21011 }
21012 }
21013
21014 /* Substitute template parameters to obtain the specialization. */
21015 if (fndecl == NULL_TREE)
21016 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21017 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21018 pop_nested_class ();
21019 pop_from_top_level ();
21020
21021 if (fndecl == error_mark_node)
21022 {
21023 pop_deferring_access_checks ();
21024 return error_mark_node;
21025 }
21026
21027 /* The DECL_TI_TEMPLATE should always be the immediate parent
21028 template, not the most general template. */
21029 DECL_TI_TEMPLATE (fndecl) = tmpl;
21030 DECL_TI_ARGS (fndecl) = targ_ptr;
21031
21032 set_instantiating_module (fndecl);
21033
21034 /* Now we know the specialization, compute access previously
21035 deferred. Do no access control for inheriting constructors,
21036 as we already checked access for the inherited constructor. */
21037 if (!(flag_new_inheriting_ctors
21038 && DECL_INHERITED_CTOR (fndecl)))
21039 {
21040 push_access_scope (fndecl);
21041 if (!perform_deferred_access_checks (complain))
21042 access_ok = false;
21043 pop_access_scope (fndecl);
21044 }
21045 pop_deferring_access_checks ();
21046
21047 /* If we've just instantiated the main entry point for a function,
21048 instantiate all the alternate entry points as well. We do this
21049 by cloning the instantiation of the main entry point, not by
21050 instantiating the template clones. */
21051 if (tree chain = DECL_CHAIN (gen_tmpl))
21052 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21053 clone_cdtor (fndecl, /*update_methods=*/false);
21054
21055 if (!access_ok)
21056 {
21057 if (!(complain & tf_error))
21058 {
21059 /* Remember to reinstantiate when we're out of SFINAE so the user
21060 can see the errors. */
21061 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21062 }
21063 return error_mark_node;
21064 }
21065 return fndecl;
21066 }
21067
21068 /* Wrapper for instantiate_template_1. */
21069
21070 tree
21071 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21072 {
21073 tree ret;
21074 timevar_push (TV_TEMPLATE_INST);
21075 ret = instantiate_template_1 (tmpl, orig_args, complain);
21076 timevar_pop (TV_TEMPLATE_INST);
21077 return ret;
21078 }
21079
21080 /* Instantiate the alias template TMPL with ARGS. Also push a template
21081 instantiation level, which instantiate_template doesn't do because
21082 functions and variables have sufficient context established by the
21083 callers. */
21084
21085 static tree
21086 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21087 {
21088 if (tmpl == error_mark_node || args == error_mark_node)
21089 return error_mark_node;
21090
21091 args =
21092 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21093 args, tmpl, complain,
21094 /*require_all_args=*/true,
21095 /*use_default_args=*/true);
21096
21097 /* FIXME check for satisfaction in check_instantiated_args. */
21098 if (flag_concepts
21099 && !any_dependent_template_arguments_p (args)
21100 && !constraints_satisfied_p (tmpl, args))
21101 {
21102 if (complain & tf_error)
21103 {
21104 auto_diagnostic_group d;
21105 error ("template constraint failure for %qD", tmpl);
21106 diagnose_constraints (input_location, tmpl, args);
21107 }
21108 return error_mark_node;
21109 }
21110
21111 if (!push_tinst_level (tmpl, args))
21112 return error_mark_node;
21113 tree r = instantiate_template (tmpl, args, complain);
21114 pop_tinst_level ();
21115
21116 return r;
21117 }
21118
21119 /* PARM is a template parameter pack for FN. Returns true iff
21120 PARM is used in a deducible way in the argument list of FN. */
21121
21122 static bool
21123 pack_deducible_p (tree parm, tree fn)
21124 {
21125 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21126 for (; t; t = TREE_CHAIN (t))
21127 {
21128 tree type = TREE_VALUE (t);
21129 tree packs;
21130 if (!PACK_EXPANSION_P (type))
21131 continue;
21132 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21133 packs; packs = TREE_CHAIN (packs))
21134 if (template_args_equal (TREE_VALUE (packs), parm))
21135 {
21136 /* The template parameter pack is used in a function parameter
21137 pack. If this is the end of the parameter list, the
21138 template parameter pack is deducible. */
21139 if (TREE_CHAIN (t) == void_list_node)
21140 return true;
21141 else
21142 /* Otherwise, not. Well, it could be deduced from
21143 a non-pack parameter, but doing so would end up with
21144 a deduction mismatch, so don't bother. */
21145 return false;
21146 }
21147 }
21148 /* The template parameter pack isn't used in any function parameter
21149 packs, but it might be used deeper, e.g. tuple<Args...>. */
21150 return true;
21151 }
21152
21153 /* Subroutine of fn_type_unification: check non-dependent parms for
21154 convertibility. */
21155
21156 static int
21157 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21158 tree fn, unification_kind_t strict, int flags,
21159 struct conversion **convs, bool explain_p)
21160 {
21161 /* Non-constructor methods need to leave a conversion for 'this', which
21162 isn't included in nargs here. */
21163 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21164 && !DECL_CONSTRUCTOR_P (fn));
21165
21166 for (unsigned ia = 0;
21167 parms && parms != void_list_node && ia < nargs; )
21168 {
21169 tree parm = TREE_VALUE (parms);
21170
21171 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21172 && (!TREE_CHAIN (parms)
21173 || TREE_CHAIN (parms) == void_list_node))
21174 /* For a function parameter pack that occurs at the end of the
21175 parameter-declaration-list, the type A of each remaining
21176 argument of the call is compared with the type P of the
21177 declarator-id of the function parameter pack. */
21178 break;
21179
21180 parms = TREE_CHAIN (parms);
21181
21182 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21183 /* For a function parameter pack that does not occur at the
21184 end of the parameter-declaration-list, the type of the
21185 parameter pack is a non-deduced context. */
21186 continue;
21187
21188 if (!uses_template_parms (parm))
21189 {
21190 tree arg = args[ia];
21191 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21192 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21193
21194 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21195 conv_p, explain_p))
21196 return 1;
21197 }
21198
21199 ++ia;
21200 }
21201
21202 return 0;
21203 }
21204
21205 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21206 NARGS elements of the arguments that are being used when calling
21207 it. TARGS is a vector into which the deduced template arguments
21208 are placed.
21209
21210 Returns either a FUNCTION_DECL for the matching specialization of FN or
21211 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21212 true, diagnostics will be printed to explain why it failed.
21213
21214 If FN is a conversion operator, or we are trying to produce a specific
21215 specialization, RETURN_TYPE is the return type desired.
21216
21217 The EXPLICIT_TARGS are explicit template arguments provided via a
21218 template-id.
21219
21220 The parameter STRICT is one of:
21221
21222 DEDUCE_CALL:
21223 We are deducing arguments for a function call, as in
21224 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21225 deducing arguments for a call to the result of a conversion
21226 function template, as in [over.call.object].
21227
21228 DEDUCE_CONV:
21229 We are deducing arguments for a conversion function, as in
21230 [temp.deduct.conv].
21231
21232 DEDUCE_EXACT:
21233 We are deducing arguments when doing an explicit instantiation
21234 as in [temp.explicit], when determining an explicit specialization
21235 as in [temp.expl.spec], or when taking the address of a function
21236 template, as in [temp.deduct.funcaddr]. */
21237
21238 tree
21239 fn_type_unification (tree fn,
21240 tree explicit_targs,
21241 tree targs,
21242 const tree *args,
21243 unsigned int nargs,
21244 tree return_type,
21245 unification_kind_t strict,
21246 int flags,
21247 struct conversion **convs,
21248 bool explain_p,
21249 bool decltype_p)
21250 {
21251 tree parms;
21252 tree fntype;
21253 tree decl = NULL_TREE;
21254 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21255 bool ok;
21256 static int deduction_depth;
21257 /* type_unification_real will pass back any access checks from default
21258 template argument substitution. */
21259 vec<deferred_access_check, va_gc> *checks = NULL;
21260 /* We don't have all the template args yet. */
21261 bool incomplete = true;
21262
21263 tree orig_fn = fn;
21264 if (flag_new_inheriting_ctors)
21265 fn = strip_inheriting_ctors (fn);
21266
21267 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21268 tree r = error_mark_node;
21269
21270 tree full_targs = targs;
21271 if (TMPL_ARGS_DEPTH (targs)
21272 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21273 full_targs = (add_outermost_template_args
21274 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21275 targs));
21276
21277 if (decltype_p)
21278 complain |= tf_decltype;
21279
21280 /* In C++0x, it's possible to have a function template whose type depends
21281 on itself recursively. This is most obvious with decltype, but can also
21282 occur with enumeration scope (c++/48969). So we need to catch infinite
21283 recursion and reject the substitution at deduction time; this function
21284 will return error_mark_node for any repeated substitution.
21285
21286 This also catches excessive recursion such as when f<N> depends on
21287 f<N-1> across all integers, and returns error_mark_node for all the
21288 substitutions back up to the initial one.
21289
21290 This is, of course, not reentrant. */
21291 if (excessive_deduction_depth)
21292 return error_mark_node;
21293 ++deduction_depth;
21294
21295 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21296
21297 fntype = TREE_TYPE (fn);
21298 if (explicit_targs)
21299 {
21300 /* [temp.deduct]
21301
21302 The specified template arguments must match the template
21303 parameters in kind (i.e., type, nontype, template), and there
21304 must not be more arguments than there are parameters;
21305 otherwise type deduction fails.
21306
21307 Nontype arguments must match the types of the corresponding
21308 nontype template parameters, or must be convertible to the
21309 types of the corresponding nontype parameters as specified in
21310 _temp.arg.nontype_, otherwise type deduction fails.
21311
21312 All references in the function type of the function template
21313 to the corresponding template parameters are replaced by the
21314 specified template argument values. If a substitution in a
21315 template parameter or in the function type of the function
21316 template results in an invalid type, type deduction fails. */
21317 int i, len = TREE_VEC_LENGTH (tparms);
21318 location_t loc = input_location;
21319 incomplete = false;
21320
21321 if (explicit_targs == error_mark_node)
21322 goto fail;
21323
21324 if (TMPL_ARGS_DEPTH (explicit_targs)
21325 < TMPL_ARGS_DEPTH (full_targs))
21326 explicit_targs = add_outermost_template_args (full_targs,
21327 explicit_targs);
21328
21329 /* Adjust any explicit template arguments before entering the
21330 substitution context. */
21331 explicit_targs
21332 = (coerce_template_parms (tparms, explicit_targs, fn,
21333 complain|tf_partial,
21334 /*require_all_args=*/false,
21335 /*use_default_args=*/false));
21336 if (explicit_targs == error_mark_node)
21337 goto fail;
21338
21339 /* Substitute the explicit args into the function type. This is
21340 necessary so that, for instance, explicitly declared function
21341 arguments can match null pointed constants. If we were given
21342 an incomplete set of explicit args, we must not do semantic
21343 processing during substitution as we could create partial
21344 instantiations. */
21345 for (i = 0; i < len; i++)
21346 {
21347 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21348 bool parameter_pack = false;
21349 tree targ = TREE_VEC_ELT (explicit_targs, i);
21350
21351 /* Dig out the actual parm. */
21352 if (TREE_CODE (parm) == TYPE_DECL
21353 || TREE_CODE (parm) == TEMPLATE_DECL)
21354 {
21355 parm = TREE_TYPE (parm);
21356 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21357 }
21358 else if (TREE_CODE (parm) == PARM_DECL)
21359 {
21360 parm = DECL_INITIAL (parm);
21361 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21362 }
21363
21364 if (targ == NULL_TREE)
21365 /* No explicit argument for this template parameter. */
21366 incomplete = true;
21367 else if (parameter_pack && pack_deducible_p (parm, fn))
21368 {
21369 /* Mark the argument pack as "incomplete". We could
21370 still deduce more arguments during unification.
21371 We remove this mark in type_unification_real. */
21372 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21373 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21374 = ARGUMENT_PACK_ARGS (targ);
21375
21376 /* We have some incomplete argument packs. */
21377 incomplete = true;
21378 }
21379 }
21380
21381 if (incomplete)
21382 {
21383 if (!push_tinst_level (fn, explicit_targs))
21384 {
21385 excessive_deduction_depth = true;
21386 goto fail;
21387 }
21388 ++processing_template_decl;
21389 input_location = DECL_SOURCE_LOCATION (fn);
21390 /* Ignore any access checks; we'll see them again in
21391 instantiate_template and they might have the wrong
21392 access path at this point. */
21393 push_deferring_access_checks (dk_deferred);
21394 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21395 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21396 pop_deferring_access_checks ();
21397 input_location = loc;
21398 --processing_template_decl;
21399 pop_tinst_level ();
21400
21401 if (fntype == error_mark_node)
21402 goto fail;
21403 }
21404
21405 /* Place the explicitly specified arguments in TARGS. */
21406 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21407 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21408 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21409 if (!incomplete && CHECKING_P
21410 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21411 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21412 (targs, NUM_TMPL_ARGS (explicit_targs));
21413 }
21414
21415 if (return_type && strict != DEDUCE_CALL)
21416 {
21417 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21418 new_args[0] = return_type;
21419 memcpy (new_args + 1, args, nargs * sizeof (tree));
21420 args = new_args;
21421 ++nargs;
21422 }
21423
21424 if (!incomplete)
21425 goto deduced;
21426
21427 /* Never do unification on the 'this' parameter. */
21428 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21429
21430 if (return_type && strict == DEDUCE_CALL)
21431 {
21432 /* We're deducing for a call to the result of a template conversion
21433 function. The parms we really want are in return_type. */
21434 if (INDIRECT_TYPE_P (return_type))
21435 return_type = TREE_TYPE (return_type);
21436 parms = TYPE_ARG_TYPES (return_type);
21437 }
21438 else if (return_type)
21439 {
21440 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21441 }
21442
21443 /* We allow incomplete unification without an error message here
21444 because the standard doesn't seem to explicitly prohibit it. Our
21445 callers must be ready to deal with unification failures in any
21446 event. */
21447
21448 /* If we aren't explaining yet, push tinst context so we can see where
21449 any errors (e.g. from class instantiations triggered by instantiation
21450 of default template arguments) come from. If we are explaining, this
21451 context is redundant. */
21452 if (!explain_p && !push_tinst_level (fn, targs))
21453 {
21454 excessive_deduction_depth = true;
21455 goto fail;
21456 }
21457
21458 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21459 full_targs, parms, args, nargs, /*subr=*/0,
21460 strict, &checks, explain_p);
21461 if (!explain_p)
21462 pop_tinst_level ();
21463 if (!ok)
21464 goto fail;
21465
21466 /* Now that we have bindings for all of the template arguments,
21467 ensure that the arguments deduced for the template template
21468 parameters have compatible template parameter lists. We cannot
21469 check this property before we have deduced all template
21470 arguments, because the template parameter types of a template
21471 template parameter might depend on prior template parameters
21472 deduced after the template template parameter. The following
21473 ill-formed example illustrates this issue:
21474
21475 template<typename T, template<T> class C> void f(C<5>, T);
21476
21477 template<int N> struct X {};
21478
21479 void g() {
21480 f(X<5>(), 5l); // error: template argument deduction fails
21481 }
21482
21483 The template parameter list of 'C' depends on the template type
21484 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21485 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21486 time that we deduce 'C'. */
21487 if (!template_template_parm_bindings_ok_p
21488 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21489 {
21490 unify_inconsistent_template_template_parameters (explain_p);
21491 goto fail;
21492 }
21493
21494 deduced:
21495
21496 /* CWG2369: Check satisfaction before non-deducible conversions. */
21497 if (!constraints_satisfied_p (fn, targs))
21498 {
21499 if (explain_p)
21500 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21501 goto fail;
21502 }
21503
21504 /* DR 1391: All parameters have args, now check non-dependent parms for
21505 convertibility. We don't do this if all args were explicitly specified,
21506 as the standard says that we substitute explicit args immediately. */
21507 if (incomplete
21508 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21509 convs, explain_p))
21510 goto fail;
21511
21512 /* All is well so far. Now, check:
21513
21514 [temp.deduct]
21515
21516 When all template arguments have been deduced, all uses of
21517 template parameters in nondeduced contexts are replaced with
21518 the corresponding deduced argument values. If the
21519 substitution results in an invalid type, as described above,
21520 type deduction fails. */
21521 if (!push_tinst_level (fn, targs))
21522 {
21523 excessive_deduction_depth = true;
21524 goto fail;
21525 }
21526
21527 /* Also collect access checks from the instantiation. */
21528 reopen_deferring_access_checks (checks);
21529
21530 decl = instantiate_template (fn, targs, complain);
21531
21532 checks = get_deferred_access_checks ();
21533 pop_deferring_access_checks ();
21534
21535 pop_tinst_level ();
21536
21537 if (decl == error_mark_node)
21538 goto fail;
21539
21540 /* Now perform any access checks encountered during substitution. */
21541 push_access_scope (decl);
21542 ok = perform_access_checks (checks, complain);
21543 pop_access_scope (decl);
21544 if (!ok)
21545 goto fail;
21546
21547 /* If we're looking for an exact match, check that what we got
21548 is indeed an exact match. It might not be if some template
21549 parameters are used in non-deduced contexts. But don't check
21550 for an exact match if we have dependent template arguments;
21551 in that case we're doing partial ordering, and we already know
21552 that we have two candidates that will provide the actual type. */
21553 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21554 {
21555 tree substed = TREE_TYPE (decl);
21556 unsigned int i;
21557
21558 tree sarg
21559 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21560 if (return_type)
21561 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21562 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21563 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21564 {
21565 unify_type_mismatch (explain_p, args[i],
21566 TREE_VALUE (sarg));
21567 goto fail;
21568 }
21569 }
21570
21571 /* After doing deduction with the inherited constructor, actually return an
21572 instantiation of the inheriting constructor. */
21573 if (orig_fn != fn)
21574 decl = instantiate_template (orig_fn, targs, complain);
21575
21576 r = decl;
21577
21578 fail:
21579 --deduction_depth;
21580 if (excessive_deduction_depth)
21581 {
21582 if (deduction_depth == 0)
21583 /* Reset once we're all the way out. */
21584 excessive_deduction_depth = false;
21585 }
21586
21587 return r;
21588 }
21589
21590 /* Adjust types before performing type deduction, as described in
21591 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21592 sections are symmetric. PARM is the type of a function parameter
21593 or the return type of the conversion function. ARG is the type of
21594 the argument passed to the call, or the type of the value
21595 initialized with the result of the conversion function.
21596 ARG_EXPR is the original argument expression, which may be null. */
21597
21598 static int
21599 maybe_adjust_types_for_deduction (unification_kind_t strict,
21600 tree* parm,
21601 tree* arg,
21602 tree arg_expr)
21603 {
21604 int result = 0;
21605
21606 switch (strict)
21607 {
21608 case DEDUCE_CALL:
21609 break;
21610
21611 case DEDUCE_CONV:
21612 /* Swap PARM and ARG throughout the remainder of this
21613 function; the handling is precisely symmetric since PARM
21614 will initialize ARG rather than vice versa. */
21615 std::swap (parm, arg);
21616 break;
21617
21618 case DEDUCE_EXACT:
21619 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21620 too, but here handle it by stripping the reference from PARM
21621 rather than by adding it to ARG. */
21622 if (TYPE_REF_P (*parm)
21623 && TYPE_REF_IS_RVALUE (*parm)
21624 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21625 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21626 && TYPE_REF_P (*arg)
21627 && !TYPE_REF_IS_RVALUE (*arg))
21628 *parm = TREE_TYPE (*parm);
21629 /* Nothing else to do in this case. */
21630 return 0;
21631
21632 default:
21633 gcc_unreachable ();
21634 }
21635
21636 if (!TYPE_REF_P (*parm))
21637 {
21638 /* [temp.deduct.call]
21639
21640 If P is not a reference type:
21641
21642 --If A is an array type, the pointer type produced by the
21643 array-to-pointer standard conversion (_conv.array_) is
21644 used in place of A for type deduction; otherwise,
21645
21646 --If A is a function type, the pointer type produced by
21647 the function-to-pointer standard conversion
21648 (_conv.func_) is used in place of A for type deduction;
21649 otherwise,
21650
21651 --If A is a cv-qualified type, the top level
21652 cv-qualifiers of A's type are ignored for type
21653 deduction. */
21654 if (TREE_CODE (*arg) == ARRAY_TYPE)
21655 *arg = build_pointer_type (TREE_TYPE (*arg));
21656 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21657 *arg = build_pointer_type (*arg);
21658 else
21659 *arg = TYPE_MAIN_VARIANT (*arg);
21660 }
21661
21662 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21663 reference to a cv-unqualified template parameter that does not represent a
21664 template parameter of a class template (during class template argument
21665 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21666 an lvalue, the type "lvalue reference to A" is used in place of A for type
21667 deduction. */
21668 if (TYPE_REF_P (*parm)
21669 && TYPE_REF_IS_RVALUE (*parm)
21670 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21671 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21672 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21673 && (arg_expr ? lvalue_p (arg_expr)
21674 /* try_one_overload doesn't provide an arg_expr, but
21675 functions are always lvalues. */
21676 : TREE_CODE (*arg) == FUNCTION_TYPE))
21677 *arg = build_reference_type (*arg);
21678
21679 /* [temp.deduct.call]
21680
21681 If P is a cv-qualified type, the top level cv-qualifiers
21682 of P's type are ignored for type deduction. If P is a
21683 reference type, the type referred to by P is used for
21684 type deduction. */
21685 *parm = TYPE_MAIN_VARIANT (*parm);
21686 if (TYPE_REF_P (*parm))
21687 {
21688 *parm = TREE_TYPE (*parm);
21689 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21690 }
21691
21692 /* DR 322. For conversion deduction, remove a reference type on parm
21693 too (which has been swapped into ARG). */
21694 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21695 *arg = TREE_TYPE (*arg);
21696
21697 return result;
21698 }
21699
21700 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21701 template which doesn't contain any deducible template parameters; check if
21702 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21703 unify_one_argument. */
21704
21705 static int
21706 check_non_deducible_conversion (tree parm, tree arg, int strict,
21707 int flags, struct conversion **conv_p,
21708 bool explain_p)
21709 {
21710 tree type;
21711
21712 if (!TYPE_P (arg))
21713 type = TREE_TYPE (arg);
21714 else
21715 type = arg;
21716
21717 if (same_type_p (parm, type))
21718 return unify_success (explain_p);
21719
21720 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21721 if (strict == DEDUCE_CONV)
21722 {
21723 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21724 return unify_success (explain_p);
21725 }
21726 else if (strict != DEDUCE_EXACT)
21727 {
21728 bool ok = false;
21729 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21730 if (conv_p)
21731 /* Avoid recalculating this in add_function_candidate. */
21732 ok = (*conv_p
21733 = good_conversion (parm, type, conv_arg, flags, complain));
21734 else
21735 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21736 if (ok)
21737 return unify_success (explain_p);
21738 }
21739
21740 if (strict == DEDUCE_EXACT)
21741 return unify_type_mismatch (explain_p, parm, arg);
21742 else
21743 return unify_arg_conversion (explain_p, parm, type, arg);
21744 }
21745
21746 static bool uses_deducible_template_parms (tree type);
21747
21748 /* Returns true iff the expression EXPR is one from which a template
21749 argument can be deduced. In other words, if it's an undecorated
21750 use of a template non-type parameter. */
21751
21752 static bool
21753 deducible_expression (tree expr)
21754 {
21755 /* Strip implicit conversions. */
21756 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21757 expr = TREE_OPERAND (expr, 0);
21758 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21759 }
21760
21761 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21762 deducible way; that is, if it has a max value of <PARM> - 1. */
21763
21764 static bool
21765 deducible_array_bound (tree domain)
21766 {
21767 if (domain == NULL_TREE)
21768 return false;
21769
21770 tree max = TYPE_MAX_VALUE (domain);
21771 if (TREE_CODE (max) != MINUS_EXPR)
21772 return false;
21773
21774 return deducible_expression (TREE_OPERAND (max, 0));
21775 }
21776
21777 /* Returns true iff the template arguments ARGS use a template parameter
21778 in a deducible way. */
21779
21780 static bool
21781 deducible_template_args (tree args)
21782 {
21783 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21784 {
21785 bool deducible;
21786 tree elt = TREE_VEC_ELT (args, i);
21787 if (ARGUMENT_PACK_P (elt))
21788 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21789 else
21790 {
21791 if (PACK_EXPANSION_P (elt))
21792 elt = PACK_EXPANSION_PATTERN (elt);
21793 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21794 deducible = true;
21795 else if (TYPE_P (elt))
21796 deducible = uses_deducible_template_parms (elt);
21797 else
21798 deducible = deducible_expression (elt);
21799 }
21800 if (deducible)
21801 return true;
21802 }
21803 return false;
21804 }
21805
21806 /* Returns true iff TYPE contains any deducible references to template
21807 parameters, as per 14.8.2.5. */
21808
21809 static bool
21810 uses_deducible_template_parms (tree type)
21811 {
21812 if (PACK_EXPANSION_P (type))
21813 type = PACK_EXPANSION_PATTERN (type);
21814
21815 /* T
21816 cv-list T
21817 TT<T>
21818 TT<i>
21819 TT<> */
21820 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21821 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21822 return true;
21823
21824 /* T*
21825 T&
21826 T&& */
21827 if (INDIRECT_TYPE_P (type))
21828 return uses_deducible_template_parms (TREE_TYPE (type));
21829
21830 /* T[integer-constant ]
21831 type [i] */
21832 if (TREE_CODE (type) == ARRAY_TYPE)
21833 return (uses_deducible_template_parms (TREE_TYPE (type))
21834 || deducible_array_bound (TYPE_DOMAIN (type)));
21835
21836 /* T type ::*
21837 type T::*
21838 T T::*
21839 T (type ::*)()
21840 type (T::*)()
21841 type (type ::*)(T)
21842 type (T::*)(T)
21843 T (type ::*)(T)
21844 T (T::*)()
21845 T (T::*)(T) */
21846 if (TYPE_PTRMEM_P (type))
21847 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21848 || (uses_deducible_template_parms
21849 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21850
21851 /* template-name <T> (where template-name refers to a class template)
21852 template-name <i> (where template-name refers to a class template) */
21853 if (CLASS_TYPE_P (type)
21854 && CLASSTYPE_TEMPLATE_INFO (type)
21855 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21856 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21857 (CLASSTYPE_TI_ARGS (type)));
21858
21859 /* type (T)
21860 T()
21861 T(T) */
21862 if (FUNC_OR_METHOD_TYPE_P (type))
21863 {
21864 if (uses_deducible_template_parms (TREE_TYPE (type)))
21865 return true;
21866 tree parm = TYPE_ARG_TYPES (type);
21867 if (TREE_CODE (type) == METHOD_TYPE)
21868 parm = TREE_CHAIN (parm);
21869 for (; parm; parm = TREE_CHAIN (parm))
21870 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21871 return true;
21872 }
21873
21874 return false;
21875 }
21876
21877 /* Subroutine of type_unification_real and unify_pack_expansion to
21878 handle unification of a single P/A pair. Parameters are as
21879 for those functions. */
21880
21881 static int
21882 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21883 int subr, unification_kind_t strict,
21884 bool explain_p)
21885 {
21886 tree arg_expr = NULL_TREE;
21887 int arg_strict;
21888
21889 if (arg == error_mark_node || parm == error_mark_node)
21890 return unify_invalid (explain_p);
21891 if (arg == unknown_type_node)
21892 /* We can't deduce anything from this, but we might get all the
21893 template args from other function args. */
21894 return unify_success (explain_p);
21895
21896 /* Implicit conversions (Clause 4) will be performed on a function
21897 argument to convert it to the type of the corresponding function
21898 parameter if the parameter type contains no template-parameters that
21899 participate in template argument deduction. */
21900 if (strict != DEDUCE_EXACT
21901 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21902 /* For function parameters with no deducible template parameters,
21903 just return. We'll check non-dependent conversions later. */
21904 return unify_success (explain_p);
21905
21906 switch (strict)
21907 {
21908 case DEDUCE_CALL:
21909 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21910 | UNIFY_ALLOW_MORE_CV_QUAL
21911 | UNIFY_ALLOW_DERIVED);
21912 break;
21913
21914 case DEDUCE_CONV:
21915 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21916 break;
21917
21918 case DEDUCE_EXACT:
21919 arg_strict = UNIFY_ALLOW_NONE;
21920 break;
21921
21922 default:
21923 gcc_unreachable ();
21924 }
21925
21926 /* We only do these transformations if this is the top-level
21927 parameter_type_list in a call or declaration matching; in other
21928 situations (nested function declarators, template argument lists) we
21929 won't be comparing a type to an expression, and we don't do any type
21930 adjustments. */
21931 if (!subr)
21932 {
21933 if (!TYPE_P (arg))
21934 {
21935 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21936 if (type_unknown_p (arg))
21937 {
21938 /* [temp.deduct.type] A template-argument can be
21939 deduced from a pointer to function or pointer
21940 to member function argument if the set of
21941 overloaded functions does not contain function
21942 templates and at most one of a set of
21943 overloaded functions provides a unique
21944 match. */
21945 resolve_overloaded_unification (tparms, targs, parm,
21946 arg, strict,
21947 arg_strict, explain_p);
21948 /* If a unique match was not found, this is a
21949 non-deduced context, so we still succeed. */
21950 return unify_success (explain_p);
21951 }
21952
21953 arg_expr = arg;
21954 arg = unlowered_expr_type (arg);
21955 if (arg == error_mark_node)
21956 return unify_invalid (explain_p);
21957 }
21958
21959 arg_strict |=
21960 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21961 }
21962 else
21963 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21964 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21965 return unify_template_argument_mismatch (explain_p, parm, arg);
21966
21967 /* For deduction from an init-list we need the actual list. */
21968 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21969 arg = arg_expr;
21970 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21971 }
21972
21973 /* for_each_template_parm callback that always returns 0. */
21974
21975 static int
21976 zero_r (tree, void *)
21977 {
21978 return 0;
21979 }
21980
21981 /* for_each_template_parm any_fn callback to handle deduction of a template
21982 type argument from the type of an array bound. */
21983
21984 static int
21985 array_deduction_r (tree t, void *data)
21986 {
21987 tree_pair_p d = (tree_pair_p)data;
21988 tree &tparms = d->purpose;
21989 tree &targs = d->value;
21990
21991 if (TREE_CODE (t) == ARRAY_TYPE)
21992 if (tree dom = TYPE_DOMAIN (t))
21993 if (tree max = TYPE_MAX_VALUE (dom))
21994 {
21995 if (TREE_CODE (max) == MINUS_EXPR)
21996 max = TREE_OPERAND (max, 0);
21997 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21998 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21999 UNIFY_ALLOW_NONE, /*explain*/false);
22000 }
22001
22002 /* Keep walking. */
22003 return 0;
22004 }
22005
22006 /* Try to deduce any not-yet-deduced template type arguments from the type of
22007 an array bound. This is handled separately from unify because 14.8.2.5 says
22008 "The type of a type parameter is only deduced from an array bound if it is
22009 not otherwise deduced." */
22010
22011 static void
22012 try_array_deduction (tree tparms, tree targs, tree parm)
22013 {
22014 tree_pair_s data = { tparms, targs };
22015 hash_set<tree> visited;
22016 for_each_template_parm (parm, zero_r, &data, &visited,
22017 /*nondeduced*/false, array_deduction_r);
22018 }
22019
22020 /* Most parms like fn_type_unification.
22021
22022 If SUBR is 1, we're being called recursively (to unify the
22023 arguments of a function or method parameter of a function
22024 template).
22025
22026 CHECKS is a pointer to a vector of access checks encountered while
22027 substituting default template arguments. */
22028
22029 static int
22030 type_unification_real (tree tparms,
22031 tree full_targs,
22032 tree xparms,
22033 const tree *xargs,
22034 unsigned int xnargs,
22035 int subr,
22036 unification_kind_t strict,
22037 vec<deferred_access_check, va_gc> **checks,
22038 bool explain_p)
22039 {
22040 tree parm, arg;
22041 int i;
22042 int ntparms = TREE_VEC_LENGTH (tparms);
22043 int saw_undeduced = 0;
22044 tree parms;
22045 const tree *args;
22046 unsigned int nargs;
22047 unsigned int ia;
22048
22049 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22050 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22051 gcc_assert (ntparms > 0);
22052
22053 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22054
22055 /* Reset the number of non-defaulted template arguments contained
22056 in TARGS. */
22057 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22058
22059 again:
22060 parms = xparms;
22061 args = xargs;
22062 nargs = xnargs;
22063
22064 ia = 0;
22065 while (parms && parms != void_list_node
22066 && ia < nargs)
22067 {
22068 parm = TREE_VALUE (parms);
22069
22070 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22071 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22072 /* For a function parameter pack that occurs at the end of the
22073 parameter-declaration-list, the type A of each remaining
22074 argument of the call is compared with the type P of the
22075 declarator-id of the function parameter pack. */
22076 break;
22077
22078 parms = TREE_CHAIN (parms);
22079
22080 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22081 /* For a function parameter pack that does not occur at the
22082 end of the parameter-declaration-list, the type of the
22083 parameter pack is a non-deduced context. */
22084 continue;
22085
22086 arg = args[ia];
22087 ++ia;
22088
22089 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22090 explain_p))
22091 return 1;
22092 }
22093
22094 if (parms
22095 && parms != void_list_node
22096 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22097 {
22098 /* Unify the remaining arguments with the pack expansion type. */
22099 tree argvec;
22100 tree parmvec = make_tree_vec (1);
22101
22102 /* Allocate a TREE_VEC and copy in all of the arguments */
22103 argvec = make_tree_vec (nargs - ia);
22104 for (i = 0; ia < nargs; ++ia, ++i)
22105 TREE_VEC_ELT (argvec, i) = args[ia];
22106
22107 /* Copy the parameter into parmvec. */
22108 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22109 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22110 /*subr=*/subr, explain_p))
22111 return 1;
22112
22113 /* Advance to the end of the list of parameters. */
22114 parms = TREE_CHAIN (parms);
22115 }
22116
22117 /* Fail if we've reached the end of the parm list, and more args
22118 are present, and the parm list isn't variadic. */
22119 if (ia < nargs && parms == void_list_node)
22120 return unify_too_many_arguments (explain_p, nargs, ia);
22121 /* Fail if parms are left and they don't have default values and
22122 they aren't all deduced as empty packs (c++/57397). This is
22123 consistent with sufficient_parms_p. */
22124 if (parms && parms != void_list_node
22125 && TREE_PURPOSE (parms) == NULL_TREE)
22126 {
22127 unsigned int count = nargs;
22128 tree p = parms;
22129 bool type_pack_p;
22130 do
22131 {
22132 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22133 if (!type_pack_p)
22134 count++;
22135 p = TREE_CHAIN (p);
22136 }
22137 while (p && p != void_list_node);
22138 if (count != nargs)
22139 return unify_too_few_arguments (explain_p, ia, count,
22140 type_pack_p);
22141 }
22142
22143 if (!subr)
22144 {
22145 tsubst_flags_t complain = (explain_p
22146 ? tf_warning_or_error
22147 : tf_none);
22148 bool tried_array_deduction = (cxx_dialect < cxx17);
22149
22150 for (i = 0; i < ntparms; i++)
22151 {
22152 tree targ = TREE_VEC_ELT (targs, i);
22153 tree tparm = TREE_VEC_ELT (tparms, i);
22154
22155 /* Clear the "incomplete" flags on all argument packs now so that
22156 substituting them into later default arguments works. */
22157 if (targ && ARGUMENT_PACK_P (targ))
22158 {
22159 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22160 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22161 }
22162
22163 if (targ || tparm == error_mark_node)
22164 continue;
22165 tparm = TREE_VALUE (tparm);
22166
22167 if (TREE_CODE (tparm) == TYPE_DECL
22168 && !tried_array_deduction)
22169 {
22170 try_array_deduction (tparms, targs, xparms);
22171 tried_array_deduction = true;
22172 if (TREE_VEC_ELT (targs, i))
22173 continue;
22174 }
22175
22176 /* If this is an undeduced nontype parameter that depends on
22177 a type parameter, try another pass; its type may have been
22178 deduced from a later argument than the one from which
22179 this parameter can be deduced. */
22180 if (TREE_CODE (tparm) == PARM_DECL
22181 && uses_template_parms (TREE_TYPE (tparm))
22182 && saw_undeduced < 2)
22183 {
22184 saw_undeduced = 1;
22185 continue;
22186 }
22187
22188 /* Core issue #226 (C++0x) [temp.deduct]:
22189
22190 If a template argument has not been deduced, its
22191 default template argument, if any, is used.
22192
22193 When we are in C++98 mode, TREE_PURPOSE will either
22194 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22195 to explicitly check cxx_dialect here. */
22196 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22197 /* OK, there is a default argument. Wait until after the
22198 conversion check to do substitution. */
22199 continue;
22200
22201 /* If the type parameter is a parameter pack, then it will
22202 be deduced to an empty parameter pack. */
22203 if (template_parameter_pack_p (tparm))
22204 {
22205 tree arg;
22206
22207 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22208 {
22209 arg = make_node (NONTYPE_ARGUMENT_PACK);
22210 TREE_CONSTANT (arg) = 1;
22211 }
22212 else
22213 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22214
22215 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22216
22217 TREE_VEC_ELT (targs, i) = arg;
22218 continue;
22219 }
22220
22221 return unify_parameter_deduction_failure (explain_p, tparm);
22222 }
22223
22224 /* Now substitute into the default template arguments. */
22225 for (i = 0; i < ntparms; i++)
22226 {
22227 tree targ = TREE_VEC_ELT (targs, i);
22228 tree tparm = TREE_VEC_ELT (tparms, i);
22229
22230 if (targ || tparm == error_mark_node)
22231 continue;
22232 tree parm = TREE_VALUE (tparm);
22233 tree arg = TREE_PURPOSE (tparm);
22234 reopen_deferring_access_checks (*checks);
22235 location_t save_loc = input_location;
22236 if (DECL_P (parm))
22237 input_location = DECL_SOURCE_LOCATION (parm);
22238
22239 if (saw_undeduced == 1
22240 && TREE_CODE (parm) == PARM_DECL
22241 && uses_template_parms (TREE_TYPE (parm)))
22242 {
22243 /* The type of this non-type parameter depends on undeduced
22244 parameters. Don't try to use its default argument yet,
22245 since we might deduce an argument for it on the next pass,
22246 but do check whether the arguments we already have cause
22247 substitution failure, so that that happens before we try
22248 later default arguments (78489). */
22249 ++processing_template_decl;
22250 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22251 NULL_TREE);
22252 --processing_template_decl;
22253 if (type == error_mark_node)
22254 arg = error_mark_node;
22255 else
22256 arg = NULL_TREE;
22257 }
22258 else
22259 {
22260 /* Even if the call is happening in template context, getting
22261 here means it's non-dependent, and a default argument is
22262 considered a separate definition under [temp.decls], so we can
22263 do this substitution without processing_template_decl. This
22264 is important if the default argument contains something that
22265 might be instantiation-dependent like access (87480). */
22266 processing_template_decl_sentinel s;
22267 tree substed = NULL_TREE;
22268 if (saw_undeduced == 1)
22269 {
22270 /* First instatiate in template context, in case we still
22271 depend on undeduced template parameters. */
22272 ++processing_template_decl;
22273 substed = tsubst_template_arg (arg, full_targs, complain,
22274 NULL_TREE);
22275 --processing_template_decl;
22276 if (substed != error_mark_node
22277 && !uses_template_parms (substed))
22278 /* We replaced all the tparms, substitute again out of
22279 template context. */
22280 substed = NULL_TREE;
22281 }
22282 if (!substed)
22283 substed = tsubst_template_arg (arg, full_targs, complain,
22284 NULL_TREE);
22285
22286 if (!uses_template_parms (substed))
22287 arg = convert_template_argument (parm, substed, full_targs,
22288 complain, i, NULL_TREE);
22289 else if (saw_undeduced == 1)
22290 arg = NULL_TREE;
22291 else
22292 arg = error_mark_node;
22293 }
22294
22295 input_location = save_loc;
22296 *checks = get_deferred_access_checks ();
22297 pop_deferring_access_checks ();
22298
22299 if (arg == error_mark_node)
22300 return 1;
22301 else if (arg)
22302 {
22303 TREE_VEC_ELT (targs, i) = arg;
22304 /* The position of the first default template argument,
22305 is also the number of non-defaulted arguments in TARGS.
22306 Record that. */
22307 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22309 }
22310 }
22311
22312 if (saw_undeduced++ == 1)
22313 goto again;
22314 }
22315
22316 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22317 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22318
22319 return unify_success (explain_p);
22320 }
22321
22322 /* Subroutine of type_unification_real. Args are like the variables
22323 at the call site. ARG is an overloaded function (or template-id);
22324 we try deducing template args from each of the overloads, and if
22325 only one succeeds, we go with that. Modifies TARGS and returns
22326 true on success. */
22327
22328 static bool
22329 resolve_overloaded_unification (tree tparms,
22330 tree targs,
22331 tree parm,
22332 tree arg,
22333 unification_kind_t strict,
22334 int sub_strict,
22335 bool explain_p)
22336 {
22337 tree tempargs = copy_node (targs);
22338 int good = 0;
22339 tree goodfn = NULL_TREE;
22340 bool addr_p;
22341
22342 if (TREE_CODE (arg) == ADDR_EXPR)
22343 {
22344 arg = TREE_OPERAND (arg, 0);
22345 addr_p = true;
22346 }
22347 else
22348 addr_p = false;
22349
22350 if (TREE_CODE (arg) == COMPONENT_REF)
22351 /* Handle `&x' where `x' is some static or non-static member
22352 function name. */
22353 arg = TREE_OPERAND (arg, 1);
22354
22355 if (TREE_CODE (arg) == OFFSET_REF)
22356 arg = TREE_OPERAND (arg, 1);
22357
22358 /* Strip baselink information. */
22359 if (BASELINK_P (arg))
22360 arg = BASELINK_FUNCTIONS (arg);
22361
22362 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22363 {
22364 /* If we got some explicit template args, we need to plug them into
22365 the affected templates before we try to unify, in case the
22366 explicit args will completely resolve the templates in question. */
22367
22368 int ok = 0;
22369 tree expl_subargs = TREE_OPERAND (arg, 1);
22370 arg = TREE_OPERAND (arg, 0);
22371
22372 for (lkp_iterator iter (arg); iter; ++iter)
22373 {
22374 tree fn = *iter;
22375 tree subargs, elem;
22376
22377 if (TREE_CODE (fn) != TEMPLATE_DECL)
22378 continue;
22379
22380 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22381 expl_subargs, NULL_TREE, tf_none,
22382 /*require_all_args=*/true,
22383 /*use_default_args=*/true);
22384 if (subargs != error_mark_node
22385 && !any_dependent_template_arguments_p (subargs))
22386 {
22387 fn = instantiate_template (fn, subargs, tf_none);
22388 if (!constraints_satisfied_p (fn))
22389 continue;
22390 if (undeduced_auto_decl (fn))
22391 {
22392 /* Instantiate the function to deduce its return type. */
22393 ++function_depth;
22394 instantiate_decl (fn, /*defer*/false, /*class*/false);
22395 --function_depth;
22396 }
22397
22398 if (flag_noexcept_type)
22399 maybe_instantiate_noexcept (fn, tf_none);
22400
22401 elem = TREE_TYPE (fn);
22402 if (try_one_overload (tparms, targs, tempargs, parm,
22403 elem, strict, sub_strict, addr_p, explain_p)
22404 && (!goodfn || !same_type_p (goodfn, elem)))
22405 {
22406 goodfn = elem;
22407 ++good;
22408 }
22409 }
22410 else if (subargs)
22411 ++ok;
22412 }
22413 /* If no templates (or more than one) are fully resolved by the
22414 explicit arguments, this template-id is a non-deduced context; it
22415 could still be OK if we deduce all template arguments for the
22416 enclosing call through other arguments. */
22417 if (good != 1)
22418 good = ok;
22419 }
22420 else if (!OVL_P (arg))
22421 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22422 -- but the deduction does not succeed because the expression is
22423 not just the function on its own. */
22424 return false;
22425 else
22426 for (lkp_iterator iter (arg); iter; ++iter)
22427 {
22428 tree fn = *iter;
22429 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22430 strict, sub_strict, addr_p, explain_p)
22431 && (!goodfn || !decls_match (goodfn, fn)))
22432 {
22433 goodfn = fn;
22434 ++good;
22435 }
22436 }
22437
22438 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22439 to function or pointer to member function argument if the set of
22440 overloaded functions does not contain function templates and at most
22441 one of a set of overloaded functions provides a unique match.
22442
22443 So if we found multiple possibilities, we return success but don't
22444 deduce anything. */
22445
22446 if (good == 1)
22447 {
22448 int i = TREE_VEC_LENGTH (targs);
22449 for (; i--; )
22450 if (TREE_VEC_ELT (tempargs, i))
22451 {
22452 tree old = TREE_VEC_ELT (targs, i);
22453 tree new_ = TREE_VEC_ELT (tempargs, i);
22454 if (new_ && old && ARGUMENT_PACK_P (old)
22455 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22456 /* Don't forget explicit template arguments in a pack. */
22457 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22458 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22459 TREE_VEC_ELT (targs, i) = new_;
22460 }
22461 }
22462 if (good)
22463 return true;
22464
22465 return false;
22466 }
22467
22468 /* Core DR 115: In contexts where deduction is done and fails, or in
22469 contexts where deduction is not done, if a template argument list is
22470 specified and it, along with any default template arguments, identifies
22471 a single function template specialization, then the template-id is an
22472 lvalue for the function template specialization. */
22473
22474 tree
22475 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22476 {
22477 tree expr, offset, baselink;
22478 bool addr;
22479
22480 if (!type_unknown_p (orig_expr))
22481 return orig_expr;
22482
22483 expr = orig_expr;
22484 addr = false;
22485 offset = NULL_TREE;
22486 baselink = NULL_TREE;
22487
22488 if (TREE_CODE (expr) == ADDR_EXPR)
22489 {
22490 expr = TREE_OPERAND (expr, 0);
22491 addr = true;
22492 }
22493 if (TREE_CODE (expr) == OFFSET_REF)
22494 {
22495 offset = expr;
22496 expr = TREE_OPERAND (expr, 1);
22497 }
22498 if (BASELINK_P (expr))
22499 {
22500 baselink = expr;
22501 expr = BASELINK_FUNCTIONS (expr);
22502 }
22503
22504 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22505 {
22506 int good = 0;
22507 tree goodfn = NULL_TREE;
22508
22509 /* If we got some explicit template args, we need to plug them into
22510 the affected templates before we try to unify, in case the
22511 explicit args will completely resolve the templates in question. */
22512
22513 tree expl_subargs = TREE_OPERAND (expr, 1);
22514 tree arg = TREE_OPERAND (expr, 0);
22515 tree badfn = NULL_TREE;
22516 tree badargs = NULL_TREE;
22517
22518 for (lkp_iterator iter (arg); iter; ++iter)
22519 {
22520 tree fn = *iter;
22521 tree subargs, elem;
22522
22523 if (TREE_CODE (fn) != TEMPLATE_DECL)
22524 continue;
22525
22526 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22527 expl_subargs, NULL_TREE, tf_none,
22528 /*require_all_args=*/true,
22529 /*use_default_args=*/true);
22530 if (subargs != error_mark_node
22531 && !any_dependent_template_arguments_p (subargs))
22532 {
22533 elem = instantiate_template (fn, subargs, tf_none);
22534 if (elem == error_mark_node)
22535 {
22536 badfn = fn;
22537 badargs = subargs;
22538 }
22539 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22540 && constraints_satisfied_p (elem))
22541 {
22542 goodfn = elem;
22543 ++good;
22544 }
22545 }
22546 }
22547 if (good == 1)
22548 {
22549 mark_used (goodfn);
22550 expr = goodfn;
22551 if (baselink)
22552 expr = build_baselink (BASELINK_BINFO (baselink),
22553 BASELINK_ACCESS_BINFO (baselink),
22554 expr, BASELINK_OPTYPE (baselink));
22555 if (offset)
22556 {
22557 tree base
22558 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22559 expr = build_offset_ref (base, expr, addr, complain);
22560 }
22561 if (addr)
22562 expr = cp_build_addr_expr (expr, complain);
22563 return expr;
22564 }
22565 else if (good == 0 && badargs && (complain & tf_error))
22566 /* There were no good options and at least one bad one, so let the
22567 user know what the problem is. */
22568 instantiate_template (badfn, badargs, complain);
22569 }
22570 return orig_expr;
22571 }
22572
22573 /* As above, but error out if the expression remains overloaded. */
22574
22575 tree
22576 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22577 {
22578 exp = resolve_nondeduced_context (exp, complain);
22579 if (type_unknown_p (exp))
22580 {
22581 if (complain & tf_error)
22582 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22583 return error_mark_node;
22584 }
22585 return exp;
22586 }
22587
22588 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22589 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22590 different overloads deduce different arguments for a given parm.
22591 ADDR_P is true if the expression for which deduction is being
22592 performed was of the form "& fn" rather than simply "fn".
22593
22594 Returns 1 on success. */
22595
22596 static int
22597 try_one_overload (tree tparms,
22598 tree orig_targs,
22599 tree targs,
22600 tree parm,
22601 tree arg,
22602 unification_kind_t strict,
22603 int sub_strict,
22604 bool addr_p,
22605 bool explain_p)
22606 {
22607 int nargs;
22608 tree tempargs;
22609 int i;
22610
22611 if (arg == error_mark_node)
22612 return 0;
22613
22614 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22615 to function or pointer to member function argument if the set of
22616 overloaded functions does not contain function templates and at most
22617 one of a set of overloaded functions provides a unique match.
22618
22619 So if this is a template, just return success. */
22620
22621 if (uses_template_parms (arg))
22622 return 1;
22623
22624 if (TREE_CODE (arg) == METHOD_TYPE)
22625 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22626 else if (addr_p)
22627 arg = build_pointer_type (arg);
22628
22629 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22630
22631 /* We don't copy orig_targs for this because if we have already deduced
22632 some template args from previous args, unify would complain when we
22633 try to deduce a template parameter for the same argument, even though
22634 there isn't really a conflict. */
22635 nargs = TREE_VEC_LENGTH (targs);
22636 tempargs = make_tree_vec (nargs);
22637
22638 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22639 return 0;
22640
22641 /* First make sure we didn't deduce anything that conflicts with
22642 explicitly specified args. */
22643 for (i = nargs; i--; )
22644 {
22645 tree elt = TREE_VEC_ELT (tempargs, i);
22646 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22647
22648 if (!elt)
22649 /*NOP*/;
22650 else if (uses_template_parms (elt))
22651 /* Since we're unifying against ourselves, we will fill in
22652 template args used in the function parm list with our own
22653 template parms. Discard them. */
22654 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22655 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22656 {
22657 /* Check that the argument at each index of the deduced argument pack
22658 is equivalent to the corresponding explicitly specified argument.
22659 We may have deduced more arguments than were explicitly specified,
22660 and that's OK. */
22661
22662 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22663 that's wrong if we deduce the same argument pack from multiple
22664 function arguments: it's only incomplete the first time. */
22665
22666 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22667 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22668
22669 if (TREE_VEC_LENGTH (deduced_pack)
22670 < TREE_VEC_LENGTH (explicit_pack))
22671 return 0;
22672
22673 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22674 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22675 TREE_VEC_ELT (deduced_pack, j)))
22676 return 0;
22677 }
22678 else if (oldelt && !template_args_equal (oldelt, elt))
22679 return 0;
22680 }
22681
22682 for (i = nargs; i--; )
22683 {
22684 tree elt = TREE_VEC_ELT (tempargs, i);
22685
22686 if (elt)
22687 TREE_VEC_ELT (targs, i) = elt;
22688 }
22689
22690 return 1;
22691 }
22692
22693 /* PARM is a template class (perhaps with unbound template
22694 parameters). ARG is a fully instantiated type. If ARG can be
22695 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22696 TARGS are as for unify. */
22697
22698 static tree
22699 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22700 bool explain_p)
22701 {
22702 tree copy_of_targs;
22703
22704 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22705 return NULL_TREE;
22706 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22707 /* Matches anything. */;
22708 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22709 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22710 return NULL_TREE;
22711
22712 /* We need to make a new template argument vector for the call to
22713 unify. If we used TARGS, we'd clutter it up with the result of
22714 the attempted unification, even if this class didn't work out.
22715 We also don't want to commit ourselves to all the unifications
22716 we've already done, since unification is supposed to be done on
22717 an argument-by-argument basis. In other words, consider the
22718 following pathological case:
22719
22720 template <int I, int J, int K>
22721 struct S {};
22722
22723 template <int I, int J>
22724 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22725
22726 template <int I, int J, int K>
22727 void f(S<I, J, K>, S<I, I, I>);
22728
22729 void g() {
22730 S<0, 0, 0> s0;
22731 S<0, 1, 2> s2;
22732
22733 f(s0, s2);
22734 }
22735
22736 Now, by the time we consider the unification involving `s2', we
22737 already know that we must have `f<0, 0, 0>'. But, even though
22738 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22739 because there are two ways to unify base classes of S<0, 1, 2>
22740 with S<I, I, I>. If we kept the already deduced knowledge, we
22741 would reject the possibility I=1. */
22742 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22743
22744 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22745 {
22746 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22747 return NULL_TREE;
22748 return arg;
22749 }
22750
22751 /* If unification failed, we're done. */
22752 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22753 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22754 return NULL_TREE;
22755
22756 return arg;
22757 }
22758
22759 /* Given a template type PARM and a class type ARG, find the unique
22760 base type in ARG that is an instance of PARM. We do not examine
22761 ARG itself; only its base-classes. If there is not exactly one
22762 appropriate base class, return NULL_TREE. PARM may be the type of
22763 a partial specialization, as well as a plain template type. Used
22764 by unify. */
22765
22766 static enum template_base_result
22767 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22768 bool explain_p, tree *result)
22769 {
22770 tree rval = NULL_TREE;
22771 tree binfo;
22772
22773 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22774
22775 binfo = TYPE_BINFO (complete_type (arg));
22776 if (!binfo)
22777 {
22778 /* The type could not be completed. */
22779 *result = NULL_TREE;
22780 return tbr_incomplete_type;
22781 }
22782
22783 /* Walk in inheritance graph order. The search order is not
22784 important, and this avoids multiple walks of virtual bases. */
22785 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22786 {
22787 tree r = try_class_unification (tparms, targs, parm,
22788 BINFO_TYPE (binfo), explain_p);
22789
22790 if (r)
22791 {
22792 /* If there is more than one satisfactory baseclass, then:
22793
22794 [temp.deduct.call]
22795
22796 If they yield more than one possible deduced A, the type
22797 deduction fails.
22798
22799 applies. */
22800 if (rval && !same_type_p (r, rval))
22801 {
22802 /* [temp.deduct.call]/4.3: If there is a class C that is a
22803 (direct or indirect) base class of D and derived (directly or
22804 indirectly) from a class B and that would be a valid deduced
22805 A, the deduced A cannot be B or pointer to B, respectively. */
22806 if (DERIVED_FROM_P (r, rval))
22807 /* Ignore r. */
22808 continue;
22809 else if (DERIVED_FROM_P (rval, r))
22810 /* Ignore rval. */;
22811 else
22812 {
22813 *result = NULL_TREE;
22814 return tbr_ambiguous_baseclass;
22815 }
22816 }
22817
22818 rval = r;
22819 }
22820 }
22821
22822 *result = rval;
22823 return tbr_success;
22824 }
22825
22826 /* Returns the level of DECL, which declares a template parameter. */
22827
22828 static int
22829 template_decl_level (tree decl)
22830 {
22831 switch (TREE_CODE (decl))
22832 {
22833 case TYPE_DECL:
22834 case TEMPLATE_DECL:
22835 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22836
22837 case PARM_DECL:
22838 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22839
22840 default:
22841 gcc_unreachable ();
22842 }
22843 return 0;
22844 }
22845
22846 /* Decide whether ARG can be unified with PARM, considering only the
22847 cv-qualifiers of each type, given STRICT as documented for unify.
22848 Returns nonzero iff the unification is OK on that basis. */
22849
22850 static int
22851 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22852 {
22853 int arg_quals = cp_type_quals (arg);
22854 int parm_quals = cp_type_quals (parm);
22855
22856 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22857 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22858 {
22859 /* Although a CVR qualifier is ignored when being applied to a
22860 substituted template parameter ([8.3.2]/1 for example), that
22861 does not allow us to unify "const T" with "int&" because both
22862 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22863 It is ok when we're allowing additional CV qualifiers
22864 at the outer level [14.8.2.1]/3,1st bullet. */
22865 if ((TYPE_REF_P (arg)
22866 || FUNC_OR_METHOD_TYPE_P (arg))
22867 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22868 return 0;
22869
22870 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22871 && (parm_quals & TYPE_QUAL_RESTRICT))
22872 return 0;
22873 }
22874
22875 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22876 && (arg_quals & parm_quals) != parm_quals)
22877 return 0;
22878
22879 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22880 && (parm_quals & arg_quals) != arg_quals)
22881 return 0;
22882
22883 return 1;
22884 }
22885
22886 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22887 void
22888 template_parm_level_and_index (tree parm, int* level, int* index)
22889 {
22890 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22891 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22892 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22893 {
22894 *index = TEMPLATE_TYPE_IDX (parm);
22895 *level = TEMPLATE_TYPE_LEVEL (parm);
22896 }
22897 else
22898 {
22899 *index = TEMPLATE_PARM_IDX (parm);
22900 *level = TEMPLATE_PARM_LEVEL (parm);
22901 }
22902 }
22903
22904 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22905 do { \
22906 if (unify (TP, TA, P, A, S, EP)) \
22907 return 1; \
22908 } while (0)
22909
22910 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22911 expansion at the end of PACKED_PARMS. Returns 0 if the type
22912 deduction succeeds, 1 otherwise. STRICT is the same as in
22913 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22914 function call argument list. We'll need to adjust the arguments to make them
22915 types. SUBR tells us if this is from a recursive call to
22916 type_unification_real, or for comparing two template argument
22917 lists. */
22918
22919 static int
22920 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22921 tree packed_args, unification_kind_t strict,
22922 bool subr, bool explain_p)
22923 {
22924 tree parm
22925 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22926 tree pattern = PACK_EXPANSION_PATTERN (parm);
22927 tree pack, packs = NULL_TREE;
22928 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22929
22930 /* Add in any args remembered from an earlier partial instantiation. */
22931 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22932 int levels = TMPL_ARGS_DEPTH (targs);
22933
22934 packed_args = expand_template_argument_pack (packed_args);
22935
22936 int len = TREE_VEC_LENGTH (packed_args);
22937
22938 /* Determine the parameter packs we will be deducing from the
22939 pattern, and record their current deductions. */
22940 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22941 pack; pack = TREE_CHAIN (pack))
22942 {
22943 tree parm_pack = TREE_VALUE (pack);
22944 int idx, level;
22945
22946 /* Only template parameter packs can be deduced, not e.g. function
22947 parameter packs or __bases or __integer_pack. */
22948 if (!TEMPLATE_PARM_P (parm_pack))
22949 continue;
22950
22951 /* Determine the index and level of this parameter pack. */
22952 template_parm_level_and_index (parm_pack, &level, &idx);
22953 if (level < levels)
22954 continue;
22955
22956 /* Keep track of the parameter packs and their corresponding
22957 argument packs. */
22958 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22959 TREE_TYPE (packs) = make_tree_vec (len - start);
22960 }
22961
22962 /* Loop through all of the arguments that have not yet been
22963 unified and unify each with the pattern. */
22964 for (i = start; i < len; i++)
22965 {
22966 tree parm;
22967 bool any_explicit = false;
22968 tree arg = TREE_VEC_ELT (packed_args, i);
22969
22970 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22971 or the element of its argument pack at the current index if
22972 this argument was explicitly specified. */
22973 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22974 {
22975 int idx, level;
22976 tree arg, pargs;
22977 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22978
22979 arg = NULL_TREE;
22980 if (TREE_VALUE (pack)
22981 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22982 && (i - start < TREE_VEC_LENGTH (pargs)))
22983 {
22984 any_explicit = true;
22985 arg = TREE_VEC_ELT (pargs, i - start);
22986 }
22987 TMPL_ARG (targs, level, idx) = arg;
22988 }
22989
22990 /* If we had explicit template arguments, substitute them into the
22991 pattern before deduction. */
22992 if (any_explicit)
22993 {
22994 /* Some arguments might still be unspecified or dependent. */
22995 bool dependent;
22996 ++processing_template_decl;
22997 dependent = any_dependent_template_arguments_p (targs);
22998 if (!dependent)
22999 --processing_template_decl;
23000 parm = tsubst (pattern, targs,
23001 explain_p ? tf_warning_or_error : tf_none,
23002 NULL_TREE);
23003 if (dependent)
23004 --processing_template_decl;
23005 if (parm == error_mark_node)
23006 return 1;
23007 }
23008 else
23009 parm = pattern;
23010
23011 /* Unify the pattern with the current argument. */
23012 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23013 explain_p))
23014 return 1;
23015
23016 /* For each parameter pack, collect the deduced value. */
23017 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23018 {
23019 int idx, level;
23020 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23021
23022 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23023 TMPL_ARG (targs, level, idx);
23024 }
23025 }
23026
23027 /* Verify that the results of unification with the parameter packs
23028 produce results consistent with what we've seen before, and make
23029 the deduced argument packs available. */
23030 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23031 {
23032 tree old_pack = TREE_VALUE (pack);
23033 tree new_args = TREE_TYPE (pack);
23034 int i, len = TREE_VEC_LENGTH (new_args);
23035 int idx, level;
23036 bool nondeduced_p = false;
23037
23038 /* By default keep the original deduced argument pack.
23039 If necessary, more specific code is going to update the
23040 resulting deduced argument later down in this function. */
23041 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23042 TMPL_ARG (targs, level, idx) = old_pack;
23043
23044 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23045 actually deduce anything. */
23046 for (i = 0; i < len && !nondeduced_p; ++i)
23047 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23048 nondeduced_p = true;
23049 if (nondeduced_p)
23050 continue;
23051
23052 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23053 {
23054 /* If we had fewer function args than explicit template args,
23055 just use the explicits. */
23056 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23057 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23058 if (len < explicit_len)
23059 new_args = explicit_args;
23060 }
23061
23062 if (!old_pack)
23063 {
23064 tree result;
23065 /* Build the deduced *_ARGUMENT_PACK. */
23066 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23067 {
23068 result = make_node (NONTYPE_ARGUMENT_PACK);
23069 TREE_CONSTANT (result) = 1;
23070 }
23071 else
23072 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23073
23074 SET_ARGUMENT_PACK_ARGS (result, new_args);
23075
23076 /* Note the deduced argument packs for this parameter
23077 pack. */
23078 TMPL_ARG (targs, level, idx) = result;
23079 }
23080 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23081 && (ARGUMENT_PACK_ARGS (old_pack)
23082 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23083 {
23084 /* We only had the explicitly-provided arguments before, but
23085 now we have a complete set of arguments. */
23086 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23087
23088 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23089 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23090 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23091 }
23092 else
23093 {
23094 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23095 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23096 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23097 /* During template argument deduction for the aggregate deduction
23098 candidate, the number of elements in a trailing parameter pack
23099 is only deduced from the number of remaining function
23100 arguments if it is not otherwise deduced. */
23101 if (cxx_dialect >= cxx20
23102 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23103 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23104 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23105 if (!comp_template_args (old_args, new_args,
23106 &bad_old_arg, &bad_new_arg))
23107 /* Inconsistent unification of this parameter pack. */
23108 return unify_parameter_pack_inconsistent (explain_p,
23109 bad_old_arg,
23110 bad_new_arg);
23111 }
23112 }
23113
23114 return unify_success (explain_p);
23115 }
23116
23117 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23118 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23119 parameters and return value are as for unify. */
23120
23121 static int
23122 unify_array_domain (tree tparms, tree targs,
23123 tree parm_dom, tree arg_dom,
23124 bool explain_p)
23125 {
23126 tree parm_max;
23127 tree arg_max;
23128 bool parm_cst;
23129 bool arg_cst;
23130
23131 /* Our representation of array types uses "N - 1" as the
23132 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23133 not an integer constant. We cannot unify arbitrarily
23134 complex expressions, so we eliminate the MINUS_EXPRs
23135 here. */
23136 parm_max = TYPE_MAX_VALUE (parm_dom);
23137 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23138 if (!parm_cst)
23139 {
23140 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23141 parm_max = TREE_OPERAND (parm_max, 0);
23142 }
23143 arg_max = TYPE_MAX_VALUE (arg_dom);
23144 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23145 if (!arg_cst)
23146 {
23147 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23148 trying to unify the type of a variable with the type
23149 of a template parameter. For example:
23150
23151 template <unsigned int N>
23152 void f (char (&) [N]);
23153 int g();
23154 void h(int i) {
23155 char a[g(i)];
23156 f(a);
23157 }
23158
23159 Here, the type of the ARG will be "int [g(i)]", and
23160 may be a SAVE_EXPR, etc. */
23161 if (TREE_CODE (arg_max) != MINUS_EXPR)
23162 return unify_vla_arg (explain_p, arg_dom);
23163 arg_max = TREE_OPERAND (arg_max, 0);
23164 }
23165
23166 /* If only one of the bounds used a MINUS_EXPR, compensate
23167 by adding one to the other bound. */
23168 if (parm_cst && !arg_cst)
23169 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23170 integer_type_node,
23171 parm_max,
23172 integer_one_node);
23173 else if (arg_cst && !parm_cst)
23174 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23175 integer_type_node,
23176 arg_max,
23177 integer_one_node);
23178
23179 return unify (tparms, targs, parm_max, arg_max,
23180 UNIFY_ALLOW_INTEGER, explain_p);
23181 }
23182
23183 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23184
23185 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23186
23187 static pa_kind_t
23188 pa_kind (tree t)
23189 {
23190 if (PACK_EXPANSION_P (t))
23191 t = PACK_EXPANSION_PATTERN (t);
23192 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23193 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23194 || DECL_TYPE_TEMPLATE_P (t))
23195 return pa_tmpl;
23196 else if (TYPE_P (t))
23197 return pa_type;
23198 else
23199 return pa_expr;
23200 }
23201
23202 /* Deduce the value of template parameters. TPARMS is the (innermost)
23203 set of template parameters to a template. TARGS is the bindings
23204 for those template parameters, as determined thus far; TARGS may
23205 include template arguments for outer levels of template parameters
23206 as well. PARM is a parameter to a template function, or a
23207 subcomponent of that parameter; ARG is the corresponding argument.
23208 This function attempts to match PARM with ARG in a manner
23209 consistent with the existing assignments in TARGS. If more values
23210 are deduced, then TARGS is updated.
23211
23212 Returns 0 if the type deduction succeeds, 1 otherwise. The
23213 parameter STRICT is a bitwise or of the following flags:
23214
23215 UNIFY_ALLOW_NONE:
23216 Require an exact match between PARM and ARG.
23217 UNIFY_ALLOW_MORE_CV_QUAL:
23218 Allow the deduced ARG to be more cv-qualified (by qualification
23219 conversion) than ARG.
23220 UNIFY_ALLOW_LESS_CV_QUAL:
23221 Allow the deduced ARG to be less cv-qualified than ARG.
23222 UNIFY_ALLOW_DERIVED:
23223 Allow the deduced ARG to be a template base class of ARG,
23224 or a pointer to a template base class of the type pointed to by
23225 ARG.
23226 UNIFY_ALLOW_INTEGER:
23227 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23228 case for more information.
23229 UNIFY_ALLOW_OUTER_LEVEL:
23230 This is the outermost level of a deduction. Used to determine validity
23231 of qualification conversions. A valid qualification conversion must
23232 have const qualified pointers leading up to the inner type which
23233 requires additional CV quals, except at the outer level, where const
23234 is not required [conv.qual]. It would be normal to set this flag in
23235 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23236 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23237 This is the outermost level of a deduction, and PARM can be more CV
23238 qualified at this point.
23239 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23240 This is the outermost level of a deduction, and PARM can be less CV
23241 qualified at this point. */
23242
23243 static int
23244 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23245 bool explain_p)
23246 {
23247 int idx;
23248 tree targ;
23249 tree tparm;
23250 int strict_in = strict;
23251 tsubst_flags_t complain = (explain_p
23252 ? tf_warning_or_error
23253 : tf_none);
23254
23255 /* I don't think this will do the right thing with respect to types.
23256 But the only case I've seen it in so far has been array bounds, where
23257 signedness is the only information lost, and I think that will be
23258 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23259 finish_id_expression_1, and are also OK. */
23260 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23261 parm = TREE_OPERAND (parm, 0);
23262
23263 if (arg == error_mark_node)
23264 return unify_invalid (explain_p);
23265 if (arg == unknown_type_node
23266 || arg == init_list_type_node)
23267 /* We can't deduce anything from this, but we might get all the
23268 template args from other function args. */
23269 return unify_success (explain_p);
23270
23271 if (parm == any_targ_node || arg == any_targ_node)
23272 return unify_success (explain_p);
23273
23274 /* If PARM uses template parameters, then we can't bail out here,
23275 even if ARG == PARM, since we won't record unifications for the
23276 template parameters. We might need them if we're trying to
23277 figure out which of two things is more specialized. */
23278 if (arg == parm && !uses_template_parms (parm))
23279 return unify_success (explain_p);
23280
23281 /* Handle init lists early, so the rest of the function can assume
23282 we're dealing with a type. */
23283 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23284 {
23285 tree elt, elttype;
23286 unsigned i;
23287 tree orig_parm = parm;
23288
23289 if (!is_std_init_list (parm)
23290 && TREE_CODE (parm) != ARRAY_TYPE)
23291 /* We can only deduce from an initializer list argument if the
23292 parameter is std::initializer_list or an array; otherwise this
23293 is a non-deduced context. */
23294 return unify_success (explain_p);
23295
23296 if (TREE_CODE (parm) == ARRAY_TYPE)
23297 elttype = TREE_TYPE (parm);
23298 else
23299 {
23300 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23301 /* Deduction is defined in terms of a single type, so just punt
23302 on the (bizarre) std::initializer_list<T...>. */
23303 if (PACK_EXPANSION_P (elttype))
23304 return unify_success (explain_p);
23305 }
23306
23307 if (strict != DEDUCE_EXACT
23308 && TYPE_P (elttype)
23309 && !uses_deducible_template_parms (elttype))
23310 /* If ELTTYPE has no deducible template parms, skip deduction from
23311 the list elements. */;
23312 else
23313 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23314 {
23315 int elt_strict = strict;
23316
23317 if (elt == error_mark_node)
23318 return unify_invalid (explain_p);
23319
23320 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23321 {
23322 tree type = TREE_TYPE (elt);
23323 if (type == error_mark_node)
23324 return unify_invalid (explain_p);
23325 /* It should only be possible to get here for a call. */
23326 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23327 elt_strict |= maybe_adjust_types_for_deduction
23328 (DEDUCE_CALL, &elttype, &type, elt);
23329 elt = type;
23330 }
23331
23332 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23333 explain_p);
23334 }
23335
23336 if (TREE_CODE (parm) == ARRAY_TYPE
23337 && deducible_array_bound (TYPE_DOMAIN (parm)))
23338 {
23339 /* Also deduce from the length of the initializer list. */
23340 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23341 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23342 if (idx == error_mark_node)
23343 return unify_invalid (explain_p);
23344 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23345 idx, explain_p);
23346 }
23347
23348 /* If the std::initializer_list<T> deduction worked, replace the
23349 deduced A with std::initializer_list<A>. */
23350 if (orig_parm != parm)
23351 {
23352 idx = TEMPLATE_TYPE_IDX (orig_parm);
23353 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23354 targ = listify (targ);
23355 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23356 }
23357 return unify_success (explain_p);
23358 }
23359
23360 /* If parm and arg aren't the same kind of thing (template, type, or
23361 expression), fail early. */
23362 if (pa_kind (parm) != pa_kind (arg))
23363 return unify_invalid (explain_p);
23364
23365 /* Immediately reject some pairs that won't unify because of
23366 cv-qualification mismatches. */
23367 if (TREE_CODE (arg) == TREE_CODE (parm)
23368 && TYPE_P (arg)
23369 /* It is the elements of the array which hold the cv quals of an array
23370 type, and the elements might be template type parms. We'll check
23371 when we recurse. */
23372 && TREE_CODE (arg) != ARRAY_TYPE
23373 /* We check the cv-qualifiers when unifying with template type
23374 parameters below. We want to allow ARG `const T' to unify with
23375 PARM `T' for example, when computing which of two templates
23376 is more specialized, for example. */
23377 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23378 && !check_cv_quals_for_unify (strict_in, arg, parm))
23379 return unify_cv_qual_mismatch (explain_p, parm, arg);
23380
23381 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23382 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23383 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23384 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23385 strict &= ~UNIFY_ALLOW_DERIVED;
23386 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23387 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23388
23389 switch (TREE_CODE (parm))
23390 {
23391 case TYPENAME_TYPE:
23392 case SCOPE_REF:
23393 case UNBOUND_CLASS_TEMPLATE:
23394 /* In a type which contains a nested-name-specifier, template
23395 argument values cannot be deduced for template parameters used
23396 within the nested-name-specifier. */
23397 return unify_success (explain_p);
23398
23399 case TEMPLATE_TYPE_PARM:
23400 case TEMPLATE_TEMPLATE_PARM:
23401 case BOUND_TEMPLATE_TEMPLATE_PARM:
23402 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23403 if (error_operand_p (tparm))
23404 return unify_invalid (explain_p);
23405
23406 if (TEMPLATE_TYPE_LEVEL (parm)
23407 != template_decl_level (tparm))
23408 /* The PARM is not one we're trying to unify. Just check
23409 to see if it matches ARG. */
23410 {
23411 if (TREE_CODE (arg) == TREE_CODE (parm)
23412 && (is_auto (parm) ? is_auto (arg)
23413 : same_type_p (parm, arg)))
23414 return unify_success (explain_p);
23415 else
23416 return unify_type_mismatch (explain_p, parm, arg);
23417 }
23418 idx = TEMPLATE_TYPE_IDX (parm);
23419 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23420 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23421 if (error_operand_p (tparm))
23422 return unify_invalid (explain_p);
23423
23424 /* Check for mixed types and values. */
23425 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23426 && TREE_CODE (tparm) != TYPE_DECL)
23427 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23428 && TREE_CODE (tparm) != TEMPLATE_DECL))
23429 gcc_unreachable ();
23430
23431 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23432 {
23433 if ((strict_in & UNIFY_ALLOW_DERIVED)
23434 && CLASS_TYPE_P (arg))
23435 {
23436 /* First try to match ARG directly. */
23437 tree t = try_class_unification (tparms, targs, parm, arg,
23438 explain_p);
23439 if (!t)
23440 {
23441 /* Otherwise, look for a suitable base of ARG, as below. */
23442 enum template_base_result r;
23443 r = get_template_base (tparms, targs, parm, arg,
23444 explain_p, &t);
23445 if (!t)
23446 return unify_no_common_base (explain_p, r, parm, arg);
23447 arg = t;
23448 }
23449 }
23450 /* ARG must be constructed from a template class or a template
23451 template parameter. */
23452 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23453 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23454 return unify_template_deduction_failure (explain_p, parm, arg);
23455
23456 /* Deduce arguments T, i from TT<T> or TT<i>. */
23457 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23458 return 1;
23459
23460 arg = TYPE_TI_TEMPLATE (arg);
23461
23462 /* Fall through to deduce template name. */
23463 }
23464
23465 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23466 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23467 {
23468 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23469
23470 /* Simple cases: Value already set, does match or doesn't. */
23471 if (targ != NULL_TREE && template_args_equal (targ, arg))
23472 return unify_success (explain_p);
23473 else if (targ)
23474 return unify_inconsistency (explain_p, parm, targ, arg);
23475 }
23476 else
23477 {
23478 /* If PARM is `const T' and ARG is only `int', we don't have
23479 a match unless we are allowing additional qualification.
23480 If ARG is `const int' and PARM is just `T' that's OK;
23481 that binds `const int' to `T'. */
23482 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23483 arg, parm))
23484 return unify_cv_qual_mismatch (explain_p, parm, arg);
23485
23486 /* Consider the case where ARG is `const volatile int' and
23487 PARM is `const T'. Then, T should be `volatile int'. */
23488 arg = cp_build_qualified_type_real
23489 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23490 if (arg == error_mark_node)
23491 return unify_invalid (explain_p);
23492
23493 /* Simple cases: Value already set, does match or doesn't. */
23494 if (targ != NULL_TREE && same_type_p (targ, arg))
23495 return unify_success (explain_p);
23496 else if (targ)
23497 return unify_inconsistency (explain_p, parm, targ, arg);
23498
23499 /* Make sure that ARG is not a variable-sized array. (Note
23500 that were talking about variable-sized arrays (like
23501 `int[n]'), rather than arrays of unknown size (like
23502 `int[]').) We'll get very confused by such a type since
23503 the bound of the array is not constant, and therefore
23504 not mangleable. Besides, such types are not allowed in
23505 ISO C++, so we can do as we please here. We do allow
23506 them for 'auto' deduction, since that isn't ABI-exposed. */
23507 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23508 return unify_vla_arg (explain_p, arg);
23509
23510 /* Strip typedefs as in convert_template_argument. */
23511 arg = canonicalize_type_argument (arg, tf_none);
23512 }
23513
23514 /* If ARG is a parameter pack or an expansion, we cannot unify
23515 against it unless PARM is also a parameter pack. */
23516 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23517 && !template_parameter_pack_p (parm))
23518 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23519
23520 /* If the argument deduction results is a METHOD_TYPE,
23521 then there is a problem.
23522 METHOD_TYPE doesn't map to any real C++ type the result of
23523 the deduction cannot be of that type. */
23524 if (TREE_CODE (arg) == METHOD_TYPE)
23525 return unify_method_type_error (explain_p, arg);
23526
23527 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23528 return unify_success (explain_p);
23529
23530 case TEMPLATE_PARM_INDEX:
23531 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23532 if (error_operand_p (tparm))
23533 return unify_invalid (explain_p);
23534
23535 if (TEMPLATE_PARM_LEVEL (parm)
23536 != template_decl_level (tparm))
23537 {
23538 /* The PARM is not one we're trying to unify. Just check
23539 to see if it matches ARG. */
23540 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23541 && cp_tree_equal (parm, arg));
23542 if (result)
23543 unify_expression_unequal (explain_p, parm, arg);
23544 return result;
23545 }
23546
23547 idx = TEMPLATE_PARM_IDX (parm);
23548 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23549
23550 if (targ)
23551 {
23552 if ((strict & UNIFY_ALLOW_INTEGER)
23553 && TREE_TYPE (targ) && TREE_TYPE (arg)
23554 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23555 /* We're deducing from an array bound, the type doesn't matter. */
23556 arg = fold_convert (TREE_TYPE (targ), arg);
23557 int x = !cp_tree_equal (targ, arg);
23558 if (x)
23559 unify_inconsistency (explain_p, parm, targ, arg);
23560 return x;
23561 }
23562
23563 /* [temp.deduct.type] If, in the declaration of a function template
23564 with a non-type template-parameter, the non-type
23565 template-parameter is used in an expression in the function
23566 parameter-list and, if the corresponding template-argument is
23567 deduced, the template-argument type shall match the type of the
23568 template-parameter exactly, except that a template-argument
23569 deduced from an array bound may be of any integral type.
23570 The non-type parameter might use already deduced type parameters. */
23571 tparm = TREE_TYPE (parm);
23572 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23573 /* We don't have enough levels of args to do any substitution. This
23574 can happen in the context of -fnew-ttp-matching. */;
23575 else
23576 {
23577 ++processing_template_decl;
23578 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23579 --processing_template_decl;
23580
23581 if (tree a = type_uses_auto (tparm))
23582 {
23583 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23584 if (tparm == error_mark_node)
23585 return 1;
23586 }
23587 }
23588
23589 if (!TREE_TYPE (arg))
23590 /* Template-parameter dependent expression. Just accept it for now.
23591 It will later be processed in convert_template_argument. */
23592 ;
23593 else if (same_type_ignoring_top_level_qualifiers_p
23594 (non_reference (TREE_TYPE (arg)),
23595 non_reference (tparm)))
23596 /* OK. Ignore top-level quals here because a class-type template
23597 parameter object is const. */;
23598 else if ((strict & UNIFY_ALLOW_INTEGER)
23599 && CP_INTEGRAL_TYPE_P (tparm))
23600 /* Convert the ARG to the type of PARM; the deduced non-type
23601 template argument must exactly match the types of the
23602 corresponding parameter. */
23603 arg = fold (build_nop (tparm, arg));
23604 else if (uses_template_parms (tparm))
23605 {
23606 /* We haven't deduced the type of this parameter yet. */
23607 if (cxx_dialect >= cxx17
23608 /* We deduce from array bounds in try_array_deduction. */
23609 && !(strict & UNIFY_ALLOW_INTEGER)
23610 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
23611 {
23612 /* Deduce it from the non-type argument. */
23613 tree atype = TREE_TYPE (arg);
23614 RECUR_AND_CHECK_FAILURE (tparms, targs,
23615 tparm, atype,
23616 UNIFY_ALLOW_NONE, explain_p);
23617 /* Now check whether the type of this parameter is still
23618 dependent, and give up if so. */
23619 ++processing_template_decl;
23620 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23621 --processing_template_decl;
23622 if (uses_template_parms (tparm))
23623 return unify_success (explain_p);
23624 }
23625 else
23626 /* Try again later. */
23627 return unify_success (explain_p);
23628 }
23629 else
23630 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23631
23632 /* If ARG is a parameter pack or an expansion, we cannot unify
23633 against it unless PARM is also a parameter pack. */
23634 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23635 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23636 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23637
23638 {
23639 bool removed_attr = false;
23640 arg = strip_typedefs_expr (arg, &removed_attr);
23641 }
23642 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23643 return unify_success (explain_p);
23644
23645 case PTRMEM_CST:
23646 {
23647 /* A pointer-to-member constant can be unified only with
23648 another constant. */
23649 if (TREE_CODE (arg) != PTRMEM_CST)
23650 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23651
23652 /* Just unify the class member. It would be useless (and possibly
23653 wrong, depending on the strict flags) to unify also
23654 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23655 arg refer to the same variable, even if through different
23656 classes. For instance:
23657
23658 struct A { int x; };
23659 struct B : A { };
23660
23661 Unification of &A::x and &B::x must succeed. */
23662 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23663 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23664 }
23665
23666 case POINTER_TYPE:
23667 {
23668 if (!TYPE_PTR_P (arg))
23669 return unify_type_mismatch (explain_p, parm, arg);
23670
23671 /* [temp.deduct.call]
23672
23673 A can be another pointer or pointer to member type that can
23674 be converted to the deduced A via a qualification
23675 conversion (_conv.qual_).
23676
23677 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23678 This will allow for additional cv-qualification of the
23679 pointed-to types if appropriate. */
23680
23681 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23682 /* The derived-to-base conversion only persists through one
23683 level of pointers. */
23684 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23685
23686 return unify (tparms, targs, TREE_TYPE (parm),
23687 TREE_TYPE (arg), strict, explain_p);
23688 }
23689
23690 case REFERENCE_TYPE:
23691 if (!TYPE_REF_P (arg))
23692 return unify_type_mismatch (explain_p, parm, arg);
23693 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23694 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23695
23696 case ARRAY_TYPE:
23697 if (TREE_CODE (arg) != ARRAY_TYPE)
23698 return unify_type_mismatch (explain_p, parm, arg);
23699 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23700 != (TYPE_DOMAIN (arg) == NULL_TREE))
23701 return unify_type_mismatch (explain_p, parm, arg);
23702 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23703 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23704 if (TYPE_DOMAIN (parm) != NULL_TREE)
23705 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23706 TYPE_DOMAIN (arg), explain_p);
23707 return unify_success (explain_p);
23708
23709 case REAL_TYPE:
23710 case COMPLEX_TYPE:
23711 case VECTOR_TYPE:
23712 case INTEGER_TYPE:
23713 case BOOLEAN_TYPE:
23714 case ENUMERAL_TYPE:
23715 case VOID_TYPE:
23716 case OPAQUE_TYPE:
23717 case NULLPTR_TYPE:
23718 if (TREE_CODE (arg) != TREE_CODE (parm))
23719 return unify_type_mismatch (explain_p, parm, arg);
23720
23721 /* We have already checked cv-qualification at the top of the
23722 function. */
23723 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23724 return unify_type_mismatch (explain_p, parm, arg);
23725
23726 /* As far as unification is concerned, this wins. Later checks
23727 will invalidate it if necessary. */
23728 return unify_success (explain_p);
23729
23730 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23731 /* Type INTEGER_CST can come from ordinary constant template args. */
23732 case INTEGER_CST:
23733 while (CONVERT_EXPR_P (arg))
23734 arg = TREE_OPERAND (arg, 0);
23735
23736 if (TREE_CODE (arg) != INTEGER_CST)
23737 return unify_template_argument_mismatch (explain_p, parm, arg);
23738 return (tree_int_cst_equal (parm, arg)
23739 ? unify_success (explain_p)
23740 : unify_template_argument_mismatch (explain_p, parm, arg));
23741
23742 case TREE_VEC:
23743 {
23744 int i, len, argslen;
23745 int parm_variadic_p = 0;
23746
23747 if (TREE_CODE (arg) != TREE_VEC)
23748 return unify_template_argument_mismatch (explain_p, parm, arg);
23749
23750 len = TREE_VEC_LENGTH (parm);
23751 argslen = TREE_VEC_LENGTH (arg);
23752
23753 /* Check for pack expansions in the parameters. */
23754 for (i = 0; i < len; ++i)
23755 {
23756 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23757 {
23758 if (i == len - 1)
23759 /* We can unify against something with a trailing
23760 parameter pack. */
23761 parm_variadic_p = 1;
23762 else
23763 /* [temp.deduct.type]/9: If the template argument list of
23764 P contains a pack expansion that is not the last
23765 template argument, the entire template argument list
23766 is a non-deduced context. */
23767 return unify_success (explain_p);
23768 }
23769 }
23770
23771 /* If we don't have enough arguments to satisfy the parameters
23772 (not counting the pack expression at the end), or we have
23773 too many arguments for a parameter list that doesn't end in
23774 a pack expression, we can't unify. */
23775 if (parm_variadic_p
23776 ? argslen < len - parm_variadic_p
23777 : argslen != len)
23778 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23779
23780 /* Unify all of the parameters that precede the (optional)
23781 pack expression. */
23782 for (i = 0; i < len - parm_variadic_p; ++i)
23783 {
23784 RECUR_AND_CHECK_FAILURE (tparms, targs,
23785 TREE_VEC_ELT (parm, i),
23786 TREE_VEC_ELT (arg, i),
23787 UNIFY_ALLOW_NONE, explain_p);
23788 }
23789 if (parm_variadic_p)
23790 return unify_pack_expansion (tparms, targs, parm, arg,
23791 DEDUCE_EXACT,
23792 /*subr=*/true, explain_p);
23793 return unify_success (explain_p);
23794 }
23795
23796 case RECORD_TYPE:
23797 case UNION_TYPE:
23798 if (TREE_CODE (arg) != TREE_CODE (parm))
23799 return unify_type_mismatch (explain_p, parm, arg);
23800
23801 if (TYPE_PTRMEMFUNC_P (parm))
23802 {
23803 if (!TYPE_PTRMEMFUNC_P (arg))
23804 return unify_type_mismatch (explain_p, parm, arg);
23805
23806 return unify (tparms, targs,
23807 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23808 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23809 strict, explain_p);
23810 }
23811 else if (TYPE_PTRMEMFUNC_P (arg))
23812 return unify_type_mismatch (explain_p, parm, arg);
23813
23814 if (CLASSTYPE_TEMPLATE_INFO (parm))
23815 {
23816 tree t = NULL_TREE;
23817
23818 if (strict_in & UNIFY_ALLOW_DERIVED)
23819 {
23820 /* First, we try to unify the PARM and ARG directly. */
23821 t = try_class_unification (tparms, targs,
23822 parm, arg, explain_p);
23823
23824 if (!t)
23825 {
23826 /* Fallback to the special case allowed in
23827 [temp.deduct.call]:
23828
23829 If P is a class, and P has the form
23830 template-id, then A can be a derived class of
23831 the deduced A. Likewise, if P is a pointer to
23832 a class of the form template-id, A can be a
23833 pointer to a derived class pointed to by the
23834 deduced A. */
23835 enum template_base_result r;
23836 r = get_template_base (tparms, targs, parm, arg,
23837 explain_p, &t);
23838
23839 if (!t)
23840 {
23841 /* Don't give the derived diagnostic if we're
23842 already dealing with the same template. */
23843 bool same_template
23844 = (CLASSTYPE_TEMPLATE_INFO (arg)
23845 && (CLASSTYPE_TI_TEMPLATE (parm)
23846 == CLASSTYPE_TI_TEMPLATE (arg)));
23847 return unify_no_common_base (explain_p && !same_template,
23848 r, parm, arg);
23849 }
23850 }
23851 }
23852 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23853 && (CLASSTYPE_TI_TEMPLATE (parm)
23854 == CLASSTYPE_TI_TEMPLATE (arg)))
23855 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23856 Then, we should unify `int' and `U'. */
23857 t = arg;
23858 else
23859 /* There's no chance of unification succeeding. */
23860 return unify_type_mismatch (explain_p, parm, arg);
23861
23862 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23863 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23864 }
23865 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23866 return unify_type_mismatch (explain_p, parm, arg);
23867 return unify_success (explain_p);
23868
23869 case METHOD_TYPE:
23870 case FUNCTION_TYPE:
23871 {
23872 unsigned int nargs;
23873 tree *args;
23874 tree a;
23875 unsigned int i;
23876
23877 if (TREE_CODE (arg) != TREE_CODE (parm))
23878 return unify_type_mismatch (explain_p, parm, arg);
23879
23880 /* CV qualifications for methods can never be deduced, they must
23881 match exactly. We need to check them explicitly here,
23882 because type_unification_real treats them as any other
23883 cv-qualified parameter. */
23884 if (TREE_CODE (parm) == METHOD_TYPE
23885 && (!check_cv_quals_for_unify
23886 (UNIFY_ALLOW_NONE,
23887 class_of_this_parm (arg),
23888 class_of_this_parm (parm))))
23889 return unify_cv_qual_mismatch (explain_p, parm, arg);
23890 if (TREE_CODE (arg) == FUNCTION_TYPE
23891 && type_memfn_quals (parm) != type_memfn_quals (arg))
23892 return unify_cv_qual_mismatch (explain_p, parm, arg);
23893 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23894 return unify_type_mismatch (explain_p, parm, arg);
23895
23896 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23897 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23898
23899 nargs = list_length (TYPE_ARG_TYPES (arg));
23900 args = XALLOCAVEC (tree, nargs);
23901 for (a = TYPE_ARG_TYPES (arg), i = 0;
23902 a != NULL_TREE && a != void_list_node;
23903 a = TREE_CHAIN (a), ++i)
23904 args[i] = TREE_VALUE (a);
23905 nargs = i;
23906
23907 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23908 args, nargs, 1, DEDUCE_EXACT,
23909 NULL, explain_p))
23910 return 1;
23911
23912 if (flag_noexcept_type)
23913 {
23914 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23915 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23916 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23917 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23918 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23919 && uses_template_parms (TREE_PURPOSE (pspec)))
23920 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23921 TREE_PURPOSE (aspec),
23922 UNIFY_ALLOW_NONE, explain_p);
23923 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23924 return unify_type_mismatch (explain_p, parm, arg);
23925 }
23926
23927 return 0;
23928 }
23929
23930 case OFFSET_TYPE:
23931 /* Unify a pointer to member with a pointer to member function, which
23932 deduces the type of the member as a function type. */
23933 if (TYPE_PTRMEMFUNC_P (arg))
23934 {
23935 /* Check top-level cv qualifiers */
23936 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23937 return unify_cv_qual_mismatch (explain_p, parm, arg);
23938
23939 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23940 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23941 UNIFY_ALLOW_NONE, explain_p);
23942
23943 /* Determine the type of the function we are unifying against. */
23944 tree fntype = static_fn_type (arg);
23945
23946 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23947 }
23948
23949 if (TREE_CODE (arg) != OFFSET_TYPE)
23950 return unify_type_mismatch (explain_p, parm, arg);
23951 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23952 TYPE_OFFSET_BASETYPE (arg),
23953 UNIFY_ALLOW_NONE, explain_p);
23954 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23955 strict, explain_p);
23956
23957 case CONST_DECL:
23958 if (DECL_TEMPLATE_PARM_P (parm))
23959 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23960 if (arg != scalar_constant_value (parm))
23961 return unify_template_argument_mismatch (explain_p, parm, arg);
23962 return unify_success (explain_p);
23963
23964 case FIELD_DECL:
23965 case TEMPLATE_DECL:
23966 /* Matched cases are handled by the ARG == PARM test above. */
23967 return unify_template_argument_mismatch (explain_p, parm, arg);
23968
23969 case VAR_DECL:
23970 /* We might get a variable as a non-type template argument in parm if the
23971 corresponding parameter is type-dependent. Make any necessary
23972 adjustments based on whether arg is a reference. */
23973 if (CONSTANT_CLASS_P (arg))
23974 parm = fold_non_dependent_expr (parm, complain);
23975 else if (REFERENCE_REF_P (arg))
23976 {
23977 tree sub = TREE_OPERAND (arg, 0);
23978 STRIP_NOPS (sub);
23979 if (TREE_CODE (sub) == ADDR_EXPR)
23980 arg = TREE_OPERAND (sub, 0);
23981 }
23982 /* Now use the normal expression code to check whether they match. */
23983 goto expr;
23984
23985 case TYPE_ARGUMENT_PACK:
23986 case NONTYPE_ARGUMENT_PACK:
23987 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23988 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23989
23990 case TYPEOF_TYPE:
23991 case DECLTYPE_TYPE:
23992 case UNDERLYING_TYPE:
23993 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23994 or UNDERLYING_TYPE nodes. */
23995 return unify_success (explain_p);
23996
23997 case ERROR_MARK:
23998 /* Unification fails if we hit an error node. */
23999 return unify_invalid (explain_p);
24000
24001 case INDIRECT_REF:
24002 if (REFERENCE_REF_P (parm))
24003 {
24004 bool pexp = PACK_EXPANSION_P (arg);
24005 if (pexp)
24006 arg = PACK_EXPANSION_PATTERN (arg);
24007 if (REFERENCE_REF_P (arg))
24008 arg = TREE_OPERAND (arg, 0);
24009 if (pexp)
24010 arg = make_pack_expansion (arg, complain);
24011 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24012 strict, explain_p);
24013 }
24014 /* FALLTHRU */
24015
24016 default:
24017 /* An unresolved overload is a nondeduced context. */
24018 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24019 return unify_success (explain_p);
24020 gcc_assert (EXPR_P (parm)
24021 || COMPOUND_LITERAL_P (parm)
24022 || TREE_CODE (parm) == TRAIT_EXPR);
24023 expr:
24024 /* We must be looking at an expression. This can happen with
24025 something like:
24026
24027 template <int I>
24028 void foo(S<I>, S<I + 2>);
24029
24030 or
24031
24032 template<typename T>
24033 void foo(A<T, T{}>);
24034
24035 This is a "non-deduced context":
24036
24037 [deduct.type]
24038
24039 The non-deduced contexts are:
24040
24041 --A non-type template argument or an array bound in which
24042 a subexpression references a template parameter.
24043
24044 In these cases, we assume deduction succeeded, but don't
24045 actually infer any unifications. */
24046
24047 if (!uses_template_parms (parm)
24048 && !template_args_equal (parm, arg))
24049 return unify_expression_unequal (explain_p, parm, arg);
24050 else
24051 return unify_success (explain_p);
24052 }
24053 }
24054 #undef RECUR_AND_CHECK_FAILURE
24055 \f
24056 /* Note that DECL can be defined in this translation unit, if
24057 required. */
24058
24059 static void
24060 mark_definable (tree decl)
24061 {
24062 tree clone;
24063 DECL_NOT_REALLY_EXTERN (decl) = 1;
24064 FOR_EACH_CLONE (clone, decl)
24065 DECL_NOT_REALLY_EXTERN (clone) = 1;
24066 }
24067
24068 /* Called if RESULT is explicitly instantiated, or is a member of an
24069 explicitly instantiated class. */
24070
24071 void
24072 mark_decl_instantiated (tree result, int extern_p)
24073 {
24074 SET_DECL_EXPLICIT_INSTANTIATION (result);
24075
24076 /* If this entity has already been written out, it's too late to
24077 make any modifications. */
24078 if (TREE_ASM_WRITTEN (result))
24079 return;
24080
24081 /* For anonymous namespace we don't need to do anything. */
24082 if (decl_anon_ns_mem_p (result))
24083 {
24084 gcc_assert (!TREE_PUBLIC (result));
24085 return;
24086 }
24087
24088 if (TREE_CODE (result) != FUNCTION_DECL)
24089 /* The TREE_PUBLIC flag for function declarations will have been
24090 set correctly by tsubst. */
24091 TREE_PUBLIC (result) = 1;
24092
24093 /* This might have been set by an earlier implicit instantiation. */
24094 DECL_COMDAT (result) = 0;
24095
24096 if (extern_p)
24097 DECL_NOT_REALLY_EXTERN (result) = 0;
24098 else
24099 {
24100 mark_definable (result);
24101 mark_needed (result);
24102 /* Always make artificials weak. */
24103 if (DECL_ARTIFICIAL (result) && flag_weak)
24104 comdat_linkage (result);
24105 /* For WIN32 we also want to put explicit instantiations in
24106 linkonce sections. */
24107 else if (TREE_PUBLIC (result))
24108 maybe_make_one_only (result);
24109 if (TREE_CODE (result) == FUNCTION_DECL
24110 && DECL_TEMPLATE_INSTANTIATED (result))
24111 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24112 since start_preparsed_function wouldn't have if we had an earlier
24113 extern explicit instantiation. */
24114 DECL_EXTERNAL (result) = 0;
24115 }
24116
24117 /* If EXTERN_P, then this function will not be emitted -- unless
24118 followed by an explicit instantiation, at which point its linkage
24119 will be adjusted. If !EXTERN_P, then this function will be
24120 emitted here. In neither circumstance do we want
24121 import_export_decl to adjust the linkage. */
24122 DECL_INTERFACE_KNOWN (result) = 1;
24123 }
24124
24125 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24126 important template arguments. If any are missing, we check whether
24127 they're important by using error_mark_node for substituting into any
24128 args that were used for partial ordering (the ones between ARGS and END)
24129 and seeing if it bubbles up. */
24130
24131 static bool
24132 check_undeduced_parms (tree targs, tree args, tree end)
24133 {
24134 bool found = false;
24135 int i;
24136 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24137 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24138 {
24139 found = true;
24140 TREE_VEC_ELT (targs, i) = error_mark_node;
24141 }
24142 if (found)
24143 {
24144 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24145 if (substed == error_mark_node)
24146 return true;
24147 }
24148 return false;
24149 }
24150
24151 /* Given two function templates PAT1 and PAT2, return:
24152
24153 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24154 -1 if PAT2 is more specialized than PAT1.
24155 0 if neither is more specialized.
24156
24157 LEN indicates the number of parameters we should consider
24158 (defaulted parameters should not be considered).
24159
24160 The 1998 std underspecified function template partial ordering, and
24161 DR214 addresses the issue. We take pairs of arguments, one from
24162 each of the templates, and deduce them against each other. One of
24163 the templates will be more specialized if all the *other*
24164 template's arguments deduce against its arguments and at least one
24165 of its arguments *does* *not* deduce against the other template's
24166 corresponding argument. Deduction is done as for class templates.
24167 The arguments used in deduction have reference and top level cv
24168 qualifiers removed. Iff both arguments were originally reference
24169 types *and* deduction succeeds in both directions, an lvalue reference
24170 wins against an rvalue reference and otherwise the template
24171 with the more cv-qualified argument wins for that pairing (if
24172 neither is more cv-qualified, they both are equal). Unlike regular
24173 deduction, after all the arguments have been deduced in this way,
24174 we do *not* verify the deduced template argument values can be
24175 substituted into non-deduced contexts.
24176
24177 The logic can be a bit confusing here, because we look at deduce1 and
24178 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24179 can find template arguments for pat1 to make arg1 look like arg2, that
24180 means that arg2 is at least as specialized as arg1. */
24181
24182 int
24183 more_specialized_fn (tree pat1, tree pat2, int len)
24184 {
24185 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24186 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24187 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24188 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24189 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24190 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24191 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24192 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24193 tree origs1, origs2;
24194 bool lose1 = false;
24195 bool lose2 = false;
24196
24197 /* Remove the this parameter from non-static member functions. If
24198 one is a non-static member function and the other is not a static
24199 member function, remove the first parameter from that function
24200 also. This situation occurs for operator functions where we
24201 locate both a member function (with this pointer) and non-member
24202 operator (with explicit first operand). */
24203 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24204 {
24205 len--; /* LEN is the number of significant arguments for DECL1 */
24206 args1 = TREE_CHAIN (args1);
24207 if (!DECL_STATIC_FUNCTION_P (decl2))
24208 args2 = TREE_CHAIN (args2);
24209 }
24210 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24211 {
24212 args2 = TREE_CHAIN (args2);
24213 if (!DECL_STATIC_FUNCTION_P (decl1))
24214 {
24215 len--;
24216 args1 = TREE_CHAIN (args1);
24217 }
24218 }
24219
24220 /* If only one is a conversion operator, they are unordered. */
24221 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24222 return 0;
24223
24224 /* Consider the return type for a conversion function */
24225 if (DECL_CONV_FN_P (decl1))
24226 {
24227 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24228 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24229 len++;
24230 }
24231
24232 processing_template_decl++;
24233
24234 origs1 = args1;
24235 origs2 = args2;
24236
24237 while (len--
24238 /* Stop when an ellipsis is seen. */
24239 && args1 != NULL_TREE && args2 != NULL_TREE)
24240 {
24241 tree arg1 = TREE_VALUE (args1);
24242 tree arg2 = TREE_VALUE (args2);
24243 int deduce1, deduce2;
24244 int quals1 = -1;
24245 int quals2 = -1;
24246 int ref1 = 0;
24247 int ref2 = 0;
24248
24249 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24250 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24251 {
24252 /* When both arguments are pack expansions, we need only
24253 unify the patterns themselves. */
24254 arg1 = PACK_EXPANSION_PATTERN (arg1);
24255 arg2 = PACK_EXPANSION_PATTERN (arg2);
24256
24257 /* This is the last comparison we need to do. */
24258 len = 0;
24259 }
24260
24261 if (TYPE_REF_P (arg1))
24262 {
24263 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24264 arg1 = TREE_TYPE (arg1);
24265 quals1 = cp_type_quals (arg1);
24266 }
24267
24268 if (TYPE_REF_P (arg2))
24269 {
24270 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24271 arg2 = TREE_TYPE (arg2);
24272 quals2 = cp_type_quals (arg2);
24273 }
24274
24275 arg1 = TYPE_MAIN_VARIANT (arg1);
24276 arg2 = TYPE_MAIN_VARIANT (arg2);
24277
24278 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24279 {
24280 int i, len2 = remaining_arguments (args2);
24281 tree parmvec = make_tree_vec (1);
24282 tree argvec = make_tree_vec (len2);
24283 tree ta = args2;
24284
24285 /* Setup the parameter vector, which contains only ARG1. */
24286 TREE_VEC_ELT (parmvec, 0) = arg1;
24287
24288 /* Setup the argument vector, which contains the remaining
24289 arguments. */
24290 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24291 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24292
24293 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24294 argvec, DEDUCE_EXACT,
24295 /*subr=*/true, /*explain_p=*/false)
24296 == 0);
24297
24298 /* We cannot deduce in the other direction, because ARG1 is
24299 a pack expansion but ARG2 is not. */
24300 deduce2 = 0;
24301 }
24302 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24303 {
24304 int i, len1 = remaining_arguments (args1);
24305 tree parmvec = make_tree_vec (1);
24306 tree argvec = make_tree_vec (len1);
24307 tree ta = args1;
24308
24309 /* Setup the parameter vector, which contains only ARG1. */
24310 TREE_VEC_ELT (parmvec, 0) = arg2;
24311
24312 /* Setup the argument vector, which contains the remaining
24313 arguments. */
24314 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24315 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24316
24317 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24318 argvec, DEDUCE_EXACT,
24319 /*subr=*/true, /*explain_p=*/false)
24320 == 0);
24321
24322 /* We cannot deduce in the other direction, because ARG2 is
24323 a pack expansion but ARG1 is not.*/
24324 deduce1 = 0;
24325 }
24326
24327 else
24328 {
24329 /* The normal case, where neither argument is a pack
24330 expansion. */
24331 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24332 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24333 == 0);
24334 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24335 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24336 == 0);
24337 }
24338
24339 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24340 arg2, then arg2 is not as specialized as arg1. */
24341 if (!deduce1)
24342 lose2 = true;
24343 if (!deduce2)
24344 lose1 = true;
24345
24346 /* "If, for a given type, deduction succeeds in both directions
24347 (i.e., the types are identical after the transformations above)
24348 and both P and A were reference types (before being replaced with
24349 the type referred to above):
24350 - if the type from the argument template was an lvalue reference and
24351 the type from the parameter template was not, the argument type is
24352 considered to be more specialized than the other; otherwise,
24353 - if the type from the argument template is more cv-qualified
24354 than the type from the parameter template (as described above),
24355 the argument type is considered to be more specialized than the other;
24356 otherwise,
24357 - neither type is more specialized than the other." */
24358
24359 if (deduce1 && deduce2)
24360 {
24361 if (ref1 && ref2 && ref1 != ref2)
24362 {
24363 if (ref1 > ref2)
24364 lose1 = true;
24365 else
24366 lose2 = true;
24367 }
24368 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24369 {
24370 if ((quals1 & quals2) == quals2)
24371 lose2 = true;
24372 if ((quals1 & quals2) == quals1)
24373 lose1 = true;
24374 }
24375 }
24376
24377 if (lose1 && lose2)
24378 /* We've failed to deduce something in either direction.
24379 These must be unordered. */
24380 break;
24381
24382 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24383 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24384 /* We have already processed all of the arguments in our
24385 handing of the pack expansion type. */
24386 len = 0;
24387
24388 args1 = TREE_CHAIN (args1);
24389 args2 = TREE_CHAIN (args2);
24390 }
24391
24392 /* "In most cases, all template parameters must have values in order for
24393 deduction to succeed, but for partial ordering purposes a template
24394 parameter may remain without a value provided it is not used in the
24395 types being used for partial ordering."
24396
24397 Thus, if we are missing any of the targs1 we need to substitute into
24398 origs1, then pat2 is not as specialized as pat1. This can happen when
24399 there is a nondeduced context. */
24400 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24401 lose2 = true;
24402 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24403 lose1 = true;
24404
24405 processing_template_decl--;
24406
24407 /* If both deductions succeed, the partial ordering selects the more
24408 constrained template. */
24409 /* P2113: If the corresponding template-parameters of the
24410 template-parameter-lists are not equivalent ([temp.over.link]) or if
24411 the function parameters that positionally correspond between the two
24412 templates are not of the same type, neither template is more
24413 specialized than the other. */
24414 if (!lose1 && !lose2
24415 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24416 DECL_TEMPLATE_PARMS (pat2))
24417 && compparms (origs1, origs2))
24418 {
24419 int winner = more_constrained (decl1, decl2);
24420 if (winner > 0)
24421 lose2 = true;
24422 else if (winner < 0)
24423 lose1 = true;
24424 }
24425
24426 /* All things being equal, if the next argument is a pack expansion
24427 for one function but not for the other, prefer the
24428 non-variadic function. FIXME this is bogus; see c++/41958. */
24429 if (lose1 == lose2
24430 && args1 && TREE_VALUE (args1)
24431 && args2 && TREE_VALUE (args2))
24432 {
24433 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24434 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24435 }
24436
24437 if (lose1 == lose2)
24438 return 0;
24439 else if (!lose1)
24440 return 1;
24441 else
24442 return -1;
24443 }
24444
24445 /* Determine which of two partial specializations of TMPL is more
24446 specialized.
24447
24448 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24449 to the first partial specialization. The TREE_PURPOSE is the
24450 innermost set of template parameters for the partial
24451 specialization. PAT2 is similar, but for the second template.
24452
24453 Return 1 if the first partial specialization is more specialized;
24454 -1 if the second is more specialized; 0 if neither is more
24455 specialized.
24456
24457 See [temp.class.order] for information about determining which of
24458 two templates is more specialized. */
24459
24460 static int
24461 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24462 {
24463 tree targs;
24464 int winner = 0;
24465 bool any_deductions = false;
24466
24467 tree tmpl1 = TREE_VALUE (pat1);
24468 tree tmpl2 = TREE_VALUE (pat2);
24469 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24470 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24471
24472 /* Just like what happens for functions, if we are ordering between
24473 different template specializations, we may encounter dependent
24474 types in the arguments, and we need our dependency check functions
24475 to behave correctly. */
24476 ++processing_template_decl;
24477 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24478 if (targs)
24479 {
24480 --winner;
24481 any_deductions = true;
24482 }
24483
24484 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24485 if (targs)
24486 {
24487 ++winner;
24488 any_deductions = true;
24489 }
24490 --processing_template_decl;
24491
24492 /* If both deductions succeed, the partial ordering selects the more
24493 constrained template. */
24494 if (!winner && any_deductions)
24495 winner = more_constrained (tmpl1, tmpl2);
24496
24497 /* In the case of a tie where at least one of the templates
24498 has a parameter pack at the end, the template with the most
24499 non-packed parameters wins. */
24500 if (winner == 0
24501 && any_deductions
24502 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24503 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24504 {
24505 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24506 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24507 int len1 = TREE_VEC_LENGTH (args1);
24508 int len2 = TREE_VEC_LENGTH (args2);
24509
24510 /* We don't count the pack expansion at the end. */
24511 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24512 --len1;
24513 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24514 --len2;
24515
24516 if (len1 > len2)
24517 return 1;
24518 else if (len1 < len2)
24519 return -1;
24520 }
24521
24522 return winner;
24523 }
24524
24525 /* Return the template arguments that will produce the function signature
24526 DECL from the function template FN, with the explicit template
24527 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24528 also match. Return NULL_TREE if no satisfactory arguments could be
24529 found. */
24530
24531 static tree
24532 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24533 {
24534 int ntparms = DECL_NTPARMS (fn);
24535 tree targs = make_tree_vec (ntparms);
24536 tree decl_type = TREE_TYPE (decl);
24537 tree decl_arg_types;
24538 tree *args;
24539 unsigned int nargs, ix;
24540 tree arg;
24541
24542 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24543
24544 /* Never do unification on the 'this' parameter. */
24545 decl_arg_types = skip_artificial_parms_for (decl,
24546 TYPE_ARG_TYPES (decl_type));
24547
24548 nargs = list_length (decl_arg_types);
24549 args = XALLOCAVEC (tree, nargs);
24550 for (arg = decl_arg_types, ix = 0;
24551 arg != NULL_TREE && arg != void_list_node;
24552 arg = TREE_CHAIN (arg), ++ix)
24553 args[ix] = TREE_VALUE (arg);
24554
24555 if (fn_type_unification (fn, explicit_args, targs,
24556 args, ix,
24557 (check_rettype || DECL_CONV_FN_P (fn)
24558 ? TREE_TYPE (decl_type) : NULL_TREE),
24559 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24560 /*explain_p=*/false,
24561 /*decltype*/false)
24562 == error_mark_node)
24563 return NULL_TREE;
24564
24565 return targs;
24566 }
24567
24568 /* Return the innermost template arguments that, when applied to a partial
24569 specialization SPEC_TMPL of TMPL, yield the ARGS.
24570
24571 For example, suppose we have:
24572
24573 template <class T, class U> struct S {};
24574 template <class T> struct S<T*, int> {};
24575
24576 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24577 partial specialization and the ARGS will be {double*, int}. The resulting
24578 vector will be {double}, indicating that `T' is bound to `double'. */
24579
24580 static tree
24581 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24582 {
24583 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24584 tree spec_args
24585 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24586 int i, ntparms = TREE_VEC_LENGTH (tparms);
24587 tree deduced_args;
24588 tree innermost_deduced_args;
24589
24590 innermost_deduced_args = make_tree_vec (ntparms);
24591 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24592 {
24593 deduced_args = copy_node (args);
24594 SET_TMPL_ARGS_LEVEL (deduced_args,
24595 TMPL_ARGS_DEPTH (deduced_args),
24596 innermost_deduced_args);
24597 }
24598 else
24599 deduced_args = innermost_deduced_args;
24600
24601 bool tried_array_deduction = (cxx_dialect < cxx17);
24602 again:
24603 if (unify (tparms, deduced_args,
24604 INNERMOST_TEMPLATE_ARGS (spec_args),
24605 INNERMOST_TEMPLATE_ARGS (args),
24606 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24607 return NULL_TREE;
24608
24609 for (i = 0; i < ntparms; ++i)
24610 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24611 {
24612 if (!tried_array_deduction)
24613 {
24614 try_array_deduction (tparms, innermost_deduced_args,
24615 INNERMOST_TEMPLATE_ARGS (spec_args));
24616 tried_array_deduction = true;
24617 if (TREE_VEC_ELT (innermost_deduced_args, i))
24618 goto again;
24619 }
24620 return NULL_TREE;
24621 }
24622
24623 if (!push_tinst_level (spec_tmpl, deduced_args))
24624 {
24625 excessive_deduction_depth = true;
24626 return NULL_TREE;
24627 }
24628
24629 /* Verify that nondeduced template arguments agree with the type
24630 obtained from argument deduction.
24631
24632 For example:
24633
24634 struct A { typedef int X; };
24635 template <class T, class U> struct C {};
24636 template <class T> struct C<T, typename T::X> {};
24637
24638 Then with the instantiation `C<A, int>', we can deduce that
24639 `T' is `A' but unify () does not check whether `typename T::X'
24640 is `int'. */
24641 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24642
24643 if (spec_args != error_mark_node)
24644 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24645 INNERMOST_TEMPLATE_ARGS (spec_args),
24646 tmpl, tf_none, false, false);
24647
24648 pop_tinst_level ();
24649
24650 if (spec_args == error_mark_node
24651 /* We only need to check the innermost arguments; the other
24652 arguments will always agree. */
24653 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24654 INNERMOST_TEMPLATE_ARGS (args)))
24655 return NULL_TREE;
24656
24657 /* Now that we have bindings for all of the template arguments,
24658 ensure that the arguments deduced for the template template
24659 parameters have compatible template parameter lists. See the use
24660 of template_template_parm_bindings_ok_p in fn_type_unification
24661 for more information. */
24662 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24663 return NULL_TREE;
24664
24665 return deduced_args;
24666 }
24667
24668 // Compare two function templates T1 and T2 by deducing bindings
24669 // from one against the other. If both deductions succeed, compare
24670 // constraints to see which is more constrained.
24671 static int
24672 more_specialized_inst (tree t1, tree t2)
24673 {
24674 int fate = 0;
24675 int count = 0;
24676
24677 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24678 {
24679 --fate;
24680 ++count;
24681 }
24682
24683 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24684 {
24685 ++fate;
24686 ++count;
24687 }
24688
24689 // If both deductions succeed, then one may be more constrained.
24690 if (count == 2 && fate == 0)
24691 fate = more_constrained (t1, t2);
24692
24693 return fate;
24694 }
24695
24696 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24697 Return the TREE_LIST node with the most specialized template, if
24698 any. If there is no most specialized template, the error_mark_node
24699 is returned.
24700
24701 Note that this function does not look at, or modify, the
24702 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24703 returned is one of the elements of INSTANTIATIONS, callers may
24704 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24705 and retrieve it from the value returned. */
24706
24707 tree
24708 most_specialized_instantiation (tree templates)
24709 {
24710 tree fn, champ;
24711
24712 ++processing_template_decl;
24713
24714 champ = templates;
24715 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24716 {
24717 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24718 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24719 if (fate == -1)
24720 champ = fn;
24721 else if (!fate)
24722 {
24723 /* Equally specialized, move to next function. If there
24724 is no next function, nothing's most specialized. */
24725 fn = TREE_CHAIN (fn);
24726 champ = fn;
24727 if (!fn)
24728 break;
24729 }
24730 }
24731
24732 if (champ)
24733 /* Now verify that champ is better than everything earlier in the
24734 instantiation list. */
24735 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24736 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24737 {
24738 champ = NULL_TREE;
24739 break;
24740 }
24741 }
24742
24743 processing_template_decl--;
24744
24745 if (!champ)
24746 return error_mark_node;
24747
24748 return champ;
24749 }
24750
24751 /* If DECL is a specialization of some template, return the most
24752 general such template. Otherwise, returns NULL_TREE.
24753
24754 For example, given:
24755
24756 template <class T> struct S { template <class U> void f(U); };
24757
24758 if TMPL is `template <class U> void S<int>::f(U)' this will return
24759 the full template. This function will not trace past partial
24760 specializations, however. For example, given in addition:
24761
24762 template <class T> struct S<T*> { template <class U> void f(U); };
24763
24764 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24765 `template <class T> template <class U> S<T*>::f(U)'. */
24766
24767 tree
24768 most_general_template (tree decl)
24769 {
24770 if (TREE_CODE (decl) != TEMPLATE_DECL)
24771 {
24772 if (tree tinfo = get_template_info (decl))
24773 decl = TI_TEMPLATE (tinfo);
24774 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24775 template friend, or a FIELD_DECL for a capture pack. */
24776 if (TREE_CODE (decl) != TEMPLATE_DECL)
24777 return NULL_TREE;
24778 }
24779
24780 /* Look for more and more general templates. */
24781 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24782 {
24783 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24784 (See cp-tree.h for details.) */
24785 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24786 break;
24787
24788 if (CLASS_TYPE_P (TREE_TYPE (decl))
24789 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24790 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24791 break;
24792
24793 /* Stop if we run into an explicitly specialized class template. */
24794 if (!DECL_NAMESPACE_SCOPE_P (decl)
24795 && DECL_CONTEXT (decl)
24796 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24797 break;
24798
24799 decl = DECL_TI_TEMPLATE (decl);
24800 }
24801
24802 return decl;
24803 }
24804
24805 /* Return the most specialized of the template partial specializations
24806 which can produce TARGET, a specialization of some class or variable
24807 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24808 a TEMPLATE_DECL node corresponding to the partial specialization, while
24809 the TREE_PURPOSE is the set of template arguments that must be
24810 substituted into the template pattern in order to generate TARGET.
24811
24812 If the choice of partial specialization is ambiguous, a diagnostic
24813 is issued, and the error_mark_node is returned. If there are no
24814 partial specializations matching TARGET, then NULL_TREE is
24815 returned, indicating that the primary template should be used. */
24816
24817 tree
24818 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24819 {
24820 tree list = NULL_TREE;
24821 tree t;
24822 tree champ;
24823 int fate;
24824 bool ambiguous_p;
24825 tree outer_args = NULL_TREE;
24826 tree tmpl, args;
24827
24828 if (TYPE_P (target))
24829 {
24830 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24831 tmpl = TI_TEMPLATE (tinfo);
24832 args = TI_ARGS (tinfo);
24833 }
24834 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24835 {
24836 tmpl = TREE_OPERAND (target, 0);
24837 args = TREE_OPERAND (target, 1);
24838 }
24839 else if (VAR_P (target))
24840 {
24841 tree tinfo = DECL_TEMPLATE_INFO (target);
24842 tmpl = TI_TEMPLATE (tinfo);
24843 args = TI_ARGS (tinfo);
24844 }
24845 else
24846 gcc_unreachable ();
24847
24848 tree main_tmpl = most_general_template (tmpl);
24849
24850 /* For determining which partial specialization to use, only the
24851 innermost args are interesting. */
24852 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24853 {
24854 outer_args = strip_innermost_template_args (args, 1);
24855 args = INNERMOST_TEMPLATE_ARGS (args);
24856 }
24857
24858 /* The caller hasn't called push_to_top_level yet, but we need
24859 get_partial_spec_bindings to be done in non-template context so that we'll
24860 fully resolve everything. */
24861 processing_template_decl_sentinel ptds;
24862
24863 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24864 {
24865 const tree ospec_tmpl = TREE_VALUE (t);
24866
24867 tree spec_tmpl;
24868 if (outer_args)
24869 {
24870 /* Substitute in the template args from the enclosing class. */
24871 ++processing_template_decl;
24872 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24873 --processing_template_decl;
24874 if (spec_tmpl == error_mark_node)
24875 return error_mark_node;
24876 }
24877 else
24878 spec_tmpl = ospec_tmpl;
24879
24880 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24881 if (spec_args)
24882 {
24883 if (outer_args)
24884 spec_args = add_to_template_args (outer_args, spec_args);
24885
24886 /* Keep the candidate only if the constraints are satisfied,
24887 or if we're not compiling with concepts. */
24888 if (!flag_concepts
24889 || constraints_satisfied_p (ospec_tmpl, spec_args))
24890 {
24891 list = tree_cons (spec_args, ospec_tmpl, list);
24892 TREE_TYPE (list) = TREE_TYPE (t);
24893 }
24894 }
24895 }
24896
24897 if (! list)
24898 return NULL_TREE;
24899
24900 ambiguous_p = false;
24901 t = list;
24902 champ = t;
24903 t = TREE_CHAIN (t);
24904 for (; t; t = TREE_CHAIN (t))
24905 {
24906 fate = more_specialized_partial_spec (tmpl, champ, t);
24907 if (fate == 1)
24908 ;
24909 else
24910 {
24911 if (fate == 0)
24912 {
24913 t = TREE_CHAIN (t);
24914 if (! t)
24915 {
24916 ambiguous_p = true;
24917 break;
24918 }
24919 }
24920 champ = t;
24921 }
24922 }
24923
24924 if (!ambiguous_p)
24925 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24926 {
24927 fate = more_specialized_partial_spec (tmpl, champ, t);
24928 if (fate != 1)
24929 {
24930 ambiguous_p = true;
24931 break;
24932 }
24933 }
24934
24935 if (ambiguous_p)
24936 {
24937 const char *str;
24938 char *spaces = NULL;
24939 if (!(complain & tf_error))
24940 return error_mark_node;
24941 if (TYPE_P (target))
24942 error ("ambiguous template instantiation for %q#T", target);
24943 else
24944 error ("ambiguous template instantiation for %q#D", target);
24945 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24946 for (t = list; t; t = TREE_CHAIN (t))
24947 {
24948 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24949 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24950 "%s %#qS", spaces ? spaces : str, subst);
24951 spaces = spaces ? spaces : get_spaces (str);
24952 }
24953 free (spaces);
24954 return error_mark_node;
24955 }
24956
24957 return champ;
24958 }
24959
24960 /* Explicitly instantiate DECL. */
24961
24962 void
24963 do_decl_instantiation (tree decl, tree storage)
24964 {
24965 tree result = NULL_TREE;
24966 int extern_p = 0;
24967
24968 if (!decl || decl == error_mark_node)
24969 /* An error occurred, for which grokdeclarator has already issued
24970 an appropriate message. */
24971 return;
24972 else if (! DECL_LANG_SPECIFIC (decl))
24973 {
24974 error ("explicit instantiation of non-template %q#D", decl);
24975 return;
24976 }
24977 else if (DECL_DECLARED_CONCEPT_P (decl))
24978 {
24979 if (VAR_P (decl))
24980 error ("explicit instantiation of variable concept %q#D", decl);
24981 else
24982 error ("explicit instantiation of function concept %q#D", decl);
24983 return;
24984 }
24985
24986 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24987 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24988
24989 if (VAR_P (decl) && !var_templ)
24990 {
24991 /* There is an asymmetry here in the way VAR_DECLs and
24992 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24993 the latter, the DECL we get back will be marked as a
24994 template instantiation, and the appropriate
24995 DECL_TEMPLATE_INFO will be set up. This does not happen for
24996 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24997 should handle VAR_DECLs as it currently handles
24998 FUNCTION_DECLs. */
24999 if (!DECL_CLASS_SCOPE_P (decl))
25000 {
25001 error ("%qD is not a static data member of a class template", decl);
25002 return;
25003 }
25004 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
25005 if (!result || !VAR_P (result))
25006 {
25007 error ("no matching template for %qD found", decl);
25008 return;
25009 }
25010 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25011 {
25012 error ("type %qT for explicit instantiation %qD does not match "
25013 "declared type %qT", TREE_TYPE (result), decl,
25014 TREE_TYPE (decl));
25015 return;
25016 }
25017 }
25018 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25019 {
25020 error ("explicit instantiation of %q#D", decl);
25021 return;
25022 }
25023 else
25024 result = decl;
25025
25026 /* Check for various error cases. Note that if the explicit
25027 instantiation is valid the RESULT will currently be marked as an
25028 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25029 until we get here. */
25030
25031 if (DECL_TEMPLATE_SPECIALIZATION (result))
25032 {
25033 /* DR 259 [temp.spec].
25034
25035 Both an explicit instantiation and a declaration of an explicit
25036 specialization shall not appear in a program unless the explicit
25037 instantiation follows a declaration of the explicit specialization.
25038
25039 For a given set of template parameters, if an explicit
25040 instantiation of a template appears after a declaration of an
25041 explicit specialization for that template, the explicit
25042 instantiation has no effect. */
25043 return;
25044 }
25045 else if (DECL_EXPLICIT_INSTANTIATION (result))
25046 {
25047 /* [temp.spec]
25048
25049 No program shall explicitly instantiate any template more
25050 than once.
25051
25052 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25053 the first instantiation was `extern' and the second is not,
25054 and EXTERN_P for the opposite case. */
25055 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25056 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25057 /* If an "extern" explicit instantiation follows an ordinary
25058 explicit instantiation, the template is instantiated. */
25059 if (extern_p)
25060 return;
25061 }
25062 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25063 {
25064 error ("no matching template for %qD found", result);
25065 return;
25066 }
25067 else if (!DECL_TEMPLATE_INFO (result))
25068 {
25069 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25070 return;
25071 }
25072
25073 if (storage == NULL_TREE)
25074 ;
25075 else if (storage == ridpointers[(int) RID_EXTERN])
25076 {
25077 if (cxx_dialect == cxx98)
25078 pedwarn (input_location, OPT_Wpedantic,
25079 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25080 "instantiations");
25081 extern_p = 1;
25082 }
25083 else
25084 error ("storage class %qD applied to template instantiation", storage);
25085
25086 check_explicit_instantiation_namespace (result);
25087 mark_decl_instantiated (result, extern_p);
25088 if (! extern_p)
25089 instantiate_decl (result, /*defer_ok=*/true,
25090 /*expl_inst_class_mem_p=*/false);
25091 }
25092
25093 static void
25094 mark_class_instantiated (tree t, int extern_p)
25095 {
25096 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25097 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25098 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25099 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25100 if (! extern_p)
25101 {
25102 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25103 rest_of_type_compilation (t, 1);
25104 }
25105 }
25106
25107 /* Perform an explicit instantiation of template class T. STORAGE, if
25108 non-null, is the RID for extern, inline or static. COMPLAIN is
25109 nonzero if this is called from the parser, zero if called recursively,
25110 since the standard is unclear (as detailed below). */
25111
25112 void
25113 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25114 {
25115 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25116 {
25117 if (tree ti = TYPE_TEMPLATE_INFO (t))
25118 error ("explicit instantiation of non-class template %qD",
25119 TI_TEMPLATE (ti));
25120 else
25121 error ("explicit instantiation of non-template type %qT", t);
25122 return;
25123 }
25124
25125 complete_type (t);
25126
25127 if (!COMPLETE_TYPE_P (t))
25128 {
25129 if (complain & tf_error)
25130 error ("explicit instantiation of %q#T before definition of template",
25131 t);
25132 return;
25133 }
25134
25135 /* At most one of these will be true. */
25136 bool extern_p = false;
25137 bool nomem_p = false;
25138 bool static_p = false;
25139
25140 if (storage != NULL_TREE)
25141 {
25142 if (storage == ridpointers[(int) RID_EXTERN])
25143 {
25144 if (cxx_dialect == cxx98)
25145 pedwarn (input_location, OPT_Wpedantic,
25146 "ISO C++ 1998 forbids the use of %<extern%> on "
25147 "explicit instantiations");
25148 }
25149 else
25150 pedwarn (input_location, OPT_Wpedantic,
25151 "ISO C++ forbids the use of %qE"
25152 " on explicit instantiations", storage);
25153
25154 if (storage == ridpointers[(int) RID_INLINE])
25155 nomem_p = true;
25156 else if (storage == ridpointers[(int) RID_EXTERN])
25157 extern_p = true;
25158 else if (storage == ridpointers[(int) RID_STATIC])
25159 static_p = true;
25160 else
25161 error ("storage class %qD applied to template instantiation",
25162 storage);
25163 }
25164
25165 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25166 /* DR 259 [temp.spec].
25167
25168 Both an explicit instantiation and a declaration of an explicit
25169 specialization shall not appear in a program unless the
25170 explicit instantiation follows a declaration of the explicit
25171 specialization.
25172
25173 For a given set of template parameters, if an explicit
25174 instantiation of a template appears after a declaration of an
25175 explicit specialization for that template, the explicit
25176 instantiation has no effect. */
25177 return;
25178
25179 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25180 {
25181 /* We've already instantiated the template. */
25182
25183 /* [temp.spec]
25184
25185 No program shall explicitly instantiate any template more
25186 than once.
25187
25188 If EXTERN_P then this is ok. */
25189 if (!extern_p && (complain & tf_error))
25190 permerror (input_location,
25191 "duplicate explicit instantiation of %q#T", t);
25192
25193 return;
25194 }
25195
25196 check_explicit_instantiation_namespace (TYPE_NAME (t));
25197 mark_class_instantiated (t, extern_p);
25198
25199 if (nomem_p)
25200 return;
25201
25202 /* In contrast to implicit instantiation, where only the
25203 declarations, and not the definitions, of members are
25204 instantiated, we have here:
25205
25206 [temp.explicit]
25207
25208 An explicit instantiation that names a class template
25209 specialization is also an explicit instantiation of the same
25210 kind (declaration or definition) of each of its members (not
25211 including members inherited from base classes and members
25212 that are templates) that has not been previously explicitly
25213 specialized in the translation unit containing the explicit
25214 instantiation, provided that the associated constraints, if
25215 any, of that member are satisfied by the template arguments
25216 of the explicit instantiation. */
25217 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25218 if ((VAR_P (fld)
25219 || (TREE_CODE (fld) == FUNCTION_DECL
25220 && !static_p
25221 && user_provided_p (fld)))
25222 && DECL_TEMPLATE_INSTANTIATION (fld)
25223 && constraints_satisfied_p (fld))
25224 {
25225 mark_decl_instantiated (fld, extern_p);
25226 if (! extern_p)
25227 instantiate_decl (fld, /*defer_ok=*/true,
25228 /*expl_inst_class_mem_p=*/true);
25229 }
25230 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25231 {
25232 tree type = TREE_TYPE (fld);
25233
25234 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25235 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25236 do_type_instantiation (type, storage, 0);
25237 }
25238 }
25239
25240 /* Given a function DECL, which is a specialization of TMPL, modify
25241 DECL to be a re-instantiation of TMPL with the same template
25242 arguments. TMPL should be the template into which tsubst'ing
25243 should occur for DECL, not the most general template.
25244
25245 One reason for doing this is a scenario like this:
25246
25247 template <class T>
25248 void f(const T&, int i);
25249
25250 void g() { f(3, 7); }
25251
25252 template <class T>
25253 void f(const T& t, const int i) { }
25254
25255 Note that when the template is first instantiated, with
25256 instantiate_template, the resulting DECL will have no name for the
25257 first parameter, and the wrong type for the second. So, when we go
25258 to instantiate the DECL, we regenerate it. */
25259
25260 static void
25261 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25262 {
25263 /* The arguments used to instantiate DECL, from the most general
25264 template. */
25265 tree code_pattern;
25266
25267 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25268
25269 /* Make sure that we can see identifiers, and compute access
25270 correctly. */
25271 push_access_scope (decl);
25272
25273 if (TREE_CODE (decl) == FUNCTION_DECL)
25274 {
25275 tree decl_parm;
25276 tree pattern_parm;
25277 tree specs;
25278 int args_depth;
25279 int parms_depth;
25280
25281 args_depth = TMPL_ARGS_DEPTH (args);
25282 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25283 if (args_depth > parms_depth)
25284 args = get_innermost_template_args (args, parms_depth);
25285
25286 /* Instantiate a dynamic exception-specification. noexcept will be
25287 handled below. */
25288 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25289 if (TREE_VALUE (raises))
25290 {
25291 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25292 args, tf_error, NULL_TREE,
25293 /*defer_ok*/false);
25294 if (specs && specs != error_mark_node)
25295 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25296 specs);
25297 }
25298
25299 /* Merge parameter declarations. */
25300 decl_parm = skip_artificial_parms_for (decl,
25301 DECL_ARGUMENTS (decl));
25302 pattern_parm
25303 = skip_artificial_parms_for (code_pattern,
25304 DECL_ARGUMENTS (code_pattern));
25305 while (decl_parm && !DECL_PACK_P (pattern_parm))
25306 {
25307 tree parm_type;
25308 tree attributes;
25309
25310 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25311 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25312 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25313 NULL_TREE);
25314 parm_type = type_decays_to (parm_type);
25315 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25316 TREE_TYPE (decl_parm) = parm_type;
25317 attributes = DECL_ATTRIBUTES (pattern_parm);
25318 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25319 {
25320 DECL_ATTRIBUTES (decl_parm) = attributes;
25321 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25322 }
25323 decl_parm = DECL_CHAIN (decl_parm);
25324 pattern_parm = DECL_CHAIN (pattern_parm);
25325 }
25326 /* Merge any parameters that match with the function parameter
25327 pack. */
25328 if (pattern_parm && DECL_PACK_P (pattern_parm))
25329 {
25330 int i, len;
25331 tree expanded_types;
25332 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25333 the parameters in this function parameter pack. */
25334 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25335 args, tf_error, NULL_TREE);
25336 len = TREE_VEC_LENGTH (expanded_types);
25337 for (i = 0; i < len; i++)
25338 {
25339 tree parm_type;
25340 tree attributes;
25341
25342 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25343 /* Rename the parameter to include the index. */
25344 DECL_NAME (decl_parm) =
25345 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25346 parm_type = TREE_VEC_ELT (expanded_types, i);
25347 parm_type = type_decays_to (parm_type);
25348 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25349 TREE_TYPE (decl_parm) = parm_type;
25350 attributes = DECL_ATTRIBUTES (pattern_parm);
25351 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25352 {
25353 DECL_ATTRIBUTES (decl_parm) = attributes;
25354 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25355 }
25356 decl_parm = DECL_CHAIN (decl_parm);
25357 }
25358 }
25359 /* Merge additional specifiers from the CODE_PATTERN. */
25360 if (DECL_DECLARED_INLINE_P (code_pattern)
25361 && !DECL_DECLARED_INLINE_P (decl))
25362 DECL_DECLARED_INLINE_P (decl) = 1;
25363
25364 maybe_instantiate_noexcept (decl, tf_error);
25365 }
25366 else if (VAR_P (decl))
25367 {
25368 start_lambda_scope (decl);
25369 DECL_INITIAL (decl) =
25370 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25371 tf_error, DECL_TI_TEMPLATE (decl));
25372 finish_lambda_scope ();
25373 if (VAR_HAD_UNKNOWN_BOUND (decl))
25374 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25375 tf_error, DECL_TI_TEMPLATE (decl));
25376 }
25377 else
25378 gcc_unreachable ();
25379
25380 pop_access_scope (decl);
25381 }
25382
25383 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25384 substituted to get DECL. */
25385
25386 tree
25387 template_for_substitution (tree decl)
25388 {
25389 tree tmpl = DECL_TI_TEMPLATE (decl);
25390
25391 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25392 for the instantiation. This is not always the most general
25393 template. Consider, for example:
25394
25395 template <class T>
25396 struct S { template <class U> void f();
25397 template <> void f<int>(); };
25398
25399 and an instantiation of S<double>::f<int>. We want TD to be the
25400 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25401 while (/* An instantiation cannot have a definition, so we need a
25402 more general template. */
25403 DECL_TEMPLATE_INSTANTIATION (tmpl)
25404 /* We must also deal with friend templates. Given:
25405
25406 template <class T> struct S {
25407 template <class U> friend void f() {};
25408 };
25409
25410 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25411 so far as the language is concerned, but that's still
25412 where we get the pattern for the instantiation from. On
25413 other hand, if the definition comes outside the class, say:
25414
25415 template <class T> struct S {
25416 template <class U> friend void f();
25417 };
25418 template <class U> friend void f() {}
25419
25420 we don't need to look any further. That's what the check for
25421 DECL_INITIAL is for. */
25422 || (TREE_CODE (decl) == FUNCTION_DECL
25423 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25424 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25425 {
25426 /* The present template, TD, should not be a definition. If it
25427 were a definition, we should be using it! Note that we
25428 cannot restructure the loop to just keep going until we find
25429 a template with a definition, since that might go too far if
25430 a specialization was declared, but not defined. */
25431
25432 /* Fetch the more general template. */
25433 tmpl = DECL_TI_TEMPLATE (tmpl);
25434 }
25435
25436 return tmpl;
25437 }
25438
25439 /* Returns true if we need to instantiate this template instance even if we
25440 know we aren't going to emit it. */
25441
25442 bool
25443 always_instantiate_p (tree decl)
25444 {
25445 /* We always instantiate inline functions so that we can inline them. An
25446 explicit instantiation declaration prohibits implicit instantiation of
25447 non-inline functions. With high levels of optimization, we would
25448 normally inline non-inline functions -- but we're not allowed to do
25449 that for "extern template" functions. Therefore, we check
25450 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25451 return ((TREE_CODE (decl) == FUNCTION_DECL
25452 && (DECL_DECLARED_INLINE_P (decl)
25453 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25454 /* And we need to instantiate static data members so that
25455 their initializers are available in integral constant
25456 expressions. */
25457 || (VAR_P (decl)
25458 && decl_maybe_constant_var_p (decl)));
25459 }
25460
25461 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25462 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25463 error, true otherwise. */
25464
25465 bool
25466 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25467 {
25468 if (fn == error_mark_node)
25469 return false;
25470
25471 /* Don't instantiate a noexcept-specification from template context. */
25472 if (processing_template_decl
25473 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25474 return true;
25475
25476 if (DECL_MAYBE_DELETED (fn))
25477 {
25478 if (fn == current_function_decl)
25479 /* We're in start_preparsed_function, keep going. */
25480 return true;
25481
25482 ++function_depth;
25483 synthesize_method (fn);
25484 --function_depth;
25485 return !DECL_MAYBE_DELETED (fn);
25486 }
25487
25488 tree fntype = TREE_TYPE (fn);
25489 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
25490
25491 if (!spec || !TREE_PURPOSE (spec))
25492 return true;
25493
25494 tree noex = TREE_PURPOSE (spec);
25495 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25496 && TREE_CODE (noex) != DEFERRED_PARSE)
25497 return true;
25498
25499 tree orig_fn = NULL_TREE;
25500 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25501 its FUNCTION_DECL for the rest of this function -- push_access_scope
25502 doesn't accept TEMPLATE_DECLs. */
25503 if (DECL_FUNCTION_TEMPLATE_P (fn))
25504 {
25505 orig_fn = fn;
25506 fn = DECL_TEMPLATE_RESULT (fn);
25507 }
25508
25509 if (DECL_CLONED_FUNCTION_P (fn))
25510 {
25511 tree prime = DECL_CLONED_FUNCTION (fn);
25512 if (!maybe_instantiate_noexcept (prime, complain))
25513 return false;
25514 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25515 }
25516 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25517 {
25518 static hash_set<tree>* fns = new hash_set<tree>;
25519 bool added = false;
25520 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25521 {
25522 spec = get_defaulted_eh_spec (fn, complain);
25523 if (spec == error_mark_node)
25524 /* This might have failed because of an unparsed DMI, so
25525 let's try again later. */
25526 return false;
25527 }
25528 else if (!(added = !fns->add (fn)))
25529 {
25530 /* If hash_set::add returns true, the element was already there. */
25531 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25532 DECL_SOURCE_LOCATION (fn));
25533 error_at (loc,
25534 "exception specification of %qD depends on itself",
25535 fn);
25536 spec = noexcept_false_spec;
25537 }
25538 else if (push_tinst_level (fn))
25539 {
25540 push_to_top_level ();
25541 push_access_scope (fn);
25542 push_deferring_access_checks (dk_no_deferred);
25543 input_location = DECL_SOURCE_LOCATION (fn);
25544
25545 if (!DECL_LOCAL_DECL_P (fn))
25546 {
25547 /* If needed, set current_class_ptr for the benefit of
25548 tsubst_copy/PARM_DECL. The exception pattern will
25549 refer to the parm of the template, not the
25550 instantiation. */
25551 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25552 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25553 {
25554 tree this_parm = DECL_ARGUMENTS (tdecl);
25555 current_class_ptr = NULL_TREE;
25556 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25557 current_class_ptr = this_parm;
25558 }
25559 }
25560
25561 /* If this function is represented by a TEMPLATE_DECL, then
25562 the deferred noexcept-specification might still contain
25563 dependent types, even after substitution. And we need the
25564 dependency check functions to work in build_noexcept_spec. */
25565 if (orig_fn)
25566 ++processing_template_decl;
25567
25568 /* Do deferred instantiation of the noexcept-specifier. */
25569 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25570 DEFERRED_NOEXCEPT_ARGS (noex),
25571 tf_warning_or_error, fn,
25572 /*function_p=*/false,
25573 /*i_c_e_p=*/true);
25574
25575 /* Build up the noexcept-specification. */
25576 spec = build_noexcept_spec (noex, tf_warning_or_error);
25577
25578 if (orig_fn)
25579 --processing_template_decl;
25580
25581 pop_deferring_access_checks ();
25582 pop_access_scope (fn);
25583 pop_tinst_level ();
25584 pop_from_top_level ();
25585 }
25586 else
25587 spec = noexcept_false_spec;
25588
25589 if (added)
25590 fns->remove (fn);
25591 }
25592
25593 if (spec == error_mark_node)
25594 {
25595 /* This failed with a hard error, so let's go with false. */
25596 gcc_assert (seen_error ());
25597 spec = noexcept_false_spec;
25598 }
25599
25600 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25601 if (orig_fn)
25602 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25603
25604 return true;
25605 }
25606
25607 /* We're starting to process the function INST, an instantiation of PATTERN;
25608 add their parameters to local_specializations. */
25609
25610 static void
25611 register_parameter_specializations (tree pattern, tree inst)
25612 {
25613 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25614 tree spec_parm = DECL_ARGUMENTS (inst);
25615 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25616 {
25617 register_local_specialization (spec_parm, tmpl_parm);
25618 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25619 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25620 }
25621 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25622 {
25623 if (!DECL_PACK_P (tmpl_parm)
25624 || (spec_parm && DECL_PACK_P (spec_parm)))
25625 {
25626 register_local_specialization (spec_parm, tmpl_parm);
25627 spec_parm = DECL_CHAIN (spec_parm);
25628 }
25629 else
25630 {
25631 /* Register the (value) argument pack as a specialization of
25632 TMPL_PARM, then move on. */
25633 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25634 register_local_specialization (argpack, tmpl_parm);
25635 }
25636 }
25637 gcc_assert (!spec_parm);
25638 }
25639
25640 /* Instantiate the body of D using PATTERN with ARGS. We have
25641 already determined PATTERN is the correct template to use.
25642 NESTED_P is true if this is a nested function, in which case
25643 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25644
25645 static void
25646 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25647 {
25648 tree td = NULL_TREE;
25649 tree code_pattern = pattern;
25650
25651 if (!nested_p)
25652 {
25653 td = pattern;
25654 code_pattern = DECL_TEMPLATE_RESULT (td);
25655 }
25656 else
25657 /* Only OMP reductions are nested. */
25658 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25659
25660 vec<tree> omp_privatization_save;
25661 if (current_function_decl)
25662 save_omp_privatization_clauses (omp_privatization_save);
25663
25664 bool push_to_top
25665 = !(current_function_decl
25666 && !LAMBDA_FUNCTION_P (d)
25667 && decl_function_context (d) == current_function_decl);
25668
25669 if (push_to_top)
25670 push_to_top_level ();
25671 else
25672 {
25673 gcc_assert (!processing_template_decl);
25674 push_function_context ();
25675 cp_unevaluated_operand = 0;
25676 c_inhibit_evaluation_warnings = 0;
25677 }
25678
25679 if (VAR_P (d))
25680 {
25681 /* The variable might be a lambda's extra scope, and that
25682 lambda's visibility depends on D's. */
25683 maybe_commonize_var (d);
25684 determine_visibility (d);
25685 }
25686
25687 /* Mark D as instantiated so that recursive calls to
25688 instantiate_decl do not try to instantiate it again. */
25689 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25690
25691 if (td)
25692 /* Regenerate the declaration in case the template has been modified
25693 by a subsequent redeclaration. */
25694 regenerate_decl_from_template (d, td, args);
25695
25696 /* We already set the file and line above. Reset them now in case
25697 they changed as a result of calling regenerate_decl_from_template. */
25698 input_location = DECL_SOURCE_LOCATION (d);
25699
25700 if (VAR_P (d))
25701 {
25702 /* Clear out DECL_RTL; whatever was there before may not be right
25703 since we've reset the type of the declaration. */
25704 SET_DECL_RTL (d, NULL);
25705 DECL_IN_AGGR_P (d) = 0;
25706
25707 /* The initializer is placed in DECL_INITIAL by
25708 regenerate_decl_from_template so we don't need to
25709 push/pop_access_scope again here. Pull it out so that
25710 cp_finish_decl can process it. */
25711 bool const_init = false;
25712 tree init = DECL_INITIAL (d);
25713 DECL_INITIAL (d) = NULL_TREE;
25714 DECL_INITIALIZED_P (d) = 0;
25715
25716 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25717 initializer. That function will defer actual emission until
25718 we have a chance to determine linkage. */
25719 DECL_EXTERNAL (d) = 0;
25720
25721 /* Enter the scope of D so that access-checking works correctly. */
25722 bool enter_context = DECL_CLASS_SCOPE_P (d);
25723 if (enter_context)
25724 push_nested_class (DECL_CONTEXT (d));
25725
25726 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25727 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25728
25729 if (enter_context)
25730 pop_nested_class ();
25731 }
25732 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25733 synthesize_method (d);
25734 else if (TREE_CODE (d) == FUNCTION_DECL)
25735 {
25736 /* Set up the list of local specializations. */
25737 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25738 tree block = NULL_TREE;
25739
25740 /* Set up context. */
25741 if (nested_p)
25742 block = push_stmt_list ();
25743 else
25744 {
25745 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25746
25747 perform_instantiation_time_access_checks (code_pattern, args);
25748 }
25749
25750 /* Create substitution entries for the parameters. */
25751 register_parameter_specializations (code_pattern, d);
25752
25753 /* Substitute into the body of the function. */
25754 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25755 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25756 tf_warning_or_error, d);
25757 else
25758 {
25759 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25760 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25761 /*integral_constant_expression_p=*/false);
25762
25763 /* Set the current input_location to the end of the function
25764 so that finish_function knows where we are. */
25765 input_location
25766 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25767
25768 /* Remember if we saw an infinite loop in the template. */
25769 current_function_infinite_loop
25770 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25771 }
25772
25773 /* Finish the function. */
25774 if (nested_p)
25775 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25776 else
25777 {
25778 d = finish_function (/*inline_p=*/false);
25779 expand_or_defer_fn (d);
25780 }
25781
25782 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25783 cp_check_omp_declare_reduction (d);
25784 }
25785
25786 /* We're not deferring instantiation any more. */
25787 if (!nested_p)
25788 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25789
25790 if (push_to_top)
25791 pop_from_top_level ();
25792 else
25793 pop_function_context ();
25794
25795 if (current_function_decl)
25796 restore_omp_privatization_clauses (omp_privatization_save);
25797 }
25798
25799 /* Produce the definition of D, a _DECL generated from a template. If
25800 DEFER_OK is true, then we don't have to actually do the
25801 instantiation now; we just have to do it sometime. Normally it is
25802 an error if this is an explicit instantiation but D is undefined.
25803 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25804 instantiated class template. */
25805
25806 tree
25807 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25808 {
25809 tree tmpl = DECL_TI_TEMPLATE (d);
25810 tree gen_args;
25811 tree args;
25812 tree td;
25813 tree code_pattern;
25814 tree spec;
25815 tree gen_tmpl;
25816 bool pattern_defined;
25817 location_t saved_loc = input_location;
25818 int saved_unevaluated_operand = cp_unevaluated_operand;
25819 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25820 bool external_p;
25821 bool deleted_p;
25822
25823 /* This function should only be used to instantiate templates for
25824 functions and static member variables. */
25825 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25826
25827 /* A concept is never instantiated. */
25828 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25829
25830 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25831
25832 /* Variables are never deferred; if instantiation is required, they
25833 are instantiated right away. That allows for better code in the
25834 case that an expression refers to the value of the variable --
25835 if the variable has a constant value the referring expression can
25836 take advantage of that fact. */
25837 if (VAR_P (d))
25838 defer_ok = false;
25839
25840 /* Don't instantiate cloned functions. Instead, instantiate the
25841 functions they cloned. */
25842 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25843 d = DECL_CLONED_FUNCTION (d);
25844
25845 if (DECL_TEMPLATE_INSTANTIATED (d)
25846 || TREE_TYPE (d) == error_mark_node
25847 || (TREE_CODE (d) == FUNCTION_DECL
25848 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25849 || DECL_TEMPLATE_SPECIALIZATION (d))
25850 /* D has already been instantiated or explicitly specialized, so
25851 there's nothing for us to do here.
25852
25853 It might seem reasonable to check whether or not D is an explicit
25854 instantiation, and, if so, stop here. But when an explicit
25855 instantiation is deferred until the end of the compilation,
25856 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25857 the instantiation. */
25858 return d;
25859
25860 /* Check to see whether we know that this template will be
25861 instantiated in some other file, as with "extern template"
25862 extension. */
25863 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25864
25865 /* In general, we do not instantiate such templates. */
25866 if (external_p && !always_instantiate_p (d))
25867 return d;
25868
25869 gen_tmpl = most_general_template (tmpl);
25870 gen_args = DECL_TI_ARGS (d);
25871
25872 /* We should already have the extra args. */
25873 gcc_checking_assert (tmpl == gen_tmpl
25874 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25875 == TMPL_ARGS_DEPTH (gen_args)));
25876 /* And what's in the hash table should match D. */
25877 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25878 == d
25879 || spec == NULL_TREE);
25880
25881 /* This needs to happen before any tsubsting. */
25882 if (! push_tinst_level (d))
25883 return d;
25884
25885 timevar_push (TV_TEMPLATE_INST);
25886
25887 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25888 for the instantiation. */
25889 td = template_for_substitution (d);
25890 args = gen_args;
25891
25892 if (VAR_P (d))
25893 {
25894 /* Look up an explicit specialization, if any. */
25895 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25896 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25897 if (elt && elt != error_mark_node)
25898 {
25899 td = TREE_VALUE (elt);
25900 args = TREE_PURPOSE (elt);
25901 }
25902 }
25903
25904 code_pattern = DECL_TEMPLATE_RESULT (td);
25905
25906 /* We should never be trying to instantiate a member of a class
25907 template or partial specialization. */
25908 gcc_assert (d != code_pattern);
25909
25910 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25911 || DECL_TEMPLATE_SPECIALIZATION (td))
25912 /* In the case of a friend template whose definition is provided
25913 outside the class, we may have too many arguments. Drop the
25914 ones we don't need. The same is true for specializations. */
25915 args = get_innermost_template_args
25916 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25917
25918 if (TREE_CODE (d) == FUNCTION_DECL)
25919 {
25920 deleted_p = DECL_DELETED_FN (code_pattern);
25921 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25922 && DECL_INITIAL (code_pattern) != error_mark_node)
25923 || DECL_DEFAULTED_FN (code_pattern)
25924 || deleted_p);
25925 }
25926 else
25927 {
25928 deleted_p = false;
25929 if (DECL_CLASS_SCOPE_P (code_pattern))
25930 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25931 else
25932 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25933 }
25934
25935 /* We may be in the middle of deferred access check. Disable it now. */
25936 push_deferring_access_checks (dk_no_deferred);
25937
25938 /* Unless an explicit instantiation directive has already determined
25939 the linkage of D, remember that a definition is available for
25940 this entity. */
25941 if (pattern_defined
25942 && !DECL_INTERFACE_KNOWN (d)
25943 && !DECL_NOT_REALLY_EXTERN (d))
25944 mark_definable (d);
25945
25946 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25947 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25948 input_location = DECL_SOURCE_LOCATION (d);
25949
25950 /* If D is a member of an explicitly instantiated class template,
25951 and no definition is available, treat it like an implicit
25952 instantiation. */
25953 if (!pattern_defined && expl_inst_class_mem_p
25954 && DECL_EXPLICIT_INSTANTIATION (d))
25955 {
25956 /* Leave linkage flags alone on instantiations with anonymous
25957 visibility. */
25958 if (TREE_PUBLIC (d))
25959 {
25960 DECL_NOT_REALLY_EXTERN (d) = 0;
25961 DECL_INTERFACE_KNOWN (d) = 0;
25962 }
25963 SET_DECL_IMPLICIT_INSTANTIATION (d);
25964 }
25965
25966 /* Defer all other templates, unless we have been explicitly
25967 forbidden from doing so. */
25968 if (/* If there is no definition, we cannot instantiate the
25969 template. */
25970 ! pattern_defined
25971 /* If it's OK to postpone instantiation, do so. */
25972 || defer_ok
25973 /* If this is a static data member that will be defined
25974 elsewhere, we don't want to instantiate the entire data
25975 member, but we do want to instantiate the initializer so that
25976 we can substitute that elsewhere. */
25977 || (external_p && VAR_P (d))
25978 /* Handle here a deleted function too, avoid generating
25979 its body (c++/61080). */
25980 || deleted_p)
25981 {
25982 /* The definition of the static data member is now required so
25983 we must substitute the initializer. */
25984 if (VAR_P (d)
25985 && !DECL_INITIAL (d)
25986 && DECL_INITIAL (code_pattern))
25987 {
25988 tree ns;
25989 tree init;
25990 bool const_init = false;
25991 bool enter_context = DECL_CLASS_SCOPE_P (d);
25992
25993 ns = decl_namespace_context (d);
25994 push_nested_namespace (ns);
25995 if (enter_context)
25996 push_nested_class (DECL_CONTEXT (d));
25997 init = tsubst_expr (DECL_INITIAL (code_pattern),
25998 args,
25999 tf_warning_or_error, NULL_TREE,
26000 /*integral_constant_expression_p=*/false);
26001 /* If instantiating the initializer involved instantiating this
26002 again, don't call cp_finish_decl twice. */
26003 if (!DECL_INITIAL (d))
26004 {
26005 /* Make sure the initializer is still constant, in case of
26006 circular dependency (template/instantiate6.C). */
26007 const_init
26008 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26009 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26010 /*asmspec_tree=*/NULL_TREE,
26011 LOOKUP_ONLYCONVERTING);
26012 }
26013 if (enter_context)
26014 pop_nested_class ();
26015 pop_nested_namespace (ns);
26016 }
26017
26018 /* We restore the source position here because it's used by
26019 add_pending_template. */
26020 input_location = saved_loc;
26021
26022 if (at_eof && !pattern_defined
26023 && DECL_EXPLICIT_INSTANTIATION (d)
26024 && DECL_NOT_REALLY_EXTERN (d))
26025 /* [temp.explicit]
26026
26027 The definition of a non-exported function template, a
26028 non-exported member function template, or a non-exported
26029 member function or static data member of a class template
26030 shall be present in every translation unit in which it is
26031 explicitly instantiated. */
26032 permerror (input_location, "explicit instantiation of %qD "
26033 "but no definition available", d);
26034
26035 /* If we're in unevaluated context, we just wanted to get the
26036 constant value; this isn't an odr use, so don't queue
26037 a full instantiation. */
26038 if (!cp_unevaluated_operand
26039 /* ??? Historically, we have instantiated inline functions, even
26040 when marked as "extern template". */
26041 && !(external_p && VAR_P (d)))
26042 add_pending_template (d);
26043 }
26044 else
26045 {
26046 if (variable_template_p (gen_tmpl))
26047 note_variable_template_instantiation (d);
26048 instantiate_body (td, args, d, false);
26049 }
26050
26051 pop_deferring_access_checks ();
26052 timevar_pop (TV_TEMPLATE_INST);
26053 pop_tinst_level ();
26054 input_location = saved_loc;
26055 cp_unevaluated_operand = saved_unevaluated_operand;
26056 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26057
26058 return d;
26059 }
26060
26061 /* Run through the list of templates that we wish we could
26062 instantiate, and instantiate any we can. RETRIES is the
26063 number of times we retry pending template instantiation. */
26064
26065 void
26066 instantiate_pending_templates (int retries)
26067 {
26068 int reconsider;
26069 location_t saved_loc = input_location;
26070
26071 /* Instantiating templates may trigger vtable generation. This in turn
26072 may require further template instantiations. We place a limit here
26073 to avoid infinite loop. */
26074 if (pending_templates && retries >= max_tinst_depth)
26075 {
26076 tree decl = pending_templates->tinst->maybe_get_node ();
26077
26078 fatal_error (input_location,
26079 "template instantiation depth exceeds maximum of %d"
26080 " instantiating %q+D, possibly from virtual table generation"
26081 " (use %<-ftemplate-depth=%> to increase the maximum)",
26082 max_tinst_depth, decl);
26083 if (TREE_CODE (decl) == FUNCTION_DECL)
26084 /* Pretend that we defined it. */
26085 DECL_INITIAL (decl) = error_mark_node;
26086 return;
26087 }
26088
26089 do
26090 {
26091 struct pending_template **t = &pending_templates;
26092 struct pending_template *last = NULL;
26093 reconsider = 0;
26094 while (*t)
26095 {
26096 tree instantiation = reopen_tinst_level ((*t)->tinst);
26097 bool complete = false;
26098
26099 if (TYPE_P (instantiation))
26100 {
26101 if (!COMPLETE_TYPE_P (instantiation))
26102 {
26103 instantiate_class_template (instantiation);
26104 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26105 for (tree fld = TYPE_FIELDS (instantiation);
26106 fld; fld = TREE_CHAIN (fld))
26107 if ((VAR_P (fld)
26108 || (TREE_CODE (fld) == FUNCTION_DECL
26109 && !DECL_ARTIFICIAL (fld)))
26110 && DECL_TEMPLATE_INSTANTIATION (fld))
26111 instantiate_decl (fld,
26112 /*defer_ok=*/false,
26113 /*expl_inst_class_mem_p=*/false);
26114
26115 if (COMPLETE_TYPE_P (instantiation))
26116 reconsider = 1;
26117 }
26118
26119 complete = COMPLETE_TYPE_P (instantiation);
26120 }
26121 else
26122 {
26123 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26124 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26125 {
26126 instantiation
26127 = instantiate_decl (instantiation,
26128 /*defer_ok=*/false,
26129 /*expl_inst_class_mem_p=*/false);
26130 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26131 reconsider = 1;
26132 }
26133
26134 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26135 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26136 }
26137
26138 if (complete)
26139 {
26140 /* If INSTANTIATION has been instantiated, then we don't
26141 need to consider it again in the future. */
26142 struct pending_template *drop = *t;
26143 *t = (*t)->next;
26144 set_refcount_ptr (drop->tinst);
26145 pending_template_freelist ().free (drop);
26146 }
26147 else
26148 {
26149 last = *t;
26150 t = &(*t)->next;
26151 }
26152 tinst_depth = 0;
26153 set_refcount_ptr (current_tinst_level);
26154 }
26155 last_pending_template = last;
26156 }
26157 while (reconsider);
26158
26159 input_location = saved_loc;
26160 }
26161
26162 /* Substitute ARGVEC into T, which is a list of initializers for
26163 either base class or a non-static data member. The TREE_PURPOSEs
26164 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26165 instantiate_decl. */
26166
26167 static tree
26168 tsubst_initializer_list (tree t, tree argvec)
26169 {
26170 tree inits = NULL_TREE;
26171 tree target_ctor = error_mark_node;
26172
26173 for (; t; t = TREE_CHAIN (t))
26174 {
26175 tree decl;
26176 tree init;
26177 tree expanded_bases = NULL_TREE;
26178 tree expanded_arguments = NULL_TREE;
26179 int i, len = 1;
26180
26181 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26182 {
26183 tree expr;
26184 tree arg;
26185
26186 /* Expand the base class expansion type into separate base
26187 classes. */
26188 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26189 tf_warning_or_error,
26190 NULL_TREE);
26191 if (expanded_bases == error_mark_node)
26192 continue;
26193
26194 /* We'll be building separate TREE_LISTs of arguments for
26195 each base. */
26196 len = TREE_VEC_LENGTH (expanded_bases);
26197 expanded_arguments = make_tree_vec (len);
26198 for (i = 0; i < len; i++)
26199 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26200
26201 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26202 expand each argument in the TREE_VALUE of t. */
26203 expr = make_node (EXPR_PACK_EXPANSION);
26204 PACK_EXPANSION_LOCAL_P (expr) = true;
26205 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26206 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26207
26208 if (TREE_VALUE (t) == void_type_node)
26209 /* VOID_TYPE_NODE is used to indicate
26210 value-initialization. */
26211 {
26212 for (i = 0; i < len; i++)
26213 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26214 }
26215 else
26216 {
26217 /* Substitute parameter packs into each argument in the
26218 TREE_LIST. */
26219 in_base_initializer = 1;
26220 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26221 {
26222 tree expanded_exprs;
26223
26224 /* Expand the argument. */
26225 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26226 expanded_exprs
26227 = tsubst_pack_expansion (expr, argvec,
26228 tf_warning_or_error,
26229 NULL_TREE);
26230 if (expanded_exprs == error_mark_node)
26231 continue;
26232
26233 /* Prepend each of the expanded expressions to the
26234 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26235 for (i = 0; i < len; i++)
26236 {
26237 TREE_VEC_ELT (expanded_arguments, i) =
26238 tree_cons (NULL_TREE,
26239 TREE_VEC_ELT (expanded_exprs, i),
26240 TREE_VEC_ELT (expanded_arguments, i));
26241 }
26242 }
26243 in_base_initializer = 0;
26244
26245 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26246 since we built them backwards. */
26247 for (i = 0; i < len; i++)
26248 {
26249 TREE_VEC_ELT (expanded_arguments, i) =
26250 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26251 }
26252 }
26253 }
26254
26255 for (i = 0; i < len; ++i)
26256 {
26257 if (expanded_bases)
26258 {
26259 decl = TREE_VEC_ELT (expanded_bases, i);
26260 decl = expand_member_init (decl);
26261 init = TREE_VEC_ELT (expanded_arguments, i);
26262 }
26263 else
26264 {
26265 tree tmp;
26266 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26267 tf_warning_or_error, NULL_TREE);
26268
26269 decl = expand_member_init (decl);
26270 if (decl && !DECL_P (decl))
26271 in_base_initializer = 1;
26272
26273 init = TREE_VALUE (t);
26274 tmp = init;
26275 if (init != void_type_node)
26276 init = tsubst_expr (init, argvec,
26277 tf_warning_or_error, NULL_TREE,
26278 /*integral_constant_expression_p=*/false);
26279 if (init == NULL_TREE && tmp != NULL_TREE)
26280 /* If we had an initializer but it instantiated to nothing,
26281 value-initialize the object. This will only occur when
26282 the initializer was a pack expansion where the parameter
26283 packs used in that expansion were of length zero. */
26284 init = void_type_node;
26285 in_base_initializer = 0;
26286 }
26287
26288 if (target_ctor != error_mark_node
26289 && init != error_mark_node)
26290 {
26291 error ("mem-initializer for %qD follows constructor delegation",
26292 decl);
26293 return inits;
26294 }
26295 /* Look for a target constructor. */
26296 if (init != error_mark_node
26297 && decl && CLASS_TYPE_P (decl)
26298 && same_type_p (decl, current_class_type))
26299 {
26300 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26301 if (inits)
26302 {
26303 error ("constructor delegation follows mem-initializer for %qD",
26304 TREE_PURPOSE (inits));
26305 continue;
26306 }
26307 target_ctor = init;
26308 }
26309
26310 if (decl)
26311 {
26312 init = build_tree_list (decl, init);
26313 /* Carry over the dummy TREE_TYPE node containing the source
26314 location. */
26315 TREE_TYPE (init) = TREE_TYPE (t);
26316 TREE_CHAIN (init) = inits;
26317 inits = init;
26318 }
26319 }
26320 }
26321 return inits;
26322 }
26323
26324 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26325
26326 static void
26327 set_current_access_from_decl (tree decl)
26328 {
26329 if (TREE_PRIVATE (decl))
26330 current_access_specifier = access_private_node;
26331 else if (TREE_PROTECTED (decl))
26332 current_access_specifier = access_protected_node;
26333 else
26334 current_access_specifier = access_public_node;
26335 }
26336
26337 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26338 is the instantiation (which should have been created with
26339 start_enum) and ARGS are the template arguments to use. */
26340
26341 static void
26342 tsubst_enum (tree tag, tree newtag, tree args)
26343 {
26344 tree e;
26345
26346 if (SCOPED_ENUM_P (newtag))
26347 begin_scope (sk_scoped_enum, newtag);
26348
26349 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26350 {
26351 tree value;
26352 tree decl;
26353
26354 decl = TREE_VALUE (e);
26355 /* Note that in a template enum, the TREE_VALUE is the
26356 CONST_DECL, not the corresponding INTEGER_CST. */
26357 value = tsubst_expr (DECL_INITIAL (decl),
26358 args, tf_warning_or_error, NULL_TREE,
26359 /*integral_constant_expression_p=*/true);
26360
26361 /* Give this enumeration constant the correct access. */
26362 set_current_access_from_decl (decl);
26363
26364 /* Actually build the enumerator itself. Here we're assuming that
26365 enumerators can't have dependent attributes. */
26366 build_enumerator (DECL_NAME (decl), value, newtag,
26367 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26368 }
26369
26370 if (SCOPED_ENUM_P (newtag))
26371 finish_scope ();
26372
26373 finish_enum_value_list (newtag);
26374 finish_enum (newtag);
26375
26376 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26377 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26378 }
26379
26380 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26381 its type -- but without substituting the innermost set of template
26382 arguments. So, innermost set of template parameters will appear in
26383 the type. */
26384
26385 tree
26386 get_mostly_instantiated_function_type (tree decl)
26387 {
26388 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26389 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26390 }
26391
26392 /* Return truthvalue if we're processing a template different from
26393 the last one involved in diagnostics. */
26394 bool
26395 problematic_instantiation_changed (void)
26396 {
26397 return current_tinst_level != last_error_tinst_level;
26398 }
26399
26400 /* Remember current template involved in diagnostics. */
26401 void
26402 record_last_problematic_instantiation (void)
26403 {
26404 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26405 }
26406
26407 struct tinst_level *
26408 current_instantiation (void)
26409 {
26410 return current_tinst_level;
26411 }
26412
26413 /* Return TRUE if current_function_decl is being instantiated, false
26414 otherwise. */
26415
26416 bool
26417 instantiating_current_function_p (void)
26418 {
26419 return (current_instantiation ()
26420 && (current_instantiation ()->maybe_get_node ()
26421 == current_function_decl));
26422 }
26423
26424 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26425 type. Return false for ok, true for disallowed. Issue error and
26426 inform messages under control of COMPLAIN. */
26427
26428 static bool
26429 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26430 {
26431 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26432 return false;
26433 else if (TYPE_PTR_P (type))
26434 return false;
26435 else if (TYPE_REF_P (type)
26436 && !TYPE_REF_IS_RVALUE (type))
26437 return false;
26438 else if (TYPE_PTRMEM_P (type))
26439 return false;
26440 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26441 {
26442 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26443 {
26444 if (complain & tf_error)
26445 error ("non-type template parameters of deduced class type only "
26446 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26447 return true;
26448 }
26449 return false;
26450 }
26451 else if (TREE_CODE (type) == TYPENAME_TYPE)
26452 return false;
26453 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26454 return false;
26455 else if (TREE_CODE (type) == NULLPTR_TYPE)
26456 return false;
26457 /* A bound template template parm could later be instantiated to have a valid
26458 nontype parm type via an alias template. */
26459 else if (cxx_dialect >= cxx11
26460 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26461 return false;
26462 else if (VOID_TYPE_P (type))
26463 /* Fall through. */;
26464 else if (cxx_dialect >= cxx20)
26465 {
26466 if (dependent_type_p (type))
26467 return false;
26468 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26469 return true;
26470 if (structural_type_p (type))
26471 return false;
26472 if (complain & tf_error)
26473 {
26474 auto_diagnostic_group d;
26475 error ("%qT is not a valid type for a template non-type "
26476 "parameter because it is not structural", type);
26477 structural_type_p (type, true);
26478 }
26479 return true;
26480 }
26481 else if (CLASS_TYPE_P (type))
26482 {
26483 if (complain & tf_error)
26484 error ("non-type template parameters of class type only available "
26485 "with %<-std=c++20%> or %<-std=gnu++20%>");
26486 return true;
26487 }
26488
26489 if (complain & tf_error)
26490 {
26491 if (type == error_mark_node)
26492 inform (input_location, "invalid template non-type parameter");
26493 else
26494 error ("%q#T is not a valid type for a template non-type parameter",
26495 type);
26496 }
26497 return true;
26498 }
26499
26500 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26501 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26502
26503 static bool
26504 dependent_type_p_r (tree type)
26505 {
26506 tree scope;
26507
26508 /* [temp.dep.type]
26509
26510 A type is dependent if it is:
26511
26512 -- a template parameter. Template template parameters are types
26513 for us (since TYPE_P holds true for them) so we handle
26514 them here. */
26515 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26516 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26517 return true;
26518 /* -- a qualified-id with a nested-name-specifier which contains a
26519 class-name that names a dependent type or whose unqualified-id
26520 names a dependent type. */
26521 if (TREE_CODE (type) == TYPENAME_TYPE)
26522 return true;
26523
26524 /* An alias template specialization can be dependent even if the
26525 resulting type is not. */
26526 if (dependent_alias_template_spec_p (type, nt_transparent))
26527 return true;
26528
26529 /* -- a cv-qualified type where the cv-unqualified type is
26530 dependent.
26531 No code is necessary for this bullet; the code below handles
26532 cv-qualified types, and we don't want to strip aliases with
26533 TYPE_MAIN_VARIANT because of DR 1558. */
26534 /* -- a compound type constructed from any dependent type. */
26535 if (TYPE_PTRMEM_P (type))
26536 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26537 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26538 (type)));
26539 else if (INDIRECT_TYPE_P (type))
26540 return dependent_type_p (TREE_TYPE (type));
26541 else if (FUNC_OR_METHOD_TYPE_P (type))
26542 {
26543 tree arg_type;
26544
26545 if (dependent_type_p (TREE_TYPE (type)))
26546 return true;
26547 for (arg_type = TYPE_ARG_TYPES (type);
26548 arg_type;
26549 arg_type = TREE_CHAIN (arg_type))
26550 if (dependent_type_p (TREE_VALUE (arg_type)))
26551 return true;
26552 if (cxx_dialect >= cxx17)
26553 /* A value-dependent noexcept-specifier makes the type dependent. */
26554 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26555 if (tree noex = TREE_PURPOSE (spec))
26556 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26557 affect overload resolution and treating it as dependent breaks
26558 things. Same for an unparsed noexcept expression. */
26559 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26560 && TREE_CODE (noex) != DEFERRED_PARSE
26561 && value_dependent_expression_p (noex))
26562 return true;
26563 return false;
26564 }
26565 /* -- an array type constructed from any dependent type or whose
26566 size is specified by a constant expression that is
26567 value-dependent.
26568
26569 We checked for type- and value-dependence of the bounds in
26570 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26571 if (TREE_CODE (type) == ARRAY_TYPE)
26572 {
26573 if (TYPE_DOMAIN (type)
26574 && dependent_type_p (TYPE_DOMAIN (type)))
26575 return true;
26576 return dependent_type_p (TREE_TYPE (type));
26577 }
26578
26579 /* -- a template-id in which either the template name is a template
26580 parameter ... */
26581 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26582 return true;
26583 /* ... or any of the template arguments is a dependent type or
26584 an expression that is type-dependent or value-dependent. */
26585 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26586 && (any_dependent_template_arguments_p
26587 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26588 return true;
26589
26590 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26591 dependent; if the argument of the `typeof' expression is not
26592 type-dependent, then it should already been have resolved. */
26593 if (TREE_CODE (type) == TYPEOF_TYPE
26594 || TREE_CODE (type) == DECLTYPE_TYPE
26595 || TREE_CODE (type) == UNDERLYING_TYPE)
26596 return true;
26597
26598 /* A template argument pack is dependent if any of its packed
26599 arguments are. */
26600 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26601 {
26602 tree args = ARGUMENT_PACK_ARGS (type);
26603 int i, len = TREE_VEC_LENGTH (args);
26604 for (i = 0; i < len; ++i)
26605 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26606 return true;
26607 }
26608
26609 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26610 be template parameters. */
26611 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26612 return true;
26613
26614 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26615 return true;
26616
26617 /* The standard does not specifically mention types that are local
26618 to template functions or local classes, but they should be
26619 considered dependent too. For example:
26620
26621 template <int I> void f() {
26622 enum E { a = I };
26623 S<sizeof (E)> s;
26624 }
26625
26626 The size of `E' cannot be known until the value of `I' has been
26627 determined. Therefore, `E' must be considered dependent. */
26628 scope = TYPE_CONTEXT (type);
26629 if (scope && TYPE_P (scope))
26630 return dependent_type_p (scope);
26631 /* Don't use type_dependent_expression_p here, as it can lead
26632 to infinite recursion trying to determine whether a lambda
26633 nested in a lambda is dependent (c++/47687). */
26634 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26635 && DECL_LANG_SPECIFIC (scope)
26636 && DECL_TEMPLATE_INFO (scope)
26637 && (any_dependent_template_arguments_p
26638 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26639 return true;
26640
26641 /* Other types are non-dependent. */
26642 return false;
26643 }
26644
26645 /* Returns TRUE if TYPE is dependent, in the sense of
26646 [temp.dep.type]. Note that a NULL type is considered dependent. */
26647
26648 bool
26649 dependent_type_p (tree type)
26650 {
26651 /* If there are no template parameters in scope, then there can't be
26652 any dependent types. */
26653 if (!processing_template_decl)
26654 {
26655 /* If we are not processing a template, then nobody should be
26656 providing us with a dependent type. */
26657 gcc_assert (type);
26658 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26659 return false;
26660 }
26661
26662 /* If the type is NULL, we have not computed a type for the entity
26663 in question; in that case, the type is dependent. */
26664 if (!type)
26665 return true;
26666
26667 /* Erroneous types can be considered non-dependent. */
26668 if (type == error_mark_node)
26669 return false;
26670
26671 /* Getting here with global_type_node means we improperly called this
26672 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26673 gcc_checking_assert (type != global_type_node);
26674
26675 /* If we have not already computed the appropriate value for TYPE,
26676 do so now. */
26677 if (!TYPE_DEPENDENT_P_VALID (type))
26678 {
26679 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26680 TYPE_DEPENDENT_P_VALID (type) = 1;
26681 }
26682
26683 return TYPE_DEPENDENT_P (type);
26684 }
26685
26686 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26687 lookup. In other words, a dependent type that is not the current
26688 instantiation. */
26689
26690 bool
26691 dependent_scope_p (tree scope)
26692 {
26693 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26694 && !currently_open_class (scope));
26695 }
26696
26697 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26698 an unknown base of 'this' (and is therefore instantiation-dependent). */
26699
26700 static bool
26701 unknown_base_ref_p (tree t)
26702 {
26703 if (!current_class_ptr)
26704 return false;
26705
26706 tree mem = TREE_OPERAND (t, 1);
26707 if (shared_member_p (mem))
26708 return false;
26709
26710 tree cur = current_nonlambda_class_type ();
26711 if (!any_dependent_bases_p (cur))
26712 return false;
26713
26714 tree ctx = TREE_OPERAND (t, 0);
26715 if (DERIVED_FROM_P (ctx, cur))
26716 return false;
26717
26718 return true;
26719 }
26720
26721 /* T is a SCOPE_REF; return whether we need to consider it
26722 instantiation-dependent so that we can check access at instantiation
26723 time even though we know which member it resolves to. */
26724
26725 static bool
26726 instantiation_dependent_scope_ref_p (tree t)
26727 {
26728 if (DECL_P (TREE_OPERAND (t, 1))
26729 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26730 && !unknown_base_ref_p (t)
26731 && accessible_in_template_p (TREE_OPERAND (t, 0),
26732 TREE_OPERAND (t, 1)))
26733 return false;
26734 else
26735 return true;
26736 }
26737
26738 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26739 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26740 expression. */
26741
26742 /* Note that this predicate is not appropriate for general expressions;
26743 only constant expressions (that satisfy potential_constant_expression)
26744 can be tested for value dependence. */
26745
26746 bool
26747 value_dependent_expression_p (tree expression)
26748 {
26749 if (!processing_template_decl || expression == NULL_TREE)
26750 return false;
26751
26752 /* A type-dependent expression is also value-dependent. */
26753 if (type_dependent_expression_p (expression))
26754 return true;
26755
26756 switch (TREE_CODE (expression))
26757 {
26758 case BASELINK:
26759 /* A dependent member function of the current instantiation. */
26760 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26761
26762 case FUNCTION_DECL:
26763 /* A dependent member function of the current instantiation. */
26764 if (DECL_CLASS_SCOPE_P (expression)
26765 && dependent_type_p (DECL_CONTEXT (expression)))
26766 return true;
26767 break;
26768
26769 case IDENTIFIER_NODE:
26770 /* A name that has not been looked up -- must be dependent. */
26771 return true;
26772
26773 case TEMPLATE_PARM_INDEX:
26774 /* A non-type template parm. */
26775 return true;
26776
26777 case CONST_DECL:
26778 /* A non-type template parm. */
26779 if (DECL_TEMPLATE_PARM_P (expression))
26780 return true;
26781 return value_dependent_expression_p (DECL_INITIAL (expression));
26782
26783 case VAR_DECL:
26784 /* A constant with literal type and is initialized
26785 with an expression that is value-dependent. */
26786 if (DECL_DEPENDENT_INIT_P (expression)
26787 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26788 || TYPE_REF_P (TREE_TYPE (expression)))
26789 return true;
26790 if (DECL_HAS_VALUE_EXPR_P (expression))
26791 {
26792 tree value_expr = DECL_VALUE_EXPR (expression);
26793 if (value_dependent_expression_p (value_expr)
26794 /* __PRETTY_FUNCTION__ inside a template function is dependent
26795 on the name of the function. */
26796 || (DECL_PRETTY_FUNCTION_P (expression)
26797 /* It might be used in a template, but not a template
26798 function, in which case its DECL_VALUE_EXPR will be
26799 "top level". */
26800 && value_expr == error_mark_node))
26801 return true;
26802 }
26803 return false;
26804
26805 case DYNAMIC_CAST_EXPR:
26806 case STATIC_CAST_EXPR:
26807 case CONST_CAST_EXPR:
26808 case REINTERPRET_CAST_EXPR:
26809 case CAST_EXPR:
26810 case IMPLICIT_CONV_EXPR:
26811 /* These expressions are value-dependent if the type to which
26812 the cast occurs is dependent or the expression being casted
26813 is value-dependent. */
26814 {
26815 tree type = TREE_TYPE (expression);
26816
26817 if (dependent_type_p (type))
26818 return true;
26819
26820 /* A functional cast has a list of operands. */
26821 expression = TREE_OPERAND (expression, 0);
26822 if (!expression)
26823 {
26824 /* If there are no operands, it must be an expression such
26825 as "int()". This should not happen for aggregate types
26826 because it would form non-constant expressions. */
26827 gcc_assert (cxx_dialect >= cxx11
26828 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26829
26830 return false;
26831 }
26832
26833 if (TREE_CODE (expression) == TREE_LIST)
26834 return any_value_dependent_elements_p (expression);
26835
26836 return value_dependent_expression_p (expression);
26837 }
26838
26839 case SIZEOF_EXPR:
26840 if (SIZEOF_EXPR_TYPE_P (expression))
26841 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26842 /* FALLTHRU */
26843 case ALIGNOF_EXPR:
26844 case TYPEID_EXPR:
26845 /* A `sizeof' expression is value-dependent if the operand is
26846 type-dependent or is a pack expansion. */
26847 expression = TREE_OPERAND (expression, 0);
26848 if (PACK_EXPANSION_P (expression))
26849 return true;
26850 else if (TYPE_P (expression))
26851 return dependent_type_p (expression);
26852 return instantiation_dependent_uneval_expression_p (expression);
26853
26854 case AT_ENCODE_EXPR:
26855 /* An 'encode' expression is value-dependent if the operand is
26856 type-dependent. */
26857 expression = TREE_OPERAND (expression, 0);
26858 return dependent_type_p (expression);
26859
26860 case NOEXCEPT_EXPR:
26861 expression = TREE_OPERAND (expression, 0);
26862 return instantiation_dependent_uneval_expression_p (expression);
26863
26864 case SCOPE_REF:
26865 /* All instantiation-dependent expressions should also be considered
26866 value-dependent. */
26867 return instantiation_dependent_scope_ref_p (expression);
26868
26869 case COMPONENT_REF:
26870 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26871 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26872
26873 case NONTYPE_ARGUMENT_PACK:
26874 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26875 is value-dependent. */
26876 {
26877 tree values = ARGUMENT_PACK_ARGS (expression);
26878 int i, len = TREE_VEC_LENGTH (values);
26879
26880 for (i = 0; i < len; ++i)
26881 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26882 return true;
26883
26884 return false;
26885 }
26886
26887 case TRAIT_EXPR:
26888 {
26889 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26890
26891 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26892 return true;
26893
26894 if (!type2)
26895 return false;
26896
26897 if (TREE_CODE (type2) != TREE_LIST)
26898 return dependent_type_p (type2);
26899
26900 for (; type2; type2 = TREE_CHAIN (type2))
26901 if (dependent_type_p (TREE_VALUE (type2)))
26902 return true;
26903
26904 return false;
26905 }
26906
26907 case MODOP_EXPR:
26908 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26909 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26910
26911 case ARRAY_REF:
26912 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26913 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26914
26915 case ADDR_EXPR:
26916 {
26917 tree op = TREE_OPERAND (expression, 0);
26918 return (value_dependent_expression_p (op)
26919 || has_value_dependent_address (op));
26920 }
26921
26922 case REQUIRES_EXPR:
26923 /* Treat all requires-expressions as value-dependent so
26924 we don't try to fold them. */
26925 return true;
26926
26927 case TYPE_REQ:
26928 return dependent_type_p (TREE_OPERAND (expression, 0));
26929
26930 case CALL_EXPR:
26931 {
26932 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26933 return true;
26934 tree fn = get_callee_fndecl (expression);
26935 int i, nargs;
26936 nargs = call_expr_nargs (expression);
26937 for (i = 0; i < nargs; ++i)
26938 {
26939 tree op = CALL_EXPR_ARG (expression, i);
26940 /* In a call to a constexpr member function, look through the
26941 implicit ADDR_EXPR on the object argument so that it doesn't
26942 cause the call to be considered value-dependent. We also
26943 look through it in potential_constant_expression. */
26944 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26945 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26946 && TREE_CODE (op) == ADDR_EXPR)
26947 op = TREE_OPERAND (op, 0);
26948 if (value_dependent_expression_p (op))
26949 return true;
26950 }
26951 return false;
26952 }
26953
26954 case TEMPLATE_ID_EXPR:
26955 return concept_definition_p (TREE_OPERAND (expression, 0));
26956
26957 case CONSTRUCTOR:
26958 {
26959 unsigned ix;
26960 tree val;
26961 if (dependent_type_p (TREE_TYPE (expression)))
26962 return true;
26963 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26964 if (value_dependent_expression_p (val))
26965 return true;
26966 return false;
26967 }
26968
26969 case STMT_EXPR:
26970 /* Treat a GNU statement expression as dependent to avoid crashing
26971 under instantiate_non_dependent_expr; it can't be constant. */
26972 return true;
26973
26974 default:
26975 /* A constant expression is value-dependent if any subexpression is
26976 value-dependent. */
26977 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26978 {
26979 case tcc_reference:
26980 case tcc_unary:
26981 case tcc_comparison:
26982 case tcc_binary:
26983 case tcc_expression:
26984 case tcc_vl_exp:
26985 {
26986 int i, len = cp_tree_operand_length (expression);
26987
26988 for (i = 0; i < len; i++)
26989 {
26990 tree t = TREE_OPERAND (expression, i);
26991
26992 /* In some cases, some of the operands may be missing.
26993 (For example, in the case of PREDECREMENT_EXPR, the
26994 amount to increment by may be missing.) That doesn't
26995 make the expression dependent. */
26996 if (t && value_dependent_expression_p (t))
26997 return true;
26998 }
26999 }
27000 break;
27001 default:
27002 break;
27003 }
27004 break;
27005 }
27006
27007 /* The expression is not value-dependent. */
27008 return false;
27009 }
27010
27011 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27012 [temp.dep.expr]. Note that an expression with no type is
27013 considered dependent. Other parts of the compiler arrange for an
27014 expression with type-dependent subexpressions to have no type, so
27015 this function doesn't have to be fully recursive. */
27016
27017 bool
27018 type_dependent_expression_p (tree expression)
27019 {
27020 if (!processing_template_decl)
27021 return false;
27022
27023 if (expression == NULL_TREE || expression == error_mark_node)
27024 return false;
27025
27026 STRIP_ANY_LOCATION_WRAPPER (expression);
27027
27028 /* An unresolved name is always dependent. */
27029 if (identifier_p (expression)
27030 || TREE_CODE (expression) == USING_DECL
27031 || TREE_CODE (expression) == WILDCARD_DECL)
27032 return true;
27033
27034 /* A lambda-expression in template context is dependent. dependent_type_p is
27035 true for a lambda in the scope of a class or function template, but that
27036 doesn't cover all template contexts, like a default template argument. */
27037 if (TREE_CODE (expression) == LAMBDA_EXPR)
27038 return true;
27039
27040 /* A fold expression is type-dependent. */
27041 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27042 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27043 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27044 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27045 return true;
27046
27047 /* Some expression forms are never type-dependent. */
27048 if (TREE_CODE (expression) == SIZEOF_EXPR
27049 || TREE_CODE (expression) == ALIGNOF_EXPR
27050 || TREE_CODE (expression) == AT_ENCODE_EXPR
27051 || TREE_CODE (expression) == NOEXCEPT_EXPR
27052 || TREE_CODE (expression) == TRAIT_EXPR
27053 || TREE_CODE (expression) == TYPEID_EXPR
27054 || TREE_CODE (expression) == DELETE_EXPR
27055 || TREE_CODE (expression) == VEC_DELETE_EXPR
27056 || TREE_CODE (expression) == THROW_EXPR
27057 || TREE_CODE (expression) == REQUIRES_EXPR)
27058 return false;
27059
27060 /* The types of these expressions depends only on the type to which
27061 the cast occurs. */
27062 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27063 || TREE_CODE (expression) == STATIC_CAST_EXPR
27064 || TREE_CODE (expression) == CONST_CAST_EXPR
27065 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27066 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27067 || TREE_CODE (expression) == CAST_EXPR)
27068 return dependent_type_p (TREE_TYPE (expression));
27069
27070 /* The types of these expressions depends only on the type created
27071 by the expression. */
27072 if (TREE_CODE (expression) == NEW_EXPR
27073 || TREE_CODE (expression) == VEC_NEW_EXPR)
27074 {
27075 /* For NEW_EXPR tree nodes created inside a template, either
27076 the object type itself or a TREE_LIST may appear as the
27077 operand 1. */
27078 tree type = TREE_OPERAND (expression, 1);
27079 if (TREE_CODE (type) == TREE_LIST)
27080 /* This is an array type. We need to check array dimensions
27081 as well. */
27082 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27083 || value_dependent_expression_p
27084 (TREE_OPERAND (TREE_VALUE (type), 1));
27085 /* Array type whose dimension has to be deduced. */
27086 else if (TREE_CODE (type) == ARRAY_TYPE
27087 && TREE_OPERAND (expression, 2) == NULL_TREE)
27088 return true;
27089 else
27090 return dependent_type_p (type);
27091 }
27092
27093 if (TREE_CODE (expression) == SCOPE_REF)
27094 {
27095 tree scope = TREE_OPERAND (expression, 0);
27096 tree name = TREE_OPERAND (expression, 1);
27097
27098 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27099 contains an identifier associated by name lookup with one or more
27100 declarations declared with a dependent type, or...a
27101 nested-name-specifier or qualified-id that names a member of an
27102 unknown specialization. */
27103 return (type_dependent_expression_p (name)
27104 || dependent_scope_p (scope));
27105 }
27106
27107 if (TREE_CODE (expression) == TEMPLATE_DECL
27108 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27109 return uses_outer_template_parms (expression);
27110
27111 if (TREE_CODE (expression) == STMT_EXPR)
27112 expression = stmt_expr_value_expr (expression);
27113
27114 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27115 {
27116 tree elt;
27117 unsigned i;
27118
27119 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27120 {
27121 if (type_dependent_expression_p (elt))
27122 return true;
27123 }
27124 return false;
27125 }
27126
27127 /* A static data member of the current instantiation with incomplete
27128 array type is type-dependent, as the definition and specializations
27129 can have different bounds. */
27130 if (VAR_P (expression)
27131 && DECL_CLASS_SCOPE_P (expression)
27132 && dependent_type_p (DECL_CONTEXT (expression))
27133 && VAR_HAD_UNKNOWN_BOUND (expression))
27134 return true;
27135
27136 /* An array of unknown bound depending on a variadic parameter, eg:
27137
27138 template<typename... Args>
27139 void foo (Args... args)
27140 {
27141 int arr[] = { args... };
27142 }
27143
27144 template<int... vals>
27145 void bar ()
27146 {
27147 int arr[] = { vals... };
27148 }
27149
27150 If the array has no length and has an initializer, it must be that
27151 we couldn't determine its length in cp_complete_array_type because
27152 it is dependent. */
27153 if (VAR_P (expression)
27154 && TREE_TYPE (expression) != NULL_TREE
27155 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27156 && !TYPE_DOMAIN (TREE_TYPE (expression))
27157 && DECL_INITIAL (expression))
27158 return true;
27159
27160 /* A function or variable template-id is type-dependent if it has any
27161 dependent template arguments. */
27162 if (VAR_OR_FUNCTION_DECL_P (expression)
27163 && DECL_LANG_SPECIFIC (expression)
27164 && DECL_TEMPLATE_INFO (expression))
27165 {
27166 /* Consider the innermost template arguments, since those are the ones
27167 that come from the template-id; the template arguments for the
27168 enclosing class do not make it type-dependent unless they are used in
27169 the type of the decl. */
27170 if (instantiates_primary_template_p (expression)
27171 && (any_dependent_template_arguments_p
27172 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27173 return true;
27174 }
27175
27176 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27177 type-dependent. Checking this is important for functions with auto return
27178 type, which looks like a dependent type. */
27179 if (TREE_CODE (expression) == FUNCTION_DECL
27180 && !(DECL_CLASS_SCOPE_P (expression)
27181 && dependent_type_p (DECL_CONTEXT (expression)))
27182 && !(DECL_LANG_SPECIFIC (expression)
27183 && DECL_UNIQUE_FRIEND_P (expression)
27184 && (!DECL_FRIEND_CONTEXT (expression)
27185 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27186 && !DECL_LOCAL_DECL_P (expression))
27187 {
27188 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27189 || undeduced_auto_decl (expression));
27190 return false;
27191 }
27192
27193 /* Always dependent, on the number of arguments if nothing else. */
27194 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27195 return true;
27196
27197 if (TREE_TYPE (expression) == unknown_type_node)
27198 {
27199 if (TREE_CODE (expression) == ADDR_EXPR)
27200 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27201 if (TREE_CODE (expression) == COMPONENT_REF
27202 || TREE_CODE (expression) == OFFSET_REF)
27203 {
27204 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27205 return true;
27206 expression = TREE_OPERAND (expression, 1);
27207 if (identifier_p (expression))
27208 return false;
27209 }
27210 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27211 if (TREE_CODE (expression) == SCOPE_REF)
27212 return false;
27213
27214 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27215 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27216 || TREE_CODE (expression) == CO_YIELD_EXPR)
27217 return true;
27218
27219 if (BASELINK_P (expression))
27220 {
27221 if (BASELINK_OPTYPE (expression)
27222 && dependent_type_p (BASELINK_OPTYPE (expression)))
27223 return true;
27224 expression = BASELINK_FUNCTIONS (expression);
27225 }
27226
27227 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27228 {
27229 if (any_dependent_template_arguments_p
27230 (TREE_OPERAND (expression, 1)))
27231 return true;
27232 expression = TREE_OPERAND (expression, 0);
27233 if (identifier_p (expression))
27234 return true;
27235 }
27236
27237 gcc_assert (OVL_P (expression));
27238
27239 for (lkp_iterator iter (expression); iter; ++iter)
27240 if (type_dependent_expression_p (*iter))
27241 return true;
27242
27243 return false;
27244 }
27245
27246 /* The type of a non-type template parm declared with a placeholder type
27247 depends on the corresponding template argument, even though
27248 placeholders are not normally considered dependent. */
27249 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27250 && is_auto (TREE_TYPE (expression)))
27251 return true;
27252
27253 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27254
27255 /* Dependent type attributes might not have made it from the decl to
27256 the type yet. */
27257 if (DECL_P (expression)
27258 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27259 return true;
27260
27261 return (dependent_type_p (TREE_TYPE (expression)));
27262 }
27263
27264 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27265 type-dependent if the expression refers to a member of the current
27266 instantiation and the type of the referenced member is dependent, or the
27267 class member access expression refers to a member of an unknown
27268 specialization.
27269
27270 This function returns true if the OBJECT in such a class member access
27271 expression is of an unknown specialization. */
27272
27273 bool
27274 type_dependent_object_expression_p (tree object)
27275 {
27276 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27277 dependent. */
27278 if (TREE_CODE (object) == IDENTIFIER_NODE)
27279 return true;
27280 tree scope = TREE_TYPE (object);
27281 return (!scope || dependent_scope_p (scope));
27282 }
27283
27284 /* walk_tree callback function for instantiation_dependent_expression_p,
27285 below. Returns non-zero if a dependent subexpression is found. */
27286
27287 static tree
27288 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27289 void * /*data*/)
27290 {
27291 if (TYPE_P (*tp))
27292 {
27293 /* We don't have to worry about decltype currently because decltype
27294 of an instantiation-dependent expr is a dependent type. This
27295 might change depending on the resolution of DR 1172. */
27296 *walk_subtrees = false;
27297 return NULL_TREE;
27298 }
27299 enum tree_code code = TREE_CODE (*tp);
27300 switch (code)
27301 {
27302 /* Don't treat an argument list as dependent just because it has no
27303 TREE_TYPE. */
27304 case TREE_LIST:
27305 case TREE_VEC:
27306 case NONTYPE_ARGUMENT_PACK:
27307 return NULL_TREE;
27308
27309 case TEMPLATE_PARM_INDEX:
27310 if (dependent_type_p (TREE_TYPE (*tp)))
27311 return *tp;
27312 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27313 return *tp;
27314 /* We'll check value-dependence separately. */
27315 return NULL_TREE;
27316
27317 /* Handle expressions with type operands. */
27318 case SIZEOF_EXPR:
27319 case ALIGNOF_EXPR:
27320 case TYPEID_EXPR:
27321 case AT_ENCODE_EXPR:
27322 {
27323 tree op = TREE_OPERAND (*tp, 0);
27324 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27325 op = TREE_TYPE (op);
27326 if (TYPE_P (op))
27327 {
27328 if (dependent_type_p (op))
27329 return *tp;
27330 else
27331 {
27332 *walk_subtrees = false;
27333 return NULL_TREE;
27334 }
27335 }
27336 break;
27337 }
27338
27339 case COMPONENT_REF:
27340 if (identifier_p (TREE_OPERAND (*tp, 1)))
27341 /* In a template, finish_class_member_access_expr creates a
27342 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27343 type-dependent, so that we can check access control at
27344 instantiation time (PR 42277). See also Core issue 1273. */
27345 return *tp;
27346 break;
27347
27348 case SCOPE_REF:
27349 if (instantiation_dependent_scope_ref_p (*tp))
27350 return *tp;
27351 else
27352 break;
27353
27354 /* Treat statement-expressions as dependent. */
27355 case BIND_EXPR:
27356 return *tp;
27357
27358 /* Treat requires-expressions as dependent. */
27359 case REQUIRES_EXPR:
27360 return *tp;
27361
27362 case CALL_EXPR:
27363 /* Treat concept checks as dependent. */
27364 if (concept_check_p (*tp))
27365 return *tp;
27366 break;
27367
27368 case TEMPLATE_ID_EXPR:
27369 /* Treat concept checks as dependent. */
27370 if (concept_check_p (*tp))
27371 return *tp;
27372 break;
27373
27374 case CONSTRUCTOR:
27375 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27376 return *tp;
27377 break;
27378
27379 default:
27380 break;
27381 }
27382
27383 if (type_dependent_expression_p (*tp))
27384 return *tp;
27385 else
27386 return NULL_TREE;
27387 }
27388
27389 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27390 sense defined by the ABI:
27391
27392 "An expression is instantiation-dependent if it is type-dependent
27393 or value-dependent, or it has a subexpression that is type-dependent
27394 or value-dependent."
27395
27396 Except don't actually check value-dependence for unevaluated expressions,
27397 because in sizeof(i) we don't care about the value of i. Checking
27398 type-dependence will in turn check value-dependence of array bounds/template
27399 arguments as needed. */
27400
27401 bool
27402 instantiation_dependent_uneval_expression_p (tree expression)
27403 {
27404 tree result;
27405
27406 if (!processing_template_decl)
27407 return false;
27408
27409 if (expression == error_mark_node)
27410 return false;
27411
27412 result = cp_walk_tree_without_duplicates (&expression,
27413 instantiation_dependent_r, NULL);
27414 return result != NULL_TREE;
27415 }
27416
27417 /* As above, but also check value-dependence of the expression as a whole. */
27418
27419 bool
27420 instantiation_dependent_expression_p (tree expression)
27421 {
27422 return (instantiation_dependent_uneval_expression_p (expression)
27423 || (potential_constant_expression (expression)
27424 && value_dependent_expression_p (expression)));
27425 }
27426
27427 /* Like type_dependent_expression_p, but it also works while not processing
27428 a template definition, i.e. during substitution or mangling. */
27429
27430 bool
27431 type_dependent_expression_p_push (tree expr)
27432 {
27433 bool b;
27434 ++processing_template_decl;
27435 b = type_dependent_expression_p (expr);
27436 --processing_template_decl;
27437 return b;
27438 }
27439
27440 /* Returns TRUE if ARGS contains a type-dependent expression. */
27441
27442 bool
27443 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27444 {
27445 unsigned int i;
27446 tree arg;
27447
27448 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27449 {
27450 if (type_dependent_expression_p (arg))
27451 return true;
27452 }
27453 return false;
27454 }
27455
27456 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27457 expressions) contains any type-dependent expressions. */
27458
27459 bool
27460 any_type_dependent_elements_p (const_tree list)
27461 {
27462 for (; list; list = TREE_CHAIN (list))
27463 if (type_dependent_expression_p (TREE_VALUE (list)))
27464 return true;
27465
27466 return false;
27467 }
27468
27469 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27470 expressions) contains any value-dependent expressions. */
27471
27472 bool
27473 any_value_dependent_elements_p (const_tree list)
27474 {
27475 for (; list; list = TREE_CHAIN (list))
27476 if (value_dependent_expression_p (TREE_VALUE (list)))
27477 return true;
27478
27479 return false;
27480 }
27481
27482 /* Returns TRUE if the ARG (a template argument) is dependent. */
27483
27484 bool
27485 dependent_template_arg_p (tree arg)
27486 {
27487 if (!processing_template_decl)
27488 return false;
27489
27490 /* Assume a template argument that was wrongly written by the user
27491 is dependent. This is consistent with what
27492 any_dependent_template_arguments_p [that calls this function]
27493 does. */
27494 if (!arg || arg == error_mark_node)
27495 return true;
27496
27497 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27498 arg = argument_pack_select_arg (arg);
27499
27500 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27501 return true;
27502 if (TREE_CODE (arg) == TEMPLATE_DECL)
27503 {
27504 if (DECL_TEMPLATE_PARM_P (arg))
27505 return true;
27506 /* A member template of a dependent class is not necessarily
27507 type-dependent, but it is a dependent template argument because it
27508 will be a member of an unknown specialization to that template. */
27509 tree scope = CP_DECL_CONTEXT (arg);
27510 return TYPE_P (scope) && dependent_type_p (scope);
27511 }
27512 else if (ARGUMENT_PACK_P (arg))
27513 {
27514 tree args = ARGUMENT_PACK_ARGS (arg);
27515 int i, len = TREE_VEC_LENGTH (args);
27516 for (i = 0; i < len; ++i)
27517 {
27518 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27519 return true;
27520 }
27521
27522 return false;
27523 }
27524 else if (TYPE_P (arg))
27525 return dependent_type_p (arg);
27526 else
27527 return value_dependent_expression_p (arg);
27528 }
27529
27530 /* Returns true if ARGS (a collection of template arguments) contains
27531 any types that require structural equality testing. */
27532
27533 bool
27534 any_template_arguments_need_structural_equality_p (tree args)
27535 {
27536 int i;
27537 int j;
27538
27539 if (!args)
27540 return false;
27541 if (args == error_mark_node)
27542 return true;
27543
27544 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27545 {
27546 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27547 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27548 {
27549 tree arg = TREE_VEC_ELT (level, j);
27550 tree packed_args = NULL_TREE;
27551 int k, len = 1;
27552
27553 if (ARGUMENT_PACK_P (arg))
27554 {
27555 /* Look inside the argument pack. */
27556 packed_args = ARGUMENT_PACK_ARGS (arg);
27557 len = TREE_VEC_LENGTH (packed_args);
27558 }
27559
27560 for (k = 0; k < len; ++k)
27561 {
27562 if (packed_args)
27563 arg = TREE_VEC_ELT (packed_args, k);
27564
27565 if (error_operand_p (arg))
27566 return true;
27567 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27568 continue;
27569 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27570 return true;
27571 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27572 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27573 return true;
27574 }
27575 }
27576 }
27577
27578 return false;
27579 }
27580
27581 /* Returns true if ARGS (a collection of template arguments) contains
27582 any dependent arguments. */
27583
27584 bool
27585 any_dependent_template_arguments_p (const_tree args)
27586 {
27587 int i;
27588 int j;
27589
27590 if (!args)
27591 return false;
27592 if (args == error_mark_node)
27593 return true;
27594
27595 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27596 {
27597 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27598 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27599 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27600 return true;
27601 }
27602
27603 return false;
27604 }
27605
27606 /* Returns true if ARGS contains any errors. */
27607
27608 bool
27609 any_erroneous_template_args_p (const_tree args)
27610 {
27611 int i;
27612 int j;
27613
27614 if (args == error_mark_node)
27615 return true;
27616
27617 if (args && TREE_CODE (args) != TREE_VEC)
27618 {
27619 if (tree ti = get_template_info (args))
27620 args = TI_ARGS (ti);
27621 else
27622 args = NULL_TREE;
27623 }
27624
27625 if (!args)
27626 return false;
27627
27628 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27629 {
27630 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27631 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27632 if (error_operand_p (TREE_VEC_ELT (level, j)))
27633 return true;
27634 }
27635
27636 return false;
27637 }
27638
27639 /* Returns TRUE if the template TMPL is type-dependent. */
27640
27641 bool
27642 dependent_template_p (tree tmpl)
27643 {
27644 if (TREE_CODE (tmpl) == OVERLOAD)
27645 {
27646 for (lkp_iterator iter (tmpl); iter; ++iter)
27647 if (dependent_template_p (*iter))
27648 return true;
27649 return false;
27650 }
27651
27652 /* Template template parameters are dependent. */
27653 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27654 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27655 return true;
27656 /* So are names that have not been looked up. */
27657 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27658 return true;
27659 return false;
27660 }
27661
27662 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27663
27664 bool
27665 dependent_template_id_p (tree tmpl, tree args)
27666 {
27667 return (dependent_template_p (tmpl)
27668 || any_dependent_template_arguments_p (args));
27669 }
27670
27671 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27672 are dependent. */
27673
27674 bool
27675 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27676 {
27677 int i;
27678
27679 if (!processing_template_decl)
27680 return false;
27681
27682 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27683 {
27684 tree decl = TREE_VEC_ELT (declv, i);
27685 tree init = TREE_VEC_ELT (initv, i);
27686 tree cond = TREE_VEC_ELT (condv, i);
27687 tree incr = TREE_VEC_ELT (incrv, i);
27688
27689 if (type_dependent_expression_p (decl)
27690 || TREE_CODE (decl) == SCOPE_REF)
27691 return true;
27692
27693 if (init && type_dependent_expression_p (init))
27694 return true;
27695
27696 if (cond == global_namespace)
27697 return true;
27698
27699 if (type_dependent_expression_p (cond))
27700 return true;
27701
27702 if (COMPARISON_CLASS_P (cond)
27703 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27704 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27705 return true;
27706
27707 if (TREE_CODE (incr) == MODOP_EXPR)
27708 {
27709 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27710 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27711 return true;
27712 }
27713 else if (type_dependent_expression_p (incr))
27714 return true;
27715 else if (TREE_CODE (incr) == MODIFY_EXPR)
27716 {
27717 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27718 return true;
27719 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27720 {
27721 tree t = TREE_OPERAND (incr, 1);
27722 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27723 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27724 return true;
27725
27726 /* If this loop has a class iterator with != comparison
27727 with increment other than i++/++i/i--/--i, make sure the
27728 increment is constant. */
27729 if (CLASS_TYPE_P (TREE_TYPE (decl))
27730 && TREE_CODE (cond) == NE_EXPR)
27731 {
27732 if (TREE_OPERAND (t, 0) == decl)
27733 t = TREE_OPERAND (t, 1);
27734 else
27735 t = TREE_OPERAND (t, 0);
27736 if (TREE_CODE (t) != INTEGER_CST)
27737 return true;
27738 }
27739 }
27740 }
27741 }
27742
27743 return false;
27744 }
27745
27746 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27747 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27748 no such TYPE can be found. Note that this function peers inside
27749 uninstantiated templates and therefore should be used only in
27750 extremely limited situations. ONLY_CURRENT_P restricts this
27751 peering to the currently open classes hierarchy (which is required
27752 when comparing types). */
27753
27754 tree
27755 resolve_typename_type (tree type, bool only_current_p)
27756 {
27757 tree scope;
27758 tree name;
27759 tree decl;
27760 int quals;
27761 tree pushed_scope;
27762 tree result;
27763
27764 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27765
27766 scope = TYPE_CONTEXT (type);
27767 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27768 gcc_checking_assert (uses_template_parms (scope));
27769
27770 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27771 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27772 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27773 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27774 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27775 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27776 the TYPENAME_TYPE instead, we avoid messing up with a possible
27777 typedef variant case. */
27778 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27779
27780 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27781 it first before we can figure out what NAME refers to. */
27782 if (TREE_CODE (scope) == TYPENAME_TYPE)
27783 {
27784 if (TYPENAME_IS_RESOLVING_P (scope))
27785 /* Given a class template A with a dependent base with nested type C,
27786 typedef typename A::C::C C will land us here, as trying to resolve
27787 the initial A::C leads to the local C typedef, which leads back to
27788 A::C::C. So we break the recursion now. */
27789 return type;
27790 else
27791 scope = resolve_typename_type (scope, only_current_p);
27792 }
27793 /* If we don't know what SCOPE refers to, then we cannot resolve the
27794 TYPENAME_TYPE. */
27795 if (!CLASS_TYPE_P (scope))
27796 return type;
27797 /* If this is a typedef, we don't want to look inside (c++/11987). */
27798 if (typedef_variant_p (type))
27799 return type;
27800 /* If SCOPE isn't the template itself, it will not have a valid
27801 TYPE_FIELDS list. */
27802 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27803 /* scope is either the template itself or a compatible instantiation
27804 like X<T>, so look up the name in the original template. */
27805 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27806 /* If scope has no fields, it can't be a current instantiation. Check this
27807 before currently_open_class to avoid infinite recursion (71515). */
27808 if (!TYPE_FIELDS (scope))
27809 return type;
27810 /* If the SCOPE is not the current instantiation, there's no reason
27811 to look inside it. */
27812 if (only_current_p && !currently_open_class (scope))
27813 return type;
27814 /* Enter the SCOPE so that name lookup will be resolved as if we
27815 were in the class definition. In particular, SCOPE will no
27816 longer be considered a dependent type. */
27817 pushed_scope = push_scope (scope);
27818 /* Look up the declaration. */
27819 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27820 tf_warning_or_error);
27821
27822 result = NULL_TREE;
27823
27824 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27825 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27826 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27827 if (!decl)
27828 /*nop*/;
27829 else if (identifier_p (fullname)
27830 && TREE_CODE (decl) == TYPE_DECL)
27831 {
27832 result = TREE_TYPE (decl);
27833 if (result == error_mark_node)
27834 result = NULL_TREE;
27835 }
27836 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27837 && DECL_CLASS_TEMPLATE_P (decl))
27838 {
27839 /* Obtain the template and the arguments. */
27840 tree tmpl = TREE_OPERAND (fullname, 0);
27841 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27842 {
27843 /* We get here with a plain identifier because a previous tentative
27844 parse of the nested-name-specifier as part of a ptr-operator saw
27845 ::template X<A>. The use of ::template is necessary in a
27846 ptr-operator, but wrong in a declarator-id.
27847
27848 [temp.names]: In a qualified-id of a declarator-id, the keyword
27849 template shall not appear at the top level. */
27850 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27851 "keyword %<template%> not allowed in declarator-id");
27852 tmpl = decl;
27853 }
27854 tree args = TREE_OPERAND (fullname, 1);
27855 /* Instantiate the template. */
27856 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27857 /*entering_scope=*/true,
27858 tf_error | tf_user);
27859 if (result == error_mark_node)
27860 result = NULL_TREE;
27861 }
27862
27863 /* Leave the SCOPE. */
27864 if (pushed_scope)
27865 pop_scope (pushed_scope);
27866
27867 /* If we failed to resolve it, return the original typename. */
27868 if (!result)
27869 return type;
27870
27871 /* If lookup found a typename type, resolve that too. */
27872 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27873 {
27874 /* Ill-formed programs can cause infinite recursion here, so we
27875 must catch that. */
27876 TYPENAME_IS_RESOLVING_P (result) = 1;
27877 result = resolve_typename_type (result, only_current_p);
27878 TYPENAME_IS_RESOLVING_P (result) = 0;
27879 }
27880
27881 /* Qualify the resulting type. */
27882 quals = cp_type_quals (type);
27883 if (quals)
27884 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27885
27886 return result;
27887 }
27888
27889 /* EXPR is an expression which is not type-dependent. Return a proxy
27890 for EXPR that can be used to compute the types of larger
27891 expressions containing EXPR. */
27892
27893 tree
27894 build_non_dependent_expr (tree expr)
27895 {
27896 tree orig_expr = expr;
27897 tree inner_expr;
27898
27899 /* When checking, try to get a constant value for all non-dependent
27900 expressions in order to expose bugs in *_dependent_expression_p
27901 and constexpr. This can affect code generation, see PR70704, so
27902 only do this for -fchecking=2. */
27903 if (flag_checking > 1
27904 && cxx_dialect >= cxx11
27905 /* Don't do this during nsdmi parsing as it can lead to
27906 unexpected recursive instantiations. */
27907 && !parsing_nsdmi ()
27908 /* Don't do this during concept processing either and for
27909 the same reason. */
27910 && !processing_constraint_expression_p ())
27911 fold_non_dependent_expr (expr, tf_none);
27912
27913 STRIP_ANY_LOCATION_WRAPPER (expr);
27914
27915 /* Preserve OVERLOADs; the functions must be available to resolve
27916 types. */
27917 inner_expr = expr;
27918 if (TREE_CODE (inner_expr) == STMT_EXPR)
27919 inner_expr = stmt_expr_value_expr (inner_expr);
27920 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27921 inner_expr = TREE_OPERAND (inner_expr, 0);
27922 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27923 inner_expr = TREE_OPERAND (inner_expr, 1);
27924 if (is_overloaded_fn (inner_expr)
27925 || TREE_CODE (inner_expr) == OFFSET_REF)
27926 return orig_expr;
27927 /* There is no need to return a proxy for a variable or enumerator. */
27928 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27929 return orig_expr;
27930 /* Preserve string constants; conversions from string constants to
27931 "char *" are allowed, even though normally a "const char *"
27932 cannot be used to initialize a "char *". */
27933 if (TREE_CODE (expr) == STRING_CST)
27934 return orig_expr;
27935 /* Preserve void and arithmetic constants, as an optimization -- there is no
27936 reason to create a new node. */
27937 if (TREE_CODE (expr) == VOID_CST
27938 || TREE_CODE (expr) == INTEGER_CST
27939 || TREE_CODE (expr) == REAL_CST)
27940 return orig_expr;
27941 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27942 There is at least one place where we want to know that a
27943 particular expression is a throw-expression: when checking a ?:
27944 expression, there are special rules if the second or third
27945 argument is a throw-expression. */
27946 if (TREE_CODE (expr) == THROW_EXPR)
27947 return orig_expr;
27948
27949 /* Don't wrap an initializer list, we need to be able to look inside. */
27950 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27951 return orig_expr;
27952
27953 /* Don't wrap a dummy object, we need to be able to test for it. */
27954 if (is_dummy_object (expr))
27955 return orig_expr;
27956
27957 if (TREE_CODE (expr) == COND_EXPR)
27958 return build3 (COND_EXPR,
27959 TREE_TYPE (expr),
27960 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27961 (TREE_OPERAND (expr, 1)
27962 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27963 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27964 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27965 if (TREE_CODE (expr) == COMPOUND_EXPR
27966 && !COMPOUND_EXPR_OVERLOADED (expr))
27967 return build2 (COMPOUND_EXPR,
27968 TREE_TYPE (expr),
27969 TREE_OPERAND (expr, 0),
27970 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27971
27972 /* If the type is unknown, it can't really be non-dependent */
27973 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27974
27975 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27976 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27977 TREE_TYPE (expr), expr);
27978 }
27979
27980 /* ARGS is a vector of expressions as arguments to a function call.
27981 Replace the arguments with equivalent non-dependent expressions.
27982 This modifies ARGS in place. */
27983
27984 void
27985 make_args_non_dependent (vec<tree, va_gc> *args)
27986 {
27987 unsigned int ix;
27988 tree arg;
27989
27990 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27991 {
27992 tree newarg = build_non_dependent_expr (arg);
27993 if (newarg != arg)
27994 (*args)[ix] = newarg;
27995 }
27996 }
27997
27998 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27999 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
28000 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
28001
28002 static tree
28003 make_auto_1 (tree name, bool set_canonical)
28004 {
28005 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
28006 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
28007 TYPE_STUB_DECL (au) = TYPE_NAME (au);
28008 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
28009 (0, processing_template_decl + 1, processing_template_decl + 1,
28010 TYPE_NAME (au), NULL_TREE);
28011 if (set_canonical)
28012 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28013 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28014 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28015 if (name == decltype_auto_identifier)
28016 AUTO_IS_DECLTYPE (au) = true;
28017
28018 return au;
28019 }
28020
28021 tree
28022 make_decltype_auto (void)
28023 {
28024 return make_auto_1 (decltype_auto_identifier, true);
28025 }
28026
28027 tree
28028 make_auto (void)
28029 {
28030 return make_auto_1 (auto_identifier, true);
28031 }
28032
28033 /* Return a C++17 deduction placeholder for class template TMPL. */
28034
28035 tree
28036 make_template_placeholder (tree tmpl)
28037 {
28038 tree t = make_auto_1 (auto_identifier, false);
28039 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28040 /* Our canonical type depends on the placeholder. */
28041 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28042 return t;
28043 }
28044
28045 /* True iff T is a C++17 class template deduction placeholder. */
28046
28047 bool
28048 template_placeholder_p (tree t)
28049 {
28050 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28051 }
28052
28053 /* Make a "constrained auto" type-specifier. This is an auto or
28054 decltype(auto) type with constraints that must be associated after
28055 deduction. The constraint is formed from the given concept CON
28056 and its optional sequence of template arguments ARGS.
28057
28058 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28059
28060 static tree
28061 make_constrained_placeholder_type (tree type, tree con, tree args)
28062 {
28063 /* Build the constraint. */
28064 tree tmpl = DECL_TI_TEMPLATE (con);
28065 tree expr = tmpl;
28066 if (TREE_CODE (con) == FUNCTION_DECL)
28067 expr = ovl_make (tmpl);
28068 ++processing_template_decl;
28069 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28070 --processing_template_decl;
28071
28072 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
28073
28074 /* Our canonical type depends on the constraint. */
28075 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28076
28077 /* Attach the constraint to the type declaration. */
28078 return TYPE_NAME (type);
28079 }
28080
28081 /* Make a "constrained auto" type-specifier. */
28082
28083 tree
28084 make_constrained_auto (tree con, tree args)
28085 {
28086 tree type = make_auto_1 (auto_identifier, false);
28087 return make_constrained_placeholder_type (type, con, args);
28088 }
28089
28090 /* Make a "constrained decltype(auto)" type-specifier. */
28091
28092 tree
28093 make_constrained_decltype_auto (tree con, tree args)
28094 {
28095 tree type = make_auto_1 (decltype_auto_identifier, false);
28096 return make_constrained_placeholder_type (type, con, args);
28097 }
28098
28099 /* Build and return a concept definition. Like other templates, the
28100 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28101 the TEMPLATE_DECL. */
28102
28103 tree
28104 finish_concept_definition (cp_expr id, tree init)
28105 {
28106 gcc_assert (identifier_p (id));
28107 gcc_assert (processing_template_decl);
28108
28109 location_t loc = id.get_location();
28110
28111 /* A concept-definition shall not have associated constraints. */
28112 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28113 {
28114 error_at (loc, "a concept cannot be constrained");
28115 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28116 }
28117
28118 /* A concept-definition shall appear in namespace scope. Templates
28119 aren't allowed in block scope, so we only need to check for class
28120 scope. */
28121 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28122 {
28123 error_at (loc, "concept %qE not in namespace scope", *id);
28124 return error_mark_node;
28125 }
28126
28127 /* Initially build the concept declaration; its type is bool. */
28128 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28129 DECL_CONTEXT (decl) = current_scope ();
28130 DECL_INITIAL (decl) = init;
28131
28132 set_originating_module (decl, false);
28133
28134 /* Push the enclosing template. */
28135 return push_template_decl (decl);
28136 }
28137
28138 /* Given type ARG, return std::initializer_list<ARG>. */
28139
28140 static tree
28141 listify (tree arg)
28142 {
28143 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28144
28145 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28146 {
28147 gcc_rich_location richloc (input_location);
28148 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28149 error_at (&richloc,
28150 "deducing from brace-enclosed initializer list"
28151 " requires %<#include <initializer_list>%>");
28152
28153 return error_mark_node;
28154 }
28155 tree argvec = make_tree_vec (1);
28156 TREE_VEC_ELT (argvec, 0) = arg;
28157
28158 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28159 NULL_TREE, 0, tf_warning_or_error);
28160 }
28161
28162 /* Replace auto in TYPE with std::initializer_list<auto>. */
28163
28164 static tree
28165 listify_autos (tree type, tree auto_node)
28166 {
28167 tree init_auto = listify (strip_top_quals (auto_node));
28168 tree argvec = make_tree_vec (1);
28169 TREE_VEC_ELT (argvec, 0) = init_auto;
28170 if (processing_template_decl)
28171 argvec = add_to_template_args (current_template_args (), argvec);
28172 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28173 }
28174
28175 /* Hash traits for hashing possibly constrained 'auto'
28176 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28177
28178 struct auto_hash : default_hash_traits<tree>
28179 {
28180 static inline hashval_t hash (tree);
28181 static inline bool equal (tree, tree);
28182 };
28183
28184 /* Hash the 'auto' T. */
28185
28186 inline hashval_t
28187 auto_hash::hash (tree t)
28188 {
28189 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28190 /* Matching constrained-type-specifiers denote the same template
28191 parameter, so hash the constraint. */
28192 return hash_placeholder_constraint (c);
28193 else
28194 /* But unconstrained autos are all separate, so just hash the pointer. */
28195 return iterative_hash_object (t, 0);
28196 }
28197
28198 /* Compare two 'auto's. */
28199
28200 inline bool
28201 auto_hash::equal (tree t1, tree t2)
28202 {
28203 if (t1 == t2)
28204 return true;
28205
28206 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28207 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28208
28209 /* Two unconstrained autos are distinct. */
28210 if (!c1 || !c2)
28211 return false;
28212
28213 return equivalent_placeholder_constraints (c1, c2);
28214 }
28215
28216 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28217 constrained) auto, add it to the vector. */
28218
28219 static int
28220 extract_autos_r (tree t, void *data)
28221 {
28222 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28223 if (is_auto (t))
28224 {
28225 /* All the autos were built with index 0; fix that up now. */
28226 tree *p = hash.find_slot (t, INSERT);
28227 unsigned idx;
28228 if (*p)
28229 /* If this is a repeated constrained-type-specifier, use the index we
28230 chose before. */
28231 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28232 else
28233 {
28234 /* Otherwise this is new, so use the current count. */
28235 *p = t;
28236 idx = hash.elements () - 1;
28237 }
28238 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28239 }
28240
28241 /* Always keep walking. */
28242 return 0;
28243 }
28244
28245 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28246 says they can appear anywhere in the type. */
28247
28248 static tree
28249 extract_autos (tree type)
28250 {
28251 hash_set<tree> visited;
28252 hash_table<auto_hash> hash (2);
28253
28254 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28255
28256 tree tree_vec = make_tree_vec (hash.elements());
28257 for (hash_table<auto_hash>::iterator iter = hash.begin();
28258 iter != hash.end(); ++iter)
28259 {
28260 tree elt = *iter;
28261 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28262 TREE_VEC_ELT (tree_vec, i)
28263 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28264 }
28265
28266 return tree_vec;
28267 }
28268
28269 /* The stem for deduction guide names. */
28270 const char *const dguide_base = "__dguide_";
28271
28272 /* Return the name for a deduction guide for class template TMPL. */
28273
28274 tree
28275 dguide_name (tree tmpl)
28276 {
28277 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28278 tree tname = TYPE_IDENTIFIER (type);
28279 char *buf = (char *) alloca (1 + strlen (dguide_base)
28280 + IDENTIFIER_LENGTH (tname));
28281 memcpy (buf, dguide_base, strlen (dguide_base));
28282 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28283 IDENTIFIER_LENGTH (tname) + 1);
28284 tree dname = get_identifier (buf);
28285 TREE_TYPE (dname) = type;
28286 return dname;
28287 }
28288
28289 /* True if NAME is the name of a deduction guide. */
28290
28291 bool
28292 dguide_name_p (tree name)
28293 {
28294 return (TREE_CODE (name) == IDENTIFIER_NODE
28295 && TREE_TYPE (name)
28296 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28297 strlen (dguide_base)));
28298 }
28299
28300 /* True if FN is a deduction guide. */
28301
28302 bool
28303 deduction_guide_p (const_tree fn)
28304 {
28305 if (DECL_P (fn))
28306 if (tree name = DECL_NAME (fn))
28307 return dguide_name_p (name);
28308 return false;
28309 }
28310
28311 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28312
28313 bool
28314 copy_guide_p (const_tree fn)
28315 {
28316 gcc_assert (deduction_guide_p (fn));
28317 if (!DECL_ARTIFICIAL (fn))
28318 return false;
28319 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28320 return (TREE_CHAIN (parms) == void_list_node
28321 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28322 }
28323
28324 /* True if FN is a guide generated from a constructor template. */
28325
28326 bool
28327 template_guide_p (const_tree fn)
28328 {
28329 gcc_assert (deduction_guide_p (fn));
28330 if (!DECL_ARTIFICIAL (fn))
28331 return false;
28332 tree tmpl = DECL_TI_TEMPLATE (fn);
28333 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28334 return PRIMARY_TEMPLATE_P (org);
28335 return false;
28336 }
28337
28338 /* True if FN is an aggregate initialization guide or the copy deduction
28339 guide. */
28340
28341 bool
28342 builtin_guide_p (const_tree fn)
28343 {
28344 if (!deduction_guide_p (fn))
28345 return false;
28346 if (!DECL_ARTIFICIAL (fn))
28347 /* Explicitly declared. */
28348 return false;
28349 if (DECL_ABSTRACT_ORIGIN (fn))
28350 /* Derived from a constructor. */
28351 return false;
28352 return true;
28353 }
28354
28355 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28356 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28357 template parameter types. Note that the handling of template template
28358 parameters relies on current_template_parms being set appropriately for the
28359 new template. */
28360
28361 static tree
28362 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28363 tree tsubst_args, tsubst_flags_t complain)
28364 {
28365 if (olddecl == error_mark_node)
28366 return error_mark_node;
28367
28368 tree oldidx = get_template_parm_index (olddecl);
28369
28370 tree newtype;
28371 if (TREE_CODE (olddecl) == TYPE_DECL
28372 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28373 {
28374 tree oldtype = TREE_TYPE (olddecl);
28375 newtype = cxx_make_type (TREE_CODE (oldtype));
28376 TYPE_MAIN_VARIANT (newtype) = newtype;
28377 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28378 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28379 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28380 }
28381 else
28382 {
28383 newtype = TREE_TYPE (olddecl);
28384 if (type_uses_auto (newtype))
28385 {
28386 // Substitute once to fix references to other template parameters.
28387 newtype = tsubst (newtype, tsubst_args,
28388 complain|tf_partial, NULL_TREE);
28389 // Now substitute again to reduce the level of the auto.
28390 newtype = tsubst (newtype, current_template_args (),
28391 complain, NULL_TREE);
28392 }
28393 else
28394 newtype = tsubst (newtype, tsubst_args,
28395 complain, NULL_TREE);
28396 }
28397
28398 tree newdecl
28399 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28400 DECL_NAME (olddecl), newtype);
28401 SET_DECL_TEMPLATE_PARM_P (newdecl);
28402
28403 tree newidx;
28404 if (TREE_CODE (olddecl) == TYPE_DECL
28405 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28406 {
28407 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28408 = build_template_parm_index (index, level, level,
28409 newdecl, newtype);
28410 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28411 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28412 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28413 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28414 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28415 else
28416 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28417
28418 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28419 {
28420 DECL_TEMPLATE_RESULT (newdecl)
28421 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28422 DECL_NAME (olddecl), newtype);
28423 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28424 // First create a copy (ttargs) of tsubst_args with an
28425 // additional level for the template template parameter's own
28426 // template parameters (ttparms).
28427 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28428 (DECL_TEMPLATE_PARMS (olddecl)));
28429 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28430 tree ttargs = make_tree_vec (depth + 1);
28431 for (int i = 0; i < depth; ++i)
28432 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28433 TREE_VEC_ELT (ttargs, depth)
28434 = template_parms_level_to_args (ttparms);
28435 // Substitute ttargs into ttparms to fix references to
28436 // other template parameters.
28437 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28438 complain|tf_partial);
28439 // Now substitute again with args based on tparms, to reduce
28440 // the level of the ttparms.
28441 ttargs = current_template_args ();
28442 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28443 complain);
28444 // Finally, tack the adjusted parms onto tparms.
28445 ttparms = tree_cons (size_int (depth), ttparms,
28446 current_template_parms);
28447 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28448 }
28449 }
28450 else
28451 {
28452 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28453 tree newconst
28454 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28455 TREE_CODE (oldconst),
28456 DECL_NAME (oldconst), newtype);
28457 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28458 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28459 SET_DECL_TEMPLATE_PARM_P (newconst);
28460 newidx = build_template_parm_index (index, level, level,
28461 newconst, newtype);
28462 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28463 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28464 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28465 }
28466
28467 return newdecl;
28468 }
28469
28470 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28471 template parameter. */
28472
28473 static tree
28474 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28475 tree targs, unsigned targs_index, tsubst_flags_t complain)
28476 {
28477 tree olddecl = TREE_VALUE (oldelt);
28478 tree newdecl = rewrite_template_parm (olddecl, index, level,
28479 targs, complain);
28480 if (newdecl == error_mark_node)
28481 return error_mark_node;
28482 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28483 targs, complain, NULL_TREE);
28484 tree list = build_tree_list (newdef, newdecl);
28485 TEMPLATE_PARM_CONSTRAINTS (list)
28486 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28487 targs, complain, NULL_TREE);
28488 int depth = TMPL_ARGS_DEPTH (targs);
28489 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28490 return list;
28491 }
28492
28493 /* Returns a C++17 class deduction guide template based on the constructor
28494 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28495 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28496 aggregate initialization guide. */
28497
28498 static tree
28499 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28500 {
28501 tree tparms, targs, fparms, fargs, ci;
28502 bool memtmpl = false;
28503 bool explicit_p;
28504 location_t loc;
28505 tree fn_tmpl = NULL_TREE;
28506
28507 if (outer_args)
28508 {
28509 ++processing_template_decl;
28510 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28511 --processing_template_decl;
28512 }
28513
28514 if (!DECL_DECLARES_FUNCTION_P (ctor))
28515 {
28516 if (TYPE_P (ctor))
28517 {
28518 bool copy_p = TYPE_REF_P (ctor);
28519 if (copy_p)
28520 fparms = tree_cons (NULL_TREE, type, void_list_node);
28521 else
28522 fparms = void_list_node;
28523 }
28524 else if (TREE_CODE (ctor) == TREE_LIST)
28525 fparms = ctor;
28526 else
28527 gcc_unreachable ();
28528
28529 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28530 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28531 targs = CLASSTYPE_TI_ARGS (type);
28532 ci = NULL_TREE;
28533 fargs = NULL_TREE;
28534 loc = DECL_SOURCE_LOCATION (ctmpl);
28535 explicit_p = false;
28536 }
28537 else
28538 {
28539 ++processing_template_decl;
28540 bool ok = true;
28541
28542 fn_tmpl
28543 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28544 : DECL_TI_TEMPLATE (ctor));
28545 if (outer_args)
28546 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28547 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28548
28549 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28550 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28551 fully specialized args for the enclosing class. Strip those off, as
28552 the deduction guide won't have those template parameters. */
28553 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28554 TMPL_PARMS_DEPTH (tparms));
28555 /* Discard the 'this' parameter. */
28556 fparms = FUNCTION_ARG_CHAIN (ctor);
28557 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28558 ci = get_constraints (ctor);
28559 loc = DECL_SOURCE_LOCATION (ctor);
28560 explicit_p = DECL_NONCONVERTING_P (ctor);
28561
28562 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28563 {
28564 memtmpl = true;
28565
28566 /* For a member template constructor, we need to flatten the two
28567 template parameter lists into one, and then adjust the function
28568 signature accordingly. This gets...complicated. */
28569 tree save_parms = current_template_parms;
28570
28571 /* For a member template we should have two levels of parms/args, one
28572 for the class and one for the constructor. We stripped
28573 specialized args for further enclosing classes above. */
28574 const int depth = 2;
28575 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28576
28577 /* Template args for translating references to the two-level template
28578 parameters into references to the one-level template parameters we
28579 are creating. */
28580 tree tsubst_args = copy_node (targs);
28581 TMPL_ARGS_LEVEL (tsubst_args, depth)
28582 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28583
28584 /* Template parms for the constructor template. */
28585 tree ftparms = TREE_VALUE (tparms);
28586 unsigned flen = TREE_VEC_LENGTH (ftparms);
28587 /* Template parms for the class template. */
28588 tparms = TREE_CHAIN (tparms);
28589 tree ctparms = TREE_VALUE (tparms);
28590 unsigned clen = TREE_VEC_LENGTH (ctparms);
28591 /* Template parms for the deduction guide start as a copy of the
28592 template parms for the class. We set current_template_parms for
28593 lookup_template_class_1. */
28594 current_template_parms = tparms = copy_node (tparms);
28595 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28596 for (unsigned i = 0; i < clen; ++i)
28597 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28598
28599 /* Now we need to rewrite the constructor parms to append them to the
28600 class parms. */
28601 for (unsigned i = 0; i < flen; ++i)
28602 {
28603 unsigned index = i + clen;
28604 unsigned level = 1;
28605 tree oldelt = TREE_VEC_ELT (ftparms, i);
28606 tree newelt
28607 = rewrite_tparm_list (oldelt, index, level,
28608 tsubst_args, i, complain);
28609 if (newelt == error_mark_node)
28610 ok = false;
28611 TREE_VEC_ELT (new_vec, index) = newelt;
28612 }
28613
28614 /* Now we have a final set of template parms to substitute into the
28615 function signature. */
28616 targs = template_parms_to_args (tparms);
28617 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28618 complain, ctor);
28619 if (fparms == error_mark_node)
28620 ok = false;
28621 if (ci)
28622 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28623
28624 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28625 cp_unevaluated_operand. */
28626 cp_evaluated ev;
28627 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28628 current_template_parms = save_parms;
28629 }
28630 else
28631 {
28632 /* Substitute in the same arguments to rewrite class members into
28633 references to members of an unknown specialization. */
28634 cp_evaluated ev;
28635 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28636 fargs = tsubst (fargs, targs, complain, ctor);
28637 if (ci)
28638 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28639 }
28640
28641 --processing_template_decl;
28642 if (!ok)
28643 return error_mark_node;
28644 }
28645
28646 if (!memtmpl)
28647 {
28648 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28649 tparms = copy_node (tparms);
28650 INNERMOST_TEMPLATE_PARMS (tparms)
28651 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28652 }
28653
28654 tree fntype = build_function_type (type, fparms);
28655 tree ded_fn = build_lang_decl_loc (loc,
28656 FUNCTION_DECL,
28657 dguide_name (type), fntype);
28658 DECL_ARGUMENTS (ded_fn) = fargs;
28659 DECL_ARTIFICIAL (ded_fn) = true;
28660 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28661 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28662 DECL_ARTIFICIAL (ded_tmpl) = true;
28663 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28664 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28665 if (DECL_P (ctor))
28666 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28667 if (ci)
28668 set_constraints (ded_tmpl, ci);
28669
28670 return ded_tmpl;
28671 }
28672
28673 /* Add to LIST the member types for the reshaped initializer CTOR. */
28674
28675 static tree
28676 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28677 {
28678 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28679 tree idx, val; unsigned i;
28680 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28681 {
28682 tree ftype = elt ? elt : TREE_TYPE (idx);
28683 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28684 && CONSTRUCTOR_NELTS (val)
28685 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28686 type gets a single initializer. */
28687 && CP_AGGREGATE_TYPE_P (ftype)
28688 && !(TREE_CODE (ftype) == ARRAY_TYPE
28689 && uses_template_parms (TYPE_DOMAIN (ftype))))
28690 {
28691 tree subelt = NULL_TREE;
28692 if (TREE_CODE (ftype) == ARRAY_TYPE)
28693 subelt = TREE_TYPE (ftype);
28694 list = collect_ctor_idx_types (val, list, subelt);
28695 continue;
28696 }
28697 tree arg = NULL_TREE;
28698 if (i == v->length() - 1
28699 && PACK_EXPANSION_P (ftype))
28700 /* Give the trailing pack expansion parameter a default argument to
28701 match aggregate initialization behavior, even if we deduce the
28702 length of the pack separately to more than we have initializers. */
28703 arg = build_constructor (init_list_type_node, NULL);
28704 /* if ei is of array type and xi is a braced-init-list or string literal,
28705 Ti is an rvalue reference to the declared type of ei */
28706 STRIP_ANY_LOCATION_WRAPPER (val);
28707 if (TREE_CODE (ftype) == ARRAY_TYPE
28708 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28709 || TREE_CODE (val) == STRING_CST))
28710 {
28711 if (TREE_CODE (val) == STRING_CST)
28712 ftype = cp_build_qualified_type
28713 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28714 ftype = (cp_build_reference_type
28715 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28716 }
28717 list = tree_cons (arg, ftype, list);
28718 }
28719
28720 return list;
28721 }
28722
28723 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28724
28725 static bool
28726 is_spec_or_derived (tree etype, tree tmpl)
28727 {
28728 if (!etype || !CLASS_TYPE_P (etype))
28729 return false;
28730
28731 tree type = TREE_TYPE (tmpl);
28732 tree tparms = (INNERMOST_TEMPLATE_PARMS
28733 (DECL_TEMPLATE_PARMS (tmpl)));
28734 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28735 int err = unify (tparms, targs, type, etype,
28736 UNIFY_ALLOW_DERIVED, /*explain*/false);
28737 ggc_free (targs);
28738 return !err;
28739 }
28740
28741 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28742 INIT. */
28743
28744 static tree
28745 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28746 {
28747 if (cxx_dialect < cxx20)
28748 return NULL_TREE;
28749
28750 if (init == NULL_TREE)
28751 return NULL_TREE;
28752
28753 tree type = TREE_TYPE (tmpl);
28754 if (!CP_AGGREGATE_TYPE_P (type))
28755 return NULL_TREE;
28756
28757 /* No aggregate candidate for copy-initialization. */
28758 if (args->length() == 1)
28759 {
28760 tree val = (*args)[0];
28761 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28762 return NULL_TREE;
28763 }
28764
28765 /* If we encounter a problem, we just won't add the candidate. */
28766 tsubst_flags_t complain = tf_none;
28767
28768 tree parms = NULL_TREE;
28769 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28770 {
28771 init = reshape_init (type, init, complain);
28772 if (init == error_mark_node)
28773 return NULL_TREE;
28774 parms = collect_ctor_idx_types (init, parms);
28775 }
28776 else if (TREE_CODE (init) == TREE_LIST)
28777 {
28778 int len = list_length (init);
28779 for (tree field = TYPE_FIELDS (type);
28780 len;
28781 --len, field = DECL_CHAIN (field))
28782 {
28783 field = next_initializable_field (field);
28784 if (!field)
28785 return NULL_TREE;
28786 tree ftype = finish_decltype_type (field, true, complain);
28787 parms = tree_cons (NULL_TREE, ftype, parms);
28788 }
28789 }
28790 else
28791 /* Aggregate initialization doesn't apply to an initializer expression. */
28792 return NULL_TREE;
28793
28794 if (parms)
28795 {
28796 tree last = parms;
28797 parms = nreverse (parms);
28798 TREE_CHAIN (last) = void_list_node;
28799 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28800 return guide;
28801 }
28802
28803 return NULL_TREE;
28804 }
28805
28806 /* UGUIDES are the deduction guides for the underlying template of alias
28807 template TMPL; adjust them to be deduction guides for TMPL. */
28808
28809 static tree
28810 alias_ctad_tweaks (tree tmpl, tree uguides)
28811 {
28812 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28813 class type (9.2.8.2) where the template-name names an alias template A,
28814 the defining-type-id of A must be of the form
28815
28816 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28817
28818 as specified in 9.2.8.2. The guides of A are the set of functions or
28819 function templates formed as follows. For each function or function
28820 template f in the guides of the template named by the simple-template-id
28821 of the defining-type-id, the template arguments of the return type of f
28822 are deduced from the defining-type-id of A according to the process in
28823 13.10.2.5 with the exception that deduction does not fail if not all
28824 template arguments are deduced. Let g denote the result of substituting
28825 these deductions into f. If substitution succeeds, form a function or
28826 function template f' with the following properties and add it to the set
28827 of guides of A:
28828
28829 * The function type of f' is the function type of g.
28830
28831 * If f is a function template, f' is a function template whose template
28832 parameter list consists of all the template parameters of A (including
28833 their default template arguments) that appear in the above deductions or
28834 (recursively) in their default template arguments, followed by the
28835 template parameters of f that were not deduced (including their default
28836 template arguments), otherwise f' is not a function template.
28837
28838 * The associated constraints (13.5.2) are the conjunction of the
28839 associated constraints of g and a constraint that is satisfied if and only
28840 if the arguments of A are deducible (see below) from the return type.
28841
28842 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28843 be so as well.
28844
28845 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28846 considered to be so as well.
28847
28848 * The explicit-specifier of f' is the explicit-specifier of g (if
28849 any). */
28850
28851 /* This implementation differs from the above in two significant ways:
28852
28853 1) We include all template parameters of A, not just some.
28854 2) The added constraint is same_type instead of deducible.
28855
28856 I believe that while it's probably possible to construct a testcase that
28857 behaves differently with this simplification, it should have the same
28858 effect for real uses. Including all template parameters means that we
28859 deduce all parameters of A when resolving the call, so when we're in the
28860 constraint we don't need to deduce them again, we can just check whether
28861 the deduction produced the desired result. */
28862
28863 tsubst_flags_t complain = tf_warning_or_error;
28864 tree atype = TREE_TYPE (tmpl);
28865 tree aguides = NULL_TREE;
28866 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28867 unsigned natparms = TREE_VEC_LENGTH (atparms);
28868 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28869 for (ovl_iterator iter (uguides); iter; ++iter)
28870 {
28871 tree f = *iter;
28872 tree in_decl = f;
28873 location_t loc = DECL_SOURCE_LOCATION (f);
28874 tree ret = TREE_TYPE (TREE_TYPE (f));
28875 tree fprime = f;
28876 if (TREE_CODE (f) == TEMPLATE_DECL)
28877 {
28878 processing_template_decl_sentinel ptds (/*reset*/false);
28879 ++processing_template_decl;
28880
28881 /* Deduce template arguments for f from the type-id of A. */
28882 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28883 unsigned len = TREE_VEC_LENGTH (ftparms);
28884 tree targs = make_tree_vec (len);
28885 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28886 gcc_assert (!err);
28887
28888 /* The number of parms for f' is the number of parms for A plus
28889 non-deduced parms of f. */
28890 unsigned ndlen = 0;
28891 unsigned j;
28892 for (unsigned i = 0; i < len; ++i)
28893 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28894 ++ndlen;
28895 tree gtparms = make_tree_vec (natparms + ndlen);
28896
28897 /* First copy over the parms of A. */
28898 for (j = 0; j < natparms; ++j)
28899 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28900 /* Now rewrite the non-deduced parms of f. */
28901 for (unsigned i = 0; ndlen && i < len; ++i)
28902 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28903 {
28904 --ndlen;
28905 unsigned index = j++;
28906 unsigned level = 1;
28907 tree oldlist = TREE_VEC_ELT (ftparms, i);
28908 tree list = rewrite_tparm_list (oldlist, index, level,
28909 targs, i, complain);
28910 TREE_VEC_ELT (gtparms, index) = list;
28911 }
28912 gtparms = build_tree_list (size_one_node, gtparms);
28913
28914 /* Substitute the deduced arguments plus the rewritten template
28915 parameters into f to get g. This covers the type, copyness,
28916 guideness, and explicit-specifier. */
28917 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28918 if (g == error_mark_node)
28919 return error_mark_node;
28920 DECL_USE_TEMPLATE (g) = 0;
28921 fprime = build_template_decl (g, gtparms, false);
28922 DECL_TEMPLATE_RESULT (fprime) = g;
28923 TREE_TYPE (fprime) = TREE_TYPE (g);
28924 tree gtargs = template_parms_to_args (gtparms);
28925 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28926 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28927
28928 /* Substitute the associated constraints. */
28929 tree ci = get_constraints (f);
28930 if (ci)
28931 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28932 if (ci == error_mark_node)
28933 return error_mark_node;
28934
28935 /* Add a constraint that the return type matches the instantiation of
28936 A with the same template arguments. */
28937 ret = TREE_TYPE (TREE_TYPE (fprime));
28938 if (!same_type_p (atype, ret)
28939 /* FIXME this should mean they don't compare as equivalent. */
28940 || dependent_alias_template_spec_p (atype, nt_opaque))
28941 {
28942 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28943 ci = append_constraint (ci, same);
28944 }
28945
28946 if (ci)
28947 {
28948 remove_constraints (fprime);
28949 set_constraints (fprime, ci);
28950 }
28951 }
28952 else
28953 {
28954 /* For a non-template deduction guide, if the arguments of A aren't
28955 deducible from the return type, don't add the candidate. */
28956 tree targs = make_tree_vec (natparms);
28957 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28958 for (unsigned i = 0; !err && i < natparms; ++i)
28959 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28960 err = true;
28961 if (err)
28962 continue;
28963 }
28964
28965 aguides = lookup_add (fprime, aguides);
28966 }
28967
28968 return aguides;
28969 }
28970
28971 /* Return artificial deduction guides built from the constructors of class
28972 template TMPL. */
28973
28974 static tree
28975 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28976 {
28977 tree type = TREE_TYPE (tmpl);
28978 tree outer_args = NULL_TREE;
28979 if (DECL_CLASS_SCOPE_P (tmpl)
28980 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28981 {
28982 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28983 type = TREE_TYPE (most_general_template (tmpl));
28984 }
28985
28986 tree cands = NULL_TREE;
28987
28988 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28989 {
28990 /* Skip inherited constructors. */
28991 if (iter.using_p ())
28992 continue;
28993
28994 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28995 cands = lookup_add (guide, cands);
28996 }
28997
28998 /* Add implicit default constructor deduction guide. */
28999 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
29000 {
29001 tree guide = build_deduction_guide (type, type, outer_args,
29002 complain);
29003 cands = lookup_add (guide, cands);
29004 }
29005
29006 /* Add copy guide. */
29007 {
29008 tree gtype = build_reference_type (type);
29009 tree guide = build_deduction_guide (type, gtype, outer_args,
29010 complain);
29011 cands = lookup_add (guide, cands);
29012 }
29013
29014 return cands;
29015 }
29016
29017 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29018
29019 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29020 aggregate candidate is added separately because it depends on the
29021 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29022 guide. */
29023
29024 static tree
29025 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29026 {
29027 tree guides = NULL_TREE;
29028 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29029 {
29030 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29031 tree tinfo = get_template_info (under);
29032 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29033 complain);
29034 }
29035 else
29036 {
29037 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29038 dguide_name (tmpl),
29039 LOOK_want::NORMAL, /*complain*/false);
29040 if (guides == error_mark_node)
29041 guides = NULL_TREE;
29042 else
29043 any_dguides_p = true;
29044 }
29045
29046 /* Cache the deduction guides for a template. We also remember the result of
29047 lookup, and rebuild everything if it changes; should be very rare. */
29048 tree_pair_p cache = NULL;
29049 if (tree_pair_p &r
29050 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29051 {
29052 cache = r;
29053 if (cache->purpose == guides)
29054 return cache->value;
29055 }
29056 else
29057 {
29058 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29059 cache->purpose = guides;
29060 }
29061
29062 tree cands = NULL_TREE;
29063 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29064 cands = alias_ctad_tweaks (tmpl, guides);
29065 else
29066 {
29067 cands = ctor_deduction_guides_for (tmpl, complain);
29068 for (ovl_iterator it (guides); it; ++it)
29069 cands = lookup_add (*it, cands);
29070 }
29071
29072 cache->value = cands;
29073 return cands;
29074 }
29075
29076 /* Return whether TMPL is a (class template argument-) deducible template. */
29077
29078 bool
29079 ctad_template_p (tree tmpl)
29080 {
29081 /* A deducible template is either a class template or is an alias template
29082 whose defining-type-id is of the form
29083
29084 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29085
29086 where the nested-name-specifier (if any) is non-dependent and the
29087 template-name of the simple-template-id names a deducible template. */
29088
29089 if (DECL_CLASS_TEMPLATE_P (tmpl)
29090 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29091 return true;
29092 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29093 return false;
29094 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29095 if (tree tinfo = get_template_info (orig))
29096 return ctad_template_p (TI_TEMPLATE (tinfo));
29097 return false;
29098 }
29099
29100 /* Deduce template arguments for the class template placeholder PTYPE for
29101 template TMPL based on the initializer INIT, and return the resulting
29102 type. */
29103
29104 static tree
29105 do_class_deduction (tree ptype, tree tmpl, tree init,
29106 int flags, tsubst_flags_t complain)
29107 {
29108 /* We should have handled this in the caller. */
29109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29110 return ptype;
29111
29112 /* Initializing one placeholder from another. */
29113 if (init && TREE_CODE (init) == TEMPLATE_PARM_INDEX
29114 && is_auto (TREE_TYPE (init))
29115 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29116 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29117
29118 /* Look through alias templates that just rename another template. */
29119 tmpl = get_underlying_template (tmpl);
29120 if (!ctad_template_p (tmpl))
29121 {
29122 if (complain & tf_error)
29123 error ("non-deducible template %qT used without template arguments", tmpl);
29124 return error_mark_node;
29125 }
29126 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29127 {
29128 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29129 if (complain & tf_warning_or_error)
29130 pedwarn (input_location, 0, "alias template deduction only available "
29131 "with %<-std=c++20%> or %<-std=gnu++20%>");
29132 }
29133
29134 tree type = TREE_TYPE (tmpl);
29135
29136 bool try_list_ctor = false;
29137 bool list_init_p = false;
29138
29139 releasing_vec rv_args = NULL;
29140 vec<tree,va_gc> *&args = *&rv_args;
29141 if (init == NULL_TREE)
29142 args = make_tree_vector ();
29143 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29144 {
29145 list_init_p = true;
29146 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29147 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
29148 {
29149 /* As an exception, the first phase in 16.3.1.7 (considering the
29150 initializer list as a single argument) is omitted if the
29151 initializer list consists of a single expression of type cv U,
29152 where U is a specialization of C or a class derived from a
29153 specialization of C. */
29154 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29155 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29156 try_list_ctor = false;
29157 }
29158 if (try_list_ctor || is_std_init_list (type))
29159 args = make_tree_vector_single (init);
29160 else
29161 args = make_tree_vector_from_ctor (init);
29162 }
29163 else if (TREE_CODE (init) == TREE_LIST)
29164 args = make_tree_vector_from_list (init);
29165 else
29166 args = make_tree_vector_single (init);
29167
29168 /* Do this now to avoid problems with erroneous args later on. */
29169 args = resolve_args (args, complain);
29170 if (args == NULL)
29171 return error_mark_node;
29172
29173 bool any_dguides_p = false;
29174 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29175 if (cands == error_mark_node)
29176 return error_mark_node;
29177
29178 /* Prune explicit deduction guides in copy-initialization context (but
29179 not copy-list-initialization). */
29180 bool elided = false;
29181 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29182 {
29183 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29184 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29185 elided = true;
29186
29187 if (elided)
29188 {
29189 /* Found a nonconverting guide, prune the candidates. */
29190 tree pruned = NULL_TREE;
29191 for (lkp_iterator iter (cands); iter; ++iter)
29192 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29193 pruned = lookup_add (*iter, pruned);
29194
29195 cands = pruned;
29196 }
29197 }
29198
29199 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29200 cands = lookup_add (guide, cands);
29201
29202 tree call = error_mark_node;
29203
29204 /* If this is list-initialization and the class has a list constructor, first
29205 try deducing from the list as a single argument, as [over.match.list]. */
29206 tree list_cands = NULL_TREE;
29207 if (try_list_ctor && cands)
29208 for (lkp_iterator iter (cands); iter; ++iter)
29209 {
29210 tree dg = *iter;
29211 if (is_list_ctor (dg))
29212 list_cands = lookup_add (dg, list_cands);
29213 }
29214 if (list_cands)
29215 {
29216 ++cp_unevaluated_operand;
29217 call = build_new_function_call (list_cands, &args, tf_decltype);
29218 --cp_unevaluated_operand;
29219
29220 if (call == error_mark_node)
29221 {
29222 /* That didn't work, now try treating the list as a sequence of
29223 arguments. */
29224 release_tree_vector (args);
29225 args = make_tree_vector_from_ctor (init);
29226 }
29227 }
29228
29229 if (elided && !cands)
29230 {
29231 error ("cannot deduce template arguments for copy-initialization"
29232 " of %qT, as it has no non-explicit deduction guides or "
29233 "user-declared constructors", type);
29234 return error_mark_node;
29235 }
29236 else if (!cands && call == error_mark_node)
29237 {
29238 error ("cannot deduce template arguments of %qT, as it has no viable "
29239 "deduction guides", type);
29240 return error_mark_node;
29241 }
29242
29243 if (call == error_mark_node)
29244 {
29245 ++cp_unevaluated_operand;
29246 call = build_new_function_call (cands, &args, tf_decltype);
29247 --cp_unevaluated_operand;
29248 }
29249
29250 if (call == error_mark_node)
29251 {
29252 if (complain & tf_warning_or_error)
29253 {
29254 error ("class template argument deduction failed:");
29255
29256 ++cp_unevaluated_operand;
29257 call = build_new_function_call (cands, &args,
29258 complain | tf_decltype);
29259 --cp_unevaluated_operand;
29260
29261 if (elided)
29262 inform (input_location, "explicit deduction guides not considered "
29263 "for copy-initialization");
29264 }
29265 return error_mark_node;
29266 }
29267 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29268 constructor is chosen, the initialization is ill-formed. */
29269 else if (flags & LOOKUP_ONLYCONVERTING)
29270 {
29271 tree fndecl = cp_get_callee_fndecl_nofold (call);
29272 if (fndecl && DECL_NONCONVERTING_P (fndecl))
29273 {
29274 if (complain & tf_warning_or_error)
29275 {
29276 // TODO: Pass down location from cp_finish_decl.
29277 error ("class template argument deduction for %qT failed: "
29278 "explicit deduction guide selected in "
29279 "copy-list-initialization", type);
29280 inform (DECL_SOURCE_LOCATION (fndecl),
29281 "explicit deduction guide declared here");
29282
29283 }
29284 return error_mark_node;
29285 }
29286 }
29287
29288 /* If CTAD succeeded but the type doesn't have any explicit deduction
29289 guides, this deduction might not be what the user intended. */
29290 if (call != error_mark_node && !any_dguides_p)
29291 {
29292 tree fndecl = cp_get_callee_fndecl_nofold (call);
29293 if (fndecl != NULL_TREE
29294 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29295 || global_dc->dc_warn_system_headers)
29296 && warning (OPT_Wctad_maybe_unsupported,
29297 "%qT may not intend to support class template argument "
29298 "deduction", type))
29299 inform (input_location, "add a deduction guide to suppress this "
29300 "warning");
29301 }
29302
29303 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29304 }
29305
29306 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29307 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29308 The CONTEXT determines the context in which auto deduction is performed
29309 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29310 OUTER_TARGS are used during template argument deduction
29311 (context == adc_unify) to properly substitute the result, and is ignored
29312 in other contexts.
29313
29314 For partial-concept-ids, extra args may be appended to the list of deduced
29315 template arguments prior to determining constraint satisfaction. */
29316
29317 tree
29318 do_auto_deduction (tree type, tree init, tree auto_node,
29319 tsubst_flags_t complain, auto_deduction_context context,
29320 tree outer_targs, int flags)
29321 {
29322 tree targs;
29323
29324 if (init == error_mark_node)
29325 return error_mark_node;
29326
29327 if (init && type_dependent_expression_p (init)
29328 && context != adc_unify)
29329 /* Defining a subset of type-dependent expressions that we can deduce
29330 from ahead of time isn't worth the trouble. */
29331 return type;
29332
29333 /* Similarly, we can't deduce from another undeduced decl. */
29334 if (init && undeduced_auto_decl (init))
29335 return type;
29336
29337 /* We may be doing a partial substitution, but we still want to replace
29338 auto_node. */
29339 complain &= ~tf_partial;
29340
29341 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29342 /* C++17 class template argument deduction. */
29343 return do_class_deduction (type, tmpl, init, flags, complain);
29344
29345 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29346 /* Nothing we can do with this, even in deduction context. */
29347 return type;
29348
29349 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29350 with either a new invented type template parameter U or, if the
29351 initializer is a braced-init-list (8.5.4), with
29352 std::initializer_list<U>. */
29353 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29354 {
29355 if (!DIRECT_LIST_INIT_P (init))
29356 type = listify_autos (type, auto_node);
29357 else if (CONSTRUCTOR_NELTS (init) == 1)
29358 init = CONSTRUCTOR_ELT (init, 0)->value;
29359 else
29360 {
29361 if (complain & tf_warning_or_error)
29362 {
29363 if (permerror (input_location, "direct-list-initialization of "
29364 "%<auto%> requires exactly one element"))
29365 inform (input_location,
29366 "for deduction to %<std::initializer_list%>, use copy-"
29367 "list-initialization (i.e. add %<=%> before the %<{%>)");
29368 }
29369 type = listify_autos (type, auto_node);
29370 }
29371 }
29372
29373 if (type == error_mark_node)
29374 return error_mark_node;
29375
29376 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29377 {
29378 /* We don't recurse here because we can't deduce from a nested
29379 initializer_list. */
29380 if (CONSTRUCTOR_ELTS (init))
29381 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
29382 elt.value = resolve_nondeduced_context (elt.value, complain);
29383 }
29384 else
29385 init = resolve_nondeduced_context (init, complain);
29386
29387 if (context == adc_decomp_type
29388 && auto_node == type
29389 && init != error_mark_node
29390 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29391 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29392 and initializer has array type, deduce cv-qualified array type. */
29393 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29394 complain);
29395 else if (AUTO_IS_DECLTYPE (auto_node))
29396 {
29397 tree stripped_init = tree_strip_any_location_wrapper (init);
29398 if (REFERENCE_REF_P (stripped_init))
29399 stripped_init = TREE_OPERAND (stripped_init, 0);
29400 bool id = (DECL_P (stripped_init)
29401 || ((TREE_CODE (init) == COMPONENT_REF
29402 || TREE_CODE (init) == SCOPE_REF)
29403 && !REF_PARENTHESIZED_P (init)));
29404 targs = make_tree_vec (1);
29405 TREE_VEC_ELT (targs, 0)
29406 = finish_decltype_type (init, id, tf_warning_or_error);
29407 if (type != auto_node)
29408 {
29409 if (complain & tf_error)
29410 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29411 return error_mark_node;
29412 }
29413 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29414 {
29415 if (complain & tf_error)
29416 error ("%<decltype(auto)%> cannot be cv-qualified");
29417 return error_mark_node;
29418 }
29419 }
29420 else
29421 {
29422 if (error_operand_p (init))
29423 return error_mark_node;
29424
29425 tree parms = build_tree_list (NULL_TREE, type);
29426 tree tparms;
29427
29428 if (flag_concepts)
29429 tparms = extract_autos (type);
29430 else
29431 {
29432 tparms = make_tree_vec (1);
29433 TREE_VEC_ELT (tparms, 0)
29434 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29435 }
29436
29437 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29438 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29439 DEDUCE_CALL,
29440 NULL, /*explain_p=*/false);
29441 if (val > 0)
29442 {
29443 if (processing_template_decl)
29444 /* Try again at instantiation time. */
29445 return type;
29446 if (type && type != error_mark_node
29447 && (complain & tf_error))
29448 /* If type is error_mark_node a diagnostic must have been
29449 emitted by now. Also, having a mention to '<type error>'
29450 in the diagnostic is not really useful to the user. */
29451 {
29452 if (cfun
29453 && FNDECL_USED_AUTO (current_function_decl)
29454 && (auto_node
29455 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29456 && LAMBDA_FUNCTION_P (current_function_decl))
29457 error ("unable to deduce lambda return type from %qE", init);
29458 else
29459 error ("unable to deduce %qT from %qE", type, init);
29460 type_unification_real (tparms, targs, parms, &init, 1, 0,
29461 DEDUCE_CALL,
29462 NULL, /*explain_p=*/true);
29463 }
29464 return error_mark_node;
29465 }
29466 }
29467
29468 /* Check any placeholder constraints against the deduced type. */
29469 if (flag_concepts && !processing_template_decl)
29470 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29471 {
29472 /* Use the deduced type to check the associated constraints. If we
29473 have a partial-concept-id, rebuild the argument list so that
29474 we check using the extra arguments. */
29475 check = unpack_concept_check (check);
29476 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29477 tree cdecl = TREE_OPERAND (check, 0);
29478 if (OVL_P (cdecl))
29479 cdecl = OVL_FIRST (cdecl);
29480 tree cargs = TREE_OPERAND (check, 1);
29481 if (TREE_VEC_LENGTH (cargs) > 1)
29482 {
29483 cargs = copy_node (cargs);
29484 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29485 }
29486 else
29487 cargs = targs;
29488
29489 /* Rebuild the check using the deduced arguments. */
29490 check = build_concept_check (cdecl, cargs, tf_none);
29491
29492 if (!constraints_satisfied_p (check))
29493 {
29494 if (complain & tf_warning_or_error)
29495 {
29496 auto_diagnostic_group d;
29497 switch (context)
29498 {
29499 case adc_unspecified:
29500 case adc_unify:
29501 error("placeholder constraints not satisfied");
29502 break;
29503 case adc_variable_type:
29504 case adc_decomp_type:
29505 error ("deduced initializer does not satisfy "
29506 "placeholder constraints");
29507 break;
29508 case adc_return_type:
29509 error ("deduced return type does not satisfy "
29510 "placeholder constraints");
29511 break;
29512 case adc_requirement:
29513 error ("deduced expression type does not satisfy "
29514 "placeholder constraints");
29515 break;
29516 }
29517 diagnose_constraints (input_location, check, targs);
29518 }
29519 return error_mark_node;
29520 }
29521 }
29522
29523 if (processing_template_decl && context != adc_unify)
29524 outer_targs = current_template_args ();
29525 targs = add_to_template_args (outer_targs, targs);
29526 return tsubst (type, targs, complain, NULL_TREE);
29527 }
29528
29529 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29530 result. */
29531
29532 tree
29533 splice_late_return_type (tree type, tree late_return_type)
29534 {
29535 if (late_return_type)
29536 {
29537 gcc_assert (is_auto (type) || seen_error ());
29538 return late_return_type;
29539 }
29540
29541 if (tree *auto_node = find_type_usage (&type, is_auto))
29542 {
29543 tree idx = get_template_parm_index (*auto_node);
29544 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29545 {
29546 /* In an abbreviated function template we didn't know we were dealing
29547 with a function template when we saw the auto return type, so update
29548 it to have the correct level. */
29549 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29550 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29551 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29552 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29553 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29554 *auto_node = new_auto;
29555 }
29556 }
29557 return type;
29558 }
29559
29560 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29561 'decltype(auto)' or a deduced class template. */
29562
29563 bool
29564 is_auto (const_tree type)
29565 {
29566 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29567 && (TYPE_IDENTIFIER (type) == auto_identifier
29568 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29569 return true;
29570 else
29571 return false;
29572 }
29573
29574 /* for_each_template_parm callback for type_uses_auto. */
29575
29576 int
29577 is_auto_r (tree tp, void */*data*/)
29578 {
29579 return is_auto (tp);
29580 }
29581
29582 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29583 a use of `auto'. Returns NULL_TREE otherwise. */
29584
29585 tree
29586 type_uses_auto (tree type)
29587 {
29588 if (type == NULL_TREE)
29589 return NULL_TREE;
29590 else if (flag_concepts)
29591 {
29592 /* The Concepts TS allows multiple autos in one type-specifier; just
29593 return the first one we find, do_auto_deduction will collect all of
29594 them. */
29595 if (uses_template_parms (type))
29596 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29597 /*visited*/NULL, /*nondeduced*/false);
29598 else
29599 return NULL_TREE;
29600 }
29601 else if (tree *tp = find_type_usage (&type, is_auto))
29602 return *tp;
29603 else
29604 return NULL_TREE;
29605 }
29606
29607 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29608 concepts are enabled, auto is acceptable in template arguments, but
29609 only when TEMPL identifies a template class. Return TRUE if any
29610 such errors were reported. */
29611
29612 bool
29613 check_auto_in_tmpl_args (tree tmpl, tree args)
29614 {
29615 /* If there were previous errors, nevermind. */
29616 if (!args || TREE_CODE (args) != TREE_VEC)
29617 return false;
29618
29619 /* If TMPL is an identifier, we're parsing and we can't tell yet
29620 whether TMPL is supposed to be a type, a function or a variable.
29621 We'll only be able to tell during template substitution, so we
29622 expect to be called again then. If concepts are enabled and we
29623 know we have a type, we're ok. */
29624 if (flag_concepts
29625 && (identifier_p (tmpl)
29626 || (DECL_P (tmpl)
29627 && (DECL_TYPE_TEMPLATE_P (tmpl)
29628 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29629 return false;
29630
29631 /* Quickly search for any occurrences of auto; usually there won't
29632 be any, and then we'll avoid allocating the vector. */
29633 if (!type_uses_auto (args))
29634 return false;
29635
29636 bool errors = false;
29637
29638 tree vec = extract_autos (args);
29639 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29640 {
29641 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29642 error_at (DECL_SOURCE_LOCATION (xauto),
29643 "invalid use of %qT in template argument", xauto);
29644 errors = true;
29645 }
29646
29647 return errors;
29648 }
29649
29650 /* Recursively walk over && expressions searching for EXPR. Return a reference
29651 to that expression. */
29652
29653 static tree *find_template_requirement (tree *t, tree key)
29654 {
29655 if (*t == key)
29656 return t;
29657 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29658 {
29659 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29660 return p;
29661 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29662 return p;
29663 }
29664 return 0;
29665 }
29666
29667 /* Convert the generic type parameters in PARM that match the types given in the
29668 range [START_IDX, END_IDX) from the current_template_parms into generic type
29669 packs. */
29670
29671 tree
29672 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29673 {
29674 tree current = current_template_parms;
29675 int depth = TMPL_PARMS_DEPTH (current);
29676 current = INNERMOST_TEMPLATE_PARMS (current);
29677 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29678
29679 for (int i = 0; i < start_idx; ++i)
29680 TREE_VEC_ELT (replacement, i)
29681 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29682
29683 for (int i = start_idx; i < end_idx; ++i)
29684 {
29685 /* Create a distinct parameter pack type from the current parm and add it
29686 to the replacement args to tsubst below into the generic function
29687 parameter. */
29688 tree node = TREE_VEC_ELT (current, i);
29689 tree o = TREE_TYPE (TREE_VALUE (node));
29690 tree t = copy_type (o);
29691 TEMPLATE_TYPE_PARM_INDEX (t)
29692 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29693 t, 0, 0, tf_none);
29694 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29695 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29696 TYPE_MAIN_VARIANT (t) = t;
29697 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29698 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29699 TREE_VEC_ELT (replacement, i) = t;
29700
29701 /* Replace the current template parameter with new pack. */
29702 TREE_VALUE (node) = TREE_CHAIN (t);
29703
29704 /* Surgically adjust the associated constraint of adjusted parameter
29705 and it's corresponding contribution to the current template
29706 requirements. */
29707 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29708 {
29709 tree id = unpack_concept_check (constr);
29710 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29711 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29712 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29713
29714 /* If there was a constraint, we also need to replace that in
29715 the template requirements, which we've already built. */
29716 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29717 reqs = find_template_requirement (reqs, constr);
29718 *reqs = fold;
29719 }
29720 }
29721
29722 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29723 TREE_VEC_ELT (replacement, i)
29724 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29725
29726 /* If there are more levels then build up the replacement with the outer
29727 template parms. */
29728 if (depth > 1)
29729 replacement = add_to_template_args (template_parms_to_args
29730 (TREE_CHAIN (current_template_parms)),
29731 replacement);
29732
29733 return tsubst (parm, replacement, tf_none, NULL_TREE);
29734 }
29735
29736 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29737 0..N-1. */
29738
29739 void
29740 declare_integer_pack (void)
29741 {
29742 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29743 build_function_type_list (integer_type_node,
29744 integer_type_node,
29745 NULL_TREE),
29746 NULL_TREE, ECF_CONST);
29747 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29748 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29749 CP_BUILT_IN_INTEGER_PACK);
29750 }
29751
29752 /* Walk the decl or type specialization table calling FN on each
29753 entry. */
29754
29755 void
29756 walk_specializations (bool decls_p,
29757 void (*fn) (bool decls_p, spec_entry *entry, void *data),
29758 void *data)
29759 {
29760 spec_hash_table *table = decls_p ? decl_specializations
29761 : type_specializations;
29762 spec_hash_table::iterator end (table->end ());
29763 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
29764 fn (decls_p, *iter, data);
29765 }
29766
29767 /* Lookup the specialization of *ELT, in the decl or type
29768 specialization table. Return the SPEC that's already there (NULL if
29769 nothing). If INSERT is true, and there was nothing, add the new
29770 spec. */
29771
29772 tree
29773 match_mergeable_specialization (bool decl_p, spec_entry *elt, bool insert)
29774 {
29775 hash_table<spec_hasher> *specializations
29776 = decl_p ? decl_specializations : type_specializations;
29777 hashval_t hash = spec_hasher::hash (elt);
29778 spec_entry **slot
29779 = specializations->find_slot_with_hash (elt, hash,
29780 insert ? INSERT : NO_INSERT);
29781 if (slot && *slot)
29782 return (*slot)->spec;
29783
29784 if (insert)
29785 {
29786 auto entry = ggc_alloc<spec_entry> ();
29787 *entry = *elt;
29788 *slot = entry;
29789 }
29790
29791 return NULL_TREE;
29792 }
29793
29794 /* Return flags encoding whether SPEC is on the instantiation and/or
29795 specialization lists of TMPL. */
29796
29797 unsigned
29798 get_mergeable_specialization_flags (tree tmpl, tree decl)
29799 {
29800 unsigned flags = 0;
29801
29802 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
29803 inst; inst = TREE_CHAIN (inst))
29804 if (TREE_VALUE (inst) == decl)
29805 {
29806 flags |= 1;
29807 break;
29808 }
29809
29810 if (CLASS_TYPE_P (TREE_TYPE (decl))
29811 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
29812 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
29813 /* Only need to search if DECL is a partial specialization. */
29814 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
29815 part; part = TREE_CHAIN (part))
29816 if (TREE_VALUE (part) == decl)
29817 {
29818 flags |= 2;
29819 break;
29820 }
29821
29822 return flags;
29823 }
29824
29825 /* Add a new specialization of TMPL. FLAGS is as returned from
29826 get_mergeable_specialization_flags. */
29827
29828 void
29829 add_mergeable_specialization (tree tmpl, tree args, tree decl, unsigned flags)
29830 {
29831 if (flags & 1)
29832 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
29833 = tree_cons (args, decl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
29834
29835 if (flags & 2)
29836 {
29837 /* A partial specialization. */
29838 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
29839 = tree_cons (args, decl, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
29840 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (tmpl))
29841 = TREE_TYPE (DECL_TEMPLATE_RESULT (decl));
29842 }
29843 }
29844
29845 /* Set up the hash tables for template instantiations. */
29846
29847 void
29848 init_template_processing (void)
29849 {
29850 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29851 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29852
29853 if (cxx_dialect >= cxx11)
29854 declare_integer_pack ();
29855 }
29856
29857 /* Print stats about the template hash tables for -fstats. */
29858
29859 void
29860 print_template_statistics (void)
29861 {
29862 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29863 "%f collisions\n", (long) decl_specializations->size (),
29864 (long) decl_specializations->elements (),
29865 decl_specializations->collisions ());
29866 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29867 "%f collisions\n", (long) type_specializations->size (),
29868 (long) type_specializations->elements (),
29869 type_specializations->collisions ());
29870 }
29871
29872 #if CHECKING_P
29873
29874 namespace selftest {
29875
29876 /* Verify that build_non_dependent_expr () works, for various expressions,
29877 and that location wrappers don't affect the results. */
29878
29879 static void
29880 test_build_non_dependent_expr ()
29881 {
29882 location_t loc = BUILTINS_LOCATION;
29883
29884 /* Verify constants, without and with location wrappers. */
29885 tree int_cst = build_int_cst (integer_type_node, 42);
29886 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29887
29888 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29889 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29890 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29891
29892 tree string_lit = build_string (4, "foo");
29893 TREE_TYPE (string_lit) = char_array_type_node;
29894 string_lit = fix_string_type (string_lit);
29895 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29896
29897 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29898 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29899 ASSERT_EQ (wrapped_string_lit,
29900 build_non_dependent_expr (wrapped_string_lit));
29901 }
29902
29903 /* Verify that type_dependent_expression_p () works correctly, even
29904 in the presence of location wrapper nodes. */
29905
29906 static void
29907 test_type_dependent_expression_p ()
29908 {
29909 location_t loc = BUILTINS_LOCATION;
29910
29911 tree name = get_identifier ("foo");
29912
29913 /* If no templates are involved, nothing is type-dependent. */
29914 gcc_assert (!processing_template_decl);
29915 ASSERT_FALSE (type_dependent_expression_p (name));
29916
29917 ++processing_template_decl;
29918
29919 /* Within a template, an unresolved name is always type-dependent. */
29920 ASSERT_TRUE (type_dependent_expression_p (name));
29921
29922 /* Ensure it copes with NULL_TREE and errors. */
29923 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29924 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29925
29926 /* A USING_DECL in a template should be type-dependent, even if wrapped
29927 with a location wrapper (PR c++/83799). */
29928 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29929 TREE_TYPE (using_decl) = integer_type_node;
29930 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29931 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29932 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29933 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29934
29935 --processing_template_decl;
29936 }
29937
29938 /* Run all of the selftests within this file. */
29939
29940 void
29941 cp_pt_c_tests ()
29942 {
29943 test_build_non_dependent_expr ();
29944 test_type_dependent_expression_p ();
29945 }
29946
29947 } // namespace selftest
29948
29949 #endif /* #if CHECKING_P */
29950
29951 #include "gt-cp-pt.h"