c++: Detect deduction guide redeclaration [PR97099]
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 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 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45 #include "target.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
55 {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
68
69 static GTY(()) struct tinst_level *current_tinst_level;
70
71 static GTY(()) vec<tree, va_gc> *saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
80 //
81 // Implementation of the RAII helper for creating new local
82 // specializations.
83 local_specialization_stack::local_specialization_stack (lss_policy policy)
84 : saved (local_specializations)
85 {
86 if (policy == lss_nop)
87 ;
88 else if (policy == lss_blank || !saved)
89 local_specializations = new hash_map<tree, tree>;
90 else
91 local_specializations = new hash_map<tree, tree>(*saved);
92 }
93
94 local_specialization_stack::~local_specialization_stack ()
95 {
96 if (local_specializations != saved)
97 {
98 delete local_specializations;
99 local_specializations = saved;
100 }
101 }
102
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth;
105
106 struct GTY((for_user)) spec_entry
107 {
108 tree tmpl;
109 tree args;
110 tree spec;
111 };
112
113 struct spec_hasher : ggc_ptr_hash<spec_entry>
114 {
115 static hashval_t hash (spec_entry *);
116 static bool equal (spec_entry *, spec_entry *);
117 };
118
119 /* The general template is not in these tables. */
120 typedef hash_table<spec_hasher> spec_hash_table;
121 static GTY (()) spec_hash_table *decl_specializations;
122 static GTY (()) spec_hash_table *type_specializations;
123
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
129
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
138
139 enum template_base_result {
140 tbr_incomplete_type,
141 tbr_ambiguous_baseclass,
142 tbr_success
143 };
144
145 static bool resolve_overloaded_unification (tree, tree, tree, tree,
146 unification_kind_t, int,
147 bool);
148 static int try_one_overload (tree, tree, tree, tree, tree,
149 unification_kind_t, int, bool, bool);
150 static int unify (tree, tree, tree, tree, int, bool);
151 static void add_pending_template (tree);
152 static tree reopen_tinst_level (struct tinst_level *);
153 static tree tsubst_initializer_list (tree, tree);
154 static tree get_partial_spec_bindings (tree, tree, tree);
155 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
156 bool, bool);
157 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
158 bool, bool);
159 static void tsubst_enum (tree, tree, tree);
160 static tree add_to_template_args (tree, tree);
161 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
162 static int check_non_deducible_conversion (tree, tree, int, int,
163 struct conversion **, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
165 tree);
166 static int type_unification_real (tree, tree, tree, const tree *,
167 unsigned int, int, unification_kind_t,
168 vec<deferred_access_check, va_gc> **,
169 bool);
170 static void note_template_header (int);
171 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
172 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
173 static tree convert_template_argument (tree, tree, tree,
174 tsubst_flags_t, int, tree);
175 static tree for_each_template_parm (tree, tree_fn_t, void*,
176 hash_set<tree> *, bool, tree_fn_t = NULL);
177 static tree expand_template_argument_pack (tree);
178 static tree build_template_parm_index (int, int, int, tree, tree);
179 static bool inline_needs_template_parms (tree, bool);
180 static void push_inline_template_parms_recursive (tree, int);
181 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
182 static int mark_template_parm (tree, void *);
183 static int template_parm_this_level_p (tree, void *);
184 static tree tsubst_friend_function (tree, tree);
185 static tree tsubst_friend_class (tree, tree);
186 static int can_complete_type_without_circularity (tree);
187 static tree get_bindings (tree, tree, tree, bool);
188 static int template_decl_level (tree);
189 static int check_cv_quals_for_unify (int, tree, tree);
190 static int unify_pack_expansion (tree, tree, tree,
191 tree, unification_kind_t, bool, bool);
192 static tree copy_template_args (tree);
193 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
194 tree most_specialized_partial_spec (tree, tsubst_flags_t);
195 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
196 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
198 static bool check_specialization_scope (void);
199 static tree process_partial_specialization (tree);
200 static void set_current_access_from_decl (tree);
201 static enum template_base_result get_template_base (tree, tree, tree, tree,
202 bool , tree *);
203 static tree try_class_unification (tree, tree, tree, tree, bool);
204 static bool class_nttp_const_wrapper_p (tree t);
205 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
206 tree, tree);
207 static bool template_template_parm_bindings_ok_p (tree, tree);
208 static void tsubst_default_arguments (tree, tsubst_flags_t);
209 static tree for_each_template_parm_r (tree *, int *, void *);
210 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
211 static void copy_default_args_to_explicit_spec (tree);
212 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
213 static bool dependent_template_arg_p (tree);
214 static bool any_template_arguments_need_structural_equality_p (tree);
215 static bool dependent_type_p_r (tree);
216 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
217 static tree tsubst_decl (tree, tree, tsubst_flags_t);
218 static void perform_instantiation_time_access_checks (tree, tree);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree get_underlying_template (tree);
225 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
226 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
227 static tree make_argument_pack (tree);
228 static void register_parameter_specializations (tree, tree);
229 static tree enclosing_instantiation_of (tree tctx);
230 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
231
232 /* Make the current scope suitable for access checking when we are
233 processing T. T can be FUNCTION_DECL for instantiated function
234 template, VAR_DECL for static member variable, or TYPE_DECL for
235 alias template (needed by instantiate_decl). */
236
237 void
238 push_access_scope (tree t)
239 {
240 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
241 || TREE_CODE (t) == TYPE_DECL);
242
243 if (DECL_FRIEND_CONTEXT (t))
244 push_nested_class (DECL_FRIEND_CONTEXT (t));
245 else if (DECL_CLASS_SCOPE_P (t))
246 push_nested_class (DECL_CONTEXT (t));
247 else
248 push_to_top_level ();
249
250 if (TREE_CODE (t) == FUNCTION_DECL)
251 {
252 vec_safe_push (saved_access_scope, current_function_decl);
253 current_function_decl = t;
254 }
255 }
256
257 /* Restore the scope set up by push_access_scope. T is the node we
258 are processing. */
259
260 void
261 pop_access_scope (tree t)
262 {
263 if (TREE_CODE (t) == FUNCTION_DECL)
264 current_function_decl = saved_access_scope->pop();
265
266 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
267 pop_nested_class ();
268 else
269 pop_from_top_level ();
270 }
271
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
276
277 tree
278 finish_member_template_decl (tree decl)
279 {
280 if (decl == error_mark_node)
281 return error_mark_node;
282
283 gcc_assert (DECL_P (decl));
284
285 if (TREE_CODE (decl) == TYPE_DECL)
286 {
287 tree type;
288
289 type = TREE_TYPE (decl);
290 if (type == error_mark_node)
291 return error_mark_node;
292 if (MAYBE_CLASS_TYPE_P (type)
293 && CLASSTYPE_TEMPLATE_INFO (type)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 {
296 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
297 check_member_template (tmpl);
298 return tmpl;
299 }
300 return NULL_TREE;
301 }
302 else if (TREE_CODE (decl) == FIELD_DECL)
303 error_at (DECL_SOURCE_LOCATION (decl),
304 "data member %qD cannot be a member template", decl);
305 else if (DECL_TEMPLATE_INFO (decl))
306 {
307 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
308 {
309 check_member_template (DECL_TI_TEMPLATE (decl));
310 return DECL_TI_TEMPLATE (decl);
311 }
312 else
313 return decl;
314 }
315 else
316 error_at (DECL_SOURCE_LOCATION (decl),
317 "invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 {
394 if (tree fctx = DECL_FRIEND_CONTEXT (type))
395 type = fctx;
396 else
397 type = CP_DECL_CONTEXT (type);
398 }
399 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
400 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
401 else
402 type = CP_TYPE_CONTEXT (type);
403 }
404
405 return depth;
406 }
407
408 /* Return TRUE if NODE instantiates a template that has arguments of
409 its own, be it directly a primary template or indirectly through a
410 partial specializations. */
411 static bool
412 instantiates_primary_template_p (tree node)
413 {
414 tree tinfo = get_template_info (node);
415 if (!tinfo)
416 return false;
417
418 tree tmpl = TI_TEMPLATE (tinfo);
419 if (PRIMARY_TEMPLATE_P (tmpl))
420 return true;
421
422 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
423 return false;
424
425 /* So now we know we have a specialization, but it could be a full
426 or a partial specialization. To tell which, compare the depth of
427 its template arguments with those of its context. */
428
429 tree ctxt = DECL_CONTEXT (tmpl);
430 tree ctinfo = get_template_info (ctxt);
431 if (!ctinfo)
432 return true;
433
434 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
435 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
436 }
437
438 /* Subroutine of maybe_begin_member_template_processing.
439 Returns true if processing DECL needs us to push template parms. */
440
441 static bool
442 inline_needs_template_parms (tree decl, bool nsdmi)
443 {
444 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
445 return false;
446
447 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
448 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
449 }
450
451 /* Subroutine of maybe_begin_member_template_processing.
452 Push the template parms in PARMS, starting from LEVELS steps into the
453 chain, and ending at the beginning, since template parms are listed
454 innermost first. */
455
456 static void
457 push_inline_template_parms_recursive (tree parmlist, int levels)
458 {
459 tree parms = TREE_VALUE (parmlist);
460 int i;
461
462 if (levels > 1)
463 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
464
465 ++processing_template_decl;
466 current_template_parms
467 = tree_cons (size_int (processing_template_decl),
468 parms, current_template_parms);
469 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
470
471 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
472 NULL);
473 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
474 {
475 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
476
477 if (error_operand_p (parm))
478 continue;
479
480 gcc_assert (DECL_P (parm));
481
482 switch (TREE_CODE (parm))
483 {
484 case TYPE_DECL:
485 case TEMPLATE_DECL:
486 pushdecl (parm);
487 break;
488
489 case PARM_DECL:
490 /* Push the CONST_DECL. */
491 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
492 break;
493
494 default:
495 gcc_unreachable ();
496 }
497 }
498 }
499
500 /* Restore the template parameter context for a member template, a
501 friend template defined in a class definition, or a non-template
502 member of template class. */
503
504 void
505 maybe_begin_member_template_processing (tree decl)
506 {
507 tree parms;
508 int levels = 0;
509 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
510
511 if (nsdmi)
512 {
513 tree ctx = DECL_CONTEXT (decl);
514 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
515 /* Disregard full specializations (c++/60999). */
516 && uses_template_parms (ctx)
517 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
518 }
519
520 if (inline_needs_template_parms (decl, nsdmi))
521 {
522 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
523 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
524
525 if (DECL_TEMPLATE_SPECIALIZATION (decl))
526 {
527 --levels;
528 parms = TREE_CHAIN (parms);
529 }
530
531 push_inline_template_parms_recursive (parms, levels);
532 }
533
534 /* Remember how many levels of template parameters we pushed so that
535 we can pop them later. */
536 inline_parm_levels.safe_push (levels);
537 }
538
539 /* Undo the effects of maybe_begin_member_template_processing. */
540
541 void
542 maybe_end_member_template_processing (void)
543 {
544 int i;
545 int last;
546
547 if (inline_parm_levels.length () == 0)
548 return;
549
550 last = inline_parm_levels.pop ();
551 for (i = 0; i < last; ++i)
552 {
553 --processing_template_decl;
554 current_template_parms = TREE_CHAIN (current_template_parms);
555 poplevel (0, 0, 0);
556 }
557 }
558
559 /* Return a new template argument vector which contains all of ARGS,
560 but has as its innermost set of arguments the EXTRA_ARGS. */
561
562 static tree
563 add_to_template_args (tree args, tree extra_args)
564 {
565 tree new_args;
566 int extra_depth;
567 int i;
568 int j;
569
570 if (args == NULL_TREE || extra_args == error_mark_node)
571 return extra_args;
572
573 extra_depth = TMPL_ARGS_DEPTH (extra_args);
574 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
575
576 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
577 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
578
579 for (j = 1; j <= extra_depth; ++j, ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
581
582 return new_args;
583 }
584
585 /* Like add_to_template_args, but only the outermost ARGS are added to
586 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
587 (EXTRA_ARGS) levels are added. This function is used to combine
588 the template arguments from a partial instantiation with the
589 template arguments used to attain the full instantiation from the
590 partial instantiation.
591
592 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
593
594 tree
595 add_outermost_template_args (tree args, tree extra_args)
596 {
597 tree new_args;
598
599 if (!args)
600 return extra_args;
601 if (TREE_CODE (args) == TEMPLATE_DECL)
602 {
603 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
604 args = TI_ARGS (ti);
605 }
606
607 /* If there are more levels of EXTRA_ARGS than there are ARGS,
608 something very fishy is going on. */
609 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
610
611 /* If *all* the new arguments will be the EXTRA_ARGS, just return
612 them. */
613 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
614 return extra_args;
615
616 /* For the moment, we make ARGS look like it contains fewer levels. */
617 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
618
619 new_args = add_to_template_args (args, extra_args);
620
621 /* Now, we restore ARGS to its full dimensions. */
622 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
623
624 return new_args;
625 }
626
627 /* Return the N levels of innermost template arguments from the ARGS. */
628
629 tree
630 get_innermost_template_args (tree args, int n)
631 {
632 tree new_args;
633 int extra_levels;
634 int i;
635
636 gcc_assert (n >= 0);
637
638 /* If N is 1, just return the innermost set of template arguments. */
639 if (n == 1)
640 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
641
642 /* If we're not removing anything, just return the arguments we were
643 given. */
644 extra_levels = TMPL_ARGS_DEPTH (args) - n;
645 gcc_assert (extra_levels >= 0);
646 if (extra_levels == 0)
647 return args;
648
649 /* Make a new set of arguments, not containing the outer arguments. */
650 new_args = make_tree_vec (n);
651 for (i = 1; i <= n; ++i)
652 SET_TMPL_ARGS_LEVEL (new_args, i,
653 TMPL_ARGS_LEVEL (args, i + extra_levels));
654
655 return new_args;
656 }
657
658 /* The inverse of get_innermost_template_args: Return all but the innermost
659 EXTRA_LEVELS levels of template arguments from the ARGS. */
660
661 static tree
662 strip_innermost_template_args (tree args, int extra_levels)
663 {
664 tree new_args;
665 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
666 int i;
667
668 gcc_assert (n >= 0);
669
670 /* If N is 1, just return the outermost set of template arguments. */
671 if (n == 1)
672 return TMPL_ARGS_LEVEL (args, 1);
673
674 /* If we're not removing anything, just return the arguments we were
675 given. */
676 gcc_assert (extra_levels >= 0);
677 if (extra_levels == 0)
678 return args;
679
680 /* Make a new set of arguments, not containing the inner arguments. */
681 new_args = make_tree_vec (n);
682 for (i = 1; i <= n; ++i)
683 SET_TMPL_ARGS_LEVEL (new_args, i,
684 TMPL_ARGS_LEVEL (args, i));
685
686 return new_args;
687 }
688
689 /* We've got a template header coming up; push to a new level for storing
690 the parms. */
691
692 void
693 begin_template_parm_list (void)
694 {
695 /* We use a non-tag-transparent scope here, which causes pushtag to
696 put tags in this scope, rather than in the enclosing class or
697 namespace scope. This is the right thing, since we want
698 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
699 global template class, push_template_decl handles putting the
700 TEMPLATE_DECL into top-level scope. For a nested template class,
701 e.g.:
702
703 template <class T> struct S1 {
704 template <class T> struct S2 {};
705 };
706
707 pushtag contains special code to insert the TEMPLATE_DECL for S2
708 at the right scope. */
709 begin_scope (sk_template_parms, NULL);
710 ++processing_template_decl;
711 ++processing_template_parmlist;
712 note_template_header (0);
713
714 /* Add a dummy parameter level while we process the parameter list. */
715 current_template_parms
716 = tree_cons (size_int (processing_template_decl),
717 make_tree_vec (0),
718 current_template_parms);
719 }
720
721 /* This routine is called when a specialization is declared. If it is
722 invalid to declare a specialization here, an error is reported and
723 false is returned, otherwise this routine will return true. */
724
725 static bool
726 check_specialization_scope (void)
727 {
728 tree scope = current_scope ();
729
730 /* [temp.expl.spec]
731
732 An explicit specialization shall be declared in the namespace of
733 which the template is a member, or, for member templates, in the
734 namespace of which the enclosing class or enclosing class
735 template is a member. An explicit specialization of a member
736 function, member class or static data member of a class template
737 shall be declared in the namespace of which the class template
738 is a member. */
739 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
740 {
741 error ("explicit specialization in non-namespace scope %qD", scope);
742 return false;
743 }
744
745 /* [temp.expl.spec]
746
747 In an explicit specialization declaration for a member of a class
748 template or a member template that appears in namespace scope,
749 the member template and some of its enclosing class templates may
750 remain unspecialized, except that the declaration shall not
751 explicitly specialize a class member template if its enclosing
752 class templates are not explicitly specialized as well. */
753 if (current_template_parms)
754 {
755 error ("enclosing class templates are not explicitly specialized");
756 return false;
757 }
758
759 return true;
760 }
761
762 /* We've just seen template <>. */
763
764 bool
765 begin_specialization (void)
766 {
767 begin_scope (sk_template_spec, NULL);
768 note_template_header (1);
769 return check_specialization_scope ();
770 }
771
772 /* Called at then end of processing a declaration preceded by
773 template<>. */
774
775 void
776 end_specialization (void)
777 {
778 finish_scope ();
779 reset_specialization ();
780 }
781
782 /* Any template <>'s that we have seen thus far are not referring to a
783 function specialization. */
784
785 void
786 reset_specialization (void)
787 {
788 processing_specialization = 0;
789 template_header_count = 0;
790 }
791
792 /* We've just seen a template header. If SPECIALIZATION is nonzero,
793 it was of the form template <>. */
794
795 static void
796 note_template_header (int specialization)
797 {
798 processing_specialization = specialization;
799 template_header_count++;
800 }
801
802 /* We're beginning an explicit instantiation. */
803
804 void
805 begin_explicit_instantiation (void)
806 {
807 gcc_assert (!processing_explicit_instantiation);
808 processing_explicit_instantiation = true;
809 }
810
811
812 void
813 end_explicit_instantiation (void)
814 {
815 gcc_assert (processing_explicit_instantiation);
816 processing_explicit_instantiation = false;
817 }
818
819 /* An explicit specialization or partial specialization of TMPL is being
820 declared. Check that the namespace in which the specialization is
821 occurring is permissible. Returns false iff it is invalid to
822 specialize TMPL in the current namespace. */
823
824 static bool
825 check_specialization_namespace (tree tmpl)
826 {
827 tree tpl_ns = decl_namespace_context (tmpl);
828
829 /* [tmpl.expl.spec]
830
831 An explicit specialization shall be declared in a namespace enclosing the
832 specialized template. An explicit specialization whose declarator-id is
833 not qualified shall be declared in the nearest enclosing namespace of the
834 template, or, if the namespace is inline (7.3.1), any namespace from its
835 enclosing namespace set. */
836 if (current_scope() != DECL_CONTEXT (tmpl)
837 && !at_namespace_scope_p ())
838 {
839 error ("specialization of %qD must appear at namespace scope", tmpl);
840 return false;
841 }
842
843 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
844 /* Same or enclosing namespace. */
845 return true;
846 else
847 {
848 auto_diagnostic_group d;
849 if (permerror (input_location,
850 "specialization of %qD in different namespace", tmpl))
851 inform (DECL_SOURCE_LOCATION (tmpl),
852 " from definition of %q#D", tmpl);
853 return false;
854 }
855 }
856
857 /* SPEC is an explicit instantiation. Check that it is valid to
858 perform this explicit instantiation in the current namespace. */
859
860 static void
861 check_explicit_instantiation_namespace (tree spec)
862 {
863 tree ns;
864
865 /* DR 275: An explicit instantiation shall appear in an enclosing
866 namespace of its template. */
867 ns = decl_namespace_context (spec);
868 if (!is_nested_namespace (current_namespace, ns))
869 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
870 "(which does not enclose namespace %qD)",
871 spec, current_namespace, ns);
872 }
873
874 /* Returns the type of a template specialization only if that
875 specialization needs to be defined. Otherwise (e.g., if the type has
876 already been defined), the function returns NULL_TREE. */
877
878 static tree
879 maybe_new_partial_specialization (tree type)
880 {
881 /* An implicit instantiation of an incomplete type implies
882 the definition of a new class template.
883
884 template<typename T>
885 struct S;
886
887 template<typename T>
888 struct S<T*>;
889
890 Here, S<T*> is an implicit instantiation of S whose type
891 is incomplete. */
892 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
893 return type;
894
895 /* It can also be the case that TYPE is a completed specialization.
896 Continuing the previous example, suppose we also declare:
897
898 template<typename T>
899 requires Integral<T>
900 struct S<T*>;
901
902 Here, S<T*> refers to the specialization S<T*> defined
903 above. However, we need to differentiate definitions because
904 we intend to define a new partial specialization. In this case,
905 we rely on the fact that the constraints are different for
906 this declaration than that above.
907
908 Note that we also get here for injected class names and
909 late-parsed template definitions. We must ensure that we
910 do not create new type declarations for those cases. */
911 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
912 {
913 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
914 tree args = CLASSTYPE_TI_ARGS (type);
915
916 /* If there are no template parameters, this cannot be a new
917 partial template specialization? */
918 if (!current_template_parms)
919 return NULL_TREE;
920
921 /* The injected-class-name is not a new partial specialization. */
922 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
923 return NULL_TREE;
924
925 /* If the constraints are not the same as those of the primary
926 then, we can probably create a new specialization. */
927 tree type_constr = current_template_constraints ();
928
929 if (type == TREE_TYPE (tmpl))
930 {
931 tree main_constr = get_constraints (tmpl);
932 if (equivalent_constraints (type_constr, main_constr))
933 return NULL_TREE;
934 }
935
936 /* Also, if there's a pre-existing specialization with matching
937 constraints, then this also isn't new. */
938 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
939 while (specs)
940 {
941 tree spec_tmpl = TREE_VALUE (specs);
942 tree spec_args = TREE_PURPOSE (specs);
943 tree spec_constr = get_constraints (spec_tmpl);
944 if (comp_template_args (args, spec_args)
945 && equivalent_constraints (type_constr, spec_constr))
946 return NULL_TREE;
947 specs = TREE_CHAIN (specs);
948 }
949
950 /* Create a new type node (and corresponding type decl)
951 for the newly declared specialization. */
952 tree t = make_class_type (TREE_CODE (type));
953 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
954 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
955
956 /* We only need a separate type node for storing the definition of this
957 partial specialization; uses of S<T*> are unconstrained, so all are
958 equivalent. So keep TYPE_CANONICAL the same. */
959 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
960
961 /* Build the corresponding type decl. */
962 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
963 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
964 DECL_SOURCE_LOCATION (d) = input_location;
965 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
966 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
967
968 return t;
969 }
970
971 return NULL_TREE;
972 }
973
974 /* The TYPE is being declared. If it is a template type, that means it
975 is a partial specialization. Do appropriate error-checking. */
976
977 tree
978 maybe_process_partial_specialization (tree type)
979 {
980 tree context;
981
982 if (type == error_mark_node)
983 return error_mark_node;
984
985 /* A lambda that appears in specialization context is not itself a
986 specialization. */
987 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
988 return type;
989
990 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
991 {
992 error ("name of class shadows template template parameter %qD",
993 TYPE_NAME (type));
994 return error_mark_node;
995 }
996
997 context = TYPE_CONTEXT (type);
998
999 if (TYPE_ALIAS_P (type))
1000 {
1001 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1002
1003 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1004 error ("specialization of alias template %qD",
1005 TI_TEMPLATE (tinfo));
1006 else
1007 error ("explicit specialization of non-template %qT", type);
1008 return error_mark_node;
1009 }
1010 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1011 {
1012 /* This is for ordinary explicit specialization and partial
1013 specialization of a template class such as:
1014
1015 template <> class C<int>;
1016
1017 or:
1018
1019 template <class T> class C<T*>;
1020
1021 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1022
1023 if (tree t = maybe_new_partial_specialization (type))
1024 {
1025 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1026 && !at_namespace_scope_p ())
1027 return error_mark_node;
1028 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1029 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1030 if (processing_template_decl)
1031 {
1032 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1033 if (decl == error_mark_node)
1034 return error_mark_node;
1035 return TREE_TYPE (decl);
1036 }
1037 }
1038 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1039 error ("specialization of %qT after instantiation", type);
1040 else if (errorcount && !processing_specialization
1041 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1042 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1043 /* Trying to define a specialization either without a template<> header
1044 or in an inappropriate place. We've already given an error, so just
1045 bail now so we don't actually define the specialization. */
1046 return error_mark_node;
1047 }
1048 else if (CLASS_TYPE_P (type)
1049 && !CLASSTYPE_USE_TEMPLATE (type)
1050 && CLASSTYPE_TEMPLATE_INFO (type)
1051 && context && CLASS_TYPE_P (context)
1052 && CLASSTYPE_TEMPLATE_INFO (context))
1053 {
1054 /* This is for an explicit specialization of member class
1055 template according to [temp.expl.spec/18]:
1056
1057 template <> template <class U> class C<int>::D;
1058
1059 The context `C<int>' must be an implicit instantiation.
1060 Otherwise this is just a member class template declared
1061 earlier like:
1062
1063 template <> class C<int> { template <class U> class D; };
1064 template <> template <class U> class C<int>::D;
1065
1066 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1067 while in the second case, `C<int>::D' is a primary template
1068 and `C<T>::D' may not exist. */
1069
1070 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1071 && !COMPLETE_TYPE_P (type))
1072 {
1073 tree t;
1074 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1075
1076 if (current_namespace
1077 != decl_namespace_context (tmpl))
1078 {
1079 if (permerror (input_location,
1080 "specialization of %qD in different namespace",
1081 type))
1082 inform (DECL_SOURCE_LOCATION (tmpl),
1083 "from definition of %q#D", tmpl);
1084 }
1085
1086 /* Check for invalid specialization after instantiation:
1087
1088 template <> template <> class C<int>::D<int>;
1089 template <> template <class U> class C<int>::D; */
1090
1091 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1092 t; t = TREE_CHAIN (t))
1093 {
1094 tree inst = TREE_VALUE (t);
1095 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1096 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1097 {
1098 /* We already have a full specialization of this partial
1099 instantiation, or a full specialization has been
1100 looked up but not instantiated. Reassign it to the
1101 new member specialization template. */
1102 spec_entry elt;
1103 spec_entry *entry;
1104
1105 elt.tmpl = most_general_template (tmpl);
1106 elt.args = CLASSTYPE_TI_ARGS (inst);
1107 elt.spec = inst;
1108
1109 type_specializations->remove_elt (&elt);
1110
1111 elt.tmpl = tmpl;
1112 CLASSTYPE_TI_ARGS (inst)
1113 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1114
1115 spec_entry **slot
1116 = type_specializations->find_slot (&elt, INSERT);
1117 entry = ggc_alloc<spec_entry> ();
1118 *entry = elt;
1119 *slot = entry;
1120 }
1121 else
1122 /* But if we've had an implicit instantiation, that's a
1123 problem ([temp.expl.spec]/6). */
1124 error ("specialization %qT after instantiation %qT",
1125 type, inst);
1126 }
1127
1128 /* Mark TYPE as a specialization. And as a result, we only
1129 have one level of template argument for the innermost
1130 class template. */
1131 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1132 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1133 CLASSTYPE_TI_ARGS (type)
1134 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1135 }
1136 }
1137 else if (processing_specialization)
1138 {
1139 /* Someday C++0x may allow for enum template specialization. */
1140 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1141 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1142 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1143 "of %qD not allowed by ISO C++", type);
1144 else
1145 {
1146 error ("explicit specialization of non-template %qT", type);
1147 return error_mark_node;
1148 }
1149 }
1150
1151 return type;
1152 }
1153
1154 /* Returns nonzero if we can optimize the retrieval of specializations
1155 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1156 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1157
1158 static inline bool
1159 optimize_specialization_lookup_p (tree tmpl)
1160 {
1161 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1162 && DECL_CLASS_SCOPE_P (tmpl)
1163 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1164 parameter. */
1165 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1166 /* The optimized lookup depends on the fact that the
1167 template arguments for the member function template apply
1168 purely to the containing class, which is not true if the
1169 containing class is an explicit or partial
1170 specialization. */
1171 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1172 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1173 && !DECL_CONV_FN_P (tmpl)
1174 /* It is possible to have a template that is not a member
1175 template and is not a member of a template class:
1176
1177 template <typename T>
1178 struct S { friend A::f(); };
1179
1180 Here, the friend function is a template, but the context does
1181 not have template information. The optimized lookup relies
1182 on having ARGS be the template arguments for both the class
1183 and the function template. */
1184 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1185 }
1186
1187 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1188 gone through coerce_template_parms by now. */
1189
1190 static void
1191 verify_unstripped_args_1 (tree inner)
1192 {
1193 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1194 {
1195 tree arg = TREE_VEC_ELT (inner, i);
1196 if (TREE_CODE (arg) == TEMPLATE_DECL)
1197 /* OK */;
1198 else if (TYPE_P (arg))
1199 gcc_assert (strip_typedefs (arg, NULL) == arg);
1200 else if (ARGUMENT_PACK_P (arg))
1201 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1202 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1203 /* Allow typedefs on the type of a non-type argument, since a
1204 parameter can have them. */;
1205 else
1206 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1207 }
1208 }
1209
1210 static void
1211 verify_unstripped_args (tree args)
1212 {
1213 ++processing_template_decl;
1214 if (!any_dependent_template_arguments_p (args))
1215 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1216 --processing_template_decl;
1217 }
1218
1219 /* Retrieve the specialization (in the sense of [temp.spec] - a
1220 specialization is either an instantiation or an explicit
1221 specialization) of TMPL for the given template ARGS. If there is
1222 no such specialization, return NULL_TREE. The ARGS are a vector of
1223 arguments, or a vector of vectors of arguments, in the case of
1224 templates with more than one level of parameters.
1225
1226 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1227 then we search for a partial specialization matching ARGS. This
1228 parameter is ignored if TMPL is not a class template.
1229
1230 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1231 result is a NONTYPE_ARGUMENT_PACK. */
1232
1233 static tree
1234 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1235 {
1236 if (tmpl == NULL_TREE)
1237 return NULL_TREE;
1238
1239 if (args == error_mark_node)
1240 return NULL_TREE;
1241
1242 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1243 || TREE_CODE (tmpl) == FIELD_DECL);
1244
1245 /* There should be as many levels of arguments as there are
1246 levels of parameters. */
1247 gcc_assert (TMPL_ARGS_DEPTH (args)
1248 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1249 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1250 : template_class_depth (DECL_CONTEXT (tmpl))));
1251
1252 if (flag_checking)
1253 verify_unstripped_args (args);
1254
1255 /* Lambda functions in templates aren't instantiated normally, but through
1256 tsubst_lambda_expr. */
1257 if (lambda_fn_in_template_p (tmpl))
1258 return NULL_TREE;
1259
1260 if (optimize_specialization_lookup_p (tmpl))
1261 {
1262 /* The template arguments actually apply to the containing
1263 class. Find the class specialization with those
1264 arguments. */
1265 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1266 tree class_specialization
1267 = retrieve_specialization (class_template, args, 0);
1268 if (!class_specialization)
1269 return NULL_TREE;
1270
1271 /* Find the instance of TMPL. */
1272 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1273 for (ovl_iterator iter (fns); iter; ++iter)
1274 {
1275 tree fn = *iter;
1276 if (tree ti = get_template_info (fn))
1277 if (TI_TEMPLATE (ti) == tmpl
1278 /* using-declarations can bring in a different
1279 instantiation of tmpl as a member of a different
1280 instantiation of tmpl's class. We don't want those
1281 here. */
1282 && DECL_CONTEXT (fn) == class_specialization)
1283 return fn;
1284 }
1285 return NULL_TREE;
1286 }
1287 else
1288 {
1289 spec_entry *found;
1290 spec_entry elt;
1291 spec_hash_table *specializations;
1292
1293 elt.tmpl = tmpl;
1294 elt.args = args;
1295 elt.spec = NULL_TREE;
1296
1297 if (DECL_CLASS_TEMPLATE_P (tmpl))
1298 specializations = type_specializations;
1299 else
1300 specializations = decl_specializations;
1301
1302 if (hash == 0)
1303 hash = spec_hasher::hash (&elt);
1304 found = specializations->find_with_hash (&elt, hash);
1305 if (found)
1306 return found->spec;
1307 }
1308
1309 return NULL_TREE;
1310 }
1311
1312 /* Like retrieve_specialization, but for local declarations. */
1313
1314 tree
1315 retrieve_local_specialization (tree tmpl)
1316 {
1317 if (local_specializations == NULL)
1318 return NULL_TREE;
1319
1320 tree *slot = local_specializations->get (tmpl);
1321 return slot ? *slot : NULL_TREE;
1322 }
1323
1324 /* Returns nonzero iff DECL is a specialization of TMPL. */
1325
1326 int
1327 is_specialization_of (tree decl, tree tmpl)
1328 {
1329 tree t;
1330
1331 if (TREE_CODE (decl) == FUNCTION_DECL)
1332 {
1333 for (t = decl;
1334 t != NULL_TREE;
1335 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1336 if (t == tmpl)
1337 return 1;
1338 }
1339 else
1340 {
1341 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1342
1343 for (t = TREE_TYPE (decl);
1344 t != NULL_TREE;
1345 t = CLASSTYPE_USE_TEMPLATE (t)
1346 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1347 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1348 return 1;
1349 }
1350
1351 return 0;
1352 }
1353
1354 /* Returns nonzero iff DECL is a specialization of friend declaration
1355 FRIEND_DECL according to [temp.friend]. */
1356
1357 bool
1358 is_specialization_of_friend (tree decl, tree friend_decl)
1359 {
1360 bool need_template = true;
1361 int template_depth;
1362
1363 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1364 || TREE_CODE (decl) == TYPE_DECL);
1365
1366 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1367 of a template class, we want to check if DECL is a specialization
1368 if this. */
1369 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1370 && DECL_TEMPLATE_INFO (friend_decl)
1371 && !DECL_USE_TEMPLATE (friend_decl))
1372 {
1373 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1374 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1375 need_template = false;
1376 }
1377 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1378 && !PRIMARY_TEMPLATE_P (friend_decl))
1379 need_template = false;
1380
1381 /* There is nothing to do if this is not a template friend. */
1382 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1383 return false;
1384
1385 if (is_specialization_of (decl, friend_decl))
1386 return true;
1387
1388 /* [temp.friend/6]
1389 A member of a class template may be declared to be a friend of a
1390 non-template class. In this case, the corresponding member of
1391 every specialization of the class template is a friend of the
1392 class granting friendship.
1393
1394 For example, given a template friend declaration
1395
1396 template <class T> friend void A<T>::f();
1397
1398 the member function below is considered a friend
1399
1400 template <> struct A<int> {
1401 void f();
1402 };
1403
1404 For this type of template friend, TEMPLATE_DEPTH below will be
1405 nonzero. To determine if DECL is a friend of FRIEND, we first
1406 check if the enclosing class is a specialization of another. */
1407
1408 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1409 if (template_depth
1410 && DECL_CLASS_SCOPE_P (decl)
1411 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1412 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1413 {
1414 /* Next, we check the members themselves. In order to handle
1415 a few tricky cases, such as when FRIEND_DECL's are
1416
1417 template <class T> friend void A<T>::g(T t);
1418 template <class T> template <T t> friend void A<T>::h();
1419
1420 and DECL's are
1421
1422 void A<int>::g(int);
1423 template <int> void A<int>::h();
1424
1425 we need to figure out ARGS, the template arguments from
1426 the context of DECL. This is required for template substitution
1427 of `T' in the function parameter of `g' and template parameter
1428 of `h' in the above examples. Here ARGS corresponds to `int'. */
1429
1430 tree context = DECL_CONTEXT (decl);
1431 tree args = NULL_TREE;
1432 int current_depth = 0;
1433
1434 while (current_depth < template_depth)
1435 {
1436 if (CLASSTYPE_TEMPLATE_INFO (context))
1437 {
1438 if (current_depth == 0)
1439 args = TYPE_TI_ARGS (context);
1440 else
1441 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1442 current_depth++;
1443 }
1444 context = TYPE_CONTEXT (context);
1445 }
1446
1447 if (TREE_CODE (decl) == FUNCTION_DECL)
1448 {
1449 bool is_template;
1450 tree friend_type;
1451 tree decl_type;
1452 tree friend_args_type;
1453 tree decl_args_type;
1454
1455 /* Make sure that both DECL and FRIEND_DECL are templates or
1456 non-templates. */
1457 is_template = DECL_TEMPLATE_INFO (decl)
1458 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1459 if (need_template ^ is_template)
1460 return false;
1461 else if (is_template)
1462 {
1463 /* If both are templates, check template parameter list. */
1464 tree friend_parms
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1466 args, tf_none);
1467 if (!comp_template_parms
1468 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1469 friend_parms))
1470 return false;
1471
1472 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1473 }
1474 else
1475 decl_type = TREE_TYPE (decl);
1476
1477 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1478 tf_none, NULL_TREE);
1479 if (friend_type == error_mark_node)
1480 return false;
1481
1482 /* Check if return types match. */
1483 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1484 return false;
1485
1486 /* Check if function parameter types match, ignoring the
1487 `this' parameter. */
1488 friend_args_type = TYPE_ARG_TYPES (friend_type);
1489 decl_args_type = TYPE_ARG_TYPES (decl_type);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1491 friend_args_type = TREE_CHAIN (friend_args_type);
1492 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1493 decl_args_type = TREE_CHAIN (decl_args_type);
1494
1495 return compparms (decl_args_type, friend_args_type);
1496 }
1497 else
1498 {
1499 /* DECL is a TYPE_DECL */
1500 bool is_template;
1501 tree decl_type = TREE_TYPE (decl);
1502
1503 /* Make sure that both DECL and FRIEND_DECL are templates or
1504 non-templates. */
1505 is_template
1506 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1507 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1508
1509 if (need_template ^ is_template)
1510 return false;
1511 else if (is_template)
1512 {
1513 tree friend_parms;
1514 /* If both are templates, check the name of the two
1515 TEMPLATE_DECL's first because is_friend didn't. */
1516 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1517 != DECL_NAME (friend_decl))
1518 return false;
1519
1520 /* Now check template parameter list. */
1521 friend_parms
1522 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1523 args, tf_none);
1524 return comp_template_parms
1525 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1526 friend_parms);
1527 }
1528 else
1529 return (DECL_NAME (decl)
1530 == DECL_NAME (friend_decl));
1531 }
1532 }
1533 return false;
1534 }
1535
1536 /* Register the specialization SPEC as a specialization of TMPL with
1537 the indicated ARGS. IS_FRIEND indicates whether the specialization
1538 is actually just a friend declaration. ATTRLIST is the list of
1539 attributes that the specialization is declared with or NULL when
1540 it isn't. Returns SPEC, or an equivalent prior declaration, if
1541 available.
1542
1543 We also store instantiations of field packs in the hash table, even
1544 though they are not themselves templates, to make lookup easier. */
1545
1546 static tree
1547 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1548 hashval_t hash)
1549 {
1550 tree fn;
1551 spec_entry **slot = NULL;
1552 spec_entry elt;
1553
1554 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1555 || (TREE_CODE (tmpl) == FIELD_DECL
1556 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1557
1558 if (TREE_CODE (spec) == FUNCTION_DECL
1559 && uses_template_parms (DECL_TI_ARGS (spec)))
1560 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1561 register it; we want the corresponding TEMPLATE_DECL instead.
1562 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1563 the more obvious `uses_template_parms (spec)' to avoid problems
1564 with default function arguments. In particular, given
1565 something like this:
1566
1567 template <class T> void f(T t1, T t = T())
1568
1569 the default argument expression is not substituted for in an
1570 instantiation unless and until it is actually needed. */
1571 return spec;
1572
1573 if (optimize_specialization_lookup_p (tmpl))
1574 /* We don't put these specializations in the hash table, but we might
1575 want to give an error about a mismatch. */
1576 fn = retrieve_specialization (tmpl, args, 0);
1577 else
1578 {
1579 elt.tmpl = tmpl;
1580 elt.args = args;
1581 elt.spec = spec;
1582
1583 if (hash == 0)
1584 hash = spec_hasher::hash (&elt);
1585
1586 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1587 if (*slot)
1588 fn = (*slot)->spec;
1589 else
1590 fn = NULL_TREE;
1591 }
1592
1593 /* We can sometimes try to re-register a specialization that we've
1594 already got. In particular, regenerate_decl_from_template calls
1595 duplicate_decls which will update the specialization list. But,
1596 we'll still get called again here anyhow. It's more convenient
1597 to simply allow this than to try to prevent it. */
1598 if (fn == spec)
1599 return spec;
1600 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1601 {
1602 if (DECL_TEMPLATE_INSTANTIATION (fn))
1603 {
1604 if (DECL_ODR_USED (fn)
1605 || DECL_EXPLICIT_INSTANTIATION (fn))
1606 {
1607 error ("specialization of %qD after instantiation",
1608 fn);
1609 return error_mark_node;
1610 }
1611 else
1612 {
1613 tree clone;
1614 /* This situation should occur only if the first
1615 specialization is an implicit instantiation, the
1616 second is an explicit specialization, and the
1617 implicit instantiation has not yet been used. That
1618 situation can occur if we have implicitly
1619 instantiated a member function and then specialized
1620 it later.
1621
1622 We can also wind up here if a friend declaration that
1623 looked like an instantiation turns out to be a
1624 specialization:
1625
1626 template <class T> void foo(T);
1627 class S { friend void foo<>(int) };
1628 template <> void foo(int);
1629
1630 We transform the existing DECL in place so that any
1631 pointers to it become pointers to the updated
1632 declaration.
1633
1634 If there was a definition for the template, but not
1635 for the specialization, we want this to look as if
1636 there were no definition, and vice versa. */
1637 DECL_INITIAL (fn) = NULL_TREE;
1638 duplicate_decls (spec, fn, is_friend);
1639 /* The call to duplicate_decls will have applied
1640 [temp.expl.spec]:
1641
1642 An explicit specialization of a function template
1643 is inline only if it is explicitly declared to be,
1644 and independently of whether its function template
1645 is.
1646
1647 to the primary function; now copy the inline bits to
1648 the various clones. */
1649 FOR_EACH_CLONE (clone, fn)
1650 {
1651 DECL_DECLARED_INLINE_P (clone)
1652 = DECL_DECLARED_INLINE_P (fn);
1653 DECL_SOURCE_LOCATION (clone)
1654 = DECL_SOURCE_LOCATION (fn);
1655 DECL_DELETED_FN (clone)
1656 = DECL_DELETED_FN (fn);
1657 }
1658 check_specialization_namespace (tmpl);
1659
1660 return fn;
1661 }
1662 }
1663 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1664 {
1665 tree dd = duplicate_decls (spec, fn, is_friend);
1666 if (dd == error_mark_node)
1667 /* We've already complained in duplicate_decls. */
1668 return error_mark_node;
1669
1670 if (dd == NULL_TREE && DECL_INITIAL (spec))
1671 /* Dup decl failed, but this is a new definition. Set the
1672 line number so any errors match this new
1673 definition. */
1674 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1675
1676 return fn;
1677 }
1678 }
1679 else if (fn)
1680 return duplicate_decls (spec, fn, is_friend);
1681
1682 /* A specialization must be declared in the same namespace as the
1683 template it is specializing. */
1684 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1685 && !check_specialization_namespace (tmpl))
1686 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1687
1688 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1689 {
1690 spec_entry *entry = ggc_alloc<spec_entry> ();
1691 gcc_assert (tmpl && args && spec);
1692 *entry = elt;
1693 *slot = entry;
1694 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1695 && PRIMARY_TEMPLATE_P (tmpl)
1696 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1697 || variable_template_p (tmpl))
1698 /* If TMPL is a forward declaration of a template function, keep a list
1699 of all specializations in case we need to reassign them to a friend
1700 template later in tsubst_friend_function.
1701
1702 Also keep a list of all variable template instantiations so that
1703 process_partial_specialization can check whether a later partial
1704 specialization would have used it. */
1705 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1706 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1707 }
1708
1709 return spec;
1710 }
1711
1712 /* Returns true iff two spec_entry nodes are equivalent. */
1713
1714 int comparing_specializations;
1715
1716 bool
1717 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1718 {
1719 int equal;
1720
1721 ++comparing_specializations;
1722 equal = (e1->tmpl == e2->tmpl
1723 && comp_template_args (e1->args, e2->args));
1724 if (equal && flag_concepts
1725 /* tmpl could be a FIELD_DECL for a capture pack. */
1726 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1727 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1728 && uses_template_parms (e1->args))
1729 {
1730 /* Partial specializations of a variable template can be distinguished by
1731 constraints. */
1732 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1733 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1734 equal = equivalent_constraints (c1, c2);
1735 }
1736 --comparing_specializations;
1737
1738 return equal;
1739 }
1740
1741 /* Returns a hash for a template TMPL and template arguments ARGS. */
1742
1743 static hashval_t
1744 hash_tmpl_and_args (tree tmpl, tree args)
1745 {
1746 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1747 return iterative_hash_template_arg (args, val);
1748 }
1749
1750 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1751 ignoring SPEC. */
1752
1753 hashval_t
1754 spec_hasher::hash (spec_entry *e)
1755 {
1756 return hash_tmpl_and_args (e->tmpl, e->args);
1757 }
1758
1759 /* Recursively calculate a hash value for a template argument ARG, for use
1760 in the hash tables of template specializations. We must be
1761 careful to (at least) skip the same entities template_args_equal
1762 does. */
1763
1764 hashval_t
1765 iterative_hash_template_arg (tree arg, hashval_t val)
1766 {
1767 if (arg == NULL_TREE)
1768 return iterative_hash_object (arg, val);
1769
1770 if (!TYPE_P (arg))
1771 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1772 while (CONVERT_EXPR_P (arg)
1773 || TREE_CODE (arg) == NON_LVALUE_EXPR
1774 || class_nttp_const_wrapper_p (arg))
1775 arg = TREE_OPERAND (arg, 0);
1776
1777 enum tree_code code = TREE_CODE (arg);
1778
1779 val = iterative_hash_object (code, val);
1780
1781 switch (code)
1782 {
1783 case ARGUMENT_PACK_SELECT:
1784 gcc_unreachable ();
1785
1786 case ERROR_MARK:
1787 return val;
1788
1789 case IDENTIFIER_NODE:
1790 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1791
1792 case TREE_VEC:
1793 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1794 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1795 return val;
1796
1797 case TYPE_PACK_EXPANSION:
1798 case EXPR_PACK_EXPANSION:
1799 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1800 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1801
1802 case TYPE_ARGUMENT_PACK:
1803 case NONTYPE_ARGUMENT_PACK:
1804 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1805
1806 case TREE_LIST:
1807 for (; arg; arg = TREE_CHAIN (arg))
1808 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1809 return val;
1810
1811 case OVERLOAD:
1812 for (lkp_iterator iter (arg); iter; ++iter)
1813 val = iterative_hash_template_arg (*iter, val);
1814 return val;
1815
1816 case CONSTRUCTOR:
1817 {
1818 tree field, value;
1819 unsigned i;
1820 iterative_hash_template_arg (TREE_TYPE (arg), val);
1821 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1822 {
1823 val = iterative_hash_template_arg (field, val);
1824 val = iterative_hash_template_arg (value, val);
1825 }
1826 return val;
1827 }
1828
1829 case PARM_DECL:
1830 if (!DECL_ARTIFICIAL (arg))
1831 {
1832 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1833 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1834 }
1835 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1836
1837 case TARGET_EXPR:
1838 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1839
1840 case PTRMEM_CST:
1841 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1842 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1843
1844 case TEMPLATE_PARM_INDEX:
1845 val = iterative_hash_template_arg
1846 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1847 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1848 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1849
1850 case TRAIT_EXPR:
1851 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1852 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1853 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1854
1855 case BASELINK:
1856 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1857 val);
1858 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1859 val);
1860
1861 case MODOP_EXPR:
1862 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1863 code = TREE_CODE (TREE_OPERAND (arg, 1));
1864 val = iterative_hash_object (code, val);
1865 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1866
1867 case LAMBDA_EXPR:
1868 /* [temp.over.link] Two lambda-expressions are never considered
1869 equivalent.
1870
1871 So just hash the closure type. */
1872 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1873
1874 case CAST_EXPR:
1875 case IMPLICIT_CONV_EXPR:
1876 case STATIC_CAST_EXPR:
1877 case REINTERPRET_CAST_EXPR:
1878 case CONST_CAST_EXPR:
1879 case DYNAMIC_CAST_EXPR:
1880 case NEW_EXPR:
1881 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1882 /* Now hash operands as usual. */
1883 break;
1884
1885 case CALL_EXPR:
1886 {
1887 tree fn = CALL_EXPR_FN (arg);
1888 if (tree name = dependent_name (fn))
1889 {
1890 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1891 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1892 fn = name;
1893 }
1894 val = iterative_hash_template_arg (fn, val);
1895 call_expr_arg_iterator ai;
1896 for (tree x = first_call_expr_arg (arg, &ai); x;
1897 x = next_call_expr_arg (&ai))
1898 val = iterative_hash_template_arg (x, val);
1899 return val;
1900 }
1901
1902 default:
1903 break;
1904 }
1905
1906 char tclass = TREE_CODE_CLASS (code);
1907 switch (tclass)
1908 {
1909 case tcc_type:
1910 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1911 {
1912 // We want an alias specialization that survived strip_typedefs
1913 // to hash differently from its TYPE_CANONICAL, to avoid hash
1914 // collisions that compare as different in template_args_equal.
1915 // These could be dependent specializations that strip_typedefs
1916 // left alone, or untouched specializations because
1917 // coerce_template_parms returns the unconverted template
1918 // arguments if it sees incomplete argument packs.
1919 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1920 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1921 }
1922
1923 switch (TREE_CODE (arg))
1924 {
1925 case TEMPLATE_TEMPLATE_PARM:
1926 {
1927 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1928
1929 /* Do not recurse with TPI directly, as that is unbounded
1930 recursion. */
1931 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1932 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1933 }
1934 break;
1935
1936 case DECLTYPE_TYPE:
1937 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1938 break;
1939
1940 default:
1941 if (tree canonical = TYPE_CANONICAL (arg))
1942 val = iterative_hash_object (TYPE_HASH (canonical), val);
1943 break;
1944 }
1945
1946 return val;
1947
1948 case tcc_declaration:
1949 case tcc_constant:
1950 return iterative_hash_expr (arg, val);
1951
1952 default:
1953 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1954 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1955 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1956 return val;
1957 }
1958
1959 gcc_unreachable ();
1960 return 0;
1961 }
1962
1963 /* Unregister the specialization SPEC as a specialization of TMPL.
1964 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1965 if the SPEC was listed as a specialization of TMPL.
1966
1967 Note that SPEC has been ggc_freed, so we can't look inside it. */
1968
1969 bool
1970 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1971 {
1972 spec_entry *entry;
1973 spec_entry elt;
1974
1975 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1976 elt.args = TI_ARGS (tinfo);
1977 elt.spec = NULL_TREE;
1978
1979 entry = decl_specializations->find (&elt);
1980 if (entry != NULL)
1981 {
1982 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1983 gcc_assert (new_spec != NULL_TREE);
1984 entry->spec = new_spec;
1985 return 1;
1986 }
1987
1988 return 0;
1989 }
1990
1991 /* Like register_specialization, but for local declarations. We are
1992 registering SPEC, an instantiation of TMPL. */
1993
1994 void
1995 register_local_specialization (tree spec, tree tmpl)
1996 {
1997 gcc_assert (tmpl != spec);
1998 local_specializations->put (tmpl, spec);
1999 }
2000
2001 /* TYPE is a class type. Returns true if TYPE is an explicitly
2002 specialized class. */
2003
2004 bool
2005 explicit_class_specialization_p (tree type)
2006 {
2007 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2008 return false;
2009 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2010 }
2011
2012 /* Print the list of functions at FNS, going through all the overloads
2013 for each element of the list. Alternatively, FNS cannot be a
2014 TREE_LIST, in which case it will be printed together with all the
2015 overloads.
2016
2017 MORE and *STR should respectively be FALSE and NULL when the function
2018 is called from the outside. They are used internally on recursive
2019 calls. print_candidates manages the two parameters and leaves NULL
2020 in *STR when it ends. */
2021
2022 static void
2023 print_candidates_1 (tree fns, char **str, bool more = false)
2024 {
2025 if (TREE_CODE (fns) == TREE_LIST)
2026 for (; fns; fns = TREE_CHAIN (fns))
2027 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2028 else
2029 for (lkp_iterator iter (fns); iter;)
2030 {
2031 tree cand = *iter;
2032 ++iter;
2033
2034 const char *pfx = *str;
2035 if (!pfx)
2036 {
2037 if (more || iter)
2038 pfx = _("candidates are:");
2039 else
2040 pfx = _("candidate is:");
2041 *str = get_spaces (pfx);
2042 }
2043 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2044 }
2045 }
2046
2047 /* Print the list of candidate FNS in an error message. FNS can also
2048 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2049
2050 void
2051 print_candidates (tree fns)
2052 {
2053 char *str = NULL;
2054 print_candidates_1 (fns, &str);
2055 free (str);
2056 }
2057
2058 /* Get a (possibly) constrained template declaration for the
2059 purpose of ordering candidates. */
2060 static tree
2061 get_template_for_ordering (tree list)
2062 {
2063 gcc_assert (TREE_CODE (list) == TREE_LIST);
2064 tree f = TREE_VALUE (list);
2065 if (tree ti = DECL_TEMPLATE_INFO (f))
2066 return TI_TEMPLATE (ti);
2067 return f;
2068 }
2069
2070 /* Among candidates having the same signature, return the
2071 most constrained or NULL_TREE if there is no best candidate.
2072 If the signatures of candidates vary (e.g., template
2073 specialization vs. member function), then there can be no
2074 most constrained.
2075
2076 Note that we don't compare constraints on the functions
2077 themselves, but rather those of their templates. */
2078 static tree
2079 most_constrained_function (tree candidates)
2080 {
2081 // Try to find the best candidate in a first pass.
2082 tree champ = candidates;
2083 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2084 {
2085 int winner = more_constrained (get_template_for_ordering (champ),
2086 get_template_for_ordering (c));
2087 if (winner == -1)
2088 champ = c; // The candidate is more constrained
2089 else if (winner == 0)
2090 return NULL_TREE; // Neither is more constrained
2091 }
2092
2093 // Verify that the champ is better than previous candidates.
2094 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2095 if (!more_constrained (get_template_for_ordering (champ),
2096 get_template_for_ordering (c)))
2097 return NULL_TREE;
2098 }
2099
2100 return champ;
2101 }
2102
2103
2104 /* Returns the template (one of the functions given by TEMPLATE_ID)
2105 which can be specialized to match the indicated DECL with the
2106 explicit template args given in TEMPLATE_ID. The DECL may be
2107 NULL_TREE if none is available. In that case, the functions in
2108 TEMPLATE_ID are non-members.
2109
2110 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2111 specialization of a member template.
2112
2113 The TEMPLATE_COUNT is the number of references to qualifying
2114 template classes that appeared in the name of the function. See
2115 check_explicit_specialization for a more accurate description.
2116
2117 TSK indicates what kind of template declaration (if any) is being
2118 declared. TSK_TEMPLATE indicates that the declaration given by
2119 DECL, though a FUNCTION_DECL, has template parameters, and is
2120 therefore a template function.
2121
2122 The template args (those explicitly specified and those deduced)
2123 are output in a newly created vector *TARGS_OUT.
2124
2125 If it is impossible to determine the result, an error message is
2126 issued. The error_mark_node is returned to indicate failure. */
2127
2128 static tree
2129 determine_specialization (tree template_id,
2130 tree decl,
2131 tree* targs_out,
2132 int need_member_template,
2133 int template_count,
2134 tmpl_spec_kind tsk)
2135 {
2136 tree fns;
2137 tree targs;
2138 tree explicit_targs;
2139 tree candidates = NULL_TREE;
2140
2141 /* A TREE_LIST of templates of which DECL may be a specialization.
2142 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2143 corresponding TREE_PURPOSE is the set of template arguments that,
2144 when used to instantiate the template, would produce a function
2145 with the signature of DECL. */
2146 tree templates = NULL_TREE;
2147 int header_count;
2148 cp_binding_level *b;
2149
2150 *targs_out = NULL_TREE;
2151
2152 if (template_id == error_mark_node || decl == error_mark_node)
2153 return error_mark_node;
2154
2155 /* We shouldn't be specializing a member template of an
2156 unspecialized class template; we already gave an error in
2157 check_specialization_scope, now avoid crashing. */
2158 if (!VAR_P (decl)
2159 && template_count && DECL_CLASS_SCOPE_P (decl)
2160 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2161 {
2162 gcc_assert (errorcount);
2163 return error_mark_node;
2164 }
2165
2166 fns = TREE_OPERAND (template_id, 0);
2167 explicit_targs = TREE_OPERAND (template_id, 1);
2168
2169 if (fns == error_mark_node)
2170 return error_mark_node;
2171
2172 /* Check for baselinks. */
2173 if (BASELINK_P (fns))
2174 fns = BASELINK_FUNCTIONS (fns);
2175
2176 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2177 {
2178 error_at (DECL_SOURCE_LOCATION (decl),
2179 "%qD is not a function template", fns);
2180 return error_mark_node;
2181 }
2182 else if (VAR_P (decl) && !variable_template_p (fns))
2183 {
2184 error ("%qD is not a variable template", fns);
2185 return error_mark_node;
2186 }
2187
2188 /* Count the number of template headers specified for this
2189 specialization. */
2190 header_count = 0;
2191 for (b = current_binding_level;
2192 b->kind == sk_template_parms;
2193 b = b->level_chain)
2194 ++header_count;
2195
2196 tree orig_fns = fns;
2197
2198 if (variable_template_p (fns))
2199 {
2200 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2201 targs = coerce_template_parms (parms, explicit_targs, fns,
2202 tf_warning_or_error,
2203 /*req_all*/true, /*use_defarg*/true);
2204 if (targs != error_mark_node)
2205 templates = tree_cons (targs, fns, templates);
2206 }
2207 else for (lkp_iterator iter (fns); iter; ++iter)
2208 {
2209 tree fn = *iter;
2210
2211 if (TREE_CODE (fn) == TEMPLATE_DECL)
2212 {
2213 tree decl_arg_types;
2214 tree fn_arg_types;
2215 tree insttype;
2216
2217 /* In case of explicit specialization, we need to check if
2218 the number of template headers appearing in the specialization
2219 is correct. This is usually done in check_explicit_specialization,
2220 but the check done there cannot be exhaustive when specializing
2221 member functions. Consider the following code:
2222
2223 template <> void A<int>::f(int);
2224 template <> template <> void A<int>::f(int);
2225
2226 Assuming that A<int> is not itself an explicit specialization
2227 already, the first line specializes "f" which is a non-template
2228 member function, whilst the second line specializes "f" which
2229 is a template member function. So both lines are syntactically
2230 correct, and check_explicit_specialization does not reject
2231 them.
2232
2233 Here, we can do better, as we are matching the specialization
2234 against the declarations. We count the number of template
2235 headers, and we check if they match TEMPLATE_COUNT + 1
2236 (TEMPLATE_COUNT is the number of qualifying template classes,
2237 plus there must be another header for the member template
2238 itself).
2239
2240 Notice that if header_count is zero, this is not a
2241 specialization but rather a template instantiation, so there
2242 is no check we can perform here. */
2243 if (header_count && header_count != template_count + 1)
2244 continue;
2245
2246 /* Check that the number of template arguments at the
2247 innermost level for DECL is the same as for FN. */
2248 if (current_binding_level->kind == sk_template_parms
2249 && !current_binding_level->explicit_spec_p
2250 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2251 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2252 (current_template_parms))))
2253 continue;
2254
2255 /* DECL might be a specialization of FN. */
2256 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2257 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2258
2259 /* For a non-static member function, we need to make sure
2260 that the const qualification is the same. Since
2261 get_bindings does not try to merge the "this" parameter,
2262 we must do the comparison explicitly. */
2263 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2264 {
2265 if (!same_type_p (TREE_VALUE (fn_arg_types),
2266 TREE_VALUE (decl_arg_types)))
2267 continue;
2268
2269 /* And the ref-qualification. */
2270 if (type_memfn_rqual (TREE_TYPE (decl))
2271 != type_memfn_rqual (TREE_TYPE (fn)))
2272 continue;
2273 }
2274
2275 /* Skip the "this" parameter and, for constructors of
2276 classes with virtual bases, the VTT parameter. A
2277 full specialization of a constructor will have a VTT
2278 parameter, but a template never will. */
2279 decl_arg_types
2280 = skip_artificial_parms_for (decl, decl_arg_types);
2281 fn_arg_types
2282 = skip_artificial_parms_for (fn, fn_arg_types);
2283
2284 /* Function templates cannot be specializations; there are
2285 no partial specializations of functions. Therefore, if
2286 the type of DECL does not match FN, there is no
2287 match.
2288
2289 Note that it should never be the case that we have both
2290 candidates added here, and for regular member functions
2291 below. */
2292 if (tsk == tsk_template)
2293 {
2294 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2295 current_template_parms))
2296 continue;
2297 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2298 TREE_TYPE (TREE_TYPE (fn))))
2299 continue;
2300 if (!compparms (fn_arg_types, decl_arg_types))
2301 continue;
2302
2303 tree freq = get_trailing_function_requirements (fn);
2304 tree dreq = get_trailing_function_requirements (decl);
2305 if (!freq != !dreq)
2306 continue;
2307 if (freq)
2308 {
2309 tree fargs = DECL_TI_ARGS (fn);
2310 tsubst_flags_t complain = tf_none;
2311 freq = tsubst_constraint (freq, fargs, complain, fn);
2312 if (!cp_tree_equal (freq, dreq))
2313 continue;
2314 }
2315
2316 candidates = tree_cons (NULL_TREE, fn, candidates);
2317 continue;
2318 }
2319
2320 /* See whether this function might be a specialization of this
2321 template. Suppress access control because we might be trying
2322 to make this specialization a friend, and we have already done
2323 access control for the declaration of the specialization. */
2324 push_deferring_access_checks (dk_no_check);
2325 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2326 pop_deferring_access_checks ();
2327
2328 if (!targs)
2329 /* We cannot deduce template arguments that when used to
2330 specialize TMPL will produce DECL. */
2331 continue;
2332
2333 if (uses_template_parms (targs))
2334 /* We deduced something involving 'auto', which isn't a valid
2335 template argument. */
2336 continue;
2337
2338 /* Remove, from the set of candidates, all those functions
2339 whose constraints are not satisfied. */
2340 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2341 continue;
2342
2343 // Then, try to form the new function type.
2344 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2345 if (insttype == error_mark_node)
2346 continue;
2347 fn_arg_types
2348 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2349 if (!compparms (fn_arg_types, decl_arg_types))
2350 continue;
2351
2352 /* Save this template, and the arguments deduced. */
2353 templates = tree_cons (targs, fn, templates);
2354 }
2355 else if (need_member_template)
2356 /* FN is an ordinary member function, and we need a
2357 specialization of a member template. */
2358 ;
2359 else if (TREE_CODE (fn) != FUNCTION_DECL)
2360 /* We can get IDENTIFIER_NODEs here in certain erroneous
2361 cases. */
2362 ;
2363 else if (!DECL_FUNCTION_MEMBER_P (fn))
2364 /* This is just an ordinary non-member function. Nothing can
2365 be a specialization of that. */
2366 ;
2367 else if (DECL_ARTIFICIAL (fn))
2368 /* Cannot specialize functions that are created implicitly. */
2369 ;
2370 else
2371 {
2372 tree decl_arg_types;
2373
2374 /* This is an ordinary member function. However, since
2375 we're here, we can assume its enclosing class is a
2376 template class. For example,
2377
2378 template <typename T> struct S { void f(); };
2379 template <> void S<int>::f() {}
2380
2381 Here, S<int>::f is a non-template, but S<int> is a
2382 template class. If FN has the same type as DECL, we
2383 might be in business. */
2384
2385 if (!DECL_TEMPLATE_INFO (fn))
2386 /* Its enclosing class is an explicit specialization
2387 of a template class. This is not a candidate. */
2388 continue;
2389
2390 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2391 TREE_TYPE (TREE_TYPE (fn))))
2392 /* The return types differ. */
2393 continue;
2394
2395 /* Adjust the type of DECL in case FN is a static member. */
2396 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2397 if (DECL_STATIC_FUNCTION_P (fn)
2398 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2399 decl_arg_types = TREE_CHAIN (decl_arg_types);
2400
2401 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2402 decl_arg_types))
2403 continue;
2404
2405 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2406 && (type_memfn_rqual (TREE_TYPE (decl))
2407 != type_memfn_rqual (TREE_TYPE (fn))))
2408 continue;
2409
2410 // If the deduced arguments do not satisfy the constraints,
2411 // this is not a candidate.
2412 if (flag_concepts && !constraints_satisfied_p (fn))
2413 continue;
2414
2415 // Add the candidate.
2416 candidates = tree_cons (NULL_TREE, fn, candidates);
2417 }
2418 }
2419
2420 if (templates && TREE_CHAIN (templates))
2421 {
2422 /* We have:
2423
2424 [temp.expl.spec]
2425
2426 It is possible for a specialization with a given function
2427 signature to be instantiated from more than one function
2428 template. In such cases, explicit specification of the
2429 template arguments must be used to uniquely identify the
2430 function template specialization being specialized.
2431
2432 Note that here, there's no suggestion that we're supposed to
2433 determine which of the candidate templates is most
2434 specialized. However, we, also have:
2435
2436 [temp.func.order]
2437
2438 Partial ordering of overloaded function template
2439 declarations is used in the following contexts to select
2440 the function template to which a function template
2441 specialization refers:
2442
2443 -- when an explicit specialization refers to a function
2444 template.
2445
2446 So, we do use the partial ordering rules, at least for now.
2447 This extension can only serve to make invalid programs valid,
2448 so it's safe. And, there is strong anecdotal evidence that
2449 the committee intended the partial ordering rules to apply;
2450 the EDG front end has that behavior, and John Spicer claims
2451 that the committee simply forgot to delete the wording in
2452 [temp.expl.spec]. */
2453 tree tmpl = most_specialized_instantiation (templates);
2454 if (tmpl != error_mark_node)
2455 {
2456 templates = tmpl;
2457 TREE_CHAIN (templates) = NULL_TREE;
2458 }
2459 }
2460
2461 // Concepts allows multiple declarations of member functions
2462 // with the same signature. Like above, we need to rely on
2463 // on the partial ordering of those candidates to determine which
2464 // is the best.
2465 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2466 {
2467 if (tree cand = most_constrained_function (candidates))
2468 {
2469 candidates = cand;
2470 TREE_CHAIN (cand) = NULL_TREE;
2471 }
2472 }
2473
2474 if (templates == NULL_TREE && candidates == NULL_TREE)
2475 {
2476 error ("template-id %qD for %q+D does not match any template "
2477 "declaration", template_id, decl);
2478 if (header_count && header_count != template_count + 1)
2479 inform (DECL_SOURCE_LOCATION (decl),
2480 "saw %d %<template<>%>, need %d for "
2481 "specializing a member function template",
2482 header_count, template_count + 1);
2483 else
2484 print_candidates (orig_fns);
2485 return error_mark_node;
2486 }
2487 else if ((templates && TREE_CHAIN (templates))
2488 || (candidates && TREE_CHAIN (candidates))
2489 || (templates && candidates))
2490 {
2491 error ("ambiguous template specialization %qD for %q+D",
2492 template_id, decl);
2493 candidates = chainon (candidates, templates);
2494 print_candidates (candidates);
2495 return error_mark_node;
2496 }
2497
2498 /* We have one, and exactly one, match. */
2499 if (candidates)
2500 {
2501 tree fn = TREE_VALUE (candidates);
2502 *targs_out = copy_node (DECL_TI_ARGS (fn));
2503
2504 /* Propagate the candidate's constraints to the declaration. */
2505 if (tsk != tsk_template)
2506 set_constraints (decl, get_constraints (fn));
2507
2508 /* DECL is a re-declaration or partial instantiation of a template
2509 function. */
2510 if (TREE_CODE (fn) == TEMPLATE_DECL)
2511 return fn;
2512 /* It was a specialization of an ordinary member function in a
2513 template class. */
2514 return DECL_TI_TEMPLATE (fn);
2515 }
2516
2517 /* It was a specialization of a template. */
2518 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2519 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2520 {
2521 *targs_out = copy_node (targs);
2522 SET_TMPL_ARGS_LEVEL (*targs_out,
2523 TMPL_ARGS_DEPTH (*targs_out),
2524 TREE_PURPOSE (templates));
2525 }
2526 else
2527 *targs_out = TREE_PURPOSE (templates);
2528 return TREE_VALUE (templates);
2529 }
2530
2531 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2532 but with the default argument values filled in from those in the
2533 TMPL_TYPES. */
2534
2535 static tree
2536 copy_default_args_to_explicit_spec_1 (tree spec_types,
2537 tree tmpl_types)
2538 {
2539 tree new_spec_types;
2540
2541 if (!spec_types)
2542 return NULL_TREE;
2543
2544 if (spec_types == void_list_node)
2545 return void_list_node;
2546
2547 /* Substitute into the rest of the list. */
2548 new_spec_types =
2549 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2550 TREE_CHAIN (tmpl_types));
2551
2552 /* Add the default argument for this parameter. */
2553 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2554 TREE_VALUE (spec_types),
2555 new_spec_types);
2556 }
2557
2558 /* DECL is an explicit specialization. Replicate default arguments
2559 from the template it specializes. (That way, code like:
2560
2561 template <class T> void f(T = 3);
2562 template <> void f(double);
2563 void g () { f (); }
2564
2565 works, as required.) An alternative approach would be to look up
2566 the correct default arguments at the call-site, but this approach
2567 is consistent with how implicit instantiations are handled. */
2568
2569 static void
2570 copy_default_args_to_explicit_spec (tree decl)
2571 {
2572 tree tmpl;
2573 tree spec_types;
2574 tree tmpl_types;
2575 tree new_spec_types;
2576 tree old_type;
2577 tree new_type;
2578 tree t;
2579 tree object_type = NULL_TREE;
2580 tree in_charge = NULL_TREE;
2581 tree vtt = NULL_TREE;
2582
2583 /* See if there's anything we need to do. */
2584 tmpl = DECL_TI_TEMPLATE (decl);
2585 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2586 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2587 if (TREE_PURPOSE (t))
2588 break;
2589 if (!t)
2590 return;
2591
2592 old_type = TREE_TYPE (decl);
2593 spec_types = TYPE_ARG_TYPES (old_type);
2594
2595 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2596 {
2597 /* Remove the this pointer, but remember the object's type for
2598 CV quals. */
2599 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2600 spec_types = TREE_CHAIN (spec_types);
2601 tmpl_types = TREE_CHAIN (tmpl_types);
2602
2603 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2604 {
2605 /* DECL may contain more parameters than TMPL due to the extra
2606 in-charge parameter in constructors and destructors. */
2607 in_charge = spec_types;
2608 spec_types = TREE_CHAIN (spec_types);
2609 }
2610 if (DECL_HAS_VTT_PARM_P (decl))
2611 {
2612 vtt = spec_types;
2613 spec_types = TREE_CHAIN (spec_types);
2614 }
2615 }
2616
2617 /* Compute the merged default arguments. */
2618 new_spec_types =
2619 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2620
2621 /* Compute the new FUNCTION_TYPE. */
2622 if (object_type)
2623 {
2624 if (vtt)
2625 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2626 TREE_VALUE (vtt),
2627 new_spec_types);
2628
2629 if (in_charge)
2630 /* Put the in-charge parameter back. */
2631 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2632 TREE_VALUE (in_charge),
2633 new_spec_types);
2634
2635 new_type = build_method_type_directly (object_type,
2636 TREE_TYPE (old_type),
2637 new_spec_types);
2638 }
2639 else
2640 new_type = build_function_type (TREE_TYPE (old_type),
2641 new_spec_types);
2642 new_type = cp_build_type_attribute_variant (new_type,
2643 TYPE_ATTRIBUTES (old_type));
2644 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2645
2646 TREE_TYPE (decl) = new_type;
2647 }
2648
2649 /* Return the number of template headers we expect to see for a definition
2650 or specialization of CTYPE or one of its non-template members. */
2651
2652 int
2653 num_template_headers_for_class (tree ctype)
2654 {
2655 int num_templates = 0;
2656
2657 while (ctype && CLASS_TYPE_P (ctype))
2658 {
2659 /* You're supposed to have one `template <...>' for every
2660 template class, but you don't need one for a full
2661 specialization. For example:
2662
2663 template <class T> struct S{};
2664 template <> struct S<int> { void f(); };
2665 void S<int>::f () {}
2666
2667 is correct; there shouldn't be a `template <>' for the
2668 definition of `S<int>::f'. */
2669 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2670 /* If CTYPE does not have template information of any
2671 kind, then it is not a template, nor is it nested
2672 within a template. */
2673 break;
2674 if (explicit_class_specialization_p (ctype))
2675 break;
2676 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2677 ++num_templates;
2678
2679 ctype = TYPE_CONTEXT (ctype);
2680 }
2681
2682 return num_templates;
2683 }
2684
2685 /* Do a simple sanity check on the template headers that precede the
2686 variable declaration DECL. */
2687
2688 void
2689 check_template_variable (tree decl)
2690 {
2691 tree ctx = CP_DECL_CONTEXT (decl);
2692 int wanted = num_template_headers_for_class (ctx);
2693 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2694 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2695 {
2696 if (cxx_dialect < cxx14)
2697 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2698 "variable templates only available with "
2699 "%<-std=c++14%> or %<-std=gnu++14%>");
2700
2701 // Namespace-scope variable templates should have a template header.
2702 ++wanted;
2703 }
2704 if (template_header_count > wanted)
2705 {
2706 auto_diagnostic_group d;
2707 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2708 "too many template headers for %qD "
2709 "(should be %d)",
2710 decl, wanted);
2711 if (warned && CLASS_TYPE_P (ctx)
2712 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2713 inform (DECL_SOURCE_LOCATION (decl),
2714 "members of an explicitly specialized class are defined "
2715 "without a template header");
2716 }
2717 }
2718
2719 /* An explicit specialization whose declarator-id or class-head-name is not
2720 qualified shall be declared in the nearest enclosing namespace of the
2721 template, or, if the namespace is inline (7.3.1), any namespace from its
2722 enclosing namespace set.
2723
2724 If the name declared in the explicit instantiation is an unqualified name,
2725 the explicit instantiation shall appear in the namespace where its template
2726 is declared or, if that namespace is inline (7.3.1), any namespace from its
2727 enclosing namespace set. */
2728
2729 void
2730 check_unqualified_spec_or_inst (tree t, location_t loc)
2731 {
2732 tree tmpl = most_general_template (t);
2733 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2734 && !is_nested_namespace (current_namespace,
2735 CP_DECL_CONTEXT (tmpl), true))
2736 {
2737 if (processing_specialization)
2738 permerror (loc, "explicit specialization of %qD outside its "
2739 "namespace must use a nested-name-specifier", tmpl);
2740 else if (processing_explicit_instantiation
2741 && cxx_dialect >= cxx11)
2742 /* This was allowed in C++98, so only pedwarn. */
2743 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2744 "outside its namespace must use a nested-name-"
2745 "specifier", tmpl);
2746 }
2747 }
2748
2749 /* Warn for a template specialization SPEC that is missing some of a set
2750 of function or type attributes that the template TEMPL is declared with.
2751 ATTRLIST is a list of additional attributes that SPEC should be taken
2752 to ultimately be declared with. */
2753
2754 static void
2755 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2756 {
2757 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2758 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2759
2760 /* Avoid warning if the difference between the primary and
2761 the specialization is not in one of the attributes below. */
2762 const char* const blacklist[] = {
2763 "alloc_align", "alloc_size", "assume_aligned", "format",
2764 "format_arg", "malloc", "nonnull", NULL
2765 };
2766
2767 /* Put together a list of the black listed attributes that the primary
2768 template is declared with that the specialization is not, in case
2769 it's not apparent from the most recent declaration of the primary. */
2770 pretty_printer str;
2771 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2772 blacklist, &str);
2773
2774 if (!nattrs)
2775 return;
2776
2777 auto_diagnostic_group d;
2778 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2779 "explicit specialization %q#D may be missing attributes",
2780 spec))
2781 inform (DECL_SOURCE_LOCATION (tmpl),
2782 nattrs > 1
2783 ? G_("missing primary template attributes %s")
2784 : G_("missing primary template attribute %s"),
2785 pp_formatted_text (&str));
2786 }
2787
2788 /* Check to see if the function just declared, as indicated in
2789 DECLARATOR, and in DECL, is a specialization of a function
2790 template. We may also discover that the declaration is an explicit
2791 instantiation at this point.
2792
2793 Returns DECL, or an equivalent declaration that should be used
2794 instead if all goes well. Issues an error message if something is
2795 amiss. Returns error_mark_node if the error is not easily
2796 recoverable.
2797
2798 FLAGS is a bitmask consisting of the following flags:
2799
2800 2: The function has a definition.
2801 4: The function is a friend.
2802
2803 The TEMPLATE_COUNT is the number of references to qualifying
2804 template classes that appeared in the name of the function. For
2805 example, in
2806
2807 template <class T> struct S { void f(); };
2808 void S<int>::f();
2809
2810 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2811 classes are not counted in the TEMPLATE_COUNT, so that in
2812
2813 template <class T> struct S {};
2814 template <> struct S<int> { void f(); }
2815 template <> void S<int>::f();
2816
2817 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2818 invalid; there should be no template <>.)
2819
2820 If the function is a specialization, it is marked as such via
2821 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2822 is set up correctly, and it is added to the list of specializations
2823 for that template. */
2824
2825 tree
2826 check_explicit_specialization (tree declarator,
2827 tree decl,
2828 int template_count,
2829 int flags,
2830 tree attrlist)
2831 {
2832 int have_def = flags & 2;
2833 int is_friend = flags & 4;
2834 bool is_concept = flags & 8;
2835 int specialization = 0;
2836 int explicit_instantiation = 0;
2837 int member_specialization = 0;
2838 tree ctype = DECL_CLASS_CONTEXT (decl);
2839 tree dname = DECL_NAME (decl);
2840 tmpl_spec_kind tsk;
2841
2842 if (is_friend)
2843 {
2844 if (!processing_specialization)
2845 tsk = tsk_none;
2846 else
2847 tsk = tsk_excessive_parms;
2848 }
2849 else
2850 tsk = current_tmpl_spec_kind (template_count);
2851
2852 switch (tsk)
2853 {
2854 case tsk_none:
2855 if (processing_specialization && !VAR_P (decl))
2856 {
2857 specialization = 1;
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2859 }
2860 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2861 {
2862 if (is_friend)
2863 /* This could be something like:
2864
2865 template <class T> void f(T);
2866 class S { friend void f<>(int); } */
2867 specialization = 1;
2868 else
2869 {
2870 /* This case handles bogus declarations like template <>
2871 template <class T> void f<int>(); */
2872
2873 error_at (cp_expr_loc_or_input_loc (declarator),
2874 "template-id %qE in declaration of primary template",
2875 declarator);
2876 return decl;
2877 }
2878 }
2879 break;
2880
2881 case tsk_invalid_member_spec:
2882 /* The error has already been reported in
2883 check_specialization_scope. */
2884 return error_mark_node;
2885
2886 case tsk_invalid_expl_inst:
2887 error ("template parameter list used in explicit instantiation");
2888
2889 /* Fall through. */
2890
2891 case tsk_expl_inst:
2892 if (have_def)
2893 error ("definition provided for explicit instantiation");
2894
2895 explicit_instantiation = 1;
2896 break;
2897
2898 case tsk_excessive_parms:
2899 case tsk_insufficient_parms:
2900 if (tsk == tsk_excessive_parms)
2901 error ("too many template parameter lists in declaration of %qD",
2902 decl);
2903 else if (template_header_count)
2904 error("too few template parameter lists in declaration of %qD", decl);
2905 else
2906 error("explicit specialization of %qD must be introduced by "
2907 "%<template <>%>", decl);
2908
2909 /* Fall through. */
2910 case tsk_expl_spec:
2911 if (is_concept)
2912 error ("explicit specialization declared %<concept%>");
2913
2914 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2915 /* In cases like template<> constexpr bool v = true;
2916 We'll give an error in check_template_variable. */
2917 break;
2918
2919 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2920 if (ctype)
2921 member_specialization = 1;
2922 else
2923 specialization = 1;
2924 break;
2925
2926 case tsk_template:
2927 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2928 {
2929 /* This case handles bogus declarations like template <>
2930 template <class T> void f<int>(); */
2931
2932 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2933 error_at (cp_expr_loc_or_input_loc (declarator),
2934 "template-id %qE in declaration of primary template",
2935 declarator);
2936 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2937 {
2938 /* Partial specialization of variable template. */
2939 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2940 specialization = 1;
2941 goto ok;
2942 }
2943 else if (cxx_dialect < cxx14)
2944 error_at (cp_expr_loc_or_input_loc (declarator),
2945 "non-type partial specialization %qE "
2946 "is not allowed", declarator);
2947 else
2948 error_at (cp_expr_loc_or_input_loc (declarator),
2949 "non-class, non-variable partial specialization %qE "
2950 "is not allowed", declarator);
2951 return decl;
2952 ok:;
2953 }
2954
2955 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2956 /* This is a specialization of a member template, without
2957 specialization the containing class. Something like:
2958
2959 template <class T> struct S {
2960 template <class U> void f (U);
2961 };
2962 template <> template <class U> void S<int>::f(U) {}
2963
2964 That's a specialization -- but of the entire template. */
2965 specialization = 1;
2966 break;
2967
2968 default:
2969 gcc_unreachable ();
2970 }
2971
2972 if ((specialization || member_specialization)
2973 /* This doesn't apply to variable templates. */
2974 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2975 {
2976 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2977 for (; t; t = TREE_CHAIN (t))
2978 if (TREE_PURPOSE (t))
2979 {
2980 permerror (input_location,
2981 "default argument specified in explicit specialization");
2982 break;
2983 }
2984 }
2985
2986 if (specialization || member_specialization || explicit_instantiation)
2987 {
2988 tree tmpl = NULL_TREE;
2989 tree targs = NULL_TREE;
2990 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
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 /* If lookup fails, look for a friend declaration so we can
3012 give a better diagnostic. */
3013 fns = (lookup_qualified_name
3014 (CP_DECL_CONTEXT (decl), dname,
3015 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3016 /*complain*/true));
3017
3018 if (fns == error_mark_node || !is_overloaded_fn (fns))
3019 {
3020 error ("%qD is not a template function", dname);
3021 fns = error_mark_node;
3022 }
3023 }
3024
3025 declarator = lookup_template_function (fns, NULL_TREE);
3026 }
3027
3028 if (declarator == error_mark_node)
3029 return error_mark_node;
3030
3031 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3032 {
3033 if (!explicit_instantiation)
3034 /* A specialization in class scope. This is invalid,
3035 but the error will already have been flagged by
3036 check_specialization_scope. */
3037 return error_mark_node;
3038 else
3039 {
3040 /* It's not valid to write an explicit instantiation in
3041 class scope, e.g.:
3042
3043 class C { template void f(); }
3044
3045 This case is caught by the parser. However, on
3046 something like:
3047
3048 template class C { void f(); };
3049
3050 (which is invalid) we can get here. The error will be
3051 issued later. */
3052 ;
3053 }
3054
3055 return decl;
3056 }
3057 else if (ctype != NULL_TREE
3058 && (identifier_p (TREE_OPERAND (declarator, 0))))
3059 {
3060 // We'll match variable templates in start_decl.
3061 if (VAR_P (decl))
3062 return decl;
3063
3064 /* Find the list of functions in ctype that have the same
3065 name as the declared function. */
3066 tree name = TREE_OPERAND (declarator, 0);
3067
3068 if (constructor_name_p (name, ctype))
3069 {
3070 if (DECL_CONSTRUCTOR_P (decl)
3071 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3072 : !CLASSTYPE_DESTRUCTOR (ctype))
3073 {
3074 /* From [temp.expl.spec]:
3075
3076 If such an explicit specialization for the member
3077 of a class template names an implicitly-declared
3078 special member function (clause _special_), the
3079 program is ill-formed.
3080
3081 Similar language is found in [temp.explicit]. */
3082 error ("specialization of implicitly-declared special member function");
3083 return error_mark_node;
3084 }
3085
3086 name = DECL_NAME (decl);
3087 }
3088
3089 /* For a type-conversion operator, We might be looking for
3090 `operator int' which will be a specialization of
3091 `operator T'. Grab all the conversion operators, and
3092 then select from them. */
3093 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3094 ? conv_op_identifier : name);
3095
3096 if (fns == NULL_TREE)
3097 {
3098 error ("no member function %qD declared in %qT", name, ctype);
3099 return error_mark_node;
3100 }
3101 else
3102 TREE_OPERAND (declarator, 0) = fns;
3103 }
3104
3105 /* Figure out what exactly is being specialized at this point.
3106 Note that for an explicit instantiation, even one for a
3107 member function, we cannot tell a priori whether the
3108 instantiation is for a member template, or just a member
3109 function of a template class. Even if a member template is
3110 being instantiated, the member template arguments may be
3111 elided if they can be deduced from the rest of the
3112 declaration. */
3113 tmpl = determine_specialization (declarator, decl,
3114 &targs,
3115 member_specialization,
3116 template_count,
3117 tsk);
3118
3119 if (!tmpl || tmpl == error_mark_node)
3120 /* We couldn't figure out what this declaration was
3121 specializing. */
3122 return error_mark_node;
3123 else
3124 {
3125 if (TREE_CODE (decl) == FUNCTION_DECL
3126 && DECL_HIDDEN_FRIEND_P (tmpl))
3127 {
3128 auto_diagnostic_group d;
3129 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3130 "friend declaration %qD is not visible to "
3131 "explicit specialization", tmpl))
3132 inform (DECL_SOURCE_LOCATION (tmpl),
3133 "friend declaration here");
3134 }
3135 else if (!ctype && !is_friend
3136 && CP_DECL_CONTEXT (decl) == current_namespace)
3137 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3138
3139 tree gen_tmpl = most_general_template (tmpl);
3140
3141 if (explicit_instantiation)
3142 {
3143 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3144 is done by do_decl_instantiation later. */
3145
3146 int arg_depth = TMPL_ARGS_DEPTH (targs);
3147 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3148
3149 if (arg_depth > parm_depth)
3150 {
3151 /* If TMPL is not the most general template (for
3152 example, if TMPL is a friend template that is
3153 injected into namespace scope), then there will
3154 be too many levels of TARGS. Remove some of them
3155 here. */
3156 int i;
3157 tree new_targs;
3158
3159 new_targs = make_tree_vec (parm_depth);
3160 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3161 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3162 = TREE_VEC_ELT (targs, i);
3163 targs = new_targs;
3164 }
3165
3166 return instantiate_template (tmpl, targs, tf_error);
3167 }
3168
3169 /* If we thought that the DECL was a member function, but it
3170 turns out to be specializing a static member function,
3171 make DECL a static member function as well. */
3172 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3173 && DECL_STATIC_FUNCTION_P (tmpl)
3174 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3175 revert_static_member_fn (decl);
3176
3177 /* If this is a specialization of a member template of a
3178 template class, we want to return the TEMPLATE_DECL, not
3179 the specialization of it. */
3180 if (tsk == tsk_template && !was_template_id)
3181 {
3182 tree result = DECL_TEMPLATE_RESULT (tmpl);
3183 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3184 DECL_INITIAL (result) = NULL_TREE;
3185 if (have_def)
3186 {
3187 tree parm;
3188 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3189 DECL_SOURCE_LOCATION (result)
3190 = DECL_SOURCE_LOCATION (decl);
3191 /* We want to use the argument list specified in the
3192 definition, not in the original declaration. */
3193 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3194 for (parm = DECL_ARGUMENTS (result); parm;
3195 parm = DECL_CHAIN (parm))
3196 DECL_CONTEXT (parm) = result;
3197 }
3198 return register_specialization (tmpl, gen_tmpl, targs,
3199 is_friend, 0);
3200 }
3201
3202 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3203 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3204
3205 if (was_template_id)
3206 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3207
3208 /* Inherit default function arguments from the template
3209 DECL is specializing. */
3210 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3211 copy_default_args_to_explicit_spec (decl);
3212
3213 /* This specialization has the same protection as the
3214 template it specializes. */
3215 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3216 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3217
3218 /* 7.1.1-1 [dcl.stc]
3219
3220 A storage-class-specifier shall not be specified in an
3221 explicit specialization...
3222
3223 The parser rejects these, so unless action is taken here,
3224 explicit function specializations will always appear with
3225 global linkage.
3226
3227 The action recommended by the C++ CWG in response to C++
3228 defect report 605 is to make the storage class and linkage
3229 of the explicit specialization match the templated function:
3230
3231 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3232 */
3233 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3234 {
3235 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3236 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3237
3238 /* A concept cannot be specialized. */
3239 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3240 {
3241 error ("explicit specialization of function concept %qD",
3242 gen_tmpl);
3243 return error_mark_node;
3244 }
3245
3246 /* This specialization has the same linkage and visibility as
3247 the function template it specializes. */
3248 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3249 if (! TREE_PUBLIC (decl))
3250 {
3251 DECL_INTERFACE_KNOWN (decl) = 1;
3252 DECL_NOT_REALLY_EXTERN (decl) = 1;
3253 }
3254 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3255 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3256 {
3257 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3258 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3259 }
3260 }
3261
3262 /* If DECL is a friend declaration, declared using an
3263 unqualified name, the namespace associated with DECL may
3264 have been set incorrectly. For example, in:
3265
3266 template <typename T> void f(T);
3267 namespace N {
3268 struct S { friend void f<int>(int); }
3269 }
3270
3271 we will have set the DECL_CONTEXT for the friend
3272 declaration to N, rather than to the global namespace. */
3273 if (DECL_NAMESPACE_SCOPE_P (decl))
3274 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3275
3276 if (is_friend && !have_def)
3277 /* This is not really a declaration of a specialization.
3278 It's just the name of an instantiation. But, it's not
3279 a request for an instantiation, either. */
3280 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3281 else if (TREE_CODE (decl) == FUNCTION_DECL)
3282 /* A specialization is not necessarily COMDAT. */
3283 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3284 && DECL_DECLARED_INLINE_P (decl));
3285 else if (VAR_P (decl))
3286 DECL_COMDAT (decl) = false;
3287
3288 /* If this is a full specialization, register it so that we can find
3289 it again. Partial specializations will be registered in
3290 process_partial_specialization. */
3291 if (!processing_template_decl)
3292 {
3293 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3294
3295 decl = register_specialization (decl, gen_tmpl, targs,
3296 is_friend, 0);
3297 }
3298
3299
3300 /* A 'structor should already have clones. */
3301 gcc_assert (decl == error_mark_node
3302 || variable_template_p (tmpl)
3303 || !(DECL_CONSTRUCTOR_P (decl)
3304 || DECL_DESTRUCTOR_P (decl))
3305 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3306 }
3307 }
3308
3309 return decl;
3310 }
3311
3312 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3313 parameters. These are represented in the same format used for
3314 DECL_TEMPLATE_PARMS. */
3315
3316 int
3317 comp_template_parms (const_tree parms1, const_tree parms2)
3318 {
3319 const_tree p1;
3320 const_tree p2;
3321
3322 if (parms1 == parms2)
3323 return 1;
3324
3325 for (p1 = parms1, p2 = parms2;
3326 p1 != NULL_TREE && p2 != NULL_TREE;
3327 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3328 {
3329 tree t1 = TREE_VALUE (p1);
3330 tree t2 = TREE_VALUE (p2);
3331 int i;
3332
3333 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3334 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3335
3336 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3337 return 0;
3338
3339 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3340 {
3341 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3342 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3343
3344 /* If either of the template parameters are invalid, assume
3345 they match for the sake of error recovery. */
3346 if (error_operand_p (parm1) || error_operand_p (parm2))
3347 return 1;
3348
3349 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3350 return 0;
3351
3352 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3353 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3354 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3355 continue;
3356 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3357 return 0;
3358 }
3359 }
3360
3361 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3362 /* One set of parameters has more parameters lists than the
3363 other. */
3364 return 0;
3365
3366 return 1;
3367 }
3368
3369 /* Returns true if two template parameters are declared with
3370 equivalent constraints. */
3371
3372 static bool
3373 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3374 {
3375 tree req1 = TREE_TYPE (parm1);
3376 tree req2 = TREE_TYPE (parm2);
3377 if (!req1 != !req2)
3378 return false;
3379 if (req1)
3380 return cp_tree_equal (req1, req2);
3381 return true;
3382 }
3383
3384 /* Returns true when two template parameters are equivalent. */
3385
3386 static bool
3387 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3388 {
3389 tree decl1 = TREE_VALUE (parm1);
3390 tree decl2 = TREE_VALUE (parm2);
3391
3392 /* If either of the template parameters are invalid, assume
3393 they match for the sake of error recovery. */
3394 if (error_operand_p (decl1) || error_operand_p (decl2))
3395 return true;
3396
3397 /* ... they declare parameters of the same kind. */
3398 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3399 return false;
3400
3401 /* ... one parameter was introduced by a parameter declaration, then
3402 both are. This case arises as a result of eagerly rewriting declarations
3403 during parsing. */
3404 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3405 return false;
3406
3407 /* ... if either declares a pack, they both do. */
3408 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3409 return false;
3410
3411 if (TREE_CODE (decl1) == PARM_DECL)
3412 {
3413 /* ... if they declare non-type parameters, the types are equivalent. */
3414 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3415 return false;
3416 }
3417 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3418 {
3419 /* ... if they declare template template parameters, their template
3420 parameter lists are equivalent. */
3421 if (!template_heads_equivalent_p (decl1, decl2))
3422 return false;
3423 }
3424
3425 /* ... if they are declared with a qualified-concept name, they both
3426 are, and those names are equivalent. */
3427 return template_parameter_constraints_equivalent_p (parm1, parm2);
3428 }
3429
3430 /* Returns true if two template parameters lists are equivalent.
3431 Two template parameter lists are equivalent if they have the
3432 same length and their corresponding parameters are equivalent.
3433
3434 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3435 data structure returned by DECL_TEMPLATE_PARMS.
3436
3437 This is generally the same implementation as comp_template_parms
3438 except that it also the concept names and arguments used to
3439 introduce parameters. */
3440
3441 static bool
3442 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3443 {
3444 if (parms1 == parms2)
3445 return true;
3446
3447 const_tree p1 = parms1;
3448 const_tree p2 = parms2;
3449 while (p1 != NULL_TREE && p2 != NULL_TREE)
3450 {
3451 tree list1 = TREE_VALUE (p1);
3452 tree list2 = TREE_VALUE (p2);
3453
3454 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3455 return 0;
3456
3457 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3458 {
3459 tree parm1 = TREE_VEC_ELT (list1, i);
3460 tree parm2 = TREE_VEC_ELT (list2, i);
3461 if (!template_parameters_equivalent_p (parm1, parm2))
3462 return false;
3463 }
3464
3465 p1 = TREE_CHAIN (p1);
3466 p2 = TREE_CHAIN (p2);
3467 }
3468
3469 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3470 return false;
3471
3472 return true;
3473 }
3474
3475 /* Return true if the requires-clause of the template parameter lists are
3476 equivalent and false otherwise. */
3477 static bool
3478 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3479 {
3480 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3481 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3482 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3483 return false;
3484 if (!cp_tree_equal (req1, req2))
3485 return false;
3486 return true;
3487 }
3488
3489 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3490 Two template heads are equivalent if their template parameter
3491 lists are equivalent and their requires clauses are equivalent.
3492
3493 In pre-C++20, this is equivalent to calling comp_template_parms
3494 for the template parameters of TMPL1 and TMPL2. */
3495
3496 bool
3497 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3498 {
3499 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3500 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3501
3502 /* Don't change the matching rules for pre-C++20. */
3503 if (cxx_dialect < cxx20)
3504 return comp_template_parms (parms1, parms2);
3505
3506 /* ... have the same number of template parameters, and their
3507 corresponding parameters are equivalent. */
3508 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3509 return false;
3510
3511 /* ... if either has a requires-clause, they both do and their
3512 corresponding constraint-expressions are equivalent. */
3513 return template_requirements_equivalent_p (parms1, parms2);
3514 }
3515
3516 /* Determine whether PARM is a parameter pack. */
3517
3518 bool
3519 template_parameter_pack_p (const_tree parm)
3520 {
3521 /* Determine if we have a non-type template parameter pack. */
3522 if (TREE_CODE (parm) == PARM_DECL)
3523 return (DECL_TEMPLATE_PARM_P (parm)
3524 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3525 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3526 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3527
3528 /* If this is a list of template parameters, we could get a
3529 TYPE_DECL or a TEMPLATE_DECL. */
3530 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3531 parm = TREE_TYPE (parm);
3532
3533 /* Otherwise it must be a type template parameter. */
3534 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3535 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3536 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3537 }
3538
3539 /* Determine if T is a function parameter pack. */
3540
3541 bool
3542 function_parameter_pack_p (const_tree t)
3543 {
3544 if (t && TREE_CODE (t) == PARM_DECL)
3545 return DECL_PACK_P (t);
3546 return false;
3547 }
3548
3549 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3550 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3551
3552 tree
3553 get_function_template_decl (const_tree primary_func_tmpl_inst)
3554 {
3555 if (! primary_func_tmpl_inst
3556 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3557 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3558 return NULL;
3559
3560 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3561 }
3562
3563 /* Return true iff the function parameter PARAM_DECL was expanded
3564 from the function parameter pack PACK. */
3565
3566 bool
3567 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3568 {
3569 if (DECL_ARTIFICIAL (param_decl)
3570 || !function_parameter_pack_p (pack))
3571 return false;
3572
3573 /* The parameter pack and its pack arguments have the same
3574 DECL_PARM_INDEX. */
3575 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3576 }
3577
3578 /* Determine whether ARGS describes a variadic template args list,
3579 i.e., one that is terminated by a template argument pack. */
3580
3581 static bool
3582 template_args_variadic_p (tree args)
3583 {
3584 int nargs;
3585 tree last_parm;
3586
3587 if (args == NULL_TREE)
3588 return false;
3589
3590 args = INNERMOST_TEMPLATE_ARGS (args);
3591 nargs = TREE_VEC_LENGTH (args);
3592
3593 if (nargs == 0)
3594 return false;
3595
3596 last_parm = TREE_VEC_ELT (args, nargs - 1);
3597
3598 return ARGUMENT_PACK_P (last_parm);
3599 }
3600
3601 /* Generate a new name for the parameter pack name NAME (an
3602 IDENTIFIER_NODE) that incorporates its */
3603
3604 static tree
3605 make_ith_pack_parameter_name (tree name, int i)
3606 {
3607 /* Munge the name to include the parameter index. */
3608 #define NUMBUF_LEN 128
3609 char numbuf[NUMBUF_LEN];
3610 char* newname;
3611 int newname_len;
3612
3613 if (name == NULL_TREE)
3614 return name;
3615 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3616 newname_len = IDENTIFIER_LENGTH (name)
3617 + strlen (numbuf) + 2;
3618 newname = (char*)alloca (newname_len);
3619 snprintf (newname, newname_len,
3620 "%s#%i", IDENTIFIER_POINTER (name), i);
3621 return get_identifier (newname);
3622 }
3623
3624 /* Return true if T is a primary function, class or alias template
3625 specialization, not including the template pattern. */
3626
3627 bool
3628 primary_template_specialization_p (const_tree t)
3629 {
3630 if (!t)
3631 return false;
3632
3633 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3634 return (DECL_LANG_SPECIFIC (t)
3635 && DECL_USE_TEMPLATE (t)
3636 && DECL_TEMPLATE_INFO (t)
3637 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3638 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3639 return (CLASSTYPE_TEMPLATE_INFO (t)
3640 && CLASSTYPE_USE_TEMPLATE (t)
3641 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3642 else if (alias_template_specialization_p (t, nt_transparent))
3643 return true;
3644 return false;
3645 }
3646
3647 /* Return true if PARM is a template template parameter. */
3648
3649 bool
3650 template_template_parameter_p (const_tree parm)
3651 {
3652 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3653 }
3654
3655 /* Return true iff PARM is a DECL representing a type template
3656 parameter. */
3657
3658 bool
3659 template_type_parameter_p (const_tree parm)
3660 {
3661 return (parm
3662 && (TREE_CODE (parm) == TYPE_DECL
3663 || TREE_CODE (parm) == TEMPLATE_DECL)
3664 && DECL_TEMPLATE_PARM_P (parm));
3665 }
3666
3667 /* Return the template parameters of T if T is a
3668 primary template instantiation, NULL otherwise. */
3669
3670 tree
3671 get_primary_template_innermost_parameters (const_tree t)
3672 {
3673 tree parms = NULL, template_info = NULL;
3674
3675 if ((template_info = get_template_info (t))
3676 && primary_template_specialization_p (t))
3677 parms = INNERMOST_TEMPLATE_PARMS
3678 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3679
3680 return parms;
3681 }
3682
3683 /* Return the template parameters of the LEVELth level from the full list
3684 of template parameters PARMS. */
3685
3686 tree
3687 get_template_parms_at_level (tree parms, int level)
3688 {
3689 tree p;
3690 if (!parms
3691 || TREE_CODE (parms) != TREE_LIST
3692 || level > TMPL_PARMS_DEPTH (parms))
3693 return NULL_TREE;
3694
3695 for (p = parms; p; p = TREE_CHAIN (p))
3696 if (TMPL_PARMS_DEPTH (p) == level)
3697 return p;
3698
3699 return NULL_TREE;
3700 }
3701
3702 /* Returns the template arguments of T if T is a template instantiation,
3703 NULL otherwise. */
3704
3705 tree
3706 get_template_innermost_arguments (const_tree t)
3707 {
3708 tree args = NULL, template_info = NULL;
3709
3710 if ((template_info = get_template_info (t))
3711 && TI_ARGS (template_info))
3712 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3713
3714 return args;
3715 }
3716
3717 /* Return the argument pack elements of T if T is a template argument pack,
3718 NULL otherwise. */
3719
3720 tree
3721 get_template_argument_pack_elems (const_tree t)
3722 {
3723 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3724 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3725 return NULL;
3726
3727 return ARGUMENT_PACK_ARGS (t);
3728 }
3729
3730 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3731 ARGUMENT_PACK_SELECT represents. */
3732
3733 static tree
3734 argument_pack_select_arg (tree t)
3735 {
3736 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3737 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3738
3739 /* If the selected argument is an expansion E, that most likely means we were
3740 called from gen_elem_of_pack_expansion_instantiation during the
3741 substituting of an argument pack (of which the Ith element is a pack
3742 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3743 In this case, the Ith element resulting from this substituting is going to
3744 be a pack expansion, which pattern is the pattern of E. Let's return the
3745 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3746 resulting pack expansion from it. */
3747 if (PACK_EXPANSION_P (arg))
3748 {
3749 /* Make sure we aren't throwing away arg info. */
3750 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3751 arg = PACK_EXPANSION_PATTERN (arg);
3752 }
3753
3754 return arg;
3755 }
3756
3757
3758 /* True iff FN is a function representing a built-in variadic parameter
3759 pack. */
3760
3761 bool
3762 builtin_pack_fn_p (tree fn)
3763 {
3764 if (!fn
3765 || TREE_CODE (fn) != FUNCTION_DECL
3766 || !DECL_IS_BUILTIN (fn))
3767 return false;
3768
3769 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3770 return true;
3771
3772 return false;
3773 }
3774
3775 /* True iff CALL is a call to a function representing a built-in variadic
3776 parameter pack. */
3777
3778 static bool
3779 builtin_pack_call_p (tree call)
3780 {
3781 if (TREE_CODE (call) != CALL_EXPR)
3782 return false;
3783 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3784 }
3785
3786 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3787
3788 static tree
3789 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3790 tree in_decl)
3791 {
3792 tree ohi = CALL_EXPR_ARG (call, 0);
3793 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3794 false/*fn*/, true/*int_cst*/);
3795
3796 if (value_dependent_expression_p (hi))
3797 {
3798 if (hi != ohi)
3799 {
3800 call = copy_node (call);
3801 CALL_EXPR_ARG (call, 0) = hi;
3802 }
3803 tree ex = make_pack_expansion (call, complain);
3804 tree vec = make_tree_vec (1);
3805 TREE_VEC_ELT (vec, 0) = ex;
3806 return vec;
3807 }
3808 else
3809 {
3810 hi = cxx_constant_value (hi);
3811 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3812
3813 /* Calculate the largest value of len that won't make the size of the vec
3814 overflow an int. The compiler will exceed resource limits long before
3815 this, but it seems a decent place to diagnose. */
3816 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3817
3818 if (len < 0 || len > max)
3819 {
3820 if ((complain & tf_error)
3821 && hi != error_mark_node)
3822 error ("argument to %<__integer_pack%> must be between 0 and %d",
3823 max);
3824 return error_mark_node;
3825 }
3826
3827 tree vec = make_tree_vec (len);
3828
3829 for (int i = 0; i < len; ++i)
3830 TREE_VEC_ELT (vec, i) = size_int (i);
3831
3832 return vec;
3833 }
3834 }
3835
3836 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3837 CALL. */
3838
3839 static tree
3840 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3841 tree in_decl)
3842 {
3843 if (!builtin_pack_call_p (call))
3844 return NULL_TREE;
3845
3846 tree fn = CALL_EXPR_FN (call);
3847
3848 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3849 return expand_integer_pack (call, args, complain, in_decl);
3850
3851 return NULL_TREE;
3852 }
3853
3854 /* Structure used to track the progress of find_parameter_packs_r. */
3855 struct find_parameter_pack_data
3856 {
3857 /* TREE_LIST that will contain all of the parameter packs found by
3858 the traversal. */
3859 tree* parameter_packs;
3860
3861 /* Set of AST nodes that have been visited by the traversal. */
3862 hash_set<tree> *visited;
3863
3864 /* True iff we're making a type pack expansion. */
3865 bool type_pack_expansion_p;
3866 };
3867
3868 /* Identifies all of the argument packs that occur in a template
3869 argument and appends them to the TREE_LIST inside DATA, which is a
3870 find_parameter_pack_data structure. This is a subroutine of
3871 make_pack_expansion and uses_parameter_packs. */
3872 static tree
3873 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3874 {
3875 tree t = *tp;
3876 struct find_parameter_pack_data* ppd =
3877 (struct find_parameter_pack_data*)data;
3878 bool parameter_pack_p = false;
3879
3880 /* Don't look through typedefs; we are interested in whether a
3881 parameter pack is actually written in the expression/type we're
3882 looking at, not the target type. */
3883 if (TYPE_P (t) && typedef_variant_p (t))
3884 {
3885 /* But do look at arguments for an alias template. */
3886 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3887 cp_walk_tree (&TI_ARGS (tinfo),
3888 &find_parameter_packs_r,
3889 ppd, ppd->visited);
3890 *walk_subtrees = 0;
3891 return NULL_TREE;
3892 }
3893
3894 /* Identify whether this is a parameter pack or not. */
3895 switch (TREE_CODE (t))
3896 {
3897 case TEMPLATE_PARM_INDEX:
3898 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3899 parameter_pack_p = true;
3900 break;
3901
3902 case TEMPLATE_TYPE_PARM:
3903 t = TYPE_MAIN_VARIANT (t);
3904 /* FALLTHRU */
3905 case TEMPLATE_TEMPLATE_PARM:
3906 /* If the placeholder appears in the decl-specifier-seq of a function
3907 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3908 is a pack expansion, the invented template parameter is a template
3909 parameter pack. */
3910 if (ppd->type_pack_expansion_p && is_auto (t))
3911 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3912 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3913 parameter_pack_p = true;
3914 break;
3915
3916 case FIELD_DECL:
3917 case PARM_DECL:
3918 if (DECL_PACK_P (t))
3919 {
3920 /* We don't want to walk into the type of a PARM_DECL,
3921 because we don't want to see the type parameter pack. */
3922 *walk_subtrees = 0;
3923 parameter_pack_p = true;
3924 }
3925 break;
3926
3927 case VAR_DECL:
3928 if (DECL_PACK_P (t))
3929 {
3930 /* We don't want to walk into the type of a variadic capture proxy,
3931 because we don't want to see the type parameter pack. */
3932 *walk_subtrees = 0;
3933 parameter_pack_p = true;
3934 }
3935 else if (variable_template_specialization_p (t))
3936 {
3937 cp_walk_tree (&DECL_TI_ARGS (t),
3938 find_parameter_packs_r,
3939 ppd, ppd->visited);
3940 *walk_subtrees = 0;
3941 }
3942 break;
3943
3944 case CALL_EXPR:
3945 if (builtin_pack_call_p (t))
3946 parameter_pack_p = true;
3947 break;
3948
3949 case BASES:
3950 parameter_pack_p = true;
3951 break;
3952 default:
3953 /* Not a parameter pack. */
3954 break;
3955 }
3956
3957 if (parameter_pack_p)
3958 {
3959 /* Add this parameter pack to the list. */
3960 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3961 }
3962
3963 if (TYPE_P (t))
3964 cp_walk_tree (&TYPE_CONTEXT (t),
3965 &find_parameter_packs_r, ppd, ppd->visited);
3966
3967 /* This switch statement will return immediately if we don't find a
3968 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3969 switch (TREE_CODE (t))
3970 {
3971 case BOUND_TEMPLATE_TEMPLATE_PARM:
3972 /* Check the template itself. */
3973 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3974 &find_parameter_packs_r, ppd, ppd->visited);
3975 return NULL_TREE;
3976
3977 case DECL_EXPR:
3978 {
3979 tree decl = DECL_EXPR_DECL (t);
3980 /* Ignore the declaration of a capture proxy for a parameter pack. */
3981 if (is_capture_proxy (decl))
3982 *walk_subtrees = 0;
3983 if (is_typedef_decl (decl))
3984 /* Since we stop at typedefs above, we need to look through them at
3985 the point of the DECL_EXPR. */
3986 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3987 &find_parameter_packs_r, ppd, ppd->visited);
3988 return NULL_TREE;
3989 }
3990
3991 case TEMPLATE_DECL:
3992 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3993 return NULL_TREE;
3994 cp_walk_tree (&TREE_TYPE (t),
3995 &find_parameter_packs_r, ppd, ppd->visited);
3996 return NULL_TREE;
3997
3998 case TYPE_PACK_EXPANSION:
3999 case EXPR_PACK_EXPANSION:
4000 *walk_subtrees = 0;
4001 return NULL_TREE;
4002
4003 case INTEGER_TYPE:
4004 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4005 ppd, ppd->visited);
4006 *walk_subtrees = 0;
4007 return NULL_TREE;
4008
4009 case IDENTIFIER_NODE:
4010 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4011 ppd->visited);
4012 *walk_subtrees = 0;
4013 return NULL_TREE;
4014
4015 case LAMBDA_EXPR:
4016 {
4017 /* Since we defer implicit capture, look in the parms and body. */
4018 tree fn = lambda_function (t);
4019 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4020 ppd->visited);
4021 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4022 ppd->visited);
4023 return NULL_TREE;
4024 }
4025
4026 case DECLTYPE_TYPE:
4027 {
4028 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4029 type_pack_expansion_p to false so that any placeholders
4030 within the expression don't get marked as parameter packs. */
4031 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4032 ppd->type_pack_expansion_p = false;
4033 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4034 ppd, ppd->visited);
4035 ppd->type_pack_expansion_p = type_pack_expansion_p;
4036 *walk_subtrees = 0;
4037 return NULL_TREE;
4038 }
4039
4040 case IF_STMT:
4041 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4042 ppd, ppd->visited);
4043 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4044 ppd, ppd->visited);
4045 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4046 ppd, ppd->visited);
4047 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4048 *walk_subtrees = 0;
4049 return NULL_TREE;
4050
4051 default:
4052 return NULL_TREE;
4053 }
4054
4055 return NULL_TREE;
4056 }
4057
4058 /* Determines if the expression or type T uses any parameter packs. */
4059 tree
4060 uses_parameter_packs (tree t)
4061 {
4062 tree parameter_packs = NULL_TREE;
4063 struct find_parameter_pack_data ppd;
4064 ppd.parameter_packs = &parameter_packs;
4065 ppd.visited = new hash_set<tree>;
4066 ppd.type_pack_expansion_p = false;
4067 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4068 delete ppd.visited;
4069 return parameter_packs;
4070 }
4071
4072 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4073 representation a base-class initializer into a parameter pack
4074 expansion. If all goes well, the resulting node will be an
4075 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4076 respectively. */
4077 tree
4078 make_pack_expansion (tree arg, tsubst_flags_t complain)
4079 {
4080 tree result;
4081 tree parameter_packs = NULL_TREE;
4082 bool for_types = false;
4083 struct find_parameter_pack_data ppd;
4084
4085 if (!arg || arg == error_mark_node)
4086 return arg;
4087
4088 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4089 {
4090 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4091 class initializer. In this case, the TREE_PURPOSE will be a
4092 _TYPE node (representing the base class expansion we're
4093 initializing) and the TREE_VALUE will be a TREE_LIST
4094 containing the initialization arguments.
4095
4096 The resulting expansion looks somewhat different from most
4097 expansions. Rather than returning just one _EXPANSION, we
4098 return a TREE_LIST whose TREE_PURPOSE is a
4099 TYPE_PACK_EXPANSION containing the bases that will be
4100 initialized. The TREE_VALUE will be identical to the
4101 original TREE_VALUE, which is a list of arguments that will
4102 be passed to each base. We do not introduce any new pack
4103 expansion nodes into the TREE_VALUE (although it is possible
4104 that some already exist), because the TREE_PURPOSE and
4105 TREE_VALUE all need to be expanded together with the same
4106 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4107 resulting TREE_PURPOSE will mention the parameter packs in
4108 both the bases and the arguments to the bases. */
4109 tree purpose;
4110 tree value;
4111 tree parameter_packs = NULL_TREE;
4112
4113 /* Determine which parameter packs will be used by the base
4114 class expansion. */
4115 ppd.visited = new hash_set<tree>;
4116 ppd.parameter_packs = &parameter_packs;
4117 ppd.type_pack_expansion_p = false;
4118 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4119 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4120 &ppd, ppd.visited);
4121
4122 if (parameter_packs == NULL_TREE)
4123 {
4124 if (complain & tf_error)
4125 error ("base initializer expansion %qT contains no parameter packs",
4126 arg);
4127 delete ppd.visited;
4128 return error_mark_node;
4129 }
4130
4131 if (TREE_VALUE (arg) != void_type_node)
4132 {
4133 /* Collect the sets of parameter packs used in each of the
4134 initialization arguments. */
4135 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4136 {
4137 /* Determine which parameter packs will be expanded in this
4138 argument. */
4139 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4140 &ppd, ppd.visited);
4141 }
4142 }
4143
4144 delete ppd.visited;
4145
4146 /* Create the pack expansion type for the base type. */
4147 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4148 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4149 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4150 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4151
4152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4153 they will rarely be compared to anything. */
4154 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4155
4156 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4157 }
4158
4159 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4160 for_types = true;
4161
4162 /* Build the PACK_EXPANSION_* node. */
4163 result = for_types
4164 ? cxx_make_type (TYPE_PACK_EXPANSION)
4165 : make_node (EXPR_PACK_EXPANSION);
4166 SET_PACK_EXPANSION_PATTERN (result, arg);
4167 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4168 {
4169 /* Propagate type and const-expression information. */
4170 TREE_TYPE (result) = TREE_TYPE (arg);
4171 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4172 /* Mark this read now, since the expansion might be length 0. */
4173 mark_exp_read (arg);
4174 }
4175 else
4176 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4177 they will rarely be compared to anything. */
4178 SET_TYPE_STRUCTURAL_EQUALITY (result);
4179
4180 /* Determine which parameter packs will be expanded. */
4181 ppd.parameter_packs = &parameter_packs;
4182 ppd.visited = new hash_set<tree>;
4183 ppd.type_pack_expansion_p = TYPE_P (arg);
4184 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4185 delete ppd.visited;
4186
4187 /* Make sure we found some parameter packs. */
4188 if (parameter_packs == NULL_TREE)
4189 {
4190 if (complain & tf_error)
4191 {
4192 if (TYPE_P (arg))
4193 error ("expansion pattern %qT contains no parameter packs", arg);
4194 else
4195 error ("expansion pattern %qE contains no parameter packs", arg);
4196 }
4197 return error_mark_node;
4198 }
4199 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4200
4201 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4202
4203 return result;
4204 }
4205
4206 /* Checks T for any "bare" parameter packs, which have not yet been
4207 expanded, and issues an error if any are found. This operation can
4208 only be done on full expressions or types (e.g., an expression
4209 statement, "if" condition, etc.), because we could have expressions like:
4210
4211 foo(f(g(h(args)))...)
4212
4213 where "args" is a parameter pack. check_for_bare_parameter_packs
4214 should not be called for the subexpressions args, h(args),
4215 g(h(args)), or f(g(h(args))), because we would produce erroneous
4216 error messages.
4217
4218 Returns TRUE and emits an error if there were bare parameter packs,
4219 returns FALSE otherwise. */
4220 bool
4221 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4222 {
4223 tree parameter_packs = NULL_TREE;
4224 struct find_parameter_pack_data ppd;
4225
4226 if (!processing_template_decl || !t || t == error_mark_node)
4227 return false;
4228
4229 /* A lambda might use a parameter pack from the containing context. */
4230 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4231 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4232 return false;
4233
4234 if (TREE_CODE (t) == TYPE_DECL)
4235 t = TREE_TYPE (t);
4236
4237 ppd.parameter_packs = &parameter_packs;
4238 ppd.visited = new hash_set<tree>;
4239 ppd.type_pack_expansion_p = false;
4240 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4241 delete ppd.visited;
4242
4243 if (parameter_packs)
4244 {
4245 if (loc == UNKNOWN_LOCATION)
4246 loc = cp_expr_loc_or_input_loc (t);
4247 error_at (loc, "parameter packs not expanded with %<...%>:");
4248 while (parameter_packs)
4249 {
4250 tree pack = TREE_VALUE (parameter_packs);
4251 tree name = NULL_TREE;
4252
4253 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4254 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4255 name = TYPE_NAME (pack);
4256 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4257 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4258 else if (TREE_CODE (pack) == CALL_EXPR)
4259 name = DECL_NAME (CALL_EXPR_FN (pack));
4260 else
4261 name = DECL_NAME (pack);
4262
4263 if (name)
4264 inform (loc, " %qD", name);
4265 else
4266 inform (loc, " %s", "<anonymous>");
4267
4268 parameter_packs = TREE_CHAIN (parameter_packs);
4269 }
4270
4271 return true;
4272 }
4273
4274 return false;
4275 }
4276
4277 /* Expand any parameter packs that occur in the template arguments in
4278 ARGS. */
4279 tree
4280 expand_template_argument_pack (tree args)
4281 {
4282 if (args == error_mark_node)
4283 return error_mark_node;
4284
4285 tree result_args = NULL_TREE;
4286 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4287 int num_result_args = -1;
4288 int non_default_args_count = -1;
4289
4290 /* First, determine if we need to expand anything, and the number of
4291 slots we'll need. */
4292 for (in_arg = 0; in_arg < nargs; ++in_arg)
4293 {
4294 tree arg = TREE_VEC_ELT (args, in_arg);
4295 if (arg == NULL_TREE)
4296 return args;
4297 if (ARGUMENT_PACK_P (arg))
4298 {
4299 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4300 if (num_result_args < 0)
4301 num_result_args = in_arg + num_packed;
4302 else
4303 num_result_args += num_packed;
4304 }
4305 else
4306 {
4307 if (num_result_args >= 0)
4308 num_result_args++;
4309 }
4310 }
4311
4312 /* If no expansion is necessary, we're done. */
4313 if (num_result_args < 0)
4314 return args;
4315
4316 /* Expand arguments. */
4317 result_args = make_tree_vec (num_result_args);
4318 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4319 non_default_args_count =
4320 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4321 for (in_arg = 0; in_arg < nargs; ++in_arg)
4322 {
4323 tree arg = TREE_VEC_ELT (args, in_arg);
4324 if (ARGUMENT_PACK_P (arg))
4325 {
4326 tree packed = ARGUMENT_PACK_ARGS (arg);
4327 int i, num_packed = TREE_VEC_LENGTH (packed);
4328 for (i = 0; i < num_packed; ++i, ++out_arg)
4329 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4330 if (non_default_args_count > 0)
4331 non_default_args_count += num_packed - 1;
4332 }
4333 else
4334 {
4335 TREE_VEC_ELT (result_args, out_arg) = arg;
4336 ++out_arg;
4337 }
4338 }
4339 if (non_default_args_count >= 0)
4340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4341 return result_args;
4342 }
4343
4344 /* Checks if DECL shadows a template parameter.
4345
4346 [temp.local]: A template-parameter shall not be redeclared within its
4347 scope (including nested scopes).
4348
4349 Emits an error and returns TRUE if the DECL shadows a parameter,
4350 returns FALSE otherwise. */
4351
4352 bool
4353 check_template_shadow (tree decl)
4354 {
4355 tree olddecl;
4356
4357 /* If we're not in a template, we can't possibly shadow a template
4358 parameter. */
4359 if (!current_template_parms)
4360 return true;
4361
4362 /* Figure out what we're shadowing. */
4363 decl = OVL_FIRST (decl);
4364 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4365
4366 /* If there's no previous binding for this name, we're not shadowing
4367 anything, let alone a template parameter. */
4368 if (!olddecl)
4369 return true;
4370
4371 /* If we're not shadowing a template parameter, we're done. Note
4372 that OLDDECL might be an OVERLOAD (or perhaps even an
4373 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4374 node. */
4375 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4376 return true;
4377
4378 /* We check for decl != olddecl to avoid bogus errors for using a
4379 name inside a class. We check TPFI to avoid duplicate errors for
4380 inline member templates. */
4381 if (decl == olddecl
4382 || (DECL_TEMPLATE_PARM_P (decl)
4383 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4384 return true;
4385
4386 /* Don't complain about the injected class name, as we've already
4387 complained about the class itself. */
4388 if (DECL_SELF_REFERENCE_P (decl))
4389 return false;
4390
4391 if (DECL_TEMPLATE_PARM_P (decl))
4392 error ("declaration of template parameter %q+D shadows "
4393 "template parameter", decl);
4394 else
4395 error ("declaration of %q+#D shadows template parameter", decl);
4396 inform (DECL_SOURCE_LOCATION (olddecl),
4397 "template parameter %qD declared here", olddecl);
4398 return false;
4399 }
4400
4401 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4402 ORIG_LEVEL, DECL, and TYPE. */
4403
4404 static tree
4405 build_template_parm_index (int index,
4406 int level,
4407 int orig_level,
4408 tree decl,
4409 tree type)
4410 {
4411 tree t = make_node (TEMPLATE_PARM_INDEX);
4412 TEMPLATE_PARM_IDX (t) = index;
4413 TEMPLATE_PARM_LEVEL (t) = level;
4414 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4415 TEMPLATE_PARM_DECL (t) = decl;
4416 TREE_TYPE (t) = type;
4417 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4418 TREE_READONLY (t) = TREE_READONLY (decl);
4419
4420 return t;
4421 }
4422
4423 /* Find the canonical type parameter for the given template type
4424 parameter. Returns the canonical type parameter, which may be TYPE
4425 if no such parameter existed. */
4426
4427 static tree
4428 canonical_type_parameter (tree type)
4429 {
4430 int idx = TEMPLATE_TYPE_IDX (type);
4431
4432 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4433
4434 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4435 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4436
4437 for (tree list = (*canonical_template_parms)[idx];
4438 list; list = TREE_CHAIN (list))
4439 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4440 return TREE_VALUE (list);
4441
4442 (*canonical_template_parms)[idx]
4443 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4444 return type;
4445 }
4446
4447 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4448 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4449 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4450 new one is created. */
4451
4452 static tree
4453 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4454 tsubst_flags_t complain)
4455 {
4456 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4457 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4458 != TEMPLATE_PARM_LEVEL (index) - levels)
4459 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4460 {
4461 tree orig_decl = TEMPLATE_PARM_DECL (index);
4462
4463 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4464 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4465 type);
4466 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4467 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4468 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4469 DECL_ARTIFICIAL (decl) = 1;
4470 SET_DECL_TEMPLATE_PARM_P (decl);
4471
4472 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4473 TEMPLATE_PARM_LEVEL (index) - levels,
4474 TEMPLATE_PARM_ORIG_LEVEL (index),
4475 decl, type);
4476 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4477 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4478 = TEMPLATE_PARM_PARAMETER_PACK (index);
4479
4480 /* Template template parameters need this. */
4481 tree inner = decl;
4482 if (TREE_CODE (decl) == TEMPLATE_DECL)
4483 {
4484 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4485 TYPE_DECL, DECL_NAME (decl), type);
4486 DECL_TEMPLATE_RESULT (decl) = inner;
4487 DECL_ARTIFICIAL (inner) = true;
4488 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4489 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4490 }
4491
4492 /* Attach the TPI to the decl. */
4493 if (TREE_CODE (inner) == TYPE_DECL)
4494 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4495 else
4496 DECL_INITIAL (decl) = tpi;
4497 }
4498
4499 return TEMPLATE_PARM_DESCENDANTS (index);
4500 }
4501
4502 /* Process information from new template parameter PARM and append it
4503 to the LIST being built. This new parameter is a non-type
4504 parameter iff IS_NON_TYPE is true. This new parameter is a
4505 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4506 is in PARM_LOC. */
4507
4508 tree
4509 process_template_parm (tree list, location_t parm_loc, tree parm,
4510 bool is_non_type, bool is_parameter_pack)
4511 {
4512 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4513 tree prev = NULL_TREE;
4514 int idx = 0;
4515
4516 if (list)
4517 {
4518 prev = tree_last (list);
4519
4520 tree p = TREE_VALUE (prev);
4521 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4522 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4523 else if (TREE_CODE (p) == PARM_DECL)
4524 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4525
4526 ++idx;
4527 }
4528
4529 tree decl = NULL_TREE;
4530 tree defval = TREE_PURPOSE (parm);
4531 tree constr = TREE_TYPE (parm);
4532
4533 if (is_non_type)
4534 {
4535 parm = TREE_VALUE (parm);
4536
4537 SET_DECL_TEMPLATE_PARM_P (parm);
4538
4539 if (TREE_TYPE (parm) != error_mark_node)
4540 {
4541 /* [temp.param]
4542
4543 The top-level cv-qualifiers on the template-parameter are
4544 ignored when determining its type. */
4545 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4546 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4547 TREE_TYPE (parm) = error_mark_node;
4548 else if (uses_parameter_packs (TREE_TYPE (parm))
4549 && !is_parameter_pack
4550 /* If we're in a nested template parameter list, the template
4551 template parameter could be a parameter pack. */
4552 && processing_template_parmlist == 1)
4553 {
4554 /* This template parameter is not a parameter pack, but it
4555 should be. Complain about "bare" parameter packs. */
4556 check_for_bare_parameter_packs (TREE_TYPE (parm));
4557
4558 /* Recover by calling this a parameter pack. */
4559 is_parameter_pack = true;
4560 }
4561 }
4562
4563 /* A template parameter is not modifiable. */
4564 TREE_CONSTANT (parm) = 1;
4565 TREE_READONLY (parm) = 1;
4566 decl = build_decl (parm_loc,
4567 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4568 TREE_CONSTANT (decl) = 1;
4569 TREE_READONLY (decl) = 1;
4570 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4571 = build_template_parm_index (idx, processing_template_decl,
4572 processing_template_decl,
4573 decl, TREE_TYPE (parm));
4574
4575 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4576 = is_parameter_pack;
4577 }
4578 else
4579 {
4580 tree t;
4581 parm = TREE_VALUE (TREE_VALUE (parm));
4582
4583 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4584 {
4585 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4586 /* This is for distinguishing between real templates and template
4587 template parameters */
4588 TREE_TYPE (parm) = t;
4589
4590 /* any_template_parm_r expects to be able to get the targs of a
4591 DECL_TEMPLATE_RESULT. */
4592 tree result = DECL_TEMPLATE_RESULT (parm);
4593 TREE_TYPE (result) = t;
4594 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4595 tree tinfo = build_template_info (parm, args);
4596 retrofit_lang_decl (result);
4597 DECL_TEMPLATE_INFO (result) = tinfo;
4598
4599 decl = parm;
4600 }
4601 else
4602 {
4603 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4604 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4605 decl = build_decl (parm_loc,
4606 TYPE_DECL, parm, t);
4607 }
4608
4609 TYPE_NAME (t) = decl;
4610 TYPE_STUB_DECL (t) = decl;
4611 parm = decl;
4612 TEMPLATE_TYPE_PARM_INDEX (t)
4613 = build_template_parm_index (idx, processing_template_decl,
4614 processing_template_decl,
4615 decl, TREE_TYPE (parm));
4616 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4617 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4618 SET_TYPE_STRUCTURAL_EQUALITY (t);
4619 else
4620 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4621 }
4622 DECL_ARTIFICIAL (decl) = 1;
4623 SET_DECL_TEMPLATE_PARM_P (decl);
4624
4625 /* Build requirements for the type/template parameter.
4626 This must be done after SET_DECL_TEMPLATE_PARM_P or
4627 process_template_parm could fail. */
4628 tree reqs = finish_shorthand_constraint (parm, constr);
4629
4630 decl = pushdecl (decl);
4631 if (!is_non_type)
4632 parm = decl;
4633
4634 /* Build the parameter node linking the parameter declaration,
4635 its default argument (if any), and its constraints (if any). */
4636 parm = build_tree_list (defval, parm);
4637 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4638
4639 if (prev)
4640 TREE_CHAIN (prev) = parm;
4641 else
4642 list = parm;
4643
4644 return list;
4645 }
4646
4647 /* The end of a template parameter list has been reached. Process the
4648 tree list into a parameter vector, converting each parameter into a more
4649 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4650 as PARM_DECLs. */
4651
4652 tree
4653 end_template_parm_list (tree parms)
4654 {
4655 tree saved_parmlist = make_tree_vec (list_length (parms));
4656
4657 /* Pop the dummy parameter level and add the real one. We do not
4658 morph the dummy parameter in place, as it might have been
4659 captured by a (nested) template-template-parm. */
4660 current_template_parms = TREE_CHAIN (current_template_parms);
4661
4662 current_template_parms
4663 = tree_cons (size_int (processing_template_decl),
4664 saved_parmlist, current_template_parms);
4665
4666 for (unsigned ix = 0; parms; ix++)
4667 {
4668 tree parm = parms;
4669 parms = TREE_CHAIN (parms);
4670 TREE_CHAIN (parm) = NULL_TREE;
4671
4672 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4673 }
4674
4675 --processing_template_parmlist;
4676
4677 return saved_parmlist;
4678 }
4679
4680 // Explicitly indicate the end of the template parameter list. We assume
4681 // that the current template parameters have been constructed and/or
4682 // managed explicitly, as when creating new template template parameters
4683 // from a shorthand constraint.
4684 void
4685 end_template_parm_list ()
4686 {
4687 --processing_template_parmlist;
4688 }
4689
4690 /* end_template_decl is called after a template declaration is seen. */
4691
4692 void
4693 end_template_decl (void)
4694 {
4695 reset_specialization ();
4696
4697 if (! processing_template_decl)
4698 return;
4699
4700 /* This matches the pushlevel in begin_template_parm_list. */
4701 finish_scope ();
4702
4703 --processing_template_decl;
4704 current_template_parms = TREE_CHAIN (current_template_parms);
4705 }
4706
4707 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4708 thereof, and converts it into an argument suitable to be passed to
4709 the type substitution functions. Note that if the TREE_LIST contains
4710 an error_mark node, the returned argument is error_mark_node. */
4711
4712 tree
4713 template_parm_to_arg (tree t)
4714 {
4715 if (!t)
4716 return NULL_TREE;
4717
4718 if (TREE_CODE (t) == TREE_LIST)
4719 t = TREE_VALUE (t);
4720
4721 if (error_operand_p (t))
4722 return error_mark_node;
4723
4724 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4725 {
4726 if (TREE_CODE (t) == TYPE_DECL
4727 || TREE_CODE (t) == TEMPLATE_DECL)
4728 t = TREE_TYPE (t);
4729 else
4730 t = DECL_INITIAL (t);
4731 }
4732
4733 gcc_assert (TEMPLATE_PARM_P (t));
4734
4735 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4736 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4737 {
4738 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4739 {
4740 /* Turn this argument into a TYPE_ARGUMENT_PACK
4741 with a single element, which expands T. */
4742 tree vec = make_tree_vec (1);
4743 if (CHECKING_P)
4744 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4745
4746 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4747
4748 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4749 SET_ARGUMENT_PACK_ARGS (t, vec);
4750 }
4751 }
4752 else
4753 {
4754 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4755 {
4756 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4757 with a single element, which expands T. */
4758 tree vec = make_tree_vec (1);
4759 if (CHECKING_P)
4760 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4761
4762 t = convert_from_reference (t);
4763 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4764
4765 t = make_node (NONTYPE_ARGUMENT_PACK);
4766 SET_ARGUMENT_PACK_ARGS (t, vec);
4767 }
4768 else
4769 t = convert_from_reference (t);
4770 }
4771 return t;
4772 }
4773
4774 /* Given a single level of template parameters (a TREE_VEC), return it
4775 as a set of template arguments. */
4776
4777 tree
4778 template_parms_level_to_args (tree parms)
4779 {
4780 tree a = copy_node (parms);
4781 TREE_TYPE (a) = NULL_TREE;
4782 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4783 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4784
4785 if (CHECKING_P)
4786 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4787
4788 return a;
4789 }
4790
4791 /* Given a set of template parameters, return them as a set of template
4792 arguments. The template parameters are represented as a TREE_VEC, in
4793 the form documented in cp-tree.h for template arguments. */
4794
4795 tree
4796 template_parms_to_args (tree parms)
4797 {
4798 tree header;
4799 tree args = NULL_TREE;
4800 int length = TMPL_PARMS_DEPTH (parms);
4801 int l = length;
4802
4803 /* If there is only one level of template parameters, we do not
4804 create a TREE_VEC of TREE_VECs. Instead, we return a single
4805 TREE_VEC containing the arguments. */
4806 if (length > 1)
4807 args = make_tree_vec (length);
4808
4809 for (header = parms; header; header = TREE_CHAIN (header))
4810 {
4811 tree a = template_parms_level_to_args (TREE_VALUE (header));
4812
4813 if (length > 1)
4814 TREE_VEC_ELT (args, --l) = a;
4815 else
4816 args = a;
4817 }
4818
4819 return args;
4820 }
4821
4822 /* Within the declaration of a template, return the currently active
4823 template parameters as an argument TREE_VEC. */
4824
4825 static tree
4826 current_template_args (void)
4827 {
4828 return template_parms_to_args (current_template_parms);
4829 }
4830
4831 /* Return the fully generic arguments for of TMPL, i.e. what
4832 current_template_args would be while parsing it. */
4833
4834 tree
4835 generic_targs_for (tree tmpl)
4836 {
4837 if (tmpl == NULL_TREE)
4838 return NULL_TREE;
4839 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4840 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4841 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4842 template parameter, it has no TEMPLATE_INFO; for a partial
4843 specialization, it has the arguments for the primary template, and we
4844 want the arguments for the partial specialization. */;
4845 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4846 if (tree ti = get_template_info (result))
4847 return TI_ARGS (ti);
4848 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4849 }
4850
4851 /* Update the declared TYPE by doing any lookups which were thought to be
4852 dependent, but are not now that we know the SCOPE of the declarator. */
4853
4854 tree
4855 maybe_update_decl_type (tree orig_type, tree scope)
4856 {
4857 tree type = orig_type;
4858
4859 if (type == NULL_TREE)
4860 return type;
4861
4862 if (TREE_CODE (orig_type) == TYPE_DECL)
4863 type = TREE_TYPE (type);
4864
4865 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4866 && dependent_type_p (type)
4867 /* Don't bother building up the args in this case. */
4868 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4869 {
4870 /* tsubst in the args corresponding to the template parameters,
4871 including auto if present. Most things will be unchanged, but
4872 make_typename_type and tsubst_qualified_id will resolve
4873 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4874 tree args = current_template_args ();
4875 tree auto_node = type_uses_auto (type);
4876 tree pushed;
4877 if (auto_node)
4878 {
4879 tree auto_vec = make_tree_vec (1);
4880 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4881 args = add_to_template_args (args, auto_vec);
4882 }
4883 pushed = push_scope (scope);
4884 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4885 if (pushed)
4886 pop_scope (scope);
4887 }
4888
4889 if (type == error_mark_node)
4890 return orig_type;
4891
4892 if (TREE_CODE (orig_type) == TYPE_DECL)
4893 {
4894 if (same_type_p (type, TREE_TYPE (orig_type)))
4895 type = orig_type;
4896 else
4897 type = TYPE_NAME (type);
4898 }
4899 return type;
4900 }
4901
4902 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4903 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4904 the new template is a member template. */
4905
4906 static tree
4907 build_template_decl (tree decl, tree parms, bool member_template_p)
4908 {
4909 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4910 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4911 DECL_TEMPLATE_PARMS (tmpl) = parms;
4912 DECL_TEMPLATE_RESULT (tmpl) = decl;
4913 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4914 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4915 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4916 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4917
4918 return tmpl;
4919 }
4920
4921 struct template_parm_data
4922 {
4923 /* The level of the template parameters we are currently
4924 processing. */
4925 int level;
4926
4927 /* The index of the specialization argument we are currently
4928 processing. */
4929 int current_arg;
4930
4931 /* An array whose size is the number of template parameters. The
4932 elements are nonzero if the parameter has been used in any one
4933 of the arguments processed so far. */
4934 int* parms;
4935
4936 /* An array whose size is the number of template arguments. The
4937 elements are nonzero if the argument makes use of template
4938 parameters of this level. */
4939 int* arg_uses_template_parms;
4940 };
4941
4942 /* Subroutine of push_template_decl used to see if each template
4943 parameter in a partial specialization is used in the explicit
4944 argument list. If T is of the LEVEL given in DATA (which is
4945 treated as a template_parm_data*), then DATA->PARMS is marked
4946 appropriately. */
4947
4948 static int
4949 mark_template_parm (tree t, void* data)
4950 {
4951 int level;
4952 int idx;
4953 struct template_parm_data* tpd = (struct template_parm_data*) data;
4954
4955 template_parm_level_and_index (t, &level, &idx);
4956
4957 if (level == tpd->level)
4958 {
4959 tpd->parms[idx] = 1;
4960 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4961 }
4962
4963 /* In C++17 the type of a non-type argument is a deduced context. */
4964 if (cxx_dialect >= cxx17
4965 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4966 for_each_template_parm (TREE_TYPE (t),
4967 &mark_template_parm,
4968 data,
4969 NULL,
4970 /*include_nondeduced_p=*/false);
4971
4972 /* Return zero so that for_each_template_parm will continue the
4973 traversal of the tree; we want to mark *every* template parm. */
4974 return 0;
4975 }
4976
4977 /* Process the partial specialization DECL. */
4978
4979 static tree
4980 process_partial_specialization (tree decl)
4981 {
4982 tree type = TREE_TYPE (decl);
4983 tree tinfo = get_template_info (decl);
4984 tree maintmpl = TI_TEMPLATE (tinfo);
4985 tree specargs = TI_ARGS (tinfo);
4986 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4987 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4988 tree inner_parms;
4989 tree inst;
4990 int nargs = TREE_VEC_LENGTH (inner_args);
4991 int ntparms;
4992 int i;
4993 bool did_error_intro = false;
4994 struct template_parm_data tpd;
4995 struct template_parm_data tpd2;
4996
4997 gcc_assert (current_template_parms);
4998
4999 /* A concept cannot be specialized. */
5000 if (flag_concepts && variable_concept_p (maintmpl))
5001 {
5002 error ("specialization of variable concept %q#D", maintmpl);
5003 return error_mark_node;
5004 }
5005
5006 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5007 ntparms = TREE_VEC_LENGTH (inner_parms);
5008
5009 /* We check that each of the template parameters given in the
5010 partial specialization is used in the argument list to the
5011 specialization. For example:
5012
5013 template <class T> struct S;
5014 template <class T> struct S<T*>;
5015
5016 The second declaration is OK because `T*' uses the template
5017 parameter T, whereas
5018
5019 template <class T> struct S<int>;
5020
5021 is no good. Even trickier is:
5022
5023 template <class T>
5024 struct S1
5025 {
5026 template <class U>
5027 struct S2;
5028 template <class U>
5029 struct S2<T>;
5030 };
5031
5032 The S2<T> declaration is actually invalid; it is a
5033 full-specialization. Of course,
5034
5035 template <class U>
5036 struct S2<T (*)(U)>;
5037
5038 or some such would have been OK. */
5039 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5040 tpd.parms = XALLOCAVEC (int, ntparms);
5041 memset (tpd.parms, 0, sizeof (int) * ntparms);
5042
5043 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5044 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5045 for (i = 0; i < nargs; ++i)
5046 {
5047 tpd.current_arg = i;
5048 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5049 &mark_template_parm,
5050 &tpd,
5051 NULL,
5052 /*include_nondeduced_p=*/false);
5053 }
5054 for (i = 0; i < ntparms; ++i)
5055 if (tpd.parms[i] == 0)
5056 {
5057 /* One of the template parms was not used in a deduced context in the
5058 specialization. */
5059 if (!did_error_intro)
5060 {
5061 error ("template parameters not deducible in "
5062 "partial specialization:");
5063 did_error_intro = true;
5064 }
5065
5066 inform (input_location, " %qD",
5067 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5068 }
5069
5070 if (did_error_intro)
5071 return error_mark_node;
5072
5073 /* [temp.class.spec]
5074
5075 The argument list of the specialization shall not be identical to
5076 the implicit argument list of the primary template. */
5077 tree main_args
5078 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5079 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5080 && (!flag_concepts
5081 || !strictly_subsumes (current_template_constraints (),
5082 main_args, maintmpl)))
5083 {
5084 if (!flag_concepts)
5085 error ("partial specialization %q+D does not specialize "
5086 "any template arguments; to define the primary template, "
5087 "remove the template argument list", decl);
5088 else
5089 error ("partial specialization %q+D does not specialize any "
5090 "template arguments and is not more constrained than "
5091 "the primary template; to define the primary template, "
5092 "remove the template argument list", decl);
5093 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5094 }
5095
5096 /* A partial specialization that replaces multiple parameters of the
5097 primary template with a pack expansion is less specialized for those
5098 parameters. */
5099 if (nargs < DECL_NTPARMS (maintmpl))
5100 {
5101 error ("partial specialization is not more specialized than the "
5102 "primary template because it replaces multiple parameters "
5103 "with a pack expansion");
5104 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5105 /* Avoid crash in process_partial_specialization. */
5106 return decl;
5107 }
5108
5109 else if (nargs > DECL_NTPARMS (maintmpl))
5110 {
5111 error ("too many arguments for partial specialization %qT", type);
5112 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5113 /* Avoid crash below. */
5114 return decl;
5115 }
5116
5117 /* If we aren't in a dependent class, we can actually try deduction. */
5118 else if (tpd.level == 1
5119 /* FIXME we should be able to handle a partial specialization of a
5120 partial instantiation, but currently we can't (c++/41727). */
5121 && TMPL_ARGS_DEPTH (specargs) == 1
5122 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5123 {
5124 auto_diagnostic_group d;
5125 if (permerror (input_location, "partial specialization %qD is not "
5126 "more specialized than", decl))
5127 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5128 maintmpl);
5129 }
5130
5131 /* [temp.class.spec]
5132
5133 A partially specialized non-type argument expression shall not
5134 involve template parameters of the partial specialization except
5135 when the argument expression is a simple identifier.
5136
5137 The type of a template parameter corresponding to a specialized
5138 non-type argument shall not be dependent on a parameter of the
5139 specialization.
5140
5141 Also, we verify that pack expansions only occur at the
5142 end of the argument list. */
5143 tpd2.parms = 0;
5144 for (i = 0; i < nargs; ++i)
5145 {
5146 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5147 tree arg = TREE_VEC_ELT (inner_args, i);
5148 tree packed_args = NULL_TREE;
5149 int j, len = 1;
5150
5151 if (ARGUMENT_PACK_P (arg))
5152 {
5153 /* Extract the arguments from the argument pack. We'll be
5154 iterating over these in the following loop. */
5155 packed_args = ARGUMENT_PACK_ARGS (arg);
5156 len = TREE_VEC_LENGTH (packed_args);
5157 }
5158
5159 for (j = 0; j < len; j++)
5160 {
5161 if (packed_args)
5162 /* Get the Jth argument in the parameter pack. */
5163 arg = TREE_VEC_ELT (packed_args, j);
5164
5165 if (PACK_EXPANSION_P (arg))
5166 {
5167 /* Pack expansions must come at the end of the
5168 argument list. */
5169 if ((packed_args && j < len - 1)
5170 || (!packed_args && i < nargs - 1))
5171 {
5172 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5173 error ("parameter pack argument %qE must be at the "
5174 "end of the template argument list", arg);
5175 else
5176 error ("parameter pack argument %qT must be at the "
5177 "end of the template argument list", arg);
5178 }
5179 }
5180
5181 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5182 /* We only care about the pattern. */
5183 arg = PACK_EXPANSION_PATTERN (arg);
5184
5185 if (/* These first two lines are the `non-type' bit. */
5186 !TYPE_P (arg)
5187 && TREE_CODE (arg) != TEMPLATE_DECL
5188 /* This next two lines are the `argument expression is not just a
5189 simple identifier' condition and also the `specialized
5190 non-type argument' bit. */
5191 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5192 && !((REFERENCE_REF_P (arg)
5193 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5194 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5195 {
5196 if ((!packed_args && tpd.arg_uses_template_parms[i])
5197 || (packed_args && uses_template_parms (arg)))
5198 error_at (cp_expr_loc_or_input_loc (arg),
5199 "template argument %qE involves template "
5200 "parameter(s)", arg);
5201 else
5202 {
5203 /* Look at the corresponding template parameter,
5204 marking which template parameters its type depends
5205 upon. */
5206 tree type = TREE_TYPE (parm);
5207
5208 if (!tpd2.parms)
5209 {
5210 /* We haven't yet initialized TPD2. Do so now. */
5211 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5212 /* The number of parameters here is the number in the
5213 main template, which, as checked in the assertion
5214 above, is NARGS. */
5215 tpd2.parms = XALLOCAVEC (int, nargs);
5216 tpd2.level =
5217 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5218 }
5219
5220 /* Mark the template parameters. But this time, we're
5221 looking for the template parameters of the main
5222 template, not in the specialization. */
5223 tpd2.current_arg = i;
5224 tpd2.arg_uses_template_parms[i] = 0;
5225 memset (tpd2.parms, 0, sizeof (int) * nargs);
5226 for_each_template_parm (type,
5227 &mark_template_parm,
5228 &tpd2,
5229 NULL,
5230 /*include_nondeduced_p=*/false);
5231
5232 if (tpd2.arg_uses_template_parms [i])
5233 {
5234 /* The type depended on some template parameters.
5235 If they are fully specialized in the
5236 specialization, that's OK. */
5237 int j;
5238 int count = 0;
5239 for (j = 0; j < nargs; ++j)
5240 if (tpd2.parms[j] != 0
5241 && tpd.arg_uses_template_parms [j])
5242 ++count;
5243 if (count != 0)
5244 error_n (input_location, count,
5245 "type %qT of template argument %qE depends "
5246 "on a template parameter",
5247 "type %qT of template argument %qE depends "
5248 "on template parameters",
5249 type,
5250 arg);
5251 }
5252 }
5253 }
5254 }
5255 }
5256
5257 /* We should only get here once. */
5258 if (TREE_CODE (decl) == TYPE_DECL)
5259 gcc_assert (!COMPLETE_TYPE_P (type));
5260
5261 // Build the template decl.
5262 tree tmpl = build_template_decl (decl, current_template_parms,
5263 DECL_MEMBER_TEMPLATE_P (maintmpl));
5264 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5265 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5266 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5267
5268 /* Give template template parms a DECL_CONTEXT of the template
5269 for which they are a parameter. */
5270 for (i = 0; i < ntparms; ++i)
5271 {
5272 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5273 if (TREE_CODE (parm) == TEMPLATE_DECL)
5274 DECL_CONTEXT (parm) = tmpl;
5275 }
5276
5277 if (VAR_P (decl))
5278 /* We didn't register this in check_explicit_specialization so we could
5279 wait until the constraints were set. */
5280 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5281 else
5282 associate_classtype_constraints (type);
5283
5284 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5285 = tree_cons (specargs, tmpl,
5286 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5287 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5288
5289 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5290 inst = TREE_CHAIN (inst))
5291 {
5292 tree instance = TREE_VALUE (inst);
5293 if (TYPE_P (instance)
5294 ? (COMPLETE_TYPE_P (instance)
5295 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5296 : DECL_TEMPLATE_INSTANTIATION (instance))
5297 {
5298 tree spec = most_specialized_partial_spec (instance, tf_none);
5299 tree inst_decl = (DECL_P (instance)
5300 ? instance : TYPE_NAME (instance));
5301 if (!spec)
5302 /* OK */;
5303 else if (spec == error_mark_node)
5304 permerror (input_location,
5305 "declaration of %qD ambiguates earlier template "
5306 "instantiation for %qD", decl, inst_decl);
5307 else if (TREE_VALUE (spec) == tmpl)
5308 permerror (input_location,
5309 "partial specialization of %qD after instantiation "
5310 "of %qD", decl, inst_decl);
5311 }
5312 }
5313
5314 return decl;
5315 }
5316
5317 /* PARM is a template parameter of some form; return the corresponding
5318 TEMPLATE_PARM_INDEX. */
5319
5320 static tree
5321 get_template_parm_index (tree parm)
5322 {
5323 if (TREE_CODE (parm) == PARM_DECL
5324 || TREE_CODE (parm) == CONST_DECL)
5325 parm = DECL_INITIAL (parm);
5326 else if (TREE_CODE (parm) == TYPE_DECL
5327 || TREE_CODE (parm) == TEMPLATE_DECL)
5328 parm = TREE_TYPE (parm);
5329 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5330 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5331 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5332 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5333 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5334 return parm;
5335 }
5336
5337 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5338 parameter packs used by the template parameter PARM. */
5339
5340 static void
5341 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5342 {
5343 /* A type parm can't refer to another parm. */
5344 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5345 return;
5346 else if (TREE_CODE (parm) == PARM_DECL)
5347 {
5348 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5349 ppd, ppd->visited);
5350 return;
5351 }
5352
5353 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5354
5355 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5356 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5357 {
5358 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5359 if (template_parameter_pack_p (p))
5360 /* Any packs in the type are expanded by this parameter. */;
5361 else
5362 fixed_parameter_pack_p_1 (p, ppd);
5363 }
5364 }
5365
5366 /* PARM is a template parameter pack. Return any parameter packs used in
5367 its type or the type of any of its template parameters. If there are
5368 any such packs, it will be instantiated into a fixed template parameter
5369 list by partial instantiation rather than be fully deduced. */
5370
5371 tree
5372 fixed_parameter_pack_p (tree parm)
5373 {
5374 /* This can only be true in a member template. */
5375 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5376 return NULL_TREE;
5377 /* This can only be true for a parameter pack. */
5378 if (!template_parameter_pack_p (parm))
5379 return NULL_TREE;
5380 /* A type parm can't refer to another parm. */
5381 if (TREE_CODE (parm) == TYPE_DECL)
5382 return NULL_TREE;
5383
5384 tree parameter_packs = NULL_TREE;
5385 struct find_parameter_pack_data ppd;
5386 ppd.parameter_packs = &parameter_packs;
5387 ppd.visited = new hash_set<tree>;
5388 ppd.type_pack_expansion_p = false;
5389
5390 fixed_parameter_pack_p_1 (parm, &ppd);
5391
5392 delete ppd.visited;
5393 return parameter_packs;
5394 }
5395
5396 /* Check that a template declaration's use of default arguments and
5397 parameter packs is not invalid. Here, PARMS are the template
5398 parameters. IS_PRIMARY is true if DECL is the thing declared by
5399 a primary template. IS_PARTIAL is true if DECL is a partial
5400 specialization.
5401
5402 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5403 function template declaration or a friend class template
5404 declaration. In the function case, 1 indicates a declaration, 2
5405 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5406 emitted for extraneous default arguments.
5407
5408 Returns TRUE if there were no errors found, FALSE otherwise. */
5409
5410 bool
5411 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5412 bool is_partial, int is_friend_decl)
5413 {
5414 const char *msg;
5415 int last_level_to_check;
5416 tree parm_level;
5417 bool no_errors = true;
5418
5419 /* [temp.param]
5420
5421 A default template-argument shall not be specified in a
5422 function template declaration or a function template definition, nor
5423 in the template-parameter-list of the definition of a member of a
5424 class template. */
5425
5426 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5427 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5428 /* You can't have a function template declaration in a local
5429 scope, nor you can you define a member of a class template in a
5430 local scope. */
5431 return true;
5432
5433 if ((TREE_CODE (decl) == TYPE_DECL
5434 && TREE_TYPE (decl)
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5436 || (TREE_CODE (decl) == FUNCTION_DECL
5437 && LAMBDA_FUNCTION_P (decl)))
5438 /* A lambda doesn't have an explicit declaration; don't complain
5439 about the parms of the enclosing class. */
5440 return true;
5441
5442 if (current_class_type
5443 && !TYPE_BEING_DEFINED (current_class_type)
5444 && DECL_LANG_SPECIFIC (decl)
5445 && DECL_DECLARES_FUNCTION_P (decl)
5446 /* If this is either a friend defined in the scope of the class
5447 or a member function. */
5448 && (DECL_FUNCTION_MEMBER_P (decl)
5449 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5450 : DECL_FRIEND_CONTEXT (decl)
5451 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5452 : false)
5453 /* And, if it was a member function, it really was defined in
5454 the scope of the class. */
5455 && (!DECL_FUNCTION_MEMBER_P (decl)
5456 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5457 /* We already checked these parameters when the template was
5458 declared, so there's no need to do it again now. This function
5459 was defined in class scope, but we're processing its body now
5460 that the class is complete. */
5461 return true;
5462
5463 /* Core issue 226 (C++0x only): the following only applies to class
5464 templates. */
5465 if (is_primary
5466 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5467 {
5468 /* [temp.param]
5469
5470 If a template-parameter has a default template-argument, all
5471 subsequent template-parameters shall have a default
5472 template-argument supplied. */
5473 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5474 {
5475 tree inner_parms = TREE_VALUE (parm_level);
5476 int ntparms = TREE_VEC_LENGTH (inner_parms);
5477 int seen_def_arg_p = 0;
5478 int i;
5479
5480 for (i = 0; i < ntparms; ++i)
5481 {
5482 tree parm = TREE_VEC_ELT (inner_parms, i);
5483
5484 if (parm == error_mark_node)
5485 continue;
5486
5487 if (TREE_PURPOSE (parm))
5488 seen_def_arg_p = 1;
5489 else if (seen_def_arg_p
5490 && !template_parameter_pack_p (TREE_VALUE (parm)))
5491 {
5492 error ("no default argument for %qD", TREE_VALUE (parm));
5493 /* For better subsequent error-recovery, we indicate that
5494 there should have been a default argument. */
5495 TREE_PURPOSE (parm) = error_mark_node;
5496 no_errors = false;
5497 }
5498 else if (!is_partial
5499 && !is_friend_decl
5500 /* Don't complain about an enclosing partial
5501 specialization. */
5502 && parm_level == parms
5503 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5504 && i < ntparms - 1
5505 && template_parameter_pack_p (TREE_VALUE (parm))
5506 /* A fixed parameter pack will be partially
5507 instantiated into a fixed length list. */
5508 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5509 {
5510 /* A primary class template, primary variable template
5511 (DR 2032), or alias template can only have one
5512 parameter pack, at the end of the template
5513 parameter list. */
5514
5515 error ("parameter pack %q+D must be at the end of the"
5516 " template parameter list", TREE_VALUE (parm));
5517
5518 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5519 = error_mark_node;
5520 no_errors = false;
5521 }
5522 }
5523 }
5524 }
5525
5526 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5527 || is_partial
5528 || !is_primary
5529 || is_friend_decl)
5530 /* For an ordinary class template, default template arguments are
5531 allowed at the innermost level, e.g.:
5532 template <class T = int>
5533 struct S {};
5534 but, in a partial specialization, they're not allowed even
5535 there, as we have in [temp.class.spec]:
5536
5537 The template parameter list of a specialization shall not
5538 contain default template argument values.
5539
5540 So, for a partial specialization, or for a function template
5541 (in C++98/C++03), we look at all of them. */
5542 ;
5543 else
5544 /* But, for a primary class template that is not a partial
5545 specialization we look at all template parameters except the
5546 innermost ones. */
5547 parms = TREE_CHAIN (parms);
5548
5549 /* Figure out what error message to issue. */
5550 if (is_friend_decl == 2)
5551 msg = G_("default template arguments may not be used in function template "
5552 "friend re-declaration");
5553 else if (is_friend_decl)
5554 msg = G_("default template arguments may not be used in template "
5555 "friend declarations");
5556 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5557 msg = G_("default template arguments may not be used in function templates "
5558 "without %<-std=c++11%> or %<-std=gnu++11%>");
5559 else if (is_partial)
5560 msg = G_("default template arguments may not be used in "
5561 "partial specializations");
5562 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5563 msg = G_("default argument for template parameter for class enclosing %qD");
5564 else
5565 /* Per [temp.param]/9, "A default template-argument shall not be
5566 specified in the template-parameter-lists of the definition of
5567 a member of a class template that appears outside of the member's
5568 class.", thus if we aren't handling a member of a class template
5569 there is no need to examine the parameters. */
5570 return true;
5571
5572 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5573 /* If we're inside a class definition, there's no need to
5574 examine the parameters to the class itself. On the one
5575 hand, they will be checked when the class is defined, and,
5576 on the other, default arguments are valid in things like:
5577 template <class T = double>
5578 struct S { template <class U> void f(U); };
5579 Here the default argument for `S' has no bearing on the
5580 declaration of `f'. */
5581 last_level_to_check = template_class_depth (current_class_type) + 1;
5582 else
5583 /* Check everything. */
5584 last_level_to_check = 0;
5585
5586 for (parm_level = parms;
5587 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5588 parm_level = TREE_CHAIN (parm_level))
5589 {
5590 tree inner_parms = TREE_VALUE (parm_level);
5591 int i;
5592 int ntparms;
5593
5594 ntparms = TREE_VEC_LENGTH (inner_parms);
5595 for (i = 0; i < ntparms; ++i)
5596 {
5597 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5598 continue;
5599
5600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5601 {
5602 if (msg)
5603 {
5604 no_errors = false;
5605 if (is_friend_decl == 2)
5606 return no_errors;
5607
5608 error (msg, decl);
5609 msg = 0;
5610 }
5611
5612 /* Clear out the default argument so that we are not
5613 confused later. */
5614 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5615 }
5616 }
5617
5618 /* At this point, if we're still interested in issuing messages,
5619 they must apply to classes surrounding the object declared. */
5620 if (msg)
5621 msg = G_("default argument for template parameter for class "
5622 "enclosing %qD");
5623 }
5624
5625 return no_errors;
5626 }
5627
5628 /* Worker for push_template_decl_real, called via
5629 for_each_template_parm. DATA is really an int, indicating the
5630 level of the parameters we are interested in. If T is a template
5631 parameter of that level, return nonzero. */
5632
5633 static int
5634 template_parm_this_level_p (tree t, void* data)
5635 {
5636 int this_level = *(int *)data;
5637 int level;
5638
5639 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5640 level = TEMPLATE_PARM_LEVEL (t);
5641 else
5642 level = TEMPLATE_TYPE_LEVEL (t);
5643 return level == this_level;
5644 }
5645
5646 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5647 DATA is really an int, indicating the innermost outer level of parameters.
5648 If T is a template parameter of that level or further out, return
5649 nonzero. */
5650
5651 static int
5652 template_parm_outer_level (tree t, void *data)
5653 {
5654 int this_level = *(int *)data;
5655 int level;
5656
5657 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5658 level = TEMPLATE_PARM_LEVEL (t);
5659 else
5660 level = TEMPLATE_TYPE_LEVEL (t);
5661 return level <= this_level;
5662 }
5663
5664 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5665 parameters given by current_template_args, or reuses a
5666 previously existing one, if appropriate. Returns the DECL, or an
5667 equivalent one, if it is replaced via a call to duplicate_decls.
5668
5669 If IS_FRIEND is true, DECL is a friend declaration. */
5670
5671 tree
5672 push_template_decl_real (tree decl, bool is_friend)
5673 {
5674 tree tmpl;
5675 tree args;
5676 tree info;
5677 tree ctx;
5678 bool is_primary;
5679 bool is_partial;
5680 int new_template_p = 0;
5681 /* True if the template is a member template, in the sense of
5682 [temp.mem]. */
5683 bool member_template_p = false;
5684
5685 if (decl == error_mark_node || !current_template_parms)
5686 return error_mark_node;
5687
5688 /* See if this is a partial specialization. */
5689 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5690 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5691 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5692 || (VAR_P (decl)
5693 && DECL_LANG_SPECIFIC (decl)
5694 && DECL_TEMPLATE_SPECIALIZATION (decl)
5695 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5696
5697 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5698 is_friend = true;
5699
5700 if (is_friend)
5701 /* For a friend, we want the context of the friend, not
5702 the type of which it is a friend. */
5703 ctx = CP_DECL_CONTEXT (decl);
5704 else if (CP_DECL_CONTEXT (decl)
5705 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5706 /* In the case of a virtual function, we want the class in which
5707 it is defined. */
5708 ctx = CP_DECL_CONTEXT (decl);
5709 else
5710 /* Otherwise, if we're currently defining some class, the DECL
5711 is assumed to be a member of the class. */
5712 ctx = current_scope ();
5713
5714 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5715 ctx = NULL_TREE;
5716
5717 if (!DECL_CONTEXT (decl))
5718 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5719
5720 /* See if this is a primary template. */
5721 if (is_friend && ctx
5722 && uses_template_parms_level (ctx, processing_template_decl))
5723 /* A friend template that specifies a class context, i.e.
5724 template <typename T> friend void A<T>::f();
5725 is not primary. */
5726 is_primary = false;
5727 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5728 is_primary = false;
5729 else
5730 is_primary = template_parm_scope_p ();
5731
5732 if (is_primary)
5733 {
5734 warning (OPT_Wtemplates, "template %qD declared", decl);
5735
5736 if (DECL_CLASS_SCOPE_P (decl))
5737 member_template_p = true;
5738 if (TREE_CODE (decl) == TYPE_DECL
5739 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5740 {
5741 error ("template class without a name");
5742 return error_mark_node;
5743 }
5744 else if (TREE_CODE (decl) == FUNCTION_DECL)
5745 {
5746 if (member_template_p)
5747 {
5748 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5749 error ("member template %qD may not have virt-specifiers", decl);
5750 }
5751 if (DECL_DESTRUCTOR_P (decl))
5752 {
5753 /* [temp.mem]
5754
5755 A destructor shall not be a member template. */
5756 error_at (DECL_SOURCE_LOCATION (decl),
5757 "destructor %qD declared as member template", decl);
5758 return error_mark_node;
5759 }
5760 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5761 && (!prototype_p (TREE_TYPE (decl))
5762 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5763 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5764 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5765 == void_list_node)))
5766 {
5767 /* [basic.stc.dynamic.allocation]
5768
5769 An allocation function can be a function
5770 template. ... Template allocation functions shall
5771 have two or more parameters. */
5772 error ("invalid template declaration of %qD", decl);
5773 return error_mark_node;
5774 }
5775 }
5776 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5777 && CLASS_TYPE_P (TREE_TYPE (decl)))
5778 {
5779 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5780 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5781 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5782 {
5783 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5784 if (TREE_CODE (t) == TYPE_DECL)
5785 t = TREE_TYPE (t);
5786 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5787 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5788 }
5789 }
5790 else if (TREE_CODE (decl) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl))
5792 /* alias-declaration */
5793 gcc_assert (!DECL_ARTIFICIAL (decl));
5794 else if (VAR_P (decl))
5795 /* C++14 variable template. */;
5796 else if (TREE_CODE (decl) == CONCEPT_DECL)
5797 /* C++20 concept definitions. */;
5798 else
5799 {
5800 error ("template declaration of %q#D", decl);
5801 return error_mark_node;
5802 }
5803 }
5804
5805 /* Check to see that the rules regarding the use of default
5806 arguments are not being violated. We check args for a friend
5807 functions when we know whether it's a definition, introducing
5808 declaration or re-declaration. */
5809 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5810 check_default_tmpl_args (decl, current_template_parms,
5811 is_primary, is_partial, is_friend);
5812
5813 /* Ensure that there are no parameter packs in the type of this
5814 declaration that have not been expanded. */
5815 if (TREE_CODE (decl) == FUNCTION_DECL)
5816 {
5817 /* Check each of the arguments individually to see if there are
5818 any bare parameter packs. */
5819 tree type = TREE_TYPE (decl);
5820 tree arg = DECL_ARGUMENTS (decl);
5821 tree argtype = TYPE_ARG_TYPES (type);
5822
5823 while (arg && argtype)
5824 {
5825 if (!DECL_PACK_P (arg)
5826 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5827 {
5828 /* This is a PARM_DECL that contains unexpanded parameter
5829 packs. We have already complained about this in the
5830 check_for_bare_parameter_packs call, so just replace
5831 these types with ERROR_MARK_NODE. */
5832 TREE_TYPE (arg) = error_mark_node;
5833 TREE_VALUE (argtype) = error_mark_node;
5834 }
5835
5836 arg = DECL_CHAIN (arg);
5837 argtype = TREE_CHAIN (argtype);
5838 }
5839
5840 /* Check for bare parameter packs in the return type and the
5841 exception specifiers. */
5842 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5843 /* Errors were already issued, set return type to int
5844 as the frontend doesn't expect error_mark_node as
5845 the return type. */
5846 TREE_TYPE (type) = integer_type_node;
5847 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5848 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5849 }
5850 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5851 ? DECL_ORIGINAL_TYPE (decl)
5852 : TREE_TYPE (decl)))
5853 {
5854 TREE_TYPE (decl) = error_mark_node;
5855 return error_mark_node;
5856 }
5857
5858 if (is_partial)
5859 return process_partial_specialization (decl);
5860
5861 args = current_template_args ();
5862
5863 if (!ctx
5864 || TREE_CODE (ctx) == FUNCTION_DECL
5865 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5866 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5867 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5868 {
5869 if (DECL_LANG_SPECIFIC (decl)
5870 && DECL_TEMPLATE_INFO (decl)
5871 && DECL_TI_TEMPLATE (decl))
5872 tmpl = DECL_TI_TEMPLATE (decl);
5873 /* If DECL is a TYPE_DECL for a class-template, then there won't
5874 be DECL_LANG_SPECIFIC. The information equivalent to
5875 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5876 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5877 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5878 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5879 {
5880 /* Since a template declaration already existed for this
5881 class-type, we must be redeclaring it here. Make sure
5882 that the redeclaration is valid. */
5883 redeclare_class_template (TREE_TYPE (decl),
5884 current_template_parms,
5885 current_template_constraints ());
5886 /* We don't need to create a new TEMPLATE_DECL; just use the
5887 one we already had. */
5888 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5889 }
5890 else
5891 {
5892 tmpl = build_template_decl (decl, current_template_parms,
5893 member_template_p);
5894 new_template_p = 1;
5895
5896 if (DECL_LANG_SPECIFIC (decl)
5897 && DECL_TEMPLATE_SPECIALIZATION (decl))
5898 {
5899 /* A specialization of a member template of a template
5900 class. */
5901 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5902 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5903 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5904 }
5905 }
5906 }
5907 else
5908 {
5909 tree a, t, current, parms;
5910 int i;
5911 tree tinfo = get_template_info (decl);
5912
5913 if (!tinfo)
5914 {
5915 error ("template definition of non-template %q#D", decl);
5916 return error_mark_node;
5917 }
5918
5919 tmpl = TI_TEMPLATE (tinfo);
5920
5921 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5922 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5923 && DECL_TEMPLATE_SPECIALIZATION (decl)
5924 && DECL_MEMBER_TEMPLATE_P (tmpl))
5925 {
5926 tree new_tmpl;
5927
5928 /* The declaration is a specialization of a member
5929 template, declared outside the class. Therefore, the
5930 innermost template arguments will be NULL, so we
5931 replace them with the arguments determined by the
5932 earlier call to check_explicit_specialization. */
5933 args = DECL_TI_ARGS (decl);
5934
5935 new_tmpl
5936 = build_template_decl (decl, current_template_parms,
5937 member_template_p);
5938 DECL_TI_TEMPLATE (decl) = new_tmpl;
5939 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5940 DECL_TEMPLATE_INFO (new_tmpl)
5941 = build_template_info (tmpl, args);
5942
5943 register_specialization (new_tmpl,
5944 most_general_template (tmpl),
5945 args,
5946 is_friend, 0);
5947 return decl;
5948 }
5949
5950 /* Make sure the template headers we got make sense. */
5951
5952 parms = DECL_TEMPLATE_PARMS (tmpl);
5953 i = TMPL_PARMS_DEPTH (parms);
5954 if (TMPL_ARGS_DEPTH (args) != i)
5955 {
5956 error ("expected %d levels of template parms for %q#D, got %d",
5957 i, decl, TMPL_ARGS_DEPTH (args));
5958 DECL_INTERFACE_KNOWN (decl) = 1;
5959 return error_mark_node;
5960 }
5961 else
5962 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5963 {
5964 a = TMPL_ARGS_LEVEL (args, i);
5965 t = INNERMOST_TEMPLATE_PARMS (parms);
5966
5967 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5968 {
5969 if (current == decl)
5970 error ("got %d template parameters for %q#D",
5971 TREE_VEC_LENGTH (a), decl);
5972 else
5973 error ("got %d template parameters for %q#T",
5974 TREE_VEC_LENGTH (a), current);
5975 error (" but %d required", TREE_VEC_LENGTH (t));
5976 /* Avoid crash in import_export_decl. */
5977 DECL_INTERFACE_KNOWN (decl) = 1;
5978 return error_mark_node;
5979 }
5980
5981 if (current == decl)
5982 current = ctx;
5983 else if (current == NULL_TREE)
5984 /* Can happen in erroneous input. */
5985 break;
5986 else
5987 current = get_containing_scope (current);
5988 }
5989
5990 /* Check that the parms are used in the appropriate qualifying scopes
5991 in the declarator. */
5992 if (!comp_template_args
5993 (TI_ARGS (tinfo),
5994 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5995 {
5996 error ("template arguments to %qD do not match original "
5997 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5998 if (!uses_template_parms (TI_ARGS (tinfo)))
5999 inform (input_location, "use %<template<>%> for"
6000 " an explicit specialization");
6001 /* Avoid crash in import_export_decl. */
6002 DECL_INTERFACE_KNOWN (decl) = 1;
6003 return error_mark_node;
6004 }
6005 }
6006
6007 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
6008
6009 if (new_template_p)
6010 {
6011 /* Push template declarations for global functions and types.
6012 Note that we do not try to push a global template friend
6013 declared in a template class; such a thing may well depend on
6014 the template parameters of the class and we'll push it when
6015 instantiating the befriending class. */
6016 if (!ctx
6017 && !(is_friend && template_class_depth (current_class_type) > 0))
6018 {
6019 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6020 if (tmpl == error_mark_node)
6021 return error_mark_node;
6022
6023 /* Hide template friend classes that haven't been declared yet. */
6024 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6025 {
6026 DECL_ANTICIPATED (tmpl) = 1;
6027 DECL_FRIEND_P (tmpl) = 1;
6028 }
6029 }
6030 }
6031 else
6032 /* The type may have been completed, or (erroneously) changed. */
6033 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6034
6035 if (is_primary)
6036 {
6037 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6038
6039 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6040
6041 /* Give template template parms a DECL_CONTEXT of the template
6042 for which they are a parameter. */
6043 parms = INNERMOST_TEMPLATE_PARMS (parms);
6044 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6045 {
6046 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6047 if (TREE_CODE (parm) == TEMPLATE_DECL)
6048 DECL_CONTEXT (parm) = tmpl;
6049 }
6050
6051 if (TREE_CODE (decl) == TYPE_DECL
6052 && TYPE_DECL_ALIAS_P (decl))
6053 {
6054 if (tree constr
6055 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6056 {
6057 /* ??? Why don't we do this here for all templates? */
6058 constr = build_constraints (constr, NULL_TREE);
6059 set_constraints (decl, constr);
6060 }
6061 if (complex_alias_template_p (tmpl))
6062 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6063 }
6064 }
6065
6066 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6067 back to its most general template. If TMPL is a specialization,
6068 ARGS may only have the innermost set of arguments. Add the missing
6069 argument levels if necessary. */
6070 if (DECL_TEMPLATE_INFO (tmpl))
6071 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6072
6073 info = build_template_info (tmpl, args);
6074
6075 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6076 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6077 else
6078 {
6079 if (is_primary)
6080 retrofit_lang_decl (decl);
6081 if (DECL_LANG_SPECIFIC (decl)
6082 && !(VAR_OR_FUNCTION_DECL_P (decl)
6083 && DECL_LOCAL_DECL_P (decl)))
6084 DECL_TEMPLATE_INFO (decl) = info;
6085 }
6086
6087 if (flag_implicit_templates
6088 && !is_friend
6089 && TREE_PUBLIC (decl)
6090 && VAR_OR_FUNCTION_DECL_P (decl))
6091 /* Set DECL_COMDAT on template instantiations; if we force
6092 them to be emitted by explicit instantiation,
6093 mark_needed will tell cgraph to do the right thing. */
6094 DECL_COMDAT (decl) = true;
6095
6096 return DECL_TEMPLATE_RESULT (tmpl);
6097 }
6098
6099 tree
6100 push_template_decl (tree decl)
6101 {
6102 return push_template_decl_real (decl, false);
6103 }
6104
6105 /* FN is an inheriting constructor that inherits from the constructor
6106 template INHERITED; turn FN into a constructor template with a matching
6107 template header. */
6108
6109 tree
6110 add_inherited_template_parms (tree fn, tree inherited)
6111 {
6112 tree inner_parms
6113 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6114 inner_parms = copy_node (inner_parms);
6115 tree parms
6116 = tree_cons (size_int (processing_template_decl + 1),
6117 inner_parms, current_template_parms);
6118 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6119 tree args = template_parms_to_args (parms);
6120 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6121 DECL_ARTIFICIAL (tmpl) = true;
6122 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6123 return tmpl;
6124 }
6125
6126 /* Called when a class template TYPE is redeclared with the indicated
6127 template PARMS, e.g.:
6128
6129 template <class T> struct S;
6130 template <class T> struct S {}; */
6131
6132 bool
6133 redeclare_class_template (tree type, tree parms, tree cons)
6134 {
6135 tree tmpl;
6136 tree tmpl_parms;
6137 int i;
6138
6139 if (!TYPE_TEMPLATE_INFO (type))
6140 {
6141 error ("%qT is not a template type", type);
6142 return false;
6143 }
6144
6145 tmpl = TYPE_TI_TEMPLATE (type);
6146 if (!PRIMARY_TEMPLATE_P (tmpl))
6147 /* The type is nested in some template class. Nothing to worry
6148 about here; there are no new template parameters for the nested
6149 type. */
6150 return true;
6151
6152 if (!parms)
6153 {
6154 error ("template specifiers not specified in declaration of %qD",
6155 tmpl);
6156 return false;
6157 }
6158
6159 parms = INNERMOST_TEMPLATE_PARMS (parms);
6160 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6161
6162 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6163 {
6164 error_n (input_location, TREE_VEC_LENGTH (parms),
6165 "redeclared with %d template parameter",
6166 "redeclared with %d template parameters",
6167 TREE_VEC_LENGTH (parms));
6168 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6169 "previous declaration %qD used %d template parameter",
6170 "previous declaration %qD used %d template parameters",
6171 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6172 return false;
6173 }
6174
6175 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6176 {
6177 tree tmpl_parm;
6178 tree parm;
6179 tree tmpl_default;
6180 tree parm_default;
6181
6182 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6183 || TREE_VEC_ELT (parms, i) == error_mark_node)
6184 continue;
6185
6186 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6187 if (error_operand_p (tmpl_parm))
6188 return false;
6189
6190 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6191 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6192 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6193
6194 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6195 TEMPLATE_DECL. */
6196 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6197 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6198 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6199 || (TREE_CODE (tmpl_parm) != PARM_DECL
6200 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6201 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6202 || (TREE_CODE (tmpl_parm) == PARM_DECL
6203 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6204 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6205 {
6206 auto_diagnostic_group d;
6207 error ("template parameter %q+#D", tmpl_parm);
6208 inform (input_location, "redeclared here as %q#D", parm);
6209 return false;
6210 }
6211
6212 /* The parameters can be declared to introduce different
6213 constraints. */
6214 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6215 tree p2 = TREE_VEC_ELT (parms, i);
6216 if (!template_parameter_constraints_equivalent_p (p1, p2))
6217 {
6218 auto_diagnostic_group d;
6219 error ("declaration of template parameter %q+#D with different "
6220 "constraints", parm);
6221 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6222 "original declaration appeared here");
6223 return false;
6224 }
6225
6226 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6227 {
6228 /* We have in [temp.param]:
6229
6230 A template-parameter may not be given default arguments
6231 by two different declarations in the same scope. */
6232 auto_diagnostic_group d;
6233 error_at (input_location, "redefinition of default argument for %q#D", parm);
6234 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6235 "original definition appeared here");
6236 return false;
6237 }
6238
6239 if (parm_default != NULL_TREE)
6240 /* Update the previous template parameters (which are the ones
6241 that will really count) with the new default value. */
6242 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6243 else if (tmpl_default != NULL_TREE)
6244 /* Update the new parameters, too; they'll be used as the
6245 parameters for any members. */
6246 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6247
6248 /* Give each template template parm in this redeclaration a
6249 DECL_CONTEXT of the template for which they are a parameter. */
6250 if (TREE_CODE (parm) == TEMPLATE_DECL)
6251 {
6252 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6253 DECL_CONTEXT (parm) = tmpl;
6254 }
6255
6256 if (TREE_CODE (parm) == TYPE_DECL)
6257 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6258 }
6259
6260 tree ci = get_constraints (tmpl);
6261 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6262 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6263
6264 /* Two classes with different constraints declare different entities. */
6265 if (!cp_tree_equal (req1, req2))
6266 {
6267 auto_diagnostic_group d;
6268 error_at (input_location, "redeclaration %q#D with different "
6269 "constraints", tmpl);
6270 inform (DECL_SOURCE_LOCATION (tmpl),
6271 "original declaration appeared here");
6272 return false;
6273 }
6274
6275 return true;
6276 }
6277
6278 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6279 to be used when the caller has already checked
6280 (processing_template_decl
6281 && !instantiation_dependent_expression_p (expr)
6282 && potential_constant_expression (expr))
6283 and cleared processing_template_decl. */
6284
6285 tree
6286 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6287 {
6288 return tsubst_copy_and_build (expr,
6289 /*args=*/NULL_TREE,
6290 complain,
6291 /*in_decl=*/NULL_TREE,
6292 /*function_p=*/false,
6293 /*integral_constant_expression_p=*/true);
6294 }
6295
6296 /* Simplify EXPR if it is a non-dependent expression. Returns the
6297 (possibly simplified) expression. */
6298
6299 tree
6300 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6301 {
6302 if (expr == NULL_TREE)
6303 return NULL_TREE;
6304
6305 /* If we're in a template, but EXPR isn't value dependent, simplify
6306 it. We're supposed to treat:
6307
6308 template <typename T> void f(T[1 + 1]);
6309 template <typename T> void f(T[2]);
6310
6311 as two declarations of the same function, for example. */
6312 if (processing_template_decl
6313 && is_nondependent_constant_expression (expr))
6314 {
6315 processing_template_decl_sentinel s;
6316 expr = instantiate_non_dependent_expr_internal (expr, complain);
6317 }
6318 return expr;
6319 }
6320
6321 tree
6322 instantiate_non_dependent_expr (tree expr)
6323 {
6324 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6325 }
6326
6327 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6328 an uninstantiated expression. */
6329
6330 tree
6331 instantiate_non_dependent_or_null (tree expr)
6332 {
6333 if (expr == NULL_TREE)
6334 return NULL_TREE;
6335 if (processing_template_decl)
6336 {
6337 if (!is_nondependent_constant_expression (expr))
6338 expr = NULL_TREE;
6339 else
6340 {
6341 processing_template_decl_sentinel s;
6342 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6343 }
6344 }
6345 return expr;
6346 }
6347
6348 /* True iff T is a specialization of a variable template. */
6349
6350 bool
6351 variable_template_specialization_p (tree t)
6352 {
6353 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6354 return false;
6355 tree tmpl = DECL_TI_TEMPLATE (t);
6356 return variable_template_p (tmpl);
6357 }
6358
6359 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6360 template declaration, or a TYPE_DECL for an alias declaration. */
6361
6362 bool
6363 alias_type_or_template_p (tree t)
6364 {
6365 if (t == NULL_TREE)
6366 return false;
6367 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6368 || (TYPE_P (t)
6369 && TYPE_NAME (t)
6370 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6371 || DECL_ALIAS_TEMPLATE_P (t));
6372 }
6373
6374 /* If T is a specialization of an alias template, return it; otherwise return
6375 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6376
6377 tree
6378 alias_template_specialization_p (const_tree t,
6379 bool transparent_typedefs)
6380 {
6381 if (!TYPE_P (t))
6382 return NULL_TREE;
6383
6384 /* It's an alias template specialization if it's an alias and its
6385 TYPE_NAME is a specialization of a primary template. */
6386 if (typedef_variant_p (t))
6387 {
6388 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6389 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6390 return CONST_CAST_TREE (t);
6391 if (transparent_typedefs)
6392 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6393 (TYPE_NAME (t)),
6394 transparent_typedefs);
6395 }
6396
6397 return NULL_TREE;
6398 }
6399
6400 /* An alias template is complex from a SFINAE perspective if a template-id
6401 using that alias can be ill-formed when the expansion is not, as with
6402 the void_t template. We determine this by checking whether the
6403 expansion for the alias template uses all its template parameters. */
6404
6405 struct uses_all_template_parms_data
6406 {
6407 int level;
6408 bool *seen;
6409 };
6410
6411 static int
6412 uses_all_template_parms_r (tree t, void *data_)
6413 {
6414 struct uses_all_template_parms_data &data
6415 = *(struct uses_all_template_parms_data*)data_;
6416 tree idx = get_template_parm_index (t);
6417
6418 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6419 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6420 return 0;
6421 }
6422
6423 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6424
6425 static int
6426 complex_pack_expansion_r (tree t, void *data_)
6427 {
6428 /* An alias template with a pack expansion that expands a pack from the
6429 enclosing class needs to be considered complex, to avoid confusion with
6430 the same pack being used as an argument to the alias's own template
6431 parameter (91966). */
6432 if (!PACK_EXPANSION_P (t))
6433 return 0;
6434 struct uses_all_template_parms_data &data
6435 = *(struct uses_all_template_parms_data*)data_;
6436 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6437 pack = TREE_CHAIN (pack))
6438 {
6439 tree parm_pack = TREE_VALUE (pack);
6440 if (!TEMPLATE_PARM_P (parm_pack))
6441 continue;
6442 int idx, level;
6443 template_parm_level_and_index (parm_pack, &level, &idx);
6444 if (level < data.level)
6445 return 1;
6446 }
6447 return 0;
6448 }
6449
6450 static bool
6451 complex_alias_template_p (const_tree tmpl)
6452 {
6453 /* A renaming alias isn't complex. */
6454 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6455 return false;
6456
6457 /* Any other constrained alias is complex. */
6458 if (get_constraints (tmpl))
6459 return true;
6460
6461 struct uses_all_template_parms_data data;
6462 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6463 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6464 data.level = TMPL_PARMS_DEPTH (parms);
6465 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6466 data.seen = XALLOCAVEC (bool, len);
6467 for (int i = 0; i < len; ++i)
6468 data.seen[i] = false;
6469
6470 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6471 NULL, true, complex_pack_expansion_r))
6472 return true;
6473 for (int i = 0; i < len; ++i)
6474 if (!data.seen[i])
6475 return true;
6476 return false;
6477 }
6478
6479 /* If T is a specialization of a complex alias template with dependent
6480 template-arguments, return it; otherwise return NULL_TREE. If T is a
6481 typedef to such a specialization, return the specialization. */
6482
6483 tree
6484 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6485 {
6486 if (!TYPE_P (t) || !typedef_variant_p (t))
6487 return NULL_TREE;
6488
6489 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6490 if (tinfo
6491 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6492 && (any_dependent_template_arguments_p
6493 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6494 return CONST_CAST_TREE (t);
6495
6496 if (transparent_typedefs)
6497 {
6498 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6499 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6500 }
6501
6502 return NULL_TREE;
6503 }
6504
6505 /* Return the number of innermost template parameters in TMPL. */
6506
6507 static int
6508 num_innermost_template_parms (const_tree tmpl)
6509 {
6510 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6511 return TREE_VEC_LENGTH (parms);
6512 }
6513
6514 /* Return either TMPL or another template that it is equivalent to under DR
6515 1286: An alias that just changes the name of a template is equivalent to
6516 the other template. */
6517
6518 static tree
6519 get_underlying_template (tree tmpl)
6520 {
6521 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6522 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6523 {
6524 /* Determine if the alias is equivalent to an underlying template. */
6525 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6526 /* The underlying type may have been ill-formed. Don't proceed. */
6527 if (!orig_type)
6528 break;
6529 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6530 if (!tinfo)
6531 break;
6532
6533 tree underlying = TI_TEMPLATE (tinfo);
6534 if (!PRIMARY_TEMPLATE_P (underlying)
6535 || (num_innermost_template_parms (tmpl)
6536 != num_innermost_template_parms (underlying)))
6537 break;
6538
6539 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6540 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6541 break;
6542
6543 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6544 it's appropriate to treat a less-constrained alias as equivalent. */
6545 if (!at_least_as_constrained (underlying, tmpl))
6546 break;
6547
6548 /* Alias is equivalent. Strip it and repeat. */
6549 tmpl = underlying;
6550 }
6551
6552 return tmpl;
6553 }
6554
6555 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6556 must be a reference-to-function or a pointer-to-function type, as specified
6557 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6558 and check that the resulting function has external linkage. */
6559
6560 static tree
6561 convert_nontype_argument_function (tree type, tree expr,
6562 tsubst_flags_t complain)
6563 {
6564 tree fns = expr;
6565 tree fn, fn_no_ptr;
6566 linkage_kind linkage;
6567
6568 fn = instantiate_type (type, fns, tf_none);
6569 if (fn == error_mark_node)
6570 return error_mark_node;
6571
6572 if (value_dependent_expression_p (fn))
6573 goto accept;
6574
6575 fn_no_ptr = strip_fnptr_conv (fn);
6576 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6577 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6578 if (BASELINK_P (fn_no_ptr))
6579 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6580
6581 /* [temp.arg.nontype]/1
6582
6583 A template-argument for a non-type, non-template template-parameter
6584 shall be one of:
6585 [...]
6586 -- the address of an object or function with external [C++11: or
6587 internal] linkage. */
6588
6589 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6590 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6591 {
6592 if (complain & tf_error)
6593 {
6594 location_t loc = cp_expr_loc_or_input_loc (expr);
6595 error_at (loc, "%qE is not a valid template argument for type %qT",
6596 expr, type);
6597 if (TYPE_PTR_P (type))
6598 inform (loc, "it must be the address of a function "
6599 "with external linkage");
6600 else
6601 inform (loc, "it must be the name of a function with "
6602 "external linkage");
6603 }
6604 return NULL_TREE;
6605 }
6606
6607 linkage = decl_linkage (fn_no_ptr);
6608 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6609 {
6610 if (complain & tf_error)
6611 {
6612 location_t loc = cp_expr_loc_or_input_loc (expr);
6613 if (cxx_dialect >= cxx11)
6614 error_at (loc, "%qE is not a valid template argument for type "
6615 "%qT because %qD has no linkage",
6616 expr, type, fn_no_ptr);
6617 else
6618 error_at (loc, "%qE is not a valid template argument for type "
6619 "%qT because %qD does not have external linkage",
6620 expr, type, fn_no_ptr);
6621 }
6622 return NULL_TREE;
6623 }
6624
6625 accept:
6626 if (TYPE_REF_P (type))
6627 {
6628 if (REFERENCE_REF_P (fn))
6629 fn = TREE_OPERAND (fn, 0);
6630 else
6631 fn = build_address (fn);
6632 }
6633 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6634 fn = build_nop (type, fn);
6635
6636 return fn;
6637 }
6638
6639 /* Subroutine of convert_nontype_argument.
6640 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6641 Emit an error otherwise. */
6642
6643 static bool
6644 check_valid_ptrmem_cst_expr (tree type, tree expr,
6645 tsubst_flags_t complain)
6646 {
6647 tree orig_expr = expr;
6648 STRIP_NOPS (expr);
6649 if (null_ptr_cst_p (expr))
6650 return true;
6651 if (TREE_CODE (expr) == PTRMEM_CST
6652 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6653 PTRMEM_CST_CLASS (expr)))
6654 return true;
6655 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6656 return true;
6657 if (processing_template_decl
6658 && TREE_CODE (expr) == ADDR_EXPR
6659 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6660 return true;
6661 if (complain & tf_error)
6662 {
6663 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6664 error_at (loc, "%qE is not a valid template argument for type %qT",
6665 orig_expr, type);
6666 if (TREE_CODE (expr) != PTRMEM_CST)
6667 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6668 else
6669 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6670 }
6671 return false;
6672 }
6673
6674 /* Returns TRUE iff the address of OP is value-dependent.
6675
6676 14.6.2.4 [temp.dep.temp]:
6677 A non-integral non-type template-argument is dependent if its type is
6678 dependent or it has either of the following forms
6679 qualified-id
6680 & qualified-id
6681 and contains a nested-name-specifier which specifies a class-name that
6682 names a dependent type.
6683
6684 We generalize this to just say that the address of a member of a
6685 dependent class is value-dependent; the above doesn't cover the
6686 address of a static data member named with an unqualified-id. */
6687
6688 static bool
6689 has_value_dependent_address (tree op)
6690 {
6691 STRIP_ANY_LOCATION_WRAPPER (op);
6692
6693 /* We could use get_inner_reference here, but there's no need;
6694 this is only relevant for template non-type arguments, which
6695 can only be expressed as &id-expression. */
6696 if (DECL_P (op))
6697 {
6698 tree ctx = CP_DECL_CONTEXT (op);
6699 if (TYPE_P (ctx) && dependent_type_p (ctx))
6700 return true;
6701 }
6702
6703 return false;
6704 }
6705
6706 /* The next set of functions are used for providing helpful explanatory
6707 diagnostics for failed overload resolution. Their messages should be
6708 indented by two spaces for consistency with the messages in
6709 call.c */
6710
6711 static int
6712 unify_success (bool /*explain_p*/)
6713 {
6714 return 0;
6715 }
6716
6717 /* Other failure functions should call this one, to provide a single function
6718 for setting a breakpoint on. */
6719
6720 static int
6721 unify_invalid (bool /*explain_p*/)
6722 {
6723 return 1;
6724 }
6725
6726 static int
6727 unify_parameter_deduction_failure (bool explain_p, tree parm)
6728 {
6729 if (explain_p)
6730 inform (input_location,
6731 " couldn%'t deduce template parameter %qD", parm);
6732 return unify_invalid (explain_p);
6733 }
6734
6735 static int
6736 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6737 {
6738 if (explain_p)
6739 inform (input_location,
6740 " types %qT and %qT have incompatible cv-qualifiers",
6741 parm, arg);
6742 return unify_invalid (explain_p);
6743 }
6744
6745 static int
6746 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6747 {
6748 if (explain_p)
6749 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6750 return unify_invalid (explain_p);
6751 }
6752
6753 static int
6754 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6755 {
6756 if (explain_p)
6757 inform (input_location,
6758 " template parameter %qD is not a parameter pack, but "
6759 "argument %qD is",
6760 parm, arg);
6761 return unify_invalid (explain_p);
6762 }
6763
6764 static int
6765 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6766 {
6767 if (explain_p)
6768 inform (input_location,
6769 " template argument %qE does not match "
6770 "pointer-to-member constant %qE",
6771 arg, parm);
6772 return unify_invalid (explain_p);
6773 }
6774
6775 static int
6776 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6777 {
6778 if (explain_p)
6779 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6780 return unify_invalid (explain_p);
6781 }
6782
6783 static int
6784 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6785 {
6786 if (explain_p)
6787 inform (input_location,
6788 " inconsistent parameter pack deduction with %qT and %qT",
6789 old_arg, new_arg);
6790 return unify_invalid (explain_p);
6791 }
6792
6793 static int
6794 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6795 {
6796 if (explain_p)
6797 {
6798 if (TYPE_P (parm))
6799 inform (input_location,
6800 " deduced conflicting types for parameter %qT (%qT and %qT)",
6801 parm, first, second);
6802 else
6803 inform (input_location,
6804 " deduced conflicting values for non-type parameter "
6805 "%qE (%qE and %qE)", parm, first, second);
6806 }
6807 return unify_invalid (explain_p);
6808 }
6809
6810 static int
6811 unify_vla_arg (bool explain_p, tree arg)
6812 {
6813 if (explain_p)
6814 inform (input_location,
6815 " variable-sized array type %qT is not "
6816 "a valid template argument",
6817 arg);
6818 return unify_invalid (explain_p);
6819 }
6820
6821 static int
6822 unify_method_type_error (bool explain_p, tree arg)
6823 {
6824 if (explain_p)
6825 inform (input_location,
6826 " member function type %qT is not a valid template argument",
6827 arg);
6828 return unify_invalid (explain_p);
6829 }
6830
6831 static int
6832 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6833 {
6834 if (explain_p)
6835 {
6836 if (least_p)
6837 inform_n (input_location, wanted,
6838 " candidate expects at least %d argument, %d provided",
6839 " candidate expects at least %d arguments, %d provided",
6840 wanted, have);
6841 else
6842 inform_n (input_location, wanted,
6843 " candidate expects %d argument, %d provided",
6844 " candidate expects %d arguments, %d provided",
6845 wanted, have);
6846 }
6847 return unify_invalid (explain_p);
6848 }
6849
6850 static int
6851 unify_too_many_arguments (bool explain_p, int have, int wanted)
6852 {
6853 return unify_arity (explain_p, have, wanted);
6854 }
6855
6856 static int
6857 unify_too_few_arguments (bool explain_p, int have, int wanted,
6858 bool least_p = false)
6859 {
6860 return unify_arity (explain_p, have, wanted, least_p);
6861 }
6862
6863 static int
6864 unify_arg_conversion (bool explain_p, tree to_type,
6865 tree from_type, tree arg)
6866 {
6867 if (explain_p)
6868 inform (cp_expr_loc_or_input_loc (arg),
6869 " cannot convert %qE (type %qT) to type %qT",
6870 arg, from_type, to_type);
6871 return unify_invalid (explain_p);
6872 }
6873
6874 static int
6875 unify_no_common_base (bool explain_p, enum template_base_result r,
6876 tree parm, tree arg)
6877 {
6878 if (explain_p)
6879 switch (r)
6880 {
6881 case tbr_ambiguous_baseclass:
6882 inform (input_location, " %qT is an ambiguous base class of %qT",
6883 parm, arg);
6884 break;
6885 default:
6886 inform (input_location, " %qT is not derived from %qT", arg, parm);
6887 break;
6888 }
6889 return unify_invalid (explain_p);
6890 }
6891
6892 static int
6893 unify_inconsistent_template_template_parameters (bool explain_p)
6894 {
6895 if (explain_p)
6896 inform (input_location,
6897 " template parameters of a template template argument are "
6898 "inconsistent with other deduced template arguments");
6899 return unify_invalid (explain_p);
6900 }
6901
6902 static int
6903 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6904 {
6905 if (explain_p)
6906 inform (input_location,
6907 " cannot deduce a template for %qT from non-template type %qT",
6908 parm, arg);
6909 return unify_invalid (explain_p);
6910 }
6911
6912 static int
6913 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6914 {
6915 if (explain_p)
6916 inform (input_location,
6917 " template argument %qE does not match %qE", arg, parm);
6918 return unify_invalid (explain_p);
6919 }
6920
6921 /* True if T is a C++20 template parameter object to store the argument for a
6922 template parameter of class type. */
6923
6924 bool
6925 template_parm_object_p (const_tree t)
6926 {
6927 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6928 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6929 }
6930
6931 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6932 argument for TYPE, points to an unsuitable object.
6933
6934 Also adjust the type of the index in C++20 array subobject references. */
6935
6936 static bool
6937 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6938 {
6939 switch (TREE_CODE (expr))
6940 {
6941 CASE_CONVERT:
6942 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6943 complain);
6944
6945 case TARGET_EXPR:
6946 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6947 complain);
6948
6949 case CONSTRUCTOR:
6950 {
6951 unsigned i; tree elt;
6952 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6953 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6954 return true;
6955 }
6956 break;
6957
6958 case ADDR_EXPR:
6959 {
6960 tree decl = TREE_OPERAND (expr, 0);
6961
6962 if (cxx_dialect >= cxx20)
6963 while (TREE_CODE (decl) == COMPONENT_REF
6964 || TREE_CODE (decl) == ARRAY_REF)
6965 {
6966 tree &op = TREE_OPERAND (decl, 1);
6967 if (TREE_CODE (decl) == ARRAY_REF
6968 && TREE_CODE (op) == INTEGER_CST)
6969 /* Canonicalize array offsets to ptrdiff_t; how they were
6970 written doesn't matter for subobject identity. */
6971 op = fold_convert (ptrdiff_type_node, op);
6972 decl = TREE_OPERAND (decl, 0);
6973 }
6974
6975 if (!VAR_P (decl))
6976 {
6977 if (complain & tf_error)
6978 error_at (cp_expr_loc_or_input_loc (expr),
6979 "%qE is not a valid template argument of type %qT "
6980 "because %qE is not a variable", expr, type, decl);
6981 return true;
6982 }
6983 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6984 {
6985 if (complain & tf_error)
6986 error_at (cp_expr_loc_or_input_loc (expr),
6987 "%qE is not a valid template argument of type %qT "
6988 "in C++98 because %qD does not have external linkage",
6989 expr, type, decl);
6990 return true;
6991 }
6992 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6993 && decl_linkage (decl) == lk_none)
6994 {
6995 if (complain & tf_error)
6996 error_at (cp_expr_loc_or_input_loc (expr),
6997 "%qE is not a valid template argument of type %qT "
6998 "because %qD has no linkage", expr, type, decl);
6999 return true;
7000 }
7001 /* C++17: For a non-type template-parameter of reference or pointer
7002 type, the value of the constant expression shall not refer to (or
7003 for a pointer type, shall not be the address of):
7004 * a subobject (4.5),
7005 * a temporary object (15.2),
7006 * a string literal (5.13.5),
7007 * the result of a typeid expression (8.2.8), or
7008 * a predefined __func__ variable (11.4.1). */
7009 else if (DECL_ARTIFICIAL (decl))
7010 {
7011 if (complain & tf_error)
7012 error ("the address of %qD is not a valid template argument",
7013 decl);
7014 return true;
7015 }
7016 else if (cxx_dialect < cxx20
7017 && !(same_type_ignoring_top_level_qualifiers_p
7018 (strip_array_types (TREE_TYPE (type)),
7019 strip_array_types (TREE_TYPE (decl)))))
7020 {
7021 if (complain & tf_error)
7022 error ("the address of the %qT subobject of %qD is not a "
7023 "valid template argument", TREE_TYPE (type), decl);
7024 return true;
7025 }
7026 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7027 {
7028 if (complain & tf_error)
7029 error ("the address of %qD is not a valid template argument "
7030 "because it does not have static storage duration",
7031 decl);
7032 return true;
7033 }
7034 }
7035 break;
7036
7037 default:
7038 if (!INDIRECT_TYPE_P (type))
7039 /* We're only concerned about pointers and references here. */;
7040 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7041 /* Null pointer values are OK in C++11. */;
7042 else
7043 {
7044 if (VAR_P (expr))
7045 {
7046 if (complain & tf_error)
7047 error ("%qD is not a valid template argument "
7048 "because %qD is a variable, not the address of "
7049 "a variable", expr, expr);
7050 return true;
7051 }
7052 else
7053 {
7054 if (complain & tf_error)
7055 error ("%qE is not a valid template argument for %qT "
7056 "because it is not the address of a variable",
7057 expr, type);
7058 return true;
7059 }
7060 }
7061 }
7062 return false;
7063
7064 }
7065
7066 /* The template arguments corresponding to template parameter objects of types
7067 that contain pointers to members. */
7068
7069 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7070
7071 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7072 template argument EXPR. */
7073
7074 static tree
7075 get_template_parm_object (tree expr, tsubst_flags_t complain)
7076 {
7077 if (TREE_CODE (expr) == TARGET_EXPR)
7078 expr = TARGET_EXPR_INITIAL (expr);
7079
7080 if (!TREE_CONSTANT (expr))
7081 {
7082 if ((complain & tf_error)
7083 && require_rvalue_constant_expression (expr))
7084 cxx_constant_value (expr);
7085 return error_mark_node;
7086 }
7087 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7088 return error_mark_node;
7089
7090 tree name = mangle_template_parm_object (expr);
7091 tree decl = get_global_binding (name);
7092 if (decl)
7093 return decl;
7094
7095 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7096 decl = create_temporary_var (type);
7097 TREE_STATIC (decl) = true;
7098 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7099 TREE_READONLY (decl) = true;
7100 DECL_NAME (decl) = name;
7101 SET_DECL_ASSEMBLER_NAME (decl, name);
7102 DECL_CONTEXT (decl) = global_namespace;
7103 comdat_linkage (decl);
7104
7105 if (!zero_init_p (type))
7106 {
7107 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7108 lower_var_init before we're done mangling. So store the original
7109 value elsewhere. */
7110 tree copy = unshare_constructor (expr);
7111 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7112 }
7113
7114 pushdecl_top_level_and_finish (decl, expr);
7115
7116 return decl;
7117 }
7118
7119 /* Return the actual template argument corresponding to template parameter
7120 object VAR. */
7121
7122 tree
7123 tparm_object_argument (tree var)
7124 {
7125 if (zero_init_p (TREE_TYPE (var)))
7126 return DECL_INITIAL (var);
7127 return *(tparm_obj_values->get (var));
7128 }
7129
7130 /* Attempt to convert the non-type template parameter EXPR to the
7131 indicated TYPE. If the conversion is successful, return the
7132 converted value. If the conversion is unsuccessful, return
7133 NULL_TREE if we issued an error message, or error_mark_node if we
7134 did not. We issue error messages for out-and-out bad template
7135 parameters, but not simply because the conversion failed, since we
7136 might be just trying to do argument deduction. Both TYPE and EXPR
7137 must be non-dependent.
7138
7139 The conversion follows the special rules described in
7140 [temp.arg.nontype], and it is much more strict than an implicit
7141 conversion.
7142
7143 This function is called twice for each template argument (see
7144 lookup_template_class for a more accurate description of this
7145 problem). This means that we need to handle expressions which
7146 are not valid in a C++ source, but can be created from the
7147 first call (for instance, casts to perform conversions). These
7148 hacks can go away after we fix the double coercion problem. */
7149
7150 static tree
7151 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7152 {
7153 tree expr_type;
7154 location_t loc = cp_expr_loc_or_input_loc (expr);
7155
7156 /* Detect immediately string literals as invalid non-type argument.
7157 This special-case is not needed for correctness (we would easily
7158 catch this later), but only to provide better diagnostic for this
7159 common user mistake. As suggested by DR 100, we do not mention
7160 linkage issues in the diagnostic as this is not the point. */
7161 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7162 {
7163 if (complain & tf_error)
7164 error ("%qE is not a valid template argument for type %qT "
7165 "because string literals can never be used in this context",
7166 expr, type);
7167 return NULL_TREE;
7168 }
7169
7170 /* Add the ADDR_EXPR now for the benefit of
7171 value_dependent_expression_p. */
7172 if (TYPE_PTROBV_P (type)
7173 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7174 {
7175 expr = decay_conversion (expr, complain);
7176 if (expr == error_mark_node)
7177 return error_mark_node;
7178 }
7179
7180 /* If we are in a template, EXPR may be non-dependent, but still
7181 have a syntactic, rather than semantic, form. For example, EXPR
7182 might be a SCOPE_REF, rather than the VAR_DECL to which the
7183 SCOPE_REF refers. Preserving the qualifying scope is necessary
7184 so that access checking can be performed when the template is
7185 instantiated -- but here we need the resolved form so that we can
7186 convert the argument. */
7187 bool non_dep = false;
7188 if (TYPE_REF_OBJ_P (type)
7189 && has_value_dependent_address (expr))
7190 /* If we want the address and it's value-dependent, don't fold. */;
7191 else if (processing_template_decl
7192 && is_nondependent_constant_expression (expr))
7193 non_dep = true;
7194 if (error_operand_p (expr))
7195 return error_mark_node;
7196 expr_type = TREE_TYPE (expr);
7197
7198 /* If the argument is non-dependent, perform any conversions in
7199 non-dependent context as well. */
7200 processing_template_decl_sentinel s (non_dep);
7201 if (non_dep)
7202 expr = instantiate_non_dependent_expr_internal (expr, complain);
7203
7204 const bool val_dep_p = value_dependent_expression_p (expr);
7205 if (val_dep_p)
7206 expr = canonicalize_expr_argument (expr, complain);
7207
7208 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7209 to a non-type argument of "nullptr". */
7210 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7211 expr = fold_simple (convert (type, expr));
7212
7213 /* In C++11, integral or enumeration non-type template arguments can be
7214 arbitrary constant expressions. Pointer and pointer to
7215 member arguments can be general constant expressions that evaluate
7216 to a null value, but otherwise still need to be of a specific form. */
7217 if (cxx_dialect >= cxx11)
7218 {
7219 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7220 /* A PTRMEM_CST is already constant, and a valid template
7221 argument for a parameter of pointer to member type, we just want
7222 to leave it in that form rather than lower it to a
7223 CONSTRUCTOR. */;
7224 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7225 || cxx_dialect >= cxx17)
7226 {
7227 /* C++17: A template-argument for a non-type template-parameter shall
7228 be a converted constant expression (8.20) of the type of the
7229 template-parameter. */
7230 expr = build_converted_constant_expr (type, expr, complain);
7231 if (expr == error_mark_node)
7232 /* Make sure we return NULL_TREE only if we have really issued
7233 an error, as described above. */
7234 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7235 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7236 {
7237 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7238 return expr;
7239 }
7240 expr = maybe_constant_value (expr, NULL_TREE,
7241 /*manifestly_const_eval=*/true);
7242 expr = convert_from_reference (expr);
7243 }
7244 else if (TYPE_PTR_OR_PTRMEM_P (type))
7245 {
7246 tree folded = maybe_constant_value (expr, NULL_TREE,
7247 /*manifestly_const_eval=*/true);
7248 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7249 : null_member_pointer_value_p (folded))
7250 expr = folded;
7251 }
7252 }
7253
7254 if (TYPE_REF_P (type))
7255 expr = mark_lvalue_use (expr);
7256 else
7257 expr = mark_rvalue_use (expr);
7258
7259 /* HACK: Due to double coercion, we can get a
7260 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7261 which is the tree that we built on the first call (see
7262 below when coercing to reference to object or to reference to
7263 function). We just strip everything and get to the arg.
7264 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7265 for examples. */
7266 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7267 {
7268 tree probe_type, probe = expr;
7269 if (REFERENCE_REF_P (probe))
7270 probe = TREE_OPERAND (probe, 0);
7271 probe_type = TREE_TYPE (probe);
7272 if (TREE_CODE (probe) == NOP_EXPR)
7273 {
7274 /* ??? Maybe we could use convert_from_reference here, but we
7275 would need to relax its constraints because the NOP_EXPR
7276 could actually change the type to something more cv-qualified,
7277 and this is not folded by convert_from_reference. */
7278 tree addr = TREE_OPERAND (probe, 0);
7279 if (TYPE_REF_P (probe_type)
7280 && TREE_CODE (addr) == ADDR_EXPR
7281 && TYPE_PTR_P (TREE_TYPE (addr))
7282 && (same_type_ignoring_top_level_qualifiers_p
7283 (TREE_TYPE (probe_type),
7284 TREE_TYPE (TREE_TYPE (addr)))))
7285 {
7286 expr = TREE_OPERAND (addr, 0);
7287 expr_type = TREE_TYPE (probe_type);
7288 }
7289 }
7290 }
7291
7292 /* [temp.arg.nontype]/5, bullet 1
7293
7294 For a non-type template-parameter of integral or enumeration type,
7295 integral promotions (_conv.prom_) and integral conversions
7296 (_conv.integral_) are applied. */
7297 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7298 || TREE_CODE (type) == REAL_TYPE)
7299 {
7300 if (cxx_dialect < cxx11)
7301 {
7302 tree t = build_converted_constant_expr (type, expr, complain);
7303 t = maybe_constant_value (t);
7304 if (t != error_mark_node)
7305 expr = t;
7306 }
7307
7308 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7309 return error_mark_node;
7310
7311 /* Notice that there are constant expressions like '4 % 0' which
7312 do not fold into integer constants. */
7313 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7314 {
7315 if (complain & tf_error)
7316 {
7317 int errs = errorcount, warns = warningcount + werrorcount;
7318 if (!require_potential_constant_expression (expr))
7319 expr = error_mark_node;
7320 else
7321 expr = cxx_constant_value (expr);
7322 if (errorcount > errs || warningcount + werrorcount > warns)
7323 inform (loc, "in template argument for type %qT", type);
7324 if (expr == error_mark_node)
7325 return NULL_TREE;
7326 /* else cxx_constant_value complained but gave us
7327 a real constant, so go ahead. */
7328 if (!CONSTANT_CLASS_P (expr))
7329 {
7330 /* Some assemble time constant expressions like
7331 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7332 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7333 as we can emit them into .rodata initializers of
7334 variables, yet they can't fold into an INTEGER_CST at
7335 compile time. Refuse them here. */
7336 gcc_checking_assert (reduced_constant_expression_p (expr));
7337 error_at (loc, "template argument %qE for type %qT not "
7338 "a compile-time constant", expr, type);
7339 return NULL_TREE;
7340 }
7341 }
7342 else
7343 return NULL_TREE;
7344 }
7345
7346 /* Avoid typedef problems. */
7347 if (TREE_TYPE (expr) != type)
7348 expr = fold_convert (type, expr);
7349 }
7350 /* [temp.arg.nontype]/5, bullet 2
7351
7352 For a non-type template-parameter of type pointer to object,
7353 qualification conversions (_conv.qual_) and the array-to-pointer
7354 conversion (_conv.array_) are applied. */
7355 else if (TYPE_PTROBV_P (type))
7356 {
7357 tree decayed = expr;
7358
7359 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7360 decay_conversion or an explicit cast. If it's a problematic cast,
7361 we'll complain about it below. */
7362 if (TREE_CODE (expr) == NOP_EXPR)
7363 {
7364 tree probe = expr;
7365 STRIP_NOPS (probe);
7366 if (TREE_CODE (probe) == ADDR_EXPR
7367 && TYPE_PTR_P (TREE_TYPE (probe)))
7368 {
7369 expr = probe;
7370 expr_type = TREE_TYPE (expr);
7371 }
7372 }
7373
7374 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7375
7376 A template-argument for a non-type, non-template template-parameter
7377 shall be one of: [...]
7378
7379 -- the name of a non-type template-parameter;
7380 -- the address of an object or function with external linkage, [...]
7381 expressed as "& id-expression" where the & is optional if the name
7382 refers to a function or array, or if the corresponding
7383 template-parameter is a reference.
7384
7385 Here, we do not care about functions, as they are invalid anyway
7386 for a parameter of type pointer-to-object. */
7387
7388 if (val_dep_p)
7389 /* Non-type template parameters are OK. */
7390 ;
7391 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7392 /* Null pointer values are OK in C++11. */;
7393 else if (TREE_CODE (expr) != ADDR_EXPR
7394 && !INDIRECT_TYPE_P (expr_type))
7395 /* Other values, like integer constants, might be valid
7396 non-type arguments of some other type. */
7397 return error_mark_node;
7398 else if (invalid_tparm_referent_p (type, expr, complain))
7399 return NULL_TREE;
7400
7401 expr = decayed;
7402
7403 expr = perform_qualification_conversions (type, expr);
7404 if (expr == error_mark_node)
7405 return error_mark_node;
7406 }
7407 /* [temp.arg.nontype]/5, bullet 3
7408
7409 For a non-type template-parameter of type reference to object, no
7410 conversions apply. The type referred to by the reference may be more
7411 cv-qualified than the (otherwise identical) type of the
7412 template-argument. The template-parameter is bound directly to the
7413 template-argument, which must be an lvalue. */
7414 else if (TYPE_REF_OBJ_P (type))
7415 {
7416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7417 expr_type))
7418 return error_mark_node;
7419
7420 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7421 {
7422 if (complain & tf_error)
7423 error ("%qE is not a valid template argument for type %qT "
7424 "because of conflicts in cv-qualification", expr, type);
7425 return NULL_TREE;
7426 }
7427
7428 if (!lvalue_p (expr))
7429 {
7430 if (complain & tf_error)
7431 error ("%qE is not a valid template argument for type %qT "
7432 "because it is not an lvalue", expr, type);
7433 return NULL_TREE;
7434 }
7435
7436 /* [temp.arg.nontype]/1
7437
7438 A template-argument for a non-type, non-template template-parameter
7439 shall be one of: [...]
7440
7441 -- the address of an object or function with external linkage. */
7442 if (INDIRECT_REF_P (expr)
7443 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7444 {
7445 expr = TREE_OPERAND (expr, 0);
7446 if (DECL_P (expr))
7447 {
7448 if (complain & tf_error)
7449 error ("%q#D is not a valid template argument for type %qT "
7450 "because a reference variable does not have a constant "
7451 "address", expr, type);
7452 return NULL_TREE;
7453 }
7454 }
7455
7456 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7457 /* OK, dependent reference. We don't want to ask whether a DECL is
7458 itself value-dependent, since what we want here is its address. */;
7459 else
7460 {
7461 expr = build_address (expr);
7462
7463 if (invalid_tparm_referent_p (type, expr, complain))
7464 return NULL_TREE;
7465 }
7466
7467 if (!same_type_p (type, TREE_TYPE (expr)))
7468 expr = build_nop (type, expr);
7469 }
7470 /* [temp.arg.nontype]/5, bullet 4
7471
7472 For a non-type template-parameter of type pointer to function, only
7473 the function-to-pointer conversion (_conv.func_) is applied. If the
7474 template-argument represents a set of overloaded functions (or a
7475 pointer to such), the matching function is selected from the set
7476 (_over.over_). */
7477 else if (TYPE_PTRFN_P (type))
7478 {
7479 /* If the argument is a template-id, we might not have enough
7480 context information to decay the pointer. */
7481 if (!type_unknown_p (expr_type))
7482 {
7483 expr = decay_conversion (expr, complain);
7484 if (expr == error_mark_node)
7485 return error_mark_node;
7486 }
7487
7488 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7489 /* Null pointer values are OK in C++11. */
7490 return perform_qualification_conversions (type, expr);
7491
7492 expr = convert_nontype_argument_function (type, expr, complain);
7493 if (!expr || expr == error_mark_node)
7494 return expr;
7495 }
7496 /* [temp.arg.nontype]/5, bullet 5
7497
7498 For a non-type template-parameter of type reference to function, no
7499 conversions apply. If the template-argument represents a set of
7500 overloaded functions, the matching function is selected from the set
7501 (_over.over_). */
7502 else if (TYPE_REFFN_P (type))
7503 {
7504 if (TREE_CODE (expr) == ADDR_EXPR)
7505 {
7506 if (complain & tf_error)
7507 {
7508 error ("%qE is not a valid template argument for type %qT "
7509 "because it is a pointer", expr, type);
7510 inform (input_location, "try using %qE instead",
7511 TREE_OPERAND (expr, 0));
7512 }
7513 return NULL_TREE;
7514 }
7515
7516 expr = convert_nontype_argument_function (type, expr, complain);
7517 if (!expr || expr == error_mark_node)
7518 return expr;
7519 }
7520 /* [temp.arg.nontype]/5, bullet 6
7521
7522 For a non-type template-parameter of type pointer to member function,
7523 no conversions apply. If the template-argument represents a set of
7524 overloaded member functions, the matching member function is selected
7525 from the set (_over.over_). */
7526 else if (TYPE_PTRMEMFUNC_P (type))
7527 {
7528 expr = instantiate_type (type, expr, tf_none);
7529 if (expr == error_mark_node)
7530 return error_mark_node;
7531
7532 /* [temp.arg.nontype] bullet 1 says the pointer to member
7533 expression must be a pointer-to-member constant. */
7534 if (!val_dep_p
7535 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7536 return NULL_TREE;
7537
7538 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7539 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7540 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7541 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7542 }
7543 /* [temp.arg.nontype]/5, bullet 7
7544
7545 For a non-type template-parameter of type pointer to data member,
7546 qualification conversions (_conv.qual_) are applied. */
7547 else if (TYPE_PTRDATAMEM_P (type))
7548 {
7549 /* [temp.arg.nontype] bullet 1 says the pointer to member
7550 expression must be a pointer-to-member constant. */
7551 if (!val_dep_p
7552 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7553 return NULL_TREE;
7554
7555 expr = perform_qualification_conversions (type, expr);
7556 if (expr == error_mark_node)
7557 return expr;
7558 }
7559 else if (NULLPTR_TYPE_P (type))
7560 {
7561 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7562 {
7563 if (complain & tf_error)
7564 error ("%qE is not a valid template argument for type %qT "
7565 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7566 return NULL_TREE;
7567 }
7568 return expr;
7569 }
7570 else if (CLASS_TYPE_P (type))
7571 {
7572 /* Replace the argument with a reference to the corresponding template
7573 parameter object. */
7574 if (!val_dep_p)
7575 expr = get_template_parm_object (expr, complain);
7576 if (expr == error_mark_node)
7577 return NULL_TREE;
7578 }
7579 /* A template non-type parameter must be one of the above. */
7580 else
7581 gcc_unreachable ();
7582
7583 /* Sanity check: did we actually convert the argument to the
7584 right type? */
7585 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7586 (type, TREE_TYPE (expr)));
7587 return convert_from_reference (expr);
7588 }
7589
7590 /* Subroutine of coerce_template_template_parms, which returns 1 if
7591 PARM_PARM and ARG_PARM match using the rule for the template
7592 parameters of template template parameters. Both PARM and ARG are
7593 template parameters; the rest of the arguments are the same as for
7594 coerce_template_template_parms.
7595 */
7596 static int
7597 coerce_template_template_parm (tree parm,
7598 tree arg,
7599 tsubst_flags_t complain,
7600 tree in_decl,
7601 tree outer_args)
7602 {
7603 if (arg == NULL_TREE || error_operand_p (arg)
7604 || parm == NULL_TREE || error_operand_p (parm))
7605 return 0;
7606
7607 if (TREE_CODE (arg) != TREE_CODE (parm))
7608 return 0;
7609
7610 switch (TREE_CODE (parm))
7611 {
7612 case TEMPLATE_DECL:
7613 /* We encounter instantiations of templates like
7614 template <template <template <class> class> class TT>
7615 class C; */
7616 {
7617 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7618 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7619
7620 if (!coerce_template_template_parms
7621 (parmparm, argparm, complain, in_decl, outer_args))
7622 return 0;
7623 }
7624 /* Fall through. */
7625
7626 case TYPE_DECL:
7627 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7628 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7629 /* Argument is a parameter pack but parameter is not. */
7630 return 0;
7631 break;
7632
7633 case PARM_DECL:
7634 /* The tsubst call is used to handle cases such as
7635
7636 template <int> class C {};
7637 template <class T, template <T> class TT> class D {};
7638 D<int, C> d;
7639
7640 i.e. the parameter list of TT depends on earlier parameters. */
7641 if (!uses_template_parms (TREE_TYPE (arg)))
7642 {
7643 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7644 if (!uses_template_parms (t)
7645 && !same_type_p (t, TREE_TYPE (arg)))
7646 return 0;
7647 }
7648
7649 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7650 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7651 /* Argument is a parameter pack but parameter is not. */
7652 return 0;
7653
7654 break;
7655
7656 default:
7657 gcc_unreachable ();
7658 }
7659
7660 return 1;
7661 }
7662
7663 /* Coerce template argument list ARGLIST for use with template
7664 template-parameter TEMPL. */
7665
7666 static tree
7667 coerce_template_args_for_ttp (tree templ, tree arglist,
7668 tsubst_flags_t complain)
7669 {
7670 /* Consider an example where a template template parameter declared as
7671
7672 template <class T, class U = std::allocator<T> > class TT
7673
7674 The template parameter level of T and U are one level larger than
7675 of TT. To proper process the default argument of U, say when an
7676 instantiation `TT<int>' is seen, we need to build the full
7677 arguments containing {int} as the innermost level. Outer levels,
7678 available when not appearing as default template argument, can be
7679 obtained from the arguments of the enclosing template.
7680
7681 Suppose that TT is later substituted with std::vector. The above
7682 instantiation is `TT<int, std::allocator<T> >' with TT at
7683 level 1, and T at level 2, while the template arguments at level 1
7684 becomes {std::vector} and the inner level 2 is {int}. */
7685
7686 tree outer = DECL_CONTEXT (templ);
7687 if (outer)
7688 outer = generic_targs_for (outer);
7689 else if (current_template_parms)
7690 {
7691 /* This is an argument of the current template, so we haven't set
7692 DECL_CONTEXT yet. */
7693 tree relevant_template_parms;
7694
7695 /* Parameter levels that are greater than the level of the given
7696 template template parm are irrelevant. */
7697 relevant_template_parms = current_template_parms;
7698 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7699 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7700 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7701
7702 outer = template_parms_to_args (relevant_template_parms);
7703 }
7704
7705 if (outer)
7706 arglist = add_to_template_args (outer, arglist);
7707
7708 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7709 return coerce_template_parms (parmlist, arglist, templ,
7710 complain,
7711 /*require_all_args=*/true,
7712 /*use_default_args=*/true);
7713 }
7714
7715 /* A cache of template template parameters with match-all default
7716 arguments. */
7717 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7718
7719 /* T is a bound template template-parameter. Copy its arguments into default
7720 arguments of the template template-parameter's template parameters. */
7721
7722 static tree
7723 add_defaults_to_ttp (tree otmpl)
7724 {
7725 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7726 return *c;
7727
7728 tree ntmpl = copy_node (otmpl);
7729
7730 tree ntype = copy_node (TREE_TYPE (otmpl));
7731 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7732 TYPE_MAIN_VARIANT (ntype) = ntype;
7733 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7734 TYPE_NAME (ntype) = ntmpl;
7735 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7736
7737 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7738 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7739 TEMPLATE_PARM_DECL (idx) = ntmpl;
7740 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7741
7742 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7743 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7744 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7745 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7746 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7747 {
7748 tree o = TREE_VEC_ELT (vec, i);
7749 if (!template_parameter_pack_p (TREE_VALUE (o)))
7750 {
7751 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7752 TREE_PURPOSE (n) = any_targ_node;
7753 }
7754 }
7755
7756 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7757 return ntmpl;
7758 }
7759
7760 /* ARG is a bound potential template template-argument, and PARGS is a list
7761 of arguments for the corresponding template template-parameter. Adjust
7762 PARGS as appropriate for application to ARG's template, and if ARG is a
7763 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7764 arguments to the template template parameter. */
7765
7766 static tree
7767 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7768 {
7769 ++processing_template_decl;
7770 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7771 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7772 {
7773 /* When comparing two template template-parameters in partial ordering,
7774 rewrite the one currently being used as an argument to have default
7775 arguments for all parameters. */
7776 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7777 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7778 if (pargs != error_mark_node)
7779 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7780 TYPE_TI_ARGS (arg));
7781 }
7782 else
7783 {
7784 tree aparms
7785 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7786 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7787 /*require_all*/true,
7788 /*use_default*/true);
7789 }
7790 --processing_template_decl;
7791 return pargs;
7792 }
7793
7794 /* Subroutine of unify for the case when PARM is a
7795 BOUND_TEMPLATE_TEMPLATE_PARM. */
7796
7797 static int
7798 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7799 bool explain_p)
7800 {
7801 tree parmvec = TYPE_TI_ARGS (parm);
7802 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7803
7804 /* The template template parm might be variadic and the argument
7805 not, so flatten both argument lists. */
7806 parmvec = expand_template_argument_pack (parmvec);
7807 argvec = expand_template_argument_pack (argvec);
7808
7809 if (flag_new_ttp)
7810 {
7811 /* In keeping with P0522R0, adjust P's template arguments
7812 to apply to A's template; then flatten it again. */
7813 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7814 nparmvec = expand_template_argument_pack (nparmvec);
7815
7816 if (unify (tparms, targs, nparmvec, argvec,
7817 UNIFY_ALLOW_NONE, explain_p))
7818 return 1;
7819
7820 /* If the P0522 adjustment eliminated a pack expansion, deduce
7821 empty packs. */
7822 if (flag_new_ttp
7823 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7824 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7825 DEDUCE_EXACT, /*sub*/true, explain_p))
7826 return 1;
7827 }
7828 else
7829 {
7830 /* Deduce arguments T, i from TT<T> or TT<i>.
7831 We check each element of PARMVEC and ARGVEC individually
7832 rather than the whole TREE_VEC since they can have
7833 different number of elements, which is allowed under N2555. */
7834
7835 int len = TREE_VEC_LENGTH (parmvec);
7836
7837 /* Check if the parameters end in a pack, making them
7838 variadic. */
7839 int parm_variadic_p = 0;
7840 if (len > 0
7841 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7842 parm_variadic_p = 1;
7843
7844 for (int i = 0; i < len - parm_variadic_p; ++i)
7845 /* If the template argument list of P contains a pack
7846 expansion that is not the last template argument, the
7847 entire template argument list is a non-deduced
7848 context. */
7849 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7850 return unify_success (explain_p);
7851
7852 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7853 return unify_too_few_arguments (explain_p,
7854 TREE_VEC_LENGTH (argvec), len);
7855
7856 for (int i = 0; i < len - parm_variadic_p; ++i)
7857 if (unify (tparms, targs,
7858 TREE_VEC_ELT (parmvec, i),
7859 TREE_VEC_ELT (argvec, i),
7860 UNIFY_ALLOW_NONE, explain_p))
7861 return 1;
7862
7863 if (parm_variadic_p
7864 && unify_pack_expansion (tparms, targs,
7865 parmvec, argvec,
7866 DEDUCE_EXACT,
7867 /*subr=*/true, explain_p))
7868 return 1;
7869 }
7870
7871 return 0;
7872 }
7873
7874 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7875 template template parameters. Both PARM_PARMS and ARG_PARMS are
7876 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7877 or PARM_DECL.
7878
7879 Consider the example:
7880 template <class T> class A;
7881 template<template <class U> class TT> class B;
7882
7883 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7884 the parameters to A, and OUTER_ARGS contains A. */
7885
7886 static int
7887 coerce_template_template_parms (tree parm_parms,
7888 tree arg_parms,
7889 tsubst_flags_t complain,
7890 tree in_decl,
7891 tree outer_args)
7892 {
7893 int nparms, nargs, i;
7894 tree parm, arg;
7895 int variadic_p = 0;
7896
7897 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7898 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7899
7900 nparms = TREE_VEC_LENGTH (parm_parms);
7901 nargs = TREE_VEC_LENGTH (arg_parms);
7902
7903 if (flag_new_ttp)
7904 {
7905 /* P0522R0: A template template-parameter P is at least as specialized as
7906 a template template-argument A if, given the following rewrite to two
7907 function templates, the function template corresponding to P is at
7908 least as specialized as the function template corresponding to A
7909 according to the partial ordering rules for function templates
7910 ([temp.func.order]). Given an invented class template X with the
7911 template parameter list of A (including default arguments):
7912
7913 * Each of the two function templates has the same template parameters,
7914 respectively, as P or A.
7915
7916 * Each function template has a single function parameter whose type is
7917 a specialization of X with template arguments corresponding to the
7918 template parameters from the respective function template where, for
7919 each template parameter PP in the template parameter list of the
7920 function template, a corresponding template argument AA is formed. If
7921 PP declares a parameter pack, then AA is the pack expansion
7922 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7923
7924 If the rewrite produces an invalid type, then P is not at least as
7925 specialized as A. */
7926
7927 /* So coerce P's args to apply to A's parms, and then deduce between A's
7928 args and the converted args. If that succeeds, A is at least as
7929 specialized as P, so they match.*/
7930 tree pargs = template_parms_level_to_args (parm_parms);
7931 pargs = add_outermost_template_args (outer_args, pargs);
7932 ++processing_template_decl;
7933 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7934 /*require_all*/true, /*use_default*/true);
7935 --processing_template_decl;
7936 if (pargs != error_mark_node)
7937 {
7938 tree targs = make_tree_vec (nargs);
7939 tree aargs = template_parms_level_to_args (arg_parms);
7940 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7941 /*explain*/false))
7942 return 1;
7943 }
7944 }
7945
7946 /* Determine whether we have a parameter pack at the end of the
7947 template template parameter's template parameter list. */
7948 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7949 {
7950 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7951
7952 if (error_operand_p (parm))
7953 return 0;
7954
7955 switch (TREE_CODE (parm))
7956 {
7957 case TEMPLATE_DECL:
7958 case TYPE_DECL:
7959 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7960 variadic_p = 1;
7961 break;
7962
7963 case PARM_DECL:
7964 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7965 variadic_p = 1;
7966 break;
7967
7968 default:
7969 gcc_unreachable ();
7970 }
7971 }
7972
7973 if (nargs != nparms
7974 && !(variadic_p && nargs >= nparms - 1))
7975 return 0;
7976
7977 /* Check all of the template parameters except the parameter pack at
7978 the end (if any). */
7979 for (i = 0; i < nparms - variadic_p; ++i)
7980 {
7981 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7982 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7983 continue;
7984
7985 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7986 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7987
7988 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7989 outer_args))
7990 return 0;
7991
7992 }
7993
7994 if (variadic_p)
7995 {
7996 /* Check each of the template parameters in the template
7997 argument against the template parameter pack at the end of
7998 the template template parameter. */
7999 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8000 return 0;
8001
8002 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8003
8004 for (; i < nargs; ++i)
8005 {
8006 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8007 continue;
8008
8009 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8010
8011 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8012 outer_args))
8013 return 0;
8014 }
8015 }
8016
8017 return 1;
8018 }
8019
8020 /* Verifies that the deduced template arguments (in TARGS) for the
8021 template template parameters (in TPARMS) represent valid bindings,
8022 by comparing the template parameter list of each template argument
8023 to the template parameter list of its corresponding template
8024 template parameter, in accordance with DR150. This
8025 routine can only be called after all template arguments have been
8026 deduced. It will return TRUE if all of the template template
8027 parameter bindings are okay, FALSE otherwise. */
8028 bool
8029 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8030 {
8031 int i, ntparms = TREE_VEC_LENGTH (tparms);
8032 bool ret = true;
8033
8034 /* We're dealing with template parms in this process. */
8035 ++processing_template_decl;
8036
8037 targs = INNERMOST_TEMPLATE_ARGS (targs);
8038
8039 for (i = 0; i < ntparms; ++i)
8040 {
8041 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8042 tree targ = TREE_VEC_ELT (targs, i);
8043
8044 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8045 {
8046 tree packed_args = NULL_TREE;
8047 int idx, len = 1;
8048
8049 if (ARGUMENT_PACK_P (targ))
8050 {
8051 /* Look inside the argument pack. */
8052 packed_args = ARGUMENT_PACK_ARGS (targ);
8053 len = TREE_VEC_LENGTH (packed_args);
8054 }
8055
8056 for (idx = 0; idx < len; ++idx)
8057 {
8058 tree targ_parms = NULL_TREE;
8059
8060 if (packed_args)
8061 /* Extract the next argument from the argument
8062 pack. */
8063 targ = TREE_VEC_ELT (packed_args, idx);
8064
8065 if (PACK_EXPANSION_P (targ))
8066 /* Look at the pattern of the pack expansion. */
8067 targ = PACK_EXPANSION_PATTERN (targ);
8068
8069 /* Extract the template parameters from the template
8070 argument. */
8071 if (TREE_CODE (targ) == TEMPLATE_DECL)
8072 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8073 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8074 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8075
8076 /* Verify that we can coerce the template template
8077 parameters from the template argument to the template
8078 parameter. This requires an exact match. */
8079 if (targ_parms
8080 && !coerce_template_template_parms
8081 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8082 targ_parms,
8083 tf_none,
8084 tparm,
8085 targs))
8086 {
8087 ret = false;
8088 goto out;
8089 }
8090 }
8091 }
8092 }
8093
8094 out:
8095
8096 --processing_template_decl;
8097 return ret;
8098 }
8099
8100 /* Since type attributes aren't mangled, we need to strip them from
8101 template type arguments. */
8102
8103 tree
8104 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8105 {
8106 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8107 return arg;
8108 bool removed_attributes = false;
8109 tree canon = strip_typedefs (arg, &removed_attributes);
8110 if (removed_attributes
8111 && (complain & tf_warning))
8112 warning (OPT_Wignored_attributes,
8113 "ignoring attributes on template argument %qT", arg);
8114 return canon;
8115 }
8116
8117 /* And from inside dependent non-type arguments like sizeof(Type). */
8118
8119 static tree
8120 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8121 {
8122 if (!arg || arg == error_mark_node)
8123 return arg;
8124 bool removed_attributes = false;
8125 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8126 if (removed_attributes
8127 && (complain & tf_warning))
8128 warning (OPT_Wignored_attributes,
8129 "ignoring attributes in template argument %qE", arg);
8130 return canon;
8131 }
8132
8133 // A template declaration can be substituted for a constrained
8134 // template template parameter only when the argument is more
8135 // constrained than the parameter.
8136 static bool
8137 is_compatible_template_arg (tree parm, tree arg)
8138 {
8139 tree parm_cons = get_constraints (parm);
8140
8141 /* For now, allow constrained template template arguments
8142 and unconstrained template template parameters. */
8143 if (parm_cons == NULL_TREE)
8144 return true;
8145
8146 /* If the template parameter is constrained, we need to rewrite its
8147 constraints in terms of the ARG's template parameters. This ensures
8148 that all of the template parameter types will have the same depth.
8149
8150 Note that this is only valid when coerce_template_template_parm is
8151 true for the innermost template parameters of PARM and ARG. In other
8152 words, because coercion is successful, this conversion will be valid. */
8153 tree new_args = NULL_TREE;
8154 if (parm_cons)
8155 {
8156 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8157 new_args = template_parms_level_to_args (aparms);
8158 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8159 tf_none, NULL_TREE);
8160 if (parm_cons == error_mark_node)
8161 return false;
8162 }
8163
8164 return weakly_subsumes (parm_cons, new_args, arg);
8165 }
8166
8167 // Convert a placeholder argument into a binding to the original
8168 // parameter. The original parameter is saved as the TREE_TYPE of
8169 // ARG.
8170 static inline tree
8171 convert_wildcard_argument (tree parm, tree arg)
8172 {
8173 TREE_TYPE (arg) = parm;
8174 return arg;
8175 }
8176
8177 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8178 because one of them is dependent. But we need to represent the
8179 conversion for the benefit of cp_tree_equal. */
8180
8181 static tree
8182 maybe_convert_nontype_argument (tree type, tree arg)
8183 {
8184 /* Auto parms get no conversion. */
8185 if (type_uses_auto (type))
8186 return arg;
8187 /* We don't need or want to add this conversion now if we're going to use the
8188 argument for deduction. */
8189 if (value_dependent_expression_p (arg))
8190 return arg;
8191
8192 type = cv_unqualified (type);
8193 tree argtype = TREE_TYPE (arg);
8194 if (same_type_p (type, argtype))
8195 return arg;
8196
8197 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8198 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8199 return arg;
8200 }
8201
8202 /* Convert the indicated template ARG as necessary to match the
8203 indicated template PARM. Returns the converted ARG, or
8204 error_mark_node if the conversion was unsuccessful. Error and
8205 warning messages are issued under control of COMPLAIN. This
8206 conversion is for the Ith parameter in the parameter list. ARGS is
8207 the full set of template arguments deduced so far. */
8208
8209 static tree
8210 convert_template_argument (tree parm,
8211 tree arg,
8212 tree args,
8213 tsubst_flags_t complain,
8214 int i,
8215 tree in_decl)
8216 {
8217 tree orig_arg;
8218 tree val;
8219 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8220
8221 if (parm == error_mark_node || error_operand_p (arg))
8222 return error_mark_node;
8223
8224 /* Trivially convert placeholders. */
8225 if (TREE_CODE (arg) == WILDCARD_DECL)
8226 return convert_wildcard_argument (parm, arg);
8227
8228 if (arg == any_targ_node)
8229 return arg;
8230
8231 if (TREE_CODE (arg) == TREE_LIST
8232 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8233 {
8234 /* The template argument was the name of some
8235 member function. That's usually
8236 invalid, but static members are OK. In any
8237 case, grab the underlying fields/functions
8238 and issue an error later if required. */
8239 TREE_TYPE (arg) = unknown_type_node;
8240 }
8241
8242 orig_arg = arg;
8243
8244 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8245 requires_type = (TREE_CODE (parm) == TYPE_DECL
8246 || requires_tmpl_type);
8247
8248 /* When determining whether an argument pack expansion is a template,
8249 look at the pattern. */
8250 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8251 arg = PACK_EXPANSION_PATTERN (arg);
8252
8253 /* Deal with an injected-class-name used as a template template arg. */
8254 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8255 {
8256 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8257 if (TREE_CODE (t) == TEMPLATE_DECL)
8258 {
8259 if (cxx_dialect >= cxx11)
8260 /* OK under DR 1004. */;
8261 else if (complain & tf_warning_or_error)
8262 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8263 " used as template template argument", TYPE_NAME (arg));
8264 else if (flag_pedantic_errors)
8265 t = arg;
8266
8267 arg = t;
8268 }
8269 }
8270
8271 is_tmpl_type =
8272 ((TREE_CODE (arg) == TEMPLATE_DECL
8273 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8274 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8275 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8276 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8277
8278 if (is_tmpl_type
8279 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8280 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8281 arg = TYPE_STUB_DECL (arg);
8282
8283 is_type = TYPE_P (arg) || is_tmpl_type;
8284
8285 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8286 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8287 {
8288 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8289 {
8290 if (complain & tf_error)
8291 error ("invalid use of destructor %qE as a type", orig_arg);
8292 return error_mark_node;
8293 }
8294
8295 permerror (input_location,
8296 "to refer to a type member of a template parameter, "
8297 "use %<typename %E%>", orig_arg);
8298
8299 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8300 TREE_OPERAND (arg, 1),
8301 typename_type,
8302 complain);
8303 arg = orig_arg;
8304 is_type = 1;
8305 }
8306 if (is_type != requires_type)
8307 {
8308 if (in_decl)
8309 {
8310 if (complain & tf_error)
8311 {
8312 error ("type/value mismatch at argument %d in template "
8313 "parameter list for %qD",
8314 i + 1, in_decl);
8315 if (is_type)
8316 {
8317 /* The template argument is a type, but we're expecting
8318 an expression. */
8319 inform (input_location,
8320 " expected a constant of type %qT, got %qT",
8321 TREE_TYPE (parm),
8322 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8323 /* [temp.arg]/2: "In a template-argument, an ambiguity
8324 between a type-id and an expression is resolved to a
8325 type-id, regardless of the form of the corresponding
8326 template-parameter." So give the user a clue. */
8327 if (TREE_CODE (arg) == FUNCTION_TYPE)
8328 inform (input_location, " ambiguous template argument "
8329 "for non-type template parameter is treated as "
8330 "function type");
8331 }
8332 else if (requires_tmpl_type)
8333 inform (input_location,
8334 " expected a class template, got %qE", orig_arg);
8335 else
8336 inform (input_location,
8337 " expected a type, got %qE", orig_arg);
8338 }
8339 }
8340 return error_mark_node;
8341 }
8342 if (is_tmpl_type ^ requires_tmpl_type)
8343 {
8344 if (in_decl && (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_tmpl_type)
8350 inform (input_location,
8351 " expected a type, got %qT", DECL_NAME (arg));
8352 else
8353 inform (input_location,
8354 " expected a class template, got %qT", orig_arg);
8355 }
8356 return error_mark_node;
8357 }
8358
8359 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8360 /* We already did the appropriate conversion when packing args. */
8361 val = orig_arg;
8362 else if (is_type)
8363 {
8364 if (requires_tmpl_type)
8365 {
8366 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8367 /* The number of argument required is not known yet.
8368 Just accept it for now. */
8369 val = orig_arg;
8370 else
8371 {
8372 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8373 tree argparm;
8374
8375 /* Strip alias templates that are equivalent to another
8376 template. */
8377 arg = get_underlying_template (arg);
8378 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8379
8380 if (coerce_template_template_parms (parmparm, argparm,
8381 complain, in_decl,
8382 args))
8383 {
8384 val = arg;
8385
8386 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8387 TEMPLATE_DECL. */
8388 if (val != error_mark_node)
8389 {
8390 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8391 val = TREE_TYPE (val);
8392 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8393 val = make_pack_expansion (val, complain);
8394 }
8395 }
8396 else
8397 {
8398 if (in_decl && (complain & tf_error))
8399 {
8400 error ("type/value mismatch at argument %d in "
8401 "template parameter list for %qD",
8402 i + 1, in_decl);
8403 inform (input_location,
8404 " expected a template of type %qD, got %qT",
8405 parm, orig_arg);
8406 }
8407
8408 val = error_mark_node;
8409 }
8410
8411 // Check that the constraints are compatible before allowing the
8412 // substitution.
8413 if (val != error_mark_node)
8414 if (!is_compatible_template_arg (parm, arg))
8415 {
8416 if (in_decl && (complain & tf_error))
8417 {
8418 error ("constraint mismatch at argument %d in "
8419 "template parameter list for %qD",
8420 i + 1, in_decl);
8421 inform (input_location, " expected %qD but got %qD",
8422 parm, arg);
8423 }
8424 val = error_mark_node;
8425 }
8426 }
8427 }
8428 else
8429 val = orig_arg;
8430 /* We only form one instance of each template specialization.
8431 Therefore, if we use a non-canonical variant (i.e., a
8432 typedef), any future messages referring to the type will use
8433 the typedef, which is confusing if those future uses do not
8434 themselves also use the typedef. */
8435 if (TYPE_P (val))
8436 val = canonicalize_type_argument (val, complain);
8437 }
8438 else
8439 {
8440 tree t = TREE_TYPE (parm);
8441
8442 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8443 > TMPL_ARGS_DEPTH (args))
8444 /* We don't have enough levels of args to do any substitution. This
8445 can happen in the context of -fnew-ttp-matching. */;
8446 else if (tree a = type_uses_auto (t))
8447 {
8448 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8449 if (t == error_mark_node)
8450 return error_mark_node;
8451 }
8452 else
8453 t = tsubst (t, args, complain, in_decl);
8454
8455 if (invalid_nontype_parm_type_p (t, complain))
8456 return error_mark_node;
8457
8458 if (t != TREE_TYPE (parm))
8459 t = canonicalize_type_argument (t, complain);
8460
8461 if (!type_dependent_expression_p (orig_arg)
8462 && !uses_template_parms (t))
8463 /* We used to call digest_init here. However, digest_init
8464 will report errors, which we don't want when complain
8465 is zero. More importantly, digest_init will try too
8466 hard to convert things: for example, `0' should not be
8467 converted to pointer type at this point according to
8468 the standard. Accepting this is not merely an
8469 extension, since deciding whether or not these
8470 conversions can occur is part of determining which
8471 function template to call, or whether a given explicit
8472 argument specification is valid. */
8473 val = convert_nontype_argument (t, orig_arg, complain);
8474 else
8475 {
8476 val = canonicalize_expr_argument (orig_arg, complain);
8477 val = maybe_convert_nontype_argument (t, val);
8478 }
8479
8480
8481 if (val == NULL_TREE)
8482 val = error_mark_node;
8483 else if (val == error_mark_node && (complain & tf_error))
8484 error_at (cp_expr_loc_or_input_loc (orig_arg),
8485 "could not convert template argument %qE from %qT to %qT",
8486 orig_arg, TREE_TYPE (orig_arg), t);
8487
8488 if (INDIRECT_REF_P (val))
8489 {
8490 /* Reject template arguments that are references to built-in
8491 functions with no library fallbacks. */
8492 const_tree inner = TREE_OPERAND (val, 0);
8493 const_tree innertype = TREE_TYPE (inner);
8494 if (innertype
8495 && TYPE_REF_P (innertype)
8496 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8497 && TREE_OPERAND_LENGTH (inner) > 0
8498 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8499 return error_mark_node;
8500 }
8501
8502 if (TREE_CODE (val) == SCOPE_REF)
8503 {
8504 /* Strip typedefs from the SCOPE_REF. */
8505 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8506 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8507 complain);
8508 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8509 QUALIFIED_NAME_IS_TEMPLATE (val));
8510 }
8511 }
8512
8513 return val;
8514 }
8515
8516 /* Coerces the remaining template arguments in INNER_ARGS (from
8517 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8518 Returns the coerced argument pack. PARM_IDX is the position of this
8519 parameter in the template parameter list. ARGS is the original
8520 template argument list. */
8521 static tree
8522 coerce_template_parameter_pack (tree parms,
8523 int parm_idx,
8524 tree args,
8525 tree inner_args,
8526 int arg_idx,
8527 tree new_args,
8528 int* lost,
8529 tree in_decl,
8530 tsubst_flags_t complain)
8531 {
8532 tree parm = TREE_VEC_ELT (parms, parm_idx);
8533 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8534 tree packed_args;
8535 tree argument_pack;
8536 tree packed_parms = NULL_TREE;
8537
8538 if (arg_idx > nargs)
8539 arg_idx = nargs;
8540
8541 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8542 {
8543 /* When the template parameter is a non-type template parameter pack
8544 or template template parameter pack whose type or template
8545 parameters use parameter packs, we know exactly how many arguments
8546 we are looking for. Build a vector of the instantiated decls for
8547 these template parameters in PACKED_PARMS. */
8548 /* We can't use make_pack_expansion here because it would interpret a
8549 _DECL as a use rather than a declaration. */
8550 tree decl = TREE_VALUE (parm);
8551 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8552 SET_PACK_EXPANSION_PATTERN (exp, decl);
8553 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8554 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8555
8556 TREE_VEC_LENGTH (args)--;
8557 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8558 TREE_VEC_LENGTH (args)++;
8559
8560 if (packed_parms == error_mark_node)
8561 return error_mark_node;
8562
8563 /* If we're doing a partial instantiation of a member template,
8564 verify that all of the types used for the non-type
8565 template parameter pack are, in fact, valid for non-type
8566 template parameters. */
8567 if (arg_idx < nargs
8568 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8569 {
8570 int j, len = TREE_VEC_LENGTH (packed_parms);
8571 for (j = 0; j < len; ++j)
8572 {
8573 tree t = TREE_VEC_ELT (packed_parms, j);
8574 if (TREE_CODE (t) == PARM_DECL
8575 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8576 return error_mark_node;
8577 }
8578 /* We don't know how many args we have yet, just
8579 use the unconverted ones for now. */
8580 return NULL_TREE;
8581 }
8582
8583 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8584 }
8585 /* Check if we have a placeholder pack, which indicates we're
8586 in the context of a introduction list. In that case we want
8587 to match this pack to the single placeholder. */
8588 else if (arg_idx < nargs
8589 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8590 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8591 {
8592 nargs = arg_idx + 1;
8593 packed_args = make_tree_vec (1);
8594 }
8595 else
8596 packed_args = make_tree_vec (nargs - arg_idx);
8597
8598 /* Convert the remaining arguments, which will be a part of the
8599 parameter pack "parm". */
8600 int first_pack_arg = arg_idx;
8601 for (; arg_idx < nargs; ++arg_idx)
8602 {
8603 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8604 tree actual_parm = TREE_VALUE (parm);
8605 int pack_idx = arg_idx - first_pack_arg;
8606
8607 if (packed_parms)
8608 {
8609 /* Once we've packed as many args as we have types, stop. */
8610 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8611 break;
8612 else if (PACK_EXPANSION_P (arg))
8613 /* We don't know how many args we have yet, just
8614 use the unconverted ones for now. */
8615 return NULL_TREE;
8616 else
8617 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8618 }
8619
8620 if (arg == error_mark_node)
8621 {
8622 if (complain & tf_error)
8623 error ("template argument %d is invalid", arg_idx + 1);
8624 }
8625 else
8626 arg = convert_template_argument (actual_parm,
8627 arg, new_args, complain, parm_idx,
8628 in_decl);
8629 if (arg == error_mark_node)
8630 (*lost)++;
8631 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8632 }
8633
8634 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8635 && TREE_VEC_LENGTH (packed_args) > 0)
8636 {
8637 if (complain & tf_error)
8638 error ("wrong number of template arguments (%d, should be %d)",
8639 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8640 return error_mark_node;
8641 }
8642
8643 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8644 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8645 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8646 else
8647 {
8648 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8649 TREE_CONSTANT (argument_pack) = 1;
8650 }
8651
8652 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8653 if (CHECKING_P)
8654 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8655 TREE_VEC_LENGTH (packed_args));
8656 return argument_pack;
8657 }
8658
8659 /* Returns the number of pack expansions in the template argument vector
8660 ARGS. */
8661
8662 static int
8663 pack_expansion_args_count (tree args)
8664 {
8665 int i;
8666 int count = 0;
8667 if (args)
8668 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8669 {
8670 tree elt = TREE_VEC_ELT (args, i);
8671 if (elt && PACK_EXPANSION_P (elt))
8672 ++count;
8673 }
8674 return count;
8675 }
8676
8677 /* Convert all template arguments to their appropriate types, and
8678 return a vector containing the innermost resulting template
8679 arguments. If any error occurs, return error_mark_node. Error and
8680 warning messages are issued under control of COMPLAIN.
8681
8682 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8683 for arguments not specified in ARGS. Otherwise, if
8684 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8685 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8686 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8687 ARGS. */
8688
8689 static tree
8690 coerce_template_parms (tree parms,
8691 tree args,
8692 tree in_decl,
8693 tsubst_flags_t complain,
8694 bool require_all_args,
8695 bool use_default_args)
8696 {
8697 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8698 tree orig_inner_args;
8699 tree inner_args;
8700 tree new_args;
8701 tree new_inner_args;
8702
8703 /* When used as a boolean value, indicates whether this is a
8704 variadic template parameter list. Since it's an int, we can also
8705 subtract it from nparms to get the number of non-variadic
8706 parameters. */
8707 int variadic_p = 0;
8708 int variadic_args_p = 0;
8709 int post_variadic_parms = 0;
8710
8711 /* Adjustment to nparms for fixed parameter packs. */
8712 int fixed_pack_adjust = 0;
8713 int fixed_packs = 0;
8714 int missing = 0;
8715
8716 /* Likewise for parameters with default arguments. */
8717 int default_p = 0;
8718
8719 if (args == error_mark_node)
8720 return error_mark_node;
8721
8722 nparms = TREE_VEC_LENGTH (parms);
8723
8724 /* Determine if there are any parameter packs or default arguments. */
8725 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8726 {
8727 tree parm = TREE_VEC_ELT (parms, parm_idx);
8728 if (variadic_p)
8729 ++post_variadic_parms;
8730 if (template_parameter_pack_p (TREE_VALUE (parm)))
8731 ++variadic_p;
8732 if (TREE_PURPOSE (parm))
8733 ++default_p;
8734 }
8735
8736 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8737 /* If there are no parameters that follow a parameter pack, we need to
8738 expand any argument packs so that we can deduce a parameter pack from
8739 some non-packed args followed by an argument pack, as in variadic85.C.
8740 If there are such parameters, we need to leave argument packs intact
8741 so the arguments are assigned properly. This can happen when dealing
8742 with a nested class inside a partial specialization of a class
8743 template, as in variadic92.C, or when deducing a template parameter pack
8744 from a sub-declarator, as in variadic114.C. */
8745 if (!post_variadic_parms)
8746 inner_args = expand_template_argument_pack (inner_args);
8747
8748 /* Count any pack expansion args. */
8749 variadic_args_p = pack_expansion_args_count (inner_args);
8750
8751 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8752 if ((nargs - variadic_args_p > nparms && !variadic_p)
8753 || (nargs < nparms - variadic_p
8754 && require_all_args
8755 && !variadic_args_p
8756 && (!use_default_args
8757 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8758 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8759 {
8760 bad_nargs:
8761 if (complain & tf_error)
8762 {
8763 if (variadic_p || default_p)
8764 {
8765 nparms -= variadic_p + default_p;
8766 error ("wrong number of template arguments "
8767 "(%d, should be at least %d)", nargs, nparms);
8768 }
8769 else
8770 error ("wrong number of template arguments "
8771 "(%d, should be %d)", nargs, nparms);
8772
8773 if (in_decl)
8774 inform (DECL_SOURCE_LOCATION (in_decl),
8775 "provided for %qD", in_decl);
8776 }
8777
8778 return error_mark_node;
8779 }
8780 /* We can't pass a pack expansion to a non-pack parameter of an alias
8781 template (DR 1430). */
8782 else if (in_decl
8783 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8784 || concept_definition_p (in_decl))
8785 && variadic_args_p
8786 && nargs - variadic_args_p < nparms - variadic_p)
8787 {
8788 if (complain & tf_error)
8789 {
8790 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8791 {
8792 tree arg = TREE_VEC_ELT (inner_args, i);
8793 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8794
8795 if (PACK_EXPANSION_P (arg)
8796 && !template_parameter_pack_p (parm))
8797 {
8798 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8799 error_at (location_of (arg),
8800 "pack expansion argument for non-pack parameter "
8801 "%qD of alias template %qD", parm, in_decl);
8802 else
8803 error_at (location_of (arg),
8804 "pack expansion argument for non-pack parameter "
8805 "%qD of concept %qD", parm, in_decl);
8806 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8807 goto found;
8808 }
8809 }
8810 gcc_unreachable ();
8811 found:;
8812 }
8813 return error_mark_node;
8814 }
8815
8816 /* We need to evaluate the template arguments, even though this
8817 template-id may be nested within a "sizeof". */
8818 cp_evaluated ev;
8819
8820 new_inner_args = make_tree_vec (nparms);
8821 new_args = add_outermost_template_args (args, new_inner_args);
8822 int pack_adjust = 0;
8823 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8824 {
8825 tree arg;
8826 tree parm;
8827
8828 /* Get the Ith template parameter. */
8829 parm = TREE_VEC_ELT (parms, parm_idx);
8830
8831 if (parm == error_mark_node)
8832 {
8833 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8834 continue;
8835 }
8836
8837 /* Calculate the next argument. */
8838 if (arg_idx < nargs)
8839 arg = TREE_VEC_ELT (inner_args, arg_idx);
8840 else
8841 arg = NULL_TREE;
8842
8843 if (template_parameter_pack_p (TREE_VALUE (parm))
8844 && (arg || require_all_args || !(complain & tf_partial))
8845 && !(arg && ARGUMENT_PACK_P (arg)))
8846 {
8847 /* Some arguments will be placed in the
8848 template parameter pack PARM. */
8849 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8850 inner_args, arg_idx,
8851 new_args, &lost,
8852 in_decl, complain);
8853
8854 if (arg == NULL_TREE)
8855 {
8856 /* We don't know how many args we have yet, just use the
8857 unconverted (and still packed) ones for now. */
8858 new_inner_args = orig_inner_args;
8859 arg_idx = nargs;
8860 break;
8861 }
8862
8863 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8864
8865 /* Store this argument. */
8866 if (arg == error_mark_node)
8867 {
8868 lost++;
8869 /* We are done with all of the arguments. */
8870 arg_idx = nargs;
8871 break;
8872 }
8873 else
8874 {
8875 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8876 arg_idx += pack_adjust;
8877 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8878 {
8879 ++fixed_packs;
8880 fixed_pack_adjust += pack_adjust;
8881 }
8882 }
8883
8884 continue;
8885 }
8886 else if (arg)
8887 {
8888 if (PACK_EXPANSION_P (arg))
8889 {
8890 /* "If every valid specialization of a variadic template
8891 requires an empty template parameter pack, the template is
8892 ill-formed, no diagnostic required." So check that the
8893 pattern works with this parameter. */
8894 tree pattern = PACK_EXPANSION_PATTERN (arg);
8895 tree conv = convert_template_argument (TREE_VALUE (parm),
8896 pattern, new_args,
8897 complain, parm_idx,
8898 in_decl);
8899 if (conv == error_mark_node)
8900 {
8901 if (complain & tf_error)
8902 inform (input_location, "so any instantiation with a "
8903 "non-empty parameter pack would be ill-formed");
8904 ++lost;
8905 }
8906 else if (TYPE_P (conv) && !TYPE_P (pattern))
8907 /* Recover from missing typename. */
8908 TREE_VEC_ELT (inner_args, arg_idx)
8909 = make_pack_expansion (conv, complain);
8910
8911 /* We don't know how many args we have yet, just
8912 use the unconverted ones for now. */
8913 new_inner_args = inner_args;
8914 arg_idx = nargs;
8915 break;
8916 }
8917 }
8918 else if (require_all_args)
8919 {
8920 /* There must be a default arg in this case. */
8921 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8922 complain, in_decl);
8923 /* The position of the first default template argument,
8924 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8925 Record that. */
8926 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8927 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8928 arg_idx - pack_adjust);
8929 }
8930 else
8931 break;
8932
8933 if (arg == error_mark_node)
8934 {
8935 if (complain & tf_error)
8936 error ("template argument %d is invalid", arg_idx + 1);
8937 }
8938 else if (!arg)
8939 {
8940 /* This can occur if there was an error in the template
8941 parameter list itself (which we would already have
8942 reported) that we are trying to recover from, e.g., a class
8943 template with a parameter list such as
8944 template<typename..., typename> (cpp0x/variadic150.C). */
8945 ++lost;
8946
8947 /* This can also happen with a fixed parameter pack (71834). */
8948 if (arg_idx >= nargs)
8949 ++missing;
8950 }
8951 else
8952 arg = convert_template_argument (TREE_VALUE (parm),
8953 arg, new_args, complain,
8954 parm_idx, in_decl);
8955
8956 if (arg == error_mark_node)
8957 lost++;
8958
8959 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8960 }
8961
8962 if (missing || arg_idx < nargs - variadic_args_p)
8963 {
8964 /* If we had fixed parameter packs, we didn't know how many arguments we
8965 actually needed earlier; now we do. */
8966 nparms += fixed_pack_adjust;
8967 variadic_p -= fixed_packs;
8968 goto bad_nargs;
8969 }
8970
8971 if (arg_idx < nargs)
8972 {
8973 /* We had some pack expansion arguments that will only work if the packs
8974 are empty, but wait until instantiation time to complain.
8975 See variadic-ttp3.C. */
8976
8977 /* Except that we can't provide empty packs to alias templates or
8978 concepts when there are no corresponding parameters. Basically,
8979 we can get here with this:
8980
8981 template<typename T> concept C = true;
8982
8983 template<typename... Args>
8984 requires C<Args...>
8985 void f();
8986
8987 When parsing C<Args...>, we try to form a concept check of
8988 C<?, Args...>. Without the extra check for substituting an empty
8989 pack past the last parameter, we can accept the check as valid.
8990
8991 FIXME: This may be valid for alias templates (but I doubt it).
8992
8993 FIXME: The error could be better also. */
8994 if (in_decl && concept_definition_p (in_decl))
8995 {
8996 if (complain & tf_error)
8997 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8998 "too many arguments");
8999 return error_mark_node;
9000 }
9001
9002 int len = nparms + (nargs - arg_idx);
9003 tree args = make_tree_vec (len);
9004 int i = 0;
9005 for (; i < nparms; ++i)
9006 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9007 for (; i < len; ++i, ++arg_idx)
9008 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9009 arg_idx - pack_adjust);
9010 new_inner_args = args;
9011 }
9012
9013 if (lost)
9014 {
9015 gcc_assert (!(complain & tf_error) || seen_error ());
9016 return error_mark_node;
9017 }
9018
9019 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9020 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9021 TREE_VEC_LENGTH (new_inner_args));
9022
9023 return new_inner_args;
9024 }
9025
9026 /* Convert all template arguments to their appropriate types, and
9027 return a vector containing the innermost resulting template
9028 arguments. If any error occurs, return error_mark_node. Error and
9029 warning messages are not issued.
9030
9031 Note that no function argument deduction is performed, and default
9032 arguments are used to fill in unspecified arguments. */
9033 tree
9034 coerce_template_parms (tree parms, tree args, tree in_decl)
9035 {
9036 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9037 }
9038
9039 /* Convert all template arguments to their appropriate type, and
9040 instantiate default arguments as needed. This returns a vector
9041 containing the innermost resulting template arguments, or
9042 error_mark_node if unsuccessful. */
9043 tree
9044 coerce_template_parms (tree parms, tree args, tree in_decl,
9045 tsubst_flags_t complain)
9046 {
9047 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9048 }
9049
9050 /* Like coerce_template_parms. If PARMS represents all template
9051 parameters levels, this function returns a vector of vectors
9052 representing all the resulting argument levels. Note that in this
9053 case, only the innermost arguments are coerced because the
9054 outermost ones are supposed to have been coerced already.
9055
9056 Otherwise, if PARMS represents only (the innermost) vector of
9057 parameters, this function returns a vector containing just the
9058 innermost resulting arguments. */
9059
9060 static tree
9061 coerce_innermost_template_parms (tree parms,
9062 tree args,
9063 tree in_decl,
9064 tsubst_flags_t complain,
9065 bool require_all_args,
9066 bool use_default_args)
9067 {
9068 int parms_depth = TMPL_PARMS_DEPTH (parms);
9069 int args_depth = TMPL_ARGS_DEPTH (args);
9070 tree coerced_args;
9071
9072 if (parms_depth > 1)
9073 {
9074 coerced_args = make_tree_vec (parms_depth);
9075 tree level;
9076 int cur_depth;
9077
9078 for (level = parms, cur_depth = parms_depth;
9079 parms_depth > 0 && level != NULL_TREE;
9080 level = TREE_CHAIN (level), --cur_depth)
9081 {
9082 tree l;
9083 if (cur_depth == args_depth)
9084 l = coerce_template_parms (TREE_VALUE (level),
9085 args, in_decl, complain,
9086 require_all_args,
9087 use_default_args);
9088 else
9089 l = TMPL_ARGS_LEVEL (args, cur_depth);
9090
9091 if (l == error_mark_node)
9092 return error_mark_node;
9093
9094 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9095 }
9096 }
9097 else
9098 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9099 args, in_decl, complain,
9100 require_all_args,
9101 use_default_args);
9102 return coerced_args;
9103 }
9104
9105 /* Returns true if T is a wrapper to make a C++20 template parameter
9106 object const. */
9107
9108 static bool
9109 class_nttp_const_wrapper_p (tree t)
9110 {
9111 if (cxx_dialect < cxx20)
9112 return false;
9113 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9114 && CP_TYPE_CONST_P (TREE_TYPE (t))
9115 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9116 }
9117
9118 /* Returns 1 if template args OT and NT are equivalent. */
9119
9120 int
9121 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9122 {
9123 if (nt == ot)
9124 return 1;
9125 if (nt == NULL_TREE || ot == NULL_TREE)
9126 return false;
9127 if (nt == any_targ_node || ot == any_targ_node)
9128 return true;
9129
9130 if (class_nttp_const_wrapper_p (nt))
9131 nt = TREE_OPERAND (nt, 0);
9132 if (class_nttp_const_wrapper_p (ot))
9133 ot = TREE_OPERAND (ot, 0);
9134
9135 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9136 /* For member templates */
9137 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9138 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9139 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9140 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9141 PACK_EXPANSION_PATTERN (nt))
9142 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9143 PACK_EXPANSION_EXTRA_ARGS (nt)));
9144 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9145 return cp_tree_equal (ot, nt);
9146 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9147 gcc_unreachable ();
9148 else if (TYPE_P (nt) || TYPE_P (ot))
9149 {
9150 if (!(TYPE_P (nt) && TYPE_P (ot)))
9151 return false;
9152 /* Don't treat an alias template specialization with dependent
9153 arguments as equivalent to its underlying type when used as a
9154 template argument; we need them to be distinct so that we
9155 substitute into the specialization arguments at instantiation
9156 time. And aliases can't be equivalent without being ==, so
9157 we don't need to look any deeper.
9158
9159 During partial ordering, however, we need to treat them normally so
9160 that we can order uses of the same alias with different
9161 cv-qualification (79960). */
9162 if (!partial_order
9163 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9164 return false;
9165 else
9166 return same_type_p (ot, nt);
9167 }
9168 else
9169 {
9170 /* Try to treat a template non-type argument that has been converted
9171 to the parameter type as equivalent to one that hasn't yet. */
9172 for (enum tree_code code1 = TREE_CODE (ot);
9173 CONVERT_EXPR_CODE_P (code1)
9174 || code1 == NON_LVALUE_EXPR;
9175 code1 = TREE_CODE (ot))
9176 ot = TREE_OPERAND (ot, 0);
9177
9178 for (enum tree_code code2 = TREE_CODE (nt);
9179 CONVERT_EXPR_CODE_P (code2)
9180 || code2 == NON_LVALUE_EXPR;
9181 code2 = TREE_CODE (nt))
9182 nt = TREE_OPERAND (nt, 0);
9183
9184 return cp_tree_equal (ot, nt);
9185 }
9186 }
9187
9188 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9189 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9190 NEWARG_PTR with the offending arguments if they are non-NULL. */
9191
9192 int
9193 comp_template_args (tree oldargs, tree newargs,
9194 tree *oldarg_ptr, tree *newarg_ptr,
9195 bool partial_order)
9196 {
9197 int i;
9198
9199 if (oldargs == newargs)
9200 return 1;
9201
9202 if (!oldargs || !newargs)
9203 return 0;
9204
9205 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9206 return 0;
9207
9208 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9209 {
9210 tree nt = TREE_VEC_ELT (newargs, i);
9211 tree ot = TREE_VEC_ELT (oldargs, i);
9212
9213 if (! template_args_equal (ot, nt, partial_order))
9214 {
9215 if (oldarg_ptr != NULL)
9216 *oldarg_ptr = ot;
9217 if (newarg_ptr != NULL)
9218 *newarg_ptr = nt;
9219 return 0;
9220 }
9221 }
9222 return 1;
9223 }
9224
9225 inline bool
9226 comp_template_args_porder (tree oargs, tree nargs)
9227 {
9228 return comp_template_args (oargs, nargs, NULL, NULL, true);
9229 }
9230
9231 /* Implement a freelist interface for objects of type T.
9232
9233 Head is a separate object, rather than a regular member, so that we
9234 can define it as a GTY deletable pointer, which is highly
9235 desirable. A data member could be declared that way, but then the
9236 containing object would implicitly get GTY((user)), which would
9237 prevent us from instantiating freelists as global objects.
9238 Although this way we can create freelist global objects, they're
9239 such thin wrappers that instantiating temporaries at every use
9240 loses nothing and saves permanent storage for the freelist object.
9241
9242 Member functions next, anew, poison and reinit have default
9243 implementations that work for most of the types we're interested
9244 in, but if they don't work for some type, they should be explicitly
9245 specialized. See the comments before them for requirements, and
9246 the example specializations for the tree_list_freelist. */
9247 template <typename T>
9248 class freelist
9249 {
9250 /* Return the next object in a chain. We could just do type
9251 punning, but if we access the object with its underlying type, we
9252 avoid strict-aliasing trouble. This needs only work between
9253 poison and reinit. */
9254 static T *&next (T *obj) { return obj->next; }
9255
9256 /* Return a newly allocated, uninitialized or minimally-initialized
9257 object of type T. Any initialization performed by anew should
9258 either remain across the life of the object and the execution of
9259 poison, or be redone by reinit. */
9260 static T *anew () { return ggc_alloc<T> (); }
9261
9262 /* Optionally scribble all over the bits holding the object, so that
9263 they become (mostly?) uninitialized memory. This is called while
9264 preparing to make the object part of the free list. */
9265 static void poison (T *obj) {
9266 T *p ATTRIBUTE_UNUSED = obj;
9267 T **q ATTRIBUTE_UNUSED = &next (obj);
9268
9269 #ifdef ENABLE_GC_CHECKING
9270 /* Poison the data, to indicate the data is garbage. */
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9272 memset (p, 0xa5, sizeof (*p));
9273 #endif
9274 /* Let valgrind know the object is free. */
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9276
9277 /* Let valgrind know the next portion of the object is available,
9278 but uninitialized. */
9279 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9280 }
9281
9282 /* Bring an object that underwent at least one lifecycle after anew
9283 and before the most recent free and poison, back to a usable
9284 state, reinitializing whatever is needed for it to be
9285 functionally equivalent to an object just allocated and returned
9286 by anew. This may poison or clear the next field, used by
9287 freelist housekeeping after poison was called. */
9288 static void reinit (T *obj) {
9289 T **q ATTRIBUTE_UNUSED = &next (obj);
9290
9291 #ifdef ENABLE_GC_CHECKING
9292 memset (q, 0xa5, sizeof (*q));
9293 #endif
9294 /* Let valgrind know the entire object is available, but
9295 uninitialized. */
9296 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9297 }
9298
9299 /* Reference a GTY-deletable pointer that points to the first object
9300 in the free list proper. */
9301 T *&head;
9302 public:
9303 /* Construct a freelist object chaining objects off of HEAD. */
9304 freelist (T *&head) : head(head) {}
9305
9306 /* Add OBJ to the free object list. The former head becomes OBJ's
9307 successor. */
9308 void free (T *obj)
9309 {
9310 poison (obj);
9311 next (obj) = head;
9312 head = obj;
9313 }
9314
9315 /* Take an object from the free list, if one is available, or
9316 allocate a new one. Objects taken from the free list should be
9317 regarded as filled with garbage, except for bits that are
9318 configured to be preserved across free and alloc. */
9319 T *alloc ()
9320 {
9321 if (head)
9322 {
9323 T *obj = head;
9324 head = next (head);
9325 reinit (obj);
9326 return obj;
9327 }
9328 else
9329 return anew ();
9330 }
9331 };
9332
9333 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9334 want to allocate a TREE_LIST using the usual interface, and ensure
9335 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9336 build_tree_list logic in reinit, so this could go out of sync. */
9337 template <>
9338 inline tree &
9339 freelist<tree_node>::next (tree obj)
9340 {
9341 return TREE_CHAIN (obj);
9342 }
9343 template <>
9344 inline tree
9345 freelist<tree_node>::anew ()
9346 {
9347 return build_tree_list (NULL, NULL);
9348 }
9349 template <>
9350 inline void
9351 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9352 {
9353 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9354 tree p ATTRIBUTE_UNUSED = obj;
9355 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9356 tree *q ATTRIBUTE_UNUSED = &next (obj);
9357
9358 #ifdef ENABLE_GC_CHECKING
9359 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9360
9361 /* Poison the data, to indicate the data is garbage. */
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9363 memset (p, 0xa5, size);
9364 #endif
9365 /* Let valgrind know the object is free. */
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9367 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9368 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9370
9371 #ifdef ENABLE_GC_CHECKING
9372 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9373 /* Keep TREE_CHAIN functional. */
9374 TREE_SET_CODE (obj, TREE_LIST);
9375 #else
9376 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9377 #endif
9378 }
9379 template <>
9380 inline void
9381 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9382 {
9383 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9384
9385 #ifdef ENABLE_GC_CHECKING
9386 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9388 memset (obj, 0, sizeof (tree_list));
9389 #endif
9390
9391 /* Let valgrind know the entire object is available, but
9392 uninitialized. */
9393 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9394
9395 #ifdef ENABLE_GC_CHECKING
9396 TREE_SET_CODE (obj, TREE_LIST);
9397 #else
9398 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9399 #endif
9400 }
9401
9402 /* Point to the first object in the TREE_LIST freelist. */
9403 static GTY((deletable)) tree tree_list_freelist_head;
9404 /* Return the/an actual TREE_LIST freelist. */
9405 static inline freelist<tree_node>
9406 tree_list_freelist ()
9407 {
9408 return tree_list_freelist_head;
9409 }
9410
9411 /* Point to the first object in the tinst_level freelist. */
9412 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9413 /* Return the/an actual tinst_level freelist. */
9414 static inline freelist<tinst_level>
9415 tinst_level_freelist ()
9416 {
9417 return tinst_level_freelist_head;
9418 }
9419
9420 /* Point to the first object in the pending_template freelist. */
9421 static GTY((deletable)) pending_template *pending_template_freelist_head;
9422 /* Return the/an actual pending_template freelist. */
9423 static inline freelist<pending_template>
9424 pending_template_freelist ()
9425 {
9426 return pending_template_freelist_head;
9427 }
9428
9429 /* Build the TREE_LIST object out of a split list, store it
9430 permanently, and return it. */
9431 tree
9432 tinst_level::to_list ()
9433 {
9434 gcc_assert (split_list_p ());
9435 tree ret = tree_list_freelist ().alloc ();
9436 TREE_PURPOSE (ret) = tldcl;
9437 TREE_VALUE (ret) = targs;
9438 tldcl = ret;
9439 targs = NULL;
9440 gcc_assert (tree_list_p ());
9441 return ret;
9442 }
9443
9444 const unsigned short tinst_level::refcount_infinity;
9445
9446 /* Increment OBJ's refcount unless it is already infinite. */
9447 static tinst_level *
9448 inc_refcount_use (tinst_level *obj)
9449 {
9450 if (obj && obj->refcount != tinst_level::refcount_infinity)
9451 ++obj->refcount;
9452 return obj;
9453 }
9454
9455 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9456 void
9457 tinst_level::free (tinst_level *obj)
9458 {
9459 if (obj->tree_list_p ())
9460 tree_list_freelist ().free (obj->get_node ());
9461 tinst_level_freelist ().free (obj);
9462 }
9463
9464 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9465 OBJ's DECL and OBJ, and start over with the tinst_level object that
9466 used to be referenced by OBJ's NEXT. */
9467 static void
9468 dec_refcount_use (tinst_level *obj)
9469 {
9470 while (obj
9471 && obj->refcount != tinst_level::refcount_infinity
9472 && !--obj->refcount)
9473 {
9474 tinst_level *next = obj->next;
9475 tinst_level::free (obj);
9476 obj = next;
9477 }
9478 }
9479
9480 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9481 and of the former PTR. Omitting the second argument is equivalent
9482 to passing (T*)NULL; this is allowed because passing the
9483 zero-valued integral constant NULL confuses type deduction and/or
9484 overload resolution. */
9485 template <typename T>
9486 static void
9487 set_refcount_ptr (T *& ptr, T *obj = NULL)
9488 {
9489 T *save = ptr;
9490 ptr = inc_refcount_use (obj);
9491 dec_refcount_use (save);
9492 }
9493
9494 static void
9495 add_pending_template (tree d)
9496 {
9497 tree ti = (TYPE_P (d)
9498 ? CLASSTYPE_TEMPLATE_INFO (d)
9499 : DECL_TEMPLATE_INFO (d));
9500 struct pending_template *pt;
9501 int level;
9502
9503 if (TI_PENDING_TEMPLATE_FLAG (ti))
9504 return;
9505
9506 /* We are called both from instantiate_decl, where we've already had a
9507 tinst_level pushed, and instantiate_template, where we haven't.
9508 Compensate. */
9509 gcc_assert (TREE_CODE (d) != TREE_LIST);
9510 level = !current_tinst_level
9511 || current_tinst_level->maybe_get_node () != d;
9512
9513 if (level)
9514 push_tinst_level (d);
9515
9516 pt = pending_template_freelist ().alloc ();
9517 pt->next = NULL;
9518 pt->tinst = NULL;
9519 set_refcount_ptr (pt->tinst, current_tinst_level);
9520 if (last_pending_template)
9521 last_pending_template->next = pt;
9522 else
9523 pending_templates = pt;
9524
9525 last_pending_template = pt;
9526
9527 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9528
9529 if (level)
9530 pop_tinst_level ();
9531 }
9532
9533
9534 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9535 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9536 documentation for TEMPLATE_ID_EXPR. */
9537
9538 tree
9539 lookup_template_function (tree fns, tree arglist)
9540 {
9541 if (fns == error_mark_node || arglist == error_mark_node)
9542 return error_mark_node;
9543
9544 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9545
9546 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9547 {
9548 error ("%q#D is not a function template", fns);
9549 return error_mark_node;
9550 }
9551
9552 if (BASELINK_P (fns))
9553 {
9554 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9555 unknown_type_node,
9556 BASELINK_FUNCTIONS (fns),
9557 arglist);
9558 return fns;
9559 }
9560
9561 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9562 }
9563
9564 /* Within the scope of a template class S<T>, the name S gets bound
9565 (in build_self_reference) to a TYPE_DECL for the class, not a
9566 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9567 or one of its enclosing classes, and that type is a template,
9568 return the associated TEMPLATE_DECL. Otherwise, the original
9569 DECL is returned.
9570
9571 Also handle the case when DECL is a TREE_LIST of ambiguous
9572 injected-class-names from different bases. */
9573
9574 tree
9575 maybe_get_template_decl_from_type_decl (tree decl)
9576 {
9577 if (decl == NULL_TREE)
9578 return decl;
9579
9580 /* DR 176: A lookup that finds an injected-class-name (10.2
9581 [class.member.lookup]) can result in an ambiguity in certain cases
9582 (for example, if it is found in more than one base class). If all of
9583 the injected-class-names that are found refer to specializations of
9584 the same class template, and if the name is followed by a
9585 template-argument-list, the reference refers to the class template
9586 itself and not a specialization thereof, and is not ambiguous. */
9587 if (TREE_CODE (decl) == TREE_LIST)
9588 {
9589 tree t, tmpl = NULL_TREE;
9590 for (t = decl; t; t = TREE_CHAIN (t))
9591 {
9592 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9593 if (!tmpl)
9594 tmpl = elt;
9595 else if (tmpl != elt)
9596 break;
9597 }
9598 if (tmpl && t == NULL_TREE)
9599 return tmpl;
9600 else
9601 return decl;
9602 }
9603
9604 return (decl != NULL_TREE
9605 && DECL_SELF_REFERENCE_P (decl)
9606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9607 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9608 }
9609
9610 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9611 parameters, find the desired type.
9612
9613 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9614
9615 IN_DECL, if non-NULL, is the template declaration we are trying to
9616 instantiate.
9617
9618 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9619 the class we are looking up.
9620
9621 Issue error and warning messages under control of COMPLAIN.
9622
9623 If the template class is really a local class in a template
9624 function, then the FUNCTION_CONTEXT is the function in which it is
9625 being instantiated.
9626
9627 ??? Note that this function is currently called *twice* for each
9628 template-id: the first time from the parser, while creating the
9629 incomplete type (finish_template_type), and the second type during the
9630 real instantiation (instantiate_template_class). This is surely something
9631 that we want to avoid. It also causes some problems with argument
9632 coercion (see convert_nontype_argument for more information on this). */
9633
9634 static tree
9635 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9636 int entering_scope, tsubst_flags_t complain)
9637 {
9638 tree templ = NULL_TREE, parmlist;
9639 tree t;
9640 spec_entry **slot;
9641 spec_entry *entry;
9642 spec_entry elt;
9643 hashval_t hash;
9644
9645 if (identifier_p (d1))
9646 {
9647 tree value = innermost_non_namespace_value (d1);
9648 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9649 templ = value;
9650 else
9651 {
9652 if (context)
9653 push_decl_namespace (context);
9654 templ = lookup_name (d1);
9655 templ = maybe_get_template_decl_from_type_decl (templ);
9656 if (context)
9657 pop_decl_namespace ();
9658 }
9659 if (templ)
9660 context = DECL_CONTEXT (templ);
9661 }
9662 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9663 {
9664 tree type = TREE_TYPE (d1);
9665
9666 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9667 an implicit typename for the second A. Deal with it. */
9668 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9669 type = TREE_TYPE (type);
9670
9671 if (CLASSTYPE_TEMPLATE_INFO (type))
9672 {
9673 templ = CLASSTYPE_TI_TEMPLATE (type);
9674 d1 = DECL_NAME (templ);
9675 }
9676 }
9677 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9678 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9679 {
9680 templ = TYPE_TI_TEMPLATE (d1);
9681 d1 = DECL_NAME (templ);
9682 }
9683 else if (DECL_TYPE_TEMPLATE_P (d1))
9684 {
9685 templ = d1;
9686 d1 = DECL_NAME (templ);
9687 context = DECL_CONTEXT (templ);
9688 }
9689 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9690 {
9691 templ = d1;
9692 d1 = DECL_NAME (templ);
9693 }
9694
9695 /* Issue an error message if we didn't find a template. */
9696 if (! templ)
9697 {
9698 if (complain & tf_error)
9699 error ("%qT is not a template", d1);
9700 return error_mark_node;
9701 }
9702
9703 if (TREE_CODE (templ) != TEMPLATE_DECL
9704 /* Make sure it's a user visible template, if it was named by
9705 the user. */
9706 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9707 && !PRIMARY_TEMPLATE_P (templ)))
9708 {
9709 if (complain & tf_error)
9710 {
9711 error ("non-template type %qT used as a template", d1);
9712 if (in_decl)
9713 error ("for template declaration %q+D", in_decl);
9714 }
9715 return error_mark_node;
9716 }
9717
9718 complain &= ~tf_user;
9719
9720 /* An alias that just changes the name of a template is equivalent to the
9721 other template, so if any of the arguments are pack expansions, strip
9722 the alias to avoid problems with a pack expansion passed to a non-pack
9723 alias template parameter (DR 1430). */
9724 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9725 templ = get_underlying_template (templ);
9726
9727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9728 {
9729 tree parm;
9730 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9731 if (arglist2 == error_mark_node
9732 || (!uses_template_parms (arglist2)
9733 && check_instantiated_args (templ, arglist2, complain)))
9734 return error_mark_node;
9735
9736 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9737 return parm;
9738 }
9739 else
9740 {
9741 tree template_type = TREE_TYPE (templ);
9742 tree gen_tmpl;
9743 tree type_decl;
9744 tree found = NULL_TREE;
9745 int arg_depth;
9746 int parm_depth;
9747 int is_dependent_type;
9748 int use_partial_inst_tmpl = false;
9749
9750 if (template_type == error_mark_node)
9751 /* An error occurred while building the template TEMPL, and a
9752 diagnostic has most certainly been emitted for that
9753 already. Let's propagate that error. */
9754 return error_mark_node;
9755
9756 gen_tmpl = most_general_template (templ);
9757 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9758 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9759 arg_depth = TMPL_ARGS_DEPTH (arglist);
9760
9761 if (arg_depth == 1 && parm_depth > 1)
9762 {
9763 /* We've been given an incomplete set of template arguments.
9764 For example, given:
9765
9766 template <class T> struct S1 {
9767 template <class U> struct S2 {};
9768 template <class U> struct S2<U*> {};
9769 };
9770
9771 we will be called with an ARGLIST of `U*', but the
9772 TEMPLATE will be `template <class T> template
9773 <class U> struct S1<T>::S2'. We must fill in the missing
9774 arguments. */
9775 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9776 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9777 arg_depth = TMPL_ARGS_DEPTH (arglist);
9778 }
9779
9780 /* Now we should have enough arguments. */
9781 gcc_assert (parm_depth == arg_depth);
9782
9783 /* From here on, we're only interested in the most general
9784 template. */
9785
9786 /* Calculate the BOUND_ARGS. These will be the args that are
9787 actually tsubst'd into the definition to create the
9788 instantiation. */
9789 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9790 complain,
9791 /*require_all_args=*/true,
9792 /*use_default_args=*/true);
9793
9794 if (arglist == error_mark_node)
9795 /* We were unable to bind the arguments. */
9796 return error_mark_node;
9797
9798 /* In the scope of a template class, explicit references to the
9799 template class refer to the type of the template, not any
9800 instantiation of it. For example, in:
9801
9802 template <class T> class C { void f(C<T>); }
9803
9804 the `C<T>' is just the same as `C'. Outside of the
9805 class, however, such a reference is an instantiation. */
9806 if (entering_scope
9807 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9808 || currently_open_class (template_type))
9809 {
9810 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9811
9812 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9813 return template_type;
9814 }
9815
9816 /* If we already have this specialization, return it. */
9817 elt.tmpl = gen_tmpl;
9818 elt.args = arglist;
9819 elt.spec = NULL_TREE;
9820 hash = spec_hasher::hash (&elt);
9821 entry = type_specializations->find_with_hash (&elt, hash);
9822
9823 if (entry)
9824 return entry->spec;
9825
9826 /* If the template's constraints are not satisfied,
9827 then we cannot form a valid type.
9828
9829 Note that the check is deferred until after the hash
9830 lookup. This prevents redundant checks on previously
9831 instantiated specializations. */
9832 if (flag_concepts
9833 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9834 && !constraints_satisfied_p (gen_tmpl, arglist))
9835 {
9836 if (complain & tf_error)
9837 {
9838 auto_diagnostic_group d;
9839 error ("template constraint failure for %qD", gen_tmpl);
9840 diagnose_constraints (input_location, gen_tmpl, arglist);
9841 }
9842 return error_mark_node;
9843 }
9844
9845 is_dependent_type = uses_template_parms (arglist);
9846
9847 /* If the deduced arguments are invalid, then the binding
9848 failed. */
9849 if (!is_dependent_type
9850 && check_instantiated_args (gen_tmpl,
9851 INNERMOST_TEMPLATE_ARGS (arglist),
9852 complain))
9853 return error_mark_node;
9854
9855 if (!is_dependent_type
9856 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9857 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9858 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9859 {
9860 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9861 DECL_NAME (gen_tmpl),
9862 /*tag_scope=*/ts_global);
9863 return found;
9864 }
9865
9866 context = DECL_CONTEXT (gen_tmpl);
9867 if (context && TYPE_P (context))
9868 {
9869 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9870 context = complete_type (context);
9871 }
9872 else
9873 context = tsubst (context, arglist, complain, in_decl);
9874
9875 if (context == error_mark_node)
9876 return error_mark_node;
9877
9878 if (!context)
9879 context = global_namespace;
9880
9881 /* Create the type. */
9882 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9883 {
9884 /* The user referred to a specialization of an alias
9885 template represented by GEN_TMPL.
9886
9887 [temp.alias]/2 says:
9888
9889 When a template-id refers to the specialization of an
9890 alias template, it is equivalent to the associated
9891 type obtained by substitution of its
9892 template-arguments for the template-parameters in the
9893 type-id of the alias template. */
9894
9895 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9896 /* Note that the call above (by indirectly calling
9897 register_specialization in tsubst_decl) registers the
9898 TYPE_DECL representing the specialization of the alias
9899 template. So next time someone substitutes ARGLIST for
9900 the template parms into the alias template (GEN_TMPL),
9901 she'll get that TYPE_DECL back. */
9902
9903 if (t == error_mark_node)
9904 return t;
9905 }
9906 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9907 {
9908 if (!is_dependent_type)
9909 {
9910 set_current_access_from_decl (TYPE_NAME (template_type));
9911 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9912 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9913 arglist, complain, in_decl),
9914 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9915 arglist, complain, in_decl),
9916 SCOPED_ENUM_P (template_type), NULL);
9917
9918 if (t == error_mark_node)
9919 return t;
9920 }
9921 else
9922 {
9923 /* We don't want to call start_enum for this type, since
9924 the values for the enumeration constants may involve
9925 template parameters. And, no one should be interested
9926 in the enumeration constants for such a type. */
9927 t = cxx_make_type (ENUMERAL_TYPE);
9928 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9929 }
9930 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9931 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9932 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9933 }
9934 else if (CLASS_TYPE_P (template_type))
9935 {
9936 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9937 instantiated here. */
9938 gcc_assert (!LAMBDA_TYPE_P (template_type));
9939
9940 t = make_class_type (TREE_CODE (template_type));
9941 CLASSTYPE_DECLARED_CLASS (t)
9942 = CLASSTYPE_DECLARED_CLASS (template_type);
9943 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9944
9945 /* A local class. Make sure the decl gets registered properly. */
9946 if (context == current_function_decl)
9947 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9948 == error_mark_node)
9949 return error_mark_node;
9950
9951 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9952 /* This instantiation is another name for the primary
9953 template type. Set the TYPE_CANONICAL field
9954 appropriately. */
9955 TYPE_CANONICAL (t) = template_type;
9956 else if (any_template_arguments_need_structural_equality_p (arglist))
9957 /* Some of the template arguments require structural
9958 equality testing, so this template class requires
9959 structural equality testing. */
9960 SET_TYPE_STRUCTURAL_EQUALITY (t);
9961 }
9962 else
9963 gcc_unreachable ();
9964
9965 /* If we called start_enum or pushtag above, this information
9966 will already be set up. */
9967 type_decl = TYPE_NAME (t);
9968 if (!type_decl)
9969 {
9970 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9971
9972 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9973 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9974 DECL_SOURCE_LOCATION (type_decl)
9975 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9976 }
9977
9978 if (CLASS_TYPE_P (template_type))
9979 {
9980 TREE_PRIVATE (type_decl)
9981 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9982 TREE_PROTECTED (type_decl)
9983 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9984 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9985 {
9986 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9987 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9988 }
9989 }
9990
9991 if (OVERLOAD_TYPE_P (t)
9992 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9993 {
9994 static const char *tags[] = {"abi_tag", "may_alias"};
9995
9996 for (unsigned ix = 0; ix != 2; ix++)
9997 {
9998 tree attributes
9999 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10000
10001 if (attributes)
10002 TYPE_ATTRIBUTES (t)
10003 = tree_cons (TREE_PURPOSE (attributes),
10004 TREE_VALUE (attributes),
10005 TYPE_ATTRIBUTES (t));
10006 }
10007 }
10008
10009 /* Let's consider the explicit specialization of a member
10010 of a class template specialization that is implicitly instantiated,
10011 e.g.:
10012 template<class T>
10013 struct S
10014 {
10015 template<class U> struct M {}; //#0
10016 };
10017
10018 template<>
10019 template<>
10020 struct S<int>::M<char> //#1
10021 {
10022 int i;
10023 };
10024 [temp.expl.spec]/4 says this is valid.
10025
10026 In this case, when we write:
10027 S<int>::M<char> m;
10028
10029 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10030 the one of #0.
10031
10032 When we encounter #1, we want to store the partial instantiation
10033 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10034
10035 For all cases other than this "explicit specialization of member of a
10036 class template", we just want to store the most general template into
10037 the CLASSTYPE_TI_TEMPLATE of M.
10038
10039 This case of "explicit specialization of member of a class template"
10040 only happens when:
10041 1/ the enclosing class is an instantiation of, and therefore not
10042 the same as, the context of the most general template, and
10043 2/ we aren't looking at the partial instantiation itself, i.e.
10044 the innermost arguments are not the same as the innermost parms of
10045 the most general template.
10046
10047 So it's only when 1/ and 2/ happens that we want to use the partial
10048 instantiation of the member template in lieu of its most general
10049 template. */
10050
10051 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10052 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10053 /* the enclosing class must be an instantiation... */
10054 && CLASS_TYPE_P (context)
10055 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10056 {
10057 TREE_VEC_LENGTH (arglist)--;
10058 ++processing_template_decl;
10059 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10060 tree partial_inst_args =
10061 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10062 arglist, complain, NULL_TREE);
10063 --processing_template_decl;
10064 TREE_VEC_LENGTH (arglist)++;
10065 if (partial_inst_args == error_mark_node)
10066 return error_mark_node;
10067 use_partial_inst_tmpl =
10068 /*...and we must not be looking at the partial instantiation
10069 itself. */
10070 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10071 partial_inst_args);
10072 }
10073
10074 if (!use_partial_inst_tmpl)
10075 /* This case is easy; there are no member templates involved. */
10076 found = gen_tmpl;
10077 else
10078 {
10079 /* This is a full instantiation of a member template. Find
10080 the partial instantiation of which this is an instance. */
10081
10082 /* Temporarily reduce by one the number of levels in the ARGLIST
10083 so as to avoid comparing the last set of arguments. */
10084 TREE_VEC_LENGTH (arglist)--;
10085 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10086 TREE_VEC_LENGTH (arglist)++;
10087 /* FOUND is either a proper class type, or an alias
10088 template specialization. In the later case, it's a
10089 TYPE_DECL, resulting from the substituting of arguments
10090 for parameters in the TYPE_DECL of the alias template
10091 done earlier. So be careful while getting the template
10092 of FOUND. */
10093 found = (TREE_CODE (found) == TEMPLATE_DECL
10094 ? found
10095 : (TREE_CODE (found) == TYPE_DECL
10096 ? DECL_TI_TEMPLATE (found)
10097 : CLASSTYPE_TI_TEMPLATE (found)));
10098
10099 if (DECL_CLASS_TEMPLATE_P (found)
10100 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10101 {
10102 /* If this partial instantiation is specialized, we want to
10103 use it for hash table lookup. */
10104 elt.tmpl = found;
10105 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10106 hash = spec_hasher::hash (&elt);
10107 }
10108 }
10109
10110 /* Build template info for the new specialization. */
10111 if (TYPE_ALIAS_P (t))
10112 {
10113 /* This is constructed during instantiation of the alias
10114 decl. But for member templates of template classes, that
10115 is not correct as we need to refer to the partially
10116 instantiated template, not the most general template.
10117 The incorrect knowledge will not have escaped this
10118 instantiation process, so we're good just updating the
10119 template_info we made then. */
10120 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10121 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10122 if (TI_TEMPLATE (ti) != found)
10123 {
10124 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10125 TI_TEMPLATE (ti) = found;
10126 }
10127 }
10128 else
10129 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10130
10131 elt.spec = t;
10132 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10133 gcc_checking_assert (*slot == NULL);
10134 entry = ggc_alloc<spec_entry> ();
10135 *entry = elt;
10136 *slot = entry;
10137
10138 /* Note this use of the partial instantiation so we can check it
10139 later in maybe_process_partial_specialization. */
10140 DECL_TEMPLATE_INSTANTIATIONS (found)
10141 = tree_cons (arglist, t,
10142 DECL_TEMPLATE_INSTANTIATIONS (found));
10143
10144 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10145 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10146 /* Now that the type has been registered on the instantiations
10147 list, we set up the enumerators. Because the enumeration
10148 constants may involve the enumeration type itself, we make
10149 sure to register the type first, and then create the
10150 constants. That way, doing tsubst_expr for the enumeration
10151 constants won't result in recursive calls here; we'll find
10152 the instantiation and exit above. */
10153 tsubst_enum (template_type, t, arglist);
10154
10155 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10156 /* If the type makes use of template parameters, the
10157 code that generates debugging information will crash. */
10158 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10159
10160 /* Possibly limit visibility based on template args. */
10161 TREE_PUBLIC (type_decl) = 1;
10162 determine_visibility (type_decl);
10163
10164 inherit_targ_abi_tags (t);
10165
10166 return t;
10167 }
10168 }
10169
10170 /* Wrapper for lookup_template_class_1. */
10171
10172 tree
10173 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10174 int entering_scope, tsubst_flags_t complain)
10175 {
10176 tree ret;
10177 timevar_push (TV_TEMPLATE_INST);
10178 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10179 entering_scope, complain);
10180 timevar_pop (TV_TEMPLATE_INST);
10181 return ret;
10182 }
10183
10184 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10185
10186 tree
10187 lookup_template_variable (tree templ, tree arglist)
10188 {
10189 if (flag_concepts && variable_concept_p (templ))
10190 return build_concept_check (templ, arglist, tf_none);
10191
10192 /* The type of the expression is NULL_TREE since the template-id could refer
10193 to an explicit or partial specialization. */
10194 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10195 }
10196
10197 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10198
10199 tree
10200 finish_template_variable (tree var, tsubst_flags_t complain)
10201 {
10202 tree templ = TREE_OPERAND (var, 0);
10203 tree arglist = TREE_OPERAND (var, 1);
10204
10205 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10206 arglist = add_outermost_template_args (tmpl_args, arglist);
10207
10208 templ = most_general_template (templ);
10209 tree parms = DECL_TEMPLATE_PARMS (templ);
10210 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10211 /*req_all*/true,
10212 /*use_default*/true);
10213 if (arglist == error_mark_node)
10214 return error_mark_node;
10215
10216 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10217 {
10218 if (complain & tf_error)
10219 {
10220 auto_diagnostic_group d;
10221 error ("use of invalid variable template %qE", var);
10222 diagnose_constraints (location_of (var), templ, arglist);
10223 }
10224 return error_mark_node;
10225 }
10226
10227 return instantiate_template (templ, arglist, complain);
10228 }
10229
10230 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10231 TARGS template args, and instantiate it if it's not dependent. */
10232
10233 tree
10234 lookup_and_finish_template_variable (tree templ, tree targs,
10235 tsubst_flags_t complain)
10236 {
10237 templ = lookup_template_variable (templ, targs);
10238 if (!any_dependent_template_arguments_p (targs))
10239 {
10240 templ = finish_template_variable (templ, complain);
10241 mark_used (templ);
10242 }
10243
10244 return convert_from_reference (templ);
10245 }
10246
10247 \f
10248 struct pair_fn_data
10249 {
10250 tree_fn_t fn;
10251 tree_fn_t any_fn;
10252 void *data;
10253 /* True when we should also visit template parameters that occur in
10254 non-deduced contexts. */
10255 bool include_nondeduced_p;
10256 hash_set<tree> *visited;
10257 };
10258
10259 /* Called from for_each_template_parm via walk_tree. */
10260
10261 static tree
10262 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10263 {
10264 tree t = *tp;
10265 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10266 tree_fn_t fn = pfd->fn;
10267 void *data = pfd->data;
10268 tree result = NULL_TREE;
10269
10270 #define WALK_SUBTREE(NODE) \
10271 do \
10272 { \
10273 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10274 pfd->include_nondeduced_p, \
10275 pfd->any_fn); \
10276 if (result) goto out; \
10277 } \
10278 while (0)
10279
10280 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10281 return t;
10282
10283 if (TYPE_P (t)
10284 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10285 WALK_SUBTREE (TYPE_CONTEXT (t));
10286
10287 switch (TREE_CODE (t))
10288 {
10289 case RECORD_TYPE:
10290 if (TYPE_PTRMEMFUNC_P (t))
10291 break;
10292 /* Fall through. */
10293
10294 case UNION_TYPE:
10295 case ENUMERAL_TYPE:
10296 if (!TYPE_TEMPLATE_INFO (t))
10297 *walk_subtrees = 0;
10298 else
10299 WALK_SUBTREE (TYPE_TI_ARGS (t));
10300 break;
10301
10302 case INTEGER_TYPE:
10303 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10304 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10305 break;
10306
10307 case METHOD_TYPE:
10308 /* Since we're not going to walk subtrees, we have to do this
10309 explicitly here. */
10310 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10311 /* Fall through. */
10312
10313 case FUNCTION_TYPE:
10314 /* Check the return type. */
10315 WALK_SUBTREE (TREE_TYPE (t));
10316
10317 /* Check the parameter types. Since default arguments are not
10318 instantiated until they are needed, the TYPE_ARG_TYPES may
10319 contain expressions that involve template parameters. But,
10320 no-one should be looking at them yet. And, once they're
10321 instantiated, they don't contain template parameters, so
10322 there's no point in looking at them then, either. */
10323 {
10324 tree parm;
10325
10326 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10327 WALK_SUBTREE (TREE_VALUE (parm));
10328
10329 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10330 want walk_tree walking into them itself. */
10331 *walk_subtrees = 0;
10332 }
10333
10334 if (flag_noexcept_type)
10335 {
10336 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10337 if (spec)
10338 WALK_SUBTREE (TREE_PURPOSE (spec));
10339 }
10340 break;
10341
10342 case TYPEOF_TYPE:
10343 case DECLTYPE_TYPE:
10344 case UNDERLYING_TYPE:
10345 if (pfd->include_nondeduced_p
10346 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10347 pfd->visited,
10348 pfd->include_nondeduced_p,
10349 pfd->any_fn))
10350 return error_mark_node;
10351 *walk_subtrees = false;
10352 break;
10353
10354 case FUNCTION_DECL:
10355 case VAR_DECL:
10356 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10357 WALK_SUBTREE (DECL_TI_ARGS (t));
10358 /* Fall through. */
10359
10360 case PARM_DECL:
10361 case CONST_DECL:
10362 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10363 WALK_SUBTREE (DECL_INITIAL (t));
10364 if (DECL_CONTEXT (t)
10365 && pfd->include_nondeduced_p)
10366 WALK_SUBTREE (DECL_CONTEXT (t));
10367 break;
10368
10369 case BOUND_TEMPLATE_TEMPLATE_PARM:
10370 /* Record template parameters such as `T' inside `TT<T>'. */
10371 WALK_SUBTREE (TYPE_TI_ARGS (t));
10372 /* Fall through. */
10373
10374 case TEMPLATE_TEMPLATE_PARM:
10375 case TEMPLATE_TYPE_PARM:
10376 case TEMPLATE_PARM_INDEX:
10377 if (fn && (*fn)(t, data))
10378 return t;
10379 else if (!fn)
10380 return t;
10381 break;
10382
10383 case TEMPLATE_DECL:
10384 /* A template template parameter is encountered. */
10385 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10386 WALK_SUBTREE (TREE_TYPE (t));
10387
10388 /* Already substituted template template parameter */
10389 *walk_subtrees = 0;
10390 break;
10391
10392 case TYPENAME_TYPE:
10393 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10394 partial instantiation. */
10395 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10396 *walk_subtrees = 0;
10397 break;
10398
10399 case CONSTRUCTOR:
10400 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10401 && pfd->include_nondeduced_p)
10402 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10403 break;
10404
10405 case INDIRECT_REF:
10406 case COMPONENT_REF:
10407 /* If there's no type, then this thing must be some expression
10408 involving template parameters. */
10409 if (!fn && !TREE_TYPE (t))
10410 return error_mark_node;
10411 break;
10412
10413 case MODOP_EXPR:
10414 case CAST_EXPR:
10415 case IMPLICIT_CONV_EXPR:
10416 case REINTERPRET_CAST_EXPR:
10417 case CONST_CAST_EXPR:
10418 case STATIC_CAST_EXPR:
10419 case DYNAMIC_CAST_EXPR:
10420 case ARROW_EXPR:
10421 case DOTSTAR_EXPR:
10422 case TYPEID_EXPR:
10423 case PSEUDO_DTOR_EXPR:
10424 if (!fn)
10425 return error_mark_node;
10426 break;
10427
10428 case SCOPE_REF:
10429 if (pfd->include_nondeduced_p)
10430 WALK_SUBTREE (TREE_OPERAND (t, 0));
10431 break;
10432
10433 case REQUIRES_EXPR:
10434 {
10435 if (!fn)
10436 return error_mark_node;
10437
10438 /* Recursively walk the type of each constraint variable. */
10439 tree p = TREE_OPERAND (t, 0);
10440 while (p)
10441 {
10442 WALK_SUBTREE (TREE_TYPE (p));
10443 p = TREE_CHAIN (p);
10444 }
10445 }
10446 break;
10447
10448 default:
10449 break;
10450 }
10451
10452 #undef WALK_SUBTREE
10453
10454 /* We didn't find any template parameters we liked. */
10455 out:
10456 return result;
10457 }
10458
10459 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10460 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10461 call FN with the parameter and the DATA.
10462 If FN returns nonzero, the iteration is terminated, and
10463 for_each_template_parm returns 1. Otherwise, the iteration
10464 continues. If FN never returns a nonzero value, the value
10465 returned by for_each_template_parm is 0. If FN is NULL, it is
10466 considered to be the function which always returns 1.
10467
10468 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10469 parameters that occur in non-deduced contexts. When false, only
10470 visits those template parameters that can be deduced. */
10471
10472 static tree
10473 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10474 hash_set<tree> *visited,
10475 bool include_nondeduced_p,
10476 tree_fn_t any_fn)
10477 {
10478 struct pair_fn_data pfd;
10479 tree result;
10480
10481 /* Set up. */
10482 pfd.fn = fn;
10483 pfd.any_fn = any_fn;
10484 pfd.data = data;
10485 pfd.include_nondeduced_p = include_nondeduced_p;
10486
10487 /* Walk the tree. (Conceptually, we would like to walk without
10488 duplicates, but for_each_template_parm_r recursively calls
10489 for_each_template_parm, so we would need to reorganize a fair
10490 bit to use walk_tree_without_duplicates, so we keep our own
10491 visited list.) */
10492 if (visited)
10493 pfd.visited = visited;
10494 else
10495 pfd.visited = new hash_set<tree>;
10496 result = cp_walk_tree (&t,
10497 for_each_template_parm_r,
10498 &pfd,
10499 pfd.visited);
10500
10501 /* Clean up. */
10502 if (!visited)
10503 {
10504 delete pfd.visited;
10505 pfd.visited = 0;
10506 }
10507
10508 return result;
10509 }
10510
10511 struct find_template_parameter_info
10512 {
10513 explicit find_template_parameter_info (tree ctx_parms)
10514 : parm_list (NULL_TREE),
10515 ctx_parms (ctx_parms),
10516 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10517 {}
10518
10519 hash_set<tree> visited;
10520 hash_set<tree> parms;
10521 tree parm_list;
10522 tree ctx_parms;
10523 int max_depth;
10524 };
10525
10526 /* Appends the declaration of T to the list in DATA. */
10527
10528 static int
10529 keep_template_parm (tree t, void* data)
10530 {
10531 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10532
10533 /* Template parameters declared within the expression are not part of
10534 the parameter mapping. For example, in this concept:
10535
10536 template<typename T>
10537 concept C = requires { <expr> } -> same_as<int>;
10538
10539 the return specifier same_as<int> declares a new decltype parameter
10540 that must not be part of the parameter mapping. The same is true
10541 for generic lambda parameters, lambda template parameters, etc. */
10542 int level;
10543 int index;
10544 template_parm_level_and_index (t, &level, &index);
10545 if (level > ftpi->max_depth)
10546 return 0;
10547
10548 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10549 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10550 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10551 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10552
10553 /* Arguments like const T yield parameters like const T. This means that
10554 a template-id like X<T, const T> would yield two distinct parameters:
10555 T and const T. Adjust types to their unqualified versions. */
10556 if (TYPE_P (t))
10557 t = TYPE_MAIN_VARIANT (t);
10558 if (!ftpi->parms.add (t))
10559 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10560
10561 return 0;
10562 }
10563
10564 /* Ensure that we recursively examine certain terms that are not normally
10565 visited in for_each_template_parm_r. */
10566
10567 static int
10568 any_template_parm_r (tree t, void *data)
10569 {
10570 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10571
10572 #define WALK_SUBTREE(NODE) \
10573 do \
10574 { \
10575 for_each_template_parm (NODE, keep_template_parm, data, \
10576 &ftpi->visited, true, \
10577 any_template_parm_r); \
10578 } \
10579 while (0)
10580
10581 /* A mention of a member alias/typedef is a use of all of its template
10582 arguments, including those from the enclosing class, so we don't use
10583 alias_template_specialization_p here. */
10584 if (TYPE_P (t) && typedef_variant_p (t))
10585 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10586 WALK_SUBTREE (TI_ARGS (tinfo));
10587
10588 switch (TREE_CODE (t))
10589 {
10590 case TEMPLATE_TYPE_PARM:
10591 /* Type constraints of a placeholder type may contain parameters. */
10592 if (is_auto (t))
10593 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10594 WALK_SUBTREE (constr);
10595 break;
10596
10597 case TEMPLATE_ID_EXPR:
10598 /* Search through references to variable templates. */
10599 WALK_SUBTREE (TREE_OPERAND (t, 0));
10600 WALK_SUBTREE (TREE_OPERAND (t, 1));
10601 break;
10602
10603 case TEMPLATE_PARM_INDEX:
10604 case PARM_DECL:
10605 /* A parameter or constraint variable may also depend on a template
10606 parameter without explicitly naming it. */
10607 WALK_SUBTREE (TREE_TYPE (t));
10608 break;
10609
10610 case TEMPLATE_DECL:
10611 {
10612 /* If T is a member template that shares template parameters with
10613 ctx_parms, we need to mark all those parameters for mapping. */
10614 tree dparms = DECL_TEMPLATE_PARMS (t);
10615 tree cparms = ftpi->ctx_parms;
10616 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10617 dparms = TREE_CHAIN (dparms);
10618 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10619 cparms = TREE_CHAIN (cparms);
10620 while (dparms
10621 && (TREE_TYPE (TREE_VALUE (dparms))
10622 != TREE_TYPE (TREE_VALUE (cparms))))
10623 dparms = TREE_CHAIN (dparms),
10624 cparms = TREE_CHAIN (cparms);
10625 if (dparms)
10626 {
10627 int ddepth = TMPL_PARMS_DEPTH (dparms);
10628 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10629 for (int i = 0; i < ddepth; ++i)
10630 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10631 }
10632 }
10633 break;
10634
10635 case LAMBDA_EXPR:
10636 {
10637 /* Look in the parms and body. */
10638 tree fn = lambda_function (t);
10639 WALK_SUBTREE (TREE_TYPE (fn));
10640 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10641 }
10642 break;
10643
10644 case IDENTIFIER_NODE:
10645 if (IDENTIFIER_CONV_OP_P (t))
10646 /* The conversion-type-id of a conversion operator may be dependent. */
10647 WALK_SUBTREE (TREE_TYPE (t));
10648 break;
10649
10650 default:
10651 break;
10652 }
10653
10654 /* Keep walking. */
10655 return 0;
10656 }
10657
10658 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10659 are the template parameters in scope. */
10660
10661 tree
10662 find_template_parameters (tree t, tree ctx_parms)
10663 {
10664 if (!ctx_parms)
10665 return NULL_TREE;
10666
10667 find_template_parameter_info ftpi (ctx_parms);
10668 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10669 /*include_nondeduced*/true, any_template_parm_r);
10670 return ftpi.parm_list;
10671 }
10672
10673 /* Returns true if T depends on any template parameter. */
10674
10675 int
10676 uses_template_parms (tree t)
10677 {
10678 if (t == NULL_TREE)
10679 return false;
10680
10681 bool dependent_p;
10682 int saved_processing_template_decl;
10683
10684 saved_processing_template_decl = processing_template_decl;
10685 if (!saved_processing_template_decl)
10686 processing_template_decl = 1;
10687 if (TYPE_P (t))
10688 dependent_p = dependent_type_p (t);
10689 else if (TREE_CODE (t) == TREE_VEC)
10690 dependent_p = any_dependent_template_arguments_p (t);
10691 else if (TREE_CODE (t) == TREE_LIST)
10692 dependent_p = (uses_template_parms (TREE_VALUE (t))
10693 || uses_template_parms (TREE_CHAIN (t)));
10694 else if (TREE_CODE (t) == TYPE_DECL)
10695 dependent_p = dependent_type_p (TREE_TYPE (t));
10696 else if (t == error_mark_node)
10697 dependent_p = false;
10698 else
10699 dependent_p = value_dependent_expression_p (t);
10700
10701 processing_template_decl = saved_processing_template_decl;
10702
10703 return dependent_p;
10704 }
10705
10706 /* Returns true iff current_function_decl is an incompletely instantiated
10707 template. Useful instead of processing_template_decl because the latter
10708 is set to 0 during instantiate_non_dependent_expr. */
10709
10710 bool
10711 in_template_function (void)
10712 {
10713 tree fn = current_function_decl;
10714 bool ret;
10715 ++processing_template_decl;
10716 ret = (fn && DECL_LANG_SPECIFIC (fn)
10717 && DECL_TEMPLATE_INFO (fn)
10718 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10719 --processing_template_decl;
10720 return ret;
10721 }
10722
10723 /* Returns true if T depends on any template parameter with level LEVEL. */
10724
10725 bool
10726 uses_template_parms_level (tree t, int level)
10727 {
10728 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10729 /*include_nondeduced_p=*/true);
10730 }
10731
10732 /* Returns true if the signature of DECL depends on any template parameter from
10733 its enclosing class. */
10734
10735 bool
10736 uses_outer_template_parms (tree decl)
10737 {
10738 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10739 if (depth == 0)
10740 return false;
10741 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10742 &depth, NULL, /*include_nondeduced_p=*/true))
10743 return true;
10744 if (PRIMARY_TEMPLATE_P (decl)
10745 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10746 (DECL_TEMPLATE_PARMS (decl)),
10747 template_parm_outer_level,
10748 &depth, NULL, /*include_nondeduced_p=*/true))
10749 return true;
10750 tree ci = get_constraints (decl);
10751 if (ci)
10752 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10753 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10754 &depth, NULL, /*nondeduced*/true))
10755 return true;
10756 return false;
10757 }
10758
10759 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10760 ill-formed translation unit, i.e. a variable or function that isn't
10761 usable in a constant expression. */
10762
10763 static inline bool
10764 neglectable_inst_p (tree d)
10765 {
10766 return (d && DECL_P (d)
10767 && !undeduced_auto_decl (d)
10768 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10769 : decl_maybe_constant_var_p (d)));
10770 }
10771
10772 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10773 neglectable and instantiated from within an erroneous instantiation. */
10774
10775 static bool
10776 limit_bad_template_recursion (tree decl)
10777 {
10778 struct tinst_level *lev = current_tinst_level;
10779 int errs = errorcount + sorrycount;
10780 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10781 return false;
10782
10783 for (; lev; lev = lev->next)
10784 if (neglectable_inst_p (lev->maybe_get_node ()))
10785 break;
10786
10787 return (lev && errs > lev->errors);
10788 }
10789
10790 static int tinst_depth;
10791 extern int max_tinst_depth;
10792 int depth_reached;
10793
10794 static GTY(()) struct tinst_level *last_error_tinst_level;
10795
10796 /* We're starting to instantiate D; record the template instantiation context
10797 at LOC for diagnostics and to restore it later. */
10798
10799 bool
10800 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10801 {
10802 struct tinst_level *new_level;
10803
10804 if (tinst_depth >= max_tinst_depth)
10805 {
10806 /* Tell error.c not to try to instantiate any templates. */
10807 at_eof = 2;
10808 fatal_error (input_location,
10809 "template instantiation depth exceeds maximum of %d"
10810 " (use %<-ftemplate-depth=%> to increase the maximum)",
10811 max_tinst_depth);
10812 return false;
10813 }
10814
10815 /* If the current instantiation caused problems, don't let it instantiate
10816 anything else. Do allow deduction substitution and decls usable in
10817 constant expressions. */
10818 if (!targs && limit_bad_template_recursion (tldcl))
10819 {
10820 /* Avoid no_linkage_errors and unused function warnings for this
10821 decl. */
10822 TREE_NO_WARNING (tldcl) = 1;
10823 return false;
10824 }
10825
10826 /* When not -quiet, dump template instantiations other than functions, since
10827 announce_function will take care of those. */
10828 if (!quiet_flag && !targs
10829 && TREE_CODE (tldcl) != TREE_LIST
10830 && TREE_CODE (tldcl) != FUNCTION_DECL)
10831 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10832
10833 new_level = tinst_level_freelist ().alloc ();
10834 new_level->tldcl = tldcl;
10835 new_level->targs = targs;
10836 new_level->locus = loc;
10837 new_level->errors = errorcount + sorrycount;
10838 new_level->next = NULL;
10839 new_level->refcount = 0;
10840 set_refcount_ptr (new_level->next, current_tinst_level);
10841 set_refcount_ptr (current_tinst_level, new_level);
10842
10843 ++tinst_depth;
10844 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10845 depth_reached = tinst_depth;
10846
10847 return true;
10848 }
10849
10850 /* We're starting substitution of TMPL<ARGS>; record the template
10851 substitution context for diagnostics and to restore it later. */
10852
10853 bool
10854 push_tinst_level (tree tmpl, tree args)
10855 {
10856 return push_tinst_level_loc (tmpl, args, input_location);
10857 }
10858
10859 /* We're starting to instantiate D; record INPUT_LOCATION and the
10860 template instantiation context for diagnostics and to restore it
10861 later. */
10862
10863 bool
10864 push_tinst_level (tree d)
10865 {
10866 return push_tinst_level_loc (d, input_location);
10867 }
10868
10869 /* Likewise, but record LOC as the program location. */
10870
10871 bool
10872 push_tinst_level_loc (tree d, location_t loc)
10873 {
10874 gcc_assert (TREE_CODE (d) != TREE_LIST);
10875 return push_tinst_level_loc (d, NULL, loc);
10876 }
10877
10878 /* We're done instantiating this template; return to the instantiation
10879 context. */
10880
10881 void
10882 pop_tinst_level (void)
10883 {
10884 /* Restore the filename and line number stashed away when we started
10885 this instantiation. */
10886 input_location = current_tinst_level->locus;
10887 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10888 --tinst_depth;
10889 }
10890
10891 /* We're instantiating a deferred template; restore the template
10892 instantiation context in which the instantiation was requested, which
10893 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10894
10895 static tree
10896 reopen_tinst_level (struct tinst_level *level)
10897 {
10898 struct tinst_level *t;
10899
10900 tinst_depth = 0;
10901 for (t = level; t; t = t->next)
10902 ++tinst_depth;
10903
10904 set_refcount_ptr (current_tinst_level, level);
10905 pop_tinst_level ();
10906 if (current_tinst_level)
10907 current_tinst_level->errors = errorcount+sorrycount;
10908 return level->maybe_get_node ();
10909 }
10910
10911 /* Returns the TINST_LEVEL which gives the original instantiation
10912 context. */
10913
10914 struct tinst_level *
10915 outermost_tinst_level (void)
10916 {
10917 struct tinst_level *level = current_tinst_level;
10918 if (level)
10919 while (level->next)
10920 level = level->next;
10921 return level;
10922 }
10923
10924 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10925 vector of template arguments, as for tsubst.
10926
10927 Returns an appropriate tsubst'd friend declaration. */
10928
10929 static tree
10930 tsubst_friend_function (tree decl, tree args)
10931 {
10932 tree new_friend;
10933
10934 if (TREE_CODE (decl) == FUNCTION_DECL
10935 && DECL_TEMPLATE_INSTANTIATION (decl)
10936 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10937 /* This was a friend declared with an explicit template
10938 argument list, e.g.:
10939
10940 friend void f<>(T);
10941
10942 to indicate that f was a template instantiation, not a new
10943 function declaration. Now, we have to figure out what
10944 instantiation of what template. */
10945 {
10946 tree template_id, arglist, fns;
10947 tree new_args;
10948 tree tmpl;
10949 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10950
10951 /* Friend functions are looked up in the containing namespace scope.
10952 We must enter that scope, to avoid finding member functions of the
10953 current class with same name. */
10954 push_nested_namespace (ns);
10955 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10956 tf_warning_or_error, NULL_TREE,
10957 /*integral_constant_expression_p=*/false);
10958 pop_nested_namespace (ns);
10959 arglist = tsubst (DECL_TI_ARGS (decl), args,
10960 tf_warning_or_error, NULL_TREE);
10961 template_id = lookup_template_function (fns, arglist);
10962
10963 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10964 tmpl = determine_specialization (template_id, new_friend,
10965 &new_args,
10966 /*need_member_template=*/0,
10967 TREE_VEC_LENGTH (args),
10968 tsk_none);
10969 return instantiate_template (tmpl, new_args, tf_error);
10970 }
10971
10972 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10973 if (new_friend == error_mark_node)
10974 return error_mark_node;
10975
10976 /* The NEW_FRIEND will look like an instantiation, to the
10977 compiler, but is not an instantiation from the point of view of
10978 the language. For example, we might have had:
10979
10980 template <class T> struct S {
10981 template <class U> friend void f(T, U);
10982 };
10983
10984 Then, in S<int>, template <class U> void f(int, U) is not an
10985 instantiation of anything. */
10986
10987 DECL_USE_TEMPLATE (new_friend) = 0;
10988 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10989 {
10990 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10991 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10992 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10993
10994 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10995 match in decls_match. */
10996 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10997 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10998 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10999 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11000 }
11001
11002 /* The mangled name for the NEW_FRIEND is incorrect. The function
11003 is not a template instantiation and should not be mangled like
11004 one. Therefore, we forget the mangling here; we'll recompute it
11005 later if we need it. */
11006 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11007 {
11008 SET_DECL_RTL (new_friend, NULL);
11009 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11010 }
11011
11012 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11013 {
11014 tree old_decl;
11015 tree ns;
11016
11017 /* We must save some information from NEW_FRIEND before calling
11018 duplicate decls since that function will free NEW_FRIEND if
11019 possible. */
11020 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11021 tree new_friend_result_template_info = NULL_TREE;
11022 bool new_friend_is_defn =
11023 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11024 (template_for_substitution (new_friend)))
11025 != NULL_TREE);
11026 tree not_tmpl = new_friend;
11027
11028 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11029 {
11030 /* This declaration is a `primary' template. */
11031 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11032
11033 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11034 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11035 }
11036
11037 /* Inside pushdecl_namespace_level, we will push into the
11038 current namespace. However, the friend function should go
11039 into the namespace of the template. */
11040 ns = decl_namespace_context (new_friend);
11041 push_nested_namespace (ns);
11042 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
11043 pop_nested_namespace (ns);
11044
11045 if (old_decl == error_mark_node)
11046 return error_mark_node;
11047
11048 if (old_decl != new_friend)
11049 {
11050 /* This new friend declaration matched an existing
11051 declaration. For example, given:
11052
11053 template <class T> void f(T);
11054 template <class U> class C {
11055 template <class T> friend void f(T) {}
11056 };
11057
11058 the friend declaration actually provides the definition
11059 of `f', once C has been instantiated for some type. So,
11060 old_decl will be the out-of-class template declaration,
11061 while new_friend is the in-class definition.
11062
11063 But, if `f' was called before this point, the
11064 instantiation of `f' will have DECL_TI_ARGS corresponding
11065 to `T' but not to `U', references to which might appear
11066 in the definition of `f'. Previously, the most general
11067 template for an instantiation of `f' was the out-of-class
11068 version; now it is the in-class version. Therefore, we
11069 run through all specialization of `f', adding to their
11070 DECL_TI_ARGS appropriately. In particular, they need a
11071 new set of outer arguments, corresponding to the
11072 arguments for this class instantiation.
11073
11074 The same situation can arise with something like this:
11075
11076 friend void f(int);
11077 template <class T> class C {
11078 friend void f(T) {}
11079 };
11080
11081 when `C<int>' is instantiated. Now, `f(int)' is defined
11082 in the class. */
11083
11084 if (!new_friend_is_defn)
11085 /* On the other hand, if the in-class declaration does
11086 *not* provide a definition, then we don't want to alter
11087 existing definitions. We can just leave everything
11088 alone. */
11089 ;
11090 else
11091 {
11092 tree new_template = TI_TEMPLATE (new_friend_template_info);
11093 tree new_args = TI_ARGS (new_friend_template_info);
11094
11095 /* Overwrite whatever template info was there before, if
11096 any, with the new template information pertaining to
11097 the declaration. */
11098 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11099
11100 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11101 {
11102 /* We should have called reregister_specialization in
11103 duplicate_decls. */
11104 gcc_assert (retrieve_specialization (new_template,
11105 new_args, 0)
11106 == old_decl);
11107
11108 /* Instantiate it if the global has already been used. */
11109 if (DECL_ODR_USED (old_decl))
11110 instantiate_decl (old_decl, /*defer_ok=*/true,
11111 /*expl_inst_class_mem_p=*/false);
11112 }
11113 else
11114 {
11115 tree t;
11116
11117 /* Indicate that the old function template is a partial
11118 instantiation. */
11119 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11120 = new_friend_result_template_info;
11121
11122 gcc_assert (new_template
11123 == most_general_template (new_template));
11124 gcc_assert (new_template != old_decl);
11125
11126 /* Reassign any specializations already in the hash table
11127 to the new more general template, and add the
11128 additional template args. */
11129 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11130 t != NULL_TREE;
11131 t = TREE_CHAIN (t))
11132 {
11133 tree spec = TREE_VALUE (t);
11134 spec_entry elt;
11135
11136 elt.tmpl = old_decl;
11137 elt.args = DECL_TI_ARGS (spec);
11138 elt.spec = NULL_TREE;
11139
11140 decl_specializations->remove_elt (&elt);
11141
11142 DECL_TI_ARGS (spec)
11143 = add_outermost_template_args (new_args,
11144 DECL_TI_ARGS (spec));
11145
11146 register_specialization
11147 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11148
11149 }
11150 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11151 }
11152 }
11153
11154 /* The information from NEW_FRIEND has been merged into OLD_DECL
11155 by duplicate_decls. */
11156 new_friend = old_decl;
11157 }
11158 }
11159 else
11160 {
11161 tree context = DECL_CONTEXT (new_friend);
11162 bool dependent_p;
11163
11164 /* In the code
11165 template <class T> class C {
11166 template <class U> friend void C1<U>::f (); // case 1
11167 friend void C2<T>::f (); // case 2
11168 };
11169 we only need to make sure CONTEXT is a complete type for
11170 case 2. To distinguish between the two cases, we note that
11171 CONTEXT of case 1 remains dependent type after tsubst while
11172 this isn't true for case 2. */
11173 ++processing_template_decl;
11174 dependent_p = dependent_type_p (context);
11175 --processing_template_decl;
11176
11177 if (!dependent_p
11178 && !complete_type_or_else (context, NULL_TREE))
11179 return error_mark_node;
11180
11181 if (COMPLETE_TYPE_P (context))
11182 {
11183 tree fn = new_friend;
11184 /* do_friend adds the TEMPLATE_DECL for any member friend
11185 template even if it isn't a member template, i.e.
11186 template <class T> friend A<T>::f();
11187 Look through it in that case. */
11188 if (TREE_CODE (fn) == TEMPLATE_DECL
11189 && !PRIMARY_TEMPLATE_P (fn))
11190 fn = DECL_TEMPLATE_RESULT (fn);
11191 /* Check to see that the declaration is really present, and,
11192 possibly obtain an improved declaration. */
11193 fn = check_classfn (context, fn, NULL_TREE);
11194
11195 if (fn)
11196 new_friend = fn;
11197 }
11198 }
11199
11200 return new_friend;
11201 }
11202
11203 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11204 template arguments, as for tsubst.
11205
11206 Returns an appropriate tsubst'd friend type or error_mark_node on
11207 failure. */
11208
11209 static tree
11210 tsubst_friend_class (tree friend_tmpl, tree args)
11211 {
11212 tree tmpl;
11213
11214 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11215 {
11216 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11217 return TREE_TYPE (tmpl);
11218 }
11219
11220 tree context = CP_DECL_CONTEXT (friend_tmpl);
11221 if (TREE_CODE (context) == NAMESPACE_DECL)
11222 push_nested_namespace (context);
11223 else
11224 {
11225 context = tsubst (context, args, tf_error, NULL_TREE);
11226 push_nested_class (context);
11227 }
11228
11229 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11230 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11231
11232 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11233 {
11234 /* The friend template has already been declared. Just
11235 check to see that the declarations match, and install any new
11236 default parameters. We must tsubst the default parameters,
11237 of course. We only need the innermost template parameters
11238 because that is all that redeclare_class_template will look
11239 at. */
11240 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11241 > TMPL_ARGS_DEPTH (args))
11242 {
11243 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11244 args, tf_warning_or_error);
11245 location_t saved_input_location = input_location;
11246 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11247 tree cons = get_constraints (tmpl);
11248 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11249 input_location = saved_input_location;
11250 }
11251 }
11252 else
11253 {
11254 /* The friend template has not already been declared. In this
11255 case, the instantiation of the template class will cause the
11256 injection of this template into the namespace scope. */
11257 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11258
11259 if (tmpl != error_mark_node)
11260 {
11261 /* The new TMPL is not an instantiation of anything, so we
11262 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11263 for the new type because that is supposed to be the
11264 corresponding template decl, i.e., TMPL. */
11265 DECL_USE_TEMPLATE (tmpl) = 0;
11266 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11267 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11268 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11269 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11270
11271 /* It is hidden. */
11272 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11273 DECL_ANTICIPATED (tmpl)
11274 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11275
11276 /* Substitute into and set the constraints on the new declaration. */
11277 if (tree ci = get_constraints (friend_tmpl))
11278 {
11279 ++processing_template_decl;
11280 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11281 DECL_FRIEND_CONTEXT (friend_tmpl));
11282 --processing_template_decl;
11283 set_constraints (tmpl, ci);
11284 }
11285
11286 /* Inject this template into the enclosing namspace scope. */
11287 tmpl = pushdecl_namespace_level (tmpl, true);
11288 }
11289 }
11290
11291 if (TREE_CODE (context) == NAMESPACE_DECL)
11292 pop_nested_namespace (context);
11293 else
11294 pop_nested_class ();
11295
11296 return TREE_TYPE (tmpl);
11297 }
11298
11299 /* Returns zero if TYPE cannot be completed later due to circularity.
11300 Otherwise returns one. */
11301
11302 static int
11303 can_complete_type_without_circularity (tree type)
11304 {
11305 if (type == NULL_TREE || type == error_mark_node)
11306 return 0;
11307 else if (COMPLETE_TYPE_P (type))
11308 return 1;
11309 else if (TREE_CODE (type) == ARRAY_TYPE)
11310 return can_complete_type_without_circularity (TREE_TYPE (type));
11311 else if (CLASS_TYPE_P (type)
11312 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11313 return 0;
11314 else
11315 return 1;
11316 }
11317
11318 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11319 tsubst_flags_t, tree);
11320
11321 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11322 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11323
11324 static tree
11325 tsubst_attribute (tree t, tree *decl_p, tree args,
11326 tsubst_flags_t complain, tree in_decl)
11327 {
11328 gcc_assert (ATTR_IS_DEPENDENT (t));
11329
11330 tree val = TREE_VALUE (t);
11331 if (val == NULL_TREE)
11332 /* Nothing to do. */;
11333 else if ((flag_openmp || flag_openmp_simd)
11334 && is_attribute_p ("omp declare simd",
11335 get_attribute_name (t)))
11336 {
11337 tree clauses = TREE_VALUE (val);
11338 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11339 complain, in_decl);
11340 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11341 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11342 tree parms = DECL_ARGUMENTS (*decl_p);
11343 clauses
11344 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11345 if (clauses)
11346 val = build_tree_list (NULL_TREE, clauses);
11347 else
11348 val = NULL_TREE;
11349 }
11350 else if (flag_openmp
11351 && is_attribute_p ("omp declare variant base",
11352 get_attribute_name (t)))
11353 {
11354 ++cp_unevaluated_operand;
11355 tree varid
11356 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11357 in_decl, /*integral_constant_expression_p=*/false);
11358 --cp_unevaluated_operand;
11359 tree chain = TREE_CHAIN (val);
11360 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11361 tree ctx = copy_list (TREE_VALUE (val));
11362 tree simd = get_identifier ("simd");
11363 tree score = get_identifier (" score");
11364 tree condition = get_identifier ("condition");
11365 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11366 {
11367 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11368 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11369 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11370 {
11371 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11372 {
11373 tree clauses = TREE_VALUE (t2);
11374 clauses = tsubst_omp_clauses (clauses,
11375 C_ORT_OMP_DECLARE_SIMD, args,
11376 complain, in_decl);
11377 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11378 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11379 TREE_VALUE (t2) = clauses;
11380 }
11381 else
11382 {
11383 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11384 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11385 if (TREE_VALUE (t3))
11386 {
11387 bool allow_string
11388 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11389 && TREE_PURPOSE (t3) != score);
11390 tree v = TREE_VALUE (t3);
11391 if (TREE_CODE (v) == STRING_CST && allow_string)
11392 continue;
11393 v = tsubst_expr (v, args, complain, in_decl, true);
11394 v = fold_non_dependent_expr (v);
11395 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11396 || (TREE_PURPOSE (t3) == score
11397 ? TREE_CODE (v) != INTEGER_CST
11398 : !tree_fits_shwi_p (v)))
11399 {
11400 location_t loc
11401 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11402 match_loc);
11403 if (TREE_PURPOSE (t3) == score)
11404 error_at (loc, "score argument must be "
11405 "constant integer expression");
11406 else if (allow_string)
11407 error_at (loc, "property must be constant "
11408 "integer expression or string "
11409 "literal");
11410 else
11411 error_at (loc, "property must be constant "
11412 "integer expression");
11413 return NULL_TREE;
11414 }
11415 else if (TREE_PURPOSE (t3) == score
11416 && tree_int_cst_sgn (v) < 0)
11417 {
11418 location_t loc
11419 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11420 match_loc);
11421 error_at (loc, "score argument must be "
11422 "non-negative");
11423 return NULL_TREE;
11424 }
11425 TREE_VALUE (t3) = v;
11426 }
11427 }
11428 }
11429 }
11430 val = tree_cons (varid, ctx, chain);
11431 }
11432 /* If the first attribute argument is an identifier, don't
11433 pass it through tsubst. Attributes like mode, format,
11434 cleanup and several target specific attributes expect it
11435 unmodified. */
11436 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11437 {
11438 tree chain
11439 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11440 /*integral_constant_expression_p=*/false);
11441 if (chain != TREE_CHAIN (val))
11442 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11443 }
11444 else if (PACK_EXPANSION_P (val))
11445 {
11446 /* An attribute pack expansion. */
11447 tree purp = TREE_PURPOSE (t);
11448 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11449 if (pack == error_mark_node)
11450 return error_mark_node;
11451 int len = TREE_VEC_LENGTH (pack);
11452 tree list = NULL_TREE;
11453 tree *q = &list;
11454 for (int i = 0; i < len; ++i)
11455 {
11456 tree elt = TREE_VEC_ELT (pack, i);
11457 *q = build_tree_list (purp, elt);
11458 q = &TREE_CHAIN (*q);
11459 }
11460 return list;
11461 }
11462 else
11463 val = tsubst_expr (val, args, complain, in_decl,
11464 /*integral_constant_expression_p=*/false);
11465
11466 if (val != TREE_VALUE (t))
11467 return build_tree_list (TREE_PURPOSE (t), val);
11468 return t;
11469 }
11470
11471 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11472 unchanged or a new TREE_LIST chain. */
11473
11474 static tree
11475 tsubst_attributes (tree attributes, tree args,
11476 tsubst_flags_t complain, tree in_decl)
11477 {
11478 tree last_dep = NULL_TREE;
11479
11480 for (tree t = attributes; t; t = TREE_CHAIN (t))
11481 if (ATTR_IS_DEPENDENT (t))
11482 {
11483 last_dep = t;
11484 attributes = copy_list (attributes);
11485 break;
11486 }
11487
11488 if (last_dep)
11489 for (tree *p = &attributes; *p; )
11490 {
11491 tree t = *p;
11492 if (ATTR_IS_DEPENDENT (t))
11493 {
11494 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11495 if (subst != t)
11496 {
11497 *p = subst;
11498 while (*p)
11499 p = &TREE_CHAIN (*p);
11500 *p = TREE_CHAIN (t);
11501 continue;
11502 }
11503 }
11504 p = &TREE_CHAIN (*p);
11505 }
11506
11507 return attributes;
11508 }
11509
11510 /* Apply any attributes which had to be deferred until instantiation
11511 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11512 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11513
11514 static void
11515 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11516 tree args, tsubst_flags_t complain, tree in_decl)
11517 {
11518 tree last_dep = NULL_TREE;
11519 tree t;
11520 tree *p;
11521
11522 if (attributes == NULL_TREE)
11523 return;
11524
11525 if (DECL_P (*decl_p))
11526 {
11527 if (TREE_TYPE (*decl_p) == error_mark_node)
11528 return;
11529 p = &DECL_ATTRIBUTES (*decl_p);
11530 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11531 to our attributes parameter. */
11532 gcc_assert (*p == attributes);
11533 }
11534 else
11535 {
11536 p = &TYPE_ATTRIBUTES (*decl_p);
11537 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11538 lookup_template_class_1, and should be preserved. */
11539 gcc_assert (*p != attributes);
11540 while (*p)
11541 p = &TREE_CHAIN (*p);
11542 }
11543
11544 for (t = attributes; t; t = TREE_CHAIN (t))
11545 if (ATTR_IS_DEPENDENT (t))
11546 {
11547 last_dep = t;
11548 attributes = copy_list (attributes);
11549 break;
11550 }
11551
11552 *p = attributes;
11553 if (last_dep)
11554 {
11555 tree late_attrs = NULL_TREE;
11556 tree *q = &late_attrs;
11557
11558 for (; *p; )
11559 {
11560 t = *p;
11561 if (ATTR_IS_DEPENDENT (t))
11562 {
11563 *p = TREE_CHAIN (t);
11564 TREE_CHAIN (t) = NULL_TREE;
11565 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11566 while (*q)
11567 q = &TREE_CHAIN (*q);
11568 }
11569 else
11570 p = &TREE_CHAIN (t);
11571 }
11572
11573 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11574 }
11575 }
11576
11577 /* The template TMPL is being instantiated with the template arguments TARGS.
11578 Perform the access checks that we deferred when parsing the template. */
11579
11580 static void
11581 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11582 {
11583 unsigned i;
11584 deferred_access_check *chk;
11585
11586 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11587 return;
11588
11589 if (vec<deferred_access_check, va_gc> *access_checks
11590 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11591 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11592 {
11593 tree decl = chk->decl;
11594 tree diag_decl = chk->diag_decl;
11595 tree type_scope = TREE_TYPE (chk->binfo);
11596
11597 if (uses_template_parms (type_scope))
11598 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11599
11600 /* Make access check error messages point to the location
11601 of the use of the typedef. */
11602 iloc_sentinel ils (chk->loc);
11603 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11604 decl, diag_decl, tf_warning_or_error);
11605 }
11606 }
11607
11608 static tree
11609 instantiate_class_template_1 (tree type)
11610 {
11611 tree templ, args, pattern, t, member;
11612 tree typedecl;
11613 tree pbinfo;
11614 tree base_list;
11615 unsigned int saved_maximum_field_alignment;
11616 tree fn_context;
11617
11618 if (type == error_mark_node)
11619 return error_mark_node;
11620
11621 if (COMPLETE_OR_OPEN_TYPE_P (type)
11622 || uses_template_parms (type))
11623 return type;
11624
11625 /* Figure out which template is being instantiated. */
11626 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11627 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11628
11629 /* Mark the type as in the process of being defined. */
11630 TYPE_BEING_DEFINED (type) = 1;
11631
11632 /* We may be in the middle of deferred access check. Disable
11633 it now. */
11634 deferring_access_check_sentinel acs (dk_no_deferred);
11635
11636 /* Determine what specialization of the original template to
11637 instantiate. */
11638 t = most_specialized_partial_spec (type, tf_warning_or_error);
11639 if (t == error_mark_node)
11640 return error_mark_node;
11641 else if (t)
11642 {
11643 /* This TYPE is actually an instantiation of a partial
11644 specialization. We replace the innermost set of ARGS with
11645 the arguments appropriate for substitution. For example,
11646 given:
11647
11648 template <class T> struct S {};
11649 template <class T> struct S<T*> {};
11650
11651 and supposing that we are instantiating S<int*>, ARGS will
11652 presently be {int*} -- but we need {int}. */
11653 pattern = TREE_TYPE (t);
11654 args = TREE_PURPOSE (t);
11655 }
11656 else
11657 {
11658 pattern = TREE_TYPE (templ);
11659 args = CLASSTYPE_TI_ARGS (type);
11660 }
11661
11662 /* If the template we're instantiating is incomplete, then clearly
11663 there's nothing we can do. */
11664 if (!COMPLETE_TYPE_P (pattern))
11665 {
11666 /* We can try again later. */
11667 TYPE_BEING_DEFINED (type) = 0;
11668 return type;
11669 }
11670
11671 /* If we've recursively instantiated too many templates, stop. */
11672 if (! push_tinst_level (type))
11673 return type;
11674
11675 int saved_unevaluated_operand = cp_unevaluated_operand;
11676 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11677
11678 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11679 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11680 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11681 fn_context = error_mark_node;
11682 if (!fn_context)
11683 push_to_top_level ();
11684 else
11685 {
11686 cp_unevaluated_operand = 0;
11687 c_inhibit_evaluation_warnings = 0;
11688 }
11689 /* Use #pragma pack from the template context. */
11690 saved_maximum_field_alignment = maximum_field_alignment;
11691 maximum_field_alignment = TYPE_PRECISION (pattern);
11692
11693 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11694
11695 /* Set the input location to the most specialized template definition.
11696 This is needed if tsubsting causes an error. */
11697 typedecl = TYPE_MAIN_DECL (pattern);
11698 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11699 DECL_SOURCE_LOCATION (typedecl);
11700
11701 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11702 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11703 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11704 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11705 if (ANON_AGGR_TYPE_P (pattern))
11706 SET_ANON_AGGR_TYPE_P (type);
11707 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11708 {
11709 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11710 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11711 /* Adjust visibility for template arguments. */
11712 determine_visibility (TYPE_MAIN_DECL (type));
11713 }
11714 if (CLASS_TYPE_P (type))
11715 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11716
11717 pbinfo = TYPE_BINFO (pattern);
11718
11719 /* We should never instantiate a nested class before its enclosing
11720 class; we need to look up the nested class by name before we can
11721 instantiate it, and that lookup should instantiate the enclosing
11722 class. */
11723 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11724 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11725
11726 base_list = NULL_TREE;
11727 if (BINFO_N_BASE_BINFOS (pbinfo))
11728 {
11729 tree pbase_binfo;
11730 tree pushed_scope;
11731 int i;
11732
11733 /* We must enter the scope containing the type, as that is where
11734 the accessibility of types named in dependent bases are
11735 looked up from. */
11736 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11737
11738 /* Substitute into each of the bases to determine the actual
11739 basetypes. */
11740 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11741 {
11742 tree base;
11743 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11744 tree expanded_bases = NULL_TREE;
11745 int idx, len = 1;
11746
11747 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11748 {
11749 expanded_bases =
11750 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11751 args, tf_error, NULL_TREE);
11752 if (expanded_bases == error_mark_node)
11753 continue;
11754
11755 len = TREE_VEC_LENGTH (expanded_bases);
11756 }
11757
11758 for (idx = 0; idx < len; idx++)
11759 {
11760 if (expanded_bases)
11761 /* Extract the already-expanded base class. */
11762 base = TREE_VEC_ELT (expanded_bases, idx);
11763 else
11764 /* Substitute to figure out the base class. */
11765 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11766 NULL_TREE);
11767
11768 if (base == error_mark_node)
11769 continue;
11770
11771 base_list = tree_cons (access, base, base_list);
11772 if (BINFO_VIRTUAL_P (pbase_binfo))
11773 TREE_TYPE (base_list) = integer_type_node;
11774 }
11775 }
11776
11777 /* The list is now in reverse order; correct that. */
11778 base_list = nreverse (base_list);
11779
11780 if (pushed_scope)
11781 pop_scope (pushed_scope);
11782 }
11783 /* Now call xref_basetypes to set up all the base-class
11784 information. */
11785 xref_basetypes (type, base_list);
11786
11787 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11788 (int) ATTR_FLAG_TYPE_IN_PLACE,
11789 args, tf_error, NULL_TREE);
11790 fixup_attribute_variants (type);
11791
11792 /* Now that our base classes are set up, enter the scope of the
11793 class, so that name lookups into base classes, etc. will work
11794 correctly. This is precisely analogous to what we do in
11795 begin_class_definition when defining an ordinary non-template
11796 class, except we also need to push the enclosing classes. */
11797 push_nested_class (type);
11798
11799 /* Now members are processed in the order of declaration. */
11800 for (member = CLASSTYPE_DECL_LIST (pattern);
11801 member; member = TREE_CHAIN (member))
11802 {
11803 tree t = TREE_VALUE (member);
11804
11805 if (TREE_PURPOSE (member))
11806 {
11807 if (TYPE_P (t))
11808 {
11809 if (LAMBDA_TYPE_P (t))
11810 /* A closure type for a lambda in an NSDMI or default argument.
11811 Ignore it; it will be regenerated when needed. */
11812 continue;
11813
11814 /* Build new CLASSTYPE_NESTED_UTDS. */
11815 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11816 && TYPE_LANG_SPECIFIC (t)
11817 && CLASSTYPE_IS_TEMPLATE (t));
11818
11819 /* If the member is a class template, then -- even after
11820 substitution -- there may be dependent types in the
11821 template argument list for the class. We increment
11822 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11823 that function will assume that no types are dependent
11824 when outside of a template. */
11825 if (class_template_p)
11826 ++processing_template_decl;
11827 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11828 if (class_template_p)
11829 --processing_template_decl;
11830 if (newtag == error_mark_node)
11831 continue;
11832
11833 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11834 {
11835 tree name = TYPE_IDENTIFIER (t);
11836
11837 if (class_template_p)
11838 /* Unfortunately, lookup_template_class sets
11839 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11840 instantiation (i.e., for the type of a member
11841 template class nested within a template class.)
11842 This behavior is required for
11843 maybe_process_partial_specialization to work
11844 correctly, but is not accurate in this case;
11845 the TAG is not an instantiation of anything.
11846 (The corresponding TEMPLATE_DECL is an
11847 instantiation, but the TYPE is not.) */
11848 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11849
11850 /* Now, we call pushtag to put this NEWTAG into the scope of
11851 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11852 pushtag calling push_template_decl. We don't have to do
11853 this for enums because it will already have been done in
11854 tsubst_enum. */
11855 if (name)
11856 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11857 pushtag (name, newtag, /*tag_scope=*/ts_current);
11858 }
11859 }
11860 else if (DECL_DECLARES_FUNCTION_P (t))
11861 {
11862 tree r;
11863
11864 if (TREE_CODE (t) == TEMPLATE_DECL)
11865 ++processing_template_decl;
11866 r = tsubst (t, args, tf_error, NULL_TREE);
11867 if (TREE_CODE (t) == TEMPLATE_DECL)
11868 --processing_template_decl;
11869 set_current_access_from_decl (r);
11870 finish_member_declaration (r);
11871 /* Instantiate members marked with attribute used. */
11872 if (r != error_mark_node && DECL_PRESERVE_P (r))
11873 mark_used (r);
11874 if (TREE_CODE (r) == FUNCTION_DECL
11875 && DECL_OMP_DECLARE_REDUCTION_P (r))
11876 cp_check_omp_declare_reduction (r);
11877 }
11878 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11879 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11880 /* A closure type for a lambda in an NSDMI or default argument.
11881 Ignore it; it will be regenerated when needed. */;
11882 else
11883 {
11884 /* Build new TYPE_FIELDS. */
11885 if (TREE_CODE (t) == STATIC_ASSERT)
11886 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11887 /*integral_constant_expression_p=*/true);
11888 else if (TREE_CODE (t) != CONST_DECL)
11889 {
11890 tree r;
11891 tree vec = NULL_TREE;
11892 int len = 1;
11893
11894 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11895 /* The file and line for this declaration, to
11896 assist in error message reporting. Since we
11897 called push_tinst_level above, we don't need to
11898 restore these. */
11899 input_location = DECL_SOURCE_LOCATION (t);
11900
11901 if (TREE_CODE (t) == TEMPLATE_DECL)
11902 ++processing_template_decl;
11903 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11904 if (TREE_CODE (t) == TEMPLATE_DECL)
11905 --processing_template_decl;
11906
11907 if (TREE_CODE (r) == TREE_VEC)
11908 {
11909 /* A capture pack became multiple fields. */
11910 vec = r;
11911 len = TREE_VEC_LENGTH (vec);
11912 }
11913
11914 for (int i = 0; i < len; ++i)
11915 {
11916 if (vec)
11917 r = TREE_VEC_ELT (vec, i);
11918 if (VAR_P (r))
11919 {
11920 /* In [temp.inst]:
11921
11922 [t]he initialization (and any associated
11923 side-effects) of a static data member does
11924 not occur unless the static data member is
11925 itself used in a way that requires the
11926 definition of the static data member to
11927 exist.
11928
11929 Therefore, we do not substitute into the
11930 initialized for the static data member here. */
11931 finish_static_data_member_decl
11932 (r,
11933 /*init=*/NULL_TREE,
11934 /*init_const_expr_p=*/false,
11935 /*asmspec_tree=*/NULL_TREE,
11936 /*flags=*/0);
11937 /* Instantiate members marked with attribute used. */
11938 if (r != error_mark_node && DECL_PRESERVE_P (r))
11939 mark_used (r);
11940 }
11941 else if (TREE_CODE (r) == FIELD_DECL)
11942 {
11943 /* Determine whether R has a valid type and can be
11944 completed later. If R is invalid, then its type
11945 is replaced by error_mark_node. */
11946 tree rtype = TREE_TYPE (r);
11947 if (can_complete_type_without_circularity (rtype))
11948 complete_type (rtype);
11949
11950 if (!complete_or_array_type_p (rtype))
11951 {
11952 /* If R's type couldn't be completed and
11953 it isn't a flexible array member (whose
11954 type is incomplete by definition) give
11955 an error. */
11956 cxx_incomplete_type_error (r, rtype);
11957 TREE_TYPE (r) = error_mark_node;
11958 }
11959 else if (TREE_CODE (rtype) == ARRAY_TYPE
11960 && TYPE_DOMAIN (rtype) == NULL_TREE
11961 && (TREE_CODE (type) == UNION_TYPE
11962 || TREE_CODE (type) == QUAL_UNION_TYPE))
11963 {
11964 error ("flexible array member %qD in union", r);
11965 TREE_TYPE (r) = error_mark_node;
11966 }
11967 else if (!verify_type_context (input_location,
11968 TCTX_FIELD, rtype))
11969 TREE_TYPE (r) = error_mark_node;
11970 }
11971
11972 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11973 such a thing will already have been added to the field
11974 list by tsubst_enum in finish_member_declaration in the
11975 CLASSTYPE_NESTED_UTDS case above. */
11976 if (!(TREE_CODE (r) == TYPE_DECL
11977 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11978 && DECL_ARTIFICIAL (r)))
11979 {
11980 set_current_access_from_decl (r);
11981 finish_member_declaration (r);
11982 }
11983 }
11984 }
11985 }
11986 }
11987 else
11988 {
11989 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11990 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11991 {
11992 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11993
11994 tree friend_type = t;
11995 bool adjust_processing_template_decl = false;
11996
11997 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11998 {
11999 /* template <class T> friend class C; */
12000 friend_type = tsubst_friend_class (friend_type, args);
12001 adjust_processing_template_decl = true;
12002 }
12003 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12004 {
12005 /* template <class T> friend class C::D; */
12006 friend_type = tsubst (friend_type, args,
12007 tf_warning_or_error, NULL_TREE);
12008 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12009 friend_type = TREE_TYPE (friend_type);
12010 adjust_processing_template_decl = true;
12011 }
12012 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12013 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12014 {
12015 /* This could be either
12016
12017 friend class T::C;
12018
12019 when dependent_type_p is false or
12020
12021 template <class U> friend class T::C;
12022
12023 otherwise. */
12024 /* Bump processing_template_decl in case this is something like
12025 template <class T> friend struct A<T>::B. */
12026 ++processing_template_decl;
12027 friend_type = tsubst (friend_type, args,
12028 tf_warning_or_error, NULL_TREE);
12029 if (dependent_type_p (friend_type))
12030 adjust_processing_template_decl = true;
12031 --processing_template_decl;
12032 }
12033 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
12034 && !CLASSTYPE_USE_TEMPLATE (friend_type)
12035 && TYPE_HIDDEN_P (friend_type))
12036 {
12037 /* friend class C;
12038
12039 where C hasn't been declared yet. Let's lookup name
12040 from namespace scope directly, bypassing any name that
12041 come from dependent base class. */
12042 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
12043
12044 /* The call to xref_tag_from_type does injection for friend
12045 classes. */
12046 push_nested_namespace (ns);
12047 friend_type =
12048 xref_tag_from_type (friend_type, NULL_TREE,
12049 /*tag_scope=*/ts_current);
12050 pop_nested_namespace (ns);
12051 }
12052 else if (uses_template_parms (friend_type))
12053 /* friend class C<T>; */
12054 friend_type = tsubst (friend_type, args,
12055 tf_warning_or_error, NULL_TREE);
12056 /* Otherwise it's
12057
12058 friend class C;
12059
12060 where C is already declared or
12061
12062 friend class C<int>;
12063
12064 We don't have to do anything in these cases. */
12065
12066 if (adjust_processing_template_decl)
12067 /* Trick make_friend_class into realizing that the friend
12068 we're adding is a template, not an ordinary class. It's
12069 important that we use make_friend_class since it will
12070 perform some error-checking and output cross-reference
12071 information. */
12072 ++processing_template_decl;
12073
12074 if (friend_type != error_mark_node)
12075 make_friend_class (type, friend_type, /*complain=*/false);
12076
12077 if (adjust_processing_template_decl)
12078 --processing_template_decl;
12079 }
12080 else
12081 {
12082 /* Build new DECL_FRIENDLIST. */
12083 tree r;
12084
12085 /* The file and line for this declaration, to
12086 assist in error message reporting. Since we
12087 called push_tinst_level above, we don't need to
12088 restore these. */
12089 input_location = DECL_SOURCE_LOCATION (t);
12090
12091 if (TREE_CODE (t) == TEMPLATE_DECL)
12092 {
12093 ++processing_template_decl;
12094 push_deferring_access_checks (dk_no_check);
12095 }
12096
12097 r = tsubst_friend_function (t, args);
12098 add_friend (type, r, /*complain=*/false);
12099 if (TREE_CODE (t) == TEMPLATE_DECL)
12100 {
12101 pop_deferring_access_checks ();
12102 --processing_template_decl;
12103 }
12104 }
12105 }
12106 }
12107
12108 if (fn_context)
12109 {
12110 /* Restore these before substituting into the lambda capture
12111 initializers. */
12112 cp_unevaluated_operand = saved_unevaluated_operand;
12113 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12114 }
12115
12116 /* Set the file and line number information to whatever is given for
12117 the class itself. This puts error messages involving generated
12118 implicit functions at a predictable point, and the same point
12119 that would be used for non-template classes. */
12120 input_location = DECL_SOURCE_LOCATION (typedecl);
12121
12122 unreverse_member_declarations (type);
12123 finish_struct_1 (type);
12124 TYPE_BEING_DEFINED (type) = 0;
12125
12126 /* We don't instantiate default arguments for member functions. 14.7.1:
12127
12128 The implicit instantiation of a class template specialization causes
12129 the implicit instantiation of the declarations, but not of the
12130 definitions or default arguments, of the class member functions,
12131 member classes, static data members and member templates.... */
12132
12133 perform_instantiation_time_access_checks (pattern, args);
12134 perform_deferred_access_checks (tf_warning_or_error);
12135 pop_nested_class ();
12136 maximum_field_alignment = saved_maximum_field_alignment;
12137 if (!fn_context)
12138 pop_from_top_level ();
12139 pop_tinst_level ();
12140
12141 /* The vtable for a template class can be emitted in any translation
12142 unit in which the class is instantiated. When there is no key
12143 method, however, finish_struct_1 will already have added TYPE to
12144 the keyed_classes. */
12145 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12146 vec_safe_push (keyed_classes, type);
12147
12148 return type;
12149 }
12150
12151 /* Wrapper for instantiate_class_template_1. */
12152
12153 tree
12154 instantiate_class_template (tree type)
12155 {
12156 tree ret;
12157 timevar_push (TV_TEMPLATE_INST);
12158 ret = instantiate_class_template_1 (type);
12159 timevar_pop (TV_TEMPLATE_INST);
12160 return ret;
12161 }
12162
12163 tree
12164 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12165 {
12166 tree r;
12167
12168 if (!t)
12169 r = t;
12170 else if (TYPE_P (t))
12171 r = tsubst (t, args, complain, in_decl);
12172 else
12173 {
12174 if (!(complain & tf_warning))
12175 ++c_inhibit_evaluation_warnings;
12176 r = tsubst_expr (t, args, complain, in_decl,
12177 /*integral_constant_expression_p=*/true);
12178 if (!(complain & tf_warning))
12179 --c_inhibit_evaluation_warnings;
12180 }
12181
12182 return r;
12183 }
12184
12185 /* Given a function parameter pack TMPL_PARM and some function parameters
12186 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12187 and set *SPEC_P to point at the next point in the list. */
12188
12189 tree
12190 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12191 {
12192 /* Collect all of the extra "packed" parameters into an
12193 argument pack. */
12194 tree parmvec;
12195 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12196 tree spec_parm = *spec_p;
12197 int i, len;
12198
12199 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12200 if (tmpl_parm
12201 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12202 break;
12203
12204 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12205 parmvec = make_tree_vec (len);
12206 spec_parm = *spec_p;
12207 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12208 {
12209 tree elt = spec_parm;
12210 if (DECL_PACK_P (elt))
12211 elt = make_pack_expansion (elt);
12212 TREE_VEC_ELT (parmvec, i) = elt;
12213 }
12214
12215 /* Build the argument packs. */
12216 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12217 *spec_p = spec_parm;
12218
12219 return argpack;
12220 }
12221
12222 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12223 NONTYPE_ARGUMENT_PACK. */
12224
12225 static tree
12226 make_fnparm_pack (tree spec_parm)
12227 {
12228 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12229 }
12230
12231 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12232 pack expansion with no extra args, 2 if it has extra args, or 0
12233 if it is not a pack expansion. */
12234
12235 static int
12236 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12237 {
12238 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12239 /* We're being called before this happens in tsubst_pack_expansion. */
12240 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12241 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12242 if (i >= TREE_VEC_LENGTH (vec))
12243 return 0;
12244 tree elt = TREE_VEC_ELT (vec, i);
12245 if (DECL_P (elt))
12246 /* A decl pack is itself an expansion. */
12247 elt = TREE_TYPE (elt);
12248 if (!PACK_EXPANSION_P (elt))
12249 return 0;
12250 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12251 return 2;
12252 return 1;
12253 }
12254
12255
12256 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12257
12258 static tree
12259 make_argument_pack_select (tree arg_pack, unsigned index)
12260 {
12261 tree aps = make_node (ARGUMENT_PACK_SELECT);
12262
12263 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12264 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12265
12266 return aps;
12267 }
12268
12269 /* This is a subroutine of tsubst_pack_expansion.
12270
12271 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12272 mechanism to store the (non complete list of) arguments of the
12273 substitution and return a non substituted pack expansion, in order
12274 to wait for when we have enough arguments to really perform the
12275 substitution. */
12276
12277 static bool
12278 use_pack_expansion_extra_args_p (tree parm_packs,
12279 int arg_pack_len,
12280 bool has_empty_arg)
12281 {
12282 /* If one pack has an expansion and another pack has a normal
12283 argument or if one pack has an empty argument and an another
12284 one hasn't then tsubst_pack_expansion cannot perform the
12285 substitution and need to fall back on the
12286 PACK_EXPANSION_EXTRA mechanism. */
12287 if (parm_packs == NULL_TREE)
12288 return false;
12289 else if (has_empty_arg)
12290 {
12291 /* If all the actual packs are pack expansions, we can still
12292 subsitute directly. */
12293 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12294 {
12295 tree a = TREE_VALUE (p);
12296 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12297 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12298 a = ARGUMENT_PACK_ARGS (a);
12299 if (TREE_VEC_LENGTH (a) == 1)
12300 a = TREE_VEC_ELT (a, 0);
12301 if (PACK_EXPANSION_P (a))
12302 continue;
12303 return true;
12304 }
12305 return false;
12306 }
12307
12308 bool has_expansion_arg = false;
12309 for (int i = 0 ; i < arg_pack_len; ++i)
12310 {
12311 bool has_non_expansion_arg = false;
12312 for (tree parm_pack = parm_packs;
12313 parm_pack;
12314 parm_pack = TREE_CHAIN (parm_pack))
12315 {
12316 tree arg = TREE_VALUE (parm_pack);
12317
12318 int exp = argument_pack_element_is_expansion_p (arg, i);
12319 if (exp == 2)
12320 /* We can't substitute a pack expansion with extra args into
12321 our pattern. */
12322 return true;
12323 else if (exp)
12324 has_expansion_arg = true;
12325 else
12326 has_non_expansion_arg = true;
12327 }
12328
12329 if (has_expansion_arg && has_non_expansion_arg)
12330 return true;
12331 }
12332 return false;
12333 }
12334
12335 /* [temp.variadic]/6 says that:
12336
12337 The instantiation of a pack expansion [...]
12338 produces a list E1,E2, ..., En, where N is the number of elements
12339 in the pack expansion parameters.
12340
12341 This subroutine of tsubst_pack_expansion produces one of these Ei.
12342
12343 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12344 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12345 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12346 INDEX is the index 'i' of the element Ei to produce. ARGS,
12347 COMPLAIN, and IN_DECL are the same parameters as for the
12348 tsubst_pack_expansion function.
12349
12350 The function returns the resulting Ei upon successful completion,
12351 or error_mark_node.
12352
12353 Note that this function possibly modifies the ARGS parameter, so
12354 it's the responsibility of the caller to restore it. */
12355
12356 static tree
12357 gen_elem_of_pack_expansion_instantiation (tree pattern,
12358 tree parm_packs,
12359 unsigned index,
12360 tree args /* This parm gets
12361 modified. */,
12362 tsubst_flags_t complain,
12363 tree in_decl)
12364 {
12365 tree t;
12366 bool ith_elem_is_expansion = false;
12367
12368 /* For each parameter pack, change the substitution of the parameter
12369 pack to the ith argument in its argument pack, then expand the
12370 pattern. */
12371 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12372 {
12373 tree parm = TREE_PURPOSE (pack);
12374 tree arg_pack = TREE_VALUE (pack);
12375 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12376
12377 ith_elem_is_expansion |=
12378 argument_pack_element_is_expansion_p (arg_pack, index);
12379
12380 /* Select the Ith argument from the pack. */
12381 if (TREE_CODE (parm) == PARM_DECL
12382 || VAR_P (parm)
12383 || TREE_CODE (parm) == FIELD_DECL)
12384 {
12385 if (index == 0)
12386 {
12387 aps = make_argument_pack_select (arg_pack, index);
12388 if (!mark_used (parm, complain) && !(complain & tf_error))
12389 return error_mark_node;
12390 register_local_specialization (aps, parm);
12391 }
12392 else
12393 aps = retrieve_local_specialization (parm);
12394 }
12395 else
12396 {
12397 int idx, level;
12398 template_parm_level_and_index (parm, &level, &idx);
12399
12400 if (index == 0)
12401 {
12402 aps = make_argument_pack_select (arg_pack, index);
12403 /* Update the corresponding argument. */
12404 TMPL_ARG (args, level, idx) = aps;
12405 }
12406 else
12407 /* Re-use the ARGUMENT_PACK_SELECT. */
12408 aps = TMPL_ARG (args, level, idx);
12409 }
12410 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12411 }
12412
12413 /* Substitute into the PATTERN with the (possibly altered)
12414 arguments. */
12415 if (pattern == in_decl)
12416 /* Expanding a fixed parameter pack from
12417 coerce_template_parameter_pack. */
12418 t = tsubst_decl (pattern, args, complain);
12419 else if (pattern == error_mark_node)
12420 t = error_mark_node;
12421 else if (!TYPE_P (pattern))
12422 t = tsubst_expr (pattern, args, complain, in_decl,
12423 /*integral_constant_expression_p=*/false);
12424 else
12425 t = tsubst (pattern, args, complain, in_decl);
12426
12427 /* If the Ith argument pack element is a pack expansion, then
12428 the Ith element resulting from the substituting is going to
12429 be a pack expansion as well. */
12430 if (ith_elem_is_expansion)
12431 t = make_pack_expansion (t, complain);
12432
12433 return t;
12434 }
12435
12436 /* When the unexpanded parameter pack in a fold expression expands to an empty
12437 sequence, the value of the expression is as follows; the program is
12438 ill-formed if the operator is not listed in this table.
12439
12440 && true
12441 || false
12442 , void() */
12443
12444 tree
12445 expand_empty_fold (tree t, tsubst_flags_t complain)
12446 {
12447 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12448 if (!FOLD_EXPR_MODIFY_P (t))
12449 switch (code)
12450 {
12451 case TRUTH_ANDIF_EXPR:
12452 return boolean_true_node;
12453 case TRUTH_ORIF_EXPR:
12454 return boolean_false_node;
12455 case COMPOUND_EXPR:
12456 return void_node;
12457 default:
12458 break;
12459 }
12460
12461 if (complain & tf_error)
12462 error_at (location_of (t),
12463 "fold of empty expansion over %O", code);
12464 return error_mark_node;
12465 }
12466
12467 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12468 form an expression that combines the two terms using the
12469 operator of T. */
12470
12471 static tree
12472 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12473 {
12474 tree op = FOLD_EXPR_OP (t);
12475 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12476
12477 // Handle compound assignment operators.
12478 if (FOLD_EXPR_MODIFY_P (t))
12479 return build_x_modify_expr (input_location, left, code, right, complain);
12480
12481 warning_sentinel s(warn_parentheses);
12482 switch (code)
12483 {
12484 case COMPOUND_EXPR:
12485 return build_x_compound_expr (input_location, left, right, complain);
12486 default:
12487 return build_x_binary_op (input_location, code,
12488 left, TREE_CODE (left),
12489 right, TREE_CODE (right),
12490 /*overload=*/NULL,
12491 complain);
12492 }
12493 }
12494
12495 /* Substitute ARGS into the pack of a fold expression T. */
12496
12497 static inline tree
12498 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12499 {
12500 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12501 }
12502
12503 /* Substitute ARGS into the pack of a fold expression T. */
12504
12505 static inline tree
12506 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12507 {
12508 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12509 }
12510
12511 /* Expand a PACK of arguments into a grouped as left fold.
12512 Given a pack containing elements A0, A1, ..., An and an
12513 operator @, this builds the expression:
12514
12515 ((A0 @ A1) @ A2) ... @ An
12516
12517 Note that PACK must not be empty.
12518
12519 The operator is defined by the original fold expression T. */
12520
12521 static tree
12522 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12523 {
12524 tree left = TREE_VEC_ELT (pack, 0);
12525 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12526 {
12527 tree right = TREE_VEC_ELT (pack, i);
12528 left = fold_expression (t, left, right, complain);
12529 }
12530 return left;
12531 }
12532
12533 /* Substitute into a unary left fold expression. */
12534
12535 static tree
12536 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12537 tree in_decl)
12538 {
12539 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12540 if (pack == error_mark_node)
12541 return error_mark_node;
12542 if (PACK_EXPANSION_P (pack))
12543 {
12544 tree r = copy_node (t);
12545 FOLD_EXPR_PACK (r) = pack;
12546 return r;
12547 }
12548 if (TREE_VEC_LENGTH (pack) == 0)
12549 return expand_empty_fold (t, complain);
12550 else
12551 return expand_left_fold (t, pack, complain);
12552 }
12553
12554 /* Substitute into a binary left fold expression.
12555
12556 Do ths by building a single (non-empty) vector of argumnts and
12557 building the expression from those elements. */
12558
12559 static tree
12560 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12561 tree in_decl)
12562 {
12563 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12564 if (pack == error_mark_node)
12565 return error_mark_node;
12566 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12567 if (init == error_mark_node)
12568 return error_mark_node;
12569
12570 if (PACK_EXPANSION_P (pack))
12571 {
12572 tree r = copy_node (t);
12573 FOLD_EXPR_PACK (r) = pack;
12574 FOLD_EXPR_INIT (r) = init;
12575 return r;
12576 }
12577
12578 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12579 TREE_VEC_ELT (vec, 0) = init;
12580 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12581 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12582
12583 return expand_left_fold (t, vec, complain);
12584 }
12585
12586 /* Expand a PACK of arguments into a grouped as right fold.
12587 Given a pack containing elementns A0, A1, ..., and an
12588 operator @, this builds the expression:
12589
12590 A0@ ... (An-2 @ (An-1 @ An))
12591
12592 Note that PACK must not be empty.
12593
12594 The operator is defined by the original fold expression T. */
12595
12596 tree
12597 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12598 {
12599 // Build the expression.
12600 int n = TREE_VEC_LENGTH (pack);
12601 tree right = TREE_VEC_ELT (pack, n - 1);
12602 for (--n; n != 0; --n)
12603 {
12604 tree left = TREE_VEC_ELT (pack, n - 1);
12605 right = fold_expression (t, left, right, complain);
12606 }
12607 return right;
12608 }
12609
12610 /* Substitute into a unary right fold expression. */
12611
12612 static tree
12613 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12614 tree in_decl)
12615 {
12616 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12617 if (pack == error_mark_node)
12618 return error_mark_node;
12619 if (PACK_EXPANSION_P (pack))
12620 {
12621 tree r = copy_node (t);
12622 FOLD_EXPR_PACK (r) = pack;
12623 return r;
12624 }
12625 if (TREE_VEC_LENGTH (pack) == 0)
12626 return expand_empty_fold (t, complain);
12627 else
12628 return expand_right_fold (t, pack, complain);
12629 }
12630
12631 /* Substitute into a binary right fold expression.
12632
12633 Do ths by building a single (non-empty) vector of arguments and
12634 building the expression from those elements. */
12635
12636 static tree
12637 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12638 tree in_decl)
12639 {
12640 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12641 if (pack == error_mark_node)
12642 return error_mark_node;
12643 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12644 if (init == error_mark_node)
12645 return error_mark_node;
12646
12647 if (PACK_EXPANSION_P (pack))
12648 {
12649 tree r = copy_node (t);
12650 FOLD_EXPR_PACK (r) = pack;
12651 FOLD_EXPR_INIT (r) = init;
12652 return r;
12653 }
12654
12655 int n = TREE_VEC_LENGTH (pack);
12656 tree vec = make_tree_vec (n + 1);
12657 for (int i = 0; i < n; ++i)
12658 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12659 TREE_VEC_ELT (vec, n) = init;
12660
12661 return expand_right_fold (t, vec, complain);
12662 }
12663
12664 /* Walk through the pattern of a pack expansion, adding everything in
12665 local_specializations to a list. */
12666
12667 class el_data
12668 {
12669 public:
12670 hash_set<tree> internal;
12671 tree extra;
12672 tsubst_flags_t complain;
12673
12674 el_data (tsubst_flags_t c)
12675 : extra (NULL_TREE), complain (c) {}
12676 };
12677 static tree
12678 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12679 {
12680 el_data &data = *reinterpret_cast<el_data*>(data_);
12681 tree *extra = &data.extra;
12682 tsubst_flags_t complain = data.complain;
12683
12684 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12685 /* Remember local typedefs (85214). */
12686 tp = &TYPE_NAME (*tp);
12687
12688 if (TREE_CODE (*tp) == DECL_EXPR)
12689 data.internal.add (DECL_EXPR_DECL (*tp));
12690 else if (tree spec = retrieve_local_specialization (*tp))
12691 {
12692 if (data.internal.contains (*tp))
12693 /* Don't mess with variables declared within the pattern. */
12694 return NULL_TREE;
12695 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12696 {
12697 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12698 tree args = ARGUMENT_PACK_ARGS (spec);
12699 if (TREE_VEC_LENGTH (args) == 1)
12700 {
12701 tree elt = TREE_VEC_ELT (args, 0);
12702 if (PACK_EXPANSION_P (elt))
12703 elt = PACK_EXPANSION_PATTERN (elt);
12704 if (DECL_PACK_P (elt))
12705 spec = elt;
12706 }
12707 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12708 {
12709 /* Handle lambda capture here, since we aren't doing any
12710 substitution now, and so tsubst_copy won't call
12711 process_outer_var_ref. */
12712 tree args = ARGUMENT_PACK_ARGS (spec);
12713 int len = TREE_VEC_LENGTH (args);
12714 for (int i = 0; i < len; ++i)
12715 {
12716 tree arg = TREE_VEC_ELT (args, i);
12717 tree carg = arg;
12718 if (outer_automatic_var_p (arg))
12719 carg = process_outer_var_ref (arg, complain);
12720 if (carg != arg)
12721 {
12722 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12723 proxies. */
12724 if (i == 0)
12725 {
12726 spec = copy_node (spec);
12727 args = copy_node (args);
12728 SET_ARGUMENT_PACK_ARGS (spec, args);
12729 register_local_specialization (spec, *tp);
12730 }
12731 TREE_VEC_ELT (args, i) = carg;
12732 }
12733 }
12734 }
12735 }
12736 if (outer_automatic_var_p (spec))
12737 spec = process_outer_var_ref (spec, complain);
12738 *extra = tree_cons (*tp, spec, *extra);
12739 }
12740 return NULL_TREE;
12741 }
12742 static tree
12743 extract_local_specs (tree pattern, tsubst_flags_t complain)
12744 {
12745 el_data data (complain);
12746 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12747 return data.extra;
12748 }
12749
12750 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12751 for use in PACK_EXPANSION_EXTRA_ARGS. */
12752
12753 tree
12754 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12755 {
12756 tree extra = args;
12757 if (local_specializations)
12758 if (tree locals = extract_local_specs (pattern, complain))
12759 extra = tree_cons (NULL_TREE, extra, locals);
12760 return extra;
12761 }
12762
12763 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12764 normal template args to ARGS. */
12765
12766 tree
12767 add_extra_args (tree extra, tree args)
12768 {
12769 if (extra && TREE_CODE (extra) == TREE_LIST)
12770 {
12771 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12772 {
12773 /* The partial instantiation involved local declarations collected in
12774 extract_local_specs; map from the general template to our local
12775 context. */
12776 tree gen = TREE_PURPOSE (elt);
12777 tree inst = TREE_VALUE (elt);
12778 if (DECL_P (inst))
12779 if (tree local = retrieve_local_specialization (inst))
12780 inst = local;
12781 /* else inst is already a full instantiation of the pack. */
12782 register_local_specialization (inst, gen);
12783 }
12784 gcc_assert (!TREE_PURPOSE (extra));
12785 extra = TREE_VALUE (extra);
12786 }
12787 #if 1
12788 /* I think we should always be able to substitute dependent args into the
12789 pattern. If that turns out to be incorrect in some cases, enable the
12790 alternate code (and add complain/in_decl parms to this function). */
12791 gcc_checking_assert (!uses_template_parms (extra));
12792 #else
12793 if (!uses_template_parms (extra))
12794 {
12795 gcc_unreachable ();
12796 extra = tsubst_template_args (extra, args, complain, in_decl);
12797 args = add_outermost_template_args (args, extra);
12798 }
12799 else
12800 #endif
12801 args = add_to_template_args (extra, args);
12802 return args;
12803 }
12804
12805 /* Substitute ARGS into T, which is an pack expansion
12806 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12807 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12808 (if only a partial substitution could be performed) or
12809 ERROR_MARK_NODE if there was an error. */
12810 tree
12811 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12812 tree in_decl)
12813 {
12814 tree pattern;
12815 tree pack, packs = NULL_TREE;
12816 bool unsubstituted_packs = false;
12817 int i, len = -1;
12818 tree result;
12819 bool need_local_specializations = false;
12820 int levels;
12821
12822 gcc_assert (PACK_EXPANSION_P (t));
12823 pattern = PACK_EXPANSION_PATTERN (t);
12824
12825 /* Add in any args remembered from an earlier partial instantiation. */
12826 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12827
12828 levels = TMPL_ARGS_DEPTH (args);
12829
12830 /* Determine the argument packs that will instantiate the parameter
12831 packs used in the expansion expression. While we're at it,
12832 compute the number of arguments to be expanded and make sure it
12833 is consistent. */
12834 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12835 pack = TREE_CHAIN (pack))
12836 {
12837 tree parm_pack = TREE_VALUE (pack);
12838 tree arg_pack = NULL_TREE;
12839 tree orig_arg = NULL_TREE;
12840 int level = 0;
12841
12842 if (TREE_CODE (parm_pack) == BASES)
12843 {
12844 gcc_assert (parm_pack == pattern);
12845 if (BASES_DIRECT (parm_pack))
12846 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12847 args, complain,
12848 in_decl, false),
12849 complain);
12850 else
12851 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12852 args, complain, in_decl,
12853 false), complain);
12854 }
12855 else if (builtin_pack_call_p (parm_pack))
12856 {
12857 if (parm_pack != pattern)
12858 {
12859 if (complain & tf_error)
12860 sorry ("%qE is not the entire pattern of the pack expansion",
12861 parm_pack);
12862 return error_mark_node;
12863 }
12864 return expand_builtin_pack_call (parm_pack, args,
12865 complain, in_decl);
12866 }
12867 else if (TREE_CODE (parm_pack) == PARM_DECL)
12868 {
12869 /* We know we have correct local_specializations if this
12870 expansion is at function scope, or if we're dealing with a
12871 local parameter in a requires expression; for the latter,
12872 tsubst_requires_expr set it up appropriately. */
12873 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12874 arg_pack = retrieve_local_specialization (parm_pack);
12875 else
12876 /* We can't rely on local_specializations for a parameter
12877 name used later in a function declaration (such as in a
12878 late-specified return type). Even if it exists, it might
12879 have the wrong value for a recursive call. */
12880 need_local_specializations = true;
12881
12882 if (!arg_pack)
12883 {
12884 /* This parameter pack was used in an unevaluated context. Just
12885 make a dummy decl, since it's only used for its type. */
12886 ++cp_unevaluated_operand;
12887 arg_pack = tsubst_decl (parm_pack, args, complain);
12888 --cp_unevaluated_operand;
12889 if (arg_pack && DECL_PACK_P (arg_pack))
12890 /* Partial instantiation of the parm_pack, we can't build
12891 up an argument pack yet. */
12892 arg_pack = NULL_TREE;
12893 else
12894 arg_pack = make_fnparm_pack (arg_pack);
12895 }
12896 else if (DECL_PACK_P (arg_pack))
12897 /* This argument pack isn't fully instantiated yet. */
12898 arg_pack = NULL_TREE;
12899 }
12900 else if (is_capture_proxy (parm_pack))
12901 {
12902 arg_pack = retrieve_local_specialization (parm_pack);
12903 if (DECL_PACK_P (arg_pack))
12904 arg_pack = NULL_TREE;
12905 }
12906 else
12907 {
12908 int idx;
12909 template_parm_level_and_index (parm_pack, &level, &idx);
12910 if (level <= levels)
12911 arg_pack = TMPL_ARG (args, level, idx);
12912
12913 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12914 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12915 arg_pack = NULL_TREE;
12916 }
12917
12918 orig_arg = arg_pack;
12919 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12920 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12921
12922 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12923 /* This can only happen if we forget to expand an argument
12924 pack somewhere else. Just return an error, silently. */
12925 {
12926 result = make_tree_vec (1);
12927 TREE_VEC_ELT (result, 0) = error_mark_node;
12928 return result;
12929 }
12930
12931 if (arg_pack)
12932 {
12933 int my_len =
12934 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12935
12936 /* Don't bother trying to do a partial substitution with
12937 incomplete packs; we'll try again after deduction. */
12938 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12939 return t;
12940
12941 if (len < 0)
12942 len = my_len;
12943 else if (len != my_len)
12944 {
12945 if (!(complain & tf_error))
12946 /* Fail quietly. */;
12947 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12948 error ("mismatched argument pack lengths while expanding %qT",
12949 pattern);
12950 else
12951 error ("mismatched argument pack lengths while expanding %qE",
12952 pattern);
12953 return error_mark_node;
12954 }
12955
12956 /* Keep track of the parameter packs and their corresponding
12957 argument packs. */
12958 packs = tree_cons (parm_pack, arg_pack, packs);
12959 TREE_TYPE (packs) = orig_arg;
12960 }
12961 else
12962 {
12963 /* We can't substitute for this parameter pack. We use a flag as
12964 well as the missing_level counter because function parameter
12965 packs don't have a level. */
12966 gcc_assert (processing_template_decl || is_auto (parm_pack));
12967 unsubstituted_packs = true;
12968 }
12969 }
12970
12971 /* If the expansion is just T..., return the matching argument pack, unless
12972 we need to call convert_from_reference on all the elements. This is an
12973 important optimization; see c++/68422. */
12974 if (!unsubstituted_packs
12975 && TREE_PURPOSE (packs) == pattern)
12976 {
12977 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12978
12979 /* If the argument pack is a single pack expansion, pull it out. */
12980 if (TREE_VEC_LENGTH (args) == 1
12981 && pack_expansion_args_count (args))
12982 return TREE_VEC_ELT (args, 0);
12983
12984 /* Types need no adjustment, nor does sizeof..., and if we still have
12985 some pack expansion args we won't do anything yet. */
12986 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12987 || PACK_EXPANSION_SIZEOF_P (t)
12988 || pack_expansion_args_count (args))
12989 return args;
12990 /* Also optimize expression pack expansions if we can tell that the
12991 elements won't have reference type. */
12992 tree type = TREE_TYPE (pattern);
12993 if (type && !TYPE_REF_P (type)
12994 && !PACK_EXPANSION_P (type)
12995 && !WILDCARD_TYPE_P (type))
12996 return args;
12997 /* Otherwise use the normal path so we get convert_from_reference. */
12998 }
12999
13000 /* We cannot expand this expansion expression, because we don't have
13001 all of the argument packs we need. */
13002 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13003 {
13004 /* We got some full packs, but we can't substitute them in until we
13005 have values for all the packs. So remember these until then. */
13006
13007 t = make_pack_expansion (pattern, complain);
13008 PACK_EXPANSION_EXTRA_ARGS (t)
13009 = build_extra_args (pattern, args, complain);
13010 return t;
13011 }
13012
13013 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13014 type, so create our own local specializations map; the current map is
13015 either NULL or (in the case of recursive unification) might have
13016 bindings that we don't want to use or alter. */
13017 local_specialization_stack lss (need_local_specializations
13018 ? lss_blank : lss_nop);
13019
13020 if (unsubstituted_packs)
13021 {
13022 /* There were no real arguments, we're just replacing a parameter
13023 pack with another version of itself. Substitute into the
13024 pattern and return a PACK_EXPANSION_*. The caller will need to
13025 deal with that. */
13026 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13027 t = tsubst_expr (pattern, args, complain, in_decl,
13028 /*integral_constant_expression_p=*/false);
13029 else
13030 t = tsubst (pattern, args, complain, in_decl);
13031 t = make_pack_expansion (t, complain);
13032 return t;
13033 }
13034
13035 gcc_assert (len >= 0);
13036
13037 /* For each argument in each argument pack, substitute into the
13038 pattern. */
13039 result = make_tree_vec (len);
13040 tree elem_args = copy_template_args (args);
13041 for (i = 0; i < len; ++i)
13042 {
13043 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13044 i,
13045 elem_args, complain,
13046 in_decl);
13047 TREE_VEC_ELT (result, i) = t;
13048 if (t == error_mark_node)
13049 {
13050 result = error_mark_node;
13051 break;
13052 }
13053 }
13054
13055 /* Update ARGS to restore the substitution from parameter packs to
13056 their argument packs. */
13057 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13058 {
13059 tree parm = TREE_PURPOSE (pack);
13060
13061 if (TREE_CODE (parm) == PARM_DECL
13062 || VAR_P (parm)
13063 || TREE_CODE (parm) == FIELD_DECL)
13064 register_local_specialization (TREE_TYPE (pack), parm);
13065 else
13066 {
13067 int idx, level;
13068
13069 if (TREE_VALUE (pack) == NULL_TREE)
13070 continue;
13071
13072 template_parm_level_and_index (parm, &level, &idx);
13073
13074 /* Update the corresponding argument. */
13075 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13076 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13077 TREE_TYPE (pack);
13078 else
13079 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13080 }
13081 }
13082
13083 /* If the dependent pack arguments were such that we end up with only a
13084 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13085 if (len == 1 && TREE_CODE (result) == TREE_VEC
13086 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13087 return TREE_VEC_ELT (result, 0);
13088
13089 return result;
13090 }
13091
13092 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13093 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13094 parameter packs; all parms generated from a function parameter pack will
13095 have the same DECL_PARM_INDEX. */
13096
13097 tree
13098 get_pattern_parm (tree parm, tree tmpl)
13099 {
13100 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13101 tree patparm;
13102
13103 if (DECL_ARTIFICIAL (parm))
13104 {
13105 for (patparm = DECL_ARGUMENTS (pattern);
13106 patparm; patparm = DECL_CHAIN (patparm))
13107 if (DECL_ARTIFICIAL (patparm)
13108 && DECL_NAME (parm) == DECL_NAME (patparm))
13109 break;
13110 }
13111 else
13112 {
13113 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13114 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13115 gcc_assert (DECL_PARM_INDEX (patparm)
13116 == DECL_PARM_INDEX (parm));
13117 }
13118
13119 return patparm;
13120 }
13121
13122 /* Make an argument pack out of the TREE_VEC VEC. */
13123
13124 static tree
13125 make_argument_pack (tree vec)
13126 {
13127 tree pack;
13128
13129 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13130 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13131 else
13132 {
13133 pack = make_node (NONTYPE_ARGUMENT_PACK);
13134 TREE_CONSTANT (pack) = 1;
13135 }
13136 SET_ARGUMENT_PACK_ARGS (pack, vec);
13137 return pack;
13138 }
13139
13140 /* Return an exact copy of template args T that can be modified
13141 independently. */
13142
13143 static tree
13144 copy_template_args (tree t)
13145 {
13146 if (t == error_mark_node)
13147 return t;
13148
13149 int len = TREE_VEC_LENGTH (t);
13150 tree new_vec = make_tree_vec (len);
13151
13152 for (int i = 0; i < len; ++i)
13153 {
13154 tree elt = TREE_VEC_ELT (t, i);
13155 if (elt && TREE_CODE (elt) == TREE_VEC)
13156 elt = copy_template_args (elt);
13157 TREE_VEC_ELT (new_vec, i) = elt;
13158 }
13159
13160 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13161 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13162
13163 return new_vec;
13164 }
13165
13166 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13167
13168 tree
13169 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13170 tree in_decl)
13171 {
13172 /* Substitute into each of the arguments. */
13173 tree new_arg = TYPE_P (orig_arg)
13174 ? cxx_make_type (TREE_CODE (orig_arg))
13175 : make_node (TREE_CODE (orig_arg));
13176
13177 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13178 args, complain, in_decl);
13179 if (pack_args == error_mark_node)
13180 new_arg = error_mark_node;
13181 else
13182 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13183
13184 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13185 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13186
13187 return new_arg;
13188 }
13189
13190 /* Substitute ARGS into the vector or list of template arguments T. */
13191
13192 tree
13193 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13194 {
13195 tree orig_t = t;
13196 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13197 tree *elts;
13198
13199 if (t == error_mark_node)
13200 return error_mark_node;
13201
13202 len = TREE_VEC_LENGTH (t);
13203 elts = XALLOCAVEC (tree, len);
13204
13205 for (i = 0; i < len; i++)
13206 {
13207 tree orig_arg = TREE_VEC_ELT (t, i);
13208 tree new_arg;
13209
13210 if (TREE_CODE (orig_arg) == TREE_VEC)
13211 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13212 else if (PACK_EXPANSION_P (orig_arg))
13213 {
13214 /* Substitute into an expansion expression. */
13215 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13216
13217 if (TREE_CODE (new_arg) == TREE_VEC)
13218 /* Add to the expanded length adjustment the number of
13219 expanded arguments. We subtract one from this
13220 measurement, because the argument pack expression
13221 itself is already counted as 1 in
13222 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13223 the argument pack is empty. */
13224 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13225 }
13226 else if (ARGUMENT_PACK_P (orig_arg))
13227 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13228 else
13229 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13230
13231 if (new_arg == error_mark_node)
13232 return error_mark_node;
13233
13234 elts[i] = new_arg;
13235 if (new_arg != orig_arg)
13236 need_new = 1;
13237 }
13238
13239 if (!need_new)
13240 return t;
13241
13242 /* Make space for the expanded arguments coming from template
13243 argument packs. */
13244 t = make_tree_vec (len + expanded_len_adjust);
13245 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13246 arguments for a member template.
13247 In that case each TREE_VEC in ORIG_T represents a level of template
13248 arguments, and ORIG_T won't carry any non defaulted argument count.
13249 It will rather be the nested TREE_VECs that will carry one.
13250 In other words, ORIG_T carries a non defaulted argument count only
13251 if it doesn't contain any nested TREE_VEC. */
13252 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13253 {
13254 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13255 count += expanded_len_adjust;
13256 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13257 }
13258 for (i = 0, out = 0; i < len; i++)
13259 {
13260 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13261 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13262 && TREE_CODE (elts[i]) == TREE_VEC)
13263 {
13264 int idx;
13265
13266 /* Now expand the template argument pack "in place". */
13267 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13268 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13269 }
13270 else
13271 {
13272 TREE_VEC_ELT (t, out) = elts[i];
13273 out++;
13274 }
13275 }
13276
13277 return t;
13278 }
13279
13280 /* Substitute ARGS into one level PARMS of template parameters. */
13281
13282 static tree
13283 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13284 {
13285 if (parms == error_mark_node)
13286 return error_mark_node;
13287
13288 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13289
13290 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13291 {
13292 tree tuple = TREE_VEC_ELT (parms, i);
13293
13294 if (tuple == error_mark_node)
13295 continue;
13296
13297 TREE_VEC_ELT (new_vec, i) =
13298 tsubst_template_parm (tuple, args, complain);
13299 }
13300
13301 return new_vec;
13302 }
13303
13304 /* Return the result of substituting ARGS into the template parameters
13305 given by PARMS. If there are m levels of ARGS and m + n levels of
13306 PARMS, then the result will contain n levels of PARMS. For
13307 example, if PARMS is `template <class T> template <class U>
13308 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13309 result will be `template <int*, double, class V>'. */
13310
13311 static tree
13312 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13313 {
13314 tree r = NULL_TREE;
13315 tree* new_parms;
13316
13317 /* When substituting into a template, we must set
13318 PROCESSING_TEMPLATE_DECL as the template parameters may be
13319 dependent if they are based on one-another, and the dependency
13320 predicates are short-circuit outside of templates. */
13321 ++processing_template_decl;
13322
13323 for (new_parms = &r;
13324 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13325 new_parms = &(TREE_CHAIN (*new_parms)),
13326 parms = TREE_CHAIN (parms))
13327 {
13328 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13329 args, complain);
13330 *new_parms =
13331 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13332 - TMPL_ARGS_DEPTH (args)),
13333 new_vec, NULL_TREE);
13334 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13335 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13336 }
13337
13338 --processing_template_decl;
13339
13340 return r;
13341 }
13342
13343 /* Return the result of substituting ARGS into one template parameter
13344 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13345 parameter and which TREE_PURPOSE is the default argument of the
13346 template parameter. */
13347
13348 static tree
13349 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13350 {
13351 tree default_value, parm_decl;
13352
13353 if (args == NULL_TREE
13354 || t == NULL_TREE
13355 || t == error_mark_node)
13356 return t;
13357
13358 gcc_assert (TREE_CODE (t) == TREE_LIST);
13359
13360 default_value = TREE_PURPOSE (t);
13361 parm_decl = TREE_VALUE (t);
13362 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13363
13364 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13365 if (TREE_CODE (parm_decl) == PARM_DECL
13366 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13367 parm_decl = error_mark_node;
13368 default_value = tsubst_template_arg (default_value, args,
13369 complain, NULL_TREE);
13370 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13371
13372 tree r = build_tree_list (default_value, parm_decl);
13373 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13374 return r;
13375 }
13376
13377 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13378 type T. If T is not an aggregate or enumeration type, it is
13379 handled as if by tsubst. IN_DECL is as for tsubst. If
13380 ENTERING_SCOPE is nonzero, T is the context for a template which
13381 we are presently tsubst'ing. Return the substituted value. */
13382
13383 static tree
13384 tsubst_aggr_type (tree t,
13385 tree args,
13386 tsubst_flags_t complain,
13387 tree in_decl,
13388 int entering_scope)
13389 {
13390 if (t == NULL_TREE)
13391 return NULL_TREE;
13392
13393 switch (TREE_CODE (t))
13394 {
13395 case RECORD_TYPE:
13396 if (TYPE_PTRMEMFUNC_P (t))
13397 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13398
13399 /* Fall through. */
13400 case ENUMERAL_TYPE:
13401 case UNION_TYPE:
13402 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13403 {
13404 tree argvec;
13405 tree context;
13406 tree r;
13407
13408 /* In "sizeof(X<I>)" we need to evaluate "I". */
13409 cp_evaluated ev;
13410
13411 /* First, determine the context for the type we are looking
13412 up. */
13413 context = TYPE_CONTEXT (t);
13414 if (context && TYPE_P (context))
13415 {
13416 context = tsubst_aggr_type (context, args, complain,
13417 in_decl, /*entering_scope=*/1);
13418 /* If context is a nested class inside a class template,
13419 it may still need to be instantiated (c++/33959). */
13420 context = complete_type (context);
13421 }
13422
13423 /* Then, figure out what arguments are appropriate for the
13424 type we are trying to find. For example, given:
13425
13426 template <class T> struct S;
13427 template <class T, class U> void f(T, U) { S<U> su; }
13428
13429 and supposing that we are instantiating f<int, double>,
13430 then our ARGS will be {int, double}, but, when looking up
13431 S we only want {double}. */
13432 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13433 complain, in_decl);
13434 if (argvec == error_mark_node)
13435 r = error_mark_node;
13436 else if (cxx_dialect >= cxx17 && dependent_scope_p (context))
13437 {
13438 /* See maybe_dependent_member_ref. */
13439 tree name = TYPE_IDENTIFIER (t);
13440 tree fullname = name;
13441 if (instantiates_primary_template_p (t))
13442 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13443 INNERMOST_TEMPLATE_ARGS (argvec));
13444 return build_typename_type (context, name, fullname,
13445 typename_type);
13446 }
13447 else
13448 {
13449 r = lookup_template_class (t, argvec, in_decl, context,
13450 entering_scope, complain);
13451 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13452 }
13453
13454 return r;
13455 }
13456 else
13457 /* This is not a template type, so there's nothing to do. */
13458 return t;
13459
13460 default:
13461 return tsubst (t, args, complain, in_decl);
13462 }
13463 }
13464
13465 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13466
13467 /* Substitute into the default argument ARG (a default argument for
13468 FN), which has the indicated TYPE. */
13469
13470 tree
13471 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13472 tsubst_flags_t complain)
13473 {
13474 int errs = errorcount + sorrycount;
13475
13476 /* This can happen in invalid code. */
13477 if (TREE_CODE (arg) == DEFERRED_PARSE)
13478 return arg;
13479
13480 /* Shortcut {}. */
13481 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13482 && CONSTRUCTOR_NELTS (arg) == 0)
13483 return arg;
13484
13485 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13486 parm = chain_index (parmnum, parm);
13487 tree parmtype = TREE_TYPE (parm);
13488 if (DECL_BY_REFERENCE (parm))
13489 parmtype = TREE_TYPE (parmtype);
13490 if (parmtype == error_mark_node)
13491 return error_mark_node;
13492
13493 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13494
13495 tree *slot;
13496 if (defarg_inst && (slot = defarg_inst->get (parm)))
13497 return *slot;
13498
13499 /* This default argument came from a template. Instantiate the
13500 default argument here, not in tsubst. In the case of
13501 something like:
13502
13503 template <class T>
13504 struct S {
13505 static T t();
13506 void f(T = t());
13507 };
13508
13509 we must be careful to do name lookup in the scope of S<T>,
13510 rather than in the current class. */
13511 push_to_top_level ();
13512 push_access_scope (fn);
13513 push_deferring_access_checks (dk_no_deferred);
13514 start_lambda_scope (parm);
13515
13516 /* The default argument expression may cause implicitly defined
13517 member functions to be synthesized, which will result in garbage
13518 collection. We must treat this situation as if we were within
13519 the body of function so as to avoid collecting live data on the
13520 stack. */
13521 ++function_depth;
13522 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13523 complain, NULL_TREE,
13524 /*integral_constant_expression_p=*/false);
13525 --function_depth;
13526
13527 finish_lambda_scope ();
13528
13529 /* Make sure the default argument is reasonable. */
13530 arg = check_default_argument (type, arg, complain);
13531
13532 if (errorcount+sorrycount > errs
13533 && (complain & tf_warning_or_error))
13534 inform (input_location,
13535 " when instantiating default argument for call to %qD", fn);
13536
13537 pop_deferring_access_checks ();
13538 pop_access_scope (fn);
13539 pop_from_top_level ();
13540
13541 if (arg != error_mark_node && !cp_unevaluated_operand)
13542 {
13543 if (!defarg_inst)
13544 defarg_inst = decl_tree_cache_map::create_ggc (37);
13545 defarg_inst->put (parm, arg);
13546 }
13547
13548 return arg;
13549 }
13550
13551 /* Substitute into all the default arguments for FN. */
13552
13553 static void
13554 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13555 {
13556 tree arg;
13557 tree tmpl_args;
13558
13559 tmpl_args = DECL_TI_ARGS (fn);
13560
13561 /* If this function is not yet instantiated, we certainly don't need
13562 its default arguments. */
13563 if (uses_template_parms (tmpl_args))
13564 return;
13565 /* Don't do this again for clones. */
13566 if (DECL_CLONED_FUNCTION_P (fn))
13567 return;
13568
13569 int i = 0;
13570 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13571 arg;
13572 arg = TREE_CHAIN (arg), ++i)
13573 if (TREE_PURPOSE (arg))
13574 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13575 TREE_VALUE (arg),
13576 TREE_PURPOSE (arg),
13577 complain);
13578 }
13579
13580 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13581 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13582
13583 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13584
13585 void
13586 store_explicit_specifier (tree v, tree t)
13587 {
13588 if (!explicit_specifier_map)
13589 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13590 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13591 explicit_specifier_map->put (v, t);
13592 }
13593
13594 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13595
13596 static tree
13597 lookup_explicit_specifier (tree v)
13598 {
13599 return *explicit_specifier_map->get (v);
13600 }
13601
13602 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13603 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13604 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13605 identical to T. */
13606
13607 static tree
13608 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13609 tree raises, tsubst_flags_t complain)
13610 {
13611 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13612
13613 tree new_type;
13614 if (TREE_CODE (t) == FUNCTION_TYPE)
13615 {
13616 new_type = build_function_type (return_type, arg_types);
13617 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13618 }
13619 else
13620 {
13621 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13622 /* Don't pick up extra function qualifiers from the basetype. */
13623 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13624 if (! MAYBE_CLASS_TYPE_P (r))
13625 {
13626 /* [temp.deduct]
13627
13628 Type deduction may fail for any of the following
13629 reasons:
13630
13631 -- Attempting to create "pointer to member of T" when T
13632 is not a class type. */
13633 if (complain & tf_error)
13634 error ("creating pointer to member function of non-class type %qT",
13635 r);
13636 return error_mark_node;
13637 }
13638
13639 new_type = build_method_type_directly (r, return_type,
13640 TREE_CHAIN (arg_types));
13641 }
13642 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13643
13644 cp_ref_qualifier rqual = type_memfn_rqual (t);
13645 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13646 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13647 }
13648
13649 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13650 each of its formal parameters. If there is a disagreement then rebuild
13651 DECL's function type according to its formal parameter types, as part of a
13652 resolution for Core issues 1001/1322. */
13653
13654 static void
13655 maybe_rebuild_function_decl_type (tree decl)
13656 {
13657 bool function_type_needs_rebuilding = false;
13658 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13659 {
13660 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13661 while (parm_type_list && parm_type_list != void_list_node)
13662 {
13663 tree parm_type = TREE_VALUE (parm_type_list);
13664 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13665 if (!same_type_p (parm_type, formal_parm_type_unqual))
13666 {
13667 function_type_needs_rebuilding = true;
13668 break;
13669 }
13670
13671 parm_list = DECL_CHAIN (parm_list);
13672 parm_type_list = TREE_CHAIN (parm_type_list);
13673 }
13674 }
13675
13676 if (!function_type_needs_rebuilding)
13677 return;
13678
13679 const tree fntype = TREE_TYPE (decl);
13680 tree parm_list = DECL_ARGUMENTS (decl);
13681 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13682 tree new_parm_type_list = NULL_TREE;
13683 tree *q = &new_parm_type_list;
13684 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13685 {
13686 *q = copy_node (old_parm_type_list);
13687 parm_list = DECL_CHAIN (parm_list);
13688 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13689 q = &TREE_CHAIN (*q);
13690 }
13691 while (old_parm_type_list && old_parm_type_list != void_list_node)
13692 {
13693 *q = copy_node (old_parm_type_list);
13694 tree *new_parm_type = &TREE_VALUE (*q);
13695 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13696 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13697 *new_parm_type = formal_parm_type_unqual;
13698
13699 parm_list = DECL_CHAIN (parm_list);
13700 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13701 q = &TREE_CHAIN (*q);
13702 }
13703 if (old_parm_type_list == void_list_node)
13704 *q = void_list_node;
13705
13706 TREE_TYPE (decl)
13707 = rebuild_function_or_method_type (fntype,
13708 TREE_TYPE (fntype), new_parm_type_list,
13709 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13710 }
13711
13712 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13713
13714 static tree
13715 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13716 tree lambda_fntype)
13717 {
13718 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13719 hashval_t hash = 0;
13720 tree in_decl = t;
13721
13722 /* Nobody should be tsubst'ing into non-template functions. */
13723 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13724 || DECL_LOCAL_DECL_P (t));
13725
13726 if (DECL_LOCAL_DECL_P (t))
13727 {
13728 if (tree spec = retrieve_local_specialization (t))
13729 return spec;
13730 }
13731 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13732 {
13733 /* If T is not dependent, just return it. */
13734 if (!uses_template_parms (DECL_TI_ARGS (t))
13735 && !LAMBDA_FUNCTION_P (t))
13736 return t;
13737
13738 /* Calculate the most general template of which R is a
13739 specialization. */
13740 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13741
13742 /* We're substituting a lambda function under tsubst_lambda_expr but not
13743 directly from it; find the matching function we're already inside.
13744 But don't do this if T is a generic lambda with a single level of
13745 template parms, as in that case we're doing a normal instantiation. */
13746 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13747 && (!generic_lambda_fn_p (t)
13748 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13749 return enclosing_instantiation_of (t);
13750
13751 /* Calculate the complete set of arguments used to
13752 specialize R. */
13753 argvec = tsubst_template_args (DECL_TI_ARGS
13754 (DECL_TEMPLATE_RESULT
13755 (DECL_TI_TEMPLATE (t))),
13756 args, complain, in_decl);
13757 if (argvec == error_mark_node)
13758 return error_mark_node;
13759
13760 /* Check to see if we already have this specialization. */
13761 if (!lambda_fntype)
13762 {
13763 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13764 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13765 return spec;
13766 }
13767
13768 /* We can see more levels of arguments than parameters if
13769 there was a specialization of a member template, like
13770 this:
13771
13772 template <class T> struct S { template <class U> void f(); }
13773 template <> template <class U> void S<int>::f(U);
13774
13775 Here, we'll be substituting into the specialization,
13776 because that's where we can find the code we actually
13777 want to generate, but we'll have enough arguments for
13778 the most general template.
13779
13780 We also deal with the peculiar case:
13781
13782 template <class T> struct S {
13783 template <class U> friend void f();
13784 };
13785 template <class U> void f() {}
13786 template S<int>;
13787 template void f<double>();
13788
13789 Here, the ARGS for the instantiation of will be {int,
13790 double}. But, we only need as many ARGS as there are
13791 levels of template parameters in CODE_PATTERN. We are
13792 careful not to get fooled into reducing the ARGS in
13793 situations like:
13794
13795 template <class T> struct S { template <class U> void f(U); }
13796 template <class T> template <> void S<T>::f(int) {}
13797
13798 which we can spot because the pattern will be a
13799 specialization in this case. */
13800 int args_depth = TMPL_ARGS_DEPTH (args);
13801 int parms_depth =
13802 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13803
13804 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13805 args = get_innermost_template_args (args, parms_depth);
13806 }
13807 else
13808 {
13809 /* This special case arises when we have something like this:
13810
13811 template <class T> struct S {
13812 friend void f<int>(int, double);
13813 };
13814
13815 Here, the DECL_TI_TEMPLATE for the friend declaration
13816 will be an IDENTIFIER_NODE. We are being called from
13817 tsubst_friend_function, and we want only to create a
13818 new decl (R) with appropriate types so that we can call
13819 determine_specialization. */
13820 gen_tmpl = NULL_TREE;
13821 argvec = NULL_TREE;
13822 }
13823
13824 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13825 : NULL_TREE);
13826 tree ctx = closure ? closure : DECL_CONTEXT (t);
13827 bool member = ctx && TYPE_P (ctx);
13828
13829 if (member && !closure)
13830 ctx = tsubst_aggr_type (ctx, args,
13831 complain, t, /*entering_scope=*/1);
13832
13833 tree type = (lambda_fntype ? lambda_fntype
13834 : tsubst (TREE_TYPE (t), args,
13835 complain | tf_fndecl_type, in_decl));
13836 if (type == error_mark_node)
13837 return error_mark_node;
13838
13839 /* If we hit excessive deduction depth, the type is bogus even if
13840 it isn't error_mark_node, so don't build a decl. */
13841 if (excessive_deduction_depth)
13842 return error_mark_node;
13843
13844 /* We do NOT check for matching decls pushed separately at this
13845 point, as they may not represent instantiations of this
13846 template, and in any case are considered separate under the
13847 discrete model. */
13848 tree r = copy_decl (t);
13849 DECL_USE_TEMPLATE (r) = 0;
13850 TREE_TYPE (r) = type;
13851 /* Clear out the mangled name and RTL for the instantiation. */
13852 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13853 SET_DECL_RTL (r, NULL);
13854 /* Leave DECL_INITIAL set on deleted instantiations. */
13855 if (!DECL_DELETED_FN (r))
13856 DECL_INITIAL (r) = NULL_TREE;
13857 DECL_CONTEXT (r) = ctx;
13858
13859 /* Handle explicit(dependent-expr). */
13860 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13861 {
13862 tree spec = lookup_explicit_specifier (t);
13863 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13864 /*function_p=*/false,
13865 /*i_c_e_p=*/true);
13866 spec = build_explicit_specifier (spec, complain);
13867 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13868 }
13869
13870 /* OpenMP UDRs have the only argument a reference to the declared
13871 type. We want to diagnose if the declared type is a reference,
13872 which is invalid, but as references to references are usually
13873 quietly merged, diagnose it here. */
13874 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13875 {
13876 tree argtype
13877 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13878 argtype = tsubst (argtype, args, complain, in_decl);
13879 if (TYPE_REF_P (argtype))
13880 error_at (DECL_SOURCE_LOCATION (t),
13881 "reference type %qT in "
13882 "%<#pragma omp declare reduction%>", argtype);
13883 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13884 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13885 argtype);
13886 }
13887
13888 if (member && DECL_CONV_FN_P (r))
13889 /* Type-conversion operator. Reconstruct the name, in
13890 case it's the name of one of the template's parameters. */
13891 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13892
13893 tree parms = DECL_ARGUMENTS (t);
13894 if (closure)
13895 parms = DECL_CHAIN (parms);
13896 parms = tsubst (parms, args, complain, t);
13897 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13898 DECL_CONTEXT (parm) = r;
13899 if (closure)
13900 {
13901 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13902 DECL_NAME (tparm) = closure_identifier;
13903 DECL_CHAIN (tparm) = parms;
13904 parms = tparm;
13905 }
13906 DECL_ARGUMENTS (r) = parms;
13907 DECL_RESULT (r) = NULL_TREE;
13908
13909 maybe_rebuild_function_decl_type (r);
13910
13911 TREE_STATIC (r) = 0;
13912 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13913 DECL_EXTERNAL (r) = 1;
13914 /* If this is an instantiation of a function with internal
13915 linkage, we already know what object file linkage will be
13916 assigned to the instantiation. */
13917 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13918 DECL_DEFER_OUTPUT (r) = 0;
13919 DECL_CHAIN (r) = NULL_TREE;
13920 DECL_PENDING_INLINE_INFO (r) = 0;
13921 DECL_PENDING_INLINE_P (r) = 0;
13922 DECL_SAVED_TREE (r) = NULL_TREE;
13923 DECL_STRUCT_FUNCTION (r) = NULL;
13924 TREE_USED (r) = 0;
13925 /* We'll re-clone as appropriate in instantiate_template. */
13926 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13927
13928 /* If we aren't complaining now, return on error before we register
13929 the specialization so that we'll complain eventually. */
13930 if ((complain & tf_error) == 0
13931 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13932 && !grok_op_properties (r, /*complain=*/false))
13933 return error_mark_node;
13934
13935 /* Associate the constraints directly with the instantiation. We
13936 don't substitute through the constraints; that's only done when
13937 they are checked. */
13938 if (tree ci = get_constraints (t))
13939 /* Unless we're regenerating a lambda, in which case we'll set the
13940 lambda's constraints in tsubst_lambda_expr. */
13941 if (!lambda_fntype)
13942 set_constraints (r, ci);
13943
13944 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13945 SET_DECL_FRIEND_CONTEXT (r,
13946 tsubst (DECL_FRIEND_CONTEXT (t),
13947 args, complain, in_decl));
13948
13949 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13950 this in the special friend case mentioned above where
13951 GEN_TMPL is NULL. */
13952 if (gen_tmpl && !closure)
13953 {
13954 DECL_TEMPLATE_INFO (r)
13955 = build_template_info (gen_tmpl, argvec);
13956 SET_DECL_IMPLICIT_INSTANTIATION (r);
13957
13958 tree new_r
13959 = register_specialization (r, gen_tmpl, argvec, false, hash);
13960 if (new_r != r)
13961 /* We instantiated this while substituting into
13962 the type earlier (template/friend54.C). */
13963 return new_r;
13964
13965 /* We're not supposed to instantiate default arguments
13966 until they are called, for a template. But, for a
13967 declaration like:
13968
13969 template <class T> void f ()
13970 { extern void g(int i = T()); }
13971
13972 we should do the substitution when the template is
13973 instantiated. We handle the member function case in
13974 instantiate_class_template since the default arguments
13975 might refer to other members of the class. */
13976 if (!member
13977 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13978 && !uses_template_parms (argvec))
13979 tsubst_default_arguments (r, complain);
13980 }
13981 else if (DECL_LOCAL_DECL_P (r))
13982 {
13983 if (!cp_unevaluated_operand)
13984 register_local_specialization (r, t);
13985 }
13986 else
13987 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13988
13989 /* Copy the list of befriending classes. */
13990 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13991 *friends;
13992 friends = &TREE_CHAIN (*friends))
13993 {
13994 *friends = copy_node (*friends);
13995 TREE_VALUE (*friends)
13996 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13997 }
13998
13999 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14000 {
14001 maybe_retrofit_in_chrg (r);
14002 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14003 return error_mark_node;
14004 /* If this is an instantiation of a member template, clone it.
14005 If it isn't, that'll be handled by
14006 clone_constructors_and_destructors. */
14007 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14008 clone_cdtor (r, /*update_methods=*/false);
14009 }
14010 else if ((complain & tf_error) != 0
14011 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14012 && !grok_op_properties (r, /*complain=*/true))
14013 return error_mark_node;
14014
14015 /* Possibly limit visibility based on template args. */
14016 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14017 if (DECL_VISIBILITY_SPECIFIED (t))
14018 {
14019 DECL_VISIBILITY_SPECIFIED (r) = 0;
14020 DECL_ATTRIBUTES (r)
14021 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14022 }
14023 determine_visibility (r);
14024 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14025 && !processing_template_decl)
14026 defaulted_late_check (r);
14027
14028 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14029 args, complain, in_decl);
14030 if (flag_openmp)
14031 if (tree attr = lookup_attribute ("omp declare variant base",
14032 DECL_ATTRIBUTES (r)))
14033 omp_declare_variant_finalize (r, attr);
14034
14035 return r;
14036 }
14037
14038 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14039
14040 static tree
14041 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14042 tree lambda_fntype)
14043 {
14044 /* We can get here when processing a member function template,
14045 member class template, or template template parameter. */
14046 tree decl = DECL_TEMPLATE_RESULT (t);
14047 tree in_decl = t;
14048 tree spec;
14049 tree tmpl_args;
14050 tree full_args;
14051 tree r;
14052 hashval_t hash = 0;
14053
14054 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14055 {
14056 /* Template template parameter is treated here. */
14057 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14058 if (new_type == error_mark_node)
14059 r = error_mark_node;
14060 /* If we get a real template back, return it. This can happen in
14061 the context of most_specialized_partial_spec. */
14062 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14063 r = new_type;
14064 else
14065 /* The new TEMPLATE_DECL was built in
14066 reduce_template_parm_level. */
14067 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14068 return r;
14069 }
14070
14071 if (!lambda_fntype)
14072 {
14073 /* We might already have an instance of this template.
14074 The ARGS are for the surrounding class type, so the
14075 full args contain the tsubst'd args for the context,
14076 plus the innermost args from the template decl. */
14077 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14078 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14079 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14080 /* Because this is a template, the arguments will still be
14081 dependent, even after substitution. If
14082 PROCESSING_TEMPLATE_DECL is not set, the dependency
14083 predicates will short-circuit. */
14084 ++processing_template_decl;
14085 full_args = tsubst_template_args (tmpl_args, args,
14086 complain, in_decl);
14087 --processing_template_decl;
14088 if (full_args == error_mark_node)
14089 return error_mark_node;
14090
14091 /* If this is a default template template argument,
14092 tsubst might not have changed anything. */
14093 if (full_args == tmpl_args)
14094 return t;
14095
14096 hash = hash_tmpl_and_args (t, full_args);
14097 spec = retrieve_specialization (t, full_args, hash);
14098 if (spec != NULL_TREE)
14099 {
14100 if (TYPE_P (spec))
14101 /* Type partial instantiations are stored as the type by
14102 lookup_template_class_1, not here as the template. */
14103 spec = CLASSTYPE_TI_TEMPLATE (spec);
14104 return spec;
14105 }
14106 }
14107
14108 /* Make a new template decl. It will be similar to the
14109 original, but will record the current template arguments.
14110 We also create a new function declaration, which is just
14111 like the old one, but points to this new template, rather
14112 than the old one. */
14113 r = copy_decl (t);
14114 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14115 DECL_CHAIN (r) = NULL_TREE;
14116
14117 // Build new template info linking to the original template decl.
14118 if (!lambda_fntype)
14119 {
14120 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14121 SET_DECL_IMPLICIT_INSTANTIATION (r);
14122 }
14123 else
14124 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14125
14126 /* The template parameters for this new template are all the
14127 template parameters for the old template, except the
14128 outermost level of parameters. */
14129 DECL_TEMPLATE_PARMS (r)
14130 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14131 complain);
14132
14133 bool class_p = false;
14134 tree inner = decl;
14135 ++processing_template_decl;
14136 if (TREE_CODE (inner) == FUNCTION_DECL)
14137 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14138 else
14139 {
14140 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14141 {
14142 class_p = true;
14143 inner = TREE_TYPE (inner);
14144 }
14145 inner = tsubst (inner, args, complain, in_decl);
14146 }
14147 --processing_template_decl;
14148 if (inner == error_mark_node)
14149 return error_mark_node;
14150
14151 if (class_p)
14152 {
14153 /* For a partial specialization, we need to keep pointing to
14154 the primary template. */
14155 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14156 CLASSTYPE_TI_TEMPLATE (inner) = r;
14157
14158 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14159 inner = TYPE_MAIN_DECL (inner);
14160 }
14161 else if (lambda_fntype)
14162 {
14163 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14164 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14165 }
14166 else
14167 {
14168 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14169 DECL_TI_TEMPLATE (inner) = r;
14170 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14171 }
14172
14173 DECL_TEMPLATE_RESULT (r) = inner;
14174 TREE_TYPE (r) = TREE_TYPE (inner);
14175 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14176
14177 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14178 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14179
14180 if (PRIMARY_TEMPLATE_P (t))
14181 DECL_PRIMARY_TEMPLATE (r) = r;
14182
14183 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14184 && !lambda_fntype)
14185 /* Record this non-type partial instantiation. */
14186 register_specialization (r, t,
14187 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14188 false, hash);
14189
14190 return r;
14191 }
14192
14193 /* True if FN is the op() for a lambda in an uninstantiated template. */
14194
14195 bool
14196 lambda_fn_in_template_p (tree fn)
14197 {
14198 if (!fn || !LAMBDA_FUNCTION_P (fn))
14199 return false;
14200 tree closure = DECL_CONTEXT (fn);
14201 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14202 }
14203
14204 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14205 which the above is true. */
14206
14207 bool
14208 instantiated_lambda_fn_p (tree fn)
14209 {
14210 if (!fn || !LAMBDA_FUNCTION_P (fn))
14211 return false;
14212 tree closure = DECL_CONTEXT (fn);
14213 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14214 return LAMBDA_EXPR_INSTANTIATED (lam);
14215 }
14216
14217 /* We're instantiating a variable from template function TCTX. Return the
14218 corresponding current enclosing scope. This gets complicated because lambda
14219 functions in templates are regenerated rather than instantiated, but generic
14220 lambda functions are subsequently instantiated. */
14221
14222 static tree
14223 enclosing_instantiation_of (tree otctx)
14224 {
14225 tree tctx = otctx;
14226 tree fn = current_function_decl;
14227 int lambda_count = 0;
14228
14229 for (; tctx && (lambda_fn_in_template_p (tctx)
14230 || instantiated_lambda_fn_p (tctx));
14231 tctx = decl_function_context (tctx))
14232 ++lambda_count;
14233 for (; fn; fn = decl_function_context (fn))
14234 {
14235 tree ofn = fn;
14236 int flambda_count = 0;
14237 for (; fn && instantiated_lambda_fn_p (fn);
14238 fn = decl_function_context (fn))
14239 ++flambda_count;
14240 if ((fn && DECL_TEMPLATE_INFO (fn))
14241 ? most_general_template (fn) != most_general_template (tctx)
14242 : fn != tctx)
14243 continue;
14244 if (flambda_count != lambda_count)
14245 {
14246 gcc_assert (flambda_count > lambda_count);
14247 for (; flambda_count > lambda_count; --flambda_count)
14248 ofn = decl_function_context (ofn);
14249 }
14250 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14251 || DECL_CONV_FN_P (ofn));
14252 return ofn;
14253 }
14254 gcc_unreachable ();
14255 }
14256
14257 /* Substitute the ARGS into the T, which is a _DECL. Return the
14258 result of the substitution. Issue error and warning messages under
14259 control of COMPLAIN. */
14260
14261 static tree
14262 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14263 {
14264 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14265 location_t saved_loc;
14266 tree r = NULL_TREE;
14267 tree in_decl = t;
14268 hashval_t hash = 0;
14269
14270 /* Set the filename and linenumber to improve error-reporting. */
14271 saved_loc = input_location;
14272 input_location = DECL_SOURCE_LOCATION (t);
14273
14274 switch (TREE_CODE (t))
14275 {
14276 case TEMPLATE_DECL:
14277 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14278 break;
14279
14280 case FUNCTION_DECL:
14281 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14282 break;
14283
14284 case PARM_DECL:
14285 {
14286 tree type = NULL_TREE;
14287 int i, len = 1;
14288 tree expanded_types = NULL_TREE;
14289 tree prev_r = NULL_TREE;
14290 tree first_r = NULL_TREE;
14291
14292 if (DECL_PACK_P (t))
14293 {
14294 /* If there is a local specialization that isn't a
14295 parameter pack, it means that we're doing a "simple"
14296 substitution from inside tsubst_pack_expansion. Just
14297 return the local specialization (which will be a single
14298 parm). */
14299 tree spec = retrieve_local_specialization (t);
14300 if (spec
14301 && TREE_CODE (spec) == PARM_DECL
14302 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14303 RETURN (spec);
14304
14305 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14306 the parameters in this function parameter pack. */
14307 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14308 complain, in_decl);
14309 if (TREE_CODE (expanded_types) == TREE_VEC)
14310 {
14311 len = TREE_VEC_LENGTH (expanded_types);
14312
14313 /* Zero-length parameter packs are boring. Just substitute
14314 into the chain. */
14315 if (len == 0 && !cp_unevaluated_operand)
14316 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14317 TREE_CHAIN (t)));
14318 }
14319 else
14320 {
14321 /* All we did was update the type. Make a note of that. */
14322 type = expanded_types;
14323 expanded_types = NULL_TREE;
14324 }
14325 }
14326
14327 /* Loop through all of the parameters we'll build. When T is
14328 a function parameter pack, LEN is the number of expanded
14329 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14330 r = NULL_TREE;
14331 for (i = 0; i < len; ++i)
14332 {
14333 prev_r = r;
14334 r = copy_node (t);
14335 if (DECL_TEMPLATE_PARM_P (t))
14336 SET_DECL_TEMPLATE_PARM_P (r);
14337
14338 if (expanded_types)
14339 /* We're on the Ith parameter of the function parameter
14340 pack. */
14341 {
14342 /* Get the Ith type. */
14343 type = TREE_VEC_ELT (expanded_types, i);
14344
14345 /* Rename the parameter to include the index. */
14346 DECL_NAME (r)
14347 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14348 }
14349 else if (!type)
14350 /* We're dealing with a normal parameter. */
14351 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14352
14353 type = type_decays_to (type);
14354 TREE_TYPE (r) = type;
14355 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14356
14357 if (DECL_INITIAL (r))
14358 {
14359 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14360 DECL_INITIAL (r) = TREE_TYPE (r);
14361 else
14362 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14363 complain, in_decl);
14364 }
14365
14366 DECL_CONTEXT (r) = NULL_TREE;
14367
14368 if (!DECL_TEMPLATE_PARM_P (r))
14369 DECL_ARG_TYPE (r) = type_passed_as (type);
14370
14371 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14372 args, complain, in_decl);
14373
14374 /* Keep track of the first new parameter we
14375 generate. That's what will be returned to the
14376 caller. */
14377 if (!first_r)
14378 first_r = r;
14379
14380 /* Build a proper chain of parameters when substituting
14381 into a function parameter pack. */
14382 if (prev_r)
14383 DECL_CHAIN (prev_r) = r;
14384 }
14385
14386 /* If cp_unevaluated_operand is set, we're just looking for a
14387 single dummy parameter, so don't keep going. */
14388 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14389 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14390 complain, DECL_CHAIN (t));
14391
14392 /* FIRST_R contains the start of the chain we've built. */
14393 r = first_r;
14394 }
14395 break;
14396
14397 case FIELD_DECL:
14398 {
14399 tree type = NULL_TREE;
14400 tree vec = NULL_TREE;
14401 tree expanded_types = NULL_TREE;
14402 int len = 1;
14403
14404 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14405 {
14406 /* This field is a lambda capture pack. Return a TREE_VEC of
14407 the expanded fields to instantiate_class_template_1. */
14408 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14409 complain, in_decl);
14410 if (TREE_CODE (expanded_types) == TREE_VEC)
14411 {
14412 len = TREE_VEC_LENGTH (expanded_types);
14413 vec = make_tree_vec (len);
14414 }
14415 else
14416 {
14417 /* All we did was update the type. Make a note of that. */
14418 type = expanded_types;
14419 expanded_types = NULL_TREE;
14420 }
14421 }
14422
14423 for (int i = 0; i < len; ++i)
14424 {
14425 r = copy_decl (t);
14426 if (expanded_types)
14427 {
14428 type = TREE_VEC_ELT (expanded_types, i);
14429 DECL_NAME (r)
14430 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14431 }
14432 else if (!type)
14433 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14434
14435 if (type == error_mark_node)
14436 RETURN (error_mark_node);
14437 TREE_TYPE (r) = type;
14438 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14439
14440 if (DECL_C_BIT_FIELD (r))
14441 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14442 number of bits. */
14443 DECL_BIT_FIELD_REPRESENTATIVE (r)
14444 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14445 complain, in_decl,
14446 /*integral_constant_expression_p=*/true);
14447 if (DECL_INITIAL (t))
14448 {
14449 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14450 NSDMI in perform_member_init. Still set DECL_INITIAL
14451 so that we know there is one. */
14452 DECL_INITIAL (r) = void_node;
14453 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14454 retrofit_lang_decl (r);
14455 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14456 }
14457 /* We don't have to set DECL_CONTEXT here; it is set by
14458 finish_member_declaration. */
14459 DECL_CHAIN (r) = NULL_TREE;
14460
14461 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14462 args, complain, in_decl);
14463
14464 if (vec)
14465 TREE_VEC_ELT (vec, i) = r;
14466 }
14467
14468 if (vec)
14469 r = vec;
14470 }
14471 break;
14472
14473 case USING_DECL:
14474 /* We reach here only for member using decls. We also need to check
14475 uses_template_parms because DECL_DEPENDENT_P is not set for a
14476 using-declaration that designates a member of the current
14477 instantiation (c++/53549). */
14478 if (DECL_DEPENDENT_P (t)
14479 || uses_template_parms (USING_DECL_SCOPE (t)))
14480 {
14481 tree scope = USING_DECL_SCOPE (t);
14482 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14483 if (PACK_EXPANSION_P (scope))
14484 {
14485 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14486 int len = TREE_VEC_LENGTH (vec);
14487 r = make_tree_vec (len);
14488 for (int i = 0; i < len; ++i)
14489 {
14490 tree escope = TREE_VEC_ELT (vec, i);
14491 tree elt = do_class_using_decl (escope, name);
14492 if (!elt)
14493 {
14494 r = error_mark_node;
14495 break;
14496 }
14497 else
14498 {
14499 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14500 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14501 }
14502 TREE_VEC_ELT (r, i) = elt;
14503 }
14504 }
14505 else
14506 {
14507 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14508 complain, in_decl);
14509 r = do_class_using_decl (inst_scope, name);
14510 if (!r)
14511 r = error_mark_node;
14512 else
14513 {
14514 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14515 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14516 }
14517 }
14518 }
14519 else
14520 {
14521 r = copy_node (t);
14522 DECL_CHAIN (r) = NULL_TREE;
14523 }
14524 break;
14525
14526 case TYPE_DECL:
14527 case VAR_DECL:
14528 {
14529 tree argvec = NULL_TREE;
14530 tree gen_tmpl = NULL_TREE;
14531 tree tmpl = NULL_TREE;
14532 tree type = NULL_TREE;
14533
14534 if (TREE_TYPE (t) == error_mark_node)
14535 RETURN (error_mark_node);
14536
14537 if (TREE_CODE (t) == TYPE_DECL
14538 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14539 {
14540 /* If this is the canonical decl, we don't have to
14541 mess with instantiations, and often we can't (for
14542 typename, template type parms and such). Note that
14543 TYPE_NAME is not correct for the above test if
14544 we've copied the type for a typedef. */
14545 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14546 if (type == error_mark_node)
14547 RETURN (error_mark_node);
14548 r = TYPE_NAME (type);
14549 break;
14550 }
14551
14552 /* Check to see if we already have the specialization we
14553 need. */
14554 tree spec = NULL_TREE;
14555 bool local_p = false;
14556 tree ctx = DECL_CONTEXT (t);
14557 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14558 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14559 {
14560 local_p = false;
14561 if (DECL_CLASS_SCOPE_P (t))
14562 {
14563 ctx = tsubst_aggr_type (ctx, args,
14564 complain,
14565 in_decl, /*entering_scope=*/1);
14566 /* If CTX is unchanged, then T is in fact the
14567 specialization we want. That situation occurs when
14568 referencing a static data member within in its own
14569 class. We can use pointer equality, rather than
14570 same_type_p, because DECL_CONTEXT is always
14571 canonical... */
14572 if (ctx == DECL_CONTEXT (t)
14573 /* ... unless T is a member template; in which
14574 case our caller can be willing to create a
14575 specialization of that template represented
14576 by T. */
14577 && !(DECL_TI_TEMPLATE (t)
14578 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14579 spec = t;
14580 }
14581
14582 if (!spec)
14583 {
14584 tmpl = DECL_TI_TEMPLATE (t);
14585 gen_tmpl = most_general_template (tmpl);
14586 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14587 if (argvec != error_mark_node)
14588 argvec = (coerce_innermost_template_parms
14589 (DECL_TEMPLATE_PARMS (gen_tmpl),
14590 argvec, t, complain,
14591 /*all*/true, /*defarg*/true));
14592 if (argvec == error_mark_node)
14593 RETURN (error_mark_node);
14594 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14595 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14596 }
14597 }
14598 else
14599 {
14600 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14601 /* Subsequent calls to pushdecl will fill this in. */
14602 ctx = NULL_TREE;
14603 /* A local variable. */
14604 local_p = true;
14605 /* Unless this is a reference to a static variable from an
14606 enclosing function, in which case we need to fill it in now. */
14607 if (TREE_STATIC (t))
14608 {
14609 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14610 if (fn != current_function_decl)
14611 ctx = fn;
14612 }
14613 spec = retrieve_local_specialization (t);
14614 }
14615 /* If we already have the specialization we need, there is
14616 nothing more to do. */
14617 if (spec)
14618 {
14619 r = spec;
14620 break;
14621 }
14622
14623 /* Create a new node for the specialization we need. */
14624 if (type == NULL_TREE)
14625 {
14626 if (is_typedef_decl (t))
14627 type = DECL_ORIGINAL_TYPE (t);
14628 else
14629 type = TREE_TYPE (t);
14630 if (VAR_P (t)
14631 && VAR_HAD_UNKNOWN_BOUND (t)
14632 && type != error_mark_node)
14633 type = strip_array_domain (type);
14634 tree sub_args = args;
14635 if (tree auto_node = type_uses_auto (type))
14636 {
14637 /* Mask off any template args past the variable's context so we
14638 don't replace the auto with an unrelated argument. */
14639 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14640 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14641 if (extra > 0)
14642 /* This should never happen with the new lambda instantiation
14643 model, but keep the handling just in case. */
14644 gcc_assert (!CHECKING_P),
14645 sub_args = strip_innermost_template_args (args, extra);
14646 }
14647 type = tsubst (type, sub_args, complain, in_decl);
14648 /* Substituting the type might have recursively instantiated this
14649 same alias (c++/86171). */
14650 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14651 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14652 {
14653 r = spec;
14654 break;
14655 }
14656 }
14657 r = copy_decl (t);
14658 if (VAR_P (r))
14659 {
14660 DECL_INITIALIZED_P (r) = 0;
14661 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14662 if (type == error_mark_node)
14663 RETURN (error_mark_node);
14664 if (TREE_CODE (type) == FUNCTION_TYPE)
14665 {
14666 /* It may seem that this case cannot occur, since:
14667
14668 typedef void f();
14669 void g() { f x; }
14670
14671 declares a function, not a variable. However:
14672
14673 typedef void f();
14674 template <typename T> void g() { T t; }
14675 template void g<f>();
14676
14677 is an attempt to declare a variable with function
14678 type. */
14679 error ("variable %qD has function type",
14680 /* R is not yet sufficiently initialized, so we
14681 just use its name. */
14682 DECL_NAME (r));
14683 RETURN (error_mark_node);
14684 }
14685 type = complete_type (type);
14686 /* Wait until cp_finish_decl to set this again, to handle
14687 circular dependency (template/instantiate6.C). */
14688 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14689 type = check_var_type (DECL_NAME (r), type,
14690 DECL_SOURCE_LOCATION (r));
14691 if (DECL_HAS_VALUE_EXPR_P (t))
14692 {
14693 tree ve = DECL_VALUE_EXPR (t);
14694 /* If the DECL_VALUE_EXPR is converted to the declared type,
14695 preserve the identity so that gimplify_type_sizes works. */
14696 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14697 if (nop)
14698 ve = TREE_OPERAND (ve, 0);
14699 ve = tsubst_expr (ve, args, complain, in_decl,
14700 /*constant_expression_p=*/false);
14701 if (REFERENCE_REF_P (ve))
14702 {
14703 gcc_assert (TYPE_REF_P (type));
14704 ve = TREE_OPERAND (ve, 0);
14705 }
14706 if (nop)
14707 ve = build_nop (type, ve);
14708 else if (DECL_LANG_SPECIFIC (t)
14709 && DECL_OMP_PRIVATIZED_MEMBER (t)
14710 && TREE_CODE (ve) == COMPONENT_REF
14711 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14712 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14713 type = TREE_TYPE (ve);
14714 else
14715 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14716 == TYPE_MAIN_VARIANT (type));
14717 SET_DECL_VALUE_EXPR (r, ve);
14718 }
14719 if (CP_DECL_THREAD_LOCAL_P (r)
14720 && !processing_template_decl)
14721 set_decl_tls_model (r, decl_default_tls_model (r));
14722 }
14723 else if (DECL_SELF_REFERENCE_P (t))
14724 SET_DECL_SELF_REFERENCE_P (r);
14725 TREE_TYPE (r) = type;
14726 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14727 DECL_CONTEXT (r) = ctx;
14728 /* Clear out the mangled name and RTL for the instantiation. */
14729 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14730 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14731 SET_DECL_RTL (r, NULL);
14732 /* The initializer must not be expanded until it is required;
14733 see [temp.inst]. */
14734 DECL_INITIAL (r) = NULL_TREE;
14735 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14736 if (VAR_P (r))
14737 {
14738 if (DECL_LANG_SPECIFIC (r))
14739 SET_DECL_DEPENDENT_INIT_P (r, false);
14740
14741 SET_DECL_MODE (r, VOIDmode);
14742
14743 /* Possibly limit visibility based on template args. */
14744 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14745 if (DECL_VISIBILITY_SPECIFIED (t))
14746 {
14747 DECL_VISIBILITY_SPECIFIED (r) = 0;
14748 DECL_ATTRIBUTES (r)
14749 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14750 }
14751 determine_visibility (r);
14752 }
14753
14754 if (!local_p)
14755 {
14756 /* A static data member declaration is always marked
14757 external when it is declared in-class, even if an
14758 initializer is present. We mimic the non-template
14759 processing here. */
14760 DECL_EXTERNAL (r) = 1;
14761 if (DECL_NAMESPACE_SCOPE_P (t))
14762 DECL_NOT_REALLY_EXTERN (r) = 1;
14763
14764 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14765 SET_DECL_IMPLICIT_INSTANTIATION (r);
14766 if (!error_operand_p (r) || (complain & tf_error))
14767 register_specialization (r, gen_tmpl, argvec, false, hash);
14768 }
14769 else
14770 {
14771 if (DECL_LANG_SPECIFIC (r))
14772 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14773 if (!cp_unevaluated_operand)
14774 register_local_specialization (r, t);
14775 }
14776
14777 DECL_CHAIN (r) = NULL_TREE;
14778
14779 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14780 /*flags=*/0,
14781 args, complain, in_decl);
14782
14783 /* Preserve a typedef that names a type. */
14784 if (is_typedef_decl (r) && type != error_mark_node)
14785 {
14786 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14787 set_underlying_type (r);
14788 if (TYPE_DECL_ALIAS_P (r))
14789 /* An alias template specialization can be dependent
14790 even if its underlying type is not. */
14791 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14792 }
14793
14794 layout_decl (r, 0);
14795 }
14796 break;
14797
14798 default:
14799 gcc_unreachable ();
14800 }
14801 #undef RETURN
14802
14803 out:
14804 /* Restore the file and line information. */
14805 input_location = saved_loc;
14806
14807 return r;
14808 }
14809
14810 /* Substitute into the complete parameter type list PARMS. */
14811
14812 tree
14813 tsubst_function_parms (tree parms,
14814 tree args,
14815 tsubst_flags_t complain,
14816 tree in_decl)
14817 {
14818 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14819 }
14820
14821 /* Substitute into the ARG_TYPES of a function type.
14822 If END is a TREE_CHAIN, leave it and any following types
14823 un-substituted. */
14824
14825 static tree
14826 tsubst_arg_types (tree arg_types,
14827 tree args,
14828 tree end,
14829 tsubst_flags_t complain,
14830 tree in_decl)
14831 {
14832 tree remaining_arg_types;
14833 tree type = NULL_TREE;
14834 int i = 1;
14835 tree expanded_args = NULL_TREE;
14836 tree default_arg;
14837
14838 if (!arg_types || arg_types == void_list_node || arg_types == end)
14839 return arg_types;
14840
14841 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14842 args, end, complain, in_decl);
14843 if (remaining_arg_types == error_mark_node)
14844 return error_mark_node;
14845
14846 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14847 {
14848 /* For a pack expansion, perform substitution on the
14849 entire expression. Later on, we'll handle the arguments
14850 one-by-one. */
14851 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14852 args, complain, in_decl);
14853
14854 if (TREE_CODE (expanded_args) == TREE_VEC)
14855 /* So that we'll spin through the parameters, one by one. */
14856 i = TREE_VEC_LENGTH (expanded_args);
14857 else
14858 {
14859 /* We only partially substituted into the parameter
14860 pack. Our type is TYPE_PACK_EXPANSION. */
14861 type = expanded_args;
14862 expanded_args = NULL_TREE;
14863 }
14864 }
14865
14866 while (i > 0) {
14867 --i;
14868
14869 if (expanded_args)
14870 type = TREE_VEC_ELT (expanded_args, i);
14871 else if (!type)
14872 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14873
14874 if (type == error_mark_node)
14875 return error_mark_node;
14876 if (VOID_TYPE_P (type))
14877 {
14878 if (complain & tf_error)
14879 {
14880 error ("invalid parameter type %qT", type);
14881 if (in_decl)
14882 error ("in declaration %q+D", in_decl);
14883 }
14884 return error_mark_node;
14885 }
14886 /* DR 657. */
14887 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14888 return error_mark_node;
14889
14890 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14891 top-level qualifiers as required. */
14892 type = cv_unqualified (type_decays_to (type));
14893
14894 /* We do not substitute into default arguments here. The standard
14895 mandates that they be instantiated only when needed, which is
14896 done in build_over_call. */
14897 default_arg = TREE_PURPOSE (arg_types);
14898
14899 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14900 since the new op() won't have any associated template arguments for us
14901 to refer to later. */
14902 if (lambda_fn_in_template_p (in_decl))
14903 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14904 false/*fn*/, false/*constexpr*/);
14905
14906 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14907 {
14908 /* We've instantiated a template before its default arguments
14909 have been parsed. This can happen for a nested template
14910 class, and is not an error unless we require the default
14911 argument in a call of this function. */
14912 remaining_arg_types =
14913 tree_cons (default_arg, type, remaining_arg_types);
14914 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14915 remaining_arg_types);
14916 }
14917 else
14918 remaining_arg_types =
14919 hash_tree_cons (default_arg, type, remaining_arg_types);
14920 }
14921
14922 return remaining_arg_types;
14923 }
14924
14925 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14926 *not* handle the exception-specification for FNTYPE, because the
14927 initial substitution of explicitly provided template parameters
14928 during argument deduction forbids substitution into the
14929 exception-specification:
14930
14931 [temp.deduct]
14932
14933 All references in the function type of the function template to the
14934 corresponding template parameters are replaced by the specified tem-
14935 plate argument values. If a substitution in a template parameter or
14936 in the function type of the function template results in an invalid
14937 type, type deduction fails. [Note: The equivalent substitution in
14938 exception specifications is done only when the function is instanti-
14939 ated, at which point a program is ill-formed if the substitution
14940 results in an invalid type.] */
14941
14942 static tree
14943 tsubst_function_type (tree t,
14944 tree args,
14945 tsubst_flags_t complain,
14946 tree in_decl)
14947 {
14948 tree return_type;
14949 tree arg_types = NULL_TREE;
14950
14951 /* The TYPE_CONTEXT is not used for function/method types. */
14952 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14953
14954 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14955 failure. */
14956 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14957
14958 if (late_return_type_p)
14959 {
14960 /* Substitute the argument types. */
14961 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14962 complain, in_decl);
14963 if (arg_types == error_mark_node)
14964 return error_mark_node;
14965
14966 tree save_ccp = current_class_ptr;
14967 tree save_ccr = current_class_ref;
14968 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14969 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14970 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14971 if (do_inject)
14972 {
14973 /* DR 1207: 'this' is in scope in the trailing return type. */
14974 inject_this_parameter (this_type, cp_type_quals (this_type));
14975 }
14976
14977 /* Substitute the return type. */
14978 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14979
14980 if (do_inject)
14981 {
14982 current_class_ptr = save_ccp;
14983 current_class_ref = save_ccr;
14984 }
14985 }
14986 else
14987 /* Substitute the return type. */
14988 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14989
14990 if (return_type == error_mark_node)
14991 return error_mark_node;
14992 /* DR 486 clarifies that creation of a function type with an
14993 invalid return type is a deduction failure. */
14994 if (TREE_CODE (return_type) == ARRAY_TYPE
14995 || TREE_CODE (return_type) == FUNCTION_TYPE)
14996 {
14997 if (complain & tf_error)
14998 {
14999 if (TREE_CODE (return_type) == ARRAY_TYPE)
15000 error ("function returning an array");
15001 else
15002 error ("function returning a function");
15003 }
15004 return error_mark_node;
15005 }
15006 /* And DR 657. */
15007 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
15008 return error_mark_node;
15009
15010 if (!late_return_type_p)
15011 {
15012 /* Substitute the argument types. */
15013 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15014 complain, in_decl);
15015 if (arg_types == error_mark_node)
15016 return error_mark_node;
15017 }
15018
15019 /* Construct a new type node and return it. */
15020 return rebuild_function_or_method_type (t, return_type, arg_types,
15021 /*raises=*/NULL_TREE, complain);
15022 }
15023
15024 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15025 ARGS into that specification, and return the substituted
15026 specification. If there is no specification, return NULL_TREE. */
15027
15028 static tree
15029 tsubst_exception_specification (tree fntype,
15030 tree args,
15031 tsubst_flags_t complain,
15032 tree in_decl,
15033 bool defer_ok)
15034 {
15035 tree specs;
15036 tree new_specs;
15037
15038 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15039 new_specs = NULL_TREE;
15040 if (specs && TREE_PURPOSE (specs))
15041 {
15042 /* A noexcept-specifier. */
15043 tree expr = TREE_PURPOSE (specs);
15044 if (TREE_CODE (expr) == INTEGER_CST)
15045 new_specs = expr;
15046 else if (defer_ok)
15047 {
15048 /* Defer instantiation of noexcept-specifiers to avoid
15049 excessive instantiations (c++/49107). */
15050 new_specs = make_node (DEFERRED_NOEXCEPT);
15051 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15052 {
15053 /* We already partially instantiated this member template,
15054 so combine the new args with the old. */
15055 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15056 = DEFERRED_NOEXCEPT_PATTERN (expr);
15057 DEFERRED_NOEXCEPT_ARGS (new_specs)
15058 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15059 }
15060 else
15061 {
15062 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15063 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15064 }
15065 }
15066 else
15067 {
15068 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15069 {
15070 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15071 args);
15072 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15073 }
15074 new_specs = tsubst_copy_and_build
15075 (expr, args, complain, in_decl, /*function_p=*/false,
15076 /*integral_constant_expression_p=*/true);
15077 }
15078 new_specs = build_noexcept_spec (new_specs, complain);
15079 }
15080 else if (specs)
15081 {
15082 if (! TREE_VALUE (specs))
15083 new_specs = specs;
15084 else
15085 while (specs)
15086 {
15087 tree spec;
15088 int i, len = 1;
15089 tree expanded_specs = NULL_TREE;
15090
15091 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15092 {
15093 /* Expand the pack expansion type. */
15094 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15095 args, complain,
15096 in_decl);
15097
15098 if (expanded_specs == error_mark_node)
15099 return error_mark_node;
15100 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15101 len = TREE_VEC_LENGTH (expanded_specs);
15102 else
15103 {
15104 /* We're substituting into a member template, so
15105 we got a TYPE_PACK_EXPANSION back. Add that
15106 expansion and move on. */
15107 gcc_assert (TREE_CODE (expanded_specs)
15108 == TYPE_PACK_EXPANSION);
15109 new_specs = add_exception_specifier (new_specs,
15110 expanded_specs,
15111 complain);
15112 specs = TREE_CHAIN (specs);
15113 continue;
15114 }
15115 }
15116
15117 for (i = 0; i < len; ++i)
15118 {
15119 if (expanded_specs)
15120 spec = TREE_VEC_ELT (expanded_specs, i);
15121 else
15122 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15123 if (spec == error_mark_node)
15124 return spec;
15125 new_specs = add_exception_specifier (new_specs, spec,
15126 complain);
15127 }
15128
15129 specs = TREE_CHAIN (specs);
15130 }
15131 }
15132 return new_specs;
15133 }
15134
15135 /* Substitute through a TREE_LIST of types or expressions, handling pack
15136 expansions. */
15137
15138 tree
15139 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15140 {
15141 if (t == void_list_node)
15142 return t;
15143
15144 tree purpose = TREE_PURPOSE (t);
15145 tree purposevec = NULL_TREE;
15146 if (!purpose)
15147 ;
15148 else if (PACK_EXPANSION_P (purpose))
15149 {
15150 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15151 if (TREE_CODE (purpose) == TREE_VEC)
15152 purposevec = purpose;
15153 }
15154 else if (TYPE_P (purpose))
15155 purpose = tsubst (purpose, args, complain, in_decl);
15156 else
15157 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15158 if (purpose == error_mark_node || purposevec == error_mark_node)
15159 return error_mark_node;
15160
15161 tree value = TREE_VALUE (t);
15162 tree valuevec = NULL_TREE;
15163 if (!value)
15164 ;
15165 else if (PACK_EXPANSION_P (value))
15166 {
15167 value = tsubst_pack_expansion (value, args, complain, in_decl);
15168 if (TREE_CODE (value) == TREE_VEC)
15169 valuevec = value;
15170 }
15171 else if (TYPE_P (value))
15172 value = tsubst (value, args, complain, in_decl);
15173 else
15174 value = tsubst_copy_and_build (value, args, complain, in_decl);
15175 if (value == error_mark_node || valuevec == error_mark_node)
15176 return error_mark_node;
15177
15178 tree chain = TREE_CHAIN (t);
15179 if (!chain)
15180 ;
15181 else if (TREE_CODE (chain) == TREE_LIST)
15182 chain = tsubst_tree_list (chain, args, complain, in_decl);
15183 else if (TYPE_P (chain))
15184 chain = tsubst (chain, args, complain, in_decl);
15185 else
15186 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15187 if (chain == error_mark_node)
15188 return error_mark_node;
15189
15190 if (purpose == TREE_PURPOSE (t)
15191 && value == TREE_VALUE (t)
15192 && chain == TREE_CHAIN (t))
15193 return t;
15194
15195 int len;
15196 /* Determine the number of arguments. */
15197 if (purposevec)
15198 {
15199 len = TREE_VEC_LENGTH (purposevec);
15200 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15201 }
15202 else if (valuevec)
15203 len = TREE_VEC_LENGTH (valuevec);
15204 else
15205 len = 1;
15206
15207 for (int i = len; i-- > 0; )
15208 {
15209 if (purposevec)
15210 purpose = TREE_VEC_ELT (purposevec, i);
15211 if (valuevec)
15212 value = TREE_VEC_ELT (valuevec, i);
15213
15214 if (value && TYPE_P (value))
15215 chain = hash_tree_cons (purpose, value, chain);
15216 else
15217 chain = tree_cons (purpose, value, chain);
15218 }
15219
15220 return chain;
15221 }
15222
15223 /* Take the tree structure T and replace template parameters used
15224 therein with the argument vector ARGS. IN_DECL is an associated
15225 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15226 Issue error and warning messages under control of COMPLAIN. Note
15227 that we must be relatively non-tolerant of extensions here, in
15228 order to preserve conformance; if we allow substitutions that
15229 should not be allowed, we may allow argument deductions that should
15230 not succeed, and therefore report ambiguous overload situations
15231 where there are none. In theory, we could allow the substitution,
15232 but indicate that it should have failed, and allow our caller to
15233 make sure that the right thing happens, but we don't try to do this
15234 yet.
15235
15236 This function is used for dealing with types, decls and the like;
15237 for expressions, use tsubst_expr or tsubst_copy. */
15238
15239 tree
15240 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15241 {
15242 enum tree_code code;
15243 tree type, r = NULL_TREE;
15244
15245 if (t == NULL_TREE || t == error_mark_node
15246 || t == integer_type_node
15247 || t == void_type_node
15248 || t == char_type_node
15249 || t == unknown_type_node
15250 || TREE_CODE (t) == NAMESPACE_DECL
15251 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15252 return t;
15253
15254 if (DECL_P (t))
15255 return tsubst_decl (t, args, complain);
15256
15257 if (args == NULL_TREE)
15258 return t;
15259
15260 code = TREE_CODE (t);
15261
15262 if (code == IDENTIFIER_NODE)
15263 type = IDENTIFIER_TYPE_VALUE (t);
15264 else
15265 type = TREE_TYPE (t);
15266
15267 gcc_assert (type != unknown_type_node);
15268
15269 /* Reuse typedefs. We need to do this to handle dependent attributes,
15270 such as attribute aligned. */
15271 if (TYPE_P (t)
15272 && typedef_variant_p (t))
15273 {
15274 tree decl = TYPE_NAME (t);
15275
15276 if (alias_template_specialization_p (t, nt_opaque))
15277 {
15278 /* DECL represents an alias template and we want to
15279 instantiate it. */
15280 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15281 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15282 r = instantiate_alias_template (tmpl, gen_args, complain);
15283 }
15284 else if (DECL_CLASS_SCOPE_P (decl)
15285 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15286 && uses_template_parms (DECL_CONTEXT (decl)))
15287 {
15288 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15289 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15290 r = retrieve_specialization (tmpl, gen_args, 0);
15291 }
15292 else if (DECL_FUNCTION_SCOPE_P (decl)
15293 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15294 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15295 r = retrieve_local_specialization (decl);
15296 else
15297 /* The typedef is from a non-template context. */
15298 return t;
15299
15300 if (r)
15301 {
15302 r = TREE_TYPE (r);
15303 r = cp_build_qualified_type_real
15304 (r, cp_type_quals (t) | cp_type_quals (r),
15305 complain | tf_ignore_bad_quals);
15306 return r;
15307 }
15308 else
15309 {
15310 /* We don't have an instantiation yet, so drop the typedef. */
15311 int quals = cp_type_quals (t);
15312 t = DECL_ORIGINAL_TYPE (decl);
15313 t = cp_build_qualified_type_real (t, quals,
15314 complain | tf_ignore_bad_quals);
15315 }
15316 }
15317
15318 bool fndecl_type = (complain & tf_fndecl_type);
15319 complain &= ~tf_fndecl_type;
15320
15321 if (type
15322 && code != TYPENAME_TYPE
15323 && code != TEMPLATE_TYPE_PARM
15324 && code != TEMPLATE_PARM_INDEX
15325 && code != IDENTIFIER_NODE
15326 && code != FUNCTION_TYPE
15327 && code != METHOD_TYPE)
15328 type = tsubst (type, args, complain, in_decl);
15329 if (type == error_mark_node)
15330 return error_mark_node;
15331
15332 switch (code)
15333 {
15334 case RECORD_TYPE:
15335 case UNION_TYPE:
15336 case ENUMERAL_TYPE:
15337 return tsubst_aggr_type (t, args, complain, in_decl,
15338 /*entering_scope=*/0);
15339
15340 case ERROR_MARK:
15341 case IDENTIFIER_NODE:
15342 case VOID_TYPE:
15343 case REAL_TYPE:
15344 case COMPLEX_TYPE:
15345 case VECTOR_TYPE:
15346 case BOOLEAN_TYPE:
15347 case NULLPTR_TYPE:
15348 case LANG_TYPE:
15349 return t;
15350
15351 case INTEGER_TYPE:
15352 if (t == integer_type_node)
15353 return t;
15354
15355 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15356 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15357 return t;
15358
15359 {
15360 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15361
15362 max = tsubst_expr (omax, args, complain, in_decl,
15363 /*integral_constant_expression_p=*/false);
15364
15365 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15366 needed. */
15367 if (TREE_CODE (max) == NOP_EXPR
15368 && TREE_SIDE_EFFECTS (omax)
15369 && !TREE_TYPE (max))
15370 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15371
15372 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15373 with TREE_SIDE_EFFECTS that indicates this is not an integral
15374 constant expression. */
15375 if (processing_template_decl
15376 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15377 {
15378 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15379 TREE_SIDE_EFFECTS (max) = 1;
15380 }
15381
15382 return compute_array_index_type (NULL_TREE, max, complain);
15383 }
15384
15385 case TEMPLATE_TYPE_PARM:
15386 case TEMPLATE_TEMPLATE_PARM:
15387 case BOUND_TEMPLATE_TEMPLATE_PARM:
15388 case TEMPLATE_PARM_INDEX:
15389 {
15390 int idx;
15391 int level;
15392 int levels;
15393 tree arg = NULL_TREE;
15394
15395 r = NULL_TREE;
15396
15397 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15398 template_parm_level_and_index (t, &level, &idx);
15399
15400 levels = TMPL_ARGS_DEPTH (args);
15401 if (level <= levels
15402 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15403 {
15404 arg = TMPL_ARG (args, level, idx);
15405
15406 /* See through ARGUMENT_PACK_SELECT arguments. */
15407 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15408 arg = argument_pack_select_arg (arg);
15409 }
15410
15411 if (arg == error_mark_node)
15412 return error_mark_node;
15413 else if (arg != NULL_TREE)
15414 {
15415 if (ARGUMENT_PACK_P (arg))
15416 /* If ARG is an argument pack, we don't actually want to
15417 perform a substitution here, because substitutions
15418 for argument packs are only done
15419 element-by-element. We can get to this point when
15420 substituting the type of a non-type template
15421 parameter pack, when that type actually contains
15422 template parameter packs from an outer template, e.g.,
15423
15424 template<typename... Types> struct A {
15425 template<Types... Values> struct B { };
15426 }; */
15427 return t;
15428
15429 if (code == TEMPLATE_TYPE_PARM)
15430 {
15431 int quals;
15432
15433 /* When building concept checks for the purpose of
15434 deducing placeholders, we can end up with wildcards
15435 where types are expected. Adjust this to the deduced
15436 value. */
15437 if (TREE_CODE (arg) == WILDCARD_DECL)
15438 arg = TREE_TYPE (TREE_TYPE (arg));
15439
15440 gcc_assert (TYPE_P (arg));
15441
15442 quals = cp_type_quals (arg) | cp_type_quals (t);
15443
15444 return cp_build_qualified_type_real
15445 (arg, quals, complain | tf_ignore_bad_quals);
15446 }
15447 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15448 {
15449 /* We are processing a type constructed from a
15450 template template parameter. */
15451 tree argvec = tsubst (TYPE_TI_ARGS (t),
15452 args, complain, in_decl);
15453 if (argvec == error_mark_node)
15454 return error_mark_node;
15455
15456 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15457 || TREE_CODE (arg) == TEMPLATE_DECL
15458 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15459
15460 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15461 /* Consider this code:
15462
15463 template <template <class> class Template>
15464 struct Internal {
15465 template <class Arg> using Bind = Template<Arg>;
15466 };
15467
15468 template <template <class> class Template, class Arg>
15469 using Instantiate = Template<Arg>; //#0
15470
15471 template <template <class> class Template,
15472 class Argument>
15473 using Bind =
15474 Instantiate<Internal<Template>::template Bind,
15475 Argument>; //#1
15476
15477 When #1 is parsed, the
15478 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15479 parameter `Template' in #0 matches the
15480 UNBOUND_CLASS_TEMPLATE representing the argument
15481 `Internal<Template>::template Bind'; We then want
15482 to assemble the type `Bind<Argument>' that can't
15483 be fully created right now, because
15484 `Internal<Template>' not being complete, the Bind
15485 template cannot be looked up in that context. So
15486 we need to "store" `Bind<Argument>' for later
15487 when the context of Bind becomes complete. Let's
15488 store that in a TYPENAME_TYPE. */
15489 return make_typename_type (TYPE_CONTEXT (arg),
15490 build_nt (TEMPLATE_ID_EXPR,
15491 TYPE_IDENTIFIER (arg),
15492 argvec),
15493 typename_type,
15494 complain);
15495
15496 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15497 are resolving nested-types in the signature of a
15498 member function templates. Otherwise ARG is a
15499 TEMPLATE_DECL and is the real template to be
15500 instantiated. */
15501 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15502 arg = TYPE_NAME (arg);
15503
15504 r = lookup_template_class (arg,
15505 argvec, in_decl,
15506 DECL_CONTEXT (arg),
15507 /*entering_scope=*/0,
15508 complain);
15509 return cp_build_qualified_type_real
15510 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15511 }
15512 else if (code == TEMPLATE_TEMPLATE_PARM)
15513 return arg;
15514 else
15515 /* TEMPLATE_PARM_INDEX. */
15516 return convert_from_reference (unshare_expr (arg));
15517 }
15518
15519 if (level == 1)
15520 /* This can happen during the attempted tsubst'ing in
15521 unify. This means that we don't yet have any information
15522 about the template parameter in question. */
15523 return t;
15524
15525 /* Early in template argument deduction substitution, we don't
15526 want to reduce the level of 'auto', or it will be confused
15527 with a normal template parm in subsequent deduction.
15528 Similarly, don't reduce the level of template parameters to
15529 avoid mismatches when deducing their types. */
15530 if (complain & tf_partial)
15531 return t;
15532
15533 /* If we get here, we must have been looking at a parm for a
15534 more deeply nested template. Make a new version of this
15535 template parameter, but with a lower level. */
15536 switch (code)
15537 {
15538 case TEMPLATE_TYPE_PARM:
15539 case TEMPLATE_TEMPLATE_PARM:
15540 case BOUND_TEMPLATE_TEMPLATE_PARM:
15541 if (cp_type_quals (t))
15542 {
15543 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15544 r = cp_build_qualified_type_real
15545 (r, cp_type_quals (t),
15546 complain | (code == TEMPLATE_TYPE_PARM
15547 ? tf_ignore_bad_quals : 0));
15548 }
15549 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15550 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15551 && (r = (TEMPLATE_PARM_DESCENDANTS
15552 (TEMPLATE_TYPE_PARM_INDEX (t))))
15553 && (r = TREE_TYPE (r))
15554 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15555 /* Break infinite recursion when substituting the constraints
15556 of a constrained placeholder. */;
15557 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15558 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15559 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15560 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15561 r = TEMPLATE_PARM_DESCENDANTS (arg))
15562 && (TEMPLATE_PARM_LEVEL (r)
15563 == TEMPLATE_PARM_LEVEL (arg) - levels))
15564 /* Cache the simple case of lowering a type parameter. */
15565 r = TREE_TYPE (r);
15566 else
15567 {
15568 r = copy_type (t);
15569 TEMPLATE_TYPE_PARM_INDEX (r)
15570 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15571 r, levels, args, complain);
15572 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15573 TYPE_MAIN_VARIANT (r) = r;
15574 TYPE_POINTER_TO (r) = NULL_TREE;
15575 TYPE_REFERENCE_TO (r) = NULL_TREE;
15576
15577 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15578 {
15579 /* Propagate constraints on placeholders since they are
15580 only instantiated during satisfaction. */
15581 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15582 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15583 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15584 {
15585 pl = tsubst_copy (pl, args, complain, in_decl);
15586 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15587 }
15588 }
15589
15590 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15591 /* We have reduced the level of the template
15592 template parameter, but not the levels of its
15593 template parameters, so canonical_type_parameter
15594 will not be able to find the canonical template
15595 template parameter for this level. Thus, we
15596 require structural equality checking to compare
15597 TEMPLATE_TEMPLATE_PARMs. */
15598 SET_TYPE_STRUCTURAL_EQUALITY (r);
15599 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15600 SET_TYPE_STRUCTURAL_EQUALITY (r);
15601 else
15602 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15603
15604 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15605 {
15606 tree tinfo = TYPE_TEMPLATE_INFO (t);
15607 /* We might need to substitute into the types of non-type
15608 template parameters. */
15609 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15610 complain, in_decl);
15611 if (tmpl == error_mark_node)
15612 return error_mark_node;
15613 tree argvec = tsubst (TI_ARGS (tinfo), args,
15614 complain, in_decl);
15615 if (argvec == error_mark_node)
15616 return error_mark_node;
15617
15618 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15619 = build_template_info (tmpl, argvec);
15620 }
15621 }
15622 break;
15623
15624 case TEMPLATE_PARM_INDEX:
15625 /* OK, now substitute the type of the non-type parameter. We
15626 couldn't do it earlier because it might be an auto parameter,
15627 and we wouldn't need to if we had an argument. */
15628 type = tsubst (type, args, complain, in_decl);
15629 if (type == error_mark_node)
15630 return error_mark_node;
15631 r = reduce_template_parm_level (t, type, levels, args, complain);
15632 break;
15633
15634 default:
15635 gcc_unreachable ();
15636 }
15637
15638 return r;
15639 }
15640
15641 case TREE_LIST:
15642 return tsubst_tree_list (t, args, complain, in_decl);
15643
15644 case TREE_BINFO:
15645 /* We should never be tsubsting a binfo. */
15646 gcc_unreachable ();
15647
15648 case TREE_VEC:
15649 /* A vector of template arguments. */
15650 gcc_assert (!type);
15651 return tsubst_template_args (t, args, complain, in_decl);
15652
15653 case POINTER_TYPE:
15654 case REFERENCE_TYPE:
15655 {
15656 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15657 return t;
15658
15659 /* [temp.deduct]
15660
15661 Type deduction may fail for any of the following
15662 reasons:
15663
15664 -- Attempting to create a pointer to reference type.
15665 -- Attempting to create a reference to a reference type or
15666 a reference to void.
15667
15668 Core issue 106 says that creating a reference to a reference
15669 during instantiation is no longer a cause for failure. We
15670 only enforce this check in strict C++98 mode. */
15671 if ((TYPE_REF_P (type)
15672 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15673 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15674 {
15675 static location_t last_loc;
15676
15677 /* We keep track of the last time we issued this error
15678 message to avoid spewing a ton of messages during a
15679 single bad template instantiation. */
15680 if (complain & tf_error
15681 && last_loc != input_location)
15682 {
15683 if (VOID_TYPE_P (type))
15684 error ("forming reference to void");
15685 else if (code == POINTER_TYPE)
15686 error ("forming pointer to reference type %qT", type);
15687 else
15688 error ("forming reference to reference type %qT", type);
15689 last_loc = input_location;
15690 }
15691
15692 return error_mark_node;
15693 }
15694 else if (TREE_CODE (type) == FUNCTION_TYPE
15695 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15696 || type_memfn_rqual (type) != REF_QUAL_NONE))
15697 {
15698 if (complain & tf_error)
15699 {
15700 if (code == POINTER_TYPE)
15701 error ("forming pointer to qualified function type %qT",
15702 type);
15703 else
15704 error ("forming reference to qualified function type %qT",
15705 type);
15706 }
15707 return error_mark_node;
15708 }
15709 else if (code == POINTER_TYPE)
15710 {
15711 r = build_pointer_type (type);
15712 if (TREE_CODE (type) == METHOD_TYPE)
15713 r = build_ptrmemfunc_type (r);
15714 }
15715 else if (TYPE_REF_P (type))
15716 /* In C++0x, during template argument substitution, when there is an
15717 attempt to create a reference to a reference type, reference
15718 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15719
15720 "If a template-argument for a template-parameter T names a type
15721 that is a reference to a type A, an attempt to create the type
15722 'lvalue reference to cv T' creates the type 'lvalue reference to
15723 A,' while an attempt to create the type type rvalue reference to
15724 cv T' creates the type T"
15725 */
15726 r = cp_build_reference_type
15727 (TREE_TYPE (type),
15728 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15729 else
15730 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15731 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15732
15733 if (r != error_mark_node)
15734 /* Will this ever be needed for TYPE_..._TO values? */
15735 layout_type (r);
15736
15737 return r;
15738 }
15739 case OFFSET_TYPE:
15740 {
15741 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15742 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15743 {
15744 /* [temp.deduct]
15745
15746 Type deduction may fail for any of the following
15747 reasons:
15748
15749 -- Attempting to create "pointer to member of T" when T
15750 is not a class type. */
15751 if (complain & tf_error)
15752 error ("creating pointer to member of non-class type %qT", r);
15753 return error_mark_node;
15754 }
15755 if (TYPE_REF_P (type))
15756 {
15757 if (complain & tf_error)
15758 error ("creating pointer to member reference type %qT", type);
15759 return error_mark_node;
15760 }
15761 if (VOID_TYPE_P (type))
15762 {
15763 if (complain & tf_error)
15764 error ("creating pointer to member of type void");
15765 return error_mark_node;
15766 }
15767 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15768 if (TREE_CODE (type) == FUNCTION_TYPE)
15769 {
15770 /* The type of the implicit object parameter gets its
15771 cv-qualifiers from the FUNCTION_TYPE. */
15772 tree memptr;
15773 tree method_type
15774 = build_memfn_type (type, r, type_memfn_quals (type),
15775 type_memfn_rqual (type));
15776 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15777 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15778 complain);
15779 }
15780 else
15781 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15782 cp_type_quals (t),
15783 complain);
15784 }
15785 case FUNCTION_TYPE:
15786 case METHOD_TYPE:
15787 {
15788 tree fntype;
15789 tree specs;
15790 fntype = tsubst_function_type (t, args, complain, in_decl);
15791 if (fntype == error_mark_node)
15792 return error_mark_node;
15793
15794 /* Substitute the exception specification. */
15795 specs = tsubst_exception_specification (t, args, complain, in_decl,
15796 /*defer_ok*/fndecl_type);
15797 if (specs == error_mark_node)
15798 return error_mark_node;
15799 if (specs)
15800 fntype = build_exception_variant (fntype, specs);
15801 return fntype;
15802 }
15803 case ARRAY_TYPE:
15804 {
15805 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15806 if (domain == error_mark_node)
15807 return error_mark_node;
15808
15809 /* As an optimization, we avoid regenerating the array type if
15810 it will obviously be the same as T. */
15811 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15812 return t;
15813
15814 /* These checks should match the ones in create_array_type_for_decl.
15815
15816 [temp.deduct]
15817
15818 The deduction may fail for any of the following reasons:
15819
15820 -- Attempting to create an array with an element type that
15821 is void, a function type, or a reference type, or [DR337]
15822 an abstract class type. */
15823 if (VOID_TYPE_P (type)
15824 || TREE_CODE (type) == FUNCTION_TYPE
15825 || (TREE_CODE (type) == ARRAY_TYPE
15826 && TYPE_DOMAIN (type) == NULL_TREE)
15827 || TYPE_REF_P (type))
15828 {
15829 if (complain & tf_error)
15830 error ("creating array of %qT", type);
15831 return error_mark_node;
15832 }
15833
15834 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15835 return error_mark_node;
15836
15837 r = build_cplus_array_type (type, domain);
15838
15839 if (!valid_array_size_p (input_location, r, in_decl,
15840 (complain & tf_error)))
15841 return error_mark_node;
15842
15843 if (TYPE_USER_ALIGN (t))
15844 {
15845 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15846 TYPE_USER_ALIGN (r) = 1;
15847 }
15848
15849 return r;
15850 }
15851
15852 case TYPENAME_TYPE:
15853 {
15854 tree ctx = TYPE_CONTEXT (t);
15855 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15856 {
15857 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15858 if (ctx == error_mark_node
15859 || TREE_VEC_LENGTH (ctx) > 1)
15860 return error_mark_node;
15861 if (TREE_VEC_LENGTH (ctx) == 0)
15862 {
15863 if (complain & tf_error)
15864 error ("%qD is instantiated for an empty pack",
15865 TYPENAME_TYPE_FULLNAME (t));
15866 return error_mark_node;
15867 }
15868 ctx = TREE_VEC_ELT (ctx, 0);
15869 }
15870 else
15871 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15872 /*entering_scope=*/1);
15873 if (ctx == error_mark_node)
15874 return error_mark_node;
15875
15876 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15877 complain, in_decl);
15878 if (f == error_mark_node)
15879 return error_mark_node;
15880
15881 if (!MAYBE_CLASS_TYPE_P (ctx))
15882 {
15883 if (complain & tf_error)
15884 error ("%qT is not a class, struct, or union type", ctx);
15885 return error_mark_node;
15886 }
15887 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15888 {
15889 /* Normally, make_typename_type does not require that the CTX
15890 have complete type in order to allow things like:
15891
15892 template <class T> struct S { typename S<T>::X Y; };
15893
15894 But, such constructs have already been resolved by this
15895 point, so here CTX really should have complete type, unless
15896 it's a partial instantiation. */
15897 ctx = complete_type (ctx);
15898 if (!COMPLETE_TYPE_P (ctx))
15899 {
15900 if (complain & tf_error)
15901 cxx_incomplete_type_error (NULL_TREE, ctx);
15902 return error_mark_node;
15903 }
15904 }
15905
15906 f = make_typename_type (ctx, f, typename_type,
15907 complain | tf_keep_type_decl);
15908 if (f == error_mark_node)
15909 return f;
15910 if (TREE_CODE (f) == TYPE_DECL)
15911 {
15912 complain |= tf_ignore_bad_quals;
15913 f = TREE_TYPE (f);
15914 }
15915
15916 if (TREE_CODE (f) != TYPENAME_TYPE)
15917 {
15918 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15919 {
15920 if (complain & tf_error)
15921 error ("%qT resolves to %qT, which is not an enumeration type",
15922 t, f);
15923 else
15924 return error_mark_node;
15925 }
15926 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15927 {
15928 if (complain & tf_error)
15929 error ("%qT resolves to %qT, which is not a class type",
15930 t, f);
15931 else
15932 return error_mark_node;
15933 }
15934 }
15935
15936 return cp_build_qualified_type_real
15937 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15938 }
15939
15940 case UNBOUND_CLASS_TEMPLATE:
15941 {
15942 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15943 in_decl, /*entering_scope=*/1);
15944 tree name = TYPE_IDENTIFIER (t);
15945 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15946
15947 if (ctx == error_mark_node || name == error_mark_node)
15948 return error_mark_node;
15949
15950 if (parm_list)
15951 parm_list = tsubst_template_parms (parm_list, args, complain);
15952 return make_unbound_class_template (ctx, name, parm_list, complain);
15953 }
15954
15955 case TYPEOF_TYPE:
15956 {
15957 tree type;
15958
15959 ++cp_unevaluated_operand;
15960 ++c_inhibit_evaluation_warnings;
15961
15962 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15963 complain, in_decl,
15964 /*integral_constant_expression_p=*/false);
15965
15966 --cp_unevaluated_operand;
15967 --c_inhibit_evaluation_warnings;
15968
15969 type = finish_typeof (type);
15970 return cp_build_qualified_type_real (type,
15971 cp_type_quals (t)
15972 | cp_type_quals (type),
15973 complain);
15974 }
15975
15976 case DECLTYPE_TYPE:
15977 {
15978 tree type;
15979
15980 ++cp_unevaluated_operand;
15981 ++c_inhibit_evaluation_warnings;
15982
15983 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15984 complain|tf_decltype, in_decl,
15985 /*function_p*/false,
15986 /*integral_constant_expression*/false);
15987
15988 --cp_unevaluated_operand;
15989 --c_inhibit_evaluation_warnings;
15990
15991 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15992 type = lambda_capture_field_type (type,
15993 false /*explicit_init*/,
15994 DECLTYPE_FOR_REF_CAPTURE (t));
15995 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15996 type = lambda_proxy_type (type);
15997 else
15998 {
15999 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16000 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16001 && EXPR_P (type))
16002 /* In a template ~id could be either a complement expression
16003 or an unqualified-id naming a destructor; if instantiating
16004 it produces an expression, it's not an id-expression or
16005 member access. */
16006 id = false;
16007 type = finish_decltype_type (type, id, complain);
16008 }
16009 return cp_build_qualified_type_real (type,
16010 cp_type_quals (t)
16011 | cp_type_quals (type),
16012 complain | tf_ignore_bad_quals);
16013 }
16014
16015 case UNDERLYING_TYPE:
16016 {
16017 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16018 complain, in_decl);
16019 return finish_underlying_type (type);
16020 }
16021
16022 case TYPE_ARGUMENT_PACK:
16023 case NONTYPE_ARGUMENT_PACK:
16024 {
16025 tree r;
16026
16027 if (code == NONTYPE_ARGUMENT_PACK)
16028 r = make_node (code);
16029 else
16030 r = cxx_make_type (code);
16031
16032 tree pack_args = ARGUMENT_PACK_ARGS (t);
16033 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
16034 SET_ARGUMENT_PACK_ARGS (r, pack_args);
16035
16036 return r;
16037 }
16038
16039 case VOID_CST:
16040 case INTEGER_CST:
16041 case REAL_CST:
16042 case STRING_CST:
16043 case PLUS_EXPR:
16044 case MINUS_EXPR:
16045 case NEGATE_EXPR:
16046 case NOP_EXPR:
16047 case INDIRECT_REF:
16048 case ADDR_EXPR:
16049 case CALL_EXPR:
16050 case ARRAY_REF:
16051 case SCOPE_REF:
16052 /* We should use one of the expression tsubsts for these codes. */
16053 gcc_unreachable ();
16054
16055 default:
16056 sorry ("use of %qs in template", get_tree_code_name (code));
16057 return error_mark_node;
16058 }
16059 }
16060
16061 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16062 expression on the left-hand side of the "." or "->" operator. We
16063 only do the lookup if we had a dependent BASELINK. Otherwise we
16064 adjust it onto the instantiated heirarchy. */
16065
16066 static tree
16067 tsubst_baselink (tree baselink, tree object_type,
16068 tree args, tsubst_flags_t complain, tree in_decl)
16069 {
16070 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16071 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16072 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16073
16074 tree optype = BASELINK_OPTYPE (baselink);
16075 optype = tsubst (optype, args, complain, in_decl);
16076
16077 tree template_args = NULL_TREE;
16078 bool template_id_p = false;
16079 tree fns = BASELINK_FUNCTIONS (baselink);
16080 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16081 {
16082 template_id_p = true;
16083 template_args = TREE_OPERAND (fns, 1);
16084 fns = TREE_OPERAND (fns, 0);
16085 if (template_args)
16086 template_args = tsubst_template_args (template_args, args,
16087 complain, in_decl);
16088 }
16089
16090 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16091 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16092 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16093
16094 if (dependent_p)
16095 {
16096 tree name = OVL_NAME (fns);
16097 if (IDENTIFIER_CONV_OP_P (name))
16098 name = make_conv_op_name (optype);
16099
16100 if (name == complete_dtor_identifier)
16101 /* Treat as-if non-dependent below. */
16102 dependent_p = false;
16103
16104 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16105 complain);
16106 if (!baselink)
16107 {
16108 if ((complain & tf_error)
16109 && constructor_name_p (name, qualifying_scope))
16110 error ("cannot call constructor %<%T::%D%> directly",
16111 qualifying_scope, name);
16112 return error_mark_node;
16113 }
16114
16115 if (BASELINK_P (baselink))
16116 fns = BASELINK_FUNCTIONS (baselink);
16117 }
16118 else
16119 /* We're going to overwrite pieces below, make a duplicate. */
16120 baselink = copy_node (baselink);
16121
16122 /* If lookup found a single function, mark it as used at this point.
16123 (If lookup found multiple functions the one selected later by
16124 overload resolution will be marked as used at that point.) */
16125 if (!template_id_p && !really_overloaded_fn (fns))
16126 {
16127 tree fn = OVL_FIRST (fns);
16128 bool ok = mark_used (fn, complain);
16129 if (!ok && !(complain & tf_error))
16130 return error_mark_node;
16131 if (ok && BASELINK_P (baselink))
16132 /* We might have instantiated an auto function. */
16133 TREE_TYPE (baselink) = TREE_TYPE (fn);
16134 }
16135
16136 if (BASELINK_P (baselink))
16137 {
16138 /* Add back the template arguments, if present. */
16139 if (template_id_p)
16140 BASELINK_FUNCTIONS (baselink)
16141 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16142
16143 /* Update the conversion operator type. */
16144 BASELINK_OPTYPE (baselink) = optype;
16145 }
16146
16147 if (!object_type)
16148 object_type = current_class_type;
16149
16150 if (qualified_p || !dependent_p)
16151 {
16152 baselink = adjust_result_of_qualified_name_lookup (baselink,
16153 qualifying_scope,
16154 object_type);
16155 if (!qualified_p)
16156 /* We need to call adjust_result_of_qualified_name_lookup in case the
16157 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16158 so that we still get virtual function binding. */
16159 BASELINK_QUALIFIED_P (baselink) = false;
16160 }
16161
16162 return baselink;
16163 }
16164
16165 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16166 true if the qualified-id will be a postfix-expression in-and-of
16167 itself; false if more of the postfix-expression follows the
16168 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16169 of "&". */
16170
16171 static tree
16172 tsubst_qualified_id (tree qualified_id, tree args,
16173 tsubst_flags_t complain, tree in_decl,
16174 bool done, bool address_p)
16175 {
16176 tree expr;
16177 tree scope;
16178 tree name;
16179 bool is_template;
16180 tree template_args;
16181 location_t loc = UNKNOWN_LOCATION;
16182
16183 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16184
16185 /* Figure out what name to look up. */
16186 name = TREE_OPERAND (qualified_id, 1);
16187 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16188 {
16189 is_template = true;
16190 loc = EXPR_LOCATION (name);
16191 template_args = TREE_OPERAND (name, 1);
16192 if (template_args)
16193 template_args = tsubst_template_args (template_args, args,
16194 complain, in_decl);
16195 if (template_args == error_mark_node)
16196 return error_mark_node;
16197 name = TREE_OPERAND (name, 0);
16198 }
16199 else
16200 {
16201 is_template = false;
16202 template_args = NULL_TREE;
16203 }
16204
16205 /* Substitute into the qualifying scope. When there are no ARGS, we
16206 are just trying to simplify a non-dependent expression. In that
16207 case the qualifying scope may be dependent, and, in any case,
16208 substituting will not help. */
16209 scope = TREE_OPERAND (qualified_id, 0);
16210 if (args)
16211 {
16212 scope = tsubst (scope, args, complain, in_decl);
16213 expr = tsubst_copy (name, args, complain, in_decl);
16214 }
16215 else
16216 expr = name;
16217
16218 if (dependent_scope_p (scope))
16219 {
16220 if (is_template)
16221 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16222 tree r = build_qualified_name (NULL_TREE, scope, expr,
16223 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16224 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16225 return r;
16226 }
16227
16228 if (!BASELINK_P (name) && !DECL_P (expr))
16229 {
16230 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16231 {
16232 /* A BIT_NOT_EXPR is used to represent a destructor. */
16233 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16234 {
16235 error ("qualifying type %qT does not match destructor name ~%qT",
16236 scope, TREE_OPERAND (expr, 0));
16237 expr = error_mark_node;
16238 }
16239 else
16240 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16241 LOOK_want::NORMAL, false);
16242 }
16243 else
16244 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16245 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16246 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16247 {
16248 if (complain & tf_error)
16249 {
16250 error ("dependent-name %qE is parsed as a non-type, but "
16251 "instantiation yields a type", qualified_id);
16252 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16253 }
16254 return error_mark_node;
16255 }
16256 }
16257
16258 if (DECL_P (expr))
16259 {
16260 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16261 scope, complain))
16262 return error_mark_node;
16263 /* Remember that there was a reference to this entity. */
16264 if (!mark_used (expr, complain) && !(complain & tf_error))
16265 return error_mark_node;
16266 }
16267
16268 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16269 {
16270 if (complain & tf_error)
16271 qualified_name_lookup_error (scope,
16272 TREE_OPERAND (qualified_id, 1),
16273 expr, input_location);
16274 return error_mark_node;
16275 }
16276
16277 if (is_template)
16278 {
16279 /* We may be repeating a check already done during parsing, but
16280 if it was well-formed and passed then, it will pass again
16281 now, and if it didn't, we wouldn't have got here. The case
16282 we want to catch is when we couldn't tell then, and can now,
16283 namely when templ prior to substitution was an
16284 identifier. */
16285 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16286 return error_mark_node;
16287
16288 if (variable_template_p (expr))
16289 expr = lookup_and_finish_template_variable (expr, template_args,
16290 complain);
16291 else
16292 expr = lookup_template_function (expr, template_args);
16293 }
16294
16295 if (expr == error_mark_node && complain & tf_error)
16296 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16297 expr, input_location);
16298 else if (TYPE_P (scope))
16299 {
16300 expr = (adjust_result_of_qualified_name_lookup
16301 (expr, scope, current_nonlambda_class_type ()));
16302 expr = (finish_qualified_id_expr
16303 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16304 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16305 /*template_arg_p=*/false, complain));
16306 }
16307
16308 /* Expressions do not generally have reference type. */
16309 if (TREE_CODE (expr) != SCOPE_REF
16310 /* However, if we're about to form a pointer-to-member, we just
16311 want the referenced member referenced. */
16312 && TREE_CODE (expr) != OFFSET_REF)
16313 expr = convert_from_reference (expr);
16314
16315 if (REF_PARENTHESIZED_P (qualified_id))
16316 expr = force_paren_expr (expr);
16317
16318 return expr;
16319 }
16320
16321 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16322 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16323 for tsubst. */
16324
16325 static tree
16326 tsubst_init (tree init, tree decl, tree args,
16327 tsubst_flags_t complain, tree in_decl)
16328 {
16329 if (!init)
16330 return NULL_TREE;
16331
16332 init = tsubst_expr (init, args, complain, in_decl, false);
16333
16334 tree type = TREE_TYPE (decl);
16335
16336 if (!init && type != error_mark_node)
16337 {
16338 if (tree auto_node = type_uses_auto (type))
16339 {
16340 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16341 {
16342 if (complain & tf_error)
16343 error ("initializer for %q#D expands to an empty list "
16344 "of expressions", decl);
16345 return error_mark_node;
16346 }
16347 }
16348 else if (!dependent_type_p (type))
16349 {
16350 /* If we had an initializer but it
16351 instantiated to nothing,
16352 value-initialize the object. This will
16353 only occur when the initializer was a
16354 pack expansion where the parameter packs
16355 used in that expansion were of length
16356 zero. */
16357 init = build_value_init (type, complain);
16358 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16359 init = get_target_expr_sfinae (init, complain);
16360 if (TREE_CODE (init) == TARGET_EXPR)
16361 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16362 }
16363 }
16364
16365 return init;
16366 }
16367
16368 /* If T is a reference to a dependent member of the current instantiation C and
16369 we are trying to refer to that member in a partial instantiation of C,
16370 return a SCOPE_REF; otherwise, return NULL_TREE.
16371
16372 This can happen when forming a C++17 deduction guide, as in PR96199. */
16373
16374 static tree
16375 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16376 tree in_decl)
16377 {
16378 if (cxx_dialect < cxx17)
16379 return NULL_TREE;
16380
16381 tree ctx = context_for_name_lookup (t);
16382 if (!CLASS_TYPE_P (ctx))
16383 return NULL_TREE;
16384
16385 ctx = tsubst (ctx, args, complain, in_decl);
16386 if (dependent_scope_p (ctx))
16387 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16388 /*template_p=*/false);
16389
16390 return NULL_TREE;
16391 }
16392
16393 /* Like tsubst, but deals with expressions. This function just replaces
16394 template parms; to finish processing the resultant expression, use
16395 tsubst_copy_and_build or tsubst_expr. */
16396
16397 static tree
16398 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16399 {
16400 enum tree_code code;
16401 tree r;
16402
16403 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16404 return t;
16405
16406 code = TREE_CODE (t);
16407
16408 switch (code)
16409 {
16410 case PARM_DECL:
16411 r = retrieve_local_specialization (t);
16412
16413 if (r == NULL_TREE)
16414 {
16415 /* We get here for a use of 'this' in an NSDMI. */
16416 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16417 return current_class_ptr;
16418
16419 /* This can happen for a parameter name used later in a function
16420 declaration (such as in a late-specified return type). Just
16421 make a dummy decl, since it's only used for its type. */
16422 gcc_assert (cp_unevaluated_operand != 0);
16423 r = tsubst_decl (t, args, complain);
16424 /* Give it the template pattern as its context; its true context
16425 hasn't been instantiated yet and this is good enough for
16426 mangling. */
16427 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16428 }
16429
16430 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16431 r = argument_pack_select_arg (r);
16432 if (!mark_used (r, complain) && !(complain & tf_error))
16433 return error_mark_node;
16434 return r;
16435
16436 case CONST_DECL:
16437 {
16438 tree enum_type;
16439 tree v;
16440
16441 if (DECL_TEMPLATE_PARM_P (t))
16442 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16443 /* There is no need to substitute into namespace-scope
16444 enumerators. */
16445 if (DECL_NAMESPACE_SCOPE_P (t))
16446 return t;
16447 /* If ARGS is NULL, then T is known to be non-dependent. */
16448 if (args == NULL_TREE)
16449 return scalar_constant_value (t);
16450
16451 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16452 return ref;
16453
16454 /* Unfortunately, we cannot just call lookup_name here.
16455 Consider:
16456
16457 template <int I> int f() {
16458 enum E { a = I };
16459 struct S { void g() { E e = a; } };
16460 };
16461
16462 When we instantiate f<7>::S::g(), say, lookup_name is not
16463 clever enough to find f<7>::a. */
16464 enum_type
16465 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16466 /*entering_scope=*/0);
16467
16468 for (v = TYPE_VALUES (enum_type);
16469 v != NULL_TREE;
16470 v = TREE_CHAIN (v))
16471 if (TREE_PURPOSE (v) == DECL_NAME (t))
16472 return TREE_VALUE (v);
16473
16474 /* We didn't find the name. That should never happen; if
16475 name-lookup found it during preliminary parsing, we
16476 should find it again here during instantiation. */
16477 gcc_unreachable ();
16478 }
16479 return t;
16480
16481 case FIELD_DECL:
16482 if (DECL_CONTEXT (t))
16483 {
16484 tree ctx;
16485
16486 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16487 /*entering_scope=*/1);
16488 if (ctx != DECL_CONTEXT (t))
16489 {
16490 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16491 if (!r)
16492 {
16493 if (complain & tf_error)
16494 error ("using invalid field %qD", t);
16495 return error_mark_node;
16496 }
16497 return r;
16498 }
16499 }
16500
16501 return t;
16502
16503 case VAR_DECL:
16504 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16505 return ref;
16506 gcc_fallthrough();
16507 case FUNCTION_DECL:
16508 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16509 r = tsubst (t, args, complain, in_decl);
16510 else if (local_variable_p (t)
16511 && uses_template_parms (DECL_CONTEXT (t)))
16512 {
16513 r = retrieve_local_specialization (t);
16514 if (r == NULL_TREE)
16515 {
16516 /* First try name lookup to find the instantiation. */
16517 r = lookup_name (DECL_NAME (t));
16518 if (r)
16519 {
16520 if (!VAR_P (r))
16521 {
16522 /* During error-recovery we may find a non-variable,
16523 even an OVERLOAD: just bail out and avoid ICEs and
16524 duplicate diagnostics (c++/62207). */
16525 gcc_assert (seen_error ());
16526 return error_mark_node;
16527 }
16528 if (!is_capture_proxy (r))
16529 {
16530 /* Make sure the one we found is the one we want. */
16531 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16532 if (ctx != DECL_CONTEXT (r))
16533 r = NULL_TREE;
16534 }
16535 }
16536
16537 if (r)
16538 /* OK */;
16539 else
16540 {
16541 /* This can happen for a variable used in a
16542 late-specified return type of a local lambda, or for a
16543 local static or constant. Building a new VAR_DECL
16544 should be OK in all those cases. */
16545 r = tsubst_decl (t, args, complain);
16546 if (local_specializations)
16547 /* Avoid infinite recursion (79640). */
16548 register_local_specialization (r, t);
16549 if (decl_maybe_constant_var_p (r))
16550 {
16551 /* We can't call cp_finish_decl, so handle the
16552 initializer by hand. */
16553 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16554 complain, in_decl);
16555 if (!processing_template_decl)
16556 init = maybe_constant_init (init);
16557 if (processing_template_decl
16558 ? potential_constant_expression (init)
16559 : reduced_constant_expression_p (init))
16560 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16561 = TREE_CONSTANT (r) = true;
16562 DECL_INITIAL (r) = init;
16563 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16564 TREE_TYPE (r)
16565 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16566 complain, adc_variable_type);
16567 }
16568 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16569 || decl_constant_var_p (r)
16570 || seen_error ());
16571 if (!processing_template_decl
16572 && !TREE_STATIC (r))
16573 r = process_outer_var_ref (r, complain);
16574 }
16575 /* Remember this for subsequent uses. */
16576 if (local_specializations)
16577 register_local_specialization (r, t);
16578 }
16579 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16580 r = argument_pack_select_arg (r);
16581 }
16582 else
16583 r = t;
16584 if (!mark_used (r, complain))
16585 return error_mark_node;
16586 return r;
16587
16588 case NAMESPACE_DECL:
16589 return t;
16590
16591 case OVERLOAD:
16592 return t;
16593
16594 case BASELINK:
16595 return tsubst_baselink (t, current_nonlambda_class_type (),
16596 args, complain, in_decl);
16597
16598 case TEMPLATE_DECL:
16599 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16600 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16601 args, complain, in_decl);
16602 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16603 return tsubst (t, args, complain, in_decl);
16604 else if (DECL_CLASS_SCOPE_P (t)
16605 && uses_template_parms (DECL_CONTEXT (t)))
16606 {
16607 /* Template template argument like the following example need
16608 special treatment:
16609
16610 template <template <class> class TT> struct C {};
16611 template <class T> struct D {
16612 template <class U> struct E {};
16613 C<E> c; // #1
16614 };
16615 D<int> d; // #2
16616
16617 We are processing the template argument `E' in #1 for
16618 the template instantiation #2. Originally, `E' is a
16619 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16620 have to substitute this with one having context `D<int>'. */
16621
16622 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16623 if (dependent_scope_p (context))
16624 {
16625 /* When rewriting a constructor into a deduction guide, a
16626 non-dependent name can become dependent, so memtmpl<args>
16627 becomes context::template memtmpl<args>. */
16628 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16629 return build_qualified_name (type, context, DECL_NAME (t),
16630 /*template*/true);
16631 }
16632 return lookup_field (context, DECL_NAME(t), 0, false);
16633 }
16634 else
16635 /* Ordinary template template argument. */
16636 return t;
16637
16638 case NON_LVALUE_EXPR:
16639 case VIEW_CONVERT_EXPR:
16640 {
16641 /* Handle location wrappers by substituting the wrapped node
16642 first, *then* reusing the resulting type. Doing the type
16643 first ensures that we handle template parameters and
16644 parameter pack expansions. */
16645 if (location_wrapper_p (t))
16646 {
16647 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16648 complain, in_decl);
16649 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16650 }
16651 tree op = TREE_OPERAND (t, 0);
16652 if (code == VIEW_CONVERT_EXPR
16653 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16654 {
16655 /* Wrapper to make a C++20 template parameter object const. */
16656 op = tsubst_copy (op, args, complain, in_decl);
16657 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16658 {
16659 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16660 return build1 (code, type, op);
16661 }
16662 else
16663 {
16664 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16665 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16666 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16667 return op;
16668 }
16669 }
16670 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16671 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16672 {
16673 op = tsubst_copy (op, args, complain, in_decl);
16674 op = build1 (code, TREE_TYPE (op), op);
16675 REF_PARENTHESIZED_P (op) = true;
16676 return op;
16677 }
16678 /* We shouldn't see any other uses of these in templates. */
16679 gcc_unreachable ();
16680 }
16681
16682 case CAST_EXPR:
16683 case REINTERPRET_CAST_EXPR:
16684 case CONST_CAST_EXPR:
16685 case STATIC_CAST_EXPR:
16686 case DYNAMIC_CAST_EXPR:
16687 case IMPLICIT_CONV_EXPR:
16688 case CONVERT_EXPR:
16689 case NOP_EXPR:
16690 {
16691 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16692 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16693 return build1 (code, type, op0);
16694 }
16695
16696 case SIZEOF_EXPR:
16697 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16698 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16699 {
16700 tree expanded, op = TREE_OPERAND (t, 0);
16701 int len = 0;
16702
16703 if (SIZEOF_EXPR_TYPE_P (t))
16704 op = TREE_TYPE (op);
16705
16706 ++cp_unevaluated_operand;
16707 ++c_inhibit_evaluation_warnings;
16708 /* We only want to compute the number of arguments. */
16709 if (PACK_EXPANSION_P (op))
16710 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16711 else
16712 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16713 args, complain, in_decl);
16714 --cp_unevaluated_operand;
16715 --c_inhibit_evaluation_warnings;
16716
16717 if (TREE_CODE (expanded) == TREE_VEC)
16718 {
16719 len = TREE_VEC_LENGTH (expanded);
16720 /* Set TREE_USED for the benefit of -Wunused. */
16721 for (int i = 0; i < len; i++)
16722 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16723 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16724 }
16725
16726 if (expanded == error_mark_node)
16727 return error_mark_node;
16728 else if (PACK_EXPANSION_P (expanded)
16729 || (TREE_CODE (expanded) == TREE_VEC
16730 && pack_expansion_args_count (expanded)))
16731
16732 {
16733 if (PACK_EXPANSION_P (expanded))
16734 /* OK. */;
16735 else if (TREE_VEC_LENGTH (expanded) == 1)
16736 expanded = TREE_VEC_ELT (expanded, 0);
16737 else
16738 expanded = make_argument_pack (expanded);
16739
16740 if (TYPE_P (expanded))
16741 return cxx_sizeof_or_alignof_type (input_location,
16742 expanded, SIZEOF_EXPR,
16743 false,
16744 complain & tf_error);
16745 else
16746 return cxx_sizeof_or_alignof_expr (input_location,
16747 expanded, SIZEOF_EXPR,
16748 complain & tf_error);
16749 }
16750 else
16751 return build_int_cst (size_type_node, len);
16752 }
16753 if (SIZEOF_EXPR_TYPE_P (t))
16754 {
16755 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16756 args, complain, in_decl);
16757 r = build1 (NOP_EXPR, r, error_mark_node);
16758 r = build1 (SIZEOF_EXPR,
16759 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16760 SIZEOF_EXPR_TYPE_P (r) = 1;
16761 return r;
16762 }
16763 /* Fall through */
16764
16765 case INDIRECT_REF:
16766 case NEGATE_EXPR:
16767 case TRUTH_NOT_EXPR:
16768 case BIT_NOT_EXPR:
16769 case ADDR_EXPR:
16770 case UNARY_PLUS_EXPR: /* Unary + */
16771 case ALIGNOF_EXPR:
16772 case AT_ENCODE_EXPR:
16773 case ARROW_EXPR:
16774 case THROW_EXPR:
16775 case TYPEID_EXPR:
16776 case REALPART_EXPR:
16777 case IMAGPART_EXPR:
16778 case PAREN_EXPR:
16779 {
16780 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16781 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16782 r = build1 (code, type, op0);
16783 if (code == ALIGNOF_EXPR)
16784 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16785 return r;
16786 }
16787
16788 case COMPONENT_REF:
16789 {
16790 tree object;
16791 tree name;
16792
16793 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16794 name = TREE_OPERAND (t, 1);
16795 if (TREE_CODE (name) == BIT_NOT_EXPR)
16796 {
16797 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16798 complain, in_decl);
16799 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16800 }
16801 else if (TREE_CODE (name) == SCOPE_REF
16802 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16803 {
16804 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16805 complain, in_decl);
16806 name = TREE_OPERAND (name, 1);
16807 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16808 complain, in_decl);
16809 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16810 name = build_qualified_name (/*type=*/NULL_TREE,
16811 base, name,
16812 /*template_p=*/false);
16813 }
16814 else if (BASELINK_P (name))
16815 name = tsubst_baselink (name,
16816 non_reference (TREE_TYPE (object)),
16817 args, complain,
16818 in_decl);
16819 else
16820 name = tsubst_copy (name, args, complain, in_decl);
16821 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16822 }
16823
16824 case PLUS_EXPR:
16825 case MINUS_EXPR:
16826 case MULT_EXPR:
16827 case TRUNC_DIV_EXPR:
16828 case CEIL_DIV_EXPR:
16829 case FLOOR_DIV_EXPR:
16830 case ROUND_DIV_EXPR:
16831 case EXACT_DIV_EXPR:
16832 case BIT_AND_EXPR:
16833 case BIT_IOR_EXPR:
16834 case BIT_XOR_EXPR:
16835 case TRUNC_MOD_EXPR:
16836 case FLOOR_MOD_EXPR:
16837 case TRUTH_ANDIF_EXPR:
16838 case TRUTH_ORIF_EXPR:
16839 case TRUTH_AND_EXPR:
16840 case TRUTH_OR_EXPR:
16841 case RSHIFT_EXPR:
16842 case LSHIFT_EXPR:
16843 case EQ_EXPR:
16844 case NE_EXPR:
16845 case MAX_EXPR:
16846 case MIN_EXPR:
16847 case LE_EXPR:
16848 case GE_EXPR:
16849 case LT_EXPR:
16850 case GT_EXPR:
16851 case COMPOUND_EXPR:
16852 case DOTSTAR_EXPR:
16853 case MEMBER_REF:
16854 case PREDECREMENT_EXPR:
16855 case PREINCREMENT_EXPR:
16856 case POSTDECREMENT_EXPR:
16857 case POSTINCREMENT_EXPR:
16858 {
16859 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16860 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16861 return build_nt (code, op0, op1);
16862 }
16863
16864 case SCOPE_REF:
16865 {
16866 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16867 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16868 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16869 QUALIFIED_NAME_IS_TEMPLATE (t));
16870 }
16871
16872 case ARRAY_REF:
16873 {
16874 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16875 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16876 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16877 }
16878
16879 case CALL_EXPR:
16880 {
16881 int n = VL_EXP_OPERAND_LENGTH (t);
16882 tree result = build_vl_exp (CALL_EXPR, n);
16883 int i;
16884 for (i = 0; i < n; i++)
16885 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16886 complain, in_decl);
16887 return result;
16888 }
16889
16890 case COND_EXPR:
16891 case MODOP_EXPR:
16892 case PSEUDO_DTOR_EXPR:
16893 case VEC_PERM_EXPR:
16894 {
16895 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16896 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16897 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16898 r = build_nt (code, op0, op1, op2);
16899 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16900 return r;
16901 }
16902
16903 case NEW_EXPR:
16904 {
16905 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16906 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16907 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16908 r = build_nt (code, op0, op1, op2);
16909 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16910 return r;
16911 }
16912
16913 case DELETE_EXPR:
16914 {
16915 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16916 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16917 r = build_nt (code, op0, op1);
16918 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16919 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16920 return r;
16921 }
16922
16923 case TEMPLATE_ID_EXPR:
16924 {
16925 /* Substituted template arguments */
16926 tree fn = TREE_OPERAND (t, 0);
16927 tree targs = TREE_OPERAND (t, 1);
16928
16929 fn = tsubst_copy (fn, args, complain, in_decl);
16930 if (targs)
16931 targs = tsubst_template_args (targs, args, complain, in_decl);
16932
16933 return lookup_template_function (fn, targs);
16934 }
16935
16936 case TREE_LIST:
16937 {
16938 tree purpose, value, chain;
16939
16940 if (t == void_list_node)
16941 return t;
16942
16943 purpose = TREE_PURPOSE (t);
16944 if (purpose)
16945 purpose = tsubst_copy (purpose, args, complain, in_decl);
16946 value = TREE_VALUE (t);
16947 if (value)
16948 value = tsubst_copy (value, args, complain, in_decl);
16949 chain = TREE_CHAIN (t);
16950 if (chain && chain != void_type_node)
16951 chain = tsubst_copy (chain, args, complain, in_decl);
16952 if (purpose == TREE_PURPOSE (t)
16953 && value == TREE_VALUE (t)
16954 && chain == TREE_CHAIN (t))
16955 return t;
16956 return tree_cons (purpose, value, chain);
16957 }
16958
16959 case RECORD_TYPE:
16960 case UNION_TYPE:
16961 case ENUMERAL_TYPE:
16962 case INTEGER_TYPE:
16963 case TEMPLATE_TYPE_PARM:
16964 case TEMPLATE_TEMPLATE_PARM:
16965 case BOUND_TEMPLATE_TEMPLATE_PARM:
16966 case TEMPLATE_PARM_INDEX:
16967 case POINTER_TYPE:
16968 case REFERENCE_TYPE:
16969 case OFFSET_TYPE:
16970 case FUNCTION_TYPE:
16971 case METHOD_TYPE:
16972 case ARRAY_TYPE:
16973 case TYPENAME_TYPE:
16974 case UNBOUND_CLASS_TEMPLATE:
16975 case TYPEOF_TYPE:
16976 case DECLTYPE_TYPE:
16977 case TYPE_DECL:
16978 return tsubst (t, args, complain, in_decl);
16979
16980 case USING_DECL:
16981 t = DECL_NAME (t);
16982 /* Fall through. */
16983 case IDENTIFIER_NODE:
16984 if (IDENTIFIER_CONV_OP_P (t))
16985 {
16986 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16987 return make_conv_op_name (new_type);
16988 }
16989 else
16990 return t;
16991
16992 case CONSTRUCTOR:
16993 /* This is handled by tsubst_copy_and_build. */
16994 gcc_unreachable ();
16995
16996 case VA_ARG_EXPR:
16997 {
16998 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16999 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17000 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17001 }
17002
17003 case CLEANUP_POINT_EXPR:
17004 /* We shouldn't have built any of these during initial template
17005 generation. Instead, they should be built during instantiation
17006 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17007 gcc_unreachable ();
17008
17009 case OFFSET_REF:
17010 {
17011 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17012 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17013 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17014 r = build2 (code, type, op0, op1);
17015 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17016 if (!mark_used (TREE_OPERAND (r, 1), complain)
17017 && !(complain & tf_error))
17018 return error_mark_node;
17019 return r;
17020 }
17021
17022 case EXPR_PACK_EXPANSION:
17023 error ("invalid use of pack expansion expression");
17024 return error_mark_node;
17025
17026 case NONTYPE_ARGUMENT_PACK:
17027 error ("use %<...%> to expand argument pack");
17028 return error_mark_node;
17029
17030 case VOID_CST:
17031 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17032 return t;
17033
17034 case INTEGER_CST:
17035 case REAL_CST:
17036 case COMPLEX_CST:
17037 {
17038 /* Instantiate any typedefs in the type. */
17039 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17040 r = fold_convert (type, t);
17041 gcc_assert (TREE_CODE (r) == code);
17042 return r;
17043 }
17044
17045 case STRING_CST:
17046 {
17047 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17048 r = t;
17049 if (type != TREE_TYPE (t))
17050 {
17051 r = copy_node (t);
17052 TREE_TYPE (r) = type;
17053 }
17054 return r;
17055 }
17056
17057 case PTRMEM_CST:
17058 /* These can sometimes show up in a partial instantiation, but never
17059 involve template parms. */
17060 gcc_assert (!uses_template_parms (t));
17061 return t;
17062
17063 case UNARY_LEFT_FOLD_EXPR:
17064 return tsubst_unary_left_fold (t, args, complain, in_decl);
17065 case UNARY_RIGHT_FOLD_EXPR:
17066 return tsubst_unary_right_fold (t, args, complain, in_decl);
17067 case BINARY_LEFT_FOLD_EXPR:
17068 return tsubst_binary_left_fold (t, args, complain, in_decl);
17069 case BINARY_RIGHT_FOLD_EXPR:
17070 return tsubst_binary_right_fold (t, args, complain, in_decl);
17071 case PREDICT_EXPR:
17072 return t;
17073
17074 case DEBUG_BEGIN_STMT:
17075 /* ??? There's no point in copying it for now, but maybe some
17076 day it will contain more information, such as a pointer back
17077 to the containing function, inlined copy or so. */
17078 return t;
17079
17080 case CO_AWAIT_EXPR:
17081 return tsubst_expr (t, args, complain, in_decl,
17082 /*integral_constant_expression_p=*/false);
17083 break;
17084
17085 default:
17086 /* We shouldn't get here, but keep going if !flag_checking. */
17087 if (flag_checking)
17088 gcc_unreachable ();
17089 return t;
17090 }
17091 }
17092
17093 /* Helper function for tsubst_omp_clauses, used for instantiation of
17094 OMP_CLAUSE_DECL of clauses. */
17095
17096 static tree
17097 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17098 tree in_decl, tree *iterator_cache)
17099 {
17100 if (decl == NULL_TREE)
17101 return NULL_TREE;
17102
17103 /* Handle OpenMP iterators. */
17104 if (TREE_CODE (decl) == TREE_LIST
17105 && TREE_PURPOSE (decl)
17106 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17107 {
17108 tree ret;
17109 if (iterator_cache[0] == TREE_PURPOSE (decl))
17110 ret = iterator_cache[1];
17111 else
17112 {
17113 tree *tp = &ret;
17114 begin_scope (sk_omp, NULL);
17115 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17116 {
17117 *tp = copy_node (it);
17118 TREE_VEC_ELT (*tp, 0)
17119 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17120 TREE_VEC_ELT (*tp, 1)
17121 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17122 /*integral_constant_expression_p=*/false);
17123 TREE_VEC_ELT (*tp, 2)
17124 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17125 /*integral_constant_expression_p=*/false);
17126 TREE_VEC_ELT (*tp, 3)
17127 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17128 /*integral_constant_expression_p=*/false);
17129 TREE_CHAIN (*tp) = NULL_TREE;
17130 tp = &TREE_CHAIN (*tp);
17131 }
17132 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17133 iterator_cache[0] = TREE_PURPOSE (decl);
17134 iterator_cache[1] = ret;
17135 }
17136 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17137 args, complain,
17138 in_decl, NULL));
17139 }
17140
17141 /* Handle an OpenMP array section represented as a TREE_LIST (or
17142 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17143 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17144 TREE_LIST. We can handle it exactly the same as an array section
17145 (purpose, value, and a chain), even though the nomenclature
17146 (low_bound, length, etc) is different. */
17147 if (TREE_CODE (decl) == TREE_LIST)
17148 {
17149 tree low_bound
17150 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17151 /*integral_constant_expression_p=*/false);
17152 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17153 /*integral_constant_expression_p=*/false);
17154 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17155 in_decl, NULL);
17156 if (TREE_PURPOSE (decl) == low_bound
17157 && TREE_VALUE (decl) == length
17158 && TREE_CHAIN (decl) == chain)
17159 return decl;
17160 tree ret = tree_cons (low_bound, length, chain);
17161 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17162 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17163 return ret;
17164 }
17165 tree ret = tsubst_expr (decl, args, complain, in_decl,
17166 /*integral_constant_expression_p=*/false);
17167 /* Undo convert_from_reference tsubst_expr could have called. */
17168 if (decl
17169 && REFERENCE_REF_P (ret)
17170 && !REFERENCE_REF_P (decl))
17171 ret = TREE_OPERAND (ret, 0);
17172 return ret;
17173 }
17174
17175 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17176
17177 static tree
17178 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17179 tree args, tsubst_flags_t complain, tree in_decl)
17180 {
17181 tree new_clauses = NULL_TREE, nc, oc;
17182 tree linear_no_step = NULL_TREE;
17183 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17184
17185 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17186 {
17187 nc = copy_node (oc);
17188 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17189 new_clauses = nc;
17190
17191 switch (OMP_CLAUSE_CODE (nc))
17192 {
17193 case OMP_CLAUSE_LASTPRIVATE:
17194 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17195 {
17196 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17197 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17198 in_decl, /*integral_constant_expression_p=*/false);
17199 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17200 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17201 }
17202 /* FALLTHRU */
17203 case OMP_CLAUSE_PRIVATE:
17204 case OMP_CLAUSE_SHARED:
17205 case OMP_CLAUSE_FIRSTPRIVATE:
17206 case OMP_CLAUSE_COPYIN:
17207 case OMP_CLAUSE_COPYPRIVATE:
17208 case OMP_CLAUSE_UNIFORM:
17209 case OMP_CLAUSE_DEPEND:
17210 case OMP_CLAUSE_FROM:
17211 case OMP_CLAUSE_TO:
17212 case OMP_CLAUSE_MAP:
17213 case OMP_CLAUSE_NONTEMPORAL:
17214 case OMP_CLAUSE_USE_DEVICE_PTR:
17215 case OMP_CLAUSE_USE_DEVICE_ADDR:
17216 case OMP_CLAUSE_IS_DEVICE_PTR:
17217 case OMP_CLAUSE_INCLUSIVE:
17218 case OMP_CLAUSE_EXCLUSIVE:
17219 OMP_CLAUSE_DECL (nc)
17220 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17221 in_decl, iterator_cache);
17222 break;
17223 case OMP_CLAUSE_TILE:
17224 case OMP_CLAUSE_IF:
17225 case OMP_CLAUSE_NUM_THREADS:
17226 case OMP_CLAUSE_SCHEDULE:
17227 case OMP_CLAUSE_COLLAPSE:
17228 case OMP_CLAUSE_FINAL:
17229 case OMP_CLAUSE_DEVICE:
17230 case OMP_CLAUSE_DIST_SCHEDULE:
17231 case OMP_CLAUSE_NUM_TEAMS:
17232 case OMP_CLAUSE_THREAD_LIMIT:
17233 case OMP_CLAUSE_SAFELEN:
17234 case OMP_CLAUSE_SIMDLEN:
17235 case OMP_CLAUSE_NUM_TASKS:
17236 case OMP_CLAUSE_GRAINSIZE:
17237 case OMP_CLAUSE_PRIORITY:
17238 case OMP_CLAUSE_ORDERED:
17239 case OMP_CLAUSE_HINT:
17240 case OMP_CLAUSE_NUM_GANGS:
17241 case OMP_CLAUSE_NUM_WORKERS:
17242 case OMP_CLAUSE_VECTOR_LENGTH:
17243 case OMP_CLAUSE_WORKER:
17244 case OMP_CLAUSE_VECTOR:
17245 case OMP_CLAUSE_ASYNC:
17246 case OMP_CLAUSE_WAIT:
17247 OMP_CLAUSE_OPERAND (nc, 0)
17248 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17249 in_decl, /*integral_constant_expression_p=*/false);
17250 break;
17251 case OMP_CLAUSE_REDUCTION:
17252 case OMP_CLAUSE_IN_REDUCTION:
17253 case OMP_CLAUSE_TASK_REDUCTION:
17254 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17255 {
17256 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17257 if (TREE_CODE (placeholder) == SCOPE_REF)
17258 {
17259 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17260 complain, in_decl);
17261 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17262 = build_qualified_name (NULL_TREE, scope,
17263 TREE_OPERAND (placeholder, 1),
17264 false);
17265 }
17266 else
17267 gcc_assert (identifier_p (placeholder));
17268 }
17269 OMP_CLAUSE_DECL (nc)
17270 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17271 in_decl, NULL);
17272 break;
17273 case OMP_CLAUSE_GANG:
17274 case OMP_CLAUSE_ALIGNED:
17275 OMP_CLAUSE_DECL (nc)
17276 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17277 in_decl, NULL);
17278 OMP_CLAUSE_OPERAND (nc, 1)
17279 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17280 in_decl, /*integral_constant_expression_p=*/false);
17281 break;
17282 case OMP_CLAUSE_LINEAR:
17283 OMP_CLAUSE_DECL (nc)
17284 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17285 in_decl, NULL);
17286 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17287 {
17288 gcc_assert (!linear_no_step);
17289 linear_no_step = nc;
17290 }
17291 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17292 OMP_CLAUSE_LINEAR_STEP (nc)
17293 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17294 complain, in_decl, NULL);
17295 else
17296 OMP_CLAUSE_LINEAR_STEP (nc)
17297 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17298 in_decl,
17299 /*integral_constant_expression_p=*/false);
17300 break;
17301 case OMP_CLAUSE_NOWAIT:
17302 case OMP_CLAUSE_DEFAULT:
17303 case OMP_CLAUSE_UNTIED:
17304 case OMP_CLAUSE_MERGEABLE:
17305 case OMP_CLAUSE_INBRANCH:
17306 case OMP_CLAUSE_NOTINBRANCH:
17307 case OMP_CLAUSE_PROC_BIND:
17308 case OMP_CLAUSE_FOR:
17309 case OMP_CLAUSE_PARALLEL:
17310 case OMP_CLAUSE_SECTIONS:
17311 case OMP_CLAUSE_TASKGROUP:
17312 case OMP_CLAUSE_NOGROUP:
17313 case OMP_CLAUSE_THREADS:
17314 case OMP_CLAUSE_SIMD:
17315 case OMP_CLAUSE_DEFAULTMAP:
17316 case OMP_CLAUSE_ORDER:
17317 case OMP_CLAUSE_BIND:
17318 case OMP_CLAUSE_INDEPENDENT:
17319 case OMP_CLAUSE_AUTO:
17320 case OMP_CLAUSE_SEQ:
17321 case OMP_CLAUSE_IF_PRESENT:
17322 case OMP_CLAUSE_FINALIZE:
17323 break;
17324 default:
17325 gcc_unreachable ();
17326 }
17327 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17328 switch (OMP_CLAUSE_CODE (nc))
17329 {
17330 case OMP_CLAUSE_SHARED:
17331 case OMP_CLAUSE_PRIVATE:
17332 case OMP_CLAUSE_FIRSTPRIVATE:
17333 case OMP_CLAUSE_LASTPRIVATE:
17334 case OMP_CLAUSE_COPYPRIVATE:
17335 case OMP_CLAUSE_LINEAR:
17336 case OMP_CLAUSE_REDUCTION:
17337 case OMP_CLAUSE_IN_REDUCTION:
17338 case OMP_CLAUSE_TASK_REDUCTION:
17339 case OMP_CLAUSE_USE_DEVICE_PTR:
17340 case OMP_CLAUSE_USE_DEVICE_ADDR:
17341 case OMP_CLAUSE_IS_DEVICE_PTR:
17342 case OMP_CLAUSE_INCLUSIVE:
17343 case OMP_CLAUSE_EXCLUSIVE:
17344 /* tsubst_expr on SCOPE_REF results in returning
17345 finish_non_static_data_member result. Undo that here. */
17346 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17347 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17348 == IDENTIFIER_NODE))
17349 {
17350 tree t = OMP_CLAUSE_DECL (nc);
17351 tree v = t;
17352 while (v)
17353 switch (TREE_CODE (v))
17354 {
17355 case COMPONENT_REF:
17356 case MEM_REF:
17357 case INDIRECT_REF:
17358 CASE_CONVERT:
17359 case POINTER_PLUS_EXPR:
17360 v = TREE_OPERAND (v, 0);
17361 continue;
17362 case PARM_DECL:
17363 if (DECL_CONTEXT (v) == current_function_decl
17364 && DECL_ARTIFICIAL (v)
17365 && DECL_NAME (v) == this_identifier)
17366 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17367 /* FALLTHRU */
17368 default:
17369 v = NULL_TREE;
17370 break;
17371 }
17372 }
17373 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17374 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17375 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17376 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17377 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17378 {
17379 tree decl = OMP_CLAUSE_DECL (nc);
17380 if (VAR_P (decl))
17381 {
17382 retrofit_lang_decl (decl);
17383 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17384 }
17385 }
17386 break;
17387 default:
17388 break;
17389 }
17390 }
17391
17392 new_clauses = nreverse (new_clauses);
17393 if (ort != C_ORT_OMP_DECLARE_SIMD)
17394 {
17395 new_clauses = finish_omp_clauses (new_clauses, ort);
17396 if (linear_no_step)
17397 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17398 if (nc == linear_no_step)
17399 {
17400 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17401 break;
17402 }
17403 }
17404 return new_clauses;
17405 }
17406
17407 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17408
17409 static tree
17410 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17411 tree in_decl)
17412 {
17413 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17414
17415 tree purpose, value, chain;
17416
17417 if (t == NULL)
17418 return t;
17419
17420 if (TREE_CODE (t) != TREE_LIST)
17421 return tsubst_copy_and_build (t, args, complain, in_decl,
17422 /*function_p=*/false,
17423 /*integral_constant_expression_p=*/false);
17424
17425 if (t == void_list_node)
17426 return t;
17427
17428 purpose = TREE_PURPOSE (t);
17429 if (purpose)
17430 purpose = RECUR (purpose);
17431 value = TREE_VALUE (t);
17432 if (value)
17433 {
17434 if (TREE_CODE (value) != LABEL_DECL)
17435 value = RECUR (value);
17436 else
17437 {
17438 value = lookup_label (DECL_NAME (value));
17439 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17440 TREE_USED (value) = 1;
17441 }
17442 }
17443 chain = TREE_CHAIN (t);
17444 if (chain && chain != void_type_node)
17445 chain = RECUR (chain);
17446 return tree_cons (purpose, value, chain);
17447 #undef RECUR
17448 }
17449
17450 /* Used to temporarily communicate the list of #pragma omp parallel
17451 clauses to #pragma omp for instantiation if they are combined
17452 together. */
17453
17454 static tree *omp_parallel_combined_clauses;
17455
17456 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17457 tree *, unsigned int *);
17458
17459 /* Substitute one OMP_FOR iterator. */
17460
17461 static bool
17462 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17463 tree initv, tree condv, tree incrv, tree *clauses,
17464 tree args, tsubst_flags_t complain, tree in_decl,
17465 bool integral_constant_expression_p)
17466 {
17467 #define RECUR(NODE) \
17468 tsubst_expr ((NODE), args, complain, in_decl, \
17469 integral_constant_expression_p)
17470 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17471 bool ret = false;
17472
17473 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17474 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17475
17476 decl = TREE_OPERAND (init, 0);
17477 init = TREE_OPERAND (init, 1);
17478 tree decl_expr = NULL_TREE;
17479 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17480 if (range_for)
17481 {
17482 bool decomp = false;
17483 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17484 {
17485 tree v = DECL_VALUE_EXPR (decl);
17486 if (TREE_CODE (v) == ARRAY_REF
17487 && VAR_P (TREE_OPERAND (v, 0))
17488 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17489 {
17490 tree decomp_first = NULL_TREE;
17491 unsigned decomp_cnt = 0;
17492 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17493 maybe_push_decl (d);
17494 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17495 in_decl, &decomp_first, &decomp_cnt);
17496 decomp = true;
17497 if (d == error_mark_node)
17498 decl = error_mark_node;
17499 else
17500 for (unsigned int i = 0; i < decomp_cnt; i++)
17501 {
17502 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17503 {
17504 tree v = build_nt (ARRAY_REF, d,
17505 size_int (decomp_cnt - i - 1),
17506 NULL_TREE, NULL_TREE);
17507 SET_DECL_VALUE_EXPR (decomp_first, v);
17508 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17509 }
17510 fit_decomposition_lang_decl (decomp_first, d);
17511 decomp_first = DECL_CHAIN (decomp_first);
17512 }
17513 }
17514 }
17515 decl = tsubst_decl (decl, args, complain);
17516 if (!decomp)
17517 maybe_push_decl (decl);
17518 }
17519 else if (init && TREE_CODE (init) == DECL_EXPR)
17520 {
17521 /* We need to jump through some hoops to handle declarations in the
17522 init-statement, since we might need to handle auto deduction,
17523 but we need to keep control of initialization. */
17524 decl_expr = init;
17525 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17526 decl = tsubst_decl (decl, args, complain);
17527 }
17528 else
17529 {
17530 if (TREE_CODE (decl) == SCOPE_REF)
17531 {
17532 decl = RECUR (decl);
17533 if (TREE_CODE (decl) == COMPONENT_REF)
17534 {
17535 tree v = decl;
17536 while (v)
17537 switch (TREE_CODE (v))
17538 {
17539 case COMPONENT_REF:
17540 case MEM_REF:
17541 case INDIRECT_REF:
17542 CASE_CONVERT:
17543 case POINTER_PLUS_EXPR:
17544 v = TREE_OPERAND (v, 0);
17545 continue;
17546 case PARM_DECL:
17547 if (DECL_CONTEXT (v) == current_function_decl
17548 && DECL_ARTIFICIAL (v)
17549 && DECL_NAME (v) == this_identifier)
17550 {
17551 decl = TREE_OPERAND (decl, 1);
17552 decl = omp_privatize_field (decl, false);
17553 }
17554 /* FALLTHRU */
17555 default:
17556 v = NULL_TREE;
17557 break;
17558 }
17559 }
17560 }
17561 else
17562 decl = RECUR (decl);
17563 }
17564 if (init && TREE_CODE (init) == TREE_VEC)
17565 {
17566 init = copy_node (init);
17567 TREE_VEC_ELT (init, 0)
17568 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17569 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17570 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17571 }
17572 else
17573 init = RECUR (init);
17574
17575 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17576 {
17577 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17578 if (TREE_CODE (o) == TREE_LIST)
17579 TREE_VEC_ELT (orig_declv, i)
17580 = tree_cons (RECUR (TREE_PURPOSE (o)),
17581 RECUR (TREE_VALUE (o)),
17582 NULL_TREE);
17583 else
17584 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17585 }
17586
17587 if (range_for)
17588 {
17589 tree this_pre_body = NULL_TREE;
17590 tree orig_init = NULL_TREE;
17591 tree orig_decl = NULL_TREE;
17592 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17593 orig_init, cond, incr);
17594 if (orig_decl)
17595 {
17596 if (orig_declv == NULL_TREE)
17597 orig_declv = copy_node (declv);
17598 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17599 ret = true;
17600 }
17601 else if (orig_declv)
17602 TREE_VEC_ELT (orig_declv, i) = decl;
17603 }
17604
17605 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17606 if (!range_for && auto_node && init)
17607 TREE_TYPE (decl)
17608 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17609
17610 gcc_assert (!type_dependent_expression_p (decl));
17611
17612 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17613 {
17614 if (decl_expr)
17615 {
17616 /* Declare the variable, but don't let that initialize it. */
17617 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17618 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17619 RECUR (decl_expr);
17620 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17621 }
17622
17623 if (!range_for)
17624 {
17625 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17626 if (COMPARISON_CLASS_P (cond)
17627 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17628 {
17629 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17630 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17631 TREE_VEC_ELT (rhs, 0)
17632 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17633 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17634 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17635 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17636 lhs, rhs);
17637 }
17638 else
17639 cond = RECUR (cond);
17640 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17641 if (TREE_CODE (incr) == MODIFY_EXPR)
17642 {
17643 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17644 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17645 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17646 NOP_EXPR, rhs, complain);
17647 }
17648 else
17649 incr = RECUR (incr);
17650 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17651 TREE_VEC_ELT (orig_declv, i) = decl;
17652 }
17653 TREE_VEC_ELT (declv, i) = decl;
17654 TREE_VEC_ELT (initv, i) = init;
17655 TREE_VEC_ELT (condv, i) = cond;
17656 TREE_VEC_ELT (incrv, i) = incr;
17657 return ret;
17658 }
17659
17660 if (decl_expr)
17661 {
17662 /* Declare and initialize the variable. */
17663 RECUR (decl_expr);
17664 init = NULL_TREE;
17665 }
17666 else if (init)
17667 {
17668 tree *pc;
17669 int j;
17670 for (j = ((omp_parallel_combined_clauses == NULL
17671 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17672 {
17673 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17674 {
17675 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17676 && OMP_CLAUSE_DECL (*pc) == decl)
17677 break;
17678 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17679 && OMP_CLAUSE_DECL (*pc) == decl)
17680 {
17681 if (j)
17682 break;
17683 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17684 tree c = *pc;
17685 *pc = OMP_CLAUSE_CHAIN (c);
17686 OMP_CLAUSE_CHAIN (c) = *clauses;
17687 *clauses = c;
17688 }
17689 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17690 && OMP_CLAUSE_DECL (*pc) == decl)
17691 {
17692 error ("iteration variable %qD should not be firstprivate",
17693 decl);
17694 *pc = OMP_CLAUSE_CHAIN (*pc);
17695 }
17696 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17697 && OMP_CLAUSE_DECL (*pc) == decl)
17698 {
17699 error ("iteration variable %qD should not be reduction",
17700 decl);
17701 *pc = OMP_CLAUSE_CHAIN (*pc);
17702 }
17703 else
17704 pc = &OMP_CLAUSE_CHAIN (*pc);
17705 }
17706 if (*pc)
17707 break;
17708 }
17709 if (*pc == NULL_TREE)
17710 {
17711 tree c = build_omp_clause (input_location,
17712 TREE_CODE (t) == OMP_LOOP
17713 ? OMP_CLAUSE_LASTPRIVATE
17714 : OMP_CLAUSE_PRIVATE);
17715 OMP_CLAUSE_DECL (c) = decl;
17716 c = finish_omp_clauses (c, C_ORT_OMP);
17717 if (c)
17718 {
17719 OMP_CLAUSE_CHAIN (c) = *clauses;
17720 *clauses = c;
17721 }
17722 }
17723 }
17724 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17725 if (COMPARISON_CLASS_P (cond))
17726 {
17727 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17728 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17729 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17730 }
17731 else
17732 cond = RECUR (cond);
17733 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17734 switch (TREE_CODE (incr))
17735 {
17736 case PREINCREMENT_EXPR:
17737 case PREDECREMENT_EXPR:
17738 case POSTINCREMENT_EXPR:
17739 case POSTDECREMENT_EXPR:
17740 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17741 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17742 break;
17743 case MODIFY_EXPR:
17744 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17745 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17746 {
17747 tree rhs = TREE_OPERAND (incr, 1);
17748 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17749 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17750 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17751 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17752 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17753 rhs0, rhs1));
17754 }
17755 else
17756 incr = RECUR (incr);
17757 break;
17758 case MODOP_EXPR:
17759 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17760 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17761 {
17762 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17763 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17764 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17765 TREE_TYPE (decl), lhs,
17766 RECUR (TREE_OPERAND (incr, 2))));
17767 }
17768 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17769 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17770 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17771 {
17772 tree rhs = TREE_OPERAND (incr, 2);
17773 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17774 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17775 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17776 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17777 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17778 rhs0, rhs1));
17779 }
17780 else
17781 incr = RECUR (incr);
17782 break;
17783 default:
17784 incr = RECUR (incr);
17785 break;
17786 }
17787
17788 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17789 TREE_VEC_ELT (orig_declv, i) = decl;
17790 TREE_VEC_ELT (declv, i) = decl;
17791 TREE_VEC_ELT (initv, i) = init;
17792 TREE_VEC_ELT (condv, i) = cond;
17793 TREE_VEC_ELT (incrv, i) = incr;
17794 return false;
17795 #undef RECUR
17796 }
17797
17798 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17799 of OMP_TARGET's body. */
17800
17801 static tree
17802 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17803 {
17804 *walk_subtrees = 0;
17805 switch (TREE_CODE (*tp))
17806 {
17807 case OMP_TEAMS:
17808 return *tp;
17809 case BIND_EXPR:
17810 case STATEMENT_LIST:
17811 *walk_subtrees = 1;
17812 break;
17813 default:
17814 break;
17815 }
17816 return NULL_TREE;
17817 }
17818
17819 /* Helper function for tsubst_expr. For decomposition declaration
17820 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17821 also the corresponding decls representing the identifiers
17822 of the decomposition declaration. Return DECL if successful
17823 or error_mark_node otherwise, set *FIRST to the first decl
17824 in the list chained through DECL_CHAIN and *CNT to the number
17825 of such decls. */
17826
17827 static tree
17828 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17829 tsubst_flags_t complain, tree in_decl, tree *first,
17830 unsigned int *cnt)
17831 {
17832 tree decl2, decl3, prev = decl;
17833 *cnt = 0;
17834 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17835 for (decl2 = DECL_CHAIN (pattern_decl);
17836 decl2
17837 && VAR_P (decl2)
17838 && DECL_DECOMPOSITION_P (decl2)
17839 && DECL_NAME (decl2);
17840 decl2 = DECL_CHAIN (decl2))
17841 {
17842 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17843 {
17844 gcc_assert (errorcount);
17845 return error_mark_node;
17846 }
17847 (*cnt)++;
17848 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17849 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17850 tree v = DECL_VALUE_EXPR (decl2);
17851 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17852 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17853 decl3 = tsubst (decl2, args, complain, in_decl);
17854 SET_DECL_VALUE_EXPR (decl2, v);
17855 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17856 if (VAR_P (decl3))
17857 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17858 else
17859 {
17860 gcc_assert (errorcount);
17861 decl = error_mark_node;
17862 continue;
17863 }
17864 maybe_push_decl (decl3);
17865 if (error_operand_p (decl3))
17866 decl = error_mark_node;
17867 else if (decl != error_mark_node
17868 && DECL_CHAIN (decl3) != prev
17869 && decl != prev)
17870 {
17871 gcc_assert (errorcount);
17872 decl = error_mark_node;
17873 }
17874 else
17875 prev = decl3;
17876 }
17877 *first = prev;
17878 return decl;
17879 }
17880
17881 /* Return the proper local_specialization for init-capture pack DECL. */
17882
17883 static tree
17884 lookup_init_capture_pack (tree decl)
17885 {
17886 /* We handle normal pack captures by forwarding to the specialization of the
17887 captured parameter. We can't do that for pack init-captures; we need them
17888 to have their own local_specialization. We created the individual
17889 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17890 when we process the DECL_EXPR for the pack init-capture in the template.
17891 So, how do we find them? We don't know the capture proxy pack when
17892 building the individual resulting proxies, and we don't know the
17893 individual proxies when instantiating the pack. What we have in common is
17894 the FIELD_DECL.
17895
17896 So...when we instantiate the FIELD_DECL, we stick the result in
17897 local_specializations. Then at the DECL_EXPR we look up that result, see
17898 how many elements it has, synthesize the names, and look them up. */
17899
17900 tree cname = DECL_NAME (decl);
17901 tree val = DECL_VALUE_EXPR (decl);
17902 tree field = TREE_OPERAND (val, 1);
17903 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17904 tree fpack = retrieve_local_specialization (field);
17905 if (fpack == error_mark_node)
17906 return error_mark_node;
17907
17908 int len = 1;
17909 tree vec = NULL_TREE;
17910 tree r = NULL_TREE;
17911 if (TREE_CODE (fpack) == TREE_VEC)
17912 {
17913 len = TREE_VEC_LENGTH (fpack);
17914 vec = make_tree_vec (len);
17915 r = make_node (NONTYPE_ARGUMENT_PACK);
17916 SET_ARGUMENT_PACK_ARGS (r, vec);
17917 }
17918 for (int i = 0; i < len; ++i)
17919 {
17920 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17921 tree elt = lookup_name (ename);
17922 if (vec)
17923 TREE_VEC_ELT (vec, i) = elt;
17924 else
17925 r = elt;
17926 }
17927 return r;
17928 }
17929
17930 /* Like tsubst_copy for expressions, etc. but also does semantic
17931 processing. */
17932
17933 tree
17934 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17935 bool integral_constant_expression_p)
17936 {
17937 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17938 #define RECUR(NODE) \
17939 tsubst_expr ((NODE), args, complain, in_decl, \
17940 integral_constant_expression_p)
17941
17942 tree stmt, tmp;
17943 tree r;
17944 location_t loc;
17945
17946 if (t == NULL_TREE || t == error_mark_node)
17947 return t;
17948
17949 loc = input_location;
17950 if (location_t eloc = cp_expr_location (t))
17951 input_location = eloc;
17952 if (STATEMENT_CODE_P (TREE_CODE (t)))
17953 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17954
17955 switch (TREE_CODE (t))
17956 {
17957 case STATEMENT_LIST:
17958 {
17959 tree_stmt_iterator i;
17960 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17961 RECUR (tsi_stmt (i));
17962 break;
17963 }
17964
17965 case CTOR_INITIALIZER:
17966 finish_mem_initializers (tsubst_initializer_list
17967 (TREE_OPERAND (t, 0), args));
17968 break;
17969
17970 case RETURN_EXPR:
17971 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17972 break;
17973
17974 case CO_RETURN_EXPR:
17975 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17976 break;
17977
17978 case CO_YIELD_EXPR:
17979 stmt = finish_co_yield_expr (input_location,
17980 RECUR (TREE_OPERAND (t, 0)));
17981 RETURN (stmt);
17982 break;
17983
17984 case CO_AWAIT_EXPR:
17985 stmt = finish_co_await_expr (input_location,
17986 RECUR (TREE_OPERAND (t, 0)));
17987 RETURN (stmt);
17988 break;
17989
17990 case EXPR_STMT:
17991 tmp = RECUR (EXPR_STMT_EXPR (t));
17992 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17993 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17994 else
17995 finish_expr_stmt (tmp);
17996 break;
17997
17998 case USING_STMT:
17999 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18000 break;
18001
18002 case DECL_EXPR:
18003 {
18004 tree decl, pattern_decl;
18005 tree init;
18006
18007 pattern_decl = decl = DECL_EXPR_DECL (t);
18008 if (TREE_CODE (decl) == LABEL_DECL)
18009 finish_label_decl (DECL_NAME (decl));
18010 else if (TREE_CODE (decl) == USING_DECL)
18011 {
18012 tree scope = USING_DECL_SCOPE (decl);
18013 tree name = DECL_NAME (decl);
18014
18015 scope = tsubst (scope, args, complain, in_decl);
18016 finish_nonmember_using_decl (scope, name);
18017 }
18018 else if (is_capture_proxy (decl)
18019 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18020 {
18021 /* We're in tsubst_lambda_expr, we've already inserted a new
18022 capture proxy, so look it up and register it. */
18023 tree inst;
18024 if (!DECL_PACK_P (decl))
18025 {
18026 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18027 LOOK_want::HIDDEN_LAMBDA);
18028 gcc_assert (inst != decl && is_capture_proxy (inst));
18029 }
18030 else if (is_normal_capture_proxy (decl))
18031 {
18032 inst = (retrieve_local_specialization
18033 (DECL_CAPTURED_VARIABLE (decl)));
18034 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18035 || DECL_PACK_P (inst));
18036 }
18037 else
18038 inst = lookup_init_capture_pack (decl);
18039
18040 register_local_specialization (inst, decl);
18041 break;
18042 }
18043 else if (DECL_PRETTY_FUNCTION_P (decl))
18044 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18045 DECL_NAME (decl),
18046 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18047 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18048 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18049 /* Don't copy the old closure; we'll create a new one in
18050 tsubst_lambda_expr. */
18051 break;
18052 else
18053 {
18054 init = DECL_INITIAL (decl);
18055 decl = tsubst (decl, args, complain, in_decl);
18056 if (decl != error_mark_node)
18057 {
18058 /* By marking the declaration as instantiated, we avoid
18059 trying to instantiate it. Since instantiate_decl can't
18060 handle local variables, and since we've already done
18061 all that needs to be done, that's the right thing to
18062 do. */
18063 if (VAR_P (decl))
18064 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18065 if (VAR_P (decl) && !DECL_NAME (decl)
18066 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18067 /* Anonymous aggregates are a special case. */
18068 finish_anon_union (decl);
18069 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18070 {
18071 DECL_CONTEXT (decl) = current_function_decl;
18072 if (DECL_NAME (decl) == this_identifier)
18073 {
18074 tree lam = DECL_CONTEXT (current_function_decl);
18075 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18076 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18077 }
18078 insert_capture_proxy (decl);
18079 }
18080 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18081 /* We already did a pushtag. */;
18082 else if (TREE_CODE (decl) == FUNCTION_DECL
18083 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18084 && DECL_FUNCTION_SCOPE_P (pattern_decl))
18085 {
18086 /* We pretend this is regular local extern decl of
18087 a namespace-scope fn. Then we make it really
18088 local, it is a nested function. */
18089 gcc_checking_assert (DECL_LOCAL_DECL_P (decl));
18090 DECL_CONTEXT (decl) = global_namespace;
18091 pushdecl (decl);
18092 DECL_CONTEXT (decl) = current_function_decl;
18093 if (cp_check_omp_declare_reduction (decl))
18094 instantiate_body (pattern_decl, args, decl, true);
18095 }
18096 else
18097 {
18098 bool const_init = false;
18099 unsigned int cnt = 0;
18100 tree first = NULL_TREE, ndecl = error_mark_node;
18101 maybe_push_decl (decl);
18102
18103 if (VAR_P (decl)
18104 && DECL_DECOMPOSITION_P (decl)
18105 && TREE_TYPE (pattern_decl) != error_mark_node)
18106 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18107 complain, in_decl, &first,
18108 &cnt);
18109
18110 init = tsubst_init (init, decl, args, complain, in_decl);
18111
18112 if (VAR_P (decl))
18113 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18114 (pattern_decl));
18115
18116 if (ndecl != error_mark_node)
18117 cp_maybe_mangle_decomp (ndecl, first, cnt);
18118
18119 /* In a non-template function, VLA type declarations are
18120 handled in grokdeclarator; for templates, handle them
18121 now. */
18122 predeclare_vla (decl);
18123
18124 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18125
18126 if (ndecl != error_mark_node)
18127 cp_finish_decomp (ndecl, first, cnt);
18128 }
18129 }
18130 }
18131
18132 break;
18133 }
18134
18135 case FOR_STMT:
18136 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18137 RECUR (FOR_INIT_STMT (t));
18138 finish_init_stmt (stmt);
18139 tmp = RECUR (FOR_COND (t));
18140 finish_for_cond (tmp, stmt, false, 0);
18141 tmp = RECUR (FOR_EXPR (t));
18142 finish_for_expr (tmp, stmt);
18143 {
18144 bool prev = note_iteration_stmt_body_start ();
18145 RECUR (FOR_BODY (t));
18146 note_iteration_stmt_body_end (prev);
18147 }
18148 finish_for_stmt (stmt);
18149 break;
18150
18151 case RANGE_FOR_STMT:
18152 {
18153 /* Construct another range_for, if this is not a final
18154 substitution (for inside a generic lambda of a
18155 template). Otherwise convert to a regular for. */
18156 tree decl, expr;
18157 stmt = (processing_template_decl
18158 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18159 : begin_for_stmt (NULL_TREE, NULL_TREE));
18160 RECUR (RANGE_FOR_INIT_STMT (t));
18161 decl = RANGE_FOR_DECL (t);
18162 decl = tsubst (decl, args, complain, in_decl);
18163 maybe_push_decl (decl);
18164 expr = RECUR (RANGE_FOR_EXPR (t));
18165
18166 tree decomp_first = NULL_TREE;
18167 unsigned decomp_cnt = 0;
18168 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18169 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18170 complain, in_decl,
18171 &decomp_first, &decomp_cnt);
18172
18173 if (processing_template_decl)
18174 {
18175 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18176 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18177 finish_range_for_decl (stmt, decl, expr);
18178 if (decomp_first && decl != error_mark_node)
18179 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18180 }
18181 else
18182 {
18183 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18184 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18185 stmt = cp_convert_range_for (stmt, decl, expr,
18186 decomp_first, decomp_cnt,
18187 RANGE_FOR_IVDEP (t), unroll);
18188 }
18189
18190 bool prev = note_iteration_stmt_body_start ();
18191 RECUR (RANGE_FOR_BODY (t));
18192 note_iteration_stmt_body_end (prev);
18193 finish_for_stmt (stmt);
18194 }
18195 break;
18196
18197 case WHILE_STMT:
18198 stmt = begin_while_stmt ();
18199 tmp = RECUR (WHILE_COND (t));
18200 finish_while_stmt_cond (tmp, stmt, false, 0);
18201 {
18202 bool prev = note_iteration_stmt_body_start ();
18203 RECUR (WHILE_BODY (t));
18204 note_iteration_stmt_body_end (prev);
18205 }
18206 finish_while_stmt (stmt);
18207 break;
18208
18209 case DO_STMT:
18210 stmt = begin_do_stmt ();
18211 {
18212 bool prev = note_iteration_stmt_body_start ();
18213 RECUR (DO_BODY (t));
18214 note_iteration_stmt_body_end (prev);
18215 }
18216 finish_do_body (stmt);
18217 tmp = RECUR (DO_COND (t));
18218 finish_do_stmt (tmp, stmt, false, 0);
18219 break;
18220
18221 case IF_STMT:
18222 stmt = begin_if_stmt ();
18223 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18224 if (IF_STMT_CONSTEXPR_P (t))
18225 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18226 tmp = RECUR (IF_COND (t));
18227 tmp = finish_if_stmt_cond (tmp, stmt);
18228 if (IF_STMT_CONSTEXPR_P (t)
18229 && instantiation_dependent_expression_p (tmp))
18230 {
18231 /* We're partially instantiating a generic lambda, but the condition
18232 of the constexpr if is still dependent. Don't substitute into the
18233 branches now, just remember the template arguments. */
18234 do_poplevel (IF_SCOPE (stmt));
18235 IF_COND (stmt) = IF_COND (t);
18236 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18237 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18238 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18239 add_stmt (stmt);
18240 break;
18241 }
18242 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18243 /* Don't instantiate the THEN_CLAUSE. */;
18244 else
18245 {
18246 tree folded = fold_non_dependent_expr (tmp, complain);
18247 bool inhibit = integer_zerop (folded);
18248 if (inhibit)
18249 ++c_inhibit_evaluation_warnings;
18250 RECUR (THEN_CLAUSE (t));
18251 if (inhibit)
18252 --c_inhibit_evaluation_warnings;
18253 }
18254 finish_then_clause (stmt);
18255
18256 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18257 /* Don't instantiate the ELSE_CLAUSE. */;
18258 else if (ELSE_CLAUSE (t))
18259 {
18260 tree folded = fold_non_dependent_expr (tmp, complain);
18261 bool inhibit = integer_nonzerop (folded);
18262 begin_else_clause (stmt);
18263 if (inhibit)
18264 ++c_inhibit_evaluation_warnings;
18265 RECUR (ELSE_CLAUSE (t));
18266 if (inhibit)
18267 --c_inhibit_evaluation_warnings;
18268 finish_else_clause (stmt);
18269 }
18270
18271 finish_if_stmt (stmt);
18272 break;
18273
18274 case BIND_EXPR:
18275 if (BIND_EXPR_BODY_BLOCK (t))
18276 stmt = begin_function_body ();
18277 else
18278 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18279 ? BCS_TRY_BLOCK : 0);
18280
18281 RECUR (BIND_EXPR_BODY (t));
18282
18283 if (BIND_EXPR_BODY_BLOCK (t))
18284 finish_function_body (stmt);
18285 else
18286 finish_compound_stmt (stmt);
18287 break;
18288
18289 case BREAK_STMT:
18290 finish_break_stmt ();
18291 break;
18292
18293 case CONTINUE_STMT:
18294 finish_continue_stmt ();
18295 break;
18296
18297 case SWITCH_STMT:
18298 stmt = begin_switch_stmt ();
18299 tmp = RECUR (SWITCH_STMT_COND (t));
18300 finish_switch_cond (tmp, stmt);
18301 RECUR (SWITCH_STMT_BODY (t));
18302 finish_switch_stmt (stmt);
18303 break;
18304
18305 case CASE_LABEL_EXPR:
18306 {
18307 tree decl = CASE_LABEL (t);
18308 tree low = RECUR (CASE_LOW (t));
18309 tree high = RECUR (CASE_HIGH (t));
18310 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18311 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18312 {
18313 tree label = CASE_LABEL (l);
18314 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18315 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18316 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18317 }
18318 }
18319 break;
18320
18321 case LABEL_EXPR:
18322 {
18323 tree decl = LABEL_EXPR_LABEL (t);
18324 tree label;
18325
18326 label = finish_label_stmt (DECL_NAME (decl));
18327 if (TREE_CODE (label) == LABEL_DECL)
18328 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18329 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18330 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18331 }
18332 break;
18333
18334 case GOTO_EXPR:
18335 tmp = GOTO_DESTINATION (t);
18336 if (TREE_CODE (tmp) != LABEL_DECL)
18337 /* Computed goto's must be tsubst'd into. On the other hand,
18338 non-computed gotos must not be; the identifier in question
18339 will have no binding. */
18340 tmp = RECUR (tmp);
18341 else
18342 tmp = DECL_NAME (tmp);
18343 finish_goto_stmt (tmp);
18344 break;
18345
18346 case ASM_EXPR:
18347 {
18348 tree string = RECUR (ASM_STRING (t));
18349 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18350 complain, in_decl);
18351 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18352 complain, in_decl);
18353 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18354 complain, in_decl);
18355 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18356 complain, in_decl);
18357 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18358 outputs, inputs, clobbers, labels,
18359 ASM_INLINE_P (t));
18360 tree asm_expr = tmp;
18361 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18362 asm_expr = TREE_OPERAND (asm_expr, 0);
18363 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18364 }
18365 break;
18366
18367 case TRY_BLOCK:
18368 if (CLEANUP_P (t))
18369 {
18370 stmt = begin_try_block ();
18371 RECUR (TRY_STMTS (t));
18372 finish_cleanup_try_block (stmt);
18373 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18374 }
18375 else
18376 {
18377 tree compound_stmt = NULL_TREE;
18378
18379 if (FN_TRY_BLOCK_P (t))
18380 stmt = begin_function_try_block (&compound_stmt);
18381 else
18382 stmt = begin_try_block ();
18383
18384 RECUR (TRY_STMTS (t));
18385
18386 if (FN_TRY_BLOCK_P (t))
18387 finish_function_try_block (stmt);
18388 else
18389 finish_try_block (stmt);
18390
18391 RECUR (TRY_HANDLERS (t));
18392 if (FN_TRY_BLOCK_P (t))
18393 finish_function_handler_sequence (stmt, compound_stmt);
18394 else
18395 finish_handler_sequence (stmt);
18396 }
18397 break;
18398
18399 case HANDLER:
18400 {
18401 tree decl = HANDLER_PARMS (t);
18402
18403 if (decl)
18404 {
18405 decl = tsubst (decl, args, complain, in_decl);
18406 /* Prevent instantiate_decl from trying to instantiate
18407 this variable. We've already done all that needs to be
18408 done. */
18409 if (decl != error_mark_node)
18410 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18411 }
18412 stmt = begin_handler ();
18413 finish_handler_parms (decl, stmt);
18414 RECUR (HANDLER_BODY (t));
18415 finish_handler (stmt);
18416 }
18417 break;
18418
18419 case TAG_DEFN:
18420 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18421 if (CLASS_TYPE_P (tmp))
18422 {
18423 /* Local classes are not independent templates; they are
18424 instantiated along with their containing function. And this
18425 way we don't have to deal with pushing out of one local class
18426 to instantiate a member of another local class. */
18427 /* Closures are handled by the LAMBDA_EXPR. */
18428 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18429 complete_type (tmp);
18430 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18431 if ((VAR_P (fld)
18432 || (TREE_CODE (fld) == FUNCTION_DECL
18433 && !DECL_ARTIFICIAL (fld)))
18434 && DECL_TEMPLATE_INSTANTIATION (fld))
18435 instantiate_decl (fld, /*defer_ok=*/false,
18436 /*expl_inst_class=*/false);
18437 }
18438 break;
18439
18440 case STATIC_ASSERT:
18441 {
18442 tree condition;
18443
18444 ++c_inhibit_evaluation_warnings;
18445 condition =
18446 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18447 args,
18448 complain, in_decl,
18449 /*integral_constant_expression_p=*/true);
18450 --c_inhibit_evaluation_warnings;
18451
18452 finish_static_assert (condition,
18453 STATIC_ASSERT_MESSAGE (t),
18454 STATIC_ASSERT_SOURCE_LOCATION (t),
18455 /*member_p=*/false);
18456 }
18457 break;
18458
18459 case OACC_KERNELS:
18460 case OACC_PARALLEL:
18461 case OACC_SERIAL:
18462 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18463 in_decl);
18464 stmt = begin_omp_parallel ();
18465 RECUR (OMP_BODY (t));
18466 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18467 break;
18468
18469 case OMP_PARALLEL:
18470 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18471 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18472 complain, in_decl);
18473 if (OMP_PARALLEL_COMBINED (t))
18474 omp_parallel_combined_clauses = &tmp;
18475 stmt = begin_omp_parallel ();
18476 RECUR (OMP_PARALLEL_BODY (t));
18477 gcc_assert (omp_parallel_combined_clauses == NULL);
18478 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18479 = OMP_PARALLEL_COMBINED (t);
18480 pop_omp_privatization_clauses (r);
18481 break;
18482
18483 case OMP_TASK:
18484 if (OMP_TASK_BODY (t) == NULL_TREE)
18485 {
18486 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18487 complain, in_decl);
18488 t = copy_node (t);
18489 OMP_TASK_CLAUSES (t) = tmp;
18490 add_stmt (t);
18491 break;
18492 }
18493 r = push_omp_privatization_clauses (false);
18494 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18495 complain, in_decl);
18496 stmt = begin_omp_task ();
18497 RECUR (OMP_TASK_BODY (t));
18498 finish_omp_task (tmp, stmt);
18499 pop_omp_privatization_clauses (r);
18500 break;
18501
18502 case OMP_FOR:
18503 case OMP_LOOP:
18504 case OMP_SIMD:
18505 case OMP_DISTRIBUTE:
18506 case OMP_TASKLOOP:
18507 case OACC_LOOP:
18508 {
18509 tree clauses, body, pre_body;
18510 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18511 tree orig_declv = NULL_TREE;
18512 tree incrv = NULL_TREE;
18513 enum c_omp_region_type ort = C_ORT_OMP;
18514 bool any_range_for = false;
18515 int i;
18516
18517 if (TREE_CODE (t) == OACC_LOOP)
18518 ort = C_ORT_ACC;
18519
18520 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18521 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18522 in_decl);
18523 if (OMP_FOR_INIT (t) != NULL_TREE)
18524 {
18525 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18526 if (OMP_FOR_ORIG_DECLS (t))
18527 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18528 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18529 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18530 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18531 }
18532
18533 keep_next_level (true);
18534 stmt = begin_omp_structured_block ();
18535
18536 pre_body = push_stmt_list ();
18537 RECUR (OMP_FOR_PRE_BODY (t));
18538 pre_body = pop_stmt_list (pre_body);
18539
18540 if (OMP_FOR_INIT (t) != NULL_TREE)
18541 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18542 any_range_for
18543 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18544 condv, incrv, &clauses, args,
18545 complain, in_decl,
18546 integral_constant_expression_p);
18547 omp_parallel_combined_clauses = NULL;
18548
18549 if (any_range_for)
18550 {
18551 gcc_assert (orig_declv);
18552 body = begin_omp_structured_block ();
18553 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18554 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18555 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18556 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18557 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18558 TREE_VEC_ELT (declv, i));
18559 }
18560 else
18561 body = push_stmt_list ();
18562 RECUR (OMP_FOR_BODY (t));
18563 if (any_range_for)
18564 body = finish_omp_structured_block (body);
18565 else
18566 body = pop_stmt_list (body);
18567
18568 if (OMP_FOR_INIT (t) != NULL_TREE)
18569 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18570 orig_declv, initv, condv, incrv, body, pre_body,
18571 NULL, clauses);
18572 else
18573 {
18574 t = make_node (TREE_CODE (t));
18575 TREE_TYPE (t) = void_type_node;
18576 OMP_FOR_BODY (t) = body;
18577 OMP_FOR_PRE_BODY (t) = pre_body;
18578 OMP_FOR_CLAUSES (t) = clauses;
18579 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18580 add_stmt (t);
18581 }
18582
18583 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18584 t));
18585 pop_omp_privatization_clauses (r);
18586 }
18587 break;
18588
18589 case OMP_SECTIONS:
18590 omp_parallel_combined_clauses = NULL;
18591 /* FALLTHRU */
18592 case OMP_SINGLE:
18593 case OMP_TEAMS:
18594 case OMP_CRITICAL:
18595 case OMP_TASKGROUP:
18596 case OMP_SCAN:
18597 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18598 && OMP_TEAMS_COMBINED (t));
18599 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18600 in_decl);
18601 if (TREE_CODE (t) == OMP_TEAMS)
18602 {
18603 keep_next_level (true);
18604 stmt = begin_omp_structured_block ();
18605 RECUR (OMP_BODY (t));
18606 stmt = finish_omp_structured_block (stmt);
18607 }
18608 else
18609 {
18610 stmt = push_stmt_list ();
18611 RECUR (OMP_BODY (t));
18612 stmt = pop_stmt_list (stmt);
18613 }
18614
18615 if (TREE_CODE (t) == OMP_CRITICAL
18616 && tmp != NULL_TREE
18617 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18618 {
18619 error_at (OMP_CLAUSE_LOCATION (tmp),
18620 "%<#pragma omp critical%> with %<hint%> clause requires "
18621 "a name, except when %<omp_sync_hint_none%> is used");
18622 RETURN (error_mark_node);
18623 }
18624 t = copy_node (t);
18625 OMP_BODY (t) = stmt;
18626 OMP_CLAUSES (t) = tmp;
18627 add_stmt (t);
18628 pop_omp_privatization_clauses (r);
18629 break;
18630
18631 case OMP_DEPOBJ:
18632 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18633 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18634 {
18635 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18636 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18637 {
18638 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18639 args, complain, in_decl);
18640 if (tmp == NULL_TREE)
18641 tmp = error_mark_node;
18642 }
18643 else
18644 {
18645 kind = (enum omp_clause_depend_kind)
18646 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18647 tmp = NULL_TREE;
18648 }
18649 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18650 }
18651 else
18652 finish_omp_depobj (EXPR_LOCATION (t), r,
18653 OMP_CLAUSE_DEPEND_SOURCE,
18654 OMP_DEPOBJ_CLAUSES (t));
18655 break;
18656
18657 case OACC_DATA:
18658 case OMP_TARGET_DATA:
18659 case OMP_TARGET:
18660 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18661 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18662 in_decl);
18663 keep_next_level (true);
18664 stmt = begin_omp_structured_block ();
18665
18666 RECUR (OMP_BODY (t));
18667 stmt = finish_omp_structured_block (stmt);
18668
18669 t = copy_node (t);
18670 OMP_BODY (t) = stmt;
18671 OMP_CLAUSES (t) = tmp;
18672 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18673 {
18674 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18675 if (teams)
18676 {
18677 /* For combined target teams, ensure the num_teams and
18678 thread_limit clause expressions are evaluated on the host,
18679 before entering the target construct. */
18680 tree c;
18681 for (c = OMP_TEAMS_CLAUSES (teams);
18682 c; c = OMP_CLAUSE_CHAIN (c))
18683 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18684 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18685 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18686 {
18687 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18688 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18689 if (expr == error_mark_node)
18690 continue;
18691 tmp = TARGET_EXPR_SLOT (expr);
18692 add_stmt (expr);
18693 OMP_CLAUSE_OPERAND (c, 0) = expr;
18694 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18695 OMP_CLAUSE_FIRSTPRIVATE);
18696 OMP_CLAUSE_DECL (tc) = tmp;
18697 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18698 OMP_TARGET_CLAUSES (t) = tc;
18699 }
18700 }
18701 }
18702 add_stmt (t);
18703 break;
18704
18705 case OACC_DECLARE:
18706 t = copy_node (t);
18707 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18708 complain, in_decl);
18709 OACC_DECLARE_CLAUSES (t) = tmp;
18710 add_stmt (t);
18711 break;
18712
18713 case OMP_TARGET_UPDATE:
18714 case OMP_TARGET_ENTER_DATA:
18715 case OMP_TARGET_EXIT_DATA:
18716 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18717 complain, in_decl);
18718 t = copy_node (t);
18719 OMP_STANDALONE_CLAUSES (t) = tmp;
18720 add_stmt (t);
18721 break;
18722
18723 case OACC_ENTER_DATA:
18724 case OACC_EXIT_DATA:
18725 case OACC_UPDATE:
18726 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18727 complain, in_decl);
18728 t = copy_node (t);
18729 OMP_STANDALONE_CLAUSES (t) = tmp;
18730 add_stmt (t);
18731 break;
18732
18733 case OMP_ORDERED:
18734 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18735 complain, in_decl);
18736 stmt = push_stmt_list ();
18737 RECUR (OMP_BODY (t));
18738 stmt = pop_stmt_list (stmt);
18739
18740 t = copy_node (t);
18741 OMP_BODY (t) = stmt;
18742 OMP_ORDERED_CLAUSES (t) = tmp;
18743 add_stmt (t);
18744 break;
18745
18746 case OMP_MASTER:
18747 omp_parallel_combined_clauses = NULL;
18748 /* FALLTHRU */
18749 case OMP_SECTION:
18750 stmt = push_stmt_list ();
18751 RECUR (OMP_BODY (t));
18752 stmt = pop_stmt_list (stmt);
18753
18754 t = copy_node (t);
18755 OMP_BODY (t) = stmt;
18756 add_stmt (t);
18757 break;
18758
18759 case OMP_ATOMIC:
18760 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18761 tmp = NULL_TREE;
18762 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18763 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18764 complain, in_decl);
18765 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18766 {
18767 tree op1 = TREE_OPERAND (t, 1);
18768 tree rhs1 = NULL_TREE;
18769 tree lhs, rhs;
18770 if (TREE_CODE (op1) == COMPOUND_EXPR)
18771 {
18772 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18773 op1 = TREE_OPERAND (op1, 1);
18774 }
18775 lhs = RECUR (TREE_OPERAND (op1, 0));
18776 rhs = RECUR (TREE_OPERAND (op1, 1));
18777 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18778 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18779 OMP_ATOMIC_MEMORY_ORDER (t));
18780 }
18781 else
18782 {
18783 tree op1 = TREE_OPERAND (t, 1);
18784 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18785 tree rhs1 = NULL_TREE;
18786 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18787 enum tree_code opcode = NOP_EXPR;
18788 if (code == OMP_ATOMIC_READ)
18789 {
18790 v = RECUR (TREE_OPERAND (op1, 0));
18791 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18792 }
18793 else if (code == OMP_ATOMIC_CAPTURE_OLD
18794 || code == OMP_ATOMIC_CAPTURE_NEW)
18795 {
18796 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18797 v = RECUR (TREE_OPERAND (op1, 0));
18798 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18799 if (TREE_CODE (op11) == COMPOUND_EXPR)
18800 {
18801 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18802 op11 = TREE_OPERAND (op11, 1);
18803 }
18804 lhs = RECUR (TREE_OPERAND (op11, 0));
18805 rhs = RECUR (TREE_OPERAND (op11, 1));
18806 opcode = TREE_CODE (op11);
18807 if (opcode == MODIFY_EXPR)
18808 opcode = NOP_EXPR;
18809 }
18810 else
18811 {
18812 code = OMP_ATOMIC;
18813 lhs = RECUR (TREE_OPERAND (op1, 0));
18814 rhs = RECUR (TREE_OPERAND (op1, 1));
18815 }
18816 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18817 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18818 }
18819 break;
18820
18821 case TRANSACTION_EXPR:
18822 {
18823 int flags = 0;
18824 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18825 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18826
18827 if (TRANSACTION_EXPR_IS_STMT (t))
18828 {
18829 tree body = TRANSACTION_EXPR_BODY (t);
18830 tree noex = NULL_TREE;
18831 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18832 {
18833 noex = MUST_NOT_THROW_COND (body);
18834 if (noex == NULL_TREE)
18835 noex = boolean_true_node;
18836 body = TREE_OPERAND (body, 0);
18837 }
18838 stmt = begin_transaction_stmt (input_location, NULL, flags);
18839 RECUR (body);
18840 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18841 }
18842 else
18843 {
18844 stmt = build_transaction_expr (EXPR_LOCATION (t),
18845 RECUR (TRANSACTION_EXPR_BODY (t)),
18846 flags, NULL_TREE);
18847 RETURN (stmt);
18848 }
18849 }
18850 break;
18851
18852 case MUST_NOT_THROW_EXPR:
18853 {
18854 tree op0 = RECUR (TREE_OPERAND (t, 0));
18855 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18856 RETURN (build_must_not_throw_expr (op0, cond));
18857 }
18858
18859 case EXPR_PACK_EXPANSION:
18860 error ("invalid use of pack expansion expression");
18861 RETURN (error_mark_node);
18862
18863 case NONTYPE_ARGUMENT_PACK:
18864 error ("use %<...%> to expand argument pack");
18865 RETURN (error_mark_node);
18866
18867 case COMPOUND_EXPR:
18868 tmp = RECUR (TREE_OPERAND (t, 0));
18869 if (tmp == NULL_TREE)
18870 /* If the first operand was a statement, we're done with it. */
18871 RETURN (RECUR (TREE_OPERAND (t, 1)));
18872 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18873 RECUR (TREE_OPERAND (t, 1)),
18874 complain));
18875
18876 case ANNOTATE_EXPR:
18877 tmp = RECUR (TREE_OPERAND (t, 0));
18878 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18879 TREE_TYPE (tmp), tmp,
18880 RECUR (TREE_OPERAND (t, 1)),
18881 RECUR (TREE_OPERAND (t, 2))));
18882
18883 case PREDICT_EXPR:
18884 RETURN (add_stmt (copy_node (t)));
18885
18886 default:
18887 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18888
18889 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18890 /*function_p=*/false,
18891 integral_constant_expression_p));
18892 }
18893
18894 RETURN (NULL_TREE);
18895 out:
18896 input_location = loc;
18897 return r;
18898 #undef RECUR
18899 #undef RETURN
18900 }
18901
18902 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18903 function. For description of the body see comment above
18904 cp_parser_omp_declare_reduction_exprs. */
18905
18906 static void
18907 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18908 {
18909 if (t == NULL_TREE || t == error_mark_node)
18910 return;
18911
18912 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
18913
18914 tree_stmt_iterator tsi;
18915 int i;
18916 tree stmts[7];
18917 memset (stmts, 0, sizeof stmts);
18918 for (i = 0, tsi = tsi_start (t);
18919 i < 7 && !tsi_end_p (tsi);
18920 i++, tsi_next (&tsi))
18921 stmts[i] = tsi_stmt (tsi);
18922 gcc_assert (tsi_end_p (tsi));
18923
18924 if (i >= 3)
18925 {
18926 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18927 && TREE_CODE (stmts[1]) == DECL_EXPR);
18928 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18929 args, complain, in_decl);
18930 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18931 args, complain, in_decl);
18932 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18933 expect to be pushing it. */
18934 DECL_CONTEXT (omp_out) = current_function_decl;
18935 DECL_CONTEXT (omp_in) = current_function_decl;
18936 keep_next_level (true);
18937 tree block = begin_omp_structured_block ();
18938 tsubst_expr (stmts[2], args, complain, in_decl, false);
18939 block = finish_omp_structured_block (block);
18940 block = maybe_cleanup_point_expr_void (block);
18941 add_decl_expr (omp_out);
18942 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18943 TREE_NO_WARNING (omp_out) = 1;
18944 add_decl_expr (omp_in);
18945 finish_expr_stmt (block);
18946 }
18947 if (i >= 6)
18948 {
18949 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18950 && TREE_CODE (stmts[4]) == DECL_EXPR);
18951 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18952 args, complain, in_decl);
18953 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18954 args, complain, in_decl);
18955 DECL_CONTEXT (omp_priv) = current_function_decl;
18956 DECL_CONTEXT (omp_orig) = current_function_decl;
18957 keep_next_level (true);
18958 tree block = begin_omp_structured_block ();
18959 tsubst_expr (stmts[5], args, complain, in_decl, false);
18960 block = finish_omp_structured_block (block);
18961 block = maybe_cleanup_point_expr_void (block);
18962 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18963 add_decl_expr (omp_priv);
18964 add_decl_expr (omp_orig);
18965 finish_expr_stmt (block);
18966 if (i == 7)
18967 add_decl_expr (omp_orig);
18968 }
18969 }
18970
18971 /* T is a postfix-expression that is not being used in a function
18972 call. Return the substituted version of T. */
18973
18974 static tree
18975 tsubst_non_call_postfix_expression (tree t, tree args,
18976 tsubst_flags_t complain,
18977 tree in_decl)
18978 {
18979 if (TREE_CODE (t) == SCOPE_REF)
18980 t = tsubst_qualified_id (t, args, complain, in_decl,
18981 /*done=*/false, /*address_p=*/false);
18982 else
18983 t = tsubst_copy_and_build (t, args, complain, in_decl,
18984 /*function_p=*/false,
18985 /*integral_constant_expression_p=*/false);
18986
18987 return t;
18988 }
18989
18990 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18991 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18992 dependent init-capture. */
18993
18994 static void
18995 prepend_one_capture (tree field, tree init, tree &list,
18996 tsubst_flags_t complain)
18997 {
18998 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18999 {
19000 tree type = NULL_TREE;
19001 if (!init)
19002 {
19003 if (complain & tf_error)
19004 error ("empty initializer in lambda init-capture");
19005 init = error_mark_node;
19006 }
19007 else if (TREE_CODE (init) == TREE_LIST)
19008 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19009 if (!type)
19010 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19011 TREE_TYPE (field) = type;
19012 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19013 }
19014 list = tree_cons (field, init, list);
19015 }
19016
19017 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19018 instantiation context. Instantiating a pack expansion containing a lambda
19019 might result in multiple lambdas all based on the same lambda in the
19020 template. */
19021
19022 tree
19023 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19024 {
19025 tree oldfn = lambda_function (t);
19026 in_decl = oldfn;
19027
19028 tree r = build_lambda_expr ();
19029
19030 LAMBDA_EXPR_LOCATION (r)
19031 = LAMBDA_EXPR_LOCATION (t);
19032 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19033 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19034 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19035 LAMBDA_EXPR_INSTANTIATED (r) = true;
19036
19037 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19038 /* A lambda in a default argument outside a class gets no
19039 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19040 tsubst_default_argument calls start_lambda_scope, so we need to
19041 specifically ignore it here, and use the global scope. */
19042 record_null_lambda_scope (r);
19043 else
19044 record_lambda_scope (r);
19045
19046 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19047 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19048
19049 vec<tree,va_gc>* field_packs = NULL;
19050
19051 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19052 cap = TREE_CHAIN (cap))
19053 {
19054 tree ofield = TREE_PURPOSE (cap);
19055 tree init = TREE_VALUE (cap);
19056 if (PACK_EXPANSION_P (init))
19057 init = tsubst_pack_expansion (init, args, complain, in_decl);
19058 else
19059 init = tsubst_copy_and_build (init, args, complain, in_decl,
19060 /*fn*/false, /*constexpr*/false);
19061
19062 if (init == error_mark_node)
19063 return error_mark_node;
19064
19065 if (init && TREE_CODE (init) == TREE_LIST)
19066 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19067
19068 if (!processing_template_decl
19069 && init && TREE_CODE (init) != TREE_VEC
19070 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19071 {
19072 /* For a VLA, simply tsubsting the field type won't work, we need to
19073 go through add_capture again. XXX do we want to do this for all
19074 captures? */
19075 tree name = (get_identifier
19076 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19077 tree ftype = TREE_TYPE (ofield);
19078 bool by_ref = (TYPE_REF_P (ftype)
19079 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19080 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19081 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19082 continue;
19083 }
19084
19085 if (PACK_EXPANSION_P (ofield))
19086 ofield = PACK_EXPANSION_PATTERN (ofield);
19087 tree field = tsubst_decl (ofield, args, complain);
19088
19089 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19090 {
19091 /* Remember these for when we've pushed local_specializations. */
19092 vec_safe_push (field_packs, ofield);
19093 vec_safe_push (field_packs, field);
19094 }
19095
19096 if (field == error_mark_node)
19097 return error_mark_node;
19098
19099 if (TREE_CODE (field) == TREE_VEC)
19100 {
19101 int len = TREE_VEC_LENGTH (field);
19102 gcc_assert (TREE_CODE (init) == TREE_VEC
19103 && TREE_VEC_LENGTH (init) == len);
19104 for (int i = 0; i < len; ++i)
19105 prepend_one_capture (TREE_VEC_ELT (field, i),
19106 TREE_VEC_ELT (init, i),
19107 LAMBDA_EXPR_CAPTURE_LIST (r),
19108 complain);
19109 }
19110 else
19111 {
19112 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19113 complain);
19114
19115 if (id_equal (DECL_NAME (field), "__this"))
19116 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19117 }
19118 }
19119
19120 tree type = begin_lambda_type (r);
19121 if (type == error_mark_node)
19122 return error_mark_node;
19123
19124 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19125 determine_visibility (TYPE_NAME (type));
19126
19127 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19128
19129 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19130 ? DECL_TI_TEMPLATE (oldfn)
19131 : NULL_TREE);
19132
19133 tree fntype = static_fn_type (oldfn);
19134 if (oldtmpl)
19135 ++processing_template_decl;
19136 fntype = tsubst (fntype, args, complain, in_decl);
19137 if (oldtmpl)
19138 --processing_template_decl;
19139
19140 if (fntype == error_mark_node)
19141 r = error_mark_node;
19142 else
19143 {
19144 /* The body of a lambda-expression is not a subexpression of the
19145 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19146 which would be skipped if cp_unevaluated_operand. */
19147 cp_evaluated ev;
19148
19149 /* Fix the type of 'this'. */
19150 fntype = build_memfn_type (fntype, type,
19151 type_memfn_quals (fntype),
19152 type_memfn_rqual (fntype));
19153 tree fn, tmpl;
19154 if (oldtmpl)
19155 {
19156 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19157 if (tmpl == error_mark_node)
19158 {
19159 r = error_mark_node;
19160 goto out;
19161 }
19162 fn = DECL_TEMPLATE_RESULT (tmpl);
19163 finish_member_declaration (tmpl);
19164 }
19165 else
19166 {
19167 tmpl = NULL_TREE;
19168 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19169 if (fn == error_mark_node)
19170 {
19171 r = error_mark_node;
19172 goto out;
19173 }
19174 finish_member_declaration (fn);
19175 }
19176
19177 if (tree ci = get_constraints (oldfn))
19178 {
19179 /* Substitute into the lambda's constraints. */
19180 if (oldtmpl)
19181 ++processing_template_decl;
19182 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19183 if (oldtmpl)
19184 --processing_template_decl;
19185 set_constraints (fn, ci);
19186 }
19187
19188 /* Let finish_function set this. */
19189 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19190
19191 bool nested = cfun;
19192 if (nested)
19193 push_function_context ();
19194 else
19195 /* Still increment function_depth so that we don't GC in the
19196 middle of an expression. */
19197 ++function_depth;
19198
19199 local_specialization_stack s (lss_copy);
19200
19201 tree body = start_lambda_function (fn, r);
19202
19203 /* Now record them for lookup_init_capture_pack. */
19204 int fplen = vec_safe_length (field_packs);
19205 for (int i = 0; i < fplen; )
19206 {
19207 tree pack = (*field_packs)[i++];
19208 tree inst = (*field_packs)[i++];
19209 register_local_specialization (inst, pack);
19210 }
19211 release_tree_vector (field_packs);
19212
19213 register_parameter_specializations (oldfn, fn);
19214
19215 if (oldtmpl)
19216 {
19217 /* We might not partially instantiate some parts of the function, so
19218 copy these flags from the original template. */
19219 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19220 current_function_returns_value = ol->returns_value;
19221 current_function_returns_null = ol->returns_null;
19222 current_function_returns_abnormally = ol->returns_abnormally;
19223 current_function_infinite_loop = ol->infinite_loop;
19224 }
19225
19226 /* [temp.deduct] A lambda-expression appearing in a function type or a
19227 template parameter is not considered part of the immediate context for
19228 the purposes of template argument deduction. */
19229 complain = tf_warning_or_error;
19230
19231 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19232 /*constexpr*/false);
19233
19234 finish_lambda_function (body);
19235
19236 if (nested)
19237 pop_function_context ();
19238 else
19239 --function_depth;
19240
19241 /* The capture list was built up in reverse order; fix that now. */
19242 LAMBDA_EXPR_CAPTURE_LIST (r)
19243 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19244
19245 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19246
19247 maybe_add_lambda_conv_op (type);
19248 }
19249
19250 out:
19251 finish_struct (type, /*attr*/NULL_TREE);
19252
19253 insert_pending_capture_proxies ();
19254
19255 return r;
19256 }
19257
19258 /* Like tsubst but deals with expressions and performs semantic
19259 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19260
19261 tree
19262 tsubst_copy_and_build (tree t,
19263 tree args,
19264 tsubst_flags_t complain,
19265 tree in_decl,
19266 bool function_p,
19267 bool integral_constant_expression_p)
19268 {
19269 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19270 #define RECUR(NODE) \
19271 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19272 /*function_p=*/false, \
19273 integral_constant_expression_p)
19274
19275 tree retval, op1;
19276 location_t save_loc;
19277
19278 if (t == NULL_TREE || t == error_mark_node)
19279 return t;
19280
19281 save_loc = input_location;
19282 if (location_t eloc = cp_expr_location (t))
19283 input_location = eloc;
19284
19285 /* N3276 decltype magic only applies to calls at the top level or on the
19286 right side of a comma. */
19287 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19288 complain &= ~tf_decltype;
19289
19290 switch (TREE_CODE (t))
19291 {
19292 case USING_DECL:
19293 t = DECL_NAME (t);
19294 /* Fall through. */
19295 case IDENTIFIER_NODE:
19296 {
19297 tree decl;
19298 cp_id_kind idk;
19299 bool non_integral_constant_expression_p;
19300 const char *error_msg;
19301
19302 if (IDENTIFIER_CONV_OP_P (t))
19303 {
19304 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19305 t = make_conv_op_name (new_type);
19306 }
19307
19308 /* Look up the name. */
19309 decl = lookup_name (t);
19310
19311 /* By convention, expressions use ERROR_MARK_NODE to indicate
19312 failure, not NULL_TREE. */
19313 if (decl == NULL_TREE)
19314 decl = error_mark_node;
19315
19316 decl = finish_id_expression (t, decl, NULL_TREE,
19317 &idk,
19318 integral_constant_expression_p,
19319 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19320 &non_integral_constant_expression_p,
19321 /*template_p=*/false,
19322 /*done=*/true,
19323 /*address_p=*/false,
19324 /*template_arg_p=*/false,
19325 &error_msg,
19326 input_location);
19327 if (error_msg)
19328 error (error_msg);
19329 if (!function_p && identifier_p (decl))
19330 {
19331 if (complain & tf_error)
19332 unqualified_name_lookup_error (decl);
19333 decl = error_mark_node;
19334 }
19335 RETURN (decl);
19336 }
19337
19338 case TEMPLATE_ID_EXPR:
19339 {
19340 tree object;
19341 tree templ = RECUR (TREE_OPERAND (t, 0));
19342 tree targs = TREE_OPERAND (t, 1);
19343
19344 if (targs)
19345 targs = tsubst_template_args (targs, args, complain, in_decl);
19346 if (targs == error_mark_node)
19347 RETURN (error_mark_node);
19348
19349 if (TREE_CODE (templ) == SCOPE_REF)
19350 {
19351 tree name = TREE_OPERAND (templ, 1);
19352 tree tid = lookup_template_function (name, targs);
19353 TREE_OPERAND (templ, 1) = tid;
19354 RETURN (templ);
19355 }
19356
19357 if (concept_definition_p (templ))
19358 {
19359 tree check = build_concept_check (templ, targs, complain);
19360 if (check == error_mark_node)
19361 RETURN (error_mark_node);
19362
19363 tree id = unpack_concept_check (check);
19364
19365 /* If we built a function concept check, return the underlying
19366 template-id. So we can evaluate it as a function call. */
19367 if (function_concept_p (TREE_OPERAND (id, 0)))
19368 RETURN (id);
19369
19370 RETURN (check);
19371 }
19372
19373 if (variable_template_p (templ))
19374 {
19375 tree r = lookup_and_finish_template_variable (templ, targs,
19376 complain);
19377 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19378 RETURN (r);
19379 }
19380
19381 if (TREE_CODE (templ) == COMPONENT_REF)
19382 {
19383 object = TREE_OPERAND (templ, 0);
19384 templ = TREE_OPERAND (templ, 1);
19385 }
19386 else
19387 object = NULL_TREE;
19388 templ = lookup_template_function (templ, targs);
19389
19390 if (object)
19391 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19392 object, templ, NULL_TREE));
19393 else
19394 RETURN (baselink_for_fns (templ));
19395 }
19396
19397 case INDIRECT_REF:
19398 {
19399 tree r = RECUR (TREE_OPERAND (t, 0));
19400
19401 if (REFERENCE_REF_P (t))
19402 {
19403 /* A type conversion to reference type will be enclosed in
19404 such an indirect ref, but the substitution of the cast
19405 will have also added such an indirect ref. */
19406 r = convert_from_reference (r);
19407 }
19408 else
19409 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19410 complain|decltype_flag);
19411
19412 if (REF_PARENTHESIZED_P (t))
19413 r = force_paren_expr (r);
19414
19415 RETURN (r);
19416 }
19417
19418 case NOP_EXPR:
19419 {
19420 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19421 tree op0 = RECUR (TREE_OPERAND (t, 0));
19422 RETURN (build_nop (type, op0));
19423 }
19424
19425 case IMPLICIT_CONV_EXPR:
19426 {
19427 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19428 tree expr = RECUR (TREE_OPERAND (t, 0));
19429 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19430 {
19431 retval = copy_node (t);
19432 TREE_TYPE (retval) = type;
19433 TREE_OPERAND (retval, 0) = expr;
19434 RETURN (retval);
19435 }
19436 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19437 /* We'll pass this to convert_nontype_argument again, we don't need
19438 to actually perform any conversion here. */
19439 RETURN (expr);
19440 int flags = LOOKUP_IMPLICIT;
19441 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19442 flags = LOOKUP_NORMAL;
19443 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19444 flags |= LOOKUP_NO_NARROWING;
19445 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19446 flags));
19447 }
19448
19449 case CONVERT_EXPR:
19450 {
19451 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19452 tree op0 = RECUR (TREE_OPERAND (t, 0));
19453 if (op0 == error_mark_node)
19454 RETURN (error_mark_node);
19455 RETURN (build1 (CONVERT_EXPR, type, op0));
19456 }
19457
19458 case CAST_EXPR:
19459 case REINTERPRET_CAST_EXPR:
19460 case CONST_CAST_EXPR:
19461 case DYNAMIC_CAST_EXPR:
19462 case STATIC_CAST_EXPR:
19463 {
19464 tree type;
19465 tree op, r = NULL_TREE;
19466
19467 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19468 if (integral_constant_expression_p
19469 && !cast_valid_in_integral_constant_expression_p (type))
19470 {
19471 if (complain & tf_error)
19472 error ("a cast to a type other than an integral or "
19473 "enumeration type cannot appear in a constant-expression");
19474 RETURN (error_mark_node);
19475 }
19476
19477 op = RECUR (TREE_OPERAND (t, 0));
19478
19479 warning_sentinel s(warn_useless_cast);
19480 warning_sentinel s2(warn_ignored_qualifiers);
19481 switch (TREE_CODE (t))
19482 {
19483 case CAST_EXPR:
19484 r = build_functional_cast (input_location, type, op, complain);
19485 break;
19486 case REINTERPRET_CAST_EXPR:
19487 r = build_reinterpret_cast (input_location, type, op, complain);
19488 break;
19489 case CONST_CAST_EXPR:
19490 r = build_const_cast (input_location, type, op, complain);
19491 break;
19492 case DYNAMIC_CAST_EXPR:
19493 r = build_dynamic_cast (input_location, type, op, complain);
19494 break;
19495 case STATIC_CAST_EXPR:
19496 r = build_static_cast (input_location, type, op, complain);
19497 if (IMPLICIT_RVALUE_P (t))
19498 set_implicit_rvalue_p (r);
19499 break;
19500 default:
19501 gcc_unreachable ();
19502 }
19503
19504 RETURN (r);
19505 }
19506
19507 case POSTDECREMENT_EXPR:
19508 case POSTINCREMENT_EXPR:
19509 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19510 args, complain, in_decl);
19511 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19512 complain|decltype_flag));
19513
19514 case PREDECREMENT_EXPR:
19515 case PREINCREMENT_EXPR:
19516 case NEGATE_EXPR:
19517 case BIT_NOT_EXPR:
19518 case ABS_EXPR:
19519 case TRUTH_NOT_EXPR:
19520 case UNARY_PLUS_EXPR: /* Unary + */
19521 case REALPART_EXPR:
19522 case IMAGPART_EXPR:
19523 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19524 RECUR (TREE_OPERAND (t, 0)),
19525 complain|decltype_flag));
19526
19527 case FIX_TRUNC_EXPR:
19528 gcc_unreachable ();
19529
19530 case ADDR_EXPR:
19531 op1 = TREE_OPERAND (t, 0);
19532 if (TREE_CODE (op1) == LABEL_DECL)
19533 RETURN (finish_label_address_expr (DECL_NAME (op1),
19534 EXPR_LOCATION (op1)));
19535 if (TREE_CODE (op1) == SCOPE_REF)
19536 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19537 /*done=*/true, /*address_p=*/true);
19538 else
19539 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19540 in_decl);
19541 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19542 complain|decltype_flag));
19543
19544 case PLUS_EXPR:
19545 case MINUS_EXPR:
19546 case MULT_EXPR:
19547 case TRUNC_DIV_EXPR:
19548 case CEIL_DIV_EXPR:
19549 case FLOOR_DIV_EXPR:
19550 case ROUND_DIV_EXPR:
19551 case EXACT_DIV_EXPR:
19552 case BIT_AND_EXPR:
19553 case BIT_IOR_EXPR:
19554 case BIT_XOR_EXPR:
19555 case TRUNC_MOD_EXPR:
19556 case FLOOR_MOD_EXPR:
19557 case TRUTH_ANDIF_EXPR:
19558 case TRUTH_ORIF_EXPR:
19559 case TRUTH_AND_EXPR:
19560 case TRUTH_OR_EXPR:
19561 case RSHIFT_EXPR:
19562 case LSHIFT_EXPR:
19563 case EQ_EXPR:
19564 case NE_EXPR:
19565 case MAX_EXPR:
19566 case MIN_EXPR:
19567 case LE_EXPR:
19568 case GE_EXPR:
19569 case LT_EXPR:
19570 case GT_EXPR:
19571 case SPACESHIP_EXPR:
19572 case MEMBER_REF:
19573 case DOTSTAR_EXPR:
19574 {
19575 /* If T was type-dependent, suppress warnings that depend on the range
19576 of the types involved. */
19577 bool was_dep = type_dependent_expression_p_push (t);
19578
19579 tree op0 = RECUR (TREE_OPERAND (t, 0));
19580 tree op1 = RECUR (TREE_OPERAND (t, 1));
19581
19582 warning_sentinel s1(warn_type_limits, was_dep);
19583 warning_sentinel s2(warn_div_by_zero, was_dep);
19584 warning_sentinel s3(warn_logical_op, was_dep);
19585 warning_sentinel s4(warn_tautological_compare, was_dep);
19586
19587 tree r = build_x_binary_op
19588 (input_location, TREE_CODE (t),
19589 op0,
19590 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19591 ? ERROR_MARK
19592 : TREE_CODE (TREE_OPERAND (t, 0))),
19593 op1,
19594 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19595 ? ERROR_MARK
19596 : TREE_CODE (TREE_OPERAND (t, 1))),
19597 /*overload=*/NULL,
19598 complain|decltype_flag);
19599 if (EXPR_P (r) && TREE_NO_WARNING (t))
19600 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19601
19602 RETURN (r);
19603 }
19604
19605 case POINTER_PLUS_EXPR:
19606 {
19607 tree op0 = RECUR (TREE_OPERAND (t, 0));
19608 if (op0 == error_mark_node)
19609 RETURN (error_mark_node);
19610 tree op1 = RECUR (TREE_OPERAND (t, 1));
19611 if (op1 == error_mark_node)
19612 RETURN (error_mark_node);
19613 RETURN (fold_build_pointer_plus (op0, op1));
19614 }
19615
19616 case SCOPE_REF:
19617 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19618 /*address_p=*/false));
19619 case ARRAY_REF:
19620 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19621 args, complain, in_decl);
19622 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19623 RECUR (TREE_OPERAND (t, 1)),
19624 complain|decltype_flag));
19625
19626 case SIZEOF_EXPR:
19627 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19628 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19629 RETURN (tsubst_copy (t, args, complain, in_decl));
19630 /* Fall through */
19631
19632 case ALIGNOF_EXPR:
19633 {
19634 tree r;
19635
19636 op1 = TREE_OPERAND (t, 0);
19637 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19638 op1 = TREE_TYPE (op1);
19639 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19640 && ALIGNOF_EXPR_STD_P (t));
19641 if (!args)
19642 {
19643 /* When there are no ARGS, we are trying to evaluate a
19644 non-dependent expression from the parser. Trying to do
19645 the substitutions may not work. */
19646 if (!TYPE_P (op1))
19647 op1 = TREE_TYPE (op1);
19648 }
19649 else
19650 {
19651 ++cp_unevaluated_operand;
19652 ++c_inhibit_evaluation_warnings;
19653 if (TYPE_P (op1))
19654 op1 = tsubst (op1, args, complain, in_decl);
19655 else
19656 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19657 /*function_p=*/false,
19658 /*integral_constant_expression_p=*/
19659 false);
19660 --cp_unevaluated_operand;
19661 --c_inhibit_evaluation_warnings;
19662 }
19663 if (TYPE_P (op1))
19664 r = cxx_sizeof_or_alignof_type (input_location,
19665 op1, TREE_CODE (t), std_alignof,
19666 complain & tf_error);
19667 else
19668 r = cxx_sizeof_or_alignof_expr (input_location,
19669 op1, TREE_CODE (t),
19670 complain & tf_error);
19671 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19672 {
19673 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19674 {
19675 if (!processing_template_decl && TYPE_P (op1))
19676 {
19677 r = build_min (SIZEOF_EXPR, size_type_node,
19678 build1 (NOP_EXPR, op1, error_mark_node));
19679 SIZEOF_EXPR_TYPE_P (r) = 1;
19680 }
19681 else
19682 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19683 TREE_SIDE_EFFECTS (r) = 0;
19684 TREE_READONLY (r) = 1;
19685 }
19686 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19687 }
19688 RETURN (r);
19689 }
19690
19691 case AT_ENCODE_EXPR:
19692 {
19693 op1 = TREE_OPERAND (t, 0);
19694 ++cp_unevaluated_operand;
19695 ++c_inhibit_evaluation_warnings;
19696 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19697 /*function_p=*/false,
19698 /*integral_constant_expression_p=*/false);
19699 --cp_unevaluated_operand;
19700 --c_inhibit_evaluation_warnings;
19701 RETURN (objc_build_encode_expr (op1));
19702 }
19703
19704 case NOEXCEPT_EXPR:
19705 op1 = TREE_OPERAND (t, 0);
19706 ++cp_unevaluated_operand;
19707 ++c_inhibit_evaluation_warnings;
19708 ++cp_noexcept_operand;
19709 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19710 /*function_p=*/false,
19711 /*integral_constant_expression_p=*/false);
19712 --cp_unevaluated_operand;
19713 --c_inhibit_evaluation_warnings;
19714 --cp_noexcept_operand;
19715 RETURN (finish_noexcept_expr (op1, complain));
19716
19717 case MODOP_EXPR:
19718 {
19719 warning_sentinel s(warn_div_by_zero);
19720 tree lhs = RECUR (TREE_OPERAND (t, 0));
19721 tree rhs = RECUR (TREE_OPERAND (t, 2));
19722 tree r = build_x_modify_expr
19723 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19724 complain|decltype_flag);
19725 /* TREE_NO_WARNING must be set if either the expression was
19726 parenthesized or it uses an operator such as >>= rather
19727 than plain assignment. In the former case, it was already
19728 set and must be copied. In the latter case,
19729 build_x_modify_expr sets it and it must not be reset
19730 here. */
19731 if (TREE_NO_WARNING (t))
19732 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19733
19734 RETURN (r);
19735 }
19736
19737 case ARROW_EXPR:
19738 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19739 args, complain, in_decl);
19740 /* Remember that there was a reference to this entity. */
19741 if (DECL_P (op1)
19742 && !mark_used (op1, complain) && !(complain & tf_error))
19743 RETURN (error_mark_node);
19744 RETURN (build_x_arrow (input_location, op1, complain));
19745
19746 case NEW_EXPR:
19747 {
19748 tree placement = RECUR (TREE_OPERAND (t, 0));
19749 tree init = RECUR (TREE_OPERAND (t, 3));
19750 vec<tree, va_gc> *placement_vec;
19751 vec<tree, va_gc> *init_vec;
19752 tree ret;
19753 location_t loc = EXPR_LOCATION (t);
19754
19755 if (placement == NULL_TREE)
19756 placement_vec = NULL;
19757 else if (placement == error_mark_node)
19758 RETURN (error_mark_node);
19759 else
19760 {
19761 placement_vec = make_tree_vector ();
19762 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19763 vec_safe_push (placement_vec, TREE_VALUE (placement));
19764 }
19765
19766 /* If there was an initializer in the original tree, but it
19767 instantiated to an empty list, then we should pass a
19768 non-NULL empty vector to tell build_new that it was an
19769 empty initializer() rather than no initializer. This can
19770 only happen when the initializer is a pack expansion whose
19771 parameter packs are of length zero. */
19772 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19773 init_vec = NULL;
19774 else
19775 {
19776 init_vec = make_tree_vector ();
19777 if (init == void_node)
19778 gcc_assert (init_vec != NULL);
19779 else
19780 {
19781 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19782 vec_safe_push (init_vec, TREE_VALUE (init));
19783 }
19784 }
19785
19786 /* Avoid passing an enclosing decl to valid_array_size_p. */
19787 in_decl = NULL_TREE;
19788
19789 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19790 tree op2 = RECUR (TREE_OPERAND (t, 2));
19791 ret = build_new (loc, &placement_vec, op1, op2,
19792 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19793 complain);
19794
19795 if (placement_vec != NULL)
19796 release_tree_vector (placement_vec);
19797 if (init_vec != NULL)
19798 release_tree_vector (init_vec);
19799
19800 RETURN (ret);
19801 }
19802
19803 case DELETE_EXPR:
19804 {
19805 tree op0 = RECUR (TREE_OPERAND (t, 0));
19806 tree op1 = RECUR (TREE_OPERAND (t, 1));
19807 RETURN (delete_sanity (input_location, op0, op1,
19808 DELETE_EXPR_USE_VEC (t),
19809 DELETE_EXPR_USE_GLOBAL (t),
19810 complain));
19811 }
19812
19813 case COMPOUND_EXPR:
19814 {
19815 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19816 complain & ~tf_decltype, in_decl,
19817 /*function_p=*/false,
19818 integral_constant_expression_p);
19819 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19820 op0,
19821 RECUR (TREE_OPERAND (t, 1)),
19822 complain|decltype_flag));
19823 }
19824
19825 case CALL_EXPR:
19826 {
19827 tree function;
19828 unsigned int nargs, i;
19829 bool qualified_p;
19830 bool koenig_p;
19831 tree ret;
19832
19833 function = CALL_EXPR_FN (t);
19834 /* Internal function with no arguments. */
19835 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19836 RETURN (t);
19837
19838 /* When we parsed the expression, we determined whether or
19839 not Koenig lookup should be performed. */
19840 koenig_p = KOENIG_LOOKUP_P (t);
19841 if (function == NULL_TREE)
19842 {
19843 koenig_p = false;
19844 qualified_p = false;
19845 }
19846 else if (TREE_CODE (function) == SCOPE_REF)
19847 {
19848 qualified_p = true;
19849 function = tsubst_qualified_id (function, args, complain, in_decl,
19850 /*done=*/false,
19851 /*address_p=*/false);
19852 }
19853 else if (koenig_p && identifier_p (function))
19854 {
19855 /* Do nothing; calling tsubst_copy_and_build on an identifier
19856 would incorrectly perform unqualified lookup again.
19857
19858 Note that we can also have an IDENTIFIER_NODE if the earlier
19859 unqualified lookup found a member function; in that case
19860 koenig_p will be false and we do want to do the lookup
19861 again to find the instantiated member function.
19862
19863 FIXME but doing that causes c++/15272, so we need to stop
19864 using IDENTIFIER_NODE in that situation. */
19865 qualified_p = false;
19866 }
19867 else
19868 {
19869 if (TREE_CODE (function) == COMPONENT_REF)
19870 {
19871 tree op = TREE_OPERAND (function, 1);
19872
19873 qualified_p = (TREE_CODE (op) == SCOPE_REF
19874 || (BASELINK_P (op)
19875 && BASELINK_QUALIFIED_P (op)));
19876 }
19877 else
19878 qualified_p = false;
19879
19880 if (TREE_CODE (function) == ADDR_EXPR
19881 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19882 /* Avoid error about taking the address of a constructor. */
19883 function = TREE_OPERAND (function, 0);
19884
19885 function = tsubst_copy_and_build (function, args, complain,
19886 in_decl,
19887 !qualified_p,
19888 integral_constant_expression_p);
19889
19890 if (BASELINK_P (function))
19891 qualified_p = true;
19892 }
19893
19894 nargs = call_expr_nargs (t);
19895 releasing_vec call_args;
19896 for (i = 0; i < nargs; ++i)
19897 {
19898 tree arg = CALL_EXPR_ARG (t, i);
19899
19900 if (!PACK_EXPANSION_P (arg))
19901 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19902 else
19903 {
19904 /* Expand the pack expansion and push each entry onto
19905 CALL_ARGS. */
19906 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19907 if (TREE_CODE (arg) == TREE_VEC)
19908 {
19909 unsigned int len, j;
19910
19911 len = TREE_VEC_LENGTH (arg);
19912 for (j = 0; j < len; ++j)
19913 {
19914 tree value = TREE_VEC_ELT (arg, j);
19915 if (value != NULL_TREE)
19916 value = convert_from_reference (value);
19917 vec_safe_push (call_args, value);
19918 }
19919 }
19920 else
19921 {
19922 /* A partial substitution. Add one entry. */
19923 vec_safe_push (call_args, arg);
19924 }
19925 }
19926 }
19927
19928 /* Stripped-down processing for a call in a thunk. Specifically, in
19929 the thunk template for a generic lambda. */
19930 if (CALL_FROM_THUNK_P (t))
19931 {
19932 /* Now that we've expanded any packs, the number of call args
19933 might be different. */
19934 unsigned int cargs = call_args->length ();
19935 tree thisarg = NULL_TREE;
19936 if (TREE_CODE (function) == COMPONENT_REF)
19937 {
19938 thisarg = TREE_OPERAND (function, 0);
19939 if (TREE_CODE (thisarg) == INDIRECT_REF)
19940 thisarg = TREE_OPERAND (thisarg, 0);
19941 function = TREE_OPERAND (function, 1);
19942 if (TREE_CODE (function) == BASELINK)
19943 function = BASELINK_FUNCTIONS (function);
19944 }
19945 /* We aren't going to do normal overload resolution, so force the
19946 template-id to resolve. */
19947 function = resolve_nondeduced_context (function, complain);
19948 for (unsigned i = 0; i < cargs; ++i)
19949 {
19950 /* In a thunk, pass through args directly, without any
19951 conversions. */
19952 tree arg = (*call_args)[i];
19953 while (TREE_CODE (arg) != PARM_DECL)
19954 arg = TREE_OPERAND (arg, 0);
19955 (*call_args)[i] = arg;
19956 }
19957 if (thisarg)
19958 {
19959 /* If there are no other args, just push 'this'. */
19960 if (cargs == 0)
19961 vec_safe_push (call_args, thisarg);
19962 else
19963 {
19964 /* Otherwise, shift the other args over to make room. */
19965 tree last = (*call_args)[cargs - 1];
19966 vec_safe_push (call_args, last);
19967 for (int i = cargs - 1; i > 0; --i)
19968 (*call_args)[i] = (*call_args)[i - 1];
19969 (*call_args)[0] = thisarg;
19970 }
19971 }
19972 ret = build_call_a (function, call_args->length (),
19973 call_args->address ());
19974 /* The thunk location is not interesting. */
19975 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19976 CALL_FROM_THUNK_P (ret) = true;
19977 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19978 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19979
19980 RETURN (ret);
19981 }
19982
19983 /* We do not perform argument-dependent lookup if normal
19984 lookup finds a non-function, in accordance with the
19985 expected resolution of DR 218. */
19986 if (koenig_p
19987 && ((is_overloaded_fn (function)
19988 /* If lookup found a member function, the Koenig lookup is
19989 not appropriate, even if an unqualified-name was used
19990 to denote the function. */
19991 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19992 || identifier_p (function))
19993 /* Only do this when substitution turns a dependent call
19994 into a non-dependent call. */
19995 && type_dependent_expression_p_push (t)
19996 && !any_type_dependent_arguments_p (call_args))
19997 function = perform_koenig_lookup (function, call_args, tf_none);
19998
19999 if (function != NULL_TREE
20000 && identifier_p (function)
20001 && !any_type_dependent_arguments_p (call_args))
20002 {
20003 if (koenig_p && (complain & tf_warning_or_error))
20004 {
20005 /* For backwards compatibility and good diagnostics, try
20006 the unqualified lookup again if we aren't in SFINAE
20007 context. */
20008 tree unq = (tsubst_copy_and_build
20009 (function, args, complain, in_decl, true,
20010 integral_constant_expression_p));
20011 if (unq == error_mark_node)
20012 RETURN (error_mark_node);
20013
20014 if (unq != function)
20015 {
20016 /* In a lambda fn, we have to be careful to not
20017 introduce new this captures. Legacy code can't
20018 be using lambdas anyway, so it's ok to be
20019 stricter. */
20020 bool in_lambda = (current_class_type
20021 && LAMBDA_TYPE_P (current_class_type));
20022 char const *const msg
20023 = G_("%qD was not declared in this scope, "
20024 "and no declarations were found by "
20025 "argument-dependent lookup at the point "
20026 "of instantiation");
20027
20028 bool diag = true;
20029 if (in_lambda)
20030 error_at (cp_expr_loc_or_input_loc (t),
20031 msg, function);
20032 else
20033 diag = permerror (cp_expr_loc_or_input_loc (t),
20034 msg, function);
20035 if (diag)
20036 {
20037 tree fn = unq;
20038
20039 if (INDIRECT_REF_P (fn))
20040 fn = TREE_OPERAND (fn, 0);
20041 if (is_overloaded_fn (fn))
20042 fn = get_first_fn (fn);
20043
20044 if (!DECL_P (fn))
20045 /* Can't say anything more. */;
20046 else if (DECL_CLASS_SCOPE_P (fn))
20047 {
20048 location_t loc = cp_expr_loc_or_input_loc (t);
20049 inform (loc,
20050 "declarations in dependent base %qT are "
20051 "not found by unqualified lookup",
20052 DECL_CLASS_CONTEXT (fn));
20053 if (current_class_ptr)
20054 inform (loc,
20055 "use %<this->%D%> instead", function);
20056 else
20057 inform (loc,
20058 "use %<%T::%D%> instead",
20059 current_class_name, function);
20060 }
20061 else
20062 inform (DECL_SOURCE_LOCATION (fn),
20063 "%qD declared here, later in the "
20064 "translation unit", fn);
20065 if (in_lambda)
20066 RETURN (error_mark_node);
20067 }
20068
20069 function = unq;
20070 }
20071 }
20072 if (identifier_p (function))
20073 {
20074 if (complain & tf_error)
20075 unqualified_name_lookup_error (function);
20076 RETURN (error_mark_node);
20077 }
20078 }
20079
20080 /* Remember that there was a reference to this entity. */
20081 if (function != NULL_TREE
20082 && DECL_P (function)
20083 && !mark_used (function, complain) && !(complain & tf_error))
20084 RETURN (error_mark_node);
20085
20086 /* Put back tf_decltype for the actual call. */
20087 complain |= decltype_flag;
20088
20089 if (function == NULL_TREE)
20090 switch (CALL_EXPR_IFN (t))
20091 {
20092 case IFN_LAUNDER:
20093 gcc_assert (nargs == 1);
20094 if (vec_safe_length (call_args) != 1)
20095 {
20096 error_at (cp_expr_loc_or_input_loc (t),
20097 "wrong number of arguments to "
20098 "%<__builtin_launder%>");
20099 ret = error_mark_node;
20100 }
20101 else
20102 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20103 (*call_args)[0], complain);
20104 break;
20105
20106 case IFN_VEC_CONVERT:
20107 gcc_assert (nargs == 1);
20108 if (vec_safe_length (call_args) != 1)
20109 {
20110 error_at (cp_expr_loc_or_input_loc (t),
20111 "wrong number of arguments to "
20112 "%<__builtin_convertvector%>");
20113 ret = error_mark_node;
20114 break;
20115 }
20116 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20117 tsubst (TREE_TYPE (t), args,
20118 complain, in_decl),
20119 complain);
20120 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20121 RETURN (ret);
20122 break;
20123
20124 default:
20125 /* Unsupported internal function with arguments. */
20126 gcc_unreachable ();
20127 }
20128 else if (TREE_CODE (function) == OFFSET_REF
20129 || TREE_CODE (function) == DOTSTAR_EXPR
20130 || TREE_CODE (function) == MEMBER_REF)
20131 ret = build_offset_ref_call_from_tree (function, &call_args,
20132 complain);
20133 else if (TREE_CODE (function) == COMPONENT_REF)
20134 {
20135 tree instance = TREE_OPERAND (function, 0);
20136 tree fn = TREE_OPERAND (function, 1);
20137
20138 if (processing_template_decl
20139 && (type_dependent_expression_p (instance)
20140 || (!BASELINK_P (fn)
20141 && TREE_CODE (fn) != FIELD_DECL)
20142 || type_dependent_expression_p (fn)
20143 || any_type_dependent_arguments_p (call_args)))
20144 ret = build_min_nt_call_vec (function, call_args);
20145 else if (!BASELINK_P (fn))
20146 ret = finish_call_expr (function, &call_args,
20147 /*disallow_virtual=*/false,
20148 /*koenig_p=*/false,
20149 complain);
20150 else
20151 ret = (build_new_method_call
20152 (instance, fn,
20153 &call_args, NULL_TREE,
20154 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20155 /*fn_p=*/NULL,
20156 complain));
20157 }
20158 else if (concept_check_p (function))
20159 {
20160 /* FUNCTION is a template-id referring to a concept definition. */
20161 tree id = unpack_concept_check (function);
20162 tree tmpl = TREE_OPERAND (id, 0);
20163 tree args = TREE_OPERAND (id, 1);
20164
20165 /* Calls to standard and variable concepts should have been
20166 previously diagnosed. */
20167 gcc_assert (function_concept_p (tmpl));
20168
20169 /* Ensure the result is wrapped as a call expression. */
20170 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20171 }
20172 else
20173 ret = finish_call_expr (function, &call_args,
20174 /*disallow_virtual=*/qualified_p,
20175 koenig_p,
20176 complain);
20177
20178 if (ret != error_mark_node)
20179 {
20180 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20181 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20182 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20183 if (op || ord || rev)
20184 {
20185 function = extract_call_expr (ret);
20186 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20187 CALL_EXPR_ORDERED_ARGS (function) = ord;
20188 CALL_EXPR_REVERSE_ARGS (function) = rev;
20189 }
20190 }
20191
20192 RETURN (ret);
20193 }
20194
20195 case COND_EXPR:
20196 {
20197 tree cond = RECUR (TREE_OPERAND (t, 0));
20198 cond = mark_rvalue_use (cond);
20199 tree folded_cond = fold_non_dependent_expr (cond, complain);
20200 tree exp1, exp2;
20201
20202 if (TREE_CODE (folded_cond) == INTEGER_CST)
20203 {
20204 if (integer_zerop (folded_cond))
20205 {
20206 ++c_inhibit_evaluation_warnings;
20207 exp1 = RECUR (TREE_OPERAND (t, 1));
20208 --c_inhibit_evaluation_warnings;
20209 exp2 = RECUR (TREE_OPERAND (t, 2));
20210 }
20211 else
20212 {
20213 exp1 = RECUR (TREE_OPERAND (t, 1));
20214 ++c_inhibit_evaluation_warnings;
20215 exp2 = RECUR (TREE_OPERAND (t, 2));
20216 --c_inhibit_evaluation_warnings;
20217 }
20218 cond = folded_cond;
20219 }
20220 else
20221 {
20222 exp1 = RECUR (TREE_OPERAND (t, 1));
20223 exp2 = RECUR (TREE_OPERAND (t, 2));
20224 }
20225
20226 warning_sentinel s(warn_duplicated_branches);
20227 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20228 cond, exp1, exp2, complain));
20229 }
20230
20231 case PSEUDO_DTOR_EXPR:
20232 {
20233 tree op0 = RECUR (TREE_OPERAND (t, 0));
20234 tree op1 = RECUR (TREE_OPERAND (t, 1));
20235 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20236 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20237 input_location));
20238 }
20239
20240 case TREE_LIST:
20241 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20242
20243 case COMPONENT_REF:
20244 {
20245 tree object;
20246 tree object_type;
20247 tree member;
20248 tree r;
20249
20250 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20251 args, complain, in_decl);
20252 /* Remember that there was a reference to this entity. */
20253 if (DECL_P (object)
20254 && !mark_used (object, complain) && !(complain & tf_error))
20255 RETURN (error_mark_node);
20256 object_type = TREE_TYPE (object);
20257
20258 member = TREE_OPERAND (t, 1);
20259 if (BASELINK_P (member))
20260 member = tsubst_baselink (member,
20261 non_reference (TREE_TYPE (object)),
20262 args, complain, in_decl);
20263 else
20264 member = tsubst_copy (member, args, complain, in_decl);
20265 if (member == error_mark_node)
20266 RETURN (error_mark_node);
20267
20268 if (TREE_CODE (member) == FIELD_DECL)
20269 {
20270 r = finish_non_static_data_member (member, object, NULL_TREE);
20271 if (TREE_CODE (r) == COMPONENT_REF)
20272 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20273 RETURN (r);
20274 }
20275 else if (type_dependent_expression_p (object))
20276 /* We can't do much here. */;
20277 else if (!CLASS_TYPE_P (object_type))
20278 {
20279 if (scalarish_type_p (object_type))
20280 {
20281 tree s = NULL_TREE;
20282 tree dtor = member;
20283
20284 if (TREE_CODE (dtor) == SCOPE_REF)
20285 {
20286 s = TREE_OPERAND (dtor, 0);
20287 dtor = TREE_OPERAND (dtor, 1);
20288 }
20289 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20290 {
20291 dtor = TREE_OPERAND (dtor, 0);
20292 if (TYPE_P (dtor))
20293 RETURN (finish_pseudo_destructor_expr
20294 (object, s, dtor, input_location));
20295 }
20296 }
20297 }
20298 else if (TREE_CODE (member) == SCOPE_REF
20299 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20300 {
20301 /* Lookup the template functions now that we know what the
20302 scope is. */
20303 tree scope = TREE_OPERAND (member, 0);
20304 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20305 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20306 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20307 /*complain=*/false);
20308 if (BASELINK_P (member))
20309 {
20310 BASELINK_FUNCTIONS (member)
20311 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20312 args);
20313 member = (adjust_result_of_qualified_name_lookup
20314 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20315 object_type));
20316 }
20317 else
20318 {
20319 qualified_name_lookup_error (scope, tmpl, member,
20320 input_location);
20321 RETURN (error_mark_node);
20322 }
20323 }
20324 else if (TREE_CODE (member) == SCOPE_REF
20325 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20326 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20327 {
20328 if (complain & tf_error)
20329 {
20330 if (TYPE_P (TREE_OPERAND (member, 0)))
20331 error ("%qT is not a class or namespace",
20332 TREE_OPERAND (member, 0));
20333 else
20334 error ("%qD is not a class or namespace",
20335 TREE_OPERAND (member, 0));
20336 }
20337 RETURN (error_mark_node);
20338 }
20339
20340 r = finish_class_member_access_expr (object, member,
20341 /*template_p=*/false,
20342 complain);
20343 if (TREE_CODE (r) == COMPONENT_REF)
20344 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20345 RETURN (r);
20346 }
20347
20348 case THROW_EXPR:
20349 RETURN (build_throw
20350 (input_location, RECUR (TREE_OPERAND (t, 0))));
20351
20352 case CONSTRUCTOR:
20353 {
20354 vec<constructor_elt, va_gc> *n;
20355 constructor_elt *ce;
20356 unsigned HOST_WIDE_INT idx;
20357 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20358 bool process_index_p;
20359 int newlen;
20360 bool need_copy_p = false;
20361 tree r;
20362
20363 if (type == error_mark_node)
20364 RETURN (error_mark_node);
20365
20366 /* We do not want to process the index of aggregate
20367 initializers as they are identifier nodes which will be
20368 looked up by digest_init. */
20369 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20370
20371 if (null_member_pointer_value_p (t))
20372 {
20373 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20374 RETURN (t);
20375 }
20376
20377 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20378 newlen = vec_safe_length (n);
20379 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20380 {
20381 if (ce->index && process_index_p
20382 /* An identifier index is looked up in the type
20383 being initialized, not the current scope. */
20384 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20385 ce->index = RECUR (ce->index);
20386
20387 if (PACK_EXPANSION_P (ce->value))
20388 {
20389 /* Substitute into the pack expansion. */
20390 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20391 in_decl);
20392
20393 if (ce->value == error_mark_node
20394 || PACK_EXPANSION_P (ce->value))
20395 ;
20396 else if (TREE_VEC_LENGTH (ce->value) == 1)
20397 /* Just move the argument into place. */
20398 ce->value = TREE_VEC_ELT (ce->value, 0);
20399 else
20400 {
20401 /* Update the length of the final CONSTRUCTOR
20402 arguments vector, and note that we will need to
20403 copy.*/
20404 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20405 need_copy_p = true;
20406 }
20407 }
20408 else
20409 ce->value = RECUR (ce->value);
20410 }
20411
20412 if (need_copy_p)
20413 {
20414 vec<constructor_elt, va_gc> *old_n = n;
20415
20416 vec_alloc (n, newlen);
20417 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20418 {
20419 if (TREE_CODE (ce->value) == TREE_VEC)
20420 {
20421 int i, len = TREE_VEC_LENGTH (ce->value);
20422 for (i = 0; i < len; ++i)
20423 CONSTRUCTOR_APPEND_ELT (n, 0,
20424 TREE_VEC_ELT (ce->value, i));
20425 }
20426 else
20427 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20428 }
20429 }
20430
20431 r = build_constructor (init_list_type_node, n);
20432 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20433 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20434 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20435
20436 if (TREE_HAS_CONSTRUCTOR (t))
20437 {
20438 fcl_t cl = fcl_functional;
20439 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20440 cl = fcl_c99;
20441 RETURN (finish_compound_literal (type, r, complain, cl));
20442 }
20443
20444 TREE_TYPE (r) = type;
20445 RETURN (r);
20446 }
20447
20448 case TYPEID_EXPR:
20449 {
20450 tree operand_0 = TREE_OPERAND (t, 0);
20451 if (TYPE_P (operand_0))
20452 {
20453 operand_0 = tsubst (operand_0, args, complain, in_decl);
20454 RETURN (get_typeid (operand_0, complain));
20455 }
20456 else
20457 {
20458 operand_0 = RECUR (operand_0);
20459 RETURN (build_typeid (operand_0, complain));
20460 }
20461 }
20462
20463 case VAR_DECL:
20464 if (!args)
20465 RETURN (t);
20466 /* Fall through */
20467
20468 case PARM_DECL:
20469 {
20470 tree r = tsubst_copy (t, args, complain, in_decl);
20471 /* ??? We're doing a subset of finish_id_expression here. */
20472 if (tree wrap = maybe_get_tls_wrapper_call (r))
20473 /* Replace an evaluated use of the thread_local variable with
20474 a call to its wrapper. */
20475 r = wrap;
20476 else if (outer_automatic_var_p (r))
20477 r = process_outer_var_ref (r, complain);
20478
20479 if (!TYPE_REF_P (TREE_TYPE (t)))
20480 /* If the original type was a reference, we'll be wrapped in
20481 the appropriate INDIRECT_REF. */
20482 r = convert_from_reference (r);
20483 RETURN (r);
20484 }
20485
20486 case VA_ARG_EXPR:
20487 {
20488 tree op0 = RECUR (TREE_OPERAND (t, 0));
20489 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20490 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20491 }
20492
20493 case OFFSETOF_EXPR:
20494 {
20495 tree object_ptr
20496 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20497 in_decl, /*function_p=*/false,
20498 /*integral_constant_expression_p=*/false);
20499 RETURN (finish_offsetof (object_ptr,
20500 RECUR (TREE_OPERAND (t, 0)),
20501 EXPR_LOCATION (t)));
20502 }
20503
20504 case ADDRESSOF_EXPR:
20505 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20506 RECUR (TREE_OPERAND (t, 0)), complain));
20507
20508 case TRAIT_EXPR:
20509 {
20510 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20511 complain, in_decl);
20512 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20513 complain, in_decl);
20514 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20515 TRAIT_EXPR_KIND (t), type1, type2));
20516 }
20517
20518 case STMT_EXPR:
20519 {
20520 tree old_stmt_expr = cur_stmt_expr;
20521 tree stmt_expr = begin_stmt_expr ();
20522
20523 cur_stmt_expr = stmt_expr;
20524 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20525 integral_constant_expression_p);
20526 stmt_expr = finish_stmt_expr (stmt_expr, false);
20527 cur_stmt_expr = old_stmt_expr;
20528
20529 /* If the resulting list of expression statement is empty,
20530 fold it further into void_node. */
20531 if (empty_expr_stmt_p (stmt_expr))
20532 stmt_expr = void_node;
20533
20534 RETURN (stmt_expr);
20535 }
20536
20537 case LAMBDA_EXPR:
20538 {
20539 if (complain & tf_partial)
20540 {
20541 /* We don't have a full set of template arguments yet; don't touch
20542 the lambda at all. */
20543 gcc_assert (processing_template_decl);
20544 return t;
20545 }
20546 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20547
20548 RETURN (build_lambda_object (r));
20549 }
20550
20551 case TARGET_EXPR:
20552 /* We can get here for a constant initializer of non-dependent type.
20553 FIXME stop folding in cp_parser_initializer_clause. */
20554 {
20555 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20556 complain);
20557 RETURN (r);
20558 }
20559
20560 case TRANSACTION_EXPR:
20561 RETURN (tsubst_expr(t, args, complain, in_decl,
20562 integral_constant_expression_p));
20563
20564 case PAREN_EXPR:
20565 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20566
20567 case VEC_PERM_EXPR:
20568 {
20569 tree op0 = RECUR (TREE_OPERAND (t, 0));
20570 tree op1 = RECUR (TREE_OPERAND (t, 1));
20571 tree op2 = RECUR (TREE_OPERAND (t, 2));
20572 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20573 complain));
20574 }
20575
20576 case REQUIRES_EXPR:
20577 {
20578 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20579 RETURN (r);
20580 }
20581
20582 case RANGE_EXPR:
20583 /* No need to substitute further, a RANGE_EXPR will always be built
20584 with constant operands. */
20585 RETURN (t);
20586
20587 case NON_LVALUE_EXPR:
20588 case VIEW_CONVERT_EXPR:
20589 if (location_wrapper_p (t))
20590 /* We need to do this here as well as in tsubst_copy so we get the
20591 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20592 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20593 EXPR_LOCATION (t)));
20594 /* fallthrough. */
20595
20596 default:
20597 /* Handle Objective-C++ constructs, if appropriate. */
20598 {
20599 tree subst
20600 = objcp_tsubst_copy_and_build (t, args, complain,
20601 in_decl, /*function_p=*/false);
20602 if (subst)
20603 RETURN (subst);
20604 }
20605 RETURN (tsubst_copy (t, args, complain, in_decl));
20606 }
20607
20608 #undef RECUR
20609 #undef RETURN
20610 out:
20611 input_location = save_loc;
20612 return retval;
20613 }
20614
20615 /* Verify that the instantiated ARGS are valid. For type arguments,
20616 make sure that the type's linkage is ok. For non-type arguments,
20617 make sure they are constants if they are integral or enumerations.
20618 Emit an error under control of COMPLAIN, and return TRUE on error. */
20619
20620 static bool
20621 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20622 {
20623 if (dependent_template_arg_p (t))
20624 return false;
20625 if (ARGUMENT_PACK_P (t))
20626 {
20627 tree vec = ARGUMENT_PACK_ARGS (t);
20628 int len = TREE_VEC_LENGTH (vec);
20629 bool result = false;
20630 int i;
20631
20632 for (i = 0; i < len; ++i)
20633 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20634 result = true;
20635 return result;
20636 }
20637 else if (TYPE_P (t))
20638 {
20639 /* [basic.link]: A name with no linkage (notably, the name
20640 of a class or enumeration declared in a local scope)
20641 shall not be used to declare an entity with linkage.
20642 This implies that names with no linkage cannot be used as
20643 template arguments
20644
20645 DR 757 relaxes this restriction for C++0x. */
20646 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20647 : no_linkage_check (t, /*relaxed_p=*/false));
20648
20649 if (nt)
20650 {
20651 /* DR 488 makes use of a type with no linkage cause
20652 type deduction to fail. */
20653 if (complain & tf_error)
20654 {
20655 if (TYPE_UNNAMED_P (nt))
20656 error ("%qT is/uses unnamed type", t);
20657 else
20658 error ("template argument for %qD uses local type %qT",
20659 tmpl, t);
20660 }
20661 return true;
20662 }
20663 /* In order to avoid all sorts of complications, we do not
20664 allow variably-modified types as template arguments. */
20665 else if (variably_modified_type_p (t, NULL_TREE))
20666 {
20667 if (complain & tf_error)
20668 error ("%qT is a variably modified type", t);
20669 return true;
20670 }
20671 }
20672 /* Class template and alias template arguments should be OK. */
20673 else if (DECL_TYPE_TEMPLATE_P (t))
20674 ;
20675 /* A non-type argument of integral or enumerated type must be a
20676 constant. */
20677 else if (TREE_TYPE (t)
20678 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20679 && !REFERENCE_REF_P (t)
20680 && !TREE_CONSTANT (t))
20681 {
20682 if (complain & tf_error)
20683 error ("integral expression %qE is not constant", t);
20684 return true;
20685 }
20686 return false;
20687 }
20688
20689 static bool
20690 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20691 {
20692 int ix, len = DECL_NTPARMS (tmpl);
20693 bool result = false;
20694
20695 for (ix = 0; ix != len; ix++)
20696 {
20697 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20698 result = true;
20699 }
20700 if (result && (complain & tf_error))
20701 error (" trying to instantiate %qD", tmpl);
20702 return result;
20703 }
20704
20705 /* We're out of SFINAE context now, so generate diagnostics for the access
20706 errors we saw earlier when instantiating D from TMPL and ARGS. */
20707
20708 static void
20709 recheck_decl_substitution (tree d, tree tmpl, tree args)
20710 {
20711 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20712 tree type = TREE_TYPE (pattern);
20713 location_t loc = input_location;
20714
20715 push_access_scope (d);
20716 push_deferring_access_checks (dk_no_deferred);
20717 input_location = DECL_SOURCE_LOCATION (pattern);
20718 tsubst (type, args, tf_warning_or_error, d);
20719 input_location = loc;
20720 pop_deferring_access_checks ();
20721 pop_access_scope (d);
20722 }
20723
20724 /* Instantiate the indicated variable, function, or alias template TMPL with
20725 the template arguments in TARG_PTR. */
20726
20727 static tree
20728 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20729 {
20730 tree targ_ptr = orig_args;
20731 tree fndecl;
20732 tree gen_tmpl;
20733 tree spec;
20734 bool access_ok = true;
20735
20736 if (tmpl == error_mark_node)
20737 return error_mark_node;
20738
20739 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20740
20741 /* If this function is a clone, handle it specially. */
20742 if (DECL_CLONED_FUNCTION_P (tmpl))
20743 {
20744 tree spec;
20745 tree clone;
20746
20747 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20748 DECL_CLONED_FUNCTION. */
20749 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20750 targ_ptr, complain);
20751 if (spec == error_mark_node)
20752 return error_mark_node;
20753
20754 /* Look for the clone. */
20755 FOR_EACH_CLONE (clone, spec)
20756 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20757 return clone;
20758 /* We should always have found the clone by now. */
20759 gcc_unreachable ();
20760 return NULL_TREE;
20761 }
20762
20763 if (targ_ptr == error_mark_node)
20764 return error_mark_node;
20765
20766 /* Check to see if we already have this specialization. */
20767 gen_tmpl = most_general_template (tmpl);
20768 if (TMPL_ARGS_DEPTH (targ_ptr)
20769 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20770 /* targ_ptr only has the innermost template args, so add the outer ones
20771 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20772 the case of a non-dependent call within a template definition). */
20773 targ_ptr = (add_outermost_template_args
20774 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20775 targ_ptr));
20776
20777 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20778 but it doesn't seem to be on the hot path. */
20779 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20780
20781 gcc_checking_assert (tmpl == gen_tmpl
20782 || ((fndecl
20783 = retrieve_specialization (tmpl, orig_args, 0))
20784 == spec)
20785 || fndecl == NULL_TREE);
20786
20787 if (spec != NULL_TREE)
20788 {
20789 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20790 {
20791 if (complain & tf_error)
20792 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20793 return error_mark_node;
20794 }
20795 return spec;
20796 }
20797
20798 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20799 complain))
20800 return error_mark_node;
20801
20802 /* We are building a FUNCTION_DECL, during which the access of its
20803 parameters and return types have to be checked. However this
20804 FUNCTION_DECL which is the desired context for access checking
20805 is not built yet. We solve this chicken-and-egg problem by
20806 deferring all checks until we have the FUNCTION_DECL. */
20807 push_deferring_access_checks (dk_deferred);
20808
20809 /* Instantiation of the function happens in the context of the function
20810 template, not the context of the overload resolution we're doing. */
20811 push_to_top_level ();
20812 /* If there are dependent arguments, e.g. because we're doing partial
20813 ordering, make sure processing_template_decl stays set. */
20814 if (uses_template_parms (targ_ptr))
20815 ++processing_template_decl;
20816 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20817 {
20818 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20819 complain, gen_tmpl, true);
20820 push_nested_class (ctx);
20821 }
20822
20823 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20824
20825 fndecl = NULL_TREE;
20826 if (VAR_P (pattern))
20827 {
20828 /* We need to determine if we're using a partial or explicit
20829 specialization now, because the type of the variable could be
20830 different. */
20831 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20832 tree elt = most_specialized_partial_spec (tid, complain);
20833 if (elt == error_mark_node)
20834 pattern = error_mark_node;
20835 else if (elt)
20836 {
20837 tree partial_tmpl = TREE_VALUE (elt);
20838 tree partial_args = TREE_PURPOSE (elt);
20839 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20840 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20841 }
20842 }
20843
20844 /* Substitute template parameters to obtain the specialization. */
20845 if (fndecl == NULL_TREE)
20846 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20847 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20848 pop_nested_class ();
20849 pop_from_top_level ();
20850
20851 if (fndecl == error_mark_node)
20852 {
20853 pop_deferring_access_checks ();
20854 return error_mark_node;
20855 }
20856
20857 /* The DECL_TI_TEMPLATE should always be the immediate parent
20858 template, not the most general template. */
20859 DECL_TI_TEMPLATE (fndecl) = tmpl;
20860 DECL_TI_ARGS (fndecl) = targ_ptr;
20861
20862 /* Now we know the specialization, compute access previously
20863 deferred. Do no access control for inheriting constructors,
20864 as we already checked access for the inherited constructor. */
20865 if (!(flag_new_inheriting_ctors
20866 && DECL_INHERITED_CTOR (fndecl)))
20867 {
20868 push_access_scope (fndecl);
20869 if (!perform_deferred_access_checks (complain))
20870 access_ok = false;
20871 pop_access_scope (fndecl);
20872 }
20873 pop_deferring_access_checks ();
20874
20875 /* If we've just instantiated the main entry point for a function,
20876 instantiate all the alternate entry points as well. We do this
20877 by cloning the instantiation of the main entry point, not by
20878 instantiating the template clones. */
20879 if (tree chain = DECL_CHAIN (gen_tmpl))
20880 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20881 clone_cdtor (fndecl, /*update_methods=*/false);
20882
20883 if (!access_ok)
20884 {
20885 if (!(complain & tf_error))
20886 {
20887 /* Remember to reinstantiate when we're out of SFINAE so the user
20888 can see the errors. */
20889 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20890 }
20891 return error_mark_node;
20892 }
20893 return fndecl;
20894 }
20895
20896 /* Wrapper for instantiate_template_1. */
20897
20898 tree
20899 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20900 {
20901 tree ret;
20902 timevar_push (TV_TEMPLATE_INST);
20903 ret = instantiate_template_1 (tmpl, orig_args, complain);
20904 timevar_pop (TV_TEMPLATE_INST);
20905 return ret;
20906 }
20907
20908 /* Instantiate the alias template TMPL with ARGS. Also push a template
20909 instantiation level, which instantiate_template doesn't do because
20910 functions and variables have sufficient context established by the
20911 callers. */
20912
20913 static tree
20914 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20915 {
20916 if (tmpl == error_mark_node || args == error_mark_node)
20917 return error_mark_node;
20918
20919 args =
20920 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20921 args, tmpl, complain,
20922 /*require_all_args=*/true,
20923 /*use_default_args=*/true);
20924
20925 /* FIXME check for satisfaction in check_instantiated_args. */
20926 if (flag_concepts
20927 && !any_dependent_template_arguments_p (args)
20928 && !constraints_satisfied_p (tmpl, args))
20929 {
20930 if (complain & tf_error)
20931 {
20932 auto_diagnostic_group d;
20933 error ("template constraint failure for %qD", tmpl);
20934 diagnose_constraints (input_location, tmpl, args);
20935 }
20936 return error_mark_node;
20937 }
20938
20939 if (!push_tinst_level (tmpl, args))
20940 return error_mark_node;
20941 tree r = instantiate_template (tmpl, args, complain);
20942 pop_tinst_level ();
20943
20944 return r;
20945 }
20946
20947 /* PARM is a template parameter pack for FN. Returns true iff
20948 PARM is used in a deducible way in the argument list of FN. */
20949
20950 static bool
20951 pack_deducible_p (tree parm, tree fn)
20952 {
20953 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20954 for (; t; t = TREE_CHAIN (t))
20955 {
20956 tree type = TREE_VALUE (t);
20957 tree packs;
20958 if (!PACK_EXPANSION_P (type))
20959 continue;
20960 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20961 packs; packs = TREE_CHAIN (packs))
20962 if (template_args_equal (TREE_VALUE (packs), parm))
20963 {
20964 /* The template parameter pack is used in a function parameter
20965 pack. If this is the end of the parameter list, the
20966 template parameter pack is deducible. */
20967 if (TREE_CHAIN (t) == void_list_node)
20968 return true;
20969 else
20970 /* Otherwise, not. Well, it could be deduced from
20971 a non-pack parameter, but doing so would end up with
20972 a deduction mismatch, so don't bother. */
20973 return false;
20974 }
20975 }
20976 /* The template parameter pack isn't used in any function parameter
20977 packs, but it might be used deeper, e.g. tuple<Args...>. */
20978 return true;
20979 }
20980
20981 /* Subroutine of fn_type_unification: check non-dependent parms for
20982 convertibility. */
20983
20984 static int
20985 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20986 tree fn, unification_kind_t strict, int flags,
20987 struct conversion **convs, bool explain_p)
20988 {
20989 /* Non-constructor methods need to leave a conversion for 'this', which
20990 isn't included in nargs here. */
20991 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20992 && !DECL_CONSTRUCTOR_P (fn));
20993
20994 for (unsigned ia = 0;
20995 parms && parms != void_list_node && ia < nargs; )
20996 {
20997 tree parm = TREE_VALUE (parms);
20998
20999 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21000 && (!TREE_CHAIN (parms)
21001 || TREE_CHAIN (parms) == void_list_node))
21002 /* For a function parameter pack that occurs at the end of the
21003 parameter-declaration-list, the type A of each remaining
21004 argument of the call is compared with the type P of the
21005 declarator-id of the function parameter pack. */
21006 break;
21007
21008 parms = TREE_CHAIN (parms);
21009
21010 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21011 /* For a function parameter pack that does not occur at the
21012 end of the parameter-declaration-list, the type of the
21013 parameter pack is a non-deduced context. */
21014 continue;
21015
21016 if (!uses_template_parms (parm))
21017 {
21018 tree arg = args[ia];
21019 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21020 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21021
21022 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21023 conv_p, explain_p))
21024 return 1;
21025 }
21026
21027 ++ia;
21028 }
21029
21030 return 0;
21031 }
21032
21033 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21034 NARGS elements of the arguments that are being used when calling
21035 it. TARGS is a vector into which the deduced template arguments
21036 are placed.
21037
21038 Returns either a FUNCTION_DECL for the matching specialization of FN or
21039 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21040 true, diagnostics will be printed to explain why it failed.
21041
21042 If FN is a conversion operator, or we are trying to produce a specific
21043 specialization, RETURN_TYPE is the return type desired.
21044
21045 The EXPLICIT_TARGS are explicit template arguments provided via a
21046 template-id.
21047
21048 The parameter STRICT is one of:
21049
21050 DEDUCE_CALL:
21051 We are deducing arguments for a function call, as in
21052 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21053 deducing arguments for a call to the result of a conversion
21054 function template, as in [over.call.object].
21055
21056 DEDUCE_CONV:
21057 We are deducing arguments for a conversion function, as in
21058 [temp.deduct.conv].
21059
21060 DEDUCE_EXACT:
21061 We are deducing arguments when doing an explicit instantiation
21062 as in [temp.explicit], when determining an explicit specialization
21063 as in [temp.expl.spec], or when taking the address of a function
21064 template, as in [temp.deduct.funcaddr]. */
21065
21066 tree
21067 fn_type_unification (tree fn,
21068 tree explicit_targs,
21069 tree targs,
21070 const tree *args,
21071 unsigned int nargs,
21072 tree return_type,
21073 unification_kind_t strict,
21074 int flags,
21075 struct conversion **convs,
21076 bool explain_p,
21077 bool decltype_p)
21078 {
21079 tree parms;
21080 tree fntype;
21081 tree decl = NULL_TREE;
21082 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21083 bool ok;
21084 static int deduction_depth;
21085 /* type_unification_real will pass back any access checks from default
21086 template argument substitution. */
21087 vec<deferred_access_check, va_gc> *checks = NULL;
21088 /* We don't have all the template args yet. */
21089 bool incomplete = true;
21090
21091 tree orig_fn = fn;
21092 if (flag_new_inheriting_ctors)
21093 fn = strip_inheriting_ctors (fn);
21094
21095 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21096 tree r = error_mark_node;
21097
21098 tree full_targs = targs;
21099 if (TMPL_ARGS_DEPTH (targs)
21100 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21101 full_targs = (add_outermost_template_args
21102 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21103 targs));
21104
21105 if (decltype_p)
21106 complain |= tf_decltype;
21107
21108 /* In C++0x, it's possible to have a function template whose type depends
21109 on itself recursively. This is most obvious with decltype, but can also
21110 occur with enumeration scope (c++/48969). So we need to catch infinite
21111 recursion and reject the substitution at deduction time; this function
21112 will return error_mark_node for any repeated substitution.
21113
21114 This also catches excessive recursion such as when f<N> depends on
21115 f<N-1> across all integers, and returns error_mark_node for all the
21116 substitutions back up to the initial one.
21117
21118 This is, of course, not reentrant. */
21119 if (excessive_deduction_depth)
21120 return error_mark_node;
21121 ++deduction_depth;
21122
21123 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21124
21125 fntype = TREE_TYPE (fn);
21126 if (explicit_targs)
21127 {
21128 /* [temp.deduct]
21129
21130 The specified template arguments must match the template
21131 parameters in kind (i.e., type, nontype, template), and there
21132 must not be more arguments than there are parameters;
21133 otherwise type deduction fails.
21134
21135 Nontype arguments must match the types of the corresponding
21136 nontype template parameters, or must be convertible to the
21137 types of the corresponding nontype parameters as specified in
21138 _temp.arg.nontype_, otherwise type deduction fails.
21139
21140 All references in the function type of the function template
21141 to the corresponding template parameters are replaced by the
21142 specified template argument values. If a substitution in a
21143 template parameter or in the function type of the function
21144 template results in an invalid type, type deduction fails. */
21145 int i, len = TREE_VEC_LENGTH (tparms);
21146 location_t loc = input_location;
21147 incomplete = false;
21148
21149 if (explicit_targs == error_mark_node)
21150 goto fail;
21151
21152 if (TMPL_ARGS_DEPTH (explicit_targs)
21153 < TMPL_ARGS_DEPTH (full_targs))
21154 explicit_targs = add_outermost_template_args (full_targs,
21155 explicit_targs);
21156
21157 /* Adjust any explicit template arguments before entering the
21158 substitution context. */
21159 explicit_targs
21160 = (coerce_template_parms (tparms, explicit_targs, fn,
21161 complain|tf_partial,
21162 /*require_all_args=*/false,
21163 /*use_default_args=*/false));
21164 if (explicit_targs == error_mark_node)
21165 goto fail;
21166
21167 /* Substitute the explicit args into the function type. This is
21168 necessary so that, for instance, explicitly declared function
21169 arguments can match null pointed constants. If we were given
21170 an incomplete set of explicit args, we must not do semantic
21171 processing during substitution as we could create partial
21172 instantiations. */
21173 for (i = 0; i < len; i++)
21174 {
21175 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21176 bool parameter_pack = false;
21177 tree targ = TREE_VEC_ELT (explicit_targs, i);
21178
21179 /* Dig out the actual parm. */
21180 if (TREE_CODE (parm) == TYPE_DECL
21181 || TREE_CODE (parm) == TEMPLATE_DECL)
21182 {
21183 parm = TREE_TYPE (parm);
21184 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21185 }
21186 else if (TREE_CODE (parm) == PARM_DECL)
21187 {
21188 parm = DECL_INITIAL (parm);
21189 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21190 }
21191
21192 if (targ == NULL_TREE)
21193 /* No explicit argument for this template parameter. */
21194 incomplete = true;
21195 else if (parameter_pack && pack_deducible_p (parm, fn))
21196 {
21197 /* Mark the argument pack as "incomplete". We could
21198 still deduce more arguments during unification.
21199 We remove this mark in type_unification_real. */
21200 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21201 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21202 = ARGUMENT_PACK_ARGS (targ);
21203
21204 /* We have some incomplete argument packs. */
21205 incomplete = true;
21206 }
21207 }
21208
21209 if (incomplete)
21210 {
21211 if (!push_tinst_level (fn, explicit_targs))
21212 {
21213 excessive_deduction_depth = true;
21214 goto fail;
21215 }
21216 ++processing_template_decl;
21217 input_location = DECL_SOURCE_LOCATION (fn);
21218 /* Ignore any access checks; we'll see them again in
21219 instantiate_template and they might have the wrong
21220 access path at this point. */
21221 push_deferring_access_checks (dk_deferred);
21222 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21223 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21224 pop_deferring_access_checks ();
21225 input_location = loc;
21226 --processing_template_decl;
21227 pop_tinst_level ();
21228
21229 if (fntype == error_mark_node)
21230 goto fail;
21231 }
21232
21233 /* Place the explicitly specified arguments in TARGS. */
21234 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21235 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21236 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21237 if (!incomplete && CHECKING_P
21238 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21239 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21240 (targs, NUM_TMPL_ARGS (explicit_targs));
21241 }
21242
21243 if (return_type && strict != DEDUCE_CALL)
21244 {
21245 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21246 new_args[0] = return_type;
21247 memcpy (new_args + 1, args, nargs * sizeof (tree));
21248 args = new_args;
21249 ++nargs;
21250 }
21251
21252 if (!incomplete)
21253 goto deduced;
21254
21255 /* Never do unification on the 'this' parameter. */
21256 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21257
21258 if (return_type && strict == DEDUCE_CALL)
21259 {
21260 /* We're deducing for a call to the result of a template conversion
21261 function. The parms we really want are in return_type. */
21262 if (INDIRECT_TYPE_P (return_type))
21263 return_type = TREE_TYPE (return_type);
21264 parms = TYPE_ARG_TYPES (return_type);
21265 }
21266 else if (return_type)
21267 {
21268 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21269 }
21270
21271 /* We allow incomplete unification without an error message here
21272 because the standard doesn't seem to explicitly prohibit it. Our
21273 callers must be ready to deal with unification failures in any
21274 event. */
21275
21276 /* If we aren't explaining yet, push tinst context so we can see where
21277 any errors (e.g. from class instantiations triggered by instantiation
21278 of default template arguments) come from. If we are explaining, this
21279 context is redundant. */
21280 if (!explain_p && !push_tinst_level (fn, targs))
21281 {
21282 excessive_deduction_depth = true;
21283 goto fail;
21284 }
21285
21286 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21287 full_targs, parms, args, nargs, /*subr=*/0,
21288 strict, &checks, explain_p);
21289 if (!explain_p)
21290 pop_tinst_level ();
21291 if (!ok)
21292 goto fail;
21293
21294 /* Now that we have bindings for all of the template arguments,
21295 ensure that the arguments deduced for the template template
21296 parameters have compatible template parameter lists. We cannot
21297 check this property before we have deduced all template
21298 arguments, because the template parameter types of a template
21299 template parameter might depend on prior template parameters
21300 deduced after the template template parameter. The following
21301 ill-formed example illustrates this issue:
21302
21303 template<typename T, template<T> class C> void f(C<5>, T);
21304
21305 template<int N> struct X {};
21306
21307 void g() {
21308 f(X<5>(), 5l); // error: template argument deduction fails
21309 }
21310
21311 The template parameter list of 'C' depends on the template type
21312 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21313 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21314 time that we deduce 'C'. */
21315 if (!template_template_parm_bindings_ok_p
21316 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21317 {
21318 unify_inconsistent_template_template_parameters (explain_p);
21319 goto fail;
21320 }
21321
21322 deduced:
21323
21324 /* CWG2369: Check satisfaction before non-deducible conversions. */
21325 if (!constraints_satisfied_p (fn, targs))
21326 {
21327 if (explain_p)
21328 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21329 goto fail;
21330 }
21331
21332 /* DR 1391: All parameters have args, now check non-dependent parms for
21333 convertibility. We don't do this if all args were explicitly specified,
21334 as the standard says that we substitute explicit args immediately. */
21335 if (incomplete
21336 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21337 convs, explain_p))
21338 goto fail;
21339
21340 /* All is well so far. Now, check:
21341
21342 [temp.deduct]
21343
21344 When all template arguments have been deduced, all uses of
21345 template parameters in nondeduced contexts are replaced with
21346 the corresponding deduced argument values. If the
21347 substitution results in an invalid type, as described above,
21348 type deduction fails. */
21349 if (!push_tinst_level (fn, targs))
21350 {
21351 excessive_deduction_depth = true;
21352 goto fail;
21353 }
21354
21355 /* Also collect access checks from the instantiation. */
21356 reopen_deferring_access_checks (checks);
21357
21358 decl = instantiate_template (fn, targs, complain);
21359
21360 checks = get_deferred_access_checks ();
21361 pop_deferring_access_checks ();
21362
21363 pop_tinst_level ();
21364
21365 if (decl == error_mark_node)
21366 goto fail;
21367
21368 /* Now perform any access checks encountered during substitution. */
21369 push_access_scope (decl);
21370 ok = perform_access_checks (checks, complain);
21371 pop_access_scope (decl);
21372 if (!ok)
21373 goto fail;
21374
21375 /* If we're looking for an exact match, check that what we got
21376 is indeed an exact match. It might not be if some template
21377 parameters are used in non-deduced contexts. But don't check
21378 for an exact match if we have dependent template arguments;
21379 in that case we're doing partial ordering, and we already know
21380 that we have two candidates that will provide the actual type. */
21381 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21382 {
21383 tree substed = TREE_TYPE (decl);
21384 unsigned int i;
21385
21386 tree sarg
21387 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21388 if (return_type)
21389 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21390 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21391 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21392 {
21393 unify_type_mismatch (explain_p, args[i],
21394 TREE_VALUE (sarg));
21395 goto fail;
21396 }
21397 }
21398
21399 /* After doing deduction with the inherited constructor, actually return an
21400 instantiation of the inheriting constructor. */
21401 if (orig_fn != fn)
21402 decl = instantiate_template (orig_fn, targs, complain);
21403
21404 r = decl;
21405
21406 fail:
21407 --deduction_depth;
21408 if (excessive_deduction_depth)
21409 {
21410 if (deduction_depth == 0)
21411 /* Reset once we're all the way out. */
21412 excessive_deduction_depth = false;
21413 }
21414
21415 return r;
21416 }
21417
21418 /* Adjust types before performing type deduction, as described in
21419 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21420 sections are symmetric. PARM is the type of a function parameter
21421 or the return type of the conversion function. ARG is the type of
21422 the argument passed to the call, or the type of the value
21423 initialized with the result of the conversion function.
21424 ARG_EXPR is the original argument expression, which may be null. */
21425
21426 static int
21427 maybe_adjust_types_for_deduction (unification_kind_t strict,
21428 tree* parm,
21429 tree* arg,
21430 tree arg_expr)
21431 {
21432 int result = 0;
21433
21434 switch (strict)
21435 {
21436 case DEDUCE_CALL:
21437 break;
21438
21439 case DEDUCE_CONV:
21440 /* Swap PARM and ARG throughout the remainder of this
21441 function; the handling is precisely symmetric since PARM
21442 will initialize ARG rather than vice versa. */
21443 std::swap (parm, arg);
21444 break;
21445
21446 case DEDUCE_EXACT:
21447 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21448 too, but here handle it by stripping the reference from PARM
21449 rather than by adding it to ARG. */
21450 if (TYPE_REF_P (*parm)
21451 && TYPE_REF_IS_RVALUE (*parm)
21452 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21453 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21454 && TYPE_REF_P (*arg)
21455 && !TYPE_REF_IS_RVALUE (*arg))
21456 *parm = TREE_TYPE (*parm);
21457 /* Nothing else to do in this case. */
21458 return 0;
21459
21460 default:
21461 gcc_unreachable ();
21462 }
21463
21464 if (!TYPE_REF_P (*parm))
21465 {
21466 /* [temp.deduct.call]
21467
21468 If P is not a reference type:
21469
21470 --If A is an array type, the pointer type produced by the
21471 array-to-pointer standard conversion (_conv.array_) is
21472 used in place of A for type deduction; otherwise,
21473
21474 --If A is a function type, the pointer type produced by
21475 the function-to-pointer standard conversion
21476 (_conv.func_) is used in place of A for type deduction;
21477 otherwise,
21478
21479 --If A is a cv-qualified type, the top level
21480 cv-qualifiers of A's type are ignored for type
21481 deduction. */
21482 if (TREE_CODE (*arg) == ARRAY_TYPE)
21483 *arg = build_pointer_type (TREE_TYPE (*arg));
21484 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21485 *arg = build_pointer_type (*arg);
21486 else
21487 *arg = TYPE_MAIN_VARIANT (*arg);
21488 }
21489
21490 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21491 reference to a cv-unqualified template parameter that does not represent a
21492 template parameter of a class template (during class template argument
21493 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21494 an lvalue, the type "lvalue reference to A" is used in place of A for type
21495 deduction. */
21496 if (TYPE_REF_P (*parm)
21497 && TYPE_REF_IS_RVALUE (*parm)
21498 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21499 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21500 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21501 && (arg_expr ? lvalue_p (arg_expr)
21502 /* try_one_overload doesn't provide an arg_expr, but
21503 functions are always lvalues. */
21504 : TREE_CODE (*arg) == FUNCTION_TYPE))
21505 *arg = build_reference_type (*arg);
21506
21507 /* [temp.deduct.call]
21508
21509 If P is a cv-qualified type, the top level cv-qualifiers
21510 of P's type are ignored for type deduction. If P is a
21511 reference type, the type referred to by P is used for
21512 type deduction. */
21513 *parm = TYPE_MAIN_VARIANT (*parm);
21514 if (TYPE_REF_P (*parm))
21515 {
21516 *parm = TREE_TYPE (*parm);
21517 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21518 }
21519
21520 /* DR 322. For conversion deduction, remove a reference type on parm
21521 too (which has been swapped into ARG). */
21522 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21523 *arg = TREE_TYPE (*arg);
21524
21525 return result;
21526 }
21527
21528 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21529 template which doesn't contain any deducible template parameters; check if
21530 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21531 unify_one_argument. */
21532
21533 static int
21534 check_non_deducible_conversion (tree parm, tree arg, int strict,
21535 int flags, struct conversion **conv_p,
21536 bool explain_p)
21537 {
21538 tree type;
21539
21540 if (!TYPE_P (arg))
21541 type = TREE_TYPE (arg);
21542 else
21543 type = arg;
21544
21545 if (same_type_p (parm, type))
21546 return unify_success (explain_p);
21547
21548 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21549 if (strict == DEDUCE_CONV)
21550 {
21551 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21552 return unify_success (explain_p);
21553 }
21554 else if (strict != DEDUCE_EXACT)
21555 {
21556 bool ok = false;
21557 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21558 if (conv_p)
21559 /* Avoid recalculating this in add_function_candidate. */
21560 ok = (*conv_p
21561 = good_conversion (parm, type, conv_arg, flags, complain));
21562 else
21563 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21564 if (ok)
21565 return unify_success (explain_p);
21566 }
21567
21568 if (strict == DEDUCE_EXACT)
21569 return unify_type_mismatch (explain_p, parm, arg);
21570 else
21571 return unify_arg_conversion (explain_p, parm, type, arg);
21572 }
21573
21574 static bool uses_deducible_template_parms (tree type);
21575
21576 /* Returns true iff the expression EXPR is one from which a template
21577 argument can be deduced. In other words, if it's an undecorated
21578 use of a template non-type parameter. */
21579
21580 static bool
21581 deducible_expression (tree expr)
21582 {
21583 /* Strip implicit conversions. */
21584 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21585 expr = TREE_OPERAND (expr, 0);
21586 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21587 }
21588
21589 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21590 deducible way; that is, if it has a max value of <PARM> - 1. */
21591
21592 static bool
21593 deducible_array_bound (tree domain)
21594 {
21595 if (domain == NULL_TREE)
21596 return false;
21597
21598 tree max = TYPE_MAX_VALUE (domain);
21599 if (TREE_CODE (max) != MINUS_EXPR)
21600 return false;
21601
21602 return deducible_expression (TREE_OPERAND (max, 0));
21603 }
21604
21605 /* Returns true iff the template arguments ARGS use a template parameter
21606 in a deducible way. */
21607
21608 static bool
21609 deducible_template_args (tree args)
21610 {
21611 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21612 {
21613 bool deducible;
21614 tree elt = TREE_VEC_ELT (args, i);
21615 if (ARGUMENT_PACK_P (elt))
21616 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21617 else
21618 {
21619 if (PACK_EXPANSION_P (elt))
21620 elt = PACK_EXPANSION_PATTERN (elt);
21621 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21622 deducible = true;
21623 else if (TYPE_P (elt))
21624 deducible = uses_deducible_template_parms (elt);
21625 else
21626 deducible = deducible_expression (elt);
21627 }
21628 if (deducible)
21629 return true;
21630 }
21631 return false;
21632 }
21633
21634 /* Returns true iff TYPE contains any deducible references to template
21635 parameters, as per 14.8.2.5. */
21636
21637 static bool
21638 uses_deducible_template_parms (tree type)
21639 {
21640 if (PACK_EXPANSION_P (type))
21641 type = PACK_EXPANSION_PATTERN (type);
21642
21643 /* T
21644 cv-list T
21645 TT<T>
21646 TT<i>
21647 TT<> */
21648 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21649 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21650 return true;
21651
21652 /* T*
21653 T&
21654 T&& */
21655 if (INDIRECT_TYPE_P (type))
21656 return uses_deducible_template_parms (TREE_TYPE (type));
21657
21658 /* T[integer-constant ]
21659 type [i] */
21660 if (TREE_CODE (type) == ARRAY_TYPE)
21661 return (uses_deducible_template_parms (TREE_TYPE (type))
21662 || deducible_array_bound (TYPE_DOMAIN (type)));
21663
21664 /* T type ::*
21665 type T::*
21666 T T::*
21667 T (type ::*)()
21668 type (T::*)()
21669 type (type ::*)(T)
21670 type (T::*)(T)
21671 T (type ::*)(T)
21672 T (T::*)()
21673 T (T::*)(T) */
21674 if (TYPE_PTRMEM_P (type))
21675 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21676 || (uses_deducible_template_parms
21677 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21678
21679 /* template-name <T> (where template-name refers to a class template)
21680 template-name <i> (where template-name refers to a class template) */
21681 if (CLASS_TYPE_P (type)
21682 && CLASSTYPE_TEMPLATE_INFO (type)
21683 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21684 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21685 (CLASSTYPE_TI_ARGS (type)));
21686
21687 /* type (T)
21688 T()
21689 T(T) */
21690 if (FUNC_OR_METHOD_TYPE_P (type))
21691 {
21692 if (uses_deducible_template_parms (TREE_TYPE (type)))
21693 return true;
21694 tree parm = TYPE_ARG_TYPES (type);
21695 if (TREE_CODE (type) == METHOD_TYPE)
21696 parm = TREE_CHAIN (parm);
21697 for (; parm; parm = TREE_CHAIN (parm))
21698 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21699 return true;
21700 }
21701
21702 return false;
21703 }
21704
21705 /* Subroutine of type_unification_real and unify_pack_expansion to
21706 handle unification of a single P/A pair. Parameters are as
21707 for those functions. */
21708
21709 static int
21710 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21711 int subr, unification_kind_t strict,
21712 bool explain_p)
21713 {
21714 tree arg_expr = NULL_TREE;
21715 int arg_strict;
21716
21717 if (arg == error_mark_node || parm == error_mark_node)
21718 return unify_invalid (explain_p);
21719 if (arg == unknown_type_node)
21720 /* We can't deduce anything from this, but we might get all the
21721 template args from other function args. */
21722 return unify_success (explain_p);
21723
21724 /* Implicit conversions (Clause 4) will be performed on a function
21725 argument to convert it to the type of the corresponding function
21726 parameter if the parameter type contains no template-parameters that
21727 participate in template argument deduction. */
21728 if (strict != DEDUCE_EXACT
21729 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21730 /* For function parameters with no deducible template parameters,
21731 just return. We'll check non-dependent conversions later. */
21732 return unify_success (explain_p);
21733
21734 switch (strict)
21735 {
21736 case DEDUCE_CALL:
21737 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21738 | UNIFY_ALLOW_MORE_CV_QUAL
21739 | UNIFY_ALLOW_DERIVED);
21740 break;
21741
21742 case DEDUCE_CONV:
21743 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21744 break;
21745
21746 case DEDUCE_EXACT:
21747 arg_strict = UNIFY_ALLOW_NONE;
21748 break;
21749
21750 default:
21751 gcc_unreachable ();
21752 }
21753
21754 /* We only do these transformations if this is the top-level
21755 parameter_type_list in a call or declaration matching; in other
21756 situations (nested function declarators, template argument lists) we
21757 won't be comparing a type to an expression, and we don't do any type
21758 adjustments. */
21759 if (!subr)
21760 {
21761 if (!TYPE_P (arg))
21762 {
21763 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21764 if (type_unknown_p (arg))
21765 {
21766 /* [temp.deduct.type] A template-argument can be
21767 deduced from a pointer to function or pointer
21768 to member function argument if the set of
21769 overloaded functions does not contain function
21770 templates and at most one of a set of
21771 overloaded functions provides a unique
21772 match. */
21773 resolve_overloaded_unification (tparms, targs, parm,
21774 arg, strict,
21775 arg_strict, explain_p);
21776 /* If a unique match was not found, this is a
21777 non-deduced context, so we still succeed. */
21778 return unify_success (explain_p);
21779 }
21780
21781 arg_expr = arg;
21782 arg = unlowered_expr_type (arg);
21783 if (arg == error_mark_node)
21784 return unify_invalid (explain_p);
21785 }
21786
21787 arg_strict |=
21788 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21789 }
21790 else
21791 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21792 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21793 return unify_template_argument_mismatch (explain_p, parm, arg);
21794
21795 /* For deduction from an init-list we need the actual list. */
21796 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21797 arg = arg_expr;
21798 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21799 }
21800
21801 /* for_each_template_parm callback that always returns 0. */
21802
21803 static int
21804 zero_r (tree, void *)
21805 {
21806 return 0;
21807 }
21808
21809 /* for_each_template_parm any_fn callback to handle deduction of a template
21810 type argument from the type of an array bound. */
21811
21812 static int
21813 array_deduction_r (tree t, void *data)
21814 {
21815 tree_pair_p d = (tree_pair_p)data;
21816 tree &tparms = d->purpose;
21817 tree &targs = d->value;
21818
21819 if (TREE_CODE (t) == ARRAY_TYPE)
21820 if (tree dom = TYPE_DOMAIN (t))
21821 if (tree max = TYPE_MAX_VALUE (dom))
21822 {
21823 if (TREE_CODE (max) == MINUS_EXPR)
21824 max = TREE_OPERAND (max, 0);
21825 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21826 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21827 UNIFY_ALLOW_NONE, /*explain*/false);
21828 }
21829
21830 /* Keep walking. */
21831 return 0;
21832 }
21833
21834 /* Try to deduce any not-yet-deduced template type arguments from the type of
21835 an array bound. This is handled separately from unify because 14.8.2.5 says
21836 "The type of a type parameter is only deduced from an array bound if it is
21837 not otherwise deduced." */
21838
21839 static void
21840 try_array_deduction (tree tparms, tree targs, tree parm)
21841 {
21842 tree_pair_s data = { tparms, targs };
21843 hash_set<tree> visited;
21844 for_each_template_parm (parm, zero_r, &data, &visited,
21845 /*nondeduced*/false, array_deduction_r);
21846 }
21847
21848 /* Most parms like fn_type_unification.
21849
21850 If SUBR is 1, we're being called recursively (to unify the
21851 arguments of a function or method parameter of a function
21852 template).
21853
21854 CHECKS is a pointer to a vector of access checks encountered while
21855 substituting default template arguments. */
21856
21857 static int
21858 type_unification_real (tree tparms,
21859 tree full_targs,
21860 tree xparms,
21861 const tree *xargs,
21862 unsigned int xnargs,
21863 int subr,
21864 unification_kind_t strict,
21865 vec<deferred_access_check, va_gc> **checks,
21866 bool explain_p)
21867 {
21868 tree parm, arg;
21869 int i;
21870 int ntparms = TREE_VEC_LENGTH (tparms);
21871 int saw_undeduced = 0;
21872 tree parms;
21873 const tree *args;
21874 unsigned int nargs;
21875 unsigned int ia;
21876
21877 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21878 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21879 gcc_assert (ntparms > 0);
21880
21881 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21882
21883 /* Reset the number of non-defaulted template arguments contained
21884 in TARGS. */
21885 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21886
21887 again:
21888 parms = xparms;
21889 args = xargs;
21890 nargs = xnargs;
21891
21892 ia = 0;
21893 while (parms && parms != void_list_node
21894 && ia < nargs)
21895 {
21896 parm = TREE_VALUE (parms);
21897
21898 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21899 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21900 /* For a function parameter pack that occurs at the end of the
21901 parameter-declaration-list, the type A of each remaining
21902 argument of the call is compared with the type P of the
21903 declarator-id of the function parameter pack. */
21904 break;
21905
21906 parms = TREE_CHAIN (parms);
21907
21908 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21909 /* For a function parameter pack that does not occur at the
21910 end of the parameter-declaration-list, the type of the
21911 parameter pack is a non-deduced context. */
21912 continue;
21913
21914 arg = args[ia];
21915 ++ia;
21916
21917 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21918 explain_p))
21919 return 1;
21920 }
21921
21922 if (parms
21923 && parms != void_list_node
21924 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21925 {
21926 /* Unify the remaining arguments with the pack expansion type. */
21927 tree argvec;
21928 tree parmvec = make_tree_vec (1);
21929
21930 /* Allocate a TREE_VEC and copy in all of the arguments */
21931 argvec = make_tree_vec (nargs - ia);
21932 for (i = 0; ia < nargs; ++ia, ++i)
21933 TREE_VEC_ELT (argvec, i) = args[ia];
21934
21935 /* Copy the parameter into parmvec. */
21936 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21937 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21938 /*subr=*/subr, explain_p))
21939 return 1;
21940
21941 /* Advance to the end of the list of parameters. */
21942 parms = TREE_CHAIN (parms);
21943 }
21944
21945 /* Fail if we've reached the end of the parm list, and more args
21946 are present, and the parm list isn't variadic. */
21947 if (ia < nargs && parms == void_list_node)
21948 return unify_too_many_arguments (explain_p, nargs, ia);
21949 /* Fail if parms are left and they don't have default values and
21950 they aren't all deduced as empty packs (c++/57397). This is
21951 consistent with sufficient_parms_p. */
21952 if (parms && parms != void_list_node
21953 && TREE_PURPOSE (parms) == NULL_TREE)
21954 {
21955 unsigned int count = nargs;
21956 tree p = parms;
21957 bool type_pack_p;
21958 do
21959 {
21960 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21961 if (!type_pack_p)
21962 count++;
21963 p = TREE_CHAIN (p);
21964 }
21965 while (p && p != void_list_node);
21966 if (count != nargs)
21967 return unify_too_few_arguments (explain_p, ia, count,
21968 type_pack_p);
21969 }
21970
21971 if (!subr)
21972 {
21973 tsubst_flags_t complain = (explain_p
21974 ? tf_warning_or_error
21975 : tf_none);
21976 bool tried_array_deduction = (cxx_dialect < cxx17);
21977
21978 for (i = 0; i < ntparms; i++)
21979 {
21980 tree targ = TREE_VEC_ELT (targs, i);
21981 tree tparm = TREE_VEC_ELT (tparms, i);
21982
21983 /* Clear the "incomplete" flags on all argument packs now so that
21984 substituting them into later default arguments works. */
21985 if (targ && ARGUMENT_PACK_P (targ))
21986 {
21987 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21988 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21989 }
21990
21991 if (targ || tparm == error_mark_node)
21992 continue;
21993 tparm = TREE_VALUE (tparm);
21994
21995 if (TREE_CODE (tparm) == TYPE_DECL
21996 && !tried_array_deduction)
21997 {
21998 try_array_deduction (tparms, targs, xparms);
21999 tried_array_deduction = true;
22000 if (TREE_VEC_ELT (targs, i))
22001 continue;
22002 }
22003
22004 /* If this is an undeduced nontype parameter that depends on
22005 a type parameter, try another pass; its type may have been
22006 deduced from a later argument than the one from which
22007 this parameter can be deduced. */
22008 if (TREE_CODE (tparm) == PARM_DECL
22009 && uses_template_parms (TREE_TYPE (tparm))
22010 && saw_undeduced < 2)
22011 {
22012 saw_undeduced = 1;
22013 continue;
22014 }
22015
22016 /* Core issue #226 (C++0x) [temp.deduct]:
22017
22018 If a template argument has not been deduced, its
22019 default template argument, if any, is used.
22020
22021 When we are in C++98 mode, TREE_PURPOSE will either
22022 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22023 to explicitly check cxx_dialect here. */
22024 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22025 /* OK, there is a default argument. Wait until after the
22026 conversion check to do substitution. */
22027 continue;
22028
22029 /* If the type parameter is a parameter pack, then it will
22030 be deduced to an empty parameter pack. */
22031 if (template_parameter_pack_p (tparm))
22032 {
22033 tree arg;
22034
22035 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22036 {
22037 arg = make_node (NONTYPE_ARGUMENT_PACK);
22038 TREE_CONSTANT (arg) = 1;
22039 }
22040 else
22041 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22042
22043 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22044
22045 TREE_VEC_ELT (targs, i) = arg;
22046 continue;
22047 }
22048
22049 return unify_parameter_deduction_failure (explain_p, tparm);
22050 }
22051
22052 /* Now substitute into the default template arguments. */
22053 for (i = 0; i < ntparms; i++)
22054 {
22055 tree targ = TREE_VEC_ELT (targs, i);
22056 tree tparm = TREE_VEC_ELT (tparms, i);
22057
22058 if (targ || tparm == error_mark_node)
22059 continue;
22060 tree parm = TREE_VALUE (tparm);
22061 tree arg = TREE_PURPOSE (tparm);
22062 reopen_deferring_access_checks (*checks);
22063 location_t save_loc = input_location;
22064 if (DECL_P (parm))
22065 input_location = DECL_SOURCE_LOCATION (parm);
22066
22067 if (saw_undeduced == 1
22068 && TREE_CODE (parm) == PARM_DECL
22069 && uses_template_parms (TREE_TYPE (parm)))
22070 {
22071 /* The type of this non-type parameter depends on undeduced
22072 parameters. Don't try to use its default argument yet,
22073 since we might deduce an argument for it on the next pass,
22074 but do check whether the arguments we already have cause
22075 substitution failure, so that that happens before we try
22076 later default arguments (78489). */
22077 ++processing_template_decl;
22078 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22079 NULL_TREE);
22080 --processing_template_decl;
22081 if (type == error_mark_node)
22082 arg = error_mark_node;
22083 else
22084 arg = NULL_TREE;
22085 }
22086 else
22087 {
22088 /* Even if the call is happening in template context, getting
22089 here means it's non-dependent, and a default argument is
22090 considered a separate definition under [temp.decls], so we can
22091 do this substitution without processing_template_decl. This
22092 is important if the default argument contains something that
22093 might be instantiation-dependent like access (87480). */
22094 processing_template_decl_sentinel s;
22095 tree substed = NULL_TREE;
22096 if (saw_undeduced == 1)
22097 {
22098 /* First instatiate in template context, in case we still
22099 depend on undeduced template parameters. */
22100 ++processing_template_decl;
22101 substed = tsubst_template_arg (arg, full_targs, complain,
22102 NULL_TREE);
22103 --processing_template_decl;
22104 if (substed != error_mark_node
22105 && !uses_template_parms (substed))
22106 /* We replaced all the tparms, substitute again out of
22107 template context. */
22108 substed = NULL_TREE;
22109 }
22110 if (!substed)
22111 substed = tsubst_template_arg (arg, full_targs, complain,
22112 NULL_TREE);
22113
22114 if (!uses_template_parms (substed))
22115 arg = convert_template_argument (parm, substed, full_targs,
22116 complain, i, NULL_TREE);
22117 else if (saw_undeduced == 1)
22118 arg = NULL_TREE;
22119 else
22120 arg = error_mark_node;
22121 }
22122
22123 input_location = save_loc;
22124 *checks = get_deferred_access_checks ();
22125 pop_deferring_access_checks ();
22126
22127 if (arg == error_mark_node)
22128 return 1;
22129 else if (arg)
22130 {
22131 TREE_VEC_ELT (targs, i) = arg;
22132 /* The position of the first default template argument,
22133 is also the number of non-defaulted arguments in TARGS.
22134 Record that. */
22135 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22136 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22137 }
22138 }
22139
22140 if (saw_undeduced++ == 1)
22141 goto again;
22142 }
22143
22144 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22145 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22146
22147 return unify_success (explain_p);
22148 }
22149
22150 /* Subroutine of type_unification_real. Args are like the variables
22151 at the call site. ARG is an overloaded function (or template-id);
22152 we try deducing template args from each of the overloads, and if
22153 only one succeeds, we go with that. Modifies TARGS and returns
22154 true on success. */
22155
22156 static bool
22157 resolve_overloaded_unification (tree tparms,
22158 tree targs,
22159 tree parm,
22160 tree arg,
22161 unification_kind_t strict,
22162 int sub_strict,
22163 bool explain_p)
22164 {
22165 tree tempargs = copy_node (targs);
22166 int good = 0;
22167 tree goodfn = NULL_TREE;
22168 bool addr_p;
22169
22170 if (TREE_CODE (arg) == ADDR_EXPR)
22171 {
22172 arg = TREE_OPERAND (arg, 0);
22173 addr_p = true;
22174 }
22175 else
22176 addr_p = false;
22177
22178 if (TREE_CODE (arg) == COMPONENT_REF)
22179 /* Handle `&x' where `x' is some static or non-static member
22180 function name. */
22181 arg = TREE_OPERAND (arg, 1);
22182
22183 if (TREE_CODE (arg) == OFFSET_REF)
22184 arg = TREE_OPERAND (arg, 1);
22185
22186 /* Strip baselink information. */
22187 if (BASELINK_P (arg))
22188 arg = BASELINK_FUNCTIONS (arg);
22189
22190 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22191 {
22192 /* If we got some explicit template args, we need to plug them into
22193 the affected templates before we try to unify, in case the
22194 explicit args will completely resolve the templates in question. */
22195
22196 int ok = 0;
22197 tree expl_subargs = TREE_OPERAND (arg, 1);
22198 arg = TREE_OPERAND (arg, 0);
22199
22200 for (lkp_iterator iter (arg); iter; ++iter)
22201 {
22202 tree fn = *iter;
22203 tree subargs, elem;
22204
22205 if (TREE_CODE (fn) != TEMPLATE_DECL)
22206 continue;
22207
22208 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22209 expl_subargs, NULL_TREE, tf_none,
22210 /*require_all_args=*/true,
22211 /*use_default_args=*/true);
22212 if (subargs != error_mark_node
22213 && !any_dependent_template_arguments_p (subargs))
22214 {
22215 fn = instantiate_template (fn, subargs, tf_none);
22216 if (!constraints_satisfied_p (fn))
22217 continue;
22218 if (undeduced_auto_decl (fn))
22219 {
22220 /* Instantiate the function to deduce its return type. */
22221 ++function_depth;
22222 instantiate_decl (fn, /*defer*/false, /*class*/false);
22223 --function_depth;
22224 }
22225
22226 elem = TREE_TYPE (fn);
22227 if (try_one_overload (tparms, targs, tempargs, parm,
22228 elem, strict, sub_strict, addr_p, explain_p)
22229 && (!goodfn || !same_type_p (goodfn, elem)))
22230 {
22231 goodfn = elem;
22232 ++good;
22233 }
22234 }
22235 else if (subargs)
22236 ++ok;
22237 }
22238 /* If no templates (or more than one) are fully resolved by the
22239 explicit arguments, this template-id is a non-deduced context; it
22240 could still be OK if we deduce all template arguments for the
22241 enclosing call through other arguments. */
22242 if (good != 1)
22243 good = ok;
22244 }
22245 else if (!OVL_P (arg))
22246 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22247 -- but the deduction does not succeed because the expression is
22248 not just the function on its own. */
22249 return false;
22250 else
22251 for (lkp_iterator iter (arg); iter; ++iter)
22252 {
22253 tree fn = *iter;
22254 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22255 strict, sub_strict, addr_p, explain_p)
22256 && (!goodfn || !decls_match (goodfn, fn)))
22257 {
22258 goodfn = fn;
22259 ++good;
22260 }
22261 }
22262
22263 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22264 to function or pointer to member function argument if the set of
22265 overloaded functions does not contain function templates and at most
22266 one of a set of overloaded functions provides a unique match.
22267
22268 So if we found multiple possibilities, we return success but don't
22269 deduce anything. */
22270
22271 if (good == 1)
22272 {
22273 int i = TREE_VEC_LENGTH (targs);
22274 for (; i--; )
22275 if (TREE_VEC_ELT (tempargs, i))
22276 {
22277 tree old = TREE_VEC_ELT (targs, i);
22278 tree new_ = TREE_VEC_ELT (tempargs, i);
22279 if (new_ && old && ARGUMENT_PACK_P (old)
22280 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22281 /* Don't forget explicit template arguments in a pack. */
22282 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22283 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22284 TREE_VEC_ELT (targs, i) = new_;
22285 }
22286 }
22287 if (good)
22288 return true;
22289
22290 return false;
22291 }
22292
22293 /* Core DR 115: In contexts where deduction is done and fails, or in
22294 contexts where deduction is not done, if a template argument list is
22295 specified and it, along with any default template arguments, identifies
22296 a single function template specialization, then the template-id is an
22297 lvalue for the function template specialization. */
22298
22299 tree
22300 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22301 {
22302 tree expr, offset, baselink;
22303 bool addr;
22304
22305 if (!type_unknown_p (orig_expr))
22306 return orig_expr;
22307
22308 expr = orig_expr;
22309 addr = false;
22310 offset = NULL_TREE;
22311 baselink = NULL_TREE;
22312
22313 if (TREE_CODE (expr) == ADDR_EXPR)
22314 {
22315 expr = TREE_OPERAND (expr, 0);
22316 addr = true;
22317 }
22318 if (TREE_CODE (expr) == OFFSET_REF)
22319 {
22320 offset = expr;
22321 expr = TREE_OPERAND (expr, 1);
22322 }
22323 if (BASELINK_P (expr))
22324 {
22325 baselink = expr;
22326 expr = BASELINK_FUNCTIONS (expr);
22327 }
22328
22329 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22330 {
22331 int good = 0;
22332 tree goodfn = NULL_TREE;
22333
22334 /* If we got some explicit template args, we need to plug them into
22335 the affected templates before we try to unify, in case the
22336 explicit args will completely resolve the templates in question. */
22337
22338 tree expl_subargs = TREE_OPERAND (expr, 1);
22339 tree arg = TREE_OPERAND (expr, 0);
22340 tree badfn = NULL_TREE;
22341 tree badargs = NULL_TREE;
22342
22343 for (lkp_iterator iter (arg); iter; ++iter)
22344 {
22345 tree fn = *iter;
22346 tree subargs, elem;
22347
22348 if (TREE_CODE (fn) != TEMPLATE_DECL)
22349 continue;
22350
22351 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22352 expl_subargs, NULL_TREE, tf_none,
22353 /*require_all_args=*/true,
22354 /*use_default_args=*/true);
22355 if (subargs != error_mark_node
22356 && !any_dependent_template_arguments_p (subargs))
22357 {
22358 elem = instantiate_template (fn, subargs, tf_none);
22359 if (elem == error_mark_node)
22360 {
22361 badfn = fn;
22362 badargs = subargs;
22363 }
22364 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22365 && constraints_satisfied_p (elem))
22366 {
22367 goodfn = elem;
22368 ++good;
22369 }
22370 }
22371 }
22372 if (good == 1)
22373 {
22374 mark_used (goodfn);
22375 expr = goodfn;
22376 if (baselink)
22377 expr = build_baselink (BASELINK_BINFO (baselink),
22378 BASELINK_ACCESS_BINFO (baselink),
22379 expr, BASELINK_OPTYPE (baselink));
22380 if (offset)
22381 {
22382 tree base
22383 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22384 expr = build_offset_ref (base, expr, addr, complain);
22385 }
22386 if (addr)
22387 expr = cp_build_addr_expr (expr, complain);
22388 return expr;
22389 }
22390 else if (good == 0 && badargs && (complain & tf_error))
22391 /* There were no good options and at least one bad one, so let the
22392 user know what the problem is. */
22393 instantiate_template (badfn, badargs, complain);
22394 }
22395 return orig_expr;
22396 }
22397
22398 /* As above, but error out if the expression remains overloaded. */
22399
22400 tree
22401 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22402 {
22403 exp = resolve_nondeduced_context (exp, complain);
22404 if (type_unknown_p (exp))
22405 {
22406 if (complain & tf_error)
22407 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22408 return error_mark_node;
22409 }
22410 return exp;
22411 }
22412
22413 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22414 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22415 different overloads deduce different arguments for a given parm.
22416 ADDR_P is true if the expression for which deduction is being
22417 performed was of the form "& fn" rather than simply "fn".
22418
22419 Returns 1 on success. */
22420
22421 static int
22422 try_one_overload (tree tparms,
22423 tree orig_targs,
22424 tree targs,
22425 tree parm,
22426 tree arg,
22427 unification_kind_t strict,
22428 int sub_strict,
22429 bool addr_p,
22430 bool explain_p)
22431 {
22432 int nargs;
22433 tree tempargs;
22434 int i;
22435
22436 if (arg == error_mark_node)
22437 return 0;
22438
22439 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22440 to function or pointer to member function argument if the set of
22441 overloaded functions does not contain function templates and at most
22442 one of a set of overloaded functions provides a unique match.
22443
22444 So if this is a template, just return success. */
22445
22446 if (uses_template_parms (arg))
22447 return 1;
22448
22449 if (TREE_CODE (arg) == METHOD_TYPE)
22450 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22451 else if (addr_p)
22452 arg = build_pointer_type (arg);
22453
22454 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22455
22456 /* We don't copy orig_targs for this because if we have already deduced
22457 some template args from previous args, unify would complain when we
22458 try to deduce a template parameter for the same argument, even though
22459 there isn't really a conflict. */
22460 nargs = TREE_VEC_LENGTH (targs);
22461 tempargs = make_tree_vec (nargs);
22462
22463 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22464 return 0;
22465
22466 /* First make sure we didn't deduce anything that conflicts with
22467 explicitly specified args. */
22468 for (i = nargs; i--; )
22469 {
22470 tree elt = TREE_VEC_ELT (tempargs, i);
22471 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22472
22473 if (!elt)
22474 /*NOP*/;
22475 else if (uses_template_parms (elt))
22476 /* Since we're unifying against ourselves, we will fill in
22477 template args used in the function parm list with our own
22478 template parms. Discard them. */
22479 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22480 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22481 {
22482 /* Check that the argument at each index of the deduced argument pack
22483 is equivalent to the corresponding explicitly specified argument.
22484 We may have deduced more arguments than were explicitly specified,
22485 and that's OK. */
22486
22487 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22488 that's wrong if we deduce the same argument pack from multiple
22489 function arguments: it's only incomplete the first time. */
22490
22491 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22492 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22493
22494 if (TREE_VEC_LENGTH (deduced_pack)
22495 < TREE_VEC_LENGTH (explicit_pack))
22496 return 0;
22497
22498 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22499 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22500 TREE_VEC_ELT (deduced_pack, j)))
22501 return 0;
22502 }
22503 else if (oldelt && !template_args_equal (oldelt, elt))
22504 return 0;
22505 }
22506
22507 for (i = nargs; i--; )
22508 {
22509 tree elt = TREE_VEC_ELT (tempargs, i);
22510
22511 if (elt)
22512 TREE_VEC_ELT (targs, i) = elt;
22513 }
22514
22515 return 1;
22516 }
22517
22518 /* PARM is a template class (perhaps with unbound template
22519 parameters). ARG is a fully instantiated type. If ARG can be
22520 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22521 TARGS are as for unify. */
22522
22523 static tree
22524 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22525 bool explain_p)
22526 {
22527 tree copy_of_targs;
22528
22529 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22530 return NULL_TREE;
22531 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22532 /* Matches anything. */;
22533 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22534 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22535 return NULL_TREE;
22536
22537 /* We need to make a new template argument vector for the call to
22538 unify. If we used TARGS, we'd clutter it up with the result of
22539 the attempted unification, even if this class didn't work out.
22540 We also don't want to commit ourselves to all the unifications
22541 we've already done, since unification is supposed to be done on
22542 an argument-by-argument basis. In other words, consider the
22543 following pathological case:
22544
22545 template <int I, int J, int K>
22546 struct S {};
22547
22548 template <int I, int J>
22549 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22550
22551 template <int I, int J, int K>
22552 void f(S<I, J, K>, S<I, I, I>);
22553
22554 void g() {
22555 S<0, 0, 0> s0;
22556 S<0, 1, 2> s2;
22557
22558 f(s0, s2);
22559 }
22560
22561 Now, by the time we consider the unification involving `s2', we
22562 already know that we must have `f<0, 0, 0>'. But, even though
22563 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22564 because there are two ways to unify base classes of S<0, 1, 2>
22565 with S<I, I, I>. If we kept the already deduced knowledge, we
22566 would reject the possibility I=1. */
22567 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22568
22569 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22570 {
22571 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22572 return NULL_TREE;
22573 return arg;
22574 }
22575
22576 /* If unification failed, we're done. */
22577 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22578 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22579 return NULL_TREE;
22580
22581 return arg;
22582 }
22583
22584 /* Given a template type PARM and a class type ARG, find the unique
22585 base type in ARG that is an instance of PARM. We do not examine
22586 ARG itself; only its base-classes. If there is not exactly one
22587 appropriate base class, return NULL_TREE. PARM may be the type of
22588 a partial specialization, as well as a plain template type. Used
22589 by unify. */
22590
22591 static enum template_base_result
22592 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22593 bool explain_p, tree *result)
22594 {
22595 tree rval = NULL_TREE;
22596 tree binfo;
22597
22598 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22599
22600 binfo = TYPE_BINFO (complete_type (arg));
22601 if (!binfo)
22602 {
22603 /* The type could not be completed. */
22604 *result = NULL_TREE;
22605 return tbr_incomplete_type;
22606 }
22607
22608 /* Walk in inheritance graph order. The search order is not
22609 important, and this avoids multiple walks of virtual bases. */
22610 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22611 {
22612 tree r = try_class_unification (tparms, targs, parm,
22613 BINFO_TYPE (binfo), explain_p);
22614
22615 if (r)
22616 {
22617 /* If there is more than one satisfactory baseclass, then:
22618
22619 [temp.deduct.call]
22620
22621 If they yield more than one possible deduced A, the type
22622 deduction fails.
22623
22624 applies. */
22625 if (rval && !same_type_p (r, rval))
22626 {
22627 *result = NULL_TREE;
22628 return tbr_ambiguous_baseclass;
22629 }
22630
22631 rval = r;
22632 }
22633 }
22634
22635 *result = rval;
22636 return tbr_success;
22637 }
22638
22639 /* Returns the level of DECL, which declares a template parameter. */
22640
22641 static int
22642 template_decl_level (tree decl)
22643 {
22644 switch (TREE_CODE (decl))
22645 {
22646 case TYPE_DECL:
22647 case TEMPLATE_DECL:
22648 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22649
22650 case PARM_DECL:
22651 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22652
22653 default:
22654 gcc_unreachable ();
22655 }
22656 return 0;
22657 }
22658
22659 /* Decide whether ARG can be unified with PARM, considering only the
22660 cv-qualifiers of each type, given STRICT as documented for unify.
22661 Returns nonzero iff the unification is OK on that basis. */
22662
22663 static int
22664 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22665 {
22666 int arg_quals = cp_type_quals (arg);
22667 int parm_quals = cp_type_quals (parm);
22668
22669 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22670 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22671 {
22672 /* Although a CVR qualifier is ignored when being applied to a
22673 substituted template parameter ([8.3.2]/1 for example), that
22674 does not allow us to unify "const T" with "int&" because both
22675 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22676 It is ok when we're allowing additional CV qualifiers
22677 at the outer level [14.8.2.1]/3,1st bullet. */
22678 if ((TYPE_REF_P (arg)
22679 || FUNC_OR_METHOD_TYPE_P (arg))
22680 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22681 return 0;
22682
22683 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22684 && (parm_quals & TYPE_QUAL_RESTRICT))
22685 return 0;
22686 }
22687
22688 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22689 && (arg_quals & parm_quals) != parm_quals)
22690 return 0;
22691
22692 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22693 && (parm_quals & arg_quals) != arg_quals)
22694 return 0;
22695
22696 return 1;
22697 }
22698
22699 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22700 void
22701 template_parm_level_and_index (tree parm, int* level, int* index)
22702 {
22703 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22704 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22705 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22706 {
22707 *index = TEMPLATE_TYPE_IDX (parm);
22708 *level = TEMPLATE_TYPE_LEVEL (parm);
22709 }
22710 else
22711 {
22712 *index = TEMPLATE_PARM_IDX (parm);
22713 *level = TEMPLATE_PARM_LEVEL (parm);
22714 }
22715 }
22716
22717 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22718 do { \
22719 if (unify (TP, TA, P, A, S, EP)) \
22720 return 1; \
22721 } while (0)
22722
22723 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22724 expansion at the end of PACKED_PARMS. Returns 0 if the type
22725 deduction succeeds, 1 otherwise. STRICT is the same as in
22726 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22727 function call argument list. We'll need to adjust the arguments to make them
22728 types. SUBR tells us if this is from a recursive call to
22729 type_unification_real, or for comparing two template argument
22730 lists. */
22731
22732 static int
22733 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22734 tree packed_args, unification_kind_t strict,
22735 bool subr, bool explain_p)
22736 {
22737 tree parm
22738 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22739 tree pattern = PACK_EXPANSION_PATTERN (parm);
22740 tree pack, packs = NULL_TREE;
22741 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22742
22743 /* Add in any args remembered from an earlier partial instantiation. */
22744 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22745 int levels = TMPL_ARGS_DEPTH (targs);
22746
22747 packed_args = expand_template_argument_pack (packed_args);
22748
22749 int len = TREE_VEC_LENGTH (packed_args);
22750
22751 /* Determine the parameter packs we will be deducing from the
22752 pattern, and record their current deductions. */
22753 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22754 pack; pack = TREE_CHAIN (pack))
22755 {
22756 tree parm_pack = TREE_VALUE (pack);
22757 int idx, level;
22758
22759 /* Only template parameter packs can be deduced, not e.g. function
22760 parameter packs or __bases or __integer_pack. */
22761 if (!TEMPLATE_PARM_P (parm_pack))
22762 continue;
22763
22764 /* Determine the index and level of this parameter pack. */
22765 template_parm_level_and_index (parm_pack, &level, &idx);
22766 if (level < levels)
22767 continue;
22768
22769 /* Keep track of the parameter packs and their corresponding
22770 argument packs. */
22771 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22772 TREE_TYPE (packs) = make_tree_vec (len - start);
22773 }
22774
22775 /* Loop through all of the arguments that have not yet been
22776 unified and unify each with the pattern. */
22777 for (i = start; i < len; i++)
22778 {
22779 tree parm;
22780 bool any_explicit = false;
22781 tree arg = TREE_VEC_ELT (packed_args, i);
22782
22783 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22784 or the element of its argument pack at the current index if
22785 this argument was explicitly specified. */
22786 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22787 {
22788 int idx, level;
22789 tree arg, pargs;
22790 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22791
22792 arg = NULL_TREE;
22793 if (TREE_VALUE (pack)
22794 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22795 && (i - start < TREE_VEC_LENGTH (pargs)))
22796 {
22797 any_explicit = true;
22798 arg = TREE_VEC_ELT (pargs, i - start);
22799 }
22800 TMPL_ARG (targs, level, idx) = arg;
22801 }
22802
22803 /* If we had explicit template arguments, substitute them into the
22804 pattern before deduction. */
22805 if (any_explicit)
22806 {
22807 /* Some arguments might still be unspecified or dependent. */
22808 bool dependent;
22809 ++processing_template_decl;
22810 dependent = any_dependent_template_arguments_p (targs);
22811 if (!dependent)
22812 --processing_template_decl;
22813 parm = tsubst (pattern, targs,
22814 explain_p ? tf_warning_or_error : tf_none,
22815 NULL_TREE);
22816 if (dependent)
22817 --processing_template_decl;
22818 if (parm == error_mark_node)
22819 return 1;
22820 }
22821 else
22822 parm = pattern;
22823
22824 /* Unify the pattern with the current argument. */
22825 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22826 explain_p))
22827 return 1;
22828
22829 /* For each parameter pack, collect the deduced value. */
22830 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22831 {
22832 int idx, level;
22833 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22834
22835 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22836 TMPL_ARG (targs, level, idx);
22837 }
22838 }
22839
22840 /* Verify that the results of unification with the parameter packs
22841 produce results consistent with what we've seen before, and make
22842 the deduced argument packs available. */
22843 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22844 {
22845 tree old_pack = TREE_VALUE (pack);
22846 tree new_args = TREE_TYPE (pack);
22847 int i, len = TREE_VEC_LENGTH (new_args);
22848 int idx, level;
22849 bool nondeduced_p = false;
22850
22851 /* By default keep the original deduced argument pack.
22852 If necessary, more specific code is going to update the
22853 resulting deduced argument later down in this function. */
22854 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22855 TMPL_ARG (targs, level, idx) = old_pack;
22856
22857 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22858 actually deduce anything. */
22859 for (i = 0; i < len && !nondeduced_p; ++i)
22860 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22861 nondeduced_p = true;
22862 if (nondeduced_p)
22863 continue;
22864
22865 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22866 {
22867 /* If we had fewer function args than explicit template args,
22868 just use the explicits. */
22869 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22870 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22871 if (len < explicit_len)
22872 new_args = explicit_args;
22873 }
22874
22875 if (!old_pack)
22876 {
22877 tree result;
22878 /* Build the deduced *_ARGUMENT_PACK. */
22879 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22880 {
22881 result = make_node (NONTYPE_ARGUMENT_PACK);
22882 TREE_CONSTANT (result) = 1;
22883 }
22884 else
22885 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22886
22887 SET_ARGUMENT_PACK_ARGS (result, new_args);
22888
22889 /* Note the deduced argument packs for this parameter
22890 pack. */
22891 TMPL_ARG (targs, level, idx) = result;
22892 }
22893 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22894 && (ARGUMENT_PACK_ARGS (old_pack)
22895 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22896 {
22897 /* We only had the explicitly-provided arguments before, but
22898 now we have a complete set of arguments. */
22899 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22900
22901 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22902 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22903 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22904 }
22905 else
22906 {
22907 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22908 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22909 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22910 /* During template argument deduction for the aggregate deduction
22911 candidate, the number of elements in a trailing parameter pack
22912 is only deduced from the number of remaining function
22913 arguments if it is not otherwise deduced. */
22914 if (cxx_dialect >= cxx20
22915 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
22916 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
22917 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
22918 if (!comp_template_args (old_args, new_args,
22919 &bad_old_arg, &bad_new_arg))
22920 /* Inconsistent unification of this parameter pack. */
22921 return unify_parameter_pack_inconsistent (explain_p,
22922 bad_old_arg,
22923 bad_new_arg);
22924 }
22925 }
22926
22927 return unify_success (explain_p);
22928 }
22929
22930 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22931 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22932 parameters and return value are as for unify. */
22933
22934 static int
22935 unify_array_domain (tree tparms, tree targs,
22936 tree parm_dom, tree arg_dom,
22937 bool explain_p)
22938 {
22939 tree parm_max;
22940 tree arg_max;
22941 bool parm_cst;
22942 bool arg_cst;
22943
22944 /* Our representation of array types uses "N - 1" as the
22945 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22946 not an integer constant. We cannot unify arbitrarily
22947 complex expressions, so we eliminate the MINUS_EXPRs
22948 here. */
22949 parm_max = TYPE_MAX_VALUE (parm_dom);
22950 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22951 if (!parm_cst)
22952 {
22953 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22954 parm_max = TREE_OPERAND (parm_max, 0);
22955 }
22956 arg_max = TYPE_MAX_VALUE (arg_dom);
22957 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22958 if (!arg_cst)
22959 {
22960 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22961 trying to unify the type of a variable with the type
22962 of a template parameter. For example:
22963
22964 template <unsigned int N>
22965 void f (char (&) [N]);
22966 int g();
22967 void h(int i) {
22968 char a[g(i)];
22969 f(a);
22970 }
22971
22972 Here, the type of the ARG will be "int [g(i)]", and
22973 may be a SAVE_EXPR, etc. */
22974 if (TREE_CODE (arg_max) != MINUS_EXPR)
22975 return unify_vla_arg (explain_p, arg_dom);
22976 arg_max = TREE_OPERAND (arg_max, 0);
22977 }
22978
22979 /* If only one of the bounds used a MINUS_EXPR, compensate
22980 by adding one to the other bound. */
22981 if (parm_cst && !arg_cst)
22982 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22983 integer_type_node,
22984 parm_max,
22985 integer_one_node);
22986 else if (arg_cst && !parm_cst)
22987 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22988 integer_type_node,
22989 arg_max,
22990 integer_one_node);
22991
22992 return unify (tparms, targs, parm_max, arg_max,
22993 UNIFY_ALLOW_INTEGER, explain_p);
22994 }
22995
22996 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22997
22998 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22999
23000 static pa_kind_t
23001 pa_kind (tree t)
23002 {
23003 if (PACK_EXPANSION_P (t))
23004 t = PACK_EXPANSION_PATTERN (t);
23005 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23006 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23007 || DECL_TYPE_TEMPLATE_P (t))
23008 return pa_tmpl;
23009 else if (TYPE_P (t))
23010 return pa_type;
23011 else
23012 return pa_expr;
23013 }
23014
23015 /* Deduce the value of template parameters. TPARMS is the (innermost)
23016 set of template parameters to a template. TARGS is the bindings
23017 for those template parameters, as determined thus far; TARGS may
23018 include template arguments for outer levels of template parameters
23019 as well. PARM is a parameter to a template function, or a
23020 subcomponent of that parameter; ARG is the corresponding argument.
23021 This function attempts to match PARM with ARG in a manner
23022 consistent with the existing assignments in TARGS. If more values
23023 are deduced, then TARGS is updated.
23024
23025 Returns 0 if the type deduction succeeds, 1 otherwise. The
23026 parameter STRICT is a bitwise or of the following flags:
23027
23028 UNIFY_ALLOW_NONE:
23029 Require an exact match between PARM and ARG.
23030 UNIFY_ALLOW_MORE_CV_QUAL:
23031 Allow the deduced ARG to be more cv-qualified (by qualification
23032 conversion) than ARG.
23033 UNIFY_ALLOW_LESS_CV_QUAL:
23034 Allow the deduced ARG to be less cv-qualified than ARG.
23035 UNIFY_ALLOW_DERIVED:
23036 Allow the deduced ARG to be a template base class of ARG,
23037 or a pointer to a template base class of the type pointed to by
23038 ARG.
23039 UNIFY_ALLOW_INTEGER:
23040 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23041 case for more information.
23042 UNIFY_ALLOW_OUTER_LEVEL:
23043 This is the outermost level of a deduction. Used to determine validity
23044 of qualification conversions. A valid qualification conversion must
23045 have const qualified pointers leading up to the inner type which
23046 requires additional CV quals, except at the outer level, where const
23047 is not required [conv.qual]. It would be normal to set this flag in
23048 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23049 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23050 This is the outermost level of a deduction, and PARM can be more CV
23051 qualified at this point.
23052 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23053 This is the outermost level of a deduction, and PARM can be less CV
23054 qualified at this point. */
23055
23056 static int
23057 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23058 bool explain_p)
23059 {
23060 int idx;
23061 tree targ;
23062 tree tparm;
23063 int strict_in = strict;
23064 tsubst_flags_t complain = (explain_p
23065 ? tf_warning_or_error
23066 : tf_none);
23067
23068 /* I don't think this will do the right thing with respect to types.
23069 But the only case I've seen it in so far has been array bounds, where
23070 signedness is the only information lost, and I think that will be
23071 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23072 finish_id_expression_1, and are also OK. */
23073 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23074 parm = TREE_OPERAND (parm, 0);
23075
23076 if (arg == error_mark_node)
23077 return unify_invalid (explain_p);
23078 if (arg == unknown_type_node
23079 || arg == init_list_type_node)
23080 /* We can't deduce anything from this, but we might get all the
23081 template args from other function args. */
23082 return unify_success (explain_p);
23083
23084 if (parm == any_targ_node || arg == any_targ_node)
23085 return unify_success (explain_p);
23086
23087 /* If PARM uses template parameters, then we can't bail out here,
23088 even if ARG == PARM, since we won't record unifications for the
23089 template parameters. We might need them if we're trying to
23090 figure out which of two things is more specialized. */
23091 if (arg == parm && !uses_template_parms (parm))
23092 return unify_success (explain_p);
23093
23094 /* Handle init lists early, so the rest of the function can assume
23095 we're dealing with a type. */
23096 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23097 {
23098 tree elt, elttype;
23099 unsigned i;
23100 tree orig_parm = parm;
23101
23102 if (!is_std_init_list (parm)
23103 && TREE_CODE (parm) != ARRAY_TYPE)
23104 /* We can only deduce from an initializer list argument if the
23105 parameter is std::initializer_list or an array; otherwise this
23106 is a non-deduced context. */
23107 return unify_success (explain_p);
23108
23109 if (TREE_CODE (parm) == ARRAY_TYPE)
23110 elttype = TREE_TYPE (parm);
23111 else
23112 {
23113 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23114 /* Deduction is defined in terms of a single type, so just punt
23115 on the (bizarre) std::initializer_list<T...>. */
23116 if (PACK_EXPANSION_P (elttype))
23117 return unify_success (explain_p);
23118 }
23119
23120 if (strict != DEDUCE_EXACT
23121 && TYPE_P (elttype)
23122 && !uses_deducible_template_parms (elttype))
23123 /* If ELTTYPE has no deducible template parms, skip deduction from
23124 the list elements. */;
23125 else
23126 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23127 {
23128 int elt_strict = strict;
23129
23130 if (elt == error_mark_node)
23131 return unify_invalid (explain_p);
23132
23133 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23134 {
23135 tree type = TREE_TYPE (elt);
23136 if (type == error_mark_node)
23137 return unify_invalid (explain_p);
23138 /* It should only be possible to get here for a call. */
23139 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23140 elt_strict |= maybe_adjust_types_for_deduction
23141 (DEDUCE_CALL, &elttype, &type, elt);
23142 elt = type;
23143 }
23144
23145 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23146 explain_p);
23147 }
23148
23149 if (TREE_CODE (parm) == ARRAY_TYPE
23150 && deducible_array_bound (TYPE_DOMAIN (parm)))
23151 {
23152 /* Also deduce from the length of the initializer list. */
23153 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23154 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23155 if (idx == error_mark_node)
23156 return unify_invalid (explain_p);
23157 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23158 idx, explain_p);
23159 }
23160
23161 /* If the std::initializer_list<T> deduction worked, replace the
23162 deduced A with std::initializer_list<A>. */
23163 if (orig_parm != parm)
23164 {
23165 idx = TEMPLATE_TYPE_IDX (orig_parm);
23166 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23167 targ = listify (targ);
23168 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23169 }
23170 return unify_success (explain_p);
23171 }
23172
23173 /* If parm and arg aren't the same kind of thing (template, type, or
23174 expression), fail early. */
23175 if (pa_kind (parm) != pa_kind (arg))
23176 return unify_invalid (explain_p);
23177
23178 /* Immediately reject some pairs that won't unify because of
23179 cv-qualification mismatches. */
23180 if (TREE_CODE (arg) == TREE_CODE (parm)
23181 && TYPE_P (arg)
23182 /* It is the elements of the array which hold the cv quals of an array
23183 type, and the elements might be template type parms. We'll check
23184 when we recurse. */
23185 && TREE_CODE (arg) != ARRAY_TYPE
23186 /* We check the cv-qualifiers when unifying with template type
23187 parameters below. We want to allow ARG `const T' to unify with
23188 PARM `T' for example, when computing which of two templates
23189 is more specialized, for example. */
23190 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23191 && !check_cv_quals_for_unify (strict_in, arg, parm))
23192 return unify_cv_qual_mismatch (explain_p, parm, arg);
23193
23194 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23195 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23196 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23197 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23198 strict &= ~UNIFY_ALLOW_DERIVED;
23199 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23200 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23201
23202 switch (TREE_CODE (parm))
23203 {
23204 case TYPENAME_TYPE:
23205 case SCOPE_REF:
23206 case UNBOUND_CLASS_TEMPLATE:
23207 /* In a type which contains a nested-name-specifier, template
23208 argument values cannot be deduced for template parameters used
23209 within the nested-name-specifier. */
23210 return unify_success (explain_p);
23211
23212 case TEMPLATE_TYPE_PARM:
23213 case TEMPLATE_TEMPLATE_PARM:
23214 case BOUND_TEMPLATE_TEMPLATE_PARM:
23215 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23216 if (error_operand_p (tparm))
23217 return unify_invalid (explain_p);
23218
23219 if (TEMPLATE_TYPE_LEVEL (parm)
23220 != template_decl_level (tparm))
23221 /* The PARM is not one we're trying to unify. Just check
23222 to see if it matches ARG. */
23223 {
23224 if (TREE_CODE (arg) == TREE_CODE (parm)
23225 && (is_auto (parm) ? is_auto (arg)
23226 : same_type_p (parm, arg)))
23227 return unify_success (explain_p);
23228 else
23229 return unify_type_mismatch (explain_p, parm, arg);
23230 }
23231 idx = TEMPLATE_TYPE_IDX (parm);
23232 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23233 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23234 if (error_operand_p (tparm))
23235 return unify_invalid (explain_p);
23236
23237 /* Check for mixed types and values. */
23238 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23239 && TREE_CODE (tparm) != TYPE_DECL)
23240 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23241 && TREE_CODE (tparm) != TEMPLATE_DECL))
23242 gcc_unreachable ();
23243
23244 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23245 {
23246 if ((strict_in & UNIFY_ALLOW_DERIVED)
23247 && CLASS_TYPE_P (arg))
23248 {
23249 /* First try to match ARG directly. */
23250 tree t = try_class_unification (tparms, targs, parm, arg,
23251 explain_p);
23252 if (!t)
23253 {
23254 /* Otherwise, look for a suitable base of ARG, as below. */
23255 enum template_base_result r;
23256 r = get_template_base (tparms, targs, parm, arg,
23257 explain_p, &t);
23258 if (!t)
23259 return unify_no_common_base (explain_p, r, parm, arg);
23260 arg = t;
23261 }
23262 }
23263 /* ARG must be constructed from a template class or a template
23264 template parameter. */
23265 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23266 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23267 return unify_template_deduction_failure (explain_p, parm, arg);
23268
23269 /* Deduce arguments T, i from TT<T> or TT<i>. */
23270 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23271 return 1;
23272
23273 arg = TYPE_TI_TEMPLATE (arg);
23274
23275 /* Fall through to deduce template name. */
23276 }
23277
23278 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23279 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23280 {
23281 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23282
23283 /* Simple cases: Value already set, does match or doesn't. */
23284 if (targ != NULL_TREE && template_args_equal (targ, arg))
23285 return unify_success (explain_p);
23286 else if (targ)
23287 return unify_inconsistency (explain_p, parm, targ, arg);
23288 }
23289 else
23290 {
23291 /* If PARM is `const T' and ARG is only `int', we don't have
23292 a match unless we are allowing additional qualification.
23293 If ARG is `const int' and PARM is just `T' that's OK;
23294 that binds `const int' to `T'. */
23295 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23296 arg, parm))
23297 return unify_cv_qual_mismatch (explain_p, parm, arg);
23298
23299 /* Consider the case where ARG is `const volatile int' and
23300 PARM is `const T'. Then, T should be `volatile int'. */
23301 arg = cp_build_qualified_type_real
23302 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23303 if (arg == error_mark_node)
23304 return unify_invalid (explain_p);
23305
23306 /* Simple cases: Value already set, does match or doesn't. */
23307 if (targ != NULL_TREE && same_type_p (targ, arg))
23308 return unify_success (explain_p);
23309 else if (targ)
23310 return unify_inconsistency (explain_p, parm, targ, arg);
23311
23312 /* Make sure that ARG is not a variable-sized array. (Note
23313 that were talking about variable-sized arrays (like
23314 `int[n]'), rather than arrays of unknown size (like
23315 `int[]').) We'll get very confused by such a type since
23316 the bound of the array is not constant, and therefore
23317 not mangleable. Besides, such types are not allowed in
23318 ISO C++, so we can do as we please here. We do allow
23319 them for 'auto' deduction, since that isn't ABI-exposed. */
23320 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23321 return unify_vla_arg (explain_p, arg);
23322
23323 /* Strip typedefs as in convert_template_argument. */
23324 arg = canonicalize_type_argument (arg, tf_none);
23325 }
23326
23327 /* If ARG is a parameter pack or an expansion, we cannot unify
23328 against it unless PARM is also a parameter pack. */
23329 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23330 && !template_parameter_pack_p (parm))
23331 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23332
23333 /* If the argument deduction results is a METHOD_TYPE,
23334 then there is a problem.
23335 METHOD_TYPE doesn't map to any real C++ type the result of
23336 the deduction cannot be of that type. */
23337 if (TREE_CODE (arg) == METHOD_TYPE)
23338 return unify_method_type_error (explain_p, arg);
23339
23340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23341 return unify_success (explain_p);
23342
23343 case TEMPLATE_PARM_INDEX:
23344 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23345 if (error_operand_p (tparm))
23346 return unify_invalid (explain_p);
23347
23348 if (TEMPLATE_PARM_LEVEL (parm)
23349 != template_decl_level (tparm))
23350 {
23351 /* The PARM is not one we're trying to unify. Just check
23352 to see if it matches ARG. */
23353 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23354 && cp_tree_equal (parm, arg));
23355 if (result)
23356 unify_expression_unequal (explain_p, parm, arg);
23357 return result;
23358 }
23359
23360 idx = TEMPLATE_PARM_IDX (parm);
23361 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23362
23363 if (targ)
23364 {
23365 if ((strict & UNIFY_ALLOW_INTEGER)
23366 && TREE_TYPE (targ) && TREE_TYPE (arg)
23367 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23368 /* We're deducing from an array bound, the type doesn't matter. */
23369 arg = fold_convert (TREE_TYPE (targ), arg);
23370 int x = !cp_tree_equal (targ, arg);
23371 if (x)
23372 unify_inconsistency (explain_p, parm, targ, arg);
23373 return x;
23374 }
23375
23376 /* [temp.deduct.type] If, in the declaration of a function template
23377 with a non-type template-parameter, the non-type
23378 template-parameter is used in an expression in the function
23379 parameter-list and, if the corresponding template-argument is
23380 deduced, the template-argument type shall match the type of the
23381 template-parameter exactly, except that a template-argument
23382 deduced from an array bound may be of any integral type.
23383 The non-type parameter might use already deduced type parameters. */
23384 tparm = TREE_TYPE (parm);
23385 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23386 /* We don't have enough levels of args to do any substitution. This
23387 can happen in the context of -fnew-ttp-matching. */;
23388 else
23389 {
23390 ++processing_template_decl;
23391 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23392 --processing_template_decl;
23393
23394 if (tree a = type_uses_auto (tparm))
23395 {
23396 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23397 if (tparm == error_mark_node)
23398 return 1;
23399 }
23400 }
23401
23402 if (!TREE_TYPE (arg))
23403 /* Template-parameter dependent expression. Just accept it for now.
23404 It will later be processed in convert_template_argument. */
23405 ;
23406 else if (same_type_ignoring_top_level_qualifiers_p
23407 (non_reference (TREE_TYPE (arg)),
23408 non_reference (tparm)))
23409 /* OK. Ignore top-level quals here because a class-type template
23410 parameter object is const. */;
23411 else if ((strict & UNIFY_ALLOW_INTEGER)
23412 && CP_INTEGRAL_TYPE_P (tparm))
23413 /* Convert the ARG to the type of PARM; the deduced non-type
23414 template argument must exactly match the types of the
23415 corresponding parameter. */
23416 arg = fold (build_nop (tparm, arg));
23417 else if (uses_template_parms (tparm))
23418 {
23419 /* We haven't deduced the type of this parameter yet. */
23420 if (cxx_dialect >= cxx17
23421 /* We deduce from array bounds in try_array_deduction. */
23422 && !(strict & UNIFY_ALLOW_INTEGER))
23423 {
23424 /* Deduce it from the non-type argument. */
23425 tree atype = TREE_TYPE (arg);
23426 RECUR_AND_CHECK_FAILURE (tparms, targs,
23427 tparm, atype,
23428 UNIFY_ALLOW_NONE, explain_p);
23429 }
23430 else
23431 /* Try again later. */
23432 return unify_success (explain_p);
23433 }
23434 else
23435 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23436
23437 /* If ARG is a parameter pack or an expansion, we cannot unify
23438 against it unless PARM is also a parameter pack. */
23439 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23440 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23441 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23442
23443 {
23444 bool removed_attr = false;
23445 arg = strip_typedefs_expr (arg, &removed_attr);
23446 }
23447 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23448 return unify_success (explain_p);
23449
23450 case PTRMEM_CST:
23451 {
23452 /* A pointer-to-member constant can be unified only with
23453 another constant. */
23454 if (TREE_CODE (arg) != PTRMEM_CST)
23455 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23456
23457 /* Just unify the class member. It would be useless (and possibly
23458 wrong, depending on the strict flags) to unify also
23459 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23460 arg refer to the same variable, even if through different
23461 classes. For instance:
23462
23463 struct A { int x; };
23464 struct B : A { };
23465
23466 Unification of &A::x and &B::x must succeed. */
23467 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23468 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23469 }
23470
23471 case POINTER_TYPE:
23472 {
23473 if (!TYPE_PTR_P (arg))
23474 return unify_type_mismatch (explain_p, parm, arg);
23475
23476 /* [temp.deduct.call]
23477
23478 A can be another pointer or pointer to member type that can
23479 be converted to the deduced A via a qualification
23480 conversion (_conv.qual_).
23481
23482 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23483 This will allow for additional cv-qualification of the
23484 pointed-to types if appropriate. */
23485
23486 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23487 /* The derived-to-base conversion only persists through one
23488 level of pointers. */
23489 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23490
23491 return unify (tparms, targs, TREE_TYPE (parm),
23492 TREE_TYPE (arg), strict, explain_p);
23493 }
23494
23495 case REFERENCE_TYPE:
23496 if (!TYPE_REF_P (arg))
23497 return unify_type_mismatch (explain_p, parm, arg);
23498 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23499 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23500
23501 case ARRAY_TYPE:
23502 if (TREE_CODE (arg) != ARRAY_TYPE)
23503 return unify_type_mismatch (explain_p, parm, arg);
23504 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23505 != (TYPE_DOMAIN (arg) == NULL_TREE))
23506 return unify_type_mismatch (explain_p, parm, arg);
23507 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23508 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23509 if (TYPE_DOMAIN (parm) != NULL_TREE)
23510 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23511 TYPE_DOMAIN (arg), explain_p);
23512 return unify_success (explain_p);
23513
23514 case REAL_TYPE:
23515 case COMPLEX_TYPE:
23516 case VECTOR_TYPE:
23517 case INTEGER_TYPE:
23518 case BOOLEAN_TYPE:
23519 case ENUMERAL_TYPE:
23520 case VOID_TYPE:
23521 case NULLPTR_TYPE:
23522 if (TREE_CODE (arg) != TREE_CODE (parm))
23523 return unify_type_mismatch (explain_p, parm, arg);
23524
23525 /* We have already checked cv-qualification at the top of the
23526 function. */
23527 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23528 return unify_type_mismatch (explain_p, parm, arg);
23529
23530 /* As far as unification is concerned, this wins. Later checks
23531 will invalidate it if necessary. */
23532 return unify_success (explain_p);
23533
23534 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23535 /* Type INTEGER_CST can come from ordinary constant template args. */
23536 case INTEGER_CST:
23537 while (CONVERT_EXPR_P (arg))
23538 arg = TREE_OPERAND (arg, 0);
23539
23540 if (TREE_CODE (arg) != INTEGER_CST)
23541 return unify_template_argument_mismatch (explain_p, parm, arg);
23542 return (tree_int_cst_equal (parm, arg)
23543 ? unify_success (explain_p)
23544 : unify_template_argument_mismatch (explain_p, parm, arg));
23545
23546 case TREE_VEC:
23547 {
23548 int i, len, argslen;
23549 int parm_variadic_p = 0;
23550
23551 if (TREE_CODE (arg) != TREE_VEC)
23552 return unify_template_argument_mismatch (explain_p, parm, arg);
23553
23554 len = TREE_VEC_LENGTH (parm);
23555 argslen = TREE_VEC_LENGTH (arg);
23556
23557 /* Check for pack expansions in the parameters. */
23558 for (i = 0; i < len; ++i)
23559 {
23560 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23561 {
23562 if (i == len - 1)
23563 /* We can unify against something with a trailing
23564 parameter pack. */
23565 parm_variadic_p = 1;
23566 else
23567 /* [temp.deduct.type]/9: If the template argument list of
23568 P contains a pack expansion that is not the last
23569 template argument, the entire template argument list
23570 is a non-deduced context. */
23571 return unify_success (explain_p);
23572 }
23573 }
23574
23575 /* If we don't have enough arguments to satisfy the parameters
23576 (not counting the pack expression at the end), or we have
23577 too many arguments for a parameter list that doesn't end in
23578 a pack expression, we can't unify. */
23579 if (parm_variadic_p
23580 ? argslen < len - parm_variadic_p
23581 : argslen != len)
23582 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23583
23584 /* Unify all of the parameters that precede the (optional)
23585 pack expression. */
23586 for (i = 0; i < len - parm_variadic_p; ++i)
23587 {
23588 RECUR_AND_CHECK_FAILURE (tparms, targs,
23589 TREE_VEC_ELT (parm, i),
23590 TREE_VEC_ELT (arg, i),
23591 UNIFY_ALLOW_NONE, explain_p);
23592 }
23593 if (parm_variadic_p)
23594 return unify_pack_expansion (tparms, targs, parm, arg,
23595 DEDUCE_EXACT,
23596 /*subr=*/true, explain_p);
23597 return unify_success (explain_p);
23598 }
23599
23600 case RECORD_TYPE:
23601 case UNION_TYPE:
23602 if (TREE_CODE (arg) != TREE_CODE (parm))
23603 return unify_type_mismatch (explain_p, parm, arg);
23604
23605 if (TYPE_PTRMEMFUNC_P (parm))
23606 {
23607 if (!TYPE_PTRMEMFUNC_P (arg))
23608 return unify_type_mismatch (explain_p, parm, arg);
23609
23610 return unify (tparms, targs,
23611 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23612 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23613 strict, explain_p);
23614 }
23615 else if (TYPE_PTRMEMFUNC_P (arg))
23616 return unify_type_mismatch (explain_p, parm, arg);
23617
23618 if (CLASSTYPE_TEMPLATE_INFO (parm))
23619 {
23620 tree t = NULL_TREE;
23621
23622 if (strict_in & UNIFY_ALLOW_DERIVED)
23623 {
23624 /* First, we try to unify the PARM and ARG directly. */
23625 t = try_class_unification (tparms, targs,
23626 parm, arg, explain_p);
23627
23628 if (!t)
23629 {
23630 /* Fallback to the special case allowed in
23631 [temp.deduct.call]:
23632
23633 If P is a class, and P has the form
23634 template-id, then A can be a derived class of
23635 the deduced A. Likewise, if P is a pointer to
23636 a class of the form template-id, A can be a
23637 pointer to a derived class pointed to by the
23638 deduced A. */
23639 enum template_base_result r;
23640 r = get_template_base (tparms, targs, parm, arg,
23641 explain_p, &t);
23642
23643 if (!t)
23644 {
23645 /* Don't give the derived diagnostic if we're
23646 already dealing with the same template. */
23647 bool same_template
23648 = (CLASSTYPE_TEMPLATE_INFO (arg)
23649 && (CLASSTYPE_TI_TEMPLATE (parm)
23650 == CLASSTYPE_TI_TEMPLATE (arg)));
23651 return unify_no_common_base (explain_p && !same_template,
23652 r, parm, arg);
23653 }
23654 }
23655 }
23656 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23657 && (CLASSTYPE_TI_TEMPLATE (parm)
23658 == CLASSTYPE_TI_TEMPLATE (arg)))
23659 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23660 Then, we should unify `int' and `U'. */
23661 t = arg;
23662 else
23663 /* There's no chance of unification succeeding. */
23664 return unify_type_mismatch (explain_p, parm, arg);
23665
23666 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23667 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23668 }
23669 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23670 return unify_type_mismatch (explain_p, parm, arg);
23671 return unify_success (explain_p);
23672
23673 case METHOD_TYPE:
23674 case FUNCTION_TYPE:
23675 {
23676 unsigned int nargs;
23677 tree *args;
23678 tree a;
23679 unsigned int i;
23680
23681 if (TREE_CODE (arg) != TREE_CODE (parm))
23682 return unify_type_mismatch (explain_p, parm, arg);
23683
23684 /* CV qualifications for methods can never be deduced, they must
23685 match exactly. We need to check them explicitly here,
23686 because type_unification_real treats them as any other
23687 cv-qualified parameter. */
23688 if (TREE_CODE (parm) == METHOD_TYPE
23689 && (!check_cv_quals_for_unify
23690 (UNIFY_ALLOW_NONE,
23691 class_of_this_parm (arg),
23692 class_of_this_parm (parm))))
23693 return unify_cv_qual_mismatch (explain_p, parm, arg);
23694 if (TREE_CODE (arg) == FUNCTION_TYPE
23695 && type_memfn_quals (parm) != type_memfn_quals (arg))
23696 return unify_cv_qual_mismatch (explain_p, parm, arg);
23697 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23698 return unify_type_mismatch (explain_p, parm, arg);
23699
23700 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23701 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23702
23703 nargs = list_length (TYPE_ARG_TYPES (arg));
23704 args = XALLOCAVEC (tree, nargs);
23705 for (a = TYPE_ARG_TYPES (arg), i = 0;
23706 a != NULL_TREE && a != void_list_node;
23707 a = TREE_CHAIN (a), ++i)
23708 args[i] = TREE_VALUE (a);
23709 nargs = i;
23710
23711 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23712 args, nargs, 1, DEDUCE_EXACT,
23713 NULL, explain_p))
23714 return 1;
23715
23716 if (flag_noexcept_type)
23717 {
23718 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23719 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23720 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23721 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23722 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23723 && uses_template_parms (TREE_PURPOSE (pspec)))
23724 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23725 TREE_PURPOSE (aspec),
23726 UNIFY_ALLOW_NONE, explain_p);
23727 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23728 return unify_type_mismatch (explain_p, parm, arg);
23729 }
23730
23731 return 0;
23732 }
23733
23734 case OFFSET_TYPE:
23735 /* Unify a pointer to member with a pointer to member function, which
23736 deduces the type of the member as a function type. */
23737 if (TYPE_PTRMEMFUNC_P (arg))
23738 {
23739 /* Check top-level cv qualifiers */
23740 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23741 return unify_cv_qual_mismatch (explain_p, parm, arg);
23742
23743 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23744 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23745 UNIFY_ALLOW_NONE, explain_p);
23746
23747 /* Determine the type of the function we are unifying against. */
23748 tree fntype = static_fn_type (arg);
23749
23750 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23751 }
23752
23753 if (TREE_CODE (arg) != OFFSET_TYPE)
23754 return unify_type_mismatch (explain_p, parm, arg);
23755 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23756 TYPE_OFFSET_BASETYPE (arg),
23757 UNIFY_ALLOW_NONE, explain_p);
23758 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23759 strict, explain_p);
23760
23761 case CONST_DECL:
23762 if (DECL_TEMPLATE_PARM_P (parm))
23763 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23764 if (arg != scalar_constant_value (parm))
23765 return unify_template_argument_mismatch (explain_p, parm, arg);
23766 return unify_success (explain_p);
23767
23768 case FIELD_DECL:
23769 case TEMPLATE_DECL:
23770 /* Matched cases are handled by the ARG == PARM test above. */
23771 return unify_template_argument_mismatch (explain_p, parm, arg);
23772
23773 case VAR_DECL:
23774 /* We might get a variable as a non-type template argument in parm if the
23775 corresponding parameter is type-dependent. Make any necessary
23776 adjustments based on whether arg is a reference. */
23777 if (CONSTANT_CLASS_P (arg))
23778 parm = fold_non_dependent_expr (parm, complain);
23779 else if (REFERENCE_REF_P (arg))
23780 {
23781 tree sub = TREE_OPERAND (arg, 0);
23782 STRIP_NOPS (sub);
23783 if (TREE_CODE (sub) == ADDR_EXPR)
23784 arg = TREE_OPERAND (sub, 0);
23785 }
23786 /* Now use the normal expression code to check whether they match. */
23787 goto expr;
23788
23789 case TYPE_ARGUMENT_PACK:
23790 case NONTYPE_ARGUMENT_PACK:
23791 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23792 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23793
23794 case TYPEOF_TYPE:
23795 case DECLTYPE_TYPE:
23796 case UNDERLYING_TYPE:
23797 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23798 or UNDERLYING_TYPE nodes. */
23799 return unify_success (explain_p);
23800
23801 case ERROR_MARK:
23802 /* Unification fails if we hit an error node. */
23803 return unify_invalid (explain_p);
23804
23805 case INDIRECT_REF:
23806 if (REFERENCE_REF_P (parm))
23807 {
23808 bool pexp = PACK_EXPANSION_P (arg);
23809 if (pexp)
23810 arg = PACK_EXPANSION_PATTERN (arg);
23811 if (REFERENCE_REF_P (arg))
23812 arg = TREE_OPERAND (arg, 0);
23813 if (pexp)
23814 arg = make_pack_expansion (arg, complain);
23815 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23816 strict, explain_p);
23817 }
23818 /* FALLTHRU */
23819
23820 default:
23821 /* An unresolved overload is a nondeduced context. */
23822 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23823 return unify_success (explain_p);
23824 gcc_assert (EXPR_P (parm)
23825 || COMPOUND_LITERAL_P (parm)
23826 || TREE_CODE (parm) == TRAIT_EXPR);
23827 expr:
23828 /* We must be looking at an expression. This can happen with
23829 something like:
23830
23831 template <int I>
23832 void foo(S<I>, S<I + 2>);
23833
23834 or
23835
23836 template<typename T>
23837 void foo(A<T, T{}>);
23838
23839 This is a "non-deduced context":
23840
23841 [deduct.type]
23842
23843 The non-deduced contexts are:
23844
23845 --A non-type template argument or an array bound in which
23846 a subexpression references a template parameter.
23847
23848 In these cases, we assume deduction succeeded, but don't
23849 actually infer any unifications. */
23850
23851 if (!uses_template_parms (parm)
23852 && !template_args_equal (parm, arg))
23853 return unify_expression_unequal (explain_p, parm, arg);
23854 else
23855 return unify_success (explain_p);
23856 }
23857 }
23858 #undef RECUR_AND_CHECK_FAILURE
23859 \f
23860 /* Note that DECL can be defined in this translation unit, if
23861 required. */
23862
23863 static void
23864 mark_definable (tree decl)
23865 {
23866 tree clone;
23867 DECL_NOT_REALLY_EXTERN (decl) = 1;
23868 FOR_EACH_CLONE (clone, decl)
23869 DECL_NOT_REALLY_EXTERN (clone) = 1;
23870 }
23871
23872 /* Called if RESULT is explicitly instantiated, or is a member of an
23873 explicitly instantiated class. */
23874
23875 void
23876 mark_decl_instantiated (tree result, int extern_p)
23877 {
23878 SET_DECL_EXPLICIT_INSTANTIATION (result);
23879
23880 /* If this entity has already been written out, it's too late to
23881 make any modifications. */
23882 if (TREE_ASM_WRITTEN (result))
23883 return;
23884
23885 /* For anonymous namespace we don't need to do anything. */
23886 if (decl_anon_ns_mem_p (result))
23887 {
23888 gcc_assert (!TREE_PUBLIC (result));
23889 return;
23890 }
23891
23892 if (TREE_CODE (result) != FUNCTION_DECL)
23893 /* The TREE_PUBLIC flag for function declarations will have been
23894 set correctly by tsubst. */
23895 TREE_PUBLIC (result) = 1;
23896
23897 /* This might have been set by an earlier implicit instantiation. */
23898 DECL_COMDAT (result) = 0;
23899
23900 if (extern_p)
23901 DECL_NOT_REALLY_EXTERN (result) = 0;
23902 else
23903 {
23904 mark_definable (result);
23905 mark_needed (result);
23906 /* Always make artificials weak. */
23907 if (DECL_ARTIFICIAL (result) && flag_weak)
23908 comdat_linkage (result);
23909 /* For WIN32 we also want to put explicit instantiations in
23910 linkonce sections. */
23911 else if (TREE_PUBLIC (result))
23912 maybe_make_one_only (result);
23913 if (TREE_CODE (result) == FUNCTION_DECL
23914 && DECL_TEMPLATE_INSTANTIATED (result))
23915 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23916 since start_preparsed_function wouldn't have if we had an earlier
23917 extern explicit instantiation. */
23918 DECL_EXTERNAL (result) = 0;
23919 }
23920
23921 /* If EXTERN_P, then this function will not be emitted -- unless
23922 followed by an explicit instantiation, at which point its linkage
23923 will be adjusted. If !EXTERN_P, then this function will be
23924 emitted here. In neither circumstance do we want
23925 import_export_decl to adjust the linkage. */
23926 DECL_INTERFACE_KNOWN (result) = 1;
23927 }
23928
23929 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23930 important template arguments. If any are missing, we check whether
23931 they're important by using error_mark_node for substituting into any
23932 args that were used for partial ordering (the ones between ARGS and END)
23933 and seeing if it bubbles up. */
23934
23935 static bool
23936 check_undeduced_parms (tree targs, tree args, tree end)
23937 {
23938 bool found = false;
23939 int i;
23940 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23941 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23942 {
23943 found = true;
23944 TREE_VEC_ELT (targs, i) = error_mark_node;
23945 }
23946 if (found)
23947 {
23948 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23949 if (substed == error_mark_node)
23950 return true;
23951 }
23952 return false;
23953 }
23954
23955 /* Given two function templates PAT1 and PAT2, return:
23956
23957 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23958 -1 if PAT2 is more specialized than PAT1.
23959 0 if neither is more specialized.
23960
23961 LEN indicates the number of parameters we should consider
23962 (defaulted parameters should not be considered).
23963
23964 The 1998 std underspecified function template partial ordering, and
23965 DR214 addresses the issue. We take pairs of arguments, one from
23966 each of the templates, and deduce them against each other. One of
23967 the templates will be more specialized if all the *other*
23968 template's arguments deduce against its arguments and at least one
23969 of its arguments *does* *not* deduce against the other template's
23970 corresponding argument. Deduction is done as for class templates.
23971 The arguments used in deduction have reference and top level cv
23972 qualifiers removed. Iff both arguments were originally reference
23973 types *and* deduction succeeds in both directions, an lvalue reference
23974 wins against an rvalue reference and otherwise the template
23975 with the more cv-qualified argument wins for that pairing (if
23976 neither is more cv-qualified, they both are equal). Unlike regular
23977 deduction, after all the arguments have been deduced in this way,
23978 we do *not* verify the deduced template argument values can be
23979 substituted into non-deduced contexts.
23980
23981 The logic can be a bit confusing here, because we look at deduce1 and
23982 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23983 can find template arguments for pat1 to make arg1 look like arg2, that
23984 means that arg2 is at least as specialized as arg1. */
23985
23986 int
23987 more_specialized_fn (tree pat1, tree pat2, int len)
23988 {
23989 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23990 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23991 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23992 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23993 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23994 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23995 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23996 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23997 tree origs1, origs2;
23998 bool lose1 = false;
23999 bool lose2 = false;
24000
24001 /* Remove the this parameter from non-static member functions. If
24002 one is a non-static member function and the other is not a static
24003 member function, remove the first parameter from that function
24004 also. This situation occurs for operator functions where we
24005 locate both a member function (with this pointer) and non-member
24006 operator (with explicit first operand). */
24007 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24008 {
24009 len--; /* LEN is the number of significant arguments for DECL1 */
24010 args1 = TREE_CHAIN (args1);
24011 if (!DECL_STATIC_FUNCTION_P (decl2))
24012 args2 = TREE_CHAIN (args2);
24013 }
24014 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24015 {
24016 args2 = TREE_CHAIN (args2);
24017 if (!DECL_STATIC_FUNCTION_P (decl1))
24018 {
24019 len--;
24020 args1 = TREE_CHAIN (args1);
24021 }
24022 }
24023
24024 /* If only one is a conversion operator, they are unordered. */
24025 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24026 return 0;
24027
24028 /* Consider the return type for a conversion function */
24029 if (DECL_CONV_FN_P (decl1))
24030 {
24031 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24032 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24033 len++;
24034 }
24035
24036 processing_template_decl++;
24037
24038 origs1 = args1;
24039 origs2 = args2;
24040
24041 while (len--
24042 /* Stop when an ellipsis is seen. */
24043 && args1 != NULL_TREE && args2 != NULL_TREE)
24044 {
24045 tree arg1 = TREE_VALUE (args1);
24046 tree arg2 = TREE_VALUE (args2);
24047 int deduce1, deduce2;
24048 int quals1 = -1;
24049 int quals2 = -1;
24050 int ref1 = 0;
24051 int ref2 = 0;
24052
24053 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24054 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24055 {
24056 /* When both arguments are pack expansions, we need only
24057 unify the patterns themselves. */
24058 arg1 = PACK_EXPANSION_PATTERN (arg1);
24059 arg2 = PACK_EXPANSION_PATTERN (arg2);
24060
24061 /* This is the last comparison we need to do. */
24062 len = 0;
24063 }
24064
24065 if (TYPE_REF_P (arg1))
24066 {
24067 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24068 arg1 = TREE_TYPE (arg1);
24069 quals1 = cp_type_quals (arg1);
24070 }
24071
24072 if (TYPE_REF_P (arg2))
24073 {
24074 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24075 arg2 = TREE_TYPE (arg2);
24076 quals2 = cp_type_quals (arg2);
24077 }
24078
24079 arg1 = TYPE_MAIN_VARIANT (arg1);
24080 arg2 = TYPE_MAIN_VARIANT (arg2);
24081
24082 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24083 {
24084 int i, len2 = remaining_arguments (args2);
24085 tree parmvec = make_tree_vec (1);
24086 tree argvec = make_tree_vec (len2);
24087 tree ta = args2;
24088
24089 /* Setup the parameter vector, which contains only ARG1. */
24090 TREE_VEC_ELT (parmvec, 0) = arg1;
24091
24092 /* Setup the argument vector, which contains the remaining
24093 arguments. */
24094 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24095 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24096
24097 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24098 argvec, DEDUCE_EXACT,
24099 /*subr=*/true, /*explain_p=*/false)
24100 == 0);
24101
24102 /* We cannot deduce in the other direction, because ARG1 is
24103 a pack expansion but ARG2 is not. */
24104 deduce2 = 0;
24105 }
24106 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24107 {
24108 int i, len1 = remaining_arguments (args1);
24109 tree parmvec = make_tree_vec (1);
24110 tree argvec = make_tree_vec (len1);
24111 tree ta = args1;
24112
24113 /* Setup the parameter vector, which contains only ARG1. */
24114 TREE_VEC_ELT (parmvec, 0) = arg2;
24115
24116 /* Setup the argument vector, which contains the remaining
24117 arguments. */
24118 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24119 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24120
24121 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24122 argvec, DEDUCE_EXACT,
24123 /*subr=*/true, /*explain_p=*/false)
24124 == 0);
24125
24126 /* We cannot deduce in the other direction, because ARG2 is
24127 a pack expansion but ARG1 is not.*/
24128 deduce1 = 0;
24129 }
24130
24131 else
24132 {
24133 /* The normal case, where neither argument is a pack
24134 expansion. */
24135 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24136 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24137 == 0);
24138 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24139 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24140 == 0);
24141 }
24142
24143 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24144 arg2, then arg2 is not as specialized as arg1. */
24145 if (!deduce1)
24146 lose2 = true;
24147 if (!deduce2)
24148 lose1 = true;
24149
24150 /* "If, for a given type, deduction succeeds in both directions
24151 (i.e., the types are identical after the transformations above)
24152 and both P and A were reference types (before being replaced with
24153 the type referred to above):
24154 - if the type from the argument template was an lvalue reference and
24155 the type from the parameter template was not, the argument type is
24156 considered to be more specialized than the other; otherwise,
24157 - if the type from the argument template is more cv-qualified
24158 than the type from the parameter template (as described above),
24159 the argument type is considered to be more specialized than the other;
24160 otherwise,
24161 - neither type is more specialized than the other." */
24162
24163 if (deduce1 && deduce2)
24164 {
24165 if (ref1 && ref2 && ref1 != ref2)
24166 {
24167 if (ref1 > ref2)
24168 lose1 = true;
24169 else
24170 lose2 = true;
24171 }
24172 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24173 {
24174 if ((quals1 & quals2) == quals2)
24175 lose2 = true;
24176 if ((quals1 & quals2) == quals1)
24177 lose1 = true;
24178 }
24179 }
24180
24181 if (lose1 && lose2)
24182 /* We've failed to deduce something in either direction.
24183 These must be unordered. */
24184 break;
24185
24186 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24187 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24188 /* We have already processed all of the arguments in our
24189 handing of the pack expansion type. */
24190 len = 0;
24191
24192 args1 = TREE_CHAIN (args1);
24193 args2 = TREE_CHAIN (args2);
24194 }
24195
24196 /* "In most cases, all template parameters must have values in order for
24197 deduction to succeed, but for partial ordering purposes a template
24198 parameter may remain without a value provided it is not used in the
24199 types being used for partial ordering."
24200
24201 Thus, if we are missing any of the targs1 we need to substitute into
24202 origs1, then pat2 is not as specialized as pat1. This can happen when
24203 there is a nondeduced context. */
24204 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24205 lose2 = true;
24206 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24207 lose1 = true;
24208
24209 processing_template_decl--;
24210
24211 /* If both deductions succeed, the partial ordering selects the more
24212 constrained template. */
24213 /* P2113: If the corresponding template-parameters of the
24214 template-parameter-lists are not equivalent ([temp.over.link]) or if
24215 the function parameters that positionally correspond between the two
24216 templates are not of the same type, neither template is more
24217 specialized than the other. */
24218 if (!lose1 && !lose2
24219 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24220 DECL_TEMPLATE_PARMS (pat2))
24221 && compparms (origs1, origs2))
24222 {
24223 int winner = more_constrained (decl1, decl2);
24224 if (winner > 0)
24225 lose2 = true;
24226 else if (winner < 0)
24227 lose1 = true;
24228 }
24229
24230 /* All things being equal, if the next argument is a pack expansion
24231 for one function but not for the other, prefer the
24232 non-variadic function. FIXME this is bogus; see c++/41958. */
24233 if (lose1 == lose2
24234 && args1 && TREE_VALUE (args1)
24235 && args2 && TREE_VALUE (args2))
24236 {
24237 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24238 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24239 }
24240
24241 if (lose1 == lose2)
24242 return 0;
24243 else if (!lose1)
24244 return 1;
24245 else
24246 return -1;
24247 }
24248
24249 /* Determine which of two partial specializations of TMPL is more
24250 specialized.
24251
24252 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24253 to the first partial specialization. The TREE_PURPOSE is the
24254 innermost set of template parameters for the partial
24255 specialization. PAT2 is similar, but for the second template.
24256
24257 Return 1 if the first partial specialization is more specialized;
24258 -1 if the second is more specialized; 0 if neither is more
24259 specialized.
24260
24261 See [temp.class.order] for information about determining which of
24262 two templates is more specialized. */
24263
24264 static int
24265 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24266 {
24267 tree targs;
24268 int winner = 0;
24269 bool any_deductions = false;
24270
24271 tree tmpl1 = TREE_VALUE (pat1);
24272 tree tmpl2 = TREE_VALUE (pat2);
24273 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24274 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24275
24276 /* Just like what happens for functions, if we are ordering between
24277 different template specializations, we may encounter dependent
24278 types in the arguments, and we need our dependency check functions
24279 to behave correctly. */
24280 ++processing_template_decl;
24281 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24282 if (targs)
24283 {
24284 --winner;
24285 any_deductions = true;
24286 }
24287
24288 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24289 if (targs)
24290 {
24291 ++winner;
24292 any_deductions = true;
24293 }
24294 --processing_template_decl;
24295
24296 /* If both deductions succeed, the partial ordering selects the more
24297 constrained template. */
24298 if (!winner && any_deductions)
24299 winner = more_constrained (tmpl1, tmpl2);
24300
24301 /* In the case of a tie where at least one of the templates
24302 has a parameter pack at the end, the template with the most
24303 non-packed parameters wins. */
24304 if (winner == 0
24305 && any_deductions
24306 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24307 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24308 {
24309 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24310 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24311 int len1 = TREE_VEC_LENGTH (args1);
24312 int len2 = TREE_VEC_LENGTH (args2);
24313
24314 /* We don't count the pack expansion at the end. */
24315 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24316 --len1;
24317 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24318 --len2;
24319
24320 if (len1 > len2)
24321 return 1;
24322 else if (len1 < len2)
24323 return -1;
24324 }
24325
24326 return winner;
24327 }
24328
24329 /* Return the template arguments that will produce the function signature
24330 DECL from the function template FN, with the explicit template
24331 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24332 also match. Return NULL_TREE if no satisfactory arguments could be
24333 found. */
24334
24335 static tree
24336 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24337 {
24338 int ntparms = DECL_NTPARMS (fn);
24339 tree targs = make_tree_vec (ntparms);
24340 tree decl_type = TREE_TYPE (decl);
24341 tree decl_arg_types;
24342 tree *args;
24343 unsigned int nargs, ix;
24344 tree arg;
24345
24346 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24347
24348 /* Never do unification on the 'this' parameter. */
24349 decl_arg_types = skip_artificial_parms_for (decl,
24350 TYPE_ARG_TYPES (decl_type));
24351
24352 nargs = list_length (decl_arg_types);
24353 args = XALLOCAVEC (tree, nargs);
24354 for (arg = decl_arg_types, ix = 0;
24355 arg != NULL_TREE && arg != void_list_node;
24356 arg = TREE_CHAIN (arg), ++ix)
24357 args[ix] = TREE_VALUE (arg);
24358
24359 if (fn_type_unification (fn, explicit_args, targs,
24360 args, ix,
24361 (check_rettype || DECL_CONV_FN_P (fn)
24362 ? TREE_TYPE (decl_type) : NULL_TREE),
24363 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24364 /*explain_p=*/false,
24365 /*decltype*/false)
24366 == error_mark_node)
24367 return NULL_TREE;
24368
24369 return targs;
24370 }
24371
24372 /* Return the innermost template arguments that, when applied to a partial
24373 specialization SPEC_TMPL of TMPL, yield the ARGS.
24374
24375 For example, suppose we have:
24376
24377 template <class T, class U> struct S {};
24378 template <class T> struct S<T*, int> {};
24379
24380 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24381 partial specialization and the ARGS will be {double*, int}. The resulting
24382 vector will be {double}, indicating that `T' is bound to `double'. */
24383
24384 static tree
24385 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24386 {
24387 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24388 tree spec_args
24389 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24390 int i, ntparms = TREE_VEC_LENGTH (tparms);
24391 tree deduced_args;
24392 tree innermost_deduced_args;
24393
24394 innermost_deduced_args = make_tree_vec (ntparms);
24395 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24396 {
24397 deduced_args = copy_node (args);
24398 SET_TMPL_ARGS_LEVEL (deduced_args,
24399 TMPL_ARGS_DEPTH (deduced_args),
24400 innermost_deduced_args);
24401 }
24402 else
24403 deduced_args = innermost_deduced_args;
24404
24405 bool tried_array_deduction = (cxx_dialect < cxx17);
24406 again:
24407 if (unify (tparms, deduced_args,
24408 INNERMOST_TEMPLATE_ARGS (spec_args),
24409 INNERMOST_TEMPLATE_ARGS (args),
24410 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24411 return NULL_TREE;
24412
24413 for (i = 0; i < ntparms; ++i)
24414 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24415 {
24416 if (!tried_array_deduction)
24417 {
24418 try_array_deduction (tparms, innermost_deduced_args,
24419 INNERMOST_TEMPLATE_ARGS (spec_args));
24420 tried_array_deduction = true;
24421 if (TREE_VEC_ELT (innermost_deduced_args, i))
24422 goto again;
24423 }
24424 return NULL_TREE;
24425 }
24426
24427 if (!push_tinst_level (spec_tmpl, deduced_args))
24428 {
24429 excessive_deduction_depth = true;
24430 return NULL_TREE;
24431 }
24432
24433 /* Verify that nondeduced template arguments agree with the type
24434 obtained from argument deduction.
24435
24436 For example:
24437
24438 struct A { typedef int X; };
24439 template <class T, class U> struct C {};
24440 template <class T> struct C<T, typename T::X> {};
24441
24442 Then with the instantiation `C<A, int>', we can deduce that
24443 `T' is `A' but unify () does not check whether `typename T::X'
24444 is `int'. */
24445 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24446
24447 if (spec_args != error_mark_node)
24448 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24449 INNERMOST_TEMPLATE_ARGS (spec_args),
24450 tmpl, tf_none, false, false);
24451
24452 pop_tinst_level ();
24453
24454 if (spec_args == error_mark_node
24455 /* We only need to check the innermost arguments; the other
24456 arguments will always agree. */
24457 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24458 INNERMOST_TEMPLATE_ARGS (args)))
24459 return NULL_TREE;
24460
24461 /* Now that we have bindings for all of the template arguments,
24462 ensure that the arguments deduced for the template template
24463 parameters have compatible template parameter lists. See the use
24464 of template_template_parm_bindings_ok_p in fn_type_unification
24465 for more information. */
24466 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24467 return NULL_TREE;
24468
24469 return deduced_args;
24470 }
24471
24472 // Compare two function templates T1 and T2 by deducing bindings
24473 // from one against the other. If both deductions succeed, compare
24474 // constraints to see which is more constrained.
24475 static int
24476 more_specialized_inst (tree t1, tree t2)
24477 {
24478 int fate = 0;
24479 int count = 0;
24480
24481 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24482 {
24483 --fate;
24484 ++count;
24485 }
24486
24487 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24488 {
24489 ++fate;
24490 ++count;
24491 }
24492
24493 // If both deductions succeed, then one may be more constrained.
24494 if (count == 2 && fate == 0)
24495 fate = more_constrained (t1, t2);
24496
24497 return fate;
24498 }
24499
24500 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24501 Return the TREE_LIST node with the most specialized template, if
24502 any. If there is no most specialized template, the error_mark_node
24503 is returned.
24504
24505 Note that this function does not look at, or modify, the
24506 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24507 returned is one of the elements of INSTANTIATIONS, callers may
24508 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24509 and retrieve it from the value returned. */
24510
24511 tree
24512 most_specialized_instantiation (tree templates)
24513 {
24514 tree fn, champ;
24515
24516 ++processing_template_decl;
24517
24518 champ = templates;
24519 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24520 {
24521 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24522 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24523 if (fate == -1)
24524 champ = fn;
24525 else if (!fate)
24526 {
24527 /* Equally specialized, move to next function. If there
24528 is no next function, nothing's most specialized. */
24529 fn = TREE_CHAIN (fn);
24530 champ = fn;
24531 if (!fn)
24532 break;
24533 }
24534 }
24535
24536 if (champ)
24537 /* Now verify that champ is better than everything earlier in the
24538 instantiation list. */
24539 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24540 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24541 {
24542 champ = NULL_TREE;
24543 break;
24544 }
24545 }
24546
24547 processing_template_decl--;
24548
24549 if (!champ)
24550 return error_mark_node;
24551
24552 return champ;
24553 }
24554
24555 /* If DECL is a specialization of some template, return the most
24556 general such template. Otherwise, returns NULL_TREE.
24557
24558 For example, given:
24559
24560 template <class T> struct S { template <class U> void f(U); };
24561
24562 if TMPL is `template <class U> void S<int>::f(U)' this will return
24563 the full template. This function will not trace past partial
24564 specializations, however. For example, given in addition:
24565
24566 template <class T> struct S<T*> { template <class U> void f(U); };
24567
24568 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24569 `template <class T> template <class U> S<T*>::f(U)'. */
24570
24571 tree
24572 most_general_template (tree decl)
24573 {
24574 if (TREE_CODE (decl) != TEMPLATE_DECL)
24575 {
24576 if (tree tinfo = get_template_info (decl))
24577 decl = TI_TEMPLATE (tinfo);
24578 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24579 template friend, or a FIELD_DECL for a capture pack. */
24580 if (TREE_CODE (decl) != TEMPLATE_DECL)
24581 return NULL_TREE;
24582 }
24583
24584 /* Look for more and more general templates. */
24585 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24586 {
24587 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24588 (See cp-tree.h for details.) */
24589 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24590 break;
24591
24592 if (CLASS_TYPE_P (TREE_TYPE (decl))
24593 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24594 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24595 break;
24596
24597 /* Stop if we run into an explicitly specialized class template. */
24598 if (!DECL_NAMESPACE_SCOPE_P (decl)
24599 && DECL_CONTEXT (decl)
24600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24601 break;
24602
24603 decl = DECL_TI_TEMPLATE (decl);
24604 }
24605
24606 return decl;
24607 }
24608
24609 /* Return the most specialized of the template partial specializations
24610 which can produce TARGET, a specialization of some class or variable
24611 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24612 a TEMPLATE_DECL node corresponding to the partial specialization, while
24613 the TREE_PURPOSE is the set of template arguments that must be
24614 substituted into the template pattern in order to generate TARGET.
24615
24616 If the choice of partial specialization is ambiguous, a diagnostic
24617 is issued, and the error_mark_node is returned. If there are no
24618 partial specializations matching TARGET, then NULL_TREE is
24619 returned, indicating that the primary template should be used. */
24620
24621 tree
24622 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24623 {
24624 tree list = NULL_TREE;
24625 tree t;
24626 tree champ;
24627 int fate;
24628 bool ambiguous_p;
24629 tree outer_args = NULL_TREE;
24630 tree tmpl, args;
24631
24632 if (TYPE_P (target))
24633 {
24634 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24635 tmpl = TI_TEMPLATE (tinfo);
24636 args = TI_ARGS (tinfo);
24637 }
24638 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24639 {
24640 tmpl = TREE_OPERAND (target, 0);
24641 args = TREE_OPERAND (target, 1);
24642 }
24643 else if (VAR_P (target))
24644 {
24645 tree tinfo = DECL_TEMPLATE_INFO (target);
24646 tmpl = TI_TEMPLATE (tinfo);
24647 args = TI_ARGS (tinfo);
24648 }
24649 else
24650 gcc_unreachable ();
24651
24652 tree main_tmpl = most_general_template (tmpl);
24653
24654 /* For determining which partial specialization to use, only the
24655 innermost args are interesting. */
24656 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24657 {
24658 outer_args = strip_innermost_template_args (args, 1);
24659 args = INNERMOST_TEMPLATE_ARGS (args);
24660 }
24661
24662 /* The caller hasn't called push_to_top_level yet, but we need
24663 get_partial_spec_bindings to be done in non-template context so that we'll
24664 fully resolve everything. */
24665 processing_template_decl_sentinel ptds;
24666
24667 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24668 {
24669 const tree ospec_tmpl = TREE_VALUE (t);
24670
24671 tree spec_tmpl;
24672 if (outer_args)
24673 {
24674 /* Substitute in the template args from the enclosing class. */
24675 ++processing_template_decl;
24676 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24677 --processing_template_decl;
24678 if (spec_tmpl == error_mark_node)
24679 return error_mark_node;
24680 }
24681 else
24682 spec_tmpl = ospec_tmpl;
24683
24684 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24685 if (spec_args)
24686 {
24687 if (outer_args)
24688 spec_args = add_to_template_args (outer_args, spec_args);
24689
24690 /* Keep the candidate only if the constraints are satisfied,
24691 or if we're not compiling with concepts. */
24692 if (!flag_concepts
24693 || constraints_satisfied_p (ospec_tmpl, spec_args))
24694 {
24695 list = tree_cons (spec_args, ospec_tmpl, list);
24696 TREE_TYPE (list) = TREE_TYPE (t);
24697 }
24698 }
24699 }
24700
24701 if (! list)
24702 return NULL_TREE;
24703
24704 ambiguous_p = false;
24705 t = list;
24706 champ = t;
24707 t = TREE_CHAIN (t);
24708 for (; t; t = TREE_CHAIN (t))
24709 {
24710 fate = more_specialized_partial_spec (tmpl, champ, t);
24711 if (fate == 1)
24712 ;
24713 else
24714 {
24715 if (fate == 0)
24716 {
24717 t = TREE_CHAIN (t);
24718 if (! t)
24719 {
24720 ambiguous_p = true;
24721 break;
24722 }
24723 }
24724 champ = t;
24725 }
24726 }
24727
24728 if (!ambiguous_p)
24729 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24730 {
24731 fate = more_specialized_partial_spec (tmpl, champ, t);
24732 if (fate != 1)
24733 {
24734 ambiguous_p = true;
24735 break;
24736 }
24737 }
24738
24739 if (ambiguous_p)
24740 {
24741 const char *str;
24742 char *spaces = NULL;
24743 if (!(complain & tf_error))
24744 return error_mark_node;
24745 if (TYPE_P (target))
24746 error ("ambiguous template instantiation for %q#T", target);
24747 else
24748 error ("ambiguous template instantiation for %q#D", target);
24749 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24750 for (t = list; t; t = TREE_CHAIN (t))
24751 {
24752 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24753 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24754 "%s %#qS", spaces ? spaces : str, subst);
24755 spaces = spaces ? spaces : get_spaces (str);
24756 }
24757 free (spaces);
24758 return error_mark_node;
24759 }
24760
24761 return champ;
24762 }
24763
24764 /* Explicitly instantiate DECL. */
24765
24766 void
24767 do_decl_instantiation (tree decl, tree storage)
24768 {
24769 tree result = NULL_TREE;
24770 int extern_p = 0;
24771
24772 if (!decl || decl == error_mark_node)
24773 /* An error occurred, for which grokdeclarator has already issued
24774 an appropriate message. */
24775 return;
24776 else if (! DECL_LANG_SPECIFIC (decl))
24777 {
24778 error ("explicit instantiation of non-template %q#D", decl);
24779 return;
24780 }
24781 else if (DECL_DECLARED_CONCEPT_P (decl))
24782 {
24783 if (VAR_P (decl))
24784 error ("explicit instantiation of variable concept %q#D", decl);
24785 else
24786 error ("explicit instantiation of function concept %q#D", decl);
24787 return;
24788 }
24789
24790 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24791 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24792
24793 if (VAR_P (decl) && !var_templ)
24794 {
24795 /* There is an asymmetry here in the way VAR_DECLs and
24796 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24797 the latter, the DECL we get back will be marked as a
24798 template instantiation, and the appropriate
24799 DECL_TEMPLATE_INFO will be set up. This does not happen for
24800 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24801 should handle VAR_DECLs as it currently handles
24802 FUNCTION_DECLs. */
24803 if (!DECL_CLASS_SCOPE_P (decl))
24804 {
24805 error ("%qD is not a static data member of a class template", decl);
24806 return;
24807 }
24808 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24809 if (!result || !VAR_P (result))
24810 {
24811 error ("no matching template for %qD found", decl);
24812 return;
24813 }
24814 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24815 {
24816 error ("type %qT for explicit instantiation %qD does not match "
24817 "declared type %qT", TREE_TYPE (result), decl,
24818 TREE_TYPE (decl));
24819 return;
24820 }
24821 }
24822 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24823 {
24824 error ("explicit instantiation of %q#D", decl);
24825 return;
24826 }
24827 else
24828 result = decl;
24829
24830 /* Check for various error cases. Note that if the explicit
24831 instantiation is valid the RESULT will currently be marked as an
24832 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24833 until we get here. */
24834
24835 if (DECL_TEMPLATE_SPECIALIZATION (result))
24836 {
24837 /* DR 259 [temp.spec].
24838
24839 Both an explicit instantiation and a declaration of an explicit
24840 specialization shall not appear in a program unless the explicit
24841 instantiation follows a declaration of the explicit specialization.
24842
24843 For a given set of template parameters, if an explicit
24844 instantiation of a template appears after a declaration of an
24845 explicit specialization for that template, the explicit
24846 instantiation has no effect. */
24847 return;
24848 }
24849 else if (DECL_EXPLICIT_INSTANTIATION (result))
24850 {
24851 /* [temp.spec]
24852
24853 No program shall explicitly instantiate any template more
24854 than once.
24855
24856 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24857 the first instantiation was `extern' and the second is not,
24858 and EXTERN_P for the opposite case. */
24859 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24860 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24861 /* If an "extern" explicit instantiation follows an ordinary
24862 explicit instantiation, the template is instantiated. */
24863 if (extern_p)
24864 return;
24865 }
24866 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24867 {
24868 error ("no matching template for %qD found", result);
24869 return;
24870 }
24871 else if (!DECL_TEMPLATE_INFO (result))
24872 {
24873 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24874 return;
24875 }
24876
24877 if (storage == NULL_TREE)
24878 ;
24879 else if (storage == ridpointers[(int) RID_EXTERN])
24880 {
24881 if (cxx_dialect == cxx98)
24882 pedwarn (input_location, OPT_Wpedantic,
24883 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24884 "instantiations");
24885 extern_p = 1;
24886 }
24887 else
24888 error ("storage class %qD applied to template instantiation", storage);
24889
24890 check_explicit_instantiation_namespace (result);
24891 mark_decl_instantiated (result, extern_p);
24892 if (! extern_p)
24893 instantiate_decl (result, /*defer_ok=*/true,
24894 /*expl_inst_class_mem_p=*/false);
24895 }
24896
24897 static void
24898 mark_class_instantiated (tree t, int extern_p)
24899 {
24900 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24901 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24902 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24903 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24904 if (! extern_p)
24905 {
24906 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24907 rest_of_type_compilation (t, 1);
24908 }
24909 }
24910
24911 /* Called from do_type_instantiation through binding_table_foreach to
24912 do recursive instantiation for the type bound in ENTRY. */
24913 static void
24914 bt_instantiate_type_proc (binding_entry entry, void *data)
24915 {
24916 tree storage = *(tree *) data;
24917
24918 if (MAYBE_CLASS_TYPE_P (entry->type)
24919 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24920 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24921 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24922 }
24923
24924 /* Perform an explicit instantiation of template class T. STORAGE, if
24925 non-null, is the RID for extern, inline or static. COMPLAIN is
24926 nonzero if this is called from the parser, zero if called recursively,
24927 since the standard is unclear (as detailed below). */
24928
24929 void
24930 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24931 {
24932 int extern_p = 0;
24933 int nomem_p = 0;
24934 int static_p = 0;
24935 int previous_instantiation_extern_p = 0;
24936
24937 if (TREE_CODE (t) == TYPE_DECL)
24938 t = TREE_TYPE (t);
24939
24940 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24941 {
24942 tree tmpl =
24943 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24944 if (tmpl)
24945 error ("explicit instantiation of non-class template %qD", tmpl);
24946 else
24947 error ("explicit instantiation of non-template type %qT", t);
24948 return;
24949 }
24950
24951 complete_type (t);
24952
24953 if (!COMPLETE_TYPE_P (t))
24954 {
24955 if (complain & tf_error)
24956 error ("explicit instantiation of %q#T before definition of template",
24957 t);
24958 return;
24959 }
24960
24961 if (storage != NULL_TREE)
24962 {
24963 if (storage == ridpointers[(int) RID_EXTERN])
24964 {
24965 if (cxx_dialect == cxx98)
24966 pedwarn (input_location, OPT_Wpedantic,
24967 "ISO C++ 1998 forbids the use of %<extern%> on "
24968 "explicit instantiations");
24969 }
24970 else
24971 pedwarn (input_location, OPT_Wpedantic,
24972 "ISO C++ forbids the use of %qE"
24973 " on explicit instantiations", storage);
24974
24975 if (storage == ridpointers[(int) RID_INLINE])
24976 nomem_p = 1;
24977 else if (storage == ridpointers[(int) RID_EXTERN])
24978 extern_p = 1;
24979 else if (storage == ridpointers[(int) RID_STATIC])
24980 static_p = 1;
24981 else
24982 {
24983 error ("storage class %qD applied to template instantiation",
24984 storage);
24985 extern_p = 0;
24986 }
24987 }
24988
24989 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24990 {
24991 /* DR 259 [temp.spec].
24992
24993 Both an explicit instantiation and a declaration of an explicit
24994 specialization shall not appear in a program unless the explicit
24995 instantiation follows a declaration of the explicit specialization.
24996
24997 For a given set of template parameters, if an explicit
24998 instantiation of a template appears after a declaration of an
24999 explicit specialization for that template, the explicit
25000 instantiation has no effect. */
25001 return;
25002 }
25003 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
25004 {
25005 /* [temp.spec]
25006
25007 No program shall explicitly instantiate any template more
25008 than once.
25009
25010 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25011 instantiation was `extern'. If EXTERN_P then the second is.
25012 These cases are OK. */
25013 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
25014
25015 if (!previous_instantiation_extern_p && !extern_p
25016 && (complain & tf_error))
25017 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
25018
25019 /* If we've already instantiated the template, just return now. */
25020 if (!CLASSTYPE_INTERFACE_ONLY (t))
25021 return;
25022 }
25023
25024 check_explicit_instantiation_namespace (TYPE_NAME (t));
25025 mark_class_instantiated (t, extern_p);
25026
25027 if (nomem_p)
25028 return;
25029
25030 /* In contrast to implicit instantiation, where only the
25031 declarations, and not the definitions, of members are
25032 instantiated, we have here:
25033
25034 [temp.explicit]
25035
25036 An explicit instantiation that names a class template
25037 specialization is also an explicit instantiation of the same
25038 kind (declaration or definition) of each of its members (not
25039 including members inherited from base classes and members
25040 that are templates) that has not been previously explicitly
25041 specialized in the translation unit containing the explicit
25042 instantiation, provided that the associated constraints, if
25043 any, of that member are satisfied by the template arguments
25044 of the explicit instantiation. */
25045 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25046 if ((VAR_P (fld)
25047 || (TREE_CODE (fld) == FUNCTION_DECL
25048 && !static_p
25049 && user_provided_p (fld)))
25050 && DECL_TEMPLATE_INSTANTIATION (fld)
25051 && constraints_satisfied_p (fld))
25052 {
25053 mark_decl_instantiated (fld, extern_p);
25054 if (! extern_p)
25055 instantiate_decl (fld, /*defer_ok=*/true,
25056 /*expl_inst_class_mem_p=*/true);
25057 }
25058
25059 if (CLASSTYPE_NESTED_UTDS (t))
25060 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
25061 bt_instantiate_type_proc, &storage);
25062 }
25063
25064 /* Given a function DECL, which is a specialization of TMPL, modify
25065 DECL to be a re-instantiation of TMPL with the same template
25066 arguments. TMPL should be the template into which tsubst'ing
25067 should occur for DECL, not the most general template.
25068
25069 One reason for doing this is a scenario like this:
25070
25071 template <class T>
25072 void f(const T&, int i);
25073
25074 void g() { f(3, 7); }
25075
25076 template <class T>
25077 void f(const T& t, const int i) { }
25078
25079 Note that when the template is first instantiated, with
25080 instantiate_template, the resulting DECL will have no name for the
25081 first parameter, and the wrong type for the second. So, when we go
25082 to instantiate the DECL, we regenerate it. */
25083
25084 static void
25085 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25086 {
25087 /* The arguments used to instantiate DECL, from the most general
25088 template. */
25089 tree code_pattern;
25090
25091 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25092
25093 /* Make sure that we can see identifiers, and compute access
25094 correctly. */
25095 push_access_scope (decl);
25096
25097 if (TREE_CODE (decl) == FUNCTION_DECL)
25098 {
25099 tree decl_parm;
25100 tree pattern_parm;
25101 tree specs;
25102 int args_depth;
25103 int parms_depth;
25104
25105 args_depth = TMPL_ARGS_DEPTH (args);
25106 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25107 if (args_depth > parms_depth)
25108 args = get_innermost_template_args (args, parms_depth);
25109
25110 /* Instantiate a dynamic exception-specification. noexcept will be
25111 handled below. */
25112 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25113 if (TREE_VALUE (raises))
25114 {
25115 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25116 args, tf_error, NULL_TREE,
25117 /*defer_ok*/false);
25118 if (specs && specs != error_mark_node)
25119 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25120 specs);
25121 }
25122
25123 /* Merge parameter declarations. */
25124 decl_parm = skip_artificial_parms_for (decl,
25125 DECL_ARGUMENTS (decl));
25126 pattern_parm
25127 = skip_artificial_parms_for (code_pattern,
25128 DECL_ARGUMENTS (code_pattern));
25129 while (decl_parm && !DECL_PACK_P (pattern_parm))
25130 {
25131 tree parm_type;
25132 tree attributes;
25133
25134 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25135 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25136 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25137 NULL_TREE);
25138 parm_type = type_decays_to (parm_type);
25139 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25140 TREE_TYPE (decl_parm) = parm_type;
25141 attributes = DECL_ATTRIBUTES (pattern_parm);
25142 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25143 {
25144 DECL_ATTRIBUTES (decl_parm) = attributes;
25145 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25146 }
25147 decl_parm = DECL_CHAIN (decl_parm);
25148 pattern_parm = DECL_CHAIN (pattern_parm);
25149 }
25150 /* Merge any parameters that match with the function parameter
25151 pack. */
25152 if (pattern_parm && DECL_PACK_P (pattern_parm))
25153 {
25154 int i, len;
25155 tree expanded_types;
25156 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25157 the parameters in this function parameter pack. */
25158 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25159 args, tf_error, NULL_TREE);
25160 len = TREE_VEC_LENGTH (expanded_types);
25161 for (i = 0; i < len; i++)
25162 {
25163 tree parm_type;
25164 tree attributes;
25165
25166 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25167 /* Rename the parameter to include the index. */
25168 DECL_NAME (decl_parm) =
25169 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25170 parm_type = TREE_VEC_ELT (expanded_types, i);
25171 parm_type = type_decays_to (parm_type);
25172 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25173 TREE_TYPE (decl_parm) = parm_type;
25174 attributes = DECL_ATTRIBUTES (pattern_parm);
25175 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25176 {
25177 DECL_ATTRIBUTES (decl_parm) = attributes;
25178 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25179 }
25180 decl_parm = DECL_CHAIN (decl_parm);
25181 }
25182 }
25183 /* Merge additional specifiers from the CODE_PATTERN. */
25184 if (DECL_DECLARED_INLINE_P (code_pattern)
25185 && !DECL_DECLARED_INLINE_P (decl))
25186 DECL_DECLARED_INLINE_P (decl) = 1;
25187
25188 maybe_instantiate_noexcept (decl, tf_error);
25189 }
25190 else if (VAR_P (decl))
25191 {
25192 start_lambda_scope (decl);
25193 DECL_INITIAL (decl) =
25194 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25195 tf_error, DECL_TI_TEMPLATE (decl));
25196 finish_lambda_scope ();
25197 if (VAR_HAD_UNKNOWN_BOUND (decl))
25198 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25199 tf_error, DECL_TI_TEMPLATE (decl));
25200 }
25201 else
25202 gcc_unreachable ();
25203
25204 pop_access_scope (decl);
25205 }
25206
25207 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25208 substituted to get DECL. */
25209
25210 tree
25211 template_for_substitution (tree decl)
25212 {
25213 tree tmpl = DECL_TI_TEMPLATE (decl);
25214
25215 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25216 for the instantiation. This is not always the most general
25217 template. Consider, for example:
25218
25219 template <class T>
25220 struct S { template <class U> void f();
25221 template <> void f<int>(); };
25222
25223 and an instantiation of S<double>::f<int>. We want TD to be the
25224 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25225 while (/* An instantiation cannot have a definition, so we need a
25226 more general template. */
25227 DECL_TEMPLATE_INSTANTIATION (tmpl)
25228 /* We must also deal with friend templates. Given:
25229
25230 template <class T> struct S {
25231 template <class U> friend void f() {};
25232 };
25233
25234 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25235 so far as the language is concerned, but that's still
25236 where we get the pattern for the instantiation from. On
25237 other hand, if the definition comes outside the class, say:
25238
25239 template <class T> struct S {
25240 template <class U> friend void f();
25241 };
25242 template <class U> friend void f() {}
25243
25244 we don't need to look any further. That's what the check for
25245 DECL_INITIAL is for. */
25246 || (TREE_CODE (decl) == FUNCTION_DECL
25247 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25248 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25249 {
25250 /* The present template, TD, should not be a definition. If it
25251 were a definition, we should be using it! Note that we
25252 cannot restructure the loop to just keep going until we find
25253 a template with a definition, since that might go too far if
25254 a specialization was declared, but not defined. */
25255
25256 /* Fetch the more general template. */
25257 tmpl = DECL_TI_TEMPLATE (tmpl);
25258 }
25259
25260 return tmpl;
25261 }
25262
25263 /* Returns true if we need to instantiate this template instance even if we
25264 know we aren't going to emit it. */
25265
25266 bool
25267 always_instantiate_p (tree decl)
25268 {
25269 /* We always instantiate inline functions so that we can inline them. An
25270 explicit instantiation declaration prohibits implicit instantiation of
25271 non-inline functions. With high levels of optimization, we would
25272 normally inline non-inline functions -- but we're not allowed to do
25273 that for "extern template" functions. Therefore, we check
25274 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25275 return ((TREE_CODE (decl) == FUNCTION_DECL
25276 && (DECL_DECLARED_INLINE_P (decl)
25277 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25278 /* And we need to instantiate static data members so that
25279 their initializers are available in integral constant
25280 expressions. */
25281 || (VAR_P (decl)
25282 && decl_maybe_constant_var_p (decl)));
25283 }
25284
25285 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25286 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25287 error, true otherwise. */
25288
25289 bool
25290 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25291 {
25292 tree fntype, spec, noex;
25293
25294 /* Don't instantiate a noexcept-specification from template context. */
25295 if (processing_template_decl
25296 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25297 return true;
25298
25299 if (DECL_MAYBE_DELETED (fn))
25300 {
25301 if (fn == current_function_decl)
25302 /* We're in start_preparsed_function, keep going. */
25303 return true;
25304
25305 ++function_depth;
25306 synthesize_method (fn);
25307 --function_depth;
25308 return !DECL_MAYBE_DELETED (fn);
25309 }
25310
25311 fntype = TREE_TYPE (fn);
25312 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25313
25314 if (!spec || !TREE_PURPOSE (spec))
25315 return true;
25316
25317 noex = TREE_PURPOSE (spec);
25318 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25319 && TREE_CODE (noex) != DEFERRED_PARSE)
25320 return true;
25321
25322 tree orig_fn = NULL_TREE;
25323 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25324 its FUNCTION_DECL for the rest of this function -- push_access_scope
25325 doesn't accept TEMPLATE_DECLs. */
25326 if (DECL_FUNCTION_TEMPLATE_P (fn))
25327 {
25328 orig_fn = fn;
25329 fn = DECL_TEMPLATE_RESULT (fn);
25330 }
25331
25332 if (DECL_CLONED_FUNCTION_P (fn))
25333 {
25334 tree prime = DECL_CLONED_FUNCTION (fn);
25335 if (!maybe_instantiate_noexcept (prime, complain))
25336 return false;
25337 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25338 }
25339 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25340 {
25341 static hash_set<tree>* fns = new hash_set<tree>;
25342 bool added = false;
25343 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25344 {
25345 spec = get_defaulted_eh_spec (fn, complain);
25346 if (spec == error_mark_node)
25347 /* This might have failed because of an unparsed DMI, so
25348 let's try again later. */
25349 return false;
25350 }
25351 else if (!(added = !fns->add (fn)))
25352 {
25353 /* If hash_set::add returns true, the element was already there. */
25354 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25355 DECL_SOURCE_LOCATION (fn));
25356 error_at (loc,
25357 "exception specification of %qD depends on itself",
25358 fn);
25359 spec = noexcept_false_spec;
25360 }
25361 else if (push_tinst_level (fn))
25362 {
25363 push_to_top_level ();
25364 push_access_scope (fn);
25365 push_deferring_access_checks (dk_no_deferred);
25366 input_location = DECL_SOURCE_LOCATION (fn);
25367
25368 /* If needed, set current_class_ptr for the benefit of
25369 tsubst_copy/PARM_DECL. */
25370 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25371 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25372 {
25373 tree this_parm = DECL_ARGUMENTS (tdecl);
25374 current_class_ptr = NULL_TREE;
25375 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25376 current_class_ptr = this_parm;
25377 }
25378
25379 /* If this function is represented by a TEMPLATE_DECL, then
25380 the deferred noexcept-specification might still contain
25381 dependent types, even after substitution. And we need the
25382 dependency check functions to work in build_noexcept_spec. */
25383 if (orig_fn)
25384 ++processing_template_decl;
25385
25386 /* Do deferred instantiation of the noexcept-specifier. */
25387 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25388 DEFERRED_NOEXCEPT_ARGS (noex),
25389 tf_warning_or_error, fn,
25390 /*function_p=*/false,
25391 /*i_c_e_p=*/true);
25392
25393 /* Build up the noexcept-specification. */
25394 spec = build_noexcept_spec (noex, tf_warning_or_error);
25395
25396 if (orig_fn)
25397 --processing_template_decl;
25398
25399 pop_deferring_access_checks ();
25400 pop_access_scope (fn);
25401 pop_tinst_level ();
25402 pop_from_top_level ();
25403 }
25404 else
25405 spec = noexcept_false_spec;
25406
25407 if (added)
25408 fns->remove (fn);
25409 }
25410
25411 if (spec == error_mark_node)
25412 {
25413 /* This failed with a hard error, so let's go with false. */
25414 gcc_assert (seen_error ());
25415 spec = noexcept_false_spec;
25416 }
25417
25418 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25419 if (orig_fn)
25420 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25421
25422 return true;
25423 }
25424
25425 /* We're starting to process the function INST, an instantiation of PATTERN;
25426 add their parameters to local_specializations. */
25427
25428 static void
25429 register_parameter_specializations (tree pattern, tree inst)
25430 {
25431 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25432 tree spec_parm = DECL_ARGUMENTS (inst);
25433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25434 {
25435 register_local_specialization (spec_parm, tmpl_parm);
25436 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25437 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25438 }
25439 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25440 {
25441 if (!DECL_PACK_P (tmpl_parm)
25442 || (spec_parm && DECL_PACK_P (spec_parm)))
25443 {
25444 register_local_specialization (spec_parm, tmpl_parm);
25445 spec_parm = DECL_CHAIN (spec_parm);
25446 }
25447 else
25448 {
25449 /* Register the (value) argument pack as a specialization of
25450 TMPL_PARM, then move on. */
25451 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25452 register_local_specialization (argpack, tmpl_parm);
25453 }
25454 }
25455 gcc_assert (!spec_parm);
25456 }
25457
25458 /* Instantiate the body of D using PATTERN with ARGS. We have
25459 already determined PATTERN is the correct template to use.
25460 NESTED_P is true if this is a nested function, in which case
25461 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25462
25463 static void
25464 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25465 {
25466 tree td = NULL_TREE;
25467 tree code_pattern = pattern;
25468
25469 if (!nested_p)
25470 {
25471 td = pattern;
25472 code_pattern = DECL_TEMPLATE_RESULT (td);
25473 }
25474 else
25475 /* Only OMP reductions are nested. */
25476 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25477
25478 vec<tree> omp_privatization_save;
25479 if (current_function_decl)
25480 save_omp_privatization_clauses (omp_privatization_save);
25481
25482 bool push_to_top
25483 = !(current_function_decl
25484 && !LAMBDA_FUNCTION_P (d)
25485 && decl_function_context (d) == current_function_decl);
25486
25487 if (push_to_top)
25488 push_to_top_level ();
25489 else
25490 {
25491 gcc_assert (!processing_template_decl);
25492 push_function_context ();
25493 cp_unevaluated_operand = 0;
25494 c_inhibit_evaluation_warnings = 0;
25495 }
25496
25497 if (VAR_P (d))
25498 {
25499 /* The variable might be a lambda's extra scope, and that
25500 lambda's visibility depends on D's. */
25501 maybe_commonize_var (d);
25502 determine_visibility (d);
25503 }
25504
25505 /* Mark D as instantiated so that recursive calls to
25506 instantiate_decl do not try to instantiate it again. */
25507 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25508
25509 if (td)
25510 /* Regenerate the declaration in case the template has been modified
25511 by a subsequent redeclaration. */
25512 regenerate_decl_from_template (d, td, args);
25513
25514 /* We already set the file and line above. Reset them now in case
25515 they changed as a result of calling regenerate_decl_from_template. */
25516 input_location = DECL_SOURCE_LOCATION (d);
25517
25518 if (VAR_P (d))
25519 {
25520 tree init;
25521 bool const_init = false;
25522
25523 /* Clear out DECL_RTL; whatever was there before may not be right
25524 since we've reset the type of the declaration. */
25525 SET_DECL_RTL (d, NULL);
25526 DECL_IN_AGGR_P (d) = 0;
25527
25528 /* The initializer is placed in DECL_INITIAL by
25529 regenerate_decl_from_template so we don't need to
25530 push/pop_access_scope again here. Pull it out so that
25531 cp_finish_decl can process it. */
25532 init = DECL_INITIAL (d);
25533 DECL_INITIAL (d) = NULL_TREE;
25534 DECL_INITIALIZED_P (d) = 0;
25535
25536 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25537 initializer. That function will defer actual emission until
25538 we have a chance to determine linkage. */
25539 DECL_EXTERNAL (d) = 0;
25540
25541 /* Enter the scope of D so that access-checking works correctly. */
25542 bool enter_context = DECL_CLASS_SCOPE_P (d);
25543 if (enter_context)
25544 push_nested_class (DECL_CONTEXT (d));
25545
25546 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25547 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25548
25549 if (enter_context)
25550 pop_nested_class ();
25551 }
25552 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25553 synthesize_method (d);
25554 else if (TREE_CODE (d) == FUNCTION_DECL)
25555 {
25556 /* Set up the list of local specializations. */
25557 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25558 tree block = NULL_TREE;
25559
25560 /* Set up context. */
25561 if (nested_p)
25562 block = push_stmt_list ();
25563 else
25564 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25565
25566 perform_instantiation_time_access_checks (code_pattern, args);
25567
25568 /* Create substitution entries for the parameters. */
25569 register_parameter_specializations (code_pattern, d);
25570
25571 /* Substitute into the body of the function. */
25572 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25573 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25574 tf_warning_or_error, d);
25575 else
25576 {
25577 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25578 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25579 /*integral_constant_expression_p=*/false);
25580
25581 /* Set the current input_location to the end of the function
25582 so that finish_function knows where we are. */
25583 input_location
25584 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25585
25586 /* Remember if we saw an infinite loop in the template. */
25587 current_function_infinite_loop
25588 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25589 }
25590
25591 /* Finish the function. */
25592 if (nested_p)
25593 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25594 else
25595 {
25596 d = finish_function (/*inline_p=*/false);
25597 expand_or_defer_fn (d);
25598 }
25599
25600 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25601 cp_check_omp_declare_reduction (d);
25602 }
25603
25604 /* We're not deferring instantiation any more. */
25605 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25606
25607 if (push_to_top)
25608 pop_from_top_level ();
25609 else
25610 pop_function_context ();
25611
25612 if (current_function_decl)
25613 restore_omp_privatization_clauses (omp_privatization_save);
25614 }
25615
25616 /* Produce the definition of D, a _DECL generated from a template. If
25617 DEFER_OK is true, then we don't have to actually do the
25618 instantiation now; we just have to do it sometime. Normally it is
25619 an error if this is an explicit instantiation but D is undefined.
25620 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25621 instantiated class template. */
25622
25623 tree
25624 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25625 {
25626 tree tmpl = DECL_TI_TEMPLATE (d);
25627 tree gen_args;
25628 tree args;
25629 tree td;
25630 tree code_pattern;
25631 tree spec;
25632 tree gen_tmpl;
25633 bool pattern_defined;
25634 location_t saved_loc = input_location;
25635 int saved_unevaluated_operand = cp_unevaluated_operand;
25636 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25637 bool external_p;
25638 bool deleted_p;
25639
25640 /* This function should only be used to instantiate templates for
25641 functions and static member variables. */
25642 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25643
25644 /* A concept is never instantiated. */
25645 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25646
25647 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25648
25649 /* Variables are never deferred; if instantiation is required, they
25650 are instantiated right away. That allows for better code in the
25651 case that an expression refers to the value of the variable --
25652 if the variable has a constant value the referring expression can
25653 take advantage of that fact. */
25654 if (VAR_P (d))
25655 defer_ok = false;
25656
25657 /* Don't instantiate cloned functions. Instead, instantiate the
25658 functions they cloned. */
25659 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25660 d = DECL_CLONED_FUNCTION (d);
25661
25662 if (DECL_TEMPLATE_INSTANTIATED (d)
25663 || TREE_TYPE (d) == error_mark_node
25664 || (TREE_CODE (d) == FUNCTION_DECL
25665 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25666 || DECL_TEMPLATE_SPECIALIZATION (d))
25667 /* D has already been instantiated or explicitly specialized, so
25668 there's nothing for us to do here.
25669
25670 It might seem reasonable to check whether or not D is an explicit
25671 instantiation, and, if so, stop here. But when an explicit
25672 instantiation is deferred until the end of the compilation,
25673 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25674 the instantiation. */
25675 return d;
25676
25677 /* Check to see whether we know that this template will be
25678 instantiated in some other file, as with "extern template"
25679 extension. */
25680 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25681
25682 /* In general, we do not instantiate such templates. */
25683 if (external_p && !always_instantiate_p (d))
25684 return d;
25685
25686 gen_tmpl = most_general_template (tmpl);
25687 gen_args = DECL_TI_ARGS (d);
25688
25689 /* We should already have the extra args. */
25690 gcc_checking_assert (tmpl == gen_tmpl
25691 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25692 == TMPL_ARGS_DEPTH (gen_args)));
25693 /* And what's in the hash table should match D. */
25694 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25695 == d
25696 || spec == NULL_TREE);
25697
25698 /* This needs to happen before any tsubsting. */
25699 if (! push_tinst_level (d))
25700 return d;
25701
25702 timevar_push (TV_TEMPLATE_INST);
25703
25704 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25705 for the instantiation. */
25706 td = template_for_substitution (d);
25707 args = gen_args;
25708
25709 if (VAR_P (d))
25710 {
25711 /* Look up an explicit specialization, if any. */
25712 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25713 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25714 if (elt && elt != error_mark_node)
25715 {
25716 td = TREE_VALUE (elt);
25717 args = TREE_PURPOSE (elt);
25718 }
25719 }
25720
25721 code_pattern = DECL_TEMPLATE_RESULT (td);
25722
25723 /* We should never be trying to instantiate a member of a class
25724 template or partial specialization. */
25725 gcc_assert (d != code_pattern);
25726
25727 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25728 || DECL_TEMPLATE_SPECIALIZATION (td))
25729 /* In the case of a friend template whose definition is provided
25730 outside the class, we may have too many arguments. Drop the
25731 ones we don't need. The same is true for specializations. */
25732 args = get_innermost_template_args
25733 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25734
25735 if (TREE_CODE (d) == FUNCTION_DECL)
25736 {
25737 deleted_p = DECL_DELETED_FN (code_pattern);
25738 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25739 && DECL_INITIAL (code_pattern) != error_mark_node)
25740 || DECL_DEFAULTED_FN (code_pattern)
25741 || deleted_p);
25742 }
25743 else
25744 {
25745 deleted_p = false;
25746 if (DECL_CLASS_SCOPE_P (code_pattern))
25747 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25748 else
25749 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25750 }
25751
25752 /* We may be in the middle of deferred access check. Disable it now. */
25753 push_deferring_access_checks (dk_no_deferred);
25754
25755 /* Unless an explicit instantiation directive has already determined
25756 the linkage of D, remember that a definition is available for
25757 this entity. */
25758 if (pattern_defined
25759 && !DECL_INTERFACE_KNOWN (d)
25760 && !DECL_NOT_REALLY_EXTERN (d))
25761 mark_definable (d);
25762
25763 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25764 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25765 input_location = DECL_SOURCE_LOCATION (d);
25766
25767 /* If D is a member of an explicitly instantiated class template,
25768 and no definition is available, treat it like an implicit
25769 instantiation. */
25770 if (!pattern_defined && expl_inst_class_mem_p
25771 && DECL_EXPLICIT_INSTANTIATION (d))
25772 {
25773 /* Leave linkage flags alone on instantiations with anonymous
25774 visibility. */
25775 if (TREE_PUBLIC (d))
25776 {
25777 DECL_NOT_REALLY_EXTERN (d) = 0;
25778 DECL_INTERFACE_KNOWN (d) = 0;
25779 }
25780 SET_DECL_IMPLICIT_INSTANTIATION (d);
25781 }
25782
25783 /* Defer all other templates, unless we have been explicitly
25784 forbidden from doing so. */
25785 if (/* If there is no definition, we cannot instantiate the
25786 template. */
25787 ! pattern_defined
25788 /* If it's OK to postpone instantiation, do so. */
25789 || defer_ok
25790 /* If this is a static data member that will be defined
25791 elsewhere, we don't want to instantiate the entire data
25792 member, but we do want to instantiate the initializer so that
25793 we can substitute that elsewhere. */
25794 || (external_p && VAR_P (d))
25795 /* Handle here a deleted function too, avoid generating
25796 its body (c++/61080). */
25797 || deleted_p)
25798 {
25799 /* The definition of the static data member is now required so
25800 we must substitute the initializer. */
25801 if (VAR_P (d)
25802 && !DECL_INITIAL (d)
25803 && DECL_INITIAL (code_pattern))
25804 {
25805 tree ns;
25806 tree init;
25807 bool const_init = false;
25808 bool enter_context = DECL_CLASS_SCOPE_P (d);
25809
25810 ns = decl_namespace_context (d);
25811 push_nested_namespace (ns);
25812 if (enter_context)
25813 push_nested_class (DECL_CONTEXT (d));
25814 init = tsubst_expr (DECL_INITIAL (code_pattern),
25815 args,
25816 tf_warning_or_error, NULL_TREE,
25817 /*integral_constant_expression_p=*/false);
25818 /* If instantiating the initializer involved instantiating this
25819 again, don't call cp_finish_decl twice. */
25820 if (!DECL_INITIAL (d))
25821 {
25822 /* Make sure the initializer is still constant, in case of
25823 circular dependency (template/instantiate6.C). */
25824 const_init
25825 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25826 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25827 /*asmspec_tree=*/NULL_TREE,
25828 LOOKUP_ONLYCONVERTING);
25829 }
25830 if (enter_context)
25831 pop_nested_class ();
25832 pop_nested_namespace (ns);
25833 }
25834
25835 /* We restore the source position here because it's used by
25836 add_pending_template. */
25837 input_location = saved_loc;
25838
25839 if (at_eof && !pattern_defined
25840 && DECL_EXPLICIT_INSTANTIATION (d)
25841 && DECL_NOT_REALLY_EXTERN (d))
25842 /* [temp.explicit]
25843
25844 The definition of a non-exported function template, a
25845 non-exported member function template, or a non-exported
25846 member function or static data member of a class template
25847 shall be present in every translation unit in which it is
25848 explicitly instantiated. */
25849 permerror (input_location, "explicit instantiation of %qD "
25850 "but no definition available", d);
25851
25852 /* If we're in unevaluated context, we just wanted to get the
25853 constant value; this isn't an odr use, so don't queue
25854 a full instantiation. */
25855 if (!cp_unevaluated_operand
25856 /* ??? Historically, we have instantiated inline functions, even
25857 when marked as "extern template". */
25858 && !(external_p && VAR_P (d)))
25859 add_pending_template (d);
25860 }
25861 else
25862 {
25863 if (variable_template_p (gen_tmpl))
25864 note_variable_template_instantiation (d);
25865 instantiate_body (td, args, d, false);
25866 }
25867
25868 pop_deferring_access_checks ();
25869 timevar_pop (TV_TEMPLATE_INST);
25870 pop_tinst_level ();
25871 input_location = saved_loc;
25872 cp_unevaluated_operand = saved_unevaluated_operand;
25873 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25874
25875 return d;
25876 }
25877
25878 /* Run through the list of templates that we wish we could
25879 instantiate, and instantiate any we can. RETRIES is the
25880 number of times we retry pending template instantiation. */
25881
25882 void
25883 instantiate_pending_templates (int retries)
25884 {
25885 int reconsider;
25886 location_t saved_loc = input_location;
25887
25888 /* Instantiating templates may trigger vtable generation. This in turn
25889 may require further template instantiations. We place a limit here
25890 to avoid infinite loop. */
25891 if (pending_templates && retries >= max_tinst_depth)
25892 {
25893 tree decl = pending_templates->tinst->maybe_get_node ();
25894
25895 fatal_error (input_location,
25896 "template instantiation depth exceeds maximum of %d"
25897 " instantiating %q+D, possibly from virtual table generation"
25898 " (use %<-ftemplate-depth=%> to increase the maximum)",
25899 max_tinst_depth, decl);
25900 if (TREE_CODE (decl) == FUNCTION_DECL)
25901 /* Pretend that we defined it. */
25902 DECL_INITIAL (decl) = error_mark_node;
25903 return;
25904 }
25905
25906 do
25907 {
25908 struct pending_template **t = &pending_templates;
25909 struct pending_template *last = NULL;
25910 reconsider = 0;
25911 while (*t)
25912 {
25913 tree instantiation = reopen_tinst_level ((*t)->tinst);
25914 bool complete = false;
25915
25916 if (TYPE_P (instantiation))
25917 {
25918 if (!COMPLETE_TYPE_P (instantiation))
25919 {
25920 instantiate_class_template (instantiation);
25921 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25922 for (tree fld = TYPE_FIELDS (instantiation);
25923 fld; fld = TREE_CHAIN (fld))
25924 if ((VAR_P (fld)
25925 || (TREE_CODE (fld) == FUNCTION_DECL
25926 && !DECL_ARTIFICIAL (fld)))
25927 && DECL_TEMPLATE_INSTANTIATION (fld))
25928 instantiate_decl (fld,
25929 /*defer_ok=*/false,
25930 /*expl_inst_class_mem_p=*/false);
25931
25932 if (COMPLETE_TYPE_P (instantiation))
25933 reconsider = 1;
25934 }
25935
25936 complete = COMPLETE_TYPE_P (instantiation);
25937 }
25938 else
25939 {
25940 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25941 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25942 {
25943 instantiation
25944 = instantiate_decl (instantiation,
25945 /*defer_ok=*/false,
25946 /*expl_inst_class_mem_p=*/false);
25947 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25948 reconsider = 1;
25949 }
25950
25951 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25952 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25953 }
25954
25955 if (complete)
25956 {
25957 /* If INSTANTIATION has been instantiated, then we don't
25958 need to consider it again in the future. */
25959 struct pending_template *drop = *t;
25960 *t = (*t)->next;
25961 set_refcount_ptr (drop->tinst);
25962 pending_template_freelist ().free (drop);
25963 }
25964 else
25965 {
25966 last = *t;
25967 t = &(*t)->next;
25968 }
25969 tinst_depth = 0;
25970 set_refcount_ptr (current_tinst_level);
25971 }
25972 last_pending_template = last;
25973 }
25974 while (reconsider);
25975
25976 input_location = saved_loc;
25977 }
25978
25979 /* Substitute ARGVEC into T, which is a list of initializers for
25980 either base class or a non-static data member. The TREE_PURPOSEs
25981 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25982 instantiate_decl. */
25983
25984 static tree
25985 tsubst_initializer_list (tree t, tree argvec)
25986 {
25987 tree inits = NULL_TREE;
25988 tree target_ctor = error_mark_node;
25989
25990 for (; t; t = TREE_CHAIN (t))
25991 {
25992 tree decl;
25993 tree init;
25994 tree expanded_bases = NULL_TREE;
25995 tree expanded_arguments = NULL_TREE;
25996 int i, len = 1;
25997
25998 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25999 {
26000 tree expr;
26001 tree arg;
26002
26003 /* Expand the base class expansion type into separate base
26004 classes. */
26005 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26006 tf_warning_or_error,
26007 NULL_TREE);
26008 if (expanded_bases == error_mark_node)
26009 continue;
26010
26011 /* We'll be building separate TREE_LISTs of arguments for
26012 each base. */
26013 len = TREE_VEC_LENGTH (expanded_bases);
26014 expanded_arguments = make_tree_vec (len);
26015 for (i = 0; i < len; i++)
26016 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26017
26018 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26019 expand each argument in the TREE_VALUE of t. */
26020 expr = make_node (EXPR_PACK_EXPANSION);
26021 PACK_EXPANSION_LOCAL_P (expr) = true;
26022 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26023 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26024
26025 if (TREE_VALUE (t) == void_type_node)
26026 /* VOID_TYPE_NODE is used to indicate
26027 value-initialization. */
26028 {
26029 for (i = 0; i < len; i++)
26030 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26031 }
26032 else
26033 {
26034 /* Substitute parameter packs into each argument in the
26035 TREE_LIST. */
26036 in_base_initializer = 1;
26037 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26038 {
26039 tree expanded_exprs;
26040
26041 /* Expand the argument. */
26042 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26043 expanded_exprs
26044 = tsubst_pack_expansion (expr, argvec,
26045 tf_warning_or_error,
26046 NULL_TREE);
26047 if (expanded_exprs == error_mark_node)
26048 continue;
26049
26050 /* Prepend each of the expanded expressions to the
26051 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26052 for (i = 0; i < len; i++)
26053 {
26054 TREE_VEC_ELT (expanded_arguments, i) =
26055 tree_cons (NULL_TREE,
26056 TREE_VEC_ELT (expanded_exprs, i),
26057 TREE_VEC_ELT (expanded_arguments, i));
26058 }
26059 }
26060 in_base_initializer = 0;
26061
26062 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26063 since we built them backwards. */
26064 for (i = 0; i < len; i++)
26065 {
26066 TREE_VEC_ELT (expanded_arguments, i) =
26067 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26068 }
26069 }
26070 }
26071
26072 for (i = 0; i < len; ++i)
26073 {
26074 if (expanded_bases)
26075 {
26076 decl = TREE_VEC_ELT (expanded_bases, i);
26077 decl = expand_member_init (decl);
26078 init = TREE_VEC_ELT (expanded_arguments, i);
26079 }
26080 else
26081 {
26082 tree tmp;
26083 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26084 tf_warning_or_error, NULL_TREE);
26085
26086 decl = expand_member_init (decl);
26087 if (decl && !DECL_P (decl))
26088 in_base_initializer = 1;
26089
26090 init = TREE_VALUE (t);
26091 tmp = init;
26092 if (init != void_type_node)
26093 init = tsubst_expr (init, argvec,
26094 tf_warning_or_error, NULL_TREE,
26095 /*integral_constant_expression_p=*/false);
26096 if (init == NULL_TREE && tmp != NULL_TREE)
26097 /* If we had an initializer but it instantiated to nothing,
26098 value-initialize the object. This will only occur when
26099 the initializer was a pack expansion where the parameter
26100 packs used in that expansion were of length zero. */
26101 init = void_type_node;
26102 in_base_initializer = 0;
26103 }
26104
26105 if (target_ctor != error_mark_node
26106 && init != error_mark_node)
26107 {
26108 error ("mem-initializer for %qD follows constructor delegation",
26109 decl);
26110 return inits;
26111 }
26112 /* Look for a target constructor. */
26113 if (init != error_mark_node
26114 && decl && CLASS_TYPE_P (decl)
26115 && same_type_p (decl, current_class_type))
26116 {
26117 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26118 if (inits)
26119 {
26120 error ("constructor delegation follows mem-initializer for %qD",
26121 TREE_PURPOSE (inits));
26122 continue;
26123 }
26124 target_ctor = init;
26125 }
26126
26127 if (decl)
26128 {
26129 init = build_tree_list (decl, init);
26130 /* Carry over the dummy TREE_TYPE node containing the source
26131 location. */
26132 TREE_TYPE (init) = TREE_TYPE (t);
26133 TREE_CHAIN (init) = inits;
26134 inits = init;
26135 }
26136 }
26137 }
26138 return inits;
26139 }
26140
26141 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26142
26143 static void
26144 set_current_access_from_decl (tree decl)
26145 {
26146 if (TREE_PRIVATE (decl))
26147 current_access_specifier = access_private_node;
26148 else if (TREE_PROTECTED (decl))
26149 current_access_specifier = access_protected_node;
26150 else
26151 current_access_specifier = access_public_node;
26152 }
26153
26154 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26155 is the instantiation (which should have been created with
26156 start_enum) and ARGS are the template arguments to use. */
26157
26158 static void
26159 tsubst_enum (tree tag, tree newtag, tree args)
26160 {
26161 tree e;
26162
26163 if (SCOPED_ENUM_P (newtag))
26164 begin_scope (sk_scoped_enum, newtag);
26165
26166 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26167 {
26168 tree value;
26169 tree decl;
26170
26171 decl = TREE_VALUE (e);
26172 /* Note that in a template enum, the TREE_VALUE is the
26173 CONST_DECL, not the corresponding INTEGER_CST. */
26174 value = tsubst_expr (DECL_INITIAL (decl),
26175 args, tf_warning_or_error, NULL_TREE,
26176 /*integral_constant_expression_p=*/true);
26177
26178 /* Give this enumeration constant the correct access. */
26179 set_current_access_from_decl (decl);
26180
26181 /* Actually build the enumerator itself. Here we're assuming that
26182 enumerators can't have dependent attributes. */
26183 build_enumerator (DECL_NAME (decl), value, newtag,
26184 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26185 }
26186
26187 if (SCOPED_ENUM_P (newtag))
26188 finish_scope ();
26189
26190 finish_enum_value_list (newtag);
26191 finish_enum (newtag);
26192
26193 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26194 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26195 }
26196
26197 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26198 its type -- but without substituting the innermost set of template
26199 arguments. So, innermost set of template parameters will appear in
26200 the type. */
26201
26202 tree
26203 get_mostly_instantiated_function_type (tree decl)
26204 {
26205 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26206 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26207 }
26208
26209 /* Return truthvalue if we're processing a template different from
26210 the last one involved in diagnostics. */
26211 bool
26212 problematic_instantiation_changed (void)
26213 {
26214 return current_tinst_level != last_error_tinst_level;
26215 }
26216
26217 /* Remember current template involved in diagnostics. */
26218 void
26219 record_last_problematic_instantiation (void)
26220 {
26221 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26222 }
26223
26224 struct tinst_level *
26225 current_instantiation (void)
26226 {
26227 return current_tinst_level;
26228 }
26229
26230 /* Return TRUE if current_function_decl is being instantiated, false
26231 otherwise. */
26232
26233 bool
26234 instantiating_current_function_p (void)
26235 {
26236 return (current_instantiation ()
26237 && (current_instantiation ()->maybe_get_node ()
26238 == current_function_decl));
26239 }
26240
26241 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26242 type. Return false for ok, true for disallowed. Issue error and
26243 inform messages under control of COMPLAIN. */
26244
26245 static bool
26246 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26247 {
26248 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26249 return false;
26250 else if (TYPE_PTR_P (type))
26251 return false;
26252 else if (TYPE_REF_P (type)
26253 && !TYPE_REF_IS_RVALUE (type))
26254 return false;
26255 else if (TYPE_PTRMEM_P (type))
26256 return false;
26257 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26258 {
26259 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26260 {
26261 if (complain & tf_error)
26262 error ("non-type template parameters of deduced class type only "
26263 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26264 return true;
26265 }
26266 return false;
26267 }
26268 else if (TREE_CODE (type) == TYPENAME_TYPE)
26269 return false;
26270 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26271 return false;
26272 else if (TREE_CODE (type) == NULLPTR_TYPE)
26273 return false;
26274 /* A bound template template parm could later be instantiated to have a valid
26275 nontype parm type via an alias template. */
26276 else if (cxx_dialect >= cxx11
26277 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26278 return false;
26279 else if (VOID_TYPE_P (type))
26280 /* Fall through. */;
26281 else if (cxx_dialect >= cxx20)
26282 {
26283 if (dependent_type_p (type))
26284 return false;
26285 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26286 return true;
26287 if (structural_type_p (type))
26288 return false;
26289 if (complain & tf_error)
26290 {
26291 auto_diagnostic_group d;
26292 error ("%qT is not a valid type for a template non-type "
26293 "parameter because it is not structural", type);
26294 structural_type_p (type, true);
26295 }
26296 return true;
26297 }
26298 else if (CLASS_TYPE_P (type))
26299 {
26300 if (complain & tf_error)
26301 error ("non-type template parameters of class type only available "
26302 "with %<-std=c++20%> or %<-std=gnu++20%>");
26303 return true;
26304 }
26305
26306 if (complain & tf_error)
26307 {
26308 if (type == error_mark_node)
26309 inform (input_location, "invalid template non-type parameter");
26310 else
26311 error ("%q#T is not a valid type for a template non-type parameter",
26312 type);
26313 }
26314 return true;
26315 }
26316
26317 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26318 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26319
26320 static bool
26321 dependent_type_p_r (tree type)
26322 {
26323 tree scope;
26324
26325 /* [temp.dep.type]
26326
26327 A type is dependent if it is:
26328
26329 -- a template parameter. Template template parameters are types
26330 for us (since TYPE_P holds true for them) so we handle
26331 them here. */
26332 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26333 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26334 return true;
26335 /* -- a qualified-id with a nested-name-specifier which contains a
26336 class-name that names a dependent type or whose unqualified-id
26337 names a dependent type. */
26338 if (TREE_CODE (type) == TYPENAME_TYPE)
26339 return true;
26340
26341 /* An alias template specialization can be dependent even if the
26342 resulting type is not. */
26343 if (dependent_alias_template_spec_p (type, nt_transparent))
26344 return true;
26345
26346 /* -- a cv-qualified type where the cv-unqualified type is
26347 dependent.
26348 No code is necessary for this bullet; the code below handles
26349 cv-qualified types, and we don't want to strip aliases with
26350 TYPE_MAIN_VARIANT because of DR 1558. */
26351 /* -- a compound type constructed from any dependent type. */
26352 if (TYPE_PTRMEM_P (type))
26353 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26354 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26355 (type)));
26356 else if (INDIRECT_TYPE_P (type))
26357 return dependent_type_p (TREE_TYPE (type));
26358 else if (FUNC_OR_METHOD_TYPE_P (type))
26359 {
26360 tree arg_type;
26361
26362 if (dependent_type_p (TREE_TYPE (type)))
26363 return true;
26364 for (arg_type = TYPE_ARG_TYPES (type);
26365 arg_type;
26366 arg_type = TREE_CHAIN (arg_type))
26367 if (dependent_type_p (TREE_VALUE (arg_type)))
26368 return true;
26369 if (cxx_dialect >= cxx17)
26370 /* A value-dependent noexcept-specifier makes the type dependent. */
26371 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26372 if (tree noex = TREE_PURPOSE (spec))
26373 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26374 affect overload resolution and treating it as dependent breaks
26375 things. Same for an unparsed noexcept expression. */
26376 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26377 && TREE_CODE (noex) != DEFERRED_PARSE
26378 && value_dependent_expression_p (noex))
26379 return true;
26380 return false;
26381 }
26382 /* -- an array type constructed from any dependent type or whose
26383 size is specified by a constant expression that is
26384 value-dependent.
26385
26386 We checked for type- and value-dependence of the bounds in
26387 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26388 if (TREE_CODE (type) == ARRAY_TYPE)
26389 {
26390 if (TYPE_DOMAIN (type)
26391 && dependent_type_p (TYPE_DOMAIN (type)))
26392 return true;
26393 return dependent_type_p (TREE_TYPE (type));
26394 }
26395
26396 /* -- a template-id in which either the template name is a template
26397 parameter ... */
26398 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26399 return true;
26400 /* ... or any of the template arguments is a dependent type or
26401 an expression that is type-dependent or value-dependent. */
26402 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26403 && (any_dependent_template_arguments_p
26404 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26405 return true;
26406
26407 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26408 dependent; if the argument of the `typeof' expression is not
26409 type-dependent, then it should already been have resolved. */
26410 if (TREE_CODE (type) == TYPEOF_TYPE
26411 || TREE_CODE (type) == DECLTYPE_TYPE
26412 || TREE_CODE (type) == UNDERLYING_TYPE)
26413 return true;
26414
26415 /* A template argument pack is dependent if any of its packed
26416 arguments are. */
26417 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26418 {
26419 tree args = ARGUMENT_PACK_ARGS (type);
26420 int i, len = TREE_VEC_LENGTH (args);
26421 for (i = 0; i < len; ++i)
26422 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26423 return true;
26424 }
26425
26426 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26427 be template parameters. */
26428 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26429 return true;
26430
26431 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26432 return true;
26433
26434 /* The standard does not specifically mention types that are local
26435 to template functions or local classes, but they should be
26436 considered dependent too. For example:
26437
26438 template <int I> void f() {
26439 enum E { a = I };
26440 S<sizeof (E)> s;
26441 }
26442
26443 The size of `E' cannot be known until the value of `I' has been
26444 determined. Therefore, `E' must be considered dependent. */
26445 scope = TYPE_CONTEXT (type);
26446 if (scope && TYPE_P (scope))
26447 return dependent_type_p (scope);
26448 /* Don't use type_dependent_expression_p here, as it can lead
26449 to infinite recursion trying to determine whether a lambda
26450 nested in a lambda is dependent (c++/47687). */
26451 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26452 && DECL_LANG_SPECIFIC (scope)
26453 && DECL_TEMPLATE_INFO (scope)
26454 && (any_dependent_template_arguments_p
26455 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26456 return true;
26457
26458 /* Other types are non-dependent. */
26459 return false;
26460 }
26461
26462 /* Returns TRUE if TYPE is dependent, in the sense of
26463 [temp.dep.type]. Note that a NULL type is considered dependent. */
26464
26465 bool
26466 dependent_type_p (tree type)
26467 {
26468 /* If there are no template parameters in scope, then there can't be
26469 any dependent types. */
26470 if (!processing_template_decl)
26471 {
26472 /* If we are not processing a template, then nobody should be
26473 providing us with a dependent type. */
26474 gcc_assert (type);
26475 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26476 return false;
26477 }
26478
26479 /* If the type is NULL, we have not computed a type for the entity
26480 in question; in that case, the type is dependent. */
26481 if (!type)
26482 return true;
26483
26484 /* Erroneous types can be considered non-dependent. */
26485 if (type == error_mark_node)
26486 return false;
26487
26488 /* Getting here with global_type_node means we improperly called this
26489 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26490 gcc_checking_assert (type != global_type_node);
26491
26492 /* If we have not already computed the appropriate value for TYPE,
26493 do so now. */
26494 if (!TYPE_DEPENDENT_P_VALID (type))
26495 {
26496 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26497 TYPE_DEPENDENT_P_VALID (type) = 1;
26498 }
26499
26500 return TYPE_DEPENDENT_P (type);
26501 }
26502
26503 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26504 lookup. In other words, a dependent type that is not the current
26505 instantiation. */
26506
26507 bool
26508 dependent_scope_p (tree scope)
26509 {
26510 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26511 && !currently_open_class (scope));
26512 }
26513
26514 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26515 an unknown base of 'this' (and is therefore instantiation-dependent). */
26516
26517 static bool
26518 unknown_base_ref_p (tree t)
26519 {
26520 if (!current_class_ptr)
26521 return false;
26522
26523 tree mem = TREE_OPERAND (t, 1);
26524 if (shared_member_p (mem))
26525 return false;
26526
26527 tree cur = current_nonlambda_class_type ();
26528 if (!any_dependent_bases_p (cur))
26529 return false;
26530
26531 tree ctx = TREE_OPERAND (t, 0);
26532 if (DERIVED_FROM_P (ctx, cur))
26533 return false;
26534
26535 return true;
26536 }
26537
26538 /* T is a SCOPE_REF; return whether we need to consider it
26539 instantiation-dependent so that we can check access at instantiation
26540 time even though we know which member it resolves to. */
26541
26542 static bool
26543 instantiation_dependent_scope_ref_p (tree t)
26544 {
26545 if (DECL_P (TREE_OPERAND (t, 1))
26546 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26547 && !unknown_base_ref_p (t)
26548 && accessible_in_template_p (TREE_OPERAND (t, 0),
26549 TREE_OPERAND (t, 1)))
26550 return false;
26551 else
26552 return true;
26553 }
26554
26555 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26556 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26557 expression. */
26558
26559 /* Note that this predicate is not appropriate for general expressions;
26560 only constant expressions (that satisfy potential_constant_expression)
26561 can be tested for value dependence. */
26562
26563 bool
26564 value_dependent_expression_p (tree expression)
26565 {
26566 if (!processing_template_decl || expression == NULL_TREE)
26567 return false;
26568
26569 /* A type-dependent expression is also value-dependent. */
26570 if (type_dependent_expression_p (expression))
26571 return true;
26572
26573 switch (TREE_CODE (expression))
26574 {
26575 case BASELINK:
26576 /* A dependent member function of the current instantiation. */
26577 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26578
26579 case FUNCTION_DECL:
26580 /* A dependent member function of the current instantiation. */
26581 if (DECL_CLASS_SCOPE_P (expression)
26582 && dependent_type_p (DECL_CONTEXT (expression)))
26583 return true;
26584 break;
26585
26586 case IDENTIFIER_NODE:
26587 /* A name that has not been looked up -- must be dependent. */
26588 return true;
26589
26590 case TEMPLATE_PARM_INDEX:
26591 /* A non-type template parm. */
26592 return true;
26593
26594 case CONST_DECL:
26595 /* A non-type template parm. */
26596 if (DECL_TEMPLATE_PARM_P (expression))
26597 return true;
26598 return value_dependent_expression_p (DECL_INITIAL (expression));
26599
26600 case VAR_DECL:
26601 /* A constant with literal type and is initialized
26602 with an expression that is value-dependent. */
26603 if (DECL_DEPENDENT_INIT_P (expression)
26604 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26605 || TYPE_REF_P (TREE_TYPE (expression)))
26606 return true;
26607 if (DECL_HAS_VALUE_EXPR_P (expression))
26608 {
26609 tree value_expr = DECL_VALUE_EXPR (expression);
26610 if (value_dependent_expression_p (value_expr)
26611 /* __PRETTY_FUNCTION__ inside a template function is dependent
26612 on the name of the function. */
26613 || (DECL_PRETTY_FUNCTION_P (expression)
26614 /* It might be used in a template, but not a template
26615 function, in which case its DECL_VALUE_EXPR will be
26616 "top level". */
26617 && value_expr == error_mark_node))
26618 return true;
26619 }
26620 return false;
26621
26622 case DYNAMIC_CAST_EXPR:
26623 case STATIC_CAST_EXPR:
26624 case CONST_CAST_EXPR:
26625 case REINTERPRET_CAST_EXPR:
26626 case CAST_EXPR:
26627 case IMPLICIT_CONV_EXPR:
26628 /* These expressions are value-dependent if the type to which
26629 the cast occurs is dependent or the expression being casted
26630 is value-dependent. */
26631 {
26632 tree type = TREE_TYPE (expression);
26633
26634 if (dependent_type_p (type))
26635 return true;
26636
26637 /* A functional cast has a list of operands. */
26638 expression = TREE_OPERAND (expression, 0);
26639 if (!expression)
26640 {
26641 /* If there are no operands, it must be an expression such
26642 as "int()". This should not happen for aggregate types
26643 because it would form non-constant expressions. */
26644 gcc_assert (cxx_dialect >= cxx11
26645 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26646
26647 return false;
26648 }
26649
26650 if (TREE_CODE (expression) == TREE_LIST)
26651 return any_value_dependent_elements_p (expression);
26652
26653 return value_dependent_expression_p (expression);
26654 }
26655
26656 case SIZEOF_EXPR:
26657 if (SIZEOF_EXPR_TYPE_P (expression))
26658 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26659 /* FALLTHRU */
26660 case ALIGNOF_EXPR:
26661 case TYPEID_EXPR:
26662 /* A `sizeof' expression is value-dependent if the operand is
26663 type-dependent or is a pack expansion. */
26664 expression = TREE_OPERAND (expression, 0);
26665 if (PACK_EXPANSION_P (expression))
26666 return true;
26667 else if (TYPE_P (expression))
26668 return dependent_type_p (expression);
26669 return instantiation_dependent_uneval_expression_p (expression);
26670
26671 case AT_ENCODE_EXPR:
26672 /* An 'encode' expression is value-dependent if the operand is
26673 type-dependent. */
26674 expression = TREE_OPERAND (expression, 0);
26675 return dependent_type_p (expression);
26676
26677 case NOEXCEPT_EXPR:
26678 expression = TREE_OPERAND (expression, 0);
26679 return instantiation_dependent_uneval_expression_p (expression);
26680
26681 case SCOPE_REF:
26682 /* All instantiation-dependent expressions should also be considered
26683 value-dependent. */
26684 return instantiation_dependent_scope_ref_p (expression);
26685
26686 case COMPONENT_REF:
26687 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26688 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26689
26690 case NONTYPE_ARGUMENT_PACK:
26691 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26692 is value-dependent. */
26693 {
26694 tree values = ARGUMENT_PACK_ARGS (expression);
26695 int i, len = TREE_VEC_LENGTH (values);
26696
26697 for (i = 0; i < len; ++i)
26698 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26699 return true;
26700
26701 return false;
26702 }
26703
26704 case TRAIT_EXPR:
26705 {
26706 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26707
26708 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26709 return true;
26710
26711 if (!type2)
26712 return false;
26713
26714 if (TREE_CODE (type2) != TREE_LIST)
26715 return dependent_type_p (type2);
26716
26717 for (; type2; type2 = TREE_CHAIN (type2))
26718 if (dependent_type_p (TREE_VALUE (type2)))
26719 return true;
26720
26721 return false;
26722 }
26723
26724 case MODOP_EXPR:
26725 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26726 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26727
26728 case ARRAY_REF:
26729 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26730 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26731
26732 case ADDR_EXPR:
26733 {
26734 tree op = TREE_OPERAND (expression, 0);
26735 return (value_dependent_expression_p (op)
26736 || has_value_dependent_address (op));
26737 }
26738
26739 case REQUIRES_EXPR:
26740 /* Treat all requires-expressions as value-dependent so
26741 we don't try to fold them. */
26742 return true;
26743
26744 case TYPE_REQ:
26745 return dependent_type_p (TREE_OPERAND (expression, 0));
26746
26747 case CALL_EXPR:
26748 {
26749 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26750 return true;
26751 tree fn = get_callee_fndecl (expression);
26752 int i, nargs;
26753 nargs = call_expr_nargs (expression);
26754 for (i = 0; i < nargs; ++i)
26755 {
26756 tree op = CALL_EXPR_ARG (expression, i);
26757 /* In a call to a constexpr member function, look through the
26758 implicit ADDR_EXPR on the object argument so that it doesn't
26759 cause the call to be considered value-dependent. We also
26760 look through it in potential_constant_expression. */
26761 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26762 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26763 && TREE_CODE (op) == ADDR_EXPR)
26764 op = TREE_OPERAND (op, 0);
26765 if (value_dependent_expression_p (op))
26766 return true;
26767 }
26768 return false;
26769 }
26770
26771 case TEMPLATE_ID_EXPR:
26772 return concept_definition_p (TREE_OPERAND (expression, 0));
26773
26774 case CONSTRUCTOR:
26775 {
26776 unsigned ix;
26777 tree val;
26778 if (dependent_type_p (TREE_TYPE (expression)))
26779 return true;
26780 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26781 if (value_dependent_expression_p (val))
26782 return true;
26783 return false;
26784 }
26785
26786 case STMT_EXPR:
26787 /* Treat a GNU statement expression as dependent to avoid crashing
26788 under instantiate_non_dependent_expr; it can't be constant. */
26789 return true;
26790
26791 default:
26792 /* A constant expression is value-dependent if any subexpression is
26793 value-dependent. */
26794 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26795 {
26796 case tcc_reference:
26797 case tcc_unary:
26798 case tcc_comparison:
26799 case tcc_binary:
26800 case tcc_expression:
26801 case tcc_vl_exp:
26802 {
26803 int i, len = cp_tree_operand_length (expression);
26804
26805 for (i = 0; i < len; i++)
26806 {
26807 tree t = TREE_OPERAND (expression, i);
26808
26809 /* In some cases, some of the operands may be missing.
26810 (For example, in the case of PREDECREMENT_EXPR, the
26811 amount to increment by may be missing.) That doesn't
26812 make the expression dependent. */
26813 if (t && value_dependent_expression_p (t))
26814 return true;
26815 }
26816 }
26817 break;
26818 default:
26819 break;
26820 }
26821 break;
26822 }
26823
26824 /* The expression is not value-dependent. */
26825 return false;
26826 }
26827
26828 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26829 [temp.dep.expr]. Note that an expression with no type is
26830 considered dependent. Other parts of the compiler arrange for an
26831 expression with type-dependent subexpressions to have no type, so
26832 this function doesn't have to be fully recursive. */
26833
26834 bool
26835 type_dependent_expression_p (tree expression)
26836 {
26837 if (!processing_template_decl)
26838 return false;
26839
26840 if (expression == NULL_TREE || expression == error_mark_node)
26841 return false;
26842
26843 STRIP_ANY_LOCATION_WRAPPER (expression);
26844
26845 /* An unresolved name is always dependent. */
26846 if (identifier_p (expression)
26847 || TREE_CODE (expression) == USING_DECL
26848 || TREE_CODE (expression) == WILDCARD_DECL)
26849 return true;
26850
26851 /* A lambda-expression in template context is dependent. dependent_type_p is
26852 true for a lambda in the scope of a class or function template, but that
26853 doesn't cover all template contexts, like a default template argument. */
26854 if (TREE_CODE (expression) == LAMBDA_EXPR)
26855 return true;
26856
26857 /* A fold expression is type-dependent. */
26858 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26859 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26860 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26861 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26862 return true;
26863
26864 /* Some expression forms are never type-dependent. */
26865 if (TREE_CODE (expression) == SIZEOF_EXPR
26866 || TREE_CODE (expression) == ALIGNOF_EXPR
26867 || TREE_CODE (expression) == AT_ENCODE_EXPR
26868 || TREE_CODE (expression) == NOEXCEPT_EXPR
26869 || TREE_CODE (expression) == TRAIT_EXPR
26870 || TREE_CODE (expression) == TYPEID_EXPR
26871 || TREE_CODE (expression) == DELETE_EXPR
26872 || TREE_CODE (expression) == VEC_DELETE_EXPR
26873 || TREE_CODE (expression) == THROW_EXPR
26874 || TREE_CODE (expression) == REQUIRES_EXPR)
26875 return false;
26876
26877 /* The types of these expressions depends only on the type to which
26878 the cast occurs. */
26879 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26880 || TREE_CODE (expression) == STATIC_CAST_EXPR
26881 || TREE_CODE (expression) == CONST_CAST_EXPR
26882 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26883 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26884 || TREE_CODE (expression) == CAST_EXPR)
26885 return dependent_type_p (TREE_TYPE (expression));
26886
26887 /* The types of these expressions depends only on the type created
26888 by the expression. */
26889 if (TREE_CODE (expression) == NEW_EXPR
26890 || TREE_CODE (expression) == VEC_NEW_EXPR)
26891 {
26892 /* For NEW_EXPR tree nodes created inside a template, either
26893 the object type itself or a TREE_LIST may appear as the
26894 operand 1. */
26895 tree type = TREE_OPERAND (expression, 1);
26896 if (TREE_CODE (type) == TREE_LIST)
26897 /* This is an array type. We need to check array dimensions
26898 as well. */
26899 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26900 || value_dependent_expression_p
26901 (TREE_OPERAND (TREE_VALUE (type), 1));
26902 /* Array type whose dimension has to be deduced. */
26903 else if (TREE_CODE (type) == ARRAY_TYPE
26904 && TREE_OPERAND (expression, 2) == NULL_TREE)
26905 return true;
26906 else
26907 return dependent_type_p (type);
26908 }
26909
26910 if (TREE_CODE (expression) == SCOPE_REF)
26911 {
26912 tree scope = TREE_OPERAND (expression, 0);
26913 tree name = TREE_OPERAND (expression, 1);
26914
26915 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26916 contains an identifier associated by name lookup with one or more
26917 declarations declared with a dependent type, or...a
26918 nested-name-specifier or qualified-id that names a member of an
26919 unknown specialization. */
26920 return (type_dependent_expression_p (name)
26921 || dependent_scope_p (scope));
26922 }
26923
26924 if (TREE_CODE (expression) == TEMPLATE_DECL
26925 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26926 return uses_outer_template_parms (expression);
26927
26928 if (TREE_CODE (expression) == STMT_EXPR)
26929 expression = stmt_expr_value_expr (expression);
26930
26931 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26932 {
26933 tree elt;
26934 unsigned i;
26935
26936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26937 {
26938 if (type_dependent_expression_p (elt))
26939 return true;
26940 }
26941 return false;
26942 }
26943
26944 /* A static data member of the current instantiation with incomplete
26945 array type is type-dependent, as the definition and specializations
26946 can have different bounds. */
26947 if (VAR_P (expression)
26948 && DECL_CLASS_SCOPE_P (expression)
26949 && dependent_type_p (DECL_CONTEXT (expression))
26950 && VAR_HAD_UNKNOWN_BOUND (expression))
26951 return true;
26952
26953 /* An array of unknown bound depending on a variadic parameter, eg:
26954
26955 template<typename... Args>
26956 void foo (Args... args)
26957 {
26958 int arr[] = { args... };
26959 }
26960
26961 template<int... vals>
26962 void bar ()
26963 {
26964 int arr[] = { vals... };
26965 }
26966
26967 If the array has no length and has an initializer, it must be that
26968 we couldn't determine its length in cp_complete_array_type because
26969 it is dependent. */
26970 if (VAR_P (expression)
26971 && TREE_TYPE (expression) != NULL_TREE
26972 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26973 && !TYPE_DOMAIN (TREE_TYPE (expression))
26974 && DECL_INITIAL (expression))
26975 return true;
26976
26977 /* A function or variable template-id is type-dependent if it has any
26978 dependent template arguments. */
26979 if (VAR_OR_FUNCTION_DECL_P (expression)
26980 && DECL_LANG_SPECIFIC (expression)
26981 && DECL_TEMPLATE_INFO (expression))
26982 {
26983 /* Consider the innermost template arguments, since those are the ones
26984 that come from the template-id; the template arguments for the
26985 enclosing class do not make it type-dependent unless they are used in
26986 the type of the decl. */
26987 if (instantiates_primary_template_p (expression)
26988 && (any_dependent_template_arguments_p
26989 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26990 return true;
26991 }
26992
26993 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26994 type-dependent. Checking this is important for functions with auto return
26995 type, which looks like a dependent type. */
26996 if (TREE_CODE (expression) == FUNCTION_DECL
26997 && !(DECL_CLASS_SCOPE_P (expression)
26998 && dependent_type_p (DECL_CONTEXT (expression)))
26999 && !(DECL_LANG_SPECIFIC (expression)
27000 && DECL_FRIEND_P (expression)
27001 && (!DECL_FRIEND_CONTEXT (expression)
27002 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27003 && !DECL_LOCAL_DECL_P (expression))
27004 {
27005 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27006 || undeduced_auto_decl (expression));
27007 return false;
27008 }
27009
27010 /* Always dependent, on the number of arguments if nothing else. */
27011 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27012 return true;
27013
27014 if (TREE_TYPE (expression) == unknown_type_node)
27015 {
27016 if (TREE_CODE (expression) == ADDR_EXPR)
27017 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27018 if (TREE_CODE (expression) == COMPONENT_REF
27019 || TREE_CODE (expression) == OFFSET_REF)
27020 {
27021 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27022 return true;
27023 expression = TREE_OPERAND (expression, 1);
27024 if (identifier_p (expression))
27025 return false;
27026 }
27027 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27028 if (TREE_CODE (expression) == SCOPE_REF)
27029 return false;
27030
27031 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27032 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27033 || TREE_CODE (expression) == CO_YIELD_EXPR)
27034 return true;
27035
27036 if (BASELINK_P (expression))
27037 {
27038 if (BASELINK_OPTYPE (expression)
27039 && dependent_type_p (BASELINK_OPTYPE (expression)))
27040 return true;
27041 expression = BASELINK_FUNCTIONS (expression);
27042 }
27043
27044 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27045 {
27046 if (any_dependent_template_arguments_p
27047 (TREE_OPERAND (expression, 1)))
27048 return true;
27049 expression = TREE_OPERAND (expression, 0);
27050 if (identifier_p (expression))
27051 return true;
27052 }
27053
27054 gcc_assert (OVL_P (expression));
27055
27056 for (lkp_iterator iter (expression); iter; ++iter)
27057 if (type_dependent_expression_p (*iter))
27058 return true;
27059
27060 return false;
27061 }
27062
27063 /* The type of a non-type template parm declared with a placeholder type
27064 depends on the corresponding template argument, even though
27065 placeholders are not normally considered dependent. */
27066 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27067 && is_auto (TREE_TYPE (expression)))
27068 return true;
27069
27070 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27071
27072 /* Dependent type attributes might not have made it from the decl to
27073 the type yet. */
27074 if (DECL_P (expression)
27075 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27076 return true;
27077
27078 return (dependent_type_p (TREE_TYPE (expression)));
27079 }
27080
27081 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27082 type-dependent if the expression refers to a member of the current
27083 instantiation and the type of the referenced member is dependent, or the
27084 class member access expression refers to a member of an unknown
27085 specialization.
27086
27087 This function returns true if the OBJECT in such a class member access
27088 expression is of an unknown specialization. */
27089
27090 bool
27091 type_dependent_object_expression_p (tree object)
27092 {
27093 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27094 dependent. */
27095 if (TREE_CODE (object) == IDENTIFIER_NODE)
27096 return true;
27097 tree scope = TREE_TYPE (object);
27098 return (!scope || dependent_scope_p (scope));
27099 }
27100
27101 /* walk_tree callback function for instantiation_dependent_expression_p,
27102 below. Returns non-zero if a dependent subexpression is found. */
27103
27104 static tree
27105 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27106 void * /*data*/)
27107 {
27108 if (TYPE_P (*tp))
27109 {
27110 /* We don't have to worry about decltype currently because decltype
27111 of an instantiation-dependent expr is a dependent type. This
27112 might change depending on the resolution of DR 1172. */
27113 *walk_subtrees = false;
27114 return NULL_TREE;
27115 }
27116 enum tree_code code = TREE_CODE (*tp);
27117 switch (code)
27118 {
27119 /* Don't treat an argument list as dependent just because it has no
27120 TREE_TYPE. */
27121 case TREE_LIST:
27122 case TREE_VEC:
27123 case NONTYPE_ARGUMENT_PACK:
27124 return NULL_TREE;
27125
27126 case TEMPLATE_PARM_INDEX:
27127 if (dependent_type_p (TREE_TYPE (*tp)))
27128 return *tp;
27129 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27130 return *tp;
27131 /* We'll check value-dependence separately. */
27132 return NULL_TREE;
27133
27134 /* Handle expressions with type operands. */
27135 case SIZEOF_EXPR:
27136 case ALIGNOF_EXPR:
27137 case TYPEID_EXPR:
27138 case AT_ENCODE_EXPR:
27139 {
27140 tree op = TREE_OPERAND (*tp, 0);
27141 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27142 op = TREE_TYPE (op);
27143 if (TYPE_P (op))
27144 {
27145 if (dependent_type_p (op))
27146 return *tp;
27147 else
27148 {
27149 *walk_subtrees = false;
27150 return NULL_TREE;
27151 }
27152 }
27153 break;
27154 }
27155
27156 case COMPONENT_REF:
27157 if (identifier_p (TREE_OPERAND (*tp, 1)))
27158 /* In a template, finish_class_member_access_expr creates a
27159 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27160 type-dependent, so that we can check access control at
27161 instantiation time (PR 42277). See also Core issue 1273. */
27162 return *tp;
27163 break;
27164
27165 case SCOPE_REF:
27166 if (instantiation_dependent_scope_ref_p (*tp))
27167 return *tp;
27168 else
27169 break;
27170
27171 /* Treat statement-expressions as dependent. */
27172 case BIND_EXPR:
27173 return *tp;
27174
27175 /* Treat requires-expressions as dependent. */
27176 case REQUIRES_EXPR:
27177 return *tp;
27178
27179 case CALL_EXPR:
27180 /* Treat concept checks as dependent. */
27181 if (concept_check_p (*tp))
27182 return *tp;
27183 break;
27184
27185 case TEMPLATE_ID_EXPR:
27186 /* Treat concept checks as dependent. */
27187 if (concept_check_p (*tp))
27188 return *tp;
27189 break;
27190
27191 case CONSTRUCTOR:
27192 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27193 return *tp;
27194 break;
27195
27196 default:
27197 break;
27198 }
27199
27200 if (type_dependent_expression_p (*tp))
27201 return *tp;
27202 else
27203 return NULL_TREE;
27204 }
27205
27206 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27207 sense defined by the ABI:
27208
27209 "An expression is instantiation-dependent if it is type-dependent
27210 or value-dependent, or it has a subexpression that is type-dependent
27211 or value-dependent."
27212
27213 Except don't actually check value-dependence for unevaluated expressions,
27214 because in sizeof(i) we don't care about the value of i. Checking
27215 type-dependence will in turn check value-dependence of array bounds/template
27216 arguments as needed. */
27217
27218 bool
27219 instantiation_dependent_uneval_expression_p (tree expression)
27220 {
27221 tree result;
27222
27223 if (!processing_template_decl)
27224 return false;
27225
27226 if (expression == error_mark_node)
27227 return false;
27228
27229 result = cp_walk_tree_without_duplicates (&expression,
27230 instantiation_dependent_r, NULL);
27231 return result != NULL_TREE;
27232 }
27233
27234 /* As above, but also check value-dependence of the expression as a whole. */
27235
27236 bool
27237 instantiation_dependent_expression_p (tree expression)
27238 {
27239 return (instantiation_dependent_uneval_expression_p (expression)
27240 || value_dependent_expression_p (expression));
27241 }
27242
27243 /* Like type_dependent_expression_p, but it also works while not processing
27244 a template definition, i.e. during substitution or mangling. */
27245
27246 bool
27247 type_dependent_expression_p_push (tree expr)
27248 {
27249 bool b;
27250 ++processing_template_decl;
27251 b = type_dependent_expression_p (expr);
27252 --processing_template_decl;
27253 return b;
27254 }
27255
27256 /* Returns TRUE if ARGS contains a type-dependent expression. */
27257
27258 bool
27259 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27260 {
27261 unsigned int i;
27262 tree arg;
27263
27264 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27265 {
27266 if (type_dependent_expression_p (arg))
27267 return true;
27268 }
27269 return false;
27270 }
27271
27272 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27273 expressions) contains any type-dependent expressions. */
27274
27275 bool
27276 any_type_dependent_elements_p (const_tree list)
27277 {
27278 for (; list; list = TREE_CHAIN (list))
27279 if (type_dependent_expression_p (TREE_VALUE (list)))
27280 return true;
27281
27282 return false;
27283 }
27284
27285 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27286 expressions) contains any value-dependent expressions. */
27287
27288 bool
27289 any_value_dependent_elements_p (const_tree list)
27290 {
27291 for (; list; list = TREE_CHAIN (list))
27292 if (value_dependent_expression_p (TREE_VALUE (list)))
27293 return true;
27294
27295 return false;
27296 }
27297
27298 /* Returns TRUE if the ARG (a template argument) is dependent. */
27299
27300 bool
27301 dependent_template_arg_p (tree arg)
27302 {
27303 if (!processing_template_decl)
27304 return false;
27305
27306 /* Assume a template argument that was wrongly written by the user
27307 is dependent. This is consistent with what
27308 any_dependent_template_arguments_p [that calls this function]
27309 does. */
27310 if (!arg || arg == error_mark_node)
27311 return true;
27312
27313 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27314 arg = argument_pack_select_arg (arg);
27315
27316 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27317 return true;
27318 if (TREE_CODE (arg) == TEMPLATE_DECL)
27319 {
27320 if (DECL_TEMPLATE_PARM_P (arg))
27321 return true;
27322 /* A member template of a dependent class is not necessarily
27323 type-dependent, but it is a dependent template argument because it
27324 will be a member of an unknown specialization to that template. */
27325 tree scope = CP_DECL_CONTEXT (arg);
27326 return TYPE_P (scope) && dependent_type_p (scope);
27327 }
27328 else if (ARGUMENT_PACK_P (arg))
27329 {
27330 tree args = ARGUMENT_PACK_ARGS (arg);
27331 int i, len = TREE_VEC_LENGTH (args);
27332 for (i = 0; i < len; ++i)
27333 {
27334 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27335 return true;
27336 }
27337
27338 return false;
27339 }
27340 else if (TYPE_P (arg))
27341 return dependent_type_p (arg);
27342 else
27343 return value_dependent_expression_p (arg);
27344 }
27345
27346 /* Returns true if ARGS (a collection of template arguments) contains
27347 any types that require structural equality testing. */
27348
27349 bool
27350 any_template_arguments_need_structural_equality_p (tree args)
27351 {
27352 int i;
27353 int j;
27354
27355 if (!args)
27356 return false;
27357 if (args == error_mark_node)
27358 return true;
27359
27360 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27361 {
27362 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27363 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27364 {
27365 tree arg = TREE_VEC_ELT (level, j);
27366 tree packed_args = NULL_TREE;
27367 int k, len = 1;
27368
27369 if (ARGUMENT_PACK_P (arg))
27370 {
27371 /* Look inside the argument pack. */
27372 packed_args = ARGUMENT_PACK_ARGS (arg);
27373 len = TREE_VEC_LENGTH (packed_args);
27374 }
27375
27376 for (k = 0; k < len; ++k)
27377 {
27378 if (packed_args)
27379 arg = TREE_VEC_ELT (packed_args, k);
27380
27381 if (error_operand_p (arg))
27382 return true;
27383 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27384 continue;
27385 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27386 return true;
27387 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27388 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27389 return true;
27390 }
27391 }
27392 }
27393
27394 return false;
27395 }
27396
27397 /* Returns true if ARGS (a collection of template arguments) contains
27398 any dependent arguments. */
27399
27400 bool
27401 any_dependent_template_arguments_p (const_tree args)
27402 {
27403 int i;
27404 int j;
27405
27406 if (!args)
27407 return false;
27408 if (args == error_mark_node)
27409 return true;
27410
27411 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27412 {
27413 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27414 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27415 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27416 return true;
27417 }
27418
27419 return false;
27420 }
27421
27422 /* Returns true if ARGS contains any errors. */
27423
27424 bool
27425 any_erroneous_template_args_p (const_tree args)
27426 {
27427 int i;
27428 int j;
27429
27430 if (args == error_mark_node)
27431 return true;
27432
27433 if (args && TREE_CODE (args) != TREE_VEC)
27434 {
27435 if (tree ti = get_template_info (args))
27436 args = TI_ARGS (ti);
27437 else
27438 args = NULL_TREE;
27439 }
27440
27441 if (!args)
27442 return false;
27443
27444 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27445 {
27446 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27447 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27448 if (error_operand_p (TREE_VEC_ELT (level, j)))
27449 return true;
27450 }
27451
27452 return false;
27453 }
27454
27455 /* Returns TRUE if the template TMPL is type-dependent. */
27456
27457 bool
27458 dependent_template_p (tree tmpl)
27459 {
27460 if (TREE_CODE (tmpl) == OVERLOAD)
27461 {
27462 for (lkp_iterator iter (tmpl); iter; ++iter)
27463 if (dependent_template_p (*iter))
27464 return true;
27465 return false;
27466 }
27467
27468 /* Template template parameters are dependent. */
27469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27470 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27471 return true;
27472 /* So are names that have not been looked up. */
27473 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27474 return true;
27475 return false;
27476 }
27477
27478 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27479
27480 bool
27481 dependent_template_id_p (tree tmpl, tree args)
27482 {
27483 return (dependent_template_p (tmpl)
27484 || any_dependent_template_arguments_p (args));
27485 }
27486
27487 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27488 are dependent. */
27489
27490 bool
27491 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27492 {
27493 int i;
27494
27495 if (!processing_template_decl)
27496 return false;
27497
27498 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27499 {
27500 tree decl = TREE_VEC_ELT (declv, i);
27501 tree init = TREE_VEC_ELT (initv, i);
27502 tree cond = TREE_VEC_ELT (condv, i);
27503 tree incr = TREE_VEC_ELT (incrv, i);
27504
27505 if (type_dependent_expression_p (decl)
27506 || TREE_CODE (decl) == SCOPE_REF)
27507 return true;
27508
27509 if (init && type_dependent_expression_p (init))
27510 return true;
27511
27512 if (cond == global_namespace)
27513 return true;
27514
27515 if (type_dependent_expression_p (cond))
27516 return true;
27517
27518 if (COMPARISON_CLASS_P (cond)
27519 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27520 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27521 return true;
27522
27523 if (TREE_CODE (incr) == MODOP_EXPR)
27524 {
27525 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27526 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27527 return true;
27528 }
27529 else if (type_dependent_expression_p (incr))
27530 return true;
27531 else if (TREE_CODE (incr) == MODIFY_EXPR)
27532 {
27533 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27534 return true;
27535 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27536 {
27537 tree t = TREE_OPERAND (incr, 1);
27538 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27539 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27540 return true;
27541
27542 /* If this loop has a class iterator with != comparison
27543 with increment other than i++/++i/i--/--i, make sure the
27544 increment is constant. */
27545 if (CLASS_TYPE_P (TREE_TYPE (decl))
27546 && TREE_CODE (cond) == NE_EXPR)
27547 {
27548 if (TREE_OPERAND (t, 0) == decl)
27549 t = TREE_OPERAND (t, 1);
27550 else
27551 t = TREE_OPERAND (t, 0);
27552 if (TREE_CODE (t) != INTEGER_CST)
27553 return true;
27554 }
27555 }
27556 }
27557 }
27558
27559 return false;
27560 }
27561
27562 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27563 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27564 no such TYPE can be found. Note that this function peers inside
27565 uninstantiated templates and therefore should be used only in
27566 extremely limited situations. ONLY_CURRENT_P restricts this
27567 peering to the currently open classes hierarchy (which is required
27568 when comparing types). */
27569
27570 tree
27571 resolve_typename_type (tree type, bool only_current_p)
27572 {
27573 tree scope;
27574 tree name;
27575 tree decl;
27576 int quals;
27577 tree pushed_scope;
27578 tree result;
27579
27580 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27581
27582 scope = TYPE_CONTEXT (type);
27583 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27584 gcc_checking_assert (uses_template_parms (scope));
27585
27586 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27587 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27588 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27589 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27590 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27591 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27592 the TYPENAME_TYPE instead, we avoid messing up with a possible
27593 typedef variant case. */
27594 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27595
27596 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27597 it first before we can figure out what NAME refers to. */
27598 if (TREE_CODE (scope) == TYPENAME_TYPE)
27599 {
27600 if (TYPENAME_IS_RESOLVING_P (scope))
27601 /* Given a class template A with a dependent base with nested type C,
27602 typedef typename A::C::C C will land us here, as trying to resolve
27603 the initial A::C leads to the local C typedef, which leads back to
27604 A::C::C. So we break the recursion now. */
27605 return type;
27606 else
27607 scope = resolve_typename_type (scope, only_current_p);
27608 }
27609 /* If we don't know what SCOPE refers to, then we cannot resolve the
27610 TYPENAME_TYPE. */
27611 if (!CLASS_TYPE_P (scope))
27612 return type;
27613 /* If this is a typedef, we don't want to look inside (c++/11987). */
27614 if (typedef_variant_p (type))
27615 return type;
27616 /* If SCOPE isn't the template itself, it will not have a valid
27617 TYPE_FIELDS list. */
27618 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27619 /* scope is either the template itself or a compatible instantiation
27620 like X<T>, so look up the name in the original template. */
27621 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27622 /* If scope has no fields, it can't be a current instantiation. Check this
27623 before currently_open_class to avoid infinite recursion (71515). */
27624 if (!TYPE_FIELDS (scope))
27625 return type;
27626 /* If the SCOPE is not the current instantiation, there's no reason
27627 to look inside it. */
27628 if (only_current_p && !currently_open_class (scope))
27629 return type;
27630 /* Enter the SCOPE so that name lookup will be resolved as if we
27631 were in the class definition. In particular, SCOPE will no
27632 longer be considered a dependent type. */
27633 pushed_scope = push_scope (scope);
27634 /* Look up the declaration. */
27635 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27636 tf_warning_or_error);
27637
27638 result = NULL_TREE;
27639
27640 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27641 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27642 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27643 if (!decl)
27644 /*nop*/;
27645 else if (identifier_p (fullname)
27646 && TREE_CODE (decl) == TYPE_DECL)
27647 {
27648 result = TREE_TYPE (decl);
27649 if (result == error_mark_node)
27650 result = NULL_TREE;
27651 }
27652 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27653 && DECL_CLASS_TEMPLATE_P (decl))
27654 {
27655 /* Obtain the template and the arguments. */
27656 tree tmpl = TREE_OPERAND (fullname, 0);
27657 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27658 {
27659 /* We get here with a plain identifier because a previous tentative
27660 parse of the nested-name-specifier as part of a ptr-operator saw
27661 ::template X<A>. The use of ::template is necessary in a
27662 ptr-operator, but wrong in a declarator-id.
27663
27664 [temp.names]: In a qualified-id of a declarator-id, the keyword
27665 template shall not appear at the top level. */
27666 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27667 "keyword %<template%> not allowed in declarator-id");
27668 tmpl = decl;
27669 }
27670 tree args = TREE_OPERAND (fullname, 1);
27671 /* Instantiate the template. */
27672 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27673 /*entering_scope=*/true,
27674 tf_error | tf_user);
27675 if (result == error_mark_node)
27676 result = NULL_TREE;
27677 }
27678
27679 /* Leave the SCOPE. */
27680 if (pushed_scope)
27681 pop_scope (pushed_scope);
27682
27683 /* If we failed to resolve it, return the original typename. */
27684 if (!result)
27685 return type;
27686
27687 /* If lookup found a typename type, resolve that too. */
27688 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27689 {
27690 /* Ill-formed programs can cause infinite recursion here, so we
27691 must catch that. */
27692 TYPENAME_IS_RESOLVING_P (result) = 1;
27693 result = resolve_typename_type (result, only_current_p);
27694 TYPENAME_IS_RESOLVING_P (result) = 0;
27695 }
27696
27697 /* Qualify the resulting type. */
27698 quals = cp_type_quals (type);
27699 if (quals)
27700 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27701
27702 return result;
27703 }
27704
27705 /* EXPR is an expression which is not type-dependent. Return a proxy
27706 for EXPR that can be used to compute the types of larger
27707 expressions containing EXPR. */
27708
27709 tree
27710 build_non_dependent_expr (tree expr)
27711 {
27712 tree orig_expr = expr;
27713 tree inner_expr;
27714
27715 /* When checking, try to get a constant value for all non-dependent
27716 expressions in order to expose bugs in *_dependent_expression_p
27717 and constexpr. This can affect code generation, see PR70704, so
27718 only do this for -fchecking=2. */
27719 if (flag_checking > 1
27720 && cxx_dialect >= cxx11
27721 /* Don't do this during nsdmi parsing as it can lead to
27722 unexpected recursive instantiations. */
27723 && !parsing_nsdmi ()
27724 /* Don't do this during concept processing either and for
27725 the same reason. */
27726 && !processing_constraint_expression_p ())
27727 fold_non_dependent_expr (expr, tf_none);
27728
27729 STRIP_ANY_LOCATION_WRAPPER (expr);
27730
27731 /* Preserve OVERLOADs; the functions must be available to resolve
27732 types. */
27733 inner_expr = expr;
27734 if (TREE_CODE (inner_expr) == STMT_EXPR)
27735 inner_expr = stmt_expr_value_expr (inner_expr);
27736 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27737 inner_expr = TREE_OPERAND (inner_expr, 0);
27738 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27739 inner_expr = TREE_OPERAND (inner_expr, 1);
27740 if (is_overloaded_fn (inner_expr)
27741 || TREE_CODE (inner_expr) == OFFSET_REF)
27742 return orig_expr;
27743 /* There is no need to return a proxy for a variable or enumerator. */
27744 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27745 return orig_expr;
27746 /* Preserve string constants; conversions from string constants to
27747 "char *" are allowed, even though normally a "const char *"
27748 cannot be used to initialize a "char *". */
27749 if (TREE_CODE (expr) == STRING_CST)
27750 return orig_expr;
27751 /* Preserve void and arithmetic constants, as an optimization -- there is no
27752 reason to create a new node. */
27753 if (TREE_CODE (expr) == VOID_CST
27754 || TREE_CODE (expr) == INTEGER_CST
27755 || TREE_CODE (expr) == REAL_CST)
27756 return orig_expr;
27757 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27758 There is at least one place where we want to know that a
27759 particular expression is a throw-expression: when checking a ?:
27760 expression, there are special rules if the second or third
27761 argument is a throw-expression. */
27762 if (TREE_CODE (expr) == THROW_EXPR)
27763 return orig_expr;
27764
27765 /* Don't wrap an initializer list, we need to be able to look inside. */
27766 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27767 return orig_expr;
27768
27769 /* Don't wrap a dummy object, we need to be able to test for it. */
27770 if (is_dummy_object (expr))
27771 return orig_expr;
27772
27773 if (TREE_CODE (expr) == COND_EXPR)
27774 return build3 (COND_EXPR,
27775 TREE_TYPE (expr),
27776 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27777 (TREE_OPERAND (expr, 1)
27778 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27779 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27780 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27781 if (TREE_CODE (expr) == COMPOUND_EXPR
27782 && !COMPOUND_EXPR_OVERLOADED (expr))
27783 return build2 (COMPOUND_EXPR,
27784 TREE_TYPE (expr),
27785 TREE_OPERAND (expr, 0),
27786 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27787
27788 /* If the type is unknown, it can't really be non-dependent */
27789 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27790
27791 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27792 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27793 TREE_TYPE (expr), expr);
27794 }
27795
27796 /* ARGS is a vector of expressions as arguments to a function call.
27797 Replace the arguments with equivalent non-dependent expressions.
27798 This modifies ARGS in place. */
27799
27800 void
27801 make_args_non_dependent (vec<tree, va_gc> *args)
27802 {
27803 unsigned int ix;
27804 tree arg;
27805
27806 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27807 {
27808 tree newarg = build_non_dependent_expr (arg);
27809 if (newarg != arg)
27810 (*args)[ix] = newarg;
27811 }
27812 }
27813
27814 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27815 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27816 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27817
27818 static tree
27819 make_auto_1 (tree name, bool set_canonical)
27820 {
27821 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27822 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27823 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27824 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27825 (0, processing_template_decl + 1, processing_template_decl + 1,
27826 TYPE_NAME (au), NULL_TREE);
27827 if (set_canonical)
27828 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27829 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27830 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27831 if (name == decltype_auto_identifier)
27832 AUTO_IS_DECLTYPE (au) = true;
27833
27834 return au;
27835 }
27836
27837 tree
27838 make_decltype_auto (void)
27839 {
27840 return make_auto_1 (decltype_auto_identifier, true);
27841 }
27842
27843 tree
27844 make_auto (void)
27845 {
27846 return make_auto_1 (auto_identifier, true);
27847 }
27848
27849 /* Return a C++17 deduction placeholder for class template TMPL. */
27850
27851 tree
27852 make_template_placeholder (tree tmpl)
27853 {
27854 tree t = make_auto_1 (auto_identifier, false);
27855 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27856 /* Our canonical type depends on the placeholder. */
27857 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27858 return t;
27859 }
27860
27861 /* True iff T is a C++17 class template deduction placeholder. */
27862
27863 bool
27864 template_placeholder_p (tree t)
27865 {
27866 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27867 }
27868
27869 /* Make a "constrained auto" type-specifier. This is an auto or
27870 decltype(auto) type with constraints that must be associated after
27871 deduction. The constraint is formed from the given concept CON
27872 and its optional sequence of template arguments ARGS.
27873
27874 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27875
27876 static tree
27877 make_constrained_placeholder_type (tree type, tree con, tree args)
27878 {
27879 /* Build the constraint. */
27880 tree tmpl = DECL_TI_TEMPLATE (con);
27881 tree expr = tmpl;
27882 if (TREE_CODE (con) == FUNCTION_DECL)
27883 expr = ovl_make (tmpl);
27884 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27885
27886 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27887
27888 /* Our canonical type depends on the constraint. */
27889 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27890
27891 /* Attach the constraint to the type declaration. */
27892 return TYPE_NAME (type);
27893 }
27894
27895 /* Make a "constrained auto" type-specifier. */
27896
27897 tree
27898 make_constrained_auto (tree con, tree args)
27899 {
27900 tree type = make_auto_1 (auto_identifier, false);
27901 return make_constrained_placeholder_type (type, con, args);
27902 }
27903
27904 /* Make a "constrained decltype(auto)" type-specifier. */
27905
27906 tree
27907 make_constrained_decltype_auto (tree con, tree args)
27908 {
27909 tree type = make_auto_1 (decltype_auto_identifier, false);
27910 return make_constrained_placeholder_type (type, con, args);
27911 }
27912
27913 /* Build and return a concept definition. Like other templates, the
27914 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27915 the TEMPLATE_DECL. */
27916
27917 tree
27918 finish_concept_definition (cp_expr id, tree init)
27919 {
27920 gcc_assert (identifier_p (id));
27921 gcc_assert (processing_template_decl);
27922
27923 location_t loc = id.get_location();
27924
27925 /* A concept-definition shall not have associated constraints. */
27926 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27927 {
27928 error_at (loc, "a concept cannot be constrained");
27929 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27930 }
27931
27932 /* A concept-definition shall appear in namespace scope. Templates
27933 aren't allowed in block scope, so we only need to check for class
27934 scope. */
27935 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27936 {
27937 error_at (loc, "concept %qE not in namespace scope", *id);
27938 return error_mark_node;
27939 }
27940
27941 /* Initially build the concept declaration; its type is bool. */
27942 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27943 DECL_CONTEXT (decl) = current_scope ();
27944 DECL_INITIAL (decl) = init;
27945
27946 /* Push the enclosing template. */
27947 return push_template_decl (decl);
27948 }
27949
27950 /* Given type ARG, return std::initializer_list<ARG>. */
27951
27952 static tree
27953 listify (tree arg)
27954 {
27955 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27956
27957 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27958 {
27959 gcc_rich_location richloc (input_location);
27960 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27961 error_at (&richloc,
27962 "deducing from brace-enclosed initializer list"
27963 " requires %<#include <initializer_list>%>");
27964
27965 return error_mark_node;
27966 }
27967 tree argvec = make_tree_vec (1);
27968 TREE_VEC_ELT (argvec, 0) = arg;
27969
27970 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27971 NULL_TREE, 0, tf_warning_or_error);
27972 }
27973
27974 /* Replace auto in TYPE with std::initializer_list<auto>. */
27975
27976 static tree
27977 listify_autos (tree type, tree auto_node)
27978 {
27979 tree init_auto = listify (strip_top_quals (auto_node));
27980 tree argvec = make_tree_vec (1);
27981 TREE_VEC_ELT (argvec, 0) = init_auto;
27982 if (processing_template_decl)
27983 argvec = add_to_template_args (current_template_args (), argvec);
27984 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27985 }
27986
27987 /* Hash traits for hashing possibly constrained 'auto'
27988 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27989
27990 struct auto_hash : default_hash_traits<tree>
27991 {
27992 static inline hashval_t hash (tree);
27993 static inline bool equal (tree, tree);
27994 };
27995
27996 /* Hash the 'auto' T. */
27997
27998 inline hashval_t
27999 auto_hash::hash (tree t)
28000 {
28001 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28002 /* Matching constrained-type-specifiers denote the same template
28003 parameter, so hash the constraint. */
28004 return hash_placeholder_constraint (c);
28005 else
28006 /* But unconstrained autos are all separate, so just hash the pointer. */
28007 return iterative_hash_object (t, 0);
28008 }
28009
28010 /* Compare two 'auto's. */
28011
28012 inline bool
28013 auto_hash::equal (tree t1, tree t2)
28014 {
28015 if (t1 == t2)
28016 return true;
28017
28018 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28019 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28020
28021 /* Two unconstrained autos are distinct. */
28022 if (!c1 || !c2)
28023 return false;
28024
28025 return equivalent_placeholder_constraints (c1, c2);
28026 }
28027
28028 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28029 constrained) auto, add it to the vector. */
28030
28031 static int
28032 extract_autos_r (tree t, void *data)
28033 {
28034 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28035 if (is_auto (t))
28036 {
28037 /* All the autos were built with index 0; fix that up now. */
28038 tree *p = hash.find_slot (t, INSERT);
28039 unsigned idx;
28040 if (*p)
28041 /* If this is a repeated constrained-type-specifier, use the index we
28042 chose before. */
28043 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28044 else
28045 {
28046 /* Otherwise this is new, so use the current count. */
28047 *p = t;
28048 idx = hash.elements () - 1;
28049 }
28050 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28051 }
28052
28053 /* Always keep walking. */
28054 return 0;
28055 }
28056
28057 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28058 says they can appear anywhere in the type. */
28059
28060 static tree
28061 extract_autos (tree type)
28062 {
28063 hash_set<tree> visited;
28064 hash_table<auto_hash> hash (2);
28065
28066 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28067
28068 tree tree_vec = make_tree_vec (hash.elements());
28069 for (hash_table<auto_hash>::iterator iter = hash.begin();
28070 iter != hash.end(); ++iter)
28071 {
28072 tree elt = *iter;
28073 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28074 TREE_VEC_ELT (tree_vec, i)
28075 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28076 }
28077
28078 return tree_vec;
28079 }
28080
28081 /* The stem for deduction guide names. */
28082 const char *const dguide_base = "__dguide_";
28083
28084 /* Return the name for a deduction guide for class template TMPL. */
28085
28086 tree
28087 dguide_name (tree tmpl)
28088 {
28089 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28090 tree tname = TYPE_IDENTIFIER (type);
28091 char *buf = (char *) alloca (1 + strlen (dguide_base)
28092 + IDENTIFIER_LENGTH (tname));
28093 memcpy (buf, dguide_base, strlen (dguide_base));
28094 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28095 IDENTIFIER_LENGTH (tname) + 1);
28096 tree dname = get_identifier (buf);
28097 TREE_TYPE (dname) = type;
28098 return dname;
28099 }
28100
28101 /* True if NAME is the name of a deduction guide. */
28102
28103 bool
28104 dguide_name_p (tree name)
28105 {
28106 return (TREE_CODE (name) == IDENTIFIER_NODE
28107 && TREE_TYPE (name)
28108 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28109 strlen (dguide_base)));
28110 }
28111
28112 /* True if FN is a deduction guide. */
28113
28114 bool
28115 deduction_guide_p (const_tree fn)
28116 {
28117 if (DECL_P (fn))
28118 if (tree name = DECL_NAME (fn))
28119 return dguide_name_p (name);
28120 return false;
28121 }
28122
28123 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28124
28125 bool
28126 copy_guide_p (const_tree fn)
28127 {
28128 gcc_assert (deduction_guide_p (fn));
28129 if (!DECL_ARTIFICIAL (fn))
28130 return false;
28131 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28132 return (TREE_CHAIN (parms) == void_list_node
28133 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28134 }
28135
28136 /* True if FN is a guide generated from a constructor template. */
28137
28138 bool
28139 template_guide_p (const_tree fn)
28140 {
28141 gcc_assert (deduction_guide_p (fn));
28142 if (!DECL_ARTIFICIAL (fn))
28143 return false;
28144 tree tmpl = DECL_TI_TEMPLATE (fn);
28145 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28146 return PRIMARY_TEMPLATE_P (org);
28147 return false;
28148 }
28149
28150 /* True if FN is an aggregate initialization guide or the copy deduction
28151 guide. */
28152
28153 bool
28154 builtin_guide_p (const_tree fn)
28155 {
28156 if (!deduction_guide_p (fn))
28157 return false;
28158 if (!DECL_ARTIFICIAL (fn))
28159 /* Explicitly declared. */
28160 return false;
28161 if (DECL_ABSTRACT_ORIGIN (fn))
28162 /* Derived from a constructor. */
28163 return false;
28164 return true;
28165 }
28166
28167 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28168 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28169 template parameter types. Note that the handling of template template
28170 parameters relies on current_template_parms being set appropriately for the
28171 new template. */
28172
28173 static tree
28174 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28175 tree tsubst_args, tsubst_flags_t complain)
28176 {
28177 if (olddecl == error_mark_node)
28178 return error_mark_node;
28179
28180 tree oldidx = get_template_parm_index (olddecl);
28181
28182 tree newtype;
28183 if (TREE_CODE (olddecl) == TYPE_DECL
28184 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28185 {
28186 tree oldtype = TREE_TYPE (olddecl);
28187 newtype = cxx_make_type (TREE_CODE (oldtype));
28188 TYPE_MAIN_VARIANT (newtype) = newtype;
28189 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28190 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28191 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28192 }
28193 else
28194 {
28195 newtype = TREE_TYPE (olddecl);
28196 if (type_uses_auto (newtype))
28197 {
28198 // Substitute once to fix references to other template parameters.
28199 newtype = tsubst (newtype, tsubst_args,
28200 complain|tf_partial, NULL_TREE);
28201 // Now substitute again to reduce the level of the auto.
28202 newtype = tsubst (newtype, current_template_args (),
28203 complain, NULL_TREE);
28204 }
28205 else
28206 newtype = tsubst (newtype, tsubst_args,
28207 complain, NULL_TREE);
28208 }
28209
28210 tree newdecl
28211 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28212 DECL_NAME (olddecl), newtype);
28213 SET_DECL_TEMPLATE_PARM_P (newdecl);
28214
28215 tree newidx;
28216 if (TREE_CODE (olddecl) == TYPE_DECL
28217 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28218 {
28219 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28220 = build_template_parm_index (index, level, level,
28221 newdecl, newtype);
28222 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28223 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28224 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28225 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28226 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28227 else
28228 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28229
28230 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28231 {
28232 DECL_TEMPLATE_RESULT (newdecl)
28233 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28234 DECL_NAME (olddecl), newtype);
28235 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28236 // First create a copy (ttargs) of tsubst_args with an
28237 // additional level for the template template parameter's own
28238 // template parameters (ttparms).
28239 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28240 (DECL_TEMPLATE_PARMS (olddecl)));
28241 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28242 tree ttargs = make_tree_vec (depth + 1);
28243 for (int i = 0; i < depth; ++i)
28244 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28245 TREE_VEC_ELT (ttargs, depth)
28246 = template_parms_level_to_args (ttparms);
28247 // Substitute ttargs into ttparms to fix references to
28248 // other template parameters.
28249 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28250 complain|tf_partial);
28251 // Now substitute again with args based on tparms, to reduce
28252 // the level of the ttparms.
28253 ttargs = current_template_args ();
28254 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28255 complain);
28256 // Finally, tack the adjusted parms onto tparms.
28257 ttparms = tree_cons (size_int (depth), ttparms,
28258 current_template_parms);
28259 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28260 }
28261 }
28262 else
28263 {
28264 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28265 tree newconst
28266 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28267 TREE_CODE (oldconst),
28268 DECL_NAME (oldconst), newtype);
28269 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28270 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28271 SET_DECL_TEMPLATE_PARM_P (newconst);
28272 newidx = build_template_parm_index (index, level, level,
28273 newconst, newtype);
28274 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28275 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28276 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28277 }
28278
28279 return newdecl;
28280 }
28281
28282 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28283 template parameter. */
28284
28285 static tree
28286 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28287 tree targs, unsigned targs_index, tsubst_flags_t complain)
28288 {
28289 tree olddecl = TREE_VALUE (oldelt);
28290 tree newdecl = rewrite_template_parm (olddecl, index, level,
28291 targs, complain);
28292 if (newdecl == error_mark_node)
28293 return error_mark_node;
28294 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28295 targs, complain, NULL_TREE);
28296 tree list = build_tree_list (newdef, newdecl);
28297 TEMPLATE_PARM_CONSTRAINTS (list)
28298 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28299 targs, complain, NULL_TREE);
28300 int depth = TMPL_ARGS_DEPTH (targs);
28301 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28302 return list;
28303 }
28304
28305 /* Returns a C++17 class deduction guide template based on the constructor
28306 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28307 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28308 aggregate initialization guide. */
28309
28310 static tree
28311 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28312 {
28313 tree tparms, targs, fparms, fargs, ci;
28314 bool memtmpl = false;
28315 bool explicit_p;
28316 location_t loc;
28317 tree fn_tmpl = NULL_TREE;
28318
28319 if (outer_args)
28320 {
28321 ++processing_template_decl;
28322 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28323 --processing_template_decl;
28324 }
28325
28326 if (!DECL_DECLARES_FUNCTION_P (ctor))
28327 {
28328 if (TYPE_P (ctor))
28329 {
28330 bool copy_p = TYPE_REF_P (ctor);
28331 if (copy_p)
28332 fparms = tree_cons (NULL_TREE, type, void_list_node);
28333 else
28334 fparms = void_list_node;
28335 }
28336 else if (TREE_CODE (ctor) == TREE_LIST)
28337 fparms = ctor;
28338 else
28339 gcc_unreachable ();
28340
28341 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28342 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28343 targs = CLASSTYPE_TI_ARGS (type);
28344 ci = NULL_TREE;
28345 fargs = NULL_TREE;
28346 loc = DECL_SOURCE_LOCATION (ctmpl);
28347 explicit_p = false;
28348 }
28349 else
28350 {
28351 ++processing_template_decl;
28352 bool ok = true;
28353
28354 fn_tmpl
28355 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28356 : DECL_TI_TEMPLATE (ctor));
28357 if (outer_args)
28358 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28359 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28360
28361 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28362 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28363 fully specialized args for the enclosing class. Strip those off, as
28364 the deduction guide won't have those template parameters. */
28365 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28366 TMPL_PARMS_DEPTH (tparms));
28367 /* Discard the 'this' parameter. */
28368 fparms = FUNCTION_ARG_CHAIN (ctor);
28369 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28370 ci = get_constraints (ctor);
28371 loc = DECL_SOURCE_LOCATION (ctor);
28372 explicit_p = DECL_NONCONVERTING_P (ctor);
28373
28374 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28375 {
28376 memtmpl = true;
28377
28378 /* For a member template constructor, we need to flatten the two
28379 template parameter lists into one, and then adjust the function
28380 signature accordingly. This gets...complicated. */
28381 tree save_parms = current_template_parms;
28382
28383 /* For a member template we should have two levels of parms/args, one
28384 for the class and one for the constructor. We stripped
28385 specialized args for further enclosing classes above. */
28386 const int depth = 2;
28387 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28388
28389 /* Template args for translating references to the two-level template
28390 parameters into references to the one-level template parameters we
28391 are creating. */
28392 tree tsubst_args = copy_node (targs);
28393 TMPL_ARGS_LEVEL (tsubst_args, depth)
28394 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28395
28396 /* Template parms for the constructor template. */
28397 tree ftparms = TREE_VALUE (tparms);
28398 unsigned flen = TREE_VEC_LENGTH (ftparms);
28399 /* Template parms for the class template. */
28400 tparms = TREE_CHAIN (tparms);
28401 tree ctparms = TREE_VALUE (tparms);
28402 unsigned clen = TREE_VEC_LENGTH (ctparms);
28403 /* Template parms for the deduction guide start as a copy of the
28404 template parms for the class. We set current_template_parms for
28405 lookup_template_class_1. */
28406 current_template_parms = tparms = copy_node (tparms);
28407 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28408 for (unsigned i = 0; i < clen; ++i)
28409 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28410
28411 /* Now we need to rewrite the constructor parms to append them to the
28412 class parms. */
28413 for (unsigned i = 0; i < flen; ++i)
28414 {
28415 unsigned index = i + clen;
28416 unsigned level = 1;
28417 tree oldelt = TREE_VEC_ELT (ftparms, i);
28418 tree newelt
28419 = rewrite_tparm_list (oldelt, index, level,
28420 tsubst_args, i, complain);
28421 if (newelt == error_mark_node)
28422 ok = false;
28423 TREE_VEC_ELT (new_vec, index) = newelt;
28424 }
28425
28426 /* Now we have a final set of template parms to substitute into the
28427 function signature. */
28428 targs = template_parms_to_args (tparms);
28429 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28430 complain, ctor);
28431 if (fparms == error_mark_node)
28432 ok = false;
28433 if (ci)
28434 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28435
28436 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28437 cp_unevaluated_operand. */
28438 cp_evaluated ev;
28439 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28440 current_template_parms = save_parms;
28441 }
28442 else
28443 {
28444 /* Substitute in the same arguments to rewrite class members into
28445 references to members of an unknown specialization. */
28446 cp_evaluated ev;
28447 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28448 fargs = tsubst (fargs, targs, complain, ctor);
28449 if (ci)
28450 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28451 }
28452
28453 --processing_template_decl;
28454 if (!ok)
28455 return error_mark_node;
28456 }
28457
28458 if (!memtmpl)
28459 {
28460 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28461 tparms = copy_node (tparms);
28462 INNERMOST_TEMPLATE_PARMS (tparms)
28463 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28464 }
28465
28466 tree fntype = build_function_type (type, fparms);
28467 tree ded_fn = build_lang_decl_loc (loc,
28468 FUNCTION_DECL,
28469 dguide_name (type), fntype);
28470 DECL_ARGUMENTS (ded_fn) = fargs;
28471 DECL_ARTIFICIAL (ded_fn) = true;
28472 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28473 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28474 DECL_ARTIFICIAL (ded_tmpl) = true;
28475 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28476 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28477 if (DECL_P (ctor))
28478 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28479 if (ci)
28480 set_constraints (ded_tmpl, ci);
28481
28482 return ded_tmpl;
28483 }
28484
28485 /* Add to LIST the member types for the reshaped initializer CTOR. */
28486
28487 static tree
28488 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28489 {
28490 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28491 tree idx, val; unsigned i;
28492 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28493 {
28494 tree ftype = elt ? elt : TREE_TYPE (idx);
28495 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28496 && CONSTRUCTOR_NELTS (val)
28497 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28498 type gets a single initializer. */
28499 && CP_AGGREGATE_TYPE_P (ftype)
28500 && !(TREE_CODE (ftype) == ARRAY_TYPE
28501 && uses_template_parms (TYPE_DOMAIN (ftype))))
28502 {
28503 tree subelt = NULL_TREE;
28504 if (TREE_CODE (ftype) == ARRAY_TYPE)
28505 subelt = TREE_TYPE (ftype);
28506 list = collect_ctor_idx_types (val, list, subelt);
28507 continue;
28508 }
28509 tree arg = NULL_TREE;
28510 if (i == v->length() - 1
28511 && PACK_EXPANSION_P (ftype))
28512 /* Give the trailing pack expansion parameter a default argument to
28513 match aggregate initialization behavior, even if we deduce the
28514 length of the pack separately to more than we have initializers. */
28515 arg = build_constructor (init_list_type_node, NULL);
28516 /* if ei is of array type and xi is a braced-init-list or string literal,
28517 Ti is an rvalue reference to the declared type of ei */
28518 STRIP_ANY_LOCATION_WRAPPER (val);
28519 if (TREE_CODE (ftype) == ARRAY_TYPE
28520 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28521 || TREE_CODE (val) == STRING_CST))
28522 {
28523 if (TREE_CODE (val) == STRING_CST)
28524 ftype = cp_build_qualified_type
28525 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28526 ftype = (cp_build_reference_type
28527 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28528 }
28529 list = tree_cons (arg, ftype, list);
28530 }
28531
28532 return list;
28533 }
28534
28535 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28536
28537 static bool
28538 is_spec_or_derived (tree etype, tree tmpl)
28539 {
28540 if (!etype || !CLASS_TYPE_P (etype))
28541 return false;
28542
28543 tree type = TREE_TYPE (tmpl);
28544 tree tparms = (INNERMOST_TEMPLATE_PARMS
28545 (DECL_TEMPLATE_PARMS (tmpl)));
28546 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28547 int err = unify (tparms, targs, type, etype,
28548 UNIFY_ALLOW_DERIVED, /*explain*/false);
28549 ggc_free (targs);
28550 return !err;
28551 }
28552
28553 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28554 INIT. */
28555
28556 static tree
28557 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28558 {
28559 if (cxx_dialect < cxx20)
28560 return NULL_TREE;
28561
28562 if (init == NULL_TREE)
28563 return NULL_TREE;
28564
28565 tree type = TREE_TYPE (tmpl);
28566 if (!CP_AGGREGATE_TYPE_P (type))
28567 return NULL_TREE;
28568
28569 /* No aggregate candidate for copy-initialization. */
28570 if (args->length() == 1)
28571 {
28572 tree val = (*args)[0];
28573 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28574 return NULL_TREE;
28575 }
28576
28577 /* If we encounter a problem, we just won't add the candidate. */
28578 tsubst_flags_t complain = tf_none;
28579
28580 tree parms = NULL_TREE;
28581 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28582 {
28583 init = reshape_init (type, init, complain);
28584 if (init == error_mark_node)
28585 return NULL_TREE;
28586 parms = collect_ctor_idx_types (init, parms);
28587 }
28588 else if (TREE_CODE (init) == TREE_LIST)
28589 {
28590 int len = list_length (init);
28591 for (tree field = TYPE_FIELDS (type);
28592 len;
28593 --len, field = DECL_CHAIN (field))
28594 {
28595 field = next_initializable_field (field);
28596 if (!field)
28597 return NULL_TREE;
28598 tree ftype = finish_decltype_type (field, true, complain);
28599 parms = tree_cons (NULL_TREE, ftype, parms);
28600 }
28601 }
28602 else
28603 /* Aggregate initialization doesn't apply to an initializer expression. */
28604 return NULL_TREE;
28605
28606 if (parms)
28607 {
28608 tree last = parms;
28609 parms = nreverse (parms);
28610 TREE_CHAIN (last) = void_list_node;
28611 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28612 return guide;
28613 }
28614
28615 return NULL_TREE;
28616 }
28617
28618 /* UGUIDES are the deduction guides for the underlying template of alias
28619 template TMPL; adjust them to be deduction guides for TMPL. */
28620
28621 static tree
28622 alias_ctad_tweaks (tree tmpl, tree uguides)
28623 {
28624 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28625 class type (9.2.8.2) where the template-name names an alias template A,
28626 the defining-type-id of A must be of the form
28627
28628 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28629
28630 as specified in 9.2.8.2. The guides of A are the set of functions or
28631 function templates formed as follows. For each function or function
28632 template f in the guides of the template named by the simple-template-id
28633 of the defining-type-id, the template arguments of the return type of f
28634 are deduced from the defining-type-id of A according to the process in
28635 13.10.2.5 with the exception that deduction does not fail if not all
28636 template arguments are deduced. Let g denote the result of substituting
28637 these deductions into f. If substitution succeeds, form a function or
28638 function template f' with the following properties and add it to the set
28639 of guides of A:
28640
28641 * The function type of f' is the function type of g.
28642
28643 * If f is a function template, f' is a function template whose template
28644 parameter list consists of all the template parameters of A (including
28645 their default template arguments) that appear in the above deductions or
28646 (recursively) in their default template arguments, followed by the
28647 template parameters of f that were not deduced (including their default
28648 template arguments), otherwise f' is not a function template.
28649
28650 * The associated constraints (13.5.2) are the conjunction of the
28651 associated constraints of g and a constraint that is satisfied if and only
28652 if the arguments of A are deducible (see below) from the return type.
28653
28654 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28655 be so as well.
28656
28657 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28658 considered to be so as well.
28659
28660 * The explicit-specifier of f' is the explicit-specifier of g (if
28661 any). */
28662
28663 /* This implementation differs from the above in two significant ways:
28664
28665 1) We include all template parameters of A, not just some.
28666 2) The added constraint is same_type instead of deducible.
28667
28668 I believe that while it's probably possible to construct a testcase that
28669 behaves differently with this simplification, it should have the same
28670 effect for real uses. Including all template parameters means that we
28671 deduce all parameters of A when resolving the call, so when we're in the
28672 constraint we don't need to deduce them again, we can just check whether
28673 the deduction produced the desired result. */
28674
28675 tsubst_flags_t complain = tf_warning_or_error;
28676 tree atype = TREE_TYPE (tmpl);
28677 tree aguides = NULL_TREE;
28678 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28679 unsigned natparms = TREE_VEC_LENGTH (atparms);
28680 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28681 for (ovl_iterator iter (uguides); iter; ++iter)
28682 {
28683 tree f = *iter;
28684 tree in_decl = f;
28685 location_t loc = DECL_SOURCE_LOCATION (f);
28686 tree ret = TREE_TYPE (TREE_TYPE (f));
28687 tree fprime = f;
28688 if (TREE_CODE (f) == TEMPLATE_DECL)
28689 {
28690 processing_template_decl_sentinel ptds (/*reset*/false);
28691 ++processing_template_decl;
28692
28693 /* Deduce template arguments for f from the type-id of A. */
28694 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28695 unsigned len = TREE_VEC_LENGTH (ftparms);
28696 tree targs = make_tree_vec (len);
28697 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28698 gcc_assert (!err);
28699
28700 /* The number of parms for f' is the number of parms for A plus
28701 non-deduced parms of f. */
28702 unsigned ndlen = 0;
28703 unsigned j;
28704 for (unsigned i = 0; i < len; ++i)
28705 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28706 ++ndlen;
28707 tree gtparms = make_tree_vec (natparms + ndlen);
28708
28709 /* First copy over the parms of A. */
28710 for (j = 0; j < natparms; ++j)
28711 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28712 /* Now rewrite the non-deduced parms of f. */
28713 for (unsigned i = 0; ndlen && i < len; ++i)
28714 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28715 {
28716 --ndlen;
28717 unsigned index = j++;
28718 unsigned level = 1;
28719 tree oldlist = TREE_VEC_ELT (ftparms, i);
28720 tree list = rewrite_tparm_list (oldlist, index, level,
28721 targs, i, complain);
28722 TREE_VEC_ELT (gtparms, index) = list;
28723 }
28724 gtparms = build_tree_list (size_one_node, gtparms);
28725
28726 /* Substitute the deduced arguments plus the rewritten template
28727 parameters into f to get g. This covers the type, copyness,
28728 guideness, and explicit-specifier. */
28729 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28730 if (g == error_mark_node)
28731 return error_mark_node;
28732 DECL_USE_TEMPLATE (g) = 0;
28733 fprime = build_template_decl (g, gtparms, false);
28734 DECL_TEMPLATE_RESULT (fprime) = g;
28735 TREE_TYPE (fprime) = TREE_TYPE (g);
28736 tree gtargs = template_parms_to_args (gtparms);
28737 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28738 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28739
28740 /* Substitute the associated constraints. */
28741 tree ci = get_constraints (f);
28742 if (ci)
28743 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28744 if (ci == error_mark_node)
28745 return error_mark_node;
28746
28747 /* Add a constraint that the return type matches the instantiation of
28748 A with the same template arguments. */
28749 ret = TREE_TYPE (TREE_TYPE (fprime));
28750 if (!same_type_p (atype, ret)
28751 /* FIXME this should mean they don't compare as equivalent. */
28752 || dependent_alias_template_spec_p (atype, nt_opaque))
28753 {
28754 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28755 ci = append_constraint (ci, same);
28756 }
28757
28758 if (ci)
28759 {
28760 remove_constraints (fprime);
28761 set_constraints (fprime, ci);
28762 }
28763 }
28764 else
28765 {
28766 /* For a non-template deduction guide, if the arguments of A aren't
28767 deducible from the return type, don't add the candidate. */
28768 tree targs = make_tree_vec (natparms);
28769 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28770 for (unsigned i = 0; !err && i < natparms; ++i)
28771 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28772 err = true;
28773 if (err)
28774 continue;
28775 }
28776
28777 aguides = lookup_add (fprime, aguides);
28778 }
28779
28780 return aguides;
28781 }
28782
28783 /* Return artificial deduction guides built from the constructors of class
28784 template TMPL. */
28785
28786 static tree
28787 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28788 {
28789 tree type = TREE_TYPE (tmpl);
28790 tree outer_args = NULL_TREE;
28791 if (DECL_CLASS_SCOPE_P (tmpl)
28792 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28793 {
28794 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28795 type = TREE_TYPE (most_general_template (tmpl));
28796 }
28797
28798 tree cands = NULL_TREE;
28799
28800 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28801 {
28802 /* Skip inherited constructors. */
28803 if (iter.using_p ())
28804 continue;
28805
28806 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28807 cands = lookup_add (guide, cands);
28808 }
28809
28810 /* Add implicit default constructor deduction guide. */
28811 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28812 {
28813 tree guide = build_deduction_guide (type, type, outer_args,
28814 complain);
28815 cands = lookup_add (guide, cands);
28816 }
28817
28818 /* Add copy guide. */
28819 {
28820 tree gtype = build_reference_type (type);
28821 tree guide = build_deduction_guide (type, gtype, outer_args,
28822 complain);
28823 cands = lookup_add (guide, cands);
28824 }
28825
28826 return cands;
28827 }
28828
28829 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28830
28831 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28832 aggregate candidate is added separately because it depends on the
28833 initializer. */
28834
28835 static tree
28836 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28837 {
28838 tree guides = NULL_TREE;
28839 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28840 {
28841 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28842 tree tinfo = get_template_info (under);
28843 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28844 }
28845 else
28846 {
28847 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28848 dguide_name (tmpl),
28849 LOOK_want::NORMAL, /*complain*/false);
28850 if (guides == error_mark_node)
28851 guides = NULL_TREE;
28852 }
28853
28854 /* Cache the deduction guides for a template. We also remember the result of
28855 lookup, and rebuild everything if it changes; should be very rare. */
28856 tree_pair_p cache = NULL;
28857 if (tree_pair_p &r
28858 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28859 {
28860 cache = r;
28861 if (cache->purpose == guides)
28862 return cache->value;
28863 }
28864 else
28865 {
28866 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28867 cache->purpose = guides;
28868 }
28869
28870 tree cands = NULL_TREE;
28871 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28872 cands = alias_ctad_tweaks (tmpl, guides);
28873 else
28874 {
28875 cands = ctor_deduction_guides_for (tmpl, complain);
28876 for (ovl_iterator it (guides); it; ++it)
28877 cands = lookup_add (*it, cands);
28878 }
28879
28880 cache->value = cands;
28881 return cands;
28882 }
28883
28884 /* Return whether TMPL is a (class template argument-) deducible template. */
28885
28886 bool
28887 ctad_template_p (tree tmpl)
28888 {
28889 /* A deducible template is either a class template or is an alias template
28890 whose defining-type-id is of the form
28891
28892 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28893
28894 where the nested-name-specifier (if any) is non-dependent and the
28895 template-name of the simple-template-id names a deducible template. */
28896
28897 if (DECL_CLASS_TEMPLATE_P (tmpl)
28898 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28899 return true;
28900 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28901 return false;
28902 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28903 if (tree tinfo = get_template_info (orig))
28904 return ctad_template_p (TI_TEMPLATE (tinfo));
28905 return false;
28906 }
28907
28908 /* Deduce template arguments for the class template placeholder PTYPE for
28909 template TMPL based on the initializer INIT, and return the resulting
28910 type. */
28911
28912 static tree
28913 do_class_deduction (tree ptype, tree tmpl, tree init,
28914 int flags, tsubst_flags_t complain)
28915 {
28916 /* We should have handled this in the caller. */
28917 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28918 return ptype;
28919
28920 /* Look through alias templates that just rename another template. */
28921 tmpl = get_underlying_template (tmpl);
28922 if (!ctad_template_p (tmpl))
28923 {
28924 if (complain & tf_error)
28925 error ("non-deducible template %qT used without template arguments", tmpl);
28926 return error_mark_node;
28927 }
28928 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28929 {
28930 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28931 if (complain & tf_warning_or_error)
28932 pedwarn (input_location, 0, "alias template deduction only available "
28933 "with %<-std=c++20%> or %<-std=gnu++20%>");
28934 }
28935
28936 if (init && TREE_TYPE (init) == ptype)
28937 /* Using the template parm as its own argument. */
28938 return ptype;
28939
28940 tree type = TREE_TYPE (tmpl);
28941
28942 bool try_list_ctor = false;
28943
28944 releasing_vec rv_args = NULL;
28945 vec<tree,va_gc> *&args = *&rv_args;
28946 if (init == NULL_TREE)
28947 args = make_tree_vector ();
28948 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28949 {
28950 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28951 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28952 {
28953 /* As an exception, the first phase in 16.3.1.7 (considering the
28954 initializer list as a single argument) is omitted if the
28955 initializer list consists of a single expression of type cv U,
28956 where U is a specialization of C or a class derived from a
28957 specialization of C. */
28958 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28959 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28960 try_list_ctor = false;
28961 }
28962 if (try_list_ctor || is_std_init_list (type))
28963 args = make_tree_vector_single (init);
28964 else
28965 args = make_tree_vector_from_ctor (init);
28966 }
28967 else if (TREE_CODE (init) == TREE_LIST)
28968 args = make_tree_vector_from_list (init);
28969 else
28970 args = make_tree_vector_single (init);
28971
28972 /* Do this now to avoid problems with erroneous args later on. */
28973 args = resolve_args (args, complain);
28974 if (args == NULL)
28975 return error_mark_node;
28976
28977 tree cands = deduction_guides_for (tmpl, complain);
28978 if (cands == error_mark_node)
28979 return error_mark_node;
28980
28981 /* Prune explicit deduction guides in copy-initialization context. */
28982 bool elided = false;
28983 if (flags & LOOKUP_ONLYCONVERTING)
28984 {
28985 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28986 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28987 elided = true;
28988
28989 if (elided)
28990 {
28991 /* Found a nonconverting guide, prune the candidates. */
28992 tree pruned = NULL_TREE;
28993 for (lkp_iterator iter (cands); iter; ++iter)
28994 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28995 pruned = lookup_add (*iter, pruned);
28996
28997 cands = pruned;
28998 }
28999 }
29000
29001 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29002 cands = lookup_add (guide, cands);
29003
29004 tree call = error_mark_node;
29005
29006 /* If this is list-initialization and the class has a list constructor, first
29007 try deducing from the list as a single argument, as [over.match.list]. */
29008 tree list_cands = NULL_TREE;
29009 if (try_list_ctor && cands)
29010 for (lkp_iterator iter (cands); iter; ++iter)
29011 {
29012 tree dg = *iter;
29013 if (is_list_ctor (dg))
29014 list_cands = lookup_add (dg, list_cands);
29015 }
29016 if (list_cands)
29017 {
29018 ++cp_unevaluated_operand;
29019 call = build_new_function_call (list_cands, &args, tf_decltype);
29020 --cp_unevaluated_operand;
29021
29022 if (call == error_mark_node)
29023 {
29024 /* That didn't work, now try treating the list as a sequence of
29025 arguments. */
29026 release_tree_vector (args);
29027 args = make_tree_vector_from_ctor (init);
29028 }
29029 }
29030
29031 if (elided && !cands)
29032 {
29033 error ("cannot deduce template arguments for copy-initialization"
29034 " of %qT, as it has no non-explicit deduction guides or "
29035 "user-declared constructors", type);
29036 return error_mark_node;
29037 }
29038 else if (!cands && call == error_mark_node)
29039 {
29040 error ("cannot deduce template arguments of %qT, as it has no viable "
29041 "deduction guides", type);
29042 return error_mark_node;
29043 }
29044
29045 if (call == error_mark_node)
29046 {
29047 ++cp_unevaluated_operand;
29048 call = build_new_function_call (cands, &args, tf_decltype);
29049 --cp_unevaluated_operand;
29050 }
29051
29052 if (call == error_mark_node
29053 && (complain & tf_warning_or_error))
29054 {
29055 error ("class template argument deduction failed:");
29056
29057 ++cp_unevaluated_operand;
29058 call = build_new_function_call (cands, &args, complain | tf_decltype);
29059 --cp_unevaluated_operand;
29060
29061 if (elided)
29062 inform (input_location, "explicit deduction guides not considered "
29063 "for copy-initialization");
29064 }
29065
29066 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29067 }
29068
29069 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29070 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29071 The CONTEXT determines the context in which auto deduction is performed
29072 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29073 OUTER_TARGS are used during template argument deduction
29074 (context == adc_unify) to properly substitute the result, and is ignored
29075 in other contexts.
29076
29077 For partial-concept-ids, extra args may be appended to the list of deduced
29078 template arguments prior to determining constraint satisfaction. */
29079
29080 tree
29081 do_auto_deduction (tree type, tree init, tree auto_node,
29082 tsubst_flags_t complain, auto_deduction_context context,
29083 tree outer_targs, int flags)
29084 {
29085 tree targs;
29086
29087 if (init == error_mark_node)
29088 return error_mark_node;
29089
29090 if (init && type_dependent_expression_p (init)
29091 && context != adc_unify)
29092 /* Defining a subset of type-dependent expressions that we can deduce
29093 from ahead of time isn't worth the trouble. */
29094 return type;
29095
29096 /* Similarly, we can't deduce from another undeduced decl. */
29097 if (init && undeduced_auto_decl (init))
29098 return type;
29099
29100 /* We may be doing a partial substitution, but we still want to replace
29101 auto_node. */
29102 complain &= ~tf_partial;
29103
29104 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29105 /* C++17 class template argument deduction. */
29106 return do_class_deduction (type, tmpl, init, flags, complain);
29107
29108 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29109 /* Nothing we can do with this, even in deduction context. */
29110 return type;
29111
29112 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29113 with either a new invented type template parameter U or, if the
29114 initializer is a braced-init-list (8.5.4), with
29115 std::initializer_list<U>. */
29116 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29117 {
29118 if (!DIRECT_LIST_INIT_P (init))
29119 type = listify_autos (type, auto_node);
29120 else if (CONSTRUCTOR_NELTS (init) == 1)
29121 init = CONSTRUCTOR_ELT (init, 0)->value;
29122 else
29123 {
29124 if (complain & tf_warning_or_error)
29125 {
29126 if (permerror (input_location, "direct-list-initialization of "
29127 "%<auto%> requires exactly one element"))
29128 inform (input_location,
29129 "for deduction to %<std::initializer_list%>, use copy-"
29130 "list-initialization (i.e. add %<=%> before the %<{%>)");
29131 }
29132 type = listify_autos (type, auto_node);
29133 }
29134 }
29135
29136 if (type == error_mark_node)
29137 return error_mark_node;
29138
29139 init = resolve_nondeduced_context (init, complain);
29140
29141 if (context == adc_decomp_type
29142 && auto_node == type
29143 && init != error_mark_node
29144 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29145 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29146 and initializer has array type, deduce cv-qualified array type. */
29147 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29148 complain);
29149 else if (AUTO_IS_DECLTYPE (auto_node))
29150 {
29151 tree stripped_init = tree_strip_any_location_wrapper (init);
29152 bool id = (DECL_P (stripped_init)
29153 || ((TREE_CODE (init) == COMPONENT_REF
29154 || TREE_CODE (init) == SCOPE_REF)
29155 && !REF_PARENTHESIZED_P (init)));
29156 targs = make_tree_vec (1);
29157 TREE_VEC_ELT (targs, 0)
29158 = finish_decltype_type (init, id, tf_warning_or_error);
29159 if (type != auto_node)
29160 {
29161 if (complain & tf_error)
29162 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29163 return error_mark_node;
29164 }
29165 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29166 {
29167 if (complain & tf_error)
29168 error ("%<decltype(auto)%> cannot be cv-qualified");
29169 return error_mark_node;
29170 }
29171 }
29172 else
29173 {
29174 if (error_operand_p (init))
29175 return error_mark_node;
29176
29177 tree parms = build_tree_list (NULL_TREE, type);
29178 tree tparms;
29179
29180 if (flag_concepts)
29181 tparms = extract_autos (type);
29182 else
29183 {
29184 tparms = make_tree_vec (1);
29185 TREE_VEC_ELT (tparms, 0)
29186 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29187 }
29188
29189 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29190 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29191 DEDUCE_CALL,
29192 NULL, /*explain_p=*/false);
29193 if (val > 0)
29194 {
29195 if (processing_template_decl)
29196 /* Try again at instantiation time. */
29197 return type;
29198 if (type && type != error_mark_node
29199 && (complain & tf_error))
29200 /* If type is error_mark_node a diagnostic must have been
29201 emitted by now. Also, having a mention to '<type error>'
29202 in the diagnostic is not really useful to the user. */
29203 {
29204 if (cfun
29205 && FNDECL_USED_AUTO (current_function_decl)
29206 && (auto_node
29207 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29208 && LAMBDA_FUNCTION_P (current_function_decl))
29209 error ("unable to deduce lambda return type from %qE", init);
29210 else
29211 error ("unable to deduce %qT from %qE", type, init);
29212 type_unification_real (tparms, targs, parms, &init, 1, 0,
29213 DEDUCE_CALL,
29214 NULL, /*explain_p=*/true);
29215 }
29216 return error_mark_node;
29217 }
29218 }
29219
29220 /* Check any placeholder constraints against the deduced type. */
29221 if (flag_concepts && !processing_template_decl)
29222 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29223 {
29224 /* Use the deduced type to check the associated constraints. If we
29225 have a partial-concept-id, rebuild the argument list so that
29226 we check using the extra arguments. */
29227 check = unpack_concept_check (check);
29228 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29229 tree cdecl = TREE_OPERAND (check, 0);
29230 if (OVL_P (cdecl))
29231 cdecl = OVL_FIRST (cdecl);
29232 tree cargs = TREE_OPERAND (check, 1);
29233 if (TREE_VEC_LENGTH (cargs) > 1)
29234 {
29235 cargs = copy_node (cargs);
29236 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29237 }
29238 else
29239 cargs = targs;
29240
29241 /* Rebuild the check using the deduced arguments. */
29242 check = build_concept_check (cdecl, cargs, tf_none);
29243
29244 if (!constraints_satisfied_p (check))
29245 {
29246 if (complain & tf_warning_or_error)
29247 {
29248 auto_diagnostic_group d;
29249 switch (context)
29250 {
29251 case adc_unspecified:
29252 case adc_unify:
29253 error("placeholder constraints not satisfied");
29254 break;
29255 case adc_variable_type:
29256 case adc_decomp_type:
29257 error ("deduced initializer does not satisfy "
29258 "placeholder constraints");
29259 break;
29260 case adc_return_type:
29261 error ("deduced return type does not satisfy "
29262 "placeholder constraints");
29263 break;
29264 case adc_requirement:
29265 error ("deduced expression type does not satisfy "
29266 "placeholder constraints");
29267 break;
29268 }
29269 diagnose_constraints (input_location, check, targs);
29270 }
29271 return error_mark_node;
29272 }
29273 }
29274
29275 if (processing_template_decl && context != adc_unify)
29276 outer_targs = current_template_args ();
29277 targs = add_to_template_args (outer_targs, targs);
29278 return tsubst (type, targs, complain, NULL_TREE);
29279 }
29280
29281 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29282 result. */
29283
29284 tree
29285 splice_late_return_type (tree type, tree late_return_type)
29286 {
29287 if (late_return_type)
29288 {
29289 gcc_assert (is_auto (type) || seen_error ());
29290 return late_return_type;
29291 }
29292
29293 if (tree *auto_node = find_type_usage (&type, is_auto))
29294 {
29295 tree idx = get_template_parm_index (*auto_node);
29296 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29297 {
29298 /* In an abbreviated function template we didn't know we were dealing
29299 with a function template when we saw the auto return type, so update
29300 it to have the correct level. */
29301 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29302 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29303 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29304 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29305 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29306 *auto_node = new_auto;
29307 }
29308 }
29309 return type;
29310 }
29311
29312 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29313 'decltype(auto)' or a deduced class template. */
29314
29315 bool
29316 is_auto (const_tree type)
29317 {
29318 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29319 && (TYPE_IDENTIFIER (type) == auto_identifier
29320 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29321 return true;
29322 else
29323 return false;
29324 }
29325
29326 /* for_each_template_parm callback for type_uses_auto. */
29327
29328 int
29329 is_auto_r (tree tp, void */*data*/)
29330 {
29331 return is_auto (tp);
29332 }
29333
29334 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29335 a use of `auto'. Returns NULL_TREE otherwise. */
29336
29337 tree
29338 type_uses_auto (tree type)
29339 {
29340 if (type == NULL_TREE)
29341 return NULL_TREE;
29342 else if (flag_concepts)
29343 {
29344 /* The Concepts TS allows multiple autos in one type-specifier; just
29345 return the first one we find, do_auto_deduction will collect all of
29346 them. */
29347 if (uses_template_parms (type))
29348 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29349 /*visited*/NULL, /*nondeduced*/false);
29350 else
29351 return NULL_TREE;
29352 }
29353 else if (tree *tp = find_type_usage (&type, is_auto))
29354 return *tp;
29355 else
29356 return NULL_TREE;
29357 }
29358
29359 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29360 concepts are enabled, auto is acceptable in template arguments, but
29361 only when TEMPL identifies a template class. Return TRUE if any
29362 such errors were reported. */
29363
29364 bool
29365 check_auto_in_tmpl_args (tree tmpl, tree args)
29366 {
29367 /* If there were previous errors, nevermind. */
29368 if (!args || TREE_CODE (args) != TREE_VEC)
29369 return false;
29370
29371 /* If TMPL is an identifier, we're parsing and we can't tell yet
29372 whether TMPL is supposed to be a type, a function or a variable.
29373 We'll only be able to tell during template substitution, so we
29374 expect to be called again then. If concepts are enabled and we
29375 know we have a type, we're ok. */
29376 if (flag_concepts
29377 && (identifier_p (tmpl)
29378 || (DECL_P (tmpl)
29379 && (DECL_TYPE_TEMPLATE_P (tmpl)
29380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29381 return false;
29382
29383 /* Quickly search for any occurrences of auto; usually there won't
29384 be any, and then we'll avoid allocating the vector. */
29385 if (!type_uses_auto (args))
29386 return false;
29387
29388 bool errors = false;
29389
29390 tree vec = extract_autos (args);
29391 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29392 {
29393 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29394 error_at (DECL_SOURCE_LOCATION (xauto),
29395 "invalid use of %qT in template argument", xauto);
29396 errors = true;
29397 }
29398
29399 return errors;
29400 }
29401
29402 /* Recursively walk over && expressions searching for EXPR. Return a reference
29403 to that expression. */
29404
29405 static tree *find_template_requirement (tree *t, tree key)
29406 {
29407 if (*t == key)
29408 return t;
29409 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29410 {
29411 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29412 return p;
29413 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29414 return p;
29415 }
29416 return 0;
29417 }
29418
29419 /* Convert the generic type parameters in PARM that match the types given in the
29420 range [START_IDX, END_IDX) from the current_template_parms into generic type
29421 packs. */
29422
29423 tree
29424 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29425 {
29426 tree current = current_template_parms;
29427 int depth = TMPL_PARMS_DEPTH (current);
29428 current = INNERMOST_TEMPLATE_PARMS (current);
29429 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29430
29431 for (int i = 0; i < start_idx; ++i)
29432 TREE_VEC_ELT (replacement, i)
29433 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29434
29435 for (int i = start_idx; i < end_idx; ++i)
29436 {
29437 /* Create a distinct parameter pack type from the current parm and add it
29438 to the replacement args to tsubst below into the generic function
29439 parameter. */
29440 tree node = TREE_VEC_ELT (current, i);
29441 tree o = TREE_TYPE (TREE_VALUE (node));
29442 tree t = copy_type (o);
29443 TEMPLATE_TYPE_PARM_INDEX (t)
29444 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29445 t, 0, 0, tf_none);
29446 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29447 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29448 TYPE_MAIN_VARIANT (t) = t;
29449 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29450 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29451 TREE_VEC_ELT (replacement, i) = t;
29452
29453 /* Replace the current template parameter with new pack. */
29454 TREE_VALUE (node) = TREE_CHAIN (t);
29455
29456 /* Surgically adjust the associated constraint of adjusted parameter
29457 and it's corresponding contribution to the current template
29458 requirements. */
29459 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29460 {
29461 tree id = unpack_concept_check (constr);
29462 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29463 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29464 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29465
29466 /* If there was a constraint, we also need to replace that in
29467 the template requirements, which we've already built. */
29468 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29469 reqs = find_template_requirement (reqs, constr);
29470 *reqs = fold;
29471 }
29472 }
29473
29474 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29475 TREE_VEC_ELT (replacement, i)
29476 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29477
29478 /* If there are more levels then build up the replacement with the outer
29479 template parms. */
29480 if (depth > 1)
29481 replacement = add_to_template_args (template_parms_to_args
29482 (TREE_CHAIN (current_template_parms)),
29483 replacement);
29484
29485 return tsubst (parm, replacement, tf_none, NULL_TREE);
29486 }
29487
29488 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29489 0..N-1. */
29490
29491 void
29492 declare_integer_pack (void)
29493 {
29494 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29495 build_function_type_list (integer_type_node,
29496 integer_type_node,
29497 NULL_TREE),
29498 NULL_TREE, ECF_CONST);
29499 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29500 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29501 CP_BUILT_IN_INTEGER_PACK);
29502 }
29503
29504 /* Set up the hash tables for template instantiations. */
29505
29506 void
29507 init_template_processing (void)
29508 {
29509 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29510 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29511
29512 if (cxx_dialect >= cxx11)
29513 declare_integer_pack ();
29514 }
29515
29516 /* Print stats about the template hash tables for -fstats. */
29517
29518 void
29519 print_template_statistics (void)
29520 {
29521 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29522 "%f collisions\n", (long) decl_specializations->size (),
29523 (long) decl_specializations->elements (),
29524 decl_specializations->collisions ());
29525 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29526 "%f collisions\n", (long) type_specializations->size (),
29527 (long) type_specializations->elements (),
29528 type_specializations->collisions ());
29529 }
29530
29531 #if CHECKING_P
29532
29533 namespace selftest {
29534
29535 /* Verify that build_non_dependent_expr () works, for various expressions,
29536 and that location wrappers don't affect the results. */
29537
29538 static void
29539 test_build_non_dependent_expr ()
29540 {
29541 location_t loc = BUILTINS_LOCATION;
29542
29543 /* Verify constants, without and with location wrappers. */
29544 tree int_cst = build_int_cst (integer_type_node, 42);
29545 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29546
29547 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29548 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29549 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29550
29551 tree string_lit = build_string (4, "foo");
29552 TREE_TYPE (string_lit) = char_array_type_node;
29553 string_lit = fix_string_type (string_lit);
29554 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29555
29556 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29557 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29558 ASSERT_EQ (wrapped_string_lit,
29559 build_non_dependent_expr (wrapped_string_lit));
29560 }
29561
29562 /* Verify that type_dependent_expression_p () works correctly, even
29563 in the presence of location wrapper nodes. */
29564
29565 static void
29566 test_type_dependent_expression_p ()
29567 {
29568 location_t loc = BUILTINS_LOCATION;
29569
29570 tree name = get_identifier ("foo");
29571
29572 /* If no templates are involved, nothing is type-dependent. */
29573 gcc_assert (!processing_template_decl);
29574 ASSERT_FALSE (type_dependent_expression_p (name));
29575
29576 ++processing_template_decl;
29577
29578 /* Within a template, an unresolved name is always type-dependent. */
29579 ASSERT_TRUE (type_dependent_expression_p (name));
29580
29581 /* Ensure it copes with NULL_TREE and errors. */
29582 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29583 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29584
29585 /* A USING_DECL in a template should be type-dependent, even if wrapped
29586 with a location wrapper (PR c++/83799). */
29587 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29588 TREE_TYPE (using_decl) = integer_type_node;
29589 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29590 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29591 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29592 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29593
29594 --processing_template_decl;
29595 }
29596
29597 /* Run all of the selftests within this file. */
29598
29599 void
29600 cp_pt_c_tests ()
29601 {
29602 test_build_non_dependent_expr ();
29603 test_type_dependent_expression_p ();
29604 }
29605
29606 } // namespace selftest
29607
29608 #endif /* #if CHECKING_P */
29609
29610 #include "gt-cp-pt.h"