957140115e49e0a1606c6d8e01120de5930791be
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win".
26
27 Fixed by: C++20 modules. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "cp-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "intl.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "toplev.h"
42 #include "tree-iterator.h"
43 #include "type-utils.h"
44 #include "gimplify.h"
45 #include "gcc-rich-location.h"
46 #include "selftest.h"
47 #include "target.h"
48
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
52
53 /* The PENDING_TEMPLATES is a list of templates whose instantiations
54 have been deferred, either because their definitions were not yet
55 available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
57 {
58 struct pending_template *next;
59 struct tinst_level *tinst;
60 };
61
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
64
65 int processing_template_parmlist;
66 static int template_header_count;
67
68 static GTY(()) tree saved_trees;
69 static vec<int> inline_parm_levels;
70
71 static GTY(()) struct tinst_level *current_tinst_level;
72
73 static GTY(()) vec<tree, va_gc> *saved_access_scope;
74
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
79
80 // -------------------------------------------------------------------------- //
81 // Local Specialization Stack
82 //
83 // Implementation of the RAII helper for creating new local
84 // specializations.
85 local_specialization_stack::local_specialization_stack (lss_policy policy)
86 : saved (local_specializations)
87 {
88 if (policy == lss_nop)
89 ;
90 else if (policy == lss_blank || !saved)
91 local_specializations = new hash_map<tree, tree>;
92 else
93 local_specializations = new hash_map<tree, tree>(*saved);
94 }
95
96 local_specialization_stack::~local_specialization_stack ()
97 {
98 if (local_specializations != saved)
99 {
100 delete local_specializations;
101 local_specializations = saved;
102 }
103 }
104
105 /* True if we've recursed into fn_type_unification too many times. */
106 static bool excessive_deduction_depth;
107
108 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 {
110 static hashval_t hash (spec_entry *);
111 static bool equal (spec_entry *, spec_entry *);
112 };
113
114 /* The general template is not in these tables. */
115 typedef hash_table<spec_hasher> spec_hash_table;
116 static GTY (()) spec_hash_table *decl_specializations;
117 static GTY (()) spec_hash_table *type_specializations;
118
119 /* Contains canonical template parameter types. The vector is indexed by
120 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
121 TREE_LIST, whose TREE_VALUEs contain the canonical template
122 parameters of various types and levels. */
123 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
124
125 #define UNIFY_ALLOW_NONE 0
126 #define UNIFY_ALLOW_MORE_CV_QUAL 1
127 #define UNIFY_ALLOW_LESS_CV_QUAL 2
128 #define UNIFY_ALLOW_DERIVED 4
129 #define UNIFY_ALLOW_INTEGER 8
130 #define UNIFY_ALLOW_OUTER_LEVEL 16
131 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
132 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133
134 enum template_base_result {
135 tbr_incomplete_type,
136 tbr_ambiguous_baseclass,
137 tbr_success
138 };
139
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int check_non_deducible_conversion (tree, tree, int, int,
158 struct conversion **, bool);
159 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
160 tree);
161 static int type_unification_real (tree, tree, tree, const tree *,
162 unsigned int, int, unification_kind_t,
163 vec<deferred_access_check, va_gc> **,
164 bool);
165 static void note_template_header (int);
166 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
167 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
168 static tree convert_template_argument (tree, tree, tree,
169 tsubst_flags_t, int, tree);
170 static tree for_each_template_parm (tree, tree_fn_t, void*,
171 hash_set<tree> *, bool, tree_fn_t = NULL);
172 static tree expand_template_argument_pack (tree);
173 static tree build_template_parm_index (int, int, int, tree, tree);
174 static bool inline_needs_template_parms (tree, bool);
175 static void push_inline_template_parms_recursive (tree, int);
176 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
177 static int mark_template_parm (tree, void *);
178 static int template_parm_this_level_p (tree, void *);
179 static tree tsubst_friend_function (tree, tree);
180 static tree tsubst_friend_class (tree, tree);
181 static int can_complete_type_without_circularity (tree);
182 static tree get_bindings (tree, tree, tree, bool);
183 static int template_decl_level (tree);
184 static int check_cv_quals_for_unify (int, tree, tree);
185 static int unify_pack_expansion (tree, tree, tree,
186 tree, unification_kind_t, bool, bool);
187 static tree copy_template_args (tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static bool class_nttp_const_wrapper_p (tree t);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_instantiation_time_access_checks (tree, tree);
214 static tree listify (tree);
215 static tree listify_autos (tree, tree);
216 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
217 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
218 static bool complex_alias_template_p (const_tree tmpl);
219 static tree get_underlying_template (tree);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
224 static tree enclosing_instantiation_of (tree tctx);
225 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
226
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
231
232 void
233 push_access_scope (tree t)
234 {
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
237
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
244
245 if (TREE_CODE (t) == FUNCTION_DECL)
246 {
247 vec_safe_push (saved_access_scope, current_function_decl);
248 current_function_decl = t;
249 }
250 }
251
252 /* Restore the scope set up by push_access_scope. T is the node we
253 are processing. */
254
255 void
256 pop_access_scope (tree t)
257 {
258 if (TREE_CODE (t) == FUNCTION_DECL)
259 current_function_decl = saved_access_scope->pop();
260
261 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
262 pop_nested_class ();
263 else
264 pop_from_top_level ();
265 }
266
267 /* Do any processing required when DECL (a member template
268 declaration) is finished. Returns the TEMPLATE_DECL corresponding
269 to DECL, unless it is a specialization, in which case the DECL
270 itself is returned. */
271
272 tree
273 finish_member_template_decl (tree decl)
274 {
275 if (decl == error_mark_node)
276 return error_mark_node;
277
278 gcc_assert (DECL_P (decl));
279
280 if (TREE_CODE (decl) == TYPE_DECL)
281 {
282 tree type;
283
284 type = TREE_TYPE (decl);
285 if (type == error_mark_node)
286 return error_mark_node;
287 if (MAYBE_CLASS_TYPE_P (type)
288 && CLASSTYPE_TEMPLATE_INFO (type)
289 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
290 {
291 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
292 check_member_template (tmpl);
293 return tmpl;
294 }
295 return NULL_TREE;
296 }
297 else if (TREE_CODE (decl) == FIELD_DECL)
298 error_at (DECL_SOURCE_LOCATION (decl),
299 "data member %qD cannot be a member template", decl);
300 else if (DECL_TEMPLATE_INFO (decl))
301 {
302 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
303 {
304 check_member_template (DECL_TI_TEMPLATE (decl));
305 return DECL_TI_TEMPLATE (decl);
306 }
307 else
308 return decl;
309 }
310 else
311 error_at (DECL_SOURCE_LOCATION (decl),
312 "invalid member template declaration %qD", decl);
313
314 return error_mark_node;
315 }
316
317 /* Create a template info node. */
318
319 tree
320 build_template_info (tree template_decl, tree template_args)
321 {
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
326 }
327
328 /* Return the template info node corresponding to T, whatever T is. */
329
330 tree
331 get_template_info (const_tree t)
332 {
333 tree tinfo = NULL_TREE;
334
335 if (!t || t == error_mark_node)
336 return NULL;
337
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
341
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
344
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
347
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
352
353 return tinfo;
354 }
355
356 /* Returns the template nesting level of the indicated class TYPE.
357
358 For example, in:
359 template <class T>
360 struct A
361 {
362 template <class U>
363 struct B {};
364 };
365
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
369
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
373
374 int
375 template_class_depth (tree type)
376 {
377 int depth;
378
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
380 {
381 tree tinfo = get_template_info (type);
382
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
386
387 if (DECL_P (type))
388 {
389 if (tree fctx = DECL_FRIEND_CONTEXT (type))
390 type = fctx;
391 else
392 type = CP_DECL_CONTEXT (type);
393 }
394 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
406 static bool
407 instantiates_primary_template_p (tree node)
408 {
409 tree tinfo = get_template_info (node);
410 if (!tinfo)
411 return false;
412
413 tree tmpl = TI_TEMPLATE (tinfo);
414 if (PRIMARY_TEMPLATE_P (tmpl))
415 return true;
416
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
418 return false;
419
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
423
424 tree ctxt = DECL_CONTEXT (tmpl);
425 tree ctinfo = get_template_info (ctxt);
426 if (!ctinfo)
427 return true;
428
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
431 }
432
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
435
436 static bool
437 inline_needs_template_parms (tree decl, bool nsdmi)
438 {
439 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
440 return false;
441
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
443 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
444 }
445
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
449 innermost first. */
450
451 static void
452 push_inline_template_parms_recursive (tree parmlist, int levels)
453 {
454 tree parms = TREE_VALUE (parmlist);
455 int i;
456
457 if (levels > 1)
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
459
460 ++processing_template_decl;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl),
463 parms, current_template_parms);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
465
466 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
467 NULL);
468 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
469 {
470 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
471
472 if (error_operand_p (parm))
473 continue;
474
475 gcc_assert (DECL_P (parm));
476
477 switch (TREE_CODE (parm))
478 {
479 case TYPE_DECL:
480 case TEMPLATE_DECL:
481 pushdecl (parm);
482 break;
483
484 case PARM_DECL:
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
487 break;
488
489 default:
490 gcc_unreachable ();
491 }
492 }
493 }
494
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
498
499 void
500 maybe_begin_member_template_processing (tree decl)
501 {
502 tree parms;
503 int levels = 0;
504 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
505
506 if (nsdmi)
507 {
508 tree ctx = DECL_CONTEXT (decl);
509 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx)
512 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
513 }
514
515 if (inline_needs_template_parms (decl, nsdmi))
516 {
517 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
518 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
519
520 if (DECL_TEMPLATE_SPECIALIZATION (decl))
521 {
522 --levels;
523 parms = TREE_CHAIN (parms);
524 }
525
526 push_inline_template_parms_recursive (parms, levels);
527 }
528
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels.safe_push (levels);
532 }
533
534 /* Undo the effects of maybe_begin_member_template_processing. */
535
536 void
537 maybe_end_member_template_processing (void)
538 {
539 int i;
540 int last;
541
542 if (inline_parm_levels.length () == 0)
543 return;
544
545 last = inline_parm_levels.pop ();
546 for (i = 0; i < last; ++i)
547 {
548 --processing_template_decl;
549 current_template_parms = TREE_CHAIN (current_template_parms);
550 poplevel (0, 0, 0);
551 }
552 }
553
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
556
557 static tree
558 add_to_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561 int extra_depth;
562 int i;
563 int j;
564
565 if (args == NULL_TREE || extra_args == error_mark_node)
566 return extra_args;
567
568 extra_depth = TMPL_ARGS_DEPTH (extra_args);
569 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
570
571 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
573
574 for (j = 1; j <= extra_depth; ++j, ++i)
575 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
576
577 return new_args;
578 }
579
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation.
586
587 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
588
589 tree
590 add_outermost_template_args (tree args, tree extra_args)
591 {
592 tree new_args;
593
594 if (!args)
595 return extra_args;
596 if (TREE_CODE (args) == TEMPLATE_DECL)
597 {
598 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
599 args = TI_ARGS (ti);
600 }
601
602 /* If there are more levels of EXTRA_ARGS than there are ARGS,
603 something very fishy is going on. */
604 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
605
606 /* If *all* the new arguments will be the EXTRA_ARGS, just return
607 them. */
608 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
609 return extra_args;
610
611 /* For the moment, we make ARGS look like it contains fewer levels. */
612 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
613
614 new_args = add_to_template_args (args, extra_args);
615
616 /* Now, we restore ARGS to its full dimensions. */
617 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
618
619 return new_args;
620 }
621
622 /* Return the N levels of innermost template arguments from the ARGS. */
623
624 tree
625 get_innermost_template_args (tree args, int n)
626 {
627 tree new_args;
628 int extra_levels;
629 int i;
630
631 gcc_assert (n >= 0);
632
633 /* If N is 1, just return the innermost set of template arguments. */
634 if (n == 1)
635 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
636
637 /* If we're not removing anything, just return the arguments we were
638 given. */
639 extra_levels = TMPL_ARGS_DEPTH (args) - n;
640 gcc_assert (extra_levels >= 0);
641 if (extra_levels == 0)
642 return args;
643
644 /* Make a new set of arguments, not containing the outer arguments. */
645 new_args = make_tree_vec (n);
646 for (i = 1; i <= n; ++i)
647 SET_TMPL_ARGS_LEVEL (new_args, i,
648 TMPL_ARGS_LEVEL (args, i + extra_levels));
649
650 return new_args;
651 }
652
653 /* The inverse of get_innermost_template_args: Return all but the innermost
654 EXTRA_LEVELS levels of template arguments from the ARGS. */
655
656 static tree
657 strip_innermost_template_args (tree args, int extra_levels)
658 {
659 tree new_args;
660 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
661 int i;
662
663 gcc_assert (n >= 0);
664
665 /* If N is 1, just return the outermost set of template arguments. */
666 if (n == 1)
667 return TMPL_ARGS_LEVEL (args, 1);
668
669 /* If we're not removing anything, just return the arguments we were
670 given. */
671 gcc_assert (extra_levels >= 0);
672 if (extra_levels == 0)
673 return args;
674
675 /* Make a new set of arguments, not containing the inner arguments. */
676 new_args = make_tree_vec (n);
677 for (i = 1; i <= n; ++i)
678 SET_TMPL_ARGS_LEVEL (new_args, i,
679 TMPL_ARGS_LEVEL (args, i));
680
681 return new_args;
682 }
683
684 /* We've got a template header coming up; push to a new level for storing
685 the parms. */
686
687 void
688 begin_template_parm_list (void)
689 {
690 /* We use a non-tag-transparent scope here, which causes pushtag to
691 put tags in this scope, rather than in the enclosing class or
692 namespace scope. This is the right thing, since we want
693 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
694 global template class, push_template_decl handles putting the
695 TEMPLATE_DECL into top-level scope. For a nested template class,
696 e.g.:
697
698 template <class T> struct S1 {
699 template <class T> struct S2 {};
700 };
701
702 pushtag contains special code to insert the TEMPLATE_DECL for S2
703 at the right scope. */
704 begin_scope (sk_template_parms, NULL);
705 ++processing_template_decl;
706 ++processing_template_parmlist;
707 note_template_header (0);
708
709 /* Add a dummy parameter level while we process the parameter list. */
710 current_template_parms
711 = tree_cons (size_int (processing_template_decl),
712 make_tree_vec (0),
713 current_template_parms);
714 }
715
716 /* This routine is called when a specialization is declared. If it is
717 invalid to declare a specialization here, an error is reported and
718 false is returned, otherwise this routine will return true. */
719
720 static bool
721 check_specialization_scope (void)
722 {
723 tree scope = current_scope ();
724
725 /* [temp.expl.spec]
726
727 An explicit specialization shall be declared in the namespace of
728 which the template is a member, or, for member templates, in the
729 namespace of which the enclosing class or enclosing class
730 template is a member. An explicit specialization of a member
731 function, member class or static data member of a class template
732 shall be declared in the namespace of which the class template
733 is a member. */
734 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
735 {
736 error ("explicit specialization in non-namespace scope %qD", scope);
737 return false;
738 }
739
740 /* [temp.expl.spec]
741
742 In an explicit specialization declaration for a member of a class
743 template or a member template that appears in namespace scope,
744 the member template and some of its enclosing class templates may
745 remain unspecialized, except that the declaration shall not
746 explicitly specialize a class member template if its enclosing
747 class templates are not explicitly specialized as well. */
748 if (current_template_parms)
749 {
750 error ("enclosing class templates are not explicitly specialized");
751 return false;
752 }
753
754 return true;
755 }
756
757 /* We've just seen template <>. */
758
759 bool
760 begin_specialization (void)
761 {
762 begin_scope (sk_template_spec, NULL);
763 note_template_header (1);
764 return check_specialization_scope ();
765 }
766
767 /* Called at then end of processing a declaration preceded by
768 template<>. */
769
770 void
771 end_specialization (void)
772 {
773 finish_scope ();
774 reset_specialization ();
775 }
776
777 /* Any template <>'s that we have seen thus far are not referring to a
778 function specialization. */
779
780 void
781 reset_specialization (void)
782 {
783 processing_specialization = 0;
784 template_header_count = 0;
785 }
786
787 /* We've just seen a template header. If SPECIALIZATION is nonzero,
788 it was of the form template <>. */
789
790 static void
791 note_template_header (int specialization)
792 {
793 processing_specialization = specialization;
794 template_header_count++;
795 }
796
797 /* We're beginning an explicit instantiation. */
798
799 void
800 begin_explicit_instantiation (void)
801 {
802 gcc_assert (!processing_explicit_instantiation);
803 processing_explicit_instantiation = true;
804 }
805
806
807 void
808 end_explicit_instantiation (void)
809 {
810 gcc_assert (processing_explicit_instantiation);
811 processing_explicit_instantiation = false;
812 }
813
814 /* An explicit specialization or partial specialization of TMPL is being
815 declared. Check that the namespace in which the specialization is
816 occurring is permissible. Returns false iff it is invalid to
817 specialize TMPL in the current namespace. */
818
819 static bool
820 check_specialization_namespace (tree tmpl)
821 {
822 tree tpl_ns = decl_namespace_context (tmpl);
823
824 /* [tmpl.expl.spec]
825
826 An explicit specialization shall be declared in a namespace enclosing the
827 specialized template. An explicit specialization whose declarator-id is
828 not qualified shall be declared in the nearest enclosing namespace of the
829 template, or, if the namespace is inline (7.3.1), any namespace from its
830 enclosing namespace set. */
831 if (current_scope() != DECL_CONTEXT (tmpl)
832 && !at_namespace_scope_p ())
833 {
834 error ("specialization of %qD must appear at namespace scope", tmpl);
835 return false;
836 }
837
838 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
839 /* Same or enclosing namespace. */
840 return true;
841 else
842 {
843 auto_diagnostic_group d;
844 if (permerror (input_location,
845 "specialization of %qD in different namespace", tmpl))
846 inform (DECL_SOURCE_LOCATION (tmpl),
847 " from definition of %q#D", tmpl);
848 return false;
849 }
850 }
851
852 /* SPEC is an explicit instantiation. Check that it is valid to
853 perform this explicit instantiation in the current namespace. */
854
855 static void
856 check_explicit_instantiation_namespace (tree spec)
857 {
858 tree ns;
859
860 /* DR 275: An explicit instantiation shall appear in an enclosing
861 namespace of its template. */
862 ns = decl_namespace_context (spec);
863 if (!is_nested_namespace (current_namespace, ns))
864 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
865 "(which does not enclose namespace %qD)",
866 spec, current_namespace, ns);
867 }
868
869 /* Returns the type of a template specialization only if that
870 specialization needs to be defined. Otherwise (e.g., if the type has
871 already been defined), the function returns NULL_TREE. */
872
873 static tree
874 maybe_new_partial_specialization (tree type)
875 {
876 /* An implicit instantiation of an incomplete type implies
877 the definition of a new class template.
878
879 template<typename T>
880 struct S;
881
882 template<typename T>
883 struct S<T*>;
884
885 Here, S<T*> is an implicit instantiation of S whose type
886 is incomplete. */
887 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
888 return type;
889
890 /* It can also be the case that TYPE is a completed specialization.
891 Continuing the previous example, suppose we also declare:
892
893 template<typename T>
894 requires Integral<T>
895 struct S<T*>;
896
897 Here, S<T*> refers to the specialization S<T*> defined
898 above. However, we need to differentiate definitions because
899 we intend to define a new partial specialization. In this case,
900 we rely on the fact that the constraints are different for
901 this declaration than that above.
902
903 Note that we also get here for injected class names and
904 late-parsed template definitions. We must ensure that we
905 do not create new type declarations for those cases. */
906 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
907 {
908 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
909 tree args = CLASSTYPE_TI_ARGS (type);
910
911 /* If there are no template parameters, this cannot be a new
912 partial template specialization? */
913 if (!current_template_parms)
914 return NULL_TREE;
915
916 /* The injected-class-name is not a new partial specialization. */
917 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
918 return NULL_TREE;
919
920 /* If the constraints are not the same as those of the primary
921 then, we can probably create a new specialization. */
922 tree type_constr = current_template_constraints ();
923
924 if (type == TREE_TYPE (tmpl))
925 {
926 tree main_constr = get_constraints (tmpl);
927 if (equivalent_constraints (type_constr, main_constr))
928 return NULL_TREE;
929 }
930
931 /* Also, if there's a pre-existing specialization with matching
932 constraints, then this also isn't new. */
933 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
934 while (specs)
935 {
936 tree spec_tmpl = TREE_VALUE (specs);
937 tree spec_args = TREE_PURPOSE (specs);
938 tree spec_constr = get_constraints (spec_tmpl);
939 if (comp_template_args (args, spec_args)
940 && equivalent_constraints (type_constr, spec_constr))
941 return NULL_TREE;
942 specs = TREE_CHAIN (specs);
943 }
944
945 /* Create a new type node (and corresponding type decl)
946 for the newly declared specialization. */
947 tree t = make_class_type (TREE_CODE (type));
948 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
949 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
950
951 /* We only need a separate type node for storing the definition of this
952 partial specialization; uses of S<T*> are unconstrained, so all are
953 equivalent. So keep TYPE_CANONICAL the same. */
954 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
955
956 /* Build the corresponding type decl. */
957 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
958 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
959 DECL_SOURCE_LOCATION (d) = input_location;
960 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
961 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
962
963 set_instantiating_module (d);
964 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
965
966 return t;
967 }
968
969 return NULL_TREE;
970 }
971
972 /* The TYPE is being declared. If it is a template type, that means it
973 is a partial specialization. Do appropriate error-checking. */
974
975 tree
976 maybe_process_partial_specialization (tree type)
977 {
978 tree context;
979
980 if (type == error_mark_node)
981 return error_mark_node;
982
983 /* A lambda that appears in specialization context is not itself a
984 specialization. */
985 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
986 return type;
987
988 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
989 {
990 error ("name of class shadows template template parameter %qD",
991 TYPE_NAME (type));
992 return error_mark_node;
993 }
994
995 context = TYPE_CONTEXT (type);
996
997 if (TYPE_ALIAS_P (type))
998 {
999 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1000
1001 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1002 error ("specialization of alias template %qD",
1003 TI_TEMPLATE (tinfo));
1004 else
1005 error ("explicit specialization of non-template %qT", type);
1006 return error_mark_node;
1007 }
1008 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1009 {
1010 /* This is for ordinary explicit specialization and partial
1011 specialization of a template class such as:
1012
1013 template <> class C<int>;
1014
1015 or:
1016
1017 template <class T> class C<T*>;
1018
1019 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1020
1021 if (tree t = maybe_new_partial_specialization (type))
1022 {
1023 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1024 && !at_namespace_scope_p ())
1025 return error_mark_node;
1026 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1027 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1028 if (processing_template_decl)
1029 {
1030 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1031 if (decl == error_mark_node)
1032 return error_mark_node;
1033 return TREE_TYPE (decl);
1034 }
1035 }
1036 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1037 error ("specialization of %qT after instantiation", type);
1038 else if (errorcount && !processing_specialization
1039 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1040 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1041 /* Trying to define a specialization either without a template<> header
1042 or in an inappropriate place. We've already given an error, so just
1043 bail now so we don't actually define the specialization. */
1044 return error_mark_node;
1045 }
1046 else if (CLASS_TYPE_P (type)
1047 && !CLASSTYPE_USE_TEMPLATE (type)
1048 && CLASSTYPE_TEMPLATE_INFO (type)
1049 && context && CLASS_TYPE_P (context)
1050 && CLASSTYPE_TEMPLATE_INFO (context))
1051 {
1052 /* This is for an explicit specialization of member class
1053 template according to [temp.expl.spec/18]:
1054
1055 template <> template <class U> class C<int>::D;
1056
1057 The context `C<int>' must be an implicit instantiation.
1058 Otherwise this is just a member class template declared
1059 earlier like:
1060
1061 template <> class C<int> { template <class U> class D; };
1062 template <> template <class U> class C<int>::D;
1063
1064 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1065 while in the second case, `C<int>::D' is a primary template
1066 and `C<T>::D' may not exist. */
1067
1068 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1069 && !COMPLETE_TYPE_P (type))
1070 {
1071 tree t;
1072 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1073
1074 if (current_namespace
1075 != decl_namespace_context (tmpl))
1076 {
1077 if (permerror (input_location,
1078 "specialization of %qD in different namespace",
1079 type))
1080 inform (DECL_SOURCE_LOCATION (tmpl),
1081 "from definition of %q#D", tmpl);
1082 }
1083
1084 /* Check for invalid specialization after instantiation:
1085
1086 template <> template <> class C<int>::D<int>;
1087 template <> template <class U> class C<int>::D; */
1088
1089 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1090 t; t = TREE_CHAIN (t))
1091 {
1092 tree inst = TREE_VALUE (t);
1093 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1094 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1095 {
1096 /* We already have a full specialization of this partial
1097 instantiation, or a full specialization has been
1098 looked up but not instantiated. Reassign it to the
1099 new member specialization template. */
1100 spec_entry elt;
1101 spec_entry *entry;
1102
1103 elt.tmpl = most_general_template (tmpl);
1104 elt.args = CLASSTYPE_TI_ARGS (inst);
1105 elt.spec = inst;
1106
1107 type_specializations->remove_elt (&elt);
1108
1109 elt.tmpl = tmpl;
1110 CLASSTYPE_TI_ARGS (inst)
1111 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1112
1113 spec_entry **slot
1114 = type_specializations->find_slot (&elt, INSERT);
1115 entry = ggc_alloc<spec_entry> ();
1116 *entry = elt;
1117 *slot = entry;
1118 }
1119 else
1120 /* But if we've had an implicit instantiation, that's a
1121 problem ([temp.expl.spec]/6). */
1122 error ("specialization %qT after instantiation %qT",
1123 type, inst);
1124 }
1125
1126 /* Mark TYPE as a specialization. And as a result, we only
1127 have one level of template argument for the innermost
1128 class template. */
1129 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1130 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1131 CLASSTYPE_TI_ARGS (type)
1132 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1133 }
1134 }
1135 else if (processing_specialization)
1136 {
1137 /* Someday C++0x may allow for enum template specialization. */
1138 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1139 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1140 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1141 "of %qD not allowed by ISO C++", type);
1142 else
1143 {
1144 error ("explicit specialization of non-template %qT", type);
1145 return error_mark_node;
1146 }
1147 }
1148
1149 return type;
1150 }
1151
1152 /* Returns nonzero if we can optimize the retrieval of specializations
1153 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1154 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1155
1156 static inline bool
1157 optimize_specialization_lookup_p (tree tmpl)
1158 {
1159 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1160 && DECL_CLASS_SCOPE_P (tmpl)
1161 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1162 parameter. */
1163 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1164 /* The optimized lookup depends on the fact that the
1165 template arguments for the member function template apply
1166 purely to the containing class, which is not true if the
1167 containing class is an explicit or partial
1168 specialization. */
1169 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1170 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1171 && !DECL_CONV_FN_P (tmpl)
1172 /* It is possible to have a template that is not a member
1173 template and is not a member of a template class:
1174
1175 template <typename T>
1176 struct S { friend A::f(); };
1177
1178 Here, the friend function is a template, but the context does
1179 not have template information. The optimized lookup relies
1180 on having ARGS be the template arguments for both the class
1181 and the function template. */
1182 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1183 }
1184
1185 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1186 gone through coerce_template_parms by now. */
1187
1188 static void
1189 verify_unstripped_args_1 (tree inner)
1190 {
1191 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1192 {
1193 tree arg = TREE_VEC_ELT (inner, i);
1194 if (TREE_CODE (arg) == TEMPLATE_DECL)
1195 /* OK */;
1196 else if (TYPE_P (arg))
1197 gcc_assert (strip_typedefs (arg, NULL) == arg);
1198 else if (ARGUMENT_PACK_P (arg))
1199 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1200 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1201 /* Allow typedefs on the type of a non-type argument, since a
1202 parameter can have them. */;
1203 else
1204 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1205 }
1206 }
1207
1208 static void
1209 verify_unstripped_args (tree args)
1210 {
1211 ++processing_template_decl;
1212 if (!any_dependent_template_arguments_p (args))
1213 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1214 --processing_template_decl;
1215 }
1216
1217 /* Retrieve the specialization (in the sense of [temp.spec] - a
1218 specialization is either an instantiation or an explicit
1219 specialization) of TMPL for the given template ARGS. If there is
1220 no such specialization, return NULL_TREE. The ARGS are a vector of
1221 arguments, or a vector of vectors of arguments, in the case of
1222 templates with more than one level of parameters.
1223
1224 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1225 then we search for a partial specialization matching ARGS. This
1226 parameter is ignored if TMPL is not a class template.
1227
1228 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1229 result is a NONTYPE_ARGUMENT_PACK. */
1230
1231 static tree
1232 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1233 {
1234 if (tmpl == NULL_TREE)
1235 return NULL_TREE;
1236
1237 if (args == error_mark_node)
1238 return NULL_TREE;
1239
1240 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1241 || TREE_CODE (tmpl) == FIELD_DECL);
1242
1243 /* There should be as many levels of arguments as there are
1244 levels of parameters. */
1245 gcc_assert (TMPL_ARGS_DEPTH (args)
1246 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1247 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1248 : template_class_depth (DECL_CONTEXT (tmpl))));
1249
1250 if (flag_checking)
1251 verify_unstripped_args (args);
1252
1253 /* Lambda functions in templates aren't instantiated normally, but through
1254 tsubst_lambda_expr. */
1255 if (lambda_fn_in_template_p (tmpl))
1256 return NULL_TREE;
1257
1258 if (optimize_specialization_lookup_p (tmpl))
1259 {
1260 /* The template arguments actually apply to the containing
1261 class. Find the class specialization with those
1262 arguments. */
1263 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1264 tree class_specialization
1265 = retrieve_specialization (class_template, args, 0);
1266 if (!class_specialization)
1267 return NULL_TREE;
1268
1269 /* Find the instance of TMPL. */
1270 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1271 for (ovl_iterator iter (fns); iter; ++iter)
1272 {
1273 tree fn = *iter;
1274 if (tree ti = get_template_info (fn))
1275 if (TI_TEMPLATE (ti) == tmpl
1276 /* using-declarations can bring in a different
1277 instantiation of tmpl as a member of a different
1278 instantiation of tmpl's class. We don't want those
1279 here. */
1280 && DECL_CONTEXT (fn) == class_specialization)
1281 return fn;
1282 }
1283 return NULL_TREE;
1284 }
1285 else
1286 {
1287 spec_entry *found;
1288 spec_entry elt;
1289 spec_hash_table *specializations;
1290
1291 elt.tmpl = tmpl;
1292 elt.args = args;
1293 elt.spec = NULL_TREE;
1294
1295 if (DECL_CLASS_TEMPLATE_P (tmpl))
1296 specializations = type_specializations;
1297 else
1298 specializations = decl_specializations;
1299
1300 if (hash == 0)
1301 hash = spec_hasher::hash (&elt);
1302 found = specializations->find_with_hash (&elt, hash);
1303 if (found)
1304 return found->spec;
1305 }
1306
1307 return NULL_TREE;
1308 }
1309
1310 /* Like retrieve_specialization, but for local declarations. */
1311
1312 tree
1313 retrieve_local_specialization (tree tmpl)
1314 {
1315 if (local_specializations == NULL)
1316 return NULL_TREE;
1317
1318 tree *slot = local_specializations->get (tmpl);
1319 return slot ? *slot : NULL_TREE;
1320 }
1321
1322 /* Returns nonzero iff DECL is a specialization of TMPL. */
1323
1324 int
1325 is_specialization_of (tree decl, tree tmpl)
1326 {
1327 tree t;
1328
1329 if (TREE_CODE (decl) == FUNCTION_DECL)
1330 {
1331 for (t = decl;
1332 t != NULL_TREE;
1333 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1334 if (t == tmpl)
1335 return 1;
1336 }
1337 else
1338 {
1339 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1340
1341 for (t = TREE_TYPE (decl);
1342 t != NULL_TREE;
1343 t = CLASSTYPE_USE_TEMPLATE (t)
1344 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1345 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1346 return 1;
1347 }
1348
1349 return 0;
1350 }
1351
1352 /* Returns nonzero iff DECL is a specialization of friend declaration
1353 FRIEND_DECL according to [temp.friend]. */
1354
1355 bool
1356 is_specialization_of_friend (tree decl, tree friend_decl)
1357 {
1358 bool need_template = true;
1359 int template_depth;
1360
1361 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1362 || TREE_CODE (decl) == TYPE_DECL);
1363
1364 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1365 of a template class, we want to check if DECL is a specialization
1366 if this. */
1367 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1368 && DECL_TEMPLATE_INFO (friend_decl)
1369 && !DECL_USE_TEMPLATE (friend_decl))
1370 {
1371 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1372 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1373 need_template = false;
1374 }
1375 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1376 && !PRIMARY_TEMPLATE_P (friend_decl))
1377 need_template = false;
1378
1379 /* There is nothing to do if this is not a template friend. */
1380 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1381 return false;
1382
1383 if (is_specialization_of (decl, friend_decl))
1384 return true;
1385
1386 /* [temp.friend/6]
1387 A member of a class template may be declared to be a friend of a
1388 non-template class. In this case, the corresponding member of
1389 every specialization of the class template is a friend of the
1390 class granting friendship.
1391
1392 For example, given a template friend declaration
1393
1394 template <class T> friend void A<T>::f();
1395
1396 the member function below is considered a friend
1397
1398 template <> struct A<int> {
1399 void f();
1400 };
1401
1402 For this type of template friend, TEMPLATE_DEPTH below will be
1403 nonzero. To determine if DECL is a friend of FRIEND, we first
1404 check if the enclosing class is a specialization of another. */
1405
1406 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1407 if (template_depth
1408 && DECL_CLASS_SCOPE_P (decl)
1409 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1410 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1411 {
1412 /* Next, we check the members themselves. In order to handle
1413 a few tricky cases, such as when FRIEND_DECL's are
1414
1415 template <class T> friend void A<T>::g(T t);
1416 template <class T> template <T t> friend void A<T>::h();
1417
1418 and DECL's are
1419
1420 void A<int>::g(int);
1421 template <int> void A<int>::h();
1422
1423 we need to figure out ARGS, the template arguments from
1424 the context of DECL. This is required for template substitution
1425 of `T' in the function parameter of `g' and template parameter
1426 of `h' in the above examples. Here ARGS corresponds to `int'. */
1427
1428 tree context = DECL_CONTEXT (decl);
1429 tree args = NULL_TREE;
1430 int current_depth = 0;
1431
1432 while (current_depth < template_depth)
1433 {
1434 if (CLASSTYPE_TEMPLATE_INFO (context))
1435 {
1436 if (current_depth == 0)
1437 args = TYPE_TI_ARGS (context);
1438 else
1439 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1440 current_depth++;
1441 }
1442 context = TYPE_CONTEXT (context);
1443 }
1444
1445 if (TREE_CODE (decl) == FUNCTION_DECL)
1446 {
1447 bool is_template;
1448 tree friend_type;
1449 tree decl_type;
1450 tree friend_args_type;
1451 tree decl_args_type;
1452
1453 /* Make sure that both DECL and FRIEND_DECL are templates or
1454 non-templates. */
1455 is_template = DECL_TEMPLATE_INFO (decl)
1456 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1457 if (need_template ^ is_template)
1458 return false;
1459 else if (is_template)
1460 {
1461 /* If both are templates, check template parameter list. */
1462 tree friend_parms
1463 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1464 args, tf_none);
1465 if (!comp_template_parms
1466 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1467 friend_parms))
1468 return false;
1469
1470 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1471 }
1472 else
1473 decl_type = TREE_TYPE (decl);
1474
1475 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1476 tf_none, NULL_TREE);
1477 if (friend_type == error_mark_node)
1478 return false;
1479
1480 /* Check if return types match. */
1481 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1482 return false;
1483
1484 /* Check if function parameter types match, ignoring the
1485 `this' parameter. */
1486 friend_args_type = TYPE_ARG_TYPES (friend_type);
1487 decl_args_type = TYPE_ARG_TYPES (decl_type);
1488 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1489 friend_args_type = TREE_CHAIN (friend_args_type);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1491 decl_args_type = TREE_CHAIN (decl_args_type);
1492
1493 return compparms (decl_args_type, friend_args_type);
1494 }
1495 else
1496 {
1497 /* DECL is a TYPE_DECL */
1498 bool is_template;
1499 tree decl_type = TREE_TYPE (decl);
1500
1501 /* Make sure that both DECL and FRIEND_DECL are templates or
1502 non-templates. */
1503 is_template
1504 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1505 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1506
1507 if (need_template ^ is_template)
1508 return false;
1509 else if (is_template)
1510 {
1511 tree friend_parms;
1512 /* If both are templates, check the name of the two
1513 TEMPLATE_DECL's first because is_friend didn't. */
1514 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1515 != DECL_NAME (friend_decl))
1516 return false;
1517
1518 /* Now check template parameter list. */
1519 friend_parms
1520 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1521 args, tf_none);
1522 return comp_template_parms
1523 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1524 friend_parms);
1525 }
1526 else
1527 return (DECL_NAME (decl)
1528 == DECL_NAME (friend_decl));
1529 }
1530 }
1531 return false;
1532 }
1533
1534 /* Register the specialization SPEC as a specialization of TMPL with
1535 the indicated ARGS. IS_FRIEND indicates whether the specialization
1536 is actually just a friend declaration. ATTRLIST is the list of
1537 attributes that the specialization is declared with or NULL when
1538 it isn't. Returns SPEC, or an equivalent prior declaration, if
1539 available.
1540
1541 We also store instantiations of field packs in the hash table, even
1542 though they are not themselves templates, to make lookup easier. */
1543
1544 static tree
1545 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1546 hashval_t hash)
1547 {
1548 tree fn;
1549 spec_entry **slot = NULL;
1550 spec_entry elt;
1551
1552 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1553 || (TREE_CODE (tmpl) == FIELD_DECL
1554 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1555
1556 if (TREE_CODE (spec) == FUNCTION_DECL
1557 && uses_template_parms (DECL_TI_ARGS (spec)))
1558 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1559 register it; we want the corresponding TEMPLATE_DECL instead.
1560 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1561 the more obvious `uses_template_parms (spec)' to avoid problems
1562 with default function arguments. In particular, given
1563 something like this:
1564
1565 template <class T> void f(T t1, T t = T())
1566
1567 the default argument expression is not substituted for in an
1568 instantiation unless and until it is actually needed. */
1569 return spec;
1570
1571 if (optimize_specialization_lookup_p (tmpl))
1572 /* We don't put these specializations in the hash table, but we might
1573 want to give an error about a mismatch. */
1574 fn = retrieve_specialization (tmpl, args, 0);
1575 else
1576 {
1577 elt.tmpl = tmpl;
1578 elt.args = args;
1579 elt.spec = spec;
1580
1581 if (hash == 0)
1582 hash = spec_hasher::hash (&elt);
1583
1584 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1585 if (*slot)
1586 fn = (*slot)->spec;
1587 else
1588 fn = NULL_TREE;
1589 }
1590
1591 /* We can sometimes try to re-register a specialization that we've
1592 already got. In particular, regenerate_decl_from_template calls
1593 duplicate_decls which will update the specialization list. But,
1594 we'll still get called again here anyhow. It's more convenient
1595 to simply allow this than to try to prevent it. */
1596 if (fn == spec)
1597 return spec;
1598 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1599 {
1600 if (DECL_TEMPLATE_INSTANTIATION (fn))
1601 {
1602 if (DECL_ODR_USED (fn)
1603 || DECL_EXPLICIT_INSTANTIATION (fn))
1604 {
1605 error ("specialization of %qD after instantiation",
1606 fn);
1607 return error_mark_node;
1608 }
1609 else
1610 {
1611 tree clone;
1612 /* This situation should occur only if the first
1613 specialization is an implicit instantiation, the
1614 second is an explicit specialization, and the
1615 implicit instantiation has not yet been used. That
1616 situation can occur if we have implicitly
1617 instantiated a member function and then specialized
1618 it later.
1619
1620 We can also wind up here if a friend declaration that
1621 looked like an instantiation turns out to be a
1622 specialization:
1623
1624 template <class T> void foo(T);
1625 class S { friend void foo<>(int) };
1626 template <> void foo(int);
1627
1628 We transform the existing DECL in place so that any
1629 pointers to it become pointers to the updated
1630 declaration.
1631
1632 If there was a definition for the template, but not
1633 for the specialization, we want this to look as if
1634 there were no definition, and vice versa. */
1635 DECL_INITIAL (fn) = NULL_TREE;
1636 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1637 /* The call to duplicate_decls will have applied
1638 [temp.expl.spec]:
1639
1640 An explicit specialization of a function template
1641 is inline only if it is explicitly declared to be,
1642 and independently of whether its function template
1643 is.
1644
1645 to the primary function; now copy the inline bits to
1646 the various clones. */
1647 FOR_EACH_CLONE (clone, fn)
1648 {
1649 DECL_DECLARED_INLINE_P (clone)
1650 = DECL_DECLARED_INLINE_P (fn);
1651 DECL_SOURCE_LOCATION (clone)
1652 = DECL_SOURCE_LOCATION (fn);
1653 DECL_DELETED_FN (clone)
1654 = DECL_DELETED_FN (fn);
1655 }
1656 check_specialization_namespace (tmpl);
1657
1658 return fn;
1659 }
1660 }
1661 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1662 {
1663 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1664 if (dd == error_mark_node)
1665 /* We've already complained in duplicate_decls. */
1666 return error_mark_node;
1667
1668 if (dd == NULL_TREE && DECL_INITIAL (spec))
1669 /* Dup decl failed, but this is a new definition. Set the
1670 line number so any errors match this new
1671 definition. */
1672 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1673
1674 return fn;
1675 }
1676 }
1677 else if (fn)
1678 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1679
1680 /* A specialization must be declared in the same namespace as the
1681 template it is specializing. */
1682 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1683 && !check_specialization_namespace (tmpl))
1684 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1685
1686 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1687 {
1688 spec_entry *entry = ggc_alloc<spec_entry> ();
1689 gcc_assert (tmpl && args && spec);
1690 *entry = elt;
1691 *slot = entry;
1692 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1693 && PRIMARY_TEMPLATE_P (tmpl)
1694 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1695 || variable_template_p (tmpl))
1696 /* If TMPL is a forward declaration of a template function, keep a list
1697 of all specializations in case we need to reassign them to a friend
1698 template later in tsubst_friend_function.
1699
1700 Also keep a list of all variable template instantiations so that
1701 process_partial_specialization can check whether a later partial
1702 specialization would have used it. */
1703 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1704 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1705 }
1706
1707 return spec;
1708 }
1709
1710 /* Restricts tree and type comparisons. */
1711 int comparing_specializations;
1712
1713 /* Returns true iff two spec_entry nodes are equivalent. */
1714
1715 bool
1716 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1717 {
1718 int equal;
1719
1720 ++comparing_specializations;
1721 equal = (e1->tmpl == e2->tmpl
1722 && comp_template_args (e1->args, e2->args));
1723 if (equal && flag_concepts
1724 /* tmpl could be a FIELD_DECL for a capture pack. */
1725 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1726 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1727 && uses_template_parms (e1->args))
1728 {
1729 /* Partial specializations of a variable template can be distinguished by
1730 constraints. */
1731 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1732 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1733 equal = equivalent_constraints (c1, c2);
1734 }
1735 --comparing_specializations;
1736
1737 return equal;
1738 }
1739
1740 /* Returns a hash for a template TMPL and template arguments ARGS. */
1741
1742 static hashval_t
1743 hash_tmpl_and_args (tree tmpl, tree args)
1744 {
1745 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1746 return iterative_hash_template_arg (args, val);
1747 }
1748
1749 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1750 ignoring SPEC. */
1751
1752 hashval_t
1753 spec_hasher::hash (spec_entry *e)
1754 {
1755 return hash_tmpl_and_args (e->tmpl, e->args);
1756 }
1757
1758 /* Recursively calculate a hash value for a template argument ARG, for use
1759 in the hash tables of template specializations. We must be
1760 careful to (at least) skip the same entities template_args_equal
1761 does. */
1762
1763 hashval_t
1764 iterative_hash_template_arg (tree arg, hashval_t val)
1765 {
1766 if (arg == NULL_TREE)
1767 return iterative_hash_object (arg, val);
1768
1769 if (!TYPE_P (arg))
1770 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1771 while (CONVERT_EXPR_P (arg)
1772 || TREE_CODE (arg) == NON_LVALUE_EXPR
1773 || class_nttp_const_wrapper_p (arg))
1774 arg = TREE_OPERAND (arg, 0);
1775
1776 enum tree_code code = TREE_CODE (arg);
1777
1778 val = iterative_hash_object (code, val);
1779
1780 switch (code)
1781 {
1782 case ARGUMENT_PACK_SELECT:
1783 gcc_unreachable ();
1784
1785 case ERROR_MARK:
1786 return val;
1787
1788 case IDENTIFIER_NODE:
1789 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1790
1791 case TREE_VEC:
1792 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1793 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1794 return val;
1795
1796 case TYPE_PACK_EXPANSION:
1797 case EXPR_PACK_EXPANSION:
1798 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1799 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1800
1801 case TYPE_ARGUMENT_PACK:
1802 case NONTYPE_ARGUMENT_PACK:
1803 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1804
1805 case TREE_LIST:
1806 for (; arg; arg = TREE_CHAIN (arg))
1807 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1808 return val;
1809
1810 case OVERLOAD:
1811 for (lkp_iterator iter (arg); iter; ++iter)
1812 val = iterative_hash_template_arg (*iter, val);
1813 return val;
1814
1815 case CONSTRUCTOR:
1816 {
1817 tree field, value;
1818 unsigned i;
1819 iterative_hash_template_arg (TREE_TYPE (arg), val);
1820 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1821 {
1822 val = iterative_hash_template_arg (field, val);
1823 val = iterative_hash_template_arg (value, val);
1824 }
1825 return val;
1826 }
1827
1828 case PARM_DECL:
1829 if (!DECL_ARTIFICIAL (arg))
1830 {
1831 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1832 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1833 }
1834 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1835
1836 case TARGET_EXPR:
1837 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1838
1839 case PTRMEM_CST:
1840 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1841 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1842
1843 case TEMPLATE_PARM_INDEX:
1844 val = iterative_hash_template_arg
1845 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1846 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1847 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1848
1849 case TRAIT_EXPR:
1850 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1851 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1852 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1853
1854 case BASELINK:
1855 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1856 val);
1857 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1858 val);
1859
1860 case MODOP_EXPR:
1861 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1862 code = TREE_CODE (TREE_OPERAND (arg, 1));
1863 val = iterative_hash_object (code, val);
1864 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1865
1866 case LAMBDA_EXPR:
1867 /* [temp.over.link] Two lambda-expressions are never considered
1868 equivalent.
1869
1870 So just hash the closure type. */
1871 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1872
1873 case CAST_EXPR:
1874 case IMPLICIT_CONV_EXPR:
1875 case STATIC_CAST_EXPR:
1876 case REINTERPRET_CAST_EXPR:
1877 case CONST_CAST_EXPR:
1878 case DYNAMIC_CAST_EXPR:
1879 case NEW_EXPR:
1880 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1881 /* Now hash operands as usual. */
1882 break;
1883
1884 case CALL_EXPR:
1885 {
1886 tree fn = CALL_EXPR_FN (arg);
1887 if (tree name = dependent_name (fn))
1888 {
1889 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1890 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1891 fn = name;
1892 }
1893 val = iterative_hash_template_arg (fn, val);
1894 call_expr_arg_iterator ai;
1895 for (tree x = first_call_expr_arg (arg, &ai); x;
1896 x = next_call_expr_arg (&ai))
1897 val = iterative_hash_template_arg (x, val);
1898 return val;
1899 }
1900
1901 default:
1902 break;
1903 }
1904
1905 char tclass = TREE_CODE_CLASS (code);
1906 switch (tclass)
1907 {
1908 case tcc_type:
1909 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1910 {
1911 // We want an alias specialization that survived strip_typedefs
1912 // to hash differently from its TYPE_CANONICAL, to avoid hash
1913 // collisions that compare as different in template_args_equal.
1914 // These could be dependent specializations that strip_typedefs
1915 // left alone, or untouched specializations because
1916 // coerce_template_parms returns the unconverted template
1917 // arguments if it sees incomplete argument packs.
1918 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1919 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1920 }
1921
1922 switch (TREE_CODE (arg))
1923 {
1924 case TEMPLATE_TEMPLATE_PARM:
1925 {
1926 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1927
1928 /* Do not recurse with TPI directly, as that is unbounded
1929 recursion. */
1930 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1931 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1932 }
1933 break;
1934
1935 case DECLTYPE_TYPE:
1936 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1937 break;
1938
1939 default:
1940 if (tree canonical = TYPE_CANONICAL (arg))
1941 val = iterative_hash_object (TYPE_HASH (canonical), val);
1942 break;
1943 }
1944
1945 return val;
1946
1947 case tcc_declaration:
1948 case tcc_constant:
1949 return iterative_hash_expr (arg, val);
1950
1951 default:
1952 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1953 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1954 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1955 return val;
1956 }
1957
1958 gcc_unreachable ();
1959 return 0;
1960 }
1961
1962 /* Unregister the specialization SPEC as a specialization of TMPL.
1963 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1964 if the SPEC was listed as a specialization of TMPL.
1965
1966 Note that SPEC has been ggc_freed, so we can't look inside it. */
1967
1968 bool
1969 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1970 {
1971 spec_entry *entry;
1972 spec_entry elt;
1973
1974 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1975 elt.args = TI_ARGS (tinfo);
1976 elt.spec = NULL_TREE;
1977
1978 entry = decl_specializations->find (&elt);
1979 if (entry != NULL)
1980 {
1981 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1982 gcc_assert (new_spec != NULL_TREE);
1983 entry->spec = new_spec;
1984 return 1;
1985 }
1986
1987 return 0;
1988 }
1989
1990 /* Like register_specialization, but for local declarations. We are
1991 registering SPEC, an instantiation of TMPL. */
1992
1993 void
1994 register_local_specialization (tree spec, tree tmpl)
1995 {
1996 gcc_assert (tmpl != spec);
1997 local_specializations->put (tmpl, spec);
1998 }
1999
2000 /* TYPE is a class type. Returns true if TYPE is an explicitly
2001 specialized class. */
2002
2003 bool
2004 explicit_class_specialization_p (tree type)
2005 {
2006 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2007 return false;
2008 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2009 }
2010
2011 /* Print the list of functions at FNS, going through all the overloads
2012 for each element of the list. Alternatively, FNS cannot be a
2013 TREE_LIST, in which case it will be printed together with all the
2014 overloads.
2015
2016 MORE and *STR should respectively be FALSE and NULL when the function
2017 is called from the outside. They are used internally on recursive
2018 calls. print_candidates manages the two parameters and leaves NULL
2019 in *STR when it ends. */
2020
2021 static void
2022 print_candidates_1 (tree fns, char **str, bool more = false)
2023 {
2024 if (TREE_CODE (fns) == TREE_LIST)
2025 for (; fns; fns = TREE_CHAIN (fns))
2026 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2027 else
2028 for (lkp_iterator iter (fns); iter;)
2029 {
2030 tree cand = *iter;
2031 ++iter;
2032
2033 const char *pfx = *str;
2034 if (!pfx)
2035 {
2036 if (more || iter)
2037 pfx = _("candidates are:");
2038 else
2039 pfx = _("candidate is:");
2040 *str = get_spaces (pfx);
2041 }
2042 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2043 }
2044 }
2045
2046 /* Print the list of candidate FNS in an error message. FNS can also
2047 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2048
2049 void
2050 print_candidates (tree fns)
2051 {
2052 char *str = NULL;
2053 print_candidates_1 (fns, &str);
2054 free (str);
2055 }
2056
2057 /* Get a (possibly) constrained template declaration for the
2058 purpose of ordering candidates. */
2059 static tree
2060 get_template_for_ordering (tree list)
2061 {
2062 gcc_assert (TREE_CODE (list) == TREE_LIST);
2063 tree f = TREE_VALUE (list);
2064 if (tree ti = DECL_TEMPLATE_INFO (f))
2065 return TI_TEMPLATE (ti);
2066 return f;
2067 }
2068
2069 /* Among candidates having the same signature, return the
2070 most constrained or NULL_TREE if there is no best candidate.
2071 If the signatures of candidates vary (e.g., template
2072 specialization vs. member function), then there can be no
2073 most constrained.
2074
2075 Note that we don't compare constraints on the functions
2076 themselves, but rather those of their templates. */
2077 static tree
2078 most_constrained_function (tree candidates)
2079 {
2080 // Try to find the best candidate in a first pass.
2081 tree champ = candidates;
2082 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2083 {
2084 int winner = more_constrained (get_template_for_ordering (champ),
2085 get_template_for_ordering (c));
2086 if (winner == -1)
2087 champ = c; // The candidate is more constrained
2088 else if (winner == 0)
2089 return NULL_TREE; // Neither is more constrained
2090 }
2091
2092 // Verify that the champ is better than previous candidates.
2093 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2094 if (!more_constrained (get_template_for_ordering (champ),
2095 get_template_for_ordering (c)))
2096 return NULL_TREE;
2097 }
2098
2099 return champ;
2100 }
2101
2102
2103 /* Returns the template (one of the functions given by TEMPLATE_ID)
2104 which can be specialized to match the indicated DECL with the
2105 explicit template args given in TEMPLATE_ID. The DECL may be
2106 NULL_TREE if none is available. In that case, the functions in
2107 TEMPLATE_ID are non-members.
2108
2109 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2110 specialization of a member template.
2111
2112 The TEMPLATE_COUNT is the number of references to qualifying
2113 template classes that appeared in the name of the function. See
2114 check_explicit_specialization for a more accurate description.
2115
2116 TSK indicates what kind of template declaration (if any) is being
2117 declared. TSK_TEMPLATE indicates that the declaration given by
2118 DECL, though a FUNCTION_DECL, has template parameters, and is
2119 therefore a template function.
2120
2121 The template args (those explicitly specified and those deduced)
2122 are output in a newly created vector *TARGS_OUT.
2123
2124 If it is impossible to determine the result, an error message is
2125 issued. The error_mark_node is returned to indicate failure. */
2126
2127 static tree
2128 determine_specialization (tree template_id,
2129 tree decl,
2130 tree* targs_out,
2131 int need_member_template,
2132 int template_count,
2133 tmpl_spec_kind tsk)
2134 {
2135 tree fns;
2136 tree targs;
2137 tree explicit_targs;
2138 tree candidates = NULL_TREE;
2139
2140 /* A TREE_LIST of templates of which DECL may be a specialization.
2141 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2142 corresponding TREE_PURPOSE is the set of template arguments that,
2143 when used to instantiate the template, would produce a function
2144 with the signature of DECL. */
2145 tree templates = NULL_TREE;
2146 int header_count;
2147 cp_binding_level *b;
2148
2149 *targs_out = NULL_TREE;
2150
2151 if (template_id == error_mark_node || decl == error_mark_node)
2152 return error_mark_node;
2153
2154 /* We shouldn't be specializing a member template of an
2155 unspecialized class template; we already gave an error in
2156 check_specialization_scope, now avoid crashing. */
2157 if (!VAR_P (decl)
2158 && template_count && DECL_CLASS_SCOPE_P (decl)
2159 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2160 {
2161 gcc_assert (errorcount);
2162 return error_mark_node;
2163 }
2164
2165 fns = TREE_OPERAND (template_id, 0);
2166 explicit_targs = TREE_OPERAND (template_id, 1);
2167
2168 if (fns == error_mark_node)
2169 return error_mark_node;
2170
2171 /* Check for baselinks. */
2172 if (BASELINK_P (fns))
2173 fns = BASELINK_FUNCTIONS (fns);
2174
2175 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2176 {
2177 error_at (DECL_SOURCE_LOCATION (decl),
2178 "%qD is not a function template", fns);
2179 return error_mark_node;
2180 }
2181 else if (VAR_P (decl) && !variable_template_p (fns))
2182 {
2183 error ("%qD is not a variable template", fns);
2184 return error_mark_node;
2185 }
2186
2187 /* Count the number of template headers specified for this
2188 specialization. */
2189 header_count = 0;
2190 for (b = current_binding_level;
2191 b->kind == sk_template_parms;
2192 b = b->level_chain)
2193 ++header_count;
2194
2195 tree orig_fns = fns;
2196
2197 if (variable_template_p (fns))
2198 {
2199 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2200 targs = coerce_template_parms (parms, explicit_targs, fns,
2201 tf_warning_or_error,
2202 /*req_all*/true, /*use_defarg*/true);
2203 if (targs != error_mark_node)
2204 templates = tree_cons (targs, fns, templates);
2205 }
2206 else for (lkp_iterator iter (fns); iter; ++iter)
2207 {
2208 tree fn = *iter;
2209
2210 if (TREE_CODE (fn) == TEMPLATE_DECL)
2211 {
2212 tree decl_arg_types;
2213 tree fn_arg_types;
2214 tree insttype;
2215
2216 /* In case of explicit specialization, we need to check if
2217 the number of template headers appearing in the specialization
2218 is correct. This is usually done in check_explicit_specialization,
2219 but the check done there cannot be exhaustive when specializing
2220 member functions. Consider the following code:
2221
2222 template <> void A<int>::f(int);
2223 template <> template <> void A<int>::f(int);
2224
2225 Assuming that A<int> is not itself an explicit specialization
2226 already, the first line specializes "f" which is a non-template
2227 member function, whilst the second line specializes "f" which
2228 is a template member function. So both lines are syntactically
2229 correct, and check_explicit_specialization does not reject
2230 them.
2231
2232 Here, we can do better, as we are matching the specialization
2233 against the declarations. We count the number of template
2234 headers, and we check if they match TEMPLATE_COUNT + 1
2235 (TEMPLATE_COUNT is the number of qualifying template classes,
2236 plus there must be another header for the member template
2237 itself).
2238
2239 Notice that if header_count is zero, this is not a
2240 specialization but rather a template instantiation, so there
2241 is no check we can perform here. */
2242 if (header_count && header_count != template_count + 1)
2243 continue;
2244
2245 /* Check that the number of template arguments at the
2246 innermost level for DECL is the same as for FN. */
2247 if (current_binding_level->kind == sk_template_parms
2248 && !current_binding_level->explicit_spec_p
2249 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2250 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2251 (current_template_parms))))
2252 continue;
2253
2254 /* DECL might be a specialization of FN. */
2255 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2256 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2257
2258 /* For a non-static member function, we need to make sure
2259 that the const qualification is the same. Since
2260 get_bindings does not try to merge the "this" parameter,
2261 we must do the comparison explicitly. */
2262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2263 {
2264 if (!same_type_p (TREE_VALUE (fn_arg_types),
2265 TREE_VALUE (decl_arg_types)))
2266 continue;
2267
2268 /* And the ref-qualification. */
2269 if (type_memfn_rqual (TREE_TYPE (decl))
2270 != type_memfn_rqual (TREE_TYPE (fn)))
2271 continue;
2272 }
2273
2274 /* Skip the "this" parameter and, for constructors of
2275 classes with virtual bases, the VTT parameter. A
2276 full specialization of a constructor will have a VTT
2277 parameter, but a template never will. */
2278 decl_arg_types
2279 = skip_artificial_parms_for (decl, decl_arg_types);
2280 fn_arg_types
2281 = skip_artificial_parms_for (fn, fn_arg_types);
2282
2283 /* Function templates cannot be specializations; there are
2284 no partial specializations of functions. Therefore, if
2285 the type of DECL does not match FN, there is no
2286 match.
2287
2288 Note that it should never be the case that we have both
2289 candidates added here, and for regular member functions
2290 below. */
2291 if (tsk == tsk_template)
2292 {
2293 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2294 current_template_parms))
2295 continue;
2296 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2297 TREE_TYPE (TREE_TYPE (fn))))
2298 continue;
2299 if (!compparms (fn_arg_types, decl_arg_types))
2300 continue;
2301
2302 tree freq = get_trailing_function_requirements (fn);
2303 tree dreq = get_trailing_function_requirements (decl);
2304 if (!freq != !dreq)
2305 continue;
2306 if (freq)
2307 {
2308 tree fargs = DECL_TI_ARGS (fn);
2309 tsubst_flags_t complain = tf_none;
2310 freq = tsubst_constraint (freq, fargs, complain, fn);
2311 if (!cp_tree_equal (freq, dreq))
2312 continue;
2313 }
2314
2315 candidates = tree_cons (NULL_TREE, fn, candidates);
2316 continue;
2317 }
2318
2319 /* See whether this function might be a specialization of this
2320 template. Suppress access control because we might be trying
2321 to make this specialization a friend, and we have already done
2322 access control for the declaration of the specialization. */
2323 push_deferring_access_checks (dk_no_check);
2324 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2325 pop_deferring_access_checks ();
2326
2327 if (!targs)
2328 /* We cannot deduce template arguments that when used to
2329 specialize TMPL will produce DECL. */
2330 continue;
2331
2332 if (uses_template_parms (targs))
2333 /* We deduced something involving 'auto', which isn't a valid
2334 template argument. */
2335 continue;
2336
2337 /* Remove, from the set of candidates, all those functions
2338 whose constraints are not satisfied. */
2339 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2340 continue;
2341
2342 // Then, try to form the new function type.
2343 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2344 if (insttype == error_mark_node)
2345 continue;
2346 fn_arg_types
2347 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2348 if (!compparms (fn_arg_types, decl_arg_types))
2349 continue;
2350
2351 /* Save this template, and the arguments deduced. */
2352 templates = tree_cons (targs, fn, templates);
2353 }
2354 else if (need_member_template)
2355 /* FN is an ordinary member function, and we need a
2356 specialization of a member template. */
2357 ;
2358 else if (TREE_CODE (fn) != FUNCTION_DECL)
2359 /* We can get IDENTIFIER_NODEs here in certain erroneous
2360 cases. */
2361 ;
2362 else if (!DECL_FUNCTION_MEMBER_P (fn))
2363 /* This is just an ordinary non-member function. Nothing can
2364 be a specialization of that. */
2365 ;
2366 else if (DECL_ARTIFICIAL (fn))
2367 /* Cannot specialize functions that are created implicitly. */
2368 ;
2369 else
2370 {
2371 tree decl_arg_types;
2372
2373 /* This is an ordinary member function. However, since
2374 we're here, we can assume its enclosing class is a
2375 template class. For example,
2376
2377 template <typename T> struct S { void f(); };
2378 template <> void S<int>::f() {}
2379
2380 Here, S<int>::f is a non-template, but S<int> is a
2381 template class. If FN has the same type as DECL, we
2382 might be in business. */
2383
2384 if (!DECL_TEMPLATE_INFO (fn))
2385 /* Its enclosing class is an explicit specialization
2386 of a template class. This is not a candidate. */
2387 continue;
2388
2389 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2390 TREE_TYPE (TREE_TYPE (fn))))
2391 /* The return types differ. */
2392 continue;
2393
2394 /* Adjust the type of DECL in case FN is a static member. */
2395 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2396 if (DECL_STATIC_FUNCTION_P (fn)
2397 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2398 decl_arg_types = TREE_CHAIN (decl_arg_types);
2399
2400 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2401 decl_arg_types))
2402 continue;
2403
2404 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2405 && (type_memfn_rqual (TREE_TYPE (decl))
2406 != type_memfn_rqual (TREE_TYPE (fn))))
2407 continue;
2408
2409 // If the deduced arguments do not satisfy the constraints,
2410 // this is not a candidate.
2411 if (flag_concepts && !constraints_satisfied_p (fn))
2412 continue;
2413
2414 // Add the candidate.
2415 candidates = tree_cons (NULL_TREE, fn, candidates);
2416 }
2417 }
2418
2419 if (templates && TREE_CHAIN (templates))
2420 {
2421 /* We have:
2422
2423 [temp.expl.spec]
2424
2425 It is possible for a specialization with a given function
2426 signature to be instantiated from more than one function
2427 template. In such cases, explicit specification of the
2428 template arguments must be used to uniquely identify the
2429 function template specialization being specialized.
2430
2431 Note that here, there's no suggestion that we're supposed to
2432 determine which of the candidate templates is most
2433 specialized. However, we, also have:
2434
2435 [temp.func.order]
2436
2437 Partial ordering of overloaded function template
2438 declarations is used in the following contexts to select
2439 the function template to which a function template
2440 specialization refers:
2441
2442 -- when an explicit specialization refers to a function
2443 template.
2444
2445 So, we do use the partial ordering rules, at least for now.
2446 This extension can only serve to make invalid programs valid,
2447 so it's safe. And, there is strong anecdotal evidence that
2448 the committee intended the partial ordering rules to apply;
2449 the EDG front end has that behavior, and John Spicer claims
2450 that the committee simply forgot to delete the wording in
2451 [temp.expl.spec]. */
2452 tree tmpl = most_specialized_instantiation (templates);
2453 if (tmpl != error_mark_node)
2454 {
2455 templates = tmpl;
2456 TREE_CHAIN (templates) = NULL_TREE;
2457 }
2458 }
2459
2460 // Concepts allows multiple declarations of member functions
2461 // with the same signature. Like above, we need to rely on
2462 // on the partial ordering of those candidates to determine which
2463 // is the best.
2464 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2465 {
2466 if (tree cand = most_constrained_function (candidates))
2467 {
2468 candidates = cand;
2469 TREE_CHAIN (cand) = NULL_TREE;
2470 }
2471 }
2472
2473 if (templates == NULL_TREE && candidates == NULL_TREE)
2474 {
2475 error ("template-id %qD for %q+D does not match any template "
2476 "declaration", template_id, decl);
2477 if (header_count && header_count != template_count + 1)
2478 inform (DECL_SOURCE_LOCATION (decl),
2479 "saw %d %<template<>%>, need %d for "
2480 "specializing a member function template",
2481 header_count, template_count + 1);
2482 else
2483 print_candidates (orig_fns);
2484 return error_mark_node;
2485 }
2486 else if ((templates && TREE_CHAIN (templates))
2487 || (candidates && TREE_CHAIN (candidates))
2488 || (templates && candidates))
2489 {
2490 error ("ambiguous template specialization %qD for %q+D",
2491 template_id, decl);
2492 candidates = chainon (candidates, templates);
2493 print_candidates (candidates);
2494 return error_mark_node;
2495 }
2496
2497 /* We have one, and exactly one, match. */
2498 if (candidates)
2499 {
2500 tree fn = TREE_VALUE (candidates);
2501 *targs_out = copy_node (DECL_TI_ARGS (fn));
2502
2503 /* Propagate the candidate's constraints to the declaration. */
2504 if (tsk != tsk_template)
2505 set_constraints (decl, get_constraints (fn));
2506
2507 /* DECL is a re-declaration or partial instantiation of a template
2508 function. */
2509 if (TREE_CODE (fn) == TEMPLATE_DECL)
2510 return fn;
2511 /* It was a specialization of an ordinary member function in a
2512 template class. */
2513 return DECL_TI_TEMPLATE (fn);
2514 }
2515
2516 /* It was a specialization of a template. */
2517 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2518 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2519 {
2520 *targs_out = copy_node (targs);
2521 SET_TMPL_ARGS_LEVEL (*targs_out,
2522 TMPL_ARGS_DEPTH (*targs_out),
2523 TREE_PURPOSE (templates));
2524 }
2525 else
2526 *targs_out = TREE_PURPOSE (templates);
2527 return TREE_VALUE (templates);
2528 }
2529
2530 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2531 but with the default argument values filled in from those in the
2532 TMPL_TYPES. */
2533
2534 static tree
2535 copy_default_args_to_explicit_spec_1 (tree spec_types,
2536 tree tmpl_types)
2537 {
2538 tree new_spec_types;
2539
2540 if (!spec_types)
2541 return NULL_TREE;
2542
2543 if (spec_types == void_list_node)
2544 return void_list_node;
2545
2546 /* Substitute into the rest of the list. */
2547 new_spec_types =
2548 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2549 TREE_CHAIN (tmpl_types));
2550
2551 /* Add the default argument for this parameter. */
2552 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2553 TREE_VALUE (spec_types),
2554 new_spec_types);
2555 }
2556
2557 /* DECL is an explicit specialization. Replicate default arguments
2558 from the template it specializes. (That way, code like:
2559
2560 template <class T> void f(T = 3);
2561 template <> void f(double);
2562 void g () { f (); }
2563
2564 works, as required.) An alternative approach would be to look up
2565 the correct default arguments at the call-site, but this approach
2566 is consistent with how implicit instantiations are handled. */
2567
2568 static void
2569 copy_default_args_to_explicit_spec (tree decl)
2570 {
2571 tree tmpl;
2572 tree spec_types;
2573 tree tmpl_types;
2574 tree new_spec_types;
2575 tree old_type;
2576 tree new_type;
2577 tree t;
2578 tree object_type = NULL_TREE;
2579 tree in_charge = NULL_TREE;
2580 tree vtt = NULL_TREE;
2581
2582 /* See if there's anything we need to do. */
2583 tmpl = DECL_TI_TEMPLATE (decl);
2584 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2585 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2586 if (TREE_PURPOSE (t))
2587 break;
2588 if (!t)
2589 return;
2590
2591 old_type = TREE_TYPE (decl);
2592 spec_types = TYPE_ARG_TYPES (old_type);
2593
2594 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2595 {
2596 /* Remove the this pointer, but remember the object's type for
2597 CV quals. */
2598 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2599 spec_types = TREE_CHAIN (spec_types);
2600 tmpl_types = TREE_CHAIN (tmpl_types);
2601
2602 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2603 {
2604 /* DECL may contain more parameters than TMPL due to the extra
2605 in-charge parameter in constructors and destructors. */
2606 in_charge = spec_types;
2607 spec_types = TREE_CHAIN (spec_types);
2608 }
2609 if (DECL_HAS_VTT_PARM_P (decl))
2610 {
2611 vtt = spec_types;
2612 spec_types = TREE_CHAIN (spec_types);
2613 }
2614 }
2615
2616 /* Compute the merged default arguments. */
2617 new_spec_types =
2618 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2619
2620 /* Compute the new FUNCTION_TYPE. */
2621 if (object_type)
2622 {
2623 if (vtt)
2624 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2625 TREE_VALUE (vtt),
2626 new_spec_types);
2627
2628 if (in_charge)
2629 /* Put the in-charge parameter back. */
2630 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2631 TREE_VALUE (in_charge),
2632 new_spec_types);
2633
2634 new_type = build_method_type_directly (object_type,
2635 TREE_TYPE (old_type),
2636 new_spec_types);
2637 }
2638 else
2639 new_type = build_function_type (TREE_TYPE (old_type),
2640 new_spec_types);
2641 new_type = cp_build_type_attribute_variant (new_type,
2642 TYPE_ATTRIBUTES (old_type));
2643 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2644
2645 TREE_TYPE (decl) = new_type;
2646 }
2647
2648 /* Return the number of template headers we expect to see for a definition
2649 or specialization of CTYPE or one of its non-template members. */
2650
2651 int
2652 num_template_headers_for_class (tree ctype)
2653 {
2654 int num_templates = 0;
2655
2656 while (ctype && CLASS_TYPE_P (ctype))
2657 {
2658 /* You're supposed to have one `template <...>' for every
2659 template class, but you don't need one for a full
2660 specialization. For example:
2661
2662 template <class T> struct S{};
2663 template <> struct S<int> { void f(); };
2664 void S<int>::f () {}
2665
2666 is correct; there shouldn't be a `template <>' for the
2667 definition of `S<int>::f'. */
2668 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2669 /* If CTYPE does not have template information of any
2670 kind, then it is not a template, nor is it nested
2671 within a template. */
2672 break;
2673 if (explicit_class_specialization_p (ctype))
2674 break;
2675 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2676 ++num_templates;
2677
2678 ctype = TYPE_CONTEXT (ctype);
2679 }
2680
2681 return num_templates;
2682 }
2683
2684 /* Do a simple sanity check on the template headers that precede the
2685 variable declaration DECL. */
2686
2687 void
2688 check_template_variable (tree decl)
2689 {
2690 tree ctx = CP_DECL_CONTEXT (decl);
2691 int wanted = num_template_headers_for_class (ctx);
2692 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2693 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2694 {
2695 if (cxx_dialect < cxx14)
2696 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2697 "variable templates only available with "
2698 "%<-std=c++14%> or %<-std=gnu++14%>");
2699
2700 // Namespace-scope variable templates should have a template header.
2701 ++wanted;
2702 }
2703 if (template_header_count > wanted)
2704 {
2705 auto_diagnostic_group d;
2706 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2707 "too many template headers for %qD "
2708 "(should be %d)",
2709 decl, wanted);
2710 if (warned && CLASS_TYPE_P (ctx)
2711 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2712 inform (DECL_SOURCE_LOCATION (decl),
2713 "members of an explicitly specialized class are defined "
2714 "without a template header");
2715 }
2716 }
2717
2718 /* An explicit specialization whose declarator-id or class-head-name is not
2719 qualified shall be declared in the nearest enclosing namespace of the
2720 template, or, if the namespace is inline (7.3.1), any namespace from its
2721 enclosing namespace set.
2722
2723 If the name declared in the explicit instantiation is an unqualified name,
2724 the explicit instantiation shall appear in the namespace where its template
2725 is declared or, if that namespace is inline (7.3.1), any namespace from its
2726 enclosing namespace set. */
2727
2728 void
2729 check_unqualified_spec_or_inst (tree t, location_t loc)
2730 {
2731 tree tmpl = most_general_template (t);
2732 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2733 && !is_nested_namespace (current_namespace,
2734 CP_DECL_CONTEXT (tmpl), true))
2735 {
2736 if (processing_specialization)
2737 permerror (loc, "explicit specialization of %qD outside its "
2738 "namespace must use a nested-name-specifier", tmpl);
2739 else if (processing_explicit_instantiation
2740 && cxx_dialect >= cxx11)
2741 /* This was allowed in C++98, so only pedwarn. */
2742 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2743 "outside its namespace must use a nested-name-"
2744 "specifier", tmpl);
2745 }
2746 }
2747
2748 /* Warn for a template specialization SPEC that is missing some of a set
2749 of function or type attributes that the template TEMPL is declared with.
2750 ATTRLIST is a list of additional attributes that SPEC should be taken
2751 to ultimately be declared with. */
2752
2753 static void
2754 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2755 {
2756 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2757 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2758
2759 /* Avoid warning if the difference between the primary and
2760 the specialization is not in one of the attributes below. */
2761 const char* const blacklist[] = {
2762 "alloc_align", "alloc_size", "assume_aligned", "format",
2763 "format_arg", "malloc", "nonnull", NULL
2764 };
2765
2766 /* Put together a list of the black listed attributes that the primary
2767 template is declared with that the specialization is not, in case
2768 it's not apparent from the most recent declaration of the primary. */
2769 pretty_printer str;
2770 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2771 blacklist, &str);
2772
2773 if (!nattrs)
2774 return;
2775
2776 auto_diagnostic_group d;
2777 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2778 "explicit specialization %q#D may be missing attributes",
2779 spec))
2780 inform (DECL_SOURCE_LOCATION (tmpl),
2781 nattrs > 1
2782 ? G_("missing primary template attributes %s")
2783 : G_("missing primary template attribute %s"),
2784 pp_formatted_text (&str));
2785 }
2786
2787 /* Check to see if the function just declared, as indicated in
2788 DECLARATOR, and in DECL, is a specialization of a function
2789 template. We may also discover that the declaration is an explicit
2790 instantiation at this point.
2791
2792 Returns DECL, or an equivalent declaration that should be used
2793 instead if all goes well. Issues an error message if something is
2794 amiss. Returns error_mark_node if the error is not easily
2795 recoverable.
2796
2797 FLAGS is a bitmask consisting of the following flags:
2798
2799 2: The function has a definition.
2800 4: The function is a friend.
2801
2802 The TEMPLATE_COUNT is the number of references to qualifying
2803 template classes that appeared in the name of the function. For
2804 example, in
2805
2806 template <class T> struct S { void f(); };
2807 void S<int>::f();
2808
2809 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2810 classes are not counted in the TEMPLATE_COUNT, so that in
2811
2812 template <class T> struct S {};
2813 template <> struct S<int> { void f(); }
2814 template <> void S<int>::f();
2815
2816 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2817 invalid; there should be no template <>.)
2818
2819 If the function is a specialization, it is marked as such via
2820 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2821 is set up correctly, and it is added to the list of specializations
2822 for that template. */
2823
2824 tree
2825 check_explicit_specialization (tree declarator,
2826 tree decl,
2827 int template_count,
2828 int flags,
2829 tree attrlist)
2830 {
2831 int have_def = flags & 2;
2832 int is_friend = flags & 4;
2833 bool is_concept = flags & 8;
2834 int specialization = 0;
2835 int explicit_instantiation = 0;
2836 int member_specialization = 0;
2837 tree ctype = DECL_CLASS_CONTEXT (decl);
2838 tree dname = DECL_NAME (decl);
2839 tmpl_spec_kind tsk;
2840
2841 if (is_friend)
2842 {
2843 if (!processing_specialization)
2844 tsk = tsk_none;
2845 else
2846 tsk = tsk_excessive_parms;
2847 }
2848 else
2849 tsk = current_tmpl_spec_kind (template_count);
2850
2851 switch (tsk)
2852 {
2853 case tsk_none:
2854 if (processing_specialization && !VAR_P (decl))
2855 {
2856 specialization = 1;
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 }
2859 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2860 {
2861 if (is_friend)
2862 /* This could be something like:
2863
2864 template <class T> void f(T);
2865 class S { friend void f<>(int); } */
2866 specialization = 1;
2867 else
2868 {
2869 /* This case handles bogus declarations like template <>
2870 template <class T> void f<int>(); */
2871
2872 error_at (cp_expr_loc_or_input_loc (declarator),
2873 "template-id %qE in declaration of primary template",
2874 declarator);
2875 return decl;
2876 }
2877 }
2878 break;
2879
2880 case tsk_invalid_member_spec:
2881 /* The error has already been reported in
2882 check_specialization_scope. */
2883 return error_mark_node;
2884
2885 case tsk_invalid_expl_inst:
2886 error ("template parameter list used in explicit instantiation");
2887
2888 /* Fall through. */
2889
2890 case tsk_expl_inst:
2891 if (have_def)
2892 error ("definition provided for explicit instantiation");
2893
2894 explicit_instantiation = 1;
2895 break;
2896
2897 case tsk_excessive_parms:
2898 case tsk_insufficient_parms:
2899 if (tsk == tsk_excessive_parms)
2900 error ("too many template parameter lists in declaration of %qD",
2901 decl);
2902 else if (template_header_count)
2903 error("too few template parameter lists in declaration of %qD", decl);
2904 else
2905 error("explicit specialization of %qD must be introduced by "
2906 "%<template <>%>", decl);
2907
2908 /* Fall through. */
2909 case tsk_expl_spec:
2910 if (is_concept)
2911 error ("explicit specialization declared %<concept%>");
2912
2913 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2914 /* In cases like template<> constexpr bool v = true;
2915 We'll give an error in check_template_variable. */
2916 break;
2917
2918 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2919 if (ctype)
2920 member_specialization = 1;
2921 else
2922 specialization = 1;
2923 break;
2924
2925 case tsk_template:
2926 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2927 {
2928 /* This case handles bogus declarations like template <>
2929 template <class T> void f<int>(); */
2930
2931 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2932 error_at (cp_expr_loc_or_input_loc (declarator),
2933 "template-id %qE in declaration of primary template",
2934 declarator);
2935 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2936 {
2937 /* Partial specialization of variable template. */
2938 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2939 specialization = 1;
2940 goto ok;
2941 }
2942 else if (cxx_dialect < cxx14)
2943 error_at (cp_expr_loc_or_input_loc (declarator),
2944 "non-type partial specialization %qE "
2945 "is not allowed", declarator);
2946 else
2947 error_at (cp_expr_loc_or_input_loc (declarator),
2948 "non-class, non-variable partial specialization %qE "
2949 "is not allowed", declarator);
2950 return decl;
2951 ok:;
2952 }
2953
2954 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2955 /* This is a specialization of a member template, without
2956 specialization the containing class. Something like:
2957
2958 template <class T> struct S {
2959 template <class U> void f (U);
2960 };
2961 template <> template <class U> void S<int>::f(U) {}
2962
2963 That's a specialization -- but of the entire template. */
2964 specialization = 1;
2965 break;
2966
2967 default:
2968 gcc_unreachable ();
2969 }
2970
2971 if ((specialization || member_specialization)
2972 /* This doesn't apply to variable templates. */
2973 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2974 {
2975 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2976 for (; t; t = TREE_CHAIN (t))
2977 if (TREE_PURPOSE (t))
2978 {
2979 permerror (input_location,
2980 "default argument specified in explicit specialization");
2981 break;
2982 }
2983 }
2984
2985 if (specialization || member_specialization || explicit_instantiation)
2986 {
2987 tree tmpl = NULL_TREE;
2988 tree targs = NULL_TREE;
2989 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2990 bool found_hidden = false;
2991
2992 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2993 if (!was_template_id)
2994 {
2995 tree fns;
2996
2997 gcc_assert (identifier_p (declarator));
2998 if (ctype)
2999 fns = dname;
3000 else
3001 {
3002 /* If there is no class context, the explicit instantiation
3003 must be at namespace scope. */
3004 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3005
3006 /* Find the namespace binding, using the declaration
3007 context. */
3008 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3009 LOOK_want::NORMAL, true);
3010 if (fns == error_mark_node)
3011 {
3012 /* If lookup fails, look for a friend declaration so we can
3013 give a better diagnostic. */
3014 fns = (lookup_qualified_name
3015 (CP_DECL_CONTEXT (decl), dname,
3016 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3017 /*complain*/true));
3018 found_hidden = true;
3019 }
3020
3021 if (fns == error_mark_node || !is_overloaded_fn (fns))
3022 {
3023 error ("%qD is not a template function", dname);
3024 fns = error_mark_node;
3025 }
3026 }
3027
3028 declarator = lookup_template_function (fns, NULL_TREE);
3029 }
3030
3031 if (declarator == error_mark_node)
3032 return error_mark_node;
3033
3034 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3035 {
3036 if (!explicit_instantiation)
3037 /* A specialization in class scope. This is invalid,
3038 but the error will already have been flagged by
3039 check_specialization_scope. */
3040 return error_mark_node;
3041 else
3042 {
3043 /* It's not valid to write an explicit instantiation in
3044 class scope, e.g.:
3045
3046 class C { template void f(); }
3047
3048 This case is caught by the parser. However, on
3049 something like:
3050
3051 template class C { void f(); };
3052
3053 (which is invalid) we can get here. The error will be
3054 issued later. */
3055 ;
3056 }
3057
3058 return decl;
3059 }
3060 else if (ctype != NULL_TREE
3061 && (identifier_p (TREE_OPERAND (declarator, 0))))
3062 {
3063 // We'll match variable templates in start_decl.
3064 if (VAR_P (decl))
3065 return decl;
3066
3067 /* Find the list of functions in ctype that have the same
3068 name as the declared function. */
3069 tree name = TREE_OPERAND (declarator, 0);
3070
3071 if (constructor_name_p (name, ctype))
3072 {
3073 if (DECL_CONSTRUCTOR_P (decl)
3074 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3075 : !CLASSTYPE_DESTRUCTOR (ctype))
3076 {
3077 /* From [temp.expl.spec]:
3078
3079 If such an explicit specialization for the member
3080 of a class template names an implicitly-declared
3081 special member function (clause _special_), the
3082 program is ill-formed.
3083
3084 Similar language is found in [temp.explicit]. */
3085 error ("specialization of implicitly-declared special member function");
3086 return error_mark_node;
3087 }
3088
3089 name = DECL_NAME (decl);
3090 }
3091
3092 /* For a type-conversion operator, We might be looking for
3093 `operator int' which will be a specialization of
3094 `operator T'. Grab all the conversion operators, and
3095 then select from them. */
3096 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3097 ? conv_op_identifier : name);
3098
3099 if (fns == NULL_TREE)
3100 {
3101 error ("no member function %qD declared in %qT", name, ctype);
3102 return error_mark_node;
3103 }
3104 else
3105 TREE_OPERAND (declarator, 0) = fns;
3106 }
3107
3108 /* Figure out what exactly is being specialized at this point.
3109 Note that for an explicit instantiation, even one for a
3110 member function, we cannot tell a priori whether the
3111 instantiation is for a member template, or just a member
3112 function of a template class. Even if a member template is
3113 being instantiated, the member template arguments may be
3114 elided if they can be deduced from the rest of the
3115 declaration. */
3116 tmpl = determine_specialization (declarator, decl,
3117 &targs,
3118 member_specialization,
3119 template_count,
3120 tsk);
3121
3122 if (!tmpl || tmpl == error_mark_node)
3123 /* We couldn't figure out what this declaration was
3124 specializing. */
3125 return error_mark_node;
3126 else
3127 {
3128 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3129 {
3130 auto_diagnostic_group d;
3131 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3132 "friend declaration %qD is not visible to "
3133 "explicit specialization", tmpl))
3134 inform (DECL_SOURCE_LOCATION (tmpl),
3135 "friend declaration here");
3136 }
3137
3138 if (!ctype && !is_friend
3139 && CP_DECL_CONTEXT (decl) == current_namespace)
3140 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3141
3142 tree gen_tmpl = most_general_template (tmpl);
3143
3144 if (explicit_instantiation)
3145 {
3146 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3147 is done by do_decl_instantiation later. */
3148
3149 int arg_depth = TMPL_ARGS_DEPTH (targs);
3150 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3151
3152 if (arg_depth > parm_depth)
3153 {
3154 /* If TMPL is not the most general template (for
3155 example, if TMPL is a friend template that is
3156 injected into namespace scope), then there will
3157 be too many levels of TARGS. Remove some of them
3158 here. */
3159 int i;
3160 tree new_targs;
3161
3162 new_targs = make_tree_vec (parm_depth);
3163 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3164 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3165 = TREE_VEC_ELT (targs, i);
3166 targs = new_targs;
3167 }
3168
3169 return instantiate_template (tmpl, targs, tf_error);
3170 }
3171
3172 /* If we thought that the DECL was a member function, but it
3173 turns out to be specializing a static member function,
3174 make DECL a static member function as well. */
3175 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3176 && DECL_STATIC_FUNCTION_P (tmpl)
3177 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3178 revert_static_member_fn (decl);
3179
3180 /* If this is a specialization of a member template of a
3181 template class, we want to return the TEMPLATE_DECL, not
3182 the specialization of it. */
3183 if (tsk == tsk_template && !was_template_id)
3184 {
3185 tree result = DECL_TEMPLATE_RESULT (tmpl);
3186 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3187 DECL_INITIAL (result) = NULL_TREE;
3188 if (have_def)
3189 {
3190 tree parm;
3191 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3192 DECL_SOURCE_LOCATION (result)
3193 = DECL_SOURCE_LOCATION (decl);
3194 /* We want to use the argument list specified in the
3195 definition, not in the original declaration. */
3196 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3197 for (parm = DECL_ARGUMENTS (result); parm;
3198 parm = DECL_CHAIN (parm))
3199 DECL_CONTEXT (parm) = result;
3200 }
3201 return register_specialization (tmpl, gen_tmpl, targs,
3202 is_friend, 0);
3203 }
3204
3205 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3206 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3207
3208 if (was_template_id)
3209 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3210
3211 /* Inherit default function arguments from the template
3212 DECL is specializing. */
3213 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3214 copy_default_args_to_explicit_spec (decl);
3215
3216 /* This specialization has the same protection as the
3217 template it specializes. */
3218 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3219 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3220
3221 /* 7.1.1-1 [dcl.stc]
3222
3223 A storage-class-specifier shall not be specified in an
3224 explicit specialization...
3225
3226 The parser rejects these, so unless action is taken here,
3227 explicit function specializations will always appear with
3228 global linkage.
3229
3230 The action recommended by the C++ CWG in response to C++
3231 defect report 605 is to make the storage class and linkage
3232 of the explicit specialization match the templated function:
3233
3234 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3235 */
3236 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3237 {
3238 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3239 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3240
3241 /* A concept cannot be specialized. */
3242 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3243 {
3244 error ("explicit specialization of function concept %qD",
3245 gen_tmpl);
3246 return error_mark_node;
3247 }
3248
3249 /* This specialization has the same linkage and visibility as
3250 the function template it specializes. */
3251 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3252 if (! TREE_PUBLIC (decl))
3253 {
3254 DECL_INTERFACE_KNOWN (decl) = 1;
3255 DECL_NOT_REALLY_EXTERN (decl) = 1;
3256 }
3257 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3258 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3259 {
3260 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3261 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3262 }
3263 }
3264
3265 /* If DECL is a friend declaration, declared using an
3266 unqualified name, the namespace associated with DECL may
3267 have been set incorrectly. For example, in:
3268
3269 template <typename T> void f(T);
3270 namespace N {
3271 struct S { friend void f<int>(int); }
3272 }
3273
3274 we will have set the DECL_CONTEXT for the friend
3275 declaration to N, rather than to the global namespace. */
3276 if (DECL_NAMESPACE_SCOPE_P (decl))
3277 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3278
3279 if (is_friend && !have_def)
3280 /* This is not really a declaration of a specialization.
3281 It's just the name of an instantiation. But, it's not
3282 a request for an instantiation, either. */
3283 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3284 else if (TREE_CODE (decl) == FUNCTION_DECL)
3285 /* A specialization is not necessarily COMDAT. */
3286 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3287 && DECL_DECLARED_INLINE_P (decl));
3288 else if (VAR_P (decl))
3289 DECL_COMDAT (decl) = false;
3290
3291 /* If this is a full specialization, register it so that we can find
3292 it again. Partial specializations will be registered in
3293 process_partial_specialization. */
3294 if (!processing_template_decl)
3295 {
3296 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3297
3298 decl = register_specialization (decl, gen_tmpl, targs,
3299 is_friend, 0);
3300 }
3301
3302
3303 /* A 'structor should already have clones. */
3304 gcc_assert (decl == error_mark_node
3305 || variable_template_p (tmpl)
3306 || !(DECL_CONSTRUCTOR_P (decl)
3307 || DECL_DESTRUCTOR_P (decl))
3308 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3309 }
3310 }
3311
3312 return decl;
3313 }
3314
3315 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3316 parameters. These are represented in the same format used for
3317 DECL_TEMPLATE_PARMS. */
3318
3319 int
3320 comp_template_parms (const_tree parms1, const_tree parms2)
3321 {
3322 const_tree p1;
3323 const_tree p2;
3324
3325 if (parms1 == parms2)
3326 return 1;
3327
3328 for (p1 = parms1, p2 = parms2;
3329 p1 != NULL_TREE && p2 != NULL_TREE;
3330 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3331 {
3332 tree t1 = TREE_VALUE (p1);
3333 tree t2 = TREE_VALUE (p2);
3334 int i;
3335
3336 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3337 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3338
3339 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3340 return 0;
3341
3342 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3343 {
3344 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3345 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3346
3347 /* If either of the template parameters are invalid, assume
3348 they match for the sake of error recovery. */
3349 if (error_operand_p (parm1) || error_operand_p (parm2))
3350 return 1;
3351
3352 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3353 return 0;
3354
3355 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3356 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3357 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3358 continue;
3359 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3360 return 0;
3361 }
3362 }
3363
3364 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3365 /* One set of parameters has more parameters lists than the
3366 other. */
3367 return 0;
3368
3369 return 1;
3370 }
3371
3372 /* Returns true if two template parameters are declared with
3373 equivalent constraints. */
3374
3375 static bool
3376 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3377 {
3378 tree req1 = TREE_TYPE (parm1);
3379 tree req2 = TREE_TYPE (parm2);
3380 if (!req1 != !req2)
3381 return false;
3382 if (req1)
3383 return cp_tree_equal (req1, req2);
3384 return true;
3385 }
3386
3387 /* Returns true when two template parameters are equivalent. */
3388
3389 static bool
3390 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3391 {
3392 tree decl1 = TREE_VALUE (parm1);
3393 tree decl2 = TREE_VALUE (parm2);
3394
3395 /* If either of the template parameters are invalid, assume
3396 they match for the sake of error recovery. */
3397 if (error_operand_p (decl1) || error_operand_p (decl2))
3398 return true;
3399
3400 /* ... they declare parameters of the same kind. */
3401 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3402 return false;
3403
3404 /* ... one parameter was introduced by a parameter declaration, then
3405 both are. This case arises as a result of eagerly rewriting declarations
3406 during parsing. */
3407 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3408 return false;
3409
3410 /* ... if either declares a pack, they both do. */
3411 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3412 return false;
3413
3414 if (TREE_CODE (decl1) == PARM_DECL)
3415 {
3416 /* ... if they declare non-type parameters, the types are equivalent. */
3417 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3418 return false;
3419 }
3420 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3421 {
3422 /* ... if they declare template template parameters, their template
3423 parameter lists are equivalent. */
3424 if (!template_heads_equivalent_p (decl1, decl2))
3425 return false;
3426 }
3427
3428 /* ... if they are declared with a qualified-concept name, they both
3429 are, and those names are equivalent. */
3430 return template_parameter_constraints_equivalent_p (parm1, parm2);
3431 }
3432
3433 /* Returns true if two template parameters lists are equivalent.
3434 Two template parameter lists are equivalent if they have the
3435 same length and their corresponding parameters are equivalent.
3436
3437 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3438 data structure returned by DECL_TEMPLATE_PARMS.
3439
3440 This is generally the same implementation as comp_template_parms
3441 except that it also the concept names and arguments used to
3442 introduce parameters. */
3443
3444 static bool
3445 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3446 {
3447 if (parms1 == parms2)
3448 return true;
3449
3450 const_tree p1 = parms1;
3451 const_tree p2 = parms2;
3452 while (p1 != NULL_TREE && p2 != NULL_TREE)
3453 {
3454 tree list1 = TREE_VALUE (p1);
3455 tree list2 = TREE_VALUE (p2);
3456
3457 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3458 return 0;
3459
3460 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3461 {
3462 tree parm1 = TREE_VEC_ELT (list1, i);
3463 tree parm2 = TREE_VEC_ELT (list2, i);
3464 if (!template_parameters_equivalent_p (parm1, parm2))
3465 return false;
3466 }
3467
3468 p1 = TREE_CHAIN (p1);
3469 p2 = TREE_CHAIN (p2);
3470 }
3471
3472 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3473 return false;
3474
3475 return true;
3476 }
3477
3478 /* Return true if the requires-clause of the template parameter lists are
3479 equivalent and false otherwise. */
3480 static bool
3481 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3482 {
3483 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3484 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3485 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3486 return false;
3487 if (!cp_tree_equal (req1, req2))
3488 return false;
3489 return true;
3490 }
3491
3492 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3493 Two template heads are equivalent if their template parameter
3494 lists are equivalent and their requires clauses are equivalent.
3495
3496 In pre-C++20, this is equivalent to calling comp_template_parms
3497 for the template parameters of TMPL1 and TMPL2. */
3498
3499 bool
3500 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3501 {
3502 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3503 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3504
3505 /* Don't change the matching rules for pre-C++20. */
3506 if (cxx_dialect < cxx20)
3507 return comp_template_parms (parms1, parms2);
3508
3509 /* ... have the same number of template parameters, and their
3510 corresponding parameters are equivalent. */
3511 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3512 return false;
3513
3514 /* ... if either has a requires-clause, they both do and their
3515 corresponding constraint-expressions are equivalent. */
3516 return template_requirements_equivalent_p (parms1, parms2);
3517 }
3518
3519 /* Determine whether PARM is a parameter pack. */
3520
3521 bool
3522 template_parameter_pack_p (const_tree parm)
3523 {
3524 /* Determine if we have a non-type template parameter pack. */
3525 if (TREE_CODE (parm) == PARM_DECL)
3526 return (DECL_TEMPLATE_PARM_P (parm)
3527 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3528 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3529 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3530
3531 /* If this is a list of template parameters, we could get a
3532 TYPE_DECL or a TEMPLATE_DECL. */
3533 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3534 parm = TREE_TYPE (parm);
3535
3536 /* Otherwise it must be a type template parameter. */
3537 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3538 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3539 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3540 }
3541
3542 /* Determine if T is a function parameter pack. */
3543
3544 bool
3545 function_parameter_pack_p (const_tree t)
3546 {
3547 if (t && TREE_CODE (t) == PARM_DECL)
3548 return DECL_PACK_P (t);
3549 return false;
3550 }
3551
3552 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3553 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3554
3555 tree
3556 get_function_template_decl (const_tree primary_func_tmpl_inst)
3557 {
3558 if (! primary_func_tmpl_inst
3559 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3560 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3561 return NULL;
3562
3563 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3564 }
3565
3566 /* Return true iff the function parameter PARAM_DECL was expanded
3567 from the function parameter pack PACK. */
3568
3569 bool
3570 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3571 {
3572 if (DECL_ARTIFICIAL (param_decl)
3573 || !function_parameter_pack_p (pack))
3574 return false;
3575
3576 /* The parameter pack and its pack arguments have the same
3577 DECL_PARM_INDEX. */
3578 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3579 }
3580
3581 /* Determine whether ARGS describes a variadic template args list,
3582 i.e., one that is terminated by a template argument pack. */
3583
3584 static bool
3585 template_args_variadic_p (tree args)
3586 {
3587 int nargs;
3588 tree last_parm;
3589
3590 if (args == NULL_TREE)
3591 return false;
3592
3593 args = INNERMOST_TEMPLATE_ARGS (args);
3594 nargs = TREE_VEC_LENGTH (args);
3595
3596 if (nargs == 0)
3597 return false;
3598
3599 last_parm = TREE_VEC_ELT (args, nargs - 1);
3600
3601 return ARGUMENT_PACK_P (last_parm);
3602 }
3603
3604 /* Generate a new name for the parameter pack name NAME (an
3605 IDENTIFIER_NODE) that incorporates its */
3606
3607 static tree
3608 make_ith_pack_parameter_name (tree name, int i)
3609 {
3610 /* Munge the name to include the parameter index. */
3611 #define NUMBUF_LEN 128
3612 char numbuf[NUMBUF_LEN];
3613 char* newname;
3614 int newname_len;
3615
3616 if (name == NULL_TREE)
3617 return name;
3618 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3619 newname_len = IDENTIFIER_LENGTH (name)
3620 + strlen (numbuf) + 2;
3621 newname = (char*)alloca (newname_len);
3622 snprintf (newname, newname_len,
3623 "%s#%i", IDENTIFIER_POINTER (name), i);
3624 return get_identifier (newname);
3625 }
3626
3627 /* Return true if T is a primary function, class or alias template
3628 specialization, not including the template pattern. */
3629
3630 bool
3631 primary_template_specialization_p (const_tree t)
3632 {
3633 if (!t)
3634 return false;
3635
3636 if (VAR_OR_FUNCTION_DECL_P (t))
3637 return (DECL_LANG_SPECIFIC (t)
3638 && DECL_USE_TEMPLATE (t)
3639 && DECL_TEMPLATE_INFO (t)
3640 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3641 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3642 return (CLASSTYPE_TEMPLATE_INFO (t)
3643 && CLASSTYPE_USE_TEMPLATE (t)
3644 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3645 else if (alias_template_specialization_p (t, nt_transparent))
3646 return true;
3647 return false;
3648 }
3649
3650 /* Return true if PARM is a template template parameter. */
3651
3652 bool
3653 template_template_parameter_p (const_tree parm)
3654 {
3655 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3656 }
3657
3658 /* Return true iff PARM is a DECL representing a type template
3659 parameter. */
3660
3661 bool
3662 template_type_parameter_p (const_tree parm)
3663 {
3664 return (parm
3665 && (TREE_CODE (parm) == TYPE_DECL
3666 || TREE_CODE (parm) == TEMPLATE_DECL)
3667 && DECL_TEMPLATE_PARM_P (parm));
3668 }
3669
3670 /* Return the template parameters of T if T is a
3671 primary template instantiation, NULL otherwise. */
3672
3673 tree
3674 get_primary_template_innermost_parameters (const_tree t)
3675 {
3676 tree parms = NULL, template_info = NULL;
3677
3678 if ((template_info = get_template_info (t))
3679 && primary_template_specialization_p (t))
3680 parms = INNERMOST_TEMPLATE_PARMS
3681 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3682
3683 return parms;
3684 }
3685
3686 /* Return the template parameters of the LEVELth level from the full list
3687 of template parameters PARMS. */
3688
3689 tree
3690 get_template_parms_at_level (tree parms, int level)
3691 {
3692 tree p;
3693 if (!parms
3694 || TREE_CODE (parms) != TREE_LIST
3695 || level > TMPL_PARMS_DEPTH (parms))
3696 return NULL_TREE;
3697
3698 for (p = parms; p; p = TREE_CHAIN (p))
3699 if (TMPL_PARMS_DEPTH (p) == level)
3700 return p;
3701
3702 return NULL_TREE;
3703 }
3704
3705 /* Returns the template arguments of T if T is a template instantiation,
3706 NULL otherwise. */
3707
3708 tree
3709 get_template_innermost_arguments (const_tree t)
3710 {
3711 tree args = NULL, template_info = NULL;
3712
3713 if ((template_info = get_template_info (t))
3714 && TI_ARGS (template_info))
3715 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3716
3717 return args;
3718 }
3719
3720 /* Return the argument pack elements of T if T is a template argument pack,
3721 NULL otherwise. */
3722
3723 tree
3724 get_template_argument_pack_elems (const_tree t)
3725 {
3726 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3727 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3728 return NULL;
3729
3730 return ARGUMENT_PACK_ARGS (t);
3731 }
3732
3733 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3734 ARGUMENT_PACK_SELECT represents. */
3735
3736 static tree
3737 argument_pack_select_arg (tree t)
3738 {
3739 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3740 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3741
3742 /* If the selected argument is an expansion E, that most likely means we were
3743 called from gen_elem_of_pack_expansion_instantiation during the
3744 substituting of an argument pack (of which the Ith element is a pack
3745 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3746 In this case, the Ith element resulting from this substituting is going to
3747 be a pack expansion, which pattern is the pattern of E. Let's return the
3748 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3749 resulting pack expansion from it. */
3750 if (PACK_EXPANSION_P (arg))
3751 {
3752 /* Make sure we aren't throwing away arg info. */
3753 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3754 arg = PACK_EXPANSION_PATTERN (arg);
3755 }
3756
3757 return arg;
3758 }
3759
3760
3761 /* True iff FN is a function representing a built-in variadic parameter
3762 pack. */
3763
3764 bool
3765 builtin_pack_fn_p (tree fn)
3766 {
3767 if (!fn
3768 || TREE_CODE (fn) != FUNCTION_DECL
3769 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3770 return false;
3771
3772 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3773 return true;
3774
3775 return false;
3776 }
3777
3778 /* True iff CALL is a call to a function representing a built-in variadic
3779 parameter pack. */
3780
3781 static bool
3782 builtin_pack_call_p (tree call)
3783 {
3784 if (TREE_CODE (call) != CALL_EXPR)
3785 return false;
3786 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3787 }
3788
3789 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3790
3791 static tree
3792 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3793 tree in_decl)
3794 {
3795 tree ohi = CALL_EXPR_ARG (call, 0);
3796 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3797 false/*fn*/, true/*int_cst*/);
3798
3799 if (value_dependent_expression_p (hi))
3800 {
3801 if (hi != ohi)
3802 {
3803 call = copy_node (call);
3804 CALL_EXPR_ARG (call, 0) = hi;
3805 }
3806 tree ex = make_pack_expansion (call, complain);
3807 tree vec = make_tree_vec (1);
3808 TREE_VEC_ELT (vec, 0) = ex;
3809 return vec;
3810 }
3811 else
3812 {
3813 hi = cxx_constant_value (hi);
3814 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3815
3816 /* Calculate the largest value of len that won't make the size of the vec
3817 overflow an int. The compiler will exceed resource limits long before
3818 this, but it seems a decent place to diagnose. */
3819 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3820
3821 if (len < 0 || len > max)
3822 {
3823 if ((complain & tf_error)
3824 && hi != error_mark_node)
3825 error ("argument to %<__integer_pack%> must be between 0 and %d",
3826 max);
3827 return error_mark_node;
3828 }
3829
3830 tree vec = make_tree_vec (len);
3831
3832 for (int i = 0; i < len; ++i)
3833 TREE_VEC_ELT (vec, i) = size_int (i);
3834
3835 return vec;
3836 }
3837 }
3838
3839 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3840 CALL. */
3841
3842 static tree
3843 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3844 tree in_decl)
3845 {
3846 if (!builtin_pack_call_p (call))
3847 return NULL_TREE;
3848
3849 tree fn = CALL_EXPR_FN (call);
3850
3851 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3852 return expand_integer_pack (call, args, complain, in_decl);
3853
3854 return NULL_TREE;
3855 }
3856
3857 /* Structure used to track the progress of find_parameter_packs_r. */
3858 struct find_parameter_pack_data
3859 {
3860 /* TREE_LIST that will contain all of the parameter packs found by
3861 the traversal. */
3862 tree* parameter_packs;
3863
3864 /* Set of AST nodes that have been visited by the traversal. */
3865 hash_set<tree> *visited;
3866
3867 /* True iff we're making a type pack expansion. */
3868 bool type_pack_expansion_p;
3869 };
3870
3871 /* Identifies all of the argument packs that occur in a template
3872 argument and appends them to the TREE_LIST inside DATA, which is a
3873 find_parameter_pack_data structure. This is a subroutine of
3874 make_pack_expansion and uses_parameter_packs. */
3875 static tree
3876 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3877 {
3878 tree t = *tp;
3879 struct find_parameter_pack_data* ppd =
3880 (struct find_parameter_pack_data*)data;
3881 bool parameter_pack_p = false;
3882
3883 /* Don't look through typedefs; we are interested in whether a
3884 parameter pack is actually written in the expression/type we're
3885 looking at, not the target type. */
3886 if (TYPE_P (t) && typedef_variant_p (t))
3887 {
3888 /* But do look at arguments for an alias template. */
3889 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3890 cp_walk_tree (&TI_ARGS (tinfo),
3891 &find_parameter_packs_r,
3892 ppd, ppd->visited);
3893 *walk_subtrees = 0;
3894 return NULL_TREE;
3895 }
3896
3897 /* Identify whether this is a parameter pack or not. */
3898 switch (TREE_CODE (t))
3899 {
3900 case TEMPLATE_PARM_INDEX:
3901 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3902 parameter_pack_p = true;
3903 break;
3904
3905 case TEMPLATE_TYPE_PARM:
3906 t = TYPE_MAIN_VARIANT (t);
3907 /* FALLTHRU */
3908 case TEMPLATE_TEMPLATE_PARM:
3909 /* If the placeholder appears in the decl-specifier-seq of a function
3910 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3911 is a pack expansion, the invented template parameter is a template
3912 parameter pack. */
3913 if (ppd->type_pack_expansion_p && is_auto (t))
3914 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3915 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3916 parameter_pack_p = true;
3917 break;
3918
3919 case FIELD_DECL:
3920 case PARM_DECL:
3921 if (DECL_PACK_P (t))
3922 {
3923 /* We don't want to walk into the type of a PARM_DECL,
3924 because we don't want to see the type parameter pack. */
3925 *walk_subtrees = 0;
3926 parameter_pack_p = true;
3927 }
3928 break;
3929
3930 case VAR_DECL:
3931 if (DECL_PACK_P (t))
3932 {
3933 /* We don't want to walk into the type of a variadic capture proxy,
3934 because we don't want to see the type parameter pack. */
3935 *walk_subtrees = 0;
3936 parameter_pack_p = true;
3937 }
3938 else if (variable_template_specialization_p (t))
3939 {
3940 cp_walk_tree (&DECL_TI_ARGS (t),
3941 find_parameter_packs_r,
3942 ppd, ppd->visited);
3943 *walk_subtrees = 0;
3944 }
3945 break;
3946
3947 case CALL_EXPR:
3948 if (builtin_pack_call_p (t))
3949 parameter_pack_p = true;
3950 break;
3951
3952 case BASES:
3953 parameter_pack_p = true;
3954 break;
3955 default:
3956 /* Not a parameter pack. */
3957 break;
3958 }
3959
3960 if (parameter_pack_p)
3961 {
3962 /* Add this parameter pack to the list. */
3963 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3964 }
3965
3966 if (TYPE_P (t))
3967 cp_walk_tree (&TYPE_CONTEXT (t),
3968 &find_parameter_packs_r, ppd, ppd->visited);
3969
3970 /* This switch statement will return immediately if we don't find a
3971 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3972 switch (TREE_CODE (t))
3973 {
3974 case BOUND_TEMPLATE_TEMPLATE_PARM:
3975 /* Check the template itself. */
3976 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3977 &find_parameter_packs_r, ppd, ppd->visited);
3978 return NULL_TREE;
3979
3980 case DECL_EXPR:
3981 {
3982 tree decl = DECL_EXPR_DECL (t);
3983 /* Ignore the declaration of a capture proxy for a parameter pack. */
3984 if (is_capture_proxy (decl))
3985 *walk_subtrees = 0;
3986 if (is_typedef_decl (decl))
3987 /* Since we stop at typedefs above, we need to look through them at
3988 the point of the DECL_EXPR. */
3989 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3990 &find_parameter_packs_r, ppd, ppd->visited);
3991 return NULL_TREE;
3992 }
3993
3994 case TEMPLATE_DECL:
3995 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3996 return NULL_TREE;
3997 cp_walk_tree (&TREE_TYPE (t),
3998 &find_parameter_packs_r, ppd, ppd->visited);
3999 return NULL_TREE;
4000
4001 case TYPE_PACK_EXPANSION:
4002 case EXPR_PACK_EXPANSION:
4003 *walk_subtrees = 0;
4004 return NULL_TREE;
4005
4006 case INTEGER_TYPE:
4007 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4008 ppd, ppd->visited);
4009 *walk_subtrees = 0;
4010 return NULL_TREE;
4011
4012 case IDENTIFIER_NODE:
4013 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4014 ppd->visited);
4015 *walk_subtrees = 0;
4016 return NULL_TREE;
4017
4018 case LAMBDA_EXPR:
4019 {
4020 /* Since we defer implicit capture, look in the parms and body. */
4021 tree fn = lambda_function (t);
4022 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4023 ppd->visited);
4024 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4025 ppd->visited);
4026 return NULL_TREE;
4027 }
4028
4029 case DECLTYPE_TYPE:
4030 {
4031 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4032 type_pack_expansion_p to false so that any placeholders
4033 within the expression don't get marked as parameter packs. */
4034 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4035 ppd->type_pack_expansion_p = false;
4036 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4037 ppd, ppd->visited);
4038 ppd->type_pack_expansion_p = type_pack_expansion_p;
4039 *walk_subtrees = 0;
4040 return NULL_TREE;
4041 }
4042
4043 case IF_STMT:
4044 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4045 ppd, ppd->visited);
4046 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4047 ppd, ppd->visited);
4048 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4049 ppd, ppd->visited);
4050 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4051 *walk_subtrees = 0;
4052 return NULL_TREE;
4053
4054 default:
4055 return NULL_TREE;
4056 }
4057
4058 return NULL_TREE;
4059 }
4060
4061 /* Determines if the expression or type T uses any parameter packs. */
4062 tree
4063 uses_parameter_packs (tree t)
4064 {
4065 tree parameter_packs = NULL_TREE;
4066 struct find_parameter_pack_data ppd;
4067 ppd.parameter_packs = &parameter_packs;
4068 ppd.visited = new hash_set<tree>;
4069 ppd.type_pack_expansion_p = false;
4070 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4071 delete ppd.visited;
4072 return parameter_packs;
4073 }
4074
4075 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4076 representation a base-class initializer into a parameter pack
4077 expansion. If all goes well, the resulting node will be an
4078 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4079 respectively. */
4080 tree
4081 make_pack_expansion (tree arg, tsubst_flags_t complain)
4082 {
4083 tree result;
4084 tree parameter_packs = NULL_TREE;
4085 bool for_types = false;
4086 struct find_parameter_pack_data ppd;
4087
4088 if (!arg || arg == error_mark_node)
4089 return arg;
4090
4091 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4092 {
4093 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4094 class initializer. In this case, the TREE_PURPOSE will be a
4095 _TYPE node (representing the base class expansion we're
4096 initializing) and the TREE_VALUE will be a TREE_LIST
4097 containing the initialization arguments.
4098
4099 The resulting expansion looks somewhat different from most
4100 expansions. Rather than returning just one _EXPANSION, we
4101 return a TREE_LIST whose TREE_PURPOSE is a
4102 TYPE_PACK_EXPANSION containing the bases that will be
4103 initialized. The TREE_VALUE will be identical to the
4104 original TREE_VALUE, which is a list of arguments that will
4105 be passed to each base. We do not introduce any new pack
4106 expansion nodes into the TREE_VALUE (although it is possible
4107 that some already exist), because the TREE_PURPOSE and
4108 TREE_VALUE all need to be expanded together with the same
4109 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4110 resulting TREE_PURPOSE will mention the parameter packs in
4111 both the bases and the arguments to the bases. */
4112 tree purpose;
4113 tree value;
4114 tree parameter_packs = NULL_TREE;
4115
4116 /* Determine which parameter packs will be used by the base
4117 class expansion. */
4118 ppd.visited = new hash_set<tree>;
4119 ppd.parameter_packs = &parameter_packs;
4120 ppd.type_pack_expansion_p = false;
4121 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4122 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4123 &ppd, ppd.visited);
4124
4125 if (parameter_packs == NULL_TREE)
4126 {
4127 if (complain & tf_error)
4128 error ("base initializer expansion %qT contains no parameter packs",
4129 arg);
4130 delete ppd.visited;
4131 return error_mark_node;
4132 }
4133
4134 if (TREE_VALUE (arg) != void_type_node)
4135 {
4136 /* Collect the sets of parameter packs used in each of the
4137 initialization arguments. */
4138 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4139 {
4140 /* Determine which parameter packs will be expanded in this
4141 argument. */
4142 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4143 &ppd, ppd.visited);
4144 }
4145 }
4146
4147 delete ppd.visited;
4148
4149 /* Create the pack expansion type for the base type. */
4150 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4151 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4152 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4153 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4154
4155 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4156 they will rarely be compared to anything. */
4157 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4158
4159 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4160 }
4161
4162 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4163 for_types = true;
4164
4165 /* Build the PACK_EXPANSION_* node. */
4166 result = for_types
4167 ? cxx_make_type (TYPE_PACK_EXPANSION)
4168 : make_node (EXPR_PACK_EXPANSION);
4169 SET_PACK_EXPANSION_PATTERN (result, arg);
4170 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4171 {
4172 /* Propagate type and const-expression information. */
4173 TREE_TYPE (result) = TREE_TYPE (arg);
4174 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4175 /* Mark this read now, since the expansion might be length 0. */
4176 mark_exp_read (arg);
4177 }
4178 else
4179 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4180 they will rarely be compared to anything. */
4181 SET_TYPE_STRUCTURAL_EQUALITY (result);
4182
4183 /* Determine which parameter packs will be expanded. */
4184 ppd.parameter_packs = &parameter_packs;
4185 ppd.visited = new hash_set<tree>;
4186 ppd.type_pack_expansion_p = TYPE_P (arg);
4187 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4188 delete ppd.visited;
4189
4190 /* Make sure we found some parameter packs. */
4191 if (parameter_packs == NULL_TREE)
4192 {
4193 if (complain & tf_error)
4194 {
4195 if (TYPE_P (arg))
4196 error ("expansion pattern %qT contains no parameter packs", arg);
4197 else
4198 error ("expansion pattern %qE contains no parameter packs", arg);
4199 }
4200 return error_mark_node;
4201 }
4202 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4203
4204 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4205
4206 return result;
4207 }
4208
4209 /* Checks T for any "bare" parameter packs, which have not yet been
4210 expanded, and issues an error if any are found. This operation can
4211 only be done on full expressions or types (e.g., an expression
4212 statement, "if" condition, etc.), because we could have expressions like:
4213
4214 foo(f(g(h(args)))...)
4215
4216 where "args" is a parameter pack. check_for_bare_parameter_packs
4217 should not be called for the subexpressions args, h(args),
4218 g(h(args)), or f(g(h(args))), because we would produce erroneous
4219 error messages.
4220
4221 Returns TRUE and emits an error if there were bare parameter packs,
4222 returns FALSE otherwise. */
4223 bool
4224 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4225 {
4226 tree parameter_packs = NULL_TREE;
4227 struct find_parameter_pack_data ppd;
4228
4229 if (!processing_template_decl || !t || t == error_mark_node)
4230 return false;
4231
4232 if (TREE_CODE (t) == TYPE_DECL)
4233 t = TREE_TYPE (t);
4234
4235 ppd.parameter_packs = &parameter_packs;
4236 ppd.visited = new hash_set<tree>;
4237 ppd.type_pack_expansion_p = false;
4238 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4239 delete ppd.visited;
4240
4241 /* It's OK for a lambda to have an unexpanded parameter pack from the
4242 containing context, but do complain about unexpanded capture packs. */
4243 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4244 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4245 for (; parameter_packs;
4246 parameter_packs = TREE_CHAIN (parameter_packs))
4247 {
4248 tree pack = TREE_VALUE (parameter_packs);
4249 if (is_capture_proxy (pack))
4250 break;
4251 }
4252
4253 if (parameter_packs)
4254 {
4255 if (loc == UNKNOWN_LOCATION)
4256 loc = cp_expr_loc_or_input_loc (t);
4257 error_at (loc, "parameter packs not expanded with %<...%>:");
4258 while (parameter_packs)
4259 {
4260 tree pack = TREE_VALUE (parameter_packs);
4261 tree name = NULL_TREE;
4262
4263 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4264 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4265 name = TYPE_NAME (pack);
4266 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4267 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4268 else if (TREE_CODE (pack) == CALL_EXPR)
4269 name = DECL_NAME (CALL_EXPR_FN (pack));
4270 else
4271 name = DECL_NAME (pack);
4272
4273 if (name)
4274 inform (loc, " %qD", name);
4275 else
4276 inform (loc, " %s", "<anonymous>");
4277
4278 parameter_packs = TREE_CHAIN (parameter_packs);
4279 }
4280
4281 return true;
4282 }
4283
4284 return false;
4285 }
4286
4287 /* Expand any parameter packs that occur in the template arguments in
4288 ARGS. */
4289 tree
4290 expand_template_argument_pack (tree args)
4291 {
4292 if (args == error_mark_node)
4293 return error_mark_node;
4294
4295 tree result_args = NULL_TREE;
4296 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4297 int num_result_args = -1;
4298 int non_default_args_count = -1;
4299
4300 /* First, determine if we need to expand anything, and the number of
4301 slots we'll need. */
4302 for (in_arg = 0; in_arg < nargs; ++in_arg)
4303 {
4304 tree arg = TREE_VEC_ELT (args, in_arg);
4305 if (arg == NULL_TREE)
4306 return args;
4307 if (ARGUMENT_PACK_P (arg))
4308 {
4309 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4310 if (num_result_args < 0)
4311 num_result_args = in_arg + num_packed;
4312 else
4313 num_result_args += num_packed;
4314 }
4315 else
4316 {
4317 if (num_result_args >= 0)
4318 num_result_args++;
4319 }
4320 }
4321
4322 /* If no expansion is necessary, we're done. */
4323 if (num_result_args < 0)
4324 return args;
4325
4326 /* Expand arguments. */
4327 result_args = make_tree_vec (num_result_args);
4328 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4329 non_default_args_count =
4330 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4331 for (in_arg = 0; in_arg < nargs; ++in_arg)
4332 {
4333 tree arg = TREE_VEC_ELT (args, in_arg);
4334 if (ARGUMENT_PACK_P (arg))
4335 {
4336 tree packed = ARGUMENT_PACK_ARGS (arg);
4337 int i, num_packed = TREE_VEC_LENGTH (packed);
4338 for (i = 0; i < num_packed; ++i, ++out_arg)
4339 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4340 if (non_default_args_count > 0)
4341 non_default_args_count += num_packed - 1;
4342 }
4343 else
4344 {
4345 TREE_VEC_ELT (result_args, out_arg) = arg;
4346 ++out_arg;
4347 }
4348 }
4349 if (non_default_args_count >= 0)
4350 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4351 return result_args;
4352 }
4353
4354 /* Checks if DECL shadows a template parameter.
4355
4356 [temp.local]: A template-parameter shall not be redeclared within its
4357 scope (including nested scopes).
4358
4359 Emits an error and returns TRUE if the DECL shadows a parameter,
4360 returns FALSE otherwise. */
4361
4362 bool
4363 check_template_shadow (tree decl)
4364 {
4365 tree olddecl;
4366
4367 /* If we're not in a template, we can't possibly shadow a template
4368 parameter. */
4369 if (!current_template_parms)
4370 return true;
4371
4372 /* Figure out what we're shadowing. */
4373 decl = OVL_FIRST (decl);
4374 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4375
4376 /* If there's no previous binding for this name, we're not shadowing
4377 anything, let alone a template parameter. */
4378 if (!olddecl)
4379 return true;
4380
4381 /* If we're not shadowing a template parameter, we're done. Note
4382 that OLDDECL might be an OVERLOAD (or perhaps even an
4383 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4384 node. */
4385 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4386 return true;
4387
4388 /* We check for decl != olddecl to avoid bogus errors for using a
4389 name inside a class. We check TPFI to avoid duplicate errors for
4390 inline member templates. */
4391 if (decl == olddecl
4392 || (DECL_TEMPLATE_PARM_P (decl)
4393 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4394 return true;
4395
4396 /* Don't complain about the injected class name, as we've already
4397 complained about the class itself. */
4398 if (DECL_SELF_REFERENCE_P (decl))
4399 return false;
4400
4401 if (DECL_TEMPLATE_PARM_P (decl))
4402 error ("declaration of template parameter %q+D shadows "
4403 "template parameter", decl);
4404 else
4405 error ("declaration of %q+#D shadows template parameter", decl);
4406 inform (DECL_SOURCE_LOCATION (olddecl),
4407 "template parameter %qD declared here", olddecl);
4408 return false;
4409 }
4410
4411 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4412 ORIG_LEVEL, DECL, and TYPE. */
4413
4414 static tree
4415 build_template_parm_index (int index,
4416 int level,
4417 int orig_level,
4418 tree decl,
4419 tree type)
4420 {
4421 tree t = make_node (TEMPLATE_PARM_INDEX);
4422 TEMPLATE_PARM_IDX (t) = index;
4423 TEMPLATE_PARM_LEVEL (t) = level;
4424 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4425 TEMPLATE_PARM_DECL (t) = decl;
4426 TREE_TYPE (t) = type;
4427 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4428 TREE_READONLY (t) = TREE_READONLY (decl);
4429
4430 return t;
4431 }
4432
4433 /* Find the canonical type parameter for the given template type
4434 parameter. Returns the canonical type parameter, which may be TYPE
4435 if no such parameter existed. */
4436
4437 tree
4438 canonical_type_parameter (tree type)
4439 {
4440 int idx = TEMPLATE_TYPE_IDX (type);
4441
4442 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4443
4444 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4445 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4446
4447 for (tree list = (*canonical_template_parms)[idx];
4448 list; list = TREE_CHAIN (list))
4449 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4450 return TREE_VALUE (list);
4451
4452 (*canonical_template_parms)[idx]
4453 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4454 return type;
4455 }
4456
4457 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4458 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4459 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4460 new one is created. */
4461
4462 static tree
4463 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4464 tsubst_flags_t complain)
4465 {
4466 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4467 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4468 != TEMPLATE_PARM_LEVEL (index) - levels)
4469 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4470 {
4471 tree orig_decl = TEMPLATE_PARM_DECL (index);
4472
4473 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4474 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4475 type);
4476 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4477 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4478 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4479 DECL_ARTIFICIAL (decl) = 1;
4480 SET_DECL_TEMPLATE_PARM_P (decl);
4481
4482 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4483 TEMPLATE_PARM_LEVEL (index) - levels,
4484 TEMPLATE_PARM_ORIG_LEVEL (index),
4485 decl, type);
4486 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4487 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4488 = TEMPLATE_PARM_PARAMETER_PACK (index);
4489
4490 /* Template template parameters need this. */
4491 tree inner = decl;
4492 if (TREE_CODE (decl) == TEMPLATE_DECL)
4493 {
4494 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4495 TYPE_DECL, DECL_NAME (decl), type);
4496 DECL_TEMPLATE_RESULT (decl) = inner;
4497 DECL_ARTIFICIAL (inner) = true;
4498 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4499 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4500 }
4501
4502 /* Attach the TPI to the decl. */
4503 if (TREE_CODE (inner) == TYPE_DECL)
4504 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4505 else
4506 DECL_INITIAL (decl) = tpi;
4507 }
4508
4509 return TEMPLATE_PARM_DESCENDANTS (index);
4510 }
4511
4512 /* Process information from new template parameter PARM and append it
4513 to the LIST being built. This new parameter is a non-type
4514 parameter iff IS_NON_TYPE is true. This new parameter is a
4515 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4516 is in PARM_LOC. */
4517
4518 tree
4519 process_template_parm (tree list, location_t parm_loc, tree parm,
4520 bool is_non_type, bool is_parameter_pack)
4521 {
4522 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4523 tree prev = NULL_TREE;
4524 int idx = 0;
4525
4526 if (list)
4527 {
4528 prev = tree_last (list);
4529
4530 tree p = TREE_VALUE (prev);
4531 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4532 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4533 else if (TREE_CODE (p) == PARM_DECL)
4534 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4535
4536 ++idx;
4537 }
4538
4539 tree decl = NULL_TREE;
4540 tree defval = TREE_PURPOSE (parm);
4541 tree constr = TREE_TYPE (parm);
4542
4543 if (is_non_type)
4544 {
4545 parm = TREE_VALUE (parm);
4546
4547 SET_DECL_TEMPLATE_PARM_P (parm);
4548
4549 if (TREE_TYPE (parm) != error_mark_node)
4550 {
4551 /* [temp.param]
4552
4553 The top-level cv-qualifiers on the template-parameter are
4554 ignored when determining its type. */
4555 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4556 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4557 TREE_TYPE (parm) = error_mark_node;
4558 else if (uses_parameter_packs (TREE_TYPE (parm))
4559 && !is_parameter_pack
4560 /* If we're in a nested template parameter list, the template
4561 template parameter could be a parameter pack. */
4562 && processing_template_parmlist == 1)
4563 {
4564 /* This template parameter is not a parameter pack, but it
4565 should be. Complain about "bare" parameter packs. */
4566 check_for_bare_parameter_packs (TREE_TYPE (parm));
4567
4568 /* Recover by calling this a parameter pack. */
4569 is_parameter_pack = true;
4570 }
4571 }
4572
4573 /* A template parameter is not modifiable. */
4574 TREE_CONSTANT (parm) = 1;
4575 TREE_READONLY (parm) = 1;
4576 decl = build_decl (parm_loc,
4577 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4578 TREE_CONSTANT (decl) = 1;
4579 TREE_READONLY (decl) = 1;
4580 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4581 = build_template_parm_index (idx, processing_template_decl,
4582 processing_template_decl,
4583 decl, TREE_TYPE (parm));
4584
4585 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4586 = is_parameter_pack;
4587 }
4588 else
4589 {
4590 tree t;
4591 parm = TREE_VALUE (TREE_VALUE (parm));
4592
4593 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4594 {
4595 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4596 /* This is for distinguishing between real templates and template
4597 template parameters */
4598 TREE_TYPE (parm) = t;
4599
4600 /* any_template_parm_r expects to be able to get the targs of a
4601 DECL_TEMPLATE_RESULT. */
4602 tree result = DECL_TEMPLATE_RESULT (parm);
4603 TREE_TYPE (result) = t;
4604 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4605 tree tinfo = build_template_info (parm, args);
4606 retrofit_lang_decl (result);
4607 DECL_TEMPLATE_INFO (result) = tinfo;
4608
4609 decl = parm;
4610 }
4611 else
4612 {
4613 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4614 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4615 decl = build_decl (parm_loc,
4616 TYPE_DECL, parm, t);
4617 }
4618
4619 TYPE_NAME (t) = decl;
4620 TYPE_STUB_DECL (t) = decl;
4621 parm = decl;
4622 TEMPLATE_TYPE_PARM_INDEX (t)
4623 = build_template_parm_index (idx, processing_template_decl,
4624 processing_template_decl,
4625 decl, TREE_TYPE (parm));
4626 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4627 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4628 SET_TYPE_STRUCTURAL_EQUALITY (t);
4629 else
4630 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4631 }
4632 DECL_ARTIFICIAL (decl) = 1;
4633 SET_DECL_TEMPLATE_PARM_P (decl);
4634
4635 /* Build requirements for the type/template parameter.
4636 This must be done after SET_DECL_TEMPLATE_PARM_P or
4637 process_template_parm could fail. */
4638 tree reqs = finish_shorthand_constraint (parm, constr);
4639
4640 decl = pushdecl (decl);
4641 if (!is_non_type)
4642 parm = decl;
4643
4644 /* Build the parameter node linking the parameter declaration,
4645 its default argument (if any), and its constraints (if any). */
4646 parm = build_tree_list (defval, parm);
4647 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4648
4649 if (prev)
4650 TREE_CHAIN (prev) = parm;
4651 else
4652 list = parm;
4653
4654 return list;
4655 }
4656
4657 /* The end of a template parameter list has been reached. Process the
4658 tree list into a parameter vector, converting each parameter into a more
4659 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4660 as PARM_DECLs. */
4661
4662 tree
4663 end_template_parm_list (tree parms)
4664 {
4665 tree saved_parmlist = make_tree_vec (list_length (parms));
4666
4667 /* Pop the dummy parameter level and add the real one. We do not
4668 morph the dummy parameter in place, as it might have been
4669 captured by a (nested) template-template-parm. */
4670 current_template_parms = TREE_CHAIN (current_template_parms);
4671
4672 current_template_parms
4673 = tree_cons (size_int (processing_template_decl),
4674 saved_parmlist, current_template_parms);
4675
4676 for (unsigned ix = 0; parms; ix++)
4677 {
4678 tree parm = parms;
4679 parms = TREE_CHAIN (parms);
4680 TREE_CHAIN (parm) = NULL_TREE;
4681
4682 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4683 }
4684
4685 --processing_template_parmlist;
4686
4687 return saved_parmlist;
4688 }
4689
4690 // Explicitly indicate the end of the template parameter list. We assume
4691 // that the current template parameters have been constructed and/or
4692 // managed explicitly, as when creating new template template parameters
4693 // from a shorthand constraint.
4694 void
4695 end_template_parm_list ()
4696 {
4697 --processing_template_parmlist;
4698 }
4699
4700 /* end_template_decl is called after a template declaration is seen. */
4701
4702 void
4703 end_template_decl (void)
4704 {
4705 reset_specialization ();
4706
4707 if (! processing_template_decl)
4708 return;
4709
4710 /* This matches the pushlevel in begin_template_parm_list. */
4711 finish_scope ();
4712
4713 --processing_template_decl;
4714 current_template_parms = TREE_CHAIN (current_template_parms);
4715 }
4716
4717 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4718 thereof, and converts it into an argument suitable to be passed to
4719 the type substitution functions. Note that if the TREE_LIST contains
4720 an error_mark node, the returned argument is error_mark_node. */
4721
4722 tree
4723 template_parm_to_arg (tree t)
4724 {
4725 if (!t)
4726 return NULL_TREE;
4727
4728 if (TREE_CODE (t) == TREE_LIST)
4729 t = TREE_VALUE (t);
4730
4731 if (error_operand_p (t))
4732 return error_mark_node;
4733
4734 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4735 {
4736 if (TREE_CODE (t) == TYPE_DECL
4737 || TREE_CODE (t) == TEMPLATE_DECL)
4738 t = TREE_TYPE (t);
4739 else
4740 t = DECL_INITIAL (t);
4741 }
4742
4743 gcc_assert (TEMPLATE_PARM_P (t));
4744
4745 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4746 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4747 {
4748 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4749 {
4750 /* Turn this argument into a TYPE_ARGUMENT_PACK
4751 with a single element, which expands T. */
4752 tree vec = make_tree_vec (1);
4753 if (CHECKING_P)
4754 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4755
4756 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4757
4758 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4759 SET_ARGUMENT_PACK_ARGS (t, vec);
4760 }
4761 }
4762 else
4763 {
4764 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4765 {
4766 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4767 with a single element, which expands T. */
4768 tree vec = make_tree_vec (1);
4769 if (CHECKING_P)
4770 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4771
4772 t = convert_from_reference (t);
4773 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4774
4775 t = make_node (NONTYPE_ARGUMENT_PACK);
4776 SET_ARGUMENT_PACK_ARGS (t, vec);
4777 }
4778 else
4779 t = convert_from_reference (t);
4780 }
4781 return t;
4782 }
4783
4784 /* Given a single level of template parameters (a TREE_VEC), return it
4785 as a set of template arguments. */
4786
4787 tree
4788 template_parms_level_to_args (tree parms)
4789 {
4790 tree a = copy_node (parms);
4791 TREE_TYPE (a) = NULL_TREE;
4792 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4793 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4794
4795 if (CHECKING_P)
4796 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4797
4798 return a;
4799 }
4800
4801 /* Given a set of template parameters, return them as a set of template
4802 arguments. The template parameters are represented as a TREE_VEC, in
4803 the form documented in cp-tree.h for template arguments. */
4804
4805 tree
4806 template_parms_to_args (tree parms)
4807 {
4808 tree header;
4809 tree args = NULL_TREE;
4810 int length = TMPL_PARMS_DEPTH (parms);
4811 int l = length;
4812
4813 /* If there is only one level of template parameters, we do not
4814 create a TREE_VEC of TREE_VECs. Instead, we return a single
4815 TREE_VEC containing the arguments. */
4816 if (length > 1)
4817 args = make_tree_vec (length);
4818
4819 for (header = parms; header; header = TREE_CHAIN (header))
4820 {
4821 tree a = template_parms_level_to_args (TREE_VALUE (header));
4822
4823 if (length > 1)
4824 TREE_VEC_ELT (args, --l) = a;
4825 else
4826 args = a;
4827 }
4828
4829 return args;
4830 }
4831
4832 /* Within the declaration of a template, return the currently active
4833 template parameters as an argument TREE_VEC. */
4834
4835 static tree
4836 current_template_args (void)
4837 {
4838 return template_parms_to_args (current_template_parms);
4839 }
4840
4841 /* Return the fully generic arguments for of TMPL, i.e. what
4842 current_template_args would be while parsing it. */
4843
4844 tree
4845 generic_targs_for (tree tmpl)
4846 {
4847 if (tmpl == NULL_TREE)
4848 return NULL_TREE;
4849 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4850 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4851 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4852 template parameter, it has no TEMPLATE_INFO; for a partial
4853 specialization, it has the arguments for the primary template, and we
4854 want the arguments for the partial specialization. */;
4855 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4856 if (tree ti = get_template_info (result))
4857 return TI_ARGS (ti);
4858 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4859 }
4860
4861 /* Update the declared TYPE by doing any lookups which were thought to be
4862 dependent, but are not now that we know the SCOPE of the declarator. */
4863
4864 tree
4865 maybe_update_decl_type (tree orig_type, tree scope)
4866 {
4867 tree type = orig_type;
4868
4869 if (type == NULL_TREE)
4870 return type;
4871
4872 if (TREE_CODE (orig_type) == TYPE_DECL)
4873 type = TREE_TYPE (type);
4874
4875 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4876 && dependent_type_p (type)
4877 /* Don't bother building up the args in this case. */
4878 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4879 {
4880 /* tsubst in the args corresponding to the template parameters,
4881 including auto if present. Most things will be unchanged, but
4882 make_typename_type and tsubst_qualified_id will resolve
4883 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4884 tree args = current_template_args ();
4885 tree auto_node = type_uses_auto (type);
4886 tree pushed;
4887 if (auto_node)
4888 {
4889 tree auto_vec = make_tree_vec (1);
4890 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4891 args = add_to_template_args (args, auto_vec);
4892 }
4893 pushed = push_scope (scope);
4894 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4895 if (pushed)
4896 pop_scope (scope);
4897 }
4898
4899 if (type == error_mark_node)
4900 return orig_type;
4901
4902 if (TREE_CODE (orig_type) == TYPE_DECL)
4903 {
4904 if (same_type_p (type, TREE_TYPE (orig_type)))
4905 type = orig_type;
4906 else
4907 type = TYPE_NAME (type);
4908 }
4909 return type;
4910 }
4911
4912 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4913 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4914 the new template is a member template. */
4915
4916 static tree
4917 build_template_decl (tree decl, tree parms, bool member_template_p)
4918 {
4919 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4920 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4921 DECL_TEMPLATE_PARMS (tmpl) = parms;
4922 DECL_TEMPLATE_RESULT (tmpl) = decl;
4923 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4924 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4925 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4926 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4927
4928 if (modules_p ())
4929 {
4930 /* Propagate module information from the decl. */
4931 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
4932 if (DECL_LANG_SPECIFIC (decl))
4933 {
4934 DECL_MODULE_PURVIEW_P (tmpl) = DECL_MODULE_PURVIEW_P (decl);
4935 gcc_checking_assert (!DECL_MODULE_IMPORT_P (decl));
4936 }
4937 }
4938
4939 return tmpl;
4940 }
4941
4942 struct template_parm_data
4943 {
4944 /* The level of the template parameters we are currently
4945 processing. */
4946 int level;
4947
4948 /* The index of the specialization argument we are currently
4949 processing. */
4950 int current_arg;
4951
4952 /* An array whose size is the number of template parameters. The
4953 elements are nonzero if the parameter has been used in any one
4954 of the arguments processed so far. */
4955 int* parms;
4956
4957 /* An array whose size is the number of template arguments. The
4958 elements are nonzero if the argument makes use of template
4959 parameters of this level. */
4960 int* arg_uses_template_parms;
4961 };
4962
4963 /* Subroutine of push_template_decl used to see if each template
4964 parameter in a partial specialization is used in the explicit
4965 argument list. If T is of the LEVEL given in DATA (which is
4966 treated as a template_parm_data*), then DATA->PARMS is marked
4967 appropriately. */
4968
4969 static int
4970 mark_template_parm (tree t, void* data)
4971 {
4972 int level;
4973 int idx;
4974 struct template_parm_data* tpd = (struct template_parm_data*) data;
4975
4976 template_parm_level_and_index (t, &level, &idx);
4977
4978 if (level == tpd->level)
4979 {
4980 tpd->parms[idx] = 1;
4981 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4982 }
4983
4984 /* In C++17 the type of a non-type argument is a deduced context. */
4985 if (cxx_dialect >= cxx17
4986 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4987 for_each_template_parm (TREE_TYPE (t),
4988 &mark_template_parm,
4989 data,
4990 NULL,
4991 /*include_nondeduced_p=*/false);
4992
4993 /* Return zero so that for_each_template_parm will continue the
4994 traversal of the tree; we want to mark *every* template parm. */
4995 return 0;
4996 }
4997
4998 /* Process the partial specialization DECL. */
4999
5000 static tree
5001 process_partial_specialization (tree decl)
5002 {
5003 tree type = TREE_TYPE (decl);
5004 tree tinfo = get_template_info (decl);
5005 tree maintmpl = TI_TEMPLATE (tinfo);
5006 tree specargs = TI_ARGS (tinfo);
5007 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5008 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5009 tree inner_parms;
5010 tree inst;
5011 int nargs = TREE_VEC_LENGTH (inner_args);
5012 int ntparms;
5013 int i;
5014 bool did_error_intro = false;
5015 struct template_parm_data tpd;
5016 struct template_parm_data tpd2;
5017
5018 gcc_assert (current_template_parms);
5019
5020 /* A concept cannot be specialized. */
5021 if (flag_concepts && variable_concept_p (maintmpl))
5022 {
5023 error ("specialization of variable concept %q#D", maintmpl);
5024 return error_mark_node;
5025 }
5026
5027 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5028 ntparms = TREE_VEC_LENGTH (inner_parms);
5029
5030 /* We check that each of the template parameters given in the
5031 partial specialization is used in the argument list to the
5032 specialization. For example:
5033
5034 template <class T> struct S;
5035 template <class T> struct S<T*>;
5036
5037 The second declaration is OK because `T*' uses the template
5038 parameter T, whereas
5039
5040 template <class T> struct S<int>;
5041
5042 is no good. Even trickier is:
5043
5044 template <class T>
5045 struct S1
5046 {
5047 template <class U>
5048 struct S2;
5049 template <class U>
5050 struct S2<T>;
5051 };
5052
5053 The S2<T> declaration is actually invalid; it is a
5054 full-specialization. Of course,
5055
5056 template <class U>
5057 struct S2<T (*)(U)>;
5058
5059 or some such would have been OK. */
5060 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5061 tpd.parms = XALLOCAVEC (int, ntparms);
5062 memset (tpd.parms, 0, sizeof (int) * ntparms);
5063
5064 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5065 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5066 for (i = 0; i < nargs; ++i)
5067 {
5068 tpd.current_arg = i;
5069 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5070 &mark_template_parm,
5071 &tpd,
5072 NULL,
5073 /*include_nondeduced_p=*/false);
5074 }
5075 for (i = 0; i < ntparms; ++i)
5076 if (tpd.parms[i] == 0)
5077 {
5078 /* One of the template parms was not used in a deduced context in the
5079 specialization. */
5080 if (!did_error_intro)
5081 {
5082 error ("template parameters not deducible in "
5083 "partial specialization:");
5084 did_error_intro = true;
5085 }
5086
5087 inform (input_location, " %qD",
5088 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5089 }
5090
5091 if (did_error_intro)
5092 return error_mark_node;
5093
5094 /* [temp.class.spec]
5095
5096 The argument list of the specialization shall not be identical to
5097 the implicit argument list of the primary template. */
5098 tree main_args
5099 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5100 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5101 && (!flag_concepts
5102 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5103 {
5104 if (!flag_concepts)
5105 error ("partial specialization %q+D does not specialize "
5106 "any template arguments; to define the primary template, "
5107 "remove the template argument list", decl);
5108 else
5109 error ("partial specialization %q+D does not specialize any "
5110 "template arguments and is not more constrained than "
5111 "the primary template; to define the primary template, "
5112 "remove the template argument list", decl);
5113 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5114 }
5115
5116 /* A partial specialization that replaces multiple parameters of the
5117 primary template with a pack expansion is less specialized for those
5118 parameters. */
5119 if (nargs < DECL_NTPARMS (maintmpl))
5120 {
5121 error ("partial specialization is not more specialized than the "
5122 "primary template because it replaces multiple parameters "
5123 "with a pack expansion");
5124 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5125 /* Avoid crash in process_partial_specialization. */
5126 return decl;
5127 }
5128
5129 else if (nargs > DECL_NTPARMS (maintmpl))
5130 {
5131 error ("too many arguments for partial specialization %qT", type);
5132 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5133 /* Avoid crash below. */
5134 return decl;
5135 }
5136
5137 /* If we aren't in a dependent class, we can actually try deduction. */
5138 else if (tpd.level == 1
5139 /* FIXME we should be able to handle a partial specialization of a
5140 partial instantiation, but currently we can't (c++/41727). */
5141 && TMPL_ARGS_DEPTH (specargs) == 1
5142 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5143 {
5144 auto_diagnostic_group d;
5145 if (permerror (input_location, "partial specialization %qD is not "
5146 "more specialized than", decl))
5147 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5148 maintmpl);
5149 }
5150
5151 /* [temp.class.spec]
5152
5153 A partially specialized non-type argument expression shall not
5154 involve template parameters of the partial specialization except
5155 when the argument expression is a simple identifier.
5156
5157 The type of a template parameter corresponding to a specialized
5158 non-type argument shall not be dependent on a parameter of the
5159 specialization.
5160
5161 Also, we verify that pack expansions only occur at the
5162 end of the argument list. */
5163 tpd2.parms = 0;
5164 for (i = 0; i < nargs; ++i)
5165 {
5166 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5167 tree arg = TREE_VEC_ELT (inner_args, i);
5168 tree packed_args = NULL_TREE;
5169 int j, len = 1;
5170
5171 if (ARGUMENT_PACK_P (arg))
5172 {
5173 /* Extract the arguments from the argument pack. We'll be
5174 iterating over these in the following loop. */
5175 packed_args = ARGUMENT_PACK_ARGS (arg);
5176 len = TREE_VEC_LENGTH (packed_args);
5177 }
5178
5179 for (j = 0; j < len; j++)
5180 {
5181 if (packed_args)
5182 /* Get the Jth argument in the parameter pack. */
5183 arg = TREE_VEC_ELT (packed_args, j);
5184
5185 if (PACK_EXPANSION_P (arg))
5186 {
5187 /* Pack expansions must come at the end of the
5188 argument list. */
5189 if ((packed_args && j < len - 1)
5190 || (!packed_args && i < nargs - 1))
5191 {
5192 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5193 error ("parameter pack argument %qE must be at the "
5194 "end of the template argument list", arg);
5195 else
5196 error ("parameter pack argument %qT must be at the "
5197 "end of the template argument list", arg);
5198 }
5199 }
5200
5201 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5202 /* We only care about the pattern. */
5203 arg = PACK_EXPANSION_PATTERN (arg);
5204
5205 if (/* These first two lines are the `non-type' bit. */
5206 !TYPE_P (arg)
5207 && TREE_CODE (arg) != TEMPLATE_DECL
5208 /* This next two lines are the `argument expression is not just a
5209 simple identifier' condition and also the `specialized
5210 non-type argument' bit. */
5211 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5212 && !((REFERENCE_REF_P (arg)
5213 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5214 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5215 {
5216 if ((!packed_args && tpd.arg_uses_template_parms[i])
5217 || (packed_args && uses_template_parms (arg)))
5218 error_at (cp_expr_loc_or_input_loc (arg),
5219 "template argument %qE involves template "
5220 "parameter(s)", arg);
5221 else
5222 {
5223 /* Look at the corresponding template parameter,
5224 marking which template parameters its type depends
5225 upon. */
5226 tree type = TREE_TYPE (parm);
5227
5228 if (!tpd2.parms)
5229 {
5230 /* We haven't yet initialized TPD2. Do so now. */
5231 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5232 /* The number of parameters here is the number in the
5233 main template, which, as checked in the assertion
5234 above, is NARGS. */
5235 tpd2.parms = XALLOCAVEC (int, nargs);
5236 tpd2.level =
5237 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5238 }
5239
5240 /* Mark the template parameters. But this time, we're
5241 looking for the template parameters of the main
5242 template, not in the specialization. */
5243 tpd2.current_arg = i;
5244 tpd2.arg_uses_template_parms[i] = 0;
5245 memset (tpd2.parms, 0, sizeof (int) * nargs);
5246 for_each_template_parm (type,
5247 &mark_template_parm,
5248 &tpd2,
5249 NULL,
5250 /*include_nondeduced_p=*/false);
5251
5252 if (tpd2.arg_uses_template_parms [i])
5253 {
5254 /* The type depended on some template parameters.
5255 If they are fully specialized in the
5256 specialization, that's OK. */
5257 int j;
5258 int count = 0;
5259 for (j = 0; j < nargs; ++j)
5260 if (tpd2.parms[j] != 0
5261 && tpd.arg_uses_template_parms [j])
5262 ++count;
5263 if (count != 0)
5264 error_n (input_location, count,
5265 "type %qT of template argument %qE depends "
5266 "on a template parameter",
5267 "type %qT of template argument %qE depends "
5268 "on template parameters",
5269 type,
5270 arg);
5271 }
5272 }
5273 }
5274 }
5275 }
5276
5277 /* We should only get here once. */
5278 if (TREE_CODE (decl) == TYPE_DECL)
5279 gcc_assert (!COMPLETE_TYPE_P (type));
5280
5281 // Build the template decl.
5282 tree tmpl = build_template_decl (decl, current_template_parms,
5283 DECL_MEMBER_TEMPLATE_P (maintmpl));
5284 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5285 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5286 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5287
5288 /* Give template template parms a DECL_CONTEXT of the template
5289 for which they are a parameter. */
5290 for (i = 0; i < ntparms; ++i)
5291 {
5292 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5293 if (TREE_CODE (parm) == TEMPLATE_DECL)
5294 DECL_CONTEXT (parm) = tmpl;
5295 }
5296
5297 if (VAR_P (decl))
5298 /* We didn't register this in check_explicit_specialization so we could
5299 wait until the constraints were set. */
5300 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5301 else
5302 associate_classtype_constraints (type);
5303
5304 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5305 = tree_cons (specargs, tmpl,
5306 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5307 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5308
5309 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5310 inst = TREE_CHAIN (inst))
5311 {
5312 tree instance = TREE_VALUE (inst);
5313 if (TYPE_P (instance)
5314 ? (COMPLETE_TYPE_P (instance)
5315 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5316 : DECL_TEMPLATE_INSTANTIATION (instance))
5317 {
5318 tree spec = most_specialized_partial_spec (instance, tf_none);
5319 tree inst_decl = (DECL_P (instance)
5320 ? instance : TYPE_NAME (instance));
5321 if (!spec)
5322 /* OK */;
5323 else if (spec == error_mark_node)
5324 permerror (input_location,
5325 "declaration of %qD ambiguates earlier template "
5326 "instantiation for %qD", decl, inst_decl);
5327 else if (TREE_VALUE (spec) == tmpl)
5328 permerror (input_location,
5329 "partial specialization of %qD after instantiation "
5330 "of %qD", decl, inst_decl);
5331 }
5332 }
5333
5334 return decl;
5335 }
5336
5337 /* PARM is a template parameter of some form; return the corresponding
5338 TEMPLATE_PARM_INDEX. */
5339
5340 static tree
5341 get_template_parm_index (tree parm)
5342 {
5343 if (TREE_CODE (parm) == PARM_DECL
5344 || TREE_CODE (parm) == CONST_DECL)
5345 parm = DECL_INITIAL (parm);
5346 else if (TREE_CODE (parm) == TYPE_DECL
5347 || TREE_CODE (parm) == TEMPLATE_DECL)
5348 parm = TREE_TYPE (parm);
5349 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5350 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5351 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5352 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5353 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5354 return parm;
5355 }
5356
5357 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5358 parameter packs used by the template parameter PARM. */
5359
5360 static void
5361 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5362 {
5363 /* A type parm can't refer to another parm. */
5364 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5365 return;
5366 else if (TREE_CODE (parm) == PARM_DECL)
5367 {
5368 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5369 ppd, ppd->visited);
5370 return;
5371 }
5372
5373 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5374
5375 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5376 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5377 {
5378 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5379 if (template_parameter_pack_p (p))
5380 /* Any packs in the type are expanded by this parameter. */;
5381 else
5382 fixed_parameter_pack_p_1 (p, ppd);
5383 }
5384 }
5385
5386 /* PARM is a template parameter pack. Return any parameter packs used in
5387 its type or the type of any of its template parameters. If there are
5388 any such packs, it will be instantiated into a fixed template parameter
5389 list by partial instantiation rather than be fully deduced. */
5390
5391 tree
5392 fixed_parameter_pack_p (tree parm)
5393 {
5394 /* This can only be true in a member template. */
5395 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5396 return NULL_TREE;
5397 /* This can only be true for a parameter pack. */
5398 if (!template_parameter_pack_p (parm))
5399 return NULL_TREE;
5400 /* A type parm can't refer to another parm. */
5401 if (TREE_CODE (parm) == TYPE_DECL)
5402 return NULL_TREE;
5403
5404 tree parameter_packs = NULL_TREE;
5405 struct find_parameter_pack_data ppd;
5406 ppd.parameter_packs = &parameter_packs;
5407 ppd.visited = new hash_set<tree>;
5408 ppd.type_pack_expansion_p = false;
5409
5410 fixed_parameter_pack_p_1 (parm, &ppd);
5411
5412 delete ppd.visited;
5413 return parameter_packs;
5414 }
5415
5416 /* Check that a template declaration's use of default arguments and
5417 parameter packs is not invalid. Here, PARMS are the template
5418 parameters. IS_PRIMARY is true if DECL is the thing declared by
5419 a primary template. IS_PARTIAL is true if DECL is a partial
5420 specialization.
5421
5422 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5423 function template declaration or a friend class template
5424 declaration. In the function case, 1 indicates a declaration, 2
5425 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5426 emitted for extraneous default arguments.
5427
5428 Returns TRUE if there were no errors found, FALSE otherwise. */
5429
5430 bool
5431 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5432 bool is_partial, int is_friend_decl)
5433 {
5434 const char *msg;
5435 int last_level_to_check;
5436 tree parm_level;
5437 bool no_errors = true;
5438
5439 /* [temp.param]
5440
5441 A default template-argument shall not be specified in a
5442 function template declaration or a function template definition, nor
5443 in the template-parameter-list of the definition of a member of a
5444 class template. */
5445
5446 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5447 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5448 /* You can't have a function template declaration in a local
5449 scope, nor you can you define a member of a class template in a
5450 local scope. */
5451 return true;
5452
5453 if ((TREE_CODE (decl) == TYPE_DECL
5454 && TREE_TYPE (decl)
5455 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5456 || (TREE_CODE (decl) == FUNCTION_DECL
5457 && LAMBDA_FUNCTION_P (decl)))
5458 /* A lambda doesn't have an explicit declaration; don't complain
5459 about the parms of the enclosing class. */
5460 return true;
5461
5462 if (current_class_type
5463 && !TYPE_BEING_DEFINED (current_class_type)
5464 && DECL_LANG_SPECIFIC (decl)
5465 && DECL_DECLARES_FUNCTION_P (decl)
5466 /* If this is either a friend defined in the scope of the class
5467 or a member function. */
5468 && (DECL_FUNCTION_MEMBER_P (decl)
5469 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5470 : DECL_FRIEND_CONTEXT (decl)
5471 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5472 : false)
5473 /* And, if it was a member function, it really was defined in
5474 the scope of the class. */
5475 && (!DECL_FUNCTION_MEMBER_P (decl)
5476 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5477 /* We already checked these parameters when the template was
5478 declared, so there's no need to do it again now. This function
5479 was defined in class scope, but we're processing its body now
5480 that the class is complete. */
5481 return true;
5482
5483 /* Core issue 226 (C++0x only): the following only applies to class
5484 templates. */
5485 if (is_primary
5486 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5487 {
5488 /* [temp.param]
5489
5490 If a template-parameter has a default template-argument, all
5491 subsequent template-parameters shall have a default
5492 template-argument supplied. */
5493 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5494 {
5495 tree inner_parms = TREE_VALUE (parm_level);
5496 int ntparms = TREE_VEC_LENGTH (inner_parms);
5497 int seen_def_arg_p = 0;
5498 int i;
5499
5500 for (i = 0; i < ntparms; ++i)
5501 {
5502 tree parm = TREE_VEC_ELT (inner_parms, i);
5503
5504 if (parm == error_mark_node)
5505 continue;
5506
5507 if (TREE_PURPOSE (parm))
5508 seen_def_arg_p = 1;
5509 else if (seen_def_arg_p
5510 && !template_parameter_pack_p (TREE_VALUE (parm)))
5511 {
5512 error ("no default argument for %qD", TREE_VALUE (parm));
5513 /* For better subsequent error-recovery, we indicate that
5514 there should have been a default argument. */
5515 TREE_PURPOSE (parm) = error_mark_node;
5516 no_errors = false;
5517 }
5518 else if (!is_partial
5519 && !is_friend_decl
5520 /* Don't complain about an enclosing partial
5521 specialization. */
5522 && parm_level == parms
5523 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5524 && i < ntparms - 1
5525 && template_parameter_pack_p (TREE_VALUE (parm))
5526 /* A fixed parameter pack will be partially
5527 instantiated into a fixed length list. */
5528 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5529 {
5530 /* A primary class template, primary variable template
5531 (DR 2032), or alias template can only have one
5532 parameter pack, at the end of the template
5533 parameter list. */
5534
5535 error ("parameter pack %q+D must be at the end of the"
5536 " template parameter list", TREE_VALUE (parm));
5537
5538 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5539 = error_mark_node;
5540 no_errors = false;
5541 }
5542 }
5543 }
5544 }
5545
5546 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5547 || is_partial
5548 || !is_primary
5549 || is_friend_decl)
5550 /* For an ordinary class template, default template arguments are
5551 allowed at the innermost level, e.g.:
5552 template <class T = int>
5553 struct S {};
5554 but, in a partial specialization, they're not allowed even
5555 there, as we have in [temp.class.spec]:
5556
5557 The template parameter list of a specialization shall not
5558 contain default template argument values.
5559
5560 So, for a partial specialization, or for a function template
5561 (in C++98/C++03), we look at all of them. */
5562 ;
5563 else
5564 /* But, for a primary class template that is not a partial
5565 specialization we look at all template parameters except the
5566 innermost ones. */
5567 parms = TREE_CHAIN (parms);
5568
5569 /* Figure out what error message to issue. */
5570 if (is_friend_decl == 2)
5571 msg = G_("default template arguments may not be used in function template "
5572 "friend re-declaration");
5573 else if (is_friend_decl)
5574 msg = G_("default template arguments may not be used in template "
5575 "friend declarations");
5576 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5577 msg = G_("default template arguments may not be used in function templates "
5578 "without %<-std=c++11%> or %<-std=gnu++11%>");
5579 else if (is_partial)
5580 msg = G_("default template arguments may not be used in "
5581 "partial specializations");
5582 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5583 msg = G_("default argument for template parameter for class enclosing %qD");
5584 else
5585 /* Per [temp.param]/9, "A default template-argument shall not be
5586 specified in the template-parameter-lists of the definition of
5587 a member of a class template that appears outside of the member's
5588 class.", thus if we aren't handling a member of a class template
5589 there is no need to examine the parameters. */
5590 return true;
5591
5592 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5593 /* If we're inside a class definition, there's no need to
5594 examine the parameters to the class itself. On the one
5595 hand, they will be checked when the class is defined, and,
5596 on the other, default arguments are valid in things like:
5597 template <class T = double>
5598 struct S { template <class U> void f(U); };
5599 Here the default argument for `S' has no bearing on the
5600 declaration of `f'. */
5601 last_level_to_check = template_class_depth (current_class_type) + 1;
5602 else
5603 /* Check everything. */
5604 last_level_to_check = 0;
5605
5606 for (parm_level = parms;
5607 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5608 parm_level = TREE_CHAIN (parm_level))
5609 {
5610 tree inner_parms = TREE_VALUE (parm_level);
5611 int i;
5612 int ntparms;
5613
5614 ntparms = TREE_VEC_LENGTH (inner_parms);
5615 for (i = 0; i < ntparms; ++i)
5616 {
5617 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5618 continue;
5619
5620 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5621 {
5622 if (msg)
5623 {
5624 no_errors = false;
5625 if (is_friend_decl == 2)
5626 return no_errors;
5627
5628 error (msg, decl);
5629 msg = 0;
5630 }
5631
5632 /* Clear out the default argument so that we are not
5633 confused later. */
5634 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5635 }
5636 }
5637
5638 /* At this point, if we're still interested in issuing messages,
5639 they must apply to classes surrounding the object declared. */
5640 if (msg)
5641 msg = G_("default argument for template parameter for class "
5642 "enclosing %qD");
5643 }
5644
5645 return no_errors;
5646 }
5647
5648 /* Worker for push_template_decl_real, called via
5649 for_each_template_parm. DATA is really an int, indicating the
5650 level of the parameters we are interested in. If T is a template
5651 parameter of that level, return nonzero. */
5652
5653 static int
5654 template_parm_this_level_p (tree t, void* data)
5655 {
5656 int this_level = *(int *)data;
5657 int level;
5658
5659 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5660 level = TEMPLATE_PARM_LEVEL (t);
5661 else
5662 level = TEMPLATE_TYPE_LEVEL (t);
5663 return level == this_level;
5664 }
5665
5666 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5667 DATA is really an int, indicating the innermost outer level of parameters.
5668 If T is a template parameter of that level or further out, return
5669 nonzero. */
5670
5671 static int
5672 template_parm_outer_level (tree t, void *data)
5673 {
5674 int this_level = *(int *)data;
5675 int level;
5676
5677 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5678 level = TEMPLATE_PARM_LEVEL (t);
5679 else
5680 level = TEMPLATE_TYPE_LEVEL (t);
5681 return level <= this_level;
5682 }
5683
5684 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5685 parameters given by current_template_args, or reuses a
5686 previously existing one, if appropriate. Returns the DECL, or an
5687 equivalent one, if it is replaced via a call to duplicate_decls.
5688
5689 If IS_FRIEND is true, DECL is a friend declaration. */
5690
5691 tree
5692 push_template_decl (tree decl, bool is_friend)
5693 {
5694 if (decl == error_mark_node || !current_template_parms)
5695 return error_mark_node;
5696
5697 /* See if this is a partial specialization. */
5698 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5699 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5700 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5701 || (VAR_P (decl)
5702 && DECL_LANG_SPECIFIC (decl)
5703 && DECL_TEMPLATE_SPECIALIZATION (decl)
5704 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5705
5706 /* No surprising friend functions. */
5707 gcc_checking_assert (is_friend
5708 || !(TREE_CODE (decl) == FUNCTION_DECL
5709 && DECL_UNIQUE_FRIEND_P (decl)));
5710
5711 tree ctx;
5712 if (is_friend)
5713 /* For a friend, we want the context of the friend, not
5714 the type of which it is a friend. */
5715 ctx = CP_DECL_CONTEXT (decl);
5716 else if (CP_DECL_CONTEXT (decl)
5717 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5718 /* In the case of a virtual function, we want the class in which
5719 it is defined. */
5720 ctx = CP_DECL_CONTEXT (decl);
5721 else
5722 /* Otherwise, if we're currently defining some class, the DECL
5723 is assumed to be a member of the class. */
5724 ctx = current_scope ();
5725
5726 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5727 ctx = NULL_TREE;
5728
5729 if (!DECL_CONTEXT (decl))
5730 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5731
5732 /* See if this is a primary template. */
5733 bool is_primary = false;
5734 if (is_friend && ctx
5735 && uses_template_parms_level (ctx, processing_template_decl))
5736 /* A friend template that specifies a class context, i.e.
5737 template <typename T> friend void A<T>::f();
5738 is not primary. */
5739 ;
5740 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5741 /* Lambdas are not primary. */
5742 ;
5743 else
5744 is_primary = template_parm_scope_p ();
5745
5746 /* True if the template is a member template, in the sense of
5747 [temp.mem]. */
5748 bool member_template_p = false;
5749
5750 if (is_primary)
5751 {
5752 warning (OPT_Wtemplates, "template %qD declared", decl);
5753
5754 if (DECL_CLASS_SCOPE_P (decl))
5755 member_template_p = true;
5756
5757 if (TREE_CODE (decl) == TYPE_DECL
5758 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5759 {
5760 error ("template class without a name");
5761 return error_mark_node;
5762 }
5763 else if (TREE_CODE (decl) == FUNCTION_DECL)
5764 {
5765 if (member_template_p)
5766 {
5767 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5768 error ("member template %qD may not have virt-specifiers", decl);
5769 }
5770 if (DECL_DESTRUCTOR_P (decl))
5771 {
5772 /* [temp.mem]
5773
5774 A destructor shall not be a member template. */
5775 error_at (DECL_SOURCE_LOCATION (decl),
5776 "destructor %qD declared as member template", decl);
5777 return error_mark_node;
5778 }
5779 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5780 && (!prototype_p (TREE_TYPE (decl))
5781 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5782 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5783 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5784 == void_list_node)))
5785 {
5786 /* [basic.stc.dynamic.allocation]
5787
5788 An allocation function can be a function
5789 template. ... Template allocation functions shall
5790 have two or more parameters. */
5791 error ("invalid template declaration of %qD", decl);
5792 return error_mark_node;
5793 }
5794 }
5795 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5796 && CLASS_TYPE_P (TREE_TYPE (decl)))
5797 {
5798 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5799 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5800 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5801 {
5802 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5803 if (TREE_CODE (t) == TYPE_DECL)
5804 t = TREE_TYPE (t);
5805 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5806 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5807 }
5808 }
5809 else if (TREE_CODE (decl) == TYPE_DECL
5810 && TYPE_DECL_ALIAS_P (decl))
5811 /* alias-declaration */
5812 gcc_assert (!DECL_ARTIFICIAL (decl));
5813 else if (VAR_P (decl))
5814 /* C++14 variable template. */;
5815 else if (TREE_CODE (decl) == CONCEPT_DECL)
5816 /* C++20 concept definitions. */;
5817 else
5818 {
5819 error ("template declaration of %q#D", decl);
5820 return error_mark_node;
5821 }
5822 }
5823
5824 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5825 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5826 || (VAR_OR_FUNCTION_DECL_P (decl)
5827 && DECL_LOCAL_DECL_P (decl))));
5828
5829 /* Check to see that the rules regarding the use of default
5830 arguments are not being violated. We check args for a friend
5831 functions when we know whether it's a definition, introducing
5832 declaration or re-declaration. */
5833 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5834 check_default_tmpl_args (decl, current_template_parms,
5835 is_primary, is_partial, is_friend);
5836
5837 /* Ensure that there are no parameter packs in the type of this
5838 declaration that have not been expanded. */
5839 if (TREE_CODE (decl) == FUNCTION_DECL)
5840 {
5841 /* Check each of the arguments individually to see if there are
5842 any bare parameter packs. */
5843 tree type = TREE_TYPE (decl);
5844 tree arg = DECL_ARGUMENTS (decl);
5845 tree argtype = TYPE_ARG_TYPES (type);
5846
5847 while (arg && argtype)
5848 {
5849 if (!DECL_PACK_P (arg)
5850 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5851 {
5852 /* This is a PARM_DECL that contains unexpanded parameter
5853 packs. We have already complained about this in the
5854 check_for_bare_parameter_packs call, so just replace
5855 these types with ERROR_MARK_NODE. */
5856 TREE_TYPE (arg) = error_mark_node;
5857 TREE_VALUE (argtype) = error_mark_node;
5858 }
5859
5860 arg = DECL_CHAIN (arg);
5861 argtype = TREE_CHAIN (argtype);
5862 }
5863
5864 /* Check for bare parameter packs in the return type and the
5865 exception specifiers. */
5866 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5867 /* Errors were already issued, set return type to int
5868 as the frontend doesn't expect error_mark_node as
5869 the return type. */
5870 TREE_TYPE (type) = integer_type_node;
5871 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5872 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5873 }
5874 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5875 ? DECL_ORIGINAL_TYPE (decl)
5876 : TREE_TYPE (decl)))
5877 {
5878 TREE_TYPE (decl) = error_mark_node;
5879 return error_mark_node;
5880 }
5881
5882 if (is_partial)
5883 return process_partial_specialization (decl);
5884
5885 tree args = current_template_args ();
5886 tree tmpl = NULL_TREE;
5887 bool new_template_p = false;
5888 if (local_p)
5889 {
5890 /* Does not get a template head. */
5891 tmpl = NULL_TREE;
5892 gcc_checking_assert (!is_primary);
5893 }
5894 else if (!ctx
5895 || TREE_CODE (ctx) == FUNCTION_DECL
5896 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5897 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5898 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5899 && DECL_TEMPLATE_INFO (decl))))
5900 {
5901 if (DECL_LANG_SPECIFIC (decl)
5902 && DECL_TEMPLATE_INFO (decl)
5903 && DECL_TI_TEMPLATE (decl))
5904 tmpl = DECL_TI_TEMPLATE (decl);
5905 /* If DECL is a TYPE_DECL for a class-template, then there won't
5906 be DECL_LANG_SPECIFIC. The information equivalent to
5907 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5908 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5909 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5910 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5911 {
5912 /* Since a template declaration already existed for this
5913 class-type, we must be redeclaring it here. Make sure
5914 that the redeclaration is valid. */
5915 redeclare_class_template (TREE_TYPE (decl),
5916 current_template_parms,
5917 current_template_constraints ());
5918 /* We don't need to create a new TEMPLATE_DECL; just use the
5919 one we already had. */
5920 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5921 }
5922 else
5923 {
5924 tmpl = build_template_decl (decl, current_template_parms,
5925 member_template_p);
5926 new_template_p = true;
5927
5928 if (DECL_LANG_SPECIFIC (decl)
5929 && DECL_TEMPLATE_SPECIALIZATION (decl))
5930 {
5931 /* A specialization of a member template of a template
5932 class. */
5933 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5934 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5935 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5936 }
5937 }
5938 }
5939 else
5940 {
5941 tree a, t, current, parms;
5942 int i;
5943 tree tinfo = get_template_info (decl);
5944
5945 if (!tinfo)
5946 {
5947 error ("template definition of non-template %q#D", decl);
5948 return error_mark_node;
5949 }
5950
5951 tmpl = TI_TEMPLATE (tinfo);
5952
5953 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5954 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5955 && DECL_TEMPLATE_SPECIALIZATION (decl)
5956 && DECL_MEMBER_TEMPLATE_P (tmpl))
5957 {
5958 /* The declaration is a specialization of a member
5959 template, declared outside the class. Therefore, the
5960 innermost template arguments will be NULL, so we
5961 replace them with the arguments determined by the
5962 earlier call to check_explicit_specialization. */
5963 args = DECL_TI_ARGS (decl);
5964
5965 tree new_tmpl
5966 = build_template_decl (decl, current_template_parms,
5967 member_template_p);
5968 DECL_TI_TEMPLATE (decl) = new_tmpl;
5969 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5970 DECL_TEMPLATE_INFO (new_tmpl)
5971 = build_template_info (tmpl, args);
5972
5973 register_specialization (new_tmpl,
5974 most_general_template (tmpl),
5975 args,
5976 is_friend, 0);
5977 return decl;
5978 }
5979
5980 /* Make sure the template headers we got make sense. */
5981
5982 parms = DECL_TEMPLATE_PARMS (tmpl);
5983 i = TMPL_PARMS_DEPTH (parms);
5984 if (TMPL_ARGS_DEPTH (args) != i)
5985 {
5986 error ("expected %d levels of template parms for %q#D, got %d",
5987 i, decl, TMPL_ARGS_DEPTH (args));
5988 DECL_INTERFACE_KNOWN (decl) = 1;
5989 return error_mark_node;
5990 }
5991 else
5992 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5993 {
5994 a = TMPL_ARGS_LEVEL (args, i);
5995 t = INNERMOST_TEMPLATE_PARMS (parms);
5996
5997 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5998 {
5999 if (current == decl)
6000 error ("got %d template parameters for %q#D",
6001 TREE_VEC_LENGTH (a), decl);
6002 else
6003 error ("got %d template parameters for %q#T",
6004 TREE_VEC_LENGTH (a), current);
6005 error (" but %d required", TREE_VEC_LENGTH (t));
6006 /* Avoid crash in import_export_decl. */
6007 DECL_INTERFACE_KNOWN (decl) = 1;
6008 return error_mark_node;
6009 }
6010
6011 if (current == decl)
6012 current = ctx;
6013 else if (current == NULL_TREE)
6014 /* Can happen in erroneous input. */
6015 break;
6016 else
6017 current = get_containing_scope (current);
6018 }
6019
6020 /* Check that the parms are used in the appropriate qualifying scopes
6021 in the declarator. */
6022 if (!comp_template_args
6023 (TI_ARGS (tinfo),
6024 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6025 {
6026 error ("template arguments to %qD do not match original "
6027 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6028 if (!uses_template_parms (TI_ARGS (tinfo)))
6029 inform (input_location, "use %<template<>%> for"
6030 " an explicit specialization");
6031 /* Avoid crash in import_export_decl. */
6032 DECL_INTERFACE_KNOWN (decl) = 1;
6033 return error_mark_node;
6034 }
6035 }
6036
6037 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6038
6039 if (new_template_p)
6040 {
6041 /* Push template declarations for global functions and types.
6042 Note that we do not try to push a global template friend
6043 declared in a template class; such a thing may well depend on
6044 the template parameters of the class and we'll push it when
6045 instantiating the befriending class. */
6046 if (!ctx
6047 && !(is_friend && template_class_depth (current_class_type) > 0))
6048 {
6049 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6050 if (pushed == error_mark_node)
6051 return error_mark_node;
6052
6053 /* pushdecl may have found an existing template. */
6054 if (pushed != tmpl)
6055 {
6056 decl = DECL_TEMPLATE_RESULT (pushed);
6057 tmpl = NULL_TREE;
6058 }
6059 }
6060 else if (is_friend)
6061 {
6062 /* Record this decl as belonging to the current class. It's
6063 not chained onto anything else. */
6064 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6065 gcc_checking_assert (!DECL_CHAIN (tmpl));
6066 DECL_CHAIN (tmpl) = current_scope ();
6067 }
6068 }
6069 else if (tmpl)
6070 /* The type may have been completed, or (erroneously) changed. */
6071 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6072
6073 if (tmpl)
6074 {
6075 if (is_primary)
6076 {
6077 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6078
6079 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6080
6081 /* Give template template parms a DECL_CONTEXT of the template
6082 for which they are a parameter. */
6083 parms = INNERMOST_TEMPLATE_PARMS (parms);
6084 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6085 {
6086 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6087 if (TREE_CODE (parm) == TEMPLATE_DECL)
6088 DECL_CONTEXT (parm) = tmpl;
6089 }
6090
6091 if (TREE_CODE (decl) == TYPE_DECL
6092 && TYPE_DECL_ALIAS_P (decl))
6093 {
6094 if (tree constr
6095 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6096 {
6097 /* ??? Why don't we do this here for all templates? */
6098 constr = build_constraints (constr, NULL_TREE);
6099 set_constraints (decl, constr);
6100 }
6101 if (complex_alias_template_p (tmpl))
6102 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6103 }
6104 }
6105
6106 /* The DECL_TI_ARGS of DECL contains full set of arguments
6107 referring wback to its most general template. If TMPL is a
6108 specialization, ARGS may only have the innermost set of
6109 arguments. Add the missing argument levels if necessary. */
6110 if (DECL_TEMPLATE_INFO (tmpl))
6111 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6112
6113 tree info = build_template_info (tmpl, args);
6114
6115 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6116 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6117 else
6118 {
6119 retrofit_lang_decl (decl);
6120 DECL_TEMPLATE_INFO (decl) = info;
6121 }
6122 }
6123
6124 if (flag_implicit_templates
6125 && !is_friend
6126 && TREE_PUBLIC (decl)
6127 && VAR_OR_FUNCTION_DECL_P (decl))
6128 /* Set DECL_COMDAT on template instantiations; if we force
6129 them to be emitted by explicit instantiation,
6130 mark_needed will tell cgraph to do the right thing. */
6131 DECL_COMDAT (decl) = true;
6132
6133 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6134
6135 return decl;
6136 }
6137
6138 /* FN is an inheriting constructor that inherits from the constructor
6139 template INHERITED; turn FN into a constructor template with a matching
6140 template header. */
6141
6142 tree
6143 add_inherited_template_parms (tree fn, tree inherited)
6144 {
6145 tree inner_parms
6146 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6147 inner_parms = copy_node (inner_parms);
6148 tree parms
6149 = tree_cons (size_int (processing_template_decl + 1),
6150 inner_parms, current_template_parms);
6151 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6152 tree args = template_parms_to_args (parms);
6153 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6154 DECL_ARTIFICIAL (tmpl) = true;
6155 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6156 return tmpl;
6157 }
6158
6159 /* Called when a class template TYPE is redeclared with the indicated
6160 template PARMS, e.g.:
6161
6162 template <class T> struct S;
6163 template <class T> struct S {}; */
6164
6165 bool
6166 redeclare_class_template (tree type, tree parms, tree cons)
6167 {
6168 tree tmpl;
6169 tree tmpl_parms;
6170 int i;
6171
6172 if (!TYPE_TEMPLATE_INFO (type))
6173 {
6174 error ("%qT is not a template type", type);
6175 return false;
6176 }
6177
6178 tmpl = TYPE_TI_TEMPLATE (type);
6179 if (!PRIMARY_TEMPLATE_P (tmpl))
6180 /* The type is nested in some template class. Nothing to worry
6181 about here; there are no new template parameters for the nested
6182 type. */
6183 return true;
6184
6185 if (!parms)
6186 {
6187 error ("template specifiers not specified in declaration of %qD",
6188 tmpl);
6189 return false;
6190 }
6191
6192 parms = INNERMOST_TEMPLATE_PARMS (parms);
6193 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6194
6195 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6196 {
6197 error_n (input_location, TREE_VEC_LENGTH (parms),
6198 "redeclared with %d template parameter",
6199 "redeclared with %d template parameters",
6200 TREE_VEC_LENGTH (parms));
6201 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6202 "previous declaration %qD used %d template parameter",
6203 "previous declaration %qD used %d template parameters",
6204 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6205 return false;
6206 }
6207
6208 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6209 {
6210 tree tmpl_parm;
6211 tree parm;
6212 tree tmpl_default;
6213 tree parm_default;
6214
6215 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6216 || TREE_VEC_ELT (parms, i) == error_mark_node)
6217 continue;
6218
6219 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6220 if (error_operand_p (tmpl_parm))
6221 return false;
6222
6223 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6224 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6225 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6226
6227 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6228 TEMPLATE_DECL. */
6229 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6230 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6231 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6232 || (TREE_CODE (tmpl_parm) != PARM_DECL
6233 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6234 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6235 || (TREE_CODE (tmpl_parm) == PARM_DECL
6236 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6237 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6238 {
6239 auto_diagnostic_group d;
6240 error ("template parameter %q+#D", tmpl_parm);
6241 inform (input_location, "redeclared here as %q#D", parm);
6242 return false;
6243 }
6244
6245 /* The parameters can be declared to introduce different
6246 constraints. */
6247 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6248 tree p2 = TREE_VEC_ELT (parms, i);
6249 if (!template_parameter_constraints_equivalent_p (p1, p2))
6250 {
6251 auto_diagnostic_group d;
6252 error ("declaration of template parameter %q+#D with different "
6253 "constraints", parm);
6254 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6255 "original declaration appeared here");
6256 return false;
6257 }
6258
6259 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6260 {
6261 /* We have in [temp.param]:
6262
6263 A template-parameter may not be given default arguments
6264 by two different declarations in the same scope. */
6265 auto_diagnostic_group d;
6266 error_at (input_location, "redefinition of default argument for %q#D", parm);
6267 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6268 "original definition appeared here");
6269 return false;
6270 }
6271
6272 if (parm_default != NULL_TREE)
6273 /* Update the previous template parameters (which are the ones
6274 that will really count) with the new default value. */
6275 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6276 else if (tmpl_default != NULL_TREE)
6277 /* Update the new parameters, too; they'll be used as the
6278 parameters for any members. */
6279 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6280
6281 /* Give each template template parm in this redeclaration a
6282 DECL_CONTEXT of the template for which they are a parameter. */
6283 if (TREE_CODE (parm) == TEMPLATE_DECL)
6284 {
6285 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6286 DECL_CONTEXT (parm) = tmpl;
6287 }
6288
6289 if (TREE_CODE (parm) == TYPE_DECL)
6290 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6291 }
6292
6293 tree ci = get_constraints (tmpl);
6294 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6295 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6296
6297 /* Two classes with different constraints declare different entities. */
6298 if (!cp_tree_equal (req1, req2))
6299 {
6300 auto_diagnostic_group d;
6301 error_at (input_location, "redeclaration %q#D with different "
6302 "constraints", tmpl);
6303 inform (DECL_SOURCE_LOCATION (tmpl),
6304 "original declaration appeared here");
6305 return false;
6306 }
6307
6308 return true;
6309 }
6310
6311 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6312 to be used when the caller has already checked
6313 (processing_template_decl
6314 && !instantiation_dependent_expression_p (expr)
6315 && potential_constant_expression (expr))
6316 and cleared processing_template_decl. */
6317
6318 tree
6319 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6320 {
6321 return tsubst_copy_and_build (expr,
6322 /*args=*/NULL_TREE,
6323 complain,
6324 /*in_decl=*/NULL_TREE,
6325 /*function_p=*/false,
6326 /*integral_constant_expression_p=*/true);
6327 }
6328
6329 /* Simplify EXPR if it is a non-dependent expression. Returns the
6330 (possibly simplified) expression. */
6331
6332 tree
6333 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6334 {
6335 if (expr == NULL_TREE)
6336 return NULL_TREE;
6337
6338 /* If we're in a template, but EXPR isn't value dependent, simplify
6339 it. We're supposed to treat:
6340
6341 template <typename T> void f(T[1 + 1]);
6342 template <typename T> void f(T[2]);
6343
6344 as two declarations of the same function, for example. */
6345 if (processing_template_decl
6346 && is_nondependent_constant_expression (expr))
6347 {
6348 processing_template_decl_sentinel s;
6349 expr = instantiate_non_dependent_expr_internal (expr, complain);
6350 }
6351 return expr;
6352 }
6353
6354 tree
6355 instantiate_non_dependent_expr (tree expr)
6356 {
6357 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6358 }
6359
6360 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6361 an uninstantiated expression. */
6362
6363 tree
6364 instantiate_non_dependent_or_null (tree expr)
6365 {
6366 if (expr == NULL_TREE)
6367 return NULL_TREE;
6368 if (processing_template_decl)
6369 {
6370 if (!is_nondependent_constant_expression (expr))
6371 expr = NULL_TREE;
6372 else
6373 {
6374 processing_template_decl_sentinel s;
6375 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6376 }
6377 }
6378 return expr;
6379 }
6380
6381 /* True iff T is a specialization of a variable template. */
6382
6383 bool
6384 variable_template_specialization_p (tree t)
6385 {
6386 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6387 return false;
6388 tree tmpl = DECL_TI_TEMPLATE (t);
6389 return variable_template_p (tmpl);
6390 }
6391
6392 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6393 template declaration, or a TYPE_DECL for an alias declaration. */
6394
6395 bool
6396 alias_type_or_template_p (tree t)
6397 {
6398 if (t == NULL_TREE)
6399 return false;
6400 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6401 || (TYPE_P (t)
6402 && TYPE_NAME (t)
6403 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6404 || DECL_ALIAS_TEMPLATE_P (t));
6405 }
6406
6407 /* If T is a specialization of an alias template, return it; otherwise return
6408 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6409
6410 tree
6411 alias_template_specialization_p (const_tree t,
6412 bool transparent_typedefs)
6413 {
6414 if (!TYPE_P (t))
6415 return NULL_TREE;
6416
6417 /* It's an alias template specialization if it's an alias and its
6418 TYPE_NAME is a specialization of a primary template. */
6419 if (typedef_variant_p (t))
6420 {
6421 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6422 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6423 return CONST_CAST_TREE (t);
6424 if (transparent_typedefs)
6425 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6426 (TYPE_NAME (t)),
6427 transparent_typedefs);
6428 }
6429
6430 return NULL_TREE;
6431 }
6432
6433 /* An alias template is complex from a SFINAE perspective if a template-id
6434 using that alias can be ill-formed when the expansion is not, as with
6435 the void_t template. We determine this by checking whether the
6436 expansion for the alias template uses all its template parameters. */
6437
6438 struct uses_all_template_parms_data
6439 {
6440 int level;
6441 bool *seen;
6442 };
6443
6444 static int
6445 uses_all_template_parms_r (tree t, void *data_)
6446 {
6447 struct uses_all_template_parms_data &data
6448 = *(struct uses_all_template_parms_data*)data_;
6449 tree idx = get_template_parm_index (t);
6450
6451 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6452 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6453 return 0;
6454 }
6455
6456 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6457
6458 static int
6459 complex_pack_expansion_r (tree t, void *data_)
6460 {
6461 /* An alias template with a pack expansion that expands a pack from the
6462 enclosing class needs to be considered complex, to avoid confusion with
6463 the same pack being used as an argument to the alias's own template
6464 parameter (91966). */
6465 if (!PACK_EXPANSION_P (t))
6466 return 0;
6467 struct uses_all_template_parms_data &data
6468 = *(struct uses_all_template_parms_data*)data_;
6469 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6470 pack = TREE_CHAIN (pack))
6471 {
6472 tree parm_pack = TREE_VALUE (pack);
6473 if (!TEMPLATE_PARM_P (parm_pack))
6474 continue;
6475 int idx, level;
6476 template_parm_level_and_index (parm_pack, &level, &idx);
6477 if (level < data.level)
6478 return 1;
6479 }
6480 return 0;
6481 }
6482
6483 static bool
6484 complex_alias_template_p (const_tree tmpl)
6485 {
6486 /* A renaming alias isn't complex. */
6487 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6488 return false;
6489
6490 /* Any other constrained alias is complex. */
6491 if (get_constraints (tmpl))
6492 return true;
6493
6494 struct uses_all_template_parms_data data;
6495 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6496 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6497 data.level = TMPL_PARMS_DEPTH (parms);
6498 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6499 data.seen = XALLOCAVEC (bool, len);
6500 for (int i = 0; i < len; ++i)
6501 data.seen[i] = false;
6502
6503 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6504 NULL, true, complex_pack_expansion_r))
6505 return true;
6506 for (int i = 0; i < len; ++i)
6507 if (!data.seen[i])
6508 return true;
6509 return false;
6510 }
6511
6512 /* If T is a specialization of a complex alias template with dependent
6513 template-arguments, return it; otherwise return NULL_TREE. If T is a
6514 typedef to such a specialization, return the specialization. */
6515
6516 tree
6517 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6518 {
6519 if (!TYPE_P (t) || !typedef_variant_p (t))
6520 return NULL_TREE;
6521
6522 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6523 if (tinfo
6524 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6525 && (any_dependent_template_arguments_p
6526 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6527 return CONST_CAST_TREE (t);
6528
6529 if (transparent_typedefs)
6530 {
6531 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6532 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6533 }
6534
6535 return NULL_TREE;
6536 }
6537
6538 /* Return the number of innermost template parameters in TMPL. */
6539
6540 static int
6541 num_innermost_template_parms (const_tree tmpl)
6542 {
6543 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6544 return TREE_VEC_LENGTH (parms);
6545 }
6546
6547 /* Return either TMPL or another template that it is equivalent to under DR
6548 1286: An alias that just changes the name of a template is equivalent to
6549 the other template. */
6550
6551 static tree
6552 get_underlying_template (tree tmpl)
6553 {
6554 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6555 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6556 {
6557 /* Determine if the alias is equivalent to an underlying template. */
6558 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6559 /* The underlying type may have been ill-formed. Don't proceed. */
6560 if (!orig_type)
6561 break;
6562 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6563 if (!tinfo)
6564 break;
6565
6566 tree underlying = TI_TEMPLATE (tinfo);
6567 if (!PRIMARY_TEMPLATE_P (underlying)
6568 || (num_innermost_template_parms (tmpl)
6569 != num_innermost_template_parms (underlying)))
6570 break;
6571
6572 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6573 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6574 break;
6575
6576 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6577 it's appropriate to treat a less-constrained alias as equivalent. */
6578 if (!at_least_as_constrained (underlying, tmpl))
6579 break;
6580
6581 /* Alias is equivalent. Strip it and repeat. */
6582 tmpl = underlying;
6583 }
6584
6585 return tmpl;
6586 }
6587
6588 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6589 must be a reference-to-function or a pointer-to-function type, as specified
6590 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6591 and check that the resulting function has external linkage. */
6592
6593 static tree
6594 convert_nontype_argument_function (tree type, tree expr,
6595 tsubst_flags_t complain)
6596 {
6597 tree fns = expr;
6598 tree fn, fn_no_ptr;
6599 linkage_kind linkage;
6600
6601 fn = instantiate_type (type, fns, tf_none);
6602 if (fn == error_mark_node)
6603 return error_mark_node;
6604
6605 if (value_dependent_expression_p (fn))
6606 goto accept;
6607
6608 fn_no_ptr = strip_fnptr_conv (fn);
6609 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6610 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6611 if (BASELINK_P (fn_no_ptr))
6612 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6613
6614 /* [temp.arg.nontype]/1
6615
6616 A template-argument for a non-type, non-template template-parameter
6617 shall be one of:
6618 [...]
6619 -- the address of an object or function with external [C++11: or
6620 internal] linkage. */
6621
6622 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6623 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6624 {
6625 if (complain & tf_error)
6626 {
6627 location_t loc = cp_expr_loc_or_input_loc (expr);
6628 error_at (loc, "%qE is not a valid template argument for type %qT",
6629 expr, type);
6630 if (TYPE_PTR_P (type))
6631 inform (loc, "it must be the address of a function "
6632 "with external linkage");
6633 else
6634 inform (loc, "it must be the name of a function with "
6635 "external linkage");
6636 }
6637 return NULL_TREE;
6638 }
6639
6640 linkage = decl_linkage (fn_no_ptr);
6641 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6642 {
6643 if (complain & tf_error)
6644 {
6645 location_t loc = cp_expr_loc_or_input_loc (expr);
6646 if (cxx_dialect >= cxx11)
6647 error_at (loc, "%qE is not a valid template argument for type "
6648 "%qT because %qD has no linkage",
6649 expr, type, fn_no_ptr);
6650 else
6651 error_at (loc, "%qE is not a valid template argument for type "
6652 "%qT because %qD does not have external linkage",
6653 expr, type, fn_no_ptr);
6654 }
6655 return NULL_TREE;
6656 }
6657
6658 accept:
6659 if (TYPE_REF_P (type))
6660 {
6661 if (REFERENCE_REF_P (fn))
6662 fn = TREE_OPERAND (fn, 0);
6663 else
6664 fn = build_address (fn);
6665 }
6666 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6667 fn = build_nop (type, fn);
6668
6669 return fn;
6670 }
6671
6672 /* Subroutine of convert_nontype_argument.
6673 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6674 Emit an error otherwise. */
6675
6676 static bool
6677 check_valid_ptrmem_cst_expr (tree type, tree expr,
6678 tsubst_flags_t complain)
6679 {
6680 tree orig_expr = expr;
6681 STRIP_NOPS (expr);
6682 if (null_ptr_cst_p (expr))
6683 return true;
6684 if (TREE_CODE (expr) == PTRMEM_CST
6685 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6686 PTRMEM_CST_CLASS (expr)))
6687 return true;
6688 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6689 return true;
6690 if (processing_template_decl
6691 && TREE_CODE (expr) == ADDR_EXPR
6692 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6693 return true;
6694 if (complain & tf_error)
6695 {
6696 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6697 error_at (loc, "%qE is not a valid template argument for type %qT",
6698 orig_expr, type);
6699 if (TREE_CODE (expr) != PTRMEM_CST)
6700 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6701 else
6702 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6703 }
6704 return false;
6705 }
6706
6707 /* Returns TRUE iff the address of OP is value-dependent.
6708
6709 14.6.2.4 [temp.dep.temp]:
6710 A non-integral non-type template-argument is dependent if its type is
6711 dependent or it has either of the following forms
6712 qualified-id
6713 & qualified-id
6714 and contains a nested-name-specifier which specifies a class-name that
6715 names a dependent type.
6716
6717 We generalize this to just say that the address of a member of a
6718 dependent class is value-dependent; the above doesn't cover the
6719 address of a static data member named with an unqualified-id. */
6720
6721 static bool
6722 has_value_dependent_address (tree op)
6723 {
6724 STRIP_ANY_LOCATION_WRAPPER (op);
6725
6726 /* We could use get_inner_reference here, but there's no need;
6727 this is only relevant for template non-type arguments, which
6728 can only be expressed as &id-expression. */
6729 if (DECL_P (op))
6730 {
6731 tree ctx = CP_DECL_CONTEXT (op);
6732 if (TYPE_P (ctx) && dependent_type_p (ctx))
6733 return true;
6734 }
6735
6736 return false;
6737 }
6738
6739 /* The next set of functions are used for providing helpful explanatory
6740 diagnostics for failed overload resolution. Their messages should be
6741 indented by two spaces for consistency with the messages in
6742 call.c */
6743
6744 static int
6745 unify_success (bool /*explain_p*/)
6746 {
6747 return 0;
6748 }
6749
6750 /* Other failure functions should call this one, to provide a single function
6751 for setting a breakpoint on. */
6752
6753 static int
6754 unify_invalid (bool /*explain_p*/)
6755 {
6756 return 1;
6757 }
6758
6759 static int
6760 unify_parameter_deduction_failure (bool explain_p, tree parm)
6761 {
6762 if (explain_p)
6763 inform (input_location,
6764 " couldn%'t deduce template parameter %qD", parm);
6765 return unify_invalid (explain_p);
6766 }
6767
6768 static int
6769 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6770 {
6771 if (explain_p)
6772 inform (input_location,
6773 " types %qT and %qT have incompatible cv-qualifiers",
6774 parm, arg);
6775 return unify_invalid (explain_p);
6776 }
6777
6778 static int
6779 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6780 {
6781 if (explain_p)
6782 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6783 return unify_invalid (explain_p);
6784 }
6785
6786 static int
6787 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6788 {
6789 if (explain_p)
6790 inform (input_location,
6791 " template parameter %qD is not a parameter pack, but "
6792 "argument %qD is",
6793 parm, arg);
6794 return unify_invalid (explain_p);
6795 }
6796
6797 static int
6798 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6799 {
6800 if (explain_p)
6801 inform (input_location,
6802 " template argument %qE does not match "
6803 "pointer-to-member constant %qE",
6804 arg, parm);
6805 return unify_invalid (explain_p);
6806 }
6807
6808 static int
6809 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6810 {
6811 if (explain_p)
6812 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6813 return unify_invalid (explain_p);
6814 }
6815
6816 static int
6817 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6818 {
6819 if (explain_p)
6820 inform (input_location,
6821 " inconsistent parameter pack deduction with %qT and %qT",
6822 old_arg, new_arg);
6823 return unify_invalid (explain_p);
6824 }
6825
6826 static int
6827 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6828 {
6829 if (explain_p)
6830 {
6831 if (TYPE_P (parm))
6832 inform (input_location,
6833 " deduced conflicting types for parameter %qT (%qT and %qT)",
6834 parm, first, second);
6835 else
6836 inform (input_location,
6837 " deduced conflicting values for non-type parameter "
6838 "%qE (%qE and %qE)", parm, first, second);
6839 }
6840 return unify_invalid (explain_p);
6841 }
6842
6843 static int
6844 unify_vla_arg (bool explain_p, tree arg)
6845 {
6846 if (explain_p)
6847 inform (input_location,
6848 " variable-sized array type %qT is not "
6849 "a valid template argument",
6850 arg);
6851 return unify_invalid (explain_p);
6852 }
6853
6854 static int
6855 unify_method_type_error (bool explain_p, tree arg)
6856 {
6857 if (explain_p)
6858 inform (input_location,
6859 " member function type %qT is not a valid template argument",
6860 arg);
6861 return unify_invalid (explain_p);
6862 }
6863
6864 static int
6865 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6866 {
6867 if (explain_p)
6868 {
6869 if (least_p)
6870 inform_n (input_location, wanted,
6871 " candidate expects at least %d argument, %d provided",
6872 " candidate expects at least %d arguments, %d provided",
6873 wanted, have);
6874 else
6875 inform_n (input_location, wanted,
6876 " candidate expects %d argument, %d provided",
6877 " candidate expects %d arguments, %d provided",
6878 wanted, have);
6879 }
6880 return unify_invalid (explain_p);
6881 }
6882
6883 static int
6884 unify_too_many_arguments (bool explain_p, int have, int wanted)
6885 {
6886 return unify_arity (explain_p, have, wanted);
6887 }
6888
6889 static int
6890 unify_too_few_arguments (bool explain_p, int have, int wanted,
6891 bool least_p = false)
6892 {
6893 return unify_arity (explain_p, have, wanted, least_p);
6894 }
6895
6896 static int
6897 unify_arg_conversion (bool explain_p, tree to_type,
6898 tree from_type, tree arg)
6899 {
6900 if (explain_p)
6901 inform (cp_expr_loc_or_input_loc (arg),
6902 " cannot convert %qE (type %qT) to type %qT",
6903 arg, from_type, to_type);
6904 return unify_invalid (explain_p);
6905 }
6906
6907 static int
6908 unify_no_common_base (bool explain_p, enum template_base_result r,
6909 tree parm, tree arg)
6910 {
6911 if (explain_p)
6912 switch (r)
6913 {
6914 case tbr_ambiguous_baseclass:
6915 inform (input_location, " %qT is an ambiguous base class of %qT",
6916 parm, arg);
6917 break;
6918 default:
6919 inform (input_location, " %qT is not derived from %qT", arg, parm);
6920 break;
6921 }
6922 return unify_invalid (explain_p);
6923 }
6924
6925 static int
6926 unify_inconsistent_template_template_parameters (bool explain_p)
6927 {
6928 if (explain_p)
6929 inform (input_location,
6930 " template parameters of a template template argument are "
6931 "inconsistent with other deduced template arguments");
6932 return unify_invalid (explain_p);
6933 }
6934
6935 static int
6936 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6937 {
6938 if (explain_p)
6939 inform (input_location,
6940 " cannot deduce a template for %qT from non-template type %qT",
6941 parm, arg);
6942 return unify_invalid (explain_p);
6943 }
6944
6945 static int
6946 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6947 {
6948 if (explain_p)
6949 inform (input_location,
6950 " template argument %qE does not match %qE", arg, parm);
6951 return unify_invalid (explain_p);
6952 }
6953
6954 /* True if T is a C++20 template parameter object to store the argument for a
6955 template parameter of class type. */
6956
6957 bool
6958 template_parm_object_p (const_tree t)
6959 {
6960 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6961 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6962 }
6963
6964 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6965 argument for TYPE, points to an unsuitable object.
6966
6967 Also adjust the type of the index in C++20 array subobject references. */
6968
6969 static bool
6970 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6971 {
6972 switch (TREE_CODE (expr))
6973 {
6974 CASE_CONVERT:
6975 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6976 complain);
6977
6978 case TARGET_EXPR:
6979 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6980 complain);
6981
6982 case CONSTRUCTOR:
6983 {
6984 unsigned i; tree elt;
6985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6986 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6987 return true;
6988 }
6989 break;
6990
6991 case ADDR_EXPR:
6992 {
6993 tree decl = TREE_OPERAND (expr, 0);
6994
6995 if (cxx_dialect >= cxx20)
6996 while (TREE_CODE (decl) == COMPONENT_REF
6997 || TREE_CODE (decl) == ARRAY_REF)
6998 {
6999 tree &op = TREE_OPERAND (decl, 1);
7000 if (TREE_CODE (decl) == ARRAY_REF
7001 && TREE_CODE (op) == INTEGER_CST)
7002 /* Canonicalize array offsets to ptrdiff_t; how they were
7003 written doesn't matter for subobject identity. */
7004 op = fold_convert (ptrdiff_type_node, op);
7005 decl = TREE_OPERAND (decl, 0);
7006 }
7007
7008 if (!VAR_P (decl))
7009 {
7010 if (complain & tf_error)
7011 error_at (cp_expr_loc_or_input_loc (expr),
7012 "%qE is not a valid template argument of type %qT "
7013 "because %qE is not a variable", expr, type, decl);
7014 return true;
7015 }
7016 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7017 {
7018 if (complain & tf_error)
7019 error_at (cp_expr_loc_or_input_loc (expr),
7020 "%qE is not a valid template argument of type %qT "
7021 "in C++98 because %qD does not have external linkage",
7022 expr, type, decl);
7023 return true;
7024 }
7025 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7026 && decl_linkage (decl) == lk_none)
7027 {
7028 if (complain & tf_error)
7029 error_at (cp_expr_loc_or_input_loc (expr),
7030 "%qE is not a valid template argument of type %qT "
7031 "because %qD has no linkage", expr, type, decl);
7032 return true;
7033 }
7034 /* C++17: For a non-type template-parameter of reference or pointer
7035 type, the value of the constant expression shall not refer to (or
7036 for a pointer type, shall not be the address of):
7037 * a subobject (4.5),
7038 * a temporary object (15.2),
7039 * a string literal (5.13.5),
7040 * the result of a typeid expression (8.2.8), or
7041 * a predefined __func__ variable (11.4.1). */
7042 else if (DECL_ARTIFICIAL (decl))
7043 {
7044 if (complain & tf_error)
7045 error ("the address of %qD is not a valid template argument",
7046 decl);
7047 return true;
7048 }
7049 else if (cxx_dialect < cxx20
7050 && !(same_type_ignoring_top_level_qualifiers_p
7051 (strip_array_types (TREE_TYPE (type)),
7052 strip_array_types (TREE_TYPE (decl)))))
7053 {
7054 if (complain & tf_error)
7055 error ("the address of the %qT subobject of %qD is not a "
7056 "valid template argument", TREE_TYPE (type), decl);
7057 return true;
7058 }
7059 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7060 {
7061 if (complain & tf_error)
7062 error ("the address of %qD is not a valid template argument "
7063 "because it does not have static storage duration",
7064 decl);
7065 return true;
7066 }
7067 }
7068 break;
7069
7070 default:
7071 if (!INDIRECT_TYPE_P (type))
7072 /* We're only concerned about pointers and references here. */;
7073 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7074 /* Null pointer values are OK in C++11. */;
7075 else
7076 {
7077 if (VAR_P (expr))
7078 {
7079 if (complain & tf_error)
7080 error ("%qD is not a valid template argument "
7081 "because %qD is a variable, not the address of "
7082 "a variable", expr, expr);
7083 return true;
7084 }
7085 else
7086 {
7087 if (complain & tf_error)
7088 error ("%qE is not a valid template argument for %qT "
7089 "because it is not the address of a variable",
7090 expr, type);
7091 return true;
7092 }
7093 }
7094 }
7095 return false;
7096
7097 }
7098
7099 /* The template arguments corresponding to template parameter objects of types
7100 that contain pointers to members. */
7101
7102 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7103
7104 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7105 template argument EXPR. */
7106
7107 static tree
7108 get_template_parm_object (tree expr, tsubst_flags_t complain)
7109 {
7110 if (TREE_CODE (expr) == TARGET_EXPR)
7111 expr = TARGET_EXPR_INITIAL (expr);
7112
7113 if (!TREE_CONSTANT (expr))
7114 {
7115 if ((complain & tf_error)
7116 && require_rvalue_constant_expression (expr))
7117 cxx_constant_value (expr);
7118 return error_mark_node;
7119 }
7120 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7121 return error_mark_node;
7122
7123 tree name = mangle_template_parm_object (expr);
7124 tree decl = get_global_binding (name);
7125 if (decl)
7126 return decl;
7127
7128 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7129 decl = create_temporary_var (type);
7130 DECL_CONTEXT (decl) = NULL_TREE;
7131 TREE_STATIC (decl) = true;
7132 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7133 TREE_READONLY (decl) = true;
7134 DECL_NAME (decl) = name;
7135 SET_DECL_ASSEMBLER_NAME (decl, name);
7136 comdat_linkage (decl);
7137
7138 if (!zero_init_p (type))
7139 {
7140 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7141 lower_var_init before we're done mangling. So store the original
7142 value elsewhere. */
7143 tree copy = unshare_constructor (expr);
7144 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7145 }
7146
7147 pushdecl_top_level_and_finish (decl, expr);
7148
7149 return decl;
7150 }
7151
7152 /* Return the actual template argument corresponding to template parameter
7153 object VAR. */
7154
7155 tree
7156 tparm_object_argument (tree var)
7157 {
7158 if (zero_init_p (TREE_TYPE (var)))
7159 return DECL_INITIAL (var);
7160 return *(tparm_obj_values->get (var));
7161 }
7162
7163 /* Attempt to convert the non-type template parameter EXPR to the
7164 indicated TYPE. If the conversion is successful, return the
7165 converted value. If the conversion is unsuccessful, return
7166 NULL_TREE if we issued an error message, or error_mark_node if we
7167 did not. We issue error messages for out-and-out bad template
7168 parameters, but not simply because the conversion failed, since we
7169 might be just trying to do argument deduction. Both TYPE and EXPR
7170 must be non-dependent.
7171
7172 The conversion follows the special rules described in
7173 [temp.arg.nontype], and it is much more strict than an implicit
7174 conversion.
7175
7176 This function is called twice for each template argument (see
7177 lookup_template_class for a more accurate description of this
7178 problem). This means that we need to handle expressions which
7179 are not valid in a C++ source, but can be created from the
7180 first call (for instance, casts to perform conversions). These
7181 hacks can go away after we fix the double coercion problem. */
7182
7183 static tree
7184 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7185 {
7186 tree expr_type;
7187 location_t loc = cp_expr_loc_or_input_loc (expr);
7188
7189 /* Detect immediately string literals as invalid non-type argument.
7190 This special-case is not needed for correctness (we would easily
7191 catch this later), but only to provide better diagnostic for this
7192 common user mistake. As suggested by DR 100, we do not mention
7193 linkage issues in the diagnostic as this is not the point. */
7194 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7195 {
7196 if (complain & tf_error)
7197 error ("%qE is not a valid template argument for type %qT "
7198 "because string literals can never be used in this context",
7199 expr, type);
7200 return NULL_TREE;
7201 }
7202
7203 /* Add the ADDR_EXPR now for the benefit of
7204 value_dependent_expression_p. */
7205 if (TYPE_PTROBV_P (type)
7206 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7207 {
7208 expr = decay_conversion (expr, complain);
7209 if (expr == error_mark_node)
7210 return error_mark_node;
7211 }
7212
7213 /* If we are in a template, EXPR may be non-dependent, but still
7214 have a syntactic, rather than semantic, form. For example, EXPR
7215 might be a SCOPE_REF, rather than the VAR_DECL to which the
7216 SCOPE_REF refers. Preserving the qualifying scope is necessary
7217 so that access checking can be performed when the template is
7218 instantiated -- but here we need the resolved form so that we can
7219 convert the argument. */
7220 bool non_dep = false;
7221 if (TYPE_REF_OBJ_P (type)
7222 && has_value_dependent_address (expr))
7223 /* If we want the address and it's value-dependent, don't fold. */;
7224 else if (processing_template_decl
7225 && is_nondependent_constant_expression (expr))
7226 non_dep = true;
7227 if (error_operand_p (expr))
7228 return error_mark_node;
7229 expr_type = TREE_TYPE (expr);
7230
7231 /* If the argument is non-dependent, perform any conversions in
7232 non-dependent context as well. */
7233 processing_template_decl_sentinel s (non_dep);
7234 if (non_dep)
7235 expr = instantiate_non_dependent_expr_internal (expr, complain);
7236
7237 const bool val_dep_p = value_dependent_expression_p (expr);
7238 if (val_dep_p)
7239 expr = canonicalize_expr_argument (expr, complain);
7240
7241 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7242 to a non-type argument of "nullptr". */
7243 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7244 expr = fold_simple (convert (type, expr));
7245
7246 /* In C++11, integral or enumeration non-type template arguments can be
7247 arbitrary constant expressions. Pointer and pointer to
7248 member arguments can be general constant expressions that evaluate
7249 to a null value, but otherwise still need to be of a specific form. */
7250 if (cxx_dialect >= cxx11)
7251 {
7252 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7253 /* A PTRMEM_CST is already constant, and a valid template
7254 argument for a parameter of pointer to member type, we just want
7255 to leave it in that form rather than lower it to a
7256 CONSTRUCTOR. */;
7257 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7258 || cxx_dialect >= cxx17)
7259 {
7260 /* C++17: A template-argument for a non-type template-parameter shall
7261 be a converted constant expression (8.20) of the type of the
7262 template-parameter. */
7263 expr = build_converted_constant_expr (type, expr, complain);
7264 if (expr == error_mark_node)
7265 /* Make sure we return NULL_TREE only if we have really issued
7266 an error, as described above. */
7267 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7268 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7269 {
7270 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7271 return expr;
7272 }
7273 expr = maybe_constant_value (expr, NULL_TREE,
7274 /*manifestly_const_eval=*/true);
7275 expr = convert_from_reference (expr);
7276 }
7277 else if (TYPE_PTR_OR_PTRMEM_P (type))
7278 {
7279 tree folded = maybe_constant_value (expr, NULL_TREE,
7280 /*manifestly_const_eval=*/true);
7281 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7282 : null_member_pointer_value_p (folded))
7283 expr = folded;
7284 }
7285 }
7286
7287 if (TYPE_REF_P (type))
7288 expr = mark_lvalue_use (expr);
7289 else
7290 expr = mark_rvalue_use (expr);
7291
7292 /* HACK: Due to double coercion, we can get a
7293 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7294 which is the tree that we built on the first call (see
7295 below when coercing to reference to object or to reference to
7296 function). We just strip everything and get to the arg.
7297 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7298 for examples. */
7299 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7300 {
7301 tree probe_type, probe = expr;
7302 if (REFERENCE_REF_P (probe))
7303 probe = TREE_OPERAND (probe, 0);
7304 probe_type = TREE_TYPE (probe);
7305 if (TREE_CODE (probe) == NOP_EXPR)
7306 {
7307 /* ??? Maybe we could use convert_from_reference here, but we
7308 would need to relax its constraints because the NOP_EXPR
7309 could actually change the type to something more cv-qualified,
7310 and this is not folded by convert_from_reference. */
7311 tree addr = TREE_OPERAND (probe, 0);
7312 if (TYPE_REF_P (probe_type)
7313 && TREE_CODE (addr) == ADDR_EXPR
7314 && TYPE_PTR_P (TREE_TYPE (addr))
7315 && (same_type_ignoring_top_level_qualifiers_p
7316 (TREE_TYPE (probe_type),
7317 TREE_TYPE (TREE_TYPE (addr)))))
7318 {
7319 expr = TREE_OPERAND (addr, 0);
7320 expr_type = TREE_TYPE (probe_type);
7321 }
7322 }
7323 }
7324
7325 /* [temp.arg.nontype]/5, bullet 1
7326
7327 For a non-type template-parameter of integral or enumeration type,
7328 integral promotions (_conv.prom_) and integral conversions
7329 (_conv.integral_) are applied. */
7330 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7331 || TREE_CODE (type) == REAL_TYPE)
7332 {
7333 if (cxx_dialect < cxx11)
7334 {
7335 tree t = build_converted_constant_expr (type, expr, complain);
7336 t = maybe_constant_value (t);
7337 if (t != error_mark_node)
7338 expr = t;
7339 }
7340
7341 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7342 return error_mark_node;
7343
7344 /* Notice that there are constant expressions like '4 % 0' which
7345 do not fold into integer constants. */
7346 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7347 {
7348 if (complain & tf_error)
7349 {
7350 int errs = errorcount, warns = warningcount + werrorcount;
7351 if (!require_potential_constant_expression (expr))
7352 expr = error_mark_node;
7353 else
7354 expr = cxx_constant_value (expr);
7355 if (errorcount > errs || warningcount + werrorcount > warns)
7356 inform (loc, "in template argument for type %qT", type);
7357 if (expr == error_mark_node)
7358 return NULL_TREE;
7359 /* else cxx_constant_value complained but gave us
7360 a real constant, so go ahead. */
7361 if (!CONSTANT_CLASS_P (expr))
7362 {
7363 /* Some assemble time constant expressions like
7364 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7365 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7366 as we can emit them into .rodata initializers of
7367 variables, yet they can't fold into an INTEGER_CST at
7368 compile time. Refuse them here. */
7369 gcc_checking_assert (reduced_constant_expression_p (expr));
7370 error_at (loc, "template argument %qE for type %qT not "
7371 "a compile-time constant", expr, type);
7372 return NULL_TREE;
7373 }
7374 }
7375 else
7376 return NULL_TREE;
7377 }
7378
7379 /* Avoid typedef problems. */
7380 if (TREE_TYPE (expr) != type)
7381 expr = fold_convert (type, expr);
7382 }
7383 /* [temp.arg.nontype]/5, bullet 2
7384
7385 For a non-type template-parameter of type pointer to object,
7386 qualification conversions (_conv.qual_) and the array-to-pointer
7387 conversion (_conv.array_) are applied. */
7388 else if (TYPE_PTROBV_P (type))
7389 {
7390 tree decayed = expr;
7391
7392 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7393 decay_conversion or an explicit cast. If it's a problematic cast,
7394 we'll complain about it below. */
7395 if (TREE_CODE (expr) == NOP_EXPR)
7396 {
7397 tree probe = expr;
7398 STRIP_NOPS (probe);
7399 if (TREE_CODE (probe) == ADDR_EXPR
7400 && TYPE_PTR_P (TREE_TYPE (probe)))
7401 {
7402 expr = probe;
7403 expr_type = TREE_TYPE (expr);
7404 }
7405 }
7406
7407 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7408
7409 A template-argument for a non-type, non-template template-parameter
7410 shall be one of: [...]
7411
7412 -- the name of a non-type template-parameter;
7413 -- the address of an object or function with external linkage, [...]
7414 expressed as "& id-expression" where the & is optional if the name
7415 refers to a function or array, or if the corresponding
7416 template-parameter is a reference.
7417
7418 Here, we do not care about functions, as they are invalid anyway
7419 for a parameter of type pointer-to-object. */
7420
7421 if (val_dep_p)
7422 /* Non-type template parameters are OK. */
7423 ;
7424 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7425 /* Null pointer values are OK in C++11. */;
7426 else if (TREE_CODE (expr) != ADDR_EXPR
7427 && !INDIRECT_TYPE_P (expr_type))
7428 /* Other values, like integer constants, might be valid
7429 non-type arguments of some other type. */
7430 return error_mark_node;
7431 else if (invalid_tparm_referent_p (type, expr, complain))
7432 return NULL_TREE;
7433
7434 expr = decayed;
7435
7436 expr = perform_qualification_conversions (type, expr);
7437 if (expr == error_mark_node)
7438 return error_mark_node;
7439 }
7440 /* [temp.arg.nontype]/5, bullet 3
7441
7442 For a non-type template-parameter of type reference to object, no
7443 conversions apply. The type referred to by the reference may be more
7444 cv-qualified than the (otherwise identical) type of the
7445 template-argument. The template-parameter is bound directly to the
7446 template-argument, which must be an lvalue. */
7447 else if (TYPE_REF_OBJ_P (type))
7448 {
7449 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7450 expr_type))
7451 return error_mark_node;
7452
7453 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7454 {
7455 if (complain & tf_error)
7456 error ("%qE is not a valid template argument for type %qT "
7457 "because of conflicts in cv-qualification", expr, type);
7458 return NULL_TREE;
7459 }
7460
7461 if (!lvalue_p (expr))
7462 {
7463 if (complain & tf_error)
7464 error ("%qE is not a valid template argument for type %qT "
7465 "because it is not an lvalue", expr, type);
7466 return NULL_TREE;
7467 }
7468
7469 /* [temp.arg.nontype]/1
7470
7471 A template-argument for a non-type, non-template template-parameter
7472 shall be one of: [...]
7473
7474 -- the address of an object or function with external linkage. */
7475 if (INDIRECT_REF_P (expr)
7476 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7477 {
7478 expr = TREE_OPERAND (expr, 0);
7479 if (DECL_P (expr))
7480 {
7481 if (complain & tf_error)
7482 error ("%q#D is not a valid template argument for type %qT "
7483 "because a reference variable does not have a constant "
7484 "address", expr, type);
7485 return NULL_TREE;
7486 }
7487 }
7488
7489 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7490 /* OK, dependent reference. We don't want to ask whether a DECL is
7491 itself value-dependent, since what we want here is its address. */;
7492 else
7493 {
7494 expr = build_address (expr);
7495
7496 if (invalid_tparm_referent_p (type, expr, complain))
7497 return NULL_TREE;
7498 }
7499
7500 if (!same_type_p (type, TREE_TYPE (expr)))
7501 expr = build_nop (type, expr);
7502 }
7503 /* [temp.arg.nontype]/5, bullet 4
7504
7505 For a non-type template-parameter of type pointer to function, only
7506 the function-to-pointer conversion (_conv.func_) is applied. If the
7507 template-argument represents a set of overloaded functions (or a
7508 pointer to such), the matching function is selected from the set
7509 (_over.over_). */
7510 else if (TYPE_PTRFN_P (type))
7511 {
7512 /* If the argument is a template-id, we might not have enough
7513 context information to decay the pointer. */
7514 if (!type_unknown_p (expr_type))
7515 {
7516 expr = decay_conversion (expr, complain);
7517 if (expr == error_mark_node)
7518 return error_mark_node;
7519 }
7520
7521 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7522 /* Null pointer values are OK in C++11. */
7523 return perform_qualification_conversions (type, expr);
7524
7525 expr = convert_nontype_argument_function (type, expr, complain);
7526 if (!expr || expr == error_mark_node)
7527 return expr;
7528 }
7529 /* [temp.arg.nontype]/5, bullet 5
7530
7531 For a non-type template-parameter of type reference to function, no
7532 conversions apply. If the template-argument represents a set of
7533 overloaded functions, the matching function is selected from the set
7534 (_over.over_). */
7535 else if (TYPE_REFFN_P (type))
7536 {
7537 if (TREE_CODE (expr) == ADDR_EXPR)
7538 {
7539 if (complain & tf_error)
7540 {
7541 error ("%qE is not a valid template argument for type %qT "
7542 "because it is a pointer", expr, type);
7543 inform (input_location, "try using %qE instead",
7544 TREE_OPERAND (expr, 0));
7545 }
7546 return NULL_TREE;
7547 }
7548
7549 expr = convert_nontype_argument_function (type, expr, complain);
7550 if (!expr || expr == error_mark_node)
7551 return expr;
7552 }
7553 /* [temp.arg.nontype]/5, bullet 6
7554
7555 For a non-type template-parameter of type pointer to member function,
7556 no conversions apply. If the template-argument represents a set of
7557 overloaded member functions, the matching member function is selected
7558 from the set (_over.over_). */
7559 else if (TYPE_PTRMEMFUNC_P (type))
7560 {
7561 expr = instantiate_type (type, expr, tf_none);
7562 if (expr == error_mark_node)
7563 return error_mark_node;
7564
7565 /* [temp.arg.nontype] bullet 1 says the pointer to member
7566 expression must be a pointer-to-member constant. */
7567 if (!val_dep_p
7568 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7569 return NULL_TREE;
7570
7571 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7572 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7573 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7574 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7575 }
7576 /* [temp.arg.nontype]/5, bullet 7
7577
7578 For a non-type template-parameter of type pointer to data member,
7579 qualification conversions (_conv.qual_) are applied. */
7580 else if (TYPE_PTRDATAMEM_P (type))
7581 {
7582 /* [temp.arg.nontype] bullet 1 says the pointer to member
7583 expression must be a pointer-to-member constant. */
7584 if (!val_dep_p
7585 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7586 return NULL_TREE;
7587
7588 expr = perform_qualification_conversions (type, expr);
7589 if (expr == error_mark_node)
7590 return expr;
7591 }
7592 else if (NULLPTR_TYPE_P (type))
7593 {
7594 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7595 {
7596 if (complain & tf_error)
7597 error ("%qE is not a valid template argument for type %qT "
7598 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7599 return NULL_TREE;
7600 }
7601 return expr;
7602 }
7603 else if (CLASS_TYPE_P (type))
7604 {
7605 /* Replace the argument with a reference to the corresponding template
7606 parameter object. */
7607 if (!val_dep_p)
7608 expr = get_template_parm_object (expr, complain);
7609 if (expr == error_mark_node)
7610 return NULL_TREE;
7611 }
7612 /* A template non-type parameter must be one of the above. */
7613 else
7614 gcc_unreachable ();
7615
7616 /* Sanity check: did we actually convert the argument to the
7617 right type? */
7618 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7619 (type, TREE_TYPE (expr)));
7620 return convert_from_reference (expr);
7621 }
7622
7623 /* Subroutine of coerce_template_template_parms, which returns 1 if
7624 PARM_PARM and ARG_PARM match using the rule for the template
7625 parameters of template template parameters. Both PARM and ARG are
7626 template parameters; the rest of the arguments are the same as for
7627 coerce_template_template_parms.
7628 */
7629 static int
7630 coerce_template_template_parm (tree parm,
7631 tree arg,
7632 tsubst_flags_t complain,
7633 tree in_decl,
7634 tree outer_args)
7635 {
7636 if (arg == NULL_TREE || error_operand_p (arg)
7637 || parm == NULL_TREE || error_operand_p (parm))
7638 return 0;
7639
7640 if (TREE_CODE (arg) != TREE_CODE (parm))
7641 return 0;
7642
7643 switch (TREE_CODE (parm))
7644 {
7645 case TEMPLATE_DECL:
7646 /* We encounter instantiations of templates like
7647 template <template <template <class> class> class TT>
7648 class C; */
7649 {
7650 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7651 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7652
7653 if (!coerce_template_template_parms
7654 (parmparm, argparm, complain, in_decl, outer_args))
7655 return 0;
7656 }
7657 /* Fall through. */
7658
7659 case TYPE_DECL:
7660 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7661 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7662 /* Argument is a parameter pack but parameter is not. */
7663 return 0;
7664 break;
7665
7666 case PARM_DECL:
7667 /* The tsubst call is used to handle cases such as
7668
7669 template <int> class C {};
7670 template <class T, template <T> class TT> class D {};
7671 D<int, C> d;
7672
7673 i.e. the parameter list of TT depends on earlier parameters. */
7674 if (!uses_template_parms (TREE_TYPE (arg)))
7675 {
7676 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7677 if (!uses_template_parms (t)
7678 && !same_type_p (t, TREE_TYPE (arg)))
7679 return 0;
7680 }
7681
7682 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7683 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7684 /* Argument is a parameter pack but parameter is not. */
7685 return 0;
7686
7687 break;
7688
7689 default:
7690 gcc_unreachable ();
7691 }
7692
7693 return 1;
7694 }
7695
7696 /* Coerce template argument list ARGLIST for use with template
7697 template-parameter TEMPL. */
7698
7699 static tree
7700 coerce_template_args_for_ttp (tree templ, tree arglist,
7701 tsubst_flags_t complain)
7702 {
7703 /* Consider an example where a template template parameter declared as
7704
7705 template <class T, class U = std::allocator<T> > class TT
7706
7707 The template parameter level of T and U are one level larger than
7708 of TT. To proper process the default argument of U, say when an
7709 instantiation `TT<int>' is seen, we need to build the full
7710 arguments containing {int} as the innermost level. Outer levels,
7711 available when not appearing as default template argument, can be
7712 obtained from the arguments of the enclosing template.
7713
7714 Suppose that TT is later substituted with std::vector. The above
7715 instantiation is `TT<int, std::allocator<T> >' with TT at
7716 level 1, and T at level 2, while the template arguments at level 1
7717 becomes {std::vector} and the inner level 2 is {int}. */
7718
7719 tree outer = DECL_CONTEXT (templ);
7720 if (outer)
7721 outer = generic_targs_for (outer);
7722 else if (current_template_parms)
7723 {
7724 /* This is an argument of the current template, so we haven't set
7725 DECL_CONTEXT yet. */
7726 tree relevant_template_parms;
7727
7728 /* Parameter levels that are greater than the level of the given
7729 template template parm are irrelevant. */
7730 relevant_template_parms = current_template_parms;
7731 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7732 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7733 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7734
7735 outer = template_parms_to_args (relevant_template_parms);
7736 }
7737
7738 if (outer)
7739 arglist = add_to_template_args (outer, arglist);
7740
7741 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7742 return coerce_template_parms (parmlist, arglist, templ,
7743 complain,
7744 /*require_all_args=*/true,
7745 /*use_default_args=*/true);
7746 }
7747
7748 /* A cache of template template parameters with match-all default
7749 arguments. */
7750 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7751
7752 /* T is a bound template template-parameter. Copy its arguments into default
7753 arguments of the template template-parameter's template parameters. */
7754
7755 static tree
7756 add_defaults_to_ttp (tree otmpl)
7757 {
7758 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7759 return *c;
7760
7761 tree ntmpl = copy_node (otmpl);
7762
7763 tree ntype = copy_node (TREE_TYPE (otmpl));
7764 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7765 TYPE_MAIN_VARIANT (ntype) = ntype;
7766 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7767 TYPE_NAME (ntype) = ntmpl;
7768 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7769
7770 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7771 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7772 TEMPLATE_PARM_DECL (idx) = ntmpl;
7773 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7774
7775 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7776 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7777 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7778 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7779 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7780 {
7781 tree o = TREE_VEC_ELT (vec, i);
7782 if (!template_parameter_pack_p (TREE_VALUE (o)))
7783 {
7784 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7785 TREE_PURPOSE (n) = any_targ_node;
7786 }
7787 }
7788
7789 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7790 return ntmpl;
7791 }
7792
7793 /* ARG is a bound potential template template-argument, and PARGS is a list
7794 of arguments for the corresponding template template-parameter. Adjust
7795 PARGS as appropriate for application to ARG's template, and if ARG is a
7796 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7797 arguments to the template template parameter. */
7798
7799 static tree
7800 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7801 {
7802 ++processing_template_decl;
7803 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7804 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7805 {
7806 /* When comparing two template template-parameters in partial ordering,
7807 rewrite the one currently being used as an argument to have default
7808 arguments for all parameters. */
7809 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7810 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7811 if (pargs != error_mark_node)
7812 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7813 TYPE_TI_ARGS (arg));
7814 }
7815 else
7816 {
7817 tree aparms
7818 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7819 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7820 /*require_all*/true,
7821 /*use_default*/true);
7822 }
7823 --processing_template_decl;
7824 return pargs;
7825 }
7826
7827 /* Subroutine of unify for the case when PARM is a
7828 BOUND_TEMPLATE_TEMPLATE_PARM. */
7829
7830 static int
7831 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7832 bool explain_p)
7833 {
7834 tree parmvec = TYPE_TI_ARGS (parm);
7835 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7836
7837 /* The template template parm might be variadic and the argument
7838 not, so flatten both argument lists. */
7839 parmvec = expand_template_argument_pack (parmvec);
7840 argvec = expand_template_argument_pack (argvec);
7841
7842 if (flag_new_ttp)
7843 {
7844 /* In keeping with P0522R0, adjust P's template arguments
7845 to apply to A's template; then flatten it again. */
7846 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7847 nparmvec = expand_template_argument_pack (nparmvec);
7848
7849 if (unify (tparms, targs, nparmvec, argvec,
7850 UNIFY_ALLOW_NONE, explain_p))
7851 return 1;
7852
7853 /* If the P0522 adjustment eliminated a pack expansion, deduce
7854 empty packs. */
7855 if (flag_new_ttp
7856 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7857 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7858 DEDUCE_EXACT, /*sub*/true, explain_p))
7859 return 1;
7860 }
7861 else
7862 {
7863 /* Deduce arguments T, i from TT<T> or TT<i>.
7864 We check each element of PARMVEC and ARGVEC individually
7865 rather than the whole TREE_VEC since they can have
7866 different number of elements, which is allowed under N2555. */
7867
7868 int len = TREE_VEC_LENGTH (parmvec);
7869
7870 /* Check if the parameters end in a pack, making them
7871 variadic. */
7872 int parm_variadic_p = 0;
7873 if (len > 0
7874 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7875 parm_variadic_p = 1;
7876
7877 for (int i = 0; i < len - parm_variadic_p; ++i)
7878 /* If the template argument list of P contains a pack
7879 expansion that is not the last template argument, the
7880 entire template argument list is a non-deduced
7881 context. */
7882 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7883 return unify_success (explain_p);
7884
7885 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7886 return unify_too_few_arguments (explain_p,
7887 TREE_VEC_LENGTH (argvec), len);
7888
7889 for (int i = 0; i < len - parm_variadic_p; ++i)
7890 if (unify (tparms, targs,
7891 TREE_VEC_ELT (parmvec, i),
7892 TREE_VEC_ELT (argvec, i),
7893 UNIFY_ALLOW_NONE, explain_p))
7894 return 1;
7895
7896 if (parm_variadic_p
7897 && unify_pack_expansion (tparms, targs,
7898 parmvec, argvec,
7899 DEDUCE_EXACT,
7900 /*subr=*/true, explain_p))
7901 return 1;
7902 }
7903
7904 return 0;
7905 }
7906
7907 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7908 template template parameters. Both PARM_PARMS and ARG_PARMS are
7909 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7910 or PARM_DECL.
7911
7912 Consider the example:
7913 template <class T> class A;
7914 template<template <class U> class TT> class B;
7915
7916 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7917 the parameters to A, and OUTER_ARGS contains A. */
7918
7919 static int
7920 coerce_template_template_parms (tree parm_parms,
7921 tree arg_parms,
7922 tsubst_flags_t complain,
7923 tree in_decl,
7924 tree outer_args)
7925 {
7926 int nparms, nargs, i;
7927 tree parm, arg;
7928 int variadic_p = 0;
7929
7930 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7931 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7932
7933 nparms = TREE_VEC_LENGTH (parm_parms);
7934 nargs = TREE_VEC_LENGTH (arg_parms);
7935
7936 if (flag_new_ttp)
7937 {
7938 /* P0522R0: A template template-parameter P is at least as specialized as
7939 a template template-argument A if, given the following rewrite to two
7940 function templates, the function template corresponding to P is at
7941 least as specialized as the function template corresponding to A
7942 according to the partial ordering rules for function templates
7943 ([temp.func.order]). Given an invented class template X with the
7944 template parameter list of A (including default arguments):
7945
7946 * Each of the two function templates has the same template parameters,
7947 respectively, as P or A.
7948
7949 * Each function template has a single function parameter whose type is
7950 a specialization of X with template arguments corresponding to the
7951 template parameters from the respective function template where, for
7952 each template parameter PP in the template parameter list of the
7953 function template, a corresponding template argument AA is formed. If
7954 PP declares a parameter pack, then AA is the pack expansion
7955 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7956
7957 If the rewrite produces an invalid type, then P is not at least as
7958 specialized as A. */
7959
7960 /* So coerce P's args to apply to A's parms, and then deduce between A's
7961 args and the converted args. If that succeeds, A is at least as
7962 specialized as P, so they match.*/
7963 tree pargs = template_parms_level_to_args (parm_parms);
7964 pargs = add_outermost_template_args (outer_args, pargs);
7965 ++processing_template_decl;
7966 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7967 /*require_all*/true, /*use_default*/true);
7968 --processing_template_decl;
7969 if (pargs != error_mark_node)
7970 {
7971 tree targs = make_tree_vec (nargs);
7972 tree aargs = template_parms_level_to_args (arg_parms);
7973 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7974 /*explain*/false))
7975 return 1;
7976 }
7977 }
7978
7979 /* Determine whether we have a parameter pack at the end of the
7980 template template parameter's template parameter list. */
7981 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7982 {
7983 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7984
7985 if (error_operand_p (parm))
7986 return 0;
7987
7988 switch (TREE_CODE (parm))
7989 {
7990 case TEMPLATE_DECL:
7991 case TYPE_DECL:
7992 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7993 variadic_p = 1;
7994 break;
7995
7996 case PARM_DECL:
7997 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7998 variadic_p = 1;
7999 break;
8000
8001 default:
8002 gcc_unreachable ();
8003 }
8004 }
8005
8006 if (nargs != nparms
8007 && !(variadic_p && nargs >= nparms - 1))
8008 return 0;
8009
8010 /* Check all of the template parameters except the parameter pack at
8011 the end (if any). */
8012 for (i = 0; i < nparms - variadic_p; ++i)
8013 {
8014 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8015 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8016 continue;
8017
8018 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8019 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8020
8021 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8022 outer_args))
8023 return 0;
8024
8025 }
8026
8027 if (variadic_p)
8028 {
8029 /* Check each of the template parameters in the template
8030 argument against the template parameter pack at the end of
8031 the template template parameter. */
8032 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8033 return 0;
8034
8035 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8036
8037 for (; i < nargs; ++i)
8038 {
8039 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8040 continue;
8041
8042 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8043
8044 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8045 outer_args))
8046 return 0;
8047 }
8048 }
8049
8050 return 1;
8051 }
8052
8053 /* Verifies that the deduced template arguments (in TARGS) for the
8054 template template parameters (in TPARMS) represent valid bindings,
8055 by comparing the template parameter list of each template argument
8056 to the template parameter list of its corresponding template
8057 template parameter, in accordance with DR150. This
8058 routine can only be called after all template arguments have been
8059 deduced. It will return TRUE if all of the template template
8060 parameter bindings are okay, FALSE otherwise. */
8061 bool
8062 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8063 {
8064 int i, ntparms = TREE_VEC_LENGTH (tparms);
8065 bool ret = true;
8066
8067 /* We're dealing with template parms in this process. */
8068 ++processing_template_decl;
8069
8070 targs = INNERMOST_TEMPLATE_ARGS (targs);
8071
8072 for (i = 0; i < ntparms; ++i)
8073 {
8074 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8075 tree targ = TREE_VEC_ELT (targs, i);
8076
8077 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8078 {
8079 tree packed_args = NULL_TREE;
8080 int idx, len = 1;
8081
8082 if (ARGUMENT_PACK_P (targ))
8083 {
8084 /* Look inside the argument pack. */
8085 packed_args = ARGUMENT_PACK_ARGS (targ);
8086 len = TREE_VEC_LENGTH (packed_args);
8087 }
8088
8089 for (idx = 0; idx < len; ++idx)
8090 {
8091 tree targ_parms = NULL_TREE;
8092
8093 if (packed_args)
8094 /* Extract the next argument from the argument
8095 pack. */
8096 targ = TREE_VEC_ELT (packed_args, idx);
8097
8098 if (PACK_EXPANSION_P (targ))
8099 /* Look at the pattern of the pack expansion. */
8100 targ = PACK_EXPANSION_PATTERN (targ);
8101
8102 /* Extract the template parameters from the template
8103 argument. */
8104 if (TREE_CODE (targ) == TEMPLATE_DECL)
8105 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8106 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8107 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8108
8109 /* Verify that we can coerce the template template
8110 parameters from the template argument to the template
8111 parameter. This requires an exact match. */
8112 if (targ_parms
8113 && !coerce_template_template_parms
8114 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8115 targ_parms,
8116 tf_none,
8117 tparm,
8118 targs))
8119 {
8120 ret = false;
8121 goto out;
8122 }
8123 }
8124 }
8125 }
8126
8127 out:
8128
8129 --processing_template_decl;
8130 return ret;
8131 }
8132
8133 /* Since type attributes aren't mangled, we need to strip them from
8134 template type arguments. */
8135
8136 tree
8137 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8138 {
8139 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8140 return arg;
8141 bool removed_attributes = false;
8142 tree canon = strip_typedefs (arg, &removed_attributes);
8143 if (removed_attributes
8144 && (complain & tf_warning))
8145 warning (OPT_Wignored_attributes,
8146 "ignoring attributes on template argument %qT", arg);
8147 return canon;
8148 }
8149
8150 /* And from inside dependent non-type arguments like sizeof(Type). */
8151
8152 static tree
8153 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8154 {
8155 if (!arg || arg == error_mark_node)
8156 return arg;
8157 bool removed_attributes = false;
8158 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8159 if (removed_attributes
8160 && (complain & tf_warning))
8161 warning (OPT_Wignored_attributes,
8162 "ignoring attributes in template argument %qE", arg);
8163 return canon;
8164 }
8165
8166 /* A template declaration can be substituted for a constrained
8167 template template parameter only when the argument is no more
8168 constrained than the parameter. */
8169
8170 static bool
8171 is_compatible_template_arg (tree parm, tree arg)
8172 {
8173 tree parm_cons = get_constraints (parm);
8174
8175 /* For now, allow constrained template template arguments
8176 and unconstrained template template parameters. */
8177 if (parm_cons == NULL_TREE)
8178 return true;
8179
8180 /* If the template parameter is constrained, we need to rewrite its
8181 constraints in terms of the ARG's template parameters. This ensures
8182 that all of the template parameter types will have the same depth.
8183
8184 Note that this is only valid when coerce_template_template_parm is
8185 true for the innermost template parameters of PARM and ARG. In other
8186 words, because coercion is successful, this conversion will be valid. */
8187 tree new_args = NULL_TREE;
8188 if (parm_cons)
8189 {
8190 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8191 new_args = template_parms_level_to_args (aparms);
8192 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8193 tf_none, NULL_TREE);
8194 if (parm_cons == error_mark_node)
8195 return false;
8196 }
8197
8198 return weakly_subsumes (parm_cons, arg);
8199 }
8200
8201 // Convert a placeholder argument into a binding to the original
8202 // parameter. The original parameter is saved as the TREE_TYPE of
8203 // ARG.
8204 static inline tree
8205 convert_wildcard_argument (tree parm, tree arg)
8206 {
8207 TREE_TYPE (arg) = parm;
8208 return arg;
8209 }
8210
8211 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8212 because one of them is dependent. But we need to represent the
8213 conversion for the benefit of cp_tree_equal. */
8214
8215 static tree
8216 maybe_convert_nontype_argument (tree type, tree arg)
8217 {
8218 /* Auto parms get no conversion. */
8219 if (type_uses_auto (type))
8220 return arg;
8221 /* We don't need or want to add this conversion now if we're going to use the
8222 argument for deduction. */
8223 if (value_dependent_expression_p (arg))
8224 return arg;
8225
8226 type = cv_unqualified (type);
8227 tree argtype = TREE_TYPE (arg);
8228 if (same_type_p (type, argtype))
8229 return arg;
8230
8231 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8232 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8233 return arg;
8234 }
8235
8236 /* Convert the indicated template ARG as necessary to match the
8237 indicated template PARM. Returns the converted ARG, or
8238 error_mark_node if the conversion was unsuccessful. Error and
8239 warning messages are issued under control of COMPLAIN. This
8240 conversion is for the Ith parameter in the parameter list. ARGS is
8241 the full set of template arguments deduced so far. */
8242
8243 static tree
8244 convert_template_argument (tree parm,
8245 tree arg,
8246 tree args,
8247 tsubst_flags_t complain,
8248 int i,
8249 tree in_decl)
8250 {
8251 tree orig_arg;
8252 tree val;
8253 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8254
8255 if (parm == error_mark_node || error_operand_p (arg))
8256 return error_mark_node;
8257
8258 /* Trivially convert placeholders. */
8259 if (TREE_CODE (arg) == WILDCARD_DECL)
8260 return convert_wildcard_argument (parm, arg);
8261
8262 if (arg == any_targ_node)
8263 return arg;
8264
8265 if (TREE_CODE (arg) == TREE_LIST
8266 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8267 {
8268 /* The template argument was the name of some
8269 member function. That's usually
8270 invalid, but static members are OK. In any
8271 case, grab the underlying fields/functions
8272 and issue an error later if required. */
8273 TREE_TYPE (arg) = unknown_type_node;
8274 }
8275
8276 orig_arg = arg;
8277
8278 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8279 requires_type = (TREE_CODE (parm) == TYPE_DECL
8280 || requires_tmpl_type);
8281
8282 /* When determining whether an argument pack expansion is a template,
8283 look at the pattern. */
8284 if (PACK_EXPANSION_P (arg))
8285 arg = PACK_EXPANSION_PATTERN (arg);
8286
8287 /* Deal with an injected-class-name used as a template template arg. */
8288 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8289 {
8290 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8291 if (TREE_CODE (t) == TEMPLATE_DECL)
8292 {
8293 if (cxx_dialect >= cxx11)
8294 /* OK under DR 1004. */;
8295 else if (complain & tf_warning_or_error)
8296 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8297 " used as template template argument", TYPE_NAME (arg));
8298 else if (flag_pedantic_errors)
8299 t = arg;
8300
8301 arg = t;
8302 }
8303 }
8304
8305 is_tmpl_type =
8306 ((TREE_CODE (arg) == TEMPLATE_DECL
8307 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8308 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8309 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8310 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8311
8312 if (is_tmpl_type
8313 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8314 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8315 arg = TYPE_STUB_DECL (arg);
8316
8317 is_type = TYPE_P (arg) || is_tmpl_type;
8318
8319 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8320 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8321 {
8322 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8323 {
8324 if (complain & tf_error)
8325 error ("invalid use of destructor %qE as a type", orig_arg);
8326 return error_mark_node;
8327 }
8328
8329 permerror (input_location,
8330 "to refer to a type member of a template parameter, "
8331 "use %<typename %E%>", orig_arg);
8332
8333 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8334 TREE_OPERAND (arg, 1),
8335 typename_type,
8336 complain);
8337 arg = orig_arg;
8338 is_type = 1;
8339 }
8340 if (is_type != requires_type)
8341 {
8342 if (in_decl)
8343 {
8344 if (complain & tf_error)
8345 {
8346 error ("type/value mismatch at argument %d in template "
8347 "parameter list for %qD",
8348 i + 1, in_decl);
8349 if (is_type)
8350 {
8351 /* The template argument is a type, but we're expecting
8352 an expression. */
8353 inform (input_location,
8354 " expected a constant of type %qT, got %qT",
8355 TREE_TYPE (parm),
8356 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8357 /* [temp.arg]/2: "In a template-argument, an ambiguity
8358 between a type-id and an expression is resolved to a
8359 type-id, regardless of the form of the corresponding
8360 template-parameter." So give the user a clue. */
8361 if (TREE_CODE (arg) == FUNCTION_TYPE)
8362 inform (input_location, " ambiguous template argument "
8363 "for non-type template parameter is treated as "
8364 "function type");
8365 }
8366 else if (requires_tmpl_type)
8367 inform (input_location,
8368 " expected a class template, got %qE", orig_arg);
8369 else
8370 inform (input_location,
8371 " expected a type, got %qE", orig_arg);
8372 }
8373 }
8374 return error_mark_node;
8375 }
8376 if (is_tmpl_type ^ requires_tmpl_type)
8377 {
8378 if (in_decl && (complain & tf_error))
8379 {
8380 error ("type/value mismatch at argument %d in template "
8381 "parameter list for %qD",
8382 i + 1, in_decl);
8383 if (is_tmpl_type)
8384 inform (input_location,
8385 " expected a type, got %qT", DECL_NAME (arg));
8386 else
8387 inform (input_location,
8388 " expected a class template, got %qT", orig_arg);
8389 }
8390 return error_mark_node;
8391 }
8392
8393 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8394 /* We already did the appropriate conversion when packing args. */
8395 val = orig_arg;
8396 else if (is_type)
8397 {
8398 if (requires_tmpl_type)
8399 {
8400 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8401 /* The number of argument required is not known yet.
8402 Just accept it for now. */
8403 val = orig_arg;
8404 else
8405 {
8406 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8407 tree argparm;
8408
8409 /* Strip alias templates that are equivalent to another
8410 template. */
8411 arg = get_underlying_template (arg);
8412 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8413
8414 if (coerce_template_template_parms (parmparm, argparm,
8415 complain, in_decl,
8416 args))
8417 {
8418 val = arg;
8419
8420 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8421 TEMPLATE_DECL. */
8422 if (val != error_mark_node)
8423 {
8424 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8425 val = TREE_TYPE (val);
8426 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8427 val = make_pack_expansion (val, complain);
8428 }
8429 }
8430 else
8431 {
8432 if (in_decl && (complain & tf_error))
8433 {
8434 error ("type/value mismatch at argument %d in "
8435 "template parameter list for %qD",
8436 i + 1, in_decl);
8437 inform (input_location,
8438 " expected a template of type %qD, got %qT",
8439 parm, orig_arg);
8440 }
8441
8442 val = error_mark_node;
8443 }
8444
8445 // Check that the constraints are compatible before allowing the
8446 // substitution.
8447 if (val != error_mark_node)
8448 if (!is_compatible_template_arg (parm, arg))
8449 {
8450 if (in_decl && (complain & tf_error))
8451 {
8452 error ("constraint mismatch at argument %d in "
8453 "template parameter list for %qD",
8454 i + 1, in_decl);
8455 inform (input_location, " expected %qD but got %qD",
8456 parm, arg);
8457 }
8458 val = error_mark_node;
8459 }
8460 }
8461 }
8462 else
8463 val = orig_arg;
8464 /* We only form one instance of each template specialization.
8465 Therefore, if we use a non-canonical variant (i.e., a
8466 typedef), any future messages referring to the type will use
8467 the typedef, which is confusing if those future uses do not
8468 themselves also use the typedef. */
8469 if (TYPE_P (val))
8470 val = canonicalize_type_argument (val, complain);
8471 }
8472 else
8473 {
8474 tree t = TREE_TYPE (parm);
8475
8476 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8477 > TMPL_ARGS_DEPTH (args))
8478 /* We don't have enough levels of args to do any substitution. This
8479 can happen in the context of -fnew-ttp-matching. */;
8480 else if (tree a = type_uses_auto (t))
8481 {
8482 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8483 if (t == error_mark_node)
8484 return error_mark_node;
8485 }
8486 else
8487 t = tsubst (t, args, complain, in_decl);
8488
8489 if (invalid_nontype_parm_type_p (t, complain))
8490 return error_mark_node;
8491
8492 if (t != TREE_TYPE (parm))
8493 t = canonicalize_type_argument (t, complain);
8494
8495 if (!type_dependent_expression_p (orig_arg)
8496 && !uses_template_parms (t))
8497 /* We used to call digest_init here. However, digest_init
8498 will report errors, which we don't want when complain
8499 is zero. More importantly, digest_init will try too
8500 hard to convert things: for example, `0' should not be
8501 converted to pointer type at this point according to
8502 the standard. Accepting this is not merely an
8503 extension, since deciding whether or not these
8504 conversions can occur is part of determining which
8505 function template to call, or whether a given explicit
8506 argument specification is valid. */
8507 val = convert_nontype_argument (t, orig_arg, complain);
8508 else
8509 {
8510 val = canonicalize_expr_argument (orig_arg, complain);
8511 val = maybe_convert_nontype_argument (t, val);
8512 }
8513
8514
8515 if (val == NULL_TREE)
8516 val = error_mark_node;
8517 else if (val == error_mark_node && (complain & tf_error))
8518 error_at (cp_expr_loc_or_input_loc (orig_arg),
8519 "could not convert template argument %qE from %qT to %qT",
8520 orig_arg, TREE_TYPE (orig_arg), t);
8521
8522 if (INDIRECT_REF_P (val))
8523 {
8524 /* Reject template arguments that are references to built-in
8525 functions with no library fallbacks. */
8526 const_tree inner = TREE_OPERAND (val, 0);
8527 const_tree innertype = TREE_TYPE (inner);
8528 if (innertype
8529 && TYPE_REF_P (innertype)
8530 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8531 && TREE_OPERAND_LENGTH (inner) > 0
8532 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8533 return error_mark_node;
8534 }
8535
8536 if (TREE_CODE (val) == SCOPE_REF)
8537 {
8538 /* Strip typedefs from the SCOPE_REF. */
8539 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8540 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8541 complain);
8542 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8543 QUALIFIED_NAME_IS_TEMPLATE (val));
8544 }
8545 }
8546
8547 return val;
8548 }
8549
8550 /* Coerces the remaining template arguments in INNER_ARGS (from
8551 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8552 Returns the coerced argument pack. PARM_IDX is the position of this
8553 parameter in the template parameter list. ARGS is the original
8554 template argument list. */
8555 static tree
8556 coerce_template_parameter_pack (tree parms,
8557 int parm_idx,
8558 tree args,
8559 tree inner_args,
8560 int arg_idx,
8561 tree new_args,
8562 int* lost,
8563 tree in_decl,
8564 tsubst_flags_t complain)
8565 {
8566 tree parm = TREE_VEC_ELT (parms, parm_idx);
8567 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8568 tree packed_args;
8569 tree argument_pack;
8570 tree packed_parms = NULL_TREE;
8571
8572 if (arg_idx > nargs)
8573 arg_idx = nargs;
8574
8575 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8576 {
8577 /* When the template parameter is a non-type template parameter pack
8578 or template template parameter pack whose type or template
8579 parameters use parameter packs, we know exactly how many arguments
8580 we are looking for. Build a vector of the instantiated decls for
8581 these template parameters in PACKED_PARMS. */
8582 /* We can't use make_pack_expansion here because it would interpret a
8583 _DECL as a use rather than a declaration. */
8584 tree decl = TREE_VALUE (parm);
8585 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8586 SET_PACK_EXPANSION_PATTERN (exp, decl);
8587 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8588 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8589
8590 TREE_VEC_LENGTH (args)--;
8591 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8592 TREE_VEC_LENGTH (args)++;
8593
8594 if (packed_parms == error_mark_node)
8595 return error_mark_node;
8596
8597 /* If we're doing a partial instantiation of a member template,
8598 verify that all of the types used for the non-type
8599 template parameter pack are, in fact, valid for non-type
8600 template parameters. */
8601 if (arg_idx < nargs
8602 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8603 {
8604 int j, len = TREE_VEC_LENGTH (packed_parms);
8605 for (j = 0; j < len; ++j)
8606 {
8607 tree t = TREE_VEC_ELT (packed_parms, j);
8608 if (TREE_CODE (t) == PARM_DECL
8609 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8610 return error_mark_node;
8611 }
8612 /* We don't know how many args we have yet, just
8613 use the unconverted ones for now. */
8614 return NULL_TREE;
8615 }
8616
8617 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8618 }
8619 /* Check if we have a placeholder pack, which indicates we're
8620 in the context of a introduction list. In that case we want
8621 to match this pack to the single placeholder. */
8622 else if (arg_idx < nargs
8623 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8624 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8625 {
8626 nargs = arg_idx + 1;
8627 packed_args = make_tree_vec (1);
8628 }
8629 else
8630 packed_args = make_tree_vec (nargs - arg_idx);
8631
8632 /* Convert the remaining arguments, which will be a part of the
8633 parameter pack "parm". */
8634 int first_pack_arg = arg_idx;
8635 for (; arg_idx < nargs; ++arg_idx)
8636 {
8637 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8638 tree actual_parm = TREE_VALUE (parm);
8639 int pack_idx = arg_idx - first_pack_arg;
8640
8641 if (packed_parms)
8642 {
8643 /* Once we've packed as many args as we have types, stop. */
8644 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8645 break;
8646 else if (PACK_EXPANSION_P (arg))
8647 /* We don't know how many args we have yet, just
8648 use the unconverted ones for now. */
8649 return NULL_TREE;
8650 else
8651 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8652 }
8653
8654 if (arg == error_mark_node)
8655 {
8656 if (complain & tf_error)
8657 error ("template argument %d is invalid", arg_idx + 1);
8658 }
8659 else
8660 arg = convert_template_argument (actual_parm,
8661 arg, new_args, complain, parm_idx,
8662 in_decl);
8663 if (arg == error_mark_node)
8664 (*lost)++;
8665 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8666 }
8667
8668 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8669 && TREE_VEC_LENGTH (packed_args) > 0)
8670 {
8671 if (complain & tf_error)
8672 error ("wrong number of template arguments (%d, should be %d)",
8673 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8674 return error_mark_node;
8675 }
8676
8677 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8678 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8679 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8680 else
8681 {
8682 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8683 TREE_CONSTANT (argument_pack) = 1;
8684 }
8685
8686 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8687 if (CHECKING_P)
8688 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8689 TREE_VEC_LENGTH (packed_args));
8690 return argument_pack;
8691 }
8692
8693 /* Returns the number of pack expansions in the template argument vector
8694 ARGS. */
8695
8696 static int
8697 pack_expansion_args_count (tree args)
8698 {
8699 int i;
8700 int count = 0;
8701 if (args)
8702 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8703 {
8704 tree elt = TREE_VEC_ELT (args, i);
8705 if (elt && PACK_EXPANSION_P (elt))
8706 ++count;
8707 }
8708 return count;
8709 }
8710
8711 /* Convert all template arguments to their appropriate types, and
8712 return a vector containing the innermost resulting template
8713 arguments. If any error occurs, return error_mark_node. Error and
8714 warning messages are issued under control of COMPLAIN.
8715
8716 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8717 for arguments not specified in ARGS. Otherwise, if
8718 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8719 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8720 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8721 ARGS. */
8722
8723 static tree
8724 coerce_template_parms (tree parms,
8725 tree args,
8726 tree in_decl,
8727 tsubst_flags_t complain,
8728 bool require_all_args,
8729 bool use_default_args)
8730 {
8731 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8732 tree orig_inner_args;
8733 tree inner_args;
8734 tree new_args;
8735 tree new_inner_args;
8736
8737 /* When used as a boolean value, indicates whether this is a
8738 variadic template parameter list. Since it's an int, we can also
8739 subtract it from nparms to get the number of non-variadic
8740 parameters. */
8741 int variadic_p = 0;
8742 int variadic_args_p = 0;
8743 int post_variadic_parms = 0;
8744
8745 /* Adjustment to nparms for fixed parameter packs. */
8746 int fixed_pack_adjust = 0;
8747 int fixed_packs = 0;
8748 int missing = 0;
8749
8750 /* Likewise for parameters with default arguments. */
8751 int default_p = 0;
8752
8753 if (args == error_mark_node)
8754 return error_mark_node;
8755
8756 nparms = TREE_VEC_LENGTH (parms);
8757
8758 /* Determine if there are any parameter packs or default arguments. */
8759 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8760 {
8761 tree parm = TREE_VEC_ELT (parms, parm_idx);
8762 if (variadic_p)
8763 ++post_variadic_parms;
8764 if (template_parameter_pack_p (TREE_VALUE (parm)))
8765 ++variadic_p;
8766 if (TREE_PURPOSE (parm))
8767 ++default_p;
8768 }
8769
8770 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8771 /* If there are no parameters that follow a parameter pack, we need to
8772 expand any argument packs so that we can deduce a parameter pack from
8773 some non-packed args followed by an argument pack, as in variadic85.C.
8774 If there are such parameters, we need to leave argument packs intact
8775 so the arguments are assigned properly. This can happen when dealing
8776 with a nested class inside a partial specialization of a class
8777 template, as in variadic92.C, or when deducing a template parameter pack
8778 from a sub-declarator, as in variadic114.C. */
8779 if (!post_variadic_parms)
8780 inner_args = expand_template_argument_pack (inner_args);
8781
8782 /* Count any pack expansion args. */
8783 variadic_args_p = pack_expansion_args_count (inner_args);
8784
8785 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8786 if ((nargs - variadic_args_p > nparms && !variadic_p)
8787 || (nargs < nparms - variadic_p
8788 && require_all_args
8789 && !variadic_args_p
8790 && (!use_default_args
8791 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8792 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8793 {
8794 bad_nargs:
8795 if (complain & tf_error)
8796 {
8797 if (variadic_p || default_p)
8798 {
8799 nparms -= variadic_p + default_p;
8800 error ("wrong number of template arguments "
8801 "(%d, should be at least %d)", nargs, nparms);
8802 }
8803 else
8804 error ("wrong number of template arguments "
8805 "(%d, should be %d)", nargs, nparms);
8806
8807 if (in_decl)
8808 inform (DECL_SOURCE_LOCATION (in_decl),
8809 "provided for %qD", in_decl);
8810 }
8811
8812 return error_mark_node;
8813 }
8814 /* We can't pass a pack expansion to a non-pack parameter of an alias
8815 template (DR 1430). */
8816 else if (in_decl
8817 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8818 || concept_definition_p (in_decl))
8819 && variadic_args_p
8820 && nargs - variadic_args_p < nparms - variadic_p)
8821 {
8822 if (complain & tf_error)
8823 {
8824 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8825 {
8826 tree arg = TREE_VEC_ELT (inner_args, i);
8827 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8828
8829 if (PACK_EXPANSION_P (arg)
8830 && !template_parameter_pack_p (parm))
8831 {
8832 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8833 error_at (location_of (arg),
8834 "pack expansion argument for non-pack parameter "
8835 "%qD of alias template %qD", parm, in_decl);
8836 else
8837 error_at (location_of (arg),
8838 "pack expansion argument for non-pack parameter "
8839 "%qD of concept %qD", parm, in_decl);
8840 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8841 goto found;
8842 }
8843 }
8844 gcc_unreachable ();
8845 found:;
8846 }
8847 return error_mark_node;
8848 }
8849
8850 /* We need to evaluate the template arguments, even though this
8851 template-id may be nested within a "sizeof". */
8852 cp_evaluated ev;
8853
8854 new_inner_args = make_tree_vec (nparms);
8855 new_args = add_outermost_template_args (args, new_inner_args);
8856 int pack_adjust = 0;
8857 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8858 {
8859 tree arg;
8860 tree parm;
8861
8862 /* Get the Ith template parameter. */
8863 parm = TREE_VEC_ELT (parms, parm_idx);
8864
8865 if (parm == error_mark_node)
8866 {
8867 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8868 continue;
8869 }
8870
8871 /* Calculate the next argument. */
8872 if (arg_idx < nargs)
8873 arg = TREE_VEC_ELT (inner_args, arg_idx);
8874 else
8875 arg = NULL_TREE;
8876
8877 if (template_parameter_pack_p (TREE_VALUE (parm))
8878 && (arg || require_all_args || !(complain & tf_partial))
8879 && !(arg && ARGUMENT_PACK_P (arg)))
8880 {
8881 /* Some arguments will be placed in the
8882 template parameter pack PARM. */
8883 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8884 inner_args, arg_idx,
8885 new_args, &lost,
8886 in_decl, complain);
8887
8888 if (arg == NULL_TREE)
8889 {
8890 /* We don't know how many args we have yet, just use the
8891 unconverted (and still packed) ones for now. */
8892 new_inner_args = orig_inner_args;
8893 arg_idx = nargs;
8894 break;
8895 }
8896
8897 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8898
8899 /* Store this argument. */
8900 if (arg == error_mark_node)
8901 {
8902 lost++;
8903 /* We are done with all of the arguments. */
8904 arg_idx = nargs;
8905 break;
8906 }
8907 else
8908 {
8909 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8910 arg_idx += pack_adjust;
8911 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8912 {
8913 ++fixed_packs;
8914 fixed_pack_adjust += pack_adjust;
8915 }
8916 }
8917
8918 continue;
8919 }
8920 else if (arg)
8921 {
8922 if (PACK_EXPANSION_P (arg))
8923 {
8924 /* "If every valid specialization of a variadic template
8925 requires an empty template parameter pack, the template is
8926 ill-formed, no diagnostic required." So check that the
8927 pattern works with this parameter. */
8928 tree pattern = PACK_EXPANSION_PATTERN (arg);
8929 tree conv = convert_template_argument (TREE_VALUE (parm),
8930 pattern, new_args,
8931 complain, parm_idx,
8932 in_decl);
8933 if (conv == error_mark_node)
8934 {
8935 if (complain & tf_error)
8936 inform (input_location, "so any instantiation with a "
8937 "non-empty parameter pack would be ill-formed");
8938 ++lost;
8939 }
8940 else if (TYPE_P (conv) && !TYPE_P (pattern))
8941 /* Recover from missing typename. */
8942 TREE_VEC_ELT (inner_args, arg_idx)
8943 = make_pack_expansion (conv, complain);
8944
8945 /* We don't know how many args we have yet, just
8946 use the unconverted ones for now. */
8947 new_inner_args = inner_args;
8948 arg_idx = nargs;
8949 break;
8950 }
8951 }
8952 else if (require_all_args)
8953 {
8954 /* There must be a default arg in this case. */
8955 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8956 complain, in_decl);
8957 /* The position of the first default template argument,
8958 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8959 Record that. */
8960 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8961 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8962 arg_idx - pack_adjust);
8963 }
8964 else
8965 break;
8966
8967 if (arg == error_mark_node)
8968 {
8969 if (complain & tf_error)
8970 error ("template argument %d is invalid", arg_idx + 1);
8971 }
8972 else if (!arg)
8973 {
8974 /* This can occur if there was an error in the template
8975 parameter list itself (which we would already have
8976 reported) that we are trying to recover from, e.g., a class
8977 template with a parameter list such as
8978 template<typename..., typename> (cpp0x/variadic150.C). */
8979 ++lost;
8980
8981 /* This can also happen with a fixed parameter pack (71834). */
8982 if (arg_idx >= nargs)
8983 ++missing;
8984 }
8985 else
8986 arg = convert_template_argument (TREE_VALUE (parm),
8987 arg, new_args, complain,
8988 parm_idx, in_decl);
8989
8990 if (arg == error_mark_node)
8991 lost++;
8992
8993 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8994 }
8995
8996 if (missing || arg_idx < nargs - variadic_args_p)
8997 {
8998 /* If we had fixed parameter packs, we didn't know how many arguments we
8999 actually needed earlier; now we do. */
9000 nparms += fixed_pack_adjust;
9001 variadic_p -= fixed_packs;
9002 goto bad_nargs;
9003 }
9004
9005 if (arg_idx < nargs)
9006 {
9007 /* We had some pack expansion arguments that will only work if the packs
9008 are empty, but wait until instantiation time to complain.
9009 See variadic-ttp3.C. */
9010
9011 /* Except that we can't provide empty packs to alias templates or
9012 concepts when there are no corresponding parameters. Basically,
9013 we can get here with this:
9014
9015 template<typename T> concept C = true;
9016
9017 template<typename... Args>
9018 requires C<Args...>
9019 void f();
9020
9021 When parsing C<Args...>, we try to form a concept check of
9022 C<?, Args...>. Without the extra check for substituting an empty
9023 pack past the last parameter, we can accept the check as valid.
9024
9025 FIXME: This may be valid for alias templates (but I doubt it).
9026
9027 FIXME: The error could be better also. */
9028 if (in_decl && concept_definition_p (in_decl))
9029 {
9030 if (complain & tf_error)
9031 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9032 "too many arguments");
9033 return error_mark_node;
9034 }
9035
9036 int len = nparms + (nargs - arg_idx);
9037 tree args = make_tree_vec (len);
9038 int i = 0;
9039 for (; i < nparms; ++i)
9040 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9041 for (; i < len; ++i, ++arg_idx)
9042 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9043 arg_idx - pack_adjust);
9044 new_inner_args = args;
9045 }
9046
9047 if (lost)
9048 {
9049 gcc_assert (!(complain & tf_error) || seen_error ());
9050 return error_mark_node;
9051 }
9052
9053 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9054 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9055 TREE_VEC_LENGTH (new_inner_args));
9056
9057 return new_inner_args;
9058 }
9059
9060 /* Convert all template arguments to their appropriate types, and
9061 return a vector containing the innermost resulting template
9062 arguments. If any error occurs, return error_mark_node. Error and
9063 warning messages are not issued.
9064
9065 Note that no function argument deduction is performed, and default
9066 arguments are used to fill in unspecified arguments. */
9067 tree
9068 coerce_template_parms (tree parms, tree args, tree in_decl)
9069 {
9070 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9071 }
9072
9073 /* Convert all template arguments to their appropriate type, and
9074 instantiate default arguments as needed. This returns a vector
9075 containing the innermost resulting template arguments, or
9076 error_mark_node if unsuccessful. */
9077 tree
9078 coerce_template_parms (tree parms, tree args, tree in_decl,
9079 tsubst_flags_t complain)
9080 {
9081 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9082 }
9083
9084 /* Like coerce_template_parms. If PARMS represents all template
9085 parameters levels, this function returns a vector of vectors
9086 representing all the resulting argument levels. Note that in this
9087 case, only the innermost arguments are coerced because the
9088 outermost ones are supposed to have been coerced already.
9089
9090 Otherwise, if PARMS represents only (the innermost) vector of
9091 parameters, this function returns a vector containing just the
9092 innermost resulting arguments. */
9093
9094 static tree
9095 coerce_innermost_template_parms (tree parms,
9096 tree args,
9097 tree in_decl,
9098 tsubst_flags_t complain,
9099 bool require_all_args,
9100 bool use_default_args)
9101 {
9102 int parms_depth = TMPL_PARMS_DEPTH (parms);
9103 int args_depth = TMPL_ARGS_DEPTH (args);
9104 tree coerced_args;
9105
9106 if (parms_depth > 1)
9107 {
9108 coerced_args = make_tree_vec (parms_depth);
9109 tree level;
9110 int cur_depth;
9111
9112 for (level = parms, cur_depth = parms_depth;
9113 parms_depth > 0 && level != NULL_TREE;
9114 level = TREE_CHAIN (level), --cur_depth)
9115 {
9116 tree l;
9117 if (cur_depth == args_depth)
9118 l = coerce_template_parms (TREE_VALUE (level),
9119 args, in_decl, complain,
9120 require_all_args,
9121 use_default_args);
9122 else
9123 l = TMPL_ARGS_LEVEL (args, cur_depth);
9124
9125 if (l == error_mark_node)
9126 return error_mark_node;
9127
9128 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9129 }
9130 }
9131 else
9132 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9133 args, in_decl, complain,
9134 require_all_args,
9135 use_default_args);
9136 return coerced_args;
9137 }
9138
9139 /* Returns true if T is a wrapper to make a C++20 template parameter
9140 object const. */
9141
9142 static bool
9143 class_nttp_const_wrapper_p (tree t)
9144 {
9145 if (cxx_dialect < cxx20)
9146 return false;
9147 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9148 && CP_TYPE_CONST_P (TREE_TYPE (t))
9149 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9150 }
9151
9152 /* Returns 1 if template args OT and NT are equivalent. */
9153
9154 int
9155 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9156 {
9157 if (nt == ot)
9158 return 1;
9159 if (nt == NULL_TREE || ot == NULL_TREE)
9160 return false;
9161 if (nt == any_targ_node || ot == any_targ_node)
9162 return true;
9163
9164 if (class_nttp_const_wrapper_p (nt))
9165 nt = TREE_OPERAND (nt, 0);
9166 if (class_nttp_const_wrapper_p (ot))
9167 ot = TREE_OPERAND (ot, 0);
9168
9169 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9170 /* For member templates */
9171 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9172 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9173 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9174 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9175 PACK_EXPANSION_PATTERN (nt))
9176 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9177 PACK_EXPANSION_EXTRA_ARGS (nt)));
9178 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9179 return cp_tree_equal (ot, nt);
9180 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9181 gcc_unreachable ();
9182 else if (TYPE_P (nt) || TYPE_P (ot))
9183 {
9184 if (!(TYPE_P (nt) && TYPE_P (ot)))
9185 return false;
9186 /* Don't treat an alias template specialization with dependent
9187 arguments as equivalent to its underlying type when used as a
9188 template argument; we need them to be distinct so that we
9189 substitute into the specialization arguments at instantiation
9190 time. And aliases can't be equivalent without being ==, so
9191 we don't need to look any deeper.
9192
9193 During partial ordering, however, we need to treat them normally so
9194 that we can order uses of the same alias with different
9195 cv-qualification (79960). */
9196 if (!partial_order
9197 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9198 return false;
9199 else
9200 return same_type_p (ot, nt);
9201 }
9202 else
9203 {
9204 /* Try to treat a template non-type argument that has been converted
9205 to the parameter type as equivalent to one that hasn't yet. */
9206 for (enum tree_code code1 = TREE_CODE (ot);
9207 CONVERT_EXPR_CODE_P (code1)
9208 || code1 == NON_LVALUE_EXPR;
9209 code1 = TREE_CODE (ot))
9210 ot = TREE_OPERAND (ot, 0);
9211
9212 for (enum tree_code code2 = TREE_CODE (nt);
9213 CONVERT_EXPR_CODE_P (code2)
9214 || code2 == NON_LVALUE_EXPR;
9215 code2 = TREE_CODE (nt))
9216 nt = TREE_OPERAND (nt, 0);
9217
9218 return cp_tree_equal (ot, nt);
9219 }
9220 }
9221
9222 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9223 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9224 NEWARG_PTR with the offending arguments if they are non-NULL. */
9225
9226 int
9227 comp_template_args (tree oldargs, tree newargs,
9228 tree *oldarg_ptr, tree *newarg_ptr,
9229 bool partial_order)
9230 {
9231 int i;
9232
9233 if (oldargs == newargs)
9234 return 1;
9235
9236 if (!oldargs || !newargs)
9237 return 0;
9238
9239 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9240 return 0;
9241
9242 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9243 {
9244 tree nt = TREE_VEC_ELT (newargs, i);
9245 tree ot = TREE_VEC_ELT (oldargs, i);
9246
9247 if (! template_args_equal (ot, nt, partial_order))
9248 {
9249 if (oldarg_ptr != NULL)
9250 *oldarg_ptr = ot;
9251 if (newarg_ptr != NULL)
9252 *newarg_ptr = nt;
9253 return 0;
9254 }
9255 }
9256 return 1;
9257 }
9258
9259 inline bool
9260 comp_template_args_porder (tree oargs, tree nargs)
9261 {
9262 return comp_template_args (oargs, nargs, NULL, NULL, true);
9263 }
9264
9265 /* Implement a freelist interface for objects of type T.
9266
9267 Head is a separate object, rather than a regular member, so that we
9268 can define it as a GTY deletable pointer, which is highly
9269 desirable. A data member could be declared that way, but then the
9270 containing object would implicitly get GTY((user)), which would
9271 prevent us from instantiating freelists as global objects.
9272 Although this way we can create freelist global objects, they're
9273 such thin wrappers that instantiating temporaries at every use
9274 loses nothing and saves permanent storage for the freelist object.
9275
9276 Member functions next, anew, poison and reinit have default
9277 implementations that work for most of the types we're interested
9278 in, but if they don't work for some type, they should be explicitly
9279 specialized. See the comments before them for requirements, and
9280 the example specializations for the tree_list_freelist. */
9281 template <typename T>
9282 class freelist
9283 {
9284 /* Return the next object in a chain. We could just do type
9285 punning, but if we access the object with its underlying type, we
9286 avoid strict-aliasing trouble. This needs only work between
9287 poison and reinit. */
9288 static T *&next (T *obj) { return obj->next; }
9289
9290 /* Return a newly allocated, uninitialized or minimally-initialized
9291 object of type T. Any initialization performed by anew should
9292 either remain across the life of the object and the execution of
9293 poison, or be redone by reinit. */
9294 static T *anew () { return ggc_alloc<T> (); }
9295
9296 /* Optionally scribble all over the bits holding the object, so that
9297 they become (mostly?) uninitialized memory. This is called while
9298 preparing to make the object part of the free list. */
9299 static void poison (T *obj) {
9300 T *p ATTRIBUTE_UNUSED = obj;
9301 T **q ATTRIBUTE_UNUSED = &next (obj);
9302
9303 #ifdef ENABLE_GC_CHECKING
9304 /* Poison the data, to indicate the data is garbage. */
9305 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9306 memset (p, 0xa5, sizeof (*p));
9307 #endif
9308 /* Let valgrind know the object is free. */
9309 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9310
9311 /* Let valgrind know the next portion of the object is available,
9312 but uninitialized. */
9313 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9314 }
9315
9316 /* Bring an object that underwent at least one lifecycle after anew
9317 and before the most recent free and poison, back to a usable
9318 state, reinitializing whatever is needed for it to be
9319 functionally equivalent to an object just allocated and returned
9320 by anew. This may poison or clear the next field, used by
9321 freelist housekeeping after poison was called. */
9322 static void reinit (T *obj) {
9323 T **q ATTRIBUTE_UNUSED = &next (obj);
9324
9325 #ifdef ENABLE_GC_CHECKING
9326 memset (q, 0xa5, sizeof (*q));
9327 #endif
9328 /* Let valgrind know the entire object is available, but
9329 uninitialized. */
9330 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9331 }
9332
9333 /* Reference a GTY-deletable pointer that points to the first object
9334 in the free list proper. */
9335 T *&head;
9336 public:
9337 /* Construct a freelist object chaining objects off of HEAD. */
9338 freelist (T *&head) : head(head) {}
9339
9340 /* Add OBJ to the free object list. The former head becomes OBJ's
9341 successor. */
9342 void free (T *obj)
9343 {
9344 poison (obj);
9345 next (obj) = head;
9346 head = obj;
9347 }
9348
9349 /* Take an object from the free list, if one is available, or
9350 allocate a new one. Objects taken from the free list should be
9351 regarded as filled with garbage, except for bits that are
9352 configured to be preserved across free and alloc. */
9353 T *alloc ()
9354 {
9355 if (head)
9356 {
9357 T *obj = head;
9358 head = next (head);
9359 reinit (obj);
9360 return obj;
9361 }
9362 else
9363 return anew ();
9364 }
9365 };
9366
9367 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9368 want to allocate a TREE_LIST using the usual interface, and ensure
9369 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9370 build_tree_list logic in reinit, so this could go out of sync. */
9371 template <>
9372 inline tree &
9373 freelist<tree_node>::next (tree obj)
9374 {
9375 return TREE_CHAIN (obj);
9376 }
9377 template <>
9378 inline tree
9379 freelist<tree_node>::anew ()
9380 {
9381 return build_tree_list (NULL, NULL);
9382 }
9383 template <>
9384 inline void
9385 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9386 {
9387 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9388 tree p ATTRIBUTE_UNUSED = obj;
9389 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9390 tree *q ATTRIBUTE_UNUSED = &next (obj);
9391
9392 #ifdef ENABLE_GC_CHECKING
9393 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9394
9395 /* Poison the data, to indicate the data is garbage. */
9396 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9397 memset (p, 0xa5, size);
9398 #endif
9399 /* Let valgrind know the object is free. */
9400 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9401 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9402 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9403 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9404
9405 #ifdef ENABLE_GC_CHECKING
9406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9407 /* Keep TREE_CHAIN functional. */
9408 TREE_SET_CODE (obj, TREE_LIST);
9409 #else
9410 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9411 #endif
9412 }
9413 template <>
9414 inline void
9415 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9416 {
9417 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9418
9419 #ifdef ENABLE_GC_CHECKING
9420 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9421 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9422 memset (obj, 0, sizeof (tree_list));
9423 #endif
9424
9425 /* Let valgrind know the entire object is available, but
9426 uninitialized. */
9427 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9428
9429 #ifdef ENABLE_GC_CHECKING
9430 TREE_SET_CODE (obj, TREE_LIST);
9431 #else
9432 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9433 #endif
9434 }
9435
9436 /* Point to the first object in the TREE_LIST freelist. */
9437 static GTY((deletable)) tree tree_list_freelist_head;
9438 /* Return the/an actual TREE_LIST freelist. */
9439 static inline freelist<tree_node>
9440 tree_list_freelist ()
9441 {
9442 return tree_list_freelist_head;
9443 }
9444
9445 /* Point to the first object in the tinst_level freelist. */
9446 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9447 /* Return the/an actual tinst_level freelist. */
9448 static inline freelist<tinst_level>
9449 tinst_level_freelist ()
9450 {
9451 return tinst_level_freelist_head;
9452 }
9453
9454 /* Point to the first object in the pending_template freelist. */
9455 static GTY((deletable)) pending_template *pending_template_freelist_head;
9456 /* Return the/an actual pending_template freelist. */
9457 static inline freelist<pending_template>
9458 pending_template_freelist ()
9459 {
9460 return pending_template_freelist_head;
9461 }
9462
9463 /* Build the TREE_LIST object out of a split list, store it
9464 permanently, and return it. */
9465 tree
9466 tinst_level::to_list ()
9467 {
9468 gcc_assert (split_list_p ());
9469 tree ret = tree_list_freelist ().alloc ();
9470 TREE_PURPOSE (ret) = tldcl;
9471 TREE_VALUE (ret) = targs;
9472 tldcl = ret;
9473 targs = NULL;
9474 gcc_assert (tree_list_p ());
9475 return ret;
9476 }
9477
9478 const unsigned short tinst_level::refcount_infinity;
9479
9480 /* Increment OBJ's refcount unless it is already infinite. */
9481 static tinst_level *
9482 inc_refcount_use (tinst_level *obj)
9483 {
9484 if (obj && obj->refcount != tinst_level::refcount_infinity)
9485 ++obj->refcount;
9486 return obj;
9487 }
9488
9489 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9490 void
9491 tinst_level::free (tinst_level *obj)
9492 {
9493 if (obj->tree_list_p ())
9494 tree_list_freelist ().free (obj->get_node ());
9495 tinst_level_freelist ().free (obj);
9496 }
9497
9498 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9499 OBJ's DECL and OBJ, and start over with the tinst_level object that
9500 used to be referenced by OBJ's NEXT. */
9501 static void
9502 dec_refcount_use (tinst_level *obj)
9503 {
9504 while (obj
9505 && obj->refcount != tinst_level::refcount_infinity
9506 && !--obj->refcount)
9507 {
9508 tinst_level *next = obj->next;
9509 tinst_level::free (obj);
9510 obj = next;
9511 }
9512 }
9513
9514 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9515 and of the former PTR. Omitting the second argument is equivalent
9516 to passing (T*)NULL; this is allowed because passing the
9517 zero-valued integral constant NULL confuses type deduction and/or
9518 overload resolution. */
9519 template <typename T>
9520 static void
9521 set_refcount_ptr (T *& ptr, T *obj = NULL)
9522 {
9523 T *save = ptr;
9524 ptr = inc_refcount_use (obj);
9525 dec_refcount_use (save);
9526 }
9527
9528 static void
9529 add_pending_template (tree d)
9530 {
9531 tree ti = (TYPE_P (d)
9532 ? CLASSTYPE_TEMPLATE_INFO (d)
9533 : DECL_TEMPLATE_INFO (d));
9534 struct pending_template *pt;
9535 int level;
9536
9537 if (TI_PENDING_TEMPLATE_FLAG (ti))
9538 return;
9539
9540 /* We are called both from instantiate_decl, where we've already had a
9541 tinst_level pushed, and instantiate_template, where we haven't.
9542 Compensate. */
9543 gcc_assert (TREE_CODE (d) != TREE_LIST);
9544 level = !current_tinst_level
9545 || current_tinst_level->maybe_get_node () != d;
9546
9547 if (level)
9548 push_tinst_level (d);
9549
9550 pt = pending_template_freelist ().alloc ();
9551 pt->next = NULL;
9552 pt->tinst = NULL;
9553 set_refcount_ptr (pt->tinst, current_tinst_level);
9554 if (last_pending_template)
9555 last_pending_template->next = pt;
9556 else
9557 pending_templates = pt;
9558
9559 last_pending_template = pt;
9560
9561 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9562
9563 if (level)
9564 pop_tinst_level ();
9565 }
9566
9567
9568 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9569 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9570 documentation for TEMPLATE_ID_EXPR. */
9571
9572 tree
9573 lookup_template_function (tree fns, tree arglist)
9574 {
9575 if (fns == error_mark_node || arglist == error_mark_node)
9576 return error_mark_node;
9577
9578 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9579
9580 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9581 {
9582 error ("%q#D is not a function template", fns);
9583 return error_mark_node;
9584 }
9585
9586 if (BASELINK_P (fns))
9587 {
9588 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9589 unknown_type_node,
9590 BASELINK_FUNCTIONS (fns),
9591 arglist);
9592 return fns;
9593 }
9594
9595 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9596 }
9597
9598 /* Within the scope of a template class S<T>, the name S gets bound
9599 (in build_self_reference) to a TYPE_DECL for the class, not a
9600 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9601 or one of its enclosing classes, and that type is a template,
9602 return the associated TEMPLATE_DECL. Otherwise, the original
9603 DECL is returned.
9604
9605 Also handle the case when DECL is a TREE_LIST of ambiguous
9606 injected-class-names from different bases. */
9607
9608 tree
9609 maybe_get_template_decl_from_type_decl (tree decl)
9610 {
9611 if (decl == NULL_TREE)
9612 return decl;
9613
9614 /* DR 176: A lookup that finds an injected-class-name (10.2
9615 [class.member.lookup]) can result in an ambiguity in certain cases
9616 (for example, if it is found in more than one base class). If all of
9617 the injected-class-names that are found refer to specializations of
9618 the same class template, and if the name is followed by a
9619 template-argument-list, the reference refers to the class template
9620 itself and not a specialization thereof, and is not ambiguous. */
9621 if (TREE_CODE (decl) == TREE_LIST)
9622 {
9623 tree t, tmpl = NULL_TREE;
9624 for (t = decl; t; t = TREE_CHAIN (t))
9625 {
9626 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9627 if (!tmpl)
9628 tmpl = elt;
9629 else if (tmpl != elt)
9630 break;
9631 }
9632 if (tmpl && t == NULL_TREE)
9633 return tmpl;
9634 else
9635 return decl;
9636 }
9637
9638 return (decl != NULL_TREE
9639 && DECL_SELF_REFERENCE_P (decl)
9640 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9641 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9642 }
9643
9644 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9645 parameters, find the desired type.
9646
9647 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9648
9649 IN_DECL, if non-NULL, is the template declaration we are trying to
9650 instantiate.
9651
9652 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9653 the class we are looking up.
9654
9655 Issue error and warning messages under control of COMPLAIN.
9656
9657 If the template class is really a local class in a template
9658 function, then the FUNCTION_CONTEXT is the function in which it is
9659 being instantiated.
9660
9661 ??? Note that this function is currently called *twice* for each
9662 template-id: the first time from the parser, while creating the
9663 incomplete type (finish_template_type), and the second type during the
9664 real instantiation (instantiate_template_class). This is surely something
9665 that we want to avoid. It also causes some problems with argument
9666 coercion (see convert_nontype_argument for more information on this). */
9667
9668 static tree
9669 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9670 int entering_scope, tsubst_flags_t complain)
9671 {
9672 tree templ = NULL_TREE, parmlist;
9673 tree t;
9674 spec_entry **slot;
9675 spec_entry *entry;
9676 spec_entry elt;
9677 hashval_t hash;
9678
9679 if (identifier_p (d1))
9680 {
9681 tree value = innermost_non_namespace_value (d1);
9682 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9683 templ = value;
9684 else
9685 {
9686 if (context)
9687 push_decl_namespace (context);
9688 templ = lookup_name (d1);
9689 templ = maybe_get_template_decl_from_type_decl (templ);
9690 if (context)
9691 pop_decl_namespace ();
9692 }
9693 if (templ)
9694 context = DECL_CONTEXT (templ);
9695 }
9696 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9697 {
9698 tree type = TREE_TYPE (d1);
9699
9700 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9701 an implicit typename for the second A. Deal with it. */
9702 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9703 type = TREE_TYPE (type);
9704
9705 if (CLASSTYPE_TEMPLATE_INFO (type))
9706 {
9707 templ = CLASSTYPE_TI_TEMPLATE (type);
9708 d1 = DECL_NAME (templ);
9709 }
9710 }
9711 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9712 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9713 {
9714 templ = TYPE_TI_TEMPLATE (d1);
9715 d1 = DECL_NAME (templ);
9716 }
9717 else if (DECL_TYPE_TEMPLATE_P (d1))
9718 {
9719 templ = d1;
9720 d1 = DECL_NAME (templ);
9721 context = DECL_CONTEXT (templ);
9722 }
9723 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9724 {
9725 templ = d1;
9726 d1 = DECL_NAME (templ);
9727 }
9728
9729 /* Issue an error message if we didn't find a template. */
9730 if (! templ)
9731 {
9732 if (complain & tf_error)
9733 error ("%qT is not a template", d1);
9734 return error_mark_node;
9735 }
9736
9737 if (TREE_CODE (templ) != TEMPLATE_DECL
9738 /* Make sure it's a user visible template, if it was named by
9739 the user. */
9740 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9741 && !PRIMARY_TEMPLATE_P (templ)))
9742 {
9743 if (complain & tf_error)
9744 {
9745 error ("non-template type %qT used as a template", d1);
9746 if (in_decl)
9747 error ("for template declaration %q+D", in_decl);
9748 }
9749 return error_mark_node;
9750 }
9751
9752 complain &= ~tf_user;
9753
9754 /* An alias that just changes the name of a template is equivalent to the
9755 other template, so if any of the arguments are pack expansions, strip
9756 the alias to avoid problems with a pack expansion passed to a non-pack
9757 alias template parameter (DR 1430). */
9758 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9759 templ = get_underlying_template (templ);
9760
9761 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9762 {
9763 tree parm;
9764 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9765 if (arglist2 == error_mark_node
9766 || (!uses_template_parms (arglist2)
9767 && check_instantiated_args (templ, arglist2, complain)))
9768 return error_mark_node;
9769
9770 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9771 return parm;
9772 }
9773 else
9774 {
9775 tree template_type = TREE_TYPE (templ);
9776 tree gen_tmpl;
9777 tree type_decl;
9778 tree found = NULL_TREE;
9779 int arg_depth;
9780 int parm_depth;
9781 int is_dependent_type;
9782 int use_partial_inst_tmpl = false;
9783
9784 if (template_type == error_mark_node)
9785 /* An error occurred while building the template TEMPL, and a
9786 diagnostic has most certainly been emitted for that
9787 already. Let's propagate that error. */
9788 return error_mark_node;
9789
9790 gen_tmpl = most_general_template (templ);
9791 if (modules_p ())
9792 {
9793 tree origin = get_originating_module_decl (gen_tmpl);
9794 load_pending_specializations (CP_DECL_CONTEXT (origin),
9795 DECL_NAME (origin));
9796 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl))
9797 lazy_load_specializations (gen_tmpl);
9798 }
9799
9800 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9801 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9802 arg_depth = TMPL_ARGS_DEPTH (arglist);
9803
9804 if (arg_depth == 1 && parm_depth > 1)
9805 {
9806 /* We've been given an incomplete set of template arguments.
9807 For example, given:
9808
9809 template <class T> struct S1 {
9810 template <class U> struct S2 {};
9811 template <class U> struct S2<U*> {};
9812 };
9813
9814 we will be called with an ARGLIST of `U*', but the
9815 TEMPLATE will be `template <class T> template
9816 <class U> struct S1<T>::S2'. We must fill in the missing
9817 arguments. */
9818 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9819 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9820 arg_depth = TMPL_ARGS_DEPTH (arglist);
9821 }
9822
9823 /* Now we should have enough arguments. */
9824 gcc_assert (parm_depth == arg_depth);
9825
9826 /* From here on, we're only interested in the most general
9827 template. */
9828
9829 /* Calculate the BOUND_ARGS. These will be the args that are
9830 actually tsubst'd into the definition to create the
9831 instantiation. */
9832 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9833 complain,
9834 /*require_all_args=*/true,
9835 /*use_default_args=*/true);
9836
9837 if (arglist == error_mark_node)
9838 /* We were unable to bind the arguments. */
9839 return error_mark_node;
9840
9841 /* In the scope of a template class, explicit references to the
9842 template class refer to the type of the template, not any
9843 instantiation of it. For example, in:
9844
9845 template <class T> class C { void f(C<T>); }
9846
9847 the `C<T>' is just the same as `C'. Outside of the
9848 class, however, such a reference is an instantiation. */
9849 if (entering_scope
9850 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9851 || currently_open_class (template_type))
9852 {
9853 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9854
9855 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9856 return template_type;
9857 }
9858
9859 /* If we already have this specialization, return it. */
9860 elt.tmpl = gen_tmpl;
9861 elt.args = arglist;
9862 elt.spec = NULL_TREE;
9863 hash = spec_hasher::hash (&elt);
9864 entry = type_specializations->find_with_hash (&elt, hash);
9865
9866 if (entry)
9867 return entry->spec;
9868
9869 /* If the template's constraints are not satisfied,
9870 then we cannot form a valid type.
9871
9872 Note that the check is deferred until after the hash
9873 lookup. This prevents redundant checks on previously
9874 instantiated specializations. */
9875 if (flag_concepts
9876 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9877 && !constraints_satisfied_p (gen_tmpl, arglist))
9878 {
9879 if (complain & tf_error)
9880 {
9881 auto_diagnostic_group d;
9882 error ("template constraint failure for %qD", gen_tmpl);
9883 diagnose_constraints (input_location, gen_tmpl, arglist);
9884 }
9885 return error_mark_node;
9886 }
9887
9888 is_dependent_type = uses_template_parms (arglist);
9889
9890 /* If the deduced arguments are invalid, then the binding
9891 failed. */
9892 if (!is_dependent_type
9893 && check_instantiated_args (gen_tmpl,
9894 INNERMOST_TEMPLATE_ARGS (arglist),
9895 complain))
9896 return error_mark_node;
9897
9898 if (!is_dependent_type
9899 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9900 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9901 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9902 /* This occurs when the user has tried to define a tagged type
9903 in a scope that forbids it. We emitted an error during the
9904 parse. We didn't complete the bail out then, so here we
9905 are. */
9906 return error_mark_node;
9907
9908 context = DECL_CONTEXT (gen_tmpl);
9909 if (context && TYPE_P (context))
9910 {
9911 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9912 context = complete_type (context);
9913 }
9914 else
9915 context = tsubst (context, arglist, complain, in_decl);
9916
9917 if (context == error_mark_node)
9918 return error_mark_node;
9919
9920 if (!context)
9921 context = global_namespace;
9922
9923 /* Create the type. */
9924 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9925 {
9926 /* The user referred to a specialization of an alias
9927 template represented by GEN_TMPL.
9928
9929 [temp.alias]/2 says:
9930
9931 When a template-id refers to the specialization of an
9932 alias template, it is equivalent to the associated
9933 type obtained by substitution of its
9934 template-arguments for the template-parameters in the
9935 type-id of the alias template. */
9936
9937 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9938 /* Note that the call above (by indirectly calling
9939 register_specialization in tsubst_decl) registers the
9940 TYPE_DECL representing the specialization of the alias
9941 template. So next time someone substitutes ARGLIST for
9942 the template parms into the alias template (GEN_TMPL),
9943 she'll get that TYPE_DECL back. */
9944
9945 if (t == error_mark_node)
9946 return t;
9947 }
9948 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9949 {
9950 if (!is_dependent_type)
9951 {
9952 set_current_access_from_decl (TYPE_NAME (template_type));
9953 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9954 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9955 arglist, complain, in_decl),
9956 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9957 arglist, complain, in_decl),
9958 SCOPED_ENUM_P (template_type), NULL);
9959
9960 if (t == error_mark_node)
9961 return t;
9962 }
9963 else
9964 {
9965 /* We don't want to call start_enum for this type, since
9966 the values for the enumeration constants may involve
9967 template parameters. And, no one should be interested
9968 in the enumeration constants for such a type. */
9969 t = cxx_make_type (ENUMERAL_TYPE);
9970 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9971 }
9972 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9973 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9974 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9975 }
9976 else if (CLASS_TYPE_P (template_type))
9977 {
9978 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9979 instantiated here. */
9980 gcc_assert (!LAMBDA_TYPE_P (template_type));
9981
9982 t = make_class_type (TREE_CODE (template_type));
9983 CLASSTYPE_DECLARED_CLASS (t)
9984 = CLASSTYPE_DECLARED_CLASS (template_type);
9985 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9986
9987 /* A local class. Make sure the decl gets registered properly. */
9988 if (context == current_function_decl)
9989 if (pushtag (DECL_NAME (gen_tmpl), t)
9990 == error_mark_node)
9991 return error_mark_node;
9992
9993 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9994 /* This instantiation is another name for the primary
9995 template type. Set the TYPE_CANONICAL field
9996 appropriately. */
9997 TYPE_CANONICAL (t) = template_type;
9998 else if (any_template_arguments_need_structural_equality_p (arglist))
9999 /* Some of the template arguments require structural
10000 equality testing, so this template class requires
10001 structural equality testing. */
10002 SET_TYPE_STRUCTURAL_EQUALITY (t);
10003 }
10004 else
10005 gcc_unreachable ();
10006
10007 /* If we called start_enum or pushtag above, this information
10008 will already be set up. */
10009 type_decl = TYPE_NAME (t);
10010 if (!type_decl)
10011 {
10012 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10013
10014 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10015 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10016 DECL_SOURCE_LOCATION (type_decl)
10017 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10018 }
10019
10020 set_instantiating_module (type_decl);
10021 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10022 of export flag. We want to propagate this because it might
10023 be a friend declaration that pushes a new hidden binding. */
10024 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10025
10026 if (CLASS_TYPE_P (template_type))
10027 {
10028 TREE_PRIVATE (type_decl)
10029 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10030 TREE_PROTECTED (type_decl)
10031 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10032 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10033 {
10034 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10035 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10036 }
10037 }
10038
10039 if (OVERLOAD_TYPE_P (t)
10040 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10041 {
10042 static const char *tags[] = {"abi_tag", "may_alias"};
10043
10044 for (unsigned ix = 0; ix != 2; ix++)
10045 {
10046 tree attributes
10047 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10048
10049 if (attributes)
10050 TYPE_ATTRIBUTES (t)
10051 = tree_cons (TREE_PURPOSE (attributes),
10052 TREE_VALUE (attributes),
10053 TYPE_ATTRIBUTES (t));
10054 }
10055 }
10056
10057 /* Let's consider the explicit specialization of a member
10058 of a class template specialization that is implicitly instantiated,
10059 e.g.:
10060 template<class T>
10061 struct S
10062 {
10063 template<class U> struct M {}; //#0
10064 };
10065
10066 template<>
10067 template<>
10068 struct S<int>::M<char> //#1
10069 {
10070 int i;
10071 };
10072 [temp.expl.spec]/4 says this is valid.
10073
10074 In this case, when we write:
10075 S<int>::M<char> m;
10076
10077 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10078 the one of #0.
10079
10080 When we encounter #1, we want to store the partial instantiation
10081 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10082
10083 For all cases other than this "explicit specialization of member of a
10084 class template", we just want to store the most general template into
10085 the CLASSTYPE_TI_TEMPLATE of M.
10086
10087 This case of "explicit specialization of member of a class template"
10088 only happens when:
10089 1/ the enclosing class is an instantiation of, and therefore not
10090 the same as, the context of the most general template, and
10091 2/ we aren't looking at the partial instantiation itself, i.e.
10092 the innermost arguments are not the same as the innermost parms of
10093 the most general template.
10094
10095 So it's only when 1/ and 2/ happens that we want to use the partial
10096 instantiation of the member template in lieu of its most general
10097 template. */
10098
10099 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10100 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10101 /* the enclosing class must be an instantiation... */
10102 && CLASS_TYPE_P (context)
10103 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10104 {
10105 TREE_VEC_LENGTH (arglist)--;
10106 ++processing_template_decl;
10107 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10108 tree partial_inst_args =
10109 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10110 arglist, complain, NULL_TREE);
10111 --processing_template_decl;
10112 TREE_VEC_LENGTH (arglist)++;
10113 if (partial_inst_args == error_mark_node)
10114 return error_mark_node;
10115 use_partial_inst_tmpl =
10116 /*...and we must not be looking at the partial instantiation
10117 itself. */
10118 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10119 partial_inst_args);
10120 }
10121
10122 if (!use_partial_inst_tmpl)
10123 /* This case is easy; there are no member templates involved. */
10124 found = gen_tmpl;
10125 else
10126 {
10127 /* This is a full instantiation of a member template. Find
10128 the partial instantiation of which this is an instance. */
10129
10130 /* Temporarily reduce by one the number of levels in the ARGLIST
10131 so as to avoid comparing the last set of arguments. */
10132 TREE_VEC_LENGTH (arglist)--;
10133 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10134 TREE_VEC_LENGTH (arglist)++;
10135 /* FOUND is either a proper class type, or an alias
10136 template specialization. In the later case, it's a
10137 TYPE_DECL, resulting from the substituting of arguments
10138 for parameters in the TYPE_DECL of the alias template
10139 done earlier. So be careful while getting the template
10140 of FOUND. */
10141 found = (TREE_CODE (found) == TEMPLATE_DECL
10142 ? found
10143 : (TREE_CODE (found) == TYPE_DECL
10144 ? DECL_TI_TEMPLATE (found)
10145 : CLASSTYPE_TI_TEMPLATE (found)));
10146
10147 if (DECL_CLASS_TEMPLATE_P (found)
10148 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10149 {
10150 /* If this partial instantiation is specialized, we want to
10151 use it for hash table lookup. */
10152 elt.tmpl = found;
10153 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10154 hash = spec_hasher::hash (&elt);
10155 }
10156 }
10157
10158 /* Build template info for the new specialization. */
10159 if (TYPE_ALIAS_P (t))
10160 {
10161 /* This is constructed during instantiation of the alias
10162 decl. But for member templates of template classes, that
10163 is not correct as we need to refer to the partially
10164 instantiated template, not the most general template.
10165 The incorrect knowledge will not have escaped this
10166 instantiation process, so we're good just updating the
10167 template_info we made then. */
10168 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10169 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10170 if (TI_TEMPLATE (ti) != found)
10171 {
10172 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10173 TI_TEMPLATE (ti) = found;
10174 }
10175 }
10176 else
10177 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10178
10179 elt.spec = t;
10180 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10181 gcc_checking_assert (*slot == NULL);
10182 entry = ggc_alloc<spec_entry> ();
10183 *entry = elt;
10184 *slot = entry;
10185
10186 /* Note this use of the partial instantiation so we can check it
10187 later in maybe_process_partial_specialization. */
10188 DECL_TEMPLATE_INSTANTIATIONS (found)
10189 = tree_cons (arglist, t,
10190 DECL_TEMPLATE_INSTANTIATIONS (found));
10191
10192 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10193 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10194 /* Now that the type has been registered on the instantiations
10195 list, we set up the enumerators. Because the enumeration
10196 constants may involve the enumeration type itself, we make
10197 sure to register the type first, and then create the
10198 constants. That way, doing tsubst_expr for the enumeration
10199 constants won't result in recursive calls here; we'll find
10200 the instantiation and exit above. */
10201 tsubst_enum (template_type, t, arglist);
10202
10203 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10204 /* If the type makes use of template parameters, the
10205 code that generates debugging information will crash. */
10206 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10207
10208 /* Possibly limit visibility based on template args. */
10209 TREE_PUBLIC (type_decl) = 1;
10210 determine_visibility (type_decl);
10211
10212 inherit_targ_abi_tags (t);
10213
10214 return t;
10215 }
10216 }
10217
10218 /* Wrapper for lookup_template_class_1. */
10219
10220 tree
10221 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10222 int entering_scope, tsubst_flags_t complain)
10223 {
10224 tree ret;
10225 timevar_push (TV_TEMPLATE_INST);
10226 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10227 entering_scope, complain);
10228 timevar_pop (TV_TEMPLATE_INST);
10229 return ret;
10230 }
10231
10232 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10233
10234 tree
10235 lookup_template_variable (tree templ, tree arglist)
10236 {
10237 if (flag_concepts && variable_concept_p (templ))
10238 return build_concept_check (templ, arglist, tf_none);
10239
10240 /* The type of the expression is NULL_TREE since the template-id could refer
10241 to an explicit or partial specialization. */
10242 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10243 }
10244
10245 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10246
10247 tree
10248 finish_template_variable (tree var, tsubst_flags_t complain)
10249 {
10250 tree templ = TREE_OPERAND (var, 0);
10251 tree arglist = TREE_OPERAND (var, 1);
10252
10253 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10254 arglist = add_outermost_template_args (tmpl_args, arglist);
10255
10256 templ = most_general_template (templ);
10257 tree parms = DECL_TEMPLATE_PARMS (templ);
10258 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10259 /*req_all*/true,
10260 /*use_default*/true);
10261 if (arglist == error_mark_node)
10262 return error_mark_node;
10263
10264 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10265 {
10266 if (complain & tf_error)
10267 {
10268 auto_diagnostic_group d;
10269 error ("use of invalid variable template %qE", var);
10270 diagnose_constraints (location_of (var), templ, arglist);
10271 }
10272 return error_mark_node;
10273 }
10274
10275 return instantiate_template (templ, arglist, complain);
10276 }
10277
10278 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10279 TARGS template args, and instantiate it if it's not dependent. */
10280
10281 tree
10282 lookup_and_finish_template_variable (tree templ, tree targs,
10283 tsubst_flags_t complain)
10284 {
10285 templ = lookup_template_variable (templ, targs);
10286 if (!any_dependent_template_arguments_p (targs))
10287 {
10288 templ = finish_template_variable (templ, complain);
10289 mark_used (templ);
10290 }
10291
10292 return convert_from_reference (templ);
10293 }
10294
10295 /* If the set of template parameters PARMS contains a template parameter
10296 at the given LEVEL and INDEX, then return this parameter. Otherwise
10297 return NULL_TREE. */
10298
10299 static tree
10300 corresponding_template_parameter (tree parms, int level, int index)
10301 {
10302 while (TMPL_PARMS_DEPTH (parms) > level)
10303 parms = TREE_CHAIN (parms);
10304
10305 if (TMPL_PARMS_DEPTH (parms) != level
10306 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10307 return NULL_TREE;
10308
10309 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10310 /* As in template_parm_to_arg. */
10311 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10312 t = TREE_TYPE (t);
10313 else
10314 t = DECL_INITIAL (t);
10315
10316 gcc_assert (TEMPLATE_PARM_P (t));
10317 return t;
10318 }
10319
10320 /* Return the template parameter from PARMS that positionally corresponds
10321 to the template parameter PARM, or else return NULL_TREE. */
10322
10323 static tree
10324 corresponding_template_parameter (tree parms, tree parm)
10325 {
10326 int level, index;
10327 template_parm_level_and_index (parm, &level, &index);
10328 return corresponding_template_parameter (parms, level, index);
10329 }
10330
10331 \f
10332 struct pair_fn_data
10333 {
10334 tree_fn_t fn;
10335 tree_fn_t any_fn;
10336 void *data;
10337 /* True when we should also visit template parameters that occur in
10338 non-deduced contexts. */
10339 bool include_nondeduced_p;
10340 hash_set<tree> *visited;
10341 };
10342
10343 /* Called from for_each_template_parm via walk_tree. */
10344
10345 static tree
10346 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10347 {
10348 tree t = *tp;
10349 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10350 tree_fn_t fn = pfd->fn;
10351 void *data = pfd->data;
10352 tree result = NULL_TREE;
10353
10354 #define WALK_SUBTREE(NODE) \
10355 do \
10356 { \
10357 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10358 pfd->include_nondeduced_p, \
10359 pfd->any_fn); \
10360 if (result) goto out; \
10361 } \
10362 while (0)
10363
10364 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10365 return t;
10366
10367 if (TYPE_P (t)
10368 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10369 WALK_SUBTREE (TYPE_CONTEXT (t));
10370
10371 switch (TREE_CODE (t))
10372 {
10373 case RECORD_TYPE:
10374 if (TYPE_PTRMEMFUNC_P (t))
10375 break;
10376 /* Fall through. */
10377
10378 case UNION_TYPE:
10379 case ENUMERAL_TYPE:
10380 if (!TYPE_TEMPLATE_INFO (t))
10381 *walk_subtrees = 0;
10382 else
10383 WALK_SUBTREE (TYPE_TI_ARGS (t));
10384 break;
10385
10386 case INTEGER_TYPE:
10387 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10388 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10389 break;
10390
10391 case METHOD_TYPE:
10392 /* Since we're not going to walk subtrees, we have to do this
10393 explicitly here. */
10394 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10395 /* Fall through. */
10396
10397 case FUNCTION_TYPE:
10398 /* Check the return type. */
10399 WALK_SUBTREE (TREE_TYPE (t));
10400
10401 /* Check the parameter types. Since default arguments are not
10402 instantiated until they are needed, the TYPE_ARG_TYPES may
10403 contain expressions that involve template parameters. But,
10404 no-one should be looking at them yet. And, once they're
10405 instantiated, they don't contain template parameters, so
10406 there's no point in looking at them then, either. */
10407 {
10408 tree parm;
10409
10410 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10411 WALK_SUBTREE (TREE_VALUE (parm));
10412
10413 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10414 want walk_tree walking into them itself. */
10415 *walk_subtrees = 0;
10416 }
10417
10418 if (flag_noexcept_type)
10419 {
10420 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10421 if (spec)
10422 WALK_SUBTREE (TREE_PURPOSE (spec));
10423 }
10424 break;
10425
10426 case TYPEOF_TYPE:
10427 case DECLTYPE_TYPE:
10428 case UNDERLYING_TYPE:
10429 if (pfd->include_nondeduced_p
10430 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10431 pfd->visited,
10432 pfd->include_nondeduced_p,
10433 pfd->any_fn))
10434 return error_mark_node;
10435 *walk_subtrees = false;
10436 break;
10437
10438 case FUNCTION_DECL:
10439 case VAR_DECL:
10440 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10441 WALK_SUBTREE (DECL_TI_ARGS (t));
10442 /* Fall through. */
10443
10444 case PARM_DECL:
10445 case CONST_DECL:
10446 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10447 WALK_SUBTREE (DECL_INITIAL (t));
10448 if (DECL_CONTEXT (t)
10449 && pfd->include_nondeduced_p)
10450 WALK_SUBTREE (DECL_CONTEXT (t));
10451 break;
10452
10453 case BOUND_TEMPLATE_TEMPLATE_PARM:
10454 /* Record template parameters such as `T' inside `TT<T>'. */
10455 WALK_SUBTREE (TYPE_TI_ARGS (t));
10456 /* Fall through. */
10457
10458 case TEMPLATE_TEMPLATE_PARM:
10459 case TEMPLATE_TYPE_PARM:
10460 case TEMPLATE_PARM_INDEX:
10461 if (fn && (*fn)(t, data))
10462 return t;
10463 else if (!fn)
10464 return t;
10465 break;
10466
10467 case TEMPLATE_DECL:
10468 /* A template template parameter is encountered. */
10469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10470 WALK_SUBTREE (TREE_TYPE (t));
10471
10472 /* Already substituted template template parameter */
10473 *walk_subtrees = 0;
10474 break;
10475
10476 case TYPENAME_TYPE:
10477 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10478 partial instantiation. */
10479 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10480 *walk_subtrees = 0;
10481 break;
10482
10483 case CONSTRUCTOR:
10484 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10485 && pfd->include_nondeduced_p)
10486 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10487 break;
10488
10489 case INDIRECT_REF:
10490 case COMPONENT_REF:
10491 /* If there's no type, then this thing must be some expression
10492 involving template parameters. */
10493 if (!fn && !TREE_TYPE (t))
10494 return error_mark_node;
10495 break;
10496
10497 case MODOP_EXPR:
10498 case CAST_EXPR:
10499 case IMPLICIT_CONV_EXPR:
10500 case REINTERPRET_CAST_EXPR:
10501 case CONST_CAST_EXPR:
10502 case STATIC_CAST_EXPR:
10503 case DYNAMIC_CAST_EXPR:
10504 case ARROW_EXPR:
10505 case DOTSTAR_EXPR:
10506 case TYPEID_EXPR:
10507 case PSEUDO_DTOR_EXPR:
10508 if (!fn)
10509 return error_mark_node;
10510 break;
10511
10512 case SCOPE_REF:
10513 if (pfd->include_nondeduced_p)
10514 WALK_SUBTREE (TREE_OPERAND (t, 0));
10515 break;
10516
10517 case REQUIRES_EXPR:
10518 {
10519 if (!fn)
10520 return error_mark_node;
10521
10522 /* Recursively walk the type of each constraint variable. */
10523 tree p = TREE_OPERAND (t, 0);
10524 while (p)
10525 {
10526 WALK_SUBTREE (TREE_TYPE (p));
10527 p = TREE_CHAIN (p);
10528 }
10529 }
10530 break;
10531
10532 default:
10533 break;
10534 }
10535
10536 #undef WALK_SUBTREE
10537
10538 /* We didn't find any template parameters we liked. */
10539 out:
10540 return result;
10541 }
10542
10543 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10544 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10545 call FN with the parameter and the DATA.
10546 If FN returns nonzero, the iteration is terminated, and
10547 for_each_template_parm returns 1. Otherwise, the iteration
10548 continues. If FN never returns a nonzero value, the value
10549 returned by for_each_template_parm is 0. If FN is NULL, it is
10550 considered to be the function which always returns 1.
10551
10552 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10553 parameters that occur in non-deduced contexts. When false, only
10554 visits those template parameters that can be deduced. */
10555
10556 static tree
10557 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10558 hash_set<tree> *visited,
10559 bool include_nondeduced_p,
10560 tree_fn_t any_fn)
10561 {
10562 struct pair_fn_data pfd;
10563 tree result;
10564
10565 /* Set up. */
10566 pfd.fn = fn;
10567 pfd.any_fn = any_fn;
10568 pfd.data = data;
10569 pfd.include_nondeduced_p = include_nondeduced_p;
10570
10571 /* Walk the tree. (Conceptually, we would like to walk without
10572 duplicates, but for_each_template_parm_r recursively calls
10573 for_each_template_parm, so we would need to reorganize a fair
10574 bit to use walk_tree_without_duplicates, so we keep our own
10575 visited list.) */
10576 if (visited)
10577 pfd.visited = visited;
10578 else
10579 pfd.visited = new hash_set<tree>;
10580 result = cp_walk_tree (&t,
10581 for_each_template_parm_r,
10582 &pfd,
10583 pfd.visited);
10584
10585 /* Clean up. */
10586 if (!visited)
10587 {
10588 delete pfd.visited;
10589 pfd.visited = 0;
10590 }
10591
10592 return result;
10593 }
10594
10595 struct find_template_parameter_info
10596 {
10597 explicit find_template_parameter_info (tree ctx_parms)
10598 : parm_list (NULL_TREE),
10599 ctx_parms (ctx_parms),
10600 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10601 {}
10602
10603 hash_set<tree> visited;
10604 hash_set<tree> parms;
10605 tree parm_list;
10606 tree ctx_parms;
10607 int max_depth;
10608 };
10609
10610 /* Appends the declaration of T to the list in DATA. */
10611
10612 static int
10613 keep_template_parm (tree t, void* data)
10614 {
10615 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10616
10617 /* Template parameters declared within the expression are not part of
10618 the parameter mapping. For example, in this concept:
10619
10620 template<typename T>
10621 concept C = requires { <expr> } -> same_as<int>;
10622
10623 the return specifier same_as<int> declares a new decltype parameter
10624 that must not be part of the parameter mapping. The same is true
10625 for generic lambda parameters, lambda template parameters, etc. */
10626 int level;
10627 int index;
10628 template_parm_level_and_index (t, &level, &index);
10629 if (level > ftpi->max_depth)
10630 return 0;
10631
10632 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10633 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10634 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10635 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10636
10637 /* This template parameter might be an argument to a cached dependent
10638 specalization that was formed earlier inside some other template, in
10639 which case the parameter is not among the ones that are in-scope.
10640 Look in CTX_PARMS to find the corresponding in-scope template
10641 parameter, and use it instead. */
10642 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10643 t = in_scope;
10644
10645 /* Arguments like const T yield parameters like const T. This means that
10646 a template-id like X<T, const T> would yield two distinct parameters:
10647 T and const T. Adjust types to their unqualified versions. */
10648 if (TYPE_P (t))
10649 t = TYPE_MAIN_VARIANT (t);
10650 if (!ftpi->parms.add (t))
10651 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10652
10653 /* Verify the parameter we found has a valid index. */
10654 if (flag_checking)
10655 {
10656 tree parms = ftpi->ctx_parms;
10657 while (TMPL_PARMS_DEPTH (parms) > level)
10658 parms = TREE_CHAIN (parms);
10659 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10660 gcc_assert (index < len);
10661 }
10662
10663 return 0;
10664 }
10665
10666 /* Ensure that we recursively examine certain terms that are not normally
10667 visited in for_each_template_parm_r. */
10668
10669 static int
10670 any_template_parm_r (tree t, void *data)
10671 {
10672 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10673
10674 #define WALK_SUBTREE(NODE) \
10675 do \
10676 { \
10677 for_each_template_parm (NODE, keep_template_parm, data, \
10678 &ftpi->visited, true, \
10679 any_template_parm_r); \
10680 } \
10681 while (0)
10682
10683 /* A mention of a member alias/typedef is a use of all of its template
10684 arguments, including those from the enclosing class, so we don't use
10685 alias_template_specialization_p here. */
10686 if (TYPE_P (t) && typedef_variant_p (t))
10687 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10688 WALK_SUBTREE (TI_ARGS (tinfo));
10689
10690 switch (TREE_CODE (t))
10691 {
10692 case TEMPLATE_TYPE_PARM:
10693 /* Type constraints of a placeholder type may contain parameters. */
10694 if (is_auto (t))
10695 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10696 WALK_SUBTREE (constr);
10697 break;
10698
10699 case TEMPLATE_ID_EXPR:
10700 /* Search through references to variable templates. */
10701 WALK_SUBTREE (TREE_OPERAND (t, 0));
10702 WALK_SUBTREE (TREE_OPERAND (t, 1));
10703 break;
10704
10705 case TEMPLATE_PARM_INDEX:
10706 case PARM_DECL:
10707 /* A parameter or constraint variable may also depend on a template
10708 parameter without explicitly naming it. */
10709 WALK_SUBTREE (TREE_TYPE (t));
10710 break;
10711
10712 case TEMPLATE_DECL:
10713 {
10714 /* If T is a member template that shares template parameters with
10715 ctx_parms, we need to mark all those parameters for mapping. */
10716 tree dparms = DECL_TEMPLATE_PARMS (t);
10717 tree cparms = ftpi->ctx_parms;
10718 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10719 dparms = TREE_CHAIN (dparms);
10720 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10721 cparms = TREE_CHAIN (cparms);
10722 while (dparms
10723 && (TREE_TYPE (TREE_VALUE (dparms))
10724 != TREE_TYPE (TREE_VALUE (cparms))))
10725 dparms = TREE_CHAIN (dparms),
10726 cparms = TREE_CHAIN (cparms);
10727 if (dparms)
10728 {
10729 int ddepth = TMPL_PARMS_DEPTH (dparms);
10730 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10731 for (int i = 0; i < ddepth; ++i)
10732 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10733 }
10734 }
10735 break;
10736
10737 case LAMBDA_EXPR:
10738 {
10739 /* Look in the parms and body. */
10740 tree fn = lambda_function (t);
10741 WALK_SUBTREE (TREE_TYPE (fn));
10742 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10743 }
10744 break;
10745
10746 case IDENTIFIER_NODE:
10747 if (IDENTIFIER_CONV_OP_P (t))
10748 /* The conversion-type-id of a conversion operator may be dependent. */
10749 WALK_SUBTREE (TREE_TYPE (t));
10750 break;
10751
10752 default:
10753 break;
10754 }
10755
10756 /* Keep walking. */
10757 return 0;
10758 }
10759
10760 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10761 are the template parameters in scope. */
10762
10763 tree
10764 find_template_parameters (tree t, tree ctx_parms)
10765 {
10766 if (!ctx_parms)
10767 return NULL_TREE;
10768
10769 find_template_parameter_info ftpi (ctx_parms);
10770 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10771 /*include_nondeduced*/true, any_template_parm_r);
10772 return ftpi.parm_list;
10773 }
10774
10775 /* Returns true if T depends on any template parameter. */
10776
10777 int
10778 uses_template_parms (tree t)
10779 {
10780 if (t == NULL_TREE)
10781 return false;
10782
10783 bool dependent_p;
10784 int saved_processing_template_decl;
10785
10786 saved_processing_template_decl = processing_template_decl;
10787 if (!saved_processing_template_decl)
10788 processing_template_decl = 1;
10789 if (TYPE_P (t))
10790 dependent_p = dependent_type_p (t);
10791 else if (TREE_CODE (t) == TREE_VEC)
10792 dependent_p = any_dependent_template_arguments_p (t);
10793 else if (TREE_CODE (t) == TREE_LIST)
10794 dependent_p = (uses_template_parms (TREE_VALUE (t))
10795 || uses_template_parms (TREE_CHAIN (t)));
10796 else if (TREE_CODE (t) == TYPE_DECL)
10797 dependent_p = dependent_type_p (TREE_TYPE (t));
10798 else if (t == error_mark_node)
10799 dependent_p = false;
10800 else
10801 dependent_p = instantiation_dependent_expression_p (t);
10802
10803 processing_template_decl = saved_processing_template_decl;
10804
10805 return dependent_p;
10806 }
10807
10808 /* Returns true iff we're processing an incompletely instantiated function
10809 template. Useful instead of processing_template_decl because the latter
10810 is set to 0 during instantiate_non_dependent_expr. */
10811
10812 bool
10813 in_template_function (void)
10814 {
10815 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10816 the latter might get set for e.g. access checking during satisfaction. */
10817 tree fn = cfun ? cfun->decl : NULL_TREE;
10818 bool ret;
10819 ++processing_template_decl;
10820 ret = (fn && DECL_LANG_SPECIFIC (fn)
10821 && DECL_TEMPLATE_INFO (fn)
10822 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10823 --processing_template_decl;
10824 return ret;
10825 }
10826
10827 /* Returns true if T depends on any template parameter with level LEVEL. */
10828
10829 bool
10830 uses_template_parms_level (tree t, int level)
10831 {
10832 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10833 /*include_nondeduced_p=*/true);
10834 }
10835
10836 /* Returns true if the signature of DECL depends on any template parameter from
10837 its enclosing class. */
10838
10839 bool
10840 uses_outer_template_parms (tree decl)
10841 {
10842 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10843 if (depth == 0)
10844 return false;
10845 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10846 &depth, NULL, /*include_nondeduced_p=*/true))
10847 return true;
10848 if (PRIMARY_TEMPLATE_P (decl)
10849 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10850 (DECL_TEMPLATE_PARMS (decl)),
10851 template_parm_outer_level,
10852 &depth, NULL, /*include_nondeduced_p=*/true))
10853 return true;
10854 tree ci = get_constraints (decl);
10855 if (ci)
10856 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10857 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10858 &depth, NULL, /*nondeduced*/true))
10859 return true;
10860 return false;
10861 }
10862
10863 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10864 ill-formed translation unit, i.e. a variable or function that isn't
10865 usable in a constant expression. */
10866
10867 static inline bool
10868 neglectable_inst_p (tree d)
10869 {
10870 return (d && DECL_P (d)
10871 && !undeduced_auto_decl (d)
10872 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10873 : decl_maybe_constant_var_p (d)));
10874 }
10875
10876 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10877 neglectable and instantiated from within an erroneous instantiation. */
10878
10879 static bool
10880 limit_bad_template_recursion (tree decl)
10881 {
10882 struct tinst_level *lev = current_tinst_level;
10883 int errs = errorcount + sorrycount;
10884 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10885 return false;
10886
10887 for (; lev; lev = lev->next)
10888 if (neglectable_inst_p (lev->maybe_get_node ()))
10889 break;
10890
10891 return (lev && errs > lev->errors);
10892 }
10893
10894 static int tinst_depth;
10895 extern int max_tinst_depth;
10896 int depth_reached;
10897
10898 static GTY(()) struct tinst_level *last_error_tinst_level;
10899
10900 /* We're starting to instantiate D; record the template instantiation context
10901 at LOC for diagnostics and to restore it later. */
10902
10903 bool
10904 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10905 {
10906 struct tinst_level *new_level;
10907
10908 if (tinst_depth >= max_tinst_depth)
10909 {
10910 /* Tell error.c not to try to instantiate any templates. */
10911 at_eof = 2;
10912 fatal_error (input_location,
10913 "template instantiation depth exceeds maximum of %d"
10914 " (use %<-ftemplate-depth=%> to increase the maximum)",
10915 max_tinst_depth);
10916 return false;
10917 }
10918
10919 /* If the current instantiation caused problems, don't let it instantiate
10920 anything else. Do allow deduction substitution and decls usable in
10921 constant expressions. */
10922 if (!targs && limit_bad_template_recursion (tldcl))
10923 {
10924 /* Avoid no_linkage_errors and unused function warnings for this
10925 decl. */
10926 TREE_NO_WARNING (tldcl) = 1;
10927 return false;
10928 }
10929
10930 /* When not -quiet, dump template instantiations other than functions, since
10931 announce_function will take care of those. */
10932 if (!quiet_flag && !targs
10933 && TREE_CODE (tldcl) != TREE_LIST
10934 && TREE_CODE (tldcl) != FUNCTION_DECL)
10935 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10936
10937 new_level = tinst_level_freelist ().alloc ();
10938 new_level->tldcl = tldcl;
10939 new_level->targs = targs;
10940 new_level->locus = loc;
10941 new_level->errors = errorcount + sorrycount;
10942 new_level->next = NULL;
10943 new_level->refcount = 0;
10944 new_level->path = new_level->visible = nullptr;
10945 set_refcount_ptr (new_level->next, current_tinst_level);
10946 set_refcount_ptr (current_tinst_level, new_level);
10947
10948 ++tinst_depth;
10949 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10950 depth_reached = tinst_depth;
10951
10952 return true;
10953 }
10954
10955 /* We're starting substitution of TMPL<ARGS>; record the template
10956 substitution context for diagnostics and to restore it later. */
10957
10958 bool
10959 push_tinst_level (tree tmpl, tree args)
10960 {
10961 return push_tinst_level_loc (tmpl, args, input_location);
10962 }
10963
10964 /* We're starting to instantiate D; record INPUT_LOCATION and the
10965 template instantiation context for diagnostics and to restore it
10966 later. */
10967
10968 bool
10969 push_tinst_level (tree d)
10970 {
10971 return push_tinst_level_loc (d, input_location);
10972 }
10973
10974 /* Likewise, but record LOC as the program location. */
10975
10976 bool
10977 push_tinst_level_loc (tree d, location_t loc)
10978 {
10979 gcc_assert (TREE_CODE (d) != TREE_LIST);
10980 return push_tinst_level_loc (d, NULL, loc);
10981 }
10982
10983 /* We're done instantiating this template; return to the instantiation
10984 context. */
10985
10986 void
10987 pop_tinst_level (void)
10988 {
10989 /* Restore the filename and line number stashed away when we started
10990 this instantiation. */
10991 input_location = current_tinst_level->locus;
10992 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10993 --tinst_depth;
10994 }
10995
10996 /* We're instantiating a deferred template; restore the template
10997 instantiation context in which the instantiation was requested, which
10998 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10999
11000 static tree
11001 reopen_tinst_level (struct tinst_level *level)
11002 {
11003 struct tinst_level *t;
11004
11005 tinst_depth = 0;
11006 for (t = level; t; t = t->next)
11007 ++tinst_depth;
11008
11009 set_refcount_ptr (current_tinst_level, level);
11010 pop_tinst_level ();
11011 if (current_tinst_level)
11012 current_tinst_level->errors = errorcount+sorrycount;
11013 return level->maybe_get_node ();
11014 }
11015
11016 /* Returns the TINST_LEVEL which gives the original instantiation
11017 context. */
11018
11019 struct tinst_level *
11020 outermost_tinst_level (void)
11021 {
11022 struct tinst_level *level = current_tinst_level;
11023 if (level)
11024 while (level->next)
11025 level = level->next;
11026 return level;
11027 }
11028
11029 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11030 vector of template arguments, as for tsubst.
11031
11032 Returns an appropriate tsubst'd friend declaration. */
11033
11034 static tree
11035 tsubst_friend_function (tree decl, tree args)
11036 {
11037 tree new_friend;
11038
11039 if (TREE_CODE (decl) == FUNCTION_DECL
11040 && DECL_TEMPLATE_INSTANTIATION (decl)
11041 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11042 /* This was a friend declared with an explicit template
11043 argument list, e.g.:
11044
11045 friend void f<>(T);
11046
11047 to indicate that f was a template instantiation, not a new
11048 function declaration. Now, we have to figure out what
11049 instantiation of what template. */
11050 {
11051 tree template_id, arglist, fns;
11052 tree new_args;
11053 tree tmpl;
11054 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11055
11056 /* Friend functions are looked up in the containing namespace scope.
11057 We must enter that scope, to avoid finding member functions of the
11058 current class with same name. */
11059 push_nested_namespace (ns);
11060 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11061 tf_warning_or_error, NULL_TREE,
11062 /*integral_constant_expression_p=*/false);
11063 pop_nested_namespace (ns);
11064 arglist = tsubst (DECL_TI_ARGS (decl), args,
11065 tf_warning_or_error, NULL_TREE);
11066 template_id = lookup_template_function (fns, arglist);
11067
11068 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11069 tmpl = determine_specialization (template_id, new_friend,
11070 &new_args,
11071 /*need_member_template=*/0,
11072 TREE_VEC_LENGTH (args),
11073 tsk_none);
11074 return instantiate_template (tmpl, new_args, tf_error);
11075 }
11076
11077 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11078 if (new_friend == error_mark_node)
11079 return error_mark_node;
11080
11081 /* The NEW_FRIEND will look like an instantiation, to the
11082 compiler, but is not an instantiation from the point of view of
11083 the language. For example, we might have had:
11084
11085 template <class T> struct S {
11086 template <class U> friend void f(T, U);
11087 };
11088
11089 Then, in S<int>, template <class U> void f(int, U) is not an
11090 instantiation of anything. */
11091
11092 DECL_USE_TEMPLATE (new_friend) = 0;
11093 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11094 {
11095 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11096 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11097 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11098 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11099
11100 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11101 match in decls_match. */
11102 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11103 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11104 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11105 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11106 }
11107
11108 /* The mangled name for the NEW_FRIEND is incorrect. The function
11109 is not a template instantiation and should not be mangled like
11110 one. Therefore, we forget the mangling here; we'll recompute it
11111 later if we need it. */
11112 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11113 {
11114 SET_DECL_RTL (new_friend, NULL);
11115 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11116 }
11117
11118 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11119 {
11120 tree old_decl;
11121 tree ns;
11122
11123 /* We must save some information from NEW_FRIEND before calling
11124 duplicate decls since that function will free NEW_FRIEND if
11125 possible. */
11126 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11127 tree new_friend_result_template_info = NULL_TREE;
11128 bool new_friend_is_defn =
11129 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11130 (template_for_substitution (new_friend)))
11131 != NULL_TREE);
11132 tree not_tmpl = new_friend;
11133
11134 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11135 {
11136 /* This declaration is a `primary' template. */
11137 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11138
11139 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11140 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11141 }
11142
11143 /* Inside pushdecl_namespace_level, we will push into the
11144 current namespace. However, the friend function should go
11145 into the namespace of the template. */
11146 ns = decl_namespace_context (new_friend);
11147 push_nested_namespace (ns);
11148 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11149 pop_nested_namespace (ns);
11150
11151 if (old_decl == error_mark_node)
11152 return error_mark_node;
11153
11154 if (old_decl != new_friend)
11155 {
11156 /* This new friend declaration matched an existing
11157 declaration. For example, given:
11158
11159 template <class T> void f(T);
11160 template <class U> class C {
11161 template <class T> friend void f(T) {}
11162 };
11163
11164 the friend declaration actually provides the definition
11165 of `f', once C has been instantiated for some type. So,
11166 old_decl will be the out-of-class template declaration,
11167 while new_friend is the in-class definition.
11168
11169 But, if `f' was called before this point, the
11170 instantiation of `f' will have DECL_TI_ARGS corresponding
11171 to `T' but not to `U', references to which might appear
11172 in the definition of `f'. Previously, the most general
11173 template for an instantiation of `f' was the out-of-class
11174 version; now it is the in-class version. Therefore, we
11175 run through all specialization of `f', adding to their
11176 DECL_TI_ARGS appropriately. In particular, they need a
11177 new set of outer arguments, corresponding to the
11178 arguments for this class instantiation.
11179
11180 The same situation can arise with something like this:
11181
11182 friend void f(int);
11183 template <class T> class C {
11184 friend void f(T) {}
11185 };
11186
11187 when `C<int>' is instantiated. Now, `f(int)' is defined
11188 in the class. */
11189
11190 if (!new_friend_is_defn)
11191 /* On the other hand, if the in-class declaration does
11192 *not* provide a definition, then we don't want to alter
11193 existing definitions. We can just leave everything
11194 alone. */
11195 ;
11196 else
11197 {
11198 tree new_template = TI_TEMPLATE (new_friend_template_info);
11199 tree new_args = TI_ARGS (new_friend_template_info);
11200
11201 /* Overwrite whatever template info was there before, if
11202 any, with the new template information pertaining to
11203 the declaration. */
11204 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11205
11206 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11207 {
11208 /* We should have called reregister_specialization in
11209 duplicate_decls. */
11210 gcc_assert (retrieve_specialization (new_template,
11211 new_args, 0)
11212 == old_decl);
11213
11214 /* Instantiate it if the global has already been used. */
11215 if (DECL_ODR_USED (old_decl))
11216 instantiate_decl (old_decl, /*defer_ok=*/true,
11217 /*expl_inst_class_mem_p=*/false);
11218 }
11219 else
11220 {
11221 tree t;
11222
11223 /* Indicate that the old function template is a partial
11224 instantiation. */
11225 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11226 = new_friend_result_template_info;
11227
11228 gcc_assert (new_template
11229 == most_general_template (new_template));
11230 gcc_assert (new_template != old_decl);
11231
11232 /* Reassign any specializations already in the hash table
11233 to the new more general template, and add the
11234 additional template args. */
11235 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11236 t != NULL_TREE;
11237 t = TREE_CHAIN (t))
11238 {
11239 tree spec = TREE_VALUE (t);
11240 spec_entry elt;
11241
11242 elt.tmpl = old_decl;
11243 elt.args = DECL_TI_ARGS (spec);
11244 elt.spec = NULL_TREE;
11245
11246 decl_specializations->remove_elt (&elt);
11247
11248 DECL_TI_ARGS (spec)
11249 = add_outermost_template_args (new_args,
11250 DECL_TI_ARGS (spec));
11251
11252 register_specialization
11253 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11254
11255 }
11256 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11257 }
11258 }
11259
11260 /* The information from NEW_FRIEND has been merged into OLD_DECL
11261 by duplicate_decls. */
11262 new_friend = old_decl;
11263 }
11264 }
11265 else
11266 {
11267 tree context = DECL_CONTEXT (new_friend);
11268 bool dependent_p;
11269
11270 /* In the code
11271 template <class T> class C {
11272 template <class U> friend void C1<U>::f (); // case 1
11273 friend void C2<T>::f (); // case 2
11274 };
11275 we only need to make sure CONTEXT is a complete type for
11276 case 2. To distinguish between the two cases, we note that
11277 CONTEXT of case 1 remains dependent type after tsubst while
11278 this isn't true for case 2. */
11279 ++processing_template_decl;
11280 dependent_p = dependent_type_p (context);
11281 --processing_template_decl;
11282
11283 if (!dependent_p
11284 && !complete_type_or_else (context, NULL_TREE))
11285 return error_mark_node;
11286
11287 if (COMPLETE_TYPE_P (context))
11288 {
11289 tree fn = new_friend;
11290 /* do_friend adds the TEMPLATE_DECL for any member friend
11291 template even if it isn't a member template, i.e.
11292 template <class T> friend A<T>::f();
11293 Look through it in that case. */
11294 if (TREE_CODE (fn) == TEMPLATE_DECL
11295 && !PRIMARY_TEMPLATE_P (fn))
11296 fn = DECL_TEMPLATE_RESULT (fn);
11297 /* Check to see that the declaration is really present, and,
11298 possibly obtain an improved declaration. */
11299 fn = check_classfn (context, fn, NULL_TREE);
11300
11301 if (fn)
11302 new_friend = fn;
11303 }
11304 }
11305
11306 return new_friend;
11307 }
11308
11309 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11310 template arguments, as for tsubst.
11311
11312 Returns an appropriate tsubst'd friend type or error_mark_node on
11313 failure. */
11314
11315 static tree
11316 tsubst_friend_class (tree friend_tmpl, tree args)
11317 {
11318 tree tmpl;
11319
11320 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11321 {
11322 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11323 return TREE_TYPE (tmpl);
11324 }
11325
11326 tree context = CP_DECL_CONTEXT (friend_tmpl);
11327 if (TREE_CODE (context) == NAMESPACE_DECL)
11328 push_nested_namespace (context);
11329 else
11330 {
11331 context = tsubst (context, args, tf_error, NULL_TREE);
11332 push_nested_class (context);
11333 }
11334
11335 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11336 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11337
11338 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11339 {
11340 /* The friend template has already been declared. Just
11341 check to see that the declarations match, and install any new
11342 default parameters. We must tsubst the default parameters,
11343 of course. We only need the innermost template parameters
11344 because that is all that redeclare_class_template will look
11345 at. */
11346 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11347 > TMPL_ARGS_DEPTH (args))
11348 {
11349 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11350 args, tf_warning_or_error);
11351 location_t saved_input_location = input_location;
11352 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11353 tree cons = get_constraints (tmpl);
11354 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11355 input_location = saved_input_location;
11356 }
11357 }
11358 else
11359 {
11360 /* The friend template has not already been declared. In this
11361 case, the instantiation of the template class will cause the
11362 injection of this template into the namespace scope. */
11363 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11364
11365 if (tmpl != error_mark_node)
11366 {
11367 /* The new TMPL is not an instantiation of anything, so we
11368 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11369 for the new type because that is supposed to be the
11370 corresponding template decl, i.e., TMPL. */
11371 DECL_USE_TEMPLATE (tmpl) = 0;
11372 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11373 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11374 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11375 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11376
11377 /* Substitute into and set the constraints on the new declaration. */
11378 if (tree ci = get_constraints (friend_tmpl))
11379 {
11380 ++processing_template_decl;
11381 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11382 DECL_FRIEND_CONTEXT (friend_tmpl));
11383 --processing_template_decl;
11384 set_constraints (tmpl, ci);
11385 }
11386
11387 /* Inject this template into the enclosing namspace scope. */
11388 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11389 }
11390 }
11391
11392 if (TREE_CODE (context) == NAMESPACE_DECL)
11393 pop_nested_namespace (context);
11394 else
11395 pop_nested_class ();
11396
11397 return TREE_TYPE (tmpl);
11398 }
11399
11400 /* Returns zero if TYPE cannot be completed later due to circularity.
11401 Otherwise returns one. */
11402
11403 static int
11404 can_complete_type_without_circularity (tree type)
11405 {
11406 if (type == NULL_TREE || type == error_mark_node)
11407 return 0;
11408 else if (COMPLETE_TYPE_P (type))
11409 return 1;
11410 else if (TREE_CODE (type) == ARRAY_TYPE)
11411 return can_complete_type_without_circularity (TREE_TYPE (type));
11412 else if (CLASS_TYPE_P (type)
11413 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11414 return 0;
11415 else
11416 return 1;
11417 }
11418
11419 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11420 tsubst_flags_t, tree);
11421
11422 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11423 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11424
11425 static tree
11426 tsubst_attribute (tree t, tree *decl_p, tree args,
11427 tsubst_flags_t complain, tree in_decl)
11428 {
11429 gcc_assert (ATTR_IS_DEPENDENT (t));
11430
11431 tree val = TREE_VALUE (t);
11432 if (val == NULL_TREE)
11433 /* Nothing to do. */;
11434 else if ((flag_openmp || flag_openmp_simd)
11435 && is_attribute_p ("omp declare simd",
11436 get_attribute_name (t)))
11437 {
11438 tree clauses = TREE_VALUE (val);
11439 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11440 complain, in_decl);
11441 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11442 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11443 tree parms = DECL_ARGUMENTS (*decl_p);
11444 clauses
11445 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11446 if (clauses)
11447 val = build_tree_list (NULL_TREE, clauses);
11448 else
11449 val = NULL_TREE;
11450 }
11451 else if (flag_openmp
11452 && is_attribute_p ("omp declare variant base",
11453 get_attribute_name (t)))
11454 {
11455 ++cp_unevaluated_operand;
11456 tree varid
11457 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11458 in_decl, /*integral_constant_expression_p=*/false);
11459 --cp_unevaluated_operand;
11460 tree chain = TREE_CHAIN (val);
11461 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11462 tree ctx = copy_list (TREE_VALUE (val));
11463 tree simd = get_identifier ("simd");
11464 tree score = get_identifier (" score");
11465 tree condition = get_identifier ("condition");
11466 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11467 {
11468 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11469 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11470 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11471 {
11472 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11473 {
11474 tree clauses = TREE_VALUE (t2);
11475 clauses = tsubst_omp_clauses (clauses,
11476 C_ORT_OMP_DECLARE_SIMD, args,
11477 complain, in_decl);
11478 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11479 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11480 TREE_VALUE (t2) = clauses;
11481 }
11482 else
11483 {
11484 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11485 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11486 if (TREE_VALUE (t3))
11487 {
11488 bool allow_string
11489 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11490 && TREE_PURPOSE (t3) != score);
11491 tree v = TREE_VALUE (t3);
11492 if (TREE_CODE (v) == STRING_CST && allow_string)
11493 continue;
11494 v = tsubst_expr (v, args, complain, in_decl, true);
11495 v = fold_non_dependent_expr (v);
11496 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11497 || (TREE_PURPOSE (t3) == score
11498 ? TREE_CODE (v) != INTEGER_CST
11499 : !tree_fits_shwi_p (v)))
11500 {
11501 location_t loc
11502 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11503 match_loc);
11504 if (TREE_PURPOSE (t3) == score)
11505 error_at (loc, "score argument must be "
11506 "constant integer expression");
11507 else if (allow_string)
11508 error_at (loc, "property must be constant "
11509 "integer expression or string "
11510 "literal");
11511 else
11512 error_at (loc, "property must be constant "
11513 "integer expression");
11514 return NULL_TREE;
11515 }
11516 else if (TREE_PURPOSE (t3) == score
11517 && tree_int_cst_sgn (v) < 0)
11518 {
11519 location_t loc
11520 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11521 match_loc);
11522 error_at (loc, "score argument must be "
11523 "non-negative");
11524 return NULL_TREE;
11525 }
11526 TREE_VALUE (t3) = v;
11527 }
11528 }
11529 }
11530 }
11531 val = tree_cons (varid, ctx, chain);
11532 }
11533 /* If the first attribute argument is an identifier, don't
11534 pass it through tsubst. Attributes like mode, format,
11535 cleanup and several target specific attributes expect it
11536 unmodified. */
11537 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11538 {
11539 tree chain
11540 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11541 /*integral_constant_expression_p=*/false);
11542 if (chain != TREE_CHAIN (val))
11543 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11544 }
11545 else if (PACK_EXPANSION_P (val))
11546 {
11547 /* An attribute pack expansion. */
11548 tree purp = TREE_PURPOSE (t);
11549 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11550 if (pack == error_mark_node)
11551 return error_mark_node;
11552 int len = TREE_VEC_LENGTH (pack);
11553 tree list = NULL_TREE;
11554 tree *q = &list;
11555 for (int i = 0; i < len; ++i)
11556 {
11557 tree elt = TREE_VEC_ELT (pack, i);
11558 *q = build_tree_list (purp, elt);
11559 q = &TREE_CHAIN (*q);
11560 }
11561 return list;
11562 }
11563 else
11564 val = tsubst_expr (val, args, complain, in_decl,
11565 /*integral_constant_expression_p=*/false);
11566
11567 if (val != TREE_VALUE (t))
11568 return build_tree_list (TREE_PURPOSE (t), val);
11569 return t;
11570 }
11571
11572 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11573 unchanged or a new TREE_LIST chain. */
11574
11575 static tree
11576 tsubst_attributes (tree attributes, tree args,
11577 tsubst_flags_t complain, tree in_decl)
11578 {
11579 tree last_dep = NULL_TREE;
11580
11581 for (tree t = attributes; t; t = TREE_CHAIN (t))
11582 if (ATTR_IS_DEPENDENT (t))
11583 {
11584 last_dep = t;
11585 attributes = copy_list (attributes);
11586 break;
11587 }
11588
11589 if (last_dep)
11590 for (tree *p = &attributes; *p; )
11591 {
11592 tree t = *p;
11593 if (ATTR_IS_DEPENDENT (t))
11594 {
11595 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11596 if (subst != t)
11597 {
11598 *p = subst;
11599 while (*p)
11600 p = &TREE_CHAIN (*p);
11601 *p = TREE_CHAIN (t);
11602 continue;
11603 }
11604 }
11605 p = &TREE_CHAIN (*p);
11606 }
11607
11608 return attributes;
11609 }
11610
11611 /* Apply any attributes which had to be deferred until instantiation
11612 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11613 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11614
11615 static void
11616 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11617 tree args, tsubst_flags_t complain, tree in_decl)
11618 {
11619 tree last_dep = NULL_TREE;
11620 tree t;
11621 tree *p;
11622
11623 if (attributes == NULL_TREE)
11624 return;
11625
11626 if (DECL_P (*decl_p))
11627 {
11628 if (TREE_TYPE (*decl_p) == error_mark_node)
11629 return;
11630 p = &DECL_ATTRIBUTES (*decl_p);
11631 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11632 to our attributes parameter. */
11633 gcc_assert (*p == attributes);
11634 }
11635 else
11636 {
11637 p = &TYPE_ATTRIBUTES (*decl_p);
11638 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11639 lookup_template_class_1, and should be preserved. */
11640 gcc_assert (*p != attributes);
11641 while (*p)
11642 p = &TREE_CHAIN (*p);
11643 }
11644
11645 for (t = attributes; t; t = TREE_CHAIN (t))
11646 if (ATTR_IS_DEPENDENT (t))
11647 {
11648 last_dep = t;
11649 attributes = copy_list (attributes);
11650 break;
11651 }
11652
11653 *p = attributes;
11654 if (last_dep)
11655 {
11656 tree late_attrs = NULL_TREE;
11657 tree *q = &late_attrs;
11658
11659 for (; *p; )
11660 {
11661 t = *p;
11662 if (ATTR_IS_DEPENDENT (t))
11663 {
11664 *p = TREE_CHAIN (t);
11665 TREE_CHAIN (t) = NULL_TREE;
11666 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11667 while (*q)
11668 q = &TREE_CHAIN (*q);
11669 }
11670 else
11671 p = &TREE_CHAIN (t);
11672 }
11673
11674 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11675 }
11676 }
11677
11678 /* The template TMPL is being instantiated with the template arguments TARGS.
11679 Perform the access checks that we deferred when parsing the template. */
11680
11681 static void
11682 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11683 {
11684 unsigned i;
11685 deferred_access_check *chk;
11686
11687 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11688 return;
11689
11690 if (vec<deferred_access_check, va_gc> *access_checks
11691 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11692 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11693 {
11694 tree decl = chk->decl;
11695 tree diag_decl = chk->diag_decl;
11696 tree type_scope = TREE_TYPE (chk->binfo);
11697
11698 if (uses_template_parms (type_scope))
11699 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11700
11701 /* Make access check error messages point to the location
11702 of the use of the typedef. */
11703 iloc_sentinel ils (chk->loc);
11704 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11705 decl, diag_decl, tf_warning_or_error);
11706 }
11707 }
11708
11709 static tree
11710 instantiate_class_template_1 (tree type)
11711 {
11712 tree templ, args, pattern, t, member;
11713 tree typedecl;
11714 tree pbinfo;
11715 tree base_list;
11716 unsigned int saved_maximum_field_alignment;
11717 tree fn_context;
11718
11719 if (type == error_mark_node)
11720 return error_mark_node;
11721
11722 if (COMPLETE_OR_OPEN_TYPE_P (type)
11723 || uses_template_parms (type))
11724 return type;
11725
11726 /* Figure out which template is being instantiated. */
11727 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11728 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11729
11730 /* Mark the type as in the process of being defined. */
11731 TYPE_BEING_DEFINED (type) = 1;
11732
11733 /* We may be in the middle of deferred access check. Disable
11734 it now. */
11735 deferring_access_check_sentinel acs (dk_no_deferred);
11736
11737 /* Determine what specialization of the original template to
11738 instantiate. */
11739 t = most_specialized_partial_spec (type, tf_warning_or_error);
11740 if (t == error_mark_node)
11741 return error_mark_node;
11742 else if (t)
11743 {
11744 /* This TYPE is actually an instantiation of a partial
11745 specialization. We replace the innermost set of ARGS with
11746 the arguments appropriate for substitution. For example,
11747 given:
11748
11749 template <class T> struct S {};
11750 template <class T> struct S<T*> {};
11751
11752 and supposing that we are instantiating S<int*>, ARGS will
11753 presently be {int*} -- but we need {int}. */
11754 pattern = TREE_TYPE (t);
11755 args = TREE_PURPOSE (t);
11756 }
11757 else
11758 {
11759 pattern = TREE_TYPE (templ);
11760 args = CLASSTYPE_TI_ARGS (type);
11761 }
11762
11763 /* If the template we're instantiating is incomplete, then clearly
11764 there's nothing we can do. */
11765 if (!COMPLETE_TYPE_P (pattern))
11766 {
11767 /* We can try again later. */
11768 TYPE_BEING_DEFINED (type) = 0;
11769 return type;
11770 }
11771
11772 /* If we've recursively instantiated too many templates, stop. */
11773 if (! push_tinst_level (type))
11774 return type;
11775
11776 int saved_unevaluated_operand = cp_unevaluated_operand;
11777 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11778
11779 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11780 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11781 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11782 fn_context = error_mark_node;
11783 if (!fn_context)
11784 push_to_top_level ();
11785 else
11786 {
11787 cp_unevaluated_operand = 0;
11788 c_inhibit_evaluation_warnings = 0;
11789 }
11790 /* Use #pragma pack from the template context. */
11791 saved_maximum_field_alignment = maximum_field_alignment;
11792 maximum_field_alignment = TYPE_PRECISION (pattern);
11793
11794 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11795
11796 /* Set the input location to the most specialized template definition.
11797 This is needed if tsubsting causes an error. */
11798 typedecl = TYPE_MAIN_DECL (pattern);
11799 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11800 DECL_SOURCE_LOCATION (typedecl);
11801
11802 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11803 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11804 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11805 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11806 if (ANON_AGGR_TYPE_P (pattern))
11807 SET_ANON_AGGR_TYPE_P (type);
11808 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11809 {
11810 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11811 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11812 /* Adjust visibility for template arguments. */
11813 determine_visibility (TYPE_MAIN_DECL (type));
11814 }
11815 if (CLASS_TYPE_P (type))
11816 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11817
11818 pbinfo = TYPE_BINFO (pattern);
11819
11820 /* We should never instantiate a nested class before its enclosing
11821 class; we need to look up the nested class by name before we can
11822 instantiate it, and that lookup should instantiate the enclosing
11823 class. */
11824 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11825 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11826
11827 base_list = NULL_TREE;
11828 if (BINFO_N_BASE_BINFOS (pbinfo))
11829 {
11830 tree pbase_binfo;
11831 tree pushed_scope;
11832 int i;
11833
11834 /* We must enter the scope containing the type, as that is where
11835 the accessibility of types named in dependent bases are
11836 looked up from. */
11837 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11838
11839 /* Substitute into each of the bases to determine the actual
11840 basetypes. */
11841 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11842 {
11843 tree base;
11844 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11845 tree expanded_bases = NULL_TREE;
11846 int idx, len = 1;
11847
11848 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11849 {
11850 expanded_bases =
11851 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11852 args, tf_error, NULL_TREE);
11853 if (expanded_bases == error_mark_node)
11854 continue;
11855
11856 len = TREE_VEC_LENGTH (expanded_bases);
11857 }
11858
11859 for (idx = 0; idx < len; idx++)
11860 {
11861 if (expanded_bases)
11862 /* Extract the already-expanded base class. */
11863 base = TREE_VEC_ELT (expanded_bases, idx);
11864 else
11865 /* Substitute to figure out the base class. */
11866 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11867 NULL_TREE);
11868
11869 if (base == error_mark_node)
11870 continue;
11871
11872 base_list = tree_cons (access, base, base_list);
11873 if (BINFO_VIRTUAL_P (pbase_binfo))
11874 TREE_TYPE (base_list) = integer_type_node;
11875 }
11876 }
11877
11878 /* The list is now in reverse order; correct that. */
11879 base_list = nreverse (base_list);
11880
11881 if (pushed_scope)
11882 pop_scope (pushed_scope);
11883 }
11884 /* Now call xref_basetypes to set up all the base-class
11885 information. */
11886 xref_basetypes (type, base_list);
11887
11888 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11889 (int) ATTR_FLAG_TYPE_IN_PLACE,
11890 args, tf_error, NULL_TREE);
11891 fixup_attribute_variants (type);
11892
11893 /* Now that our base classes are set up, enter the scope of the
11894 class, so that name lookups into base classes, etc. will work
11895 correctly. This is precisely analogous to what we do in
11896 begin_class_definition when defining an ordinary non-template
11897 class, except we also need to push the enclosing classes. */
11898 push_nested_class (type);
11899
11900 /* Now members are processed in the order of declaration. */
11901 for (member = CLASSTYPE_DECL_LIST (pattern);
11902 member; member = TREE_CHAIN (member))
11903 {
11904 tree t = TREE_VALUE (member);
11905
11906 if (TREE_PURPOSE (member))
11907 {
11908 if (TYPE_P (t))
11909 {
11910 if (LAMBDA_TYPE_P (t))
11911 /* A closure type for a lambda in an NSDMI or default argument.
11912 Ignore it; it will be regenerated when needed. */
11913 continue;
11914
11915 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11916 && TYPE_LANG_SPECIFIC (t)
11917 && CLASSTYPE_IS_TEMPLATE (t));
11918
11919 /* If the member is a class template, then -- even after
11920 substitution -- there may be dependent types in the
11921 template argument list for the class. We increment
11922 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11923 that function will assume that no types are dependent
11924 when outside of a template. */
11925 if (class_template_p)
11926 ++processing_template_decl;
11927 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11928 if (class_template_p)
11929 --processing_template_decl;
11930 if (newtag == error_mark_node)
11931 continue;
11932
11933 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11934 {
11935 tree name = TYPE_IDENTIFIER (t);
11936
11937 if (class_template_p)
11938 /* Unfortunately, lookup_template_class sets
11939 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11940 instantiation (i.e., for the type of a member
11941 template class nested within a template class.)
11942 This behavior is required for
11943 maybe_process_partial_specialization to work
11944 correctly, but is not accurate in this case;
11945 the TAG is not an instantiation of anything.
11946 (The corresponding TEMPLATE_DECL is an
11947 instantiation, but the TYPE is not.) */
11948 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11949
11950 /* Now, we call pushtag to put this NEWTAG into the scope of
11951 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11952 pushtag calling push_template_decl. We don't have to do
11953 this for enums because it will already have been done in
11954 tsubst_enum. */
11955 if (name)
11956 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11957 pushtag (name, newtag);
11958 }
11959 }
11960 else if (DECL_DECLARES_FUNCTION_P (t))
11961 {
11962 tree r;
11963
11964 if (TREE_CODE (t) == TEMPLATE_DECL)
11965 ++processing_template_decl;
11966 r = tsubst (t, args, tf_error, NULL_TREE);
11967 if (TREE_CODE (t) == TEMPLATE_DECL)
11968 --processing_template_decl;
11969 set_current_access_from_decl (r);
11970 finish_member_declaration (r);
11971 /* Instantiate members marked with attribute used. */
11972 if (r != error_mark_node && DECL_PRESERVE_P (r))
11973 mark_used (r);
11974 if (TREE_CODE (r) == FUNCTION_DECL
11975 && DECL_OMP_DECLARE_REDUCTION_P (r))
11976 cp_check_omp_declare_reduction (r);
11977 }
11978 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11979 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11980 /* A closure type for a lambda in an NSDMI or default argument.
11981 Ignore it; it will be regenerated when needed. */;
11982 else
11983 {
11984 /* Build new TYPE_FIELDS. */
11985 if (TREE_CODE (t) == STATIC_ASSERT)
11986 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11987 /*integral_constant_expression_p=*/true);
11988 else if (TREE_CODE (t) != CONST_DECL)
11989 {
11990 tree r;
11991 tree vec = NULL_TREE;
11992 int len = 1;
11993
11994 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11995 /* The file and line for this declaration, to
11996 assist in error message reporting. Since we
11997 called push_tinst_level above, we don't need to
11998 restore these. */
11999 input_location = DECL_SOURCE_LOCATION (t);
12000
12001 if (TREE_CODE (t) == TEMPLATE_DECL)
12002 ++processing_template_decl;
12003 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12004 if (TREE_CODE (t) == TEMPLATE_DECL)
12005 --processing_template_decl;
12006
12007 if (TREE_CODE (r) == TREE_VEC)
12008 {
12009 /* A capture pack became multiple fields. */
12010 vec = r;
12011 len = TREE_VEC_LENGTH (vec);
12012 }
12013
12014 for (int i = 0; i < len; ++i)
12015 {
12016 if (vec)
12017 r = TREE_VEC_ELT (vec, i);
12018 if (VAR_P (r))
12019 {
12020 /* In [temp.inst]:
12021
12022 [t]he initialization (and any associated
12023 side-effects) of a static data member does
12024 not occur unless the static data member is
12025 itself used in a way that requires the
12026 definition of the static data member to
12027 exist.
12028
12029 Therefore, we do not substitute into the
12030 initialized for the static data member here. */
12031 finish_static_data_member_decl
12032 (r,
12033 /*init=*/NULL_TREE,
12034 /*init_const_expr_p=*/false,
12035 /*asmspec_tree=*/NULL_TREE,
12036 /*flags=*/0);
12037 /* Instantiate members marked with attribute used. */
12038 if (r != error_mark_node && DECL_PRESERVE_P (r))
12039 mark_used (r);
12040 }
12041 else if (TREE_CODE (r) == FIELD_DECL)
12042 {
12043 /* Determine whether R has a valid type and can be
12044 completed later. If R is invalid, then its type
12045 is replaced by error_mark_node. */
12046 tree rtype = TREE_TYPE (r);
12047 if (can_complete_type_without_circularity (rtype))
12048 complete_type (rtype);
12049
12050 if (!complete_or_array_type_p (rtype))
12051 {
12052 /* If R's type couldn't be completed and
12053 it isn't a flexible array member (whose
12054 type is incomplete by definition) give
12055 an error. */
12056 cxx_incomplete_type_error (r, rtype);
12057 TREE_TYPE (r) = error_mark_node;
12058 }
12059 else if (TREE_CODE (rtype) == ARRAY_TYPE
12060 && TYPE_DOMAIN (rtype) == NULL_TREE
12061 && (TREE_CODE (type) == UNION_TYPE
12062 || TREE_CODE (type) == QUAL_UNION_TYPE))
12063 {
12064 error ("flexible array member %qD in union", r);
12065 TREE_TYPE (r) = error_mark_node;
12066 }
12067 else if (!verify_type_context (input_location,
12068 TCTX_FIELD, rtype))
12069 TREE_TYPE (r) = error_mark_node;
12070 }
12071
12072 /* If it is a TYPE_DECL for a class-scoped
12073 ENUMERAL_TYPE, such a thing will already have
12074 been added to the field list by tsubst_enum
12075 in finish_member_declaration case above. */
12076 if (!(TREE_CODE (r) == TYPE_DECL
12077 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12078 && DECL_ARTIFICIAL (r)))
12079 {
12080 set_current_access_from_decl (r);
12081 finish_member_declaration (r);
12082 }
12083 }
12084 }
12085 }
12086 }
12087 else
12088 {
12089 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12090 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12091 {
12092 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12093
12094 tree friend_type = t;
12095 bool adjust_processing_template_decl = false;
12096
12097 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12098 {
12099 /* template <class T> friend class C; */
12100 friend_type = tsubst_friend_class (friend_type, args);
12101 adjust_processing_template_decl = true;
12102 }
12103 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12104 {
12105 /* template <class T> friend class C::D; */
12106 friend_type = tsubst (friend_type, args,
12107 tf_warning_or_error, NULL_TREE);
12108 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12109 friend_type = TREE_TYPE (friend_type);
12110 adjust_processing_template_decl = true;
12111 }
12112 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12113 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12114 {
12115 /* This could be either
12116
12117 friend class T::C;
12118
12119 when dependent_type_p is false or
12120
12121 template <class U> friend class T::C;
12122
12123 otherwise. */
12124 /* Bump processing_template_decl in case this is something like
12125 template <class T> friend struct A<T>::B. */
12126 ++processing_template_decl;
12127 friend_type = tsubst (friend_type, args,
12128 tf_warning_or_error, NULL_TREE);
12129 if (dependent_type_p (friend_type))
12130 adjust_processing_template_decl = true;
12131 --processing_template_decl;
12132 }
12133 else if (uses_template_parms (friend_type))
12134 /* friend class C<T>; */
12135 friend_type = tsubst (friend_type, args,
12136 tf_warning_or_error, NULL_TREE);
12137
12138 /* Otherwise it's
12139
12140 friend class C;
12141
12142 where C is already declared or
12143
12144 friend class C<int>;
12145
12146 We don't have to do anything in these cases. */
12147
12148 if (adjust_processing_template_decl)
12149 /* Trick make_friend_class into realizing that the friend
12150 we're adding is a template, not an ordinary class. It's
12151 important that we use make_friend_class since it will
12152 perform some error-checking and output cross-reference
12153 information. */
12154 ++processing_template_decl;
12155
12156 if (friend_type != error_mark_node)
12157 make_friend_class (type, friend_type, /*complain=*/false);
12158
12159 if (adjust_processing_template_decl)
12160 --processing_template_decl;
12161 }
12162 else
12163 {
12164 /* Build new DECL_FRIENDLIST. */
12165 tree r;
12166
12167 /* The file and line for this declaration, to
12168 assist in error message reporting. Since we
12169 called push_tinst_level above, we don't need to
12170 restore these. */
12171 input_location = DECL_SOURCE_LOCATION (t);
12172
12173 if (TREE_CODE (t) == TEMPLATE_DECL)
12174 {
12175 ++processing_template_decl;
12176 push_deferring_access_checks (dk_no_check);
12177 }
12178
12179 r = tsubst_friend_function (t, args);
12180 add_friend (type, r, /*complain=*/false);
12181 if (TREE_CODE (t) == TEMPLATE_DECL)
12182 {
12183 pop_deferring_access_checks ();
12184 --processing_template_decl;
12185 }
12186 }
12187 }
12188 }
12189
12190 if (fn_context)
12191 {
12192 /* Restore these before substituting into the lambda capture
12193 initializers. */
12194 cp_unevaluated_operand = saved_unevaluated_operand;
12195 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12196 }
12197
12198 /* Set the file and line number information to whatever is given for
12199 the class itself. This puts error messages involving generated
12200 implicit functions at a predictable point, and the same point
12201 that would be used for non-template classes. */
12202 input_location = DECL_SOURCE_LOCATION (typedecl);
12203
12204 unreverse_member_declarations (type);
12205 finish_struct_1 (type);
12206 TYPE_BEING_DEFINED (type) = 0;
12207
12208 /* We don't instantiate default arguments for member functions. 14.7.1:
12209
12210 The implicit instantiation of a class template specialization causes
12211 the implicit instantiation of the declarations, but not of the
12212 definitions or default arguments, of the class member functions,
12213 member classes, static data members and member templates.... */
12214
12215 perform_instantiation_time_access_checks (pattern, args);
12216 perform_deferred_access_checks (tf_warning_or_error);
12217 pop_nested_class ();
12218 maximum_field_alignment = saved_maximum_field_alignment;
12219 if (!fn_context)
12220 pop_from_top_level ();
12221 pop_tinst_level ();
12222
12223 /* The vtable for a template class can be emitted in any translation
12224 unit in which the class is instantiated. When there is no key
12225 method, however, finish_struct_1 will already have added TYPE to
12226 the keyed_classes. */
12227 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12228 vec_safe_push (keyed_classes, type);
12229
12230 return type;
12231 }
12232
12233 /* Wrapper for instantiate_class_template_1. */
12234
12235 tree
12236 instantiate_class_template (tree type)
12237 {
12238 tree ret;
12239 timevar_push (TV_TEMPLATE_INST);
12240 ret = instantiate_class_template_1 (type);
12241 timevar_pop (TV_TEMPLATE_INST);
12242 return ret;
12243 }
12244
12245 tree
12246 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12247 {
12248 tree r;
12249
12250 if (!t)
12251 r = t;
12252 else if (TYPE_P (t))
12253 r = tsubst (t, args, complain, in_decl);
12254 else
12255 {
12256 if (!(complain & tf_warning))
12257 ++c_inhibit_evaluation_warnings;
12258 r = tsubst_expr (t, args, complain, in_decl,
12259 /*integral_constant_expression_p=*/true);
12260 if (!(complain & tf_warning))
12261 --c_inhibit_evaluation_warnings;
12262 }
12263
12264 return r;
12265 }
12266
12267 /* Given a function parameter pack TMPL_PARM and some function parameters
12268 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12269 and set *SPEC_P to point at the next point in the list. */
12270
12271 tree
12272 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12273 {
12274 /* Collect all of the extra "packed" parameters into an
12275 argument pack. */
12276 tree parmvec;
12277 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12278 tree spec_parm = *spec_p;
12279 int i, len;
12280
12281 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12282 if (tmpl_parm
12283 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12284 break;
12285
12286 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12287 parmvec = make_tree_vec (len);
12288 spec_parm = *spec_p;
12289 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12290 {
12291 tree elt = spec_parm;
12292 if (DECL_PACK_P (elt))
12293 elt = make_pack_expansion (elt);
12294 TREE_VEC_ELT (parmvec, i) = elt;
12295 }
12296
12297 /* Build the argument packs. */
12298 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12299 *spec_p = spec_parm;
12300
12301 return argpack;
12302 }
12303
12304 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12305 NONTYPE_ARGUMENT_PACK. */
12306
12307 static tree
12308 make_fnparm_pack (tree spec_parm)
12309 {
12310 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12311 }
12312
12313 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12314 pack expansion with no extra args, 2 if it has extra args, or 0
12315 if it is not a pack expansion. */
12316
12317 static int
12318 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12319 {
12320 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12321 /* We're being called before this happens in tsubst_pack_expansion. */
12322 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12323 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12324 if (i >= TREE_VEC_LENGTH (vec))
12325 return 0;
12326 tree elt = TREE_VEC_ELT (vec, i);
12327 if (DECL_P (elt))
12328 /* A decl pack is itself an expansion. */
12329 elt = TREE_TYPE (elt);
12330 if (!PACK_EXPANSION_P (elt))
12331 return 0;
12332 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12333 return 2;
12334 return 1;
12335 }
12336
12337
12338 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12339
12340 static tree
12341 make_argument_pack_select (tree arg_pack, unsigned index)
12342 {
12343 tree aps = make_node (ARGUMENT_PACK_SELECT);
12344
12345 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12346 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12347
12348 return aps;
12349 }
12350
12351 /* This is a subroutine of tsubst_pack_expansion.
12352
12353 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12354 mechanism to store the (non complete list of) arguments of the
12355 substitution and return a non substituted pack expansion, in order
12356 to wait for when we have enough arguments to really perform the
12357 substitution. */
12358
12359 static bool
12360 use_pack_expansion_extra_args_p (tree parm_packs,
12361 int arg_pack_len,
12362 bool has_empty_arg)
12363 {
12364 /* If one pack has an expansion and another pack has a normal
12365 argument or if one pack has an empty argument and an another
12366 one hasn't then tsubst_pack_expansion cannot perform the
12367 substitution and need to fall back on the
12368 PACK_EXPANSION_EXTRA mechanism. */
12369 if (parm_packs == NULL_TREE)
12370 return false;
12371 else if (has_empty_arg)
12372 {
12373 /* If all the actual packs are pack expansions, we can still
12374 subsitute directly. */
12375 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12376 {
12377 tree a = TREE_VALUE (p);
12378 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12379 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12380 a = ARGUMENT_PACK_ARGS (a);
12381 if (TREE_VEC_LENGTH (a) == 1)
12382 a = TREE_VEC_ELT (a, 0);
12383 if (PACK_EXPANSION_P (a))
12384 continue;
12385 return true;
12386 }
12387 return false;
12388 }
12389
12390 bool has_expansion_arg = false;
12391 for (int i = 0 ; i < arg_pack_len; ++i)
12392 {
12393 bool has_non_expansion_arg = false;
12394 for (tree parm_pack = parm_packs;
12395 parm_pack;
12396 parm_pack = TREE_CHAIN (parm_pack))
12397 {
12398 tree arg = TREE_VALUE (parm_pack);
12399
12400 int exp = argument_pack_element_is_expansion_p (arg, i);
12401 if (exp == 2)
12402 /* We can't substitute a pack expansion with extra args into
12403 our pattern. */
12404 return true;
12405 else if (exp)
12406 has_expansion_arg = true;
12407 else
12408 has_non_expansion_arg = true;
12409 }
12410
12411 if (has_expansion_arg && has_non_expansion_arg)
12412 return true;
12413 }
12414 return false;
12415 }
12416
12417 /* [temp.variadic]/6 says that:
12418
12419 The instantiation of a pack expansion [...]
12420 produces a list E1,E2, ..., En, where N is the number of elements
12421 in the pack expansion parameters.
12422
12423 This subroutine of tsubst_pack_expansion produces one of these Ei.
12424
12425 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12426 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12427 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12428 INDEX is the index 'i' of the element Ei to produce. ARGS,
12429 COMPLAIN, and IN_DECL are the same parameters as for the
12430 tsubst_pack_expansion function.
12431
12432 The function returns the resulting Ei upon successful completion,
12433 or error_mark_node.
12434
12435 Note that this function possibly modifies the ARGS parameter, so
12436 it's the responsibility of the caller to restore it. */
12437
12438 static tree
12439 gen_elem_of_pack_expansion_instantiation (tree pattern,
12440 tree parm_packs,
12441 unsigned index,
12442 tree args /* This parm gets
12443 modified. */,
12444 tsubst_flags_t complain,
12445 tree in_decl)
12446 {
12447 tree t;
12448 bool ith_elem_is_expansion = false;
12449
12450 /* For each parameter pack, change the substitution of the parameter
12451 pack to the ith argument in its argument pack, then expand the
12452 pattern. */
12453 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12454 {
12455 tree parm = TREE_PURPOSE (pack);
12456 tree arg_pack = TREE_VALUE (pack);
12457 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12458
12459 ith_elem_is_expansion |=
12460 argument_pack_element_is_expansion_p (arg_pack, index);
12461
12462 /* Select the Ith argument from the pack. */
12463 if (TREE_CODE (parm) == PARM_DECL
12464 || VAR_P (parm)
12465 || TREE_CODE (parm) == FIELD_DECL)
12466 {
12467 if (index == 0)
12468 {
12469 aps = make_argument_pack_select (arg_pack, index);
12470 if (!mark_used (parm, complain) && !(complain & tf_error))
12471 return error_mark_node;
12472 register_local_specialization (aps, parm);
12473 }
12474 else
12475 aps = retrieve_local_specialization (parm);
12476 }
12477 else
12478 {
12479 int idx, level;
12480 template_parm_level_and_index (parm, &level, &idx);
12481
12482 if (index == 0)
12483 {
12484 aps = make_argument_pack_select (arg_pack, index);
12485 /* Update the corresponding argument. */
12486 TMPL_ARG (args, level, idx) = aps;
12487 }
12488 else
12489 /* Re-use the ARGUMENT_PACK_SELECT. */
12490 aps = TMPL_ARG (args, level, idx);
12491 }
12492 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12493 }
12494
12495 /* Substitute into the PATTERN with the (possibly altered)
12496 arguments. */
12497 if (pattern == in_decl)
12498 /* Expanding a fixed parameter pack from
12499 coerce_template_parameter_pack. */
12500 t = tsubst_decl (pattern, args, complain);
12501 else if (pattern == error_mark_node)
12502 t = error_mark_node;
12503 else if (!TYPE_P (pattern))
12504 t = tsubst_expr (pattern, args, complain, in_decl,
12505 /*integral_constant_expression_p=*/false);
12506 else
12507 t = tsubst (pattern, args, complain, in_decl);
12508
12509 /* If the Ith argument pack element is a pack expansion, then
12510 the Ith element resulting from the substituting is going to
12511 be a pack expansion as well. */
12512 if (ith_elem_is_expansion)
12513 t = make_pack_expansion (t, complain);
12514
12515 return t;
12516 }
12517
12518 /* When the unexpanded parameter pack in a fold expression expands to an empty
12519 sequence, the value of the expression is as follows; the program is
12520 ill-formed if the operator is not listed in this table.
12521
12522 && true
12523 || false
12524 , void() */
12525
12526 tree
12527 expand_empty_fold (tree t, tsubst_flags_t complain)
12528 {
12529 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12530 if (!FOLD_EXPR_MODIFY_P (t))
12531 switch (code)
12532 {
12533 case TRUTH_ANDIF_EXPR:
12534 return boolean_true_node;
12535 case TRUTH_ORIF_EXPR:
12536 return boolean_false_node;
12537 case COMPOUND_EXPR:
12538 return void_node;
12539 default:
12540 break;
12541 }
12542
12543 if (complain & tf_error)
12544 error_at (location_of (t),
12545 "fold of empty expansion over %O", code);
12546 return error_mark_node;
12547 }
12548
12549 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12550 form an expression that combines the two terms using the
12551 operator of T. */
12552
12553 static tree
12554 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12555 {
12556 tree op = FOLD_EXPR_OP (t);
12557 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12558
12559 // Handle compound assignment operators.
12560 if (FOLD_EXPR_MODIFY_P (t))
12561 return build_x_modify_expr (input_location, left, code, right, complain);
12562
12563 warning_sentinel s(warn_parentheses);
12564 switch (code)
12565 {
12566 case COMPOUND_EXPR:
12567 return build_x_compound_expr (input_location, left, right, complain);
12568 default:
12569 return build_x_binary_op (input_location, code,
12570 left, TREE_CODE (left),
12571 right, TREE_CODE (right),
12572 /*overload=*/NULL,
12573 complain);
12574 }
12575 }
12576
12577 /* Substitute ARGS into the pack of a fold expression T. */
12578
12579 static inline tree
12580 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12581 {
12582 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12583 }
12584
12585 /* Substitute ARGS into the pack of a fold expression T. */
12586
12587 static inline tree
12588 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12589 {
12590 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12591 }
12592
12593 /* Expand a PACK of arguments into a grouped as left fold.
12594 Given a pack containing elements A0, A1, ..., An and an
12595 operator @, this builds the expression:
12596
12597 ((A0 @ A1) @ A2) ... @ An
12598
12599 Note that PACK must not be empty.
12600
12601 The operator is defined by the original fold expression T. */
12602
12603 static tree
12604 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12605 {
12606 tree left = TREE_VEC_ELT (pack, 0);
12607 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12608 {
12609 tree right = TREE_VEC_ELT (pack, i);
12610 left = fold_expression (t, left, right, complain);
12611 }
12612 return left;
12613 }
12614
12615 /* Substitute into a unary left fold expression. */
12616
12617 static tree
12618 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12619 tree in_decl)
12620 {
12621 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12622 if (pack == error_mark_node)
12623 return error_mark_node;
12624 if (PACK_EXPANSION_P (pack))
12625 {
12626 tree r = copy_node (t);
12627 FOLD_EXPR_PACK (r) = pack;
12628 return r;
12629 }
12630 if (TREE_VEC_LENGTH (pack) == 0)
12631 return expand_empty_fold (t, complain);
12632 else
12633 return expand_left_fold (t, pack, complain);
12634 }
12635
12636 /* Substitute into a binary left fold expression.
12637
12638 Do ths by building a single (non-empty) vector of argumnts and
12639 building the expression from those elements. */
12640
12641 static tree
12642 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12643 tree in_decl)
12644 {
12645 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12646 if (pack == error_mark_node)
12647 return error_mark_node;
12648 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12649 if (init == error_mark_node)
12650 return error_mark_node;
12651
12652 if (PACK_EXPANSION_P (pack))
12653 {
12654 tree r = copy_node (t);
12655 FOLD_EXPR_PACK (r) = pack;
12656 FOLD_EXPR_INIT (r) = init;
12657 return r;
12658 }
12659
12660 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12661 TREE_VEC_ELT (vec, 0) = init;
12662 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12663 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12664
12665 return expand_left_fold (t, vec, complain);
12666 }
12667
12668 /* Expand a PACK of arguments into a grouped as right fold.
12669 Given a pack containing elementns A0, A1, ..., and an
12670 operator @, this builds the expression:
12671
12672 A0@ ... (An-2 @ (An-1 @ An))
12673
12674 Note that PACK must not be empty.
12675
12676 The operator is defined by the original fold expression T. */
12677
12678 tree
12679 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12680 {
12681 // Build the expression.
12682 int n = TREE_VEC_LENGTH (pack);
12683 tree right = TREE_VEC_ELT (pack, n - 1);
12684 for (--n; n != 0; --n)
12685 {
12686 tree left = TREE_VEC_ELT (pack, n - 1);
12687 right = fold_expression (t, left, right, complain);
12688 }
12689 return right;
12690 }
12691
12692 /* Substitute into a unary right fold expression. */
12693
12694 static tree
12695 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12696 tree in_decl)
12697 {
12698 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12699 if (pack == error_mark_node)
12700 return error_mark_node;
12701 if (PACK_EXPANSION_P (pack))
12702 {
12703 tree r = copy_node (t);
12704 FOLD_EXPR_PACK (r) = pack;
12705 return r;
12706 }
12707 if (TREE_VEC_LENGTH (pack) == 0)
12708 return expand_empty_fold (t, complain);
12709 else
12710 return expand_right_fold (t, pack, complain);
12711 }
12712
12713 /* Substitute into a binary right fold expression.
12714
12715 Do ths by building a single (non-empty) vector of arguments and
12716 building the expression from those elements. */
12717
12718 static tree
12719 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12720 tree in_decl)
12721 {
12722 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12723 if (pack == error_mark_node)
12724 return error_mark_node;
12725 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12726 if (init == error_mark_node)
12727 return error_mark_node;
12728
12729 if (PACK_EXPANSION_P (pack))
12730 {
12731 tree r = copy_node (t);
12732 FOLD_EXPR_PACK (r) = pack;
12733 FOLD_EXPR_INIT (r) = init;
12734 return r;
12735 }
12736
12737 int n = TREE_VEC_LENGTH (pack);
12738 tree vec = make_tree_vec (n + 1);
12739 for (int i = 0; i < n; ++i)
12740 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12741 TREE_VEC_ELT (vec, n) = init;
12742
12743 return expand_right_fold (t, vec, complain);
12744 }
12745
12746 /* Walk through the pattern of a pack expansion, adding everything in
12747 local_specializations to a list. */
12748
12749 class el_data
12750 {
12751 public:
12752 hash_set<tree> internal;
12753 tree extra;
12754 tsubst_flags_t complain;
12755
12756 el_data (tsubst_flags_t c)
12757 : extra (NULL_TREE), complain (c) {}
12758 };
12759 static tree
12760 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12761 {
12762 el_data &data = *reinterpret_cast<el_data*>(data_);
12763 tree *extra = &data.extra;
12764 tsubst_flags_t complain = data.complain;
12765
12766 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12767 /* Remember local typedefs (85214). */
12768 tp = &TYPE_NAME (*tp);
12769
12770 if (TREE_CODE (*tp) == DECL_EXPR)
12771 data.internal.add (DECL_EXPR_DECL (*tp));
12772 else if (tree spec = retrieve_local_specialization (*tp))
12773 {
12774 if (data.internal.contains (*tp))
12775 /* Don't mess with variables declared within the pattern. */
12776 return NULL_TREE;
12777 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12778 {
12779 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12780 tree args = ARGUMENT_PACK_ARGS (spec);
12781 if (TREE_VEC_LENGTH (args) == 1)
12782 {
12783 tree elt = TREE_VEC_ELT (args, 0);
12784 if (PACK_EXPANSION_P (elt))
12785 elt = PACK_EXPANSION_PATTERN (elt);
12786 if (DECL_PACK_P (elt))
12787 spec = elt;
12788 }
12789 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12790 {
12791 /* Handle lambda capture here, since we aren't doing any
12792 substitution now, and so tsubst_copy won't call
12793 process_outer_var_ref. */
12794 tree args = ARGUMENT_PACK_ARGS (spec);
12795 int len = TREE_VEC_LENGTH (args);
12796 for (int i = 0; i < len; ++i)
12797 {
12798 tree arg = TREE_VEC_ELT (args, i);
12799 tree carg = arg;
12800 if (outer_automatic_var_p (arg))
12801 carg = process_outer_var_ref (arg, complain);
12802 if (carg != arg)
12803 {
12804 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12805 proxies. */
12806 if (i == 0)
12807 {
12808 spec = copy_node (spec);
12809 args = copy_node (args);
12810 SET_ARGUMENT_PACK_ARGS (spec, args);
12811 register_local_specialization (spec, *tp);
12812 }
12813 TREE_VEC_ELT (args, i) = carg;
12814 }
12815 }
12816 }
12817 }
12818 if (outer_automatic_var_p (spec))
12819 spec = process_outer_var_ref (spec, complain);
12820 *extra = tree_cons (*tp, spec, *extra);
12821 }
12822 return NULL_TREE;
12823 }
12824 static tree
12825 extract_local_specs (tree pattern, tsubst_flags_t complain)
12826 {
12827 el_data data (complain);
12828 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12829 return data.extra;
12830 }
12831
12832 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12833 for use in PACK_EXPANSION_EXTRA_ARGS. */
12834
12835 tree
12836 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12837 {
12838 tree extra = args;
12839 if (local_specializations)
12840 if (tree locals = extract_local_specs (pattern, complain))
12841 extra = tree_cons (NULL_TREE, extra, locals);
12842 return extra;
12843 }
12844
12845 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12846 normal template args to ARGS. */
12847
12848 tree
12849 add_extra_args (tree extra, tree args)
12850 {
12851 if (extra && TREE_CODE (extra) == TREE_LIST)
12852 {
12853 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12854 {
12855 /* The partial instantiation involved local declarations collected in
12856 extract_local_specs; map from the general template to our local
12857 context. */
12858 tree gen = TREE_PURPOSE (elt);
12859 tree inst = TREE_VALUE (elt);
12860 if (DECL_P (inst))
12861 if (tree local = retrieve_local_specialization (inst))
12862 inst = local;
12863 /* else inst is already a full instantiation of the pack. */
12864 register_local_specialization (inst, gen);
12865 }
12866 gcc_assert (!TREE_PURPOSE (extra));
12867 extra = TREE_VALUE (extra);
12868 }
12869 #if 1
12870 /* I think we should always be able to substitute dependent args into the
12871 pattern. If that turns out to be incorrect in some cases, enable the
12872 alternate code (and add complain/in_decl parms to this function). */
12873 gcc_checking_assert (!uses_template_parms (extra));
12874 #else
12875 if (!uses_template_parms (extra))
12876 {
12877 gcc_unreachable ();
12878 extra = tsubst_template_args (extra, args, complain, in_decl);
12879 args = add_outermost_template_args (args, extra);
12880 }
12881 else
12882 #endif
12883 args = add_to_template_args (extra, args);
12884 return args;
12885 }
12886
12887 /* Substitute ARGS into T, which is an pack expansion
12888 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12889 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12890 (if only a partial substitution could be performed) or
12891 ERROR_MARK_NODE if there was an error. */
12892 tree
12893 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12894 tree in_decl)
12895 {
12896 tree pattern;
12897 tree pack, packs = NULL_TREE;
12898 bool unsubstituted_packs = false;
12899 int i, len = -1;
12900 tree result;
12901 bool need_local_specializations = false;
12902 int levels;
12903
12904 gcc_assert (PACK_EXPANSION_P (t));
12905 pattern = PACK_EXPANSION_PATTERN (t);
12906
12907 /* Add in any args remembered from an earlier partial instantiation. */
12908 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12909
12910 levels = TMPL_ARGS_DEPTH (args);
12911
12912 /* Determine the argument packs that will instantiate the parameter
12913 packs used in the expansion expression. While we're at it,
12914 compute the number of arguments to be expanded and make sure it
12915 is consistent. */
12916 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12917 pack = TREE_CHAIN (pack))
12918 {
12919 tree parm_pack = TREE_VALUE (pack);
12920 tree arg_pack = NULL_TREE;
12921 tree orig_arg = NULL_TREE;
12922 int level = 0;
12923
12924 if (TREE_CODE (parm_pack) == BASES)
12925 {
12926 gcc_assert (parm_pack == pattern);
12927 if (BASES_DIRECT (parm_pack))
12928 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12929 args, complain,
12930 in_decl, false),
12931 complain);
12932 else
12933 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12934 args, complain, in_decl,
12935 false), complain);
12936 }
12937 else if (builtin_pack_call_p (parm_pack))
12938 {
12939 if (parm_pack != pattern)
12940 {
12941 if (complain & tf_error)
12942 sorry ("%qE is not the entire pattern of the pack expansion",
12943 parm_pack);
12944 return error_mark_node;
12945 }
12946 return expand_builtin_pack_call (parm_pack, args,
12947 complain, in_decl);
12948 }
12949 else if (TREE_CODE (parm_pack) == PARM_DECL)
12950 {
12951 /* We know we have correct local_specializations if this
12952 expansion is at function scope, or if we're dealing with a
12953 local parameter in a requires expression; for the latter,
12954 tsubst_requires_expr set it up appropriately. */
12955 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12956 arg_pack = retrieve_local_specialization (parm_pack);
12957 else
12958 /* We can't rely on local_specializations for a parameter
12959 name used later in a function declaration (such as in a
12960 late-specified return type). Even if it exists, it might
12961 have the wrong value for a recursive call. */
12962 need_local_specializations = true;
12963
12964 if (!arg_pack)
12965 {
12966 /* This parameter pack was used in an unevaluated context. Just
12967 make a dummy decl, since it's only used for its type. */
12968 ++cp_unevaluated_operand;
12969 arg_pack = tsubst_decl (parm_pack, args, complain);
12970 --cp_unevaluated_operand;
12971 if (arg_pack && DECL_PACK_P (arg_pack))
12972 /* Partial instantiation of the parm_pack, we can't build
12973 up an argument pack yet. */
12974 arg_pack = NULL_TREE;
12975 else
12976 arg_pack = make_fnparm_pack (arg_pack);
12977 }
12978 else if (DECL_PACK_P (arg_pack))
12979 /* This argument pack isn't fully instantiated yet. */
12980 arg_pack = NULL_TREE;
12981 }
12982 else if (is_capture_proxy (parm_pack))
12983 {
12984 arg_pack = retrieve_local_specialization (parm_pack);
12985 if (DECL_PACK_P (arg_pack))
12986 arg_pack = NULL_TREE;
12987 }
12988 else
12989 {
12990 int idx;
12991 template_parm_level_and_index (parm_pack, &level, &idx);
12992 if (level <= levels)
12993 arg_pack = TMPL_ARG (args, level, idx);
12994
12995 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12996 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12997 arg_pack = NULL_TREE;
12998 }
12999
13000 orig_arg = arg_pack;
13001 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13002 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13003
13004 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13005 /* This can only happen if we forget to expand an argument
13006 pack somewhere else. Just return an error, silently. */
13007 {
13008 result = make_tree_vec (1);
13009 TREE_VEC_ELT (result, 0) = error_mark_node;
13010 return result;
13011 }
13012
13013 if (arg_pack)
13014 {
13015 int my_len =
13016 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13017
13018 /* Don't bother trying to do a partial substitution with
13019 incomplete packs; we'll try again after deduction. */
13020 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13021 return t;
13022
13023 if (len < 0)
13024 len = my_len;
13025 else if (len != my_len)
13026 {
13027 if (!(complain & tf_error))
13028 /* Fail quietly. */;
13029 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13030 error ("mismatched argument pack lengths while expanding %qT",
13031 pattern);
13032 else
13033 error ("mismatched argument pack lengths while expanding %qE",
13034 pattern);
13035 return error_mark_node;
13036 }
13037
13038 /* Keep track of the parameter packs and their corresponding
13039 argument packs. */
13040 packs = tree_cons (parm_pack, arg_pack, packs);
13041 TREE_TYPE (packs) = orig_arg;
13042 }
13043 else
13044 {
13045 /* We can't substitute for this parameter pack. We use a flag as
13046 well as the missing_level counter because function parameter
13047 packs don't have a level. */
13048 gcc_assert (processing_template_decl || is_auto (parm_pack));
13049 unsubstituted_packs = true;
13050 }
13051 }
13052
13053 /* If the expansion is just T..., return the matching argument pack, unless
13054 we need to call convert_from_reference on all the elements. This is an
13055 important optimization; see c++/68422. */
13056 if (!unsubstituted_packs
13057 && TREE_PURPOSE (packs) == pattern)
13058 {
13059 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13060
13061 /* If the argument pack is a single pack expansion, pull it out. */
13062 if (TREE_VEC_LENGTH (args) == 1
13063 && pack_expansion_args_count (args))
13064 return TREE_VEC_ELT (args, 0);
13065
13066 /* Types need no adjustment, nor does sizeof..., and if we still have
13067 some pack expansion args we won't do anything yet. */
13068 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13069 || PACK_EXPANSION_SIZEOF_P (t)
13070 || pack_expansion_args_count (args))
13071 return args;
13072 /* Also optimize expression pack expansions if we can tell that the
13073 elements won't have reference type. */
13074 tree type = TREE_TYPE (pattern);
13075 if (type && !TYPE_REF_P (type)
13076 && !PACK_EXPANSION_P (type)
13077 && !WILDCARD_TYPE_P (type))
13078 return args;
13079 /* Otherwise use the normal path so we get convert_from_reference. */
13080 }
13081
13082 /* We cannot expand this expansion expression, because we don't have
13083 all of the argument packs we need. */
13084 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13085 {
13086 /* We got some full packs, but we can't substitute them in until we
13087 have values for all the packs. So remember these until then. */
13088
13089 t = make_pack_expansion (pattern, complain);
13090 PACK_EXPANSION_EXTRA_ARGS (t)
13091 = build_extra_args (pattern, args, complain);
13092 return t;
13093 }
13094
13095 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13096 type, so create our own local specializations map; the current map is
13097 either NULL or (in the case of recursive unification) might have
13098 bindings that we don't want to use or alter. */
13099 local_specialization_stack lss (need_local_specializations
13100 ? lss_blank : lss_nop);
13101
13102 if (unsubstituted_packs)
13103 {
13104 /* There were no real arguments, we're just replacing a parameter
13105 pack with another version of itself. Substitute into the
13106 pattern and return a PACK_EXPANSION_*. The caller will need to
13107 deal with that. */
13108 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13109 t = tsubst_expr (pattern, args, complain, in_decl,
13110 /*integral_constant_expression_p=*/false);
13111 else
13112 t = tsubst (pattern, args, complain, in_decl);
13113 t = make_pack_expansion (t, complain);
13114 return t;
13115 }
13116
13117 gcc_assert (len >= 0);
13118
13119 /* For each argument in each argument pack, substitute into the
13120 pattern. */
13121 result = make_tree_vec (len);
13122 tree elem_args = copy_template_args (args);
13123 for (i = 0; i < len; ++i)
13124 {
13125 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13126 i,
13127 elem_args, complain,
13128 in_decl);
13129 TREE_VEC_ELT (result, i) = t;
13130 if (t == error_mark_node)
13131 {
13132 result = error_mark_node;
13133 break;
13134 }
13135 }
13136
13137 /* Update ARGS to restore the substitution from parameter packs to
13138 their argument packs. */
13139 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13140 {
13141 tree parm = TREE_PURPOSE (pack);
13142
13143 if (TREE_CODE (parm) == PARM_DECL
13144 || VAR_P (parm)
13145 || TREE_CODE (parm) == FIELD_DECL)
13146 register_local_specialization (TREE_TYPE (pack), parm);
13147 else
13148 {
13149 int idx, level;
13150
13151 if (TREE_VALUE (pack) == NULL_TREE)
13152 continue;
13153
13154 template_parm_level_and_index (parm, &level, &idx);
13155
13156 /* Update the corresponding argument. */
13157 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13158 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13159 TREE_TYPE (pack);
13160 else
13161 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13162 }
13163 }
13164
13165 /* If the dependent pack arguments were such that we end up with only a
13166 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13167 if (len == 1 && TREE_CODE (result) == TREE_VEC
13168 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13169 return TREE_VEC_ELT (result, 0);
13170
13171 return result;
13172 }
13173
13174 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13175 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13176 parameter packs; all parms generated from a function parameter pack will
13177 have the same DECL_PARM_INDEX. */
13178
13179 tree
13180 get_pattern_parm (tree parm, tree tmpl)
13181 {
13182 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13183 tree patparm;
13184
13185 if (DECL_ARTIFICIAL (parm))
13186 {
13187 for (patparm = DECL_ARGUMENTS (pattern);
13188 patparm; patparm = DECL_CHAIN (patparm))
13189 if (DECL_ARTIFICIAL (patparm)
13190 && DECL_NAME (parm) == DECL_NAME (patparm))
13191 break;
13192 }
13193 else
13194 {
13195 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13196 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13197 gcc_assert (DECL_PARM_INDEX (patparm)
13198 == DECL_PARM_INDEX (parm));
13199 }
13200
13201 return patparm;
13202 }
13203
13204 /* Make an argument pack out of the TREE_VEC VEC. */
13205
13206 static tree
13207 make_argument_pack (tree vec)
13208 {
13209 tree pack;
13210
13211 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13212 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13213 else
13214 {
13215 pack = make_node (NONTYPE_ARGUMENT_PACK);
13216 TREE_CONSTANT (pack) = 1;
13217 }
13218 SET_ARGUMENT_PACK_ARGS (pack, vec);
13219 return pack;
13220 }
13221
13222 /* Return an exact copy of template args T that can be modified
13223 independently. */
13224
13225 static tree
13226 copy_template_args (tree t)
13227 {
13228 if (t == error_mark_node)
13229 return t;
13230
13231 int len = TREE_VEC_LENGTH (t);
13232 tree new_vec = make_tree_vec (len);
13233
13234 for (int i = 0; i < len; ++i)
13235 {
13236 tree elt = TREE_VEC_ELT (t, i);
13237 if (elt && TREE_CODE (elt) == TREE_VEC)
13238 elt = copy_template_args (elt);
13239 TREE_VEC_ELT (new_vec, i) = elt;
13240 }
13241
13242 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13243 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13244
13245 return new_vec;
13246 }
13247
13248 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13249
13250 tree
13251 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13252 tree in_decl)
13253 {
13254 /* Substitute into each of the arguments. */
13255 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13256 args, complain, in_decl);
13257 tree new_arg = error_mark_node;
13258 if (pack_args != error_mark_node)
13259 {
13260 if (TYPE_P (orig_arg))
13261 {
13262 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13263 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13264 }
13265 else
13266 {
13267 new_arg = make_node (TREE_CODE (orig_arg));
13268 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13269 }
13270
13271 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13272 }
13273
13274 return new_arg;
13275 }
13276
13277 /* Substitute ARGS into the vector or list of template arguments T. */
13278
13279 tree
13280 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13281 {
13282 tree orig_t = t;
13283 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13284 tree *elts;
13285
13286 if (t == error_mark_node)
13287 return error_mark_node;
13288
13289 len = TREE_VEC_LENGTH (t);
13290 elts = XALLOCAVEC (tree, len);
13291
13292 for (i = 0; i < len; i++)
13293 {
13294 tree orig_arg = TREE_VEC_ELT (t, i);
13295 tree new_arg;
13296
13297 if (TREE_CODE (orig_arg) == TREE_VEC)
13298 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13299 else if (PACK_EXPANSION_P (orig_arg))
13300 {
13301 /* Substitute into an expansion expression. */
13302 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13303
13304 if (TREE_CODE (new_arg) == TREE_VEC)
13305 /* Add to the expanded length adjustment the number of
13306 expanded arguments. We subtract one from this
13307 measurement, because the argument pack expression
13308 itself is already counted as 1 in
13309 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13310 the argument pack is empty. */
13311 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13312 }
13313 else if (ARGUMENT_PACK_P (orig_arg))
13314 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13315 else
13316 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13317
13318 if (new_arg == error_mark_node)
13319 return error_mark_node;
13320
13321 elts[i] = new_arg;
13322 if (new_arg != orig_arg)
13323 need_new = 1;
13324 }
13325
13326 if (!need_new)
13327 return t;
13328
13329 /* Make space for the expanded arguments coming from template
13330 argument packs. */
13331 t = make_tree_vec (len + expanded_len_adjust);
13332 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13333 arguments for a member template.
13334 In that case each TREE_VEC in ORIG_T represents a level of template
13335 arguments, and ORIG_T won't carry any non defaulted argument count.
13336 It will rather be the nested TREE_VECs that will carry one.
13337 In other words, ORIG_T carries a non defaulted argument count only
13338 if it doesn't contain any nested TREE_VEC. */
13339 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13340 {
13341 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13342 count += expanded_len_adjust;
13343 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13344 }
13345 for (i = 0, out = 0; i < len; i++)
13346 {
13347 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13348 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13349 && TREE_CODE (elts[i]) == TREE_VEC)
13350 {
13351 int idx;
13352
13353 /* Now expand the template argument pack "in place". */
13354 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13355 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13356 }
13357 else
13358 {
13359 TREE_VEC_ELT (t, out) = elts[i];
13360 out++;
13361 }
13362 }
13363
13364 return t;
13365 }
13366
13367 /* Substitute ARGS into one level PARMS of template parameters. */
13368
13369 static tree
13370 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13371 {
13372 if (parms == error_mark_node)
13373 return error_mark_node;
13374
13375 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13376
13377 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13378 {
13379 tree tuple = TREE_VEC_ELT (parms, i);
13380
13381 if (tuple == error_mark_node)
13382 continue;
13383
13384 TREE_VEC_ELT (new_vec, i) =
13385 tsubst_template_parm (tuple, args, complain);
13386 }
13387
13388 return new_vec;
13389 }
13390
13391 /* Return the result of substituting ARGS into the template parameters
13392 given by PARMS. If there are m levels of ARGS and m + n levels of
13393 PARMS, then the result will contain n levels of PARMS. For
13394 example, if PARMS is `template <class T> template <class U>
13395 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13396 result will be `template <int*, double, class V>'. */
13397
13398 static tree
13399 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13400 {
13401 tree r = NULL_TREE;
13402 tree* new_parms;
13403
13404 /* When substituting into a template, we must set
13405 PROCESSING_TEMPLATE_DECL as the template parameters may be
13406 dependent if they are based on one-another, and the dependency
13407 predicates are short-circuit outside of templates. */
13408 ++processing_template_decl;
13409
13410 for (new_parms = &r;
13411 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13412 new_parms = &(TREE_CHAIN (*new_parms)),
13413 parms = TREE_CHAIN (parms))
13414 {
13415 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13416 args, complain);
13417 *new_parms =
13418 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13419 - TMPL_ARGS_DEPTH (args)),
13420 new_vec, NULL_TREE);
13421 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13422 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13423 }
13424
13425 --processing_template_decl;
13426
13427 return r;
13428 }
13429
13430 /* Return the result of substituting ARGS into one template parameter
13431 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13432 parameter and which TREE_PURPOSE is the default argument of the
13433 template parameter. */
13434
13435 static tree
13436 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13437 {
13438 tree default_value, parm_decl;
13439
13440 if (args == NULL_TREE
13441 || t == NULL_TREE
13442 || t == error_mark_node)
13443 return t;
13444
13445 gcc_assert (TREE_CODE (t) == TREE_LIST);
13446
13447 default_value = TREE_PURPOSE (t);
13448 parm_decl = TREE_VALUE (t);
13449 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13450
13451 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13452 if (TREE_CODE (parm_decl) == PARM_DECL
13453 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13454 parm_decl = error_mark_node;
13455 default_value = tsubst_template_arg (default_value, args,
13456 complain, NULL_TREE);
13457 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13458
13459 tree r = build_tree_list (default_value, parm_decl);
13460 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13461 return r;
13462 }
13463
13464 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13465 type T. If T is not an aggregate or enumeration type, it is
13466 handled as if by tsubst. IN_DECL is as for tsubst. If
13467 ENTERING_SCOPE is nonzero, T is the context for a template which
13468 we are presently tsubst'ing. Return the substituted value. */
13469
13470 static tree
13471 tsubst_aggr_type (tree t,
13472 tree args,
13473 tsubst_flags_t complain,
13474 tree in_decl,
13475 int entering_scope)
13476 {
13477 if (t == NULL_TREE)
13478 return NULL_TREE;
13479
13480 switch (TREE_CODE (t))
13481 {
13482 case RECORD_TYPE:
13483 if (TYPE_PTRMEMFUNC_P (t))
13484 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13485
13486 /* Fall through. */
13487 case ENUMERAL_TYPE:
13488 case UNION_TYPE:
13489 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13490 {
13491 tree argvec;
13492 tree context;
13493 tree r;
13494
13495 /* In "sizeof(X<I>)" we need to evaluate "I". */
13496 cp_evaluated ev;
13497
13498 /* First, determine the context for the type we are looking
13499 up. */
13500 context = TYPE_CONTEXT (t);
13501 if (context && TYPE_P (context))
13502 {
13503 context = tsubst_aggr_type (context, args, complain,
13504 in_decl, /*entering_scope=*/1);
13505 /* If context is a nested class inside a class template,
13506 it may still need to be instantiated (c++/33959). */
13507 context = complete_type (context);
13508 }
13509
13510 /* Then, figure out what arguments are appropriate for the
13511 type we are trying to find. For example, given:
13512
13513 template <class T> struct S;
13514 template <class T, class U> void f(T, U) { S<U> su; }
13515
13516 and supposing that we are instantiating f<int, double>,
13517 then our ARGS will be {int, double}, but, when looking up
13518 S we only want {double}. */
13519 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13520 complain, in_decl);
13521 if (argvec == error_mark_node)
13522 r = error_mark_node;
13523 else if (!entering_scope
13524 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13525 {
13526 /* See maybe_dependent_member_ref. */
13527 tree name = TYPE_IDENTIFIER (t);
13528 tree fullname = name;
13529 if (instantiates_primary_template_p (t))
13530 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13531 INNERMOST_TEMPLATE_ARGS (argvec));
13532 return build_typename_type (context, name, fullname,
13533 typename_type);
13534 }
13535 else
13536 {
13537 r = lookup_template_class (t, argvec, in_decl, context,
13538 entering_scope, complain);
13539 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13540 }
13541
13542 return r;
13543 }
13544 else
13545 /* This is not a template type, so there's nothing to do. */
13546 return t;
13547
13548 default:
13549 return tsubst (t, args, complain, in_decl);
13550 }
13551 }
13552
13553 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13554
13555 /* Substitute into the default argument ARG (a default argument for
13556 FN), which has the indicated TYPE. */
13557
13558 tree
13559 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13560 tsubst_flags_t complain)
13561 {
13562 int errs = errorcount + sorrycount;
13563
13564 /* This can happen in invalid code. */
13565 if (TREE_CODE (arg) == DEFERRED_PARSE)
13566 return arg;
13567
13568 /* Shortcut {}. */
13569 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13570 && CONSTRUCTOR_NELTS (arg) == 0)
13571 return arg;
13572
13573 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13574 parm = chain_index (parmnum, parm);
13575 tree parmtype = TREE_TYPE (parm);
13576 if (DECL_BY_REFERENCE (parm))
13577 parmtype = TREE_TYPE (parmtype);
13578 if (parmtype == error_mark_node)
13579 return error_mark_node;
13580
13581 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13582
13583 tree *slot;
13584 if (defarg_inst && (slot = defarg_inst->get (parm)))
13585 return *slot;
13586
13587 /* This default argument came from a template. Instantiate the
13588 default argument here, not in tsubst. In the case of
13589 something like:
13590
13591 template <class T>
13592 struct S {
13593 static T t();
13594 void f(T = t());
13595 };
13596
13597 we must be careful to do name lookup in the scope of S<T>,
13598 rather than in the current class. */
13599 push_to_top_level ();
13600 push_access_scope (fn);
13601 push_deferring_access_checks (dk_no_deferred);
13602 start_lambda_scope (parm);
13603
13604 /* The default argument expression may cause implicitly defined
13605 member functions to be synthesized, which will result in garbage
13606 collection. We must treat this situation as if we were within
13607 the body of function so as to avoid collecting live data on the
13608 stack. */
13609 ++function_depth;
13610 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13611 complain, NULL_TREE,
13612 /*integral_constant_expression_p=*/false);
13613 --function_depth;
13614
13615 finish_lambda_scope ();
13616
13617 /* Make sure the default argument is reasonable. */
13618 arg = check_default_argument (type, arg, complain);
13619
13620 if (errorcount+sorrycount > errs
13621 && (complain & tf_warning_or_error))
13622 inform (input_location,
13623 " when instantiating default argument for call to %qD", fn);
13624
13625 pop_deferring_access_checks ();
13626 pop_access_scope (fn);
13627 pop_from_top_level ();
13628
13629 if (arg != error_mark_node && !cp_unevaluated_operand)
13630 {
13631 if (!defarg_inst)
13632 defarg_inst = decl_tree_cache_map::create_ggc (37);
13633 defarg_inst->put (parm, arg);
13634 }
13635
13636 return arg;
13637 }
13638
13639 /* Substitute into all the default arguments for FN. */
13640
13641 static void
13642 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13643 {
13644 tree arg;
13645 tree tmpl_args;
13646
13647 tmpl_args = DECL_TI_ARGS (fn);
13648
13649 /* If this function is not yet instantiated, we certainly don't need
13650 its default arguments. */
13651 if (uses_template_parms (tmpl_args))
13652 return;
13653 /* Don't do this again for clones. */
13654 if (DECL_CLONED_FUNCTION_P (fn))
13655 return;
13656
13657 int i = 0;
13658 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13659 arg;
13660 arg = TREE_CHAIN (arg), ++i)
13661 if (TREE_PURPOSE (arg))
13662 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13663 TREE_VALUE (arg),
13664 TREE_PURPOSE (arg),
13665 complain);
13666 }
13667
13668 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13669 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13670
13671 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13672
13673 void
13674 store_explicit_specifier (tree v, tree t)
13675 {
13676 if (!explicit_specifier_map)
13677 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13678 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13679 explicit_specifier_map->put (v, t);
13680 }
13681
13682 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13683
13684 static tree
13685 lookup_explicit_specifier (tree v)
13686 {
13687 return *explicit_specifier_map->get (v);
13688 }
13689
13690 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13691 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13692 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13693 identical to T. */
13694
13695 static tree
13696 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13697 tree raises, tsubst_flags_t complain)
13698 {
13699 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13700
13701 tree new_type;
13702 if (TREE_CODE (t) == FUNCTION_TYPE)
13703 {
13704 new_type = build_function_type (return_type, arg_types);
13705 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13706 }
13707 else
13708 {
13709 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13710 /* Don't pick up extra function qualifiers from the basetype. */
13711 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13712 if (! MAYBE_CLASS_TYPE_P (r))
13713 {
13714 /* [temp.deduct]
13715
13716 Type deduction may fail for any of the following
13717 reasons:
13718
13719 -- Attempting to create "pointer to member of T" when T
13720 is not a class type. */
13721 if (complain & tf_error)
13722 error ("creating pointer to member function of non-class type %qT",
13723 r);
13724 return error_mark_node;
13725 }
13726
13727 new_type = build_method_type_directly (r, return_type,
13728 TREE_CHAIN (arg_types));
13729 }
13730 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13731
13732 cp_ref_qualifier rqual = type_memfn_rqual (t);
13733 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13734 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13735 }
13736
13737 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13738 each of its formal parameters. If there is a disagreement then rebuild
13739 DECL's function type according to its formal parameter types, as part of a
13740 resolution for Core issues 1001/1322. */
13741
13742 static void
13743 maybe_rebuild_function_decl_type (tree decl)
13744 {
13745 bool function_type_needs_rebuilding = false;
13746 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13747 {
13748 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13749 while (parm_type_list && parm_type_list != void_list_node)
13750 {
13751 tree parm_type = TREE_VALUE (parm_type_list);
13752 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13753 if (!same_type_p (parm_type, formal_parm_type_unqual))
13754 {
13755 function_type_needs_rebuilding = true;
13756 break;
13757 }
13758
13759 parm_list = DECL_CHAIN (parm_list);
13760 parm_type_list = TREE_CHAIN (parm_type_list);
13761 }
13762 }
13763
13764 if (!function_type_needs_rebuilding)
13765 return;
13766
13767 const tree fntype = TREE_TYPE (decl);
13768 tree parm_list = DECL_ARGUMENTS (decl);
13769 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13770 tree new_parm_type_list = NULL_TREE;
13771 tree *q = &new_parm_type_list;
13772 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13773 {
13774 *q = copy_node (old_parm_type_list);
13775 parm_list = DECL_CHAIN (parm_list);
13776 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13777 q = &TREE_CHAIN (*q);
13778 }
13779 while (old_parm_type_list && old_parm_type_list != void_list_node)
13780 {
13781 *q = copy_node (old_parm_type_list);
13782 tree *new_parm_type = &TREE_VALUE (*q);
13783 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13784 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13785 *new_parm_type = formal_parm_type_unqual;
13786
13787 parm_list = DECL_CHAIN (parm_list);
13788 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13789 q = &TREE_CHAIN (*q);
13790 }
13791 if (old_parm_type_list == void_list_node)
13792 *q = void_list_node;
13793
13794 TREE_TYPE (decl)
13795 = rebuild_function_or_method_type (fntype,
13796 TREE_TYPE (fntype), new_parm_type_list,
13797 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13798 }
13799
13800 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13801
13802 static tree
13803 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13804 tree lambda_fntype)
13805 {
13806 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13807 hashval_t hash = 0;
13808 tree in_decl = t;
13809
13810 /* Nobody should be tsubst'ing into non-template functions. */
13811 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13812 || DECL_LOCAL_DECL_P (t));
13813
13814 if (DECL_LOCAL_DECL_P (t))
13815 {
13816 if (tree spec = retrieve_local_specialization (t))
13817 return spec;
13818 }
13819 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13820 {
13821 /* If T is not dependent, just return it. */
13822 if (!uses_template_parms (DECL_TI_ARGS (t))
13823 && !LAMBDA_FUNCTION_P (t))
13824 return t;
13825
13826 /* Calculate the most general template of which R is a
13827 specialization. */
13828 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13829
13830 /* We're substituting a lambda function under tsubst_lambda_expr but not
13831 directly from it; find the matching function we're already inside.
13832 But don't do this if T is a generic lambda with a single level of
13833 template parms, as in that case we're doing a normal instantiation. */
13834 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13835 && (!generic_lambda_fn_p (t)
13836 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13837 return enclosing_instantiation_of (t);
13838
13839 /* Calculate the complete set of arguments used to
13840 specialize R. */
13841 argvec = tsubst_template_args (DECL_TI_ARGS
13842 (DECL_TEMPLATE_RESULT
13843 (DECL_TI_TEMPLATE (t))),
13844 args, complain, in_decl);
13845 if (argvec == error_mark_node)
13846 return error_mark_node;
13847
13848 /* Check to see if we already have this specialization. */
13849 if (!lambda_fntype)
13850 {
13851 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13852 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13853 return spec;
13854 }
13855
13856 /* We can see more levels of arguments than parameters if
13857 there was a specialization of a member template, like
13858 this:
13859
13860 template <class T> struct S { template <class U> void f(); }
13861 template <> template <class U> void S<int>::f(U);
13862
13863 Here, we'll be substituting into the specialization,
13864 because that's where we can find the code we actually
13865 want to generate, but we'll have enough arguments for
13866 the most general template.
13867
13868 We also deal with the peculiar case:
13869
13870 template <class T> struct S {
13871 template <class U> friend void f();
13872 };
13873 template <class U> void f() {}
13874 template S<int>;
13875 template void f<double>();
13876
13877 Here, the ARGS for the instantiation of will be {int,
13878 double}. But, we only need as many ARGS as there are
13879 levels of template parameters in CODE_PATTERN. We are
13880 careful not to get fooled into reducing the ARGS in
13881 situations like:
13882
13883 template <class T> struct S { template <class U> void f(U); }
13884 template <class T> template <> void S<T>::f(int) {}
13885
13886 which we can spot because the pattern will be a
13887 specialization in this case. */
13888 int args_depth = TMPL_ARGS_DEPTH (args);
13889 int parms_depth =
13890 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13891
13892 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13893 args = get_innermost_template_args (args, parms_depth);
13894 }
13895 else
13896 {
13897 /* This special case arises when we have something like this:
13898
13899 template <class T> struct S {
13900 friend void f<int>(int, double);
13901 };
13902
13903 Here, the DECL_TI_TEMPLATE for the friend declaration
13904 will be an IDENTIFIER_NODE. We are being called from
13905 tsubst_friend_function, and we want only to create a
13906 new decl (R) with appropriate types so that we can call
13907 determine_specialization. */
13908 gen_tmpl = NULL_TREE;
13909 argvec = NULL_TREE;
13910 }
13911
13912 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13913 : NULL_TREE);
13914 tree ctx = closure ? closure : DECL_CONTEXT (t);
13915 bool member = ctx && TYPE_P (ctx);
13916
13917 if (member && !closure)
13918 ctx = tsubst_aggr_type (ctx, args,
13919 complain, t, /*entering_scope=*/1);
13920
13921 tree type = (lambda_fntype ? lambda_fntype
13922 : tsubst (TREE_TYPE (t), args,
13923 complain | tf_fndecl_type, in_decl));
13924 if (type == error_mark_node)
13925 return error_mark_node;
13926
13927 /* If we hit excessive deduction depth, the type is bogus even if
13928 it isn't error_mark_node, so don't build a decl. */
13929 if (excessive_deduction_depth)
13930 return error_mark_node;
13931
13932 /* We do NOT check for matching decls pushed separately at this
13933 point, as they may not represent instantiations of this
13934 template, and in any case are considered separate under the
13935 discrete model. */
13936 tree r = copy_decl (t);
13937 DECL_USE_TEMPLATE (r) = 0;
13938 TREE_TYPE (r) = type;
13939 /* Clear out the mangled name and RTL for the instantiation. */
13940 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13941 SET_DECL_RTL (r, NULL);
13942 /* Leave DECL_INITIAL set on deleted instantiations. */
13943 if (!DECL_DELETED_FN (r))
13944 DECL_INITIAL (r) = NULL_TREE;
13945 DECL_CONTEXT (r) = ctx;
13946 set_instantiating_module (r);
13947
13948 /* Handle explicit(dependent-expr). */
13949 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13950 {
13951 tree spec = lookup_explicit_specifier (t);
13952 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13953 /*function_p=*/false,
13954 /*i_c_e_p=*/true);
13955 spec = build_explicit_specifier (spec, complain);
13956 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13957 }
13958
13959 /* OpenMP UDRs have the only argument a reference to the declared
13960 type. We want to diagnose if the declared type is a reference,
13961 which is invalid, but as references to references are usually
13962 quietly merged, diagnose it here. */
13963 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13964 {
13965 tree argtype
13966 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13967 argtype = tsubst (argtype, args, complain, in_decl);
13968 if (TYPE_REF_P (argtype))
13969 error_at (DECL_SOURCE_LOCATION (t),
13970 "reference type %qT in "
13971 "%<#pragma omp declare reduction%>", argtype);
13972 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13973 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13974 argtype);
13975 }
13976
13977 if (member && DECL_CONV_FN_P (r))
13978 /* Type-conversion operator. Reconstruct the name, in
13979 case it's the name of one of the template's parameters. */
13980 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13981
13982 tree parms = DECL_ARGUMENTS (t);
13983 if (closure)
13984 parms = DECL_CHAIN (parms);
13985 parms = tsubst (parms, args, complain, t);
13986 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13987 DECL_CONTEXT (parm) = r;
13988 if (closure)
13989 {
13990 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13991 DECL_NAME (tparm) = closure_identifier;
13992 DECL_CHAIN (tparm) = parms;
13993 parms = tparm;
13994 }
13995 DECL_ARGUMENTS (r) = parms;
13996 DECL_RESULT (r) = NULL_TREE;
13997
13998 maybe_rebuild_function_decl_type (r);
13999
14000 TREE_STATIC (r) = 0;
14001 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14002 DECL_EXTERNAL (r) = 1;
14003 /* If this is an instantiation of a function with internal
14004 linkage, we already know what object file linkage will be
14005 assigned to the instantiation. */
14006 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14007 DECL_DEFER_OUTPUT (r) = 0;
14008 DECL_CHAIN (r) = NULL_TREE;
14009 DECL_PENDING_INLINE_INFO (r) = 0;
14010 DECL_PENDING_INLINE_P (r) = 0;
14011 DECL_SAVED_TREE (r) = NULL_TREE;
14012 DECL_STRUCT_FUNCTION (r) = NULL;
14013 TREE_USED (r) = 0;
14014 /* We'll re-clone as appropriate in instantiate_template. */
14015 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14016
14017 /* If we aren't complaining now, return on error before we register
14018 the specialization so that we'll complain eventually. */
14019 if ((complain & tf_error) == 0
14020 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14021 && !grok_op_properties (r, /*complain=*/false))
14022 return error_mark_node;
14023
14024 /* Associate the constraints directly with the instantiation. We
14025 don't substitute through the constraints; that's only done when
14026 they are checked. */
14027 if (tree ci = get_constraints (t))
14028 /* Unless we're regenerating a lambda, in which case we'll set the
14029 lambda's constraints in tsubst_lambda_expr. */
14030 if (!lambda_fntype)
14031 set_constraints (r, ci);
14032
14033 if (DECL_FRIEND_CONTEXT (t))
14034 SET_DECL_FRIEND_CONTEXT (r,
14035 tsubst (DECL_FRIEND_CONTEXT (t),
14036 args, complain, in_decl));
14037
14038 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14039 this in the special friend case mentioned above where
14040 GEN_TMPL is NULL. */
14041 if (gen_tmpl && !closure)
14042 {
14043 DECL_TEMPLATE_INFO (r)
14044 = build_template_info (gen_tmpl, argvec);
14045 SET_DECL_IMPLICIT_INSTANTIATION (r);
14046
14047 tree new_r
14048 = register_specialization (r, gen_tmpl, argvec, false, hash);
14049 if (new_r != r)
14050 /* We instantiated this while substituting into
14051 the type earlier (template/friend54.C). */
14052 return new_r;
14053
14054 /* We're not supposed to instantiate default arguments
14055 until they are called, for a template. But, for a
14056 declaration like:
14057
14058 template <class T> void f ()
14059 { extern void g(int i = T()); }
14060
14061 we should do the substitution when the template is
14062 instantiated. We handle the member function case in
14063 instantiate_class_template since the default arguments
14064 might refer to other members of the class. */
14065 if (!member
14066 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14067 && !uses_template_parms (argvec))
14068 tsubst_default_arguments (r, complain);
14069 }
14070 else if (DECL_LOCAL_DECL_P (r))
14071 {
14072 if (!cp_unevaluated_operand)
14073 register_local_specialization (r, t);
14074 }
14075 else
14076 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14077
14078 /* Copy the list of befriending classes. */
14079 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14080 *friends;
14081 friends = &TREE_CHAIN (*friends))
14082 {
14083 *friends = copy_node (*friends);
14084 TREE_VALUE (*friends)
14085 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14086 }
14087
14088 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14089 {
14090 maybe_retrofit_in_chrg (r);
14091 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14092 return error_mark_node;
14093 /* If this is an instantiation of a member template, clone it.
14094 If it isn't, that'll be handled by
14095 clone_constructors_and_destructors. */
14096 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14097 clone_cdtor (r, /*update_methods=*/false);
14098 }
14099 else if ((complain & tf_error) != 0
14100 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14101 && !grok_op_properties (r, /*complain=*/true))
14102 return error_mark_node;
14103
14104 /* Possibly limit visibility based on template args. */
14105 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14106 if (DECL_VISIBILITY_SPECIFIED (t))
14107 {
14108 DECL_VISIBILITY_SPECIFIED (r) = 0;
14109 DECL_ATTRIBUTES (r)
14110 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14111 }
14112 determine_visibility (r);
14113 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14114 && !processing_template_decl)
14115 defaulted_late_check (r);
14116
14117 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14118 args, complain, in_decl);
14119 if (flag_openmp)
14120 if (tree attr = lookup_attribute ("omp declare variant base",
14121 DECL_ATTRIBUTES (r)))
14122 omp_declare_variant_finalize (r, attr);
14123
14124 return r;
14125 }
14126
14127 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14128
14129 static tree
14130 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14131 tree lambda_fntype)
14132 {
14133 /* We can get here when processing a member function template,
14134 member class template, or template template parameter. */
14135 tree decl = DECL_TEMPLATE_RESULT (t);
14136 tree in_decl = t;
14137 tree spec;
14138 tree tmpl_args;
14139 tree full_args;
14140 tree r;
14141 hashval_t hash = 0;
14142
14143 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14144 {
14145 /* Template template parameter is treated here. */
14146 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14147 if (new_type == error_mark_node)
14148 r = error_mark_node;
14149 /* If we get a real template back, return it. This can happen in
14150 the context of most_specialized_partial_spec. */
14151 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14152 r = new_type;
14153 else
14154 /* The new TEMPLATE_DECL was built in
14155 reduce_template_parm_level. */
14156 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14157 return r;
14158 }
14159
14160 if (!lambda_fntype)
14161 {
14162 /* We might already have an instance of this template.
14163 The ARGS are for the surrounding class type, so the
14164 full args contain the tsubst'd args for the context,
14165 plus the innermost args from the template decl. */
14166 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14167 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14168 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14169 /* Because this is a template, the arguments will still be
14170 dependent, even after substitution. If
14171 PROCESSING_TEMPLATE_DECL is not set, the dependency
14172 predicates will short-circuit. */
14173 ++processing_template_decl;
14174 full_args = tsubst_template_args (tmpl_args, args,
14175 complain, in_decl);
14176 --processing_template_decl;
14177 if (full_args == error_mark_node)
14178 return error_mark_node;
14179
14180 /* If this is a default template template argument,
14181 tsubst might not have changed anything. */
14182 if (full_args == tmpl_args)
14183 return t;
14184
14185 hash = hash_tmpl_and_args (t, full_args);
14186 spec = retrieve_specialization (t, full_args, hash);
14187 if (spec != NULL_TREE)
14188 {
14189 if (TYPE_P (spec))
14190 /* Type partial instantiations are stored as the type by
14191 lookup_template_class_1, not here as the template. */
14192 spec = CLASSTYPE_TI_TEMPLATE (spec);
14193 return spec;
14194 }
14195 }
14196
14197 /* Make a new template decl. It will be similar to the
14198 original, but will record the current template arguments.
14199 We also create a new function declaration, which is just
14200 like the old one, but points to this new template, rather
14201 than the old one. */
14202 r = copy_decl (t);
14203 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14204 DECL_CHAIN (r) = NULL_TREE;
14205
14206 // Build new template info linking to the original template decl.
14207 if (!lambda_fntype)
14208 {
14209 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14210 SET_DECL_IMPLICIT_INSTANTIATION (r);
14211 }
14212 else
14213 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14214
14215 /* The template parameters for this new template are all the
14216 template parameters for the old template, except the
14217 outermost level of parameters. */
14218 DECL_TEMPLATE_PARMS (r)
14219 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14220 complain);
14221
14222 bool class_p = false;
14223 tree inner = decl;
14224 ++processing_template_decl;
14225 if (TREE_CODE (inner) == FUNCTION_DECL)
14226 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14227 else
14228 {
14229 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14230 {
14231 class_p = true;
14232 inner = TREE_TYPE (inner);
14233 }
14234 if (class_p)
14235 inner = tsubst_aggr_type (inner, args, complain,
14236 in_decl, /*entering*/1);
14237 else
14238 inner = tsubst (inner, args, complain, in_decl);
14239 }
14240 --processing_template_decl;
14241 if (inner == error_mark_node)
14242 return error_mark_node;
14243
14244 if (class_p)
14245 {
14246 /* For a partial specialization, we need to keep pointing to
14247 the primary template. */
14248 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14249 CLASSTYPE_TI_TEMPLATE (inner) = r;
14250
14251 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14252 inner = TYPE_MAIN_DECL (inner);
14253 }
14254 else if (lambda_fntype)
14255 {
14256 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14257 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14258 }
14259 else
14260 {
14261 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14262 DECL_TI_TEMPLATE (inner) = r;
14263 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14264 }
14265
14266 DECL_TEMPLATE_RESULT (r) = inner;
14267 TREE_TYPE (r) = TREE_TYPE (inner);
14268 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14269
14270 if (modules_p ())
14271 {
14272 /* Propagate module information from the decl. */
14273 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14274 if (DECL_LANG_SPECIFIC (inner))
14275 {
14276 DECL_MODULE_PURVIEW_P (r) = DECL_MODULE_PURVIEW_P (inner);
14277 /* If this is a constrained template, the above tsubst of
14278 inner can find the unconstrained template, which may have
14279 come from an import. This is ok, because we don't
14280 register this instantiation (see below). */
14281 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14282 || (TEMPLATE_PARMS_CONSTRAINTS
14283 (DECL_TEMPLATE_PARMS (t))));
14284 DECL_MODULE_IMPORT_P (r) = false;
14285 }
14286 }
14287
14288 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14289 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14290
14291 if (PRIMARY_TEMPLATE_P (t))
14292 DECL_PRIMARY_TEMPLATE (r) = r;
14293
14294 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14295 /* Record this non-type partial instantiation. */
14296 register_specialization (r, t,
14297 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14298 false, hash);
14299
14300 return r;
14301 }
14302
14303 /* True if FN is the op() for a lambda in an uninstantiated template. */
14304
14305 bool
14306 lambda_fn_in_template_p (tree fn)
14307 {
14308 if (!fn || !LAMBDA_FUNCTION_P (fn))
14309 return false;
14310 tree closure = DECL_CONTEXT (fn);
14311 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14312 }
14313
14314 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14315 which the above is true. */
14316
14317 bool
14318 instantiated_lambda_fn_p (tree fn)
14319 {
14320 if (!fn || !LAMBDA_FUNCTION_P (fn))
14321 return false;
14322 tree closure = DECL_CONTEXT (fn);
14323 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14324 return LAMBDA_EXPR_INSTANTIATED (lam);
14325 }
14326
14327 /* We're instantiating a variable from template function TCTX. Return the
14328 corresponding current enclosing scope. This gets complicated because lambda
14329 functions in templates are regenerated rather than instantiated, but generic
14330 lambda functions are subsequently instantiated. */
14331
14332 static tree
14333 enclosing_instantiation_of (tree otctx)
14334 {
14335 tree tctx = otctx;
14336 tree fn = current_function_decl;
14337 int lambda_count = 0;
14338
14339 for (; tctx && (lambda_fn_in_template_p (tctx)
14340 || instantiated_lambda_fn_p (tctx));
14341 tctx = decl_function_context (tctx))
14342 ++lambda_count;
14343 for (; fn; fn = decl_function_context (fn))
14344 {
14345 tree ofn = fn;
14346 int flambda_count = 0;
14347 for (; fn && instantiated_lambda_fn_p (fn);
14348 fn = decl_function_context (fn))
14349 ++flambda_count;
14350 if ((fn && DECL_TEMPLATE_INFO (fn))
14351 ? most_general_template (fn) != most_general_template (tctx)
14352 : fn != tctx)
14353 continue;
14354 if (flambda_count != lambda_count)
14355 {
14356 gcc_assert (flambda_count > lambda_count);
14357 for (; flambda_count > lambda_count; --flambda_count)
14358 ofn = decl_function_context (ofn);
14359 }
14360 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14361 || DECL_CONV_FN_P (ofn));
14362 return ofn;
14363 }
14364 gcc_unreachable ();
14365 }
14366
14367 /* Substitute the ARGS into the T, which is a _DECL. Return the
14368 result of the substitution. Issue error and warning messages under
14369 control of COMPLAIN. */
14370
14371 static tree
14372 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14373 {
14374 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14375 location_t saved_loc;
14376 tree r = NULL_TREE;
14377 tree in_decl = t;
14378 hashval_t hash = 0;
14379
14380 /* Set the filename and linenumber to improve error-reporting. */
14381 saved_loc = input_location;
14382 input_location = DECL_SOURCE_LOCATION (t);
14383
14384 switch (TREE_CODE (t))
14385 {
14386 case TEMPLATE_DECL:
14387 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14388 break;
14389
14390 case FUNCTION_DECL:
14391 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14392 break;
14393
14394 case PARM_DECL:
14395 {
14396 tree type = NULL_TREE;
14397 int i, len = 1;
14398 tree expanded_types = NULL_TREE;
14399 tree prev_r = NULL_TREE;
14400 tree first_r = NULL_TREE;
14401
14402 if (DECL_PACK_P (t))
14403 {
14404 /* If there is a local specialization that isn't a
14405 parameter pack, it means that we're doing a "simple"
14406 substitution from inside tsubst_pack_expansion. Just
14407 return the local specialization (which will be a single
14408 parm). */
14409 tree spec = retrieve_local_specialization (t);
14410 if (spec
14411 && TREE_CODE (spec) == PARM_DECL
14412 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14413 RETURN (spec);
14414
14415 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14416 the parameters in this function parameter pack. */
14417 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14418 complain, in_decl);
14419 if (TREE_CODE (expanded_types) == TREE_VEC)
14420 {
14421 len = TREE_VEC_LENGTH (expanded_types);
14422
14423 /* Zero-length parameter packs are boring. Just substitute
14424 into the chain. */
14425 if (len == 0 && !cp_unevaluated_operand)
14426 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14427 TREE_CHAIN (t)));
14428 }
14429 else
14430 {
14431 /* All we did was update the type. Make a note of that. */
14432 type = expanded_types;
14433 expanded_types = NULL_TREE;
14434 }
14435 }
14436
14437 /* Loop through all of the parameters we'll build. When T is
14438 a function parameter pack, LEN is the number of expanded
14439 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14440 r = NULL_TREE;
14441 for (i = 0; i < len; ++i)
14442 {
14443 prev_r = r;
14444 r = copy_node (t);
14445 if (DECL_TEMPLATE_PARM_P (t))
14446 SET_DECL_TEMPLATE_PARM_P (r);
14447
14448 if (expanded_types)
14449 /* We're on the Ith parameter of the function parameter
14450 pack. */
14451 {
14452 /* Get the Ith type. */
14453 type = TREE_VEC_ELT (expanded_types, i);
14454
14455 /* Rename the parameter to include the index. */
14456 DECL_NAME (r)
14457 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14458 }
14459 else if (!type)
14460 /* We're dealing with a normal parameter. */
14461 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14462
14463 type = type_decays_to (type);
14464 TREE_TYPE (r) = type;
14465 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14466
14467 if (DECL_INITIAL (r))
14468 {
14469 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14470 DECL_INITIAL (r) = TREE_TYPE (r);
14471 else
14472 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14473 complain, in_decl);
14474 }
14475
14476 DECL_CONTEXT (r) = NULL_TREE;
14477
14478 if (!DECL_TEMPLATE_PARM_P (r))
14479 DECL_ARG_TYPE (r) = type_passed_as (type);
14480
14481 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14482 args, complain, in_decl);
14483
14484 /* Keep track of the first new parameter we
14485 generate. That's what will be returned to the
14486 caller. */
14487 if (!first_r)
14488 first_r = r;
14489
14490 /* Build a proper chain of parameters when substituting
14491 into a function parameter pack. */
14492 if (prev_r)
14493 DECL_CHAIN (prev_r) = r;
14494 }
14495
14496 /* If cp_unevaluated_operand is set, we're just looking for a
14497 single dummy parameter, so don't keep going. */
14498 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14499 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14500 complain, DECL_CHAIN (t));
14501
14502 /* FIRST_R contains the start of the chain we've built. */
14503 r = first_r;
14504 }
14505 break;
14506
14507 case FIELD_DECL:
14508 {
14509 tree type = NULL_TREE;
14510 tree vec = NULL_TREE;
14511 tree expanded_types = NULL_TREE;
14512 int len = 1;
14513
14514 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14515 {
14516 /* This field is a lambda capture pack. Return a TREE_VEC of
14517 the expanded fields to instantiate_class_template_1. */
14518 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14519 complain, in_decl);
14520 if (TREE_CODE (expanded_types) == TREE_VEC)
14521 {
14522 len = TREE_VEC_LENGTH (expanded_types);
14523 vec = make_tree_vec (len);
14524 }
14525 else
14526 {
14527 /* All we did was update the type. Make a note of that. */
14528 type = expanded_types;
14529 expanded_types = NULL_TREE;
14530 }
14531 }
14532
14533 for (int i = 0; i < len; ++i)
14534 {
14535 r = copy_decl (t);
14536 if (expanded_types)
14537 {
14538 type = TREE_VEC_ELT (expanded_types, i);
14539 DECL_NAME (r)
14540 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14541 }
14542 else if (!type)
14543 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14544
14545 if (type == error_mark_node)
14546 RETURN (error_mark_node);
14547 TREE_TYPE (r) = type;
14548 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14549
14550 if (DECL_C_BIT_FIELD (r))
14551 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14552 number of bits. */
14553 DECL_BIT_FIELD_REPRESENTATIVE (r)
14554 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14555 complain, in_decl,
14556 /*integral_constant_expression_p=*/true);
14557 if (DECL_INITIAL (t))
14558 {
14559 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14560 NSDMI in perform_member_init. Still set DECL_INITIAL
14561 so that we know there is one. */
14562 DECL_INITIAL (r) = void_node;
14563 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14564 retrofit_lang_decl (r);
14565 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14566 }
14567 /* We don't have to set DECL_CONTEXT here; it is set by
14568 finish_member_declaration. */
14569 DECL_CHAIN (r) = NULL_TREE;
14570
14571 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14572 args, complain, in_decl);
14573
14574 if (vec)
14575 TREE_VEC_ELT (vec, i) = r;
14576 }
14577
14578 if (vec)
14579 r = vec;
14580 }
14581 break;
14582
14583 case USING_DECL:
14584 /* We reach here only for member using decls. We also need to check
14585 uses_template_parms because DECL_DEPENDENT_P is not set for a
14586 using-declaration that designates a member of the current
14587 instantiation (c++/53549). */
14588 if (DECL_DEPENDENT_P (t)
14589 || uses_template_parms (USING_DECL_SCOPE (t)))
14590 {
14591 tree scope = USING_DECL_SCOPE (t);
14592 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14593 if (PACK_EXPANSION_P (scope))
14594 {
14595 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14596 int len = TREE_VEC_LENGTH (vec);
14597 r = make_tree_vec (len);
14598 for (int i = 0; i < len; ++i)
14599 {
14600 tree escope = TREE_VEC_ELT (vec, i);
14601 tree elt = do_class_using_decl (escope, name);
14602 if (!elt)
14603 {
14604 r = error_mark_node;
14605 break;
14606 }
14607 else
14608 {
14609 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14610 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14611 }
14612 TREE_VEC_ELT (r, i) = elt;
14613 }
14614 }
14615 else
14616 {
14617 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14618 complain, in_decl);
14619 r = do_class_using_decl (inst_scope, name);
14620 if (!r)
14621 r = error_mark_node;
14622 else
14623 {
14624 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14625 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14626 }
14627 }
14628 }
14629 else
14630 {
14631 r = copy_node (t);
14632 DECL_CHAIN (r) = NULL_TREE;
14633 }
14634 break;
14635
14636 case TYPE_DECL:
14637 case VAR_DECL:
14638 {
14639 tree argvec = NULL_TREE;
14640 tree gen_tmpl = NULL_TREE;
14641 tree tmpl = NULL_TREE;
14642 tree type = NULL_TREE;
14643
14644 if (TREE_TYPE (t) == error_mark_node)
14645 RETURN (error_mark_node);
14646
14647 if (TREE_CODE (t) == TYPE_DECL
14648 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14649 {
14650 /* If this is the canonical decl, we don't have to
14651 mess with instantiations, and often we can't (for
14652 typename, template type parms and such). Note that
14653 TYPE_NAME is not correct for the above test if
14654 we've copied the type for a typedef. */
14655 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14656 if (type == error_mark_node)
14657 RETURN (error_mark_node);
14658 r = TYPE_NAME (type);
14659 break;
14660 }
14661
14662 /* Check to see if we already have the specialization we
14663 need. */
14664 tree spec = NULL_TREE;
14665 bool local_p = false;
14666 tree ctx = DECL_CONTEXT (t);
14667 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14668 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14669 {
14670 local_p = false;
14671 if (DECL_CLASS_SCOPE_P (t))
14672 {
14673 ctx = tsubst_aggr_type (ctx, args,
14674 complain,
14675 in_decl, /*entering_scope=*/1);
14676 /* If CTX is unchanged, then T is in fact the
14677 specialization we want. That situation occurs when
14678 referencing a static data member within in its own
14679 class. We can use pointer equality, rather than
14680 same_type_p, because DECL_CONTEXT is always
14681 canonical... */
14682 if (ctx == DECL_CONTEXT (t)
14683 /* ... unless T is a member template; in which
14684 case our caller can be willing to create a
14685 specialization of that template represented
14686 by T. */
14687 && !(DECL_TI_TEMPLATE (t)
14688 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14689 spec = t;
14690 }
14691
14692 if (!spec)
14693 {
14694 tmpl = DECL_TI_TEMPLATE (t);
14695 gen_tmpl = most_general_template (tmpl);
14696 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14697 if (argvec != error_mark_node)
14698 argvec = (coerce_innermost_template_parms
14699 (DECL_TEMPLATE_PARMS (gen_tmpl),
14700 argvec, t, complain,
14701 /*all*/true, /*defarg*/true));
14702 if (argvec == error_mark_node)
14703 RETURN (error_mark_node);
14704 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14705 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14706 }
14707 }
14708 else
14709 {
14710 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14711 /* Subsequent calls to pushdecl will fill this in. */
14712 ctx = NULL_TREE;
14713 /* A local variable. */
14714 local_p = true;
14715 /* Unless this is a reference to a static variable from an
14716 enclosing function, in which case we need to fill it in now. */
14717 if (TREE_STATIC (t))
14718 {
14719 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14720 if (fn != current_function_decl)
14721 ctx = fn;
14722 }
14723 spec = retrieve_local_specialization (t);
14724 }
14725 /* If we already have the specialization we need, there is
14726 nothing more to do. */
14727 if (spec)
14728 {
14729 r = spec;
14730 break;
14731 }
14732
14733 /* Create a new node for the specialization we need. */
14734 if (type == NULL_TREE)
14735 {
14736 if (is_typedef_decl (t))
14737 type = DECL_ORIGINAL_TYPE (t);
14738 else
14739 type = TREE_TYPE (t);
14740 if (VAR_P (t)
14741 && VAR_HAD_UNKNOWN_BOUND (t)
14742 && type != error_mark_node)
14743 type = strip_array_domain (type);
14744 tree sub_args = args;
14745 if (tree auto_node = type_uses_auto (type))
14746 {
14747 /* Mask off any template args past the variable's context so we
14748 don't replace the auto with an unrelated argument. */
14749 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14750 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14751 if (extra > 0)
14752 /* This should never happen with the new lambda instantiation
14753 model, but keep the handling just in case. */
14754 gcc_assert (!CHECKING_P),
14755 sub_args = strip_innermost_template_args (args, extra);
14756 }
14757 type = tsubst (type, sub_args, complain, in_decl);
14758 /* Substituting the type might have recursively instantiated this
14759 same alias (c++/86171). */
14760 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14761 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14762 {
14763 r = spec;
14764 break;
14765 }
14766 }
14767 r = copy_decl (t);
14768 if (VAR_P (r))
14769 {
14770 DECL_INITIALIZED_P (r) = 0;
14771 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14772 if (type == error_mark_node)
14773 RETURN (error_mark_node);
14774 if (TREE_CODE (type) == FUNCTION_TYPE)
14775 {
14776 /* It may seem that this case cannot occur, since:
14777
14778 typedef void f();
14779 void g() { f x; }
14780
14781 declares a function, not a variable. However:
14782
14783 typedef void f();
14784 template <typename T> void g() { T t; }
14785 template void g<f>();
14786
14787 is an attempt to declare a variable with function
14788 type. */
14789 error ("variable %qD has function type",
14790 /* R is not yet sufficiently initialized, so we
14791 just use its name. */
14792 DECL_NAME (r));
14793 RETURN (error_mark_node);
14794 }
14795 type = complete_type (type);
14796 /* Wait until cp_finish_decl to set this again, to handle
14797 circular dependency (template/instantiate6.C). */
14798 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14799 type = check_var_type (DECL_NAME (r), type,
14800 DECL_SOURCE_LOCATION (r));
14801 if (DECL_HAS_VALUE_EXPR_P (t))
14802 {
14803 tree ve = DECL_VALUE_EXPR (t);
14804 /* If the DECL_VALUE_EXPR is converted to the declared type,
14805 preserve the identity so that gimplify_type_sizes works. */
14806 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14807 if (nop)
14808 ve = TREE_OPERAND (ve, 0);
14809 ve = tsubst_expr (ve, args, complain, in_decl,
14810 /*constant_expression_p=*/false);
14811 if (REFERENCE_REF_P (ve))
14812 {
14813 gcc_assert (TYPE_REF_P (type));
14814 ve = TREE_OPERAND (ve, 0);
14815 }
14816 if (nop)
14817 ve = build_nop (type, ve);
14818 else if (DECL_LANG_SPECIFIC (t)
14819 && DECL_OMP_PRIVATIZED_MEMBER (t)
14820 && TREE_CODE (ve) == COMPONENT_REF
14821 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14822 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14823 type = TREE_TYPE (ve);
14824 else
14825 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14826 == TYPE_MAIN_VARIANT (type));
14827 SET_DECL_VALUE_EXPR (r, ve);
14828 }
14829 if (CP_DECL_THREAD_LOCAL_P (r)
14830 && !processing_template_decl)
14831 set_decl_tls_model (r, decl_default_tls_model (r));
14832 }
14833 else if (DECL_SELF_REFERENCE_P (t))
14834 SET_DECL_SELF_REFERENCE_P (r);
14835 TREE_TYPE (r) = type;
14836 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14837 DECL_CONTEXT (r) = ctx;
14838 /* Clear out the mangled name and RTL for the instantiation. */
14839 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14840 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14841 SET_DECL_RTL (r, NULL);
14842 set_instantiating_module (r);
14843
14844 /* The initializer must not be expanded until it is required;
14845 see [temp.inst]. */
14846 DECL_INITIAL (r) = NULL_TREE;
14847 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14848 if (VAR_P (r))
14849 {
14850 if (DECL_LANG_SPECIFIC (r))
14851 SET_DECL_DEPENDENT_INIT_P (r, false);
14852
14853 SET_DECL_MODE (r, VOIDmode);
14854
14855 /* Possibly limit visibility based on template args. */
14856 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14857 if (DECL_VISIBILITY_SPECIFIED (t))
14858 {
14859 DECL_VISIBILITY_SPECIFIED (r) = 0;
14860 DECL_ATTRIBUTES (r)
14861 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14862 }
14863 determine_visibility (r);
14864 }
14865
14866 if (!local_p)
14867 {
14868 /* A static data member declaration is always marked
14869 external when it is declared in-class, even if an
14870 initializer is present. We mimic the non-template
14871 processing here. */
14872 DECL_EXTERNAL (r) = 1;
14873 if (DECL_NAMESPACE_SCOPE_P (t))
14874 DECL_NOT_REALLY_EXTERN (r) = 1;
14875
14876 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14877 SET_DECL_IMPLICIT_INSTANTIATION (r);
14878 if (!error_operand_p (r) || (complain & tf_error))
14879 register_specialization (r, gen_tmpl, argvec, false, hash);
14880 }
14881 else
14882 {
14883 if (DECL_LANG_SPECIFIC (r))
14884 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14885 if (!cp_unevaluated_operand)
14886 register_local_specialization (r, t);
14887 }
14888
14889 DECL_CHAIN (r) = NULL_TREE;
14890
14891 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14892 /*flags=*/0,
14893 args, complain, in_decl);
14894
14895 /* Preserve a typedef that names a type. */
14896 if (is_typedef_decl (r) && type != error_mark_node)
14897 {
14898 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14899 set_underlying_type (r);
14900 if (TYPE_DECL_ALIAS_P (r))
14901 /* An alias template specialization can be dependent
14902 even if its underlying type is not. */
14903 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14904 }
14905
14906 layout_decl (r, 0);
14907 }
14908 break;
14909
14910 default:
14911 gcc_unreachable ();
14912 }
14913 #undef RETURN
14914
14915 out:
14916 /* Restore the file and line information. */
14917 input_location = saved_loc;
14918
14919 return r;
14920 }
14921
14922 /* Substitute into the complete parameter type list PARMS. */
14923
14924 tree
14925 tsubst_function_parms (tree parms,
14926 tree args,
14927 tsubst_flags_t complain,
14928 tree in_decl)
14929 {
14930 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14931 }
14932
14933 /* Substitute into the ARG_TYPES of a function type.
14934 If END is a TREE_CHAIN, leave it and any following types
14935 un-substituted. */
14936
14937 static tree
14938 tsubst_arg_types (tree arg_types,
14939 tree args,
14940 tree end,
14941 tsubst_flags_t complain,
14942 tree in_decl)
14943 {
14944 tree remaining_arg_types;
14945 tree type = NULL_TREE;
14946 int i = 1;
14947 tree expanded_args = NULL_TREE;
14948 tree default_arg;
14949
14950 if (!arg_types || arg_types == void_list_node || arg_types == end)
14951 return arg_types;
14952
14953 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14954 args, end, complain, in_decl);
14955 if (remaining_arg_types == error_mark_node)
14956 return error_mark_node;
14957
14958 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14959 {
14960 /* For a pack expansion, perform substitution on the
14961 entire expression. Later on, we'll handle the arguments
14962 one-by-one. */
14963 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14964 args, complain, in_decl);
14965
14966 if (TREE_CODE (expanded_args) == TREE_VEC)
14967 /* So that we'll spin through the parameters, one by one. */
14968 i = TREE_VEC_LENGTH (expanded_args);
14969 else
14970 {
14971 /* We only partially substituted into the parameter
14972 pack. Our type is TYPE_PACK_EXPANSION. */
14973 type = expanded_args;
14974 expanded_args = NULL_TREE;
14975 }
14976 }
14977
14978 while (i > 0) {
14979 --i;
14980
14981 if (expanded_args)
14982 type = TREE_VEC_ELT (expanded_args, i);
14983 else if (!type)
14984 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14985
14986 if (type == error_mark_node)
14987 return error_mark_node;
14988 if (VOID_TYPE_P (type))
14989 {
14990 if (complain & tf_error)
14991 {
14992 error ("invalid parameter type %qT", type);
14993 if (in_decl)
14994 error ("in declaration %q+D", in_decl);
14995 }
14996 return error_mark_node;
14997 }
14998
14999 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15000 top-level qualifiers as required. */
15001 type = cv_unqualified (type_decays_to (type));
15002
15003 /* We do not substitute into default arguments here. The standard
15004 mandates that they be instantiated only when needed, which is
15005 done in build_over_call. */
15006 default_arg = TREE_PURPOSE (arg_types);
15007
15008 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15009 since the new op() won't have any associated template arguments for us
15010 to refer to later. */
15011 if (lambda_fn_in_template_p (in_decl))
15012 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
15013 false/*fn*/, false/*constexpr*/);
15014
15015 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15016 {
15017 /* We've instantiated a template before its default arguments
15018 have been parsed. This can happen for a nested template
15019 class, and is not an error unless we require the default
15020 argument in a call of this function. */
15021 remaining_arg_types =
15022 tree_cons (default_arg, type, remaining_arg_types);
15023 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15024 remaining_arg_types);
15025 }
15026 else
15027 remaining_arg_types =
15028 hash_tree_cons (default_arg, type, remaining_arg_types);
15029 }
15030
15031 return remaining_arg_types;
15032 }
15033
15034 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15035 *not* handle the exception-specification for FNTYPE, because the
15036 initial substitution of explicitly provided template parameters
15037 during argument deduction forbids substitution into the
15038 exception-specification:
15039
15040 [temp.deduct]
15041
15042 All references in the function type of the function template to the
15043 corresponding template parameters are replaced by the specified tem-
15044 plate argument values. If a substitution in a template parameter or
15045 in the function type of the function template results in an invalid
15046 type, type deduction fails. [Note: The equivalent substitution in
15047 exception specifications is done only when the function is instanti-
15048 ated, at which point a program is ill-formed if the substitution
15049 results in an invalid type.] */
15050
15051 static tree
15052 tsubst_function_type (tree t,
15053 tree args,
15054 tsubst_flags_t complain,
15055 tree in_decl)
15056 {
15057 tree return_type;
15058 tree arg_types = NULL_TREE;
15059
15060 /* The TYPE_CONTEXT is not used for function/method types. */
15061 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15062
15063 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15064 failure. */
15065 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15066
15067 if (late_return_type_p)
15068 {
15069 /* Substitute the argument types. */
15070 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15071 complain, in_decl);
15072 if (arg_types == error_mark_node)
15073 return error_mark_node;
15074
15075 tree save_ccp = current_class_ptr;
15076 tree save_ccr = current_class_ref;
15077 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15078 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15079 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15080 if (do_inject)
15081 {
15082 /* DR 1207: 'this' is in scope in the trailing return type. */
15083 inject_this_parameter (this_type, cp_type_quals (this_type));
15084 }
15085
15086 /* Substitute the return type. */
15087 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15088
15089 if (do_inject)
15090 {
15091 current_class_ptr = save_ccp;
15092 current_class_ref = save_ccr;
15093 }
15094 }
15095 else
15096 /* Substitute the return type. */
15097 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15098
15099 if (return_type == error_mark_node)
15100 return error_mark_node;
15101 /* DR 486 clarifies that creation of a function type with an
15102 invalid return type is a deduction failure. */
15103 if (TREE_CODE (return_type) == ARRAY_TYPE
15104 || TREE_CODE (return_type) == FUNCTION_TYPE)
15105 {
15106 if (complain & tf_error)
15107 {
15108 if (TREE_CODE (return_type) == ARRAY_TYPE)
15109 error ("function returning an array");
15110 else
15111 error ("function returning a function");
15112 }
15113 return error_mark_node;
15114 }
15115
15116 if (!late_return_type_p)
15117 {
15118 /* Substitute the argument types. */
15119 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15120 complain, in_decl);
15121 if (arg_types == error_mark_node)
15122 return error_mark_node;
15123 }
15124
15125 /* Construct a new type node and return it. */
15126 return rebuild_function_or_method_type (t, return_type, arg_types,
15127 /*raises=*/NULL_TREE, complain);
15128 }
15129
15130 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15131 ARGS into that specification, and return the substituted
15132 specification. If there is no specification, return NULL_TREE. */
15133
15134 static tree
15135 tsubst_exception_specification (tree fntype,
15136 tree args,
15137 tsubst_flags_t complain,
15138 tree in_decl,
15139 bool defer_ok)
15140 {
15141 tree specs;
15142 tree new_specs;
15143
15144 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15145 new_specs = NULL_TREE;
15146 if (specs && TREE_PURPOSE (specs))
15147 {
15148 /* A noexcept-specifier. */
15149 tree expr = TREE_PURPOSE (specs);
15150 if (TREE_CODE (expr) == INTEGER_CST)
15151 new_specs = expr;
15152 else if (defer_ok)
15153 {
15154 /* Defer instantiation of noexcept-specifiers to avoid
15155 excessive instantiations (c++/49107). */
15156 new_specs = make_node (DEFERRED_NOEXCEPT);
15157 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15158 {
15159 /* We already partially instantiated this member template,
15160 so combine the new args with the old. */
15161 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15162 = DEFERRED_NOEXCEPT_PATTERN (expr);
15163 DEFERRED_NOEXCEPT_ARGS (new_specs)
15164 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15165 }
15166 else
15167 {
15168 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15169 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15170 }
15171 }
15172 else
15173 {
15174 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15175 {
15176 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15177 args);
15178 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15179 }
15180 new_specs = tsubst_copy_and_build
15181 (expr, args, complain, in_decl, /*function_p=*/false,
15182 /*integral_constant_expression_p=*/true);
15183 }
15184 new_specs = build_noexcept_spec (new_specs, complain);
15185 }
15186 else if (specs)
15187 {
15188 if (! TREE_VALUE (specs))
15189 new_specs = specs;
15190 else
15191 while (specs)
15192 {
15193 tree spec;
15194 int i, len = 1;
15195 tree expanded_specs = NULL_TREE;
15196
15197 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15198 {
15199 /* Expand the pack expansion type. */
15200 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15201 args, complain,
15202 in_decl);
15203
15204 if (expanded_specs == error_mark_node)
15205 return error_mark_node;
15206 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15207 len = TREE_VEC_LENGTH (expanded_specs);
15208 else
15209 {
15210 /* We're substituting into a member template, so
15211 we got a TYPE_PACK_EXPANSION back. Add that
15212 expansion and move on. */
15213 gcc_assert (TREE_CODE (expanded_specs)
15214 == TYPE_PACK_EXPANSION);
15215 new_specs = add_exception_specifier (new_specs,
15216 expanded_specs,
15217 complain);
15218 specs = TREE_CHAIN (specs);
15219 continue;
15220 }
15221 }
15222
15223 for (i = 0; i < len; ++i)
15224 {
15225 if (expanded_specs)
15226 spec = TREE_VEC_ELT (expanded_specs, i);
15227 else
15228 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15229 if (spec == error_mark_node)
15230 return spec;
15231 new_specs = add_exception_specifier (new_specs, spec,
15232 complain);
15233 }
15234
15235 specs = TREE_CHAIN (specs);
15236 }
15237 }
15238 return new_specs;
15239 }
15240
15241 /* Substitute through a TREE_LIST of types or expressions, handling pack
15242 expansions. */
15243
15244 tree
15245 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15246 {
15247 if (t == void_list_node)
15248 return t;
15249
15250 tree purpose = TREE_PURPOSE (t);
15251 tree purposevec = NULL_TREE;
15252 if (!purpose)
15253 ;
15254 else if (PACK_EXPANSION_P (purpose))
15255 {
15256 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15257 if (TREE_CODE (purpose) == TREE_VEC)
15258 purposevec = purpose;
15259 }
15260 else if (TYPE_P (purpose))
15261 purpose = tsubst (purpose, args, complain, in_decl);
15262 else
15263 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15264 if (purpose == error_mark_node || purposevec == error_mark_node)
15265 return error_mark_node;
15266
15267 tree value = TREE_VALUE (t);
15268 tree valuevec = NULL_TREE;
15269 if (!value)
15270 ;
15271 else if (PACK_EXPANSION_P (value))
15272 {
15273 value = tsubst_pack_expansion (value, args, complain, in_decl);
15274 if (TREE_CODE (value) == TREE_VEC)
15275 valuevec = value;
15276 }
15277 else if (TYPE_P (value))
15278 value = tsubst (value, args, complain, in_decl);
15279 else
15280 value = tsubst_copy_and_build (value, args, complain, in_decl);
15281 if (value == error_mark_node || valuevec == error_mark_node)
15282 return error_mark_node;
15283
15284 tree chain = TREE_CHAIN (t);
15285 if (!chain)
15286 ;
15287 else if (TREE_CODE (chain) == TREE_LIST)
15288 chain = tsubst_tree_list (chain, args, complain, in_decl);
15289 else if (TYPE_P (chain))
15290 chain = tsubst (chain, args, complain, in_decl);
15291 else
15292 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15293 if (chain == error_mark_node)
15294 return error_mark_node;
15295
15296 if (purpose == TREE_PURPOSE (t)
15297 && value == TREE_VALUE (t)
15298 && chain == TREE_CHAIN (t))
15299 return t;
15300
15301 int len;
15302 /* Determine the number of arguments. */
15303 if (purposevec)
15304 {
15305 len = TREE_VEC_LENGTH (purposevec);
15306 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15307 }
15308 else if (valuevec)
15309 len = TREE_VEC_LENGTH (valuevec);
15310 else
15311 len = 1;
15312
15313 for (int i = len; i-- > 0; )
15314 {
15315 if (purposevec)
15316 purpose = TREE_VEC_ELT (purposevec, i);
15317 if (valuevec)
15318 value = TREE_VEC_ELT (valuevec, i);
15319
15320 if (value && TYPE_P (value))
15321 chain = hash_tree_cons (purpose, value, chain);
15322 else
15323 chain = tree_cons (purpose, value, chain);
15324 }
15325
15326 return chain;
15327 }
15328
15329 /* Take the tree structure T and replace template parameters used
15330 therein with the argument vector ARGS. IN_DECL is an associated
15331 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15332 Issue error and warning messages under control of COMPLAIN. Note
15333 that we must be relatively non-tolerant of extensions here, in
15334 order to preserve conformance; if we allow substitutions that
15335 should not be allowed, we may allow argument deductions that should
15336 not succeed, and therefore report ambiguous overload situations
15337 where there are none. In theory, we could allow the substitution,
15338 but indicate that it should have failed, and allow our caller to
15339 make sure that the right thing happens, but we don't try to do this
15340 yet.
15341
15342 This function is used for dealing with types, decls and the like;
15343 for expressions, use tsubst_expr or tsubst_copy. */
15344
15345 tree
15346 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15347 {
15348 enum tree_code code;
15349 tree type, r = NULL_TREE;
15350
15351 if (t == NULL_TREE || t == error_mark_node
15352 || t == integer_type_node
15353 || t == void_type_node
15354 || t == char_type_node
15355 || t == unknown_type_node
15356 || TREE_CODE (t) == NAMESPACE_DECL
15357 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15358 return t;
15359
15360 if (DECL_P (t))
15361 return tsubst_decl (t, args, complain);
15362
15363 if (args == NULL_TREE)
15364 return t;
15365
15366 code = TREE_CODE (t);
15367
15368 if (code == IDENTIFIER_NODE)
15369 type = IDENTIFIER_TYPE_VALUE (t);
15370 else
15371 type = TREE_TYPE (t);
15372
15373 gcc_assert (type != unknown_type_node);
15374
15375 /* Reuse typedefs. We need to do this to handle dependent attributes,
15376 such as attribute aligned. */
15377 if (TYPE_P (t)
15378 && typedef_variant_p (t))
15379 {
15380 tree decl = TYPE_NAME (t);
15381
15382 if (alias_template_specialization_p (t, nt_opaque))
15383 {
15384 /* DECL represents an alias template and we want to
15385 instantiate it. */
15386 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15387 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15388 r = instantiate_alias_template (tmpl, gen_args, complain);
15389 }
15390 else if (DECL_CLASS_SCOPE_P (decl)
15391 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15392 && uses_template_parms (DECL_CONTEXT (decl)))
15393 {
15394 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15395 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15396 r = retrieve_specialization (tmpl, gen_args, 0);
15397 }
15398 else if (DECL_FUNCTION_SCOPE_P (decl)
15399 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15400 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15401 r = retrieve_local_specialization (decl);
15402 else
15403 /* The typedef is from a non-template context. */
15404 return t;
15405
15406 if (r)
15407 {
15408 r = TREE_TYPE (r);
15409 r = cp_build_qualified_type_real
15410 (r, cp_type_quals (t) | cp_type_quals (r),
15411 complain | tf_ignore_bad_quals);
15412 return r;
15413 }
15414 else
15415 {
15416 /* We don't have an instantiation yet, so drop the typedef. */
15417 int quals = cp_type_quals (t);
15418 t = DECL_ORIGINAL_TYPE (decl);
15419 t = cp_build_qualified_type_real (t, quals,
15420 complain | tf_ignore_bad_quals);
15421 }
15422 }
15423
15424 bool fndecl_type = (complain & tf_fndecl_type);
15425 complain &= ~tf_fndecl_type;
15426
15427 if (type
15428 && code != TYPENAME_TYPE
15429 && code != TEMPLATE_TYPE_PARM
15430 && code != TEMPLATE_PARM_INDEX
15431 && code != IDENTIFIER_NODE
15432 && code != FUNCTION_TYPE
15433 && code != METHOD_TYPE)
15434 type = tsubst (type, args, complain, in_decl);
15435 if (type == error_mark_node)
15436 return error_mark_node;
15437
15438 switch (code)
15439 {
15440 case RECORD_TYPE:
15441 case UNION_TYPE:
15442 case ENUMERAL_TYPE:
15443 return tsubst_aggr_type (t, args, complain, in_decl,
15444 /*entering_scope=*/0);
15445
15446 case ERROR_MARK:
15447 case IDENTIFIER_NODE:
15448 case VOID_TYPE:
15449 case OPAQUE_TYPE:
15450 case REAL_TYPE:
15451 case COMPLEX_TYPE:
15452 case VECTOR_TYPE:
15453 case BOOLEAN_TYPE:
15454 case NULLPTR_TYPE:
15455 case LANG_TYPE:
15456 return t;
15457
15458 case INTEGER_TYPE:
15459 if (t == integer_type_node)
15460 return t;
15461
15462 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15463 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15464 return t;
15465
15466 {
15467 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15468
15469 max = tsubst_expr (omax, args, complain, in_decl,
15470 /*integral_constant_expression_p=*/false);
15471
15472 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15473 needed. */
15474 if (TREE_CODE (max) == NOP_EXPR
15475 && TREE_SIDE_EFFECTS (omax)
15476 && !TREE_TYPE (max))
15477 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15478
15479 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15480 with TREE_SIDE_EFFECTS that indicates this is not an integral
15481 constant expression. */
15482 if (processing_template_decl
15483 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15484 {
15485 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15486 TREE_SIDE_EFFECTS (max) = 1;
15487 }
15488
15489 return compute_array_index_type (NULL_TREE, max, complain);
15490 }
15491
15492 case TEMPLATE_TYPE_PARM:
15493 case TEMPLATE_TEMPLATE_PARM:
15494 case BOUND_TEMPLATE_TEMPLATE_PARM:
15495 case TEMPLATE_PARM_INDEX:
15496 {
15497 int idx;
15498 int level;
15499 int levels;
15500 tree arg = NULL_TREE;
15501
15502 r = NULL_TREE;
15503
15504 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15505 template_parm_level_and_index (t, &level, &idx);
15506
15507 levels = TMPL_ARGS_DEPTH (args);
15508 if (level <= levels
15509 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15510 {
15511 arg = TMPL_ARG (args, level, idx);
15512
15513 /* See through ARGUMENT_PACK_SELECT arguments. */
15514 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15515 arg = argument_pack_select_arg (arg);
15516 }
15517
15518 if (arg == error_mark_node)
15519 return error_mark_node;
15520 else if (arg != NULL_TREE)
15521 {
15522 if (ARGUMENT_PACK_P (arg))
15523 /* If ARG is an argument pack, we don't actually want to
15524 perform a substitution here, because substitutions
15525 for argument packs are only done
15526 element-by-element. We can get to this point when
15527 substituting the type of a non-type template
15528 parameter pack, when that type actually contains
15529 template parameter packs from an outer template, e.g.,
15530
15531 template<typename... Types> struct A {
15532 template<Types... Values> struct B { };
15533 }; */
15534 return t;
15535
15536 if (code == TEMPLATE_TYPE_PARM)
15537 {
15538 int quals;
15539
15540 /* When building concept checks for the purpose of
15541 deducing placeholders, we can end up with wildcards
15542 where types are expected. Adjust this to the deduced
15543 value. */
15544 if (TREE_CODE (arg) == WILDCARD_DECL)
15545 arg = TREE_TYPE (TREE_TYPE (arg));
15546
15547 gcc_assert (TYPE_P (arg));
15548
15549 quals = cp_type_quals (arg) | cp_type_quals (t);
15550
15551 return cp_build_qualified_type_real
15552 (arg, quals, complain | tf_ignore_bad_quals);
15553 }
15554 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15555 {
15556 /* We are processing a type constructed from a
15557 template template parameter. */
15558 tree argvec = tsubst (TYPE_TI_ARGS (t),
15559 args, complain, in_decl);
15560 if (argvec == error_mark_node)
15561 return error_mark_node;
15562
15563 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15564 || TREE_CODE (arg) == TEMPLATE_DECL
15565 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15566
15567 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15568 /* Consider this code:
15569
15570 template <template <class> class Template>
15571 struct Internal {
15572 template <class Arg> using Bind = Template<Arg>;
15573 };
15574
15575 template <template <class> class Template, class Arg>
15576 using Instantiate = Template<Arg>; //#0
15577
15578 template <template <class> class Template,
15579 class Argument>
15580 using Bind =
15581 Instantiate<Internal<Template>::template Bind,
15582 Argument>; //#1
15583
15584 When #1 is parsed, the
15585 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15586 parameter `Template' in #0 matches the
15587 UNBOUND_CLASS_TEMPLATE representing the argument
15588 `Internal<Template>::template Bind'; We then want
15589 to assemble the type `Bind<Argument>' that can't
15590 be fully created right now, because
15591 `Internal<Template>' not being complete, the Bind
15592 template cannot be looked up in that context. So
15593 we need to "store" `Bind<Argument>' for later
15594 when the context of Bind becomes complete. Let's
15595 store that in a TYPENAME_TYPE. */
15596 return make_typename_type (TYPE_CONTEXT (arg),
15597 build_nt (TEMPLATE_ID_EXPR,
15598 TYPE_IDENTIFIER (arg),
15599 argvec),
15600 typename_type,
15601 complain);
15602
15603 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15604 are resolving nested-types in the signature of a
15605 member function templates. Otherwise ARG is a
15606 TEMPLATE_DECL and is the real template to be
15607 instantiated. */
15608 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15609 arg = TYPE_NAME (arg);
15610
15611 r = lookup_template_class (arg,
15612 argvec, in_decl,
15613 DECL_CONTEXT (arg),
15614 /*entering_scope=*/0,
15615 complain);
15616 return cp_build_qualified_type_real
15617 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15618 }
15619 else if (code == TEMPLATE_TEMPLATE_PARM)
15620 return arg;
15621 else
15622 /* TEMPLATE_PARM_INDEX. */
15623 return convert_from_reference (unshare_expr (arg));
15624 }
15625
15626 if (level == 1)
15627 /* This can happen during the attempted tsubst'ing in
15628 unify. This means that we don't yet have any information
15629 about the template parameter in question. */
15630 return t;
15631
15632 /* Early in template argument deduction substitution, we don't
15633 want to reduce the level of 'auto', or it will be confused
15634 with a normal template parm in subsequent deduction.
15635 Similarly, don't reduce the level of template parameters to
15636 avoid mismatches when deducing their types. */
15637 if (complain & tf_partial)
15638 return t;
15639
15640 /* If we get here, we must have been looking at a parm for a
15641 more deeply nested template. Make a new version of this
15642 template parameter, but with a lower level. */
15643 switch (code)
15644 {
15645 case TEMPLATE_TYPE_PARM:
15646 case TEMPLATE_TEMPLATE_PARM:
15647 case BOUND_TEMPLATE_TEMPLATE_PARM:
15648 if (cp_type_quals (t))
15649 {
15650 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15651 r = cp_build_qualified_type_real
15652 (r, cp_type_quals (t),
15653 complain | (code == TEMPLATE_TYPE_PARM
15654 ? tf_ignore_bad_quals : 0));
15655 }
15656 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15657 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15658 && (r = (TEMPLATE_PARM_DESCENDANTS
15659 (TEMPLATE_TYPE_PARM_INDEX (t))))
15660 && (r = TREE_TYPE (r))
15661 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15662 /* Break infinite recursion when substituting the constraints
15663 of a constrained placeholder. */;
15664 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15665 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15666 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15667 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15668 r = TEMPLATE_PARM_DESCENDANTS (arg))
15669 && (TEMPLATE_PARM_LEVEL (r)
15670 == TEMPLATE_PARM_LEVEL (arg) - levels))
15671 /* Cache the simple case of lowering a type parameter. */
15672 r = TREE_TYPE (r);
15673 else
15674 {
15675 r = copy_type (t);
15676 TEMPLATE_TYPE_PARM_INDEX (r)
15677 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15678 r, levels, args, complain);
15679 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15680 TYPE_MAIN_VARIANT (r) = r;
15681 TYPE_POINTER_TO (r) = NULL_TREE;
15682 TYPE_REFERENCE_TO (r) = NULL_TREE;
15683
15684 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15685 {
15686 /* Propagate constraints on placeholders since they are
15687 only instantiated during satisfaction. */
15688 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15689 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15690 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15691 {
15692 pl = tsubst_copy (pl, args, complain, in_decl);
15693 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15694 }
15695 }
15696
15697 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15698 /* We have reduced the level of the template
15699 template parameter, but not the levels of its
15700 template parameters, so canonical_type_parameter
15701 will not be able to find the canonical template
15702 template parameter for this level. Thus, we
15703 require structural equality checking to compare
15704 TEMPLATE_TEMPLATE_PARMs. */
15705 SET_TYPE_STRUCTURAL_EQUALITY (r);
15706 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15707 SET_TYPE_STRUCTURAL_EQUALITY (r);
15708 else
15709 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15710
15711 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15712 {
15713 tree tinfo = TYPE_TEMPLATE_INFO (t);
15714 /* We might need to substitute into the types of non-type
15715 template parameters. */
15716 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15717 complain, in_decl);
15718 if (tmpl == error_mark_node)
15719 return error_mark_node;
15720 tree argvec = tsubst (TI_ARGS (tinfo), args,
15721 complain, in_decl);
15722 if (argvec == error_mark_node)
15723 return error_mark_node;
15724
15725 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15726 = build_template_info (tmpl, argvec);
15727 }
15728 }
15729 break;
15730
15731 case TEMPLATE_PARM_INDEX:
15732 /* OK, now substitute the type of the non-type parameter. We
15733 couldn't do it earlier because it might be an auto parameter,
15734 and we wouldn't need to if we had an argument. */
15735 type = tsubst (type, args, complain, in_decl);
15736 if (type == error_mark_node)
15737 return error_mark_node;
15738 r = reduce_template_parm_level (t, type, levels, args, complain);
15739 break;
15740
15741 default:
15742 gcc_unreachable ();
15743 }
15744
15745 return r;
15746 }
15747
15748 case TREE_LIST:
15749 return tsubst_tree_list (t, args, complain, in_decl);
15750
15751 case TREE_BINFO:
15752 /* We should never be tsubsting a binfo. */
15753 gcc_unreachable ();
15754
15755 case TREE_VEC:
15756 /* A vector of template arguments. */
15757 gcc_assert (!type);
15758 return tsubst_template_args (t, args, complain, in_decl);
15759
15760 case POINTER_TYPE:
15761 case REFERENCE_TYPE:
15762 {
15763 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15764 return t;
15765
15766 /* [temp.deduct]
15767
15768 Type deduction may fail for any of the following
15769 reasons:
15770
15771 -- Attempting to create a pointer to reference type.
15772 -- Attempting to create a reference to a reference type or
15773 a reference to void.
15774
15775 Core issue 106 says that creating a reference to a reference
15776 during instantiation is no longer a cause for failure. We
15777 only enforce this check in strict C++98 mode. */
15778 if ((TYPE_REF_P (type)
15779 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15780 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15781 {
15782 static location_t last_loc;
15783
15784 /* We keep track of the last time we issued this error
15785 message to avoid spewing a ton of messages during a
15786 single bad template instantiation. */
15787 if (complain & tf_error
15788 && last_loc != input_location)
15789 {
15790 if (VOID_TYPE_P (type))
15791 error ("forming reference to void");
15792 else if (code == POINTER_TYPE)
15793 error ("forming pointer to reference type %qT", type);
15794 else
15795 error ("forming reference to reference type %qT", type);
15796 last_loc = input_location;
15797 }
15798
15799 return error_mark_node;
15800 }
15801 else if (TREE_CODE (type) == FUNCTION_TYPE
15802 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15803 || type_memfn_rqual (type) != REF_QUAL_NONE))
15804 {
15805 if (complain & tf_error)
15806 {
15807 if (code == POINTER_TYPE)
15808 error ("forming pointer to qualified function type %qT",
15809 type);
15810 else
15811 error ("forming reference to qualified function type %qT",
15812 type);
15813 }
15814 return error_mark_node;
15815 }
15816 else if (code == POINTER_TYPE)
15817 {
15818 r = build_pointer_type (type);
15819 if (TREE_CODE (type) == METHOD_TYPE)
15820 r = build_ptrmemfunc_type (r);
15821 }
15822 else if (TYPE_REF_P (type))
15823 /* In C++0x, during template argument substitution, when there is an
15824 attempt to create a reference to a reference type, reference
15825 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15826
15827 "If a template-argument for a template-parameter T names a type
15828 that is a reference to a type A, an attempt to create the type
15829 'lvalue reference to cv T' creates the type 'lvalue reference to
15830 A,' while an attempt to create the type type rvalue reference to
15831 cv T' creates the type T"
15832 */
15833 r = cp_build_reference_type
15834 (TREE_TYPE (type),
15835 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15836 else
15837 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15838 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15839
15840 if (r != error_mark_node)
15841 /* Will this ever be needed for TYPE_..._TO values? */
15842 layout_type (r);
15843
15844 return r;
15845 }
15846 case OFFSET_TYPE:
15847 {
15848 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15849 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15850 {
15851 /* [temp.deduct]
15852
15853 Type deduction may fail for any of the following
15854 reasons:
15855
15856 -- Attempting to create "pointer to member of T" when T
15857 is not a class type. */
15858 if (complain & tf_error)
15859 error ("creating pointer to member of non-class type %qT", r);
15860 return error_mark_node;
15861 }
15862 if (TYPE_REF_P (type))
15863 {
15864 if (complain & tf_error)
15865 error ("creating pointer to member reference type %qT", type);
15866 return error_mark_node;
15867 }
15868 if (VOID_TYPE_P (type))
15869 {
15870 if (complain & tf_error)
15871 error ("creating pointer to member of type void");
15872 return error_mark_node;
15873 }
15874 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15875 if (TREE_CODE (type) == FUNCTION_TYPE)
15876 {
15877 /* The type of the implicit object parameter gets its
15878 cv-qualifiers from the FUNCTION_TYPE. */
15879 tree memptr;
15880 tree method_type
15881 = build_memfn_type (type, r, type_memfn_quals (type),
15882 type_memfn_rqual (type));
15883 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15884 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15885 complain);
15886 }
15887 else
15888 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15889 cp_type_quals (t),
15890 complain);
15891 }
15892 case FUNCTION_TYPE:
15893 case METHOD_TYPE:
15894 {
15895 tree fntype;
15896 tree specs;
15897 fntype = tsubst_function_type (t, args, complain, in_decl);
15898 if (fntype == error_mark_node)
15899 return error_mark_node;
15900
15901 /* Substitute the exception specification. */
15902 specs = tsubst_exception_specification (t, args, complain, in_decl,
15903 /*defer_ok*/fndecl_type);
15904 if (specs == error_mark_node)
15905 return error_mark_node;
15906 if (specs)
15907 fntype = build_exception_variant (fntype, specs);
15908 return fntype;
15909 }
15910 case ARRAY_TYPE:
15911 {
15912 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15913 if (domain == error_mark_node)
15914 return error_mark_node;
15915
15916 /* As an optimization, we avoid regenerating the array type if
15917 it will obviously be the same as T. */
15918 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15919 return t;
15920
15921 /* These checks should match the ones in create_array_type_for_decl.
15922
15923 [temp.deduct]
15924
15925 The deduction may fail for any of the following reasons:
15926
15927 -- Attempting to create an array with an element type that
15928 is void, a function type, or a reference type, or [DR337]
15929 an abstract class type. */
15930 if (VOID_TYPE_P (type)
15931 || TREE_CODE (type) == FUNCTION_TYPE
15932 || (TREE_CODE (type) == ARRAY_TYPE
15933 && TYPE_DOMAIN (type) == NULL_TREE)
15934 || TYPE_REF_P (type))
15935 {
15936 if (complain & tf_error)
15937 error ("creating array of %qT", type);
15938 return error_mark_node;
15939 }
15940
15941 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
15942 !(complain & tf_error)))
15943 return error_mark_node;
15944
15945 r = build_cplus_array_type (type, domain);
15946
15947 if (!valid_array_size_p (input_location, r, in_decl,
15948 (complain & tf_error)))
15949 return error_mark_node;
15950
15951 if (TYPE_USER_ALIGN (t))
15952 {
15953 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15954 TYPE_USER_ALIGN (r) = 1;
15955 }
15956
15957 return r;
15958 }
15959
15960 case TYPENAME_TYPE:
15961 {
15962 tree ctx = TYPE_CONTEXT (t);
15963 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15964 {
15965 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15966 if (ctx == error_mark_node
15967 || TREE_VEC_LENGTH (ctx) > 1)
15968 return error_mark_node;
15969 if (TREE_VEC_LENGTH (ctx) == 0)
15970 {
15971 if (complain & tf_error)
15972 error ("%qD is instantiated for an empty pack",
15973 TYPENAME_TYPE_FULLNAME (t));
15974 return error_mark_node;
15975 }
15976 ctx = TREE_VEC_ELT (ctx, 0);
15977 }
15978 else
15979 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15980 /*entering_scope=*/1);
15981 if (ctx == error_mark_node)
15982 return error_mark_node;
15983
15984 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15985 complain, in_decl);
15986 if (f == error_mark_node)
15987 return error_mark_node;
15988
15989 if (!MAYBE_CLASS_TYPE_P (ctx))
15990 {
15991 if (complain & tf_error)
15992 error ("%qT is not a class, struct, or union type", ctx);
15993 return error_mark_node;
15994 }
15995 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15996 {
15997 /* Normally, make_typename_type does not require that the CTX
15998 have complete type in order to allow things like:
15999
16000 template <class T> struct S { typename S<T>::X Y; };
16001
16002 But, such constructs have already been resolved by this
16003 point, so here CTX really should have complete type, unless
16004 it's a partial instantiation. */
16005 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16006 return error_mark_node;
16007 }
16008
16009 f = make_typename_type (ctx, f, typename_type,
16010 complain | tf_keep_type_decl);
16011 if (f == error_mark_node)
16012 return f;
16013 if (TREE_CODE (f) == TYPE_DECL)
16014 {
16015 complain |= tf_ignore_bad_quals;
16016 f = TREE_TYPE (f);
16017 }
16018
16019 if (TREE_CODE (f) != TYPENAME_TYPE)
16020 {
16021 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16022 {
16023 if (complain & tf_error)
16024 error ("%qT resolves to %qT, which is not an enumeration type",
16025 t, f);
16026 else
16027 return error_mark_node;
16028 }
16029 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16030 {
16031 if (complain & tf_error)
16032 error ("%qT resolves to %qT, which is not a class type",
16033 t, f);
16034 else
16035 return error_mark_node;
16036 }
16037 }
16038
16039 return cp_build_qualified_type_real
16040 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16041 }
16042
16043 case UNBOUND_CLASS_TEMPLATE:
16044 {
16045 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16046 in_decl, /*entering_scope=*/1);
16047 tree name = TYPE_IDENTIFIER (t);
16048 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16049
16050 if (ctx == error_mark_node || name == error_mark_node)
16051 return error_mark_node;
16052
16053 if (parm_list)
16054 parm_list = tsubst_template_parms (parm_list, args, complain);
16055 return make_unbound_class_template (ctx, name, parm_list, complain);
16056 }
16057
16058 case TYPEOF_TYPE:
16059 {
16060 tree type;
16061
16062 ++cp_unevaluated_operand;
16063 ++c_inhibit_evaluation_warnings;
16064
16065 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16066 complain, in_decl,
16067 /*integral_constant_expression_p=*/false);
16068
16069 --cp_unevaluated_operand;
16070 --c_inhibit_evaluation_warnings;
16071
16072 type = finish_typeof (type);
16073 return cp_build_qualified_type_real (type,
16074 cp_type_quals (t)
16075 | cp_type_quals (type),
16076 complain);
16077 }
16078
16079 case DECLTYPE_TYPE:
16080 {
16081 tree type;
16082
16083 ++cp_unevaluated_operand;
16084 ++c_inhibit_evaluation_warnings;
16085
16086 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16087 complain|tf_decltype, in_decl,
16088 /*function_p*/false,
16089 /*integral_constant_expression*/false);
16090
16091 --cp_unevaluated_operand;
16092 --c_inhibit_evaluation_warnings;
16093
16094 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16095 type = lambda_capture_field_type (type,
16096 false /*explicit_init*/,
16097 DECLTYPE_FOR_REF_CAPTURE (t));
16098 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16099 type = lambda_proxy_type (type);
16100 else
16101 {
16102 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16103 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16104 && EXPR_P (type))
16105 /* In a template ~id could be either a complement expression
16106 or an unqualified-id naming a destructor; if instantiating
16107 it produces an expression, it's not an id-expression or
16108 member access. */
16109 id = false;
16110 type = finish_decltype_type (type, id, complain);
16111 }
16112 return cp_build_qualified_type_real (type,
16113 cp_type_quals (t)
16114 | cp_type_quals (type),
16115 complain | tf_ignore_bad_quals);
16116 }
16117
16118 case UNDERLYING_TYPE:
16119 {
16120 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16121 complain, in_decl);
16122 return finish_underlying_type (type);
16123 }
16124
16125 case TYPE_ARGUMENT_PACK:
16126 case NONTYPE_ARGUMENT_PACK:
16127 return tsubst_argument_pack (t, args, complain, in_decl);
16128
16129 case VOID_CST:
16130 case INTEGER_CST:
16131 case REAL_CST:
16132 case STRING_CST:
16133 case PLUS_EXPR:
16134 case MINUS_EXPR:
16135 case NEGATE_EXPR:
16136 case NOP_EXPR:
16137 case INDIRECT_REF:
16138 case ADDR_EXPR:
16139 case CALL_EXPR:
16140 case ARRAY_REF:
16141 case SCOPE_REF:
16142 /* We should use one of the expression tsubsts for these codes. */
16143 gcc_unreachable ();
16144
16145 default:
16146 sorry ("use of %qs in template", get_tree_code_name (code));
16147 return error_mark_node;
16148 }
16149 }
16150
16151 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16152 expression on the left-hand side of the "." or "->" operator. We
16153 only do the lookup if we had a dependent BASELINK. Otherwise we
16154 adjust it onto the instantiated heirarchy. */
16155
16156 static tree
16157 tsubst_baselink (tree baselink, tree object_type,
16158 tree args, tsubst_flags_t complain, tree in_decl)
16159 {
16160 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16161 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16162 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16163
16164 tree optype = BASELINK_OPTYPE (baselink);
16165 optype = tsubst (optype, args, complain, in_decl);
16166
16167 tree template_args = NULL_TREE;
16168 bool template_id_p = false;
16169 tree fns = BASELINK_FUNCTIONS (baselink);
16170 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16171 {
16172 template_id_p = true;
16173 template_args = TREE_OPERAND (fns, 1);
16174 fns = TREE_OPERAND (fns, 0);
16175 if (template_args)
16176 template_args = tsubst_template_args (template_args, args,
16177 complain, in_decl);
16178 }
16179
16180 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16181 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16182 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16183
16184 if (dependent_p)
16185 {
16186 tree name = OVL_NAME (fns);
16187 if (IDENTIFIER_CONV_OP_P (name))
16188 name = make_conv_op_name (optype);
16189
16190 if (name == complete_dtor_identifier)
16191 /* Treat as-if non-dependent below. */
16192 dependent_p = false;
16193
16194 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16195 complain);
16196 if (!baselink)
16197 {
16198 if ((complain & tf_error)
16199 && constructor_name_p (name, qualifying_scope))
16200 error ("cannot call constructor %<%T::%D%> directly",
16201 qualifying_scope, name);
16202 return error_mark_node;
16203 }
16204
16205 if (BASELINK_P (baselink))
16206 fns = BASELINK_FUNCTIONS (baselink);
16207 }
16208 else
16209 /* We're going to overwrite pieces below, make a duplicate. */
16210 baselink = copy_node (baselink);
16211
16212 /* If lookup found a single function, mark it as used at this point.
16213 (If lookup found multiple functions the one selected later by
16214 overload resolution will be marked as used at that point.) */
16215 if (!template_id_p && !really_overloaded_fn (fns))
16216 {
16217 tree fn = OVL_FIRST (fns);
16218 bool ok = mark_used (fn, complain);
16219 if (!ok && !(complain & tf_error))
16220 return error_mark_node;
16221 if (ok && BASELINK_P (baselink))
16222 /* We might have instantiated an auto function. */
16223 TREE_TYPE (baselink) = TREE_TYPE (fn);
16224 }
16225
16226 if (BASELINK_P (baselink))
16227 {
16228 /* Add back the template arguments, if present. */
16229 if (template_id_p)
16230 BASELINK_FUNCTIONS (baselink)
16231 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16232
16233 /* Update the conversion operator type. */
16234 BASELINK_OPTYPE (baselink) = optype;
16235 }
16236
16237 if (!object_type)
16238 object_type = current_class_type;
16239
16240 if (qualified_p || !dependent_p)
16241 {
16242 baselink = adjust_result_of_qualified_name_lookup (baselink,
16243 qualifying_scope,
16244 object_type);
16245 if (!qualified_p)
16246 /* We need to call adjust_result_of_qualified_name_lookup in case the
16247 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16248 so that we still get virtual function binding. */
16249 BASELINK_QUALIFIED_P (baselink) = false;
16250 }
16251
16252 return baselink;
16253 }
16254
16255 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16256 true if the qualified-id will be a postfix-expression in-and-of
16257 itself; false if more of the postfix-expression follows the
16258 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16259 of "&". */
16260
16261 static tree
16262 tsubst_qualified_id (tree qualified_id, tree args,
16263 tsubst_flags_t complain, tree in_decl,
16264 bool done, bool address_p)
16265 {
16266 tree expr;
16267 tree scope;
16268 tree name;
16269 bool is_template;
16270 tree template_args;
16271 location_t loc = EXPR_LOCATION (qualified_id);
16272
16273 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16274
16275 /* Figure out what name to look up. */
16276 name = TREE_OPERAND (qualified_id, 1);
16277 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16278 {
16279 is_template = true;
16280 template_args = TREE_OPERAND (name, 1);
16281 if (template_args)
16282 template_args = tsubst_template_args (template_args, args,
16283 complain, in_decl);
16284 if (template_args == error_mark_node)
16285 return error_mark_node;
16286 name = TREE_OPERAND (name, 0);
16287 }
16288 else
16289 {
16290 is_template = false;
16291 template_args = NULL_TREE;
16292 }
16293
16294 /* Substitute into the qualifying scope. When there are no ARGS, we
16295 are just trying to simplify a non-dependent expression. In that
16296 case the qualifying scope may be dependent, and, in any case,
16297 substituting will not help. */
16298 scope = TREE_OPERAND (qualified_id, 0);
16299 if (args)
16300 {
16301 scope = tsubst (scope, args, complain, in_decl);
16302 expr = tsubst_copy (name, args, complain, in_decl);
16303 }
16304 else
16305 expr = name;
16306
16307 if (dependent_scope_p (scope))
16308 {
16309 if (is_template)
16310 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16311 tree r = build_qualified_name (NULL_TREE, scope, expr,
16312 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16313 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16314 return r;
16315 }
16316
16317 if (!BASELINK_P (name) && !DECL_P (expr))
16318 {
16319 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16320 {
16321 /* A BIT_NOT_EXPR is used to represent a destructor. */
16322 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16323 {
16324 error ("qualifying type %qT does not match destructor name ~%qT",
16325 scope, TREE_OPERAND (expr, 0));
16326 expr = error_mark_node;
16327 }
16328 else
16329 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16330 LOOK_want::NORMAL, false);
16331 }
16332 else
16333 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16334 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16335 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16336 {
16337 if (complain & tf_error)
16338 {
16339 error ("dependent-name %qE is parsed as a non-type, but "
16340 "instantiation yields a type", qualified_id);
16341 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16342 }
16343 return error_mark_node;
16344 }
16345 }
16346
16347 if (DECL_P (expr))
16348 {
16349 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16350 scope, complain))
16351 return error_mark_node;
16352 /* Remember that there was a reference to this entity. */
16353 if (!mark_used (expr, complain) && !(complain & tf_error))
16354 return error_mark_node;
16355 }
16356
16357 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16358 {
16359 if (complain & tf_error)
16360 qualified_name_lookup_error (scope,
16361 TREE_OPERAND (qualified_id, 1),
16362 expr, input_location);
16363 return error_mark_node;
16364 }
16365
16366 if (is_template)
16367 {
16368 /* We may be repeating a check already done during parsing, but
16369 if it was well-formed and passed then, it will pass again
16370 now, and if it didn't, we wouldn't have got here. The case
16371 we want to catch is when we couldn't tell then, and can now,
16372 namely when templ prior to substitution was an
16373 identifier. */
16374 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16375 return error_mark_node;
16376
16377 if (variable_template_p (expr))
16378 expr = lookup_and_finish_template_variable (expr, template_args,
16379 complain);
16380 else
16381 expr = lookup_template_function (expr, template_args);
16382 }
16383
16384 if (expr == error_mark_node && complain & tf_error)
16385 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16386 expr, input_location);
16387 else if (TYPE_P (scope))
16388 {
16389 expr = (adjust_result_of_qualified_name_lookup
16390 (expr, scope, current_nonlambda_class_type ()));
16391 expr = (finish_qualified_id_expr
16392 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16393 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16394 /*template_arg_p=*/false, complain));
16395 }
16396
16397 /* Expressions do not generally have reference type. */
16398 if (TREE_CODE (expr) != SCOPE_REF
16399 /* However, if we're about to form a pointer-to-member, we just
16400 want the referenced member referenced. */
16401 && TREE_CODE (expr) != OFFSET_REF)
16402 expr = convert_from_reference (expr);
16403
16404 if (REF_PARENTHESIZED_P (qualified_id))
16405 expr = force_paren_expr (expr);
16406
16407 expr = maybe_wrap_with_location (expr, loc);
16408
16409 return expr;
16410 }
16411
16412 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16413 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16414 for tsubst. */
16415
16416 static tree
16417 tsubst_init (tree init, tree decl, tree args,
16418 tsubst_flags_t complain, tree in_decl)
16419 {
16420 if (!init)
16421 return NULL_TREE;
16422
16423 init = tsubst_expr (init, args, complain, in_decl, false);
16424
16425 tree type = TREE_TYPE (decl);
16426
16427 if (!init && type != error_mark_node)
16428 {
16429 if (tree auto_node = type_uses_auto (type))
16430 {
16431 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16432 {
16433 if (complain & tf_error)
16434 error ("initializer for %q#D expands to an empty list "
16435 "of expressions", decl);
16436 return error_mark_node;
16437 }
16438 }
16439 else if (!dependent_type_p (type))
16440 {
16441 /* If we had an initializer but it
16442 instantiated to nothing,
16443 value-initialize the object. This will
16444 only occur when the initializer was a
16445 pack expansion where the parameter packs
16446 used in that expansion were of length
16447 zero. */
16448 init = build_value_init (type, complain);
16449 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16450 init = get_target_expr_sfinae (init, complain);
16451 if (TREE_CODE (init) == TARGET_EXPR)
16452 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16453 }
16454 }
16455
16456 return init;
16457 }
16458
16459 /* If T is a reference to a dependent member of the current instantiation C and
16460 we are trying to refer to that member in a partial instantiation of C,
16461 return a SCOPE_REF; otherwise, return NULL_TREE.
16462
16463 This can happen when forming a C++17 deduction guide, as in PR96199. */
16464
16465 static tree
16466 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16467 tree in_decl)
16468 {
16469 if (cxx_dialect < cxx17)
16470 return NULL_TREE;
16471
16472 tree ctx = context_for_name_lookup (t);
16473 if (!CLASS_TYPE_P (ctx))
16474 return NULL_TREE;
16475
16476 ctx = tsubst (ctx, args, complain, in_decl);
16477 if (dependent_scope_p (ctx))
16478 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16479 /*template_p=*/false);
16480
16481 return NULL_TREE;
16482 }
16483
16484 /* Like tsubst, but deals with expressions. This function just replaces
16485 template parms; to finish processing the resultant expression, use
16486 tsubst_copy_and_build or tsubst_expr. */
16487
16488 static tree
16489 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16490 {
16491 enum tree_code code;
16492 tree r;
16493
16494 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16495 return t;
16496
16497 code = TREE_CODE (t);
16498
16499 switch (code)
16500 {
16501 case PARM_DECL:
16502 r = retrieve_local_specialization (t);
16503
16504 if (r == NULL_TREE)
16505 {
16506 /* We get here for a use of 'this' in an NSDMI. */
16507 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16508 return current_class_ptr;
16509
16510 /* This can happen for a parameter name used later in a function
16511 declaration (such as in a late-specified return type). Just
16512 make a dummy decl, since it's only used for its type. */
16513 gcc_assert (cp_unevaluated_operand != 0);
16514 r = tsubst_decl (t, args, complain);
16515 /* Give it the template pattern as its context; its true context
16516 hasn't been instantiated yet and this is good enough for
16517 mangling. */
16518 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16519 }
16520
16521 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16522 r = argument_pack_select_arg (r);
16523 if (!mark_used (r, complain) && !(complain & tf_error))
16524 return error_mark_node;
16525 return r;
16526
16527 case CONST_DECL:
16528 {
16529 tree enum_type;
16530 tree v;
16531
16532 if (DECL_TEMPLATE_PARM_P (t))
16533 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16534 /* There is no need to substitute into namespace-scope
16535 enumerators. */
16536 if (DECL_NAMESPACE_SCOPE_P (t))
16537 return t;
16538 /* If ARGS is NULL, then T is known to be non-dependent. */
16539 if (args == NULL_TREE)
16540 return scalar_constant_value (t);
16541
16542 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16543 return ref;
16544
16545 /* Unfortunately, we cannot just call lookup_name here.
16546 Consider:
16547
16548 template <int I> int f() {
16549 enum E { a = I };
16550 struct S { void g() { E e = a; } };
16551 };
16552
16553 When we instantiate f<7>::S::g(), say, lookup_name is not
16554 clever enough to find f<7>::a. */
16555 enum_type
16556 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16557 /*entering_scope=*/0);
16558
16559 for (v = TYPE_VALUES (enum_type);
16560 v != NULL_TREE;
16561 v = TREE_CHAIN (v))
16562 if (TREE_PURPOSE (v) == DECL_NAME (t))
16563 return TREE_VALUE (v);
16564
16565 /* We didn't find the name. That should never happen; if
16566 name-lookup found it during preliminary parsing, we
16567 should find it again here during instantiation. */
16568 gcc_unreachable ();
16569 }
16570 return t;
16571
16572 case FIELD_DECL:
16573 if (DECL_CONTEXT (t))
16574 {
16575 tree ctx;
16576
16577 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16578 /*entering_scope=*/1);
16579 if (ctx != DECL_CONTEXT (t))
16580 {
16581 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16582 if (!r)
16583 {
16584 if (complain & tf_error)
16585 error ("using invalid field %qD", t);
16586 return error_mark_node;
16587 }
16588 return r;
16589 }
16590 }
16591
16592 return t;
16593
16594 case VAR_DECL:
16595 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16596 return ref;
16597 gcc_fallthrough();
16598 case FUNCTION_DECL:
16599 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16600 r = tsubst (t, args, complain, in_decl);
16601 else if (DECL_LOCAL_DECL_P (t))
16602 {
16603 /* Local specialization will have been created when we
16604 instantiated the DECL_EXPR_DECL. */
16605 r = retrieve_local_specialization (t);
16606 if (!r)
16607 r = error_mark_node;
16608 }
16609 else if (local_variable_p (t)
16610 && uses_template_parms (DECL_CONTEXT (t)))
16611 {
16612 r = retrieve_local_specialization (t);
16613 if (r == NULL_TREE)
16614 {
16615 /* First try name lookup to find the instantiation. */
16616 r = lookup_name (DECL_NAME (t));
16617 if (r)
16618 {
16619 if (!VAR_P (r))
16620 {
16621 /* During error-recovery we may find a non-variable,
16622 even an OVERLOAD: just bail out and avoid ICEs and
16623 duplicate diagnostics (c++/62207). */
16624 gcc_assert (seen_error ());
16625 return error_mark_node;
16626 }
16627 if (!is_capture_proxy (r))
16628 {
16629 /* Make sure the one we found is the one we want. */
16630 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16631 if (ctx != DECL_CONTEXT (r))
16632 r = NULL_TREE;
16633 }
16634 }
16635
16636 if (r)
16637 /* OK */;
16638 else
16639 {
16640 /* This can happen for a variable used in a
16641 late-specified return type of a local lambda, or for a
16642 local static or constant. Building a new VAR_DECL
16643 should be OK in all those cases. */
16644 r = tsubst_decl (t, args, complain);
16645 if (local_specializations)
16646 /* Avoid infinite recursion (79640). */
16647 register_local_specialization (r, t);
16648 if (decl_maybe_constant_var_p (r))
16649 {
16650 /* We can't call cp_finish_decl, so handle the
16651 initializer by hand. */
16652 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16653 complain, in_decl);
16654 if (!processing_template_decl)
16655 init = maybe_constant_init (init);
16656 if (processing_template_decl
16657 ? potential_constant_expression (init)
16658 : reduced_constant_expression_p (init))
16659 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16660 = TREE_CONSTANT (r) = true;
16661 DECL_INITIAL (r) = init;
16662 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16663 TREE_TYPE (r)
16664 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16665 complain, adc_variable_type);
16666 }
16667 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16668 || decl_constant_var_p (r)
16669 || seen_error ());
16670 if (!processing_template_decl
16671 && !TREE_STATIC (r))
16672 r = process_outer_var_ref (r, complain);
16673 }
16674 /* Remember this for subsequent uses. */
16675 if (local_specializations)
16676 register_local_specialization (r, t);
16677 }
16678 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16679 r = argument_pack_select_arg (r);
16680 }
16681 else
16682 r = t;
16683 if (!mark_used (r, complain))
16684 return error_mark_node;
16685 return r;
16686
16687 case NAMESPACE_DECL:
16688 return t;
16689
16690 case OVERLOAD:
16691 return t;
16692
16693 case BASELINK:
16694 return tsubst_baselink (t, current_nonlambda_class_type (),
16695 args, complain, in_decl);
16696
16697 case TEMPLATE_DECL:
16698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16699 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16700 args, complain, in_decl);
16701 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16702 return tsubst (t, args, complain, in_decl);
16703 else if (DECL_CLASS_SCOPE_P (t)
16704 && uses_template_parms (DECL_CONTEXT (t)))
16705 {
16706 /* Template template argument like the following example need
16707 special treatment:
16708
16709 template <template <class> class TT> struct C {};
16710 template <class T> struct D {
16711 template <class U> struct E {};
16712 C<E> c; // #1
16713 };
16714 D<int> d; // #2
16715
16716 We are processing the template argument `E' in #1 for
16717 the template instantiation #2. Originally, `E' is a
16718 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16719 have to substitute this with one having context `D<int>'. */
16720
16721 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16722 if (dependent_scope_p (context))
16723 {
16724 /* When rewriting a constructor into a deduction guide, a
16725 non-dependent name can become dependent, so memtmpl<args>
16726 becomes context::template memtmpl<args>. */
16727 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16728 return build_qualified_name (type, context, DECL_NAME (t),
16729 /*template*/true);
16730 }
16731 return lookup_field (context, DECL_NAME(t), 0, false);
16732 }
16733 else
16734 /* Ordinary template template argument. */
16735 return t;
16736
16737 case NON_LVALUE_EXPR:
16738 case VIEW_CONVERT_EXPR:
16739 {
16740 /* Handle location wrappers by substituting the wrapped node
16741 first, *then* reusing the resulting type. Doing the type
16742 first ensures that we handle template parameters and
16743 parameter pack expansions. */
16744 if (location_wrapper_p (t))
16745 {
16746 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16747 complain, in_decl);
16748 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16749 }
16750 tree op = TREE_OPERAND (t, 0);
16751 if (code == VIEW_CONVERT_EXPR
16752 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16753 {
16754 /* Wrapper to make a C++20 template parameter object const. */
16755 op = tsubst_copy (op, args, complain, in_decl);
16756 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16757 {
16758 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16759 return build1 (code, type, op);
16760 }
16761 else
16762 {
16763 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16764 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16765 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16766 return op;
16767 }
16768 }
16769 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16770 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16771 {
16772 op = tsubst_copy (op, args, complain, in_decl);
16773 op = build1 (code, TREE_TYPE (op), op);
16774 REF_PARENTHESIZED_P (op) = true;
16775 return op;
16776 }
16777 /* We shouldn't see any other uses of these in templates. */
16778 gcc_unreachable ();
16779 }
16780
16781 case CAST_EXPR:
16782 case REINTERPRET_CAST_EXPR:
16783 case CONST_CAST_EXPR:
16784 case STATIC_CAST_EXPR:
16785 case DYNAMIC_CAST_EXPR:
16786 case IMPLICIT_CONV_EXPR:
16787 case CONVERT_EXPR:
16788 case NOP_EXPR:
16789 {
16790 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16791 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16792 return build1 (code, type, op0);
16793 }
16794
16795 case BIT_CAST_EXPR:
16796 {
16797 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16798 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16799 r = build_min (BIT_CAST_EXPR, type, op0);
16800 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16801 return r;
16802 }
16803
16804 case SIZEOF_EXPR:
16805 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16806 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16807 {
16808 tree expanded, op = TREE_OPERAND (t, 0);
16809 int len = 0;
16810
16811 if (SIZEOF_EXPR_TYPE_P (t))
16812 op = TREE_TYPE (op);
16813
16814 ++cp_unevaluated_operand;
16815 ++c_inhibit_evaluation_warnings;
16816 /* We only want to compute the number of arguments. */
16817 if (PACK_EXPANSION_P (op))
16818 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16819 else
16820 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16821 args, complain, in_decl);
16822 --cp_unevaluated_operand;
16823 --c_inhibit_evaluation_warnings;
16824
16825 if (TREE_CODE (expanded) == TREE_VEC)
16826 {
16827 len = TREE_VEC_LENGTH (expanded);
16828 /* Set TREE_USED for the benefit of -Wunused. */
16829 for (int i = 0; i < len; i++)
16830 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16831 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16832 }
16833
16834 if (expanded == error_mark_node)
16835 return error_mark_node;
16836 else if (PACK_EXPANSION_P (expanded)
16837 || (TREE_CODE (expanded) == TREE_VEC
16838 && pack_expansion_args_count (expanded)))
16839
16840 {
16841 if (PACK_EXPANSION_P (expanded))
16842 /* OK. */;
16843 else if (TREE_VEC_LENGTH (expanded) == 1)
16844 expanded = TREE_VEC_ELT (expanded, 0);
16845 else
16846 expanded = make_argument_pack (expanded);
16847
16848 if (TYPE_P (expanded))
16849 return cxx_sizeof_or_alignof_type (input_location,
16850 expanded, SIZEOF_EXPR,
16851 false,
16852 complain & tf_error);
16853 else
16854 return cxx_sizeof_or_alignof_expr (input_location,
16855 expanded, SIZEOF_EXPR,
16856 false,
16857 complain & tf_error);
16858 }
16859 else
16860 return build_int_cst (size_type_node, len);
16861 }
16862 if (SIZEOF_EXPR_TYPE_P (t))
16863 {
16864 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16865 args, complain, in_decl);
16866 r = build1 (NOP_EXPR, r, error_mark_node);
16867 r = build1 (SIZEOF_EXPR,
16868 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16869 SIZEOF_EXPR_TYPE_P (r) = 1;
16870 return r;
16871 }
16872 /* Fall through */
16873
16874 case INDIRECT_REF:
16875 case NEGATE_EXPR:
16876 case TRUTH_NOT_EXPR:
16877 case BIT_NOT_EXPR:
16878 case ADDR_EXPR:
16879 case UNARY_PLUS_EXPR: /* Unary + */
16880 case ALIGNOF_EXPR:
16881 case AT_ENCODE_EXPR:
16882 case ARROW_EXPR:
16883 case THROW_EXPR:
16884 case TYPEID_EXPR:
16885 case REALPART_EXPR:
16886 case IMAGPART_EXPR:
16887 case PAREN_EXPR:
16888 {
16889 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16890 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16891 r = build1 (code, type, op0);
16892 if (code == ALIGNOF_EXPR)
16893 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16894 return r;
16895 }
16896
16897 case COMPONENT_REF:
16898 {
16899 tree object;
16900 tree name;
16901
16902 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16903 name = TREE_OPERAND (t, 1);
16904 if (TREE_CODE (name) == BIT_NOT_EXPR)
16905 {
16906 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16907 complain, in_decl);
16908 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16909 }
16910 else if (TREE_CODE (name) == SCOPE_REF
16911 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16912 {
16913 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16914 complain, in_decl);
16915 name = TREE_OPERAND (name, 1);
16916 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16917 complain, in_decl);
16918 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16919 name = build_qualified_name (/*type=*/NULL_TREE,
16920 base, name,
16921 /*template_p=*/false);
16922 }
16923 else if (BASELINK_P (name))
16924 name = tsubst_baselink (name,
16925 non_reference (TREE_TYPE (object)),
16926 args, complain,
16927 in_decl);
16928 else
16929 name = tsubst_copy (name, args, complain, in_decl);
16930 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16931 }
16932
16933 case PLUS_EXPR:
16934 case MINUS_EXPR:
16935 case MULT_EXPR:
16936 case TRUNC_DIV_EXPR:
16937 case CEIL_DIV_EXPR:
16938 case FLOOR_DIV_EXPR:
16939 case ROUND_DIV_EXPR:
16940 case EXACT_DIV_EXPR:
16941 case BIT_AND_EXPR:
16942 case BIT_IOR_EXPR:
16943 case BIT_XOR_EXPR:
16944 case TRUNC_MOD_EXPR:
16945 case FLOOR_MOD_EXPR:
16946 case TRUTH_ANDIF_EXPR:
16947 case TRUTH_ORIF_EXPR:
16948 case TRUTH_AND_EXPR:
16949 case TRUTH_OR_EXPR:
16950 case RSHIFT_EXPR:
16951 case LSHIFT_EXPR:
16952 case EQ_EXPR:
16953 case NE_EXPR:
16954 case MAX_EXPR:
16955 case MIN_EXPR:
16956 case LE_EXPR:
16957 case GE_EXPR:
16958 case LT_EXPR:
16959 case GT_EXPR:
16960 case COMPOUND_EXPR:
16961 case DOTSTAR_EXPR:
16962 case MEMBER_REF:
16963 case PREDECREMENT_EXPR:
16964 case PREINCREMENT_EXPR:
16965 case POSTDECREMENT_EXPR:
16966 case POSTINCREMENT_EXPR:
16967 {
16968 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16969 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16970 return build_nt (code, op0, op1);
16971 }
16972
16973 case SCOPE_REF:
16974 {
16975 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16976 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16977 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16978 QUALIFIED_NAME_IS_TEMPLATE (t));
16979 }
16980
16981 case ARRAY_REF:
16982 {
16983 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16984 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16985 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16986 }
16987
16988 case CALL_EXPR:
16989 {
16990 int n = VL_EXP_OPERAND_LENGTH (t);
16991 tree result = build_vl_exp (CALL_EXPR, n);
16992 int i;
16993 for (i = 0; i < n; i++)
16994 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16995 complain, in_decl);
16996 return result;
16997 }
16998
16999 case COND_EXPR:
17000 case MODOP_EXPR:
17001 case PSEUDO_DTOR_EXPR:
17002 case VEC_PERM_EXPR:
17003 {
17004 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17005 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17006 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17007 r = build_nt (code, op0, op1, op2);
17008 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17009 return r;
17010 }
17011
17012 case NEW_EXPR:
17013 {
17014 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17015 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17016 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17017 r = build_nt (code, op0, op1, op2);
17018 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17019 return r;
17020 }
17021
17022 case DELETE_EXPR:
17023 {
17024 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17025 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17026 r = build_nt (code, op0, op1);
17027 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17028 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17029 return r;
17030 }
17031
17032 case TEMPLATE_ID_EXPR:
17033 {
17034 /* Substituted template arguments */
17035 tree fn = TREE_OPERAND (t, 0);
17036 tree targs = TREE_OPERAND (t, 1);
17037
17038 fn = tsubst_copy (fn, args, complain, in_decl);
17039 if (targs)
17040 targs = tsubst_template_args (targs, args, complain, in_decl);
17041
17042 return lookup_template_function (fn, targs);
17043 }
17044
17045 case TREE_LIST:
17046 {
17047 tree purpose, value, chain;
17048
17049 if (t == void_list_node)
17050 return t;
17051
17052 purpose = TREE_PURPOSE (t);
17053 if (purpose)
17054 purpose = tsubst_copy (purpose, args, complain, in_decl);
17055 value = TREE_VALUE (t);
17056 if (value)
17057 value = tsubst_copy (value, args, complain, in_decl);
17058 chain = TREE_CHAIN (t);
17059 if (chain && chain != void_type_node)
17060 chain = tsubst_copy (chain, args, complain, in_decl);
17061 if (purpose == TREE_PURPOSE (t)
17062 && value == TREE_VALUE (t)
17063 && chain == TREE_CHAIN (t))
17064 return t;
17065 return tree_cons (purpose, value, chain);
17066 }
17067
17068 case RECORD_TYPE:
17069 case UNION_TYPE:
17070 case ENUMERAL_TYPE:
17071 case INTEGER_TYPE:
17072 case TEMPLATE_TYPE_PARM:
17073 case TEMPLATE_TEMPLATE_PARM:
17074 case BOUND_TEMPLATE_TEMPLATE_PARM:
17075 case TEMPLATE_PARM_INDEX:
17076 case POINTER_TYPE:
17077 case REFERENCE_TYPE:
17078 case OFFSET_TYPE:
17079 case FUNCTION_TYPE:
17080 case METHOD_TYPE:
17081 case ARRAY_TYPE:
17082 case TYPENAME_TYPE:
17083 case UNBOUND_CLASS_TEMPLATE:
17084 case TYPEOF_TYPE:
17085 case DECLTYPE_TYPE:
17086 case TYPE_DECL:
17087 return tsubst (t, args, complain, in_decl);
17088
17089 case USING_DECL:
17090 t = DECL_NAME (t);
17091 /* Fall through. */
17092 case IDENTIFIER_NODE:
17093 if (IDENTIFIER_CONV_OP_P (t))
17094 {
17095 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17096 return make_conv_op_name (new_type);
17097 }
17098 else
17099 return t;
17100
17101 case CONSTRUCTOR:
17102 /* This is handled by tsubst_copy_and_build. */
17103 gcc_unreachable ();
17104
17105 case VA_ARG_EXPR:
17106 {
17107 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17108 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17109 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17110 }
17111
17112 case CLEANUP_POINT_EXPR:
17113 /* We shouldn't have built any of these during initial template
17114 generation. Instead, they should be built during instantiation
17115 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17116 gcc_unreachable ();
17117
17118 case OFFSET_REF:
17119 {
17120 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17121 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17122 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17123 r = build2 (code, type, op0, op1);
17124 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17125 if (!mark_used (TREE_OPERAND (r, 1), complain)
17126 && !(complain & tf_error))
17127 return error_mark_node;
17128 return r;
17129 }
17130
17131 case EXPR_PACK_EXPANSION:
17132 error ("invalid use of pack expansion expression");
17133 return error_mark_node;
17134
17135 case NONTYPE_ARGUMENT_PACK:
17136 error ("use %<...%> to expand argument pack");
17137 return error_mark_node;
17138
17139 case VOID_CST:
17140 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17141 return t;
17142
17143 case INTEGER_CST:
17144 case REAL_CST:
17145 case COMPLEX_CST:
17146 {
17147 /* Instantiate any typedefs in the type. */
17148 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17149 r = fold_convert (type, t);
17150 gcc_assert (TREE_CODE (r) == code);
17151 return r;
17152 }
17153
17154 case STRING_CST:
17155 {
17156 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17157 r = t;
17158 if (type != TREE_TYPE (t))
17159 {
17160 r = copy_node (t);
17161 TREE_TYPE (r) = type;
17162 }
17163 return r;
17164 }
17165
17166 case PTRMEM_CST:
17167 /* These can sometimes show up in a partial instantiation, but never
17168 involve template parms. */
17169 gcc_assert (!uses_template_parms (t));
17170 return t;
17171
17172 case UNARY_LEFT_FOLD_EXPR:
17173 return tsubst_unary_left_fold (t, args, complain, in_decl);
17174 case UNARY_RIGHT_FOLD_EXPR:
17175 return tsubst_unary_right_fold (t, args, complain, in_decl);
17176 case BINARY_LEFT_FOLD_EXPR:
17177 return tsubst_binary_left_fold (t, args, complain, in_decl);
17178 case BINARY_RIGHT_FOLD_EXPR:
17179 return tsubst_binary_right_fold (t, args, complain, in_decl);
17180 case PREDICT_EXPR:
17181 return t;
17182
17183 case DEBUG_BEGIN_STMT:
17184 /* ??? There's no point in copying it for now, but maybe some
17185 day it will contain more information, such as a pointer back
17186 to the containing function, inlined copy or so. */
17187 return t;
17188
17189 case CO_AWAIT_EXPR:
17190 return tsubst_expr (t, args, complain, in_decl,
17191 /*integral_constant_expression_p=*/false);
17192 break;
17193
17194 default:
17195 /* We shouldn't get here, but keep going if !flag_checking. */
17196 if (flag_checking)
17197 gcc_unreachable ();
17198 return t;
17199 }
17200 }
17201
17202 /* Helper function for tsubst_omp_clauses, used for instantiation of
17203 OMP_CLAUSE_DECL of clauses. */
17204
17205 static tree
17206 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17207 tree in_decl, tree *iterator_cache)
17208 {
17209 if (decl == NULL_TREE)
17210 return NULL_TREE;
17211
17212 /* Handle OpenMP iterators. */
17213 if (TREE_CODE (decl) == TREE_LIST
17214 && TREE_PURPOSE (decl)
17215 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17216 {
17217 tree ret;
17218 if (iterator_cache[0] == TREE_PURPOSE (decl))
17219 ret = iterator_cache[1];
17220 else
17221 {
17222 tree *tp = &ret;
17223 begin_scope (sk_omp, NULL);
17224 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17225 {
17226 *tp = copy_node (it);
17227 TREE_VEC_ELT (*tp, 0)
17228 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17229 TREE_VEC_ELT (*tp, 1)
17230 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17231 /*integral_constant_expression_p=*/false);
17232 TREE_VEC_ELT (*tp, 2)
17233 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17234 /*integral_constant_expression_p=*/false);
17235 TREE_VEC_ELT (*tp, 3)
17236 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17237 /*integral_constant_expression_p=*/false);
17238 TREE_CHAIN (*tp) = NULL_TREE;
17239 tp = &TREE_CHAIN (*tp);
17240 }
17241 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17242 iterator_cache[0] = TREE_PURPOSE (decl);
17243 iterator_cache[1] = ret;
17244 }
17245 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17246 args, complain,
17247 in_decl, NULL));
17248 }
17249
17250 /* Handle an OpenMP array section represented as a TREE_LIST (or
17251 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17252 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17253 TREE_LIST. We can handle it exactly the same as an array section
17254 (purpose, value, and a chain), even though the nomenclature
17255 (low_bound, length, etc) is different. */
17256 if (TREE_CODE (decl) == TREE_LIST)
17257 {
17258 tree low_bound
17259 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17260 /*integral_constant_expression_p=*/false);
17261 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17262 /*integral_constant_expression_p=*/false);
17263 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17264 in_decl, NULL);
17265 if (TREE_PURPOSE (decl) == low_bound
17266 && TREE_VALUE (decl) == length
17267 && TREE_CHAIN (decl) == chain)
17268 return decl;
17269 tree ret = tree_cons (low_bound, length, chain);
17270 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17271 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17272 return ret;
17273 }
17274 tree ret = tsubst_expr (decl, args, complain, in_decl,
17275 /*integral_constant_expression_p=*/false);
17276 /* Undo convert_from_reference tsubst_expr could have called. */
17277 if (decl
17278 && REFERENCE_REF_P (ret)
17279 && !REFERENCE_REF_P (decl))
17280 ret = TREE_OPERAND (ret, 0);
17281 return ret;
17282 }
17283
17284 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17285
17286 static tree
17287 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17288 tree args, tsubst_flags_t complain, tree in_decl)
17289 {
17290 tree new_clauses = NULL_TREE, nc, oc;
17291 tree linear_no_step = NULL_TREE;
17292 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17293
17294 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17295 {
17296 nc = copy_node (oc);
17297 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17298 new_clauses = nc;
17299
17300 switch (OMP_CLAUSE_CODE (nc))
17301 {
17302 case OMP_CLAUSE_LASTPRIVATE:
17303 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17304 {
17305 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17306 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17307 in_decl, /*integral_constant_expression_p=*/false);
17308 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17309 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17310 }
17311 /* FALLTHRU */
17312 case OMP_CLAUSE_PRIVATE:
17313 case OMP_CLAUSE_SHARED:
17314 case OMP_CLAUSE_FIRSTPRIVATE:
17315 case OMP_CLAUSE_COPYIN:
17316 case OMP_CLAUSE_COPYPRIVATE:
17317 case OMP_CLAUSE_UNIFORM:
17318 case OMP_CLAUSE_DEPEND:
17319 case OMP_CLAUSE_FROM:
17320 case OMP_CLAUSE_TO:
17321 case OMP_CLAUSE_MAP:
17322 case OMP_CLAUSE__CACHE_:
17323 case OMP_CLAUSE_NONTEMPORAL:
17324 case OMP_CLAUSE_USE_DEVICE_PTR:
17325 case OMP_CLAUSE_USE_DEVICE_ADDR:
17326 case OMP_CLAUSE_IS_DEVICE_PTR:
17327 case OMP_CLAUSE_INCLUSIVE:
17328 case OMP_CLAUSE_EXCLUSIVE:
17329 OMP_CLAUSE_DECL (nc)
17330 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17331 in_decl, iterator_cache);
17332 break;
17333 case OMP_CLAUSE_TILE:
17334 case OMP_CLAUSE_IF:
17335 case OMP_CLAUSE_NUM_THREADS:
17336 case OMP_CLAUSE_SCHEDULE:
17337 case OMP_CLAUSE_COLLAPSE:
17338 case OMP_CLAUSE_FINAL:
17339 case OMP_CLAUSE_DEVICE:
17340 case OMP_CLAUSE_DIST_SCHEDULE:
17341 case OMP_CLAUSE_NUM_TEAMS:
17342 case OMP_CLAUSE_THREAD_LIMIT:
17343 case OMP_CLAUSE_SAFELEN:
17344 case OMP_CLAUSE_SIMDLEN:
17345 case OMP_CLAUSE_NUM_TASKS:
17346 case OMP_CLAUSE_GRAINSIZE:
17347 case OMP_CLAUSE_PRIORITY:
17348 case OMP_CLAUSE_ORDERED:
17349 case OMP_CLAUSE_HINT:
17350 case OMP_CLAUSE_NUM_GANGS:
17351 case OMP_CLAUSE_NUM_WORKERS:
17352 case OMP_CLAUSE_VECTOR_LENGTH:
17353 case OMP_CLAUSE_WORKER:
17354 case OMP_CLAUSE_VECTOR:
17355 case OMP_CLAUSE_ASYNC:
17356 case OMP_CLAUSE_WAIT:
17357 case OMP_CLAUSE_DETACH:
17358 OMP_CLAUSE_OPERAND (nc, 0)
17359 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17360 in_decl, /*integral_constant_expression_p=*/false);
17361 break;
17362 case OMP_CLAUSE_REDUCTION:
17363 case OMP_CLAUSE_IN_REDUCTION:
17364 case OMP_CLAUSE_TASK_REDUCTION:
17365 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17366 {
17367 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17368 if (TREE_CODE (placeholder) == SCOPE_REF)
17369 {
17370 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17371 complain, in_decl);
17372 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17373 = build_qualified_name (NULL_TREE, scope,
17374 TREE_OPERAND (placeholder, 1),
17375 false);
17376 }
17377 else
17378 gcc_assert (identifier_p (placeholder));
17379 }
17380 OMP_CLAUSE_DECL (nc)
17381 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17382 in_decl, NULL);
17383 break;
17384 case OMP_CLAUSE_GANG:
17385 case OMP_CLAUSE_ALIGNED:
17386 case OMP_CLAUSE_ALLOCATE:
17387 OMP_CLAUSE_DECL (nc)
17388 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17389 in_decl, NULL);
17390 OMP_CLAUSE_OPERAND (nc, 1)
17391 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17392 in_decl, /*integral_constant_expression_p=*/false);
17393 break;
17394 case OMP_CLAUSE_LINEAR:
17395 OMP_CLAUSE_DECL (nc)
17396 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17397 in_decl, NULL);
17398 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17399 {
17400 gcc_assert (!linear_no_step);
17401 linear_no_step = nc;
17402 }
17403 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17404 OMP_CLAUSE_LINEAR_STEP (nc)
17405 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17406 complain, in_decl, NULL);
17407 else
17408 OMP_CLAUSE_LINEAR_STEP (nc)
17409 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17410 in_decl,
17411 /*integral_constant_expression_p=*/false);
17412 break;
17413 case OMP_CLAUSE_NOWAIT:
17414 case OMP_CLAUSE_DEFAULT:
17415 case OMP_CLAUSE_UNTIED:
17416 case OMP_CLAUSE_MERGEABLE:
17417 case OMP_CLAUSE_INBRANCH:
17418 case OMP_CLAUSE_NOTINBRANCH:
17419 case OMP_CLAUSE_PROC_BIND:
17420 case OMP_CLAUSE_FOR:
17421 case OMP_CLAUSE_PARALLEL:
17422 case OMP_CLAUSE_SECTIONS:
17423 case OMP_CLAUSE_TASKGROUP:
17424 case OMP_CLAUSE_NOGROUP:
17425 case OMP_CLAUSE_THREADS:
17426 case OMP_CLAUSE_SIMD:
17427 case OMP_CLAUSE_DEFAULTMAP:
17428 case OMP_CLAUSE_ORDER:
17429 case OMP_CLAUSE_BIND:
17430 case OMP_CLAUSE_INDEPENDENT:
17431 case OMP_CLAUSE_AUTO:
17432 case OMP_CLAUSE_SEQ:
17433 case OMP_CLAUSE_IF_PRESENT:
17434 case OMP_CLAUSE_FINALIZE:
17435 break;
17436 default:
17437 gcc_unreachable ();
17438 }
17439 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17440 switch (OMP_CLAUSE_CODE (nc))
17441 {
17442 case OMP_CLAUSE_SHARED:
17443 case OMP_CLAUSE_PRIVATE:
17444 case OMP_CLAUSE_FIRSTPRIVATE:
17445 case OMP_CLAUSE_LASTPRIVATE:
17446 case OMP_CLAUSE_COPYPRIVATE:
17447 case OMP_CLAUSE_LINEAR:
17448 case OMP_CLAUSE_REDUCTION:
17449 case OMP_CLAUSE_IN_REDUCTION:
17450 case OMP_CLAUSE_TASK_REDUCTION:
17451 case OMP_CLAUSE_USE_DEVICE_PTR:
17452 case OMP_CLAUSE_USE_DEVICE_ADDR:
17453 case OMP_CLAUSE_IS_DEVICE_PTR:
17454 case OMP_CLAUSE_INCLUSIVE:
17455 case OMP_CLAUSE_EXCLUSIVE:
17456 case OMP_CLAUSE_ALLOCATE:
17457 /* tsubst_expr on SCOPE_REF results in returning
17458 finish_non_static_data_member result. Undo that here. */
17459 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17460 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17461 == IDENTIFIER_NODE))
17462 {
17463 tree t = OMP_CLAUSE_DECL (nc);
17464 tree v = t;
17465 while (v)
17466 switch (TREE_CODE (v))
17467 {
17468 case COMPONENT_REF:
17469 case MEM_REF:
17470 case INDIRECT_REF:
17471 CASE_CONVERT:
17472 case POINTER_PLUS_EXPR:
17473 v = TREE_OPERAND (v, 0);
17474 continue;
17475 case PARM_DECL:
17476 if (DECL_CONTEXT (v) == current_function_decl
17477 && DECL_ARTIFICIAL (v)
17478 && DECL_NAME (v) == this_identifier)
17479 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17480 /* FALLTHRU */
17481 default:
17482 v = NULL_TREE;
17483 break;
17484 }
17485 }
17486 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17487 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17488 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17489 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17490 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17491 {
17492 tree decl = OMP_CLAUSE_DECL (nc);
17493 if (VAR_P (decl))
17494 {
17495 retrofit_lang_decl (decl);
17496 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17497 }
17498 }
17499 break;
17500 default:
17501 break;
17502 }
17503 }
17504
17505 new_clauses = nreverse (new_clauses);
17506 if (ort != C_ORT_OMP_DECLARE_SIMD)
17507 {
17508 new_clauses = finish_omp_clauses (new_clauses, ort);
17509 if (linear_no_step)
17510 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17511 if (nc == linear_no_step)
17512 {
17513 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17514 break;
17515 }
17516 }
17517 return new_clauses;
17518 }
17519
17520 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17521
17522 static tree
17523 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17524 tree in_decl)
17525 {
17526 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17527
17528 tree purpose, value, chain;
17529
17530 if (t == NULL)
17531 return t;
17532
17533 if (TREE_CODE (t) != TREE_LIST)
17534 return tsubst_copy_and_build (t, args, complain, in_decl,
17535 /*function_p=*/false,
17536 /*integral_constant_expression_p=*/false);
17537
17538 if (t == void_list_node)
17539 return t;
17540
17541 purpose = TREE_PURPOSE (t);
17542 if (purpose)
17543 purpose = RECUR (purpose);
17544 value = TREE_VALUE (t);
17545 if (value)
17546 {
17547 if (TREE_CODE (value) != LABEL_DECL)
17548 value = RECUR (value);
17549 else
17550 {
17551 value = lookup_label (DECL_NAME (value));
17552 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17553 TREE_USED (value) = 1;
17554 }
17555 }
17556 chain = TREE_CHAIN (t);
17557 if (chain && chain != void_type_node)
17558 chain = RECUR (chain);
17559 return tree_cons (purpose, value, chain);
17560 #undef RECUR
17561 }
17562
17563 /* Used to temporarily communicate the list of #pragma omp parallel
17564 clauses to #pragma omp for instantiation if they are combined
17565 together. */
17566
17567 static tree *omp_parallel_combined_clauses;
17568
17569 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17570 tree *, unsigned int *);
17571
17572 /* Substitute one OMP_FOR iterator. */
17573
17574 static bool
17575 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17576 tree initv, tree condv, tree incrv, tree *clauses,
17577 tree args, tsubst_flags_t complain, tree in_decl,
17578 bool integral_constant_expression_p)
17579 {
17580 #define RECUR(NODE) \
17581 tsubst_expr ((NODE), args, complain, in_decl, \
17582 integral_constant_expression_p)
17583 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17584 bool ret = false;
17585
17586 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17587 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17588
17589 decl = TREE_OPERAND (init, 0);
17590 init = TREE_OPERAND (init, 1);
17591 tree decl_expr = NULL_TREE;
17592 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17593 if (range_for)
17594 {
17595 bool decomp = false;
17596 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17597 {
17598 tree v = DECL_VALUE_EXPR (decl);
17599 if (TREE_CODE (v) == ARRAY_REF
17600 && VAR_P (TREE_OPERAND (v, 0))
17601 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17602 {
17603 tree decomp_first = NULL_TREE;
17604 unsigned decomp_cnt = 0;
17605 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17606 maybe_push_decl (d);
17607 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17608 in_decl, &decomp_first, &decomp_cnt);
17609 decomp = true;
17610 if (d == error_mark_node)
17611 decl = error_mark_node;
17612 else
17613 for (unsigned int i = 0; i < decomp_cnt; i++)
17614 {
17615 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17616 {
17617 tree v = build_nt (ARRAY_REF, d,
17618 size_int (decomp_cnt - i - 1),
17619 NULL_TREE, NULL_TREE);
17620 SET_DECL_VALUE_EXPR (decomp_first, v);
17621 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17622 }
17623 fit_decomposition_lang_decl (decomp_first, d);
17624 decomp_first = DECL_CHAIN (decomp_first);
17625 }
17626 }
17627 }
17628 decl = tsubst_decl (decl, args, complain);
17629 if (!decomp)
17630 maybe_push_decl (decl);
17631 }
17632 else if (init && TREE_CODE (init) == DECL_EXPR)
17633 {
17634 /* We need to jump through some hoops to handle declarations in the
17635 init-statement, since we might need to handle auto deduction,
17636 but we need to keep control of initialization. */
17637 decl_expr = init;
17638 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17639 decl = tsubst_decl (decl, args, complain);
17640 }
17641 else
17642 {
17643 if (TREE_CODE (decl) == SCOPE_REF)
17644 {
17645 decl = RECUR (decl);
17646 if (TREE_CODE (decl) == COMPONENT_REF)
17647 {
17648 tree v = decl;
17649 while (v)
17650 switch (TREE_CODE (v))
17651 {
17652 case COMPONENT_REF:
17653 case MEM_REF:
17654 case INDIRECT_REF:
17655 CASE_CONVERT:
17656 case POINTER_PLUS_EXPR:
17657 v = TREE_OPERAND (v, 0);
17658 continue;
17659 case PARM_DECL:
17660 if (DECL_CONTEXT (v) == current_function_decl
17661 && DECL_ARTIFICIAL (v)
17662 && DECL_NAME (v) == this_identifier)
17663 {
17664 decl = TREE_OPERAND (decl, 1);
17665 decl = omp_privatize_field (decl, false);
17666 }
17667 /* FALLTHRU */
17668 default:
17669 v = NULL_TREE;
17670 break;
17671 }
17672 }
17673 }
17674 else
17675 decl = RECUR (decl);
17676 }
17677 if (init && TREE_CODE (init) == TREE_VEC)
17678 {
17679 init = copy_node (init);
17680 TREE_VEC_ELT (init, 0)
17681 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17682 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17683 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17684 }
17685 else
17686 init = RECUR (init);
17687
17688 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17689 {
17690 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17691 if (TREE_CODE (o) == TREE_LIST)
17692 TREE_VEC_ELT (orig_declv, i)
17693 = tree_cons (RECUR (TREE_PURPOSE (o)),
17694 RECUR (TREE_VALUE (o)),
17695 NULL_TREE);
17696 else
17697 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17698 }
17699
17700 if (range_for)
17701 {
17702 tree this_pre_body = NULL_TREE;
17703 tree orig_init = NULL_TREE;
17704 tree orig_decl = NULL_TREE;
17705 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17706 orig_init, cond, incr);
17707 if (orig_decl)
17708 {
17709 if (orig_declv == NULL_TREE)
17710 orig_declv = copy_node (declv);
17711 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17712 ret = true;
17713 }
17714 else if (orig_declv)
17715 TREE_VEC_ELT (orig_declv, i) = decl;
17716 }
17717
17718 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17719 if (!range_for && auto_node && init)
17720 TREE_TYPE (decl)
17721 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17722
17723 gcc_assert (!type_dependent_expression_p (decl));
17724
17725 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17726 {
17727 if (decl_expr)
17728 {
17729 /* Declare the variable, but don't let that initialize it. */
17730 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17731 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17732 RECUR (decl_expr);
17733 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17734 }
17735
17736 if (!range_for)
17737 {
17738 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17739 if (COMPARISON_CLASS_P (cond)
17740 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17741 {
17742 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17743 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17744 TREE_VEC_ELT (rhs, 0)
17745 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17746 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17747 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17748 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17749 lhs, rhs);
17750 }
17751 else
17752 cond = RECUR (cond);
17753 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17754 if (TREE_CODE (incr) == MODIFY_EXPR)
17755 {
17756 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17757 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17758 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17759 NOP_EXPR, rhs, complain);
17760 }
17761 else
17762 incr = RECUR (incr);
17763 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17764 TREE_VEC_ELT (orig_declv, i) = decl;
17765 }
17766 TREE_VEC_ELT (declv, i) = decl;
17767 TREE_VEC_ELT (initv, i) = init;
17768 TREE_VEC_ELT (condv, i) = cond;
17769 TREE_VEC_ELT (incrv, i) = incr;
17770 return ret;
17771 }
17772
17773 if (decl_expr)
17774 {
17775 /* Declare and initialize the variable. */
17776 RECUR (decl_expr);
17777 init = NULL_TREE;
17778 }
17779 else if (init)
17780 {
17781 tree *pc;
17782 int j;
17783 for (j = ((omp_parallel_combined_clauses == NULL
17784 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17785 {
17786 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17787 {
17788 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17789 && OMP_CLAUSE_DECL (*pc) == decl)
17790 break;
17791 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17792 && OMP_CLAUSE_DECL (*pc) == decl)
17793 {
17794 if (j)
17795 break;
17796 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17797 tree c = *pc;
17798 *pc = OMP_CLAUSE_CHAIN (c);
17799 OMP_CLAUSE_CHAIN (c) = *clauses;
17800 *clauses = c;
17801 }
17802 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17803 && OMP_CLAUSE_DECL (*pc) == decl)
17804 {
17805 error ("iteration variable %qD should not be firstprivate",
17806 decl);
17807 *pc = OMP_CLAUSE_CHAIN (*pc);
17808 }
17809 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17810 && OMP_CLAUSE_DECL (*pc) == decl)
17811 {
17812 error ("iteration variable %qD should not be reduction",
17813 decl);
17814 *pc = OMP_CLAUSE_CHAIN (*pc);
17815 }
17816 else
17817 pc = &OMP_CLAUSE_CHAIN (*pc);
17818 }
17819 if (*pc)
17820 break;
17821 }
17822 if (*pc == NULL_TREE)
17823 {
17824 tree c = build_omp_clause (input_location,
17825 TREE_CODE (t) == OMP_LOOP
17826 ? OMP_CLAUSE_LASTPRIVATE
17827 : OMP_CLAUSE_PRIVATE);
17828 OMP_CLAUSE_DECL (c) = decl;
17829 c = finish_omp_clauses (c, C_ORT_OMP);
17830 if (c)
17831 {
17832 OMP_CLAUSE_CHAIN (c) = *clauses;
17833 *clauses = c;
17834 }
17835 }
17836 }
17837 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17838 if (COMPARISON_CLASS_P (cond))
17839 {
17840 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17841 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17842 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17843 }
17844 else
17845 cond = RECUR (cond);
17846 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17847 switch (TREE_CODE (incr))
17848 {
17849 case PREINCREMENT_EXPR:
17850 case PREDECREMENT_EXPR:
17851 case POSTINCREMENT_EXPR:
17852 case POSTDECREMENT_EXPR:
17853 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17854 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17855 break;
17856 case MODIFY_EXPR:
17857 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17858 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17859 {
17860 tree rhs = TREE_OPERAND (incr, 1);
17861 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17862 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17863 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17864 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17865 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17866 rhs0, rhs1));
17867 }
17868 else
17869 incr = RECUR (incr);
17870 break;
17871 case MODOP_EXPR:
17872 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17873 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17874 {
17875 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17876 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17877 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17878 TREE_TYPE (decl), lhs,
17879 RECUR (TREE_OPERAND (incr, 2))));
17880 }
17881 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17882 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17883 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17884 {
17885 tree rhs = TREE_OPERAND (incr, 2);
17886 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17887 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17888 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17889 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17890 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17891 rhs0, rhs1));
17892 }
17893 else
17894 incr = RECUR (incr);
17895 break;
17896 default:
17897 incr = RECUR (incr);
17898 break;
17899 }
17900
17901 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17902 TREE_VEC_ELT (orig_declv, i) = decl;
17903 TREE_VEC_ELT (declv, i) = decl;
17904 TREE_VEC_ELT (initv, i) = init;
17905 TREE_VEC_ELT (condv, i) = cond;
17906 TREE_VEC_ELT (incrv, i) = incr;
17907 return false;
17908 #undef RECUR
17909 }
17910
17911 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17912 of OMP_TARGET's body. */
17913
17914 static tree
17915 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17916 {
17917 *walk_subtrees = 0;
17918 switch (TREE_CODE (*tp))
17919 {
17920 case OMP_TEAMS:
17921 return *tp;
17922 case BIND_EXPR:
17923 case STATEMENT_LIST:
17924 *walk_subtrees = 1;
17925 break;
17926 default:
17927 break;
17928 }
17929 return NULL_TREE;
17930 }
17931
17932 /* Helper function for tsubst_expr. For decomposition declaration
17933 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17934 also the corresponding decls representing the identifiers
17935 of the decomposition declaration. Return DECL if successful
17936 or error_mark_node otherwise, set *FIRST to the first decl
17937 in the list chained through DECL_CHAIN and *CNT to the number
17938 of such decls. */
17939
17940 static tree
17941 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17942 tsubst_flags_t complain, tree in_decl, tree *first,
17943 unsigned int *cnt)
17944 {
17945 tree decl2, decl3, prev = decl;
17946 *cnt = 0;
17947 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17948 for (decl2 = DECL_CHAIN (pattern_decl);
17949 decl2
17950 && VAR_P (decl2)
17951 && DECL_DECOMPOSITION_P (decl2)
17952 && DECL_NAME (decl2);
17953 decl2 = DECL_CHAIN (decl2))
17954 {
17955 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17956 {
17957 gcc_assert (errorcount);
17958 return error_mark_node;
17959 }
17960 (*cnt)++;
17961 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17962 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17963 tree v = DECL_VALUE_EXPR (decl2);
17964 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17965 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17966 decl3 = tsubst (decl2, args, complain, in_decl);
17967 SET_DECL_VALUE_EXPR (decl2, v);
17968 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17969 if (VAR_P (decl3))
17970 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17971 else
17972 {
17973 gcc_assert (errorcount);
17974 decl = error_mark_node;
17975 continue;
17976 }
17977 maybe_push_decl (decl3);
17978 if (error_operand_p (decl3))
17979 decl = error_mark_node;
17980 else if (decl != error_mark_node
17981 && DECL_CHAIN (decl3) != prev
17982 && decl != prev)
17983 {
17984 gcc_assert (errorcount);
17985 decl = error_mark_node;
17986 }
17987 else
17988 prev = decl3;
17989 }
17990 *first = prev;
17991 return decl;
17992 }
17993
17994 /* Return the proper local_specialization for init-capture pack DECL. */
17995
17996 static tree
17997 lookup_init_capture_pack (tree decl)
17998 {
17999 /* We handle normal pack captures by forwarding to the specialization of the
18000 captured parameter. We can't do that for pack init-captures; we need them
18001 to have their own local_specialization. We created the individual
18002 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18003 when we process the DECL_EXPR for the pack init-capture in the template.
18004 So, how do we find them? We don't know the capture proxy pack when
18005 building the individual resulting proxies, and we don't know the
18006 individual proxies when instantiating the pack. What we have in common is
18007 the FIELD_DECL.
18008
18009 So...when we instantiate the FIELD_DECL, we stick the result in
18010 local_specializations. Then at the DECL_EXPR we look up that result, see
18011 how many elements it has, synthesize the names, and look them up. */
18012
18013 tree cname = DECL_NAME (decl);
18014 tree val = DECL_VALUE_EXPR (decl);
18015 tree field = TREE_OPERAND (val, 1);
18016 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18017 tree fpack = retrieve_local_specialization (field);
18018 if (fpack == error_mark_node)
18019 return error_mark_node;
18020
18021 int len = 1;
18022 tree vec = NULL_TREE;
18023 tree r = NULL_TREE;
18024 if (TREE_CODE (fpack) == TREE_VEC)
18025 {
18026 len = TREE_VEC_LENGTH (fpack);
18027 vec = make_tree_vec (len);
18028 r = make_node (NONTYPE_ARGUMENT_PACK);
18029 SET_ARGUMENT_PACK_ARGS (r, vec);
18030 }
18031 for (int i = 0; i < len; ++i)
18032 {
18033 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18034 tree elt = lookup_name (ename);
18035 if (vec)
18036 TREE_VEC_ELT (vec, i) = elt;
18037 else
18038 r = elt;
18039 }
18040 return r;
18041 }
18042
18043 /* Like tsubst_copy for expressions, etc. but also does semantic
18044 processing. */
18045
18046 tree
18047 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18048 bool integral_constant_expression_p)
18049 {
18050 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18051 #define RECUR(NODE) \
18052 tsubst_expr ((NODE), args, complain, in_decl, \
18053 integral_constant_expression_p)
18054
18055 tree stmt, tmp;
18056 tree r;
18057 location_t loc;
18058
18059 if (t == NULL_TREE || t == error_mark_node)
18060 return t;
18061
18062 loc = input_location;
18063 if (location_t eloc = cp_expr_location (t))
18064 input_location = eloc;
18065 if (STATEMENT_CODE_P (TREE_CODE (t)))
18066 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18067
18068 switch (TREE_CODE (t))
18069 {
18070 case STATEMENT_LIST:
18071 {
18072 tree_stmt_iterator i;
18073 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
18074 RECUR (tsi_stmt (i));
18075 break;
18076 }
18077
18078 case CTOR_INITIALIZER:
18079 finish_mem_initializers (tsubst_initializer_list
18080 (TREE_OPERAND (t, 0), args));
18081 break;
18082
18083 case RETURN_EXPR:
18084 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18085 break;
18086
18087 case CO_RETURN_EXPR:
18088 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18089 break;
18090
18091 case CO_YIELD_EXPR:
18092 stmt = finish_co_yield_expr (input_location,
18093 RECUR (TREE_OPERAND (t, 0)));
18094 RETURN (stmt);
18095 break;
18096
18097 case CO_AWAIT_EXPR:
18098 stmt = finish_co_await_expr (input_location,
18099 RECUR (TREE_OPERAND (t, 0)));
18100 RETURN (stmt);
18101 break;
18102
18103 case EXPR_STMT:
18104 tmp = RECUR (EXPR_STMT_EXPR (t));
18105 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18106 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18107 else
18108 finish_expr_stmt (tmp);
18109 break;
18110
18111 case USING_STMT:
18112 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18113 break;
18114
18115 case DECL_EXPR:
18116 {
18117 tree decl, pattern_decl;
18118 tree init;
18119
18120 pattern_decl = decl = DECL_EXPR_DECL (t);
18121 if (TREE_CODE (decl) == LABEL_DECL)
18122 finish_label_decl (DECL_NAME (decl));
18123 else if (TREE_CODE (decl) == USING_DECL)
18124 {
18125 /* We cannot have a member-using decl here (until 'using
18126 enum T' is a thing). */
18127 gcc_checking_assert (!DECL_DEPENDENT_P (decl));
18128
18129 /* This must be a non-dependent using-decl, and we'll have
18130 used the names it found during template parsing. We do
18131 not want to do the lookup again, because we might not
18132 find the things we found then. (Again, using enum T
18133 might mean we have to do things here.) */
18134 tree scope = USING_DECL_SCOPE (decl);
18135 gcc_checking_assert (scope
18136 == tsubst (scope, args, complain, in_decl));
18137 /* We still need to push the bindings so that we can look up
18138 this name later. */
18139 push_using_decl_bindings (decl);
18140 }
18141 else if (is_capture_proxy (decl)
18142 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18143 {
18144 /* We're in tsubst_lambda_expr, we've already inserted a new
18145 capture proxy, so look it up and register it. */
18146 tree inst;
18147 if (!DECL_PACK_P (decl))
18148 {
18149 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18150 LOOK_want::HIDDEN_LAMBDA);
18151 gcc_assert (inst != decl && is_capture_proxy (inst));
18152 }
18153 else if (is_normal_capture_proxy (decl))
18154 {
18155 inst = (retrieve_local_specialization
18156 (DECL_CAPTURED_VARIABLE (decl)));
18157 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18158 || DECL_PACK_P (inst));
18159 }
18160 else
18161 inst = lookup_init_capture_pack (decl);
18162
18163 register_local_specialization (inst, decl);
18164 break;
18165 }
18166 else if (DECL_PRETTY_FUNCTION_P (decl))
18167 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18168 DECL_NAME (decl),
18169 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18170 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18171 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18172 /* Don't copy the old closure; we'll create a new one in
18173 tsubst_lambda_expr. */
18174 break;
18175 else
18176 {
18177 init = DECL_INITIAL (decl);
18178 decl = tsubst (decl, args, complain, in_decl);
18179 if (decl != error_mark_node)
18180 {
18181 /* By marking the declaration as instantiated, we avoid
18182 trying to instantiate it. Since instantiate_decl can't
18183 handle local variables, and since we've already done
18184 all that needs to be done, that's the right thing to
18185 do. */
18186 if (VAR_P (decl))
18187 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18188 if (VAR_P (decl) && !DECL_NAME (decl)
18189 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18190 /* Anonymous aggregates are a special case. */
18191 finish_anon_union (decl);
18192 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18193 {
18194 DECL_CONTEXT (decl) = current_function_decl;
18195 if (DECL_NAME (decl) == this_identifier)
18196 {
18197 tree lam = DECL_CONTEXT (current_function_decl);
18198 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18199 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18200 }
18201 insert_capture_proxy (decl);
18202 }
18203 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18204 /* We already did a pushtag. */;
18205 else if (VAR_OR_FUNCTION_DECL_P (decl)
18206 && DECL_LOCAL_DECL_P (decl))
18207 {
18208 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18209 DECL_CONTEXT (decl) = NULL_TREE;
18210 decl = pushdecl (decl);
18211 if (TREE_CODE (decl) == FUNCTION_DECL
18212 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18213 && cp_check_omp_declare_reduction (decl))
18214 instantiate_body (pattern_decl, args, decl, true);
18215 }
18216 else
18217 {
18218 bool const_init = false;
18219 unsigned int cnt = 0;
18220 tree first = NULL_TREE, ndecl = error_mark_node;
18221 maybe_push_decl (decl);
18222
18223 if (VAR_P (decl)
18224 && DECL_DECOMPOSITION_P (decl)
18225 && TREE_TYPE (pattern_decl) != error_mark_node)
18226 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18227 complain, in_decl, &first,
18228 &cnt);
18229
18230 init = tsubst_init (init, decl, args, complain, in_decl);
18231
18232 if (VAR_P (decl))
18233 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18234 (pattern_decl));
18235
18236 if (ndecl != error_mark_node)
18237 cp_maybe_mangle_decomp (ndecl, first, cnt);
18238
18239 /* In a non-template function, VLA type declarations are
18240 handled in grokdeclarator; for templates, handle them
18241 now. */
18242 predeclare_vla (decl);
18243
18244 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18245
18246 if (ndecl != error_mark_node)
18247 cp_finish_decomp (ndecl, first, cnt);
18248 }
18249 }
18250 }
18251
18252 break;
18253 }
18254
18255 case FOR_STMT:
18256 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18257 RECUR (FOR_INIT_STMT (t));
18258 finish_init_stmt (stmt);
18259 tmp = RECUR (FOR_COND (t));
18260 finish_for_cond (tmp, stmt, false, 0);
18261 tmp = RECUR (FOR_EXPR (t));
18262 finish_for_expr (tmp, stmt);
18263 {
18264 bool prev = note_iteration_stmt_body_start ();
18265 RECUR (FOR_BODY (t));
18266 note_iteration_stmt_body_end (prev);
18267 }
18268 finish_for_stmt (stmt);
18269 break;
18270
18271 case RANGE_FOR_STMT:
18272 {
18273 /* Construct another range_for, if this is not a final
18274 substitution (for inside a generic lambda of a
18275 template). Otherwise convert to a regular for. */
18276 tree decl, expr;
18277 stmt = (processing_template_decl
18278 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18279 : begin_for_stmt (NULL_TREE, NULL_TREE));
18280 RECUR (RANGE_FOR_INIT_STMT (t));
18281 decl = RANGE_FOR_DECL (t);
18282 decl = tsubst (decl, args, complain, in_decl);
18283 maybe_push_decl (decl);
18284 expr = RECUR (RANGE_FOR_EXPR (t));
18285
18286 tree decomp_first = NULL_TREE;
18287 unsigned decomp_cnt = 0;
18288 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18289 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18290 complain, in_decl,
18291 &decomp_first, &decomp_cnt);
18292
18293 if (processing_template_decl)
18294 {
18295 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18296 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18297 finish_range_for_decl (stmt, decl, expr);
18298 if (decomp_first && decl != error_mark_node)
18299 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18300 }
18301 else
18302 {
18303 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18304 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18305 stmt = cp_convert_range_for (stmt, decl, expr,
18306 decomp_first, decomp_cnt,
18307 RANGE_FOR_IVDEP (t), unroll);
18308 }
18309
18310 bool prev = note_iteration_stmt_body_start ();
18311 RECUR (RANGE_FOR_BODY (t));
18312 note_iteration_stmt_body_end (prev);
18313 finish_for_stmt (stmt);
18314 }
18315 break;
18316
18317 case WHILE_STMT:
18318 stmt = begin_while_stmt ();
18319 tmp = RECUR (WHILE_COND (t));
18320 finish_while_stmt_cond (tmp, stmt, false, 0);
18321 {
18322 bool prev = note_iteration_stmt_body_start ();
18323 RECUR (WHILE_BODY (t));
18324 note_iteration_stmt_body_end (prev);
18325 }
18326 finish_while_stmt (stmt);
18327 break;
18328
18329 case DO_STMT:
18330 stmt = begin_do_stmt ();
18331 {
18332 bool prev = note_iteration_stmt_body_start ();
18333 RECUR (DO_BODY (t));
18334 note_iteration_stmt_body_end (prev);
18335 }
18336 finish_do_body (stmt);
18337 tmp = RECUR (DO_COND (t));
18338 finish_do_stmt (tmp, stmt, false, 0);
18339 break;
18340
18341 case IF_STMT:
18342 stmt = begin_if_stmt ();
18343 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18344 if (IF_STMT_CONSTEXPR_P (t))
18345 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18346 tmp = RECUR (IF_COND (t));
18347 tmp = finish_if_stmt_cond (tmp, stmt);
18348 if (IF_STMT_CONSTEXPR_P (t)
18349 && instantiation_dependent_expression_p (tmp))
18350 {
18351 /* We're partially instantiating a generic lambda, but the condition
18352 of the constexpr if is still dependent. Don't substitute into the
18353 branches now, just remember the template arguments. */
18354 do_poplevel (IF_SCOPE (stmt));
18355 IF_COND (stmt) = IF_COND (t);
18356 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18357 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18358 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18359 add_stmt (stmt);
18360 break;
18361 }
18362 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18363 /* Don't instantiate the THEN_CLAUSE. */;
18364 else
18365 {
18366 tree folded = fold_non_dependent_expr (tmp, complain);
18367 bool inhibit = integer_zerop (folded);
18368 if (inhibit)
18369 ++c_inhibit_evaluation_warnings;
18370 RECUR (THEN_CLAUSE (t));
18371 if (inhibit)
18372 --c_inhibit_evaluation_warnings;
18373 }
18374 finish_then_clause (stmt);
18375
18376 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18377 /* Don't instantiate the ELSE_CLAUSE. */;
18378 else if (ELSE_CLAUSE (t))
18379 {
18380 tree folded = fold_non_dependent_expr (tmp, complain);
18381 bool inhibit = integer_nonzerop (folded);
18382 begin_else_clause (stmt);
18383 if (inhibit)
18384 ++c_inhibit_evaluation_warnings;
18385 RECUR (ELSE_CLAUSE (t));
18386 if (inhibit)
18387 --c_inhibit_evaluation_warnings;
18388 finish_else_clause (stmt);
18389 }
18390
18391 finish_if_stmt (stmt);
18392 break;
18393
18394 case BIND_EXPR:
18395 if (BIND_EXPR_BODY_BLOCK (t))
18396 stmt = begin_function_body ();
18397 else
18398 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18399 ? BCS_TRY_BLOCK : 0);
18400
18401 RECUR (BIND_EXPR_BODY (t));
18402
18403 if (BIND_EXPR_BODY_BLOCK (t))
18404 finish_function_body (stmt);
18405 else
18406 finish_compound_stmt (stmt);
18407 break;
18408
18409 case BREAK_STMT:
18410 finish_break_stmt ();
18411 break;
18412
18413 case CONTINUE_STMT:
18414 finish_continue_stmt ();
18415 break;
18416
18417 case SWITCH_STMT:
18418 stmt = begin_switch_stmt ();
18419 tmp = RECUR (SWITCH_STMT_COND (t));
18420 finish_switch_cond (tmp, stmt);
18421 RECUR (SWITCH_STMT_BODY (t));
18422 finish_switch_stmt (stmt);
18423 break;
18424
18425 case CASE_LABEL_EXPR:
18426 {
18427 tree decl = CASE_LABEL (t);
18428 tree low = RECUR (CASE_LOW (t));
18429 tree high = RECUR (CASE_HIGH (t));
18430 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18431 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18432 {
18433 tree label = CASE_LABEL (l);
18434 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18435 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18436 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18437 }
18438 }
18439 break;
18440
18441 case LABEL_EXPR:
18442 {
18443 tree decl = LABEL_EXPR_LABEL (t);
18444 tree label;
18445
18446 label = finish_label_stmt (DECL_NAME (decl));
18447 if (TREE_CODE (label) == LABEL_DECL)
18448 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18449 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18450 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18451 }
18452 break;
18453
18454 case GOTO_EXPR:
18455 tmp = GOTO_DESTINATION (t);
18456 if (TREE_CODE (tmp) != LABEL_DECL)
18457 /* Computed goto's must be tsubst'd into. On the other hand,
18458 non-computed gotos must not be; the identifier in question
18459 will have no binding. */
18460 tmp = RECUR (tmp);
18461 else
18462 tmp = DECL_NAME (tmp);
18463 finish_goto_stmt (tmp);
18464 break;
18465
18466 case ASM_EXPR:
18467 {
18468 tree string = RECUR (ASM_STRING (t));
18469 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18470 complain, in_decl);
18471 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18472 complain, in_decl);
18473 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18474 complain, in_decl);
18475 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18476 complain, in_decl);
18477 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18478 outputs, inputs, clobbers, labels,
18479 ASM_INLINE_P (t));
18480 tree asm_expr = tmp;
18481 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18482 asm_expr = TREE_OPERAND (asm_expr, 0);
18483 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18484 }
18485 break;
18486
18487 case TRY_BLOCK:
18488 if (CLEANUP_P (t))
18489 {
18490 stmt = begin_try_block ();
18491 RECUR (TRY_STMTS (t));
18492 finish_cleanup_try_block (stmt);
18493 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18494 }
18495 else
18496 {
18497 tree compound_stmt = NULL_TREE;
18498
18499 if (FN_TRY_BLOCK_P (t))
18500 stmt = begin_function_try_block (&compound_stmt);
18501 else
18502 stmt = begin_try_block ();
18503
18504 RECUR (TRY_STMTS (t));
18505
18506 if (FN_TRY_BLOCK_P (t))
18507 finish_function_try_block (stmt);
18508 else
18509 finish_try_block (stmt);
18510
18511 RECUR (TRY_HANDLERS (t));
18512 if (FN_TRY_BLOCK_P (t))
18513 finish_function_handler_sequence (stmt, compound_stmt);
18514 else
18515 finish_handler_sequence (stmt);
18516 }
18517 break;
18518
18519 case HANDLER:
18520 {
18521 tree decl = HANDLER_PARMS (t);
18522
18523 if (decl)
18524 {
18525 decl = tsubst (decl, args, complain, in_decl);
18526 /* Prevent instantiate_decl from trying to instantiate
18527 this variable. We've already done all that needs to be
18528 done. */
18529 if (decl != error_mark_node)
18530 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18531 }
18532 stmt = begin_handler ();
18533 finish_handler_parms (decl, stmt);
18534 RECUR (HANDLER_BODY (t));
18535 finish_handler (stmt);
18536 }
18537 break;
18538
18539 case TAG_DEFN:
18540 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18541 if (CLASS_TYPE_P (tmp))
18542 {
18543 /* Local classes are not independent templates; they are
18544 instantiated along with their containing function. And this
18545 way we don't have to deal with pushing out of one local class
18546 to instantiate a member of another local class. */
18547 /* Closures are handled by the LAMBDA_EXPR. */
18548 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18549 complete_type (tmp);
18550 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18551 if ((VAR_P (fld)
18552 || (TREE_CODE (fld) == FUNCTION_DECL
18553 && !DECL_ARTIFICIAL (fld)))
18554 && DECL_TEMPLATE_INSTANTIATION (fld))
18555 instantiate_decl (fld, /*defer_ok=*/false,
18556 /*expl_inst_class=*/false);
18557 }
18558 break;
18559
18560 case STATIC_ASSERT:
18561 {
18562 tree condition;
18563
18564 ++c_inhibit_evaluation_warnings;
18565 condition =
18566 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18567 args,
18568 complain, in_decl,
18569 /*integral_constant_expression_p=*/true);
18570 --c_inhibit_evaluation_warnings;
18571
18572 finish_static_assert (condition,
18573 STATIC_ASSERT_MESSAGE (t),
18574 STATIC_ASSERT_SOURCE_LOCATION (t),
18575 /*member_p=*/false, /*show_expr_p=*/true);
18576 }
18577 break;
18578
18579 case OACC_KERNELS:
18580 case OACC_PARALLEL:
18581 case OACC_SERIAL:
18582 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18583 in_decl);
18584 stmt = begin_omp_parallel ();
18585 RECUR (OMP_BODY (t));
18586 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18587 break;
18588
18589 case OMP_PARALLEL:
18590 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18591 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18592 complain, in_decl);
18593 if (OMP_PARALLEL_COMBINED (t))
18594 omp_parallel_combined_clauses = &tmp;
18595 stmt = begin_omp_parallel ();
18596 RECUR (OMP_PARALLEL_BODY (t));
18597 gcc_assert (omp_parallel_combined_clauses == NULL);
18598 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18599 = OMP_PARALLEL_COMBINED (t);
18600 pop_omp_privatization_clauses (r);
18601 break;
18602
18603 case OMP_TASK:
18604 if (OMP_TASK_BODY (t) == NULL_TREE)
18605 {
18606 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18607 complain, in_decl);
18608 t = copy_node (t);
18609 OMP_TASK_CLAUSES (t) = tmp;
18610 add_stmt (t);
18611 break;
18612 }
18613 r = push_omp_privatization_clauses (false);
18614 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18615 complain, in_decl);
18616 stmt = begin_omp_task ();
18617 RECUR (OMP_TASK_BODY (t));
18618 finish_omp_task (tmp, stmt);
18619 pop_omp_privatization_clauses (r);
18620 break;
18621
18622 case OMP_FOR:
18623 case OMP_LOOP:
18624 case OMP_SIMD:
18625 case OMP_DISTRIBUTE:
18626 case OMP_TASKLOOP:
18627 case OACC_LOOP:
18628 {
18629 tree clauses, body, pre_body;
18630 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18631 tree orig_declv = NULL_TREE;
18632 tree incrv = NULL_TREE;
18633 enum c_omp_region_type ort = C_ORT_OMP;
18634 bool any_range_for = false;
18635 int i;
18636
18637 if (TREE_CODE (t) == OACC_LOOP)
18638 ort = C_ORT_ACC;
18639
18640 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18641 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18642 in_decl);
18643 if (OMP_FOR_INIT (t) != NULL_TREE)
18644 {
18645 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18646 if (OMP_FOR_ORIG_DECLS (t))
18647 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18648 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18649 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18650 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18651 }
18652
18653 keep_next_level (true);
18654 stmt = begin_omp_structured_block ();
18655
18656 pre_body = push_stmt_list ();
18657 RECUR (OMP_FOR_PRE_BODY (t));
18658 pre_body = pop_stmt_list (pre_body);
18659
18660 if (OMP_FOR_INIT (t) != NULL_TREE)
18661 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18662 any_range_for
18663 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18664 condv, incrv, &clauses, args,
18665 complain, in_decl,
18666 integral_constant_expression_p);
18667 omp_parallel_combined_clauses = NULL;
18668
18669 if (any_range_for)
18670 {
18671 gcc_assert (orig_declv);
18672 body = begin_omp_structured_block ();
18673 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18674 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18675 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18676 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18677 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18678 TREE_VEC_ELT (declv, i));
18679 }
18680 else
18681 body = push_stmt_list ();
18682 RECUR (OMP_FOR_BODY (t));
18683 if (any_range_for)
18684 body = finish_omp_structured_block (body);
18685 else
18686 body = pop_stmt_list (body);
18687
18688 if (OMP_FOR_INIT (t) != NULL_TREE)
18689 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18690 orig_declv, initv, condv, incrv, body, pre_body,
18691 NULL, clauses);
18692 else
18693 {
18694 t = make_node (TREE_CODE (t));
18695 TREE_TYPE (t) = void_type_node;
18696 OMP_FOR_BODY (t) = body;
18697 OMP_FOR_PRE_BODY (t) = pre_body;
18698 OMP_FOR_CLAUSES (t) = clauses;
18699 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18700 add_stmt (t);
18701 }
18702
18703 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18704 t));
18705 pop_omp_privatization_clauses (r);
18706 }
18707 break;
18708
18709 case OMP_SECTIONS:
18710 omp_parallel_combined_clauses = NULL;
18711 /* FALLTHRU */
18712 case OMP_SINGLE:
18713 case OMP_TEAMS:
18714 case OMP_CRITICAL:
18715 case OMP_TASKGROUP:
18716 case OMP_SCAN:
18717 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18718 && OMP_TEAMS_COMBINED (t));
18719 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18720 in_decl);
18721 if (TREE_CODE (t) == OMP_TEAMS)
18722 {
18723 keep_next_level (true);
18724 stmt = begin_omp_structured_block ();
18725 RECUR (OMP_BODY (t));
18726 stmt = finish_omp_structured_block (stmt);
18727 }
18728 else
18729 {
18730 stmt = push_stmt_list ();
18731 RECUR (OMP_BODY (t));
18732 stmt = pop_stmt_list (stmt);
18733 }
18734
18735 if (TREE_CODE (t) == OMP_CRITICAL
18736 && tmp != NULL_TREE
18737 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18738 {
18739 error_at (OMP_CLAUSE_LOCATION (tmp),
18740 "%<#pragma omp critical%> with %<hint%> clause requires "
18741 "a name, except when %<omp_sync_hint_none%> is used");
18742 RETURN (error_mark_node);
18743 }
18744 t = copy_node (t);
18745 OMP_BODY (t) = stmt;
18746 OMP_CLAUSES (t) = tmp;
18747 add_stmt (t);
18748 pop_omp_privatization_clauses (r);
18749 break;
18750
18751 case OMP_DEPOBJ:
18752 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18753 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18754 {
18755 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18756 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18757 {
18758 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18759 args, complain, in_decl);
18760 if (tmp == NULL_TREE)
18761 tmp = error_mark_node;
18762 }
18763 else
18764 {
18765 kind = (enum omp_clause_depend_kind)
18766 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18767 tmp = NULL_TREE;
18768 }
18769 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18770 }
18771 else
18772 finish_omp_depobj (EXPR_LOCATION (t), r,
18773 OMP_CLAUSE_DEPEND_SOURCE,
18774 OMP_DEPOBJ_CLAUSES (t));
18775 break;
18776
18777 case OACC_DATA:
18778 case OMP_TARGET_DATA:
18779 case OMP_TARGET:
18780 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18781 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18782 in_decl);
18783 keep_next_level (true);
18784 stmt = begin_omp_structured_block ();
18785
18786 RECUR (OMP_BODY (t));
18787 stmt = finish_omp_structured_block (stmt);
18788
18789 t = copy_node (t);
18790 OMP_BODY (t) = stmt;
18791 OMP_CLAUSES (t) = tmp;
18792 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18793 {
18794 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18795 if (teams)
18796 {
18797 /* For combined target teams, ensure the num_teams and
18798 thread_limit clause expressions are evaluated on the host,
18799 before entering the target construct. */
18800 tree c;
18801 for (c = OMP_TEAMS_CLAUSES (teams);
18802 c; c = OMP_CLAUSE_CHAIN (c))
18803 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18804 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18805 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18806 {
18807 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18808 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18809 if (expr == error_mark_node)
18810 continue;
18811 tmp = TARGET_EXPR_SLOT (expr);
18812 add_stmt (expr);
18813 OMP_CLAUSE_OPERAND (c, 0) = expr;
18814 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18815 OMP_CLAUSE_FIRSTPRIVATE);
18816 OMP_CLAUSE_DECL (tc) = tmp;
18817 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18818 OMP_TARGET_CLAUSES (t) = tc;
18819 }
18820 }
18821 }
18822 add_stmt (t);
18823 break;
18824
18825 case OACC_DECLARE:
18826 t = copy_node (t);
18827 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18828 complain, in_decl);
18829 OACC_DECLARE_CLAUSES (t) = tmp;
18830 add_stmt (t);
18831 break;
18832
18833 case OMP_TARGET_UPDATE:
18834 case OMP_TARGET_ENTER_DATA:
18835 case OMP_TARGET_EXIT_DATA:
18836 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18837 complain, in_decl);
18838 t = copy_node (t);
18839 OMP_STANDALONE_CLAUSES (t) = tmp;
18840 add_stmt (t);
18841 break;
18842
18843 case OACC_CACHE:
18844 case OACC_ENTER_DATA:
18845 case OACC_EXIT_DATA:
18846 case OACC_UPDATE:
18847 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18848 complain, in_decl);
18849 t = copy_node (t);
18850 OMP_STANDALONE_CLAUSES (t) = tmp;
18851 add_stmt (t);
18852 break;
18853
18854 case OMP_ORDERED:
18855 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18856 complain, in_decl);
18857 stmt = push_stmt_list ();
18858 RECUR (OMP_BODY (t));
18859 stmt = pop_stmt_list (stmt);
18860
18861 t = copy_node (t);
18862 OMP_BODY (t) = stmt;
18863 OMP_ORDERED_CLAUSES (t) = tmp;
18864 add_stmt (t);
18865 break;
18866
18867 case OMP_MASTER:
18868 omp_parallel_combined_clauses = NULL;
18869 /* FALLTHRU */
18870 case OMP_SECTION:
18871 stmt = push_stmt_list ();
18872 RECUR (OMP_BODY (t));
18873 stmt = pop_stmt_list (stmt);
18874
18875 t = copy_node (t);
18876 OMP_BODY (t) = stmt;
18877 add_stmt (t);
18878 break;
18879
18880 case OMP_ATOMIC:
18881 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18882 tmp = NULL_TREE;
18883 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18884 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18885 complain, in_decl);
18886 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18887 {
18888 tree op1 = TREE_OPERAND (t, 1);
18889 tree rhs1 = NULL_TREE;
18890 tree lhs, rhs;
18891 if (TREE_CODE (op1) == COMPOUND_EXPR)
18892 {
18893 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18894 op1 = TREE_OPERAND (op1, 1);
18895 }
18896 lhs = RECUR (TREE_OPERAND (op1, 0));
18897 rhs = RECUR (TREE_OPERAND (op1, 1));
18898 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18899 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18900 OMP_ATOMIC_MEMORY_ORDER (t));
18901 }
18902 else
18903 {
18904 tree op1 = TREE_OPERAND (t, 1);
18905 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18906 tree rhs1 = NULL_TREE;
18907 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18908 enum tree_code opcode = NOP_EXPR;
18909 if (code == OMP_ATOMIC_READ)
18910 {
18911 v = RECUR (TREE_OPERAND (op1, 0));
18912 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18913 }
18914 else if (code == OMP_ATOMIC_CAPTURE_OLD
18915 || code == OMP_ATOMIC_CAPTURE_NEW)
18916 {
18917 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18918 v = RECUR (TREE_OPERAND (op1, 0));
18919 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18920 if (TREE_CODE (op11) == COMPOUND_EXPR)
18921 {
18922 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18923 op11 = TREE_OPERAND (op11, 1);
18924 }
18925 lhs = RECUR (TREE_OPERAND (op11, 0));
18926 rhs = RECUR (TREE_OPERAND (op11, 1));
18927 opcode = TREE_CODE (op11);
18928 if (opcode == MODIFY_EXPR)
18929 opcode = NOP_EXPR;
18930 }
18931 else
18932 {
18933 code = OMP_ATOMIC;
18934 lhs = RECUR (TREE_OPERAND (op1, 0));
18935 rhs = RECUR (TREE_OPERAND (op1, 1));
18936 }
18937 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18938 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18939 }
18940 break;
18941
18942 case TRANSACTION_EXPR:
18943 {
18944 int flags = 0;
18945 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18946 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18947
18948 if (TRANSACTION_EXPR_IS_STMT (t))
18949 {
18950 tree body = TRANSACTION_EXPR_BODY (t);
18951 tree noex = NULL_TREE;
18952 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18953 {
18954 noex = MUST_NOT_THROW_COND (body);
18955 if (noex == NULL_TREE)
18956 noex = boolean_true_node;
18957 body = TREE_OPERAND (body, 0);
18958 }
18959 stmt = begin_transaction_stmt (input_location, NULL, flags);
18960 RECUR (body);
18961 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18962 }
18963 else
18964 {
18965 stmt = build_transaction_expr (EXPR_LOCATION (t),
18966 RECUR (TRANSACTION_EXPR_BODY (t)),
18967 flags, NULL_TREE);
18968 RETURN (stmt);
18969 }
18970 }
18971 break;
18972
18973 case MUST_NOT_THROW_EXPR:
18974 {
18975 tree op0 = RECUR (TREE_OPERAND (t, 0));
18976 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18977 RETURN (build_must_not_throw_expr (op0, cond));
18978 }
18979
18980 case EXPR_PACK_EXPANSION:
18981 error ("invalid use of pack expansion expression");
18982 RETURN (error_mark_node);
18983
18984 case NONTYPE_ARGUMENT_PACK:
18985 error ("use %<...%> to expand argument pack");
18986 RETURN (error_mark_node);
18987
18988 case COMPOUND_EXPR:
18989 tmp = RECUR (TREE_OPERAND (t, 0));
18990 if (tmp == NULL_TREE)
18991 /* If the first operand was a statement, we're done with it. */
18992 RETURN (RECUR (TREE_OPERAND (t, 1)));
18993 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18994 RECUR (TREE_OPERAND (t, 1)),
18995 complain));
18996
18997 case ANNOTATE_EXPR:
18998 tmp = RECUR (TREE_OPERAND (t, 0));
18999 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19000 TREE_TYPE (tmp), tmp,
19001 RECUR (TREE_OPERAND (t, 1)),
19002 RECUR (TREE_OPERAND (t, 2))));
19003
19004 case PREDICT_EXPR:
19005 RETURN (add_stmt (copy_node (t)));
19006
19007 default:
19008 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19009
19010 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19011 /*function_p=*/false,
19012 integral_constant_expression_p));
19013 }
19014
19015 RETURN (NULL_TREE);
19016 out:
19017 input_location = loc;
19018 return r;
19019 #undef RECUR
19020 #undef RETURN
19021 }
19022
19023 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19024 function. For description of the body see comment above
19025 cp_parser_omp_declare_reduction_exprs. */
19026
19027 static void
19028 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19029 {
19030 if (t == NULL_TREE || t == error_mark_node)
19031 return;
19032
19033 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19034
19035 tree_stmt_iterator tsi;
19036 int i;
19037 tree stmts[7];
19038 memset (stmts, 0, sizeof stmts);
19039 for (i = 0, tsi = tsi_start (t);
19040 i < 7 && !tsi_end_p (tsi);
19041 i++, tsi_next (&tsi))
19042 stmts[i] = tsi_stmt (tsi);
19043 gcc_assert (tsi_end_p (tsi));
19044
19045 if (i >= 3)
19046 {
19047 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19048 && TREE_CODE (stmts[1]) == DECL_EXPR);
19049 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19050 args, complain, in_decl);
19051 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19052 args, complain, in_decl);
19053 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19054 expect to be pushing it. */
19055 DECL_CONTEXT (omp_out) = current_function_decl;
19056 DECL_CONTEXT (omp_in) = current_function_decl;
19057 keep_next_level (true);
19058 tree block = begin_omp_structured_block ();
19059 tsubst_expr (stmts[2], args, complain, in_decl, false);
19060 block = finish_omp_structured_block (block);
19061 block = maybe_cleanup_point_expr_void (block);
19062 add_decl_expr (omp_out);
19063 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
19064 TREE_NO_WARNING (omp_out) = 1;
19065 add_decl_expr (omp_in);
19066 finish_expr_stmt (block);
19067 }
19068 if (i >= 6)
19069 {
19070 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19071 && TREE_CODE (stmts[4]) == DECL_EXPR);
19072 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19073 args, complain, in_decl);
19074 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19075 args, complain, in_decl);
19076 DECL_CONTEXT (omp_priv) = current_function_decl;
19077 DECL_CONTEXT (omp_orig) = current_function_decl;
19078 keep_next_level (true);
19079 tree block = begin_omp_structured_block ();
19080 tsubst_expr (stmts[5], args, complain, in_decl, false);
19081 block = finish_omp_structured_block (block);
19082 block = maybe_cleanup_point_expr_void (block);
19083 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19084 add_decl_expr (omp_priv);
19085 add_decl_expr (omp_orig);
19086 finish_expr_stmt (block);
19087 if (i == 7)
19088 add_decl_expr (omp_orig);
19089 }
19090 }
19091
19092 /* T is a postfix-expression that is not being used in a function
19093 call. Return the substituted version of T. */
19094
19095 static tree
19096 tsubst_non_call_postfix_expression (tree t, tree args,
19097 tsubst_flags_t complain,
19098 tree in_decl)
19099 {
19100 if (TREE_CODE (t) == SCOPE_REF)
19101 t = tsubst_qualified_id (t, args, complain, in_decl,
19102 /*done=*/false, /*address_p=*/false);
19103 else
19104 t = tsubst_copy_and_build (t, args, complain, in_decl,
19105 /*function_p=*/false,
19106 /*integral_constant_expression_p=*/false);
19107
19108 return t;
19109 }
19110
19111 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19112 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19113 dependent init-capture. */
19114
19115 static void
19116 prepend_one_capture (tree field, tree init, tree &list,
19117 tsubst_flags_t complain)
19118 {
19119 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19120 {
19121 tree type = NULL_TREE;
19122 if (!init)
19123 {
19124 if (complain & tf_error)
19125 error ("empty initializer in lambda init-capture");
19126 init = error_mark_node;
19127 }
19128 else if (TREE_CODE (init) == TREE_LIST)
19129 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19130 if (!type)
19131 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19132 TREE_TYPE (field) = type;
19133 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19134 }
19135 list = tree_cons (field, init, list);
19136 }
19137
19138 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19139 instantiation context. Instantiating a pack expansion containing a lambda
19140 might result in multiple lambdas all based on the same lambda in the
19141 template. */
19142
19143 tree
19144 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19145 {
19146 tree oldfn = lambda_function (t);
19147 in_decl = oldfn;
19148
19149 tree r = build_lambda_expr ();
19150
19151 LAMBDA_EXPR_LOCATION (r)
19152 = LAMBDA_EXPR_LOCATION (t);
19153 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19154 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19155 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19156 LAMBDA_EXPR_INSTANTIATED (r) = true;
19157
19158 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19159 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19160
19161 vec<tree,va_gc>* field_packs = NULL;
19162
19163 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19164 cap = TREE_CHAIN (cap))
19165 {
19166 tree ofield = TREE_PURPOSE (cap);
19167 tree init = TREE_VALUE (cap);
19168 if (PACK_EXPANSION_P (init))
19169 init = tsubst_pack_expansion (init, args, complain, in_decl);
19170 else
19171 init = tsubst_copy_and_build (init, args, complain, in_decl,
19172 /*fn*/false, /*constexpr*/false);
19173
19174 if (init == error_mark_node)
19175 return error_mark_node;
19176
19177 if (init && TREE_CODE (init) == TREE_LIST)
19178 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19179
19180 if (!processing_template_decl
19181 && init && TREE_CODE (init) != TREE_VEC
19182 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19183 {
19184 /* For a VLA, simply tsubsting the field type won't work, we need to
19185 go through add_capture again. XXX do we want to do this for all
19186 captures? */
19187 tree name = (get_identifier
19188 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19189 tree ftype = TREE_TYPE (ofield);
19190 bool by_ref = (TYPE_REF_P (ftype)
19191 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19192 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19193 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19194 continue;
19195 }
19196
19197 if (PACK_EXPANSION_P (ofield))
19198 ofield = PACK_EXPANSION_PATTERN (ofield);
19199 tree field = tsubst_decl (ofield, args, complain);
19200
19201 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19202 {
19203 /* Remember these for when we've pushed local_specializations. */
19204 vec_safe_push (field_packs, ofield);
19205 vec_safe_push (field_packs, field);
19206 }
19207
19208 if (field == error_mark_node)
19209 return error_mark_node;
19210
19211 if (TREE_CODE (field) == TREE_VEC)
19212 {
19213 int len = TREE_VEC_LENGTH (field);
19214 gcc_assert (TREE_CODE (init) == TREE_VEC
19215 && TREE_VEC_LENGTH (init) == len);
19216 for (int i = 0; i < len; ++i)
19217 prepend_one_capture (TREE_VEC_ELT (field, i),
19218 TREE_VEC_ELT (init, i),
19219 LAMBDA_EXPR_CAPTURE_LIST (r),
19220 complain);
19221 }
19222 else
19223 {
19224 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19225 complain);
19226
19227 if (id_equal (DECL_NAME (field), "__this"))
19228 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19229 }
19230 }
19231
19232 tree type = begin_lambda_type (r);
19233 if (type == error_mark_node)
19234 return error_mark_node;
19235
19236 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19237 /* A lambda in a default argument outside a class gets no
19238 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19239 tsubst_default_argument calls start_lambda_scope, so we need to
19240 specifically ignore it here, and use the global scope. */
19241 record_null_lambda_scope (r);
19242 else
19243 record_lambda_scope (r);
19244
19245 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19246 determine_visibility (TYPE_NAME (type));
19247
19248 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19249
19250 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19251 ? DECL_TI_TEMPLATE (oldfn)
19252 : NULL_TREE);
19253
19254 tree fntype = static_fn_type (oldfn);
19255 if (oldtmpl)
19256 ++processing_template_decl;
19257 fntype = tsubst (fntype, args, complain, in_decl);
19258 if (oldtmpl)
19259 --processing_template_decl;
19260
19261 if (fntype == error_mark_node)
19262 r = error_mark_node;
19263 else
19264 {
19265 /* The body of a lambda-expression is not a subexpression of the
19266 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19267 which would be skipped if cp_unevaluated_operand. */
19268 cp_evaluated ev;
19269
19270 /* Fix the type of 'this'. */
19271 fntype = build_memfn_type (fntype, type,
19272 type_memfn_quals (fntype),
19273 type_memfn_rqual (fntype));
19274 tree fn, tmpl;
19275 if (oldtmpl)
19276 {
19277 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19278 if (tmpl == error_mark_node)
19279 {
19280 r = error_mark_node;
19281 goto out;
19282 }
19283 fn = DECL_TEMPLATE_RESULT (tmpl);
19284 finish_member_declaration (tmpl);
19285 }
19286 else
19287 {
19288 tmpl = NULL_TREE;
19289 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19290 if (fn == error_mark_node)
19291 {
19292 r = error_mark_node;
19293 goto out;
19294 }
19295 finish_member_declaration (fn);
19296 }
19297
19298 if (tree ci = get_constraints (oldfn))
19299 {
19300 /* Substitute into the lambda's constraints. */
19301 if (oldtmpl)
19302 ++processing_template_decl;
19303 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19304 if (oldtmpl)
19305 --processing_template_decl;
19306 set_constraints (fn, ci);
19307 }
19308
19309 /* Let finish_function set this. */
19310 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19311
19312 bool nested = cfun;
19313 if (nested)
19314 push_function_context ();
19315 else
19316 /* Still increment function_depth so that we don't GC in the
19317 middle of an expression. */
19318 ++function_depth;
19319
19320 local_specialization_stack s (lss_copy);
19321
19322 tree body = start_lambda_function (fn, r);
19323
19324 /* Now record them for lookup_init_capture_pack. */
19325 int fplen = vec_safe_length (field_packs);
19326 for (int i = 0; i < fplen; )
19327 {
19328 tree pack = (*field_packs)[i++];
19329 tree inst = (*field_packs)[i++];
19330 register_local_specialization (inst, pack);
19331 }
19332 release_tree_vector (field_packs);
19333
19334 register_parameter_specializations (oldfn, fn);
19335
19336 if (oldtmpl)
19337 {
19338 /* We might not partially instantiate some parts of the function, so
19339 copy these flags from the original template. */
19340 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19341 current_function_returns_value = ol->returns_value;
19342 current_function_returns_null = ol->returns_null;
19343 current_function_returns_abnormally = ol->returns_abnormally;
19344 current_function_infinite_loop = ol->infinite_loop;
19345 }
19346
19347 /* [temp.deduct] A lambda-expression appearing in a function type or a
19348 template parameter is not considered part of the immediate context for
19349 the purposes of template argument deduction. */
19350 complain = tf_warning_or_error;
19351
19352 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19353 /*constexpr*/false);
19354
19355 finish_lambda_function (body);
19356
19357 if (nested)
19358 pop_function_context ();
19359 else
19360 --function_depth;
19361
19362 /* The capture list was built up in reverse order; fix that now. */
19363 LAMBDA_EXPR_CAPTURE_LIST (r)
19364 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19365
19366 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19367
19368 maybe_add_lambda_conv_op (type);
19369 }
19370
19371 out:
19372 finish_struct (type, /*attr*/NULL_TREE);
19373
19374 insert_pending_capture_proxies ();
19375
19376 return r;
19377 }
19378
19379 /* Like tsubst but deals with expressions and performs semantic
19380 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19381 "F<TARGS> (ARGS)". */
19382
19383 tree
19384 tsubst_copy_and_build (tree t,
19385 tree args,
19386 tsubst_flags_t complain,
19387 tree in_decl,
19388 bool function_p,
19389 bool integral_constant_expression_p)
19390 {
19391 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19392 #define RECUR(NODE) \
19393 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19394 /*function_p=*/false, \
19395 integral_constant_expression_p)
19396
19397 tree retval, op1;
19398 location_t save_loc;
19399
19400 if (t == NULL_TREE || t == error_mark_node)
19401 return t;
19402
19403 save_loc = input_location;
19404 if (location_t eloc = cp_expr_location (t))
19405 input_location = eloc;
19406
19407 /* N3276 decltype magic only applies to calls at the top level or on the
19408 right side of a comma. */
19409 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19410 complain &= ~tf_decltype;
19411
19412 switch (TREE_CODE (t))
19413 {
19414 case USING_DECL:
19415 t = DECL_NAME (t);
19416 /* Fall through. */
19417 case IDENTIFIER_NODE:
19418 {
19419 tree decl;
19420 cp_id_kind idk;
19421 bool non_integral_constant_expression_p;
19422 const char *error_msg;
19423
19424 if (IDENTIFIER_CONV_OP_P (t))
19425 {
19426 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19427 t = make_conv_op_name (new_type);
19428 }
19429
19430 /* Look up the name. */
19431 decl = lookup_name (t);
19432
19433 /* By convention, expressions use ERROR_MARK_NODE to indicate
19434 failure, not NULL_TREE. */
19435 if (decl == NULL_TREE)
19436 decl = error_mark_node;
19437
19438 decl = finish_id_expression (t, decl, NULL_TREE,
19439 &idk,
19440 integral_constant_expression_p,
19441 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19442 &non_integral_constant_expression_p,
19443 /*template_p=*/false,
19444 /*done=*/true,
19445 /*address_p=*/false,
19446 /*template_arg_p=*/false,
19447 &error_msg,
19448 input_location);
19449 if (error_msg)
19450 error (error_msg);
19451 if (!function_p && identifier_p (decl))
19452 {
19453 if (complain & tf_error)
19454 unqualified_name_lookup_error (decl);
19455 decl = error_mark_node;
19456 }
19457 RETURN (decl);
19458 }
19459
19460 case TEMPLATE_ID_EXPR:
19461 {
19462 tree object;
19463 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19464 complain, in_decl,
19465 function_p,
19466 integral_constant_expression_p);
19467 tree targs = TREE_OPERAND (t, 1);
19468
19469 if (targs)
19470 targs = tsubst_template_args (targs, args, complain, in_decl);
19471 if (targs == error_mark_node)
19472 RETURN (error_mark_node);
19473
19474 if (TREE_CODE (templ) == SCOPE_REF)
19475 {
19476 tree name = TREE_OPERAND (templ, 1);
19477 tree tid = lookup_template_function (name, targs);
19478 TREE_OPERAND (templ, 1) = tid;
19479 RETURN (templ);
19480 }
19481
19482 if (concept_definition_p (templ))
19483 {
19484 tree check = build_concept_check (templ, targs, complain);
19485 if (check == error_mark_node)
19486 RETURN (error_mark_node);
19487
19488 tree id = unpack_concept_check (check);
19489
19490 /* If we built a function concept check, return the underlying
19491 template-id. So we can evaluate it as a function call. */
19492 if (function_concept_p (TREE_OPERAND (id, 0)))
19493 RETURN (id);
19494
19495 RETURN (check);
19496 }
19497
19498 if (variable_template_p (templ))
19499 {
19500 tree r = lookup_and_finish_template_variable (templ, targs,
19501 complain);
19502 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19503 RETURN (r);
19504 }
19505
19506 if (TREE_CODE (templ) == COMPONENT_REF)
19507 {
19508 object = TREE_OPERAND (templ, 0);
19509 templ = TREE_OPERAND (templ, 1);
19510 }
19511 else
19512 object = NULL_TREE;
19513
19514 tree tid = lookup_template_function (templ, targs);
19515
19516 if (object)
19517 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19518 object, tid, NULL_TREE));
19519 else if (identifier_p (templ))
19520 {
19521 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19522 name lookup found nothing when parsing the template name. */
19523 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19524 RETURN (tid);
19525 }
19526 else
19527 RETURN (baselink_for_fns (tid));
19528 }
19529
19530 case INDIRECT_REF:
19531 {
19532 tree r = RECUR (TREE_OPERAND (t, 0));
19533
19534 if (REFERENCE_REF_P (t))
19535 {
19536 /* A type conversion to reference type will be enclosed in
19537 such an indirect ref, but the substitution of the cast
19538 will have also added such an indirect ref. */
19539 r = convert_from_reference (r);
19540 }
19541 else
19542 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19543 complain|decltype_flag);
19544
19545 if (REF_PARENTHESIZED_P (t))
19546 r = force_paren_expr (r);
19547
19548 RETURN (r);
19549 }
19550
19551 case NOP_EXPR:
19552 {
19553 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19554 tree op0 = RECUR (TREE_OPERAND (t, 0));
19555 RETURN (build_nop (type, op0));
19556 }
19557
19558 case IMPLICIT_CONV_EXPR:
19559 {
19560 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19561 tree expr = RECUR (TREE_OPERAND (t, 0));
19562 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19563 {
19564 retval = copy_node (t);
19565 TREE_TYPE (retval) = type;
19566 TREE_OPERAND (retval, 0) = expr;
19567 RETURN (retval);
19568 }
19569 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19570 /* We'll pass this to convert_nontype_argument again, we don't need
19571 to actually perform any conversion here. */
19572 RETURN (expr);
19573 int flags = LOOKUP_IMPLICIT;
19574 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19575 flags = LOOKUP_NORMAL;
19576 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19577 flags |= LOOKUP_NO_NARROWING;
19578 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19579 flags));
19580 }
19581
19582 case CONVERT_EXPR:
19583 {
19584 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19585 tree op0 = RECUR (TREE_OPERAND (t, 0));
19586 if (op0 == error_mark_node)
19587 RETURN (error_mark_node);
19588 RETURN (build1 (CONVERT_EXPR, type, op0));
19589 }
19590
19591 case CAST_EXPR:
19592 case REINTERPRET_CAST_EXPR:
19593 case CONST_CAST_EXPR:
19594 case DYNAMIC_CAST_EXPR:
19595 case STATIC_CAST_EXPR:
19596 {
19597 tree type;
19598 tree op, r = NULL_TREE;
19599
19600 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19601 if (integral_constant_expression_p
19602 && !cast_valid_in_integral_constant_expression_p (type))
19603 {
19604 if (complain & tf_error)
19605 error ("a cast to a type other than an integral or "
19606 "enumeration type cannot appear in a constant-expression");
19607 RETURN (error_mark_node);
19608 }
19609
19610 op = RECUR (TREE_OPERAND (t, 0));
19611
19612 warning_sentinel s(warn_useless_cast);
19613 warning_sentinel s2(warn_ignored_qualifiers);
19614 switch (TREE_CODE (t))
19615 {
19616 case CAST_EXPR:
19617 r = build_functional_cast (input_location, type, op, complain);
19618 break;
19619 case REINTERPRET_CAST_EXPR:
19620 r = build_reinterpret_cast (input_location, type, op, complain);
19621 break;
19622 case CONST_CAST_EXPR:
19623 r = build_const_cast (input_location, type, op, complain);
19624 break;
19625 case DYNAMIC_CAST_EXPR:
19626 r = build_dynamic_cast (input_location, type, op, complain);
19627 break;
19628 case STATIC_CAST_EXPR:
19629 r = build_static_cast (input_location, type, op, complain);
19630 if (IMPLICIT_RVALUE_P (t))
19631 set_implicit_rvalue_p (r);
19632 break;
19633 default:
19634 gcc_unreachable ();
19635 }
19636
19637 RETURN (r);
19638 }
19639
19640 case BIT_CAST_EXPR:
19641 {
19642 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19643 tree op0 = RECUR (TREE_OPERAND (t, 0));
19644 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
19645 }
19646
19647 case POSTDECREMENT_EXPR:
19648 case POSTINCREMENT_EXPR:
19649 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19650 args, complain, in_decl);
19651 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19652 complain|decltype_flag));
19653
19654 case PREDECREMENT_EXPR:
19655 case PREINCREMENT_EXPR:
19656 case NEGATE_EXPR:
19657 case BIT_NOT_EXPR:
19658 case ABS_EXPR:
19659 case TRUTH_NOT_EXPR:
19660 case UNARY_PLUS_EXPR: /* Unary + */
19661 case REALPART_EXPR:
19662 case IMAGPART_EXPR:
19663 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19664 RECUR (TREE_OPERAND (t, 0)),
19665 complain|decltype_flag));
19666
19667 case FIX_TRUNC_EXPR:
19668 gcc_unreachable ();
19669
19670 case ADDR_EXPR:
19671 op1 = TREE_OPERAND (t, 0);
19672 if (TREE_CODE (op1) == LABEL_DECL)
19673 RETURN (finish_label_address_expr (DECL_NAME (op1),
19674 EXPR_LOCATION (op1)));
19675 if (TREE_CODE (op1) == SCOPE_REF)
19676 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19677 /*done=*/true, /*address_p=*/true);
19678 else
19679 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19680 in_decl);
19681 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19682 complain|decltype_flag));
19683
19684 case PLUS_EXPR:
19685 case MINUS_EXPR:
19686 case MULT_EXPR:
19687 case TRUNC_DIV_EXPR:
19688 case CEIL_DIV_EXPR:
19689 case FLOOR_DIV_EXPR:
19690 case ROUND_DIV_EXPR:
19691 case EXACT_DIV_EXPR:
19692 case BIT_AND_EXPR:
19693 case BIT_IOR_EXPR:
19694 case BIT_XOR_EXPR:
19695 case TRUNC_MOD_EXPR:
19696 case FLOOR_MOD_EXPR:
19697 case TRUTH_ANDIF_EXPR:
19698 case TRUTH_ORIF_EXPR:
19699 case TRUTH_AND_EXPR:
19700 case TRUTH_OR_EXPR:
19701 case RSHIFT_EXPR:
19702 case LSHIFT_EXPR:
19703 case EQ_EXPR:
19704 case NE_EXPR:
19705 case MAX_EXPR:
19706 case MIN_EXPR:
19707 case LE_EXPR:
19708 case GE_EXPR:
19709 case LT_EXPR:
19710 case GT_EXPR:
19711 case SPACESHIP_EXPR:
19712 case MEMBER_REF:
19713 case DOTSTAR_EXPR:
19714 {
19715 /* If T was type-dependent, suppress warnings that depend on the range
19716 of the types involved. */
19717 ++processing_template_decl;
19718 const bool was_dep = (potential_constant_expression (t)
19719 ? value_dependent_expression_p (t)
19720 : type_dependent_expression_p (t));
19721 --processing_template_decl;
19722 tree op0 = RECUR (TREE_OPERAND (t, 0));
19723 tree op1 = RECUR (TREE_OPERAND (t, 1));
19724
19725 warning_sentinel s1(warn_type_limits, was_dep);
19726 warning_sentinel s2(warn_div_by_zero, was_dep);
19727 warning_sentinel s3(warn_logical_op, was_dep);
19728 warning_sentinel s4(warn_tautological_compare, was_dep);
19729
19730 tree r = build_x_binary_op
19731 (input_location, TREE_CODE (t),
19732 op0,
19733 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19734 ? ERROR_MARK
19735 : TREE_CODE (TREE_OPERAND (t, 0))),
19736 op1,
19737 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19738 ? ERROR_MARK
19739 : TREE_CODE (TREE_OPERAND (t, 1))),
19740 /*overload=*/NULL,
19741 complain|decltype_flag);
19742 if (EXPR_P (r) && TREE_NO_WARNING (t))
19743 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19744
19745 RETURN (r);
19746 }
19747
19748 case POINTER_PLUS_EXPR:
19749 {
19750 tree op0 = RECUR (TREE_OPERAND (t, 0));
19751 if (op0 == error_mark_node)
19752 RETURN (error_mark_node);
19753 tree op1 = RECUR (TREE_OPERAND (t, 1));
19754 if (op1 == error_mark_node)
19755 RETURN (error_mark_node);
19756 RETURN (fold_build_pointer_plus (op0, op1));
19757 }
19758
19759 case SCOPE_REF:
19760 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19761 /*address_p=*/false));
19762 case ARRAY_REF:
19763 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19764 args, complain, in_decl);
19765 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19766 RECUR (TREE_OPERAND (t, 1)),
19767 complain|decltype_flag));
19768
19769 case SIZEOF_EXPR:
19770 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19771 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19772 RETURN (tsubst_copy (t, args, complain, in_decl));
19773 /* Fall through */
19774
19775 case ALIGNOF_EXPR:
19776 {
19777 tree r;
19778
19779 op1 = TREE_OPERAND (t, 0);
19780 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19781 op1 = TREE_TYPE (op1);
19782 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19783 && ALIGNOF_EXPR_STD_P (t));
19784 if (!args)
19785 {
19786 /* When there are no ARGS, we are trying to evaluate a
19787 non-dependent expression from the parser. Trying to do
19788 the substitutions may not work. */
19789 if (!TYPE_P (op1))
19790 op1 = TREE_TYPE (op1);
19791 }
19792 else
19793 {
19794 ++cp_unevaluated_operand;
19795 ++c_inhibit_evaluation_warnings;
19796 if (TYPE_P (op1))
19797 op1 = tsubst (op1, args, complain, in_decl);
19798 else
19799 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19800 /*function_p=*/false,
19801 /*integral_constant_expression_p=*/
19802 false);
19803 --cp_unevaluated_operand;
19804 --c_inhibit_evaluation_warnings;
19805 }
19806 if (TYPE_P (op1))
19807 r = cxx_sizeof_or_alignof_type (input_location,
19808 op1, TREE_CODE (t), std_alignof,
19809 complain & tf_error);
19810 else
19811 r = cxx_sizeof_or_alignof_expr (input_location,
19812 op1, TREE_CODE (t), std_alignof,
19813 complain & tf_error);
19814 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19815 {
19816 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19817 {
19818 if (!processing_template_decl && TYPE_P (op1))
19819 {
19820 r = build_min (SIZEOF_EXPR, size_type_node,
19821 build1 (NOP_EXPR, op1, error_mark_node));
19822 SIZEOF_EXPR_TYPE_P (r) = 1;
19823 }
19824 else
19825 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19826 TREE_SIDE_EFFECTS (r) = 0;
19827 TREE_READONLY (r) = 1;
19828 }
19829 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19830 }
19831 RETURN (r);
19832 }
19833
19834 case AT_ENCODE_EXPR:
19835 {
19836 op1 = TREE_OPERAND (t, 0);
19837 ++cp_unevaluated_operand;
19838 ++c_inhibit_evaluation_warnings;
19839 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19840 /*function_p=*/false,
19841 /*integral_constant_expression_p=*/false);
19842 --cp_unevaluated_operand;
19843 --c_inhibit_evaluation_warnings;
19844 RETURN (objc_build_encode_expr (op1));
19845 }
19846
19847 case NOEXCEPT_EXPR:
19848 op1 = TREE_OPERAND (t, 0);
19849 ++cp_unevaluated_operand;
19850 ++c_inhibit_evaluation_warnings;
19851 ++cp_noexcept_operand;
19852 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19853 /*function_p=*/false,
19854 /*integral_constant_expression_p=*/false);
19855 --cp_unevaluated_operand;
19856 --c_inhibit_evaluation_warnings;
19857 --cp_noexcept_operand;
19858 RETURN (finish_noexcept_expr (op1, complain));
19859
19860 case MODOP_EXPR:
19861 {
19862 warning_sentinel s(warn_div_by_zero);
19863 tree lhs = RECUR (TREE_OPERAND (t, 0));
19864 tree rhs = RECUR (TREE_OPERAND (t, 2));
19865 tree r = build_x_modify_expr
19866 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19867 complain|decltype_flag);
19868 /* TREE_NO_WARNING must be set if either the expression was
19869 parenthesized or it uses an operator such as >>= rather
19870 than plain assignment. In the former case, it was already
19871 set and must be copied. In the latter case,
19872 build_x_modify_expr sets it and it must not be reset
19873 here. */
19874 if (TREE_NO_WARNING (t))
19875 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19876
19877 RETURN (r);
19878 }
19879
19880 case ARROW_EXPR:
19881 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19882 args, complain, in_decl);
19883 /* Remember that there was a reference to this entity. */
19884 if (DECL_P (op1)
19885 && !mark_used (op1, complain) && !(complain & tf_error))
19886 RETURN (error_mark_node);
19887 RETURN (build_x_arrow (input_location, op1, complain));
19888
19889 case NEW_EXPR:
19890 {
19891 tree placement = RECUR (TREE_OPERAND (t, 0));
19892 tree init = RECUR (TREE_OPERAND (t, 3));
19893 vec<tree, va_gc> *placement_vec;
19894 vec<tree, va_gc> *init_vec;
19895 tree ret;
19896 location_t loc = EXPR_LOCATION (t);
19897
19898 if (placement == NULL_TREE)
19899 placement_vec = NULL;
19900 else if (placement == error_mark_node)
19901 RETURN (error_mark_node);
19902 else
19903 {
19904 placement_vec = make_tree_vector ();
19905 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19906 vec_safe_push (placement_vec, TREE_VALUE (placement));
19907 }
19908
19909 /* If there was an initializer in the original tree, but it
19910 instantiated to an empty list, then we should pass a
19911 non-NULL empty vector to tell build_new that it was an
19912 empty initializer() rather than no initializer. This can
19913 only happen when the initializer is a pack expansion whose
19914 parameter packs are of length zero. */
19915 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19916 init_vec = NULL;
19917 else if (init == error_mark_node)
19918 RETURN (error_mark_node);
19919 else
19920 {
19921 init_vec = make_tree_vector ();
19922 if (init == void_node)
19923 gcc_assert (init_vec != NULL);
19924 else
19925 {
19926 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19927 vec_safe_push (init_vec, TREE_VALUE (init));
19928 }
19929 }
19930
19931 /* Avoid passing an enclosing decl to valid_array_size_p. */
19932 in_decl = NULL_TREE;
19933
19934 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19935 tree op2 = RECUR (TREE_OPERAND (t, 2));
19936 ret = build_new (loc, &placement_vec, op1, op2,
19937 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19938 complain);
19939
19940 if (placement_vec != NULL)
19941 release_tree_vector (placement_vec);
19942 if (init_vec != NULL)
19943 release_tree_vector (init_vec);
19944
19945 RETURN (ret);
19946 }
19947
19948 case DELETE_EXPR:
19949 {
19950 tree op0 = RECUR (TREE_OPERAND (t, 0));
19951 tree op1 = RECUR (TREE_OPERAND (t, 1));
19952 RETURN (delete_sanity (input_location, op0, op1,
19953 DELETE_EXPR_USE_VEC (t),
19954 DELETE_EXPR_USE_GLOBAL (t),
19955 complain));
19956 }
19957
19958 case COMPOUND_EXPR:
19959 {
19960 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19961 complain & ~tf_decltype, in_decl,
19962 /*function_p=*/false,
19963 integral_constant_expression_p);
19964 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19965 op0,
19966 RECUR (TREE_OPERAND (t, 1)),
19967 complain|decltype_flag));
19968 }
19969
19970 case CALL_EXPR:
19971 {
19972 tree function;
19973 unsigned int nargs, i;
19974 bool qualified_p;
19975 bool koenig_p;
19976 tree ret;
19977
19978 function = CALL_EXPR_FN (t);
19979 /* Internal function with no arguments. */
19980 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19981 RETURN (t);
19982
19983 /* When we parsed the expression, we determined whether or
19984 not Koenig lookup should be performed. */
19985 koenig_p = KOENIG_LOOKUP_P (t);
19986 if (function == NULL_TREE)
19987 {
19988 koenig_p = false;
19989 qualified_p = false;
19990 }
19991 else if (TREE_CODE (function) == SCOPE_REF)
19992 {
19993 qualified_p = true;
19994 function = tsubst_qualified_id (function, args, complain, in_decl,
19995 /*done=*/false,
19996 /*address_p=*/false);
19997 }
19998 else if (koenig_p && identifier_p (function))
19999 {
20000 /* Do nothing; calling tsubst_copy_and_build on an identifier
20001 would incorrectly perform unqualified lookup again.
20002
20003 Note that we can also have an IDENTIFIER_NODE if the earlier
20004 unqualified lookup found a member function; in that case
20005 koenig_p will be false and we do want to do the lookup
20006 again to find the instantiated member function.
20007
20008 FIXME but doing that causes c++/15272, so we need to stop
20009 using IDENTIFIER_NODE in that situation. */
20010 qualified_p = false;
20011 }
20012 else
20013 {
20014 if (TREE_CODE (function) == COMPONENT_REF)
20015 {
20016 tree op = TREE_OPERAND (function, 1);
20017
20018 qualified_p = (TREE_CODE (op) == SCOPE_REF
20019 || (BASELINK_P (op)
20020 && BASELINK_QUALIFIED_P (op)));
20021 }
20022 else
20023 qualified_p = false;
20024
20025 if (TREE_CODE (function) == ADDR_EXPR
20026 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20027 /* Avoid error about taking the address of a constructor. */
20028 function = TREE_OPERAND (function, 0);
20029
20030 function = tsubst_copy_and_build (function, args, complain,
20031 in_decl,
20032 !qualified_p,
20033 integral_constant_expression_p);
20034
20035 if (BASELINK_P (function))
20036 qualified_p = true;
20037 }
20038
20039 nargs = call_expr_nargs (t);
20040 releasing_vec call_args;
20041 for (i = 0; i < nargs; ++i)
20042 {
20043 tree arg = CALL_EXPR_ARG (t, i);
20044
20045 if (!PACK_EXPANSION_P (arg))
20046 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
20047 else
20048 {
20049 /* Expand the pack expansion and push each entry onto
20050 CALL_ARGS. */
20051 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20052 if (TREE_CODE (arg) == TREE_VEC)
20053 {
20054 unsigned int len, j;
20055
20056 len = TREE_VEC_LENGTH (arg);
20057 for (j = 0; j < len; ++j)
20058 {
20059 tree value = TREE_VEC_ELT (arg, j);
20060 if (value != NULL_TREE)
20061 value = convert_from_reference (value);
20062 vec_safe_push (call_args, value);
20063 }
20064 }
20065 else
20066 {
20067 /* A partial substitution. Add one entry. */
20068 vec_safe_push (call_args, arg);
20069 }
20070 }
20071 }
20072
20073 /* Stripped-down processing for a call in a thunk. Specifically, in
20074 the thunk template for a generic lambda. */
20075 if (call_from_lambda_thunk_p (t))
20076 {
20077 /* Now that we've expanded any packs, the number of call args
20078 might be different. */
20079 unsigned int cargs = call_args->length ();
20080 tree thisarg = NULL_TREE;
20081 if (TREE_CODE (function) == COMPONENT_REF)
20082 {
20083 thisarg = TREE_OPERAND (function, 0);
20084 if (TREE_CODE (thisarg) == INDIRECT_REF)
20085 thisarg = TREE_OPERAND (thisarg, 0);
20086 function = TREE_OPERAND (function, 1);
20087 if (TREE_CODE (function) == BASELINK)
20088 function = BASELINK_FUNCTIONS (function);
20089 }
20090 /* We aren't going to do normal overload resolution, so force the
20091 template-id to resolve. */
20092 function = resolve_nondeduced_context (function, complain);
20093 for (unsigned i = 0; i < cargs; ++i)
20094 {
20095 /* In a thunk, pass through args directly, without any
20096 conversions. */
20097 tree arg = (*call_args)[i];
20098 while (TREE_CODE (arg) != PARM_DECL)
20099 arg = TREE_OPERAND (arg, 0);
20100 (*call_args)[i] = arg;
20101 }
20102 if (thisarg)
20103 {
20104 /* If there are no other args, just push 'this'. */
20105 if (cargs == 0)
20106 vec_safe_push (call_args, thisarg);
20107 else
20108 {
20109 /* Otherwise, shift the other args over to make room. */
20110 tree last = (*call_args)[cargs - 1];
20111 vec_safe_push (call_args, last);
20112 for (int i = cargs - 1; i > 0; --i)
20113 (*call_args)[i] = (*call_args)[i - 1];
20114 (*call_args)[0] = thisarg;
20115 }
20116 }
20117 ret = build_call_a (function, call_args->length (),
20118 call_args->address ());
20119 /* The thunk location is not interesting. */
20120 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20121 CALL_FROM_THUNK_P (ret) = true;
20122 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20123 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20124
20125 RETURN (ret);
20126 }
20127
20128 /* We do not perform argument-dependent lookup if normal
20129 lookup finds a non-function, in accordance with the
20130 resolution of DR 218. */
20131 if (koenig_p
20132 && ((is_overloaded_fn (function)
20133 /* If lookup found a member function, the Koenig lookup is
20134 not appropriate, even if an unqualified-name was used
20135 to denote the function. */
20136 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20137 || identifier_p (function)
20138 /* C++20 P0846: Lookup found nothing. */
20139 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20140 && identifier_p (TREE_OPERAND (function, 0))))
20141 /* Only do this when substitution turns a dependent call
20142 into a non-dependent call. */
20143 && type_dependent_expression_p_push (t)
20144 && !any_type_dependent_arguments_p (call_args))
20145 function = perform_koenig_lookup (function, call_args, tf_none);
20146
20147 if (function != NULL_TREE
20148 && (identifier_p (function)
20149 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20150 && identifier_p (TREE_OPERAND (function, 0))))
20151 && !any_type_dependent_arguments_p (call_args))
20152 {
20153 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20154 function = TREE_OPERAND (function, 0);
20155 if (koenig_p && (complain & tf_warning_or_error))
20156 {
20157 /* For backwards compatibility and good diagnostics, try
20158 the unqualified lookup again if we aren't in SFINAE
20159 context. */
20160 tree unq = (tsubst_copy_and_build
20161 (function, args, complain, in_decl, true,
20162 integral_constant_expression_p));
20163 if (unq == error_mark_node)
20164 RETURN (error_mark_node);
20165
20166 if (unq != function)
20167 {
20168 /* In a lambda fn, we have to be careful to not
20169 introduce new this captures. Legacy code can't
20170 be using lambdas anyway, so it's ok to be
20171 stricter. */
20172 bool in_lambda = (current_class_type
20173 && LAMBDA_TYPE_P (current_class_type));
20174 char const *const msg
20175 = G_("%qD was not declared in this scope, "
20176 "and no declarations were found by "
20177 "argument-dependent lookup at the point "
20178 "of instantiation");
20179
20180 bool diag = true;
20181 if (in_lambda)
20182 error_at (cp_expr_loc_or_input_loc (t),
20183 msg, function);
20184 else
20185 diag = permerror (cp_expr_loc_or_input_loc (t),
20186 msg, function);
20187 if (diag)
20188 {
20189 tree fn = unq;
20190
20191 if (INDIRECT_REF_P (fn))
20192 fn = TREE_OPERAND (fn, 0);
20193 if (is_overloaded_fn (fn))
20194 fn = get_first_fn (fn);
20195
20196 if (!DECL_P (fn))
20197 /* Can't say anything more. */;
20198 else if (DECL_CLASS_SCOPE_P (fn))
20199 {
20200 location_t loc = cp_expr_loc_or_input_loc (t);
20201 inform (loc,
20202 "declarations in dependent base %qT are "
20203 "not found by unqualified lookup",
20204 DECL_CLASS_CONTEXT (fn));
20205 if (current_class_ptr)
20206 inform (loc,
20207 "use %<this->%D%> instead", function);
20208 else
20209 inform (loc,
20210 "use %<%T::%D%> instead",
20211 current_class_name, function);
20212 }
20213 else
20214 inform (DECL_SOURCE_LOCATION (fn),
20215 "%qD declared here, later in the "
20216 "translation unit", fn);
20217 if (in_lambda)
20218 RETURN (error_mark_node);
20219 }
20220
20221 function = unq;
20222 }
20223 }
20224 if (identifier_p (function))
20225 {
20226 if (complain & tf_error)
20227 unqualified_name_lookup_error (function);
20228 RETURN (error_mark_node);
20229 }
20230 }
20231
20232 /* Remember that there was a reference to this entity. */
20233 if (function != NULL_TREE
20234 && DECL_P (function)
20235 && !mark_used (function, complain) && !(complain & tf_error))
20236 RETURN (error_mark_node);
20237
20238 /* Put back tf_decltype for the actual call. */
20239 complain |= decltype_flag;
20240
20241 if (function == NULL_TREE)
20242 switch (CALL_EXPR_IFN (t))
20243 {
20244 case IFN_LAUNDER:
20245 gcc_assert (nargs == 1);
20246 if (vec_safe_length (call_args) != 1)
20247 {
20248 error_at (cp_expr_loc_or_input_loc (t),
20249 "wrong number of arguments to "
20250 "%<__builtin_launder%>");
20251 ret = error_mark_node;
20252 }
20253 else
20254 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20255 (*call_args)[0], complain);
20256 break;
20257
20258 case IFN_VEC_CONVERT:
20259 gcc_assert (nargs == 1);
20260 if (vec_safe_length (call_args) != 1)
20261 {
20262 error_at (cp_expr_loc_or_input_loc (t),
20263 "wrong number of arguments to "
20264 "%<__builtin_convertvector%>");
20265 ret = error_mark_node;
20266 break;
20267 }
20268 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20269 tsubst (TREE_TYPE (t), args,
20270 complain, in_decl),
20271 complain);
20272 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20273 RETURN (ret);
20274 break;
20275
20276 default:
20277 /* Unsupported internal function with arguments. */
20278 gcc_unreachable ();
20279 }
20280 else if (TREE_CODE (function) == OFFSET_REF
20281 || TREE_CODE (function) == DOTSTAR_EXPR
20282 || TREE_CODE (function) == MEMBER_REF)
20283 ret = build_offset_ref_call_from_tree (function, &call_args,
20284 complain);
20285 else if (TREE_CODE (function) == COMPONENT_REF)
20286 {
20287 tree instance = TREE_OPERAND (function, 0);
20288 tree fn = TREE_OPERAND (function, 1);
20289
20290 if (processing_template_decl
20291 && (type_dependent_expression_p (instance)
20292 || (!BASELINK_P (fn)
20293 && TREE_CODE (fn) != FIELD_DECL)
20294 || type_dependent_expression_p (fn)
20295 || any_type_dependent_arguments_p (call_args)))
20296 ret = build_min_nt_call_vec (function, call_args);
20297 else if (!BASELINK_P (fn))
20298 ret = finish_call_expr (function, &call_args,
20299 /*disallow_virtual=*/false,
20300 /*koenig_p=*/false,
20301 complain);
20302 else
20303 ret = (build_new_method_call
20304 (instance, fn,
20305 &call_args, NULL_TREE,
20306 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20307 /*fn_p=*/NULL,
20308 complain));
20309 }
20310 else if (concept_check_p (function))
20311 {
20312 /* FUNCTION is a template-id referring to a concept definition. */
20313 tree id = unpack_concept_check (function);
20314 tree tmpl = TREE_OPERAND (id, 0);
20315 tree args = TREE_OPERAND (id, 1);
20316
20317 /* Calls to standard and variable concepts should have been
20318 previously diagnosed. */
20319 gcc_assert (function_concept_p (tmpl));
20320
20321 /* Ensure the result is wrapped as a call expression. */
20322 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20323 }
20324 else
20325 ret = finish_call_expr (function, &call_args,
20326 /*disallow_virtual=*/qualified_p,
20327 koenig_p,
20328 complain);
20329
20330 if (ret != error_mark_node)
20331 {
20332 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20333 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20334 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20335 if (op || ord || rev)
20336 {
20337 function = extract_call_expr (ret);
20338 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20339 CALL_EXPR_ORDERED_ARGS (function) = ord;
20340 CALL_EXPR_REVERSE_ARGS (function) = rev;
20341 }
20342 }
20343
20344 RETURN (ret);
20345 }
20346
20347 case COND_EXPR:
20348 {
20349 tree cond = RECUR (TREE_OPERAND (t, 0));
20350 cond = mark_rvalue_use (cond);
20351 tree folded_cond = fold_non_dependent_expr (cond, complain);
20352 tree exp1, exp2;
20353
20354 if (TREE_CODE (folded_cond) == INTEGER_CST)
20355 {
20356 if (integer_zerop (folded_cond))
20357 {
20358 ++c_inhibit_evaluation_warnings;
20359 exp1 = RECUR (TREE_OPERAND (t, 1));
20360 --c_inhibit_evaluation_warnings;
20361 exp2 = RECUR (TREE_OPERAND (t, 2));
20362 }
20363 else
20364 {
20365 exp1 = RECUR (TREE_OPERAND (t, 1));
20366 ++c_inhibit_evaluation_warnings;
20367 exp2 = RECUR (TREE_OPERAND (t, 2));
20368 --c_inhibit_evaluation_warnings;
20369 }
20370 cond = folded_cond;
20371 }
20372 else
20373 {
20374 exp1 = RECUR (TREE_OPERAND (t, 1));
20375 exp2 = RECUR (TREE_OPERAND (t, 2));
20376 }
20377
20378 warning_sentinel s(warn_duplicated_branches);
20379 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20380 cond, exp1, exp2, complain));
20381 }
20382
20383 case PSEUDO_DTOR_EXPR:
20384 {
20385 tree op0 = RECUR (TREE_OPERAND (t, 0));
20386 tree op1 = RECUR (TREE_OPERAND (t, 1));
20387 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20388 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20389 input_location));
20390 }
20391
20392 case TREE_LIST:
20393 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20394
20395 case COMPONENT_REF:
20396 {
20397 tree object;
20398 tree object_type;
20399 tree member;
20400 tree r;
20401
20402 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20403 args, complain, in_decl);
20404 /* Remember that there was a reference to this entity. */
20405 if (DECL_P (object)
20406 && !mark_used (object, complain) && !(complain & tf_error))
20407 RETURN (error_mark_node);
20408 object_type = TREE_TYPE (object);
20409
20410 member = TREE_OPERAND (t, 1);
20411 if (BASELINK_P (member))
20412 member = tsubst_baselink (member,
20413 non_reference (TREE_TYPE (object)),
20414 args, complain, in_decl);
20415 else
20416 member = tsubst_copy (member, args, complain, in_decl);
20417 if (member == error_mark_node)
20418 RETURN (error_mark_node);
20419
20420 if (TREE_CODE (member) == FIELD_DECL)
20421 {
20422 r = finish_non_static_data_member (member, object, NULL_TREE);
20423 if (TREE_CODE (r) == COMPONENT_REF)
20424 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20425 RETURN (r);
20426 }
20427 else if (type_dependent_expression_p (object))
20428 /* We can't do much here. */;
20429 else if (!CLASS_TYPE_P (object_type))
20430 {
20431 if (scalarish_type_p (object_type))
20432 {
20433 tree s = NULL_TREE;
20434 tree dtor = member;
20435
20436 if (TREE_CODE (dtor) == SCOPE_REF)
20437 {
20438 s = TREE_OPERAND (dtor, 0);
20439 dtor = TREE_OPERAND (dtor, 1);
20440 }
20441 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20442 {
20443 dtor = TREE_OPERAND (dtor, 0);
20444 if (TYPE_P (dtor))
20445 RETURN (finish_pseudo_destructor_expr
20446 (object, s, dtor, input_location));
20447 }
20448 }
20449 }
20450 else if (TREE_CODE (member) == SCOPE_REF
20451 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20452 {
20453 /* Lookup the template functions now that we know what the
20454 scope is. */
20455 tree scope = TREE_OPERAND (member, 0);
20456 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20457 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20458 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20459 /*complain=*/false);
20460 if (BASELINK_P (member))
20461 {
20462 BASELINK_FUNCTIONS (member)
20463 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20464 args);
20465 member = (adjust_result_of_qualified_name_lookup
20466 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20467 object_type));
20468 }
20469 else
20470 {
20471 qualified_name_lookup_error (scope, tmpl, member,
20472 input_location);
20473 RETURN (error_mark_node);
20474 }
20475 }
20476 else if (TREE_CODE (member) == SCOPE_REF
20477 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20478 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20479 {
20480 if (complain & tf_error)
20481 {
20482 if (TYPE_P (TREE_OPERAND (member, 0)))
20483 error ("%qT is not a class or namespace",
20484 TREE_OPERAND (member, 0));
20485 else
20486 error ("%qD is not a class or namespace",
20487 TREE_OPERAND (member, 0));
20488 }
20489 RETURN (error_mark_node);
20490 }
20491
20492 r = finish_class_member_access_expr (object, member,
20493 /*template_p=*/false,
20494 complain);
20495 if (TREE_CODE (r) == COMPONENT_REF)
20496 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20497 RETURN (r);
20498 }
20499
20500 case THROW_EXPR:
20501 RETURN (build_throw
20502 (input_location, RECUR (TREE_OPERAND (t, 0))));
20503
20504 case CONSTRUCTOR:
20505 {
20506 vec<constructor_elt, va_gc> *n;
20507 constructor_elt *ce;
20508 unsigned HOST_WIDE_INT idx;
20509 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20510 bool process_index_p;
20511 int newlen;
20512 bool need_copy_p = false;
20513 tree r;
20514
20515 if (type == error_mark_node)
20516 RETURN (error_mark_node);
20517
20518 /* We do not want to process the index of aggregate
20519 initializers as they are identifier nodes which will be
20520 looked up by digest_init. */
20521 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20522
20523 if (null_member_pointer_value_p (t))
20524 {
20525 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20526 RETURN (t);
20527 }
20528
20529 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20530 newlen = vec_safe_length (n);
20531 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20532 {
20533 if (ce->index && process_index_p
20534 /* An identifier index is looked up in the type
20535 being initialized, not the current scope. */
20536 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20537 ce->index = RECUR (ce->index);
20538
20539 if (PACK_EXPANSION_P (ce->value))
20540 {
20541 /* Substitute into the pack expansion. */
20542 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20543 in_decl);
20544
20545 if (ce->value == error_mark_node
20546 || PACK_EXPANSION_P (ce->value))
20547 ;
20548 else if (TREE_VEC_LENGTH (ce->value) == 1)
20549 /* Just move the argument into place. */
20550 ce->value = TREE_VEC_ELT (ce->value, 0);
20551 else
20552 {
20553 /* Update the length of the final CONSTRUCTOR
20554 arguments vector, and note that we will need to
20555 copy.*/
20556 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20557 need_copy_p = true;
20558 }
20559 }
20560 else
20561 ce->value = RECUR (ce->value);
20562 }
20563
20564 if (need_copy_p)
20565 {
20566 vec<constructor_elt, va_gc> *old_n = n;
20567
20568 vec_alloc (n, newlen);
20569 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20570 {
20571 if (TREE_CODE (ce->value) == TREE_VEC)
20572 {
20573 int i, len = TREE_VEC_LENGTH (ce->value);
20574 for (i = 0; i < len; ++i)
20575 CONSTRUCTOR_APPEND_ELT (n, 0,
20576 TREE_VEC_ELT (ce->value, i));
20577 }
20578 else
20579 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20580 }
20581 }
20582
20583 r = build_constructor (init_list_type_node, n);
20584 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20585 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20586 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20587
20588 if (TREE_HAS_CONSTRUCTOR (t))
20589 {
20590 fcl_t cl = fcl_functional;
20591 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20592 cl = fcl_c99;
20593 RETURN (finish_compound_literal (type, r, complain, cl));
20594 }
20595
20596 TREE_TYPE (r) = type;
20597 RETURN (r);
20598 }
20599
20600 case TYPEID_EXPR:
20601 {
20602 tree operand_0 = TREE_OPERAND (t, 0);
20603 if (TYPE_P (operand_0))
20604 {
20605 operand_0 = tsubst (operand_0, args, complain, in_decl);
20606 RETURN (get_typeid (operand_0, complain));
20607 }
20608 else
20609 {
20610 operand_0 = RECUR (operand_0);
20611 RETURN (build_typeid (operand_0, complain));
20612 }
20613 }
20614
20615 case VAR_DECL:
20616 if (!args)
20617 RETURN (t);
20618 /* Fall through */
20619
20620 case PARM_DECL:
20621 {
20622 tree r = tsubst_copy (t, args, complain, in_decl);
20623 /* ??? We're doing a subset of finish_id_expression here. */
20624 if (tree wrap = maybe_get_tls_wrapper_call (r))
20625 /* Replace an evaluated use of the thread_local variable with
20626 a call to its wrapper. */
20627 r = wrap;
20628 else if (outer_automatic_var_p (r))
20629 r = process_outer_var_ref (r, complain);
20630
20631 if (!TYPE_REF_P (TREE_TYPE (t)))
20632 /* If the original type was a reference, we'll be wrapped in
20633 the appropriate INDIRECT_REF. */
20634 r = convert_from_reference (r);
20635 RETURN (r);
20636 }
20637
20638 case VA_ARG_EXPR:
20639 {
20640 tree op0 = RECUR (TREE_OPERAND (t, 0));
20641 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20642 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20643 }
20644
20645 case OFFSETOF_EXPR:
20646 {
20647 tree object_ptr
20648 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20649 in_decl, /*function_p=*/false,
20650 /*integral_constant_expression_p=*/false);
20651 RETURN (finish_offsetof (object_ptr,
20652 RECUR (TREE_OPERAND (t, 0)),
20653 EXPR_LOCATION (t)));
20654 }
20655
20656 case ADDRESSOF_EXPR:
20657 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20658 RECUR (TREE_OPERAND (t, 0)), complain));
20659
20660 case TRAIT_EXPR:
20661 {
20662 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20663 complain, in_decl);
20664 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20665 complain, in_decl);
20666 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20667 TRAIT_EXPR_KIND (t), type1, type2));
20668 }
20669
20670 case STMT_EXPR:
20671 {
20672 tree old_stmt_expr = cur_stmt_expr;
20673 tree stmt_expr = begin_stmt_expr ();
20674
20675 cur_stmt_expr = stmt_expr;
20676 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20677 integral_constant_expression_p);
20678 stmt_expr = finish_stmt_expr (stmt_expr, false);
20679 cur_stmt_expr = old_stmt_expr;
20680
20681 /* If the resulting list of expression statement is empty,
20682 fold it further into void_node. */
20683 if (empty_expr_stmt_p (stmt_expr))
20684 stmt_expr = void_node;
20685
20686 RETURN (stmt_expr);
20687 }
20688
20689 case LAMBDA_EXPR:
20690 {
20691 if (complain & tf_partial)
20692 {
20693 /* We don't have a full set of template arguments yet; don't touch
20694 the lambda at all. */
20695 gcc_assert (processing_template_decl);
20696 return t;
20697 }
20698 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20699
20700 RETURN (build_lambda_object (r));
20701 }
20702
20703 case TARGET_EXPR:
20704 /* We can get here for a constant initializer of non-dependent type.
20705 FIXME stop folding in cp_parser_initializer_clause. */
20706 {
20707 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20708 complain);
20709 RETURN (r);
20710 }
20711
20712 case TRANSACTION_EXPR:
20713 RETURN (tsubst_expr(t, args, complain, in_decl,
20714 integral_constant_expression_p));
20715
20716 case PAREN_EXPR:
20717 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20718
20719 case VEC_PERM_EXPR:
20720 {
20721 tree op0 = RECUR (TREE_OPERAND (t, 0));
20722 tree op1 = RECUR (TREE_OPERAND (t, 1));
20723 tree op2 = RECUR (TREE_OPERAND (t, 2));
20724 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20725 complain));
20726 }
20727
20728 case REQUIRES_EXPR:
20729 {
20730 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20731 RETURN (r);
20732 }
20733
20734 case RANGE_EXPR:
20735 /* No need to substitute further, a RANGE_EXPR will always be built
20736 with constant operands. */
20737 RETURN (t);
20738
20739 case NON_LVALUE_EXPR:
20740 case VIEW_CONVERT_EXPR:
20741 if (location_wrapper_p (t))
20742 /* We need to do this here as well as in tsubst_copy so we get the
20743 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20744 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20745 EXPR_LOCATION (t)));
20746 /* fallthrough. */
20747
20748 default:
20749 /* Handle Objective-C++ constructs, if appropriate. */
20750 {
20751 tree subst
20752 = objcp_tsubst_copy_and_build (t, args, complain,
20753 in_decl, /*function_p=*/false);
20754 if (subst)
20755 RETURN (subst);
20756 }
20757 RETURN (tsubst_copy (t, args, complain, in_decl));
20758 }
20759
20760 #undef RECUR
20761 #undef RETURN
20762 out:
20763 input_location = save_loc;
20764 return retval;
20765 }
20766
20767 /* Verify that the instantiated ARGS are valid. For type arguments,
20768 make sure that the type's linkage is ok. For non-type arguments,
20769 make sure they are constants if they are integral or enumerations.
20770 Emit an error under control of COMPLAIN, and return TRUE on error. */
20771
20772 static bool
20773 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20774 {
20775 if (dependent_template_arg_p (t))
20776 return false;
20777 if (ARGUMENT_PACK_P (t))
20778 {
20779 tree vec = ARGUMENT_PACK_ARGS (t);
20780 int len = TREE_VEC_LENGTH (vec);
20781 bool result = false;
20782 int i;
20783
20784 for (i = 0; i < len; ++i)
20785 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20786 result = true;
20787 return result;
20788 }
20789 else if (TYPE_P (t))
20790 {
20791 /* [basic.link]: A name with no linkage (notably, the name
20792 of a class or enumeration declared in a local scope)
20793 shall not be used to declare an entity with linkage.
20794 This implies that names with no linkage cannot be used as
20795 template arguments
20796
20797 DR 757 relaxes this restriction for C++0x. */
20798 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20799 : no_linkage_check (t, /*relaxed_p=*/false));
20800
20801 if (nt)
20802 {
20803 /* DR 488 makes use of a type with no linkage cause
20804 type deduction to fail. */
20805 if (complain & tf_error)
20806 {
20807 if (TYPE_UNNAMED_P (nt))
20808 error ("%qT is/uses unnamed type", t);
20809 else
20810 error ("template argument for %qD uses local type %qT",
20811 tmpl, t);
20812 }
20813 return true;
20814 }
20815 /* In order to avoid all sorts of complications, we do not
20816 allow variably-modified types as template arguments. */
20817 else if (variably_modified_type_p (t, NULL_TREE))
20818 {
20819 if (complain & tf_error)
20820 error ("%qT is a variably modified type", t);
20821 return true;
20822 }
20823 }
20824 /* Class template and alias template arguments should be OK. */
20825 else if (DECL_TYPE_TEMPLATE_P (t))
20826 ;
20827 /* A non-type argument of integral or enumerated type must be a
20828 constant. */
20829 else if (TREE_TYPE (t)
20830 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20831 && !REFERENCE_REF_P (t)
20832 && !TREE_CONSTANT (t))
20833 {
20834 if (complain & tf_error)
20835 error ("integral expression %qE is not constant", t);
20836 return true;
20837 }
20838 return false;
20839 }
20840
20841 static bool
20842 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20843 {
20844 int ix, len = DECL_NTPARMS (tmpl);
20845 bool result = false;
20846
20847 for (ix = 0; ix != len; ix++)
20848 {
20849 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20850 result = true;
20851 }
20852 if (result && (complain & tf_error))
20853 error (" trying to instantiate %qD", tmpl);
20854 return result;
20855 }
20856
20857 /* We're out of SFINAE context now, so generate diagnostics for the access
20858 errors we saw earlier when instantiating D from TMPL and ARGS. */
20859
20860 static void
20861 recheck_decl_substitution (tree d, tree tmpl, tree args)
20862 {
20863 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20864 tree type = TREE_TYPE (pattern);
20865 location_t loc = input_location;
20866
20867 push_access_scope (d);
20868 push_deferring_access_checks (dk_no_deferred);
20869 input_location = DECL_SOURCE_LOCATION (pattern);
20870 tsubst (type, args, tf_warning_or_error, d);
20871 input_location = loc;
20872 pop_deferring_access_checks ();
20873 pop_access_scope (d);
20874 }
20875
20876 /* Instantiate the indicated variable, function, or alias template TMPL with
20877 the template arguments in TARG_PTR. */
20878
20879 static tree
20880 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20881 {
20882 tree targ_ptr = orig_args;
20883 tree fndecl;
20884 tree gen_tmpl;
20885 tree spec;
20886 bool access_ok = true;
20887
20888 if (tmpl == error_mark_node)
20889 return error_mark_node;
20890
20891 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20892
20893 /* If this function is a clone, handle it specially. */
20894 if (DECL_CLONED_FUNCTION_P (tmpl))
20895 {
20896 tree spec;
20897 tree clone;
20898
20899 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20900 DECL_CLONED_FUNCTION. */
20901 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20902 targ_ptr, complain);
20903 if (spec == error_mark_node)
20904 return error_mark_node;
20905
20906 /* Look for the clone. */
20907 FOR_EACH_CLONE (clone, spec)
20908 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20909 return clone;
20910 /* We should always have found the clone by now. */
20911 gcc_unreachable ();
20912 return NULL_TREE;
20913 }
20914
20915 if (targ_ptr == error_mark_node)
20916 return error_mark_node;
20917
20918 /* Check to see if we already have this specialization. */
20919 gen_tmpl = most_general_template (tmpl);
20920 if (TMPL_ARGS_DEPTH (targ_ptr)
20921 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20922 /* targ_ptr only has the innermost template args, so add the outer ones
20923 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20924 the case of a non-dependent call within a template definition). */
20925 targ_ptr = (add_outermost_template_args
20926 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20927 targ_ptr));
20928
20929 if (modules_p ())
20930 {
20931 tree origin = get_originating_module_decl (gen_tmpl);
20932 load_pending_specializations (CP_DECL_CONTEXT (origin),
20933 DECL_NAME (origin));
20934 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl))
20935 lazy_load_specializations (gen_tmpl);
20936 }
20937
20938 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20939 but it doesn't seem to be on the hot path. */
20940 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20941
20942 gcc_checking_assert (tmpl == gen_tmpl
20943 || ((fndecl
20944 = retrieve_specialization (tmpl, orig_args, 0))
20945 == spec)
20946 || fndecl == NULL_TREE);
20947
20948 if (spec != NULL_TREE)
20949 {
20950 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20951 {
20952 if (complain & tf_error)
20953 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20954 return error_mark_node;
20955 }
20956 return spec;
20957 }
20958
20959 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20960 complain))
20961 return error_mark_node;
20962
20963 /* We are building a FUNCTION_DECL, during which the access of its
20964 parameters and return types have to be checked. However this
20965 FUNCTION_DECL which is the desired context for access checking
20966 is not built yet. We solve this chicken-and-egg problem by
20967 deferring all checks until we have the FUNCTION_DECL. */
20968 push_deferring_access_checks (dk_deferred);
20969
20970 /* Instantiation of the function happens in the context of the function
20971 template, not the context of the overload resolution we're doing. */
20972 push_to_top_level ();
20973 /* If there are dependent arguments, e.g. because we're doing partial
20974 ordering, make sure processing_template_decl stays set. */
20975 if (uses_template_parms (targ_ptr))
20976 ++processing_template_decl;
20977 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20978 {
20979 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20980 complain, gen_tmpl, true);
20981 push_nested_class (ctx);
20982 }
20983
20984 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20985
20986 fndecl = NULL_TREE;
20987 if (VAR_P (pattern))
20988 {
20989 /* We need to determine if we're using a partial or explicit
20990 specialization now, because the type of the variable could be
20991 different. */
20992 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20993 tree elt = most_specialized_partial_spec (tid, complain);
20994 if (elt == error_mark_node)
20995 pattern = error_mark_node;
20996 else if (elt)
20997 {
20998 tree partial_tmpl = TREE_VALUE (elt);
20999 tree partial_args = TREE_PURPOSE (elt);
21000 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21001 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21002 }
21003 }
21004
21005 /* Substitute template parameters to obtain the specialization. */
21006 if (fndecl == NULL_TREE)
21007 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21008 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21009 pop_nested_class ();
21010 pop_from_top_level ();
21011
21012 if (fndecl == error_mark_node)
21013 {
21014 pop_deferring_access_checks ();
21015 return error_mark_node;
21016 }
21017
21018 /* The DECL_TI_TEMPLATE should always be the immediate parent
21019 template, not the most general template. */
21020 DECL_TI_TEMPLATE (fndecl) = tmpl;
21021 DECL_TI_ARGS (fndecl) = targ_ptr;
21022
21023 set_instantiating_module (fndecl);
21024
21025 /* Now we know the specialization, compute access previously
21026 deferred. Do no access control for inheriting constructors,
21027 as we already checked access for the inherited constructor. */
21028 if (!(flag_new_inheriting_ctors
21029 && DECL_INHERITED_CTOR (fndecl)))
21030 {
21031 push_access_scope (fndecl);
21032 if (!perform_deferred_access_checks (complain))
21033 access_ok = false;
21034 pop_access_scope (fndecl);
21035 }
21036 pop_deferring_access_checks ();
21037
21038 /* If we've just instantiated the main entry point for a function,
21039 instantiate all the alternate entry points as well. We do this
21040 by cloning the instantiation of the main entry point, not by
21041 instantiating the template clones. */
21042 if (tree chain = DECL_CHAIN (gen_tmpl))
21043 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21044 clone_cdtor (fndecl, /*update_methods=*/false);
21045
21046 if (!access_ok)
21047 {
21048 if (!(complain & tf_error))
21049 {
21050 /* Remember to reinstantiate when we're out of SFINAE so the user
21051 can see the errors. */
21052 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21053 }
21054 return error_mark_node;
21055 }
21056 return fndecl;
21057 }
21058
21059 /* Wrapper for instantiate_template_1. */
21060
21061 tree
21062 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21063 {
21064 tree ret;
21065 timevar_push (TV_TEMPLATE_INST);
21066 ret = instantiate_template_1 (tmpl, orig_args, complain);
21067 timevar_pop (TV_TEMPLATE_INST);
21068 return ret;
21069 }
21070
21071 /* Instantiate the alias template TMPL with ARGS. Also push a template
21072 instantiation level, which instantiate_template doesn't do because
21073 functions and variables have sufficient context established by the
21074 callers. */
21075
21076 static tree
21077 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21078 {
21079 if (tmpl == error_mark_node || args == error_mark_node)
21080 return error_mark_node;
21081
21082 args =
21083 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21084 args, tmpl, complain,
21085 /*require_all_args=*/true,
21086 /*use_default_args=*/true);
21087
21088 /* FIXME check for satisfaction in check_instantiated_args. */
21089 if (flag_concepts
21090 && !any_dependent_template_arguments_p (args)
21091 && !constraints_satisfied_p (tmpl, args))
21092 {
21093 if (complain & tf_error)
21094 {
21095 auto_diagnostic_group d;
21096 error ("template constraint failure for %qD", tmpl);
21097 diagnose_constraints (input_location, tmpl, args);
21098 }
21099 return error_mark_node;
21100 }
21101
21102 if (!push_tinst_level (tmpl, args))
21103 return error_mark_node;
21104 tree r = instantiate_template (tmpl, args, complain);
21105 pop_tinst_level ();
21106
21107 return r;
21108 }
21109
21110 /* PARM is a template parameter pack for FN. Returns true iff
21111 PARM is used in a deducible way in the argument list of FN. */
21112
21113 static bool
21114 pack_deducible_p (tree parm, tree fn)
21115 {
21116 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21117 for (; t; t = TREE_CHAIN (t))
21118 {
21119 tree type = TREE_VALUE (t);
21120 tree packs;
21121 if (!PACK_EXPANSION_P (type))
21122 continue;
21123 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21124 packs; packs = TREE_CHAIN (packs))
21125 if (template_args_equal (TREE_VALUE (packs), parm))
21126 {
21127 /* The template parameter pack is used in a function parameter
21128 pack. If this is the end of the parameter list, the
21129 template parameter pack is deducible. */
21130 if (TREE_CHAIN (t) == void_list_node)
21131 return true;
21132 else
21133 /* Otherwise, not. Well, it could be deduced from
21134 a non-pack parameter, but doing so would end up with
21135 a deduction mismatch, so don't bother. */
21136 return false;
21137 }
21138 }
21139 /* The template parameter pack isn't used in any function parameter
21140 packs, but it might be used deeper, e.g. tuple<Args...>. */
21141 return true;
21142 }
21143
21144 /* Subroutine of fn_type_unification: check non-dependent parms for
21145 convertibility. */
21146
21147 static int
21148 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21149 tree fn, unification_kind_t strict, int flags,
21150 struct conversion **convs, bool explain_p)
21151 {
21152 /* Non-constructor methods need to leave a conversion for 'this', which
21153 isn't included in nargs here. */
21154 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21155 && !DECL_CONSTRUCTOR_P (fn));
21156
21157 for (unsigned ia = 0;
21158 parms && parms != void_list_node && ia < nargs; )
21159 {
21160 tree parm = TREE_VALUE (parms);
21161
21162 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21163 && (!TREE_CHAIN (parms)
21164 || TREE_CHAIN (parms) == void_list_node))
21165 /* For a function parameter pack that occurs at the end of the
21166 parameter-declaration-list, the type A of each remaining
21167 argument of the call is compared with the type P of the
21168 declarator-id of the function parameter pack. */
21169 break;
21170
21171 parms = TREE_CHAIN (parms);
21172
21173 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21174 /* For a function parameter pack that does not occur at the
21175 end of the parameter-declaration-list, the type of the
21176 parameter pack is a non-deduced context. */
21177 continue;
21178
21179 if (!uses_template_parms (parm))
21180 {
21181 tree arg = args[ia];
21182 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21183 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21184
21185 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21186 conv_p, explain_p))
21187 return 1;
21188 }
21189
21190 ++ia;
21191 }
21192
21193 return 0;
21194 }
21195
21196 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21197 NARGS elements of the arguments that are being used when calling
21198 it. TARGS is a vector into which the deduced template arguments
21199 are placed.
21200
21201 Returns either a FUNCTION_DECL for the matching specialization of FN or
21202 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21203 true, diagnostics will be printed to explain why it failed.
21204
21205 If FN is a conversion operator, or we are trying to produce a specific
21206 specialization, RETURN_TYPE is the return type desired.
21207
21208 The EXPLICIT_TARGS are explicit template arguments provided via a
21209 template-id.
21210
21211 The parameter STRICT is one of:
21212
21213 DEDUCE_CALL:
21214 We are deducing arguments for a function call, as in
21215 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21216 deducing arguments for a call to the result of a conversion
21217 function template, as in [over.call.object].
21218
21219 DEDUCE_CONV:
21220 We are deducing arguments for a conversion function, as in
21221 [temp.deduct.conv].
21222
21223 DEDUCE_EXACT:
21224 We are deducing arguments when doing an explicit instantiation
21225 as in [temp.explicit], when determining an explicit specialization
21226 as in [temp.expl.spec], or when taking the address of a function
21227 template, as in [temp.deduct.funcaddr]. */
21228
21229 tree
21230 fn_type_unification (tree fn,
21231 tree explicit_targs,
21232 tree targs,
21233 const tree *args,
21234 unsigned int nargs,
21235 tree return_type,
21236 unification_kind_t strict,
21237 int flags,
21238 struct conversion **convs,
21239 bool explain_p,
21240 bool decltype_p)
21241 {
21242 tree parms;
21243 tree fntype;
21244 tree decl = NULL_TREE;
21245 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21246 bool ok;
21247 static int deduction_depth;
21248 /* type_unification_real will pass back any access checks from default
21249 template argument substitution. */
21250 vec<deferred_access_check, va_gc> *checks = NULL;
21251 /* We don't have all the template args yet. */
21252 bool incomplete = true;
21253
21254 tree orig_fn = fn;
21255 if (flag_new_inheriting_ctors)
21256 fn = strip_inheriting_ctors (fn);
21257
21258 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21259 tree r = error_mark_node;
21260
21261 tree full_targs = targs;
21262 if (TMPL_ARGS_DEPTH (targs)
21263 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21264 full_targs = (add_outermost_template_args
21265 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21266 targs));
21267
21268 if (decltype_p)
21269 complain |= tf_decltype;
21270
21271 /* In C++0x, it's possible to have a function template whose type depends
21272 on itself recursively. This is most obvious with decltype, but can also
21273 occur with enumeration scope (c++/48969). So we need to catch infinite
21274 recursion and reject the substitution at deduction time; this function
21275 will return error_mark_node for any repeated substitution.
21276
21277 This also catches excessive recursion such as when f<N> depends on
21278 f<N-1> across all integers, and returns error_mark_node for all the
21279 substitutions back up to the initial one.
21280
21281 This is, of course, not reentrant. */
21282 if (excessive_deduction_depth)
21283 return error_mark_node;
21284 ++deduction_depth;
21285
21286 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21287
21288 fntype = TREE_TYPE (fn);
21289 if (explicit_targs)
21290 {
21291 /* [temp.deduct]
21292
21293 The specified template arguments must match the template
21294 parameters in kind (i.e., type, nontype, template), and there
21295 must not be more arguments than there are parameters;
21296 otherwise type deduction fails.
21297
21298 Nontype arguments must match the types of the corresponding
21299 nontype template parameters, or must be convertible to the
21300 types of the corresponding nontype parameters as specified in
21301 _temp.arg.nontype_, otherwise type deduction fails.
21302
21303 All references in the function type of the function template
21304 to the corresponding template parameters are replaced by the
21305 specified template argument values. If a substitution in a
21306 template parameter or in the function type of the function
21307 template results in an invalid type, type deduction fails. */
21308 int i, len = TREE_VEC_LENGTH (tparms);
21309 location_t loc = input_location;
21310 incomplete = false;
21311
21312 if (explicit_targs == error_mark_node)
21313 goto fail;
21314
21315 if (TMPL_ARGS_DEPTH (explicit_targs)
21316 < TMPL_ARGS_DEPTH (full_targs))
21317 explicit_targs = add_outermost_template_args (full_targs,
21318 explicit_targs);
21319
21320 /* Adjust any explicit template arguments before entering the
21321 substitution context. */
21322 explicit_targs
21323 = (coerce_template_parms (tparms, explicit_targs, fn,
21324 complain|tf_partial,
21325 /*require_all_args=*/false,
21326 /*use_default_args=*/false));
21327 if (explicit_targs == error_mark_node)
21328 goto fail;
21329
21330 /* Substitute the explicit args into the function type. This is
21331 necessary so that, for instance, explicitly declared function
21332 arguments can match null pointed constants. If we were given
21333 an incomplete set of explicit args, we must not do semantic
21334 processing during substitution as we could create partial
21335 instantiations. */
21336 for (i = 0; i < len; i++)
21337 {
21338 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21339 bool parameter_pack = false;
21340 tree targ = TREE_VEC_ELT (explicit_targs, i);
21341
21342 /* Dig out the actual parm. */
21343 if (TREE_CODE (parm) == TYPE_DECL
21344 || TREE_CODE (parm) == TEMPLATE_DECL)
21345 {
21346 parm = TREE_TYPE (parm);
21347 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21348 }
21349 else if (TREE_CODE (parm) == PARM_DECL)
21350 {
21351 parm = DECL_INITIAL (parm);
21352 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21353 }
21354
21355 if (targ == NULL_TREE)
21356 /* No explicit argument for this template parameter. */
21357 incomplete = true;
21358 else if (parameter_pack && pack_deducible_p (parm, fn))
21359 {
21360 /* Mark the argument pack as "incomplete". We could
21361 still deduce more arguments during unification.
21362 We remove this mark in type_unification_real. */
21363 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21364 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21365 = ARGUMENT_PACK_ARGS (targ);
21366
21367 /* We have some incomplete argument packs. */
21368 incomplete = true;
21369 }
21370 }
21371
21372 if (incomplete)
21373 {
21374 if (!push_tinst_level (fn, explicit_targs))
21375 {
21376 excessive_deduction_depth = true;
21377 goto fail;
21378 }
21379 ++processing_template_decl;
21380 input_location = DECL_SOURCE_LOCATION (fn);
21381 /* Ignore any access checks; we'll see them again in
21382 instantiate_template and they might have the wrong
21383 access path at this point. */
21384 push_deferring_access_checks (dk_deferred);
21385 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21386 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21387 pop_deferring_access_checks ();
21388 input_location = loc;
21389 --processing_template_decl;
21390 pop_tinst_level ();
21391
21392 if (fntype == error_mark_node)
21393 goto fail;
21394 }
21395
21396 /* Place the explicitly specified arguments in TARGS. */
21397 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21398 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21399 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21400 if (!incomplete && CHECKING_P
21401 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21402 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21403 (targs, NUM_TMPL_ARGS (explicit_targs));
21404 }
21405
21406 if (return_type && strict != DEDUCE_CALL)
21407 {
21408 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21409 new_args[0] = return_type;
21410 memcpy (new_args + 1, args, nargs * sizeof (tree));
21411 args = new_args;
21412 ++nargs;
21413 }
21414
21415 if (!incomplete)
21416 goto deduced;
21417
21418 /* Never do unification on the 'this' parameter. */
21419 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21420
21421 if (return_type && strict == DEDUCE_CALL)
21422 {
21423 /* We're deducing for a call to the result of a template conversion
21424 function. The parms we really want are in return_type. */
21425 if (INDIRECT_TYPE_P (return_type))
21426 return_type = TREE_TYPE (return_type);
21427 parms = TYPE_ARG_TYPES (return_type);
21428 }
21429 else if (return_type)
21430 {
21431 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21432 }
21433
21434 /* We allow incomplete unification without an error message here
21435 because the standard doesn't seem to explicitly prohibit it. Our
21436 callers must be ready to deal with unification failures in any
21437 event. */
21438
21439 /* If we aren't explaining yet, push tinst context so we can see where
21440 any errors (e.g. from class instantiations triggered by instantiation
21441 of default template arguments) come from. If we are explaining, this
21442 context is redundant. */
21443 if (!explain_p && !push_tinst_level (fn, targs))
21444 {
21445 excessive_deduction_depth = true;
21446 goto fail;
21447 }
21448
21449 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21450 full_targs, parms, args, nargs, /*subr=*/0,
21451 strict, &checks, explain_p);
21452 if (!explain_p)
21453 pop_tinst_level ();
21454 if (!ok)
21455 goto fail;
21456
21457 /* Now that we have bindings for all of the template arguments,
21458 ensure that the arguments deduced for the template template
21459 parameters have compatible template parameter lists. We cannot
21460 check this property before we have deduced all template
21461 arguments, because the template parameter types of a template
21462 template parameter might depend on prior template parameters
21463 deduced after the template template parameter. The following
21464 ill-formed example illustrates this issue:
21465
21466 template<typename T, template<T> class C> void f(C<5>, T);
21467
21468 template<int N> struct X {};
21469
21470 void g() {
21471 f(X<5>(), 5l); // error: template argument deduction fails
21472 }
21473
21474 The template parameter list of 'C' depends on the template type
21475 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21476 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21477 time that we deduce 'C'. */
21478 if (!template_template_parm_bindings_ok_p
21479 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21480 {
21481 unify_inconsistent_template_template_parameters (explain_p);
21482 goto fail;
21483 }
21484
21485 deduced:
21486
21487 /* CWG2369: Check satisfaction before non-deducible conversions. */
21488 if (!constraints_satisfied_p (fn, targs))
21489 {
21490 if (explain_p)
21491 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21492 goto fail;
21493 }
21494
21495 /* DR 1391: All parameters have args, now check non-dependent parms for
21496 convertibility. We don't do this if all args were explicitly specified,
21497 as the standard says that we substitute explicit args immediately. */
21498 if (incomplete
21499 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21500 convs, explain_p))
21501 goto fail;
21502
21503 /* All is well so far. Now, check:
21504
21505 [temp.deduct]
21506
21507 When all template arguments have been deduced, all uses of
21508 template parameters in nondeduced contexts are replaced with
21509 the corresponding deduced argument values. If the
21510 substitution results in an invalid type, as described above,
21511 type deduction fails. */
21512 if (!push_tinst_level (fn, targs))
21513 {
21514 excessive_deduction_depth = true;
21515 goto fail;
21516 }
21517
21518 /* Also collect access checks from the instantiation. */
21519 reopen_deferring_access_checks (checks);
21520
21521 decl = instantiate_template (fn, targs, complain);
21522
21523 checks = get_deferred_access_checks ();
21524 pop_deferring_access_checks ();
21525
21526 pop_tinst_level ();
21527
21528 if (decl == error_mark_node)
21529 goto fail;
21530
21531 /* Now perform any access checks encountered during substitution. */
21532 push_access_scope (decl);
21533 ok = perform_access_checks (checks, complain);
21534 pop_access_scope (decl);
21535 if (!ok)
21536 goto fail;
21537
21538 /* If we're looking for an exact match, check that what we got
21539 is indeed an exact match. It might not be if some template
21540 parameters are used in non-deduced contexts. But don't check
21541 for an exact match if we have dependent template arguments;
21542 in that case we're doing partial ordering, and we already know
21543 that we have two candidates that will provide the actual type. */
21544 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21545 {
21546 tree substed = TREE_TYPE (decl);
21547 unsigned int i;
21548
21549 tree sarg
21550 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21551 if (return_type)
21552 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21553 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21554 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21555 {
21556 unify_type_mismatch (explain_p, args[i],
21557 TREE_VALUE (sarg));
21558 goto fail;
21559 }
21560 }
21561
21562 /* After doing deduction with the inherited constructor, actually return an
21563 instantiation of the inheriting constructor. */
21564 if (orig_fn != fn)
21565 decl = instantiate_template (orig_fn, targs, complain);
21566
21567 r = decl;
21568
21569 fail:
21570 --deduction_depth;
21571 if (excessive_deduction_depth)
21572 {
21573 if (deduction_depth == 0)
21574 /* Reset once we're all the way out. */
21575 excessive_deduction_depth = false;
21576 }
21577
21578 return r;
21579 }
21580
21581 /* Adjust types before performing type deduction, as described in
21582 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21583 sections are symmetric. PARM is the type of a function parameter
21584 or the return type of the conversion function. ARG is the type of
21585 the argument passed to the call, or the type of the value
21586 initialized with the result of the conversion function.
21587 ARG_EXPR is the original argument expression, which may be null. */
21588
21589 static int
21590 maybe_adjust_types_for_deduction (unification_kind_t strict,
21591 tree* parm,
21592 tree* arg,
21593 tree arg_expr)
21594 {
21595 int result = 0;
21596
21597 switch (strict)
21598 {
21599 case DEDUCE_CALL:
21600 break;
21601
21602 case DEDUCE_CONV:
21603 /* Swap PARM and ARG throughout the remainder of this
21604 function; the handling is precisely symmetric since PARM
21605 will initialize ARG rather than vice versa. */
21606 std::swap (parm, arg);
21607 break;
21608
21609 case DEDUCE_EXACT:
21610 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21611 too, but here handle it by stripping the reference from PARM
21612 rather than by adding it to ARG. */
21613 if (TYPE_REF_P (*parm)
21614 && TYPE_REF_IS_RVALUE (*parm)
21615 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21616 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21617 && TYPE_REF_P (*arg)
21618 && !TYPE_REF_IS_RVALUE (*arg))
21619 *parm = TREE_TYPE (*parm);
21620 /* Nothing else to do in this case. */
21621 return 0;
21622
21623 default:
21624 gcc_unreachable ();
21625 }
21626
21627 if (!TYPE_REF_P (*parm))
21628 {
21629 /* [temp.deduct.call]
21630
21631 If P is not a reference type:
21632
21633 --If A is an array type, the pointer type produced by the
21634 array-to-pointer standard conversion (_conv.array_) is
21635 used in place of A for type deduction; otherwise,
21636
21637 --If A is a function type, the pointer type produced by
21638 the function-to-pointer standard conversion
21639 (_conv.func_) is used in place of A for type deduction;
21640 otherwise,
21641
21642 --If A is a cv-qualified type, the top level
21643 cv-qualifiers of A's type are ignored for type
21644 deduction. */
21645 if (TREE_CODE (*arg) == ARRAY_TYPE)
21646 *arg = build_pointer_type (TREE_TYPE (*arg));
21647 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21648 *arg = build_pointer_type (*arg);
21649 else
21650 *arg = TYPE_MAIN_VARIANT (*arg);
21651 }
21652
21653 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21654 reference to a cv-unqualified template parameter that does not represent a
21655 template parameter of a class template (during class template argument
21656 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21657 an lvalue, the type "lvalue reference to A" is used in place of A for type
21658 deduction. */
21659 if (TYPE_REF_P (*parm)
21660 && TYPE_REF_IS_RVALUE (*parm)
21661 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21662 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21663 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21664 && (arg_expr ? lvalue_p (arg_expr)
21665 /* try_one_overload doesn't provide an arg_expr, but
21666 functions are always lvalues. */
21667 : TREE_CODE (*arg) == FUNCTION_TYPE))
21668 *arg = build_reference_type (*arg);
21669
21670 /* [temp.deduct.call]
21671
21672 If P is a cv-qualified type, the top level cv-qualifiers
21673 of P's type are ignored for type deduction. If P is a
21674 reference type, the type referred to by P is used for
21675 type deduction. */
21676 *parm = TYPE_MAIN_VARIANT (*parm);
21677 if (TYPE_REF_P (*parm))
21678 {
21679 *parm = TREE_TYPE (*parm);
21680 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21681 }
21682
21683 /* DR 322. For conversion deduction, remove a reference type on parm
21684 too (which has been swapped into ARG). */
21685 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21686 *arg = TREE_TYPE (*arg);
21687
21688 return result;
21689 }
21690
21691 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21692 template which doesn't contain any deducible template parameters; check if
21693 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21694 unify_one_argument. */
21695
21696 static int
21697 check_non_deducible_conversion (tree parm, tree arg, int strict,
21698 int flags, struct conversion **conv_p,
21699 bool explain_p)
21700 {
21701 tree type;
21702
21703 if (!TYPE_P (arg))
21704 type = TREE_TYPE (arg);
21705 else
21706 type = arg;
21707
21708 if (same_type_p (parm, type))
21709 return unify_success (explain_p);
21710
21711 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21712 if (strict == DEDUCE_CONV)
21713 {
21714 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21715 return unify_success (explain_p);
21716 }
21717 else if (strict != DEDUCE_EXACT)
21718 {
21719 bool ok = false;
21720 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21721 if (conv_p)
21722 /* Avoid recalculating this in add_function_candidate. */
21723 ok = (*conv_p
21724 = good_conversion (parm, type, conv_arg, flags, complain));
21725 else
21726 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21727 if (ok)
21728 return unify_success (explain_p);
21729 }
21730
21731 if (strict == DEDUCE_EXACT)
21732 return unify_type_mismatch (explain_p, parm, arg);
21733 else
21734 return unify_arg_conversion (explain_p, parm, type, arg);
21735 }
21736
21737 static bool uses_deducible_template_parms (tree type);
21738
21739 /* Returns true iff the expression EXPR is one from which a template
21740 argument can be deduced. In other words, if it's an undecorated
21741 use of a template non-type parameter. */
21742
21743 static bool
21744 deducible_expression (tree expr)
21745 {
21746 /* Strip implicit conversions. */
21747 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21748 expr = TREE_OPERAND (expr, 0);
21749 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21750 }
21751
21752 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21753 deducible way; that is, if it has a max value of <PARM> - 1. */
21754
21755 static bool
21756 deducible_array_bound (tree domain)
21757 {
21758 if (domain == NULL_TREE)
21759 return false;
21760
21761 tree max = TYPE_MAX_VALUE (domain);
21762 if (TREE_CODE (max) != MINUS_EXPR)
21763 return false;
21764
21765 return deducible_expression (TREE_OPERAND (max, 0));
21766 }
21767
21768 /* Returns true iff the template arguments ARGS use a template parameter
21769 in a deducible way. */
21770
21771 static bool
21772 deducible_template_args (tree args)
21773 {
21774 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21775 {
21776 bool deducible;
21777 tree elt = TREE_VEC_ELT (args, i);
21778 if (ARGUMENT_PACK_P (elt))
21779 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21780 else
21781 {
21782 if (PACK_EXPANSION_P (elt))
21783 elt = PACK_EXPANSION_PATTERN (elt);
21784 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21785 deducible = true;
21786 else if (TYPE_P (elt))
21787 deducible = uses_deducible_template_parms (elt);
21788 else
21789 deducible = deducible_expression (elt);
21790 }
21791 if (deducible)
21792 return true;
21793 }
21794 return false;
21795 }
21796
21797 /* Returns true iff TYPE contains any deducible references to template
21798 parameters, as per 14.8.2.5. */
21799
21800 static bool
21801 uses_deducible_template_parms (tree type)
21802 {
21803 if (PACK_EXPANSION_P (type))
21804 type = PACK_EXPANSION_PATTERN (type);
21805
21806 /* T
21807 cv-list T
21808 TT<T>
21809 TT<i>
21810 TT<> */
21811 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21812 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21813 return true;
21814
21815 /* T*
21816 T&
21817 T&& */
21818 if (INDIRECT_TYPE_P (type))
21819 return uses_deducible_template_parms (TREE_TYPE (type));
21820
21821 /* T[integer-constant ]
21822 type [i] */
21823 if (TREE_CODE (type) == ARRAY_TYPE)
21824 return (uses_deducible_template_parms (TREE_TYPE (type))
21825 || deducible_array_bound (TYPE_DOMAIN (type)));
21826
21827 /* T type ::*
21828 type T::*
21829 T T::*
21830 T (type ::*)()
21831 type (T::*)()
21832 type (type ::*)(T)
21833 type (T::*)(T)
21834 T (type ::*)(T)
21835 T (T::*)()
21836 T (T::*)(T) */
21837 if (TYPE_PTRMEM_P (type))
21838 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21839 || (uses_deducible_template_parms
21840 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21841
21842 /* template-name <T> (where template-name refers to a class template)
21843 template-name <i> (where template-name refers to a class template) */
21844 if (CLASS_TYPE_P (type)
21845 && CLASSTYPE_TEMPLATE_INFO (type)
21846 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21847 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21848 (CLASSTYPE_TI_ARGS (type)));
21849
21850 /* type (T)
21851 T()
21852 T(T) */
21853 if (FUNC_OR_METHOD_TYPE_P (type))
21854 {
21855 if (uses_deducible_template_parms (TREE_TYPE (type)))
21856 return true;
21857 tree parm = TYPE_ARG_TYPES (type);
21858 if (TREE_CODE (type) == METHOD_TYPE)
21859 parm = TREE_CHAIN (parm);
21860 for (; parm; parm = TREE_CHAIN (parm))
21861 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21862 return true;
21863 }
21864
21865 return false;
21866 }
21867
21868 /* Subroutine of type_unification_real and unify_pack_expansion to
21869 handle unification of a single P/A pair. Parameters are as
21870 for those functions. */
21871
21872 static int
21873 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21874 int subr, unification_kind_t strict,
21875 bool explain_p)
21876 {
21877 tree arg_expr = NULL_TREE;
21878 int arg_strict;
21879
21880 if (arg == error_mark_node || parm == error_mark_node)
21881 return unify_invalid (explain_p);
21882 if (arg == unknown_type_node)
21883 /* We can't deduce anything from this, but we might get all the
21884 template args from other function args. */
21885 return unify_success (explain_p);
21886
21887 /* Implicit conversions (Clause 4) will be performed on a function
21888 argument to convert it to the type of the corresponding function
21889 parameter if the parameter type contains no template-parameters that
21890 participate in template argument deduction. */
21891 if (strict != DEDUCE_EXACT
21892 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21893 /* For function parameters with no deducible template parameters,
21894 just return. We'll check non-dependent conversions later. */
21895 return unify_success (explain_p);
21896
21897 switch (strict)
21898 {
21899 case DEDUCE_CALL:
21900 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21901 | UNIFY_ALLOW_MORE_CV_QUAL
21902 | UNIFY_ALLOW_DERIVED);
21903 break;
21904
21905 case DEDUCE_CONV:
21906 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21907 break;
21908
21909 case DEDUCE_EXACT:
21910 arg_strict = UNIFY_ALLOW_NONE;
21911 break;
21912
21913 default:
21914 gcc_unreachable ();
21915 }
21916
21917 /* We only do these transformations if this is the top-level
21918 parameter_type_list in a call or declaration matching; in other
21919 situations (nested function declarators, template argument lists) we
21920 won't be comparing a type to an expression, and we don't do any type
21921 adjustments. */
21922 if (!subr)
21923 {
21924 if (!TYPE_P (arg))
21925 {
21926 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21927 if (type_unknown_p (arg))
21928 {
21929 /* [temp.deduct.type] A template-argument can be
21930 deduced from a pointer to function or pointer
21931 to member function argument if the set of
21932 overloaded functions does not contain function
21933 templates and at most one of a set of
21934 overloaded functions provides a unique
21935 match. */
21936 resolve_overloaded_unification (tparms, targs, parm,
21937 arg, strict,
21938 arg_strict, explain_p);
21939 /* If a unique match was not found, this is a
21940 non-deduced context, so we still succeed. */
21941 return unify_success (explain_p);
21942 }
21943
21944 arg_expr = arg;
21945 arg = unlowered_expr_type (arg);
21946 if (arg == error_mark_node)
21947 return unify_invalid (explain_p);
21948 }
21949
21950 arg_strict |=
21951 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21952 }
21953 else
21954 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21955 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21956 return unify_template_argument_mismatch (explain_p, parm, arg);
21957
21958 /* For deduction from an init-list we need the actual list. */
21959 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21960 arg = arg_expr;
21961 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21962 }
21963
21964 /* for_each_template_parm callback that always returns 0. */
21965
21966 static int
21967 zero_r (tree, void *)
21968 {
21969 return 0;
21970 }
21971
21972 /* for_each_template_parm any_fn callback to handle deduction of a template
21973 type argument from the type of an array bound. */
21974
21975 static int
21976 array_deduction_r (tree t, void *data)
21977 {
21978 tree_pair_p d = (tree_pair_p)data;
21979 tree &tparms = d->purpose;
21980 tree &targs = d->value;
21981
21982 if (TREE_CODE (t) == ARRAY_TYPE)
21983 if (tree dom = TYPE_DOMAIN (t))
21984 if (tree max = TYPE_MAX_VALUE (dom))
21985 {
21986 if (TREE_CODE (max) == MINUS_EXPR)
21987 max = TREE_OPERAND (max, 0);
21988 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21989 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21990 UNIFY_ALLOW_NONE, /*explain*/false);
21991 }
21992
21993 /* Keep walking. */
21994 return 0;
21995 }
21996
21997 /* Try to deduce any not-yet-deduced template type arguments from the type of
21998 an array bound. This is handled separately from unify because 14.8.2.5 says
21999 "The type of a type parameter is only deduced from an array bound if it is
22000 not otherwise deduced." */
22001
22002 static void
22003 try_array_deduction (tree tparms, tree targs, tree parm)
22004 {
22005 tree_pair_s data = { tparms, targs };
22006 hash_set<tree> visited;
22007 for_each_template_parm (parm, zero_r, &data, &visited,
22008 /*nondeduced*/false, array_deduction_r);
22009 }
22010
22011 /* Most parms like fn_type_unification.
22012
22013 If SUBR is 1, we're being called recursively (to unify the
22014 arguments of a function or method parameter of a function
22015 template).
22016
22017 CHECKS is a pointer to a vector of access checks encountered while
22018 substituting default template arguments. */
22019
22020 static int
22021 type_unification_real (tree tparms,
22022 tree full_targs,
22023 tree xparms,
22024 const tree *xargs,
22025 unsigned int xnargs,
22026 int subr,
22027 unification_kind_t strict,
22028 vec<deferred_access_check, va_gc> **checks,
22029 bool explain_p)
22030 {
22031 tree parm, arg;
22032 int i;
22033 int ntparms = TREE_VEC_LENGTH (tparms);
22034 int saw_undeduced = 0;
22035 tree parms;
22036 const tree *args;
22037 unsigned int nargs;
22038 unsigned int ia;
22039
22040 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22041 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22042 gcc_assert (ntparms > 0);
22043
22044 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22045
22046 /* Reset the number of non-defaulted template arguments contained
22047 in TARGS. */
22048 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22049
22050 again:
22051 parms = xparms;
22052 args = xargs;
22053 nargs = xnargs;
22054
22055 ia = 0;
22056 while (parms && parms != void_list_node
22057 && ia < nargs)
22058 {
22059 parm = TREE_VALUE (parms);
22060
22061 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22062 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22063 /* For a function parameter pack that occurs at the end of the
22064 parameter-declaration-list, the type A of each remaining
22065 argument of the call is compared with the type P of the
22066 declarator-id of the function parameter pack. */
22067 break;
22068
22069 parms = TREE_CHAIN (parms);
22070
22071 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22072 /* For a function parameter pack that does not occur at the
22073 end of the parameter-declaration-list, the type of the
22074 parameter pack is a non-deduced context. */
22075 continue;
22076
22077 arg = args[ia];
22078 ++ia;
22079
22080 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22081 explain_p))
22082 return 1;
22083 }
22084
22085 if (parms
22086 && parms != void_list_node
22087 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22088 {
22089 /* Unify the remaining arguments with the pack expansion type. */
22090 tree argvec;
22091 tree parmvec = make_tree_vec (1);
22092
22093 /* Allocate a TREE_VEC and copy in all of the arguments */
22094 argvec = make_tree_vec (nargs - ia);
22095 for (i = 0; ia < nargs; ++ia, ++i)
22096 TREE_VEC_ELT (argvec, i) = args[ia];
22097
22098 /* Copy the parameter into parmvec. */
22099 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22100 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22101 /*subr=*/subr, explain_p))
22102 return 1;
22103
22104 /* Advance to the end of the list of parameters. */
22105 parms = TREE_CHAIN (parms);
22106 }
22107
22108 /* Fail if we've reached the end of the parm list, and more args
22109 are present, and the parm list isn't variadic. */
22110 if (ia < nargs && parms == void_list_node)
22111 return unify_too_many_arguments (explain_p, nargs, ia);
22112 /* Fail if parms are left and they don't have default values and
22113 they aren't all deduced as empty packs (c++/57397). This is
22114 consistent with sufficient_parms_p. */
22115 if (parms && parms != void_list_node
22116 && TREE_PURPOSE (parms) == NULL_TREE)
22117 {
22118 unsigned int count = nargs;
22119 tree p = parms;
22120 bool type_pack_p;
22121 do
22122 {
22123 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22124 if (!type_pack_p)
22125 count++;
22126 p = TREE_CHAIN (p);
22127 }
22128 while (p && p != void_list_node);
22129 if (count != nargs)
22130 return unify_too_few_arguments (explain_p, ia, count,
22131 type_pack_p);
22132 }
22133
22134 if (!subr)
22135 {
22136 tsubst_flags_t complain = (explain_p
22137 ? tf_warning_or_error
22138 : tf_none);
22139 bool tried_array_deduction = (cxx_dialect < cxx17);
22140
22141 for (i = 0; i < ntparms; i++)
22142 {
22143 tree targ = TREE_VEC_ELT (targs, i);
22144 tree tparm = TREE_VEC_ELT (tparms, i);
22145
22146 /* Clear the "incomplete" flags on all argument packs now so that
22147 substituting them into later default arguments works. */
22148 if (targ && ARGUMENT_PACK_P (targ))
22149 {
22150 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22151 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22152 }
22153
22154 if (targ || tparm == error_mark_node)
22155 continue;
22156 tparm = TREE_VALUE (tparm);
22157
22158 if (TREE_CODE (tparm) == TYPE_DECL
22159 && !tried_array_deduction)
22160 {
22161 try_array_deduction (tparms, targs, xparms);
22162 tried_array_deduction = true;
22163 if (TREE_VEC_ELT (targs, i))
22164 continue;
22165 }
22166
22167 /* If this is an undeduced nontype parameter that depends on
22168 a type parameter, try another pass; its type may have been
22169 deduced from a later argument than the one from which
22170 this parameter can be deduced. */
22171 if (TREE_CODE (tparm) == PARM_DECL
22172 && uses_template_parms (TREE_TYPE (tparm))
22173 && saw_undeduced < 2)
22174 {
22175 saw_undeduced = 1;
22176 continue;
22177 }
22178
22179 /* Core issue #226 (C++0x) [temp.deduct]:
22180
22181 If a template argument has not been deduced, its
22182 default template argument, if any, is used.
22183
22184 When we are in C++98 mode, TREE_PURPOSE will either
22185 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22186 to explicitly check cxx_dialect here. */
22187 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22188 /* OK, there is a default argument. Wait until after the
22189 conversion check to do substitution. */
22190 continue;
22191
22192 /* If the type parameter is a parameter pack, then it will
22193 be deduced to an empty parameter pack. */
22194 if (template_parameter_pack_p (tparm))
22195 {
22196 tree arg;
22197
22198 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22199 {
22200 arg = make_node (NONTYPE_ARGUMENT_PACK);
22201 TREE_CONSTANT (arg) = 1;
22202 }
22203 else
22204 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22205
22206 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22207
22208 TREE_VEC_ELT (targs, i) = arg;
22209 continue;
22210 }
22211
22212 return unify_parameter_deduction_failure (explain_p, tparm);
22213 }
22214
22215 /* Now substitute into the default template arguments. */
22216 for (i = 0; i < ntparms; i++)
22217 {
22218 tree targ = TREE_VEC_ELT (targs, i);
22219 tree tparm = TREE_VEC_ELT (tparms, i);
22220
22221 if (targ || tparm == error_mark_node)
22222 continue;
22223 tree parm = TREE_VALUE (tparm);
22224 tree arg = TREE_PURPOSE (tparm);
22225 reopen_deferring_access_checks (*checks);
22226 location_t save_loc = input_location;
22227 if (DECL_P (parm))
22228 input_location = DECL_SOURCE_LOCATION (parm);
22229
22230 if (saw_undeduced == 1
22231 && TREE_CODE (parm) == PARM_DECL
22232 && uses_template_parms (TREE_TYPE (parm)))
22233 {
22234 /* The type of this non-type parameter depends on undeduced
22235 parameters. Don't try to use its default argument yet,
22236 since we might deduce an argument for it on the next pass,
22237 but do check whether the arguments we already have cause
22238 substitution failure, so that that happens before we try
22239 later default arguments (78489). */
22240 ++processing_template_decl;
22241 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22242 NULL_TREE);
22243 --processing_template_decl;
22244 if (type == error_mark_node)
22245 arg = error_mark_node;
22246 else
22247 arg = NULL_TREE;
22248 }
22249 else
22250 {
22251 /* Even if the call is happening in template context, getting
22252 here means it's non-dependent, and a default argument is
22253 considered a separate definition under [temp.decls], so we can
22254 do this substitution without processing_template_decl. This
22255 is important if the default argument contains something that
22256 might be instantiation-dependent like access (87480). */
22257 processing_template_decl_sentinel s;
22258 tree substed = NULL_TREE;
22259 if (saw_undeduced == 1)
22260 {
22261 /* First instatiate in template context, in case we still
22262 depend on undeduced template parameters. */
22263 ++processing_template_decl;
22264 substed = tsubst_template_arg (arg, full_targs, complain,
22265 NULL_TREE);
22266 --processing_template_decl;
22267 if (substed != error_mark_node
22268 && !uses_template_parms (substed))
22269 /* We replaced all the tparms, substitute again out of
22270 template context. */
22271 substed = NULL_TREE;
22272 }
22273 if (!substed)
22274 substed = tsubst_template_arg (arg, full_targs, complain,
22275 NULL_TREE);
22276
22277 if (!uses_template_parms (substed))
22278 arg = convert_template_argument (parm, substed, full_targs,
22279 complain, i, NULL_TREE);
22280 else if (saw_undeduced == 1)
22281 arg = NULL_TREE;
22282 else
22283 arg = error_mark_node;
22284 }
22285
22286 input_location = save_loc;
22287 *checks = get_deferred_access_checks ();
22288 pop_deferring_access_checks ();
22289
22290 if (arg == error_mark_node)
22291 return 1;
22292 else if (arg)
22293 {
22294 TREE_VEC_ELT (targs, i) = arg;
22295 /* The position of the first default template argument,
22296 is also the number of non-defaulted arguments in TARGS.
22297 Record that. */
22298 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22299 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22300 }
22301 }
22302
22303 if (saw_undeduced++ == 1)
22304 goto again;
22305 }
22306
22307 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22309
22310 return unify_success (explain_p);
22311 }
22312
22313 /* Subroutine of type_unification_real. Args are like the variables
22314 at the call site. ARG is an overloaded function (or template-id);
22315 we try deducing template args from each of the overloads, and if
22316 only one succeeds, we go with that. Modifies TARGS and returns
22317 true on success. */
22318
22319 static bool
22320 resolve_overloaded_unification (tree tparms,
22321 tree targs,
22322 tree parm,
22323 tree arg,
22324 unification_kind_t strict,
22325 int sub_strict,
22326 bool explain_p)
22327 {
22328 tree tempargs = copy_node (targs);
22329 int good = 0;
22330 tree goodfn = NULL_TREE;
22331 bool addr_p;
22332
22333 if (TREE_CODE (arg) == ADDR_EXPR)
22334 {
22335 arg = TREE_OPERAND (arg, 0);
22336 addr_p = true;
22337 }
22338 else
22339 addr_p = false;
22340
22341 if (TREE_CODE (arg) == COMPONENT_REF)
22342 /* Handle `&x' where `x' is some static or non-static member
22343 function name. */
22344 arg = TREE_OPERAND (arg, 1);
22345
22346 if (TREE_CODE (arg) == OFFSET_REF)
22347 arg = TREE_OPERAND (arg, 1);
22348
22349 /* Strip baselink information. */
22350 if (BASELINK_P (arg))
22351 arg = BASELINK_FUNCTIONS (arg);
22352
22353 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22354 {
22355 /* If we got some explicit template args, we need to plug them into
22356 the affected templates before we try to unify, in case the
22357 explicit args will completely resolve the templates in question. */
22358
22359 int ok = 0;
22360 tree expl_subargs = TREE_OPERAND (arg, 1);
22361 arg = TREE_OPERAND (arg, 0);
22362
22363 for (lkp_iterator iter (arg); iter; ++iter)
22364 {
22365 tree fn = *iter;
22366 tree subargs, elem;
22367
22368 if (TREE_CODE (fn) != TEMPLATE_DECL)
22369 continue;
22370
22371 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22372 expl_subargs, NULL_TREE, tf_none,
22373 /*require_all_args=*/true,
22374 /*use_default_args=*/true);
22375 if (subargs != error_mark_node
22376 && !any_dependent_template_arguments_p (subargs))
22377 {
22378 fn = instantiate_template (fn, subargs, tf_none);
22379 if (!constraints_satisfied_p (fn))
22380 continue;
22381 if (undeduced_auto_decl (fn))
22382 {
22383 /* Instantiate the function to deduce its return type. */
22384 ++function_depth;
22385 instantiate_decl (fn, /*defer*/false, /*class*/false);
22386 --function_depth;
22387 }
22388
22389 if (flag_noexcept_type)
22390 maybe_instantiate_noexcept (fn, tf_none);
22391
22392 elem = TREE_TYPE (fn);
22393 if (try_one_overload (tparms, targs, tempargs, parm,
22394 elem, strict, sub_strict, addr_p, explain_p)
22395 && (!goodfn || !same_type_p (goodfn, elem)))
22396 {
22397 goodfn = elem;
22398 ++good;
22399 }
22400 }
22401 else if (subargs)
22402 ++ok;
22403 }
22404 /* If no templates (or more than one) are fully resolved by the
22405 explicit arguments, this template-id is a non-deduced context; it
22406 could still be OK if we deduce all template arguments for the
22407 enclosing call through other arguments. */
22408 if (good != 1)
22409 good = ok;
22410 }
22411 else if (!OVL_P (arg))
22412 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22413 -- but the deduction does not succeed because the expression is
22414 not just the function on its own. */
22415 return false;
22416 else
22417 for (lkp_iterator iter (arg); iter; ++iter)
22418 {
22419 tree fn = *iter;
22420 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22421 strict, sub_strict, addr_p, explain_p)
22422 && (!goodfn || !decls_match (goodfn, fn)))
22423 {
22424 goodfn = fn;
22425 ++good;
22426 }
22427 }
22428
22429 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22430 to function or pointer to member function argument if the set of
22431 overloaded functions does not contain function templates and at most
22432 one of a set of overloaded functions provides a unique match.
22433
22434 So if we found multiple possibilities, we return success but don't
22435 deduce anything. */
22436
22437 if (good == 1)
22438 {
22439 int i = TREE_VEC_LENGTH (targs);
22440 for (; i--; )
22441 if (TREE_VEC_ELT (tempargs, i))
22442 {
22443 tree old = TREE_VEC_ELT (targs, i);
22444 tree new_ = TREE_VEC_ELT (tempargs, i);
22445 if (new_ && old && ARGUMENT_PACK_P (old)
22446 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22447 /* Don't forget explicit template arguments in a pack. */
22448 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22449 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22450 TREE_VEC_ELT (targs, i) = new_;
22451 }
22452 }
22453 if (good)
22454 return true;
22455
22456 return false;
22457 }
22458
22459 /* Core DR 115: In contexts where deduction is done and fails, or in
22460 contexts where deduction is not done, if a template argument list is
22461 specified and it, along with any default template arguments, identifies
22462 a single function template specialization, then the template-id is an
22463 lvalue for the function template specialization. */
22464
22465 tree
22466 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22467 {
22468 tree expr, offset, baselink;
22469 bool addr;
22470
22471 if (!type_unknown_p (orig_expr))
22472 return orig_expr;
22473
22474 expr = orig_expr;
22475 addr = false;
22476 offset = NULL_TREE;
22477 baselink = NULL_TREE;
22478
22479 if (TREE_CODE (expr) == ADDR_EXPR)
22480 {
22481 expr = TREE_OPERAND (expr, 0);
22482 addr = true;
22483 }
22484 if (TREE_CODE (expr) == OFFSET_REF)
22485 {
22486 offset = expr;
22487 expr = TREE_OPERAND (expr, 1);
22488 }
22489 if (BASELINK_P (expr))
22490 {
22491 baselink = expr;
22492 expr = BASELINK_FUNCTIONS (expr);
22493 }
22494
22495 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22496 {
22497 int good = 0;
22498 tree goodfn = NULL_TREE;
22499
22500 /* If we got some explicit template args, we need to plug them into
22501 the affected templates before we try to unify, in case the
22502 explicit args will completely resolve the templates in question. */
22503
22504 tree expl_subargs = TREE_OPERAND (expr, 1);
22505 tree arg = TREE_OPERAND (expr, 0);
22506 tree badfn = NULL_TREE;
22507 tree badargs = NULL_TREE;
22508
22509 for (lkp_iterator iter (arg); iter; ++iter)
22510 {
22511 tree fn = *iter;
22512 tree subargs, elem;
22513
22514 if (TREE_CODE (fn) != TEMPLATE_DECL)
22515 continue;
22516
22517 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22518 expl_subargs, NULL_TREE, tf_none,
22519 /*require_all_args=*/true,
22520 /*use_default_args=*/true);
22521 if (subargs != error_mark_node
22522 && !any_dependent_template_arguments_p (subargs))
22523 {
22524 elem = instantiate_template (fn, subargs, tf_none);
22525 if (elem == error_mark_node)
22526 {
22527 badfn = fn;
22528 badargs = subargs;
22529 }
22530 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22531 && constraints_satisfied_p (elem))
22532 {
22533 goodfn = elem;
22534 ++good;
22535 }
22536 }
22537 }
22538 if (good == 1)
22539 {
22540 mark_used (goodfn);
22541 expr = goodfn;
22542 if (baselink)
22543 expr = build_baselink (BASELINK_BINFO (baselink),
22544 BASELINK_ACCESS_BINFO (baselink),
22545 expr, BASELINK_OPTYPE (baselink));
22546 if (offset)
22547 {
22548 tree base
22549 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22550 expr = build_offset_ref (base, expr, addr, complain);
22551 }
22552 if (addr)
22553 expr = cp_build_addr_expr (expr, complain);
22554 return expr;
22555 }
22556 else if (good == 0 && badargs && (complain & tf_error))
22557 /* There were no good options and at least one bad one, so let the
22558 user know what the problem is. */
22559 instantiate_template (badfn, badargs, complain);
22560 }
22561 return orig_expr;
22562 }
22563
22564 /* As above, but error out if the expression remains overloaded. */
22565
22566 tree
22567 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22568 {
22569 exp = resolve_nondeduced_context (exp, complain);
22570 if (type_unknown_p (exp))
22571 {
22572 if (complain & tf_error)
22573 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22574 return error_mark_node;
22575 }
22576 return exp;
22577 }
22578
22579 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22580 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22581 different overloads deduce different arguments for a given parm.
22582 ADDR_P is true if the expression for which deduction is being
22583 performed was of the form "& fn" rather than simply "fn".
22584
22585 Returns 1 on success. */
22586
22587 static int
22588 try_one_overload (tree tparms,
22589 tree orig_targs,
22590 tree targs,
22591 tree parm,
22592 tree arg,
22593 unification_kind_t strict,
22594 int sub_strict,
22595 bool addr_p,
22596 bool explain_p)
22597 {
22598 int nargs;
22599 tree tempargs;
22600 int i;
22601
22602 if (arg == error_mark_node)
22603 return 0;
22604
22605 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22606 to function or pointer to member function argument if the set of
22607 overloaded functions does not contain function templates and at most
22608 one of a set of overloaded functions provides a unique match.
22609
22610 So if this is a template, just return success. */
22611
22612 if (uses_template_parms (arg))
22613 return 1;
22614
22615 if (TREE_CODE (arg) == METHOD_TYPE)
22616 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22617 else if (addr_p)
22618 arg = build_pointer_type (arg);
22619
22620 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22621
22622 /* We don't copy orig_targs for this because if we have already deduced
22623 some template args from previous args, unify would complain when we
22624 try to deduce a template parameter for the same argument, even though
22625 there isn't really a conflict. */
22626 nargs = TREE_VEC_LENGTH (targs);
22627 tempargs = make_tree_vec (nargs);
22628
22629 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22630 return 0;
22631
22632 /* First make sure we didn't deduce anything that conflicts with
22633 explicitly specified args. */
22634 for (i = nargs; i--; )
22635 {
22636 tree elt = TREE_VEC_ELT (tempargs, i);
22637 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22638
22639 if (!elt)
22640 /*NOP*/;
22641 else if (uses_template_parms (elt))
22642 /* Since we're unifying against ourselves, we will fill in
22643 template args used in the function parm list with our own
22644 template parms. Discard them. */
22645 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22646 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22647 {
22648 /* Check that the argument at each index of the deduced argument pack
22649 is equivalent to the corresponding explicitly specified argument.
22650 We may have deduced more arguments than were explicitly specified,
22651 and that's OK. */
22652
22653 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22654 that's wrong if we deduce the same argument pack from multiple
22655 function arguments: it's only incomplete the first time. */
22656
22657 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22658 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22659
22660 if (TREE_VEC_LENGTH (deduced_pack)
22661 < TREE_VEC_LENGTH (explicit_pack))
22662 return 0;
22663
22664 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22665 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22666 TREE_VEC_ELT (deduced_pack, j)))
22667 return 0;
22668 }
22669 else if (oldelt && !template_args_equal (oldelt, elt))
22670 return 0;
22671 }
22672
22673 for (i = nargs; i--; )
22674 {
22675 tree elt = TREE_VEC_ELT (tempargs, i);
22676
22677 if (elt)
22678 TREE_VEC_ELT (targs, i) = elt;
22679 }
22680
22681 return 1;
22682 }
22683
22684 /* PARM is a template class (perhaps with unbound template
22685 parameters). ARG is a fully instantiated type. If ARG can be
22686 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22687 TARGS are as for unify. */
22688
22689 static tree
22690 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22691 bool explain_p)
22692 {
22693 tree copy_of_targs;
22694
22695 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22696 return NULL_TREE;
22697 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22698 /* Matches anything. */;
22699 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22700 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22701 return NULL_TREE;
22702
22703 /* We need to make a new template argument vector for the call to
22704 unify. If we used TARGS, we'd clutter it up with the result of
22705 the attempted unification, even if this class didn't work out.
22706 We also don't want to commit ourselves to all the unifications
22707 we've already done, since unification is supposed to be done on
22708 an argument-by-argument basis. In other words, consider the
22709 following pathological case:
22710
22711 template <int I, int J, int K>
22712 struct S {};
22713
22714 template <int I, int J>
22715 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22716
22717 template <int I, int J, int K>
22718 void f(S<I, J, K>, S<I, I, I>);
22719
22720 void g() {
22721 S<0, 0, 0> s0;
22722 S<0, 1, 2> s2;
22723
22724 f(s0, s2);
22725 }
22726
22727 Now, by the time we consider the unification involving `s2', we
22728 already know that we must have `f<0, 0, 0>'. But, even though
22729 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22730 because there are two ways to unify base classes of S<0, 1, 2>
22731 with S<I, I, I>. If we kept the already deduced knowledge, we
22732 would reject the possibility I=1. */
22733 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22734
22735 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22736 {
22737 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22738 return NULL_TREE;
22739 return arg;
22740 }
22741
22742 /* If unification failed, we're done. */
22743 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22744 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22745 return NULL_TREE;
22746
22747 return arg;
22748 }
22749
22750 /* Given a template type PARM and a class type ARG, find the unique
22751 base type in ARG that is an instance of PARM. We do not examine
22752 ARG itself; only its base-classes. If there is not exactly one
22753 appropriate base class, return NULL_TREE. PARM may be the type of
22754 a partial specialization, as well as a plain template type. Used
22755 by unify. */
22756
22757 static enum template_base_result
22758 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22759 bool explain_p, tree *result)
22760 {
22761 tree rval = NULL_TREE;
22762 tree binfo;
22763
22764 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22765
22766 binfo = TYPE_BINFO (complete_type (arg));
22767 if (!binfo)
22768 {
22769 /* The type could not be completed. */
22770 *result = NULL_TREE;
22771 return tbr_incomplete_type;
22772 }
22773
22774 /* Walk in inheritance graph order. The search order is not
22775 important, and this avoids multiple walks of virtual bases. */
22776 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22777 {
22778 tree r = try_class_unification (tparms, targs, parm,
22779 BINFO_TYPE (binfo), explain_p);
22780
22781 if (r)
22782 {
22783 /* If there is more than one satisfactory baseclass, then:
22784
22785 [temp.deduct.call]
22786
22787 If they yield more than one possible deduced A, the type
22788 deduction fails.
22789
22790 applies. */
22791 if (rval && !same_type_p (r, rval))
22792 {
22793 /* [temp.deduct.call]/4.3: If there is a class C that is a
22794 (direct or indirect) base class of D and derived (directly or
22795 indirectly) from a class B and that would be a valid deduced
22796 A, the deduced A cannot be B or pointer to B, respectively. */
22797 if (DERIVED_FROM_P (r, rval))
22798 /* Ignore r. */
22799 continue;
22800 else if (DERIVED_FROM_P (rval, r))
22801 /* Ignore rval. */;
22802 else
22803 {
22804 *result = NULL_TREE;
22805 return tbr_ambiguous_baseclass;
22806 }
22807 }
22808
22809 rval = r;
22810 }
22811 }
22812
22813 *result = rval;
22814 return tbr_success;
22815 }
22816
22817 /* Returns the level of DECL, which declares a template parameter. */
22818
22819 static int
22820 template_decl_level (tree decl)
22821 {
22822 switch (TREE_CODE (decl))
22823 {
22824 case TYPE_DECL:
22825 case TEMPLATE_DECL:
22826 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22827
22828 case PARM_DECL:
22829 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22830
22831 default:
22832 gcc_unreachable ();
22833 }
22834 return 0;
22835 }
22836
22837 /* Decide whether ARG can be unified with PARM, considering only the
22838 cv-qualifiers of each type, given STRICT as documented for unify.
22839 Returns nonzero iff the unification is OK on that basis. */
22840
22841 static int
22842 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22843 {
22844 int arg_quals = cp_type_quals (arg);
22845 int parm_quals = cp_type_quals (parm);
22846
22847 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22848 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22849 {
22850 /* Although a CVR qualifier is ignored when being applied to a
22851 substituted template parameter ([8.3.2]/1 for example), that
22852 does not allow us to unify "const T" with "int&" because both
22853 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22854 It is ok when we're allowing additional CV qualifiers
22855 at the outer level [14.8.2.1]/3,1st bullet. */
22856 if ((TYPE_REF_P (arg)
22857 || FUNC_OR_METHOD_TYPE_P (arg))
22858 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22859 return 0;
22860
22861 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22862 && (parm_quals & TYPE_QUAL_RESTRICT))
22863 return 0;
22864 }
22865
22866 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22867 && (arg_quals & parm_quals) != parm_quals)
22868 return 0;
22869
22870 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22871 && (parm_quals & arg_quals) != arg_quals)
22872 return 0;
22873
22874 return 1;
22875 }
22876
22877 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22878 void
22879 template_parm_level_and_index (tree parm, int* level, int* index)
22880 {
22881 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22882 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22883 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22884 {
22885 *index = TEMPLATE_TYPE_IDX (parm);
22886 *level = TEMPLATE_TYPE_LEVEL (parm);
22887 }
22888 else
22889 {
22890 *index = TEMPLATE_PARM_IDX (parm);
22891 *level = TEMPLATE_PARM_LEVEL (parm);
22892 }
22893 }
22894
22895 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22896 do { \
22897 if (unify (TP, TA, P, A, S, EP)) \
22898 return 1; \
22899 } while (0)
22900
22901 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22902 expansion at the end of PACKED_PARMS. Returns 0 if the type
22903 deduction succeeds, 1 otherwise. STRICT is the same as in
22904 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22905 function call argument list. We'll need to adjust the arguments to make them
22906 types. SUBR tells us if this is from a recursive call to
22907 type_unification_real, or for comparing two template argument
22908 lists. */
22909
22910 static int
22911 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22912 tree packed_args, unification_kind_t strict,
22913 bool subr, bool explain_p)
22914 {
22915 tree parm
22916 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22917 tree pattern = PACK_EXPANSION_PATTERN (parm);
22918 tree pack, packs = NULL_TREE;
22919 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22920
22921 /* Add in any args remembered from an earlier partial instantiation. */
22922 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22923 int levels = TMPL_ARGS_DEPTH (targs);
22924
22925 packed_args = expand_template_argument_pack (packed_args);
22926
22927 int len = TREE_VEC_LENGTH (packed_args);
22928
22929 /* Determine the parameter packs we will be deducing from the
22930 pattern, and record their current deductions. */
22931 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22932 pack; pack = TREE_CHAIN (pack))
22933 {
22934 tree parm_pack = TREE_VALUE (pack);
22935 int idx, level;
22936
22937 /* Only template parameter packs can be deduced, not e.g. function
22938 parameter packs or __bases or __integer_pack. */
22939 if (!TEMPLATE_PARM_P (parm_pack))
22940 continue;
22941
22942 /* Determine the index and level of this parameter pack. */
22943 template_parm_level_and_index (parm_pack, &level, &idx);
22944 if (level < levels)
22945 continue;
22946
22947 /* Keep track of the parameter packs and their corresponding
22948 argument packs. */
22949 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22950 TREE_TYPE (packs) = make_tree_vec (len - start);
22951 }
22952
22953 /* Loop through all of the arguments that have not yet been
22954 unified and unify each with the pattern. */
22955 for (i = start; i < len; i++)
22956 {
22957 tree parm;
22958 bool any_explicit = false;
22959 tree arg = TREE_VEC_ELT (packed_args, i);
22960
22961 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22962 or the element of its argument pack at the current index if
22963 this argument was explicitly specified. */
22964 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22965 {
22966 int idx, level;
22967 tree arg, pargs;
22968 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22969
22970 arg = NULL_TREE;
22971 if (TREE_VALUE (pack)
22972 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22973 && (i - start < TREE_VEC_LENGTH (pargs)))
22974 {
22975 any_explicit = true;
22976 arg = TREE_VEC_ELT (pargs, i - start);
22977 }
22978 TMPL_ARG (targs, level, idx) = arg;
22979 }
22980
22981 /* If we had explicit template arguments, substitute them into the
22982 pattern before deduction. */
22983 if (any_explicit)
22984 {
22985 /* Some arguments might still be unspecified or dependent. */
22986 bool dependent;
22987 ++processing_template_decl;
22988 dependent = any_dependent_template_arguments_p (targs);
22989 if (!dependent)
22990 --processing_template_decl;
22991 parm = tsubst (pattern, targs,
22992 explain_p ? tf_warning_or_error : tf_none,
22993 NULL_TREE);
22994 if (dependent)
22995 --processing_template_decl;
22996 if (parm == error_mark_node)
22997 return 1;
22998 }
22999 else
23000 parm = pattern;
23001
23002 /* Unify the pattern with the current argument. */
23003 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23004 explain_p))
23005 return 1;
23006
23007 /* For each parameter pack, collect the deduced value. */
23008 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23009 {
23010 int idx, level;
23011 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23012
23013 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23014 TMPL_ARG (targs, level, idx);
23015 }
23016 }
23017
23018 /* Verify that the results of unification with the parameter packs
23019 produce results consistent with what we've seen before, and make
23020 the deduced argument packs available. */
23021 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23022 {
23023 tree old_pack = TREE_VALUE (pack);
23024 tree new_args = TREE_TYPE (pack);
23025 int i, len = TREE_VEC_LENGTH (new_args);
23026 int idx, level;
23027 bool nondeduced_p = false;
23028
23029 /* By default keep the original deduced argument pack.
23030 If necessary, more specific code is going to update the
23031 resulting deduced argument later down in this function. */
23032 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23033 TMPL_ARG (targs, level, idx) = old_pack;
23034
23035 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23036 actually deduce anything. */
23037 for (i = 0; i < len && !nondeduced_p; ++i)
23038 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23039 nondeduced_p = true;
23040 if (nondeduced_p)
23041 continue;
23042
23043 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23044 {
23045 /* If we had fewer function args than explicit template args,
23046 just use the explicits. */
23047 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23048 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23049 if (len < explicit_len)
23050 new_args = explicit_args;
23051 }
23052
23053 if (!old_pack)
23054 {
23055 tree result;
23056 /* Build the deduced *_ARGUMENT_PACK. */
23057 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23058 {
23059 result = make_node (NONTYPE_ARGUMENT_PACK);
23060 TREE_CONSTANT (result) = 1;
23061 }
23062 else
23063 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23064
23065 SET_ARGUMENT_PACK_ARGS (result, new_args);
23066
23067 /* Note the deduced argument packs for this parameter
23068 pack. */
23069 TMPL_ARG (targs, level, idx) = result;
23070 }
23071 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23072 && (ARGUMENT_PACK_ARGS (old_pack)
23073 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23074 {
23075 /* We only had the explicitly-provided arguments before, but
23076 now we have a complete set of arguments. */
23077 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23078
23079 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23080 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23081 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23082 }
23083 else
23084 {
23085 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23086 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23087 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23088 /* During template argument deduction for the aggregate deduction
23089 candidate, the number of elements in a trailing parameter pack
23090 is only deduced from the number of remaining function
23091 arguments if it is not otherwise deduced. */
23092 if (cxx_dialect >= cxx20
23093 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23094 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23095 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23096 if (!comp_template_args (old_args, new_args,
23097 &bad_old_arg, &bad_new_arg))
23098 /* Inconsistent unification of this parameter pack. */
23099 return unify_parameter_pack_inconsistent (explain_p,
23100 bad_old_arg,
23101 bad_new_arg);
23102 }
23103 }
23104
23105 return unify_success (explain_p);
23106 }
23107
23108 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23109 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23110 parameters and return value are as for unify. */
23111
23112 static int
23113 unify_array_domain (tree tparms, tree targs,
23114 tree parm_dom, tree arg_dom,
23115 bool explain_p)
23116 {
23117 tree parm_max;
23118 tree arg_max;
23119 bool parm_cst;
23120 bool arg_cst;
23121
23122 /* Our representation of array types uses "N - 1" as the
23123 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23124 not an integer constant. We cannot unify arbitrarily
23125 complex expressions, so we eliminate the MINUS_EXPRs
23126 here. */
23127 parm_max = TYPE_MAX_VALUE (parm_dom);
23128 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23129 if (!parm_cst)
23130 {
23131 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23132 parm_max = TREE_OPERAND (parm_max, 0);
23133 }
23134 arg_max = TYPE_MAX_VALUE (arg_dom);
23135 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23136 if (!arg_cst)
23137 {
23138 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23139 trying to unify the type of a variable with the type
23140 of a template parameter. For example:
23141
23142 template <unsigned int N>
23143 void f (char (&) [N]);
23144 int g();
23145 void h(int i) {
23146 char a[g(i)];
23147 f(a);
23148 }
23149
23150 Here, the type of the ARG will be "int [g(i)]", and
23151 may be a SAVE_EXPR, etc. */
23152 if (TREE_CODE (arg_max) != MINUS_EXPR)
23153 return unify_vla_arg (explain_p, arg_dom);
23154 arg_max = TREE_OPERAND (arg_max, 0);
23155 }
23156
23157 /* If only one of the bounds used a MINUS_EXPR, compensate
23158 by adding one to the other bound. */
23159 if (parm_cst && !arg_cst)
23160 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23161 integer_type_node,
23162 parm_max,
23163 integer_one_node);
23164 else if (arg_cst && !parm_cst)
23165 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23166 integer_type_node,
23167 arg_max,
23168 integer_one_node);
23169
23170 return unify (tparms, targs, parm_max, arg_max,
23171 UNIFY_ALLOW_INTEGER, explain_p);
23172 }
23173
23174 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23175
23176 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23177
23178 static pa_kind_t
23179 pa_kind (tree t)
23180 {
23181 if (PACK_EXPANSION_P (t))
23182 t = PACK_EXPANSION_PATTERN (t);
23183 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23184 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23185 || DECL_TYPE_TEMPLATE_P (t))
23186 return pa_tmpl;
23187 else if (TYPE_P (t))
23188 return pa_type;
23189 else
23190 return pa_expr;
23191 }
23192
23193 /* Deduce the value of template parameters. TPARMS is the (innermost)
23194 set of template parameters to a template. TARGS is the bindings
23195 for those template parameters, as determined thus far; TARGS may
23196 include template arguments for outer levels of template parameters
23197 as well. PARM is a parameter to a template function, or a
23198 subcomponent of that parameter; ARG is the corresponding argument.
23199 This function attempts to match PARM with ARG in a manner
23200 consistent with the existing assignments in TARGS. If more values
23201 are deduced, then TARGS is updated.
23202
23203 Returns 0 if the type deduction succeeds, 1 otherwise. The
23204 parameter STRICT is a bitwise or of the following flags:
23205
23206 UNIFY_ALLOW_NONE:
23207 Require an exact match between PARM and ARG.
23208 UNIFY_ALLOW_MORE_CV_QUAL:
23209 Allow the deduced ARG to be more cv-qualified (by qualification
23210 conversion) than ARG.
23211 UNIFY_ALLOW_LESS_CV_QUAL:
23212 Allow the deduced ARG to be less cv-qualified than ARG.
23213 UNIFY_ALLOW_DERIVED:
23214 Allow the deduced ARG to be a template base class of ARG,
23215 or a pointer to a template base class of the type pointed to by
23216 ARG.
23217 UNIFY_ALLOW_INTEGER:
23218 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23219 case for more information.
23220 UNIFY_ALLOW_OUTER_LEVEL:
23221 This is the outermost level of a deduction. Used to determine validity
23222 of qualification conversions. A valid qualification conversion must
23223 have const qualified pointers leading up to the inner type which
23224 requires additional CV quals, except at the outer level, where const
23225 is not required [conv.qual]. It would be normal to set this flag in
23226 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23227 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23228 This is the outermost level of a deduction, and PARM can be more CV
23229 qualified at this point.
23230 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23231 This is the outermost level of a deduction, and PARM can be less CV
23232 qualified at this point. */
23233
23234 static int
23235 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23236 bool explain_p)
23237 {
23238 int idx;
23239 tree targ;
23240 tree tparm;
23241 int strict_in = strict;
23242 tsubst_flags_t complain = (explain_p
23243 ? tf_warning_or_error
23244 : tf_none);
23245
23246 /* I don't think this will do the right thing with respect to types.
23247 But the only case I've seen it in so far has been array bounds, where
23248 signedness is the only information lost, and I think that will be
23249 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23250 finish_id_expression_1, and are also OK. */
23251 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23252 parm = TREE_OPERAND (parm, 0);
23253
23254 if (arg == error_mark_node)
23255 return unify_invalid (explain_p);
23256 if (arg == unknown_type_node
23257 || arg == init_list_type_node)
23258 /* We can't deduce anything from this, but we might get all the
23259 template args from other function args. */
23260 return unify_success (explain_p);
23261
23262 if (parm == any_targ_node || arg == any_targ_node)
23263 return unify_success (explain_p);
23264
23265 /* If PARM uses template parameters, then we can't bail out here,
23266 even if ARG == PARM, since we won't record unifications for the
23267 template parameters. We might need them if we're trying to
23268 figure out which of two things is more specialized. */
23269 if (arg == parm && !uses_template_parms (parm))
23270 return unify_success (explain_p);
23271
23272 /* Handle init lists early, so the rest of the function can assume
23273 we're dealing with a type. */
23274 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23275 {
23276 tree elt, elttype;
23277 unsigned i;
23278 tree orig_parm = parm;
23279
23280 if (!is_std_init_list (parm)
23281 && TREE_CODE (parm) != ARRAY_TYPE)
23282 /* We can only deduce from an initializer list argument if the
23283 parameter is std::initializer_list or an array; otherwise this
23284 is a non-deduced context. */
23285 return unify_success (explain_p);
23286
23287 if (TREE_CODE (parm) == ARRAY_TYPE)
23288 elttype = TREE_TYPE (parm);
23289 else
23290 {
23291 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23292 /* Deduction is defined in terms of a single type, so just punt
23293 on the (bizarre) std::initializer_list<T...>. */
23294 if (PACK_EXPANSION_P (elttype))
23295 return unify_success (explain_p);
23296 }
23297
23298 if (strict != DEDUCE_EXACT
23299 && TYPE_P (elttype)
23300 && !uses_deducible_template_parms (elttype))
23301 /* If ELTTYPE has no deducible template parms, skip deduction from
23302 the list elements. */;
23303 else
23304 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23305 {
23306 int elt_strict = strict;
23307
23308 if (elt == error_mark_node)
23309 return unify_invalid (explain_p);
23310
23311 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23312 {
23313 tree type = TREE_TYPE (elt);
23314 if (type == error_mark_node)
23315 return unify_invalid (explain_p);
23316 /* It should only be possible to get here for a call. */
23317 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23318 elt_strict |= maybe_adjust_types_for_deduction
23319 (DEDUCE_CALL, &elttype, &type, elt);
23320 elt = type;
23321 }
23322
23323 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23324 explain_p);
23325 }
23326
23327 if (TREE_CODE (parm) == ARRAY_TYPE
23328 && deducible_array_bound (TYPE_DOMAIN (parm)))
23329 {
23330 /* Also deduce from the length of the initializer list. */
23331 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23332 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23333 if (idx == error_mark_node)
23334 return unify_invalid (explain_p);
23335 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23336 idx, explain_p);
23337 }
23338
23339 /* If the std::initializer_list<T> deduction worked, replace the
23340 deduced A with std::initializer_list<A>. */
23341 if (orig_parm != parm)
23342 {
23343 idx = TEMPLATE_TYPE_IDX (orig_parm);
23344 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23345 targ = listify (targ);
23346 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23347 }
23348 return unify_success (explain_p);
23349 }
23350
23351 /* If parm and arg aren't the same kind of thing (template, type, or
23352 expression), fail early. */
23353 if (pa_kind (parm) != pa_kind (arg))
23354 return unify_invalid (explain_p);
23355
23356 /* Immediately reject some pairs that won't unify because of
23357 cv-qualification mismatches. */
23358 if (TREE_CODE (arg) == TREE_CODE (parm)
23359 && TYPE_P (arg)
23360 /* It is the elements of the array which hold the cv quals of an array
23361 type, and the elements might be template type parms. We'll check
23362 when we recurse. */
23363 && TREE_CODE (arg) != ARRAY_TYPE
23364 /* We check the cv-qualifiers when unifying with template type
23365 parameters below. We want to allow ARG `const T' to unify with
23366 PARM `T' for example, when computing which of two templates
23367 is more specialized, for example. */
23368 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23369 && !check_cv_quals_for_unify (strict_in, arg, parm))
23370 return unify_cv_qual_mismatch (explain_p, parm, arg);
23371
23372 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23373 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23374 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23375 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23376 strict &= ~UNIFY_ALLOW_DERIVED;
23377 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23378 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23379
23380 switch (TREE_CODE (parm))
23381 {
23382 case TYPENAME_TYPE:
23383 case SCOPE_REF:
23384 case UNBOUND_CLASS_TEMPLATE:
23385 /* In a type which contains a nested-name-specifier, template
23386 argument values cannot be deduced for template parameters used
23387 within the nested-name-specifier. */
23388 return unify_success (explain_p);
23389
23390 case TEMPLATE_TYPE_PARM:
23391 case TEMPLATE_TEMPLATE_PARM:
23392 case BOUND_TEMPLATE_TEMPLATE_PARM:
23393 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23394 if (error_operand_p (tparm))
23395 return unify_invalid (explain_p);
23396
23397 if (TEMPLATE_TYPE_LEVEL (parm)
23398 != template_decl_level (tparm))
23399 /* The PARM is not one we're trying to unify. Just check
23400 to see if it matches ARG. */
23401 {
23402 if (TREE_CODE (arg) == TREE_CODE (parm)
23403 && (is_auto (parm) ? is_auto (arg)
23404 : same_type_p (parm, arg)))
23405 return unify_success (explain_p);
23406 else
23407 return unify_type_mismatch (explain_p, parm, arg);
23408 }
23409 idx = TEMPLATE_TYPE_IDX (parm);
23410 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23411 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23412 if (error_operand_p (tparm))
23413 return unify_invalid (explain_p);
23414
23415 /* Check for mixed types and values. */
23416 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23417 && TREE_CODE (tparm) != TYPE_DECL)
23418 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23419 && TREE_CODE (tparm) != TEMPLATE_DECL))
23420 gcc_unreachable ();
23421
23422 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23423 {
23424 if ((strict_in & UNIFY_ALLOW_DERIVED)
23425 && CLASS_TYPE_P (arg))
23426 {
23427 /* First try to match ARG directly. */
23428 tree t = try_class_unification (tparms, targs, parm, arg,
23429 explain_p);
23430 if (!t)
23431 {
23432 /* Otherwise, look for a suitable base of ARG, as below. */
23433 enum template_base_result r;
23434 r = get_template_base (tparms, targs, parm, arg,
23435 explain_p, &t);
23436 if (!t)
23437 return unify_no_common_base (explain_p, r, parm, arg);
23438 arg = t;
23439 }
23440 }
23441 /* ARG must be constructed from a template class or a template
23442 template parameter. */
23443 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23444 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23445 return unify_template_deduction_failure (explain_p, parm, arg);
23446
23447 /* Deduce arguments T, i from TT<T> or TT<i>. */
23448 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23449 return 1;
23450
23451 arg = TYPE_TI_TEMPLATE (arg);
23452
23453 /* Fall through to deduce template name. */
23454 }
23455
23456 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23457 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23458 {
23459 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23460
23461 /* Simple cases: Value already set, does match or doesn't. */
23462 if (targ != NULL_TREE && template_args_equal (targ, arg))
23463 return unify_success (explain_p);
23464 else if (targ)
23465 return unify_inconsistency (explain_p, parm, targ, arg);
23466 }
23467 else
23468 {
23469 /* If PARM is `const T' and ARG is only `int', we don't have
23470 a match unless we are allowing additional qualification.
23471 If ARG is `const int' and PARM is just `T' that's OK;
23472 that binds `const int' to `T'. */
23473 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23474 arg, parm))
23475 return unify_cv_qual_mismatch (explain_p, parm, arg);
23476
23477 /* Consider the case where ARG is `const volatile int' and
23478 PARM is `const T'. Then, T should be `volatile int'. */
23479 arg = cp_build_qualified_type_real
23480 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23481 if (arg == error_mark_node)
23482 return unify_invalid (explain_p);
23483
23484 /* Simple cases: Value already set, does match or doesn't. */
23485 if (targ != NULL_TREE && same_type_p (targ, arg))
23486 return unify_success (explain_p);
23487 else if (targ)
23488 return unify_inconsistency (explain_p, parm, targ, arg);
23489
23490 /* Make sure that ARG is not a variable-sized array. (Note
23491 that were talking about variable-sized arrays (like
23492 `int[n]'), rather than arrays of unknown size (like
23493 `int[]').) We'll get very confused by such a type since
23494 the bound of the array is not constant, and therefore
23495 not mangleable. Besides, such types are not allowed in
23496 ISO C++, so we can do as we please here. We do allow
23497 them for 'auto' deduction, since that isn't ABI-exposed. */
23498 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23499 return unify_vla_arg (explain_p, arg);
23500
23501 /* Strip typedefs as in convert_template_argument. */
23502 arg = canonicalize_type_argument (arg, tf_none);
23503 }
23504
23505 /* If ARG is a parameter pack or an expansion, we cannot unify
23506 against it unless PARM is also a parameter pack. */
23507 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23508 && !template_parameter_pack_p (parm))
23509 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23510
23511 /* If the argument deduction results is a METHOD_TYPE,
23512 then there is a problem.
23513 METHOD_TYPE doesn't map to any real C++ type the result of
23514 the deduction cannot be of that type. */
23515 if (TREE_CODE (arg) == METHOD_TYPE)
23516 return unify_method_type_error (explain_p, arg);
23517
23518 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23519 return unify_success (explain_p);
23520
23521 case TEMPLATE_PARM_INDEX:
23522 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23523 if (error_operand_p (tparm))
23524 return unify_invalid (explain_p);
23525
23526 if (TEMPLATE_PARM_LEVEL (parm)
23527 != template_decl_level (tparm))
23528 {
23529 /* The PARM is not one we're trying to unify. Just check
23530 to see if it matches ARG. */
23531 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23532 && cp_tree_equal (parm, arg));
23533 if (result)
23534 unify_expression_unequal (explain_p, parm, arg);
23535 return result;
23536 }
23537
23538 idx = TEMPLATE_PARM_IDX (parm);
23539 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23540
23541 if (targ)
23542 {
23543 if ((strict & UNIFY_ALLOW_INTEGER)
23544 && TREE_TYPE (targ) && TREE_TYPE (arg)
23545 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23546 /* We're deducing from an array bound, the type doesn't matter. */
23547 arg = fold_convert (TREE_TYPE (targ), arg);
23548 int x = !cp_tree_equal (targ, arg);
23549 if (x)
23550 unify_inconsistency (explain_p, parm, targ, arg);
23551 return x;
23552 }
23553
23554 /* [temp.deduct.type] If, in the declaration of a function template
23555 with a non-type template-parameter, the non-type
23556 template-parameter is used in an expression in the function
23557 parameter-list and, if the corresponding template-argument is
23558 deduced, the template-argument type shall match the type of the
23559 template-parameter exactly, except that a template-argument
23560 deduced from an array bound may be of any integral type.
23561 The non-type parameter might use already deduced type parameters. */
23562 tparm = TREE_TYPE (parm);
23563 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23564 /* We don't have enough levels of args to do any substitution. This
23565 can happen in the context of -fnew-ttp-matching. */;
23566 else
23567 {
23568 ++processing_template_decl;
23569 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23570 --processing_template_decl;
23571
23572 if (tree a = type_uses_auto (tparm))
23573 {
23574 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23575 if (tparm == error_mark_node)
23576 return 1;
23577 }
23578 }
23579
23580 if (!TREE_TYPE (arg))
23581 /* Template-parameter dependent expression. Just accept it for now.
23582 It will later be processed in convert_template_argument. */
23583 ;
23584 else if (same_type_ignoring_top_level_qualifiers_p
23585 (non_reference (TREE_TYPE (arg)),
23586 non_reference (tparm)))
23587 /* OK. Ignore top-level quals here because a class-type template
23588 parameter object is const. */;
23589 else if ((strict & UNIFY_ALLOW_INTEGER)
23590 && CP_INTEGRAL_TYPE_P (tparm))
23591 /* Convert the ARG to the type of PARM; the deduced non-type
23592 template argument must exactly match the types of the
23593 corresponding parameter. */
23594 arg = fold (build_nop (tparm, arg));
23595 else if (uses_template_parms (tparm))
23596 {
23597 /* We haven't deduced the type of this parameter yet. */
23598 if (cxx_dialect >= cxx17
23599 /* We deduce from array bounds in try_array_deduction. */
23600 && !(strict & UNIFY_ALLOW_INTEGER)
23601 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
23602 {
23603 /* Deduce it from the non-type argument. */
23604 tree atype = TREE_TYPE (arg);
23605 RECUR_AND_CHECK_FAILURE (tparms, targs,
23606 tparm, atype,
23607 UNIFY_ALLOW_NONE, explain_p);
23608 /* Now check whether the type of this parameter is still
23609 dependent, and give up if so. */
23610 ++processing_template_decl;
23611 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23612 --processing_template_decl;
23613 if (uses_template_parms (tparm))
23614 return unify_success (explain_p);
23615 }
23616 else
23617 /* Try again later. */
23618 return unify_success (explain_p);
23619 }
23620 else
23621 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23622
23623 /* If ARG is a parameter pack or an expansion, we cannot unify
23624 against it unless PARM is also a parameter pack. */
23625 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23626 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23627 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23628
23629 {
23630 bool removed_attr = false;
23631 arg = strip_typedefs_expr (arg, &removed_attr);
23632 }
23633 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23634 return unify_success (explain_p);
23635
23636 case PTRMEM_CST:
23637 {
23638 /* A pointer-to-member constant can be unified only with
23639 another constant. */
23640 if (TREE_CODE (arg) != PTRMEM_CST)
23641 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23642
23643 /* Just unify the class member. It would be useless (and possibly
23644 wrong, depending on the strict flags) to unify also
23645 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23646 arg refer to the same variable, even if through different
23647 classes. For instance:
23648
23649 struct A { int x; };
23650 struct B : A { };
23651
23652 Unification of &A::x and &B::x must succeed. */
23653 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23654 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23655 }
23656
23657 case POINTER_TYPE:
23658 {
23659 if (!TYPE_PTR_P (arg))
23660 return unify_type_mismatch (explain_p, parm, arg);
23661
23662 /* [temp.deduct.call]
23663
23664 A can be another pointer or pointer to member type that can
23665 be converted to the deduced A via a qualification
23666 conversion (_conv.qual_).
23667
23668 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23669 This will allow for additional cv-qualification of the
23670 pointed-to types if appropriate. */
23671
23672 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23673 /* The derived-to-base conversion only persists through one
23674 level of pointers. */
23675 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23676
23677 return unify (tparms, targs, TREE_TYPE (parm),
23678 TREE_TYPE (arg), strict, explain_p);
23679 }
23680
23681 case REFERENCE_TYPE:
23682 if (!TYPE_REF_P (arg))
23683 return unify_type_mismatch (explain_p, parm, arg);
23684 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23685 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23686
23687 case ARRAY_TYPE:
23688 if (TREE_CODE (arg) != ARRAY_TYPE)
23689 return unify_type_mismatch (explain_p, parm, arg);
23690 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23691 != (TYPE_DOMAIN (arg) == NULL_TREE))
23692 return unify_type_mismatch (explain_p, parm, arg);
23693 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23694 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23695 if (TYPE_DOMAIN (parm) != NULL_TREE)
23696 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23697 TYPE_DOMAIN (arg), explain_p);
23698 return unify_success (explain_p);
23699
23700 case REAL_TYPE:
23701 case COMPLEX_TYPE:
23702 case VECTOR_TYPE:
23703 case INTEGER_TYPE:
23704 case BOOLEAN_TYPE:
23705 case ENUMERAL_TYPE:
23706 case VOID_TYPE:
23707 case OPAQUE_TYPE:
23708 case NULLPTR_TYPE:
23709 if (TREE_CODE (arg) != TREE_CODE (parm))
23710 return unify_type_mismatch (explain_p, parm, arg);
23711
23712 /* We have already checked cv-qualification at the top of the
23713 function. */
23714 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23715 return unify_type_mismatch (explain_p, parm, arg);
23716
23717 /* As far as unification is concerned, this wins. Later checks
23718 will invalidate it if necessary. */
23719 return unify_success (explain_p);
23720
23721 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23722 /* Type INTEGER_CST can come from ordinary constant template args. */
23723 case INTEGER_CST:
23724 while (CONVERT_EXPR_P (arg))
23725 arg = TREE_OPERAND (arg, 0);
23726
23727 if (TREE_CODE (arg) != INTEGER_CST)
23728 return unify_template_argument_mismatch (explain_p, parm, arg);
23729 return (tree_int_cst_equal (parm, arg)
23730 ? unify_success (explain_p)
23731 : unify_template_argument_mismatch (explain_p, parm, arg));
23732
23733 case TREE_VEC:
23734 {
23735 int i, len, argslen;
23736 int parm_variadic_p = 0;
23737
23738 if (TREE_CODE (arg) != TREE_VEC)
23739 return unify_template_argument_mismatch (explain_p, parm, arg);
23740
23741 len = TREE_VEC_LENGTH (parm);
23742 argslen = TREE_VEC_LENGTH (arg);
23743
23744 /* Check for pack expansions in the parameters. */
23745 for (i = 0; i < len; ++i)
23746 {
23747 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23748 {
23749 if (i == len - 1)
23750 /* We can unify against something with a trailing
23751 parameter pack. */
23752 parm_variadic_p = 1;
23753 else
23754 /* [temp.deduct.type]/9: If the template argument list of
23755 P contains a pack expansion that is not the last
23756 template argument, the entire template argument list
23757 is a non-deduced context. */
23758 return unify_success (explain_p);
23759 }
23760 }
23761
23762 /* If we don't have enough arguments to satisfy the parameters
23763 (not counting the pack expression at the end), or we have
23764 too many arguments for a parameter list that doesn't end in
23765 a pack expression, we can't unify. */
23766 if (parm_variadic_p
23767 ? argslen < len - parm_variadic_p
23768 : argslen != len)
23769 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23770
23771 /* Unify all of the parameters that precede the (optional)
23772 pack expression. */
23773 for (i = 0; i < len - parm_variadic_p; ++i)
23774 {
23775 RECUR_AND_CHECK_FAILURE (tparms, targs,
23776 TREE_VEC_ELT (parm, i),
23777 TREE_VEC_ELT (arg, i),
23778 UNIFY_ALLOW_NONE, explain_p);
23779 }
23780 if (parm_variadic_p)
23781 return unify_pack_expansion (tparms, targs, parm, arg,
23782 DEDUCE_EXACT,
23783 /*subr=*/true, explain_p);
23784 return unify_success (explain_p);
23785 }
23786
23787 case RECORD_TYPE:
23788 case UNION_TYPE:
23789 if (TREE_CODE (arg) != TREE_CODE (parm))
23790 return unify_type_mismatch (explain_p, parm, arg);
23791
23792 if (TYPE_PTRMEMFUNC_P (parm))
23793 {
23794 if (!TYPE_PTRMEMFUNC_P (arg))
23795 return unify_type_mismatch (explain_p, parm, arg);
23796
23797 return unify (tparms, targs,
23798 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23799 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23800 strict, explain_p);
23801 }
23802 else if (TYPE_PTRMEMFUNC_P (arg))
23803 return unify_type_mismatch (explain_p, parm, arg);
23804
23805 if (CLASSTYPE_TEMPLATE_INFO (parm))
23806 {
23807 tree t = NULL_TREE;
23808
23809 if (strict_in & UNIFY_ALLOW_DERIVED)
23810 {
23811 /* First, we try to unify the PARM and ARG directly. */
23812 t = try_class_unification (tparms, targs,
23813 parm, arg, explain_p);
23814
23815 if (!t)
23816 {
23817 /* Fallback to the special case allowed in
23818 [temp.deduct.call]:
23819
23820 If P is a class, and P has the form
23821 template-id, then A can be a derived class of
23822 the deduced A. Likewise, if P is a pointer to
23823 a class of the form template-id, A can be a
23824 pointer to a derived class pointed to by the
23825 deduced A. */
23826 enum template_base_result r;
23827 r = get_template_base (tparms, targs, parm, arg,
23828 explain_p, &t);
23829
23830 if (!t)
23831 {
23832 /* Don't give the derived diagnostic if we're
23833 already dealing with the same template. */
23834 bool same_template
23835 = (CLASSTYPE_TEMPLATE_INFO (arg)
23836 && (CLASSTYPE_TI_TEMPLATE (parm)
23837 == CLASSTYPE_TI_TEMPLATE (arg)));
23838 return unify_no_common_base (explain_p && !same_template,
23839 r, parm, arg);
23840 }
23841 }
23842 }
23843 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23844 && (CLASSTYPE_TI_TEMPLATE (parm)
23845 == CLASSTYPE_TI_TEMPLATE (arg)))
23846 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23847 Then, we should unify `int' and `U'. */
23848 t = arg;
23849 else
23850 /* There's no chance of unification succeeding. */
23851 return unify_type_mismatch (explain_p, parm, arg);
23852
23853 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23854 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23855 }
23856 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23857 return unify_type_mismatch (explain_p, parm, arg);
23858 return unify_success (explain_p);
23859
23860 case METHOD_TYPE:
23861 case FUNCTION_TYPE:
23862 {
23863 unsigned int nargs;
23864 tree *args;
23865 tree a;
23866 unsigned int i;
23867
23868 if (TREE_CODE (arg) != TREE_CODE (parm))
23869 return unify_type_mismatch (explain_p, parm, arg);
23870
23871 /* CV qualifications for methods can never be deduced, they must
23872 match exactly. We need to check them explicitly here,
23873 because type_unification_real treats them as any other
23874 cv-qualified parameter. */
23875 if (TREE_CODE (parm) == METHOD_TYPE
23876 && (!check_cv_quals_for_unify
23877 (UNIFY_ALLOW_NONE,
23878 class_of_this_parm (arg),
23879 class_of_this_parm (parm))))
23880 return unify_cv_qual_mismatch (explain_p, parm, arg);
23881 if (TREE_CODE (arg) == FUNCTION_TYPE
23882 && type_memfn_quals (parm) != type_memfn_quals (arg))
23883 return unify_cv_qual_mismatch (explain_p, parm, arg);
23884 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23885 return unify_type_mismatch (explain_p, parm, arg);
23886
23887 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23888 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23889
23890 nargs = list_length (TYPE_ARG_TYPES (arg));
23891 args = XALLOCAVEC (tree, nargs);
23892 for (a = TYPE_ARG_TYPES (arg), i = 0;
23893 a != NULL_TREE && a != void_list_node;
23894 a = TREE_CHAIN (a), ++i)
23895 args[i] = TREE_VALUE (a);
23896 nargs = i;
23897
23898 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23899 args, nargs, 1, DEDUCE_EXACT,
23900 NULL, explain_p))
23901 return 1;
23902
23903 if (flag_noexcept_type)
23904 {
23905 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23906 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23907 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23908 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23909 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23910 && uses_template_parms (TREE_PURPOSE (pspec)))
23911 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23912 TREE_PURPOSE (aspec),
23913 UNIFY_ALLOW_NONE, explain_p);
23914 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23915 return unify_type_mismatch (explain_p, parm, arg);
23916 }
23917
23918 return 0;
23919 }
23920
23921 case OFFSET_TYPE:
23922 /* Unify a pointer to member with a pointer to member function, which
23923 deduces the type of the member as a function type. */
23924 if (TYPE_PTRMEMFUNC_P (arg))
23925 {
23926 /* Check top-level cv qualifiers */
23927 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23928 return unify_cv_qual_mismatch (explain_p, parm, arg);
23929
23930 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23931 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23932 UNIFY_ALLOW_NONE, explain_p);
23933
23934 /* Determine the type of the function we are unifying against. */
23935 tree fntype = static_fn_type (arg);
23936
23937 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23938 }
23939
23940 if (TREE_CODE (arg) != OFFSET_TYPE)
23941 return unify_type_mismatch (explain_p, parm, arg);
23942 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23943 TYPE_OFFSET_BASETYPE (arg),
23944 UNIFY_ALLOW_NONE, explain_p);
23945 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23946 strict, explain_p);
23947
23948 case CONST_DECL:
23949 if (DECL_TEMPLATE_PARM_P (parm))
23950 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23951 if (arg != scalar_constant_value (parm))
23952 return unify_template_argument_mismatch (explain_p, parm, arg);
23953 return unify_success (explain_p);
23954
23955 case FIELD_DECL:
23956 case TEMPLATE_DECL:
23957 /* Matched cases are handled by the ARG == PARM test above. */
23958 return unify_template_argument_mismatch (explain_p, parm, arg);
23959
23960 case VAR_DECL:
23961 /* We might get a variable as a non-type template argument in parm if the
23962 corresponding parameter is type-dependent. Make any necessary
23963 adjustments based on whether arg is a reference. */
23964 if (CONSTANT_CLASS_P (arg))
23965 parm = fold_non_dependent_expr (parm, complain);
23966 else if (REFERENCE_REF_P (arg))
23967 {
23968 tree sub = TREE_OPERAND (arg, 0);
23969 STRIP_NOPS (sub);
23970 if (TREE_CODE (sub) == ADDR_EXPR)
23971 arg = TREE_OPERAND (sub, 0);
23972 }
23973 /* Now use the normal expression code to check whether they match. */
23974 goto expr;
23975
23976 case TYPE_ARGUMENT_PACK:
23977 case NONTYPE_ARGUMENT_PACK:
23978 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23979 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23980
23981 case TYPEOF_TYPE:
23982 case DECLTYPE_TYPE:
23983 case UNDERLYING_TYPE:
23984 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23985 or UNDERLYING_TYPE nodes. */
23986 return unify_success (explain_p);
23987
23988 case ERROR_MARK:
23989 /* Unification fails if we hit an error node. */
23990 return unify_invalid (explain_p);
23991
23992 case INDIRECT_REF:
23993 if (REFERENCE_REF_P (parm))
23994 {
23995 bool pexp = PACK_EXPANSION_P (arg);
23996 if (pexp)
23997 arg = PACK_EXPANSION_PATTERN (arg);
23998 if (REFERENCE_REF_P (arg))
23999 arg = TREE_OPERAND (arg, 0);
24000 if (pexp)
24001 arg = make_pack_expansion (arg, complain);
24002 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24003 strict, explain_p);
24004 }
24005 /* FALLTHRU */
24006
24007 default:
24008 /* An unresolved overload is a nondeduced context. */
24009 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24010 return unify_success (explain_p);
24011 gcc_assert (EXPR_P (parm)
24012 || COMPOUND_LITERAL_P (parm)
24013 || TREE_CODE (parm) == TRAIT_EXPR);
24014 expr:
24015 /* We must be looking at an expression. This can happen with
24016 something like:
24017
24018 template <int I>
24019 void foo(S<I>, S<I + 2>);
24020
24021 or
24022
24023 template<typename T>
24024 void foo(A<T, T{}>);
24025
24026 This is a "non-deduced context":
24027
24028 [deduct.type]
24029
24030 The non-deduced contexts are:
24031
24032 --A non-type template argument or an array bound in which
24033 a subexpression references a template parameter.
24034
24035 In these cases, we assume deduction succeeded, but don't
24036 actually infer any unifications. */
24037
24038 if (!uses_template_parms (parm)
24039 && !template_args_equal (parm, arg))
24040 return unify_expression_unequal (explain_p, parm, arg);
24041 else
24042 return unify_success (explain_p);
24043 }
24044 }
24045 #undef RECUR_AND_CHECK_FAILURE
24046 \f
24047 /* Note that DECL can be defined in this translation unit, if
24048 required. */
24049
24050 static void
24051 mark_definable (tree decl)
24052 {
24053 tree clone;
24054 DECL_NOT_REALLY_EXTERN (decl) = 1;
24055 FOR_EACH_CLONE (clone, decl)
24056 DECL_NOT_REALLY_EXTERN (clone) = 1;
24057 }
24058
24059 /* Called if RESULT is explicitly instantiated, or is a member of an
24060 explicitly instantiated class. */
24061
24062 void
24063 mark_decl_instantiated (tree result, int extern_p)
24064 {
24065 SET_DECL_EXPLICIT_INSTANTIATION (result);
24066
24067 /* If this entity has already been written out, it's too late to
24068 make any modifications. */
24069 if (TREE_ASM_WRITTEN (result))
24070 return;
24071
24072 /* For anonymous namespace we don't need to do anything. */
24073 if (decl_anon_ns_mem_p (result))
24074 {
24075 gcc_assert (!TREE_PUBLIC (result));
24076 return;
24077 }
24078
24079 if (TREE_CODE (result) != FUNCTION_DECL)
24080 /* The TREE_PUBLIC flag for function declarations will have been
24081 set correctly by tsubst. */
24082 TREE_PUBLIC (result) = 1;
24083
24084 /* This might have been set by an earlier implicit instantiation. */
24085 DECL_COMDAT (result) = 0;
24086
24087 if (extern_p)
24088 DECL_NOT_REALLY_EXTERN (result) = 0;
24089 else
24090 {
24091 mark_definable (result);
24092 mark_needed (result);
24093 /* Always make artificials weak. */
24094 if (DECL_ARTIFICIAL (result) && flag_weak)
24095 comdat_linkage (result);
24096 /* For WIN32 we also want to put explicit instantiations in
24097 linkonce sections. */
24098 else if (TREE_PUBLIC (result))
24099 maybe_make_one_only (result);
24100 if (TREE_CODE (result) == FUNCTION_DECL
24101 && DECL_TEMPLATE_INSTANTIATED (result))
24102 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24103 since start_preparsed_function wouldn't have if we had an earlier
24104 extern explicit instantiation. */
24105 DECL_EXTERNAL (result) = 0;
24106 }
24107
24108 /* If EXTERN_P, then this function will not be emitted -- unless
24109 followed by an explicit instantiation, at which point its linkage
24110 will be adjusted. If !EXTERN_P, then this function will be
24111 emitted here. In neither circumstance do we want
24112 import_export_decl to adjust the linkage. */
24113 DECL_INTERFACE_KNOWN (result) = 1;
24114 }
24115
24116 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24117 important template arguments. If any are missing, we check whether
24118 they're important by using error_mark_node for substituting into any
24119 args that were used for partial ordering (the ones between ARGS and END)
24120 and seeing if it bubbles up. */
24121
24122 static bool
24123 check_undeduced_parms (tree targs, tree args, tree end)
24124 {
24125 bool found = false;
24126 int i;
24127 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24128 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24129 {
24130 found = true;
24131 TREE_VEC_ELT (targs, i) = error_mark_node;
24132 }
24133 if (found)
24134 {
24135 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24136 if (substed == error_mark_node)
24137 return true;
24138 }
24139 return false;
24140 }
24141
24142 /* Given two function templates PAT1 and PAT2, return:
24143
24144 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24145 -1 if PAT2 is more specialized than PAT1.
24146 0 if neither is more specialized.
24147
24148 LEN indicates the number of parameters we should consider
24149 (defaulted parameters should not be considered).
24150
24151 The 1998 std underspecified function template partial ordering, and
24152 DR214 addresses the issue. We take pairs of arguments, one from
24153 each of the templates, and deduce them against each other. One of
24154 the templates will be more specialized if all the *other*
24155 template's arguments deduce against its arguments and at least one
24156 of its arguments *does* *not* deduce against the other template's
24157 corresponding argument. Deduction is done as for class templates.
24158 The arguments used in deduction have reference and top level cv
24159 qualifiers removed. Iff both arguments were originally reference
24160 types *and* deduction succeeds in both directions, an lvalue reference
24161 wins against an rvalue reference and otherwise the template
24162 with the more cv-qualified argument wins for that pairing (if
24163 neither is more cv-qualified, they both are equal). Unlike regular
24164 deduction, after all the arguments have been deduced in this way,
24165 we do *not* verify the deduced template argument values can be
24166 substituted into non-deduced contexts.
24167
24168 The logic can be a bit confusing here, because we look at deduce1 and
24169 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24170 can find template arguments for pat1 to make arg1 look like arg2, that
24171 means that arg2 is at least as specialized as arg1. */
24172
24173 int
24174 more_specialized_fn (tree pat1, tree pat2, int len)
24175 {
24176 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24177 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24178 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24179 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24180 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24181 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24182 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24183 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24184 tree origs1, origs2;
24185 bool lose1 = false;
24186 bool lose2 = false;
24187
24188 /* Remove the this parameter from non-static member functions. If
24189 one is a non-static member function and the other is not a static
24190 member function, remove the first parameter from that function
24191 also. This situation occurs for operator functions where we
24192 locate both a member function (with this pointer) and non-member
24193 operator (with explicit first operand). */
24194 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24195 {
24196 len--; /* LEN is the number of significant arguments for DECL1 */
24197 args1 = TREE_CHAIN (args1);
24198 if (!DECL_STATIC_FUNCTION_P (decl2))
24199 args2 = TREE_CHAIN (args2);
24200 }
24201 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24202 {
24203 args2 = TREE_CHAIN (args2);
24204 if (!DECL_STATIC_FUNCTION_P (decl1))
24205 {
24206 len--;
24207 args1 = TREE_CHAIN (args1);
24208 }
24209 }
24210
24211 /* If only one is a conversion operator, they are unordered. */
24212 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24213 return 0;
24214
24215 /* Consider the return type for a conversion function */
24216 if (DECL_CONV_FN_P (decl1))
24217 {
24218 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24219 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24220 len++;
24221 }
24222
24223 processing_template_decl++;
24224
24225 origs1 = args1;
24226 origs2 = args2;
24227
24228 while (len--
24229 /* Stop when an ellipsis is seen. */
24230 && args1 != NULL_TREE && args2 != NULL_TREE)
24231 {
24232 tree arg1 = TREE_VALUE (args1);
24233 tree arg2 = TREE_VALUE (args2);
24234 int deduce1, deduce2;
24235 int quals1 = -1;
24236 int quals2 = -1;
24237 int ref1 = 0;
24238 int ref2 = 0;
24239
24240 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24241 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24242 {
24243 /* When both arguments are pack expansions, we need only
24244 unify the patterns themselves. */
24245 arg1 = PACK_EXPANSION_PATTERN (arg1);
24246 arg2 = PACK_EXPANSION_PATTERN (arg2);
24247
24248 /* This is the last comparison we need to do. */
24249 len = 0;
24250 }
24251
24252 if (TYPE_REF_P (arg1))
24253 {
24254 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24255 arg1 = TREE_TYPE (arg1);
24256 quals1 = cp_type_quals (arg1);
24257 }
24258
24259 if (TYPE_REF_P (arg2))
24260 {
24261 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24262 arg2 = TREE_TYPE (arg2);
24263 quals2 = cp_type_quals (arg2);
24264 }
24265
24266 arg1 = TYPE_MAIN_VARIANT (arg1);
24267 arg2 = TYPE_MAIN_VARIANT (arg2);
24268
24269 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24270 {
24271 int i, len2 = remaining_arguments (args2);
24272 tree parmvec = make_tree_vec (1);
24273 tree argvec = make_tree_vec (len2);
24274 tree ta = args2;
24275
24276 /* Setup the parameter vector, which contains only ARG1. */
24277 TREE_VEC_ELT (parmvec, 0) = arg1;
24278
24279 /* Setup the argument vector, which contains the remaining
24280 arguments. */
24281 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24282 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24283
24284 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24285 argvec, DEDUCE_EXACT,
24286 /*subr=*/true, /*explain_p=*/false)
24287 == 0);
24288
24289 /* We cannot deduce in the other direction, because ARG1 is
24290 a pack expansion but ARG2 is not. */
24291 deduce2 = 0;
24292 }
24293 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24294 {
24295 int i, len1 = remaining_arguments (args1);
24296 tree parmvec = make_tree_vec (1);
24297 tree argvec = make_tree_vec (len1);
24298 tree ta = args1;
24299
24300 /* Setup the parameter vector, which contains only ARG1. */
24301 TREE_VEC_ELT (parmvec, 0) = arg2;
24302
24303 /* Setup the argument vector, which contains the remaining
24304 arguments. */
24305 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24306 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24307
24308 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24309 argvec, DEDUCE_EXACT,
24310 /*subr=*/true, /*explain_p=*/false)
24311 == 0);
24312
24313 /* We cannot deduce in the other direction, because ARG2 is
24314 a pack expansion but ARG1 is not.*/
24315 deduce1 = 0;
24316 }
24317
24318 else
24319 {
24320 /* The normal case, where neither argument is a pack
24321 expansion. */
24322 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24323 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24324 == 0);
24325 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24326 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24327 == 0);
24328 }
24329
24330 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24331 arg2, then arg2 is not as specialized as arg1. */
24332 if (!deduce1)
24333 lose2 = true;
24334 if (!deduce2)
24335 lose1 = true;
24336
24337 /* "If, for a given type, deduction succeeds in both directions
24338 (i.e., the types are identical after the transformations above)
24339 and both P and A were reference types (before being replaced with
24340 the type referred to above):
24341 - if the type from the argument template was an lvalue reference and
24342 the type from the parameter template was not, the argument type is
24343 considered to be more specialized than the other; otherwise,
24344 - if the type from the argument template is more cv-qualified
24345 than the type from the parameter template (as described above),
24346 the argument type is considered to be more specialized than the other;
24347 otherwise,
24348 - neither type is more specialized than the other." */
24349
24350 if (deduce1 && deduce2)
24351 {
24352 if (ref1 && ref2 && ref1 != ref2)
24353 {
24354 if (ref1 > ref2)
24355 lose1 = true;
24356 else
24357 lose2 = true;
24358 }
24359 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24360 {
24361 if ((quals1 & quals2) == quals2)
24362 lose2 = true;
24363 if ((quals1 & quals2) == quals1)
24364 lose1 = true;
24365 }
24366 }
24367
24368 if (lose1 && lose2)
24369 /* We've failed to deduce something in either direction.
24370 These must be unordered. */
24371 break;
24372
24373 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24374 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24375 /* We have already processed all of the arguments in our
24376 handing of the pack expansion type. */
24377 len = 0;
24378
24379 args1 = TREE_CHAIN (args1);
24380 args2 = TREE_CHAIN (args2);
24381 }
24382
24383 /* "In most cases, all template parameters must have values in order for
24384 deduction to succeed, but for partial ordering purposes a template
24385 parameter may remain without a value provided it is not used in the
24386 types being used for partial ordering."
24387
24388 Thus, if we are missing any of the targs1 we need to substitute into
24389 origs1, then pat2 is not as specialized as pat1. This can happen when
24390 there is a nondeduced context. */
24391 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24392 lose2 = true;
24393 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24394 lose1 = true;
24395
24396 processing_template_decl--;
24397
24398 /* If both deductions succeed, the partial ordering selects the more
24399 constrained template. */
24400 /* P2113: If the corresponding template-parameters of the
24401 template-parameter-lists are not equivalent ([temp.over.link]) or if
24402 the function parameters that positionally correspond between the two
24403 templates are not of the same type, neither template is more
24404 specialized than the other. */
24405 if (!lose1 && !lose2
24406 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24407 DECL_TEMPLATE_PARMS (pat2))
24408 && compparms (origs1, origs2))
24409 {
24410 int winner = more_constrained (decl1, decl2);
24411 if (winner > 0)
24412 lose2 = true;
24413 else if (winner < 0)
24414 lose1 = true;
24415 }
24416
24417 /* All things being equal, if the next argument is a pack expansion
24418 for one function but not for the other, prefer the
24419 non-variadic function. FIXME this is bogus; see c++/41958. */
24420 if (lose1 == lose2
24421 && args1 && TREE_VALUE (args1)
24422 && args2 && TREE_VALUE (args2))
24423 {
24424 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24425 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24426 }
24427
24428 if (lose1 == lose2)
24429 return 0;
24430 else if (!lose1)
24431 return 1;
24432 else
24433 return -1;
24434 }
24435
24436 /* Determine which of two partial specializations of TMPL is more
24437 specialized.
24438
24439 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24440 to the first partial specialization. The TREE_PURPOSE is the
24441 innermost set of template parameters for the partial
24442 specialization. PAT2 is similar, but for the second template.
24443
24444 Return 1 if the first partial specialization is more specialized;
24445 -1 if the second is more specialized; 0 if neither is more
24446 specialized.
24447
24448 See [temp.class.order] for information about determining which of
24449 two templates is more specialized. */
24450
24451 static int
24452 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24453 {
24454 tree targs;
24455 int winner = 0;
24456 bool any_deductions = false;
24457
24458 tree tmpl1 = TREE_VALUE (pat1);
24459 tree tmpl2 = TREE_VALUE (pat2);
24460 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24461 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24462
24463 /* Just like what happens for functions, if we are ordering between
24464 different template specializations, we may encounter dependent
24465 types in the arguments, and we need our dependency check functions
24466 to behave correctly. */
24467 ++processing_template_decl;
24468 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24469 if (targs)
24470 {
24471 --winner;
24472 any_deductions = true;
24473 }
24474
24475 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24476 if (targs)
24477 {
24478 ++winner;
24479 any_deductions = true;
24480 }
24481 --processing_template_decl;
24482
24483 /* If both deductions succeed, the partial ordering selects the more
24484 constrained template. */
24485 if (!winner && any_deductions)
24486 winner = more_constrained (tmpl1, tmpl2);
24487
24488 /* In the case of a tie where at least one of the templates
24489 has a parameter pack at the end, the template with the most
24490 non-packed parameters wins. */
24491 if (winner == 0
24492 && any_deductions
24493 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24494 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24495 {
24496 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24497 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24498 int len1 = TREE_VEC_LENGTH (args1);
24499 int len2 = TREE_VEC_LENGTH (args2);
24500
24501 /* We don't count the pack expansion at the end. */
24502 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24503 --len1;
24504 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24505 --len2;
24506
24507 if (len1 > len2)
24508 return 1;
24509 else if (len1 < len2)
24510 return -1;
24511 }
24512
24513 return winner;
24514 }
24515
24516 /* Return the template arguments that will produce the function signature
24517 DECL from the function template FN, with the explicit template
24518 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24519 also match. Return NULL_TREE if no satisfactory arguments could be
24520 found. */
24521
24522 static tree
24523 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24524 {
24525 int ntparms = DECL_NTPARMS (fn);
24526 tree targs = make_tree_vec (ntparms);
24527 tree decl_type = TREE_TYPE (decl);
24528 tree decl_arg_types;
24529 tree *args;
24530 unsigned int nargs, ix;
24531 tree arg;
24532
24533 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24534
24535 /* Never do unification on the 'this' parameter. */
24536 decl_arg_types = skip_artificial_parms_for (decl,
24537 TYPE_ARG_TYPES (decl_type));
24538
24539 nargs = list_length (decl_arg_types);
24540 args = XALLOCAVEC (tree, nargs);
24541 for (arg = decl_arg_types, ix = 0;
24542 arg != NULL_TREE && arg != void_list_node;
24543 arg = TREE_CHAIN (arg), ++ix)
24544 args[ix] = TREE_VALUE (arg);
24545
24546 if (fn_type_unification (fn, explicit_args, targs,
24547 args, ix,
24548 (check_rettype || DECL_CONV_FN_P (fn)
24549 ? TREE_TYPE (decl_type) : NULL_TREE),
24550 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24551 /*explain_p=*/false,
24552 /*decltype*/false)
24553 == error_mark_node)
24554 return NULL_TREE;
24555
24556 return targs;
24557 }
24558
24559 /* Return the innermost template arguments that, when applied to a partial
24560 specialization SPEC_TMPL of TMPL, yield the ARGS.
24561
24562 For example, suppose we have:
24563
24564 template <class T, class U> struct S {};
24565 template <class T> struct S<T*, int> {};
24566
24567 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24568 partial specialization and the ARGS will be {double*, int}. The resulting
24569 vector will be {double}, indicating that `T' is bound to `double'. */
24570
24571 static tree
24572 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24573 {
24574 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24575 tree spec_args
24576 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24577 int i, ntparms = TREE_VEC_LENGTH (tparms);
24578 tree deduced_args;
24579 tree innermost_deduced_args;
24580
24581 innermost_deduced_args = make_tree_vec (ntparms);
24582 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24583 {
24584 deduced_args = copy_node (args);
24585 SET_TMPL_ARGS_LEVEL (deduced_args,
24586 TMPL_ARGS_DEPTH (deduced_args),
24587 innermost_deduced_args);
24588 }
24589 else
24590 deduced_args = innermost_deduced_args;
24591
24592 bool tried_array_deduction = (cxx_dialect < cxx17);
24593 again:
24594 if (unify (tparms, deduced_args,
24595 INNERMOST_TEMPLATE_ARGS (spec_args),
24596 INNERMOST_TEMPLATE_ARGS (args),
24597 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24598 return NULL_TREE;
24599
24600 for (i = 0; i < ntparms; ++i)
24601 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24602 {
24603 if (!tried_array_deduction)
24604 {
24605 try_array_deduction (tparms, innermost_deduced_args,
24606 INNERMOST_TEMPLATE_ARGS (spec_args));
24607 tried_array_deduction = true;
24608 if (TREE_VEC_ELT (innermost_deduced_args, i))
24609 goto again;
24610 }
24611 return NULL_TREE;
24612 }
24613
24614 if (!push_tinst_level (spec_tmpl, deduced_args))
24615 {
24616 excessive_deduction_depth = true;
24617 return NULL_TREE;
24618 }
24619
24620 /* Verify that nondeduced template arguments agree with the type
24621 obtained from argument deduction.
24622
24623 For example:
24624
24625 struct A { typedef int X; };
24626 template <class T, class U> struct C {};
24627 template <class T> struct C<T, typename T::X> {};
24628
24629 Then with the instantiation `C<A, int>', we can deduce that
24630 `T' is `A' but unify () does not check whether `typename T::X'
24631 is `int'. */
24632 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24633
24634 if (spec_args != error_mark_node)
24635 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24636 INNERMOST_TEMPLATE_ARGS (spec_args),
24637 tmpl, tf_none, false, false);
24638
24639 pop_tinst_level ();
24640
24641 if (spec_args == error_mark_node
24642 /* We only need to check the innermost arguments; the other
24643 arguments will always agree. */
24644 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24645 INNERMOST_TEMPLATE_ARGS (args)))
24646 return NULL_TREE;
24647
24648 /* Now that we have bindings for all of the template arguments,
24649 ensure that the arguments deduced for the template template
24650 parameters have compatible template parameter lists. See the use
24651 of template_template_parm_bindings_ok_p in fn_type_unification
24652 for more information. */
24653 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24654 return NULL_TREE;
24655
24656 return deduced_args;
24657 }
24658
24659 // Compare two function templates T1 and T2 by deducing bindings
24660 // from one against the other. If both deductions succeed, compare
24661 // constraints to see which is more constrained.
24662 static int
24663 more_specialized_inst (tree t1, tree t2)
24664 {
24665 int fate = 0;
24666 int count = 0;
24667
24668 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24669 {
24670 --fate;
24671 ++count;
24672 }
24673
24674 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24675 {
24676 ++fate;
24677 ++count;
24678 }
24679
24680 // If both deductions succeed, then one may be more constrained.
24681 if (count == 2 && fate == 0)
24682 fate = more_constrained (t1, t2);
24683
24684 return fate;
24685 }
24686
24687 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24688 Return the TREE_LIST node with the most specialized template, if
24689 any. If there is no most specialized template, the error_mark_node
24690 is returned.
24691
24692 Note that this function does not look at, or modify, the
24693 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24694 returned is one of the elements of INSTANTIATIONS, callers may
24695 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24696 and retrieve it from the value returned. */
24697
24698 tree
24699 most_specialized_instantiation (tree templates)
24700 {
24701 tree fn, champ;
24702
24703 ++processing_template_decl;
24704
24705 champ = templates;
24706 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24707 {
24708 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24709 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24710 if (fate == -1)
24711 champ = fn;
24712 else if (!fate)
24713 {
24714 /* Equally specialized, move to next function. If there
24715 is no next function, nothing's most specialized. */
24716 fn = TREE_CHAIN (fn);
24717 champ = fn;
24718 if (!fn)
24719 break;
24720 }
24721 }
24722
24723 if (champ)
24724 /* Now verify that champ is better than everything earlier in the
24725 instantiation list. */
24726 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24727 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24728 {
24729 champ = NULL_TREE;
24730 break;
24731 }
24732 }
24733
24734 processing_template_decl--;
24735
24736 if (!champ)
24737 return error_mark_node;
24738
24739 return champ;
24740 }
24741
24742 /* If DECL is a specialization of some template, return the most
24743 general such template. Otherwise, returns NULL_TREE.
24744
24745 For example, given:
24746
24747 template <class T> struct S { template <class U> void f(U); };
24748
24749 if TMPL is `template <class U> void S<int>::f(U)' this will return
24750 the full template. This function will not trace past partial
24751 specializations, however. For example, given in addition:
24752
24753 template <class T> struct S<T*> { template <class U> void f(U); };
24754
24755 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24756 `template <class T> template <class U> S<T*>::f(U)'. */
24757
24758 tree
24759 most_general_template (tree decl)
24760 {
24761 if (TREE_CODE (decl) != TEMPLATE_DECL)
24762 {
24763 if (tree tinfo = get_template_info (decl))
24764 decl = TI_TEMPLATE (tinfo);
24765 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24766 template friend, or a FIELD_DECL for a capture pack. */
24767 if (TREE_CODE (decl) != TEMPLATE_DECL)
24768 return NULL_TREE;
24769 }
24770
24771 /* Look for more and more general templates. */
24772 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24773 {
24774 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24775 (See cp-tree.h for details.) */
24776 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24777 break;
24778
24779 if (CLASS_TYPE_P (TREE_TYPE (decl))
24780 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24781 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24782 break;
24783
24784 /* Stop if we run into an explicitly specialized class template. */
24785 if (!DECL_NAMESPACE_SCOPE_P (decl)
24786 && DECL_CONTEXT (decl)
24787 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24788 break;
24789
24790 decl = DECL_TI_TEMPLATE (decl);
24791 }
24792
24793 return decl;
24794 }
24795
24796 /* Return the most specialized of the template partial specializations
24797 which can produce TARGET, a specialization of some class or variable
24798 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24799 a TEMPLATE_DECL node corresponding to the partial specialization, while
24800 the TREE_PURPOSE is the set of template arguments that must be
24801 substituted into the template pattern in order to generate TARGET.
24802
24803 If the choice of partial specialization is ambiguous, a diagnostic
24804 is issued, and the error_mark_node is returned. If there are no
24805 partial specializations matching TARGET, then NULL_TREE is
24806 returned, indicating that the primary template should be used. */
24807
24808 tree
24809 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24810 {
24811 tree list = NULL_TREE;
24812 tree t;
24813 tree champ;
24814 int fate;
24815 bool ambiguous_p;
24816 tree outer_args = NULL_TREE;
24817 tree tmpl, args;
24818
24819 if (TYPE_P (target))
24820 {
24821 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24822 tmpl = TI_TEMPLATE (tinfo);
24823 args = TI_ARGS (tinfo);
24824 }
24825 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24826 {
24827 tmpl = TREE_OPERAND (target, 0);
24828 args = TREE_OPERAND (target, 1);
24829 }
24830 else if (VAR_P (target))
24831 {
24832 tree tinfo = DECL_TEMPLATE_INFO (target);
24833 tmpl = TI_TEMPLATE (tinfo);
24834 args = TI_ARGS (tinfo);
24835 }
24836 else
24837 gcc_unreachable ();
24838
24839 tree main_tmpl = most_general_template (tmpl);
24840
24841 /* For determining which partial specialization to use, only the
24842 innermost args are interesting. */
24843 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24844 {
24845 outer_args = strip_innermost_template_args (args, 1);
24846 args = INNERMOST_TEMPLATE_ARGS (args);
24847 }
24848
24849 /* The caller hasn't called push_to_top_level yet, but we need
24850 get_partial_spec_bindings to be done in non-template context so that we'll
24851 fully resolve everything. */
24852 processing_template_decl_sentinel ptds;
24853
24854 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24855 {
24856 const tree ospec_tmpl = TREE_VALUE (t);
24857
24858 tree spec_tmpl;
24859 if (outer_args)
24860 {
24861 /* Substitute in the template args from the enclosing class. */
24862 ++processing_template_decl;
24863 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24864 --processing_template_decl;
24865 if (spec_tmpl == error_mark_node)
24866 return error_mark_node;
24867 }
24868 else
24869 spec_tmpl = ospec_tmpl;
24870
24871 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24872 if (spec_args)
24873 {
24874 if (outer_args)
24875 spec_args = add_to_template_args (outer_args, spec_args);
24876
24877 /* Keep the candidate only if the constraints are satisfied,
24878 or if we're not compiling with concepts. */
24879 if (!flag_concepts
24880 || constraints_satisfied_p (ospec_tmpl, spec_args))
24881 {
24882 list = tree_cons (spec_args, ospec_tmpl, list);
24883 TREE_TYPE (list) = TREE_TYPE (t);
24884 }
24885 }
24886 }
24887
24888 if (! list)
24889 return NULL_TREE;
24890
24891 ambiguous_p = false;
24892 t = list;
24893 champ = t;
24894 t = TREE_CHAIN (t);
24895 for (; t; t = TREE_CHAIN (t))
24896 {
24897 fate = more_specialized_partial_spec (tmpl, champ, t);
24898 if (fate == 1)
24899 ;
24900 else
24901 {
24902 if (fate == 0)
24903 {
24904 t = TREE_CHAIN (t);
24905 if (! t)
24906 {
24907 ambiguous_p = true;
24908 break;
24909 }
24910 }
24911 champ = t;
24912 }
24913 }
24914
24915 if (!ambiguous_p)
24916 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24917 {
24918 fate = more_specialized_partial_spec (tmpl, champ, t);
24919 if (fate != 1)
24920 {
24921 ambiguous_p = true;
24922 break;
24923 }
24924 }
24925
24926 if (ambiguous_p)
24927 {
24928 const char *str;
24929 char *spaces = NULL;
24930 if (!(complain & tf_error))
24931 return error_mark_node;
24932 if (TYPE_P (target))
24933 error ("ambiguous template instantiation for %q#T", target);
24934 else
24935 error ("ambiguous template instantiation for %q#D", target);
24936 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24937 for (t = list; t; t = TREE_CHAIN (t))
24938 {
24939 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24940 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24941 "%s %#qS", spaces ? spaces : str, subst);
24942 spaces = spaces ? spaces : get_spaces (str);
24943 }
24944 free (spaces);
24945 return error_mark_node;
24946 }
24947
24948 return champ;
24949 }
24950
24951 /* Explicitly instantiate DECL. */
24952
24953 void
24954 do_decl_instantiation (tree decl, tree storage)
24955 {
24956 tree result = NULL_TREE;
24957 int extern_p = 0;
24958
24959 if (!decl || decl == error_mark_node)
24960 /* An error occurred, for which grokdeclarator has already issued
24961 an appropriate message. */
24962 return;
24963 else if (! DECL_LANG_SPECIFIC (decl))
24964 {
24965 error ("explicit instantiation of non-template %q#D", decl);
24966 return;
24967 }
24968 else if (DECL_DECLARED_CONCEPT_P (decl))
24969 {
24970 if (VAR_P (decl))
24971 error ("explicit instantiation of variable concept %q#D", decl);
24972 else
24973 error ("explicit instantiation of function concept %q#D", decl);
24974 return;
24975 }
24976
24977 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24978 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24979
24980 if (VAR_P (decl) && !var_templ)
24981 {
24982 /* There is an asymmetry here in the way VAR_DECLs and
24983 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24984 the latter, the DECL we get back will be marked as a
24985 template instantiation, and the appropriate
24986 DECL_TEMPLATE_INFO will be set up. This does not happen for
24987 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24988 should handle VAR_DECLs as it currently handles
24989 FUNCTION_DECLs. */
24990 if (!DECL_CLASS_SCOPE_P (decl))
24991 {
24992 error ("%qD is not a static data member of a class template", decl);
24993 return;
24994 }
24995 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24996 if (!result || !VAR_P (result))
24997 {
24998 error ("no matching template for %qD found", decl);
24999 return;
25000 }
25001 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25002 {
25003 error ("type %qT for explicit instantiation %qD does not match "
25004 "declared type %qT", TREE_TYPE (result), decl,
25005 TREE_TYPE (decl));
25006 return;
25007 }
25008 }
25009 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25010 {
25011 error ("explicit instantiation of %q#D", decl);
25012 return;
25013 }
25014 else
25015 result = decl;
25016
25017 /* Check for various error cases. Note that if the explicit
25018 instantiation is valid the RESULT will currently be marked as an
25019 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25020 until we get here. */
25021
25022 if (DECL_TEMPLATE_SPECIALIZATION (result))
25023 {
25024 /* DR 259 [temp.spec].
25025
25026 Both an explicit instantiation and a declaration of an explicit
25027 specialization shall not appear in a program unless the explicit
25028 instantiation follows a declaration of the explicit specialization.
25029
25030 For a given set of template parameters, if an explicit
25031 instantiation of a template appears after a declaration of an
25032 explicit specialization for that template, the explicit
25033 instantiation has no effect. */
25034 return;
25035 }
25036 else if (DECL_EXPLICIT_INSTANTIATION (result))
25037 {
25038 /* [temp.spec]
25039
25040 No program shall explicitly instantiate any template more
25041 than once.
25042
25043 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25044 the first instantiation was `extern' and the second is not,
25045 and EXTERN_P for the opposite case. */
25046 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25047 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25048 /* If an "extern" explicit instantiation follows an ordinary
25049 explicit instantiation, the template is instantiated. */
25050 if (extern_p)
25051 return;
25052 }
25053 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25054 {
25055 error ("no matching template for %qD found", result);
25056 return;
25057 }
25058 else if (!DECL_TEMPLATE_INFO (result))
25059 {
25060 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25061 return;
25062 }
25063
25064 if (storage == NULL_TREE)
25065 ;
25066 else if (storage == ridpointers[(int) RID_EXTERN])
25067 {
25068 if (cxx_dialect == cxx98)
25069 pedwarn (input_location, OPT_Wpedantic,
25070 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25071 "instantiations");
25072 extern_p = 1;
25073 }
25074 else
25075 error ("storage class %qD applied to template instantiation", storage);
25076
25077 check_explicit_instantiation_namespace (result);
25078 mark_decl_instantiated (result, extern_p);
25079 if (! extern_p)
25080 instantiate_decl (result, /*defer_ok=*/true,
25081 /*expl_inst_class_mem_p=*/false);
25082 }
25083
25084 static void
25085 mark_class_instantiated (tree t, int extern_p)
25086 {
25087 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25088 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25089 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25090 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25091 if (! extern_p)
25092 {
25093 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25094 rest_of_type_compilation (t, 1);
25095 }
25096 }
25097
25098 /* Perform an explicit instantiation of template class T. STORAGE, if
25099 non-null, is the RID for extern, inline or static. COMPLAIN is
25100 nonzero if this is called from the parser, zero if called recursively,
25101 since the standard is unclear (as detailed below). */
25102
25103 void
25104 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25105 {
25106 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25107 {
25108 if (tree ti = TYPE_TEMPLATE_INFO (t))
25109 error ("explicit instantiation of non-class template %qD",
25110 TI_TEMPLATE (ti));
25111 else
25112 error ("explicit instantiation of non-template type %qT", t);
25113 return;
25114 }
25115
25116 complete_type (t);
25117
25118 if (!COMPLETE_TYPE_P (t))
25119 {
25120 if (complain & tf_error)
25121 error ("explicit instantiation of %q#T before definition of template",
25122 t);
25123 return;
25124 }
25125
25126 /* At most one of these will be true. */
25127 bool extern_p = false;
25128 bool nomem_p = false;
25129 bool static_p = false;
25130
25131 if (storage != NULL_TREE)
25132 {
25133 if (storage == ridpointers[(int) RID_EXTERN])
25134 {
25135 if (cxx_dialect == cxx98)
25136 pedwarn (input_location, OPT_Wpedantic,
25137 "ISO C++ 1998 forbids the use of %<extern%> on "
25138 "explicit instantiations");
25139 }
25140 else
25141 pedwarn (input_location, OPT_Wpedantic,
25142 "ISO C++ forbids the use of %qE"
25143 " on explicit instantiations", storage);
25144
25145 if (storage == ridpointers[(int) RID_INLINE])
25146 nomem_p = true;
25147 else if (storage == ridpointers[(int) RID_EXTERN])
25148 extern_p = true;
25149 else if (storage == ridpointers[(int) RID_STATIC])
25150 static_p = true;
25151 else
25152 error ("storage class %qD applied to template instantiation",
25153 storage);
25154 }
25155
25156 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25157 /* DR 259 [temp.spec].
25158
25159 Both an explicit instantiation and a declaration of an explicit
25160 specialization shall not appear in a program unless the
25161 explicit instantiation follows a declaration of the explicit
25162 specialization.
25163
25164 For a given set of template parameters, if an explicit
25165 instantiation of a template appears after a declaration of an
25166 explicit specialization for that template, the explicit
25167 instantiation has no effect. */
25168 return;
25169
25170 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25171 {
25172 /* We've already instantiated the template. */
25173
25174 /* [temp.spec]
25175
25176 No program shall explicitly instantiate any template more
25177 than once.
25178
25179 If EXTERN_P then this is ok. */
25180 if (!extern_p && (complain & tf_error))
25181 permerror (input_location,
25182 "duplicate explicit instantiation of %q#T", t);
25183
25184 return;
25185 }
25186
25187 check_explicit_instantiation_namespace (TYPE_NAME (t));
25188 mark_class_instantiated (t, extern_p);
25189
25190 if (nomem_p)
25191 return;
25192
25193 /* In contrast to implicit instantiation, where only the
25194 declarations, and not the definitions, of members are
25195 instantiated, we have here:
25196
25197 [temp.explicit]
25198
25199 An explicit instantiation that names a class template
25200 specialization is also an explicit instantiation of the same
25201 kind (declaration or definition) of each of its members (not
25202 including members inherited from base classes and members
25203 that are templates) that has not been previously explicitly
25204 specialized in the translation unit containing the explicit
25205 instantiation, provided that the associated constraints, if
25206 any, of that member are satisfied by the template arguments
25207 of the explicit instantiation. */
25208 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25209 if ((VAR_P (fld)
25210 || (TREE_CODE (fld) == FUNCTION_DECL
25211 && !static_p
25212 && user_provided_p (fld)))
25213 && DECL_TEMPLATE_INSTANTIATION (fld)
25214 && constraints_satisfied_p (fld))
25215 {
25216 mark_decl_instantiated (fld, extern_p);
25217 if (! extern_p)
25218 instantiate_decl (fld, /*defer_ok=*/true,
25219 /*expl_inst_class_mem_p=*/true);
25220 }
25221 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25222 {
25223 tree type = TREE_TYPE (fld);
25224
25225 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25226 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25227 do_type_instantiation (type, storage, 0);
25228 }
25229 }
25230
25231 /* Given a function DECL, which is a specialization of TMPL, modify
25232 DECL to be a re-instantiation of TMPL with the same template
25233 arguments. TMPL should be the template into which tsubst'ing
25234 should occur for DECL, not the most general template.
25235
25236 One reason for doing this is a scenario like this:
25237
25238 template <class T>
25239 void f(const T&, int i);
25240
25241 void g() { f(3, 7); }
25242
25243 template <class T>
25244 void f(const T& t, const int i) { }
25245
25246 Note that when the template is first instantiated, with
25247 instantiate_template, the resulting DECL will have no name for the
25248 first parameter, and the wrong type for the second. So, when we go
25249 to instantiate the DECL, we regenerate it. */
25250
25251 static void
25252 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25253 {
25254 /* The arguments used to instantiate DECL, from the most general
25255 template. */
25256 tree code_pattern;
25257
25258 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25259
25260 /* Make sure that we can see identifiers, and compute access
25261 correctly. */
25262 push_access_scope (decl);
25263
25264 if (TREE_CODE (decl) == FUNCTION_DECL)
25265 {
25266 tree decl_parm;
25267 tree pattern_parm;
25268 tree specs;
25269 int args_depth;
25270 int parms_depth;
25271
25272 args_depth = TMPL_ARGS_DEPTH (args);
25273 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25274 if (args_depth > parms_depth)
25275 args = get_innermost_template_args (args, parms_depth);
25276
25277 /* Instantiate a dynamic exception-specification. noexcept will be
25278 handled below. */
25279 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25280 if (TREE_VALUE (raises))
25281 {
25282 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25283 args, tf_error, NULL_TREE,
25284 /*defer_ok*/false);
25285 if (specs && specs != error_mark_node)
25286 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25287 specs);
25288 }
25289
25290 /* Merge parameter declarations. */
25291 decl_parm = skip_artificial_parms_for (decl,
25292 DECL_ARGUMENTS (decl));
25293 pattern_parm
25294 = skip_artificial_parms_for (code_pattern,
25295 DECL_ARGUMENTS (code_pattern));
25296 while (decl_parm && !DECL_PACK_P (pattern_parm))
25297 {
25298 tree parm_type;
25299 tree attributes;
25300
25301 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25302 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25303 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25304 NULL_TREE);
25305 parm_type = type_decays_to (parm_type);
25306 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25307 TREE_TYPE (decl_parm) = parm_type;
25308 attributes = DECL_ATTRIBUTES (pattern_parm);
25309 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25310 {
25311 DECL_ATTRIBUTES (decl_parm) = attributes;
25312 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25313 }
25314 decl_parm = DECL_CHAIN (decl_parm);
25315 pattern_parm = DECL_CHAIN (pattern_parm);
25316 }
25317 /* Merge any parameters that match with the function parameter
25318 pack. */
25319 if (pattern_parm && DECL_PACK_P (pattern_parm))
25320 {
25321 int i, len;
25322 tree expanded_types;
25323 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25324 the parameters in this function parameter pack. */
25325 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25326 args, tf_error, NULL_TREE);
25327 len = TREE_VEC_LENGTH (expanded_types);
25328 for (i = 0; i < len; i++)
25329 {
25330 tree parm_type;
25331 tree attributes;
25332
25333 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25334 /* Rename the parameter to include the index. */
25335 DECL_NAME (decl_parm) =
25336 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25337 parm_type = TREE_VEC_ELT (expanded_types, i);
25338 parm_type = type_decays_to (parm_type);
25339 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25340 TREE_TYPE (decl_parm) = parm_type;
25341 attributes = DECL_ATTRIBUTES (pattern_parm);
25342 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25343 {
25344 DECL_ATTRIBUTES (decl_parm) = attributes;
25345 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25346 }
25347 decl_parm = DECL_CHAIN (decl_parm);
25348 }
25349 }
25350 /* Merge additional specifiers from the CODE_PATTERN. */
25351 if (DECL_DECLARED_INLINE_P (code_pattern)
25352 && !DECL_DECLARED_INLINE_P (decl))
25353 DECL_DECLARED_INLINE_P (decl) = 1;
25354
25355 maybe_instantiate_noexcept (decl, tf_error);
25356 }
25357 else if (VAR_P (decl))
25358 {
25359 start_lambda_scope (decl);
25360 DECL_INITIAL (decl) =
25361 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25362 tf_error, DECL_TI_TEMPLATE (decl));
25363 finish_lambda_scope ();
25364 if (VAR_HAD_UNKNOWN_BOUND (decl))
25365 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25366 tf_error, DECL_TI_TEMPLATE (decl));
25367 }
25368 else
25369 gcc_unreachable ();
25370
25371 pop_access_scope (decl);
25372 }
25373
25374 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25375 substituted to get DECL. */
25376
25377 tree
25378 template_for_substitution (tree decl)
25379 {
25380 tree tmpl = DECL_TI_TEMPLATE (decl);
25381
25382 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25383 for the instantiation. This is not always the most general
25384 template. Consider, for example:
25385
25386 template <class T>
25387 struct S { template <class U> void f();
25388 template <> void f<int>(); };
25389
25390 and an instantiation of S<double>::f<int>. We want TD to be the
25391 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25392 while (/* An instantiation cannot have a definition, so we need a
25393 more general template. */
25394 DECL_TEMPLATE_INSTANTIATION (tmpl)
25395 /* We must also deal with friend templates. Given:
25396
25397 template <class T> struct S {
25398 template <class U> friend void f() {};
25399 };
25400
25401 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25402 so far as the language is concerned, but that's still
25403 where we get the pattern for the instantiation from. On
25404 other hand, if the definition comes outside the class, say:
25405
25406 template <class T> struct S {
25407 template <class U> friend void f();
25408 };
25409 template <class U> friend void f() {}
25410
25411 we don't need to look any further. That's what the check for
25412 DECL_INITIAL is for. */
25413 || (TREE_CODE (decl) == FUNCTION_DECL
25414 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25415 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25416 {
25417 /* The present template, TD, should not be a definition. If it
25418 were a definition, we should be using it! Note that we
25419 cannot restructure the loop to just keep going until we find
25420 a template with a definition, since that might go too far if
25421 a specialization was declared, but not defined. */
25422
25423 /* Fetch the more general template. */
25424 tmpl = DECL_TI_TEMPLATE (tmpl);
25425 }
25426
25427 return tmpl;
25428 }
25429
25430 /* Returns true if we need to instantiate this template instance even if we
25431 know we aren't going to emit it. */
25432
25433 bool
25434 always_instantiate_p (tree decl)
25435 {
25436 /* We always instantiate inline functions so that we can inline them. An
25437 explicit instantiation declaration prohibits implicit instantiation of
25438 non-inline functions. With high levels of optimization, we would
25439 normally inline non-inline functions -- but we're not allowed to do
25440 that for "extern template" functions. Therefore, we check
25441 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25442 return ((TREE_CODE (decl) == FUNCTION_DECL
25443 && (DECL_DECLARED_INLINE_P (decl)
25444 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25445 /* And we need to instantiate static data members so that
25446 their initializers are available in integral constant
25447 expressions. */
25448 || (VAR_P (decl)
25449 && decl_maybe_constant_var_p (decl)));
25450 }
25451
25452 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25453 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25454 error, true otherwise. */
25455
25456 bool
25457 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25458 {
25459 tree fntype, spec, noex;
25460
25461 /* Don't instantiate a noexcept-specification from template context. */
25462 if (processing_template_decl
25463 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25464 return true;
25465
25466 if (DECL_MAYBE_DELETED (fn))
25467 {
25468 if (fn == current_function_decl)
25469 /* We're in start_preparsed_function, keep going. */
25470 return true;
25471
25472 ++function_depth;
25473 synthesize_method (fn);
25474 --function_depth;
25475 return !DECL_MAYBE_DELETED (fn);
25476 }
25477
25478 fntype = TREE_TYPE (fn);
25479 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25480
25481 if (!spec || !TREE_PURPOSE (spec))
25482 return true;
25483
25484 noex = TREE_PURPOSE (spec);
25485 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25486 && TREE_CODE (noex) != DEFERRED_PARSE)
25487 return true;
25488
25489 tree orig_fn = NULL_TREE;
25490 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25491 its FUNCTION_DECL for the rest of this function -- push_access_scope
25492 doesn't accept TEMPLATE_DECLs. */
25493 if (DECL_FUNCTION_TEMPLATE_P (fn))
25494 {
25495 orig_fn = fn;
25496 fn = DECL_TEMPLATE_RESULT (fn);
25497 }
25498
25499 if (DECL_CLONED_FUNCTION_P (fn))
25500 {
25501 tree prime = DECL_CLONED_FUNCTION (fn);
25502 if (!maybe_instantiate_noexcept (prime, complain))
25503 return false;
25504 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25505 }
25506 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25507 {
25508 static hash_set<tree>* fns = new hash_set<tree>;
25509 bool added = false;
25510 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25511 {
25512 spec = get_defaulted_eh_spec (fn, complain);
25513 if (spec == error_mark_node)
25514 /* This might have failed because of an unparsed DMI, so
25515 let's try again later. */
25516 return false;
25517 }
25518 else if (!(added = !fns->add (fn)))
25519 {
25520 /* If hash_set::add returns true, the element was already there. */
25521 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25522 DECL_SOURCE_LOCATION (fn));
25523 error_at (loc,
25524 "exception specification of %qD depends on itself",
25525 fn);
25526 spec = noexcept_false_spec;
25527 }
25528 else if (push_tinst_level (fn))
25529 {
25530 push_to_top_level ();
25531 push_access_scope (fn);
25532 push_deferring_access_checks (dk_no_deferred);
25533 input_location = DECL_SOURCE_LOCATION (fn);
25534
25535 if (!DECL_LOCAL_DECL_P (fn))
25536 {
25537 /* If needed, set current_class_ptr for the benefit of
25538 tsubst_copy/PARM_DECL. The exception pattern will
25539 refer to the parm of the template, not the
25540 instantiation. */
25541 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25542 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25543 {
25544 tree this_parm = DECL_ARGUMENTS (tdecl);
25545 current_class_ptr = NULL_TREE;
25546 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25547 current_class_ptr = this_parm;
25548 }
25549 }
25550
25551 /* If this function is represented by a TEMPLATE_DECL, then
25552 the deferred noexcept-specification might still contain
25553 dependent types, even after substitution. And we need the
25554 dependency check functions to work in build_noexcept_spec. */
25555 if (orig_fn)
25556 ++processing_template_decl;
25557
25558 /* Do deferred instantiation of the noexcept-specifier. */
25559 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25560 DEFERRED_NOEXCEPT_ARGS (noex),
25561 tf_warning_or_error, fn,
25562 /*function_p=*/false,
25563 /*i_c_e_p=*/true);
25564
25565 /* Build up the noexcept-specification. */
25566 spec = build_noexcept_spec (noex, tf_warning_or_error);
25567
25568 if (orig_fn)
25569 --processing_template_decl;
25570
25571 pop_deferring_access_checks ();
25572 pop_access_scope (fn);
25573 pop_tinst_level ();
25574 pop_from_top_level ();
25575 }
25576 else
25577 spec = noexcept_false_spec;
25578
25579 if (added)
25580 fns->remove (fn);
25581 }
25582
25583 if (spec == error_mark_node)
25584 {
25585 /* This failed with a hard error, so let's go with false. */
25586 gcc_assert (seen_error ());
25587 spec = noexcept_false_spec;
25588 }
25589
25590 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25591 if (orig_fn)
25592 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25593
25594 return true;
25595 }
25596
25597 /* We're starting to process the function INST, an instantiation of PATTERN;
25598 add their parameters to local_specializations. */
25599
25600 static void
25601 register_parameter_specializations (tree pattern, tree inst)
25602 {
25603 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25604 tree spec_parm = DECL_ARGUMENTS (inst);
25605 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25606 {
25607 register_local_specialization (spec_parm, tmpl_parm);
25608 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25609 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25610 }
25611 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25612 {
25613 if (!DECL_PACK_P (tmpl_parm)
25614 || (spec_parm && DECL_PACK_P (spec_parm)))
25615 {
25616 register_local_specialization (spec_parm, tmpl_parm);
25617 spec_parm = DECL_CHAIN (spec_parm);
25618 }
25619 else
25620 {
25621 /* Register the (value) argument pack as a specialization of
25622 TMPL_PARM, then move on. */
25623 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25624 register_local_specialization (argpack, tmpl_parm);
25625 }
25626 }
25627 gcc_assert (!spec_parm);
25628 }
25629
25630 /* Instantiate the body of D using PATTERN with ARGS. We have
25631 already determined PATTERN is the correct template to use.
25632 NESTED_P is true if this is a nested function, in which case
25633 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25634
25635 static void
25636 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25637 {
25638 tree td = NULL_TREE;
25639 tree code_pattern = pattern;
25640
25641 if (!nested_p)
25642 {
25643 td = pattern;
25644 code_pattern = DECL_TEMPLATE_RESULT (td);
25645 }
25646 else
25647 /* Only OMP reductions are nested. */
25648 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25649
25650 vec<tree> omp_privatization_save;
25651 if (current_function_decl)
25652 save_omp_privatization_clauses (omp_privatization_save);
25653
25654 bool push_to_top
25655 = !(current_function_decl
25656 && !LAMBDA_FUNCTION_P (d)
25657 && decl_function_context (d) == current_function_decl);
25658
25659 if (push_to_top)
25660 push_to_top_level ();
25661 else
25662 {
25663 gcc_assert (!processing_template_decl);
25664 push_function_context ();
25665 cp_unevaluated_operand = 0;
25666 c_inhibit_evaluation_warnings = 0;
25667 }
25668
25669 if (VAR_P (d))
25670 {
25671 /* The variable might be a lambda's extra scope, and that
25672 lambda's visibility depends on D's. */
25673 maybe_commonize_var (d);
25674 determine_visibility (d);
25675 }
25676
25677 /* Mark D as instantiated so that recursive calls to
25678 instantiate_decl do not try to instantiate it again. */
25679 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25680
25681 if (td)
25682 /* Regenerate the declaration in case the template has been modified
25683 by a subsequent redeclaration. */
25684 regenerate_decl_from_template (d, td, args);
25685
25686 /* We already set the file and line above. Reset them now in case
25687 they changed as a result of calling regenerate_decl_from_template. */
25688 input_location = DECL_SOURCE_LOCATION (d);
25689
25690 if (VAR_P (d))
25691 {
25692 /* Clear out DECL_RTL; whatever was there before may not be right
25693 since we've reset the type of the declaration. */
25694 SET_DECL_RTL (d, NULL);
25695 DECL_IN_AGGR_P (d) = 0;
25696
25697 /* The initializer is placed in DECL_INITIAL by
25698 regenerate_decl_from_template so we don't need to
25699 push/pop_access_scope again here. Pull it out so that
25700 cp_finish_decl can process it. */
25701 bool const_init = false;
25702 tree init = DECL_INITIAL (d);
25703 DECL_INITIAL (d) = NULL_TREE;
25704 DECL_INITIALIZED_P (d) = 0;
25705
25706 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25707 initializer. That function will defer actual emission until
25708 we have a chance to determine linkage. */
25709 DECL_EXTERNAL (d) = 0;
25710
25711 /* Enter the scope of D so that access-checking works correctly. */
25712 bool enter_context = DECL_CLASS_SCOPE_P (d);
25713 if (enter_context)
25714 push_nested_class (DECL_CONTEXT (d));
25715
25716 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25717 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25718
25719 if (enter_context)
25720 pop_nested_class ();
25721 }
25722 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25723 synthesize_method (d);
25724 else if (TREE_CODE (d) == FUNCTION_DECL)
25725 {
25726 /* Set up the list of local specializations. */
25727 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25728 tree block = NULL_TREE;
25729
25730 /* Set up context. */
25731 if (nested_p)
25732 block = push_stmt_list ();
25733 else
25734 {
25735 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25736
25737 perform_instantiation_time_access_checks (code_pattern, args);
25738 }
25739
25740 /* Create substitution entries for the parameters. */
25741 register_parameter_specializations (code_pattern, d);
25742
25743 /* Substitute into the body of the function. */
25744 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25745 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25746 tf_warning_or_error, d);
25747 else
25748 {
25749 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25750 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25751 /*integral_constant_expression_p=*/false);
25752
25753 /* Set the current input_location to the end of the function
25754 so that finish_function knows where we are. */
25755 input_location
25756 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25757
25758 /* Remember if we saw an infinite loop in the template. */
25759 current_function_infinite_loop
25760 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25761 }
25762
25763 /* Finish the function. */
25764 if (nested_p)
25765 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25766 else
25767 {
25768 d = finish_function (/*inline_p=*/false);
25769 expand_or_defer_fn (d);
25770 }
25771
25772 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25773 cp_check_omp_declare_reduction (d);
25774 }
25775
25776 /* We're not deferring instantiation any more. */
25777 if (!nested_p)
25778 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25779
25780 if (push_to_top)
25781 pop_from_top_level ();
25782 else
25783 pop_function_context ();
25784
25785 if (current_function_decl)
25786 restore_omp_privatization_clauses (omp_privatization_save);
25787 }
25788
25789 /* Produce the definition of D, a _DECL generated from a template. If
25790 DEFER_OK is true, then we don't have to actually do the
25791 instantiation now; we just have to do it sometime. Normally it is
25792 an error if this is an explicit instantiation but D is undefined.
25793 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25794 instantiated class template. */
25795
25796 tree
25797 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25798 {
25799 tree tmpl = DECL_TI_TEMPLATE (d);
25800 tree gen_args;
25801 tree args;
25802 tree td;
25803 tree code_pattern;
25804 tree spec;
25805 tree gen_tmpl;
25806 bool pattern_defined;
25807 location_t saved_loc = input_location;
25808 int saved_unevaluated_operand = cp_unevaluated_operand;
25809 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25810 bool external_p;
25811 bool deleted_p;
25812
25813 /* This function should only be used to instantiate templates for
25814 functions and static member variables. */
25815 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25816
25817 /* A concept is never instantiated. */
25818 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25819
25820 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25821
25822 /* Variables are never deferred; if instantiation is required, they
25823 are instantiated right away. That allows for better code in the
25824 case that an expression refers to the value of the variable --
25825 if the variable has a constant value the referring expression can
25826 take advantage of that fact. */
25827 if (VAR_P (d))
25828 defer_ok = false;
25829
25830 /* Don't instantiate cloned functions. Instead, instantiate the
25831 functions they cloned. */
25832 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25833 d = DECL_CLONED_FUNCTION (d);
25834
25835 if (DECL_TEMPLATE_INSTANTIATED (d)
25836 || TREE_TYPE (d) == error_mark_node
25837 || (TREE_CODE (d) == FUNCTION_DECL
25838 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25839 || DECL_TEMPLATE_SPECIALIZATION (d))
25840 /* D has already been instantiated or explicitly specialized, so
25841 there's nothing for us to do here.
25842
25843 It might seem reasonable to check whether or not D is an explicit
25844 instantiation, and, if so, stop here. But when an explicit
25845 instantiation is deferred until the end of the compilation,
25846 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25847 the instantiation. */
25848 return d;
25849
25850 /* Check to see whether we know that this template will be
25851 instantiated in some other file, as with "extern template"
25852 extension. */
25853 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25854
25855 /* In general, we do not instantiate such templates. */
25856 if (external_p && !always_instantiate_p (d))
25857 return d;
25858
25859 gen_tmpl = most_general_template (tmpl);
25860 gen_args = DECL_TI_ARGS (d);
25861
25862 /* We should already have the extra args. */
25863 gcc_checking_assert (tmpl == gen_tmpl
25864 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25865 == TMPL_ARGS_DEPTH (gen_args)));
25866 /* And what's in the hash table should match D. */
25867 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25868 == d
25869 || spec == NULL_TREE);
25870
25871 /* This needs to happen before any tsubsting. */
25872 if (! push_tinst_level (d))
25873 return d;
25874
25875 timevar_push (TV_TEMPLATE_INST);
25876
25877 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25878 for the instantiation. */
25879 td = template_for_substitution (d);
25880 args = gen_args;
25881
25882 if (VAR_P (d))
25883 {
25884 /* Look up an explicit specialization, if any. */
25885 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25886 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25887 if (elt && elt != error_mark_node)
25888 {
25889 td = TREE_VALUE (elt);
25890 args = TREE_PURPOSE (elt);
25891 }
25892 }
25893
25894 code_pattern = DECL_TEMPLATE_RESULT (td);
25895
25896 /* We should never be trying to instantiate a member of a class
25897 template or partial specialization. */
25898 gcc_assert (d != code_pattern);
25899
25900 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25901 || DECL_TEMPLATE_SPECIALIZATION (td))
25902 /* In the case of a friend template whose definition is provided
25903 outside the class, we may have too many arguments. Drop the
25904 ones we don't need. The same is true for specializations. */
25905 args = get_innermost_template_args
25906 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25907
25908 if (TREE_CODE (d) == FUNCTION_DECL)
25909 {
25910 deleted_p = DECL_DELETED_FN (code_pattern);
25911 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25912 && DECL_INITIAL (code_pattern) != error_mark_node)
25913 || DECL_DEFAULTED_FN (code_pattern)
25914 || deleted_p);
25915 }
25916 else
25917 {
25918 deleted_p = false;
25919 if (DECL_CLASS_SCOPE_P (code_pattern))
25920 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25921 else
25922 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25923 }
25924
25925 /* We may be in the middle of deferred access check. Disable it now. */
25926 push_deferring_access_checks (dk_no_deferred);
25927
25928 /* Unless an explicit instantiation directive has already determined
25929 the linkage of D, remember that a definition is available for
25930 this entity. */
25931 if (pattern_defined
25932 && !DECL_INTERFACE_KNOWN (d)
25933 && !DECL_NOT_REALLY_EXTERN (d))
25934 mark_definable (d);
25935
25936 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25937 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25938 input_location = DECL_SOURCE_LOCATION (d);
25939
25940 /* If D is a member of an explicitly instantiated class template,
25941 and no definition is available, treat it like an implicit
25942 instantiation. */
25943 if (!pattern_defined && expl_inst_class_mem_p
25944 && DECL_EXPLICIT_INSTANTIATION (d))
25945 {
25946 /* Leave linkage flags alone on instantiations with anonymous
25947 visibility. */
25948 if (TREE_PUBLIC (d))
25949 {
25950 DECL_NOT_REALLY_EXTERN (d) = 0;
25951 DECL_INTERFACE_KNOWN (d) = 0;
25952 }
25953 SET_DECL_IMPLICIT_INSTANTIATION (d);
25954 }
25955
25956 /* Defer all other templates, unless we have been explicitly
25957 forbidden from doing so. */
25958 if (/* If there is no definition, we cannot instantiate the
25959 template. */
25960 ! pattern_defined
25961 /* If it's OK to postpone instantiation, do so. */
25962 || defer_ok
25963 /* If this is a static data member that will be defined
25964 elsewhere, we don't want to instantiate the entire data
25965 member, but we do want to instantiate the initializer so that
25966 we can substitute that elsewhere. */
25967 || (external_p && VAR_P (d))
25968 /* Handle here a deleted function too, avoid generating
25969 its body (c++/61080). */
25970 || deleted_p)
25971 {
25972 /* The definition of the static data member is now required so
25973 we must substitute the initializer. */
25974 if (VAR_P (d)
25975 && !DECL_INITIAL (d)
25976 && DECL_INITIAL (code_pattern))
25977 {
25978 tree ns;
25979 tree init;
25980 bool const_init = false;
25981 bool enter_context = DECL_CLASS_SCOPE_P (d);
25982
25983 ns = decl_namespace_context (d);
25984 push_nested_namespace (ns);
25985 if (enter_context)
25986 push_nested_class (DECL_CONTEXT (d));
25987 init = tsubst_expr (DECL_INITIAL (code_pattern),
25988 args,
25989 tf_warning_or_error, NULL_TREE,
25990 /*integral_constant_expression_p=*/false);
25991 /* If instantiating the initializer involved instantiating this
25992 again, don't call cp_finish_decl twice. */
25993 if (!DECL_INITIAL (d))
25994 {
25995 /* Make sure the initializer is still constant, in case of
25996 circular dependency (template/instantiate6.C). */
25997 const_init
25998 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25999 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26000 /*asmspec_tree=*/NULL_TREE,
26001 LOOKUP_ONLYCONVERTING);
26002 }
26003 if (enter_context)
26004 pop_nested_class ();
26005 pop_nested_namespace (ns);
26006 }
26007
26008 /* We restore the source position here because it's used by
26009 add_pending_template. */
26010 input_location = saved_loc;
26011
26012 if (at_eof && !pattern_defined
26013 && DECL_EXPLICIT_INSTANTIATION (d)
26014 && DECL_NOT_REALLY_EXTERN (d))
26015 /* [temp.explicit]
26016
26017 The definition of a non-exported function template, a
26018 non-exported member function template, or a non-exported
26019 member function or static data member of a class template
26020 shall be present in every translation unit in which it is
26021 explicitly instantiated. */
26022 permerror (input_location, "explicit instantiation of %qD "
26023 "but no definition available", d);
26024
26025 /* If we're in unevaluated context, we just wanted to get the
26026 constant value; this isn't an odr use, so don't queue
26027 a full instantiation. */
26028 if (!cp_unevaluated_operand
26029 /* ??? Historically, we have instantiated inline functions, even
26030 when marked as "extern template". */
26031 && !(external_p && VAR_P (d)))
26032 add_pending_template (d);
26033 }
26034 else
26035 {
26036 if (variable_template_p (gen_tmpl))
26037 note_variable_template_instantiation (d);
26038 instantiate_body (td, args, d, false);
26039 }
26040
26041 pop_deferring_access_checks ();
26042 timevar_pop (TV_TEMPLATE_INST);
26043 pop_tinst_level ();
26044 input_location = saved_loc;
26045 cp_unevaluated_operand = saved_unevaluated_operand;
26046 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26047
26048 return d;
26049 }
26050
26051 /* Run through the list of templates that we wish we could
26052 instantiate, and instantiate any we can. RETRIES is the
26053 number of times we retry pending template instantiation. */
26054
26055 void
26056 instantiate_pending_templates (int retries)
26057 {
26058 int reconsider;
26059 location_t saved_loc = input_location;
26060
26061 /* Instantiating templates may trigger vtable generation. This in turn
26062 may require further template instantiations. We place a limit here
26063 to avoid infinite loop. */
26064 if (pending_templates && retries >= max_tinst_depth)
26065 {
26066 tree decl = pending_templates->tinst->maybe_get_node ();
26067
26068 fatal_error (input_location,
26069 "template instantiation depth exceeds maximum of %d"
26070 " instantiating %q+D, possibly from virtual table generation"
26071 " (use %<-ftemplate-depth=%> to increase the maximum)",
26072 max_tinst_depth, decl);
26073 if (TREE_CODE (decl) == FUNCTION_DECL)
26074 /* Pretend that we defined it. */
26075 DECL_INITIAL (decl) = error_mark_node;
26076 return;
26077 }
26078
26079 do
26080 {
26081 struct pending_template **t = &pending_templates;
26082 struct pending_template *last = NULL;
26083 reconsider = 0;
26084 while (*t)
26085 {
26086 tree instantiation = reopen_tinst_level ((*t)->tinst);
26087 bool complete = false;
26088
26089 if (TYPE_P (instantiation))
26090 {
26091 if (!COMPLETE_TYPE_P (instantiation))
26092 {
26093 instantiate_class_template (instantiation);
26094 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26095 for (tree fld = TYPE_FIELDS (instantiation);
26096 fld; fld = TREE_CHAIN (fld))
26097 if ((VAR_P (fld)
26098 || (TREE_CODE (fld) == FUNCTION_DECL
26099 && !DECL_ARTIFICIAL (fld)))
26100 && DECL_TEMPLATE_INSTANTIATION (fld))
26101 instantiate_decl (fld,
26102 /*defer_ok=*/false,
26103 /*expl_inst_class_mem_p=*/false);
26104
26105 if (COMPLETE_TYPE_P (instantiation))
26106 reconsider = 1;
26107 }
26108
26109 complete = COMPLETE_TYPE_P (instantiation);
26110 }
26111 else
26112 {
26113 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26114 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26115 {
26116 instantiation
26117 = instantiate_decl (instantiation,
26118 /*defer_ok=*/false,
26119 /*expl_inst_class_mem_p=*/false);
26120 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26121 reconsider = 1;
26122 }
26123
26124 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26125 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26126 }
26127
26128 if (complete)
26129 {
26130 /* If INSTANTIATION has been instantiated, then we don't
26131 need to consider it again in the future. */
26132 struct pending_template *drop = *t;
26133 *t = (*t)->next;
26134 set_refcount_ptr (drop->tinst);
26135 pending_template_freelist ().free (drop);
26136 }
26137 else
26138 {
26139 last = *t;
26140 t = &(*t)->next;
26141 }
26142 tinst_depth = 0;
26143 set_refcount_ptr (current_tinst_level);
26144 }
26145 last_pending_template = last;
26146 }
26147 while (reconsider);
26148
26149 input_location = saved_loc;
26150 }
26151
26152 /* Substitute ARGVEC into T, which is a list of initializers for
26153 either base class or a non-static data member. The TREE_PURPOSEs
26154 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26155 instantiate_decl. */
26156
26157 static tree
26158 tsubst_initializer_list (tree t, tree argvec)
26159 {
26160 tree inits = NULL_TREE;
26161 tree target_ctor = error_mark_node;
26162
26163 for (; t; t = TREE_CHAIN (t))
26164 {
26165 tree decl;
26166 tree init;
26167 tree expanded_bases = NULL_TREE;
26168 tree expanded_arguments = NULL_TREE;
26169 int i, len = 1;
26170
26171 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26172 {
26173 tree expr;
26174 tree arg;
26175
26176 /* Expand the base class expansion type into separate base
26177 classes. */
26178 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26179 tf_warning_or_error,
26180 NULL_TREE);
26181 if (expanded_bases == error_mark_node)
26182 continue;
26183
26184 /* We'll be building separate TREE_LISTs of arguments for
26185 each base. */
26186 len = TREE_VEC_LENGTH (expanded_bases);
26187 expanded_arguments = make_tree_vec (len);
26188 for (i = 0; i < len; i++)
26189 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26190
26191 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26192 expand each argument in the TREE_VALUE of t. */
26193 expr = make_node (EXPR_PACK_EXPANSION);
26194 PACK_EXPANSION_LOCAL_P (expr) = true;
26195 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26196 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26197
26198 if (TREE_VALUE (t) == void_type_node)
26199 /* VOID_TYPE_NODE is used to indicate
26200 value-initialization. */
26201 {
26202 for (i = 0; i < len; i++)
26203 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26204 }
26205 else
26206 {
26207 /* Substitute parameter packs into each argument in the
26208 TREE_LIST. */
26209 in_base_initializer = 1;
26210 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26211 {
26212 tree expanded_exprs;
26213
26214 /* Expand the argument. */
26215 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26216 expanded_exprs
26217 = tsubst_pack_expansion (expr, argvec,
26218 tf_warning_or_error,
26219 NULL_TREE);
26220 if (expanded_exprs == error_mark_node)
26221 continue;
26222
26223 /* Prepend each of the expanded expressions to the
26224 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26225 for (i = 0; i < len; i++)
26226 {
26227 TREE_VEC_ELT (expanded_arguments, i) =
26228 tree_cons (NULL_TREE,
26229 TREE_VEC_ELT (expanded_exprs, i),
26230 TREE_VEC_ELT (expanded_arguments, i));
26231 }
26232 }
26233 in_base_initializer = 0;
26234
26235 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26236 since we built them backwards. */
26237 for (i = 0; i < len; i++)
26238 {
26239 TREE_VEC_ELT (expanded_arguments, i) =
26240 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26241 }
26242 }
26243 }
26244
26245 for (i = 0; i < len; ++i)
26246 {
26247 if (expanded_bases)
26248 {
26249 decl = TREE_VEC_ELT (expanded_bases, i);
26250 decl = expand_member_init (decl);
26251 init = TREE_VEC_ELT (expanded_arguments, i);
26252 }
26253 else
26254 {
26255 tree tmp;
26256 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26257 tf_warning_or_error, NULL_TREE);
26258
26259 decl = expand_member_init (decl);
26260 if (decl && !DECL_P (decl))
26261 in_base_initializer = 1;
26262
26263 init = TREE_VALUE (t);
26264 tmp = init;
26265 if (init != void_type_node)
26266 init = tsubst_expr (init, argvec,
26267 tf_warning_or_error, NULL_TREE,
26268 /*integral_constant_expression_p=*/false);
26269 if (init == NULL_TREE && tmp != NULL_TREE)
26270 /* If we had an initializer but it instantiated to nothing,
26271 value-initialize the object. This will only occur when
26272 the initializer was a pack expansion where the parameter
26273 packs used in that expansion were of length zero. */
26274 init = void_type_node;
26275 in_base_initializer = 0;
26276 }
26277
26278 if (target_ctor != error_mark_node
26279 && init != error_mark_node)
26280 {
26281 error ("mem-initializer for %qD follows constructor delegation",
26282 decl);
26283 return inits;
26284 }
26285 /* Look for a target constructor. */
26286 if (init != error_mark_node
26287 && decl && CLASS_TYPE_P (decl)
26288 && same_type_p (decl, current_class_type))
26289 {
26290 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26291 if (inits)
26292 {
26293 error ("constructor delegation follows mem-initializer for %qD",
26294 TREE_PURPOSE (inits));
26295 continue;
26296 }
26297 target_ctor = init;
26298 }
26299
26300 if (decl)
26301 {
26302 init = build_tree_list (decl, init);
26303 /* Carry over the dummy TREE_TYPE node containing the source
26304 location. */
26305 TREE_TYPE (init) = TREE_TYPE (t);
26306 TREE_CHAIN (init) = inits;
26307 inits = init;
26308 }
26309 }
26310 }
26311 return inits;
26312 }
26313
26314 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26315
26316 static void
26317 set_current_access_from_decl (tree decl)
26318 {
26319 if (TREE_PRIVATE (decl))
26320 current_access_specifier = access_private_node;
26321 else if (TREE_PROTECTED (decl))
26322 current_access_specifier = access_protected_node;
26323 else
26324 current_access_specifier = access_public_node;
26325 }
26326
26327 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26328 is the instantiation (which should have been created with
26329 start_enum) and ARGS are the template arguments to use. */
26330
26331 static void
26332 tsubst_enum (tree tag, tree newtag, tree args)
26333 {
26334 tree e;
26335
26336 if (SCOPED_ENUM_P (newtag))
26337 begin_scope (sk_scoped_enum, newtag);
26338
26339 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26340 {
26341 tree value;
26342 tree decl;
26343
26344 decl = TREE_VALUE (e);
26345 /* Note that in a template enum, the TREE_VALUE is the
26346 CONST_DECL, not the corresponding INTEGER_CST. */
26347 value = tsubst_expr (DECL_INITIAL (decl),
26348 args, tf_warning_or_error, NULL_TREE,
26349 /*integral_constant_expression_p=*/true);
26350
26351 /* Give this enumeration constant the correct access. */
26352 set_current_access_from_decl (decl);
26353
26354 /* Actually build the enumerator itself. Here we're assuming that
26355 enumerators can't have dependent attributes. */
26356 build_enumerator (DECL_NAME (decl), value, newtag,
26357 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26358 }
26359
26360 if (SCOPED_ENUM_P (newtag))
26361 finish_scope ();
26362
26363 finish_enum_value_list (newtag);
26364 finish_enum (newtag);
26365
26366 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26367 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26368 }
26369
26370 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26371 its type -- but without substituting the innermost set of template
26372 arguments. So, innermost set of template parameters will appear in
26373 the type. */
26374
26375 tree
26376 get_mostly_instantiated_function_type (tree decl)
26377 {
26378 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26379 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26380 }
26381
26382 /* Return truthvalue if we're processing a template different from
26383 the last one involved in diagnostics. */
26384 bool
26385 problematic_instantiation_changed (void)
26386 {
26387 return current_tinst_level != last_error_tinst_level;
26388 }
26389
26390 /* Remember current template involved in diagnostics. */
26391 void
26392 record_last_problematic_instantiation (void)
26393 {
26394 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26395 }
26396
26397 struct tinst_level *
26398 current_instantiation (void)
26399 {
26400 return current_tinst_level;
26401 }
26402
26403 /* Return TRUE if current_function_decl is being instantiated, false
26404 otherwise. */
26405
26406 bool
26407 instantiating_current_function_p (void)
26408 {
26409 return (current_instantiation ()
26410 && (current_instantiation ()->maybe_get_node ()
26411 == current_function_decl));
26412 }
26413
26414 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26415 type. Return false for ok, true for disallowed. Issue error and
26416 inform messages under control of COMPLAIN. */
26417
26418 static bool
26419 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26420 {
26421 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26422 return false;
26423 else if (TYPE_PTR_P (type))
26424 return false;
26425 else if (TYPE_REF_P (type)
26426 && !TYPE_REF_IS_RVALUE (type))
26427 return false;
26428 else if (TYPE_PTRMEM_P (type))
26429 return false;
26430 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26431 {
26432 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26433 {
26434 if (complain & tf_error)
26435 error ("non-type template parameters of deduced class type only "
26436 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26437 return true;
26438 }
26439 return false;
26440 }
26441 else if (TREE_CODE (type) == TYPENAME_TYPE)
26442 return false;
26443 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26444 return false;
26445 else if (TREE_CODE (type) == NULLPTR_TYPE)
26446 return false;
26447 /* A bound template template parm could later be instantiated to have a valid
26448 nontype parm type via an alias template. */
26449 else if (cxx_dialect >= cxx11
26450 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26451 return false;
26452 else if (VOID_TYPE_P (type))
26453 /* Fall through. */;
26454 else if (cxx_dialect >= cxx20)
26455 {
26456 if (dependent_type_p (type))
26457 return false;
26458 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26459 return true;
26460 if (structural_type_p (type))
26461 return false;
26462 if (complain & tf_error)
26463 {
26464 auto_diagnostic_group d;
26465 error ("%qT is not a valid type for a template non-type "
26466 "parameter because it is not structural", type);
26467 structural_type_p (type, true);
26468 }
26469 return true;
26470 }
26471 else if (CLASS_TYPE_P (type))
26472 {
26473 if (complain & tf_error)
26474 error ("non-type template parameters of class type only available "
26475 "with %<-std=c++20%> or %<-std=gnu++20%>");
26476 return true;
26477 }
26478
26479 if (complain & tf_error)
26480 {
26481 if (type == error_mark_node)
26482 inform (input_location, "invalid template non-type parameter");
26483 else
26484 error ("%q#T is not a valid type for a template non-type parameter",
26485 type);
26486 }
26487 return true;
26488 }
26489
26490 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26491 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26492
26493 static bool
26494 dependent_type_p_r (tree type)
26495 {
26496 tree scope;
26497
26498 /* [temp.dep.type]
26499
26500 A type is dependent if it is:
26501
26502 -- a template parameter. Template template parameters are types
26503 for us (since TYPE_P holds true for them) so we handle
26504 them here. */
26505 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26506 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26507 return true;
26508 /* -- a qualified-id with a nested-name-specifier which contains a
26509 class-name that names a dependent type or whose unqualified-id
26510 names a dependent type. */
26511 if (TREE_CODE (type) == TYPENAME_TYPE)
26512 return true;
26513
26514 /* An alias template specialization can be dependent even if the
26515 resulting type is not. */
26516 if (dependent_alias_template_spec_p (type, nt_transparent))
26517 return true;
26518
26519 /* -- a cv-qualified type where the cv-unqualified type is
26520 dependent.
26521 No code is necessary for this bullet; the code below handles
26522 cv-qualified types, and we don't want to strip aliases with
26523 TYPE_MAIN_VARIANT because of DR 1558. */
26524 /* -- a compound type constructed from any dependent type. */
26525 if (TYPE_PTRMEM_P (type))
26526 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26527 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26528 (type)));
26529 else if (INDIRECT_TYPE_P (type))
26530 return dependent_type_p (TREE_TYPE (type));
26531 else if (FUNC_OR_METHOD_TYPE_P (type))
26532 {
26533 tree arg_type;
26534
26535 if (dependent_type_p (TREE_TYPE (type)))
26536 return true;
26537 for (arg_type = TYPE_ARG_TYPES (type);
26538 arg_type;
26539 arg_type = TREE_CHAIN (arg_type))
26540 if (dependent_type_p (TREE_VALUE (arg_type)))
26541 return true;
26542 if (cxx_dialect >= cxx17)
26543 /* A value-dependent noexcept-specifier makes the type dependent. */
26544 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26545 if (tree noex = TREE_PURPOSE (spec))
26546 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26547 affect overload resolution and treating it as dependent breaks
26548 things. Same for an unparsed noexcept expression. */
26549 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26550 && TREE_CODE (noex) != DEFERRED_PARSE
26551 && value_dependent_expression_p (noex))
26552 return true;
26553 return false;
26554 }
26555 /* -- an array type constructed from any dependent type or whose
26556 size is specified by a constant expression that is
26557 value-dependent.
26558
26559 We checked for type- and value-dependence of the bounds in
26560 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26561 if (TREE_CODE (type) == ARRAY_TYPE)
26562 {
26563 if (TYPE_DOMAIN (type)
26564 && dependent_type_p (TYPE_DOMAIN (type)))
26565 return true;
26566 return dependent_type_p (TREE_TYPE (type));
26567 }
26568
26569 /* -- a template-id in which either the template name is a template
26570 parameter ... */
26571 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26572 return true;
26573 /* ... or any of the template arguments is a dependent type or
26574 an expression that is type-dependent or value-dependent. */
26575 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26576 && (any_dependent_template_arguments_p
26577 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26578 return true;
26579
26580 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26581 dependent; if the argument of the `typeof' expression is not
26582 type-dependent, then it should already been have resolved. */
26583 if (TREE_CODE (type) == TYPEOF_TYPE
26584 || TREE_CODE (type) == DECLTYPE_TYPE
26585 || TREE_CODE (type) == UNDERLYING_TYPE)
26586 return true;
26587
26588 /* A template argument pack is dependent if any of its packed
26589 arguments are. */
26590 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26591 {
26592 tree args = ARGUMENT_PACK_ARGS (type);
26593 int i, len = TREE_VEC_LENGTH (args);
26594 for (i = 0; i < len; ++i)
26595 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26596 return true;
26597 }
26598
26599 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26600 be template parameters. */
26601 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26602 return true;
26603
26604 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26605 return true;
26606
26607 /* The standard does not specifically mention types that are local
26608 to template functions or local classes, but they should be
26609 considered dependent too. For example:
26610
26611 template <int I> void f() {
26612 enum E { a = I };
26613 S<sizeof (E)> s;
26614 }
26615
26616 The size of `E' cannot be known until the value of `I' has been
26617 determined. Therefore, `E' must be considered dependent. */
26618 scope = TYPE_CONTEXT (type);
26619 if (scope && TYPE_P (scope))
26620 return dependent_type_p (scope);
26621 /* Don't use type_dependent_expression_p here, as it can lead
26622 to infinite recursion trying to determine whether a lambda
26623 nested in a lambda is dependent (c++/47687). */
26624 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26625 && DECL_LANG_SPECIFIC (scope)
26626 && DECL_TEMPLATE_INFO (scope)
26627 && (any_dependent_template_arguments_p
26628 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26629 return true;
26630
26631 /* Other types are non-dependent. */
26632 return false;
26633 }
26634
26635 /* Returns TRUE if TYPE is dependent, in the sense of
26636 [temp.dep.type]. Note that a NULL type is considered dependent. */
26637
26638 bool
26639 dependent_type_p (tree type)
26640 {
26641 /* If there are no template parameters in scope, then there can't be
26642 any dependent types. */
26643 if (!processing_template_decl)
26644 {
26645 /* If we are not processing a template, then nobody should be
26646 providing us with a dependent type. */
26647 gcc_assert (type);
26648 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26649 return false;
26650 }
26651
26652 /* If the type is NULL, we have not computed a type for the entity
26653 in question; in that case, the type is dependent. */
26654 if (!type)
26655 return true;
26656
26657 /* Erroneous types can be considered non-dependent. */
26658 if (type == error_mark_node)
26659 return false;
26660
26661 /* Getting here with global_type_node means we improperly called this
26662 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26663 gcc_checking_assert (type != global_type_node);
26664
26665 /* If we have not already computed the appropriate value for TYPE,
26666 do so now. */
26667 if (!TYPE_DEPENDENT_P_VALID (type))
26668 {
26669 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26670 TYPE_DEPENDENT_P_VALID (type) = 1;
26671 }
26672
26673 return TYPE_DEPENDENT_P (type);
26674 }
26675
26676 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26677 lookup. In other words, a dependent type that is not the current
26678 instantiation. */
26679
26680 bool
26681 dependent_scope_p (tree scope)
26682 {
26683 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26684 && !currently_open_class (scope));
26685 }
26686
26687 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26688 an unknown base of 'this' (and is therefore instantiation-dependent). */
26689
26690 static bool
26691 unknown_base_ref_p (tree t)
26692 {
26693 if (!current_class_ptr)
26694 return false;
26695
26696 tree mem = TREE_OPERAND (t, 1);
26697 if (shared_member_p (mem))
26698 return false;
26699
26700 tree cur = current_nonlambda_class_type ();
26701 if (!any_dependent_bases_p (cur))
26702 return false;
26703
26704 tree ctx = TREE_OPERAND (t, 0);
26705 if (DERIVED_FROM_P (ctx, cur))
26706 return false;
26707
26708 return true;
26709 }
26710
26711 /* T is a SCOPE_REF; return whether we need to consider it
26712 instantiation-dependent so that we can check access at instantiation
26713 time even though we know which member it resolves to. */
26714
26715 static bool
26716 instantiation_dependent_scope_ref_p (tree t)
26717 {
26718 if (DECL_P (TREE_OPERAND (t, 1))
26719 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26720 && !unknown_base_ref_p (t)
26721 && accessible_in_template_p (TREE_OPERAND (t, 0),
26722 TREE_OPERAND (t, 1)))
26723 return false;
26724 else
26725 return true;
26726 }
26727
26728 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26729 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26730 expression. */
26731
26732 /* Note that this predicate is not appropriate for general expressions;
26733 only constant expressions (that satisfy potential_constant_expression)
26734 can be tested for value dependence. */
26735
26736 bool
26737 value_dependent_expression_p (tree expression)
26738 {
26739 if (!processing_template_decl || expression == NULL_TREE)
26740 return false;
26741
26742 /* A type-dependent expression is also value-dependent. */
26743 if (type_dependent_expression_p (expression))
26744 return true;
26745
26746 switch (TREE_CODE (expression))
26747 {
26748 case BASELINK:
26749 /* A dependent member function of the current instantiation. */
26750 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26751
26752 case FUNCTION_DECL:
26753 /* A dependent member function of the current instantiation. */
26754 if (DECL_CLASS_SCOPE_P (expression)
26755 && dependent_type_p (DECL_CONTEXT (expression)))
26756 return true;
26757 break;
26758
26759 case IDENTIFIER_NODE:
26760 /* A name that has not been looked up -- must be dependent. */
26761 return true;
26762
26763 case TEMPLATE_PARM_INDEX:
26764 /* A non-type template parm. */
26765 return true;
26766
26767 case CONST_DECL:
26768 /* A non-type template parm. */
26769 if (DECL_TEMPLATE_PARM_P (expression))
26770 return true;
26771 return value_dependent_expression_p (DECL_INITIAL (expression));
26772
26773 case VAR_DECL:
26774 /* A constant with literal type and is initialized
26775 with an expression that is value-dependent. */
26776 if (DECL_DEPENDENT_INIT_P (expression)
26777 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26778 || TYPE_REF_P (TREE_TYPE (expression)))
26779 return true;
26780 if (DECL_HAS_VALUE_EXPR_P (expression))
26781 {
26782 tree value_expr = DECL_VALUE_EXPR (expression);
26783 if (value_dependent_expression_p (value_expr)
26784 /* __PRETTY_FUNCTION__ inside a template function is dependent
26785 on the name of the function. */
26786 || (DECL_PRETTY_FUNCTION_P (expression)
26787 /* It might be used in a template, but not a template
26788 function, in which case its DECL_VALUE_EXPR will be
26789 "top level". */
26790 && value_expr == error_mark_node))
26791 return true;
26792 }
26793 return false;
26794
26795 case DYNAMIC_CAST_EXPR:
26796 case STATIC_CAST_EXPR:
26797 case CONST_CAST_EXPR:
26798 case REINTERPRET_CAST_EXPR:
26799 case CAST_EXPR:
26800 case IMPLICIT_CONV_EXPR:
26801 /* These expressions are value-dependent if the type to which
26802 the cast occurs is dependent or the expression being casted
26803 is value-dependent. */
26804 {
26805 tree type = TREE_TYPE (expression);
26806
26807 if (dependent_type_p (type))
26808 return true;
26809
26810 /* A functional cast has a list of operands. */
26811 expression = TREE_OPERAND (expression, 0);
26812 if (!expression)
26813 {
26814 /* If there are no operands, it must be an expression such
26815 as "int()". This should not happen for aggregate types
26816 because it would form non-constant expressions. */
26817 gcc_assert (cxx_dialect >= cxx11
26818 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26819
26820 return false;
26821 }
26822
26823 if (TREE_CODE (expression) == TREE_LIST)
26824 return any_value_dependent_elements_p (expression);
26825
26826 return value_dependent_expression_p (expression);
26827 }
26828
26829 case SIZEOF_EXPR:
26830 if (SIZEOF_EXPR_TYPE_P (expression))
26831 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26832 /* FALLTHRU */
26833 case ALIGNOF_EXPR:
26834 case TYPEID_EXPR:
26835 /* A `sizeof' expression is value-dependent if the operand is
26836 type-dependent or is a pack expansion. */
26837 expression = TREE_OPERAND (expression, 0);
26838 if (PACK_EXPANSION_P (expression))
26839 return true;
26840 else if (TYPE_P (expression))
26841 return dependent_type_p (expression);
26842 return instantiation_dependent_uneval_expression_p (expression);
26843
26844 case AT_ENCODE_EXPR:
26845 /* An 'encode' expression is value-dependent if the operand is
26846 type-dependent. */
26847 expression = TREE_OPERAND (expression, 0);
26848 return dependent_type_p (expression);
26849
26850 case NOEXCEPT_EXPR:
26851 expression = TREE_OPERAND (expression, 0);
26852 return instantiation_dependent_uneval_expression_p (expression);
26853
26854 case SCOPE_REF:
26855 /* All instantiation-dependent expressions should also be considered
26856 value-dependent. */
26857 return instantiation_dependent_scope_ref_p (expression);
26858
26859 case COMPONENT_REF:
26860 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26861 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26862
26863 case NONTYPE_ARGUMENT_PACK:
26864 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26865 is value-dependent. */
26866 {
26867 tree values = ARGUMENT_PACK_ARGS (expression);
26868 int i, len = TREE_VEC_LENGTH (values);
26869
26870 for (i = 0; i < len; ++i)
26871 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26872 return true;
26873
26874 return false;
26875 }
26876
26877 case TRAIT_EXPR:
26878 {
26879 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26880
26881 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26882 return true;
26883
26884 if (!type2)
26885 return false;
26886
26887 if (TREE_CODE (type2) != TREE_LIST)
26888 return dependent_type_p (type2);
26889
26890 for (; type2; type2 = TREE_CHAIN (type2))
26891 if (dependent_type_p (TREE_VALUE (type2)))
26892 return true;
26893
26894 return false;
26895 }
26896
26897 case MODOP_EXPR:
26898 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26899 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26900
26901 case ARRAY_REF:
26902 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26903 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26904
26905 case ADDR_EXPR:
26906 {
26907 tree op = TREE_OPERAND (expression, 0);
26908 return (value_dependent_expression_p (op)
26909 || has_value_dependent_address (op));
26910 }
26911
26912 case REQUIRES_EXPR:
26913 /* Treat all requires-expressions as value-dependent so
26914 we don't try to fold them. */
26915 return true;
26916
26917 case TYPE_REQ:
26918 return dependent_type_p (TREE_OPERAND (expression, 0));
26919
26920 case CALL_EXPR:
26921 {
26922 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26923 return true;
26924 tree fn = get_callee_fndecl (expression);
26925 int i, nargs;
26926 nargs = call_expr_nargs (expression);
26927 for (i = 0; i < nargs; ++i)
26928 {
26929 tree op = CALL_EXPR_ARG (expression, i);
26930 /* In a call to a constexpr member function, look through the
26931 implicit ADDR_EXPR on the object argument so that it doesn't
26932 cause the call to be considered value-dependent. We also
26933 look through it in potential_constant_expression. */
26934 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26935 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26936 && TREE_CODE (op) == ADDR_EXPR)
26937 op = TREE_OPERAND (op, 0);
26938 if (value_dependent_expression_p (op))
26939 return true;
26940 }
26941 return false;
26942 }
26943
26944 case TEMPLATE_ID_EXPR:
26945 return concept_definition_p (TREE_OPERAND (expression, 0));
26946
26947 case CONSTRUCTOR:
26948 {
26949 unsigned ix;
26950 tree val;
26951 if (dependent_type_p (TREE_TYPE (expression)))
26952 return true;
26953 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26954 if (value_dependent_expression_p (val))
26955 return true;
26956 return false;
26957 }
26958
26959 case STMT_EXPR:
26960 /* Treat a GNU statement expression as dependent to avoid crashing
26961 under instantiate_non_dependent_expr; it can't be constant. */
26962 return true;
26963
26964 default:
26965 /* A constant expression is value-dependent if any subexpression is
26966 value-dependent. */
26967 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26968 {
26969 case tcc_reference:
26970 case tcc_unary:
26971 case tcc_comparison:
26972 case tcc_binary:
26973 case tcc_expression:
26974 case tcc_vl_exp:
26975 {
26976 int i, len = cp_tree_operand_length (expression);
26977
26978 for (i = 0; i < len; i++)
26979 {
26980 tree t = TREE_OPERAND (expression, i);
26981
26982 /* In some cases, some of the operands may be missing.
26983 (For example, in the case of PREDECREMENT_EXPR, the
26984 amount to increment by may be missing.) That doesn't
26985 make the expression dependent. */
26986 if (t && value_dependent_expression_p (t))
26987 return true;
26988 }
26989 }
26990 break;
26991 default:
26992 break;
26993 }
26994 break;
26995 }
26996
26997 /* The expression is not value-dependent. */
26998 return false;
26999 }
27000
27001 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27002 [temp.dep.expr]. Note that an expression with no type is
27003 considered dependent. Other parts of the compiler arrange for an
27004 expression with type-dependent subexpressions to have no type, so
27005 this function doesn't have to be fully recursive. */
27006
27007 bool
27008 type_dependent_expression_p (tree expression)
27009 {
27010 if (!processing_template_decl)
27011 return false;
27012
27013 if (expression == NULL_TREE || expression == error_mark_node)
27014 return false;
27015
27016 STRIP_ANY_LOCATION_WRAPPER (expression);
27017
27018 /* An unresolved name is always dependent. */
27019 if (identifier_p (expression)
27020 || TREE_CODE (expression) == USING_DECL
27021 || TREE_CODE (expression) == WILDCARD_DECL)
27022 return true;
27023
27024 /* A lambda-expression in template context is dependent. dependent_type_p is
27025 true for a lambda in the scope of a class or function template, but that
27026 doesn't cover all template contexts, like a default template argument. */
27027 if (TREE_CODE (expression) == LAMBDA_EXPR)
27028 return true;
27029
27030 /* A fold expression is type-dependent. */
27031 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27032 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27033 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27034 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27035 return true;
27036
27037 /* Some expression forms are never type-dependent. */
27038 if (TREE_CODE (expression) == SIZEOF_EXPR
27039 || TREE_CODE (expression) == ALIGNOF_EXPR
27040 || TREE_CODE (expression) == AT_ENCODE_EXPR
27041 || TREE_CODE (expression) == NOEXCEPT_EXPR
27042 || TREE_CODE (expression) == TRAIT_EXPR
27043 || TREE_CODE (expression) == TYPEID_EXPR
27044 || TREE_CODE (expression) == DELETE_EXPR
27045 || TREE_CODE (expression) == VEC_DELETE_EXPR
27046 || TREE_CODE (expression) == THROW_EXPR
27047 || TREE_CODE (expression) == REQUIRES_EXPR)
27048 return false;
27049
27050 /* The types of these expressions depends only on the type to which
27051 the cast occurs. */
27052 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27053 || TREE_CODE (expression) == STATIC_CAST_EXPR
27054 || TREE_CODE (expression) == CONST_CAST_EXPR
27055 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27056 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27057 || TREE_CODE (expression) == CAST_EXPR)
27058 return dependent_type_p (TREE_TYPE (expression));
27059
27060 /* The types of these expressions depends only on the type created
27061 by the expression. */
27062 if (TREE_CODE (expression) == NEW_EXPR
27063 || TREE_CODE (expression) == VEC_NEW_EXPR)
27064 {
27065 /* For NEW_EXPR tree nodes created inside a template, either
27066 the object type itself or a TREE_LIST may appear as the
27067 operand 1. */
27068 tree type = TREE_OPERAND (expression, 1);
27069 if (TREE_CODE (type) == TREE_LIST)
27070 /* This is an array type. We need to check array dimensions
27071 as well. */
27072 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27073 || value_dependent_expression_p
27074 (TREE_OPERAND (TREE_VALUE (type), 1));
27075 /* Array type whose dimension has to be deduced. */
27076 else if (TREE_CODE (type) == ARRAY_TYPE
27077 && TREE_OPERAND (expression, 2) == NULL_TREE)
27078 return true;
27079 else
27080 return dependent_type_p (type);
27081 }
27082
27083 if (TREE_CODE (expression) == SCOPE_REF)
27084 {
27085 tree scope = TREE_OPERAND (expression, 0);
27086 tree name = TREE_OPERAND (expression, 1);
27087
27088 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27089 contains an identifier associated by name lookup with one or more
27090 declarations declared with a dependent type, or...a
27091 nested-name-specifier or qualified-id that names a member of an
27092 unknown specialization. */
27093 return (type_dependent_expression_p (name)
27094 || dependent_scope_p (scope));
27095 }
27096
27097 if (TREE_CODE (expression) == TEMPLATE_DECL
27098 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27099 return uses_outer_template_parms (expression);
27100
27101 if (TREE_CODE (expression) == STMT_EXPR)
27102 expression = stmt_expr_value_expr (expression);
27103
27104 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27105 {
27106 tree elt;
27107 unsigned i;
27108
27109 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27110 {
27111 if (type_dependent_expression_p (elt))
27112 return true;
27113 }
27114 return false;
27115 }
27116
27117 /* A static data member of the current instantiation with incomplete
27118 array type is type-dependent, as the definition and specializations
27119 can have different bounds. */
27120 if (VAR_P (expression)
27121 && DECL_CLASS_SCOPE_P (expression)
27122 && dependent_type_p (DECL_CONTEXT (expression))
27123 && VAR_HAD_UNKNOWN_BOUND (expression))
27124 return true;
27125
27126 /* An array of unknown bound depending on a variadic parameter, eg:
27127
27128 template<typename... Args>
27129 void foo (Args... args)
27130 {
27131 int arr[] = { args... };
27132 }
27133
27134 template<int... vals>
27135 void bar ()
27136 {
27137 int arr[] = { vals... };
27138 }
27139
27140 If the array has no length and has an initializer, it must be that
27141 we couldn't determine its length in cp_complete_array_type because
27142 it is dependent. */
27143 if (VAR_P (expression)
27144 && TREE_TYPE (expression) != NULL_TREE
27145 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27146 && !TYPE_DOMAIN (TREE_TYPE (expression))
27147 && DECL_INITIAL (expression))
27148 return true;
27149
27150 /* A function or variable template-id is type-dependent if it has any
27151 dependent template arguments. */
27152 if (VAR_OR_FUNCTION_DECL_P (expression)
27153 && DECL_LANG_SPECIFIC (expression)
27154 && DECL_TEMPLATE_INFO (expression))
27155 {
27156 /* Consider the innermost template arguments, since those are the ones
27157 that come from the template-id; the template arguments for the
27158 enclosing class do not make it type-dependent unless they are used in
27159 the type of the decl. */
27160 if (instantiates_primary_template_p (expression)
27161 && (any_dependent_template_arguments_p
27162 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27163 return true;
27164 }
27165
27166 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27167 type-dependent. Checking this is important for functions with auto return
27168 type, which looks like a dependent type. */
27169 if (TREE_CODE (expression) == FUNCTION_DECL
27170 && !(DECL_CLASS_SCOPE_P (expression)
27171 && dependent_type_p (DECL_CONTEXT (expression)))
27172 && !(DECL_LANG_SPECIFIC (expression)
27173 && DECL_UNIQUE_FRIEND_P (expression)
27174 && (!DECL_FRIEND_CONTEXT (expression)
27175 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27176 && !DECL_LOCAL_DECL_P (expression))
27177 {
27178 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27179 || undeduced_auto_decl (expression));
27180 return false;
27181 }
27182
27183 /* Always dependent, on the number of arguments if nothing else. */
27184 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27185 return true;
27186
27187 if (TREE_TYPE (expression) == unknown_type_node)
27188 {
27189 if (TREE_CODE (expression) == ADDR_EXPR)
27190 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27191 if (TREE_CODE (expression) == COMPONENT_REF
27192 || TREE_CODE (expression) == OFFSET_REF)
27193 {
27194 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27195 return true;
27196 expression = TREE_OPERAND (expression, 1);
27197 if (identifier_p (expression))
27198 return false;
27199 }
27200 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27201 if (TREE_CODE (expression) == SCOPE_REF)
27202 return false;
27203
27204 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27205 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27206 || TREE_CODE (expression) == CO_YIELD_EXPR)
27207 return true;
27208
27209 if (BASELINK_P (expression))
27210 {
27211 if (BASELINK_OPTYPE (expression)
27212 && dependent_type_p (BASELINK_OPTYPE (expression)))
27213 return true;
27214 expression = BASELINK_FUNCTIONS (expression);
27215 }
27216
27217 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27218 {
27219 if (any_dependent_template_arguments_p
27220 (TREE_OPERAND (expression, 1)))
27221 return true;
27222 expression = TREE_OPERAND (expression, 0);
27223 if (identifier_p (expression))
27224 return true;
27225 }
27226
27227 gcc_assert (OVL_P (expression));
27228
27229 for (lkp_iterator iter (expression); iter; ++iter)
27230 if (type_dependent_expression_p (*iter))
27231 return true;
27232
27233 return false;
27234 }
27235
27236 /* The type of a non-type template parm declared with a placeholder type
27237 depends on the corresponding template argument, even though
27238 placeholders are not normally considered dependent. */
27239 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27240 && is_auto (TREE_TYPE (expression)))
27241 return true;
27242
27243 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27244
27245 /* Dependent type attributes might not have made it from the decl to
27246 the type yet. */
27247 if (DECL_P (expression)
27248 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27249 return true;
27250
27251 return (dependent_type_p (TREE_TYPE (expression)));
27252 }
27253
27254 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27255 type-dependent if the expression refers to a member of the current
27256 instantiation and the type of the referenced member is dependent, or the
27257 class member access expression refers to a member of an unknown
27258 specialization.
27259
27260 This function returns true if the OBJECT in such a class member access
27261 expression is of an unknown specialization. */
27262
27263 bool
27264 type_dependent_object_expression_p (tree object)
27265 {
27266 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27267 dependent. */
27268 if (TREE_CODE (object) == IDENTIFIER_NODE)
27269 return true;
27270 tree scope = TREE_TYPE (object);
27271 return (!scope || dependent_scope_p (scope));
27272 }
27273
27274 /* walk_tree callback function for instantiation_dependent_expression_p,
27275 below. Returns non-zero if a dependent subexpression is found. */
27276
27277 static tree
27278 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27279 void * /*data*/)
27280 {
27281 if (TYPE_P (*tp))
27282 {
27283 /* We don't have to worry about decltype currently because decltype
27284 of an instantiation-dependent expr is a dependent type. This
27285 might change depending on the resolution of DR 1172. */
27286 *walk_subtrees = false;
27287 return NULL_TREE;
27288 }
27289 enum tree_code code = TREE_CODE (*tp);
27290 switch (code)
27291 {
27292 /* Don't treat an argument list as dependent just because it has no
27293 TREE_TYPE. */
27294 case TREE_LIST:
27295 case TREE_VEC:
27296 case NONTYPE_ARGUMENT_PACK:
27297 return NULL_TREE;
27298
27299 case TEMPLATE_PARM_INDEX:
27300 if (dependent_type_p (TREE_TYPE (*tp)))
27301 return *tp;
27302 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27303 return *tp;
27304 /* We'll check value-dependence separately. */
27305 return NULL_TREE;
27306
27307 /* Handle expressions with type operands. */
27308 case SIZEOF_EXPR:
27309 case ALIGNOF_EXPR:
27310 case TYPEID_EXPR:
27311 case AT_ENCODE_EXPR:
27312 {
27313 tree op = TREE_OPERAND (*tp, 0);
27314 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27315 op = TREE_TYPE (op);
27316 if (TYPE_P (op))
27317 {
27318 if (dependent_type_p (op))
27319 return *tp;
27320 else
27321 {
27322 *walk_subtrees = false;
27323 return NULL_TREE;
27324 }
27325 }
27326 break;
27327 }
27328
27329 case COMPONENT_REF:
27330 if (identifier_p (TREE_OPERAND (*tp, 1)))
27331 /* In a template, finish_class_member_access_expr creates a
27332 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27333 type-dependent, so that we can check access control at
27334 instantiation time (PR 42277). See also Core issue 1273. */
27335 return *tp;
27336 break;
27337
27338 case SCOPE_REF:
27339 if (instantiation_dependent_scope_ref_p (*tp))
27340 return *tp;
27341 else
27342 break;
27343
27344 /* Treat statement-expressions as dependent. */
27345 case BIND_EXPR:
27346 return *tp;
27347
27348 /* Treat requires-expressions as dependent. */
27349 case REQUIRES_EXPR:
27350 return *tp;
27351
27352 case CALL_EXPR:
27353 /* Treat concept checks as dependent. */
27354 if (concept_check_p (*tp))
27355 return *tp;
27356 break;
27357
27358 case TEMPLATE_ID_EXPR:
27359 /* Treat concept checks as dependent. */
27360 if (concept_check_p (*tp))
27361 return *tp;
27362 break;
27363
27364 case CONSTRUCTOR:
27365 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27366 return *tp;
27367 break;
27368
27369 default:
27370 break;
27371 }
27372
27373 if (type_dependent_expression_p (*tp))
27374 return *tp;
27375 else
27376 return NULL_TREE;
27377 }
27378
27379 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27380 sense defined by the ABI:
27381
27382 "An expression is instantiation-dependent if it is type-dependent
27383 or value-dependent, or it has a subexpression that is type-dependent
27384 or value-dependent."
27385
27386 Except don't actually check value-dependence for unevaluated expressions,
27387 because in sizeof(i) we don't care about the value of i. Checking
27388 type-dependence will in turn check value-dependence of array bounds/template
27389 arguments as needed. */
27390
27391 bool
27392 instantiation_dependent_uneval_expression_p (tree expression)
27393 {
27394 tree result;
27395
27396 if (!processing_template_decl)
27397 return false;
27398
27399 if (expression == error_mark_node)
27400 return false;
27401
27402 result = cp_walk_tree_without_duplicates (&expression,
27403 instantiation_dependent_r, NULL);
27404 return result != NULL_TREE;
27405 }
27406
27407 /* As above, but also check value-dependence of the expression as a whole. */
27408
27409 bool
27410 instantiation_dependent_expression_p (tree expression)
27411 {
27412 return (instantiation_dependent_uneval_expression_p (expression)
27413 || (potential_constant_expression (expression)
27414 && value_dependent_expression_p (expression)));
27415 }
27416
27417 /* Like type_dependent_expression_p, but it also works while not processing
27418 a template definition, i.e. during substitution or mangling. */
27419
27420 bool
27421 type_dependent_expression_p_push (tree expr)
27422 {
27423 bool b;
27424 ++processing_template_decl;
27425 b = type_dependent_expression_p (expr);
27426 --processing_template_decl;
27427 return b;
27428 }
27429
27430 /* Returns TRUE if ARGS contains a type-dependent expression. */
27431
27432 bool
27433 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27434 {
27435 unsigned int i;
27436 tree arg;
27437
27438 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27439 {
27440 if (type_dependent_expression_p (arg))
27441 return true;
27442 }
27443 return false;
27444 }
27445
27446 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27447 expressions) contains any type-dependent expressions. */
27448
27449 bool
27450 any_type_dependent_elements_p (const_tree list)
27451 {
27452 for (; list; list = TREE_CHAIN (list))
27453 if (type_dependent_expression_p (TREE_VALUE (list)))
27454 return true;
27455
27456 return false;
27457 }
27458
27459 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27460 expressions) contains any value-dependent expressions. */
27461
27462 bool
27463 any_value_dependent_elements_p (const_tree list)
27464 {
27465 for (; list; list = TREE_CHAIN (list))
27466 if (value_dependent_expression_p (TREE_VALUE (list)))
27467 return true;
27468
27469 return false;
27470 }
27471
27472 /* Returns TRUE if the ARG (a template argument) is dependent. */
27473
27474 bool
27475 dependent_template_arg_p (tree arg)
27476 {
27477 if (!processing_template_decl)
27478 return false;
27479
27480 /* Assume a template argument that was wrongly written by the user
27481 is dependent. This is consistent with what
27482 any_dependent_template_arguments_p [that calls this function]
27483 does. */
27484 if (!arg || arg == error_mark_node)
27485 return true;
27486
27487 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27488 arg = argument_pack_select_arg (arg);
27489
27490 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27491 return true;
27492 if (TREE_CODE (arg) == TEMPLATE_DECL)
27493 {
27494 if (DECL_TEMPLATE_PARM_P (arg))
27495 return true;
27496 /* A member template of a dependent class is not necessarily
27497 type-dependent, but it is a dependent template argument because it
27498 will be a member of an unknown specialization to that template. */
27499 tree scope = CP_DECL_CONTEXT (arg);
27500 return TYPE_P (scope) && dependent_type_p (scope);
27501 }
27502 else if (ARGUMENT_PACK_P (arg))
27503 {
27504 tree args = ARGUMENT_PACK_ARGS (arg);
27505 int i, len = TREE_VEC_LENGTH (args);
27506 for (i = 0; i < len; ++i)
27507 {
27508 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27509 return true;
27510 }
27511
27512 return false;
27513 }
27514 else if (TYPE_P (arg))
27515 return dependent_type_p (arg);
27516 else
27517 return value_dependent_expression_p (arg);
27518 }
27519
27520 /* Returns true if ARGS (a collection of template arguments) contains
27521 any types that require structural equality testing. */
27522
27523 bool
27524 any_template_arguments_need_structural_equality_p (tree args)
27525 {
27526 int i;
27527 int j;
27528
27529 if (!args)
27530 return false;
27531 if (args == error_mark_node)
27532 return true;
27533
27534 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27535 {
27536 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27537 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27538 {
27539 tree arg = TREE_VEC_ELT (level, j);
27540 tree packed_args = NULL_TREE;
27541 int k, len = 1;
27542
27543 if (ARGUMENT_PACK_P (arg))
27544 {
27545 /* Look inside the argument pack. */
27546 packed_args = ARGUMENT_PACK_ARGS (arg);
27547 len = TREE_VEC_LENGTH (packed_args);
27548 }
27549
27550 for (k = 0; k < len; ++k)
27551 {
27552 if (packed_args)
27553 arg = TREE_VEC_ELT (packed_args, k);
27554
27555 if (error_operand_p (arg))
27556 return true;
27557 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27558 continue;
27559 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27560 return true;
27561 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27562 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27563 return true;
27564 }
27565 }
27566 }
27567
27568 return false;
27569 }
27570
27571 /* Returns true if ARGS (a collection of template arguments) contains
27572 any dependent arguments. */
27573
27574 bool
27575 any_dependent_template_arguments_p (const_tree args)
27576 {
27577 int i;
27578 int j;
27579
27580 if (!args)
27581 return false;
27582 if (args == error_mark_node)
27583 return true;
27584
27585 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27586 {
27587 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27588 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27589 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27590 return true;
27591 }
27592
27593 return false;
27594 }
27595
27596 /* Returns true if ARGS contains any errors. */
27597
27598 bool
27599 any_erroneous_template_args_p (const_tree args)
27600 {
27601 int i;
27602 int j;
27603
27604 if (args == error_mark_node)
27605 return true;
27606
27607 if (args && TREE_CODE (args) != TREE_VEC)
27608 {
27609 if (tree ti = get_template_info (args))
27610 args = TI_ARGS (ti);
27611 else
27612 args = NULL_TREE;
27613 }
27614
27615 if (!args)
27616 return false;
27617
27618 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27619 {
27620 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27621 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27622 if (error_operand_p (TREE_VEC_ELT (level, j)))
27623 return true;
27624 }
27625
27626 return false;
27627 }
27628
27629 /* Returns TRUE if the template TMPL is type-dependent. */
27630
27631 bool
27632 dependent_template_p (tree tmpl)
27633 {
27634 if (TREE_CODE (tmpl) == OVERLOAD)
27635 {
27636 for (lkp_iterator iter (tmpl); iter; ++iter)
27637 if (dependent_template_p (*iter))
27638 return true;
27639 return false;
27640 }
27641
27642 /* Template template parameters are dependent. */
27643 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27644 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27645 return true;
27646 /* So are names that have not been looked up. */
27647 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27648 return true;
27649 return false;
27650 }
27651
27652 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27653
27654 bool
27655 dependent_template_id_p (tree tmpl, tree args)
27656 {
27657 return (dependent_template_p (tmpl)
27658 || any_dependent_template_arguments_p (args));
27659 }
27660
27661 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27662 are dependent. */
27663
27664 bool
27665 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27666 {
27667 int i;
27668
27669 if (!processing_template_decl)
27670 return false;
27671
27672 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27673 {
27674 tree decl = TREE_VEC_ELT (declv, i);
27675 tree init = TREE_VEC_ELT (initv, i);
27676 tree cond = TREE_VEC_ELT (condv, i);
27677 tree incr = TREE_VEC_ELT (incrv, i);
27678
27679 if (type_dependent_expression_p (decl)
27680 || TREE_CODE (decl) == SCOPE_REF)
27681 return true;
27682
27683 if (init && type_dependent_expression_p (init))
27684 return true;
27685
27686 if (cond == global_namespace)
27687 return true;
27688
27689 if (type_dependent_expression_p (cond))
27690 return true;
27691
27692 if (COMPARISON_CLASS_P (cond)
27693 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27694 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27695 return true;
27696
27697 if (TREE_CODE (incr) == MODOP_EXPR)
27698 {
27699 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27700 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27701 return true;
27702 }
27703 else if (type_dependent_expression_p (incr))
27704 return true;
27705 else if (TREE_CODE (incr) == MODIFY_EXPR)
27706 {
27707 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27708 return true;
27709 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27710 {
27711 tree t = TREE_OPERAND (incr, 1);
27712 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27713 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27714 return true;
27715
27716 /* If this loop has a class iterator with != comparison
27717 with increment other than i++/++i/i--/--i, make sure the
27718 increment is constant. */
27719 if (CLASS_TYPE_P (TREE_TYPE (decl))
27720 && TREE_CODE (cond) == NE_EXPR)
27721 {
27722 if (TREE_OPERAND (t, 0) == decl)
27723 t = TREE_OPERAND (t, 1);
27724 else
27725 t = TREE_OPERAND (t, 0);
27726 if (TREE_CODE (t) != INTEGER_CST)
27727 return true;
27728 }
27729 }
27730 }
27731 }
27732
27733 return false;
27734 }
27735
27736 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27737 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27738 no such TYPE can be found. Note that this function peers inside
27739 uninstantiated templates and therefore should be used only in
27740 extremely limited situations. ONLY_CURRENT_P restricts this
27741 peering to the currently open classes hierarchy (which is required
27742 when comparing types). */
27743
27744 tree
27745 resolve_typename_type (tree type, bool only_current_p)
27746 {
27747 tree scope;
27748 tree name;
27749 tree decl;
27750 int quals;
27751 tree pushed_scope;
27752 tree result;
27753
27754 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27755
27756 scope = TYPE_CONTEXT (type);
27757 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27758 gcc_checking_assert (uses_template_parms (scope));
27759
27760 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27761 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27762 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27763 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27764 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27765 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27766 the TYPENAME_TYPE instead, we avoid messing up with a possible
27767 typedef variant case. */
27768 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27769
27770 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27771 it first before we can figure out what NAME refers to. */
27772 if (TREE_CODE (scope) == TYPENAME_TYPE)
27773 {
27774 if (TYPENAME_IS_RESOLVING_P (scope))
27775 /* Given a class template A with a dependent base with nested type C,
27776 typedef typename A::C::C C will land us here, as trying to resolve
27777 the initial A::C leads to the local C typedef, which leads back to
27778 A::C::C. So we break the recursion now. */
27779 return type;
27780 else
27781 scope = resolve_typename_type (scope, only_current_p);
27782 }
27783 /* If we don't know what SCOPE refers to, then we cannot resolve the
27784 TYPENAME_TYPE. */
27785 if (!CLASS_TYPE_P (scope))
27786 return type;
27787 /* If this is a typedef, we don't want to look inside (c++/11987). */
27788 if (typedef_variant_p (type))
27789 return type;
27790 /* If SCOPE isn't the template itself, it will not have a valid
27791 TYPE_FIELDS list. */
27792 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27793 /* scope is either the template itself or a compatible instantiation
27794 like X<T>, so look up the name in the original template. */
27795 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27796 /* If scope has no fields, it can't be a current instantiation. Check this
27797 before currently_open_class to avoid infinite recursion (71515). */
27798 if (!TYPE_FIELDS (scope))
27799 return type;
27800 /* If the SCOPE is not the current instantiation, there's no reason
27801 to look inside it. */
27802 if (only_current_p && !currently_open_class (scope))
27803 return type;
27804 /* Enter the SCOPE so that name lookup will be resolved as if we
27805 were in the class definition. In particular, SCOPE will no
27806 longer be considered a dependent type. */
27807 pushed_scope = push_scope (scope);
27808 /* Look up the declaration. */
27809 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27810 tf_warning_or_error);
27811
27812 result = NULL_TREE;
27813
27814 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27815 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27816 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27817 if (!decl)
27818 /*nop*/;
27819 else if (identifier_p (fullname)
27820 && TREE_CODE (decl) == TYPE_DECL)
27821 {
27822 result = TREE_TYPE (decl);
27823 if (result == error_mark_node)
27824 result = NULL_TREE;
27825 }
27826 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27827 && DECL_CLASS_TEMPLATE_P (decl))
27828 {
27829 /* Obtain the template and the arguments. */
27830 tree tmpl = TREE_OPERAND (fullname, 0);
27831 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27832 {
27833 /* We get here with a plain identifier because a previous tentative
27834 parse of the nested-name-specifier as part of a ptr-operator saw
27835 ::template X<A>. The use of ::template is necessary in a
27836 ptr-operator, but wrong in a declarator-id.
27837
27838 [temp.names]: In a qualified-id of a declarator-id, the keyword
27839 template shall not appear at the top level. */
27840 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27841 "keyword %<template%> not allowed in declarator-id");
27842 tmpl = decl;
27843 }
27844 tree args = TREE_OPERAND (fullname, 1);
27845 /* Instantiate the template. */
27846 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27847 /*entering_scope=*/true,
27848 tf_error | tf_user);
27849 if (result == error_mark_node)
27850 result = NULL_TREE;
27851 }
27852
27853 /* Leave the SCOPE. */
27854 if (pushed_scope)
27855 pop_scope (pushed_scope);
27856
27857 /* If we failed to resolve it, return the original typename. */
27858 if (!result)
27859 return type;
27860
27861 /* If lookup found a typename type, resolve that too. */
27862 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27863 {
27864 /* Ill-formed programs can cause infinite recursion here, so we
27865 must catch that. */
27866 TYPENAME_IS_RESOLVING_P (result) = 1;
27867 result = resolve_typename_type (result, only_current_p);
27868 TYPENAME_IS_RESOLVING_P (result) = 0;
27869 }
27870
27871 /* Qualify the resulting type. */
27872 quals = cp_type_quals (type);
27873 if (quals)
27874 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27875
27876 return result;
27877 }
27878
27879 /* EXPR is an expression which is not type-dependent. Return a proxy
27880 for EXPR that can be used to compute the types of larger
27881 expressions containing EXPR. */
27882
27883 tree
27884 build_non_dependent_expr (tree expr)
27885 {
27886 tree orig_expr = expr;
27887 tree inner_expr;
27888
27889 /* When checking, try to get a constant value for all non-dependent
27890 expressions in order to expose bugs in *_dependent_expression_p
27891 and constexpr. This can affect code generation, see PR70704, so
27892 only do this for -fchecking=2. */
27893 if (flag_checking > 1
27894 && cxx_dialect >= cxx11
27895 /* Don't do this during nsdmi parsing as it can lead to
27896 unexpected recursive instantiations. */
27897 && !parsing_nsdmi ()
27898 /* Don't do this during concept processing either and for
27899 the same reason. */
27900 && !processing_constraint_expression_p ())
27901 fold_non_dependent_expr (expr, tf_none);
27902
27903 STRIP_ANY_LOCATION_WRAPPER (expr);
27904
27905 /* Preserve OVERLOADs; the functions must be available to resolve
27906 types. */
27907 inner_expr = expr;
27908 if (TREE_CODE (inner_expr) == STMT_EXPR)
27909 inner_expr = stmt_expr_value_expr (inner_expr);
27910 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27911 inner_expr = TREE_OPERAND (inner_expr, 0);
27912 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27913 inner_expr = TREE_OPERAND (inner_expr, 1);
27914 if (is_overloaded_fn (inner_expr)
27915 || TREE_CODE (inner_expr) == OFFSET_REF)
27916 return orig_expr;
27917 /* There is no need to return a proxy for a variable or enumerator. */
27918 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27919 return orig_expr;
27920 /* Preserve string constants; conversions from string constants to
27921 "char *" are allowed, even though normally a "const char *"
27922 cannot be used to initialize a "char *". */
27923 if (TREE_CODE (expr) == STRING_CST)
27924 return orig_expr;
27925 /* Preserve void and arithmetic constants, as an optimization -- there is no
27926 reason to create a new node. */
27927 if (TREE_CODE (expr) == VOID_CST
27928 || TREE_CODE (expr) == INTEGER_CST
27929 || TREE_CODE (expr) == REAL_CST)
27930 return orig_expr;
27931 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27932 There is at least one place where we want to know that a
27933 particular expression is a throw-expression: when checking a ?:
27934 expression, there are special rules if the second or third
27935 argument is a throw-expression. */
27936 if (TREE_CODE (expr) == THROW_EXPR)
27937 return orig_expr;
27938
27939 /* Don't wrap an initializer list, we need to be able to look inside. */
27940 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27941 return orig_expr;
27942
27943 /* Don't wrap a dummy object, we need to be able to test for it. */
27944 if (is_dummy_object (expr))
27945 return orig_expr;
27946
27947 if (TREE_CODE (expr) == COND_EXPR)
27948 return build3 (COND_EXPR,
27949 TREE_TYPE (expr),
27950 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27951 (TREE_OPERAND (expr, 1)
27952 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27953 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27954 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27955 if (TREE_CODE (expr) == COMPOUND_EXPR
27956 && !COMPOUND_EXPR_OVERLOADED (expr))
27957 return build2 (COMPOUND_EXPR,
27958 TREE_TYPE (expr),
27959 TREE_OPERAND (expr, 0),
27960 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27961
27962 /* If the type is unknown, it can't really be non-dependent */
27963 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27964
27965 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27966 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27967 TREE_TYPE (expr), expr);
27968 }
27969
27970 /* ARGS is a vector of expressions as arguments to a function call.
27971 Replace the arguments with equivalent non-dependent expressions.
27972 This modifies ARGS in place. */
27973
27974 void
27975 make_args_non_dependent (vec<tree, va_gc> *args)
27976 {
27977 unsigned int ix;
27978 tree arg;
27979
27980 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27981 {
27982 tree newarg = build_non_dependent_expr (arg);
27983 if (newarg != arg)
27984 (*args)[ix] = newarg;
27985 }
27986 }
27987
27988 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27989 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27990 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27991
27992 static tree
27993 make_auto_1 (tree name, bool set_canonical)
27994 {
27995 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27996 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27997 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27998 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27999 (0, processing_template_decl + 1, processing_template_decl + 1,
28000 TYPE_NAME (au), NULL_TREE);
28001 if (set_canonical)
28002 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28003 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28004 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28005 if (name == decltype_auto_identifier)
28006 AUTO_IS_DECLTYPE (au) = true;
28007
28008 return au;
28009 }
28010
28011 tree
28012 make_decltype_auto (void)
28013 {
28014 return make_auto_1 (decltype_auto_identifier, true);
28015 }
28016
28017 tree
28018 make_auto (void)
28019 {
28020 return make_auto_1 (auto_identifier, true);
28021 }
28022
28023 /* Return a C++17 deduction placeholder for class template TMPL. */
28024
28025 tree
28026 make_template_placeholder (tree tmpl)
28027 {
28028 tree t = make_auto_1 (auto_identifier, false);
28029 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28030 /* Our canonical type depends on the placeholder. */
28031 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28032 return t;
28033 }
28034
28035 /* True iff T is a C++17 class template deduction placeholder. */
28036
28037 bool
28038 template_placeholder_p (tree t)
28039 {
28040 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28041 }
28042
28043 /* Make a "constrained auto" type-specifier. This is an auto or
28044 decltype(auto) type with constraints that must be associated after
28045 deduction. The constraint is formed from the given concept CON
28046 and its optional sequence of template arguments ARGS.
28047
28048 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28049
28050 static tree
28051 make_constrained_placeholder_type (tree type, tree con, tree args)
28052 {
28053 /* Build the constraint. */
28054 tree tmpl = DECL_TI_TEMPLATE (con);
28055 tree expr = tmpl;
28056 if (TREE_CODE (con) == FUNCTION_DECL)
28057 expr = ovl_make (tmpl);
28058 ++processing_template_decl;
28059 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28060 --processing_template_decl;
28061
28062 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
28063
28064 /* Our canonical type depends on the constraint. */
28065 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28066
28067 /* Attach the constraint to the type declaration. */
28068 return TYPE_NAME (type);
28069 }
28070
28071 /* Make a "constrained auto" type-specifier. */
28072
28073 tree
28074 make_constrained_auto (tree con, tree args)
28075 {
28076 tree type = make_auto_1 (auto_identifier, false);
28077 return make_constrained_placeholder_type (type, con, args);
28078 }
28079
28080 /* Make a "constrained decltype(auto)" type-specifier. */
28081
28082 tree
28083 make_constrained_decltype_auto (tree con, tree args)
28084 {
28085 tree type = make_auto_1 (decltype_auto_identifier, false);
28086 return make_constrained_placeholder_type (type, con, args);
28087 }
28088
28089 /* Build and return a concept definition. Like other templates, the
28090 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28091 the TEMPLATE_DECL. */
28092
28093 tree
28094 finish_concept_definition (cp_expr id, tree init)
28095 {
28096 gcc_assert (identifier_p (id));
28097 gcc_assert (processing_template_decl);
28098
28099 location_t loc = id.get_location();
28100
28101 /* A concept-definition shall not have associated constraints. */
28102 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28103 {
28104 error_at (loc, "a concept cannot be constrained");
28105 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28106 }
28107
28108 /* A concept-definition shall appear in namespace scope. Templates
28109 aren't allowed in block scope, so we only need to check for class
28110 scope. */
28111 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28112 {
28113 error_at (loc, "concept %qE not in namespace scope", *id);
28114 return error_mark_node;
28115 }
28116
28117 /* Initially build the concept declaration; its type is bool. */
28118 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28119 DECL_CONTEXT (decl) = current_scope ();
28120 DECL_INITIAL (decl) = init;
28121
28122 set_originating_module (decl, false);
28123
28124 /* Push the enclosing template. */
28125 return push_template_decl (decl);
28126 }
28127
28128 /* Given type ARG, return std::initializer_list<ARG>. */
28129
28130 static tree
28131 listify (tree arg)
28132 {
28133 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28134
28135 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28136 {
28137 gcc_rich_location richloc (input_location);
28138 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28139 error_at (&richloc,
28140 "deducing from brace-enclosed initializer list"
28141 " requires %<#include <initializer_list>%>");
28142
28143 return error_mark_node;
28144 }
28145 tree argvec = make_tree_vec (1);
28146 TREE_VEC_ELT (argvec, 0) = arg;
28147
28148 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28149 NULL_TREE, 0, tf_warning_or_error);
28150 }
28151
28152 /* Replace auto in TYPE with std::initializer_list<auto>. */
28153
28154 static tree
28155 listify_autos (tree type, tree auto_node)
28156 {
28157 tree init_auto = listify (strip_top_quals (auto_node));
28158 tree argvec = make_tree_vec (1);
28159 TREE_VEC_ELT (argvec, 0) = init_auto;
28160 if (processing_template_decl)
28161 argvec = add_to_template_args (current_template_args (), argvec);
28162 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28163 }
28164
28165 /* Hash traits for hashing possibly constrained 'auto'
28166 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28167
28168 struct auto_hash : default_hash_traits<tree>
28169 {
28170 static inline hashval_t hash (tree);
28171 static inline bool equal (tree, tree);
28172 };
28173
28174 /* Hash the 'auto' T. */
28175
28176 inline hashval_t
28177 auto_hash::hash (tree t)
28178 {
28179 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28180 /* Matching constrained-type-specifiers denote the same template
28181 parameter, so hash the constraint. */
28182 return hash_placeholder_constraint (c);
28183 else
28184 /* But unconstrained autos are all separate, so just hash the pointer. */
28185 return iterative_hash_object (t, 0);
28186 }
28187
28188 /* Compare two 'auto's. */
28189
28190 inline bool
28191 auto_hash::equal (tree t1, tree t2)
28192 {
28193 if (t1 == t2)
28194 return true;
28195
28196 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28197 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28198
28199 /* Two unconstrained autos are distinct. */
28200 if (!c1 || !c2)
28201 return false;
28202
28203 return equivalent_placeholder_constraints (c1, c2);
28204 }
28205
28206 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28207 constrained) auto, add it to the vector. */
28208
28209 static int
28210 extract_autos_r (tree t, void *data)
28211 {
28212 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28213 if (is_auto (t))
28214 {
28215 /* All the autos were built with index 0; fix that up now. */
28216 tree *p = hash.find_slot (t, INSERT);
28217 unsigned idx;
28218 if (*p)
28219 /* If this is a repeated constrained-type-specifier, use the index we
28220 chose before. */
28221 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28222 else
28223 {
28224 /* Otherwise this is new, so use the current count. */
28225 *p = t;
28226 idx = hash.elements () - 1;
28227 }
28228 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28229 }
28230
28231 /* Always keep walking. */
28232 return 0;
28233 }
28234
28235 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28236 says they can appear anywhere in the type. */
28237
28238 static tree
28239 extract_autos (tree type)
28240 {
28241 hash_set<tree> visited;
28242 hash_table<auto_hash> hash (2);
28243
28244 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28245
28246 tree tree_vec = make_tree_vec (hash.elements());
28247 for (hash_table<auto_hash>::iterator iter = hash.begin();
28248 iter != hash.end(); ++iter)
28249 {
28250 tree elt = *iter;
28251 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28252 TREE_VEC_ELT (tree_vec, i)
28253 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28254 }
28255
28256 return tree_vec;
28257 }
28258
28259 /* The stem for deduction guide names. */
28260 const char *const dguide_base = "__dguide_";
28261
28262 /* Return the name for a deduction guide for class template TMPL. */
28263
28264 tree
28265 dguide_name (tree tmpl)
28266 {
28267 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28268 tree tname = TYPE_IDENTIFIER (type);
28269 char *buf = (char *) alloca (1 + strlen (dguide_base)
28270 + IDENTIFIER_LENGTH (tname));
28271 memcpy (buf, dguide_base, strlen (dguide_base));
28272 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28273 IDENTIFIER_LENGTH (tname) + 1);
28274 tree dname = get_identifier (buf);
28275 TREE_TYPE (dname) = type;
28276 return dname;
28277 }
28278
28279 /* True if NAME is the name of a deduction guide. */
28280
28281 bool
28282 dguide_name_p (tree name)
28283 {
28284 return (TREE_CODE (name) == IDENTIFIER_NODE
28285 && TREE_TYPE (name)
28286 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28287 strlen (dguide_base)));
28288 }
28289
28290 /* True if FN is a deduction guide. */
28291
28292 bool
28293 deduction_guide_p (const_tree fn)
28294 {
28295 if (DECL_P (fn))
28296 if (tree name = DECL_NAME (fn))
28297 return dguide_name_p (name);
28298 return false;
28299 }
28300
28301 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28302
28303 bool
28304 copy_guide_p (const_tree fn)
28305 {
28306 gcc_assert (deduction_guide_p (fn));
28307 if (!DECL_ARTIFICIAL (fn))
28308 return false;
28309 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28310 return (TREE_CHAIN (parms) == void_list_node
28311 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28312 }
28313
28314 /* True if FN is a guide generated from a constructor template. */
28315
28316 bool
28317 template_guide_p (const_tree fn)
28318 {
28319 gcc_assert (deduction_guide_p (fn));
28320 if (!DECL_ARTIFICIAL (fn))
28321 return false;
28322 tree tmpl = DECL_TI_TEMPLATE (fn);
28323 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28324 return PRIMARY_TEMPLATE_P (org);
28325 return false;
28326 }
28327
28328 /* True if FN is an aggregate initialization guide or the copy deduction
28329 guide. */
28330
28331 bool
28332 builtin_guide_p (const_tree fn)
28333 {
28334 if (!deduction_guide_p (fn))
28335 return false;
28336 if (!DECL_ARTIFICIAL (fn))
28337 /* Explicitly declared. */
28338 return false;
28339 if (DECL_ABSTRACT_ORIGIN (fn))
28340 /* Derived from a constructor. */
28341 return false;
28342 return true;
28343 }
28344
28345 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28346 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28347 template parameter types. Note that the handling of template template
28348 parameters relies on current_template_parms being set appropriately for the
28349 new template. */
28350
28351 static tree
28352 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28353 tree tsubst_args, tsubst_flags_t complain)
28354 {
28355 if (olddecl == error_mark_node)
28356 return error_mark_node;
28357
28358 tree oldidx = get_template_parm_index (olddecl);
28359
28360 tree newtype;
28361 if (TREE_CODE (olddecl) == TYPE_DECL
28362 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28363 {
28364 tree oldtype = TREE_TYPE (olddecl);
28365 newtype = cxx_make_type (TREE_CODE (oldtype));
28366 TYPE_MAIN_VARIANT (newtype) = newtype;
28367 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28368 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28369 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28370 }
28371 else
28372 {
28373 newtype = TREE_TYPE (olddecl);
28374 if (type_uses_auto (newtype))
28375 {
28376 // Substitute once to fix references to other template parameters.
28377 newtype = tsubst (newtype, tsubst_args,
28378 complain|tf_partial, NULL_TREE);
28379 // Now substitute again to reduce the level of the auto.
28380 newtype = tsubst (newtype, current_template_args (),
28381 complain, NULL_TREE);
28382 }
28383 else
28384 newtype = tsubst (newtype, tsubst_args,
28385 complain, NULL_TREE);
28386 }
28387
28388 tree newdecl
28389 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28390 DECL_NAME (olddecl), newtype);
28391 SET_DECL_TEMPLATE_PARM_P (newdecl);
28392
28393 tree newidx;
28394 if (TREE_CODE (olddecl) == TYPE_DECL
28395 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28396 {
28397 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28398 = build_template_parm_index (index, level, level,
28399 newdecl, newtype);
28400 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28401 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28402 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28403 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28404 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28405 else
28406 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28407
28408 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28409 {
28410 DECL_TEMPLATE_RESULT (newdecl)
28411 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28412 DECL_NAME (olddecl), newtype);
28413 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28414 // First create a copy (ttargs) of tsubst_args with an
28415 // additional level for the template template parameter's own
28416 // template parameters (ttparms).
28417 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28418 (DECL_TEMPLATE_PARMS (olddecl)));
28419 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28420 tree ttargs = make_tree_vec (depth + 1);
28421 for (int i = 0; i < depth; ++i)
28422 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28423 TREE_VEC_ELT (ttargs, depth)
28424 = template_parms_level_to_args (ttparms);
28425 // Substitute ttargs into ttparms to fix references to
28426 // other template parameters.
28427 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28428 complain|tf_partial);
28429 // Now substitute again with args based on tparms, to reduce
28430 // the level of the ttparms.
28431 ttargs = current_template_args ();
28432 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28433 complain);
28434 // Finally, tack the adjusted parms onto tparms.
28435 ttparms = tree_cons (size_int (depth), ttparms,
28436 current_template_parms);
28437 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28438 }
28439 }
28440 else
28441 {
28442 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28443 tree newconst
28444 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28445 TREE_CODE (oldconst),
28446 DECL_NAME (oldconst), newtype);
28447 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28448 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28449 SET_DECL_TEMPLATE_PARM_P (newconst);
28450 newidx = build_template_parm_index (index, level, level,
28451 newconst, newtype);
28452 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28453 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28454 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28455 }
28456
28457 return newdecl;
28458 }
28459
28460 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28461 template parameter. */
28462
28463 static tree
28464 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28465 tree targs, unsigned targs_index, tsubst_flags_t complain)
28466 {
28467 tree olddecl = TREE_VALUE (oldelt);
28468 tree newdecl = rewrite_template_parm (olddecl, index, level,
28469 targs, complain);
28470 if (newdecl == error_mark_node)
28471 return error_mark_node;
28472 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28473 targs, complain, NULL_TREE);
28474 tree list = build_tree_list (newdef, newdecl);
28475 TEMPLATE_PARM_CONSTRAINTS (list)
28476 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28477 targs, complain, NULL_TREE);
28478 int depth = TMPL_ARGS_DEPTH (targs);
28479 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28480 return list;
28481 }
28482
28483 /* Returns a C++17 class deduction guide template based on the constructor
28484 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28485 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28486 aggregate initialization guide. */
28487
28488 static tree
28489 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28490 {
28491 tree tparms, targs, fparms, fargs, ci;
28492 bool memtmpl = false;
28493 bool explicit_p;
28494 location_t loc;
28495 tree fn_tmpl = NULL_TREE;
28496
28497 if (outer_args)
28498 {
28499 ++processing_template_decl;
28500 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28501 --processing_template_decl;
28502 }
28503
28504 if (!DECL_DECLARES_FUNCTION_P (ctor))
28505 {
28506 if (TYPE_P (ctor))
28507 {
28508 bool copy_p = TYPE_REF_P (ctor);
28509 if (copy_p)
28510 fparms = tree_cons (NULL_TREE, type, void_list_node);
28511 else
28512 fparms = void_list_node;
28513 }
28514 else if (TREE_CODE (ctor) == TREE_LIST)
28515 fparms = ctor;
28516 else
28517 gcc_unreachable ();
28518
28519 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28520 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28521 targs = CLASSTYPE_TI_ARGS (type);
28522 ci = NULL_TREE;
28523 fargs = NULL_TREE;
28524 loc = DECL_SOURCE_LOCATION (ctmpl);
28525 explicit_p = false;
28526 }
28527 else
28528 {
28529 ++processing_template_decl;
28530 bool ok = true;
28531
28532 fn_tmpl
28533 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28534 : DECL_TI_TEMPLATE (ctor));
28535 if (outer_args)
28536 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28537 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28538
28539 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28540 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28541 fully specialized args for the enclosing class. Strip those off, as
28542 the deduction guide won't have those template parameters. */
28543 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28544 TMPL_PARMS_DEPTH (tparms));
28545 /* Discard the 'this' parameter. */
28546 fparms = FUNCTION_ARG_CHAIN (ctor);
28547 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28548 ci = get_constraints (ctor);
28549 loc = DECL_SOURCE_LOCATION (ctor);
28550 explicit_p = DECL_NONCONVERTING_P (ctor);
28551
28552 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28553 {
28554 memtmpl = true;
28555
28556 /* For a member template constructor, we need to flatten the two
28557 template parameter lists into one, and then adjust the function
28558 signature accordingly. This gets...complicated. */
28559 tree save_parms = current_template_parms;
28560
28561 /* For a member template we should have two levels of parms/args, one
28562 for the class and one for the constructor. We stripped
28563 specialized args for further enclosing classes above. */
28564 const int depth = 2;
28565 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28566
28567 /* Template args for translating references to the two-level template
28568 parameters into references to the one-level template parameters we
28569 are creating. */
28570 tree tsubst_args = copy_node (targs);
28571 TMPL_ARGS_LEVEL (tsubst_args, depth)
28572 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28573
28574 /* Template parms for the constructor template. */
28575 tree ftparms = TREE_VALUE (tparms);
28576 unsigned flen = TREE_VEC_LENGTH (ftparms);
28577 /* Template parms for the class template. */
28578 tparms = TREE_CHAIN (tparms);
28579 tree ctparms = TREE_VALUE (tparms);
28580 unsigned clen = TREE_VEC_LENGTH (ctparms);
28581 /* Template parms for the deduction guide start as a copy of the
28582 template parms for the class. We set current_template_parms for
28583 lookup_template_class_1. */
28584 current_template_parms = tparms = copy_node (tparms);
28585 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28586 for (unsigned i = 0; i < clen; ++i)
28587 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28588
28589 /* Now we need to rewrite the constructor parms to append them to the
28590 class parms. */
28591 for (unsigned i = 0; i < flen; ++i)
28592 {
28593 unsigned index = i + clen;
28594 unsigned level = 1;
28595 tree oldelt = TREE_VEC_ELT (ftparms, i);
28596 tree newelt
28597 = rewrite_tparm_list (oldelt, index, level,
28598 tsubst_args, i, complain);
28599 if (newelt == error_mark_node)
28600 ok = false;
28601 TREE_VEC_ELT (new_vec, index) = newelt;
28602 }
28603
28604 /* Now we have a final set of template parms to substitute into the
28605 function signature. */
28606 targs = template_parms_to_args (tparms);
28607 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28608 complain, ctor);
28609 if (fparms == error_mark_node)
28610 ok = false;
28611 if (ci)
28612 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28613
28614 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28615 cp_unevaluated_operand. */
28616 cp_evaluated ev;
28617 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28618 current_template_parms = save_parms;
28619 }
28620 else
28621 {
28622 /* Substitute in the same arguments to rewrite class members into
28623 references to members of an unknown specialization. */
28624 cp_evaluated ev;
28625 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28626 fargs = tsubst (fargs, targs, complain, ctor);
28627 if (ci)
28628 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28629 }
28630
28631 --processing_template_decl;
28632 if (!ok)
28633 return error_mark_node;
28634 }
28635
28636 if (!memtmpl)
28637 {
28638 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28639 tparms = copy_node (tparms);
28640 INNERMOST_TEMPLATE_PARMS (tparms)
28641 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28642 }
28643
28644 tree fntype = build_function_type (type, fparms);
28645 tree ded_fn = build_lang_decl_loc (loc,
28646 FUNCTION_DECL,
28647 dguide_name (type), fntype);
28648 DECL_ARGUMENTS (ded_fn) = fargs;
28649 DECL_ARTIFICIAL (ded_fn) = true;
28650 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28651 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28652 DECL_ARTIFICIAL (ded_tmpl) = true;
28653 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28654 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28655 if (DECL_P (ctor))
28656 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28657 if (ci)
28658 set_constraints (ded_tmpl, ci);
28659
28660 return ded_tmpl;
28661 }
28662
28663 /* Add to LIST the member types for the reshaped initializer CTOR. */
28664
28665 static tree
28666 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28667 {
28668 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28669 tree idx, val; unsigned i;
28670 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28671 {
28672 tree ftype = elt ? elt : TREE_TYPE (idx);
28673 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28674 && CONSTRUCTOR_NELTS (val)
28675 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28676 type gets a single initializer. */
28677 && CP_AGGREGATE_TYPE_P (ftype)
28678 && !(TREE_CODE (ftype) == ARRAY_TYPE
28679 && uses_template_parms (TYPE_DOMAIN (ftype))))
28680 {
28681 tree subelt = NULL_TREE;
28682 if (TREE_CODE (ftype) == ARRAY_TYPE)
28683 subelt = TREE_TYPE (ftype);
28684 list = collect_ctor_idx_types (val, list, subelt);
28685 continue;
28686 }
28687 tree arg = NULL_TREE;
28688 if (i == v->length() - 1
28689 && PACK_EXPANSION_P (ftype))
28690 /* Give the trailing pack expansion parameter a default argument to
28691 match aggregate initialization behavior, even if we deduce the
28692 length of the pack separately to more than we have initializers. */
28693 arg = build_constructor (init_list_type_node, NULL);
28694 /* if ei is of array type and xi is a braced-init-list or string literal,
28695 Ti is an rvalue reference to the declared type of ei */
28696 STRIP_ANY_LOCATION_WRAPPER (val);
28697 if (TREE_CODE (ftype) == ARRAY_TYPE
28698 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28699 || TREE_CODE (val) == STRING_CST))
28700 {
28701 if (TREE_CODE (val) == STRING_CST)
28702 ftype = cp_build_qualified_type
28703 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28704 ftype = (cp_build_reference_type
28705 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28706 }
28707 list = tree_cons (arg, ftype, list);
28708 }
28709
28710 return list;
28711 }
28712
28713 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28714
28715 static bool
28716 is_spec_or_derived (tree etype, tree tmpl)
28717 {
28718 if (!etype || !CLASS_TYPE_P (etype))
28719 return false;
28720
28721 tree type = TREE_TYPE (tmpl);
28722 tree tparms = (INNERMOST_TEMPLATE_PARMS
28723 (DECL_TEMPLATE_PARMS (tmpl)));
28724 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28725 int err = unify (tparms, targs, type, etype,
28726 UNIFY_ALLOW_DERIVED, /*explain*/false);
28727 ggc_free (targs);
28728 return !err;
28729 }
28730
28731 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28732 INIT. */
28733
28734 static tree
28735 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28736 {
28737 if (cxx_dialect < cxx20)
28738 return NULL_TREE;
28739
28740 if (init == NULL_TREE)
28741 return NULL_TREE;
28742
28743 tree type = TREE_TYPE (tmpl);
28744 if (!CP_AGGREGATE_TYPE_P (type))
28745 return NULL_TREE;
28746
28747 /* No aggregate candidate for copy-initialization. */
28748 if (args->length() == 1)
28749 {
28750 tree val = (*args)[0];
28751 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28752 return NULL_TREE;
28753 }
28754
28755 /* If we encounter a problem, we just won't add the candidate. */
28756 tsubst_flags_t complain = tf_none;
28757
28758 tree parms = NULL_TREE;
28759 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28760 {
28761 init = reshape_init (type, init, complain);
28762 if (init == error_mark_node)
28763 return NULL_TREE;
28764 parms = collect_ctor_idx_types (init, parms);
28765 }
28766 else if (TREE_CODE (init) == TREE_LIST)
28767 {
28768 int len = list_length (init);
28769 for (tree field = TYPE_FIELDS (type);
28770 len;
28771 --len, field = DECL_CHAIN (field))
28772 {
28773 field = next_initializable_field (field);
28774 if (!field)
28775 return NULL_TREE;
28776 tree ftype = finish_decltype_type (field, true, complain);
28777 parms = tree_cons (NULL_TREE, ftype, parms);
28778 }
28779 }
28780 else
28781 /* Aggregate initialization doesn't apply to an initializer expression. */
28782 return NULL_TREE;
28783
28784 if (parms)
28785 {
28786 tree last = parms;
28787 parms = nreverse (parms);
28788 TREE_CHAIN (last) = void_list_node;
28789 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28790 return guide;
28791 }
28792
28793 return NULL_TREE;
28794 }
28795
28796 /* UGUIDES are the deduction guides for the underlying template of alias
28797 template TMPL; adjust them to be deduction guides for TMPL. */
28798
28799 static tree
28800 alias_ctad_tweaks (tree tmpl, tree uguides)
28801 {
28802 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28803 class type (9.2.8.2) where the template-name names an alias template A,
28804 the defining-type-id of A must be of the form
28805
28806 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28807
28808 as specified in 9.2.8.2. The guides of A are the set of functions or
28809 function templates formed as follows. For each function or function
28810 template f in the guides of the template named by the simple-template-id
28811 of the defining-type-id, the template arguments of the return type of f
28812 are deduced from the defining-type-id of A according to the process in
28813 13.10.2.5 with the exception that deduction does not fail if not all
28814 template arguments are deduced. Let g denote the result of substituting
28815 these deductions into f. If substitution succeeds, form a function or
28816 function template f' with the following properties and add it to the set
28817 of guides of A:
28818
28819 * The function type of f' is the function type of g.
28820
28821 * If f is a function template, f' is a function template whose template
28822 parameter list consists of all the template parameters of A (including
28823 their default template arguments) that appear in the above deductions or
28824 (recursively) in their default template arguments, followed by the
28825 template parameters of f that were not deduced (including their default
28826 template arguments), otherwise f' is not a function template.
28827
28828 * The associated constraints (13.5.2) are the conjunction of the
28829 associated constraints of g and a constraint that is satisfied if and only
28830 if the arguments of A are deducible (see below) from the return type.
28831
28832 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28833 be so as well.
28834
28835 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28836 considered to be so as well.
28837
28838 * The explicit-specifier of f' is the explicit-specifier of g (if
28839 any). */
28840
28841 /* This implementation differs from the above in two significant ways:
28842
28843 1) We include all template parameters of A, not just some.
28844 2) The added constraint is same_type instead of deducible.
28845
28846 I believe that while it's probably possible to construct a testcase that
28847 behaves differently with this simplification, it should have the same
28848 effect for real uses. Including all template parameters means that we
28849 deduce all parameters of A when resolving the call, so when we're in the
28850 constraint we don't need to deduce them again, we can just check whether
28851 the deduction produced the desired result. */
28852
28853 tsubst_flags_t complain = tf_warning_or_error;
28854 tree atype = TREE_TYPE (tmpl);
28855 tree aguides = NULL_TREE;
28856 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28857 unsigned natparms = TREE_VEC_LENGTH (atparms);
28858 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28859 for (ovl_iterator iter (uguides); iter; ++iter)
28860 {
28861 tree f = *iter;
28862 tree in_decl = f;
28863 location_t loc = DECL_SOURCE_LOCATION (f);
28864 tree ret = TREE_TYPE (TREE_TYPE (f));
28865 tree fprime = f;
28866 if (TREE_CODE (f) == TEMPLATE_DECL)
28867 {
28868 processing_template_decl_sentinel ptds (/*reset*/false);
28869 ++processing_template_decl;
28870
28871 /* Deduce template arguments for f from the type-id of A. */
28872 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28873 unsigned len = TREE_VEC_LENGTH (ftparms);
28874 tree targs = make_tree_vec (len);
28875 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28876 gcc_assert (!err);
28877
28878 /* The number of parms for f' is the number of parms for A plus
28879 non-deduced parms of f. */
28880 unsigned ndlen = 0;
28881 unsigned j;
28882 for (unsigned i = 0; i < len; ++i)
28883 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28884 ++ndlen;
28885 tree gtparms = make_tree_vec (natparms + ndlen);
28886
28887 /* First copy over the parms of A. */
28888 for (j = 0; j < natparms; ++j)
28889 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28890 /* Now rewrite the non-deduced parms of f. */
28891 for (unsigned i = 0; ndlen && i < len; ++i)
28892 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28893 {
28894 --ndlen;
28895 unsigned index = j++;
28896 unsigned level = 1;
28897 tree oldlist = TREE_VEC_ELT (ftparms, i);
28898 tree list = rewrite_tparm_list (oldlist, index, level,
28899 targs, i, complain);
28900 TREE_VEC_ELT (gtparms, index) = list;
28901 }
28902 gtparms = build_tree_list (size_one_node, gtparms);
28903
28904 /* Substitute the deduced arguments plus the rewritten template
28905 parameters into f to get g. This covers the type, copyness,
28906 guideness, and explicit-specifier. */
28907 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28908 if (g == error_mark_node)
28909 return error_mark_node;
28910 DECL_USE_TEMPLATE (g) = 0;
28911 fprime = build_template_decl (g, gtparms, false);
28912 DECL_TEMPLATE_RESULT (fprime) = g;
28913 TREE_TYPE (fprime) = TREE_TYPE (g);
28914 tree gtargs = template_parms_to_args (gtparms);
28915 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28916 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28917
28918 /* Substitute the associated constraints. */
28919 tree ci = get_constraints (f);
28920 if (ci)
28921 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28922 if (ci == error_mark_node)
28923 return error_mark_node;
28924
28925 /* Add a constraint that the return type matches the instantiation of
28926 A with the same template arguments. */
28927 ret = TREE_TYPE (TREE_TYPE (fprime));
28928 if (!same_type_p (atype, ret)
28929 /* FIXME this should mean they don't compare as equivalent. */
28930 || dependent_alias_template_spec_p (atype, nt_opaque))
28931 {
28932 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28933 ci = append_constraint (ci, same);
28934 }
28935
28936 if (ci)
28937 {
28938 remove_constraints (fprime);
28939 set_constraints (fprime, ci);
28940 }
28941 }
28942 else
28943 {
28944 /* For a non-template deduction guide, if the arguments of A aren't
28945 deducible from the return type, don't add the candidate. */
28946 tree targs = make_tree_vec (natparms);
28947 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28948 for (unsigned i = 0; !err && i < natparms; ++i)
28949 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28950 err = true;
28951 if (err)
28952 continue;
28953 }
28954
28955 aguides = lookup_add (fprime, aguides);
28956 }
28957
28958 return aguides;
28959 }
28960
28961 /* Return artificial deduction guides built from the constructors of class
28962 template TMPL. */
28963
28964 static tree
28965 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28966 {
28967 tree type = TREE_TYPE (tmpl);
28968 tree outer_args = NULL_TREE;
28969 if (DECL_CLASS_SCOPE_P (tmpl)
28970 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28971 {
28972 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28973 type = TREE_TYPE (most_general_template (tmpl));
28974 }
28975
28976 tree cands = NULL_TREE;
28977
28978 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28979 {
28980 /* Skip inherited constructors. */
28981 if (iter.using_p ())
28982 continue;
28983
28984 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28985 cands = lookup_add (guide, cands);
28986 }
28987
28988 /* Add implicit default constructor deduction guide. */
28989 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28990 {
28991 tree guide = build_deduction_guide (type, type, outer_args,
28992 complain);
28993 cands = lookup_add (guide, cands);
28994 }
28995
28996 /* Add copy guide. */
28997 {
28998 tree gtype = build_reference_type (type);
28999 tree guide = build_deduction_guide (type, gtype, outer_args,
29000 complain);
29001 cands = lookup_add (guide, cands);
29002 }
29003
29004 return cands;
29005 }
29006
29007 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29008
29009 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29010 aggregate candidate is added separately because it depends on the
29011 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29012 guide. */
29013
29014 static tree
29015 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29016 {
29017 tree guides = NULL_TREE;
29018 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29019 {
29020 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29021 tree tinfo = get_template_info (under);
29022 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29023 complain);
29024 }
29025 else
29026 {
29027 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29028 dguide_name (tmpl),
29029 LOOK_want::NORMAL, /*complain*/false);
29030 if (guides == error_mark_node)
29031 guides = NULL_TREE;
29032 else
29033 any_dguides_p = true;
29034 }
29035
29036 /* Cache the deduction guides for a template. We also remember the result of
29037 lookup, and rebuild everything if it changes; should be very rare. */
29038 tree_pair_p cache = NULL;
29039 if (tree_pair_p &r
29040 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29041 {
29042 cache = r;
29043 if (cache->purpose == guides)
29044 return cache->value;
29045 }
29046 else
29047 {
29048 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29049 cache->purpose = guides;
29050 }
29051
29052 tree cands = NULL_TREE;
29053 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29054 cands = alias_ctad_tweaks (tmpl, guides);
29055 else
29056 {
29057 cands = ctor_deduction_guides_for (tmpl, complain);
29058 for (ovl_iterator it (guides); it; ++it)
29059 cands = lookup_add (*it, cands);
29060 }
29061
29062 cache->value = cands;
29063 return cands;
29064 }
29065
29066 /* Return whether TMPL is a (class template argument-) deducible template. */
29067
29068 bool
29069 ctad_template_p (tree tmpl)
29070 {
29071 /* A deducible template is either a class template or is an alias template
29072 whose defining-type-id is of the form
29073
29074 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29075
29076 where the nested-name-specifier (if any) is non-dependent and the
29077 template-name of the simple-template-id names a deducible template. */
29078
29079 if (DECL_CLASS_TEMPLATE_P (tmpl)
29080 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29081 return true;
29082 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29083 return false;
29084 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29085 if (tree tinfo = get_template_info (orig))
29086 return ctad_template_p (TI_TEMPLATE (tinfo));
29087 return false;
29088 }
29089
29090 /* Deduce template arguments for the class template placeholder PTYPE for
29091 template TMPL based on the initializer INIT, and return the resulting
29092 type. */
29093
29094 static tree
29095 do_class_deduction (tree ptype, tree tmpl, tree init,
29096 int flags, tsubst_flags_t complain)
29097 {
29098 /* We should have handled this in the caller. */
29099 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29100 return ptype;
29101
29102 /* Initializing one placeholder from another. */
29103 if (init && TREE_CODE (init) == TEMPLATE_PARM_INDEX
29104 && is_auto (TREE_TYPE (init))
29105 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29106 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29107
29108 /* Look through alias templates that just rename another template. */
29109 tmpl = get_underlying_template (tmpl);
29110 if (!ctad_template_p (tmpl))
29111 {
29112 if (complain & tf_error)
29113 error ("non-deducible template %qT used without template arguments", tmpl);
29114 return error_mark_node;
29115 }
29116 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29117 {
29118 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29119 if (complain & tf_warning_or_error)
29120 pedwarn (input_location, 0, "alias template deduction only available "
29121 "with %<-std=c++20%> or %<-std=gnu++20%>");
29122 }
29123
29124 tree type = TREE_TYPE (tmpl);
29125
29126 bool try_list_ctor = false;
29127 bool list_init_p = false;
29128
29129 releasing_vec rv_args = NULL;
29130 vec<tree,va_gc> *&args = *&rv_args;
29131 if (init == NULL_TREE)
29132 args = make_tree_vector ();
29133 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29134 {
29135 list_init_p = true;
29136 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29137 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
29138 {
29139 /* As an exception, the first phase in 16.3.1.7 (considering the
29140 initializer list as a single argument) is omitted if the
29141 initializer list consists of a single expression of type cv U,
29142 where U is a specialization of C or a class derived from a
29143 specialization of C. */
29144 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29145 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29146 try_list_ctor = false;
29147 }
29148 if (try_list_ctor || is_std_init_list (type))
29149 args = make_tree_vector_single (init);
29150 else
29151 args = make_tree_vector_from_ctor (init);
29152 }
29153 else if (TREE_CODE (init) == TREE_LIST)
29154 args = make_tree_vector_from_list (init);
29155 else
29156 args = make_tree_vector_single (init);
29157
29158 /* Do this now to avoid problems with erroneous args later on. */
29159 args = resolve_args (args, complain);
29160 if (args == NULL)
29161 return error_mark_node;
29162
29163 bool any_dguides_p = false;
29164 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29165 if (cands == error_mark_node)
29166 return error_mark_node;
29167
29168 /* Prune explicit deduction guides in copy-initialization context (but
29169 not copy-list-initialization). */
29170 bool elided = false;
29171 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29172 {
29173 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29174 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29175 elided = true;
29176
29177 if (elided)
29178 {
29179 /* Found a nonconverting guide, prune the candidates. */
29180 tree pruned = NULL_TREE;
29181 for (lkp_iterator iter (cands); iter; ++iter)
29182 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29183 pruned = lookup_add (*iter, pruned);
29184
29185 cands = pruned;
29186 }
29187 }
29188
29189 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29190 cands = lookup_add (guide, cands);
29191
29192 tree call = error_mark_node;
29193
29194 /* If this is list-initialization and the class has a list constructor, first
29195 try deducing from the list as a single argument, as [over.match.list]. */
29196 tree list_cands = NULL_TREE;
29197 if (try_list_ctor && cands)
29198 for (lkp_iterator iter (cands); iter; ++iter)
29199 {
29200 tree dg = *iter;
29201 if (is_list_ctor (dg))
29202 list_cands = lookup_add (dg, list_cands);
29203 }
29204 if (list_cands)
29205 {
29206 ++cp_unevaluated_operand;
29207 call = build_new_function_call (list_cands, &args, tf_decltype);
29208 --cp_unevaluated_operand;
29209
29210 if (call == error_mark_node)
29211 {
29212 /* That didn't work, now try treating the list as a sequence of
29213 arguments. */
29214 release_tree_vector (args);
29215 args = make_tree_vector_from_ctor (init);
29216 }
29217 }
29218
29219 if (elided && !cands)
29220 {
29221 error ("cannot deduce template arguments for copy-initialization"
29222 " of %qT, as it has no non-explicit deduction guides or "
29223 "user-declared constructors", type);
29224 return error_mark_node;
29225 }
29226 else if (!cands && call == error_mark_node)
29227 {
29228 error ("cannot deduce template arguments of %qT, as it has no viable "
29229 "deduction guides", type);
29230 return error_mark_node;
29231 }
29232
29233 if (call == error_mark_node)
29234 {
29235 ++cp_unevaluated_operand;
29236 call = build_new_function_call (cands, &args, tf_decltype);
29237 --cp_unevaluated_operand;
29238 }
29239
29240 if (call == error_mark_node)
29241 {
29242 if (complain & tf_warning_or_error)
29243 {
29244 error ("class template argument deduction failed:");
29245
29246 ++cp_unevaluated_operand;
29247 call = build_new_function_call (cands, &args,
29248 complain | tf_decltype);
29249 --cp_unevaluated_operand;
29250
29251 if (elided)
29252 inform (input_location, "explicit deduction guides not considered "
29253 "for copy-initialization");
29254 }
29255 return error_mark_node;
29256 }
29257 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29258 constructor is chosen, the initialization is ill-formed. */
29259 else if (flags & LOOKUP_ONLYCONVERTING)
29260 {
29261 tree fndecl = cp_get_callee_fndecl_nofold (call);
29262 if (fndecl && DECL_NONCONVERTING_P (fndecl))
29263 {
29264 if (complain & tf_warning_or_error)
29265 {
29266 // TODO: Pass down location from cp_finish_decl.
29267 error ("class template argument deduction for %qT failed: "
29268 "explicit deduction guide selected in "
29269 "copy-list-initialization", type);
29270 inform (DECL_SOURCE_LOCATION (fndecl),
29271 "explicit deduction guide declared here");
29272
29273 }
29274 return error_mark_node;
29275 }
29276 }
29277
29278 /* If CTAD succeeded but the type doesn't have any explicit deduction
29279 guides, this deduction might not be what the user intended. */
29280 if (call != error_mark_node && !any_dguides_p)
29281 {
29282 tree fndecl = cp_get_callee_fndecl_nofold (call);
29283 if (fndecl != NULL_TREE
29284 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29285 || global_dc->dc_warn_system_headers)
29286 && warning (OPT_Wctad_maybe_unsupported,
29287 "%qT may not intend to support class template argument "
29288 "deduction", type))
29289 inform (input_location, "add a deduction guide to suppress this "
29290 "warning");
29291 }
29292
29293 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29294 }
29295
29296 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29297 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29298 The CONTEXT determines the context in which auto deduction is performed
29299 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29300 OUTER_TARGS are used during template argument deduction
29301 (context == adc_unify) to properly substitute the result, and is ignored
29302 in other contexts.
29303
29304 For partial-concept-ids, extra args may be appended to the list of deduced
29305 template arguments prior to determining constraint satisfaction. */
29306
29307 tree
29308 do_auto_deduction (tree type, tree init, tree auto_node,
29309 tsubst_flags_t complain, auto_deduction_context context,
29310 tree outer_targs, int flags)
29311 {
29312 tree targs;
29313
29314 if (init == error_mark_node)
29315 return error_mark_node;
29316
29317 if (init && type_dependent_expression_p (init)
29318 && context != adc_unify)
29319 /* Defining a subset of type-dependent expressions that we can deduce
29320 from ahead of time isn't worth the trouble. */
29321 return type;
29322
29323 /* Similarly, we can't deduce from another undeduced decl. */
29324 if (init && undeduced_auto_decl (init))
29325 return type;
29326
29327 /* We may be doing a partial substitution, but we still want to replace
29328 auto_node. */
29329 complain &= ~tf_partial;
29330
29331 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29332 /* C++17 class template argument deduction. */
29333 return do_class_deduction (type, tmpl, init, flags, complain);
29334
29335 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29336 /* Nothing we can do with this, even in deduction context. */
29337 return type;
29338
29339 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29340 with either a new invented type template parameter U or, if the
29341 initializer is a braced-init-list (8.5.4), with
29342 std::initializer_list<U>. */
29343 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29344 {
29345 if (!DIRECT_LIST_INIT_P (init))
29346 type = listify_autos (type, auto_node);
29347 else if (CONSTRUCTOR_NELTS (init) == 1)
29348 init = CONSTRUCTOR_ELT (init, 0)->value;
29349 else
29350 {
29351 if (complain & tf_warning_or_error)
29352 {
29353 if (permerror (input_location, "direct-list-initialization of "
29354 "%<auto%> requires exactly one element"))
29355 inform (input_location,
29356 "for deduction to %<std::initializer_list%>, use copy-"
29357 "list-initialization (i.e. add %<=%> before the %<{%>)");
29358 }
29359 type = listify_autos (type, auto_node);
29360 }
29361 }
29362
29363 if (type == error_mark_node)
29364 return error_mark_node;
29365
29366 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29367 {
29368 /* We don't recurse here because we can't deduce from a nested
29369 initializer_list. */
29370 if (CONSTRUCTOR_ELTS (init))
29371 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
29372 elt.value = resolve_nondeduced_context (elt.value, complain);
29373 }
29374 else
29375 init = resolve_nondeduced_context (init, complain);
29376
29377 if (context == adc_decomp_type
29378 && auto_node == type
29379 && init != error_mark_node
29380 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29381 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29382 and initializer has array type, deduce cv-qualified array type. */
29383 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29384 complain);
29385 else if (AUTO_IS_DECLTYPE (auto_node))
29386 {
29387 tree stripped_init = tree_strip_any_location_wrapper (init);
29388 if (REFERENCE_REF_P (stripped_init))
29389 stripped_init = TREE_OPERAND (stripped_init, 0);
29390 bool id = (DECL_P (stripped_init)
29391 || ((TREE_CODE (init) == COMPONENT_REF
29392 || TREE_CODE (init) == SCOPE_REF)
29393 && !REF_PARENTHESIZED_P (init)));
29394 targs = make_tree_vec (1);
29395 TREE_VEC_ELT (targs, 0)
29396 = finish_decltype_type (init, id, tf_warning_or_error);
29397 if (type != auto_node)
29398 {
29399 if (complain & tf_error)
29400 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29401 return error_mark_node;
29402 }
29403 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29404 {
29405 if (complain & tf_error)
29406 error ("%<decltype(auto)%> cannot be cv-qualified");
29407 return error_mark_node;
29408 }
29409 }
29410 else
29411 {
29412 if (error_operand_p (init))
29413 return error_mark_node;
29414
29415 tree parms = build_tree_list (NULL_TREE, type);
29416 tree tparms;
29417
29418 if (flag_concepts)
29419 tparms = extract_autos (type);
29420 else
29421 {
29422 tparms = make_tree_vec (1);
29423 TREE_VEC_ELT (tparms, 0)
29424 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29425 }
29426
29427 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29428 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29429 DEDUCE_CALL,
29430 NULL, /*explain_p=*/false);
29431 if (val > 0)
29432 {
29433 if (processing_template_decl)
29434 /* Try again at instantiation time. */
29435 return type;
29436 if (type && type != error_mark_node
29437 && (complain & tf_error))
29438 /* If type is error_mark_node a diagnostic must have been
29439 emitted by now. Also, having a mention to '<type error>'
29440 in the diagnostic is not really useful to the user. */
29441 {
29442 if (cfun
29443 && FNDECL_USED_AUTO (current_function_decl)
29444 && (auto_node
29445 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29446 && LAMBDA_FUNCTION_P (current_function_decl))
29447 error ("unable to deduce lambda return type from %qE", init);
29448 else
29449 error ("unable to deduce %qT from %qE", type, init);
29450 type_unification_real (tparms, targs, parms, &init, 1, 0,
29451 DEDUCE_CALL,
29452 NULL, /*explain_p=*/true);
29453 }
29454 return error_mark_node;
29455 }
29456 }
29457
29458 /* Check any placeholder constraints against the deduced type. */
29459 if (flag_concepts && !processing_template_decl)
29460 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29461 {
29462 /* Use the deduced type to check the associated constraints. If we
29463 have a partial-concept-id, rebuild the argument list so that
29464 we check using the extra arguments. */
29465 check = unpack_concept_check (check);
29466 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29467 tree cdecl = TREE_OPERAND (check, 0);
29468 if (OVL_P (cdecl))
29469 cdecl = OVL_FIRST (cdecl);
29470 tree cargs = TREE_OPERAND (check, 1);
29471 if (TREE_VEC_LENGTH (cargs) > 1)
29472 {
29473 cargs = copy_node (cargs);
29474 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29475 }
29476 else
29477 cargs = targs;
29478
29479 /* Rebuild the check using the deduced arguments. */
29480 check = build_concept_check (cdecl, cargs, tf_none);
29481
29482 if (!constraints_satisfied_p (check))
29483 {
29484 if (complain & tf_warning_or_error)
29485 {
29486 auto_diagnostic_group d;
29487 switch (context)
29488 {
29489 case adc_unspecified:
29490 case adc_unify:
29491 error("placeholder constraints not satisfied");
29492 break;
29493 case adc_variable_type:
29494 case adc_decomp_type:
29495 error ("deduced initializer does not satisfy "
29496 "placeholder constraints");
29497 break;
29498 case adc_return_type:
29499 error ("deduced return type does not satisfy "
29500 "placeholder constraints");
29501 break;
29502 case adc_requirement:
29503 error ("deduced expression type does not satisfy "
29504 "placeholder constraints");
29505 break;
29506 }
29507 diagnose_constraints (input_location, check, targs);
29508 }
29509 return error_mark_node;
29510 }
29511 }
29512
29513 if (processing_template_decl && context != adc_unify)
29514 outer_targs = current_template_args ();
29515 targs = add_to_template_args (outer_targs, targs);
29516 return tsubst (type, targs, complain, NULL_TREE);
29517 }
29518
29519 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29520 result. */
29521
29522 tree
29523 splice_late_return_type (tree type, tree late_return_type)
29524 {
29525 if (late_return_type)
29526 {
29527 gcc_assert (is_auto (type) || seen_error ());
29528 return late_return_type;
29529 }
29530
29531 if (tree *auto_node = find_type_usage (&type, is_auto))
29532 {
29533 tree idx = get_template_parm_index (*auto_node);
29534 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29535 {
29536 /* In an abbreviated function template we didn't know we were dealing
29537 with a function template when we saw the auto return type, so update
29538 it to have the correct level. */
29539 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29540 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29541 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29542 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29543 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29544 *auto_node = new_auto;
29545 }
29546 }
29547 return type;
29548 }
29549
29550 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29551 'decltype(auto)' or a deduced class template. */
29552
29553 bool
29554 is_auto (const_tree type)
29555 {
29556 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29557 && (TYPE_IDENTIFIER (type) == auto_identifier
29558 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29559 return true;
29560 else
29561 return false;
29562 }
29563
29564 /* for_each_template_parm callback for type_uses_auto. */
29565
29566 int
29567 is_auto_r (tree tp, void */*data*/)
29568 {
29569 return is_auto (tp);
29570 }
29571
29572 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29573 a use of `auto'. Returns NULL_TREE otherwise. */
29574
29575 tree
29576 type_uses_auto (tree type)
29577 {
29578 if (type == NULL_TREE)
29579 return NULL_TREE;
29580 else if (flag_concepts)
29581 {
29582 /* The Concepts TS allows multiple autos in one type-specifier; just
29583 return the first one we find, do_auto_deduction will collect all of
29584 them. */
29585 if (uses_template_parms (type))
29586 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29587 /*visited*/NULL, /*nondeduced*/false);
29588 else
29589 return NULL_TREE;
29590 }
29591 else if (tree *tp = find_type_usage (&type, is_auto))
29592 return *tp;
29593 else
29594 return NULL_TREE;
29595 }
29596
29597 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29598 concepts are enabled, auto is acceptable in template arguments, but
29599 only when TEMPL identifies a template class. Return TRUE if any
29600 such errors were reported. */
29601
29602 bool
29603 check_auto_in_tmpl_args (tree tmpl, tree args)
29604 {
29605 /* If there were previous errors, nevermind. */
29606 if (!args || TREE_CODE (args) != TREE_VEC)
29607 return false;
29608
29609 /* If TMPL is an identifier, we're parsing and we can't tell yet
29610 whether TMPL is supposed to be a type, a function or a variable.
29611 We'll only be able to tell during template substitution, so we
29612 expect to be called again then. If concepts are enabled and we
29613 know we have a type, we're ok. */
29614 if (flag_concepts
29615 && (identifier_p (tmpl)
29616 || (DECL_P (tmpl)
29617 && (DECL_TYPE_TEMPLATE_P (tmpl)
29618 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29619 return false;
29620
29621 /* Quickly search for any occurrences of auto; usually there won't
29622 be any, and then we'll avoid allocating the vector. */
29623 if (!type_uses_auto (args))
29624 return false;
29625
29626 bool errors = false;
29627
29628 tree vec = extract_autos (args);
29629 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29630 {
29631 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29632 error_at (DECL_SOURCE_LOCATION (xauto),
29633 "invalid use of %qT in template argument", xauto);
29634 errors = true;
29635 }
29636
29637 return errors;
29638 }
29639
29640 /* Recursively walk over && expressions searching for EXPR. Return a reference
29641 to that expression. */
29642
29643 static tree *find_template_requirement (tree *t, tree key)
29644 {
29645 if (*t == key)
29646 return t;
29647 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29648 {
29649 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29650 return p;
29651 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29652 return p;
29653 }
29654 return 0;
29655 }
29656
29657 /* Convert the generic type parameters in PARM that match the types given in the
29658 range [START_IDX, END_IDX) from the current_template_parms into generic type
29659 packs. */
29660
29661 tree
29662 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29663 {
29664 tree current = current_template_parms;
29665 int depth = TMPL_PARMS_DEPTH (current);
29666 current = INNERMOST_TEMPLATE_PARMS (current);
29667 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29668
29669 for (int i = 0; i < start_idx; ++i)
29670 TREE_VEC_ELT (replacement, i)
29671 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29672
29673 for (int i = start_idx; i < end_idx; ++i)
29674 {
29675 /* Create a distinct parameter pack type from the current parm and add it
29676 to the replacement args to tsubst below into the generic function
29677 parameter. */
29678 tree node = TREE_VEC_ELT (current, i);
29679 tree o = TREE_TYPE (TREE_VALUE (node));
29680 tree t = copy_type (o);
29681 TEMPLATE_TYPE_PARM_INDEX (t)
29682 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29683 t, 0, 0, tf_none);
29684 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29685 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29686 TYPE_MAIN_VARIANT (t) = t;
29687 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29688 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29689 TREE_VEC_ELT (replacement, i) = t;
29690
29691 /* Replace the current template parameter with new pack. */
29692 TREE_VALUE (node) = TREE_CHAIN (t);
29693
29694 /* Surgically adjust the associated constraint of adjusted parameter
29695 and it's corresponding contribution to the current template
29696 requirements. */
29697 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29698 {
29699 tree id = unpack_concept_check (constr);
29700 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29701 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29702 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29703
29704 /* If there was a constraint, we also need to replace that in
29705 the template requirements, which we've already built. */
29706 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29707 reqs = find_template_requirement (reqs, constr);
29708 *reqs = fold;
29709 }
29710 }
29711
29712 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29713 TREE_VEC_ELT (replacement, i)
29714 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29715
29716 /* If there are more levels then build up the replacement with the outer
29717 template parms. */
29718 if (depth > 1)
29719 replacement = add_to_template_args (template_parms_to_args
29720 (TREE_CHAIN (current_template_parms)),
29721 replacement);
29722
29723 return tsubst (parm, replacement, tf_none, NULL_TREE);
29724 }
29725
29726 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29727 0..N-1. */
29728
29729 void
29730 declare_integer_pack (void)
29731 {
29732 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29733 build_function_type_list (integer_type_node,
29734 integer_type_node,
29735 NULL_TREE),
29736 NULL_TREE, ECF_CONST);
29737 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29738 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29739 CP_BUILT_IN_INTEGER_PACK);
29740 }
29741
29742 /* Walk the decl or type specialization table calling FN on each
29743 entry. */
29744
29745 void
29746 walk_specializations (bool decls_p,
29747 void (*fn) (bool decls_p, spec_entry *entry, void *data),
29748 void *data)
29749 {
29750 spec_hash_table *table = decls_p ? decl_specializations
29751 : type_specializations;
29752 spec_hash_table::iterator end (table->end ());
29753 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
29754 fn (decls_p, *iter, data);
29755 }
29756
29757 /* Lookup the specialization of *ELT, in the decl or type
29758 specialization table. Return the SPEC that's already there (NULL if
29759 nothing). If INSERT is true, and there was nothing, add the new
29760 spec. */
29761
29762 tree
29763 match_mergeable_specialization (bool decl_p, spec_entry *elt, bool insert)
29764 {
29765 hash_table<spec_hasher> *specializations
29766 = decl_p ? decl_specializations : type_specializations;
29767 hashval_t hash = spec_hasher::hash (elt);
29768 spec_entry **slot
29769 = specializations->find_slot_with_hash (elt, hash,
29770 insert ? INSERT : NO_INSERT);
29771 if (slot && *slot)
29772 return (*slot)->spec;
29773
29774 if (insert)
29775 {
29776 auto entry = ggc_alloc<spec_entry> ();
29777 *entry = *elt;
29778 *slot = entry;
29779 }
29780
29781 return NULL_TREE;
29782 }
29783
29784 /* Return flags encoding whether SPEC is on the instantiation and/or
29785 specialization lists of TMPL. */
29786
29787 unsigned
29788 get_mergeable_specialization_flags (tree tmpl, tree decl)
29789 {
29790 unsigned flags = 0;
29791
29792 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
29793 inst; inst = TREE_CHAIN (inst))
29794 if (TREE_VALUE (inst) == decl)
29795 {
29796 flags |= 1;
29797 break;
29798 }
29799
29800 if (CLASS_TYPE_P (TREE_TYPE (decl))
29801 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
29802 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
29803 /* Only need to search if DECL is a partial specialization. */
29804 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
29805 part; part = TREE_CHAIN (part))
29806 if (TREE_VALUE (part) == decl)
29807 {
29808 flags |= 2;
29809 break;
29810 }
29811
29812 return flags;
29813 }
29814
29815 /* Add a new specialization of TMPL. FLAGS is as returned from
29816 get_mergeable_specialization_flags. */
29817
29818 void
29819 add_mergeable_specialization (tree tmpl, tree args, tree decl, unsigned flags)
29820 {
29821 if (flags & 1)
29822 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
29823 = tree_cons (args, decl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
29824
29825 if (flags & 2)
29826 {
29827 /* A partial specialization. */
29828 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
29829 = tree_cons (args, decl, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
29830 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (tmpl))
29831 = TREE_TYPE (DECL_TEMPLATE_RESULT (decl));
29832 }
29833 }
29834
29835 /* Set up the hash tables for template instantiations. */
29836
29837 void
29838 init_template_processing (void)
29839 {
29840 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29841 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29842
29843 if (cxx_dialect >= cxx11)
29844 declare_integer_pack ();
29845 }
29846
29847 /* Print stats about the template hash tables for -fstats. */
29848
29849 void
29850 print_template_statistics (void)
29851 {
29852 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29853 "%f collisions\n", (long) decl_specializations->size (),
29854 (long) decl_specializations->elements (),
29855 decl_specializations->collisions ());
29856 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29857 "%f collisions\n", (long) type_specializations->size (),
29858 (long) type_specializations->elements (),
29859 type_specializations->collisions ());
29860 }
29861
29862 #if CHECKING_P
29863
29864 namespace selftest {
29865
29866 /* Verify that build_non_dependent_expr () works, for various expressions,
29867 and that location wrappers don't affect the results. */
29868
29869 static void
29870 test_build_non_dependent_expr ()
29871 {
29872 location_t loc = BUILTINS_LOCATION;
29873
29874 /* Verify constants, without and with location wrappers. */
29875 tree int_cst = build_int_cst (integer_type_node, 42);
29876 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29877
29878 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29879 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29880 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29881
29882 tree string_lit = build_string (4, "foo");
29883 TREE_TYPE (string_lit) = char_array_type_node;
29884 string_lit = fix_string_type (string_lit);
29885 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29886
29887 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29888 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29889 ASSERT_EQ (wrapped_string_lit,
29890 build_non_dependent_expr (wrapped_string_lit));
29891 }
29892
29893 /* Verify that type_dependent_expression_p () works correctly, even
29894 in the presence of location wrapper nodes. */
29895
29896 static void
29897 test_type_dependent_expression_p ()
29898 {
29899 location_t loc = BUILTINS_LOCATION;
29900
29901 tree name = get_identifier ("foo");
29902
29903 /* If no templates are involved, nothing is type-dependent. */
29904 gcc_assert (!processing_template_decl);
29905 ASSERT_FALSE (type_dependent_expression_p (name));
29906
29907 ++processing_template_decl;
29908
29909 /* Within a template, an unresolved name is always type-dependent. */
29910 ASSERT_TRUE (type_dependent_expression_p (name));
29911
29912 /* Ensure it copes with NULL_TREE and errors. */
29913 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29914 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29915
29916 /* A USING_DECL in a template should be type-dependent, even if wrapped
29917 with a location wrapper (PR c++/83799). */
29918 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29919 TREE_TYPE (using_decl) = integer_type_node;
29920 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29921 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29922 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29923 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29924
29925 --processing_template_decl;
29926 }
29927
29928 /* Run all of the selftests within this file. */
29929
29930 void
29931 cp_pt_c_tests ()
29932 {
29933 test_build_non_dependent_expr ();
29934 test_type_dependent_expression_p ();
29935 }
29936
29937 } // namespace selftest
29938
29939 #endif /* #if CHECKING_P */
29940
29941 #include "gt-cp-pt.h"