c++: Fix typo in NON_UNION_CLASS_TYPE_P.
[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, /*hiding=*/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, /*hiding=*/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, /*hiding=*/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 bool found_hidden = false;
2992
2993 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2994 if (!was_template_id)
2995 {
2996 tree fns;
2997
2998 gcc_assert (identifier_p (declarator));
2999 if (ctype)
3000 fns = dname;
3001 else
3002 {
3003 /* If there is no class context, the explicit instantiation
3004 must be at namespace scope. */
3005 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3006
3007 /* Find the namespace binding, using the declaration
3008 context. */
3009 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3010 LOOK_want::NORMAL, true);
3011 if (fns == error_mark_node)
3012 {
3013 /* If lookup fails, look for a friend declaration so we can
3014 give a better diagnostic. */
3015 fns = (lookup_qualified_name
3016 (CP_DECL_CONTEXT (decl), dname,
3017 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3018 /*complain*/true));
3019 found_hidden = true;
3020 }
3021
3022 if (fns == error_mark_node || !is_overloaded_fn (fns))
3023 {
3024 error ("%qD is not a template function", dname);
3025 fns = error_mark_node;
3026 }
3027 }
3028
3029 declarator = lookup_template_function (fns, NULL_TREE);
3030 }
3031
3032 if (declarator == error_mark_node)
3033 return error_mark_node;
3034
3035 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3036 {
3037 if (!explicit_instantiation)
3038 /* A specialization in class scope. This is invalid,
3039 but the error will already have been flagged by
3040 check_specialization_scope. */
3041 return error_mark_node;
3042 else
3043 {
3044 /* It's not valid to write an explicit instantiation in
3045 class scope, e.g.:
3046
3047 class C { template void f(); }
3048
3049 This case is caught by the parser. However, on
3050 something like:
3051
3052 template class C { void f(); };
3053
3054 (which is invalid) we can get here. The error will be
3055 issued later. */
3056 ;
3057 }
3058
3059 return decl;
3060 }
3061 else if (ctype != NULL_TREE
3062 && (identifier_p (TREE_OPERAND (declarator, 0))))
3063 {
3064 // We'll match variable templates in start_decl.
3065 if (VAR_P (decl))
3066 return decl;
3067
3068 /* Find the list of functions in ctype that have the same
3069 name as the declared function. */
3070 tree name = TREE_OPERAND (declarator, 0);
3071
3072 if (constructor_name_p (name, ctype))
3073 {
3074 if (DECL_CONSTRUCTOR_P (decl)
3075 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3076 : !CLASSTYPE_DESTRUCTOR (ctype))
3077 {
3078 /* From [temp.expl.spec]:
3079
3080 If such an explicit specialization for the member
3081 of a class template names an implicitly-declared
3082 special member function (clause _special_), the
3083 program is ill-formed.
3084
3085 Similar language is found in [temp.explicit]. */
3086 error ("specialization of implicitly-declared special member function");
3087 return error_mark_node;
3088 }
3089
3090 name = DECL_NAME (decl);
3091 }
3092
3093 /* For a type-conversion operator, We might be looking for
3094 `operator int' which will be a specialization of
3095 `operator T'. Grab all the conversion operators, and
3096 then select from them. */
3097 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3098 ? conv_op_identifier : name);
3099
3100 if (fns == NULL_TREE)
3101 {
3102 error ("no member function %qD declared in %qT", name, ctype);
3103 return error_mark_node;
3104 }
3105 else
3106 TREE_OPERAND (declarator, 0) = fns;
3107 }
3108
3109 /* Figure out what exactly is being specialized at this point.
3110 Note that for an explicit instantiation, even one for a
3111 member function, we cannot tell a priori whether the
3112 instantiation is for a member template, or just a member
3113 function of a template class. Even if a member template is
3114 being instantiated, the member template arguments may be
3115 elided if they can be deduced from the rest of the
3116 declaration. */
3117 tmpl = determine_specialization (declarator, decl,
3118 &targs,
3119 member_specialization,
3120 template_count,
3121 tsk);
3122
3123 if (!tmpl || tmpl == error_mark_node)
3124 /* We couldn't figure out what this declaration was
3125 specializing. */
3126 return error_mark_node;
3127 else
3128 {
3129 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3130 {
3131 auto_diagnostic_group d;
3132 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3133 "friend declaration %qD is not visible to "
3134 "explicit specialization", tmpl))
3135 inform (DECL_SOURCE_LOCATION (tmpl),
3136 "friend declaration here");
3137 }
3138
3139 if (!ctype && !is_friend
3140 && CP_DECL_CONTEXT (decl) == current_namespace)
3141 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3142
3143 tree gen_tmpl = most_general_template (tmpl);
3144
3145 if (explicit_instantiation)
3146 {
3147 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3148 is done by do_decl_instantiation later. */
3149
3150 int arg_depth = TMPL_ARGS_DEPTH (targs);
3151 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3152
3153 if (arg_depth > parm_depth)
3154 {
3155 /* If TMPL is not the most general template (for
3156 example, if TMPL is a friend template that is
3157 injected into namespace scope), then there will
3158 be too many levels of TARGS. Remove some of them
3159 here. */
3160 int i;
3161 tree new_targs;
3162
3163 new_targs = make_tree_vec (parm_depth);
3164 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3165 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3166 = TREE_VEC_ELT (targs, i);
3167 targs = new_targs;
3168 }
3169
3170 return instantiate_template (tmpl, targs, tf_error);
3171 }
3172
3173 /* If we thought that the DECL was a member function, but it
3174 turns out to be specializing a static member function,
3175 make DECL a static member function as well. */
3176 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3177 && DECL_STATIC_FUNCTION_P (tmpl)
3178 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3179 revert_static_member_fn (decl);
3180
3181 /* If this is a specialization of a member template of a
3182 template class, we want to return the TEMPLATE_DECL, not
3183 the specialization of it. */
3184 if (tsk == tsk_template && !was_template_id)
3185 {
3186 tree result = DECL_TEMPLATE_RESULT (tmpl);
3187 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3188 DECL_INITIAL (result) = NULL_TREE;
3189 if (have_def)
3190 {
3191 tree parm;
3192 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3193 DECL_SOURCE_LOCATION (result)
3194 = DECL_SOURCE_LOCATION (decl);
3195 /* We want to use the argument list specified in the
3196 definition, not in the original declaration. */
3197 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3198 for (parm = DECL_ARGUMENTS (result); parm;
3199 parm = DECL_CHAIN (parm))
3200 DECL_CONTEXT (parm) = result;
3201 }
3202 return register_specialization (tmpl, gen_tmpl, targs,
3203 is_friend, 0);
3204 }
3205
3206 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3207 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3208
3209 if (was_template_id)
3210 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3211
3212 /* Inherit default function arguments from the template
3213 DECL is specializing. */
3214 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3215 copy_default_args_to_explicit_spec (decl);
3216
3217 /* This specialization has the same protection as the
3218 template it specializes. */
3219 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3220 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3221
3222 /* 7.1.1-1 [dcl.stc]
3223
3224 A storage-class-specifier shall not be specified in an
3225 explicit specialization...
3226
3227 The parser rejects these, so unless action is taken here,
3228 explicit function specializations will always appear with
3229 global linkage.
3230
3231 The action recommended by the C++ CWG in response to C++
3232 defect report 605 is to make the storage class and linkage
3233 of the explicit specialization match the templated function:
3234
3235 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3236 */
3237 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3238 {
3239 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3240 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3241
3242 /* A concept cannot be specialized. */
3243 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3244 {
3245 error ("explicit specialization of function concept %qD",
3246 gen_tmpl);
3247 return error_mark_node;
3248 }
3249
3250 /* This specialization has the same linkage and visibility as
3251 the function template it specializes. */
3252 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3253 if (! TREE_PUBLIC (decl))
3254 {
3255 DECL_INTERFACE_KNOWN (decl) = 1;
3256 DECL_NOT_REALLY_EXTERN (decl) = 1;
3257 }
3258 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3259 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3260 {
3261 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3262 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3263 }
3264 }
3265
3266 /* If DECL is a friend declaration, declared using an
3267 unqualified name, the namespace associated with DECL may
3268 have been set incorrectly. For example, in:
3269
3270 template <typename T> void f(T);
3271 namespace N {
3272 struct S { friend void f<int>(int); }
3273 }
3274
3275 we will have set the DECL_CONTEXT for the friend
3276 declaration to N, rather than to the global namespace. */
3277 if (DECL_NAMESPACE_SCOPE_P (decl))
3278 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3279
3280 if (is_friend && !have_def)
3281 /* This is not really a declaration of a specialization.
3282 It's just the name of an instantiation. But, it's not
3283 a request for an instantiation, either. */
3284 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3285 else if (TREE_CODE (decl) == FUNCTION_DECL)
3286 /* A specialization is not necessarily COMDAT. */
3287 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3288 && DECL_DECLARED_INLINE_P (decl));
3289 else if (VAR_P (decl))
3290 DECL_COMDAT (decl) = false;
3291
3292 /* If this is a full specialization, register it so that we can find
3293 it again. Partial specializations will be registered in
3294 process_partial_specialization. */
3295 if (!processing_template_decl)
3296 {
3297 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3298
3299 decl = register_specialization (decl, gen_tmpl, targs,
3300 is_friend, 0);
3301 }
3302
3303
3304 /* A 'structor should already have clones. */
3305 gcc_assert (decl == error_mark_node
3306 || variable_template_p (tmpl)
3307 || !(DECL_CONSTRUCTOR_P (decl)
3308 || DECL_DESTRUCTOR_P (decl))
3309 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3310 }
3311 }
3312
3313 return decl;
3314 }
3315
3316 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3317 parameters. These are represented in the same format used for
3318 DECL_TEMPLATE_PARMS. */
3319
3320 int
3321 comp_template_parms (const_tree parms1, const_tree parms2)
3322 {
3323 const_tree p1;
3324 const_tree p2;
3325
3326 if (parms1 == parms2)
3327 return 1;
3328
3329 for (p1 = parms1, p2 = parms2;
3330 p1 != NULL_TREE && p2 != NULL_TREE;
3331 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3332 {
3333 tree t1 = TREE_VALUE (p1);
3334 tree t2 = TREE_VALUE (p2);
3335 int i;
3336
3337 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3338 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3339
3340 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3341 return 0;
3342
3343 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3344 {
3345 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3346 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3347
3348 /* If either of the template parameters are invalid, assume
3349 they match for the sake of error recovery. */
3350 if (error_operand_p (parm1) || error_operand_p (parm2))
3351 return 1;
3352
3353 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3354 return 0;
3355
3356 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3357 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3358 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3359 continue;
3360 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3361 return 0;
3362 }
3363 }
3364
3365 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3366 /* One set of parameters has more parameters lists than the
3367 other. */
3368 return 0;
3369
3370 return 1;
3371 }
3372
3373 /* Returns true if two template parameters are declared with
3374 equivalent constraints. */
3375
3376 static bool
3377 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3378 {
3379 tree req1 = TREE_TYPE (parm1);
3380 tree req2 = TREE_TYPE (parm2);
3381 if (!req1 != !req2)
3382 return false;
3383 if (req1)
3384 return cp_tree_equal (req1, req2);
3385 return true;
3386 }
3387
3388 /* Returns true when two template parameters are equivalent. */
3389
3390 static bool
3391 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3392 {
3393 tree decl1 = TREE_VALUE (parm1);
3394 tree decl2 = TREE_VALUE (parm2);
3395
3396 /* If either of the template parameters are invalid, assume
3397 they match for the sake of error recovery. */
3398 if (error_operand_p (decl1) || error_operand_p (decl2))
3399 return true;
3400
3401 /* ... they declare parameters of the same kind. */
3402 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3403 return false;
3404
3405 /* ... one parameter was introduced by a parameter declaration, then
3406 both are. This case arises as a result of eagerly rewriting declarations
3407 during parsing. */
3408 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3409 return false;
3410
3411 /* ... if either declares a pack, they both do. */
3412 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3413 return false;
3414
3415 if (TREE_CODE (decl1) == PARM_DECL)
3416 {
3417 /* ... if they declare non-type parameters, the types are equivalent. */
3418 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3419 return false;
3420 }
3421 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3422 {
3423 /* ... if they declare template template parameters, their template
3424 parameter lists are equivalent. */
3425 if (!template_heads_equivalent_p (decl1, decl2))
3426 return false;
3427 }
3428
3429 /* ... if they are declared with a qualified-concept name, they both
3430 are, and those names are equivalent. */
3431 return template_parameter_constraints_equivalent_p (parm1, parm2);
3432 }
3433
3434 /* Returns true if two template parameters lists are equivalent.
3435 Two template parameter lists are equivalent if they have the
3436 same length and their corresponding parameters are equivalent.
3437
3438 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3439 data structure returned by DECL_TEMPLATE_PARMS.
3440
3441 This is generally the same implementation as comp_template_parms
3442 except that it also the concept names and arguments used to
3443 introduce parameters. */
3444
3445 static bool
3446 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3447 {
3448 if (parms1 == parms2)
3449 return true;
3450
3451 const_tree p1 = parms1;
3452 const_tree p2 = parms2;
3453 while (p1 != NULL_TREE && p2 != NULL_TREE)
3454 {
3455 tree list1 = TREE_VALUE (p1);
3456 tree list2 = TREE_VALUE (p2);
3457
3458 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3459 return 0;
3460
3461 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3462 {
3463 tree parm1 = TREE_VEC_ELT (list1, i);
3464 tree parm2 = TREE_VEC_ELT (list2, i);
3465 if (!template_parameters_equivalent_p (parm1, parm2))
3466 return false;
3467 }
3468
3469 p1 = TREE_CHAIN (p1);
3470 p2 = TREE_CHAIN (p2);
3471 }
3472
3473 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3474 return false;
3475
3476 return true;
3477 }
3478
3479 /* Return true if the requires-clause of the template parameter lists are
3480 equivalent and false otherwise. */
3481 static bool
3482 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3483 {
3484 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3485 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3486 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3487 return false;
3488 if (!cp_tree_equal (req1, req2))
3489 return false;
3490 return true;
3491 }
3492
3493 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3494 Two template heads are equivalent if their template parameter
3495 lists are equivalent and their requires clauses are equivalent.
3496
3497 In pre-C++20, this is equivalent to calling comp_template_parms
3498 for the template parameters of TMPL1 and TMPL2. */
3499
3500 bool
3501 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3502 {
3503 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3504 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3505
3506 /* Don't change the matching rules for pre-C++20. */
3507 if (cxx_dialect < cxx20)
3508 return comp_template_parms (parms1, parms2);
3509
3510 /* ... have the same number of template parameters, and their
3511 corresponding parameters are equivalent. */
3512 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3513 return false;
3514
3515 /* ... if either has a requires-clause, they both do and their
3516 corresponding constraint-expressions are equivalent. */
3517 return template_requirements_equivalent_p (parms1, parms2);
3518 }
3519
3520 /* Determine whether PARM is a parameter pack. */
3521
3522 bool
3523 template_parameter_pack_p (const_tree parm)
3524 {
3525 /* Determine if we have a non-type template parameter pack. */
3526 if (TREE_CODE (parm) == PARM_DECL)
3527 return (DECL_TEMPLATE_PARM_P (parm)
3528 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3529 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3530 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3531
3532 /* If this is a list of template parameters, we could get a
3533 TYPE_DECL or a TEMPLATE_DECL. */
3534 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3535 parm = TREE_TYPE (parm);
3536
3537 /* Otherwise it must be a type template parameter. */
3538 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3539 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3540 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3541 }
3542
3543 /* Determine if T is a function parameter pack. */
3544
3545 bool
3546 function_parameter_pack_p (const_tree t)
3547 {
3548 if (t && TREE_CODE (t) == PARM_DECL)
3549 return DECL_PACK_P (t);
3550 return false;
3551 }
3552
3553 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3554 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3555
3556 tree
3557 get_function_template_decl (const_tree primary_func_tmpl_inst)
3558 {
3559 if (! primary_func_tmpl_inst
3560 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3561 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3562 return NULL;
3563
3564 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3565 }
3566
3567 /* Return true iff the function parameter PARAM_DECL was expanded
3568 from the function parameter pack PACK. */
3569
3570 bool
3571 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3572 {
3573 if (DECL_ARTIFICIAL (param_decl)
3574 || !function_parameter_pack_p (pack))
3575 return false;
3576
3577 /* The parameter pack and its pack arguments have the same
3578 DECL_PARM_INDEX. */
3579 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3580 }
3581
3582 /* Determine whether ARGS describes a variadic template args list,
3583 i.e., one that is terminated by a template argument pack. */
3584
3585 static bool
3586 template_args_variadic_p (tree args)
3587 {
3588 int nargs;
3589 tree last_parm;
3590
3591 if (args == NULL_TREE)
3592 return false;
3593
3594 args = INNERMOST_TEMPLATE_ARGS (args);
3595 nargs = TREE_VEC_LENGTH (args);
3596
3597 if (nargs == 0)
3598 return false;
3599
3600 last_parm = TREE_VEC_ELT (args, nargs - 1);
3601
3602 return ARGUMENT_PACK_P (last_parm);
3603 }
3604
3605 /* Generate a new name for the parameter pack name NAME (an
3606 IDENTIFIER_NODE) that incorporates its */
3607
3608 static tree
3609 make_ith_pack_parameter_name (tree name, int i)
3610 {
3611 /* Munge the name to include the parameter index. */
3612 #define NUMBUF_LEN 128
3613 char numbuf[NUMBUF_LEN];
3614 char* newname;
3615 int newname_len;
3616
3617 if (name == NULL_TREE)
3618 return name;
3619 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3620 newname_len = IDENTIFIER_LENGTH (name)
3621 + strlen (numbuf) + 2;
3622 newname = (char*)alloca (newname_len);
3623 snprintf (newname, newname_len,
3624 "%s#%i", IDENTIFIER_POINTER (name), i);
3625 return get_identifier (newname);
3626 }
3627
3628 /* Return true if T is a primary function, class or alias template
3629 specialization, not including the template pattern. */
3630
3631 bool
3632 primary_template_specialization_p (const_tree t)
3633 {
3634 if (!t)
3635 return false;
3636
3637 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3638 return (DECL_LANG_SPECIFIC (t)
3639 && DECL_USE_TEMPLATE (t)
3640 && DECL_TEMPLATE_INFO (t)
3641 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3642 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3643 return (CLASSTYPE_TEMPLATE_INFO (t)
3644 && CLASSTYPE_USE_TEMPLATE (t)
3645 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3646 else if (alias_template_specialization_p (t, nt_transparent))
3647 return true;
3648 return false;
3649 }
3650
3651 /* Return true if PARM is a template template parameter. */
3652
3653 bool
3654 template_template_parameter_p (const_tree parm)
3655 {
3656 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3657 }
3658
3659 /* Return true iff PARM is a DECL representing a type template
3660 parameter. */
3661
3662 bool
3663 template_type_parameter_p (const_tree parm)
3664 {
3665 return (parm
3666 && (TREE_CODE (parm) == TYPE_DECL
3667 || TREE_CODE (parm) == TEMPLATE_DECL)
3668 && DECL_TEMPLATE_PARM_P (parm));
3669 }
3670
3671 /* Return the template parameters of T if T is a
3672 primary template instantiation, NULL otherwise. */
3673
3674 tree
3675 get_primary_template_innermost_parameters (const_tree t)
3676 {
3677 tree parms = NULL, template_info = NULL;
3678
3679 if ((template_info = get_template_info (t))
3680 && primary_template_specialization_p (t))
3681 parms = INNERMOST_TEMPLATE_PARMS
3682 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3683
3684 return parms;
3685 }
3686
3687 /* Return the template parameters of the LEVELth level from the full list
3688 of template parameters PARMS. */
3689
3690 tree
3691 get_template_parms_at_level (tree parms, int level)
3692 {
3693 tree p;
3694 if (!parms
3695 || TREE_CODE (parms) != TREE_LIST
3696 || level > TMPL_PARMS_DEPTH (parms))
3697 return NULL_TREE;
3698
3699 for (p = parms; p; p = TREE_CHAIN (p))
3700 if (TMPL_PARMS_DEPTH (p) == level)
3701 return p;
3702
3703 return NULL_TREE;
3704 }
3705
3706 /* Returns the template arguments of T if T is a template instantiation,
3707 NULL otherwise. */
3708
3709 tree
3710 get_template_innermost_arguments (const_tree t)
3711 {
3712 tree args = NULL, template_info = NULL;
3713
3714 if ((template_info = get_template_info (t))
3715 && TI_ARGS (template_info))
3716 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3717
3718 return args;
3719 }
3720
3721 /* Return the argument pack elements of T if T is a template argument pack,
3722 NULL otherwise. */
3723
3724 tree
3725 get_template_argument_pack_elems (const_tree t)
3726 {
3727 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3728 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3729 return NULL;
3730
3731 return ARGUMENT_PACK_ARGS (t);
3732 }
3733
3734 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3735 ARGUMENT_PACK_SELECT represents. */
3736
3737 static tree
3738 argument_pack_select_arg (tree t)
3739 {
3740 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3741 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3742
3743 /* If the selected argument is an expansion E, that most likely means we were
3744 called from gen_elem_of_pack_expansion_instantiation during the
3745 substituting of an argument pack (of which the Ith element is a pack
3746 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3747 In this case, the Ith element resulting from this substituting is going to
3748 be a pack expansion, which pattern is the pattern of E. Let's return the
3749 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3750 resulting pack expansion from it. */
3751 if (PACK_EXPANSION_P (arg))
3752 {
3753 /* Make sure we aren't throwing away arg info. */
3754 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3755 arg = PACK_EXPANSION_PATTERN (arg);
3756 }
3757
3758 return arg;
3759 }
3760
3761
3762 /* True iff FN is a function representing a built-in variadic parameter
3763 pack. */
3764
3765 bool
3766 builtin_pack_fn_p (tree fn)
3767 {
3768 if (!fn
3769 || TREE_CODE (fn) != FUNCTION_DECL
3770 || !DECL_IS_BUILTIN (fn))
3771 return false;
3772
3773 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3774 return true;
3775
3776 return false;
3777 }
3778
3779 /* True iff CALL is a call to a function representing a built-in variadic
3780 parameter pack. */
3781
3782 static bool
3783 builtin_pack_call_p (tree call)
3784 {
3785 if (TREE_CODE (call) != CALL_EXPR)
3786 return false;
3787 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3788 }
3789
3790 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3791
3792 static tree
3793 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3794 tree in_decl)
3795 {
3796 tree ohi = CALL_EXPR_ARG (call, 0);
3797 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3798 false/*fn*/, true/*int_cst*/);
3799
3800 if (value_dependent_expression_p (hi))
3801 {
3802 if (hi != ohi)
3803 {
3804 call = copy_node (call);
3805 CALL_EXPR_ARG (call, 0) = hi;
3806 }
3807 tree ex = make_pack_expansion (call, complain);
3808 tree vec = make_tree_vec (1);
3809 TREE_VEC_ELT (vec, 0) = ex;
3810 return vec;
3811 }
3812 else
3813 {
3814 hi = cxx_constant_value (hi);
3815 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3816
3817 /* Calculate the largest value of len that won't make the size of the vec
3818 overflow an int. The compiler will exceed resource limits long before
3819 this, but it seems a decent place to diagnose. */
3820 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3821
3822 if (len < 0 || len > max)
3823 {
3824 if ((complain & tf_error)
3825 && hi != error_mark_node)
3826 error ("argument to %<__integer_pack%> must be between 0 and %d",
3827 max);
3828 return error_mark_node;
3829 }
3830
3831 tree vec = make_tree_vec (len);
3832
3833 for (int i = 0; i < len; ++i)
3834 TREE_VEC_ELT (vec, i) = size_int (i);
3835
3836 return vec;
3837 }
3838 }
3839
3840 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3841 CALL. */
3842
3843 static tree
3844 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3845 tree in_decl)
3846 {
3847 if (!builtin_pack_call_p (call))
3848 return NULL_TREE;
3849
3850 tree fn = CALL_EXPR_FN (call);
3851
3852 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3853 return expand_integer_pack (call, args, complain, in_decl);
3854
3855 return NULL_TREE;
3856 }
3857
3858 /* Structure used to track the progress of find_parameter_packs_r. */
3859 struct find_parameter_pack_data
3860 {
3861 /* TREE_LIST that will contain all of the parameter packs found by
3862 the traversal. */
3863 tree* parameter_packs;
3864
3865 /* Set of AST nodes that have been visited by the traversal. */
3866 hash_set<tree> *visited;
3867
3868 /* True iff we're making a type pack expansion. */
3869 bool type_pack_expansion_p;
3870 };
3871
3872 /* Identifies all of the argument packs that occur in a template
3873 argument and appends them to the TREE_LIST inside DATA, which is a
3874 find_parameter_pack_data structure. This is a subroutine of
3875 make_pack_expansion and uses_parameter_packs. */
3876 static tree
3877 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3878 {
3879 tree t = *tp;
3880 struct find_parameter_pack_data* ppd =
3881 (struct find_parameter_pack_data*)data;
3882 bool parameter_pack_p = false;
3883
3884 /* Don't look through typedefs; we are interested in whether a
3885 parameter pack is actually written in the expression/type we're
3886 looking at, not the target type. */
3887 if (TYPE_P (t) && typedef_variant_p (t))
3888 {
3889 /* But do look at arguments for an alias template. */
3890 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3891 cp_walk_tree (&TI_ARGS (tinfo),
3892 &find_parameter_packs_r,
3893 ppd, ppd->visited);
3894 *walk_subtrees = 0;
3895 return NULL_TREE;
3896 }
3897
3898 /* Identify whether this is a parameter pack or not. */
3899 switch (TREE_CODE (t))
3900 {
3901 case TEMPLATE_PARM_INDEX:
3902 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3903 parameter_pack_p = true;
3904 break;
3905
3906 case TEMPLATE_TYPE_PARM:
3907 t = TYPE_MAIN_VARIANT (t);
3908 /* FALLTHRU */
3909 case TEMPLATE_TEMPLATE_PARM:
3910 /* If the placeholder appears in the decl-specifier-seq of a function
3911 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3912 is a pack expansion, the invented template parameter is a template
3913 parameter pack. */
3914 if (ppd->type_pack_expansion_p && is_auto (t))
3915 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3916 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3917 parameter_pack_p = true;
3918 break;
3919
3920 case FIELD_DECL:
3921 case PARM_DECL:
3922 if (DECL_PACK_P (t))
3923 {
3924 /* We don't want to walk into the type of a PARM_DECL,
3925 because we don't want to see the type parameter pack. */
3926 *walk_subtrees = 0;
3927 parameter_pack_p = true;
3928 }
3929 break;
3930
3931 case VAR_DECL:
3932 if (DECL_PACK_P (t))
3933 {
3934 /* We don't want to walk into the type of a variadic capture proxy,
3935 because we don't want to see the type parameter pack. */
3936 *walk_subtrees = 0;
3937 parameter_pack_p = true;
3938 }
3939 else if (variable_template_specialization_p (t))
3940 {
3941 cp_walk_tree (&DECL_TI_ARGS (t),
3942 find_parameter_packs_r,
3943 ppd, ppd->visited);
3944 *walk_subtrees = 0;
3945 }
3946 break;
3947
3948 case CALL_EXPR:
3949 if (builtin_pack_call_p (t))
3950 parameter_pack_p = true;
3951 break;
3952
3953 case BASES:
3954 parameter_pack_p = true;
3955 break;
3956 default:
3957 /* Not a parameter pack. */
3958 break;
3959 }
3960
3961 if (parameter_pack_p)
3962 {
3963 /* Add this parameter pack to the list. */
3964 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3965 }
3966
3967 if (TYPE_P (t))
3968 cp_walk_tree (&TYPE_CONTEXT (t),
3969 &find_parameter_packs_r, ppd, ppd->visited);
3970
3971 /* This switch statement will return immediately if we don't find a
3972 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3973 switch (TREE_CODE (t))
3974 {
3975 case BOUND_TEMPLATE_TEMPLATE_PARM:
3976 /* Check the template itself. */
3977 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3978 &find_parameter_packs_r, ppd, ppd->visited);
3979 return NULL_TREE;
3980
3981 case DECL_EXPR:
3982 {
3983 tree decl = DECL_EXPR_DECL (t);
3984 /* Ignore the declaration of a capture proxy for a parameter pack. */
3985 if (is_capture_proxy (decl))
3986 *walk_subtrees = 0;
3987 if (is_typedef_decl (decl))
3988 /* Since we stop at typedefs above, we need to look through them at
3989 the point of the DECL_EXPR. */
3990 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3991 &find_parameter_packs_r, ppd, ppd->visited);
3992 return NULL_TREE;
3993 }
3994
3995 case TEMPLATE_DECL:
3996 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3997 return NULL_TREE;
3998 cp_walk_tree (&TREE_TYPE (t),
3999 &find_parameter_packs_r, ppd, ppd->visited);
4000 return NULL_TREE;
4001
4002 case TYPE_PACK_EXPANSION:
4003 case EXPR_PACK_EXPANSION:
4004 *walk_subtrees = 0;
4005 return NULL_TREE;
4006
4007 case INTEGER_TYPE:
4008 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4009 ppd, ppd->visited);
4010 *walk_subtrees = 0;
4011 return NULL_TREE;
4012
4013 case IDENTIFIER_NODE:
4014 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4015 ppd->visited);
4016 *walk_subtrees = 0;
4017 return NULL_TREE;
4018
4019 case LAMBDA_EXPR:
4020 {
4021 /* Since we defer implicit capture, look in the parms and body. */
4022 tree fn = lambda_function (t);
4023 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4024 ppd->visited);
4025 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4026 ppd->visited);
4027 return NULL_TREE;
4028 }
4029
4030 case DECLTYPE_TYPE:
4031 {
4032 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4033 type_pack_expansion_p to false so that any placeholders
4034 within the expression don't get marked as parameter packs. */
4035 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4036 ppd->type_pack_expansion_p = false;
4037 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4038 ppd, ppd->visited);
4039 ppd->type_pack_expansion_p = type_pack_expansion_p;
4040 *walk_subtrees = 0;
4041 return NULL_TREE;
4042 }
4043
4044 case IF_STMT:
4045 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4046 ppd, ppd->visited);
4047 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4048 ppd, ppd->visited);
4049 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4050 ppd, ppd->visited);
4051 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4052 *walk_subtrees = 0;
4053 return NULL_TREE;
4054
4055 default:
4056 return NULL_TREE;
4057 }
4058
4059 return NULL_TREE;
4060 }
4061
4062 /* Determines if the expression or type T uses any parameter packs. */
4063 tree
4064 uses_parameter_packs (tree t)
4065 {
4066 tree parameter_packs = NULL_TREE;
4067 struct find_parameter_pack_data ppd;
4068 ppd.parameter_packs = &parameter_packs;
4069 ppd.visited = new hash_set<tree>;
4070 ppd.type_pack_expansion_p = false;
4071 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4072 delete ppd.visited;
4073 return parameter_packs;
4074 }
4075
4076 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4077 representation a base-class initializer into a parameter pack
4078 expansion. If all goes well, the resulting node will be an
4079 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4080 respectively. */
4081 tree
4082 make_pack_expansion (tree arg, tsubst_flags_t complain)
4083 {
4084 tree result;
4085 tree parameter_packs = NULL_TREE;
4086 bool for_types = false;
4087 struct find_parameter_pack_data ppd;
4088
4089 if (!arg || arg == error_mark_node)
4090 return arg;
4091
4092 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4093 {
4094 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4095 class initializer. In this case, the TREE_PURPOSE will be a
4096 _TYPE node (representing the base class expansion we're
4097 initializing) and the TREE_VALUE will be a TREE_LIST
4098 containing the initialization arguments.
4099
4100 The resulting expansion looks somewhat different from most
4101 expansions. Rather than returning just one _EXPANSION, we
4102 return a TREE_LIST whose TREE_PURPOSE is a
4103 TYPE_PACK_EXPANSION containing the bases that will be
4104 initialized. The TREE_VALUE will be identical to the
4105 original TREE_VALUE, which is a list of arguments that will
4106 be passed to each base. We do not introduce any new pack
4107 expansion nodes into the TREE_VALUE (although it is possible
4108 that some already exist), because the TREE_PURPOSE and
4109 TREE_VALUE all need to be expanded together with the same
4110 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4111 resulting TREE_PURPOSE will mention the parameter packs in
4112 both the bases and the arguments to the bases. */
4113 tree purpose;
4114 tree value;
4115 tree parameter_packs = NULL_TREE;
4116
4117 /* Determine which parameter packs will be used by the base
4118 class expansion. */
4119 ppd.visited = new hash_set<tree>;
4120 ppd.parameter_packs = &parameter_packs;
4121 ppd.type_pack_expansion_p = false;
4122 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4123 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4124 &ppd, ppd.visited);
4125
4126 if (parameter_packs == NULL_TREE)
4127 {
4128 if (complain & tf_error)
4129 error ("base initializer expansion %qT contains no parameter packs",
4130 arg);
4131 delete ppd.visited;
4132 return error_mark_node;
4133 }
4134
4135 if (TREE_VALUE (arg) != void_type_node)
4136 {
4137 /* Collect the sets of parameter packs used in each of the
4138 initialization arguments. */
4139 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4140 {
4141 /* Determine which parameter packs will be expanded in this
4142 argument. */
4143 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4144 &ppd, ppd.visited);
4145 }
4146 }
4147
4148 delete ppd.visited;
4149
4150 /* Create the pack expansion type for the base type. */
4151 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4152 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4153 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4154 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4155
4156 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4157 they will rarely be compared to anything. */
4158 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4159
4160 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4161 }
4162
4163 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4164 for_types = true;
4165
4166 /* Build the PACK_EXPANSION_* node. */
4167 result = for_types
4168 ? cxx_make_type (TYPE_PACK_EXPANSION)
4169 : make_node (EXPR_PACK_EXPANSION);
4170 SET_PACK_EXPANSION_PATTERN (result, arg);
4171 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4172 {
4173 /* Propagate type and const-expression information. */
4174 TREE_TYPE (result) = TREE_TYPE (arg);
4175 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4176 /* Mark this read now, since the expansion might be length 0. */
4177 mark_exp_read (arg);
4178 }
4179 else
4180 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4181 they will rarely be compared to anything. */
4182 SET_TYPE_STRUCTURAL_EQUALITY (result);
4183
4184 /* Determine which parameter packs will be expanded. */
4185 ppd.parameter_packs = &parameter_packs;
4186 ppd.visited = new hash_set<tree>;
4187 ppd.type_pack_expansion_p = TYPE_P (arg);
4188 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4189 delete ppd.visited;
4190
4191 /* Make sure we found some parameter packs. */
4192 if (parameter_packs == NULL_TREE)
4193 {
4194 if (complain & tf_error)
4195 {
4196 if (TYPE_P (arg))
4197 error ("expansion pattern %qT contains no parameter packs", arg);
4198 else
4199 error ("expansion pattern %qE contains no parameter packs", arg);
4200 }
4201 return error_mark_node;
4202 }
4203 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4204
4205 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4206
4207 return result;
4208 }
4209
4210 /* Checks T for any "bare" parameter packs, which have not yet been
4211 expanded, and issues an error if any are found. This operation can
4212 only be done on full expressions or types (e.g., an expression
4213 statement, "if" condition, etc.), because we could have expressions like:
4214
4215 foo(f(g(h(args)))...)
4216
4217 where "args" is a parameter pack. check_for_bare_parameter_packs
4218 should not be called for the subexpressions args, h(args),
4219 g(h(args)), or f(g(h(args))), because we would produce erroneous
4220 error messages.
4221
4222 Returns TRUE and emits an error if there were bare parameter packs,
4223 returns FALSE otherwise. */
4224 bool
4225 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4226 {
4227 tree parameter_packs = NULL_TREE;
4228 struct find_parameter_pack_data ppd;
4229
4230 if (!processing_template_decl || !t || t == error_mark_node)
4231 return false;
4232
4233 /* A lambda might use a parameter pack from the containing context. */
4234 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4235 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4236 return false;
4237
4238 if (TREE_CODE (t) == TYPE_DECL)
4239 t = TREE_TYPE (t);
4240
4241 ppd.parameter_packs = &parameter_packs;
4242 ppd.visited = new hash_set<tree>;
4243 ppd.type_pack_expansion_p = false;
4244 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4245 delete ppd.visited;
4246
4247 if (parameter_packs)
4248 {
4249 if (loc == UNKNOWN_LOCATION)
4250 loc = cp_expr_loc_or_input_loc (t);
4251 error_at (loc, "parameter packs not expanded with %<...%>:");
4252 while (parameter_packs)
4253 {
4254 tree pack = TREE_VALUE (parameter_packs);
4255 tree name = NULL_TREE;
4256
4257 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4258 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4259 name = TYPE_NAME (pack);
4260 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4261 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4262 else if (TREE_CODE (pack) == CALL_EXPR)
4263 name = DECL_NAME (CALL_EXPR_FN (pack));
4264 else
4265 name = DECL_NAME (pack);
4266
4267 if (name)
4268 inform (loc, " %qD", name);
4269 else
4270 inform (loc, " %s", "<anonymous>");
4271
4272 parameter_packs = TREE_CHAIN (parameter_packs);
4273 }
4274
4275 return true;
4276 }
4277
4278 return false;
4279 }
4280
4281 /* Expand any parameter packs that occur in the template arguments in
4282 ARGS. */
4283 tree
4284 expand_template_argument_pack (tree args)
4285 {
4286 if (args == error_mark_node)
4287 return error_mark_node;
4288
4289 tree result_args = NULL_TREE;
4290 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4291 int num_result_args = -1;
4292 int non_default_args_count = -1;
4293
4294 /* First, determine if we need to expand anything, and the number of
4295 slots we'll need. */
4296 for (in_arg = 0; in_arg < nargs; ++in_arg)
4297 {
4298 tree arg = TREE_VEC_ELT (args, in_arg);
4299 if (arg == NULL_TREE)
4300 return args;
4301 if (ARGUMENT_PACK_P (arg))
4302 {
4303 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4304 if (num_result_args < 0)
4305 num_result_args = in_arg + num_packed;
4306 else
4307 num_result_args += num_packed;
4308 }
4309 else
4310 {
4311 if (num_result_args >= 0)
4312 num_result_args++;
4313 }
4314 }
4315
4316 /* If no expansion is necessary, we're done. */
4317 if (num_result_args < 0)
4318 return args;
4319
4320 /* Expand arguments. */
4321 result_args = make_tree_vec (num_result_args);
4322 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4323 non_default_args_count =
4324 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4325 for (in_arg = 0; in_arg < nargs; ++in_arg)
4326 {
4327 tree arg = TREE_VEC_ELT (args, in_arg);
4328 if (ARGUMENT_PACK_P (arg))
4329 {
4330 tree packed = ARGUMENT_PACK_ARGS (arg);
4331 int i, num_packed = TREE_VEC_LENGTH (packed);
4332 for (i = 0; i < num_packed; ++i, ++out_arg)
4333 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4334 if (non_default_args_count > 0)
4335 non_default_args_count += num_packed - 1;
4336 }
4337 else
4338 {
4339 TREE_VEC_ELT (result_args, out_arg) = arg;
4340 ++out_arg;
4341 }
4342 }
4343 if (non_default_args_count >= 0)
4344 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4345 return result_args;
4346 }
4347
4348 /* Checks if DECL shadows a template parameter.
4349
4350 [temp.local]: A template-parameter shall not be redeclared within its
4351 scope (including nested scopes).
4352
4353 Emits an error and returns TRUE if the DECL shadows a parameter,
4354 returns FALSE otherwise. */
4355
4356 bool
4357 check_template_shadow (tree decl)
4358 {
4359 tree olddecl;
4360
4361 /* If we're not in a template, we can't possibly shadow a template
4362 parameter. */
4363 if (!current_template_parms)
4364 return true;
4365
4366 /* Figure out what we're shadowing. */
4367 decl = OVL_FIRST (decl);
4368 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4369
4370 /* If there's no previous binding for this name, we're not shadowing
4371 anything, let alone a template parameter. */
4372 if (!olddecl)
4373 return true;
4374
4375 /* If we're not shadowing a template parameter, we're done. Note
4376 that OLDDECL might be an OVERLOAD (or perhaps even an
4377 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4378 node. */
4379 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4380 return true;
4381
4382 /* We check for decl != olddecl to avoid bogus errors for using a
4383 name inside a class. We check TPFI to avoid duplicate errors for
4384 inline member templates. */
4385 if (decl == olddecl
4386 || (DECL_TEMPLATE_PARM_P (decl)
4387 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4388 return true;
4389
4390 /* Don't complain about the injected class name, as we've already
4391 complained about the class itself. */
4392 if (DECL_SELF_REFERENCE_P (decl))
4393 return false;
4394
4395 if (DECL_TEMPLATE_PARM_P (decl))
4396 error ("declaration of template parameter %q+D shadows "
4397 "template parameter", decl);
4398 else
4399 error ("declaration of %q+#D shadows template parameter", decl);
4400 inform (DECL_SOURCE_LOCATION (olddecl),
4401 "template parameter %qD declared here", olddecl);
4402 return false;
4403 }
4404
4405 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4406 ORIG_LEVEL, DECL, and TYPE. */
4407
4408 static tree
4409 build_template_parm_index (int index,
4410 int level,
4411 int orig_level,
4412 tree decl,
4413 tree type)
4414 {
4415 tree t = make_node (TEMPLATE_PARM_INDEX);
4416 TEMPLATE_PARM_IDX (t) = index;
4417 TEMPLATE_PARM_LEVEL (t) = level;
4418 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4419 TEMPLATE_PARM_DECL (t) = decl;
4420 TREE_TYPE (t) = type;
4421 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4422 TREE_READONLY (t) = TREE_READONLY (decl);
4423
4424 return t;
4425 }
4426
4427 /* Find the canonical type parameter for the given template type
4428 parameter. Returns the canonical type parameter, which may be TYPE
4429 if no such parameter existed. */
4430
4431 static tree
4432 canonical_type_parameter (tree type)
4433 {
4434 int idx = TEMPLATE_TYPE_IDX (type);
4435
4436 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4437
4438 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4439 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4440
4441 for (tree list = (*canonical_template_parms)[idx];
4442 list; list = TREE_CHAIN (list))
4443 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4444 return TREE_VALUE (list);
4445
4446 (*canonical_template_parms)[idx]
4447 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4448 return type;
4449 }
4450
4451 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4452 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4453 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4454 new one is created. */
4455
4456 static tree
4457 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4458 tsubst_flags_t complain)
4459 {
4460 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4461 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4462 != TEMPLATE_PARM_LEVEL (index) - levels)
4463 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4464 {
4465 tree orig_decl = TEMPLATE_PARM_DECL (index);
4466
4467 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4468 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4469 type);
4470 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4471 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4472 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4473 DECL_ARTIFICIAL (decl) = 1;
4474 SET_DECL_TEMPLATE_PARM_P (decl);
4475
4476 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4477 TEMPLATE_PARM_LEVEL (index) - levels,
4478 TEMPLATE_PARM_ORIG_LEVEL (index),
4479 decl, type);
4480 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4481 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4482 = TEMPLATE_PARM_PARAMETER_PACK (index);
4483
4484 /* Template template parameters need this. */
4485 tree inner = decl;
4486 if (TREE_CODE (decl) == TEMPLATE_DECL)
4487 {
4488 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4489 TYPE_DECL, DECL_NAME (decl), type);
4490 DECL_TEMPLATE_RESULT (decl) = inner;
4491 DECL_ARTIFICIAL (inner) = true;
4492 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4493 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4494 }
4495
4496 /* Attach the TPI to the decl. */
4497 if (TREE_CODE (inner) == TYPE_DECL)
4498 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4499 else
4500 DECL_INITIAL (decl) = tpi;
4501 }
4502
4503 return TEMPLATE_PARM_DESCENDANTS (index);
4504 }
4505
4506 /* Process information from new template parameter PARM and append it
4507 to the LIST being built. This new parameter is a non-type
4508 parameter iff IS_NON_TYPE is true. This new parameter is a
4509 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4510 is in PARM_LOC. */
4511
4512 tree
4513 process_template_parm (tree list, location_t parm_loc, tree parm,
4514 bool is_non_type, bool is_parameter_pack)
4515 {
4516 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4517 tree prev = NULL_TREE;
4518 int idx = 0;
4519
4520 if (list)
4521 {
4522 prev = tree_last (list);
4523
4524 tree p = TREE_VALUE (prev);
4525 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4526 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4527 else if (TREE_CODE (p) == PARM_DECL)
4528 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4529
4530 ++idx;
4531 }
4532
4533 tree decl = NULL_TREE;
4534 tree defval = TREE_PURPOSE (parm);
4535 tree constr = TREE_TYPE (parm);
4536
4537 if (is_non_type)
4538 {
4539 parm = TREE_VALUE (parm);
4540
4541 SET_DECL_TEMPLATE_PARM_P (parm);
4542
4543 if (TREE_TYPE (parm) != error_mark_node)
4544 {
4545 /* [temp.param]
4546
4547 The top-level cv-qualifiers on the template-parameter are
4548 ignored when determining its type. */
4549 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4550 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4551 TREE_TYPE (parm) = error_mark_node;
4552 else if (uses_parameter_packs (TREE_TYPE (parm))
4553 && !is_parameter_pack
4554 /* If we're in a nested template parameter list, the template
4555 template parameter could be a parameter pack. */
4556 && processing_template_parmlist == 1)
4557 {
4558 /* This template parameter is not a parameter pack, but it
4559 should be. Complain about "bare" parameter packs. */
4560 check_for_bare_parameter_packs (TREE_TYPE (parm));
4561
4562 /* Recover by calling this a parameter pack. */
4563 is_parameter_pack = true;
4564 }
4565 }
4566
4567 /* A template parameter is not modifiable. */
4568 TREE_CONSTANT (parm) = 1;
4569 TREE_READONLY (parm) = 1;
4570 decl = build_decl (parm_loc,
4571 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4572 TREE_CONSTANT (decl) = 1;
4573 TREE_READONLY (decl) = 1;
4574 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4575 = build_template_parm_index (idx, processing_template_decl,
4576 processing_template_decl,
4577 decl, TREE_TYPE (parm));
4578
4579 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4580 = is_parameter_pack;
4581 }
4582 else
4583 {
4584 tree t;
4585 parm = TREE_VALUE (TREE_VALUE (parm));
4586
4587 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4588 {
4589 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4590 /* This is for distinguishing between real templates and template
4591 template parameters */
4592 TREE_TYPE (parm) = t;
4593
4594 /* any_template_parm_r expects to be able to get the targs of a
4595 DECL_TEMPLATE_RESULT. */
4596 tree result = DECL_TEMPLATE_RESULT (parm);
4597 TREE_TYPE (result) = t;
4598 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4599 tree tinfo = build_template_info (parm, args);
4600 retrofit_lang_decl (result);
4601 DECL_TEMPLATE_INFO (result) = tinfo;
4602
4603 decl = parm;
4604 }
4605 else
4606 {
4607 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4608 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4609 decl = build_decl (parm_loc,
4610 TYPE_DECL, parm, t);
4611 }
4612
4613 TYPE_NAME (t) = decl;
4614 TYPE_STUB_DECL (t) = decl;
4615 parm = decl;
4616 TEMPLATE_TYPE_PARM_INDEX (t)
4617 = build_template_parm_index (idx, processing_template_decl,
4618 processing_template_decl,
4619 decl, TREE_TYPE (parm));
4620 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4621 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4622 SET_TYPE_STRUCTURAL_EQUALITY (t);
4623 else
4624 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4625 }
4626 DECL_ARTIFICIAL (decl) = 1;
4627 SET_DECL_TEMPLATE_PARM_P (decl);
4628
4629 /* Build requirements for the type/template parameter.
4630 This must be done after SET_DECL_TEMPLATE_PARM_P or
4631 process_template_parm could fail. */
4632 tree reqs = finish_shorthand_constraint (parm, constr);
4633
4634 decl = pushdecl (decl);
4635 if (!is_non_type)
4636 parm = decl;
4637
4638 /* Build the parameter node linking the parameter declaration,
4639 its default argument (if any), and its constraints (if any). */
4640 parm = build_tree_list (defval, parm);
4641 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4642
4643 if (prev)
4644 TREE_CHAIN (prev) = parm;
4645 else
4646 list = parm;
4647
4648 return list;
4649 }
4650
4651 /* The end of a template parameter list has been reached. Process the
4652 tree list into a parameter vector, converting each parameter into a more
4653 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4654 as PARM_DECLs. */
4655
4656 tree
4657 end_template_parm_list (tree parms)
4658 {
4659 tree saved_parmlist = make_tree_vec (list_length (parms));
4660
4661 /* Pop the dummy parameter level and add the real one. We do not
4662 morph the dummy parameter in place, as it might have been
4663 captured by a (nested) template-template-parm. */
4664 current_template_parms = TREE_CHAIN (current_template_parms);
4665
4666 current_template_parms
4667 = tree_cons (size_int (processing_template_decl),
4668 saved_parmlist, current_template_parms);
4669
4670 for (unsigned ix = 0; parms; ix++)
4671 {
4672 tree parm = parms;
4673 parms = TREE_CHAIN (parms);
4674 TREE_CHAIN (parm) = NULL_TREE;
4675
4676 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4677 }
4678
4679 --processing_template_parmlist;
4680
4681 return saved_parmlist;
4682 }
4683
4684 // Explicitly indicate the end of the template parameter list. We assume
4685 // that the current template parameters have been constructed and/or
4686 // managed explicitly, as when creating new template template parameters
4687 // from a shorthand constraint.
4688 void
4689 end_template_parm_list ()
4690 {
4691 --processing_template_parmlist;
4692 }
4693
4694 /* end_template_decl is called after a template declaration is seen. */
4695
4696 void
4697 end_template_decl (void)
4698 {
4699 reset_specialization ();
4700
4701 if (! processing_template_decl)
4702 return;
4703
4704 /* This matches the pushlevel in begin_template_parm_list. */
4705 finish_scope ();
4706
4707 --processing_template_decl;
4708 current_template_parms = TREE_CHAIN (current_template_parms);
4709 }
4710
4711 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4712 thereof, and converts it into an argument suitable to be passed to
4713 the type substitution functions. Note that if the TREE_LIST contains
4714 an error_mark node, the returned argument is error_mark_node. */
4715
4716 tree
4717 template_parm_to_arg (tree t)
4718 {
4719 if (!t)
4720 return NULL_TREE;
4721
4722 if (TREE_CODE (t) == TREE_LIST)
4723 t = TREE_VALUE (t);
4724
4725 if (error_operand_p (t))
4726 return error_mark_node;
4727
4728 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4729 {
4730 if (TREE_CODE (t) == TYPE_DECL
4731 || TREE_CODE (t) == TEMPLATE_DECL)
4732 t = TREE_TYPE (t);
4733 else
4734 t = DECL_INITIAL (t);
4735 }
4736
4737 gcc_assert (TEMPLATE_PARM_P (t));
4738
4739 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4740 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4741 {
4742 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4743 {
4744 /* Turn this argument into a TYPE_ARGUMENT_PACK
4745 with a single element, which expands T. */
4746 tree vec = make_tree_vec (1);
4747 if (CHECKING_P)
4748 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4749
4750 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4751
4752 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4753 SET_ARGUMENT_PACK_ARGS (t, vec);
4754 }
4755 }
4756 else
4757 {
4758 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4759 {
4760 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4761 with a single element, which expands T. */
4762 tree vec = make_tree_vec (1);
4763 if (CHECKING_P)
4764 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4765
4766 t = convert_from_reference (t);
4767 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4768
4769 t = make_node (NONTYPE_ARGUMENT_PACK);
4770 SET_ARGUMENT_PACK_ARGS (t, vec);
4771 }
4772 else
4773 t = convert_from_reference (t);
4774 }
4775 return t;
4776 }
4777
4778 /* Given a single level of template parameters (a TREE_VEC), return it
4779 as a set of template arguments. */
4780
4781 tree
4782 template_parms_level_to_args (tree parms)
4783 {
4784 tree a = copy_node (parms);
4785 TREE_TYPE (a) = NULL_TREE;
4786 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4787 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4788
4789 if (CHECKING_P)
4790 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4791
4792 return a;
4793 }
4794
4795 /* Given a set of template parameters, return them as a set of template
4796 arguments. The template parameters are represented as a TREE_VEC, in
4797 the form documented in cp-tree.h for template arguments. */
4798
4799 tree
4800 template_parms_to_args (tree parms)
4801 {
4802 tree header;
4803 tree args = NULL_TREE;
4804 int length = TMPL_PARMS_DEPTH (parms);
4805 int l = length;
4806
4807 /* If there is only one level of template parameters, we do not
4808 create a TREE_VEC of TREE_VECs. Instead, we return a single
4809 TREE_VEC containing the arguments. */
4810 if (length > 1)
4811 args = make_tree_vec (length);
4812
4813 for (header = parms; header; header = TREE_CHAIN (header))
4814 {
4815 tree a = template_parms_level_to_args (TREE_VALUE (header));
4816
4817 if (length > 1)
4818 TREE_VEC_ELT (args, --l) = a;
4819 else
4820 args = a;
4821 }
4822
4823 return args;
4824 }
4825
4826 /* Within the declaration of a template, return the currently active
4827 template parameters as an argument TREE_VEC. */
4828
4829 static tree
4830 current_template_args (void)
4831 {
4832 return template_parms_to_args (current_template_parms);
4833 }
4834
4835 /* Return the fully generic arguments for of TMPL, i.e. what
4836 current_template_args would be while parsing it. */
4837
4838 tree
4839 generic_targs_for (tree tmpl)
4840 {
4841 if (tmpl == NULL_TREE)
4842 return NULL_TREE;
4843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4844 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4845 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4846 template parameter, it has no TEMPLATE_INFO; for a partial
4847 specialization, it has the arguments for the primary template, and we
4848 want the arguments for the partial specialization. */;
4849 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4850 if (tree ti = get_template_info (result))
4851 return TI_ARGS (ti);
4852 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4853 }
4854
4855 /* Update the declared TYPE by doing any lookups which were thought to be
4856 dependent, but are not now that we know the SCOPE of the declarator. */
4857
4858 tree
4859 maybe_update_decl_type (tree orig_type, tree scope)
4860 {
4861 tree type = orig_type;
4862
4863 if (type == NULL_TREE)
4864 return type;
4865
4866 if (TREE_CODE (orig_type) == TYPE_DECL)
4867 type = TREE_TYPE (type);
4868
4869 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4870 && dependent_type_p (type)
4871 /* Don't bother building up the args in this case. */
4872 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4873 {
4874 /* tsubst in the args corresponding to the template parameters,
4875 including auto if present. Most things will be unchanged, but
4876 make_typename_type and tsubst_qualified_id will resolve
4877 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4878 tree args = current_template_args ();
4879 tree auto_node = type_uses_auto (type);
4880 tree pushed;
4881 if (auto_node)
4882 {
4883 tree auto_vec = make_tree_vec (1);
4884 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4885 args = add_to_template_args (args, auto_vec);
4886 }
4887 pushed = push_scope (scope);
4888 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4889 if (pushed)
4890 pop_scope (scope);
4891 }
4892
4893 if (type == error_mark_node)
4894 return orig_type;
4895
4896 if (TREE_CODE (orig_type) == TYPE_DECL)
4897 {
4898 if (same_type_p (type, TREE_TYPE (orig_type)))
4899 type = orig_type;
4900 else
4901 type = TYPE_NAME (type);
4902 }
4903 return type;
4904 }
4905
4906 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4907 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4908 the new template is a member template. */
4909
4910 static tree
4911 build_template_decl (tree decl, tree parms, bool member_template_p)
4912 {
4913 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4914 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4915 DECL_TEMPLATE_PARMS (tmpl) = parms;
4916 DECL_TEMPLATE_RESULT (tmpl) = decl;
4917 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4918 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4919 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4920 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4921
4922 return tmpl;
4923 }
4924
4925 struct template_parm_data
4926 {
4927 /* The level of the template parameters we are currently
4928 processing. */
4929 int level;
4930
4931 /* The index of the specialization argument we are currently
4932 processing. */
4933 int current_arg;
4934
4935 /* An array whose size is the number of template parameters. The
4936 elements are nonzero if the parameter has been used in any one
4937 of the arguments processed so far. */
4938 int* parms;
4939
4940 /* An array whose size is the number of template arguments. The
4941 elements are nonzero if the argument makes use of template
4942 parameters of this level. */
4943 int* arg_uses_template_parms;
4944 };
4945
4946 /* Subroutine of push_template_decl used to see if each template
4947 parameter in a partial specialization is used in the explicit
4948 argument list. If T is of the LEVEL given in DATA (which is
4949 treated as a template_parm_data*), then DATA->PARMS is marked
4950 appropriately. */
4951
4952 static int
4953 mark_template_parm (tree t, void* data)
4954 {
4955 int level;
4956 int idx;
4957 struct template_parm_data* tpd = (struct template_parm_data*) data;
4958
4959 template_parm_level_and_index (t, &level, &idx);
4960
4961 if (level == tpd->level)
4962 {
4963 tpd->parms[idx] = 1;
4964 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4965 }
4966
4967 /* In C++17 the type of a non-type argument is a deduced context. */
4968 if (cxx_dialect >= cxx17
4969 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4970 for_each_template_parm (TREE_TYPE (t),
4971 &mark_template_parm,
4972 data,
4973 NULL,
4974 /*include_nondeduced_p=*/false);
4975
4976 /* Return zero so that for_each_template_parm will continue the
4977 traversal of the tree; we want to mark *every* template parm. */
4978 return 0;
4979 }
4980
4981 /* Process the partial specialization DECL. */
4982
4983 static tree
4984 process_partial_specialization (tree decl)
4985 {
4986 tree type = TREE_TYPE (decl);
4987 tree tinfo = get_template_info (decl);
4988 tree maintmpl = TI_TEMPLATE (tinfo);
4989 tree specargs = TI_ARGS (tinfo);
4990 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4991 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4992 tree inner_parms;
4993 tree inst;
4994 int nargs = TREE_VEC_LENGTH (inner_args);
4995 int ntparms;
4996 int i;
4997 bool did_error_intro = false;
4998 struct template_parm_data tpd;
4999 struct template_parm_data tpd2;
5000
5001 gcc_assert (current_template_parms);
5002
5003 /* A concept cannot be specialized. */
5004 if (flag_concepts && variable_concept_p (maintmpl))
5005 {
5006 error ("specialization of variable concept %q#D", maintmpl);
5007 return error_mark_node;
5008 }
5009
5010 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5011 ntparms = TREE_VEC_LENGTH (inner_parms);
5012
5013 /* We check that each of the template parameters given in the
5014 partial specialization is used in the argument list to the
5015 specialization. For example:
5016
5017 template <class T> struct S;
5018 template <class T> struct S<T*>;
5019
5020 The second declaration is OK because `T*' uses the template
5021 parameter T, whereas
5022
5023 template <class T> struct S<int>;
5024
5025 is no good. Even trickier is:
5026
5027 template <class T>
5028 struct S1
5029 {
5030 template <class U>
5031 struct S2;
5032 template <class U>
5033 struct S2<T>;
5034 };
5035
5036 The S2<T> declaration is actually invalid; it is a
5037 full-specialization. Of course,
5038
5039 template <class U>
5040 struct S2<T (*)(U)>;
5041
5042 or some such would have been OK. */
5043 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5044 tpd.parms = XALLOCAVEC (int, ntparms);
5045 memset (tpd.parms, 0, sizeof (int) * ntparms);
5046
5047 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5048 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5049 for (i = 0; i < nargs; ++i)
5050 {
5051 tpd.current_arg = i;
5052 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5053 &mark_template_parm,
5054 &tpd,
5055 NULL,
5056 /*include_nondeduced_p=*/false);
5057 }
5058 for (i = 0; i < ntparms; ++i)
5059 if (tpd.parms[i] == 0)
5060 {
5061 /* One of the template parms was not used in a deduced context in the
5062 specialization. */
5063 if (!did_error_intro)
5064 {
5065 error ("template parameters not deducible in "
5066 "partial specialization:");
5067 did_error_intro = true;
5068 }
5069
5070 inform (input_location, " %qD",
5071 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5072 }
5073
5074 if (did_error_intro)
5075 return error_mark_node;
5076
5077 /* [temp.class.spec]
5078
5079 The argument list of the specialization shall not be identical to
5080 the implicit argument list of the primary template. */
5081 tree main_args
5082 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5083 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5084 && (!flag_concepts
5085 || !strictly_subsumes (current_template_constraints (),
5086 main_args, maintmpl)))
5087 {
5088 if (!flag_concepts)
5089 error ("partial specialization %q+D does not specialize "
5090 "any template arguments; to define the primary template, "
5091 "remove the template argument list", decl);
5092 else
5093 error ("partial specialization %q+D does not specialize any "
5094 "template arguments and is not more constrained than "
5095 "the primary template; to define the primary template, "
5096 "remove the template argument list", decl);
5097 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5098 }
5099
5100 /* A partial specialization that replaces multiple parameters of the
5101 primary template with a pack expansion is less specialized for those
5102 parameters. */
5103 if (nargs < DECL_NTPARMS (maintmpl))
5104 {
5105 error ("partial specialization is not more specialized than the "
5106 "primary template because it replaces multiple parameters "
5107 "with a pack expansion");
5108 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5109 /* Avoid crash in process_partial_specialization. */
5110 return decl;
5111 }
5112
5113 else if (nargs > DECL_NTPARMS (maintmpl))
5114 {
5115 error ("too many arguments for partial specialization %qT", type);
5116 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5117 /* Avoid crash below. */
5118 return decl;
5119 }
5120
5121 /* If we aren't in a dependent class, we can actually try deduction. */
5122 else if (tpd.level == 1
5123 /* FIXME we should be able to handle a partial specialization of a
5124 partial instantiation, but currently we can't (c++/41727). */
5125 && TMPL_ARGS_DEPTH (specargs) == 1
5126 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5127 {
5128 auto_diagnostic_group d;
5129 if (permerror (input_location, "partial specialization %qD is not "
5130 "more specialized than", decl))
5131 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5132 maintmpl);
5133 }
5134
5135 /* [temp.class.spec]
5136
5137 A partially specialized non-type argument expression shall not
5138 involve template parameters of the partial specialization except
5139 when the argument expression is a simple identifier.
5140
5141 The type of a template parameter corresponding to a specialized
5142 non-type argument shall not be dependent on a parameter of the
5143 specialization.
5144
5145 Also, we verify that pack expansions only occur at the
5146 end of the argument list. */
5147 tpd2.parms = 0;
5148 for (i = 0; i < nargs; ++i)
5149 {
5150 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5151 tree arg = TREE_VEC_ELT (inner_args, i);
5152 tree packed_args = NULL_TREE;
5153 int j, len = 1;
5154
5155 if (ARGUMENT_PACK_P (arg))
5156 {
5157 /* Extract the arguments from the argument pack. We'll be
5158 iterating over these in the following loop. */
5159 packed_args = ARGUMENT_PACK_ARGS (arg);
5160 len = TREE_VEC_LENGTH (packed_args);
5161 }
5162
5163 for (j = 0; j < len; j++)
5164 {
5165 if (packed_args)
5166 /* Get the Jth argument in the parameter pack. */
5167 arg = TREE_VEC_ELT (packed_args, j);
5168
5169 if (PACK_EXPANSION_P (arg))
5170 {
5171 /* Pack expansions must come at the end of the
5172 argument list. */
5173 if ((packed_args && j < len - 1)
5174 || (!packed_args && i < nargs - 1))
5175 {
5176 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5177 error ("parameter pack argument %qE must be at the "
5178 "end of the template argument list", arg);
5179 else
5180 error ("parameter pack argument %qT must be at the "
5181 "end of the template argument list", arg);
5182 }
5183 }
5184
5185 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5186 /* We only care about the pattern. */
5187 arg = PACK_EXPANSION_PATTERN (arg);
5188
5189 if (/* These first two lines are the `non-type' bit. */
5190 !TYPE_P (arg)
5191 && TREE_CODE (arg) != TEMPLATE_DECL
5192 /* This next two lines are the `argument expression is not just a
5193 simple identifier' condition and also the `specialized
5194 non-type argument' bit. */
5195 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5196 && !((REFERENCE_REF_P (arg)
5197 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5198 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5199 {
5200 if ((!packed_args && tpd.arg_uses_template_parms[i])
5201 || (packed_args && uses_template_parms (arg)))
5202 error_at (cp_expr_loc_or_input_loc (arg),
5203 "template argument %qE involves template "
5204 "parameter(s)", arg);
5205 else
5206 {
5207 /* Look at the corresponding template parameter,
5208 marking which template parameters its type depends
5209 upon. */
5210 tree type = TREE_TYPE (parm);
5211
5212 if (!tpd2.parms)
5213 {
5214 /* We haven't yet initialized TPD2. Do so now. */
5215 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5216 /* The number of parameters here is the number in the
5217 main template, which, as checked in the assertion
5218 above, is NARGS. */
5219 tpd2.parms = XALLOCAVEC (int, nargs);
5220 tpd2.level =
5221 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5222 }
5223
5224 /* Mark the template parameters. But this time, we're
5225 looking for the template parameters of the main
5226 template, not in the specialization. */
5227 tpd2.current_arg = i;
5228 tpd2.arg_uses_template_parms[i] = 0;
5229 memset (tpd2.parms, 0, sizeof (int) * nargs);
5230 for_each_template_parm (type,
5231 &mark_template_parm,
5232 &tpd2,
5233 NULL,
5234 /*include_nondeduced_p=*/false);
5235
5236 if (tpd2.arg_uses_template_parms [i])
5237 {
5238 /* The type depended on some template parameters.
5239 If they are fully specialized in the
5240 specialization, that's OK. */
5241 int j;
5242 int count = 0;
5243 for (j = 0; j < nargs; ++j)
5244 if (tpd2.parms[j] != 0
5245 && tpd.arg_uses_template_parms [j])
5246 ++count;
5247 if (count != 0)
5248 error_n (input_location, count,
5249 "type %qT of template argument %qE depends "
5250 "on a template parameter",
5251 "type %qT of template argument %qE depends "
5252 "on template parameters",
5253 type,
5254 arg);
5255 }
5256 }
5257 }
5258 }
5259 }
5260
5261 /* We should only get here once. */
5262 if (TREE_CODE (decl) == TYPE_DECL)
5263 gcc_assert (!COMPLETE_TYPE_P (type));
5264
5265 // Build the template decl.
5266 tree tmpl = build_template_decl (decl, current_template_parms,
5267 DECL_MEMBER_TEMPLATE_P (maintmpl));
5268 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5269 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5270 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5271
5272 /* Give template template parms a DECL_CONTEXT of the template
5273 for which they are a parameter. */
5274 for (i = 0; i < ntparms; ++i)
5275 {
5276 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5277 if (TREE_CODE (parm) == TEMPLATE_DECL)
5278 DECL_CONTEXT (parm) = tmpl;
5279 }
5280
5281 if (VAR_P (decl))
5282 /* We didn't register this in check_explicit_specialization so we could
5283 wait until the constraints were set. */
5284 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5285 else
5286 associate_classtype_constraints (type);
5287
5288 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5289 = tree_cons (specargs, tmpl,
5290 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5291 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5292
5293 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5294 inst = TREE_CHAIN (inst))
5295 {
5296 tree instance = TREE_VALUE (inst);
5297 if (TYPE_P (instance)
5298 ? (COMPLETE_TYPE_P (instance)
5299 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5300 : DECL_TEMPLATE_INSTANTIATION (instance))
5301 {
5302 tree spec = most_specialized_partial_spec (instance, tf_none);
5303 tree inst_decl = (DECL_P (instance)
5304 ? instance : TYPE_NAME (instance));
5305 if (!spec)
5306 /* OK */;
5307 else if (spec == error_mark_node)
5308 permerror (input_location,
5309 "declaration of %qD ambiguates earlier template "
5310 "instantiation for %qD", decl, inst_decl);
5311 else if (TREE_VALUE (spec) == tmpl)
5312 permerror (input_location,
5313 "partial specialization of %qD after instantiation "
5314 "of %qD", decl, inst_decl);
5315 }
5316 }
5317
5318 return decl;
5319 }
5320
5321 /* PARM is a template parameter of some form; return the corresponding
5322 TEMPLATE_PARM_INDEX. */
5323
5324 static tree
5325 get_template_parm_index (tree parm)
5326 {
5327 if (TREE_CODE (parm) == PARM_DECL
5328 || TREE_CODE (parm) == CONST_DECL)
5329 parm = DECL_INITIAL (parm);
5330 else if (TREE_CODE (parm) == TYPE_DECL
5331 || TREE_CODE (parm) == TEMPLATE_DECL)
5332 parm = TREE_TYPE (parm);
5333 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5334 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5335 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5336 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5337 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5338 return parm;
5339 }
5340
5341 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5342 parameter packs used by the template parameter PARM. */
5343
5344 static void
5345 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5346 {
5347 /* A type parm can't refer to another parm. */
5348 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5349 return;
5350 else if (TREE_CODE (parm) == PARM_DECL)
5351 {
5352 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5353 ppd, ppd->visited);
5354 return;
5355 }
5356
5357 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5358
5359 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5360 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5361 {
5362 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5363 if (template_parameter_pack_p (p))
5364 /* Any packs in the type are expanded by this parameter. */;
5365 else
5366 fixed_parameter_pack_p_1 (p, ppd);
5367 }
5368 }
5369
5370 /* PARM is a template parameter pack. Return any parameter packs used in
5371 its type or the type of any of its template parameters. If there are
5372 any such packs, it will be instantiated into a fixed template parameter
5373 list by partial instantiation rather than be fully deduced. */
5374
5375 tree
5376 fixed_parameter_pack_p (tree parm)
5377 {
5378 /* This can only be true in a member template. */
5379 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5380 return NULL_TREE;
5381 /* This can only be true for a parameter pack. */
5382 if (!template_parameter_pack_p (parm))
5383 return NULL_TREE;
5384 /* A type parm can't refer to another parm. */
5385 if (TREE_CODE (parm) == TYPE_DECL)
5386 return NULL_TREE;
5387
5388 tree parameter_packs = NULL_TREE;
5389 struct find_parameter_pack_data ppd;
5390 ppd.parameter_packs = &parameter_packs;
5391 ppd.visited = new hash_set<tree>;
5392 ppd.type_pack_expansion_p = false;
5393
5394 fixed_parameter_pack_p_1 (parm, &ppd);
5395
5396 delete ppd.visited;
5397 return parameter_packs;
5398 }
5399
5400 /* Check that a template declaration's use of default arguments and
5401 parameter packs is not invalid. Here, PARMS are the template
5402 parameters. IS_PRIMARY is true if DECL is the thing declared by
5403 a primary template. IS_PARTIAL is true if DECL is a partial
5404 specialization.
5405
5406 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5407 function template declaration or a friend class template
5408 declaration. In the function case, 1 indicates a declaration, 2
5409 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5410 emitted for extraneous default arguments.
5411
5412 Returns TRUE if there were no errors found, FALSE otherwise. */
5413
5414 bool
5415 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5416 bool is_partial, int is_friend_decl)
5417 {
5418 const char *msg;
5419 int last_level_to_check;
5420 tree parm_level;
5421 bool no_errors = true;
5422
5423 /* [temp.param]
5424
5425 A default template-argument shall not be specified in a
5426 function template declaration or a function template definition, nor
5427 in the template-parameter-list of the definition of a member of a
5428 class template. */
5429
5430 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5431 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5432 /* You can't have a function template declaration in a local
5433 scope, nor you can you define a member of a class template in a
5434 local scope. */
5435 return true;
5436
5437 if ((TREE_CODE (decl) == TYPE_DECL
5438 && TREE_TYPE (decl)
5439 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5440 || (TREE_CODE (decl) == FUNCTION_DECL
5441 && LAMBDA_FUNCTION_P (decl)))
5442 /* A lambda doesn't have an explicit declaration; don't complain
5443 about the parms of the enclosing class. */
5444 return true;
5445
5446 if (current_class_type
5447 && !TYPE_BEING_DEFINED (current_class_type)
5448 && DECL_LANG_SPECIFIC (decl)
5449 && DECL_DECLARES_FUNCTION_P (decl)
5450 /* If this is either a friend defined in the scope of the class
5451 or a member function. */
5452 && (DECL_FUNCTION_MEMBER_P (decl)
5453 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5454 : DECL_FRIEND_CONTEXT (decl)
5455 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5456 : false)
5457 /* And, if it was a member function, it really was defined in
5458 the scope of the class. */
5459 && (!DECL_FUNCTION_MEMBER_P (decl)
5460 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5461 /* We already checked these parameters when the template was
5462 declared, so there's no need to do it again now. This function
5463 was defined in class scope, but we're processing its body now
5464 that the class is complete. */
5465 return true;
5466
5467 /* Core issue 226 (C++0x only): the following only applies to class
5468 templates. */
5469 if (is_primary
5470 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5471 {
5472 /* [temp.param]
5473
5474 If a template-parameter has a default template-argument, all
5475 subsequent template-parameters shall have a default
5476 template-argument supplied. */
5477 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5478 {
5479 tree inner_parms = TREE_VALUE (parm_level);
5480 int ntparms = TREE_VEC_LENGTH (inner_parms);
5481 int seen_def_arg_p = 0;
5482 int i;
5483
5484 for (i = 0; i < ntparms; ++i)
5485 {
5486 tree parm = TREE_VEC_ELT (inner_parms, i);
5487
5488 if (parm == error_mark_node)
5489 continue;
5490
5491 if (TREE_PURPOSE (parm))
5492 seen_def_arg_p = 1;
5493 else if (seen_def_arg_p
5494 && !template_parameter_pack_p (TREE_VALUE (parm)))
5495 {
5496 error ("no default argument for %qD", TREE_VALUE (parm));
5497 /* For better subsequent error-recovery, we indicate that
5498 there should have been a default argument. */
5499 TREE_PURPOSE (parm) = error_mark_node;
5500 no_errors = false;
5501 }
5502 else if (!is_partial
5503 && !is_friend_decl
5504 /* Don't complain about an enclosing partial
5505 specialization. */
5506 && parm_level == parms
5507 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5508 && i < ntparms - 1
5509 && template_parameter_pack_p (TREE_VALUE (parm))
5510 /* A fixed parameter pack will be partially
5511 instantiated into a fixed length list. */
5512 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5513 {
5514 /* A primary class template, primary variable template
5515 (DR 2032), or alias template can only have one
5516 parameter pack, at the end of the template
5517 parameter list. */
5518
5519 error ("parameter pack %q+D must be at the end of the"
5520 " template parameter list", TREE_VALUE (parm));
5521
5522 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5523 = error_mark_node;
5524 no_errors = false;
5525 }
5526 }
5527 }
5528 }
5529
5530 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5531 || is_partial
5532 || !is_primary
5533 || is_friend_decl)
5534 /* For an ordinary class template, default template arguments are
5535 allowed at the innermost level, e.g.:
5536 template <class T = int>
5537 struct S {};
5538 but, in a partial specialization, they're not allowed even
5539 there, as we have in [temp.class.spec]:
5540
5541 The template parameter list of a specialization shall not
5542 contain default template argument values.
5543
5544 So, for a partial specialization, or for a function template
5545 (in C++98/C++03), we look at all of them. */
5546 ;
5547 else
5548 /* But, for a primary class template that is not a partial
5549 specialization we look at all template parameters except the
5550 innermost ones. */
5551 parms = TREE_CHAIN (parms);
5552
5553 /* Figure out what error message to issue. */
5554 if (is_friend_decl == 2)
5555 msg = G_("default template arguments may not be used in function template "
5556 "friend re-declaration");
5557 else if (is_friend_decl)
5558 msg = G_("default template arguments may not be used in template "
5559 "friend declarations");
5560 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5561 msg = G_("default template arguments may not be used in function templates "
5562 "without %<-std=c++11%> or %<-std=gnu++11%>");
5563 else if (is_partial)
5564 msg = G_("default template arguments may not be used in "
5565 "partial specializations");
5566 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5567 msg = G_("default argument for template parameter for class enclosing %qD");
5568 else
5569 /* Per [temp.param]/9, "A default template-argument shall not be
5570 specified in the template-parameter-lists of the definition of
5571 a member of a class template that appears outside of the member's
5572 class.", thus if we aren't handling a member of a class template
5573 there is no need to examine the parameters. */
5574 return true;
5575
5576 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5577 /* If we're inside a class definition, there's no need to
5578 examine the parameters to the class itself. On the one
5579 hand, they will be checked when the class is defined, and,
5580 on the other, default arguments are valid in things like:
5581 template <class T = double>
5582 struct S { template <class U> void f(U); };
5583 Here the default argument for `S' has no bearing on the
5584 declaration of `f'. */
5585 last_level_to_check = template_class_depth (current_class_type) + 1;
5586 else
5587 /* Check everything. */
5588 last_level_to_check = 0;
5589
5590 for (parm_level = parms;
5591 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5592 parm_level = TREE_CHAIN (parm_level))
5593 {
5594 tree inner_parms = TREE_VALUE (parm_level);
5595 int i;
5596 int ntparms;
5597
5598 ntparms = TREE_VEC_LENGTH (inner_parms);
5599 for (i = 0; i < ntparms; ++i)
5600 {
5601 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5602 continue;
5603
5604 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5605 {
5606 if (msg)
5607 {
5608 no_errors = false;
5609 if (is_friend_decl == 2)
5610 return no_errors;
5611
5612 error (msg, decl);
5613 msg = 0;
5614 }
5615
5616 /* Clear out the default argument so that we are not
5617 confused later. */
5618 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5619 }
5620 }
5621
5622 /* At this point, if we're still interested in issuing messages,
5623 they must apply to classes surrounding the object declared. */
5624 if (msg)
5625 msg = G_("default argument for template parameter for class "
5626 "enclosing %qD");
5627 }
5628
5629 return no_errors;
5630 }
5631
5632 /* Worker for push_template_decl_real, called via
5633 for_each_template_parm. DATA is really an int, indicating the
5634 level of the parameters we are interested in. If T is a template
5635 parameter of that level, return nonzero. */
5636
5637 static int
5638 template_parm_this_level_p (tree t, void* data)
5639 {
5640 int this_level = *(int *)data;
5641 int level;
5642
5643 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5644 level = TEMPLATE_PARM_LEVEL (t);
5645 else
5646 level = TEMPLATE_TYPE_LEVEL (t);
5647 return level == this_level;
5648 }
5649
5650 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5651 DATA is really an int, indicating the innermost outer level of parameters.
5652 If T is a template parameter of that level or further out, return
5653 nonzero. */
5654
5655 static int
5656 template_parm_outer_level (tree t, void *data)
5657 {
5658 int this_level = *(int *)data;
5659 int level;
5660
5661 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5662 level = TEMPLATE_PARM_LEVEL (t);
5663 else
5664 level = TEMPLATE_TYPE_LEVEL (t);
5665 return level <= this_level;
5666 }
5667
5668 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5669 parameters given by current_template_args, or reuses a
5670 previously existing one, if appropriate. Returns the DECL, or an
5671 equivalent one, if it is replaced via a call to duplicate_decls.
5672
5673 If IS_FRIEND is true, DECL is a friend declaration. */
5674
5675 tree
5676 push_template_decl (tree decl, bool is_friend)
5677 {
5678 tree tmpl;
5679 tree args;
5680 tree info;
5681 tree ctx;
5682 bool is_primary;
5683 bool is_partial;
5684 int new_template_p = 0;
5685 /* True if the template is a member template, in the sense of
5686 [temp.mem]. */
5687 bool member_template_p = false;
5688
5689 if (decl == error_mark_node || !current_template_parms)
5690 return error_mark_node;
5691
5692 /* See if this is a partial specialization. */
5693 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5694 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5695 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5696 || (VAR_P (decl)
5697 && DECL_LANG_SPECIFIC (decl)
5698 && DECL_TEMPLATE_SPECIALIZATION (decl)
5699 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5700
5701 /* No surprising friend functions. */
5702 gcc_checking_assert (is_friend
5703 || !(TREE_CODE (decl) == FUNCTION_DECL
5704 && DECL_FRIEND_P (decl)));
5705
5706 if (is_friend)
5707 /* For a friend, we want the context of the friend, not
5708 the type of which it is a friend. */
5709 ctx = CP_DECL_CONTEXT (decl);
5710 else if (CP_DECL_CONTEXT (decl)
5711 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5712 /* In the case of a virtual function, we want the class in which
5713 it is defined. */
5714 ctx = CP_DECL_CONTEXT (decl);
5715 else
5716 /* Otherwise, if we're currently defining some class, the DECL
5717 is assumed to be a member of the class. */
5718 ctx = current_scope ();
5719
5720 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5721 ctx = NULL_TREE;
5722
5723 if (!DECL_CONTEXT (decl))
5724 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5725
5726 /* See if this is a primary template. */
5727 if (is_friend && ctx
5728 && uses_template_parms_level (ctx, processing_template_decl))
5729 /* A friend template that specifies a class context, i.e.
5730 template <typename T> friend void A<T>::f();
5731 is not primary. */
5732 is_primary = false;
5733 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5734 is_primary = false;
5735 else
5736 is_primary = template_parm_scope_p ();
5737
5738 if (is_primary)
5739 {
5740 warning (OPT_Wtemplates, "template %qD declared", decl);
5741
5742 if (DECL_CLASS_SCOPE_P (decl))
5743 member_template_p = true;
5744 if (TREE_CODE (decl) == TYPE_DECL
5745 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5746 {
5747 error ("template class without a name");
5748 return error_mark_node;
5749 }
5750 else if (TREE_CODE (decl) == FUNCTION_DECL)
5751 {
5752 if (member_template_p)
5753 {
5754 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5755 error ("member template %qD may not have virt-specifiers", decl);
5756 }
5757 if (DECL_DESTRUCTOR_P (decl))
5758 {
5759 /* [temp.mem]
5760
5761 A destructor shall not be a member template. */
5762 error_at (DECL_SOURCE_LOCATION (decl),
5763 "destructor %qD declared as member template", decl);
5764 return error_mark_node;
5765 }
5766 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5767 && (!prototype_p (TREE_TYPE (decl))
5768 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5769 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5770 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5771 == void_list_node)))
5772 {
5773 /* [basic.stc.dynamic.allocation]
5774
5775 An allocation function can be a function
5776 template. ... Template allocation functions shall
5777 have two or more parameters. */
5778 error ("invalid template declaration of %qD", decl);
5779 return error_mark_node;
5780 }
5781 }
5782 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5783 && CLASS_TYPE_P (TREE_TYPE (decl)))
5784 {
5785 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5786 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5787 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5788 {
5789 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5790 if (TREE_CODE (t) == TYPE_DECL)
5791 t = TREE_TYPE (t);
5792 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5793 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5794 }
5795 }
5796 else if (TREE_CODE (decl) == TYPE_DECL
5797 && TYPE_DECL_ALIAS_P (decl))
5798 /* alias-declaration */
5799 gcc_assert (!DECL_ARTIFICIAL (decl));
5800 else if (VAR_P (decl))
5801 /* C++14 variable template. */;
5802 else if (TREE_CODE (decl) == CONCEPT_DECL)
5803 /* C++20 concept definitions. */;
5804 else
5805 {
5806 error ("template declaration of %q#D", decl);
5807 return error_mark_node;
5808 }
5809 }
5810
5811 /* Check to see that the rules regarding the use of default
5812 arguments are not being violated. We check args for a friend
5813 functions when we know whether it's a definition, introducing
5814 declaration or re-declaration. */
5815 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5816 check_default_tmpl_args (decl, current_template_parms,
5817 is_primary, is_partial, is_friend);
5818
5819 /* Ensure that there are no parameter packs in the type of this
5820 declaration that have not been expanded. */
5821 if (TREE_CODE (decl) == FUNCTION_DECL)
5822 {
5823 /* Check each of the arguments individually to see if there are
5824 any bare parameter packs. */
5825 tree type = TREE_TYPE (decl);
5826 tree arg = DECL_ARGUMENTS (decl);
5827 tree argtype = TYPE_ARG_TYPES (type);
5828
5829 while (arg && argtype)
5830 {
5831 if (!DECL_PACK_P (arg)
5832 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5833 {
5834 /* This is a PARM_DECL that contains unexpanded parameter
5835 packs. We have already complained about this in the
5836 check_for_bare_parameter_packs call, so just replace
5837 these types with ERROR_MARK_NODE. */
5838 TREE_TYPE (arg) = error_mark_node;
5839 TREE_VALUE (argtype) = error_mark_node;
5840 }
5841
5842 arg = DECL_CHAIN (arg);
5843 argtype = TREE_CHAIN (argtype);
5844 }
5845
5846 /* Check for bare parameter packs in the return type and the
5847 exception specifiers. */
5848 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5849 /* Errors were already issued, set return type to int
5850 as the frontend doesn't expect error_mark_node as
5851 the return type. */
5852 TREE_TYPE (type) = integer_type_node;
5853 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5854 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5855 }
5856 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5857 ? DECL_ORIGINAL_TYPE (decl)
5858 : TREE_TYPE (decl)))
5859 {
5860 TREE_TYPE (decl) = error_mark_node;
5861 return error_mark_node;
5862 }
5863
5864 if (is_partial)
5865 return process_partial_specialization (decl);
5866
5867 args = current_template_args ();
5868
5869 if (!ctx
5870 || TREE_CODE (ctx) == FUNCTION_DECL
5871 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5872 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5873 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5874 {
5875 if (DECL_LANG_SPECIFIC (decl)
5876 && DECL_TEMPLATE_INFO (decl)
5877 && DECL_TI_TEMPLATE (decl))
5878 tmpl = DECL_TI_TEMPLATE (decl);
5879 /* If DECL is a TYPE_DECL for a class-template, then there won't
5880 be DECL_LANG_SPECIFIC. The information equivalent to
5881 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5882 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5883 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5884 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5885 {
5886 /* Since a template declaration already existed for this
5887 class-type, we must be redeclaring it here. Make sure
5888 that the redeclaration is valid. */
5889 redeclare_class_template (TREE_TYPE (decl),
5890 current_template_parms,
5891 current_template_constraints ());
5892 /* We don't need to create a new TEMPLATE_DECL; just use the
5893 one we already had. */
5894 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5895 }
5896 else
5897 {
5898 tmpl = build_template_decl (decl, current_template_parms,
5899 member_template_p);
5900 new_template_p = 1;
5901
5902 if (DECL_LANG_SPECIFIC (decl)
5903 && DECL_TEMPLATE_SPECIALIZATION (decl))
5904 {
5905 /* A specialization of a member template of a template
5906 class. */
5907 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5908 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5909 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5910 }
5911 }
5912 }
5913 else
5914 {
5915 tree a, t, current, parms;
5916 int i;
5917 tree tinfo = get_template_info (decl);
5918
5919 if (!tinfo)
5920 {
5921 error ("template definition of non-template %q#D", decl);
5922 return error_mark_node;
5923 }
5924
5925 tmpl = TI_TEMPLATE (tinfo);
5926
5927 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5928 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5929 && DECL_TEMPLATE_SPECIALIZATION (decl)
5930 && DECL_MEMBER_TEMPLATE_P (tmpl))
5931 {
5932 tree new_tmpl;
5933
5934 /* The declaration is a specialization of a member
5935 template, declared outside the class. Therefore, the
5936 innermost template arguments will be NULL, so we
5937 replace them with the arguments determined by the
5938 earlier call to check_explicit_specialization. */
5939 args = DECL_TI_ARGS (decl);
5940
5941 new_tmpl
5942 = build_template_decl (decl, current_template_parms,
5943 member_template_p);
5944 DECL_TI_TEMPLATE (decl) = new_tmpl;
5945 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5946 DECL_TEMPLATE_INFO (new_tmpl)
5947 = build_template_info (tmpl, args);
5948
5949 register_specialization (new_tmpl,
5950 most_general_template (tmpl),
5951 args,
5952 is_friend, 0);
5953 return decl;
5954 }
5955
5956 /* Make sure the template headers we got make sense. */
5957
5958 parms = DECL_TEMPLATE_PARMS (tmpl);
5959 i = TMPL_PARMS_DEPTH (parms);
5960 if (TMPL_ARGS_DEPTH (args) != i)
5961 {
5962 error ("expected %d levels of template parms for %q#D, got %d",
5963 i, decl, TMPL_ARGS_DEPTH (args));
5964 DECL_INTERFACE_KNOWN (decl) = 1;
5965 return error_mark_node;
5966 }
5967 else
5968 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5969 {
5970 a = TMPL_ARGS_LEVEL (args, i);
5971 t = INNERMOST_TEMPLATE_PARMS (parms);
5972
5973 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5974 {
5975 if (current == decl)
5976 error ("got %d template parameters for %q#D",
5977 TREE_VEC_LENGTH (a), decl);
5978 else
5979 error ("got %d template parameters for %q#T",
5980 TREE_VEC_LENGTH (a), current);
5981 error (" but %d required", TREE_VEC_LENGTH (t));
5982 /* Avoid crash in import_export_decl. */
5983 DECL_INTERFACE_KNOWN (decl) = 1;
5984 return error_mark_node;
5985 }
5986
5987 if (current == decl)
5988 current = ctx;
5989 else if (current == NULL_TREE)
5990 /* Can happen in erroneous input. */
5991 break;
5992 else
5993 current = get_containing_scope (current);
5994 }
5995
5996 /* Check that the parms are used in the appropriate qualifying scopes
5997 in the declarator. */
5998 if (!comp_template_args
5999 (TI_ARGS (tinfo),
6000 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6001 {
6002 error ("template arguments to %qD do not match original "
6003 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6004 if (!uses_template_parms (TI_ARGS (tinfo)))
6005 inform (input_location, "use %<template<>%> for"
6006 " an explicit specialization");
6007 /* Avoid crash in import_export_decl. */
6008 DECL_INTERFACE_KNOWN (decl) = 1;
6009 return error_mark_node;
6010 }
6011 }
6012
6013 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
6014
6015 if (new_template_p)
6016 {
6017 /* Push template declarations for global functions and types.
6018 Note that we do not try to push a global template friend
6019 declared in a template class; such a thing may well depend on
6020 the template parameters of the class and we'll push it when
6021 instantiating the befriending class. */
6022 if (!ctx
6023 && !(is_friend && template_class_depth (current_class_type) > 0))
6024 {
6025 /* Hide template friend classes that haven't been declared yet. */
6026 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6027 DECL_FRIEND_P (tmpl) = 1;
6028
6029 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6030 if (tmpl == error_mark_node)
6031 return error_mark_node;
6032 }
6033 }
6034 else
6035 /* The type may have been completed, or (erroneously) changed. */
6036 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6037
6038 if (is_primary)
6039 {
6040 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6041
6042 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6043
6044 /* Give template template parms a DECL_CONTEXT of the template
6045 for which they are a parameter. */
6046 parms = INNERMOST_TEMPLATE_PARMS (parms);
6047 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6048 {
6049 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6050 if (TREE_CODE (parm) == TEMPLATE_DECL)
6051 DECL_CONTEXT (parm) = tmpl;
6052 }
6053
6054 if (TREE_CODE (decl) == TYPE_DECL
6055 && TYPE_DECL_ALIAS_P (decl))
6056 {
6057 if (tree constr
6058 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6059 {
6060 /* ??? Why don't we do this here for all templates? */
6061 constr = build_constraints (constr, NULL_TREE);
6062 set_constraints (decl, constr);
6063 }
6064 if (complex_alias_template_p (tmpl))
6065 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6066 }
6067 }
6068
6069 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6070 back to its most general template. If TMPL is a specialization,
6071 ARGS may only have the innermost set of arguments. Add the missing
6072 argument levels if necessary. */
6073 if (DECL_TEMPLATE_INFO (tmpl))
6074 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6075
6076 info = build_template_info (tmpl, args);
6077
6078 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6079 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6080 else
6081 {
6082 if (is_primary)
6083 retrofit_lang_decl (decl);
6084 if (DECL_LANG_SPECIFIC (decl)
6085 && !(VAR_OR_FUNCTION_DECL_P (decl)
6086 && DECL_LOCAL_DECL_P (decl)))
6087 DECL_TEMPLATE_INFO (decl) = info;
6088 }
6089
6090 if (flag_implicit_templates
6091 && !is_friend
6092 && TREE_PUBLIC (decl)
6093 && VAR_OR_FUNCTION_DECL_P (decl))
6094 /* Set DECL_COMDAT on template instantiations; if we force
6095 them to be emitted by explicit instantiation,
6096 mark_needed will tell cgraph to do the right thing. */
6097 DECL_COMDAT (decl) = true;
6098
6099 return DECL_TEMPLATE_RESULT (tmpl);
6100 }
6101
6102 /* FN is an inheriting constructor that inherits from the constructor
6103 template INHERITED; turn FN into a constructor template with a matching
6104 template header. */
6105
6106 tree
6107 add_inherited_template_parms (tree fn, tree inherited)
6108 {
6109 tree inner_parms
6110 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6111 inner_parms = copy_node (inner_parms);
6112 tree parms
6113 = tree_cons (size_int (processing_template_decl + 1),
6114 inner_parms, current_template_parms);
6115 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6116 tree args = template_parms_to_args (parms);
6117 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6118 DECL_ARTIFICIAL (tmpl) = true;
6119 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6120 return tmpl;
6121 }
6122
6123 /* Called when a class template TYPE is redeclared with the indicated
6124 template PARMS, e.g.:
6125
6126 template <class T> struct S;
6127 template <class T> struct S {}; */
6128
6129 bool
6130 redeclare_class_template (tree type, tree parms, tree cons)
6131 {
6132 tree tmpl;
6133 tree tmpl_parms;
6134 int i;
6135
6136 if (!TYPE_TEMPLATE_INFO (type))
6137 {
6138 error ("%qT is not a template type", type);
6139 return false;
6140 }
6141
6142 tmpl = TYPE_TI_TEMPLATE (type);
6143 if (!PRIMARY_TEMPLATE_P (tmpl))
6144 /* The type is nested in some template class. Nothing to worry
6145 about here; there are no new template parameters for the nested
6146 type. */
6147 return true;
6148
6149 if (!parms)
6150 {
6151 error ("template specifiers not specified in declaration of %qD",
6152 tmpl);
6153 return false;
6154 }
6155
6156 parms = INNERMOST_TEMPLATE_PARMS (parms);
6157 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6158
6159 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6160 {
6161 error_n (input_location, TREE_VEC_LENGTH (parms),
6162 "redeclared with %d template parameter",
6163 "redeclared with %d template parameters",
6164 TREE_VEC_LENGTH (parms));
6165 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6166 "previous declaration %qD used %d template parameter",
6167 "previous declaration %qD used %d template parameters",
6168 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6169 return false;
6170 }
6171
6172 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6173 {
6174 tree tmpl_parm;
6175 tree parm;
6176 tree tmpl_default;
6177 tree parm_default;
6178
6179 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6180 || TREE_VEC_ELT (parms, i) == error_mark_node)
6181 continue;
6182
6183 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6184 if (error_operand_p (tmpl_parm))
6185 return false;
6186
6187 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6188 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6189 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6190
6191 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6192 TEMPLATE_DECL. */
6193 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6194 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6195 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6196 || (TREE_CODE (tmpl_parm) != PARM_DECL
6197 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6198 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6199 || (TREE_CODE (tmpl_parm) == PARM_DECL
6200 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6201 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6202 {
6203 auto_diagnostic_group d;
6204 error ("template parameter %q+#D", tmpl_parm);
6205 inform (input_location, "redeclared here as %q#D", parm);
6206 return false;
6207 }
6208
6209 /* The parameters can be declared to introduce different
6210 constraints. */
6211 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6212 tree p2 = TREE_VEC_ELT (parms, i);
6213 if (!template_parameter_constraints_equivalent_p (p1, p2))
6214 {
6215 auto_diagnostic_group d;
6216 error ("declaration of template parameter %q+#D with different "
6217 "constraints", parm);
6218 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6219 "original declaration appeared here");
6220 return false;
6221 }
6222
6223 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6224 {
6225 /* We have in [temp.param]:
6226
6227 A template-parameter may not be given default arguments
6228 by two different declarations in the same scope. */
6229 auto_diagnostic_group d;
6230 error_at (input_location, "redefinition of default argument for %q#D", parm);
6231 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6232 "original definition appeared here");
6233 return false;
6234 }
6235
6236 if (parm_default != NULL_TREE)
6237 /* Update the previous template parameters (which are the ones
6238 that will really count) with the new default value. */
6239 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6240 else if (tmpl_default != NULL_TREE)
6241 /* Update the new parameters, too; they'll be used as the
6242 parameters for any members. */
6243 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6244
6245 /* Give each template template parm in this redeclaration a
6246 DECL_CONTEXT of the template for which they are a parameter. */
6247 if (TREE_CODE (parm) == TEMPLATE_DECL)
6248 {
6249 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6250 DECL_CONTEXT (parm) = tmpl;
6251 }
6252
6253 if (TREE_CODE (parm) == TYPE_DECL)
6254 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6255 }
6256
6257 tree ci = get_constraints (tmpl);
6258 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6259 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6260
6261 /* Two classes with different constraints declare different entities. */
6262 if (!cp_tree_equal (req1, req2))
6263 {
6264 auto_diagnostic_group d;
6265 error_at (input_location, "redeclaration %q#D with different "
6266 "constraints", tmpl);
6267 inform (DECL_SOURCE_LOCATION (tmpl),
6268 "original declaration appeared here");
6269 return false;
6270 }
6271
6272 return true;
6273 }
6274
6275 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6276 to be used when the caller has already checked
6277 (processing_template_decl
6278 && !instantiation_dependent_expression_p (expr)
6279 && potential_constant_expression (expr))
6280 and cleared processing_template_decl. */
6281
6282 tree
6283 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6284 {
6285 return tsubst_copy_and_build (expr,
6286 /*args=*/NULL_TREE,
6287 complain,
6288 /*in_decl=*/NULL_TREE,
6289 /*function_p=*/false,
6290 /*integral_constant_expression_p=*/true);
6291 }
6292
6293 /* Simplify EXPR if it is a non-dependent expression. Returns the
6294 (possibly simplified) expression. */
6295
6296 tree
6297 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6298 {
6299 if (expr == NULL_TREE)
6300 return NULL_TREE;
6301
6302 /* If we're in a template, but EXPR isn't value dependent, simplify
6303 it. We're supposed to treat:
6304
6305 template <typename T> void f(T[1 + 1]);
6306 template <typename T> void f(T[2]);
6307
6308 as two declarations of the same function, for example. */
6309 if (processing_template_decl
6310 && is_nondependent_constant_expression (expr))
6311 {
6312 processing_template_decl_sentinel s;
6313 expr = instantiate_non_dependent_expr_internal (expr, complain);
6314 }
6315 return expr;
6316 }
6317
6318 tree
6319 instantiate_non_dependent_expr (tree expr)
6320 {
6321 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6322 }
6323
6324 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6325 an uninstantiated expression. */
6326
6327 tree
6328 instantiate_non_dependent_or_null (tree expr)
6329 {
6330 if (expr == NULL_TREE)
6331 return NULL_TREE;
6332 if (processing_template_decl)
6333 {
6334 if (!is_nondependent_constant_expression (expr))
6335 expr = NULL_TREE;
6336 else
6337 {
6338 processing_template_decl_sentinel s;
6339 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6340 }
6341 }
6342 return expr;
6343 }
6344
6345 /* True iff T is a specialization of a variable template. */
6346
6347 bool
6348 variable_template_specialization_p (tree t)
6349 {
6350 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6351 return false;
6352 tree tmpl = DECL_TI_TEMPLATE (t);
6353 return variable_template_p (tmpl);
6354 }
6355
6356 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6357 template declaration, or a TYPE_DECL for an alias declaration. */
6358
6359 bool
6360 alias_type_or_template_p (tree t)
6361 {
6362 if (t == NULL_TREE)
6363 return false;
6364 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6365 || (TYPE_P (t)
6366 && TYPE_NAME (t)
6367 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6368 || DECL_ALIAS_TEMPLATE_P (t));
6369 }
6370
6371 /* If T is a specialization of an alias template, return it; otherwise return
6372 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6373
6374 tree
6375 alias_template_specialization_p (const_tree t,
6376 bool transparent_typedefs)
6377 {
6378 if (!TYPE_P (t))
6379 return NULL_TREE;
6380
6381 /* It's an alias template specialization if it's an alias and its
6382 TYPE_NAME is a specialization of a primary template. */
6383 if (typedef_variant_p (t))
6384 {
6385 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6386 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6387 return CONST_CAST_TREE (t);
6388 if (transparent_typedefs)
6389 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6390 (TYPE_NAME (t)),
6391 transparent_typedefs);
6392 }
6393
6394 return NULL_TREE;
6395 }
6396
6397 /* An alias template is complex from a SFINAE perspective if a template-id
6398 using that alias can be ill-formed when the expansion is not, as with
6399 the void_t template. We determine this by checking whether the
6400 expansion for the alias template uses all its template parameters. */
6401
6402 struct uses_all_template_parms_data
6403 {
6404 int level;
6405 bool *seen;
6406 };
6407
6408 static int
6409 uses_all_template_parms_r (tree t, void *data_)
6410 {
6411 struct uses_all_template_parms_data &data
6412 = *(struct uses_all_template_parms_data*)data_;
6413 tree idx = get_template_parm_index (t);
6414
6415 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6416 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6417 return 0;
6418 }
6419
6420 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6421
6422 static int
6423 complex_pack_expansion_r (tree t, void *data_)
6424 {
6425 /* An alias template with a pack expansion that expands a pack from the
6426 enclosing class needs to be considered complex, to avoid confusion with
6427 the same pack being used as an argument to the alias's own template
6428 parameter (91966). */
6429 if (!PACK_EXPANSION_P (t))
6430 return 0;
6431 struct uses_all_template_parms_data &data
6432 = *(struct uses_all_template_parms_data*)data_;
6433 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6434 pack = TREE_CHAIN (pack))
6435 {
6436 tree parm_pack = TREE_VALUE (pack);
6437 if (!TEMPLATE_PARM_P (parm_pack))
6438 continue;
6439 int idx, level;
6440 template_parm_level_and_index (parm_pack, &level, &idx);
6441 if (level < data.level)
6442 return 1;
6443 }
6444 return 0;
6445 }
6446
6447 static bool
6448 complex_alias_template_p (const_tree tmpl)
6449 {
6450 /* A renaming alias isn't complex. */
6451 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6452 return false;
6453
6454 /* Any other constrained alias is complex. */
6455 if (get_constraints (tmpl))
6456 return true;
6457
6458 struct uses_all_template_parms_data data;
6459 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6460 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6461 data.level = TMPL_PARMS_DEPTH (parms);
6462 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6463 data.seen = XALLOCAVEC (bool, len);
6464 for (int i = 0; i < len; ++i)
6465 data.seen[i] = false;
6466
6467 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6468 NULL, true, complex_pack_expansion_r))
6469 return true;
6470 for (int i = 0; i < len; ++i)
6471 if (!data.seen[i])
6472 return true;
6473 return false;
6474 }
6475
6476 /* If T is a specialization of a complex alias template with dependent
6477 template-arguments, return it; otherwise return NULL_TREE. If T is a
6478 typedef to such a specialization, return the specialization. */
6479
6480 tree
6481 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6482 {
6483 if (!TYPE_P (t) || !typedef_variant_p (t))
6484 return NULL_TREE;
6485
6486 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6487 if (tinfo
6488 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6489 && (any_dependent_template_arguments_p
6490 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6491 return CONST_CAST_TREE (t);
6492
6493 if (transparent_typedefs)
6494 {
6495 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6496 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6497 }
6498
6499 return NULL_TREE;
6500 }
6501
6502 /* Return the number of innermost template parameters in TMPL. */
6503
6504 static int
6505 num_innermost_template_parms (const_tree tmpl)
6506 {
6507 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6508 return TREE_VEC_LENGTH (parms);
6509 }
6510
6511 /* Return either TMPL or another template that it is equivalent to under DR
6512 1286: An alias that just changes the name of a template is equivalent to
6513 the other template. */
6514
6515 static tree
6516 get_underlying_template (tree tmpl)
6517 {
6518 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6519 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6520 {
6521 /* Determine if the alias is equivalent to an underlying template. */
6522 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6523 /* The underlying type may have been ill-formed. Don't proceed. */
6524 if (!orig_type)
6525 break;
6526 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6527 if (!tinfo)
6528 break;
6529
6530 tree underlying = TI_TEMPLATE (tinfo);
6531 if (!PRIMARY_TEMPLATE_P (underlying)
6532 || (num_innermost_template_parms (tmpl)
6533 != num_innermost_template_parms (underlying)))
6534 break;
6535
6536 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6537 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6538 break;
6539
6540 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6541 it's appropriate to treat a less-constrained alias as equivalent. */
6542 if (!at_least_as_constrained (underlying, tmpl))
6543 break;
6544
6545 /* Alias is equivalent. Strip it and repeat. */
6546 tmpl = underlying;
6547 }
6548
6549 return tmpl;
6550 }
6551
6552 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6553 must be a reference-to-function or a pointer-to-function type, as specified
6554 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6555 and check that the resulting function has external linkage. */
6556
6557 static tree
6558 convert_nontype_argument_function (tree type, tree expr,
6559 tsubst_flags_t complain)
6560 {
6561 tree fns = expr;
6562 tree fn, fn_no_ptr;
6563 linkage_kind linkage;
6564
6565 fn = instantiate_type (type, fns, tf_none);
6566 if (fn == error_mark_node)
6567 return error_mark_node;
6568
6569 if (value_dependent_expression_p (fn))
6570 goto accept;
6571
6572 fn_no_ptr = strip_fnptr_conv (fn);
6573 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6574 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6575 if (BASELINK_P (fn_no_ptr))
6576 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6577
6578 /* [temp.arg.nontype]/1
6579
6580 A template-argument for a non-type, non-template template-parameter
6581 shall be one of:
6582 [...]
6583 -- the address of an object or function with external [C++11: or
6584 internal] linkage. */
6585
6586 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6587 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6588 {
6589 if (complain & tf_error)
6590 {
6591 location_t loc = cp_expr_loc_or_input_loc (expr);
6592 error_at (loc, "%qE is not a valid template argument for type %qT",
6593 expr, type);
6594 if (TYPE_PTR_P (type))
6595 inform (loc, "it must be the address of a function "
6596 "with external linkage");
6597 else
6598 inform (loc, "it must be the name of a function with "
6599 "external linkage");
6600 }
6601 return NULL_TREE;
6602 }
6603
6604 linkage = decl_linkage (fn_no_ptr);
6605 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6606 {
6607 if (complain & tf_error)
6608 {
6609 location_t loc = cp_expr_loc_or_input_loc (expr);
6610 if (cxx_dialect >= cxx11)
6611 error_at (loc, "%qE is not a valid template argument for type "
6612 "%qT because %qD has no linkage",
6613 expr, type, fn_no_ptr);
6614 else
6615 error_at (loc, "%qE is not a valid template argument for type "
6616 "%qT because %qD does not have external linkage",
6617 expr, type, fn_no_ptr);
6618 }
6619 return NULL_TREE;
6620 }
6621
6622 accept:
6623 if (TYPE_REF_P (type))
6624 {
6625 if (REFERENCE_REF_P (fn))
6626 fn = TREE_OPERAND (fn, 0);
6627 else
6628 fn = build_address (fn);
6629 }
6630 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6631 fn = build_nop (type, fn);
6632
6633 return fn;
6634 }
6635
6636 /* Subroutine of convert_nontype_argument.
6637 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6638 Emit an error otherwise. */
6639
6640 static bool
6641 check_valid_ptrmem_cst_expr (tree type, tree expr,
6642 tsubst_flags_t complain)
6643 {
6644 tree orig_expr = expr;
6645 STRIP_NOPS (expr);
6646 if (null_ptr_cst_p (expr))
6647 return true;
6648 if (TREE_CODE (expr) == PTRMEM_CST
6649 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6650 PTRMEM_CST_CLASS (expr)))
6651 return true;
6652 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6653 return true;
6654 if (processing_template_decl
6655 && TREE_CODE (expr) == ADDR_EXPR
6656 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6657 return true;
6658 if (complain & tf_error)
6659 {
6660 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6661 error_at (loc, "%qE is not a valid template argument for type %qT",
6662 orig_expr, type);
6663 if (TREE_CODE (expr) != PTRMEM_CST)
6664 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6665 else
6666 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6667 }
6668 return false;
6669 }
6670
6671 /* Returns TRUE iff the address of OP is value-dependent.
6672
6673 14.6.2.4 [temp.dep.temp]:
6674 A non-integral non-type template-argument is dependent if its type is
6675 dependent or it has either of the following forms
6676 qualified-id
6677 & qualified-id
6678 and contains a nested-name-specifier which specifies a class-name that
6679 names a dependent type.
6680
6681 We generalize this to just say that the address of a member of a
6682 dependent class is value-dependent; the above doesn't cover the
6683 address of a static data member named with an unqualified-id. */
6684
6685 static bool
6686 has_value_dependent_address (tree op)
6687 {
6688 STRIP_ANY_LOCATION_WRAPPER (op);
6689
6690 /* We could use get_inner_reference here, but there's no need;
6691 this is only relevant for template non-type arguments, which
6692 can only be expressed as &id-expression. */
6693 if (DECL_P (op))
6694 {
6695 tree ctx = CP_DECL_CONTEXT (op);
6696 if (TYPE_P (ctx) && dependent_type_p (ctx))
6697 return true;
6698 }
6699
6700 return false;
6701 }
6702
6703 /* The next set of functions are used for providing helpful explanatory
6704 diagnostics for failed overload resolution. Their messages should be
6705 indented by two spaces for consistency with the messages in
6706 call.c */
6707
6708 static int
6709 unify_success (bool /*explain_p*/)
6710 {
6711 return 0;
6712 }
6713
6714 /* Other failure functions should call this one, to provide a single function
6715 for setting a breakpoint on. */
6716
6717 static int
6718 unify_invalid (bool /*explain_p*/)
6719 {
6720 return 1;
6721 }
6722
6723 static int
6724 unify_parameter_deduction_failure (bool explain_p, tree parm)
6725 {
6726 if (explain_p)
6727 inform (input_location,
6728 " couldn%'t deduce template parameter %qD", parm);
6729 return unify_invalid (explain_p);
6730 }
6731
6732 static int
6733 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6734 {
6735 if (explain_p)
6736 inform (input_location,
6737 " types %qT and %qT have incompatible cv-qualifiers",
6738 parm, arg);
6739 return unify_invalid (explain_p);
6740 }
6741
6742 static int
6743 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6744 {
6745 if (explain_p)
6746 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6747 return unify_invalid (explain_p);
6748 }
6749
6750 static int
6751 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6752 {
6753 if (explain_p)
6754 inform (input_location,
6755 " template parameter %qD is not a parameter pack, but "
6756 "argument %qD is",
6757 parm, arg);
6758 return unify_invalid (explain_p);
6759 }
6760
6761 static int
6762 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6763 {
6764 if (explain_p)
6765 inform (input_location,
6766 " template argument %qE does not match "
6767 "pointer-to-member constant %qE",
6768 arg, parm);
6769 return unify_invalid (explain_p);
6770 }
6771
6772 static int
6773 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6774 {
6775 if (explain_p)
6776 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6777 return unify_invalid (explain_p);
6778 }
6779
6780 static int
6781 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6782 {
6783 if (explain_p)
6784 inform (input_location,
6785 " inconsistent parameter pack deduction with %qT and %qT",
6786 old_arg, new_arg);
6787 return unify_invalid (explain_p);
6788 }
6789
6790 static int
6791 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6792 {
6793 if (explain_p)
6794 {
6795 if (TYPE_P (parm))
6796 inform (input_location,
6797 " deduced conflicting types for parameter %qT (%qT and %qT)",
6798 parm, first, second);
6799 else
6800 inform (input_location,
6801 " deduced conflicting values for non-type parameter "
6802 "%qE (%qE and %qE)", parm, first, second);
6803 }
6804 return unify_invalid (explain_p);
6805 }
6806
6807 static int
6808 unify_vla_arg (bool explain_p, tree arg)
6809 {
6810 if (explain_p)
6811 inform (input_location,
6812 " variable-sized array type %qT is not "
6813 "a valid template argument",
6814 arg);
6815 return unify_invalid (explain_p);
6816 }
6817
6818 static int
6819 unify_method_type_error (bool explain_p, tree arg)
6820 {
6821 if (explain_p)
6822 inform (input_location,
6823 " member function type %qT is not a valid template argument",
6824 arg);
6825 return unify_invalid (explain_p);
6826 }
6827
6828 static int
6829 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6830 {
6831 if (explain_p)
6832 {
6833 if (least_p)
6834 inform_n (input_location, wanted,
6835 " candidate expects at least %d argument, %d provided",
6836 " candidate expects at least %d arguments, %d provided",
6837 wanted, have);
6838 else
6839 inform_n (input_location, wanted,
6840 " candidate expects %d argument, %d provided",
6841 " candidate expects %d arguments, %d provided",
6842 wanted, have);
6843 }
6844 return unify_invalid (explain_p);
6845 }
6846
6847 static int
6848 unify_too_many_arguments (bool explain_p, int have, int wanted)
6849 {
6850 return unify_arity (explain_p, have, wanted);
6851 }
6852
6853 static int
6854 unify_too_few_arguments (bool explain_p, int have, int wanted,
6855 bool least_p = false)
6856 {
6857 return unify_arity (explain_p, have, wanted, least_p);
6858 }
6859
6860 static int
6861 unify_arg_conversion (bool explain_p, tree to_type,
6862 tree from_type, tree arg)
6863 {
6864 if (explain_p)
6865 inform (cp_expr_loc_or_input_loc (arg),
6866 " cannot convert %qE (type %qT) to type %qT",
6867 arg, from_type, to_type);
6868 return unify_invalid (explain_p);
6869 }
6870
6871 static int
6872 unify_no_common_base (bool explain_p, enum template_base_result r,
6873 tree parm, tree arg)
6874 {
6875 if (explain_p)
6876 switch (r)
6877 {
6878 case tbr_ambiguous_baseclass:
6879 inform (input_location, " %qT is an ambiguous base class of %qT",
6880 parm, arg);
6881 break;
6882 default:
6883 inform (input_location, " %qT is not derived from %qT", arg, parm);
6884 break;
6885 }
6886 return unify_invalid (explain_p);
6887 }
6888
6889 static int
6890 unify_inconsistent_template_template_parameters (bool explain_p)
6891 {
6892 if (explain_p)
6893 inform (input_location,
6894 " template parameters of a template template argument are "
6895 "inconsistent with other deduced template arguments");
6896 return unify_invalid (explain_p);
6897 }
6898
6899 static int
6900 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6901 {
6902 if (explain_p)
6903 inform (input_location,
6904 " cannot deduce a template for %qT from non-template type %qT",
6905 parm, arg);
6906 return unify_invalid (explain_p);
6907 }
6908
6909 static int
6910 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6911 {
6912 if (explain_p)
6913 inform (input_location,
6914 " template argument %qE does not match %qE", arg, parm);
6915 return unify_invalid (explain_p);
6916 }
6917
6918 /* True if T is a C++20 template parameter object to store the argument for a
6919 template parameter of class type. */
6920
6921 bool
6922 template_parm_object_p (const_tree t)
6923 {
6924 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6925 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6926 }
6927
6928 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6929 argument for TYPE, points to an unsuitable object.
6930
6931 Also adjust the type of the index in C++20 array subobject references. */
6932
6933 static bool
6934 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6935 {
6936 switch (TREE_CODE (expr))
6937 {
6938 CASE_CONVERT:
6939 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6940 complain);
6941
6942 case TARGET_EXPR:
6943 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6944 complain);
6945
6946 case CONSTRUCTOR:
6947 {
6948 unsigned i; tree elt;
6949 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6950 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6951 return true;
6952 }
6953 break;
6954
6955 case ADDR_EXPR:
6956 {
6957 tree decl = TREE_OPERAND (expr, 0);
6958
6959 if (cxx_dialect >= cxx20)
6960 while (TREE_CODE (decl) == COMPONENT_REF
6961 || TREE_CODE (decl) == ARRAY_REF)
6962 {
6963 tree &op = TREE_OPERAND (decl, 1);
6964 if (TREE_CODE (decl) == ARRAY_REF
6965 && TREE_CODE (op) == INTEGER_CST)
6966 /* Canonicalize array offsets to ptrdiff_t; how they were
6967 written doesn't matter for subobject identity. */
6968 op = fold_convert (ptrdiff_type_node, op);
6969 decl = TREE_OPERAND (decl, 0);
6970 }
6971
6972 if (!VAR_P (decl))
6973 {
6974 if (complain & tf_error)
6975 error_at (cp_expr_loc_or_input_loc (expr),
6976 "%qE is not a valid template argument of type %qT "
6977 "because %qE is not a variable", expr, type, decl);
6978 return true;
6979 }
6980 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6981 {
6982 if (complain & tf_error)
6983 error_at (cp_expr_loc_or_input_loc (expr),
6984 "%qE is not a valid template argument of type %qT "
6985 "in C++98 because %qD does not have external linkage",
6986 expr, type, decl);
6987 return true;
6988 }
6989 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6990 && decl_linkage (decl) == lk_none)
6991 {
6992 if (complain & tf_error)
6993 error_at (cp_expr_loc_or_input_loc (expr),
6994 "%qE is not a valid template argument of type %qT "
6995 "because %qD has no linkage", expr, type, decl);
6996 return true;
6997 }
6998 /* C++17: For a non-type template-parameter of reference or pointer
6999 type, the value of the constant expression shall not refer to (or
7000 for a pointer type, shall not be the address of):
7001 * a subobject (4.5),
7002 * a temporary object (15.2),
7003 * a string literal (5.13.5),
7004 * the result of a typeid expression (8.2.8), or
7005 * a predefined __func__ variable (11.4.1). */
7006 else if (DECL_ARTIFICIAL (decl))
7007 {
7008 if (complain & tf_error)
7009 error ("the address of %qD is not a valid template argument",
7010 decl);
7011 return true;
7012 }
7013 else if (cxx_dialect < cxx20
7014 && !(same_type_ignoring_top_level_qualifiers_p
7015 (strip_array_types (TREE_TYPE (type)),
7016 strip_array_types (TREE_TYPE (decl)))))
7017 {
7018 if (complain & tf_error)
7019 error ("the address of the %qT subobject of %qD is not a "
7020 "valid template argument", TREE_TYPE (type), decl);
7021 return true;
7022 }
7023 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7024 {
7025 if (complain & tf_error)
7026 error ("the address of %qD is not a valid template argument "
7027 "because it does not have static storage duration",
7028 decl);
7029 return true;
7030 }
7031 }
7032 break;
7033
7034 default:
7035 if (!INDIRECT_TYPE_P (type))
7036 /* We're only concerned about pointers and references here. */;
7037 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7038 /* Null pointer values are OK in C++11. */;
7039 else
7040 {
7041 if (VAR_P (expr))
7042 {
7043 if (complain & tf_error)
7044 error ("%qD is not a valid template argument "
7045 "because %qD is a variable, not the address of "
7046 "a variable", expr, expr);
7047 return true;
7048 }
7049 else
7050 {
7051 if (complain & tf_error)
7052 error ("%qE is not a valid template argument for %qT "
7053 "because it is not the address of a variable",
7054 expr, type);
7055 return true;
7056 }
7057 }
7058 }
7059 return false;
7060
7061 }
7062
7063 /* The template arguments corresponding to template parameter objects of types
7064 that contain pointers to members. */
7065
7066 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7067
7068 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7069 template argument EXPR. */
7070
7071 static tree
7072 get_template_parm_object (tree expr, tsubst_flags_t complain)
7073 {
7074 if (TREE_CODE (expr) == TARGET_EXPR)
7075 expr = TARGET_EXPR_INITIAL (expr);
7076
7077 if (!TREE_CONSTANT (expr))
7078 {
7079 if ((complain & tf_error)
7080 && require_rvalue_constant_expression (expr))
7081 cxx_constant_value (expr);
7082 return error_mark_node;
7083 }
7084 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7085 return error_mark_node;
7086
7087 tree name = mangle_template_parm_object (expr);
7088 tree decl = get_global_binding (name);
7089 if (decl)
7090 return decl;
7091
7092 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7093 decl = create_temporary_var (type);
7094 DECL_CONTEXT (decl) = NULL_TREE;
7095 TREE_STATIC (decl) = true;
7096 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7097 TREE_READONLY (decl) = true;
7098 DECL_NAME (decl) = name;
7099 SET_DECL_ASSEMBLER_NAME (decl, name);
7100 comdat_linkage (decl);
7101
7102 if (!zero_init_p (type))
7103 {
7104 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7105 lower_var_init before we're done mangling. So store the original
7106 value elsewhere. */
7107 tree copy = unshare_constructor (expr);
7108 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7109 }
7110
7111 pushdecl_top_level_and_finish (decl, expr);
7112
7113 return decl;
7114 }
7115
7116 /* Return the actual template argument corresponding to template parameter
7117 object VAR. */
7118
7119 tree
7120 tparm_object_argument (tree var)
7121 {
7122 if (zero_init_p (TREE_TYPE (var)))
7123 return DECL_INITIAL (var);
7124 return *(tparm_obj_values->get (var));
7125 }
7126
7127 /* Attempt to convert the non-type template parameter EXPR to the
7128 indicated TYPE. If the conversion is successful, return the
7129 converted value. If the conversion is unsuccessful, return
7130 NULL_TREE if we issued an error message, or error_mark_node if we
7131 did not. We issue error messages for out-and-out bad template
7132 parameters, but not simply because the conversion failed, since we
7133 might be just trying to do argument deduction. Both TYPE and EXPR
7134 must be non-dependent.
7135
7136 The conversion follows the special rules described in
7137 [temp.arg.nontype], and it is much more strict than an implicit
7138 conversion.
7139
7140 This function is called twice for each template argument (see
7141 lookup_template_class for a more accurate description of this
7142 problem). This means that we need to handle expressions which
7143 are not valid in a C++ source, but can be created from the
7144 first call (for instance, casts to perform conversions). These
7145 hacks can go away after we fix the double coercion problem. */
7146
7147 static tree
7148 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7149 {
7150 tree expr_type;
7151 location_t loc = cp_expr_loc_or_input_loc (expr);
7152
7153 /* Detect immediately string literals as invalid non-type argument.
7154 This special-case is not needed for correctness (we would easily
7155 catch this later), but only to provide better diagnostic for this
7156 common user mistake. As suggested by DR 100, we do not mention
7157 linkage issues in the diagnostic as this is not the point. */
7158 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7159 {
7160 if (complain & tf_error)
7161 error ("%qE is not a valid template argument for type %qT "
7162 "because string literals can never be used in this context",
7163 expr, type);
7164 return NULL_TREE;
7165 }
7166
7167 /* Add the ADDR_EXPR now for the benefit of
7168 value_dependent_expression_p. */
7169 if (TYPE_PTROBV_P (type)
7170 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7171 {
7172 expr = decay_conversion (expr, complain);
7173 if (expr == error_mark_node)
7174 return error_mark_node;
7175 }
7176
7177 /* If we are in a template, EXPR may be non-dependent, but still
7178 have a syntactic, rather than semantic, form. For example, EXPR
7179 might be a SCOPE_REF, rather than the VAR_DECL to which the
7180 SCOPE_REF refers. Preserving the qualifying scope is necessary
7181 so that access checking can be performed when the template is
7182 instantiated -- but here we need the resolved form so that we can
7183 convert the argument. */
7184 bool non_dep = false;
7185 if (TYPE_REF_OBJ_P (type)
7186 && has_value_dependent_address (expr))
7187 /* If we want the address and it's value-dependent, don't fold. */;
7188 else if (processing_template_decl
7189 && is_nondependent_constant_expression (expr))
7190 non_dep = true;
7191 if (error_operand_p (expr))
7192 return error_mark_node;
7193 expr_type = TREE_TYPE (expr);
7194
7195 /* If the argument is non-dependent, perform any conversions in
7196 non-dependent context as well. */
7197 processing_template_decl_sentinel s (non_dep);
7198 if (non_dep)
7199 expr = instantiate_non_dependent_expr_internal (expr, complain);
7200
7201 const bool val_dep_p = value_dependent_expression_p (expr);
7202 if (val_dep_p)
7203 expr = canonicalize_expr_argument (expr, complain);
7204
7205 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7206 to a non-type argument of "nullptr". */
7207 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7208 expr = fold_simple (convert (type, expr));
7209
7210 /* In C++11, integral or enumeration non-type template arguments can be
7211 arbitrary constant expressions. Pointer and pointer to
7212 member arguments can be general constant expressions that evaluate
7213 to a null value, but otherwise still need to be of a specific form. */
7214 if (cxx_dialect >= cxx11)
7215 {
7216 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7217 /* A PTRMEM_CST is already constant, and a valid template
7218 argument for a parameter of pointer to member type, we just want
7219 to leave it in that form rather than lower it to a
7220 CONSTRUCTOR. */;
7221 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7222 || cxx_dialect >= cxx17)
7223 {
7224 /* C++17: A template-argument for a non-type template-parameter shall
7225 be a converted constant expression (8.20) of the type of the
7226 template-parameter. */
7227 expr = build_converted_constant_expr (type, expr, complain);
7228 if (expr == error_mark_node)
7229 /* Make sure we return NULL_TREE only if we have really issued
7230 an error, as described above. */
7231 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7232 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7233 {
7234 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7235 return expr;
7236 }
7237 expr = maybe_constant_value (expr, NULL_TREE,
7238 /*manifestly_const_eval=*/true);
7239 expr = convert_from_reference (expr);
7240 }
7241 else if (TYPE_PTR_OR_PTRMEM_P (type))
7242 {
7243 tree folded = maybe_constant_value (expr, NULL_TREE,
7244 /*manifestly_const_eval=*/true);
7245 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7246 : null_member_pointer_value_p (folded))
7247 expr = folded;
7248 }
7249 }
7250
7251 if (TYPE_REF_P (type))
7252 expr = mark_lvalue_use (expr);
7253 else
7254 expr = mark_rvalue_use (expr);
7255
7256 /* HACK: Due to double coercion, we can get a
7257 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7258 which is the tree that we built on the first call (see
7259 below when coercing to reference to object or to reference to
7260 function). We just strip everything and get to the arg.
7261 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7262 for examples. */
7263 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7264 {
7265 tree probe_type, probe = expr;
7266 if (REFERENCE_REF_P (probe))
7267 probe = TREE_OPERAND (probe, 0);
7268 probe_type = TREE_TYPE (probe);
7269 if (TREE_CODE (probe) == NOP_EXPR)
7270 {
7271 /* ??? Maybe we could use convert_from_reference here, but we
7272 would need to relax its constraints because the NOP_EXPR
7273 could actually change the type to something more cv-qualified,
7274 and this is not folded by convert_from_reference. */
7275 tree addr = TREE_OPERAND (probe, 0);
7276 if (TYPE_REF_P (probe_type)
7277 && TREE_CODE (addr) == ADDR_EXPR
7278 && TYPE_PTR_P (TREE_TYPE (addr))
7279 && (same_type_ignoring_top_level_qualifiers_p
7280 (TREE_TYPE (probe_type),
7281 TREE_TYPE (TREE_TYPE (addr)))))
7282 {
7283 expr = TREE_OPERAND (addr, 0);
7284 expr_type = TREE_TYPE (probe_type);
7285 }
7286 }
7287 }
7288
7289 /* [temp.arg.nontype]/5, bullet 1
7290
7291 For a non-type template-parameter of integral or enumeration type,
7292 integral promotions (_conv.prom_) and integral conversions
7293 (_conv.integral_) are applied. */
7294 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7295 || TREE_CODE (type) == REAL_TYPE)
7296 {
7297 if (cxx_dialect < cxx11)
7298 {
7299 tree t = build_converted_constant_expr (type, expr, complain);
7300 t = maybe_constant_value (t);
7301 if (t != error_mark_node)
7302 expr = t;
7303 }
7304
7305 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7306 return error_mark_node;
7307
7308 /* Notice that there are constant expressions like '4 % 0' which
7309 do not fold into integer constants. */
7310 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7311 {
7312 if (complain & tf_error)
7313 {
7314 int errs = errorcount, warns = warningcount + werrorcount;
7315 if (!require_potential_constant_expression (expr))
7316 expr = error_mark_node;
7317 else
7318 expr = cxx_constant_value (expr);
7319 if (errorcount > errs || warningcount + werrorcount > warns)
7320 inform (loc, "in template argument for type %qT", type);
7321 if (expr == error_mark_node)
7322 return NULL_TREE;
7323 /* else cxx_constant_value complained but gave us
7324 a real constant, so go ahead. */
7325 if (!CONSTANT_CLASS_P (expr))
7326 {
7327 /* Some assemble time constant expressions like
7328 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7329 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7330 as we can emit them into .rodata initializers of
7331 variables, yet they can't fold into an INTEGER_CST at
7332 compile time. Refuse them here. */
7333 gcc_checking_assert (reduced_constant_expression_p (expr));
7334 error_at (loc, "template argument %qE for type %qT not "
7335 "a compile-time constant", expr, type);
7336 return NULL_TREE;
7337 }
7338 }
7339 else
7340 return NULL_TREE;
7341 }
7342
7343 /* Avoid typedef problems. */
7344 if (TREE_TYPE (expr) != type)
7345 expr = fold_convert (type, expr);
7346 }
7347 /* [temp.arg.nontype]/5, bullet 2
7348
7349 For a non-type template-parameter of type pointer to object,
7350 qualification conversions (_conv.qual_) and the array-to-pointer
7351 conversion (_conv.array_) are applied. */
7352 else if (TYPE_PTROBV_P (type))
7353 {
7354 tree decayed = expr;
7355
7356 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7357 decay_conversion or an explicit cast. If it's a problematic cast,
7358 we'll complain about it below. */
7359 if (TREE_CODE (expr) == NOP_EXPR)
7360 {
7361 tree probe = expr;
7362 STRIP_NOPS (probe);
7363 if (TREE_CODE (probe) == ADDR_EXPR
7364 && TYPE_PTR_P (TREE_TYPE (probe)))
7365 {
7366 expr = probe;
7367 expr_type = TREE_TYPE (expr);
7368 }
7369 }
7370
7371 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7372
7373 A template-argument for a non-type, non-template template-parameter
7374 shall be one of: [...]
7375
7376 -- the name of a non-type template-parameter;
7377 -- the address of an object or function with external linkage, [...]
7378 expressed as "& id-expression" where the & is optional if the name
7379 refers to a function or array, or if the corresponding
7380 template-parameter is a reference.
7381
7382 Here, we do not care about functions, as they are invalid anyway
7383 for a parameter of type pointer-to-object. */
7384
7385 if (val_dep_p)
7386 /* Non-type template parameters are OK. */
7387 ;
7388 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7389 /* Null pointer values are OK in C++11. */;
7390 else if (TREE_CODE (expr) != ADDR_EXPR
7391 && !INDIRECT_TYPE_P (expr_type))
7392 /* Other values, like integer constants, might be valid
7393 non-type arguments of some other type. */
7394 return error_mark_node;
7395 else if (invalid_tparm_referent_p (type, expr, complain))
7396 return NULL_TREE;
7397
7398 expr = decayed;
7399
7400 expr = perform_qualification_conversions (type, expr);
7401 if (expr == error_mark_node)
7402 return error_mark_node;
7403 }
7404 /* [temp.arg.nontype]/5, bullet 3
7405
7406 For a non-type template-parameter of type reference to object, no
7407 conversions apply. The type referred to by the reference may be more
7408 cv-qualified than the (otherwise identical) type of the
7409 template-argument. The template-parameter is bound directly to the
7410 template-argument, which must be an lvalue. */
7411 else if (TYPE_REF_OBJ_P (type))
7412 {
7413 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7414 expr_type))
7415 return error_mark_node;
7416
7417 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7418 {
7419 if (complain & tf_error)
7420 error ("%qE is not a valid template argument for type %qT "
7421 "because of conflicts in cv-qualification", expr, type);
7422 return NULL_TREE;
7423 }
7424
7425 if (!lvalue_p (expr))
7426 {
7427 if (complain & tf_error)
7428 error ("%qE is not a valid template argument for type %qT "
7429 "because it is not an lvalue", expr, type);
7430 return NULL_TREE;
7431 }
7432
7433 /* [temp.arg.nontype]/1
7434
7435 A template-argument for a non-type, non-template template-parameter
7436 shall be one of: [...]
7437
7438 -- the address of an object or function with external linkage. */
7439 if (INDIRECT_REF_P (expr)
7440 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7441 {
7442 expr = TREE_OPERAND (expr, 0);
7443 if (DECL_P (expr))
7444 {
7445 if (complain & tf_error)
7446 error ("%q#D is not a valid template argument for type %qT "
7447 "because a reference variable does not have a constant "
7448 "address", expr, type);
7449 return NULL_TREE;
7450 }
7451 }
7452
7453 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7454 /* OK, dependent reference. We don't want to ask whether a DECL is
7455 itself value-dependent, since what we want here is its address. */;
7456 else
7457 {
7458 expr = build_address (expr);
7459
7460 if (invalid_tparm_referent_p (type, expr, complain))
7461 return NULL_TREE;
7462 }
7463
7464 if (!same_type_p (type, TREE_TYPE (expr)))
7465 expr = build_nop (type, expr);
7466 }
7467 /* [temp.arg.nontype]/5, bullet 4
7468
7469 For a non-type template-parameter of type pointer to function, only
7470 the function-to-pointer conversion (_conv.func_) is applied. If the
7471 template-argument represents a set of overloaded functions (or a
7472 pointer to such), the matching function is selected from the set
7473 (_over.over_). */
7474 else if (TYPE_PTRFN_P (type))
7475 {
7476 /* If the argument is a template-id, we might not have enough
7477 context information to decay the pointer. */
7478 if (!type_unknown_p (expr_type))
7479 {
7480 expr = decay_conversion (expr, complain);
7481 if (expr == error_mark_node)
7482 return error_mark_node;
7483 }
7484
7485 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7486 /* Null pointer values are OK in C++11. */
7487 return perform_qualification_conversions (type, expr);
7488
7489 expr = convert_nontype_argument_function (type, expr, complain);
7490 if (!expr || expr == error_mark_node)
7491 return expr;
7492 }
7493 /* [temp.arg.nontype]/5, bullet 5
7494
7495 For a non-type template-parameter of type reference to function, no
7496 conversions apply. If the template-argument represents a set of
7497 overloaded functions, the matching function is selected from the set
7498 (_over.over_). */
7499 else if (TYPE_REFFN_P (type))
7500 {
7501 if (TREE_CODE (expr) == ADDR_EXPR)
7502 {
7503 if (complain & tf_error)
7504 {
7505 error ("%qE is not a valid template argument for type %qT "
7506 "because it is a pointer", expr, type);
7507 inform (input_location, "try using %qE instead",
7508 TREE_OPERAND (expr, 0));
7509 }
7510 return NULL_TREE;
7511 }
7512
7513 expr = convert_nontype_argument_function (type, expr, complain);
7514 if (!expr || expr == error_mark_node)
7515 return expr;
7516 }
7517 /* [temp.arg.nontype]/5, bullet 6
7518
7519 For a non-type template-parameter of type pointer to member function,
7520 no conversions apply. If the template-argument represents a set of
7521 overloaded member functions, the matching member function is selected
7522 from the set (_over.over_). */
7523 else if (TYPE_PTRMEMFUNC_P (type))
7524 {
7525 expr = instantiate_type (type, expr, tf_none);
7526 if (expr == error_mark_node)
7527 return error_mark_node;
7528
7529 /* [temp.arg.nontype] bullet 1 says the pointer to member
7530 expression must be a pointer-to-member constant. */
7531 if (!val_dep_p
7532 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7533 return NULL_TREE;
7534
7535 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7536 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7537 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7538 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7539 }
7540 /* [temp.arg.nontype]/5, bullet 7
7541
7542 For a non-type template-parameter of type pointer to data member,
7543 qualification conversions (_conv.qual_) are applied. */
7544 else if (TYPE_PTRDATAMEM_P (type))
7545 {
7546 /* [temp.arg.nontype] bullet 1 says the pointer to member
7547 expression must be a pointer-to-member constant. */
7548 if (!val_dep_p
7549 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7550 return NULL_TREE;
7551
7552 expr = perform_qualification_conversions (type, expr);
7553 if (expr == error_mark_node)
7554 return expr;
7555 }
7556 else if (NULLPTR_TYPE_P (type))
7557 {
7558 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7559 {
7560 if (complain & tf_error)
7561 error ("%qE is not a valid template argument for type %qT "
7562 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7563 return NULL_TREE;
7564 }
7565 return expr;
7566 }
7567 else if (CLASS_TYPE_P (type))
7568 {
7569 /* Replace the argument with a reference to the corresponding template
7570 parameter object. */
7571 if (!val_dep_p)
7572 expr = get_template_parm_object (expr, complain);
7573 if (expr == error_mark_node)
7574 return NULL_TREE;
7575 }
7576 /* A template non-type parameter must be one of the above. */
7577 else
7578 gcc_unreachable ();
7579
7580 /* Sanity check: did we actually convert the argument to the
7581 right type? */
7582 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7583 (type, TREE_TYPE (expr)));
7584 return convert_from_reference (expr);
7585 }
7586
7587 /* Subroutine of coerce_template_template_parms, which returns 1 if
7588 PARM_PARM and ARG_PARM match using the rule for the template
7589 parameters of template template parameters. Both PARM and ARG are
7590 template parameters; the rest of the arguments are the same as for
7591 coerce_template_template_parms.
7592 */
7593 static int
7594 coerce_template_template_parm (tree parm,
7595 tree arg,
7596 tsubst_flags_t complain,
7597 tree in_decl,
7598 tree outer_args)
7599 {
7600 if (arg == NULL_TREE || error_operand_p (arg)
7601 || parm == NULL_TREE || error_operand_p (parm))
7602 return 0;
7603
7604 if (TREE_CODE (arg) != TREE_CODE (parm))
7605 return 0;
7606
7607 switch (TREE_CODE (parm))
7608 {
7609 case TEMPLATE_DECL:
7610 /* We encounter instantiations of templates like
7611 template <template <template <class> class> class TT>
7612 class C; */
7613 {
7614 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7615 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7616
7617 if (!coerce_template_template_parms
7618 (parmparm, argparm, complain, in_decl, outer_args))
7619 return 0;
7620 }
7621 /* Fall through. */
7622
7623 case TYPE_DECL:
7624 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7625 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7626 /* Argument is a parameter pack but parameter is not. */
7627 return 0;
7628 break;
7629
7630 case PARM_DECL:
7631 /* The tsubst call is used to handle cases such as
7632
7633 template <int> class C {};
7634 template <class T, template <T> class TT> class D {};
7635 D<int, C> d;
7636
7637 i.e. the parameter list of TT depends on earlier parameters. */
7638 if (!uses_template_parms (TREE_TYPE (arg)))
7639 {
7640 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7641 if (!uses_template_parms (t)
7642 && !same_type_p (t, TREE_TYPE (arg)))
7643 return 0;
7644 }
7645
7646 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7647 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7648 /* Argument is a parameter pack but parameter is not. */
7649 return 0;
7650
7651 break;
7652
7653 default:
7654 gcc_unreachable ();
7655 }
7656
7657 return 1;
7658 }
7659
7660 /* Coerce template argument list ARGLIST for use with template
7661 template-parameter TEMPL. */
7662
7663 static tree
7664 coerce_template_args_for_ttp (tree templ, tree arglist,
7665 tsubst_flags_t complain)
7666 {
7667 /* Consider an example where a template template parameter declared as
7668
7669 template <class T, class U = std::allocator<T> > class TT
7670
7671 The template parameter level of T and U are one level larger than
7672 of TT. To proper process the default argument of U, say when an
7673 instantiation `TT<int>' is seen, we need to build the full
7674 arguments containing {int} as the innermost level. Outer levels,
7675 available when not appearing as default template argument, can be
7676 obtained from the arguments of the enclosing template.
7677
7678 Suppose that TT is later substituted with std::vector. The above
7679 instantiation is `TT<int, std::allocator<T> >' with TT at
7680 level 1, and T at level 2, while the template arguments at level 1
7681 becomes {std::vector} and the inner level 2 is {int}. */
7682
7683 tree outer = DECL_CONTEXT (templ);
7684 if (outer)
7685 outer = generic_targs_for (outer);
7686 else if (current_template_parms)
7687 {
7688 /* This is an argument of the current template, so we haven't set
7689 DECL_CONTEXT yet. */
7690 tree relevant_template_parms;
7691
7692 /* Parameter levels that are greater than the level of the given
7693 template template parm are irrelevant. */
7694 relevant_template_parms = current_template_parms;
7695 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7696 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7697 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7698
7699 outer = template_parms_to_args (relevant_template_parms);
7700 }
7701
7702 if (outer)
7703 arglist = add_to_template_args (outer, arglist);
7704
7705 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7706 return coerce_template_parms (parmlist, arglist, templ,
7707 complain,
7708 /*require_all_args=*/true,
7709 /*use_default_args=*/true);
7710 }
7711
7712 /* A cache of template template parameters with match-all default
7713 arguments. */
7714 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7715
7716 /* T is a bound template template-parameter. Copy its arguments into default
7717 arguments of the template template-parameter's template parameters. */
7718
7719 static tree
7720 add_defaults_to_ttp (tree otmpl)
7721 {
7722 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7723 return *c;
7724
7725 tree ntmpl = copy_node (otmpl);
7726
7727 tree ntype = copy_node (TREE_TYPE (otmpl));
7728 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7729 TYPE_MAIN_VARIANT (ntype) = ntype;
7730 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7731 TYPE_NAME (ntype) = ntmpl;
7732 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7733
7734 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7735 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7736 TEMPLATE_PARM_DECL (idx) = ntmpl;
7737 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7738
7739 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7740 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7741 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7742 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7743 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7744 {
7745 tree o = TREE_VEC_ELT (vec, i);
7746 if (!template_parameter_pack_p (TREE_VALUE (o)))
7747 {
7748 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7749 TREE_PURPOSE (n) = any_targ_node;
7750 }
7751 }
7752
7753 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7754 return ntmpl;
7755 }
7756
7757 /* ARG is a bound potential template template-argument, and PARGS is a list
7758 of arguments for the corresponding template template-parameter. Adjust
7759 PARGS as appropriate for application to ARG's template, and if ARG is a
7760 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7761 arguments to the template template parameter. */
7762
7763 static tree
7764 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7765 {
7766 ++processing_template_decl;
7767 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7768 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7769 {
7770 /* When comparing two template template-parameters in partial ordering,
7771 rewrite the one currently being used as an argument to have default
7772 arguments for all parameters. */
7773 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7774 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7775 if (pargs != error_mark_node)
7776 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7777 TYPE_TI_ARGS (arg));
7778 }
7779 else
7780 {
7781 tree aparms
7782 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7783 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7784 /*require_all*/true,
7785 /*use_default*/true);
7786 }
7787 --processing_template_decl;
7788 return pargs;
7789 }
7790
7791 /* Subroutine of unify for the case when PARM is a
7792 BOUND_TEMPLATE_TEMPLATE_PARM. */
7793
7794 static int
7795 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7796 bool explain_p)
7797 {
7798 tree parmvec = TYPE_TI_ARGS (parm);
7799 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7800
7801 /* The template template parm might be variadic and the argument
7802 not, so flatten both argument lists. */
7803 parmvec = expand_template_argument_pack (parmvec);
7804 argvec = expand_template_argument_pack (argvec);
7805
7806 if (flag_new_ttp)
7807 {
7808 /* In keeping with P0522R0, adjust P's template arguments
7809 to apply to A's template; then flatten it again. */
7810 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7811 nparmvec = expand_template_argument_pack (nparmvec);
7812
7813 if (unify (tparms, targs, nparmvec, argvec,
7814 UNIFY_ALLOW_NONE, explain_p))
7815 return 1;
7816
7817 /* If the P0522 adjustment eliminated a pack expansion, deduce
7818 empty packs. */
7819 if (flag_new_ttp
7820 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7821 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7822 DEDUCE_EXACT, /*sub*/true, explain_p))
7823 return 1;
7824 }
7825 else
7826 {
7827 /* Deduce arguments T, i from TT<T> or TT<i>.
7828 We check each element of PARMVEC and ARGVEC individually
7829 rather than the whole TREE_VEC since they can have
7830 different number of elements, which is allowed under N2555. */
7831
7832 int len = TREE_VEC_LENGTH (parmvec);
7833
7834 /* Check if the parameters end in a pack, making them
7835 variadic. */
7836 int parm_variadic_p = 0;
7837 if (len > 0
7838 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7839 parm_variadic_p = 1;
7840
7841 for (int i = 0; i < len - parm_variadic_p; ++i)
7842 /* If the template argument list of P contains a pack
7843 expansion that is not the last template argument, the
7844 entire template argument list is a non-deduced
7845 context. */
7846 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7847 return unify_success (explain_p);
7848
7849 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7850 return unify_too_few_arguments (explain_p,
7851 TREE_VEC_LENGTH (argvec), len);
7852
7853 for (int i = 0; i < len - parm_variadic_p; ++i)
7854 if (unify (tparms, targs,
7855 TREE_VEC_ELT (parmvec, i),
7856 TREE_VEC_ELT (argvec, i),
7857 UNIFY_ALLOW_NONE, explain_p))
7858 return 1;
7859
7860 if (parm_variadic_p
7861 && unify_pack_expansion (tparms, targs,
7862 parmvec, argvec,
7863 DEDUCE_EXACT,
7864 /*subr=*/true, explain_p))
7865 return 1;
7866 }
7867
7868 return 0;
7869 }
7870
7871 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7872 template template parameters. Both PARM_PARMS and ARG_PARMS are
7873 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7874 or PARM_DECL.
7875
7876 Consider the example:
7877 template <class T> class A;
7878 template<template <class U> class TT> class B;
7879
7880 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7881 the parameters to A, and OUTER_ARGS contains A. */
7882
7883 static int
7884 coerce_template_template_parms (tree parm_parms,
7885 tree arg_parms,
7886 tsubst_flags_t complain,
7887 tree in_decl,
7888 tree outer_args)
7889 {
7890 int nparms, nargs, i;
7891 tree parm, arg;
7892 int variadic_p = 0;
7893
7894 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7895 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7896
7897 nparms = TREE_VEC_LENGTH (parm_parms);
7898 nargs = TREE_VEC_LENGTH (arg_parms);
7899
7900 if (flag_new_ttp)
7901 {
7902 /* P0522R0: A template template-parameter P is at least as specialized as
7903 a template template-argument A if, given the following rewrite to two
7904 function templates, the function template corresponding to P is at
7905 least as specialized as the function template corresponding to A
7906 according to the partial ordering rules for function templates
7907 ([temp.func.order]). Given an invented class template X with the
7908 template parameter list of A (including default arguments):
7909
7910 * Each of the two function templates has the same template parameters,
7911 respectively, as P or A.
7912
7913 * Each function template has a single function parameter whose type is
7914 a specialization of X with template arguments corresponding to the
7915 template parameters from the respective function template where, for
7916 each template parameter PP in the template parameter list of the
7917 function template, a corresponding template argument AA is formed. If
7918 PP declares a parameter pack, then AA is the pack expansion
7919 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7920
7921 If the rewrite produces an invalid type, then P is not at least as
7922 specialized as A. */
7923
7924 /* So coerce P's args to apply to A's parms, and then deduce between A's
7925 args and the converted args. If that succeeds, A is at least as
7926 specialized as P, so they match.*/
7927 tree pargs = template_parms_level_to_args (parm_parms);
7928 pargs = add_outermost_template_args (outer_args, pargs);
7929 ++processing_template_decl;
7930 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7931 /*require_all*/true, /*use_default*/true);
7932 --processing_template_decl;
7933 if (pargs != error_mark_node)
7934 {
7935 tree targs = make_tree_vec (nargs);
7936 tree aargs = template_parms_level_to_args (arg_parms);
7937 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7938 /*explain*/false))
7939 return 1;
7940 }
7941 }
7942
7943 /* Determine whether we have a parameter pack at the end of the
7944 template template parameter's template parameter list. */
7945 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7946 {
7947 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7948
7949 if (error_operand_p (parm))
7950 return 0;
7951
7952 switch (TREE_CODE (parm))
7953 {
7954 case TEMPLATE_DECL:
7955 case TYPE_DECL:
7956 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7957 variadic_p = 1;
7958 break;
7959
7960 case PARM_DECL:
7961 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7962 variadic_p = 1;
7963 break;
7964
7965 default:
7966 gcc_unreachable ();
7967 }
7968 }
7969
7970 if (nargs != nparms
7971 && !(variadic_p && nargs >= nparms - 1))
7972 return 0;
7973
7974 /* Check all of the template parameters except the parameter pack at
7975 the end (if any). */
7976 for (i = 0; i < nparms - variadic_p; ++i)
7977 {
7978 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7979 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7980 continue;
7981
7982 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7983 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7984
7985 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7986 outer_args))
7987 return 0;
7988
7989 }
7990
7991 if (variadic_p)
7992 {
7993 /* Check each of the template parameters in the template
7994 argument against the template parameter pack at the end of
7995 the template template parameter. */
7996 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7997 return 0;
7998
7999 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8000
8001 for (; i < nargs; ++i)
8002 {
8003 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8004 continue;
8005
8006 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8007
8008 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8009 outer_args))
8010 return 0;
8011 }
8012 }
8013
8014 return 1;
8015 }
8016
8017 /* Verifies that the deduced template arguments (in TARGS) for the
8018 template template parameters (in TPARMS) represent valid bindings,
8019 by comparing the template parameter list of each template argument
8020 to the template parameter list of its corresponding template
8021 template parameter, in accordance with DR150. This
8022 routine can only be called after all template arguments have been
8023 deduced. It will return TRUE if all of the template template
8024 parameter bindings are okay, FALSE otherwise. */
8025 bool
8026 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8027 {
8028 int i, ntparms = TREE_VEC_LENGTH (tparms);
8029 bool ret = true;
8030
8031 /* We're dealing with template parms in this process. */
8032 ++processing_template_decl;
8033
8034 targs = INNERMOST_TEMPLATE_ARGS (targs);
8035
8036 for (i = 0; i < ntparms; ++i)
8037 {
8038 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8039 tree targ = TREE_VEC_ELT (targs, i);
8040
8041 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8042 {
8043 tree packed_args = NULL_TREE;
8044 int idx, len = 1;
8045
8046 if (ARGUMENT_PACK_P (targ))
8047 {
8048 /* Look inside the argument pack. */
8049 packed_args = ARGUMENT_PACK_ARGS (targ);
8050 len = TREE_VEC_LENGTH (packed_args);
8051 }
8052
8053 for (idx = 0; idx < len; ++idx)
8054 {
8055 tree targ_parms = NULL_TREE;
8056
8057 if (packed_args)
8058 /* Extract the next argument from the argument
8059 pack. */
8060 targ = TREE_VEC_ELT (packed_args, idx);
8061
8062 if (PACK_EXPANSION_P (targ))
8063 /* Look at the pattern of the pack expansion. */
8064 targ = PACK_EXPANSION_PATTERN (targ);
8065
8066 /* Extract the template parameters from the template
8067 argument. */
8068 if (TREE_CODE (targ) == TEMPLATE_DECL)
8069 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8070 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8071 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8072
8073 /* Verify that we can coerce the template template
8074 parameters from the template argument to the template
8075 parameter. This requires an exact match. */
8076 if (targ_parms
8077 && !coerce_template_template_parms
8078 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8079 targ_parms,
8080 tf_none,
8081 tparm,
8082 targs))
8083 {
8084 ret = false;
8085 goto out;
8086 }
8087 }
8088 }
8089 }
8090
8091 out:
8092
8093 --processing_template_decl;
8094 return ret;
8095 }
8096
8097 /* Since type attributes aren't mangled, we need to strip them from
8098 template type arguments. */
8099
8100 tree
8101 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8102 {
8103 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8104 return arg;
8105 bool removed_attributes = false;
8106 tree canon = strip_typedefs (arg, &removed_attributes);
8107 if (removed_attributes
8108 && (complain & tf_warning))
8109 warning (OPT_Wignored_attributes,
8110 "ignoring attributes on template argument %qT", arg);
8111 return canon;
8112 }
8113
8114 /* And from inside dependent non-type arguments like sizeof(Type). */
8115
8116 static tree
8117 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8118 {
8119 if (!arg || arg == error_mark_node)
8120 return arg;
8121 bool removed_attributes = false;
8122 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8123 if (removed_attributes
8124 && (complain & tf_warning))
8125 warning (OPT_Wignored_attributes,
8126 "ignoring attributes in template argument %qE", arg);
8127 return canon;
8128 }
8129
8130 // A template declaration can be substituted for a constrained
8131 // template template parameter only when the argument is more
8132 // constrained than the parameter.
8133 static bool
8134 is_compatible_template_arg (tree parm, tree arg)
8135 {
8136 tree parm_cons = get_constraints (parm);
8137
8138 /* For now, allow constrained template template arguments
8139 and unconstrained template template parameters. */
8140 if (parm_cons == NULL_TREE)
8141 return true;
8142
8143 /* If the template parameter is constrained, we need to rewrite its
8144 constraints in terms of the ARG's template parameters. This ensures
8145 that all of the template parameter types will have the same depth.
8146
8147 Note that this is only valid when coerce_template_template_parm is
8148 true for the innermost template parameters of PARM and ARG. In other
8149 words, because coercion is successful, this conversion will be valid. */
8150 tree new_args = NULL_TREE;
8151 if (parm_cons)
8152 {
8153 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8154 new_args = template_parms_level_to_args (aparms);
8155 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8156 tf_none, NULL_TREE);
8157 if (parm_cons == error_mark_node)
8158 return false;
8159 }
8160
8161 return weakly_subsumes (parm_cons, new_args, arg);
8162 }
8163
8164 // Convert a placeholder argument into a binding to the original
8165 // parameter. The original parameter is saved as the TREE_TYPE of
8166 // ARG.
8167 static inline tree
8168 convert_wildcard_argument (tree parm, tree arg)
8169 {
8170 TREE_TYPE (arg) = parm;
8171 return arg;
8172 }
8173
8174 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8175 because one of them is dependent. But we need to represent the
8176 conversion for the benefit of cp_tree_equal. */
8177
8178 static tree
8179 maybe_convert_nontype_argument (tree type, tree arg)
8180 {
8181 /* Auto parms get no conversion. */
8182 if (type_uses_auto (type))
8183 return arg;
8184 /* We don't need or want to add this conversion now if we're going to use the
8185 argument for deduction. */
8186 if (value_dependent_expression_p (arg))
8187 return arg;
8188
8189 type = cv_unqualified (type);
8190 tree argtype = TREE_TYPE (arg);
8191 if (same_type_p (type, argtype))
8192 return arg;
8193
8194 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8195 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8196 return arg;
8197 }
8198
8199 /* Convert the indicated template ARG as necessary to match the
8200 indicated template PARM. Returns the converted ARG, or
8201 error_mark_node if the conversion was unsuccessful. Error and
8202 warning messages are issued under control of COMPLAIN. This
8203 conversion is for the Ith parameter in the parameter list. ARGS is
8204 the full set of template arguments deduced so far. */
8205
8206 static tree
8207 convert_template_argument (tree parm,
8208 tree arg,
8209 tree args,
8210 tsubst_flags_t complain,
8211 int i,
8212 tree in_decl)
8213 {
8214 tree orig_arg;
8215 tree val;
8216 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8217
8218 if (parm == error_mark_node || error_operand_p (arg))
8219 return error_mark_node;
8220
8221 /* Trivially convert placeholders. */
8222 if (TREE_CODE (arg) == WILDCARD_DECL)
8223 return convert_wildcard_argument (parm, arg);
8224
8225 if (arg == any_targ_node)
8226 return arg;
8227
8228 if (TREE_CODE (arg) == TREE_LIST
8229 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8230 {
8231 /* The template argument was the name of some
8232 member function. That's usually
8233 invalid, but static members are OK. In any
8234 case, grab the underlying fields/functions
8235 and issue an error later if required. */
8236 TREE_TYPE (arg) = unknown_type_node;
8237 }
8238
8239 orig_arg = arg;
8240
8241 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8242 requires_type = (TREE_CODE (parm) == TYPE_DECL
8243 || requires_tmpl_type);
8244
8245 /* When determining whether an argument pack expansion is a template,
8246 look at the pattern. */
8247 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8248 arg = PACK_EXPANSION_PATTERN (arg);
8249
8250 /* Deal with an injected-class-name used as a template template arg. */
8251 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8252 {
8253 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8254 if (TREE_CODE (t) == TEMPLATE_DECL)
8255 {
8256 if (cxx_dialect >= cxx11)
8257 /* OK under DR 1004. */;
8258 else if (complain & tf_warning_or_error)
8259 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8260 " used as template template argument", TYPE_NAME (arg));
8261 else if (flag_pedantic_errors)
8262 t = arg;
8263
8264 arg = t;
8265 }
8266 }
8267
8268 is_tmpl_type =
8269 ((TREE_CODE (arg) == TEMPLATE_DECL
8270 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8271 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8272 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8273 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8274
8275 if (is_tmpl_type
8276 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8277 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8278 arg = TYPE_STUB_DECL (arg);
8279
8280 is_type = TYPE_P (arg) || is_tmpl_type;
8281
8282 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8283 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8284 {
8285 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8286 {
8287 if (complain & tf_error)
8288 error ("invalid use of destructor %qE as a type", orig_arg);
8289 return error_mark_node;
8290 }
8291
8292 permerror (input_location,
8293 "to refer to a type member of a template parameter, "
8294 "use %<typename %E%>", orig_arg);
8295
8296 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8297 TREE_OPERAND (arg, 1),
8298 typename_type,
8299 complain);
8300 arg = orig_arg;
8301 is_type = 1;
8302 }
8303 if (is_type != requires_type)
8304 {
8305 if (in_decl)
8306 {
8307 if (complain & tf_error)
8308 {
8309 error ("type/value mismatch at argument %d in template "
8310 "parameter list for %qD",
8311 i + 1, in_decl);
8312 if (is_type)
8313 {
8314 /* The template argument is a type, but we're expecting
8315 an expression. */
8316 inform (input_location,
8317 " expected a constant of type %qT, got %qT",
8318 TREE_TYPE (parm),
8319 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8320 /* [temp.arg]/2: "In a template-argument, an ambiguity
8321 between a type-id and an expression is resolved to a
8322 type-id, regardless of the form of the corresponding
8323 template-parameter." So give the user a clue. */
8324 if (TREE_CODE (arg) == FUNCTION_TYPE)
8325 inform (input_location, " ambiguous template argument "
8326 "for non-type template parameter is treated as "
8327 "function type");
8328 }
8329 else if (requires_tmpl_type)
8330 inform (input_location,
8331 " expected a class template, got %qE", orig_arg);
8332 else
8333 inform (input_location,
8334 " expected a type, got %qE", orig_arg);
8335 }
8336 }
8337 return error_mark_node;
8338 }
8339 if (is_tmpl_type ^ requires_tmpl_type)
8340 {
8341 if (in_decl && (complain & tf_error))
8342 {
8343 error ("type/value mismatch at argument %d in template "
8344 "parameter list for %qD",
8345 i + 1, in_decl);
8346 if (is_tmpl_type)
8347 inform (input_location,
8348 " expected a type, got %qT", DECL_NAME (arg));
8349 else
8350 inform (input_location,
8351 " expected a class template, got %qT", orig_arg);
8352 }
8353 return error_mark_node;
8354 }
8355
8356 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8357 /* We already did the appropriate conversion when packing args. */
8358 val = orig_arg;
8359 else if (is_type)
8360 {
8361 if (requires_tmpl_type)
8362 {
8363 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8364 /* The number of argument required is not known yet.
8365 Just accept it for now. */
8366 val = orig_arg;
8367 else
8368 {
8369 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8370 tree argparm;
8371
8372 /* Strip alias templates that are equivalent to another
8373 template. */
8374 arg = get_underlying_template (arg);
8375 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8376
8377 if (coerce_template_template_parms (parmparm, argparm,
8378 complain, in_decl,
8379 args))
8380 {
8381 val = arg;
8382
8383 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8384 TEMPLATE_DECL. */
8385 if (val != error_mark_node)
8386 {
8387 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8388 val = TREE_TYPE (val);
8389 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8390 val = make_pack_expansion (val, complain);
8391 }
8392 }
8393 else
8394 {
8395 if (in_decl && (complain & tf_error))
8396 {
8397 error ("type/value mismatch at argument %d in "
8398 "template parameter list for %qD",
8399 i + 1, in_decl);
8400 inform (input_location,
8401 " expected a template of type %qD, got %qT",
8402 parm, orig_arg);
8403 }
8404
8405 val = error_mark_node;
8406 }
8407
8408 // Check that the constraints are compatible before allowing the
8409 // substitution.
8410 if (val != error_mark_node)
8411 if (!is_compatible_template_arg (parm, arg))
8412 {
8413 if (in_decl && (complain & tf_error))
8414 {
8415 error ("constraint mismatch at argument %d in "
8416 "template parameter list for %qD",
8417 i + 1, in_decl);
8418 inform (input_location, " expected %qD but got %qD",
8419 parm, arg);
8420 }
8421 val = error_mark_node;
8422 }
8423 }
8424 }
8425 else
8426 val = orig_arg;
8427 /* We only form one instance of each template specialization.
8428 Therefore, if we use a non-canonical variant (i.e., a
8429 typedef), any future messages referring to the type will use
8430 the typedef, which is confusing if those future uses do not
8431 themselves also use the typedef. */
8432 if (TYPE_P (val))
8433 val = canonicalize_type_argument (val, complain);
8434 }
8435 else
8436 {
8437 tree t = TREE_TYPE (parm);
8438
8439 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8440 > TMPL_ARGS_DEPTH (args))
8441 /* We don't have enough levels of args to do any substitution. This
8442 can happen in the context of -fnew-ttp-matching. */;
8443 else if (tree a = type_uses_auto (t))
8444 {
8445 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8446 if (t == error_mark_node)
8447 return error_mark_node;
8448 }
8449 else
8450 t = tsubst (t, args, complain, in_decl);
8451
8452 if (invalid_nontype_parm_type_p (t, complain))
8453 return error_mark_node;
8454
8455 if (t != TREE_TYPE (parm))
8456 t = canonicalize_type_argument (t, complain);
8457
8458 if (!type_dependent_expression_p (orig_arg)
8459 && !uses_template_parms (t))
8460 /* We used to call digest_init here. However, digest_init
8461 will report errors, which we don't want when complain
8462 is zero. More importantly, digest_init will try too
8463 hard to convert things: for example, `0' should not be
8464 converted to pointer type at this point according to
8465 the standard. Accepting this is not merely an
8466 extension, since deciding whether or not these
8467 conversions can occur is part of determining which
8468 function template to call, or whether a given explicit
8469 argument specification is valid. */
8470 val = convert_nontype_argument (t, orig_arg, complain);
8471 else
8472 {
8473 val = canonicalize_expr_argument (orig_arg, complain);
8474 val = maybe_convert_nontype_argument (t, val);
8475 }
8476
8477
8478 if (val == NULL_TREE)
8479 val = error_mark_node;
8480 else if (val == error_mark_node && (complain & tf_error))
8481 error_at (cp_expr_loc_or_input_loc (orig_arg),
8482 "could not convert template argument %qE from %qT to %qT",
8483 orig_arg, TREE_TYPE (orig_arg), t);
8484
8485 if (INDIRECT_REF_P (val))
8486 {
8487 /* Reject template arguments that are references to built-in
8488 functions with no library fallbacks. */
8489 const_tree inner = TREE_OPERAND (val, 0);
8490 const_tree innertype = TREE_TYPE (inner);
8491 if (innertype
8492 && TYPE_REF_P (innertype)
8493 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8494 && TREE_OPERAND_LENGTH (inner) > 0
8495 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8496 return error_mark_node;
8497 }
8498
8499 if (TREE_CODE (val) == SCOPE_REF)
8500 {
8501 /* Strip typedefs from the SCOPE_REF. */
8502 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8503 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8504 complain);
8505 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8506 QUALIFIED_NAME_IS_TEMPLATE (val));
8507 }
8508 }
8509
8510 return val;
8511 }
8512
8513 /* Coerces the remaining template arguments in INNER_ARGS (from
8514 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8515 Returns the coerced argument pack. PARM_IDX is the position of this
8516 parameter in the template parameter list. ARGS is the original
8517 template argument list. */
8518 static tree
8519 coerce_template_parameter_pack (tree parms,
8520 int parm_idx,
8521 tree args,
8522 tree inner_args,
8523 int arg_idx,
8524 tree new_args,
8525 int* lost,
8526 tree in_decl,
8527 tsubst_flags_t complain)
8528 {
8529 tree parm = TREE_VEC_ELT (parms, parm_idx);
8530 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8531 tree packed_args;
8532 tree argument_pack;
8533 tree packed_parms = NULL_TREE;
8534
8535 if (arg_idx > nargs)
8536 arg_idx = nargs;
8537
8538 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8539 {
8540 /* When the template parameter is a non-type template parameter pack
8541 or template template parameter pack whose type or template
8542 parameters use parameter packs, we know exactly how many arguments
8543 we are looking for. Build a vector of the instantiated decls for
8544 these template parameters in PACKED_PARMS. */
8545 /* We can't use make_pack_expansion here because it would interpret a
8546 _DECL as a use rather than a declaration. */
8547 tree decl = TREE_VALUE (parm);
8548 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8549 SET_PACK_EXPANSION_PATTERN (exp, decl);
8550 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8551 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8552
8553 TREE_VEC_LENGTH (args)--;
8554 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8555 TREE_VEC_LENGTH (args)++;
8556
8557 if (packed_parms == error_mark_node)
8558 return error_mark_node;
8559
8560 /* If we're doing a partial instantiation of a member template,
8561 verify that all of the types used for the non-type
8562 template parameter pack are, in fact, valid for non-type
8563 template parameters. */
8564 if (arg_idx < nargs
8565 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8566 {
8567 int j, len = TREE_VEC_LENGTH (packed_parms);
8568 for (j = 0; j < len; ++j)
8569 {
8570 tree t = TREE_VEC_ELT (packed_parms, j);
8571 if (TREE_CODE (t) == PARM_DECL
8572 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8573 return error_mark_node;
8574 }
8575 /* We don't know how many args we have yet, just
8576 use the unconverted ones for now. */
8577 return NULL_TREE;
8578 }
8579
8580 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8581 }
8582 /* Check if we have a placeholder pack, which indicates we're
8583 in the context of a introduction list. In that case we want
8584 to match this pack to the single placeholder. */
8585 else if (arg_idx < nargs
8586 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8587 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8588 {
8589 nargs = arg_idx + 1;
8590 packed_args = make_tree_vec (1);
8591 }
8592 else
8593 packed_args = make_tree_vec (nargs - arg_idx);
8594
8595 /* Convert the remaining arguments, which will be a part of the
8596 parameter pack "parm". */
8597 int first_pack_arg = arg_idx;
8598 for (; arg_idx < nargs; ++arg_idx)
8599 {
8600 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8601 tree actual_parm = TREE_VALUE (parm);
8602 int pack_idx = arg_idx - first_pack_arg;
8603
8604 if (packed_parms)
8605 {
8606 /* Once we've packed as many args as we have types, stop. */
8607 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8608 break;
8609 else if (PACK_EXPANSION_P (arg))
8610 /* We don't know how many args we have yet, just
8611 use the unconverted ones for now. */
8612 return NULL_TREE;
8613 else
8614 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8615 }
8616
8617 if (arg == error_mark_node)
8618 {
8619 if (complain & tf_error)
8620 error ("template argument %d is invalid", arg_idx + 1);
8621 }
8622 else
8623 arg = convert_template_argument (actual_parm,
8624 arg, new_args, complain, parm_idx,
8625 in_decl);
8626 if (arg == error_mark_node)
8627 (*lost)++;
8628 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8629 }
8630
8631 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8632 && TREE_VEC_LENGTH (packed_args) > 0)
8633 {
8634 if (complain & tf_error)
8635 error ("wrong number of template arguments (%d, should be %d)",
8636 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8637 return error_mark_node;
8638 }
8639
8640 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8641 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8642 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8643 else
8644 {
8645 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8646 TREE_CONSTANT (argument_pack) = 1;
8647 }
8648
8649 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8650 if (CHECKING_P)
8651 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8652 TREE_VEC_LENGTH (packed_args));
8653 return argument_pack;
8654 }
8655
8656 /* Returns the number of pack expansions in the template argument vector
8657 ARGS. */
8658
8659 static int
8660 pack_expansion_args_count (tree args)
8661 {
8662 int i;
8663 int count = 0;
8664 if (args)
8665 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8666 {
8667 tree elt = TREE_VEC_ELT (args, i);
8668 if (elt && PACK_EXPANSION_P (elt))
8669 ++count;
8670 }
8671 return count;
8672 }
8673
8674 /* Convert all template arguments to their appropriate types, and
8675 return a vector containing the innermost resulting template
8676 arguments. If any error occurs, return error_mark_node. Error and
8677 warning messages are issued under control of COMPLAIN.
8678
8679 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8680 for arguments not specified in ARGS. Otherwise, if
8681 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8682 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8683 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8684 ARGS. */
8685
8686 static tree
8687 coerce_template_parms (tree parms,
8688 tree args,
8689 tree in_decl,
8690 tsubst_flags_t complain,
8691 bool require_all_args,
8692 bool use_default_args)
8693 {
8694 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8695 tree orig_inner_args;
8696 tree inner_args;
8697 tree new_args;
8698 tree new_inner_args;
8699
8700 /* When used as a boolean value, indicates whether this is a
8701 variadic template parameter list. Since it's an int, we can also
8702 subtract it from nparms to get the number of non-variadic
8703 parameters. */
8704 int variadic_p = 0;
8705 int variadic_args_p = 0;
8706 int post_variadic_parms = 0;
8707
8708 /* Adjustment to nparms for fixed parameter packs. */
8709 int fixed_pack_adjust = 0;
8710 int fixed_packs = 0;
8711 int missing = 0;
8712
8713 /* Likewise for parameters with default arguments. */
8714 int default_p = 0;
8715
8716 if (args == error_mark_node)
8717 return error_mark_node;
8718
8719 nparms = TREE_VEC_LENGTH (parms);
8720
8721 /* Determine if there are any parameter packs or default arguments. */
8722 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8723 {
8724 tree parm = TREE_VEC_ELT (parms, parm_idx);
8725 if (variadic_p)
8726 ++post_variadic_parms;
8727 if (template_parameter_pack_p (TREE_VALUE (parm)))
8728 ++variadic_p;
8729 if (TREE_PURPOSE (parm))
8730 ++default_p;
8731 }
8732
8733 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8734 /* If there are no parameters that follow a parameter pack, we need to
8735 expand any argument packs so that we can deduce a parameter pack from
8736 some non-packed args followed by an argument pack, as in variadic85.C.
8737 If there are such parameters, we need to leave argument packs intact
8738 so the arguments are assigned properly. This can happen when dealing
8739 with a nested class inside a partial specialization of a class
8740 template, as in variadic92.C, or when deducing a template parameter pack
8741 from a sub-declarator, as in variadic114.C. */
8742 if (!post_variadic_parms)
8743 inner_args = expand_template_argument_pack (inner_args);
8744
8745 /* Count any pack expansion args. */
8746 variadic_args_p = pack_expansion_args_count (inner_args);
8747
8748 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8749 if ((nargs - variadic_args_p > nparms && !variadic_p)
8750 || (nargs < nparms - variadic_p
8751 && require_all_args
8752 && !variadic_args_p
8753 && (!use_default_args
8754 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8755 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8756 {
8757 bad_nargs:
8758 if (complain & tf_error)
8759 {
8760 if (variadic_p || default_p)
8761 {
8762 nparms -= variadic_p + default_p;
8763 error ("wrong number of template arguments "
8764 "(%d, should be at least %d)", nargs, nparms);
8765 }
8766 else
8767 error ("wrong number of template arguments "
8768 "(%d, should be %d)", nargs, nparms);
8769
8770 if (in_decl)
8771 inform (DECL_SOURCE_LOCATION (in_decl),
8772 "provided for %qD", in_decl);
8773 }
8774
8775 return error_mark_node;
8776 }
8777 /* We can't pass a pack expansion to a non-pack parameter of an alias
8778 template (DR 1430). */
8779 else if (in_decl
8780 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8781 || concept_definition_p (in_decl))
8782 && variadic_args_p
8783 && nargs - variadic_args_p < nparms - variadic_p)
8784 {
8785 if (complain & tf_error)
8786 {
8787 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8788 {
8789 tree arg = TREE_VEC_ELT (inner_args, i);
8790 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8791
8792 if (PACK_EXPANSION_P (arg)
8793 && !template_parameter_pack_p (parm))
8794 {
8795 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8796 error_at (location_of (arg),
8797 "pack expansion argument for non-pack parameter "
8798 "%qD of alias template %qD", parm, in_decl);
8799 else
8800 error_at (location_of (arg),
8801 "pack expansion argument for non-pack parameter "
8802 "%qD of concept %qD", parm, in_decl);
8803 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8804 goto found;
8805 }
8806 }
8807 gcc_unreachable ();
8808 found:;
8809 }
8810 return error_mark_node;
8811 }
8812
8813 /* We need to evaluate the template arguments, even though this
8814 template-id may be nested within a "sizeof". */
8815 cp_evaluated ev;
8816
8817 new_inner_args = make_tree_vec (nparms);
8818 new_args = add_outermost_template_args (args, new_inner_args);
8819 int pack_adjust = 0;
8820 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8821 {
8822 tree arg;
8823 tree parm;
8824
8825 /* Get the Ith template parameter. */
8826 parm = TREE_VEC_ELT (parms, parm_idx);
8827
8828 if (parm == error_mark_node)
8829 {
8830 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8831 continue;
8832 }
8833
8834 /* Calculate the next argument. */
8835 if (arg_idx < nargs)
8836 arg = TREE_VEC_ELT (inner_args, arg_idx);
8837 else
8838 arg = NULL_TREE;
8839
8840 if (template_parameter_pack_p (TREE_VALUE (parm))
8841 && (arg || require_all_args || !(complain & tf_partial))
8842 && !(arg && ARGUMENT_PACK_P (arg)))
8843 {
8844 /* Some arguments will be placed in the
8845 template parameter pack PARM. */
8846 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8847 inner_args, arg_idx,
8848 new_args, &lost,
8849 in_decl, complain);
8850
8851 if (arg == NULL_TREE)
8852 {
8853 /* We don't know how many args we have yet, just use the
8854 unconverted (and still packed) ones for now. */
8855 new_inner_args = orig_inner_args;
8856 arg_idx = nargs;
8857 break;
8858 }
8859
8860 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8861
8862 /* Store this argument. */
8863 if (arg == error_mark_node)
8864 {
8865 lost++;
8866 /* We are done with all of the arguments. */
8867 arg_idx = nargs;
8868 break;
8869 }
8870 else
8871 {
8872 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8873 arg_idx += pack_adjust;
8874 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8875 {
8876 ++fixed_packs;
8877 fixed_pack_adjust += pack_adjust;
8878 }
8879 }
8880
8881 continue;
8882 }
8883 else if (arg)
8884 {
8885 if (PACK_EXPANSION_P (arg))
8886 {
8887 /* "If every valid specialization of a variadic template
8888 requires an empty template parameter pack, the template is
8889 ill-formed, no diagnostic required." So check that the
8890 pattern works with this parameter. */
8891 tree pattern = PACK_EXPANSION_PATTERN (arg);
8892 tree conv = convert_template_argument (TREE_VALUE (parm),
8893 pattern, new_args,
8894 complain, parm_idx,
8895 in_decl);
8896 if (conv == error_mark_node)
8897 {
8898 if (complain & tf_error)
8899 inform (input_location, "so any instantiation with a "
8900 "non-empty parameter pack would be ill-formed");
8901 ++lost;
8902 }
8903 else if (TYPE_P (conv) && !TYPE_P (pattern))
8904 /* Recover from missing typename. */
8905 TREE_VEC_ELT (inner_args, arg_idx)
8906 = make_pack_expansion (conv, complain);
8907
8908 /* We don't know how many args we have yet, just
8909 use the unconverted ones for now. */
8910 new_inner_args = inner_args;
8911 arg_idx = nargs;
8912 break;
8913 }
8914 }
8915 else if (require_all_args)
8916 {
8917 /* There must be a default arg in this case. */
8918 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8919 complain, in_decl);
8920 /* The position of the first default template argument,
8921 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8922 Record that. */
8923 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8924 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8925 arg_idx - pack_adjust);
8926 }
8927 else
8928 break;
8929
8930 if (arg == error_mark_node)
8931 {
8932 if (complain & tf_error)
8933 error ("template argument %d is invalid", arg_idx + 1);
8934 }
8935 else if (!arg)
8936 {
8937 /* This can occur if there was an error in the template
8938 parameter list itself (which we would already have
8939 reported) that we are trying to recover from, e.g., a class
8940 template with a parameter list such as
8941 template<typename..., typename> (cpp0x/variadic150.C). */
8942 ++lost;
8943
8944 /* This can also happen with a fixed parameter pack (71834). */
8945 if (arg_idx >= nargs)
8946 ++missing;
8947 }
8948 else
8949 arg = convert_template_argument (TREE_VALUE (parm),
8950 arg, new_args, complain,
8951 parm_idx, in_decl);
8952
8953 if (arg == error_mark_node)
8954 lost++;
8955
8956 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8957 }
8958
8959 if (missing || arg_idx < nargs - variadic_args_p)
8960 {
8961 /* If we had fixed parameter packs, we didn't know how many arguments we
8962 actually needed earlier; now we do. */
8963 nparms += fixed_pack_adjust;
8964 variadic_p -= fixed_packs;
8965 goto bad_nargs;
8966 }
8967
8968 if (arg_idx < nargs)
8969 {
8970 /* We had some pack expansion arguments that will only work if the packs
8971 are empty, but wait until instantiation time to complain.
8972 See variadic-ttp3.C. */
8973
8974 /* Except that we can't provide empty packs to alias templates or
8975 concepts when there are no corresponding parameters. Basically,
8976 we can get here with this:
8977
8978 template<typename T> concept C = true;
8979
8980 template<typename... Args>
8981 requires C<Args...>
8982 void f();
8983
8984 When parsing C<Args...>, we try to form a concept check of
8985 C<?, Args...>. Without the extra check for substituting an empty
8986 pack past the last parameter, we can accept the check as valid.
8987
8988 FIXME: This may be valid for alias templates (but I doubt it).
8989
8990 FIXME: The error could be better also. */
8991 if (in_decl && concept_definition_p (in_decl))
8992 {
8993 if (complain & tf_error)
8994 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8995 "too many arguments");
8996 return error_mark_node;
8997 }
8998
8999 int len = nparms + (nargs - arg_idx);
9000 tree args = make_tree_vec (len);
9001 int i = 0;
9002 for (; i < nparms; ++i)
9003 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9004 for (; i < len; ++i, ++arg_idx)
9005 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9006 arg_idx - pack_adjust);
9007 new_inner_args = args;
9008 }
9009
9010 if (lost)
9011 {
9012 gcc_assert (!(complain & tf_error) || seen_error ());
9013 return error_mark_node;
9014 }
9015
9016 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9017 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9018 TREE_VEC_LENGTH (new_inner_args));
9019
9020 return new_inner_args;
9021 }
9022
9023 /* Convert all template arguments to their appropriate types, and
9024 return a vector containing the innermost resulting template
9025 arguments. If any error occurs, return error_mark_node. Error and
9026 warning messages are not issued.
9027
9028 Note that no function argument deduction is performed, and default
9029 arguments are used to fill in unspecified arguments. */
9030 tree
9031 coerce_template_parms (tree parms, tree args, tree in_decl)
9032 {
9033 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9034 }
9035
9036 /* Convert all template arguments to their appropriate type, and
9037 instantiate default arguments as needed. This returns a vector
9038 containing the innermost resulting template arguments, or
9039 error_mark_node if unsuccessful. */
9040 tree
9041 coerce_template_parms (tree parms, tree args, tree in_decl,
9042 tsubst_flags_t complain)
9043 {
9044 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9045 }
9046
9047 /* Like coerce_template_parms. If PARMS represents all template
9048 parameters levels, this function returns a vector of vectors
9049 representing all the resulting argument levels. Note that in this
9050 case, only the innermost arguments are coerced because the
9051 outermost ones are supposed to have been coerced already.
9052
9053 Otherwise, if PARMS represents only (the innermost) vector of
9054 parameters, this function returns a vector containing just the
9055 innermost resulting arguments. */
9056
9057 static tree
9058 coerce_innermost_template_parms (tree parms,
9059 tree args,
9060 tree in_decl,
9061 tsubst_flags_t complain,
9062 bool require_all_args,
9063 bool use_default_args)
9064 {
9065 int parms_depth = TMPL_PARMS_DEPTH (parms);
9066 int args_depth = TMPL_ARGS_DEPTH (args);
9067 tree coerced_args;
9068
9069 if (parms_depth > 1)
9070 {
9071 coerced_args = make_tree_vec (parms_depth);
9072 tree level;
9073 int cur_depth;
9074
9075 for (level = parms, cur_depth = parms_depth;
9076 parms_depth > 0 && level != NULL_TREE;
9077 level = TREE_CHAIN (level), --cur_depth)
9078 {
9079 tree l;
9080 if (cur_depth == args_depth)
9081 l = coerce_template_parms (TREE_VALUE (level),
9082 args, in_decl, complain,
9083 require_all_args,
9084 use_default_args);
9085 else
9086 l = TMPL_ARGS_LEVEL (args, cur_depth);
9087
9088 if (l == error_mark_node)
9089 return error_mark_node;
9090
9091 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9092 }
9093 }
9094 else
9095 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9096 args, in_decl, complain,
9097 require_all_args,
9098 use_default_args);
9099 return coerced_args;
9100 }
9101
9102 /* Returns true if T is a wrapper to make a C++20 template parameter
9103 object const. */
9104
9105 static bool
9106 class_nttp_const_wrapper_p (tree t)
9107 {
9108 if (cxx_dialect < cxx20)
9109 return false;
9110 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9111 && CP_TYPE_CONST_P (TREE_TYPE (t))
9112 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9113 }
9114
9115 /* Returns 1 if template args OT and NT are equivalent. */
9116
9117 int
9118 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9119 {
9120 if (nt == ot)
9121 return 1;
9122 if (nt == NULL_TREE || ot == NULL_TREE)
9123 return false;
9124 if (nt == any_targ_node || ot == any_targ_node)
9125 return true;
9126
9127 if (class_nttp_const_wrapper_p (nt))
9128 nt = TREE_OPERAND (nt, 0);
9129 if (class_nttp_const_wrapper_p (ot))
9130 ot = TREE_OPERAND (ot, 0);
9131
9132 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9133 /* For member templates */
9134 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9135 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9136 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9137 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9138 PACK_EXPANSION_PATTERN (nt))
9139 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9140 PACK_EXPANSION_EXTRA_ARGS (nt)));
9141 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9142 return cp_tree_equal (ot, nt);
9143 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9144 gcc_unreachable ();
9145 else if (TYPE_P (nt) || TYPE_P (ot))
9146 {
9147 if (!(TYPE_P (nt) && TYPE_P (ot)))
9148 return false;
9149 /* Don't treat an alias template specialization with dependent
9150 arguments as equivalent to its underlying type when used as a
9151 template argument; we need them to be distinct so that we
9152 substitute into the specialization arguments at instantiation
9153 time. And aliases can't be equivalent without being ==, so
9154 we don't need to look any deeper.
9155
9156 During partial ordering, however, we need to treat them normally so
9157 that we can order uses of the same alias with different
9158 cv-qualification (79960). */
9159 if (!partial_order
9160 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9161 return false;
9162 else
9163 return same_type_p (ot, nt);
9164 }
9165 else
9166 {
9167 /* Try to treat a template non-type argument that has been converted
9168 to the parameter type as equivalent to one that hasn't yet. */
9169 for (enum tree_code code1 = TREE_CODE (ot);
9170 CONVERT_EXPR_CODE_P (code1)
9171 || code1 == NON_LVALUE_EXPR;
9172 code1 = TREE_CODE (ot))
9173 ot = TREE_OPERAND (ot, 0);
9174
9175 for (enum tree_code code2 = TREE_CODE (nt);
9176 CONVERT_EXPR_CODE_P (code2)
9177 || code2 == NON_LVALUE_EXPR;
9178 code2 = TREE_CODE (nt))
9179 nt = TREE_OPERAND (nt, 0);
9180
9181 return cp_tree_equal (ot, nt);
9182 }
9183 }
9184
9185 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9186 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9187 NEWARG_PTR with the offending arguments if they are non-NULL. */
9188
9189 int
9190 comp_template_args (tree oldargs, tree newargs,
9191 tree *oldarg_ptr, tree *newarg_ptr,
9192 bool partial_order)
9193 {
9194 int i;
9195
9196 if (oldargs == newargs)
9197 return 1;
9198
9199 if (!oldargs || !newargs)
9200 return 0;
9201
9202 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9203 return 0;
9204
9205 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9206 {
9207 tree nt = TREE_VEC_ELT (newargs, i);
9208 tree ot = TREE_VEC_ELT (oldargs, i);
9209
9210 if (! template_args_equal (ot, nt, partial_order))
9211 {
9212 if (oldarg_ptr != NULL)
9213 *oldarg_ptr = ot;
9214 if (newarg_ptr != NULL)
9215 *newarg_ptr = nt;
9216 return 0;
9217 }
9218 }
9219 return 1;
9220 }
9221
9222 inline bool
9223 comp_template_args_porder (tree oargs, tree nargs)
9224 {
9225 return comp_template_args (oargs, nargs, NULL, NULL, true);
9226 }
9227
9228 /* Implement a freelist interface for objects of type T.
9229
9230 Head is a separate object, rather than a regular member, so that we
9231 can define it as a GTY deletable pointer, which is highly
9232 desirable. A data member could be declared that way, but then the
9233 containing object would implicitly get GTY((user)), which would
9234 prevent us from instantiating freelists as global objects.
9235 Although this way we can create freelist global objects, they're
9236 such thin wrappers that instantiating temporaries at every use
9237 loses nothing and saves permanent storage for the freelist object.
9238
9239 Member functions next, anew, poison and reinit have default
9240 implementations that work for most of the types we're interested
9241 in, but if they don't work for some type, they should be explicitly
9242 specialized. See the comments before them for requirements, and
9243 the example specializations for the tree_list_freelist. */
9244 template <typename T>
9245 class freelist
9246 {
9247 /* Return the next object in a chain. We could just do type
9248 punning, but if we access the object with its underlying type, we
9249 avoid strict-aliasing trouble. This needs only work between
9250 poison and reinit. */
9251 static T *&next (T *obj) { return obj->next; }
9252
9253 /* Return a newly allocated, uninitialized or minimally-initialized
9254 object of type T. Any initialization performed by anew should
9255 either remain across the life of the object and the execution of
9256 poison, or be redone by reinit. */
9257 static T *anew () { return ggc_alloc<T> (); }
9258
9259 /* Optionally scribble all over the bits holding the object, so that
9260 they become (mostly?) uninitialized memory. This is called while
9261 preparing to make the object part of the free list. */
9262 static void poison (T *obj) {
9263 T *p ATTRIBUTE_UNUSED = obj;
9264 T **q ATTRIBUTE_UNUSED = &next (obj);
9265
9266 #ifdef ENABLE_GC_CHECKING
9267 /* Poison the data, to indicate the data is garbage. */
9268 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9269 memset (p, 0xa5, sizeof (*p));
9270 #endif
9271 /* Let valgrind know the object is free. */
9272 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9273
9274 /* Let valgrind know the next portion of the object is available,
9275 but uninitialized. */
9276 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9277 }
9278
9279 /* Bring an object that underwent at least one lifecycle after anew
9280 and before the most recent free and poison, back to a usable
9281 state, reinitializing whatever is needed for it to be
9282 functionally equivalent to an object just allocated and returned
9283 by anew. This may poison or clear the next field, used by
9284 freelist housekeeping after poison was called. */
9285 static void reinit (T *obj) {
9286 T **q ATTRIBUTE_UNUSED = &next (obj);
9287
9288 #ifdef ENABLE_GC_CHECKING
9289 memset (q, 0xa5, sizeof (*q));
9290 #endif
9291 /* Let valgrind know the entire object is available, but
9292 uninitialized. */
9293 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9294 }
9295
9296 /* Reference a GTY-deletable pointer that points to the first object
9297 in the free list proper. */
9298 T *&head;
9299 public:
9300 /* Construct a freelist object chaining objects off of HEAD. */
9301 freelist (T *&head) : head(head) {}
9302
9303 /* Add OBJ to the free object list. The former head becomes OBJ's
9304 successor. */
9305 void free (T *obj)
9306 {
9307 poison (obj);
9308 next (obj) = head;
9309 head = obj;
9310 }
9311
9312 /* Take an object from the free list, if one is available, or
9313 allocate a new one. Objects taken from the free list should be
9314 regarded as filled with garbage, except for bits that are
9315 configured to be preserved across free and alloc. */
9316 T *alloc ()
9317 {
9318 if (head)
9319 {
9320 T *obj = head;
9321 head = next (head);
9322 reinit (obj);
9323 return obj;
9324 }
9325 else
9326 return anew ();
9327 }
9328 };
9329
9330 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9331 want to allocate a TREE_LIST using the usual interface, and ensure
9332 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9333 build_tree_list logic in reinit, so this could go out of sync. */
9334 template <>
9335 inline tree &
9336 freelist<tree_node>::next (tree obj)
9337 {
9338 return TREE_CHAIN (obj);
9339 }
9340 template <>
9341 inline tree
9342 freelist<tree_node>::anew ()
9343 {
9344 return build_tree_list (NULL, NULL);
9345 }
9346 template <>
9347 inline void
9348 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9349 {
9350 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9351 tree p ATTRIBUTE_UNUSED = obj;
9352 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9353 tree *q ATTRIBUTE_UNUSED = &next (obj);
9354
9355 #ifdef ENABLE_GC_CHECKING
9356 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9357
9358 /* Poison the data, to indicate the data is garbage. */
9359 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9360 memset (p, 0xa5, size);
9361 #endif
9362 /* Let valgrind know the object is free. */
9363 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9364 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9367
9368 #ifdef ENABLE_GC_CHECKING
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9370 /* Keep TREE_CHAIN functional. */
9371 TREE_SET_CODE (obj, TREE_LIST);
9372 #else
9373 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9374 #endif
9375 }
9376 template <>
9377 inline void
9378 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9379 {
9380 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9381
9382 #ifdef ENABLE_GC_CHECKING
9383 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9384 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9385 memset (obj, 0, sizeof (tree_list));
9386 #endif
9387
9388 /* Let valgrind know the entire object is available, but
9389 uninitialized. */
9390 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9391
9392 #ifdef ENABLE_GC_CHECKING
9393 TREE_SET_CODE (obj, TREE_LIST);
9394 #else
9395 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9396 #endif
9397 }
9398
9399 /* Point to the first object in the TREE_LIST freelist. */
9400 static GTY((deletable)) tree tree_list_freelist_head;
9401 /* Return the/an actual TREE_LIST freelist. */
9402 static inline freelist<tree_node>
9403 tree_list_freelist ()
9404 {
9405 return tree_list_freelist_head;
9406 }
9407
9408 /* Point to the first object in the tinst_level freelist. */
9409 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9410 /* Return the/an actual tinst_level freelist. */
9411 static inline freelist<tinst_level>
9412 tinst_level_freelist ()
9413 {
9414 return tinst_level_freelist_head;
9415 }
9416
9417 /* Point to the first object in the pending_template freelist. */
9418 static GTY((deletable)) pending_template *pending_template_freelist_head;
9419 /* Return the/an actual pending_template freelist. */
9420 static inline freelist<pending_template>
9421 pending_template_freelist ()
9422 {
9423 return pending_template_freelist_head;
9424 }
9425
9426 /* Build the TREE_LIST object out of a split list, store it
9427 permanently, and return it. */
9428 tree
9429 tinst_level::to_list ()
9430 {
9431 gcc_assert (split_list_p ());
9432 tree ret = tree_list_freelist ().alloc ();
9433 TREE_PURPOSE (ret) = tldcl;
9434 TREE_VALUE (ret) = targs;
9435 tldcl = ret;
9436 targs = NULL;
9437 gcc_assert (tree_list_p ());
9438 return ret;
9439 }
9440
9441 const unsigned short tinst_level::refcount_infinity;
9442
9443 /* Increment OBJ's refcount unless it is already infinite. */
9444 static tinst_level *
9445 inc_refcount_use (tinst_level *obj)
9446 {
9447 if (obj && obj->refcount != tinst_level::refcount_infinity)
9448 ++obj->refcount;
9449 return obj;
9450 }
9451
9452 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9453 void
9454 tinst_level::free (tinst_level *obj)
9455 {
9456 if (obj->tree_list_p ())
9457 tree_list_freelist ().free (obj->get_node ());
9458 tinst_level_freelist ().free (obj);
9459 }
9460
9461 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9462 OBJ's DECL and OBJ, and start over with the tinst_level object that
9463 used to be referenced by OBJ's NEXT. */
9464 static void
9465 dec_refcount_use (tinst_level *obj)
9466 {
9467 while (obj
9468 && obj->refcount != tinst_level::refcount_infinity
9469 && !--obj->refcount)
9470 {
9471 tinst_level *next = obj->next;
9472 tinst_level::free (obj);
9473 obj = next;
9474 }
9475 }
9476
9477 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9478 and of the former PTR. Omitting the second argument is equivalent
9479 to passing (T*)NULL; this is allowed because passing the
9480 zero-valued integral constant NULL confuses type deduction and/or
9481 overload resolution. */
9482 template <typename T>
9483 static void
9484 set_refcount_ptr (T *& ptr, T *obj = NULL)
9485 {
9486 T *save = ptr;
9487 ptr = inc_refcount_use (obj);
9488 dec_refcount_use (save);
9489 }
9490
9491 static void
9492 add_pending_template (tree d)
9493 {
9494 tree ti = (TYPE_P (d)
9495 ? CLASSTYPE_TEMPLATE_INFO (d)
9496 : DECL_TEMPLATE_INFO (d));
9497 struct pending_template *pt;
9498 int level;
9499
9500 if (TI_PENDING_TEMPLATE_FLAG (ti))
9501 return;
9502
9503 /* We are called both from instantiate_decl, where we've already had a
9504 tinst_level pushed, and instantiate_template, where we haven't.
9505 Compensate. */
9506 gcc_assert (TREE_CODE (d) != TREE_LIST);
9507 level = !current_tinst_level
9508 || current_tinst_level->maybe_get_node () != d;
9509
9510 if (level)
9511 push_tinst_level (d);
9512
9513 pt = pending_template_freelist ().alloc ();
9514 pt->next = NULL;
9515 pt->tinst = NULL;
9516 set_refcount_ptr (pt->tinst, current_tinst_level);
9517 if (last_pending_template)
9518 last_pending_template->next = pt;
9519 else
9520 pending_templates = pt;
9521
9522 last_pending_template = pt;
9523
9524 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9525
9526 if (level)
9527 pop_tinst_level ();
9528 }
9529
9530
9531 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9532 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9533 documentation for TEMPLATE_ID_EXPR. */
9534
9535 tree
9536 lookup_template_function (tree fns, tree arglist)
9537 {
9538 if (fns == error_mark_node || arglist == error_mark_node)
9539 return error_mark_node;
9540
9541 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9542
9543 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9544 {
9545 error ("%q#D is not a function template", fns);
9546 return error_mark_node;
9547 }
9548
9549 if (BASELINK_P (fns))
9550 {
9551 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9552 unknown_type_node,
9553 BASELINK_FUNCTIONS (fns),
9554 arglist);
9555 return fns;
9556 }
9557
9558 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9559 }
9560
9561 /* Within the scope of a template class S<T>, the name S gets bound
9562 (in build_self_reference) to a TYPE_DECL for the class, not a
9563 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9564 or one of its enclosing classes, and that type is a template,
9565 return the associated TEMPLATE_DECL. Otherwise, the original
9566 DECL is returned.
9567
9568 Also handle the case when DECL is a TREE_LIST of ambiguous
9569 injected-class-names from different bases. */
9570
9571 tree
9572 maybe_get_template_decl_from_type_decl (tree decl)
9573 {
9574 if (decl == NULL_TREE)
9575 return decl;
9576
9577 /* DR 176: A lookup that finds an injected-class-name (10.2
9578 [class.member.lookup]) can result in an ambiguity in certain cases
9579 (for example, if it is found in more than one base class). If all of
9580 the injected-class-names that are found refer to specializations of
9581 the same class template, and if the name is followed by a
9582 template-argument-list, the reference refers to the class template
9583 itself and not a specialization thereof, and is not ambiguous. */
9584 if (TREE_CODE (decl) == TREE_LIST)
9585 {
9586 tree t, tmpl = NULL_TREE;
9587 for (t = decl; t; t = TREE_CHAIN (t))
9588 {
9589 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9590 if (!tmpl)
9591 tmpl = elt;
9592 else if (tmpl != elt)
9593 break;
9594 }
9595 if (tmpl && t == NULL_TREE)
9596 return tmpl;
9597 else
9598 return decl;
9599 }
9600
9601 return (decl != NULL_TREE
9602 && DECL_SELF_REFERENCE_P (decl)
9603 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9604 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9605 }
9606
9607 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9608 parameters, find the desired type.
9609
9610 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9611
9612 IN_DECL, if non-NULL, is the template declaration we are trying to
9613 instantiate.
9614
9615 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9616 the class we are looking up.
9617
9618 Issue error and warning messages under control of COMPLAIN.
9619
9620 If the template class is really a local class in a template
9621 function, then the FUNCTION_CONTEXT is the function in which it is
9622 being instantiated.
9623
9624 ??? Note that this function is currently called *twice* for each
9625 template-id: the first time from the parser, while creating the
9626 incomplete type (finish_template_type), and the second type during the
9627 real instantiation (instantiate_template_class). This is surely something
9628 that we want to avoid. It also causes some problems with argument
9629 coercion (see convert_nontype_argument for more information on this). */
9630
9631 static tree
9632 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9633 int entering_scope, tsubst_flags_t complain)
9634 {
9635 tree templ = NULL_TREE, parmlist;
9636 tree t;
9637 spec_entry **slot;
9638 spec_entry *entry;
9639 spec_entry elt;
9640 hashval_t hash;
9641
9642 if (identifier_p (d1))
9643 {
9644 tree value = innermost_non_namespace_value (d1);
9645 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9646 templ = value;
9647 else
9648 {
9649 if (context)
9650 push_decl_namespace (context);
9651 templ = lookup_name (d1);
9652 templ = maybe_get_template_decl_from_type_decl (templ);
9653 if (context)
9654 pop_decl_namespace ();
9655 }
9656 if (templ)
9657 context = DECL_CONTEXT (templ);
9658 }
9659 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9660 {
9661 tree type = TREE_TYPE (d1);
9662
9663 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9664 an implicit typename for the second A. Deal with it. */
9665 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9666 type = TREE_TYPE (type);
9667
9668 if (CLASSTYPE_TEMPLATE_INFO (type))
9669 {
9670 templ = CLASSTYPE_TI_TEMPLATE (type);
9671 d1 = DECL_NAME (templ);
9672 }
9673 }
9674 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9675 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9676 {
9677 templ = TYPE_TI_TEMPLATE (d1);
9678 d1 = DECL_NAME (templ);
9679 }
9680 else if (DECL_TYPE_TEMPLATE_P (d1))
9681 {
9682 templ = d1;
9683 d1 = DECL_NAME (templ);
9684 context = DECL_CONTEXT (templ);
9685 }
9686 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9687 {
9688 templ = d1;
9689 d1 = DECL_NAME (templ);
9690 }
9691
9692 /* Issue an error message if we didn't find a template. */
9693 if (! templ)
9694 {
9695 if (complain & tf_error)
9696 error ("%qT is not a template", d1);
9697 return error_mark_node;
9698 }
9699
9700 if (TREE_CODE (templ) != TEMPLATE_DECL
9701 /* Make sure it's a user visible template, if it was named by
9702 the user. */
9703 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9704 && !PRIMARY_TEMPLATE_P (templ)))
9705 {
9706 if (complain & tf_error)
9707 {
9708 error ("non-template type %qT used as a template", d1);
9709 if (in_decl)
9710 error ("for template declaration %q+D", in_decl);
9711 }
9712 return error_mark_node;
9713 }
9714
9715 complain &= ~tf_user;
9716
9717 /* An alias that just changes the name of a template is equivalent to the
9718 other template, so if any of the arguments are pack expansions, strip
9719 the alias to avoid problems with a pack expansion passed to a non-pack
9720 alias template parameter (DR 1430). */
9721 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9722 templ = get_underlying_template (templ);
9723
9724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9725 {
9726 tree parm;
9727 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9728 if (arglist2 == error_mark_node
9729 || (!uses_template_parms (arglist2)
9730 && check_instantiated_args (templ, arglist2, complain)))
9731 return error_mark_node;
9732
9733 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9734 return parm;
9735 }
9736 else
9737 {
9738 tree template_type = TREE_TYPE (templ);
9739 tree gen_tmpl;
9740 tree type_decl;
9741 tree found = NULL_TREE;
9742 int arg_depth;
9743 int parm_depth;
9744 int is_dependent_type;
9745 int use_partial_inst_tmpl = false;
9746
9747 if (template_type == error_mark_node)
9748 /* An error occurred while building the template TEMPL, and a
9749 diagnostic has most certainly been emitted for that
9750 already. Let's propagate that error. */
9751 return error_mark_node;
9752
9753 gen_tmpl = most_general_template (templ);
9754 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9755 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9756 arg_depth = TMPL_ARGS_DEPTH (arglist);
9757
9758 if (arg_depth == 1 && parm_depth > 1)
9759 {
9760 /* We've been given an incomplete set of template arguments.
9761 For example, given:
9762
9763 template <class T> struct S1 {
9764 template <class U> struct S2 {};
9765 template <class U> struct S2<U*> {};
9766 };
9767
9768 we will be called with an ARGLIST of `U*', but the
9769 TEMPLATE will be `template <class T> template
9770 <class U> struct S1<T>::S2'. We must fill in the missing
9771 arguments. */
9772 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9773 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9774 arg_depth = TMPL_ARGS_DEPTH (arglist);
9775 }
9776
9777 /* Now we should have enough arguments. */
9778 gcc_assert (parm_depth == arg_depth);
9779
9780 /* From here on, we're only interested in the most general
9781 template. */
9782
9783 /* Calculate the BOUND_ARGS. These will be the args that are
9784 actually tsubst'd into the definition to create the
9785 instantiation. */
9786 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9787 complain,
9788 /*require_all_args=*/true,
9789 /*use_default_args=*/true);
9790
9791 if (arglist == error_mark_node)
9792 /* We were unable to bind the arguments. */
9793 return error_mark_node;
9794
9795 /* In the scope of a template class, explicit references to the
9796 template class refer to the type of the template, not any
9797 instantiation of it. For example, in:
9798
9799 template <class T> class C { void f(C<T>); }
9800
9801 the `C<T>' is just the same as `C'. Outside of the
9802 class, however, such a reference is an instantiation. */
9803 if (entering_scope
9804 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9805 || currently_open_class (template_type))
9806 {
9807 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9808
9809 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9810 return template_type;
9811 }
9812
9813 /* If we already have this specialization, return it. */
9814 elt.tmpl = gen_tmpl;
9815 elt.args = arglist;
9816 elt.spec = NULL_TREE;
9817 hash = spec_hasher::hash (&elt);
9818 entry = type_specializations->find_with_hash (&elt, hash);
9819
9820 if (entry)
9821 return entry->spec;
9822
9823 /* If the template's constraints are not satisfied,
9824 then we cannot form a valid type.
9825
9826 Note that the check is deferred until after the hash
9827 lookup. This prevents redundant checks on previously
9828 instantiated specializations. */
9829 if (flag_concepts
9830 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9831 && !constraints_satisfied_p (gen_tmpl, arglist))
9832 {
9833 if (complain & tf_error)
9834 {
9835 auto_diagnostic_group d;
9836 error ("template constraint failure for %qD", gen_tmpl);
9837 diagnose_constraints (input_location, gen_tmpl, arglist);
9838 }
9839 return error_mark_node;
9840 }
9841
9842 is_dependent_type = uses_template_parms (arglist);
9843
9844 /* If the deduced arguments are invalid, then the binding
9845 failed. */
9846 if (!is_dependent_type
9847 && check_instantiated_args (gen_tmpl,
9848 INNERMOST_TEMPLATE_ARGS (arglist),
9849 complain))
9850 return error_mark_node;
9851
9852 if (!is_dependent_type
9853 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9854 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9855 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9856 /* This occurs when the user has tried to define a tagged type
9857 in a scope that forbids it. We emitted an error during the
9858 parse. We didn't complete the bail out then, so here we
9859 are. */
9860 return error_mark_node;
9861
9862 context = DECL_CONTEXT (gen_tmpl);
9863 if (context && TYPE_P (context))
9864 {
9865 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9866 context = complete_type (context);
9867 }
9868 else
9869 context = tsubst (context, arglist, complain, in_decl);
9870
9871 if (context == error_mark_node)
9872 return error_mark_node;
9873
9874 if (!context)
9875 context = global_namespace;
9876
9877 /* Create the type. */
9878 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9879 {
9880 /* The user referred to a specialization of an alias
9881 template represented by GEN_TMPL.
9882
9883 [temp.alias]/2 says:
9884
9885 When a template-id refers to the specialization of an
9886 alias template, it is equivalent to the associated
9887 type obtained by substitution of its
9888 template-arguments for the template-parameters in the
9889 type-id of the alias template. */
9890
9891 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9892 /* Note that the call above (by indirectly calling
9893 register_specialization in tsubst_decl) registers the
9894 TYPE_DECL representing the specialization of the alias
9895 template. So next time someone substitutes ARGLIST for
9896 the template parms into the alias template (GEN_TMPL),
9897 she'll get that TYPE_DECL back. */
9898
9899 if (t == error_mark_node)
9900 return t;
9901 }
9902 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9903 {
9904 if (!is_dependent_type)
9905 {
9906 set_current_access_from_decl (TYPE_NAME (template_type));
9907 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9908 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9909 arglist, complain, in_decl),
9910 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9911 arglist, complain, in_decl),
9912 SCOPED_ENUM_P (template_type), NULL);
9913
9914 if (t == error_mark_node)
9915 return t;
9916 }
9917 else
9918 {
9919 /* We don't want to call start_enum for this type, since
9920 the values for the enumeration constants may involve
9921 template parameters. And, no one should be interested
9922 in the enumeration constants for such a type. */
9923 t = cxx_make_type (ENUMERAL_TYPE);
9924 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9925 }
9926 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9927 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9928 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9929 }
9930 else if (CLASS_TYPE_P (template_type))
9931 {
9932 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9933 instantiated here. */
9934 gcc_assert (!LAMBDA_TYPE_P (template_type));
9935
9936 t = make_class_type (TREE_CODE (template_type));
9937 CLASSTYPE_DECLARED_CLASS (t)
9938 = CLASSTYPE_DECLARED_CLASS (template_type);
9939 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9940
9941 /* A local class. Make sure the decl gets registered properly. */
9942 if (context == current_function_decl)
9943 if (pushtag (DECL_NAME (gen_tmpl), t)
9944 == error_mark_node)
9945 return error_mark_node;
9946
9947 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9948 /* This instantiation is another name for the primary
9949 template type. Set the TYPE_CANONICAL field
9950 appropriately. */
9951 TYPE_CANONICAL (t) = template_type;
9952 else if (any_template_arguments_need_structural_equality_p (arglist))
9953 /* Some of the template arguments require structural
9954 equality testing, so this template class requires
9955 structural equality testing. */
9956 SET_TYPE_STRUCTURAL_EQUALITY (t);
9957 }
9958 else
9959 gcc_unreachable ();
9960
9961 /* If we called start_enum or pushtag above, this information
9962 will already be set up. */
9963 type_decl = TYPE_NAME (t);
9964 if (!type_decl)
9965 {
9966 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9967
9968 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9969 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9970 DECL_SOURCE_LOCATION (type_decl)
9971 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9972 }
9973
9974 if (CLASS_TYPE_P (template_type))
9975 {
9976 TREE_PRIVATE (type_decl)
9977 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9978 TREE_PROTECTED (type_decl)
9979 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9980 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9981 {
9982 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9983 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9984 }
9985 }
9986
9987 if (OVERLOAD_TYPE_P (t)
9988 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9989 {
9990 static const char *tags[] = {"abi_tag", "may_alias"};
9991
9992 for (unsigned ix = 0; ix != 2; ix++)
9993 {
9994 tree attributes
9995 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9996
9997 if (attributes)
9998 TYPE_ATTRIBUTES (t)
9999 = tree_cons (TREE_PURPOSE (attributes),
10000 TREE_VALUE (attributes),
10001 TYPE_ATTRIBUTES (t));
10002 }
10003 }
10004
10005 /* Let's consider the explicit specialization of a member
10006 of a class template specialization that is implicitly instantiated,
10007 e.g.:
10008 template<class T>
10009 struct S
10010 {
10011 template<class U> struct M {}; //#0
10012 };
10013
10014 template<>
10015 template<>
10016 struct S<int>::M<char> //#1
10017 {
10018 int i;
10019 };
10020 [temp.expl.spec]/4 says this is valid.
10021
10022 In this case, when we write:
10023 S<int>::M<char> m;
10024
10025 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10026 the one of #0.
10027
10028 When we encounter #1, we want to store the partial instantiation
10029 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10030
10031 For all cases other than this "explicit specialization of member of a
10032 class template", we just want to store the most general template into
10033 the CLASSTYPE_TI_TEMPLATE of M.
10034
10035 This case of "explicit specialization of member of a class template"
10036 only happens when:
10037 1/ the enclosing class is an instantiation of, and therefore not
10038 the same as, the context of the most general template, and
10039 2/ we aren't looking at the partial instantiation itself, i.e.
10040 the innermost arguments are not the same as the innermost parms of
10041 the most general template.
10042
10043 So it's only when 1/ and 2/ happens that we want to use the partial
10044 instantiation of the member template in lieu of its most general
10045 template. */
10046
10047 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10048 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10049 /* the enclosing class must be an instantiation... */
10050 && CLASS_TYPE_P (context)
10051 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10052 {
10053 TREE_VEC_LENGTH (arglist)--;
10054 ++processing_template_decl;
10055 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10056 tree partial_inst_args =
10057 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10058 arglist, complain, NULL_TREE);
10059 --processing_template_decl;
10060 TREE_VEC_LENGTH (arglist)++;
10061 if (partial_inst_args == error_mark_node)
10062 return error_mark_node;
10063 use_partial_inst_tmpl =
10064 /*...and we must not be looking at the partial instantiation
10065 itself. */
10066 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10067 partial_inst_args);
10068 }
10069
10070 if (!use_partial_inst_tmpl)
10071 /* This case is easy; there are no member templates involved. */
10072 found = gen_tmpl;
10073 else
10074 {
10075 /* This is a full instantiation of a member template. Find
10076 the partial instantiation of which this is an instance. */
10077
10078 /* Temporarily reduce by one the number of levels in the ARGLIST
10079 so as to avoid comparing the last set of arguments. */
10080 TREE_VEC_LENGTH (arglist)--;
10081 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10082 TREE_VEC_LENGTH (arglist)++;
10083 /* FOUND is either a proper class type, or an alias
10084 template specialization. In the later case, it's a
10085 TYPE_DECL, resulting from the substituting of arguments
10086 for parameters in the TYPE_DECL of the alias template
10087 done earlier. So be careful while getting the template
10088 of FOUND. */
10089 found = (TREE_CODE (found) == TEMPLATE_DECL
10090 ? found
10091 : (TREE_CODE (found) == TYPE_DECL
10092 ? DECL_TI_TEMPLATE (found)
10093 : CLASSTYPE_TI_TEMPLATE (found)));
10094
10095 if (DECL_CLASS_TEMPLATE_P (found)
10096 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10097 {
10098 /* If this partial instantiation is specialized, we want to
10099 use it for hash table lookup. */
10100 elt.tmpl = found;
10101 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10102 hash = spec_hasher::hash (&elt);
10103 }
10104 }
10105
10106 /* Build template info for the new specialization. */
10107 if (TYPE_ALIAS_P (t))
10108 {
10109 /* This is constructed during instantiation of the alias
10110 decl. But for member templates of template classes, that
10111 is not correct as we need to refer to the partially
10112 instantiated template, not the most general template.
10113 The incorrect knowledge will not have escaped this
10114 instantiation process, so we're good just updating the
10115 template_info we made then. */
10116 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10117 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10118 if (TI_TEMPLATE (ti) != found)
10119 {
10120 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10121 TI_TEMPLATE (ti) = found;
10122 }
10123 }
10124 else
10125 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10126
10127 elt.spec = t;
10128 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10129 gcc_checking_assert (*slot == NULL);
10130 entry = ggc_alloc<spec_entry> ();
10131 *entry = elt;
10132 *slot = entry;
10133
10134 /* Note this use of the partial instantiation so we can check it
10135 later in maybe_process_partial_specialization. */
10136 DECL_TEMPLATE_INSTANTIATIONS (found)
10137 = tree_cons (arglist, t,
10138 DECL_TEMPLATE_INSTANTIATIONS (found));
10139
10140 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10141 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10142 /* Now that the type has been registered on the instantiations
10143 list, we set up the enumerators. Because the enumeration
10144 constants may involve the enumeration type itself, we make
10145 sure to register the type first, and then create the
10146 constants. That way, doing tsubst_expr for the enumeration
10147 constants won't result in recursive calls here; we'll find
10148 the instantiation and exit above. */
10149 tsubst_enum (template_type, t, arglist);
10150
10151 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10152 /* If the type makes use of template parameters, the
10153 code that generates debugging information will crash. */
10154 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10155
10156 /* Possibly limit visibility based on template args. */
10157 TREE_PUBLIC (type_decl) = 1;
10158 determine_visibility (type_decl);
10159
10160 inherit_targ_abi_tags (t);
10161
10162 return t;
10163 }
10164 }
10165
10166 /* Wrapper for lookup_template_class_1. */
10167
10168 tree
10169 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10170 int entering_scope, tsubst_flags_t complain)
10171 {
10172 tree ret;
10173 timevar_push (TV_TEMPLATE_INST);
10174 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10175 entering_scope, complain);
10176 timevar_pop (TV_TEMPLATE_INST);
10177 return ret;
10178 }
10179
10180 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10181
10182 tree
10183 lookup_template_variable (tree templ, tree arglist)
10184 {
10185 if (flag_concepts && variable_concept_p (templ))
10186 return build_concept_check (templ, arglist, tf_none);
10187
10188 /* The type of the expression is NULL_TREE since the template-id could refer
10189 to an explicit or partial specialization. */
10190 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10191 }
10192
10193 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10194
10195 tree
10196 finish_template_variable (tree var, tsubst_flags_t complain)
10197 {
10198 tree templ = TREE_OPERAND (var, 0);
10199 tree arglist = TREE_OPERAND (var, 1);
10200
10201 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10202 arglist = add_outermost_template_args (tmpl_args, arglist);
10203
10204 templ = most_general_template (templ);
10205 tree parms = DECL_TEMPLATE_PARMS (templ);
10206 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10207 /*req_all*/true,
10208 /*use_default*/true);
10209 if (arglist == error_mark_node)
10210 return error_mark_node;
10211
10212 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10213 {
10214 if (complain & tf_error)
10215 {
10216 auto_diagnostic_group d;
10217 error ("use of invalid variable template %qE", var);
10218 diagnose_constraints (location_of (var), templ, arglist);
10219 }
10220 return error_mark_node;
10221 }
10222
10223 return instantiate_template (templ, arglist, complain);
10224 }
10225
10226 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10227 TARGS template args, and instantiate it if it's not dependent. */
10228
10229 tree
10230 lookup_and_finish_template_variable (tree templ, tree targs,
10231 tsubst_flags_t complain)
10232 {
10233 templ = lookup_template_variable (templ, targs);
10234 if (!any_dependent_template_arguments_p (targs))
10235 {
10236 templ = finish_template_variable (templ, complain);
10237 mark_used (templ);
10238 }
10239
10240 return convert_from_reference (templ);
10241 }
10242
10243 /* If the set of template parameters PARMS contains a template parameter
10244 at the given LEVEL and INDEX, then return this parameter. Otherwise
10245 return NULL_TREE. */
10246
10247 static tree
10248 corresponding_template_parameter (tree parms, int level, int index)
10249 {
10250 while (TMPL_PARMS_DEPTH (parms) > level)
10251 parms = TREE_CHAIN (parms);
10252
10253 if (TMPL_PARMS_DEPTH (parms) != level
10254 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10255 return NULL_TREE;
10256
10257 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10258 /* As in template_parm_to_arg. */
10259 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10260 t = TREE_TYPE (t);
10261 else
10262 t = DECL_INITIAL (t);
10263
10264 gcc_assert (TEMPLATE_PARM_P (t));
10265 return t;
10266 }
10267
10268 /* Return the template parameter from PARMS that positionally corresponds
10269 to the template parameter PARM, or else return NULL_TREE. */
10270
10271 static tree
10272 corresponding_template_parameter (tree parms, tree parm)
10273 {
10274 int level, index;
10275 template_parm_level_and_index (parm, &level, &index);
10276 return corresponding_template_parameter (parms, level, index);
10277 }
10278
10279 \f
10280 struct pair_fn_data
10281 {
10282 tree_fn_t fn;
10283 tree_fn_t any_fn;
10284 void *data;
10285 /* True when we should also visit template parameters that occur in
10286 non-deduced contexts. */
10287 bool include_nondeduced_p;
10288 hash_set<tree> *visited;
10289 };
10290
10291 /* Called from for_each_template_parm via walk_tree. */
10292
10293 static tree
10294 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10295 {
10296 tree t = *tp;
10297 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10298 tree_fn_t fn = pfd->fn;
10299 void *data = pfd->data;
10300 tree result = NULL_TREE;
10301
10302 #define WALK_SUBTREE(NODE) \
10303 do \
10304 { \
10305 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10306 pfd->include_nondeduced_p, \
10307 pfd->any_fn); \
10308 if (result) goto out; \
10309 } \
10310 while (0)
10311
10312 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10313 return t;
10314
10315 if (TYPE_P (t)
10316 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10317 WALK_SUBTREE (TYPE_CONTEXT (t));
10318
10319 switch (TREE_CODE (t))
10320 {
10321 case RECORD_TYPE:
10322 if (TYPE_PTRMEMFUNC_P (t))
10323 break;
10324 /* Fall through. */
10325
10326 case UNION_TYPE:
10327 case ENUMERAL_TYPE:
10328 if (!TYPE_TEMPLATE_INFO (t))
10329 *walk_subtrees = 0;
10330 else
10331 WALK_SUBTREE (TYPE_TI_ARGS (t));
10332 break;
10333
10334 case INTEGER_TYPE:
10335 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10336 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10337 break;
10338
10339 case METHOD_TYPE:
10340 /* Since we're not going to walk subtrees, we have to do this
10341 explicitly here. */
10342 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10343 /* Fall through. */
10344
10345 case FUNCTION_TYPE:
10346 /* Check the return type. */
10347 WALK_SUBTREE (TREE_TYPE (t));
10348
10349 /* Check the parameter types. Since default arguments are not
10350 instantiated until they are needed, the TYPE_ARG_TYPES may
10351 contain expressions that involve template parameters. But,
10352 no-one should be looking at them yet. And, once they're
10353 instantiated, they don't contain template parameters, so
10354 there's no point in looking at them then, either. */
10355 {
10356 tree parm;
10357
10358 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10359 WALK_SUBTREE (TREE_VALUE (parm));
10360
10361 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10362 want walk_tree walking into them itself. */
10363 *walk_subtrees = 0;
10364 }
10365
10366 if (flag_noexcept_type)
10367 {
10368 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10369 if (spec)
10370 WALK_SUBTREE (TREE_PURPOSE (spec));
10371 }
10372 break;
10373
10374 case TYPEOF_TYPE:
10375 case DECLTYPE_TYPE:
10376 case UNDERLYING_TYPE:
10377 if (pfd->include_nondeduced_p
10378 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10379 pfd->visited,
10380 pfd->include_nondeduced_p,
10381 pfd->any_fn))
10382 return error_mark_node;
10383 *walk_subtrees = false;
10384 break;
10385
10386 case FUNCTION_DECL:
10387 case VAR_DECL:
10388 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10389 WALK_SUBTREE (DECL_TI_ARGS (t));
10390 /* Fall through. */
10391
10392 case PARM_DECL:
10393 case CONST_DECL:
10394 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10395 WALK_SUBTREE (DECL_INITIAL (t));
10396 if (DECL_CONTEXT (t)
10397 && pfd->include_nondeduced_p)
10398 WALK_SUBTREE (DECL_CONTEXT (t));
10399 break;
10400
10401 case BOUND_TEMPLATE_TEMPLATE_PARM:
10402 /* Record template parameters such as `T' inside `TT<T>'. */
10403 WALK_SUBTREE (TYPE_TI_ARGS (t));
10404 /* Fall through. */
10405
10406 case TEMPLATE_TEMPLATE_PARM:
10407 case TEMPLATE_TYPE_PARM:
10408 case TEMPLATE_PARM_INDEX:
10409 if (fn && (*fn)(t, data))
10410 return t;
10411 else if (!fn)
10412 return t;
10413 break;
10414
10415 case TEMPLATE_DECL:
10416 /* A template template parameter is encountered. */
10417 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10418 WALK_SUBTREE (TREE_TYPE (t));
10419
10420 /* Already substituted template template parameter */
10421 *walk_subtrees = 0;
10422 break;
10423
10424 case TYPENAME_TYPE:
10425 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10426 partial instantiation. */
10427 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10428 *walk_subtrees = 0;
10429 break;
10430
10431 case CONSTRUCTOR:
10432 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10433 && pfd->include_nondeduced_p)
10434 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10435 break;
10436
10437 case INDIRECT_REF:
10438 case COMPONENT_REF:
10439 /* If there's no type, then this thing must be some expression
10440 involving template parameters. */
10441 if (!fn && !TREE_TYPE (t))
10442 return error_mark_node;
10443 break;
10444
10445 case MODOP_EXPR:
10446 case CAST_EXPR:
10447 case IMPLICIT_CONV_EXPR:
10448 case REINTERPRET_CAST_EXPR:
10449 case CONST_CAST_EXPR:
10450 case STATIC_CAST_EXPR:
10451 case DYNAMIC_CAST_EXPR:
10452 case ARROW_EXPR:
10453 case DOTSTAR_EXPR:
10454 case TYPEID_EXPR:
10455 case PSEUDO_DTOR_EXPR:
10456 if (!fn)
10457 return error_mark_node;
10458 break;
10459
10460 case SCOPE_REF:
10461 if (pfd->include_nondeduced_p)
10462 WALK_SUBTREE (TREE_OPERAND (t, 0));
10463 break;
10464
10465 case REQUIRES_EXPR:
10466 {
10467 if (!fn)
10468 return error_mark_node;
10469
10470 /* Recursively walk the type of each constraint variable. */
10471 tree p = TREE_OPERAND (t, 0);
10472 while (p)
10473 {
10474 WALK_SUBTREE (TREE_TYPE (p));
10475 p = TREE_CHAIN (p);
10476 }
10477 }
10478 break;
10479
10480 default:
10481 break;
10482 }
10483
10484 #undef WALK_SUBTREE
10485
10486 /* We didn't find any template parameters we liked. */
10487 out:
10488 return result;
10489 }
10490
10491 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10492 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10493 call FN with the parameter and the DATA.
10494 If FN returns nonzero, the iteration is terminated, and
10495 for_each_template_parm returns 1. Otherwise, the iteration
10496 continues. If FN never returns a nonzero value, the value
10497 returned by for_each_template_parm is 0. If FN is NULL, it is
10498 considered to be the function which always returns 1.
10499
10500 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10501 parameters that occur in non-deduced contexts. When false, only
10502 visits those template parameters that can be deduced. */
10503
10504 static tree
10505 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10506 hash_set<tree> *visited,
10507 bool include_nondeduced_p,
10508 tree_fn_t any_fn)
10509 {
10510 struct pair_fn_data pfd;
10511 tree result;
10512
10513 /* Set up. */
10514 pfd.fn = fn;
10515 pfd.any_fn = any_fn;
10516 pfd.data = data;
10517 pfd.include_nondeduced_p = include_nondeduced_p;
10518
10519 /* Walk the tree. (Conceptually, we would like to walk without
10520 duplicates, but for_each_template_parm_r recursively calls
10521 for_each_template_parm, so we would need to reorganize a fair
10522 bit to use walk_tree_without_duplicates, so we keep our own
10523 visited list.) */
10524 if (visited)
10525 pfd.visited = visited;
10526 else
10527 pfd.visited = new hash_set<tree>;
10528 result = cp_walk_tree (&t,
10529 for_each_template_parm_r,
10530 &pfd,
10531 pfd.visited);
10532
10533 /* Clean up. */
10534 if (!visited)
10535 {
10536 delete pfd.visited;
10537 pfd.visited = 0;
10538 }
10539
10540 return result;
10541 }
10542
10543 struct find_template_parameter_info
10544 {
10545 explicit find_template_parameter_info (tree ctx_parms)
10546 : parm_list (NULL_TREE),
10547 ctx_parms (ctx_parms),
10548 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10549 {}
10550
10551 hash_set<tree> visited;
10552 hash_set<tree> parms;
10553 tree parm_list;
10554 tree ctx_parms;
10555 int max_depth;
10556 };
10557
10558 /* Appends the declaration of T to the list in DATA. */
10559
10560 static int
10561 keep_template_parm (tree t, void* data)
10562 {
10563 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10564
10565 /* Template parameters declared within the expression are not part of
10566 the parameter mapping. For example, in this concept:
10567
10568 template<typename T>
10569 concept C = requires { <expr> } -> same_as<int>;
10570
10571 the return specifier same_as<int> declares a new decltype parameter
10572 that must not be part of the parameter mapping. The same is true
10573 for generic lambda parameters, lambda template parameters, etc. */
10574 int level;
10575 int index;
10576 template_parm_level_and_index (t, &level, &index);
10577 if (level > ftpi->max_depth)
10578 return 0;
10579
10580 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10581 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10582 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10583 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10584
10585 /* This template parameter might be an argument to a cached dependent
10586 specalization that was formed earlier inside some other template, in
10587 which case the parameter is not among the ones that are in-scope.
10588 Look in CTX_PARMS to find the corresponding in-scope template
10589 parameter, and use it instead. */
10590 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10591 t = in_scope;
10592
10593 /* Arguments like const T yield parameters like const T. This means that
10594 a template-id like X<T, const T> would yield two distinct parameters:
10595 T and const T. Adjust types to their unqualified versions. */
10596 if (TYPE_P (t))
10597 t = TYPE_MAIN_VARIANT (t);
10598 if (!ftpi->parms.add (t))
10599 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10600
10601 return 0;
10602 }
10603
10604 /* Ensure that we recursively examine certain terms that are not normally
10605 visited in for_each_template_parm_r. */
10606
10607 static int
10608 any_template_parm_r (tree t, void *data)
10609 {
10610 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10611
10612 #define WALK_SUBTREE(NODE) \
10613 do \
10614 { \
10615 for_each_template_parm (NODE, keep_template_parm, data, \
10616 &ftpi->visited, true, \
10617 any_template_parm_r); \
10618 } \
10619 while (0)
10620
10621 /* A mention of a member alias/typedef is a use of all of its template
10622 arguments, including those from the enclosing class, so we don't use
10623 alias_template_specialization_p here. */
10624 if (TYPE_P (t) && typedef_variant_p (t))
10625 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10626 WALK_SUBTREE (TI_ARGS (tinfo));
10627
10628 switch (TREE_CODE (t))
10629 {
10630 case TEMPLATE_TYPE_PARM:
10631 /* Type constraints of a placeholder type may contain parameters. */
10632 if (is_auto (t))
10633 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10634 WALK_SUBTREE (constr);
10635 break;
10636
10637 case TEMPLATE_ID_EXPR:
10638 /* Search through references to variable templates. */
10639 WALK_SUBTREE (TREE_OPERAND (t, 0));
10640 WALK_SUBTREE (TREE_OPERAND (t, 1));
10641 break;
10642
10643 case TEMPLATE_PARM_INDEX:
10644 case PARM_DECL:
10645 /* A parameter or constraint variable may also depend on a template
10646 parameter without explicitly naming it. */
10647 WALK_SUBTREE (TREE_TYPE (t));
10648 break;
10649
10650 case TEMPLATE_DECL:
10651 {
10652 /* If T is a member template that shares template parameters with
10653 ctx_parms, we need to mark all those parameters for mapping. */
10654 tree dparms = DECL_TEMPLATE_PARMS (t);
10655 tree cparms = ftpi->ctx_parms;
10656 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10657 dparms = TREE_CHAIN (dparms);
10658 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10659 cparms = TREE_CHAIN (cparms);
10660 while (dparms
10661 && (TREE_TYPE (TREE_VALUE (dparms))
10662 != TREE_TYPE (TREE_VALUE (cparms))))
10663 dparms = TREE_CHAIN (dparms),
10664 cparms = TREE_CHAIN (cparms);
10665 if (dparms)
10666 {
10667 int ddepth = TMPL_PARMS_DEPTH (dparms);
10668 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10669 for (int i = 0; i < ddepth; ++i)
10670 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10671 }
10672 }
10673 break;
10674
10675 case LAMBDA_EXPR:
10676 {
10677 /* Look in the parms and body. */
10678 tree fn = lambda_function (t);
10679 WALK_SUBTREE (TREE_TYPE (fn));
10680 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10681 }
10682 break;
10683
10684 case IDENTIFIER_NODE:
10685 if (IDENTIFIER_CONV_OP_P (t))
10686 /* The conversion-type-id of a conversion operator may be dependent. */
10687 WALK_SUBTREE (TREE_TYPE (t));
10688 break;
10689
10690 default:
10691 break;
10692 }
10693
10694 /* Keep walking. */
10695 return 0;
10696 }
10697
10698 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10699 are the template parameters in scope. */
10700
10701 tree
10702 find_template_parameters (tree t, tree ctx_parms)
10703 {
10704 if (!ctx_parms)
10705 return NULL_TREE;
10706
10707 find_template_parameter_info ftpi (ctx_parms);
10708 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10709 /*include_nondeduced*/true, any_template_parm_r);
10710 return ftpi.parm_list;
10711 }
10712
10713 /* Returns true if T depends on any template parameter. */
10714
10715 int
10716 uses_template_parms (tree t)
10717 {
10718 if (t == NULL_TREE)
10719 return false;
10720
10721 bool dependent_p;
10722 int saved_processing_template_decl;
10723
10724 saved_processing_template_decl = processing_template_decl;
10725 if (!saved_processing_template_decl)
10726 processing_template_decl = 1;
10727 if (TYPE_P (t))
10728 dependent_p = dependent_type_p (t);
10729 else if (TREE_CODE (t) == TREE_VEC)
10730 dependent_p = any_dependent_template_arguments_p (t);
10731 else if (TREE_CODE (t) == TREE_LIST)
10732 dependent_p = (uses_template_parms (TREE_VALUE (t))
10733 || uses_template_parms (TREE_CHAIN (t)));
10734 else if (TREE_CODE (t) == TYPE_DECL)
10735 dependent_p = dependent_type_p (TREE_TYPE (t));
10736 else if (t == error_mark_node)
10737 dependent_p = false;
10738 else
10739 dependent_p = value_dependent_expression_p (t);
10740
10741 processing_template_decl = saved_processing_template_decl;
10742
10743 return dependent_p;
10744 }
10745
10746 /* Returns true iff current_function_decl is an incompletely instantiated
10747 template. Useful instead of processing_template_decl because the latter
10748 is set to 0 during instantiate_non_dependent_expr. */
10749
10750 bool
10751 in_template_function (void)
10752 {
10753 tree fn = current_function_decl;
10754 bool ret;
10755 ++processing_template_decl;
10756 ret = (fn && DECL_LANG_SPECIFIC (fn)
10757 && DECL_TEMPLATE_INFO (fn)
10758 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10759 --processing_template_decl;
10760 return ret;
10761 }
10762
10763 /* Returns true if T depends on any template parameter with level LEVEL. */
10764
10765 bool
10766 uses_template_parms_level (tree t, int level)
10767 {
10768 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10769 /*include_nondeduced_p=*/true);
10770 }
10771
10772 /* Returns true if the signature of DECL depends on any template parameter from
10773 its enclosing class. */
10774
10775 bool
10776 uses_outer_template_parms (tree decl)
10777 {
10778 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10779 if (depth == 0)
10780 return false;
10781 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10782 &depth, NULL, /*include_nondeduced_p=*/true))
10783 return true;
10784 if (PRIMARY_TEMPLATE_P (decl)
10785 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10786 (DECL_TEMPLATE_PARMS (decl)),
10787 template_parm_outer_level,
10788 &depth, NULL, /*include_nondeduced_p=*/true))
10789 return true;
10790 tree ci = get_constraints (decl);
10791 if (ci)
10792 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10793 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10794 &depth, NULL, /*nondeduced*/true))
10795 return true;
10796 return false;
10797 }
10798
10799 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10800 ill-formed translation unit, i.e. a variable or function that isn't
10801 usable in a constant expression. */
10802
10803 static inline bool
10804 neglectable_inst_p (tree d)
10805 {
10806 return (d && DECL_P (d)
10807 && !undeduced_auto_decl (d)
10808 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10809 : decl_maybe_constant_var_p (d)));
10810 }
10811
10812 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10813 neglectable and instantiated from within an erroneous instantiation. */
10814
10815 static bool
10816 limit_bad_template_recursion (tree decl)
10817 {
10818 struct tinst_level *lev = current_tinst_level;
10819 int errs = errorcount + sorrycount;
10820 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10821 return false;
10822
10823 for (; lev; lev = lev->next)
10824 if (neglectable_inst_p (lev->maybe_get_node ()))
10825 break;
10826
10827 return (lev && errs > lev->errors);
10828 }
10829
10830 static int tinst_depth;
10831 extern int max_tinst_depth;
10832 int depth_reached;
10833
10834 static GTY(()) struct tinst_level *last_error_tinst_level;
10835
10836 /* We're starting to instantiate D; record the template instantiation context
10837 at LOC for diagnostics and to restore it later. */
10838
10839 bool
10840 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10841 {
10842 struct tinst_level *new_level;
10843
10844 if (tinst_depth >= max_tinst_depth)
10845 {
10846 /* Tell error.c not to try to instantiate any templates. */
10847 at_eof = 2;
10848 fatal_error (input_location,
10849 "template instantiation depth exceeds maximum of %d"
10850 " (use %<-ftemplate-depth=%> to increase the maximum)",
10851 max_tinst_depth);
10852 return false;
10853 }
10854
10855 /* If the current instantiation caused problems, don't let it instantiate
10856 anything else. Do allow deduction substitution and decls usable in
10857 constant expressions. */
10858 if (!targs && limit_bad_template_recursion (tldcl))
10859 {
10860 /* Avoid no_linkage_errors and unused function warnings for this
10861 decl. */
10862 TREE_NO_WARNING (tldcl) = 1;
10863 return false;
10864 }
10865
10866 /* When not -quiet, dump template instantiations other than functions, since
10867 announce_function will take care of those. */
10868 if (!quiet_flag && !targs
10869 && TREE_CODE (tldcl) != TREE_LIST
10870 && TREE_CODE (tldcl) != FUNCTION_DECL)
10871 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10872
10873 new_level = tinst_level_freelist ().alloc ();
10874 new_level->tldcl = tldcl;
10875 new_level->targs = targs;
10876 new_level->locus = loc;
10877 new_level->errors = errorcount + sorrycount;
10878 new_level->next = NULL;
10879 new_level->refcount = 0;
10880 set_refcount_ptr (new_level->next, current_tinst_level);
10881 set_refcount_ptr (current_tinst_level, new_level);
10882
10883 ++tinst_depth;
10884 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10885 depth_reached = tinst_depth;
10886
10887 return true;
10888 }
10889
10890 /* We're starting substitution of TMPL<ARGS>; record the template
10891 substitution context for diagnostics and to restore it later. */
10892
10893 bool
10894 push_tinst_level (tree tmpl, tree args)
10895 {
10896 return push_tinst_level_loc (tmpl, args, input_location);
10897 }
10898
10899 /* We're starting to instantiate D; record INPUT_LOCATION and the
10900 template instantiation context for diagnostics and to restore it
10901 later. */
10902
10903 bool
10904 push_tinst_level (tree d)
10905 {
10906 return push_tinst_level_loc (d, input_location);
10907 }
10908
10909 /* Likewise, but record LOC as the program location. */
10910
10911 bool
10912 push_tinst_level_loc (tree d, location_t loc)
10913 {
10914 gcc_assert (TREE_CODE (d) != TREE_LIST);
10915 return push_tinst_level_loc (d, NULL, loc);
10916 }
10917
10918 /* We're done instantiating this template; return to the instantiation
10919 context. */
10920
10921 void
10922 pop_tinst_level (void)
10923 {
10924 /* Restore the filename and line number stashed away when we started
10925 this instantiation. */
10926 input_location = current_tinst_level->locus;
10927 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10928 --tinst_depth;
10929 }
10930
10931 /* We're instantiating a deferred template; restore the template
10932 instantiation context in which the instantiation was requested, which
10933 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10934
10935 static tree
10936 reopen_tinst_level (struct tinst_level *level)
10937 {
10938 struct tinst_level *t;
10939
10940 tinst_depth = 0;
10941 for (t = level; t; t = t->next)
10942 ++tinst_depth;
10943
10944 set_refcount_ptr (current_tinst_level, level);
10945 pop_tinst_level ();
10946 if (current_tinst_level)
10947 current_tinst_level->errors = errorcount+sorrycount;
10948 return level->maybe_get_node ();
10949 }
10950
10951 /* Returns the TINST_LEVEL which gives the original instantiation
10952 context. */
10953
10954 struct tinst_level *
10955 outermost_tinst_level (void)
10956 {
10957 struct tinst_level *level = current_tinst_level;
10958 if (level)
10959 while (level->next)
10960 level = level->next;
10961 return level;
10962 }
10963
10964 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10965 vector of template arguments, as for tsubst.
10966
10967 Returns an appropriate tsubst'd friend declaration. */
10968
10969 static tree
10970 tsubst_friend_function (tree decl, tree args)
10971 {
10972 tree new_friend;
10973
10974 if (TREE_CODE (decl) == FUNCTION_DECL
10975 && DECL_TEMPLATE_INSTANTIATION (decl)
10976 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10977 /* This was a friend declared with an explicit template
10978 argument list, e.g.:
10979
10980 friend void f<>(T);
10981
10982 to indicate that f was a template instantiation, not a new
10983 function declaration. Now, we have to figure out what
10984 instantiation of what template. */
10985 {
10986 tree template_id, arglist, fns;
10987 tree new_args;
10988 tree tmpl;
10989 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10990
10991 /* Friend functions are looked up in the containing namespace scope.
10992 We must enter that scope, to avoid finding member functions of the
10993 current class with same name. */
10994 push_nested_namespace (ns);
10995 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10996 tf_warning_or_error, NULL_TREE,
10997 /*integral_constant_expression_p=*/false);
10998 pop_nested_namespace (ns);
10999 arglist = tsubst (DECL_TI_ARGS (decl), args,
11000 tf_warning_or_error, NULL_TREE);
11001 template_id = lookup_template_function (fns, arglist);
11002
11003 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11004 tmpl = determine_specialization (template_id, new_friend,
11005 &new_args,
11006 /*need_member_template=*/0,
11007 TREE_VEC_LENGTH (args),
11008 tsk_none);
11009 return instantiate_template (tmpl, new_args, tf_error);
11010 }
11011
11012 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11013 if (new_friend == error_mark_node)
11014 return error_mark_node;
11015
11016 /* The NEW_FRIEND will look like an instantiation, to the
11017 compiler, but is not an instantiation from the point of view of
11018 the language. For example, we might have had:
11019
11020 template <class T> struct S {
11021 template <class U> friend void f(T, U);
11022 };
11023
11024 Then, in S<int>, template <class U> void f(int, U) is not an
11025 instantiation of anything. */
11026
11027 DECL_USE_TEMPLATE (new_friend) = 0;
11028 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11029 {
11030 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11031 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11032 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11033
11034 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11035 match in decls_match. */
11036 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11037 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11038 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11039 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11040 }
11041
11042 /* The mangled name for the NEW_FRIEND is incorrect. The function
11043 is not a template instantiation and should not be mangled like
11044 one. Therefore, we forget the mangling here; we'll recompute it
11045 later if we need it. */
11046 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11047 {
11048 SET_DECL_RTL (new_friend, NULL);
11049 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11050 }
11051
11052 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11053 {
11054 tree old_decl;
11055 tree ns;
11056
11057 /* We must save some information from NEW_FRIEND before calling
11058 duplicate decls since that function will free NEW_FRIEND if
11059 possible. */
11060 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11061 tree new_friend_result_template_info = NULL_TREE;
11062 bool new_friend_is_defn =
11063 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11064 (template_for_substitution (new_friend)))
11065 != NULL_TREE);
11066 tree not_tmpl = new_friend;
11067
11068 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11069 {
11070 /* This declaration is a `primary' template. */
11071 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11072
11073 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11074 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11075 }
11076
11077 /* Inside pushdecl_namespace_level, we will push into the
11078 current namespace. However, the friend function should go
11079 into the namespace of the template. */
11080 ns = decl_namespace_context (new_friend);
11081 push_nested_namespace (ns);
11082 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11083 pop_nested_namespace (ns);
11084
11085 if (old_decl == error_mark_node)
11086 return error_mark_node;
11087
11088 if (old_decl != new_friend)
11089 {
11090 /* This new friend declaration matched an existing
11091 declaration. For example, given:
11092
11093 template <class T> void f(T);
11094 template <class U> class C {
11095 template <class T> friend void f(T) {}
11096 };
11097
11098 the friend declaration actually provides the definition
11099 of `f', once C has been instantiated for some type. So,
11100 old_decl will be the out-of-class template declaration,
11101 while new_friend is the in-class definition.
11102
11103 But, if `f' was called before this point, the
11104 instantiation of `f' will have DECL_TI_ARGS corresponding
11105 to `T' but not to `U', references to which might appear
11106 in the definition of `f'. Previously, the most general
11107 template for an instantiation of `f' was the out-of-class
11108 version; now it is the in-class version. Therefore, we
11109 run through all specialization of `f', adding to their
11110 DECL_TI_ARGS appropriately. In particular, they need a
11111 new set of outer arguments, corresponding to the
11112 arguments for this class instantiation.
11113
11114 The same situation can arise with something like this:
11115
11116 friend void f(int);
11117 template <class T> class C {
11118 friend void f(T) {}
11119 };
11120
11121 when `C<int>' is instantiated. Now, `f(int)' is defined
11122 in the class. */
11123
11124 if (!new_friend_is_defn)
11125 /* On the other hand, if the in-class declaration does
11126 *not* provide a definition, then we don't want to alter
11127 existing definitions. We can just leave everything
11128 alone. */
11129 ;
11130 else
11131 {
11132 tree new_template = TI_TEMPLATE (new_friend_template_info);
11133 tree new_args = TI_ARGS (new_friend_template_info);
11134
11135 /* Overwrite whatever template info was there before, if
11136 any, with the new template information pertaining to
11137 the declaration. */
11138 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11139
11140 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11141 {
11142 /* We should have called reregister_specialization in
11143 duplicate_decls. */
11144 gcc_assert (retrieve_specialization (new_template,
11145 new_args, 0)
11146 == old_decl);
11147
11148 /* Instantiate it if the global has already been used. */
11149 if (DECL_ODR_USED (old_decl))
11150 instantiate_decl (old_decl, /*defer_ok=*/true,
11151 /*expl_inst_class_mem_p=*/false);
11152 }
11153 else
11154 {
11155 tree t;
11156
11157 /* Indicate that the old function template is a partial
11158 instantiation. */
11159 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11160 = new_friend_result_template_info;
11161
11162 gcc_assert (new_template
11163 == most_general_template (new_template));
11164 gcc_assert (new_template != old_decl);
11165
11166 /* Reassign any specializations already in the hash table
11167 to the new more general template, and add the
11168 additional template args. */
11169 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11170 t != NULL_TREE;
11171 t = TREE_CHAIN (t))
11172 {
11173 tree spec = TREE_VALUE (t);
11174 spec_entry elt;
11175
11176 elt.tmpl = old_decl;
11177 elt.args = DECL_TI_ARGS (spec);
11178 elt.spec = NULL_TREE;
11179
11180 decl_specializations->remove_elt (&elt);
11181
11182 DECL_TI_ARGS (spec)
11183 = add_outermost_template_args (new_args,
11184 DECL_TI_ARGS (spec));
11185
11186 register_specialization
11187 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11188
11189 }
11190 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11191 }
11192 }
11193
11194 /* The information from NEW_FRIEND has been merged into OLD_DECL
11195 by duplicate_decls. */
11196 new_friend = old_decl;
11197 }
11198 }
11199 else
11200 {
11201 tree context = DECL_CONTEXT (new_friend);
11202 bool dependent_p;
11203
11204 /* In the code
11205 template <class T> class C {
11206 template <class U> friend void C1<U>::f (); // case 1
11207 friend void C2<T>::f (); // case 2
11208 };
11209 we only need to make sure CONTEXT is a complete type for
11210 case 2. To distinguish between the two cases, we note that
11211 CONTEXT of case 1 remains dependent type after tsubst while
11212 this isn't true for case 2. */
11213 ++processing_template_decl;
11214 dependent_p = dependent_type_p (context);
11215 --processing_template_decl;
11216
11217 if (!dependent_p
11218 && !complete_type_or_else (context, NULL_TREE))
11219 return error_mark_node;
11220
11221 if (COMPLETE_TYPE_P (context))
11222 {
11223 tree fn = new_friend;
11224 /* do_friend adds the TEMPLATE_DECL for any member friend
11225 template even if it isn't a member template, i.e.
11226 template <class T> friend A<T>::f();
11227 Look through it in that case. */
11228 if (TREE_CODE (fn) == TEMPLATE_DECL
11229 && !PRIMARY_TEMPLATE_P (fn))
11230 fn = DECL_TEMPLATE_RESULT (fn);
11231 /* Check to see that the declaration is really present, and,
11232 possibly obtain an improved declaration. */
11233 fn = check_classfn (context, fn, NULL_TREE);
11234
11235 if (fn)
11236 new_friend = fn;
11237 }
11238 }
11239
11240 return new_friend;
11241 }
11242
11243 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11244 template arguments, as for tsubst.
11245
11246 Returns an appropriate tsubst'd friend type or error_mark_node on
11247 failure. */
11248
11249 static tree
11250 tsubst_friend_class (tree friend_tmpl, tree args)
11251 {
11252 tree tmpl;
11253
11254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11255 {
11256 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11257 return TREE_TYPE (tmpl);
11258 }
11259
11260 tree context = CP_DECL_CONTEXT (friend_tmpl);
11261 if (TREE_CODE (context) == NAMESPACE_DECL)
11262 push_nested_namespace (context);
11263 else
11264 {
11265 context = tsubst (context, args, tf_error, NULL_TREE);
11266 push_nested_class (context);
11267 }
11268
11269 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11270 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11271
11272 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11273 {
11274 /* The friend template has already been declared. Just
11275 check to see that the declarations match, and install any new
11276 default parameters. We must tsubst the default parameters,
11277 of course. We only need the innermost template parameters
11278 because that is all that redeclare_class_template will look
11279 at. */
11280 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11281 > TMPL_ARGS_DEPTH (args))
11282 {
11283 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11284 args, tf_warning_or_error);
11285 location_t saved_input_location = input_location;
11286 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11287 tree cons = get_constraints (tmpl);
11288 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11289 input_location = saved_input_location;
11290 }
11291 }
11292 else
11293 {
11294 /* The friend template has not already been declared. In this
11295 case, the instantiation of the template class will cause the
11296 injection of this template into the namespace scope. */
11297 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11298
11299 if (tmpl != error_mark_node)
11300 {
11301 /* The new TMPL is not an instantiation of anything, so we
11302 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11303 for the new type because that is supposed to be the
11304 corresponding template decl, i.e., TMPL. */
11305 DECL_USE_TEMPLATE (tmpl) = 0;
11306 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11307 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11308 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11309 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11310
11311 /* Substitute into and set the constraints on the new declaration. */
11312 if (tree ci = get_constraints (friend_tmpl))
11313 {
11314 ++processing_template_decl;
11315 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11316 DECL_FRIEND_CONTEXT (friend_tmpl));
11317 --processing_template_decl;
11318 set_constraints (tmpl, ci);
11319 }
11320
11321 /* Inject this template into the enclosing namspace scope. */
11322 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11323 }
11324 }
11325
11326 if (TREE_CODE (context) == NAMESPACE_DECL)
11327 pop_nested_namespace (context);
11328 else
11329 pop_nested_class ();
11330
11331 return TREE_TYPE (tmpl);
11332 }
11333
11334 /* Returns zero if TYPE cannot be completed later due to circularity.
11335 Otherwise returns one. */
11336
11337 static int
11338 can_complete_type_without_circularity (tree type)
11339 {
11340 if (type == NULL_TREE || type == error_mark_node)
11341 return 0;
11342 else if (COMPLETE_TYPE_P (type))
11343 return 1;
11344 else if (TREE_CODE (type) == ARRAY_TYPE)
11345 return can_complete_type_without_circularity (TREE_TYPE (type));
11346 else if (CLASS_TYPE_P (type)
11347 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11348 return 0;
11349 else
11350 return 1;
11351 }
11352
11353 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11354 tsubst_flags_t, tree);
11355
11356 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11357 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11358
11359 static tree
11360 tsubst_attribute (tree t, tree *decl_p, tree args,
11361 tsubst_flags_t complain, tree in_decl)
11362 {
11363 gcc_assert (ATTR_IS_DEPENDENT (t));
11364
11365 tree val = TREE_VALUE (t);
11366 if (val == NULL_TREE)
11367 /* Nothing to do. */;
11368 else if ((flag_openmp || flag_openmp_simd)
11369 && is_attribute_p ("omp declare simd",
11370 get_attribute_name (t)))
11371 {
11372 tree clauses = TREE_VALUE (val);
11373 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11374 complain, in_decl);
11375 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11376 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11377 tree parms = DECL_ARGUMENTS (*decl_p);
11378 clauses
11379 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11380 if (clauses)
11381 val = build_tree_list (NULL_TREE, clauses);
11382 else
11383 val = NULL_TREE;
11384 }
11385 else if (flag_openmp
11386 && is_attribute_p ("omp declare variant base",
11387 get_attribute_name (t)))
11388 {
11389 ++cp_unevaluated_operand;
11390 tree varid
11391 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11392 in_decl, /*integral_constant_expression_p=*/false);
11393 --cp_unevaluated_operand;
11394 tree chain = TREE_CHAIN (val);
11395 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11396 tree ctx = copy_list (TREE_VALUE (val));
11397 tree simd = get_identifier ("simd");
11398 tree score = get_identifier (" score");
11399 tree condition = get_identifier ("condition");
11400 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11401 {
11402 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11403 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11404 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11405 {
11406 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11407 {
11408 tree clauses = TREE_VALUE (t2);
11409 clauses = tsubst_omp_clauses (clauses,
11410 C_ORT_OMP_DECLARE_SIMD, args,
11411 complain, in_decl);
11412 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11413 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11414 TREE_VALUE (t2) = clauses;
11415 }
11416 else
11417 {
11418 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11419 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11420 if (TREE_VALUE (t3))
11421 {
11422 bool allow_string
11423 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11424 && TREE_PURPOSE (t3) != score);
11425 tree v = TREE_VALUE (t3);
11426 if (TREE_CODE (v) == STRING_CST && allow_string)
11427 continue;
11428 v = tsubst_expr (v, args, complain, in_decl, true);
11429 v = fold_non_dependent_expr (v);
11430 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11431 || (TREE_PURPOSE (t3) == score
11432 ? TREE_CODE (v) != INTEGER_CST
11433 : !tree_fits_shwi_p (v)))
11434 {
11435 location_t loc
11436 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11437 match_loc);
11438 if (TREE_PURPOSE (t3) == score)
11439 error_at (loc, "score argument must be "
11440 "constant integer expression");
11441 else if (allow_string)
11442 error_at (loc, "property must be constant "
11443 "integer expression or string "
11444 "literal");
11445 else
11446 error_at (loc, "property must be constant "
11447 "integer expression");
11448 return NULL_TREE;
11449 }
11450 else if (TREE_PURPOSE (t3) == score
11451 && tree_int_cst_sgn (v) < 0)
11452 {
11453 location_t loc
11454 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11455 match_loc);
11456 error_at (loc, "score argument must be "
11457 "non-negative");
11458 return NULL_TREE;
11459 }
11460 TREE_VALUE (t3) = v;
11461 }
11462 }
11463 }
11464 }
11465 val = tree_cons (varid, ctx, chain);
11466 }
11467 /* If the first attribute argument is an identifier, don't
11468 pass it through tsubst. Attributes like mode, format,
11469 cleanup and several target specific attributes expect it
11470 unmodified. */
11471 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11472 {
11473 tree chain
11474 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11475 /*integral_constant_expression_p=*/false);
11476 if (chain != TREE_CHAIN (val))
11477 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11478 }
11479 else if (PACK_EXPANSION_P (val))
11480 {
11481 /* An attribute pack expansion. */
11482 tree purp = TREE_PURPOSE (t);
11483 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11484 if (pack == error_mark_node)
11485 return error_mark_node;
11486 int len = TREE_VEC_LENGTH (pack);
11487 tree list = NULL_TREE;
11488 tree *q = &list;
11489 for (int i = 0; i < len; ++i)
11490 {
11491 tree elt = TREE_VEC_ELT (pack, i);
11492 *q = build_tree_list (purp, elt);
11493 q = &TREE_CHAIN (*q);
11494 }
11495 return list;
11496 }
11497 else
11498 val = tsubst_expr (val, args, complain, in_decl,
11499 /*integral_constant_expression_p=*/false);
11500
11501 if (val != TREE_VALUE (t))
11502 return build_tree_list (TREE_PURPOSE (t), val);
11503 return t;
11504 }
11505
11506 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11507 unchanged or a new TREE_LIST chain. */
11508
11509 static tree
11510 tsubst_attributes (tree attributes, tree args,
11511 tsubst_flags_t complain, tree in_decl)
11512 {
11513 tree last_dep = NULL_TREE;
11514
11515 for (tree t = attributes; t; t = TREE_CHAIN (t))
11516 if (ATTR_IS_DEPENDENT (t))
11517 {
11518 last_dep = t;
11519 attributes = copy_list (attributes);
11520 break;
11521 }
11522
11523 if (last_dep)
11524 for (tree *p = &attributes; *p; )
11525 {
11526 tree t = *p;
11527 if (ATTR_IS_DEPENDENT (t))
11528 {
11529 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11530 if (subst != t)
11531 {
11532 *p = subst;
11533 while (*p)
11534 p = &TREE_CHAIN (*p);
11535 *p = TREE_CHAIN (t);
11536 continue;
11537 }
11538 }
11539 p = &TREE_CHAIN (*p);
11540 }
11541
11542 return attributes;
11543 }
11544
11545 /* Apply any attributes which had to be deferred until instantiation
11546 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11547 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11548
11549 static void
11550 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11551 tree args, tsubst_flags_t complain, tree in_decl)
11552 {
11553 tree last_dep = NULL_TREE;
11554 tree t;
11555 tree *p;
11556
11557 if (attributes == NULL_TREE)
11558 return;
11559
11560 if (DECL_P (*decl_p))
11561 {
11562 if (TREE_TYPE (*decl_p) == error_mark_node)
11563 return;
11564 p = &DECL_ATTRIBUTES (*decl_p);
11565 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11566 to our attributes parameter. */
11567 gcc_assert (*p == attributes);
11568 }
11569 else
11570 {
11571 p = &TYPE_ATTRIBUTES (*decl_p);
11572 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11573 lookup_template_class_1, and should be preserved. */
11574 gcc_assert (*p != attributes);
11575 while (*p)
11576 p = &TREE_CHAIN (*p);
11577 }
11578
11579 for (t = attributes; t; t = TREE_CHAIN (t))
11580 if (ATTR_IS_DEPENDENT (t))
11581 {
11582 last_dep = t;
11583 attributes = copy_list (attributes);
11584 break;
11585 }
11586
11587 *p = attributes;
11588 if (last_dep)
11589 {
11590 tree late_attrs = NULL_TREE;
11591 tree *q = &late_attrs;
11592
11593 for (; *p; )
11594 {
11595 t = *p;
11596 if (ATTR_IS_DEPENDENT (t))
11597 {
11598 *p = TREE_CHAIN (t);
11599 TREE_CHAIN (t) = NULL_TREE;
11600 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11601 while (*q)
11602 q = &TREE_CHAIN (*q);
11603 }
11604 else
11605 p = &TREE_CHAIN (t);
11606 }
11607
11608 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11609 }
11610 }
11611
11612 /* The template TMPL is being instantiated with the template arguments TARGS.
11613 Perform the access checks that we deferred when parsing the template. */
11614
11615 static void
11616 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11617 {
11618 unsigned i;
11619 deferred_access_check *chk;
11620
11621 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11622 return;
11623
11624 if (vec<deferred_access_check, va_gc> *access_checks
11625 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11626 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11627 {
11628 tree decl = chk->decl;
11629 tree diag_decl = chk->diag_decl;
11630 tree type_scope = TREE_TYPE (chk->binfo);
11631
11632 if (uses_template_parms (type_scope))
11633 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11634
11635 /* Make access check error messages point to the location
11636 of the use of the typedef. */
11637 iloc_sentinel ils (chk->loc);
11638 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11639 decl, diag_decl, tf_warning_or_error);
11640 }
11641 }
11642
11643 static tree
11644 instantiate_class_template_1 (tree type)
11645 {
11646 tree templ, args, pattern, t, member;
11647 tree typedecl;
11648 tree pbinfo;
11649 tree base_list;
11650 unsigned int saved_maximum_field_alignment;
11651 tree fn_context;
11652
11653 if (type == error_mark_node)
11654 return error_mark_node;
11655
11656 if (COMPLETE_OR_OPEN_TYPE_P (type)
11657 || uses_template_parms (type))
11658 return type;
11659
11660 /* Figure out which template is being instantiated. */
11661 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11662 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11663
11664 /* Mark the type as in the process of being defined. */
11665 TYPE_BEING_DEFINED (type) = 1;
11666
11667 /* We may be in the middle of deferred access check. Disable
11668 it now. */
11669 deferring_access_check_sentinel acs (dk_no_deferred);
11670
11671 /* Determine what specialization of the original template to
11672 instantiate. */
11673 t = most_specialized_partial_spec (type, tf_warning_or_error);
11674 if (t == error_mark_node)
11675 return error_mark_node;
11676 else if (t)
11677 {
11678 /* This TYPE is actually an instantiation of a partial
11679 specialization. We replace the innermost set of ARGS with
11680 the arguments appropriate for substitution. For example,
11681 given:
11682
11683 template <class T> struct S {};
11684 template <class T> struct S<T*> {};
11685
11686 and supposing that we are instantiating S<int*>, ARGS will
11687 presently be {int*} -- but we need {int}. */
11688 pattern = TREE_TYPE (t);
11689 args = TREE_PURPOSE (t);
11690 }
11691 else
11692 {
11693 pattern = TREE_TYPE (templ);
11694 args = CLASSTYPE_TI_ARGS (type);
11695 }
11696
11697 /* If the template we're instantiating is incomplete, then clearly
11698 there's nothing we can do. */
11699 if (!COMPLETE_TYPE_P (pattern))
11700 {
11701 /* We can try again later. */
11702 TYPE_BEING_DEFINED (type) = 0;
11703 return type;
11704 }
11705
11706 /* If we've recursively instantiated too many templates, stop. */
11707 if (! push_tinst_level (type))
11708 return type;
11709
11710 int saved_unevaluated_operand = cp_unevaluated_operand;
11711 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11712
11713 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11714 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11715 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11716 fn_context = error_mark_node;
11717 if (!fn_context)
11718 push_to_top_level ();
11719 else
11720 {
11721 cp_unevaluated_operand = 0;
11722 c_inhibit_evaluation_warnings = 0;
11723 }
11724 /* Use #pragma pack from the template context. */
11725 saved_maximum_field_alignment = maximum_field_alignment;
11726 maximum_field_alignment = TYPE_PRECISION (pattern);
11727
11728 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11729
11730 /* Set the input location to the most specialized template definition.
11731 This is needed if tsubsting causes an error. */
11732 typedecl = TYPE_MAIN_DECL (pattern);
11733 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11734 DECL_SOURCE_LOCATION (typedecl);
11735
11736 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11737 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11738 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11739 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11740 if (ANON_AGGR_TYPE_P (pattern))
11741 SET_ANON_AGGR_TYPE_P (type);
11742 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11743 {
11744 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11745 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11746 /* Adjust visibility for template arguments. */
11747 determine_visibility (TYPE_MAIN_DECL (type));
11748 }
11749 if (CLASS_TYPE_P (type))
11750 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11751
11752 pbinfo = TYPE_BINFO (pattern);
11753
11754 /* We should never instantiate a nested class before its enclosing
11755 class; we need to look up the nested class by name before we can
11756 instantiate it, and that lookup should instantiate the enclosing
11757 class. */
11758 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11759 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11760
11761 base_list = NULL_TREE;
11762 if (BINFO_N_BASE_BINFOS (pbinfo))
11763 {
11764 tree pbase_binfo;
11765 tree pushed_scope;
11766 int i;
11767
11768 /* We must enter the scope containing the type, as that is where
11769 the accessibility of types named in dependent bases are
11770 looked up from. */
11771 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11772
11773 /* Substitute into each of the bases to determine the actual
11774 basetypes. */
11775 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11776 {
11777 tree base;
11778 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11779 tree expanded_bases = NULL_TREE;
11780 int idx, len = 1;
11781
11782 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11783 {
11784 expanded_bases =
11785 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11786 args, tf_error, NULL_TREE);
11787 if (expanded_bases == error_mark_node)
11788 continue;
11789
11790 len = TREE_VEC_LENGTH (expanded_bases);
11791 }
11792
11793 for (idx = 0; idx < len; idx++)
11794 {
11795 if (expanded_bases)
11796 /* Extract the already-expanded base class. */
11797 base = TREE_VEC_ELT (expanded_bases, idx);
11798 else
11799 /* Substitute to figure out the base class. */
11800 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11801 NULL_TREE);
11802
11803 if (base == error_mark_node)
11804 continue;
11805
11806 base_list = tree_cons (access, base, base_list);
11807 if (BINFO_VIRTUAL_P (pbase_binfo))
11808 TREE_TYPE (base_list) = integer_type_node;
11809 }
11810 }
11811
11812 /* The list is now in reverse order; correct that. */
11813 base_list = nreverse (base_list);
11814
11815 if (pushed_scope)
11816 pop_scope (pushed_scope);
11817 }
11818 /* Now call xref_basetypes to set up all the base-class
11819 information. */
11820 xref_basetypes (type, base_list);
11821
11822 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11823 (int) ATTR_FLAG_TYPE_IN_PLACE,
11824 args, tf_error, NULL_TREE);
11825 fixup_attribute_variants (type);
11826
11827 /* Now that our base classes are set up, enter the scope of the
11828 class, so that name lookups into base classes, etc. will work
11829 correctly. This is precisely analogous to what we do in
11830 begin_class_definition when defining an ordinary non-template
11831 class, except we also need to push the enclosing classes. */
11832 push_nested_class (type);
11833
11834 /* Now members are processed in the order of declaration. */
11835 for (member = CLASSTYPE_DECL_LIST (pattern);
11836 member; member = TREE_CHAIN (member))
11837 {
11838 tree t = TREE_VALUE (member);
11839
11840 if (TREE_PURPOSE (member))
11841 {
11842 if (TYPE_P (t))
11843 {
11844 if (LAMBDA_TYPE_P (t))
11845 /* A closure type for a lambda in an NSDMI or default argument.
11846 Ignore it; it will be regenerated when needed. */
11847 continue;
11848
11849 /* Build new CLASSTYPE_NESTED_UTDS. */
11850 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11851 && TYPE_LANG_SPECIFIC (t)
11852 && CLASSTYPE_IS_TEMPLATE (t));
11853
11854 /* If the member is a class template, then -- even after
11855 substitution -- there may be dependent types in the
11856 template argument list for the class. We increment
11857 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11858 that function will assume that no types are dependent
11859 when outside of a template. */
11860 if (class_template_p)
11861 ++processing_template_decl;
11862 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11863 if (class_template_p)
11864 --processing_template_decl;
11865 if (newtag == error_mark_node)
11866 continue;
11867
11868 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11869 {
11870 tree name = TYPE_IDENTIFIER (t);
11871
11872 if (class_template_p)
11873 /* Unfortunately, lookup_template_class sets
11874 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11875 instantiation (i.e., for the type of a member
11876 template class nested within a template class.)
11877 This behavior is required for
11878 maybe_process_partial_specialization to work
11879 correctly, but is not accurate in this case;
11880 the TAG is not an instantiation of anything.
11881 (The corresponding TEMPLATE_DECL is an
11882 instantiation, but the TYPE is not.) */
11883 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11884
11885 /* Now, we call pushtag to put this NEWTAG into the scope of
11886 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11887 pushtag calling push_template_decl. We don't have to do
11888 this for enums because it will already have been done in
11889 tsubst_enum. */
11890 if (name)
11891 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11892 pushtag (name, newtag);
11893 }
11894 }
11895 else if (DECL_DECLARES_FUNCTION_P (t))
11896 {
11897 tree r;
11898
11899 if (TREE_CODE (t) == TEMPLATE_DECL)
11900 ++processing_template_decl;
11901 r = tsubst (t, args, tf_error, NULL_TREE);
11902 if (TREE_CODE (t) == TEMPLATE_DECL)
11903 --processing_template_decl;
11904 set_current_access_from_decl (r);
11905 finish_member_declaration (r);
11906 /* Instantiate members marked with attribute used. */
11907 if (r != error_mark_node && DECL_PRESERVE_P (r))
11908 mark_used (r);
11909 if (TREE_CODE (r) == FUNCTION_DECL
11910 && DECL_OMP_DECLARE_REDUCTION_P (r))
11911 cp_check_omp_declare_reduction (r);
11912 }
11913 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11914 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11915 /* A closure type for a lambda in an NSDMI or default argument.
11916 Ignore it; it will be regenerated when needed. */;
11917 else
11918 {
11919 /* Build new TYPE_FIELDS. */
11920 if (TREE_CODE (t) == STATIC_ASSERT)
11921 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11922 /*integral_constant_expression_p=*/true);
11923 else if (TREE_CODE (t) != CONST_DECL)
11924 {
11925 tree r;
11926 tree vec = NULL_TREE;
11927 int len = 1;
11928
11929 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11930 /* The file and line for this declaration, to
11931 assist in error message reporting. Since we
11932 called push_tinst_level above, we don't need to
11933 restore these. */
11934 input_location = DECL_SOURCE_LOCATION (t);
11935
11936 if (TREE_CODE (t) == TEMPLATE_DECL)
11937 ++processing_template_decl;
11938 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11939 if (TREE_CODE (t) == TEMPLATE_DECL)
11940 --processing_template_decl;
11941
11942 if (TREE_CODE (r) == TREE_VEC)
11943 {
11944 /* A capture pack became multiple fields. */
11945 vec = r;
11946 len = TREE_VEC_LENGTH (vec);
11947 }
11948
11949 for (int i = 0; i < len; ++i)
11950 {
11951 if (vec)
11952 r = TREE_VEC_ELT (vec, i);
11953 if (VAR_P (r))
11954 {
11955 /* In [temp.inst]:
11956
11957 [t]he initialization (and any associated
11958 side-effects) of a static data member does
11959 not occur unless the static data member is
11960 itself used in a way that requires the
11961 definition of the static data member to
11962 exist.
11963
11964 Therefore, we do not substitute into the
11965 initialized for the static data member here. */
11966 finish_static_data_member_decl
11967 (r,
11968 /*init=*/NULL_TREE,
11969 /*init_const_expr_p=*/false,
11970 /*asmspec_tree=*/NULL_TREE,
11971 /*flags=*/0);
11972 /* Instantiate members marked with attribute used. */
11973 if (r != error_mark_node && DECL_PRESERVE_P (r))
11974 mark_used (r);
11975 }
11976 else if (TREE_CODE (r) == FIELD_DECL)
11977 {
11978 /* Determine whether R has a valid type and can be
11979 completed later. If R is invalid, then its type
11980 is replaced by error_mark_node. */
11981 tree rtype = TREE_TYPE (r);
11982 if (can_complete_type_without_circularity (rtype))
11983 complete_type (rtype);
11984
11985 if (!complete_or_array_type_p (rtype))
11986 {
11987 /* If R's type couldn't be completed and
11988 it isn't a flexible array member (whose
11989 type is incomplete by definition) give
11990 an error. */
11991 cxx_incomplete_type_error (r, rtype);
11992 TREE_TYPE (r) = error_mark_node;
11993 }
11994 else if (TREE_CODE (rtype) == ARRAY_TYPE
11995 && TYPE_DOMAIN (rtype) == NULL_TREE
11996 && (TREE_CODE (type) == UNION_TYPE
11997 || TREE_CODE (type) == QUAL_UNION_TYPE))
11998 {
11999 error ("flexible array member %qD in union", r);
12000 TREE_TYPE (r) = error_mark_node;
12001 }
12002 else if (!verify_type_context (input_location,
12003 TCTX_FIELD, rtype))
12004 TREE_TYPE (r) = error_mark_node;
12005 }
12006
12007 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
12008 such a thing will already have been added to the field
12009 list by tsubst_enum in finish_member_declaration in the
12010 CLASSTYPE_NESTED_UTDS case above. */
12011 if (!(TREE_CODE (r) == TYPE_DECL
12012 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12013 && DECL_ARTIFICIAL (r)))
12014 {
12015 set_current_access_from_decl (r);
12016 finish_member_declaration (r);
12017 }
12018 }
12019 }
12020 }
12021 }
12022 else
12023 {
12024 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12025 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12026 {
12027 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12028
12029 tree friend_type = t;
12030 bool adjust_processing_template_decl = false;
12031
12032 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12033 {
12034 /* template <class T> friend class C; */
12035 friend_type = tsubst_friend_class (friend_type, args);
12036 adjust_processing_template_decl = true;
12037 }
12038 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12039 {
12040 /* template <class T> friend class C::D; */
12041 friend_type = tsubst (friend_type, args,
12042 tf_warning_or_error, NULL_TREE);
12043 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12044 friend_type = TREE_TYPE (friend_type);
12045 adjust_processing_template_decl = true;
12046 }
12047 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12048 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12049 {
12050 /* This could be either
12051
12052 friend class T::C;
12053
12054 when dependent_type_p is false or
12055
12056 template <class U> friend class T::C;
12057
12058 otherwise. */
12059 /* Bump processing_template_decl in case this is something like
12060 template <class T> friend struct A<T>::B. */
12061 ++processing_template_decl;
12062 friend_type = tsubst (friend_type, args,
12063 tf_warning_or_error, NULL_TREE);
12064 if (dependent_type_p (friend_type))
12065 adjust_processing_template_decl = true;
12066 --processing_template_decl;
12067 }
12068 else if (uses_template_parms (friend_type))
12069 /* friend class C<T>; */
12070 friend_type = tsubst (friend_type, args,
12071 tf_warning_or_error, NULL_TREE);
12072
12073 /* Otherwise it's
12074
12075 friend class C;
12076
12077 where C is already declared or
12078
12079 friend class C<int>;
12080
12081 We don't have to do anything in these cases. */
12082
12083 if (adjust_processing_template_decl)
12084 /* Trick make_friend_class into realizing that the friend
12085 we're adding is a template, not an ordinary class. It's
12086 important that we use make_friend_class since it will
12087 perform some error-checking and output cross-reference
12088 information. */
12089 ++processing_template_decl;
12090
12091 if (friend_type != error_mark_node)
12092 make_friend_class (type, friend_type, /*complain=*/false);
12093
12094 if (adjust_processing_template_decl)
12095 --processing_template_decl;
12096 }
12097 else
12098 {
12099 /* Build new DECL_FRIENDLIST. */
12100 tree r;
12101
12102 /* The file and line for this declaration, to
12103 assist in error message reporting. Since we
12104 called push_tinst_level above, we don't need to
12105 restore these. */
12106 input_location = DECL_SOURCE_LOCATION (t);
12107
12108 if (TREE_CODE (t) == TEMPLATE_DECL)
12109 {
12110 ++processing_template_decl;
12111 push_deferring_access_checks (dk_no_check);
12112 }
12113
12114 r = tsubst_friend_function (t, args);
12115 add_friend (type, r, /*complain=*/false);
12116 if (TREE_CODE (t) == TEMPLATE_DECL)
12117 {
12118 pop_deferring_access_checks ();
12119 --processing_template_decl;
12120 }
12121 }
12122 }
12123 }
12124
12125 if (fn_context)
12126 {
12127 /* Restore these before substituting into the lambda capture
12128 initializers. */
12129 cp_unevaluated_operand = saved_unevaluated_operand;
12130 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12131 }
12132
12133 /* Set the file and line number information to whatever is given for
12134 the class itself. This puts error messages involving generated
12135 implicit functions at a predictable point, and the same point
12136 that would be used for non-template classes. */
12137 input_location = DECL_SOURCE_LOCATION (typedecl);
12138
12139 unreverse_member_declarations (type);
12140 finish_struct_1 (type);
12141 TYPE_BEING_DEFINED (type) = 0;
12142
12143 /* We don't instantiate default arguments for member functions. 14.7.1:
12144
12145 The implicit instantiation of a class template specialization causes
12146 the implicit instantiation of the declarations, but not of the
12147 definitions or default arguments, of the class member functions,
12148 member classes, static data members and member templates.... */
12149
12150 perform_instantiation_time_access_checks (pattern, args);
12151 perform_deferred_access_checks (tf_warning_or_error);
12152 pop_nested_class ();
12153 maximum_field_alignment = saved_maximum_field_alignment;
12154 if (!fn_context)
12155 pop_from_top_level ();
12156 pop_tinst_level ();
12157
12158 /* The vtable for a template class can be emitted in any translation
12159 unit in which the class is instantiated. When there is no key
12160 method, however, finish_struct_1 will already have added TYPE to
12161 the keyed_classes. */
12162 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12163 vec_safe_push (keyed_classes, type);
12164
12165 return type;
12166 }
12167
12168 /* Wrapper for instantiate_class_template_1. */
12169
12170 tree
12171 instantiate_class_template (tree type)
12172 {
12173 tree ret;
12174 timevar_push (TV_TEMPLATE_INST);
12175 ret = instantiate_class_template_1 (type);
12176 timevar_pop (TV_TEMPLATE_INST);
12177 return ret;
12178 }
12179
12180 tree
12181 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12182 {
12183 tree r;
12184
12185 if (!t)
12186 r = t;
12187 else if (TYPE_P (t))
12188 r = tsubst (t, args, complain, in_decl);
12189 else
12190 {
12191 if (!(complain & tf_warning))
12192 ++c_inhibit_evaluation_warnings;
12193 r = tsubst_expr (t, args, complain, in_decl,
12194 /*integral_constant_expression_p=*/true);
12195 if (!(complain & tf_warning))
12196 --c_inhibit_evaluation_warnings;
12197 }
12198
12199 return r;
12200 }
12201
12202 /* Given a function parameter pack TMPL_PARM and some function parameters
12203 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12204 and set *SPEC_P to point at the next point in the list. */
12205
12206 tree
12207 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12208 {
12209 /* Collect all of the extra "packed" parameters into an
12210 argument pack. */
12211 tree parmvec;
12212 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12213 tree spec_parm = *spec_p;
12214 int i, len;
12215
12216 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12217 if (tmpl_parm
12218 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12219 break;
12220
12221 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12222 parmvec = make_tree_vec (len);
12223 spec_parm = *spec_p;
12224 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12225 {
12226 tree elt = spec_parm;
12227 if (DECL_PACK_P (elt))
12228 elt = make_pack_expansion (elt);
12229 TREE_VEC_ELT (parmvec, i) = elt;
12230 }
12231
12232 /* Build the argument packs. */
12233 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12234 *spec_p = spec_parm;
12235
12236 return argpack;
12237 }
12238
12239 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12240 NONTYPE_ARGUMENT_PACK. */
12241
12242 static tree
12243 make_fnparm_pack (tree spec_parm)
12244 {
12245 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12246 }
12247
12248 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12249 pack expansion with no extra args, 2 if it has extra args, or 0
12250 if it is not a pack expansion. */
12251
12252 static int
12253 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12254 {
12255 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12256 /* We're being called before this happens in tsubst_pack_expansion. */
12257 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12258 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12259 if (i >= TREE_VEC_LENGTH (vec))
12260 return 0;
12261 tree elt = TREE_VEC_ELT (vec, i);
12262 if (DECL_P (elt))
12263 /* A decl pack is itself an expansion. */
12264 elt = TREE_TYPE (elt);
12265 if (!PACK_EXPANSION_P (elt))
12266 return 0;
12267 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12268 return 2;
12269 return 1;
12270 }
12271
12272
12273 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12274
12275 static tree
12276 make_argument_pack_select (tree arg_pack, unsigned index)
12277 {
12278 tree aps = make_node (ARGUMENT_PACK_SELECT);
12279
12280 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12281 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12282
12283 return aps;
12284 }
12285
12286 /* This is a subroutine of tsubst_pack_expansion.
12287
12288 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12289 mechanism to store the (non complete list of) arguments of the
12290 substitution and return a non substituted pack expansion, in order
12291 to wait for when we have enough arguments to really perform the
12292 substitution. */
12293
12294 static bool
12295 use_pack_expansion_extra_args_p (tree parm_packs,
12296 int arg_pack_len,
12297 bool has_empty_arg)
12298 {
12299 /* If one pack has an expansion and another pack has a normal
12300 argument or if one pack has an empty argument and an another
12301 one hasn't then tsubst_pack_expansion cannot perform the
12302 substitution and need to fall back on the
12303 PACK_EXPANSION_EXTRA mechanism. */
12304 if (parm_packs == NULL_TREE)
12305 return false;
12306 else if (has_empty_arg)
12307 {
12308 /* If all the actual packs are pack expansions, we can still
12309 subsitute directly. */
12310 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12311 {
12312 tree a = TREE_VALUE (p);
12313 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12314 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12315 a = ARGUMENT_PACK_ARGS (a);
12316 if (TREE_VEC_LENGTH (a) == 1)
12317 a = TREE_VEC_ELT (a, 0);
12318 if (PACK_EXPANSION_P (a))
12319 continue;
12320 return true;
12321 }
12322 return false;
12323 }
12324
12325 bool has_expansion_arg = false;
12326 for (int i = 0 ; i < arg_pack_len; ++i)
12327 {
12328 bool has_non_expansion_arg = false;
12329 for (tree parm_pack = parm_packs;
12330 parm_pack;
12331 parm_pack = TREE_CHAIN (parm_pack))
12332 {
12333 tree arg = TREE_VALUE (parm_pack);
12334
12335 int exp = argument_pack_element_is_expansion_p (arg, i);
12336 if (exp == 2)
12337 /* We can't substitute a pack expansion with extra args into
12338 our pattern. */
12339 return true;
12340 else if (exp)
12341 has_expansion_arg = true;
12342 else
12343 has_non_expansion_arg = true;
12344 }
12345
12346 if (has_expansion_arg && has_non_expansion_arg)
12347 return true;
12348 }
12349 return false;
12350 }
12351
12352 /* [temp.variadic]/6 says that:
12353
12354 The instantiation of a pack expansion [...]
12355 produces a list E1,E2, ..., En, where N is the number of elements
12356 in the pack expansion parameters.
12357
12358 This subroutine of tsubst_pack_expansion produces one of these Ei.
12359
12360 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12361 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12362 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12363 INDEX is the index 'i' of the element Ei to produce. ARGS,
12364 COMPLAIN, and IN_DECL are the same parameters as for the
12365 tsubst_pack_expansion function.
12366
12367 The function returns the resulting Ei upon successful completion,
12368 or error_mark_node.
12369
12370 Note that this function possibly modifies the ARGS parameter, so
12371 it's the responsibility of the caller to restore it. */
12372
12373 static tree
12374 gen_elem_of_pack_expansion_instantiation (tree pattern,
12375 tree parm_packs,
12376 unsigned index,
12377 tree args /* This parm gets
12378 modified. */,
12379 tsubst_flags_t complain,
12380 tree in_decl)
12381 {
12382 tree t;
12383 bool ith_elem_is_expansion = false;
12384
12385 /* For each parameter pack, change the substitution of the parameter
12386 pack to the ith argument in its argument pack, then expand the
12387 pattern. */
12388 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12389 {
12390 tree parm = TREE_PURPOSE (pack);
12391 tree arg_pack = TREE_VALUE (pack);
12392 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12393
12394 ith_elem_is_expansion |=
12395 argument_pack_element_is_expansion_p (arg_pack, index);
12396
12397 /* Select the Ith argument from the pack. */
12398 if (TREE_CODE (parm) == PARM_DECL
12399 || VAR_P (parm)
12400 || TREE_CODE (parm) == FIELD_DECL)
12401 {
12402 if (index == 0)
12403 {
12404 aps = make_argument_pack_select (arg_pack, index);
12405 if (!mark_used (parm, complain) && !(complain & tf_error))
12406 return error_mark_node;
12407 register_local_specialization (aps, parm);
12408 }
12409 else
12410 aps = retrieve_local_specialization (parm);
12411 }
12412 else
12413 {
12414 int idx, level;
12415 template_parm_level_and_index (parm, &level, &idx);
12416
12417 if (index == 0)
12418 {
12419 aps = make_argument_pack_select (arg_pack, index);
12420 /* Update the corresponding argument. */
12421 TMPL_ARG (args, level, idx) = aps;
12422 }
12423 else
12424 /* Re-use the ARGUMENT_PACK_SELECT. */
12425 aps = TMPL_ARG (args, level, idx);
12426 }
12427 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12428 }
12429
12430 /* Substitute into the PATTERN with the (possibly altered)
12431 arguments. */
12432 if (pattern == in_decl)
12433 /* Expanding a fixed parameter pack from
12434 coerce_template_parameter_pack. */
12435 t = tsubst_decl (pattern, args, complain);
12436 else if (pattern == error_mark_node)
12437 t = error_mark_node;
12438 else if (!TYPE_P (pattern))
12439 t = tsubst_expr (pattern, args, complain, in_decl,
12440 /*integral_constant_expression_p=*/false);
12441 else
12442 t = tsubst (pattern, args, complain, in_decl);
12443
12444 /* If the Ith argument pack element is a pack expansion, then
12445 the Ith element resulting from the substituting is going to
12446 be a pack expansion as well. */
12447 if (ith_elem_is_expansion)
12448 t = make_pack_expansion (t, complain);
12449
12450 return t;
12451 }
12452
12453 /* When the unexpanded parameter pack in a fold expression expands to an empty
12454 sequence, the value of the expression is as follows; the program is
12455 ill-formed if the operator is not listed in this table.
12456
12457 && true
12458 || false
12459 , void() */
12460
12461 tree
12462 expand_empty_fold (tree t, tsubst_flags_t complain)
12463 {
12464 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12465 if (!FOLD_EXPR_MODIFY_P (t))
12466 switch (code)
12467 {
12468 case TRUTH_ANDIF_EXPR:
12469 return boolean_true_node;
12470 case TRUTH_ORIF_EXPR:
12471 return boolean_false_node;
12472 case COMPOUND_EXPR:
12473 return void_node;
12474 default:
12475 break;
12476 }
12477
12478 if (complain & tf_error)
12479 error_at (location_of (t),
12480 "fold of empty expansion over %O", code);
12481 return error_mark_node;
12482 }
12483
12484 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12485 form an expression that combines the two terms using the
12486 operator of T. */
12487
12488 static tree
12489 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12490 {
12491 tree op = FOLD_EXPR_OP (t);
12492 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12493
12494 // Handle compound assignment operators.
12495 if (FOLD_EXPR_MODIFY_P (t))
12496 return build_x_modify_expr (input_location, left, code, right, complain);
12497
12498 warning_sentinel s(warn_parentheses);
12499 switch (code)
12500 {
12501 case COMPOUND_EXPR:
12502 return build_x_compound_expr (input_location, left, right, complain);
12503 default:
12504 return build_x_binary_op (input_location, code,
12505 left, TREE_CODE (left),
12506 right, TREE_CODE (right),
12507 /*overload=*/NULL,
12508 complain);
12509 }
12510 }
12511
12512 /* Substitute ARGS into the pack of a fold expression T. */
12513
12514 static inline tree
12515 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12516 {
12517 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12518 }
12519
12520 /* Substitute ARGS into the pack of a fold expression T. */
12521
12522 static inline tree
12523 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12524 {
12525 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12526 }
12527
12528 /* Expand a PACK of arguments into a grouped as left fold.
12529 Given a pack containing elements A0, A1, ..., An and an
12530 operator @, this builds the expression:
12531
12532 ((A0 @ A1) @ A2) ... @ An
12533
12534 Note that PACK must not be empty.
12535
12536 The operator is defined by the original fold expression T. */
12537
12538 static tree
12539 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12540 {
12541 tree left = TREE_VEC_ELT (pack, 0);
12542 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12543 {
12544 tree right = TREE_VEC_ELT (pack, i);
12545 left = fold_expression (t, left, right, complain);
12546 }
12547 return left;
12548 }
12549
12550 /* Substitute into a unary left fold expression. */
12551
12552 static tree
12553 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12554 tree in_decl)
12555 {
12556 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12557 if (pack == error_mark_node)
12558 return error_mark_node;
12559 if (PACK_EXPANSION_P (pack))
12560 {
12561 tree r = copy_node (t);
12562 FOLD_EXPR_PACK (r) = pack;
12563 return r;
12564 }
12565 if (TREE_VEC_LENGTH (pack) == 0)
12566 return expand_empty_fold (t, complain);
12567 else
12568 return expand_left_fold (t, pack, complain);
12569 }
12570
12571 /* Substitute into a binary left fold expression.
12572
12573 Do ths by building a single (non-empty) vector of argumnts and
12574 building the expression from those elements. */
12575
12576 static tree
12577 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12578 tree in_decl)
12579 {
12580 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12581 if (pack == error_mark_node)
12582 return error_mark_node;
12583 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12584 if (init == error_mark_node)
12585 return error_mark_node;
12586
12587 if (PACK_EXPANSION_P (pack))
12588 {
12589 tree r = copy_node (t);
12590 FOLD_EXPR_PACK (r) = pack;
12591 FOLD_EXPR_INIT (r) = init;
12592 return r;
12593 }
12594
12595 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12596 TREE_VEC_ELT (vec, 0) = init;
12597 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12598 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12599
12600 return expand_left_fold (t, vec, complain);
12601 }
12602
12603 /* Expand a PACK of arguments into a grouped as right fold.
12604 Given a pack containing elementns A0, A1, ..., and an
12605 operator @, this builds the expression:
12606
12607 A0@ ... (An-2 @ (An-1 @ An))
12608
12609 Note that PACK must not be empty.
12610
12611 The operator is defined by the original fold expression T. */
12612
12613 tree
12614 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12615 {
12616 // Build the expression.
12617 int n = TREE_VEC_LENGTH (pack);
12618 tree right = TREE_VEC_ELT (pack, n - 1);
12619 for (--n; n != 0; --n)
12620 {
12621 tree left = TREE_VEC_ELT (pack, n - 1);
12622 right = fold_expression (t, left, right, complain);
12623 }
12624 return right;
12625 }
12626
12627 /* Substitute into a unary right fold expression. */
12628
12629 static tree
12630 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12631 tree in_decl)
12632 {
12633 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12634 if (pack == error_mark_node)
12635 return error_mark_node;
12636 if (PACK_EXPANSION_P (pack))
12637 {
12638 tree r = copy_node (t);
12639 FOLD_EXPR_PACK (r) = pack;
12640 return r;
12641 }
12642 if (TREE_VEC_LENGTH (pack) == 0)
12643 return expand_empty_fold (t, complain);
12644 else
12645 return expand_right_fold (t, pack, complain);
12646 }
12647
12648 /* Substitute into a binary right fold expression.
12649
12650 Do ths by building a single (non-empty) vector of arguments and
12651 building the expression from those elements. */
12652
12653 static tree
12654 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12655 tree in_decl)
12656 {
12657 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12658 if (pack == error_mark_node)
12659 return error_mark_node;
12660 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12661 if (init == error_mark_node)
12662 return error_mark_node;
12663
12664 if (PACK_EXPANSION_P (pack))
12665 {
12666 tree r = copy_node (t);
12667 FOLD_EXPR_PACK (r) = pack;
12668 FOLD_EXPR_INIT (r) = init;
12669 return r;
12670 }
12671
12672 int n = TREE_VEC_LENGTH (pack);
12673 tree vec = make_tree_vec (n + 1);
12674 for (int i = 0; i < n; ++i)
12675 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12676 TREE_VEC_ELT (vec, n) = init;
12677
12678 return expand_right_fold (t, vec, complain);
12679 }
12680
12681 /* Walk through the pattern of a pack expansion, adding everything in
12682 local_specializations to a list. */
12683
12684 class el_data
12685 {
12686 public:
12687 hash_set<tree> internal;
12688 tree extra;
12689 tsubst_flags_t complain;
12690
12691 el_data (tsubst_flags_t c)
12692 : extra (NULL_TREE), complain (c) {}
12693 };
12694 static tree
12695 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12696 {
12697 el_data &data = *reinterpret_cast<el_data*>(data_);
12698 tree *extra = &data.extra;
12699 tsubst_flags_t complain = data.complain;
12700
12701 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12702 /* Remember local typedefs (85214). */
12703 tp = &TYPE_NAME (*tp);
12704
12705 if (TREE_CODE (*tp) == DECL_EXPR)
12706 data.internal.add (DECL_EXPR_DECL (*tp));
12707 else if (tree spec = retrieve_local_specialization (*tp))
12708 {
12709 if (data.internal.contains (*tp))
12710 /* Don't mess with variables declared within the pattern. */
12711 return NULL_TREE;
12712 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12713 {
12714 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12715 tree args = ARGUMENT_PACK_ARGS (spec);
12716 if (TREE_VEC_LENGTH (args) == 1)
12717 {
12718 tree elt = TREE_VEC_ELT (args, 0);
12719 if (PACK_EXPANSION_P (elt))
12720 elt = PACK_EXPANSION_PATTERN (elt);
12721 if (DECL_PACK_P (elt))
12722 spec = elt;
12723 }
12724 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12725 {
12726 /* Handle lambda capture here, since we aren't doing any
12727 substitution now, and so tsubst_copy won't call
12728 process_outer_var_ref. */
12729 tree args = ARGUMENT_PACK_ARGS (spec);
12730 int len = TREE_VEC_LENGTH (args);
12731 for (int i = 0; i < len; ++i)
12732 {
12733 tree arg = TREE_VEC_ELT (args, i);
12734 tree carg = arg;
12735 if (outer_automatic_var_p (arg))
12736 carg = process_outer_var_ref (arg, complain);
12737 if (carg != arg)
12738 {
12739 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12740 proxies. */
12741 if (i == 0)
12742 {
12743 spec = copy_node (spec);
12744 args = copy_node (args);
12745 SET_ARGUMENT_PACK_ARGS (spec, args);
12746 register_local_specialization (spec, *tp);
12747 }
12748 TREE_VEC_ELT (args, i) = carg;
12749 }
12750 }
12751 }
12752 }
12753 if (outer_automatic_var_p (spec))
12754 spec = process_outer_var_ref (spec, complain);
12755 *extra = tree_cons (*tp, spec, *extra);
12756 }
12757 return NULL_TREE;
12758 }
12759 static tree
12760 extract_local_specs (tree pattern, tsubst_flags_t complain)
12761 {
12762 el_data data (complain);
12763 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12764 return data.extra;
12765 }
12766
12767 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12768 for use in PACK_EXPANSION_EXTRA_ARGS. */
12769
12770 tree
12771 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12772 {
12773 tree extra = args;
12774 if (local_specializations)
12775 if (tree locals = extract_local_specs (pattern, complain))
12776 extra = tree_cons (NULL_TREE, extra, locals);
12777 return extra;
12778 }
12779
12780 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12781 normal template args to ARGS. */
12782
12783 tree
12784 add_extra_args (tree extra, tree args)
12785 {
12786 if (extra && TREE_CODE (extra) == TREE_LIST)
12787 {
12788 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12789 {
12790 /* The partial instantiation involved local declarations collected in
12791 extract_local_specs; map from the general template to our local
12792 context. */
12793 tree gen = TREE_PURPOSE (elt);
12794 tree inst = TREE_VALUE (elt);
12795 if (DECL_P (inst))
12796 if (tree local = retrieve_local_specialization (inst))
12797 inst = local;
12798 /* else inst is already a full instantiation of the pack. */
12799 register_local_specialization (inst, gen);
12800 }
12801 gcc_assert (!TREE_PURPOSE (extra));
12802 extra = TREE_VALUE (extra);
12803 }
12804 #if 1
12805 /* I think we should always be able to substitute dependent args into the
12806 pattern. If that turns out to be incorrect in some cases, enable the
12807 alternate code (and add complain/in_decl parms to this function). */
12808 gcc_checking_assert (!uses_template_parms (extra));
12809 #else
12810 if (!uses_template_parms (extra))
12811 {
12812 gcc_unreachable ();
12813 extra = tsubst_template_args (extra, args, complain, in_decl);
12814 args = add_outermost_template_args (args, extra);
12815 }
12816 else
12817 #endif
12818 args = add_to_template_args (extra, args);
12819 return args;
12820 }
12821
12822 /* Substitute ARGS into T, which is an pack expansion
12823 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12824 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12825 (if only a partial substitution could be performed) or
12826 ERROR_MARK_NODE if there was an error. */
12827 tree
12828 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12829 tree in_decl)
12830 {
12831 tree pattern;
12832 tree pack, packs = NULL_TREE;
12833 bool unsubstituted_packs = false;
12834 int i, len = -1;
12835 tree result;
12836 bool need_local_specializations = false;
12837 int levels;
12838
12839 gcc_assert (PACK_EXPANSION_P (t));
12840 pattern = PACK_EXPANSION_PATTERN (t);
12841
12842 /* Add in any args remembered from an earlier partial instantiation. */
12843 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12844
12845 levels = TMPL_ARGS_DEPTH (args);
12846
12847 /* Determine the argument packs that will instantiate the parameter
12848 packs used in the expansion expression. While we're at it,
12849 compute the number of arguments to be expanded and make sure it
12850 is consistent. */
12851 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12852 pack = TREE_CHAIN (pack))
12853 {
12854 tree parm_pack = TREE_VALUE (pack);
12855 tree arg_pack = NULL_TREE;
12856 tree orig_arg = NULL_TREE;
12857 int level = 0;
12858
12859 if (TREE_CODE (parm_pack) == BASES)
12860 {
12861 gcc_assert (parm_pack == pattern);
12862 if (BASES_DIRECT (parm_pack))
12863 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12864 args, complain,
12865 in_decl, false),
12866 complain);
12867 else
12868 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12869 args, complain, in_decl,
12870 false), complain);
12871 }
12872 else if (builtin_pack_call_p (parm_pack))
12873 {
12874 if (parm_pack != pattern)
12875 {
12876 if (complain & tf_error)
12877 sorry ("%qE is not the entire pattern of the pack expansion",
12878 parm_pack);
12879 return error_mark_node;
12880 }
12881 return expand_builtin_pack_call (parm_pack, args,
12882 complain, in_decl);
12883 }
12884 else if (TREE_CODE (parm_pack) == PARM_DECL)
12885 {
12886 /* We know we have correct local_specializations if this
12887 expansion is at function scope, or if we're dealing with a
12888 local parameter in a requires expression; for the latter,
12889 tsubst_requires_expr set it up appropriately. */
12890 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12891 arg_pack = retrieve_local_specialization (parm_pack);
12892 else
12893 /* We can't rely on local_specializations for a parameter
12894 name used later in a function declaration (such as in a
12895 late-specified return type). Even if it exists, it might
12896 have the wrong value for a recursive call. */
12897 need_local_specializations = true;
12898
12899 if (!arg_pack)
12900 {
12901 /* This parameter pack was used in an unevaluated context. Just
12902 make a dummy decl, since it's only used for its type. */
12903 ++cp_unevaluated_operand;
12904 arg_pack = tsubst_decl (parm_pack, args, complain);
12905 --cp_unevaluated_operand;
12906 if (arg_pack && DECL_PACK_P (arg_pack))
12907 /* Partial instantiation of the parm_pack, we can't build
12908 up an argument pack yet. */
12909 arg_pack = NULL_TREE;
12910 else
12911 arg_pack = make_fnparm_pack (arg_pack);
12912 }
12913 else if (DECL_PACK_P (arg_pack))
12914 /* This argument pack isn't fully instantiated yet. */
12915 arg_pack = NULL_TREE;
12916 }
12917 else if (is_capture_proxy (parm_pack))
12918 {
12919 arg_pack = retrieve_local_specialization (parm_pack);
12920 if (DECL_PACK_P (arg_pack))
12921 arg_pack = NULL_TREE;
12922 }
12923 else
12924 {
12925 int idx;
12926 template_parm_level_and_index (parm_pack, &level, &idx);
12927 if (level <= levels)
12928 arg_pack = TMPL_ARG (args, level, idx);
12929
12930 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12931 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12932 arg_pack = NULL_TREE;
12933 }
12934
12935 orig_arg = arg_pack;
12936 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12937 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12938
12939 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12940 /* This can only happen if we forget to expand an argument
12941 pack somewhere else. Just return an error, silently. */
12942 {
12943 result = make_tree_vec (1);
12944 TREE_VEC_ELT (result, 0) = error_mark_node;
12945 return result;
12946 }
12947
12948 if (arg_pack)
12949 {
12950 int my_len =
12951 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12952
12953 /* Don't bother trying to do a partial substitution with
12954 incomplete packs; we'll try again after deduction. */
12955 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12956 return t;
12957
12958 if (len < 0)
12959 len = my_len;
12960 else if (len != my_len)
12961 {
12962 if (!(complain & tf_error))
12963 /* Fail quietly. */;
12964 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12965 error ("mismatched argument pack lengths while expanding %qT",
12966 pattern);
12967 else
12968 error ("mismatched argument pack lengths while expanding %qE",
12969 pattern);
12970 return error_mark_node;
12971 }
12972
12973 /* Keep track of the parameter packs and their corresponding
12974 argument packs. */
12975 packs = tree_cons (parm_pack, arg_pack, packs);
12976 TREE_TYPE (packs) = orig_arg;
12977 }
12978 else
12979 {
12980 /* We can't substitute for this parameter pack. We use a flag as
12981 well as the missing_level counter because function parameter
12982 packs don't have a level. */
12983 gcc_assert (processing_template_decl || is_auto (parm_pack));
12984 unsubstituted_packs = true;
12985 }
12986 }
12987
12988 /* If the expansion is just T..., return the matching argument pack, unless
12989 we need to call convert_from_reference on all the elements. This is an
12990 important optimization; see c++/68422. */
12991 if (!unsubstituted_packs
12992 && TREE_PURPOSE (packs) == pattern)
12993 {
12994 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12995
12996 /* If the argument pack is a single pack expansion, pull it out. */
12997 if (TREE_VEC_LENGTH (args) == 1
12998 && pack_expansion_args_count (args))
12999 return TREE_VEC_ELT (args, 0);
13000
13001 /* Types need no adjustment, nor does sizeof..., and if we still have
13002 some pack expansion args we won't do anything yet. */
13003 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13004 || PACK_EXPANSION_SIZEOF_P (t)
13005 || pack_expansion_args_count (args))
13006 return args;
13007 /* Also optimize expression pack expansions if we can tell that the
13008 elements won't have reference type. */
13009 tree type = TREE_TYPE (pattern);
13010 if (type && !TYPE_REF_P (type)
13011 && !PACK_EXPANSION_P (type)
13012 && !WILDCARD_TYPE_P (type))
13013 return args;
13014 /* Otherwise use the normal path so we get convert_from_reference. */
13015 }
13016
13017 /* We cannot expand this expansion expression, because we don't have
13018 all of the argument packs we need. */
13019 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13020 {
13021 /* We got some full packs, but we can't substitute them in until we
13022 have values for all the packs. So remember these until then. */
13023
13024 t = make_pack_expansion (pattern, complain);
13025 PACK_EXPANSION_EXTRA_ARGS (t)
13026 = build_extra_args (pattern, args, complain);
13027 return t;
13028 }
13029
13030 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13031 type, so create our own local specializations map; the current map is
13032 either NULL or (in the case of recursive unification) might have
13033 bindings that we don't want to use or alter. */
13034 local_specialization_stack lss (need_local_specializations
13035 ? lss_blank : lss_nop);
13036
13037 if (unsubstituted_packs)
13038 {
13039 /* There were no real arguments, we're just replacing a parameter
13040 pack with another version of itself. Substitute into the
13041 pattern and return a PACK_EXPANSION_*. The caller will need to
13042 deal with that. */
13043 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13044 t = tsubst_expr (pattern, args, complain, in_decl,
13045 /*integral_constant_expression_p=*/false);
13046 else
13047 t = tsubst (pattern, args, complain, in_decl);
13048 t = make_pack_expansion (t, complain);
13049 return t;
13050 }
13051
13052 gcc_assert (len >= 0);
13053
13054 /* For each argument in each argument pack, substitute into the
13055 pattern. */
13056 result = make_tree_vec (len);
13057 tree elem_args = copy_template_args (args);
13058 for (i = 0; i < len; ++i)
13059 {
13060 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13061 i,
13062 elem_args, complain,
13063 in_decl);
13064 TREE_VEC_ELT (result, i) = t;
13065 if (t == error_mark_node)
13066 {
13067 result = error_mark_node;
13068 break;
13069 }
13070 }
13071
13072 /* Update ARGS to restore the substitution from parameter packs to
13073 their argument packs. */
13074 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13075 {
13076 tree parm = TREE_PURPOSE (pack);
13077
13078 if (TREE_CODE (parm) == PARM_DECL
13079 || VAR_P (parm)
13080 || TREE_CODE (parm) == FIELD_DECL)
13081 register_local_specialization (TREE_TYPE (pack), parm);
13082 else
13083 {
13084 int idx, level;
13085
13086 if (TREE_VALUE (pack) == NULL_TREE)
13087 continue;
13088
13089 template_parm_level_and_index (parm, &level, &idx);
13090
13091 /* Update the corresponding argument. */
13092 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13093 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13094 TREE_TYPE (pack);
13095 else
13096 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13097 }
13098 }
13099
13100 /* If the dependent pack arguments were such that we end up with only a
13101 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13102 if (len == 1 && TREE_CODE (result) == TREE_VEC
13103 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13104 return TREE_VEC_ELT (result, 0);
13105
13106 return result;
13107 }
13108
13109 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13110 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13111 parameter packs; all parms generated from a function parameter pack will
13112 have the same DECL_PARM_INDEX. */
13113
13114 tree
13115 get_pattern_parm (tree parm, tree tmpl)
13116 {
13117 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13118 tree patparm;
13119
13120 if (DECL_ARTIFICIAL (parm))
13121 {
13122 for (patparm = DECL_ARGUMENTS (pattern);
13123 patparm; patparm = DECL_CHAIN (patparm))
13124 if (DECL_ARTIFICIAL (patparm)
13125 && DECL_NAME (parm) == DECL_NAME (patparm))
13126 break;
13127 }
13128 else
13129 {
13130 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13131 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13132 gcc_assert (DECL_PARM_INDEX (patparm)
13133 == DECL_PARM_INDEX (parm));
13134 }
13135
13136 return patparm;
13137 }
13138
13139 /* Make an argument pack out of the TREE_VEC VEC. */
13140
13141 static tree
13142 make_argument_pack (tree vec)
13143 {
13144 tree pack;
13145
13146 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13147 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13148 else
13149 {
13150 pack = make_node (NONTYPE_ARGUMENT_PACK);
13151 TREE_CONSTANT (pack) = 1;
13152 }
13153 SET_ARGUMENT_PACK_ARGS (pack, vec);
13154 return pack;
13155 }
13156
13157 /* Return an exact copy of template args T that can be modified
13158 independently. */
13159
13160 static tree
13161 copy_template_args (tree t)
13162 {
13163 if (t == error_mark_node)
13164 return t;
13165
13166 int len = TREE_VEC_LENGTH (t);
13167 tree new_vec = make_tree_vec (len);
13168
13169 for (int i = 0; i < len; ++i)
13170 {
13171 tree elt = TREE_VEC_ELT (t, i);
13172 if (elt && TREE_CODE (elt) == TREE_VEC)
13173 elt = copy_template_args (elt);
13174 TREE_VEC_ELT (new_vec, i) = elt;
13175 }
13176
13177 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13178 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13179
13180 return new_vec;
13181 }
13182
13183 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13184
13185 tree
13186 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13187 tree in_decl)
13188 {
13189 /* Substitute into each of the arguments. */
13190 tree new_arg = TYPE_P (orig_arg)
13191 ? cxx_make_type (TREE_CODE (orig_arg))
13192 : make_node (TREE_CODE (orig_arg));
13193
13194 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13195 args, complain, in_decl);
13196 if (pack_args == error_mark_node)
13197 new_arg = error_mark_node;
13198 else
13199 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13200
13201 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13202 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13203
13204 return new_arg;
13205 }
13206
13207 /* Substitute ARGS into the vector or list of template arguments T. */
13208
13209 tree
13210 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13211 {
13212 tree orig_t = t;
13213 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13214 tree *elts;
13215
13216 if (t == error_mark_node)
13217 return error_mark_node;
13218
13219 len = TREE_VEC_LENGTH (t);
13220 elts = XALLOCAVEC (tree, len);
13221
13222 for (i = 0; i < len; i++)
13223 {
13224 tree orig_arg = TREE_VEC_ELT (t, i);
13225 tree new_arg;
13226
13227 if (TREE_CODE (orig_arg) == TREE_VEC)
13228 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13229 else if (PACK_EXPANSION_P (orig_arg))
13230 {
13231 /* Substitute into an expansion expression. */
13232 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13233
13234 if (TREE_CODE (new_arg) == TREE_VEC)
13235 /* Add to the expanded length adjustment the number of
13236 expanded arguments. We subtract one from this
13237 measurement, because the argument pack expression
13238 itself is already counted as 1 in
13239 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13240 the argument pack is empty. */
13241 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13242 }
13243 else if (ARGUMENT_PACK_P (orig_arg))
13244 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13245 else
13246 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13247
13248 if (new_arg == error_mark_node)
13249 return error_mark_node;
13250
13251 elts[i] = new_arg;
13252 if (new_arg != orig_arg)
13253 need_new = 1;
13254 }
13255
13256 if (!need_new)
13257 return t;
13258
13259 /* Make space for the expanded arguments coming from template
13260 argument packs. */
13261 t = make_tree_vec (len + expanded_len_adjust);
13262 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13263 arguments for a member template.
13264 In that case each TREE_VEC in ORIG_T represents a level of template
13265 arguments, and ORIG_T won't carry any non defaulted argument count.
13266 It will rather be the nested TREE_VECs that will carry one.
13267 In other words, ORIG_T carries a non defaulted argument count only
13268 if it doesn't contain any nested TREE_VEC. */
13269 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13270 {
13271 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13272 count += expanded_len_adjust;
13273 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13274 }
13275 for (i = 0, out = 0; i < len; i++)
13276 {
13277 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13278 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13279 && TREE_CODE (elts[i]) == TREE_VEC)
13280 {
13281 int idx;
13282
13283 /* Now expand the template argument pack "in place". */
13284 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13285 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13286 }
13287 else
13288 {
13289 TREE_VEC_ELT (t, out) = elts[i];
13290 out++;
13291 }
13292 }
13293
13294 return t;
13295 }
13296
13297 /* Substitute ARGS into one level PARMS of template parameters. */
13298
13299 static tree
13300 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13301 {
13302 if (parms == error_mark_node)
13303 return error_mark_node;
13304
13305 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13306
13307 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13308 {
13309 tree tuple = TREE_VEC_ELT (parms, i);
13310
13311 if (tuple == error_mark_node)
13312 continue;
13313
13314 TREE_VEC_ELT (new_vec, i) =
13315 tsubst_template_parm (tuple, args, complain);
13316 }
13317
13318 return new_vec;
13319 }
13320
13321 /* Return the result of substituting ARGS into the template parameters
13322 given by PARMS. If there are m levels of ARGS and m + n levels of
13323 PARMS, then the result will contain n levels of PARMS. For
13324 example, if PARMS is `template <class T> template <class U>
13325 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13326 result will be `template <int*, double, class V>'. */
13327
13328 static tree
13329 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13330 {
13331 tree r = NULL_TREE;
13332 tree* new_parms;
13333
13334 /* When substituting into a template, we must set
13335 PROCESSING_TEMPLATE_DECL as the template parameters may be
13336 dependent if they are based on one-another, and the dependency
13337 predicates are short-circuit outside of templates. */
13338 ++processing_template_decl;
13339
13340 for (new_parms = &r;
13341 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13342 new_parms = &(TREE_CHAIN (*new_parms)),
13343 parms = TREE_CHAIN (parms))
13344 {
13345 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13346 args, complain);
13347 *new_parms =
13348 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13349 - TMPL_ARGS_DEPTH (args)),
13350 new_vec, NULL_TREE);
13351 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13352 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13353 }
13354
13355 --processing_template_decl;
13356
13357 return r;
13358 }
13359
13360 /* Return the result of substituting ARGS into one template parameter
13361 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13362 parameter and which TREE_PURPOSE is the default argument of the
13363 template parameter. */
13364
13365 static tree
13366 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13367 {
13368 tree default_value, parm_decl;
13369
13370 if (args == NULL_TREE
13371 || t == NULL_TREE
13372 || t == error_mark_node)
13373 return t;
13374
13375 gcc_assert (TREE_CODE (t) == TREE_LIST);
13376
13377 default_value = TREE_PURPOSE (t);
13378 parm_decl = TREE_VALUE (t);
13379 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13380
13381 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13382 if (TREE_CODE (parm_decl) == PARM_DECL
13383 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13384 parm_decl = error_mark_node;
13385 default_value = tsubst_template_arg (default_value, args,
13386 complain, NULL_TREE);
13387 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13388
13389 tree r = build_tree_list (default_value, parm_decl);
13390 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13391 return r;
13392 }
13393
13394 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13395 type T. If T is not an aggregate or enumeration type, it is
13396 handled as if by tsubst. IN_DECL is as for tsubst. If
13397 ENTERING_SCOPE is nonzero, T is the context for a template which
13398 we are presently tsubst'ing. Return the substituted value. */
13399
13400 static tree
13401 tsubst_aggr_type (tree t,
13402 tree args,
13403 tsubst_flags_t complain,
13404 tree in_decl,
13405 int entering_scope)
13406 {
13407 if (t == NULL_TREE)
13408 return NULL_TREE;
13409
13410 switch (TREE_CODE (t))
13411 {
13412 case RECORD_TYPE:
13413 if (TYPE_PTRMEMFUNC_P (t))
13414 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13415
13416 /* Fall through. */
13417 case ENUMERAL_TYPE:
13418 case UNION_TYPE:
13419 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13420 {
13421 tree argvec;
13422 tree context;
13423 tree r;
13424
13425 /* In "sizeof(X<I>)" we need to evaluate "I". */
13426 cp_evaluated ev;
13427
13428 /* First, determine the context for the type we are looking
13429 up. */
13430 context = TYPE_CONTEXT (t);
13431 if (context && TYPE_P (context))
13432 {
13433 context = tsubst_aggr_type (context, args, complain,
13434 in_decl, /*entering_scope=*/1);
13435 /* If context is a nested class inside a class template,
13436 it may still need to be instantiated (c++/33959). */
13437 context = complete_type (context);
13438 }
13439
13440 /* Then, figure out what arguments are appropriate for the
13441 type we are trying to find. For example, given:
13442
13443 template <class T> struct S;
13444 template <class T, class U> void f(T, U) { S<U> su; }
13445
13446 and supposing that we are instantiating f<int, double>,
13447 then our ARGS will be {int, double}, but, when looking up
13448 S we only want {double}. */
13449 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13450 complain, in_decl);
13451 if (argvec == error_mark_node)
13452 r = error_mark_node;
13453 else if (cxx_dialect >= cxx17 && dependent_scope_p (context))
13454 {
13455 /* See maybe_dependent_member_ref. */
13456 tree name = TYPE_IDENTIFIER (t);
13457 tree fullname = name;
13458 if (instantiates_primary_template_p (t))
13459 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13460 INNERMOST_TEMPLATE_ARGS (argvec));
13461 return build_typename_type (context, name, fullname,
13462 typename_type);
13463 }
13464 else
13465 {
13466 r = lookup_template_class (t, argvec, in_decl, context,
13467 entering_scope, complain);
13468 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13469 }
13470
13471 return r;
13472 }
13473 else
13474 /* This is not a template type, so there's nothing to do. */
13475 return t;
13476
13477 default:
13478 return tsubst (t, args, complain, in_decl);
13479 }
13480 }
13481
13482 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13483
13484 /* Substitute into the default argument ARG (a default argument for
13485 FN), which has the indicated TYPE. */
13486
13487 tree
13488 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13489 tsubst_flags_t complain)
13490 {
13491 int errs = errorcount + sorrycount;
13492
13493 /* This can happen in invalid code. */
13494 if (TREE_CODE (arg) == DEFERRED_PARSE)
13495 return arg;
13496
13497 /* Shortcut {}. */
13498 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13499 && CONSTRUCTOR_NELTS (arg) == 0)
13500 return arg;
13501
13502 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13503 parm = chain_index (parmnum, parm);
13504 tree parmtype = TREE_TYPE (parm);
13505 if (DECL_BY_REFERENCE (parm))
13506 parmtype = TREE_TYPE (parmtype);
13507 if (parmtype == error_mark_node)
13508 return error_mark_node;
13509
13510 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13511
13512 tree *slot;
13513 if (defarg_inst && (slot = defarg_inst->get (parm)))
13514 return *slot;
13515
13516 /* This default argument came from a template. Instantiate the
13517 default argument here, not in tsubst. In the case of
13518 something like:
13519
13520 template <class T>
13521 struct S {
13522 static T t();
13523 void f(T = t());
13524 };
13525
13526 we must be careful to do name lookup in the scope of S<T>,
13527 rather than in the current class. */
13528 push_to_top_level ();
13529 push_access_scope (fn);
13530 push_deferring_access_checks (dk_no_deferred);
13531 start_lambda_scope (parm);
13532
13533 /* The default argument expression may cause implicitly defined
13534 member functions to be synthesized, which will result in garbage
13535 collection. We must treat this situation as if we were within
13536 the body of function so as to avoid collecting live data on the
13537 stack. */
13538 ++function_depth;
13539 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13540 complain, NULL_TREE,
13541 /*integral_constant_expression_p=*/false);
13542 --function_depth;
13543
13544 finish_lambda_scope ();
13545
13546 /* Make sure the default argument is reasonable. */
13547 arg = check_default_argument (type, arg, complain);
13548
13549 if (errorcount+sorrycount > errs
13550 && (complain & tf_warning_or_error))
13551 inform (input_location,
13552 " when instantiating default argument for call to %qD", fn);
13553
13554 pop_deferring_access_checks ();
13555 pop_access_scope (fn);
13556 pop_from_top_level ();
13557
13558 if (arg != error_mark_node && !cp_unevaluated_operand)
13559 {
13560 if (!defarg_inst)
13561 defarg_inst = decl_tree_cache_map::create_ggc (37);
13562 defarg_inst->put (parm, arg);
13563 }
13564
13565 return arg;
13566 }
13567
13568 /* Substitute into all the default arguments for FN. */
13569
13570 static void
13571 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13572 {
13573 tree arg;
13574 tree tmpl_args;
13575
13576 tmpl_args = DECL_TI_ARGS (fn);
13577
13578 /* If this function is not yet instantiated, we certainly don't need
13579 its default arguments. */
13580 if (uses_template_parms (tmpl_args))
13581 return;
13582 /* Don't do this again for clones. */
13583 if (DECL_CLONED_FUNCTION_P (fn))
13584 return;
13585
13586 int i = 0;
13587 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13588 arg;
13589 arg = TREE_CHAIN (arg), ++i)
13590 if (TREE_PURPOSE (arg))
13591 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13592 TREE_VALUE (arg),
13593 TREE_PURPOSE (arg),
13594 complain);
13595 }
13596
13597 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13598 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13599
13600 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13601
13602 void
13603 store_explicit_specifier (tree v, tree t)
13604 {
13605 if (!explicit_specifier_map)
13606 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13607 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13608 explicit_specifier_map->put (v, t);
13609 }
13610
13611 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13612
13613 static tree
13614 lookup_explicit_specifier (tree v)
13615 {
13616 return *explicit_specifier_map->get (v);
13617 }
13618
13619 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13620 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13621 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13622 identical to T. */
13623
13624 static tree
13625 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13626 tree raises, tsubst_flags_t complain)
13627 {
13628 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13629
13630 tree new_type;
13631 if (TREE_CODE (t) == FUNCTION_TYPE)
13632 {
13633 new_type = build_function_type (return_type, arg_types);
13634 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13635 }
13636 else
13637 {
13638 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13639 /* Don't pick up extra function qualifiers from the basetype. */
13640 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13641 if (! MAYBE_CLASS_TYPE_P (r))
13642 {
13643 /* [temp.deduct]
13644
13645 Type deduction may fail for any of the following
13646 reasons:
13647
13648 -- Attempting to create "pointer to member of T" when T
13649 is not a class type. */
13650 if (complain & tf_error)
13651 error ("creating pointer to member function of non-class type %qT",
13652 r);
13653 return error_mark_node;
13654 }
13655
13656 new_type = build_method_type_directly (r, return_type,
13657 TREE_CHAIN (arg_types));
13658 }
13659 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13660
13661 cp_ref_qualifier rqual = type_memfn_rqual (t);
13662 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13663 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13664 }
13665
13666 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13667 each of its formal parameters. If there is a disagreement then rebuild
13668 DECL's function type according to its formal parameter types, as part of a
13669 resolution for Core issues 1001/1322. */
13670
13671 static void
13672 maybe_rebuild_function_decl_type (tree decl)
13673 {
13674 bool function_type_needs_rebuilding = false;
13675 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13676 {
13677 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13678 while (parm_type_list && parm_type_list != void_list_node)
13679 {
13680 tree parm_type = TREE_VALUE (parm_type_list);
13681 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13682 if (!same_type_p (parm_type, formal_parm_type_unqual))
13683 {
13684 function_type_needs_rebuilding = true;
13685 break;
13686 }
13687
13688 parm_list = DECL_CHAIN (parm_list);
13689 parm_type_list = TREE_CHAIN (parm_type_list);
13690 }
13691 }
13692
13693 if (!function_type_needs_rebuilding)
13694 return;
13695
13696 const tree fntype = TREE_TYPE (decl);
13697 tree parm_list = DECL_ARGUMENTS (decl);
13698 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13699 tree new_parm_type_list = NULL_TREE;
13700 tree *q = &new_parm_type_list;
13701 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13702 {
13703 *q = copy_node (old_parm_type_list);
13704 parm_list = DECL_CHAIN (parm_list);
13705 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13706 q = &TREE_CHAIN (*q);
13707 }
13708 while (old_parm_type_list && old_parm_type_list != void_list_node)
13709 {
13710 *q = copy_node (old_parm_type_list);
13711 tree *new_parm_type = &TREE_VALUE (*q);
13712 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13713 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13714 *new_parm_type = formal_parm_type_unqual;
13715
13716 parm_list = DECL_CHAIN (parm_list);
13717 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13718 q = &TREE_CHAIN (*q);
13719 }
13720 if (old_parm_type_list == void_list_node)
13721 *q = void_list_node;
13722
13723 TREE_TYPE (decl)
13724 = rebuild_function_or_method_type (fntype,
13725 TREE_TYPE (fntype), new_parm_type_list,
13726 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13727 }
13728
13729 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13730
13731 static tree
13732 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13733 tree lambda_fntype)
13734 {
13735 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13736 hashval_t hash = 0;
13737 tree in_decl = t;
13738
13739 /* Nobody should be tsubst'ing into non-template functions. */
13740 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13741 || DECL_LOCAL_DECL_P (t));
13742
13743 if (DECL_LOCAL_DECL_P (t))
13744 {
13745 if (tree spec = retrieve_local_specialization (t))
13746 return spec;
13747 }
13748 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13749 {
13750 /* If T is not dependent, just return it. */
13751 if (!uses_template_parms (DECL_TI_ARGS (t))
13752 && !LAMBDA_FUNCTION_P (t))
13753 return t;
13754
13755 /* Calculate the most general template of which R is a
13756 specialization. */
13757 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13758
13759 /* We're substituting a lambda function under tsubst_lambda_expr but not
13760 directly from it; find the matching function we're already inside.
13761 But don't do this if T is a generic lambda with a single level of
13762 template parms, as in that case we're doing a normal instantiation. */
13763 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13764 && (!generic_lambda_fn_p (t)
13765 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13766 return enclosing_instantiation_of (t);
13767
13768 /* Calculate the complete set of arguments used to
13769 specialize R. */
13770 argvec = tsubst_template_args (DECL_TI_ARGS
13771 (DECL_TEMPLATE_RESULT
13772 (DECL_TI_TEMPLATE (t))),
13773 args, complain, in_decl);
13774 if (argvec == error_mark_node)
13775 return error_mark_node;
13776
13777 /* Check to see if we already have this specialization. */
13778 if (!lambda_fntype)
13779 {
13780 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13781 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13782 return spec;
13783 }
13784
13785 /* We can see more levels of arguments than parameters if
13786 there was a specialization of a member template, like
13787 this:
13788
13789 template <class T> struct S { template <class U> void f(); }
13790 template <> template <class U> void S<int>::f(U);
13791
13792 Here, we'll be substituting into the specialization,
13793 because that's where we can find the code we actually
13794 want to generate, but we'll have enough arguments for
13795 the most general template.
13796
13797 We also deal with the peculiar case:
13798
13799 template <class T> struct S {
13800 template <class U> friend void f();
13801 };
13802 template <class U> void f() {}
13803 template S<int>;
13804 template void f<double>();
13805
13806 Here, the ARGS for the instantiation of will be {int,
13807 double}. But, we only need as many ARGS as there are
13808 levels of template parameters in CODE_PATTERN. We are
13809 careful not to get fooled into reducing the ARGS in
13810 situations like:
13811
13812 template <class T> struct S { template <class U> void f(U); }
13813 template <class T> template <> void S<T>::f(int) {}
13814
13815 which we can spot because the pattern will be a
13816 specialization in this case. */
13817 int args_depth = TMPL_ARGS_DEPTH (args);
13818 int parms_depth =
13819 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13820
13821 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13822 args = get_innermost_template_args (args, parms_depth);
13823 }
13824 else
13825 {
13826 /* This special case arises when we have something like this:
13827
13828 template <class T> struct S {
13829 friend void f<int>(int, double);
13830 };
13831
13832 Here, the DECL_TI_TEMPLATE for the friend declaration
13833 will be an IDENTIFIER_NODE. We are being called from
13834 tsubst_friend_function, and we want only to create a
13835 new decl (R) with appropriate types so that we can call
13836 determine_specialization. */
13837 gen_tmpl = NULL_TREE;
13838 argvec = NULL_TREE;
13839 }
13840
13841 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13842 : NULL_TREE);
13843 tree ctx = closure ? closure : DECL_CONTEXT (t);
13844 bool member = ctx && TYPE_P (ctx);
13845
13846 if (member && !closure)
13847 ctx = tsubst_aggr_type (ctx, args,
13848 complain, t, /*entering_scope=*/1);
13849
13850 tree type = (lambda_fntype ? lambda_fntype
13851 : tsubst (TREE_TYPE (t), args,
13852 complain | tf_fndecl_type, in_decl));
13853 if (type == error_mark_node)
13854 return error_mark_node;
13855
13856 /* If we hit excessive deduction depth, the type is bogus even if
13857 it isn't error_mark_node, so don't build a decl. */
13858 if (excessive_deduction_depth)
13859 return error_mark_node;
13860
13861 /* We do NOT check for matching decls pushed separately at this
13862 point, as they may not represent instantiations of this
13863 template, and in any case are considered separate under the
13864 discrete model. */
13865 tree r = copy_decl (t);
13866 DECL_USE_TEMPLATE (r) = 0;
13867 TREE_TYPE (r) = type;
13868 /* Clear out the mangled name and RTL for the instantiation. */
13869 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13870 SET_DECL_RTL (r, NULL);
13871 /* Leave DECL_INITIAL set on deleted instantiations. */
13872 if (!DECL_DELETED_FN (r))
13873 DECL_INITIAL (r) = NULL_TREE;
13874 DECL_CONTEXT (r) = ctx;
13875
13876 /* Handle explicit(dependent-expr). */
13877 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13878 {
13879 tree spec = lookup_explicit_specifier (t);
13880 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13881 /*function_p=*/false,
13882 /*i_c_e_p=*/true);
13883 spec = build_explicit_specifier (spec, complain);
13884 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13885 }
13886
13887 /* OpenMP UDRs have the only argument a reference to the declared
13888 type. We want to diagnose if the declared type is a reference,
13889 which is invalid, but as references to references are usually
13890 quietly merged, diagnose it here. */
13891 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13892 {
13893 tree argtype
13894 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13895 argtype = tsubst (argtype, args, complain, in_decl);
13896 if (TYPE_REF_P (argtype))
13897 error_at (DECL_SOURCE_LOCATION (t),
13898 "reference type %qT in "
13899 "%<#pragma omp declare reduction%>", argtype);
13900 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13901 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13902 argtype);
13903 }
13904
13905 if (member && DECL_CONV_FN_P (r))
13906 /* Type-conversion operator. Reconstruct the name, in
13907 case it's the name of one of the template's parameters. */
13908 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13909
13910 tree parms = DECL_ARGUMENTS (t);
13911 if (closure)
13912 parms = DECL_CHAIN (parms);
13913 parms = tsubst (parms, args, complain, t);
13914 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13915 DECL_CONTEXT (parm) = r;
13916 if (closure)
13917 {
13918 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13919 DECL_NAME (tparm) = closure_identifier;
13920 DECL_CHAIN (tparm) = parms;
13921 parms = tparm;
13922 }
13923 DECL_ARGUMENTS (r) = parms;
13924 DECL_RESULT (r) = NULL_TREE;
13925
13926 maybe_rebuild_function_decl_type (r);
13927
13928 TREE_STATIC (r) = 0;
13929 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13930 DECL_EXTERNAL (r) = 1;
13931 /* If this is an instantiation of a function with internal
13932 linkage, we already know what object file linkage will be
13933 assigned to the instantiation. */
13934 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13935 DECL_DEFER_OUTPUT (r) = 0;
13936 DECL_CHAIN (r) = NULL_TREE;
13937 DECL_PENDING_INLINE_INFO (r) = 0;
13938 DECL_PENDING_INLINE_P (r) = 0;
13939 DECL_SAVED_TREE (r) = NULL_TREE;
13940 DECL_STRUCT_FUNCTION (r) = NULL;
13941 TREE_USED (r) = 0;
13942 /* We'll re-clone as appropriate in instantiate_template. */
13943 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13944
13945 /* If we aren't complaining now, return on error before we register
13946 the specialization so that we'll complain eventually. */
13947 if ((complain & tf_error) == 0
13948 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13949 && !grok_op_properties (r, /*complain=*/false))
13950 return error_mark_node;
13951
13952 /* Associate the constraints directly with the instantiation. We
13953 don't substitute through the constraints; that's only done when
13954 they are checked. */
13955 if (tree ci = get_constraints (t))
13956 /* Unless we're regenerating a lambda, in which case we'll set the
13957 lambda's constraints in tsubst_lambda_expr. */
13958 if (!lambda_fntype)
13959 set_constraints (r, ci);
13960
13961 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13962 SET_DECL_FRIEND_CONTEXT (r,
13963 tsubst (DECL_FRIEND_CONTEXT (t),
13964 args, complain, in_decl));
13965
13966 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13967 this in the special friend case mentioned above where
13968 GEN_TMPL is NULL. */
13969 if (gen_tmpl && !closure)
13970 {
13971 DECL_TEMPLATE_INFO (r)
13972 = build_template_info (gen_tmpl, argvec);
13973 SET_DECL_IMPLICIT_INSTANTIATION (r);
13974
13975 tree new_r
13976 = register_specialization (r, gen_tmpl, argvec, false, hash);
13977 if (new_r != r)
13978 /* We instantiated this while substituting into
13979 the type earlier (template/friend54.C). */
13980 return new_r;
13981
13982 /* We're not supposed to instantiate default arguments
13983 until they are called, for a template. But, for a
13984 declaration like:
13985
13986 template <class T> void f ()
13987 { extern void g(int i = T()); }
13988
13989 we should do the substitution when the template is
13990 instantiated. We handle the member function case in
13991 instantiate_class_template since the default arguments
13992 might refer to other members of the class. */
13993 if (!member
13994 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13995 && !uses_template_parms (argvec))
13996 tsubst_default_arguments (r, complain);
13997 }
13998 else if (DECL_LOCAL_DECL_P (r))
13999 {
14000 if (!cp_unevaluated_operand)
14001 register_local_specialization (r, t);
14002 }
14003 else
14004 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14005
14006 /* Copy the list of befriending classes. */
14007 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14008 *friends;
14009 friends = &TREE_CHAIN (*friends))
14010 {
14011 *friends = copy_node (*friends);
14012 TREE_VALUE (*friends)
14013 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14014 }
14015
14016 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14017 {
14018 maybe_retrofit_in_chrg (r);
14019 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14020 return error_mark_node;
14021 /* If this is an instantiation of a member template, clone it.
14022 If it isn't, that'll be handled by
14023 clone_constructors_and_destructors. */
14024 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14025 clone_cdtor (r, /*update_methods=*/false);
14026 }
14027 else if ((complain & tf_error) != 0
14028 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14029 && !grok_op_properties (r, /*complain=*/true))
14030 return error_mark_node;
14031
14032 /* Possibly limit visibility based on template args. */
14033 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14034 if (DECL_VISIBILITY_SPECIFIED (t))
14035 {
14036 DECL_VISIBILITY_SPECIFIED (r) = 0;
14037 DECL_ATTRIBUTES (r)
14038 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14039 }
14040 determine_visibility (r);
14041 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14042 && !processing_template_decl)
14043 defaulted_late_check (r);
14044
14045 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14046 args, complain, in_decl);
14047 if (flag_openmp)
14048 if (tree attr = lookup_attribute ("omp declare variant base",
14049 DECL_ATTRIBUTES (r)))
14050 omp_declare_variant_finalize (r, attr);
14051
14052 return r;
14053 }
14054
14055 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14056
14057 static tree
14058 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14059 tree lambda_fntype)
14060 {
14061 /* We can get here when processing a member function template,
14062 member class template, or template template parameter. */
14063 tree decl = DECL_TEMPLATE_RESULT (t);
14064 tree in_decl = t;
14065 tree spec;
14066 tree tmpl_args;
14067 tree full_args;
14068 tree r;
14069 hashval_t hash = 0;
14070
14071 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14072 {
14073 /* Template template parameter is treated here. */
14074 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14075 if (new_type == error_mark_node)
14076 r = error_mark_node;
14077 /* If we get a real template back, return it. This can happen in
14078 the context of most_specialized_partial_spec. */
14079 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14080 r = new_type;
14081 else
14082 /* The new TEMPLATE_DECL was built in
14083 reduce_template_parm_level. */
14084 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14085 return r;
14086 }
14087
14088 if (!lambda_fntype)
14089 {
14090 /* We might already have an instance of this template.
14091 The ARGS are for the surrounding class type, so the
14092 full args contain the tsubst'd args for the context,
14093 plus the innermost args from the template decl. */
14094 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14095 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14096 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14097 /* Because this is a template, the arguments will still be
14098 dependent, even after substitution. If
14099 PROCESSING_TEMPLATE_DECL is not set, the dependency
14100 predicates will short-circuit. */
14101 ++processing_template_decl;
14102 full_args = tsubst_template_args (tmpl_args, args,
14103 complain, in_decl);
14104 --processing_template_decl;
14105 if (full_args == error_mark_node)
14106 return error_mark_node;
14107
14108 /* If this is a default template template argument,
14109 tsubst might not have changed anything. */
14110 if (full_args == tmpl_args)
14111 return t;
14112
14113 hash = hash_tmpl_and_args (t, full_args);
14114 spec = retrieve_specialization (t, full_args, hash);
14115 if (spec != NULL_TREE)
14116 {
14117 if (TYPE_P (spec))
14118 /* Type partial instantiations are stored as the type by
14119 lookup_template_class_1, not here as the template. */
14120 spec = CLASSTYPE_TI_TEMPLATE (spec);
14121 return spec;
14122 }
14123 }
14124
14125 /* Make a new template decl. It will be similar to the
14126 original, but will record the current template arguments.
14127 We also create a new function declaration, which is just
14128 like the old one, but points to this new template, rather
14129 than the old one. */
14130 r = copy_decl (t);
14131 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14132 DECL_CHAIN (r) = NULL_TREE;
14133
14134 // Build new template info linking to the original template decl.
14135 if (!lambda_fntype)
14136 {
14137 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14138 SET_DECL_IMPLICIT_INSTANTIATION (r);
14139 }
14140 else
14141 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14142
14143 /* The template parameters for this new template are all the
14144 template parameters for the old template, except the
14145 outermost level of parameters. */
14146 DECL_TEMPLATE_PARMS (r)
14147 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14148 complain);
14149
14150 bool class_p = false;
14151 tree inner = decl;
14152 ++processing_template_decl;
14153 if (TREE_CODE (inner) == FUNCTION_DECL)
14154 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14155 else
14156 {
14157 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14158 {
14159 class_p = true;
14160 inner = TREE_TYPE (inner);
14161 }
14162 inner = tsubst (inner, args, complain, in_decl);
14163 }
14164 --processing_template_decl;
14165 if (inner == error_mark_node)
14166 return error_mark_node;
14167
14168 if (class_p)
14169 {
14170 /* For a partial specialization, we need to keep pointing to
14171 the primary template. */
14172 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14173 CLASSTYPE_TI_TEMPLATE (inner) = r;
14174
14175 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14176 inner = TYPE_MAIN_DECL (inner);
14177 }
14178 else if (lambda_fntype)
14179 {
14180 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14181 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14182 }
14183 else
14184 {
14185 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14186 DECL_TI_TEMPLATE (inner) = r;
14187 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14188 }
14189
14190 DECL_TEMPLATE_RESULT (r) = inner;
14191 TREE_TYPE (r) = TREE_TYPE (inner);
14192 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14193
14194 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14195 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14196
14197 if (PRIMARY_TEMPLATE_P (t))
14198 DECL_PRIMARY_TEMPLATE (r) = r;
14199
14200 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14201 && !lambda_fntype)
14202 /* Record this non-type partial instantiation. */
14203 register_specialization (r, t,
14204 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14205 false, hash);
14206
14207 return r;
14208 }
14209
14210 /* True if FN is the op() for a lambda in an uninstantiated template. */
14211
14212 bool
14213 lambda_fn_in_template_p (tree fn)
14214 {
14215 if (!fn || !LAMBDA_FUNCTION_P (fn))
14216 return false;
14217 tree closure = DECL_CONTEXT (fn);
14218 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14219 }
14220
14221 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14222 which the above is true. */
14223
14224 bool
14225 instantiated_lambda_fn_p (tree fn)
14226 {
14227 if (!fn || !LAMBDA_FUNCTION_P (fn))
14228 return false;
14229 tree closure = DECL_CONTEXT (fn);
14230 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14231 return LAMBDA_EXPR_INSTANTIATED (lam);
14232 }
14233
14234 /* We're instantiating a variable from template function TCTX. Return the
14235 corresponding current enclosing scope. This gets complicated because lambda
14236 functions in templates are regenerated rather than instantiated, but generic
14237 lambda functions are subsequently instantiated. */
14238
14239 static tree
14240 enclosing_instantiation_of (tree otctx)
14241 {
14242 tree tctx = otctx;
14243 tree fn = current_function_decl;
14244 int lambda_count = 0;
14245
14246 for (; tctx && (lambda_fn_in_template_p (tctx)
14247 || instantiated_lambda_fn_p (tctx));
14248 tctx = decl_function_context (tctx))
14249 ++lambda_count;
14250 for (; fn; fn = decl_function_context (fn))
14251 {
14252 tree ofn = fn;
14253 int flambda_count = 0;
14254 for (; fn && instantiated_lambda_fn_p (fn);
14255 fn = decl_function_context (fn))
14256 ++flambda_count;
14257 if ((fn && DECL_TEMPLATE_INFO (fn))
14258 ? most_general_template (fn) != most_general_template (tctx)
14259 : fn != tctx)
14260 continue;
14261 if (flambda_count != lambda_count)
14262 {
14263 gcc_assert (flambda_count > lambda_count);
14264 for (; flambda_count > lambda_count; --flambda_count)
14265 ofn = decl_function_context (ofn);
14266 }
14267 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14268 || DECL_CONV_FN_P (ofn));
14269 return ofn;
14270 }
14271 gcc_unreachable ();
14272 }
14273
14274 /* Substitute the ARGS into the T, which is a _DECL. Return the
14275 result of the substitution. Issue error and warning messages under
14276 control of COMPLAIN. */
14277
14278 static tree
14279 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14280 {
14281 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14282 location_t saved_loc;
14283 tree r = NULL_TREE;
14284 tree in_decl = t;
14285 hashval_t hash = 0;
14286
14287 /* Set the filename and linenumber to improve error-reporting. */
14288 saved_loc = input_location;
14289 input_location = DECL_SOURCE_LOCATION (t);
14290
14291 switch (TREE_CODE (t))
14292 {
14293 case TEMPLATE_DECL:
14294 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14295 break;
14296
14297 case FUNCTION_DECL:
14298 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14299 break;
14300
14301 case PARM_DECL:
14302 {
14303 tree type = NULL_TREE;
14304 int i, len = 1;
14305 tree expanded_types = NULL_TREE;
14306 tree prev_r = NULL_TREE;
14307 tree first_r = NULL_TREE;
14308
14309 if (DECL_PACK_P (t))
14310 {
14311 /* If there is a local specialization that isn't a
14312 parameter pack, it means that we're doing a "simple"
14313 substitution from inside tsubst_pack_expansion. Just
14314 return the local specialization (which will be a single
14315 parm). */
14316 tree spec = retrieve_local_specialization (t);
14317 if (spec
14318 && TREE_CODE (spec) == PARM_DECL
14319 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14320 RETURN (spec);
14321
14322 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14323 the parameters in this function parameter pack. */
14324 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14325 complain, in_decl);
14326 if (TREE_CODE (expanded_types) == TREE_VEC)
14327 {
14328 len = TREE_VEC_LENGTH (expanded_types);
14329
14330 /* Zero-length parameter packs are boring. Just substitute
14331 into the chain. */
14332 if (len == 0 && !cp_unevaluated_operand)
14333 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14334 TREE_CHAIN (t)));
14335 }
14336 else
14337 {
14338 /* All we did was update the type. Make a note of that. */
14339 type = expanded_types;
14340 expanded_types = NULL_TREE;
14341 }
14342 }
14343
14344 /* Loop through all of the parameters we'll build. When T is
14345 a function parameter pack, LEN is the number of expanded
14346 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14347 r = NULL_TREE;
14348 for (i = 0; i < len; ++i)
14349 {
14350 prev_r = r;
14351 r = copy_node (t);
14352 if (DECL_TEMPLATE_PARM_P (t))
14353 SET_DECL_TEMPLATE_PARM_P (r);
14354
14355 if (expanded_types)
14356 /* We're on the Ith parameter of the function parameter
14357 pack. */
14358 {
14359 /* Get the Ith type. */
14360 type = TREE_VEC_ELT (expanded_types, i);
14361
14362 /* Rename the parameter to include the index. */
14363 DECL_NAME (r)
14364 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14365 }
14366 else if (!type)
14367 /* We're dealing with a normal parameter. */
14368 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14369
14370 type = type_decays_to (type);
14371 TREE_TYPE (r) = type;
14372 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14373
14374 if (DECL_INITIAL (r))
14375 {
14376 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14377 DECL_INITIAL (r) = TREE_TYPE (r);
14378 else
14379 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14380 complain, in_decl);
14381 }
14382
14383 DECL_CONTEXT (r) = NULL_TREE;
14384
14385 if (!DECL_TEMPLATE_PARM_P (r))
14386 DECL_ARG_TYPE (r) = type_passed_as (type);
14387
14388 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14389 args, complain, in_decl);
14390
14391 /* Keep track of the first new parameter we
14392 generate. That's what will be returned to the
14393 caller. */
14394 if (!first_r)
14395 first_r = r;
14396
14397 /* Build a proper chain of parameters when substituting
14398 into a function parameter pack. */
14399 if (prev_r)
14400 DECL_CHAIN (prev_r) = r;
14401 }
14402
14403 /* If cp_unevaluated_operand is set, we're just looking for a
14404 single dummy parameter, so don't keep going. */
14405 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14406 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14407 complain, DECL_CHAIN (t));
14408
14409 /* FIRST_R contains the start of the chain we've built. */
14410 r = first_r;
14411 }
14412 break;
14413
14414 case FIELD_DECL:
14415 {
14416 tree type = NULL_TREE;
14417 tree vec = NULL_TREE;
14418 tree expanded_types = NULL_TREE;
14419 int len = 1;
14420
14421 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14422 {
14423 /* This field is a lambda capture pack. Return a TREE_VEC of
14424 the expanded fields to instantiate_class_template_1. */
14425 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14426 complain, in_decl);
14427 if (TREE_CODE (expanded_types) == TREE_VEC)
14428 {
14429 len = TREE_VEC_LENGTH (expanded_types);
14430 vec = make_tree_vec (len);
14431 }
14432 else
14433 {
14434 /* All we did was update the type. Make a note of that. */
14435 type = expanded_types;
14436 expanded_types = NULL_TREE;
14437 }
14438 }
14439
14440 for (int i = 0; i < len; ++i)
14441 {
14442 r = copy_decl (t);
14443 if (expanded_types)
14444 {
14445 type = TREE_VEC_ELT (expanded_types, i);
14446 DECL_NAME (r)
14447 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14448 }
14449 else if (!type)
14450 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14451
14452 if (type == error_mark_node)
14453 RETURN (error_mark_node);
14454 TREE_TYPE (r) = type;
14455 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14456
14457 if (DECL_C_BIT_FIELD (r))
14458 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14459 number of bits. */
14460 DECL_BIT_FIELD_REPRESENTATIVE (r)
14461 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14462 complain, in_decl,
14463 /*integral_constant_expression_p=*/true);
14464 if (DECL_INITIAL (t))
14465 {
14466 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14467 NSDMI in perform_member_init. Still set DECL_INITIAL
14468 so that we know there is one. */
14469 DECL_INITIAL (r) = void_node;
14470 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14471 retrofit_lang_decl (r);
14472 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14473 }
14474 /* We don't have to set DECL_CONTEXT here; it is set by
14475 finish_member_declaration. */
14476 DECL_CHAIN (r) = NULL_TREE;
14477
14478 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14479 args, complain, in_decl);
14480
14481 if (vec)
14482 TREE_VEC_ELT (vec, i) = r;
14483 }
14484
14485 if (vec)
14486 r = vec;
14487 }
14488 break;
14489
14490 case USING_DECL:
14491 /* We reach here only for member using decls. We also need to check
14492 uses_template_parms because DECL_DEPENDENT_P is not set for a
14493 using-declaration that designates a member of the current
14494 instantiation (c++/53549). */
14495 if (DECL_DEPENDENT_P (t)
14496 || uses_template_parms (USING_DECL_SCOPE (t)))
14497 {
14498 tree scope = USING_DECL_SCOPE (t);
14499 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14500 if (PACK_EXPANSION_P (scope))
14501 {
14502 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14503 int len = TREE_VEC_LENGTH (vec);
14504 r = make_tree_vec (len);
14505 for (int i = 0; i < len; ++i)
14506 {
14507 tree escope = TREE_VEC_ELT (vec, i);
14508 tree elt = do_class_using_decl (escope, name);
14509 if (!elt)
14510 {
14511 r = error_mark_node;
14512 break;
14513 }
14514 else
14515 {
14516 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14517 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14518 }
14519 TREE_VEC_ELT (r, i) = elt;
14520 }
14521 }
14522 else
14523 {
14524 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14525 complain, in_decl);
14526 r = do_class_using_decl (inst_scope, name);
14527 if (!r)
14528 r = error_mark_node;
14529 else
14530 {
14531 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14532 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14533 }
14534 }
14535 }
14536 else
14537 {
14538 r = copy_node (t);
14539 DECL_CHAIN (r) = NULL_TREE;
14540 }
14541 break;
14542
14543 case TYPE_DECL:
14544 case VAR_DECL:
14545 {
14546 tree argvec = NULL_TREE;
14547 tree gen_tmpl = NULL_TREE;
14548 tree tmpl = NULL_TREE;
14549 tree type = NULL_TREE;
14550
14551 if (TREE_TYPE (t) == error_mark_node)
14552 RETURN (error_mark_node);
14553
14554 if (TREE_CODE (t) == TYPE_DECL
14555 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14556 {
14557 /* If this is the canonical decl, we don't have to
14558 mess with instantiations, and often we can't (for
14559 typename, template type parms and such). Note that
14560 TYPE_NAME is not correct for the above test if
14561 we've copied the type for a typedef. */
14562 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14563 if (type == error_mark_node)
14564 RETURN (error_mark_node);
14565 r = TYPE_NAME (type);
14566 break;
14567 }
14568
14569 /* Check to see if we already have the specialization we
14570 need. */
14571 tree spec = NULL_TREE;
14572 bool local_p = false;
14573 tree ctx = DECL_CONTEXT (t);
14574 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14575 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14576 {
14577 local_p = false;
14578 if (DECL_CLASS_SCOPE_P (t))
14579 {
14580 ctx = tsubst_aggr_type (ctx, args,
14581 complain,
14582 in_decl, /*entering_scope=*/1);
14583 /* If CTX is unchanged, then T is in fact the
14584 specialization we want. That situation occurs when
14585 referencing a static data member within in its own
14586 class. We can use pointer equality, rather than
14587 same_type_p, because DECL_CONTEXT is always
14588 canonical... */
14589 if (ctx == DECL_CONTEXT (t)
14590 /* ... unless T is a member template; in which
14591 case our caller can be willing to create a
14592 specialization of that template represented
14593 by T. */
14594 && !(DECL_TI_TEMPLATE (t)
14595 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14596 spec = t;
14597 }
14598
14599 if (!spec)
14600 {
14601 tmpl = DECL_TI_TEMPLATE (t);
14602 gen_tmpl = most_general_template (tmpl);
14603 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14604 if (argvec != error_mark_node)
14605 argvec = (coerce_innermost_template_parms
14606 (DECL_TEMPLATE_PARMS (gen_tmpl),
14607 argvec, t, complain,
14608 /*all*/true, /*defarg*/true));
14609 if (argvec == error_mark_node)
14610 RETURN (error_mark_node);
14611 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14612 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14613 }
14614 }
14615 else
14616 {
14617 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14618 /* Subsequent calls to pushdecl will fill this in. */
14619 ctx = NULL_TREE;
14620 /* A local variable. */
14621 local_p = true;
14622 /* Unless this is a reference to a static variable from an
14623 enclosing function, in which case we need to fill it in now. */
14624 if (TREE_STATIC (t))
14625 {
14626 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14627 if (fn != current_function_decl)
14628 ctx = fn;
14629 }
14630 spec = retrieve_local_specialization (t);
14631 }
14632 /* If we already have the specialization we need, there is
14633 nothing more to do. */
14634 if (spec)
14635 {
14636 r = spec;
14637 break;
14638 }
14639
14640 /* Create a new node for the specialization we need. */
14641 if (type == NULL_TREE)
14642 {
14643 if (is_typedef_decl (t))
14644 type = DECL_ORIGINAL_TYPE (t);
14645 else
14646 type = TREE_TYPE (t);
14647 if (VAR_P (t)
14648 && VAR_HAD_UNKNOWN_BOUND (t)
14649 && type != error_mark_node)
14650 type = strip_array_domain (type);
14651 tree sub_args = args;
14652 if (tree auto_node = type_uses_auto (type))
14653 {
14654 /* Mask off any template args past the variable's context so we
14655 don't replace the auto with an unrelated argument. */
14656 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14657 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14658 if (extra > 0)
14659 /* This should never happen with the new lambda instantiation
14660 model, but keep the handling just in case. */
14661 gcc_assert (!CHECKING_P),
14662 sub_args = strip_innermost_template_args (args, extra);
14663 }
14664 type = tsubst (type, sub_args, complain, in_decl);
14665 /* Substituting the type might have recursively instantiated this
14666 same alias (c++/86171). */
14667 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14668 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14669 {
14670 r = spec;
14671 break;
14672 }
14673 }
14674 r = copy_decl (t);
14675 if (VAR_P (r))
14676 {
14677 DECL_INITIALIZED_P (r) = 0;
14678 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14679 if (type == error_mark_node)
14680 RETURN (error_mark_node);
14681 if (TREE_CODE (type) == FUNCTION_TYPE)
14682 {
14683 /* It may seem that this case cannot occur, since:
14684
14685 typedef void f();
14686 void g() { f x; }
14687
14688 declares a function, not a variable. However:
14689
14690 typedef void f();
14691 template <typename T> void g() { T t; }
14692 template void g<f>();
14693
14694 is an attempt to declare a variable with function
14695 type. */
14696 error ("variable %qD has function type",
14697 /* R is not yet sufficiently initialized, so we
14698 just use its name. */
14699 DECL_NAME (r));
14700 RETURN (error_mark_node);
14701 }
14702 type = complete_type (type);
14703 /* Wait until cp_finish_decl to set this again, to handle
14704 circular dependency (template/instantiate6.C). */
14705 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14706 type = check_var_type (DECL_NAME (r), type,
14707 DECL_SOURCE_LOCATION (r));
14708 if (DECL_HAS_VALUE_EXPR_P (t))
14709 {
14710 tree ve = DECL_VALUE_EXPR (t);
14711 /* If the DECL_VALUE_EXPR is converted to the declared type,
14712 preserve the identity so that gimplify_type_sizes works. */
14713 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14714 if (nop)
14715 ve = TREE_OPERAND (ve, 0);
14716 ve = tsubst_expr (ve, args, complain, in_decl,
14717 /*constant_expression_p=*/false);
14718 if (REFERENCE_REF_P (ve))
14719 {
14720 gcc_assert (TYPE_REF_P (type));
14721 ve = TREE_OPERAND (ve, 0);
14722 }
14723 if (nop)
14724 ve = build_nop (type, ve);
14725 else if (DECL_LANG_SPECIFIC (t)
14726 && DECL_OMP_PRIVATIZED_MEMBER (t)
14727 && TREE_CODE (ve) == COMPONENT_REF
14728 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14729 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14730 type = TREE_TYPE (ve);
14731 else
14732 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14733 == TYPE_MAIN_VARIANT (type));
14734 SET_DECL_VALUE_EXPR (r, ve);
14735 }
14736 if (CP_DECL_THREAD_LOCAL_P (r)
14737 && !processing_template_decl)
14738 set_decl_tls_model (r, decl_default_tls_model (r));
14739 }
14740 else if (DECL_SELF_REFERENCE_P (t))
14741 SET_DECL_SELF_REFERENCE_P (r);
14742 TREE_TYPE (r) = type;
14743 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14744 DECL_CONTEXT (r) = ctx;
14745 /* Clear out the mangled name and RTL for the instantiation. */
14746 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14747 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14748 SET_DECL_RTL (r, NULL);
14749 /* The initializer must not be expanded until it is required;
14750 see [temp.inst]. */
14751 DECL_INITIAL (r) = NULL_TREE;
14752 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14753 if (VAR_P (r))
14754 {
14755 if (DECL_LANG_SPECIFIC (r))
14756 SET_DECL_DEPENDENT_INIT_P (r, false);
14757
14758 SET_DECL_MODE (r, VOIDmode);
14759
14760 /* Possibly limit visibility based on template args. */
14761 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14762 if (DECL_VISIBILITY_SPECIFIED (t))
14763 {
14764 DECL_VISIBILITY_SPECIFIED (r) = 0;
14765 DECL_ATTRIBUTES (r)
14766 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14767 }
14768 determine_visibility (r);
14769 }
14770
14771 if (!local_p)
14772 {
14773 /* A static data member declaration is always marked
14774 external when it is declared in-class, even if an
14775 initializer is present. We mimic the non-template
14776 processing here. */
14777 DECL_EXTERNAL (r) = 1;
14778 if (DECL_NAMESPACE_SCOPE_P (t))
14779 DECL_NOT_REALLY_EXTERN (r) = 1;
14780
14781 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14782 SET_DECL_IMPLICIT_INSTANTIATION (r);
14783 if (!error_operand_p (r) || (complain & tf_error))
14784 register_specialization (r, gen_tmpl, argvec, false, hash);
14785 }
14786 else
14787 {
14788 if (DECL_LANG_SPECIFIC (r))
14789 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14790 if (!cp_unevaluated_operand)
14791 register_local_specialization (r, t);
14792 }
14793
14794 DECL_CHAIN (r) = NULL_TREE;
14795
14796 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14797 /*flags=*/0,
14798 args, complain, in_decl);
14799
14800 /* Preserve a typedef that names a type. */
14801 if (is_typedef_decl (r) && type != error_mark_node)
14802 {
14803 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14804 set_underlying_type (r);
14805 if (TYPE_DECL_ALIAS_P (r))
14806 /* An alias template specialization can be dependent
14807 even if its underlying type is not. */
14808 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14809 }
14810
14811 layout_decl (r, 0);
14812 }
14813 break;
14814
14815 default:
14816 gcc_unreachable ();
14817 }
14818 #undef RETURN
14819
14820 out:
14821 /* Restore the file and line information. */
14822 input_location = saved_loc;
14823
14824 return r;
14825 }
14826
14827 /* Substitute into the complete parameter type list PARMS. */
14828
14829 tree
14830 tsubst_function_parms (tree parms,
14831 tree args,
14832 tsubst_flags_t complain,
14833 tree in_decl)
14834 {
14835 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14836 }
14837
14838 /* Substitute into the ARG_TYPES of a function type.
14839 If END is a TREE_CHAIN, leave it and any following types
14840 un-substituted. */
14841
14842 static tree
14843 tsubst_arg_types (tree arg_types,
14844 tree args,
14845 tree end,
14846 tsubst_flags_t complain,
14847 tree in_decl)
14848 {
14849 tree remaining_arg_types;
14850 tree type = NULL_TREE;
14851 int i = 1;
14852 tree expanded_args = NULL_TREE;
14853 tree default_arg;
14854
14855 if (!arg_types || arg_types == void_list_node || arg_types == end)
14856 return arg_types;
14857
14858 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14859 args, end, complain, in_decl);
14860 if (remaining_arg_types == error_mark_node)
14861 return error_mark_node;
14862
14863 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14864 {
14865 /* For a pack expansion, perform substitution on the
14866 entire expression. Later on, we'll handle the arguments
14867 one-by-one. */
14868 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14869 args, complain, in_decl);
14870
14871 if (TREE_CODE (expanded_args) == TREE_VEC)
14872 /* So that we'll spin through the parameters, one by one. */
14873 i = TREE_VEC_LENGTH (expanded_args);
14874 else
14875 {
14876 /* We only partially substituted into the parameter
14877 pack. Our type is TYPE_PACK_EXPANSION. */
14878 type = expanded_args;
14879 expanded_args = NULL_TREE;
14880 }
14881 }
14882
14883 while (i > 0) {
14884 --i;
14885
14886 if (expanded_args)
14887 type = TREE_VEC_ELT (expanded_args, i);
14888 else if (!type)
14889 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14890
14891 if (type == error_mark_node)
14892 return error_mark_node;
14893 if (VOID_TYPE_P (type))
14894 {
14895 if (complain & tf_error)
14896 {
14897 error ("invalid parameter type %qT", type);
14898 if (in_decl)
14899 error ("in declaration %q+D", in_decl);
14900 }
14901 return error_mark_node;
14902 }
14903 /* DR 657. */
14904 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14905 return error_mark_node;
14906
14907 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14908 top-level qualifiers as required. */
14909 type = cv_unqualified (type_decays_to (type));
14910
14911 /* We do not substitute into default arguments here. The standard
14912 mandates that they be instantiated only when needed, which is
14913 done in build_over_call. */
14914 default_arg = TREE_PURPOSE (arg_types);
14915
14916 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14917 since the new op() won't have any associated template arguments for us
14918 to refer to later. */
14919 if (lambda_fn_in_template_p (in_decl))
14920 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14921 false/*fn*/, false/*constexpr*/);
14922
14923 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14924 {
14925 /* We've instantiated a template before its default arguments
14926 have been parsed. This can happen for a nested template
14927 class, and is not an error unless we require the default
14928 argument in a call of this function. */
14929 remaining_arg_types =
14930 tree_cons (default_arg, type, remaining_arg_types);
14931 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14932 remaining_arg_types);
14933 }
14934 else
14935 remaining_arg_types =
14936 hash_tree_cons (default_arg, type, remaining_arg_types);
14937 }
14938
14939 return remaining_arg_types;
14940 }
14941
14942 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14943 *not* handle the exception-specification for FNTYPE, because the
14944 initial substitution of explicitly provided template parameters
14945 during argument deduction forbids substitution into the
14946 exception-specification:
14947
14948 [temp.deduct]
14949
14950 All references in the function type of the function template to the
14951 corresponding template parameters are replaced by the specified tem-
14952 plate argument values. If a substitution in a template parameter or
14953 in the function type of the function template results in an invalid
14954 type, type deduction fails. [Note: The equivalent substitution in
14955 exception specifications is done only when the function is instanti-
14956 ated, at which point a program is ill-formed if the substitution
14957 results in an invalid type.] */
14958
14959 static tree
14960 tsubst_function_type (tree t,
14961 tree args,
14962 tsubst_flags_t complain,
14963 tree in_decl)
14964 {
14965 tree return_type;
14966 tree arg_types = NULL_TREE;
14967
14968 /* The TYPE_CONTEXT is not used for function/method types. */
14969 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14970
14971 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14972 failure. */
14973 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14974
14975 if (late_return_type_p)
14976 {
14977 /* Substitute the argument types. */
14978 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14979 complain, in_decl);
14980 if (arg_types == error_mark_node)
14981 return error_mark_node;
14982
14983 tree save_ccp = current_class_ptr;
14984 tree save_ccr = current_class_ref;
14985 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14986 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14987 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14988 if (do_inject)
14989 {
14990 /* DR 1207: 'this' is in scope in the trailing return type. */
14991 inject_this_parameter (this_type, cp_type_quals (this_type));
14992 }
14993
14994 /* Substitute the return type. */
14995 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14996
14997 if (do_inject)
14998 {
14999 current_class_ptr = save_ccp;
15000 current_class_ref = save_ccr;
15001 }
15002 }
15003 else
15004 /* Substitute the return type. */
15005 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15006
15007 if (return_type == error_mark_node)
15008 return error_mark_node;
15009 /* DR 486 clarifies that creation of a function type with an
15010 invalid return type is a deduction failure. */
15011 if (TREE_CODE (return_type) == ARRAY_TYPE
15012 || TREE_CODE (return_type) == FUNCTION_TYPE)
15013 {
15014 if (complain & tf_error)
15015 {
15016 if (TREE_CODE (return_type) == ARRAY_TYPE)
15017 error ("function returning an array");
15018 else
15019 error ("function returning a function");
15020 }
15021 return error_mark_node;
15022 }
15023 /* And DR 657. */
15024 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
15025 return error_mark_node;
15026
15027 if (!late_return_type_p)
15028 {
15029 /* Substitute the argument types. */
15030 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15031 complain, in_decl);
15032 if (arg_types == error_mark_node)
15033 return error_mark_node;
15034 }
15035
15036 /* Construct a new type node and return it. */
15037 return rebuild_function_or_method_type (t, return_type, arg_types,
15038 /*raises=*/NULL_TREE, complain);
15039 }
15040
15041 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15042 ARGS into that specification, and return the substituted
15043 specification. If there is no specification, return NULL_TREE. */
15044
15045 static tree
15046 tsubst_exception_specification (tree fntype,
15047 tree args,
15048 tsubst_flags_t complain,
15049 tree in_decl,
15050 bool defer_ok)
15051 {
15052 tree specs;
15053 tree new_specs;
15054
15055 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15056 new_specs = NULL_TREE;
15057 if (specs && TREE_PURPOSE (specs))
15058 {
15059 /* A noexcept-specifier. */
15060 tree expr = TREE_PURPOSE (specs);
15061 if (TREE_CODE (expr) == INTEGER_CST)
15062 new_specs = expr;
15063 else if (defer_ok)
15064 {
15065 /* Defer instantiation of noexcept-specifiers to avoid
15066 excessive instantiations (c++/49107). */
15067 new_specs = make_node (DEFERRED_NOEXCEPT);
15068 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15069 {
15070 /* We already partially instantiated this member template,
15071 so combine the new args with the old. */
15072 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15073 = DEFERRED_NOEXCEPT_PATTERN (expr);
15074 DEFERRED_NOEXCEPT_ARGS (new_specs)
15075 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15076 }
15077 else
15078 {
15079 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15080 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15081 }
15082 }
15083 else
15084 {
15085 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15086 {
15087 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15088 args);
15089 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15090 }
15091 new_specs = tsubst_copy_and_build
15092 (expr, args, complain, in_decl, /*function_p=*/false,
15093 /*integral_constant_expression_p=*/true);
15094 }
15095 new_specs = build_noexcept_spec (new_specs, complain);
15096 }
15097 else if (specs)
15098 {
15099 if (! TREE_VALUE (specs))
15100 new_specs = specs;
15101 else
15102 while (specs)
15103 {
15104 tree spec;
15105 int i, len = 1;
15106 tree expanded_specs = NULL_TREE;
15107
15108 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15109 {
15110 /* Expand the pack expansion type. */
15111 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15112 args, complain,
15113 in_decl);
15114
15115 if (expanded_specs == error_mark_node)
15116 return error_mark_node;
15117 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15118 len = TREE_VEC_LENGTH (expanded_specs);
15119 else
15120 {
15121 /* We're substituting into a member template, so
15122 we got a TYPE_PACK_EXPANSION back. Add that
15123 expansion and move on. */
15124 gcc_assert (TREE_CODE (expanded_specs)
15125 == TYPE_PACK_EXPANSION);
15126 new_specs = add_exception_specifier (new_specs,
15127 expanded_specs,
15128 complain);
15129 specs = TREE_CHAIN (specs);
15130 continue;
15131 }
15132 }
15133
15134 for (i = 0; i < len; ++i)
15135 {
15136 if (expanded_specs)
15137 spec = TREE_VEC_ELT (expanded_specs, i);
15138 else
15139 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15140 if (spec == error_mark_node)
15141 return spec;
15142 new_specs = add_exception_specifier (new_specs, spec,
15143 complain);
15144 }
15145
15146 specs = TREE_CHAIN (specs);
15147 }
15148 }
15149 return new_specs;
15150 }
15151
15152 /* Substitute through a TREE_LIST of types or expressions, handling pack
15153 expansions. */
15154
15155 tree
15156 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15157 {
15158 if (t == void_list_node)
15159 return t;
15160
15161 tree purpose = TREE_PURPOSE (t);
15162 tree purposevec = NULL_TREE;
15163 if (!purpose)
15164 ;
15165 else if (PACK_EXPANSION_P (purpose))
15166 {
15167 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15168 if (TREE_CODE (purpose) == TREE_VEC)
15169 purposevec = purpose;
15170 }
15171 else if (TYPE_P (purpose))
15172 purpose = tsubst (purpose, args, complain, in_decl);
15173 else
15174 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15175 if (purpose == error_mark_node || purposevec == error_mark_node)
15176 return error_mark_node;
15177
15178 tree value = TREE_VALUE (t);
15179 tree valuevec = NULL_TREE;
15180 if (!value)
15181 ;
15182 else if (PACK_EXPANSION_P (value))
15183 {
15184 value = tsubst_pack_expansion (value, args, complain, in_decl);
15185 if (TREE_CODE (value) == TREE_VEC)
15186 valuevec = value;
15187 }
15188 else if (TYPE_P (value))
15189 value = tsubst (value, args, complain, in_decl);
15190 else
15191 value = tsubst_copy_and_build (value, args, complain, in_decl);
15192 if (value == error_mark_node || valuevec == error_mark_node)
15193 return error_mark_node;
15194
15195 tree chain = TREE_CHAIN (t);
15196 if (!chain)
15197 ;
15198 else if (TREE_CODE (chain) == TREE_LIST)
15199 chain = tsubst_tree_list (chain, args, complain, in_decl);
15200 else if (TYPE_P (chain))
15201 chain = tsubst (chain, args, complain, in_decl);
15202 else
15203 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15204 if (chain == error_mark_node)
15205 return error_mark_node;
15206
15207 if (purpose == TREE_PURPOSE (t)
15208 && value == TREE_VALUE (t)
15209 && chain == TREE_CHAIN (t))
15210 return t;
15211
15212 int len;
15213 /* Determine the number of arguments. */
15214 if (purposevec)
15215 {
15216 len = TREE_VEC_LENGTH (purposevec);
15217 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15218 }
15219 else if (valuevec)
15220 len = TREE_VEC_LENGTH (valuevec);
15221 else
15222 len = 1;
15223
15224 for (int i = len; i-- > 0; )
15225 {
15226 if (purposevec)
15227 purpose = TREE_VEC_ELT (purposevec, i);
15228 if (valuevec)
15229 value = TREE_VEC_ELT (valuevec, i);
15230
15231 if (value && TYPE_P (value))
15232 chain = hash_tree_cons (purpose, value, chain);
15233 else
15234 chain = tree_cons (purpose, value, chain);
15235 }
15236
15237 return chain;
15238 }
15239
15240 /* Take the tree structure T and replace template parameters used
15241 therein with the argument vector ARGS. IN_DECL is an associated
15242 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15243 Issue error and warning messages under control of COMPLAIN. Note
15244 that we must be relatively non-tolerant of extensions here, in
15245 order to preserve conformance; if we allow substitutions that
15246 should not be allowed, we may allow argument deductions that should
15247 not succeed, and therefore report ambiguous overload situations
15248 where there are none. In theory, we could allow the substitution,
15249 but indicate that it should have failed, and allow our caller to
15250 make sure that the right thing happens, but we don't try to do this
15251 yet.
15252
15253 This function is used for dealing with types, decls and the like;
15254 for expressions, use tsubst_expr or tsubst_copy. */
15255
15256 tree
15257 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15258 {
15259 enum tree_code code;
15260 tree type, r = NULL_TREE;
15261
15262 if (t == NULL_TREE || t == error_mark_node
15263 || t == integer_type_node
15264 || t == void_type_node
15265 || t == char_type_node
15266 || t == unknown_type_node
15267 || TREE_CODE (t) == NAMESPACE_DECL
15268 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15269 return t;
15270
15271 if (DECL_P (t))
15272 return tsubst_decl (t, args, complain);
15273
15274 if (args == NULL_TREE)
15275 return t;
15276
15277 code = TREE_CODE (t);
15278
15279 if (code == IDENTIFIER_NODE)
15280 type = IDENTIFIER_TYPE_VALUE (t);
15281 else
15282 type = TREE_TYPE (t);
15283
15284 gcc_assert (type != unknown_type_node);
15285
15286 /* Reuse typedefs. We need to do this to handle dependent attributes,
15287 such as attribute aligned. */
15288 if (TYPE_P (t)
15289 && typedef_variant_p (t))
15290 {
15291 tree decl = TYPE_NAME (t);
15292
15293 if (alias_template_specialization_p (t, nt_opaque))
15294 {
15295 /* DECL represents an alias template and we want to
15296 instantiate it. */
15297 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15298 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15299 r = instantiate_alias_template (tmpl, gen_args, complain);
15300 }
15301 else if (DECL_CLASS_SCOPE_P (decl)
15302 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15303 && uses_template_parms (DECL_CONTEXT (decl)))
15304 {
15305 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15306 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15307 r = retrieve_specialization (tmpl, gen_args, 0);
15308 }
15309 else if (DECL_FUNCTION_SCOPE_P (decl)
15310 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15311 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15312 r = retrieve_local_specialization (decl);
15313 else
15314 /* The typedef is from a non-template context. */
15315 return t;
15316
15317 if (r)
15318 {
15319 r = TREE_TYPE (r);
15320 r = cp_build_qualified_type_real
15321 (r, cp_type_quals (t) | cp_type_quals (r),
15322 complain | tf_ignore_bad_quals);
15323 return r;
15324 }
15325 else
15326 {
15327 /* We don't have an instantiation yet, so drop the typedef. */
15328 int quals = cp_type_quals (t);
15329 t = DECL_ORIGINAL_TYPE (decl);
15330 t = cp_build_qualified_type_real (t, quals,
15331 complain | tf_ignore_bad_quals);
15332 }
15333 }
15334
15335 bool fndecl_type = (complain & tf_fndecl_type);
15336 complain &= ~tf_fndecl_type;
15337
15338 if (type
15339 && code != TYPENAME_TYPE
15340 && code != TEMPLATE_TYPE_PARM
15341 && code != TEMPLATE_PARM_INDEX
15342 && code != IDENTIFIER_NODE
15343 && code != FUNCTION_TYPE
15344 && code != METHOD_TYPE)
15345 type = tsubst (type, args, complain, in_decl);
15346 if (type == error_mark_node)
15347 return error_mark_node;
15348
15349 switch (code)
15350 {
15351 case RECORD_TYPE:
15352 case UNION_TYPE:
15353 case ENUMERAL_TYPE:
15354 return tsubst_aggr_type (t, args, complain, in_decl,
15355 /*entering_scope=*/0);
15356
15357 case ERROR_MARK:
15358 case IDENTIFIER_NODE:
15359 case VOID_TYPE:
15360 case REAL_TYPE:
15361 case COMPLEX_TYPE:
15362 case VECTOR_TYPE:
15363 case BOOLEAN_TYPE:
15364 case NULLPTR_TYPE:
15365 case LANG_TYPE:
15366 return t;
15367
15368 case INTEGER_TYPE:
15369 if (t == integer_type_node)
15370 return t;
15371
15372 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15373 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15374 return t;
15375
15376 {
15377 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15378
15379 max = tsubst_expr (omax, args, complain, in_decl,
15380 /*integral_constant_expression_p=*/false);
15381
15382 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15383 needed. */
15384 if (TREE_CODE (max) == NOP_EXPR
15385 && TREE_SIDE_EFFECTS (omax)
15386 && !TREE_TYPE (max))
15387 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15388
15389 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15390 with TREE_SIDE_EFFECTS that indicates this is not an integral
15391 constant expression. */
15392 if (processing_template_decl
15393 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15394 {
15395 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15396 TREE_SIDE_EFFECTS (max) = 1;
15397 }
15398
15399 return compute_array_index_type (NULL_TREE, max, complain);
15400 }
15401
15402 case TEMPLATE_TYPE_PARM:
15403 case TEMPLATE_TEMPLATE_PARM:
15404 case BOUND_TEMPLATE_TEMPLATE_PARM:
15405 case TEMPLATE_PARM_INDEX:
15406 {
15407 int idx;
15408 int level;
15409 int levels;
15410 tree arg = NULL_TREE;
15411
15412 r = NULL_TREE;
15413
15414 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15415 template_parm_level_and_index (t, &level, &idx);
15416
15417 levels = TMPL_ARGS_DEPTH (args);
15418 if (level <= levels
15419 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15420 {
15421 arg = TMPL_ARG (args, level, idx);
15422
15423 /* See through ARGUMENT_PACK_SELECT arguments. */
15424 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15425 arg = argument_pack_select_arg (arg);
15426 }
15427
15428 if (arg == error_mark_node)
15429 return error_mark_node;
15430 else if (arg != NULL_TREE)
15431 {
15432 if (ARGUMENT_PACK_P (arg))
15433 /* If ARG is an argument pack, we don't actually want to
15434 perform a substitution here, because substitutions
15435 for argument packs are only done
15436 element-by-element. We can get to this point when
15437 substituting the type of a non-type template
15438 parameter pack, when that type actually contains
15439 template parameter packs from an outer template, e.g.,
15440
15441 template<typename... Types> struct A {
15442 template<Types... Values> struct B { };
15443 }; */
15444 return t;
15445
15446 if (code == TEMPLATE_TYPE_PARM)
15447 {
15448 int quals;
15449
15450 /* When building concept checks for the purpose of
15451 deducing placeholders, we can end up with wildcards
15452 where types are expected. Adjust this to the deduced
15453 value. */
15454 if (TREE_CODE (arg) == WILDCARD_DECL)
15455 arg = TREE_TYPE (TREE_TYPE (arg));
15456
15457 gcc_assert (TYPE_P (arg));
15458
15459 quals = cp_type_quals (arg) | cp_type_quals (t);
15460
15461 return cp_build_qualified_type_real
15462 (arg, quals, complain | tf_ignore_bad_quals);
15463 }
15464 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15465 {
15466 /* We are processing a type constructed from a
15467 template template parameter. */
15468 tree argvec = tsubst (TYPE_TI_ARGS (t),
15469 args, complain, in_decl);
15470 if (argvec == error_mark_node)
15471 return error_mark_node;
15472
15473 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15474 || TREE_CODE (arg) == TEMPLATE_DECL
15475 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15476
15477 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15478 /* Consider this code:
15479
15480 template <template <class> class Template>
15481 struct Internal {
15482 template <class Arg> using Bind = Template<Arg>;
15483 };
15484
15485 template <template <class> class Template, class Arg>
15486 using Instantiate = Template<Arg>; //#0
15487
15488 template <template <class> class Template,
15489 class Argument>
15490 using Bind =
15491 Instantiate<Internal<Template>::template Bind,
15492 Argument>; //#1
15493
15494 When #1 is parsed, the
15495 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15496 parameter `Template' in #0 matches the
15497 UNBOUND_CLASS_TEMPLATE representing the argument
15498 `Internal<Template>::template Bind'; We then want
15499 to assemble the type `Bind<Argument>' that can't
15500 be fully created right now, because
15501 `Internal<Template>' not being complete, the Bind
15502 template cannot be looked up in that context. So
15503 we need to "store" `Bind<Argument>' for later
15504 when the context of Bind becomes complete. Let's
15505 store that in a TYPENAME_TYPE. */
15506 return make_typename_type (TYPE_CONTEXT (arg),
15507 build_nt (TEMPLATE_ID_EXPR,
15508 TYPE_IDENTIFIER (arg),
15509 argvec),
15510 typename_type,
15511 complain);
15512
15513 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15514 are resolving nested-types in the signature of a
15515 member function templates. Otherwise ARG is a
15516 TEMPLATE_DECL and is the real template to be
15517 instantiated. */
15518 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15519 arg = TYPE_NAME (arg);
15520
15521 r = lookup_template_class (arg,
15522 argvec, in_decl,
15523 DECL_CONTEXT (arg),
15524 /*entering_scope=*/0,
15525 complain);
15526 return cp_build_qualified_type_real
15527 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15528 }
15529 else if (code == TEMPLATE_TEMPLATE_PARM)
15530 return arg;
15531 else
15532 /* TEMPLATE_PARM_INDEX. */
15533 return convert_from_reference (unshare_expr (arg));
15534 }
15535
15536 if (level == 1)
15537 /* This can happen during the attempted tsubst'ing in
15538 unify. This means that we don't yet have any information
15539 about the template parameter in question. */
15540 return t;
15541
15542 /* Early in template argument deduction substitution, we don't
15543 want to reduce the level of 'auto', or it will be confused
15544 with a normal template parm in subsequent deduction.
15545 Similarly, don't reduce the level of template parameters to
15546 avoid mismatches when deducing their types. */
15547 if (complain & tf_partial)
15548 return t;
15549
15550 /* If we get here, we must have been looking at a parm for a
15551 more deeply nested template. Make a new version of this
15552 template parameter, but with a lower level. */
15553 switch (code)
15554 {
15555 case TEMPLATE_TYPE_PARM:
15556 case TEMPLATE_TEMPLATE_PARM:
15557 case BOUND_TEMPLATE_TEMPLATE_PARM:
15558 if (cp_type_quals (t))
15559 {
15560 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15561 r = cp_build_qualified_type_real
15562 (r, cp_type_quals (t),
15563 complain | (code == TEMPLATE_TYPE_PARM
15564 ? tf_ignore_bad_quals : 0));
15565 }
15566 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15567 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15568 && (r = (TEMPLATE_PARM_DESCENDANTS
15569 (TEMPLATE_TYPE_PARM_INDEX (t))))
15570 && (r = TREE_TYPE (r))
15571 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15572 /* Break infinite recursion when substituting the constraints
15573 of a constrained placeholder. */;
15574 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15575 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15576 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15577 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15578 r = TEMPLATE_PARM_DESCENDANTS (arg))
15579 && (TEMPLATE_PARM_LEVEL (r)
15580 == TEMPLATE_PARM_LEVEL (arg) - levels))
15581 /* Cache the simple case of lowering a type parameter. */
15582 r = TREE_TYPE (r);
15583 else
15584 {
15585 r = copy_type (t);
15586 TEMPLATE_TYPE_PARM_INDEX (r)
15587 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15588 r, levels, args, complain);
15589 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15590 TYPE_MAIN_VARIANT (r) = r;
15591 TYPE_POINTER_TO (r) = NULL_TREE;
15592 TYPE_REFERENCE_TO (r) = NULL_TREE;
15593
15594 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15595 {
15596 /* Propagate constraints on placeholders since they are
15597 only instantiated during satisfaction. */
15598 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15599 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15600 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15601 {
15602 pl = tsubst_copy (pl, args, complain, in_decl);
15603 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15604 }
15605 }
15606
15607 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15608 /* We have reduced the level of the template
15609 template parameter, but not the levels of its
15610 template parameters, so canonical_type_parameter
15611 will not be able to find the canonical template
15612 template parameter for this level. Thus, we
15613 require structural equality checking to compare
15614 TEMPLATE_TEMPLATE_PARMs. */
15615 SET_TYPE_STRUCTURAL_EQUALITY (r);
15616 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15617 SET_TYPE_STRUCTURAL_EQUALITY (r);
15618 else
15619 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15620
15621 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15622 {
15623 tree tinfo = TYPE_TEMPLATE_INFO (t);
15624 /* We might need to substitute into the types of non-type
15625 template parameters. */
15626 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15627 complain, in_decl);
15628 if (tmpl == error_mark_node)
15629 return error_mark_node;
15630 tree argvec = tsubst (TI_ARGS (tinfo), args,
15631 complain, in_decl);
15632 if (argvec == error_mark_node)
15633 return error_mark_node;
15634
15635 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15636 = build_template_info (tmpl, argvec);
15637 }
15638 }
15639 break;
15640
15641 case TEMPLATE_PARM_INDEX:
15642 /* OK, now substitute the type of the non-type parameter. We
15643 couldn't do it earlier because it might be an auto parameter,
15644 and we wouldn't need to if we had an argument. */
15645 type = tsubst (type, args, complain, in_decl);
15646 if (type == error_mark_node)
15647 return error_mark_node;
15648 r = reduce_template_parm_level (t, type, levels, args, complain);
15649 break;
15650
15651 default:
15652 gcc_unreachable ();
15653 }
15654
15655 return r;
15656 }
15657
15658 case TREE_LIST:
15659 return tsubst_tree_list (t, args, complain, in_decl);
15660
15661 case TREE_BINFO:
15662 /* We should never be tsubsting a binfo. */
15663 gcc_unreachable ();
15664
15665 case TREE_VEC:
15666 /* A vector of template arguments. */
15667 gcc_assert (!type);
15668 return tsubst_template_args (t, args, complain, in_decl);
15669
15670 case POINTER_TYPE:
15671 case REFERENCE_TYPE:
15672 {
15673 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15674 return t;
15675
15676 /* [temp.deduct]
15677
15678 Type deduction may fail for any of the following
15679 reasons:
15680
15681 -- Attempting to create a pointer to reference type.
15682 -- Attempting to create a reference to a reference type or
15683 a reference to void.
15684
15685 Core issue 106 says that creating a reference to a reference
15686 during instantiation is no longer a cause for failure. We
15687 only enforce this check in strict C++98 mode. */
15688 if ((TYPE_REF_P (type)
15689 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15690 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15691 {
15692 static location_t last_loc;
15693
15694 /* We keep track of the last time we issued this error
15695 message to avoid spewing a ton of messages during a
15696 single bad template instantiation. */
15697 if (complain & tf_error
15698 && last_loc != input_location)
15699 {
15700 if (VOID_TYPE_P (type))
15701 error ("forming reference to void");
15702 else if (code == POINTER_TYPE)
15703 error ("forming pointer to reference type %qT", type);
15704 else
15705 error ("forming reference to reference type %qT", type);
15706 last_loc = input_location;
15707 }
15708
15709 return error_mark_node;
15710 }
15711 else if (TREE_CODE (type) == FUNCTION_TYPE
15712 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15713 || type_memfn_rqual (type) != REF_QUAL_NONE))
15714 {
15715 if (complain & tf_error)
15716 {
15717 if (code == POINTER_TYPE)
15718 error ("forming pointer to qualified function type %qT",
15719 type);
15720 else
15721 error ("forming reference to qualified function type %qT",
15722 type);
15723 }
15724 return error_mark_node;
15725 }
15726 else if (code == POINTER_TYPE)
15727 {
15728 r = build_pointer_type (type);
15729 if (TREE_CODE (type) == METHOD_TYPE)
15730 r = build_ptrmemfunc_type (r);
15731 }
15732 else if (TYPE_REF_P (type))
15733 /* In C++0x, during template argument substitution, when there is an
15734 attempt to create a reference to a reference type, reference
15735 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15736
15737 "If a template-argument for a template-parameter T names a type
15738 that is a reference to a type A, an attempt to create the type
15739 'lvalue reference to cv T' creates the type 'lvalue reference to
15740 A,' while an attempt to create the type type rvalue reference to
15741 cv T' creates the type T"
15742 */
15743 r = cp_build_reference_type
15744 (TREE_TYPE (type),
15745 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15746 else
15747 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15748 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15749
15750 if (r != error_mark_node)
15751 /* Will this ever be needed for TYPE_..._TO values? */
15752 layout_type (r);
15753
15754 return r;
15755 }
15756 case OFFSET_TYPE:
15757 {
15758 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15759 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15760 {
15761 /* [temp.deduct]
15762
15763 Type deduction may fail for any of the following
15764 reasons:
15765
15766 -- Attempting to create "pointer to member of T" when T
15767 is not a class type. */
15768 if (complain & tf_error)
15769 error ("creating pointer to member of non-class type %qT", r);
15770 return error_mark_node;
15771 }
15772 if (TYPE_REF_P (type))
15773 {
15774 if (complain & tf_error)
15775 error ("creating pointer to member reference type %qT", type);
15776 return error_mark_node;
15777 }
15778 if (VOID_TYPE_P (type))
15779 {
15780 if (complain & tf_error)
15781 error ("creating pointer to member of type void");
15782 return error_mark_node;
15783 }
15784 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15785 if (TREE_CODE (type) == FUNCTION_TYPE)
15786 {
15787 /* The type of the implicit object parameter gets its
15788 cv-qualifiers from the FUNCTION_TYPE. */
15789 tree memptr;
15790 tree method_type
15791 = build_memfn_type (type, r, type_memfn_quals (type),
15792 type_memfn_rqual (type));
15793 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15794 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15795 complain);
15796 }
15797 else
15798 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15799 cp_type_quals (t),
15800 complain);
15801 }
15802 case FUNCTION_TYPE:
15803 case METHOD_TYPE:
15804 {
15805 tree fntype;
15806 tree specs;
15807 fntype = tsubst_function_type (t, args, complain, in_decl);
15808 if (fntype == error_mark_node)
15809 return error_mark_node;
15810
15811 /* Substitute the exception specification. */
15812 specs = tsubst_exception_specification (t, args, complain, in_decl,
15813 /*defer_ok*/fndecl_type);
15814 if (specs == error_mark_node)
15815 return error_mark_node;
15816 if (specs)
15817 fntype = build_exception_variant (fntype, specs);
15818 return fntype;
15819 }
15820 case ARRAY_TYPE:
15821 {
15822 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15823 if (domain == error_mark_node)
15824 return error_mark_node;
15825
15826 /* As an optimization, we avoid regenerating the array type if
15827 it will obviously be the same as T. */
15828 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15829 return t;
15830
15831 /* These checks should match the ones in create_array_type_for_decl.
15832
15833 [temp.deduct]
15834
15835 The deduction may fail for any of the following reasons:
15836
15837 -- Attempting to create an array with an element type that
15838 is void, a function type, or a reference type, or [DR337]
15839 an abstract class type. */
15840 if (VOID_TYPE_P (type)
15841 || TREE_CODE (type) == FUNCTION_TYPE
15842 || (TREE_CODE (type) == ARRAY_TYPE
15843 && TYPE_DOMAIN (type) == NULL_TREE)
15844 || TYPE_REF_P (type))
15845 {
15846 if (complain & tf_error)
15847 error ("creating array of %qT", type);
15848 return error_mark_node;
15849 }
15850
15851 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15852 return error_mark_node;
15853
15854 r = build_cplus_array_type (type, domain);
15855
15856 if (!valid_array_size_p (input_location, r, in_decl,
15857 (complain & tf_error)))
15858 return error_mark_node;
15859
15860 if (TYPE_USER_ALIGN (t))
15861 {
15862 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15863 TYPE_USER_ALIGN (r) = 1;
15864 }
15865
15866 return r;
15867 }
15868
15869 case TYPENAME_TYPE:
15870 {
15871 tree ctx = TYPE_CONTEXT (t);
15872 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15873 {
15874 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15875 if (ctx == error_mark_node
15876 || TREE_VEC_LENGTH (ctx) > 1)
15877 return error_mark_node;
15878 if (TREE_VEC_LENGTH (ctx) == 0)
15879 {
15880 if (complain & tf_error)
15881 error ("%qD is instantiated for an empty pack",
15882 TYPENAME_TYPE_FULLNAME (t));
15883 return error_mark_node;
15884 }
15885 ctx = TREE_VEC_ELT (ctx, 0);
15886 }
15887 else
15888 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15889 /*entering_scope=*/1);
15890 if (ctx == error_mark_node)
15891 return error_mark_node;
15892
15893 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15894 complain, in_decl);
15895 if (f == error_mark_node)
15896 return error_mark_node;
15897
15898 if (!MAYBE_CLASS_TYPE_P (ctx))
15899 {
15900 if (complain & tf_error)
15901 error ("%qT is not a class, struct, or union type", ctx);
15902 return error_mark_node;
15903 }
15904 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15905 {
15906 /* Normally, make_typename_type does not require that the CTX
15907 have complete type in order to allow things like:
15908
15909 template <class T> struct S { typename S<T>::X Y; };
15910
15911 But, such constructs have already been resolved by this
15912 point, so here CTX really should have complete type, unless
15913 it's a partial instantiation. */
15914 ctx = complete_type (ctx);
15915 if (!COMPLETE_TYPE_P (ctx))
15916 {
15917 if (complain & tf_error)
15918 cxx_incomplete_type_error (NULL_TREE, ctx);
15919 return error_mark_node;
15920 }
15921 }
15922
15923 f = make_typename_type (ctx, f, typename_type,
15924 complain | tf_keep_type_decl);
15925 if (f == error_mark_node)
15926 return f;
15927 if (TREE_CODE (f) == TYPE_DECL)
15928 {
15929 complain |= tf_ignore_bad_quals;
15930 f = TREE_TYPE (f);
15931 }
15932
15933 if (TREE_CODE (f) != TYPENAME_TYPE)
15934 {
15935 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15936 {
15937 if (complain & tf_error)
15938 error ("%qT resolves to %qT, which is not an enumeration type",
15939 t, f);
15940 else
15941 return error_mark_node;
15942 }
15943 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15944 {
15945 if (complain & tf_error)
15946 error ("%qT resolves to %qT, which is not a class type",
15947 t, f);
15948 else
15949 return error_mark_node;
15950 }
15951 }
15952
15953 return cp_build_qualified_type_real
15954 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15955 }
15956
15957 case UNBOUND_CLASS_TEMPLATE:
15958 {
15959 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15960 in_decl, /*entering_scope=*/1);
15961 tree name = TYPE_IDENTIFIER (t);
15962 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15963
15964 if (ctx == error_mark_node || name == error_mark_node)
15965 return error_mark_node;
15966
15967 if (parm_list)
15968 parm_list = tsubst_template_parms (parm_list, args, complain);
15969 return make_unbound_class_template (ctx, name, parm_list, complain);
15970 }
15971
15972 case TYPEOF_TYPE:
15973 {
15974 tree type;
15975
15976 ++cp_unevaluated_operand;
15977 ++c_inhibit_evaluation_warnings;
15978
15979 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15980 complain, in_decl,
15981 /*integral_constant_expression_p=*/false);
15982
15983 --cp_unevaluated_operand;
15984 --c_inhibit_evaluation_warnings;
15985
15986 type = finish_typeof (type);
15987 return cp_build_qualified_type_real (type,
15988 cp_type_quals (t)
15989 | cp_type_quals (type),
15990 complain);
15991 }
15992
15993 case DECLTYPE_TYPE:
15994 {
15995 tree type;
15996
15997 ++cp_unevaluated_operand;
15998 ++c_inhibit_evaluation_warnings;
15999
16000 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16001 complain|tf_decltype, in_decl,
16002 /*function_p*/false,
16003 /*integral_constant_expression*/false);
16004
16005 --cp_unevaluated_operand;
16006 --c_inhibit_evaluation_warnings;
16007
16008 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16009 type = lambda_capture_field_type (type,
16010 false /*explicit_init*/,
16011 DECLTYPE_FOR_REF_CAPTURE (t));
16012 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16013 type = lambda_proxy_type (type);
16014 else
16015 {
16016 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16017 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16018 && EXPR_P (type))
16019 /* In a template ~id could be either a complement expression
16020 or an unqualified-id naming a destructor; if instantiating
16021 it produces an expression, it's not an id-expression or
16022 member access. */
16023 id = false;
16024 type = finish_decltype_type (type, id, complain);
16025 }
16026 return cp_build_qualified_type_real (type,
16027 cp_type_quals (t)
16028 | cp_type_quals (type),
16029 complain | tf_ignore_bad_quals);
16030 }
16031
16032 case UNDERLYING_TYPE:
16033 {
16034 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16035 complain, in_decl);
16036 return finish_underlying_type (type);
16037 }
16038
16039 case TYPE_ARGUMENT_PACK:
16040 case NONTYPE_ARGUMENT_PACK:
16041 {
16042 tree r;
16043
16044 if (code == NONTYPE_ARGUMENT_PACK)
16045 r = make_node (code);
16046 else
16047 r = cxx_make_type (code);
16048
16049 tree pack_args = ARGUMENT_PACK_ARGS (t);
16050 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
16051 SET_ARGUMENT_PACK_ARGS (r, pack_args);
16052
16053 return r;
16054 }
16055
16056 case VOID_CST:
16057 case INTEGER_CST:
16058 case REAL_CST:
16059 case STRING_CST:
16060 case PLUS_EXPR:
16061 case MINUS_EXPR:
16062 case NEGATE_EXPR:
16063 case NOP_EXPR:
16064 case INDIRECT_REF:
16065 case ADDR_EXPR:
16066 case CALL_EXPR:
16067 case ARRAY_REF:
16068 case SCOPE_REF:
16069 /* We should use one of the expression tsubsts for these codes. */
16070 gcc_unreachable ();
16071
16072 default:
16073 sorry ("use of %qs in template", get_tree_code_name (code));
16074 return error_mark_node;
16075 }
16076 }
16077
16078 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16079 expression on the left-hand side of the "." or "->" operator. We
16080 only do the lookup if we had a dependent BASELINK. Otherwise we
16081 adjust it onto the instantiated heirarchy. */
16082
16083 static tree
16084 tsubst_baselink (tree baselink, tree object_type,
16085 tree args, tsubst_flags_t complain, tree in_decl)
16086 {
16087 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16088 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16089 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16090
16091 tree optype = BASELINK_OPTYPE (baselink);
16092 optype = tsubst (optype, args, complain, in_decl);
16093
16094 tree template_args = NULL_TREE;
16095 bool template_id_p = false;
16096 tree fns = BASELINK_FUNCTIONS (baselink);
16097 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16098 {
16099 template_id_p = true;
16100 template_args = TREE_OPERAND (fns, 1);
16101 fns = TREE_OPERAND (fns, 0);
16102 if (template_args)
16103 template_args = tsubst_template_args (template_args, args,
16104 complain, in_decl);
16105 }
16106
16107 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16108 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16109 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16110
16111 if (dependent_p)
16112 {
16113 tree name = OVL_NAME (fns);
16114 if (IDENTIFIER_CONV_OP_P (name))
16115 name = make_conv_op_name (optype);
16116
16117 if (name == complete_dtor_identifier)
16118 /* Treat as-if non-dependent below. */
16119 dependent_p = false;
16120
16121 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16122 complain);
16123 if (!baselink)
16124 {
16125 if ((complain & tf_error)
16126 && constructor_name_p (name, qualifying_scope))
16127 error ("cannot call constructor %<%T::%D%> directly",
16128 qualifying_scope, name);
16129 return error_mark_node;
16130 }
16131
16132 if (BASELINK_P (baselink))
16133 fns = BASELINK_FUNCTIONS (baselink);
16134 }
16135 else
16136 /* We're going to overwrite pieces below, make a duplicate. */
16137 baselink = copy_node (baselink);
16138
16139 /* If lookup found a single function, mark it as used at this point.
16140 (If lookup found multiple functions the one selected later by
16141 overload resolution will be marked as used at that point.) */
16142 if (!template_id_p && !really_overloaded_fn (fns))
16143 {
16144 tree fn = OVL_FIRST (fns);
16145 bool ok = mark_used (fn, complain);
16146 if (!ok && !(complain & tf_error))
16147 return error_mark_node;
16148 if (ok && BASELINK_P (baselink))
16149 /* We might have instantiated an auto function. */
16150 TREE_TYPE (baselink) = TREE_TYPE (fn);
16151 }
16152
16153 if (BASELINK_P (baselink))
16154 {
16155 /* Add back the template arguments, if present. */
16156 if (template_id_p)
16157 BASELINK_FUNCTIONS (baselink)
16158 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16159
16160 /* Update the conversion operator type. */
16161 BASELINK_OPTYPE (baselink) = optype;
16162 }
16163
16164 if (!object_type)
16165 object_type = current_class_type;
16166
16167 if (qualified_p || !dependent_p)
16168 {
16169 baselink = adjust_result_of_qualified_name_lookup (baselink,
16170 qualifying_scope,
16171 object_type);
16172 if (!qualified_p)
16173 /* We need to call adjust_result_of_qualified_name_lookup in case the
16174 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16175 so that we still get virtual function binding. */
16176 BASELINK_QUALIFIED_P (baselink) = false;
16177 }
16178
16179 return baselink;
16180 }
16181
16182 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16183 true if the qualified-id will be a postfix-expression in-and-of
16184 itself; false if more of the postfix-expression follows the
16185 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16186 of "&". */
16187
16188 static tree
16189 tsubst_qualified_id (tree qualified_id, tree args,
16190 tsubst_flags_t complain, tree in_decl,
16191 bool done, bool address_p)
16192 {
16193 tree expr;
16194 tree scope;
16195 tree name;
16196 bool is_template;
16197 tree template_args;
16198 location_t loc = UNKNOWN_LOCATION;
16199
16200 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16201
16202 /* Figure out what name to look up. */
16203 name = TREE_OPERAND (qualified_id, 1);
16204 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16205 {
16206 is_template = true;
16207 loc = EXPR_LOCATION (name);
16208 template_args = TREE_OPERAND (name, 1);
16209 if (template_args)
16210 template_args = tsubst_template_args (template_args, args,
16211 complain, in_decl);
16212 if (template_args == error_mark_node)
16213 return error_mark_node;
16214 name = TREE_OPERAND (name, 0);
16215 }
16216 else
16217 {
16218 is_template = false;
16219 template_args = NULL_TREE;
16220 }
16221
16222 /* Substitute into the qualifying scope. When there are no ARGS, we
16223 are just trying to simplify a non-dependent expression. In that
16224 case the qualifying scope may be dependent, and, in any case,
16225 substituting will not help. */
16226 scope = TREE_OPERAND (qualified_id, 0);
16227 if (args)
16228 {
16229 scope = tsubst (scope, args, complain, in_decl);
16230 expr = tsubst_copy (name, args, complain, in_decl);
16231 }
16232 else
16233 expr = name;
16234
16235 if (dependent_scope_p (scope))
16236 {
16237 if (is_template)
16238 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16239 tree r = build_qualified_name (NULL_TREE, scope, expr,
16240 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16241 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16242 return r;
16243 }
16244
16245 if (!BASELINK_P (name) && !DECL_P (expr))
16246 {
16247 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16248 {
16249 /* A BIT_NOT_EXPR is used to represent a destructor. */
16250 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16251 {
16252 error ("qualifying type %qT does not match destructor name ~%qT",
16253 scope, TREE_OPERAND (expr, 0));
16254 expr = error_mark_node;
16255 }
16256 else
16257 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16258 LOOK_want::NORMAL, false);
16259 }
16260 else
16261 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16262 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16263 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16264 {
16265 if (complain & tf_error)
16266 {
16267 error ("dependent-name %qE is parsed as a non-type, but "
16268 "instantiation yields a type", qualified_id);
16269 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16270 }
16271 return error_mark_node;
16272 }
16273 }
16274
16275 if (DECL_P (expr))
16276 {
16277 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16278 scope, complain))
16279 return error_mark_node;
16280 /* Remember that there was a reference to this entity. */
16281 if (!mark_used (expr, complain) && !(complain & tf_error))
16282 return error_mark_node;
16283 }
16284
16285 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16286 {
16287 if (complain & tf_error)
16288 qualified_name_lookup_error (scope,
16289 TREE_OPERAND (qualified_id, 1),
16290 expr, input_location);
16291 return error_mark_node;
16292 }
16293
16294 if (is_template)
16295 {
16296 /* We may be repeating a check already done during parsing, but
16297 if it was well-formed and passed then, it will pass again
16298 now, and if it didn't, we wouldn't have got here. The case
16299 we want to catch is when we couldn't tell then, and can now,
16300 namely when templ prior to substitution was an
16301 identifier. */
16302 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16303 return error_mark_node;
16304
16305 if (variable_template_p (expr))
16306 expr = lookup_and_finish_template_variable (expr, template_args,
16307 complain);
16308 else
16309 expr = lookup_template_function (expr, template_args);
16310 }
16311
16312 if (expr == error_mark_node && complain & tf_error)
16313 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16314 expr, input_location);
16315 else if (TYPE_P (scope))
16316 {
16317 expr = (adjust_result_of_qualified_name_lookup
16318 (expr, scope, current_nonlambda_class_type ()));
16319 expr = (finish_qualified_id_expr
16320 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16321 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16322 /*template_arg_p=*/false, complain));
16323 }
16324
16325 /* Expressions do not generally have reference type. */
16326 if (TREE_CODE (expr) != SCOPE_REF
16327 /* However, if we're about to form a pointer-to-member, we just
16328 want the referenced member referenced. */
16329 && TREE_CODE (expr) != OFFSET_REF)
16330 expr = convert_from_reference (expr);
16331
16332 if (REF_PARENTHESIZED_P (qualified_id))
16333 expr = force_paren_expr (expr);
16334
16335 return expr;
16336 }
16337
16338 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16339 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16340 for tsubst. */
16341
16342 static tree
16343 tsubst_init (tree init, tree decl, tree args,
16344 tsubst_flags_t complain, tree in_decl)
16345 {
16346 if (!init)
16347 return NULL_TREE;
16348
16349 init = tsubst_expr (init, args, complain, in_decl, false);
16350
16351 tree type = TREE_TYPE (decl);
16352
16353 if (!init && type != error_mark_node)
16354 {
16355 if (tree auto_node = type_uses_auto (type))
16356 {
16357 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16358 {
16359 if (complain & tf_error)
16360 error ("initializer for %q#D expands to an empty list "
16361 "of expressions", decl);
16362 return error_mark_node;
16363 }
16364 }
16365 else if (!dependent_type_p (type))
16366 {
16367 /* If we had an initializer but it
16368 instantiated to nothing,
16369 value-initialize the object. This will
16370 only occur when the initializer was a
16371 pack expansion where the parameter packs
16372 used in that expansion were of length
16373 zero. */
16374 init = build_value_init (type, complain);
16375 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16376 init = get_target_expr_sfinae (init, complain);
16377 if (TREE_CODE (init) == TARGET_EXPR)
16378 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16379 }
16380 }
16381
16382 return init;
16383 }
16384
16385 /* If T is a reference to a dependent member of the current instantiation C and
16386 we are trying to refer to that member in a partial instantiation of C,
16387 return a SCOPE_REF; otherwise, return NULL_TREE.
16388
16389 This can happen when forming a C++17 deduction guide, as in PR96199. */
16390
16391 static tree
16392 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16393 tree in_decl)
16394 {
16395 if (cxx_dialect < cxx17)
16396 return NULL_TREE;
16397
16398 tree ctx = context_for_name_lookup (t);
16399 if (!CLASS_TYPE_P (ctx))
16400 return NULL_TREE;
16401
16402 ctx = tsubst (ctx, args, complain, in_decl);
16403 if (dependent_scope_p (ctx))
16404 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16405 /*template_p=*/false);
16406
16407 return NULL_TREE;
16408 }
16409
16410 /* Like tsubst, but deals with expressions. This function just replaces
16411 template parms; to finish processing the resultant expression, use
16412 tsubst_copy_and_build or tsubst_expr. */
16413
16414 static tree
16415 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16416 {
16417 enum tree_code code;
16418 tree r;
16419
16420 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16421 return t;
16422
16423 code = TREE_CODE (t);
16424
16425 switch (code)
16426 {
16427 case PARM_DECL:
16428 r = retrieve_local_specialization (t);
16429
16430 if (r == NULL_TREE)
16431 {
16432 /* We get here for a use of 'this' in an NSDMI. */
16433 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16434 return current_class_ptr;
16435
16436 /* This can happen for a parameter name used later in a function
16437 declaration (such as in a late-specified return type). Just
16438 make a dummy decl, since it's only used for its type. */
16439 gcc_assert (cp_unevaluated_operand != 0);
16440 r = tsubst_decl (t, args, complain);
16441 /* Give it the template pattern as its context; its true context
16442 hasn't been instantiated yet and this is good enough for
16443 mangling. */
16444 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16445 }
16446
16447 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16448 r = argument_pack_select_arg (r);
16449 if (!mark_used (r, complain) && !(complain & tf_error))
16450 return error_mark_node;
16451 return r;
16452
16453 case CONST_DECL:
16454 {
16455 tree enum_type;
16456 tree v;
16457
16458 if (DECL_TEMPLATE_PARM_P (t))
16459 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16460 /* There is no need to substitute into namespace-scope
16461 enumerators. */
16462 if (DECL_NAMESPACE_SCOPE_P (t))
16463 return t;
16464 /* If ARGS is NULL, then T is known to be non-dependent. */
16465 if (args == NULL_TREE)
16466 return scalar_constant_value (t);
16467
16468 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16469 return ref;
16470
16471 /* Unfortunately, we cannot just call lookup_name here.
16472 Consider:
16473
16474 template <int I> int f() {
16475 enum E { a = I };
16476 struct S { void g() { E e = a; } };
16477 };
16478
16479 When we instantiate f<7>::S::g(), say, lookup_name is not
16480 clever enough to find f<7>::a. */
16481 enum_type
16482 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16483 /*entering_scope=*/0);
16484
16485 for (v = TYPE_VALUES (enum_type);
16486 v != NULL_TREE;
16487 v = TREE_CHAIN (v))
16488 if (TREE_PURPOSE (v) == DECL_NAME (t))
16489 return TREE_VALUE (v);
16490
16491 /* We didn't find the name. That should never happen; if
16492 name-lookup found it during preliminary parsing, we
16493 should find it again here during instantiation. */
16494 gcc_unreachable ();
16495 }
16496 return t;
16497
16498 case FIELD_DECL:
16499 if (DECL_CONTEXT (t))
16500 {
16501 tree ctx;
16502
16503 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16504 /*entering_scope=*/1);
16505 if (ctx != DECL_CONTEXT (t))
16506 {
16507 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16508 if (!r)
16509 {
16510 if (complain & tf_error)
16511 error ("using invalid field %qD", t);
16512 return error_mark_node;
16513 }
16514 return r;
16515 }
16516 }
16517
16518 return t;
16519
16520 case VAR_DECL:
16521 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16522 return ref;
16523 gcc_fallthrough();
16524 case FUNCTION_DECL:
16525 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16526 r = tsubst (t, args, complain, in_decl);
16527 else if (DECL_LOCAL_DECL_P (t))
16528 {
16529 /* Local specialization will have been created when we
16530 instantiated the DECL_EXPR_DECL. */
16531 r = retrieve_local_specialization (t);
16532 if (!r)
16533 r = error_mark_node;
16534 }
16535 else if (local_variable_p (t)
16536 && uses_template_parms (DECL_CONTEXT (t)))
16537 {
16538 r = retrieve_local_specialization (t);
16539 if (r == NULL_TREE)
16540 {
16541 /* First try name lookup to find the instantiation. */
16542 r = lookup_name (DECL_NAME (t));
16543 if (r)
16544 {
16545 if (!VAR_P (r))
16546 {
16547 /* During error-recovery we may find a non-variable,
16548 even an OVERLOAD: just bail out and avoid ICEs and
16549 duplicate diagnostics (c++/62207). */
16550 gcc_assert (seen_error ());
16551 return error_mark_node;
16552 }
16553 if (!is_capture_proxy (r))
16554 {
16555 /* Make sure the one we found is the one we want. */
16556 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16557 if (ctx != DECL_CONTEXT (r))
16558 r = NULL_TREE;
16559 }
16560 }
16561
16562 if (r)
16563 /* OK */;
16564 else
16565 {
16566 /* This can happen for a variable used in a
16567 late-specified return type of a local lambda, or for a
16568 local static or constant. Building a new VAR_DECL
16569 should be OK in all those cases. */
16570 r = tsubst_decl (t, args, complain);
16571 if (local_specializations)
16572 /* Avoid infinite recursion (79640). */
16573 register_local_specialization (r, t);
16574 if (decl_maybe_constant_var_p (r))
16575 {
16576 /* We can't call cp_finish_decl, so handle the
16577 initializer by hand. */
16578 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16579 complain, in_decl);
16580 if (!processing_template_decl)
16581 init = maybe_constant_init (init);
16582 if (processing_template_decl
16583 ? potential_constant_expression (init)
16584 : reduced_constant_expression_p (init))
16585 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16586 = TREE_CONSTANT (r) = true;
16587 DECL_INITIAL (r) = init;
16588 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16589 TREE_TYPE (r)
16590 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16591 complain, adc_variable_type);
16592 }
16593 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16594 || decl_constant_var_p (r)
16595 || seen_error ());
16596 if (!processing_template_decl
16597 && !TREE_STATIC (r))
16598 r = process_outer_var_ref (r, complain);
16599 }
16600 /* Remember this for subsequent uses. */
16601 if (local_specializations)
16602 register_local_specialization (r, t);
16603 }
16604 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16605 r = argument_pack_select_arg (r);
16606 }
16607 else
16608 r = t;
16609 if (!mark_used (r, complain))
16610 return error_mark_node;
16611 return r;
16612
16613 case NAMESPACE_DECL:
16614 return t;
16615
16616 case OVERLOAD:
16617 return t;
16618
16619 case BASELINK:
16620 return tsubst_baselink (t, current_nonlambda_class_type (),
16621 args, complain, in_decl);
16622
16623 case TEMPLATE_DECL:
16624 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16625 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16626 args, complain, in_decl);
16627 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16628 return tsubst (t, args, complain, in_decl);
16629 else if (DECL_CLASS_SCOPE_P (t)
16630 && uses_template_parms (DECL_CONTEXT (t)))
16631 {
16632 /* Template template argument like the following example need
16633 special treatment:
16634
16635 template <template <class> class TT> struct C {};
16636 template <class T> struct D {
16637 template <class U> struct E {};
16638 C<E> c; // #1
16639 };
16640 D<int> d; // #2
16641
16642 We are processing the template argument `E' in #1 for
16643 the template instantiation #2. Originally, `E' is a
16644 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16645 have to substitute this with one having context `D<int>'. */
16646
16647 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16648 if (dependent_scope_p (context))
16649 {
16650 /* When rewriting a constructor into a deduction guide, a
16651 non-dependent name can become dependent, so memtmpl<args>
16652 becomes context::template memtmpl<args>. */
16653 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16654 return build_qualified_name (type, context, DECL_NAME (t),
16655 /*template*/true);
16656 }
16657 return lookup_field (context, DECL_NAME(t), 0, false);
16658 }
16659 else
16660 /* Ordinary template template argument. */
16661 return t;
16662
16663 case NON_LVALUE_EXPR:
16664 case VIEW_CONVERT_EXPR:
16665 {
16666 /* Handle location wrappers by substituting the wrapped node
16667 first, *then* reusing the resulting type. Doing the type
16668 first ensures that we handle template parameters and
16669 parameter pack expansions. */
16670 if (location_wrapper_p (t))
16671 {
16672 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16673 complain, in_decl);
16674 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16675 }
16676 tree op = TREE_OPERAND (t, 0);
16677 if (code == VIEW_CONVERT_EXPR
16678 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16679 {
16680 /* Wrapper to make a C++20 template parameter object const. */
16681 op = tsubst_copy (op, args, complain, in_decl);
16682 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16683 {
16684 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16685 return build1 (code, type, op);
16686 }
16687 else
16688 {
16689 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16690 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16691 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16692 return op;
16693 }
16694 }
16695 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16696 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16697 {
16698 op = tsubst_copy (op, args, complain, in_decl);
16699 op = build1 (code, TREE_TYPE (op), op);
16700 REF_PARENTHESIZED_P (op) = true;
16701 return op;
16702 }
16703 /* We shouldn't see any other uses of these in templates. */
16704 gcc_unreachable ();
16705 }
16706
16707 case CAST_EXPR:
16708 case REINTERPRET_CAST_EXPR:
16709 case CONST_CAST_EXPR:
16710 case STATIC_CAST_EXPR:
16711 case DYNAMIC_CAST_EXPR:
16712 case IMPLICIT_CONV_EXPR:
16713 case CONVERT_EXPR:
16714 case NOP_EXPR:
16715 {
16716 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16717 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16718 return build1 (code, type, op0);
16719 }
16720
16721 case SIZEOF_EXPR:
16722 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16723 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16724 {
16725 tree expanded, op = TREE_OPERAND (t, 0);
16726 int len = 0;
16727
16728 if (SIZEOF_EXPR_TYPE_P (t))
16729 op = TREE_TYPE (op);
16730
16731 ++cp_unevaluated_operand;
16732 ++c_inhibit_evaluation_warnings;
16733 /* We only want to compute the number of arguments. */
16734 if (PACK_EXPANSION_P (op))
16735 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16736 else
16737 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16738 args, complain, in_decl);
16739 --cp_unevaluated_operand;
16740 --c_inhibit_evaluation_warnings;
16741
16742 if (TREE_CODE (expanded) == TREE_VEC)
16743 {
16744 len = TREE_VEC_LENGTH (expanded);
16745 /* Set TREE_USED for the benefit of -Wunused. */
16746 for (int i = 0; i < len; i++)
16747 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16748 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16749 }
16750
16751 if (expanded == error_mark_node)
16752 return error_mark_node;
16753 else if (PACK_EXPANSION_P (expanded)
16754 || (TREE_CODE (expanded) == TREE_VEC
16755 && pack_expansion_args_count (expanded)))
16756
16757 {
16758 if (PACK_EXPANSION_P (expanded))
16759 /* OK. */;
16760 else if (TREE_VEC_LENGTH (expanded) == 1)
16761 expanded = TREE_VEC_ELT (expanded, 0);
16762 else
16763 expanded = make_argument_pack (expanded);
16764
16765 if (TYPE_P (expanded))
16766 return cxx_sizeof_or_alignof_type (input_location,
16767 expanded, SIZEOF_EXPR,
16768 false,
16769 complain & tf_error);
16770 else
16771 return cxx_sizeof_or_alignof_expr (input_location,
16772 expanded, SIZEOF_EXPR,
16773 complain & tf_error);
16774 }
16775 else
16776 return build_int_cst (size_type_node, len);
16777 }
16778 if (SIZEOF_EXPR_TYPE_P (t))
16779 {
16780 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16781 args, complain, in_decl);
16782 r = build1 (NOP_EXPR, r, error_mark_node);
16783 r = build1 (SIZEOF_EXPR,
16784 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16785 SIZEOF_EXPR_TYPE_P (r) = 1;
16786 return r;
16787 }
16788 /* Fall through */
16789
16790 case INDIRECT_REF:
16791 case NEGATE_EXPR:
16792 case TRUTH_NOT_EXPR:
16793 case BIT_NOT_EXPR:
16794 case ADDR_EXPR:
16795 case UNARY_PLUS_EXPR: /* Unary + */
16796 case ALIGNOF_EXPR:
16797 case AT_ENCODE_EXPR:
16798 case ARROW_EXPR:
16799 case THROW_EXPR:
16800 case TYPEID_EXPR:
16801 case REALPART_EXPR:
16802 case IMAGPART_EXPR:
16803 case PAREN_EXPR:
16804 {
16805 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16806 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16807 r = build1 (code, type, op0);
16808 if (code == ALIGNOF_EXPR)
16809 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16810 return r;
16811 }
16812
16813 case COMPONENT_REF:
16814 {
16815 tree object;
16816 tree name;
16817
16818 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16819 name = TREE_OPERAND (t, 1);
16820 if (TREE_CODE (name) == BIT_NOT_EXPR)
16821 {
16822 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16823 complain, in_decl);
16824 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16825 }
16826 else if (TREE_CODE (name) == SCOPE_REF
16827 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16828 {
16829 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16830 complain, in_decl);
16831 name = TREE_OPERAND (name, 1);
16832 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16833 complain, in_decl);
16834 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16835 name = build_qualified_name (/*type=*/NULL_TREE,
16836 base, name,
16837 /*template_p=*/false);
16838 }
16839 else if (BASELINK_P (name))
16840 name = tsubst_baselink (name,
16841 non_reference (TREE_TYPE (object)),
16842 args, complain,
16843 in_decl);
16844 else
16845 name = tsubst_copy (name, args, complain, in_decl);
16846 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16847 }
16848
16849 case PLUS_EXPR:
16850 case MINUS_EXPR:
16851 case MULT_EXPR:
16852 case TRUNC_DIV_EXPR:
16853 case CEIL_DIV_EXPR:
16854 case FLOOR_DIV_EXPR:
16855 case ROUND_DIV_EXPR:
16856 case EXACT_DIV_EXPR:
16857 case BIT_AND_EXPR:
16858 case BIT_IOR_EXPR:
16859 case BIT_XOR_EXPR:
16860 case TRUNC_MOD_EXPR:
16861 case FLOOR_MOD_EXPR:
16862 case TRUTH_ANDIF_EXPR:
16863 case TRUTH_ORIF_EXPR:
16864 case TRUTH_AND_EXPR:
16865 case TRUTH_OR_EXPR:
16866 case RSHIFT_EXPR:
16867 case LSHIFT_EXPR:
16868 case EQ_EXPR:
16869 case NE_EXPR:
16870 case MAX_EXPR:
16871 case MIN_EXPR:
16872 case LE_EXPR:
16873 case GE_EXPR:
16874 case LT_EXPR:
16875 case GT_EXPR:
16876 case COMPOUND_EXPR:
16877 case DOTSTAR_EXPR:
16878 case MEMBER_REF:
16879 case PREDECREMENT_EXPR:
16880 case PREINCREMENT_EXPR:
16881 case POSTDECREMENT_EXPR:
16882 case POSTINCREMENT_EXPR:
16883 {
16884 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16885 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16886 return build_nt (code, op0, op1);
16887 }
16888
16889 case SCOPE_REF:
16890 {
16891 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16892 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16893 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16894 QUALIFIED_NAME_IS_TEMPLATE (t));
16895 }
16896
16897 case ARRAY_REF:
16898 {
16899 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16900 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16901 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16902 }
16903
16904 case CALL_EXPR:
16905 {
16906 int n = VL_EXP_OPERAND_LENGTH (t);
16907 tree result = build_vl_exp (CALL_EXPR, n);
16908 int i;
16909 for (i = 0; i < n; i++)
16910 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16911 complain, in_decl);
16912 return result;
16913 }
16914
16915 case COND_EXPR:
16916 case MODOP_EXPR:
16917 case PSEUDO_DTOR_EXPR:
16918 case VEC_PERM_EXPR:
16919 {
16920 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16921 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16922 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16923 r = build_nt (code, op0, op1, op2);
16924 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16925 return r;
16926 }
16927
16928 case NEW_EXPR:
16929 {
16930 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16931 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16932 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16933 r = build_nt (code, op0, op1, op2);
16934 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16935 return r;
16936 }
16937
16938 case DELETE_EXPR:
16939 {
16940 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16941 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16942 r = build_nt (code, op0, op1);
16943 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16944 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16945 return r;
16946 }
16947
16948 case TEMPLATE_ID_EXPR:
16949 {
16950 /* Substituted template arguments */
16951 tree fn = TREE_OPERAND (t, 0);
16952 tree targs = TREE_OPERAND (t, 1);
16953
16954 fn = tsubst_copy (fn, args, complain, in_decl);
16955 if (targs)
16956 targs = tsubst_template_args (targs, args, complain, in_decl);
16957
16958 return lookup_template_function (fn, targs);
16959 }
16960
16961 case TREE_LIST:
16962 {
16963 tree purpose, value, chain;
16964
16965 if (t == void_list_node)
16966 return t;
16967
16968 purpose = TREE_PURPOSE (t);
16969 if (purpose)
16970 purpose = tsubst_copy (purpose, args, complain, in_decl);
16971 value = TREE_VALUE (t);
16972 if (value)
16973 value = tsubst_copy (value, args, complain, in_decl);
16974 chain = TREE_CHAIN (t);
16975 if (chain && chain != void_type_node)
16976 chain = tsubst_copy (chain, args, complain, in_decl);
16977 if (purpose == TREE_PURPOSE (t)
16978 && value == TREE_VALUE (t)
16979 && chain == TREE_CHAIN (t))
16980 return t;
16981 return tree_cons (purpose, value, chain);
16982 }
16983
16984 case RECORD_TYPE:
16985 case UNION_TYPE:
16986 case ENUMERAL_TYPE:
16987 case INTEGER_TYPE:
16988 case TEMPLATE_TYPE_PARM:
16989 case TEMPLATE_TEMPLATE_PARM:
16990 case BOUND_TEMPLATE_TEMPLATE_PARM:
16991 case TEMPLATE_PARM_INDEX:
16992 case POINTER_TYPE:
16993 case REFERENCE_TYPE:
16994 case OFFSET_TYPE:
16995 case FUNCTION_TYPE:
16996 case METHOD_TYPE:
16997 case ARRAY_TYPE:
16998 case TYPENAME_TYPE:
16999 case UNBOUND_CLASS_TEMPLATE:
17000 case TYPEOF_TYPE:
17001 case DECLTYPE_TYPE:
17002 case TYPE_DECL:
17003 return tsubst (t, args, complain, in_decl);
17004
17005 case USING_DECL:
17006 t = DECL_NAME (t);
17007 /* Fall through. */
17008 case IDENTIFIER_NODE:
17009 if (IDENTIFIER_CONV_OP_P (t))
17010 {
17011 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17012 return make_conv_op_name (new_type);
17013 }
17014 else
17015 return t;
17016
17017 case CONSTRUCTOR:
17018 /* This is handled by tsubst_copy_and_build. */
17019 gcc_unreachable ();
17020
17021 case VA_ARG_EXPR:
17022 {
17023 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17024 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17025 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17026 }
17027
17028 case CLEANUP_POINT_EXPR:
17029 /* We shouldn't have built any of these during initial template
17030 generation. Instead, they should be built during instantiation
17031 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17032 gcc_unreachable ();
17033
17034 case OFFSET_REF:
17035 {
17036 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17037 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17038 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17039 r = build2 (code, type, op0, op1);
17040 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17041 if (!mark_used (TREE_OPERAND (r, 1), complain)
17042 && !(complain & tf_error))
17043 return error_mark_node;
17044 return r;
17045 }
17046
17047 case EXPR_PACK_EXPANSION:
17048 error ("invalid use of pack expansion expression");
17049 return error_mark_node;
17050
17051 case NONTYPE_ARGUMENT_PACK:
17052 error ("use %<...%> to expand argument pack");
17053 return error_mark_node;
17054
17055 case VOID_CST:
17056 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17057 return t;
17058
17059 case INTEGER_CST:
17060 case REAL_CST:
17061 case COMPLEX_CST:
17062 {
17063 /* Instantiate any typedefs in the type. */
17064 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17065 r = fold_convert (type, t);
17066 gcc_assert (TREE_CODE (r) == code);
17067 return r;
17068 }
17069
17070 case STRING_CST:
17071 {
17072 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17073 r = t;
17074 if (type != TREE_TYPE (t))
17075 {
17076 r = copy_node (t);
17077 TREE_TYPE (r) = type;
17078 }
17079 return r;
17080 }
17081
17082 case PTRMEM_CST:
17083 /* These can sometimes show up in a partial instantiation, but never
17084 involve template parms. */
17085 gcc_assert (!uses_template_parms (t));
17086 return t;
17087
17088 case UNARY_LEFT_FOLD_EXPR:
17089 return tsubst_unary_left_fold (t, args, complain, in_decl);
17090 case UNARY_RIGHT_FOLD_EXPR:
17091 return tsubst_unary_right_fold (t, args, complain, in_decl);
17092 case BINARY_LEFT_FOLD_EXPR:
17093 return tsubst_binary_left_fold (t, args, complain, in_decl);
17094 case BINARY_RIGHT_FOLD_EXPR:
17095 return tsubst_binary_right_fold (t, args, complain, in_decl);
17096 case PREDICT_EXPR:
17097 return t;
17098
17099 case DEBUG_BEGIN_STMT:
17100 /* ??? There's no point in copying it for now, but maybe some
17101 day it will contain more information, such as a pointer back
17102 to the containing function, inlined copy or so. */
17103 return t;
17104
17105 case CO_AWAIT_EXPR:
17106 return tsubst_expr (t, args, complain, in_decl,
17107 /*integral_constant_expression_p=*/false);
17108 break;
17109
17110 default:
17111 /* We shouldn't get here, but keep going if !flag_checking. */
17112 if (flag_checking)
17113 gcc_unreachable ();
17114 return t;
17115 }
17116 }
17117
17118 /* Helper function for tsubst_omp_clauses, used for instantiation of
17119 OMP_CLAUSE_DECL of clauses. */
17120
17121 static tree
17122 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17123 tree in_decl, tree *iterator_cache)
17124 {
17125 if (decl == NULL_TREE)
17126 return NULL_TREE;
17127
17128 /* Handle OpenMP iterators. */
17129 if (TREE_CODE (decl) == TREE_LIST
17130 && TREE_PURPOSE (decl)
17131 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17132 {
17133 tree ret;
17134 if (iterator_cache[0] == TREE_PURPOSE (decl))
17135 ret = iterator_cache[1];
17136 else
17137 {
17138 tree *tp = &ret;
17139 begin_scope (sk_omp, NULL);
17140 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17141 {
17142 *tp = copy_node (it);
17143 TREE_VEC_ELT (*tp, 0)
17144 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17145 TREE_VEC_ELT (*tp, 1)
17146 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17147 /*integral_constant_expression_p=*/false);
17148 TREE_VEC_ELT (*tp, 2)
17149 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17150 /*integral_constant_expression_p=*/false);
17151 TREE_VEC_ELT (*tp, 3)
17152 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17153 /*integral_constant_expression_p=*/false);
17154 TREE_CHAIN (*tp) = NULL_TREE;
17155 tp = &TREE_CHAIN (*tp);
17156 }
17157 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17158 iterator_cache[0] = TREE_PURPOSE (decl);
17159 iterator_cache[1] = ret;
17160 }
17161 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17162 args, complain,
17163 in_decl, NULL));
17164 }
17165
17166 /* Handle an OpenMP array section represented as a TREE_LIST (or
17167 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17168 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17169 TREE_LIST. We can handle it exactly the same as an array section
17170 (purpose, value, and a chain), even though the nomenclature
17171 (low_bound, length, etc) is different. */
17172 if (TREE_CODE (decl) == TREE_LIST)
17173 {
17174 tree low_bound
17175 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17176 /*integral_constant_expression_p=*/false);
17177 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17178 /*integral_constant_expression_p=*/false);
17179 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17180 in_decl, NULL);
17181 if (TREE_PURPOSE (decl) == low_bound
17182 && TREE_VALUE (decl) == length
17183 && TREE_CHAIN (decl) == chain)
17184 return decl;
17185 tree ret = tree_cons (low_bound, length, chain);
17186 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17187 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17188 return ret;
17189 }
17190 tree ret = tsubst_expr (decl, args, complain, in_decl,
17191 /*integral_constant_expression_p=*/false);
17192 /* Undo convert_from_reference tsubst_expr could have called. */
17193 if (decl
17194 && REFERENCE_REF_P (ret)
17195 && !REFERENCE_REF_P (decl))
17196 ret = TREE_OPERAND (ret, 0);
17197 return ret;
17198 }
17199
17200 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17201
17202 static tree
17203 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17204 tree args, tsubst_flags_t complain, tree in_decl)
17205 {
17206 tree new_clauses = NULL_TREE, nc, oc;
17207 tree linear_no_step = NULL_TREE;
17208 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17209
17210 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17211 {
17212 nc = copy_node (oc);
17213 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17214 new_clauses = nc;
17215
17216 switch (OMP_CLAUSE_CODE (nc))
17217 {
17218 case OMP_CLAUSE_LASTPRIVATE:
17219 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17220 {
17221 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17222 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17223 in_decl, /*integral_constant_expression_p=*/false);
17224 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17225 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17226 }
17227 /* FALLTHRU */
17228 case OMP_CLAUSE_PRIVATE:
17229 case OMP_CLAUSE_SHARED:
17230 case OMP_CLAUSE_FIRSTPRIVATE:
17231 case OMP_CLAUSE_COPYIN:
17232 case OMP_CLAUSE_COPYPRIVATE:
17233 case OMP_CLAUSE_UNIFORM:
17234 case OMP_CLAUSE_DEPEND:
17235 case OMP_CLAUSE_FROM:
17236 case OMP_CLAUSE_TO:
17237 case OMP_CLAUSE_MAP:
17238 case OMP_CLAUSE_NONTEMPORAL:
17239 case OMP_CLAUSE_USE_DEVICE_PTR:
17240 case OMP_CLAUSE_USE_DEVICE_ADDR:
17241 case OMP_CLAUSE_IS_DEVICE_PTR:
17242 case OMP_CLAUSE_INCLUSIVE:
17243 case OMP_CLAUSE_EXCLUSIVE:
17244 OMP_CLAUSE_DECL (nc)
17245 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17246 in_decl, iterator_cache);
17247 break;
17248 case OMP_CLAUSE_TILE:
17249 case OMP_CLAUSE_IF:
17250 case OMP_CLAUSE_NUM_THREADS:
17251 case OMP_CLAUSE_SCHEDULE:
17252 case OMP_CLAUSE_COLLAPSE:
17253 case OMP_CLAUSE_FINAL:
17254 case OMP_CLAUSE_DEVICE:
17255 case OMP_CLAUSE_DIST_SCHEDULE:
17256 case OMP_CLAUSE_NUM_TEAMS:
17257 case OMP_CLAUSE_THREAD_LIMIT:
17258 case OMP_CLAUSE_SAFELEN:
17259 case OMP_CLAUSE_SIMDLEN:
17260 case OMP_CLAUSE_NUM_TASKS:
17261 case OMP_CLAUSE_GRAINSIZE:
17262 case OMP_CLAUSE_PRIORITY:
17263 case OMP_CLAUSE_ORDERED:
17264 case OMP_CLAUSE_HINT:
17265 case OMP_CLAUSE_NUM_GANGS:
17266 case OMP_CLAUSE_NUM_WORKERS:
17267 case OMP_CLAUSE_VECTOR_LENGTH:
17268 case OMP_CLAUSE_WORKER:
17269 case OMP_CLAUSE_VECTOR:
17270 case OMP_CLAUSE_ASYNC:
17271 case OMP_CLAUSE_WAIT:
17272 OMP_CLAUSE_OPERAND (nc, 0)
17273 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17274 in_decl, /*integral_constant_expression_p=*/false);
17275 break;
17276 case OMP_CLAUSE_REDUCTION:
17277 case OMP_CLAUSE_IN_REDUCTION:
17278 case OMP_CLAUSE_TASK_REDUCTION:
17279 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17280 {
17281 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17282 if (TREE_CODE (placeholder) == SCOPE_REF)
17283 {
17284 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17285 complain, in_decl);
17286 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17287 = build_qualified_name (NULL_TREE, scope,
17288 TREE_OPERAND (placeholder, 1),
17289 false);
17290 }
17291 else
17292 gcc_assert (identifier_p (placeholder));
17293 }
17294 OMP_CLAUSE_DECL (nc)
17295 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17296 in_decl, NULL);
17297 break;
17298 case OMP_CLAUSE_GANG:
17299 case OMP_CLAUSE_ALIGNED:
17300 OMP_CLAUSE_DECL (nc)
17301 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17302 in_decl, NULL);
17303 OMP_CLAUSE_OPERAND (nc, 1)
17304 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17305 in_decl, /*integral_constant_expression_p=*/false);
17306 break;
17307 case OMP_CLAUSE_LINEAR:
17308 OMP_CLAUSE_DECL (nc)
17309 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17310 in_decl, NULL);
17311 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17312 {
17313 gcc_assert (!linear_no_step);
17314 linear_no_step = nc;
17315 }
17316 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17317 OMP_CLAUSE_LINEAR_STEP (nc)
17318 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17319 complain, in_decl, NULL);
17320 else
17321 OMP_CLAUSE_LINEAR_STEP (nc)
17322 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17323 in_decl,
17324 /*integral_constant_expression_p=*/false);
17325 break;
17326 case OMP_CLAUSE_NOWAIT:
17327 case OMP_CLAUSE_DEFAULT:
17328 case OMP_CLAUSE_UNTIED:
17329 case OMP_CLAUSE_MERGEABLE:
17330 case OMP_CLAUSE_INBRANCH:
17331 case OMP_CLAUSE_NOTINBRANCH:
17332 case OMP_CLAUSE_PROC_BIND:
17333 case OMP_CLAUSE_FOR:
17334 case OMP_CLAUSE_PARALLEL:
17335 case OMP_CLAUSE_SECTIONS:
17336 case OMP_CLAUSE_TASKGROUP:
17337 case OMP_CLAUSE_NOGROUP:
17338 case OMP_CLAUSE_THREADS:
17339 case OMP_CLAUSE_SIMD:
17340 case OMP_CLAUSE_DEFAULTMAP:
17341 case OMP_CLAUSE_ORDER:
17342 case OMP_CLAUSE_BIND:
17343 case OMP_CLAUSE_INDEPENDENT:
17344 case OMP_CLAUSE_AUTO:
17345 case OMP_CLAUSE_SEQ:
17346 case OMP_CLAUSE_IF_PRESENT:
17347 case OMP_CLAUSE_FINALIZE:
17348 break;
17349 default:
17350 gcc_unreachable ();
17351 }
17352 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17353 switch (OMP_CLAUSE_CODE (nc))
17354 {
17355 case OMP_CLAUSE_SHARED:
17356 case OMP_CLAUSE_PRIVATE:
17357 case OMP_CLAUSE_FIRSTPRIVATE:
17358 case OMP_CLAUSE_LASTPRIVATE:
17359 case OMP_CLAUSE_COPYPRIVATE:
17360 case OMP_CLAUSE_LINEAR:
17361 case OMP_CLAUSE_REDUCTION:
17362 case OMP_CLAUSE_IN_REDUCTION:
17363 case OMP_CLAUSE_TASK_REDUCTION:
17364 case OMP_CLAUSE_USE_DEVICE_PTR:
17365 case OMP_CLAUSE_USE_DEVICE_ADDR:
17366 case OMP_CLAUSE_IS_DEVICE_PTR:
17367 case OMP_CLAUSE_INCLUSIVE:
17368 case OMP_CLAUSE_EXCLUSIVE:
17369 /* tsubst_expr on SCOPE_REF results in returning
17370 finish_non_static_data_member result. Undo that here. */
17371 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17372 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17373 == IDENTIFIER_NODE))
17374 {
17375 tree t = OMP_CLAUSE_DECL (nc);
17376 tree v = t;
17377 while (v)
17378 switch (TREE_CODE (v))
17379 {
17380 case COMPONENT_REF:
17381 case MEM_REF:
17382 case INDIRECT_REF:
17383 CASE_CONVERT:
17384 case POINTER_PLUS_EXPR:
17385 v = TREE_OPERAND (v, 0);
17386 continue;
17387 case PARM_DECL:
17388 if (DECL_CONTEXT (v) == current_function_decl
17389 && DECL_ARTIFICIAL (v)
17390 && DECL_NAME (v) == this_identifier)
17391 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17392 /* FALLTHRU */
17393 default:
17394 v = NULL_TREE;
17395 break;
17396 }
17397 }
17398 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17399 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17400 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17401 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17402 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17403 {
17404 tree decl = OMP_CLAUSE_DECL (nc);
17405 if (VAR_P (decl))
17406 {
17407 retrofit_lang_decl (decl);
17408 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17409 }
17410 }
17411 break;
17412 default:
17413 break;
17414 }
17415 }
17416
17417 new_clauses = nreverse (new_clauses);
17418 if (ort != C_ORT_OMP_DECLARE_SIMD)
17419 {
17420 new_clauses = finish_omp_clauses (new_clauses, ort);
17421 if (linear_no_step)
17422 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17423 if (nc == linear_no_step)
17424 {
17425 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17426 break;
17427 }
17428 }
17429 return new_clauses;
17430 }
17431
17432 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17433
17434 static tree
17435 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17436 tree in_decl)
17437 {
17438 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17439
17440 tree purpose, value, chain;
17441
17442 if (t == NULL)
17443 return t;
17444
17445 if (TREE_CODE (t) != TREE_LIST)
17446 return tsubst_copy_and_build (t, args, complain, in_decl,
17447 /*function_p=*/false,
17448 /*integral_constant_expression_p=*/false);
17449
17450 if (t == void_list_node)
17451 return t;
17452
17453 purpose = TREE_PURPOSE (t);
17454 if (purpose)
17455 purpose = RECUR (purpose);
17456 value = TREE_VALUE (t);
17457 if (value)
17458 {
17459 if (TREE_CODE (value) != LABEL_DECL)
17460 value = RECUR (value);
17461 else
17462 {
17463 value = lookup_label (DECL_NAME (value));
17464 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17465 TREE_USED (value) = 1;
17466 }
17467 }
17468 chain = TREE_CHAIN (t);
17469 if (chain && chain != void_type_node)
17470 chain = RECUR (chain);
17471 return tree_cons (purpose, value, chain);
17472 #undef RECUR
17473 }
17474
17475 /* Used to temporarily communicate the list of #pragma omp parallel
17476 clauses to #pragma omp for instantiation if they are combined
17477 together. */
17478
17479 static tree *omp_parallel_combined_clauses;
17480
17481 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17482 tree *, unsigned int *);
17483
17484 /* Substitute one OMP_FOR iterator. */
17485
17486 static bool
17487 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17488 tree initv, tree condv, tree incrv, tree *clauses,
17489 tree args, tsubst_flags_t complain, tree in_decl,
17490 bool integral_constant_expression_p)
17491 {
17492 #define RECUR(NODE) \
17493 tsubst_expr ((NODE), args, complain, in_decl, \
17494 integral_constant_expression_p)
17495 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17496 bool ret = false;
17497
17498 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17499 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17500
17501 decl = TREE_OPERAND (init, 0);
17502 init = TREE_OPERAND (init, 1);
17503 tree decl_expr = NULL_TREE;
17504 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17505 if (range_for)
17506 {
17507 bool decomp = false;
17508 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17509 {
17510 tree v = DECL_VALUE_EXPR (decl);
17511 if (TREE_CODE (v) == ARRAY_REF
17512 && VAR_P (TREE_OPERAND (v, 0))
17513 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17514 {
17515 tree decomp_first = NULL_TREE;
17516 unsigned decomp_cnt = 0;
17517 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17518 maybe_push_decl (d);
17519 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17520 in_decl, &decomp_first, &decomp_cnt);
17521 decomp = true;
17522 if (d == error_mark_node)
17523 decl = error_mark_node;
17524 else
17525 for (unsigned int i = 0; i < decomp_cnt; i++)
17526 {
17527 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17528 {
17529 tree v = build_nt (ARRAY_REF, d,
17530 size_int (decomp_cnt - i - 1),
17531 NULL_TREE, NULL_TREE);
17532 SET_DECL_VALUE_EXPR (decomp_first, v);
17533 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17534 }
17535 fit_decomposition_lang_decl (decomp_first, d);
17536 decomp_first = DECL_CHAIN (decomp_first);
17537 }
17538 }
17539 }
17540 decl = tsubst_decl (decl, args, complain);
17541 if (!decomp)
17542 maybe_push_decl (decl);
17543 }
17544 else if (init && TREE_CODE (init) == DECL_EXPR)
17545 {
17546 /* We need to jump through some hoops to handle declarations in the
17547 init-statement, since we might need to handle auto deduction,
17548 but we need to keep control of initialization. */
17549 decl_expr = init;
17550 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17551 decl = tsubst_decl (decl, args, complain);
17552 }
17553 else
17554 {
17555 if (TREE_CODE (decl) == SCOPE_REF)
17556 {
17557 decl = RECUR (decl);
17558 if (TREE_CODE (decl) == COMPONENT_REF)
17559 {
17560 tree v = decl;
17561 while (v)
17562 switch (TREE_CODE (v))
17563 {
17564 case COMPONENT_REF:
17565 case MEM_REF:
17566 case INDIRECT_REF:
17567 CASE_CONVERT:
17568 case POINTER_PLUS_EXPR:
17569 v = TREE_OPERAND (v, 0);
17570 continue;
17571 case PARM_DECL:
17572 if (DECL_CONTEXT (v) == current_function_decl
17573 && DECL_ARTIFICIAL (v)
17574 && DECL_NAME (v) == this_identifier)
17575 {
17576 decl = TREE_OPERAND (decl, 1);
17577 decl = omp_privatize_field (decl, false);
17578 }
17579 /* FALLTHRU */
17580 default:
17581 v = NULL_TREE;
17582 break;
17583 }
17584 }
17585 }
17586 else
17587 decl = RECUR (decl);
17588 }
17589 if (init && TREE_CODE (init) == TREE_VEC)
17590 {
17591 init = copy_node (init);
17592 TREE_VEC_ELT (init, 0)
17593 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17594 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17595 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17596 }
17597 else
17598 init = RECUR (init);
17599
17600 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17601 {
17602 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17603 if (TREE_CODE (o) == TREE_LIST)
17604 TREE_VEC_ELT (orig_declv, i)
17605 = tree_cons (RECUR (TREE_PURPOSE (o)),
17606 RECUR (TREE_VALUE (o)),
17607 NULL_TREE);
17608 else
17609 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17610 }
17611
17612 if (range_for)
17613 {
17614 tree this_pre_body = NULL_TREE;
17615 tree orig_init = NULL_TREE;
17616 tree orig_decl = NULL_TREE;
17617 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17618 orig_init, cond, incr);
17619 if (orig_decl)
17620 {
17621 if (orig_declv == NULL_TREE)
17622 orig_declv = copy_node (declv);
17623 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17624 ret = true;
17625 }
17626 else if (orig_declv)
17627 TREE_VEC_ELT (orig_declv, i) = decl;
17628 }
17629
17630 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17631 if (!range_for && auto_node && init)
17632 TREE_TYPE (decl)
17633 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17634
17635 gcc_assert (!type_dependent_expression_p (decl));
17636
17637 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17638 {
17639 if (decl_expr)
17640 {
17641 /* Declare the variable, but don't let that initialize it. */
17642 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17643 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17644 RECUR (decl_expr);
17645 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17646 }
17647
17648 if (!range_for)
17649 {
17650 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17651 if (COMPARISON_CLASS_P (cond)
17652 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17653 {
17654 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17655 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17656 TREE_VEC_ELT (rhs, 0)
17657 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17658 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17659 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17660 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17661 lhs, rhs);
17662 }
17663 else
17664 cond = RECUR (cond);
17665 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17666 if (TREE_CODE (incr) == MODIFY_EXPR)
17667 {
17668 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17669 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17670 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17671 NOP_EXPR, rhs, complain);
17672 }
17673 else
17674 incr = RECUR (incr);
17675 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17676 TREE_VEC_ELT (orig_declv, i) = decl;
17677 }
17678 TREE_VEC_ELT (declv, i) = decl;
17679 TREE_VEC_ELT (initv, i) = init;
17680 TREE_VEC_ELT (condv, i) = cond;
17681 TREE_VEC_ELT (incrv, i) = incr;
17682 return ret;
17683 }
17684
17685 if (decl_expr)
17686 {
17687 /* Declare and initialize the variable. */
17688 RECUR (decl_expr);
17689 init = NULL_TREE;
17690 }
17691 else if (init)
17692 {
17693 tree *pc;
17694 int j;
17695 for (j = ((omp_parallel_combined_clauses == NULL
17696 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17697 {
17698 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17699 {
17700 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17701 && OMP_CLAUSE_DECL (*pc) == decl)
17702 break;
17703 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17704 && OMP_CLAUSE_DECL (*pc) == decl)
17705 {
17706 if (j)
17707 break;
17708 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17709 tree c = *pc;
17710 *pc = OMP_CLAUSE_CHAIN (c);
17711 OMP_CLAUSE_CHAIN (c) = *clauses;
17712 *clauses = c;
17713 }
17714 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17715 && OMP_CLAUSE_DECL (*pc) == decl)
17716 {
17717 error ("iteration variable %qD should not be firstprivate",
17718 decl);
17719 *pc = OMP_CLAUSE_CHAIN (*pc);
17720 }
17721 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17722 && OMP_CLAUSE_DECL (*pc) == decl)
17723 {
17724 error ("iteration variable %qD should not be reduction",
17725 decl);
17726 *pc = OMP_CLAUSE_CHAIN (*pc);
17727 }
17728 else
17729 pc = &OMP_CLAUSE_CHAIN (*pc);
17730 }
17731 if (*pc)
17732 break;
17733 }
17734 if (*pc == NULL_TREE)
17735 {
17736 tree c = build_omp_clause (input_location,
17737 TREE_CODE (t) == OMP_LOOP
17738 ? OMP_CLAUSE_LASTPRIVATE
17739 : OMP_CLAUSE_PRIVATE);
17740 OMP_CLAUSE_DECL (c) = decl;
17741 c = finish_omp_clauses (c, C_ORT_OMP);
17742 if (c)
17743 {
17744 OMP_CLAUSE_CHAIN (c) = *clauses;
17745 *clauses = c;
17746 }
17747 }
17748 }
17749 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17750 if (COMPARISON_CLASS_P (cond))
17751 {
17752 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17753 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17754 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17755 }
17756 else
17757 cond = RECUR (cond);
17758 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17759 switch (TREE_CODE (incr))
17760 {
17761 case PREINCREMENT_EXPR:
17762 case PREDECREMENT_EXPR:
17763 case POSTINCREMENT_EXPR:
17764 case POSTDECREMENT_EXPR:
17765 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17766 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17767 break;
17768 case MODIFY_EXPR:
17769 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17770 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17771 {
17772 tree rhs = TREE_OPERAND (incr, 1);
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 case MODOP_EXPR:
17784 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17785 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17786 {
17787 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17788 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17789 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17790 TREE_TYPE (decl), lhs,
17791 RECUR (TREE_OPERAND (incr, 2))));
17792 }
17793 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17794 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17795 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17796 {
17797 tree rhs = TREE_OPERAND (incr, 2);
17798 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17799 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17800 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17801 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17802 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17803 rhs0, rhs1));
17804 }
17805 else
17806 incr = RECUR (incr);
17807 break;
17808 default:
17809 incr = RECUR (incr);
17810 break;
17811 }
17812
17813 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17814 TREE_VEC_ELT (orig_declv, i) = decl;
17815 TREE_VEC_ELT (declv, i) = decl;
17816 TREE_VEC_ELT (initv, i) = init;
17817 TREE_VEC_ELT (condv, i) = cond;
17818 TREE_VEC_ELT (incrv, i) = incr;
17819 return false;
17820 #undef RECUR
17821 }
17822
17823 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17824 of OMP_TARGET's body. */
17825
17826 static tree
17827 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17828 {
17829 *walk_subtrees = 0;
17830 switch (TREE_CODE (*tp))
17831 {
17832 case OMP_TEAMS:
17833 return *tp;
17834 case BIND_EXPR:
17835 case STATEMENT_LIST:
17836 *walk_subtrees = 1;
17837 break;
17838 default:
17839 break;
17840 }
17841 return NULL_TREE;
17842 }
17843
17844 /* Helper function for tsubst_expr. For decomposition declaration
17845 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17846 also the corresponding decls representing the identifiers
17847 of the decomposition declaration. Return DECL if successful
17848 or error_mark_node otherwise, set *FIRST to the first decl
17849 in the list chained through DECL_CHAIN and *CNT to the number
17850 of such decls. */
17851
17852 static tree
17853 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17854 tsubst_flags_t complain, tree in_decl, tree *first,
17855 unsigned int *cnt)
17856 {
17857 tree decl2, decl3, prev = decl;
17858 *cnt = 0;
17859 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17860 for (decl2 = DECL_CHAIN (pattern_decl);
17861 decl2
17862 && VAR_P (decl2)
17863 && DECL_DECOMPOSITION_P (decl2)
17864 && DECL_NAME (decl2);
17865 decl2 = DECL_CHAIN (decl2))
17866 {
17867 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17868 {
17869 gcc_assert (errorcount);
17870 return error_mark_node;
17871 }
17872 (*cnt)++;
17873 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17874 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17875 tree v = DECL_VALUE_EXPR (decl2);
17876 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17877 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17878 decl3 = tsubst (decl2, args, complain, in_decl);
17879 SET_DECL_VALUE_EXPR (decl2, v);
17880 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17881 if (VAR_P (decl3))
17882 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17883 else
17884 {
17885 gcc_assert (errorcount);
17886 decl = error_mark_node;
17887 continue;
17888 }
17889 maybe_push_decl (decl3);
17890 if (error_operand_p (decl3))
17891 decl = error_mark_node;
17892 else if (decl != error_mark_node
17893 && DECL_CHAIN (decl3) != prev
17894 && decl != prev)
17895 {
17896 gcc_assert (errorcount);
17897 decl = error_mark_node;
17898 }
17899 else
17900 prev = decl3;
17901 }
17902 *first = prev;
17903 return decl;
17904 }
17905
17906 /* Return the proper local_specialization for init-capture pack DECL. */
17907
17908 static tree
17909 lookup_init_capture_pack (tree decl)
17910 {
17911 /* We handle normal pack captures by forwarding to the specialization of the
17912 captured parameter. We can't do that for pack init-captures; we need them
17913 to have their own local_specialization. We created the individual
17914 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17915 when we process the DECL_EXPR for the pack init-capture in the template.
17916 So, how do we find them? We don't know the capture proxy pack when
17917 building the individual resulting proxies, and we don't know the
17918 individual proxies when instantiating the pack. What we have in common is
17919 the FIELD_DECL.
17920
17921 So...when we instantiate the FIELD_DECL, we stick the result in
17922 local_specializations. Then at the DECL_EXPR we look up that result, see
17923 how many elements it has, synthesize the names, and look them up. */
17924
17925 tree cname = DECL_NAME (decl);
17926 tree val = DECL_VALUE_EXPR (decl);
17927 tree field = TREE_OPERAND (val, 1);
17928 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17929 tree fpack = retrieve_local_specialization (field);
17930 if (fpack == error_mark_node)
17931 return error_mark_node;
17932
17933 int len = 1;
17934 tree vec = NULL_TREE;
17935 tree r = NULL_TREE;
17936 if (TREE_CODE (fpack) == TREE_VEC)
17937 {
17938 len = TREE_VEC_LENGTH (fpack);
17939 vec = make_tree_vec (len);
17940 r = make_node (NONTYPE_ARGUMENT_PACK);
17941 SET_ARGUMENT_PACK_ARGS (r, vec);
17942 }
17943 for (int i = 0; i < len; ++i)
17944 {
17945 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17946 tree elt = lookup_name (ename);
17947 if (vec)
17948 TREE_VEC_ELT (vec, i) = elt;
17949 else
17950 r = elt;
17951 }
17952 return r;
17953 }
17954
17955 /* Like tsubst_copy for expressions, etc. but also does semantic
17956 processing. */
17957
17958 tree
17959 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17960 bool integral_constant_expression_p)
17961 {
17962 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17963 #define RECUR(NODE) \
17964 tsubst_expr ((NODE), args, complain, in_decl, \
17965 integral_constant_expression_p)
17966
17967 tree stmt, tmp;
17968 tree r;
17969 location_t loc;
17970
17971 if (t == NULL_TREE || t == error_mark_node)
17972 return t;
17973
17974 loc = input_location;
17975 if (location_t eloc = cp_expr_location (t))
17976 input_location = eloc;
17977 if (STATEMENT_CODE_P (TREE_CODE (t)))
17978 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17979
17980 switch (TREE_CODE (t))
17981 {
17982 case STATEMENT_LIST:
17983 {
17984 tree_stmt_iterator i;
17985 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17986 RECUR (tsi_stmt (i));
17987 break;
17988 }
17989
17990 case CTOR_INITIALIZER:
17991 finish_mem_initializers (tsubst_initializer_list
17992 (TREE_OPERAND (t, 0), args));
17993 break;
17994
17995 case RETURN_EXPR:
17996 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17997 break;
17998
17999 case CO_RETURN_EXPR:
18000 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18001 break;
18002
18003 case CO_YIELD_EXPR:
18004 stmt = finish_co_yield_expr (input_location,
18005 RECUR (TREE_OPERAND (t, 0)));
18006 RETURN (stmt);
18007 break;
18008
18009 case CO_AWAIT_EXPR:
18010 stmt = finish_co_await_expr (input_location,
18011 RECUR (TREE_OPERAND (t, 0)));
18012 RETURN (stmt);
18013 break;
18014
18015 case EXPR_STMT:
18016 tmp = RECUR (EXPR_STMT_EXPR (t));
18017 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18018 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18019 else
18020 finish_expr_stmt (tmp);
18021 break;
18022
18023 case USING_STMT:
18024 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18025 break;
18026
18027 case DECL_EXPR:
18028 {
18029 tree decl, pattern_decl;
18030 tree init;
18031
18032 pattern_decl = decl = DECL_EXPR_DECL (t);
18033 if (TREE_CODE (decl) == LABEL_DECL)
18034 finish_label_decl (DECL_NAME (decl));
18035 else if (TREE_CODE (decl) == USING_DECL)
18036 {
18037 tree scope = USING_DECL_SCOPE (decl);
18038 tree name = DECL_NAME (decl);
18039
18040 scope = tsubst (scope, args, complain, in_decl);
18041 finish_nonmember_using_decl (scope, name);
18042 }
18043 else if (is_capture_proxy (decl)
18044 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18045 {
18046 /* We're in tsubst_lambda_expr, we've already inserted a new
18047 capture proxy, so look it up and register it. */
18048 tree inst;
18049 if (!DECL_PACK_P (decl))
18050 {
18051 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18052 LOOK_want::HIDDEN_LAMBDA);
18053 gcc_assert (inst != decl && is_capture_proxy (inst));
18054 }
18055 else if (is_normal_capture_proxy (decl))
18056 {
18057 inst = (retrieve_local_specialization
18058 (DECL_CAPTURED_VARIABLE (decl)));
18059 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18060 || DECL_PACK_P (inst));
18061 }
18062 else
18063 inst = lookup_init_capture_pack (decl);
18064
18065 register_local_specialization (inst, decl);
18066 break;
18067 }
18068 else if (DECL_PRETTY_FUNCTION_P (decl))
18069 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18070 DECL_NAME (decl),
18071 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18072 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18073 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18074 /* Don't copy the old closure; we'll create a new one in
18075 tsubst_lambda_expr. */
18076 break;
18077 else
18078 {
18079 init = DECL_INITIAL (decl);
18080 decl = tsubst (decl, args, complain, in_decl);
18081 if (decl != error_mark_node)
18082 {
18083 /* By marking the declaration as instantiated, we avoid
18084 trying to instantiate it. Since instantiate_decl can't
18085 handle local variables, and since we've already done
18086 all that needs to be done, that's the right thing to
18087 do. */
18088 if (VAR_P (decl))
18089 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18090 if (VAR_P (decl) && !DECL_NAME (decl)
18091 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18092 /* Anonymous aggregates are a special case. */
18093 finish_anon_union (decl);
18094 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18095 {
18096 DECL_CONTEXT (decl) = current_function_decl;
18097 if (DECL_NAME (decl) == this_identifier)
18098 {
18099 tree lam = DECL_CONTEXT (current_function_decl);
18100 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18101 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18102 }
18103 insert_capture_proxy (decl);
18104 }
18105 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18106 /* We already did a pushtag. */;
18107 else if (TREE_CODE (decl) == FUNCTION_DECL
18108 && DECL_LOCAL_DECL_P (decl)
18109 && DECL_OMP_DECLARE_REDUCTION_P (decl))
18110 {
18111 DECL_CONTEXT (decl) = current_function_decl;
18112 pushdecl (decl);
18113 if (cp_check_omp_declare_reduction (decl))
18114 instantiate_body (pattern_decl, args, decl, true);
18115 }
18116 else
18117 {
18118 bool const_init = false;
18119 unsigned int cnt = 0;
18120 tree first = NULL_TREE, ndecl = error_mark_node;
18121 maybe_push_decl (decl);
18122
18123 if (VAR_P (decl)
18124 && DECL_DECOMPOSITION_P (decl)
18125 && TREE_TYPE (pattern_decl) != error_mark_node)
18126 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18127 complain, in_decl, &first,
18128 &cnt);
18129
18130 init = tsubst_init (init, decl, args, complain, in_decl);
18131
18132 if (VAR_P (decl))
18133 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18134 (pattern_decl));
18135
18136 if (ndecl != error_mark_node)
18137 cp_maybe_mangle_decomp (ndecl, first, cnt);
18138
18139 /* In a non-template function, VLA type declarations are
18140 handled in grokdeclarator; for templates, handle them
18141 now. */
18142 predeclare_vla (decl);
18143
18144 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18145
18146 if (ndecl != error_mark_node)
18147 cp_finish_decomp (ndecl, first, cnt);
18148 }
18149 }
18150 }
18151
18152 break;
18153 }
18154
18155 case FOR_STMT:
18156 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18157 RECUR (FOR_INIT_STMT (t));
18158 finish_init_stmt (stmt);
18159 tmp = RECUR (FOR_COND (t));
18160 finish_for_cond (tmp, stmt, false, 0);
18161 tmp = RECUR (FOR_EXPR (t));
18162 finish_for_expr (tmp, stmt);
18163 {
18164 bool prev = note_iteration_stmt_body_start ();
18165 RECUR (FOR_BODY (t));
18166 note_iteration_stmt_body_end (prev);
18167 }
18168 finish_for_stmt (stmt);
18169 break;
18170
18171 case RANGE_FOR_STMT:
18172 {
18173 /* Construct another range_for, if this is not a final
18174 substitution (for inside a generic lambda of a
18175 template). Otherwise convert to a regular for. */
18176 tree decl, expr;
18177 stmt = (processing_template_decl
18178 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18179 : begin_for_stmt (NULL_TREE, NULL_TREE));
18180 RECUR (RANGE_FOR_INIT_STMT (t));
18181 decl = RANGE_FOR_DECL (t);
18182 decl = tsubst (decl, args, complain, in_decl);
18183 maybe_push_decl (decl);
18184 expr = RECUR (RANGE_FOR_EXPR (t));
18185
18186 tree decomp_first = NULL_TREE;
18187 unsigned decomp_cnt = 0;
18188 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18189 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18190 complain, in_decl,
18191 &decomp_first, &decomp_cnt);
18192
18193 if (processing_template_decl)
18194 {
18195 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18196 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18197 finish_range_for_decl (stmt, decl, expr);
18198 if (decomp_first && decl != error_mark_node)
18199 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18200 }
18201 else
18202 {
18203 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18204 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18205 stmt = cp_convert_range_for (stmt, decl, expr,
18206 decomp_first, decomp_cnt,
18207 RANGE_FOR_IVDEP (t), unroll);
18208 }
18209
18210 bool prev = note_iteration_stmt_body_start ();
18211 RECUR (RANGE_FOR_BODY (t));
18212 note_iteration_stmt_body_end (prev);
18213 finish_for_stmt (stmt);
18214 }
18215 break;
18216
18217 case WHILE_STMT:
18218 stmt = begin_while_stmt ();
18219 tmp = RECUR (WHILE_COND (t));
18220 finish_while_stmt_cond (tmp, stmt, false, 0);
18221 {
18222 bool prev = note_iteration_stmt_body_start ();
18223 RECUR (WHILE_BODY (t));
18224 note_iteration_stmt_body_end (prev);
18225 }
18226 finish_while_stmt (stmt);
18227 break;
18228
18229 case DO_STMT:
18230 stmt = begin_do_stmt ();
18231 {
18232 bool prev = note_iteration_stmt_body_start ();
18233 RECUR (DO_BODY (t));
18234 note_iteration_stmt_body_end (prev);
18235 }
18236 finish_do_body (stmt);
18237 tmp = RECUR (DO_COND (t));
18238 finish_do_stmt (tmp, stmt, false, 0);
18239 break;
18240
18241 case IF_STMT:
18242 stmt = begin_if_stmt ();
18243 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18244 if (IF_STMT_CONSTEXPR_P (t))
18245 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18246 tmp = RECUR (IF_COND (t));
18247 tmp = finish_if_stmt_cond (tmp, stmt);
18248 if (IF_STMT_CONSTEXPR_P (t)
18249 && instantiation_dependent_expression_p (tmp))
18250 {
18251 /* We're partially instantiating a generic lambda, but the condition
18252 of the constexpr if is still dependent. Don't substitute into the
18253 branches now, just remember the template arguments. */
18254 do_poplevel (IF_SCOPE (stmt));
18255 IF_COND (stmt) = IF_COND (t);
18256 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18257 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18258 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18259 add_stmt (stmt);
18260 break;
18261 }
18262 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18263 /* Don't instantiate the THEN_CLAUSE. */;
18264 else
18265 {
18266 tree folded = fold_non_dependent_expr (tmp, complain);
18267 bool inhibit = integer_zerop (folded);
18268 if (inhibit)
18269 ++c_inhibit_evaluation_warnings;
18270 RECUR (THEN_CLAUSE (t));
18271 if (inhibit)
18272 --c_inhibit_evaluation_warnings;
18273 }
18274 finish_then_clause (stmt);
18275
18276 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18277 /* Don't instantiate the ELSE_CLAUSE. */;
18278 else if (ELSE_CLAUSE (t))
18279 {
18280 tree folded = fold_non_dependent_expr (tmp, complain);
18281 bool inhibit = integer_nonzerop (folded);
18282 begin_else_clause (stmt);
18283 if (inhibit)
18284 ++c_inhibit_evaluation_warnings;
18285 RECUR (ELSE_CLAUSE (t));
18286 if (inhibit)
18287 --c_inhibit_evaluation_warnings;
18288 finish_else_clause (stmt);
18289 }
18290
18291 finish_if_stmt (stmt);
18292 break;
18293
18294 case BIND_EXPR:
18295 if (BIND_EXPR_BODY_BLOCK (t))
18296 stmt = begin_function_body ();
18297 else
18298 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18299 ? BCS_TRY_BLOCK : 0);
18300
18301 RECUR (BIND_EXPR_BODY (t));
18302
18303 if (BIND_EXPR_BODY_BLOCK (t))
18304 finish_function_body (stmt);
18305 else
18306 finish_compound_stmt (stmt);
18307 break;
18308
18309 case BREAK_STMT:
18310 finish_break_stmt ();
18311 break;
18312
18313 case CONTINUE_STMT:
18314 finish_continue_stmt ();
18315 break;
18316
18317 case SWITCH_STMT:
18318 stmt = begin_switch_stmt ();
18319 tmp = RECUR (SWITCH_STMT_COND (t));
18320 finish_switch_cond (tmp, stmt);
18321 RECUR (SWITCH_STMT_BODY (t));
18322 finish_switch_stmt (stmt);
18323 break;
18324
18325 case CASE_LABEL_EXPR:
18326 {
18327 tree decl = CASE_LABEL (t);
18328 tree low = RECUR (CASE_LOW (t));
18329 tree high = RECUR (CASE_HIGH (t));
18330 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18331 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18332 {
18333 tree label = CASE_LABEL (l);
18334 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18335 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18336 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18337 }
18338 }
18339 break;
18340
18341 case LABEL_EXPR:
18342 {
18343 tree decl = LABEL_EXPR_LABEL (t);
18344 tree label;
18345
18346 label = finish_label_stmt (DECL_NAME (decl));
18347 if (TREE_CODE (label) == LABEL_DECL)
18348 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18349 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18350 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18351 }
18352 break;
18353
18354 case GOTO_EXPR:
18355 tmp = GOTO_DESTINATION (t);
18356 if (TREE_CODE (tmp) != LABEL_DECL)
18357 /* Computed goto's must be tsubst'd into. On the other hand,
18358 non-computed gotos must not be; the identifier in question
18359 will have no binding. */
18360 tmp = RECUR (tmp);
18361 else
18362 tmp = DECL_NAME (tmp);
18363 finish_goto_stmt (tmp);
18364 break;
18365
18366 case ASM_EXPR:
18367 {
18368 tree string = RECUR (ASM_STRING (t));
18369 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18370 complain, in_decl);
18371 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18372 complain, in_decl);
18373 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18374 complain, in_decl);
18375 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18376 complain, in_decl);
18377 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18378 outputs, inputs, clobbers, labels,
18379 ASM_INLINE_P (t));
18380 tree asm_expr = tmp;
18381 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18382 asm_expr = TREE_OPERAND (asm_expr, 0);
18383 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18384 }
18385 break;
18386
18387 case TRY_BLOCK:
18388 if (CLEANUP_P (t))
18389 {
18390 stmt = begin_try_block ();
18391 RECUR (TRY_STMTS (t));
18392 finish_cleanup_try_block (stmt);
18393 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18394 }
18395 else
18396 {
18397 tree compound_stmt = NULL_TREE;
18398
18399 if (FN_TRY_BLOCK_P (t))
18400 stmt = begin_function_try_block (&compound_stmt);
18401 else
18402 stmt = begin_try_block ();
18403
18404 RECUR (TRY_STMTS (t));
18405
18406 if (FN_TRY_BLOCK_P (t))
18407 finish_function_try_block (stmt);
18408 else
18409 finish_try_block (stmt);
18410
18411 RECUR (TRY_HANDLERS (t));
18412 if (FN_TRY_BLOCK_P (t))
18413 finish_function_handler_sequence (stmt, compound_stmt);
18414 else
18415 finish_handler_sequence (stmt);
18416 }
18417 break;
18418
18419 case HANDLER:
18420 {
18421 tree decl = HANDLER_PARMS (t);
18422
18423 if (decl)
18424 {
18425 decl = tsubst (decl, args, complain, in_decl);
18426 /* Prevent instantiate_decl from trying to instantiate
18427 this variable. We've already done all that needs to be
18428 done. */
18429 if (decl != error_mark_node)
18430 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18431 }
18432 stmt = begin_handler ();
18433 finish_handler_parms (decl, stmt);
18434 RECUR (HANDLER_BODY (t));
18435 finish_handler (stmt);
18436 }
18437 break;
18438
18439 case TAG_DEFN:
18440 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18441 if (CLASS_TYPE_P (tmp))
18442 {
18443 /* Local classes are not independent templates; they are
18444 instantiated along with their containing function. And this
18445 way we don't have to deal with pushing out of one local class
18446 to instantiate a member of another local class. */
18447 /* Closures are handled by the LAMBDA_EXPR. */
18448 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18449 complete_type (tmp);
18450 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18451 if ((VAR_P (fld)
18452 || (TREE_CODE (fld) == FUNCTION_DECL
18453 && !DECL_ARTIFICIAL (fld)))
18454 && DECL_TEMPLATE_INSTANTIATION (fld))
18455 instantiate_decl (fld, /*defer_ok=*/false,
18456 /*expl_inst_class=*/false);
18457 }
18458 break;
18459
18460 case STATIC_ASSERT:
18461 {
18462 tree condition;
18463
18464 ++c_inhibit_evaluation_warnings;
18465 condition =
18466 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18467 args,
18468 complain, in_decl,
18469 /*integral_constant_expression_p=*/true);
18470 --c_inhibit_evaluation_warnings;
18471
18472 finish_static_assert (condition,
18473 STATIC_ASSERT_MESSAGE (t),
18474 STATIC_ASSERT_SOURCE_LOCATION (t),
18475 /*member_p=*/false);
18476 }
18477 break;
18478
18479 case OACC_KERNELS:
18480 case OACC_PARALLEL:
18481 case OACC_SERIAL:
18482 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18483 in_decl);
18484 stmt = begin_omp_parallel ();
18485 RECUR (OMP_BODY (t));
18486 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18487 break;
18488
18489 case OMP_PARALLEL:
18490 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18491 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18492 complain, in_decl);
18493 if (OMP_PARALLEL_COMBINED (t))
18494 omp_parallel_combined_clauses = &tmp;
18495 stmt = begin_omp_parallel ();
18496 RECUR (OMP_PARALLEL_BODY (t));
18497 gcc_assert (omp_parallel_combined_clauses == NULL);
18498 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18499 = OMP_PARALLEL_COMBINED (t);
18500 pop_omp_privatization_clauses (r);
18501 break;
18502
18503 case OMP_TASK:
18504 if (OMP_TASK_BODY (t) == NULL_TREE)
18505 {
18506 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18507 complain, in_decl);
18508 t = copy_node (t);
18509 OMP_TASK_CLAUSES (t) = tmp;
18510 add_stmt (t);
18511 break;
18512 }
18513 r = push_omp_privatization_clauses (false);
18514 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18515 complain, in_decl);
18516 stmt = begin_omp_task ();
18517 RECUR (OMP_TASK_BODY (t));
18518 finish_omp_task (tmp, stmt);
18519 pop_omp_privatization_clauses (r);
18520 break;
18521
18522 case OMP_FOR:
18523 case OMP_LOOP:
18524 case OMP_SIMD:
18525 case OMP_DISTRIBUTE:
18526 case OMP_TASKLOOP:
18527 case OACC_LOOP:
18528 {
18529 tree clauses, body, pre_body;
18530 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18531 tree orig_declv = NULL_TREE;
18532 tree incrv = NULL_TREE;
18533 enum c_omp_region_type ort = C_ORT_OMP;
18534 bool any_range_for = false;
18535 int i;
18536
18537 if (TREE_CODE (t) == OACC_LOOP)
18538 ort = C_ORT_ACC;
18539
18540 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18541 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18542 in_decl);
18543 if (OMP_FOR_INIT (t) != NULL_TREE)
18544 {
18545 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18546 if (OMP_FOR_ORIG_DECLS (t))
18547 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18548 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18549 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18550 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18551 }
18552
18553 keep_next_level (true);
18554 stmt = begin_omp_structured_block ();
18555
18556 pre_body = push_stmt_list ();
18557 RECUR (OMP_FOR_PRE_BODY (t));
18558 pre_body = pop_stmt_list (pre_body);
18559
18560 if (OMP_FOR_INIT (t) != NULL_TREE)
18561 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18562 any_range_for
18563 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18564 condv, incrv, &clauses, args,
18565 complain, in_decl,
18566 integral_constant_expression_p);
18567 omp_parallel_combined_clauses = NULL;
18568
18569 if (any_range_for)
18570 {
18571 gcc_assert (orig_declv);
18572 body = begin_omp_structured_block ();
18573 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18574 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18575 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18576 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18577 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18578 TREE_VEC_ELT (declv, i));
18579 }
18580 else
18581 body = push_stmt_list ();
18582 RECUR (OMP_FOR_BODY (t));
18583 if (any_range_for)
18584 body = finish_omp_structured_block (body);
18585 else
18586 body = pop_stmt_list (body);
18587
18588 if (OMP_FOR_INIT (t) != NULL_TREE)
18589 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18590 orig_declv, initv, condv, incrv, body, pre_body,
18591 NULL, clauses);
18592 else
18593 {
18594 t = make_node (TREE_CODE (t));
18595 TREE_TYPE (t) = void_type_node;
18596 OMP_FOR_BODY (t) = body;
18597 OMP_FOR_PRE_BODY (t) = pre_body;
18598 OMP_FOR_CLAUSES (t) = clauses;
18599 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18600 add_stmt (t);
18601 }
18602
18603 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18604 t));
18605 pop_omp_privatization_clauses (r);
18606 }
18607 break;
18608
18609 case OMP_SECTIONS:
18610 omp_parallel_combined_clauses = NULL;
18611 /* FALLTHRU */
18612 case OMP_SINGLE:
18613 case OMP_TEAMS:
18614 case OMP_CRITICAL:
18615 case OMP_TASKGROUP:
18616 case OMP_SCAN:
18617 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18618 && OMP_TEAMS_COMBINED (t));
18619 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18620 in_decl);
18621 if (TREE_CODE (t) == OMP_TEAMS)
18622 {
18623 keep_next_level (true);
18624 stmt = begin_omp_structured_block ();
18625 RECUR (OMP_BODY (t));
18626 stmt = finish_omp_structured_block (stmt);
18627 }
18628 else
18629 {
18630 stmt = push_stmt_list ();
18631 RECUR (OMP_BODY (t));
18632 stmt = pop_stmt_list (stmt);
18633 }
18634
18635 if (TREE_CODE (t) == OMP_CRITICAL
18636 && tmp != NULL_TREE
18637 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18638 {
18639 error_at (OMP_CLAUSE_LOCATION (tmp),
18640 "%<#pragma omp critical%> with %<hint%> clause requires "
18641 "a name, except when %<omp_sync_hint_none%> is used");
18642 RETURN (error_mark_node);
18643 }
18644 t = copy_node (t);
18645 OMP_BODY (t) = stmt;
18646 OMP_CLAUSES (t) = tmp;
18647 add_stmt (t);
18648 pop_omp_privatization_clauses (r);
18649 break;
18650
18651 case OMP_DEPOBJ:
18652 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18653 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18654 {
18655 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18656 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18657 {
18658 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18659 args, complain, in_decl);
18660 if (tmp == NULL_TREE)
18661 tmp = error_mark_node;
18662 }
18663 else
18664 {
18665 kind = (enum omp_clause_depend_kind)
18666 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18667 tmp = NULL_TREE;
18668 }
18669 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18670 }
18671 else
18672 finish_omp_depobj (EXPR_LOCATION (t), r,
18673 OMP_CLAUSE_DEPEND_SOURCE,
18674 OMP_DEPOBJ_CLAUSES (t));
18675 break;
18676
18677 case OACC_DATA:
18678 case OMP_TARGET_DATA:
18679 case OMP_TARGET:
18680 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18681 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18682 in_decl);
18683 keep_next_level (true);
18684 stmt = begin_omp_structured_block ();
18685
18686 RECUR (OMP_BODY (t));
18687 stmt = finish_omp_structured_block (stmt);
18688
18689 t = copy_node (t);
18690 OMP_BODY (t) = stmt;
18691 OMP_CLAUSES (t) = tmp;
18692 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18693 {
18694 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18695 if (teams)
18696 {
18697 /* For combined target teams, ensure the num_teams and
18698 thread_limit clause expressions are evaluated on the host,
18699 before entering the target construct. */
18700 tree c;
18701 for (c = OMP_TEAMS_CLAUSES (teams);
18702 c; c = OMP_CLAUSE_CHAIN (c))
18703 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18704 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18705 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18706 {
18707 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18708 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18709 if (expr == error_mark_node)
18710 continue;
18711 tmp = TARGET_EXPR_SLOT (expr);
18712 add_stmt (expr);
18713 OMP_CLAUSE_OPERAND (c, 0) = expr;
18714 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18715 OMP_CLAUSE_FIRSTPRIVATE);
18716 OMP_CLAUSE_DECL (tc) = tmp;
18717 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18718 OMP_TARGET_CLAUSES (t) = tc;
18719 }
18720 }
18721 }
18722 add_stmt (t);
18723 break;
18724
18725 case OACC_DECLARE:
18726 t = copy_node (t);
18727 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18728 complain, in_decl);
18729 OACC_DECLARE_CLAUSES (t) = tmp;
18730 add_stmt (t);
18731 break;
18732
18733 case OMP_TARGET_UPDATE:
18734 case OMP_TARGET_ENTER_DATA:
18735 case OMP_TARGET_EXIT_DATA:
18736 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18737 complain, in_decl);
18738 t = copy_node (t);
18739 OMP_STANDALONE_CLAUSES (t) = tmp;
18740 add_stmt (t);
18741 break;
18742
18743 case OACC_ENTER_DATA:
18744 case OACC_EXIT_DATA:
18745 case OACC_UPDATE:
18746 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18747 complain, in_decl);
18748 t = copy_node (t);
18749 OMP_STANDALONE_CLAUSES (t) = tmp;
18750 add_stmt (t);
18751 break;
18752
18753 case OMP_ORDERED:
18754 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18755 complain, in_decl);
18756 stmt = push_stmt_list ();
18757 RECUR (OMP_BODY (t));
18758 stmt = pop_stmt_list (stmt);
18759
18760 t = copy_node (t);
18761 OMP_BODY (t) = stmt;
18762 OMP_ORDERED_CLAUSES (t) = tmp;
18763 add_stmt (t);
18764 break;
18765
18766 case OMP_MASTER:
18767 omp_parallel_combined_clauses = NULL;
18768 /* FALLTHRU */
18769 case OMP_SECTION:
18770 stmt = push_stmt_list ();
18771 RECUR (OMP_BODY (t));
18772 stmt = pop_stmt_list (stmt);
18773
18774 t = copy_node (t);
18775 OMP_BODY (t) = stmt;
18776 add_stmt (t);
18777 break;
18778
18779 case OMP_ATOMIC:
18780 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18781 tmp = NULL_TREE;
18782 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18783 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18784 complain, in_decl);
18785 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18786 {
18787 tree op1 = TREE_OPERAND (t, 1);
18788 tree rhs1 = NULL_TREE;
18789 tree lhs, rhs;
18790 if (TREE_CODE (op1) == COMPOUND_EXPR)
18791 {
18792 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18793 op1 = TREE_OPERAND (op1, 1);
18794 }
18795 lhs = RECUR (TREE_OPERAND (op1, 0));
18796 rhs = RECUR (TREE_OPERAND (op1, 1));
18797 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18798 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18799 OMP_ATOMIC_MEMORY_ORDER (t));
18800 }
18801 else
18802 {
18803 tree op1 = TREE_OPERAND (t, 1);
18804 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18805 tree rhs1 = NULL_TREE;
18806 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18807 enum tree_code opcode = NOP_EXPR;
18808 if (code == OMP_ATOMIC_READ)
18809 {
18810 v = RECUR (TREE_OPERAND (op1, 0));
18811 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18812 }
18813 else if (code == OMP_ATOMIC_CAPTURE_OLD
18814 || code == OMP_ATOMIC_CAPTURE_NEW)
18815 {
18816 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18817 v = RECUR (TREE_OPERAND (op1, 0));
18818 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18819 if (TREE_CODE (op11) == COMPOUND_EXPR)
18820 {
18821 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18822 op11 = TREE_OPERAND (op11, 1);
18823 }
18824 lhs = RECUR (TREE_OPERAND (op11, 0));
18825 rhs = RECUR (TREE_OPERAND (op11, 1));
18826 opcode = TREE_CODE (op11);
18827 if (opcode == MODIFY_EXPR)
18828 opcode = NOP_EXPR;
18829 }
18830 else
18831 {
18832 code = OMP_ATOMIC;
18833 lhs = RECUR (TREE_OPERAND (op1, 0));
18834 rhs = RECUR (TREE_OPERAND (op1, 1));
18835 }
18836 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18837 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18838 }
18839 break;
18840
18841 case TRANSACTION_EXPR:
18842 {
18843 int flags = 0;
18844 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18845 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18846
18847 if (TRANSACTION_EXPR_IS_STMT (t))
18848 {
18849 tree body = TRANSACTION_EXPR_BODY (t);
18850 tree noex = NULL_TREE;
18851 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18852 {
18853 noex = MUST_NOT_THROW_COND (body);
18854 if (noex == NULL_TREE)
18855 noex = boolean_true_node;
18856 body = TREE_OPERAND (body, 0);
18857 }
18858 stmt = begin_transaction_stmt (input_location, NULL, flags);
18859 RECUR (body);
18860 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18861 }
18862 else
18863 {
18864 stmt = build_transaction_expr (EXPR_LOCATION (t),
18865 RECUR (TRANSACTION_EXPR_BODY (t)),
18866 flags, NULL_TREE);
18867 RETURN (stmt);
18868 }
18869 }
18870 break;
18871
18872 case MUST_NOT_THROW_EXPR:
18873 {
18874 tree op0 = RECUR (TREE_OPERAND (t, 0));
18875 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18876 RETURN (build_must_not_throw_expr (op0, cond));
18877 }
18878
18879 case EXPR_PACK_EXPANSION:
18880 error ("invalid use of pack expansion expression");
18881 RETURN (error_mark_node);
18882
18883 case NONTYPE_ARGUMENT_PACK:
18884 error ("use %<...%> to expand argument pack");
18885 RETURN (error_mark_node);
18886
18887 case COMPOUND_EXPR:
18888 tmp = RECUR (TREE_OPERAND (t, 0));
18889 if (tmp == NULL_TREE)
18890 /* If the first operand was a statement, we're done with it. */
18891 RETURN (RECUR (TREE_OPERAND (t, 1)));
18892 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18893 RECUR (TREE_OPERAND (t, 1)),
18894 complain));
18895
18896 case ANNOTATE_EXPR:
18897 tmp = RECUR (TREE_OPERAND (t, 0));
18898 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18899 TREE_TYPE (tmp), tmp,
18900 RECUR (TREE_OPERAND (t, 1)),
18901 RECUR (TREE_OPERAND (t, 2))));
18902
18903 case PREDICT_EXPR:
18904 RETURN (add_stmt (copy_node (t)));
18905
18906 default:
18907 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18908
18909 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18910 /*function_p=*/false,
18911 integral_constant_expression_p));
18912 }
18913
18914 RETURN (NULL_TREE);
18915 out:
18916 input_location = loc;
18917 return r;
18918 #undef RECUR
18919 #undef RETURN
18920 }
18921
18922 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18923 function. For description of the body see comment above
18924 cp_parser_omp_declare_reduction_exprs. */
18925
18926 static void
18927 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18928 {
18929 if (t == NULL_TREE || t == error_mark_node)
18930 return;
18931
18932 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
18933
18934 tree_stmt_iterator tsi;
18935 int i;
18936 tree stmts[7];
18937 memset (stmts, 0, sizeof stmts);
18938 for (i = 0, tsi = tsi_start (t);
18939 i < 7 && !tsi_end_p (tsi);
18940 i++, tsi_next (&tsi))
18941 stmts[i] = tsi_stmt (tsi);
18942 gcc_assert (tsi_end_p (tsi));
18943
18944 if (i >= 3)
18945 {
18946 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18947 && TREE_CODE (stmts[1]) == DECL_EXPR);
18948 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18949 args, complain, in_decl);
18950 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18951 args, complain, in_decl);
18952 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18953 expect to be pushing it. */
18954 DECL_CONTEXT (omp_out) = current_function_decl;
18955 DECL_CONTEXT (omp_in) = current_function_decl;
18956 keep_next_level (true);
18957 tree block = begin_omp_structured_block ();
18958 tsubst_expr (stmts[2], args, complain, in_decl, false);
18959 block = finish_omp_structured_block (block);
18960 block = maybe_cleanup_point_expr_void (block);
18961 add_decl_expr (omp_out);
18962 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18963 TREE_NO_WARNING (omp_out) = 1;
18964 add_decl_expr (omp_in);
18965 finish_expr_stmt (block);
18966 }
18967 if (i >= 6)
18968 {
18969 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18970 && TREE_CODE (stmts[4]) == DECL_EXPR);
18971 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18972 args, complain, in_decl);
18973 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18974 args, complain, in_decl);
18975 DECL_CONTEXT (omp_priv) = current_function_decl;
18976 DECL_CONTEXT (omp_orig) = current_function_decl;
18977 keep_next_level (true);
18978 tree block = begin_omp_structured_block ();
18979 tsubst_expr (stmts[5], args, complain, in_decl, false);
18980 block = finish_omp_structured_block (block);
18981 block = maybe_cleanup_point_expr_void (block);
18982 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18983 add_decl_expr (omp_priv);
18984 add_decl_expr (omp_orig);
18985 finish_expr_stmt (block);
18986 if (i == 7)
18987 add_decl_expr (omp_orig);
18988 }
18989 }
18990
18991 /* T is a postfix-expression that is not being used in a function
18992 call. Return the substituted version of T. */
18993
18994 static tree
18995 tsubst_non_call_postfix_expression (tree t, tree args,
18996 tsubst_flags_t complain,
18997 tree in_decl)
18998 {
18999 if (TREE_CODE (t) == SCOPE_REF)
19000 t = tsubst_qualified_id (t, args, complain, in_decl,
19001 /*done=*/false, /*address_p=*/false);
19002 else
19003 t = tsubst_copy_and_build (t, args, complain, in_decl,
19004 /*function_p=*/false,
19005 /*integral_constant_expression_p=*/false);
19006
19007 return t;
19008 }
19009
19010 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19011 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19012 dependent init-capture. */
19013
19014 static void
19015 prepend_one_capture (tree field, tree init, tree &list,
19016 tsubst_flags_t complain)
19017 {
19018 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19019 {
19020 tree type = NULL_TREE;
19021 if (!init)
19022 {
19023 if (complain & tf_error)
19024 error ("empty initializer in lambda init-capture");
19025 init = error_mark_node;
19026 }
19027 else if (TREE_CODE (init) == TREE_LIST)
19028 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19029 if (!type)
19030 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19031 TREE_TYPE (field) = type;
19032 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19033 }
19034 list = tree_cons (field, init, list);
19035 }
19036
19037 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19038 instantiation context. Instantiating a pack expansion containing a lambda
19039 might result in multiple lambdas all based on the same lambda in the
19040 template. */
19041
19042 tree
19043 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19044 {
19045 tree oldfn = lambda_function (t);
19046 in_decl = oldfn;
19047
19048 tree r = build_lambda_expr ();
19049
19050 LAMBDA_EXPR_LOCATION (r)
19051 = LAMBDA_EXPR_LOCATION (t);
19052 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19053 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19054 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19055 LAMBDA_EXPR_INSTANTIATED (r) = true;
19056
19057 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19058 /* A lambda in a default argument outside a class gets no
19059 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19060 tsubst_default_argument calls start_lambda_scope, so we need to
19061 specifically ignore it here, and use the global scope. */
19062 record_null_lambda_scope (r);
19063 else
19064 record_lambda_scope (r);
19065
19066 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19067 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19068
19069 vec<tree,va_gc>* field_packs = NULL;
19070
19071 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19072 cap = TREE_CHAIN (cap))
19073 {
19074 tree ofield = TREE_PURPOSE (cap);
19075 tree init = TREE_VALUE (cap);
19076 if (PACK_EXPANSION_P (init))
19077 init = tsubst_pack_expansion (init, args, complain, in_decl);
19078 else
19079 init = tsubst_copy_and_build (init, args, complain, in_decl,
19080 /*fn*/false, /*constexpr*/false);
19081
19082 if (init == error_mark_node)
19083 return error_mark_node;
19084
19085 if (init && TREE_CODE (init) == TREE_LIST)
19086 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19087
19088 if (!processing_template_decl
19089 && init && TREE_CODE (init) != TREE_VEC
19090 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19091 {
19092 /* For a VLA, simply tsubsting the field type won't work, we need to
19093 go through add_capture again. XXX do we want to do this for all
19094 captures? */
19095 tree name = (get_identifier
19096 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19097 tree ftype = TREE_TYPE (ofield);
19098 bool by_ref = (TYPE_REF_P (ftype)
19099 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19100 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19101 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19102 continue;
19103 }
19104
19105 if (PACK_EXPANSION_P (ofield))
19106 ofield = PACK_EXPANSION_PATTERN (ofield);
19107 tree field = tsubst_decl (ofield, args, complain);
19108
19109 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19110 {
19111 /* Remember these for when we've pushed local_specializations. */
19112 vec_safe_push (field_packs, ofield);
19113 vec_safe_push (field_packs, field);
19114 }
19115
19116 if (field == error_mark_node)
19117 return error_mark_node;
19118
19119 if (TREE_CODE (field) == TREE_VEC)
19120 {
19121 int len = TREE_VEC_LENGTH (field);
19122 gcc_assert (TREE_CODE (init) == TREE_VEC
19123 && TREE_VEC_LENGTH (init) == len);
19124 for (int i = 0; i < len; ++i)
19125 prepend_one_capture (TREE_VEC_ELT (field, i),
19126 TREE_VEC_ELT (init, i),
19127 LAMBDA_EXPR_CAPTURE_LIST (r),
19128 complain);
19129 }
19130 else
19131 {
19132 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19133 complain);
19134
19135 if (id_equal (DECL_NAME (field), "__this"))
19136 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19137 }
19138 }
19139
19140 tree type = begin_lambda_type (r);
19141 if (type == error_mark_node)
19142 return error_mark_node;
19143
19144 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19145 determine_visibility (TYPE_NAME (type));
19146
19147 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19148
19149 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19150 ? DECL_TI_TEMPLATE (oldfn)
19151 : NULL_TREE);
19152
19153 tree fntype = static_fn_type (oldfn);
19154 if (oldtmpl)
19155 ++processing_template_decl;
19156 fntype = tsubst (fntype, args, complain, in_decl);
19157 if (oldtmpl)
19158 --processing_template_decl;
19159
19160 if (fntype == error_mark_node)
19161 r = error_mark_node;
19162 else
19163 {
19164 /* The body of a lambda-expression is not a subexpression of the
19165 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19166 which would be skipped if cp_unevaluated_operand. */
19167 cp_evaluated ev;
19168
19169 /* Fix the type of 'this'. */
19170 fntype = build_memfn_type (fntype, type,
19171 type_memfn_quals (fntype),
19172 type_memfn_rqual (fntype));
19173 tree fn, tmpl;
19174 if (oldtmpl)
19175 {
19176 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19177 if (tmpl == error_mark_node)
19178 {
19179 r = error_mark_node;
19180 goto out;
19181 }
19182 fn = DECL_TEMPLATE_RESULT (tmpl);
19183 finish_member_declaration (tmpl);
19184 }
19185 else
19186 {
19187 tmpl = NULL_TREE;
19188 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19189 if (fn == error_mark_node)
19190 {
19191 r = error_mark_node;
19192 goto out;
19193 }
19194 finish_member_declaration (fn);
19195 }
19196
19197 if (tree ci = get_constraints (oldfn))
19198 {
19199 /* Substitute into the lambda's constraints. */
19200 if (oldtmpl)
19201 ++processing_template_decl;
19202 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19203 if (oldtmpl)
19204 --processing_template_decl;
19205 set_constraints (fn, ci);
19206 }
19207
19208 /* Let finish_function set this. */
19209 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19210
19211 bool nested = cfun;
19212 if (nested)
19213 push_function_context ();
19214 else
19215 /* Still increment function_depth so that we don't GC in the
19216 middle of an expression. */
19217 ++function_depth;
19218
19219 local_specialization_stack s (lss_copy);
19220
19221 tree body = start_lambda_function (fn, r);
19222
19223 /* Now record them for lookup_init_capture_pack. */
19224 int fplen = vec_safe_length (field_packs);
19225 for (int i = 0; i < fplen; )
19226 {
19227 tree pack = (*field_packs)[i++];
19228 tree inst = (*field_packs)[i++];
19229 register_local_specialization (inst, pack);
19230 }
19231 release_tree_vector (field_packs);
19232
19233 register_parameter_specializations (oldfn, fn);
19234
19235 if (oldtmpl)
19236 {
19237 /* We might not partially instantiate some parts of the function, so
19238 copy these flags from the original template. */
19239 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19240 current_function_returns_value = ol->returns_value;
19241 current_function_returns_null = ol->returns_null;
19242 current_function_returns_abnormally = ol->returns_abnormally;
19243 current_function_infinite_loop = ol->infinite_loop;
19244 }
19245
19246 /* [temp.deduct] A lambda-expression appearing in a function type or a
19247 template parameter is not considered part of the immediate context for
19248 the purposes of template argument deduction. */
19249 complain = tf_warning_or_error;
19250
19251 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19252 /*constexpr*/false);
19253
19254 finish_lambda_function (body);
19255
19256 if (nested)
19257 pop_function_context ();
19258 else
19259 --function_depth;
19260
19261 /* The capture list was built up in reverse order; fix that now. */
19262 LAMBDA_EXPR_CAPTURE_LIST (r)
19263 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19264
19265 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19266
19267 maybe_add_lambda_conv_op (type);
19268 }
19269
19270 out:
19271 finish_struct (type, /*attr*/NULL_TREE);
19272
19273 insert_pending_capture_proxies ();
19274
19275 return r;
19276 }
19277
19278 /* Like tsubst but deals with expressions and performs semantic
19279 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19280
19281 tree
19282 tsubst_copy_and_build (tree t,
19283 tree args,
19284 tsubst_flags_t complain,
19285 tree in_decl,
19286 bool function_p,
19287 bool integral_constant_expression_p)
19288 {
19289 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19290 #define RECUR(NODE) \
19291 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19292 /*function_p=*/false, \
19293 integral_constant_expression_p)
19294
19295 tree retval, op1;
19296 location_t save_loc;
19297
19298 if (t == NULL_TREE || t == error_mark_node)
19299 return t;
19300
19301 save_loc = input_location;
19302 if (location_t eloc = cp_expr_location (t))
19303 input_location = eloc;
19304
19305 /* N3276 decltype magic only applies to calls at the top level or on the
19306 right side of a comma. */
19307 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19308 complain &= ~tf_decltype;
19309
19310 switch (TREE_CODE (t))
19311 {
19312 case USING_DECL:
19313 t = DECL_NAME (t);
19314 /* Fall through. */
19315 case IDENTIFIER_NODE:
19316 {
19317 tree decl;
19318 cp_id_kind idk;
19319 bool non_integral_constant_expression_p;
19320 const char *error_msg;
19321
19322 if (IDENTIFIER_CONV_OP_P (t))
19323 {
19324 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19325 t = make_conv_op_name (new_type);
19326 }
19327
19328 /* Look up the name. */
19329 decl = lookup_name (t);
19330
19331 /* By convention, expressions use ERROR_MARK_NODE to indicate
19332 failure, not NULL_TREE. */
19333 if (decl == NULL_TREE)
19334 decl = error_mark_node;
19335
19336 decl = finish_id_expression (t, decl, NULL_TREE,
19337 &idk,
19338 integral_constant_expression_p,
19339 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19340 &non_integral_constant_expression_p,
19341 /*template_p=*/false,
19342 /*done=*/true,
19343 /*address_p=*/false,
19344 /*template_arg_p=*/false,
19345 &error_msg,
19346 input_location);
19347 if (error_msg)
19348 error (error_msg);
19349 if (!function_p && identifier_p (decl))
19350 {
19351 if (complain & tf_error)
19352 unqualified_name_lookup_error (decl);
19353 decl = error_mark_node;
19354 }
19355 RETURN (decl);
19356 }
19357
19358 case TEMPLATE_ID_EXPR:
19359 {
19360 tree object;
19361 tree templ = RECUR (TREE_OPERAND (t, 0));
19362 tree targs = TREE_OPERAND (t, 1);
19363
19364 if (targs)
19365 targs = tsubst_template_args (targs, args, complain, in_decl);
19366 if (targs == error_mark_node)
19367 RETURN (error_mark_node);
19368
19369 if (TREE_CODE (templ) == SCOPE_REF)
19370 {
19371 tree name = TREE_OPERAND (templ, 1);
19372 tree tid = lookup_template_function (name, targs);
19373 TREE_OPERAND (templ, 1) = tid;
19374 RETURN (templ);
19375 }
19376
19377 if (concept_definition_p (templ))
19378 {
19379 tree check = build_concept_check (templ, targs, complain);
19380 if (check == error_mark_node)
19381 RETURN (error_mark_node);
19382
19383 tree id = unpack_concept_check (check);
19384
19385 /* If we built a function concept check, return the underlying
19386 template-id. So we can evaluate it as a function call. */
19387 if (function_concept_p (TREE_OPERAND (id, 0)))
19388 RETURN (id);
19389
19390 RETURN (check);
19391 }
19392
19393 if (variable_template_p (templ))
19394 {
19395 tree r = lookup_and_finish_template_variable (templ, targs,
19396 complain);
19397 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19398 RETURN (r);
19399 }
19400
19401 if (TREE_CODE (templ) == COMPONENT_REF)
19402 {
19403 object = TREE_OPERAND (templ, 0);
19404 templ = TREE_OPERAND (templ, 1);
19405 }
19406 else
19407 object = NULL_TREE;
19408 templ = lookup_template_function (templ, targs);
19409
19410 if (object)
19411 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19412 object, templ, NULL_TREE));
19413 else
19414 RETURN (baselink_for_fns (templ));
19415 }
19416
19417 case INDIRECT_REF:
19418 {
19419 tree r = RECUR (TREE_OPERAND (t, 0));
19420
19421 if (REFERENCE_REF_P (t))
19422 {
19423 /* A type conversion to reference type will be enclosed in
19424 such an indirect ref, but the substitution of the cast
19425 will have also added such an indirect ref. */
19426 r = convert_from_reference (r);
19427 }
19428 else
19429 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19430 complain|decltype_flag);
19431
19432 if (REF_PARENTHESIZED_P (t))
19433 r = force_paren_expr (r);
19434
19435 RETURN (r);
19436 }
19437
19438 case NOP_EXPR:
19439 {
19440 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19441 tree op0 = RECUR (TREE_OPERAND (t, 0));
19442 RETURN (build_nop (type, op0));
19443 }
19444
19445 case IMPLICIT_CONV_EXPR:
19446 {
19447 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19448 tree expr = RECUR (TREE_OPERAND (t, 0));
19449 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19450 {
19451 retval = copy_node (t);
19452 TREE_TYPE (retval) = type;
19453 TREE_OPERAND (retval, 0) = expr;
19454 RETURN (retval);
19455 }
19456 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19457 /* We'll pass this to convert_nontype_argument again, we don't need
19458 to actually perform any conversion here. */
19459 RETURN (expr);
19460 int flags = LOOKUP_IMPLICIT;
19461 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19462 flags = LOOKUP_NORMAL;
19463 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19464 flags |= LOOKUP_NO_NARROWING;
19465 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19466 flags));
19467 }
19468
19469 case CONVERT_EXPR:
19470 {
19471 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19472 tree op0 = RECUR (TREE_OPERAND (t, 0));
19473 if (op0 == error_mark_node)
19474 RETURN (error_mark_node);
19475 RETURN (build1 (CONVERT_EXPR, type, op0));
19476 }
19477
19478 case CAST_EXPR:
19479 case REINTERPRET_CAST_EXPR:
19480 case CONST_CAST_EXPR:
19481 case DYNAMIC_CAST_EXPR:
19482 case STATIC_CAST_EXPR:
19483 {
19484 tree type;
19485 tree op, r = NULL_TREE;
19486
19487 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19488 if (integral_constant_expression_p
19489 && !cast_valid_in_integral_constant_expression_p (type))
19490 {
19491 if (complain & tf_error)
19492 error ("a cast to a type other than an integral or "
19493 "enumeration type cannot appear in a constant-expression");
19494 RETURN (error_mark_node);
19495 }
19496
19497 op = RECUR (TREE_OPERAND (t, 0));
19498
19499 warning_sentinel s(warn_useless_cast);
19500 warning_sentinel s2(warn_ignored_qualifiers);
19501 switch (TREE_CODE (t))
19502 {
19503 case CAST_EXPR:
19504 r = build_functional_cast (input_location, type, op, complain);
19505 break;
19506 case REINTERPRET_CAST_EXPR:
19507 r = build_reinterpret_cast (input_location, type, op, complain);
19508 break;
19509 case CONST_CAST_EXPR:
19510 r = build_const_cast (input_location, type, op, complain);
19511 break;
19512 case DYNAMIC_CAST_EXPR:
19513 r = build_dynamic_cast (input_location, type, op, complain);
19514 break;
19515 case STATIC_CAST_EXPR:
19516 r = build_static_cast (input_location, type, op, complain);
19517 if (IMPLICIT_RVALUE_P (t))
19518 set_implicit_rvalue_p (r);
19519 break;
19520 default:
19521 gcc_unreachable ();
19522 }
19523
19524 RETURN (r);
19525 }
19526
19527 case POSTDECREMENT_EXPR:
19528 case POSTINCREMENT_EXPR:
19529 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19530 args, complain, in_decl);
19531 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19532 complain|decltype_flag));
19533
19534 case PREDECREMENT_EXPR:
19535 case PREINCREMENT_EXPR:
19536 case NEGATE_EXPR:
19537 case BIT_NOT_EXPR:
19538 case ABS_EXPR:
19539 case TRUTH_NOT_EXPR:
19540 case UNARY_PLUS_EXPR: /* Unary + */
19541 case REALPART_EXPR:
19542 case IMAGPART_EXPR:
19543 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19544 RECUR (TREE_OPERAND (t, 0)),
19545 complain|decltype_flag));
19546
19547 case FIX_TRUNC_EXPR:
19548 gcc_unreachable ();
19549
19550 case ADDR_EXPR:
19551 op1 = TREE_OPERAND (t, 0);
19552 if (TREE_CODE (op1) == LABEL_DECL)
19553 RETURN (finish_label_address_expr (DECL_NAME (op1),
19554 EXPR_LOCATION (op1)));
19555 if (TREE_CODE (op1) == SCOPE_REF)
19556 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19557 /*done=*/true, /*address_p=*/true);
19558 else
19559 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19560 in_decl);
19561 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19562 complain|decltype_flag));
19563
19564 case PLUS_EXPR:
19565 case MINUS_EXPR:
19566 case MULT_EXPR:
19567 case TRUNC_DIV_EXPR:
19568 case CEIL_DIV_EXPR:
19569 case FLOOR_DIV_EXPR:
19570 case ROUND_DIV_EXPR:
19571 case EXACT_DIV_EXPR:
19572 case BIT_AND_EXPR:
19573 case BIT_IOR_EXPR:
19574 case BIT_XOR_EXPR:
19575 case TRUNC_MOD_EXPR:
19576 case FLOOR_MOD_EXPR:
19577 case TRUTH_ANDIF_EXPR:
19578 case TRUTH_ORIF_EXPR:
19579 case TRUTH_AND_EXPR:
19580 case TRUTH_OR_EXPR:
19581 case RSHIFT_EXPR:
19582 case LSHIFT_EXPR:
19583 case EQ_EXPR:
19584 case NE_EXPR:
19585 case MAX_EXPR:
19586 case MIN_EXPR:
19587 case LE_EXPR:
19588 case GE_EXPR:
19589 case LT_EXPR:
19590 case GT_EXPR:
19591 case SPACESHIP_EXPR:
19592 case MEMBER_REF:
19593 case DOTSTAR_EXPR:
19594 {
19595 /* If T was type-dependent, suppress warnings that depend on the range
19596 of the types involved. */
19597 bool was_dep = type_dependent_expression_p_push (t);
19598
19599 tree op0 = RECUR (TREE_OPERAND (t, 0));
19600 tree op1 = RECUR (TREE_OPERAND (t, 1));
19601
19602 warning_sentinel s1(warn_type_limits, was_dep);
19603 warning_sentinel s2(warn_div_by_zero, was_dep);
19604 warning_sentinel s3(warn_logical_op, was_dep);
19605 warning_sentinel s4(warn_tautological_compare, was_dep);
19606
19607 tree r = build_x_binary_op
19608 (input_location, TREE_CODE (t),
19609 op0,
19610 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19611 ? ERROR_MARK
19612 : TREE_CODE (TREE_OPERAND (t, 0))),
19613 op1,
19614 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19615 ? ERROR_MARK
19616 : TREE_CODE (TREE_OPERAND (t, 1))),
19617 /*overload=*/NULL,
19618 complain|decltype_flag);
19619 if (EXPR_P (r) && TREE_NO_WARNING (t))
19620 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19621
19622 RETURN (r);
19623 }
19624
19625 case POINTER_PLUS_EXPR:
19626 {
19627 tree op0 = RECUR (TREE_OPERAND (t, 0));
19628 if (op0 == error_mark_node)
19629 RETURN (error_mark_node);
19630 tree op1 = RECUR (TREE_OPERAND (t, 1));
19631 if (op1 == error_mark_node)
19632 RETURN (error_mark_node);
19633 RETURN (fold_build_pointer_plus (op0, op1));
19634 }
19635
19636 case SCOPE_REF:
19637 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19638 /*address_p=*/false));
19639 case ARRAY_REF:
19640 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19641 args, complain, in_decl);
19642 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19643 RECUR (TREE_OPERAND (t, 1)),
19644 complain|decltype_flag));
19645
19646 case SIZEOF_EXPR:
19647 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19648 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19649 RETURN (tsubst_copy (t, args, complain, in_decl));
19650 /* Fall through */
19651
19652 case ALIGNOF_EXPR:
19653 {
19654 tree r;
19655
19656 op1 = TREE_OPERAND (t, 0);
19657 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19658 op1 = TREE_TYPE (op1);
19659 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19660 && ALIGNOF_EXPR_STD_P (t));
19661 if (!args)
19662 {
19663 /* When there are no ARGS, we are trying to evaluate a
19664 non-dependent expression from the parser. Trying to do
19665 the substitutions may not work. */
19666 if (!TYPE_P (op1))
19667 op1 = TREE_TYPE (op1);
19668 }
19669 else
19670 {
19671 ++cp_unevaluated_operand;
19672 ++c_inhibit_evaluation_warnings;
19673 if (TYPE_P (op1))
19674 op1 = tsubst (op1, args, complain, in_decl);
19675 else
19676 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19677 /*function_p=*/false,
19678 /*integral_constant_expression_p=*/
19679 false);
19680 --cp_unevaluated_operand;
19681 --c_inhibit_evaluation_warnings;
19682 }
19683 if (TYPE_P (op1))
19684 r = cxx_sizeof_or_alignof_type (input_location,
19685 op1, TREE_CODE (t), std_alignof,
19686 complain & tf_error);
19687 else
19688 r = cxx_sizeof_or_alignof_expr (input_location,
19689 op1, TREE_CODE (t),
19690 complain & tf_error);
19691 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19692 {
19693 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19694 {
19695 if (!processing_template_decl && TYPE_P (op1))
19696 {
19697 r = build_min (SIZEOF_EXPR, size_type_node,
19698 build1 (NOP_EXPR, op1, error_mark_node));
19699 SIZEOF_EXPR_TYPE_P (r) = 1;
19700 }
19701 else
19702 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19703 TREE_SIDE_EFFECTS (r) = 0;
19704 TREE_READONLY (r) = 1;
19705 }
19706 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19707 }
19708 RETURN (r);
19709 }
19710
19711 case AT_ENCODE_EXPR:
19712 {
19713 op1 = TREE_OPERAND (t, 0);
19714 ++cp_unevaluated_operand;
19715 ++c_inhibit_evaluation_warnings;
19716 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19717 /*function_p=*/false,
19718 /*integral_constant_expression_p=*/false);
19719 --cp_unevaluated_operand;
19720 --c_inhibit_evaluation_warnings;
19721 RETURN (objc_build_encode_expr (op1));
19722 }
19723
19724 case NOEXCEPT_EXPR:
19725 op1 = TREE_OPERAND (t, 0);
19726 ++cp_unevaluated_operand;
19727 ++c_inhibit_evaluation_warnings;
19728 ++cp_noexcept_operand;
19729 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19730 /*function_p=*/false,
19731 /*integral_constant_expression_p=*/false);
19732 --cp_unevaluated_operand;
19733 --c_inhibit_evaluation_warnings;
19734 --cp_noexcept_operand;
19735 RETURN (finish_noexcept_expr (op1, complain));
19736
19737 case MODOP_EXPR:
19738 {
19739 warning_sentinel s(warn_div_by_zero);
19740 tree lhs = RECUR (TREE_OPERAND (t, 0));
19741 tree rhs = RECUR (TREE_OPERAND (t, 2));
19742 tree r = build_x_modify_expr
19743 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19744 complain|decltype_flag);
19745 /* TREE_NO_WARNING must be set if either the expression was
19746 parenthesized or it uses an operator such as >>= rather
19747 than plain assignment. In the former case, it was already
19748 set and must be copied. In the latter case,
19749 build_x_modify_expr sets it and it must not be reset
19750 here. */
19751 if (TREE_NO_WARNING (t))
19752 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19753
19754 RETURN (r);
19755 }
19756
19757 case ARROW_EXPR:
19758 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19759 args, complain, in_decl);
19760 /* Remember that there was a reference to this entity. */
19761 if (DECL_P (op1)
19762 && !mark_used (op1, complain) && !(complain & tf_error))
19763 RETURN (error_mark_node);
19764 RETURN (build_x_arrow (input_location, op1, complain));
19765
19766 case NEW_EXPR:
19767 {
19768 tree placement = RECUR (TREE_OPERAND (t, 0));
19769 tree init = RECUR (TREE_OPERAND (t, 3));
19770 vec<tree, va_gc> *placement_vec;
19771 vec<tree, va_gc> *init_vec;
19772 tree ret;
19773 location_t loc = EXPR_LOCATION (t);
19774
19775 if (placement == NULL_TREE)
19776 placement_vec = NULL;
19777 else if (placement == error_mark_node)
19778 RETURN (error_mark_node);
19779 else
19780 {
19781 placement_vec = make_tree_vector ();
19782 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19783 vec_safe_push (placement_vec, TREE_VALUE (placement));
19784 }
19785
19786 /* If there was an initializer in the original tree, but it
19787 instantiated to an empty list, then we should pass a
19788 non-NULL empty vector to tell build_new that it was an
19789 empty initializer() rather than no initializer. This can
19790 only happen when the initializer is a pack expansion whose
19791 parameter packs are of length zero. */
19792 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19793 init_vec = NULL;
19794 else
19795 {
19796 init_vec = make_tree_vector ();
19797 if (init == void_node)
19798 gcc_assert (init_vec != NULL);
19799 else
19800 {
19801 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19802 vec_safe_push (init_vec, TREE_VALUE (init));
19803 }
19804 }
19805
19806 /* Avoid passing an enclosing decl to valid_array_size_p. */
19807 in_decl = NULL_TREE;
19808
19809 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19810 tree op2 = RECUR (TREE_OPERAND (t, 2));
19811 ret = build_new (loc, &placement_vec, op1, op2,
19812 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19813 complain);
19814
19815 if (placement_vec != NULL)
19816 release_tree_vector (placement_vec);
19817 if (init_vec != NULL)
19818 release_tree_vector (init_vec);
19819
19820 RETURN (ret);
19821 }
19822
19823 case DELETE_EXPR:
19824 {
19825 tree op0 = RECUR (TREE_OPERAND (t, 0));
19826 tree op1 = RECUR (TREE_OPERAND (t, 1));
19827 RETURN (delete_sanity (input_location, op0, op1,
19828 DELETE_EXPR_USE_VEC (t),
19829 DELETE_EXPR_USE_GLOBAL (t),
19830 complain));
19831 }
19832
19833 case COMPOUND_EXPR:
19834 {
19835 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19836 complain & ~tf_decltype, in_decl,
19837 /*function_p=*/false,
19838 integral_constant_expression_p);
19839 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19840 op0,
19841 RECUR (TREE_OPERAND (t, 1)),
19842 complain|decltype_flag));
19843 }
19844
19845 case CALL_EXPR:
19846 {
19847 tree function;
19848 unsigned int nargs, i;
19849 bool qualified_p;
19850 bool koenig_p;
19851 tree ret;
19852
19853 function = CALL_EXPR_FN (t);
19854 /* Internal function with no arguments. */
19855 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19856 RETURN (t);
19857
19858 /* When we parsed the expression, we determined whether or
19859 not Koenig lookup should be performed. */
19860 koenig_p = KOENIG_LOOKUP_P (t);
19861 if (function == NULL_TREE)
19862 {
19863 koenig_p = false;
19864 qualified_p = false;
19865 }
19866 else if (TREE_CODE (function) == SCOPE_REF)
19867 {
19868 qualified_p = true;
19869 function = tsubst_qualified_id (function, args, complain, in_decl,
19870 /*done=*/false,
19871 /*address_p=*/false);
19872 }
19873 else if (koenig_p && identifier_p (function))
19874 {
19875 /* Do nothing; calling tsubst_copy_and_build on an identifier
19876 would incorrectly perform unqualified lookup again.
19877
19878 Note that we can also have an IDENTIFIER_NODE if the earlier
19879 unqualified lookup found a member function; in that case
19880 koenig_p will be false and we do want to do the lookup
19881 again to find the instantiated member function.
19882
19883 FIXME but doing that causes c++/15272, so we need to stop
19884 using IDENTIFIER_NODE in that situation. */
19885 qualified_p = false;
19886 }
19887 else
19888 {
19889 if (TREE_CODE (function) == COMPONENT_REF)
19890 {
19891 tree op = TREE_OPERAND (function, 1);
19892
19893 qualified_p = (TREE_CODE (op) == SCOPE_REF
19894 || (BASELINK_P (op)
19895 && BASELINK_QUALIFIED_P (op)));
19896 }
19897 else
19898 qualified_p = false;
19899
19900 if (TREE_CODE (function) == ADDR_EXPR
19901 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19902 /* Avoid error about taking the address of a constructor. */
19903 function = TREE_OPERAND (function, 0);
19904
19905 function = tsubst_copy_and_build (function, args, complain,
19906 in_decl,
19907 !qualified_p,
19908 integral_constant_expression_p);
19909
19910 if (BASELINK_P (function))
19911 qualified_p = true;
19912 }
19913
19914 nargs = call_expr_nargs (t);
19915 releasing_vec call_args;
19916 for (i = 0; i < nargs; ++i)
19917 {
19918 tree arg = CALL_EXPR_ARG (t, i);
19919
19920 if (!PACK_EXPANSION_P (arg))
19921 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19922 else
19923 {
19924 /* Expand the pack expansion and push each entry onto
19925 CALL_ARGS. */
19926 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19927 if (TREE_CODE (arg) == TREE_VEC)
19928 {
19929 unsigned int len, j;
19930
19931 len = TREE_VEC_LENGTH (arg);
19932 for (j = 0; j < len; ++j)
19933 {
19934 tree value = TREE_VEC_ELT (arg, j);
19935 if (value != NULL_TREE)
19936 value = convert_from_reference (value);
19937 vec_safe_push (call_args, value);
19938 }
19939 }
19940 else
19941 {
19942 /* A partial substitution. Add one entry. */
19943 vec_safe_push (call_args, arg);
19944 }
19945 }
19946 }
19947
19948 /* Stripped-down processing for a call in a thunk. Specifically, in
19949 the thunk template for a generic lambda. */
19950 if (call_from_lambda_thunk_p (t))
19951 {
19952 /* Now that we've expanded any packs, the number of call args
19953 might be different. */
19954 unsigned int cargs = call_args->length ();
19955 tree thisarg = NULL_TREE;
19956 if (TREE_CODE (function) == COMPONENT_REF)
19957 {
19958 thisarg = TREE_OPERAND (function, 0);
19959 if (TREE_CODE (thisarg) == INDIRECT_REF)
19960 thisarg = TREE_OPERAND (thisarg, 0);
19961 function = TREE_OPERAND (function, 1);
19962 if (TREE_CODE (function) == BASELINK)
19963 function = BASELINK_FUNCTIONS (function);
19964 }
19965 /* We aren't going to do normal overload resolution, so force the
19966 template-id to resolve. */
19967 function = resolve_nondeduced_context (function, complain);
19968 for (unsigned i = 0; i < cargs; ++i)
19969 {
19970 /* In a thunk, pass through args directly, without any
19971 conversions. */
19972 tree arg = (*call_args)[i];
19973 while (TREE_CODE (arg) != PARM_DECL)
19974 arg = TREE_OPERAND (arg, 0);
19975 (*call_args)[i] = arg;
19976 }
19977 if (thisarg)
19978 {
19979 /* If there are no other args, just push 'this'. */
19980 if (cargs == 0)
19981 vec_safe_push (call_args, thisarg);
19982 else
19983 {
19984 /* Otherwise, shift the other args over to make room. */
19985 tree last = (*call_args)[cargs - 1];
19986 vec_safe_push (call_args, last);
19987 for (int i = cargs - 1; i > 0; --i)
19988 (*call_args)[i] = (*call_args)[i - 1];
19989 (*call_args)[0] = thisarg;
19990 }
19991 }
19992 ret = build_call_a (function, call_args->length (),
19993 call_args->address ());
19994 /* The thunk location is not interesting. */
19995 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19996 CALL_FROM_THUNK_P (ret) = true;
19997 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19998 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19999
20000 RETURN (ret);
20001 }
20002
20003 /* We do not perform argument-dependent lookup if normal
20004 lookup finds a non-function, in accordance with the
20005 expected resolution of DR 218. */
20006 if (koenig_p
20007 && ((is_overloaded_fn (function)
20008 /* If lookup found a member function, the Koenig lookup is
20009 not appropriate, even if an unqualified-name was used
20010 to denote the function. */
20011 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20012 || identifier_p (function))
20013 /* Only do this when substitution turns a dependent call
20014 into a non-dependent call. */
20015 && type_dependent_expression_p_push (t)
20016 && !any_type_dependent_arguments_p (call_args))
20017 function = perform_koenig_lookup (function, call_args, tf_none);
20018
20019 if (function != NULL_TREE
20020 && identifier_p (function)
20021 && !any_type_dependent_arguments_p (call_args))
20022 {
20023 if (koenig_p && (complain & tf_warning_or_error))
20024 {
20025 /* For backwards compatibility and good diagnostics, try
20026 the unqualified lookup again if we aren't in SFINAE
20027 context. */
20028 tree unq = (tsubst_copy_and_build
20029 (function, args, complain, in_decl, true,
20030 integral_constant_expression_p));
20031 if (unq == error_mark_node)
20032 RETURN (error_mark_node);
20033
20034 if (unq != function)
20035 {
20036 /* In a lambda fn, we have to be careful to not
20037 introduce new this captures. Legacy code can't
20038 be using lambdas anyway, so it's ok to be
20039 stricter. */
20040 bool in_lambda = (current_class_type
20041 && LAMBDA_TYPE_P (current_class_type));
20042 char const *const msg
20043 = G_("%qD was not declared in this scope, "
20044 "and no declarations were found by "
20045 "argument-dependent lookup at the point "
20046 "of instantiation");
20047
20048 bool diag = true;
20049 if (in_lambda)
20050 error_at (cp_expr_loc_or_input_loc (t),
20051 msg, function);
20052 else
20053 diag = permerror (cp_expr_loc_or_input_loc (t),
20054 msg, function);
20055 if (diag)
20056 {
20057 tree fn = unq;
20058
20059 if (INDIRECT_REF_P (fn))
20060 fn = TREE_OPERAND (fn, 0);
20061 if (is_overloaded_fn (fn))
20062 fn = get_first_fn (fn);
20063
20064 if (!DECL_P (fn))
20065 /* Can't say anything more. */;
20066 else if (DECL_CLASS_SCOPE_P (fn))
20067 {
20068 location_t loc = cp_expr_loc_or_input_loc (t);
20069 inform (loc,
20070 "declarations in dependent base %qT are "
20071 "not found by unqualified lookup",
20072 DECL_CLASS_CONTEXT (fn));
20073 if (current_class_ptr)
20074 inform (loc,
20075 "use %<this->%D%> instead", function);
20076 else
20077 inform (loc,
20078 "use %<%T::%D%> instead",
20079 current_class_name, function);
20080 }
20081 else
20082 inform (DECL_SOURCE_LOCATION (fn),
20083 "%qD declared here, later in the "
20084 "translation unit", fn);
20085 if (in_lambda)
20086 RETURN (error_mark_node);
20087 }
20088
20089 function = unq;
20090 }
20091 }
20092 if (identifier_p (function))
20093 {
20094 if (complain & tf_error)
20095 unqualified_name_lookup_error (function);
20096 RETURN (error_mark_node);
20097 }
20098 }
20099
20100 /* Remember that there was a reference to this entity. */
20101 if (function != NULL_TREE
20102 && DECL_P (function)
20103 && !mark_used (function, complain) && !(complain & tf_error))
20104 RETURN (error_mark_node);
20105
20106 /* Put back tf_decltype for the actual call. */
20107 complain |= decltype_flag;
20108
20109 if (function == NULL_TREE)
20110 switch (CALL_EXPR_IFN (t))
20111 {
20112 case IFN_LAUNDER:
20113 gcc_assert (nargs == 1);
20114 if (vec_safe_length (call_args) != 1)
20115 {
20116 error_at (cp_expr_loc_or_input_loc (t),
20117 "wrong number of arguments to "
20118 "%<__builtin_launder%>");
20119 ret = error_mark_node;
20120 }
20121 else
20122 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20123 (*call_args)[0], complain);
20124 break;
20125
20126 case IFN_VEC_CONVERT:
20127 gcc_assert (nargs == 1);
20128 if (vec_safe_length (call_args) != 1)
20129 {
20130 error_at (cp_expr_loc_or_input_loc (t),
20131 "wrong number of arguments to "
20132 "%<__builtin_convertvector%>");
20133 ret = error_mark_node;
20134 break;
20135 }
20136 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20137 tsubst (TREE_TYPE (t), args,
20138 complain, in_decl),
20139 complain);
20140 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20141 RETURN (ret);
20142 break;
20143
20144 default:
20145 /* Unsupported internal function with arguments. */
20146 gcc_unreachable ();
20147 }
20148 else if (TREE_CODE (function) == OFFSET_REF
20149 || TREE_CODE (function) == DOTSTAR_EXPR
20150 || TREE_CODE (function) == MEMBER_REF)
20151 ret = build_offset_ref_call_from_tree (function, &call_args,
20152 complain);
20153 else if (TREE_CODE (function) == COMPONENT_REF)
20154 {
20155 tree instance = TREE_OPERAND (function, 0);
20156 tree fn = TREE_OPERAND (function, 1);
20157
20158 if (processing_template_decl
20159 && (type_dependent_expression_p (instance)
20160 || (!BASELINK_P (fn)
20161 && TREE_CODE (fn) != FIELD_DECL)
20162 || type_dependent_expression_p (fn)
20163 || any_type_dependent_arguments_p (call_args)))
20164 ret = build_min_nt_call_vec (function, call_args);
20165 else if (!BASELINK_P (fn))
20166 ret = finish_call_expr (function, &call_args,
20167 /*disallow_virtual=*/false,
20168 /*koenig_p=*/false,
20169 complain);
20170 else
20171 ret = (build_new_method_call
20172 (instance, fn,
20173 &call_args, NULL_TREE,
20174 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20175 /*fn_p=*/NULL,
20176 complain));
20177 }
20178 else if (concept_check_p (function))
20179 {
20180 /* FUNCTION is a template-id referring to a concept definition. */
20181 tree id = unpack_concept_check (function);
20182 tree tmpl = TREE_OPERAND (id, 0);
20183 tree args = TREE_OPERAND (id, 1);
20184
20185 /* Calls to standard and variable concepts should have been
20186 previously diagnosed. */
20187 gcc_assert (function_concept_p (tmpl));
20188
20189 /* Ensure the result is wrapped as a call expression. */
20190 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20191 }
20192 else
20193 ret = finish_call_expr (function, &call_args,
20194 /*disallow_virtual=*/qualified_p,
20195 koenig_p,
20196 complain);
20197
20198 if (ret != error_mark_node)
20199 {
20200 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20201 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20202 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20203 if (op || ord || rev)
20204 {
20205 function = extract_call_expr (ret);
20206 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20207 CALL_EXPR_ORDERED_ARGS (function) = ord;
20208 CALL_EXPR_REVERSE_ARGS (function) = rev;
20209 }
20210 }
20211
20212 RETURN (ret);
20213 }
20214
20215 case COND_EXPR:
20216 {
20217 tree cond = RECUR (TREE_OPERAND (t, 0));
20218 cond = mark_rvalue_use (cond);
20219 tree folded_cond = fold_non_dependent_expr (cond, complain);
20220 tree exp1, exp2;
20221
20222 if (TREE_CODE (folded_cond) == INTEGER_CST)
20223 {
20224 if (integer_zerop (folded_cond))
20225 {
20226 ++c_inhibit_evaluation_warnings;
20227 exp1 = RECUR (TREE_OPERAND (t, 1));
20228 --c_inhibit_evaluation_warnings;
20229 exp2 = RECUR (TREE_OPERAND (t, 2));
20230 }
20231 else
20232 {
20233 exp1 = RECUR (TREE_OPERAND (t, 1));
20234 ++c_inhibit_evaluation_warnings;
20235 exp2 = RECUR (TREE_OPERAND (t, 2));
20236 --c_inhibit_evaluation_warnings;
20237 }
20238 cond = folded_cond;
20239 }
20240 else
20241 {
20242 exp1 = RECUR (TREE_OPERAND (t, 1));
20243 exp2 = RECUR (TREE_OPERAND (t, 2));
20244 }
20245
20246 warning_sentinel s(warn_duplicated_branches);
20247 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20248 cond, exp1, exp2, complain));
20249 }
20250
20251 case PSEUDO_DTOR_EXPR:
20252 {
20253 tree op0 = RECUR (TREE_OPERAND (t, 0));
20254 tree op1 = RECUR (TREE_OPERAND (t, 1));
20255 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20256 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20257 input_location));
20258 }
20259
20260 case TREE_LIST:
20261 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20262
20263 case COMPONENT_REF:
20264 {
20265 tree object;
20266 tree object_type;
20267 tree member;
20268 tree r;
20269
20270 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20271 args, complain, in_decl);
20272 /* Remember that there was a reference to this entity. */
20273 if (DECL_P (object)
20274 && !mark_used (object, complain) && !(complain & tf_error))
20275 RETURN (error_mark_node);
20276 object_type = TREE_TYPE (object);
20277
20278 member = TREE_OPERAND (t, 1);
20279 if (BASELINK_P (member))
20280 member = tsubst_baselink (member,
20281 non_reference (TREE_TYPE (object)),
20282 args, complain, in_decl);
20283 else
20284 member = tsubst_copy (member, args, complain, in_decl);
20285 if (member == error_mark_node)
20286 RETURN (error_mark_node);
20287
20288 if (TREE_CODE (member) == FIELD_DECL)
20289 {
20290 r = finish_non_static_data_member (member, object, NULL_TREE);
20291 if (TREE_CODE (r) == COMPONENT_REF)
20292 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20293 RETURN (r);
20294 }
20295 else if (type_dependent_expression_p (object))
20296 /* We can't do much here. */;
20297 else if (!CLASS_TYPE_P (object_type))
20298 {
20299 if (scalarish_type_p (object_type))
20300 {
20301 tree s = NULL_TREE;
20302 tree dtor = member;
20303
20304 if (TREE_CODE (dtor) == SCOPE_REF)
20305 {
20306 s = TREE_OPERAND (dtor, 0);
20307 dtor = TREE_OPERAND (dtor, 1);
20308 }
20309 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20310 {
20311 dtor = TREE_OPERAND (dtor, 0);
20312 if (TYPE_P (dtor))
20313 RETURN (finish_pseudo_destructor_expr
20314 (object, s, dtor, input_location));
20315 }
20316 }
20317 }
20318 else if (TREE_CODE (member) == SCOPE_REF
20319 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20320 {
20321 /* Lookup the template functions now that we know what the
20322 scope is. */
20323 tree scope = TREE_OPERAND (member, 0);
20324 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20325 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20326 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20327 /*complain=*/false);
20328 if (BASELINK_P (member))
20329 {
20330 BASELINK_FUNCTIONS (member)
20331 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20332 args);
20333 member = (adjust_result_of_qualified_name_lookup
20334 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20335 object_type));
20336 }
20337 else
20338 {
20339 qualified_name_lookup_error (scope, tmpl, member,
20340 input_location);
20341 RETURN (error_mark_node);
20342 }
20343 }
20344 else if (TREE_CODE (member) == SCOPE_REF
20345 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20346 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20347 {
20348 if (complain & tf_error)
20349 {
20350 if (TYPE_P (TREE_OPERAND (member, 0)))
20351 error ("%qT is not a class or namespace",
20352 TREE_OPERAND (member, 0));
20353 else
20354 error ("%qD is not a class or namespace",
20355 TREE_OPERAND (member, 0));
20356 }
20357 RETURN (error_mark_node);
20358 }
20359
20360 r = finish_class_member_access_expr (object, member,
20361 /*template_p=*/false,
20362 complain);
20363 if (TREE_CODE (r) == COMPONENT_REF)
20364 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20365 RETURN (r);
20366 }
20367
20368 case THROW_EXPR:
20369 RETURN (build_throw
20370 (input_location, RECUR (TREE_OPERAND (t, 0))));
20371
20372 case CONSTRUCTOR:
20373 {
20374 vec<constructor_elt, va_gc> *n;
20375 constructor_elt *ce;
20376 unsigned HOST_WIDE_INT idx;
20377 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20378 bool process_index_p;
20379 int newlen;
20380 bool need_copy_p = false;
20381 tree r;
20382
20383 if (type == error_mark_node)
20384 RETURN (error_mark_node);
20385
20386 /* We do not want to process the index of aggregate
20387 initializers as they are identifier nodes which will be
20388 looked up by digest_init. */
20389 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20390
20391 if (null_member_pointer_value_p (t))
20392 {
20393 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20394 RETURN (t);
20395 }
20396
20397 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20398 newlen = vec_safe_length (n);
20399 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20400 {
20401 if (ce->index && process_index_p
20402 /* An identifier index is looked up in the type
20403 being initialized, not the current scope. */
20404 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20405 ce->index = RECUR (ce->index);
20406
20407 if (PACK_EXPANSION_P (ce->value))
20408 {
20409 /* Substitute into the pack expansion. */
20410 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20411 in_decl);
20412
20413 if (ce->value == error_mark_node
20414 || PACK_EXPANSION_P (ce->value))
20415 ;
20416 else if (TREE_VEC_LENGTH (ce->value) == 1)
20417 /* Just move the argument into place. */
20418 ce->value = TREE_VEC_ELT (ce->value, 0);
20419 else
20420 {
20421 /* Update the length of the final CONSTRUCTOR
20422 arguments vector, and note that we will need to
20423 copy.*/
20424 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20425 need_copy_p = true;
20426 }
20427 }
20428 else
20429 ce->value = RECUR (ce->value);
20430 }
20431
20432 if (need_copy_p)
20433 {
20434 vec<constructor_elt, va_gc> *old_n = n;
20435
20436 vec_alloc (n, newlen);
20437 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20438 {
20439 if (TREE_CODE (ce->value) == TREE_VEC)
20440 {
20441 int i, len = TREE_VEC_LENGTH (ce->value);
20442 for (i = 0; i < len; ++i)
20443 CONSTRUCTOR_APPEND_ELT (n, 0,
20444 TREE_VEC_ELT (ce->value, i));
20445 }
20446 else
20447 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20448 }
20449 }
20450
20451 r = build_constructor (init_list_type_node, n);
20452 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20453 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20454 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20455
20456 if (TREE_HAS_CONSTRUCTOR (t))
20457 {
20458 fcl_t cl = fcl_functional;
20459 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20460 cl = fcl_c99;
20461 RETURN (finish_compound_literal (type, r, complain, cl));
20462 }
20463
20464 TREE_TYPE (r) = type;
20465 RETURN (r);
20466 }
20467
20468 case TYPEID_EXPR:
20469 {
20470 tree operand_0 = TREE_OPERAND (t, 0);
20471 if (TYPE_P (operand_0))
20472 {
20473 operand_0 = tsubst (operand_0, args, complain, in_decl);
20474 RETURN (get_typeid (operand_0, complain));
20475 }
20476 else
20477 {
20478 operand_0 = RECUR (operand_0);
20479 RETURN (build_typeid (operand_0, complain));
20480 }
20481 }
20482
20483 case VAR_DECL:
20484 if (!args)
20485 RETURN (t);
20486 /* Fall through */
20487
20488 case PARM_DECL:
20489 {
20490 tree r = tsubst_copy (t, args, complain, in_decl);
20491 /* ??? We're doing a subset of finish_id_expression here. */
20492 if (tree wrap = maybe_get_tls_wrapper_call (r))
20493 /* Replace an evaluated use of the thread_local variable with
20494 a call to its wrapper. */
20495 r = wrap;
20496 else if (outer_automatic_var_p (r))
20497 r = process_outer_var_ref (r, complain);
20498
20499 if (!TYPE_REF_P (TREE_TYPE (t)))
20500 /* If the original type was a reference, we'll be wrapped in
20501 the appropriate INDIRECT_REF. */
20502 r = convert_from_reference (r);
20503 RETURN (r);
20504 }
20505
20506 case VA_ARG_EXPR:
20507 {
20508 tree op0 = RECUR (TREE_OPERAND (t, 0));
20509 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20510 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20511 }
20512
20513 case OFFSETOF_EXPR:
20514 {
20515 tree object_ptr
20516 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20517 in_decl, /*function_p=*/false,
20518 /*integral_constant_expression_p=*/false);
20519 RETURN (finish_offsetof (object_ptr,
20520 RECUR (TREE_OPERAND (t, 0)),
20521 EXPR_LOCATION (t)));
20522 }
20523
20524 case ADDRESSOF_EXPR:
20525 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20526 RECUR (TREE_OPERAND (t, 0)), complain));
20527
20528 case TRAIT_EXPR:
20529 {
20530 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20531 complain, in_decl);
20532 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20533 complain, in_decl);
20534 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20535 TRAIT_EXPR_KIND (t), type1, type2));
20536 }
20537
20538 case STMT_EXPR:
20539 {
20540 tree old_stmt_expr = cur_stmt_expr;
20541 tree stmt_expr = begin_stmt_expr ();
20542
20543 cur_stmt_expr = stmt_expr;
20544 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20545 integral_constant_expression_p);
20546 stmt_expr = finish_stmt_expr (stmt_expr, false);
20547 cur_stmt_expr = old_stmt_expr;
20548
20549 /* If the resulting list of expression statement is empty,
20550 fold it further into void_node. */
20551 if (empty_expr_stmt_p (stmt_expr))
20552 stmt_expr = void_node;
20553
20554 RETURN (stmt_expr);
20555 }
20556
20557 case LAMBDA_EXPR:
20558 {
20559 if (complain & tf_partial)
20560 {
20561 /* We don't have a full set of template arguments yet; don't touch
20562 the lambda at all. */
20563 gcc_assert (processing_template_decl);
20564 return t;
20565 }
20566 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20567
20568 RETURN (build_lambda_object (r));
20569 }
20570
20571 case TARGET_EXPR:
20572 /* We can get here for a constant initializer of non-dependent type.
20573 FIXME stop folding in cp_parser_initializer_clause. */
20574 {
20575 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20576 complain);
20577 RETURN (r);
20578 }
20579
20580 case TRANSACTION_EXPR:
20581 RETURN (tsubst_expr(t, args, complain, in_decl,
20582 integral_constant_expression_p));
20583
20584 case PAREN_EXPR:
20585 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20586
20587 case VEC_PERM_EXPR:
20588 {
20589 tree op0 = RECUR (TREE_OPERAND (t, 0));
20590 tree op1 = RECUR (TREE_OPERAND (t, 1));
20591 tree op2 = RECUR (TREE_OPERAND (t, 2));
20592 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20593 complain));
20594 }
20595
20596 case REQUIRES_EXPR:
20597 {
20598 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20599 RETURN (r);
20600 }
20601
20602 case RANGE_EXPR:
20603 /* No need to substitute further, a RANGE_EXPR will always be built
20604 with constant operands. */
20605 RETURN (t);
20606
20607 case NON_LVALUE_EXPR:
20608 case VIEW_CONVERT_EXPR:
20609 if (location_wrapper_p (t))
20610 /* We need to do this here as well as in tsubst_copy so we get the
20611 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20612 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20613 EXPR_LOCATION (t)));
20614 /* fallthrough. */
20615
20616 default:
20617 /* Handle Objective-C++ constructs, if appropriate. */
20618 {
20619 tree subst
20620 = objcp_tsubst_copy_and_build (t, args, complain,
20621 in_decl, /*function_p=*/false);
20622 if (subst)
20623 RETURN (subst);
20624 }
20625 RETURN (tsubst_copy (t, args, complain, in_decl));
20626 }
20627
20628 #undef RECUR
20629 #undef RETURN
20630 out:
20631 input_location = save_loc;
20632 return retval;
20633 }
20634
20635 /* Verify that the instantiated ARGS are valid. For type arguments,
20636 make sure that the type's linkage is ok. For non-type arguments,
20637 make sure they are constants if they are integral or enumerations.
20638 Emit an error under control of COMPLAIN, and return TRUE on error. */
20639
20640 static bool
20641 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20642 {
20643 if (dependent_template_arg_p (t))
20644 return false;
20645 if (ARGUMENT_PACK_P (t))
20646 {
20647 tree vec = ARGUMENT_PACK_ARGS (t);
20648 int len = TREE_VEC_LENGTH (vec);
20649 bool result = false;
20650 int i;
20651
20652 for (i = 0; i < len; ++i)
20653 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20654 result = true;
20655 return result;
20656 }
20657 else if (TYPE_P (t))
20658 {
20659 /* [basic.link]: A name with no linkage (notably, the name
20660 of a class or enumeration declared in a local scope)
20661 shall not be used to declare an entity with linkage.
20662 This implies that names with no linkage cannot be used as
20663 template arguments
20664
20665 DR 757 relaxes this restriction for C++0x. */
20666 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20667 : no_linkage_check (t, /*relaxed_p=*/false));
20668
20669 if (nt)
20670 {
20671 /* DR 488 makes use of a type with no linkage cause
20672 type deduction to fail. */
20673 if (complain & tf_error)
20674 {
20675 if (TYPE_UNNAMED_P (nt))
20676 error ("%qT is/uses unnamed type", t);
20677 else
20678 error ("template argument for %qD uses local type %qT",
20679 tmpl, t);
20680 }
20681 return true;
20682 }
20683 /* In order to avoid all sorts of complications, we do not
20684 allow variably-modified types as template arguments. */
20685 else if (variably_modified_type_p (t, NULL_TREE))
20686 {
20687 if (complain & tf_error)
20688 error ("%qT is a variably modified type", t);
20689 return true;
20690 }
20691 }
20692 /* Class template and alias template arguments should be OK. */
20693 else if (DECL_TYPE_TEMPLATE_P (t))
20694 ;
20695 /* A non-type argument of integral or enumerated type must be a
20696 constant. */
20697 else if (TREE_TYPE (t)
20698 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20699 && !REFERENCE_REF_P (t)
20700 && !TREE_CONSTANT (t))
20701 {
20702 if (complain & tf_error)
20703 error ("integral expression %qE is not constant", t);
20704 return true;
20705 }
20706 return false;
20707 }
20708
20709 static bool
20710 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20711 {
20712 int ix, len = DECL_NTPARMS (tmpl);
20713 bool result = false;
20714
20715 for (ix = 0; ix != len; ix++)
20716 {
20717 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20718 result = true;
20719 }
20720 if (result && (complain & tf_error))
20721 error (" trying to instantiate %qD", tmpl);
20722 return result;
20723 }
20724
20725 /* We're out of SFINAE context now, so generate diagnostics for the access
20726 errors we saw earlier when instantiating D from TMPL and ARGS. */
20727
20728 static void
20729 recheck_decl_substitution (tree d, tree tmpl, tree args)
20730 {
20731 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20732 tree type = TREE_TYPE (pattern);
20733 location_t loc = input_location;
20734
20735 push_access_scope (d);
20736 push_deferring_access_checks (dk_no_deferred);
20737 input_location = DECL_SOURCE_LOCATION (pattern);
20738 tsubst (type, args, tf_warning_or_error, d);
20739 input_location = loc;
20740 pop_deferring_access_checks ();
20741 pop_access_scope (d);
20742 }
20743
20744 /* Instantiate the indicated variable, function, or alias template TMPL with
20745 the template arguments in TARG_PTR. */
20746
20747 static tree
20748 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20749 {
20750 tree targ_ptr = orig_args;
20751 tree fndecl;
20752 tree gen_tmpl;
20753 tree spec;
20754 bool access_ok = true;
20755
20756 if (tmpl == error_mark_node)
20757 return error_mark_node;
20758
20759 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20760
20761 /* If this function is a clone, handle it specially. */
20762 if (DECL_CLONED_FUNCTION_P (tmpl))
20763 {
20764 tree spec;
20765 tree clone;
20766
20767 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20768 DECL_CLONED_FUNCTION. */
20769 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20770 targ_ptr, complain);
20771 if (spec == error_mark_node)
20772 return error_mark_node;
20773
20774 /* Look for the clone. */
20775 FOR_EACH_CLONE (clone, spec)
20776 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20777 return clone;
20778 /* We should always have found the clone by now. */
20779 gcc_unreachable ();
20780 return NULL_TREE;
20781 }
20782
20783 if (targ_ptr == error_mark_node)
20784 return error_mark_node;
20785
20786 /* Check to see if we already have this specialization. */
20787 gen_tmpl = most_general_template (tmpl);
20788 if (TMPL_ARGS_DEPTH (targ_ptr)
20789 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20790 /* targ_ptr only has the innermost template args, so add the outer ones
20791 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20792 the case of a non-dependent call within a template definition). */
20793 targ_ptr = (add_outermost_template_args
20794 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20795 targ_ptr));
20796
20797 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20798 but it doesn't seem to be on the hot path. */
20799 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20800
20801 gcc_checking_assert (tmpl == gen_tmpl
20802 || ((fndecl
20803 = retrieve_specialization (tmpl, orig_args, 0))
20804 == spec)
20805 || fndecl == NULL_TREE);
20806
20807 if (spec != NULL_TREE)
20808 {
20809 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20810 {
20811 if (complain & tf_error)
20812 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20813 return error_mark_node;
20814 }
20815 return spec;
20816 }
20817
20818 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20819 complain))
20820 return error_mark_node;
20821
20822 /* We are building a FUNCTION_DECL, during which the access of its
20823 parameters and return types have to be checked. However this
20824 FUNCTION_DECL which is the desired context for access checking
20825 is not built yet. We solve this chicken-and-egg problem by
20826 deferring all checks until we have the FUNCTION_DECL. */
20827 push_deferring_access_checks (dk_deferred);
20828
20829 /* Instantiation of the function happens in the context of the function
20830 template, not the context of the overload resolution we're doing. */
20831 push_to_top_level ();
20832 /* If there are dependent arguments, e.g. because we're doing partial
20833 ordering, make sure processing_template_decl stays set. */
20834 if (uses_template_parms (targ_ptr))
20835 ++processing_template_decl;
20836 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20837 {
20838 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20839 complain, gen_tmpl, true);
20840 push_nested_class (ctx);
20841 }
20842
20843 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20844
20845 fndecl = NULL_TREE;
20846 if (VAR_P (pattern))
20847 {
20848 /* We need to determine if we're using a partial or explicit
20849 specialization now, because the type of the variable could be
20850 different. */
20851 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20852 tree elt = most_specialized_partial_spec (tid, complain);
20853 if (elt == error_mark_node)
20854 pattern = error_mark_node;
20855 else if (elt)
20856 {
20857 tree partial_tmpl = TREE_VALUE (elt);
20858 tree partial_args = TREE_PURPOSE (elt);
20859 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20860 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20861 }
20862 }
20863
20864 /* Substitute template parameters to obtain the specialization. */
20865 if (fndecl == NULL_TREE)
20866 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20867 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20868 pop_nested_class ();
20869 pop_from_top_level ();
20870
20871 if (fndecl == error_mark_node)
20872 {
20873 pop_deferring_access_checks ();
20874 return error_mark_node;
20875 }
20876
20877 /* The DECL_TI_TEMPLATE should always be the immediate parent
20878 template, not the most general template. */
20879 DECL_TI_TEMPLATE (fndecl) = tmpl;
20880 DECL_TI_ARGS (fndecl) = targ_ptr;
20881
20882 /* Now we know the specialization, compute access previously
20883 deferred. Do no access control for inheriting constructors,
20884 as we already checked access for the inherited constructor. */
20885 if (!(flag_new_inheriting_ctors
20886 && DECL_INHERITED_CTOR (fndecl)))
20887 {
20888 push_access_scope (fndecl);
20889 if (!perform_deferred_access_checks (complain))
20890 access_ok = false;
20891 pop_access_scope (fndecl);
20892 }
20893 pop_deferring_access_checks ();
20894
20895 /* If we've just instantiated the main entry point for a function,
20896 instantiate all the alternate entry points as well. We do this
20897 by cloning the instantiation of the main entry point, not by
20898 instantiating the template clones. */
20899 if (tree chain = DECL_CHAIN (gen_tmpl))
20900 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20901 clone_cdtor (fndecl, /*update_methods=*/false);
20902
20903 if (!access_ok)
20904 {
20905 if (!(complain & tf_error))
20906 {
20907 /* Remember to reinstantiate when we're out of SFINAE so the user
20908 can see the errors. */
20909 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20910 }
20911 return error_mark_node;
20912 }
20913 return fndecl;
20914 }
20915
20916 /* Wrapper for instantiate_template_1. */
20917
20918 tree
20919 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20920 {
20921 tree ret;
20922 timevar_push (TV_TEMPLATE_INST);
20923 ret = instantiate_template_1 (tmpl, orig_args, complain);
20924 timevar_pop (TV_TEMPLATE_INST);
20925 return ret;
20926 }
20927
20928 /* Instantiate the alias template TMPL with ARGS. Also push a template
20929 instantiation level, which instantiate_template doesn't do because
20930 functions and variables have sufficient context established by the
20931 callers. */
20932
20933 static tree
20934 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20935 {
20936 if (tmpl == error_mark_node || args == error_mark_node)
20937 return error_mark_node;
20938
20939 args =
20940 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20941 args, tmpl, complain,
20942 /*require_all_args=*/true,
20943 /*use_default_args=*/true);
20944
20945 /* FIXME check for satisfaction in check_instantiated_args. */
20946 if (flag_concepts
20947 && !any_dependent_template_arguments_p (args)
20948 && !constraints_satisfied_p (tmpl, args))
20949 {
20950 if (complain & tf_error)
20951 {
20952 auto_diagnostic_group d;
20953 error ("template constraint failure for %qD", tmpl);
20954 diagnose_constraints (input_location, tmpl, args);
20955 }
20956 return error_mark_node;
20957 }
20958
20959 if (!push_tinst_level (tmpl, args))
20960 return error_mark_node;
20961 tree r = instantiate_template (tmpl, args, complain);
20962 pop_tinst_level ();
20963
20964 return r;
20965 }
20966
20967 /* PARM is a template parameter pack for FN. Returns true iff
20968 PARM is used in a deducible way in the argument list of FN. */
20969
20970 static bool
20971 pack_deducible_p (tree parm, tree fn)
20972 {
20973 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20974 for (; t; t = TREE_CHAIN (t))
20975 {
20976 tree type = TREE_VALUE (t);
20977 tree packs;
20978 if (!PACK_EXPANSION_P (type))
20979 continue;
20980 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20981 packs; packs = TREE_CHAIN (packs))
20982 if (template_args_equal (TREE_VALUE (packs), parm))
20983 {
20984 /* The template parameter pack is used in a function parameter
20985 pack. If this is the end of the parameter list, the
20986 template parameter pack is deducible. */
20987 if (TREE_CHAIN (t) == void_list_node)
20988 return true;
20989 else
20990 /* Otherwise, not. Well, it could be deduced from
20991 a non-pack parameter, but doing so would end up with
20992 a deduction mismatch, so don't bother. */
20993 return false;
20994 }
20995 }
20996 /* The template parameter pack isn't used in any function parameter
20997 packs, but it might be used deeper, e.g. tuple<Args...>. */
20998 return true;
20999 }
21000
21001 /* Subroutine of fn_type_unification: check non-dependent parms for
21002 convertibility. */
21003
21004 static int
21005 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21006 tree fn, unification_kind_t strict, int flags,
21007 struct conversion **convs, bool explain_p)
21008 {
21009 /* Non-constructor methods need to leave a conversion for 'this', which
21010 isn't included in nargs here. */
21011 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21012 && !DECL_CONSTRUCTOR_P (fn));
21013
21014 for (unsigned ia = 0;
21015 parms && parms != void_list_node && ia < nargs; )
21016 {
21017 tree parm = TREE_VALUE (parms);
21018
21019 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21020 && (!TREE_CHAIN (parms)
21021 || TREE_CHAIN (parms) == void_list_node))
21022 /* For a function parameter pack that occurs at the end of the
21023 parameter-declaration-list, the type A of each remaining
21024 argument of the call is compared with the type P of the
21025 declarator-id of the function parameter pack. */
21026 break;
21027
21028 parms = TREE_CHAIN (parms);
21029
21030 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21031 /* For a function parameter pack that does not occur at the
21032 end of the parameter-declaration-list, the type of the
21033 parameter pack is a non-deduced context. */
21034 continue;
21035
21036 if (!uses_template_parms (parm))
21037 {
21038 tree arg = args[ia];
21039 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21040 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21041
21042 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21043 conv_p, explain_p))
21044 return 1;
21045 }
21046
21047 ++ia;
21048 }
21049
21050 return 0;
21051 }
21052
21053 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21054 NARGS elements of the arguments that are being used when calling
21055 it. TARGS is a vector into which the deduced template arguments
21056 are placed.
21057
21058 Returns either a FUNCTION_DECL for the matching specialization of FN or
21059 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21060 true, diagnostics will be printed to explain why it failed.
21061
21062 If FN is a conversion operator, or we are trying to produce a specific
21063 specialization, RETURN_TYPE is the return type desired.
21064
21065 The EXPLICIT_TARGS are explicit template arguments provided via a
21066 template-id.
21067
21068 The parameter STRICT is one of:
21069
21070 DEDUCE_CALL:
21071 We are deducing arguments for a function call, as in
21072 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21073 deducing arguments for a call to the result of a conversion
21074 function template, as in [over.call.object].
21075
21076 DEDUCE_CONV:
21077 We are deducing arguments for a conversion function, as in
21078 [temp.deduct.conv].
21079
21080 DEDUCE_EXACT:
21081 We are deducing arguments when doing an explicit instantiation
21082 as in [temp.explicit], when determining an explicit specialization
21083 as in [temp.expl.spec], or when taking the address of a function
21084 template, as in [temp.deduct.funcaddr]. */
21085
21086 tree
21087 fn_type_unification (tree fn,
21088 tree explicit_targs,
21089 tree targs,
21090 const tree *args,
21091 unsigned int nargs,
21092 tree return_type,
21093 unification_kind_t strict,
21094 int flags,
21095 struct conversion **convs,
21096 bool explain_p,
21097 bool decltype_p)
21098 {
21099 tree parms;
21100 tree fntype;
21101 tree decl = NULL_TREE;
21102 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21103 bool ok;
21104 static int deduction_depth;
21105 /* type_unification_real will pass back any access checks from default
21106 template argument substitution. */
21107 vec<deferred_access_check, va_gc> *checks = NULL;
21108 /* We don't have all the template args yet. */
21109 bool incomplete = true;
21110
21111 tree orig_fn = fn;
21112 if (flag_new_inheriting_ctors)
21113 fn = strip_inheriting_ctors (fn);
21114
21115 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21116 tree r = error_mark_node;
21117
21118 tree full_targs = targs;
21119 if (TMPL_ARGS_DEPTH (targs)
21120 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21121 full_targs = (add_outermost_template_args
21122 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21123 targs));
21124
21125 if (decltype_p)
21126 complain |= tf_decltype;
21127
21128 /* In C++0x, it's possible to have a function template whose type depends
21129 on itself recursively. This is most obvious with decltype, but can also
21130 occur with enumeration scope (c++/48969). So we need to catch infinite
21131 recursion and reject the substitution at deduction time; this function
21132 will return error_mark_node for any repeated substitution.
21133
21134 This also catches excessive recursion such as when f<N> depends on
21135 f<N-1> across all integers, and returns error_mark_node for all the
21136 substitutions back up to the initial one.
21137
21138 This is, of course, not reentrant. */
21139 if (excessive_deduction_depth)
21140 return error_mark_node;
21141 ++deduction_depth;
21142
21143 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21144
21145 fntype = TREE_TYPE (fn);
21146 if (explicit_targs)
21147 {
21148 /* [temp.deduct]
21149
21150 The specified template arguments must match the template
21151 parameters in kind (i.e., type, nontype, template), and there
21152 must not be more arguments than there are parameters;
21153 otherwise type deduction fails.
21154
21155 Nontype arguments must match the types of the corresponding
21156 nontype template parameters, or must be convertible to the
21157 types of the corresponding nontype parameters as specified in
21158 _temp.arg.nontype_, otherwise type deduction fails.
21159
21160 All references in the function type of the function template
21161 to the corresponding template parameters are replaced by the
21162 specified template argument values. If a substitution in a
21163 template parameter or in the function type of the function
21164 template results in an invalid type, type deduction fails. */
21165 int i, len = TREE_VEC_LENGTH (tparms);
21166 location_t loc = input_location;
21167 incomplete = false;
21168
21169 if (explicit_targs == error_mark_node)
21170 goto fail;
21171
21172 if (TMPL_ARGS_DEPTH (explicit_targs)
21173 < TMPL_ARGS_DEPTH (full_targs))
21174 explicit_targs = add_outermost_template_args (full_targs,
21175 explicit_targs);
21176
21177 /* Adjust any explicit template arguments before entering the
21178 substitution context. */
21179 explicit_targs
21180 = (coerce_template_parms (tparms, explicit_targs, fn,
21181 complain|tf_partial,
21182 /*require_all_args=*/false,
21183 /*use_default_args=*/false));
21184 if (explicit_targs == error_mark_node)
21185 goto fail;
21186
21187 /* Substitute the explicit args into the function type. This is
21188 necessary so that, for instance, explicitly declared function
21189 arguments can match null pointed constants. If we were given
21190 an incomplete set of explicit args, we must not do semantic
21191 processing during substitution as we could create partial
21192 instantiations. */
21193 for (i = 0; i < len; i++)
21194 {
21195 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21196 bool parameter_pack = false;
21197 tree targ = TREE_VEC_ELT (explicit_targs, i);
21198
21199 /* Dig out the actual parm. */
21200 if (TREE_CODE (parm) == TYPE_DECL
21201 || TREE_CODE (parm) == TEMPLATE_DECL)
21202 {
21203 parm = TREE_TYPE (parm);
21204 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21205 }
21206 else if (TREE_CODE (parm) == PARM_DECL)
21207 {
21208 parm = DECL_INITIAL (parm);
21209 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21210 }
21211
21212 if (targ == NULL_TREE)
21213 /* No explicit argument for this template parameter. */
21214 incomplete = true;
21215 else if (parameter_pack && pack_deducible_p (parm, fn))
21216 {
21217 /* Mark the argument pack as "incomplete". We could
21218 still deduce more arguments during unification.
21219 We remove this mark in type_unification_real. */
21220 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21221 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21222 = ARGUMENT_PACK_ARGS (targ);
21223
21224 /* We have some incomplete argument packs. */
21225 incomplete = true;
21226 }
21227 }
21228
21229 if (incomplete)
21230 {
21231 if (!push_tinst_level (fn, explicit_targs))
21232 {
21233 excessive_deduction_depth = true;
21234 goto fail;
21235 }
21236 ++processing_template_decl;
21237 input_location = DECL_SOURCE_LOCATION (fn);
21238 /* Ignore any access checks; we'll see them again in
21239 instantiate_template and they might have the wrong
21240 access path at this point. */
21241 push_deferring_access_checks (dk_deferred);
21242 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21243 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21244 pop_deferring_access_checks ();
21245 input_location = loc;
21246 --processing_template_decl;
21247 pop_tinst_level ();
21248
21249 if (fntype == error_mark_node)
21250 goto fail;
21251 }
21252
21253 /* Place the explicitly specified arguments in TARGS. */
21254 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21255 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21256 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21257 if (!incomplete && CHECKING_P
21258 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21259 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21260 (targs, NUM_TMPL_ARGS (explicit_targs));
21261 }
21262
21263 if (return_type && strict != DEDUCE_CALL)
21264 {
21265 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21266 new_args[0] = return_type;
21267 memcpy (new_args + 1, args, nargs * sizeof (tree));
21268 args = new_args;
21269 ++nargs;
21270 }
21271
21272 if (!incomplete)
21273 goto deduced;
21274
21275 /* Never do unification on the 'this' parameter. */
21276 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21277
21278 if (return_type && strict == DEDUCE_CALL)
21279 {
21280 /* We're deducing for a call to the result of a template conversion
21281 function. The parms we really want are in return_type. */
21282 if (INDIRECT_TYPE_P (return_type))
21283 return_type = TREE_TYPE (return_type);
21284 parms = TYPE_ARG_TYPES (return_type);
21285 }
21286 else if (return_type)
21287 {
21288 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21289 }
21290
21291 /* We allow incomplete unification without an error message here
21292 because the standard doesn't seem to explicitly prohibit it. Our
21293 callers must be ready to deal with unification failures in any
21294 event. */
21295
21296 /* If we aren't explaining yet, push tinst context so we can see where
21297 any errors (e.g. from class instantiations triggered by instantiation
21298 of default template arguments) come from. If we are explaining, this
21299 context is redundant. */
21300 if (!explain_p && !push_tinst_level (fn, targs))
21301 {
21302 excessive_deduction_depth = true;
21303 goto fail;
21304 }
21305
21306 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21307 full_targs, parms, args, nargs, /*subr=*/0,
21308 strict, &checks, explain_p);
21309 if (!explain_p)
21310 pop_tinst_level ();
21311 if (!ok)
21312 goto fail;
21313
21314 /* Now that we have bindings for all of the template arguments,
21315 ensure that the arguments deduced for the template template
21316 parameters have compatible template parameter lists. We cannot
21317 check this property before we have deduced all template
21318 arguments, because the template parameter types of a template
21319 template parameter might depend on prior template parameters
21320 deduced after the template template parameter. The following
21321 ill-formed example illustrates this issue:
21322
21323 template<typename T, template<T> class C> void f(C<5>, T);
21324
21325 template<int N> struct X {};
21326
21327 void g() {
21328 f(X<5>(), 5l); // error: template argument deduction fails
21329 }
21330
21331 The template parameter list of 'C' depends on the template type
21332 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21333 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21334 time that we deduce 'C'. */
21335 if (!template_template_parm_bindings_ok_p
21336 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21337 {
21338 unify_inconsistent_template_template_parameters (explain_p);
21339 goto fail;
21340 }
21341
21342 deduced:
21343
21344 /* CWG2369: Check satisfaction before non-deducible conversions. */
21345 if (!constraints_satisfied_p (fn, targs))
21346 {
21347 if (explain_p)
21348 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21349 goto fail;
21350 }
21351
21352 /* DR 1391: All parameters have args, now check non-dependent parms for
21353 convertibility. We don't do this if all args were explicitly specified,
21354 as the standard says that we substitute explicit args immediately. */
21355 if (incomplete
21356 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21357 convs, explain_p))
21358 goto fail;
21359
21360 /* All is well so far. Now, check:
21361
21362 [temp.deduct]
21363
21364 When all template arguments have been deduced, all uses of
21365 template parameters in nondeduced contexts are replaced with
21366 the corresponding deduced argument values. If the
21367 substitution results in an invalid type, as described above,
21368 type deduction fails. */
21369 if (!push_tinst_level (fn, targs))
21370 {
21371 excessive_deduction_depth = true;
21372 goto fail;
21373 }
21374
21375 /* Also collect access checks from the instantiation. */
21376 reopen_deferring_access_checks (checks);
21377
21378 decl = instantiate_template (fn, targs, complain);
21379
21380 checks = get_deferred_access_checks ();
21381 pop_deferring_access_checks ();
21382
21383 pop_tinst_level ();
21384
21385 if (decl == error_mark_node)
21386 goto fail;
21387
21388 /* Now perform any access checks encountered during substitution. */
21389 push_access_scope (decl);
21390 ok = perform_access_checks (checks, complain);
21391 pop_access_scope (decl);
21392 if (!ok)
21393 goto fail;
21394
21395 /* If we're looking for an exact match, check that what we got
21396 is indeed an exact match. It might not be if some template
21397 parameters are used in non-deduced contexts. But don't check
21398 for an exact match if we have dependent template arguments;
21399 in that case we're doing partial ordering, and we already know
21400 that we have two candidates that will provide the actual type. */
21401 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21402 {
21403 tree substed = TREE_TYPE (decl);
21404 unsigned int i;
21405
21406 tree sarg
21407 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21408 if (return_type)
21409 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21410 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21411 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21412 {
21413 unify_type_mismatch (explain_p, args[i],
21414 TREE_VALUE (sarg));
21415 goto fail;
21416 }
21417 }
21418
21419 /* After doing deduction with the inherited constructor, actually return an
21420 instantiation of the inheriting constructor. */
21421 if (orig_fn != fn)
21422 decl = instantiate_template (orig_fn, targs, complain);
21423
21424 r = decl;
21425
21426 fail:
21427 --deduction_depth;
21428 if (excessive_deduction_depth)
21429 {
21430 if (deduction_depth == 0)
21431 /* Reset once we're all the way out. */
21432 excessive_deduction_depth = false;
21433 }
21434
21435 return r;
21436 }
21437
21438 /* Adjust types before performing type deduction, as described in
21439 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21440 sections are symmetric. PARM is the type of a function parameter
21441 or the return type of the conversion function. ARG is the type of
21442 the argument passed to the call, or the type of the value
21443 initialized with the result of the conversion function.
21444 ARG_EXPR is the original argument expression, which may be null. */
21445
21446 static int
21447 maybe_adjust_types_for_deduction (unification_kind_t strict,
21448 tree* parm,
21449 tree* arg,
21450 tree arg_expr)
21451 {
21452 int result = 0;
21453
21454 switch (strict)
21455 {
21456 case DEDUCE_CALL:
21457 break;
21458
21459 case DEDUCE_CONV:
21460 /* Swap PARM and ARG throughout the remainder of this
21461 function; the handling is precisely symmetric since PARM
21462 will initialize ARG rather than vice versa. */
21463 std::swap (parm, arg);
21464 break;
21465
21466 case DEDUCE_EXACT:
21467 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21468 too, but here handle it by stripping the reference from PARM
21469 rather than by adding it to ARG. */
21470 if (TYPE_REF_P (*parm)
21471 && TYPE_REF_IS_RVALUE (*parm)
21472 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21473 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21474 && TYPE_REF_P (*arg)
21475 && !TYPE_REF_IS_RVALUE (*arg))
21476 *parm = TREE_TYPE (*parm);
21477 /* Nothing else to do in this case. */
21478 return 0;
21479
21480 default:
21481 gcc_unreachable ();
21482 }
21483
21484 if (!TYPE_REF_P (*parm))
21485 {
21486 /* [temp.deduct.call]
21487
21488 If P is not a reference type:
21489
21490 --If A is an array type, the pointer type produced by the
21491 array-to-pointer standard conversion (_conv.array_) is
21492 used in place of A for type deduction; otherwise,
21493
21494 --If A is a function type, the pointer type produced by
21495 the function-to-pointer standard conversion
21496 (_conv.func_) is used in place of A for type deduction;
21497 otherwise,
21498
21499 --If A is a cv-qualified type, the top level
21500 cv-qualifiers of A's type are ignored for type
21501 deduction. */
21502 if (TREE_CODE (*arg) == ARRAY_TYPE)
21503 *arg = build_pointer_type (TREE_TYPE (*arg));
21504 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21505 *arg = build_pointer_type (*arg);
21506 else
21507 *arg = TYPE_MAIN_VARIANT (*arg);
21508 }
21509
21510 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21511 reference to a cv-unqualified template parameter that does not represent a
21512 template parameter of a class template (during class template argument
21513 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21514 an lvalue, the type "lvalue reference to A" is used in place of A for type
21515 deduction. */
21516 if (TYPE_REF_P (*parm)
21517 && TYPE_REF_IS_RVALUE (*parm)
21518 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21519 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21520 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21521 && (arg_expr ? lvalue_p (arg_expr)
21522 /* try_one_overload doesn't provide an arg_expr, but
21523 functions are always lvalues. */
21524 : TREE_CODE (*arg) == FUNCTION_TYPE))
21525 *arg = build_reference_type (*arg);
21526
21527 /* [temp.deduct.call]
21528
21529 If P is a cv-qualified type, the top level cv-qualifiers
21530 of P's type are ignored for type deduction. If P is a
21531 reference type, the type referred to by P is used for
21532 type deduction. */
21533 *parm = TYPE_MAIN_VARIANT (*parm);
21534 if (TYPE_REF_P (*parm))
21535 {
21536 *parm = TREE_TYPE (*parm);
21537 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21538 }
21539
21540 /* DR 322. For conversion deduction, remove a reference type on parm
21541 too (which has been swapped into ARG). */
21542 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21543 *arg = TREE_TYPE (*arg);
21544
21545 return result;
21546 }
21547
21548 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21549 template which doesn't contain any deducible template parameters; check if
21550 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21551 unify_one_argument. */
21552
21553 static int
21554 check_non_deducible_conversion (tree parm, tree arg, int strict,
21555 int flags, struct conversion **conv_p,
21556 bool explain_p)
21557 {
21558 tree type;
21559
21560 if (!TYPE_P (arg))
21561 type = TREE_TYPE (arg);
21562 else
21563 type = arg;
21564
21565 if (same_type_p (parm, type))
21566 return unify_success (explain_p);
21567
21568 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21569 if (strict == DEDUCE_CONV)
21570 {
21571 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21572 return unify_success (explain_p);
21573 }
21574 else if (strict != DEDUCE_EXACT)
21575 {
21576 bool ok = false;
21577 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21578 if (conv_p)
21579 /* Avoid recalculating this in add_function_candidate. */
21580 ok = (*conv_p
21581 = good_conversion (parm, type, conv_arg, flags, complain));
21582 else
21583 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21584 if (ok)
21585 return unify_success (explain_p);
21586 }
21587
21588 if (strict == DEDUCE_EXACT)
21589 return unify_type_mismatch (explain_p, parm, arg);
21590 else
21591 return unify_arg_conversion (explain_p, parm, type, arg);
21592 }
21593
21594 static bool uses_deducible_template_parms (tree type);
21595
21596 /* Returns true iff the expression EXPR is one from which a template
21597 argument can be deduced. In other words, if it's an undecorated
21598 use of a template non-type parameter. */
21599
21600 static bool
21601 deducible_expression (tree expr)
21602 {
21603 /* Strip implicit conversions. */
21604 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21605 expr = TREE_OPERAND (expr, 0);
21606 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21607 }
21608
21609 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21610 deducible way; that is, if it has a max value of <PARM> - 1. */
21611
21612 static bool
21613 deducible_array_bound (tree domain)
21614 {
21615 if (domain == NULL_TREE)
21616 return false;
21617
21618 tree max = TYPE_MAX_VALUE (domain);
21619 if (TREE_CODE (max) != MINUS_EXPR)
21620 return false;
21621
21622 return deducible_expression (TREE_OPERAND (max, 0));
21623 }
21624
21625 /* Returns true iff the template arguments ARGS use a template parameter
21626 in a deducible way. */
21627
21628 static bool
21629 deducible_template_args (tree args)
21630 {
21631 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21632 {
21633 bool deducible;
21634 tree elt = TREE_VEC_ELT (args, i);
21635 if (ARGUMENT_PACK_P (elt))
21636 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21637 else
21638 {
21639 if (PACK_EXPANSION_P (elt))
21640 elt = PACK_EXPANSION_PATTERN (elt);
21641 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21642 deducible = true;
21643 else if (TYPE_P (elt))
21644 deducible = uses_deducible_template_parms (elt);
21645 else
21646 deducible = deducible_expression (elt);
21647 }
21648 if (deducible)
21649 return true;
21650 }
21651 return false;
21652 }
21653
21654 /* Returns true iff TYPE contains any deducible references to template
21655 parameters, as per 14.8.2.5. */
21656
21657 static bool
21658 uses_deducible_template_parms (tree type)
21659 {
21660 if (PACK_EXPANSION_P (type))
21661 type = PACK_EXPANSION_PATTERN (type);
21662
21663 /* T
21664 cv-list T
21665 TT<T>
21666 TT<i>
21667 TT<> */
21668 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21669 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21670 return true;
21671
21672 /* T*
21673 T&
21674 T&& */
21675 if (INDIRECT_TYPE_P (type))
21676 return uses_deducible_template_parms (TREE_TYPE (type));
21677
21678 /* T[integer-constant ]
21679 type [i] */
21680 if (TREE_CODE (type) == ARRAY_TYPE)
21681 return (uses_deducible_template_parms (TREE_TYPE (type))
21682 || deducible_array_bound (TYPE_DOMAIN (type)));
21683
21684 /* T type ::*
21685 type T::*
21686 T T::*
21687 T (type ::*)()
21688 type (T::*)()
21689 type (type ::*)(T)
21690 type (T::*)(T)
21691 T (type ::*)(T)
21692 T (T::*)()
21693 T (T::*)(T) */
21694 if (TYPE_PTRMEM_P (type))
21695 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21696 || (uses_deducible_template_parms
21697 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21698
21699 /* template-name <T> (where template-name refers to a class template)
21700 template-name <i> (where template-name refers to a class template) */
21701 if (CLASS_TYPE_P (type)
21702 && CLASSTYPE_TEMPLATE_INFO (type)
21703 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21704 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21705 (CLASSTYPE_TI_ARGS (type)));
21706
21707 /* type (T)
21708 T()
21709 T(T) */
21710 if (FUNC_OR_METHOD_TYPE_P (type))
21711 {
21712 if (uses_deducible_template_parms (TREE_TYPE (type)))
21713 return true;
21714 tree parm = TYPE_ARG_TYPES (type);
21715 if (TREE_CODE (type) == METHOD_TYPE)
21716 parm = TREE_CHAIN (parm);
21717 for (; parm; parm = TREE_CHAIN (parm))
21718 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21719 return true;
21720 }
21721
21722 return false;
21723 }
21724
21725 /* Subroutine of type_unification_real and unify_pack_expansion to
21726 handle unification of a single P/A pair. Parameters are as
21727 for those functions. */
21728
21729 static int
21730 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21731 int subr, unification_kind_t strict,
21732 bool explain_p)
21733 {
21734 tree arg_expr = NULL_TREE;
21735 int arg_strict;
21736
21737 if (arg == error_mark_node || parm == error_mark_node)
21738 return unify_invalid (explain_p);
21739 if (arg == unknown_type_node)
21740 /* We can't deduce anything from this, but we might get all the
21741 template args from other function args. */
21742 return unify_success (explain_p);
21743
21744 /* Implicit conversions (Clause 4) will be performed on a function
21745 argument to convert it to the type of the corresponding function
21746 parameter if the parameter type contains no template-parameters that
21747 participate in template argument deduction. */
21748 if (strict != DEDUCE_EXACT
21749 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21750 /* For function parameters with no deducible template parameters,
21751 just return. We'll check non-dependent conversions later. */
21752 return unify_success (explain_p);
21753
21754 switch (strict)
21755 {
21756 case DEDUCE_CALL:
21757 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21758 | UNIFY_ALLOW_MORE_CV_QUAL
21759 | UNIFY_ALLOW_DERIVED);
21760 break;
21761
21762 case DEDUCE_CONV:
21763 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21764 break;
21765
21766 case DEDUCE_EXACT:
21767 arg_strict = UNIFY_ALLOW_NONE;
21768 break;
21769
21770 default:
21771 gcc_unreachable ();
21772 }
21773
21774 /* We only do these transformations if this is the top-level
21775 parameter_type_list in a call or declaration matching; in other
21776 situations (nested function declarators, template argument lists) we
21777 won't be comparing a type to an expression, and we don't do any type
21778 adjustments. */
21779 if (!subr)
21780 {
21781 if (!TYPE_P (arg))
21782 {
21783 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21784 if (type_unknown_p (arg))
21785 {
21786 /* [temp.deduct.type] A template-argument can be
21787 deduced from a pointer to function or pointer
21788 to member function argument if the set of
21789 overloaded functions does not contain function
21790 templates and at most one of a set of
21791 overloaded functions provides a unique
21792 match. */
21793 resolve_overloaded_unification (tparms, targs, parm,
21794 arg, strict,
21795 arg_strict, explain_p);
21796 /* If a unique match was not found, this is a
21797 non-deduced context, so we still succeed. */
21798 return unify_success (explain_p);
21799 }
21800
21801 arg_expr = arg;
21802 arg = unlowered_expr_type (arg);
21803 if (arg == error_mark_node)
21804 return unify_invalid (explain_p);
21805 }
21806
21807 arg_strict |=
21808 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21809 }
21810 else
21811 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21812 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21813 return unify_template_argument_mismatch (explain_p, parm, arg);
21814
21815 /* For deduction from an init-list we need the actual list. */
21816 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21817 arg = arg_expr;
21818 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21819 }
21820
21821 /* for_each_template_parm callback that always returns 0. */
21822
21823 static int
21824 zero_r (tree, void *)
21825 {
21826 return 0;
21827 }
21828
21829 /* for_each_template_parm any_fn callback to handle deduction of a template
21830 type argument from the type of an array bound. */
21831
21832 static int
21833 array_deduction_r (tree t, void *data)
21834 {
21835 tree_pair_p d = (tree_pair_p)data;
21836 tree &tparms = d->purpose;
21837 tree &targs = d->value;
21838
21839 if (TREE_CODE (t) == ARRAY_TYPE)
21840 if (tree dom = TYPE_DOMAIN (t))
21841 if (tree max = TYPE_MAX_VALUE (dom))
21842 {
21843 if (TREE_CODE (max) == MINUS_EXPR)
21844 max = TREE_OPERAND (max, 0);
21845 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21846 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21847 UNIFY_ALLOW_NONE, /*explain*/false);
21848 }
21849
21850 /* Keep walking. */
21851 return 0;
21852 }
21853
21854 /* Try to deduce any not-yet-deduced template type arguments from the type of
21855 an array bound. This is handled separately from unify because 14.8.2.5 says
21856 "The type of a type parameter is only deduced from an array bound if it is
21857 not otherwise deduced." */
21858
21859 static void
21860 try_array_deduction (tree tparms, tree targs, tree parm)
21861 {
21862 tree_pair_s data = { tparms, targs };
21863 hash_set<tree> visited;
21864 for_each_template_parm (parm, zero_r, &data, &visited,
21865 /*nondeduced*/false, array_deduction_r);
21866 }
21867
21868 /* Most parms like fn_type_unification.
21869
21870 If SUBR is 1, we're being called recursively (to unify the
21871 arguments of a function or method parameter of a function
21872 template).
21873
21874 CHECKS is a pointer to a vector of access checks encountered while
21875 substituting default template arguments. */
21876
21877 static int
21878 type_unification_real (tree tparms,
21879 tree full_targs,
21880 tree xparms,
21881 const tree *xargs,
21882 unsigned int xnargs,
21883 int subr,
21884 unification_kind_t strict,
21885 vec<deferred_access_check, va_gc> **checks,
21886 bool explain_p)
21887 {
21888 tree parm, arg;
21889 int i;
21890 int ntparms = TREE_VEC_LENGTH (tparms);
21891 int saw_undeduced = 0;
21892 tree parms;
21893 const tree *args;
21894 unsigned int nargs;
21895 unsigned int ia;
21896
21897 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21898 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21899 gcc_assert (ntparms > 0);
21900
21901 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21902
21903 /* Reset the number of non-defaulted template arguments contained
21904 in TARGS. */
21905 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21906
21907 again:
21908 parms = xparms;
21909 args = xargs;
21910 nargs = xnargs;
21911
21912 ia = 0;
21913 while (parms && parms != void_list_node
21914 && ia < nargs)
21915 {
21916 parm = TREE_VALUE (parms);
21917
21918 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21919 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21920 /* For a function parameter pack that occurs at the end of the
21921 parameter-declaration-list, the type A of each remaining
21922 argument of the call is compared with the type P of the
21923 declarator-id of the function parameter pack. */
21924 break;
21925
21926 parms = TREE_CHAIN (parms);
21927
21928 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21929 /* For a function parameter pack that does not occur at the
21930 end of the parameter-declaration-list, the type of the
21931 parameter pack is a non-deduced context. */
21932 continue;
21933
21934 arg = args[ia];
21935 ++ia;
21936
21937 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21938 explain_p))
21939 return 1;
21940 }
21941
21942 if (parms
21943 && parms != void_list_node
21944 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21945 {
21946 /* Unify the remaining arguments with the pack expansion type. */
21947 tree argvec;
21948 tree parmvec = make_tree_vec (1);
21949
21950 /* Allocate a TREE_VEC and copy in all of the arguments */
21951 argvec = make_tree_vec (nargs - ia);
21952 for (i = 0; ia < nargs; ++ia, ++i)
21953 TREE_VEC_ELT (argvec, i) = args[ia];
21954
21955 /* Copy the parameter into parmvec. */
21956 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21957 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21958 /*subr=*/subr, explain_p))
21959 return 1;
21960
21961 /* Advance to the end of the list of parameters. */
21962 parms = TREE_CHAIN (parms);
21963 }
21964
21965 /* Fail if we've reached the end of the parm list, and more args
21966 are present, and the parm list isn't variadic. */
21967 if (ia < nargs && parms == void_list_node)
21968 return unify_too_many_arguments (explain_p, nargs, ia);
21969 /* Fail if parms are left and they don't have default values and
21970 they aren't all deduced as empty packs (c++/57397). This is
21971 consistent with sufficient_parms_p. */
21972 if (parms && parms != void_list_node
21973 && TREE_PURPOSE (parms) == NULL_TREE)
21974 {
21975 unsigned int count = nargs;
21976 tree p = parms;
21977 bool type_pack_p;
21978 do
21979 {
21980 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21981 if (!type_pack_p)
21982 count++;
21983 p = TREE_CHAIN (p);
21984 }
21985 while (p && p != void_list_node);
21986 if (count != nargs)
21987 return unify_too_few_arguments (explain_p, ia, count,
21988 type_pack_p);
21989 }
21990
21991 if (!subr)
21992 {
21993 tsubst_flags_t complain = (explain_p
21994 ? tf_warning_or_error
21995 : tf_none);
21996 bool tried_array_deduction = (cxx_dialect < cxx17);
21997
21998 for (i = 0; i < ntparms; i++)
21999 {
22000 tree targ = TREE_VEC_ELT (targs, i);
22001 tree tparm = TREE_VEC_ELT (tparms, i);
22002
22003 /* Clear the "incomplete" flags on all argument packs now so that
22004 substituting them into later default arguments works. */
22005 if (targ && ARGUMENT_PACK_P (targ))
22006 {
22007 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22008 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22009 }
22010
22011 if (targ || tparm == error_mark_node)
22012 continue;
22013 tparm = TREE_VALUE (tparm);
22014
22015 if (TREE_CODE (tparm) == TYPE_DECL
22016 && !tried_array_deduction)
22017 {
22018 try_array_deduction (tparms, targs, xparms);
22019 tried_array_deduction = true;
22020 if (TREE_VEC_ELT (targs, i))
22021 continue;
22022 }
22023
22024 /* If this is an undeduced nontype parameter that depends on
22025 a type parameter, try another pass; its type may have been
22026 deduced from a later argument than the one from which
22027 this parameter can be deduced. */
22028 if (TREE_CODE (tparm) == PARM_DECL
22029 && uses_template_parms (TREE_TYPE (tparm))
22030 && saw_undeduced < 2)
22031 {
22032 saw_undeduced = 1;
22033 continue;
22034 }
22035
22036 /* Core issue #226 (C++0x) [temp.deduct]:
22037
22038 If a template argument has not been deduced, its
22039 default template argument, if any, is used.
22040
22041 When we are in C++98 mode, TREE_PURPOSE will either
22042 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22043 to explicitly check cxx_dialect here. */
22044 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22045 /* OK, there is a default argument. Wait until after the
22046 conversion check to do substitution. */
22047 continue;
22048
22049 /* If the type parameter is a parameter pack, then it will
22050 be deduced to an empty parameter pack. */
22051 if (template_parameter_pack_p (tparm))
22052 {
22053 tree arg;
22054
22055 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22056 {
22057 arg = make_node (NONTYPE_ARGUMENT_PACK);
22058 TREE_CONSTANT (arg) = 1;
22059 }
22060 else
22061 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22062
22063 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22064
22065 TREE_VEC_ELT (targs, i) = arg;
22066 continue;
22067 }
22068
22069 return unify_parameter_deduction_failure (explain_p, tparm);
22070 }
22071
22072 /* Now substitute into the default template arguments. */
22073 for (i = 0; i < ntparms; i++)
22074 {
22075 tree targ = TREE_VEC_ELT (targs, i);
22076 tree tparm = TREE_VEC_ELT (tparms, i);
22077
22078 if (targ || tparm == error_mark_node)
22079 continue;
22080 tree parm = TREE_VALUE (tparm);
22081 tree arg = TREE_PURPOSE (tparm);
22082 reopen_deferring_access_checks (*checks);
22083 location_t save_loc = input_location;
22084 if (DECL_P (parm))
22085 input_location = DECL_SOURCE_LOCATION (parm);
22086
22087 if (saw_undeduced == 1
22088 && TREE_CODE (parm) == PARM_DECL
22089 && uses_template_parms (TREE_TYPE (parm)))
22090 {
22091 /* The type of this non-type parameter depends on undeduced
22092 parameters. Don't try to use its default argument yet,
22093 since we might deduce an argument for it on the next pass,
22094 but do check whether the arguments we already have cause
22095 substitution failure, so that that happens before we try
22096 later default arguments (78489). */
22097 ++processing_template_decl;
22098 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22099 NULL_TREE);
22100 --processing_template_decl;
22101 if (type == error_mark_node)
22102 arg = error_mark_node;
22103 else
22104 arg = NULL_TREE;
22105 }
22106 else
22107 {
22108 /* Even if the call is happening in template context, getting
22109 here means it's non-dependent, and a default argument is
22110 considered a separate definition under [temp.decls], so we can
22111 do this substitution without processing_template_decl. This
22112 is important if the default argument contains something that
22113 might be instantiation-dependent like access (87480). */
22114 processing_template_decl_sentinel s;
22115 tree substed = NULL_TREE;
22116 if (saw_undeduced == 1)
22117 {
22118 /* First instatiate in template context, in case we still
22119 depend on undeduced template parameters. */
22120 ++processing_template_decl;
22121 substed = tsubst_template_arg (arg, full_targs, complain,
22122 NULL_TREE);
22123 --processing_template_decl;
22124 if (substed != error_mark_node
22125 && !uses_template_parms (substed))
22126 /* We replaced all the tparms, substitute again out of
22127 template context. */
22128 substed = NULL_TREE;
22129 }
22130 if (!substed)
22131 substed = tsubst_template_arg (arg, full_targs, complain,
22132 NULL_TREE);
22133
22134 if (!uses_template_parms (substed))
22135 arg = convert_template_argument (parm, substed, full_targs,
22136 complain, i, NULL_TREE);
22137 else if (saw_undeduced == 1)
22138 arg = NULL_TREE;
22139 else
22140 arg = error_mark_node;
22141 }
22142
22143 input_location = save_loc;
22144 *checks = get_deferred_access_checks ();
22145 pop_deferring_access_checks ();
22146
22147 if (arg == error_mark_node)
22148 return 1;
22149 else if (arg)
22150 {
22151 TREE_VEC_ELT (targs, i) = arg;
22152 /* The position of the first default template argument,
22153 is also the number of non-defaulted arguments in TARGS.
22154 Record that. */
22155 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22156 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22157 }
22158 }
22159
22160 if (saw_undeduced++ == 1)
22161 goto again;
22162 }
22163
22164 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22165 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22166
22167 return unify_success (explain_p);
22168 }
22169
22170 /* Subroutine of type_unification_real. Args are like the variables
22171 at the call site. ARG is an overloaded function (or template-id);
22172 we try deducing template args from each of the overloads, and if
22173 only one succeeds, we go with that. Modifies TARGS and returns
22174 true on success. */
22175
22176 static bool
22177 resolve_overloaded_unification (tree tparms,
22178 tree targs,
22179 tree parm,
22180 tree arg,
22181 unification_kind_t strict,
22182 int sub_strict,
22183 bool explain_p)
22184 {
22185 tree tempargs = copy_node (targs);
22186 int good = 0;
22187 tree goodfn = NULL_TREE;
22188 bool addr_p;
22189
22190 if (TREE_CODE (arg) == ADDR_EXPR)
22191 {
22192 arg = TREE_OPERAND (arg, 0);
22193 addr_p = true;
22194 }
22195 else
22196 addr_p = false;
22197
22198 if (TREE_CODE (arg) == COMPONENT_REF)
22199 /* Handle `&x' where `x' is some static or non-static member
22200 function name. */
22201 arg = TREE_OPERAND (arg, 1);
22202
22203 if (TREE_CODE (arg) == OFFSET_REF)
22204 arg = TREE_OPERAND (arg, 1);
22205
22206 /* Strip baselink information. */
22207 if (BASELINK_P (arg))
22208 arg = BASELINK_FUNCTIONS (arg);
22209
22210 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22211 {
22212 /* If we got some explicit template args, we need to plug them into
22213 the affected templates before we try to unify, in case the
22214 explicit args will completely resolve the templates in question. */
22215
22216 int ok = 0;
22217 tree expl_subargs = TREE_OPERAND (arg, 1);
22218 arg = TREE_OPERAND (arg, 0);
22219
22220 for (lkp_iterator iter (arg); iter; ++iter)
22221 {
22222 tree fn = *iter;
22223 tree subargs, elem;
22224
22225 if (TREE_CODE (fn) != TEMPLATE_DECL)
22226 continue;
22227
22228 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22229 expl_subargs, NULL_TREE, tf_none,
22230 /*require_all_args=*/true,
22231 /*use_default_args=*/true);
22232 if (subargs != error_mark_node
22233 && !any_dependent_template_arguments_p (subargs))
22234 {
22235 fn = instantiate_template (fn, subargs, tf_none);
22236 if (!constraints_satisfied_p (fn))
22237 continue;
22238 if (undeduced_auto_decl (fn))
22239 {
22240 /* Instantiate the function to deduce its return type. */
22241 ++function_depth;
22242 instantiate_decl (fn, /*defer*/false, /*class*/false);
22243 --function_depth;
22244 }
22245
22246 elem = TREE_TYPE (fn);
22247 if (try_one_overload (tparms, targs, tempargs, parm,
22248 elem, strict, sub_strict, addr_p, explain_p)
22249 && (!goodfn || !same_type_p (goodfn, elem)))
22250 {
22251 goodfn = elem;
22252 ++good;
22253 }
22254 }
22255 else if (subargs)
22256 ++ok;
22257 }
22258 /* If no templates (or more than one) are fully resolved by the
22259 explicit arguments, this template-id is a non-deduced context; it
22260 could still be OK if we deduce all template arguments for the
22261 enclosing call through other arguments. */
22262 if (good != 1)
22263 good = ok;
22264 }
22265 else if (!OVL_P (arg))
22266 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22267 -- but the deduction does not succeed because the expression is
22268 not just the function on its own. */
22269 return false;
22270 else
22271 for (lkp_iterator iter (arg); iter; ++iter)
22272 {
22273 tree fn = *iter;
22274 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22275 strict, sub_strict, addr_p, explain_p)
22276 && (!goodfn || !decls_match (goodfn, fn)))
22277 {
22278 goodfn = fn;
22279 ++good;
22280 }
22281 }
22282
22283 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22284 to function or pointer to member function argument if the set of
22285 overloaded functions does not contain function templates and at most
22286 one of a set of overloaded functions provides a unique match.
22287
22288 So if we found multiple possibilities, we return success but don't
22289 deduce anything. */
22290
22291 if (good == 1)
22292 {
22293 int i = TREE_VEC_LENGTH (targs);
22294 for (; i--; )
22295 if (TREE_VEC_ELT (tempargs, i))
22296 {
22297 tree old = TREE_VEC_ELT (targs, i);
22298 tree new_ = TREE_VEC_ELT (tempargs, i);
22299 if (new_ && old && ARGUMENT_PACK_P (old)
22300 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22301 /* Don't forget explicit template arguments in a pack. */
22302 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22303 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22304 TREE_VEC_ELT (targs, i) = new_;
22305 }
22306 }
22307 if (good)
22308 return true;
22309
22310 return false;
22311 }
22312
22313 /* Core DR 115: In contexts where deduction is done and fails, or in
22314 contexts where deduction is not done, if a template argument list is
22315 specified and it, along with any default template arguments, identifies
22316 a single function template specialization, then the template-id is an
22317 lvalue for the function template specialization. */
22318
22319 tree
22320 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22321 {
22322 tree expr, offset, baselink;
22323 bool addr;
22324
22325 if (!type_unknown_p (orig_expr))
22326 return orig_expr;
22327
22328 expr = orig_expr;
22329 addr = false;
22330 offset = NULL_TREE;
22331 baselink = NULL_TREE;
22332
22333 if (TREE_CODE (expr) == ADDR_EXPR)
22334 {
22335 expr = TREE_OPERAND (expr, 0);
22336 addr = true;
22337 }
22338 if (TREE_CODE (expr) == OFFSET_REF)
22339 {
22340 offset = expr;
22341 expr = TREE_OPERAND (expr, 1);
22342 }
22343 if (BASELINK_P (expr))
22344 {
22345 baselink = expr;
22346 expr = BASELINK_FUNCTIONS (expr);
22347 }
22348
22349 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22350 {
22351 int good = 0;
22352 tree goodfn = NULL_TREE;
22353
22354 /* If we got some explicit template args, we need to plug them into
22355 the affected templates before we try to unify, in case the
22356 explicit args will completely resolve the templates in question. */
22357
22358 tree expl_subargs = TREE_OPERAND (expr, 1);
22359 tree arg = TREE_OPERAND (expr, 0);
22360 tree badfn = NULL_TREE;
22361 tree badargs = NULL_TREE;
22362
22363 for (lkp_iterator iter (arg); iter; ++iter)
22364 {
22365 tree fn = *iter;
22366 tree subargs, elem;
22367
22368 if (TREE_CODE (fn) != TEMPLATE_DECL)
22369 continue;
22370
22371 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22372 expl_subargs, NULL_TREE, tf_none,
22373 /*require_all_args=*/true,
22374 /*use_default_args=*/true);
22375 if (subargs != error_mark_node
22376 && !any_dependent_template_arguments_p (subargs))
22377 {
22378 elem = instantiate_template (fn, subargs, tf_none);
22379 if (elem == error_mark_node)
22380 {
22381 badfn = fn;
22382 badargs = subargs;
22383 }
22384 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22385 && constraints_satisfied_p (elem))
22386 {
22387 goodfn = elem;
22388 ++good;
22389 }
22390 }
22391 }
22392 if (good == 1)
22393 {
22394 mark_used (goodfn);
22395 expr = goodfn;
22396 if (baselink)
22397 expr = build_baselink (BASELINK_BINFO (baselink),
22398 BASELINK_ACCESS_BINFO (baselink),
22399 expr, BASELINK_OPTYPE (baselink));
22400 if (offset)
22401 {
22402 tree base
22403 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22404 expr = build_offset_ref (base, expr, addr, complain);
22405 }
22406 if (addr)
22407 expr = cp_build_addr_expr (expr, complain);
22408 return expr;
22409 }
22410 else if (good == 0 && badargs && (complain & tf_error))
22411 /* There were no good options and at least one bad one, so let the
22412 user know what the problem is. */
22413 instantiate_template (badfn, badargs, complain);
22414 }
22415 return orig_expr;
22416 }
22417
22418 /* As above, but error out if the expression remains overloaded. */
22419
22420 tree
22421 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22422 {
22423 exp = resolve_nondeduced_context (exp, complain);
22424 if (type_unknown_p (exp))
22425 {
22426 if (complain & tf_error)
22427 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22428 return error_mark_node;
22429 }
22430 return exp;
22431 }
22432
22433 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22434 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22435 different overloads deduce different arguments for a given parm.
22436 ADDR_P is true if the expression for which deduction is being
22437 performed was of the form "& fn" rather than simply "fn".
22438
22439 Returns 1 on success. */
22440
22441 static int
22442 try_one_overload (tree tparms,
22443 tree orig_targs,
22444 tree targs,
22445 tree parm,
22446 tree arg,
22447 unification_kind_t strict,
22448 int sub_strict,
22449 bool addr_p,
22450 bool explain_p)
22451 {
22452 int nargs;
22453 tree tempargs;
22454 int i;
22455
22456 if (arg == error_mark_node)
22457 return 0;
22458
22459 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22460 to function or pointer to member function argument if the set of
22461 overloaded functions does not contain function templates and at most
22462 one of a set of overloaded functions provides a unique match.
22463
22464 So if this is a template, just return success. */
22465
22466 if (uses_template_parms (arg))
22467 return 1;
22468
22469 if (TREE_CODE (arg) == METHOD_TYPE)
22470 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22471 else if (addr_p)
22472 arg = build_pointer_type (arg);
22473
22474 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22475
22476 /* We don't copy orig_targs for this because if we have already deduced
22477 some template args from previous args, unify would complain when we
22478 try to deduce a template parameter for the same argument, even though
22479 there isn't really a conflict. */
22480 nargs = TREE_VEC_LENGTH (targs);
22481 tempargs = make_tree_vec (nargs);
22482
22483 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22484 return 0;
22485
22486 /* First make sure we didn't deduce anything that conflicts with
22487 explicitly specified args. */
22488 for (i = nargs; i--; )
22489 {
22490 tree elt = TREE_VEC_ELT (tempargs, i);
22491 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22492
22493 if (!elt)
22494 /*NOP*/;
22495 else if (uses_template_parms (elt))
22496 /* Since we're unifying against ourselves, we will fill in
22497 template args used in the function parm list with our own
22498 template parms. Discard them. */
22499 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22500 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22501 {
22502 /* Check that the argument at each index of the deduced argument pack
22503 is equivalent to the corresponding explicitly specified argument.
22504 We may have deduced more arguments than were explicitly specified,
22505 and that's OK. */
22506
22507 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22508 that's wrong if we deduce the same argument pack from multiple
22509 function arguments: it's only incomplete the first time. */
22510
22511 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22512 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22513
22514 if (TREE_VEC_LENGTH (deduced_pack)
22515 < TREE_VEC_LENGTH (explicit_pack))
22516 return 0;
22517
22518 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22519 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22520 TREE_VEC_ELT (deduced_pack, j)))
22521 return 0;
22522 }
22523 else if (oldelt && !template_args_equal (oldelt, elt))
22524 return 0;
22525 }
22526
22527 for (i = nargs; i--; )
22528 {
22529 tree elt = TREE_VEC_ELT (tempargs, i);
22530
22531 if (elt)
22532 TREE_VEC_ELT (targs, i) = elt;
22533 }
22534
22535 return 1;
22536 }
22537
22538 /* PARM is a template class (perhaps with unbound template
22539 parameters). ARG is a fully instantiated type. If ARG can be
22540 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22541 TARGS are as for unify. */
22542
22543 static tree
22544 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22545 bool explain_p)
22546 {
22547 tree copy_of_targs;
22548
22549 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22550 return NULL_TREE;
22551 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22552 /* Matches anything. */;
22553 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22554 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22555 return NULL_TREE;
22556
22557 /* We need to make a new template argument vector for the call to
22558 unify. If we used TARGS, we'd clutter it up with the result of
22559 the attempted unification, even if this class didn't work out.
22560 We also don't want to commit ourselves to all the unifications
22561 we've already done, since unification is supposed to be done on
22562 an argument-by-argument basis. In other words, consider the
22563 following pathological case:
22564
22565 template <int I, int J, int K>
22566 struct S {};
22567
22568 template <int I, int J>
22569 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22570
22571 template <int I, int J, int K>
22572 void f(S<I, J, K>, S<I, I, I>);
22573
22574 void g() {
22575 S<0, 0, 0> s0;
22576 S<0, 1, 2> s2;
22577
22578 f(s0, s2);
22579 }
22580
22581 Now, by the time we consider the unification involving `s2', we
22582 already know that we must have `f<0, 0, 0>'. But, even though
22583 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22584 because there are two ways to unify base classes of S<0, 1, 2>
22585 with S<I, I, I>. If we kept the already deduced knowledge, we
22586 would reject the possibility I=1. */
22587 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22588
22589 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22590 {
22591 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22592 return NULL_TREE;
22593 return arg;
22594 }
22595
22596 /* If unification failed, we're done. */
22597 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22598 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22599 return NULL_TREE;
22600
22601 return arg;
22602 }
22603
22604 /* Given a template type PARM and a class type ARG, find the unique
22605 base type in ARG that is an instance of PARM. We do not examine
22606 ARG itself; only its base-classes. If there is not exactly one
22607 appropriate base class, return NULL_TREE. PARM may be the type of
22608 a partial specialization, as well as a plain template type. Used
22609 by unify. */
22610
22611 static enum template_base_result
22612 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22613 bool explain_p, tree *result)
22614 {
22615 tree rval = NULL_TREE;
22616 tree binfo;
22617
22618 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22619
22620 binfo = TYPE_BINFO (complete_type (arg));
22621 if (!binfo)
22622 {
22623 /* The type could not be completed. */
22624 *result = NULL_TREE;
22625 return tbr_incomplete_type;
22626 }
22627
22628 /* Walk in inheritance graph order. The search order is not
22629 important, and this avoids multiple walks of virtual bases. */
22630 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22631 {
22632 tree r = try_class_unification (tparms, targs, parm,
22633 BINFO_TYPE (binfo), explain_p);
22634
22635 if (r)
22636 {
22637 /* If there is more than one satisfactory baseclass, then:
22638
22639 [temp.deduct.call]
22640
22641 If they yield more than one possible deduced A, the type
22642 deduction fails.
22643
22644 applies. */
22645 if (rval && !same_type_p (r, rval))
22646 {
22647 *result = NULL_TREE;
22648 return tbr_ambiguous_baseclass;
22649 }
22650
22651 rval = r;
22652 }
22653 }
22654
22655 *result = rval;
22656 return tbr_success;
22657 }
22658
22659 /* Returns the level of DECL, which declares a template parameter. */
22660
22661 static int
22662 template_decl_level (tree decl)
22663 {
22664 switch (TREE_CODE (decl))
22665 {
22666 case TYPE_DECL:
22667 case TEMPLATE_DECL:
22668 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22669
22670 case PARM_DECL:
22671 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22672
22673 default:
22674 gcc_unreachable ();
22675 }
22676 return 0;
22677 }
22678
22679 /* Decide whether ARG can be unified with PARM, considering only the
22680 cv-qualifiers of each type, given STRICT as documented for unify.
22681 Returns nonzero iff the unification is OK on that basis. */
22682
22683 static int
22684 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22685 {
22686 int arg_quals = cp_type_quals (arg);
22687 int parm_quals = cp_type_quals (parm);
22688
22689 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22690 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22691 {
22692 /* Although a CVR qualifier is ignored when being applied to a
22693 substituted template parameter ([8.3.2]/1 for example), that
22694 does not allow us to unify "const T" with "int&" because both
22695 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22696 It is ok when we're allowing additional CV qualifiers
22697 at the outer level [14.8.2.1]/3,1st bullet. */
22698 if ((TYPE_REF_P (arg)
22699 || FUNC_OR_METHOD_TYPE_P (arg))
22700 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22701 return 0;
22702
22703 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22704 && (parm_quals & TYPE_QUAL_RESTRICT))
22705 return 0;
22706 }
22707
22708 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22709 && (arg_quals & parm_quals) != parm_quals)
22710 return 0;
22711
22712 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22713 && (parm_quals & arg_quals) != arg_quals)
22714 return 0;
22715
22716 return 1;
22717 }
22718
22719 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22720 void
22721 template_parm_level_and_index (tree parm, int* level, int* index)
22722 {
22723 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22724 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22725 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22726 {
22727 *index = TEMPLATE_TYPE_IDX (parm);
22728 *level = TEMPLATE_TYPE_LEVEL (parm);
22729 }
22730 else
22731 {
22732 *index = TEMPLATE_PARM_IDX (parm);
22733 *level = TEMPLATE_PARM_LEVEL (parm);
22734 }
22735 }
22736
22737 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22738 do { \
22739 if (unify (TP, TA, P, A, S, EP)) \
22740 return 1; \
22741 } while (0)
22742
22743 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22744 expansion at the end of PACKED_PARMS. Returns 0 if the type
22745 deduction succeeds, 1 otherwise. STRICT is the same as in
22746 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22747 function call argument list. We'll need to adjust the arguments to make them
22748 types. SUBR tells us if this is from a recursive call to
22749 type_unification_real, or for comparing two template argument
22750 lists. */
22751
22752 static int
22753 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22754 tree packed_args, unification_kind_t strict,
22755 bool subr, bool explain_p)
22756 {
22757 tree parm
22758 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22759 tree pattern = PACK_EXPANSION_PATTERN (parm);
22760 tree pack, packs = NULL_TREE;
22761 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22762
22763 /* Add in any args remembered from an earlier partial instantiation. */
22764 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22765 int levels = TMPL_ARGS_DEPTH (targs);
22766
22767 packed_args = expand_template_argument_pack (packed_args);
22768
22769 int len = TREE_VEC_LENGTH (packed_args);
22770
22771 /* Determine the parameter packs we will be deducing from the
22772 pattern, and record their current deductions. */
22773 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22774 pack; pack = TREE_CHAIN (pack))
22775 {
22776 tree parm_pack = TREE_VALUE (pack);
22777 int idx, level;
22778
22779 /* Only template parameter packs can be deduced, not e.g. function
22780 parameter packs or __bases or __integer_pack. */
22781 if (!TEMPLATE_PARM_P (parm_pack))
22782 continue;
22783
22784 /* Determine the index and level of this parameter pack. */
22785 template_parm_level_and_index (parm_pack, &level, &idx);
22786 if (level < levels)
22787 continue;
22788
22789 /* Keep track of the parameter packs and their corresponding
22790 argument packs. */
22791 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22792 TREE_TYPE (packs) = make_tree_vec (len - start);
22793 }
22794
22795 /* Loop through all of the arguments that have not yet been
22796 unified and unify each with the pattern. */
22797 for (i = start; i < len; i++)
22798 {
22799 tree parm;
22800 bool any_explicit = false;
22801 tree arg = TREE_VEC_ELT (packed_args, i);
22802
22803 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22804 or the element of its argument pack at the current index if
22805 this argument was explicitly specified. */
22806 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22807 {
22808 int idx, level;
22809 tree arg, pargs;
22810 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22811
22812 arg = NULL_TREE;
22813 if (TREE_VALUE (pack)
22814 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22815 && (i - start < TREE_VEC_LENGTH (pargs)))
22816 {
22817 any_explicit = true;
22818 arg = TREE_VEC_ELT (pargs, i - start);
22819 }
22820 TMPL_ARG (targs, level, idx) = arg;
22821 }
22822
22823 /* If we had explicit template arguments, substitute them into the
22824 pattern before deduction. */
22825 if (any_explicit)
22826 {
22827 /* Some arguments might still be unspecified or dependent. */
22828 bool dependent;
22829 ++processing_template_decl;
22830 dependent = any_dependent_template_arguments_p (targs);
22831 if (!dependent)
22832 --processing_template_decl;
22833 parm = tsubst (pattern, targs,
22834 explain_p ? tf_warning_or_error : tf_none,
22835 NULL_TREE);
22836 if (dependent)
22837 --processing_template_decl;
22838 if (parm == error_mark_node)
22839 return 1;
22840 }
22841 else
22842 parm = pattern;
22843
22844 /* Unify the pattern with the current argument. */
22845 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22846 explain_p))
22847 return 1;
22848
22849 /* For each parameter pack, collect the deduced value. */
22850 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22851 {
22852 int idx, level;
22853 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22854
22855 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22856 TMPL_ARG (targs, level, idx);
22857 }
22858 }
22859
22860 /* Verify that the results of unification with the parameter packs
22861 produce results consistent with what we've seen before, and make
22862 the deduced argument packs available. */
22863 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22864 {
22865 tree old_pack = TREE_VALUE (pack);
22866 tree new_args = TREE_TYPE (pack);
22867 int i, len = TREE_VEC_LENGTH (new_args);
22868 int idx, level;
22869 bool nondeduced_p = false;
22870
22871 /* By default keep the original deduced argument pack.
22872 If necessary, more specific code is going to update the
22873 resulting deduced argument later down in this function. */
22874 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22875 TMPL_ARG (targs, level, idx) = old_pack;
22876
22877 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22878 actually deduce anything. */
22879 for (i = 0; i < len && !nondeduced_p; ++i)
22880 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22881 nondeduced_p = true;
22882 if (nondeduced_p)
22883 continue;
22884
22885 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22886 {
22887 /* If we had fewer function args than explicit template args,
22888 just use the explicits. */
22889 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22890 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22891 if (len < explicit_len)
22892 new_args = explicit_args;
22893 }
22894
22895 if (!old_pack)
22896 {
22897 tree result;
22898 /* Build the deduced *_ARGUMENT_PACK. */
22899 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22900 {
22901 result = make_node (NONTYPE_ARGUMENT_PACK);
22902 TREE_CONSTANT (result) = 1;
22903 }
22904 else
22905 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22906
22907 SET_ARGUMENT_PACK_ARGS (result, new_args);
22908
22909 /* Note the deduced argument packs for this parameter
22910 pack. */
22911 TMPL_ARG (targs, level, idx) = result;
22912 }
22913 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22914 && (ARGUMENT_PACK_ARGS (old_pack)
22915 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22916 {
22917 /* We only had the explicitly-provided arguments before, but
22918 now we have a complete set of arguments. */
22919 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22920
22921 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22922 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22923 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22924 }
22925 else
22926 {
22927 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22928 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22929 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22930 /* During template argument deduction for the aggregate deduction
22931 candidate, the number of elements in a trailing parameter pack
22932 is only deduced from the number of remaining function
22933 arguments if it is not otherwise deduced. */
22934 if (cxx_dialect >= cxx20
22935 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
22936 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
22937 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
22938 if (!comp_template_args (old_args, new_args,
22939 &bad_old_arg, &bad_new_arg))
22940 /* Inconsistent unification of this parameter pack. */
22941 return unify_parameter_pack_inconsistent (explain_p,
22942 bad_old_arg,
22943 bad_new_arg);
22944 }
22945 }
22946
22947 return unify_success (explain_p);
22948 }
22949
22950 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22951 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22952 parameters and return value are as for unify. */
22953
22954 static int
22955 unify_array_domain (tree tparms, tree targs,
22956 tree parm_dom, tree arg_dom,
22957 bool explain_p)
22958 {
22959 tree parm_max;
22960 tree arg_max;
22961 bool parm_cst;
22962 bool arg_cst;
22963
22964 /* Our representation of array types uses "N - 1" as the
22965 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22966 not an integer constant. We cannot unify arbitrarily
22967 complex expressions, so we eliminate the MINUS_EXPRs
22968 here. */
22969 parm_max = TYPE_MAX_VALUE (parm_dom);
22970 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22971 if (!parm_cst)
22972 {
22973 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22974 parm_max = TREE_OPERAND (parm_max, 0);
22975 }
22976 arg_max = TYPE_MAX_VALUE (arg_dom);
22977 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22978 if (!arg_cst)
22979 {
22980 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22981 trying to unify the type of a variable with the type
22982 of a template parameter. For example:
22983
22984 template <unsigned int N>
22985 void f (char (&) [N]);
22986 int g();
22987 void h(int i) {
22988 char a[g(i)];
22989 f(a);
22990 }
22991
22992 Here, the type of the ARG will be "int [g(i)]", and
22993 may be a SAVE_EXPR, etc. */
22994 if (TREE_CODE (arg_max) != MINUS_EXPR)
22995 return unify_vla_arg (explain_p, arg_dom);
22996 arg_max = TREE_OPERAND (arg_max, 0);
22997 }
22998
22999 /* If only one of the bounds used a MINUS_EXPR, compensate
23000 by adding one to the other bound. */
23001 if (parm_cst && !arg_cst)
23002 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23003 integer_type_node,
23004 parm_max,
23005 integer_one_node);
23006 else if (arg_cst && !parm_cst)
23007 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23008 integer_type_node,
23009 arg_max,
23010 integer_one_node);
23011
23012 return unify (tparms, targs, parm_max, arg_max,
23013 UNIFY_ALLOW_INTEGER, explain_p);
23014 }
23015
23016 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23017
23018 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23019
23020 static pa_kind_t
23021 pa_kind (tree t)
23022 {
23023 if (PACK_EXPANSION_P (t))
23024 t = PACK_EXPANSION_PATTERN (t);
23025 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23026 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23027 || DECL_TYPE_TEMPLATE_P (t))
23028 return pa_tmpl;
23029 else if (TYPE_P (t))
23030 return pa_type;
23031 else
23032 return pa_expr;
23033 }
23034
23035 /* Deduce the value of template parameters. TPARMS is the (innermost)
23036 set of template parameters to a template. TARGS is the bindings
23037 for those template parameters, as determined thus far; TARGS may
23038 include template arguments for outer levels of template parameters
23039 as well. PARM is a parameter to a template function, or a
23040 subcomponent of that parameter; ARG is the corresponding argument.
23041 This function attempts to match PARM with ARG in a manner
23042 consistent with the existing assignments in TARGS. If more values
23043 are deduced, then TARGS is updated.
23044
23045 Returns 0 if the type deduction succeeds, 1 otherwise. The
23046 parameter STRICT is a bitwise or of the following flags:
23047
23048 UNIFY_ALLOW_NONE:
23049 Require an exact match between PARM and ARG.
23050 UNIFY_ALLOW_MORE_CV_QUAL:
23051 Allow the deduced ARG to be more cv-qualified (by qualification
23052 conversion) than ARG.
23053 UNIFY_ALLOW_LESS_CV_QUAL:
23054 Allow the deduced ARG to be less cv-qualified than ARG.
23055 UNIFY_ALLOW_DERIVED:
23056 Allow the deduced ARG to be a template base class of ARG,
23057 or a pointer to a template base class of the type pointed to by
23058 ARG.
23059 UNIFY_ALLOW_INTEGER:
23060 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23061 case for more information.
23062 UNIFY_ALLOW_OUTER_LEVEL:
23063 This is the outermost level of a deduction. Used to determine validity
23064 of qualification conversions. A valid qualification conversion must
23065 have const qualified pointers leading up to the inner type which
23066 requires additional CV quals, except at the outer level, where const
23067 is not required [conv.qual]. It would be normal to set this flag in
23068 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23069 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23070 This is the outermost level of a deduction, and PARM can be more CV
23071 qualified at this point.
23072 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23073 This is the outermost level of a deduction, and PARM can be less CV
23074 qualified at this point. */
23075
23076 static int
23077 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23078 bool explain_p)
23079 {
23080 int idx;
23081 tree targ;
23082 tree tparm;
23083 int strict_in = strict;
23084 tsubst_flags_t complain = (explain_p
23085 ? tf_warning_or_error
23086 : tf_none);
23087
23088 /* I don't think this will do the right thing with respect to types.
23089 But the only case I've seen it in so far has been array bounds, where
23090 signedness is the only information lost, and I think that will be
23091 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23092 finish_id_expression_1, and are also OK. */
23093 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23094 parm = TREE_OPERAND (parm, 0);
23095
23096 if (arg == error_mark_node)
23097 return unify_invalid (explain_p);
23098 if (arg == unknown_type_node
23099 || arg == init_list_type_node)
23100 /* We can't deduce anything from this, but we might get all the
23101 template args from other function args. */
23102 return unify_success (explain_p);
23103
23104 if (parm == any_targ_node || arg == any_targ_node)
23105 return unify_success (explain_p);
23106
23107 /* If PARM uses template parameters, then we can't bail out here,
23108 even if ARG == PARM, since we won't record unifications for the
23109 template parameters. We might need them if we're trying to
23110 figure out which of two things is more specialized. */
23111 if (arg == parm && !uses_template_parms (parm))
23112 return unify_success (explain_p);
23113
23114 /* Handle init lists early, so the rest of the function can assume
23115 we're dealing with a type. */
23116 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23117 {
23118 tree elt, elttype;
23119 unsigned i;
23120 tree orig_parm = parm;
23121
23122 if (!is_std_init_list (parm)
23123 && TREE_CODE (parm) != ARRAY_TYPE)
23124 /* We can only deduce from an initializer list argument if the
23125 parameter is std::initializer_list or an array; otherwise this
23126 is a non-deduced context. */
23127 return unify_success (explain_p);
23128
23129 if (TREE_CODE (parm) == ARRAY_TYPE)
23130 elttype = TREE_TYPE (parm);
23131 else
23132 {
23133 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23134 /* Deduction is defined in terms of a single type, so just punt
23135 on the (bizarre) std::initializer_list<T...>. */
23136 if (PACK_EXPANSION_P (elttype))
23137 return unify_success (explain_p);
23138 }
23139
23140 if (strict != DEDUCE_EXACT
23141 && TYPE_P (elttype)
23142 && !uses_deducible_template_parms (elttype))
23143 /* If ELTTYPE has no deducible template parms, skip deduction from
23144 the list elements. */;
23145 else
23146 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23147 {
23148 int elt_strict = strict;
23149
23150 if (elt == error_mark_node)
23151 return unify_invalid (explain_p);
23152
23153 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23154 {
23155 tree type = TREE_TYPE (elt);
23156 if (type == error_mark_node)
23157 return unify_invalid (explain_p);
23158 /* It should only be possible to get here for a call. */
23159 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23160 elt_strict |= maybe_adjust_types_for_deduction
23161 (DEDUCE_CALL, &elttype, &type, elt);
23162 elt = type;
23163 }
23164
23165 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23166 explain_p);
23167 }
23168
23169 if (TREE_CODE (parm) == ARRAY_TYPE
23170 && deducible_array_bound (TYPE_DOMAIN (parm)))
23171 {
23172 /* Also deduce from the length of the initializer list. */
23173 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23174 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23175 if (idx == error_mark_node)
23176 return unify_invalid (explain_p);
23177 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23178 idx, explain_p);
23179 }
23180
23181 /* If the std::initializer_list<T> deduction worked, replace the
23182 deduced A with std::initializer_list<A>. */
23183 if (orig_parm != parm)
23184 {
23185 idx = TEMPLATE_TYPE_IDX (orig_parm);
23186 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23187 targ = listify (targ);
23188 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23189 }
23190 return unify_success (explain_p);
23191 }
23192
23193 /* If parm and arg aren't the same kind of thing (template, type, or
23194 expression), fail early. */
23195 if (pa_kind (parm) != pa_kind (arg))
23196 return unify_invalid (explain_p);
23197
23198 /* Immediately reject some pairs that won't unify because of
23199 cv-qualification mismatches. */
23200 if (TREE_CODE (arg) == TREE_CODE (parm)
23201 && TYPE_P (arg)
23202 /* It is the elements of the array which hold the cv quals of an array
23203 type, and the elements might be template type parms. We'll check
23204 when we recurse. */
23205 && TREE_CODE (arg) != ARRAY_TYPE
23206 /* We check the cv-qualifiers when unifying with template type
23207 parameters below. We want to allow ARG `const T' to unify with
23208 PARM `T' for example, when computing which of two templates
23209 is more specialized, for example. */
23210 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23211 && !check_cv_quals_for_unify (strict_in, arg, parm))
23212 return unify_cv_qual_mismatch (explain_p, parm, arg);
23213
23214 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23215 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23216 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23217 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23218 strict &= ~UNIFY_ALLOW_DERIVED;
23219 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23220 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23221
23222 switch (TREE_CODE (parm))
23223 {
23224 case TYPENAME_TYPE:
23225 case SCOPE_REF:
23226 case UNBOUND_CLASS_TEMPLATE:
23227 /* In a type which contains a nested-name-specifier, template
23228 argument values cannot be deduced for template parameters used
23229 within the nested-name-specifier. */
23230 return unify_success (explain_p);
23231
23232 case TEMPLATE_TYPE_PARM:
23233 case TEMPLATE_TEMPLATE_PARM:
23234 case BOUND_TEMPLATE_TEMPLATE_PARM:
23235 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23236 if (error_operand_p (tparm))
23237 return unify_invalid (explain_p);
23238
23239 if (TEMPLATE_TYPE_LEVEL (parm)
23240 != template_decl_level (tparm))
23241 /* The PARM is not one we're trying to unify. Just check
23242 to see if it matches ARG. */
23243 {
23244 if (TREE_CODE (arg) == TREE_CODE (parm)
23245 && (is_auto (parm) ? is_auto (arg)
23246 : same_type_p (parm, arg)))
23247 return unify_success (explain_p);
23248 else
23249 return unify_type_mismatch (explain_p, parm, arg);
23250 }
23251 idx = TEMPLATE_TYPE_IDX (parm);
23252 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23253 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23254 if (error_operand_p (tparm))
23255 return unify_invalid (explain_p);
23256
23257 /* Check for mixed types and values. */
23258 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23259 && TREE_CODE (tparm) != TYPE_DECL)
23260 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23261 && TREE_CODE (tparm) != TEMPLATE_DECL))
23262 gcc_unreachable ();
23263
23264 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23265 {
23266 if ((strict_in & UNIFY_ALLOW_DERIVED)
23267 && CLASS_TYPE_P (arg))
23268 {
23269 /* First try to match ARG directly. */
23270 tree t = try_class_unification (tparms, targs, parm, arg,
23271 explain_p);
23272 if (!t)
23273 {
23274 /* Otherwise, look for a suitable base of ARG, as below. */
23275 enum template_base_result r;
23276 r = get_template_base (tparms, targs, parm, arg,
23277 explain_p, &t);
23278 if (!t)
23279 return unify_no_common_base (explain_p, r, parm, arg);
23280 arg = t;
23281 }
23282 }
23283 /* ARG must be constructed from a template class or a template
23284 template parameter. */
23285 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23286 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23287 return unify_template_deduction_failure (explain_p, parm, arg);
23288
23289 /* Deduce arguments T, i from TT<T> or TT<i>. */
23290 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23291 return 1;
23292
23293 arg = TYPE_TI_TEMPLATE (arg);
23294
23295 /* Fall through to deduce template name. */
23296 }
23297
23298 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23299 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23300 {
23301 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23302
23303 /* Simple cases: Value already set, does match or doesn't. */
23304 if (targ != NULL_TREE && template_args_equal (targ, arg))
23305 return unify_success (explain_p);
23306 else if (targ)
23307 return unify_inconsistency (explain_p, parm, targ, arg);
23308 }
23309 else
23310 {
23311 /* If PARM is `const T' and ARG is only `int', we don't have
23312 a match unless we are allowing additional qualification.
23313 If ARG is `const int' and PARM is just `T' that's OK;
23314 that binds `const int' to `T'. */
23315 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23316 arg, parm))
23317 return unify_cv_qual_mismatch (explain_p, parm, arg);
23318
23319 /* Consider the case where ARG is `const volatile int' and
23320 PARM is `const T'. Then, T should be `volatile int'. */
23321 arg = cp_build_qualified_type_real
23322 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23323 if (arg == error_mark_node)
23324 return unify_invalid (explain_p);
23325
23326 /* Simple cases: Value already set, does match or doesn't. */
23327 if (targ != NULL_TREE && same_type_p (targ, arg))
23328 return unify_success (explain_p);
23329 else if (targ)
23330 return unify_inconsistency (explain_p, parm, targ, arg);
23331
23332 /* Make sure that ARG is not a variable-sized array. (Note
23333 that were talking about variable-sized arrays (like
23334 `int[n]'), rather than arrays of unknown size (like
23335 `int[]').) We'll get very confused by such a type since
23336 the bound of the array is not constant, and therefore
23337 not mangleable. Besides, such types are not allowed in
23338 ISO C++, so we can do as we please here. We do allow
23339 them for 'auto' deduction, since that isn't ABI-exposed. */
23340 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23341 return unify_vla_arg (explain_p, arg);
23342
23343 /* Strip typedefs as in convert_template_argument. */
23344 arg = canonicalize_type_argument (arg, tf_none);
23345 }
23346
23347 /* If ARG is a parameter pack or an expansion, we cannot unify
23348 against it unless PARM is also a parameter pack. */
23349 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23350 && !template_parameter_pack_p (parm))
23351 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23352
23353 /* If the argument deduction results is a METHOD_TYPE,
23354 then there is a problem.
23355 METHOD_TYPE doesn't map to any real C++ type the result of
23356 the deduction cannot be of that type. */
23357 if (TREE_CODE (arg) == METHOD_TYPE)
23358 return unify_method_type_error (explain_p, arg);
23359
23360 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23361 return unify_success (explain_p);
23362
23363 case TEMPLATE_PARM_INDEX:
23364 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23365 if (error_operand_p (tparm))
23366 return unify_invalid (explain_p);
23367
23368 if (TEMPLATE_PARM_LEVEL (parm)
23369 != template_decl_level (tparm))
23370 {
23371 /* The PARM is not one we're trying to unify. Just check
23372 to see if it matches ARG. */
23373 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23374 && cp_tree_equal (parm, arg));
23375 if (result)
23376 unify_expression_unequal (explain_p, parm, arg);
23377 return result;
23378 }
23379
23380 idx = TEMPLATE_PARM_IDX (parm);
23381 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23382
23383 if (targ)
23384 {
23385 if ((strict & UNIFY_ALLOW_INTEGER)
23386 && TREE_TYPE (targ) && TREE_TYPE (arg)
23387 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23388 /* We're deducing from an array bound, the type doesn't matter. */
23389 arg = fold_convert (TREE_TYPE (targ), arg);
23390 int x = !cp_tree_equal (targ, arg);
23391 if (x)
23392 unify_inconsistency (explain_p, parm, targ, arg);
23393 return x;
23394 }
23395
23396 /* [temp.deduct.type] If, in the declaration of a function template
23397 with a non-type template-parameter, the non-type
23398 template-parameter is used in an expression in the function
23399 parameter-list and, if the corresponding template-argument is
23400 deduced, the template-argument type shall match the type of the
23401 template-parameter exactly, except that a template-argument
23402 deduced from an array bound may be of any integral type.
23403 The non-type parameter might use already deduced type parameters. */
23404 tparm = TREE_TYPE (parm);
23405 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23406 /* We don't have enough levels of args to do any substitution. This
23407 can happen in the context of -fnew-ttp-matching. */;
23408 else
23409 {
23410 ++processing_template_decl;
23411 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23412 --processing_template_decl;
23413
23414 if (tree a = type_uses_auto (tparm))
23415 {
23416 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23417 if (tparm == error_mark_node)
23418 return 1;
23419 }
23420 }
23421
23422 if (!TREE_TYPE (arg))
23423 /* Template-parameter dependent expression. Just accept it for now.
23424 It will later be processed in convert_template_argument. */
23425 ;
23426 else if (same_type_ignoring_top_level_qualifiers_p
23427 (non_reference (TREE_TYPE (arg)),
23428 non_reference (tparm)))
23429 /* OK. Ignore top-level quals here because a class-type template
23430 parameter object is const. */;
23431 else if ((strict & UNIFY_ALLOW_INTEGER)
23432 && CP_INTEGRAL_TYPE_P (tparm))
23433 /* Convert the ARG to the type of PARM; the deduced non-type
23434 template argument must exactly match the types of the
23435 corresponding parameter. */
23436 arg = fold (build_nop (tparm, arg));
23437 else if (uses_template_parms (tparm))
23438 {
23439 /* We haven't deduced the type of this parameter yet. */
23440 if (cxx_dialect >= cxx17
23441 /* We deduce from array bounds in try_array_deduction. */
23442 && !(strict & UNIFY_ALLOW_INTEGER))
23443 {
23444 /* Deduce it from the non-type argument. */
23445 tree atype = TREE_TYPE (arg);
23446 RECUR_AND_CHECK_FAILURE (tparms, targs,
23447 tparm, atype,
23448 UNIFY_ALLOW_NONE, explain_p);
23449 }
23450 else
23451 /* Try again later. */
23452 return unify_success (explain_p);
23453 }
23454 else
23455 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23456
23457 /* If ARG is a parameter pack or an expansion, we cannot unify
23458 against it unless PARM is also a parameter pack. */
23459 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23460 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23461 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23462
23463 {
23464 bool removed_attr = false;
23465 arg = strip_typedefs_expr (arg, &removed_attr);
23466 }
23467 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23468 return unify_success (explain_p);
23469
23470 case PTRMEM_CST:
23471 {
23472 /* A pointer-to-member constant can be unified only with
23473 another constant. */
23474 if (TREE_CODE (arg) != PTRMEM_CST)
23475 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23476
23477 /* Just unify the class member. It would be useless (and possibly
23478 wrong, depending on the strict flags) to unify also
23479 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23480 arg refer to the same variable, even if through different
23481 classes. For instance:
23482
23483 struct A { int x; };
23484 struct B : A { };
23485
23486 Unification of &A::x and &B::x must succeed. */
23487 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23488 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23489 }
23490
23491 case POINTER_TYPE:
23492 {
23493 if (!TYPE_PTR_P (arg))
23494 return unify_type_mismatch (explain_p, parm, arg);
23495
23496 /* [temp.deduct.call]
23497
23498 A can be another pointer or pointer to member type that can
23499 be converted to the deduced A via a qualification
23500 conversion (_conv.qual_).
23501
23502 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23503 This will allow for additional cv-qualification of the
23504 pointed-to types if appropriate. */
23505
23506 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23507 /* The derived-to-base conversion only persists through one
23508 level of pointers. */
23509 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23510
23511 return unify (tparms, targs, TREE_TYPE (parm),
23512 TREE_TYPE (arg), strict, explain_p);
23513 }
23514
23515 case REFERENCE_TYPE:
23516 if (!TYPE_REF_P (arg))
23517 return unify_type_mismatch (explain_p, parm, arg);
23518 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23519 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23520
23521 case ARRAY_TYPE:
23522 if (TREE_CODE (arg) != ARRAY_TYPE)
23523 return unify_type_mismatch (explain_p, parm, arg);
23524 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23525 != (TYPE_DOMAIN (arg) == NULL_TREE))
23526 return unify_type_mismatch (explain_p, parm, arg);
23527 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23528 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23529 if (TYPE_DOMAIN (parm) != NULL_TREE)
23530 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23531 TYPE_DOMAIN (arg), explain_p);
23532 return unify_success (explain_p);
23533
23534 case REAL_TYPE:
23535 case COMPLEX_TYPE:
23536 case VECTOR_TYPE:
23537 case INTEGER_TYPE:
23538 case BOOLEAN_TYPE:
23539 case ENUMERAL_TYPE:
23540 case VOID_TYPE:
23541 case NULLPTR_TYPE:
23542 if (TREE_CODE (arg) != TREE_CODE (parm))
23543 return unify_type_mismatch (explain_p, parm, arg);
23544
23545 /* We have already checked cv-qualification at the top of the
23546 function. */
23547 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23548 return unify_type_mismatch (explain_p, parm, arg);
23549
23550 /* As far as unification is concerned, this wins. Later checks
23551 will invalidate it if necessary. */
23552 return unify_success (explain_p);
23553
23554 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23555 /* Type INTEGER_CST can come from ordinary constant template args. */
23556 case INTEGER_CST:
23557 while (CONVERT_EXPR_P (arg))
23558 arg = TREE_OPERAND (arg, 0);
23559
23560 if (TREE_CODE (arg) != INTEGER_CST)
23561 return unify_template_argument_mismatch (explain_p, parm, arg);
23562 return (tree_int_cst_equal (parm, arg)
23563 ? unify_success (explain_p)
23564 : unify_template_argument_mismatch (explain_p, parm, arg));
23565
23566 case TREE_VEC:
23567 {
23568 int i, len, argslen;
23569 int parm_variadic_p = 0;
23570
23571 if (TREE_CODE (arg) != TREE_VEC)
23572 return unify_template_argument_mismatch (explain_p, parm, arg);
23573
23574 len = TREE_VEC_LENGTH (parm);
23575 argslen = TREE_VEC_LENGTH (arg);
23576
23577 /* Check for pack expansions in the parameters. */
23578 for (i = 0; i < len; ++i)
23579 {
23580 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23581 {
23582 if (i == len - 1)
23583 /* We can unify against something with a trailing
23584 parameter pack. */
23585 parm_variadic_p = 1;
23586 else
23587 /* [temp.deduct.type]/9: If the template argument list of
23588 P contains a pack expansion that is not the last
23589 template argument, the entire template argument list
23590 is a non-deduced context. */
23591 return unify_success (explain_p);
23592 }
23593 }
23594
23595 /* If we don't have enough arguments to satisfy the parameters
23596 (not counting the pack expression at the end), or we have
23597 too many arguments for a parameter list that doesn't end in
23598 a pack expression, we can't unify. */
23599 if (parm_variadic_p
23600 ? argslen < len - parm_variadic_p
23601 : argslen != len)
23602 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23603
23604 /* Unify all of the parameters that precede the (optional)
23605 pack expression. */
23606 for (i = 0; i < len - parm_variadic_p; ++i)
23607 {
23608 RECUR_AND_CHECK_FAILURE (tparms, targs,
23609 TREE_VEC_ELT (parm, i),
23610 TREE_VEC_ELT (arg, i),
23611 UNIFY_ALLOW_NONE, explain_p);
23612 }
23613 if (parm_variadic_p)
23614 return unify_pack_expansion (tparms, targs, parm, arg,
23615 DEDUCE_EXACT,
23616 /*subr=*/true, explain_p);
23617 return unify_success (explain_p);
23618 }
23619
23620 case RECORD_TYPE:
23621 case UNION_TYPE:
23622 if (TREE_CODE (arg) != TREE_CODE (parm))
23623 return unify_type_mismatch (explain_p, parm, arg);
23624
23625 if (TYPE_PTRMEMFUNC_P (parm))
23626 {
23627 if (!TYPE_PTRMEMFUNC_P (arg))
23628 return unify_type_mismatch (explain_p, parm, arg);
23629
23630 return unify (tparms, targs,
23631 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23632 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23633 strict, explain_p);
23634 }
23635 else if (TYPE_PTRMEMFUNC_P (arg))
23636 return unify_type_mismatch (explain_p, parm, arg);
23637
23638 if (CLASSTYPE_TEMPLATE_INFO (parm))
23639 {
23640 tree t = NULL_TREE;
23641
23642 if (strict_in & UNIFY_ALLOW_DERIVED)
23643 {
23644 /* First, we try to unify the PARM and ARG directly. */
23645 t = try_class_unification (tparms, targs,
23646 parm, arg, explain_p);
23647
23648 if (!t)
23649 {
23650 /* Fallback to the special case allowed in
23651 [temp.deduct.call]:
23652
23653 If P is a class, and P has the form
23654 template-id, then A can be a derived class of
23655 the deduced A. Likewise, if P is a pointer to
23656 a class of the form template-id, A can be a
23657 pointer to a derived class pointed to by the
23658 deduced A. */
23659 enum template_base_result r;
23660 r = get_template_base (tparms, targs, parm, arg,
23661 explain_p, &t);
23662
23663 if (!t)
23664 {
23665 /* Don't give the derived diagnostic if we're
23666 already dealing with the same template. */
23667 bool same_template
23668 = (CLASSTYPE_TEMPLATE_INFO (arg)
23669 && (CLASSTYPE_TI_TEMPLATE (parm)
23670 == CLASSTYPE_TI_TEMPLATE (arg)));
23671 return unify_no_common_base (explain_p && !same_template,
23672 r, parm, arg);
23673 }
23674 }
23675 }
23676 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23677 && (CLASSTYPE_TI_TEMPLATE (parm)
23678 == CLASSTYPE_TI_TEMPLATE (arg)))
23679 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23680 Then, we should unify `int' and `U'. */
23681 t = arg;
23682 else
23683 /* There's no chance of unification succeeding. */
23684 return unify_type_mismatch (explain_p, parm, arg);
23685
23686 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23687 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23688 }
23689 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23690 return unify_type_mismatch (explain_p, parm, arg);
23691 return unify_success (explain_p);
23692
23693 case METHOD_TYPE:
23694 case FUNCTION_TYPE:
23695 {
23696 unsigned int nargs;
23697 tree *args;
23698 tree a;
23699 unsigned int i;
23700
23701 if (TREE_CODE (arg) != TREE_CODE (parm))
23702 return unify_type_mismatch (explain_p, parm, arg);
23703
23704 /* CV qualifications for methods can never be deduced, they must
23705 match exactly. We need to check them explicitly here,
23706 because type_unification_real treats them as any other
23707 cv-qualified parameter. */
23708 if (TREE_CODE (parm) == METHOD_TYPE
23709 && (!check_cv_quals_for_unify
23710 (UNIFY_ALLOW_NONE,
23711 class_of_this_parm (arg),
23712 class_of_this_parm (parm))))
23713 return unify_cv_qual_mismatch (explain_p, parm, arg);
23714 if (TREE_CODE (arg) == FUNCTION_TYPE
23715 && type_memfn_quals (parm) != type_memfn_quals (arg))
23716 return unify_cv_qual_mismatch (explain_p, parm, arg);
23717 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23718 return unify_type_mismatch (explain_p, parm, arg);
23719
23720 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23721 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23722
23723 nargs = list_length (TYPE_ARG_TYPES (arg));
23724 args = XALLOCAVEC (tree, nargs);
23725 for (a = TYPE_ARG_TYPES (arg), i = 0;
23726 a != NULL_TREE && a != void_list_node;
23727 a = TREE_CHAIN (a), ++i)
23728 args[i] = TREE_VALUE (a);
23729 nargs = i;
23730
23731 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23732 args, nargs, 1, DEDUCE_EXACT,
23733 NULL, explain_p))
23734 return 1;
23735
23736 if (flag_noexcept_type)
23737 {
23738 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23739 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23740 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23741 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23742 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23743 && uses_template_parms (TREE_PURPOSE (pspec)))
23744 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23745 TREE_PURPOSE (aspec),
23746 UNIFY_ALLOW_NONE, explain_p);
23747 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23748 return unify_type_mismatch (explain_p, parm, arg);
23749 }
23750
23751 return 0;
23752 }
23753
23754 case OFFSET_TYPE:
23755 /* Unify a pointer to member with a pointer to member function, which
23756 deduces the type of the member as a function type. */
23757 if (TYPE_PTRMEMFUNC_P (arg))
23758 {
23759 /* Check top-level cv qualifiers */
23760 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23761 return unify_cv_qual_mismatch (explain_p, parm, arg);
23762
23763 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23764 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23765 UNIFY_ALLOW_NONE, explain_p);
23766
23767 /* Determine the type of the function we are unifying against. */
23768 tree fntype = static_fn_type (arg);
23769
23770 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23771 }
23772
23773 if (TREE_CODE (arg) != OFFSET_TYPE)
23774 return unify_type_mismatch (explain_p, parm, arg);
23775 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23776 TYPE_OFFSET_BASETYPE (arg),
23777 UNIFY_ALLOW_NONE, explain_p);
23778 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23779 strict, explain_p);
23780
23781 case CONST_DECL:
23782 if (DECL_TEMPLATE_PARM_P (parm))
23783 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23784 if (arg != scalar_constant_value (parm))
23785 return unify_template_argument_mismatch (explain_p, parm, arg);
23786 return unify_success (explain_p);
23787
23788 case FIELD_DECL:
23789 case TEMPLATE_DECL:
23790 /* Matched cases are handled by the ARG == PARM test above. */
23791 return unify_template_argument_mismatch (explain_p, parm, arg);
23792
23793 case VAR_DECL:
23794 /* We might get a variable as a non-type template argument in parm if the
23795 corresponding parameter is type-dependent. Make any necessary
23796 adjustments based on whether arg is a reference. */
23797 if (CONSTANT_CLASS_P (arg))
23798 parm = fold_non_dependent_expr (parm, complain);
23799 else if (REFERENCE_REF_P (arg))
23800 {
23801 tree sub = TREE_OPERAND (arg, 0);
23802 STRIP_NOPS (sub);
23803 if (TREE_CODE (sub) == ADDR_EXPR)
23804 arg = TREE_OPERAND (sub, 0);
23805 }
23806 /* Now use the normal expression code to check whether they match. */
23807 goto expr;
23808
23809 case TYPE_ARGUMENT_PACK:
23810 case NONTYPE_ARGUMENT_PACK:
23811 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23812 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23813
23814 case TYPEOF_TYPE:
23815 case DECLTYPE_TYPE:
23816 case UNDERLYING_TYPE:
23817 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23818 or UNDERLYING_TYPE nodes. */
23819 return unify_success (explain_p);
23820
23821 case ERROR_MARK:
23822 /* Unification fails if we hit an error node. */
23823 return unify_invalid (explain_p);
23824
23825 case INDIRECT_REF:
23826 if (REFERENCE_REF_P (parm))
23827 {
23828 bool pexp = PACK_EXPANSION_P (arg);
23829 if (pexp)
23830 arg = PACK_EXPANSION_PATTERN (arg);
23831 if (REFERENCE_REF_P (arg))
23832 arg = TREE_OPERAND (arg, 0);
23833 if (pexp)
23834 arg = make_pack_expansion (arg, complain);
23835 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23836 strict, explain_p);
23837 }
23838 /* FALLTHRU */
23839
23840 default:
23841 /* An unresolved overload is a nondeduced context. */
23842 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23843 return unify_success (explain_p);
23844 gcc_assert (EXPR_P (parm)
23845 || COMPOUND_LITERAL_P (parm)
23846 || TREE_CODE (parm) == TRAIT_EXPR);
23847 expr:
23848 /* We must be looking at an expression. This can happen with
23849 something like:
23850
23851 template <int I>
23852 void foo(S<I>, S<I + 2>);
23853
23854 or
23855
23856 template<typename T>
23857 void foo(A<T, T{}>);
23858
23859 This is a "non-deduced context":
23860
23861 [deduct.type]
23862
23863 The non-deduced contexts are:
23864
23865 --A non-type template argument or an array bound in which
23866 a subexpression references a template parameter.
23867
23868 In these cases, we assume deduction succeeded, but don't
23869 actually infer any unifications. */
23870
23871 if (!uses_template_parms (parm)
23872 && !template_args_equal (parm, arg))
23873 return unify_expression_unequal (explain_p, parm, arg);
23874 else
23875 return unify_success (explain_p);
23876 }
23877 }
23878 #undef RECUR_AND_CHECK_FAILURE
23879 \f
23880 /* Note that DECL can be defined in this translation unit, if
23881 required. */
23882
23883 static void
23884 mark_definable (tree decl)
23885 {
23886 tree clone;
23887 DECL_NOT_REALLY_EXTERN (decl) = 1;
23888 FOR_EACH_CLONE (clone, decl)
23889 DECL_NOT_REALLY_EXTERN (clone) = 1;
23890 }
23891
23892 /* Called if RESULT is explicitly instantiated, or is a member of an
23893 explicitly instantiated class. */
23894
23895 void
23896 mark_decl_instantiated (tree result, int extern_p)
23897 {
23898 SET_DECL_EXPLICIT_INSTANTIATION (result);
23899
23900 /* If this entity has already been written out, it's too late to
23901 make any modifications. */
23902 if (TREE_ASM_WRITTEN (result))
23903 return;
23904
23905 /* For anonymous namespace we don't need to do anything. */
23906 if (decl_anon_ns_mem_p (result))
23907 {
23908 gcc_assert (!TREE_PUBLIC (result));
23909 return;
23910 }
23911
23912 if (TREE_CODE (result) != FUNCTION_DECL)
23913 /* The TREE_PUBLIC flag for function declarations will have been
23914 set correctly by tsubst. */
23915 TREE_PUBLIC (result) = 1;
23916
23917 /* This might have been set by an earlier implicit instantiation. */
23918 DECL_COMDAT (result) = 0;
23919
23920 if (extern_p)
23921 DECL_NOT_REALLY_EXTERN (result) = 0;
23922 else
23923 {
23924 mark_definable (result);
23925 mark_needed (result);
23926 /* Always make artificials weak. */
23927 if (DECL_ARTIFICIAL (result) && flag_weak)
23928 comdat_linkage (result);
23929 /* For WIN32 we also want to put explicit instantiations in
23930 linkonce sections. */
23931 else if (TREE_PUBLIC (result))
23932 maybe_make_one_only (result);
23933 if (TREE_CODE (result) == FUNCTION_DECL
23934 && DECL_TEMPLATE_INSTANTIATED (result))
23935 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23936 since start_preparsed_function wouldn't have if we had an earlier
23937 extern explicit instantiation. */
23938 DECL_EXTERNAL (result) = 0;
23939 }
23940
23941 /* If EXTERN_P, then this function will not be emitted -- unless
23942 followed by an explicit instantiation, at which point its linkage
23943 will be adjusted. If !EXTERN_P, then this function will be
23944 emitted here. In neither circumstance do we want
23945 import_export_decl to adjust the linkage. */
23946 DECL_INTERFACE_KNOWN (result) = 1;
23947 }
23948
23949 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23950 important template arguments. If any are missing, we check whether
23951 they're important by using error_mark_node for substituting into any
23952 args that were used for partial ordering (the ones between ARGS and END)
23953 and seeing if it bubbles up. */
23954
23955 static bool
23956 check_undeduced_parms (tree targs, tree args, tree end)
23957 {
23958 bool found = false;
23959 int i;
23960 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23961 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23962 {
23963 found = true;
23964 TREE_VEC_ELT (targs, i) = error_mark_node;
23965 }
23966 if (found)
23967 {
23968 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23969 if (substed == error_mark_node)
23970 return true;
23971 }
23972 return false;
23973 }
23974
23975 /* Given two function templates PAT1 and PAT2, return:
23976
23977 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23978 -1 if PAT2 is more specialized than PAT1.
23979 0 if neither is more specialized.
23980
23981 LEN indicates the number of parameters we should consider
23982 (defaulted parameters should not be considered).
23983
23984 The 1998 std underspecified function template partial ordering, and
23985 DR214 addresses the issue. We take pairs of arguments, one from
23986 each of the templates, and deduce them against each other. One of
23987 the templates will be more specialized if all the *other*
23988 template's arguments deduce against its arguments and at least one
23989 of its arguments *does* *not* deduce against the other template's
23990 corresponding argument. Deduction is done as for class templates.
23991 The arguments used in deduction have reference and top level cv
23992 qualifiers removed. Iff both arguments were originally reference
23993 types *and* deduction succeeds in both directions, an lvalue reference
23994 wins against an rvalue reference and otherwise the template
23995 with the more cv-qualified argument wins for that pairing (if
23996 neither is more cv-qualified, they both are equal). Unlike regular
23997 deduction, after all the arguments have been deduced in this way,
23998 we do *not* verify the deduced template argument values can be
23999 substituted into non-deduced contexts.
24000
24001 The logic can be a bit confusing here, because we look at deduce1 and
24002 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24003 can find template arguments for pat1 to make arg1 look like arg2, that
24004 means that arg2 is at least as specialized as arg1. */
24005
24006 int
24007 more_specialized_fn (tree pat1, tree pat2, int len)
24008 {
24009 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24010 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24011 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24012 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24013 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24014 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24015 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24016 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24017 tree origs1, origs2;
24018 bool lose1 = false;
24019 bool lose2 = false;
24020
24021 /* Remove the this parameter from non-static member functions. If
24022 one is a non-static member function and the other is not a static
24023 member function, remove the first parameter from that function
24024 also. This situation occurs for operator functions where we
24025 locate both a member function (with this pointer) and non-member
24026 operator (with explicit first operand). */
24027 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24028 {
24029 len--; /* LEN is the number of significant arguments for DECL1 */
24030 args1 = TREE_CHAIN (args1);
24031 if (!DECL_STATIC_FUNCTION_P (decl2))
24032 args2 = TREE_CHAIN (args2);
24033 }
24034 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24035 {
24036 args2 = TREE_CHAIN (args2);
24037 if (!DECL_STATIC_FUNCTION_P (decl1))
24038 {
24039 len--;
24040 args1 = TREE_CHAIN (args1);
24041 }
24042 }
24043
24044 /* If only one is a conversion operator, they are unordered. */
24045 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24046 return 0;
24047
24048 /* Consider the return type for a conversion function */
24049 if (DECL_CONV_FN_P (decl1))
24050 {
24051 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24052 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24053 len++;
24054 }
24055
24056 processing_template_decl++;
24057
24058 origs1 = args1;
24059 origs2 = args2;
24060
24061 while (len--
24062 /* Stop when an ellipsis is seen. */
24063 && args1 != NULL_TREE && args2 != NULL_TREE)
24064 {
24065 tree arg1 = TREE_VALUE (args1);
24066 tree arg2 = TREE_VALUE (args2);
24067 int deduce1, deduce2;
24068 int quals1 = -1;
24069 int quals2 = -1;
24070 int ref1 = 0;
24071 int ref2 = 0;
24072
24073 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24074 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24075 {
24076 /* When both arguments are pack expansions, we need only
24077 unify the patterns themselves. */
24078 arg1 = PACK_EXPANSION_PATTERN (arg1);
24079 arg2 = PACK_EXPANSION_PATTERN (arg2);
24080
24081 /* This is the last comparison we need to do. */
24082 len = 0;
24083 }
24084
24085 if (TYPE_REF_P (arg1))
24086 {
24087 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24088 arg1 = TREE_TYPE (arg1);
24089 quals1 = cp_type_quals (arg1);
24090 }
24091
24092 if (TYPE_REF_P (arg2))
24093 {
24094 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24095 arg2 = TREE_TYPE (arg2);
24096 quals2 = cp_type_quals (arg2);
24097 }
24098
24099 arg1 = TYPE_MAIN_VARIANT (arg1);
24100 arg2 = TYPE_MAIN_VARIANT (arg2);
24101
24102 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24103 {
24104 int i, len2 = remaining_arguments (args2);
24105 tree parmvec = make_tree_vec (1);
24106 tree argvec = make_tree_vec (len2);
24107 tree ta = args2;
24108
24109 /* Setup the parameter vector, which contains only ARG1. */
24110 TREE_VEC_ELT (parmvec, 0) = arg1;
24111
24112 /* Setup the argument vector, which contains the remaining
24113 arguments. */
24114 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24115 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24116
24117 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24118 argvec, DEDUCE_EXACT,
24119 /*subr=*/true, /*explain_p=*/false)
24120 == 0);
24121
24122 /* We cannot deduce in the other direction, because ARG1 is
24123 a pack expansion but ARG2 is not. */
24124 deduce2 = 0;
24125 }
24126 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24127 {
24128 int i, len1 = remaining_arguments (args1);
24129 tree parmvec = make_tree_vec (1);
24130 tree argvec = make_tree_vec (len1);
24131 tree ta = args1;
24132
24133 /* Setup the parameter vector, which contains only ARG1. */
24134 TREE_VEC_ELT (parmvec, 0) = arg2;
24135
24136 /* Setup the argument vector, which contains the remaining
24137 arguments. */
24138 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24139 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24140
24141 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24142 argvec, DEDUCE_EXACT,
24143 /*subr=*/true, /*explain_p=*/false)
24144 == 0);
24145
24146 /* We cannot deduce in the other direction, because ARG2 is
24147 a pack expansion but ARG1 is not.*/
24148 deduce1 = 0;
24149 }
24150
24151 else
24152 {
24153 /* The normal case, where neither argument is a pack
24154 expansion. */
24155 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24156 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24157 == 0);
24158 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24159 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24160 == 0);
24161 }
24162
24163 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24164 arg2, then arg2 is not as specialized as arg1. */
24165 if (!deduce1)
24166 lose2 = true;
24167 if (!deduce2)
24168 lose1 = true;
24169
24170 /* "If, for a given type, deduction succeeds in both directions
24171 (i.e., the types are identical after the transformations above)
24172 and both P and A were reference types (before being replaced with
24173 the type referred to above):
24174 - if the type from the argument template was an lvalue reference and
24175 the type from the parameter template was not, the argument type is
24176 considered to be more specialized than the other; otherwise,
24177 - if the type from the argument template is more cv-qualified
24178 than the type from the parameter template (as described above),
24179 the argument type is considered to be more specialized than the other;
24180 otherwise,
24181 - neither type is more specialized than the other." */
24182
24183 if (deduce1 && deduce2)
24184 {
24185 if (ref1 && ref2 && ref1 != ref2)
24186 {
24187 if (ref1 > ref2)
24188 lose1 = true;
24189 else
24190 lose2 = true;
24191 }
24192 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24193 {
24194 if ((quals1 & quals2) == quals2)
24195 lose2 = true;
24196 if ((quals1 & quals2) == quals1)
24197 lose1 = true;
24198 }
24199 }
24200
24201 if (lose1 && lose2)
24202 /* We've failed to deduce something in either direction.
24203 These must be unordered. */
24204 break;
24205
24206 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24207 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24208 /* We have already processed all of the arguments in our
24209 handing of the pack expansion type. */
24210 len = 0;
24211
24212 args1 = TREE_CHAIN (args1);
24213 args2 = TREE_CHAIN (args2);
24214 }
24215
24216 /* "In most cases, all template parameters must have values in order for
24217 deduction to succeed, but for partial ordering purposes a template
24218 parameter may remain without a value provided it is not used in the
24219 types being used for partial ordering."
24220
24221 Thus, if we are missing any of the targs1 we need to substitute into
24222 origs1, then pat2 is not as specialized as pat1. This can happen when
24223 there is a nondeduced context. */
24224 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24225 lose2 = true;
24226 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24227 lose1 = true;
24228
24229 processing_template_decl--;
24230
24231 /* If both deductions succeed, the partial ordering selects the more
24232 constrained template. */
24233 /* P2113: If the corresponding template-parameters of the
24234 template-parameter-lists are not equivalent ([temp.over.link]) or if
24235 the function parameters that positionally correspond between the two
24236 templates are not of the same type, neither template is more
24237 specialized than the other. */
24238 if (!lose1 && !lose2
24239 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24240 DECL_TEMPLATE_PARMS (pat2))
24241 && compparms (origs1, origs2))
24242 {
24243 int winner = more_constrained (decl1, decl2);
24244 if (winner > 0)
24245 lose2 = true;
24246 else if (winner < 0)
24247 lose1 = true;
24248 }
24249
24250 /* All things being equal, if the next argument is a pack expansion
24251 for one function but not for the other, prefer the
24252 non-variadic function. FIXME this is bogus; see c++/41958. */
24253 if (lose1 == lose2
24254 && args1 && TREE_VALUE (args1)
24255 && args2 && TREE_VALUE (args2))
24256 {
24257 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24258 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24259 }
24260
24261 if (lose1 == lose2)
24262 return 0;
24263 else if (!lose1)
24264 return 1;
24265 else
24266 return -1;
24267 }
24268
24269 /* Determine which of two partial specializations of TMPL is more
24270 specialized.
24271
24272 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24273 to the first partial specialization. The TREE_PURPOSE is the
24274 innermost set of template parameters for the partial
24275 specialization. PAT2 is similar, but for the second template.
24276
24277 Return 1 if the first partial specialization is more specialized;
24278 -1 if the second is more specialized; 0 if neither is more
24279 specialized.
24280
24281 See [temp.class.order] for information about determining which of
24282 two templates is more specialized. */
24283
24284 static int
24285 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24286 {
24287 tree targs;
24288 int winner = 0;
24289 bool any_deductions = false;
24290
24291 tree tmpl1 = TREE_VALUE (pat1);
24292 tree tmpl2 = TREE_VALUE (pat2);
24293 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24294 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24295
24296 /* Just like what happens for functions, if we are ordering between
24297 different template specializations, we may encounter dependent
24298 types in the arguments, and we need our dependency check functions
24299 to behave correctly. */
24300 ++processing_template_decl;
24301 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24302 if (targs)
24303 {
24304 --winner;
24305 any_deductions = true;
24306 }
24307
24308 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24309 if (targs)
24310 {
24311 ++winner;
24312 any_deductions = true;
24313 }
24314 --processing_template_decl;
24315
24316 /* If both deductions succeed, the partial ordering selects the more
24317 constrained template. */
24318 if (!winner && any_deductions)
24319 winner = more_constrained (tmpl1, tmpl2);
24320
24321 /* In the case of a tie where at least one of the templates
24322 has a parameter pack at the end, the template with the most
24323 non-packed parameters wins. */
24324 if (winner == 0
24325 && any_deductions
24326 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24327 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24328 {
24329 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24330 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24331 int len1 = TREE_VEC_LENGTH (args1);
24332 int len2 = TREE_VEC_LENGTH (args2);
24333
24334 /* We don't count the pack expansion at the end. */
24335 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24336 --len1;
24337 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24338 --len2;
24339
24340 if (len1 > len2)
24341 return 1;
24342 else if (len1 < len2)
24343 return -1;
24344 }
24345
24346 return winner;
24347 }
24348
24349 /* Return the template arguments that will produce the function signature
24350 DECL from the function template FN, with the explicit template
24351 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24352 also match. Return NULL_TREE if no satisfactory arguments could be
24353 found. */
24354
24355 static tree
24356 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24357 {
24358 int ntparms = DECL_NTPARMS (fn);
24359 tree targs = make_tree_vec (ntparms);
24360 tree decl_type = TREE_TYPE (decl);
24361 tree decl_arg_types;
24362 tree *args;
24363 unsigned int nargs, ix;
24364 tree arg;
24365
24366 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24367
24368 /* Never do unification on the 'this' parameter. */
24369 decl_arg_types = skip_artificial_parms_for (decl,
24370 TYPE_ARG_TYPES (decl_type));
24371
24372 nargs = list_length (decl_arg_types);
24373 args = XALLOCAVEC (tree, nargs);
24374 for (arg = decl_arg_types, ix = 0;
24375 arg != NULL_TREE && arg != void_list_node;
24376 arg = TREE_CHAIN (arg), ++ix)
24377 args[ix] = TREE_VALUE (arg);
24378
24379 if (fn_type_unification (fn, explicit_args, targs,
24380 args, ix,
24381 (check_rettype || DECL_CONV_FN_P (fn)
24382 ? TREE_TYPE (decl_type) : NULL_TREE),
24383 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24384 /*explain_p=*/false,
24385 /*decltype*/false)
24386 == error_mark_node)
24387 return NULL_TREE;
24388
24389 return targs;
24390 }
24391
24392 /* Return the innermost template arguments that, when applied to a partial
24393 specialization SPEC_TMPL of TMPL, yield the ARGS.
24394
24395 For example, suppose we have:
24396
24397 template <class T, class U> struct S {};
24398 template <class T> struct S<T*, int> {};
24399
24400 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24401 partial specialization and the ARGS will be {double*, int}. The resulting
24402 vector will be {double}, indicating that `T' is bound to `double'. */
24403
24404 static tree
24405 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24406 {
24407 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24408 tree spec_args
24409 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24410 int i, ntparms = TREE_VEC_LENGTH (tparms);
24411 tree deduced_args;
24412 tree innermost_deduced_args;
24413
24414 innermost_deduced_args = make_tree_vec (ntparms);
24415 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24416 {
24417 deduced_args = copy_node (args);
24418 SET_TMPL_ARGS_LEVEL (deduced_args,
24419 TMPL_ARGS_DEPTH (deduced_args),
24420 innermost_deduced_args);
24421 }
24422 else
24423 deduced_args = innermost_deduced_args;
24424
24425 bool tried_array_deduction = (cxx_dialect < cxx17);
24426 again:
24427 if (unify (tparms, deduced_args,
24428 INNERMOST_TEMPLATE_ARGS (spec_args),
24429 INNERMOST_TEMPLATE_ARGS (args),
24430 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24431 return NULL_TREE;
24432
24433 for (i = 0; i < ntparms; ++i)
24434 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24435 {
24436 if (!tried_array_deduction)
24437 {
24438 try_array_deduction (tparms, innermost_deduced_args,
24439 INNERMOST_TEMPLATE_ARGS (spec_args));
24440 tried_array_deduction = true;
24441 if (TREE_VEC_ELT (innermost_deduced_args, i))
24442 goto again;
24443 }
24444 return NULL_TREE;
24445 }
24446
24447 if (!push_tinst_level (spec_tmpl, deduced_args))
24448 {
24449 excessive_deduction_depth = true;
24450 return NULL_TREE;
24451 }
24452
24453 /* Verify that nondeduced template arguments agree with the type
24454 obtained from argument deduction.
24455
24456 For example:
24457
24458 struct A { typedef int X; };
24459 template <class T, class U> struct C {};
24460 template <class T> struct C<T, typename T::X> {};
24461
24462 Then with the instantiation `C<A, int>', we can deduce that
24463 `T' is `A' but unify () does not check whether `typename T::X'
24464 is `int'. */
24465 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24466
24467 if (spec_args != error_mark_node)
24468 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24469 INNERMOST_TEMPLATE_ARGS (spec_args),
24470 tmpl, tf_none, false, false);
24471
24472 pop_tinst_level ();
24473
24474 if (spec_args == error_mark_node
24475 /* We only need to check the innermost arguments; the other
24476 arguments will always agree. */
24477 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24478 INNERMOST_TEMPLATE_ARGS (args)))
24479 return NULL_TREE;
24480
24481 /* Now that we have bindings for all of the template arguments,
24482 ensure that the arguments deduced for the template template
24483 parameters have compatible template parameter lists. See the use
24484 of template_template_parm_bindings_ok_p in fn_type_unification
24485 for more information. */
24486 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24487 return NULL_TREE;
24488
24489 return deduced_args;
24490 }
24491
24492 // Compare two function templates T1 and T2 by deducing bindings
24493 // from one against the other. If both deductions succeed, compare
24494 // constraints to see which is more constrained.
24495 static int
24496 more_specialized_inst (tree t1, tree t2)
24497 {
24498 int fate = 0;
24499 int count = 0;
24500
24501 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24502 {
24503 --fate;
24504 ++count;
24505 }
24506
24507 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24508 {
24509 ++fate;
24510 ++count;
24511 }
24512
24513 // If both deductions succeed, then one may be more constrained.
24514 if (count == 2 && fate == 0)
24515 fate = more_constrained (t1, t2);
24516
24517 return fate;
24518 }
24519
24520 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24521 Return the TREE_LIST node with the most specialized template, if
24522 any. If there is no most specialized template, the error_mark_node
24523 is returned.
24524
24525 Note that this function does not look at, or modify, the
24526 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24527 returned is one of the elements of INSTANTIATIONS, callers may
24528 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24529 and retrieve it from the value returned. */
24530
24531 tree
24532 most_specialized_instantiation (tree templates)
24533 {
24534 tree fn, champ;
24535
24536 ++processing_template_decl;
24537
24538 champ = templates;
24539 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24540 {
24541 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24542 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24543 if (fate == -1)
24544 champ = fn;
24545 else if (!fate)
24546 {
24547 /* Equally specialized, move to next function. If there
24548 is no next function, nothing's most specialized. */
24549 fn = TREE_CHAIN (fn);
24550 champ = fn;
24551 if (!fn)
24552 break;
24553 }
24554 }
24555
24556 if (champ)
24557 /* Now verify that champ is better than everything earlier in the
24558 instantiation list. */
24559 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24560 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24561 {
24562 champ = NULL_TREE;
24563 break;
24564 }
24565 }
24566
24567 processing_template_decl--;
24568
24569 if (!champ)
24570 return error_mark_node;
24571
24572 return champ;
24573 }
24574
24575 /* If DECL is a specialization of some template, return the most
24576 general such template. Otherwise, returns NULL_TREE.
24577
24578 For example, given:
24579
24580 template <class T> struct S { template <class U> void f(U); };
24581
24582 if TMPL is `template <class U> void S<int>::f(U)' this will return
24583 the full template. This function will not trace past partial
24584 specializations, however. For example, given in addition:
24585
24586 template <class T> struct S<T*> { template <class U> void f(U); };
24587
24588 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24589 `template <class T> template <class U> S<T*>::f(U)'. */
24590
24591 tree
24592 most_general_template (tree decl)
24593 {
24594 if (TREE_CODE (decl) != TEMPLATE_DECL)
24595 {
24596 if (tree tinfo = get_template_info (decl))
24597 decl = TI_TEMPLATE (tinfo);
24598 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24599 template friend, or a FIELD_DECL for a capture pack. */
24600 if (TREE_CODE (decl) != TEMPLATE_DECL)
24601 return NULL_TREE;
24602 }
24603
24604 /* Look for more and more general templates. */
24605 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24606 {
24607 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24608 (See cp-tree.h for details.) */
24609 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24610 break;
24611
24612 if (CLASS_TYPE_P (TREE_TYPE (decl))
24613 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24614 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24615 break;
24616
24617 /* Stop if we run into an explicitly specialized class template. */
24618 if (!DECL_NAMESPACE_SCOPE_P (decl)
24619 && DECL_CONTEXT (decl)
24620 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24621 break;
24622
24623 decl = DECL_TI_TEMPLATE (decl);
24624 }
24625
24626 return decl;
24627 }
24628
24629 /* Return the most specialized of the template partial specializations
24630 which can produce TARGET, a specialization of some class or variable
24631 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24632 a TEMPLATE_DECL node corresponding to the partial specialization, while
24633 the TREE_PURPOSE is the set of template arguments that must be
24634 substituted into the template pattern in order to generate TARGET.
24635
24636 If the choice of partial specialization is ambiguous, a diagnostic
24637 is issued, and the error_mark_node is returned. If there are no
24638 partial specializations matching TARGET, then NULL_TREE is
24639 returned, indicating that the primary template should be used. */
24640
24641 tree
24642 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24643 {
24644 tree list = NULL_TREE;
24645 tree t;
24646 tree champ;
24647 int fate;
24648 bool ambiguous_p;
24649 tree outer_args = NULL_TREE;
24650 tree tmpl, args;
24651
24652 if (TYPE_P (target))
24653 {
24654 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24655 tmpl = TI_TEMPLATE (tinfo);
24656 args = TI_ARGS (tinfo);
24657 }
24658 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24659 {
24660 tmpl = TREE_OPERAND (target, 0);
24661 args = TREE_OPERAND (target, 1);
24662 }
24663 else if (VAR_P (target))
24664 {
24665 tree tinfo = DECL_TEMPLATE_INFO (target);
24666 tmpl = TI_TEMPLATE (tinfo);
24667 args = TI_ARGS (tinfo);
24668 }
24669 else
24670 gcc_unreachable ();
24671
24672 tree main_tmpl = most_general_template (tmpl);
24673
24674 /* For determining which partial specialization to use, only the
24675 innermost args are interesting. */
24676 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24677 {
24678 outer_args = strip_innermost_template_args (args, 1);
24679 args = INNERMOST_TEMPLATE_ARGS (args);
24680 }
24681
24682 /* The caller hasn't called push_to_top_level yet, but we need
24683 get_partial_spec_bindings to be done in non-template context so that we'll
24684 fully resolve everything. */
24685 processing_template_decl_sentinel ptds;
24686
24687 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24688 {
24689 const tree ospec_tmpl = TREE_VALUE (t);
24690
24691 tree spec_tmpl;
24692 if (outer_args)
24693 {
24694 /* Substitute in the template args from the enclosing class. */
24695 ++processing_template_decl;
24696 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24697 --processing_template_decl;
24698 if (spec_tmpl == error_mark_node)
24699 return error_mark_node;
24700 }
24701 else
24702 spec_tmpl = ospec_tmpl;
24703
24704 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24705 if (spec_args)
24706 {
24707 if (outer_args)
24708 spec_args = add_to_template_args (outer_args, spec_args);
24709
24710 /* Keep the candidate only if the constraints are satisfied,
24711 or if we're not compiling with concepts. */
24712 if (!flag_concepts
24713 || constraints_satisfied_p (ospec_tmpl, spec_args))
24714 {
24715 list = tree_cons (spec_args, ospec_tmpl, list);
24716 TREE_TYPE (list) = TREE_TYPE (t);
24717 }
24718 }
24719 }
24720
24721 if (! list)
24722 return NULL_TREE;
24723
24724 ambiguous_p = false;
24725 t = list;
24726 champ = t;
24727 t = TREE_CHAIN (t);
24728 for (; t; t = TREE_CHAIN (t))
24729 {
24730 fate = more_specialized_partial_spec (tmpl, champ, t);
24731 if (fate == 1)
24732 ;
24733 else
24734 {
24735 if (fate == 0)
24736 {
24737 t = TREE_CHAIN (t);
24738 if (! t)
24739 {
24740 ambiguous_p = true;
24741 break;
24742 }
24743 }
24744 champ = t;
24745 }
24746 }
24747
24748 if (!ambiguous_p)
24749 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24750 {
24751 fate = more_specialized_partial_spec (tmpl, champ, t);
24752 if (fate != 1)
24753 {
24754 ambiguous_p = true;
24755 break;
24756 }
24757 }
24758
24759 if (ambiguous_p)
24760 {
24761 const char *str;
24762 char *spaces = NULL;
24763 if (!(complain & tf_error))
24764 return error_mark_node;
24765 if (TYPE_P (target))
24766 error ("ambiguous template instantiation for %q#T", target);
24767 else
24768 error ("ambiguous template instantiation for %q#D", target);
24769 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24770 for (t = list; t; t = TREE_CHAIN (t))
24771 {
24772 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24773 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24774 "%s %#qS", spaces ? spaces : str, subst);
24775 spaces = spaces ? spaces : get_spaces (str);
24776 }
24777 free (spaces);
24778 return error_mark_node;
24779 }
24780
24781 return champ;
24782 }
24783
24784 /* Explicitly instantiate DECL. */
24785
24786 void
24787 do_decl_instantiation (tree decl, tree storage)
24788 {
24789 tree result = NULL_TREE;
24790 int extern_p = 0;
24791
24792 if (!decl || decl == error_mark_node)
24793 /* An error occurred, for which grokdeclarator has already issued
24794 an appropriate message. */
24795 return;
24796 else if (! DECL_LANG_SPECIFIC (decl))
24797 {
24798 error ("explicit instantiation of non-template %q#D", decl);
24799 return;
24800 }
24801 else if (DECL_DECLARED_CONCEPT_P (decl))
24802 {
24803 if (VAR_P (decl))
24804 error ("explicit instantiation of variable concept %q#D", decl);
24805 else
24806 error ("explicit instantiation of function concept %q#D", decl);
24807 return;
24808 }
24809
24810 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24811 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24812
24813 if (VAR_P (decl) && !var_templ)
24814 {
24815 /* There is an asymmetry here in the way VAR_DECLs and
24816 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24817 the latter, the DECL we get back will be marked as a
24818 template instantiation, and the appropriate
24819 DECL_TEMPLATE_INFO will be set up. This does not happen for
24820 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24821 should handle VAR_DECLs as it currently handles
24822 FUNCTION_DECLs. */
24823 if (!DECL_CLASS_SCOPE_P (decl))
24824 {
24825 error ("%qD is not a static data member of a class template", decl);
24826 return;
24827 }
24828 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24829 if (!result || !VAR_P (result))
24830 {
24831 error ("no matching template for %qD found", decl);
24832 return;
24833 }
24834 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24835 {
24836 error ("type %qT for explicit instantiation %qD does not match "
24837 "declared type %qT", TREE_TYPE (result), decl,
24838 TREE_TYPE (decl));
24839 return;
24840 }
24841 }
24842 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24843 {
24844 error ("explicit instantiation of %q#D", decl);
24845 return;
24846 }
24847 else
24848 result = decl;
24849
24850 /* Check for various error cases. Note that if the explicit
24851 instantiation is valid the RESULT will currently be marked as an
24852 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24853 until we get here. */
24854
24855 if (DECL_TEMPLATE_SPECIALIZATION (result))
24856 {
24857 /* DR 259 [temp.spec].
24858
24859 Both an explicit instantiation and a declaration of an explicit
24860 specialization shall not appear in a program unless the explicit
24861 instantiation follows a declaration of the explicit specialization.
24862
24863 For a given set of template parameters, if an explicit
24864 instantiation of a template appears after a declaration of an
24865 explicit specialization for that template, the explicit
24866 instantiation has no effect. */
24867 return;
24868 }
24869 else if (DECL_EXPLICIT_INSTANTIATION (result))
24870 {
24871 /* [temp.spec]
24872
24873 No program shall explicitly instantiate any template more
24874 than once.
24875
24876 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24877 the first instantiation was `extern' and the second is not,
24878 and EXTERN_P for the opposite case. */
24879 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24880 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24881 /* If an "extern" explicit instantiation follows an ordinary
24882 explicit instantiation, the template is instantiated. */
24883 if (extern_p)
24884 return;
24885 }
24886 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24887 {
24888 error ("no matching template for %qD found", result);
24889 return;
24890 }
24891 else if (!DECL_TEMPLATE_INFO (result))
24892 {
24893 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24894 return;
24895 }
24896
24897 if (storage == NULL_TREE)
24898 ;
24899 else if (storage == ridpointers[(int) RID_EXTERN])
24900 {
24901 if (cxx_dialect == cxx98)
24902 pedwarn (input_location, OPT_Wpedantic,
24903 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24904 "instantiations");
24905 extern_p = 1;
24906 }
24907 else
24908 error ("storage class %qD applied to template instantiation", storage);
24909
24910 check_explicit_instantiation_namespace (result);
24911 mark_decl_instantiated (result, extern_p);
24912 if (! extern_p)
24913 instantiate_decl (result, /*defer_ok=*/true,
24914 /*expl_inst_class_mem_p=*/false);
24915 }
24916
24917 static void
24918 mark_class_instantiated (tree t, int extern_p)
24919 {
24920 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24921 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24922 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24923 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24924 if (! extern_p)
24925 {
24926 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24927 rest_of_type_compilation (t, 1);
24928 }
24929 }
24930
24931 /* Called from do_type_instantiation through binding_table_foreach to
24932 do recursive instantiation for the type bound in ENTRY. */
24933 static void
24934 bt_instantiate_type_proc (binding_entry entry, void *data)
24935 {
24936 tree storage = *(tree *) data;
24937
24938 if (MAYBE_CLASS_TYPE_P (entry->type)
24939 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24940 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24941 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24942 }
24943
24944 /* Perform an explicit instantiation of template class T. STORAGE, if
24945 non-null, is the RID for extern, inline or static. COMPLAIN is
24946 nonzero if this is called from the parser, zero if called recursively,
24947 since the standard is unclear (as detailed below). */
24948
24949 void
24950 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24951 {
24952 int extern_p = 0;
24953 int nomem_p = 0;
24954 int static_p = 0;
24955 int previous_instantiation_extern_p = 0;
24956
24957 if (TREE_CODE (t) == TYPE_DECL)
24958 t = TREE_TYPE (t);
24959
24960 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24961 {
24962 tree tmpl =
24963 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24964 if (tmpl)
24965 error ("explicit instantiation of non-class template %qD", tmpl);
24966 else
24967 error ("explicit instantiation of non-template type %qT", t);
24968 return;
24969 }
24970
24971 complete_type (t);
24972
24973 if (!COMPLETE_TYPE_P (t))
24974 {
24975 if (complain & tf_error)
24976 error ("explicit instantiation of %q#T before definition of template",
24977 t);
24978 return;
24979 }
24980
24981 if (storage != NULL_TREE)
24982 {
24983 if (storage == ridpointers[(int) RID_EXTERN])
24984 {
24985 if (cxx_dialect == cxx98)
24986 pedwarn (input_location, OPT_Wpedantic,
24987 "ISO C++ 1998 forbids the use of %<extern%> on "
24988 "explicit instantiations");
24989 }
24990 else
24991 pedwarn (input_location, OPT_Wpedantic,
24992 "ISO C++ forbids the use of %qE"
24993 " on explicit instantiations", storage);
24994
24995 if (storage == ridpointers[(int) RID_INLINE])
24996 nomem_p = 1;
24997 else if (storage == ridpointers[(int) RID_EXTERN])
24998 extern_p = 1;
24999 else if (storage == ridpointers[(int) RID_STATIC])
25000 static_p = 1;
25001 else
25002 {
25003 error ("storage class %qD applied to template instantiation",
25004 storage);
25005 extern_p = 0;
25006 }
25007 }
25008
25009 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25010 {
25011 /* DR 259 [temp.spec].
25012
25013 Both an explicit instantiation and a declaration of an explicit
25014 specialization shall not appear in a program unless the explicit
25015 instantiation follows a declaration of the explicit specialization.
25016
25017 For a given set of template parameters, if an explicit
25018 instantiation of a template appears after a declaration of an
25019 explicit specialization for that template, the explicit
25020 instantiation has no effect. */
25021 return;
25022 }
25023 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
25024 {
25025 /* [temp.spec]
25026
25027 No program shall explicitly instantiate any template more
25028 than once.
25029
25030 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25031 instantiation was `extern'. If EXTERN_P then the second is.
25032 These cases are OK. */
25033 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
25034
25035 if (!previous_instantiation_extern_p && !extern_p
25036 && (complain & tf_error))
25037 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
25038
25039 /* If we've already instantiated the template, just return now. */
25040 if (!CLASSTYPE_INTERFACE_ONLY (t))
25041 return;
25042 }
25043
25044 check_explicit_instantiation_namespace (TYPE_NAME (t));
25045 mark_class_instantiated (t, extern_p);
25046
25047 if (nomem_p)
25048 return;
25049
25050 /* In contrast to implicit instantiation, where only the
25051 declarations, and not the definitions, of members are
25052 instantiated, we have here:
25053
25054 [temp.explicit]
25055
25056 An explicit instantiation that names a class template
25057 specialization is also an explicit instantiation of the same
25058 kind (declaration or definition) of each of its members (not
25059 including members inherited from base classes and members
25060 that are templates) that has not been previously explicitly
25061 specialized in the translation unit containing the explicit
25062 instantiation, provided that the associated constraints, if
25063 any, of that member are satisfied by the template arguments
25064 of the explicit instantiation. */
25065 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25066 if ((VAR_P (fld)
25067 || (TREE_CODE (fld) == FUNCTION_DECL
25068 && !static_p
25069 && user_provided_p (fld)))
25070 && DECL_TEMPLATE_INSTANTIATION (fld)
25071 && constraints_satisfied_p (fld))
25072 {
25073 mark_decl_instantiated (fld, extern_p);
25074 if (! extern_p)
25075 instantiate_decl (fld, /*defer_ok=*/true,
25076 /*expl_inst_class_mem_p=*/true);
25077 }
25078
25079 if (CLASSTYPE_NESTED_UTDS (t))
25080 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
25081 bt_instantiate_type_proc, &storage);
25082 }
25083
25084 /* Given a function DECL, which is a specialization of TMPL, modify
25085 DECL to be a re-instantiation of TMPL with the same template
25086 arguments. TMPL should be the template into which tsubst'ing
25087 should occur for DECL, not the most general template.
25088
25089 One reason for doing this is a scenario like this:
25090
25091 template <class T>
25092 void f(const T&, int i);
25093
25094 void g() { f(3, 7); }
25095
25096 template <class T>
25097 void f(const T& t, const int i) { }
25098
25099 Note that when the template is first instantiated, with
25100 instantiate_template, the resulting DECL will have no name for the
25101 first parameter, and the wrong type for the second. So, when we go
25102 to instantiate the DECL, we regenerate it. */
25103
25104 static void
25105 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25106 {
25107 /* The arguments used to instantiate DECL, from the most general
25108 template. */
25109 tree code_pattern;
25110
25111 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25112
25113 /* Make sure that we can see identifiers, and compute access
25114 correctly. */
25115 push_access_scope (decl);
25116
25117 if (TREE_CODE (decl) == FUNCTION_DECL)
25118 {
25119 tree decl_parm;
25120 tree pattern_parm;
25121 tree specs;
25122 int args_depth;
25123 int parms_depth;
25124
25125 args_depth = TMPL_ARGS_DEPTH (args);
25126 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25127 if (args_depth > parms_depth)
25128 args = get_innermost_template_args (args, parms_depth);
25129
25130 /* Instantiate a dynamic exception-specification. noexcept will be
25131 handled below. */
25132 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25133 if (TREE_VALUE (raises))
25134 {
25135 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25136 args, tf_error, NULL_TREE,
25137 /*defer_ok*/false);
25138 if (specs && specs != error_mark_node)
25139 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25140 specs);
25141 }
25142
25143 /* Merge parameter declarations. */
25144 decl_parm = skip_artificial_parms_for (decl,
25145 DECL_ARGUMENTS (decl));
25146 pattern_parm
25147 = skip_artificial_parms_for (code_pattern,
25148 DECL_ARGUMENTS (code_pattern));
25149 while (decl_parm && !DECL_PACK_P (pattern_parm))
25150 {
25151 tree parm_type;
25152 tree attributes;
25153
25154 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25155 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25156 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25157 NULL_TREE);
25158 parm_type = type_decays_to (parm_type);
25159 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25160 TREE_TYPE (decl_parm) = parm_type;
25161 attributes = DECL_ATTRIBUTES (pattern_parm);
25162 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25163 {
25164 DECL_ATTRIBUTES (decl_parm) = attributes;
25165 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25166 }
25167 decl_parm = DECL_CHAIN (decl_parm);
25168 pattern_parm = DECL_CHAIN (pattern_parm);
25169 }
25170 /* Merge any parameters that match with the function parameter
25171 pack. */
25172 if (pattern_parm && DECL_PACK_P (pattern_parm))
25173 {
25174 int i, len;
25175 tree expanded_types;
25176 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25177 the parameters in this function parameter pack. */
25178 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25179 args, tf_error, NULL_TREE);
25180 len = TREE_VEC_LENGTH (expanded_types);
25181 for (i = 0; i < len; i++)
25182 {
25183 tree parm_type;
25184 tree attributes;
25185
25186 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25187 /* Rename the parameter to include the index. */
25188 DECL_NAME (decl_parm) =
25189 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25190 parm_type = TREE_VEC_ELT (expanded_types, i);
25191 parm_type = type_decays_to (parm_type);
25192 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25193 TREE_TYPE (decl_parm) = parm_type;
25194 attributes = DECL_ATTRIBUTES (pattern_parm);
25195 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25196 {
25197 DECL_ATTRIBUTES (decl_parm) = attributes;
25198 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25199 }
25200 decl_parm = DECL_CHAIN (decl_parm);
25201 }
25202 }
25203 /* Merge additional specifiers from the CODE_PATTERN. */
25204 if (DECL_DECLARED_INLINE_P (code_pattern)
25205 && !DECL_DECLARED_INLINE_P (decl))
25206 DECL_DECLARED_INLINE_P (decl) = 1;
25207
25208 maybe_instantiate_noexcept (decl, tf_error);
25209 }
25210 else if (VAR_P (decl))
25211 {
25212 start_lambda_scope (decl);
25213 DECL_INITIAL (decl) =
25214 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25215 tf_error, DECL_TI_TEMPLATE (decl));
25216 finish_lambda_scope ();
25217 if (VAR_HAD_UNKNOWN_BOUND (decl))
25218 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25219 tf_error, DECL_TI_TEMPLATE (decl));
25220 }
25221 else
25222 gcc_unreachable ();
25223
25224 pop_access_scope (decl);
25225 }
25226
25227 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25228 substituted to get DECL. */
25229
25230 tree
25231 template_for_substitution (tree decl)
25232 {
25233 tree tmpl = DECL_TI_TEMPLATE (decl);
25234
25235 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25236 for the instantiation. This is not always the most general
25237 template. Consider, for example:
25238
25239 template <class T>
25240 struct S { template <class U> void f();
25241 template <> void f<int>(); };
25242
25243 and an instantiation of S<double>::f<int>. We want TD to be the
25244 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25245 while (/* An instantiation cannot have a definition, so we need a
25246 more general template. */
25247 DECL_TEMPLATE_INSTANTIATION (tmpl)
25248 /* We must also deal with friend templates. Given:
25249
25250 template <class T> struct S {
25251 template <class U> friend void f() {};
25252 };
25253
25254 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25255 so far as the language is concerned, but that's still
25256 where we get the pattern for the instantiation from. On
25257 other hand, if the definition comes outside the class, say:
25258
25259 template <class T> struct S {
25260 template <class U> friend void f();
25261 };
25262 template <class U> friend void f() {}
25263
25264 we don't need to look any further. That's what the check for
25265 DECL_INITIAL is for. */
25266 || (TREE_CODE (decl) == FUNCTION_DECL
25267 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25268 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25269 {
25270 /* The present template, TD, should not be a definition. If it
25271 were a definition, we should be using it! Note that we
25272 cannot restructure the loop to just keep going until we find
25273 a template with a definition, since that might go too far if
25274 a specialization was declared, but not defined. */
25275
25276 /* Fetch the more general template. */
25277 tmpl = DECL_TI_TEMPLATE (tmpl);
25278 }
25279
25280 return tmpl;
25281 }
25282
25283 /* Returns true if we need to instantiate this template instance even if we
25284 know we aren't going to emit it. */
25285
25286 bool
25287 always_instantiate_p (tree decl)
25288 {
25289 /* We always instantiate inline functions so that we can inline them. An
25290 explicit instantiation declaration prohibits implicit instantiation of
25291 non-inline functions. With high levels of optimization, we would
25292 normally inline non-inline functions -- but we're not allowed to do
25293 that for "extern template" functions. Therefore, we check
25294 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25295 return ((TREE_CODE (decl) == FUNCTION_DECL
25296 && (DECL_DECLARED_INLINE_P (decl)
25297 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25298 /* And we need to instantiate static data members so that
25299 their initializers are available in integral constant
25300 expressions. */
25301 || (VAR_P (decl)
25302 && decl_maybe_constant_var_p (decl)));
25303 }
25304
25305 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25306 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25307 error, true otherwise. */
25308
25309 bool
25310 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25311 {
25312 tree fntype, spec, noex;
25313
25314 /* Don't instantiate a noexcept-specification from template context. */
25315 if (processing_template_decl
25316 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25317 return true;
25318
25319 if (DECL_MAYBE_DELETED (fn))
25320 {
25321 if (fn == current_function_decl)
25322 /* We're in start_preparsed_function, keep going. */
25323 return true;
25324
25325 ++function_depth;
25326 synthesize_method (fn);
25327 --function_depth;
25328 return !DECL_MAYBE_DELETED (fn);
25329 }
25330
25331 fntype = TREE_TYPE (fn);
25332 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25333
25334 if (!spec || !TREE_PURPOSE (spec))
25335 return true;
25336
25337 noex = TREE_PURPOSE (spec);
25338 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25339 && TREE_CODE (noex) != DEFERRED_PARSE)
25340 return true;
25341
25342 tree orig_fn = NULL_TREE;
25343 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25344 its FUNCTION_DECL for the rest of this function -- push_access_scope
25345 doesn't accept TEMPLATE_DECLs. */
25346 if (DECL_FUNCTION_TEMPLATE_P (fn))
25347 {
25348 orig_fn = fn;
25349 fn = DECL_TEMPLATE_RESULT (fn);
25350 }
25351
25352 if (DECL_CLONED_FUNCTION_P (fn))
25353 {
25354 tree prime = DECL_CLONED_FUNCTION (fn);
25355 if (!maybe_instantiate_noexcept (prime, complain))
25356 return false;
25357 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25358 }
25359 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25360 {
25361 static hash_set<tree>* fns = new hash_set<tree>;
25362 bool added = false;
25363 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25364 {
25365 spec = get_defaulted_eh_spec (fn, complain);
25366 if (spec == error_mark_node)
25367 /* This might have failed because of an unparsed DMI, so
25368 let's try again later. */
25369 return false;
25370 }
25371 else if (!(added = !fns->add (fn)))
25372 {
25373 /* If hash_set::add returns true, the element was already there. */
25374 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25375 DECL_SOURCE_LOCATION (fn));
25376 error_at (loc,
25377 "exception specification of %qD depends on itself",
25378 fn);
25379 spec = noexcept_false_spec;
25380 }
25381 else if (push_tinst_level (fn))
25382 {
25383 push_to_top_level ();
25384 push_access_scope (fn);
25385 push_deferring_access_checks (dk_no_deferred);
25386 input_location = DECL_SOURCE_LOCATION (fn);
25387
25388 if (!DECL_LOCAL_DECL_P (fn))
25389 {
25390 /* If needed, set current_class_ptr for the benefit of
25391 tsubst_copy/PARM_DECL. The exception pattern will
25392 refer to the parm of the template, not the
25393 instantiation. */
25394 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25395 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25396 {
25397 tree this_parm = DECL_ARGUMENTS (tdecl);
25398 current_class_ptr = NULL_TREE;
25399 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25400 current_class_ptr = this_parm;
25401 }
25402 }
25403
25404 /* If this function is represented by a TEMPLATE_DECL, then
25405 the deferred noexcept-specification might still contain
25406 dependent types, even after substitution. And we need the
25407 dependency check functions to work in build_noexcept_spec. */
25408 if (orig_fn)
25409 ++processing_template_decl;
25410
25411 /* Do deferred instantiation of the noexcept-specifier. */
25412 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25413 DEFERRED_NOEXCEPT_ARGS (noex),
25414 tf_warning_or_error, fn,
25415 /*function_p=*/false,
25416 /*i_c_e_p=*/true);
25417
25418 /* Build up the noexcept-specification. */
25419 spec = build_noexcept_spec (noex, tf_warning_or_error);
25420
25421 if (orig_fn)
25422 --processing_template_decl;
25423
25424 pop_deferring_access_checks ();
25425 pop_access_scope (fn);
25426 pop_tinst_level ();
25427 pop_from_top_level ();
25428 }
25429 else
25430 spec = noexcept_false_spec;
25431
25432 if (added)
25433 fns->remove (fn);
25434 }
25435
25436 if (spec == error_mark_node)
25437 {
25438 /* This failed with a hard error, so let's go with false. */
25439 gcc_assert (seen_error ());
25440 spec = noexcept_false_spec;
25441 }
25442
25443 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25444 if (orig_fn)
25445 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25446
25447 return true;
25448 }
25449
25450 /* We're starting to process the function INST, an instantiation of PATTERN;
25451 add their parameters to local_specializations. */
25452
25453 static void
25454 register_parameter_specializations (tree pattern, tree inst)
25455 {
25456 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25457 tree spec_parm = DECL_ARGUMENTS (inst);
25458 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25459 {
25460 register_local_specialization (spec_parm, tmpl_parm);
25461 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25462 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25463 }
25464 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25465 {
25466 if (!DECL_PACK_P (tmpl_parm)
25467 || (spec_parm && DECL_PACK_P (spec_parm)))
25468 {
25469 register_local_specialization (spec_parm, tmpl_parm);
25470 spec_parm = DECL_CHAIN (spec_parm);
25471 }
25472 else
25473 {
25474 /* Register the (value) argument pack as a specialization of
25475 TMPL_PARM, then move on. */
25476 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25477 register_local_specialization (argpack, tmpl_parm);
25478 }
25479 }
25480 gcc_assert (!spec_parm);
25481 }
25482
25483 /* Instantiate the body of D using PATTERN with ARGS. We have
25484 already determined PATTERN is the correct template to use.
25485 NESTED_P is true if this is a nested function, in which case
25486 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25487
25488 static void
25489 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25490 {
25491 tree td = NULL_TREE;
25492 tree code_pattern = pattern;
25493
25494 if (!nested_p)
25495 {
25496 td = pattern;
25497 code_pattern = DECL_TEMPLATE_RESULT (td);
25498 }
25499 else
25500 /* Only OMP reductions are nested. */
25501 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25502
25503 vec<tree> omp_privatization_save;
25504 if (current_function_decl)
25505 save_omp_privatization_clauses (omp_privatization_save);
25506
25507 bool push_to_top
25508 = !(current_function_decl
25509 && !LAMBDA_FUNCTION_P (d)
25510 && decl_function_context (d) == current_function_decl);
25511
25512 if (push_to_top)
25513 push_to_top_level ();
25514 else
25515 {
25516 gcc_assert (!processing_template_decl);
25517 push_function_context ();
25518 cp_unevaluated_operand = 0;
25519 c_inhibit_evaluation_warnings = 0;
25520 }
25521
25522 if (VAR_P (d))
25523 {
25524 /* The variable might be a lambda's extra scope, and that
25525 lambda's visibility depends on D's. */
25526 maybe_commonize_var (d);
25527 determine_visibility (d);
25528 }
25529
25530 /* Mark D as instantiated so that recursive calls to
25531 instantiate_decl do not try to instantiate it again. */
25532 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25533
25534 if (td)
25535 /* Regenerate the declaration in case the template has been modified
25536 by a subsequent redeclaration. */
25537 regenerate_decl_from_template (d, td, args);
25538
25539 /* We already set the file and line above. Reset them now in case
25540 they changed as a result of calling regenerate_decl_from_template. */
25541 input_location = DECL_SOURCE_LOCATION (d);
25542
25543 if (VAR_P (d))
25544 {
25545 tree init;
25546 bool const_init = false;
25547
25548 /* Clear out DECL_RTL; whatever was there before may not be right
25549 since we've reset the type of the declaration. */
25550 SET_DECL_RTL (d, NULL);
25551 DECL_IN_AGGR_P (d) = 0;
25552
25553 /* The initializer is placed in DECL_INITIAL by
25554 regenerate_decl_from_template so we don't need to
25555 push/pop_access_scope again here. Pull it out so that
25556 cp_finish_decl can process it. */
25557 init = DECL_INITIAL (d);
25558 DECL_INITIAL (d) = NULL_TREE;
25559 DECL_INITIALIZED_P (d) = 0;
25560
25561 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25562 initializer. That function will defer actual emission until
25563 we have a chance to determine linkage. */
25564 DECL_EXTERNAL (d) = 0;
25565
25566 /* Enter the scope of D so that access-checking works correctly. */
25567 bool enter_context = DECL_CLASS_SCOPE_P (d);
25568 if (enter_context)
25569 push_nested_class (DECL_CONTEXT (d));
25570
25571 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25572 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25573
25574 if (enter_context)
25575 pop_nested_class ();
25576 }
25577 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25578 synthesize_method (d);
25579 else if (TREE_CODE (d) == FUNCTION_DECL)
25580 {
25581 /* Set up the list of local specializations. */
25582 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25583 tree block = NULL_TREE;
25584
25585 /* Set up context. */
25586 if (nested_p)
25587 block = push_stmt_list ();
25588 else
25589 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25590
25591 perform_instantiation_time_access_checks (code_pattern, args);
25592
25593 /* Create substitution entries for the parameters. */
25594 register_parameter_specializations (code_pattern, d);
25595
25596 /* Substitute into the body of the function. */
25597 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25598 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25599 tf_warning_or_error, d);
25600 else
25601 {
25602 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25603 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25604 /*integral_constant_expression_p=*/false);
25605
25606 /* Set the current input_location to the end of the function
25607 so that finish_function knows where we are. */
25608 input_location
25609 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25610
25611 /* Remember if we saw an infinite loop in the template. */
25612 current_function_infinite_loop
25613 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25614 }
25615
25616 /* Finish the function. */
25617 if (nested_p)
25618 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25619 else
25620 {
25621 d = finish_function (/*inline_p=*/false);
25622 expand_or_defer_fn (d);
25623 }
25624
25625 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25626 cp_check_omp_declare_reduction (d);
25627 }
25628
25629 /* We're not deferring instantiation any more. */
25630 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25631
25632 if (push_to_top)
25633 pop_from_top_level ();
25634 else
25635 pop_function_context ();
25636
25637 if (current_function_decl)
25638 restore_omp_privatization_clauses (omp_privatization_save);
25639 }
25640
25641 /* Produce the definition of D, a _DECL generated from a template. If
25642 DEFER_OK is true, then we don't have to actually do the
25643 instantiation now; we just have to do it sometime. Normally it is
25644 an error if this is an explicit instantiation but D is undefined.
25645 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25646 instantiated class template. */
25647
25648 tree
25649 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25650 {
25651 tree tmpl = DECL_TI_TEMPLATE (d);
25652 tree gen_args;
25653 tree args;
25654 tree td;
25655 tree code_pattern;
25656 tree spec;
25657 tree gen_tmpl;
25658 bool pattern_defined;
25659 location_t saved_loc = input_location;
25660 int saved_unevaluated_operand = cp_unevaluated_operand;
25661 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25662 bool external_p;
25663 bool deleted_p;
25664
25665 /* This function should only be used to instantiate templates for
25666 functions and static member variables. */
25667 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25668
25669 /* A concept is never instantiated. */
25670 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25671
25672 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25673
25674 /* Variables are never deferred; if instantiation is required, they
25675 are instantiated right away. That allows for better code in the
25676 case that an expression refers to the value of the variable --
25677 if the variable has a constant value the referring expression can
25678 take advantage of that fact. */
25679 if (VAR_P (d))
25680 defer_ok = false;
25681
25682 /* Don't instantiate cloned functions. Instead, instantiate the
25683 functions they cloned. */
25684 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25685 d = DECL_CLONED_FUNCTION (d);
25686
25687 if (DECL_TEMPLATE_INSTANTIATED (d)
25688 || TREE_TYPE (d) == error_mark_node
25689 || (TREE_CODE (d) == FUNCTION_DECL
25690 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25691 || DECL_TEMPLATE_SPECIALIZATION (d))
25692 /* D has already been instantiated or explicitly specialized, so
25693 there's nothing for us to do here.
25694
25695 It might seem reasonable to check whether or not D is an explicit
25696 instantiation, and, if so, stop here. But when an explicit
25697 instantiation is deferred until the end of the compilation,
25698 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25699 the instantiation. */
25700 return d;
25701
25702 /* Check to see whether we know that this template will be
25703 instantiated in some other file, as with "extern template"
25704 extension. */
25705 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25706
25707 /* In general, we do not instantiate such templates. */
25708 if (external_p && !always_instantiate_p (d))
25709 return d;
25710
25711 gen_tmpl = most_general_template (tmpl);
25712 gen_args = DECL_TI_ARGS (d);
25713
25714 /* We should already have the extra args. */
25715 gcc_checking_assert (tmpl == gen_tmpl
25716 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25717 == TMPL_ARGS_DEPTH (gen_args)));
25718 /* And what's in the hash table should match D. */
25719 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25720 == d
25721 || spec == NULL_TREE);
25722
25723 /* This needs to happen before any tsubsting. */
25724 if (! push_tinst_level (d))
25725 return d;
25726
25727 timevar_push (TV_TEMPLATE_INST);
25728
25729 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25730 for the instantiation. */
25731 td = template_for_substitution (d);
25732 args = gen_args;
25733
25734 if (VAR_P (d))
25735 {
25736 /* Look up an explicit specialization, if any. */
25737 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25738 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25739 if (elt && elt != error_mark_node)
25740 {
25741 td = TREE_VALUE (elt);
25742 args = TREE_PURPOSE (elt);
25743 }
25744 }
25745
25746 code_pattern = DECL_TEMPLATE_RESULT (td);
25747
25748 /* We should never be trying to instantiate a member of a class
25749 template or partial specialization. */
25750 gcc_assert (d != code_pattern);
25751
25752 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25753 || DECL_TEMPLATE_SPECIALIZATION (td))
25754 /* In the case of a friend template whose definition is provided
25755 outside the class, we may have too many arguments. Drop the
25756 ones we don't need. The same is true for specializations. */
25757 args = get_innermost_template_args
25758 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25759
25760 if (TREE_CODE (d) == FUNCTION_DECL)
25761 {
25762 deleted_p = DECL_DELETED_FN (code_pattern);
25763 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25764 && DECL_INITIAL (code_pattern) != error_mark_node)
25765 || DECL_DEFAULTED_FN (code_pattern)
25766 || deleted_p);
25767 }
25768 else
25769 {
25770 deleted_p = false;
25771 if (DECL_CLASS_SCOPE_P (code_pattern))
25772 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25773 else
25774 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25775 }
25776
25777 /* We may be in the middle of deferred access check. Disable it now. */
25778 push_deferring_access_checks (dk_no_deferred);
25779
25780 /* Unless an explicit instantiation directive has already determined
25781 the linkage of D, remember that a definition is available for
25782 this entity. */
25783 if (pattern_defined
25784 && !DECL_INTERFACE_KNOWN (d)
25785 && !DECL_NOT_REALLY_EXTERN (d))
25786 mark_definable (d);
25787
25788 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25789 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25790 input_location = DECL_SOURCE_LOCATION (d);
25791
25792 /* If D is a member of an explicitly instantiated class template,
25793 and no definition is available, treat it like an implicit
25794 instantiation. */
25795 if (!pattern_defined && expl_inst_class_mem_p
25796 && DECL_EXPLICIT_INSTANTIATION (d))
25797 {
25798 /* Leave linkage flags alone on instantiations with anonymous
25799 visibility. */
25800 if (TREE_PUBLIC (d))
25801 {
25802 DECL_NOT_REALLY_EXTERN (d) = 0;
25803 DECL_INTERFACE_KNOWN (d) = 0;
25804 }
25805 SET_DECL_IMPLICIT_INSTANTIATION (d);
25806 }
25807
25808 /* Defer all other templates, unless we have been explicitly
25809 forbidden from doing so. */
25810 if (/* If there is no definition, we cannot instantiate the
25811 template. */
25812 ! pattern_defined
25813 /* If it's OK to postpone instantiation, do so. */
25814 || defer_ok
25815 /* If this is a static data member that will be defined
25816 elsewhere, we don't want to instantiate the entire data
25817 member, but we do want to instantiate the initializer so that
25818 we can substitute that elsewhere. */
25819 || (external_p && VAR_P (d))
25820 /* Handle here a deleted function too, avoid generating
25821 its body (c++/61080). */
25822 || deleted_p)
25823 {
25824 /* The definition of the static data member is now required so
25825 we must substitute the initializer. */
25826 if (VAR_P (d)
25827 && !DECL_INITIAL (d)
25828 && DECL_INITIAL (code_pattern))
25829 {
25830 tree ns;
25831 tree init;
25832 bool const_init = false;
25833 bool enter_context = DECL_CLASS_SCOPE_P (d);
25834
25835 ns = decl_namespace_context (d);
25836 push_nested_namespace (ns);
25837 if (enter_context)
25838 push_nested_class (DECL_CONTEXT (d));
25839 init = tsubst_expr (DECL_INITIAL (code_pattern),
25840 args,
25841 tf_warning_or_error, NULL_TREE,
25842 /*integral_constant_expression_p=*/false);
25843 /* If instantiating the initializer involved instantiating this
25844 again, don't call cp_finish_decl twice. */
25845 if (!DECL_INITIAL (d))
25846 {
25847 /* Make sure the initializer is still constant, in case of
25848 circular dependency (template/instantiate6.C). */
25849 const_init
25850 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25851 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25852 /*asmspec_tree=*/NULL_TREE,
25853 LOOKUP_ONLYCONVERTING);
25854 }
25855 if (enter_context)
25856 pop_nested_class ();
25857 pop_nested_namespace (ns);
25858 }
25859
25860 /* We restore the source position here because it's used by
25861 add_pending_template. */
25862 input_location = saved_loc;
25863
25864 if (at_eof && !pattern_defined
25865 && DECL_EXPLICIT_INSTANTIATION (d)
25866 && DECL_NOT_REALLY_EXTERN (d))
25867 /* [temp.explicit]
25868
25869 The definition of a non-exported function template, a
25870 non-exported member function template, or a non-exported
25871 member function or static data member of a class template
25872 shall be present in every translation unit in which it is
25873 explicitly instantiated. */
25874 permerror (input_location, "explicit instantiation of %qD "
25875 "but no definition available", d);
25876
25877 /* If we're in unevaluated context, we just wanted to get the
25878 constant value; this isn't an odr use, so don't queue
25879 a full instantiation. */
25880 if (!cp_unevaluated_operand
25881 /* ??? Historically, we have instantiated inline functions, even
25882 when marked as "extern template". */
25883 && !(external_p && VAR_P (d)))
25884 add_pending_template (d);
25885 }
25886 else
25887 {
25888 if (variable_template_p (gen_tmpl))
25889 note_variable_template_instantiation (d);
25890 instantiate_body (td, args, d, false);
25891 }
25892
25893 pop_deferring_access_checks ();
25894 timevar_pop (TV_TEMPLATE_INST);
25895 pop_tinst_level ();
25896 input_location = saved_loc;
25897 cp_unevaluated_operand = saved_unevaluated_operand;
25898 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25899
25900 return d;
25901 }
25902
25903 /* Run through the list of templates that we wish we could
25904 instantiate, and instantiate any we can. RETRIES is the
25905 number of times we retry pending template instantiation. */
25906
25907 void
25908 instantiate_pending_templates (int retries)
25909 {
25910 int reconsider;
25911 location_t saved_loc = input_location;
25912
25913 /* Instantiating templates may trigger vtable generation. This in turn
25914 may require further template instantiations. We place a limit here
25915 to avoid infinite loop. */
25916 if (pending_templates && retries >= max_tinst_depth)
25917 {
25918 tree decl = pending_templates->tinst->maybe_get_node ();
25919
25920 fatal_error (input_location,
25921 "template instantiation depth exceeds maximum of %d"
25922 " instantiating %q+D, possibly from virtual table generation"
25923 " (use %<-ftemplate-depth=%> to increase the maximum)",
25924 max_tinst_depth, decl);
25925 if (TREE_CODE (decl) == FUNCTION_DECL)
25926 /* Pretend that we defined it. */
25927 DECL_INITIAL (decl) = error_mark_node;
25928 return;
25929 }
25930
25931 do
25932 {
25933 struct pending_template **t = &pending_templates;
25934 struct pending_template *last = NULL;
25935 reconsider = 0;
25936 while (*t)
25937 {
25938 tree instantiation = reopen_tinst_level ((*t)->tinst);
25939 bool complete = false;
25940
25941 if (TYPE_P (instantiation))
25942 {
25943 if (!COMPLETE_TYPE_P (instantiation))
25944 {
25945 instantiate_class_template (instantiation);
25946 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25947 for (tree fld = TYPE_FIELDS (instantiation);
25948 fld; fld = TREE_CHAIN (fld))
25949 if ((VAR_P (fld)
25950 || (TREE_CODE (fld) == FUNCTION_DECL
25951 && !DECL_ARTIFICIAL (fld)))
25952 && DECL_TEMPLATE_INSTANTIATION (fld))
25953 instantiate_decl (fld,
25954 /*defer_ok=*/false,
25955 /*expl_inst_class_mem_p=*/false);
25956
25957 if (COMPLETE_TYPE_P (instantiation))
25958 reconsider = 1;
25959 }
25960
25961 complete = COMPLETE_TYPE_P (instantiation);
25962 }
25963 else
25964 {
25965 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25966 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25967 {
25968 instantiation
25969 = instantiate_decl (instantiation,
25970 /*defer_ok=*/false,
25971 /*expl_inst_class_mem_p=*/false);
25972 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25973 reconsider = 1;
25974 }
25975
25976 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25977 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25978 }
25979
25980 if (complete)
25981 {
25982 /* If INSTANTIATION has been instantiated, then we don't
25983 need to consider it again in the future. */
25984 struct pending_template *drop = *t;
25985 *t = (*t)->next;
25986 set_refcount_ptr (drop->tinst);
25987 pending_template_freelist ().free (drop);
25988 }
25989 else
25990 {
25991 last = *t;
25992 t = &(*t)->next;
25993 }
25994 tinst_depth = 0;
25995 set_refcount_ptr (current_tinst_level);
25996 }
25997 last_pending_template = last;
25998 }
25999 while (reconsider);
26000
26001 input_location = saved_loc;
26002 }
26003
26004 /* Substitute ARGVEC into T, which is a list of initializers for
26005 either base class or a non-static data member. The TREE_PURPOSEs
26006 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26007 instantiate_decl. */
26008
26009 static tree
26010 tsubst_initializer_list (tree t, tree argvec)
26011 {
26012 tree inits = NULL_TREE;
26013 tree target_ctor = error_mark_node;
26014
26015 for (; t; t = TREE_CHAIN (t))
26016 {
26017 tree decl;
26018 tree init;
26019 tree expanded_bases = NULL_TREE;
26020 tree expanded_arguments = NULL_TREE;
26021 int i, len = 1;
26022
26023 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26024 {
26025 tree expr;
26026 tree arg;
26027
26028 /* Expand the base class expansion type into separate base
26029 classes. */
26030 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26031 tf_warning_or_error,
26032 NULL_TREE);
26033 if (expanded_bases == error_mark_node)
26034 continue;
26035
26036 /* We'll be building separate TREE_LISTs of arguments for
26037 each base. */
26038 len = TREE_VEC_LENGTH (expanded_bases);
26039 expanded_arguments = make_tree_vec (len);
26040 for (i = 0; i < len; i++)
26041 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26042
26043 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26044 expand each argument in the TREE_VALUE of t. */
26045 expr = make_node (EXPR_PACK_EXPANSION);
26046 PACK_EXPANSION_LOCAL_P (expr) = true;
26047 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26048 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26049
26050 if (TREE_VALUE (t) == void_type_node)
26051 /* VOID_TYPE_NODE is used to indicate
26052 value-initialization. */
26053 {
26054 for (i = 0; i < len; i++)
26055 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26056 }
26057 else
26058 {
26059 /* Substitute parameter packs into each argument in the
26060 TREE_LIST. */
26061 in_base_initializer = 1;
26062 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26063 {
26064 tree expanded_exprs;
26065
26066 /* Expand the argument. */
26067 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26068 expanded_exprs
26069 = tsubst_pack_expansion (expr, argvec,
26070 tf_warning_or_error,
26071 NULL_TREE);
26072 if (expanded_exprs == error_mark_node)
26073 continue;
26074
26075 /* Prepend each of the expanded expressions to the
26076 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26077 for (i = 0; i < len; i++)
26078 {
26079 TREE_VEC_ELT (expanded_arguments, i) =
26080 tree_cons (NULL_TREE,
26081 TREE_VEC_ELT (expanded_exprs, i),
26082 TREE_VEC_ELT (expanded_arguments, i));
26083 }
26084 }
26085 in_base_initializer = 0;
26086
26087 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26088 since we built them backwards. */
26089 for (i = 0; i < len; i++)
26090 {
26091 TREE_VEC_ELT (expanded_arguments, i) =
26092 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26093 }
26094 }
26095 }
26096
26097 for (i = 0; i < len; ++i)
26098 {
26099 if (expanded_bases)
26100 {
26101 decl = TREE_VEC_ELT (expanded_bases, i);
26102 decl = expand_member_init (decl);
26103 init = TREE_VEC_ELT (expanded_arguments, i);
26104 }
26105 else
26106 {
26107 tree tmp;
26108 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26109 tf_warning_or_error, NULL_TREE);
26110
26111 decl = expand_member_init (decl);
26112 if (decl && !DECL_P (decl))
26113 in_base_initializer = 1;
26114
26115 init = TREE_VALUE (t);
26116 tmp = init;
26117 if (init != void_type_node)
26118 init = tsubst_expr (init, argvec,
26119 tf_warning_or_error, NULL_TREE,
26120 /*integral_constant_expression_p=*/false);
26121 if (init == NULL_TREE && tmp != NULL_TREE)
26122 /* If we had an initializer but it instantiated to nothing,
26123 value-initialize the object. This will only occur when
26124 the initializer was a pack expansion where the parameter
26125 packs used in that expansion were of length zero. */
26126 init = void_type_node;
26127 in_base_initializer = 0;
26128 }
26129
26130 if (target_ctor != error_mark_node
26131 && init != error_mark_node)
26132 {
26133 error ("mem-initializer for %qD follows constructor delegation",
26134 decl);
26135 return inits;
26136 }
26137 /* Look for a target constructor. */
26138 if (init != error_mark_node
26139 && decl && CLASS_TYPE_P (decl)
26140 && same_type_p (decl, current_class_type))
26141 {
26142 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26143 if (inits)
26144 {
26145 error ("constructor delegation follows mem-initializer for %qD",
26146 TREE_PURPOSE (inits));
26147 continue;
26148 }
26149 target_ctor = init;
26150 }
26151
26152 if (decl)
26153 {
26154 init = build_tree_list (decl, init);
26155 /* Carry over the dummy TREE_TYPE node containing the source
26156 location. */
26157 TREE_TYPE (init) = TREE_TYPE (t);
26158 TREE_CHAIN (init) = inits;
26159 inits = init;
26160 }
26161 }
26162 }
26163 return inits;
26164 }
26165
26166 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26167
26168 static void
26169 set_current_access_from_decl (tree decl)
26170 {
26171 if (TREE_PRIVATE (decl))
26172 current_access_specifier = access_private_node;
26173 else if (TREE_PROTECTED (decl))
26174 current_access_specifier = access_protected_node;
26175 else
26176 current_access_specifier = access_public_node;
26177 }
26178
26179 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26180 is the instantiation (which should have been created with
26181 start_enum) and ARGS are the template arguments to use. */
26182
26183 static void
26184 tsubst_enum (tree tag, tree newtag, tree args)
26185 {
26186 tree e;
26187
26188 if (SCOPED_ENUM_P (newtag))
26189 begin_scope (sk_scoped_enum, newtag);
26190
26191 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26192 {
26193 tree value;
26194 tree decl;
26195
26196 decl = TREE_VALUE (e);
26197 /* Note that in a template enum, the TREE_VALUE is the
26198 CONST_DECL, not the corresponding INTEGER_CST. */
26199 value = tsubst_expr (DECL_INITIAL (decl),
26200 args, tf_warning_or_error, NULL_TREE,
26201 /*integral_constant_expression_p=*/true);
26202
26203 /* Give this enumeration constant the correct access. */
26204 set_current_access_from_decl (decl);
26205
26206 /* Actually build the enumerator itself. Here we're assuming that
26207 enumerators can't have dependent attributes. */
26208 build_enumerator (DECL_NAME (decl), value, newtag,
26209 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26210 }
26211
26212 if (SCOPED_ENUM_P (newtag))
26213 finish_scope ();
26214
26215 finish_enum_value_list (newtag);
26216 finish_enum (newtag);
26217
26218 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26219 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26220 }
26221
26222 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26223 its type -- but without substituting the innermost set of template
26224 arguments. So, innermost set of template parameters will appear in
26225 the type. */
26226
26227 tree
26228 get_mostly_instantiated_function_type (tree decl)
26229 {
26230 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26231 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26232 }
26233
26234 /* Return truthvalue if we're processing a template different from
26235 the last one involved in diagnostics. */
26236 bool
26237 problematic_instantiation_changed (void)
26238 {
26239 return current_tinst_level != last_error_tinst_level;
26240 }
26241
26242 /* Remember current template involved in diagnostics. */
26243 void
26244 record_last_problematic_instantiation (void)
26245 {
26246 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26247 }
26248
26249 struct tinst_level *
26250 current_instantiation (void)
26251 {
26252 return current_tinst_level;
26253 }
26254
26255 /* Return TRUE if current_function_decl is being instantiated, false
26256 otherwise. */
26257
26258 bool
26259 instantiating_current_function_p (void)
26260 {
26261 return (current_instantiation ()
26262 && (current_instantiation ()->maybe_get_node ()
26263 == current_function_decl));
26264 }
26265
26266 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26267 type. Return false for ok, true for disallowed. Issue error and
26268 inform messages under control of COMPLAIN. */
26269
26270 static bool
26271 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26272 {
26273 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26274 return false;
26275 else if (TYPE_PTR_P (type))
26276 return false;
26277 else if (TYPE_REF_P (type)
26278 && !TYPE_REF_IS_RVALUE (type))
26279 return false;
26280 else if (TYPE_PTRMEM_P (type))
26281 return false;
26282 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26283 {
26284 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26285 {
26286 if (complain & tf_error)
26287 error ("non-type template parameters of deduced class type only "
26288 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26289 return true;
26290 }
26291 return false;
26292 }
26293 else if (TREE_CODE (type) == TYPENAME_TYPE)
26294 return false;
26295 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26296 return false;
26297 else if (TREE_CODE (type) == NULLPTR_TYPE)
26298 return false;
26299 /* A bound template template parm could later be instantiated to have a valid
26300 nontype parm type via an alias template. */
26301 else if (cxx_dialect >= cxx11
26302 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26303 return false;
26304 else if (VOID_TYPE_P (type))
26305 /* Fall through. */;
26306 else if (cxx_dialect >= cxx20)
26307 {
26308 if (dependent_type_p (type))
26309 return false;
26310 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26311 return true;
26312 if (structural_type_p (type))
26313 return false;
26314 if (complain & tf_error)
26315 {
26316 auto_diagnostic_group d;
26317 error ("%qT is not a valid type for a template non-type "
26318 "parameter because it is not structural", type);
26319 structural_type_p (type, true);
26320 }
26321 return true;
26322 }
26323 else if (CLASS_TYPE_P (type))
26324 {
26325 if (complain & tf_error)
26326 error ("non-type template parameters of class type only available "
26327 "with %<-std=c++20%> or %<-std=gnu++20%>");
26328 return true;
26329 }
26330
26331 if (complain & tf_error)
26332 {
26333 if (type == error_mark_node)
26334 inform (input_location, "invalid template non-type parameter");
26335 else
26336 error ("%q#T is not a valid type for a template non-type parameter",
26337 type);
26338 }
26339 return true;
26340 }
26341
26342 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26343 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26344
26345 static bool
26346 dependent_type_p_r (tree type)
26347 {
26348 tree scope;
26349
26350 /* [temp.dep.type]
26351
26352 A type is dependent if it is:
26353
26354 -- a template parameter. Template template parameters are types
26355 for us (since TYPE_P holds true for them) so we handle
26356 them here. */
26357 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26358 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26359 return true;
26360 /* -- a qualified-id with a nested-name-specifier which contains a
26361 class-name that names a dependent type or whose unqualified-id
26362 names a dependent type. */
26363 if (TREE_CODE (type) == TYPENAME_TYPE)
26364 return true;
26365
26366 /* An alias template specialization can be dependent even if the
26367 resulting type is not. */
26368 if (dependent_alias_template_spec_p (type, nt_transparent))
26369 return true;
26370
26371 /* -- a cv-qualified type where the cv-unqualified type is
26372 dependent.
26373 No code is necessary for this bullet; the code below handles
26374 cv-qualified types, and we don't want to strip aliases with
26375 TYPE_MAIN_VARIANT because of DR 1558. */
26376 /* -- a compound type constructed from any dependent type. */
26377 if (TYPE_PTRMEM_P (type))
26378 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26379 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26380 (type)));
26381 else if (INDIRECT_TYPE_P (type))
26382 return dependent_type_p (TREE_TYPE (type));
26383 else if (FUNC_OR_METHOD_TYPE_P (type))
26384 {
26385 tree arg_type;
26386
26387 if (dependent_type_p (TREE_TYPE (type)))
26388 return true;
26389 for (arg_type = TYPE_ARG_TYPES (type);
26390 arg_type;
26391 arg_type = TREE_CHAIN (arg_type))
26392 if (dependent_type_p (TREE_VALUE (arg_type)))
26393 return true;
26394 if (cxx_dialect >= cxx17)
26395 /* A value-dependent noexcept-specifier makes the type dependent. */
26396 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26397 if (tree noex = TREE_PURPOSE (spec))
26398 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26399 affect overload resolution and treating it as dependent breaks
26400 things. Same for an unparsed noexcept expression. */
26401 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26402 && TREE_CODE (noex) != DEFERRED_PARSE
26403 && value_dependent_expression_p (noex))
26404 return true;
26405 return false;
26406 }
26407 /* -- an array type constructed from any dependent type or whose
26408 size is specified by a constant expression that is
26409 value-dependent.
26410
26411 We checked for type- and value-dependence of the bounds in
26412 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26413 if (TREE_CODE (type) == ARRAY_TYPE)
26414 {
26415 if (TYPE_DOMAIN (type)
26416 && dependent_type_p (TYPE_DOMAIN (type)))
26417 return true;
26418 return dependent_type_p (TREE_TYPE (type));
26419 }
26420
26421 /* -- a template-id in which either the template name is a template
26422 parameter ... */
26423 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26424 return true;
26425 /* ... or any of the template arguments is a dependent type or
26426 an expression that is type-dependent or value-dependent. */
26427 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26428 && (any_dependent_template_arguments_p
26429 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26430 return true;
26431
26432 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26433 dependent; if the argument of the `typeof' expression is not
26434 type-dependent, then it should already been have resolved. */
26435 if (TREE_CODE (type) == TYPEOF_TYPE
26436 || TREE_CODE (type) == DECLTYPE_TYPE
26437 || TREE_CODE (type) == UNDERLYING_TYPE)
26438 return true;
26439
26440 /* A template argument pack is dependent if any of its packed
26441 arguments are. */
26442 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26443 {
26444 tree args = ARGUMENT_PACK_ARGS (type);
26445 int i, len = TREE_VEC_LENGTH (args);
26446 for (i = 0; i < len; ++i)
26447 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26448 return true;
26449 }
26450
26451 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26452 be template parameters. */
26453 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26454 return true;
26455
26456 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26457 return true;
26458
26459 /* The standard does not specifically mention types that are local
26460 to template functions or local classes, but they should be
26461 considered dependent too. For example:
26462
26463 template <int I> void f() {
26464 enum E { a = I };
26465 S<sizeof (E)> s;
26466 }
26467
26468 The size of `E' cannot be known until the value of `I' has been
26469 determined. Therefore, `E' must be considered dependent. */
26470 scope = TYPE_CONTEXT (type);
26471 if (scope && TYPE_P (scope))
26472 return dependent_type_p (scope);
26473 /* Don't use type_dependent_expression_p here, as it can lead
26474 to infinite recursion trying to determine whether a lambda
26475 nested in a lambda is dependent (c++/47687). */
26476 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26477 && DECL_LANG_SPECIFIC (scope)
26478 && DECL_TEMPLATE_INFO (scope)
26479 && (any_dependent_template_arguments_p
26480 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26481 return true;
26482
26483 /* Other types are non-dependent. */
26484 return false;
26485 }
26486
26487 /* Returns TRUE if TYPE is dependent, in the sense of
26488 [temp.dep.type]. Note that a NULL type is considered dependent. */
26489
26490 bool
26491 dependent_type_p (tree type)
26492 {
26493 /* If there are no template parameters in scope, then there can't be
26494 any dependent types. */
26495 if (!processing_template_decl)
26496 {
26497 /* If we are not processing a template, then nobody should be
26498 providing us with a dependent type. */
26499 gcc_assert (type);
26500 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26501 return false;
26502 }
26503
26504 /* If the type is NULL, we have not computed a type for the entity
26505 in question; in that case, the type is dependent. */
26506 if (!type)
26507 return true;
26508
26509 /* Erroneous types can be considered non-dependent. */
26510 if (type == error_mark_node)
26511 return false;
26512
26513 /* Getting here with global_type_node means we improperly called this
26514 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26515 gcc_checking_assert (type != global_type_node);
26516
26517 /* If we have not already computed the appropriate value for TYPE,
26518 do so now. */
26519 if (!TYPE_DEPENDENT_P_VALID (type))
26520 {
26521 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26522 TYPE_DEPENDENT_P_VALID (type) = 1;
26523 }
26524
26525 return TYPE_DEPENDENT_P (type);
26526 }
26527
26528 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26529 lookup. In other words, a dependent type that is not the current
26530 instantiation. */
26531
26532 bool
26533 dependent_scope_p (tree scope)
26534 {
26535 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26536 && !currently_open_class (scope));
26537 }
26538
26539 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26540 an unknown base of 'this' (and is therefore instantiation-dependent). */
26541
26542 static bool
26543 unknown_base_ref_p (tree t)
26544 {
26545 if (!current_class_ptr)
26546 return false;
26547
26548 tree mem = TREE_OPERAND (t, 1);
26549 if (shared_member_p (mem))
26550 return false;
26551
26552 tree cur = current_nonlambda_class_type ();
26553 if (!any_dependent_bases_p (cur))
26554 return false;
26555
26556 tree ctx = TREE_OPERAND (t, 0);
26557 if (DERIVED_FROM_P (ctx, cur))
26558 return false;
26559
26560 return true;
26561 }
26562
26563 /* T is a SCOPE_REF; return whether we need to consider it
26564 instantiation-dependent so that we can check access at instantiation
26565 time even though we know which member it resolves to. */
26566
26567 static bool
26568 instantiation_dependent_scope_ref_p (tree t)
26569 {
26570 if (DECL_P (TREE_OPERAND (t, 1))
26571 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26572 && !unknown_base_ref_p (t)
26573 && accessible_in_template_p (TREE_OPERAND (t, 0),
26574 TREE_OPERAND (t, 1)))
26575 return false;
26576 else
26577 return true;
26578 }
26579
26580 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26581 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26582 expression. */
26583
26584 /* Note that this predicate is not appropriate for general expressions;
26585 only constant expressions (that satisfy potential_constant_expression)
26586 can be tested for value dependence. */
26587
26588 bool
26589 value_dependent_expression_p (tree expression)
26590 {
26591 if (!processing_template_decl || expression == NULL_TREE)
26592 return false;
26593
26594 /* A type-dependent expression is also value-dependent. */
26595 if (type_dependent_expression_p (expression))
26596 return true;
26597
26598 switch (TREE_CODE (expression))
26599 {
26600 case BASELINK:
26601 /* A dependent member function of the current instantiation. */
26602 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26603
26604 case FUNCTION_DECL:
26605 /* A dependent member function of the current instantiation. */
26606 if (DECL_CLASS_SCOPE_P (expression)
26607 && dependent_type_p (DECL_CONTEXT (expression)))
26608 return true;
26609 break;
26610
26611 case IDENTIFIER_NODE:
26612 /* A name that has not been looked up -- must be dependent. */
26613 return true;
26614
26615 case TEMPLATE_PARM_INDEX:
26616 /* A non-type template parm. */
26617 return true;
26618
26619 case CONST_DECL:
26620 /* A non-type template parm. */
26621 if (DECL_TEMPLATE_PARM_P (expression))
26622 return true;
26623 return value_dependent_expression_p (DECL_INITIAL (expression));
26624
26625 case VAR_DECL:
26626 /* A constant with literal type and is initialized
26627 with an expression that is value-dependent. */
26628 if (DECL_DEPENDENT_INIT_P (expression)
26629 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26630 || TYPE_REF_P (TREE_TYPE (expression)))
26631 return true;
26632 if (DECL_HAS_VALUE_EXPR_P (expression))
26633 {
26634 tree value_expr = DECL_VALUE_EXPR (expression);
26635 if (value_dependent_expression_p (value_expr)
26636 /* __PRETTY_FUNCTION__ inside a template function is dependent
26637 on the name of the function. */
26638 || (DECL_PRETTY_FUNCTION_P (expression)
26639 /* It might be used in a template, but not a template
26640 function, in which case its DECL_VALUE_EXPR will be
26641 "top level". */
26642 && value_expr == error_mark_node))
26643 return true;
26644 }
26645 return false;
26646
26647 case DYNAMIC_CAST_EXPR:
26648 case STATIC_CAST_EXPR:
26649 case CONST_CAST_EXPR:
26650 case REINTERPRET_CAST_EXPR:
26651 case CAST_EXPR:
26652 case IMPLICIT_CONV_EXPR:
26653 /* These expressions are value-dependent if the type to which
26654 the cast occurs is dependent or the expression being casted
26655 is value-dependent. */
26656 {
26657 tree type = TREE_TYPE (expression);
26658
26659 if (dependent_type_p (type))
26660 return true;
26661
26662 /* A functional cast has a list of operands. */
26663 expression = TREE_OPERAND (expression, 0);
26664 if (!expression)
26665 {
26666 /* If there are no operands, it must be an expression such
26667 as "int()". This should not happen for aggregate types
26668 because it would form non-constant expressions. */
26669 gcc_assert (cxx_dialect >= cxx11
26670 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26671
26672 return false;
26673 }
26674
26675 if (TREE_CODE (expression) == TREE_LIST)
26676 return any_value_dependent_elements_p (expression);
26677
26678 return value_dependent_expression_p (expression);
26679 }
26680
26681 case SIZEOF_EXPR:
26682 if (SIZEOF_EXPR_TYPE_P (expression))
26683 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26684 /* FALLTHRU */
26685 case ALIGNOF_EXPR:
26686 case TYPEID_EXPR:
26687 /* A `sizeof' expression is value-dependent if the operand is
26688 type-dependent or is a pack expansion. */
26689 expression = TREE_OPERAND (expression, 0);
26690 if (PACK_EXPANSION_P (expression))
26691 return true;
26692 else if (TYPE_P (expression))
26693 return dependent_type_p (expression);
26694 return instantiation_dependent_uneval_expression_p (expression);
26695
26696 case AT_ENCODE_EXPR:
26697 /* An 'encode' expression is value-dependent if the operand is
26698 type-dependent. */
26699 expression = TREE_OPERAND (expression, 0);
26700 return dependent_type_p (expression);
26701
26702 case NOEXCEPT_EXPR:
26703 expression = TREE_OPERAND (expression, 0);
26704 return instantiation_dependent_uneval_expression_p (expression);
26705
26706 case SCOPE_REF:
26707 /* All instantiation-dependent expressions should also be considered
26708 value-dependent. */
26709 return instantiation_dependent_scope_ref_p (expression);
26710
26711 case COMPONENT_REF:
26712 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26713 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26714
26715 case NONTYPE_ARGUMENT_PACK:
26716 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26717 is value-dependent. */
26718 {
26719 tree values = ARGUMENT_PACK_ARGS (expression);
26720 int i, len = TREE_VEC_LENGTH (values);
26721
26722 for (i = 0; i < len; ++i)
26723 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26724 return true;
26725
26726 return false;
26727 }
26728
26729 case TRAIT_EXPR:
26730 {
26731 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26732
26733 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26734 return true;
26735
26736 if (!type2)
26737 return false;
26738
26739 if (TREE_CODE (type2) != TREE_LIST)
26740 return dependent_type_p (type2);
26741
26742 for (; type2; type2 = TREE_CHAIN (type2))
26743 if (dependent_type_p (TREE_VALUE (type2)))
26744 return true;
26745
26746 return false;
26747 }
26748
26749 case MODOP_EXPR:
26750 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26751 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26752
26753 case ARRAY_REF:
26754 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26755 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26756
26757 case ADDR_EXPR:
26758 {
26759 tree op = TREE_OPERAND (expression, 0);
26760 return (value_dependent_expression_p (op)
26761 || has_value_dependent_address (op));
26762 }
26763
26764 case REQUIRES_EXPR:
26765 /* Treat all requires-expressions as value-dependent so
26766 we don't try to fold them. */
26767 return true;
26768
26769 case TYPE_REQ:
26770 return dependent_type_p (TREE_OPERAND (expression, 0));
26771
26772 case CALL_EXPR:
26773 {
26774 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26775 return true;
26776 tree fn = get_callee_fndecl (expression);
26777 int i, nargs;
26778 nargs = call_expr_nargs (expression);
26779 for (i = 0; i < nargs; ++i)
26780 {
26781 tree op = CALL_EXPR_ARG (expression, i);
26782 /* In a call to a constexpr member function, look through the
26783 implicit ADDR_EXPR on the object argument so that it doesn't
26784 cause the call to be considered value-dependent. We also
26785 look through it in potential_constant_expression. */
26786 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26787 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26788 && TREE_CODE (op) == ADDR_EXPR)
26789 op = TREE_OPERAND (op, 0);
26790 if (value_dependent_expression_p (op))
26791 return true;
26792 }
26793 return false;
26794 }
26795
26796 case TEMPLATE_ID_EXPR:
26797 return concept_definition_p (TREE_OPERAND (expression, 0));
26798
26799 case CONSTRUCTOR:
26800 {
26801 unsigned ix;
26802 tree val;
26803 if (dependent_type_p (TREE_TYPE (expression)))
26804 return true;
26805 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26806 if (value_dependent_expression_p (val))
26807 return true;
26808 return false;
26809 }
26810
26811 case STMT_EXPR:
26812 /* Treat a GNU statement expression as dependent to avoid crashing
26813 under instantiate_non_dependent_expr; it can't be constant. */
26814 return true;
26815
26816 default:
26817 /* A constant expression is value-dependent if any subexpression is
26818 value-dependent. */
26819 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26820 {
26821 case tcc_reference:
26822 case tcc_unary:
26823 case tcc_comparison:
26824 case tcc_binary:
26825 case tcc_expression:
26826 case tcc_vl_exp:
26827 {
26828 int i, len = cp_tree_operand_length (expression);
26829
26830 for (i = 0; i < len; i++)
26831 {
26832 tree t = TREE_OPERAND (expression, i);
26833
26834 /* In some cases, some of the operands may be missing.
26835 (For example, in the case of PREDECREMENT_EXPR, the
26836 amount to increment by may be missing.) That doesn't
26837 make the expression dependent. */
26838 if (t && value_dependent_expression_p (t))
26839 return true;
26840 }
26841 }
26842 break;
26843 default:
26844 break;
26845 }
26846 break;
26847 }
26848
26849 /* The expression is not value-dependent. */
26850 return false;
26851 }
26852
26853 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26854 [temp.dep.expr]. Note that an expression with no type is
26855 considered dependent. Other parts of the compiler arrange for an
26856 expression with type-dependent subexpressions to have no type, so
26857 this function doesn't have to be fully recursive. */
26858
26859 bool
26860 type_dependent_expression_p (tree expression)
26861 {
26862 if (!processing_template_decl)
26863 return false;
26864
26865 if (expression == NULL_TREE || expression == error_mark_node)
26866 return false;
26867
26868 STRIP_ANY_LOCATION_WRAPPER (expression);
26869
26870 /* An unresolved name is always dependent. */
26871 if (identifier_p (expression)
26872 || TREE_CODE (expression) == USING_DECL
26873 || TREE_CODE (expression) == WILDCARD_DECL)
26874 return true;
26875
26876 /* A lambda-expression in template context is dependent. dependent_type_p is
26877 true for a lambda in the scope of a class or function template, but that
26878 doesn't cover all template contexts, like a default template argument. */
26879 if (TREE_CODE (expression) == LAMBDA_EXPR)
26880 return true;
26881
26882 /* A fold expression is type-dependent. */
26883 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26884 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26885 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26886 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26887 return true;
26888
26889 /* Some expression forms are never type-dependent. */
26890 if (TREE_CODE (expression) == SIZEOF_EXPR
26891 || TREE_CODE (expression) == ALIGNOF_EXPR
26892 || TREE_CODE (expression) == AT_ENCODE_EXPR
26893 || TREE_CODE (expression) == NOEXCEPT_EXPR
26894 || TREE_CODE (expression) == TRAIT_EXPR
26895 || TREE_CODE (expression) == TYPEID_EXPR
26896 || TREE_CODE (expression) == DELETE_EXPR
26897 || TREE_CODE (expression) == VEC_DELETE_EXPR
26898 || TREE_CODE (expression) == THROW_EXPR
26899 || TREE_CODE (expression) == REQUIRES_EXPR)
26900 return false;
26901
26902 /* The types of these expressions depends only on the type to which
26903 the cast occurs. */
26904 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26905 || TREE_CODE (expression) == STATIC_CAST_EXPR
26906 || TREE_CODE (expression) == CONST_CAST_EXPR
26907 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26908 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26909 || TREE_CODE (expression) == CAST_EXPR)
26910 return dependent_type_p (TREE_TYPE (expression));
26911
26912 /* The types of these expressions depends only on the type created
26913 by the expression. */
26914 if (TREE_CODE (expression) == NEW_EXPR
26915 || TREE_CODE (expression) == VEC_NEW_EXPR)
26916 {
26917 /* For NEW_EXPR tree nodes created inside a template, either
26918 the object type itself or a TREE_LIST may appear as the
26919 operand 1. */
26920 tree type = TREE_OPERAND (expression, 1);
26921 if (TREE_CODE (type) == TREE_LIST)
26922 /* This is an array type. We need to check array dimensions
26923 as well. */
26924 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26925 || value_dependent_expression_p
26926 (TREE_OPERAND (TREE_VALUE (type), 1));
26927 /* Array type whose dimension has to be deduced. */
26928 else if (TREE_CODE (type) == ARRAY_TYPE
26929 && TREE_OPERAND (expression, 2) == NULL_TREE)
26930 return true;
26931 else
26932 return dependent_type_p (type);
26933 }
26934
26935 if (TREE_CODE (expression) == SCOPE_REF)
26936 {
26937 tree scope = TREE_OPERAND (expression, 0);
26938 tree name = TREE_OPERAND (expression, 1);
26939
26940 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26941 contains an identifier associated by name lookup with one or more
26942 declarations declared with a dependent type, or...a
26943 nested-name-specifier or qualified-id that names a member of an
26944 unknown specialization. */
26945 return (type_dependent_expression_p (name)
26946 || dependent_scope_p (scope));
26947 }
26948
26949 if (TREE_CODE (expression) == TEMPLATE_DECL
26950 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26951 return uses_outer_template_parms (expression);
26952
26953 if (TREE_CODE (expression) == STMT_EXPR)
26954 expression = stmt_expr_value_expr (expression);
26955
26956 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26957 {
26958 tree elt;
26959 unsigned i;
26960
26961 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26962 {
26963 if (type_dependent_expression_p (elt))
26964 return true;
26965 }
26966 return false;
26967 }
26968
26969 /* A static data member of the current instantiation with incomplete
26970 array type is type-dependent, as the definition and specializations
26971 can have different bounds. */
26972 if (VAR_P (expression)
26973 && DECL_CLASS_SCOPE_P (expression)
26974 && dependent_type_p (DECL_CONTEXT (expression))
26975 && VAR_HAD_UNKNOWN_BOUND (expression))
26976 return true;
26977
26978 /* An array of unknown bound depending on a variadic parameter, eg:
26979
26980 template<typename... Args>
26981 void foo (Args... args)
26982 {
26983 int arr[] = { args... };
26984 }
26985
26986 template<int... vals>
26987 void bar ()
26988 {
26989 int arr[] = { vals... };
26990 }
26991
26992 If the array has no length and has an initializer, it must be that
26993 we couldn't determine its length in cp_complete_array_type because
26994 it is dependent. */
26995 if (VAR_P (expression)
26996 && TREE_TYPE (expression) != NULL_TREE
26997 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26998 && !TYPE_DOMAIN (TREE_TYPE (expression))
26999 && DECL_INITIAL (expression))
27000 return true;
27001
27002 /* A function or variable template-id is type-dependent if it has any
27003 dependent template arguments. */
27004 if (VAR_OR_FUNCTION_DECL_P (expression)
27005 && DECL_LANG_SPECIFIC (expression)
27006 && DECL_TEMPLATE_INFO (expression))
27007 {
27008 /* Consider the innermost template arguments, since those are the ones
27009 that come from the template-id; the template arguments for the
27010 enclosing class do not make it type-dependent unless they are used in
27011 the type of the decl. */
27012 if (instantiates_primary_template_p (expression)
27013 && (any_dependent_template_arguments_p
27014 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27015 return true;
27016 }
27017
27018 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27019 type-dependent. Checking this is important for functions with auto return
27020 type, which looks like a dependent type. */
27021 if (TREE_CODE (expression) == FUNCTION_DECL
27022 && !(DECL_CLASS_SCOPE_P (expression)
27023 && dependent_type_p (DECL_CONTEXT (expression)))
27024 && !(DECL_LANG_SPECIFIC (expression)
27025 && DECL_FRIEND_P (expression)
27026 && (!DECL_FRIEND_CONTEXT (expression)
27027 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27028 && !DECL_LOCAL_DECL_P (expression))
27029 {
27030 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27031 || undeduced_auto_decl (expression));
27032 return false;
27033 }
27034
27035 /* Always dependent, on the number of arguments if nothing else. */
27036 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27037 return true;
27038
27039 if (TREE_TYPE (expression) == unknown_type_node)
27040 {
27041 if (TREE_CODE (expression) == ADDR_EXPR)
27042 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27043 if (TREE_CODE (expression) == COMPONENT_REF
27044 || TREE_CODE (expression) == OFFSET_REF)
27045 {
27046 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27047 return true;
27048 expression = TREE_OPERAND (expression, 1);
27049 if (identifier_p (expression))
27050 return false;
27051 }
27052 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27053 if (TREE_CODE (expression) == SCOPE_REF)
27054 return false;
27055
27056 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27057 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27058 || TREE_CODE (expression) == CO_YIELD_EXPR)
27059 return true;
27060
27061 if (BASELINK_P (expression))
27062 {
27063 if (BASELINK_OPTYPE (expression)
27064 && dependent_type_p (BASELINK_OPTYPE (expression)))
27065 return true;
27066 expression = BASELINK_FUNCTIONS (expression);
27067 }
27068
27069 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27070 {
27071 if (any_dependent_template_arguments_p
27072 (TREE_OPERAND (expression, 1)))
27073 return true;
27074 expression = TREE_OPERAND (expression, 0);
27075 if (identifier_p (expression))
27076 return true;
27077 }
27078
27079 gcc_assert (OVL_P (expression));
27080
27081 for (lkp_iterator iter (expression); iter; ++iter)
27082 if (type_dependent_expression_p (*iter))
27083 return true;
27084
27085 return false;
27086 }
27087
27088 /* The type of a non-type template parm declared with a placeholder type
27089 depends on the corresponding template argument, even though
27090 placeholders are not normally considered dependent. */
27091 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27092 && is_auto (TREE_TYPE (expression)))
27093 return true;
27094
27095 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27096
27097 /* Dependent type attributes might not have made it from the decl to
27098 the type yet. */
27099 if (DECL_P (expression)
27100 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27101 return true;
27102
27103 return (dependent_type_p (TREE_TYPE (expression)));
27104 }
27105
27106 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27107 type-dependent if the expression refers to a member of the current
27108 instantiation and the type of the referenced member is dependent, or the
27109 class member access expression refers to a member of an unknown
27110 specialization.
27111
27112 This function returns true if the OBJECT in such a class member access
27113 expression is of an unknown specialization. */
27114
27115 bool
27116 type_dependent_object_expression_p (tree object)
27117 {
27118 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27119 dependent. */
27120 if (TREE_CODE (object) == IDENTIFIER_NODE)
27121 return true;
27122 tree scope = TREE_TYPE (object);
27123 return (!scope || dependent_scope_p (scope));
27124 }
27125
27126 /* walk_tree callback function for instantiation_dependent_expression_p,
27127 below. Returns non-zero if a dependent subexpression is found. */
27128
27129 static tree
27130 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27131 void * /*data*/)
27132 {
27133 if (TYPE_P (*tp))
27134 {
27135 /* We don't have to worry about decltype currently because decltype
27136 of an instantiation-dependent expr is a dependent type. This
27137 might change depending on the resolution of DR 1172. */
27138 *walk_subtrees = false;
27139 return NULL_TREE;
27140 }
27141 enum tree_code code = TREE_CODE (*tp);
27142 switch (code)
27143 {
27144 /* Don't treat an argument list as dependent just because it has no
27145 TREE_TYPE. */
27146 case TREE_LIST:
27147 case TREE_VEC:
27148 case NONTYPE_ARGUMENT_PACK:
27149 return NULL_TREE;
27150
27151 case TEMPLATE_PARM_INDEX:
27152 if (dependent_type_p (TREE_TYPE (*tp)))
27153 return *tp;
27154 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27155 return *tp;
27156 /* We'll check value-dependence separately. */
27157 return NULL_TREE;
27158
27159 /* Handle expressions with type operands. */
27160 case SIZEOF_EXPR:
27161 case ALIGNOF_EXPR:
27162 case TYPEID_EXPR:
27163 case AT_ENCODE_EXPR:
27164 {
27165 tree op = TREE_OPERAND (*tp, 0);
27166 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27167 op = TREE_TYPE (op);
27168 if (TYPE_P (op))
27169 {
27170 if (dependent_type_p (op))
27171 return *tp;
27172 else
27173 {
27174 *walk_subtrees = false;
27175 return NULL_TREE;
27176 }
27177 }
27178 break;
27179 }
27180
27181 case COMPONENT_REF:
27182 if (identifier_p (TREE_OPERAND (*tp, 1)))
27183 /* In a template, finish_class_member_access_expr creates a
27184 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27185 type-dependent, so that we can check access control at
27186 instantiation time (PR 42277). See also Core issue 1273. */
27187 return *tp;
27188 break;
27189
27190 case SCOPE_REF:
27191 if (instantiation_dependent_scope_ref_p (*tp))
27192 return *tp;
27193 else
27194 break;
27195
27196 /* Treat statement-expressions as dependent. */
27197 case BIND_EXPR:
27198 return *tp;
27199
27200 /* Treat requires-expressions as dependent. */
27201 case REQUIRES_EXPR:
27202 return *tp;
27203
27204 case CALL_EXPR:
27205 /* Treat concept checks as dependent. */
27206 if (concept_check_p (*tp))
27207 return *tp;
27208 break;
27209
27210 case TEMPLATE_ID_EXPR:
27211 /* Treat concept checks as dependent. */
27212 if (concept_check_p (*tp))
27213 return *tp;
27214 break;
27215
27216 case CONSTRUCTOR:
27217 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27218 return *tp;
27219 break;
27220
27221 default:
27222 break;
27223 }
27224
27225 if (type_dependent_expression_p (*tp))
27226 return *tp;
27227 else
27228 return NULL_TREE;
27229 }
27230
27231 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27232 sense defined by the ABI:
27233
27234 "An expression is instantiation-dependent if it is type-dependent
27235 or value-dependent, or it has a subexpression that is type-dependent
27236 or value-dependent."
27237
27238 Except don't actually check value-dependence for unevaluated expressions,
27239 because in sizeof(i) we don't care about the value of i. Checking
27240 type-dependence will in turn check value-dependence of array bounds/template
27241 arguments as needed. */
27242
27243 bool
27244 instantiation_dependent_uneval_expression_p (tree expression)
27245 {
27246 tree result;
27247
27248 if (!processing_template_decl)
27249 return false;
27250
27251 if (expression == error_mark_node)
27252 return false;
27253
27254 result = cp_walk_tree_without_duplicates (&expression,
27255 instantiation_dependent_r, NULL);
27256 return result != NULL_TREE;
27257 }
27258
27259 /* As above, but also check value-dependence of the expression as a whole. */
27260
27261 bool
27262 instantiation_dependent_expression_p (tree expression)
27263 {
27264 return (instantiation_dependent_uneval_expression_p (expression)
27265 || value_dependent_expression_p (expression));
27266 }
27267
27268 /* Like type_dependent_expression_p, but it also works while not processing
27269 a template definition, i.e. during substitution or mangling. */
27270
27271 bool
27272 type_dependent_expression_p_push (tree expr)
27273 {
27274 bool b;
27275 ++processing_template_decl;
27276 b = type_dependent_expression_p (expr);
27277 --processing_template_decl;
27278 return b;
27279 }
27280
27281 /* Returns TRUE if ARGS contains a type-dependent expression. */
27282
27283 bool
27284 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27285 {
27286 unsigned int i;
27287 tree arg;
27288
27289 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27290 {
27291 if (type_dependent_expression_p (arg))
27292 return true;
27293 }
27294 return false;
27295 }
27296
27297 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27298 expressions) contains any type-dependent expressions. */
27299
27300 bool
27301 any_type_dependent_elements_p (const_tree list)
27302 {
27303 for (; list; list = TREE_CHAIN (list))
27304 if (type_dependent_expression_p (TREE_VALUE (list)))
27305 return true;
27306
27307 return false;
27308 }
27309
27310 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27311 expressions) contains any value-dependent expressions. */
27312
27313 bool
27314 any_value_dependent_elements_p (const_tree list)
27315 {
27316 for (; list; list = TREE_CHAIN (list))
27317 if (value_dependent_expression_p (TREE_VALUE (list)))
27318 return true;
27319
27320 return false;
27321 }
27322
27323 /* Returns TRUE if the ARG (a template argument) is dependent. */
27324
27325 bool
27326 dependent_template_arg_p (tree arg)
27327 {
27328 if (!processing_template_decl)
27329 return false;
27330
27331 /* Assume a template argument that was wrongly written by the user
27332 is dependent. This is consistent with what
27333 any_dependent_template_arguments_p [that calls this function]
27334 does. */
27335 if (!arg || arg == error_mark_node)
27336 return true;
27337
27338 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27339 arg = argument_pack_select_arg (arg);
27340
27341 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27342 return true;
27343 if (TREE_CODE (arg) == TEMPLATE_DECL)
27344 {
27345 if (DECL_TEMPLATE_PARM_P (arg))
27346 return true;
27347 /* A member template of a dependent class is not necessarily
27348 type-dependent, but it is a dependent template argument because it
27349 will be a member of an unknown specialization to that template. */
27350 tree scope = CP_DECL_CONTEXT (arg);
27351 return TYPE_P (scope) && dependent_type_p (scope);
27352 }
27353 else if (ARGUMENT_PACK_P (arg))
27354 {
27355 tree args = ARGUMENT_PACK_ARGS (arg);
27356 int i, len = TREE_VEC_LENGTH (args);
27357 for (i = 0; i < len; ++i)
27358 {
27359 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27360 return true;
27361 }
27362
27363 return false;
27364 }
27365 else if (TYPE_P (arg))
27366 return dependent_type_p (arg);
27367 else
27368 return value_dependent_expression_p (arg);
27369 }
27370
27371 /* Returns true if ARGS (a collection of template arguments) contains
27372 any types that require structural equality testing. */
27373
27374 bool
27375 any_template_arguments_need_structural_equality_p (tree args)
27376 {
27377 int i;
27378 int j;
27379
27380 if (!args)
27381 return false;
27382 if (args == error_mark_node)
27383 return true;
27384
27385 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27386 {
27387 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27388 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27389 {
27390 tree arg = TREE_VEC_ELT (level, j);
27391 tree packed_args = NULL_TREE;
27392 int k, len = 1;
27393
27394 if (ARGUMENT_PACK_P (arg))
27395 {
27396 /* Look inside the argument pack. */
27397 packed_args = ARGUMENT_PACK_ARGS (arg);
27398 len = TREE_VEC_LENGTH (packed_args);
27399 }
27400
27401 for (k = 0; k < len; ++k)
27402 {
27403 if (packed_args)
27404 arg = TREE_VEC_ELT (packed_args, k);
27405
27406 if (error_operand_p (arg))
27407 return true;
27408 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27409 continue;
27410 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27411 return true;
27412 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27413 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27414 return true;
27415 }
27416 }
27417 }
27418
27419 return false;
27420 }
27421
27422 /* Returns true if ARGS (a collection of template arguments) contains
27423 any dependent arguments. */
27424
27425 bool
27426 any_dependent_template_arguments_p (const_tree args)
27427 {
27428 int i;
27429 int j;
27430
27431 if (!args)
27432 return false;
27433 if (args == error_mark_node)
27434 return true;
27435
27436 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27437 {
27438 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27439 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27440 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27441 return true;
27442 }
27443
27444 return false;
27445 }
27446
27447 /* Returns true if ARGS contains any errors. */
27448
27449 bool
27450 any_erroneous_template_args_p (const_tree args)
27451 {
27452 int i;
27453 int j;
27454
27455 if (args == error_mark_node)
27456 return true;
27457
27458 if (args && TREE_CODE (args) != TREE_VEC)
27459 {
27460 if (tree ti = get_template_info (args))
27461 args = TI_ARGS (ti);
27462 else
27463 args = NULL_TREE;
27464 }
27465
27466 if (!args)
27467 return false;
27468
27469 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27470 {
27471 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27472 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27473 if (error_operand_p (TREE_VEC_ELT (level, j)))
27474 return true;
27475 }
27476
27477 return false;
27478 }
27479
27480 /* Returns TRUE if the template TMPL is type-dependent. */
27481
27482 bool
27483 dependent_template_p (tree tmpl)
27484 {
27485 if (TREE_CODE (tmpl) == OVERLOAD)
27486 {
27487 for (lkp_iterator iter (tmpl); iter; ++iter)
27488 if (dependent_template_p (*iter))
27489 return true;
27490 return false;
27491 }
27492
27493 /* Template template parameters are dependent. */
27494 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27495 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27496 return true;
27497 /* So are names that have not been looked up. */
27498 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27499 return true;
27500 return false;
27501 }
27502
27503 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27504
27505 bool
27506 dependent_template_id_p (tree tmpl, tree args)
27507 {
27508 return (dependent_template_p (tmpl)
27509 || any_dependent_template_arguments_p (args));
27510 }
27511
27512 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27513 are dependent. */
27514
27515 bool
27516 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27517 {
27518 int i;
27519
27520 if (!processing_template_decl)
27521 return false;
27522
27523 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27524 {
27525 tree decl = TREE_VEC_ELT (declv, i);
27526 tree init = TREE_VEC_ELT (initv, i);
27527 tree cond = TREE_VEC_ELT (condv, i);
27528 tree incr = TREE_VEC_ELT (incrv, i);
27529
27530 if (type_dependent_expression_p (decl)
27531 || TREE_CODE (decl) == SCOPE_REF)
27532 return true;
27533
27534 if (init && type_dependent_expression_p (init))
27535 return true;
27536
27537 if (cond == global_namespace)
27538 return true;
27539
27540 if (type_dependent_expression_p (cond))
27541 return true;
27542
27543 if (COMPARISON_CLASS_P (cond)
27544 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27545 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27546 return true;
27547
27548 if (TREE_CODE (incr) == MODOP_EXPR)
27549 {
27550 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27551 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27552 return true;
27553 }
27554 else if (type_dependent_expression_p (incr))
27555 return true;
27556 else if (TREE_CODE (incr) == MODIFY_EXPR)
27557 {
27558 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27559 return true;
27560 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27561 {
27562 tree t = TREE_OPERAND (incr, 1);
27563 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27564 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27565 return true;
27566
27567 /* If this loop has a class iterator with != comparison
27568 with increment other than i++/++i/i--/--i, make sure the
27569 increment is constant. */
27570 if (CLASS_TYPE_P (TREE_TYPE (decl))
27571 && TREE_CODE (cond) == NE_EXPR)
27572 {
27573 if (TREE_OPERAND (t, 0) == decl)
27574 t = TREE_OPERAND (t, 1);
27575 else
27576 t = TREE_OPERAND (t, 0);
27577 if (TREE_CODE (t) != INTEGER_CST)
27578 return true;
27579 }
27580 }
27581 }
27582 }
27583
27584 return false;
27585 }
27586
27587 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27588 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27589 no such TYPE can be found. Note that this function peers inside
27590 uninstantiated templates and therefore should be used only in
27591 extremely limited situations. ONLY_CURRENT_P restricts this
27592 peering to the currently open classes hierarchy (which is required
27593 when comparing types). */
27594
27595 tree
27596 resolve_typename_type (tree type, bool only_current_p)
27597 {
27598 tree scope;
27599 tree name;
27600 tree decl;
27601 int quals;
27602 tree pushed_scope;
27603 tree result;
27604
27605 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27606
27607 scope = TYPE_CONTEXT (type);
27608 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27609 gcc_checking_assert (uses_template_parms (scope));
27610
27611 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27612 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27613 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27614 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27615 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27616 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27617 the TYPENAME_TYPE instead, we avoid messing up with a possible
27618 typedef variant case. */
27619 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27620
27621 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27622 it first before we can figure out what NAME refers to. */
27623 if (TREE_CODE (scope) == TYPENAME_TYPE)
27624 {
27625 if (TYPENAME_IS_RESOLVING_P (scope))
27626 /* Given a class template A with a dependent base with nested type C,
27627 typedef typename A::C::C C will land us here, as trying to resolve
27628 the initial A::C leads to the local C typedef, which leads back to
27629 A::C::C. So we break the recursion now. */
27630 return type;
27631 else
27632 scope = resolve_typename_type (scope, only_current_p);
27633 }
27634 /* If we don't know what SCOPE refers to, then we cannot resolve the
27635 TYPENAME_TYPE. */
27636 if (!CLASS_TYPE_P (scope))
27637 return type;
27638 /* If this is a typedef, we don't want to look inside (c++/11987). */
27639 if (typedef_variant_p (type))
27640 return type;
27641 /* If SCOPE isn't the template itself, it will not have a valid
27642 TYPE_FIELDS list. */
27643 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27644 /* scope is either the template itself or a compatible instantiation
27645 like X<T>, so look up the name in the original template. */
27646 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27647 /* If scope has no fields, it can't be a current instantiation. Check this
27648 before currently_open_class to avoid infinite recursion (71515). */
27649 if (!TYPE_FIELDS (scope))
27650 return type;
27651 /* If the SCOPE is not the current instantiation, there's no reason
27652 to look inside it. */
27653 if (only_current_p && !currently_open_class (scope))
27654 return type;
27655 /* Enter the SCOPE so that name lookup will be resolved as if we
27656 were in the class definition. In particular, SCOPE will no
27657 longer be considered a dependent type. */
27658 pushed_scope = push_scope (scope);
27659 /* Look up the declaration. */
27660 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27661 tf_warning_or_error);
27662
27663 result = NULL_TREE;
27664
27665 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27666 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27667 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27668 if (!decl)
27669 /*nop*/;
27670 else if (identifier_p (fullname)
27671 && TREE_CODE (decl) == TYPE_DECL)
27672 {
27673 result = TREE_TYPE (decl);
27674 if (result == error_mark_node)
27675 result = NULL_TREE;
27676 }
27677 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27678 && DECL_CLASS_TEMPLATE_P (decl))
27679 {
27680 /* Obtain the template and the arguments. */
27681 tree tmpl = TREE_OPERAND (fullname, 0);
27682 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27683 {
27684 /* We get here with a plain identifier because a previous tentative
27685 parse of the nested-name-specifier as part of a ptr-operator saw
27686 ::template X<A>. The use of ::template is necessary in a
27687 ptr-operator, but wrong in a declarator-id.
27688
27689 [temp.names]: In a qualified-id of a declarator-id, the keyword
27690 template shall not appear at the top level. */
27691 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27692 "keyword %<template%> not allowed in declarator-id");
27693 tmpl = decl;
27694 }
27695 tree args = TREE_OPERAND (fullname, 1);
27696 /* Instantiate the template. */
27697 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27698 /*entering_scope=*/true,
27699 tf_error | tf_user);
27700 if (result == error_mark_node)
27701 result = NULL_TREE;
27702 }
27703
27704 /* Leave the SCOPE. */
27705 if (pushed_scope)
27706 pop_scope (pushed_scope);
27707
27708 /* If we failed to resolve it, return the original typename. */
27709 if (!result)
27710 return type;
27711
27712 /* If lookup found a typename type, resolve that too. */
27713 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27714 {
27715 /* Ill-formed programs can cause infinite recursion here, so we
27716 must catch that. */
27717 TYPENAME_IS_RESOLVING_P (result) = 1;
27718 result = resolve_typename_type (result, only_current_p);
27719 TYPENAME_IS_RESOLVING_P (result) = 0;
27720 }
27721
27722 /* Qualify the resulting type. */
27723 quals = cp_type_quals (type);
27724 if (quals)
27725 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27726
27727 return result;
27728 }
27729
27730 /* EXPR is an expression which is not type-dependent. Return a proxy
27731 for EXPR that can be used to compute the types of larger
27732 expressions containing EXPR. */
27733
27734 tree
27735 build_non_dependent_expr (tree expr)
27736 {
27737 tree orig_expr = expr;
27738 tree inner_expr;
27739
27740 /* When checking, try to get a constant value for all non-dependent
27741 expressions in order to expose bugs in *_dependent_expression_p
27742 and constexpr. This can affect code generation, see PR70704, so
27743 only do this for -fchecking=2. */
27744 if (flag_checking > 1
27745 && cxx_dialect >= cxx11
27746 /* Don't do this during nsdmi parsing as it can lead to
27747 unexpected recursive instantiations. */
27748 && !parsing_nsdmi ()
27749 /* Don't do this during concept processing either and for
27750 the same reason. */
27751 && !processing_constraint_expression_p ())
27752 fold_non_dependent_expr (expr, tf_none);
27753
27754 STRIP_ANY_LOCATION_WRAPPER (expr);
27755
27756 /* Preserve OVERLOADs; the functions must be available to resolve
27757 types. */
27758 inner_expr = expr;
27759 if (TREE_CODE (inner_expr) == STMT_EXPR)
27760 inner_expr = stmt_expr_value_expr (inner_expr);
27761 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27762 inner_expr = TREE_OPERAND (inner_expr, 0);
27763 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27764 inner_expr = TREE_OPERAND (inner_expr, 1);
27765 if (is_overloaded_fn (inner_expr)
27766 || TREE_CODE (inner_expr) == OFFSET_REF)
27767 return orig_expr;
27768 /* There is no need to return a proxy for a variable or enumerator. */
27769 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27770 return orig_expr;
27771 /* Preserve string constants; conversions from string constants to
27772 "char *" are allowed, even though normally a "const char *"
27773 cannot be used to initialize a "char *". */
27774 if (TREE_CODE (expr) == STRING_CST)
27775 return orig_expr;
27776 /* Preserve void and arithmetic constants, as an optimization -- there is no
27777 reason to create a new node. */
27778 if (TREE_CODE (expr) == VOID_CST
27779 || TREE_CODE (expr) == INTEGER_CST
27780 || TREE_CODE (expr) == REAL_CST)
27781 return orig_expr;
27782 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27783 There is at least one place where we want to know that a
27784 particular expression is a throw-expression: when checking a ?:
27785 expression, there are special rules if the second or third
27786 argument is a throw-expression. */
27787 if (TREE_CODE (expr) == THROW_EXPR)
27788 return orig_expr;
27789
27790 /* Don't wrap an initializer list, we need to be able to look inside. */
27791 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27792 return orig_expr;
27793
27794 /* Don't wrap a dummy object, we need to be able to test for it. */
27795 if (is_dummy_object (expr))
27796 return orig_expr;
27797
27798 if (TREE_CODE (expr) == COND_EXPR)
27799 return build3 (COND_EXPR,
27800 TREE_TYPE (expr),
27801 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27802 (TREE_OPERAND (expr, 1)
27803 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27804 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27805 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27806 if (TREE_CODE (expr) == COMPOUND_EXPR
27807 && !COMPOUND_EXPR_OVERLOADED (expr))
27808 return build2 (COMPOUND_EXPR,
27809 TREE_TYPE (expr),
27810 TREE_OPERAND (expr, 0),
27811 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27812
27813 /* If the type is unknown, it can't really be non-dependent */
27814 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27815
27816 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27817 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27818 TREE_TYPE (expr), expr);
27819 }
27820
27821 /* ARGS is a vector of expressions as arguments to a function call.
27822 Replace the arguments with equivalent non-dependent expressions.
27823 This modifies ARGS in place. */
27824
27825 void
27826 make_args_non_dependent (vec<tree, va_gc> *args)
27827 {
27828 unsigned int ix;
27829 tree arg;
27830
27831 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27832 {
27833 tree newarg = build_non_dependent_expr (arg);
27834 if (newarg != arg)
27835 (*args)[ix] = newarg;
27836 }
27837 }
27838
27839 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27840 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27841 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27842
27843 static tree
27844 make_auto_1 (tree name, bool set_canonical)
27845 {
27846 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27847 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27848 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27849 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27850 (0, processing_template_decl + 1, processing_template_decl + 1,
27851 TYPE_NAME (au), NULL_TREE);
27852 if (set_canonical)
27853 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27854 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27855 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27856 if (name == decltype_auto_identifier)
27857 AUTO_IS_DECLTYPE (au) = true;
27858
27859 return au;
27860 }
27861
27862 tree
27863 make_decltype_auto (void)
27864 {
27865 return make_auto_1 (decltype_auto_identifier, true);
27866 }
27867
27868 tree
27869 make_auto (void)
27870 {
27871 return make_auto_1 (auto_identifier, true);
27872 }
27873
27874 /* Return a C++17 deduction placeholder for class template TMPL. */
27875
27876 tree
27877 make_template_placeholder (tree tmpl)
27878 {
27879 tree t = make_auto_1 (auto_identifier, false);
27880 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27881 /* Our canonical type depends on the placeholder. */
27882 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27883 return t;
27884 }
27885
27886 /* True iff T is a C++17 class template deduction placeholder. */
27887
27888 bool
27889 template_placeholder_p (tree t)
27890 {
27891 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27892 }
27893
27894 /* Make a "constrained auto" type-specifier. This is an auto or
27895 decltype(auto) type with constraints that must be associated after
27896 deduction. The constraint is formed from the given concept CON
27897 and its optional sequence of template arguments ARGS.
27898
27899 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27900
27901 static tree
27902 make_constrained_placeholder_type (tree type, tree con, tree args)
27903 {
27904 /* Build the constraint. */
27905 tree tmpl = DECL_TI_TEMPLATE (con);
27906 tree expr = tmpl;
27907 if (TREE_CODE (con) == FUNCTION_DECL)
27908 expr = ovl_make (tmpl);
27909 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27910
27911 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27912
27913 /* Our canonical type depends on the constraint. */
27914 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27915
27916 /* Attach the constraint to the type declaration. */
27917 return TYPE_NAME (type);
27918 }
27919
27920 /* Make a "constrained auto" type-specifier. */
27921
27922 tree
27923 make_constrained_auto (tree con, tree args)
27924 {
27925 tree type = make_auto_1 (auto_identifier, false);
27926 return make_constrained_placeholder_type (type, con, args);
27927 }
27928
27929 /* Make a "constrained decltype(auto)" type-specifier. */
27930
27931 tree
27932 make_constrained_decltype_auto (tree con, tree args)
27933 {
27934 tree type = make_auto_1 (decltype_auto_identifier, false);
27935 return make_constrained_placeholder_type (type, con, args);
27936 }
27937
27938 /* Build and return a concept definition. Like other templates, the
27939 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27940 the TEMPLATE_DECL. */
27941
27942 tree
27943 finish_concept_definition (cp_expr id, tree init)
27944 {
27945 gcc_assert (identifier_p (id));
27946 gcc_assert (processing_template_decl);
27947
27948 location_t loc = id.get_location();
27949
27950 /* A concept-definition shall not have associated constraints. */
27951 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27952 {
27953 error_at (loc, "a concept cannot be constrained");
27954 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27955 }
27956
27957 /* A concept-definition shall appear in namespace scope. Templates
27958 aren't allowed in block scope, so we only need to check for class
27959 scope. */
27960 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27961 {
27962 error_at (loc, "concept %qE not in namespace scope", *id);
27963 return error_mark_node;
27964 }
27965
27966 /* Initially build the concept declaration; its type is bool. */
27967 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27968 DECL_CONTEXT (decl) = current_scope ();
27969 DECL_INITIAL (decl) = init;
27970
27971 /* Push the enclosing template. */
27972 return push_template_decl (decl);
27973 }
27974
27975 /* Given type ARG, return std::initializer_list<ARG>. */
27976
27977 static tree
27978 listify (tree arg)
27979 {
27980 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27981
27982 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27983 {
27984 gcc_rich_location richloc (input_location);
27985 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27986 error_at (&richloc,
27987 "deducing from brace-enclosed initializer list"
27988 " requires %<#include <initializer_list>%>");
27989
27990 return error_mark_node;
27991 }
27992 tree argvec = make_tree_vec (1);
27993 TREE_VEC_ELT (argvec, 0) = arg;
27994
27995 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27996 NULL_TREE, 0, tf_warning_or_error);
27997 }
27998
27999 /* Replace auto in TYPE with std::initializer_list<auto>. */
28000
28001 static tree
28002 listify_autos (tree type, tree auto_node)
28003 {
28004 tree init_auto = listify (strip_top_quals (auto_node));
28005 tree argvec = make_tree_vec (1);
28006 TREE_VEC_ELT (argvec, 0) = init_auto;
28007 if (processing_template_decl)
28008 argvec = add_to_template_args (current_template_args (), argvec);
28009 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28010 }
28011
28012 /* Hash traits for hashing possibly constrained 'auto'
28013 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28014
28015 struct auto_hash : default_hash_traits<tree>
28016 {
28017 static inline hashval_t hash (tree);
28018 static inline bool equal (tree, tree);
28019 };
28020
28021 /* Hash the 'auto' T. */
28022
28023 inline hashval_t
28024 auto_hash::hash (tree t)
28025 {
28026 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28027 /* Matching constrained-type-specifiers denote the same template
28028 parameter, so hash the constraint. */
28029 return hash_placeholder_constraint (c);
28030 else
28031 /* But unconstrained autos are all separate, so just hash the pointer. */
28032 return iterative_hash_object (t, 0);
28033 }
28034
28035 /* Compare two 'auto's. */
28036
28037 inline bool
28038 auto_hash::equal (tree t1, tree t2)
28039 {
28040 if (t1 == t2)
28041 return true;
28042
28043 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28044 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28045
28046 /* Two unconstrained autos are distinct. */
28047 if (!c1 || !c2)
28048 return false;
28049
28050 return equivalent_placeholder_constraints (c1, c2);
28051 }
28052
28053 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28054 constrained) auto, add it to the vector. */
28055
28056 static int
28057 extract_autos_r (tree t, void *data)
28058 {
28059 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28060 if (is_auto (t))
28061 {
28062 /* All the autos were built with index 0; fix that up now. */
28063 tree *p = hash.find_slot (t, INSERT);
28064 unsigned idx;
28065 if (*p)
28066 /* If this is a repeated constrained-type-specifier, use the index we
28067 chose before. */
28068 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28069 else
28070 {
28071 /* Otherwise this is new, so use the current count. */
28072 *p = t;
28073 idx = hash.elements () - 1;
28074 }
28075 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28076 }
28077
28078 /* Always keep walking. */
28079 return 0;
28080 }
28081
28082 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28083 says they can appear anywhere in the type. */
28084
28085 static tree
28086 extract_autos (tree type)
28087 {
28088 hash_set<tree> visited;
28089 hash_table<auto_hash> hash (2);
28090
28091 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28092
28093 tree tree_vec = make_tree_vec (hash.elements());
28094 for (hash_table<auto_hash>::iterator iter = hash.begin();
28095 iter != hash.end(); ++iter)
28096 {
28097 tree elt = *iter;
28098 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28099 TREE_VEC_ELT (tree_vec, i)
28100 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28101 }
28102
28103 return tree_vec;
28104 }
28105
28106 /* The stem for deduction guide names. */
28107 const char *const dguide_base = "__dguide_";
28108
28109 /* Return the name for a deduction guide for class template TMPL. */
28110
28111 tree
28112 dguide_name (tree tmpl)
28113 {
28114 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28115 tree tname = TYPE_IDENTIFIER (type);
28116 char *buf = (char *) alloca (1 + strlen (dguide_base)
28117 + IDENTIFIER_LENGTH (tname));
28118 memcpy (buf, dguide_base, strlen (dguide_base));
28119 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28120 IDENTIFIER_LENGTH (tname) + 1);
28121 tree dname = get_identifier (buf);
28122 TREE_TYPE (dname) = type;
28123 return dname;
28124 }
28125
28126 /* True if NAME is the name of a deduction guide. */
28127
28128 bool
28129 dguide_name_p (tree name)
28130 {
28131 return (TREE_CODE (name) == IDENTIFIER_NODE
28132 && TREE_TYPE (name)
28133 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28134 strlen (dguide_base)));
28135 }
28136
28137 /* True if FN is a deduction guide. */
28138
28139 bool
28140 deduction_guide_p (const_tree fn)
28141 {
28142 if (DECL_P (fn))
28143 if (tree name = DECL_NAME (fn))
28144 return dguide_name_p (name);
28145 return false;
28146 }
28147
28148 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28149
28150 bool
28151 copy_guide_p (const_tree fn)
28152 {
28153 gcc_assert (deduction_guide_p (fn));
28154 if (!DECL_ARTIFICIAL (fn))
28155 return false;
28156 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28157 return (TREE_CHAIN (parms) == void_list_node
28158 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28159 }
28160
28161 /* True if FN is a guide generated from a constructor template. */
28162
28163 bool
28164 template_guide_p (const_tree fn)
28165 {
28166 gcc_assert (deduction_guide_p (fn));
28167 if (!DECL_ARTIFICIAL (fn))
28168 return false;
28169 tree tmpl = DECL_TI_TEMPLATE (fn);
28170 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28171 return PRIMARY_TEMPLATE_P (org);
28172 return false;
28173 }
28174
28175 /* True if FN is an aggregate initialization guide or the copy deduction
28176 guide. */
28177
28178 bool
28179 builtin_guide_p (const_tree fn)
28180 {
28181 if (!deduction_guide_p (fn))
28182 return false;
28183 if (!DECL_ARTIFICIAL (fn))
28184 /* Explicitly declared. */
28185 return false;
28186 if (DECL_ABSTRACT_ORIGIN (fn))
28187 /* Derived from a constructor. */
28188 return false;
28189 return true;
28190 }
28191
28192 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28193 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28194 template parameter types. Note that the handling of template template
28195 parameters relies on current_template_parms being set appropriately for the
28196 new template. */
28197
28198 static tree
28199 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28200 tree tsubst_args, tsubst_flags_t complain)
28201 {
28202 if (olddecl == error_mark_node)
28203 return error_mark_node;
28204
28205 tree oldidx = get_template_parm_index (olddecl);
28206
28207 tree newtype;
28208 if (TREE_CODE (olddecl) == TYPE_DECL
28209 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28210 {
28211 tree oldtype = TREE_TYPE (olddecl);
28212 newtype = cxx_make_type (TREE_CODE (oldtype));
28213 TYPE_MAIN_VARIANT (newtype) = newtype;
28214 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28215 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28216 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28217 }
28218 else
28219 {
28220 newtype = TREE_TYPE (olddecl);
28221 if (type_uses_auto (newtype))
28222 {
28223 // Substitute once to fix references to other template parameters.
28224 newtype = tsubst (newtype, tsubst_args,
28225 complain|tf_partial, NULL_TREE);
28226 // Now substitute again to reduce the level of the auto.
28227 newtype = tsubst (newtype, current_template_args (),
28228 complain, NULL_TREE);
28229 }
28230 else
28231 newtype = tsubst (newtype, tsubst_args,
28232 complain, NULL_TREE);
28233 }
28234
28235 tree newdecl
28236 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28237 DECL_NAME (olddecl), newtype);
28238 SET_DECL_TEMPLATE_PARM_P (newdecl);
28239
28240 tree newidx;
28241 if (TREE_CODE (olddecl) == TYPE_DECL
28242 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28243 {
28244 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28245 = build_template_parm_index (index, level, level,
28246 newdecl, newtype);
28247 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28248 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28249 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28250 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28251 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28252 else
28253 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28254
28255 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28256 {
28257 DECL_TEMPLATE_RESULT (newdecl)
28258 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28259 DECL_NAME (olddecl), newtype);
28260 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28261 // First create a copy (ttargs) of tsubst_args with an
28262 // additional level for the template template parameter's own
28263 // template parameters (ttparms).
28264 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28265 (DECL_TEMPLATE_PARMS (olddecl)));
28266 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28267 tree ttargs = make_tree_vec (depth + 1);
28268 for (int i = 0; i < depth; ++i)
28269 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28270 TREE_VEC_ELT (ttargs, depth)
28271 = template_parms_level_to_args (ttparms);
28272 // Substitute ttargs into ttparms to fix references to
28273 // other template parameters.
28274 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28275 complain|tf_partial);
28276 // Now substitute again with args based on tparms, to reduce
28277 // the level of the ttparms.
28278 ttargs = current_template_args ();
28279 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28280 complain);
28281 // Finally, tack the adjusted parms onto tparms.
28282 ttparms = tree_cons (size_int (depth), ttparms,
28283 current_template_parms);
28284 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28285 }
28286 }
28287 else
28288 {
28289 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28290 tree newconst
28291 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28292 TREE_CODE (oldconst),
28293 DECL_NAME (oldconst), newtype);
28294 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28295 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28296 SET_DECL_TEMPLATE_PARM_P (newconst);
28297 newidx = build_template_parm_index (index, level, level,
28298 newconst, newtype);
28299 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28300 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28301 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28302 }
28303
28304 return newdecl;
28305 }
28306
28307 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28308 template parameter. */
28309
28310 static tree
28311 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28312 tree targs, unsigned targs_index, tsubst_flags_t complain)
28313 {
28314 tree olddecl = TREE_VALUE (oldelt);
28315 tree newdecl = rewrite_template_parm (olddecl, index, level,
28316 targs, complain);
28317 if (newdecl == error_mark_node)
28318 return error_mark_node;
28319 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28320 targs, complain, NULL_TREE);
28321 tree list = build_tree_list (newdef, newdecl);
28322 TEMPLATE_PARM_CONSTRAINTS (list)
28323 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28324 targs, complain, NULL_TREE);
28325 int depth = TMPL_ARGS_DEPTH (targs);
28326 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28327 return list;
28328 }
28329
28330 /* Returns a C++17 class deduction guide template based on the constructor
28331 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28332 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28333 aggregate initialization guide. */
28334
28335 static tree
28336 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28337 {
28338 tree tparms, targs, fparms, fargs, ci;
28339 bool memtmpl = false;
28340 bool explicit_p;
28341 location_t loc;
28342 tree fn_tmpl = NULL_TREE;
28343
28344 if (outer_args)
28345 {
28346 ++processing_template_decl;
28347 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28348 --processing_template_decl;
28349 }
28350
28351 if (!DECL_DECLARES_FUNCTION_P (ctor))
28352 {
28353 if (TYPE_P (ctor))
28354 {
28355 bool copy_p = TYPE_REF_P (ctor);
28356 if (copy_p)
28357 fparms = tree_cons (NULL_TREE, type, void_list_node);
28358 else
28359 fparms = void_list_node;
28360 }
28361 else if (TREE_CODE (ctor) == TREE_LIST)
28362 fparms = ctor;
28363 else
28364 gcc_unreachable ();
28365
28366 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28367 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28368 targs = CLASSTYPE_TI_ARGS (type);
28369 ci = NULL_TREE;
28370 fargs = NULL_TREE;
28371 loc = DECL_SOURCE_LOCATION (ctmpl);
28372 explicit_p = false;
28373 }
28374 else
28375 {
28376 ++processing_template_decl;
28377 bool ok = true;
28378
28379 fn_tmpl
28380 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28381 : DECL_TI_TEMPLATE (ctor));
28382 if (outer_args)
28383 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28384 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28385
28386 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28387 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28388 fully specialized args for the enclosing class. Strip those off, as
28389 the deduction guide won't have those template parameters. */
28390 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28391 TMPL_PARMS_DEPTH (tparms));
28392 /* Discard the 'this' parameter. */
28393 fparms = FUNCTION_ARG_CHAIN (ctor);
28394 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28395 ci = get_constraints (ctor);
28396 loc = DECL_SOURCE_LOCATION (ctor);
28397 explicit_p = DECL_NONCONVERTING_P (ctor);
28398
28399 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28400 {
28401 memtmpl = true;
28402
28403 /* For a member template constructor, we need to flatten the two
28404 template parameter lists into one, and then adjust the function
28405 signature accordingly. This gets...complicated. */
28406 tree save_parms = current_template_parms;
28407
28408 /* For a member template we should have two levels of parms/args, one
28409 for the class and one for the constructor. We stripped
28410 specialized args for further enclosing classes above. */
28411 const int depth = 2;
28412 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28413
28414 /* Template args for translating references to the two-level template
28415 parameters into references to the one-level template parameters we
28416 are creating. */
28417 tree tsubst_args = copy_node (targs);
28418 TMPL_ARGS_LEVEL (tsubst_args, depth)
28419 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28420
28421 /* Template parms for the constructor template. */
28422 tree ftparms = TREE_VALUE (tparms);
28423 unsigned flen = TREE_VEC_LENGTH (ftparms);
28424 /* Template parms for the class template. */
28425 tparms = TREE_CHAIN (tparms);
28426 tree ctparms = TREE_VALUE (tparms);
28427 unsigned clen = TREE_VEC_LENGTH (ctparms);
28428 /* Template parms for the deduction guide start as a copy of the
28429 template parms for the class. We set current_template_parms for
28430 lookup_template_class_1. */
28431 current_template_parms = tparms = copy_node (tparms);
28432 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28433 for (unsigned i = 0; i < clen; ++i)
28434 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28435
28436 /* Now we need to rewrite the constructor parms to append them to the
28437 class parms. */
28438 for (unsigned i = 0; i < flen; ++i)
28439 {
28440 unsigned index = i + clen;
28441 unsigned level = 1;
28442 tree oldelt = TREE_VEC_ELT (ftparms, i);
28443 tree newelt
28444 = rewrite_tparm_list (oldelt, index, level,
28445 tsubst_args, i, complain);
28446 if (newelt == error_mark_node)
28447 ok = false;
28448 TREE_VEC_ELT (new_vec, index) = newelt;
28449 }
28450
28451 /* Now we have a final set of template parms to substitute into the
28452 function signature. */
28453 targs = template_parms_to_args (tparms);
28454 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28455 complain, ctor);
28456 if (fparms == error_mark_node)
28457 ok = false;
28458 if (ci)
28459 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28460
28461 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28462 cp_unevaluated_operand. */
28463 cp_evaluated ev;
28464 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28465 current_template_parms = save_parms;
28466 }
28467 else
28468 {
28469 /* Substitute in the same arguments to rewrite class members into
28470 references to members of an unknown specialization. */
28471 cp_evaluated ev;
28472 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28473 fargs = tsubst (fargs, targs, complain, ctor);
28474 if (ci)
28475 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28476 }
28477
28478 --processing_template_decl;
28479 if (!ok)
28480 return error_mark_node;
28481 }
28482
28483 if (!memtmpl)
28484 {
28485 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28486 tparms = copy_node (tparms);
28487 INNERMOST_TEMPLATE_PARMS (tparms)
28488 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28489 }
28490
28491 tree fntype = build_function_type (type, fparms);
28492 tree ded_fn = build_lang_decl_loc (loc,
28493 FUNCTION_DECL,
28494 dguide_name (type), fntype);
28495 DECL_ARGUMENTS (ded_fn) = fargs;
28496 DECL_ARTIFICIAL (ded_fn) = true;
28497 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28498 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28499 DECL_ARTIFICIAL (ded_tmpl) = true;
28500 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28501 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28502 if (DECL_P (ctor))
28503 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28504 if (ci)
28505 set_constraints (ded_tmpl, ci);
28506
28507 return ded_tmpl;
28508 }
28509
28510 /* Add to LIST the member types for the reshaped initializer CTOR. */
28511
28512 static tree
28513 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28514 {
28515 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28516 tree idx, val; unsigned i;
28517 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28518 {
28519 tree ftype = elt ? elt : TREE_TYPE (idx);
28520 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28521 && CONSTRUCTOR_NELTS (val)
28522 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28523 type gets a single initializer. */
28524 && CP_AGGREGATE_TYPE_P (ftype)
28525 && !(TREE_CODE (ftype) == ARRAY_TYPE
28526 && uses_template_parms (TYPE_DOMAIN (ftype))))
28527 {
28528 tree subelt = NULL_TREE;
28529 if (TREE_CODE (ftype) == ARRAY_TYPE)
28530 subelt = TREE_TYPE (ftype);
28531 list = collect_ctor_idx_types (val, list, subelt);
28532 continue;
28533 }
28534 tree arg = NULL_TREE;
28535 if (i == v->length() - 1
28536 && PACK_EXPANSION_P (ftype))
28537 /* Give the trailing pack expansion parameter a default argument to
28538 match aggregate initialization behavior, even if we deduce the
28539 length of the pack separately to more than we have initializers. */
28540 arg = build_constructor (init_list_type_node, NULL);
28541 /* if ei is of array type and xi is a braced-init-list or string literal,
28542 Ti is an rvalue reference to the declared type of ei */
28543 STRIP_ANY_LOCATION_WRAPPER (val);
28544 if (TREE_CODE (ftype) == ARRAY_TYPE
28545 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28546 || TREE_CODE (val) == STRING_CST))
28547 {
28548 if (TREE_CODE (val) == STRING_CST)
28549 ftype = cp_build_qualified_type
28550 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28551 ftype = (cp_build_reference_type
28552 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28553 }
28554 list = tree_cons (arg, ftype, list);
28555 }
28556
28557 return list;
28558 }
28559
28560 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28561
28562 static bool
28563 is_spec_or_derived (tree etype, tree tmpl)
28564 {
28565 if (!etype || !CLASS_TYPE_P (etype))
28566 return false;
28567
28568 tree type = TREE_TYPE (tmpl);
28569 tree tparms = (INNERMOST_TEMPLATE_PARMS
28570 (DECL_TEMPLATE_PARMS (tmpl)));
28571 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28572 int err = unify (tparms, targs, type, etype,
28573 UNIFY_ALLOW_DERIVED, /*explain*/false);
28574 ggc_free (targs);
28575 return !err;
28576 }
28577
28578 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28579 INIT. */
28580
28581 static tree
28582 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28583 {
28584 if (cxx_dialect < cxx20)
28585 return NULL_TREE;
28586
28587 if (init == NULL_TREE)
28588 return NULL_TREE;
28589
28590 tree type = TREE_TYPE (tmpl);
28591 if (!CP_AGGREGATE_TYPE_P (type))
28592 return NULL_TREE;
28593
28594 /* No aggregate candidate for copy-initialization. */
28595 if (args->length() == 1)
28596 {
28597 tree val = (*args)[0];
28598 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28599 return NULL_TREE;
28600 }
28601
28602 /* If we encounter a problem, we just won't add the candidate. */
28603 tsubst_flags_t complain = tf_none;
28604
28605 tree parms = NULL_TREE;
28606 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28607 {
28608 init = reshape_init (type, init, complain);
28609 if (init == error_mark_node)
28610 return NULL_TREE;
28611 parms = collect_ctor_idx_types (init, parms);
28612 }
28613 else if (TREE_CODE (init) == TREE_LIST)
28614 {
28615 int len = list_length (init);
28616 for (tree field = TYPE_FIELDS (type);
28617 len;
28618 --len, field = DECL_CHAIN (field))
28619 {
28620 field = next_initializable_field (field);
28621 if (!field)
28622 return NULL_TREE;
28623 tree ftype = finish_decltype_type (field, true, complain);
28624 parms = tree_cons (NULL_TREE, ftype, parms);
28625 }
28626 }
28627 else
28628 /* Aggregate initialization doesn't apply to an initializer expression. */
28629 return NULL_TREE;
28630
28631 if (parms)
28632 {
28633 tree last = parms;
28634 parms = nreverse (parms);
28635 TREE_CHAIN (last) = void_list_node;
28636 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28637 return guide;
28638 }
28639
28640 return NULL_TREE;
28641 }
28642
28643 /* UGUIDES are the deduction guides for the underlying template of alias
28644 template TMPL; adjust them to be deduction guides for TMPL. */
28645
28646 static tree
28647 alias_ctad_tweaks (tree tmpl, tree uguides)
28648 {
28649 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28650 class type (9.2.8.2) where the template-name names an alias template A,
28651 the defining-type-id of A must be of the form
28652
28653 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28654
28655 as specified in 9.2.8.2. The guides of A are the set of functions or
28656 function templates formed as follows. For each function or function
28657 template f in the guides of the template named by the simple-template-id
28658 of the defining-type-id, the template arguments of the return type of f
28659 are deduced from the defining-type-id of A according to the process in
28660 13.10.2.5 with the exception that deduction does not fail if not all
28661 template arguments are deduced. Let g denote the result of substituting
28662 these deductions into f. If substitution succeeds, form a function or
28663 function template f' with the following properties and add it to the set
28664 of guides of A:
28665
28666 * The function type of f' is the function type of g.
28667
28668 * If f is a function template, f' is a function template whose template
28669 parameter list consists of all the template parameters of A (including
28670 their default template arguments) that appear in the above deductions or
28671 (recursively) in their default template arguments, followed by the
28672 template parameters of f that were not deduced (including their default
28673 template arguments), otherwise f' is not a function template.
28674
28675 * The associated constraints (13.5.2) are the conjunction of the
28676 associated constraints of g and a constraint that is satisfied if and only
28677 if the arguments of A are deducible (see below) from the return type.
28678
28679 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28680 be so as well.
28681
28682 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28683 considered to be so as well.
28684
28685 * The explicit-specifier of f' is the explicit-specifier of g (if
28686 any). */
28687
28688 /* This implementation differs from the above in two significant ways:
28689
28690 1) We include all template parameters of A, not just some.
28691 2) The added constraint is same_type instead of deducible.
28692
28693 I believe that while it's probably possible to construct a testcase that
28694 behaves differently with this simplification, it should have the same
28695 effect for real uses. Including all template parameters means that we
28696 deduce all parameters of A when resolving the call, so when we're in the
28697 constraint we don't need to deduce them again, we can just check whether
28698 the deduction produced the desired result. */
28699
28700 tsubst_flags_t complain = tf_warning_or_error;
28701 tree atype = TREE_TYPE (tmpl);
28702 tree aguides = NULL_TREE;
28703 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28704 unsigned natparms = TREE_VEC_LENGTH (atparms);
28705 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28706 for (ovl_iterator iter (uguides); iter; ++iter)
28707 {
28708 tree f = *iter;
28709 tree in_decl = f;
28710 location_t loc = DECL_SOURCE_LOCATION (f);
28711 tree ret = TREE_TYPE (TREE_TYPE (f));
28712 tree fprime = f;
28713 if (TREE_CODE (f) == TEMPLATE_DECL)
28714 {
28715 processing_template_decl_sentinel ptds (/*reset*/false);
28716 ++processing_template_decl;
28717
28718 /* Deduce template arguments for f from the type-id of A. */
28719 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28720 unsigned len = TREE_VEC_LENGTH (ftparms);
28721 tree targs = make_tree_vec (len);
28722 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28723 gcc_assert (!err);
28724
28725 /* The number of parms for f' is the number of parms for A plus
28726 non-deduced parms of f. */
28727 unsigned ndlen = 0;
28728 unsigned j;
28729 for (unsigned i = 0; i < len; ++i)
28730 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28731 ++ndlen;
28732 tree gtparms = make_tree_vec (natparms + ndlen);
28733
28734 /* First copy over the parms of A. */
28735 for (j = 0; j < natparms; ++j)
28736 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28737 /* Now rewrite the non-deduced parms of f. */
28738 for (unsigned i = 0; ndlen && i < len; ++i)
28739 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28740 {
28741 --ndlen;
28742 unsigned index = j++;
28743 unsigned level = 1;
28744 tree oldlist = TREE_VEC_ELT (ftparms, i);
28745 tree list = rewrite_tparm_list (oldlist, index, level,
28746 targs, i, complain);
28747 TREE_VEC_ELT (gtparms, index) = list;
28748 }
28749 gtparms = build_tree_list (size_one_node, gtparms);
28750
28751 /* Substitute the deduced arguments plus the rewritten template
28752 parameters into f to get g. This covers the type, copyness,
28753 guideness, and explicit-specifier. */
28754 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28755 if (g == error_mark_node)
28756 return error_mark_node;
28757 DECL_USE_TEMPLATE (g) = 0;
28758 fprime = build_template_decl (g, gtparms, false);
28759 DECL_TEMPLATE_RESULT (fprime) = g;
28760 TREE_TYPE (fprime) = TREE_TYPE (g);
28761 tree gtargs = template_parms_to_args (gtparms);
28762 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28763 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28764
28765 /* Substitute the associated constraints. */
28766 tree ci = get_constraints (f);
28767 if (ci)
28768 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28769 if (ci == error_mark_node)
28770 return error_mark_node;
28771
28772 /* Add a constraint that the return type matches the instantiation of
28773 A with the same template arguments. */
28774 ret = TREE_TYPE (TREE_TYPE (fprime));
28775 if (!same_type_p (atype, ret)
28776 /* FIXME this should mean they don't compare as equivalent. */
28777 || dependent_alias_template_spec_p (atype, nt_opaque))
28778 {
28779 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28780 ci = append_constraint (ci, same);
28781 }
28782
28783 if (ci)
28784 {
28785 remove_constraints (fprime);
28786 set_constraints (fprime, ci);
28787 }
28788 }
28789 else
28790 {
28791 /* For a non-template deduction guide, if the arguments of A aren't
28792 deducible from the return type, don't add the candidate. */
28793 tree targs = make_tree_vec (natparms);
28794 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28795 for (unsigned i = 0; !err && i < natparms; ++i)
28796 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28797 err = true;
28798 if (err)
28799 continue;
28800 }
28801
28802 aguides = lookup_add (fprime, aguides);
28803 }
28804
28805 return aguides;
28806 }
28807
28808 /* Return artificial deduction guides built from the constructors of class
28809 template TMPL. */
28810
28811 static tree
28812 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28813 {
28814 tree type = TREE_TYPE (tmpl);
28815 tree outer_args = NULL_TREE;
28816 if (DECL_CLASS_SCOPE_P (tmpl)
28817 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28818 {
28819 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28820 type = TREE_TYPE (most_general_template (tmpl));
28821 }
28822
28823 tree cands = NULL_TREE;
28824
28825 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28826 {
28827 /* Skip inherited constructors. */
28828 if (iter.using_p ())
28829 continue;
28830
28831 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28832 cands = lookup_add (guide, cands);
28833 }
28834
28835 /* Add implicit default constructor deduction guide. */
28836 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28837 {
28838 tree guide = build_deduction_guide (type, type, outer_args,
28839 complain);
28840 cands = lookup_add (guide, cands);
28841 }
28842
28843 /* Add copy guide. */
28844 {
28845 tree gtype = build_reference_type (type);
28846 tree guide = build_deduction_guide (type, gtype, outer_args,
28847 complain);
28848 cands = lookup_add (guide, cands);
28849 }
28850
28851 return cands;
28852 }
28853
28854 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28855
28856 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28857 aggregate candidate is added separately because it depends on the
28858 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28859 guide. */
28860
28861 static tree
28862 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
28863 {
28864 tree guides = NULL_TREE;
28865 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28866 {
28867 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28868 tree tinfo = get_template_info (under);
28869 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
28870 complain);
28871 }
28872 else
28873 {
28874 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28875 dguide_name (tmpl),
28876 LOOK_want::NORMAL, /*complain*/false);
28877 if (guides == error_mark_node)
28878 guides = NULL_TREE;
28879 else
28880 any_dguides_p = true;
28881 }
28882
28883 /* Cache the deduction guides for a template. We also remember the result of
28884 lookup, and rebuild everything if it changes; should be very rare. */
28885 tree_pair_p cache = NULL;
28886 if (tree_pair_p &r
28887 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28888 {
28889 cache = r;
28890 if (cache->purpose == guides)
28891 return cache->value;
28892 }
28893 else
28894 {
28895 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28896 cache->purpose = guides;
28897 }
28898
28899 tree cands = NULL_TREE;
28900 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28901 cands = alias_ctad_tweaks (tmpl, guides);
28902 else
28903 {
28904 cands = ctor_deduction_guides_for (tmpl, complain);
28905 for (ovl_iterator it (guides); it; ++it)
28906 cands = lookup_add (*it, cands);
28907 }
28908
28909 cache->value = cands;
28910 return cands;
28911 }
28912
28913 /* Return whether TMPL is a (class template argument-) deducible template. */
28914
28915 bool
28916 ctad_template_p (tree tmpl)
28917 {
28918 /* A deducible template is either a class template or is an alias template
28919 whose defining-type-id is of the form
28920
28921 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28922
28923 where the nested-name-specifier (if any) is non-dependent and the
28924 template-name of the simple-template-id names a deducible template. */
28925
28926 if (DECL_CLASS_TEMPLATE_P (tmpl)
28927 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28928 return true;
28929 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28930 return false;
28931 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28932 if (tree tinfo = get_template_info (orig))
28933 return ctad_template_p (TI_TEMPLATE (tinfo));
28934 return false;
28935 }
28936
28937 /* Deduce template arguments for the class template placeholder PTYPE for
28938 template TMPL based on the initializer INIT, and return the resulting
28939 type. */
28940
28941 static tree
28942 do_class_deduction (tree ptype, tree tmpl, tree init,
28943 int flags, tsubst_flags_t complain)
28944 {
28945 /* We should have handled this in the caller. */
28946 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28947 return ptype;
28948
28949 /* Look through alias templates that just rename another template. */
28950 tmpl = get_underlying_template (tmpl);
28951 if (!ctad_template_p (tmpl))
28952 {
28953 if (complain & tf_error)
28954 error ("non-deducible template %qT used without template arguments", tmpl);
28955 return error_mark_node;
28956 }
28957 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28958 {
28959 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28960 if (complain & tf_warning_or_error)
28961 pedwarn (input_location, 0, "alias template deduction only available "
28962 "with %<-std=c++20%> or %<-std=gnu++20%>");
28963 }
28964
28965 if (init && TREE_TYPE (init) == ptype)
28966 /* Using the template parm as its own argument. */
28967 return ptype;
28968
28969 tree type = TREE_TYPE (tmpl);
28970
28971 bool try_list_ctor = false;
28972 bool list_init_p = false;
28973
28974 releasing_vec rv_args = NULL;
28975 vec<tree,va_gc> *&args = *&rv_args;
28976 if (init == NULL_TREE)
28977 args = make_tree_vector ();
28978 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28979 {
28980 list_init_p = true;
28981 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28982 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28983 {
28984 /* As an exception, the first phase in 16.3.1.7 (considering the
28985 initializer list as a single argument) is omitted if the
28986 initializer list consists of a single expression of type cv U,
28987 where U is a specialization of C or a class derived from a
28988 specialization of C. */
28989 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28990 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28991 try_list_ctor = false;
28992 }
28993 if (try_list_ctor || is_std_init_list (type))
28994 args = make_tree_vector_single (init);
28995 else
28996 args = make_tree_vector_from_ctor (init);
28997 }
28998 else if (TREE_CODE (init) == TREE_LIST)
28999 args = make_tree_vector_from_list (init);
29000 else
29001 args = make_tree_vector_single (init);
29002
29003 /* Do this now to avoid problems with erroneous args later on. */
29004 args = resolve_args (args, complain);
29005 if (args == NULL)
29006 return error_mark_node;
29007
29008 bool any_dguides_p = false;
29009 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29010 if (cands == error_mark_node)
29011 return error_mark_node;
29012
29013 /* Prune explicit deduction guides in copy-initialization context (but
29014 not copy-list-initialization). */
29015 bool elided = false;
29016 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29017 {
29018 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29019 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29020 elided = true;
29021
29022 if (elided)
29023 {
29024 /* Found a nonconverting guide, prune the candidates. */
29025 tree pruned = NULL_TREE;
29026 for (lkp_iterator iter (cands); iter; ++iter)
29027 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29028 pruned = lookup_add (*iter, pruned);
29029
29030 cands = pruned;
29031 }
29032 }
29033
29034 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29035 cands = lookup_add (guide, cands);
29036
29037 tree call = error_mark_node;
29038
29039 /* If this is list-initialization and the class has a list constructor, first
29040 try deducing from the list as a single argument, as [over.match.list]. */
29041 tree list_cands = NULL_TREE;
29042 if (try_list_ctor && cands)
29043 for (lkp_iterator iter (cands); iter; ++iter)
29044 {
29045 tree dg = *iter;
29046 if (is_list_ctor (dg))
29047 list_cands = lookup_add (dg, list_cands);
29048 }
29049 if (list_cands)
29050 {
29051 ++cp_unevaluated_operand;
29052 call = build_new_function_call (list_cands, &args, tf_decltype);
29053 --cp_unevaluated_operand;
29054
29055 if (call == error_mark_node)
29056 {
29057 /* That didn't work, now try treating the list as a sequence of
29058 arguments. */
29059 release_tree_vector (args);
29060 args = make_tree_vector_from_ctor (init);
29061 }
29062 }
29063
29064 if (elided && !cands)
29065 {
29066 error ("cannot deduce template arguments for copy-initialization"
29067 " of %qT, as it has no non-explicit deduction guides or "
29068 "user-declared constructors", type);
29069 return error_mark_node;
29070 }
29071 else if (!cands && call == error_mark_node)
29072 {
29073 error ("cannot deduce template arguments of %qT, as it has no viable "
29074 "deduction guides", type);
29075 return error_mark_node;
29076 }
29077
29078 if (call == error_mark_node)
29079 {
29080 ++cp_unevaluated_operand;
29081 call = build_new_function_call (cands, &args, tf_decltype);
29082 --cp_unevaluated_operand;
29083 }
29084
29085 if (call == error_mark_node)
29086 {
29087 if (complain & tf_warning_or_error)
29088 {
29089 error ("class template argument deduction failed:");
29090
29091 ++cp_unevaluated_operand;
29092 call = build_new_function_call (cands, &args,
29093 complain | tf_decltype);
29094 --cp_unevaluated_operand;
29095
29096 if (elided)
29097 inform (input_location, "explicit deduction guides not considered "
29098 "for copy-initialization");
29099 }
29100 return error_mark_node;
29101 }
29102 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29103 constructor is chosen, the initialization is ill-formed. */
29104 else if (flags & LOOKUP_ONLYCONVERTING)
29105 {
29106 tree fndecl = cp_get_callee_fndecl_nofold (call);
29107 if (fndecl && DECL_NONCONVERTING_P (fndecl))
29108 {
29109 if (complain & tf_warning_or_error)
29110 {
29111 // TODO: Pass down location from cp_finish_decl.
29112 error ("class template argument deduction for %qT failed: "
29113 "explicit deduction guide selected in "
29114 "copy-list-initialization", type);
29115 inform (DECL_SOURCE_LOCATION (fndecl),
29116 "explicit deduction guide declared here");
29117
29118 }
29119 return error_mark_node;
29120 }
29121 }
29122
29123 /* If CTAD succeeded but the type doesn't have any explicit deduction
29124 guides, this deduction might not be what the user intended. */
29125 if (call != error_mark_node && !any_dguides_p)
29126 {
29127 tree fndecl = cp_get_callee_fndecl_nofold (call);
29128 if (fndecl != NULL_TREE
29129 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29130 || global_dc->dc_warn_system_headers)
29131 && warning (OPT_Wctad_maybe_unsupported,
29132 "%qT may not intend to support class template argument "
29133 "deduction", type))
29134 inform (input_location, "add a deduction guide to suppress this "
29135 "warning");
29136 }
29137
29138 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29139 }
29140
29141 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29142 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29143 The CONTEXT determines the context in which auto deduction is performed
29144 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29145 OUTER_TARGS are used during template argument deduction
29146 (context == adc_unify) to properly substitute the result, and is ignored
29147 in other contexts.
29148
29149 For partial-concept-ids, extra args may be appended to the list of deduced
29150 template arguments prior to determining constraint satisfaction. */
29151
29152 tree
29153 do_auto_deduction (tree type, tree init, tree auto_node,
29154 tsubst_flags_t complain, auto_deduction_context context,
29155 tree outer_targs, int flags)
29156 {
29157 tree targs;
29158
29159 if (init == error_mark_node)
29160 return error_mark_node;
29161
29162 if (init && type_dependent_expression_p (init)
29163 && context != adc_unify)
29164 /* Defining a subset of type-dependent expressions that we can deduce
29165 from ahead of time isn't worth the trouble. */
29166 return type;
29167
29168 /* Similarly, we can't deduce from another undeduced decl. */
29169 if (init && undeduced_auto_decl (init))
29170 return type;
29171
29172 /* We may be doing a partial substitution, but we still want to replace
29173 auto_node. */
29174 complain &= ~tf_partial;
29175
29176 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29177 /* C++17 class template argument deduction. */
29178 return do_class_deduction (type, tmpl, init, flags, complain);
29179
29180 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29181 /* Nothing we can do with this, even in deduction context. */
29182 return type;
29183
29184 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29185 with either a new invented type template parameter U or, if the
29186 initializer is a braced-init-list (8.5.4), with
29187 std::initializer_list<U>. */
29188 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29189 {
29190 if (!DIRECT_LIST_INIT_P (init))
29191 type = listify_autos (type, auto_node);
29192 else if (CONSTRUCTOR_NELTS (init) == 1)
29193 init = CONSTRUCTOR_ELT (init, 0)->value;
29194 else
29195 {
29196 if (complain & tf_warning_or_error)
29197 {
29198 if (permerror (input_location, "direct-list-initialization of "
29199 "%<auto%> requires exactly one element"))
29200 inform (input_location,
29201 "for deduction to %<std::initializer_list%>, use copy-"
29202 "list-initialization (i.e. add %<=%> before the %<{%>)");
29203 }
29204 type = listify_autos (type, auto_node);
29205 }
29206 }
29207
29208 if (type == error_mark_node)
29209 return error_mark_node;
29210
29211 init = resolve_nondeduced_context (init, complain);
29212
29213 if (context == adc_decomp_type
29214 && auto_node == type
29215 && init != error_mark_node
29216 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29217 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29218 and initializer has array type, deduce cv-qualified array type. */
29219 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29220 complain);
29221 else if (AUTO_IS_DECLTYPE (auto_node))
29222 {
29223 tree stripped_init = tree_strip_any_location_wrapper (init);
29224 bool id = (DECL_P (stripped_init)
29225 || ((TREE_CODE (init) == COMPONENT_REF
29226 || TREE_CODE (init) == SCOPE_REF)
29227 && !REF_PARENTHESIZED_P (init)));
29228 targs = make_tree_vec (1);
29229 TREE_VEC_ELT (targs, 0)
29230 = finish_decltype_type (init, id, tf_warning_or_error);
29231 if (type != auto_node)
29232 {
29233 if (complain & tf_error)
29234 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29235 return error_mark_node;
29236 }
29237 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29238 {
29239 if (complain & tf_error)
29240 error ("%<decltype(auto)%> cannot be cv-qualified");
29241 return error_mark_node;
29242 }
29243 }
29244 else
29245 {
29246 if (error_operand_p (init))
29247 return error_mark_node;
29248
29249 tree parms = build_tree_list (NULL_TREE, type);
29250 tree tparms;
29251
29252 if (flag_concepts)
29253 tparms = extract_autos (type);
29254 else
29255 {
29256 tparms = make_tree_vec (1);
29257 TREE_VEC_ELT (tparms, 0)
29258 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29259 }
29260
29261 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29262 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29263 DEDUCE_CALL,
29264 NULL, /*explain_p=*/false);
29265 if (val > 0)
29266 {
29267 if (processing_template_decl)
29268 /* Try again at instantiation time. */
29269 return type;
29270 if (type && type != error_mark_node
29271 && (complain & tf_error))
29272 /* If type is error_mark_node a diagnostic must have been
29273 emitted by now. Also, having a mention to '<type error>'
29274 in the diagnostic is not really useful to the user. */
29275 {
29276 if (cfun
29277 && FNDECL_USED_AUTO (current_function_decl)
29278 && (auto_node
29279 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29280 && LAMBDA_FUNCTION_P (current_function_decl))
29281 error ("unable to deduce lambda return type from %qE", init);
29282 else
29283 error ("unable to deduce %qT from %qE", type, init);
29284 type_unification_real (tparms, targs, parms, &init, 1, 0,
29285 DEDUCE_CALL,
29286 NULL, /*explain_p=*/true);
29287 }
29288 return error_mark_node;
29289 }
29290 }
29291
29292 /* Check any placeholder constraints against the deduced type. */
29293 if (flag_concepts && !processing_template_decl)
29294 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29295 {
29296 /* Use the deduced type to check the associated constraints. If we
29297 have a partial-concept-id, rebuild the argument list so that
29298 we check using the extra arguments. */
29299 check = unpack_concept_check (check);
29300 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29301 tree cdecl = TREE_OPERAND (check, 0);
29302 if (OVL_P (cdecl))
29303 cdecl = OVL_FIRST (cdecl);
29304 tree cargs = TREE_OPERAND (check, 1);
29305 if (TREE_VEC_LENGTH (cargs) > 1)
29306 {
29307 cargs = copy_node (cargs);
29308 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29309 }
29310 else
29311 cargs = targs;
29312
29313 /* Rebuild the check using the deduced arguments. */
29314 check = build_concept_check (cdecl, cargs, tf_none);
29315
29316 if (!constraints_satisfied_p (check))
29317 {
29318 if (complain & tf_warning_or_error)
29319 {
29320 auto_diagnostic_group d;
29321 switch (context)
29322 {
29323 case adc_unspecified:
29324 case adc_unify:
29325 error("placeholder constraints not satisfied");
29326 break;
29327 case adc_variable_type:
29328 case adc_decomp_type:
29329 error ("deduced initializer does not satisfy "
29330 "placeholder constraints");
29331 break;
29332 case adc_return_type:
29333 error ("deduced return type does not satisfy "
29334 "placeholder constraints");
29335 break;
29336 case adc_requirement:
29337 error ("deduced expression type does not satisfy "
29338 "placeholder constraints");
29339 break;
29340 }
29341 diagnose_constraints (input_location, check, targs);
29342 }
29343 return error_mark_node;
29344 }
29345 }
29346
29347 if (processing_template_decl && context != adc_unify)
29348 outer_targs = current_template_args ();
29349 targs = add_to_template_args (outer_targs, targs);
29350 return tsubst (type, targs, complain, NULL_TREE);
29351 }
29352
29353 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29354 result. */
29355
29356 tree
29357 splice_late_return_type (tree type, tree late_return_type)
29358 {
29359 if (late_return_type)
29360 {
29361 gcc_assert (is_auto (type) || seen_error ());
29362 return late_return_type;
29363 }
29364
29365 if (tree *auto_node = find_type_usage (&type, is_auto))
29366 {
29367 tree idx = get_template_parm_index (*auto_node);
29368 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29369 {
29370 /* In an abbreviated function template we didn't know we were dealing
29371 with a function template when we saw the auto return type, so update
29372 it to have the correct level. */
29373 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29374 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29375 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29376 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29377 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29378 *auto_node = new_auto;
29379 }
29380 }
29381 return type;
29382 }
29383
29384 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29385 'decltype(auto)' or a deduced class template. */
29386
29387 bool
29388 is_auto (const_tree type)
29389 {
29390 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29391 && (TYPE_IDENTIFIER (type) == auto_identifier
29392 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29393 return true;
29394 else
29395 return false;
29396 }
29397
29398 /* for_each_template_parm callback for type_uses_auto. */
29399
29400 int
29401 is_auto_r (tree tp, void */*data*/)
29402 {
29403 return is_auto (tp);
29404 }
29405
29406 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29407 a use of `auto'. Returns NULL_TREE otherwise. */
29408
29409 tree
29410 type_uses_auto (tree type)
29411 {
29412 if (type == NULL_TREE)
29413 return NULL_TREE;
29414 else if (flag_concepts)
29415 {
29416 /* The Concepts TS allows multiple autos in one type-specifier; just
29417 return the first one we find, do_auto_deduction will collect all of
29418 them. */
29419 if (uses_template_parms (type))
29420 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29421 /*visited*/NULL, /*nondeduced*/false);
29422 else
29423 return NULL_TREE;
29424 }
29425 else if (tree *tp = find_type_usage (&type, is_auto))
29426 return *tp;
29427 else
29428 return NULL_TREE;
29429 }
29430
29431 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29432 concepts are enabled, auto is acceptable in template arguments, but
29433 only when TEMPL identifies a template class. Return TRUE if any
29434 such errors were reported. */
29435
29436 bool
29437 check_auto_in_tmpl_args (tree tmpl, tree args)
29438 {
29439 /* If there were previous errors, nevermind. */
29440 if (!args || TREE_CODE (args) != TREE_VEC)
29441 return false;
29442
29443 /* If TMPL is an identifier, we're parsing and we can't tell yet
29444 whether TMPL is supposed to be a type, a function or a variable.
29445 We'll only be able to tell during template substitution, so we
29446 expect to be called again then. If concepts are enabled and we
29447 know we have a type, we're ok. */
29448 if (flag_concepts
29449 && (identifier_p (tmpl)
29450 || (DECL_P (tmpl)
29451 && (DECL_TYPE_TEMPLATE_P (tmpl)
29452 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29453 return false;
29454
29455 /* Quickly search for any occurrences of auto; usually there won't
29456 be any, and then we'll avoid allocating the vector. */
29457 if (!type_uses_auto (args))
29458 return false;
29459
29460 bool errors = false;
29461
29462 tree vec = extract_autos (args);
29463 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29464 {
29465 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29466 error_at (DECL_SOURCE_LOCATION (xauto),
29467 "invalid use of %qT in template argument", xauto);
29468 errors = true;
29469 }
29470
29471 return errors;
29472 }
29473
29474 /* Recursively walk over && expressions searching for EXPR. Return a reference
29475 to that expression. */
29476
29477 static tree *find_template_requirement (tree *t, tree key)
29478 {
29479 if (*t == key)
29480 return t;
29481 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29482 {
29483 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29484 return p;
29485 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29486 return p;
29487 }
29488 return 0;
29489 }
29490
29491 /* Convert the generic type parameters in PARM that match the types given in the
29492 range [START_IDX, END_IDX) from the current_template_parms into generic type
29493 packs. */
29494
29495 tree
29496 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29497 {
29498 tree current = current_template_parms;
29499 int depth = TMPL_PARMS_DEPTH (current);
29500 current = INNERMOST_TEMPLATE_PARMS (current);
29501 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29502
29503 for (int i = 0; i < start_idx; ++i)
29504 TREE_VEC_ELT (replacement, i)
29505 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29506
29507 for (int i = start_idx; i < end_idx; ++i)
29508 {
29509 /* Create a distinct parameter pack type from the current parm and add it
29510 to the replacement args to tsubst below into the generic function
29511 parameter. */
29512 tree node = TREE_VEC_ELT (current, i);
29513 tree o = TREE_TYPE (TREE_VALUE (node));
29514 tree t = copy_type (o);
29515 TEMPLATE_TYPE_PARM_INDEX (t)
29516 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29517 t, 0, 0, tf_none);
29518 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29519 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29520 TYPE_MAIN_VARIANT (t) = t;
29521 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29522 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29523 TREE_VEC_ELT (replacement, i) = t;
29524
29525 /* Replace the current template parameter with new pack. */
29526 TREE_VALUE (node) = TREE_CHAIN (t);
29527
29528 /* Surgically adjust the associated constraint of adjusted parameter
29529 and it's corresponding contribution to the current template
29530 requirements. */
29531 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29532 {
29533 tree id = unpack_concept_check (constr);
29534 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29535 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29536 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29537
29538 /* If there was a constraint, we also need to replace that in
29539 the template requirements, which we've already built. */
29540 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29541 reqs = find_template_requirement (reqs, constr);
29542 *reqs = fold;
29543 }
29544 }
29545
29546 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29547 TREE_VEC_ELT (replacement, i)
29548 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29549
29550 /* If there are more levels then build up the replacement with the outer
29551 template parms. */
29552 if (depth > 1)
29553 replacement = add_to_template_args (template_parms_to_args
29554 (TREE_CHAIN (current_template_parms)),
29555 replacement);
29556
29557 return tsubst (parm, replacement, tf_none, NULL_TREE);
29558 }
29559
29560 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29561 0..N-1. */
29562
29563 void
29564 declare_integer_pack (void)
29565 {
29566 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29567 build_function_type_list (integer_type_node,
29568 integer_type_node,
29569 NULL_TREE),
29570 NULL_TREE, ECF_CONST);
29571 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29572 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29573 CP_BUILT_IN_INTEGER_PACK);
29574 }
29575
29576 /* Set up the hash tables for template instantiations. */
29577
29578 void
29579 init_template_processing (void)
29580 {
29581 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29582 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29583
29584 if (cxx_dialect >= cxx11)
29585 declare_integer_pack ();
29586 }
29587
29588 /* Print stats about the template hash tables for -fstats. */
29589
29590 void
29591 print_template_statistics (void)
29592 {
29593 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29594 "%f collisions\n", (long) decl_specializations->size (),
29595 (long) decl_specializations->elements (),
29596 decl_specializations->collisions ());
29597 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29598 "%f collisions\n", (long) type_specializations->size (),
29599 (long) type_specializations->elements (),
29600 type_specializations->collisions ());
29601 }
29602
29603 #if CHECKING_P
29604
29605 namespace selftest {
29606
29607 /* Verify that build_non_dependent_expr () works, for various expressions,
29608 and that location wrappers don't affect the results. */
29609
29610 static void
29611 test_build_non_dependent_expr ()
29612 {
29613 location_t loc = BUILTINS_LOCATION;
29614
29615 /* Verify constants, without and with location wrappers. */
29616 tree int_cst = build_int_cst (integer_type_node, 42);
29617 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29618
29619 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29620 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29621 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29622
29623 tree string_lit = build_string (4, "foo");
29624 TREE_TYPE (string_lit) = char_array_type_node;
29625 string_lit = fix_string_type (string_lit);
29626 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29627
29628 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29629 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29630 ASSERT_EQ (wrapped_string_lit,
29631 build_non_dependent_expr (wrapped_string_lit));
29632 }
29633
29634 /* Verify that type_dependent_expression_p () works correctly, even
29635 in the presence of location wrapper nodes. */
29636
29637 static void
29638 test_type_dependent_expression_p ()
29639 {
29640 location_t loc = BUILTINS_LOCATION;
29641
29642 tree name = get_identifier ("foo");
29643
29644 /* If no templates are involved, nothing is type-dependent. */
29645 gcc_assert (!processing_template_decl);
29646 ASSERT_FALSE (type_dependent_expression_p (name));
29647
29648 ++processing_template_decl;
29649
29650 /* Within a template, an unresolved name is always type-dependent. */
29651 ASSERT_TRUE (type_dependent_expression_p (name));
29652
29653 /* Ensure it copes with NULL_TREE and errors. */
29654 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29655 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29656
29657 /* A USING_DECL in a template should be type-dependent, even if wrapped
29658 with a location wrapper (PR c++/83799). */
29659 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29660 TREE_TYPE (using_decl) = integer_type_node;
29661 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29662 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29663 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29664 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29665
29666 --processing_template_decl;
29667 }
29668
29669 /* Run all of the selftests within this file. */
29670
29671 void
29672 cp_pt_c_tests ()
29673 {
29674 test_build_non_dependent_expr ();
29675 test_type_dependent_expression_p ();
29676 }
29677
29678 } // namespace selftest
29679
29680 #endif /* #if CHECKING_P */
29681
29682 #include "gt-cp-pt.h"