c++: Break out actual instantiation from instantiate_decl
[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
231 /* Make the current scope suitable for access checking when we are
232 processing T. T can be FUNCTION_DECL for instantiated function
233 template, VAR_DECL for static member variable, or TYPE_DECL for
234 alias template (needed by instantiate_decl). */
235
236 void
237 push_access_scope (tree t)
238 {
239 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
240 || TREE_CODE (t) == TYPE_DECL);
241
242 if (DECL_FRIEND_CONTEXT (t))
243 push_nested_class (DECL_FRIEND_CONTEXT (t));
244 else if (DECL_CLASS_SCOPE_P (t))
245 push_nested_class (DECL_CONTEXT (t));
246 else
247 push_to_top_level ();
248
249 if (TREE_CODE (t) == FUNCTION_DECL)
250 {
251 vec_safe_push (saved_access_scope, current_function_decl);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 current_function_decl = saved_access_scope->pop();
264
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
269 }
270
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
275
276 tree
277 finish_member_template_decl (tree decl)
278 {
279 if (decl == error_mark_node)
280 return error_mark_node;
281
282 gcc_assert (DECL_P (decl));
283
284 if (TREE_CODE (decl) == TYPE_DECL)
285 {
286 tree type;
287
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
294 {
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
298 }
299 return NULL_TREE;
300 }
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error_at (DECL_SOURCE_LOCATION (decl),
303 "data member %qD cannot be a member template", decl);
304 else if (DECL_TEMPLATE_INFO (decl))
305 {
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 {
308 check_member_template (DECL_TI_TEMPLATE (decl));
309 return DECL_TI_TEMPLATE (decl);
310 }
311 else
312 return decl;
313 }
314 else
315 error_at (DECL_SOURCE_LOCATION (decl),
316 "invalid member template declaration %qD", decl);
317
318 return error_mark_node;
319 }
320
321 /* Create a template info node. */
322
323 tree
324 build_template_info (tree template_decl, tree template_args)
325 {
326 tree result = make_node (TEMPLATE_INFO);
327 TI_TEMPLATE (result) = template_decl;
328 TI_ARGS (result) = template_args;
329 return result;
330 }
331
332 /* Return the template info node corresponding to T, whatever T is. */
333
334 tree
335 get_template_info (const_tree t)
336 {
337 tree tinfo = NULL_TREE;
338
339 if (!t || t == error_mark_node)
340 return NULL;
341
342 if (TREE_CODE (t) == NAMESPACE_DECL
343 || TREE_CODE (t) == PARM_DECL)
344 return NULL;
345
346 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
347 tinfo = DECL_TEMPLATE_INFO (t);
348
349 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
350 t = TREE_TYPE (t);
351
352 if (OVERLOAD_TYPE_P (t))
353 tinfo = TYPE_TEMPLATE_INFO (t);
354 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
355 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
356
357 return tinfo;
358 }
359
360 /* Returns the template nesting level of the indicated class TYPE.
361
362 For example, in:
363 template <class T>
364 struct A
365 {
366 template <class U>
367 struct B {};
368 };
369
370 A<T>::B<U> has depth two, while A<T> has depth one.
371 Both A<T>::B<int> and A<int>::B<U> have depth one, if
372 they are instantiations, not specializations.
373
374 This function is guaranteed to return 0 if passed NULL_TREE so
375 that, for example, `template_class_depth (current_class_type)' is
376 always safe. */
377
378 int
379 template_class_depth (tree type)
380 {
381 int depth;
382
383 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
384 {
385 tree tinfo = get_template_info (type);
386
387 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
388 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
389 ++depth;
390
391 if (DECL_P (type))
392 {
393 if (tree fctx = DECL_FRIEND_CONTEXT (type))
394 type = fctx;
395 else
396 type = CP_DECL_CONTEXT (type);
397 }
398 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
399 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
400 else
401 type = CP_TYPE_CONTEXT (type);
402 }
403
404 return depth;
405 }
406
407 /* Return TRUE if NODE instantiates a template that has arguments of
408 its own, be it directly a primary template or indirectly through a
409 partial specializations. */
410 static bool
411 instantiates_primary_template_p (tree node)
412 {
413 tree tinfo = get_template_info (node);
414 if (!tinfo)
415 return false;
416
417 tree tmpl = TI_TEMPLATE (tinfo);
418 if (PRIMARY_TEMPLATE_P (tmpl))
419 return true;
420
421 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
422 return false;
423
424 /* So now we know we have a specialization, but it could be a full
425 or a partial specialization. To tell which, compare the depth of
426 its template arguments with those of its context. */
427
428 tree ctxt = DECL_CONTEXT (tmpl);
429 tree ctinfo = get_template_info (ctxt);
430 if (!ctinfo)
431 return true;
432
433 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
434 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
435 }
436
437 /* Subroutine of maybe_begin_member_template_processing.
438 Returns true if processing DECL needs us to push template parms. */
439
440 static bool
441 inline_needs_template_parms (tree decl, bool nsdmi)
442 {
443 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
444 return false;
445
446 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
447 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
448 }
449
450 /* Subroutine of maybe_begin_member_template_processing.
451 Push the template parms in PARMS, starting from LEVELS steps into the
452 chain, and ending at the beginning, since template parms are listed
453 innermost first. */
454
455 static void
456 push_inline_template_parms_recursive (tree parmlist, int levels)
457 {
458 tree parms = TREE_VALUE (parmlist);
459 int i;
460
461 if (levels > 1)
462 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
463
464 ++processing_template_decl;
465 current_template_parms
466 = tree_cons (size_int (processing_template_decl),
467 parms, current_template_parms);
468 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
469
470 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
471 NULL);
472 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
473 {
474 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
475
476 if (error_operand_p (parm))
477 continue;
478
479 gcc_assert (DECL_P (parm));
480
481 switch (TREE_CODE (parm))
482 {
483 case TYPE_DECL:
484 case TEMPLATE_DECL:
485 pushdecl (parm);
486 break;
487
488 case PARM_DECL:
489 /* Push the CONST_DECL. */
490 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
491 break;
492
493 default:
494 gcc_unreachable ();
495 }
496 }
497 }
498
499 /* Restore the template parameter context for a member template, a
500 friend template defined in a class definition, or a non-template
501 member of template class. */
502
503 void
504 maybe_begin_member_template_processing (tree decl)
505 {
506 tree parms;
507 int levels = 0;
508 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
509
510 if (nsdmi)
511 {
512 tree ctx = DECL_CONTEXT (decl);
513 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
514 /* Disregard full specializations (c++/60999). */
515 && uses_template_parms (ctx)
516 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
517 }
518
519 if (inline_needs_template_parms (decl, nsdmi))
520 {
521 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
522 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
523
524 if (DECL_TEMPLATE_SPECIALIZATION (decl))
525 {
526 --levels;
527 parms = TREE_CHAIN (parms);
528 }
529
530 push_inline_template_parms_recursive (parms, levels);
531 }
532
533 /* Remember how many levels of template parameters we pushed so that
534 we can pop them later. */
535 inline_parm_levels.safe_push (levels);
536 }
537
538 /* Undo the effects of maybe_begin_member_template_processing. */
539
540 void
541 maybe_end_member_template_processing (void)
542 {
543 int i;
544 int last;
545
546 if (inline_parm_levels.length () == 0)
547 return;
548
549 last = inline_parm_levels.pop ();
550 for (i = 0; i < last; ++i)
551 {
552 --processing_template_decl;
553 current_template_parms = TREE_CHAIN (current_template_parms);
554 poplevel (0, 0, 0);
555 }
556 }
557
558 /* Return a new template argument vector which contains all of ARGS,
559 but has as its innermost set of arguments the EXTRA_ARGS. */
560
561 static tree
562 add_to_template_args (tree args, tree extra_args)
563 {
564 tree new_args;
565 int extra_depth;
566 int i;
567 int j;
568
569 if (args == NULL_TREE || extra_args == error_mark_node)
570 return extra_args;
571
572 extra_depth = TMPL_ARGS_DEPTH (extra_args);
573 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
574
575 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
576 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
577
578 for (j = 1; j <= extra_depth; ++j, ++i)
579 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
580
581 return new_args;
582 }
583
584 /* Like add_to_template_args, but only the outermost ARGS are added to
585 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
586 (EXTRA_ARGS) levels are added. This function is used to combine
587 the template arguments from a partial instantiation with the
588 template arguments used to attain the full instantiation from the
589 partial instantiation.
590
591 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
592
593 tree
594 add_outermost_template_args (tree args, tree extra_args)
595 {
596 tree new_args;
597
598 if (!args)
599 return extra_args;
600 if (TREE_CODE (args) == TEMPLATE_DECL)
601 {
602 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
603 args = TI_ARGS (ti);
604 }
605
606 /* If there are more levels of EXTRA_ARGS than there are ARGS,
607 something very fishy is going on. */
608 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
609
610 /* If *all* the new arguments will be the EXTRA_ARGS, just return
611 them. */
612 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
613 return extra_args;
614
615 /* For the moment, we make ARGS look like it contains fewer levels. */
616 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
617
618 new_args = add_to_template_args (args, extra_args);
619
620 /* Now, we restore ARGS to its full dimensions. */
621 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
622
623 return new_args;
624 }
625
626 /* Return the N levels of innermost template arguments from the ARGS. */
627
628 tree
629 get_innermost_template_args (tree args, int n)
630 {
631 tree new_args;
632 int extra_levels;
633 int i;
634
635 gcc_assert (n >= 0);
636
637 /* If N is 1, just return the innermost set of template arguments. */
638 if (n == 1)
639 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
640
641 /* If we're not removing anything, just return the arguments we were
642 given. */
643 extra_levels = TMPL_ARGS_DEPTH (args) - n;
644 gcc_assert (extra_levels >= 0);
645 if (extra_levels == 0)
646 return args;
647
648 /* Make a new set of arguments, not containing the outer arguments. */
649 new_args = make_tree_vec (n);
650 for (i = 1; i <= n; ++i)
651 SET_TMPL_ARGS_LEVEL (new_args, i,
652 TMPL_ARGS_LEVEL (args, i + extra_levels));
653
654 return new_args;
655 }
656
657 /* The inverse of get_innermost_template_args: Return all but the innermost
658 EXTRA_LEVELS levels of template arguments from the ARGS. */
659
660 static tree
661 strip_innermost_template_args (tree args, int extra_levels)
662 {
663 tree new_args;
664 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
665 int i;
666
667 gcc_assert (n >= 0);
668
669 /* If N is 1, just return the outermost set of template arguments. */
670 if (n == 1)
671 return TMPL_ARGS_LEVEL (args, 1);
672
673 /* If we're not removing anything, just return the arguments we were
674 given. */
675 gcc_assert (extra_levels >= 0);
676 if (extra_levels == 0)
677 return args;
678
679 /* Make a new set of arguments, not containing the inner arguments. */
680 new_args = make_tree_vec (n);
681 for (i = 1; i <= n; ++i)
682 SET_TMPL_ARGS_LEVEL (new_args, i,
683 TMPL_ARGS_LEVEL (args, i));
684
685 return new_args;
686 }
687
688 /* We've got a template header coming up; push to a new level for storing
689 the parms. */
690
691 void
692 begin_template_parm_list (void)
693 {
694 /* We use a non-tag-transparent scope here, which causes pushtag to
695 put tags in this scope, rather than in the enclosing class or
696 namespace scope. This is the right thing, since we want
697 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
698 global template class, push_template_decl handles putting the
699 TEMPLATE_DECL into top-level scope. For a nested template class,
700 e.g.:
701
702 template <class T> struct S1 {
703 template <class T> struct S2 {};
704 };
705
706 pushtag contains special code to insert the TEMPLATE_DECL for S2
707 at the right scope. */
708 begin_scope (sk_template_parms, NULL);
709 ++processing_template_decl;
710 ++processing_template_parmlist;
711 note_template_header (0);
712
713 /* Add a dummy parameter level while we process the parameter list. */
714 current_template_parms
715 = tree_cons (size_int (processing_template_decl),
716 make_tree_vec (0),
717 current_template_parms);
718 }
719
720 /* This routine is called when a specialization is declared. If it is
721 invalid to declare a specialization here, an error is reported and
722 false is returned, otherwise this routine will return true. */
723
724 static bool
725 check_specialization_scope (void)
726 {
727 tree scope = current_scope ();
728
729 /* [temp.expl.spec]
730
731 An explicit specialization shall be declared in the namespace of
732 which the template is a member, or, for member templates, in the
733 namespace of which the enclosing class or enclosing class
734 template is a member. An explicit specialization of a member
735 function, member class or static data member of a class template
736 shall be declared in the namespace of which the class template
737 is a member. */
738 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
739 {
740 error ("explicit specialization in non-namespace scope %qD", scope);
741 return false;
742 }
743
744 /* [temp.expl.spec]
745
746 In an explicit specialization declaration for a member of a class
747 template or a member template that appears in namespace scope,
748 the member template and some of its enclosing class templates may
749 remain unspecialized, except that the declaration shall not
750 explicitly specialize a class member template if its enclosing
751 class templates are not explicitly specialized as well. */
752 if (current_template_parms)
753 {
754 error ("enclosing class templates are not explicitly specialized");
755 return false;
756 }
757
758 return true;
759 }
760
761 /* We've just seen template <>. */
762
763 bool
764 begin_specialization (void)
765 {
766 begin_scope (sk_template_spec, NULL);
767 note_template_header (1);
768 return check_specialization_scope ();
769 }
770
771 /* Called at then end of processing a declaration preceded by
772 template<>. */
773
774 void
775 end_specialization (void)
776 {
777 finish_scope ();
778 reset_specialization ();
779 }
780
781 /* Any template <>'s that we have seen thus far are not referring to a
782 function specialization. */
783
784 void
785 reset_specialization (void)
786 {
787 processing_specialization = 0;
788 template_header_count = 0;
789 }
790
791 /* We've just seen a template header. If SPECIALIZATION is nonzero,
792 it was of the form template <>. */
793
794 static void
795 note_template_header (int specialization)
796 {
797 processing_specialization = specialization;
798 template_header_count++;
799 }
800
801 /* We're beginning an explicit instantiation. */
802
803 void
804 begin_explicit_instantiation (void)
805 {
806 gcc_assert (!processing_explicit_instantiation);
807 processing_explicit_instantiation = true;
808 }
809
810
811 void
812 end_explicit_instantiation (void)
813 {
814 gcc_assert (processing_explicit_instantiation);
815 processing_explicit_instantiation = false;
816 }
817
818 /* An explicit specialization or partial specialization of TMPL is being
819 declared. Check that the namespace in which the specialization is
820 occurring is permissible. Returns false iff it is invalid to
821 specialize TMPL in the current namespace. */
822
823 static bool
824 check_specialization_namespace (tree tmpl)
825 {
826 tree tpl_ns = decl_namespace_context (tmpl);
827
828 /* [tmpl.expl.spec]
829
830 An explicit specialization shall be declared in a namespace enclosing the
831 specialized template. An explicit specialization whose declarator-id is
832 not qualified shall be declared in the nearest enclosing namespace of the
833 template, or, if the namespace is inline (7.3.1), any namespace from its
834 enclosing namespace set. */
835 if (current_scope() != DECL_CONTEXT (tmpl)
836 && !at_namespace_scope_p ())
837 {
838 error ("specialization of %qD must appear at namespace scope", tmpl);
839 return false;
840 }
841
842 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
843 /* Same or enclosing namespace. */
844 return true;
845 else
846 {
847 auto_diagnostic_group d;
848 if (permerror (input_location,
849 "specialization of %qD in different namespace", tmpl))
850 inform (DECL_SOURCE_LOCATION (tmpl),
851 " from definition of %q#D", tmpl);
852 return false;
853 }
854 }
855
856 /* SPEC is an explicit instantiation. Check that it is valid to
857 perform this explicit instantiation in the current namespace. */
858
859 static void
860 check_explicit_instantiation_namespace (tree spec)
861 {
862 tree ns;
863
864 /* DR 275: An explicit instantiation shall appear in an enclosing
865 namespace of its template. */
866 ns = decl_namespace_context (spec);
867 if (!is_nested_namespace (current_namespace, ns))
868 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
869 "(which does not enclose namespace %qD)",
870 spec, current_namespace, ns);
871 }
872
873 /* Returns the type of a template specialization only if that
874 specialization needs to be defined. Otherwise (e.g., if the type has
875 already been defined), the function returns NULL_TREE. */
876
877 static tree
878 maybe_new_partial_specialization (tree type)
879 {
880 /* An implicit instantiation of an incomplete type implies
881 the definition of a new class template.
882
883 template<typename T>
884 struct S;
885
886 template<typename T>
887 struct S<T*>;
888
889 Here, S<T*> is an implicit instantiation of S whose type
890 is incomplete. */
891 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
892 return type;
893
894 /* It can also be the case that TYPE is a completed specialization.
895 Continuing the previous example, suppose we also declare:
896
897 template<typename T>
898 requires Integral<T>
899 struct S<T*>;
900
901 Here, S<T*> refers to the specialization S<T*> defined
902 above. However, we need to differentiate definitions because
903 we intend to define a new partial specialization. In this case,
904 we rely on the fact that the constraints are different for
905 this declaration than that above.
906
907 Note that we also get here for injected class names and
908 late-parsed template definitions. We must ensure that we
909 do not create new type declarations for those cases. */
910 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
911 {
912 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
913 tree args = CLASSTYPE_TI_ARGS (type);
914
915 /* If there are no template parameters, this cannot be a new
916 partial template specialization? */
917 if (!current_template_parms)
918 return NULL_TREE;
919
920 /* The injected-class-name is not a new partial specialization. */
921 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
922 return NULL_TREE;
923
924 /* If the constraints are not the same as those of the primary
925 then, we can probably create a new specialization. */
926 tree type_constr = current_template_constraints ();
927
928 if (type == TREE_TYPE (tmpl))
929 {
930 tree main_constr = get_constraints (tmpl);
931 if (equivalent_constraints (type_constr, main_constr))
932 return NULL_TREE;
933 }
934
935 /* Also, if there's a pre-existing specialization with matching
936 constraints, then this also isn't new. */
937 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
938 while (specs)
939 {
940 tree spec_tmpl = TREE_VALUE (specs);
941 tree spec_args = TREE_PURPOSE (specs);
942 tree spec_constr = get_constraints (spec_tmpl);
943 if (comp_template_args (args, spec_args)
944 && equivalent_constraints (type_constr, spec_constr))
945 return NULL_TREE;
946 specs = TREE_CHAIN (specs);
947 }
948
949 /* Create a new type node (and corresponding type decl)
950 for the newly declared specialization. */
951 tree t = make_class_type (TREE_CODE (type));
952 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
953 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
954
955 /* We only need a separate type node for storing the definition of this
956 partial specialization; uses of S<T*> are unconstrained, so all are
957 equivalent. So keep TYPE_CANONICAL the same. */
958 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
959
960 /* Build the corresponding type decl. */
961 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
962 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
963 DECL_SOURCE_LOCATION (d) = input_location;
964 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
965 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
966
967 return t;
968 }
969
970 return NULL_TREE;
971 }
972
973 /* The TYPE is being declared. If it is a template type, that means it
974 is a partial specialization. Do appropriate error-checking. */
975
976 tree
977 maybe_process_partial_specialization (tree type)
978 {
979 tree context;
980
981 if (type == error_mark_node)
982 return error_mark_node;
983
984 /* A lambda that appears in specialization context is not itself a
985 specialization. */
986 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
987 return type;
988
989 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
990 {
991 error ("name of class shadows template template parameter %qD",
992 TYPE_NAME (type));
993 return error_mark_node;
994 }
995
996 context = TYPE_CONTEXT (type);
997
998 if (TYPE_ALIAS_P (type))
999 {
1000 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1001
1002 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1003 error ("specialization of alias template %qD",
1004 TI_TEMPLATE (tinfo));
1005 else
1006 error ("explicit specialization of non-template %qT", type);
1007 return error_mark_node;
1008 }
1009 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1010 {
1011 /* This is for ordinary explicit specialization and partial
1012 specialization of a template class such as:
1013
1014 template <> class C<int>;
1015
1016 or:
1017
1018 template <class T> class C<T*>;
1019
1020 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1021
1022 if (tree t = maybe_new_partial_specialization (type))
1023 {
1024 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1025 && !at_namespace_scope_p ())
1026 return error_mark_node;
1027 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1028 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1029 if (processing_template_decl)
1030 {
1031 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1032 if (decl == error_mark_node)
1033 return error_mark_node;
1034 return TREE_TYPE (decl);
1035 }
1036 }
1037 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1038 error ("specialization of %qT after instantiation", type);
1039 else if (errorcount && !processing_specialization
1040 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1041 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1042 /* Trying to define a specialization either without a template<> header
1043 or in an inappropriate place. We've already given an error, so just
1044 bail now so we don't actually define the specialization. */
1045 return error_mark_node;
1046 }
1047 else if (CLASS_TYPE_P (type)
1048 && !CLASSTYPE_USE_TEMPLATE (type)
1049 && CLASSTYPE_TEMPLATE_INFO (type)
1050 && context && CLASS_TYPE_P (context)
1051 && CLASSTYPE_TEMPLATE_INFO (context))
1052 {
1053 /* This is for an explicit specialization of member class
1054 template according to [temp.expl.spec/18]:
1055
1056 template <> template <class U> class C<int>::D;
1057
1058 The context `C<int>' must be an implicit instantiation.
1059 Otherwise this is just a member class template declared
1060 earlier like:
1061
1062 template <> class C<int> { template <class U> class D; };
1063 template <> template <class U> class C<int>::D;
1064
1065 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1066 while in the second case, `C<int>::D' is a primary template
1067 and `C<T>::D' may not exist. */
1068
1069 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1070 && !COMPLETE_TYPE_P (type))
1071 {
1072 tree t;
1073 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1074
1075 if (current_namespace
1076 != decl_namespace_context (tmpl))
1077 {
1078 if (permerror (input_location,
1079 "specialization of %qD in different namespace",
1080 type))
1081 inform (DECL_SOURCE_LOCATION (tmpl),
1082 "from definition of %q#D", tmpl);
1083 }
1084
1085 /* Check for invalid specialization after instantiation:
1086
1087 template <> template <> class C<int>::D<int>;
1088 template <> template <class U> class C<int>::D; */
1089
1090 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1091 t; t = TREE_CHAIN (t))
1092 {
1093 tree inst = TREE_VALUE (t);
1094 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1095 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1096 {
1097 /* We already have a full specialization of this partial
1098 instantiation, or a full specialization has been
1099 looked up but not instantiated. Reassign it to the
1100 new member specialization template. */
1101 spec_entry elt;
1102 spec_entry *entry;
1103
1104 elt.tmpl = most_general_template (tmpl);
1105 elt.args = CLASSTYPE_TI_ARGS (inst);
1106 elt.spec = inst;
1107
1108 type_specializations->remove_elt (&elt);
1109
1110 elt.tmpl = tmpl;
1111 CLASSTYPE_TI_ARGS (inst)
1112 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1113
1114 spec_entry **slot
1115 = type_specializations->find_slot (&elt, INSERT);
1116 entry = ggc_alloc<spec_entry> ();
1117 *entry = elt;
1118 *slot = entry;
1119 }
1120 else
1121 /* But if we've had an implicit instantiation, that's a
1122 problem ([temp.expl.spec]/6). */
1123 error ("specialization %qT after instantiation %qT",
1124 type, inst);
1125 }
1126
1127 /* Mark TYPE as a specialization. And as a result, we only
1128 have one level of template argument for the innermost
1129 class template. */
1130 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1131 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1132 CLASSTYPE_TI_ARGS (type)
1133 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1134 }
1135 }
1136 else if (processing_specialization)
1137 {
1138 /* Someday C++0x may allow for enum template specialization. */
1139 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1140 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1141 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1142 "of %qD not allowed by ISO C++", type);
1143 else
1144 {
1145 error ("explicit specialization of non-template %qT", type);
1146 return error_mark_node;
1147 }
1148 }
1149
1150 return type;
1151 }
1152
1153 /* Returns nonzero if we can optimize the retrieval of specializations
1154 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1155 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1156
1157 static inline bool
1158 optimize_specialization_lookup_p (tree tmpl)
1159 {
1160 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1161 && DECL_CLASS_SCOPE_P (tmpl)
1162 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1163 parameter. */
1164 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1165 /* The optimized lookup depends on the fact that the
1166 template arguments for the member function template apply
1167 purely to the containing class, which is not true if the
1168 containing class is an explicit or partial
1169 specialization. */
1170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1171 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1172 && !DECL_CONV_FN_P (tmpl)
1173 /* It is possible to have a template that is not a member
1174 template and is not a member of a template class:
1175
1176 template <typename T>
1177 struct S { friend A::f(); };
1178
1179 Here, the friend function is a template, but the context does
1180 not have template information. The optimized lookup relies
1181 on having ARGS be the template arguments for both the class
1182 and the function template. */
1183 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1184 }
1185
1186 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1187 gone through coerce_template_parms by now. */
1188
1189 static void
1190 verify_unstripped_args_1 (tree inner)
1191 {
1192 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1193 {
1194 tree arg = TREE_VEC_ELT (inner, i);
1195 if (TREE_CODE (arg) == TEMPLATE_DECL)
1196 /* OK */;
1197 else if (TYPE_P (arg))
1198 gcc_assert (strip_typedefs (arg, NULL) == arg);
1199 else if (ARGUMENT_PACK_P (arg))
1200 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1201 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1202 /* Allow typedefs on the type of a non-type argument, since a
1203 parameter can have them. */;
1204 else
1205 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1206 }
1207 }
1208
1209 static void
1210 verify_unstripped_args (tree args)
1211 {
1212 ++processing_template_decl;
1213 if (!any_dependent_template_arguments_p (args))
1214 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1215 --processing_template_decl;
1216 }
1217
1218 /* Retrieve the specialization (in the sense of [temp.spec] - a
1219 specialization is either an instantiation or an explicit
1220 specialization) of TMPL for the given template ARGS. If there is
1221 no such specialization, return NULL_TREE. The ARGS are a vector of
1222 arguments, or a vector of vectors of arguments, in the case of
1223 templates with more than one level of parameters.
1224
1225 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1226 then we search for a partial specialization matching ARGS. This
1227 parameter is ignored if TMPL is not a class template.
1228
1229 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1230 result is a NONTYPE_ARGUMENT_PACK. */
1231
1232 static tree
1233 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1234 {
1235 if (tmpl == NULL_TREE)
1236 return NULL_TREE;
1237
1238 if (args == error_mark_node)
1239 return NULL_TREE;
1240
1241 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1242 || TREE_CODE (tmpl) == FIELD_DECL);
1243
1244 /* There should be as many levels of arguments as there are
1245 levels of parameters. */
1246 gcc_assert (TMPL_ARGS_DEPTH (args)
1247 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1248 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1249 : template_class_depth (DECL_CONTEXT (tmpl))));
1250
1251 if (flag_checking)
1252 verify_unstripped_args (args);
1253
1254 /* Lambda functions in templates aren't instantiated normally, but through
1255 tsubst_lambda_expr. */
1256 if (lambda_fn_in_template_p (tmpl))
1257 return NULL_TREE;
1258
1259 if (optimize_specialization_lookup_p (tmpl))
1260 {
1261 /* The template arguments actually apply to the containing
1262 class. Find the class specialization with those
1263 arguments. */
1264 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1265 tree class_specialization
1266 = retrieve_specialization (class_template, args, 0);
1267 if (!class_specialization)
1268 return NULL_TREE;
1269
1270 /* Find the instance of TMPL. */
1271 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1272 for (ovl_iterator iter (fns); iter; ++iter)
1273 {
1274 tree fn = *iter;
1275 if (tree ti = get_template_info (fn))
1276 if (TI_TEMPLATE (ti) == tmpl
1277 /* using-declarations can bring in a different
1278 instantiation of tmpl as a member of a different
1279 instantiation of tmpl's class. We don't want those
1280 here. */
1281 && DECL_CONTEXT (fn) == class_specialization)
1282 return fn;
1283 }
1284 return NULL_TREE;
1285 }
1286 else
1287 {
1288 spec_entry *found;
1289 spec_entry elt;
1290 spec_hash_table *specializations;
1291
1292 elt.tmpl = tmpl;
1293 elt.args = args;
1294 elt.spec = NULL_TREE;
1295
1296 if (DECL_CLASS_TEMPLATE_P (tmpl))
1297 specializations = type_specializations;
1298 else
1299 specializations = decl_specializations;
1300
1301 if (hash == 0)
1302 hash = spec_hasher::hash (&elt);
1303 found = specializations->find_with_hash (&elt, hash);
1304 if (found)
1305 return found->spec;
1306 }
1307
1308 return NULL_TREE;
1309 }
1310
1311 /* Like retrieve_specialization, but for local declarations. */
1312
1313 tree
1314 retrieve_local_specialization (tree tmpl)
1315 {
1316 if (local_specializations == NULL)
1317 return NULL_TREE;
1318
1319 tree *slot = local_specializations->get (tmpl);
1320 return slot ? *slot : NULL_TREE;
1321 }
1322
1323 /* Returns nonzero iff DECL is a specialization of TMPL. */
1324
1325 int
1326 is_specialization_of (tree decl, tree tmpl)
1327 {
1328 tree t;
1329
1330 if (TREE_CODE (decl) == FUNCTION_DECL)
1331 {
1332 for (t = decl;
1333 t != NULL_TREE;
1334 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1335 if (t == tmpl)
1336 return 1;
1337 }
1338 else
1339 {
1340 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1341
1342 for (t = TREE_TYPE (decl);
1343 t != NULL_TREE;
1344 t = CLASSTYPE_USE_TEMPLATE (t)
1345 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1346 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1347 return 1;
1348 }
1349
1350 return 0;
1351 }
1352
1353 /* Returns nonzero iff DECL is a specialization of friend declaration
1354 FRIEND_DECL according to [temp.friend]. */
1355
1356 bool
1357 is_specialization_of_friend (tree decl, tree friend_decl)
1358 {
1359 bool need_template = true;
1360 int template_depth;
1361
1362 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1363 || TREE_CODE (decl) == TYPE_DECL);
1364
1365 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1366 of a template class, we want to check if DECL is a specialization
1367 if this. */
1368 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1369 && DECL_TEMPLATE_INFO (friend_decl)
1370 && !DECL_USE_TEMPLATE (friend_decl))
1371 {
1372 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1373 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1374 need_template = false;
1375 }
1376 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1377 && !PRIMARY_TEMPLATE_P (friend_decl))
1378 need_template = false;
1379
1380 /* There is nothing to do if this is not a template friend. */
1381 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1382 return false;
1383
1384 if (is_specialization_of (decl, friend_decl))
1385 return true;
1386
1387 /* [temp.friend/6]
1388 A member of a class template may be declared to be a friend of a
1389 non-template class. In this case, the corresponding member of
1390 every specialization of the class template is a friend of the
1391 class granting friendship.
1392
1393 For example, given a template friend declaration
1394
1395 template <class T> friend void A<T>::f();
1396
1397 the member function below is considered a friend
1398
1399 template <> struct A<int> {
1400 void f();
1401 };
1402
1403 For this type of template friend, TEMPLATE_DEPTH below will be
1404 nonzero. To determine if DECL is a friend of FRIEND, we first
1405 check if the enclosing class is a specialization of another. */
1406
1407 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1408 if (template_depth
1409 && DECL_CLASS_SCOPE_P (decl)
1410 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1411 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1412 {
1413 /* Next, we check the members themselves. In order to handle
1414 a few tricky cases, such as when FRIEND_DECL's are
1415
1416 template <class T> friend void A<T>::g(T t);
1417 template <class T> template <T t> friend void A<T>::h();
1418
1419 and DECL's are
1420
1421 void A<int>::g(int);
1422 template <int> void A<int>::h();
1423
1424 we need to figure out ARGS, the template arguments from
1425 the context of DECL. This is required for template substitution
1426 of `T' in the function parameter of `g' and template parameter
1427 of `h' in the above examples. Here ARGS corresponds to `int'. */
1428
1429 tree context = DECL_CONTEXT (decl);
1430 tree args = NULL_TREE;
1431 int current_depth = 0;
1432
1433 while (current_depth < template_depth)
1434 {
1435 if (CLASSTYPE_TEMPLATE_INFO (context))
1436 {
1437 if (current_depth == 0)
1438 args = TYPE_TI_ARGS (context);
1439 else
1440 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1441 current_depth++;
1442 }
1443 context = TYPE_CONTEXT (context);
1444 }
1445
1446 if (TREE_CODE (decl) == FUNCTION_DECL)
1447 {
1448 bool is_template;
1449 tree friend_type;
1450 tree decl_type;
1451 tree friend_args_type;
1452 tree decl_args_type;
1453
1454 /* Make sure that both DECL and FRIEND_DECL are templates or
1455 non-templates. */
1456 is_template = DECL_TEMPLATE_INFO (decl)
1457 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1458 if (need_template ^ is_template)
1459 return false;
1460 else if (is_template)
1461 {
1462 /* If both are templates, check template parameter list. */
1463 tree friend_parms
1464 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1465 args, tf_none);
1466 if (!comp_template_parms
1467 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1468 friend_parms))
1469 return false;
1470
1471 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1472 }
1473 else
1474 decl_type = TREE_TYPE (decl);
1475
1476 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1477 tf_none, NULL_TREE);
1478 if (friend_type == error_mark_node)
1479 return false;
1480
1481 /* Check if return types match. */
1482 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1483 return false;
1484
1485 /* Check if function parameter types match, ignoring the
1486 `this' parameter. */
1487 friend_args_type = TYPE_ARG_TYPES (friend_type);
1488 decl_args_type = TYPE_ARG_TYPES (decl_type);
1489 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1490 friend_args_type = TREE_CHAIN (friend_args_type);
1491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1492 decl_args_type = TREE_CHAIN (decl_args_type);
1493
1494 return compparms (decl_args_type, friend_args_type);
1495 }
1496 else
1497 {
1498 /* DECL is a TYPE_DECL */
1499 bool is_template;
1500 tree decl_type = TREE_TYPE (decl);
1501
1502 /* Make sure that both DECL and FRIEND_DECL are templates or
1503 non-templates. */
1504 is_template
1505 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1506 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1507
1508 if (need_template ^ is_template)
1509 return false;
1510 else if (is_template)
1511 {
1512 tree friend_parms;
1513 /* If both are templates, check the name of the two
1514 TEMPLATE_DECL's first because is_friend didn't. */
1515 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1516 != DECL_NAME (friend_decl))
1517 return false;
1518
1519 /* Now check template parameter list. */
1520 friend_parms
1521 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1522 args, tf_none);
1523 return comp_template_parms
1524 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1525 friend_parms);
1526 }
1527 else
1528 return (DECL_NAME (decl)
1529 == DECL_NAME (friend_decl));
1530 }
1531 }
1532 return false;
1533 }
1534
1535 /* Register the specialization SPEC as a specialization of TMPL with
1536 the indicated ARGS. IS_FRIEND indicates whether the specialization
1537 is actually just a friend declaration. ATTRLIST is the list of
1538 attributes that the specialization is declared with or NULL when
1539 it isn't. Returns SPEC, or an equivalent prior declaration, if
1540 available.
1541
1542 We also store instantiations of field packs in the hash table, even
1543 though they are not themselves templates, to make lookup easier. */
1544
1545 static tree
1546 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1547 hashval_t hash)
1548 {
1549 tree fn;
1550 spec_entry **slot = NULL;
1551 spec_entry elt;
1552
1553 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1554 || (TREE_CODE (tmpl) == FIELD_DECL
1555 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1556
1557 if (TREE_CODE (spec) == FUNCTION_DECL
1558 && uses_template_parms (DECL_TI_ARGS (spec)))
1559 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1560 register it; we want the corresponding TEMPLATE_DECL instead.
1561 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1562 the more obvious `uses_template_parms (spec)' to avoid problems
1563 with default function arguments. In particular, given
1564 something like this:
1565
1566 template <class T> void f(T t1, T t = T())
1567
1568 the default argument expression is not substituted for in an
1569 instantiation unless and until it is actually needed. */
1570 return spec;
1571
1572 if (optimize_specialization_lookup_p (tmpl))
1573 /* We don't put these specializations in the hash table, but we might
1574 want to give an error about a mismatch. */
1575 fn = retrieve_specialization (tmpl, args, 0);
1576 else
1577 {
1578 elt.tmpl = tmpl;
1579 elt.args = args;
1580 elt.spec = spec;
1581
1582 if (hash == 0)
1583 hash = spec_hasher::hash (&elt);
1584
1585 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1586 if (*slot)
1587 fn = (*slot)->spec;
1588 else
1589 fn = NULL_TREE;
1590 }
1591
1592 /* We can sometimes try to re-register a specialization that we've
1593 already got. In particular, regenerate_decl_from_template calls
1594 duplicate_decls which will update the specialization list. But,
1595 we'll still get called again here anyhow. It's more convenient
1596 to simply allow this than to try to prevent it. */
1597 if (fn == spec)
1598 return spec;
1599 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1600 {
1601 if (DECL_TEMPLATE_INSTANTIATION (fn))
1602 {
1603 if (DECL_ODR_USED (fn)
1604 || DECL_EXPLICIT_INSTANTIATION (fn))
1605 {
1606 error ("specialization of %qD after instantiation",
1607 fn);
1608 return error_mark_node;
1609 }
1610 else
1611 {
1612 tree clone;
1613 /* This situation should occur only if the first
1614 specialization is an implicit instantiation, the
1615 second is an explicit specialization, and the
1616 implicit instantiation has not yet been used. That
1617 situation can occur if we have implicitly
1618 instantiated a member function and then specialized
1619 it later.
1620
1621 We can also wind up here if a friend declaration that
1622 looked like an instantiation turns out to be a
1623 specialization:
1624
1625 template <class T> void foo(T);
1626 class S { friend void foo<>(int) };
1627 template <> void foo(int);
1628
1629 We transform the existing DECL in place so that any
1630 pointers to it become pointers to the updated
1631 declaration.
1632
1633 If there was a definition for the template, but not
1634 for the specialization, we want this to look as if
1635 there were no definition, and vice versa. */
1636 DECL_INITIAL (fn) = NULL_TREE;
1637 duplicate_decls (spec, fn, is_friend);
1638 /* The call to duplicate_decls will have applied
1639 [temp.expl.spec]:
1640
1641 An explicit specialization of a function template
1642 is inline only if it is explicitly declared to be,
1643 and independently of whether its function template
1644 is.
1645
1646 to the primary function; now copy the inline bits to
1647 the various clones. */
1648 FOR_EACH_CLONE (clone, fn)
1649 {
1650 DECL_DECLARED_INLINE_P (clone)
1651 = DECL_DECLARED_INLINE_P (fn);
1652 DECL_SOURCE_LOCATION (clone)
1653 = DECL_SOURCE_LOCATION (fn);
1654 DECL_DELETED_FN (clone)
1655 = DECL_DELETED_FN (fn);
1656 }
1657 check_specialization_namespace (tmpl);
1658
1659 return fn;
1660 }
1661 }
1662 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1663 {
1664 tree dd = duplicate_decls (spec, fn, is_friend);
1665 if (dd == error_mark_node)
1666 /* We've already complained in duplicate_decls. */
1667 return error_mark_node;
1668
1669 if (dd == NULL_TREE && DECL_INITIAL (spec))
1670 /* Dup decl failed, but this is a new definition. Set the
1671 line number so any errors match this new
1672 definition. */
1673 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1674
1675 return fn;
1676 }
1677 }
1678 else if (fn)
1679 return duplicate_decls (spec, fn, is_friend);
1680
1681 /* A specialization must be declared in the same namespace as the
1682 template it is specializing. */
1683 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1684 && !check_specialization_namespace (tmpl))
1685 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1686
1687 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1688 {
1689 spec_entry *entry = ggc_alloc<spec_entry> ();
1690 gcc_assert (tmpl && args && spec);
1691 *entry = elt;
1692 *slot = entry;
1693 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1694 && PRIMARY_TEMPLATE_P (tmpl)
1695 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1696 || variable_template_p (tmpl))
1697 /* If TMPL is a forward declaration of a template function, keep a list
1698 of all specializations in case we need to reassign them to a friend
1699 template later in tsubst_friend_function.
1700
1701 Also keep a list of all variable template instantiations so that
1702 process_partial_specialization can check whether a later partial
1703 specialization would have used it. */
1704 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1705 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1706 }
1707
1708 return spec;
1709 }
1710
1711 /* Returns true iff two spec_entry nodes are equivalent. */
1712
1713 int comparing_specializations;
1714
1715 bool
1716 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1717 {
1718 int equal;
1719
1720 ++comparing_specializations;
1721 equal = (e1->tmpl == e2->tmpl
1722 && comp_template_args (e1->args, e2->args));
1723 if (equal && flag_concepts
1724 /* tmpl could be a FIELD_DECL for a capture pack. */
1725 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1726 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1727 && uses_template_parms (e1->args))
1728 {
1729 /* Partial specializations of a variable template can be distinguished by
1730 constraints. */
1731 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1732 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1733 equal = equivalent_constraints (c1, c2);
1734 }
1735 --comparing_specializations;
1736
1737 return equal;
1738 }
1739
1740 /* Returns a hash for a template TMPL and template arguments ARGS. */
1741
1742 static hashval_t
1743 hash_tmpl_and_args (tree tmpl, tree args)
1744 {
1745 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1746 return iterative_hash_template_arg (args, val);
1747 }
1748
1749 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1750 ignoring SPEC. */
1751
1752 hashval_t
1753 spec_hasher::hash (spec_entry *e)
1754 {
1755 return hash_tmpl_and_args (e->tmpl, e->args);
1756 }
1757
1758 /* Recursively calculate a hash value for a template argument ARG, for use
1759 in the hash tables of template specializations. We must be
1760 careful to (at least) skip the same entities template_args_equal
1761 does. */
1762
1763 hashval_t
1764 iterative_hash_template_arg (tree arg, hashval_t val)
1765 {
1766 if (arg == NULL_TREE)
1767 return iterative_hash_object (arg, val);
1768
1769 if (!TYPE_P (arg))
1770 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1771 while (CONVERT_EXPR_P (arg)
1772 || TREE_CODE (arg) == NON_LVALUE_EXPR
1773 || class_nttp_const_wrapper_p (arg))
1774 arg = TREE_OPERAND (arg, 0);
1775
1776 enum tree_code code = TREE_CODE (arg);
1777
1778 val = iterative_hash_object (code, val);
1779
1780 switch (code)
1781 {
1782 case ARGUMENT_PACK_SELECT:
1783 gcc_unreachable ();
1784
1785 case ERROR_MARK:
1786 return val;
1787
1788 case IDENTIFIER_NODE:
1789 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1790
1791 case TREE_VEC:
1792 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1793 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1794 return val;
1795
1796 case TYPE_PACK_EXPANSION:
1797 case EXPR_PACK_EXPANSION:
1798 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1799 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1800
1801 case TYPE_ARGUMENT_PACK:
1802 case NONTYPE_ARGUMENT_PACK:
1803 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1804
1805 case TREE_LIST:
1806 for (; arg; arg = TREE_CHAIN (arg))
1807 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1808 return val;
1809
1810 case OVERLOAD:
1811 for (lkp_iterator iter (arg); iter; ++iter)
1812 val = iterative_hash_template_arg (*iter, val);
1813 return val;
1814
1815 case CONSTRUCTOR:
1816 {
1817 tree field, value;
1818 unsigned i;
1819 iterative_hash_template_arg (TREE_TYPE (arg), val);
1820 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1821 {
1822 val = iterative_hash_template_arg (field, val);
1823 val = iterative_hash_template_arg (value, val);
1824 }
1825 return val;
1826 }
1827
1828 case PARM_DECL:
1829 if (!DECL_ARTIFICIAL (arg))
1830 {
1831 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1832 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1833 }
1834 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1835
1836 case TARGET_EXPR:
1837 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1838
1839 case PTRMEM_CST:
1840 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1841 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1842
1843 case TEMPLATE_PARM_INDEX:
1844 val = iterative_hash_template_arg
1845 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1846 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1847 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1848
1849 case TRAIT_EXPR:
1850 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1851 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1852 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1853
1854 case BASELINK:
1855 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1856 val);
1857 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1858 val);
1859
1860 case MODOP_EXPR:
1861 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1862 code = TREE_CODE (TREE_OPERAND (arg, 1));
1863 val = iterative_hash_object (code, val);
1864 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1865
1866 case LAMBDA_EXPR:
1867 /* [temp.over.link] Two lambda-expressions are never considered
1868 equivalent.
1869
1870 So just hash the closure type. */
1871 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1872
1873 case CAST_EXPR:
1874 case IMPLICIT_CONV_EXPR:
1875 case STATIC_CAST_EXPR:
1876 case REINTERPRET_CAST_EXPR:
1877 case CONST_CAST_EXPR:
1878 case DYNAMIC_CAST_EXPR:
1879 case NEW_EXPR:
1880 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1881 /* Now hash operands as usual. */
1882 break;
1883
1884 case CALL_EXPR:
1885 {
1886 tree fn = CALL_EXPR_FN (arg);
1887 if (tree name = dependent_name (fn))
1888 {
1889 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1890 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1891 fn = name;
1892 }
1893 val = iterative_hash_template_arg (fn, val);
1894 call_expr_arg_iterator ai;
1895 for (tree x = first_call_expr_arg (arg, &ai); x;
1896 x = next_call_expr_arg (&ai))
1897 val = iterative_hash_template_arg (x, val);
1898 return val;
1899 }
1900
1901 default:
1902 break;
1903 }
1904
1905 char tclass = TREE_CODE_CLASS (code);
1906 switch (tclass)
1907 {
1908 case tcc_type:
1909 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1910 {
1911 // We want an alias specialization that survived strip_typedefs
1912 // to hash differently from its TYPE_CANONICAL, to avoid hash
1913 // collisions that compare as different in template_args_equal.
1914 // These could be dependent specializations that strip_typedefs
1915 // left alone, or untouched specializations because
1916 // coerce_template_parms returns the unconverted template
1917 // arguments if it sees incomplete argument packs.
1918 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1919 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1920 }
1921
1922 switch (TREE_CODE (arg))
1923 {
1924 case TEMPLATE_TEMPLATE_PARM:
1925 {
1926 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1927
1928 /* Do not recurse with TPI directly, as that is unbounded
1929 recursion. */
1930 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1931 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1932 }
1933 break;
1934
1935 case DECLTYPE_TYPE:
1936 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1937 break;
1938
1939 default:
1940 if (tree canonical = TYPE_CANONICAL (arg))
1941 val = iterative_hash_object (TYPE_HASH (canonical), val);
1942 break;
1943 }
1944
1945 return val;
1946
1947 case tcc_declaration:
1948 case tcc_constant:
1949 return iterative_hash_expr (arg, val);
1950
1951 default:
1952 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1953 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1954 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1955 return val;
1956 }
1957
1958 gcc_unreachable ();
1959 return 0;
1960 }
1961
1962 /* Unregister the specialization SPEC as a specialization of TMPL.
1963 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1964 if the SPEC was listed as a specialization of TMPL.
1965
1966 Note that SPEC has been ggc_freed, so we can't look inside it. */
1967
1968 bool
1969 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1970 {
1971 spec_entry *entry;
1972 spec_entry elt;
1973
1974 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1975 elt.args = TI_ARGS (tinfo);
1976 elt.spec = NULL_TREE;
1977
1978 entry = decl_specializations->find (&elt);
1979 if (entry != NULL)
1980 {
1981 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1982 gcc_assert (new_spec != NULL_TREE);
1983 entry->spec = new_spec;
1984 return 1;
1985 }
1986
1987 return 0;
1988 }
1989
1990 /* Like register_specialization, but for local declarations. We are
1991 registering SPEC, an instantiation of TMPL. */
1992
1993 void
1994 register_local_specialization (tree spec, tree tmpl)
1995 {
1996 gcc_assert (tmpl != spec);
1997 local_specializations->put (tmpl, spec);
1998 }
1999
2000 /* TYPE is a class type. Returns true if TYPE is an explicitly
2001 specialized class. */
2002
2003 bool
2004 explicit_class_specialization_p (tree type)
2005 {
2006 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2007 return false;
2008 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2009 }
2010
2011 /* Print the list of functions at FNS, going through all the overloads
2012 for each element of the list. Alternatively, FNS cannot be a
2013 TREE_LIST, in which case it will be printed together with all the
2014 overloads.
2015
2016 MORE and *STR should respectively be FALSE and NULL when the function
2017 is called from the outside. They are used internally on recursive
2018 calls. print_candidates manages the two parameters and leaves NULL
2019 in *STR when it ends. */
2020
2021 static void
2022 print_candidates_1 (tree fns, char **str, bool more = false)
2023 {
2024 if (TREE_CODE (fns) == TREE_LIST)
2025 for (; fns; fns = TREE_CHAIN (fns))
2026 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2027 else
2028 for (lkp_iterator iter (fns); iter;)
2029 {
2030 tree cand = *iter;
2031 ++iter;
2032
2033 const char *pfx = *str;
2034 if (!pfx)
2035 {
2036 if (more || iter)
2037 pfx = _("candidates are:");
2038 else
2039 pfx = _("candidate is:");
2040 *str = get_spaces (pfx);
2041 }
2042 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2043 }
2044 }
2045
2046 /* Print the list of candidate FNS in an error message. FNS can also
2047 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2048
2049 void
2050 print_candidates (tree fns)
2051 {
2052 char *str = NULL;
2053 print_candidates_1 (fns, &str);
2054 free (str);
2055 }
2056
2057 /* Get a (possibly) constrained template declaration for the
2058 purpose of ordering candidates. */
2059 static tree
2060 get_template_for_ordering (tree list)
2061 {
2062 gcc_assert (TREE_CODE (list) == TREE_LIST);
2063 tree f = TREE_VALUE (list);
2064 if (tree ti = DECL_TEMPLATE_INFO (f))
2065 return TI_TEMPLATE (ti);
2066 return f;
2067 }
2068
2069 /* Among candidates having the same signature, return the
2070 most constrained or NULL_TREE if there is no best candidate.
2071 If the signatures of candidates vary (e.g., template
2072 specialization vs. member function), then there can be no
2073 most constrained.
2074
2075 Note that we don't compare constraints on the functions
2076 themselves, but rather those of their templates. */
2077 static tree
2078 most_constrained_function (tree candidates)
2079 {
2080 // Try to find the best candidate in a first pass.
2081 tree champ = candidates;
2082 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2083 {
2084 int winner = more_constrained (get_template_for_ordering (champ),
2085 get_template_for_ordering (c));
2086 if (winner == -1)
2087 champ = c; // The candidate is more constrained
2088 else if (winner == 0)
2089 return NULL_TREE; // Neither is more constrained
2090 }
2091
2092 // Verify that the champ is better than previous candidates.
2093 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2094 if (!more_constrained (get_template_for_ordering (champ),
2095 get_template_for_ordering (c)))
2096 return NULL_TREE;
2097 }
2098
2099 return champ;
2100 }
2101
2102
2103 /* Returns the template (one of the functions given by TEMPLATE_ID)
2104 which can be specialized to match the indicated DECL with the
2105 explicit template args given in TEMPLATE_ID. The DECL may be
2106 NULL_TREE if none is available. In that case, the functions in
2107 TEMPLATE_ID are non-members.
2108
2109 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2110 specialization of a member template.
2111
2112 The TEMPLATE_COUNT is the number of references to qualifying
2113 template classes that appeared in the name of the function. See
2114 check_explicit_specialization for a more accurate description.
2115
2116 TSK indicates what kind of template declaration (if any) is being
2117 declared. TSK_TEMPLATE indicates that the declaration given by
2118 DECL, though a FUNCTION_DECL, has template parameters, and is
2119 therefore a template function.
2120
2121 The template args (those explicitly specified and those deduced)
2122 are output in a newly created vector *TARGS_OUT.
2123
2124 If it is impossible to determine the result, an error message is
2125 issued. The error_mark_node is returned to indicate failure. */
2126
2127 static tree
2128 determine_specialization (tree template_id,
2129 tree decl,
2130 tree* targs_out,
2131 int need_member_template,
2132 int template_count,
2133 tmpl_spec_kind tsk)
2134 {
2135 tree fns;
2136 tree targs;
2137 tree explicit_targs;
2138 tree candidates = NULL_TREE;
2139
2140 /* A TREE_LIST of templates of which DECL may be a specialization.
2141 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2142 corresponding TREE_PURPOSE is the set of template arguments that,
2143 when used to instantiate the template, would produce a function
2144 with the signature of DECL. */
2145 tree templates = NULL_TREE;
2146 int header_count;
2147 cp_binding_level *b;
2148
2149 *targs_out = NULL_TREE;
2150
2151 if (template_id == error_mark_node || decl == error_mark_node)
2152 return error_mark_node;
2153
2154 /* We shouldn't be specializing a member template of an
2155 unspecialized class template; we already gave an error in
2156 check_specialization_scope, now avoid crashing. */
2157 if (!VAR_P (decl)
2158 && template_count && DECL_CLASS_SCOPE_P (decl)
2159 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2160 {
2161 gcc_assert (errorcount);
2162 return error_mark_node;
2163 }
2164
2165 fns = TREE_OPERAND (template_id, 0);
2166 explicit_targs = TREE_OPERAND (template_id, 1);
2167
2168 if (fns == error_mark_node)
2169 return error_mark_node;
2170
2171 /* Check for baselinks. */
2172 if (BASELINK_P (fns))
2173 fns = BASELINK_FUNCTIONS (fns);
2174
2175 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2176 {
2177 error_at (DECL_SOURCE_LOCATION (decl),
2178 "%qD is not a function template", fns);
2179 return error_mark_node;
2180 }
2181 else if (VAR_P (decl) && !variable_template_p (fns))
2182 {
2183 error ("%qD is not a variable template", fns);
2184 return error_mark_node;
2185 }
2186
2187 /* Count the number of template headers specified for this
2188 specialization. */
2189 header_count = 0;
2190 for (b = current_binding_level;
2191 b->kind == sk_template_parms;
2192 b = b->level_chain)
2193 ++header_count;
2194
2195 tree orig_fns = fns;
2196
2197 if (variable_template_p (fns))
2198 {
2199 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2200 targs = coerce_template_parms (parms, explicit_targs, fns,
2201 tf_warning_or_error,
2202 /*req_all*/true, /*use_defarg*/true);
2203 if (targs != error_mark_node)
2204 templates = tree_cons (targs, fns, templates);
2205 }
2206 else for (lkp_iterator iter (fns); iter; ++iter)
2207 {
2208 tree fn = *iter;
2209
2210 if (TREE_CODE (fn) == TEMPLATE_DECL)
2211 {
2212 tree decl_arg_types;
2213 tree fn_arg_types;
2214 tree insttype;
2215
2216 /* In case of explicit specialization, we need to check if
2217 the number of template headers appearing in the specialization
2218 is correct. This is usually done in check_explicit_specialization,
2219 but the check done there cannot be exhaustive when specializing
2220 member functions. Consider the following code:
2221
2222 template <> void A<int>::f(int);
2223 template <> template <> void A<int>::f(int);
2224
2225 Assuming that A<int> is not itself an explicit specialization
2226 already, the first line specializes "f" which is a non-template
2227 member function, whilst the second line specializes "f" which
2228 is a template member function. So both lines are syntactically
2229 correct, and check_explicit_specialization does not reject
2230 them.
2231
2232 Here, we can do better, as we are matching the specialization
2233 against the declarations. We count the number of template
2234 headers, and we check if they match TEMPLATE_COUNT + 1
2235 (TEMPLATE_COUNT is the number of qualifying template classes,
2236 plus there must be another header for the member template
2237 itself).
2238
2239 Notice that if header_count is zero, this is not a
2240 specialization but rather a template instantiation, so there
2241 is no check we can perform here. */
2242 if (header_count && header_count != template_count + 1)
2243 continue;
2244
2245 /* Check that the number of template arguments at the
2246 innermost level for DECL is the same as for FN. */
2247 if (current_binding_level->kind == sk_template_parms
2248 && !current_binding_level->explicit_spec_p
2249 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2250 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2251 (current_template_parms))))
2252 continue;
2253
2254 /* DECL might be a specialization of FN. */
2255 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2256 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2257
2258 /* For a non-static member function, we need to make sure
2259 that the const qualification is the same. Since
2260 get_bindings does not try to merge the "this" parameter,
2261 we must do the comparison explicitly. */
2262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2263 {
2264 if (!same_type_p (TREE_VALUE (fn_arg_types),
2265 TREE_VALUE (decl_arg_types)))
2266 continue;
2267
2268 /* And the ref-qualification. */
2269 if (type_memfn_rqual (TREE_TYPE (decl))
2270 != type_memfn_rqual (TREE_TYPE (fn)))
2271 continue;
2272 }
2273
2274 /* Skip the "this" parameter and, for constructors of
2275 classes with virtual bases, the VTT parameter. A
2276 full specialization of a constructor will have a VTT
2277 parameter, but a template never will. */
2278 decl_arg_types
2279 = skip_artificial_parms_for (decl, decl_arg_types);
2280 fn_arg_types
2281 = skip_artificial_parms_for (fn, fn_arg_types);
2282
2283 /* Function templates cannot be specializations; there are
2284 no partial specializations of functions. Therefore, if
2285 the type of DECL does not match FN, there is no
2286 match.
2287
2288 Note that it should never be the case that we have both
2289 candidates added here, and for regular member functions
2290 below. */
2291 if (tsk == tsk_template)
2292 {
2293 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2294 current_template_parms))
2295 continue;
2296 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2297 TREE_TYPE (TREE_TYPE (fn))))
2298 continue;
2299 if (!compparms (fn_arg_types, decl_arg_types))
2300 continue;
2301
2302 tree freq = get_trailing_function_requirements (fn);
2303 tree dreq = get_trailing_function_requirements (decl);
2304 if (!freq != !dreq)
2305 continue;
2306 if (freq)
2307 {
2308 tree fargs = DECL_TI_ARGS (fn);
2309 tsubst_flags_t complain = tf_none;
2310 freq = tsubst_constraint (freq, fargs, complain, fn);
2311 if (!cp_tree_equal (freq, dreq))
2312 continue;
2313 }
2314
2315 candidates = tree_cons (NULL_TREE, fn, candidates);
2316 continue;
2317 }
2318
2319 /* See whether this function might be a specialization of this
2320 template. Suppress access control because we might be trying
2321 to make this specialization a friend, and we have already done
2322 access control for the declaration of the specialization. */
2323 push_deferring_access_checks (dk_no_check);
2324 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2325 pop_deferring_access_checks ();
2326
2327 if (!targs)
2328 /* We cannot deduce template arguments that when used to
2329 specialize TMPL will produce DECL. */
2330 continue;
2331
2332 if (uses_template_parms (targs))
2333 /* We deduced something involving 'auto', which isn't a valid
2334 template argument. */
2335 continue;
2336
2337 /* Remove, from the set of candidates, all those functions
2338 whose constraints are not satisfied. */
2339 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2340 continue;
2341
2342 // Then, try to form the new function type.
2343 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2344 if (insttype == error_mark_node)
2345 continue;
2346 fn_arg_types
2347 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2348 if (!compparms (fn_arg_types, decl_arg_types))
2349 continue;
2350
2351 /* Save this template, and the arguments deduced. */
2352 templates = tree_cons (targs, fn, templates);
2353 }
2354 else if (need_member_template)
2355 /* FN is an ordinary member function, and we need a
2356 specialization of a member template. */
2357 ;
2358 else if (TREE_CODE (fn) != FUNCTION_DECL)
2359 /* We can get IDENTIFIER_NODEs here in certain erroneous
2360 cases. */
2361 ;
2362 else if (!DECL_FUNCTION_MEMBER_P (fn))
2363 /* This is just an ordinary non-member function. Nothing can
2364 be a specialization of that. */
2365 ;
2366 else if (DECL_ARTIFICIAL (fn))
2367 /* Cannot specialize functions that are created implicitly. */
2368 ;
2369 else
2370 {
2371 tree decl_arg_types;
2372
2373 /* This is an ordinary member function. However, since
2374 we're here, we can assume its enclosing class is a
2375 template class. For example,
2376
2377 template <typename T> struct S { void f(); };
2378 template <> void S<int>::f() {}
2379
2380 Here, S<int>::f is a non-template, but S<int> is a
2381 template class. If FN has the same type as DECL, we
2382 might be in business. */
2383
2384 if (!DECL_TEMPLATE_INFO (fn))
2385 /* Its enclosing class is an explicit specialization
2386 of a template class. This is not a candidate. */
2387 continue;
2388
2389 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2390 TREE_TYPE (TREE_TYPE (fn))))
2391 /* The return types differ. */
2392 continue;
2393
2394 /* Adjust the type of DECL in case FN is a static member. */
2395 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2396 if (DECL_STATIC_FUNCTION_P (fn)
2397 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2398 decl_arg_types = TREE_CHAIN (decl_arg_types);
2399
2400 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2401 decl_arg_types))
2402 continue;
2403
2404 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2405 && (type_memfn_rqual (TREE_TYPE (decl))
2406 != type_memfn_rqual (TREE_TYPE (fn))))
2407 continue;
2408
2409 // If the deduced arguments do not satisfy the constraints,
2410 // this is not a candidate.
2411 if (flag_concepts && !constraints_satisfied_p (fn))
2412 continue;
2413
2414 // Add the candidate.
2415 candidates = tree_cons (NULL_TREE, fn, candidates);
2416 }
2417 }
2418
2419 if (templates && TREE_CHAIN (templates))
2420 {
2421 /* We have:
2422
2423 [temp.expl.spec]
2424
2425 It is possible for a specialization with a given function
2426 signature to be instantiated from more than one function
2427 template. In such cases, explicit specification of the
2428 template arguments must be used to uniquely identify the
2429 function template specialization being specialized.
2430
2431 Note that here, there's no suggestion that we're supposed to
2432 determine which of the candidate templates is most
2433 specialized. However, we, also have:
2434
2435 [temp.func.order]
2436
2437 Partial ordering of overloaded function template
2438 declarations is used in the following contexts to select
2439 the function template to which a function template
2440 specialization refers:
2441
2442 -- when an explicit specialization refers to a function
2443 template.
2444
2445 So, we do use the partial ordering rules, at least for now.
2446 This extension can only serve to make invalid programs valid,
2447 so it's safe. And, there is strong anecdotal evidence that
2448 the committee intended the partial ordering rules to apply;
2449 the EDG front end has that behavior, and John Spicer claims
2450 that the committee simply forgot to delete the wording in
2451 [temp.expl.spec]. */
2452 tree tmpl = most_specialized_instantiation (templates);
2453 if (tmpl != error_mark_node)
2454 {
2455 templates = tmpl;
2456 TREE_CHAIN (templates) = NULL_TREE;
2457 }
2458 }
2459
2460 // Concepts allows multiple declarations of member functions
2461 // with the same signature. Like above, we need to rely on
2462 // on the partial ordering of those candidates to determine which
2463 // is the best.
2464 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2465 {
2466 if (tree cand = most_constrained_function (candidates))
2467 {
2468 candidates = cand;
2469 TREE_CHAIN (cand) = NULL_TREE;
2470 }
2471 }
2472
2473 if (templates == NULL_TREE && candidates == NULL_TREE)
2474 {
2475 error ("template-id %qD for %q+D does not match any template "
2476 "declaration", template_id, decl);
2477 if (header_count && header_count != template_count + 1)
2478 inform (DECL_SOURCE_LOCATION (decl),
2479 "saw %d %<template<>%>, need %d for "
2480 "specializing a member function template",
2481 header_count, template_count + 1);
2482 else
2483 print_candidates (orig_fns);
2484 return error_mark_node;
2485 }
2486 else if ((templates && TREE_CHAIN (templates))
2487 || (candidates && TREE_CHAIN (candidates))
2488 || (templates && candidates))
2489 {
2490 error ("ambiguous template specialization %qD for %q+D",
2491 template_id, decl);
2492 candidates = chainon (candidates, templates);
2493 print_candidates (candidates);
2494 return error_mark_node;
2495 }
2496
2497 /* We have one, and exactly one, match. */
2498 if (candidates)
2499 {
2500 tree fn = TREE_VALUE (candidates);
2501 *targs_out = copy_node (DECL_TI_ARGS (fn));
2502
2503 /* Propagate the candidate's constraints to the declaration. */
2504 if (tsk != tsk_template)
2505 set_constraints (decl, get_constraints (fn));
2506
2507 /* DECL is a re-declaration or partial instantiation of a template
2508 function. */
2509 if (TREE_CODE (fn) == TEMPLATE_DECL)
2510 return fn;
2511 /* It was a specialization of an ordinary member function in a
2512 template class. */
2513 return DECL_TI_TEMPLATE (fn);
2514 }
2515
2516 /* It was a specialization of a template. */
2517 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2518 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2519 {
2520 *targs_out = copy_node (targs);
2521 SET_TMPL_ARGS_LEVEL (*targs_out,
2522 TMPL_ARGS_DEPTH (*targs_out),
2523 TREE_PURPOSE (templates));
2524 }
2525 else
2526 *targs_out = TREE_PURPOSE (templates);
2527 return TREE_VALUE (templates);
2528 }
2529
2530 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2531 but with the default argument values filled in from those in the
2532 TMPL_TYPES. */
2533
2534 static tree
2535 copy_default_args_to_explicit_spec_1 (tree spec_types,
2536 tree tmpl_types)
2537 {
2538 tree new_spec_types;
2539
2540 if (!spec_types)
2541 return NULL_TREE;
2542
2543 if (spec_types == void_list_node)
2544 return void_list_node;
2545
2546 /* Substitute into the rest of the list. */
2547 new_spec_types =
2548 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2549 TREE_CHAIN (tmpl_types));
2550
2551 /* Add the default argument for this parameter. */
2552 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2553 TREE_VALUE (spec_types),
2554 new_spec_types);
2555 }
2556
2557 /* DECL is an explicit specialization. Replicate default arguments
2558 from the template it specializes. (That way, code like:
2559
2560 template <class T> void f(T = 3);
2561 template <> void f(double);
2562 void g () { f (); }
2563
2564 works, as required.) An alternative approach would be to look up
2565 the correct default arguments at the call-site, but this approach
2566 is consistent with how implicit instantiations are handled. */
2567
2568 static void
2569 copy_default_args_to_explicit_spec (tree decl)
2570 {
2571 tree tmpl;
2572 tree spec_types;
2573 tree tmpl_types;
2574 tree new_spec_types;
2575 tree old_type;
2576 tree new_type;
2577 tree t;
2578 tree object_type = NULL_TREE;
2579 tree in_charge = NULL_TREE;
2580 tree vtt = NULL_TREE;
2581
2582 /* See if there's anything we need to do. */
2583 tmpl = DECL_TI_TEMPLATE (decl);
2584 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2585 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2586 if (TREE_PURPOSE (t))
2587 break;
2588 if (!t)
2589 return;
2590
2591 old_type = TREE_TYPE (decl);
2592 spec_types = TYPE_ARG_TYPES (old_type);
2593
2594 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2595 {
2596 /* Remove the this pointer, but remember the object's type for
2597 CV quals. */
2598 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2599 spec_types = TREE_CHAIN (spec_types);
2600 tmpl_types = TREE_CHAIN (tmpl_types);
2601
2602 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2603 {
2604 /* DECL may contain more parameters than TMPL due to the extra
2605 in-charge parameter in constructors and destructors. */
2606 in_charge = spec_types;
2607 spec_types = TREE_CHAIN (spec_types);
2608 }
2609 if (DECL_HAS_VTT_PARM_P (decl))
2610 {
2611 vtt = spec_types;
2612 spec_types = TREE_CHAIN (spec_types);
2613 }
2614 }
2615
2616 /* Compute the merged default arguments. */
2617 new_spec_types =
2618 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2619
2620 /* Compute the new FUNCTION_TYPE. */
2621 if (object_type)
2622 {
2623 if (vtt)
2624 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2625 TREE_VALUE (vtt),
2626 new_spec_types);
2627
2628 if (in_charge)
2629 /* Put the in-charge parameter back. */
2630 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2631 TREE_VALUE (in_charge),
2632 new_spec_types);
2633
2634 new_type = build_method_type_directly (object_type,
2635 TREE_TYPE (old_type),
2636 new_spec_types);
2637 }
2638 else
2639 new_type = build_function_type (TREE_TYPE (old_type),
2640 new_spec_types);
2641 new_type = cp_build_type_attribute_variant (new_type,
2642 TYPE_ATTRIBUTES (old_type));
2643 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2644
2645 TREE_TYPE (decl) = new_type;
2646 }
2647
2648 /* Return the number of template headers we expect to see for a definition
2649 or specialization of CTYPE or one of its non-template members. */
2650
2651 int
2652 num_template_headers_for_class (tree ctype)
2653 {
2654 int num_templates = 0;
2655
2656 while (ctype && CLASS_TYPE_P (ctype))
2657 {
2658 /* You're supposed to have one `template <...>' for every
2659 template class, but you don't need one for a full
2660 specialization. For example:
2661
2662 template <class T> struct S{};
2663 template <> struct S<int> { void f(); };
2664 void S<int>::f () {}
2665
2666 is correct; there shouldn't be a `template <>' for the
2667 definition of `S<int>::f'. */
2668 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2669 /* If CTYPE does not have template information of any
2670 kind, then it is not a template, nor is it nested
2671 within a template. */
2672 break;
2673 if (explicit_class_specialization_p (ctype))
2674 break;
2675 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2676 ++num_templates;
2677
2678 ctype = TYPE_CONTEXT (ctype);
2679 }
2680
2681 return num_templates;
2682 }
2683
2684 /* Do a simple sanity check on the template headers that precede the
2685 variable declaration DECL. */
2686
2687 void
2688 check_template_variable (tree decl)
2689 {
2690 tree ctx = CP_DECL_CONTEXT (decl);
2691 int wanted = num_template_headers_for_class (ctx);
2692 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2693 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2694 {
2695 if (cxx_dialect < cxx14)
2696 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2697 "variable templates only available with "
2698 "%<-std=c++14%> or %<-std=gnu++14%>");
2699
2700 // Namespace-scope variable templates should have a template header.
2701 ++wanted;
2702 }
2703 if (template_header_count > wanted)
2704 {
2705 auto_diagnostic_group d;
2706 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2707 "too many template headers for %qD "
2708 "(should be %d)",
2709 decl, wanted);
2710 if (warned && CLASS_TYPE_P (ctx)
2711 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2712 inform (DECL_SOURCE_LOCATION (decl),
2713 "members of an explicitly specialized class are defined "
2714 "without a template header");
2715 }
2716 }
2717
2718 /* An explicit specialization whose declarator-id or class-head-name is not
2719 qualified shall be declared in the nearest enclosing namespace of the
2720 template, or, if the namespace is inline (7.3.1), any namespace from its
2721 enclosing namespace set.
2722
2723 If the name declared in the explicit instantiation is an unqualified name,
2724 the explicit instantiation shall appear in the namespace where its template
2725 is declared or, if that namespace is inline (7.3.1), any namespace from its
2726 enclosing namespace set. */
2727
2728 void
2729 check_unqualified_spec_or_inst (tree t, location_t loc)
2730 {
2731 tree tmpl = most_general_template (t);
2732 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2733 && !is_nested_namespace (current_namespace,
2734 CP_DECL_CONTEXT (tmpl), true))
2735 {
2736 if (processing_specialization)
2737 permerror (loc, "explicit specialization of %qD outside its "
2738 "namespace must use a nested-name-specifier", tmpl);
2739 else if (processing_explicit_instantiation
2740 && cxx_dialect >= cxx11)
2741 /* This was allowed in C++98, so only pedwarn. */
2742 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2743 "outside its namespace must use a nested-name-"
2744 "specifier", tmpl);
2745 }
2746 }
2747
2748 /* Warn for a template specialization SPEC that is missing some of a set
2749 of function or type attributes that the template TEMPL is declared with.
2750 ATTRLIST is a list of additional attributes that SPEC should be taken
2751 to ultimately be declared with. */
2752
2753 static void
2754 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2755 {
2756 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2757 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2758
2759 /* Avoid warning if the difference between the primary and
2760 the specialization is not in one of the attributes below. */
2761 const char* const blacklist[] = {
2762 "alloc_align", "alloc_size", "assume_aligned", "format",
2763 "format_arg", "malloc", "nonnull", NULL
2764 };
2765
2766 /* Put together a list of the black listed attributes that the primary
2767 template is declared with that the specialization is not, in case
2768 it's not apparent from the most recent declaration of the primary. */
2769 pretty_printer str;
2770 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2771 blacklist, &str);
2772
2773 if (!nattrs)
2774 return;
2775
2776 auto_diagnostic_group d;
2777 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2778 "explicit specialization %q#D may be missing attributes",
2779 spec))
2780 inform (DECL_SOURCE_LOCATION (tmpl),
2781 nattrs > 1
2782 ? G_("missing primary template attributes %s")
2783 : G_("missing primary template attribute %s"),
2784 pp_formatted_text (&str));
2785 }
2786
2787 /* Check to see if the function just declared, as indicated in
2788 DECLARATOR, and in DECL, is a specialization of a function
2789 template. We may also discover that the declaration is an explicit
2790 instantiation at this point.
2791
2792 Returns DECL, or an equivalent declaration that should be used
2793 instead if all goes well. Issues an error message if something is
2794 amiss. Returns error_mark_node if the error is not easily
2795 recoverable.
2796
2797 FLAGS is a bitmask consisting of the following flags:
2798
2799 2: The function has a definition.
2800 4: The function is a friend.
2801
2802 The TEMPLATE_COUNT is the number of references to qualifying
2803 template classes that appeared in the name of the function. For
2804 example, in
2805
2806 template <class T> struct S { void f(); };
2807 void S<int>::f();
2808
2809 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2810 classes are not counted in the TEMPLATE_COUNT, so that in
2811
2812 template <class T> struct S {};
2813 template <> struct S<int> { void f(); }
2814 template <> void S<int>::f();
2815
2816 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2817 invalid; there should be no template <>.)
2818
2819 If the function is a specialization, it is marked as such via
2820 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2821 is set up correctly, and it is added to the list of specializations
2822 for that template. */
2823
2824 tree
2825 check_explicit_specialization (tree declarator,
2826 tree decl,
2827 int template_count,
2828 int flags,
2829 tree attrlist)
2830 {
2831 int have_def = flags & 2;
2832 int is_friend = flags & 4;
2833 bool is_concept = flags & 8;
2834 int specialization = 0;
2835 int explicit_instantiation = 0;
2836 int member_specialization = 0;
2837 tree ctype = DECL_CLASS_CONTEXT (decl);
2838 tree dname = DECL_NAME (decl);
2839 tmpl_spec_kind tsk;
2840
2841 if (is_friend)
2842 {
2843 if (!processing_specialization)
2844 tsk = tsk_none;
2845 else
2846 tsk = tsk_excessive_parms;
2847 }
2848 else
2849 tsk = current_tmpl_spec_kind (template_count);
2850
2851 switch (tsk)
2852 {
2853 case tsk_none:
2854 if (processing_specialization && !VAR_P (decl))
2855 {
2856 specialization = 1;
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 }
2859 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2860 {
2861 if (is_friend)
2862 /* This could be something like:
2863
2864 template <class T> void f(T);
2865 class S { friend void f<>(int); } */
2866 specialization = 1;
2867 else
2868 {
2869 /* This case handles bogus declarations like template <>
2870 template <class T> void f<int>(); */
2871
2872 error_at (cp_expr_loc_or_input_loc (declarator),
2873 "template-id %qE in declaration of primary template",
2874 declarator);
2875 return decl;
2876 }
2877 }
2878 break;
2879
2880 case tsk_invalid_member_spec:
2881 /* The error has already been reported in
2882 check_specialization_scope. */
2883 return error_mark_node;
2884
2885 case tsk_invalid_expl_inst:
2886 error ("template parameter list used in explicit instantiation");
2887
2888 /* Fall through. */
2889
2890 case tsk_expl_inst:
2891 if (have_def)
2892 error ("definition provided for explicit instantiation");
2893
2894 explicit_instantiation = 1;
2895 break;
2896
2897 case tsk_excessive_parms:
2898 case tsk_insufficient_parms:
2899 if (tsk == tsk_excessive_parms)
2900 error ("too many template parameter lists in declaration of %qD",
2901 decl);
2902 else if (template_header_count)
2903 error("too few template parameter lists in declaration of %qD", decl);
2904 else
2905 error("explicit specialization of %qD must be introduced by "
2906 "%<template <>%>", decl);
2907
2908 /* Fall through. */
2909 case tsk_expl_spec:
2910 if (is_concept)
2911 error ("explicit specialization declared %<concept%>");
2912
2913 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2914 /* In cases like template<> constexpr bool v = true;
2915 We'll give an error in check_template_variable. */
2916 break;
2917
2918 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2919 if (ctype)
2920 member_specialization = 1;
2921 else
2922 specialization = 1;
2923 break;
2924
2925 case tsk_template:
2926 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2927 {
2928 /* This case handles bogus declarations like template <>
2929 template <class T> void f<int>(); */
2930
2931 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2932 error_at (cp_expr_loc_or_input_loc (declarator),
2933 "template-id %qE in declaration of primary template",
2934 declarator);
2935 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2936 {
2937 /* Partial specialization of variable template. */
2938 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2939 specialization = 1;
2940 goto ok;
2941 }
2942 else if (cxx_dialect < cxx14)
2943 error_at (cp_expr_loc_or_input_loc (declarator),
2944 "non-type partial specialization %qE "
2945 "is not allowed", declarator);
2946 else
2947 error_at (cp_expr_loc_or_input_loc (declarator),
2948 "non-class, non-variable partial specialization %qE "
2949 "is not allowed", declarator);
2950 return decl;
2951 ok:;
2952 }
2953
2954 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2955 /* This is a specialization of a member template, without
2956 specialization the containing class. Something like:
2957
2958 template <class T> struct S {
2959 template <class U> void f (U);
2960 };
2961 template <> template <class U> void S<int>::f(U) {}
2962
2963 That's a specialization -- but of the entire template. */
2964 specialization = 1;
2965 break;
2966
2967 default:
2968 gcc_unreachable ();
2969 }
2970
2971 if ((specialization || member_specialization)
2972 /* This doesn't apply to variable templates. */
2973 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2974 {
2975 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2976 for (; t; t = TREE_CHAIN (t))
2977 if (TREE_PURPOSE (t))
2978 {
2979 permerror (input_location,
2980 "default argument specified in explicit specialization");
2981 break;
2982 }
2983 }
2984
2985 if (specialization || member_specialization || explicit_instantiation)
2986 {
2987 tree tmpl = NULL_TREE;
2988 tree targs = NULL_TREE;
2989 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2990
2991 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2992 if (!was_template_id)
2993 {
2994 tree fns;
2995
2996 gcc_assert (identifier_p (declarator));
2997 if (ctype)
2998 fns = dname;
2999 else
3000 {
3001 /* If there is no class context, the explicit instantiation
3002 must be at namespace scope. */
3003 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3004
3005 /* Find the namespace binding, using the declaration
3006 context. */
3007 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3008 LOOK_want::NORMAL, true);
3009 if (fns == error_mark_node)
3010 /* If lookup fails, look for a friend declaration so we can
3011 give a better diagnostic. */
3012 fns = (lookup_qualified_name
3013 (CP_DECL_CONTEXT (decl), dname,
3014 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3015 /*complain*/true));
3016
3017 if (fns == error_mark_node || !is_overloaded_fn (fns))
3018 {
3019 error ("%qD is not a template function", dname);
3020 fns = error_mark_node;
3021 }
3022 }
3023
3024 declarator = lookup_template_function (fns, NULL_TREE);
3025 }
3026
3027 if (declarator == error_mark_node)
3028 return error_mark_node;
3029
3030 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3031 {
3032 if (!explicit_instantiation)
3033 /* A specialization in class scope. This is invalid,
3034 but the error will already have been flagged by
3035 check_specialization_scope. */
3036 return error_mark_node;
3037 else
3038 {
3039 /* It's not valid to write an explicit instantiation in
3040 class scope, e.g.:
3041
3042 class C { template void f(); }
3043
3044 This case is caught by the parser. However, on
3045 something like:
3046
3047 template class C { void f(); };
3048
3049 (which is invalid) we can get here. The error will be
3050 issued later. */
3051 ;
3052 }
3053
3054 return decl;
3055 }
3056 else if (ctype != NULL_TREE
3057 && (identifier_p (TREE_OPERAND (declarator, 0))))
3058 {
3059 // We'll match variable templates in start_decl.
3060 if (VAR_P (decl))
3061 return decl;
3062
3063 /* Find the list of functions in ctype that have the same
3064 name as the declared function. */
3065 tree name = TREE_OPERAND (declarator, 0);
3066
3067 if (constructor_name_p (name, ctype))
3068 {
3069 if (DECL_CONSTRUCTOR_P (decl)
3070 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3071 : !CLASSTYPE_DESTRUCTOR (ctype))
3072 {
3073 /* From [temp.expl.spec]:
3074
3075 If such an explicit specialization for the member
3076 of a class template names an implicitly-declared
3077 special member function (clause _special_), the
3078 program is ill-formed.
3079
3080 Similar language is found in [temp.explicit]. */
3081 error ("specialization of implicitly-declared special member function");
3082 return error_mark_node;
3083 }
3084
3085 name = DECL_NAME (decl);
3086 }
3087
3088 /* For a type-conversion operator, We might be looking for
3089 `operator int' which will be a specialization of
3090 `operator T'. Grab all the conversion operators, and
3091 then select from them. */
3092 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3093 ? conv_op_identifier : name);
3094
3095 if (fns == NULL_TREE)
3096 {
3097 error ("no member function %qD declared in %qT", name, ctype);
3098 return error_mark_node;
3099 }
3100 else
3101 TREE_OPERAND (declarator, 0) = fns;
3102 }
3103
3104 /* Figure out what exactly is being specialized at this point.
3105 Note that for an explicit instantiation, even one for a
3106 member function, we cannot tell a priori whether the
3107 instantiation is for a member template, or just a member
3108 function of a template class. Even if a member template is
3109 being instantiated, the member template arguments may be
3110 elided if they can be deduced from the rest of the
3111 declaration. */
3112 tmpl = determine_specialization (declarator, decl,
3113 &targs,
3114 member_specialization,
3115 template_count,
3116 tsk);
3117
3118 if (!tmpl || tmpl == error_mark_node)
3119 /* We couldn't figure out what this declaration was
3120 specializing. */
3121 return error_mark_node;
3122 else
3123 {
3124 if (TREE_CODE (decl) == FUNCTION_DECL
3125 && DECL_HIDDEN_FRIEND_P (tmpl))
3126 {
3127 auto_diagnostic_group d;
3128 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3129 "friend declaration %qD is not visible to "
3130 "explicit specialization", tmpl))
3131 inform (DECL_SOURCE_LOCATION (tmpl),
3132 "friend declaration here");
3133 }
3134 else if (!ctype && !is_friend
3135 && CP_DECL_CONTEXT (decl) == current_namespace)
3136 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3137
3138 tree gen_tmpl = most_general_template (tmpl);
3139
3140 if (explicit_instantiation)
3141 {
3142 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3143 is done by do_decl_instantiation later. */
3144
3145 int arg_depth = TMPL_ARGS_DEPTH (targs);
3146 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3147
3148 if (arg_depth > parm_depth)
3149 {
3150 /* If TMPL is not the most general template (for
3151 example, if TMPL is a friend template that is
3152 injected into namespace scope), then there will
3153 be too many levels of TARGS. Remove some of them
3154 here. */
3155 int i;
3156 tree new_targs;
3157
3158 new_targs = make_tree_vec (parm_depth);
3159 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3160 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3161 = TREE_VEC_ELT (targs, i);
3162 targs = new_targs;
3163 }
3164
3165 return instantiate_template (tmpl, targs, tf_error);
3166 }
3167
3168 /* If we thought that the DECL was a member function, but it
3169 turns out to be specializing a static member function,
3170 make DECL a static member function as well. */
3171 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3172 && DECL_STATIC_FUNCTION_P (tmpl)
3173 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3174 revert_static_member_fn (decl);
3175
3176 /* If this is a specialization of a member template of a
3177 template class, we want to return the TEMPLATE_DECL, not
3178 the specialization of it. */
3179 if (tsk == tsk_template && !was_template_id)
3180 {
3181 tree result = DECL_TEMPLATE_RESULT (tmpl);
3182 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3183 DECL_INITIAL (result) = NULL_TREE;
3184 if (have_def)
3185 {
3186 tree parm;
3187 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3188 DECL_SOURCE_LOCATION (result)
3189 = DECL_SOURCE_LOCATION (decl);
3190 /* We want to use the argument list specified in the
3191 definition, not in the original declaration. */
3192 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3193 for (parm = DECL_ARGUMENTS (result); parm;
3194 parm = DECL_CHAIN (parm))
3195 DECL_CONTEXT (parm) = result;
3196 }
3197 return register_specialization (tmpl, gen_tmpl, targs,
3198 is_friend, 0);
3199 }
3200
3201 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3202 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3203
3204 if (was_template_id)
3205 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3206
3207 /* Inherit default function arguments from the template
3208 DECL is specializing. */
3209 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3210 copy_default_args_to_explicit_spec (decl);
3211
3212 /* This specialization has the same protection as the
3213 template it specializes. */
3214 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3215 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3216
3217 /* 7.1.1-1 [dcl.stc]
3218
3219 A storage-class-specifier shall not be specified in an
3220 explicit specialization...
3221
3222 The parser rejects these, so unless action is taken here,
3223 explicit function specializations will always appear with
3224 global linkage.
3225
3226 The action recommended by the C++ CWG in response to C++
3227 defect report 605 is to make the storage class and linkage
3228 of the explicit specialization match the templated function:
3229
3230 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3231 */
3232 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3233 {
3234 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3235 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3236
3237 /* A concept cannot be specialized. */
3238 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3239 {
3240 error ("explicit specialization of function concept %qD",
3241 gen_tmpl);
3242 return error_mark_node;
3243 }
3244
3245 /* This specialization has the same linkage and visibility as
3246 the function template it specializes. */
3247 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3248 if (! TREE_PUBLIC (decl))
3249 {
3250 DECL_INTERFACE_KNOWN (decl) = 1;
3251 DECL_NOT_REALLY_EXTERN (decl) = 1;
3252 }
3253 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3254 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3255 {
3256 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3257 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3258 }
3259 }
3260
3261 /* If DECL is a friend declaration, declared using an
3262 unqualified name, the namespace associated with DECL may
3263 have been set incorrectly. For example, in:
3264
3265 template <typename T> void f(T);
3266 namespace N {
3267 struct S { friend void f<int>(int); }
3268 }
3269
3270 we will have set the DECL_CONTEXT for the friend
3271 declaration to N, rather than to the global namespace. */
3272 if (DECL_NAMESPACE_SCOPE_P (decl))
3273 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3274
3275 if (is_friend && !have_def)
3276 /* This is not really a declaration of a specialization.
3277 It's just the name of an instantiation. But, it's not
3278 a request for an instantiation, either. */
3279 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3280 else if (TREE_CODE (decl) == FUNCTION_DECL)
3281 /* A specialization is not necessarily COMDAT. */
3282 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3283 && DECL_DECLARED_INLINE_P (decl));
3284 else if (VAR_P (decl))
3285 DECL_COMDAT (decl) = false;
3286
3287 /* If this is a full specialization, register it so that we can find
3288 it again. Partial specializations will be registered in
3289 process_partial_specialization. */
3290 if (!processing_template_decl)
3291 {
3292 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3293
3294 decl = register_specialization (decl, gen_tmpl, targs,
3295 is_friend, 0);
3296 }
3297
3298
3299 /* A 'structor should already have clones. */
3300 gcc_assert (decl == error_mark_node
3301 || variable_template_p (tmpl)
3302 || !(DECL_CONSTRUCTOR_P (decl)
3303 || DECL_DESTRUCTOR_P (decl))
3304 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3305 }
3306 }
3307
3308 return decl;
3309 }
3310
3311 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3312 parameters. These are represented in the same format used for
3313 DECL_TEMPLATE_PARMS. */
3314
3315 int
3316 comp_template_parms (const_tree parms1, const_tree parms2)
3317 {
3318 const_tree p1;
3319 const_tree p2;
3320
3321 if (parms1 == parms2)
3322 return 1;
3323
3324 for (p1 = parms1, p2 = parms2;
3325 p1 != NULL_TREE && p2 != NULL_TREE;
3326 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3327 {
3328 tree t1 = TREE_VALUE (p1);
3329 tree t2 = TREE_VALUE (p2);
3330 int i;
3331
3332 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3333 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3334
3335 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3336 return 0;
3337
3338 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3339 {
3340 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3341 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3342
3343 /* If either of the template parameters are invalid, assume
3344 they match for the sake of error recovery. */
3345 if (error_operand_p (parm1) || error_operand_p (parm2))
3346 return 1;
3347
3348 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3349 return 0;
3350
3351 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3352 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3353 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3354 continue;
3355 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3356 return 0;
3357 }
3358 }
3359
3360 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3361 /* One set of parameters has more parameters lists than the
3362 other. */
3363 return 0;
3364
3365 return 1;
3366 }
3367
3368 /* Returns true if two template parameters are declared with
3369 equivalent constraints. */
3370
3371 static bool
3372 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3373 {
3374 tree req1 = TREE_TYPE (parm1);
3375 tree req2 = TREE_TYPE (parm2);
3376 if (!req1 != !req2)
3377 return false;
3378 if (req1)
3379 return cp_tree_equal (req1, req2);
3380 return true;
3381 }
3382
3383 /* Returns true when two template parameters are equivalent. */
3384
3385 static bool
3386 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3387 {
3388 tree decl1 = TREE_VALUE (parm1);
3389 tree decl2 = TREE_VALUE (parm2);
3390
3391 /* If either of the template parameters are invalid, assume
3392 they match for the sake of error recovery. */
3393 if (error_operand_p (decl1) || error_operand_p (decl2))
3394 return true;
3395
3396 /* ... they declare parameters of the same kind. */
3397 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3398 return false;
3399
3400 /* ... one parameter was introduced by a parameter declaration, then
3401 both are. This case arises as a result of eagerly rewriting declarations
3402 during parsing. */
3403 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3404 return false;
3405
3406 /* ... if either declares a pack, they both do. */
3407 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3408 return false;
3409
3410 if (TREE_CODE (decl1) == PARM_DECL)
3411 {
3412 /* ... if they declare non-type parameters, the types are equivalent. */
3413 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3414 return false;
3415 }
3416 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3417 {
3418 /* ... if they declare template template parameters, their template
3419 parameter lists are equivalent. */
3420 if (!template_heads_equivalent_p (decl1, decl2))
3421 return false;
3422 }
3423
3424 /* ... if they are declared with a qualified-concept name, they both
3425 are, and those names are equivalent. */
3426 return template_parameter_constraints_equivalent_p (parm1, parm2);
3427 }
3428
3429 /* Returns true if two template parameters lists are equivalent.
3430 Two template parameter lists are equivalent if they have the
3431 same length and their corresponding parameters are equivalent.
3432
3433 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3434 data structure returned by DECL_TEMPLATE_PARMS.
3435
3436 This is generally the same implementation as comp_template_parms
3437 except that it also the concept names and arguments used to
3438 introduce parameters. */
3439
3440 static bool
3441 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3442 {
3443 if (parms1 == parms2)
3444 return true;
3445
3446 const_tree p1 = parms1;
3447 const_tree p2 = parms2;
3448 while (p1 != NULL_TREE && p2 != NULL_TREE)
3449 {
3450 tree list1 = TREE_VALUE (p1);
3451 tree list2 = TREE_VALUE (p2);
3452
3453 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3454 return 0;
3455
3456 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3457 {
3458 tree parm1 = TREE_VEC_ELT (list1, i);
3459 tree parm2 = TREE_VEC_ELT (list2, i);
3460 if (!template_parameters_equivalent_p (parm1, parm2))
3461 return false;
3462 }
3463
3464 p1 = TREE_CHAIN (p1);
3465 p2 = TREE_CHAIN (p2);
3466 }
3467
3468 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3469 return false;
3470
3471 return true;
3472 }
3473
3474 /* Return true if the requires-clause of the template parameter lists are
3475 equivalent and false otherwise. */
3476 static bool
3477 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3478 {
3479 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3480 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3481 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3482 return false;
3483 if (!cp_tree_equal (req1, req2))
3484 return false;
3485 return true;
3486 }
3487
3488 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3489 Two template heads are equivalent if their template parameter
3490 lists are equivalent and their requires clauses are equivalent.
3491
3492 In pre-C++20, this is equivalent to calling comp_template_parms
3493 for the template parameters of TMPL1 and TMPL2. */
3494
3495 bool
3496 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3497 {
3498 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3499 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3500
3501 /* Don't change the matching rules for pre-C++20. */
3502 if (cxx_dialect < cxx20)
3503 return comp_template_parms (parms1, parms2);
3504
3505 /* ... have the same number of template parameters, and their
3506 corresponding parameters are equivalent. */
3507 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3508 return false;
3509
3510 /* ... if either has a requires-clause, they both do and their
3511 corresponding constraint-expressions are equivalent. */
3512 return template_requirements_equivalent_p (parms1, parms2);
3513 }
3514
3515 /* Determine whether PARM is a parameter pack. */
3516
3517 bool
3518 template_parameter_pack_p (const_tree parm)
3519 {
3520 /* Determine if we have a non-type template parameter pack. */
3521 if (TREE_CODE (parm) == PARM_DECL)
3522 return (DECL_TEMPLATE_PARM_P (parm)
3523 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3524 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3525 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3526
3527 /* If this is a list of template parameters, we could get a
3528 TYPE_DECL or a TEMPLATE_DECL. */
3529 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3530 parm = TREE_TYPE (parm);
3531
3532 /* Otherwise it must be a type template parameter. */
3533 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3534 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3535 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3536 }
3537
3538 /* Determine if T is a function parameter pack. */
3539
3540 bool
3541 function_parameter_pack_p (const_tree t)
3542 {
3543 if (t && TREE_CODE (t) == PARM_DECL)
3544 return DECL_PACK_P (t);
3545 return false;
3546 }
3547
3548 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3549 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3550
3551 tree
3552 get_function_template_decl (const_tree primary_func_tmpl_inst)
3553 {
3554 if (! primary_func_tmpl_inst
3555 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3556 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3557 return NULL;
3558
3559 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3560 }
3561
3562 /* Return true iff the function parameter PARAM_DECL was expanded
3563 from the function parameter pack PACK. */
3564
3565 bool
3566 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3567 {
3568 if (DECL_ARTIFICIAL (param_decl)
3569 || !function_parameter_pack_p (pack))
3570 return false;
3571
3572 /* The parameter pack and its pack arguments have the same
3573 DECL_PARM_INDEX. */
3574 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3575 }
3576
3577 /* Determine whether ARGS describes a variadic template args list,
3578 i.e., one that is terminated by a template argument pack. */
3579
3580 static bool
3581 template_args_variadic_p (tree args)
3582 {
3583 int nargs;
3584 tree last_parm;
3585
3586 if (args == NULL_TREE)
3587 return false;
3588
3589 args = INNERMOST_TEMPLATE_ARGS (args);
3590 nargs = TREE_VEC_LENGTH (args);
3591
3592 if (nargs == 0)
3593 return false;
3594
3595 last_parm = TREE_VEC_ELT (args, nargs - 1);
3596
3597 return ARGUMENT_PACK_P (last_parm);
3598 }
3599
3600 /* Generate a new name for the parameter pack name NAME (an
3601 IDENTIFIER_NODE) that incorporates its */
3602
3603 static tree
3604 make_ith_pack_parameter_name (tree name, int i)
3605 {
3606 /* Munge the name to include the parameter index. */
3607 #define NUMBUF_LEN 128
3608 char numbuf[NUMBUF_LEN];
3609 char* newname;
3610 int newname_len;
3611
3612 if (name == NULL_TREE)
3613 return name;
3614 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3615 newname_len = IDENTIFIER_LENGTH (name)
3616 + strlen (numbuf) + 2;
3617 newname = (char*)alloca (newname_len);
3618 snprintf (newname, newname_len,
3619 "%s#%i", IDENTIFIER_POINTER (name), i);
3620 return get_identifier (newname);
3621 }
3622
3623 /* Return true if T is a primary function, class or alias template
3624 specialization, not including the template pattern. */
3625
3626 bool
3627 primary_template_specialization_p (const_tree t)
3628 {
3629 if (!t)
3630 return false;
3631
3632 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3633 return (DECL_LANG_SPECIFIC (t)
3634 && DECL_USE_TEMPLATE (t)
3635 && DECL_TEMPLATE_INFO (t)
3636 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3637 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3638 return (CLASSTYPE_TEMPLATE_INFO (t)
3639 && CLASSTYPE_USE_TEMPLATE (t)
3640 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3641 else if (alias_template_specialization_p (t, nt_transparent))
3642 return true;
3643 return false;
3644 }
3645
3646 /* Return true if PARM is a template template parameter. */
3647
3648 bool
3649 template_template_parameter_p (const_tree parm)
3650 {
3651 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3652 }
3653
3654 /* Return true iff PARM is a DECL representing a type template
3655 parameter. */
3656
3657 bool
3658 template_type_parameter_p (const_tree parm)
3659 {
3660 return (parm
3661 && (TREE_CODE (parm) == TYPE_DECL
3662 || TREE_CODE (parm) == TEMPLATE_DECL)
3663 && DECL_TEMPLATE_PARM_P (parm));
3664 }
3665
3666 /* Return the template parameters of T if T is a
3667 primary template instantiation, NULL otherwise. */
3668
3669 tree
3670 get_primary_template_innermost_parameters (const_tree t)
3671 {
3672 tree parms = NULL, template_info = NULL;
3673
3674 if ((template_info = get_template_info (t))
3675 && primary_template_specialization_p (t))
3676 parms = INNERMOST_TEMPLATE_PARMS
3677 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3678
3679 return parms;
3680 }
3681
3682 /* Return the template parameters of the LEVELth level from the full list
3683 of template parameters PARMS. */
3684
3685 tree
3686 get_template_parms_at_level (tree parms, int level)
3687 {
3688 tree p;
3689 if (!parms
3690 || TREE_CODE (parms) != TREE_LIST
3691 || level > TMPL_PARMS_DEPTH (parms))
3692 return NULL_TREE;
3693
3694 for (p = parms; p; p = TREE_CHAIN (p))
3695 if (TMPL_PARMS_DEPTH (p) == level)
3696 return p;
3697
3698 return NULL_TREE;
3699 }
3700
3701 /* Returns the template arguments of T if T is a template instantiation,
3702 NULL otherwise. */
3703
3704 tree
3705 get_template_innermost_arguments (const_tree t)
3706 {
3707 tree args = NULL, template_info = NULL;
3708
3709 if ((template_info = get_template_info (t))
3710 && TI_ARGS (template_info))
3711 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3712
3713 return args;
3714 }
3715
3716 /* Return the argument pack elements of T if T is a template argument pack,
3717 NULL otherwise. */
3718
3719 tree
3720 get_template_argument_pack_elems (const_tree t)
3721 {
3722 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3723 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3724 return NULL;
3725
3726 return ARGUMENT_PACK_ARGS (t);
3727 }
3728
3729 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3730 ARGUMENT_PACK_SELECT represents. */
3731
3732 static tree
3733 argument_pack_select_arg (tree t)
3734 {
3735 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3736 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3737
3738 /* If the selected argument is an expansion E, that most likely means we were
3739 called from gen_elem_of_pack_expansion_instantiation during the
3740 substituting of an argument pack (of which the Ith element is a pack
3741 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3742 In this case, the Ith element resulting from this substituting is going to
3743 be a pack expansion, which pattern is the pattern of E. Let's return the
3744 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3745 resulting pack expansion from it. */
3746 if (PACK_EXPANSION_P (arg))
3747 {
3748 /* Make sure we aren't throwing away arg info. */
3749 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3750 arg = PACK_EXPANSION_PATTERN (arg);
3751 }
3752
3753 return arg;
3754 }
3755
3756
3757 /* True iff FN is a function representing a built-in variadic parameter
3758 pack. */
3759
3760 bool
3761 builtin_pack_fn_p (tree fn)
3762 {
3763 if (!fn
3764 || TREE_CODE (fn) != FUNCTION_DECL
3765 || !DECL_IS_BUILTIN (fn))
3766 return false;
3767
3768 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3769 return true;
3770
3771 return false;
3772 }
3773
3774 /* True iff CALL is a call to a function representing a built-in variadic
3775 parameter pack. */
3776
3777 static bool
3778 builtin_pack_call_p (tree call)
3779 {
3780 if (TREE_CODE (call) != CALL_EXPR)
3781 return false;
3782 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3783 }
3784
3785 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3786
3787 static tree
3788 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3789 tree in_decl)
3790 {
3791 tree ohi = CALL_EXPR_ARG (call, 0);
3792 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3793 false/*fn*/, true/*int_cst*/);
3794
3795 if (value_dependent_expression_p (hi))
3796 {
3797 if (hi != ohi)
3798 {
3799 call = copy_node (call);
3800 CALL_EXPR_ARG (call, 0) = hi;
3801 }
3802 tree ex = make_pack_expansion (call, complain);
3803 tree vec = make_tree_vec (1);
3804 TREE_VEC_ELT (vec, 0) = ex;
3805 return vec;
3806 }
3807 else
3808 {
3809 hi = cxx_constant_value (hi);
3810 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3811
3812 /* Calculate the largest value of len that won't make the size of the vec
3813 overflow an int. The compiler will exceed resource limits long before
3814 this, but it seems a decent place to diagnose. */
3815 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3816
3817 if (len < 0 || len > max)
3818 {
3819 if ((complain & tf_error)
3820 && hi != error_mark_node)
3821 error ("argument to %<__integer_pack%> must be between 0 and %d",
3822 max);
3823 return error_mark_node;
3824 }
3825
3826 tree vec = make_tree_vec (len);
3827
3828 for (int i = 0; i < len; ++i)
3829 TREE_VEC_ELT (vec, i) = size_int (i);
3830
3831 return vec;
3832 }
3833 }
3834
3835 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3836 CALL. */
3837
3838 static tree
3839 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3840 tree in_decl)
3841 {
3842 if (!builtin_pack_call_p (call))
3843 return NULL_TREE;
3844
3845 tree fn = CALL_EXPR_FN (call);
3846
3847 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3848 return expand_integer_pack (call, args, complain, in_decl);
3849
3850 return NULL_TREE;
3851 }
3852
3853 /* Structure used to track the progress of find_parameter_packs_r. */
3854 struct find_parameter_pack_data
3855 {
3856 /* TREE_LIST that will contain all of the parameter packs found by
3857 the traversal. */
3858 tree* parameter_packs;
3859
3860 /* Set of AST nodes that have been visited by the traversal. */
3861 hash_set<tree> *visited;
3862
3863 /* True iff we're making a type pack expansion. */
3864 bool type_pack_expansion_p;
3865 };
3866
3867 /* Identifies all of the argument packs that occur in a template
3868 argument and appends them to the TREE_LIST inside DATA, which is a
3869 find_parameter_pack_data structure. This is a subroutine of
3870 make_pack_expansion and uses_parameter_packs. */
3871 static tree
3872 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3873 {
3874 tree t = *tp;
3875 struct find_parameter_pack_data* ppd =
3876 (struct find_parameter_pack_data*)data;
3877 bool parameter_pack_p = false;
3878
3879 /* Don't look through typedefs; we are interested in whether a
3880 parameter pack is actually written in the expression/type we're
3881 looking at, not the target type. */
3882 if (TYPE_P (t) && typedef_variant_p (t))
3883 {
3884 /* But do look at arguments for an alias template. */
3885 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3886 cp_walk_tree (&TI_ARGS (tinfo),
3887 &find_parameter_packs_r,
3888 ppd, ppd->visited);
3889 *walk_subtrees = 0;
3890 return NULL_TREE;
3891 }
3892
3893 /* Identify whether this is a parameter pack or not. */
3894 switch (TREE_CODE (t))
3895 {
3896 case TEMPLATE_PARM_INDEX:
3897 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3898 parameter_pack_p = true;
3899 break;
3900
3901 case TEMPLATE_TYPE_PARM:
3902 t = TYPE_MAIN_VARIANT (t);
3903 /* FALLTHRU */
3904 case TEMPLATE_TEMPLATE_PARM:
3905 /* If the placeholder appears in the decl-specifier-seq of a function
3906 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3907 is a pack expansion, the invented template parameter is a template
3908 parameter pack. */
3909 if (ppd->type_pack_expansion_p && is_auto (t))
3910 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3911 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3912 parameter_pack_p = true;
3913 break;
3914
3915 case FIELD_DECL:
3916 case PARM_DECL:
3917 if (DECL_PACK_P (t))
3918 {
3919 /* We don't want to walk into the type of a PARM_DECL,
3920 because we don't want to see the type parameter pack. */
3921 *walk_subtrees = 0;
3922 parameter_pack_p = true;
3923 }
3924 break;
3925
3926 case VAR_DECL:
3927 if (DECL_PACK_P (t))
3928 {
3929 /* We don't want to walk into the type of a variadic capture proxy,
3930 because we don't want to see the type parameter pack. */
3931 *walk_subtrees = 0;
3932 parameter_pack_p = true;
3933 }
3934 else if (variable_template_specialization_p (t))
3935 {
3936 cp_walk_tree (&DECL_TI_ARGS (t),
3937 find_parameter_packs_r,
3938 ppd, ppd->visited);
3939 *walk_subtrees = 0;
3940 }
3941 break;
3942
3943 case CALL_EXPR:
3944 if (builtin_pack_call_p (t))
3945 parameter_pack_p = true;
3946 break;
3947
3948 case BASES:
3949 parameter_pack_p = true;
3950 break;
3951 default:
3952 /* Not a parameter pack. */
3953 break;
3954 }
3955
3956 if (parameter_pack_p)
3957 {
3958 /* Add this parameter pack to the list. */
3959 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3960 }
3961
3962 if (TYPE_P (t))
3963 cp_walk_tree (&TYPE_CONTEXT (t),
3964 &find_parameter_packs_r, ppd, ppd->visited);
3965
3966 /* This switch statement will return immediately if we don't find a
3967 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3968 switch (TREE_CODE (t))
3969 {
3970 case BOUND_TEMPLATE_TEMPLATE_PARM:
3971 /* Check the template itself. */
3972 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3973 &find_parameter_packs_r, ppd, ppd->visited);
3974 return NULL_TREE;
3975
3976 case DECL_EXPR:
3977 {
3978 tree decl = DECL_EXPR_DECL (t);
3979 /* Ignore the declaration of a capture proxy for a parameter pack. */
3980 if (is_capture_proxy (decl))
3981 *walk_subtrees = 0;
3982 if (is_typedef_decl (decl))
3983 /* Since we stop at typedefs above, we need to look through them at
3984 the point of the DECL_EXPR. */
3985 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3986 &find_parameter_packs_r, ppd, ppd->visited);
3987 return NULL_TREE;
3988 }
3989
3990 case TEMPLATE_DECL:
3991 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3992 return NULL_TREE;
3993 cp_walk_tree (&TREE_TYPE (t),
3994 &find_parameter_packs_r, ppd, ppd->visited);
3995 return NULL_TREE;
3996
3997 case TYPE_PACK_EXPANSION:
3998 case EXPR_PACK_EXPANSION:
3999 *walk_subtrees = 0;
4000 return NULL_TREE;
4001
4002 case INTEGER_TYPE:
4003 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4004 ppd, ppd->visited);
4005 *walk_subtrees = 0;
4006 return NULL_TREE;
4007
4008 case IDENTIFIER_NODE:
4009 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4010 ppd->visited);
4011 *walk_subtrees = 0;
4012 return NULL_TREE;
4013
4014 case LAMBDA_EXPR:
4015 {
4016 /* Since we defer implicit capture, look in the parms and body. */
4017 tree fn = lambda_function (t);
4018 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4019 ppd->visited);
4020 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4021 ppd->visited);
4022 return NULL_TREE;
4023 }
4024
4025 case DECLTYPE_TYPE:
4026 {
4027 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4028 type_pack_expansion_p to false so that any placeholders
4029 within the expression don't get marked as parameter packs. */
4030 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4031 ppd->type_pack_expansion_p = false;
4032 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4033 ppd, ppd->visited);
4034 ppd->type_pack_expansion_p = type_pack_expansion_p;
4035 *walk_subtrees = 0;
4036 return NULL_TREE;
4037 }
4038
4039 case IF_STMT:
4040 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4041 ppd, ppd->visited);
4042 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4043 ppd, ppd->visited);
4044 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4045 ppd, ppd->visited);
4046 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4047 *walk_subtrees = 0;
4048 return NULL_TREE;
4049
4050 default:
4051 return NULL_TREE;
4052 }
4053
4054 return NULL_TREE;
4055 }
4056
4057 /* Determines if the expression or type T uses any parameter packs. */
4058 tree
4059 uses_parameter_packs (tree t)
4060 {
4061 tree parameter_packs = NULL_TREE;
4062 struct find_parameter_pack_data ppd;
4063 ppd.parameter_packs = &parameter_packs;
4064 ppd.visited = new hash_set<tree>;
4065 ppd.type_pack_expansion_p = false;
4066 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4067 delete ppd.visited;
4068 return parameter_packs;
4069 }
4070
4071 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4072 representation a base-class initializer into a parameter pack
4073 expansion. If all goes well, the resulting node will be an
4074 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4075 respectively. */
4076 tree
4077 make_pack_expansion (tree arg, tsubst_flags_t complain)
4078 {
4079 tree result;
4080 tree parameter_packs = NULL_TREE;
4081 bool for_types = false;
4082 struct find_parameter_pack_data ppd;
4083
4084 if (!arg || arg == error_mark_node)
4085 return arg;
4086
4087 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4088 {
4089 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4090 class initializer. In this case, the TREE_PURPOSE will be a
4091 _TYPE node (representing the base class expansion we're
4092 initializing) and the TREE_VALUE will be a TREE_LIST
4093 containing the initialization arguments.
4094
4095 The resulting expansion looks somewhat different from most
4096 expansions. Rather than returning just one _EXPANSION, we
4097 return a TREE_LIST whose TREE_PURPOSE is a
4098 TYPE_PACK_EXPANSION containing the bases that will be
4099 initialized. The TREE_VALUE will be identical to the
4100 original TREE_VALUE, which is a list of arguments that will
4101 be passed to each base. We do not introduce any new pack
4102 expansion nodes into the TREE_VALUE (although it is possible
4103 that some already exist), because the TREE_PURPOSE and
4104 TREE_VALUE all need to be expanded together with the same
4105 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4106 resulting TREE_PURPOSE will mention the parameter packs in
4107 both the bases and the arguments to the bases. */
4108 tree purpose;
4109 tree value;
4110 tree parameter_packs = NULL_TREE;
4111
4112 /* Determine which parameter packs will be used by the base
4113 class expansion. */
4114 ppd.visited = new hash_set<tree>;
4115 ppd.parameter_packs = &parameter_packs;
4116 ppd.type_pack_expansion_p = false;
4117 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4118 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4119 &ppd, ppd.visited);
4120
4121 if (parameter_packs == NULL_TREE)
4122 {
4123 if (complain & tf_error)
4124 error ("base initializer expansion %qT contains no parameter packs",
4125 arg);
4126 delete ppd.visited;
4127 return error_mark_node;
4128 }
4129
4130 if (TREE_VALUE (arg) != void_type_node)
4131 {
4132 /* Collect the sets of parameter packs used in each of the
4133 initialization arguments. */
4134 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4135 {
4136 /* Determine which parameter packs will be expanded in this
4137 argument. */
4138 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4139 &ppd, ppd.visited);
4140 }
4141 }
4142
4143 delete ppd.visited;
4144
4145 /* Create the pack expansion type for the base type. */
4146 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4147 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4148 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4149 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4150
4151 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4152 they will rarely be compared to anything. */
4153 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4154
4155 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4156 }
4157
4158 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4159 for_types = true;
4160
4161 /* Build the PACK_EXPANSION_* node. */
4162 result = for_types
4163 ? cxx_make_type (TYPE_PACK_EXPANSION)
4164 : make_node (EXPR_PACK_EXPANSION);
4165 SET_PACK_EXPANSION_PATTERN (result, arg);
4166 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4167 {
4168 /* Propagate type and const-expression information. */
4169 TREE_TYPE (result) = TREE_TYPE (arg);
4170 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4171 /* Mark this read now, since the expansion might be length 0. */
4172 mark_exp_read (arg);
4173 }
4174 else
4175 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4176 they will rarely be compared to anything. */
4177 SET_TYPE_STRUCTURAL_EQUALITY (result);
4178
4179 /* Determine which parameter packs will be expanded. */
4180 ppd.parameter_packs = &parameter_packs;
4181 ppd.visited = new hash_set<tree>;
4182 ppd.type_pack_expansion_p = TYPE_P (arg);
4183 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4184 delete ppd.visited;
4185
4186 /* Make sure we found some parameter packs. */
4187 if (parameter_packs == NULL_TREE)
4188 {
4189 if (complain & tf_error)
4190 {
4191 if (TYPE_P (arg))
4192 error ("expansion pattern %qT contains no parameter packs", arg);
4193 else
4194 error ("expansion pattern %qE contains no parameter packs", arg);
4195 }
4196 return error_mark_node;
4197 }
4198 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4199
4200 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4201
4202 return result;
4203 }
4204
4205 /* Checks T for any "bare" parameter packs, which have not yet been
4206 expanded, and issues an error if any are found. This operation can
4207 only be done on full expressions or types (e.g., an expression
4208 statement, "if" condition, etc.), because we could have expressions like:
4209
4210 foo(f(g(h(args)))...)
4211
4212 where "args" is a parameter pack. check_for_bare_parameter_packs
4213 should not be called for the subexpressions args, h(args),
4214 g(h(args)), or f(g(h(args))), because we would produce erroneous
4215 error messages.
4216
4217 Returns TRUE and emits an error if there were bare parameter packs,
4218 returns FALSE otherwise. */
4219 bool
4220 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4221 {
4222 tree parameter_packs = NULL_TREE;
4223 struct find_parameter_pack_data ppd;
4224
4225 if (!processing_template_decl || !t || t == error_mark_node)
4226 return false;
4227
4228 /* A lambda might use a parameter pack from the containing context. */
4229 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4230 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4231 return false;
4232
4233 if (TREE_CODE (t) == TYPE_DECL)
4234 t = TREE_TYPE (t);
4235
4236 ppd.parameter_packs = &parameter_packs;
4237 ppd.visited = new hash_set<tree>;
4238 ppd.type_pack_expansion_p = false;
4239 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4240 delete ppd.visited;
4241
4242 if (parameter_packs)
4243 {
4244 if (loc == UNKNOWN_LOCATION)
4245 loc = cp_expr_loc_or_input_loc (t);
4246 error_at (loc, "parameter packs not expanded with %<...%>:");
4247 while (parameter_packs)
4248 {
4249 tree pack = TREE_VALUE (parameter_packs);
4250 tree name = NULL_TREE;
4251
4252 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4253 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4254 name = TYPE_NAME (pack);
4255 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4256 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4257 else if (TREE_CODE (pack) == CALL_EXPR)
4258 name = DECL_NAME (CALL_EXPR_FN (pack));
4259 else
4260 name = DECL_NAME (pack);
4261
4262 if (name)
4263 inform (loc, " %qD", name);
4264 else
4265 inform (loc, " %s", "<anonymous>");
4266
4267 parameter_packs = TREE_CHAIN (parameter_packs);
4268 }
4269
4270 return true;
4271 }
4272
4273 return false;
4274 }
4275
4276 /* Expand any parameter packs that occur in the template arguments in
4277 ARGS. */
4278 tree
4279 expand_template_argument_pack (tree args)
4280 {
4281 if (args == error_mark_node)
4282 return error_mark_node;
4283
4284 tree result_args = NULL_TREE;
4285 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4286 int num_result_args = -1;
4287 int non_default_args_count = -1;
4288
4289 /* First, determine if we need to expand anything, and the number of
4290 slots we'll need. */
4291 for (in_arg = 0; in_arg < nargs; ++in_arg)
4292 {
4293 tree arg = TREE_VEC_ELT (args, in_arg);
4294 if (arg == NULL_TREE)
4295 return args;
4296 if (ARGUMENT_PACK_P (arg))
4297 {
4298 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4299 if (num_result_args < 0)
4300 num_result_args = in_arg + num_packed;
4301 else
4302 num_result_args += num_packed;
4303 }
4304 else
4305 {
4306 if (num_result_args >= 0)
4307 num_result_args++;
4308 }
4309 }
4310
4311 /* If no expansion is necessary, we're done. */
4312 if (num_result_args < 0)
4313 return args;
4314
4315 /* Expand arguments. */
4316 result_args = make_tree_vec (num_result_args);
4317 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4318 non_default_args_count =
4319 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4320 for (in_arg = 0; in_arg < nargs; ++in_arg)
4321 {
4322 tree arg = TREE_VEC_ELT (args, in_arg);
4323 if (ARGUMENT_PACK_P (arg))
4324 {
4325 tree packed = ARGUMENT_PACK_ARGS (arg);
4326 int i, num_packed = TREE_VEC_LENGTH (packed);
4327 for (i = 0; i < num_packed; ++i, ++out_arg)
4328 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4329 if (non_default_args_count > 0)
4330 non_default_args_count += num_packed - 1;
4331 }
4332 else
4333 {
4334 TREE_VEC_ELT (result_args, out_arg) = arg;
4335 ++out_arg;
4336 }
4337 }
4338 if (non_default_args_count >= 0)
4339 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4340 return result_args;
4341 }
4342
4343 /* Checks if DECL shadows a template parameter.
4344
4345 [temp.local]: A template-parameter shall not be redeclared within its
4346 scope (including nested scopes).
4347
4348 Emits an error and returns TRUE if the DECL shadows a parameter,
4349 returns FALSE otherwise. */
4350
4351 bool
4352 check_template_shadow (tree decl)
4353 {
4354 tree olddecl;
4355
4356 /* If we're not in a template, we can't possibly shadow a template
4357 parameter. */
4358 if (!current_template_parms)
4359 return true;
4360
4361 /* Figure out what we're shadowing. */
4362 decl = OVL_FIRST (decl);
4363 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4364
4365 /* If there's no previous binding for this name, we're not shadowing
4366 anything, let alone a template parameter. */
4367 if (!olddecl)
4368 return true;
4369
4370 /* If we're not shadowing a template parameter, we're done. Note
4371 that OLDDECL might be an OVERLOAD (or perhaps even an
4372 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4373 node. */
4374 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4375 return true;
4376
4377 /* We check for decl != olddecl to avoid bogus errors for using a
4378 name inside a class. We check TPFI to avoid duplicate errors for
4379 inline member templates. */
4380 if (decl == olddecl
4381 || (DECL_TEMPLATE_PARM_P (decl)
4382 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4383 return true;
4384
4385 /* Don't complain about the injected class name, as we've already
4386 complained about the class itself. */
4387 if (DECL_SELF_REFERENCE_P (decl))
4388 return false;
4389
4390 if (DECL_TEMPLATE_PARM_P (decl))
4391 error ("declaration of template parameter %q+D shadows "
4392 "template parameter", decl);
4393 else
4394 error ("declaration of %q+#D shadows template parameter", decl);
4395 inform (DECL_SOURCE_LOCATION (olddecl),
4396 "template parameter %qD declared here", olddecl);
4397 return false;
4398 }
4399
4400 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4401 ORIG_LEVEL, DECL, and TYPE. */
4402
4403 static tree
4404 build_template_parm_index (int index,
4405 int level,
4406 int orig_level,
4407 tree decl,
4408 tree type)
4409 {
4410 tree t = make_node (TEMPLATE_PARM_INDEX);
4411 TEMPLATE_PARM_IDX (t) = index;
4412 TEMPLATE_PARM_LEVEL (t) = level;
4413 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4414 TEMPLATE_PARM_DECL (t) = decl;
4415 TREE_TYPE (t) = type;
4416 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4417 TREE_READONLY (t) = TREE_READONLY (decl);
4418
4419 return t;
4420 }
4421
4422 /* Find the canonical type parameter for the given template type
4423 parameter. Returns the canonical type parameter, which may be TYPE
4424 if no such parameter existed. */
4425
4426 static tree
4427 canonical_type_parameter (tree type)
4428 {
4429 int idx = TEMPLATE_TYPE_IDX (type);
4430
4431 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4432
4433 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4434 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4435
4436 for (tree list = (*canonical_template_parms)[idx];
4437 list; list = TREE_CHAIN (list))
4438 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4439 return TREE_VALUE (list);
4440
4441 (*canonical_template_parms)[idx]
4442 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4443 return type;
4444 }
4445
4446 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4447 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4448 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4449 new one is created. */
4450
4451 static tree
4452 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4453 tsubst_flags_t complain)
4454 {
4455 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4456 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4457 != TEMPLATE_PARM_LEVEL (index) - levels)
4458 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4459 {
4460 tree orig_decl = TEMPLATE_PARM_DECL (index);
4461
4462 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4463 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4464 type);
4465 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4466 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4467 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4468 DECL_ARTIFICIAL (decl) = 1;
4469 SET_DECL_TEMPLATE_PARM_P (decl);
4470
4471 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4472 TEMPLATE_PARM_LEVEL (index) - levels,
4473 TEMPLATE_PARM_ORIG_LEVEL (index),
4474 decl, type);
4475 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4476 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4477 = TEMPLATE_PARM_PARAMETER_PACK (index);
4478
4479 /* Template template parameters need this. */
4480 tree inner = decl;
4481 if (TREE_CODE (decl) == TEMPLATE_DECL)
4482 {
4483 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4484 TYPE_DECL, DECL_NAME (decl), type);
4485 DECL_TEMPLATE_RESULT (decl) = inner;
4486 DECL_ARTIFICIAL (inner) = true;
4487 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4488 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4489 }
4490
4491 /* Attach the TPI to the decl. */
4492 if (TREE_CODE (inner) == TYPE_DECL)
4493 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4494 else
4495 DECL_INITIAL (decl) = tpi;
4496 }
4497
4498 return TEMPLATE_PARM_DESCENDANTS (index);
4499 }
4500
4501 /* Process information from new template parameter PARM and append it
4502 to the LIST being built. This new parameter is a non-type
4503 parameter iff IS_NON_TYPE is true. This new parameter is a
4504 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4505 is in PARM_LOC. */
4506
4507 tree
4508 process_template_parm (tree list, location_t parm_loc, tree parm,
4509 bool is_non_type, bool is_parameter_pack)
4510 {
4511 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4512 tree prev = NULL_TREE;
4513 int idx = 0;
4514
4515 if (list)
4516 {
4517 prev = tree_last (list);
4518
4519 tree p = TREE_VALUE (prev);
4520 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4521 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4522 else if (TREE_CODE (p) == PARM_DECL)
4523 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4524
4525 ++idx;
4526 }
4527
4528 tree decl = NULL_TREE;
4529 tree defval = TREE_PURPOSE (parm);
4530 tree constr = TREE_TYPE (parm);
4531
4532 if (is_non_type)
4533 {
4534 parm = TREE_VALUE (parm);
4535
4536 SET_DECL_TEMPLATE_PARM_P (parm);
4537
4538 if (TREE_TYPE (parm) != error_mark_node)
4539 {
4540 /* [temp.param]
4541
4542 The top-level cv-qualifiers on the template-parameter are
4543 ignored when determining its type. */
4544 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4545 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4546 TREE_TYPE (parm) = error_mark_node;
4547 else if (uses_parameter_packs (TREE_TYPE (parm))
4548 && !is_parameter_pack
4549 /* If we're in a nested template parameter list, the template
4550 template parameter could be a parameter pack. */
4551 && processing_template_parmlist == 1)
4552 {
4553 /* This template parameter is not a parameter pack, but it
4554 should be. Complain about "bare" parameter packs. */
4555 check_for_bare_parameter_packs (TREE_TYPE (parm));
4556
4557 /* Recover by calling this a parameter pack. */
4558 is_parameter_pack = true;
4559 }
4560 }
4561
4562 /* A template parameter is not modifiable. */
4563 TREE_CONSTANT (parm) = 1;
4564 TREE_READONLY (parm) = 1;
4565 decl = build_decl (parm_loc,
4566 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4567 TREE_CONSTANT (decl) = 1;
4568 TREE_READONLY (decl) = 1;
4569 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4570 = build_template_parm_index (idx, processing_template_decl,
4571 processing_template_decl,
4572 decl, TREE_TYPE (parm));
4573
4574 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4575 = is_parameter_pack;
4576 }
4577 else
4578 {
4579 tree t;
4580 parm = TREE_VALUE (TREE_VALUE (parm));
4581
4582 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4583 {
4584 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4585 /* This is for distinguishing between real templates and template
4586 template parameters */
4587 TREE_TYPE (parm) = t;
4588
4589 /* any_template_parm_r expects to be able to get the targs of a
4590 DECL_TEMPLATE_RESULT. */
4591 tree result = DECL_TEMPLATE_RESULT (parm);
4592 TREE_TYPE (result) = t;
4593 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4594 tree tinfo = build_template_info (parm, args);
4595 retrofit_lang_decl (result);
4596 DECL_TEMPLATE_INFO (result) = tinfo;
4597
4598 decl = parm;
4599 }
4600 else
4601 {
4602 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4603 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4604 decl = build_decl (parm_loc,
4605 TYPE_DECL, parm, t);
4606 }
4607
4608 TYPE_NAME (t) = decl;
4609 TYPE_STUB_DECL (t) = decl;
4610 parm = decl;
4611 TEMPLATE_TYPE_PARM_INDEX (t)
4612 = build_template_parm_index (idx, processing_template_decl,
4613 processing_template_decl,
4614 decl, TREE_TYPE (parm));
4615 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4616 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4617 SET_TYPE_STRUCTURAL_EQUALITY (t);
4618 else
4619 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4620 }
4621 DECL_ARTIFICIAL (decl) = 1;
4622 SET_DECL_TEMPLATE_PARM_P (decl);
4623
4624 /* Build requirements for the type/template parameter.
4625 This must be done after SET_DECL_TEMPLATE_PARM_P or
4626 process_template_parm could fail. */
4627 tree reqs = finish_shorthand_constraint (parm, constr);
4628
4629 decl = pushdecl (decl);
4630 if (!is_non_type)
4631 parm = decl;
4632
4633 /* Build the parameter node linking the parameter declaration,
4634 its default argument (if any), and its constraints (if any). */
4635 parm = build_tree_list (defval, parm);
4636 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4637
4638 if (prev)
4639 TREE_CHAIN (prev) = parm;
4640 else
4641 list = parm;
4642
4643 return list;
4644 }
4645
4646 /* The end of a template parameter list has been reached. Process the
4647 tree list into a parameter vector, converting each parameter into a more
4648 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4649 as PARM_DECLs. */
4650
4651 tree
4652 end_template_parm_list (tree parms)
4653 {
4654 tree saved_parmlist = make_tree_vec (list_length (parms));
4655
4656 /* Pop the dummy parameter level and add the real one. We do not
4657 morph the dummy parameter in place, as it might have been
4658 captured by a (nested) template-template-parm. */
4659 current_template_parms = TREE_CHAIN (current_template_parms);
4660
4661 current_template_parms
4662 = tree_cons (size_int (processing_template_decl),
4663 saved_parmlist, current_template_parms);
4664
4665 for (unsigned ix = 0; parms; ix++)
4666 {
4667 tree parm = parms;
4668 parms = TREE_CHAIN (parms);
4669 TREE_CHAIN (parm) = NULL_TREE;
4670
4671 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4672 }
4673
4674 --processing_template_parmlist;
4675
4676 return saved_parmlist;
4677 }
4678
4679 // Explicitly indicate the end of the template parameter list. We assume
4680 // that the current template parameters have been constructed and/or
4681 // managed explicitly, as when creating new template template parameters
4682 // from a shorthand constraint.
4683 void
4684 end_template_parm_list ()
4685 {
4686 --processing_template_parmlist;
4687 }
4688
4689 /* end_template_decl is called after a template declaration is seen. */
4690
4691 void
4692 end_template_decl (void)
4693 {
4694 reset_specialization ();
4695
4696 if (! processing_template_decl)
4697 return;
4698
4699 /* This matches the pushlevel in begin_template_parm_list. */
4700 finish_scope ();
4701
4702 --processing_template_decl;
4703 current_template_parms = TREE_CHAIN (current_template_parms);
4704 }
4705
4706 /* Takes a TREE_LIST representing a template parameter and convert it
4707 into an argument suitable to be passed to the type substitution
4708 functions. Note that If the TREE_LIST contains an error_mark
4709 node, the returned argument is error_mark_node. */
4710
4711 tree
4712 template_parm_to_arg (tree t)
4713 {
4714
4715 if (t == NULL_TREE
4716 || TREE_CODE (t) != TREE_LIST)
4717 return t;
4718
4719 if (error_operand_p (TREE_VALUE (t)))
4720 return error_mark_node;
4721
4722 t = TREE_VALUE (t);
4723
4724 if (TREE_CODE (t) == TYPE_DECL
4725 || TREE_CODE (t) == TEMPLATE_DECL)
4726 {
4727 t = TREE_TYPE (t);
4728
4729 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4730 {
4731 /* Turn this argument into a TYPE_ARGUMENT_PACK
4732 with a single element, which expands T. */
4733 tree vec = make_tree_vec (1);
4734 if (CHECKING_P)
4735 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4736
4737 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4738
4739 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4740 SET_ARGUMENT_PACK_ARGS (t, vec);
4741 }
4742 }
4743 else
4744 {
4745 t = DECL_INITIAL (t);
4746
4747 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4748 {
4749 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4750 with a single element, which expands T. */
4751 tree vec = make_tree_vec (1);
4752 if (CHECKING_P)
4753 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4754
4755 t = convert_from_reference (t);
4756 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4757
4758 t = make_node (NONTYPE_ARGUMENT_PACK);
4759 SET_ARGUMENT_PACK_ARGS (t, vec);
4760 }
4761 else
4762 t = convert_from_reference (t);
4763 }
4764 return t;
4765 }
4766
4767 /* Given a single level of template parameters (a TREE_VEC), return it
4768 as a set of template arguments. */
4769
4770 tree
4771 template_parms_level_to_args (tree parms)
4772 {
4773 tree a = copy_node (parms);
4774 TREE_TYPE (a) = NULL_TREE;
4775 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4776 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4777
4778 if (CHECKING_P)
4779 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4780
4781 return a;
4782 }
4783
4784 /* Given a set of template parameters, return them as a set of template
4785 arguments. The template parameters are represented as a TREE_VEC, in
4786 the form documented in cp-tree.h for template arguments. */
4787
4788 tree
4789 template_parms_to_args (tree parms)
4790 {
4791 tree header;
4792 tree args = NULL_TREE;
4793 int length = TMPL_PARMS_DEPTH (parms);
4794 int l = length;
4795
4796 /* If there is only one level of template parameters, we do not
4797 create a TREE_VEC of TREE_VECs. Instead, we return a single
4798 TREE_VEC containing the arguments. */
4799 if (length > 1)
4800 args = make_tree_vec (length);
4801
4802 for (header = parms; header; header = TREE_CHAIN (header))
4803 {
4804 tree a = template_parms_level_to_args (TREE_VALUE (header));
4805
4806 if (length > 1)
4807 TREE_VEC_ELT (args, --l) = a;
4808 else
4809 args = a;
4810 }
4811
4812 return args;
4813 }
4814
4815 /* Within the declaration of a template, return the currently active
4816 template parameters as an argument TREE_VEC. */
4817
4818 static tree
4819 current_template_args (void)
4820 {
4821 return template_parms_to_args (current_template_parms);
4822 }
4823
4824 /* Return the fully generic arguments for of TMPL, i.e. what
4825 current_template_args would be while parsing it. */
4826
4827 tree
4828 generic_targs_for (tree tmpl)
4829 {
4830 if (tmpl == NULL_TREE)
4831 return NULL_TREE;
4832 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4833 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4834 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4835 template parameter, it has no TEMPLATE_INFO; for a partial
4836 specialization, it has the arguments for the primary template, and we
4837 want the arguments for the partial specialization. */;
4838 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4839 if (tree ti = get_template_info (result))
4840 return TI_ARGS (ti);
4841 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4842 }
4843
4844 /* Update the declared TYPE by doing any lookups which were thought to be
4845 dependent, but are not now that we know the SCOPE of the declarator. */
4846
4847 tree
4848 maybe_update_decl_type (tree orig_type, tree scope)
4849 {
4850 tree type = orig_type;
4851
4852 if (type == NULL_TREE)
4853 return type;
4854
4855 if (TREE_CODE (orig_type) == TYPE_DECL)
4856 type = TREE_TYPE (type);
4857
4858 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4859 && dependent_type_p (type)
4860 /* Don't bother building up the args in this case. */
4861 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4862 {
4863 /* tsubst in the args corresponding to the template parameters,
4864 including auto if present. Most things will be unchanged, but
4865 make_typename_type and tsubst_qualified_id will resolve
4866 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4867 tree args = current_template_args ();
4868 tree auto_node = type_uses_auto (type);
4869 tree pushed;
4870 if (auto_node)
4871 {
4872 tree auto_vec = make_tree_vec (1);
4873 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4874 args = add_to_template_args (args, auto_vec);
4875 }
4876 pushed = push_scope (scope);
4877 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4878 if (pushed)
4879 pop_scope (scope);
4880 }
4881
4882 if (type == error_mark_node)
4883 return orig_type;
4884
4885 if (TREE_CODE (orig_type) == TYPE_DECL)
4886 {
4887 if (same_type_p (type, TREE_TYPE (orig_type)))
4888 type = orig_type;
4889 else
4890 type = TYPE_NAME (type);
4891 }
4892 return type;
4893 }
4894
4895 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4896 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4897 the new template is a member template. */
4898
4899 static tree
4900 build_template_decl (tree decl, tree parms, bool member_template_p)
4901 {
4902 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4903 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4904 DECL_TEMPLATE_PARMS (tmpl) = parms;
4905 DECL_TEMPLATE_RESULT (tmpl) = decl;
4906 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4907 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4908 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4909 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4910
4911 return tmpl;
4912 }
4913
4914 struct template_parm_data
4915 {
4916 /* The level of the template parameters we are currently
4917 processing. */
4918 int level;
4919
4920 /* The index of the specialization argument we are currently
4921 processing. */
4922 int current_arg;
4923
4924 /* An array whose size is the number of template parameters. The
4925 elements are nonzero if the parameter has been used in any one
4926 of the arguments processed so far. */
4927 int* parms;
4928
4929 /* An array whose size is the number of template arguments. The
4930 elements are nonzero if the argument makes use of template
4931 parameters of this level. */
4932 int* arg_uses_template_parms;
4933 };
4934
4935 /* Subroutine of push_template_decl used to see if each template
4936 parameter in a partial specialization is used in the explicit
4937 argument list. If T is of the LEVEL given in DATA (which is
4938 treated as a template_parm_data*), then DATA->PARMS is marked
4939 appropriately. */
4940
4941 static int
4942 mark_template_parm (tree t, void* data)
4943 {
4944 int level;
4945 int idx;
4946 struct template_parm_data* tpd = (struct template_parm_data*) data;
4947
4948 template_parm_level_and_index (t, &level, &idx);
4949
4950 if (level == tpd->level)
4951 {
4952 tpd->parms[idx] = 1;
4953 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4954 }
4955
4956 /* In C++17 the type of a non-type argument is a deduced context. */
4957 if (cxx_dialect >= cxx17
4958 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4959 for_each_template_parm (TREE_TYPE (t),
4960 &mark_template_parm,
4961 data,
4962 NULL,
4963 /*include_nondeduced_p=*/false);
4964
4965 /* Return zero so that for_each_template_parm will continue the
4966 traversal of the tree; we want to mark *every* template parm. */
4967 return 0;
4968 }
4969
4970 /* Process the partial specialization DECL. */
4971
4972 static tree
4973 process_partial_specialization (tree decl)
4974 {
4975 tree type = TREE_TYPE (decl);
4976 tree tinfo = get_template_info (decl);
4977 tree maintmpl = TI_TEMPLATE (tinfo);
4978 tree specargs = TI_ARGS (tinfo);
4979 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4980 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4981 tree inner_parms;
4982 tree inst;
4983 int nargs = TREE_VEC_LENGTH (inner_args);
4984 int ntparms;
4985 int i;
4986 bool did_error_intro = false;
4987 struct template_parm_data tpd;
4988 struct template_parm_data tpd2;
4989
4990 gcc_assert (current_template_parms);
4991
4992 /* A concept cannot be specialized. */
4993 if (flag_concepts && variable_concept_p (maintmpl))
4994 {
4995 error ("specialization of variable concept %q#D", maintmpl);
4996 return error_mark_node;
4997 }
4998
4999 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5000 ntparms = TREE_VEC_LENGTH (inner_parms);
5001
5002 /* We check that each of the template parameters given in the
5003 partial specialization is used in the argument list to the
5004 specialization. For example:
5005
5006 template <class T> struct S;
5007 template <class T> struct S<T*>;
5008
5009 The second declaration is OK because `T*' uses the template
5010 parameter T, whereas
5011
5012 template <class T> struct S<int>;
5013
5014 is no good. Even trickier is:
5015
5016 template <class T>
5017 struct S1
5018 {
5019 template <class U>
5020 struct S2;
5021 template <class U>
5022 struct S2<T>;
5023 };
5024
5025 The S2<T> declaration is actually invalid; it is a
5026 full-specialization. Of course,
5027
5028 template <class U>
5029 struct S2<T (*)(U)>;
5030
5031 or some such would have been OK. */
5032 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5033 tpd.parms = XALLOCAVEC (int, ntparms);
5034 memset (tpd.parms, 0, sizeof (int) * ntparms);
5035
5036 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5037 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5038 for (i = 0; i < nargs; ++i)
5039 {
5040 tpd.current_arg = i;
5041 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5042 &mark_template_parm,
5043 &tpd,
5044 NULL,
5045 /*include_nondeduced_p=*/false);
5046 }
5047 for (i = 0; i < ntparms; ++i)
5048 if (tpd.parms[i] == 0)
5049 {
5050 /* One of the template parms was not used in a deduced context in the
5051 specialization. */
5052 if (!did_error_intro)
5053 {
5054 error ("template parameters not deducible in "
5055 "partial specialization:");
5056 did_error_intro = true;
5057 }
5058
5059 inform (input_location, " %qD",
5060 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5061 }
5062
5063 if (did_error_intro)
5064 return error_mark_node;
5065
5066 /* [temp.class.spec]
5067
5068 The argument list of the specialization shall not be identical to
5069 the implicit argument list of the primary template. */
5070 tree main_args
5071 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5072 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5073 && (!flag_concepts
5074 || !strictly_subsumes (current_template_constraints (),
5075 main_args, maintmpl)))
5076 {
5077 if (!flag_concepts)
5078 error ("partial specialization %q+D does not specialize "
5079 "any template arguments; to define the primary template, "
5080 "remove the template argument list", decl);
5081 else
5082 error ("partial specialization %q+D does not specialize any "
5083 "template arguments and is not more constrained than "
5084 "the primary template; to define the primary template, "
5085 "remove the template argument list", decl);
5086 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5087 }
5088
5089 /* A partial specialization that replaces multiple parameters of the
5090 primary template with a pack expansion is less specialized for those
5091 parameters. */
5092 if (nargs < DECL_NTPARMS (maintmpl))
5093 {
5094 error ("partial specialization is not more specialized than the "
5095 "primary template because it replaces multiple parameters "
5096 "with a pack expansion");
5097 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5098 /* Avoid crash in process_partial_specialization. */
5099 return decl;
5100 }
5101
5102 else if (nargs > DECL_NTPARMS (maintmpl))
5103 {
5104 error ("too many arguments for partial specialization %qT", type);
5105 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5106 /* Avoid crash below. */
5107 return decl;
5108 }
5109
5110 /* If we aren't in a dependent class, we can actually try deduction. */
5111 else if (tpd.level == 1
5112 /* FIXME we should be able to handle a partial specialization of a
5113 partial instantiation, but currently we can't (c++/41727). */
5114 && TMPL_ARGS_DEPTH (specargs) == 1
5115 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5116 {
5117 auto_diagnostic_group d;
5118 if (permerror (input_location, "partial specialization %qD is not "
5119 "more specialized than", decl))
5120 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5121 maintmpl);
5122 }
5123
5124 /* [temp.class.spec]
5125
5126 A partially specialized non-type argument expression shall not
5127 involve template parameters of the partial specialization except
5128 when the argument expression is a simple identifier.
5129
5130 The type of a template parameter corresponding to a specialized
5131 non-type argument shall not be dependent on a parameter of the
5132 specialization.
5133
5134 Also, we verify that pack expansions only occur at the
5135 end of the argument list. */
5136 tpd2.parms = 0;
5137 for (i = 0; i < nargs; ++i)
5138 {
5139 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5140 tree arg = TREE_VEC_ELT (inner_args, i);
5141 tree packed_args = NULL_TREE;
5142 int j, len = 1;
5143
5144 if (ARGUMENT_PACK_P (arg))
5145 {
5146 /* Extract the arguments from the argument pack. We'll be
5147 iterating over these in the following loop. */
5148 packed_args = ARGUMENT_PACK_ARGS (arg);
5149 len = TREE_VEC_LENGTH (packed_args);
5150 }
5151
5152 for (j = 0; j < len; j++)
5153 {
5154 if (packed_args)
5155 /* Get the Jth argument in the parameter pack. */
5156 arg = TREE_VEC_ELT (packed_args, j);
5157
5158 if (PACK_EXPANSION_P (arg))
5159 {
5160 /* Pack expansions must come at the end of the
5161 argument list. */
5162 if ((packed_args && j < len - 1)
5163 || (!packed_args && i < nargs - 1))
5164 {
5165 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5166 error ("parameter pack argument %qE must be at the "
5167 "end of the template argument list", arg);
5168 else
5169 error ("parameter pack argument %qT must be at the "
5170 "end of the template argument list", arg);
5171 }
5172 }
5173
5174 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5175 /* We only care about the pattern. */
5176 arg = PACK_EXPANSION_PATTERN (arg);
5177
5178 if (/* These first two lines are the `non-type' bit. */
5179 !TYPE_P (arg)
5180 && TREE_CODE (arg) != TEMPLATE_DECL
5181 /* This next two lines are the `argument expression is not just a
5182 simple identifier' condition and also the `specialized
5183 non-type argument' bit. */
5184 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5185 && !((REFERENCE_REF_P (arg)
5186 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5187 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5188 {
5189 if ((!packed_args && tpd.arg_uses_template_parms[i])
5190 || (packed_args && uses_template_parms (arg)))
5191 error_at (cp_expr_loc_or_input_loc (arg),
5192 "template argument %qE involves template "
5193 "parameter(s)", arg);
5194 else
5195 {
5196 /* Look at the corresponding template parameter,
5197 marking which template parameters its type depends
5198 upon. */
5199 tree type = TREE_TYPE (parm);
5200
5201 if (!tpd2.parms)
5202 {
5203 /* We haven't yet initialized TPD2. Do so now. */
5204 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5205 /* The number of parameters here is the number in the
5206 main template, which, as checked in the assertion
5207 above, is NARGS. */
5208 tpd2.parms = XALLOCAVEC (int, nargs);
5209 tpd2.level =
5210 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5211 }
5212
5213 /* Mark the template parameters. But this time, we're
5214 looking for the template parameters of the main
5215 template, not in the specialization. */
5216 tpd2.current_arg = i;
5217 tpd2.arg_uses_template_parms[i] = 0;
5218 memset (tpd2.parms, 0, sizeof (int) * nargs);
5219 for_each_template_parm (type,
5220 &mark_template_parm,
5221 &tpd2,
5222 NULL,
5223 /*include_nondeduced_p=*/false);
5224
5225 if (tpd2.arg_uses_template_parms [i])
5226 {
5227 /* The type depended on some template parameters.
5228 If they are fully specialized in the
5229 specialization, that's OK. */
5230 int j;
5231 int count = 0;
5232 for (j = 0; j < nargs; ++j)
5233 if (tpd2.parms[j] != 0
5234 && tpd.arg_uses_template_parms [j])
5235 ++count;
5236 if (count != 0)
5237 error_n (input_location, count,
5238 "type %qT of template argument %qE depends "
5239 "on a template parameter",
5240 "type %qT of template argument %qE depends "
5241 "on template parameters",
5242 type,
5243 arg);
5244 }
5245 }
5246 }
5247 }
5248 }
5249
5250 /* We should only get here once. */
5251 if (TREE_CODE (decl) == TYPE_DECL)
5252 gcc_assert (!COMPLETE_TYPE_P (type));
5253
5254 // Build the template decl.
5255 tree tmpl = build_template_decl (decl, current_template_parms,
5256 DECL_MEMBER_TEMPLATE_P (maintmpl));
5257 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5258 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5259 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5260
5261 /* Give template template parms a DECL_CONTEXT of the template
5262 for which they are a parameter. */
5263 for (i = 0; i < ntparms; ++i)
5264 {
5265 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5266 if (TREE_CODE (parm) == TEMPLATE_DECL)
5267 DECL_CONTEXT (parm) = tmpl;
5268 }
5269
5270 if (VAR_P (decl))
5271 /* We didn't register this in check_explicit_specialization so we could
5272 wait until the constraints were set. */
5273 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5274 else
5275 associate_classtype_constraints (type);
5276
5277 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5278 = tree_cons (specargs, tmpl,
5279 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5280 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5281
5282 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5283 inst = TREE_CHAIN (inst))
5284 {
5285 tree instance = TREE_VALUE (inst);
5286 if (TYPE_P (instance)
5287 ? (COMPLETE_TYPE_P (instance)
5288 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5289 : DECL_TEMPLATE_INSTANTIATION (instance))
5290 {
5291 tree spec = most_specialized_partial_spec (instance, tf_none);
5292 tree inst_decl = (DECL_P (instance)
5293 ? instance : TYPE_NAME (instance));
5294 if (!spec)
5295 /* OK */;
5296 else if (spec == error_mark_node)
5297 permerror (input_location,
5298 "declaration of %qD ambiguates earlier template "
5299 "instantiation for %qD", decl, inst_decl);
5300 else if (TREE_VALUE (spec) == tmpl)
5301 permerror (input_location,
5302 "partial specialization of %qD after instantiation "
5303 "of %qD", decl, inst_decl);
5304 }
5305 }
5306
5307 return decl;
5308 }
5309
5310 /* PARM is a template parameter of some form; return the corresponding
5311 TEMPLATE_PARM_INDEX. */
5312
5313 static tree
5314 get_template_parm_index (tree parm)
5315 {
5316 if (TREE_CODE (parm) == PARM_DECL
5317 || TREE_CODE (parm) == CONST_DECL)
5318 parm = DECL_INITIAL (parm);
5319 else if (TREE_CODE (parm) == TYPE_DECL
5320 || TREE_CODE (parm) == TEMPLATE_DECL)
5321 parm = TREE_TYPE (parm);
5322 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5323 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5324 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5325 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5326 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5327 return parm;
5328 }
5329
5330 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5331 parameter packs used by the template parameter PARM. */
5332
5333 static void
5334 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5335 {
5336 /* A type parm can't refer to another parm. */
5337 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5338 return;
5339 else if (TREE_CODE (parm) == PARM_DECL)
5340 {
5341 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5342 ppd, ppd->visited);
5343 return;
5344 }
5345
5346 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5347
5348 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5349 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5350 {
5351 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5352 if (template_parameter_pack_p (p))
5353 /* Any packs in the type are expanded by this parameter. */;
5354 else
5355 fixed_parameter_pack_p_1 (p, ppd);
5356 }
5357 }
5358
5359 /* PARM is a template parameter pack. Return any parameter packs used in
5360 its type or the type of any of its template parameters. If there are
5361 any such packs, it will be instantiated into a fixed template parameter
5362 list by partial instantiation rather than be fully deduced. */
5363
5364 tree
5365 fixed_parameter_pack_p (tree parm)
5366 {
5367 /* This can only be true in a member template. */
5368 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5369 return NULL_TREE;
5370 /* This can only be true for a parameter pack. */
5371 if (!template_parameter_pack_p (parm))
5372 return NULL_TREE;
5373 /* A type parm can't refer to another parm. */
5374 if (TREE_CODE (parm) == TYPE_DECL)
5375 return NULL_TREE;
5376
5377 tree parameter_packs = NULL_TREE;
5378 struct find_parameter_pack_data ppd;
5379 ppd.parameter_packs = &parameter_packs;
5380 ppd.visited = new hash_set<tree>;
5381 ppd.type_pack_expansion_p = false;
5382
5383 fixed_parameter_pack_p_1 (parm, &ppd);
5384
5385 delete ppd.visited;
5386 return parameter_packs;
5387 }
5388
5389 /* Check that a template declaration's use of default arguments and
5390 parameter packs is not invalid. Here, PARMS are the template
5391 parameters. IS_PRIMARY is true if DECL is the thing declared by
5392 a primary template. IS_PARTIAL is true if DECL is a partial
5393 specialization.
5394
5395 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5396 function template declaration or a friend class template
5397 declaration. In the function case, 1 indicates a declaration, 2
5398 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5399 emitted for extraneous default arguments.
5400
5401 Returns TRUE if there were no errors found, FALSE otherwise. */
5402
5403 bool
5404 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5405 bool is_partial, int is_friend_decl)
5406 {
5407 const char *msg;
5408 int last_level_to_check;
5409 tree parm_level;
5410 bool no_errors = true;
5411
5412 /* [temp.param]
5413
5414 A default template-argument shall not be specified in a
5415 function template declaration or a function template definition, nor
5416 in the template-parameter-list of the definition of a member of a
5417 class template. */
5418
5419 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5420 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5421 /* You can't have a function template declaration in a local
5422 scope, nor you can you define a member of a class template in a
5423 local scope. */
5424 return true;
5425
5426 if ((TREE_CODE (decl) == TYPE_DECL
5427 && TREE_TYPE (decl)
5428 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5429 || (TREE_CODE (decl) == FUNCTION_DECL
5430 && LAMBDA_FUNCTION_P (decl)))
5431 /* A lambda doesn't have an explicit declaration; don't complain
5432 about the parms of the enclosing class. */
5433 return true;
5434
5435 if (current_class_type
5436 && !TYPE_BEING_DEFINED (current_class_type)
5437 && DECL_LANG_SPECIFIC (decl)
5438 && DECL_DECLARES_FUNCTION_P (decl)
5439 /* If this is either a friend defined in the scope of the class
5440 or a member function. */
5441 && (DECL_FUNCTION_MEMBER_P (decl)
5442 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5443 : DECL_FRIEND_CONTEXT (decl)
5444 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5445 : false)
5446 /* And, if it was a member function, it really was defined in
5447 the scope of the class. */
5448 && (!DECL_FUNCTION_MEMBER_P (decl)
5449 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5450 /* We already checked these parameters when the template was
5451 declared, so there's no need to do it again now. This function
5452 was defined in class scope, but we're processing its body now
5453 that the class is complete. */
5454 return true;
5455
5456 /* Core issue 226 (C++0x only): the following only applies to class
5457 templates. */
5458 if (is_primary
5459 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5460 {
5461 /* [temp.param]
5462
5463 If a template-parameter has a default template-argument, all
5464 subsequent template-parameters shall have a default
5465 template-argument supplied. */
5466 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5467 {
5468 tree inner_parms = TREE_VALUE (parm_level);
5469 int ntparms = TREE_VEC_LENGTH (inner_parms);
5470 int seen_def_arg_p = 0;
5471 int i;
5472
5473 for (i = 0; i < ntparms; ++i)
5474 {
5475 tree parm = TREE_VEC_ELT (inner_parms, i);
5476
5477 if (parm == error_mark_node)
5478 continue;
5479
5480 if (TREE_PURPOSE (parm))
5481 seen_def_arg_p = 1;
5482 else if (seen_def_arg_p
5483 && !template_parameter_pack_p (TREE_VALUE (parm)))
5484 {
5485 error ("no default argument for %qD", TREE_VALUE (parm));
5486 /* For better subsequent error-recovery, we indicate that
5487 there should have been a default argument. */
5488 TREE_PURPOSE (parm) = error_mark_node;
5489 no_errors = false;
5490 }
5491 else if (!is_partial
5492 && !is_friend_decl
5493 /* Don't complain about an enclosing partial
5494 specialization. */
5495 && parm_level == parms
5496 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5497 && i < ntparms - 1
5498 && template_parameter_pack_p (TREE_VALUE (parm))
5499 /* A fixed parameter pack will be partially
5500 instantiated into a fixed length list. */
5501 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5502 {
5503 /* A primary class template, primary variable template
5504 (DR 2032), or alias template can only have one
5505 parameter pack, at the end of the template
5506 parameter list. */
5507
5508 error ("parameter pack %q+D must be at the end of the"
5509 " template parameter list", TREE_VALUE (parm));
5510
5511 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5512 = error_mark_node;
5513 no_errors = false;
5514 }
5515 }
5516 }
5517 }
5518
5519 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5520 || is_partial
5521 || !is_primary
5522 || is_friend_decl)
5523 /* For an ordinary class template, default template arguments are
5524 allowed at the innermost level, e.g.:
5525 template <class T = int>
5526 struct S {};
5527 but, in a partial specialization, they're not allowed even
5528 there, as we have in [temp.class.spec]:
5529
5530 The template parameter list of a specialization shall not
5531 contain default template argument values.
5532
5533 So, for a partial specialization, or for a function template
5534 (in C++98/C++03), we look at all of them. */
5535 ;
5536 else
5537 /* But, for a primary class template that is not a partial
5538 specialization we look at all template parameters except the
5539 innermost ones. */
5540 parms = TREE_CHAIN (parms);
5541
5542 /* Figure out what error message to issue. */
5543 if (is_friend_decl == 2)
5544 msg = G_("default template arguments may not be used in function template "
5545 "friend re-declaration");
5546 else if (is_friend_decl)
5547 msg = G_("default template arguments may not be used in template "
5548 "friend declarations");
5549 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5550 msg = G_("default template arguments may not be used in function templates "
5551 "without %<-std=c++11%> or %<-std=gnu++11%>");
5552 else if (is_partial)
5553 msg = G_("default template arguments may not be used in "
5554 "partial specializations");
5555 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5556 msg = G_("default argument for template parameter for class enclosing %qD");
5557 else
5558 /* Per [temp.param]/9, "A default template-argument shall not be
5559 specified in the template-parameter-lists of the definition of
5560 a member of a class template that appears outside of the member's
5561 class.", thus if we aren't handling a member of a class template
5562 there is no need to examine the parameters. */
5563 return true;
5564
5565 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5566 /* If we're inside a class definition, there's no need to
5567 examine the parameters to the class itself. On the one
5568 hand, they will be checked when the class is defined, and,
5569 on the other, default arguments are valid in things like:
5570 template <class T = double>
5571 struct S { template <class U> void f(U); };
5572 Here the default argument for `S' has no bearing on the
5573 declaration of `f'. */
5574 last_level_to_check = template_class_depth (current_class_type) + 1;
5575 else
5576 /* Check everything. */
5577 last_level_to_check = 0;
5578
5579 for (parm_level = parms;
5580 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5581 parm_level = TREE_CHAIN (parm_level))
5582 {
5583 tree inner_parms = TREE_VALUE (parm_level);
5584 int i;
5585 int ntparms;
5586
5587 ntparms = TREE_VEC_LENGTH (inner_parms);
5588 for (i = 0; i < ntparms; ++i)
5589 {
5590 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5591 continue;
5592
5593 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5594 {
5595 if (msg)
5596 {
5597 no_errors = false;
5598 if (is_friend_decl == 2)
5599 return no_errors;
5600
5601 error (msg, decl);
5602 msg = 0;
5603 }
5604
5605 /* Clear out the default argument so that we are not
5606 confused later. */
5607 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5608 }
5609 }
5610
5611 /* At this point, if we're still interested in issuing messages,
5612 they must apply to classes surrounding the object declared. */
5613 if (msg)
5614 msg = G_("default argument for template parameter for class "
5615 "enclosing %qD");
5616 }
5617
5618 return no_errors;
5619 }
5620
5621 /* Worker for push_template_decl_real, called via
5622 for_each_template_parm. DATA is really an int, indicating the
5623 level of the parameters we are interested in. If T is a template
5624 parameter of that level, return nonzero. */
5625
5626 static int
5627 template_parm_this_level_p (tree t, void* data)
5628 {
5629 int this_level = *(int *)data;
5630 int level;
5631
5632 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5633 level = TEMPLATE_PARM_LEVEL (t);
5634 else
5635 level = TEMPLATE_TYPE_LEVEL (t);
5636 return level == this_level;
5637 }
5638
5639 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5640 DATA is really an int, indicating the innermost outer level of parameters.
5641 If T is a template parameter of that level or further out, return
5642 nonzero. */
5643
5644 static int
5645 template_parm_outer_level (tree t, void *data)
5646 {
5647 int this_level = *(int *)data;
5648 int level;
5649
5650 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5651 level = TEMPLATE_PARM_LEVEL (t);
5652 else
5653 level = TEMPLATE_TYPE_LEVEL (t);
5654 return level <= this_level;
5655 }
5656
5657 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5658 parameters given by current_template_args, or reuses a
5659 previously existing one, if appropriate. Returns the DECL, or an
5660 equivalent one, if it is replaced via a call to duplicate_decls.
5661
5662 If IS_FRIEND is true, DECL is a friend declaration. */
5663
5664 tree
5665 push_template_decl_real (tree decl, bool is_friend)
5666 {
5667 tree tmpl;
5668 tree args;
5669 tree info;
5670 tree ctx;
5671 bool is_primary;
5672 bool is_partial;
5673 int new_template_p = 0;
5674 /* True if the template is a member template, in the sense of
5675 [temp.mem]. */
5676 bool member_template_p = false;
5677
5678 if (decl == error_mark_node || !current_template_parms)
5679 return error_mark_node;
5680
5681 /* See if this is a partial specialization. */
5682 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5683 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5684 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5685 || (VAR_P (decl)
5686 && DECL_LANG_SPECIFIC (decl)
5687 && DECL_TEMPLATE_SPECIALIZATION (decl)
5688 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5689
5690 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5691 is_friend = true;
5692
5693 if (is_friend)
5694 /* For a friend, we want the context of the friend, not
5695 the type of which it is a friend. */
5696 ctx = CP_DECL_CONTEXT (decl);
5697 else if (CP_DECL_CONTEXT (decl)
5698 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5699 /* In the case of a virtual function, we want the class in which
5700 it is defined. */
5701 ctx = CP_DECL_CONTEXT (decl);
5702 else
5703 /* Otherwise, if we're currently defining some class, the DECL
5704 is assumed to be a member of the class. */
5705 ctx = current_scope ();
5706
5707 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5708 ctx = NULL_TREE;
5709
5710 if (!DECL_CONTEXT (decl))
5711 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5712
5713 /* See if this is a primary template. */
5714 if (is_friend && ctx
5715 && uses_template_parms_level (ctx, processing_template_decl))
5716 /* A friend template that specifies a class context, i.e.
5717 template <typename T> friend void A<T>::f();
5718 is not primary. */
5719 is_primary = false;
5720 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5721 is_primary = false;
5722 else
5723 is_primary = template_parm_scope_p ();
5724
5725 if (is_primary)
5726 {
5727 warning (OPT_Wtemplates, "template %qD declared", decl);
5728
5729 if (DECL_CLASS_SCOPE_P (decl))
5730 member_template_p = true;
5731 if (TREE_CODE (decl) == TYPE_DECL
5732 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5733 {
5734 error ("template class without a name");
5735 return error_mark_node;
5736 }
5737 else if (TREE_CODE (decl) == FUNCTION_DECL)
5738 {
5739 if (member_template_p)
5740 {
5741 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5742 error ("member template %qD may not have virt-specifiers", decl);
5743 }
5744 if (DECL_DESTRUCTOR_P (decl))
5745 {
5746 /* [temp.mem]
5747
5748 A destructor shall not be a member template. */
5749 error_at (DECL_SOURCE_LOCATION (decl),
5750 "destructor %qD declared as member template", decl);
5751 return error_mark_node;
5752 }
5753 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5754 && (!prototype_p (TREE_TYPE (decl))
5755 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5756 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5757 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5758 == void_list_node)))
5759 {
5760 /* [basic.stc.dynamic.allocation]
5761
5762 An allocation function can be a function
5763 template. ... Template allocation functions shall
5764 have two or more parameters. */
5765 error ("invalid template declaration of %qD", decl);
5766 return error_mark_node;
5767 }
5768 }
5769 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5770 && CLASS_TYPE_P (TREE_TYPE (decl)))
5771 {
5772 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5773 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5774 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5775 {
5776 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5777 if (TREE_CODE (t) == TYPE_DECL)
5778 t = TREE_TYPE (t);
5779 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5780 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5781 }
5782 }
5783 else if (TREE_CODE (decl) == TYPE_DECL
5784 && TYPE_DECL_ALIAS_P (decl))
5785 /* alias-declaration */
5786 gcc_assert (!DECL_ARTIFICIAL (decl));
5787 else if (VAR_P (decl))
5788 /* C++14 variable template. */;
5789 else if (TREE_CODE (decl) == CONCEPT_DECL)
5790 /* C++20 concept definitions. */;
5791 else
5792 {
5793 error ("template declaration of %q#D", decl);
5794 return error_mark_node;
5795 }
5796 }
5797
5798 /* Check to see that the rules regarding the use of default
5799 arguments are not being violated. We check args for a friend
5800 functions when we know whether it's a definition, introducing
5801 declaration or re-declaration. */
5802 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5803 check_default_tmpl_args (decl, current_template_parms,
5804 is_primary, is_partial, is_friend);
5805
5806 /* Ensure that there are no parameter packs in the type of this
5807 declaration that have not been expanded. */
5808 if (TREE_CODE (decl) == FUNCTION_DECL)
5809 {
5810 /* Check each of the arguments individually to see if there are
5811 any bare parameter packs. */
5812 tree type = TREE_TYPE (decl);
5813 tree arg = DECL_ARGUMENTS (decl);
5814 tree argtype = TYPE_ARG_TYPES (type);
5815
5816 while (arg && argtype)
5817 {
5818 if (!DECL_PACK_P (arg)
5819 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5820 {
5821 /* This is a PARM_DECL that contains unexpanded parameter
5822 packs. We have already complained about this in the
5823 check_for_bare_parameter_packs call, so just replace
5824 these types with ERROR_MARK_NODE. */
5825 TREE_TYPE (arg) = error_mark_node;
5826 TREE_VALUE (argtype) = error_mark_node;
5827 }
5828
5829 arg = DECL_CHAIN (arg);
5830 argtype = TREE_CHAIN (argtype);
5831 }
5832
5833 /* Check for bare parameter packs in the return type and the
5834 exception specifiers. */
5835 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5836 /* Errors were already issued, set return type to int
5837 as the frontend doesn't expect error_mark_node as
5838 the return type. */
5839 TREE_TYPE (type) = integer_type_node;
5840 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5841 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5842 }
5843 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5844 ? DECL_ORIGINAL_TYPE (decl)
5845 : TREE_TYPE (decl)))
5846 {
5847 TREE_TYPE (decl) = error_mark_node;
5848 return error_mark_node;
5849 }
5850
5851 if (is_partial)
5852 return process_partial_specialization (decl);
5853
5854 args = current_template_args ();
5855
5856 if (!ctx
5857 || TREE_CODE (ctx) == FUNCTION_DECL
5858 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5859 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5860 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5861 {
5862 if (DECL_LANG_SPECIFIC (decl)
5863 && DECL_TEMPLATE_INFO (decl)
5864 && DECL_TI_TEMPLATE (decl))
5865 tmpl = DECL_TI_TEMPLATE (decl);
5866 /* If DECL is a TYPE_DECL for a class-template, then there won't
5867 be DECL_LANG_SPECIFIC. The information equivalent to
5868 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5869 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5870 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5871 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5872 {
5873 /* Since a template declaration already existed for this
5874 class-type, we must be redeclaring it here. Make sure
5875 that the redeclaration is valid. */
5876 redeclare_class_template (TREE_TYPE (decl),
5877 current_template_parms,
5878 current_template_constraints ());
5879 /* We don't need to create a new TEMPLATE_DECL; just use the
5880 one we already had. */
5881 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5882 }
5883 else
5884 {
5885 tmpl = build_template_decl (decl, current_template_parms,
5886 member_template_p);
5887 new_template_p = 1;
5888
5889 if (DECL_LANG_SPECIFIC (decl)
5890 && DECL_TEMPLATE_SPECIALIZATION (decl))
5891 {
5892 /* A specialization of a member template of a template
5893 class. */
5894 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5895 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5896 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5897 }
5898 }
5899 }
5900 else
5901 {
5902 tree a, t, current, parms;
5903 int i;
5904 tree tinfo = get_template_info (decl);
5905
5906 if (!tinfo)
5907 {
5908 error ("template definition of non-template %q#D", decl);
5909 return error_mark_node;
5910 }
5911
5912 tmpl = TI_TEMPLATE (tinfo);
5913
5914 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5915 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5916 && DECL_TEMPLATE_SPECIALIZATION (decl)
5917 && DECL_MEMBER_TEMPLATE_P (tmpl))
5918 {
5919 tree new_tmpl;
5920
5921 /* The declaration is a specialization of a member
5922 template, declared outside the class. Therefore, the
5923 innermost template arguments will be NULL, so we
5924 replace them with the arguments determined by the
5925 earlier call to check_explicit_specialization. */
5926 args = DECL_TI_ARGS (decl);
5927
5928 new_tmpl
5929 = build_template_decl (decl, current_template_parms,
5930 member_template_p);
5931 DECL_TI_TEMPLATE (decl) = new_tmpl;
5932 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5933 DECL_TEMPLATE_INFO (new_tmpl)
5934 = build_template_info (tmpl, args);
5935
5936 register_specialization (new_tmpl,
5937 most_general_template (tmpl),
5938 args,
5939 is_friend, 0);
5940 return decl;
5941 }
5942
5943 /* Make sure the template headers we got make sense. */
5944
5945 parms = DECL_TEMPLATE_PARMS (tmpl);
5946 i = TMPL_PARMS_DEPTH (parms);
5947 if (TMPL_ARGS_DEPTH (args) != i)
5948 {
5949 error ("expected %d levels of template parms for %q#D, got %d",
5950 i, decl, TMPL_ARGS_DEPTH (args));
5951 DECL_INTERFACE_KNOWN (decl) = 1;
5952 return error_mark_node;
5953 }
5954 else
5955 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5956 {
5957 a = TMPL_ARGS_LEVEL (args, i);
5958 t = INNERMOST_TEMPLATE_PARMS (parms);
5959
5960 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5961 {
5962 if (current == decl)
5963 error ("got %d template parameters for %q#D",
5964 TREE_VEC_LENGTH (a), decl);
5965 else
5966 error ("got %d template parameters for %q#T",
5967 TREE_VEC_LENGTH (a), current);
5968 error (" but %d required", TREE_VEC_LENGTH (t));
5969 /* Avoid crash in import_export_decl. */
5970 DECL_INTERFACE_KNOWN (decl) = 1;
5971 return error_mark_node;
5972 }
5973
5974 if (current == decl)
5975 current = ctx;
5976 else if (current == NULL_TREE)
5977 /* Can happen in erroneous input. */
5978 break;
5979 else
5980 current = get_containing_scope (current);
5981 }
5982
5983 /* Check that the parms are used in the appropriate qualifying scopes
5984 in the declarator. */
5985 if (!comp_template_args
5986 (TI_ARGS (tinfo),
5987 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5988 {
5989 error ("template arguments to %qD do not match original "
5990 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5991 if (!uses_template_parms (TI_ARGS (tinfo)))
5992 inform (input_location, "use %<template<>%> for"
5993 " an explicit specialization");
5994 /* Avoid crash in import_export_decl. */
5995 DECL_INTERFACE_KNOWN (decl) = 1;
5996 return error_mark_node;
5997 }
5998 }
5999
6000 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
6001
6002 if (new_template_p)
6003 {
6004 /* Push template declarations for global functions and types.
6005 Note that we do not try to push a global template friend
6006 declared in a template class; such a thing may well depend on
6007 the template parameters of the class and we'll push it when
6008 instantiating the befriending class. */
6009 if (!ctx
6010 && !(is_friend && template_class_depth (current_class_type) > 0))
6011 {
6012 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6013 if (tmpl == error_mark_node)
6014 return error_mark_node;
6015
6016 /* Hide template friend classes that haven't been declared yet. */
6017 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6018 {
6019 DECL_ANTICIPATED (tmpl) = 1;
6020 DECL_FRIEND_P (tmpl) = 1;
6021 }
6022 }
6023 }
6024 else
6025 /* The type may have been completed, or (erroneously) changed. */
6026 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6027
6028 if (is_primary)
6029 {
6030 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6031
6032 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6033
6034 /* Give template template parms a DECL_CONTEXT of the template
6035 for which they are a parameter. */
6036 parms = INNERMOST_TEMPLATE_PARMS (parms);
6037 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6038 {
6039 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6040 if (TREE_CODE (parm) == TEMPLATE_DECL)
6041 DECL_CONTEXT (parm) = tmpl;
6042 }
6043
6044 if (TREE_CODE (decl) == TYPE_DECL
6045 && TYPE_DECL_ALIAS_P (decl))
6046 {
6047 if (tree constr
6048 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6049 {
6050 /* ??? Why don't we do this here for all templates? */
6051 constr = build_constraints (constr, NULL_TREE);
6052 set_constraints (decl, constr);
6053 }
6054 if (complex_alias_template_p (tmpl))
6055 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6056 }
6057 }
6058
6059 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6060 back to its most general template. If TMPL is a specialization,
6061 ARGS may only have the innermost set of arguments. Add the missing
6062 argument levels if necessary. */
6063 if (DECL_TEMPLATE_INFO (tmpl))
6064 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6065
6066 info = build_template_info (tmpl, args);
6067
6068 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6069 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6070 else
6071 {
6072 if (is_primary)
6073 retrofit_lang_decl (decl);
6074 if (DECL_LANG_SPECIFIC (decl)
6075 && !(VAR_OR_FUNCTION_DECL_P (decl)
6076 && DECL_LOCAL_DECL_P (decl)
6077 /* OMP reductions still need a template header. */
6078 && !(TREE_CODE (decl) == FUNCTION_DECL
6079 && DECL_OMP_DECLARE_REDUCTION_P (decl))))
6080 DECL_TEMPLATE_INFO (decl) = info;
6081 }
6082
6083 if (flag_implicit_templates
6084 && !is_friend
6085 && TREE_PUBLIC (decl)
6086 && VAR_OR_FUNCTION_DECL_P (decl))
6087 /* Set DECL_COMDAT on template instantiations; if we force
6088 them to be emitted by explicit instantiation,
6089 mark_needed will tell cgraph to do the right thing. */
6090 DECL_COMDAT (decl) = true;
6091
6092 return DECL_TEMPLATE_RESULT (tmpl);
6093 }
6094
6095 tree
6096 push_template_decl (tree decl)
6097 {
6098 return push_template_decl_real (decl, false);
6099 }
6100
6101 /* FN is an inheriting constructor that inherits from the constructor
6102 template INHERITED; turn FN into a constructor template with a matching
6103 template header. */
6104
6105 tree
6106 add_inherited_template_parms (tree fn, tree inherited)
6107 {
6108 tree inner_parms
6109 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6110 inner_parms = copy_node (inner_parms);
6111 tree parms
6112 = tree_cons (size_int (processing_template_decl + 1),
6113 inner_parms, current_template_parms);
6114 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6115 tree args = template_parms_to_args (parms);
6116 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6117 DECL_ARTIFICIAL (tmpl) = true;
6118 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6119 return tmpl;
6120 }
6121
6122 /* Called when a class template TYPE is redeclared with the indicated
6123 template PARMS, e.g.:
6124
6125 template <class T> struct S;
6126 template <class T> struct S {}; */
6127
6128 bool
6129 redeclare_class_template (tree type, tree parms, tree cons)
6130 {
6131 tree tmpl;
6132 tree tmpl_parms;
6133 int i;
6134
6135 if (!TYPE_TEMPLATE_INFO (type))
6136 {
6137 error ("%qT is not a template type", type);
6138 return false;
6139 }
6140
6141 tmpl = TYPE_TI_TEMPLATE (type);
6142 if (!PRIMARY_TEMPLATE_P (tmpl))
6143 /* The type is nested in some template class. Nothing to worry
6144 about here; there are no new template parameters for the nested
6145 type. */
6146 return true;
6147
6148 if (!parms)
6149 {
6150 error ("template specifiers not specified in declaration of %qD",
6151 tmpl);
6152 return false;
6153 }
6154
6155 parms = INNERMOST_TEMPLATE_PARMS (parms);
6156 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6157
6158 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6159 {
6160 error_n (input_location, TREE_VEC_LENGTH (parms),
6161 "redeclared with %d template parameter",
6162 "redeclared with %d template parameters",
6163 TREE_VEC_LENGTH (parms));
6164 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6165 "previous declaration %qD used %d template parameter",
6166 "previous declaration %qD used %d template parameters",
6167 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6168 return false;
6169 }
6170
6171 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6172 {
6173 tree tmpl_parm;
6174 tree parm;
6175 tree tmpl_default;
6176 tree parm_default;
6177
6178 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6179 || TREE_VEC_ELT (parms, i) == error_mark_node)
6180 continue;
6181
6182 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6183 if (error_operand_p (tmpl_parm))
6184 return false;
6185
6186 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6187 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6188 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6189
6190 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6191 TEMPLATE_DECL. */
6192 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6193 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6194 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6195 || (TREE_CODE (tmpl_parm) != PARM_DECL
6196 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6197 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6198 || (TREE_CODE (tmpl_parm) == PARM_DECL
6199 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6200 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6201 {
6202 auto_diagnostic_group d;
6203 error ("template parameter %q+#D", tmpl_parm);
6204 inform (input_location, "redeclared here as %q#D", parm);
6205 return false;
6206 }
6207
6208 /* The parameters can be declared to introduce different
6209 constraints. */
6210 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6211 tree p2 = TREE_VEC_ELT (parms, i);
6212 if (!template_parameter_constraints_equivalent_p (p1, p2))
6213 {
6214 auto_diagnostic_group d;
6215 error ("declaration of template parameter %q+#D with different "
6216 "constraints", parm);
6217 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6218 "original declaration appeared here");
6219 return false;
6220 }
6221
6222 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6223 {
6224 /* We have in [temp.param]:
6225
6226 A template-parameter may not be given default arguments
6227 by two different declarations in the same scope. */
6228 auto_diagnostic_group d;
6229 error_at (input_location, "redefinition of default argument for %q#D", parm);
6230 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6231 "original definition appeared here");
6232 return false;
6233 }
6234
6235 if (parm_default != NULL_TREE)
6236 /* Update the previous template parameters (which are the ones
6237 that will really count) with the new default value. */
6238 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6239 else if (tmpl_default != NULL_TREE)
6240 /* Update the new parameters, too; they'll be used as the
6241 parameters for any members. */
6242 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6243
6244 /* Give each template template parm in this redeclaration a
6245 DECL_CONTEXT of the template for which they are a parameter. */
6246 if (TREE_CODE (parm) == TEMPLATE_DECL)
6247 {
6248 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6249 DECL_CONTEXT (parm) = tmpl;
6250 }
6251
6252 if (TREE_CODE (parm) == TYPE_DECL)
6253 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6254 }
6255
6256 tree ci = get_constraints (tmpl);
6257 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6258 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6259
6260 /* Two classes with different constraints declare different entities. */
6261 if (!cp_tree_equal (req1, req2))
6262 {
6263 auto_diagnostic_group d;
6264 error_at (input_location, "redeclaration %q#D with different "
6265 "constraints", tmpl);
6266 inform (DECL_SOURCE_LOCATION (tmpl),
6267 "original declaration appeared here");
6268 return false;
6269 }
6270
6271 return true;
6272 }
6273
6274 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6275 to be used when the caller has already checked
6276 (processing_template_decl
6277 && !instantiation_dependent_expression_p (expr)
6278 && potential_constant_expression (expr))
6279 and cleared processing_template_decl. */
6280
6281 tree
6282 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6283 {
6284 return tsubst_copy_and_build (expr,
6285 /*args=*/NULL_TREE,
6286 complain,
6287 /*in_decl=*/NULL_TREE,
6288 /*function_p=*/false,
6289 /*integral_constant_expression_p=*/true);
6290 }
6291
6292 /* Simplify EXPR if it is a non-dependent expression. Returns the
6293 (possibly simplified) expression. */
6294
6295 tree
6296 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6297 {
6298 if (expr == NULL_TREE)
6299 return NULL_TREE;
6300
6301 /* If we're in a template, but EXPR isn't value dependent, simplify
6302 it. We're supposed to treat:
6303
6304 template <typename T> void f(T[1 + 1]);
6305 template <typename T> void f(T[2]);
6306
6307 as two declarations of the same function, for example. */
6308 if (processing_template_decl
6309 && is_nondependent_constant_expression (expr))
6310 {
6311 processing_template_decl_sentinel s;
6312 expr = instantiate_non_dependent_expr_internal (expr, complain);
6313 }
6314 return expr;
6315 }
6316
6317 tree
6318 instantiate_non_dependent_expr (tree expr)
6319 {
6320 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6321 }
6322
6323 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6324 an uninstantiated expression. */
6325
6326 tree
6327 instantiate_non_dependent_or_null (tree expr)
6328 {
6329 if (expr == NULL_TREE)
6330 return NULL_TREE;
6331 if (processing_template_decl)
6332 {
6333 if (!is_nondependent_constant_expression (expr))
6334 expr = NULL_TREE;
6335 else
6336 {
6337 processing_template_decl_sentinel s;
6338 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6339 }
6340 }
6341 return expr;
6342 }
6343
6344 /* True iff T is a specialization of a variable template. */
6345
6346 bool
6347 variable_template_specialization_p (tree t)
6348 {
6349 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6350 return false;
6351 tree tmpl = DECL_TI_TEMPLATE (t);
6352 return variable_template_p (tmpl);
6353 }
6354
6355 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6356 template declaration, or a TYPE_DECL for an alias declaration. */
6357
6358 bool
6359 alias_type_or_template_p (tree t)
6360 {
6361 if (t == NULL_TREE)
6362 return false;
6363 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6364 || (TYPE_P (t)
6365 && TYPE_NAME (t)
6366 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6367 || DECL_ALIAS_TEMPLATE_P (t));
6368 }
6369
6370 /* If T is a specialization of an alias template, return it; otherwise return
6371 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6372
6373 tree
6374 alias_template_specialization_p (const_tree t,
6375 bool transparent_typedefs)
6376 {
6377 if (!TYPE_P (t))
6378 return NULL_TREE;
6379
6380 /* It's an alias template specialization if it's an alias and its
6381 TYPE_NAME is a specialization of a primary template. */
6382 if (typedef_variant_p (t))
6383 {
6384 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6385 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6386 return CONST_CAST_TREE (t);
6387 if (transparent_typedefs)
6388 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6389 (TYPE_NAME (t)),
6390 transparent_typedefs);
6391 }
6392
6393 return NULL_TREE;
6394 }
6395
6396 /* An alias template is complex from a SFINAE perspective if a template-id
6397 using that alias can be ill-formed when the expansion is not, as with
6398 the void_t template. We determine this by checking whether the
6399 expansion for the alias template uses all its template parameters. */
6400
6401 struct uses_all_template_parms_data
6402 {
6403 int level;
6404 bool *seen;
6405 };
6406
6407 static int
6408 uses_all_template_parms_r (tree t, void *data_)
6409 {
6410 struct uses_all_template_parms_data &data
6411 = *(struct uses_all_template_parms_data*)data_;
6412 tree idx = get_template_parm_index (t);
6413
6414 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6415 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6416 return 0;
6417 }
6418
6419 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6420
6421 static int
6422 complex_pack_expansion_r (tree t, void *data_)
6423 {
6424 /* An alias template with a pack expansion that expands a pack from the
6425 enclosing class needs to be considered complex, to avoid confusion with
6426 the same pack being used as an argument to the alias's own template
6427 parameter (91966). */
6428 if (!PACK_EXPANSION_P (t))
6429 return 0;
6430 struct uses_all_template_parms_data &data
6431 = *(struct uses_all_template_parms_data*)data_;
6432 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6433 pack = TREE_CHAIN (pack))
6434 {
6435 tree parm_pack = TREE_VALUE (pack);
6436 if (!TEMPLATE_PARM_P (parm_pack))
6437 continue;
6438 int idx, level;
6439 template_parm_level_and_index (parm_pack, &level, &idx);
6440 if (level < data.level)
6441 return 1;
6442 }
6443 return 0;
6444 }
6445
6446 static bool
6447 complex_alias_template_p (const_tree tmpl)
6448 {
6449 /* A renaming alias isn't complex. */
6450 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6451 return false;
6452
6453 /* Any other constrained alias is complex. */
6454 if (get_constraints (tmpl))
6455 return true;
6456
6457 struct uses_all_template_parms_data data;
6458 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6459 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6460 data.level = TMPL_PARMS_DEPTH (parms);
6461 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6462 data.seen = XALLOCAVEC (bool, len);
6463 for (int i = 0; i < len; ++i)
6464 data.seen[i] = false;
6465
6466 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6467 NULL, true, complex_pack_expansion_r))
6468 return true;
6469 for (int i = 0; i < len; ++i)
6470 if (!data.seen[i])
6471 return true;
6472 return false;
6473 }
6474
6475 /* If T is a specialization of a complex alias template with dependent
6476 template-arguments, return it; otherwise return NULL_TREE. If T is a
6477 typedef to such a specialization, return the specialization. */
6478
6479 tree
6480 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6481 {
6482 if (!TYPE_P (t) || !typedef_variant_p (t))
6483 return NULL_TREE;
6484
6485 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6486 if (tinfo
6487 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6488 && (any_dependent_template_arguments_p
6489 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6490 return CONST_CAST_TREE (t);
6491
6492 if (transparent_typedefs)
6493 {
6494 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6495 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6496 }
6497
6498 return NULL_TREE;
6499 }
6500
6501 /* Return the number of innermost template parameters in TMPL. */
6502
6503 static int
6504 num_innermost_template_parms (const_tree tmpl)
6505 {
6506 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6507 return TREE_VEC_LENGTH (parms);
6508 }
6509
6510 /* Return either TMPL or another template that it is equivalent to under DR
6511 1286: An alias that just changes the name of a template is equivalent to
6512 the other template. */
6513
6514 static tree
6515 get_underlying_template (tree tmpl)
6516 {
6517 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6518 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6519 {
6520 /* Determine if the alias is equivalent to an underlying template. */
6521 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6522 /* The underlying type may have been ill-formed. Don't proceed. */
6523 if (!orig_type)
6524 break;
6525 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6526 if (!tinfo)
6527 break;
6528
6529 tree underlying = TI_TEMPLATE (tinfo);
6530 if (!PRIMARY_TEMPLATE_P (underlying)
6531 || (num_innermost_template_parms (tmpl)
6532 != num_innermost_template_parms (underlying)))
6533 break;
6534
6535 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6536 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6537 break;
6538
6539 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6540 it's appropriate to treat a less-constrained alias as equivalent. */
6541 if (!at_least_as_constrained (underlying, tmpl))
6542 break;
6543
6544 /* Alias is equivalent. Strip it and repeat. */
6545 tmpl = underlying;
6546 }
6547
6548 return tmpl;
6549 }
6550
6551 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6552 must be a reference-to-function or a pointer-to-function type, as specified
6553 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6554 and check that the resulting function has external linkage. */
6555
6556 static tree
6557 convert_nontype_argument_function (tree type, tree expr,
6558 tsubst_flags_t complain)
6559 {
6560 tree fns = expr;
6561 tree fn, fn_no_ptr;
6562 linkage_kind linkage;
6563
6564 fn = instantiate_type (type, fns, tf_none);
6565 if (fn == error_mark_node)
6566 return error_mark_node;
6567
6568 if (value_dependent_expression_p (fn))
6569 goto accept;
6570
6571 fn_no_ptr = strip_fnptr_conv (fn);
6572 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6573 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6574 if (BASELINK_P (fn_no_ptr))
6575 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6576
6577 /* [temp.arg.nontype]/1
6578
6579 A template-argument for a non-type, non-template template-parameter
6580 shall be one of:
6581 [...]
6582 -- the address of an object or function with external [C++11: or
6583 internal] linkage. */
6584
6585 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6586 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6587 {
6588 if (complain & tf_error)
6589 {
6590 location_t loc = cp_expr_loc_or_input_loc (expr);
6591 error_at (loc, "%qE is not a valid template argument for type %qT",
6592 expr, type);
6593 if (TYPE_PTR_P (type))
6594 inform (loc, "it must be the address of a function "
6595 "with external linkage");
6596 else
6597 inform (loc, "it must be the name of a function with "
6598 "external linkage");
6599 }
6600 return NULL_TREE;
6601 }
6602
6603 linkage = decl_linkage (fn_no_ptr);
6604 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6605 {
6606 if (complain & tf_error)
6607 {
6608 location_t loc = cp_expr_loc_or_input_loc (expr);
6609 if (cxx_dialect >= cxx11)
6610 error_at (loc, "%qE is not a valid template argument for type "
6611 "%qT because %qD has no linkage",
6612 expr, type, fn_no_ptr);
6613 else
6614 error_at (loc, "%qE is not a valid template argument for type "
6615 "%qT because %qD does not have external linkage",
6616 expr, type, fn_no_ptr);
6617 }
6618 return NULL_TREE;
6619 }
6620
6621 accept:
6622 if (TYPE_REF_P (type))
6623 {
6624 if (REFERENCE_REF_P (fn))
6625 fn = TREE_OPERAND (fn, 0);
6626 else
6627 fn = build_address (fn);
6628 }
6629 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6630 fn = build_nop (type, fn);
6631
6632 return fn;
6633 }
6634
6635 /* Subroutine of convert_nontype_argument.
6636 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6637 Emit an error otherwise. */
6638
6639 static bool
6640 check_valid_ptrmem_cst_expr (tree type, tree expr,
6641 tsubst_flags_t complain)
6642 {
6643 tree orig_expr = expr;
6644 STRIP_NOPS (expr);
6645 if (null_ptr_cst_p (expr))
6646 return true;
6647 if (TREE_CODE (expr) == PTRMEM_CST
6648 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6649 PTRMEM_CST_CLASS (expr)))
6650 return true;
6651 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6652 return true;
6653 if (processing_template_decl
6654 && TREE_CODE (expr) == ADDR_EXPR
6655 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6656 return true;
6657 if (complain & tf_error)
6658 {
6659 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6660 error_at (loc, "%qE is not a valid template argument for type %qT",
6661 orig_expr, type);
6662 if (TREE_CODE (expr) != PTRMEM_CST)
6663 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6664 else
6665 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6666 }
6667 return false;
6668 }
6669
6670 /* Returns TRUE iff the address of OP is value-dependent.
6671
6672 14.6.2.4 [temp.dep.temp]:
6673 A non-integral non-type template-argument is dependent if its type is
6674 dependent or it has either of the following forms
6675 qualified-id
6676 & qualified-id
6677 and contains a nested-name-specifier which specifies a class-name that
6678 names a dependent type.
6679
6680 We generalize this to just say that the address of a member of a
6681 dependent class is value-dependent; the above doesn't cover the
6682 address of a static data member named with an unqualified-id. */
6683
6684 static bool
6685 has_value_dependent_address (tree op)
6686 {
6687 STRIP_ANY_LOCATION_WRAPPER (op);
6688
6689 /* We could use get_inner_reference here, but there's no need;
6690 this is only relevant for template non-type arguments, which
6691 can only be expressed as &id-expression. */
6692 if (DECL_P (op))
6693 {
6694 tree ctx = CP_DECL_CONTEXT (op);
6695 if (TYPE_P (ctx) && dependent_type_p (ctx))
6696 return true;
6697 }
6698
6699 return false;
6700 }
6701
6702 /* The next set of functions are used for providing helpful explanatory
6703 diagnostics for failed overload resolution. Their messages should be
6704 indented by two spaces for consistency with the messages in
6705 call.c */
6706
6707 static int
6708 unify_success (bool /*explain_p*/)
6709 {
6710 return 0;
6711 }
6712
6713 /* Other failure functions should call this one, to provide a single function
6714 for setting a breakpoint on. */
6715
6716 static int
6717 unify_invalid (bool /*explain_p*/)
6718 {
6719 return 1;
6720 }
6721
6722 static int
6723 unify_parameter_deduction_failure (bool explain_p, tree parm)
6724 {
6725 if (explain_p)
6726 inform (input_location,
6727 " couldn%'t deduce template parameter %qD", parm);
6728 return unify_invalid (explain_p);
6729 }
6730
6731 static int
6732 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6733 {
6734 if (explain_p)
6735 inform (input_location,
6736 " types %qT and %qT have incompatible cv-qualifiers",
6737 parm, arg);
6738 return unify_invalid (explain_p);
6739 }
6740
6741 static int
6742 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6743 {
6744 if (explain_p)
6745 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6746 return unify_invalid (explain_p);
6747 }
6748
6749 static int
6750 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6751 {
6752 if (explain_p)
6753 inform (input_location,
6754 " template parameter %qD is not a parameter pack, but "
6755 "argument %qD is",
6756 parm, arg);
6757 return unify_invalid (explain_p);
6758 }
6759
6760 static int
6761 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6762 {
6763 if (explain_p)
6764 inform (input_location,
6765 " template argument %qE does not match "
6766 "pointer-to-member constant %qE",
6767 arg, parm);
6768 return unify_invalid (explain_p);
6769 }
6770
6771 static int
6772 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6773 {
6774 if (explain_p)
6775 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6776 return unify_invalid (explain_p);
6777 }
6778
6779 static int
6780 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6781 {
6782 if (explain_p)
6783 inform (input_location,
6784 " inconsistent parameter pack deduction with %qT and %qT",
6785 old_arg, new_arg);
6786 return unify_invalid (explain_p);
6787 }
6788
6789 static int
6790 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6791 {
6792 if (explain_p)
6793 {
6794 if (TYPE_P (parm))
6795 inform (input_location,
6796 " deduced conflicting types for parameter %qT (%qT and %qT)",
6797 parm, first, second);
6798 else
6799 inform (input_location,
6800 " deduced conflicting values for non-type parameter "
6801 "%qE (%qE and %qE)", parm, first, second);
6802 }
6803 return unify_invalid (explain_p);
6804 }
6805
6806 static int
6807 unify_vla_arg (bool explain_p, tree arg)
6808 {
6809 if (explain_p)
6810 inform (input_location,
6811 " variable-sized array type %qT is not "
6812 "a valid template argument",
6813 arg);
6814 return unify_invalid (explain_p);
6815 }
6816
6817 static int
6818 unify_method_type_error (bool explain_p, tree arg)
6819 {
6820 if (explain_p)
6821 inform (input_location,
6822 " member function type %qT is not a valid template argument",
6823 arg);
6824 return unify_invalid (explain_p);
6825 }
6826
6827 static int
6828 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6829 {
6830 if (explain_p)
6831 {
6832 if (least_p)
6833 inform_n (input_location, wanted,
6834 " candidate expects at least %d argument, %d provided",
6835 " candidate expects at least %d arguments, %d provided",
6836 wanted, have);
6837 else
6838 inform_n (input_location, wanted,
6839 " candidate expects %d argument, %d provided",
6840 " candidate expects %d arguments, %d provided",
6841 wanted, have);
6842 }
6843 return unify_invalid (explain_p);
6844 }
6845
6846 static int
6847 unify_too_many_arguments (bool explain_p, int have, int wanted)
6848 {
6849 return unify_arity (explain_p, have, wanted);
6850 }
6851
6852 static int
6853 unify_too_few_arguments (bool explain_p, int have, int wanted,
6854 bool least_p = false)
6855 {
6856 return unify_arity (explain_p, have, wanted, least_p);
6857 }
6858
6859 static int
6860 unify_arg_conversion (bool explain_p, tree to_type,
6861 tree from_type, tree arg)
6862 {
6863 if (explain_p)
6864 inform (cp_expr_loc_or_input_loc (arg),
6865 " cannot convert %qE (type %qT) to type %qT",
6866 arg, from_type, to_type);
6867 return unify_invalid (explain_p);
6868 }
6869
6870 static int
6871 unify_no_common_base (bool explain_p, enum template_base_result r,
6872 tree parm, tree arg)
6873 {
6874 if (explain_p)
6875 switch (r)
6876 {
6877 case tbr_ambiguous_baseclass:
6878 inform (input_location, " %qT is an ambiguous base class of %qT",
6879 parm, arg);
6880 break;
6881 default:
6882 inform (input_location, " %qT is not derived from %qT", arg, parm);
6883 break;
6884 }
6885 return unify_invalid (explain_p);
6886 }
6887
6888 static int
6889 unify_inconsistent_template_template_parameters (bool explain_p)
6890 {
6891 if (explain_p)
6892 inform (input_location,
6893 " template parameters of a template template argument are "
6894 "inconsistent with other deduced template arguments");
6895 return unify_invalid (explain_p);
6896 }
6897
6898 static int
6899 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6900 {
6901 if (explain_p)
6902 inform (input_location,
6903 " cannot deduce a template for %qT from non-template type %qT",
6904 parm, arg);
6905 return unify_invalid (explain_p);
6906 }
6907
6908 static int
6909 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6910 {
6911 if (explain_p)
6912 inform (input_location,
6913 " template argument %qE does not match %qE", arg, parm);
6914 return unify_invalid (explain_p);
6915 }
6916
6917 /* True if T is a C++20 template parameter object to store the argument for a
6918 template parameter of class type. */
6919
6920 bool
6921 template_parm_object_p (const_tree t)
6922 {
6923 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6924 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6925 }
6926
6927 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6928 argument for TYPE, points to an unsuitable object.
6929
6930 Also adjust the type of the index in C++20 array subobject references. */
6931
6932 static bool
6933 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6934 {
6935 switch (TREE_CODE (expr))
6936 {
6937 CASE_CONVERT:
6938 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6939 complain);
6940
6941 case TARGET_EXPR:
6942 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6943 complain);
6944
6945 case CONSTRUCTOR:
6946 {
6947 unsigned i; tree elt;
6948 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6949 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6950 return true;
6951 }
6952 break;
6953
6954 case ADDR_EXPR:
6955 {
6956 tree decl = TREE_OPERAND (expr, 0);
6957
6958 if (cxx_dialect >= cxx20)
6959 while (TREE_CODE (decl) == COMPONENT_REF
6960 || TREE_CODE (decl) == ARRAY_REF)
6961 {
6962 tree &op = TREE_OPERAND (decl, 1);
6963 if (TREE_CODE (decl) == ARRAY_REF
6964 && TREE_CODE (op) == INTEGER_CST)
6965 /* Canonicalize array offsets to ptrdiff_t; how they were
6966 written doesn't matter for subobject identity. */
6967 op = fold_convert (ptrdiff_type_node, op);
6968 decl = TREE_OPERAND (decl, 0);
6969 }
6970
6971 if (!VAR_P (decl))
6972 {
6973 if (complain & tf_error)
6974 error_at (cp_expr_loc_or_input_loc (expr),
6975 "%qE is not a valid template argument of type %qT "
6976 "because %qE is not a variable", expr, type, decl);
6977 return true;
6978 }
6979 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6980 {
6981 if (complain & tf_error)
6982 error_at (cp_expr_loc_or_input_loc (expr),
6983 "%qE is not a valid template argument of type %qT "
6984 "in C++98 because %qD does not have external linkage",
6985 expr, type, decl);
6986 return true;
6987 }
6988 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6989 && decl_linkage (decl) == lk_none)
6990 {
6991 if (complain & tf_error)
6992 error_at (cp_expr_loc_or_input_loc (expr),
6993 "%qE is not a valid template argument of type %qT "
6994 "because %qD has no linkage", expr, type, decl);
6995 return true;
6996 }
6997 /* C++17: For a non-type template-parameter of reference or pointer
6998 type, the value of the constant expression shall not refer to (or
6999 for a pointer type, shall not be the address of):
7000 * a subobject (4.5),
7001 * a temporary object (15.2),
7002 * a string literal (5.13.5),
7003 * the result of a typeid expression (8.2.8), or
7004 * a predefined __func__ variable (11.4.1). */
7005 else if (DECL_ARTIFICIAL (decl))
7006 {
7007 if (complain & tf_error)
7008 error ("the address of %qD is not a valid template argument",
7009 decl);
7010 return true;
7011 }
7012 else if (cxx_dialect < cxx20
7013 && !(same_type_ignoring_top_level_qualifiers_p
7014 (strip_array_types (TREE_TYPE (type)),
7015 strip_array_types (TREE_TYPE (decl)))))
7016 {
7017 if (complain & tf_error)
7018 error ("the address of the %qT subobject of %qD is not a "
7019 "valid template argument", TREE_TYPE (type), decl);
7020 return true;
7021 }
7022 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7023 {
7024 if (complain & tf_error)
7025 error ("the address of %qD is not a valid template argument "
7026 "because it does not have static storage duration",
7027 decl);
7028 return true;
7029 }
7030 }
7031 break;
7032
7033 default:
7034 if (!INDIRECT_TYPE_P (type))
7035 /* We're only concerned about pointers and references here. */;
7036 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7037 /* Null pointer values are OK in C++11. */;
7038 else
7039 {
7040 if (VAR_P (expr))
7041 {
7042 if (complain & tf_error)
7043 error ("%qD is not a valid template argument "
7044 "because %qD is a variable, not the address of "
7045 "a variable", expr, expr);
7046 return true;
7047 }
7048 else
7049 {
7050 if (complain & tf_error)
7051 error ("%qE is not a valid template argument for %qT "
7052 "because it is not the address of a variable",
7053 expr, type);
7054 return true;
7055 }
7056 }
7057 }
7058 return false;
7059
7060 }
7061
7062 /* The template arguments corresponding to template parameter objects of types
7063 that contain pointers to members. */
7064
7065 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7066
7067 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7068 template argument EXPR. */
7069
7070 static tree
7071 get_template_parm_object (tree expr, tsubst_flags_t complain)
7072 {
7073 if (TREE_CODE (expr) == TARGET_EXPR)
7074 expr = TARGET_EXPR_INITIAL (expr);
7075
7076 if (!TREE_CONSTANT (expr))
7077 {
7078 if ((complain & tf_error)
7079 && require_rvalue_constant_expression (expr))
7080 cxx_constant_value (expr);
7081 return error_mark_node;
7082 }
7083 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7084 return error_mark_node;
7085
7086 tree name = mangle_template_parm_object (expr);
7087 tree decl = get_global_binding (name);
7088 if (decl)
7089 return decl;
7090
7091 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7092 decl = create_temporary_var (type);
7093 TREE_STATIC (decl) = true;
7094 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7095 TREE_READONLY (decl) = true;
7096 DECL_NAME (decl) = name;
7097 SET_DECL_ASSEMBLER_NAME (decl, name);
7098 DECL_CONTEXT (decl) = global_namespace;
7099 comdat_linkage (decl);
7100
7101 if (!zero_init_p (type))
7102 {
7103 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7104 lower_var_init before we're done mangling. So store the original
7105 value elsewhere. */
7106 tree copy = unshare_constructor (expr);
7107 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7108 }
7109
7110 pushdecl_top_level_and_finish (decl, expr);
7111
7112 return decl;
7113 }
7114
7115 /* Return the actual template argument corresponding to template parameter
7116 object VAR. */
7117
7118 tree
7119 tparm_object_argument (tree var)
7120 {
7121 if (zero_init_p (TREE_TYPE (var)))
7122 return DECL_INITIAL (var);
7123 return *(tparm_obj_values->get (var));
7124 }
7125
7126 /* Attempt to convert the non-type template parameter EXPR to the
7127 indicated TYPE. If the conversion is successful, return the
7128 converted value. If the conversion is unsuccessful, return
7129 NULL_TREE if we issued an error message, or error_mark_node if we
7130 did not. We issue error messages for out-and-out bad template
7131 parameters, but not simply because the conversion failed, since we
7132 might be just trying to do argument deduction. Both TYPE and EXPR
7133 must be non-dependent.
7134
7135 The conversion follows the special rules described in
7136 [temp.arg.nontype], and it is much more strict than an implicit
7137 conversion.
7138
7139 This function is called twice for each template argument (see
7140 lookup_template_class for a more accurate description of this
7141 problem). This means that we need to handle expressions which
7142 are not valid in a C++ source, but can be created from the
7143 first call (for instance, casts to perform conversions). These
7144 hacks can go away after we fix the double coercion problem. */
7145
7146 static tree
7147 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7148 {
7149 tree expr_type;
7150 location_t loc = cp_expr_loc_or_input_loc (expr);
7151
7152 /* Detect immediately string literals as invalid non-type argument.
7153 This special-case is not needed for correctness (we would easily
7154 catch this later), but only to provide better diagnostic for this
7155 common user mistake. As suggested by DR 100, we do not mention
7156 linkage issues in the diagnostic as this is not the point. */
7157 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7158 {
7159 if (complain & tf_error)
7160 error ("%qE is not a valid template argument for type %qT "
7161 "because string literals can never be used in this context",
7162 expr, type);
7163 return NULL_TREE;
7164 }
7165
7166 /* Add the ADDR_EXPR now for the benefit of
7167 value_dependent_expression_p. */
7168 if (TYPE_PTROBV_P (type)
7169 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7170 {
7171 expr = decay_conversion (expr, complain);
7172 if (expr == error_mark_node)
7173 return error_mark_node;
7174 }
7175
7176 /* If we are in a template, EXPR may be non-dependent, but still
7177 have a syntactic, rather than semantic, form. For example, EXPR
7178 might be a SCOPE_REF, rather than the VAR_DECL to which the
7179 SCOPE_REF refers. Preserving the qualifying scope is necessary
7180 so that access checking can be performed when the template is
7181 instantiated -- but here we need the resolved form so that we can
7182 convert the argument. */
7183 bool non_dep = false;
7184 if (TYPE_REF_OBJ_P (type)
7185 && has_value_dependent_address (expr))
7186 /* If we want the address and it's value-dependent, don't fold. */;
7187 else if (processing_template_decl
7188 && is_nondependent_constant_expression (expr))
7189 non_dep = true;
7190 if (error_operand_p (expr))
7191 return error_mark_node;
7192 expr_type = TREE_TYPE (expr);
7193
7194 /* If the argument is non-dependent, perform any conversions in
7195 non-dependent context as well. */
7196 processing_template_decl_sentinel s (non_dep);
7197 if (non_dep)
7198 expr = instantiate_non_dependent_expr_internal (expr, complain);
7199
7200 const bool val_dep_p = value_dependent_expression_p (expr);
7201 if (val_dep_p)
7202 expr = canonicalize_expr_argument (expr, complain);
7203
7204 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7205 to a non-type argument of "nullptr". */
7206 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7207 expr = fold_simple (convert (type, expr));
7208
7209 /* In C++11, integral or enumeration non-type template arguments can be
7210 arbitrary constant expressions. Pointer and pointer to
7211 member arguments can be general constant expressions that evaluate
7212 to a null value, but otherwise still need to be of a specific form. */
7213 if (cxx_dialect >= cxx11)
7214 {
7215 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7216 /* A PTRMEM_CST is already constant, and a valid template
7217 argument for a parameter of pointer to member type, we just want
7218 to leave it in that form rather than lower it to a
7219 CONSTRUCTOR. */;
7220 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7221 || cxx_dialect >= cxx17)
7222 {
7223 /* C++17: A template-argument for a non-type template-parameter shall
7224 be a converted constant expression (8.20) of the type of the
7225 template-parameter. */
7226 expr = build_converted_constant_expr (type, expr, complain);
7227 if (expr == error_mark_node)
7228 /* Make sure we return NULL_TREE only if we have really issued
7229 an error, as described above. */
7230 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7231 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7232 {
7233 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7234 return expr;
7235 }
7236 expr = maybe_constant_value (expr, NULL_TREE,
7237 /*manifestly_const_eval=*/true);
7238 expr = convert_from_reference (expr);
7239 }
7240 else if (TYPE_PTR_OR_PTRMEM_P (type))
7241 {
7242 tree folded = maybe_constant_value (expr, NULL_TREE,
7243 /*manifestly_const_eval=*/true);
7244 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7245 : null_member_pointer_value_p (folded))
7246 expr = folded;
7247 }
7248 }
7249
7250 if (TYPE_REF_P (type))
7251 expr = mark_lvalue_use (expr);
7252 else
7253 expr = mark_rvalue_use (expr);
7254
7255 /* HACK: Due to double coercion, we can get a
7256 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7257 which is the tree that we built on the first call (see
7258 below when coercing to reference to object or to reference to
7259 function). We just strip everything and get to the arg.
7260 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7261 for examples. */
7262 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7263 {
7264 tree probe_type, probe = expr;
7265 if (REFERENCE_REF_P (probe))
7266 probe = TREE_OPERAND (probe, 0);
7267 probe_type = TREE_TYPE (probe);
7268 if (TREE_CODE (probe) == NOP_EXPR)
7269 {
7270 /* ??? Maybe we could use convert_from_reference here, but we
7271 would need to relax its constraints because the NOP_EXPR
7272 could actually change the type to something more cv-qualified,
7273 and this is not folded by convert_from_reference. */
7274 tree addr = TREE_OPERAND (probe, 0);
7275 if (TYPE_REF_P (probe_type)
7276 && TREE_CODE (addr) == ADDR_EXPR
7277 && TYPE_PTR_P (TREE_TYPE (addr))
7278 && (same_type_ignoring_top_level_qualifiers_p
7279 (TREE_TYPE (probe_type),
7280 TREE_TYPE (TREE_TYPE (addr)))))
7281 {
7282 expr = TREE_OPERAND (addr, 0);
7283 expr_type = TREE_TYPE (probe_type);
7284 }
7285 }
7286 }
7287
7288 /* [temp.arg.nontype]/5, bullet 1
7289
7290 For a non-type template-parameter of integral or enumeration type,
7291 integral promotions (_conv.prom_) and integral conversions
7292 (_conv.integral_) are applied. */
7293 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7294 || TREE_CODE (type) == REAL_TYPE)
7295 {
7296 if (cxx_dialect < cxx11)
7297 {
7298 tree t = build_converted_constant_expr (type, expr, complain);
7299 t = maybe_constant_value (t);
7300 if (t != error_mark_node)
7301 expr = t;
7302 }
7303
7304 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7305 return error_mark_node;
7306
7307 /* Notice that there are constant expressions like '4 % 0' which
7308 do not fold into integer constants. */
7309 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7310 {
7311 if (complain & tf_error)
7312 {
7313 int errs = errorcount, warns = warningcount + werrorcount;
7314 if (!require_potential_constant_expression (expr))
7315 expr = error_mark_node;
7316 else
7317 expr = cxx_constant_value (expr);
7318 if (errorcount > errs || warningcount + werrorcount > warns)
7319 inform (loc, "in template argument for type %qT", type);
7320 if (expr == error_mark_node)
7321 return NULL_TREE;
7322 /* else cxx_constant_value complained but gave us
7323 a real constant, so go ahead. */
7324 if (!CONSTANT_CLASS_P (expr))
7325 {
7326 /* Some assemble time constant expressions like
7327 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7328 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7329 as we can emit them into .rodata initializers of
7330 variables, yet they can't fold into an INTEGER_CST at
7331 compile time. Refuse them here. */
7332 gcc_checking_assert (reduced_constant_expression_p (expr));
7333 error_at (loc, "template argument %qE for type %qT not "
7334 "a compile-time constant", expr, type);
7335 return NULL_TREE;
7336 }
7337 }
7338 else
7339 return NULL_TREE;
7340 }
7341
7342 /* Avoid typedef problems. */
7343 if (TREE_TYPE (expr) != type)
7344 expr = fold_convert (type, expr);
7345 }
7346 /* [temp.arg.nontype]/5, bullet 2
7347
7348 For a non-type template-parameter of type pointer to object,
7349 qualification conversions (_conv.qual_) and the array-to-pointer
7350 conversion (_conv.array_) are applied. */
7351 else if (TYPE_PTROBV_P (type))
7352 {
7353 tree decayed = expr;
7354
7355 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7356 decay_conversion or an explicit cast. If it's a problematic cast,
7357 we'll complain about it below. */
7358 if (TREE_CODE (expr) == NOP_EXPR)
7359 {
7360 tree probe = expr;
7361 STRIP_NOPS (probe);
7362 if (TREE_CODE (probe) == ADDR_EXPR
7363 && TYPE_PTR_P (TREE_TYPE (probe)))
7364 {
7365 expr = probe;
7366 expr_type = TREE_TYPE (expr);
7367 }
7368 }
7369
7370 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7371
7372 A template-argument for a non-type, non-template template-parameter
7373 shall be one of: [...]
7374
7375 -- the name of a non-type template-parameter;
7376 -- the address of an object or function with external linkage, [...]
7377 expressed as "& id-expression" where the & is optional if the name
7378 refers to a function or array, or if the corresponding
7379 template-parameter is a reference.
7380
7381 Here, we do not care about functions, as they are invalid anyway
7382 for a parameter of type pointer-to-object. */
7383
7384 if (val_dep_p)
7385 /* Non-type template parameters are OK. */
7386 ;
7387 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7388 /* Null pointer values are OK in C++11. */;
7389 else if (TREE_CODE (expr) != ADDR_EXPR
7390 && !INDIRECT_TYPE_P (expr_type))
7391 /* Other values, like integer constants, might be valid
7392 non-type arguments of some other type. */
7393 return error_mark_node;
7394 else if (invalid_tparm_referent_p (type, expr, complain))
7395 return NULL_TREE;
7396
7397 expr = decayed;
7398
7399 expr = perform_qualification_conversions (type, expr);
7400 if (expr == error_mark_node)
7401 return error_mark_node;
7402 }
7403 /* [temp.arg.nontype]/5, bullet 3
7404
7405 For a non-type template-parameter of type reference to object, no
7406 conversions apply. The type referred to by the reference may be more
7407 cv-qualified than the (otherwise identical) type of the
7408 template-argument. The template-parameter is bound directly to the
7409 template-argument, which must be an lvalue. */
7410 else if (TYPE_REF_OBJ_P (type))
7411 {
7412 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7413 expr_type))
7414 return error_mark_node;
7415
7416 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7417 {
7418 if (complain & tf_error)
7419 error ("%qE is not a valid template argument for type %qT "
7420 "because of conflicts in cv-qualification", expr, type);
7421 return NULL_TREE;
7422 }
7423
7424 if (!lvalue_p (expr))
7425 {
7426 if (complain & tf_error)
7427 error ("%qE is not a valid template argument for type %qT "
7428 "because it is not an lvalue", expr, type);
7429 return NULL_TREE;
7430 }
7431
7432 /* [temp.arg.nontype]/1
7433
7434 A template-argument for a non-type, non-template template-parameter
7435 shall be one of: [...]
7436
7437 -- the address of an object or function with external linkage. */
7438 if (INDIRECT_REF_P (expr)
7439 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7440 {
7441 expr = TREE_OPERAND (expr, 0);
7442 if (DECL_P (expr))
7443 {
7444 if (complain & tf_error)
7445 error ("%q#D is not a valid template argument for type %qT "
7446 "because a reference variable does not have a constant "
7447 "address", expr, type);
7448 return NULL_TREE;
7449 }
7450 }
7451
7452 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7453 /* OK, dependent reference. We don't want to ask whether a DECL is
7454 itself value-dependent, since what we want here is its address. */;
7455 else
7456 {
7457 expr = build_address (expr);
7458
7459 if (invalid_tparm_referent_p (type, expr, complain))
7460 return NULL_TREE;
7461 }
7462
7463 if (!same_type_p (type, TREE_TYPE (expr)))
7464 expr = build_nop (type, expr);
7465 }
7466 /* [temp.arg.nontype]/5, bullet 4
7467
7468 For a non-type template-parameter of type pointer to function, only
7469 the function-to-pointer conversion (_conv.func_) is applied. If the
7470 template-argument represents a set of overloaded functions (or a
7471 pointer to such), the matching function is selected from the set
7472 (_over.over_). */
7473 else if (TYPE_PTRFN_P (type))
7474 {
7475 /* If the argument is a template-id, we might not have enough
7476 context information to decay the pointer. */
7477 if (!type_unknown_p (expr_type))
7478 {
7479 expr = decay_conversion (expr, complain);
7480 if (expr == error_mark_node)
7481 return error_mark_node;
7482 }
7483
7484 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7485 /* Null pointer values are OK in C++11. */
7486 return perform_qualification_conversions (type, expr);
7487
7488 expr = convert_nontype_argument_function (type, expr, complain);
7489 if (!expr || expr == error_mark_node)
7490 return expr;
7491 }
7492 /* [temp.arg.nontype]/5, bullet 5
7493
7494 For a non-type template-parameter of type reference to function, no
7495 conversions apply. If the template-argument represents a set of
7496 overloaded functions, the matching function is selected from the set
7497 (_over.over_). */
7498 else if (TYPE_REFFN_P (type))
7499 {
7500 if (TREE_CODE (expr) == ADDR_EXPR)
7501 {
7502 if (complain & tf_error)
7503 {
7504 error ("%qE is not a valid template argument for type %qT "
7505 "because it is a pointer", expr, type);
7506 inform (input_location, "try using %qE instead",
7507 TREE_OPERAND (expr, 0));
7508 }
7509 return NULL_TREE;
7510 }
7511
7512 expr = convert_nontype_argument_function (type, expr, complain);
7513 if (!expr || expr == error_mark_node)
7514 return expr;
7515 }
7516 /* [temp.arg.nontype]/5, bullet 6
7517
7518 For a non-type template-parameter of type pointer to member function,
7519 no conversions apply. If the template-argument represents a set of
7520 overloaded member functions, the matching member function is selected
7521 from the set (_over.over_). */
7522 else if (TYPE_PTRMEMFUNC_P (type))
7523 {
7524 expr = instantiate_type (type, expr, tf_none);
7525 if (expr == error_mark_node)
7526 return error_mark_node;
7527
7528 /* [temp.arg.nontype] bullet 1 says the pointer to member
7529 expression must be a pointer-to-member constant. */
7530 if (!val_dep_p
7531 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7532 return NULL_TREE;
7533
7534 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7535 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7536 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7537 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7538 }
7539 /* [temp.arg.nontype]/5, bullet 7
7540
7541 For a non-type template-parameter of type pointer to data member,
7542 qualification conversions (_conv.qual_) are applied. */
7543 else if (TYPE_PTRDATAMEM_P (type))
7544 {
7545 /* [temp.arg.nontype] bullet 1 says the pointer to member
7546 expression must be a pointer-to-member constant. */
7547 if (!val_dep_p
7548 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7549 return NULL_TREE;
7550
7551 expr = perform_qualification_conversions (type, expr);
7552 if (expr == error_mark_node)
7553 return expr;
7554 }
7555 else if (NULLPTR_TYPE_P (type))
7556 {
7557 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7558 {
7559 if (complain & tf_error)
7560 error ("%qE is not a valid template argument for type %qT "
7561 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7562 return NULL_TREE;
7563 }
7564 return expr;
7565 }
7566 else if (CLASS_TYPE_P (type))
7567 {
7568 /* Replace the argument with a reference to the corresponding template
7569 parameter object. */
7570 if (!val_dep_p)
7571 expr = get_template_parm_object (expr, complain);
7572 if (expr == error_mark_node)
7573 return NULL_TREE;
7574 }
7575 /* A template non-type parameter must be one of the above. */
7576 else
7577 gcc_unreachable ();
7578
7579 /* Sanity check: did we actually convert the argument to the
7580 right type? */
7581 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7582 (type, TREE_TYPE (expr)));
7583 return convert_from_reference (expr);
7584 }
7585
7586 /* Subroutine of coerce_template_template_parms, which returns 1 if
7587 PARM_PARM and ARG_PARM match using the rule for the template
7588 parameters of template template parameters. Both PARM and ARG are
7589 template parameters; the rest of the arguments are the same as for
7590 coerce_template_template_parms.
7591 */
7592 static int
7593 coerce_template_template_parm (tree parm,
7594 tree arg,
7595 tsubst_flags_t complain,
7596 tree in_decl,
7597 tree outer_args)
7598 {
7599 if (arg == NULL_TREE || error_operand_p (arg)
7600 || parm == NULL_TREE || error_operand_p (parm))
7601 return 0;
7602
7603 if (TREE_CODE (arg) != TREE_CODE (parm))
7604 return 0;
7605
7606 switch (TREE_CODE (parm))
7607 {
7608 case TEMPLATE_DECL:
7609 /* We encounter instantiations of templates like
7610 template <template <template <class> class> class TT>
7611 class C; */
7612 {
7613 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7614 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7615
7616 if (!coerce_template_template_parms
7617 (parmparm, argparm, complain, in_decl, outer_args))
7618 return 0;
7619 }
7620 /* Fall through. */
7621
7622 case TYPE_DECL:
7623 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7624 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7625 /* Argument is a parameter pack but parameter is not. */
7626 return 0;
7627 break;
7628
7629 case PARM_DECL:
7630 /* The tsubst call is used to handle cases such as
7631
7632 template <int> class C {};
7633 template <class T, template <T> class TT> class D {};
7634 D<int, C> d;
7635
7636 i.e. the parameter list of TT depends on earlier parameters. */
7637 if (!uses_template_parms (TREE_TYPE (arg)))
7638 {
7639 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7640 if (!uses_template_parms (t)
7641 && !same_type_p (t, TREE_TYPE (arg)))
7642 return 0;
7643 }
7644
7645 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7646 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7647 /* Argument is a parameter pack but parameter is not. */
7648 return 0;
7649
7650 break;
7651
7652 default:
7653 gcc_unreachable ();
7654 }
7655
7656 return 1;
7657 }
7658
7659 /* Coerce template argument list ARGLIST for use with template
7660 template-parameter TEMPL. */
7661
7662 static tree
7663 coerce_template_args_for_ttp (tree templ, tree arglist,
7664 tsubst_flags_t complain)
7665 {
7666 /* Consider an example where a template template parameter declared as
7667
7668 template <class T, class U = std::allocator<T> > class TT
7669
7670 The template parameter level of T and U are one level larger than
7671 of TT. To proper process the default argument of U, say when an
7672 instantiation `TT<int>' is seen, we need to build the full
7673 arguments containing {int} as the innermost level. Outer levels,
7674 available when not appearing as default template argument, can be
7675 obtained from the arguments of the enclosing template.
7676
7677 Suppose that TT is later substituted with std::vector. The above
7678 instantiation is `TT<int, std::allocator<T> >' with TT at
7679 level 1, and T at level 2, while the template arguments at level 1
7680 becomes {std::vector} and the inner level 2 is {int}. */
7681
7682 tree outer = DECL_CONTEXT (templ);
7683 if (outer)
7684 outer = generic_targs_for (outer);
7685 else if (current_template_parms)
7686 {
7687 /* This is an argument of the current template, so we haven't set
7688 DECL_CONTEXT yet. */
7689 tree relevant_template_parms;
7690
7691 /* Parameter levels that are greater than the level of the given
7692 template template parm are irrelevant. */
7693 relevant_template_parms = current_template_parms;
7694 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7695 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7696 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7697
7698 outer = template_parms_to_args (relevant_template_parms);
7699 }
7700
7701 if (outer)
7702 arglist = add_to_template_args (outer, arglist);
7703
7704 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7705 return coerce_template_parms (parmlist, arglist, templ,
7706 complain,
7707 /*require_all_args=*/true,
7708 /*use_default_args=*/true);
7709 }
7710
7711 /* A cache of template template parameters with match-all default
7712 arguments. */
7713 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7714
7715 /* T is a bound template template-parameter. Copy its arguments into default
7716 arguments of the template template-parameter's template parameters. */
7717
7718 static tree
7719 add_defaults_to_ttp (tree otmpl)
7720 {
7721 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7722 return *c;
7723
7724 tree ntmpl = copy_node (otmpl);
7725
7726 tree ntype = copy_node (TREE_TYPE (otmpl));
7727 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7728 TYPE_MAIN_VARIANT (ntype) = ntype;
7729 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7730 TYPE_NAME (ntype) = ntmpl;
7731 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7732
7733 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7734 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7735 TEMPLATE_PARM_DECL (idx) = ntmpl;
7736 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7737
7738 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7739 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7740 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7741 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7742 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7743 {
7744 tree o = TREE_VEC_ELT (vec, i);
7745 if (!template_parameter_pack_p (TREE_VALUE (o)))
7746 {
7747 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7748 TREE_PURPOSE (n) = any_targ_node;
7749 }
7750 }
7751
7752 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7753 return ntmpl;
7754 }
7755
7756 /* ARG is a bound potential template template-argument, and PARGS is a list
7757 of arguments for the corresponding template template-parameter. Adjust
7758 PARGS as appropriate for application to ARG's template, and if ARG is a
7759 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7760 arguments to the template template parameter. */
7761
7762 static tree
7763 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7764 {
7765 ++processing_template_decl;
7766 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7767 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7768 {
7769 /* When comparing two template template-parameters in partial ordering,
7770 rewrite the one currently being used as an argument to have default
7771 arguments for all parameters. */
7772 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7773 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7774 if (pargs != error_mark_node)
7775 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7776 TYPE_TI_ARGS (arg));
7777 }
7778 else
7779 {
7780 tree aparms
7781 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7782 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7783 /*require_all*/true,
7784 /*use_default*/true);
7785 }
7786 --processing_template_decl;
7787 return pargs;
7788 }
7789
7790 /* Subroutine of unify for the case when PARM is a
7791 BOUND_TEMPLATE_TEMPLATE_PARM. */
7792
7793 static int
7794 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7795 bool explain_p)
7796 {
7797 tree parmvec = TYPE_TI_ARGS (parm);
7798 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7799
7800 /* The template template parm might be variadic and the argument
7801 not, so flatten both argument lists. */
7802 parmvec = expand_template_argument_pack (parmvec);
7803 argvec = expand_template_argument_pack (argvec);
7804
7805 if (flag_new_ttp)
7806 {
7807 /* In keeping with P0522R0, adjust P's template arguments
7808 to apply to A's template; then flatten it again. */
7809 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7810 nparmvec = expand_template_argument_pack (nparmvec);
7811
7812 if (unify (tparms, targs, nparmvec, argvec,
7813 UNIFY_ALLOW_NONE, explain_p))
7814 return 1;
7815
7816 /* If the P0522 adjustment eliminated a pack expansion, deduce
7817 empty packs. */
7818 if (flag_new_ttp
7819 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7820 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7821 DEDUCE_EXACT, /*sub*/true, explain_p))
7822 return 1;
7823 }
7824 else
7825 {
7826 /* Deduce arguments T, i from TT<T> or TT<i>.
7827 We check each element of PARMVEC and ARGVEC individually
7828 rather than the whole TREE_VEC since they can have
7829 different number of elements, which is allowed under N2555. */
7830
7831 int len = TREE_VEC_LENGTH (parmvec);
7832
7833 /* Check if the parameters end in a pack, making them
7834 variadic. */
7835 int parm_variadic_p = 0;
7836 if (len > 0
7837 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7838 parm_variadic_p = 1;
7839
7840 for (int i = 0; i < len - parm_variadic_p; ++i)
7841 /* If the template argument list of P contains a pack
7842 expansion that is not the last template argument, the
7843 entire template argument list is a non-deduced
7844 context. */
7845 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7846 return unify_success (explain_p);
7847
7848 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7849 return unify_too_few_arguments (explain_p,
7850 TREE_VEC_LENGTH (argvec), len);
7851
7852 for (int i = 0; i < len - parm_variadic_p; ++i)
7853 if (unify (tparms, targs,
7854 TREE_VEC_ELT (parmvec, i),
7855 TREE_VEC_ELT (argvec, i),
7856 UNIFY_ALLOW_NONE, explain_p))
7857 return 1;
7858
7859 if (parm_variadic_p
7860 && unify_pack_expansion (tparms, targs,
7861 parmvec, argvec,
7862 DEDUCE_EXACT,
7863 /*subr=*/true, explain_p))
7864 return 1;
7865 }
7866
7867 return 0;
7868 }
7869
7870 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7871 template template parameters. Both PARM_PARMS and ARG_PARMS are
7872 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7873 or PARM_DECL.
7874
7875 Consider the example:
7876 template <class T> class A;
7877 template<template <class U> class TT> class B;
7878
7879 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7880 the parameters to A, and OUTER_ARGS contains A. */
7881
7882 static int
7883 coerce_template_template_parms (tree parm_parms,
7884 tree arg_parms,
7885 tsubst_flags_t complain,
7886 tree in_decl,
7887 tree outer_args)
7888 {
7889 int nparms, nargs, i;
7890 tree parm, arg;
7891 int variadic_p = 0;
7892
7893 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7894 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7895
7896 nparms = TREE_VEC_LENGTH (parm_parms);
7897 nargs = TREE_VEC_LENGTH (arg_parms);
7898
7899 if (flag_new_ttp)
7900 {
7901 /* P0522R0: A template template-parameter P is at least as specialized as
7902 a template template-argument A if, given the following rewrite to two
7903 function templates, the function template corresponding to P is at
7904 least as specialized as the function template corresponding to A
7905 according to the partial ordering rules for function templates
7906 ([temp.func.order]). Given an invented class template X with the
7907 template parameter list of A (including default arguments):
7908
7909 * Each of the two function templates has the same template parameters,
7910 respectively, as P or A.
7911
7912 * Each function template has a single function parameter whose type is
7913 a specialization of X with template arguments corresponding to the
7914 template parameters from the respective function template where, for
7915 each template parameter PP in the template parameter list of the
7916 function template, a corresponding template argument AA is formed. If
7917 PP declares a parameter pack, then AA is the pack expansion
7918 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7919
7920 If the rewrite produces an invalid type, then P is not at least as
7921 specialized as A. */
7922
7923 /* So coerce P's args to apply to A's parms, and then deduce between A's
7924 args and the converted args. If that succeeds, A is at least as
7925 specialized as P, so they match.*/
7926 tree pargs = template_parms_level_to_args (parm_parms);
7927 pargs = add_outermost_template_args (outer_args, pargs);
7928 ++processing_template_decl;
7929 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7930 /*require_all*/true, /*use_default*/true);
7931 --processing_template_decl;
7932 if (pargs != error_mark_node)
7933 {
7934 tree targs = make_tree_vec (nargs);
7935 tree aargs = template_parms_level_to_args (arg_parms);
7936 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7937 /*explain*/false))
7938 return 1;
7939 }
7940 }
7941
7942 /* Determine whether we have a parameter pack at the end of the
7943 template template parameter's template parameter list. */
7944 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7945 {
7946 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7947
7948 if (error_operand_p (parm))
7949 return 0;
7950
7951 switch (TREE_CODE (parm))
7952 {
7953 case TEMPLATE_DECL:
7954 case TYPE_DECL:
7955 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7956 variadic_p = 1;
7957 break;
7958
7959 case PARM_DECL:
7960 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7961 variadic_p = 1;
7962 break;
7963
7964 default:
7965 gcc_unreachable ();
7966 }
7967 }
7968
7969 if (nargs != nparms
7970 && !(variadic_p && nargs >= nparms - 1))
7971 return 0;
7972
7973 /* Check all of the template parameters except the parameter pack at
7974 the end (if any). */
7975 for (i = 0; i < nparms - variadic_p; ++i)
7976 {
7977 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7978 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7979 continue;
7980
7981 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7982 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7983
7984 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7985 outer_args))
7986 return 0;
7987
7988 }
7989
7990 if (variadic_p)
7991 {
7992 /* Check each of the template parameters in the template
7993 argument against the template parameter pack at the end of
7994 the template template parameter. */
7995 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7996 return 0;
7997
7998 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7999
8000 for (; i < nargs; ++i)
8001 {
8002 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8003 continue;
8004
8005 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8006
8007 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8008 outer_args))
8009 return 0;
8010 }
8011 }
8012
8013 return 1;
8014 }
8015
8016 /* Verifies that the deduced template arguments (in TARGS) for the
8017 template template parameters (in TPARMS) represent valid bindings,
8018 by comparing the template parameter list of each template argument
8019 to the template parameter list of its corresponding template
8020 template parameter, in accordance with DR150. This
8021 routine can only be called after all template arguments have been
8022 deduced. It will return TRUE if all of the template template
8023 parameter bindings are okay, FALSE otherwise. */
8024 bool
8025 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8026 {
8027 int i, ntparms = TREE_VEC_LENGTH (tparms);
8028 bool ret = true;
8029
8030 /* We're dealing with template parms in this process. */
8031 ++processing_template_decl;
8032
8033 targs = INNERMOST_TEMPLATE_ARGS (targs);
8034
8035 for (i = 0; i < ntparms; ++i)
8036 {
8037 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8038 tree targ = TREE_VEC_ELT (targs, i);
8039
8040 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8041 {
8042 tree packed_args = NULL_TREE;
8043 int idx, len = 1;
8044
8045 if (ARGUMENT_PACK_P (targ))
8046 {
8047 /* Look inside the argument pack. */
8048 packed_args = ARGUMENT_PACK_ARGS (targ);
8049 len = TREE_VEC_LENGTH (packed_args);
8050 }
8051
8052 for (idx = 0; idx < len; ++idx)
8053 {
8054 tree targ_parms = NULL_TREE;
8055
8056 if (packed_args)
8057 /* Extract the next argument from the argument
8058 pack. */
8059 targ = TREE_VEC_ELT (packed_args, idx);
8060
8061 if (PACK_EXPANSION_P (targ))
8062 /* Look at the pattern of the pack expansion. */
8063 targ = PACK_EXPANSION_PATTERN (targ);
8064
8065 /* Extract the template parameters from the template
8066 argument. */
8067 if (TREE_CODE (targ) == TEMPLATE_DECL)
8068 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8069 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8070 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8071
8072 /* Verify that we can coerce the template template
8073 parameters from the template argument to the template
8074 parameter. This requires an exact match. */
8075 if (targ_parms
8076 && !coerce_template_template_parms
8077 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8078 targ_parms,
8079 tf_none,
8080 tparm,
8081 targs))
8082 {
8083 ret = false;
8084 goto out;
8085 }
8086 }
8087 }
8088 }
8089
8090 out:
8091
8092 --processing_template_decl;
8093 return ret;
8094 }
8095
8096 /* Since type attributes aren't mangled, we need to strip them from
8097 template type arguments. */
8098
8099 tree
8100 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8101 {
8102 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8103 return arg;
8104 bool removed_attributes = false;
8105 tree canon = strip_typedefs (arg, &removed_attributes);
8106 if (removed_attributes
8107 && (complain & tf_warning))
8108 warning (OPT_Wignored_attributes,
8109 "ignoring attributes on template argument %qT", arg);
8110 return canon;
8111 }
8112
8113 /* And from inside dependent non-type arguments like sizeof(Type). */
8114
8115 static tree
8116 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8117 {
8118 if (!arg || arg == error_mark_node)
8119 return arg;
8120 bool removed_attributes = false;
8121 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8122 if (removed_attributes
8123 && (complain & tf_warning))
8124 warning (OPT_Wignored_attributes,
8125 "ignoring attributes in template argument %qE", arg);
8126 return canon;
8127 }
8128
8129 // A template declaration can be substituted for a constrained
8130 // template template parameter only when the argument is more
8131 // constrained than the parameter.
8132 static bool
8133 is_compatible_template_arg (tree parm, tree arg)
8134 {
8135 tree parm_cons = get_constraints (parm);
8136
8137 /* For now, allow constrained template template arguments
8138 and unconstrained template template parameters. */
8139 if (parm_cons == NULL_TREE)
8140 return true;
8141
8142 /* If the template parameter is constrained, we need to rewrite its
8143 constraints in terms of the ARG's template parameters. This ensures
8144 that all of the template parameter types will have the same depth.
8145
8146 Note that this is only valid when coerce_template_template_parm is
8147 true for the innermost template parameters of PARM and ARG. In other
8148 words, because coercion is successful, this conversion will be valid. */
8149 tree new_args = NULL_TREE;
8150 if (parm_cons)
8151 {
8152 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8153 new_args = template_parms_level_to_args (aparms);
8154 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8155 tf_none, NULL_TREE);
8156 if (parm_cons == error_mark_node)
8157 return false;
8158 }
8159
8160 return weakly_subsumes (parm_cons, new_args, arg);
8161 }
8162
8163 // Convert a placeholder argument into a binding to the original
8164 // parameter. The original parameter is saved as the TREE_TYPE of
8165 // ARG.
8166 static inline tree
8167 convert_wildcard_argument (tree parm, tree arg)
8168 {
8169 TREE_TYPE (arg) = parm;
8170 return arg;
8171 }
8172
8173 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8174 because one of them is dependent. But we need to represent the
8175 conversion for the benefit of cp_tree_equal. */
8176
8177 static tree
8178 maybe_convert_nontype_argument (tree type, tree arg)
8179 {
8180 /* Auto parms get no conversion. */
8181 if (type_uses_auto (type))
8182 return arg;
8183 /* We don't need or want to add this conversion now if we're going to use the
8184 argument for deduction. */
8185 if (value_dependent_expression_p (arg))
8186 return arg;
8187
8188 type = cv_unqualified (type);
8189 tree argtype = TREE_TYPE (arg);
8190 if (same_type_p (type, argtype))
8191 return arg;
8192
8193 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8194 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8195 return arg;
8196 }
8197
8198 /* Convert the indicated template ARG as necessary to match the
8199 indicated template PARM. Returns the converted ARG, or
8200 error_mark_node if the conversion was unsuccessful. Error and
8201 warning messages are issued under control of COMPLAIN. This
8202 conversion is for the Ith parameter in the parameter list. ARGS is
8203 the full set of template arguments deduced so far. */
8204
8205 static tree
8206 convert_template_argument (tree parm,
8207 tree arg,
8208 tree args,
8209 tsubst_flags_t complain,
8210 int i,
8211 tree in_decl)
8212 {
8213 tree orig_arg;
8214 tree val;
8215 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8216
8217 if (parm == error_mark_node || error_operand_p (arg))
8218 return error_mark_node;
8219
8220 /* Trivially convert placeholders. */
8221 if (TREE_CODE (arg) == WILDCARD_DECL)
8222 return convert_wildcard_argument (parm, arg);
8223
8224 if (arg == any_targ_node)
8225 return arg;
8226
8227 if (TREE_CODE (arg) == TREE_LIST
8228 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8229 {
8230 /* The template argument was the name of some
8231 member function. That's usually
8232 invalid, but static members are OK. In any
8233 case, grab the underlying fields/functions
8234 and issue an error later if required. */
8235 TREE_TYPE (arg) = unknown_type_node;
8236 }
8237
8238 orig_arg = arg;
8239
8240 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8241 requires_type = (TREE_CODE (parm) == TYPE_DECL
8242 || requires_tmpl_type);
8243
8244 /* When determining whether an argument pack expansion is a template,
8245 look at the pattern. */
8246 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8247 arg = PACK_EXPANSION_PATTERN (arg);
8248
8249 /* Deal with an injected-class-name used as a template template arg. */
8250 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8251 {
8252 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8253 if (TREE_CODE (t) == TEMPLATE_DECL)
8254 {
8255 if (cxx_dialect >= cxx11)
8256 /* OK under DR 1004. */;
8257 else if (complain & tf_warning_or_error)
8258 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8259 " used as template template argument", TYPE_NAME (arg));
8260 else if (flag_pedantic_errors)
8261 t = arg;
8262
8263 arg = t;
8264 }
8265 }
8266
8267 is_tmpl_type =
8268 ((TREE_CODE (arg) == TEMPLATE_DECL
8269 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8270 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8271 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8272 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8273
8274 if (is_tmpl_type
8275 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8276 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8277 arg = TYPE_STUB_DECL (arg);
8278
8279 is_type = TYPE_P (arg) || is_tmpl_type;
8280
8281 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8282 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8283 {
8284 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8285 {
8286 if (complain & tf_error)
8287 error ("invalid use of destructor %qE as a type", orig_arg);
8288 return error_mark_node;
8289 }
8290
8291 permerror (input_location,
8292 "to refer to a type member of a template parameter, "
8293 "use %<typename %E%>", orig_arg);
8294
8295 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8296 TREE_OPERAND (arg, 1),
8297 typename_type,
8298 complain);
8299 arg = orig_arg;
8300 is_type = 1;
8301 }
8302 if (is_type != requires_type)
8303 {
8304 if (in_decl)
8305 {
8306 if (complain & tf_error)
8307 {
8308 error ("type/value mismatch at argument %d in template "
8309 "parameter list for %qD",
8310 i + 1, in_decl);
8311 if (is_type)
8312 {
8313 /* The template argument is a type, but we're expecting
8314 an expression. */
8315 inform (input_location,
8316 " expected a constant of type %qT, got %qT",
8317 TREE_TYPE (parm),
8318 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8319 /* [temp.arg]/2: "In a template-argument, an ambiguity
8320 between a type-id and an expression is resolved to a
8321 type-id, regardless of the form of the corresponding
8322 template-parameter." So give the user a clue. */
8323 if (TREE_CODE (arg) == FUNCTION_TYPE)
8324 inform (input_location, " ambiguous template argument "
8325 "for non-type template parameter is treated as "
8326 "function type");
8327 }
8328 else if (requires_tmpl_type)
8329 inform (input_location,
8330 " expected a class template, got %qE", orig_arg);
8331 else
8332 inform (input_location,
8333 " expected a type, got %qE", orig_arg);
8334 }
8335 }
8336 return error_mark_node;
8337 }
8338 if (is_tmpl_type ^ requires_tmpl_type)
8339 {
8340 if (in_decl && (complain & tf_error))
8341 {
8342 error ("type/value mismatch at argument %d in template "
8343 "parameter list for %qD",
8344 i + 1, in_decl);
8345 if (is_tmpl_type)
8346 inform (input_location,
8347 " expected a type, got %qT", DECL_NAME (arg));
8348 else
8349 inform (input_location,
8350 " expected a class template, got %qT", orig_arg);
8351 }
8352 return error_mark_node;
8353 }
8354
8355 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8356 /* We already did the appropriate conversion when packing args. */
8357 val = orig_arg;
8358 else if (is_type)
8359 {
8360 if (requires_tmpl_type)
8361 {
8362 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8363 /* The number of argument required is not known yet.
8364 Just accept it for now. */
8365 val = orig_arg;
8366 else
8367 {
8368 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8369 tree argparm;
8370
8371 /* Strip alias templates that are equivalent to another
8372 template. */
8373 arg = get_underlying_template (arg);
8374 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8375
8376 if (coerce_template_template_parms (parmparm, argparm,
8377 complain, in_decl,
8378 args))
8379 {
8380 val = arg;
8381
8382 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8383 TEMPLATE_DECL. */
8384 if (val != error_mark_node)
8385 {
8386 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8387 val = TREE_TYPE (val);
8388 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8389 val = make_pack_expansion (val, complain);
8390 }
8391 }
8392 else
8393 {
8394 if (in_decl && (complain & tf_error))
8395 {
8396 error ("type/value mismatch at argument %d in "
8397 "template parameter list for %qD",
8398 i + 1, in_decl);
8399 inform (input_location,
8400 " expected a template of type %qD, got %qT",
8401 parm, orig_arg);
8402 }
8403
8404 val = error_mark_node;
8405 }
8406
8407 // Check that the constraints are compatible before allowing the
8408 // substitution.
8409 if (val != error_mark_node)
8410 if (!is_compatible_template_arg (parm, arg))
8411 {
8412 if (in_decl && (complain & tf_error))
8413 {
8414 error ("constraint mismatch at argument %d in "
8415 "template parameter list for %qD",
8416 i + 1, in_decl);
8417 inform (input_location, " expected %qD but got %qD",
8418 parm, arg);
8419 }
8420 val = error_mark_node;
8421 }
8422 }
8423 }
8424 else
8425 val = orig_arg;
8426 /* We only form one instance of each template specialization.
8427 Therefore, if we use a non-canonical variant (i.e., a
8428 typedef), any future messages referring to the type will use
8429 the typedef, which is confusing if those future uses do not
8430 themselves also use the typedef. */
8431 if (TYPE_P (val))
8432 val = canonicalize_type_argument (val, complain);
8433 }
8434 else
8435 {
8436 tree t = TREE_TYPE (parm);
8437
8438 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8439 > TMPL_ARGS_DEPTH (args))
8440 /* We don't have enough levels of args to do any substitution. This
8441 can happen in the context of -fnew-ttp-matching. */;
8442 else if (tree a = type_uses_auto (t))
8443 {
8444 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8445 if (t == error_mark_node)
8446 return error_mark_node;
8447 }
8448 else
8449 t = tsubst (t, args, complain, in_decl);
8450
8451 if (invalid_nontype_parm_type_p (t, complain))
8452 return error_mark_node;
8453
8454 if (t != TREE_TYPE (parm))
8455 t = canonicalize_type_argument (t, complain);
8456
8457 if (!type_dependent_expression_p (orig_arg)
8458 && !uses_template_parms (t))
8459 /* We used to call digest_init here. However, digest_init
8460 will report errors, which we don't want when complain
8461 is zero. More importantly, digest_init will try too
8462 hard to convert things: for example, `0' should not be
8463 converted to pointer type at this point according to
8464 the standard. Accepting this is not merely an
8465 extension, since deciding whether or not these
8466 conversions can occur is part of determining which
8467 function template to call, or whether a given explicit
8468 argument specification is valid. */
8469 val = convert_nontype_argument (t, orig_arg, complain);
8470 else
8471 {
8472 val = canonicalize_expr_argument (orig_arg, complain);
8473 val = maybe_convert_nontype_argument (t, val);
8474 }
8475
8476
8477 if (val == NULL_TREE)
8478 val = error_mark_node;
8479 else if (val == error_mark_node && (complain & tf_error))
8480 error_at (cp_expr_loc_or_input_loc (orig_arg),
8481 "could not convert template argument %qE from %qT to %qT",
8482 orig_arg, TREE_TYPE (orig_arg), t);
8483
8484 if (INDIRECT_REF_P (val))
8485 {
8486 /* Reject template arguments that are references to built-in
8487 functions with no library fallbacks. */
8488 const_tree inner = TREE_OPERAND (val, 0);
8489 const_tree innertype = TREE_TYPE (inner);
8490 if (innertype
8491 && TYPE_REF_P (innertype)
8492 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8493 && TREE_OPERAND_LENGTH (inner) > 0
8494 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8495 return error_mark_node;
8496 }
8497
8498 if (TREE_CODE (val) == SCOPE_REF)
8499 {
8500 /* Strip typedefs from the SCOPE_REF. */
8501 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8502 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8503 complain);
8504 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8505 QUALIFIED_NAME_IS_TEMPLATE (val));
8506 }
8507 }
8508
8509 return val;
8510 }
8511
8512 /* Coerces the remaining template arguments in INNER_ARGS (from
8513 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8514 Returns the coerced argument pack. PARM_IDX is the position of this
8515 parameter in the template parameter list. ARGS is the original
8516 template argument list. */
8517 static tree
8518 coerce_template_parameter_pack (tree parms,
8519 int parm_idx,
8520 tree args,
8521 tree inner_args,
8522 int arg_idx,
8523 tree new_args,
8524 int* lost,
8525 tree in_decl,
8526 tsubst_flags_t complain)
8527 {
8528 tree parm = TREE_VEC_ELT (parms, parm_idx);
8529 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8530 tree packed_args;
8531 tree argument_pack;
8532 tree packed_parms = NULL_TREE;
8533
8534 if (arg_idx > nargs)
8535 arg_idx = nargs;
8536
8537 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8538 {
8539 /* When the template parameter is a non-type template parameter pack
8540 or template template parameter pack whose type or template
8541 parameters use parameter packs, we know exactly how many arguments
8542 we are looking for. Build a vector of the instantiated decls for
8543 these template parameters in PACKED_PARMS. */
8544 /* We can't use make_pack_expansion here because it would interpret a
8545 _DECL as a use rather than a declaration. */
8546 tree decl = TREE_VALUE (parm);
8547 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8548 SET_PACK_EXPANSION_PATTERN (exp, decl);
8549 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8550 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8551
8552 TREE_VEC_LENGTH (args)--;
8553 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8554 TREE_VEC_LENGTH (args)++;
8555
8556 if (packed_parms == error_mark_node)
8557 return error_mark_node;
8558
8559 /* If we're doing a partial instantiation of a member template,
8560 verify that all of the types used for the non-type
8561 template parameter pack are, in fact, valid for non-type
8562 template parameters. */
8563 if (arg_idx < nargs
8564 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8565 {
8566 int j, len = TREE_VEC_LENGTH (packed_parms);
8567 for (j = 0; j < len; ++j)
8568 {
8569 tree t = TREE_VEC_ELT (packed_parms, j);
8570 if (TREE_CODE (t) == PARM_DECL
8571 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8572 return error_mark_node;
8573 }
8574 /* We don't know how many args we have yet, just
8575 use the unconverted ones for now. */
8576 return NULL_TREE;
8577 }
8578
8579 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8580 }
8581 /* Check if we have a placeholder pack, which indicates we're
8582 in the context of a introduction list. In that case we want
8583 to match this pack to the single placeholder. */
8584 else if (arg_idx < nargs
8585 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8586 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8587 {
8588 nargs = arg_idx + 1;
8589 packed_args = make_tree_vec (1);
8590 }
8591 else
8592 packed_args = make_tree_vec (nargs - arg_idx);
8593
8594 /* Convert the remaining arguments, which will be a part of the
8595 parameter pack "parm". */
8596 int first_pack_arg = arg_idx;
8597 for (; arg_idx < nargs; ++arg_idx)
8598 {
8599 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8600 tree actual_parm = TREE_VALUE (parm);
8601 int pack_idx = arg_idx - first_pack_arg;
8602
8603 if (packed_parms)
8604 {
8605 /* Once we've packed as many args as we have types, stop. */
8606 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8607 break;
8608 else if (PACK_EXPANSION_P (arg))
8609 /* We don't know how many args we have yet, just
8610 use the unconverted ones for now. */
8611 return NULL_TREE;
8612 else
8613 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8614 }
8615
8616 if (arg == error_mark_node)
8617 {
8618 if (complain & tf_error)
8619 error ("template argument %d is invalid", arg_idx + 1);
8620 }
8621 else
8622 arg = convert_template_argument (actual_parm,
8623 arg, new_args, complain, parm_idx,
8624 in_decl);
8625 if (arg == error_mark_node)
8626 (*lost)++;
8627 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8628 }
8629
8630 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8631 && TREE_VEC_LENGTH (packed_args) > 0)
8632 {
8633 if (complain & tf_error)
8634 error ("wrong number of template arguments (%d, should be %d)",
8635 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8636 return error_mark_node;
8637 }
8638
8639 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8640 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8641 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8642 else
8643 {
8644 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8645 TREE_CONSTANT (argument_pack) = 1;
8646 }
8647
8648 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8649 if (CHECKING_P)
8650 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8651 TREE_VEC_LENGTH (packed_args));
8652 return argument_pack;
8653 }
8654
8655 /* Returns the number of pack expansions in the template argument vector
8656 ARGS. */
8657
8658 static int
8659 pack_expansion_args_count (tree args)
8660 {
8661 int i;
8662 int count = 0;
8663 if (args)
8664 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8665 {
8666 tree elt = TREE_VEC_ELT (args, i);
8667 if (elt && PACK_EXPANSION_P (elt))
8668 ++count;
8669 }
8670 return count;
8671 }
8672
8673 /* Convert all template arguments to their appropriate types, and
8674 return a vector containing the innermost resulting template
8675 arguments. If any error occurs, return error_mark_node. Error and
8676 warning messages are issued under control of COMPLAIN.
8677
8678 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8679 for arguments not specified in ARGS. Otherwise, if
8680 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8681 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8682 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8683 ARGS. */
8684
8685 static tree
8686 coerce_template_parms (tree parms,
8687 tree args,
8688 tree in_decl,
8689 tsubst_flags_t complain,
8690 bool require_all_args,
8691 bool use_default_args)
8692 {
8693 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8694 tree orig_inner_args;
8695 tree inner_args;
8696 tree new_args;
8697 tree new_inner_args;
8698
8699 /* When used as a boolean value, indicates whether this is a
8700 variadic template parameter list. Since it's an int, we can also
8701 subtract it from nparms to get the number of non-variadic
8702 parameters. */
8703 int variadic_p = 0;
8704 int variadic_args_p = 0;
8705 int post_variadic_parms = 0;
8706
8707 /* Adjustment to nparms for fixed parameter packs. */
8708 int fixed_pack_adjust = 0;
8709 int fixed_packs = 0;
8710 int missing = 0;
8711
8712 /* Likewise for parameters with default arguments. */
8713 int default_p = 0;
8714
8715 if (args == error_mark_node)
8716 return error_mark_node;
8717
8718 nparms = TREE_VEC_LENGTH (parms);
8719
8720 /* Determine if there are any parameter packs or default arguments. */
8721 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8722 {
8723 tree parm = TREE_VEC_ELT (parms, parm_idx);
8724 if (variadic_p)
8725 ++post_variadic_parms;
8726 if (template_parameter_pack_p (TREE_VALUE (parm)))
8727 ++variadic_p;
8728 if (TREE_PURPOSE (parm))
8729 ++default_p;
8730 }
8731
8732 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8733 /* If there are no parameters that follow a parameter pack, we need to
8734 expand any argument packs so that we can deduce a parameter pack from
8735 some non-packed args followed by an argument pack, as in variadic85.C.
8736 If there are such parameters, we need to leave argument packs intact
8737 so the arguments are assigned properly. This can happen when dealing
8738 with a nested class inside a partial specialization of a class
8739 template, as in variadic92.C, or when deducing a template parameter pack
8740 from a sub-declarator, as in variadic114.C. */
8741 if (!post_variadic_parms)
8742 inner_args = expand_template_argument_pack (inner_args);
8743
8744 /* Count any pack expansion args. */
8745 variadic_args_p = pack_expansion_args_count (inner_args);
8746
8747 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8748 if ((nargs - variadic_args_p > nparms && !variadic_p)
8749 || (nargs < nparms - variadic_p
8750 && require_all_args
8751 && !variadic_args_p
8752 && (!use_default_args
8753 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8754 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8755 {
8756 bad_nargs:
8757 if (complain & tf_error)
8758 {
8759 if (variadic_p || default_p)
8760 {
8761 nparms -= variadic_p + default_p;
8762 error ("wrong number of template arguments "
8763 "(%d, should be at least %d)", nargs, nparms);
8764 }
8765 else
8766 error ("wrong number of template arguments "
8767 "(%d, should be %d)", nargs, nparms);
8768
8769 if (in_decl)
8770 inform (DECL_SOURCE_LOCATION (in_decl),
8771 "provided for %qD", in_decl);
8772 }
8773
8774 return error_mark_node;
8775 }
8776 /* We can't pass a pack expansion to a non-pack parameter of an alias
8777 template (DR 1430). */
8778 else if (in_decl
8779 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8780 || concept_definition_p (in_decl))
8781 && variadic_args_p
8782 && nargs - variadic_args_p < nparms - variadic_p)
8783 {
8784 if (complain & tf_error)
8785 {
8786 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8787 {
8788 tree arg = TREE_VEC_ELT (inner_args, i);
8789 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8790
8791 if (PACK_EXPANSION_P (arg)
8792 && !template_parameter_pack_p (parm))
8793 {
8794 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8795 error_at (location_of (arg),
8796 "pack expansion argument for non-pack parameter "
8797 "%qD of alias template %qD", parm, in_decl);
8798 else
8799 error_at (location_of (arg),
8800 "pack expansion argument for non-pack parameter "
8801 "%qD of concept %qD", parm, in_decl);
8802 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8803 goto found;
8804 }
8805 }
8806 gcc_unreachable ();
8807 found:;
8808 }
8809 return error_mark_node;
8810 }
8811
8812 /* We need to evaluate the template arguments, even though this
8813 template-id may be nested within a "sizeof". */
8814 cp_evaluated ev;
8815
8816 new_inner_args = make_tree_vec (nparms);
8817 new_args = add_outermost_template_args (args, new_inner_args);
8818 int pack_adjust = 0;
8819 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8820 {
8821 tree arg;
8822 tree parm;
8823
8824 /* Get the Ith template parameter. */
8825 parm = TREE_VEC_ELT (parms, parm_idx);
8826
8827 if (parm == error_mark_node)
8828 {
8829 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8830 continue;
8831 }
8832
8833 /* Calculate the next argument. */
8834 if (arg_idx < nargs)
8835 arg = TREE_VEC_ELT (inner_args, arg_idx);
8836 else
8837 arg = NULL_TREE;
8838
8839 if (template_parameter_pack_p (TREE_VALUE (parm))
8840 && (arg || require_all_args || !(complain & tf_partial))
8841 && !(arg && ARGUMENT_PACK_P (arg)))
8842 {
8843 /* Some arguments will be placed in the
8844 template parameter pack PARM. */
8845 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8846 inner_args, arg_idx,
8847 new_args, &lost,
8848 in_decl, complain);
8849
8850 if (arg == NULL_TREE)
8851 {
8852 /* We don't know how many args we have yet, just use the
8853 unconverted (and still packed) ones for now. */
8854 new_inner_args = orig_inner_args;
8855 arg_idx = nargs;
8856 break;
8857 }
8858
8859 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8860
8861 /* Store this argument. */
8862 if (arg == error_mark_node)
8863 {
8864 lost++;
8865 /* We are done with all of the arguments. */
8866 arg_idx = nargs;
8867 break;
8868 }
8869 else
8870 {
8871 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8872 arg_idx += pack_adjust;
8873 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8874 {
8875 ++fixed_packs;
8876 fixed_pack_adjust += pack_adjust;
8877 }
8878 }
8879
8880 continue;
8881 }
8882 else if (arg)
8883 {
8884 if (PACK_EXPANSION_P (arg))
8885 {
8886 /* "If every valid specialization of a variadic template
8887 requires an empty template parameter pack, the template is
8888 ill-formed, no diagnostic required." So check that the
8889 pattern works with this parameter. */
8890 tree pattern = PACK_EXPANSION_PATTERN (arg);
8891 tree conv = convert_template_argument (TREE_VALUE (parm),
8892 pattern, new_args,
8893 complain, parm_idx,
8894 in_decl);
8895 if (conv == error_mark_node)
8896 {
8897 if (complain & tf_error)
8898 inform (input_location, "so any instantiation with a "
8899 "non-empty parameter pack would be ill-formed");
8900 ++lost;
8901 }
8902 else if (TYPE_P (conv) && !TYPE_P (pattern))
8903 /* Recover from missing typename. */
8904 TREE_VEC_ELT (inner_args, arg_idx)
8905 = make_pack_expansion (conv, complain);
8906
8907 /* We don't know how many args we have yet, just
8908 use the unconverted ones for now. */
8909 new_inner_args = inner_args;
8910 arg_idx = nargs;
8911 break;
8912 }
8913 }
8914 else if (require_all_args)
8915 {
8916 /* There must be a default arg in this case. */
8917 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8918 complain, in_decl);
8919 /* The position of the first default template argument,
8920 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8921 Record that. */
8922 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8923 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8924 arg_idx - pack_adjust);
8925 }
8926 else
8927 break;
8928
8929 if (arg == error_mark_node)
8930 {
8931 if (complain & tf_error)
8932 error ("template argument %d is invalid", arg_idx + 1);
8933 }
8934 else if (!arg)
8935 {
8936 /* This can occur if there was an error in the template
8937 parameter list itself (which we would already have
8938 reported) that we are trying to recover from, e.g., a class
8939 template with a parameter list such as
8940 template<typename..., typename> (cpp0x/variadic150.C). */
8941 ++lost;
8942
8943 /* This can also happen with a fixed parameter pack (71834). */
8944 if (arg_idx >= nargs)
8945 ++missing;
8946 }
8947 else
8948 arg = convert_template_argument (TREE_VALUE (parm),
8949 arg, new_args, complain,
8950 parm_idx, in_decl);
8951
8952 if (arg == error_mark_node)
8953 lost++;
8954
8955 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8956 }
8957
8958 if (missing || arg_idx < nargs - variadic_args_p)
8959 {
8960 /* If we had fixed parameter packs, we didn't know how many arguments we
8961 actually needed earlier; now we do. */
8962 nparms += fixed_pack_adjust;
8963 variadic_p -= fixed_packs;
8964 goto bad_nargs;
8965 }
8966
8967 if (arg_idx < nargs)
8968 {
8969 /* We had some pack expansion arguments that will only work if the packs
8970 are empty, but wait until instantiation time to complain.
8971 See variadic-ttp3.C. */
8972
8973 /* Except that we can't provide empty packs to alias templates or
8974 concepts when there are no corresponding parameters. Basically,
8975 we can get here with this:
8976
8977 template<typename T> concept C = true;
8978
8979 template<typename... Args>
8980 requires C<Args...>
8981 void f();
8982
8983 When parsing C<Args...>, we try to form a concept check of
8984 C<?, Args...>. Without the extra check for substituting an empty
8985 pack past the last parameter, we can accept the check as valid.
8986
8987 FIXME: This may be valid for alias templates (but I doubt it).
8988
8989 FIXME: The error could be better also. */
8990 if (in_decl && concept_definition_p (in_decl))
8991 {
8992 if (complain & tf_error)
8993 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8994 "too many arguments");
8995 return error_mark_node;
8996 }
8997
8998 int len = nparms + (nargs - arg_idx);
8999 tree args = make_tree_vec (len);
9000 int i = 0;
9001 for (; i < nparms; ++i)
9002 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9003 for (; i < len; ++i, ++arg_idx)
9004 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9005 arg_idx - pack_adjust);
9006 new_inner_args = args;
9007 }
9008
9009 if (lost)
9010 {
9011 gcc_assert (!(complain & tf_error) || seen_error ());
9012 return error_mark_node;
9013 }
9014
9015 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9016 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9017 TREE_VEC_LENGTH (new_inner_args));
9018
9019 return new_inner_args;
9020 }
9021
9022 /* Convert all template arguments to their appropriate types, and
9023 return a vector containing the innermost resulting template
9024 arguments. If any error occurs, return error_mark_node. Error and
9025 warning messages are not issued.
9026
9027 Note that no function argument deduction is performed, and default
9028 arguments are used to fill in unspecified arguments. */
9029 tree
9030 coerce_template_parms (tree parms, tree args, tree in_decl)
9031 {
9032 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9033 }
9034
9035 /* Convert all template arguments to their appropriate type, and
9036 instantiate default arguments as needed. This returns a vector
9037 containing the innermost resulting template arguments, or
9038 error_mark_node if unsuccessful. */
9039 tree
9040 coerce_template_parms (tree parms, tree args, tree in_decl,
9041 tsubst_flags_t complain)
9042 {
9043 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9044 }
9045
9046 /* Like coerce_template_parms. If PARMS represents all template
9047 parameters levels, this function returns a vector of vectors
9048 representing all the resulting argument levels. Note that in this
9049 case, only the innermost arguments are coerced because the
9050 outermost ones are supposed to have been coerced already.
9051
9052 Otherwise, if PARMS represents only (the innermost) vector of
9053 parameters, this function returns a vector containing just the
9054 innermost resulting arguments. */
9055
9056 static tree
9057 coerce_innermost_template_parms (tree parms,
9058 tree args,
9059 tree in_decl,
9060 tsubst_flags_t complain,
9061 bool require_all_args,
9062 bool use_default_args)
9063 {
9064 int parms_depth = TMPL_PARMS_DEPTH (parms);
9065 int args_depth = TMPL_ARGS_DEPTH (args);
9066 tree coerced_args;
9067
9068 if (parms_depth > 1)
9069 {
9070 coerced_args = make_tree_vec (parms_depth);
9071 tree level;
9072 int cur_depth;
9073
9074 for (level = parms, cur_depth = parms_depth;
9075 parms_depth > 0 && level != NULL_TREE;
9076 level = TREE_CHAIN (level), --cur_depth)
9077 {
9078 tree l;
9079 if (cur_depth == args_depth)
9080 l = coerce_template_parms (TREE_VALUE (level),
9081 args, in_decl, complain,
9082 require_all_args,
9083 use_default_args);
9084 else
9085 l = TMPL_ARGS_LEVEL (args, cur_depth);
9086
9087 if (l == error_mark_node)
9088 return error_mark_node;
9089
9090 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9091 }
9092 }
9093 else
9094 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9095 args, in_decl, complain,
9096 require_all_args,
9097 use_default_args);
9098 return coerced_args;
9099 }
9100
9101 /* Returns true if T is a wrapper to make a C++20 template parameter
9102 object const. */
9103
9104 static bool
9105 class_nttp_const_wrapper_p (tree t)
9106 {
9107 if (cxx_dialect < cxx20)
9108 return false;
9109 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9110 && CP_TYPE_CONST_P (TREE_TYPE (t))
9111 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9112 }
9113
9114 /* Returns 1 if template args OT and NT are equivalent. */
9115
9116 int
9117 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9118 {
9119 if (nt == ot)
9120 return 1;
9121 if (nt == NULL_TREE || ot == NULL_TREE)
9122 return false;
9123 if (nt == any_targ_node || ot == any_targ_node)
9124 return true;
9125
9126 if (class_nttp_const_wrapper_p (nt))
9127 nt = TREE_OPERAND (nt, 0);
9128 if (class_nttp_const_wrapper_p (ot))
9129 ot = TREE_OPERAND (ot, 0);
9130
9131 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9132 /* For member templates */
9133 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9134 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9135 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9136 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9137 PACK_EXPANSION_PATTERN (nt))
9138 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9139 PACK_EXPANSION_EXTRA_ARGS (nt)));
9140 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9141 return cp_tree_equal (ot, nt);
9142 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9143 gcc_unreachable ();
9144 else if (TYPE_P (nt) || TYPE_P (ot))
9145 {
9146 if (!(TYPE_P (nt) && TYPE_P (ot)))
9147 return false;
9148 /* Don't treat an alias template specialization with dependent
9149 arguments as equivalent to its underlying type when used as a
9150 template argument; we need them to be distinct so that we
9151 substitute into the specialization arguments at instantiation
9152 time. And aliases can't be equivalent without being ==, so
9153 we don't need to look any deeper.
9154
9155 During partial ordering, however, we need to treat them normally so
9156 that we can order uses of the same alias with different
9157 cv-qualification (79960). */
9158 if (!partial_order
9159 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9160 return false;
9161 else
9162 return same_type_p (ot, nt);
9163 }
9164 else
9165 {
9166 /* Try to treat a template non-type argument that has been converted
9167 to the parameter type as equivalent to one that hasn't yet. */
9168 for (enum tree_code code1 = TREE_CODE (ot);
9169 CONVERT_EXPR_CODE_P (code1)
9170 || code1 == NON_LVALUE_EXPR;
9171 code1 = TREE_CODE (ot))
9172 ot = TREE_OPERAND (ot, 0);
9173
9174 for (enum tree_code code2 = TREE_CODE (nt);
9175 CONVERT_EXPR_CODE_P (code2)
9176 || code2 == NON_LVALUE_EXPR;
9177 code2 = TREE_CODE (nt))
9178 nt = TREE_OPERAND (nt, 0);
9179
9180 return cp_tree_equal (ot, nt);
9181 }
9182 }
9183
9184 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9185 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9186 NEWARG_PTR with the offending arguments if they are non-NULL. */
9187
9188 int
9189 comp_template_args (tree oldargs, tree newargs,
9190 tree *oldarg_ptr, tree *newarg_ptr,
9191 bool partial_order)
9192 {
9193 int i;
9194
9195 if (oldargs == newargs)
9196 return 1;
9197
9198 if (!oldargs || !newargs)
9199 return 0;
9200
9201 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9202 return 0;
9203
9204 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9205 {
9206 tree nt = TREE_VEC_ELT (newargs, i);
9207 tree ot = TREE_VEC_ELT (oldargs, i);
9208
9209 if (! template_args_equal (ot, nt, partial_order))
9210 {
9211 if (oldarg_ptr != NULL)
9212 *oldarg_ptr = ot;
9213 if (newarg_ptr != NULL)
9214 *newarg_ptr = nt;
9215 return 0;
9216 }
9217 }
9218 return 1;
9219 }
9220
9221 inline bool
9222 comp_template_args_porder (tree oargs, tree nargs)
9223 {
9224 return comp_template_args (oargs, nargs, NULL, NULL, true);
9225 }
9226
9227 /* Implement a freelist interface for objects of type T.
9228
9229 Head is a separate object, rather than a regular member, so that we
9230 can define it as a GTY deletable pointer, which is highly
9231 desirable. A data member could be declared that way, but then the
9232 containing object would implicitly get GTY((user)), which would
9233 prevent us from instantiating freelists as global objects.
9234 Although this way we can create freelist global objects, they're
9235 such thin wrappers that instantiating temporaries at every use
9236 loses nothing and saves permanent storage for the freelist object.
9237
9238 Member functions next, anew, poison and reinit have default
9239 implementations that work for most of the types we're interested
9240 in, but if they don't work for some type, they should be explicitly
9241 specialized. See the comments before them for requirements, and
9242 the example specializations for the tree_list_freelist. */
9243 template <typename T>
9244 class freelist
9245 {
9246 /* Return the next object in a chain. We could just do type
9247 punning, but if we access the object with its underlying type, we
9248 avoid strict-aliasing trouble. This needs only work between
9249 poison and reinit. */
9250 static T *&next (T *obj) { return obj->next; }
9251
9252 /* Return a newly allocated, uninitialized or minimally-initialized
9253 object of type T. Any initialization performed by anew should
9254 either remain across the life of the object and the execution of
9255 poison, or be redone by reinit. */
9256 static T *anew () { return ggc_alloc<T> (); }
9257
9258 /* Optionally scribble all over the bits holding the object, so that
9259 they become (mostly?) uninitialized memory. This is called while
9260 preparing to make the object part of the free list. */
9261 static void poison (T *obj) {
9262 T *p ATTRIBUTE_UNUSED = obj;
9263 T **q ATTRIBUTE_UNUSED = &next (obj);
9264
9265 #ifdef ENABLE_GC_CHECKING
9266 /* Poison the data, to indicate the data is garbage. */
9267 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9268 memset (p, 0xa5, sizeof (*p));
9269 #endif
9270 /* Let valgrind know the object is free. */
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9272
9273 /* Let valgrind know the next portion of the object is available,
9274 but uninitialized. */
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9276 }
9277
9278 /* Bring an object that underwent at least one lifecycle after anew
9279 and before the most recent free and poison, back to a usable
9280 state, reinitializing whatever is needed for it to be
9281 functionally equivalent to an object just allocated and returned
9282 by anew. This may poison or clear the next field, used by
9283 freelist housekeeping after poison was called. */
9284 static void reinit (T *obj) {
9285 T **q ATTRIBUTE_UNUSED = &next (obj);
9286
9287 #ifdef ENABLE_GC_CHECKING
9288 memset (q, 0xa5, sizeof (*q));
9289 #endif
9290 /* Let valgrind know the entire object is available, but
9291 uninitialized. */
9292 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9293 }
9294
9295 /* Reference a GTY-deletable pointer that points to the first object
9296 in the free list proper. */
9297 T *&head;
9298 public:
9299 /* Construct a freelist object chaining objects off of HEAD. */
9300 freelist (T *&head) : head(head) {}
9301
9302 /* Add OBJ to the free object list. The former head becomes OBJ's
9303 successor. */
9304 void free (T *obj)
9305 {
9306 poison (obj);
9307 next (obj) = head;
9308 head = obj;
9309 }
9310
9311 /* Take an object from the free list, if one is available, or
9312 allocate a new one. Objects taken from the free list should be
9313 regarded as filled with garbage, except for bits that are
9314 configured to be preserved across free and alloc. */
9315 T *alloc ()
9316 {
9317 if (head)
9318 {
9319 T *obj = head;
9320 head = next (head);
9321 reinit (obj);
9322 return obj;
9323 }
9324 else
9325 return anew ();
9326 }
9327 };
9328
9329 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9330 want to allocate a TREE_LIST using the usual interface, and ensure
9331 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9332 build_tree_list logic in reinit, so this could go out of sync. */
9333 template <>
9334 inline tree &
9335 freelist<tree_node>::next (tree obj)
9336 {
9337 return TREE_CHAIN (obj);
9338 }
9339 template <>
9340 inline tree
9341 freelist<tree_node>::anew ()
9342 {
9343 return build_tree_list (NULL, NULL);
9344 }
9345 template <>
9346 inline void
9347 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9348 {
9349 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9350 tree p ATTRIBUTE_UNUSED = obj;
9351 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9352 tree *q ATTRIBUTE_UNUSED = &next (obj);
9353
9354 #ifdef ENABLE_GC_CHECKING
9355 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9356
9357 /* Poison the data, to indicate the data is garbage. */
9358 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9359 memset (p, 0xa5, size);
9360 #endif
9361 /* Let valgrind know the object is free. */
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9363 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9364 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9366
9367 #ifdef ENABLE_GC_CHECKING
9368 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9369 /* Keep TREE_CHAIN functional. */
9370 TREE_SET_CODE (obj, TREE_LIST);
9371 #else
9372 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9373 #endif
9374 }
9375 template <>
9376 inline void
9377 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9378 {
9379 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9380
9381 #ifdef ENABLE_GC_CHECKING
9382 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9383 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9384 memset (obj, 0, sizeof (tree_list));
9385 #endif
9386
9387 /* Let valgrind know the entire object is available, but
9388 uninitialized. */
9389 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9390
9391 #ifdef ENABLE_GC_CHECKING
9392 TREE_SET_CODE (obj, TREE_LIST);
9393 #else
9394 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9395 #endif
9396 }
9397
9398 /* Point to the first object in the TREE_LIST freelist. */
9399 static GTY((deletable)) tree tree_list_freelist_head;
9400 /* Return the/an actual TREE_LIST freelist. */
9401 static inline freelist<tree_node>
9402 tree_list_freelist ()
9403 {
9404 return tree_list_freelist_head;
9405 }
9406
9407 /* Point to the first object in the tinst_level freelist. */
9408 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9409 /* Return the/an actual tinst_level freelist. */
9410 static inline freelist<tinst_level>
9411 tinst_level_freelist ()
9412 {
9413 return tinst_level_freelist_head;
9414 }
9415
9416 /* Point to the first object in the pending_template freelist. */
9417 static GTY((deletable)) pending_template *pending_template_freelist_head;
9418 /* Return the/an actual pending_template freelist. */
9419 static inline freelist<pending_template>
9420 pending_template_freelist ()
9421 {
9422 return pending_template_freelist_head;
9423 }
9424
9425 /* Build the TREE_LIST object out of a split list, store it
9426 permanently, and return it. */
9427 tree
9428 tinst_level::to_list ()
9429 {
9430 gcc_assert (split_list_p ());
9431 tree ret = tree_list_freelist ().alloc ();
9432 TREE_PURPOSE (ret) = tldcl;
9433 TREE_VALUE (ret) = targs;
9434 tldcl = ret;
9435 targs = NULL;
9436 gcc_assert (tree_list_p ());
9437 return ret;
9438 }
9439
9440 const unsigned short tinst_level::refcount_infinity;
9441
9442 /* Increment OBJ's refcount unless it is already infinite. */
9443 static tinst_level *
9444 inc_refcount_use (tinst_level *obj)
9445 {
9446 if (obj && obj->refcount != tinst_level::refcount_infinity)
9447 ++obj->refcount;
9448 return obj;
9449 }
9450
9451 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9452 void
9453 tinst_level::free (tinst_level *obj)
9454 {
9455 if (obj->tree_list_p ())
9456 tree_list_freelist ().free (obj->get_node ());
9457 tinst_level_freelist ().free (obj);
9458 }
9459
9460 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9461 OBJ's DECL and OBJ, and start over with the tinst_level object that
9462 used to be referenced by OBJ's NEXT. */
9463 static void
9464 dec_refcount_use (tinst_level *obj)
9465 {
9466 while (obj
9467 && obj->refcount != tinst_level::refcount_infinity
9468 && !--obj->refcount)
9469 {
9470 tinst_level *next = obj->next;
9471 tinst_level::free (obj);
9472 obj = next;
9473 }
9474 }
9475
9476 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9477 and of the former PTR. Omitting the second argument is equivalent
9478 to passing (T*)NULL; this is allowed because passing the
9479 zero-valued integral constant NULL confuses type deduction and/or
9480 overload resolution. */
9481 template <typename T>
9482 static void
9483 set_refcount_ptr (T *& ptr, T *obj = NULL)
9484 {
9485 T *save = ptr;
9486 ptr = inc_refcount_use (obj);
9487 dec_refcount_use (save);
9488 }
9489
9490 static void
9491 add_pending_template (tree d)
9492 {
9493 tree ti = (TYPE_P (d)
9494 ? CLASSTYPE_TEMPLATE_INFO (d)
9495 : DECL_TEMPLATE_INFO (d));
9496 struct pending_template *pt;
9497 int level;
9498
9499 if (TI_PENDING_TEMPLATE_FLAG (ti))
9500 return;
9501
9502 /* We are called both from instantiate_decl, where we've already had a
9503 tinst_level pushed, and instantiate_template, where we haven't.
9504 Compensate. */
9505 gcc_assert (TREE_CODE (d) != TREE_LIST);
9506 level = !current_tinst_level
9507 || current_tinst_level->maybe_get_node () != d;
9508
9509 if (level)
9510 push_tinst_level (d);
9511
9512 pt = pending_template_freelist ().alloc ();
9513 pt->next = NULL;
9514 pt->tinst = NULL;
9515 set_refcount_ptr (pt->tinst, current_tinst_level);
9516 if (last_pending_template)
9517 last_pending_template->next = pt;
9518 else
9519 pending_templates = pt;
9520
9521 last_pending_template = pt;
9522
9523 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9524
9525 if (level)
9526 pop_tinst_level ();
9527 }
9528
9529
9530 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9531 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9532 documentation for TEMPLATE_ID_EXPR. */
9533
9534 tree
9535 lookup_template_function (tree fns, tree arglist)
9536 {
9537 if (fns == error_mark_node || arglist == error_mark_node)
9538 return error_mark_node;
9539
9540 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9541
9542 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9543 {
9544 error ("%q#D is not a function template", fns);
9545 return error_mark_node;
9546 }
9547
9548 if (BASELINK_P (fns))
9549 {
9550 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9551 unknown_type_node,
9552 BASELINK_FUNCTIONS (fns),
9553 arglist);
9554 return fns;
9555 }
9556
9557 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9558 }
9559
9560 /* Within the scope of a template class S<T>, the name S gets bound
9561 (in build_self_reference) to a TYPE_DECL for the class, not a
9562 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9563 or one of its enclosing classes, and that type is a template,
9564 return the associated TEMPLATE_DECL. Otherwise, the original
9565 DECL is returned.
9566
9567 Also handle the case when DECL is a TREE_LIST of ambiguous
9568 injected-class-names from different bases. */
9569
9570 tree
9571 maybe_get_template_decl_from_type_decl (tree decl)
9572 {
9573 if (decl == NULL_TREE)
9574 return decl;
9575
9576 /* DR 176: A lookup that finds an injected-class-name (10.2
9577 [class.member.lookup]) can result in an ambiguity in certain cases
9578 (for example, if it is found in more than one base class). If all of
9579 the injected-class-names that are found refer to specializations of
9580 the same class template, and if the name is followed by a
9581 template-argument-list, the reference refers to the class template
9582 itself and not a specialization thereof, and is not ambiguous. */
9583 if (TREE_CODE (decl) == TREE_LIST)
9584 {
9585 tree t, tmpl = NULL_TREE;
9586 for (t = decl; t; t = TREE_CHAIN (t))
9587 {
9588 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9589 if (!tmpl)
9590 tmpl = elt;
9591 else if (tmpl != elt)
9592 break;
9593 }
9594 if (tmpl && t == NULL_TREE)
9595 return tmpl;
9596 else
9597 return decl;
9598 }
9599
9600 return (decl != NULL_TREE
9601 && DECL_SELF_REFERENCE_P (decl)
9602 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9603 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9604 }
9605
9606 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9607 parameters, find the desired type.
9608
9609 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9610
9611 IN_DECL, if non-NULL, is the template declaration we are trying to
9612 instantiate.
9613
9614 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9615 the class we are looking up.
9616
9617 Issue error and warning messages under control of COMPLAIN.
9618
9619 If the template class is really a local class in a template
9620 function, then the FUNCTION_CONTEXT is the function in which it is
9621 being instantiated.
9622
9623 ??? Note that this function is currently called *twice* for each
9624 template-id: the first time from the parser, while creating the
9625 incomplete type (finish_template_type), and the second type during the
9626 real instantiation (instantiate_template_class). This is surely something
9627 that we want to avoid. It also causes some problems with argument
9628 coercion (see convert_nontype_argument for more information on this). */
9629
9630 static tree
9631 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9632 int entering_scope, tsubst_flags_t complain)
9633 {
9634 tree templ = NULL_TREE, parmlist;
9635 tree t;
9636 spec_entry **slot;
9637 spec_entry *entry;
9638 spec_entry elt;
9639 hashval_t hash;
9640
9641 if (identifier_p (d1))
9642 {
9643 tree value = innermost_non_namespace_value (d1);
9644 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9645 templ = value;
9646 else
9647 {
9648 if (context)
9649 push_decl_namespace (context);
9650 templ = lookup_name (d1);
9651 templ = maybe_get_template_decl_from_type_decl (templ);
9652 if (context)
9653 pop_decl_namespace ();
9654 }
9655 if (templ)
9656 context = DECL_CONTEXT (templ);
9657 }
9658 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9659 {
9660 tree type = TREE_TYPE (d1);
9661
9662 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9663 an implicit typename for the second A. Deal with it. */
9664 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9665 type = TREE_TYPE (type);
9666
9667 if (CLASSTYPE_TEMPLATE_INFO (type))
9668 {
9669 templ = CLASSTYPE_TI_TEMPLATE (type);
9670 d1 = DECL_NAME (templ);
9671 }
9672 }
9673 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9674 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9675 {
9676 templ = TYPE_TI_TEMPLATE (d1);
9677 d1 = DECL_NAME (templ);
9678 }
9679 else if (DECL_TYPE_TEMPLATE_P (d1))
9680 {
9681 templ = d1;
9682 d1 = DECL_NAME (templ);
9683 context = DECL_CONTEXT (templ);
9684 }
9685 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9686 {
9687 templ = d1;
9688 d1 = DECL_NAME (templ);
9689 }
9690
9691 /* Issue an error message if we didn't find a template. */
9692 if (! templ)
9693 {
9694 if (complain & tf_error)
9695 error ("%qT is not a template", d1);
9696 return error_mark_node;
9697 }
9698
9699 if (TREE_CODE (templ) != TEMPLATE_DECL
9700 /* Make sure it's a user visible template, if it was named by
9701 the user. */
9702 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9703 && !PRIMARY_TEMPLATE_P (templ)))
9704 {
9705 if (complain & tf_error)
9706 {
9707 error ("non-template type %qT used as a template", d1);
9708 if (in_decl)
9709 error ("for template declaration %q+D", in_decl);
9710 }
9711 return error_mark_node;
9712 }
9713
9714 complain &= ~tf_user;
9715
9716 /* An alias that just changes the name of a template is equivalent to the
9717 other template, so if any of the arguments are pack expansions, strip
9718 the alias to avoid problems with a pack expansion passed to a non-pack
9719 alias template parameter (DR 1430). */
9720 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9721 templ = get_underlying_template (templ);
9722
9723 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9724 {
9725 tree parm;
9726 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9727 if (arglist2 == error_mark_node
9728 || (!uses_template_parms (arglist2)
9729 && check_instantiated_args (templ, arglist2, complain)))
9730 return error_mark_node;
9731
9732 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9733 return parm;
9734 }
9735 else
9736 {
9737 tree template_type = TREE_TYPE (templ);
9738 tree gen_tmpl;
9739 tree type_decl;
9740 tree found = NULL_TREE;
9741 int arg_depth;
9742 int parm_depth;
9743 int is_dependent_type;
9744 int use_partial_inst_tmpl = false;
9745
9746 if (template_type == error_mark_node)
9747 /* An error occurred while building the template TEMPL, and a
9748 diagnostic has most certainly been emitted for that
9749 already. Let's propagate that error. */
9750 return error_mark_node;
9751
9752 gen_tmpl = most_general_template (templ);
9753 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9754 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9755 arg_depth = TMPL_ARGS_DEPTH (arglist);
9756
9757 if (arg_depth == 1 && parm_depth > 1)
9758 {
9759 /* We've been given an incomplete set of template arguments.
9760 For example, given:
9761
9762 template <class T> struct S1 {
9763 template <class U> struct S2 {};
9764 template <class U> struct S2<U*> {};
9765 };
9766
9767 we will be called with an ARGLIST of `U*', but the
9768 TEMPLATE will be `template <class T> template
9769 <class U> struct S1<T>::S2'. We must fill in the missing
9770 arguments. */
9771 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9772 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9773 arg_depth = TMPL_ARGS_DEPTH (arglist);
9774 }
9775
9776 /* Now we should have enough arguments. */
9777 gcc_assert (parm_depth == arg_depth);
9778
9779 /* From here on, we're only interested in the most general
9780 template. */
9781
9782 /* Calculate the BOUND_ARGS. These will be the args that are
9783 actually tsubst'd into the definition to create the
9784 instantiation. */
9785 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9786 complain,
9787 /*require_all_args=*/true,
9788 /*use_default_args=*/true);
9789
9790 if (arglist == error_mark_node)
9791 /* We were unable to bind the arguments. */
9792 return error_mark_node;
9793
9794 /* In the scope of a template class, explicit references to the
9795 template class refer to the type of the template, not any
9796 instantiation of it. For example, in:
9797
9798 template <class T> class C { void f(C<T>); }
9799
9800 the `C<T>' is just the same as `C'. Outside of the
9801 class, however, such a reference is an instantiation. */
9802 if (entering_scope
9803 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9804 || currently_open_class (template_type))
9805 {
9806 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9807
9808 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9809 return template_type;
9810 }
9811
9812 /* If we already have this specialization, return it. */
9813 elt.tmpl = gen_tmpl;
9814 elt.args = arglist;
9815 elt.spec = NULL_TREE;
9816 hash = spec_hasher::hash (&elt);
9817 entry = type_specializations->find_with_hash (&elt, hash);
9818
9819 if (entry)
9820 return entry->spec;
9821
9822 /* If the template's constraints are not satisfied,
9823 then we cannot form a valid type.
9824
9825 Note that the check is deferred until after the hash
9826 lookup. This prevents redundant checks on previously
9827 instantiated specializations. */
9828 if (flag_concepts
9829 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9830 && !constraints_satisfied_p (gen_tmpl, arglist))
9831 {
9832 if (complain & tf_error)
9833 {
9834 auto_diagnostic_group d;
9835 error ("template constraint failure for %qD", gen_tmpl);
9836 diagnose_constraints (input_location, gen_tmpl, arglist);
9837 }
9838 return error_mark_node;
9839 }
9840
9841 is_dependent_type = uses_template_parms (arglist);
9842
9843 /* If the deduced arguments are invalid, then the binding
9844 failed. */
9845 if (!is_dependent_type
9846 && check_instantiated_args (gen_tmpl,
9847 INNERMOST_TEMPLATE_ARGS (arglist),
9848 complain))
9849 return error_mark_node;
9850
9851 if (!is_dependent_type
9852 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9853 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9854 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9855 {
9856 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9857 DECL_NAME (gen_tmpl),
9858 /*tag_scope=*/ts_global);
9859 return found;
9860 }
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, /*tag_scope=*/ts_current)
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 \f
10244 struct pair_fn_data
10245 {
10246 tree_fn_t fn;
10247 tree_fn_t any_fn;
10248 void *data;
10249 /* True when we should also visit template parameters that occur in
10250 non-deduced contexts. */
10251 bool include_nondeduced_p;
10252 hash_set<tree> *visited;
10253 };
10254
10255 /* Called from for_each_template_parm via walk_tree. */
10256
10257 static tree
10258 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10259 {
10260 tree t = *tp;
10261 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10262 tree_fn_t fn = pfd->fn;
10263 void *data = pfd->data;
10264 tree result = NULL_TREE;
10265
10266 #define WALK_SUBTREE(NODE) \
10267 do \
10268 { \
10269 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10270 pfd->include_nondeduced_p, \
10271 pfd->any_fn); \
10272 if (result) goto out; \
10273 } \
10274 while (0)
10275
10276 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10277 return t;
10278
10279 if (TYPE_P (t)
10280 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10281 WALK_SUBTREE (TYPE_CONTEXT (t));
10282
10283 switch (TREE_CODE (t))
10284 {
10285 case RECORD_TYPE:
10286 if (TYPE_PTRMEMFUNC_P (t))
10287 break;
10288 /* Fall through. */
10289
10290 case UNION_TYPE:
10291 case ENUMERAL_TYPE:
10292 if (!TYPE_TEMPLATE_INFO (t))
10293 *walk_subtrees = 0;
10294 else
10295 WALK_SUBTREE (TYPE_TI_ARGS (t));
10296 break;
10297
10298 case INTEGER_TYPE:
10299 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10300 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10301 break;
10302
10303 case METHOD_TYPE:
10304 /* Since we're not going to walk subtrees, we have to do this
10305 explicitly here. */
10306 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10307 /* Fall through. */
10308
10309 case FUNCTION_TYPE:
10310 /* Check the return type. */
10311 WALK_SUBTREE (TREE_TYPE (t));
10312
10313 /* Check the parameter types. Since default arguments are not
10314 instantiated until they are needed, the TYPE_ARG_TYPES may
10315 contain expressions that involve template parameters. But,
10316 no-one should be looking at them yet. And, once they're
10317 instantiated, they don't contain template parameters, so
10318 there's no point in looking at them then, either. */
10319 {
10320 tree parm;
10321
10322 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10323 WALK_SUBTREE (TREE_VALUE (parm));
10324
10325 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10326 want walk_tree walking into them itself. */
10327 *walk_subtrees = 0;
10328 }
10329
10330 if (flag_noexcept_type)
10331 {
10332 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10333 if (spec)
10334 WALK_SUBTREE (TREE_PURPOSE (spec));
10335 }
10336 break;
10337
10338 case TYPEOF_TYPE:
10339 case DECLTYPE_TYPE:
10340 case UNDERLYING_TYPE:
10341 if (pfd->include_nondeduced_p
10342 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10343 pfd->visited,
10344 pfd->include_nondeduced_p,
10345 pfd->any_fn))
10346 return error_mark_node;
10347 *walk_subtrees = false;
10348 break;
10349
10350 case FUNCTION_DECL:
10351 case VAR_DECL:
10352 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10353 WALK_SUBTREE (DECL_TI_ARGS (t));
10354 /* Fall through. */
10355
10356 case PARM_DECL:
10357 case CONST_DECL:
10358 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10359 WALK_SUBTREE (DECL_INITIAL (t));
10360 if (DECL_CONTEXT (t)
10361 && pfd->include_nondeduced_p)
10362 WALK_SUBTREE (DECL_CONTEXT (t));
10363 break;
10364
10365 case BOUND_TEMPLATE_TEMPLATE_PARM:
10366 /* Record template parameters such as `T' inside `TT<T>'. */
10367 WALK_SUBTREE (TYPE_TI_ARGS (t));
10368 /* Fall through. */
10369
10370 case TEMPLATE_TEMPLATE_PARM:
10371 case TEMPLATE_TYPE_PARM:
10372 case TEMPLATE_PARM_INDEX:
10373 if (fn && (*fn)(t, data))
10374 return t;
10375 else if (!fn)
10376 return t;
10377 break;
10378
10379 case TEMPLATE_DECL:
10380 /* A template template parameter is encountered. */
10381 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10382 WALK_SUBTREE (TREE_TYPE (t));
10383
10384 /* Already substituted template template parameter */
10385 *walk_subtrees = 0;
10386 break;
10387
10388 case TYPENAME_TYPE:
10389 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10390 partial instantiation. */
10391 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10392 *walk_subtrees = 0;
10393 break;
10394
10395 case CONSTRUCTOR:
10396 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10397 && pfd->include_nondeduced_p)
10398 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10399 break;
10400
10401 case INDIRECT_REF:
10402 case COMPONENT_REF:
10403 /* If there's no type, then this thing must be some expression
10404 involving template parameters. */
10405 if (!fn && !TREE_TYPE (t))
10406 return error_mark_node;
10407 break;
10408
10409 case MODOP_EXPR:
10410 case CAST_EXPR:
10411 case IMPLICIT_CONV_EXPR:
10412 case REINTERPRET_CAST_EXPR:
10413 case CONST_CAST_EXPR:
10414 case STATIC_CAST_EXPR:
10415 case DYNAMIC_CAST_EXPR:
10416 case ARROW_EXPR:
10417 case DOTSTAR_EXPR:
10418 case TYPEID_EXPR:
10419 case PSEUDO_DTOR_EXPR:
10420 if (!fn)
10421 return error_mark_node;
10422 break;
10423
10424 case SCOPE_REF:
10425 if (pfd->include_nondeduced_p)
10426 WALK_SUBTREE (TREE_OPERAND (t, 0));
10427 break;
10428
10429 case REQUIRES_EXPR:
10430 {
10431 if (!fn)
10432 return error_mark_node;
10433
10434 /* Recursively walk the type of each constraint variable. */
10435 tree p = TREE_OPERAND (t, 0);
10436 while (p)
10437 {
10438 WALK_SUBTREE (TREE_TYPE (p));
10439 p = TREE_CHAIN (p);
10440 }
10441 }
10442 break;
10443
10444 default:
10445 break;
10446 }
10447
10448 #undef WALK_SUBTREE
10449
10450 /* We didn't find any template parameters we liked. */
10451 out:
10452 return result;
10453 }
10454
10455 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10456 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10457 call FN with the parameter and the DATA.
10458 If FN returns nonzero, the iteration is terminated, and
10459 for_each_template_parm returns 1. Otherwise, the iteration
10460 continues. If FN never returns a nonzero value, the value
10461 returned by for_each_template_parm is 0. If FN is NULL, it is
10462 considered to be the function which always returns 1.
10463
10464 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10465 parameters that occur in non-deduced contexts. When false, only
10466 visits those template parameters that can be deduced. */
10467
10468 static tree
10469 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10470 hash_set<tree> *visited,
10471 bool include_nondeduced_p,
10472 tree_fn_t any_fn)
10473 {
10474 struct pair_fn_data pfd;
10475 tree result;
10476
10477 /* Set up. */
10478 pfd.fn = fn;
10479 pfd.any_fn = any_fn;
10480 pfd.data = data;
10481 pfd.include_nondeduced_p = include_nondeduced_p;
10482
10483 /* Walk the tree. (Conceptually, we would like to walk without
10484 duplicates, but for_each_template_parm_r recursively calls
10485 for_each_template_parm, so we would need to reorganize a fair
10486 bit to use walk_tree_without_duplicates, so we keep our own
10487 visited list.) */
10488 if (visited)
10489 pfd.visited = visited;
10490 else
10491 pfd.visited = new hash_set<tree>;
10492 result = cp_walk_tree (&t,
10493 for_each_template_parm_r,
10494 &pfd,
10495 pfd.visited);
10496
10497 /* Clean up. */
10498 if (!visited)
10499 {
10500 delete pfd.visited;
10501 pfd.visited = 0;
10502 }
10503
10504 return result;
10505 }
10506
10507 struct find_template_parameter_info
10508 {
10509 explicit find_template_parameter_info (tree ctx_parms)
10510 : parm_list (NULL_TREE),
10511 ctx_parms (ctx_parms),
10512 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10513 {}
10514
10515 hash_set<tree> visited;
10516 hash_set<tree> parms;
10517 tree parm_list;
10518 tree ctx_parms;
10519 int max_depth;
10520 };
10521
10522 /* Appends the declaration of T to the list in DATA. */
10523
10524 static int
10525 keep_template_parm (tree t, void* data)
10526 {
10527 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10528
10529 /* Template parameters declared within the expression are not part of
10530 the parameter mapping. For example, in this concept:
10531
10532 template<typename T>
10533 concept C = requires { <expr> } -> same_as<int>;
10534
10535 the return specifier same_as<int> declares a new decltype parameter
10536 that must not be part of the parameter mapping. The same is true
10537 for generic lambda parameters, lambda template parameters, etc. */
10538 int level;
10539 int index;
10540 template_parm_level_and_index (t, &level, &index);
10541 if (level > ftpi->max_depth)
10542 return 0;
10543
10544 /* Arguments like const T yield parameters like const T. This means that
10545 a template-id like X<T, const T> would yield two distinct parameters:
10546 T and const T. Adjust types to their unqualified versions. */
10547 if (TYPE_P (t))
10548 t = TYPE_MAIN_VARIANT (t);
10549 if (!ftpi->parms.add (t))
10550 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10551
10552 return 0;
10553 }
10554
10555 /* Ensure that we recursively examine certain terms that are not normally
10556 visited in for_each_template_parm_r. */
10557
10558 static int
10559 any_template_parm_r (tree t, void *data)
10560 {
10561 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10562
10563 #define WALK_SUBTREE(NODE) \
10564 do \
10565 { \
10566 for_each_template_parm (NODE, keep_template_parm, data, \
10567 &ftpi->visited, true, \
10568 any_template_parm_r); \
10569 } \
10570 while (0)
10571
10572 /* A mention of a member alias/typedef is a use of all of its template
10573 arguments, including those from the enclosing class, so we don't use
10574 alias_template_specialization_p here. */
10575 if (TYPE_P (t) && typedef_variant_p (t))
10576 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10577 WALK_SUBTREE (TI_ARGS (tinfo));
10578
10579 switch (TREE_CODE (t))
10580 {
10581 case TEMPLATE_TYPE_PARM:
10582 /* Type constraints of a placeholder type may contain parameters. */
10583 if (is_auto (t))
10584 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10585 WALK_SUBTREE (constr);
10586 break;
10587
10588 case TEMPLATE_ID_EXPR:
10589 /* Search through references to variable templates. */
10590 WALK_SUBTREE (TREE_OPERAND (t, 0));
10591 WALK_SUBTREE (TREE_OPERAND (t, 1));
10592 break;
10593
10594 case TEMPLATE_PARM_INDEX:
10595 case PARM_DECL:
10596 /* A parameter or constraint variable may also depend on a template
10597 parameter without explicitly naming it. */
10598 WALK_SUBTREE (TREE_TYPE (t));
10599 break;
10600
10601 case TEMPLATE_DECL:
10602 {
10603 /* If T is a member template that shares template parameters with
10604 ctx_parms, we need to mark all those parameters for mapping. */
10605 tree dparms = DECL_TEMPLATE_PARMS (t);
10606 tree cparms = ftpi->ctx_parms;
10607 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10608 dparms = TREE_CHAIN (dparms);
10609 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10610 cparms = TREE_CHAIN (cparms);
10611 while (dparms
10612 && (TREE_TYPE (TREE_VALUE (dparms))
10613 != TREE_TYPE (TREE_VALUE (cparms))))
10614 dparms = TREE_CHAIN (dparms),
10615 cparms = TREE_CHAIN (cparms);
10616 if (dparms)
10617 {
10618 int ddepth = TMPL_PARMS_DEPTH (dparms);
10619 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10620 for (int i = 0; i < ddepth; ++i)
10621 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10622 }
10623 }
10624 break;
10625
10626 case LAMBDA_EXPR:
10627 {
10628 /* Look in the parms and body. */
10629 tree fn = lambda_function (t);
10630 WALK_SUBTREE (TREE_TYPE (fn));
10631 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10632 }
10633 break;
10634
10635 case IDENTIFIER_NODE:
10636 if (IDENTIFIER_CONV_OP_P (t))
10637 /* The conversion-type-id of a conversion operator may be dependent. */
10638 WALK_SUBTREE (TREE_TYPE (t));
10639 break;
10640
10641 default:
10642 break;
10643 }
10644
10645 /* Keep walking. */
10646 return 0;
10647 }
10648
10649 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10650 are the template parameters in scope. */
10651
10652 tree
10653 find_template_parameters (tree t, tree ctx_parms)
10654 {
10655 if (!ctx_parms)
10656 return NULL_TREE;
10657
10658 find_template_parameter_info ftpi (ctx_parms);
10659 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10660 /*include_nondeduced*/true, any_template_parm_r);
10661 return ftpi.parm_list;
10662 }
10663
10664 /* Returns true if T depends on any template parameter. */
10665
10666 int
10667 uses_template_parms (tree t)
10668 {
10669 if (t == NULL_TREE)
10670 return false;
10671
10672 bool dependent_p;
10673 int saved_processing_template_decl;
10674
10675 saved_processing_template_decl = processing_template_decl;
10676 if (!saved_processing_template_decl)
10677 processing_template_decl = 1;
10678 if (TYPE_P (t))
10679 dependent_p = dependent_type_p (t);
10680 else if (TREE_CODE (t) == TREE_VEC)
10681 dependent_p = any_dependent_template_arguments_p (t);
10682 else if (TREE_CODE (t) == TREE_LIST)
10683 dependent_p = (uses_template_parms (TREE_VALUE (t))
10684 || uses_template_parms (TREE_CHAIN (t)));
10685 else if (TREE_CODE (t) == TYPE_DECL)
10686 dependent_p = dependent_type_p (TREE_TYPE (t));
10687 else if (t == error_mark_node)
10688 dependent_p = false;
10689 else
10690 dependent_p = value_dependent_expression_p (t);
10691
10692 processing_template_decl = saved_processing_template_decl;
10693
10694 return dependent_p;
10695 }
10696
10697 /* Returns true iff current_function_decl is an incompletely instantiated
10698 template. Useful instead of processing_template_decl because the latter
10699 is set to 0 during instantiate_non_dependent_expr. */
10700
10701 bool
10702 in_template_function (void)
10703 {
10704 tree fn = current_function_decl;
10705 bool ret;
10706 ++processing_template_decl;
10707 ret = (fn && DECL_LANG_SPECIFIC (fn)
10708 && DECL_TEMPLATE_INFO (fn)
10709 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10710 --processing_template_decl;
10711 return ret;
10712 }
10713
10714 /* Returns true if T depends on any template parameter with level LEVEL. */
10715
10716 bool
10717 uses_template_parms_level (tree t, int level)
10718 {
10719 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10720 /*include_nondeduced_p=*/true);
10721 }
10722
10723 /* Returns true if the signature of DECL depends on any template parameter from
10724 its enclosing class. */
10725
10726 bool
10727 uses_outer_template_parms (tree decl)
10728 {
10729 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10730 if (depth == 0)
10731 return false;
10732 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10733 &depth, NULL, /*include_nondeduced_p=*/true))
10734 return true;
10735 if (PRIMARY_TEMPLATE_P (decl)
10736 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10737 (DECL_TEMPLATE_PARMS (decl)),
10738 template_parm_outer_level,
10739 &depth, NULL, /*include_nondeduced_p=*/true))
10740 return true;
10741 tree ci = get_constraints (decl);
10742 if (ci)
10743 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10744 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10745 &depth, NULL, /*nondeduced*/true))
10746 return true;
10747 return false;
10748 }
10749
10750 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10751 ill-formed translation unit, i.e. a variable or function that isn't
10752 usable in a constant expression. */
10753
10754 static inline bool
10755 neglectable_inst_p (tree d)
10756 {
10757 return (d && DECL_P (d)
10758 && !undeduced_auto_decl (d)
10759 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10760 : decl_maybe_constant_var_p (d)));
10761 }
10762
10763 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10764 neglectable and instantiated from within an erroneous instantiation. */
10765
10766 static bool
10767 limit_bad_template_recursion (tree decl)
10768 {
10769 struct tinst_level *lev = current_tinst_level;
10770 int errs = errorcount + sorrycount;
10771 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10772 return false;
10773
10774 for (; lev; lev = lev->next)
10775 if (neglectable_inst_p (lev->maybe_get_node ()))
10776 break;
10777
10778 return (lev && errs > lev->errors);
10779 }
10780
10781 static int tinst_depth;
10782 extern int max_tinst_depth;
10783 int depth_reached;
10784
10785 static GTY(()) struct tinst_level *last_error_tinst_level;
10786
10787 /* We're starting to instantiate D; record the template instantiation context
10788 at LOC for diagnostics and to restore it later. */
10789
10790 bool
10791 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10792 {
10793 struct tinst_level *new_level;
10794
10795 if (tinst_depth >= max_tinst_depth)
10796 {
10797 /* Tell error.c not to try to instantiate any templates. */
10798 at_eof = 2;
10799 fatal_error (input_location,
10800 "template instantiation depth exceeds maximum of %d"
10801 " (use %<-ftemplate-depth=%> to increase the maximum)",
10802 max_tinst_depth);
10803 return false;
10804 }
10805
10806 /* If the current instantiation caused problems, don't let it instantiate
10807 anything else. Do allow deduction substitution and decls usable in
10808 constant expressions. */
10809 if (!targs && limit_bad_template_recursion (tldcl))
10810 {
10811 /* Avoid no_linkage_errors and unused function warnings for this
10812 decl. */
10813 TREE_NO_WARNING (tldcl) = 1;
10814 return false;
10815 }
10816
10817 /* When not -quiet, dump template instantiations other than functions, since
10818 announce_function will take care of those. */
10819 if (!quiet_flag && !targs
10820 && TREE_CODE (tldcl) != TREE_LIST
10821 && TREE_CODE (tldcl) != FUNCTION_DECL)
10822 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10823
10824 new_level = tinst_level_freelist ().alloc ();
10825 new_level->tldcl = tldcl;
10826 new_level->targs = targs;
10827 new_level->locus = loc;
10828 new_level->errors = errorcount + sorrycount;
10829 new_level->next = NULL;
10830 new_level->refcount = 0;
10831 set_refcount_ptr (new_level->next, current_tinst_level);
10832 set_refcount_ptr (current_tinst_level, new_level);
10833
10834 ++tinst_depth;
10835 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10836 depth_reached = tinst_depth;
10837
10838 return true;
10839 }
10840
10841 /* We're starting substitution of TMPL<ARGS>; record the template
10842 substitution context for diagnostics and to restore it later. */
10843
10844 bool
10845 push_tinst_level (tree tmpl, tree args)
10846 {
10847 return push_tinst_level_loc (tmpl, args, input_location);
10848 }
10849
10850 /* We're starting to instantiate D; record INPUT_LOCATION and the
10851 template instantiation context for diagnostics and to restore it
10852 later. */
10853
10854 bool
10855 push_tinst_level (tree d)
10856 {
10857 return push_tinst_level_loc (d, input_location);
10858 }
10859
10860 /* Likewise, but record LOC as the program location. */
10861
10862 bool
10863 push_tinst_level_loc (tree d, location_t loc)
10864 {
10865 gcc_assert (TREE_CODE (d) != TREE_LIST);
10866 return push_tinst_level_loc (d, NULL, loc);
10867 }
10868
10869 /* We're done instantiating this template; return to the instantiation
10870 context. */
10871
10872 void
10873 pop_tinst_level (void)
10874 {
10875 /* Restore the filename and line number stashed away when we started
10876 this instantiation. */
10877 input_location = current_tinst_level->locus;
10878 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10879 --tinst_depth;
10880 }
10881
10882 /* We're instantiating a deferred template; restore the template
10883 instantiation context in which the instantiation was requested, which
10884 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10885
10886 static tree
10887 reopen_tinst_level (struct tinst_level *level)
10888 {
10889 struct tinst_level *t;
10890
10891 tinst_depth = 0;
10892 for (t = level; t; t = t->next)
10893 ++tinst_depth;
10894
10895 set_refcount_ptr (current_tinst_level, level);
10896 pop_tinst_level ();
10897 if (current_tinst_level)
10898 current_tinst_level->errors = errorcount+sorrycount;
10899 return level->maybe_get_node ();
10900 }
10901
10902 /* Returns the TINST_LEVEL which gives the original instantiation
10903 context. */
10904
10905 struct tinst_level *
10906 outermost_tinst_level (void)
10907 {
10908 struct tinst_level *level = current_tinst_level;
10909 if (level)
10910 while (level->next)
10911 level = level->next;
10912 return level;
10913 }
10914
10915 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10916 vector of template arguments, as for tsubst.
10917
10918 Returns an appropriate tsubst'd friend declaration. */
10919
10920 static tree
10921 tsubst_friend_function (tree decl, tree args)
10922 {
10923 tree new_friend;
10924
10925 if (TREE_CODE (decl) == FUNCTION_DECL
10926 && DECL_TEMPLATE_INSTANTIATION (decl)
10927 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10928 /* This was a friend declared with an explicit template
10929 argument list, e.g.:
10930
10931 friend void f<>(T);
10932
10933 to indicate that f was a template instantiation, not a new
10934 function declaration. Now, we have to figure out what
10935 instantiation of what template. */
10936 {
10937 tree template_id, arglist, fns;
10938 tree new_args;
10939 tree tmpl;
10940 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10941
10942 /* Friend functions are looked up in the containing namespace scope.
10943 We must enter that scope, to avoid finding member functions of the
10944 current class with same name. */
10945 push_nested_namespace (ns);
10946 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10947 tf_warning_or_error, NULL_TREE,
10948 /*integral_constant_expression_p=*/false);
10949 pop_nested_namespace (ns);
10950 arglist = tsubst (DECL_TI_ARGS (decl), args,
10951 tf_warning_or_error, NULL_TREE);
10952 template_id = lookup_template_function (fns, arglist);
10953
10954 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10955 tmpl = determine_specialization (template_id, new_friend,
10956 &new_args,
10957 /*need_member_template=*/0,
10958 TREE_VEC_LENGTH (args),
10959 tsk_none);
10960 return instantiate_template (tmpl, new_args, tf_error);
10961 }
10962
10963 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10964 if (new_friend == error_mark_node)
10965 return error_mark_node;
10966
10967 /* The NEW_FRIEND will look like an instantiation, to the
10968 compiler, but is not an instantiation from the point of view of
10969 the language. For example, we might have had:
10970
10971 template <class T> struct S {
10972 template <class U> friend void f(T, U);
10973 };
10974
10975 Then, in S<int>, template <class U> void f(int, U) is not an
10976 instantiation of anything. */
10977
10978 DECL_USE_TEMPLATE (new_friend) = 0;
10979 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10980 {
10981 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10982 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10983 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10984
10985 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10986 match in decls_match. */
10987 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10988 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10989 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10990 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10991 }
10992
10993 /* The mangled name for the NEW_FRIEND is incorrect. The function
10994 is not a template instantiation and should not be mangled like
10995 one. Therefore, we forget the mangling here; we'll recompute it
10996 later if we need it. */
10997 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10998 {
10999 SET_DECL_RTL (new_friend, NULL);
11000 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11001 }
11002
11003 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11004 {
11005 tree old_decl;
11006 tree ns;
11007
11008 /* We must save some information from NEW_FRIEND before calling
11009 duplicate decls since that function will free NEW_FRIEND if
11010 possible. */
11011 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11012 tree new_friend_result_template_info = NULL_TREE;
11013 bool new_friend_is_defn =
11014 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11015 (template_for_substitution (new_friend)))
11016 != NULL_TREE);
11017 tree not_tmpl = new_friend;
11018
11019 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11020 {
11021 /* This declaration is a `primary' template. */
11022 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11023
11024 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11025 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11026 }
11027
11028 /* Inside pushdecl_namespace_level, we will push into the
11029 current namespace. However, the friend function should go
11030 into the namespace of the template. */
11031 ns = decl_namespace_context (new_friend);
11032 push_nested_namespace (ns);
11033 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
11034 pop_nested_namespace (ns);
11035
11036 if (old_decl == error_mark_node)
11037 return error_mark_node;
11038
11039 if (old_decl != new_friend)
11040 {
11041 /* This new friend declaration matched an existing
11042 declaration. For example, given:
11043
11044 template <class T> void f(T);
11045 template <class U> class C {
11046 template <class T> friend void f(T) {}
11047 };
11048
11049 the friend declaration actually provides the definition
11050 of `f', once C has been instantiated for some type. So,
11051 old_decl will be the out-of-class template declaration,
11052 while new_friend is the in-class definition.
11053
11054 But, if `f' was called before this point, the
11055 instantiation of `f' will have DECL_TI_ARGS corresponding
11056 to `T' but not to `U', references to which might appear
11057 in the definition of `f'. Previously, the most general
11058 template for an instantiation of `f' was the out-of-class
11059 version; now it is the in-class version. Therefore, we
11060 run through all specialization of `f', adding to their
11061 DECL_TI_ARGS appropriately. In particular, they need a
11062 new set of outer arguments, corresponding to the
11063 arguments for this class instantiation.
11064
11065 The same situation can arise with something like this:
11066
11067 friend void f(int);
11068 template <class T> class C {
11069 friend void f(T) {}
11070 };
11071
11072 when `C<int>' is instantiated. Now, `f(int)' is defined
11073 in the class. */
11074
11075 if (!new_friend_is_defn)
11076 /* On the other hand, if the in-class declaration does
11077 *not* provide a definition, then we don't want to alter
11078 existing definitions. We can just leave everything
11079 alone. */
11080 ;
11081 else
11082 {
11083 tree new_template = TI_TEMPLATE (new_friend_template_info);
11084 tree new_args = TI_ARGS (new_friend_template_info);
11085
11086 /* Overwrite whatever template info was there before, if
11087 any, with the new template information pertaining to
11088 the declaration. */
11089 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11090
11091 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11092 {
11093 /* We should have called reregister_specialization in
11094 duplicate_decls. */
11095 gcc_assert (retrieve_specialization (new_template,
11096 new_args, 0)
11097 == old_decl);
11098
11099 /* Instantiate it if the global has already been used. */
11100 if (DECL_ODR_USED (old_decl))
11101 instantiate_decl (old_decl, /*defer_ok=*/true,
11102 /*expl_inst_class_mem_p=*/false);
11103 }
11104 else
11105 {
11106 tree t;
11107
11108 /* Indicate that the old function template is a partial
11109 instantiation. */
11110 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11111 = new_friend_result_template_info;
11112
11113 gcc_assert (new_template
11114 == most_general_template (new_template));
11115 gcc_assert (new_template != old_decl);
11116
11117 /* Reassign any specializations already in the hash table
11118 to the new more general template, and add the
11119 additional template args. */
11120 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11121 t != NULL_TREE;
11122 t = TREE_CHAIN (t))
11123 {
11124 tree spec = TREE_VALUE (t);
11125 spec_entry elt;
11126
11127 elt.tmpl = old_decl;
11128 elt.args = DECL_TI_ARGS (spec);
11129 elt.spec = NULL_TREE;
11130
11131 decl_specializations->remove_elt (&elt);
11132
11133 DECL_TI_ARGS (spec)
11134 = add_outermost_template_args (new_args,
11135 DECL_TI_ARGS (spec));
11136
11137 register_specialization
11138 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11139
11140 }
11141 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11142 }
11143 }
11144
11145 /* The information from NEW_FRIEND has been merged into OLD_DECL
11146 by duplicate_decls. */
11147 new_friend = old_decl;
11148 }
11149 }
11150 else
11151 {
11152 tree context = DECL_CONTEXT (new_friend);
11153 bool dependent_p;
11154
11155 /* In the code
11156 template <class T> class C {
11157 template <class U> friend void C1<U>::f (); // case 1
11158 friend void C2<T>::f (); // case 2
11159 };
11160 we only need to make sure CONTEXT is a complete type for
11161 case 2. To distinguish between the two cases, we note that
11162 CONTEXT of case 1 remains dependent type after tsubst while
11163 this isn't true for case 2. */
11164 ++processing_template_decl;
11165 dependent_p = dependent_type_p (context);
11166 --processing_template_decl;
11167
11168 if (!dependent_p
11169 && !complete_type_or_else (context, NULL_TREE))
11170 return error_mark_node;
11171
11172 if (COMPLETE_TYPE_P (context))
11173 {
11174 tree fn = new_friend;
11175 /* do_friend adds the TEMPLATE_DECL for any member friend
11176 template even if it isn't a member template, i.e.
11177 template <class T> friend A<T>::f();
11178 Look through it in that case. */
11179 if (TREE_CODE (fn) == TEMPLATE_DECL
11180 && !PRIMARY_TEMPLATE_P (fn))
11181 fn = DECL_TEMPLATE_RESULT (fn);
11182 /* Check to see that the declaration is really present, and,
11183 possibly obtain an improved declaration. */
11184 fn = check_classfn (context, fn, NULL_TREE);
11185
11186 if (fn)
11187 new_friend = fn;
11188 }
11189 }
11190
11191 return new_friend;
11192 }
11193
11194 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11195 template arguments, as for tsubst.
11196
11197 Returns an appropriate tsubst'd friend type or error_mark_node on
11198 failure. */
11199
11200 static tree
11201 tsubst_friend_class (tree friend_tmpl, tree args)
11202 {
11203 tree tmpl;
11204
11205 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11206 {
11207 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11208 return TREE_TYPE (tmpl);
11209 }
11210
11211 tree context = CP_DECL_CONTEXT (friend_tmpl);
11212 if (TREE_CODE (context) == NAMESPACE_DECL)
11213 push_nested_namespace (context);
11214 else
11215 {
11216 context = tsubst (context, args, tf_error, NULL_TREE);
11217 push_nested_class (context);
11218 }
11219
11220 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11221 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11222
11223 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11224 {
11225 /* The friend template has already been declared. Just
11226 check to see that the declarations match, and install any new
11227 default parameters. We must tsubst the default parameters,
11228 of course. We only need the innermost template parameters
11229 because that is all that redeclare_class_template will look
11230 at. */
11231 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11232 > TMPL_ARGS_DEPTH (args))
11233 {
11234 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11235 args, tf_warning_or_error);
11236 location_t saved_input_location = input_location;
11237 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11238 tree cons = get_constraints (tmpl);
11239 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11240 input_location = saved_input_location;
11241 }
11242 }
11243 else
11244 {
11245 /* The friend template has not already been declared. In this
11246 case, the instantiation of the template class will cause the
11247 injection of this template into the namespace scope. */
11248 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11249
11250 if (tmpl != error_mark_node)
11251 {
11252 /* The new TMPL is not an instantiation of anything, so we
11253 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11254 for the new type because that is supposed to be the
11255 corresponding template decl, i.e., TMPL. */
11256 DECL_USE_TEMPLATE (tmpl) = 0;
11257 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11258 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11259 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11260 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11261
11262 /* It is hidden. */
11263 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11264 DECL_ANTICIPATED (tmpl)
11265 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11266
11267 /* Substitute into and set the constraints on the new declaration. */
11268 if (tree ci = get_constraints (friend_tmpl))
11269 {
11270 ++processing_template_decl;
11271 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11272 DECL_FRIEND_CONTEXT (friend_tmpl));
11273 --processing_template_decl;
11274 set_constraints (tmpl, ci);
11275 }
11276
11277 /* Inject this template into the enclosing namspace scope. */
11278 tmpl = pushdecl_namespace_level (tmpl, true);
11279 }
11280 }
11281
11282 if (TREE_CODE (context) == NAMESPACE_DECL)
11283 pop_nested_namespace (context);
11284 else
11285 pop_nested_class ();
11286
11287 return TREE_TYPE (tmpl);
11288 }
11289
11290 /* Returns zero if TYPE cannot be completed later due to circularity.
11291 Otherwise returns one. */
11292
11293 static int
11294 can_complete_type_without_circularity (tree type)
11295 {
11296 if (type == NULL_TREE || type == error_mark_node)
11297 return 0;
11298 else if (COMPLETE_TYPE_P (type))
11299 return 1;
11300 else if (TREE_CODE (type) == ARRAY_TYPE)
11301 return can_complete_type_without_circularity (TREE_TYPE (type));
11302 else if (CLASS_TYPE_P (type)
11303 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11304 return 0;
11305 else
11306 return 1;
11307 }
11308
11309 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11310 tsubst_flags_t, tree);
11311
11312 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11313 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11314
11315 static tree
11316 tsubst_attribute (tree t, tree *decl_p, tree args,
11317 tsubst_flags_t complain, tree in_decl)
11318 {
11319 gcc_assert (ATTR_IS_DEPENDENT (t));
11320
11321 tree val = TREE_VALUE (t);
11322 if (val == NULL_TREE)
11323 /* Nothing to do. */;
11324 else if ((flag_openmp || flag_openmp_simd)
11325 && is_attribute_p ("omp declare simd",
11326 get_attribute_name (t)))
11327 {
11328 tree clauses = TREE_VALUE (val);
11329 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11330 complain, in_decl);
11331 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11332 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11333 tree parms = DECL_ARGUMENTS (*decl_p);
11334 clauses
11335 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11336 if (clauses)
11337 val = build_tree_list (NULL_TREE, clauses);
11338 else
11339 val = NULL_TREE;
11340 }
11341 else if (flag_openmp
11342 && is_attribute_p ("omp declare variant base",
11343 get_attribute_name (t)))
11344 {
11345 ++cp_unevaluated_operand;
11346 tree varid
11347 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11348 in_decl, /*integral_constant_expression_p=*/false);
11349 --cp_unevaluated_operand;
11350 tree chain = TREE_CHAIN (val);
11351 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11352 tree ctx = copy_list (TREE_VALUE (val));
11353 tree simd = get_identifier ("simd");
11354 tree score = get_identifier (" score");
11355 tree condition = get_identifier ("condition");
11356 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11357 {
11358 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11359 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11360 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11361 {
11362 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11363 {
11364 tree clauses = TREE_VALUE (t2);
11365 clauses = tsubst_omp_clauses (clauses,
11366 C_ORT_OMP_DECLARE_SIMD, args,
11367 complain, in_decl);
11368 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11369 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11370 TREE_VALUE (t2) = clauses;
11371 }
11372 else
11373 {
11374 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11375 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11376 if (TREE_VALUE (t3))
11377 {
11378 bool allow_string
11379 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11380 && TREE_PURPOSE (t3) != score);
11381 tree v = TREE_VALUE (t3);
11382 if (TREE_CODE (v) == STRING_CST && allow_string)
11383 continue;
11384 v = tsubst_expr (v, args, complain, in_decl, true);
11385 v = fold_non_dependent_expr (v);
11386 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11387 || (TREE_PURPOSE (t3) == score
11388 ? TREE_CODE (v) != INTEGER_CST
11389 : !tree_fits_shwi_p (v)))
11390 {
11391 location_t loc
11392 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11393 match_loc);
11394 if (TREE_PURPOSE (t3) == score)
11395 error_at (loc, "score argument must be "
11396 "constant integer expression");
11397 else if (allow_string)
11398 error_at (loc, "property must be constant "
11399 "integer expression or string "
11400 "literal");
11401 else
11402 error_at (loc, "property must be constant "
11403 "integer expression");
11404 return NULL_TREE;
11405 }
11406 else if (TREE_PURPOSE (t3) == score
11407 && tree_int_cst_sgn (v) < 0)
11408 {
11409 location_t loc
11410 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11411 match_loc);
11412 error_at (loc, "score argument must be "
11413 "non-negative");
11414 return NULL_TREE;
11415 }
11416 TREE_VALUE (t3) = v;
11417 }
11418 }
11419 }
11420 }
11421 val = tree_cons (varid, ctx, chain);
11422 }
11423 /* If the first attribute argument is an identifier, don't
11424 pass it through tsubst. Attributes like mode, format,
11425 cleanup and several target specific attributes expect it
11426 unmodified. */
11427 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11428 {
11429 tree chain
11430 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11431 /*integral_constant_expression_p=*/false);
11432 if (chain != TREE_CHAIN (val))
11433 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11434 }
11435 else if (PACK_EXPANSION_P (val))
11436 {
11437 /* An attribute pack expansion. */
11438 tree purp = TREE_PURPOSE (t);
11439 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11440 if (pack == error_mark_node)
11441 return error_mark_node;
11442 int len = TREE_VEC_LENGTH (pack);
11443 tree list = NULL_TREE;
11444 tree *q = &list;
11445 for (int i = 0; i < len; ++i)
11446 {
11447 tree elt = TREE_VEC_ELT (pack, i);
11448 *q = build_tree_list (purp, elt);
11449 q = &TREE_CHAIN (*q);
11450 }
11451 return list;
11452 }
11453 else
11454 val = tsubst_expr (val, args, complain, in_decl,
11455 /*integral_constant_expression_p=*/false);
11456
11457 if (val != TREE_VALUE (t))
11458 return build_tree_list (TREE_PURPOSE (t), val);
11459 return t;
11460 }
11461
11462 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11463 unchanged or a new TREE_LIST chain. */
11464
11465 static tree
11466 tsubst_attributes (tree attributes, tree args,
11467 tsubst_flags_t complain, tree in_decl)
11468 {
11469 tree last_dep = NULL_TREE;
11470
11471 for (tree t = attributes; t; t = TREE_CHAIN (t))
11472 if (ATTR_IS_DEPENDENT (t))
11473 {
11474 last_dep = t;
11475 attributes = copy_list (attributes);
11476 break;
11477 }
11478
11479 if (last_dep)
11480 for (tree *p = &attributes; *p; )
11481 {
11482 tree t = *p;
11483 if (ATTR_IS_DEPENDENT (t))
11484 {
11485 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11486 if (subst != t)
11487 {
11488 *p = subst;
11489 while (*p)
11490 p = &TREE_CHAIN (*p);
11491 *p = TREE_CHAIN (t);
11492 continue;
11493 }
11494 }
11495 p = &TREE_CHAIN (*p);
11496 }
11497
11498 return attributes;
11499 }
11500
11501 /* Apply any attributes which had to be deferred until instantiation
11502 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11503 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11504
11505 static void
11506 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11507 tree args, tsubst_flags_t complain, tree in_decl)
11508 {
11509 tree last_dep = NULL_TREE;
11510 tree t;
11511 tree *p;
11512
11513 if (attributes == NULL_TREE)
11514 return;
11515
11516 if (DECL_P (*decl_p))
11517 {
11518 if (TREE_TYPE (*decl_p) == error_mark_node)
11519 return;
11520 p = &DECL_ATTRIBUTES (*decl_p);
11521 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11522 to our attributes parameter. */
11523 gcc_assert (*p == attributes);
11524 }
11525 else
11526 {
11527 p = &TYPE_ATTRIBUTES (*decl_p);
11528 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11529 lookup_template_class_1, and should be preserved. */
11530 gcc_assert (*p != attributes);
11531 while (*p)
11532 p = &TREE_CHAIN (*p);
11533 }
11534
11535 for (t = attributes; t; t = TREE_CHAIN (t))
11536 if (ATTR_IS_DEPENDENT (t))
11537 {
11538 last_dep = t;
11539 attributes = copy_list (attributes);
11540 break;
11541 }
11542
11543 *p = attributes;
11544 if (last_dep)
11545 {
11546 tree late_attrs = NULL_TREE;
11547 tree *q = &late_attrs;
11548
11549 for (; *p; )
11550 {
11551 t = *p;
11552 if (ATTR_IS_DEPENDENT (t))
11553 {
11554 *p = TREE_CHAIN (t);
11555 TREE_CHAIN (t) = NULL_TREE;
11556 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11557 while (*q)
11558 q = &TREE_CHAIN (*q);
11559 }
11560 else
11561 p = &TREE_CHAIN (t);
11562 }
11563
11564 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11565 }
11566 }
11567
11568 /* The template TMPL is being instantiated with the template arguments TARGS.
11569 Perform the access checks that we deferred when parsing the template. */
11570
11571 static void
11572 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11573 {
11574 unsigned i;
11575 deferred_access_check *chk;
11576
11577 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11578 return;
11579
11580 if (vec<deferred_access_check, va_gc> *access_checks
11581 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11582 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11583 {
11584 tree decl = chk->decl;
11585 tree diag_decl = chk->diag_decl;
11586 tree type_scope = TREE_TYPE (chk->binfo);
11587
11588 if (uses_template_parms (type_scope))
11589 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11590
11591 /* Make access check error messages point to the location
11592 of the use of the typedef. */
11593 iloc_sentinel ils (chk->loc);
11594 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11595 decl, diag_decl, tf_warning_or_error);
11596 }
11597 }
11598
11599 static tree
11600 instantiate_class_template_1 (tree type)
11601 {
11602 tree templ, args, pattern, t, member;
11603 tree typedecl;
11604 tree pbinfo;
11605 tree base_list;
11606 unsigned int saved_maximum_field_alignment;
11607 tree fn_context;
11608
11609 if (type == error_mark_node)
11610 return error_mark_node;
11611
11612 if (COMPLETE_OR_OPEN_TYPE_P (type)
11613 || uses_template_parms (type))
11614 return type;
11615
11616 /* Figure out which template is being instantiated. */
11617 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11618 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11619
11620 /* Mark the type as in the process of being defined. */
11621 TYPE_BEING_DEFINED (type) = 1;
11622
11623 /* We may be in the middle of deferred access check. Disable
11624 it now. */
11625 deferring_access_check_sentinel acs (dk_no_deferred);
11626
11627 /* Determine what specialization of the original template to
11628 instantiate. */
11629 t = most_specialized_partial_spec (type, tf_warning_or_error);
11630 if (t == error_mark_node)
11631 return error_mark_node;
11632 else if (t)
11633 {
11634 /* This TYPE is actually an instantiation of a partial
11635 specialization. We replace the innermost set of ARGS with
11636 the arguments appropriate for substitution. For example,
11637 given:
11638
11639 template <class T> struct S {};
11640 template <class T> struct S<T*> {};
11641
11642 and supposing that we are instantiating S<int*>, ARGS will
11643 presently be {int*} -- but we need {int}. */
11644 pattern = TREE_TYPE (t);
11645 args = TREE_PURPOSE (t);
11646 }
11647 else
11648 {
11649 pattern = TREE_TYPE (templ);
11650 args = CLASSTYPE_TI_ARGS (type);
11651 }
11652
11653 /* If the template we're instantiating is incomplete, then clearly
11654 there's nothing we can do. */
11655 if (!COMPLETE_TYPE_P (pattern))
11656 {
11657 /* We can try again later. */
11658 TYPE_BEING_DEFINED (type) = 0;
11659 return type;
11660 }
11661
11662 /* If we've recursively instantiated too many templates, stop. */
11663 if (! push_tinst_level (type))
11664 return type;
11665
11666 int saved_unevaluated_operand = cp_unevaluated_operand;
11667 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11668
11669 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11670 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11671 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11672 fn_context = error_mark_node;
11673 if (!fn_context)
11674 push_to_top_level ();
11675 else
11676 {
11677 cp_unevaluated_operand = 0;
11678 c_inhibit_evaluation_warnings = 0;
11679 }
11680 /* Use #pragma pack from the template context. */
11681 saved_maximum_field_alignment = maximum_field_alignment;
11682 maximum_field_alignment = TYPE_PRECISION (pattern);
11683
11684 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11685
11686 /* Set the input location to the most specialized template definition.
11687 This is needed if tsubsting causes an error. */
11688 typedecl = TYPE_MAIN_DECL (pattern);
11689 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11690 DECL_SOURCE_LOCATION (typedecl);
11691
11692 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11693 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11694 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11695 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11696 if (ANON_AGGR_TYPE_P (pattern))
11697 SET_ANON_AGGR_TYPE_P (type);
11698 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11699 {
11700 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11701 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11702 /* Adjust visibility for template arguments. */
11703 determine_visibility (TYPE_MAIN_DECL (type));
11704 }
11705 if (CLASS_TYPE_P (type))
11706 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11707
11708 pbinfo = TYPE_BINFO (pattern);
11709
11710 /* We should never instantiate a nested class before its enclosing
11711 class; we need to look up the nested class by name before we can
11712 instantiate it, and that lookup should instantiate the enclosing
11713 class. */
11714 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11715 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11716
11717 base_list = NULL_TREE;
11718 if (BINFO_N_BASE_BINFOS (pbinfo))
11719 {
11720 tree pbase_binfo;
11721 tree pushed_scope;
11722 int i;
11723
11724 /* We must enter the scope containing the type, as that is where
11725 the accessibility of types named in dependent bases are
11726 looked up from. */
11727 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11728
11729 /* Substitute into each of the bases to determine the actual
11730 basetypes. */
11731 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11732 {
11733 tree base;
11734 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11735 tree expanded_bases = NULL_TREE;
11736 int idx, len = 1;
11737
11738 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11739 {
11740 expanded_bases =
11741 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11742 args, tf_error, NULL_TREE);
11743 if (expanded_bases == error_mark_node)
11744 continue;
11745
11746 len = TREE_VEC_LENGTH (expanded_bases);
11747 }
11748
11749 for (idx = 0; idx < len; idx++)
11750 {
11751 if (expanded_bases)
11752 /* Extract the already-expanded base class. */
11753 base = TREE_VEC_ELT (expanded_bases, idx);
11754 else
11755 /* Substitute to figure out the base class. */
11756 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11757 NULL_TREE);
11758
11759 if (base == error_mark_node)
11760 continue;
11761
11762 base_list = tree_cons (access, base, base_list);
11763 if (BINFO_VIRTUAL_P (pbase_binfo))
11764 TREE_TYPE (base_list) = integer_type_node;
11765 }
11766 }
11767
11768 /* The list is now in reverse order; correct that. */
11769 base_list = nreverse (base_list);
11770
11771 if (pushed_scope)
11772 pop_scope (pushed_scope);
11773 }
11774 /* Now call xref_basetypes to set up all the base-class
11775 information. */
11776 xref_basetypes (type, base_list);
11777
11778 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11779 (int) ATTR_FLAG_TYPE_IN_PLACE,
11780 args, tf_error, NULL_TREE);
11781 fixup_attribute_variants (type);
11782
11783 /* Now that our base classes are set up, enter the scope of the
11784 class, so that name lookups into base classes, etc. will work
11785 correctly. This is precisely analogous to what we do in
11786 begin_class_definition when defining an ordinary non-template
11787 class, except we also need to push the enclosing classes. */
11788 push_nested_class (type);
11789
11790 /* Now members are processed in the order of declaration. */
11791 for (member = CLASSTYPE_DECL_LIST (pattern);
11792 member; member = TREE_CHAIN (member))
11793 {
11794 tree t = TREE_VALUE (member);
11795
11796 if (TREE_PURPOSE (member))
11797 {
11798 if (TYPE_P (t))
11799 {
11800 if (LAMBDA_TYPE_P (t))
11801 /* A closure type for a lambda in an NSDMI or default argument.
11802 Ignore it; it will be regenerated when needed. */
11803 continue;
11804
11805 /* Build new CLASSTYPE_NESTED_UTDS. */
11806 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11807 && TYPE_LANG_SPECIFIC (t)
11808 && CLASSTYPE_IS_TEMPLATE (t));
11809
11810 /* If the member is a class template, then -- even after
11811 substitution -- there may be dependent types in the
11812 template argument list for the class. We increment
11813 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11814 that function will assume that no types are dependent
11815 when outside of a template. */
11816 if (class_template_p)
11817 ++processing_template_decl;
11818 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11819 if (class_template_p)
11820 --processing_template_decl;
11821 if (newtag == error_mark_node)
11822 continue;
11823
11824 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11825 {
11826 tree name = TYPE_IDENTIFIER (t);
11827
11828 if (class_template_p)
11829 /* Unfortunately, lookup_template_class sets
11830 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11831 instantiation (i.e., for the type of a member
11832 template class nested within a template class.)
11833 This behavior is required for
11834 maybe_process_partial_specialization to work
11835 correctly, but is not accurate in this case;
11836 the TAG is not an instantiation of anything.
11837 (The corresponding TEMPLATE_DECL is an
11838 instantiation, but the TYPE is not.) */
11839 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11840
11841 /* Now, we call pushtag to put this NEWTAG into the scope of
11842 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11843 pushtag calling push_template_decl. We don't have to do
11844 this for enums because it will already have been done in
11845 tsubst_enum. */
11846 if (name)
11847 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11848 pushtag (name, newtag, /*tag_scope=*/ts_current);
11849 }
11850 }
11851 else if (DECL_DECLARES_FUNCTION_P (t))
11852 {
11853 tree r;
11854
11855 if (TREE_CODE (t) == TEMPLATE_DECL)
11856 ++processing_template_decl;
11857 r = tsubst (t, args, tf_error, NULL_TREE);
11858 if (TREE_CODE (t) == TEMPLATE_DECL)
11859 --processing_template_decl;
11860 set_current_access_from_decl (r);
11861 finish_member_declaration (r);
11862 /* Instantiate members marked with attribute used. */
11863 if (r != error_mark_node && DECL_PRESERVE_P (r))
11864 mark_used (r);
11865 if (TREE_CODE (r) == FUNCTION_DECL
11866 && DECL_OMP_DECLARE_REDUCTION_P (r))
11867 cp_check_omp_declare_reduction (r);
11868 }
11869 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11870 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11871 /* A closure type for a lambda in an NSDMI or default argument.
11872 Ignore it; it will be regenerated when needed. */;
11873 else
11874 {
11875 /* Build new TYPE_FIELDS. */
11876 if (TREE_CODE (t) == STATIC_ASSERT)
11877 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11878 /*integral_constant_expression_p=*/true);
11879 else if (TREE_CODE (t) != CONST_DECL)
11880 {
11881 tree r;
11882 tree vec = NULL_TREE;
11883 int len = 1;
11884
11885 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11886 /* The file and line for this declaration, to
11887 assist in error message reporting. Since we
11888 called push_tinst_level above, we don't need to
11889 restore these. */
11890 input_location = DECL_SOURCE_LOCATION (t);
11891
11892 if (TREE_CODE (t) == TEMPLATE_DECL)
11893 ++processing_template_decl;
11894 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11895 if (TREE_CODE (t) == TEMPLATE_DECL)
11896 --processing_template_decl;
11897
11898 if (TREE_CODE (r) == TREE_VEC)
11899 {
11900 /* A capture pack became multiple fields. */
11901 vec = r;
11902 len = TREE_VEC_LENGTH (vec);
11903 }
11904
11905 for (int i = 0; i < len; ++i)
11906 {
11907 if (vec)
11908 r = TREE_VEC_ELT (vec, i);
11909 if (VAR_P (r))
11910 {
11911 /* In [temp.inst]:
11912
11913 [t]he initialization (and any associated
11914 side-effects) of a static data member does
11915 not occur unless the static data member is
11916 itself used in a way that requires the
11917 definition of the static data member to
11918 exist.
11919
11920 Therefore, we do not substitute into the
11921 initialized for the static data member here. */
11922 finish_static_data_member_decl
11923 (r,
11924 /*init=*/NULL_TREE,
11925 /*init_const_expr_p=*/false,
11926 /*asmspec_tree=*/NULL_TREE,
11927 /*flags=*/0);
11928 /* Instantiate members marked with attribute used. */
11929 if (r != error_mark_node && DECL_PRESERVE_P (r))
11930 mark_used (r);
11931 }
11932 else if (TREE_CODE (r) == FIELD_DECL)
11933 {
11934 /* Determine whether R has a valid type and can be
11935 completed later. If R is invalid, then its type
11936 is replaced by error_mark_node. */
11937 tree rtype = TREE_TYPE (r);
11938 if (can_complete_type_without_circularity (rtype))
11939 complete_type (rtype);
11940
11941 if (!complete_or_array_type_p (rtype))
11942 {
11943 /* If R's type couldn't be completed and
11944 it isn't a flexible array member (whose
11945 type is incomplete by definition) give
11946 an error. */
11947 cxx_incomplete_type_error (r, rtype);
11948 TREE_TYPE (r) = error_mark_node;
11949 }
11950 else if (TREE_CODE (rtype) == ARRAY_TYPE
11951 && TYPE_DOMAIN (rtype) == NULL_TREE
11952 && (TREE_CODE (type) == UNION_TYPE
11953 || TREE_CODE (type) == QUAL_UNION_TYPE))
11954 {
11955 error ("flexible array member %qD in union", r);
11956 TREE_TYPE (r) = error_mark_node;
11957 }
11958 else if (!verify_type_context (input_location,
11959 TCTX_FIELD, rtype))
11960 TREE_TYPE (r) = error_mark_node;
11961 }
11962
11963 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11964 such a thing will already have been added to the field
11965 list by tsubst_enum in finish_member_declaration in the
11966 CLASSTYPE_NESTED_UTDS case above. */
11967 if (!(TREE_CODE (r) == TYPE_DECL
11968 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11969 && DECL_ARTIFICIAL (r)))
11970 {
11971 set_current_access_from_decl (r);
11972 finish_member_declaration (r);
11973 }
11974 }
11975 }
11976 }
11977 }
11978 else
11979 {
11980 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11981 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11982 {
11983 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11984
11985 tree friend_type = t;
11986 bool adjust_processing_template_decl = false;
11987
11988 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11989 {
11990 /* template <class T> friend class C; */
11991 friend_type = tsubst_friend_class (friend_type, args);
11992 adjust_processing_template_decl = true;
11993 }
11994 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11995 {
11996 /* template <class T> friend class C::D; */
11997 friend_type = tsubst (friend_type, args,
11998 tf_warning_or_error, NULL_TREE);
11999 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12000 friend_type = TREE_TYPE (friend_type);
12001 adjust_processing_template_decl = true;
12002 }
12003 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12004 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12005 {
12006 /* This could be either
12007
12008 friend class T::C;
12009
12010 when dependent_type_p is false or
12011
12012 template <class U> friend class T::C;
12013
12014 otherwise. */
12015 /* Bump processing_template_decl in case this is something like
12016 template <class T> friend struct A<T>::B. */
12017 ++processing_template_decl;
12018 friend_type = tsubst (friend_type, args,
12019 tf_warning_or_error, NULL_TREE);
12020 if (dependent_type_p (friend_type))
12021 adjust_processing_template_decl = true;
12022 --processing_template_decl;
12023 }
12024 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
12025 && !CLASSTYPE_USE_TEMPLATE (friend_type)
12026 && TYPE_HIDDEN_P (friend_type))
12027 {
12028 /* friend class C;
12029
12030 where C hasn't been declared yet. Let's lookup name
12031 from namespace scope directly, bypassing any name that
12032 come from dependent base class. */
12033 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
12034
12035 /* The call to xref_tag_from_type does injection for friend
12036 classes. */
12037 push_nested_namespace (ns);
12038 friend_type =
12039 xref_tag_from_type (friend_type, NULL_TREE,
12040 /*tag_scope=*/ts_current);
12041 pop_nested_namespace (ns);
12042 }
12043 else if (uses_template_parms (friend_type))
12044 /* friend class C<T>; */
12045 friend_type = tsubst (friend_type, args,
12046 tf_warning_or_error, NULL_TREE);
12047 /* Otherwise it's
12048
12049 friend class C;
12050
12051 where C is already declared or
12052
12053 friend class C<int>;
12054
12055 We don't have to do anything in these cases. */
12056
12057 if (adjust_processing_template_decl)
12058 /* Trick make_friend_class into realizing that the friend
12059 we're adding is a template, not an ordinary class. It's
12060 important that we use make_friend_class since it will
12061 perform some error-checking and output cross-reference
12062 information. */
12063 ++processing_template_decl;
12064
12065 if (friend_type != error_mark_node)
12066 make_friend_class (type, friend_type, /*complain=*/false);
12067
12068 if (adjust_processing_template_decl)
12069 --processing_template_decl;
12070 }
12071 else
12072 {
12073 /* Build new DECL_FRIENDLIST. */
12074 tree r;
12075
12076 /* The file and line for this declaration, to
12077 assist in error message reporting. Since we
12078 called push_tinst_level above, we don't need to
12079 restore these. */
12080 input_location = DECL_SOURCE_LOCATION (t);
12081
12082 if (TREE_CODE (t) == TEMPLATE_DECL)
12083 {
12084 ++processing_template_decl;
12085 push_deferring_access_checks (dk_no_check);
12086 }
12087
12088 r = tsubst_friend_function (t, args);
12089 add_friend (type, r, /*complain=*/false);
12090 if (TREE_CODE (t) == TEMPLATE_DECL)
12091 {
12092 pop_deferring_access_checks ();
12093 --processing_template_decl;
12094 }
12095 }
12096 }
12097 }
12098
12099 if (fn_context)
12100 {
12101 /* Restore these before substituting into the lambda capture
12102 initializers. */
12103 cp_unevaluated_operand = saved_unevaluated_operand;
12104 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12105 }
12106
12107 /* Set the file and line number information to whatever is given for
12108 the class itself. This puts error messages involving generated
12109 implicit functions at a predictable point, and the same point
12110 that would be used for non-template classes. */
12111 input_location = DECL_SOURCE_LOCATION (typedecl);
12112
12113 unreverse_member_declarations (type);
12114 finish_struct_1 (type);
12115 TYPE_BEING_DEFINED (type) = 0;
12116
12117 /* We don't instantiate default arguments for member functions. 14.7.1:
12118
12119 The implicit instantiation of a class template specialization causes
12120 the implicit instantiation of the declarations, but not of the
12121 definitions or default arguments, of the class member functions,
12122 member classes, static data members and member templates.... */
12123
12124 perform_instantiation_time_access_checks (pattern, args);
12125 perform_deferred_access_checks (tf_warning_or_error);
12126 pop_nested_class ();
12127 maximum_field_alignment = saved_maximum_field_alignment;
12128 if (!fn_context)
12129 pop_from_top_level ();
12130 pop_tinst_level ();
12131
12132 /* The vtable for a template class can be emitted in any translation
12133 unit in which the class is instantiated. When there is no key
12134 method, however, finish_struct_1 will already have added TYPE to
12135 the keyed_classes. */
12136 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12137 vec_safe_push (keyed_classes, type);
12138
12139 return type;
12140 }
12141
12142 /* Wrapper for instantiate_class_template_1. */
12143
12144 tree
12145 instantiate_class_template (tree type)
12146 {
12147 tree ret;
12148 timevar_push (TV_TEMPLATE_INST);
12149 ret = instantiate_class_template_1 (type);
12150 timevar_pop (TV_TEMPLATE_INST);
12151 return ret;
12152 }
12153
12154 tree
12155 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12156 {
12157 tree r;
12158
12159 if (!t)
12160 r = t;
12161 else if (TYPE_P (t))
12162 r = tsubst (t, args, complain, in_decl);
12163 else
12164 {
12165 if (!(complain & tf_warning))
12166 ++c_inhibit_evaluation_warnings;
12167 r = tsubst_expr (t, args, complain, in_decl,
12168 /*integral_constant_expression_p=*/true);
12169 if (!(complain & tf_warning))
12170 --c_inhibit_evaluation_warnings;
12171 }
12172
12173 return r;
12174 }
12175
12176 /* Given a function parameter pack TMPL_PARM and some function parameters
12177 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12178 and set *SPEC_P to point at the next point in the list. */
12179
12180 tree
12181 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12182 {
12183 /* Collect all of the extra "packed" parameters into an
12184 argument pack. */
12185 tree parmvec;
12186 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12187 tree spec_parm = *spec_p;
12188 int i, len;
12189
12190 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12191 if (tmpl_parm
12192 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12193 break;
12194
12195 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12196 parmvec = make_tree_vec (len);
12197 spec_parm = *spec_p;
12198 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12199 {
12200 tree elt = spec_parm;
12201 if (DECL_PACK_P (elt))
12202 elt = make_pack_expansion (elt);
12203 TREE_VEC_ELT (parmvec, i) = elt;
12204 }
12205
12206 /* Build the argument packs. */
12207 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12208 *spec_p = spec_parm;
12209
12210 return argpack;
12211 }
12212
12213 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12214 NONTYPE_ARGUMENT_PACK. */
12215
12216 static tree
12217 make_fnparm_pack (tree spec_parm)
12218 {
12219 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12220 }
12221
12222 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12223 pack expansion with no extra args, 2 if it has extra args, or 0
12224 if it is not a pack expansion. */
12225
12226 static int
12227 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12228 {
12229 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12230 /* We're being called before this happens in tsubst_pack_expansion. */
12231 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12232 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12233 if (i >= TREE_VEC_LENGTH (vec))
12234 return 0;
12235 tree elt = TREE_VEC_ELT (vec, i);
12236 if (DECL_P (elt))
12237 /* A decl pack is itself an expansion. */
12238 elt = TREE_TYPE (elt);
12239 if (!PACK_EXPANSION_P (elt))
12240 return 0;
12241 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12242 return 2;
12243 return 1;
12244 }
12245
12246
12247 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12248
12249 static tree
12250 make_argument_pack_select (tree arg_pack, unsigned index)
12251 {
12252 tree aps = make_node (ARGUMENT_PACK_SELECT);
12253
12254 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12255 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12256
12257 return aps;
12258 }
12259
12260 /* This is a subroutine of tsubst_pack_expansion.
12261
12262 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12263 mechanism to store the (non complete list of) arguments of the
12264 substitution and return a non substituted pack expansion, in order
12265 to wait for when we have enough arguments to really perform the
12266 substitution. */
12267
12268 static bool
12269 use_pack_expansion_extra_args_p (tree parm_packs,
12270 int arg_pack_len,
12271 bool has_empty_arg)
12272 {
12273 /* If one pack has an expansion and another pack has a normal
12274 argument or if one pack has an empty argument and an another
12275 one hasn't then tsubst_pack_expansion cannot perform the
12276 substitution and need to fall back on the
12277 PACK_EXPANSION_EXTRA mechanism. */
12278 if (parm_packs == NULL_TREE)
12279 return false;
12280 else if (has_empty_arg)
12281 {
12282 /* If all the actual packs are pack expansions, we can still
12283 subsitute directly. */
12284 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12285 {
12286 tree a = TREE_VALUE (p);
12287 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12288 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12289 a = ARGUMENT_PACK_ARGS (a);
12290 if (TREE_VEC_LENGTH (a) == 1)
12291 a = TREE_VEC_ELT (a, 0);
12292 if (PACK_EXPANSION_P (a))
12293 continue;
12294 return true;
12295 }
12296 return false;
12297 }
12298
12299 bool has_expansion_arg = false;
12300 for (int i = 0 ; i < arg_pack_len; ++i)
12301 {
12302 bool has_non_expansion_arg = false;
12303 for (tree parm_pack = parm_packs;
12304 parm_pack;
12305 parm_pack = TREE_CHAIN (parm_pack))
12306 {
12307 tree arg = TREE_VALUE (parm_pack);
12308
12309 int exp = argument_pack_element_is_expansion_p (arg, i);
12310 if (exp == 2)
12311 /* We can't substitute a pack expansion with extra args into
12312 our pattern. */
12313 return true;
12314 else if (exp)
12315 has_expansion_arg = true;
12316 else
12317 has_non_expansion_arg = true;
12318 }
12319
12320 if (has_expansion_arg && has_non_expansion_arg)
12321 return true;
12322 }
12323 return false;
12324 }
12325
12326 /* [temp.variadic]/6 says that:
12327
12328 The instantiation of a pack expansion [...]
12329 produces a list E1,E2, ..., En, where N is the number of elements
12330 in the pack expansion parameters.
12331
12332 This subroutine of tsubst_pack_expansion produces one of these Ei.
12333
12334 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12335 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12336 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12337 INDEX is the index 'i' of the element Ei to produce. ARGS,
12338 COMPLAIN, and IN_DECL are the same parameters as for the
12339 tsubst_pack_expansion function.
12340
12341 The function returns the resulting Ei upon successful completion,
12342 or error_mark_node.
12343
12344 Note that this function possibly modifies the ARGS parameter, so
12345 it's the responsibility of the caller to restore it. */
12346
12347 static tree
12348 gen_elem_of_pack_expansion_instantiation (tree pattern,
12349 tree parm_packs,
12350 unsigned index,
12351 tree args /* This parm gets
12352 modified. */,
12353 tsubst_flags_t complain,
12354 tree in_decl)
12355 {
12356 tree t;
12357 bool ith_elem_is_expansion = false;
12358
12359 /* For each parameter pack, change the substitution of the parameter
12360 pack to the ith argument in its argument pack, then expand the
12361 pattern. */
12362 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12363 {
12364 tree parm = TREE_PURPOSE (pack);
12365 tree arg_pack = TREE_VALUE (pack);
12366 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12367
12368 ith_elem_is_expansion |=
12369 argument_pack_element_is_expansion_p (arg_pack, index);
12370
12371 /* Select the Ith argument from the pack. */
12372 if (TREE_CODE (parm) == PARM_DECL
12373 || VAR_P (parm)
12374 || TREE_CODE (parm) == FIELD_DECL)
12375 {
12376 if (index == 0)
12377 {
12378 aps = make_argument_pack_select (arg_pack, index);
12379 if (!mark_used (parm, complain) && !(complain & tf_error))
12380 return error_mark_node;
12381 register_local_specialization (aps, parm);
12382 }
12383 else
12384 aps = retrieve_local_specialization (parm);
12385 }
12386 else
12387 {
12388 int idx, level;
12389 template_parm_level_and_index (parm, &level, &idx);
12390
12391 if (index == 0)
12392 {
12393 aps = make_argument_pack_select (arg_pack, index);
12394 /* Update the corresponding argument. */
12395 TMPL_ARG (args, level, idx) = aps;
12396 }
12397 else
12398 /* Re-use the ARGUMENT_PACK_SELECT. */
12399 aps = TMPL_ARG (args, level, idx);
12400 }
12401 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12402 }
12403
12404 /* Substitute into the PATTERN with the (possibly altered)
12405 arguments. */
12406 if (pattern == in_decl)
12407 /* Expanding a fixed parameter pack from
12408 coerce_template_parameter_pack. */
12409 t = tsubst_decl (pattern, args, complain);
12410 else if (pattern == error_mark_node)
12411 t = error_mark_node;
12412 else if (!TYPE_P (pattern))
12413 t = tsubst_expr (pattern, args, complain, in_decl,
12414 /*integral_constant_expression_p=*/false);
12415 else
12416 t = tsubst (pattern, args, complain, in_decl);
12417
12418 /* If the Ith argument pack element is a pack expansion, then
12419 the Ith element resulting from the substituting is going to
12420 be a pack expansion as well. */
12421 if (ith_elem_is_expansion)
12422 t = make_pack_expansion (t, complain);
12423
12424 return t;
12425 }
12426
12427 /* When the unexpanded parameter pack in a fold expression expands to an empty
12428 sequence, the value of the expression is as follows; the program is
12429 ill-formed if the operator is not listed in this table.
12430
12431 && true
12432 || false
12433 , void() */
12434
12435 tree
12436 expand_empty_fold (tree t, tsubst_flags_t complain)
12437 {
12438 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12439 if (!FOLD_EXPR_MODIFY_P (t))
12440 switch (code)
12441 {
12442 case TRUTH_ANDIF_EXPR:
12443 return boolean_true_node;
12444 case TRUTH_ORIF_EXPR:
12445 return boolean_false_node;
12446 case COMPOUND_EXPR:
12447 return void_node;
12448 default:
12449 break;
12450 }
12451
12452 if (complain & tf_error)
12453 error_at (location_of (t),
12454 "fold of empty expansion over %O", code);
12455 return error_mark_node;
12456 }
12457
12458 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12459 form an expression that combines the two terms using the
12460 operator of T. */
12461
12462 static tree
12463 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12464 {
12465 tree op = FOLD_EXPR_OP (t);
12466 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12467
12468 // Handle compound assignment operators.
12469 if (FOLD_EXPR_MODIFY_P (t))
12470 return build_x_modify_expr (input_location, left, code, right, complain);
12471
12472 warning_sentinel s(warn_parentheses);
12473 switch (code)
12474 {
12475 case COMPOUND_EXPR:
12476 return build_x_compound_expr (input_location, left, right, complain);
12477 default:
12478 return build_x_binary_op (input_location, code,
12479 left, TREE_CODE (left),
12480 right, TREE_CODE (right),
12481 /*overload=*/NULL,
12482 complain);
12483 }
12484 }
12485
12486 /* Substitute ARGS into the pack of a fold expression T. */
12487
12488 static inline tree
12489 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12490 {
12491 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12492 }
12493
12494 /* Substitute ARGS into the pack of a fold expression T. */
12495
12496 static inline tree
12497 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12498 {
12499 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12500 }
12501
12502 /* Expand a PACK of arguments into a grouped as left fold.
12503 Given a pack containing elements A0, A1, ..., An and an
12504 operator @, this builds the expression:
12505
12506 ((A0 @ A1) @ A2) ... @ An
12507
12508 Note that PACK must not be empty.
12509
12510 The operator is defined by the original fold expression T. */
12511
12512 static tree
12513 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12514 {
12515 tree left = TREE_VEC_ELT (pack, 0);
12516 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12517 {
12518 tree right = TREE_VEC_ELT (pack, i);
12519 left = fold_expression (t, left, right, complain);
12520 }
12521 return left;
12522 }
12523
12524 /* Substitute into a unary left fold expression. */
12525
12526 static tree
12527 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12528 tree in_decl)
12529 {
12530 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12531 if (pack == error_mark_node)
12532 return error_mark_node;
12533 if (PACK_EXPANSION_P (pack))
12534 {
12535 tree r = copy_node (t);
12536 FOLD_EXPR_PACK (r) = pack;
12537 return r;
12538 }
12539 if (TREE_VEC_LENGTH (pack) == 0)
12540 return expand_empty_fold (t, complain);
12541 else
12542 return expand_left_fold (t, pack, complain);
12543 }
12544
12545 /* Substitute into a binary left fold expression.
12546
12547 Do ths by building a single (non-empty) vector of argumnts and
12548 building the expression from those elements. */
12549
12550 static tree
12551 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12552 tree in_decl)
12553 {
12554 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12555 if (pack == error_mark_node)
12556 return error_mark_node;
12557 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12558 if (init == error_mark_node)
12559 return error_mark_node;
12560
12561 if (PACK_EXPANSION_P (pack))
12562 {
12563 tree r = copy_node (t);
12564 FOLD_EXPR_PACK (r) = pack;
12565 FOLD_EXPR_INIT (r) = init;
12566 return r;
12567 }
12568
12569 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12570 TREE_VEC_ELT (vec, 0) = init;
12571 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12572 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12573
12574 return expand_left_fold (t, vec, complain);
12575 }
12576
12577 /* Expand a PACK of arguments into a grouped as right fold.
12578 Given a pack containing elementns A0, A1, ..., and an
12579 operator @, this builds the expression:
12580
12581 A0@ ... (An-2 @ (An-1 @ An))
12582
12583 Note that PACK must not be empty.
12584
12585 The operator is defined by the original fold expression T. */
12586
12587 tree
12588 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12589 {
12590 // Build the expression.
12591 int n = TREE_VEC_LENGTH (pack);
12592 tree right = TREE_VEC_ELT (pack, n - 1);
12593 for (--n; n != 0; --n)
12594 {
12595 tree left = TREE_VEC_ELT (pack, n - 1);
12596 right = fold_expression (t, left, right, complain);
12597 }
12598 return right;
12599 }
12600
12601 /* Substitute into a unary right fold expression. */
12602
12603 static tree
12604 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12605 tree in_decl)
12606 {
12607 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12608 if (pack == error_mark_node)
12609 return error_mark_node;
12610 if (PACK_EXPANSION_P (pack))
12611 {
12612 tree r = copy_node (t);
12613 FOLD_EXPR_PACK (r) = pack;
12614 return r;
12615 }
12616 if (TREE_VEC_LENGTH (pack) == 0)
12617 return expand_empty_fold (t, complain);
12618 else
12619 return expand_right_fold (t, pack, complain);
12620 }
12621
12622 /* Substitute into a binary right fold expression.
12623
12624 Do ths by building a single (non-empty) vector of arguments and
12625 building the expression from those elements. */
12626
12627 static tree
12628 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12629 tree in_decl)
12630 {
12631 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12632 if (pack == error_mark_node)
12633 return error_mark_node;
12634 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12635 if (init == error_mark_node)
12636 return error_mark_node;
12637
12638 if (PACK_EXPANSION_P (pack))
12639 {
12640 tree r = copy_node (t);
12641 FOLD_EXPR_PACK (r) = pack;
12642 FOLD_EXPR_INIT (r) = init;
12643 return r;
12644 }
12645
12646 int n = TREE_VEC_LENGTH (pack);
12647 tree vec = make_tree_vec (n + 1);
12648 for (int i = 0; i < n; ++i)
12649 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12650 TREE_VEC_ELT (vec, n) = init;
12651
12652 return expand_right_fold (t, vec, complain);
12653 }
12654
12655 /* Walk through the pattern of a pack expansion, adding everything in
12656 local_specializations to a list. */
12657
12658 class el_data
12659 {
12660 public:
12661 hash_set<tree> internal;
12662 tree extra;
12663 tsubst_flags_t complain;
12664
12665 el_data (tsubst_flags_t c)
12666 : extra (NULL_TREE), complain (c) {}
12667 };
12668 static tree
12669 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12670 {
12671 el_data &data = *reinterpret_cast<el_data*>(data_);
12672 tree *extra = &data.extra;
12673 tsubst_flags_t complain = data.complain;
12674
12675 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12676 /* Remember local typedefs (85214). */
12677 tp = &TYPE_NAME (*tp);
12678
12679 if (TREE_CODE (*tp) == DECL_EXPR)
12680 data.internal.add (DECL_EXPR_DECL (*tp));
12681 else if (tree spec = retrieve_local_specialization (*tp))
12682 {
12683 if (data.internal.contains (*tp))
12684 /* Don't mess with variables declared within the pattern. */
12685 return NULL_TREE;
12686 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12687 {
12688 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12689 tree args = ARGUMENT_PACK_ARGS (spec);
12690 if (TREE_VEC_LENGTH (args) == 1)
12691 {
12692 tree elt = TREE_VEC_ELT (args, 0);
12693 if (PACK_EXPANSION_P (elt))
12694 elt = PACK_EXPANSION_PATTERN (elt);
12695 if (DECL_PACK_P (elt))
12696 spec = elt;
12697 }
12698 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12699 {
12700 /* Handle lambda capture here, since we aren't doing any
12701 substitution now, and so tsubst_copy won't call
12702 process_outer_var_ref. */
12703 tree args = ARGUMENT_PACK_ARGS (spec);
12704 int len = TREE_VEC_LENGTH (args);
12705 for (int i = 0; i < len; ++i)
12706 {
12707 tree arg = TREE_VEC_ELT (args, i);
12708 tree carg = arg;
12709 if (outer_automatic_var_p (arg))
12710 carg = process_outer_var_ref (arg, complain);
12711 if (carg != arg)
12712 {
12713 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12714 proxies. */
12715 if (i == 0)
12716 {
12717 spec = copy_node (spec);
12718 args = copy_node (args);
12719 SET_ARGUMENT_PACK_ARGS (spec, args);
12720 register_local_specialization (spec, *tp);
12721 }
12722 TREE_VEC_ELT (args, i) = carg;
12723 }
12724 }
12725 }
12726 }
12727 if (outer_automatic_var_p (spec))
12728 spec = process_outer_var_ref (spec, complain);
12729 *extra = tree_cons (*tp, spec, *extra);
12730 }
12731 return NULL_TREE;
12732 }
12733 static tree
12734 extract_local_specs (tree pattern, tsubst_flags_t complain)
12735 {
12736 el_data data (complain);
12737 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12738 return data.extra;
12739 }
12740
12741 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12742 for use in PACK_EXPANSION_EXTRA_ARGS. */
12743
12744 tree
12745 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12746 {
12747 tree extra = args;
12748 if (local_specializations)
12749 if (tree locals = extract_local_specs (pattern, complain))
12750 extra = tree_cons (NULL_TREE, extra, locals);
12751 return extra;
12752 }
12753
12754 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12755 normal template args to ARGS. */
12756
12757 tree
12758 add_extra_args (tree extra, tree args)
12759 {
12760 if (extra && TREE_CODE (extra) == TREE_LIST)
12761 {
12762 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12763 {
12764 /* The partial instantiation involved local declarations collected in
12765 extract_local_specs; map from the general template to our local
12766 context. */
12767 tree gen = TREE_PURPOSE (elt);
12768 tree inst = TREE_VALUE (elt);
12769 if (DECL_P (inst))
12770 if (tree local = retrieve_local_specialization (inst))
12771 inst = local;
12772 /* else inst is already a full instantiation of the pack. */
12773 register_local_specialization (inst, gen);
12774 }
12775 gcc_assert (!TREE_PURPOSE (extra));
12776 extra = TREE_VALUE (extra);
12777 }
12778 #if 1
12779 /* I think we should always be able to substitute dependent args into the
12780 pattern. If that turns out to be incorrect in some cases, enable the
12781 alternate code (and add complain/in_decl parms to this function). */
12782 gcc_checking_assert (!uses_template_parms (extra));
12783 #else
12784 if (!uses_template_parms (extra))
12785 {
12786 gcc_unreachable ();
12787 extra = tsubst_template_args (extra, args, complain, in_decl);
12788 args = add_outermost_template_args (args, extra);
12789 }
12790 else
12791 #endif
12792 args = add_to_template_args (extra, args);
12793 return args;
12794 }
12795
12796 /* Substitute ARGS into T, which is an pack expansion
12797 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12798 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12799 (if only a partial substitution could be performed) or
12800 ERROR_MARK_NODE if there was an error. */
12801 tree
12802 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12803 tree in_decl)
12804 {
12805 tree pattern;
12806 tree pack, packs = NULL_TREE;
12807 bool unsubstituted_packs = false;
12808 int i, len = -1;
12809 tree result;
12810 bool need_local_specializations = false;
12811 int levels;
12812
12813 gcc_assert (PACK_EXPANSION_P (t));
12814 pattern = PACK_EXPANSION_PATTERN (t);
12815
12816 /* Add in any args remembered from an earlier partial instantiation. */
12817 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12818
12819 levels = TMPL_ARGS_DEPTH (args);
12820
12821 /* Determine the argument packs that will instantiate the parameter
12822 packs used in the expansion expression. While we're at it,
12823 compute the number of arguments to be expanded and make sure it
12824 is consistent. */
12825 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12826 pack = TREE_CHAIN (pack))
12827 {
12828 tree parm_pack = TREE_VALUE (pack);
12829 tree arg_pack = NULL_TREE;
12830 tree orig_arg = NULL_TREE;
12831 int level = 0;
12832
12833 if (TREE_CODE (parm_pack) == BASES)
12834 {
12835 gcc_assert (parm_pack == pattern);
12836 if (BASES_DIRECT (parm_pack))
12837 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12838 args, complain,
12839 in_decl, false),
12840 complain);
12841 else
12842 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12843 args, complain, in_decl,
12844 false), complain);
12845 }
12846 else if (builtin_pack_call_p (parm_pack))
12847 {
12848 if (parm_pack != pattern)
12849 {
12850 if (complain & tf_error)
12851 sorry ("%qE is not the entire pattern of the pack expansion",
12852 parm_pack);
12853 return error_mark_node;
12854 }
12855 return expand_builtin_pack_call (parm_pack, args,
12856 complain, in_decl);
12857 }
12858 else if (TREE_CODE (parm_pack) == PARM_DECL)
12859 {
12860 /* We know we have correct local_specializations if this
12861 expansion is at function scope, or if we're dealing with a
12862 local parameter in a requires expression; for the latter,
12863 tsubst_requires_expr set it up appropriately. */
12864 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12865 arg_pack = retrieve_local_specialization (parm_pack);
12866 else
12867 /* We can't rely on local_specializations for a parameter
12868 name used later in a function declaration (such as in a
12869 late-specified return type). Even if it exists, it might
12870 have the wrong value for a recursive call. */
12871 need_local_specializations = true;
12872
12873 if (!arg_pack)
12874 {
12875 /* This parameter pack was used in an unevaluated context. Just
12876 make a dummy decl, since it's only used for its type. */
12877 ++cp_unevaluated_operand;
12878 arg_pack = tsubst_decl (parm_pack, args, complain);
12879 --cp_unevaluated_operand;
12880 if (arg_pack && DECL_PACK_P (arg_pack))
12881 /* Partial instantiation of the parm_pack, we can't build
12882 up an argument pack yet. */
12883 arg_pack = NULL_TREE;
12884 else
12885 arg_pack = make_fnparm_pack (arg_pack);
12886 }
12887 else if (DECL_PACK_P (arg_pack))
12888 /* This argument pack isn't fully instantiated yet. */
12889 arg_pack = NULL_TREE;
12890 }
12891 else if (is_capture_proxy (parm_pack))
12892 {
12893 arg_pack = retrieve_local_specialization (parm_pack);
12894 if (DECL_PACK_P (arg_pack))
12895 arg_pack = NULL_TREE;
12896 }
12897 else
12898 {
12899 int idx;
12900 template_parm_level_and_index (parm_pack, &level, &idx);
12901 if (level <= levels)
12902 arg_pack = TMPL_ARG (args, level, idx);
12903
12904 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12905 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12906 arg_pack = NULL_TREE;
12907 }
12908
12909 orig_arg = arg_pack;
12910 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12911 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12912
12913 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12914 /* This can only happen if we forget to expand an argument
12915 pack somewhere else. Just return an error, silently. */
12916 {
12917 result = make_tree_vec (1);
12918 TREE_VEC_ELT (result, 0) = error_mark_node;
12919 return result;
12920 }
12921
12922 if (arg_pack)
12923 {
12924 int my_len =
12925 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12926
12927 /* Don't bother trying to do a partial substitution with
12928 incomplete packs; we'll try again after deduction. */
12929 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12930 return t;
12931
12932 if (len < 0)
12933 len = my_len;
12934 else if (len != my_len)
12935 {
12936 if (!(complain & tf_error))
12937 /* Fail quietly. */;
12938 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12939 error ("mismatched argument pack lengths while expanding %qT",
12940 pattern);
12941 else
12942 error ("mismatched argument pack lengths while expanding %qE",
12943 pattern);
12944 return error_mark_node;
12945 }
12946
12947 /* Keep track of the parameter packs and their corresponding
12948 argument packs. */
12949 packs = tree_cons (parm_pack, arg_pack, packs);
12950 TREE_TYPE (packs) = orig_arg;
12951 }
12952 else
12953 {
12954 /* We can't substitute for this parameter pack. We use a flag as
12955 well as the missing_level counter because function parameter
12956 packs don't have a level. */
12957 gcc_assert (processing_template_decl || is_auto (parm_pack));
12958 unsubstituted_packs = true;
12959 }
12960 }
12961
12962 /* If the expansion is just T..., return the matching argument pack, unless
12963 we need to call convert_from_reference on all the elements. This is an
12964 important optimization; see c++/68422. */
12965 if (!unsubstituted_packs
12966 && TREE_PURPOSE (packs) == pattern)
12967 {
12968 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12969
12970 /* If the argument pack is a single pack expansion, pull it out. */
12971 if (TREE_VEC_LENGTH (args) == 1
12972 && pack_expansion_args_count (args))
12973 return TREE_VEC_ELT (args, 0);
12974
12975 /* Types need no adjustment, nor does sizeof..., and if we still have
12976 some pack expansion args we won't do anything yet. */
12977 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12978 || PACK_EXPANSION_SIZEOF_P (t)
12979 || pack_expansion_args_count (args))
12980 return args;
12981 /* Also optimize expression pack expansions if we can tell that the
12982 elements won't have reference type. */
12983 tree type = TREE_TYPE (pattern);
12984 if (type && !TYPE_REF_P (type)
12985 && !PACK_EXPANSION_P (type)
12986 && !WILDCARD_TYPE_P (type))
12987 return args;
12988 /* Otherwise use the normal path so we get convert_from_reference. */
12989 }
12990
12991 /* We cannot expand this expansion expression, because we don't have
12992 all of the argument packs we need. */
12993 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12994 {
12995 /* We got some full packs, but we can't substitute them in until we
12996 have values for all the packs. So remember these until then. */
12997
12998 t = make_pack_expansion (pattern, complain);
12999 PACK_EXPANSION_EXTRA_ARGS (t)
13000 = build_extra_args (pattern, args, complain);
13001 return t;
13002 }
13003
13004 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13005 type, so create our own local specializations map; the current map is
13006 either NULL or (in the case of recursive unification) might have
13007 bindings that we don't want to use or alter. */
13008 local_specialization_stack lss (need_local_specializations
13009 ? lss_blank : lss_nop);
13010
13011 if (unsubstituted_packs)
13012 {
13013 /* There were no real arguments, we're just replacing a parameter
13014 pack with another version of itself. Substitute into the
13015 pattern and return a PACK_EXPANSION_*. The caller will need to
13016 deal with that. */
13017 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13018 t = tsubst_expr (pattern, args, complain, in_decl,
13019 /*integral_constant_expression_p=*/false);
13020 else
13021 t = tsubst (pattern, args, complain, in_decl);
13022 t = make_pack_expansion (t, complain);
13023 return t;
13024 }
13025
13026 gcc_assert (len >= 0);
13027
13028 /* For each argument in each argument pack, substitute into the
13029 pattern. */
13030 result = make_tree_vec (len);
13031 tree elem_args = copy_template_args (args);
13032 for (i = 0; i < len; ++i)
13033 {
13034 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13035 i,
13036 elem_args, complain,
13037 in_decl);
13038 TREE_VEC_ELT (result, i) = t;
13039 if (t == error_mark_node)
13040 {
13041 result = error_mark_node;
13042 break;
13043 }
13044 }
13045
13046 /* Update ARGS to restore the substitution from parameter packs to
13047 their argument packs. */
13048 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13049 {
13050 tree parm = TREE_PURPOSE (pack);
13051
13052 if (TREE_CODE (parm) == PARM_DECL
13053 || VAR_P (parm)
13054 || TREE_CODE (parm) == FIELD_DECL)
13055 register_local_specialization (TREE_TYPE (pack), parm);
13056 else
13057 {
13058 int idx, level;
13059
13060 if (TREE_VALUE (pack) == NULL_TREE)
13061 continue;
13062
13063 template_parm_level_and_index (parm, &level, &idx);
13064
13065 /* Update the corresponding argument. */
13066 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13067 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13068 TREE_TYPE (pack);
13069 else
13070 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13071 }
13072 }
13073
13074 /* If the dependent pack arguments were such that we end up with only a
13075 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13076 if (len == 1 && TREE_CODE (result) == TREE_VEC
13077 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13078 return TREE_VEC_ELT (result, 0);
13079
13080 return result;
13081 }
13082
13083 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13084 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13085 parameter packs; all parms generated from a function parameter pack will
13086 have the same DECL_PARM_INDEX. */
13087
13088 tree
13089 get_pattern_parm (tree parm, tree tmpl)
13090 {
13091 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13092 tree patparm;
13093
13094 if (DECL_ARTIFICIAL (parm))
13095 {
13096 for (patparm = DECL_ARGUMENTS (pattern);
13097 patparm; patparm = DECL_CHAIN (patparm))
13098 if (DECL_ARTIFICIAL (patparm)
13099 && DECL_NAME (parm) == DECL_NAME (patparm))
13100 break;
13101 }
13102 else
13103 {
13104 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13105 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13106 gcc_assert (DECL_PARM_INDEX (patparm)
13107 == DECL_PARM_INDEX (parm));
13108 }
13109
13110 return patparm;
13111 }
13112
13113 /* Make an argument pack out of the TREE_VEC VEC. */
13114
13115 static tree
13116 make_argument_pack (tree vec)
13117 {
13118 tree pack;
13119
13120 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13121 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13122 else
13123 {
13124 pack = make_node (NONTYPE_ARGUMENT_PACK);
13125 TREE_CONSTANT (pack) = 1;
13126 }
13127 SET_ARGUMENT_PACK_ARGS (pack, vec);
13128 return pack;
13129 }
13130
13131 /* Return an exact copy of template args T that can be modified
13132 independently. */
13133
13134 static tree
13135 copy_template_args (tree t)
13136 {
13137 if (t == error_mark_node)
13138 return t;
13139
13140 int len = TREE_VEC_LENGTH (t);
13141 tree new_vec = make_tree_vec (len);
13142
13143 for (int i = 0; i < len; ++i)
13144 {
13145 tree elt = TREE_VEC_ELT (t, i);
13146 if (elt && TREE_CODE (elt) == TREE_VEC)
13147 elt = copy_template_args (elt);
13148 TREE_VEC_ELT (new_vec, i) = elt;
13149 }
13150
13151 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13152 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13153
13154 return new_vec;
13155 }
13156
13157 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13158
13159 tree
13160 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13161 tree in_decl)
13162 {
13163 /* Substitute into each of the arguments. */
13164 tree new_arg = TYPE_P (orig_arg)
13165 ? cxx_make_type (TREE_CODE (orig_arg))
13166 : make_node (TREE_CODE (orig_arg));
13167
13168 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13169 args, complain, in_decl);
13170 if (pack_args == error_mark_node)
13171 new_arg = error_mark_node;
13172 else
13173 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13174
13175 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13176 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13177
13178 return new_arg;
13179 }
13180
13181 /* Substitute ARGS into the vector or list of template arguments T. */
13182
13183 tree
13184 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13185 {
13186 tree orig_t = t;
13187 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13188 tree *elts;
13189
13190 if (t == error_mark_node)
13191 return error_mark_node;
13192
13193 len = TREE_VEC_LENGTH (t);
13194 elts = XALLOCAVEC (tree, len);
13195
13196 for (i = 0; i < len; i++)
13197 {
13198 tree orig_arg = TREE_VEC_ELT (t, i);
13199 tree new_arg;
13200
13201 if (TREE_CODE (orig_arg) == TREE_VEC)
13202 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13203 else if (PACK_EXPANSION_P (orig_arg))
13204 {
13205 /* Substitute into an expansion expression. */
13206 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13207
13208 if (TREE_CODE (new_arg) == TREE_VEC)
13209 /* Add to the expanded length adjustment the number of
13210 expanded arguments. We subtract one from this
13211 measurement, because the argument pack expression
13212 itself is already counted as 1 in
13213 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13214 the argument pack is empty. */
13215 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13216 }
13217 else if (ARGUMENT_PACK_P (orig_arg))
13218 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13219 else
13220 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13221
13222 if (new_arg == error_mark_node)
13223 return error_mark_node;
13224
13225 elts[i] = new_arg;
13226 if (new_arg != orig_arg)
13227 need_new = 1;
13228 }
13229
13230 if (!need_new)
13231 return t;
13232
13233 /* Make space for the expanded arguments coming from template
13234 argument packs. */
13235 t = make_tree_vec (len + expanded_len_adjust);
13236 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13237 arguments for a member template.
13238 In that case each TREE_VEC in ORIG_T represents a level of template
13239 arguments, and ORIG_T won't carry any non defaulted argument count.
13240 It will rather be the nested TREE_VECs that will carry one.
13241 In other words, ORIG_T carries a non defaulted argument count only
13242 if it doesn't contain any nested TREE_VEC. */
13243 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13244 {
13245 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13246 count += expanded_len_adjust;
13247 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13248 }
13249 for (i = 0, out = 0; i < len; i++)
13250 {
13251 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13252 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13253 && TREE_CODE (elts[i]) == TREE_VEC)
13254 {
13255 int idx;
13256
13257 /* Now expand the template argument pack "in place". */
13258 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13259 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13260 }
13261 else
13262 {
13263 TREE_VEC_ELT (t, out) = elts[i];
13264 out++;
13265 }
13266 }
13267
13268 return t;
13269 }
13270
13271 /* Substitute ARGS into one level PARMS of template parameters. */
13272
13273 static tree
13274 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13275 {
13276 if (parms == error_mark_node)
13277 return error_mark_node;
13278
13279 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13280
13281 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13282 {
13283 tree tuple = TREE_VEC_ELT (parms, i);
13284
13285 if (tuple == error_mark_node)
13286 continue;
13287
13288 TREE_VEC_ELT (new_vec, i) =
13289 tsubst_template_parm (tuple, args, complain);
13290 }
13291
13292 return new_vec;
13293 }
13294
13295 /* Return the result of substituting ARGS into the template parameters
13296 given by PARMS. If there are m levels of ARGS and m + n levels of
13297 PARMS, then the result will contain n levels of PARMS. For
13298 example, if PARMS is `template <class T> template <class U>
13299 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13300 result will be `template <int*, double, class V>'. */
13301
13302 static tree
13303 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13304 {
13305 tree r = NULL_TREE;
13306 tree* new_parms;
13307
13308 /* When substituting into a template, we must set
13309 PROCESSING_TEMPLATE_DECL as the template parameters may be
13310 dependent if they are based on one-another, and the dependency
13311 predicates are short-circuit outside of templates. */
13312 ++processing_template_decl;
13313
13314 for (new_parms = &r;
13315 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13316 new_parms = &(TREE_CHAIN (*new_parms)),
13317 parms = TREE_CHAIN (parms))
13318 {
13319 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13320 args, complain);
13321 *new_parms =
13322 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13323 - TMPL_ARGS_DEPTH (args)),
13324 new_vec, NULL_TREE);
13325 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13326 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13327 }
13328
13329 --processing_template_decl;
13330
13331 return r;
13332 }
13333
13334 /* Return the result of substituting ARGS into one template parameter
13335 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13336 parameter and which TREE_PURPOSE is the default argument of the
13337 template parameter. */
13338
13339 static tree
13340 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13341 {
13342 tree default_value, parm_decl;
13343
13344 if (args == NULL_TREE
13345 || t == NULL_TREE
13346 || t == error_mark_node)
13347 return t;
13348
13349 gcc_assert (TREE_CODE (t) == TREE_LIST);
13350
13351 default_value = TREE_PURPOSE (t);
13352 parm_decl = TREE_VALUE (t);
13353 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13354
13355 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13356 if (TREE_CODE (parm_decl) == PARM_DECL
13357 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13358 parm_decl = error_mark_node;
13359 default_value = tsubst_template_arg (default_value, args,
13360 complain, NULL_TREE);
13361 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13362
13363 tree r = build_tree_list (default_value, parm_decl);
13364 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13365 return r;
13366 }
13367
13368 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13369 type T. If T is not an aggregate or enumeration type, it is
13370 handled as if by tsubst. IN_DECL is as for tsubst. If
13371 ENTERING_SCOPE is nonzero, T is the context for a template which
13372 we are presently tsubst'ing. Return the substituted value. */
13373
13374 static tree
13375 tsubst_aggr_type (tree t,
13376 tree args,
13377 tsubst_flags_t complain,
13378 tree in_decl,
13379 int entering_scope)
13380 {
13381 if (t == NULL_TREE)
13382 return NULL_TREE;
13383
13384 switch (TREE_CODE (t))
13385 {
13386 case RECORD_TYPE:
13387 if (TYPE_PTRMEMFUNC_P (t))
13388 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13389
13390 /* Fall through. */
13391 case ENUMERAL_TYPE:
13392 case UNION_TYPE:
13393 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13394 {
13395 tree argvec;
13396 tree context;
13397 tree r;
13398
13399 /* In "sizeof(X<I>)" we need to evaluate "I". */
13400 cp_evaluated ev;
13401
13402 /* First, determine the context for the type we are looking
13403 up. */
13404 context = TYPE_CONTEXT (t);
13405 if (context && TYPE_P (context))
13406 {
13407 context = tsubst_aggr_type (context, args, complain,
13408 in_decl, /*entering_scope=*/1);
13409 /* If context is a nested class inside a class template,
13410 it may still need to be instantiated (c++/33959). */
13411 context = complete_type (context);
13412 }
13413
13414 /* Then, figure out what arguments are appropriate for the
13415 type we are trying to find. For example, given:
13416
13417 template <class T> struct S;
13418 template <class T, class U> void f(T, U) { S<U> su; }
13419
13420 and supposing that we are instantiating f<int, double>,
13421 then our ARGS will be {int, double}, but, when looking up
13422 S we only want {double}. */
13423 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13424 complain, in_decl);
13425 if (argvec == error_mark_node)
13426 r = error_mark_node;
13427 else if (cxx_dialect >= cxx17 && dependent_scope_p (context))
13428 {
13429 /* See maybe_dependent_member_ref. */
13430 tree name = TYPE_IDENTIFIER (t);
13431 tree fullname = name;
13432 if (instantiates_primary_template_p (t))
13433 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13434 INNERMOST_TEMPLATE_ARGS (argvec));
13435 return build_typename_type (context, name, fullname,
13436 typename_type);
13437 }
13438 else
13439 {
13440 r = lookup_template_class (t, argvec, in_decl, context,
13441 entering_scope, complain);
13442 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13443 }
13444
13445 return r;
13446 }
13447 else
13448 /* This is not a template type, so there's nothing to do. */
13449 return t;
13450
13451 default:
13452 return tsubst (t, args, complain, in_decl);
13453 }
13454 }
13455
13456 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13457
13458 /* Substitute into the default argument ARG (a default argument for
13459 FN), which has the indicated TYPE. */
13460
13461 tree
13462 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13463 tsubst_flags_t complain)
13464 {
13465 int errs = errorcount + sorrycount;
13466
13467 /* This can happen in invalid code. */
13468 if (TREE_CODE (arg) == DEFERRED_PARSE)
13469 return arg;
13470
13471 /* Shortcut {}. */
13472 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13473 && CONSTRUCTOR_NELTS (arg) == 0)
13474 return arg;
13475
13476 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13477 parm = chain_index (parmnum, parm);
13478 tree parmtype = TREE_TYPE (parm);
13479 if (DECL_BY_REFERENCE (parm))
13480 parmtype = TREE_TYPE (parmtype);
13481 if (parmtype == error_mark_node)
13482 return error_mark_node;
13483
13484 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13485
13486 tree *slot;
13487 if (defarg_inst && (slot = defarg_inst->get (parm)))
13488 return *slot;
13489
13490 /* This default argument came from a template. Instantiate the
13491 default argument here, not in tsubst. In the case of
13492 something like:
13493
13494 template <class T>
13495 struct S {
13496 static T t();
13497 void f(T = t());
13498 };
13499
13500 we must be careful to do name lookup in the scope of S<T>,
13501 rather than in the current class. */
13502 push_to_top_level ();
13503 push_access_scope (fn);
13504 push_deferring_access_checks (dk_no_deferred);
13505 start_lambda_scope (parm);
13506
13507 /* The default argument expression may cause implicitly defined
13508 member functions to be synthesized, which will result in garbage
13509 collection. We must treat this situation as if we were within
13510 the body of function so as to avoid collecting live data on the
13511 stack. */
13512 ++function_depth;
13513 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13514 complain, NULL_TREE,
13515 /*integral_constant_expression_p=*/false);
13516 --function_depth;
13517
13518 finish_lambda_scope ();
13519
13520 /* Make sure the default argument is reasonable. */
13521 arg = check_default_argument (type, arg, complain);
13522
13523 if (errorcount+sorrycount > errs
13524 && (complain & tf_warning_or_error))
13525 inform (input_location,
13526 " when instantiating default argument for call to %qD", fn);
13527
13528 pop_deferring_access_checks ();
13529 pop_access_scope (fn);
13530 pop_from_top_level ();
13531
13532 if (arg != error_mark_node && !cp_unevaluated_operand)
13533 {
13534 if (!defarg_inst)
13535 defarg_inst = decl_tree_cache_map::create_ggc (37);
13536 defarg_inst->put (parm, arg);
13537 }
13538
13539 return arg;
13540 }
13541
13542 /* Substitute into all the default arguments for FN. */
13543
13544 static void
13545 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13546 {
13547 tree arg;
13548 tree tmpl_args;
13549
13550 tmpl_args = DECL_TI_ARGS (fn);
13551
13552 /* If this function is not yet instantiated, we certainly don't need
13553 its default arguments. */
13554 if (uses_template_parms (tmpl_args))
13555 return;
13556 /* Don't do this again for clones. */
13557 if (DECL_CLONED_FUNCTION_P (fn))
13558 return;
13559
13560 int i = 0;
13561 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13562 arg;
13563 arg = TREE_CHAIN (arg), ++i)
13564 if (TREE_PURPOSE (arg))
13565 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13566 TREE_VALUE (arg),
13567 TREE_PURPOSE (arg),
13568 complain);
13569 }
13570
13571 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13572 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13573
13574 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13575
13576 void
13577 store_explicit_specifier (tree v, tree t)
13578 {
13579 if (!explicit_specifier_map)
13580 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13581 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13582 explicit_specifier_map->put (v, t);
13583 }
13584
13585 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13586
13587 static tree
13588 lookup_explicit_specifier (tree v)
13589 {
13590 return *explicit_specifier_map->get (v);
13591 }
13592
13593 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13594 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13595 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13596 identical to T. */
13597
13598 static tree
13599 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13600 tree raises, tsubst_flags_t complain)
13601 {
13602 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13603
13604 tree new_type;
13605 if (TREE_CODE (t) == FUNCTION_TYPE)
13606 {
13607 new_type = build_function_type (return_type, arg_types);
13608 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13609 }
13610 else
13611 {
13612 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13613 /* Don't pick up extra function qualifiers from the basetype. */
13614 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13615 if (! MAYBE_CLASS_TYPE_P (r))
13616 {
13617 /* [temp.deduct]
13618
13619 Type deduction may fail for any of the following
13620 reasons:
13621
13622 -- Attempting to create "pointer to member of T" when T
13623 is not a class type. */
13624 if (complain & tf_error)
13625 error ("creating pointer to member function of non-class type %qT",
13626 r);
13627 return error_mark_node;
13628 }
13629
13630 new_type = build_method_type_directly (r, return_type,
13631 TREE_CHAIN (arg_types));
13632 }
13633 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13634
13635 cp_ref_qualifier rqual = type_memfn_rqual (t);
13636 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13637 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13638 }
13639
13640 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13641 each of its formal parameters. If there is a disagreement then rebuild
13642 DECL's function type according to its formal parameter types, as part of a
13643 resolution for Core issues 1001/1322. */
13644
13645 static void
13646 maybe_rebuild_function_decl_type (tree decl)
13647 {
13648 bool function_type_needs_rebuilding = false;
13649 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13650 {
13651 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13652 while (parm_type_list && parm_type_list != void_list_node)
13653 {
13654 tree parm_type = TREE_VALUE (parm_type_list);
13655 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13656 if (!same_type_p (parm_type, formal_parm_type_unqual))
13657 {
13658 function_type_needs_rebuilding = true;
13659 break;
13660 }
13661
13662 parm_list = DECL_CHAIN (parm_list);
13663 parm_type_list = TREE_CHAIN (parm_type_list);
13664 }
13665 }
13666
13667 if (!function_type_needs_rebuilding)
13668 return;
13669
13670 const tree fntype = TREE_TYPE (decl);
13671 tree parm_list = DECL_ARGUMENTS (decl);
13672 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13673 tree new_parm_type_list = NULL_TREE;
13674 tree *q = &new_parm_type_list;
13675 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13676 {
13677 *q = copy_node (old_parm_type_list);
13678 parm_list = DECL_CHAIN (parm_list);
13679 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13680 q = &TREE_CHAIN (*q);
13681 }
13682 while (old_parm_type_list && old_parm_type_list != void_list_node)
13683 {
13684 *q = copy_node (old_parm_type_list);
13685 tree *new_parm_type = &TREE_VALUE (*q);
13686 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13687 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13688 *new_parm_type = formal_parm_type_unqual;
13689
13690 parm_list = DECL_CHAIN (parm_list);
13691 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13692 q = &TREE_CHAIN (*q);
13693 }
13694 if (old_parm_type_list == void_list_node)
13695 *q = void_list_node;
13696
13697 TREE_TYPE (decl)
13698 = rebuild_function_or_method_type (fntype,
13699 TREE_TYPE (fntype), new_parm_type_list,
13700 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13701 }
13702
13703 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13704
13705 static tree
13706 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13707 tree lambda_fntype)
13708 {
13709 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13710 hashval_t hash = 0;
13711 tree in_decl = t;
13712
13713 /* Nobody should be tsubst'ing into non-template functions. */
13714 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13715 || DECL_LOCAL_DECL_P (t));
13716
13717 if (DECL_LOCAL_DECL_P (t)
13718 && !DECL_OMP_DECLARE_REDUCTION_P (t))
13719 {
13720 if (tree spec = retrieve_local_specialization (t))
13721 return spec;
13722 }
13723 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13724 {
13725 /* If T is not dependent, just return it. */
13726 if (!uses_template_parms (DECL_TI_ARGS (t))
13727 && !LAMBDA_FUNCTION_P (t))
13728 return t;
13729
13730 /* Calculate the most general template of which R is a
13731 specialization. */
13732 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13733
13734 /* We're substituting a lambda function under tsubst_lambda_expr but not
13735 directly from it; find the matching function we're already inside.
13736 But don't do this if T is a generic lambda with a single level of
13737 template parms, as in that case we're doing a normal instantiation. */
13738 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13739 && (!generic_lambda_fn_p (t)
13740 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13741 return enclosing_instantiation_of (t);
13742
13743 /* Calculate the complete set of arguments used to
13744 specialize R. */
13745 argvec = tsubst_template_args (DECL_TI_ARGS
13746 (DECL_TEMPLATE_RESULT
13747 (DECL_TI_TEMPLATE (t))),
13748 args, complain, in_decl);
13749 if (argvec == error_mark_node)
13750 return error_mark_node;
13751
13752 /* Check to see if we already have this specialization. */
13753 if (!lambda_fntype)
13754 {
13755 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13756 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13757 return spec;
13758 }
13759
13760 /* We can see more levels of arguments than parameters if
13761 there was a specialization of a member template, like
13762 this:
13763
13764 template <class T> struct S { template <class U> void f(); }
13765 template <> template <class U> void S<int>::f(U);
13766
13767 Here, we'll be substituting into the specialization,
13768 because that's where we can find the code we actually
13769 want to generate, but we'll have enough arguments for
13770 the most general template.
13771
13772 We also deal with the peculiar case:
13773
13774 template <class T> struct S {
13775 template <class U> friend void f();
13776 };
13777 template <class U> void f() {}
13778 template S<int>;
13779 template void f<double>();
13780
13781 Here, the ARGS for the instantiation of will be {int,
13782 double}. But, we only need as many ARGS as there are
13783 levels of template parameters in CODE_PATTERN. We are
13784 careful not to get fooled into reducing the ARGS in
13785 situations like:
13786
13787 template <class T> struct S { template <class U> void f(U); }
13788 template <class T> template <> void S<T>::f(int) {}
13789
13790 which we can spot because the pattern will be a
13791 specialization in this case. */
13792 int args_depth = TMPL_ARGS_DEPTH (args);
13793 int parms_depth =
13794 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13795
13796 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13797 args = get_innermost_template_args (args, parms_depth);
13798 }
13799 else
13800 {
13801 /* This special case arises when we have something like this:
13802
13803 template <class T> struct S {
13804 friend void f<int>(int, double);
13805 };
13806
13807 Here, the DECL_TI_TEMPLATE for the friend declaration
13808 will be an IDENTIFIER_NODE. We are being called from
13809 tsubst_friend_function, and we want only to create a
13810 new decl (R) with appropriate types so that we can call
13811 determine_specialization. */
13812 gen_tmpl = NULL_TREE;
13813 argvec = NULL_TREE;
13814 }
13815
13816 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13817 : NULL_TREE);
13818 tree ctx = closure ? closure : DECL_CONTEXT (t);
13819 bool member = ctx && TYPE_P (ctx);
13820
13821 if (member && !closure)
13822 ctx = tsubst_aggr_type (ctx, args,
13823 complain, t, /*entering_scope=*/1);
13824
13825 tree type = (lambda_fntype ? lambda_fntype
13826 : tsubst (TREE_TYPE (t), args,
13827 complain | tf_fndecl_type, in_decl));
13828 if (type == error_mark_node)
13829 return error_mark_node;
13830
13831 /* If we hit excessive deduction depth, the type is bogus even if
13832 it isn't error_mark_node, so don't build a decl. */
13833 if (excessive_deduction_depth)
13834 return error_mark_node;
13835
13836 /* We do NOT check for matching decls pushed separately at this
13837 point, as they may not represent instantiations of this
13838 template, and in any case are considered separate under the
13839 discrete model. */
13840 tree r = copy_decl (t);
13841 DECL_USE_TEMPLATE (r) = 0;
13842 TREE_TYPE (r) = type;
13843 /* Clear out the mangled name and RTL for the instantiation. */
13844 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13845 SET_DECL_RTL (r, NULL);
13846 /* Leave DECL_INITIAL set on deleted instantiations. */
13847 if (!DECL_DELETED_FN (r))
13848 DECL_INITIAL (r) = NULL_TREE;
13849 DECL_CONTEXT (r) = ctx;
13850
13851 /* Handle explicit(dependent-expr). */
13852 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13853 {
13854 tree spec = lookup_explicit_specifier (t);
13855 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13856 /*function_p=*/false,
13857 /*i_c_e_p=*/true);
13858 spec = build_explicit_specifier (spec, complain);
13859 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13860 }
13861
13862 /* OpenMP UDRs have the only argument a reference to the declared
13863 type. We want to diagnose if the declared type is a reference,
13864 which is invalid, but as references to references are usually
13865 quietly merged, diagnose it here. */
13866 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13867 {
13868 tree argtype
13869 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13870 argtype = tsubst (argtype, args, complain, in_decl);
13871 if (TYPE_REF_P (argtype))
13872 error_at (DECL_SOURCE_LOCATION (t),
13873 "reference type %qT in "
13874 "%<#pragma omp declare reduction%>", argtype);
13875 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13876 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13877 argtype);
13878 }
13879
13880 if (member && DECL_CONV_FN_P (r))
13881 /* Type-conversion operator. Reconstruct the name, in
13882 case it's the name of one of the template's parameters. */
13883 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13884
13885 tree parms = DECL_ARGUMENTS (t);
13886 if (closure)
13887 parms = DECL_CHAIN (parms);
13888 parms = tsubst (parms, args, complain, t);
13889 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13890 DECL_CONTEXT (parm) = r;
13891 if (closure)
13892 {
13893 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13894 DECL_NAME (tparm) = closure_identifier;
13895 DECL_CHAIN (tparm) = parms;
13896 parms = tparm;
13897 }
13898 DECL_ARGUMENTS (r) = parms;
13899 DECL_RESULT (r) = NULL_TREE;
13900
13901 maybe_rebuild_function_decl_type (r);
13902
13903 TREE_STATIC (r) = 0;
13904 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13905 DECL_EXTERNAL (r) = 1;
13906 /* If this is an instantiation of a function with internal
13907 linkage, we already know what object file linkage will be
13908 assigned to the instantiation. */
13909 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13910 DECL_DEFER_OUTPUT (r) = 0;
13911 DECL_CHAIN (r) = NULL_TREE;
13912 DECL_PENDING_INLINE_INFO (r) = 0;
13913 DECL_PENDING_INLINE_P (r) = 0;
13914 DECL_SAVED_TREE (r) = NULL_TREE;
13915 DECL_STRUCT_FUNCTION (r) = NULL;
13916 TREE_USED (r) = 0;
13917 /* We'll re-clone as appropriate in instantiate_template. */
13918 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13919
13920 /* If we aren't complaining now, return on error before we register
13921 the specialization so that we'll complain eventually. */
13922 if ((complain & tf_error) == 0
13923 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13924 && !grok_op_properties (r, /*complain=*/false))
13925 return error_mark_node;
13926
13927 /* Associate the constraints directly with the instantiation. We
13928 don't substitute through the constraints; that's only done when
13929 they are checked. */
13930 if (tree ci = get_constraints (t))
13931 /* Unless we're regenerating a lambda, in which case we'll set the
13932 lambda's constraints in tsubst_lambda_expr. */
13933 if (!lambda_fntype)
13934 set_constraints (r, ci);
13935
13936 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13937 SET_DECL_FRIEND_CONTEXT (r,
13938 tsubst (DECL_FRIEND_CONTEXT (t),
13939 args, complain, in_decl));
13940
13941 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13942 this in the special friend case mentioned above where
13943 GEN_TMPL is NULL. */
13944 if (gen_tmpl && !closure)
13945 {
13946 DECL_TEMPLATE_INFO (r)
13947 = build_template_info (gen_tmpl, argvec);
13948 SET_DECL_IMPLICIT_INSTANTIATION (r);
13949
13950 tree new_r
13951 = register_specialization (r, gen_tmpl, argvec, false, hash);
13952 if (new_r != r)
13953 /* We instantiated this while substituting into
13954 the type earlier (template/friend54.C). */
13955 return new_r;
13956
13957 /* We're not supposed to instantiate default arguments
13958 until they are called, for a template. But, for a
13959 declaration like:
13960
13961 template <class T> void f ()
13962 { extern void g(int i = T()); }
13963
13964 we should do the substitution when the template is
13965 instantiated. We handle the member function case in
13966 instantiate_class_template since the default arguments
13967 might refer to other members of the class. */
13968 if (!member
13969 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13970 && !uses_template_parms (argvec))
13971 tsubst_default_arguments (r, complain);
13972 }
13973 else if (DECL_LOCAL_DECL_P (r)
13974 && !DECL_OMP_DECLARE_REDUCTION_P (r))
13975 {
13976 if (!cp_unevaluated_operand)
13977 register_local_specialization (r, t);
13978 }
13979 else
13980 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13981
13982 /* Copy the list of befriending classes. */
13983 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13984 *friends;
13985 friends = &TREE_CHAIN (*friends))
13986 {
13987 *friends = copy_node (*friends);
13988 TREE_VALUE (*friends)
13989 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13990 }
13991
13992 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13993 {
13994 maybe_retrofit_in_chrg (r);
13995 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13996 return error_mark_node;
13997 /* If this is an instantiation of a member template, clone it.
13998 If it isn't, that'll be handled by
13999 clone_constructors_and_destructors. */
14000 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14001 clone_cdtor (r, /*update_methods=*/false);
14002 }
14003 else if ((complain & tf_error) != 0
14004 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14005 && !grok_op_properties (r, /*complain=*/true))
14006 return error_mark_node;
14007
14008 /* Possibly limit visibility based on template args. */
14009 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14010 if (DECL_VISIBILITY_SPECIFIED (t))
14011 {
14012 DECL_VISIBILITY_SPECIFIED (r) = 0;
14013 DECL_ATTRIBUTES (r)
14014 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14015 }
14016 determine_visibility (r);
14017 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14018 && !processing_template_decl)
14019 defaulted_late_check (r);
14020
14021 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14022 args, complain, in_decl);
14023 if (flag_openmp)
14024 if (tree attr = lookup_attribute ("omp declare variant base",
14025 DECL_ATTRIBUTES (r)))
14026 omp_declare_variant_finalize (r, attr);
14027
14028 return r;
14029 }
14030
14031 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14032
14033 static tree
14034 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14035 tree lambda_fntype)
14036 {
14037 /* We can get here when processing a member function template,
14038 member class template, or template template parameter. */
14039 tree decl = DECL_TEMPLATE_RESULT (t);
14040 tree in_decl = t;
14041 tree spec;
14042 tree tmpl_args;
14043 tree full_args;
14044 tree r;
14045 hashval_t hash = 0;
14046
14047 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14048 {
14049 /* Template template parameter is treated here. */
14050 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14051 if (new_type == error_mark_node)
14052 r = error_mark_node;
14053 /* If we get a real template back, return it. This can happen in
14054 the context of most_specialized_partial_spec. */
14055 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14056 r = new_type;
14057 else
14058 /* The new TEMPLATE_DECL was built in
14059 reduce_template_parm_level. */
14060 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14061 return r;
14062 }
14063
14064 if (!lambda_fntype)
14065 {
14066 /* We might already have an instance of this template.
14067 The ARGS are for the surrounding class type, so the
14068 full args contain the tsubst'd args for the context,
14069 plus the innermost args from the template decl. */
14070 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14071 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14072 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14073 /* Because this is a template, the arguments will still be
14074 dependent, even after substitution. If
14075 PROCESSING_TEMPLATE_DECL is not set, the dependency
14076 predicates will short-circuit. */
14077 ++processing_template_decl;
14078 full_args = tsubst_template_args (tmpl_args, args,
14079 complain, in_decl);
14080 --processing_template_decl;
14081 if (full_args == error_mark_node)
14082 return error_mark_node;
14083
14084 /* If this is a default template template argument,
14085 tsubst might not have changed anything. */
14086 if (full_args == tmpl_args)
14087 return t;
14088
14089 hash = hash_tmpl_and_args (t, full_args);
14090 spec = retrieve_specialization (t, full_args, hash);
14091 if (spec != NULL_TREE)
14092 {
14093 if (TYPE_P (spec))
14094 /* Type partial instantiations are stored as the type by
14095 lookup_template_class_1, not here as the template. */
14096 spec = CLASSTYPE_TI_TEMPLATE (spec);
14097 return spec;
14098 }
14099 }
14100
14101 /* Make a new template decl. It will be similar to the
14102 original, but will record the current template arguments.
14103 We also create a new function declaration, which is just
14104 like the old one, but points to this new template, rather
14105 than the old one. */
14106 r = copy_decl (t);
14107 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14108 DECL_CHAIN (r) = NULL_TREE;
14109
14110 // Build new template info linking to the original template decl.
14111 if (!lambda_fntype)
14112 {
14113 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14114 SET_DECL_IMPLICIT_INSTANTIATION (r);
14115 }
14116 else
14117 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14118
14119 /* The template parameters for this new template are all the
14120 template parameters for the old template, except the
14121 outermost level of parameters. */
14122 DECL_TEMPLATE_PARMS (r)
14123 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14124 complain);
14125
14126 bool class_p = false;
14127 tree inner = decl;
14128 ++processing_template_decl;
14129 if (TREE_CODE (inner) == FUNCTION_DECL)
14130 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14131 else
14132 {
14133 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14134 {
14135 class_p = true;
14136 inner = TREE_TYPE (inner);
14137 }
14138 inner = tsubst (inner, args, complain, in_decl);
14139 }
14140 --processing_template_decl;
14141 if (inner == error_mark_node)
14142 return error_mark_node;
14143
14144 if (class_p)
14145 {
14146 /* For a partial specialization, we need to keep pointing to
14147 the primary template. */
14148 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14149 CLASSTYPE_TI_TEMPLATE (inner) = r;
14150
14151 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14152 inner = TYPE_MAIN_DECL (inner);
14153 }
14154 else if (lambda_fntype)
14155 {
14156 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14157 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14158 }
14159 else
14160 {
14161 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14162 DECL_TI_TEMPLATE (inner) = r;
14163 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14164 }
14165
14166 DECL_TEMPLATE_RESULT (r) = inner;
14167 TREE_TYPE (r) = TREE_TYPE (inner);
14168 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14169
14170 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14171 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14172
14173 if (PRIMARY_TEMPLATE_P (t))
14174 DECL_PRIMARY_TEMPLATE (r) = r;
14175
14176 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14177 && !lambda_fntype)
14178 /* Record this non-type partial instantiation. */
14179 register_specialization (r, t,
14180 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14181 false, hash);
14182
14183 return r;
14184 }
14185
14186 /* True if FN is the op() for a lambda in an uninstantiated template. */
14187
14188 bool
14189 lambda_fn_in_template_p (tree fn)
14190 {
14191 if (!fn || !LAMBDA_FUNCTION_P (fn))
14192 return false;
14193 tree closure = DECL_CONTEXT (fn);
14194 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14195 }
14196
14197 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14198 which the above is true. */
14199
14200 bool
14201 instantiated_lambda_fn_p (tree fn)
14202 {
14203 if (!fn || !LAMBDA_FUNCTION_P (fn))
14204 return false;
14205 tree closure = DECL_CONTEXT (fn);
14206 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14207 return LAMBDA_EXPR_INSTANTIATED (lam);
14208 }
14209
14210 /* We're instantiating a variable from template function TCTX. Return the
14211 corresponding current enclosing scope. This gets complicated because lambda
14212 functions in templates are regenerated rather than instantiated, but generic
14213 lambda functions are subsequently instantiated. */
14214
14215 static tree
14216 enclosing_instantiation_of (tree otctx)
14217 {
14218 tree tctx = otctx;
14219 tree fn = current_function_decl;
14220 int lambda_count = 0;
14221
14222 for (; tctx && (lambda_fn_in_template_p (tctx)
14223 || instantiated_lambda_fn_p (tctx));
14224 tctx = decl_function_context (tctx))
14225 ++lambda_count;
14226 for (; fn; fn = decl_function_context (fn))
14227 {
14228 tree ofn = fn;
14229 int flambda_count = 0;
14230 for (; fn && instantiated_lambda_fn_p (fn);
14231 fn = decl_function_context (fn))
14232 ++flambda_count;
14233 if ((fn && DECL_TEMPLATE_INFO (fn))
14234 ? most_general_template (fn) != most_general_template (tctx)
14235 : fn != tctx)
14236 continue;
14237 if (flambda_count != lambda_count)
14238 {
14239 gcc_assert (flambda_count > lambda_count);
14240 for (; flambda_count > lambda_count; --flambda_count)
14241 ofn = decl_function_context (ofn);
14242 }
14243 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14244 || DECL_CONV_FN_P (ofn));
14245 return ofn;
14246 }
14247 gcc_unreachable ();
14248 }
14249
14250 /* Substitute the ARGS into the T, which is a _DECL. Return the
14251 result of the substitution. Issue error and warning messages under
14252 control of COMPLAIN. */
14253
14254 static tree
14255 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14256 {
14257 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14258 location_t saved_loc;
14259 tree r = NULL_TREE;
14260 tree in_decl = t;
14261 hashval_t hash = 0;
14262
14263 /* Set the filename and linenumber to improve error-reporting. */
14264 saved_loc = input_location;
14265 input_location = DECL_SOURCE_LOCATION (t);
14266
14267 switch (TREE_CODE (t))
14268 {
14269 case TEMPLATE_DECL:
14270 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14271 break;
14272
14273 case FUNCTION_DECL:
14274 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14275 break;
14276
14277 case PARM_DECL:
14278 {
14279 tree type = NULL_TREE;
14280 int i, len = 1;
14281 tree expanded_types = NULL_TREE;
14282 tree prev_r = NULL_TREE;
14283 tree first_r = NULL_TREE;
14284
14285 if (DECL_PACK_P (t))
14286 {
14287 /* If there is a local specialization that isn't a
14288 parameter pack, it means that we're doing a "simple"
14289 substitution from inside tsubst_pack_expansion. Just
14290 return the local specialization (which will be a single
14291 parm). */
14292 tree spec = retrieve_local_specialization (t);
14293 if (spec
14294 && TREE_CODE (spec) == PARM_DECL
14295 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14296 RETURN (spec);
14297
14298 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14299 the parameters in this function parameter pack. */
14300 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14301 complain, in_decl);
14302 if (TREE_CODE (expanded_types) == TREE_VEC)
14303 {
14304 len = TREE_VEC_LENGTH (expanded_types);
14305
14306 /* Zero-length parameter packs are boring. Just substitute
14307 into the chain. */
14308 if (len == 0 && !cp_unevaluated_operand)
14309 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14310 TREE_CHAIN (t)));
14311 }
14312 else
14313 {
14314 /* All we did was update the type. Make a note of that. */
14315 type = expanded_types;
14316 expanded_types = NULL_TREE;
14317 }
14318 }
14319
14320 /* Loop through all of the parameters we'll build. When T is
14321 a function parameter pack, LEN is the number of expanded
14322 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14323 r = NULL_TREE;
14324 for (i = 0; i < len; ++i)
14325 {
14326 prev_r = r;
14327 r = copy_node (t);
14328 if (DECL_TEMPLATE_PARM_P (t))
14329 SET_DECL_TEMPLATE_PARM_P (r);
14330
14331 if (expanded_types)
14332 /* We're on the Ith parameter of the function parameter
14333 pack. */
14334 {
14335 /* Get the Ith type. */
14336 type = TREE_VEC_ELT (expanded_types, i);
14337
14338 /* Rename the parameter to include the index. */
14339 DECL_NAME (r)
14340 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14341 }
14342 else if (!type)
14343 /* We're dealing with a normal parameter. */
14344 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14345
14346 type = type_decays_to (type);
14347 TREE_TYPE (r) = type;
14348 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14349
14350 if (DECL_INITIAL (r))
14351 {
14352 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14353 DECL_INITIAL (r) = TREE_TYPE (r);
14354 else
14355 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14356 complain, in_decl);
14357 }
14358
14359 DECL_CONTEXT (r) = NULL_TREE;
14360
14361 if (!DECL_TEMPLATE_PARM_P (r))
14362 DECL_ARG_TYPE (r) = type_passed_as (type);
14363
14364 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14365 args, complain, in_decl);
14366
14367 /* Keep track of the first new parameter we
14368 generate. That's what will be returned to the
14369 caller. */
14370 if (!first_r)
14371 first_r = r;
14372
14373 /* Build a proper chain of parameters when substituting
14374 into a function parameter pack. */
14375 if (prev_r)
14376 DECL_CHAIN (prev_r) = r;
14377 }
14378
14379 /* If cp_unevaluated_operand is set, we're just looking for a
14380 single dummy parameter, so don't keep going. */
14381 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14382 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14383 complain, DECL_CHAIN (t));
14384
14385 /* FIRST_R contains the start of the chain we've built. */
14386 r = first_r;
14387 }
14388 break;
14389
14390 case FIELD_DECL:
14391 {
14392 tree type = NULL_TREE;
14393 tree vec = NULL_TREE;
14394 tree expanded_types = NULL_TREE;
14395 int len = 1;
14396
14397 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14398 {
14399 /* This field is a lambda capture pack. Return a TREE_VEC of
14400 the expanded fields to instantiate_class_template_1. */
14401 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14402 complain, in_decl);
14403 if (TREE_CODE (expanded_types) == TREE_VEC)
14404 {
14405 len = TREE_VEC_LENGTH (expanded_types);
14406 vec = make_tree_vec (len);
14407 }
14408 else
14409 {
14410 /* All we did was update the type. Make a note of that. */
14411 type = expanded_types;
14412 expanded_types = NULL_TREE;
14413 }
14414 }
14415
14416 for (int i = 0; i < len; ++i)
14417 {
14418 r = copy_decl (t);
14419 if (expanded_types)
14420 {
14421 type = TREE_VEC_ELT (expanded_types, i);
14422 DECL_NAME (r)
14423 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14424 }
14425 else if (!type)
14426 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14427
14428 if (type == error_mark_node)
14429 RETURN (error_mark_node);
14430 TREE_TYPE (r) = type;
14431 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14432
14433 if (DECL_C_BIT_FIELD (r))
14434 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14435 number of bits. */
14436 DECL_BIT_FIELD_REPRESENTATIVE (r)
14437 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14438 complain, in_decl,
14439 /*integral_constant_expression_p=*/true);
14440 if (DECL_INITIAL (t))
14441 {
14442 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14443 NSDMI in perform_member_init. Still set DECL_INITIAL
14444 so that we know there is one. */
14445 DECL_INITIAL (r) = void_node;
14446 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14447 retrofit_lang_decl (r);
14448 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14449 }
14450 /* We don't have to set DECL_CONTEXT here; it is set by
14451 finish_member_declaration. */
14452 DECL_CHAIN (r) = NULL_TREE;
14453
14454 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14455 args, complain, in_decl);
14456
14457 if (vec)
14458 TREE_VEC_ELT (vec, i) = r;
14459 }
14460
14461 if (vec)
14462 r = vec;
14463 }
14464 break;
14465
14466 case USING_DECL:
14467 /* We reach here only for member using decls. We also need to check
14468 uses_template_parms because DECL_DEPENDENT_P is not set for a
14469 using-declaration that designates a member of the current
14470 instantiation (c++/53549). */
14471 if (DECL_DEPENDENT_P (t)
14472 || uses_template_parms (USING_DECL_SCOPE (t)))
14473 {
14474 tree scope = USING_DECL_SCOPE (t);
14475 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14476 if (PACK_EXPANSION_P (scope))
14477 {
14478 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14479 int len = TREE_VEC_LENGTH (vec);
14480 r = make_tree_vec (len);
14481 for (int i = 0; i < len; ++i)
14482 {
14483 tree escope = TREE_VEC_ELT (vec, i);
14484 tree elt = do_class_using_decl (escope, name);
14485 if (!elt)
14486 {
14487 r = error_mark_node;
14488 break;
14489 }
14490 else
14491 {
14492 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14493 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14494 }
14495 TREE_VEC_ELT (r, i) = elt;
14496 }
14497 }
14498 else
14499 {
14500 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14501 complain, in_decl);
14502 r = do_class_using_decl (inst_scope, name);
14503 if (!r)
14504 r = error_mark_node;
14505 else
14506 {
14507 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14508 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14509 }
14510 }
14511 }
14512 else
14513 {
14514 r = copy_node (t);
14515 DECL_CHAIN (r) = NULL_TREE;
14516 }
14517 break;
14518
14519 case TYPE_DECL:
14520 case VAR_DECL:
14521 {
14522 tree argvec = NULL_TREE;
14523 tree gen_tmpl = NULL_TREE;
14524 tree tmpl = NULL_TREE;
14525 tree type = NULL_TREE;
14526
14527 if (TREE_TYPE (t) == error_mark_node)
14528 RETURN (error_mark_node);
14529
14530 if (TREE_CODE (t) == TYPE_DECL
14531 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14532 {
14533 /* If this is the canonical decl, we don't have to
14534 mess with instantiations, and often we can't (for
14535 typename, template type parms and such). Note that
14536 TYPE_NAME is not correct for the above test if
14537 we've copied the type for a typedef. */
14538 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14539 if (type == error_mark_node)
14540 RETURN (error_mark_node);
14541 r = TYPE_NAME (type);
14542 break;
14543 }
14544
14545 /* Check to see if we already have the specialization we
14546 need. */
14547 tree spec = NULL_TREE;
14548 bool local_p = false;
14549 tree ctx = DECL_CONTEXT (t);
14550 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14551 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14552 {
14553 local_p = false;
14554 if (DECL_CLASS_SCOPE_P (t))
14555 {
14556 ctx = tsubst_aggr_type (ctx, args,
14557 complain,
14558 in_decl, /*entering_scope=*/1);
14559 /* If CTX is unchanged, then T is in fact the
14560 specialization we want. That situation occurs when
14561 referencing a static data member within in its own
14562 class. We can use pointer equality, rather than
14563 same_type_p, because DECL_CONTEXT is always
14564 canonical... */
14565 if (ctx == DECL_CONTEXT (t)
14566 /* ... unless T is a member template; in which
14567 case our caller can be willing to create a
14568 specialization of that template represented
14569 by T. */
14570 && !(DECL_TI_TEMPLATE (t)
14571 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14572 spec = t;
14573 }
14574
14575 if (!spec)
14576 {
14577 tmpl = DECL_TI_TEMPLATE (t);
14578 gen_tmpl = most_general_template (tmpl);
14579 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14580 if (argvec != error_mark_node)
14581 argvec = (coerce_innermost_template_parms
14582 (DECL_TEMPLATE_PARMS (gen_tmpl),
14583 argvec, t, complain,
14584 /*all*/true, /*defarg*/true));
14585 if (argvec == error_mark_node)
14586 RETURN (error_mark_node);
14587 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14588 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14589 }
14590 }
14591 else
14592 {
14593 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14594 /* Subsequent calls to pushdecl will fill this in. */
14595 ctx = NULL_TREE;
14596 /* A local variable. */
14597 local_p = true;
14598 /* Unless this is a reference to a static variable from an
14599 enclosing function, in which case we need to fill it in now. */
14600 if (TREE_STATIC (t))
14601 {
14602 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14603 if (fn != current_function_decl)
14604 ctx = fn;
14605 }
14606 spec = retrieve_local_specialization (t);
14607 }
14608 /* If we already have the specialization we need, there is
14609 nothing more to do. */
14610 if (spec)
14611 {
14612 r = spec;
14613 break;
14614 }
14615
14616 /* Create a new node for the specialization we need. */
14617 if (type == NULL_TREE)
14618 {
14619 if (is_typedef_decl (t))
14620 type = DECL_ORIGINAL_TYPE (t);
14621 else
14622 type = TREE_TYPE (t);
14623 if (VAR_P (t)
14624 && VAR_HAD_UNKNOWN_BOUND (t)
14625 && type != error_mark_node)
14626 type = strip_array_domain (type);
14627 tree sub_args = args;
14628 if (tree auto_node = type_uses_auto (type))
14629 {
14630 /* Mask off any template args past the variable's context so we
14631 don't replace the auto with an unrelated argument. */
14632 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14633 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14634 if (extra > 0)
14635 /* This should never happen with the new lambda instantiation
14636 model, but keep the handling just in case. */
14637 gcc_assert (!CHECKING_P),
14638 sub_args = strip_innermost_template_args (args, extra);
14639 }
14640 type = tsubst (type, sub_args, complain, in_decl);
14641 /* Substituting the type might have recursively instantiated this
14642 same alias (c++/86171). */
14643 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14644 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14645 {
14646 r = spec;
14647 break;
14648 }
14649 }
14650 r = copy_decl (t);
14651 if (VAR_P (r))
14652 {
14653 DECL_INITIALIZED_P (r) = 0;
14654 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14655 if (type == error_mark_node)
14656 RETURN (error_mark_node);
14657 if (TREE_CODE (type) == FUNCTION_TYPE)
14658 {
14659 /* It may seem that this case cannot occur, since:
14660
14661 typedef void f();
14662 void g() { f x; }
14663
14664 declares a function, not a variable. However:
14665
14666 typedef void f();
14667 template <typename T> void g() { T t; }
14668 template void g<f>();
14669
14670 is an attempt to declare a variable with function
14671 type. */
14672 error ("variable %qD has function type",
14673 /* R is not yet sufficiently initialized, so we
14674 just use its name. */
14675 DECL_NAME (r));
14676 RETURN (error_mark_node);
14677 }
14678 type = complete_type (type);
14679 /* Wait until cp_finish_decl to set this again, to handle
14680 circular dependency (template/instantiate6.C). */
14681 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14682 type = check_var_type (DECL_NAME (r), type,
14683 DECL_SOURCE_LOCATION (r));
14684 if (DECL_HAS_VALUE_EXPR_P (t))
14685 {
14686 tree ve = DECL_VALUE_EXPR (t);
14687 /* If the DECL_VALUE_EXPR is converted to the declared type,
14688 preserve the identity so that gimplify_type_sizes works. */
14689 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14690 if (nop)
14691 ve = TREE_OPERAND (ve, 0);
14692 ve = tsubst_expr (ve, args, complain, in_decl,
14693 /*constant_expression_p=*/false);
14694 if (REFERENCE_REF_P (ve))
14695 {
14696 gcc_assert (TYPE_REF_P (type));
14697 ve = TREE_OPERAND (ve, 0);
14698 }
14699 if (nop)
14700 ve = build_nop (type, ve);
14701 else if (DECL_LANG_SPECIFIC (t)
14702 && DECL_OMP_PRIVATIZED_MEMBER (t)
14703 && TREE_CODE (ve) == COMPONENT_REF
14704 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14705 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14706 type = TREE_TYPE (ve);
14707 else
14708 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14709 == TYPE_MAIN_VARIANT (type));
14710 SET_DECL_VALUE_EXPR (r, ve);
14711 }
14712 if (CP_DECL_THREAD_LOCAL_P (r)
14713 && !processing_template_decl)
14714 set_decl_tls_model (r, decl_default_tls_model (r));
14715 }
14716 else if (DECL_SELF_REFERENCE_P (t))
14717 SET_DECL_SELF_REFERENCE_P (r);
14718 TREE_TYPE (r) = type;
14719 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14720 DECL_CONTEXT (r) = ctx;
14721 /* Clear out the mangled name and RTL for the instantiation. */
14722 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14723 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14724 SET_DECL_RTL (r, NULL);
14725 /* The initializer must not be expanded until it is required;
14726 see [temp.inst]. */
14727 DECL_INITIAL (r) = NULL_TREE;
14728 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14729 if (VAR_P (r))
14730 {
14731 if (DECL_LANG_SPECIFIC (r))
14732 SET_DECL_DEPENDENT_INIT_P (r, false);
14733
14734 SET_DECL_MODE (r, VOIDmode);
14735
14736 /* Possibly limit visibility based on template args. */
14737 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14738 if (DECL_VISIBILITY_SPECIFIED (t))
14739 {
14740 DECL_VISIBILITY_SPECIFIED (r) = 0;
14741 DECL_ATTRIBUTES (r)
14742 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14743 }
14744 determine_visibility (r);
14745 }
14746
14747 if (!local_p)
14748 {
14749 /* A static data member declaration is always marked
14750 external when it is declared in-class, even if an
14751 initializer is present. We mimic the non-template
14752 processing here. */
14753 DECL_EXTERNAL (r) = 1;
14754 if (DECL_NAMESPACE_SCOPE_P (t))
14755 DECL_NOT_REALLY_EXTERN (r) = 1;
14756
14757 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14758 SET_DECL_IMPLICIT_INSTANTIATION (r);
14759 if (!error_operand_p (r) || (complain & tf_error))
14760 register_specialization (r, gen_tmpl, argvec, false, hash);
14761 }
14762 else
14763 {
14764 if (DECL_LANG_SPECIFIC (r))
14765 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14766 if (!cp_unevaluated_operand)
14767 register_local_specialization (r, t);
14768 }
14769
14770 DECL_CHAIN (r) = NULL_TREE;
14771
14772 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14773 /*flags=*/0,
14774 args, complain, in_decl);
14775
14776 /* Preserve a typedef that names a type. */
14777 if (is_typedef_decl (r) && type != error_mark_node)
14778 {
14779 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14780 set_underlying_type (r);
14781 if (TYPE_DECL_ALIAS_P (r))
14782 /* An alias template specialization can be dependent
14783 even if its underlying type is not. */
14784 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14785 }
14786
14787 layout_decl (r, 0);
14788 }
14789 break;
14790
14791 default:
14792 gcc_unreachable ();
14793 }
14794 #undef RETURN
14795
14796 out:
14797 /* Restore the file and line information. */
14798 input_location = saved_loc;
14799
14800 return r;
14801 }
14802
14803 /* Substitute into the complete parameter type list PARMS. */
14804
14805 tree
14806 tsubst_function_parms (tree parms,
14807 tree args,
14808 tsubst_flags_t complain,
14809 tree in_decl)
14810 {
14811 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14812 }
14813
14814 /* Substitute into the ARG_TYPES of a function type.
14815 If END is a TREE_CHAIN, leave it and any following types
14816 un-substituted. */
14817
14818 static tree
14819 tsubst_arg_types (tree arg_types,
14820 tree args,
14821 tree end,
14822 tsubst_flags_t complain,
14823 tree in_decl)
14824 {
14825 tree remaining_arg_types;
14826 tree type = NULL_TREE;
14827 int i = 1;
14828 tree expanded_args = NULL_TREE;
14829 tree default_arg;
14830
14831 if (!arg_types || arg_types == void_list_node || arg_types == end)
14832 return arg_types;
14833
14834 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14835 args, end, complain, in_decl);
14836 if (remaining_arg_types == error_mark_node)
14837 return error_mark_node;
14838
14839 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14840 {
14841 /* For a pack expansion, perform substitution on the
14842 entire expression. Later on, we'll handle the arguments
14843 one-by-one. */
14844 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14845 args, complain, in_decl);
14846
14847 if (TREE_CODE (expanded_args) == TREE_VEC)
14848 /* So that we'll spin through the parameters, one by one. */
14849 i = TREE_VEC_LENGTH (expanded_args);
14850 else
14851 {
14852 /* We only partially substituted into the parameter
14853 pack. Our type is TYPE_PACK_EXPANSION. */
14854 type = expanded_args;
14855 expanded_args = NULL_TREE;
14856 }
14857 }
14858
14859 while (i > 0) {
14860 --i;
14861
14862 if (expanded_args)
14863 type = TREE_VEC_ELT (expanded_args, i);
14864 else if (!type)
14865 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14866
14867 if (type == error_mark_node)
14868 return error_mark_node;
14869 if (VOID_TYPE_P (type))
14870 {
14871 if (complain & tf_error)
14872 {
14873 error ("invalid parameter type %qT", type);
14874 if (in_decl)
14875 error ("in declaration %q+D", in_decl);
14876 }
14877 return error_mark_node;
14878 }
14879 /* DR 657. */
14880 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14881 return error_mark_node;
14882
14883 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14884 top-level qualifiers as required. */
14885 type = cv_unqualified (type_decays_to (type));
14886
14887 /* We do not substitute into default arguments here. The standard
14888 mandates that they be instantiated only when needed, which is
14889 done in build_over_call. */
14890 default_arg = TREE_PURPOSE (arg_types);
14891
14892 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14893 since the new op() won't have any associated template arguments for us
14894 to refer to later. */
14895 if (lambda_fn_in_template_p (in_decl))
14896 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14897 false/*fn*/, false/*constexpr*/);
14898
14899 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14900 {
14901 /* We've instantiated a template before its default arguments
14902 have been parsed. This can happen for a nested template
14903 class, and is not an error unless we require the default
14904 argument in a call of this function. */
14905 remaining_arg_types =
14906 tree_cons (default_arg, type, remaining_arg_types);
14907 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14908 remaining_arg_types);
14909 }
14910 else
14911 remaining_arg_types =
14912 hash_tree_cons (default_arg, type, remaining_arg_types);
14913 }
14914
14915 return remaining_arg_types;
14916 }
14917
14918 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14919 *not* handle the exception-specification for FNTYPE, because the
14920 initial substitution of explicitly provided template parameters
14921 during argument deduction forbids substitution into the
14922 exception-specification:
14923
14924 [temp.deduct]
14925
14926 All references in the function type of the function template to the
14927 corresponding template parameters are replaced by the specified tem-
14928 plate argument values. If a substitution in a template parameter or
14929 in the function type of the function template results in an invalid
14930 type, type deduction fails. [Note: The equivalent substitution in
14931 exception specifications is done only when the function is instanti-
14932 ated, at which point a program is ill-formed if the substitution
14933 results in an invalid type.] */
14934
14935 static tree
14936 tsubst_function_type (tree t,
14937 tree args,
14938 tsubst_flags_t complain,
14939 tree in_decl)
14940 {
14941 tree return_type;
14942 tree arg_types = NULL_TREE;
14943
14944 /* The TYPE_CONTEXT is not used for function/method types. */
14945 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14946
14947 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14948 failure. */
14949 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14950
14951 if (late_return_type_p)
14952 {
14953 /* Substitute the argument types. */
14954 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14955 complain, in_decl);
14956 if (arg_types == error_mark_node)
14957 return error_mark_node;
14958
14959 tree save_ccp = current_class_ptr;
14960 tree save_ccr = current_class_ref;
14961 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14962 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14963 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14964 if (do_inject)
14965 {
14966 /* DR 1207: 'this' is in scope in the trailing return type. */
14967 inject_this_parameter (this_type, cp_type_quals (this_type));
14968 }
14969
14970 /* Substitute the return type. */
14971 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14972
14973 if (do_inject)
14974 {
14975 current_class_ptr = save_ccp;
14976 current_class_ref = save_ccr;
14977 }
14978 }
14979 else
14980 /* Substitute the return type. */
14981 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14982
14983 if (return_type == error_mark_node)
14984 return error_mark_node;
14985 /* DR 486 clarifies that creation of a function type with an
14986 invalid return type is a deduction failure. */
14987 if (TREE_CODE (return_type) == ARRAY_TYPE
14988 || TREE_CODE (return_type) == FUNCTION_TYPE)
14989 {
14990 if (complain & tf_error)
14991 {
14992 if (TREE_CODE (return_type) == ARRAY_TYPE)
14993 error ("function returning an array");
14994 else
14995 error ("function returning a function");
14996 }
14997 return error_mark_node;
14998 }
14999 /* And DR 657. */
15000 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
15001 return error_mark_node;
15002
15003 if (!late_return_type_p)
15004 {
15005 /* Substitute the argument types. */
15006 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15007 complain, in_decl);
15008 if (arg_types == error_mark_node)
15009 return error_mark_node;
15010 }
15011
15012 /* Construct a new type node and return it. */
15013 return rebuild_function_or_method_type (t, return_type, arg_types,
15014 /*raises=*/NULL_TREE, complain);
15015 }
15016
15017 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15018 ARGS into that specification, and return the substituted
15019 specification. If there is no specification, return NULL_TREE. */
15020
15021 static tree
15022 tsubst_exception_specification (tree fntype,
15023 tree args,
15024 tsubst_flags_t complain,
15025 tree in_decl,
15026 bool defer_ok)
15027 {
15028 tree specs;
15029 tree new_specs;
15030
15031 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15032 new_specs = NULL_TREE;
15033 if (specs && TREE_PURPOSE (specs))
15034 {
15035 /* A noexcept-specifier. */
15036 tree expr = TREE_PURPOSE (specs);
15037 if (TREE_CODE (expr) == INTEGER_CST)
15038 new_specs = expr;
15039 else if (defer_ok)
15040 {
15041 /* Defer instantiation of noexcept-specifiers to avoid
15042 excessive instantiations (c++/49107). */
15043 new_specs = make_node (DEFERRED_NOEXCEPT);
15044 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15045 {
15046 /* We already partially instantiated this member template,
15047 so combine the new args with the old. */
15048 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15049 = DEFERRED_NOEXCEPT_PATTERN (expr);
15050 DEFERRED_NOEXCEPT_ARGS (new_specs)
15051 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15052 }
15053 else
15054 {
15055 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15056 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15057 }
15058 }
15059 else
15060 {
15061 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15062 {
15063 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15064 args);
15065 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15066 }
15067 new_specs = tsubst_copy_and_build
15068 (expr, args, complain, in_decl, /*function_p=*/false,
15069 /*integral_constant_expression_p=*/true);
15070 }
15071 new_specs = build_noexcept_spec (new_specs, complain);
15072 }
15073 else if (specs)
15074 {
15075 if (! TREE_VALUE (specs))
15076 new_specs = specs;
15077 else
15078 while (specs)
15079 {
15080 tree spec;
15081 int i, len = 1;
15082 tree expanded_specs = NULL_TREE;
15083
15084 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15085 {
15086 /* Expand the pack expansion type. */
15087 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15088 args, complain,
15089 in_decl);
15090
15091 if (expanded_specs == error_mark_node)
15092 return error_mark_node;
15093 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15094 len = TREE_VEC_LENGTH (expanded_specs);
15095 else
15096 {
15097 /* We're substituting into a member template, so
15098 we got a TYPE_PACK_EXPANSION back. Add that
15099 expansion and move on. */
15100 gcc_assert (TREE_CODE (expanded_specs)
15101 == TYPE_PACK_EXPANSION);
15102 new_specs = add_exception_specifier (new_specs,
15103 expanded_specs,
15104 complain);
15105 specs = TREE_CHAIN (specs);
15106 continue;
15107 }
15108 }
15109
15110 for (i = 0; i < len; ++i)
15111 {
15112 if (expanded_specs)
15113 spec = TREE_VEC_ELT (expanded_specs, i);
15114 else
15115 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15116 if (spec == error_mark_node)
15117 return spec;
15118 new_specs = add_exception_specifier (new_specs, spec,
15119 complain);
15120 }
15121
15122 specs = TREE_CHAIN (specs);
15123 }
15124 }
15125 return new_specs;
15126 }
15127
15128 /* Substitute through a TREE_LIST of types or expressions, handling pack
15129 expansions. */
15130
15131 tree
15132 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15133 {
15134 if (t == void_list_node)
15135 return t;
15136
15137 tree purpose = TREE_PURPOSE (t);
15138 tree purposevec = NULL_TREE;
15139 if (!purpose)
15140 ;
15141 else if (PACK_EXPANSION_P (purpose))
15142 {
15143 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15144 if (TREE_CODE (purpose) == TREE_VEC)
15145 purposevec = purpose;
15146 }
15147 else if (TYPE_P (purpose))
15148 purpose = tsubst (purpose, args, complain, in_decl);
15149 else
15150 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15151 if (purpose == error_mark_node || purposevec == error_mark_node)
15152 return error_mark_node;
15153
15154 tree value = TREE_VALUE (t);
15155 tree valuevec = NULL_TREE;
15156 if (!value)
15157 ;
15158 else if (PACK_EXPANSION_P (value))
15159 {
15160 value = tsubst_pack_expansion (value, args, complain, in_decl);
15161 if (TREE_CODE (value) == TREE_VEC)
15162 valuevec = value;
15163 }
15164 else if (TYPE_P (value))
15165 value = tsubst (value, args, complain, in_decl);
15166 else
15167 value = tsubst_copy_and_build (value, args, complain, in_decl);
15168 if (value == error_mark_node || valuevec == error_mark_node)
15169 return error_mark_node;
15170
15171 tree chain = TREE_CHAIN (t);
15172 if (!chain)
15173 ;
15174 else if (TREE_CODE (chain) == TREE_LIST)
15175 chain = tsubst_tree_list (chain, args, complain, in_decl);
15176 else if (TYPE_P (chain))
15177 chain = tsubst (chain, args, complain, in_decl);
15178 else
15179 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15180 if (chain == error_mark_node)
15181 return error_mark_node;
15182
15183 if (purpose == TREE_PURPOSE (t)
15184 && value == TREE_VALUE (t)
15185 && chain == TREE_CHAIN (t))
15186 return t;
15187
15188 int len;
15189 /* Determine the number of arguments. */
15190 if (purposevec)
15191 {
15192 len = TREE_VEC_LENGTH (purposevec);
15193 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15194 }
15195 else if (valuevec)
15196 len = TREE_VEC_LENGTH (valuevec);
15197 else
15198 len = 1;
15199
15200 for (int i = len; i-- > 0; )
15201 {
15202 if (purposevec)
15203 purpose = TREE_VEC_ELT (purposevec, i);
15204 if (valuevec)
15205 value = TREE_VEC_ELT (valuevec, i);
15206
15207 if (value && TYPE_P (value))
15208 chain = hash_tree_cons (purpose, value, chain);
15209 else
15210 chain = tree_cons (purpose, value, chain);
15211 }
15212
15213 return chain;
15214 }
15215
15216 /* Take the tree structure T and replace template parameters used
15217 therein with the argument vector ARGS. IN_DECL is an associated
15218 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15219 Issue error and warning messages under control of COMPLAIN. Note
15220 that we must be relatively non-tolerant of extensions here, in
15221 order to preserve conformance; if we allow substitutions that
15222 should not be allowed, we may allow argument deductions that should
15223 not succeed, and therefore report ambiguous overload situations
15224 where there are none. In theory, we could allow the substitution,
15225 but indicate that it should have failed, and allow our caller to
15226 make sure that the right thing happens, but we don't try to do this
15227 yet.
15228
15229 This function is used for dealing with types, decls and the like;
15230 for expressions, use tsubst_expr or tsubst_copy. */
15231
15232 tree
15233 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15234 {
15235 enum tree_code code;
15236 tree type, r = NULL_TREE;
15237
15238 if (t == NULL_TREE || t == error_mark_node
15239 || t == integer_type_node
15240 || t == void_type_node
15241 || t == char_type_node
15242 || t == unknown_type_node
15243 || TREE_CODE (t) == NAMESPACE_DECL
15244 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15245 return t;
15246
15247 if (DECL_P (t))
15248 return tsubst_decl (t, args, complain);
15249
15250 if (args == NULL_TREE)
15251 return t;
15252
15253 code = TREE_CODE (t);
15254
15255 if (code == IDENTIFIER_NODE)
15256 type = IDENTIFIER_TYPE_VALUE (t);
15257 else
15258 type = TREE_TYPE (t);
15259
15260 gcc_assert (type != unknown_type_node);
15261
15262 /* Reuse typedefs. We need to do this to handle dependent attributes,
15263 such as attribute aligned. */
15264 if (TYPE_P (t)
15265 && typedef_variant_p (t))
15266 {
15267 tree decl = TYPE_NAME (t);
15268
15269 if (alias_template_specialization_p (t, nt_opaque))
15270 {
15271 /* DECL represents an alias template and we want to
15272 instantiate it. */
15273 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15274 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15275 r = instantiate_alias_template (tmpl, gen_args, complain);
15276 }
15277 else if (DECL_CLASS_SCOPE_P (decl)
15278 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15279 && uses_template_parms (DECL_CONTEXT (decl)))
15280 {
15281 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15282 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15283 r = retrieve_specialization (tmpl, gen_args, 0);
15284 }
15285 else if (DECL_FUNCTION_SCOPE_P (decl)
15286 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15287 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15288 r = retrieve_local_specialization (decl);
15289 else
15290 /* The typedef is from a non-template context. */
15291 return t;
15292
15293 if (r)
15294 {
15295 r = TREE_TYPE (r);
15296 r = cp_build_qualified_type_real
15297 (r, cp_type_quals (t) | cp_type_quals (r),
15298 complain | tf_ignore_bad_quals);
15299 return r;
15300 }
15301 else
15302 {
15303 /* We don't have an instantiation yet, so drop the typedef. */
15304 int quals = cp_type_quals (t);
15305 t = DECL_ORIGINAL_TYPE (decl);
15306 t = cp_build_qualified_type_real (t, quals,
15307 complain | tf_ignore_bad_quals);
15308 }
15309 }
15310
15311 bool fndecl_type = (complain & tf_fndecl_type);
15312 complain &= ~tf_fndecl_type;
15313
15314 if (type
15315 && code != TYPENAME_TYPE
15316 && code != TEMPLATE_TYPE_PARM
15317 && code != TEMPLATE_PARM_INDEX
15318 && code != IDENTIFIER_NODE
15319 && code != FUNCTION_TYPE
15320 && code != METHOD_TYPE)
15321 type = tsubst (type, args, complain, in_decl);
15322 if (type == error_mark_node)
15323 return error_mark_node;
15324
15325 switch (code)
15326 {
15327 case RECORD_TYPE:
15328 case UNION_TYPE:
15329 case ENUMERAL_TYPE:
15330 return tsubst_aggr_type (t, args, complain, in_decl,
15331 /*entering_scope=*/0);
15332
15333 case ERROR_MARK:
15334 case IDENTIFIER_NODE:
15335 case VOID_TYPE:
15336 case REAL_TYPE:
15337 case COMPLEX_TYPE:
15338 case VECTOR_TYPE:
15339 case BOOLEAN_TYPE:
15340 case NULLPTR_TYPE:
15341 case LANG_TYPE:
15342 return t;
15343
15344 case INTEGER_TYPE:
15345 if (t == integer_type_node)
15346 return t;
15347
15348 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15349 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15350 return t;
15351
15352 {
15353 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15354
15355 max = tsubst_expr (omax, args, complain, in_decl,
15356 /*integral_constant_expression_p=*/false);
15357
15358 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15359 needed. */
15360 if (TREE_CODE (max) == NOP_EXPR
15361 && TREE_SIDE_EFFECTS (omax)
15362 && !TREE_TYPE (max))
15363 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15364
15365 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15366 with TREE_SIDE_EFFECTS that indicates this is not an integral
15367 constant expression. */
15368 if (processing_template_decl
15369 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15370 {
15371 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15372 TREE_SIDE_EFFECTS (max) = 1;
15373 }
15374
15375 return compute_array_index_type (NULL_TREE, max, complain);
15376 }
15377
15378 case TEMPLATE_TYPE_PARM:
15379 case TEMPLATE_TEMPLATE_PARM:
15380 case BOUND_TEMPLATE_TEMPLATE_PARM:
15381 case TEMPLATE_PARM_INDEX:
15382 {
15383 int idx;
15384 int level;
15385 int levels;
15386 tree arg = NULL_TREE;
15387
15388 r = NULL_TREE;
15389
15390 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15391 template_parm_level_and_index (t, &level, &idx);
15392
15393 levels = TMPL_ARGS_DEPTH (args);
15394 if (level <= levels
15395 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15396 {
15397 arg = TMPL_ARG (args, level, idx);
15398
15399 /* See through ARGUMENT_PACK_SELECT arguments. */
15400 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15401 arg = argument_pack_select_arg (arg);
15402 }
15403
15404 if (arg == error_mark_node)
15405 return error_mark_node;
15406 else if (arg != NULL_TREE)
15407 {
15408 if (ARGUMENT_PACK_P (arg))
15409 /* If ARG is an argument pack, we don't actually want to
15410 perform a substitution here, because substitutions
15411 for argument packs are only done
15412 element-by-element. We can get to this point when
15413 substituting the type of a non-type template
15414 parameter pack, when that type actually contains
15415 template parameter packs from an outer template, e.g.,
15416
15417 template<typename... Types> struct A {
15418 template<Types... Values> struct B { };
15419 }; */
15420 return t;
15421
15422 if (code == TEMPLATE_TYPE_PARM)
15423 {
15424 int quals;
15425
15426 /* When building concept checks for the purpose of
15427 deducing placeholders, we can end up with wildcards
15428 where types are expected. Adjust this to the deduced
15429 value. */
15430 if (TREE_CODE (arg) == WILDCARD_DECL)
15431 arg = TREE_TYPE (TREE_TYPE (arg));
15432
15433 gcc_assert (TYPE_P (arg));
15434
15435 quals = cp_type_quals (arg) | cp_type_quals (t);
15436
15437 return cp_build_qualified_type_real
15438 (arg, quals, complain | tf_ignore_bad_quals);
15439 }
15440 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15441 {
15442 /* We are processing a type constructed from a
15443 template template parameter. */
15444 tree argvec = tsubst (TYPE_TI_ARGS (t),
15445 args, complain, in_decl);
15446 if (argvec == error_mark_node)
15447 return error_mark_node;
15448
15449 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15450 || TREE_CODE (arg) == TEMPLATE_DECL
15451 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15452
15453 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15454 /* Consider this code:
15455
15456 template <template <class> class Template>
15457 struct Internal {
15458 template <class Arg> using Bind = Template<Arg>;
15459 };
15460
15461 template <template <class> class Template, class Arg>
15462 using Instantiate = Template<Arg>; //#0
15463
15464 template <template <class> class Template,
15465 class Argument>
15466 using Bind =
15467 Instantiate<Internal<Template>::template Bind,
15468 Argument>; //#1
15469
15470 When #1 is parsed, the
15471 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15472 parameter `Template' in #0 matches the
15473 UNBOUND_CLASS_TEMPLATE representing the argument
15474 `Internal<Template>::template Bind'; We then want
15475 to assemble the type `Bind<Argument>' that can't
15476 be fully created right now, because
15477 `Internal<Template>' not being complete, the Bind
15478 template cannot be looked up in that context. So
15479 we need to "store" `Bind<Argument>' for later
15480 when the context of Bind becomes complete. Let's
15481 store that in a TYPENAME_TYPE. */
15482 return make_typename_type (TYPE_CONTEXT (arg),
15483 build_nt (TEMPLATE_ID_EXPR,
15484 TYPE_IDENTIFIER (arg),
15485 argvec),
15486 typename_type,
15487 complain);
15488
15489 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15490 are resolving nested-types in the signature of a
15491 member function templates. Otherwise ARG is a
15492 TEMPLATE_DECL and is the real template to be
15493 instantiated. */
15494 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15495 arg = TYPE_NAME (arg);
15496
15497 r = lookup_template_class (arg,
15498 argvec, in_decl,
15499 DECL_CONTEXT (arg),
15500 /*entering_scope=*/0,
15501 complain);
15502 return cp_build_qualified_type_real
15503 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15504 }
15505 else if (code == TEMPLATE_TEMPLATE_PARM)
15506 return arg;
15507 else
15508 /* TEMPLATE_PARM_INDEX. */
15509 return convert_from_reference (unshare_expr (arg));
15510 }
15511
15512 if (level == 1)
15513 /* This can happen during the attempted tsubst'ing in
15514 unify. This means that we don't yet have any information
15515 about the template parameter in question. */
15516 return t;
15517
15518 /* Early in template argument deduction substitution, we don't
15519 want to reduce the level of 'auto', or it will be confused
15520 with a normal template parm in subsequent deduction.
15521 Similarly, don't reduce the level of template parameters to
15522 avoid mismatches when deducing their types. */
15523 if (complain & tf_partial)
15524 return t;
15525
15526 /* If we get here, we must have been looking at a parm for a
15527 more deeply nested template. Make a new version of this
15528 template parameter, but with a lower level. */
15529 switch (code)
15530 {
15531 case TEMPLATE_TYPE_PARM:
15532 case TEMPLATE_TEMPLATE_PARM:
15533 case BOUND_TEMPLATE_TEMPLATE_PARM:
15534 if (cp_type_quals (t))
15535 {
15536 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15537 r = cp_build_qualified_type_real
15538 (r, cp_type_quals (t),
15539 complain | (code == TEMPLATE_TYPE_PARM
15540 ? tf_ignore_bad_quals : 0));
15541 }
15542 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15543 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15544 && (r = (TEMPLATE_PARM_DESCENDANTS
15545 (TEMPLATE_TYPE_PARM_INDEX (t))))
15546 && (r = TREE_TYPE (r))
15547 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15548 /* Break infinite recursion when substituting the constraints
15549 of a constrained placeholder. */;
15550 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15551 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15552 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15553 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15554 r = TEMPLATE_PARM_DESCENDANTS (arg))
15555 && (TEMPLATE_PARM_LEVEL (r)
15556 == TEMPLATE_PARM_LEVEL (arg) - levels))
15557 /* Cache the simple case of lowering a type parameter. */
15558 r = TREE_TYPE (r);
15559 else
15560 {
15561 r = copy_type (t);
15562 TEMPLATE_TYPE_PARM_INDEX (r)
15563 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15564 r, levels, args, complain);
15565 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15566 TYPE_MAIN_VARIANT (r) = r;
15567 TYPE_POINTER_TO (r) = NULL_TREE;
15568 TYPE_REFERENCE_TO (r) = NULL_TREE;
15569
15570 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15571 {
15572 /* Propagate constraints on placeholders since they are
15573 only instantiated during satisfaction. */
15574 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15575 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15576 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15577 {
15578 pl = tsubst_copy (pl, args, complain, in_decl);
15579 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15580 }
15581 }
15582
15583 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15584 /* We have reduced the level of the template
15585 template parameter, but not the levels of its
15586 template parameters, so canonical_type_parameter
15587 will not be able to find the canonical template
15588 template parameter for this level. Thus, we
15589 require structural equality checking to compare
15590 TEMPLATE_TEMPLATE_PARMs. */
15591 SET_TYPE_STRUCTURAL_EQUALITY (r);
15592 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15593 SET_TYPE_STRUCTURAL_EQUALITY (r);
15594 else
15595 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15596
15597 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15598 {
15599 tree tinfo = TYPE_TEMPLATE_INFO (t);
15600 /* We might need to substitute into the types of non-type
15601 template parameters. */
15602 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15603 complain, in_decl);
15604 if (tmpl == error_mark_node)
15605 return error_mark_node;
15606 tree argvec = tsubst (TI_ARGS (tinfo), args,
15607 complain, in_decl);
15608 if (argvec == error_mark_node)
15609 return error_mark_node;
15610
15611 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15612 = build_template_info (tmpl, argvec);
15613 }
15614 }
15615 break;
15616
15617 case TEMPLATE_PARM_INDEX:
15618 /* OK, now substitute the type of the non-type parameter. We
15619 couldn't do it earlier because it might be an auto parameter,
15620 and we wouldn't need to if we had an argument. */
15621 type = tsubst (type, args, complain, in_decl);
15622 if (type == error_mark_node)
15623 return error_mark_node;
15624 r = reduce_template_parm_level (t, type, levels, args, complain);
15625 break;
15626
15627 default:
15628 gcc_unreachable ();
15629 }
15630
15631 return r;
15632 }
15633
15634 case TREE_LIST:
15635 return tsubst_tree_list (t, args, complain, in_decl);
15636
15637 case TREE_BINFO:
15638 /* We should never be tsubsting a binfo. */
15639 gcc_unreachable ();
15640
15641 case TREE_VEC:
15642 /* A vector of template arguments. */
15643 gcc_assert (!type);
15644 return tsubst_template_args (t, args, complain, in_decl);
15645
15646 case POINTER_TYPE:
15647 case REFERENCE_TYPE:
15648 {
15649 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15650 return t;
15651
15652 /* [temp.deduct]
15653
15654 Type deduction may fail for any of the following
15655 reasons:
15656
15657 -- Attempting to create a pointer to reference type.
15658 -- Attempting to create a reference to a reference type or
15659 a reference to void.
15660
15661 Core issue 106 says that creating a reference to a reference
15662 during instantiation is no longer a cause for failure. We
15663 only enforce this check in strict C++98 mode. */
15664 if ((TYPE_REF_P (type)
15665 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15666 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15667 {
15668 static location_t last_loc;
15669
15670 /* We keep track of the last time we issued this error
15671 message to avoid spewing a ton of messages during a
15672 single bad template instantiation. */
15673 if (complain & tf_error
15674 && last_loc != input_location)
15675 {
15676 if (VOID_TYPE_P (type))
15677 error ("forming reference to void");
15678 else if (code == POINTER_TYPE)
15679 error ("forming pointer to reference type %qT", type);
15680 else
15681 error ("forming reference to reference type %qT", type);
15682 last_loc = input_location;
15683 }
15684
15685 return error_mark_node;
15686 }
15687 else if (TREE_CODE (type) == FUNCTION_TYPE
15688 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15689 || type_memfn_rqual (type) != REF_QUAL_NONE))
15690 {
15691 if (complain & tf_error)
15692 {
15693 if (code == POINTER_TYPE)
15694 error ("forming pointer to qualified function type %qT",
15695 type);
15696 else
15697 error ("forming reference to qualified function type %qT",
15698 type);
15699 }
15700 return error_mark_node;
15701 }
15702 else if (code == POINTER_TYPE)
15703 {
15704 r = build_pointer_type (type);
15705 if (TREE_CODE (type) == METHOD_TYPE)
15706 r = build_ptrmemfunc_type (r);
15707 }
15708 else if (TYPE_REF_P (type))
15709 /* In C++0x, during template argument substitution, when there is an
15710 attempt to create a reference to a reference type, reference
15711 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15712
15713 "If a template-argument for a template-parameter T names a type
15714 that is a reference to a type A, an attempt to create the type
15715 'lvalue reference to cv T' creates the type 'lvalue reference to
15716 A,' while an attempt to create the type type rvalue reference to
15717 cv T' creates the type T"
15718 */
15719 r = cp_build_reference_type
15720 (TREE_TYPE (type),
15721 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15722 else
15723 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15724 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15725
15726 if (r != error_mark_node)
15727 /* Will this ever be needed for TYPE_..._TO values? */
15728 layout_type (r);
15729
15730 return r;
15731 }
15732 case OFFSET_TYPE:
15733 {
15734 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15735 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15736 {
15737 /* [temp.deduct]
15738
15739 Type deduction may fail for any of the following
15740 reasons:
15741
15742 -- Attempting to create "pointer to member of T" when T
15743 is not a class type. */
15744 if (complain & tf_error)
15745 error ("creating pointer to member of non-class type %qT", r);
15746 return error_mark_node;
15747 }
15748 if (TYPE_REF_P (type))
15749 {
15750 if (complain & tf_error)
15751 error ("creating pointer to member reference type %qT", type);
15752 return error_mark_node;
15753 }
15754 if (VOID_TYPE_P (type))
15755 {
15756 if (complain & tf_error)
15757 error ("creating pointer to member of type void");
15758 return error_mark_node;
15759 }
15760 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15761 if (TREE_CODE (type) == FUNCTION_TYPE)
15762 {
15763 /* The type of the implicit object parameter gets its
15764 cv-qualifiers from the FUNCTION_TYPE. */
15765 tree memptr;
15766 tree method_type
15767 = build_memfn_type (type, r, type_memfn_quals (type),
15768 type_memfn_rqual (type));
15769 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15770 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15771 complain);
15772 }
15773 else
15774 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15775 cp_type_quals (t),
15776 complain);
15777 }
15778 case FUNCTION_TYPE:
15779 case METHOD_TYPE:
15780 {
15781 tree fntype;
15782 tree specs;
15783 fntype = tsubst_function_type (t, args, complain, in_decl);
15784 if (fntype == error_mark_node)
15785 return error_mark_node;
15786
15787 /* Substitute the exception specification. */
15788 specs = tsubst_exception_specification (t, args, complain, in_decl,
15789 /*defer_ok*/fndecl_type);
15790 if (specs == error_mark_node)
15791 return error_mark_node;
15792 if (specs)
15793 fntype = build_exception_variant (fntype, specs);
15794 return fntype;
15795 }
15796 case ARRAY_TYPE:
15797 {
15798 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15799 if (domain == error_mark_node)
15800 return error_mark_node;
15801
15802 /* As an optimization, we avoid regenerating the array type if
15803 it will obviously be the same as T. */
15804 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15805 return t;
15806
15807 /* These checks should match the ones in create_array_type_for_decl.
15808
15809 [temp.deduct]
15810
15811 The deduction may fail for any of the following reasons:
15812
15813 -- Attempting to create an array with an element type that
15814 is void, a function type, or a reference type, or [DR337]
15815 an abstract class type. */
15816 if (VOID_TYPE_P (type)
15817 || TREE_CODE (type) == FUNCTION_TYPE
15818 || (TREE_CODE (type) == ARRAY_TYPE
15819 && TYPE_DOMAIN (type) == NULL_TREE)
15820 || TYPE_REF_P (type))
15821 {
15822 if (complain & tf_error)
15823 error ("creating array of %qT", type);
15824 return error_mark_node;
15825 }
15826
15827 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15828 return error_mark_node;
15829
15830 r = build_cplus_array_type (type, domain);
15831
15832 if (!valid_array_size_p (input_location, r, in_decl,
15833 (complain & tf_error)))
15834 return error_mark_node;
15835
15836 if (TYPE_USER_ALIGN (t))
15837 {
15838 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15839 TYPE_USER_ALIGN (r) = 1;
15840 }
15841
15842 return r;
15843 }
15844
15845 case TYPENAME_TYPE:
15846 {
15847 tree ctx = TYPE_CONTEXT (t);
15848 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15849 {
15850 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15851 if (ctx == error_mark_node
15852 || TREE_VEC_LENGTH (ctx) > 1)
15853 return error_mark_node;
15854 if (TREE_VEC_LENGTH (ctx) == 0)
15855 {
15856 if (complain & tf_error)
15857 error ("%qD is instantiated for an empty pack",
15858 TYPENAME_TYPE_FULLNAME (t));
15859 return error_mark_node;
15860 }
15861 ctx = TREE_VEC_ELT (ctx, 0);
15862 }
15863 else
15864 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15865 /*entering_scope=*/1);
15866 if (ctx == error_mark_node)
15867 return error_mark_node;
15868
15869 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15870 complain, in_decl);
15871 if (f == error_mark_node)
15872 return error_mark_node;
15873
15874 if (!MAYBE_CLASS_TYPE_P (ctx))
15875 {
15876 if (complain & tf_error)
15877 error ("%qT is not a class, struct, or union type", ctx);
15878 return error_mark_node;
15879 }
15880 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15881 {
15882 /* Normally, make_typename_type does not require that the CTX
15883 have complete type in order to allow things like:
15884
15885 template <class T> struct S { typename S<T>::X Y; };
15886
15887 But, such constructs have already been resolved by this
15888 point, so here CTX really should have complete type, unless
15889 it's a partial instantiation. */
15890 ctx = complete_type (ctx);
15891 if (!COMPLETE_TYPE_P (ctx))
15892 {
15893 if (complain & tf_error)
15894 cxx_incomplete_type_error (NULL_TREE, ctx);
15895 return error_mark_node;
15896 }
15897 }
15898
15899 f = make_typename_type (ctx, f, typename_type,
15900 complain | tf_keep_type_decl);
15901 if (f == error_mark_node)
15902 return f;
15903 if (TREE_CODE (f) == TYPE_DECL)
15904 {
15905 complain |= tf_ignore_bad_quals;
15906 f = TREE_TYPE (f);
15907 }
15908
15909 if (TREE_CODE (f) != TYPENAME_TYPE)
15910 {
15911 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15912 {
15913 if (complain & tf_error)
15914 error ("%qT resolves to %qT, which is not an enumeration type",
15915 t, f);
15916 else
15917 return error_mark_node;
15918 }
15919 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15920 {
15921 if (complain & tf_error)
15922 error ("%qT resolves to %qT, which is not a class type",
15923 t, f);
15924 else
15925 return error_mark_node;
15926 }
15927 }
15928
15929 return cp_build_qualified_type_real
15930 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15931 }
15932
15933 case UNBOUND_CLASS_TEMPLATE:
15934 {
15935 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15936 in_decl, /*entering_scope=*/1);
15937 tree name = TYPE_IDENTIFIER (t);
15938 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15939
15940 if (ctx == error_mark_node || name == error_mark_node)
15941 return error_mark_node;
15942
15943 if (parm_list)
15944 parm_list = tsubst_template_parms (parm_list, args, complain);
15945 return make_unbound_class_template (ctx, name, parm_list, complain);
15946 }
15947
15948 case TYPEOF_TYPE:
15949 {
15950 tree type;
15951
15952 ++cp_unevaluated_operand;
15953 ++c_inhibit_evaluation_warnings;
15954
15955 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15956 complain, in_decl,
15957 /*integral_constant_expression_p=*/false);
15958
15959 --cp_unevaluated_operand;
15960 --c_inhibit_evaluation_warnings;
15961
15962 type = finish_typeof (type);
15963 return cp_build_qualified_type_real (type,
15964 cp_type_quals (t)
15965 | cp_type_quals (type),
15966 complain);
15967 }
15968
15969 case DECLTYPE_TYPE:
15970 {
15971 tree type;
15972
15973 ++cp_unevaluated_operand;
15974 ++c_inhibit_evaluation_warnings;
15975
15976 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15977 complain|tf_decltype, in_decl,
15978 /*function_p*/false,
15979 /*integral_constant_expression*/false);
15980
15981 --cp_unevaluated_operand;
15982 --c_inhibit_evaluation_warnings;
15983
15984 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15985 type = lambda_capture_field_type (type,
15986 false /*explicit_init*/,
15987 DECLTYPE_FOR_REF_CAPTURE (t));
15988 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15989 type = lambda_proxy_type (type);
15990 else
15991 {
15992 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15993 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15994 && EXPR_P (type))
15995 /* In a template ~id could be either a complement expression
15996 or an unqualified-id naming a destructor; if instantiating
15997 it produces an expression, it's not an id-expression or
15998 member access. */
15999 id = false;
16000 type = finish_decltype_type (type, id, complain);
16001 }
16002 return cp_build_qualified_type_real (type,
16003 cp_type_quals (t)
16004 | cp_type_quals (type),
16005 complain | tf_ignore_bad_quals);
16006 }
16007
16008 case UNDERLYING_TYPE:
16009 {
16010 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16011 complain, in_decl);
16012 return finish_underlying_type (type);
16013 }
16014
16015 case TYPE_ARGUMENT_PACK:
16016 case NONTYPE_ARGUMENT_PACK:
16017 {
16018 tree r;
16019
16020 if (code == NONTYPE_ARGUMENT_PACK)
16021 r = make_node (code);
16022 else
16023 r = cxx_make_type (code);
16024
16025 tree pack_args = ARGUMENT_PACK_ARGS (t);
16026 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
16027 SET_ARGUMENT_PACK_ARGS (r, pack_args);
16028
16029 return r;
16030 }
16031
16032 case VOID_CST:
16033 case INTEGER_CST:
16034 case REAL_CST:
16035 case STRING_CST:
16036 case PLUS_EXPR:
16037 case MINUS_EXPR:
16038 case NEGATE_EXPR:
16039 case NOP_EXPR:
16040 case INDIRECT_REF:
16041 case ADDR_EXPR:
16042 case CALL_EXPR:
16043 case ARRAY_REF:
16044 case SCOPE_REF:
16045 /* We should use one of the expression tsubsts for these codes. */
16046 gcc_unreachable ();
16047
16048 default:
16049 sorry ("use of %qs in template", get_tree_code_name (code));
16050 return error_mark_node;
16051 }
16052 }
16053
16054 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16055 expression on the left-hand side of the "." or "->" operator. We
16056 only do the lookup if we had a dependent BASELINK. Otherwise we
16057 adjust it onto the instantiated heirarchy. */
16058
16059 static tree
16060 tsubst_baselink (tree baselink, tree object_type,
16061 tree args, tsubst_flags_t complain, tree in_decl)
16062 {
16063 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16064 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16065 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16066
16067 tree optype = BASELINK_OPTYPE (baselink);
16068 optype = tsubst (optype, args, complain, in_decl);
16069
16070 tree template_args = NULL_TREE;
16071 bool template_id_p = false;
16072 tree fns = BASELINK_FUNCTIONS (baselink);
16073 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16074 {
16075 template_id_p = true;
16076 template_args = TREE_OPERAND (fns, 1);
16077 fns = TREE_OPERAND (fns, 0);
16078 if (template_args)
16079 template_args = tsubst_template_args (template_args, args,
16080 complain, in_decl);
16081 }
16082
16083 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16084 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16085 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16086
16087 if (dependent_p)
16088 {
16089 tree name = OVL_NAME (fns);
16090 if (IDENTIFIER_CONV_OP_P (name))
16091 name = make_conv_op_name (optype);
16092
16093 if (name == complete_dtor_identifier)
16094 /* Treat as-if non-dependent below. */
16095 dependent_p = false;
16096
16097 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16098 complain);
16099 if (!baselink)
16100 {
16101 if ((complain & tf_error)
16102 && constructor_name_p (name, qualifying_scope))
16103 error ("cannot call constructor %<%T::%D%> directly",
16104 qualifying_scope, name);
16105 return error_mark_node;
16106 }
16107
16108 if (BASELINK_P (baselink))
16109 fns = BASELINK_FUNCTIONS (baselink);
16110 }
16111 else
16112 /* We're going to overwrite pieces below, make a duplicate. */
16113 baselink = copy_node (baselink);
16114
16115 /* If lookup found a single function, mark it as used at this point.
16116 (If lookup found multiple functions the one selected later by
16117 overload resolution will be marked as used at that point.) */
16118 if (!template_id_p && !really_overloaded_fn (fns))
16119 {
16120 tree fn = OVL_FIRST (fns);
16121 bool ok = mark_used (fn, complain);
16122 if (!ok && !(complain & tf_error))
16123 return error_mark_node;
16124 if (ok && BASELINK_P (baselink))
16125 /* We might have instantiated an auto function. */
16126 TREE_TYPE (baselink) = TREE_TYPE (fn);
16127 }
16128
16129 if (BASELINK_P (baselink))
16130 {
16131 /* Add back the template arguments, if present. */
16132 if (template_id_p)
16133 BASELINK_FUNCTIONS (baselink)
16134 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16135
16136 /* Update the conversion operator type. */
16137 BASELINK_OPTYPE (baselink) = optype;
16138 }
16139
16140 if (!object_type)
16141 object_type = current_class_type;
16142
16143 if (qualified_p || !dependent_p)
16144 {
16145 baselink = adjust_result_of_qualified_name_lookup (baselink,
16146 qualifying_scope,
16147 object_type);
16148 if (!qualified_p)
16149 /* We need to call adjust_result_of_qualified_name_lookup in case the
16150 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16151 so that we still get virtual function binding. */
16152 BASELINK_QUALIFIED_P (baselink) = false;
16153 }
16154
16155 return baselink;
16156 }
16157
16158 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16159 true if the qualified-id will be a postfix-expression in-and-of
16160 itself; false if more of the postfix-expression follows the
16161 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16162 of "&". */
16163
16164 static tree
16165 tsubst_qualified_id (tree qualified_id, tree args,
16166 tsubst_flags_t complain, tree in_decl,
16167 bool done, bool address_p)
16168 {
16169 tree expr;
16170 tree scope;
16171 tree name;
16172 bool is_template;
16173 tree template_args;
16174 location_t loc = UNKNOWN_LOCATION;
16175
16176 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16177
16178 /* Figure out what name to look up. */
16179 name = TREE_OPERAND (qualified_id, 1);
16180 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16181 {
16182 is_template = true;
16183 loc = EXPR_LOCATION (name);
16184 template_args = TREE_OPERAND (name, 1);
16185 if (template_args)
16186 template_args = tsubst_template_args (template_args, args,
16187 complain, in_decl);
16188 if (template_args == error_mark_node)
16189 return error_mark_node;
16190 name = TREE_OPERAND (name, 0);
16191 }
16192 else
16193 {
16194 is_template = false;
16195 template_args = NULL_TREE;
16196 }
16197
16198 /* Substitute into the qualifying scope. When there are no ARGS, we
16199 are just trying to simplify a non-dependent expression. In that
16200 case the qualifying scope may be dependent, and, in any case,
16201 substituting will not help. */
16202 scope = TREE_OPERAND (qualified_id, 0);
16203 if (args)
16204 {
16205 scope = tsubst (scope, args, complain, in_decl);
16206 expr = tsubst_copy (name, args, complain, in_decl);
16207 }
16208 else
16209 expr = name;
16210
16211 if (dependent_scope_p (scope))
16212 {
16213 if (is_template)
16214 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16215 tree r = build_qualified_name (NULL_TREE, scope, expr,
16216 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16217 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16218 return r;
16219 }
16220
16221 if (!BASELINK_P (name) && !DECL_P (expr))
16222 {
16223 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16224 {
16225 /* A BIT_NOT_EXPR is used to represent a destructor. */
16226 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16227 {
16228 error ("qualifying type %qT does not match destructor name ~%qT",
16229 scope, TREE_OPERAND (expr, 0));
16230 expr = error_mark_node;
16231 }
16232 else
16233 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16234 LOOK_want::NORMAL, false);
16235 }
16236 else
16237 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16238 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16239 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16240 {
16241 if (complain & tf_error)
16242 {
16243 error ("dependent-name %qE is parsed as a non-type, but "
16244 "instantiation yields a type", qualified_id);
16245 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16246 }
16247 return error_mark_node;
16248 }
16249 }
16250
16251 if (DECL_P (expr))
16252 {
16253 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16254 scope, complain))
16255 return error_mark_node;
16256 /* Remember that there was a reference to this entity. */
16257 if (!mark_used (expr, complain) && !(complain & tf_error))
16258 return error_mark_node;
16259 }
16260
16261 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16262 {
16263 if (complain & tf_error)
16264 qualified_name_lookup_error (scope,
16265 TREE_OPERAND (qualified_id, 1),
16266 expr, input_location);
16267 return error_mark_node;
16268 }
16269
16270 if (is_template)
16271 {
16272 /* We may be repeating a check already done during parsing, but
16273 if it was well-formed and passed then, it will pass again
16274 now, and if it didn't, we wouldn't have got here. The case
16275 we want to catch is when we couldn't tell then, and can now,
16276 namely when templ prior to substitution was an
16277 identifier. */
16278 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16279 return error_mark_node;
16280
16281 if (variable_template_p (expr))
16282 expr = lookup_and_finish_template_variable (expr, template_args,
16283 complain);
16284 else
16285 expr = lookup_template_function (expr, template_args);
16286 }
16287
16288 if (expr == error_mark_node && complain & tf_error)
16289 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16290 expr, input_location);
16291 else if (TYPE_P (scope))
16292 {
16293 expr = (adjust_result_of_qualified_name_lookup
16294 (expr, scope, current_nonlambda_class_type ()));
16295 expr = (finish_qualified_id_expr
16296 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16297 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16298 /*template_arg_p=*/false, complain));
16299 }
16300
16301 /* Expressions do not generally have reference type. */
16302 if (TREE_CODE (expr) != SCOPE_REF
16303 /* However, if we're about to form a pointer-to-member, we just
16304 want the referenced member referenced. */
16305 && TREE_CODE (expr) != OFFSET_REF)
16306 expr = convert_from_reference (expr);
16307
16308 if (REF_PARENTHESIZED_P (qualified_id))
16309 expr = force_paren_expr (expr);
16310
16311 return expr;
16312 }
16313
16314 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16315 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16316 for tsubst. */
16317
16318 static tree
16319 tsubst_init (tree init, tree decl, tree args,
16320 tsubst_flags_t complain, tree in_decl)
16321 {
16322 if (!init)
16323 return NULL_TREE;
16324
16325 init = tsubst_expr (init, args, complain, in_decl, false);
16326
16327 tree type = TREE_TYPE (decl);
16328
16329 if (!init && type != error_mark_node)
16330 {
16331 if (tree auto_node = type_uses_auto (type))
16332 {
16333 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16334 {
16335 if (complain & tf_error)
16336 error ("initializer for %q#D expands to an empty list "
16337 "of expressions", decl);
16338 return error_mark_node;
16339 }
16340 }
16341 else if (!dependent_type_p (type))
16342 {
16343 /* If we had an initializer but it
16344 instantiated to nothing,
16345 value-initialize the object. This will
16346 only occur when the initializer was a
16347 pack expansion where the parameter packs
16348 used in that expansion were of length
16349 zero. */
16350 init = build_value_init (type, complain);
16351 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16352 init = get_target_expr_sfinae (init, complain);
16353 if (TREE_CODE (init) == TARGET_EXPR)
16354 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16355 }
16356 }
16357
16358 return init;
16359 }
16360
16361 /* If T is a reference to a dependent member of the current instantiation C and
16362 we are trying to refer to that member in a partial instantiation of C,
16363 return a SCOPE_REF; otherwise, return NULL_TREE.
16364
16365 This can happen when forming a C++17 deduction guide, as in PR96199. */
16366
16367 static tree
16368 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16369 tree in_decl)
16370 {
16371 if (cxx_dialect < cxx17)
16372 return NULL_TREE;
16373
16374 tree ctx = context_for_name_lookup (t);
16375 if (!CLASS_TYPE_P (ctx))
16376 return NULL_TREE;
16377
16378 ctx = tsubst (ctx, args, complain, in_decl);
16379 if (dependent_scope_p (ctx))
16380 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16381 /*template_p=*/false);
16382
16383 return NULL_TREE;
16384 }
16385
16386 /* Like tsubst, but deals with expressions. This function just replaces
16387 template parms; to finish processing the resultant expression, use
16388 tsubst_copy_and_build or tsubst_expr. */
16389
16390 static tree
16391 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16392 {
16393 enum tree_code code;
16394 tree r;
16395
16396 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16397 return t;
16398
16399 code = TREE_CODE (t);
16400
16401 switch (code)
16402 {
16403 case PARM_DECL:
16404 r = retrieve_local_specialization (t);
16405
16406 if (r == NULL_TREE)
16407 {
16408 /* We get here for a use of 'this' in an NSDMI. */
16409 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16410 return current_class_ptr;
16411
16412 /* This can happen for a parameter name used later in a function
16413 declaration (such as in a late-specified return type). Just
16414 make a dummy decl, since it's only used for its type. */
16415 gcc_assert (cp_unevaluated_operand != 0);
16416 r = tsubst_decl (t, args, complain);
16417 /* Give it the template pattern as its context; its true context
16418 hasn't been instantiated yet and this is good enough for
16419 mangling. */
16420 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16421 }
16422
16423 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16424 r = argument_pack_select_arg (r);
16425 if (!mark_used (r, complain) && !(complain & tf_error))
16426 return error_mark_node;
16427 return r;
16428
16429 case CONST_DECL:
16430 {
16431 tree enum_type;
16432 tree v;
16433
16434 if (DECL_TEMPLATE_PARM_P (t))
16435 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16436 /* There is no need to substitute into namespace-scope
16437 enumerators. */
16438 if (DECL_NAMESPACE_SCOPE_P (t))
16439 return t;
16440 /* If ARGS is NULL, then T is known to be non-dependent. */
16441 if (args == NULL_TREE)
16442 return scalar_constant_value (t);
16443
16444 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16445 return ref;
16446
16447 /* Unfortunately, we cannot just call lookup_name here.
16448 Consider:
16449
16450 template <int I> int f() {
16451 enum E { a = I };
16452 struct S { void g() { E e = a; } };
16453 };
16454
16455 When we instantiate f<7>::S::g(), say, lookup_name is not
16456 clever enough to find f<7>::a. */
16457 enum_type
16458 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16459 /*entering_scope=*/0);
16460
16461 for (v = TYPE_VALUES (enum_type);
16462 v != NULL_TREE;
16463 v = TREE_CHAIN (v))
16464 if (TREE_PURPOSE (v) == DECL_NAME (t))
16465 return TREE_VALUE (v);
16466
16467 /* We didn't find the name. That should never happen; if
16468 name-lookup found it during preliminary parsing, we
16469 should find it again here during instantiation. */
16470 gcc_unreachable ();
16471 }
16472 return t;
16473
16474 case FIELD_DECL:
16475 if (DECL_CONTEXT (t))
16476 {
16477 tree ctx;
16478
16479 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16480 /*entering_scope=*/1);
16481 if (ctx != DECL_CONTEXT (t))
16482 {
16483 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16484 if (!r)
16485 {
16486 if (complain & tf_error)
16487 error ("using invalid field %qD", t);
16488 return error_mark_node;
16489 }
16490 return r;
16491 }
16492 }
16493
16494 return t;
16495
16496 case VAR_DECL:
16497 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16498 return ref;
16499 gcc_fallthrough();
16500 case FUNCTION_DECL:
16501 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16502 r = tsubst (t, args, complain, in_decl);
16503 else if (local_variable_p (t)
16504 && uses_template_parms (DECL_CONTEXT (t)))
16505 {
16506 r = retrieve_local_specialization (t);
16507 if (r == NULL_TREE)
16508 {
16509 /* First try name lookup to find the instantiation. */
16510 r = lookup_name (DECL_NAME (t));
16511 if (r)
16512 {
16513 if (!VAR_P (r))
16514 {
16515 /* During error-recovery we may find a non-variable,
16516 even an OVERLOAD: just bail out and avoid ICEs and
16517 duplicate diagnostics (c++/62207). */
16518 gcc_assert (seen_error ());
16519 return error_mark_node;
16520 }
16521 if (!is_capture_proxy (r))
16522 {
16523 /* Make sure the one we found is the one we want. */
16524 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16525 if (ctx != DECL_CONTEXT (r))
16526 r = NULL_TREE;
16527 }
16528 }
16529
16530 if (r)
16531 /* OK */;
16532 else
16533 {
16534 /* This can happen for a variable used in a
16535 late-specified return type of a local lambda, or for a
16536 local static or constant. Building a new VAR_DECL
16537 should be OK in all those cases. */
16538 r = tsubst_decl (t, args, complain);
16539 if (local_specializations)
16540 /* Avoid infinite recursion (79640). */
16541 register_local_specialization (r, t);
16542 if (decl_maybe_constant_var_p (r))
16543 {
16544 /* We can't call cp_finish_decl, so handle the
16545 initializer by hand. */
16546 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16547 complain, in_decl);
16548 if (!processing_template_decl)
16549 init = maybe_constant_init (init);
16550 if (processing_template_decl
16551 ? potential_constant_expression (init)
16552 : reduced_constant_expression_p (init))
16553 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16554 = TREE_CONSTANT (r) = true;
16555 DECL_INITIAL (r) = init;
16556 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16557 TREE_TYPE (r)
16558 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16559 complain, adc_variable_type);
16560 }
16561 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16562 || decl_constant_var_p (r)
16563 || seen_error ());
16564 if (!processing_template_decl
16565 && !TREE_STATIC (r))
16566 r = process_outer_var_ref (r, complain);
16567 }
16568 /* Remember this for subsequent uses. */
16569 if (local_specializations)
16570 register_local_specialization (r, t);
16571 }
16572 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16573 r = argument_pack_select_arg (r);
16574 }
16575 else
16576 r = t;
16577 if (!mark_used (r, complain))
16578 return error_mark_node;
16579 return r;
16580
16581 case NAMESPACE_DECL:
16582 return t;
16583
16584 case OVERLOAD:
16585 return t;
16586
16587 case BASELINK:
16588 return tsubst_baselink (t, current_nonlambda_class_type (),
16589 args, complain, in_decl);
16590
16591 case TEMPLATE_DECL:
16592 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16593 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16594 args, complain, in_decl);
16595 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16596 return tsubst (t, args, complain, in_decl);
16597 else if (DECL_CLASS_SCOPE_P (t)
16598 && uses_template_parms (DECL_CONTEXT (t)))
16599 {
16600 /* Template template argument like the following example need
16601 special treatment:
16602
16603 template <template <class> class TT> struct C {};
16604 template <class T> struct D {
16605 template <class U> struct E {};
16606 C<E> c; // #1
16607 };
16608 D<int> d; // #2
16609
16610 We are processing the template argument `E' in #1 for
16611 the template instantiation #2. Originally, `E' is a
16612 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16613 have to substitute this with one having context `D<int>'. */
16614
16615 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16616 if (dependent_scope_p (context))
16617 {
16618 /* When rewriting a constructor into a deduction guide, a
16619 non-dependent name can become dependent, so memtmpl<args>
16620 becomes context::template memtmpl<args>. */
16621 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16622 return build_qualified_name (type, context, DECL_NAME (t),
16623 /*template*/true);
16624 }
16625 return lookup_field (context, DECL_NAME(t), 0, false);
16626 }
16627 else
16628 /* Ordinary template template argument. */
16629 return t;
16630
16631 case NON_LVALUE_EXPR:
16632 case VIEW_CONVERT_EXPR:
16633 {
16634 /* Handle location wrappers by substituting the wrapped node
16635 first, *then* reusing the resulting type. Doing the type
16636 first ensures that we handle template parameters and
16637 parameter pack expansions. */
16638 if (location_wrapper_p (t))
16639 {
16640 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16641 complain, in_decl);
16642 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16643 }
16644 tree op = TREE_OPERAND (t, 0);
16645 if (code == VIEW_CONVERT_EXPR
16646 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16647 {
16648 /* Wrapper to make a C++20 template parameter object const. */
16649 op = tsubst_copy (op, args, complain, in_decl);
16650 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16651 {
16652 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16653 return build1 (code, type, op);
16654 }
16655 else
16656 {
16657 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16658 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16659 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16660 return op;
16661 }
16662 }
16663 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16664 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16665 {
16666 op = tsubst_copy (op, args, complain, in_decl);
16667 op = build1 (code, TREE_TYPE (op), op);
16668 REF_PARENTHESIZED_P (op) = true;
16669 return op;
16670 }
16671 /* We shouldn't see any other uses of these in templates. */
16672 gcc_unreachable ();
16673 }
16674
16675 case CAST_EXPR:
16676 case REINTERPRET_CAST_EXPR:
16677 case CONST_CAST_EXPR:
16678 case STATIC_CAST_EXPR:
16679 case DYNAMIC_CAST_EXPR:
16680 case IMPLICIT_CONV_EXPR:
16681 case CONVERT_EXPR:
16682 case NOP_EXPR:
16683 {
16684 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16685 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16686 return build1 (code, type, op0);
16687 }
16688
16689 case SIZEOF_EXPR:
16690 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16691 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16692 {
16693 tree expanded, op = TREE_OPERAND (t, 0);
16694 int len = 0;
16695
16696 if (SIZEOF_EXPR_TYPE_P (t))
16697 op = TREE_TYPE (op);
16698
16699 ++cp_unevaluated_operand;
16700 ++c_inhibit_evaluation_warnings;
16701 /* We only want to compute the number of arguments. */
16702 if (PACK_EXPANSION_P (op))
16703 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16704 else
16705 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16706 args, complain, in_decl);
16707 --cp_unevaluated_operand;
16708 --c_inhibit_evaluation_warnings;
16709
16710 if (TREE_CODE (expanded) == TREE_VEC)
16711 {
16712 len = TREE_VEC_LENGTH (expanded);
16713 /* Set TREE_USED for the benefit of -Wunused. */
16714 for (int i = 0; i < len; i++)
16715 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16716 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16717 }
16718
16719 if (expanded == error_mark_node)
16720 return error_mark_node;
16721 else if (PACK_EXPANSION_P (expanded)
16722 || (TREE_CODE (expanded) == TREE_VEC
16723 && pack_expansion_args_count (expanded)))
16724
16725 {
16726 if (PACK_EXPANSION_P (expanded))
16727 /* OK. */;
16728 else if (TREE_VEC_LENGTH (expanded) == 1)
16729 expanded = TREE_VEC_ELT (expanded, 0);
16730 else
16731 expanded = make_argument_pack (expanded);
16732
16733 if (TYPE_P (expanded))
16734 return cxx_sizeof_or_alignof_type (input_location,
16735 expanded, SIZEOF_EXPR,
16736 false,
16737 complain & tf_error);
16738 else
16739 return cxx_sizeof_or_alignof_expr (input_location,
16740 expanded, SIZEOF_EXPR,
16741 complain & tf_error);
16742 }
16743 else
16744 return build_int_cst (size_type_node, len);
16745 }
16746 if (SIZEOF_EXPR_TYPE_P (t))
16747 {
16748 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16749 args, complain, in_decl);
16750 r = build1 (NOP_EXPR, r, error_mark_node);
16751 r = build1 (SIZEOF_EXPR,
16752 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16753 SIZEOF_EXPR_TYPE_P (r) = 1;
16754 return r;
16755 }
16756 /* Fall through */
16757
16758 case INDIRECT_REF:
16759 case NEGATE_EXPR:
16760 case TRUTH_NOT_EXPR:
16761 case BIT_NOT_EXPR:
16762 case ADDR_EXPR:
16763 case UNARY_PLUS_EXPR: /* Unary + */
16764 case ALIGNOF_EXPR:
16765 case AT_ENCODE_EXPR:
16766 case ARROW_EXPR:
16767 case THROW_EXPR:
16768 case TYPEID_EXPR:
16769 case REALPART_EXPR:
16770 case IMAGPART_EXPR:
16771 case PAREN_EXPR:
16772 {
16773 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16774 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16775 r = build1 (code, type, op0);
16776 if (code == ALIGNOF_EXPR)
16777 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16778 return r;
16779 }
16780
16781 case COMPONENT_REF:
16782 {
16783 tree object;
16784 tree name;
16785
16786 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16787 name = TREE_OPERAND (t, 1);
16788 if (TREE_CODE (name) == BIT_NOT_EXPR)
16789 {
16790 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16791 complain, in_decl);
16792 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16793 }
16794 else if (TREE_CODE (name) == SCOPE_REF
16795 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16796 {
16797 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16798 complain, in_decl);
16799 name = TREE_OPERAND (name, 1);
16800 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16801 complain, in_decl);
16802 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16803 name = build_qualified_name (/*type=*/NULL_TREE,
16804 base, name,
16805 /*template_p=*/false);
16806 }
16807 else if (BASELINK_P (name))
16808 name = tsubst_baselink (name,
16809 non_reference (TREE_TYPE (object)),
16810 args, complain,
16811 in_decl);
16812 else
16813 name = tsubst_copy (name, args, complain, in_decl);
16814 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16815 }
16816
16817 case PLUS_EXPR:
16818 case MINUS_EXPR:
16819 case MULT_EXPR:
16820 case TRUNC_DIV_EXPR:
16821 case CEIL_DIV_EXPR:
16822 case FLOOR_DIV_EXPR:
16823 case ROUND_DIV_EXPR:
16824 case EXACT_DIV_EXPR:
16825 case BIT_AND_EXPR:
16826 case BIT_IOR_EXPR:
16827 case BIT_XOR_EXPR:
16828 case TRUNC_MOD_EXPR:
16829 case FLOOR_MOD_EXPR:
16830 case TRUTH_ANDIF_EXPR:
16831 case TRUTH_ORIF_EXPR:
16832 case TRUTH_AND_EXPR:
16833 case TRUTH_OR_EXPR:
16834 case RSHIFT_EXPR:
16835 case LSHIFT_EXPR:
16836 case EQ_EXPR:
16837 case NE_EXPR:
16838 case MAX_EXPR:
16839 case MIN_EXPR:
16840 case LE_EXPR:
16841 case GE_EXPR:
16842 case LT_EXPR:
16843 case GT_EXPR:
16844 case COMPOUND_EXPR:
16845 case DOTSTAR_EXPR:
16846 case MEMBER_REF:
16847 case PREDECREMENT_EXPR:
16848 case PREINCREMENT_EXPR:
16849 case POSTDECREMENT_EXPR:
16850 case POSTINCREMENT_EXPR:
16851 {
16852 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16853 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16854 return build_nt (code, op0, op1);
16855 }
16856
16857 case SCOPE_REF:
16858 {
16859 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16860 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16861 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16862 QUALIFIED_NAME_IS_TEMPLATE (t));
16863 }
16864
16865 case ARRAY_REF:
16866 {
16867 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16868 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16869 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16870 }
16871
16872 case CALL_EXPR:
16873 {
16874 int n = VL_EXP_OPERAND_LENGTH (t);
16875 tree result = build_vl_exp (CALL_EXPR, n);
16876 int i;
16877 for (i = 0; i < n; i++)
16878 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16879 complain, in_decl);
16880 return result;
16881 }
16882
16883 case COND_EXPR:
16884 case MODOP_EXPR:
16885 case PSEUDO_DTOR_EXPR:
16886 case VEC_PERM_EXPR:
16887 {
16888 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16889 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16890 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16891 r = build_nt (code, op0, op1, op2);
16892 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16893 return r;
16894 }
16895
16896 case NEW_EXPR:
16897 {
16898 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16899 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16900 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16901 r = build_nt (code, op0, op1, op2);
16902 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16903 return r;
16904 }
16905
16906 case DELETE_EXPR:
16907 {
16908 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16909 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16910 r = build_nt (code, op0, op1);
16911 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16912 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16913 return r;
16914 }
16915
16916 case TEMPLATE_ID_EXPR:
16917 {
16918 /* Substituted template arguments */
16919 tree fn = TREE_OPERAND (t, 0);
16920 tree targs = TREE_OPERAND (t, 1);
16921
16922 fn = tsubst_copy (fn, args, complain, in_decl);
16923 if (targs)
16924 targs = tsubst_template_args (targs, args, complain, in_decl);
16925
16926 return lookup_template_function (fn, targs);
16927 }
16928
16929 case TREE_LIST:
16930 {
16931 tree purpose, value, chain;
16932
16933 if (t == void_list_node)
16934 return t;
16935
16936 purpose = TREE_PURPOSE (t);
16937 if (purpose)
16938 purpose = tsubst_copy (purpose, args, complain, in_decl);
16939 value = TREE_VALUE (t);
16940 if (value)
16941 value = tsubst_copy (value, args, complain, in_decl);
16942 chain = TREE_CHAIN (t);
16943 if (chain && chain != void_type_node)
16944 chain = tsubst_copy (chain, args, complain, in_decl);
16945 if (purpose == TREE_PURPOSE (t)
16946 && value == TREE_VALUE (t)
16947 && chain == TREE_CHAIN (t))
16948 return t;
16949 return tree_cons (purpose, value, chain);
16950 }
16951
16952 case RECORD_TYPE:
16953 case UNION_TYPE:
16954 case ENUMERAL_TYPE:
16955 case INTEGER_TYPE:
16956 case TEMPLATE_TYPE_PARM:
16957 case TEMPLATE_TEMPLATE_PARM:
16958 case BOUND_TEMPLATE_TEMPLATE_PARM:
16959 case TEMPLATE_PARM_INDEX:
16960 case POINTER_TYPE:
16961 case REFERENCE_TYPE:
16962 case OFFSET_TYPE:
16963 case FUNCTION_TYPE:
16964 case METHOD_TYPE:
16965 case ARRAY_TYPE:
16966 case TYPENAME_TYPE:
16967 case UNBOUND_CLASS_TEMPLATE:
16968 case TYPEOF_TYPE:
16969 case DECLTYPE_TYPE:
16970 case TYPE_DECL:
16971 return tsubst (t, args, complain, in_decl);
16972
16973 case USING_DECL:
16974 t = DECL_NAME (t);
16975 /* Fall through. */
16976 case IDENTIFIER_NODE:
16977 if (IDENTIFIER_CONV_OP_P (t))
16978 {
16979 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16980 return make_conv_op_name (new_type);
16981 }
16982 else
16983 return t;
16984
16985 case CONSTRUCTOR:
16986 /* This is handled by tsubst_copy_and_build. */
16987 gcc_unreachable ();
16988
16989 case VA_ARG_EXPR:
16990 {
16991 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16992 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16993 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16994 }
16995
16996 case CLEANUP_POINT_EXPR:
16997 /* We shouldn't have built any of these during initial template
16998 generation. Instead, they should be built during instantiation
16999 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17000 gcc_unreachable ();
17001
17002 case OFFSET_REF:
17003 {
17004 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17005 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17006 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17007 r = build2 (code, type, op0, op1);
17008 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17009 if (!mark_used (TREE_OPERAND (r, 1), complain)
17010 && !(complain & tf_error))
17011 return error_mark_node;
17012 return r;
17013 }
17014
17015 case EXPR_PACK_EXPANSION:
17016 error ("invalid use of pack expansion expression");
17017 return error_mark_node;
17018
17019 case NONTYPE_ARGUMENT_PACK:
17020 error ("use %<...%> to expand argument pack");
17021 return error_mark_node;
17022
17023 case VOID_CST:
17024 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17025 return t;
17026
17027 case INTEGER_CST:
17028 case REAL_CST:
17029 case COMPLEX_CST:
17030 {
17031 /* Instantiate any typedefs in the type. */
17032 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17033 r = fold_convert (type, t);
17034 gcc_assert (TREE_CODE (r) == code);
17035 return r;
17036 }
17037
17038 case STRING_CST:
17039 {
17040 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17041 r = t;
17042 if (type != TREE_TYPE (t))
17043 {
17044 r = copy_node (t);
17045 TREE_TYPE (r) = type;
17046 }
17047 return r;
17048 }
17049
17050 case PTRMEM_CST:
17051 /* These can sometimes show up in a partial instantiation, but never
17052 involve template parms. */
17053 gcc_assert (!uses_template_parms (t));
17054 return t;
17055
17056 case UNARY_LEFT_FOLD_EXPR:
17057 return tsubst_unary_left_fold (t, args, complain, in_decl);
17058 case UNARY_RIGHT_FOLD_EXPR:
17059 return tsubst_unary_right_fold (t, args, complain, in_decl);
17060 case BINARY_LEFT_FOLD_EXPR:
17061 return tsubst_binary_left_fold (t, args, complain, in_decl);
17062 case BINARY_RIGHT_FOLD_EXPR:
17063 return tsubst_binary_right_fold (t, args, complain, in_decl);
17064 case PREDICT_EXPR:
17065 return t;
17066
17067 case DEBUG_BEGIN_STMT:
17068 /* ??? There's no point in copying it for now, but maybe some
17069 day it will contain more information, such as a pointer back
17070 to the containing function, inlined copy or so. */
17071 return t;
17072
17073 case CO_AWAIT_EXPR:
17074 return tsubst_expr (t, args, complain, in_decl,
17075 /*integral_constant_expression_p=*/false);
17076 break;
17077
17078 default:
17079 /* We shouldn't get here, but keep going if !flag_checking. */
17080 if (flag_checking)
17081 gcc_unreachable ();
17082 return t;
17083 }
17084 }
17085
17086 /* Helper function for tsubst_omp_clauses, used for instantiation of
17087 OMP_CLAUSE_DECL of clauses. */
17088
17089 static tree
17090 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17091 tree in_decl, tree *iterator_cache)
17092 {
17093 if (decl == NULL_TREE)
17094 return NULL_TREE;
17095
17096 /* Handle OpenMP iterators. */
17097 if (TREE_CODE (decl) == TREE_LIST
17098 && TREE_PURPOSE (decl)
17099 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17100 {
17101 tree ret;
17102 if (iterator_cache[0] == TREE_PURPOSE (decl))
17103 ret = iterator_cache[1];
17104 else
17105 {
17106 tree *tp = &ret;
17107 begin_scope (sk_omp, NULL);
17108 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17109 {
17110 *tp = copy_node (it);
17111 TREE_VEC_ELT (*tp, 0)
17112 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17113 TREE_VEC_ELT (*tp, 1)
17114 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17115 /*integral_constant_expression_p=*/false);
17116 TREE_VEC_ELT (*tp, 2)
17117 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17118 /*integral_constant_expression_p=*/false);
17119 TREE_VEC_ELT (*tp, 3)
17120 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17121 /*integral_constant_expression_p=*/false);
17122 TREE_CHAIN (*tp) = NULL_TREE;
17123 tp = &TREE_CHAIN (*tp);
17124 }
17125 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17126 iterator_cache[0] = TREE_PURPOSE (decl);
17127 iterator_cache[1] = ret;
17128 }
17129 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17130 args, complain,
17131 in_decl, NULL));
17132 }
17133
17134 /* Handle an OpenMP array section represented as a TREE_LIST (or
17135 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17136 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17137 TREE_LIST. We can handle it exactly the same as an array section
17138 (purpose, value, and a chain), even though the nomenclature
17139 (low_bound, length, etc) is different. */
17140 if (TREE_CODE (decl) == TREE_LIST)
17141 {
17142 tree low_bound
17143 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17144 /*integral_constant_expression_p=*/false);
17145 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17146 /*integral_constant_expression_p=*/false);
17147 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17148 in_decl, NULL);
17149 if (TREE_PURPOSE (decl) == low_bound
17150 && TREE_VALUE (decl) == length
17151 && TREE_CHAIN (decl) == chain)
17152 return decl;
17153 tree ret = tree_cons (low_bound, length, chain);
17154 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17155 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17156 return ret;
17157 }
17158 tree ret = tsubst_expr (decl, args, complain, in_decl,
17159 /*integral_constant_expression_p=*/false);
17160 /* Undo convert_from_reference tsubst_expr could have called. */
17161 if (decl
17162 && REFERENCE_REF_P (ret)
17163 && !REFERENCE_REF_P (decl))
17164 ret = TREE_OPERAND (ret, 0);
17165 return ret;
17166 }
17167
17168 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17169
17170 static tree
17171 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17172 tree args, tsubst_flags_t complain, tree in_decl)
17173 {
17174 tree new_clauses = NULL_TREE, nc, oc;
17175 tree linear_no_step = NULL_TREE;
17176 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17177
17178 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17179 {
17180 nc = copy_node (oc);
17181 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17182 new_clauses = nc;
17183
17184 switch (OMP_CLAUSE_CODE (nc))
17185 {
17186 case OMP_CLAUSE_LASTPRIVATE:
17187 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17188 {
17189 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17190 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17191 in_decl, /*integral_constant_expression_p=*/false);
17192 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17193 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17194 }
17195 /* FALLTHRU */
17196 case OMP_CLAUSE_PRIVATE:
17197 case OMP_CLAUSE_SHARED:
17198 case OMP_CLAUSE_FIRSTPRIVATE:
17199 case OMP_CLAUSE_COPYIN:
17200 case OMP_CLAUSE_COPYPRIVATE:
17201 case OMP_CLAUSE_UNIFORM:
17202 case OMP_CLAUSE_DEPEND:
17203 case OMP_CLAUSE_FROM:
17204 case OMP_CLAUSE_TO:
17205 case OMP_CLAUSE_MAP:
17206 case OMP_CLAUSE_NONTEMPORAL:
17207 case OMP_CLAUSE_USE_DEVICE_PTR:
17208 case OMP_CLAUSE_USE_DEVICE_ADDR:
17209 case OMP_CLAUSE_IS_DEVICE_PTR:
17210 case OMP_CLAUSE_INCLUSIVE:
17211 case OMP_CLAUSE_EXCLUSIVE:
17212 OMP_CLAUSE_DECL (nc)
17213 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17214 in_decl, iterator_cache);
17215 break;
17216 case OMP_CLAUSE_TILE:
17217 case OMP_CLAUSE_IF:
17218 case OMP_CLAUSE_NUM_THREADS:
17219 case OMP_CLAUSE_SCHEDULE:
17220 case OMP_CLAUSE_COLLAPSE:
17221 case OMP_CLAUSE_FINAL:
17222 case OMP_CLAUSE_DEVICE:
17223 case OMP_CLAUSE_DIST_SCHEDULE:
17224 case OMP_CLAUSE_NUM_TEAMS:
17225 case OMP_CLAUSE_THREAD_LIMIT:
17226 case OMP_CLAUSE_SAFELEN:
17227 case OMP_CLAUSE_SIMDLEN:
17228 case OMP_CLAUSE_NUM_TASKS:
17229 case OMP_CLAUSE_GRAINSIZE:
17230 case OMP_CLAUSE_PRIORITY:
17231 case OMP_CLAUSE_ORDERED:
17232 case OMP_CLAUSE_HINT:
17233 case OMP_CLAUSE_NUM_GANGS:
17234 case OMP_CLAUSE_NUM_WORKERS:
17235 case OMP_CLAUSE_VECTOR_LENGTH:
17236 case OMP_CLAUSE_WORKER:
17237 case OMP_CLAUSE_VECTOR:
17238 case OMP_CLAUSE_ASYNC:
17239 case OMP_CLAUSE_WAIT:
17240 OMP_CLAUSE_OPERAND (nc, 0)
17241 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17242 in_decl, /*integral_constant_expression_p=*/false);
17243 break;
17244 case OMP_CLAUSE_REDUCTION:
17245 case OMP_CLAUSE_IN_REDUCTION:
17246 case OMP_CLAUSE_TASK_REDUCTION:
17247 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17248 {
17249 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17250 if (TREE_CODE (placeholder) == SCOPE_REF)
17251 {
17252 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17253 complain, in_decl);
17254 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17255 = build_qualified_name (NULL_TREE, scope,
17256 TREE_OPERAND (placeholder, 1),
17257 false);
17258 }
17259 else
17260 gcc_assert (identifier_p (placeholder));
17261 }
17262 OMP_CLAUSE_DECL (nc)
17263 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17264 in_decl, NULL);
17265 break;
17266 case OMP_CLAUSE_GANG:
17267 case OMP_CLAUSE_ALIGNED:
17268 OMP_CLAUSE_DECL (nc)
17269 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17270 in_decl, NULL);
17271 OMP_CLAUSE_OPERAND (nc, 1)
17272 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17273 in_decl, /*integral_constant_expression_p=*/false);
17274 break;
17275 case OMP_CLAUSE_LINEAR:
17276 OMP_CLAUSE_DECL (nc)
17277 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17278 in_decl, NULL);
17279 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17280 {
17281 gcc_assert (!linear_no_step);
17282 linear_no_step = nc;
17283 }
17284 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17285 OMP_CLAUSE_LINEAR_STEP (nc)
17286 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17287 complain, in_decl, NULL);
17288 else
17289 OMP_CLAUSE_LINEAR_STEP (nc)
17290 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17291 in_decl,
17292 /*integral_constant_expression_p=*/false);
17293 break;
17294 case OMP_CLAUSE_NOWAIT:
17295 case OMP_CLAUSE_DEFAULT:
17296 case OMP_CLAUSE_UNTIED:
17297 case OMP_CLAUSE_MERGEABLE:
17298 case OMP_CLAUSE_INBRANCH:
17299 case OMP_CLAUSE_NOTINBRANCH:
17300 case OMP_CLAUSE_PROC_BIND:
17301 case OMP_CLAUSE_FOR:
17302 case OMP_CLAUSE_PARALLEL:
17303 case OMP_CLAUSE_SECTIONS:
17304 case OMP_CLAUSE_TASKGROUP:
17305 case OMP_CLAUSE_NOGROUP:
17306 case OMP_CLAUSE_THREADS:
17307 case OMP_CLAUSE_SIMD:
17308 case OMP_CLAUSE_DEFAULTMAP:
17309 case OMP_CLAUSE_ORDER:
17310 case OMP_CLAUSE_BIND:
17311 case OMP_CLAUSE_INDEPENDENT:
17312 case OMP_CLAUSE_AUTO:
17313 case OMP_CLAUSE_SEQ:
17314 case OMP_CLAUSE_IF_PRESENT:
17315 case OMP_CLAUSE_FINALIZE:
17316 break;
17317 default:
17318 gcc_unreachable ();
17319 }
17320 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17321 switch (OMP_CLAUSE_CODE (nc))
17322 {
17323 case OMP_CLAUSE_SHARED:
17324 case OMP_CLAUSE_PRIVATE:
17325 case OMP_CLAUSE_FIRSTPRIVATE:
17326 case OMP_CLAUSE_LASTPRIVATE:
17327 case OMP_CLAUSE_COPYPRIVATE:
17328 case OMP_CLAUSE_LINEAR:
17329 case OMP_CLAUSE_REDUCTION:
17330 case OMP_CLAUSE_IN_REDUCTION:
17331 case OMP_CLAUSE_TASK_REDUCTION:
17332 case OMP_CLAUSE_USE_DEVICE_PTR:
17333 case OMP_CLAUSE_USE_DEVICE_ADDR:
17334 case OMP_CLAUSE_IS_DEVICE_PTR:
17335 case OMP_CLAUSE_INCLUSIVE:
17336 case OMP_CLAUSE_EXCLUSIVE:
17337 /* tsubst_expr on SCOPE_REF results in returning
17338 finish_non_static_data_member result. Undo that here. */
17339 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17340 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17341 == IDENTIFIER_NODE))
17342 {
17343 tree t = OMP_CLAUSE_DECL (nc);
17344 tree v = t;
17345 while (v)
17346 switch (TREE_CODE (v))
17347 {
17348 case COMPONENT_REF:
17349 case MEM_REF:
17350 case INDIRECT_REF:
17351 CASE_CONVERT:
17352 case POINTER_PLUS_EXPR:
17353 v = TREE_OPERAND (v, 0);
17354 continue;
17355 case PARM_DECL:
17356 if (DECL_CONTEXT (v) == current_function_decl
17357 && DECL_ARTIFICIAL (v)
17358 && DECL_NAME (v) == this_identifier)
17359 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17360 /* FALLTHRU */
17361 default:
17362 v = NULL_TREE;
17363 break;
17364 }
17365 }
17366 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17367 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17368 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17369 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17370 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17371 {
17372 tree decl = OMP_CLAUSE_DECL (nc);
17373 if (VAR_P (decl))
17374 {
17375 retrofit_lang_decl (decl);
17376 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17377 }
17378 }
17379 break;
17380 default:
17381 break;
17382 }
17383 }
17384
17385 new_clauses = nreverse (new_clauses);
17386 if (ort != C_ORT_OMP_DECLARE_SIMD)
17387 {
17388 new_clauses = finish_omp_clauses (new_clauses, ort);
17389 if (linear_no_step)
17390 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17391 if (nc == linear_no_step)
17392 {
17393 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17394 break;
17395 }
17396 }
17397 return new_clauses;
17398 }
17399
17400 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17401
17402 static tree
17403 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17404 tree in_decl)
17405 {
17406 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17407
17408 tree purpose, value, chain;
17409
17410 if (t == NULL)
17411 return t;
17412
17413 if (TREE_CODE (t) != TREE_LIST)
17414 return tsubst_copy_and_build (t, args, complain, in_decl,
17415 /*function_p=*/false,
17416 /*integral_constant_expression_p=*/false);
17417
17418 if (t == void_list_node)
17419 return t;
17420
17421 purpose = TREE_PURPOSE (t);
17422 if (purpose)
17423 purpose = RECUR (purpose);
17424 value = TREE_VALUE (t);
17425 if (value)
17426 {
17427 if (TREE_CODE (value) != LABEL_DECL)
17428 value = RECUR (value);
17429 else
17430 {
17431 value = lookup_label (DECL_NAME (value));
17432 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17433 TREE_USED (value) = 1;
17434 }
17435 }
17436 chain = TREE_CHAIN (t);
17437 if (chain && chain != void_type_node)
17438 chain = RECUR (chain);
17439 return tree_cons (purpose, value, chain);
17440 #undef RECUR
17441 }
17442
17443 /* Used to temporarily communicate the list of #pragma omp parallel
17444 clauses to #pragma omp for instantiation if they are combined
17445 together. */
17446
17447 static tree *omp_parallel_combined_clauses;
17448
17449 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17450 tree *, unsigned int *);
17451
17452 /* Substitute one OMP_FOR iterator. */
17453
17454 static bool
17455 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17456 tree initv, tree condv, tree incrv, tree *clauses,
17457 tree args, tsubst_flags_t complain, tree in_decl,
17458 bool integral_constant_expression_p)
17459 {
17460 #define RECUR(NODE) \
17461 tsubst_expr ((NODE), args, complain, in_decl, \
17462 integral_constant_expression_p)
17463 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17464 bool ret = false;
17465
17466 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17467 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17468
17469 decl = TREE_OPERAND (init, 0);
17470 init = TREE_OPERAND (init, 1);
17471 tree decl_expr = NULL_TREE;
17472 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17473 if (range_for)
17474 {
17475 bool decomp = false;
17476 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17477 {
17478 tree v = DECL_VALUE_EXPR (decl);
17479 if (TREE_CODE (v) == ARRAY_REF
17480 && VAR_P (TREE_OPERAND (v, 0))
17481 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17482 {
17483 tree decomp_first = NULL_TREE;
17484 unsigned decomp_cnt = 0;
17485 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17486 maybe_push_decl (d);
17487 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17488 in_decl, &decomp_first, &decomp_cnt);
17489 decomp = true;
17490 if (d == error_mark_node)
17491 decl = error_mark_node;
17492 else
17493 for (unsigned int i = 0; i < decomp_cnt; i++)
17494 {
17495 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17496 {
17497 tree v = build_nt (ARRAY_REF, d,
17498 size_int (decomp_cnt - i - 1),
17499 NULL_TREE, NULL_TREE);
17500 SET_DECL_VALUE_EXPR (decomp_first, v);
17501 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17502 }
17503 fit_decomposition_lang_decl (decomp_first, d);
17504 decomp_first = DECL_CHAIN (decomp_first);
17505 }
17506 }
17507 }
17508 decl = tsubst_decl (decl, args, complain);
17509 if (!decomp)
17510 maybe_push_decl (decl);
17511 }
17512 else if (init && TREE_CODE (init) == DECL_EXPR)
17513 {
17514 /* We need to jump through some hoops to handle declarations in the
17515 init-statement, since we might need to handle auto deduction,
17516 but we need to keep control of initialization. */
17517 decl_expr = init;
17518 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17519 decl = tsubst_decl (decl, args, complain);
17520 }
17521 else
17522 {
17523 if (TREE_CODE (decl) == SCOPE_REF)
17524 {
17525 decl = RECUR (decl);
17526 if (TREE_CODE (decl) == COMPONENT_REF)
17527 {
17528 tree v = decl;
17529 while (v)
17530 switch (TREE_CODE (v))
17531 {
17532 case COMPONENT_REF:
17533 case MEM_REF:
17534 case INDIRECT_REF:
17535 CASE_CONVERT:
17536 case POINTER_PLUS_EXPR:
17537 v = TREE_OPERAND (v, 0);
17538 continue;
17539 case PARM_DECL:
17540 if (DECL_CONTEXT (v) == current_function_decl
17541 && DECL_ARTIFICIAL (v)
17542 && DECL_NAME (v) == this_identifier)
17543 {
17544 decl = TREE_OPERAND (decl, 1);
17545 decl = omp_privatize_field (decl, false);
17546 }
17547 /* FALLTHRU */
17548 default:
17549 v = NULL_TREE;
17550 break;
17551 }
17552 }
17553 }
17554 else
17555 decl = RECUR (decl);
17556 }
17557 if (init && TREE_CODE (init) == TREE_VEC)
17558 {
17559 init = copy_node (init);
17560 TREE_VEC_ELT (init, 0)
17561 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17562 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17563 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17564 }
17565 else
17566 init = RECUR (init);
17567
17568 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17569 {
17570 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17571 if (TREE_CODE (o) == TREE_LIST)
17572 TREE_VEC_ELT (orig_declv, i)
17573 = tree_cons (RECUR (TREE_PURPOSE (o)),
17574 RECUR (TREE_VALUE (o)),
17575 NULL_TREE);
17576 else
17577 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17578 }
17579
17580 if (range_for)
17581 {
17582 tree this_pre_body = NULL_TREE;
17583 tree orig_init = NULL_TREE;
17584 tree orig_decl = NULL_TREE;
17585 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17586 orig_init, cond, incr);
17587 if (orig_decl)
17588 {
17589 if (orig_declv == NULL_TREE)
17590 orig_declv = copy_node (declv);
17591 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17592 ret = true;
17593 }
17594 else if (orig_declv)
17595 TREE_VEC_ELT (orig_declv, i) = decl;
17596 }
17597
17598 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17599 if (!range_for && auto_node && init)
17600 TREE_TYPE (decl)
17601 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17602
17603 gcc_assert (!type_dependent_expression_p (decl));
17604
17605 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17606 {
17607 if (decl_expr)
17608 {
17609 /* Declare the variable, but don't let that initialize it. */
17610 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17611 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17612 RECUR (decl_expr);
17613 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17614 }
17615
17616 if (!range_for)
17617 {
17618 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17619 if (COMPARISON_CLASS_P (cond)
17620 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17621 {
17622 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17623 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17624 TREE_VEC_ELT (rhs, 0)
17625 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17626 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17627 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17628 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17629 lhs, rhs);
17630 }
17631 else
17632 cond = RECUR (cond);
17633 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17634 if (TREE_CODE (incr) == MODIFY_EXPR)
17635 {
17636 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17637 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17638 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17639 NOP_EXPR, rhs, complain);
17640 }
17641 else
17642 incr = RECUR (incr);
17643 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17644 TREE_VEC_ELT (orig_declv, i) = decl;
17645 }
17646 TREE_VEC_ELT (declv, i) = decl;
17647 TREE_VEC_ELT (initv, i) = init;
17648 TREE_VEC_ELT (condv, i) = cond;
17649 TREE_VEC_ELT (incrv, i) = incr;
17650 return ret;
17651 }
17652
17653 if (decl_expr)
17654 {
17655 /* Declare and initialize the variable. */
17656 RECUR (decl_expr);
17657 init = NULL_TREE;
17658 }
17659 else if (init)
17660 {
17661 tree *pc;
17662 int j;
17663 for (j = ((omp_parallel_combined_clauses == NULL
17664 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17665 {
17666 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17667 {
17668 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17669 && OMP_CLAUSE_DECL (*pc) == decl)
17670 break;
17671 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17672 && OMP_CLAUSE_DECL (*pc) == decl)
17673 {
17674 if (j)
17675 break;
17676 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17677 tree c = *pc;
17678 *pc = OMP_CLAUSE_CHAIN (c);
17679 OMP_CLAUSE_CHAIN (c) = *clauses;
17680 *clauses = c;
17681 }
17682 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17683 && OMP_CLAUSE_DECL (*pc) == decl)
17684 {
17685 error ("iteration variable %qD should not be firstprivate",
17686 decl);
17687 *pc = OMP_CLAUSE_CHAIN (*pc);
17688 }
17689 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17690 && OMP_CLAUSE_DECL (*pc) == decl)
17691 {
17692 error ("iteration variable %qD should not be reduction",
17693 decl);
17694 *pc = OMP_CLAUSE_CHAIN (*pc);
17695 }
17696 else
17697 pc = &OMP_CLAUSE_CHAIN (*pc);
17698 }
17699 if (*pc)
17700 break;
17701 }
17702 if (*pc == NULL_TREE)
17703 {
17704 tree c = build_omp_clause (input_location,
17705 TREE_CODE (t) == OMP_LOOP
17706 ? OMP_CLAUSE_LASTPRIVATE
17707 : OMP_CLAUSE_PRIVATE);
17708 OMP_CLAUSE_DECL (c) = decl;
17709 c = finish_omp_clauses (c, C_ORT_OMP);
17710 if (c)
17711 {
17712 OMP_CLAUSE_CHAIN (c) = *clauses;
17713 *clauses = c;
17714 }
17715 }
17716 }
17717 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17718 if (COMPARISON_CLASS_P (cond))
17719 {
17720 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17721 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17722 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17723 }
17724 else
17725 cond = RECUR (cond);
17726 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17727 switch (TREE_CODE (incr))
17728 {
17729 case PREINCREMENT_EXPR:
17730 case PREDECREMENT_EXPR:
17731 case POSTINCREMENT_EXPR:
17732 case POSTDECREMENT_EXPR:
17733 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17734 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17735 break;
17736 case MODIFY_EXPR:
17737 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17738 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17739 {
17740 tree rhs = TREE_OPERAND (incr, 1);
17741 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17742 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17743 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17744 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17745 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17746 rhs0, rhs1));
17747 }
17748 else
17749 incr = RECUR (incr);
17750 break;
17751 case MODOP_EXPR:
17752 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17753 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17754 {
17755 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17756 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17757 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17758 TREE_TYPE (decl), lhs,
17759 RECUR (TREE_OPERAND (incr, 2))));
17760 }
17761 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17762 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17763 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17764 {
17765 tree rhs = TREE_OPERAND (incr, 2);
17766 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17767 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17768 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17769 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17770 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17771 rhs0, rhs1));
17772 }
17773 else
17774 incr = RECUR (incr);
17775 break;
17776 default:
17777 incr = RECUR (incr);
17778 break;
17779 }
17780
17781 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17782 TREE_VEC_ELT (orig_declv, i) = decl;
17783 TREE_VEC_ELT (declv, i) = decl;
17784 TREE_VEC_ELT (initv, i) = init;
17785 TREE_VEC_ELT (condv, i) = cond;
17786 TREE_VEC_ELT (incrv, i) = incr;
17787 return false;
17788 #undef RECUR
17789 }
17790
17791 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17792 of OMP_TARGET's body. */
17793
17794 static tree
17795 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17796 {
17797 *walk_subtrees = 0;
17798 switch (TREE_CODE (*tp))
17799 {
17800 case OMP_TEAMS:
17801 return *tp;
17802 case BIND_EXPR:
17803 case STATEMENT_LIST:
17804 *walk_subtrees = 1;
17805 break;
17806 default:
17807 break;
17808 }
17809 return NULL_TREE;
17810 }
17811
17812 /* Helper function for tsubst_expr. For decomposition declaration
17813 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17814 also the corresponding decls representing the identifiers
17815 of the decomposition declaration. Return DECL if successful
17816 or error_mark_node otherwise, set *FIRST to the first decl
17817 in the list chained through DECL_CHAIN and *CNT to the number
17818 of such decls. */
17819
17820 static tree
17821 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17822 tsubst_flags_t complain, tree in_decl, tree *first,
17823 unsigned int *cnt)
17824 {
17825 tree decl2, decl3, prev = decl;
17826 *cnt = 0;
17827 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17828 for (decl2 = DECL_CHAIN (pattern_decl);
17829 decl2
17830 && VAR_P (decl2)
17831 && DECL_DECOMPOSITION_P (decl2)
17832 && DECL_NAME (decl2);
17833 decl2 = DECL_CHAIN (decl2))
17834 {
17835 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17836 {
17837 gcc_assert (errorcount);
17838 return error_mark_node;
17839 }
17840 (*cnt)++;
17841 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17842 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17843 tree v = DECL_VALUE_EXPR (decl2);
17844 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17845 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17846 decl3 = tsubst (decl2, args, complain, in_decl);
17847 SET_DECL_VALUE_EXPR (decl2, v);
17848 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17849 if (VAR_P (decl3))
17850 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17851 else
17852 {
17853 gcc_assert (errorcount);
17854 decl = error_mark_node;
17855 continue;
17856 }
17857 maybe_push_decl (decl3);
17858 if (error_operand_p (decl3))
17859 decl = error_mark_node;
17860 else if (decl != error_mark_node
17861 && DECL_CHAIN (decl3) != prev
17862 && decl != prev)
17863 {
17864 gcc_assert (errorcount);
17865 decl = error_mark_node;
17866 }
17867 else
17868 prev = decl3;
17869 }
17870 *first = prev;
17871 return decl;
17872 }
17873
17874 /* Return the proper local_specialization for init-capture pack DECL. */
17875
17876 static tree
17877 lookup_init_capture_pack (tree decl)
17878 {
17879 /* We handle normal pack captures by forwarding to the specialization of the
17880 captured parameter. We can't do that for pack init-captures; we need them
17881 to have their own local_specialization. We created the individual
17882 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17883 when we process the DECL_EXPR for the pack init-capture in the template.
17884 So, how do we find them? We don't know the capture proxy pack when
17885 building the individual resulting proxies, and we don't know the
17886 individual proxies when instantiating the pack. What we have in common is
17887 the FIELD_DECL.
17888
17889 So...when we instantiate the FIELD_DECL, we stick the result in
17890 local_specializations. Then at the DECL_EXPR we look up that result, see
17891 how many elements it has, synthesize the names, and look them up. */
17892
17893 tree cname = DECL_NAME (decl);
17894 tree val = DECL_VALUE_EXPR (decl);
17895 tree field = TREE_OPERAND (val, 1);
17896 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17897 tree fpack = retrieve_local_specialization (field);
17898 if (fpack == error_mark_node)
17899 return error_mark_node;
17900
17901 int len = 1;
17902 tree vec = NULL_TREE;
17903 tree r = NULL_TREE;
17904 if (TREE_CODE (fpack) == TREE_VEC)
17905 {
17906 len = TREE_VEC_LENGTH (fpack);
17907 vec = make_tree_vec (len);
17908 r = make_node (NONTYPE_ARGUMENT_PACK);
17909 SET_ARGUMENT_PACK_ARGS (r, vec);
17910 }
17911 for (int i = 0; i < len; ++i)
17912 {
17913 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17914 tree elt = lookup_name (ename);
17915 if (vec)
17916 TREE_VEC_ELT (vec, i) = elt;
17917 else
17918 r = elt;
17919 }
17920 return r;
17921 }
17922
17923 /* Like tsubst_copy for expressions, etc. but also does semantic
17924 processing. */
17925
17926 tree
17927 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17928 bool integral_constant_expression_p)
17929 {
17930 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17931 #define RECUR(NODE) \
17932 tsubst_expr ((NODE), args, complain, in_decl, \
17933 integral_constant_expression_p)
17934
17935 tree stmt, tmp;
17936 tree r;
17937 location_t loc;
17938
17939 if (t == NULL_TREE || t == error_mark_node)
17940 return t;
17941
17942 loc = input_location;
17943 if (location_t eloc = cp_expr_location (t))
17944 input_location = eloc;
17945 if (STATEMENT_CODE_P (TREE_CODE (t)))
17946 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17947
17948 switch (TREE_CODE (t))
17949 {
17950 case STATEMENT_LIST:
17951 {
17952 tree_stmt_iterator i;
17953 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17954 RECUR (tsi_stmt (i));
17955 break;
17956 }
17957
17958 case CTOR_INITIALIZER:
17959 finish_mem_initializers (tsubst_initializer_list
17960 (TREE_OPERAND (t, 0), args));
17961 break;
17962
17963 case RETURN_EXPR:
17964 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17965 break;
17966
17967 case CO_RETURN_EXPR:
17968 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17969 break;
17970
17971 case CO_YIELD_EXPR:
17972 stmt = finish_co_yield_expr (input_location,
17973 RECUR (TREE_OPERAND (t, 0)));
17974 RETURN (stmt);
17975 break;
17976
17977 case CO_AWAIT_EXPR:
17978 stmt = finish_co_await_expr (input_location,
17979 RECUR (TREE_OPERAND (t, 0)));
17980 RETURN (stmt);
17981 break;
17982
17983 case EXPR_STMT:
17984 tmp = RECUR (EXPR_STMT_EXPR (t));
17985 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17986 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17987 else
17988 finish_expr_stmt (tmp);
17989 break;
17990
17991 case USING_STMT:
17992 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17993 break;
17994
17995 case DECL_EXPR:
17996 {
17997 tree decl, pattern_decl;
17998 tree init;
17999
18000 pattern_decl = decl = DECL_EXPR_DECL (t);
18001 if (TREE_CODE (decl) == LABEL_DECL)
18002 finish_label_decl (DECL_NAME (decl));
18003 else if (TREE_CODE (decl) == USING_DECL)
18004 {
18005 tree scope = USING_DECL_SCOPE (decl);
18006 tree name = DECL_NAME (decl);
18007
18008 scope = tsubst (scope, args, complain, in_decl);
18009 finish_nonmember_using_decl (scope, name);
18010 }
18011 else if (is_capture_proxy (decl)
18012 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18013 {
18014 /* We're in tsubst_lambda_expr, we've already inserted a new
18015 capture proxy, so look it up and register it. */
18016 tree inst;
18017 if (!DECL_PACK_P (decl))
18018 {
18019 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18020 LOOK_want::HIDDEN_LAMBDA);
18021 gcc_assert (inst != decl && is_capture_proxy (inst));
18022 }
18023 else if (is_normal_capture_proxy (decl))
18024 {
18025 inst = (retrieve_local_specialization
18026 (DECL_CAPTURED_VARIABLE (decl)));
18027 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18028 || DECL_PACK_P (inst));
18029 }
18030 else
18031 inst = lookup_init_capture_pack (decl);
18032
18033 register_local_specialization (inst, decl);
18034 break;
18035 }
18036 else if (DECL_PRETTY_FUNCTION_P (decl))
18037 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18038 DECL_NAME (decl),
18039 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18040 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18041 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18042 /* Don't copy the old closure; we'll create a new one in
18043 tsubst_lambda_expr. */
18044 break;
18045 else
18046 {
18047 init = DECL_INITIAL (decl);
18048 decl = tsubst (decl, args, complain, in_decl);
18049 if (decl != error_mark_node)
18050 {
18051 /* By marking the declaration as instantiated, we avoid
18052 trying to instantiate it. Since instantiate_decl can't
18053 handle local variables, and since we've already done
18054 all that needs to be done, that's the right thing to
18055 do. */
18056 if (VAR_P (decl))
18057 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18058 if (VAR_P (decl) && !DECL_NAME (decl)
18059 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18060 /* Anonymous aggregates are a special case. */
18061 finish_anon_union (decl);
18062 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18063 {
18064 DECL_CONTEXT (decl) = current_function_decl;
18065 if (DECL_NAME (decl) == this_identifier)
18066 {
18067 tree lam = DECL_CONTEXT (current_function_decl);
18068 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18069 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18070 }
18071 insert_capture_proxy (decl);
18072 }
18073 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18074 /* We already did a pushtag. */;
18075 else if (TREE_CODE (decl) == FUNCTION_DECL
18076 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18077 && DECL_FUNCTION_SCOPE_P (pattern_decl))
18078 {
18079 /* We pretend this is regular local extern decl of
18080 a namespace-scope fn. Then we make it really
18081 local, it is a nested function. */
18082 gcc_checking_assert (DECL_LOCAL_DECL_P (decl));
18083 DECL_CONTEXT (decl) = global_namespace;
18084 pushdecl (decl);
18085 DECL_CONTEXT (decl) = current_function_decl;
18086 cp_check_omp_declare_reduction (decl);
18087 }
18088 else
18089 {
18090 bool const_init = false;
18091 unsigned int cnt = 0;
18092 tree first = NULL_TREE, ndecl = error_mark_node;
18093 maybe_push_decl (decl);
18094
18095 if (VAR_P (decl)
18096 && DECL_DECOMPOSITION_P (decl)
18097 && TREE_TYPE (pattern_decl) != error_mark_node)
18098 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18099 complain, in_decl, &first,
18100 &cnt);
18101
18102 init = tsubst_init (init, decl, args, complain, in_decl);
18103
18104 if (VAR_P (decl))
18105 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18106 (pattern_decl));
18107
18108 if (ndecl != error_mark_node)
18109 cp_maybe_mangle_decomp (ndecl, first, cnt);
18110
18111 /* In a non-template function, VLA type declarations are
18112 handled in grokdeclarator; for templates, handle them
18113 now. */
18114 predeclare_vla (decl);
18115
18116 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18117
18118 if (ndecl != error_mark_node)
18119 cp_finish_decomp (ndecl, first, cnt);
18120 }
18121 }
18122 }
18123
18124 break;
18125 }
18126
18127 case FOR_STMT:
18128 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18129 RECUR (FOR_INIT_STMT (t));
18130 finish_init_stmt (stmt);
18131 tmp = RECUR (FOR_COND (t));
18132 finish_for_cond (tmp, stmt, false, 0);
18133 tmp = RECUR (FOR_EXPR (t));
18134 finish_for_expr (tmp, stmt);
18135 {
18136 bool prev = note_iteration_stmt_body_start ();
18137 RECUR (FOR_BODY (t));
18138 note_iteration_stmt_body_end (prev);
18139 }
18140 finish_for_stmt (stmt);
18141 break;
18142
18143 case RANGE_FOR_STMT:
18144 {
18145 /* Construct another range_for, if this is not a final
18146 substitution (for inside a generic lambda of a
18147 template). Otherwise convert to a regular for. */
18148 tree decl, expr;
18149 stmt = (processing_template_decl
18150 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18151 : begin_for_stmt (NULL_TREE, NULL_TREE));
18152 RECUR (RANGE_FOR_INIT_STMT (t));
18153 decl = RANGE_FOR_DECL (t);
18154 decl = tsubst (decl, args, complain, in_decl);
18155 maybe_push_decl (decl);
18156 expr = RECUR (RANGE_FOR_EXPR (t));
18157
18158 tree decomp_first = NULL_TREE;
18159 unsigned decomp_cnt = 0;
18160 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18161 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18162 complain, in_decl,
18163 &decomp_first, &decomp_cnt);
18164
18165 if (processing_template_decl)
18166 {
18167 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18168 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18169 finish_range_for_decl (stmt, decl, expr);
18170 if (decomp_first && decl != error_mark_node)
18171 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18172 }
18173 else
18174 {
18175 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18176 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18177 stmt = cp_convert_range_for (stmt, decl, expr,
18178 decomp_first, decomp_cnt,
18179 RANGE_FOR_IVDEP (t), unroll);
18180 }
18181
18182 bool prev = note_iteration_stmt_body_start ();
18183 RECUR (RANGE_FOR_BODY (t));
18184 note_iteration_stmt_body_end (prev);
18185 finish_for_stmt (stmt);
18186 }
18187 break;
18188
18189 case WHILE_STMT:
18190 stmt = begin_while_stmt ();
18191 tmp = RECUR (WHILE_COND (t));
18192 finish_while_stmt_cond (tmp, stmt, false, 0);
18193 {
18194 bool prev = note_iteration_stmt_body_start ();
18195 RECUR (WHILE_BODY (t));
18196 note_iteration_stmt_body_end (prev);
18197 }
18198 finish_while_stmt (stmt);
18199 break;
18200
18201 case DO_STMT:
18202 stmt = begin_do_stmt ();
18203 {
18204 bool prev = note_iteration_stmt_body_start ();
18205 RECUR (DO_BODY (t));
18206 note_iteration_stmt_body_end (prev);
18207 }
18208 finish_do_body (stmt);
18209 tmp = RECUR (DO_COND (t));
18210 finish_do_stmt (tmp, stmt, false, 0);
18211 break;
18212
18213 case IF_STMT:
18214 stmt = begin_if_stmt ();
18215 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18216 if (IF_STMT_CONSTEXPR_P (t))
18217 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18218 tmp = RECUR (IF_COND (t));
18219 tmp = finish_if_stmt_cond (tmp, stmt);
18220 if (IF_STMT_CONSTEXPR_P (t)
18221 && instantiation_dependent_expression_p (tmp))
18222 {
18223 /* We're partially instantiating a generic lambda, but the condition
18224 of the constexpr if is still dependent. Don't substitute into the
18225 branches now, just remember the template arguments. */
18226 do_poplevel (IF_SCOPE (stmt));
18227 IF_COND (stmt) = IF_COND (t);
18228 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18229 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18230 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18231 add_stmt (stmt);
18232 break;
18233 }
18234 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18235 /* Don't instantiate the THEN_CLAUSE. */;
18236 else
18237 {
18238 tree folded = fold_non_dependent_expr (tmp, complain);
18239 bool inhibit = integer_zerop (folded);
18240 if (inhibit)
18241 ++c_inhibit_evaluation_warnings;
18242 RECUR (THEN_CLAUSE (t));
18243 if (inhibit)
18244 --c_inhibit_evaluation_warnings;
18245 }
18246 finish_then_clause (stmt);
18247
18248 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18249 /* Don't instantiate the ELSE_CLAUSE. */;
18250 else if (ELSE_CLAUSE (t))
18251 {
18252 tree folded = fold_non_dependent_expr (tmp, complain);
18253 bool inhibit = integer_nonzerop (folded);
18254 begin_else_clause (stmt);
18255 if (inhibit)
18256 ++c_inhibit_evaluation_warnings;
18257 RECUR (ELSE_CLAUSE (t));
18258 if (inhibit)
18259 --c_inhibit_evaluation_warnings;
18260 finish_else_clause (stmt);
18261 }
18262
18263 finish_if_stmt (stmt);
18264 break;
18265
18266 case BIND_EXPR:
18267 if (BIND_EXPR_BODY_BLOCK (t))
18268 stmt = begin_function_body ();
18269 else
18270 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18271 ? BCS_TRY_BLOCK : 0);
18272
18273 RECUR (BIND_EXPR_BODY (t));
18274
18275 if (BIND_EXPR_BODY_BLOCK (t))
18276 finish_function_body (stmt);
18277 else
18278 finish_compound_stmt (stmt);
18279 break;
18280
18281 case BREAK_STMT:
18282 finish_break_stmt ();
18283 break;
18284
18285 case CONTINUE_STMT:
18286 finish_continue_stmt ();
18287 break;
18288
18289 case SWITCH_STMT:
18290 stmt = begin_switch_stmt ();
18291 tmp = RECUR (SWITCH_STMT_COND (t));
18292 finish_switch_cond (tmp, stmt);
18293 RECUR (SWITCH_STMT_BODY (t));
18294 finish_switch_stmt (stmt);
18295 break;
18296
18297 case CASE_LABEL_EXPR:
18298 {
18299 tree decl = CASE_LABEL (t);
18300 tree low = RECUR (CASE_LOW (t));
18301 tree high = RECUR (CASE_HIGH (t));
18302 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18303 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18304 {
18305 tree label = CASE_LABEL (l);
18306 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18307 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18308 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18309 }
18310 }
18311 break;
18312
18313 case LABEL_EXPR:
18314 {
18315 tree decl = LABEL_EXPR_LABEL (t);
18316 tree label;
18317
18318 label = finish_label_stmt (DECL_NAME (decl));
18319 if (TREE_CODE (label) == LABEL_DECL)
18320 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18321 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18322 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18323 }
18324 break;
18325
18326 case GOTO_EXPR:
18327 tmp = GOTO_DESTINATION (t);
18328 if (TREE_CODE (tmp) != LABEL_DECL)
18329 /* Computed goto's must be tsubst'd into. On the other hand,
18330 non-computed gotos must not be; the identifier in question
18331 will have no binding. */
18332 tmp = RECUR (tmp);
18333 else
18334 tmp = DECL_NAME (tmp);
18335 finish_goto_stmt (tmp);
18336 break;
18337
18338 case ASM_EXPR:
18339 {
18340 tree string = RECUR (ASM_STRING (t));
18341 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18342 complain, in_decl);
18343 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18344 complain, in_decl);
18345 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18346 complain, in_decl);
18347 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18348 complain, in_decl);
18349 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18350 outputs, inputs, clobbers, labels,
18351 ASM_INLINE_P (t));
18352 tree asm_expr = tmp;
18353 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18354 asm_expr = TREE_OPERAND (asm_expr, 0);
18355 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18356 }
18357 break;
18358
18359 case TRY_BLOCK:
18360 if (CLEANUP_P (t))
18361 {
18362 stmt = begin_try_block ();
18363 RECUR (TRY_STMTS (t));
18364 finish_cleanup_try_block (stmt);
18365 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18366 }
18367 else
18368 {
18369 tree compound_stmt = NULL_TREE;
18370
18371 if (FN_TRY_BLOCK_P (t))
18372 stmt = begin_function_try_block (&compound_stmt);
18373 else
18374 stmt = begin_try_block ();
18375
18376 RECUR (TRY_STMTS (t));
18377
18378 if (FN_TRY_BLOCK_P (t))
18379 finish_function_try_block (stmt);
18380 else
18381 finish_try_block (stmt);
18382
18383 RECUR (TRY_HANDLERS (t));
18384 if (FN_TRY_BLOCK_P (t))
18385 finish_function_handler_sequence (stmt, compound_stmt);
18386 else
18387 finish_handler_sequence (stmt);
18388 }
18389 break;
18390
18391 case HANDLER:
18392 {
18393 tree decl = HANDLER_PARMS (t);
18394
18395 if (decl)
18396 {
18397 decl = tsubst (decl, args, complain, in_decl);
18398 /* Prevent instantiate_decl from trying to instantiate
18399 this variable. We've already done all that needs to be
18400 done. */
18401 if (decl != error_mark_node)
18402 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18403 }
18404 stmt = begin_handler ();
18405 finish_handler_parms (decl, stmt);
18406 RECUR (HANDLER_BODY (t));
18407 finish_handler (stmt);
18408 }
18409 break;
18410
18411 case TAG_DEFN:
18412 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18413 if (CLASS_TYPE_P (tmp))
18414 {
18415 /* Local classes are not independent templates; they are
18416 instantiated along with their containing function. And this
18417 way we don't have to deal with pushing out of one local class
18418 to instantiate a member of another local class. */
18419 /* Closures are handled by the LAMBDA_EXPR. */
18420 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18421 complete_type (tmp);
18422 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18423 if ((VAR_P (fld)
18424 || (TREE_CODE (fld) == FUNCTION_DECL
18425 && !DECL_ARTIFICIAL (fld)))
18426 && DECL_TEMPLATE_INSTANTIATION (fld))
18427 instantiate_decl (fld, /*defer_ok=*/false,
18428 /*expl_inst_class=*/false);
18429 }
18430 break;
18431
18432 case STATIC_ASSERT:
18433 {
18434 tree condition;
18435
18436 ++c_inhibit_evaluation_warnings;
18437 condition =
18438 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18439 args,
18440 complain, in_decl,
18441 /*integral_constant_expression_p=*/true);
18442 --c_inhibit_evaluation_warnings;
18443
18444 finish_static_assert (condition,
18445 STATIC_ASSERT_MESSAGE (t),
18446 STATIC_ASSERT_SOURCE_LOCATION (t),
18447 /*member_p=*/false);
18448 }
18449 break;
18450
18451 case OACC_KERNELS:
18452 case OACC_PARALLEL:
18453 case OACC_SERIAL:
18454 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18455 in_decl);
18456 stmt = begin_omp_parallel ();
18457 RECUR (OMP_BODY (t));
18458 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18459 break;
18460
18461 case OMP_PARALLEL:
18462 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18463 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18464 complain, in_decl);
18465 if (OMP_PARALLEL_COMBINED (t))
18466 omp_parallel_combined_clauses = &tmp;
18467 stmt = begin_omp_parallel ();
18468 RECUR (OMP_PARALLEL_BODY (t));
18469 gcc_assert (omp_parallel_combined_clauses == NULL);
18470 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18471 = OMP_PARALLEL_COMBINED (t);
18472 pop_omp_privatization_clauses (r);
18473 break;
18474
18475 case OMP_TASK:
18476 if (OMP_TASK_BODY (t) == NULL_TREE)
18477 {
18478 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18479 complain, in_decl);
18480 t = copy_node (t);
18481 OMP_TASK_CLAUSES (t) = tmp;
18482 add_stmt (t);
18483 break;
18484 }
18485 r = push_omp_privatization_clauses (false);
18486 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18487 complain, in_decl);
18488 stmt = begin_omp_task ();
18489 RECUR (OMP_TASK_BODY (t));
18490 finish_omp_task (tmp, stmt);
18491 pop_omp_privatization_clauses (r);
18492 break;
18493
18494 case OMP_FOR:
18495 case OMP_LOOP:
18496 case OMP_SIMD:
18497 case OMP_DISTRIBUTE:
18498 case OMP_TASKLOOP:
18499 case OACC_LOOP:
18500 {
18501 tree clauses, body, pre_body;
18502 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18503 tree orig_declv = NULL_TREE;
18504 tree incrv = NULL_TREE;
18505 enum c_omp_region_type ort = C_ORT_OMP;
18506 bool any_range_for = false;
18507 int i;
18508
18509 if (TREE_CODE (t) == OACC_LOOP)
18510 ort = C_ORT_ACC;
18511
18512 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18513 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18514 in_decl);
18515 if (OMP_FOR_INIT (t) != NULL_TREE)
18516 {
18517 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18518 if (OMP_FOR_ORIG_DECLS (t))
18519 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18520 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18521 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18522 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18523 }
18524
18525 keep_next_level (true);
18526 stmt = begin_omp_structured_block ();
18527
18528 pre_body = push_stmt_list ();
18529 RECUR (OMP_FOR_PRE_BODY (t));
18530 pre_body = pop_stmt_list (pre_body);
18531
18532 if (OMP_FOR_INIT (t) != NULL_TREE)
18533 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18534 any_range_for
18535 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18536 condv, incrv, &clauses, args,
18537 complain, in_decl,
18538 integral_constant_expression_p);
18539 omp_parallel_combined_clauses = NULL;
18540
18541 if (any_range_for)
18542 {
18543 gcc_assert (orig_declv);
18544 body = begin_omp_structured_block ();
18545 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18546 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18547 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18548 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18549 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18550 TREE_VEC_ELT (declv, i));
18551 }
18552 else
18553 body = push_stmt_list ();
18554 RECUR (OMP_FOR_BODY (t));
18555 if (any_range_for)
18556 body = finish_omp_structured_block (body);
18557 else
18558 body = pop_stmt_list (body);
18559
18560 if (OMP_FOR_INIT (t) != NULL_TREE)
18561 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18562 orig_declv, initv, condv, incrv, body, pre_body,
18563 NULL, clauses);
18564 else
18565 {
18566 t = make_node (TREE_CODE (t));
18567 TREE_TYPE (t) = void_type_node;
18568 OMP_FOR_BODY (t) = body;
18569 OMP_FOR_PRE_BODY (t) = pre_body;
18570 OMP_FOR_CLAUSES (t) = clauses;
18571 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18572 add_stmt (t);
18573 }
18574
18575 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18576 t));
18577 pop_omp_privatization_clauses (r);
18578 }
18579 break;
18580
18581 case OMP_SECTIONS:
18582 omp_parallel_combined_clauses = NULL;
18583 /* FALLTHRU */
18584 case OMP_SINGLE:
18585 case OMP_TEAMS:
18586 case OMP_CRITICAL:
18587 case OMP_TASKGROUP:
18588 case OMP_SCAN:
18589 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18590 && OMP_TEAMS_COMBINED (t));
18591 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18592 in_decl);
18593 if (TREE_CODE (t) == OMP_TEAMS)
18594 {
18595 keep_next_level (true);
18596 stmt = begin_omp_structured_block ();
18597 RECUR (OMP_BODY (t));
18598 stmt = finish_omp_structured_block (stmt);
18599 }
18600 else
18601 {
18602 stmt = push_stmt_list ();
18603 RECUR (OMP_BODY (t));
18604 stmt = pop_stmt_list (stmt);
18605 }
18606
18607 if (TREE_CODE (t) == OMP_CRITICAL
18608 && tmp != NULL_TREE
18609 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18610 {
18611 error_at (OMP_CLAUSE_LOCATION (tmp),
18612 "%<#pragma omp critical%> with %<hint%> clause requires "
18613 "a name, except when %<omp_sync_hint_none%> is used");
18614 RETURN (error_mark_node);
18615 }
18616 t = copy_node (t);
18617 OMP_BODY (t) = stmt;
18618 OMP_CLAUSES (t) = tmp;
18619 add_stmt (t);
18620 pop_omp_privatization_clauses (r);
18621 break;
18622
18623 case OMP_DEPOBJ:
18624 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18625 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18626 {
18627 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18628 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18629 {
18630 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18631 args, complain, in_decl);
18632 if (tmp == NULL_TREE)
18633 tmp = error_mark_node;
18634 }
18635 else
18636 {
18637 kind = (enum omp_clause_depend_kind)
18638 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18639 tmp = NULL_TREE;
18640 }
18641 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18642 }
18643 else
18644 finish_omp_depobj (EXPR_LOCATION (t), r,
18645 OMP_CLAUSE_DEPEND_SOURCE,
18646 OMP_DEPOBJ_CLAUSES (t));
18647 break;
18648
18649 case OACC_DATA:
18650 case OMP_TARGET_DATA:
18651 case OMP_TARGET:
18652 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18653 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18654 in_decl);
18655 keep_next_level (true);
18656 stmt = begin_omp_structured_block ();
18657
18658 RECUR (OMP_BODY (t));
18659 stmt = finish_omp_structured_block (stmt);
18660
18661 t = copy_node (t);
18662 OMP_BODY (t) = stmt;
18663 OMP_CLAUSES (t) = tmp;
18664 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18665 {
18666 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18667 if (teams)
18668 {
18669 /* For combined target teams, ensure the num_teams and
18670 thread_limit clause expressions are evaluated on the host,
18671 before entering the target construct. */
18672 tree c;
18673 for (c = OMP_TEAMS_CLAUSES (teams);
18674 c; c = OMP_CLAUSE_CHAIN (c))
18675 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18676 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18677 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18678 {
18679 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18680 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18681 if (expr == error_mark_node)
18682 continue;
18683 tmp = TARGET_EXPR_SLOT (expr);
18684 add_stmt (expr);
18685 OMP_CLAUSE_OPERAND (c, 0) = expr;
18686 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18687 OMP_CLAUSE_FIRSTPRIVATE);
18688 OMP_CLAUSE_DECL (tc) = tmp;
18689 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18690 OMP_TARGET_CLAUSES (t) = tc;
18691 }
18692 }
18693 }
18694 add_stmt (t);
18695 break;
18696
18697 case OACC_DECLARE:
18698 t = copy_node (t);
18699 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18700 complain, in_decl);
18701 OACC_DECLARE_CLAUSES (t) = tmp;
18702 add_stmt (t);
18703 break;
18704
18705 case OMP_TARGET_UPDATE:
18706 case OMP_TARGET_ENTER_DATA:
18707 case OMP_TARGET_EXIT_DATA:
18708 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18709 complain, in_decl);
18710 t = copy_node (t);
18711 OMP_STANDALONE_CLAUSES (t) = tmp;
18712 add_stmt (t);
18713 break;
18714
18715 case OACC_ENTER_DATA:
18716 case OACC_EXIT_DATA:
18717 case OACC_UPDATE:
18718 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18719 complain, in_decl);
18720 t = copy_node (t);
18721 OMP_STANDALONE_CLAUSES (t) = tmp;
18722 add_stmt (t);
18723 break;
18724
18725 case OMP_ORDERED:
18726 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18727 complain, in_decl);
18728 stmt = push_stmt_list ();
18729 RECUR (OMP_BODY (t));
18730 stmt = pop_stmt_list (stmt);
18731
18732 t = copy_node (t);
18733 OMP_BODY (t) = stmt;
18734 OMP_ORDERED_CLAUSES (t) = tmp;
18735 add_stmt (t);
18736 break;
18737
18738 case OMP_MASTER:
18739 omp_parallel_combined_clauses = NULL;
18740 /* FALLTHRU */
18741 case OMP_SECTION:
18742 stmt = push_stmt_list ();
18743 RECUR (OMP_BODY (t));
18744 stmt = pop_stmt_list (stmt);
18745
18746 t = copy_node (t);
18747 OMP_BODY (t) = stmt;
18748 add_stmt (t);
18749 break;
18750
18751 case OMP_ATOMIC:
18752 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18753 tmp = NULL_TREE;
18754 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18755 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18756 complain, in_decl);
18757 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18758 {
18759 tree op1 = TREE_OPERAND (t, 1);
18760 tree rhs1 = NULL_TREE;
18761 tree lhs, rhs;
18762 if (TREE_CODE (op1) == COMPOUND_EXPR)
18763 {
18764 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18765 op1 = TREE_OPERAND (op1, 1);
18766 }
18767 lhs = RECUR (TREE_OPERAND (op1, 0));
18768 rhs = RECUR (TREE_OPERAND (op1, 1));
18769 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18770 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18771 OMP_ATOMIC_MEMORY_ORDER (t));
18772 }
18773 else
18774 {
18775 tree op1 = TREE_OPERAND (t, 1);
18776 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18777 tree rhs1 = NULL_TREE;
18778 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18779 enum tree_code opcode = NOP_EXPR;
18780 if (code == OMP_ATOMIC_READ)
18781 {
18782 v = RECUR (TREE_OPERAND (op1, 0));
18783 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18784 }
18785 else if (code == OMP_ATOMIC_CAPTURE_OLD
18786 || code == OMP_ATOMIC_CAPTURE_NEW)
18787 {
18788 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18789 v = RECUR (TREE_OPERAND (op1, 0));
18790 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18791 if (TREE_CODE (op11) == COMPOUND_EXPR)
18792 {
18793 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18794 op11 = TREE_OPERAND (op11, 1);
18795 }
18796 lhs = RECUR (TREE_OPERAND (op11, 0));
18797 rhs = RECUR (TREE_OPERAND (op11, 1));
18798 opcode = TREE_CODE (op11);
18799 if (opcode == MODIFY_EXPR)
18800 opcode = NOP_EXPR;
18801 }
18802 else
18803 {
18804 code = OMP_ATOMIC;
18805 lhs = RECUR (TREE_OPERAND (op1, 0));
18806 rhs = RECUR (TREE_OPERAND (op1, 1));
18807 }
18808 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18809 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18810 }
18811 break;
18812
18813 case TRANSACTION_EXPR:
18814 {
18815 int flags = 0;
18816 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18817 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18818
18819 if (TRANSACTION_EXPR_IS_STMT (t))
18820 {
18821 tree body = TRANSACTION_EXPR_BODY (t);
18822 tree noex = NULL_TREE;
18823 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18824 {
18825 noex = MUST_NOT_THROW_COND (body);
18826 if (noex == NULL_TREE)
18827 noex = boolean_true_node;
18828 body = TREE_OPERAND (body, 0);
18829 }
18830 stmt = begin_transaction_stmt (input_location, NULL, flags);
18831 RECUR (body);
18832 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18833 }
18834 else
18835 {
18836 stmt = build_transaction_expr (EXPR_LOCATION (t),
18837 RECUR (TRANSACTION_EXPR_BODY (t)),
18838 flags, NULL_TREE);
18839 RETURN (stmt);
18840 }
18841 }
18842 break;
18843
18844 case MUST_NOT_THROW_EXPR:
18845 {
18846 tree op0 = RECUR (TREE_OPERAND (t, 0));
18847 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18848 RETURN (build_must_not_throw_expr (op0, cond));
18849 }
18850
18851 case EXPR_PACK_EXPANSION:
18852 error ("invalid use of pack expansion expression");
18853 RETURN (error_mark_node);
18854
18855 case NONTYPE_ARGUMENT_PACK:
18856 error ("use %<...%> to expand argument pack");
18857 RETURN (error_mark_node);
18858
18859 case COMPOUND_EXPR:
18860 tmp = RECUR (TREE_OPERAND (t, 0));
18861 if (tmp == NULL_TREE)
18862 /* If the first operand was a statement, we're done with it. */
18863 RETURN (RECUR (TREE_OPERAND (t, 1)));
18864 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18865 RECUR (TREE_OPERAND (t, 1)),
18866 complain));
18867
18868 case ANNOTATE_EXPR:
18869 tmp = RECUR (TREE_OPERAND (t, 0));
18870 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18871 TREE_TYPE (tmp), tmp,
18872 RECUR (TREE_OPERAND (t, 1)),
18873 RECUR (TREE_OPERAND (t, 2))));
18874
18875 case PREDICT_EXPR:
18876 RETURN (add_stmt (copy_node (t)));
18877
18878 default:
18879 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18880
18881 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18882 /*function_p=*/false,
18883 integral_constant_expression_p));
18884 }
18885
18886 RETURN (NULL_TREE);
18887 out:
18888 input_location = loc;
18889 return r;
18890 #undef RECUR
18891 #undef RETURN
18892 }
18893
18894 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18895 function. For description of the body see comment above
18896 cp_parser_omp_declare_reduction_exprs. */
18897
18898 static void
18899 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18900 {
18901 if (t == NULL_TREE || t == error_mark_node)
18902 return;
18903
18904 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
18905
18906 tree_stmt_iterator tsi;
18907 int i;
18908 tree stmts[7];
18909 memset (stmts, 0, sizeof stmts);
18910 for (i = 0, tsi = tsi_start (t);
18911 i < 7 && !tsi_end_p (tsi);
18912 i++, tsi_next (&tsi))
18913 stmts[i] = tsi_stmt (tsi);
18914 gcc_assert (tsi_end_p (tsi));
18915
18916 if (i >= 3)
18917 {
18918 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18919 && TREE_CODE (stmts[1]) == DECL_EXPR);
18920 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18921 args, complain, in_decl);
18922 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18923 args, complain, in_decl);
18924 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18925 expect to be pushing it. */
18926 DECL_CONTEXT (omp_out) = current_function_decl;
18927 DECL_CONTEXT (omp_in) = current_function_decl;
18928 keep_next_level (true);
18929 tree block = begin_omp_structured_block ();
18930 tsubst_expr (stmts[2], args, complain, in_decl, false);
18931 block = finish_omp_structured_block (block);
18932 block = maybe_cleanup_point_expr_void (block);
18933 add_decl_expr (omp_out);
18934 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18935 TREE_NO_WARNING (omp_out) = 1;
18936 add_decl_expr (omp_in);
18937 finish_expr_stmt (block);
18938 }
18939 if (i >= 6)
18940 {
18941 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18942 && TREE_CODE (stmts[4]) == DECL_EXPR);
18943 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18944 args, complain, in_decl);
18945 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18946 args, complain, in_decl);
18947 DECL_CONTEXT (omp_priv) = current_function_decl;
18948 DECL_CONTEXT (omp_orig) = current_function_decl;
18949 keep_next_level (true);
18950 tree block = begin_omp_structured_block ();
18951 tsubst_expr (stmts[5], args, complain, in_decl, false);
18952 block = finish_omp_structured_block (block);
18953 block = maybe_cleanup_point_expr_void (block);
18954 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18955 add_decl_expr (omp_priv);
18956 add_decl_expr (omp_orig);
18957 finish_expr_stmt (block);
18958 if (i == 7)
18959 add_decl_expr (omp_orig);
18960 }
18961 }
18962
18963 /* T is a postfix-expression that is not being used in a function
18964 call. Return the substituted version of T. */
18965
18966 static tree
18967 tsubst_non_call_postfix_expression (tree t, tree args,
18968 tsubst_flags_t complain,
18969 tree in_decl)
18970 {
18971 if (TREE_CODE (t) == SCOPE_REF)
18972 t = tsubst_qualified_id (t, args, complain, in_decl,
18973 /*done=*/false, /*address_p=*/false);
18974 else
18975 t = tsubst_copy_and_build (t, args, complain, in_decl,
18976 /*function_p=*/false,
18977 /*integral_constant_expression_p=*/false);
18978
18979 return t;
18980 }
18981
18982 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18983 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18984 dependent init-capture. */
18985
18986 static void
18987 prepend_one_capture (tree field, tree init, tree &list,
18988 tsubst_flags_t complain)
18989 {
18990 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18991 {
18992 tree type = NULL_TREE;
18993 if (!init)
18994 {
18995 if (complain & tf_error)
18996 error ("empty initializer in lambda init-capture");
18997 init = error_mark_node;
18998 }
18999 else if (TREE_CODE (init) == TREE_LIST)
19000 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19001 if (!type)
19002 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19003 TREE_TYPE (field) = type;
19004 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19005 }
19006 list = tree_cons (field, init, list);
19007 }
19008
19009 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19010 instantiation context. Instantiating a pack expansion containing a lambda
19011 might result in multiple lambdas all based on the same lambda in the
19012 template. */
19013
19014 tree
19015 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19016 {
19017 tree oldfn = lambda_function (t);
19018 in_decl = oldfn;
19019
19020 tree r = build_lambda_expr ();
19021
19022 LAMBDA_EXPR_LOCATION (r)
19023 = LAMBDA_EXPR_LOCATION (t);
19024 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19025 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19026 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19027 LAMBDA_EXPR_INSTANTIATED (r) = true;
19028
19029 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19030 /* A lambda in a default argument outside a class gets no
19031 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19032 tsubst_default_argument calls start_lambda_scope, so we need to
19033 specifically ignore it here, and use the global scope. */
19034 record_null_lambda_scope (r);
19035 else
19036 record_lambda_scope (r);
19037
19038 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19039 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19040
19041 vec<tree,va_gc>* field_packs = NULL;
19042
19043 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19044 cap = TREE_CHAIN (cap))
19045 {
19046 tree ofield = TREE_PURPOSE (cap);
19047 tree init = TREE_VALUE (cap);
19048 if (PACK_EXPANSION_P (init))
19049 init = tsubst_pack_expansion (init, args, complain, in_decl);
19050 else
19051 init = tsubst_copy_and_build (init, args, complain, in_decl,
19052 /*fn*/false, /*constexpr*/false);
19053
19054 if (init == error_mark_node)
19055 return error_mark_node;
19056
19057 if (init && TREE_CODE (init) == TREE_LIST)
19058 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19059
19060 if (!processing_template_decl
19061 && init && TREE_CODE (init) != TREE_VEC
19062 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19063 {
19064 /* For a VLA, simply tsubsting the field type won't work, we need to
19065 go through add_capture again. XXX do we want to do this for all
19066 captures? */
19067 tree name = (get_identifier
19068 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19069 tree ftype = TREE_TYPE (ofield);
19070 bool by_ref = (TYPE_REF_P (ftype)
19071 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19072 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19073 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19074 continue;
19075 }
19076
19077 if (PACK_EXPANSION_P (ofield))
19078 ofield = PACK_EXPANSION_PATTERN (ofield);
19079 tree field = tsubst_decl (ofield, args, complain);
19080
19081 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19082 {
19083 /* Remember these for when we've pushed local_specializations. */
19084 vec_safe_push (field_packs, ofield);
19085 vec_safe_push (field_packs, field);
19086 }
19087
19088 if (field == error_mark_node)
19089 return error_mark_node;
19090
19091 if (TREE_CODE (field) == TREE_VEC)
19092 {
19093 int len = TREE_VEC_LENGTH (field);
19094 gcc_assert (TREE_CODE (init) == TREE_VEC
19095 && TREE_VEC_LENGTH (init) == len);
19096 for (int i = 0; i < len; ++i)
19097 prepend_one_capture (TREE_VEC_ELT (field, i),
19098 TREE_VEC_ELT (init, i),
19099 LAMBDA_EXPR_CAPTURE_LIST (r),
19100 complain);
19101 }
19102 else
19103 {
19104 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19105 complain);
19106
19107 if (id_equal (DECL_NAME (field), "__this"))
19108 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19109 }
19110 }
19111
19112 tree type = begin_lambda_type (r);
19113 if (type == error_mark_node)
19114 return error_mark_node;
19115
19116 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19117 determine_visibility (TYPE_NAME (type));
19118
19119 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19120
19121 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19122 ? DECL_TI_TEMPLATE (oldfn)
19123 : NULL_TREE);
19124
19125 tree fntype = static_fn_type (oldfn);
19126 if (oldtmpl)
19127 ++processing_template_decl;
19128 fntype = tsubst (fntype, args, complain, in_decl);
19129 if (oldtmpl)
19130 --processing_template_decl;
19131
19132 if (fntype == error_mark_node)
19133 r = error_mark_node;
19134 else
19135 {
19136 /* The body of a lambda-expression is not a subexpression of the
19137 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19138 which would be skipped if cp_unevaluated_operand. */
19139 cp_evaluated ev;
19140
19141 /* Fix the type of 'this'. */
19142 fntype = build_memfn_type (fntype, type,
19143 type_memfn_quals (fntype),
19144 type_memfn_rqual (fntype));
19145 tree fn, tmpl;
19146 if (oldtmpl)
19147 {
19148 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19149 if (tmpl == error_mark_node)
19150 {
19151 r = error_mark_node;
19152 goto out;
19153 }
19154 fn = DECL_TEMPLATE_RESULT (tmpl);
19155 finish_member_declaration (tmpl);
19156 }
19157 else
19158 {
19159 tmpl = NULL_TREE;
19160 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19161 if (fn == error_mark_node)
19162 {
19163 r = error_mark_node;
19164 goto out;
19165 }
19166 finish_member_declaration (fn);
19167 }
19168
19169 if (tree ci = get_constraints (oldfn))
19170 {
19171 /* Substitute into the lambda's constraints. */
19172 if (oldtmpl)
19173 ++processing_template_decl;
19174 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19175 if (oldtmpl)
19176 --processing_template_decl;
19177 set_constraints (fn, ci);
19178 }
19179
19180 /* Let finish_function set this. */
19181 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19182
19183 bool nested = cfun;
19184 if (nested)
19185 push_function_context ();
19186 else
19187 /* Still increment function_depth so that we don't GC in the
19188 middle of an expression. */
19189 ++function_depth;
19190
19191 local_specialization_stack s (lss_copy);
19192
19193 tree body = start_lambda_function (fn, r);
19194
19195 /* Now record them for lookup_init_capture_pack. */
19196 int fplen = vec_safe_length (field_packs);
19197 for (int i = 0; i < fplen; )
19198 {
19199 tree pack = (*field_packs)[i++];
19200 tree inst = (*field_packs)[i++];
19201 register_local_specialization (inst, pack);
19202 }
19203 release_tree_vector (field_packs);
19204
19205 register_parameter_specializations (oldfn, fn);
19206
19207 if (oldtmpl)
19208 {
19209 /* We might not partially instantiate some parts of the function, so
19210 copy these flags from the original template. */
19211 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19212 current_function_returns_value = ol->returns_value;
19213 current_function_returns_null = ol->returns_null;
19214 current_function_returns_abnormally = ol->returns_abnormally;
19215 current_function_infinite_loop = ol->infinite_loop;
19216 }
19217
19218 /* [temp.deduct] A lambda-expression appearing in a function type or a
19219 template parameter is not considered part of the immediate context for
19220 the purposes of template argument deduction. */
19221 complain = tf_warning_or_error;
19222
19223 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19224 /*constexpr*/false);
19225
19226 finish_lambda_function (body);
19227
19228 if (nested)
19229 pop_function_context ();
19230 else
19231 --function_depth;
19232
19233 /* The capture list was built up in reverse order; fix that now. */
19234 LAMBDA_EXPR_CAPTURE_LIST (r)
19235 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19236
19237 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19238
19239 maybe_add_lambda_conv_op (type);
19240 }
19241
19242 out:
19243 finish_struct (type, /*attr*/NULL_TREE);
19244
19245 insert_pending_capture_proxies ();
19246
19247 return r;
19248 }
19249
19250 /* Like tsubst but deals with expressions and performs semantic
19251 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19252
19253 tree
19254 tsubst_copy_and_build (tree t,
19255 tree args,
19256 tsubst_flags_t complain,
19257 tree in_decl,
19258 bool function_p,
19259 bool integral_constant_expression_p)
19260 {
19261 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19262 #define RECUR(NODE) \
19263 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19264 /*function_p=*/false, \
19265 integral_constant_expression_p)
19266
19267 tree retval, op1;
19268 location_t save_loc;
19269
19270 if (t == NULL_TREE || t == error_mark_node)
19271 return t;
19272
19273 save_loc = input_location;
19274 if (location_t eloc = cp_expr_location (t))
19275 input_location = eloc;
19276
19277 /* N3276 decltype magic only applies to calls at the top level or on the
19278 right side of a comma. */
19279 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19280 complain &= ~tf_decltype;
19281
19282 switch (TREE_CODE (t))
19283 {
19284 case USING_DECL:
19285 t = DECL_NAME (t);
19286 /* Fall through. */
19287 case IDENTIFIER_NODE:
19288 {
19289 tree decl;
19290 cp_id_kind idk;
19291 bool non_integral_constant_expression_p;
19292 const char *error_msg;
19293
19294 if (IDENTIFIER_CONV_OP_P (t))
19295 {
19296 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19297 t = make_conv_op_name (new_type);
19298 }
19299
19300 /* Look up the name. */
19301 decl = lookup_name (t);
19302
19303 /* By convention, expressions use ERROR_MARK_NODE to indicate
19304 failure, not NULL_TREE. */
19305 if (decl == NULL_TREE)
19306 decl = error_mark_node;
19307
19308 decl = finish_id_expression (t, decl, NULL_TREE,
19309 &idk,
19310 integral_constant_expression_p,
19311 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19312 &non_integral_constant_expression_p,
19313 /*template_p=*/false,
19314 /*done=*/true,
19315 /*address_p=*/false,
19316 /*template_arg_p=*/false,
19317 &error_msg,
19318 input_location);
19319 if (error_msg)
19320 error (error_msg);
19321 if (!function_p && identifier_p (decl))
19322 {
19323 if (complain & tf_error)
19324 unqualified_name_lookup_error (decl);
19325 decl = error_mark_node;
19326 }
19327 RETURN (decl);
19328 }
19329
19330 case TEMPLATE_ID_EXPR:
19331 {
19332 tree object;
19333 tree templ = RECUR (TREE_OPERAND (t, 0));
19334 tree targs = TREE_OPERAND (t, 1);
19335
19336 if (targs)
19337 targs = tsubst_template_args (targs, args, complain, in_decl);
19338 if (targs == error_mark_node)
19339 RETURN (error_mark_node);
19340
19341 if (TREE_CODE (templ) == SCOPE_REF)
19342 {
19343 tree name = TREE_OPERAND (templ, 1);
19344 tree tid = lookup_template_function (name, targs);
19345 TREE_OPERAND (templ, 1) = tid;
19346 RETURN (templ);
19347 }
19348
19349 if (concept_definition_p (templ))
19350 {
19351 tree check = build_concept_check (templ, targs, complain);
19352 if (check == error_mark_node)
19353 RETURN (error_mark_node);
19354
19355 tree id = unpack_concept_check (check);
19356
19357 /* If we built a function concept check, return the underlying
19358 template-id. So we can evaluate it as a function call. */
19359 if (function_concept_p (TREE_OPERAND (id, 0)))
19360 RETURN (id);
19361
19362 RETURN (check);
19363 }
19364
19365 if (variable_template_p (templ))
19366 {
19367 tree r = lookup_and_finish_template_variable (templ, targs,
19368 complain);
19369 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19370 RETURN (r);
19371 }
19372
19373 if (TREE_CODE (templ) == COMPONENT_REF)
19374 {
19375 object = TREE_OPERAND (templ, 0);
19376 templ = TREE_OPERAND (templ, 1);
19377 }
19378 else
19379 object = NULL_TREE;
19380 templ = lookup_template_function (templ, targs);
19381
19382 if (object)
19383 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19384 object, templ, NULL_TREE));
19385 else
19386 RETURN (baselink_for_fns (templ));
19387 }
19388
19389 case INDIRECT_REF:
19390 {
19391 tree r = RECUR (TREE_OPERAND (t, 0));
19392
19393 if (REFERENCE_REF_P (t))
19394 {
19395 /* A type conversion to reference type will be enclosed in
19396 such an indirect ref, but the substitution of the cast
19397 will have also added such an indirect ref. */
19398 r = convert_from_reference (r);
19399 }
19400 else
19401 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19402 complain|decltype_flag);
19403
19404 if (REF_PARENTHESIZED_P (t))
19405 r = force_paren_expr (r);
19406
19407 RETURN (r);
19408 }
19409
19410 case NOP_EXPR:
19411 {
19412 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19413 tree op0 = RECUR (TREE_OPERAND (t, 0));
19414 RETURN (build_nop (type, op0));
19415 }
19416
19417 case IMPLICIT_CONV_EXPR:
19418 {
19419 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19420 tree expr = RECUR (TREE_OPERAND (t, 0));
19421 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19422 {
19423 retval = copy_node (t);
19424 TREE_TYPE (retval) = type;
19425 TREE_OPERAND (retval, 0) = expr;
19426 RETURN (retval);
19427 }
19428 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19429 /* We'll pass this to convert_nontype_argument again, we don't need
19430 to actually perform any conversion here. */
19431 RETURN (expr);
19432 int flags = LOOKUP_IMPLICIT;
19433 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19434 flags = LOOKUP_NORMAL;
19435 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19436 flags |= LOOKUP_NO_NARROWING;
19437 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19438 flags));
19439 }
19440
19441 case CONVERT_EXPR:
19442 {
19443 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19444 tree op0 = RECUR (TREE_OPERAND (t, 0));
19445 if (op0 == error_mark_node)
19446 RETURN (error_mark_node);
19447 RETURN (build1 (CONVERT_EXPR, type, op0));
19448 }
19449
19450 case CAST_EXPR:
19451 case REINTERPRET_CAST_EXPR:
19452 case CONST_CAST_EXPR:
19453 case DYNAMIC_CAST_EXPR:
19454 case STATIC_CAST_EXPR:
19455 {
19456 tree type;
19457 tree op, r = NULL_TREE;
19458
19459 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19460 if (integral_constant_expression_p
19461 && !cast_valid_in_integral_constant_expression_p (type))
19462 {
19463 if (complain & tf_error)
19464 error ("a cast to a type other than an integral or "
19465 "enumeration type cannot appear in a constant-expression");
19466 RETURN (error_mark_node);
19467 }
19468
19469 op = RECUR (TREE_OPERAND (t, 0));
19470
19471 warning_sentinel s(warn_useless_cast);
19472 warning_sentinel s2(warn_ignored_qualifiers);
19473 switch (TREE_CODE (t))
19474 {
19475 case CAST_EXPR:
19476 r = build_functional_cast (input_location, type, op, complain);
19477 break;
19478 case REINTERPRET_CAST_EXPR:
19479 r = build_reinterpret_cast (input_location, type, op, complain);
19480 break;
19481 case CONST_CAST_EXPR:
19482 r = build_const_cast (input_location, type, op, complain);
19483 break;
19484 case DYNAMIC_CAST_EXPR:
19485 r = build_dynamic_cast (input_location, type, op, complain);
19486 break;
19487 case STATIC_CAST_EXPR:
19488 r = build_static_cast (input_location, type, op, complain);
19489 if (IMPLICIT_RVALUE_P (t))
19490 set_implicit_rvalue_p (r);
19491 break;
19492 default:
19493 gcc_unreachable ();
19494 }
19495
19496 RETURN (r);
19497 }
19498
19499 case POSTDECREMENT_EXPR:
19500 case POSTINCREMENT_EXPR:
19501 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19502 args, complain, in_decl);
19503 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19504 complain|decltype_flag));
19505
19506 case PREDECREMENT_EXPR:
19507 case PREINCREMENT_EXPR:
19508 case NEGATE_EXPR:
19509 case BIT_NOT_EXPR:
19510 case ABS_EXPR:
19511 case TRUTH_NOT_EXPR:
19512 case UNARY_PLUS_EXPR: /* Unary + */
19513 case REALPART_EXPR:
19514 case IMAGPART_EXPR:
19515 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19516 RECUR (TREE_OPERAND (t, 0)),
19517 complain|decltype_flag));
19518
19519 case FIX_TRUNC_EXPR:
19520 gcc_unreachable ();
19521
19522 case ADDR_EXPR:
19523 op1 = TREE_OPERAND (t, 0);
19524 if (TREE_CODE (op1) == LABEL_DECL)
19525 RETURN (finish_label_address_expr (DECL_NAME (op1),
19526 EXPR_LOCATION (op1)));
19527 if (TREE_CODE (op1) == SCOPE_REF)
19528 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19529 /*done=*/true, /*address_p=*/true);
19530 else
19531 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19532 in_decl);
19533 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19534 complain|decltype_flag));
19535
19536 case PLUS_EXPR:
19537 case MINUS_EXPR:
19538 case MULT_EXPR:
19539 case TRUNC_DIV_EXPR:
19540 case CEIL_DIV_EXPR:
19541 case FLOOR_DIV_EXPR:
19542 case ROUND_DIV_EXPR:
19543 case EXACT_DIV_EXPR:
19544 case BIT_AND_EXPR:
19545 case BIT_IOR_EXPR:
19546 case BIT_XOR_EXPR:
19547 case TRUNC_MOD_EXPR:
19548 case FLOOR_MOD_EXPR:
19549 case TRUTH_ANDIF_EXPR:
19550 case TRUTH_ORIF_EXPR:
19551 case TRUTH_AND_EXPR:
19552 case TRUTH_OR_EXPR:
19553 case RSHIFT_EXPR:
19554 case LSHIFT_EXPR:
19555 case EQ_EXPR:
19556 case NE_EXPR:
19557 case MAX_EXPR:
19558 case MIN_EXPR:
19559 case LE_EXPR:
19560 case GE_EXPR:
19561 case LT_EXPR:
19562 case GT_EXPR:
19563 case SPACESHIP_EXPR:
19564 case MEMBER_REF:
19565 case DOTSTAR_EXPR:
19566 {
19567 /* If T was type-dependent, suppress warnings that depend on the range
19568 of the types involved. */
19569 bool was_dep = type_dependent_expression_p_push (t);
19570
19571 tree op0 = RECUR (TREE_OPERAND (t, 0));
19572 tree op1 = RECUR (TREE_OPERAND (t, 1));
19573
19574 warning_sentinel s1(warn_type_limits, was_dep);
19575 warning_sentinel s2(warn_div_by_zero, was_dep);
19576 warning_sentinel s3(warn_logical_op, was_dep);
19577 warning_sentinel s4(warn_tautological_compare, was_dep);
19578
19579 tree r = build_x_binary_op
19580 (input_location, TREE_CODE (t),
19581 op0,
19582 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19583 ? ERROR_MARK
19584 : TREE_CODE (TREE_OPERAND (t, 0))),
19585 op1,
19586 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19587 ? ERROR_MARK
19588 : TREE_CODE (TREE_OPERAND (t, 1))),
19589 /*overload=*/NULL,
19590 complain|decltype_flag);
19591 if (EXPR_P (r) && TREE_NO_WARNING (t))
19592 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19593
19594 RETURN (r);
19595 }
19596
19597 case POINTER_PLUS_EXPR:
19598 {
19599 tree op0 = RECUR (TREE_OPERAND (t, 0));
19600 if (op0 == error_mark_node)
19601 RETURN (error_mark_node);
19602 tree op1 = RECUR (TREE_OPERAND (t, 1));
19603 if (op1 == error_mark_node)
19604 RETURN (error_mark_node);
19605 RETURN (fold_build_pointer_plus (op0, op1));
19606 }
19607
19608 case SCOPE_REF:
19609 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19610 /*address_p=*/false));
19611 case ARRAY_REF:
19612 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19613 args, complain, in_decl);
19614 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19615 RECUR (TREE_OPERAND (t, 1)),
19616 complain|decltype_flag));
19617
19618 case SIZEOF_EXPR:
19619 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19620 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19621 RETURN (tsubst_copy (t, args, complain, in_decl));
19622 /* Fall through */
19623
19624 case ALIGNOF_EXPR:
19625 {
19626 tree r;
19627
19628 op1 = TREE_OPERAND (t, 0);
19629 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19630 op1 = TREE_TYPE (op1);
19631 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19632 && ALIGNOF_EXPR_STD_P (t));
19633 if (!args)
19634 {
19635 /* When there are no ARGS, we are trying to evaluate a
19636 non-dependent expression from the parser. Trying to do
19637 the substitutions may not work. */
19638 if (!TYPE_P (op1))
19639 op1 = TREE_TYPE (op1);
19640 }
19641 else
19642 {
19643 ++cp_unevaluated_operand;
19644 ++c_inhibit_evaluation_warnings;
19645 if (TYPE_P (op1))
19646 op1 = tsubst (op1, args, complain, in_decl);
19647 else
19648 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19649 /*function_p=*/false,
19650 /*integral_constant_expression_p=*/
19651 false);
19652 --cp_unevaluated_operand;
19653 --c_inhibit_evaluation_warnings;
19654 }
19655 if (TYPE_P (op1))
19656 r = cxx_sizeof_or_alignof_type (input_location,
19657 op1, TREE_CODE (t), std_alignof,
19658 complain & tf_error);
19659 else
19660 r = cxx_sizeof_or_alignof_expr (input_location,
19661 op1, TREE_CODE (t),
19662 complain & tf_error);
19663 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19664 {
19665 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19666 {
19667 if (!processing_template_decl && TYPE_P (op1))
19668 {
19669 r = build_min (SIZEOF_EXPR, size_type_node,
19670 build1 (NOP_EXPR, op1, error_mark_node));
19671 SIZEOF_EXPR_TYPE_P (r) = 1;
19672 }
19673 else
19674 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19675 TREE_SIDE_EFFECTS (r) = 0;
19676 TREE_READONLY (r) = 1;
19677 }
19678 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19679 }
19680 RETURN (r);
19681 }
19682
19683 case AT_ENCODE_EXPR:
19684 {
19685 op1 = TREE_OPERAND (t, 0);
19686 ++cp_unevaluated_operand;
19687 ++c_inhibit_evaluation_warnings;
19688 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19689 /*function_p=*/false,
19690 /*integral_constant_expression_p=*/false);
19691 --cp_unevaluated_operand;
19692 --c_inhibit_evaluation_warnings;
19693 RETURN (objc_build_encode_expr (op1));
19694 }
19695
19696 case NOEXCEPT_EXPR:
19697 op1 = TREE_OPERAND (t, 0);
19698 ++cp_unevaluated_operand;
19699 ++c_inhibit_evaluation_warnings;
19700 ++cp_noexcept_operand;
19701 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19702 /*function_p=*/false,
19703 /*integral_constant_expression_p=*/false);
19704 --cp_unevaluated_operand;
19705 --c_inhibit_evaluation_warnings;
19706 --cp_noexcept_operand;
19707 RETURN (finish_noexcept_expr (op1, complain));
19708
19709 case MODOP_EXPR:
19710 {
19711 warning_sentinel s(warn_div_by_zero);
19712 tree lhs = RECUR (TREE_OPERAND (t, 0));
19713 tree rhs = RECUR (TREE_OPERAND (t, 2));
19714 tree r = build_x_modify_expr
19715 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19716 complain|decltype_flag);
19717 /* TREE_NO_WARNING must be set if either the expression was
19718 parenthesized or it uses an operator such as >>= rather
19719 than plain assignment. In the former case, it was already
19720 set and must be copied. In the latter case,
19721 build_x_modify_expr sets it and it must not be reset
19722 here. */
19723 if (TREE_NO_WARNING (t))
19724 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19725
19726 RETURN (r);
19727 }
19728
19729 case ARROW_EXPR:
19730 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19731 args, complain, in_decl);
19732 /* Remember that there was a reference to this entity. */
19733 if (DECL_P (op1)
19734 && !mark_used (op1, complain) && !(complain & tf_error))
19735 RETURN (error_mark_node);
19736 RETURN (build_x_arrow (input_location, op1, complain));
19737
19738 case NEW_EXPR:
19739 {
19740 tree placement = RECUR (TREE_OPERAND (t, 0));
19741 tree init = RECUR (TREE_OPERAND (t, 3));
19742 vec<tree, va_gc> *placement_vec;
19743 vec<tree, va_gc> *init_vec;
19744 tree ret;
19745 location_t loc = EXPR_LOCATION (t);
19746
19747 if (placement == NULL_TREE)
19748 placement_vec = NULL;
19749 else if (placement == error_mark_node)
19750 RETURN (error_mark_node);
19751 else
19752 {
19753 placement_vec = make_tree_vector ();
19754 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19755 vec_safe_push (placement_vec, TREE_VALUE (placement));
19756 }
19757
19758 /* If there was an initializer in the original tree, but it
19759 instantiated to an empty list, then we should pass a
19760 non-NULL empty vector to tell build_new that it was an
19761 empty initializer() rather than no initializer. This can
19762 only happen when the initializer is a pack expansion whose
19763 parameter packs are of length zero. */
19764 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19765 init_vec = NULL;
19766 else
19767 {
19768 init_vec = make_tree_vector ();
19769 if (init == void_node)
19770 gcc_assert (init_vec != NULL);
19771 else
19772 {
19773 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19774 vec_safe_push (init_vec, TREE_VALUE (init));
19775 }
19776 }
19777
19778 /* Avoid passing an enclosing decl to valid_array_size_p. */
19779 in_decl = NULL_TREE;
19780
19781 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19782 tree op2 = RECUR (TREE_OPERAND (t, 2));
19783 ret = build_new (loc, &placement_vec, op1, op2,
19784 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19785 complain);
19786
19787 if (placement_vec != NULL)
19788 release_tree_vector (placement_vec);
19789 if (init_vec != NULL)
19790 release_tree_vector (init_vec);
19791
19792 RETURN (ret);
19793 }
19794
19795 case DELETE_EXPR:
19796 {
19797 tree op0 = RECUR (TREE_OPERAND (t, 0));
19798 tree op1 = RECUR (TREE_OPERAND (t, 1));
19799 RETURN (delete_sanity (input_location, op0, op1,
19800 DELETE_EXPR_USE_VEC (t),
19801 DELETE_EXPR_USE_GLOBAL (t),
19802 complain));
19803 }
19804
19805 case COMPOUND_EXPR:
19806 {
19807 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19808 complain & ~tf_decltype, in_decl,
19809 /*function_p=*/false,
19810 integral_constant_expression_p);
19811 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19812 op0,
19813 RECUR (TREE_OPERAND (t, 1)),
19814 complain|decltype_flag));
19815 }
19816
19817 case CALL_EXPR:
19818 {
19819 tree function;
19820 unsigned int nargs, i;
19821 bool qualified_p;
19822 bool koenig_p;
19823 tree ret;
19824
19825 function = CALL_EXPR_FN (t);
19826 /* Internal function with no arguments. */
19827 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19828 RETURN (t);
19829
19830 /* When we parsed the expression, we determined whether or
19831 not Koenig lookup should be performed. */
19832 koenig_p = KOENIG_LOOKUP_P (t);
19833 if (function == NULL_TREE)
19834 {
19835 koenig_p = false;
19836 qualified_p = false;
19837 }
19838 else if (TREE_CODE (function) == SCOPE_REF)
19839 {
19840 qualified_p = true;
19841 function = tsubst_qualified_id (function, args, complain, in_decl,
19842 /*done=*/false,
19843 /*address_p=*/false);
19844 }
19845 else if (koenig_p && identifier_p (function))
19846 {
19847 /* Do nothing; calling tsubst_copy_and_build on an identifier
19848 would incorrectly perform unqualified lookup again.
19849
19850 Note that we can also have an IDENTIFIER_NODE if the earlier
19851 unqualified lookup found a member function; in that case
19852 koenig_p will be false and we do want to do the lookup
19853 again to find the instantiated member function.
19854
19855 FIXME but doing that causes c++/15272, so we need to stop
19856 using IDENTIFIER_NODE in that situation. */
19857 qualified_p = false;
19858 }
19859 else
19860 {
19861 if (TREE_CODE (function) == COMPONENT_REF)
19862 {
19863 tree op = TREE_OPERAND (function, 1);
19864
19865 qualified_p = (TREE_CODE (op) == SCOPE_REF
19866 || (BASELINK_P (op)
19867 && BASELINK_QUALIFIED_P (op)));
19868 }
19869 else
19870 qualified_p = false;
19871
19872 if (TREE_CODE (function) == ADDR_EXPR
19873 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19874 /* Avoid error about taking the address of a constructor. */
19875 function = TREE_OPERAND (function, 0);
19876
19877 function = tsubst_copy_and_build (function, args, complain,
19878 in_decl,
19879 !qualified_p,
19880 integral_constant_expression_p);
19881
19882 if (BASELINK_P (function))
19883 qualified_p = true;
19884 }
19885
19886 nargs = call_expr_nargs (t);
19887 releasing_vec call_args;
19888 for (i = 0; i < nargs; ++i)
19889 {
19890 tree arg = CALL_EXPR_ARG (t, i);
19891
19892 if (!PACK_EXPANSION_P (arg))
19893 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19894 else
19895 {
19896 /* Expand the pack expansion and push each entry onto
19897 CALL_ARGS. */
19898 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19899 if (TREE_CODE (arg) == TREE_VEC)
19900 {
19901 unsigned int len, j;
19902
19903 len = TREE_VEC_LENGTH (arg);
19904 for (j = 0; j < len; ++j)
19905 {
19906 tree value = TREE_VEC_ELT (arg, j);
19907 if (value != NULL_TREE)
19908 value = convert_from_reference (value);
19909 vec_safe_push (call_args, value);
19910 }
19911 }
19912 else
19913 {
19914 /* A partial substitution. Add one entry. */
19915 vec_safe_push (call_args, arg);
19916 }
19917 }
19918 }
19919
19920 /* Stripped-down processing for a call in a thunk. Specifically, in
19921 the thunk template for a generic lambda. */
19922 if (CALL_FROM_THUNK_P (t))
19923 {
19924 /* Now that we've expanded any packs, the number of call args
19925 might be different. */
19926 unsigned int cargs = call_args->length ();
19927 tree thisarg = NULL_TREE;
19928 if (TREE_CODE (function) == COMPONENT_REF)
19929 {
19930 thisarg = TREE_OPERAND (function, 0);
19931 if (TREE_CODE (thisarg) == INDIRECT_REF)
19932 thisarg = TREE_OPERAND (thisarg, 0);
19933 function = TREE_OPERAND (function, 1);
19934 if (TREE_CODE (function) == BASELINK)
19935 function = BASELINK_FUNCTIONS (function);
19936 }
19937 /* We aren't going to do normal overload resolution, so force the
19938 template-id to resolve. */
19939 function = resolve_nondeduced_context (function, complain);
19940 for (unsigned i = 0; i < cargs; ++i)
19941 {
19942 /* In a thunk, pass through args directly, without any
19943 conversions. */
19944 tree arg = (*call_args)[i];
19945 while (TREE_CODE (arg) != PARM_DECL)
19946 arg = TREE_OPERAND (arg, 0);
19947 (*call_args)[i] = arg;
19948 }
19949 if (thisarg)
19950 {
19951 /* If there are no other args, just push 'this'. */
19952 if (cargs == 0)
19953 vec_safe_push (call_args, thisarg);
19954 else
19955 {
19956 /* Otherwise, shift the other args over to make room. */
19957 tree last = (*call_args)[cargs - 1];
19958 vec_safe_push (call_args, last);
19959 for (int i = cargs - 1; i > 0; --i)
19960 (*call_args)[i] = (*call_args)[i - 1];
19961 (*call_args)[0] = thisarg;
19962 }
19963 }
19964 ret = build_call_a (function, call_args->length (),
19965 call_args->address ());
19966 /* The thunk location is not interesting. */
19967 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19968 CALL_FROM_THUNK_P (ret) = true;
19969 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19970 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19971
19972 RETURN (ret);
19973 }
19974
19975 /* We do not perform argument-dependent lookup if normal
19976 lookup finds a non-function, in accordance with the
19977 expected resolution of DR 218. */
19978 if (koenig_p
19979 && ((is_overloaded_fn (function)
19980 /* If lookup found a member function, the Koenig lookup is
19981 not appropriate, even if an unqualified-name was used
19982 to denote the function. */
19983 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19984 || identifier_p (function))
19985 /* Only do this when substitution turns a dependent call
19986 into a non-dependent call. */
19987 && type_dependent_expression_p_push (t)
19988 && !any_type_dependent_arguments_p (call_args))
19989 function = perform_koenig_lookup (function, call_args, tf_none);
19990
19991 if (function != NULL_TREE
19992 && identifier_p (function)
19993 && !any_type_dependent_arguments_p (call_args))
19994 {
19995 if (koenig_p && (complain & tf_warning_or_error))
19996 {
19997 /* For backwards compatibility and good diagnostics, try
19998 the unqualified lookup again if we aren't in SFINAE
19999 context. */
20000 tree unq = (tsubst_copy_and_build
20001 (function, args, complain, in_decl, true,
20002 integral_constant_expression_p));
20003 if (unq == error_mark_node)
20004 RETURN (error_mark_node);
20005
20006 if (unq != function)
20007 {
20008 /* In a lambda fn, we have to be careful to not
20009 introduce new this captures. Legacy code can't
20010 be using lambdas anyway, so it's ok to be
20011 stricter. */
20012 bool in_lambda = (current_class_type
20013 && LAMBDA_TYPE_P (current_class_type));
20014 char const *const msg
20015 = G_("%qD was not declared in this scope, "
20016 "and no declarations were found by "
20017 "argument-dependent lookup at the point "
20018 "of instantiation");
20019
20020 bool diag = true;
20021 if (in_lambda)
20022 error_at (cp_expr_loc_or_input_loc (t),
20023 msg, function);
20024 else
20025 diag = permerror (cp_expr_loc_or_input_loc (t),
20026 msg, function);
20027 if (diag)
20028 {
20029 tree fn = unq;
20030
20031 if (INDIRECT_REF_P (fn))
20032 fn = TREE_OPERAND (fn, 0);
20033 if (is_overloaded_fn (fn))
20034 fn = get_first_fn (fn);
20035
20036 if (!DECL_P (fn))
20037 /* Can't say anything more. */;
20038 else if (DECL_CLASS_SCOPE_P (fn))
20039 {
20040 location_t loc = cp_expr_loc_or_input_loc (t);
20041 inform (loc,
20042 "declarations in dependent base %qT are "
20043 "not found by unqualified lookup",
20044 DECL_CLASS_CONTEXT (fn));
20045 if (current_class_ptr)
20046 inform (loc,
20047 "use %<this->%D%> instead", function);
20048 else
20049 inform (loc,
20050 "use %<%T::%D%> instead",
20051 current_class_name, function);
20052 }
20053 else
20054 inform (DECL_SOURCE_LOCATION (fn),
20055 "%qD declared here, later in the "
20056 "translation unit", fn);
20057 if (in_lambda)
20058 RETURN (error_mark_node);
20059 }
20060
20061 function = unq;
20062 }
20063 }
20064 if (identifier_p (function))
20065 {
20066 if (complain & tf_error)
20067 unqualified_name_lookup_error (function);
20068 RETURN (error_mark_node);
20069 }
20070 }
20071
20072 /* Remember that there was a reference to this entity. */
20073 if (function != NULL_TREE
20074 && DECL_P (function)
20075 && !mark_used (function, complain) && !(complain & tf_error))
20076 RETURN (error_mark_node);
20077
20078 /* Put back tf_decltype for the actual call. */
20079 complain |= decltype_flag;
20080
20081 if (function == NULL_TREE)
20082 switch (CALL_EXPR_IFN (t))
20083 {
20084 case IFN_LAUNDER:
20085 gcc_assert (nargs == 1);
20086 if (vec_safe_length (call_args) != 1)
20087 {
20088 error_at (cp_expr_loc_or_input_loc (t),
20089 "wrong number of arguments to "
20090 "%<__builtin_launder%>");
20091 ret = error_mark_node;
20092 }
20093 else
20094 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20095 (*call_args)[0], complain);
20096 break;
20097
20098 case IFN_VEC_CONVERT:
20099 gcc_assert (nargs == 1);
20100 if (vec_safe_length (call_args) != 1)
20101 {
20102 error_at (cp_expr_loc_or_input_loc (t),
20103 "wrong number of arguments to "
20104 "%<__builtin_convertvector%>");
20105 ret = error_mark_node;
20106 break;
20107 }
20108 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20109 tsubst (TREE_TYPE (t), args,
20110 complain, in_decl),
20111 complain);
20112 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20113 RETURN (ret);
20114 break;
20115
20116 default:
20117 /* Unsupported internal function with arguments. */
20118 gcc_unreachable ();
20119 }
20120 else if (TREE_CODE (function) == OFFSET_REF
20121 || TREE_CODE (function) == DOTSTAR_EXPR
20122 || TREE_CODE (function) == MEMBER_REF)
20123 ret = build_offset_ref_call_from_tree (function, &call_args,
20124 complain);
20125 else if (TREE_CODE (function) == COMPONENT_REF)
20126 {
20127 tree instance = TREE_OPERAND (function, 0);
20128 tree fn = TREE_OPERAND (function, 1);
20129
20130 if (processing_template_decl
20131 && (type_dependent_expression_p (instance)
20132 || (!BASELINK_P (fn)
20133 && TREE_CODE (fn) != FIELD_DECL)
20134 || type_dependent_expression_p (fn)
20135 || any_type_dependent_arguments_p (call_args)))
20136 ret = build_min_nt_call_vec (function, call_args);
20137 else if (!BASELINK_P (fn))
20138 ret = finish_call_expr (function, &call_args,
20139 /*disallow_virtual=*/false,
20140 /*koenig_p=*/false,
20141 complain);
20142 else
20143 ret = (build_new_method_call
20144 (instance, fn,
20145 &call_args, NULL_TREE,
20146 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20147 /*fn_p=*/NULL,
20148 complain));
20149 }
20150 else if (concept_check_p (function))
20151 {
20152 /* FUNCTION is a template-id referring to a concept definition. */
20153 tree id = unpack_concept_check (function);
20154 tree tmpl = TREE_OPERAND (id, 0);
20155 tree args = TREE_OPERAND (id, 1);
20156
20157 /* Calls to standard and variable concepts should have been
20158 previously diagnosed. */
20159 gcc_assert (function_concept_p (tmpl));
20160
20161 /* Ensure the result is wrapped as a call expression. */
20162 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20163 }
20164 else
20165 ret = finish_call_expr (function, &call_args,
20166 /*disallow_virtual=*/qualified_p,
20167 koenig_p,
20168 complain);
20169
20170 if (ret != error_mark_node)
20171 {
20172 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20173 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20174 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20175 if (op || ord || rev)
20176 {
20177 function = extract_call_expr (ret);
20178 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20179 CALL_EXPR_ORDERED_ARGS (function) = ord;
20180 CALL_EXPR_REVERSE_ARGS (function) = rev;
20181 }
20182 }
20183
20184 RETURN (ret);
20185 }
20186
20187 case COND_EXPR:
20188 {
20189 tree cond = RECUR (TREE_OPERAND (t, 0));
20190 cond = mark_rvalue_use (cond);
20191 tree folded_cond = fold_non_dependent_expr (cond, complain);
20192 tree exp1, exp2;
20193
20194 if (TREE_CODE (folded_cond) == INTEGER_CST)
20195 {
20196 if (integer_zerop (folded_cond))
20197 {
20198 ++c_inhibit_evaluation_warnings;
20199 exp1 = RECUR (TREE_OPERAND (t, 1));
20200 --c_inhibit_evaluation_warnings;
20201 exp2 = RECUR (TREE_OPERAND (t, 2));
20202 }
20203 else
20204 {
20205 exp1 = RECUR (TREE_OPERAND (t, 1));
20206 ++c_inhibit_evaluation_warnings;
20207 exp2 = RECUR (TREE_OPERAND (t, 2));
20208 --c_inhibit_evaluation_warnings;
20209 }
20210 cond = folded_cond;
20211 }
20212 else
20213 {
20214 exp1 = RECUR (TREE_OPERAND (t, 1));
20215 exp2 = RECUR (TREE_OPERAND (t, 2));
20216 }
20217
20218 warning_sentinel s(warn_duplicated_branches);
20219 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20220 cond, exp1, exp2, complain));
20221 }
20222
20223 case PSEUDO_DTOR_EXPR:
20224 {
20225 tree op0 = RECUR (TREE_OPERAND (t, 0));
20226 tree op1 = RECUR (TREE_OPERAND (t, 1));
20227 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20228 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20229 input_location));
20230 }
20231
20232 case TREE_LIST:
20233 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20234
20235 case COMPONENT_REF:
20236 {
20237 tree object;
20238 tree object_type;
20239 tree member;
20240 tree r;
20241
20242 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20243 args, complain, in_decl);
20244 /* Remember that there was a reference to this entity. */
20245 if (DECL_P (object)
20246 && !mark_used (object, complain) && !(complain & tf_error))
20247 RETURN (error_mark_node);
20248 object_type = TREE_TYPE (object);
20249
20250 member = TREE_OPERAND (t, 1);
20251 if (BASELINK_P (member))
20252 member = tsubst_baselink (member,
20253 non_reference (TREE_TYPE (object)),
20254 args, complain, in_decl);
20255 else
20256 member = tsubst_copy (member, args, complain, in_decl);
20257 if (member == error_mark_node)
20258 RETURN (error_mark_node);
20259
20260 if (TREE_CODE (member) == FIELD_DECL)
20261 {
20262 r = finish_non_static_data_member (member, object, NULL_TREE);
20263 if (TREE_CODE (r) == COMPONENT_REF)
20264 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20265 RETURN (r);
20266 }
20267 else if (type_dependent_expression_p (object))
20268 /* We can't do much here. */;
20269 else if (!CLASS_TYPE_P (object_type))
20270 {
20271 if (scalarish_type_p (object_type))
20272 {
20273 tree s = NULL_TREE;
20274 tree dtor = member;
20275
20276 if (TREE_CODE (dtor) == SCOPE_REF)
20277 {
20278 s = TREE_OPERAND (dtor, 0);
20279 dtor = TREE_OPERAND (dtor, 1);
20280 }
20281 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20282 {
20283 dtor = TREE_OPERAND (dtor, 0);
20284 if (TYPE_P (dtor))
20285 RETURN (finish_pseudo_destructor_expr
20286 (object, s, dtor, input_location));
20287 }
20288 }
20289 }
20290 else if (TREE_CODE (member) == SCOPE_REF
20291 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20292 {
20293 /* Lookup the template functions now that we know what the
20294 scope is. */
20295 tree scope = TREE_OPERAND (member, 0);
20296 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20297 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20298 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20299 /*complain=*/false);
20300 if (BASELINK_P (member))
20301 {
20302 BASELINK_FUNCTIONS (member)
20303 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20304 args);
20305 member = (adjust_result_of_qualified_name_lookup
20306 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20307 object_type));
20308 }
20309 else
20310 {
20311 qualified_name_lookup_error (scope, tmpl, member,
20312 input_location);
20313 RETURN (error_mark_node);
20314 }
20315 }
20316 else if (TREE_CODE (member) == SCOPE_REF
20317 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20318 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20319 {
20320 if (complain & tf_error)
20321 {
20322 if (TYPE_P (TREE_OPERAND (member, 0)))
20323 error ("%qT is not a class or namespace",
20324 TREE_OPERAND (member, 0));
20325 else
20326 error ("%qD is not a class or namespace",
20327 TREE_OPERAND (member, 0));
20328 }
20329 RETURN (error_mark_node);
20330 }
20331
20332 r = finish_class_member_access_expr (object, member,
20333 /*template_p=*/false,
20334 complain);
20335 if (TREE_CODE (r) == COMPONENT_REF)
20336 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20337 RETURN (r);
20338 }
20339
20340 case THROW_EXPR:
20341 RETURN (build_throw
20342 (input_location, RECUR (TREE_OPERAND (t, 0))));
20343
20344 case CONSTRUCTOR:
20345 {
20346 vec<constructor_elt, va_gc> *n;
20347 constructor_elt *ce;
20348 unsigned HOST_WIDE_INT idx;
20349 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20350 bool process_index_p;
20351 int newlen;
20352 bool need_copy_p = false;
20353 tree r;
20354
20355 if (type == error_mark_node)
20356 RETURN (error_mark_node);
20357
20358 /* We do not want to process the index of aggregate
20359 initializers as they are identifier nodes which will be
20360 looked up by digest_init. */
20361 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20362
20363 if (null_member_pointer_value_p (t))
20364 {
20365 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20366 RETURN (t);
20367 }
20368
20369 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20370 newlen = vec_safe_length (n);
20371 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20372 {
20373 if (ce->index && process_index_p
20374 /* An identifier index is looked up in the type
20375 being initialized, not the current scope. */
20376 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20377 ce->index = RECUR (ce->index);
20378
20379 if (PACK_EXPANSION_P (ce->value))
20380 {
20381 /* Substitute into the pack expansion. */
20382 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20383 in_decl);
20384
20385 if (ce->value == error_mark_node
20386 || PACK_EXPANSION_P (ce->value))
20387 ;
20388 else if (TREE_VEC_LENGTH (ce->value) == 1)
20389 /* Just move the argument into place. */
20390 ce->value = TREE_VEC_ELT (ce->value, 0);
20391 else
20392 {
20393 /* Update the length of the final CONSTRUCTOR
20394 arguments vector, and note that we will need to
20395 copy.*/
20396 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20397 need_copy_p = true;
20398 }
20399 }
20400 else
20401 ce->value = RECUR (ce->value);
20402 }
20403
20404 if (need_copy_p)
20405 {
20406 vec<constructor_elt, va_gc> *old_n = n;
20407
20408 vec_alloc (n, newlen);
20409 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20410 {
20411 if (TREE_CODE (ce->value) == TREE_VEC)
20412 {
20413 int i, len = TREE_VEC_LENGTH (ce->value);
20414 for (i = 0; i < len; ++i)
20415 CONSTRUCTOR_APPEND_ELT (n, 0,
20416 TREE_VEC_ELT (ce->value, i));
20417 }
20418 else
20419 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20420 }
20421 }
20422
20423 r = build_constructor (init_list_type_node, n);
20424 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20425 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20426 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20427
20428 if (TREE_HAS_CONSTRUCTOR (t))
20429 {
20430 fcl_t cl = fcl_functional;
20431 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20432 cl = fcl_c99;
20433 RETURN (finish_compound_literal (type, r, complain, cl));
20434 }
20435
20436 TREE_TYPE (r) = type;
20437 RETURN (r);
20438 }
20439
20440 case TYPEID_EXPR:
20441 {
20442 tree operand_0 = TREE_OPERAND (t, 0);
20443 if (TYPE_P (operand_0))
20444 {
20445 operand_0 = tsubst (operand_0, args, complain, in_decl);
20446 RETURN (get_typeid (operand_0, complain));
20447 }
20448 else
20449 {
20450 operand_0 = RECUR (operand_0);
20451 RETURN (build_typeid (operand_0, complain));
20452 }
20453 }
20454
20455 case VAR_DECL:
20456 if (!args)
20457 RETURN (t);
20458 /* Fall through */
20459
20460 case PARM_DECL:
20461 {
20462 tree r = tsubst_copy (t, args, complain, in_decl);
20463 /* ??? We're doing a subset of finish_id_expression here. */
20464 if (tree wrap = maybe_get_tls_wrapper_call (r))
20465 /* Replace an evaluated use of the thread_local variable with
20466 a call to its wrapper. */
20467 r = wrap;
20468 else if (outer_automatic_var_p (r))
20469 r = process_outer_var_ref (r, complain);
20470
20471 if (!TYPE_REF_P (TREE_TYPE (t)))
20472 /* If the original type was a reference, we'll be wrapped in
20473 the appropriate INDIRECT_REF. */
20474 r = convert_from_reference (r);
20475 RETURN (r);
20476 }
20477
20478 case VA_ARG_EXPR:
20479 {
20480 tree op0 = RECUR (TREE_OPERAND (t, 0));
20481 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20482 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20483 }
20484
20485 case OFFSETOF_EXPR:
20486 {
20487 tree object_ptr
20488 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20489 in_decl, /*function_p=*/false,
20490 /*integral_constant_expression_p=*/false);
20491 RETURN (finish_offsetof (object_ptr,
20492 RECUR (TREE_OPERAND (t, 0)),
20493 EXPR_LOCATION (t)));
20494 }
20495
20496 case ADDRESSOF_EXPR:
20497 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20498 RECUR (TREE_OPERAND (t, 0)), complain));
20499
20500 case TRAIT_EXPR:
20501 {
20502 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20503 complain, in_decl);
20504 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20505 complain, in_decl);
20506 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20507 TRAIT_EXPR_KIND (t), type1, type2));
20508 }
20509
20510 case STMT_EXPR:
20511 {
20512 tree old_stmt_expr = cur_stmt_expr;
20513 tree stmt_expr = begin_stmt_expr ();
20514
20515 cur_stmt_expr = stmt_expr;
20516 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20517 integral_constant_expression_p);
20518 stmt_expr = finish_stmt_expr (stmt_expr, false);
20519 cur_stmt_expr = old_stmt_expr;
20520
20521 /* If the resulting list of expression statement is empty,
20522 fold it further into void_node. */
20523 if (empty_expr_stmt_p (stmt_expr))
20524 stmt_expr = void_node;
20525
20526 RETURN (stmt_expr);
20527 }
20528
20529 case LAMBDA_EXPR:
20530 {
20531 if (complain & tf_partial)
20532 {
20533 /* We don't have a full set of template arguments yet; don't touch
20534 the lambda at all. */
20535 gcc_assert (processing_template_decl);
20536 return t;
20537 }
20538 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20539
20540 RETURN (build_lambda_object (r));
20541 }
20542
20543 case TARGET_EXPR:
20544 /* We can get here for a constant initializer of non-dependent type.
20545 FIXME stop folding in cp_parser_initializer_clause. */
20546 {
20547 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20548 complain);
20549 RETURN (r);
20550 }
20551
20552 case TRANSACTION_EXPR:
20553 RETURN (tsubst_expr(t, args, complain, in_decl,
20554 integral_constant_expression_p));
20555
20556 case PAREN_EXPR:
20557 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20558
20559 case VEC_PERM_EXPR:
20560 {
20561 tree op0 = RECUR (TREE_OPERAND (t, 0));
20562 tree op1 = RECUR (TREE_OPERAND (t, 1));
20563 tree op2 = RECUR (TREE_OPERAND (t, 2));
20564 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20565 complain));
20566 }
20567
20568 case REQUIRES_EXPR:
20569 {
20570 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20571 RETURN (r);
20572 }
20573
20574 case RANGE_EXPR:
20575 /* No need to substitute further, a RANGE_EXPR will always be built
20576 with constant operands. */
20577 RETURN (t);
20578
20579 case NON_LVALUE_EXPR:
20580 case VIEW_CONVERT_EXPR:
20581 if (location_wrapper_p (t))
20582 /* We need to do this here as well as in tsubst_copy so we get the
20583 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20584 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20585 EXPR_LOCATION (t)));
20586 /* fallthrough. */
20587
20588 default:
20589 /* Handle Objective-C++ constructs, if appropriate. */
20590 {
20591 tree subst
20592 = objcp_tsubst_copy_and_build (t, args, complain,
20593 in_decl, /*function_p=*/false);
20594 if (subst)
20595 RETURN (subst);
20596 }
20597 RETURN (tsubst_copy (t, args, complain, in_decl));
20598 }
20599
20600 #undef RECUR
20601 #undef RETURN
20602 out:
20603 input_location = save_loc;
20604 return retval;
20605 }
20606
20607 /* Verify that the instantiated ARGS are valid. For type arguments,
20608 make sure that the type's linkage is ok. For non-type arguments,
20609 make sure they are constants if they are integral or enumerations.
20610 Emit an error under control of COMPLAIN, and return TRUE on error. */
20611
20612 static bool
20613 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20614 {
20615 if (dependent_template_arg_p (t))
20616 return false;
20617 if (ARGUMENT_PACK_P (t))
20618 {
20619 tree vec = ARGUMENT_PACK_ARGS (t);
20620 int len = TREE_VEC_LENGTH (vec);
20621 bool result = false;
20622 int i;
20623
20624 for (i = 0; i < len; ++i)
20625 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20626 result = true;
20627 return result;
20628 }
20629 else if (TYPE_P (t))
20630 {
20631 /* [basic.link]: A name with no linkage (notably, the name
20632 of a class or enumeration declared in a local scope)
20633 shall not be used to declare an entity with linkage.
20634 This implies that names with no linkage cannot be used as
20635 template arguments
20636
20637 DR 757 relaxes this restriction for C++0x. */
20638 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20639 : no_linkage_check (t, /*relaxed_p=*/false));
20640
20641 if (nt)
20642 {
20643 /* DR 488 makes use of a type with no linkage cause
20644 type deduction to fail. */
20645 if (complain & tf_error)
20646 {
20647 if (TYPE_UNNAMED_P (nt))
20648 error ("%qT is/uses unnamed type", t);
20649 else
20650 error ("template argument for %qD uses local type %qT",
20651 tmpl, t);
20652 }
20653 return true;
20654 }
20655 /* In order to avoid all sorts of complications, we do not
20656 allow variably-modified types as template arguments. */
20657 else if (variably_modified_type_p (t, NULL_TREE))
20658 {
20659 if (complain & tf_error)
20660 error ("%qT is a variably modified type", t);
20661 return true;
20662 }
20663 }
20664 /* Class template and alias template arguments should be OK. */
20665 else if (DECL_TYPE_TEMPLATE_P (t))
20666 ;
20667 /* A non-type argument of integral or enumerated type must be a
20668 constant. */
20669 else if (TREE_TYPE (t)
20670 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20671 && !REFERENCE_REF_P (t)
20672 && !TREE_CONSTANT (t))
20673 {
20674 if (complain & tf_error)
20675 error ("integral expression %qE is not constant", t);
20676 return true;
20677 }
20678 return false;
20679 }
20680
20681 static bool
20682 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20683 {
20684 int ix, len = DECL_NTPARMS (tmpl);
20685 bool result = false;
20686
20687 for (ix = 0; ix != len; ix++)
20688 {
20689 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20690 result = true;
20691 }
20692 if (result && (complain & tf_error))
20693 error (" trying to instantiate %qD", tmpl);
20694 return result;
20695 }
20696
20697 /* We're out of SFINAE context now, so generate diagnostics for the access
20698 errors we saw earlier when instantiating D from TMPL and ARGS. */
20699
20700 static void
20701 recheck_decl_substitution (tree d, tree tmpl, tree args)
20702 {
20703 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20704 tree type = TREE_TYPE (pattern);
20705 location_t loc = input_location;
20706
20707 push_access_scope (d);
20708 push_deferring_access_checks (dk_no_deferred);
20709 input_location = DECL_SOURCE_LOCATION (pattern);
20710 tsubst (type, args, tf_warning_or_error, d);
20711 input_location = loc;
20712 pop_deferring_access_checks ();
20713 pop_access_scope (d);
20714 }
20715
20716 /* Instantiate the indicated variable, function, or alias template TMPL with
20717 the template arguments in TARG_PTR. */
20718
20719 static tree
20720 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20721 {
20722 tree targ_ptr = orig_args;
20723 tree fndecl;
20724 tree gen_tmpl;
20725 tree spec;
20726 bool access_ok = true;
20727
20728 if (tmpl == error_mark_node)
20729 return error_mark_node;
20730
20731 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20732
20733 /* If this function is a clone, handle it specially. */
20734 if (DECL_CLONED_FUNCTION_P (tmpl))
20735 {
20736 tree spec;
20737 tree clone;
20738
20739 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20740 DECL_CLONED_FUNCTION. */
20741 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20742 targ_ptr, complain);
20743 if (spec == error_mark_node)
20744 return error_mark_node;
20745
20746 /* Look for the clone. */
20747 FOR_EACH_CLONE (clone, spec)
20748 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20749 return clone;
20750 /* We should always have found the clone by now. */
20751 gcc_unreachable ();
20752 return NULL_TREE;
20753 }
20754
20755 if (targ_ptr == error_mark_node)
20756 return error_mark_node;
20757
20758 /* Check to see if we already have this specialization. */
20759 gen_tmpl = most_general_template (tmpl);
20760 if (TMPL_ARGS_DEPTH (targ_ptr)
20761 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20762 /* targ_ptr only has the innermost template args, so add the outer ones
20763 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20764 the case of a non-dependent call within a template definition). */
20765 targ_ptr = (add_outermost_template_args
20766 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20767 targ_ptr));
20768
20769 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20770 but it doesn't seem to be on the hot path. */
20771 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20772
20773 gcc_checking_assert (tmpl == gen_tmpl
20774 || ((fndecl
20775 = retrieve_specialization (tmpl, orig_args, 0))
20776 == spec)
20777 || fndecl == NULL_TREE);
20778
20779 if (spec != NULL_TREE)
20780 {
20781 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20782 {
20783 if (complain & tf_error)
20784 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20785 return error_mark_node;
20786 }
20787 return spec;
20788 }
20789
20790 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20791 complain))
20792 return error_mark_node;
20793
20794 /* We are building a FUNCTION_DECL, during which the access of its
20795 parameters and return types have to be checked. However this
20796 FUNCTION_DECL which is the desired context for access checking
20797 is not built yet. We solve this chicken-and-egg problem by
20798 deferring all checks until we have the FUNCTION_DECL. */
20799 push_deferring_access_checks (dk_deferred);
20800
20801 /* Instantiation of the function happens in the context of the function
20802 template, not the context of the overload resolution we're doing. */
20803 push_to_top_level ();
20804 /* If there are dependent arguments, e.g. because we're doing partial
20805 ordering, make sure processing_template_decl stays set. */
20806 if (uses_template_parms (targ_ptr))
20807 ++processing_template_decl;
20808 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20809 {
20810 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20811 complain, gen_tmpl, true);
20812 push_nested_class (ctx);
20813 }
20814
20815 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20816
20817 fndecl = NULL_TREE;
20818 if (VAR_P (pattern))
20819 {
20820 /* We need to determine if we're using a partial or explicit
20821 specialization now, because the type of the variable could be
20822 different. */
20823 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20824 tree elt = most_specialized_partial_spec (tid, complain);
20825 if (elt == error_mark_node)
20826 pattern = error_mark_node;
20827 else if (elt)
20828 {
20829 tree partial_tmpl = TREE_VALUE (elt);
20830 tree partial_args = TREE_PURPOSE (elt);
20831 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20832 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20833 }
20834 }
20835
20836 /* Substitute template parameters to obtain the specialization. */
20837 if (fndecl == NULL_TREE)
20838 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20839 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20840 pop_nested_class ();
20841 pop_from_top_level ();
20842
20843 if (fndecl == error_mark_node)
20844 {
20845 pop_deferring_access_checks ();
20846 return error_mark_node;
20847 }
20848
20849 /* The DECL_TI_TEMPLATE should always be the immediate parent
20850 template, not the most general template. */
20851 DECL_TI_TEMPLATE (fndecl) = tmpl;
20852 DECL_TI_ARGS (fndecl) = targ_ptr;
20853
20854 /* Now we know the specialization, compute access previously
20855 deferred. Do no access control for inheriting constructors,
20856 as we already checked access for the inherited constructor. */
20857 if (!(flag_new_inheriting_ctors
20858 && DECL_INHERITED_CTOR (fndecl)))
20859 {
20860 push_access_scope (fndecl);
20861 if (!perform_deferred_access_checks (complain))
20862 access_ok = false;
20863 pop_access_scope (fndecl);
20864 }
20865 pop_deferring_access_checks ();
20866
20867 /* If we've just instantiated the main entry point for a function,
20868 instantiate all the alternate entry points as well. We do this
20869 by cloning the instantiation of the main entry point, not by
20870 instantiating the template clones. */
20871 if (tree chain = DECL_CHAIN (gen_tmpl))
20872 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20873 clone_cdtor (fndecl, /*update_methods=*/false);
20874
20875 if (!access_ok)
20876 {
20877 if (!(complain & tf_error))
20878 {
20879 /* Remember to reinstantiate when we're out of SFINAE so the user
20880 can see the errors. */
20881 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20882 }
20883 return error_mark_node;
20884 }
20885 return fndecl;
20886 }
20887
20888 /* Wrapper for instantiate_template_1. */
20889
20890 tree
20891 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20892 {
20893 tree ret;
20894 timevar_push (TV_TEMPLATE_INST);
20895 ret = instantiate_template_1 (tmpl, orig_args, complain);
20896 timevar_pop (TV_TEMPLATE_INST);
20897 return ret;
20898 }
20899
20900 /* Instantiate the alias template TMPL with ARGS. Also push a template
20901 instantiation level, which instantiate_template doesn't do because
20902 functions and variables have sufficient context established by the
20903 callers. */
20904
20905 static tree
20906 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20907 {
20908 if (tmpl == error_mark_node || args == error_mark_node)
20909 return error_mark_node;
20910
20911 args =
20912 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20913 args, tmpl, complain,
20914 /*require_all_args=*/true,
20915 /*use_default_args=*/true);
20916
20917 /* FIXME check for satisfaction in check_instantiated_args. */
20918 if (flag_concepts
20919 && !any_dependent_template_arguments_p (args)
20920 && !constraints_satisfied_p (tmpl, args))
20921 {
20922 if (complain & tf_error)
20923 {
20924 auto_diagnostic_group d;
20925 error ("template constraint failure for %qD", tmpl);
20926 diagnose_constraints (input_location, tmpl, args);
20927 }
20928 return error_mark_node;
20929 }
20930
20931 if (!push_tinst_level (tmpl, args))
20932 return error_mark_node;
20933 tree r = instantiate_template (tmpl, args, complain);
20934 pop_tinst_level ();
20935
20936 return r;
20937 }
20938
20939 /* PARM is a template parameter pack for FN. Returns true iff
20940 PARM is used in a deducible way in the argument list of FN. */
20941
20942 static bool
20943 pack_deducible_p (tree parm, tree fn)
20944 {
20945 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20946 for (; t; t = TREE_CHAIN (t))
20947 {
20948 tree type = TREE_VALUE (t);
20949 tree packs;
20950 if (!PACK_EXPANSION_P (type))
20951 continue;
20952 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20953 packs; packs = TREE_CHAIN (packs))
20954 if (template_args_equal (TREE_VALUE (packs), parm))
20955 {
20956 /* The template parameter pack is used in a function parameter
20957 pack. If this is the end of the parameter list, the
20958 template parameter pack is deducible. */
20959 if (TREE_CHAIN (t) == void_list_node)
20960 return true;
20961 else
20962 /* Otherwise, not. Well, it could be deduced from
20963 a non-pack parameter, but doing so would end up with
20964 a deduction mismatch, so don't bother. */
20965 return false;
20966 }
20967 }
20968 /* The template parameter pack isn't used in any function parameter
20969 packs, but it might be used deeper, e.g. tuple<Args...>. */
20970 return true;
20971 }
20972
20973 /* Subroutine of fn_type_unification: check non-dependent parms for
20974 convertibility. */
20975
20976 static int
20977 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20978 tree fn, unification_kind_t strict, int flags,
20979 struct conversion **convs, bool explain_p)
20980 {
20981 /* Non-constructor methods need to leave a conversion for 'this', which
20982 isn't included in nargs here. */
20983 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20984 && !DECL_CONSTRUCTOR_P (fn));
20985
20986 for (unsigned ia = 0;
20987 parms && parms != void_list_node && ia < nargs; )
20988 {
20989 tree parm = TREE_VALUE (parms);
20990
20991 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20992 && (!TREE_CHAIN (parms)
20993 || TREE_CHAIN (parms) == void_list_node))
20994 /* For a function parameter pack that occurs at the end of the
20995 parameter-declaration-list, the type A of each remaining
20996 argument of the call is compared with the type P of the
20997 declarator-id of the function parameter pack. */
20998 break;
20999
21000 parms = TREE_CHAIN (parms);
21001
21002 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21003 /* For a function parameter pack that does not occur at the
21004 end of the parameter-declaration-list, the type of the
21005 parameter pack is a non-deduced context. */
21006 continue;
21007
21008 if (!uses_template_parms (parm))
21009 {
21010 tree arg = args[ia];
21011 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21012 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21013
21014 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21015 conv_p, explain_p))
21016 return 1;
21017 }
21018
21019 ++ia;
21020 }
21021
21022 return 0;
21023 }
21024
21025 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21026 NARGS elements of the arguments that are being used when calling
21027 it. TARGS is a vector into which the deduced template arguments
21028 are placed.
21029
21030 Returns either a FUNCTION_DECL for the matching specialization of FN or
21031 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21032 true, diagnostics will be printed to explain why it failed.
21033
21034 If FN is a conversion operator, or we are trying to produce a specific
21035 specialization, RETURN_TYPE is the return type desired.
21036
21037 The EXPLICIT_TARGS are explicit template arguments provided via a
21038 template-id.
21039
21040 The parameter STRICT is one of:
21041
21042 DEDUCE_CALL:
21043 We are deducing arguments for a function call, as in
21044 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21045 deducing arguments for a call to the result of a conversion
21046 function template, as in [over.call.object].
21047
21048 DEDUCE_CONV:
21049 We are deducing arguments for a conversion function, as in
21050 [temp.deduct.conv].
21051
21052 DEDUCE_EXACT:
21053 We are deducing arguments when doing an explicit instantiation
21054 as in [temp.explicit], when determining an explicit specialization
21055 as in [temp.expl.spec], or when taking the address of a function
21056 template, as in [temp.deduct.funcaddr]. */
21057
21058 tree
21059 fn_type_unification (tree fn,
21060 tree explicit_targs,
21061 tree targs,
21062 const tree *args,
21063 unsigned int nargs,
21064 tree return_type,
21065 unification_kind_t strict,
21066 int flags,
21067 struct conversion **convs,
21068 bool explain_p,
21069 bool decltype_p)
21070 {
21071 tree parms;
21072 tree fntype;
21073 tree decl = NULL_TREE;
21074 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21075 bool ok;
21076 static int deduction_depth;
21077 /* type_unification_real will pass back any access checks from default
21078 template argument substitution. */
21079 vec<deferred_access_check, va_gc> *checks = NULL;
21080 /* We don't have all the template args yet. */
21081 bool incomplete = true;
21082
21083 tree orig_fn = fn;
21084 if (flag_new_inheriting_ctors)
21085 fn = strip_inheriting_ctors (fn);
21086
21087 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21088 tree r = error_mark_node;
21089
21090 tree full_targs = targs;
21091 if (TMPL_ARGS_DEPTH (targs)
21092 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21093 full_targs = (add_outermost_template_args
21094 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21095 targs));
21096
21097 if (decltype_p)
21098 complain |= tf_decltype;
21099
21100 /* In C++0x, it's possible to have a function template whose type depends
21101 on itself recursively. This is most obvious with decltype, but can also
21102 occur with enumeration scope (c++/48969). So we need to catch infinite
21103 recursion and reject the substitution at deduction time; this function
21104 will return error_mark_node for any repeated substitution.
21105
21106 This also catches excessive recursion such as when f<N> depends on
21107 f<N-1> across all integers, and returns error_mark_node for all the
21108 substitutions back up to the initial one.
21109
21110 This is, of course, not reentrant. */
21111 if (excessive_deduction_depth)
21112 return error_mark_node;
21113 ++deduction_depth;
21114
21115 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21116
21117 fntype = TREE_TYPE (fn);
21118 if (explicit_targs)
21119 {
21120 /* [temp.deduct]
21121
21122 The specified template arguments must match the template
21123 parameters in kind (i.e., type, nontype, template), and there
21124 must not be more arguments than there are parameters;
21125 otherwise type deduction fails.
21126
21127 Nontype arguments must match the types of the corresponding
21128 nontype template parameters, or must be convertible to the
21129 types of the corresponding nontype parameters as specified in
21130 _temp.arg.nontype_, otherwise type deduction fails.
21131
21132 All references in the function type of the function template
21133 to the corresponding template parameters are replaced by the
21134 specified template argument values. If a substitution in a
21135 template parameter or in the function type of the function
21136 template results in an invalid type, type deduction fails. */
21137 int i, len = TREE_VEC_LENGTH (tparms);
21138 location_t loc = input_location;
21139 incomplete = false;
21140
21141 if (explicit_targs == error_mark_node)
21142 goto fail;
21143
21144 if (TMPL_ARGS_DEPTH (explicit_targs)
21145 < TMPL_ARGS_DEPTH (full_targs))
21146 explicit_targs = add_outermost_template_args (full_targs,
21147 explicit_targs);
21148
21149 /* Adjust any explicit template arguments before entering the
21150 substitution context. */
21151 explicit_targs
21152 = (coerce_template_parms (tparms, explicit_targs, fn,
21153 complain|tf_partial,
21154 /*require_all_args=*/false,
21155 /*use_default_args=*/false));
21156 if (explicit_targs == error_mark_node)
21157 goto fail;
21158
21159 /* Substitute the explicit args into the function type. This is
21160 necessary so that, for instance, explicitly declared function
21161 arguments can match null pointed constants. If we were given
21162 an incomplete set of explicit args, we must not do semantic
21163 processing during substitution as we could create partial
21164 instantiations. */
21165 for (i = 0; i < len; i++)
21166 {
21167 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21168 bool parameter_pack = false;
21169 tree targ = TREE_VEC_ELT (explicit_targs, i);
21170
21171 /* Dig out the actual parm. */
21172 if (TREE_CODE (parm) == TYPE_DECL
21173 || TREE_CODE (parm) == TEMPLATE_DECL)
21174 {
21175 parm = TREE_TYPE (parm);
21176 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21177 }
21178 else if (TREE_CODE (parm) == PARM_DECL)
21179 {
21180 parm = DECL_INITIAL (parm);
21181 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21182 }
21183
21184 if (targ == NULL_TREE)
21185 /* No explicit argument for this template parameter. */
21186 incomplete = true;
21187 else if (parameter_pack && pack_deducible_p (parm, fn))
21188 {
21189 /* Mark the argument pack as "incomplete". We could
21190 still deduce more arguments during unification.
21191 We remove this mark in type_unification_real. */
21192 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21193 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21194 = ARGUMENT_PACK_ARGS (targ);
21195
21196 /* We have some incomplete argument packs. */
21197 incomplete = true;
21198 }
21199 }
21200
21201 if (incomplete)
21202 {
21203 if (!push_tinst_level (fn, explicit_targs))
21204 {
21205 excessive_deduction_depth = true;
21206 goto fail;
21207 }
21208 ++processing_template_decl;
21209 input_location = DECL_SOURCE_LOCATION (fn);
21210 /* Ignore any access checks; we'll see them again in
21211 instantiate_template and they might have the wrong
21212 access path at this point. */
21213 push_deferring_access_checks (dk_deferred);
21214 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21215 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21216 pop_deferring_access_checks ();
21217 input_location = loc;
21218 --processing_template_decl;
21219 pop_tinst_level ();
21220
21221 if (fntype == error_mark_node)
21222 goto fail;
21223 }
21224
21225 /* Place the explicitly specified arguments in TARGS. */
21226 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21227 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21228 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21229 if (!incomplete && CHECKING_P
21230 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21231 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21232 (targs, NUM_TMPL_ARGS (explicit_targs));
21233 }
21234
21235 if (return_type && strict != DEDUCE_CALL)
21236 {
21237 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21238 new_args[0] = return_type;
21239 memcpy (new_args + 1, args, nargs * sizeof (tree));
21240 args = new_args;
21241 ++nargs;
21242 }
21243
21244 if (!incomplete)
21245 goto deduced;
21246
21247 /* Never do unification on the 'this' parameter. */
21248 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21249
21250 if (return_type && strict == DEDUCE_CALL)
21251 {
21252 /* We're deducing for a call to the result of a template conversion
21253 function. The parms we really want are in return_type. */
21254 if (INDIRECT_TYPE_P (return_type))
21255 return_type = TREE_TYPE (return_type);
21256 parms = TYPE_ARG_TYPES (return_type);
21257 }
21258 else if (return_type)
21259 {
21260 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21261 }
21262
21263 /* We allow incomplete unification without an error message here
21264 because the standard doesn't seem to explicitly prohibit it. Our
21265 callers must be ready to deal with unification failures in any
21266 event. */
21267
21268 /* If we aren't explaining yet, push tinst context so we can see where
21269 any errors (e.g. from class instantiations triggered by instantiation
21270 of default template arguments) come from. If we are explaining, this
21271 context is redundant. */
21272 if (!explain_p && !push_tinst_level (fn, targs))
21273 {
21274 excessive_deduction_depth = true;
21275 goto fail;
21276 }
21277
21278 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21279 full_targs, parms, args, nargs, /*subr=*/0,
21280 strict, &checks, explain_p);
21281 if (!explain_p)
21282 pop_tinst_level ();
21283 if (!ok)
21284 goto fail;
21285
21286 /* Now that we have bindings for all of the template arguments,
21287 ensure that the arguments deduced for the template template
21288 parameters have compatible template parameter lists. We cannot
21289 check this property before we have deduced all template
21290 arguments, because the template parameter types of a template
21291 template parameter might depend on prior template parameters
21292 deduced after the template template parameter. The following
21293 ill-formed example illustrates this issue:
21294
21295 template<typename T, template<T> class C> void f(C<5>, T);
21296
21297 template<int N> struct X {};
21298
21299 void g() {
21300 f(X<5>(), 5l); // error: template argument deduction fails
21301 }
21302
21303 The template parameter list of 'C' depends on the template type
21304 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21305 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21306 time that we deduce 'C'. */
21307 if (!template_template_parm_bindings_ok_p
21308 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21309 {
21310 unify_inconsistent_template_template_parameters (explain_p);
21311 goto fail;
21312 }
21313
21314 deduced:
21315
21316 /* CWG2369: Check satisfaction before non-deducible conversions. */
21317 if (!constraints_satisfied_p (fn, targs))
21318 {
21319 if (explain_p)
21320 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21321 goto fail;
21322 }
21323
21324 /* DR 1391: All parameters have args, now check non-dependent parms for
21325 convertibility. We don't do this if all args were explicitly specified,
21326 as the standard says that we substitute explicit args immediately. */
21327 if (incomplete
21328 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21329 convs, explain_p))
21330 goto fail;
21331
21332 /* All is well so far. Now, check:
21333
21334 [temp.deduct]
21335
21336 When all template arguments have been deduced, all uses of
21337 template parameters in nondeduced contexts are replaced with
21338 the corresponding deduced argument values. If the
21339 substitution results in an invalid type, as described above,
21340 type deduction fails. */
21341 if (!push_tinst_level (fn, targs))
21342 {
21343 excessive_deduction_depth = true;
21344 goto fail;
21345 }
21346
21347 /* Also collect access checks from the instantiation. */
21348 reopen_deferring_access_checks (checks);
21349
21350 decl = instantiate_template (fn, targs, complain);
21351
21352 checks = get_deferred_access_checks ();
21353 pop_deferring_access_checks ();
21354
21355 pop_tinst_level ();
21356
21357 if (decl == error_mark_node)
21358 goto fail;
21359
21360 /* Now perform any access checks encountered during substitution. */
21361 push_access_scope (decl);
21362 ok = perform_access_checks (checks, complain);
21363 pop_access_scope (decl);
21364 if (!ok)
21365 goto fail;
21366
21367 /* If we're looking for an exact match, check that what we got
21368 is indeed an exact match. It might not be if some template
21369 parameters are used in non-deduced contexts. But don't check
21370 for an exact match if we have dependent template arguments;
21371 in that case we're doing partial ordering, and we already know
21372 that we have two candidates that will provide the actual type. */
21373 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21374 {
21375 tree substed = TREE_TYPE (decl);
21376 unsigned int i;
21377
21378 tree sarg
21379 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21380 if (return_type)
21381 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21382 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21383 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21384 {
21385 unify_type_mismatch (explain_p, args[i],
21386 TREE_VALUE (sarg));
21387 goto fail;
21388 }
21389 }
21390
21391 /* After doing deduction with the inherited constructor, actually return an
21392 instantiation of the inheriting constructor. */
21393 if (orig_fn != fn)
21394 decl = instantiate_template (orig_fn, targs, complain);
21395
21396 r = decl;
21397
21398 fail:
21399 --deduction_depth;
21400 if (excessive_deduction_depth)
21401 {
21402 if (deduction_depth == 0)
21403 /* Reset once we're all the way out. */
21404 excessive_deduction_depth = false;
21405 }
21406
21407 return r;
21408 }
21409
21410 /* Adjust types before performing type deduction, as described in
21411 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21412 sections are symmetric. PARM is the type of a function parameter
21413 or the return type of the conversion function. ARG is the type of
21414 the argument passed to the call, or the type of the value
21415 initialized with the result of the conversion function.
21416 ARG_EXPR is the original argument expression, which may be null. */
21417
21418 static int
21419 maybe_adjust_types_for_deduction (unification_kind_t strict,
21420 tree* parm,
21421 tree* arg,
21422 tree arg_expr)
21423 {
21424 int result = 0;
21425
21426 switch (strict)
21427 {
21428 case DEDUCE_CALL:
21429 break;
21430
21431 case DEDUCE_CONV:
21432 /* Swap PARM and ARG throughout the remainder of this
21433 function; the handling is precisely symmetric since PARM
21434 will initialize ARG rather than vice versa. */
21435 std::swap (parm, arg);
21436 break;
21437
21438 case DEDUCE_EXACT:
21439 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21440 too, but here handle it by stripping the reference from PARM
21441 rather than by adding it to ARG. */
21442 if (TYPE_REF_P (*parm)
21443 && TYPE_REF_IS_RVALUE (*parm)
21444 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21445 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21446 && TYPE_REF_P (*arg)
21447 && !TYPE_REF_IS_RVALUE (*arg))
21448 *parm = TREE_TYPE (*parm);
21449 /* Nothing else to do in this case. */
21450 return 0;
21451
21452 default:
21453 gcc_unreachable ();
21454 }
21455
21456 if (!TYPE_REF_P (*parm))
21457 {
21458 /* [temp.deduct.call]
21459
21460 If P is not a reference type:
21461
21462 --If A is an array type, the pointer type produced by the
21463 array-to-pointer standard conversion (_conv.array_) is
21464 used in place of A for type deduction; otherwise,
21465
21466 --If A is a function type, the pointer type produced by
21467 the function-to-pointer standard conversion
21468 (_conv.func_) is used in place of A for type deduction;
21469 otherwise,
21470
21471 --If A is a cv-qualified type, the top level
21472 cv-qualifiers of A's type are ignored for type
21473 deduction. */
21474 if (TREE_CODE (*arg) == ARRAY_TYPE)
21475 *arg = build_pointer_type (TREE_TYPE (*arg));
21476 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21477 *arg = build_pointer_type (*arg);
21478 else
21479 *arg = TYPE_MAIN_VARIANT (*arg);
21480 }
21481
21482 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21483 reference to a cv-unqualified template parameter that does not represent a
21484 template parameter of a class template (during class template argument
21485 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21486 an lvalue, the type "lvalue reference to A" is used in place of A for type
21487 deduction. */
21488 if (TYPE_REF_P (*parm)
21489 && TYPE_REF_IS_RVALUE (*parm)
21490 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21491 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21492 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21493 && (arg_expr ? lvalue_p (arg_expr)
21494 /* try_one_overload doesn't provide an arg_expr, but
21495 functions are always lvalues. */
21496 : TREE_CODE (*arg) == FUNCTION_TYPE))
21497 *arg = build_reference_type (*arg);
21498
21499 /* [temp.deduct.call]
21500
21501 If P is a cv-qualified type, the top level cv-qualifiers
21502 of P's type are ignored for type deduction. If P is a
21503 reference type, the type referred to by P is used for
21504 type deduction. */
21505 *parm = TYPE_MAIN_VARIANT (*parm);
21506 if (TYPE_REF_P (*parm))
21507 {
21508 *parm = TREE_TYPE (*parm);
21509 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21510 }
21511
21512 /* DR 322. For conversion deduction, remove a reference type on parm
21513 too (which has been swapped into ARG). */
21514 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21515 *arg = TREE_TYPE (*arg);
21516
21517 return result;
21518 }
21519
21520 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21521 template which doesn't contain any deducible template parameters; check if
21522 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21523 unify_one_argument. */
21524
21525 static int
21526 check_non_deducible_conversion (tree parm, tree arg, int strict,
21527 int flags, struct conversion **conv_p,
21528 bool explain_p)
21529 {
21530 tree type;
21531
21532 if (!TYPE_P (arg))
21533 type = TREE_TYPE (arg);
21534 else
21535 type = arg;
21536
21537 if (same_type_p (parm, type))
21538 return unify_success (explain_p);
21539
21540 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21541 if (strict == DEDUCE_CONV)
21542 {
21543 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21544 return unify_success (explain_p);
21545 }
21546 else if (strict != DEDUCE_EXACT)
21547 {
21548 bool ok = false;
21549 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21550 if (conv_p)
21551 /* Avoid recalculating this in add_function_candidate. */
21552 ok = (*conv_p
21553 = good_conversion (parm, type, conv_arg, flags, complain));
21554 else
21555 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21556 if (ok)
21557 return unify_success (explain_p);
21558 }
21559
21560 if (strict == DEDUCE_EXACT)
21561 return unify_type_mismatch (explain_p, parm, arg);
21562 else
21563 return unify_arg_conversion (explain_p, parm, type, arg);
21564 }
21565
21566 static bool uses_deducible_template_parms (tree type);
21567
21568 /* Returns true iff the expression EXPR is one from which a template
21569 argument can be deduced. In other words, if it's an undecorated
21570 use of a template non-type parameter. */
21571
21572 static bool
21573 deducible_expression (tree expr)
21574 {
21575 /* Strip implicit conversions. */
21576 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21577 expr = TREE_OPERAND (expr, 0);
21578 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21579 }
21580
21581 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21582 deducible way; that is, if it has a max value of <PARM> - 1. */
21583
21584 static bool
21585 deducible_array_bound (tree domain)
21586 {
21587 if (domain == NULL_TREE)
21588 return false;
21589
21590 tree max = TYPE_MAX_VALUE (domain);
21591 if (TREE_CODE (max) != MINUS_EXPR)
21592 return false;
21593
21594 return deducible_expression (TREE_OPERAND (max, 0));
21595 }
21596
21597 /* Returns true iff the template arguments ARGS use a template parameter
21598 in a deducible way. */
21599
21600 static bool
21601 deducible_template_args (tree args)
21602 {
21603 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21604 {
21605 bool deducible;
21606 tree elt = TREE_VEC_ELT (args, i);
21607 if (ARGUMENT_PACK_P (elt))
21608 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21609 else
21610 {
21611 if (PACK_EXPANSION_P (elt))
21612 elt = PACK_EXPANSION_PATTERN (elt);
21613 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21614 deducible = true;
21615 else if (TYPE_P (elt))
21616 deducible = uses_deducible_template_parms (elt);
21617 else
21618 deducible = deducible_expression (elt);
21619 }
21620 if (deducible)
21621 return true;
21622 }
21623 return false;
21624 }
21625
21626 /* Returns true iff TYPE contains any deducible references to template
21627 parameters, as per 14.8.2.5. */
21628
21629 static bool
21630 uses_deducible_template_parms (tree type)
21631 {
21632 if (PACK_EXPANSION_P (type))
21633 type = PACK_EXPANSION_PATTERN (type);
21634
21635 /* T
21636 cv-list T
21637 TT<T>
21638 TT<i>
21639 TT<> */
21640 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21641 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21642 return true;
21643
21644 /* T*
21645 T&
21646 T&& */
21647 if (INDIRECT_TYPE_P (type))
21648 return uses_deducible_template_parms (TREE_TYPE (type));
21649
21650 /* T[integer-constant ]
21651 type [i] */
21652 if (TREE_CODE (type) == ARRAY_TYPE)
21653 return (uses_deducible_template_parms (TREE_TYPE (type))
21654 || deducible_array_bound (TYPE_DOMAIN (type)));
21655
21656 /* T type ::*
21657 type T::*
21658 T T::*
21659 T (type ::*)()
21660 type (T::*)()
21661 type (type ::*)(T)
21662 type (T::*)(T)
21663 T (type ::*)(T)
21664 T (T::*)()
21665 T (T::*)(T) */
21666 if (TYPE_PTRMEM_P (type))
21667 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21668 || (uses_deducible_template_parms
21669 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21670
21671 /* template-name <T> (where template-name refers to a class template)
21672 template-name <i> (where template-name refers to a class template) */
21673 if (CLASS_TYPE_P (type)
21674 && CLASSTYPE_TEMPLATE_INFO (type)
21675 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21676 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21677 (CLASSTYPE_TI_ARGS (type)));
21678
21679 /* type (T)
21680 T()
21681 T(T) */
21682 if (FUNC_OR_METHOD_TYPE_P (type))
21683 {
21684 if (uses_deducible_template_parms (TREE_TYPE (type)))
21685 return true;
21686 tree parm = TYPE_ARG_TYPES (type);
21687 if (TREE_CODE (type) == METHOD_TYPE)
21688 parm = TREE_CHAIN (parm);
21689 for (; parm; parm = TREE_CHAIN (parm))
21690 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21691 return true;
21692 }
21693
21694 return false;
21695 }
21696
21697 /* Subroutine of type_unification_real and unify_pack_expansion to
21698 handle unification of a single P/A pair. Parameters are as
21699 for those functions. */
21700
21701 static int
21702 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21703 int subr, unification_kind_t strict,
21704 bool explain_p)
21705 {
21706 tree arg_expr = NULL_TREE;
21707 int arg_strict;
21708
21709 if (arg == error_mark_node || parm == error_mark_node)
21710 return unify_invalid (explain_p);
21711 if (arg == unknown_type_node)
21712 /* We can't deduce anything from this, but we might get all the
21713 template args from other function args. */
21714 return unify_success (explain_p);
21715
21716 /* Implicit conversions (Clause 4) will be performed on a function
21717 argument to convert it to the type of the corresponding function
21718 parameter if the parameter type contains no template-parameters that
21719 participate in template argument deduction. */
21720 if (strict != DEDUCE_EXACT
21721 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21722 /* For function parameters with no deducible template parameters,
21723 just return. We'll check non-dependent conversions later. */
21724 return unify_success (explain_p);
21725
21726 switch (strict)
21727 {
21728 case DEDUCE_CALL:
21729 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21730 | UNIFY_ALLOW_MORE_CV_QUAL
21731 | UNIFY_ALLOW_DERIVED);
21732 break;
21733
21734 case DEDUCE_CONV:
21735 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21736 break;
21737
21738 case DEDUCE_EXACT:
21739 arg_strict = UNIFY_ALLOW_NONE;
21740 break;
21741
21742 default:
21743 gcc_unreachable ();
21744 }
21745
21746 /* We only do these transformations if this is the top-level
21747 parameter_type_list in a call or declaration matching; in other
21748 situations (nested function declarators, template argument lists) we
21749 won't be comparing a type to an expression, and we don't do any type
21750 adjustments. */
21751 if (!subr)
21752 {
21753 if (!TYPE_P (arg))
21754 {
21755 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21756 if (type_unknown_p (arg))
21757 {
21758 /* [temp.deduct.type] A template-argument can be
21759 deduced from a pointer to function or pointer
21760 to member function argument if the set of
21761 overloaded functions does not contain function
21762 templates and at most one of a set of
21763 overloaded functions provides a unique
21764 match. */
21765 resolve_overloaded_unification (tparms, targs, parm,
21766 arg, strict,
21767 arg_strict, explain_p);
21768 /* If a unique match was not found, this is a
21769 non-deduced context, so we still succeed. */
21770 return unify_success (explain_p);
21771 }
21772
21773 arg_expr = arg;
21774 arg = unlowered_expr_type (arg);
21775 if (arg == error_mark_node)
21776 return unify_invalid (explain_p);
21777 }
21778
21779 arg_strict |=
21780 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21781 }
21782 else
21783 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21784 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21785 return unify_template_argument_mismatch (explain_p, parm, arg);
21786
21787 /* For deduction from an init-list we need the actual list. */
21788 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21789 arg = arg_expr;
21790 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21791 }
21792
21793 /* for_each_template_parm callback that always returns 0. */
21794
21795 static int
21796 zero_r (tree, void *)
21797 {
21798 return 0;
21799 }
21800
21801 /* for_each_template_parm any_fn callback to handle deduction of a template
21802 type argument from the type of an array bound. */
21803
21804 static int
21805 array_deduction_r (tree t, void *data)
21806 {
21807 tree_pair_p d = (tree_pair_p)data;
21808 tree &tparms = d->purpose;
21809 tree &targs = d->value;
21810
21811 if (TREE_CODE (t) == ARRAY_TYPE)
21812 if (tree dom = TYPE_DOMAIN (t))
21813 if (tree max = TYPE_MAX_VALUE (dom))
21814 {
21815 if (TREE_CODE (max) == MINUS_EXPR)
21816 max = TREE_OPERAND (max, 0);
21817 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21818 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21819 UNIFY_ALLOW_NONE, /*explain*/false);
21820 }
21821
21822 /* Keep walking. */
21823 return 0;
21824 }
21825
21826 /* Try to deduce any not-yet-deduced template type arguments from the type of
21827 an array bound. This is handled separately from unify because 14.8.2.5 says
21828 "The type of a type parameter is only deduced from an array bound if it is
21829 not otherwise deduced." */
21830
21831 static void
21832 try_array_deduction (tree tparms, tree targs, tree parm)
21833 {
21834 tree_pair_s data = { tparms, targs };
21835 hash_set<tree> visited;
21836 for_each_template_parm (parm, zero_r, &data, &visited,
21837 /*nondeduced*/false, array_deduction_r);
21838 }
21839
21840 /* Most parms like fn_type_unification.
21841
21842 If SUBR is 1, we're being called recursively (to unify the
21843 arguments of a function or method parameter of a function
21844 template).
21845
21846 CHECKS is a pointer to a vector of access checks encountered while
21847 substituting default template arguments. */
21848
21849 static int
21850 type_unification_real (tree tparms,
21851 tree full_targs,
21852 tree xparms,
21853 const tree *xargs,
21854 unsigned int xnargs,
21855 int subr,
21856 unification_kind_t strict,
21857 vec<deferred_access_check, va_gc> **checks,
21858 bool explain_p)
21859 {
21860 tree parm, arg;
21861 int i;
21862 int ntparms = TREE_VEC_LENGTH (tparms);
21863 int saw_undeduced = 0;
21864 tree parms;
21865 const tree *args;
21866 unsigned int nargs;
21867 unsigned int ia;
21868
21869 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21870 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21871 gcc_assert (ntparms > 0);
21872
21873 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21874
21875 /* Reset the number of non-defaulted template arguments contained
21876 in TARGS. */
21877 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21878
21879 again:
21880 parms = xparms;
21881 args = xargs;
21882 nargs = xnargs;
21883
21884 ia = 0;
21885 while (parms && parms != void_list_node
21886 && ia < nargs)
21887 {
21888 parm = TREE_VALUE (parms);
21889
21890 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21891 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21892 /* For a function parameter pack that occurs at the end of the
21893 parameter-declaration-list, the type A of each remaining
21894 argument of the call is compared with the type P of the
21895 declarator-id of the function parameter pack. */
21896 break;
21897
21898 parms = TREE_CHAIN (parms);
21899
21900 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21901 /* For a function parameter pack that does not occur at the
21902 end of the parameter-declaration-list, the type of the
21903 parameter pack is a non-deduced context. */
21904 continue;
21905
21906 arg = args[ia];
21907 ++ia;
21908
21909 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21910 explain_p))
21911 return 1;
21912 }
21913
21914 if (parms
21915 && parms != void_list_node
21916 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21917 {
21918 /* Unify the remaining arguments with the pack expansion type. */
21919 tree argvec;
21920 tree parmvec = make_tree_vec (1);
21921
21922 /* Allocate a TREE_VEC and copy in all of the arguments */
21923 argvec = make_tree_vec (nargs - ia);
21924 for (i = 0; ia < nargs; ++ia, ++i)
21925 TREE_VEC_ELT (argvec, i) = args[ia];
21926
21927 /* Copy the parameter into parmvec. */
21928 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21929 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21930 /*subr=*/subr, explain_p))
21931 return 1;
21932
21933 /* Advance to the end of the list of parameters. */
21934 parms = TREE_CHAIN (parms);
21935 }
21936
21937 /* Fail if we've reached the end of the parm list, and more args
21938 are present, and the parm list isn't variadic. */
21939 if (ia < nargs && parms == void_list_node)
21940 return unify_too_many_arguments (explain_p, nargs, ia);
21941 /* Fail if parms are left and they don't have default values and
21942 they aren't all deduced as empty packs (c++/57397). This is
21943 consistent with sufficient_parms_p. */
21944 if (parms && parms != void_list_node
21945 && TREE_PURPOSE (parms) == NULL_TREE)
21946 {
21947 unsigned int count = nargs;
21948 tree p = parms;
21949 bool type_pack_p;
21950 do
21951 {
21952 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21953 if (!type_pack_p)
21954 count++;
21955 p = TREE_CHAIN (p);
21956 }
21957 while (p && p != void_list_node);
21958 if (count != nargs)
21959 return unify_too_few_arguments (explain_p, ia, count,
21960 type_pack_p);
21961 }
21962
21963 if (!subr)
21964 {
21965 tsubst_flags_t complain = (explain_p
21966 ? tf_warning_or_error
21967 : tf_none);
21968 bool tried_array_deduction = (cxx_dialect < cxx17);
21969
21970 for (i = 0; i < ntparms; i++)
21971 {
21972 tree targ = TREE_VEC_ELT (targs, i);
21973 tree tparm = TREE_VEC_ELT (tparms, i);
21974
21975 /* Clear the "incomplete" flags on all argument packs now so that
21976 substituting them into later default arguments works. */
21977 if (targ && ARGUMENT_PACK_P (targ))
21978 {
21979 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21980 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21981 }
21982
21983 if (targ || tparm == error_mark_node)
21984 continue;
21985 tparm = TREE_VALUE (tparm);
21986
21987 if (TREE_CODE (tparm) == TYPE_DECL
21988 && !tried_array_deduction)
21989 {
21990 try_array_deduction (tparms, targs, xparms);
21991 tried_array_deduction = true;
21992 if (TREE_VEC_ELT (targs, i))
21993 continue;
21994 }
21995
21996 /* If this is an undeduced nontype parameter that depends on
21997 a type parameter, try another pass; its type may have been
21998 deduced from a later argument than the one from which
21999 this parameter can be deduced. */
22000 if (TREE_CODE (tparm) == PARM_DECL
22001 && uses_template_parms (TREE_TYPE (tparm))
22002 && saw_undeduced < 2)
22003 {
22004 saw_undeduced = 1;
22005 continue;
22006 }
22007
22008 /* Core issue #226 (C++0x) [temp.deduct]:
22009
22010 If a template argument has not been deduced, its
22011 default template argument, if any, is used.
22012
22013 When we are in C++98 mode, TREE_PURPOSE will either
22014 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22015 to explicitly check cxx_dialect here. */
22016 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22017 /* OK, there is a default argument. Wait until after the
22018 conversion check to do substitution. */
22019 continue;
22020
22021 /* If the type parameter is a parameter pack, then it will
22022 be deduced to an empty parameter pack. */
22023 if (template_parameter_pack_p (tparm))
22024 {
22025 tree arg;
22026
22027 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22028 {
22029 arg = make_node (NONTYPE_ARGUMENT_PACK);
22030 TREE_CONSTANT (arg) = 1;
22031 }
22032 else
22033 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22034
22035 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22036
22037 TREE_VEC_ELT (targs, i) = arg;
22038 continue;
22039 }
22040
22041 return unify_parameter_deduction_failure (explain_p, tparm);
22042 }
22043
22044 /* Now substitute into the default template arguments. */
22045 for (i = 0; i < ntparms; i++)
22046 {
22047 tree targ = TREE_VEC_ELT (targs, i);
22048 tree tparm = TREE_VEC_ELT (tparms, i);
22049
22050 if (targ || tparm == error_mark_node)
22051 continue;
22052 tree parm = TREE_VALUE (tparm);
22053 tree arg = TREE_PURPOSE (tparm);
22054 reopen_deferring_access_checks (*checks);
22055 location_t save_loc = input_location;
22056 if (DECL_P (parm))
22057 input_location = DECL_SOURCE_LOCATION (parm);
22058
22059 if (saw_undeduced == 1
22060 && TREE_CODE (parm) == PARM_DECL
22061 && uses_template_parms (TREE_TYPE (parm)))
22062 {
22063 /* The type of this non-type parameter depends on undeduced
22064 parameters. Don't try to use its default argument yet,
22065 since we might deduce an argument for it on the next pass,
22066 but do check whether the arguments we already have cause
22067 substitution failure, so that that happens before we try
22068 later default arguments (78489). */
22069 ++processing_template_decl;
22070 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22071 NULL_TREE);
22072 --processing_template_decl;
22073 if (type == error_mark_node)
22074 arg = error_mark_node;
22075 else
22076 arg = NULL_TREE;
22077 }
22078 else
22079 {
22080 /* Even if the call is happening in template context, getting
22081 here means it's non-dependent, and a default argument is
22082 considered a separate definition under [temp.decls], so we can
22083 do this substitution without processing_template_decl. This
22084 is important if the default argument contains something that
22085 might be instantiation-dependent like access (87480). */
22086 processing_template_decl_sentinel s;
22087 tree substed = NULL_TREE;
22088 if (saw_undeduced == 1)
22089 {
22090 /* First instatiate in template context, in case we still
22091 depend on undeduced template parameters. */
22092 ++processing_template_decl;
22093 substed = tsubst_template_arg (arg, full_targs, complain,
22094 NULL_TREE);
22095 --processing_template_decl;
22096 if (substed != error_mark_node
22097 && !uses_template_parms (substed))
22098 /* We replaced all the tparms, substitute again out of
22099 template context. */
22100 substed = NULL_TREE;
22101 }
22102 if (!substed)
22103 substed = tsubst_template_arg (arg, full_targs, complain,
22104 NULL_TREE);
22105
22106 if (!uses_template_parms (substed))
22107 arg = convert_template_argument (parm, substed, full_targs,
22108 complain, i, NULL_TREE);
22109 else if (saw_undeduced == 1)
22110 arg = NULL_TREE;
22111 else
22112 arg = error_mark_node;
22113 }
22114
22115 input_location = save_loc;
22116 *checks = get_deferred_access_checks ();
22117 pop_deferring_access_checks ();
22118
22119 if (arg == error_mark_node)
22120 return 1;
22121 else if (arg)
22122 {
22123 TREE_VEC_ELT (targs, i) = arg;
22124 /* The position of the first default template argument,
22125 is also the number of non-defaulted arguments in TARGS.
22126 Record that. */
22127 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22128 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22129 }
22130 }
22131
22132 if (saw_undeduced++ == 1)
22133 goto again;
22134 }
22135
22136 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22137 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22138
22139 return unify_success (explain_p);
22140 }
22141
22142 /* Subroutine of type_unification_real. Args are like the variables
22143 at the call site. ARG is an overloaded function (or template-id);
22144 we try deducing template args from each of the overloads, and if
22145 only one succeeds, we go with that. Modifies TARGS and returns
22146 true on success. */
22147
22148 static bool
22149 resolve_overloaded_unification (tree tparms,
22150 tree targs,
22151 tree parm,
22152 tree arg,
22153 unification_kind_t strict,
22154 int sub_strict,
22155 bool explain_p)
22156 {
22157 tree tempargs = copy_node (targs);
22158 int good = 0;
22159 tree goodfn = NULL_TREE;
22160 bool addr_p;
22161
22162 if (TREE_CODE (arg) == ADDR_EXPR)
22163 {
22164 arg = TREE_OPERAND (arg, 0);
22165 addr_p = true;
22166 }
22167 else
22168 addr_p = false;
22169
22170 if (TREE_CODE (arg) == COMPONENT_REF)
22171 /* Handle `&x' where `x' is some static or non-static member
22172 function name. */
22173 arg = TREE_OPERAND (arg, 1);
22174
22175 if (TREE_CODE (arg) == OFFSET_REF)
22176 arg = TREE_OPERAND (arg, 1);
22177
22178 /* Strip baselink information. */
22179 if (BASELINK_P (arg))
22180 arg = BASELINK_FUNCTIONS (arg);
22181
22182 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22183 {
22184 /* If we got some explicit template args, we need to plug them into
22185 the affected templates before we try to unify, in case the
22186 explicit args will completely resolve the templates in question. */
22187
22188 int ok = 0;
22189 tree expl_subargs = TREE_OPERAND (arg, 1);
22190 arg = TREE_OPERAND (arg, 0);
22191
22192 for (lkp_iterator iter (arg); iter; ++iter)
22193 {
22194 tree fn = *iter;
22195 tree subargs, elem;
22196
22197 if (TREE_CODE (fn) != TEMPLATE_DECL)
22198 continue;
22199
22200 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22201 expl_subargs, NULL_TREE, tf_none,
22202 /*require_all_args=*/true,
22203 /*use_default_args=*/true);
22204 if (subargs != error_mark_node
22205 && !any_dependent_template_arguments_p (subargs))
22206 {
22207 fn = instantiate_template (fn, subargs, tf_none);
22208 if (!constraints_satisfied_p (fn))
22209 continue;
22210 if (undeduced_auto_decl (fn))
22211 {
22212 /* Instantiate the function to deduce its return type. */
22213 ++function_depth;
22214 instantiate_decl (fn, /*defer*/false, /*class*/false);
22215 --function_depth;
22216 }
22217
22218 elem = TREE_TYPE (fn);
22219 if (try_one_overload (tparms, targs, tempargs, parm,
22220 elem, strict, sub_strict, addr_p, explain_p)
22221 && (!goodfn || !same_type_p (goodfn, elem)))
22222 {
22223 goodfn = elem;
22224 ++good;
22225 }
22226 }
22227 else if (subargs)
22228 ++ok;
22229 }
22230 /* If no templates (or more than one) are fully resolved by the
22231 explicit arguments, this template-id is a non-deduced context; it
22232 could still be OK if we deduce all template arguments for the
22233 enclosing call through other arguments. */
22234 if (good != 1)
22235 good = ok;
22236 }
22237 else if (!OVL_P (arg))
22238 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22239 -- but the deduction does not succeed because the expression is
22240 not just the function on its own. */
22241 return false;
22242 else
22243 for (lkp_iterator iter (arg); iter; ++iter)
22244 {
22245 tree fn = *iter;
22246 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22247 strict, sub_strict, addr_p, explain_p)
22248 && (!goodfn || !decls_match (goodfn, fn)))
22249 {
22250 goodfn = fn;
22251 ++good;
22252 }
22253 }
22254
22255 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22256 to function or pointer to member function argument if the set of
22257 overloaded functions does not contain function templates and at most
22258 one of a set of overloaded functions provides a unique match.
22259
22260 So if we found multiple possibilities, we return success but don't
22261 deduce anything. */
22262
22263 if (good == 1)
22264 {
22265 int i = TREE_VEC_LENGTH (targs);
22266 for (; i--; )
22267 if (TREE_VEC_ELT (tempargs, i))
22268 {
22269 tree old = TREE_VEC_ELT (targs, i);
22270 tree new_ = TREE_VEC_ELT (tempargs, i);
22271 if (new_ && old && ARGUMENT_PACK_P (old)
22272 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22273 /* Don't forget explicit template arguments in a pack. */
22274 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22275 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22276 TREE_VEC_ELT (targs, i) = new_;
22277 }
22278 }
22279 if (good)
22280 return true;
22281
22282 return false;
22283 }
22284
22285 /* Core DR 115: In contexts where deduction is done and fails, or in
22286 contexts where deduction is not done, if a template argument list is
22287 specified and it, along with any default template arguments, identifies
22288 a single function template specialization, then the template-id is an
22289 lvalue for the function template specialization. */
22290
22291 tree
22292 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22293 {
22294 tree expr, offset, baselink;
22295 bool addr;
22296
22297 if (!type_unknown_p (orig_expr))
22298 return orig_expr;
22299
22300 expr = orig_expr;
22301 addr = false;
22302 offset = NULL_TREE;
22303 baselink = NULL_TREE;
22304
22305 if (TREE_CODE (expr) == ADDR_EXPR)
22306 {
22307 expr = TREE_OPERAND (expr, 0);
22308 addr = true;
22309 }
22310 if (TREE_CODE (expr) == OFFSET_REF)
22311 {
22312 offset = expr;
22313 expr = TREE_OPERAND (expr, 1);
22314 }
22315 if (BASELINK_P (expr))
22316 {
22317 baselink = expr;
22318 expr = BASELINK_FUNCTIONS (expr);
22319 }
22320
22321 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22322 {
22323 int good = 0;
22324 tree goodfn = NULL_TREE;
22325
22326 /* If we got some explicit template args, we need to plug them into
22327 the affected templates before we try to unify, in case the
22328 explicit args will completely resolve the templates in question. */
22329
22330 tree expl_subargs = TREE_OPERAND (expr, 1);
22331 tree arg = TREE_OPERAND (expr, 0);
22332 tree badfn = NULL_TREE;
22333 tree badargs = NULL_TREE;
22334
22335 for (lkp_iterator iter (arg); iter; ++iter)
22336 {
22337 tree fn = *iter;
22338 tree subargs, elem;
22339
22340 if (TREE_CODE (fn) != TEMPLATE_DECL)
22341 continue;
22342
22343 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22344 expl_subargs, NULL_TREE, tf_none,
22345 /*require_all_args=*/true,
22346 /*use_default_args=*/true);
22347 if (subargs != error_mark_node
22348 && !any_dependent_template_arguments_p (subargs))
22349 {
22350 elem = instantiate_template (fn, subargs, tf_none);
22351 if (elem == error_mark_node)
22352 {
22353 badfn = fn;
22354 badargs = subargs;
22355 }
22356 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22357 && constraints_satisfied_p (elem))
22358 {
22359 goodfn = elem;
22360 ++good;
22361 }
22362 }
22363 }
22364 if (good == 1)
22365 {
22366 mark_used (goodfn);
22367 expr = goodfn;
22368 if (baselink)
22369 expr = build_baselink (BASELINK_BINFO (baselink),
22370 BASELINK_ACCESS_BINFO (baselink),
22371 expr, BASELINK_OPTYPE (baselink));
22372 if (offset)
22373 {
22374 tree base
22375 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22376 expr = build_offset_ref (base, expr, addr, complain);
22377 }
22378 if (addr)
22379 expr = cp_build_addr_expr (expr, complain);
22380 return expr;
22381 }
22382 else if (good == 0 && badargs && (complain & tf_error))
22383 /* There were no good options and at least one bad one, so let the
22384 user know what the problem is. */
22385 instantiate_template (badfn, badargs, complain);
22386 }
22387 return orig_expr;
22388 }
22389
22390 /* As above, but error out if the expression remains overloaded. */
22391
22392 tree
22393 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22394 {
22395 exp = resolve_nondeduced_context (exp, complain);
22396 if (type_unknown_p (exp))
22397 {
22398 if (complain & tf_error)
22399 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22400 return error_mark_node;
22401 }
22402 return exp;
22403 }
22404
22405 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22406 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22407 different overloads deduce different arguments for a given parm.
22408 ADDR_P is true if the expression for which deduction is being
22409 performed was of the form "& fn" rather than simply "fn".
22410
22411 Returns 1 on success. */
22412
22413 static int
22414 try_one_overload (tree tparms,
22415 tree orig_targs,
22416 tree targs,
22417 tree parm,
22418 tree arg,
22419 unification_kind_t strict,
22420 int sub_strict,
22421 bool addr_p,
22422 bool explain_p)
22423 {
22424 int nargs;
22425 tree tempargs;
22426 int i;
22427
22428 if (arg == error_mark_node)
22429 return 0;
22430
22431 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22432 to function or pointer to member function argument if the set of
22433 overloaded functions does not contain function templates and at most
22434 one of a set of overloaded functions provides a unique match.
22435
22436 So if this is a template, just return success. */
22437
22438 if (uses_template_parms (arg))
22439 return 1;
22440
22441 if (TREE_CODE (arg) == METHOD_TYPE)
22442 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22443 else if (addr_p)
22444 arg = build_pointer_type (arg);
22445
22446 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22447
22448 /* We don't copy orig_targs for this because if we have already deduced
22449 some template args from previous args, unify would complain when we
22450 try to deduce a template parameter for the same argument, even though
22451 there isn't really a conflict. */
22452 nargs = TREE_VEC_LENGTH (targs);
22453 tempargs = make_tree_vec (nargs);
22454
22455 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22456 return 0;
22457
22458 /* First make sure we didn't deduce anything that conflicts with
22459 explicitly specified args. */
22460 for (i = nargs; i--; )
22461 {
22462 tree elt = TREE_VEC_ELT (tempargs, i);
22463 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22464
22465 if (!elt)
22466 /*NOP*/;
22467 else if (uses_template_parms (elt))
22468 /* Since we're unifying against ourselves, we will fill in
22469 template args used in the function parm list with our own
22470 template parms. Discard them. */
22471 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22472 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22473 {
22474 /* Check that the argument at each index of the deduced argument pack
22475 is equivalent to the corresponding explicitly specified argument.
22476 We may have deduced more arguments than were explicitly specified,
22477 and that's OK. */
22478
22479 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22480 that's wrong if we deduce the same argument pack from multiple
22481 function arguments: it's only incomplete the first time. */
22482
22483 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22484 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22485
22486 if (TREE_VEC_LENGTH (deduced_pack)
22487 < TREE_VEC_LENGTH (explicit_pack))
22488 return 0;
22489
22490 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22491 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22492 TREE_VEC_ELT (deduced_pack, j)))
22493 return 0;
22494 }
22495 else if (oldelt && !template_args_equal (oldelt, elt))
22496 return 0;
22497 }
22498
22499 for (i = nargs; i--; )
22500 {
22501 tree elt = TREE_VEC_ELT (tempargs, i);
22502
22503 if (elt)
22504 TREE_VEC_ELT (targs, i) = elt;
22505 }
22506
22507 return 1;
22508 }
22509
22510 /* PARM is a template class (perhaps with unbound template
22511 parameters). ARG is a fully instantiated type. If ARG can be
22512 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22513 TARGS are as for unify. */
22514
22515 static tree
22516 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22517 bool explain_p)
22518 {
22519 tree copy_of_targs;
22520
22521 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22522 return NULL_TREE;
22523 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22524 /* Matches anything. */;
22525 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22526 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22527 return NULL_TREE;
22528
22529 /* We need to make a new template argument vector for the call to
22530 unify. If we used TARGS, we'd clutter it up with the result of
22531 the attempted unification, even if this class didn't work out.
22532 We also don't want to commit ourselves to all the unifications
22533 we've already done, since unification is supposed to be done on
22534 an argument-by-argument basis. In other words, consider the
22535 following pathological case:
22536
22537 template <int I, int J, int K>
22538 struct S {};
22539
22540 template <int I, int J>
22541 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22542
22543 template <int I, int J, int K>
22544 void f(S<I, J, K>, S<I, I, I>);
22545
22546 void g() {
22547 S<0, 0, 0> s0;
22548 S<0, 1, 2> s2;
22549
22550 f(s0, s2);
22551 }
22552
22553 Now, by the time we consider the unification involving `s2', we
22554 already know that we must have `f<0, 0, 0>'. But, even though
22555 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22556 because there are two ways to unify base classes of S<0, 1, 2>
22557 with S<I, I, I>. If we kept the already deduced knowledge, we
22558 would reject the possibility I=1. */
22559 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22560
22561 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22562 {
22563 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22564 return NULL_TREE;
22565 return arg;
22566 }
22567
22568 /* If unification failed, we're done. */
22569 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22570 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22571 return NULL_TREE;
22572
22573 return arg;
22574 }
22575
22576 /* Given a template type PARM and a class type ARG, find the unique
22577 base type in ARG that is an instance of PARM. We do not examine
22578 ARG itself; only its base-classes. If there is not exactly one
22579 appropriate base class, return NULL_TREE. PARM may be the type of
22580 a partial specialization, as well as a plain template type. Used
22581 by unify. */
22582
22583 static enum template_base_result
22584 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22585 bool explain_p, tree *result)
22586 {
22587 tree rval = NULL_TREE;
22588 tree binfo;
22589
22590 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22591
22592 binfo = TYPE_BINFO (complete_type (arg));
22593 if (!binfo)
22594 {
22595 /* The type could not be completed. */
22596 *result = NULL_TREE;
22597 return tbr_incomplete_type;
22598 }
22599
22600 /* Walk in inheritance graph order. The search order is not
22601 important, and this avoids multiple walks of virtual bases. */
22602 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22603 {
22604 tree r = try_class_unification (tparms, targs, parm,
22605 BINFO_TYPE (binfo), explain_p);
22606
22607 if (r)
22608 {
22609 /* If there is more than one satisfactory baseclass, then:
22610
22611 [temp.deduct.call]
22612
22613 If they yield more than one possible deduced A, the type
22614 deduction fails.
22615
22616 applies. */
22617 if (rval && !same_type_p (r, rval))
22618 {
22619 *result = NULL_TREE;
22620 return tbr_ambiguous_baseclass;
22621 }
22622
22623 rval = r;
22624 }
22625 }
22626
22627 *result = rval;
22628 return tbr_success;
22629 }
22630
22631 /* Returns the level of DECL, which declares a template parameter. */
22632
22633 static int
22634 template_decl_level (tree decl)
22635 {
22636 switch (TREE_CODE (decl))
22637 {
22638 case TYPE_DECL:
22639 case TEMPLATE_DECL:
22640 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22641
22642 case PARM_DECL:
22643 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22644
22645 default:
22646 gcc_unreachable ();
22647 }
22648 return 0;
22649 }
22650
22651 /* Decide whether ARG can be unified with PARM, considering only the
22652 cv-qualifiers of each type, given STRICT as documented for unify.
22653 Returns nonzero iff the unification is OK on that basis. */
22654
22655 static int
22656 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22657 {
22658 int arg_quals = cp_type_quals (arg);
22659 int parm_quals = cp_type_quals (parm);
22660
22661 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22662 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22663 {
22664 /* Although a CVR qualifier is ignored when being applied to a
22665 substituted template parameter ([8.3.2]/1 for example), that
22666 does not allow us to unify "const T" with "int&" because both
22667 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22668 It is ok when we're allowing additional CV qualifiers
22669 at the outer level [14.8.2.1]/3,1st bullet. */
22670 if ((TYPE_REF_P (arg)
22671 || FUNC_OR_METHOD_TYPE_P (arg))
22672 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22673 return 0;
22674
22675 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22676 && (parm_quals & TYPE_QUAL_RESTRICT))
22677 return 0;
22678 }
22679
22680 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22681 && (arg_quals & parm_quals) != parm_quals)
22682 return 0;
22683
22684 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22685 && (parm_quals & arg_quals) != arg_quals)
22686 return 0;
22687
22688 return 1;
22689 }
22690
22691 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22692 void
22693 template_parm_level_and_index (tree parm, int* level, int* index)
22694 {
22695 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22696 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22697 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22698 {
22699 *index = TEMPLATE_TYPE_IDX (parm);
22700 *level = TEMPLATE_TYPE_LEVEL (parm);
22701 }
22702 else
22703 {
22704 *index = TEMPLATE_PARM_IDX (parm);
22705 *level = TEMPLATE_PARM_LEVEL (parm);
22706 }
22707 }
22708
22709 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22710 do { \
22711 if (unify (TP, TA, P, A, S, EP)) \
22712 return 1; \
22713 } while (0)
22714
22715 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22716 expansion at the end of PACKED_PARMS. Returns 0 if the type
22717 deduction succeeds, 1 otherwise. STRICT is the same as in
22718 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22719 function call argument list. We'll need to adjust the arguments to make them
22720 types. SUBR tells us if this is from a recursive call to
22721 type_unification_real, or for comparing two template argument
22722 lists. */
22723
22724 static int
22725 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22726 tree packed_args, unification_kind_t strict,
22727 bool subr, bool explain_p)
22728 {
22729 tree parm
22730 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22731 tree pattern = PACK_EXPANSION_PATTERN (parm);
22732 tree pack, packs = NULL_TREE;
22733 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22734
22735 /* Add in any args remembered from an earlier partial instantiation. */
22736 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22737 int levels = TMPL_ARGS_DEPTH (targs);
22738
22739 packed_args = expand_template_argument_pack (packed_args);
22740
22741 int len = TREE_VEC_LENGTH (packed_args);
22742
22743 /* Determine the parameter packs we will be deducing from the
22744 pattern, and record their current deductions. */
22745 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22746 pack; pack = TREE_CHAIN (pack))
22747 {
22748 tree parm_pack = TREE_VALUE (pack);
22749 int idx, level;
22750
22751 /* Only template parameter packs can be deduced, not e.g. function
22752 parameter packs or __bases or __integer_pack. */
22753 if (!TEMPLATE_PARM_P (parm_pack))
22754 continue;
22755
22756 /* Determine the index and level of this parameter pack. */
22757 template_parm_level_and_index (parm_pack, &level, &idx);
22758 if (level < levels)
22759 continue;
22760
22761 /* Keep track of the parameter packs and their corresponding
22762 argument packs. */
22763 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22764 TREE_TYPE (packs) = make_tree_vec (len - start);
22765 }
22766
22767 /* Loop through all of the arguments that have not yet been
22768 unified and unify each with the pattern. */
22769 for (i = start; i < len; i++)
22770 {
22771 tree parm;
22772 bool any_explicit = false;
22773 tree arg = TREE_VEC_ELT (packed_args, i);
22774
22775 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22776 or the element of its argument pack at the current index if
22777 this argument was explicitly specified. */
22778 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22779 {
22780 int idx, level;
22781 tree arg, pargs;
22782 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22783
22784 arg = NULL_TREE;
22785 if (TREE_VALUE (pack)
22786 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22787 && (i - start < TREE_VEC_LENGTH (pargs)))
22788 {
22789 any_explicit = true;
22790 arg = TREE_VEC_ELT (pargs, i - start);
22791 }
22792 TMPL_ARG (targs, level, idx) = arg;
22793 }
22794
22795 /* If we had explicit template arguments, substitute them into the
22796 pattern before deduction. */
22797 if (any_explicit)
22798 {
22799 /* Some arguments might still be unspecified or dependent. */
22800 bool dependent;
22801 ++processing_template_decl;
22802 dependent = any_dependent_template_arguments_p (targs);
22803 if (!dependent)
22804 --processing_template_decl;
22805 parm = tsubst (pattern, targs,
22806 explain_p ? tf_warning_or_error : tf_none,
22807 NULL_TREE);
22808 if (dependent)
22809 --processing_template_decl;
22810 if (parm == error_mark_node)
22811 return 1;
22812 }
22813 else
22814 parm = pattern;
22815
22816 /* Unify the pattern with the current argument. */
22817 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22818 explain_p))
22819 return 1;
22820
22821 /* For each parameter pack, collect the deduced value. */
22822 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22823 {
22824 int idx, level;
22825 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22826
22827 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22828 TMPL_ARG (targs, level, idx);
22829 }
22830 }
22831
22832 /* Verify that the results of unification with the parameter packs
22833 produce results consistent with what we've seen before, and make
22834 the deduced argument packs available. */
22835 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22836 {
22837 tree old_pack = TREE_VALUE (pack);
22838 tree new_args = TREE_TYPE (pack);
22839 int i, len = TREE_VEC_LENGTH (new_args);
22840 int idx, level;
22841 bool nondeduced_p = false;
22842
22843 /* By default keep the original deduced argument pack.
22844 If necessary, more specific code is going to update the
22845 resulting deduced argument later down in this function. */
22846 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22847 TMPL_ARG (targs, level, idx) = old_pack;
22848
22849 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22850 actually deduce anything. */
22851 for (i = 0; i < len && !nondeduced_p; ++i)
22852 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22853 nondeduced_p = true;
22854 if (nondeduced_p)
22855 continue;
22856
22857 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22858 {
22859 /* If we had fewer function args than explicit template args,
22860 just use the explicits. */
22861 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22862 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22863 if (len < explicit_len)
22864 new_args = explicit_args;
22865 }
22866
22867 if (!old_pack)
22868 {
22869 tree result;
22870 /* Build the deduced *_ARGUMENT_PACK. */
22871 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22872 {
22873 result = make_node (NONTYPE_ARGUMENT_PACK);
22874 TREE_CONSTANT (result) = 1;
22875 }
22876 else
22877 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22878
22879 SET_ARGUMENT_PACK_ARGS (result, new_args);
22880
22881 /* Note the deduced argument packs for this parameter
22882 pack. */
22883 TMPL_ARG (targs, level, idx) = result;
22884 }
22885 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22886 && (ARGUMENT_PACK_ARGS (old_pack)
22887 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22888 {
22889 /* We only had the explicitly-provided arguments before, but
22890 now we have a complete set of arguments. */
22891 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22892
22893 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22894 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22895 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22896 }
22897 else
22898 {
22899 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22900 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22901 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22902 /* During template argument deduction for the aggregate deduction
22903 candidate, the number of elements in a trailing parameter pack
22904 is only deduced from the number of remaining function
22905 arguments if it is not otherwise deduced. */
22906 if (cxx_dialect >= cxx20
22907 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
22908 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
22909 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
22910 if (!comp_template_args (old_args, new_args,
22911 &bad_old_arg, &bad_new_arg))
22912 /* Inconsistent unification of this parameter pack. */
22913 return unify_parameter_pack_inconsistent (explain_p,
22914 bad_old_arg,
22915 bad_new_arg);
22916 }
22917 }
22918
22919 return unify_success (explain_p);
22920 }
22921
22922 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22923 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22924 parameters and return value are as for unify. */
22925
22926 static int
22927 unify_array_domain (tree tparms, tree targs,
22928 tree parm_dom, tree arg_dom,
22929 bool explain_p)
22930 {
22931 tree parm_max;
22932 tree arg_max;
22933 bool parm_cst;
22934 bool arg_cst;
22935
22936 /* Our representation of array types uses "N - 1" as the
22937 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22938 not an integer constant. We cannot unify arbitrarily
22939 complex expressions, so we eliminate the MINUS_EXPRs
22940 here. */
22941 parm_max = TYPE_MAX_VALUE (parm_dom);
22942 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22943 if (!parm_cst)
22944 {
22945 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22946 parm_max = TREE_OPERAND (parm_max, 0);
22947 }
22948 arg_max = TYPE_MAX_VALUE (arg_dom);
22949 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22950 if (!arg_cst)
22951 {
22952 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22953 trying to unify the type of a variable with the type
22954 of a template parameter. For example:
22955
22956 template <unsigned int N>
22957 void f (char (&) [N]);
22958 int g();
22959 void h(int i) {
22960 char a[g(i)];
22961 f(a);
22962 }
22963
22964 Here, the type of the ARG will be "int [g(i)]", and
22965 may be a SAVE_EXPR, etc. */
22966 if (TREE_CODE (arg_max) != MINUS_EXPR)
22967 return unify_vla_arg (explain_p, arg_dom);
22968 arg_max = TREE_OPERAND (arg_max, 0);
22969 }
22970
22971 /* If only one of the bounds used a MINUS_EXPR, compensate
22972 by adding one to the other bound. */
22973 if (parm_cst && !arg_cst)
22974 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22975 integer_type_node,
22976 parm_max,
22977 integer_one_node);
22978 else if (arg_cst && !parm_cst)
22979 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22980 integer_type_node,
22981 arg_max,
22982 integer_one_node);
22983
22984 return unify (tparms, targs, parm_max, arg_max,
22985 UNIFY_ALLOW_INTEGER, explain_p);
22986 }
22987
22988 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22989
22990 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22991
22992 static pa_kind_t
22993 pa_kind (tree t)
22994 {
22995 if (PACK_EXPANSION_P (t))
22996 t = PACK_EXPANSION_PATTERN (t);
22997 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22998 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22999 || DECL_TYPE_TEMPLATE_P (t))
23000 return pa_tmpl;
23001 else if (TYPE_P (t))
23002 return pa_type;
23003 else
23004 return pa_expr;
23005 }
23006
23007 /* Deduce the value of template parameters. TPARMS is the (innermost)
23008 set of template parameters to a template. TARGS is the bindings
23009 for those template parameters, as determined thus far; TARGS may
23010 include template arguments for outer levels of template parameters
23011 as well. PARM is a parameter to a template function, or a
23012 subcomponent of that parameter; ARG is the corresponding argument.
23013 This function attempts to match PARM with ARG in a manner
23014 consistent with the existing assignments in TARGS. If more values
23015 are deduced, then TARGS is updated.
23016
23017 Returns 0 if the type deduction succeeds, 1 otherwise. The
23018 parameter STRICT is a bitwise or of the following flags:
23019
23020 UNIFY_ALLOW_NONE:
23021 Require an exact match between PARM and ARG.
23022 UNIFY_ALLOW_MORE_CV_QUAL:
23023 Allow the deduced ARG to be more cv-qualified (by qualification
23024 conversion) than ARG.
23025 UNIFY_ALLOW_LESS_CV_QUAL:
23026 Allow the deduced ARG to be less cv-qualified than ARG.
23027 UNIFY_ALLOW_DERIVED:
23028 Allow the deduced ARG to be a template base class of ARG,
23029 or a pointer to a template base class of the type pointed to by
23030 ARG.
23031 UNIFY_ALLOW_INTEGER:
23032 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23033 case for more information.
23034 UNIFY_ALLOW_OUTER_LEVEL:
23035 This is the outermost level of a deduction. Used to determine validity
23036 of qualification conversions. A valid qualification conversion must
23037 have const qualified pointers leading up to the inner type which
23038 requires additional CV quals, except at the outer level, where const
23039 is not required [conv.qual]. It would be normal to set this flag in
23040 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23041 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23042 This is the outermost level of a deduction, and PARM can be more CV
23043 qualified at this point.
23044 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23045 This is the outermost level of a deduction, and PARM can be less CV
23046 qualified at this point. */
23047
23048 static int
23049 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23050 bool explain_p)
23051 {
23052 int idx;
23053 tree targ;
23054 tree tparm;
23055 int strict_in = strict;
23056 tsubst_flags_t complain = (explain_p
23057 ? tf_warning_or_error
23058 : tf_none);
23059
23060 /* I don't think this will do the right thing with respect to types.
23061 But the only case I've seen it in so far has been array bounds, where
23062 signedness is the only information lost, and I think that will be
23063 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23064 finish_id_expression_1, and are also OK. */
23065 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23066 parm = TREE_OPERAND (parm, 0);
23067
23068 if (arg == error_mark_node)
23069 return unify_invalid (explain_p);
23070 if (arg == unknown_type_node
23071 || arg == init_list_type_node)
23072 /* We can't deduce anything from this, but we might get all the
23073 template args from other function args. */
23074 return unify_success (explain_p);
23075
23076 if (parm == any_targ_node || arg == any_targ_node)
23077 return unify_success (explain_p);
23078
23079 /* If PARM uses template parameters, then we can't bail out here,
23080 even if ARG == PARM, since we won't record unifications for the
23081 template parameters. We might need them if we're trying to
23082 figure out which of two things is more specialized. */
23083 if (arg == parm && !uses_template_parms (parm))
23084 return unify_success (explain_p);
23085
23086 /* Handle init lists early, so the rest of the function can assume
23087 we're dealing with a type. */
23088 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23089 {
23090 tree elt, elttype;
23091 unsigned i;
23092 tree orig_parm = parm;
23093
23094 if (!is_std_init_list (parm)
23095 && TREE_CODE (parm) != ARRAY_TYPE)
23096 /* We can only deduce from an initializer list argument if the
23097 parameter is std::initializer_list or an array; otherwise this
23098 is a non-deduced context. */
23099 return unify_success (explain_p);
23100
23101 if (TREE_CODE (parm) == ARRAY_TYPE)
23102 elttype = TREE_TYPE (parm);
23103 else
23104 {
23105 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23106 /* Deduction is defined in terms of a single type, so just punt
23107 on the (bizarre) std::initializer_list<T...>. */
23108 if (PACK_EXPANSION_P (elttype))
23109 return unify_success (explain_p);
23110 }
23111
23112 if (strict != DEDUCE_EXACT
23113 && TYPE_P (elttype)
23114 && !uses_deducible_template_parms (elttype))
23115 /* If ELTTYPE has no deducible template parms, skip deduction from
23116 the list elements. */;
23117 else
23118 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23119 {
23120 int elt_strict = strict;
23121
23122 if (elt == error_mark_node)
23123 return unify_invalid (explain_p);
23124
23125 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23126 {
23127 tree type = TREE_TYPE (elt);
23128 if (type == error_mark_node)
23129 return unify_invalid (explain_p);
23130 /* It should only be possible to get here for a call. */
23131 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23132 elt_strict |= maybe_adjust_types_for_deduction
23133 (DEDUCE_CALL, &elttype, &type, elt);
23134 elt = type;
23135 }
23136
23137 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23138 explain_p);
23139 }
23140
23141 if (TREE_CODE (parm) == ARRAY_TYPE
23142 && deducible_array_bound (TYPE_DOMAIN (parm)))
23143 {
23144 /* Also deduce from the length of the initializer list. */
23145 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23146 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23147 if (idx == error_mark_node)
23148 return unify_invalid (explain_p);
23149 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23150 idx, explain_p);
23151 }
23152
23153 /* If the std::initializer_list<T> deduction worked, replace the
23154 deduced A with std::initializer_list<A>. */
23155 if (orig_parm != parm)
23156 {
23157 idx = TEMPLATE_TYPE_IDX (orig_parm);
23158 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23159 targ = listify (targ);
23160 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23161 }
23162 return unify_success (explain_p);
23163 }
23164
23165 /* If parm and arg aren't the same kind of thing (template, type, or
23166 expression), fail early. */
23167 if (pa_kind (parm) != pa_kind (arg))
23168 return unify_invalid (explain_p);
23169
23170 /* Immediately reject some pairs that won't unify because of
23171 cv-qualification mismatches. */
23172 if (TREE_CODE (arg) == TREE_CODE (parm)
23173 && TYPE_P (arg)
23174 /* It is the elements of the array which hold the cv quals of an array
23175 type, and the elements might be template type parms. We'll check
23176 when we recurse. */
23177 && TREE_CODE (arg) != ARRAY_TYPE
23178 /* We check the cv-qualifiers when unifying with template type
23179 parameters below. We want to allow ARG `const T' to unify with
23180 PARM `T' for example, when computing which of two templates
23181 is more specialized, for example. */
23182 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23183 && !check_cv_quals_for_unify (strict_in, arg, parm))
23184 return unify_cv_qual_mismatch (explain_p, parm, arg);
23185
23186 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23187 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23188 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23189 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23190 strict &= ~UNIFY_ALLOW_DERIVED;
23191 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23192 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23193
23194 switch (TREE_CODE (parm))
23195 {
23196 case TYPENAME_TYPE:
23197 case SCOPE_REF:
23198 case UNBOUND_CLASS_TEMPLATE:
23199 /* In a type which contains a nested-name-specifier, template
23200 argument values cannot be deduced for template parameters used
23201 within the nested-name-specifier. */
23202 return unify_success (explain_p);
23203
23204 case TEMPLATE_TYPE_PARM:
23205 case TEMPLATE_TEMPLATE_PARM:
23206 case BOUND_TEMPLATE_TEMPLATE_PARM:
23207 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23208 if (error_operand_p (tparm))
23209 return unify_invalid (explain_p);
23210
23211 if (TEMPLATE_TYPE_LEVEL (parm)
23212 != template_decl_level (tparm))
23213 /* The PARM is not one we're trying to unify. Just check
23214 to see if it matches ARG. */
23215 {
23216 if (TREE_CODE (arg) == TREE_CODE (parm)
23217 && (is_auto (parm) ? is_auto (arg)
23218 : same_type_p (parm, arg)))
23219 return unify_success (explain_p);
23220 else
23221 return unify_type_mismatch (explain_p, parm, arg);
23222 }
23223 idx = TEMPLATE_TYPE_IDX (parm);
23224 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23225 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23226 if (error_operand_p (tparm))
23227 return unify_invalid (explain_p);
23228
23229 /* Check for mixed types and values. */
23230 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23231 && TREE_CODE (tparm) != TYPE_DECL)
23232 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23233 && TREE_CODE (tparm) != TEMPLATE_DECL))
23234 gcc_unreachable ();
23235
23236 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23237 {
23238 if ((strict_in & UNIFY_ALLOW_DERIVED)
23239 && CLASS_TYPE_P (arg))
23240 {
23241 /* First try to match ARG directly. */
23242 tree t = try_class_unification (tparms, targs, parm, arg,
23243 explain_p);
23244 if (!t)
23245 {
23246 /* Otherwise, look for a suitable base of ARG, as below. */
23247 enum template_base_result r;
23248 r = get_template_base (tparms, targs, parm, arg,
23249 explain_p, &t);
23250 if (!t)
23251 return unify_no_common_base (explain_p, r, parm, arg);
23252 arg = t;
23253 }
23254 }
23255 /* ARG must be constructed from a template class or a template
23256 template parameter. */
23257 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23258 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23259 return unify_template_deduction_failure (explain_p, parm, arg);
23260
23261 /* Deduce arguments T, i from TT<T> or TT<i>. */
23262 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23263 return 1;
23264
23265 arg = TYPE_TI_TEMPLATE (arg);
23266
23267 /* Fall through to deduce template name. */
23268 }
23269
23270 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23271 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23272 {
23273 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23274
23275 /* Simple cases: Value already set, does match or doesn't. */
23276 if (targ != NULL_TREE && template_args_equal (targ, arg))
23277 return unify_success (explain_p);
23278 else if (targ)
23279 return unify_inconsistency (explain_p, parm, targ, arg);
23280 }
23281 else
23282 {
23283 /* If PARM is `const T' and ARG is only `int', we don't have
23284 a match unless we are allowing additional qualification.
23285 If ARG is `const int' and PARM is just `T' that's OK;
23286 that binds `const int' to `T'. */
23287 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23288 arg, parm))
23289 return unify_cv_qual_mismatch (explain_p, parm, arg);
23290
23291 /* Consider the case where ARG is `const volatile int' and
23292 PARM is `const T'. Then, T should be `volatile int'. */
23293 arg = cp_build_qualified_type_real
23294 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23295 if (arg == error_mark_node)
23296 return unify_invalid (explain_p);
23297
23298 /* Simple cases: Value already set, does match or doesn't. */
23299 if (targ != NULL_TREE && same_type_p (targ, arg))
23300 return unify_success (explain_p);
23301 else if (targ)
23302 return unify_inconsistency (explain_p, parm, targ, arg);
23303
23304 /* Make sure that ARG is not a variable-sized array. (Note
23305 that were talking about variable-sized arrays (like
23306 `int[n]'), rather than arrays of unknown size (like
23307 `int[]').) We'll get very confused by such a type since
23308 the bound of the array is not constant, and therefore
23309 not mangleable. Besides, such types are not allowed in
23310 ISO C++, so we can do as we please here. We do allow
23311 them for 'auto' deduction, since that isn't ABI-exposed. */
23312 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23313 return unify_vla_arg (explain_p, arg);
23314
23315 /* Strip typedefs as in convert_template_argument. */
23316 arg = canonicalize_type_argument (arg, tf_none);
23317 }
23318
23319 /* If ARG is a parameter pack or an expansion, we cannot unify
23320 against it unless PARM is also a parameter pack. */
23321 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23322 && !template_parameter_pack_p (parm))
23323 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23324
23325 /* If the argument deduction results is a METHOD_TYPE,
23326 then there is a problem.
23327 METHOD_TYPE doesn't map to any real C++ type the result of
23328 the deduction cannot be of that type. */
23329 if (TREE_CODE (arg) == METHOD_TYPE)
23330 return unify_method_type_error (explain_p, arg);
23331
23332 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23333 return unify_success (explain_p);
23334
23335 case TEMPLATE_PARM_INDEX:
23336 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23337 if (error_operand_p (tparm))
23338 return unify_invalid (explain_p);
23339
23340 if (TEMPLATE_PARM_LEVEL (parm)
23341 != template_decl_level (tparm))
23342 {
23343 /* The PARM is not one we're trying to unify. Just check
23344 to see if it matches ARG. */
23345 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23346 && cp_tree_equal (parm, arg));
23347 if (result)
23348 unify_expression_unequal (explain_p, parm, arg);
23349 return result;
23350 }
23351
23352 idx = TEMPLATE_PARM_IDX (parm);
23353 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23354
23355 if (targ)
23356 {
23357 if ((strict & UNIFY_ALLOW_INTEGER)
23358 && TREE_TYPE (targ) && TREE_TYPE (arg)
23359 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23360 /* We're deducing from an array bound, the type doesn't matter. */
23361 arg = fold_convert (TREE_TYPE (targ), arg);
23362 int x = !cp_tree_equal (targ, arg);
23363 if (x)
23364 unify_inconsistency (explain_p, parm, targ, arg);
23365 return x;
23366 }
23367
23368 /* [temp.deduct.type] If, in the declaration of a function template
23369 with a non-type template-parameter, the non-type
23370 template-parameter is used in an expression in the function
23371 parameter-list and, if the corresponding template-argument is
23372 deduced, the template-argument type shall match the type of the
23373 template-parameter exactly, except that a template-argument
23374 deduced from an array bound may be of any integral type.
23375 The non-type parameter might use already deduced type parameters. */
23376 tparm = TREE_TYPE (parm);
23377 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23378 /* We don't have enough levels of args to do any substitution. This
23379 can happen in the context of -fnew-ttp-matching. */;
23380 else
23381 {
23382 ++processing_template_decl;
23383 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23384 --processing_template_decl;
23385
23386 if (tree a = type_uses_auto (tparm))
23387 {
23388 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23389 if (tparm == error_mark_node)
23390 return 1;
23391 }
23392 }
23393
23394 if (!TREE_TYPE (arg))
23395 /* Template-parameter dependent expression. Just accept it for now.
23396 It will later be processed in convert_template_argument. */
23397 ;
23398 else if (same_type_ignoring_top_level_qualifiers_p
23399 (non_reference (TREE_TYPE (arg)),
23400 non_reference (tparm)))
23401 /* OK. Ignore top-level quals here because a class-type template
23402 parameter object is const. */;
23403 else if ((strict & UNIFY_ALLOW_INTEGER)
23404 && CP_INTEGRAL_TYPE_P (tparm))
23405 /* Convert the ARG to the type of PARM; the deduced non-type
23406 template argument must exactly match the types of the
23407 corresponding parameter. */
23408 arg = fold (build_nop (tparm, arg));
23409 else if (uses_template_parms (tparm))
23410 {
23411 /* We haven't deduced the type of this parameter yet. */
23412 if (cxx_dialect >= cxx17
23413 /* We deduce from array bounds in try_array_deduction. */
23414 && !(strict & UNIFY_ALLOW_INTEGER))
23415 {
23416 /* Deduce it from the non-type argument. */
23417 tree atype = TREE_TYPE (arg);
23418 RECUR_AND_CHECK_FAILURE (tparms, targs,
23419 tparm, atype,
23420 UNIFY_ALLOW_NONE, explain_p);
23421 }
23422 else
23423 /* Try again later. */
23424 return unify_success (explain_p);
23425 }
23426 else
23427 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23428
23429 /* If ARG is a parameter pack or an expansion, we cannot unify
23430 against it unless PARM is also a parameter pack. */
23431 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23432 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23433 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23434
23435 {
23436 bool removed_attr = false;
23437 arg = strip_typedefs_expr (arg, &removed_attr);
23438 }
23439 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23440 return unify_success (explain_p);
23441
23442 case PTRMEM_CST:
23443 {
23444 /* A pointer-to-member constant can be unified only with
23445 another constant. */
23446 if (TREE_CODE (arg) != PTRMEM_CST)
23447 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23448
23449 /* Just unify the class member. It would be useless (and possibly
23450 wrong, depending on the strict flags) to unify also
23451 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23452 arg refer to the same variable, even if through different
23453 classes. For instance:
23454
23455 struct A { int x; };
23456 struct B : A { };
23457
23458 Unification of &A::x and &B::x must succeed. */
23459 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23460 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23461 }
23462
23463 case POINTER_TYPE:
23464 {
23465 if (!TYPE_PTR_P (arg))
23466 return unify_type_mismatch (explain_p, parm, arg);
23467
23468 /* [temp.deduct.call]
23469
23470 A can be another pointer or pointer to member type that can
23471 be converted to the deduced A via a qualification
23472 conversion (_conv.qual_).
23473
23474 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23475 This will allow for additional cv-qualification of the
23476 pointed-to types if appropriate. */
23477
23478 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23479 /* The derived-to-base conversion only persists through one
23480 level of pointers. */
23481 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23482
23483 return unify (tparms, targs, TREE_TYPE (parm),
23484 TREE_TYPE (arg), strict, explain_p);
23485 }
23486
23487 case REFERENCE_TYPE:
23488 if (!TYPE_REF_P (arg))
23489 return unify_type_mismatch (explain_p, parm, arg);
23490 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23491 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23492
23493 case ARRAY_TYPE:
23494 if (TREE_CODE (arg) != ARRAY_TYPE)
23495 return unify_type_mismatch (explain_p, parm, arg);
23496 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23497 != (TYPE_DOMAIN (arg) == NULL_TREE))
23498 return unify_type_mismatch (explain_p, parm, arg);
23499 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23500 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23501 if (TYPE_DOMAIN (parm) != NULL_TREE)
23502 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23503 TYPE_DOMAIN (arg), explain_p);
23504 return unify_success (explain_p);
23505
23506 case REAL_TYPE:
23507 case COMPLEX_TYPE:
23508 case VECTOR_TYPE:
23509 case INTEGER_TYPE:
23510 case BOOLEAN_TYPE:
23511 case ENUMERAL_TYPE:
23512 case VOID_TYPE:
23513 case NULLPTR_TYPE:
23514 if (TREE_CODE (arg) != TREE_CODE (parm))
23515 return unify_type_mismatch (explain_p, parm, arg);
23516
23517 /* We have already checked cv-qualification at the top of the
23518 function. */
23519 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23520 return unify_type_mismatch (explain_p, parm, arg);
23521
23522 /* As far as unification is concerned, this wins. Later checks
23523 will invalidate it if necessary. */
23524 return unify_success (explain_p);
23525
23526 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23527 /* Type INTEGER_CST can come from ordinary constant template args. */
23528 case INTEGER_CST:
23529 while (CONVERT_EXPR_P (arg))
23530 arg = TREE_OPERAND (arg, 0);
23531
23532 if (TREE_CODE (arg) != INTEGER_CST)
23533 return unify_template_argument_mismatch (explain_p, parm, arg);
23534 return (tree_int_cst_equal (parm, arg)
23535 ? unify_success (explain_p)
23536 : unify_template_argument_mismatch (explain_p, parm, arg));
23537
23538 case TREE_VEC:
23539 {
23540 int i, len, argslen;
23541 int parm_variadic_p = 0;
23542
23543 if (TREE_CODE (arg) != TREE_VEC)
23544 return unify_template_argument_mismatch (explain_p, parm, arg);
23545
23546 len = TREE_VEC_LENGTH (parm);
23547 argslen = TREE_VEC_LENGTH (arg);
23548
23549 /* Check for pack expansions in the parameters. */
23550 for (i = 0; i < len; ++i)
23551 {
23552 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23553 {
23554 if (i == len - 1)
23555 /* We can unify against something with a trailing
23556 parameter pack. */
23557 parm_variadic_p = 1;
23558 else
23559 /* [temp.deduct.type]/9: If the template argument list of
23560 P contains a pack expansion that is not the last
23561 template argument, the entire template argument list
23562 is a non-deduced context. */
23563 return unify_success (explain_p);
23564 }
23565 }
23566
23567 /* If we don't have enough arguments to satisfy the parameters
23568 (not counting the pack expression at the end), or we have
23569 too many arguments for a parameter list that doesn't end in
23570 a pack expression, we can't unify. */
23571 if (parm_variadic_p
23572 ? argslen < len - parm_variadic_p
23573 : argslen != len)
23574 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23575
23576 /* Unify all of the parameters that precede the (optional)
23577 pack expression. */
23578 for (i = 0; i < len - parm_variadic_p; ++i)
23579 {
23580 RECUR_AND_CHECK_FAILURE (tparms, targs,
23581 TREE_VEC_ELT (parm, i),
23582 TREE_VEC_ELT (arg, i),
23583 UNIFY_ALLOW_NONE, explain_p);
23584 }
23585 if (parm_variadic_p)
23586 return unify_pack_expansion (tparms, targs, parm, arg,
23587 DEDUCE_EXACT,
23588 /*subr=*/true, explain_p);
23589 return unify_success (explain_p);
23590 }
23591
23592 case RECORD_TYPE:
23593 case UNION_TYPE:
23594 if (TREE_CODE (arg) != TREE_CODE (parm))
23595 return unify_type_mismatch (explain_p, parm, arg);
23596
23597 if (TYPE_PTRMEMFUNC_P (parm))
23598 {
23599 if (!TYPE_PTRMEMFUNC_P (arg))
23600 return unify_type_mismatch (explain_p, parm, arg);
23601
23602 return unify (tparms, targs,
23603 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23604 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23605 strict, explain_p);
23606 }
23607 else if (TYPE_PTRMEMFUNC_P (arg))
23608 return unify_type_mismatch (explain_p, parm, arg);
23609
23610 if (CLASSTYPE_TEMPLATE_INFO (parm))
23611 {
23612 tree t = NULL_TREE;
23613
23614 if (strict_in & UNIFY_ALLOW_DERIVED)
23615 {
23616 /* First, we try to unify the PARM and ARG directly. */
23617 t = try_class_unification (tparms, targs,
23618 parm, arg, explain_p);
23619
23620 if (!t)
23621 {
23622 /* Fallback to the special case allowed in
23623 [temp.deduct.call]:
23624
23625 If P is a class, and P has the form
23626 template-id, then A can be a derived class of
23627 the deduced A. Likewise, if P is a pointer to
23628 a class of the form template-id, A can be a
23629 pointer to a derived class pointed to by the
23630 deduced A. */
23631 enum template_base_result r;
23632 r = get_template_base (tparms, targs, parm, arg,
23633 explain_p, &t);
23634
23635 if (!t)
23636 {
23637 /* Don't give the derived diagnostic if we're
23638 already dealing with the same template. */
23639 bool same_template
23640 = (CLASSTYPE_TEMPLATE_INFO (arg)
23641 && (CLASSTYPE_TI_TEMPLATE (parm)
23642 == CLASSTYPE_TI_TEMPLATE (arg)));
23643 return unify_no_common_base (explain_p && !same_template,
23644 r, parm, arg);
23645 }
23646 }
23647 }
23648 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23649 && (CLASSTYPE_TI_TEMPLATE (parm)
23650 == CLASSTYPE_TI_TEMPLATE (arg)))
23651 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23652 Then, we should unify `int' and `U'. */
23653 t = arg;
23654 else
23655 /* There's no chance of unification succeeding. */
23656 return unify_type_mismatch (explain_p, parm, arg);
23657
23658 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23659 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23660 }
23661 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23662 return unify_type_mismatch (explain_p, parm, arg);
23663 return unify_success (explain_p);
23664
23665 case METHOD_TYPE:
23666 case FUNCTION_TYPE:
23667 {
23668 unsigned int nargs;
23669 tree *args;
23670 tree a;
23671 unsigned int i;
23672
23673 if (TREE_CODE (arg) != TREE_CODE (parm))
23674 return unify_type_mismatch (explain_p, parm, arg);
23675
23676 /* CV qualifications for methods can never be deduced, they must
23677 match exactly. We need to check them explicitly here,
23678 because type_unification_real treats them as any other
23679 cv-qualified parameter. */
23680 if (TREE_CODE (parm) == METHOD_TYPE
23681 && (!check_cv_quals_for_unify
23682 (UNIFY_ALLOW_NONE,
23683 class_of_this_parm (arg),
23684 class_of_this_parm (parm))))
23685 return unify_cv_qual_mismatch (explain_p, parm, arg);
23686 if (TREE_CODE (arg) == FUNCTION_TYPE
23687 && type_memfn_quals (parm) != type_memfn_quals (arg))
23688 return unify_cv_qual_mismatch (explain_p, parm, arg);
23689 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23690 return unify_type_mismatch (explain_p, parm, arg);
23691
23692 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23693 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23694
23695 nargs = list_length (TYPE_ARG_TYPES (arg));
23696 args = XALLOCAVEC (tree, nargs);
23697 for (a = TYPE_ARG_TYPES (arg), i = 0;
23698 a != NULL_TREE && a != void_list_node;
23699 a = TREE_CHAIN (a), ++i)
23700 args[i] = TREE_VALUE (a);
23701 nargs = i;
23702
23703 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23704 args, nargs, 1, DEDUCE_EXACT,
23705 NULL, explain_p))
23706 return 1;
23707
23708 if (flag_noexcept_type)
23709 {
23710 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23711 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23712 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23713 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23714 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23715 && uses_template_parms (TREE_PURPOSE (pspec)))
23716 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23717 TREE_PURPOSE (aspec),
23718 UNIFY_ALLOW_NONE, explain_p);
23719 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23720 return unify_type_mismatch (explain_p, parm, arg);
23721 }
23722
23723 return 0;
23724 }
23725
23726 case OFFSET_TYPE:
23727 /* Unify a pointer to member with a pointer to member function, which
23728 deduces the type of the member as a function type. */
23729 if (TYPE_PTRMEMFUNC_P (arg))
23730 {
23731 /* Check top-level cv qualifiers */
23732 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23733 return unify_cv_qual_mismatch (explain_p, parm, arg);
23734
23735 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23736 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23737 UNIFY_ALLOW_NONE, explain_p);
23738
23739 /* Determine the type of the function we are unifying against. */
23740 tree fntype = static_fn_type (arg);
23741
23742 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23743 }
23744
23745 if (TREE_CODE (arg) != OFFSET_TYPE)
23746 return unify_type_mismatch (explain_p, parm, arg);
23747 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23748 TYPE_OFFSET_BASETYPE (arg),
23749 UNIFY_ALLOW_NONE, explain_p);
23750 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23751 strict, explain_p);
23752
23753 case CONST_DECL:
23754 if (DECL_TEMPLATE_PARM_P (parm))
23755 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23756 if (arg != scalar_constant_value (parm))
23757 return unify_template_argument_mismatch (explain_p, parm, arg);
23758 return unify_success (explain_p);
23759
23760 case FIELD_DECL:
23761 case TEMPLATE_DECL:
23762 /* Matched cases are handled by the ARG == PARM test above. */
23763 return unify_template_argument_mismatch (explain_p, parm, arg);
23764
23765 case VAR_DECL:
23766 /* We might get a variable as a non-type template argument in parm if the
23767 corresponding parameter is type-dependent. Make any necessary
23768 adjustments based on whether arg is a reference. */
23769 if (CONSTANT_CLASS_P (arg))
23770 parm = fold_non_dependent_expr (parm, complain);
23771 else if (REFERENCE_REF_P (arg))
23772 {
23773 tree sub = TREE_OPERAND (arg, 0);
23774 STRIP_NOPS (sub);
23775 if (TREE_CODE (sub) == ADDR_EXPR)
23776 arg = TREE_OPERAND (sub, 0);
23777 }
23778 /* Now use the normal expression code to check whether they match. */
23779 goto expr;
23780
23781 case TYPE_ARGUMENT_PACK:
23782 case NONTYPE_ARGUMENT_PACK:
23783 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23784 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23785
23786 case TYPEOF_TYPE:
23787 case DECLTYPE_TYPE:
23788 case UNDERLYING_TYPE:
23789 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23790 or UNDERLYING_TYPE nodes. */
23791 return unify_success (explain_p);
23792
23793 case ERROR_MARK:
23794 /* Unification fails if we hit an error node. */
23795 return unify_invalid (explain_p);
23796
23797 case INDIRECT_REF:
23798 if (REFERENCE_REF_P (parm))
23799 {
23800 bool pexp = PACK_EXPANSION_P (arg);
23801 if (pexp)
23802 arg = PACK_EXPANSION_PATTERN (arg);
23803 if (REFERENCE_REF_P (arg))
23804 arg = TREE_OPERAND (arg, 0);
23805 if (pexp)
23806 arg = make_pack_expansion (arg, complain);
23807 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23808 strict, explain_p);
23809 }
23810 /* FALLTHRU */
23811
23812 default:
23813 /* An unresolved overload is a nondeduced context. */
23814 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23815 return unify_success (explain_p);
23816 gcc_assert (EXPR_P (parm)
23817 || COMPOUND_LITERAL_P (parm)
23818 || TREE_CODE (parm) == TRAIT_EXPR);
23819 expr:
23820 /* We must be looking at an expression. This can happen with
23821 something like:
23822
23823 template <int I>
23824 void foo(S<I>, S<I + 2>);
23825
23826 or
23827
23828 template<typename T>
23829 void foo(A<T, T{}>);
23830
23831 This is a "non-deduced context":
23832
23833 [deduct.type]
23834
23835 The non-deduced contexts are:
23836
23837 --A non-type template argument or an array bound in which
23838 a subexpression references a template parameter.
23839
23840 In these cases, we assume deduction succeeded, but don't
23841 actually infer any unifications. */
23842
23843 if (!uses_template_parms (parm)
23844 && !template_args_equal (parm, arg))
23845 return unify_expression_unequal (explain_p, parm, arg);
23846 else
23847 return unify_success (explain_p);
23848 }
23849 }
23850 #undef RECUR_AND_CHECK_FAILURE
23851 \f
23852 /* Note that DECL can be defined in this translation unit, if
23853 required. */
23854
23855 static void
23856 mark_definable (tree decl)
23857 {
23858 tree clone;
23859 DECL_NOT_REALLY_EXTERN (decl) = 1;
23860 FOR_EACH_CLONE (clone, decl)
23861 DECL_NOT_REALLY_EXTERN (clone) = 1;
23862 }
23863
23864 /* Called if RESULT is explicitly instantiated, or is a member of an
23865 explicitly instantiated class. */
23866
23867 void
23868 mark_decl_instantiated (tree result, int extern_p)
23869 {
23870 SET_DECL_EXPLICIT_INSTANTIATION (result);
23871
23872 /* If this entity has already been written out, it's too late to
23873 make any modifications. */
23874 if (TREE_ASM_WRITTEN (result))
23875 return;
23876
23877 /* For anonymous namespace we don't need to do anything. */
23878 if (decl_anon_ns_mem_p (result))
23879 {
23880 gcc_assert (!TREE_PUBLIC (result));
23881 return;
23882 }
23883
23884 if (TREE_CODE (result) != FUNCTION_DECL)
23885 /* The TREE_PUBLIC flag for function declarations will have been
23886 set correctly by tsubst. */
23887 TREE_PUBLIC (result) = 1;
23888
23889 /* This might have been set by an earlier implicit instantiation. */
23890 DECL_COMDAT (result) = 0;
23891
23892 if (extern_p)
23893 DECL_NOT_REALLY_EXTERN (result) = 0;
23894 else
23895 {
23896 mark_definable (result);
23897 mark_needed (result);
23898 /* Always make artificials weak. */
23899 if (DECL_ARTIFICIAL (result) && flag_weak)
23900 comdat_linkage (result);
23901 /* For WIN32 we also want to put explicit instantiations in
23902 linkonce sections. */
23903 else if (TREE_PUBLIC (result))
23904 maybe_make_one_only (result);
23905 if (TREE_CODE (result) == FUNCTION_DECL
23906 && DECL_TEMPLATE_INSTANTIATED (result))
23907 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23908 since start_preparsed_function wouldn't have if we had an earlier
23909 extern explicit instantiation. */
23910 DECL_EXTERNAL (result) = 0;
23911 }
23912
23913 /* If EXTERN_P, then this function will not be emitted -- unless
23914 followed by an explicit instantiation, at which point its linkage
23915 will be adjusted. If !EXTERN_P, then this function will be
23916 emitted here. In neither circumstance do we want
23917 import_export_decl to adjust the linkage. */
23918 DECL_INTERFACE_KNOWN (result) = 1;
23919 }
23920
23921 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23922 important template arguments. If any are missing, we check whether
23923 they're important by using error_mark_node for substituting into any
23924 args that were used for partial ordering (the ones between ARGS and END)
23925 and seeing if it bubbles up. */
23926
23927 static bool
23928 check_undeduced_parms (tree targs, tree args, tree end)
23929 {
23930 bool found = false;
23931 int i;
23932 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23933 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23934 {
23935 found = true;
23936 TREE_VEC_ELT (targs, i) = error_mark_node;
23937 }
23938 if (found)
23939 {
23940 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23941 if (substed == error_mark_node)
23942 return true;
23943 }
23944 return false;
23945 }
23946
23947 /* Given two function templates PAT1 and PAT2, return:
23948
23949 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23950 -1 if PAT2 is more specialized than PAT1.
23951 0 if neither is more specialized.
23952
23953 LEN indicates the number of parameters we should consider
23954 (defaulted parameters should not be considered).
23955
23956 The 1998 std underspecified function template partial ordering, and
23957 DR214 addresses the issue. We take pairs of arguments, one from
23958 each of the templates, and deduce them against each other. One of
23959 the templates will be more specialized if all the *other*
23960 template's arguments deduce against its arguments and at least one
23961 of its arguments *does* *not* deduce against the other template's
23962 corresponding argument. Deduction is done as for class templates.
23963 The arguments used in deduction have reference and top level cv
23964 qualifiers removed. Iff both arguments were originally reference
23965 types *and* deduction succeeds in both directions, an lvalue reference
23966 wins against an rvalue reference and otherwise the template
23967 with the more cv-qualified argument wins for that pairing (if
23968 neither is more cv-qualified, they both are equal). Unlike regular
23969 deduction, after all the arguments have been deduced in this way,
23970 we do *not* verify the deduced template argument values can be
23971 substituted into non-deduced contexts.
23972
23973 The logic can be a bit confusing here, because we look at deduce1 and
23974 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23975 can find template arguments for pat1 to make arg1 look like arg2, that
23976 means that arg2 is at least as specialized as arg1. */
23977
23978 int
23979 more_specialized_fn (tree pat1, tree pat2, int len)
23980 {
23981 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23982 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23983 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23984 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23985 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23986 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23987 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23988 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23989 tree origs1, origs2;
23990 bool lose1 = false;
23991 bool lose2 = false;
23992
23993 /* Remove the this parameter from non-static member functions. If
23994 one is a non-static member function and the other is not a static
23995 member function, remove the first parameter from that function
23996 also. This situation occurs for operator functions where we
23997 locate both a member function (with this pointer) and non-member
23998 operator (with explicit first operand). */
23999 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24000 {
24001 len--; /* LEN is the number of significant arguments for DECL1 */
24002 args1 = TREE_CHAIN (args1);
24003 if (!DECL_STATIC_FUNCTION_P (decl2))
24004 args2 = TREE_CHAIN (args2);
24005 }
24006 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24007 {
24008 args2 = TREE_CHAIN (args2);
24009 if (!DECL_STATIC_FUNCTION_P (decl1))
24010 {
24011 len--;
24012 args1 = TREE_CHAIN (args1);
24013 }
24014 }
24015
24016 /* If only one is a conversion operator, they are unordered. */
24017 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24018 return 0;
24019
24020 /* Consider the return type for a conversion function */
24021 if (DECL_CONV_FN_P (decl1))
24022 {
24023 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24024 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24025 len++;
24026 }
24027
24028 processing_template_decl++;
24029
24030 origs1 = args1;
24031 origs2 = args2;
24032
24033 while (len--
24034 /* Stop when an ellipsis is seen. */
24035 && args1 != NULL_TREE && args2 != NULL_TREE)
24036 {
24037 tree arg1 = TREE_VALUE (args1);
24038 tree arg2 = TREE_VALUE (args2);
24039 int deduce1, deduce2;
24040 int quals1 = -1;
24041 int quals2 = -1;
24042 int ref1 = 0;
24043 int ref2 = 0;
24044
24045 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24046 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24047 {
24048 /* When both arguments are pack expansions, we need only
24049 unify the patterns themselves. */
24050 arg1 = PACK_EXPANSION_PATTERN (arg1);
24051 arg2 = PACK_EXPANSION_PATTERN (arg2);
24052
24053 /* This is the last comparison we need to do. */
24054 len = 0;
24055 }
24056
24057 if (TYPE_REF_P (arg1))
24058 {
24059 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24060 arg1 = TREE_TYPE (arg1);
24061 quals1 = cp_type_quals (arg1);
24062 }
24063
24064 if (TYPE_REF_P (arg2))
24065 {
24066 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24067 arg2 = TREE_TYPE (arg2);
24068 quals2 = cp_type_quals (arg2);
24069 }
24070
24071 arg1 = TYPE_MAIN_VARIANT (arg1);
24072 arg2 = TYPE_MAIN_VARIANT (arg2);
24073
24074 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24075 {
24076 int i, len2 = remaining_arguments (args2);
24077 tree parmvec = make_tree_vec (1);
24078 tree argvec = make_tree_vec (len2);
24079 tree ta = args2;
24080
24081 /* Setup the parameter vector, which contains only ARG1. */
24082 TREE_VEC_ELT (parmvec, 0) = arg1;
24083
24084 /* Setup the argument vector, which contains the remaining
24085 arguments. */
24086 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24087 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24088
24089 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24090 argvec, DEDUCE_EXACT,
24091 /*subr=*/true, /*explain_p=*/false)
24092 == 0);
24093
24094 /* We cannot deduce in the other direction, because ARG1 is
24095 a pack expansion but ARG2 is not. */
24096 deduce2 = 0;
24097 }
24098 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24099 {
24100 int i, len1 = remaining_arguments (args1);
24101 tree parmvec = make_tree_vec (1);
24102 tree argvec = make_tree_vec (len1);
24103 tree ta = args1;
24104
24105 /* Setup the parameter vector, which contains only ARG1. */
24106 TREE_VEC_ELT (parmvec, 0) = arg2;
24107
24108 /* Setup the argument vector, which contains the remaining
24109 arguments. */
24110 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24111 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24112
24113 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24114 argvec, DEDUCE_EXACT,
24115 /*subr=*/true, /*explain_p=*/false)
24116 == 0);
24117
24118 /* We cannot deduce in the other direction, because ARG2 is
24119 a pack expansion but ARG1 is not.*/
24120 deduce1 = 0;
24121 }
24122
24123 else
24124 {
24125 /* The normal case, where neither argument is a pack
24126 expansion. */
24127 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24128 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24129 == 0);
24130 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24131 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24132 == 0);
24133 }
24134
24135 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24136 arg2, then arg2 is not as specialized as arg1. */
24137 if (!deduce1)
24138 lose2 = true;
24139 if (!deduce2)
24140 lose1 = true;
24141
24142 /* "If, for a given type, deduction succeeds in both directions
24143 (i.e., the types are identical after the transformations above)
24144 and both P and A were reference types (before being replaced with
24145 the type referred to above):
24146 - if the type from the argument template was an lvalue reference and
24147 the type from the parameter template was not, the argument type is
24148 considered to be more specialized than the other; otherwise,
24149 - if the type from the argument template is more cv-qualified
24150 than the type from the parameter template (as described above),
24151 the argument type is considered to be more specialized than the other;
24152 otherwise,
24153 - neither type is more specialized than the other." */
24154
24155 if (deduce1 && deduce2)
24156 {
24157 if (ref1 && ref2 && ref1 != ref2)
24158 {
24159 if (ref1 > ref2)
24160 lose1 = true;
24161 else
24162 lose2 = true;
24163 }
24164 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24165 {
24166 if ((quals1 & quals2) == quals2)
24167 lose2 = true;
24168 if ((quals1 & quals2) == quals1)
24169 lose1 = true;
24170 }
24171 }
24172
24173 if (lose1 && lose2)
24174 /* We've failed to deduce something in either direction.
24175 These must be unordered. */
24176 break;
24177
24178 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24179 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24180 /* We have already processed all of the arguments in our
24181 handing of the pack expansion type. */
24182 len = 0;
24183
24184 args1 = TREE_CHAIN (args1);
24185 args2 = TREE_CHAIN (args2);
24186 }
24187
24188 /* "In most cases, all template parameters must have values in order for
24189 deduction to succeed, but for partial ordering purposes a template
24190 parameter may remain without a value provided it is not used in the
24191 types being used for partial ordering."
24192
24193 Thus, if we are missing any of the targs1 we need to substitute into
24194 origs1, then pat2 is not as specialized as pat1. This can happen when
24195 there is a nondeduced context. */
24196 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24197 lose2 = true;
24198 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24199 lose1 = true;
24200
24201 processing_template_decl--;
24202
24203 /* If both deductions succeed, the partial ordering selects the more
24204 constrained template. */
24205 /* P2113: If the corresponding template-parameters of the
24206 template-parameter-lists are not equivalent ([temp.over.link]) or if
24207 the function parameters that positionally correspond between the two
24208 templates are not of the same type, neither template is more
24209 specialized than the other. */
24210 if (!lose1 && !lose2
24211 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24212 DECL_TEMPLATE_PARMS (pat2))
24213 && compparms (origs1, origs2))
24214 {
24215 int winner = more_constrained (decl1, decl2);
24216 if (winner > 0)
24217 lose2 = true;
24218 else if (winner < 0)
24219 lose1 = true;
24220 }
24221
24222 /* All things being equal, if the next argument is a pack expansion
24223 for one function but not for the other, prefer the
24224 non-variadic function. FIXME this is bogus; see c++/41958. */
24225 if (lose1 == lose2
24226 && args1 && TREE_VALUE (args1)
24227 && args2 && TREE_VALUE (args2))
24228 {
24229 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24230 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24231 }
24232
24233 if (lose1 == lose2)
24234 return 0;
24235 else if (!lose1)
24236 return 1;
24237 else
24238 return -1;
24239 }
24240
24241 /* Determine which of two partial specializations of TMPL is more
24242 specialized.
24243
24244 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24245 to the first partial specialization. The TREE_PURPOSE is the
24246 innermost set of template parameters for the partial
24247 specialization. PAT2 is similar, but for the second template.
24248
24249 Return 1 if the first partial specialization is more specialized;
24250 -1 if the second is more specialized; 0 if neither is more
24251 specialized.
24252
24253 See [temp.class.order] for information about determining which of
24254 two templates is more specialized. */
24255
24256 static int
24257 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24258 {
24259 tree targs;
24260 int winner = 0;
24261 bool any_deductions = false;
24262
24263 tree tmpl1 = TREE_VALUE (pat1);
24264 tree tmpl2 = TREE_VALUE (pat2);
24265 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24266 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24267
24268 /* Just like what happens for functions, if we are ordering between
24269 different template specializations, we may encounter dependent
24270 types in the arguments, and we need our dependency check functions
24271 to behave correctly. */
24272 ++processing_template_decl;
24273 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24274 if (targs)
24275 {
24276 --winner;
24277 any_deductions = true;
24278 }
24279
24280 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24281 if (targs)
24282 {
24283 ++winner;
24284 any_deductions = true;
24285 }
24286 --processing_template_decl;
24287
24288 /* If both deductions succeed, the partial ordering selects the more
24289 constrained template. */
24290 if (!winner && any_deductions)
24291 winner = more_constrained (tmpl1, tmpl2);
24292
24293 /* In the case of a tie where at least one of the templates
24294 has a parameter pack at the end, the template with the most
24295 non-packed parameters wins. */
24296 if (winner == 0
24297 && any_deductions
24298 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24299 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24300 {
24301 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24302 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24303 int len1 = TREE_VEC_LENGTH (args1);
24304 int len2 = TREE_VEC_LENGTH (args2);
24305
24306 /* We don't count the pack expansion at the end. */
24307 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24308 --len1;
24309 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24310 --len2;
24311
24312 if (len1 > len2)
24313 return 1;
24314 else if (len1 < len2)
24315 return -1;
24316 }
24317
24318 return winner;
24319 }
24320
24321 /* Return the template arguments that will produce the function signature
24322 DECL from the function template FN, with the explicit template
24323 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24324 also match. Return NULL_TREE if no satisfactory arguments could be
24325 found. */
24326
24327 static tree
24328 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24329 {
24330 int ntparms = DECL_NTPARMS (fn);
24331 tree targs = make_tree_vec (ntparms);
24332 tree decl_type = TREE_TYPE (decl);
24333 tree decl_arg_types;
24334 tree *args;
24335 unsigned int nargs, ix;
24336 tree arg;
24337
24338 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24339
24340 /* Never do unification on the 'this' parameter. */
24341 decl_arg_types = skip_artificial_parms_for (decl,
24342 TYPE_ARG_TYPES (decl_type));
24343
24344 nargs = list_length (decl_arg_types);
24345 args = XALLOCAVEC (tree, nargs);
24346 for (arg = decl_arg_types, ix = 0;
24347 arg != NULL_TREE && arg != void_list_node;
24348 arg = TREE_CHAIN (arg), ++ix)
24349 args[ix] = TREE_VALUE (arg);
24350
24351 if (fn_type_unification (fn, explicit_args, targs,
24352 args, ix,
24353 (check_rettype || DECL_CONV_FN_P (fn)
24354 ? TREE_TYPE (decl_type) : NULL_TREE),
24355 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24356 /*explain_p=*/false,
24357 /*decltype*/false)
24358 == error_mark_node)
24359 return NULL_TREE;
24360
24361 return targs;
24362 }
24363
24364 /* Return the innermost template arguments that, when applied to a partial
24365 specialization SPEC_TMPL of TMPL, yield the ARGS.
24366
24367 For example, suppose we have:
24368
24369 template <class T, class U> struct S {};
24370 template <class T> struct S<T*, int> {};
24371
24372 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24373 partial specialization and the ARGS will be {double*, int}. The resulting
24374 vector will be {double}, indicating that `T' is bound to `double'. */
24375
24376 static tree
24377 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24378 {
24379 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24380 tree spec_args
24381 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24382 int i, ntparms = TREE_VEC_LENGTH (tparms);
24383 tree deduced_args;
24384 tree innermost_deduced_args;
24385
24386 innermost_deduced_args = make_tree_vec (ntparms);
24387 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24388 {
24389 deduced_args = copy_node (args);
24390 SET_TMPL_ARGS_LEVEL (deduced_args,
24391 TMPL_ARGS_DEPTH (deduced_args),
24392 innermost_deduced_args);
24393 }
24394 else
24395 deduced_args = innermost_deduced_args;
24396
24397 bool tried_array_deduction = (cxx_dialect < cxx17);
24398 again:
24399 if (unify (tparms, deduced_args,
24400 INNERMOST_TEMPLATE_ARGS (spec_args),
24401 INNERMOST_TEMPLATE_ARGS (args),
24402 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24403 return NULL_TREE;
24404
24405 for (i = 0; i < ntparms; ++i)
24406 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24407 {
24408 if (!tried_array_deduction)
24409 {
24410 try_array_deduction (tparms, innermost_deduced_args,
24411 INNERMOST_TEMPLATE_ARGS (spec_args));
24412 tried_array_deduction = true;
24413 if (TREE_VEC_ELT (innermost_deduced_args, i))
24414 goto again;
24415 }
24416 return NULL_TREE;
24417 }
24418
24419 if (!push_tinst_level (spec_tmpl, deduced_args))
24420 {
24421 excessive_deduction_depth = true;
24422 return NULL_TREE;
24423 }
24424
24425 /* Verify that nondeduced template arguments agree with the type
24426 obtained from argument deduction.
24427
24428 For example:
24429
24430 struct A { typedef int X; };
24431 template <class T, class U> struct C {};
24432 template <class T> struct C<T, typename T::X> {};
24433
24434 Then with the instantiation `C<A, int>', we can deduce that
24435 `T' is `A' but unify () does not check whether `typename T::X'
24436 is `int'. */
24437 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24438
24439 if (spec_args != error_mark_node)
24440 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24441 INNERMOST_TEMPLATE_ARGS (spec_args),
24442 tmpl, tf_none, false, false);
24443
24444 pop_tinst_level ();
24445
24446 if (spec_args == error_mark_node
24447 /* We only need to check the innermost arguments; the other
24448 arguments will always agree. */
24449 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24450 INNERMOST_TEMPLATE_ARGS (args)))
24451 return NULL_TREE;
24452
24453 /* Now that we have bindings for all of the template arguments,
24454 ensure that the arguments deduced for the template template
24455 parameters have compatible template parameter lists. See the use
24456 of template_template_parm_bindings_ok_p in fn_type_unification
24457 for more information. */
24458 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24459 return NULL_TREE;
24460
24461 return deduced_args;
24462 }
24463
24464 // Compare two function templates T1 and T2 by deducing bindings
24465 // from one against the other. If both deductions succeed, compare
24466 // constraints to see which is more constrained.
24467 static int
24468 more_specialized_inst (tree t1, tree t2)
24469 {
24470 int fate = 0;
24471 int count = 0;
24472
24473 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24474 {
24475 --fate;
24476 ++count;
24477 }
24478
24479 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24480 {
24481 ++fate;
24482 ++count;
24483 }
24484
24485 // If both deductions succeed, then one may be more constrained.
24486 if (count == 2 && fate == 0)
24487 fate = more_constrained (t1, t2);
24488
24489 return fate;
24490 }
24491
24492 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24493 Return the TREE_LIST node with the most specialized template, if
24494 any. If there is no most specialized template, the error_mark_node
24495 is returned.
24496
24497 Note that this function does not look at, or modify, the
24498 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24499 returned is one of the elements of INSTANTIATIONS, callers may
24500 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24501 and retrieve it from the value returned. */
24502
24503 tree
24504 most_specialized_instantiation (tree templates)
24505 {
24506 tree fn, champ;
24507
24508 ++processing_template_decl;
24509
24510 champ = templates;
24511 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24512 {
24513 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24514 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24515 if (fate == -1)
24516 champ = fn;
24517 else if (!fate)
24518 {
24519 /* Equally specialized, move to next function. If there
24520 is no next function, nothing's most specialized. */
24521 fn = TREE_CHAIN (fn);
24522 champ = fn;
24523 if (!fn)
24524 break;
24525 }
24526 }
24527
24528 if (champ)
24529 /* Now verify that champ is better than everything earlier in the
24530 instantiation list. */
24531 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24532 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24533 {
24534 champ = NULL_TREE;
24535 break;
24536 }
24537 }
24538
24539 processing_template_decl--;
24540
24541 if (!champ)
24542 return error_mark_node;
24543
24544 return champ;
24545 }
24546
24547 /* If DECL is a specialization of some template, return the most
24548 general such template. Otherwise, returns NULL_TREE.
24549
24550 For example, given:
24551
24552 template <class T> struct S { template <class U> void f(U); };
24553
24554 if TMPL is `template <class U> void S<int>::f(U)' this will return
24555 the full template. This function will not trace past partial
24556 specializations, however. For example, given in addition:
24557
24558 template <class T> struct S<T*> { template <class U> void f(U); };
24559
24560 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24561 `template <class T> template <class U> S<T*>::f(U)'. */
24562
24563 tree
24564 most_general_template (tree decl)
24565 {
24566 if (TREE_CODE (decl) != TEMPLATE_DECL)
24567 {
24568 if (tree tinfo = get_template_info (decl))
24569 decl = TI_TEMPLATE (tinfo);
24570 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24571 template friend, or a FIELD_DECL for a capture pack. */
24572 if (TREE_CODE (decl) != TEMPLATE_DECL)
24573 return NULL_TREE;
24574 }
24575
24576 /* Look for more and more general templates. */
24577 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24578 {
24579 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24580 (See cp-tree.h for details.) */
24581 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24582 break;
24583
24584 if (CLASS_TYPE_P (TREE_TYPE (decl))
24585 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24586 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24587 break;
24588
24589 /* Stop if we run into an explicitly specialized class template. */
24590 if (!DECL_NAMESPACE_SCOPE_P (decl)
24591 && DECL_CONTEXT (decl)
24592 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24593 break;
24594
24595 decl = DECL_TI_TEMPLATE (decl);
24596 }
24597
24598 return decl;
24599 }
24600
24601 /* Return the most specialized of the template partial specializations
24602 which can produce TARGET, a specialization of some class or variable
24603 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24604 a TEMPLATE_DECL node corresponding to the partial specialization, while
24605 the TREE_PURPOSE is the set of template arguments that must be
24606 substituted into the template pattern in order to generate TARGET.
24607
24608 If the choice of partial specialization is ambiguous, a diagnostic
24609 is issued, and the error_mark_node is returned. If there are no
24610 partial specializations matching TARGET, then NULL_TREE is
24611 returned, indicating that the primary template should be used. */
24612
24613 tree
24614 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24615 {
24616 tree list = NULL_TREE;
24617 tree t;
24618 tree champ;
24619 int fate;
24620 bool ambiguous_p;
24621 tree outer_args = NULL_TREE;
24622 tree tmpl, args;
24623
24624 if (TYPE_P (target))
24625 {
24626 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24627 tmpl = TI_TEMPLATE (tinfo);
24628 args = TI_ARGS (tinfo);
24629 }
24630 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24631 {
24632 tmpl = TREE_OPERAND (target, 0);
24633 args = TREE_OPERAND (target, 1);
24634 }
24635 else if (VAR_P (target))
24636 {
24637 tree tinfo = DECL_TEMPLATE_INFO (target);
24638 tmpl = TI_TEMPLATE (tinfo);
24639 args = TI_ARGS (tinfo);
24640 }
24641 else
24642 gcc_unreachable ();
24643
24644 tree main_tmpl = most_general_template (tmpl);
24645
24646 /* For determining which partial specialization to use, only the
24647 innermost args are interesting. */
24648 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24649 {
24650 outer_args = strip_innermost_template_args (args, 1);
24651 args = INNERMOST_TEMPLATE_ARGS (args);
24652 }
24653
24654 /* The caller hasn't called push_to_top_level yet, but we need
24655 get_partial_spec_bindings to be done in non-template context so that we'll
24656 fully resolve everything. */
24657 processing_template_decl_sentinel ptds;
24658
24659 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24660 {
24661 const tree ospec_tmpl = TREE_VALUE (t);
24662
24663 tree spec_tmpl;
24664 if (outer_args)
24665 {
24666 /* Substitute in the template args from the enclosing class. */
24667 ++processing_template_decl;
24668 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24669 --processing_template_decl;
24670 if (spec_tmpl == error_mark_node)
24671 return error_mark_node;
24672 }
24673 else
24674 spec_tmpl = ospec_tmpl;
24675
24676 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24677 if (spec_args)
24678 {
24679 if (outer_args)
24680 spec_args = add_to_template_args (outer_args, spec_args);
24681
24682 /* Keep the candidate only if the constraints are satisfied,
24683 or if we're not compiling with concepts. */
24684 if (!flag_concepts
24685 || constraints_satisfied_p (ospec_tmpl, spec_args))
24686 {
24687 list = tree_cons (spec_args, ospec_tmpl, list);
24688 TREE_TYPE (list) = TREE_TYPE (t);
24689 }
24690 }
24691 }
24692
24693 if (! list)
24694 return NULL_TREE;
24695
24696 ambiguous_p = false;
24697 t = list;
24698 champ = t;
24699 t = TREE_CHAIN (t);
24700 for (; t; t = TREE_CHAIN (t))
24701 {
24702 fate = more_specialized_partial_spec (tmpl, champ, t);
24703 if (fate == 1)
24704 ;
24705 else
24706 {
24707 if (fate == 0)
24708 {
24709 t = TREE_CHAIN (t);
24710 if (! t)
24711 {
24712 ambiguous_p = true;
24713 break;
24714 }
24715 }
24716 champ = t;
24717 }
24718 }
24719
24720 if (!ambiguous_p)
24721 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24722 {
24723 fate = more_specialized_partial_spec (tmpl, champ, t);
24724 if (fate != 1)
24725 {
24726 ambiguous_p = true;
24727 break;
24728 }
24729 }
24730
24731 if (ambiguous_p)
24732 {
24733 const char *str;
24734 char *spaces = NULL;
24735 if (!(complain & tf_error))
24736 return error_mark_node;
24737 if (TYPE_P (target))
24738 error ("ambiguous template instantiation for %q#T", target);
24739 else
24740 error ("ambiguous template instantiation for %q#D", target);
24741 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24742 for (t = list; t; t = TREE_CHAIN (t))
24743 {
24744 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24745 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24746 "%s %#qS", spaces ? spaces : str, subst);
24747 spaces = spaces ? spaces : get_spaces (str);
24748 }
24749 free (spaces);
24750 return error_mark_node;
24751 }
24752
24753 return champ;
24754 }
24755
24756 /* Explicitly instantiate DECL. */
24757
24758 void
24759 do_decl_instantiation (tree decl, tree storage)
24760 {
24761 tree result = NULL_TREE;
24762 int extern_p = 0;
24763
24764 if (!decl || decl == error_mark_node)
24765 /* An error occurred, for which grokdeclarator has already issued
24766 an appropriate message. */
24767 return;
24768 else if (! DECL_LANG_SPECIFIC (decl))
24769 {
24770 error ("explicit instantiation of non-template %q#D", decl);
24771 return;
24772 }
24773 else if (DECL_DECLARED_CONCEPT_P (decl))
24774 {
24775 if (VAR_P (decl))
24776 error ("explicit instantiation of variable concept %q#D", decl);
24777 else
24778 error ("explicit instantiation of function concept %q#D", decl);
24779 return;
24780 }
24781
24782 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24783 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24784
24785 if (VAR_P (decl) && !var_templ)
24786 {
24787 /* There is an asymmetry here in the way VAR_DECLs and
24788 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24789 the latter, the DECL we get back will be marked as a
24790 template instantiation, and the appropriate
24791 DECL_TEMPLATE_INFO will be set up. This does not happen for
24792 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24793 should handle VAR_DECLs as it currently handles
24794 FUNCTION_DECLs. */
24795 if (!DECL_CLASS_SCOPE_P (decl))
24796 {
24797 error ("%qD is not a static data member of a class template", decl);
24798 return;
24799 }
24800 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24801 if (!result || !VAR_P (result))
24802 {
24803 error ("no matching template for %qD found", decl);
24804 return;
24805 }
24806 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24807 {
24808 error ("type %qT for explicit instantiation %qD does not match "
24809 "declared type %qT", TREE_TYPE (result), decl,
24810 TREE_TYPE (decl));
24811 return;
24812 }
24813 }
24814 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24815 {
24816 error ("explicit instantiation of %q#D", decl);
24817 return;
24818 }
24819 else
24820 result = decl;
24821
24822 /* Check for various error cases. Note that if the explicit
24823 instantiation is valid the RESULT will currently be marked as an
24824 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24825 until we get here. */
24826
24827 if (DECL_TEMPLATE_SPECIALIZATION (result))
24828 {
24829 /* DR 259 [temp.spec].
24830
24831 Both an explicit instantiation and a declaration of an explicit
24832 specialization shall not appear in a program unless the explicit
24833 instantiation follows a declaration of the explicit specialization.
24834
24835 For a given set of template parameters, if an explicit
24836 instantiation of a template appears after a declaration of an
24837 explicit specialization for that template, the explicit
24838 instantiation has no effect. */
24839 return;
24840 }
24841 else if (DECL_EXPLICIT_INSTANTIATION (result))
24842 {
24843 /* [temp.spec]
24844
24845 No program shall explicitly instantiate any template more
24846 than once.
24847
24848 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24849 the first instantiation was `extern' and the second is not,
24850 and EXTERN_P for the opposite case. */
24851 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24852 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24853 /* If an "extern" explicit instantiation follows an ordinary
24854 explicit instantiation, the template is instantiated. */
24855 if (extern_p)
24856 return;
24857 }
24858 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24859 {
24860 error ("no matching template for %qD found", result);
24861 return;
24862 }
24863 else if (!DECL_TEMPLATE_INFO (result))
24864 {
24865 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24866 return;
24867 }
24868
24869 if (storage == NULL_TREE)
24870 ;
24871 else if (storage == ridpointers[(int) RID_EXTERN])
24872 {
24873 if (cxx_dialect == cxx98)
24874 pedwarn (input_location, OPT_Wpedantic,
24875 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24876 "instantiations");
24877 extern_p = 1;
24878 }
24879 else
24880 error ("storage class %qD applied to template instantiation", storage);
24881
24882 check_explicit_instantiation_namespace (result);
24883 mark_decl_instantiated (result, extern_p);
24884 if (! extern_p)
24885 instantiate_decl (result, /*defer_ok=*/true,
24886 /*expl_inst_class_mem_p=*/false);
24887 }
24888
24889 static void
24890 mark_class_instantiated (tree t, int extern_p)
24891 {
24892 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24893 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24894 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24895 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24896 if (! extern_p)
24897 {
24898 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24899 rest_of_type_compilation (t, 1);
24900 }
24901 }
24902
24903 /* Called from do_type_instantiation through binding_table_foreach to
24904 do recursive instantiation for the type bound in ENTRY. */
24905 static void
24906 bt_instantiate_type_proc (binding_entry entry, void *data)
24907 {
24908 tree storage = *(tree *) data;
24909
24910 if (MAYBE_CLASS_TYPE_P (entry->type)
24911 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24912 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24913 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24914 }
24915
24916 /* Perform an explicit instantiation of template class T. STORAGE, if
24917 non-null, is the RID for extern, inline or static. COMPLAIN is
24918 nonzero if this is called from the parser, zero if called recursively,
24919 since the standard is unclear (as detailed below). */
24920
24921 void
24922 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24923 {
24924 int extern_p = 0;
24925 int nomem_p = 0;
24926 int static_p = 0;
24927 int previous_instantiation_extern_p = 0;
24928
24929 if (TREE_CODE (t) == TYPE_DECL)
24930 t = TREE_TYPE (t);
24931
24932 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24933 {
24934 tree tmpl =
24935 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24936 if (tmpl)
24937 error ("explicit instantiation of non-class template %qD", tmpl);
24938 else
24939 error ("explicit instantiation of non-template type %qT", t);
24940 return;
24941 }
24942
24943 complete_type (t);
24944
24945 if (!COMPLETE_TYPE_P (t))
24946 {
24947 if (complain & tf_error)
24948 error ("explicit instantiation of %q#T before definition of template",
24949 t);
24950 return;
24951 }
24952
24953 if (storage != NULL_TREE)
24954 {
24955 if (storage == ridpointers[(int) RID_EXTERN])
24956 {
24957 if (cxx_dialect == cxx98)
24958 pedwarn (input_location, OPT_Wpedantic,
24959 "ISO C++ 1998 forbids the use of %<extern%> on "
24960 "explicit instantiations");
24961 }
24962 else
24963 pedwarn (input_location, OPT_Wpedantic,
24964 "ISO C++ forbids the use of %qE"
24965 " on explicit instantiations", storage);
24966
24967 if (storage == ridpointers[(int) RID_INLINE])
24968 nomem_p = 1;
24969 else if (storage == ridpointers[(int) RID_EXTERN])
24970 extern_p = 1;
24971 else if (storage == ridpointers[(int) RID_STATIC])
24972 static_p = 1;
24973 else
24974 {
24975 error ("storage class %qD applied to template instantiation",
24976 storage);
24977 extern_p = 0;
24978 }
24979 }
24980
24981 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24982 {
24983 /* DR 259 [temp.spec].
24984
24985 Both an explicit instantiation and a declaration of an explicit
24986 specialization shall not appear in a program unless the explicit
24987 instantiation follows a declaration of the explicit specialization.
24988
24989 For a given set of template parameters, if an explicit
24990 instantiation of a template appears after a declaration of an
24991 explicit specialization for that template, the explicit
24992 instantiation has no effect. */
24993 return;
24994 }
24995 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24996 {
24997 /* [temp.spec]
24998
24999 No program shall explicitly instantiate any template more
25000 than once.
25001
25002 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25003 instantiation was `extern'. If EXTERN_P then the second is.
25004 These cases are OK. */
25005 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
25006
25007 if (!previous_instantiation_extern_p && !extern_p
25008 && (complain & tf_error))
25009 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
25010
25011 /* If we've already instantiated the template, just return now. */
25012 if (!CLASSTYPE_INTERFACE_ONLY (t))
25013 return;
25014 }
25015
25016 check_explicit_instantiation_namespace (TYPE_NAME (t));
25017 mark_class_instantiated (t, extern_p);
25018
25019 if (nomem_p)
25020 return;
25021
25022 /* In contrast to implicit instantiation, where only the
25023 declarations, and not the definitions, of members are
25024 instantiated, we have here:
25025
25026 [temp.explicit]
25027
25028 An explicit instantiation that names a class template
25029 specialization is also an explicit instantiation of the same
25030 kind (declaration or definition) of each of its members (not
25031 including members inherited from base classes and members
25032 that are templates) that has not been previously explicitly
25033 specialized in the translation unit containing the explicit
25034 instantiation, provided that the associated constraints, if
25035 any, of that member are satisfied by the template arguments
25036 of the explicit instantiation. */
25037 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25038 if ((VAR_P (fld)
25039 || (TREE_CODE (fld) == FUNCTION_DECL
25040 && !static_p
25041 && user_provided_p (fld)))
25042 && DECL_TEMPLATE_INSTANTIATION (fld)
25043 && constraints_satisfied_p (fld))
25044 {
25045 mark_decl_instantiated (fld, extern_p);
25046 if (! extern_p)
25047 instantiate_decl (fld, /*defer_ok=*/true,
25048 /*expl_inst_class_mem_p=*/true);
25049 }
25050
25051 if (CLASSTYPE_NESTED_UTDS (t))
25052 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
25053 bt_instantiate_type_proc, &storage);
25054 }
25055
25056 /* Given a function DECL, which is a specialization of TMPL, modify
25057 DECL to be a re-instantiation of TMPL with the same template
25058 arguments. TMPL should be the template into which tsubst'ing
25059 should occur for DECL, not the most general template.
25060
25061 One reason for doing this is a scenario like this:
25062
25063 template <class T>
25064 void f(const T&, int i);
25065
25066 void g() { f(3, 7); }
25067
25068 template <class T>
25069 void f(const T& t, const int i) { }
25070
25071 Note that when the template is first instantiated, with
25072 instantiate_template, the resulting DECL will have no name for the
25073 first parameter, and the wrong type for the second. So, when we go
25074 to instantiate the DECL, we regenerate it. */
25075
25076 static void
25077 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25078 {
25079 /* The arguments used to instantiate DECL, from the most general
25080 template. */
25081 tree code_pattern;
25082
25083 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25084
25085 /* Make sure that we can see identifiers, and compute access
25086 correctly. */
25087 push_access_scope (decl);
25088
25089 if (TREE_CODE (decl) == FUNCTION_DECL)
25090 {
25091 tree decl_parm;
25092 tree pattern_parm;
25093 tree specs;
25094 int args_depth;
25095 int parms_depth;
25096
25097 args_depth = TMPL_ARGS_DEPTH (args);
25098 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25099 if (args_depth > parms_depth)
25100 args = get_innermost_template_args (args, parms_depth);
25101
25102 /* Instantiate a dynamic exception-specification. noexcept will be
25103 handled below. */
25104 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25105 if (TREE_VALUE (raises))
25106 {
25107 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25108 args, tf_error, NULL_TREE,
25109 /*defer_ok*/false);
25110 if (specs && specs != error_mark_node)
25111 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25112 specs);
25113 }
25114
25115 /* Merge parameter declarations. */
25116 decl_parm = skip_artificial_parms_for (decl,
25117 DECL_ARGUMENTS (decl));
25118 pattern_parm
25119 = skip_artificial_parms_for (code_pattern,
25120 DECL_ARGUMENTS (code_pattern));
25121 while (decl_parm && !DECL_PACK_P (pattern_parm))
25122 {
25123 tree parm_type;
25124 tree attributes;
25125
25126 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25127 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25128 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25129 NULL_TREE);
25130 parm_type = type_decays_to (parm_type);
25131 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25132 TREE_TYPE (decl_parm) = parm_type;
25133 attributes = DECL_ATTRIBUTES (pattern_parm);
25134 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25135 {
25136 DECL_ATTRIBUTES (decl_parm) = attributes;
25137 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25138 }
25139 decl_parm = DECL_CHAIN (decl_parm);
25140 pattern_parm = DECL_CHAIN (pattern_parm);
25141 }
25142 /* Merge any parameters that match with the function parameter
25143 pack. */
25144 if (pattern_parm && DECL_PACK_P (pattern_parm))
25145 {
25146 int i, len;
25147 tree expanded_types;
25148 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25149 the parameters in this function parameter pack. */
25150 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25151 args, tf_error, NULL_TREE);
25152 len = TREE_VEC_LENGTH (expanded_types);
25153 for (i = 0; i < len; i++)
25154 {
25155 tree parm_type;
25156 tree attributes;
25157
25158 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25159 /* Rename the parameter to include the index. */
25160 DECL_NAME (decl_parm) =
25161 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25162 parm_type = TREE_VEC_ELT (expanded_types, i);
25163 parm_type = type_decays_to (parm_type);
25164 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25165 TREE_TYPE (decl_parm) = parm_type;
25166 attributes = DECL_ATTRIBUTES (pattern_parm);
25167 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25168 {
25169 DECL_ATTRIBUTES (decl_parm) = attributes;
25170 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25171 }
25172 decl_parm = DECL_CHAIN (decl_parm);
25173 }
25174 }
25175 /* Merge additional specifiers from the CODE_PATTERN. */
25176 if (DECL_DECLARED_INLINE_P (code_pattern)
25177 && !DECL_DECLARED_INLINE_P (decl))
25178 DECL_DECLARED_INLINE_P (decl) = 1;
25179
25180 maybe_instantiate_noexcept (decl, tf_error);
25181 }
25182 else if (VAR_P (decl))
25183 {
25184 start_lambda_scope (decl);
25185 DECL_INITIAL (decl) =
25186 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25187 tf_error, DECL_TI_TEMPLATE (decl));
25188 finish_lambda_scope ();
25189 if (VAR_HAD_UNKNOWN_BOUND (decl))
25190 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25191 tf_error, DECL_TI_TEMPLATE (decl));
25192 }
25193 else
25194 gcc_unreachable ();
25195
25196 pop_access_scope (decl);
25197 }
25198
25199 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25200 substituted to get DECL. */
25201
25202 tree
25203 template_for_substitution (tree decl)
25204 {
25205 tree tmpl = DECL_TI_TEMPLATE (decl);
25206
25207 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25208 for the instantiation. This is not always the most general
25209 template. Consider, for example:
25210
25211 template <class T>
25212 struct S { template <class U> void f();
25213 template <> void f<int>(); };
25214
25215 and an instantiation of S<double>::f<int>. We want TD to be the
25216 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25217 while (/* An instantiation cannot have a definition, so we need a
25218 more general template. */
25219 DECL_TEMPLATE_INSTANTIATION (tmpl)
25220 /* We must also deal with friend templates. Given:
25221
25222 template <class T> struct S {
25223 template <class U> friend void f() {};
25224 };
25225
25226 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25227 so far as the language is concerned, but that's still
25228 where we get the pattern for the instantiation from. On
25229 other hand, if the definition comes outside the class, say:
25230
25231 template <class T> struct S {
25232 template <class U> friend void f();
25233 };
25234 template <class U> friend void f() {}
25235
25236 we don't need to look any further. That's what the check for
25237 DECL_INITIAL is for. */
25238 || (TREE_CODE (decl) == FUNCTION_DECL
25239 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25240 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25241 {
25242 /* The present template, TD, should not be a definition. If it
25243 were a definition, we should be using it! Note that we
25244 cannot restructure the loop to just keep going until we find
25245 a template with a definition, since that might go too far if
25246 a specialization was declared, but not defined. */
25247
25248 /* Fetch the more general template. */
25249 tmpl = DECL_TI_TEMPLATE (tmpl);
25250 }
25251
25252 return tmpl;
25253 }
25254
25255 /* Returns true if we need to instantiate this template instance even if we
25256 know we aren't going to emit it. */
25257
25258 bool
25259 always_instantiate_p (tree decl)
25260 {
25261 /* We always instantiate inline functions so that we can inline them. An
25262 explicit instantiation declaration prohibits implicit instantiation of
25263 non-inline functions. With high levels of optimization, we would
25264 normally inline non-inline functions -- but we're not allowed to do
25265 that for "extern template" functions. Therefore, we check
25266 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25267 return ((TREE_CODE (decl) == FUNCTION_DECL
25268 && (DECL_DECLARED_INLINE_P (decl)
25269 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25270 /* And we need to instantiate static data members so that
25271 their initializers are available in integral constant
25272 expressions. */
25273 || (VAR_P (decl)
25274 && decl_maybe_constant_var_p (decl)));
25275 }
25276
25277 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25278 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25279 error, true otherwise. */
25280
25281 bool
25282 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25283 {
25284 tree fntype, spec, noex;
25285
25286 /* Don't instantiate a noexcept-specification from template context. */
25287 if (processing_template_decl
25288 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25289 return true;
25290
25291 if (DECL_MAYBE_DELETED (fn))
25292 {
25293 if (fn == current_function_decl)
25294 /* We're in start_preparsed_function, keep going. */
25295 return true;
25296
25297 ++function_depth;
25298 synthesize_method (fn);
25299 --function_depth;
25300 return !DECL_MAYBE_DELETED (fn);
25301 }
25302
25303 fntype = TREE_TYPE (fn);
25304 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25305
25306 if (!spec || !TREE_PURPOSE (spec))
25307 return true;
25308
25309 noex = TREE_PURPOSE (spec);
25310 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25311 && TREE_CODE (noex) != DEFERRED_PARSE)
25312 return true;
25313
25314 tree orig_fn = NULL_TREE;
25315 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25316 its FUNCTION_DECL for the rest of this function -- push_access_scope
25317 doesn't accept TEMPLATE_DECLs. */
25318 if (DECL_FUNCTION_TEMPLATE_P (fn))
25319 {
25320 orig_fn = fn;
25321 fn = DECL_TEMPLATE_RESULT (fn);
25322 }
25323
25324 if (DECL_CLONED_FUNCTION_P (fn))
25325 {
25326 tree prime = DECL_CLONED_FUNCTION (fn);
25327 if (!maybe_instantiate_noexcept (prime, complain))
25328 return false;
25329 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25330 }
25331 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25332 {
25333 static hash_set<tree>* fns = new hash_set<tree>;
25334 bool added = false;
25335 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25336 {
25337 spec = get_defaulted_eh_spec (fn, complain);
25338 if (spec == error_mark_node)
25339 /* This might have failed because of an unparsed DMI, so
25340 let's try again later. */
25341 return false;
25342 }
25343 else if (!(added = !fns->add (fn)))
25344 {
25345 /* If hash_set::add returns true, the element was already there. */
25346 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25347 DECL_SOURCE_LOCATION (fn));
25348 error_at (loc,
25349 "exception specification of %qD depends on itself",
25350 fn);
25351 spec = noexcept_false_spec;
25352 }
25353 else if (push_tinst_level (fn))
25354 {
25355 push_to_top_level ();
25356 push_access_scope (fn);
25357 push_deferring_access_checks (dk_no_deferred);
25358 input_location = DECL_SOURCE_LOCATION (fn);
25359
25360 /* If needed, set current_class_ptr for the benefit of
25361 tsubst_copy/PARM_DECL. */
25362 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25363 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25364 {
25365 tree this_parm = DECL_ARGUMENTS (tdecl);
25366 current_class_ptr = NULL_TREE;
25367 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25368 current_class_ptr = this_parm;
25369 }
25370
25371 /* If this function is represented by a TEMPLATE_DECL, then
25372 the deferred noexcept-specification might still contain
25373 dependent types, even after substitution. And we need the
25374 dependency check functions to work in build_noexcept_spec. */
25375 if (orig_fn)
25376 ++processing_template_decl;
25377
25378 /* Do deferred instantiation of the noexcept-specifier. */
25379 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25380 DEFERRED_NOEXCEPT_ARGS (noex),
25381 tf_warning_or_error, fn,
25382 /*function_p=*/false,
25383 /*i_c_e_p=*/true);
25384
25385 /* Build up the noexcept-specification. */
25386 spec = build_noexcept_spec (noex, tf_warning_or_error);
25387
25388 if (orig_fn)
25389 --processing_template_decl;
25390
25391 pop_deferring_access_checks ();
25392 pop_access_scope (fn);
25393 pop_tinst_level ();
25394 pop_from_top_level ();
25395 }
25396 else
25397 spec = noexcept_false_spec;
25398
25399 if (added)
25400 fns->remove (fn);
25401 }
25402
25403 if (spec == error_mark_node)
25404 {
25405 /* This failed with a hard error, so let's go with false. */
25406 gcc_assert (seen_error ());
25407 spec = noexcept_false_spec;
25408 }
25409
25410 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25411 if (orig_fn)
25412 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25413
25414 return true;
25415 }
25416
25417 /* We're starting to process the function INST, an instantiation of PATTERN;
25418 add their parameters to local_specializations. */
25419
25420 static void
25421 register_parameter_specializations (tree pattern, tree inst)
25422 {
25423 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25424 tree spec_parm = DECL_ARGUMENTS (inst);
25425 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25426 {
25427 register_local_specialization (spec_parm, tmpl_parm);
25428 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25429 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25430 }
25431 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25432 {
25433 if (!DECL_PACK_P (tmpl_parm)
25434 || (spec_parm && DECL_PACK_P (spec_parm)))
25435 {
25436 register_local_specialization (spec_parm, tmpl_parm);
25437 spec_parm = DECL_CHAIN (spec_parm);
25438 }
25439 else
25440 {
25441 /* Register the (value) argument pack as a specialization of
25442 TMPL_PARM, then move on. */
25443 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25444 register_local_specialization (argpack, tmpl_parm);
25445 }
25446 }
25447 gcc_assert (!spec_parm);
25448 }
25449
25450 /* Instantiate the body of D using PATTERN with ARGS. We have
25451 already determined PATTERN is the correct template to use. */
25452
25453 static void
25454 instantiate_body (tree pattern, tree args, tree d)
25455 {
25456 gcc_checking_assert (TREE_CODE (pattern) == TEMPLATE_DECL);
25457
25458 tree td = pattern;
25459 tree code_pattern = DECL_TEMPLATE_RESULT (td);
25460
25461 tree fn_context = decl_function_context (d);
25462 if (LAMBDA_FUNCTION_P (d))
25463 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25464 fn_context = NULL_TREE;
25465 bool nested = current_function_decl != NULL_TREE;
25466 bool push_to_top = !(nested && fn_context == current_function_decl);
25467
25468 vec<tree> omp_privatization_save;
25469 if (nested)
25470 save_omp_privatization_clauses (omp_privatization_save);
25471
25472 if (push_to_top)
25473 push_to_top_level ();
25474 else
25475 {
25476 gcc_assert (!processing_template_decl);
25477 push_function_context ();
25478 cp_unevaluated_operand = 0;
25479 c_inhibit_evaluation_warnings = 0;
25480 }
25481
25482 if (VAR_P (d))
25483 {
25484 /* The variable might be a lambda's extra scope, and that
25485 lambda's visibility depends on D's. */
25486 maybe_commonize_var (d);
25487 determine_visibility (d);
25488 }
25489
25490 /* Mark D as instantiated so that recursive calls to
25491 instantiate_decl do not try to instantiate it again. */
25492 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25493
25494 /* Regenerate the declaration in case the template has been modified
25495 by a subsequent redeclaration. */
25496 regenerate_decl_from_template (d, td, args);
25497
25498 /* We already set the file and line above. Reset them now in case
25499 they changed as a result of calling regenerate_decl_from_template. */
25500 input_location = DECL_SOURCE_LOCATION (d);
25501
25502 if (VAR_P (d))
25503 {
25504 tree init;
25505 bool const_init = false;
25506
25507 /* Clear out DECL_RTL; whatever was there before may not be right
25508 since we've reset the type of the declaration. */
25509 SET_DECL_RTL (d, NULL);
25510 DECL_IN_AGGR_P (d) = 0;
25511
25512 /* The initializer is placed in DECL_INITIAL by
25513 regenerate_decl_from_template so we don't need to
25514 push/pop_access_scope again here. Pull it out so that
25515 cp_finish_decl can process it. */
25516 init = DECL_INITIAL (d);
25517 DECL_INITIAL (d) = NULL_TREE;
25518 DECL_INITIALIZED_P (d) = 0;
25519
25520 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25521 initializer. That function will defer actual emission until
25522 we have a chance to determine linkage. */
25523 DECL_EXTERNAL (d) = 0;
25524
25525 /* Enter the scope of D so that access-checking works correctly. */
25526 bool enter_context = DECL_CLASS_SCOPE_P (d);
25527 if (enter_context)
25528 push_nested_class (DECL_CONTEXT (d));
25529
25530 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25531 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25532
25533 if (enter_context)
25534 pop_nested_class ();
25535 }
25536 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25537 synthesize_method (d);
25538 else if (TREE_CODE (d) == FUNCTION_DECL)
25539 {
25540 /* Set up the list of local specializations. */
25541 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25542 tree block = NULL_TREE;
25543
25544 /* Set up context. */
25545 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25546 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25547 block = push_stmt_list ();
25548 else
25549 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25550
25551 perform_instantiation_time_access_checks (code_pattern, args);
25552
25553 /* Create substitution entries for the parameters. */
25554 register_parameter_specializations (code_pattern, d);
25555
25556 /* Substitute into the body of the function. */
25557 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25558 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25559 tf_warning_or_error, DECL_TI_TEMPLATE (d));
25560 else
25561 {
25562 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25563 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25564 /*integral_constant_expression_p=*/false);
25565
25566 /* Set the current input_location to the end of the function
25567 so that finish_function knows where we are. */
25568 input_location
25569 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25570
25571 /* Remember if we saw an infinite loop in the template. */
25572 current_function_infinite_loop
25573 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25574 }
25575
25576 /* Finish the function. */
25577 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25578 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25579 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25580 else
25581 {
25582 d = finish_function (/*inline_p=*/false);
25583 expand_or_defer_fn (d);
25584 }
25585
25586 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25587 cp_check_omp_declare_reduction (d);
25588 }
25589
25590 /* We're not deferring instantiation any more. */
25591 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25592
25593 if (push_to_top)
25594 pop_from_top_level ();
25595 else
25596 pop_function_context ();
25597
25598 if (nested)
25599 restore_omp_privatization_clauses (omp_privatization_save);
25600 }
25601
25602 /* Produce the definition of D, a _DECL generated from a template. If
25603 DEFER_OK is true, then we don't have to actually do the
25604 instantiation now; we just have to do it sometime. Normally it is
25605 an error if this is an explicit instantiation but D is undefined.
25606 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25607 instantiated class template. */
25608
25609 tree
25610 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25611 {
25612 tree tmpl = DECL_TI_TEMPLATE (d);
25613 tree gen_args;
25614 tree args;
25615 tree td;
25616 tree code_pattern;
25617 tree spec;
25618 tree gen_tmpl;
25619 bool pattern_defined;
25620 location_t saved_loc = input_location;
25621 int saved_unevaluated_operand = cp_unevaluated_operand;
25622 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25623 bool external_p;
25624 bool deleted_p;
25625
25626 /* This function should only be used to instantiate templates for
25627 functions and static member variables. */
25628 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25629
25630 /* A concept is never instantiated. */
25631 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25632
25633 /* Variables are never deferred; if instantiation is required, they
25634 are instantiated right away. That allows for better code in the
25635 case that an expression refers to the value of the variable --
25636 if the variable has a constant value the referring expression can
25637 take advantage of that fact. */
25638 if (VAR_P (d))
25639 defer_ok = false;
25640
25641 /* Don't instantiate cloned functions. Instead, instantiate the
25642 functions they cloned. */
25643 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25644 d = DECL_CLONED_FUNCTION (d);
25645
25646 if (DECL_TEMPLATE_INSTANTIATED (d)
25647 || TREE_TYPE (d) == error_mark_node
25648 || (TREE_CODE (d) == FUNCTION_DECL
25649 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25650 || DECL_TEMPLATE_SPECIALIZATION (d))
25651 /* D has already been instantiated or explicitly specialized, so
25652 there's nothing for us to do here.
25653
25654 It might seem reasonable to check whether or not D is an explicit
25655 instantiation, and, if so, stop here. But when an explicit
25656 instantiation is deferred until the end of the compilation,
25657 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25658 the instantiation. */
25659 return d;
25660
25661 /* Check to see whether we know that this template will be
25662 instantiated in some other file, as with "extern template"
25663 extension. */
25664 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25665
25666 /* In general, we do not instantiate such templates. */
25667 if (external_p && !always_instantiate_p (d))
25668 return d;
25669
25670 gen_tmpl = most_general_template (tmpl);
25671 gen_args = DECL_TI_ARGS (d);
25672
25673 /* We should already have the extra args. */
25674 gcc_checking_assert (tmpl == gen_tmpl
25675 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25676 == TMPL_ARGS_DEPTH (gen_args)));
25677 /* And what's in the hash table should match D. */
25678 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25679 == d
25680 || spec == NULL_TREE);
25681
25682 /* This needs to happen before any tsubsting. */
25683 if (! push_tinst_level (d))
25684 return d;
25685
25686 timevar_push (TV_TEMPLATE_INST);
25687
25688 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25689 for the instantiation. */
25690 td = template_for_substitution (d);
25691 args = gen_args;
25692
25693 if (VAR_P (d))
25694 {
25695 /* Look up an explicit specialization, if any. */
25696 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25697 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25698 if (elt && elt != error_mark_node)
25699 {
25700 td = TREE_VALUE (elt);
25701 args = TREE_PURPOSE (elt);
25702 }
25703 }
25704
25705 code_pattern = DECL_TEMPLATE_RESULT (td);
25706
25707 /* We should never be trying to instantiate a member of a class
25708 template or partial specialization. */
25709 gcc_assert (d != code_pattern);
25710
25711 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25712 || DECL_TEMPLATE_SPECIALIZATION (td))
25713 /* In the case of a friend template whose definition is provided
25714 outside the class, we may have too many arguments. Drop the
25715 ones we don't need. The same is true for specializations. */
25716 args = get_innermost_template_args
25717 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25718
25719 if (TREE_CODE (d) == FUNCTION_DECL)
25720 {
25721 deleted_p = DECL_DELETED_FN (code_pattern);
25722 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25723 && DECL_INITIAL (code_pattern) != error_mark_node)
25724 || DECL_DEFAULTED_FN (code_pattern)
25725 || deleted_p);
25726 }
25727 else
25728 {
25729 deleted_p = false;
25730 if (DECL_CLASS_SCOPE_P (code_pattern))
25731 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25732 else
25733 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25734 }
25735
25736 /* We may be in the middle of deferred access check. Disable it now. */
25737 push_deferring_access_checks (dk_no_deferred);
25738
25739 /* Unless an explicit instantiation directive has already determined
25740 the linkage of D, remember that a definition is available for
25741 this entity. */
25742 if (pattern_defined
25743 && !DECL_INTERFACE_KNOWN (d)
25744 && !DECL_NOT_REALLY_EXTERN (d))
25745 mark_definable (d);
25746
25747 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25748 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25749 input_location = DECL_SOURCE_LOCATION (d);
25750
25751 /* If D is a member of an explicitly instantiated class template,
25752 and no definition is available, treat it like an implicit
25753 instantiation. */
25754 if (!pattern_defined && expl_inst_class_mem_p
25755 && DECL_EXPLICIT_INSTANTIATION (d))
25756 {
25757 /* Leave linkage flags alone on instantiations with anonymous
25758 visibility. */
25759 if (TREE_PUBLIC (d))
25760 {
25761 DECL_NOT_REALLY_EXTERN (d) = 0;
25762 DECL_INTERFACE_KNOWN (d) = 0;
25763 }
25764 SET_DECL_IMPLICIT_INSTANTIATION (d);
25765 }
25766
25767 /* Defer all other templates, unless we have been explicitly
25768 forbidden from doing so. */
25769 if (/* If there is no definition, we cannot instantiate the
25770 template. */
25771 ! pattern_defined
25772 /* If it's OK to postpone instantiation, do so. */
25773 || defer_ok
25774 /* If this is a static data member that will be defined
25775 elsewhere, we don't want to instantiate the entire data
25776 member, but we do want to instantiate the initializer so that
25777 we can substitute that elsewhere. */
25778 || (external_p && VAR_P (d))
25779 /* Handle here a deleted function too, avoid generating
25780 its body (c++/61080). */
25781 || deleted_p)
25782 {
25783 /* The definition of the static data member is now required so
25784 we must substitute the initializer. */
25785 if (VAR_P (d)
25786 && !DECL_INITIAL (d)
25787 && DECL_INITIAL (code_pattern))
25788 {
25789 tree ns;
25790 tree init;
25791 bool const_init = false;
25792 bool enter_context = DECL_CLASS_SCOPE_P (d);
25793
25794 ns = decl_namespace_context (d);
25795 push_nested_namespace (ns);
25796 if (enter_context)
25797 push_nested_class (DECL_CONTEXT (d));
25798 init = tsubst_expr (DECL_INITIAL (code_pattern),
25799 args,
25800 tf_warning_or_error, NULL_TREE,
25801 /*integral_constant_expression_p=*/false);
25802 /* If instantiating the initializer involved instantiating this
25803 again, don't call cp_finish_decl twice. */
25804 if (!DECL_INITIAL (d))
25805 {
25806 /* Make sure the initializer is still constant, in case of
25807 circular dependency (template/instantiate6.C). */
25808 const_init
25809 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25810 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25811 /*asmspec_tree=*/NULL_TREE,
25812 LOOKUP_ONLYCONVERTING);
25813 }
25814 if (enter_context)
25815 pop_nested_class ();
25816 pop_nested_namespace (ns);
25817 }
25818
25819 /* We restore the source position here because it's used by
25820 add_pending_template. */
25821 input_location = saved_loc;
25822
25823 if (at_eof && !pattern_defined
25824 && DECL_EXPLICIT_INSTANTIATION (d)
25825 && DECL_NOT_REALLY_EXTERN (d))
25826 /* [temp.explicit]
25827
25828 The definition of a non-exported function template, a
25829 non-exported member function template, or a non-exported
25830 member function or static data member of a class template
25831 shall be present in every translation unit in which it is
25832 explicitly instantiated. */
25833 permerror (input_location, "explicit instantiation of %qD "
25834 "but no definition available", d);
25835
25836 /* If we're in unevaluated context, we just wanted to get the
25837 constant value; this isn't an odr use, so don't queue
25838 a full instantiation. */
25839 if (!cp_unevaluated_operand
25840 /* ??? Historically, we have instantiated inline functions, even
25841 when marked as "extern template". */
25842 && !(external_p && VAR_P (d)))
25843 add_pending_template (d);
25844 }
25845 else
25846 {
25847 if (variable_template_p (gen_tmpl))
25848 note_variable_template_instantiation (d);
25849 instantiate_body (td, args, d);
25850 }
25851
25852 pop_deferring_access_checks ();
25853 timevar_pop (TV_TEMPLATE_INST);
25854 pop_tinst_level ();
25855 input_location = saved_loc;
25856 cp_unevaluated_operand = saved_unevaluated_operand;
25857 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25858
25859 return d;
25860 }
25861
25862 /* Run through the list of templates that we wish we could
25863 instantiate, and instantiate any we can. RETRIES is the
25864 number of times we retry pending template instantiation. */
25865
25866 void
25867 instantiate_pending_templates (int retries)
25868 {
25869 int reconsider;
25870 location_t saved_loc = input_location;
25871
25872 /* Instantiating templates may trigger vtable generation. This in turn
25873 may require further template instantiations. We place a limit here
25874 to avoid infinite loop. */
25875 if (pending_templates && retries >= max_tinst_depth)
25876 {
25877 tree decl = pending_templates->tinst->maybe_get_node ();
25878
25879 fatal_error (input_location,
25880 "template instantiation depth exceeds maximum of %d"
25881 " instantiating %q+D, possibly from virtual table generation"
25882 " (use %<-ftemplate-depth=%> to increase the maximum)",
25883 max_tinst_depth, decl);
25884 if (TREE_CODE (decl) == FUNCTION_DECL)
25885 /* Pretend that we defined it. */
25886 DECL_INITIAL (decl) = error_mark_node;
25887 return;
25888 }
25889
25890 do
25891 {
25892 struct pending_template **t = &pending_templates;
25893 struct pending_template *last = NULL;
25894 reconsider = 0;
25895 while (*t)
25896 {
25897 tree instantiation = reopen_tinst_level ((*t)->tinst);
25898 bool complete = false;
25899
25900 if (TYPE_P (instantiation))
25901 {
25902 if (!COMPLETE_TYPE_P (instantiation))
25903 {
25904 instantiate_class_template (instantiation);
25905 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25906 for (tree fld = TYPE_FIELDS (instantiation);
25907 fld; fld = TREE_CHAIN (fld))
25908 if ((VAR_P (fld)
25909 || (TREE_CODE (fld) == FUNCTION_DECL
25910 && !DECL_ARTIFICIAL (fld)))
25911 && DECL_TEMPLATE_INSTANTIATION (fld))
25912 instantiate_decl (fld,
25913 /*defer_ok=*/false,
25914 /*expl_inst_class_mem_p=*/false);
25915
25916 if (COMPLETE_TYPE_P (instantiation))
25917 reconsider = 1;
25918 }
25919
25920 complete = COMPLETE_TYPE_P (instantiation);
25921 }
25922 else
25923 {
25924 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25925 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25926 {
25927 instantiation
25928 = instantiate_decl (instantiation,
25929 /*defer_ok=*/false,
25930 /*expl_inst_class_mem_p=*/false);
25931 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25932 reconsider = 1;
25933 }
25934
25935 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25936 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25937 }
25938
25939 if (complete)
25940 {
25941 /* If INSTANTIATION has been instantiated, then we don't
25942 need to consider it again in the future. */
25943 struct pending_template *drop = *t;
25944 *t = (*t)->next;
25945 set_refcount_ptr (drop->tinst);
25946 pending_template_freelist ().free (drop);
25947 }
25948 else
25949 {
25950 last = *t;
25951 t = &(*t)->next;
25952 }
25953 tinst_depth = 0;
25954 set_refcount_ptr (current_tinst_level);
25955 }
25956 last_pending_template = last;
25957 }
25958 while (reconsider);
25959
25960 input_location = saved_loc;
25961 }
25962
25963 /* Substitute ARGVEC into T, which is a list of initializers for
25964 either base class or a non-static data member. The TREE_PURPOSEs
25965 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25966 instantiate_decl. */
25967
25968 static tree
25969 tsubst_initializer_list (tree t, tree argvec)
25970 {
25971 tree inits = NULL_TREE;
25972 tree target_ctor = error_mark_node;
25973
25974 for (; t; t = TREE_CHAIN (t))
25975 {
25976 tree decl;
25977 tree init;
25978 tree expanded_bases = NULL_TREE;
25979 tree expanded_arguments = NULL_TREE;
25980 int i, len = 1;
25981
25982 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25983 {
25984 tree expr;
25985 tree arg;
25986
25987 /* Expand the base class expansion type into separate base
25988 classes. */
25989 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25990 tf_warning_or_error,
25991 NULL_TREE);
25992 if (expanded_bases == error_mark_node)
25993 continue;
25994
25995 /* We'll be building separate TREE_LISTs of arguments for
25996 each base. */
25997 len = TREE_VEC_LENGTH (expanded_bases);
25998 expanded_arguments = make_tree_vec (len);
25999 for (i = 0; i < len; i++)
26000 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26001
26002 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26003 expand each argument in the TREE_VALUE of t. */
26004 expr = make_node (EXPR_PACK_EXPANSION);
26005 PACK_EXPANSION_LOCAL_P (expr) = true;
26006 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26007 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26008
26009 if (TREE_VALUE (t) == void_type_node)
26010 /* VOID_TYPE_NODE is used to indicate
26011 value-initialization. */
26012 {
26013 for (i = 0; i < len; i++)
26014 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26015 }
26016 else
26017 {
26018 /* Substitute parameter packs into each argument in the
26019 TREE_LIST. */
26020 in_base_initializer = 1;
26021 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26022 {
26023 tree expanded_exprs;
26024
26025 /* Expand the argument. */
26026 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26027 expanded_exprs
26028 = tsubst_pack_expansion (expr, argvec,
26029 tf_warning_or_error,
26030 NULL_TREE);
26031 if (expanded_exprs == error_mark_node)
26032 continue;
26033
26034 /* Prepend each of the expanded expressions to the
26035 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26036 for (i = 0; i < len; i++)
26037 {
26038 TREE_VEC_ELT (expanded_arguments, i) =
26039 tree_cons (NULL_TREE,
26040 TREE_VEC_ELT (expanded_exprs, i),
26041 TREE_VEC_ELT (expanded_arguments, i));
26042 }
26043 }
26044 in_base_initializer = 0;
26045
26046 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26047 since we built them backwards. */
26048 for (i = 0; i < len; i++)
26049 {
26050 TREE_VEC_ELT (expanded_arguments, i) =
26051 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26052 }
26053 }
26054 }
26055
26056 for (i = 0; i < len; ++i)
26057 {
26058 if (expanded_bases)
26059 {
26060 decl = TREE_VEC_ELT (expanded_bases, i);
26061 decl = expand_member_init (decl);
26062 init = TREE_VEC_ELT (expanded_arguments, i);
26063 }
26064 else
26065 {
26066 tree tmp;
26067 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26068 tf_warning_or_error, NULL_TREE);
26069
26070 decl = expand_member_init (decl);
26071 if (decl && !DECL_P (decl))
26072 in_base_initializer = 1;
26073
26074 init = TREE_VALUE (t);
26075 tmp = init;
26076 if (init != void_type_node)
26077 init = tsubst_expr (init, argvec,
26078 tf_warning_or_error, NULL_TREE,
26079 /*integral_constant_expression_p=*/false);
26080 if (init == NULL_TREE && tmp != NULL_TREE)
26081 /* If we had an initializer but it instantiated to nothing,
26082 value-initialize the object. This will only occur when
26083 the initializer was a pack expansion where the parameter
26084 packs used in that expansion were of length zero. */
26085 init = void_type_node;
26086 in_base_initializer = 0;
26087 }
26088
26089 if (target_ctor != error_mark_node
26090 && init != error_mark_node)
26091 {
26092 error ("mem-initializer for %qD follows constructor delegation",
26093 decl);
26094 return inits;
26095 }
26096 /* Look for a target constructor. */
26097 if (init != error_mark_node
26098 && decl && CLASS_TYPE_P (decl)
26099 && same_type_p (decl, current_class_type))
26100 {
26101 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26102 if (inits)
26103 {
26104 error ("constructor delegation follows mem-initializer for %qD",
26105 TREE_PURPOSE (inits));
26106 continue;
26107 }
26108 target_ctor = init;
26109 }
26110
26111 if (decl)
26112 {
26113 init = build_tree_list (decl, init);
26114 /* Carry over the dummy TREE_TYPE node containing the source
26115 location. */
26116 TREE_TYPE (init) = TREE_TYPE (t);
26117 TREE_CHAIN (init) = inits;
26118 inits = init;
26119 }
26120 }
26121 }
26122 return inits;
26123 }
26124
26125 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26126
26127 static void
26128 set_current_access_from_decl (tree decl)
26129 {
26130 if (TREE_PRIVATE (decl))
26131 current_access_specifier = access_private_node;
26132 else if (TREE_PROTECTED (decl))
26133 current_access_specifier = access_protected_node;
26134 else
26135 current_access_specifier = access_public_node;
26136 }
26137
26138 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26139 is the instantiation (which should have been created with
26140 start_enum) and ARGS are the template arguments to use. */
26141
26142 static void
26143 tsubst_enum (tree tag, tree newtag, tree args)
26144 {
26145 tree e;
26146
26147 if (SCOPED_ENUM_P (newtag))
26148 begin_scope (sk_scoped_enum, newtag);
26149
26150 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26151 {
26152 tree value;
26153 tree decl;
26154
26155 decl = TREE_VALUE (e);
26156 /* Note that in a template enum, the TREE_VALUE is the
26157 CONST_DECL, not the corresponding INTEGER_CST. */
26158 value = tsubst_expr (DECL_INITIAL (decl),
26159 args, tf_warning_or_error, NULL_TREE,
26160 /*integral_constant_expression_p=*/true);
26161
26162 /* Give this enumeration constant the correct access. */
26163 set_current_access_from_decl (decl);
26164
26165 /* Actually build the enumerator itself. Here we're assuming that
26166 enumerators can't have dependent attributes. */
26167 build_enumerator (DECL_NAME (decl), value, newtag,
26168 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26169 }
26170
26171 if (SCOPED_ENUM_P (newtag))
26172 finish_scope ();
26173
26174 finish_enum_value_list (newtag);
26175 finish_enum (newtag);
26176
26177 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26178 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26179 }
26180
26181 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26182 its type -- but without substituting the innermost set of template
26183 arguments. So, innermost set of template parameters will appear in
26184 the type. */
26185
26186 tree
26187 get_mostly_instantiated_function_type (tree decl)
26188 {
26189 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26190 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26191 }
26192
26193 /* Return truthvalue if we're processing a template different from
26194 the last one involved in diagnostics. */
26195 bool
26196 problematic_instantiation_changed (void)
26197 {
26198 return current_tinst_level != last_error_tinst_level;
26199 }
26200
26201 /* Remember current template involved in diagnostics. */
26202 void
26203 record_last_problematic_instantiation (void)
26204 {
26205 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26206 }
26207
26208 struct tinst_level *
26209 current_instantiation (void)
26210 {
26211 return current_tinst_level;
26212 }
26213
26214 /* Return TRUE if current_function_decl is being instantiated, false
26215 otherwise. */
26216
26217 bool
26218 instantiating_current_function_p (void)
26219 {
26220 return (current_instantiation ()
26221 && (current_instantiation ()->maybe_get_node ()
26222 == current_function_decl));
26223 }
26224
26225 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26226 type. Return false for ok, true for disallowed. Issue error and
26227 inform messages under control of COMPLAIN. */
26228
26229 static bool
26230 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26231 {
26232 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26233 return false;
26234 else if (TYPE_PTR_P (type))
26235 return false;
26236 else if (TYPE_REF_P (type)
26237 && !TYPE_REF_IS_RVALUE (type))
26238 return false;
26239 else if (TYPE_PTRMEM_P (type))
26240 return false;
26241 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26242 {
26243 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26244 {
26245 if (complain & tf_error)
26246 error ("non-type template parameters of deduced class type only "
26247 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26248 return true;
26249 }
26250 return false;
26251 }
26252 else if (TREE_CODE (type) == TYPENAME_TYPE)
26253 return false;
26254 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26255 return false;
26256 else if (TREE_CODE (type) == NULLPTR_TYPE)
26257 return false;
26258 /* A bound template template parm could later be instantiated to have a valid
26259 nontype parm type via an alias template. */
26260 else if (cxx_dialect >= cxx11
26261 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26262 return false;
26263 else if (VOID_TYPE_P (type))
26264 /* Fall through. */;
26265 else if (cxx_dialect >= cxx20)
26266 {
26267 if (dependent_type_p (type))
26268 return false;
26269 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26270 return true;
26271 if (structural_type_p (type))
26272 return false;
26273 if (complain & tf_error)
26274 {
26275 auto_diagnostic_group d;
26276 error ("%qT is not a valid type for a template non-type "
26277 "parameter because it is not structural", type);
26278 structural_type_p (type, true);
26279 }
26280 return true;
26281 }
26282 else if (CLASS_TYPE_P (type))
26283 {
26284 if (complain & tf_error)
26285 error ("non-type template parameters of class type only available "
26286 "with %<-std=c++20%> or %<-std=gnu++20%>");
26287 return true;
26288 }
26289
26290 if (complain & tf_error)
26291 {
26292 if (type == error_mark_node)
26293 inform (input_location, "invalid template non-type parameter");
26294 else
26295 error ("%q#T is not a valid type for a template non-type parameter",
26296 type);
26297 }
26298 return true;
26299 }
26300
26301 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26302 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26303
26304 static bool
26305 dependent_type_p_r (tree type)
26306 {
26307 tree scope;
26308
26309 /* [temp.dep.type]
26310
26311 A type is dependent if it is:
26312
26313 -- a template parameter. Template template parameters are types
26314 for us (since TYPE_P holds true for them) so we handle
26315 them here. */
26316 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26317 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26318 return true;
26319 /* -- a qualified-id with a nested-name-specifier which contains a
26320 class-name that names a dependent type or whose unqualified-id
26321 names a dependent type. */
26322 if (TREE_CODE (type) == TYPENAME_TYPE)
26323 return true;
26324
26325 /* An alias template specialization can be dependent even if the
26326 resulting type is not. */
26327 if (dependent_alias_template_spec_p (type, nt_transparent))
26328 return true;
26329
26330 /* -- a cv-qualified type where the cv-unqualified type is
26331 dependent.
26332 No code is necessary for this bullet; the code below handles
26333 cv-qualified types, and we don't want to strip aliases with
26334 TYPE_MAIN_VARIANT because of DR 1558. */
26335 /* -- a compound type constructed from any dependent type. */
26336 if (TYPE_PTRMEM_P (type))
26337 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26338 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26339 (type)));
26340 else if (INDIRECT_TYPE_P (type))
26341 return dependent_type_p (TREE_TYPE (type));
26342 else if (FUNC_OR_METHOD_TYPE_P (type))
26343 {
26344 tree arg_type;
26345
26346 if (dependent_type_p (TREE_TYPE (type)))
26347 return true;
26348 for (arg_type = TYPE_ARG_TYPES (type);
26349 arg_type;
26350 arg_type = TREE_CHAIN (arg_type))
26351 if (dependent_type_p (TREE_VALUE (arg_type)))
26352 return true;
26353 if (cxx_dialect >= cxx17)
26354 /* A value-dependent noexcept-specifier makes the type dependent. */
26355 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26356 if (tree noex = TREE_PURPOSE (spec))
26357 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26358 affect overload resolution and treating it as dependent breaks
26359 things. Same for an unparsed noexcept expression. */
26360 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26361 && TREE_CODE (noex) != DEFERRED_PARSE
26362 && value_dependent_expression_p (noex))
26363 return true;
26364 return false;
26365 }
26366 /* -- an array type constructed from any dependent type or whose
26367 size is specified by a constant expression that is
26368 value-dependent.
26369
26370 We checked for type- and value-dependence of the bounds in
26371 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26372 if (TREE_CODE (type) == ARRAY_TYPE)
26373 {
26374 if (TYPE_DOMAIN (type)
26375 && dependent_type_p (TYPE_DOMAIN (type)))
26376 return true;
26377 return dependent_type_p (TREE_TYPE (type));
26378 }
26379
26380 /* -- a template-id in which either the template name is a template
26381 parameter ... */
26382 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26383 return true;
26384 /* ... or any of the template arguments is a dependent type or
26385 an expression that is type-dependent or value-dependent. */
26386 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26387 && (any_dependent_template_arguments_p
26388 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26389 return true;
26390
26391 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26392 dependent; if the argument of the `typeof' expression is not
26393 type-dependent, then it should already been have resolved. */
26394 if (TREE_CODE (type) == TYPEOF_TYPE
26395 || TREE_CODE (type) == DECLTYPE_TYPE
26396 || TREE_CODE (type) == UNDERLYING_TYPE)
26397 return true;
26398
26399 /* A template argument pack is dependent if any of its packed
26400 arguments are. */
26401 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26402 {
26403 tree args = ARGUMENT_PACK_ARGS (type);
26404 int i, len = TREE_VEC_LENGTH (args);
26405 for (i = 0; i < len; ++i)
26406 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26407 return true;
26408 }
26409
26410 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26411 be template parameters. */
26412 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26413 return true;
26414
26415 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26416 return true;
26417
26418 /* The standard does not specifically mention types that are local
26419 to template functions or local classes, but they should be
26420 considered dependent too. For example:
26421
26422 template <int I> void f() {
26423 enum E { a = I };
26424 S<sizeof (E)> s;
26425 }
26426
26427 The size of `E' cannot be known until the value of `I' has been
26428 determined. Therefore, `E' must be considered dependent. */
26429 scope = TYPE_CONTEXT (type);
26430 if (scope && TYPE_P (scope))
26431 return dependent_type_p (scope);
26432 /* Don't use type_dependent_expression_p here, as it can lead
26433 to infinite recursion trying to determine whether a lambda
26434 nested in a lambda is dependent (c++/47687). */
26435 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26436 && DECL_LANG_SPECIFIC (scope)
26437 && DECL_TEMPLATE_INFO (scope)
26438 && (any_dependent_template_arguments_p
26439 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26440 return true;
26441
26442 /* Other types are non-dependent. */
26443 return false;
26444 }
26445
26446 /* Returns TRUE if TYPE is dependent, in the sense of
26447 [temp.dep.type]. Note that a NULL type is considered dependent. */
26448
26449 bool
26450 dependent_type_p (tree type)
26451 {
26452 /* If there are no template parameters in scope, then there can't be
26453 any dependent types. */
26454 if (!processing_template_decl)
26455 {
26456 /* If we are not processing a template, then nobody should be
26457 providing us with a dependent type. */
26458 gcc_assert (type);
26459 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26460 return false;
26461 }
26462
26463 /* If the type is NULL, we have not computed a type for the entity
26464 in question; in that case, the type is dependent. */
26465 if (!type)
26466 return true;
26467
26468 /* Erroneous types can be considered non-dependent. */
26469 if (type == error_mark_node)
26470 return false;
26471
26472 /* Getting here with global_type_node means we improperly called this
26473 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26474 gcc_checking_assert (type != global_type_node);
26475
26476 /* If we have not already computed the appropriate value for TYPE,
26477 do so now. */
26478 if (!TYPE_DEPENDENT_P_VALID (type))
26479 {
26480 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26481 TYPE_DEPENDENT_P_VALID (type) = 1;
26482 }
26483
26484 return TYPE_DEPENDENT_P (type);
26485 }
26486
26487 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26488 lookup. In other words, a dependent type that is not the current
26489 instantiation. */
26490
26491 bool
26492 dependent_scope_p (tree scope)
26493 {
26494 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26495 && !currently_open_class (scope));
26496 }
26497
26498 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26499 an unknown base of 'this' (and is therefore instantiation-dependent). */
26500
26501 static bool
26502 unknown_base_ref_p (tree t)
26503 {
26504 if (!current_class_ptr)
26505 return false;
26506
26507 tree mem = TREE_OPERAND (t, 1);
26508 if (shared_member_p (mem))
26509 return false;
26510
26511 tree cur = current_nonlambda_class_type ();
26512 if (!any_dependent_bases_p (cur))
26513 return false;
26514
26515 tree ctx = TREE_OPERAND (t, 0);
26516 if (DERIVED_FROM_P (ctx, cur))
26517 return false;
26518
26519 return true;
26520 }
26521
26522 /* T is a SCOPE_REF; return whether we need to consider it
26523 instantiation-dependent so that we can check access at instantiation
26524 time even though we know which member it resolves to. */
26525
26526 static bool
26527 instantiation_dependent_scope_ref_p (tree t)
26528 {
26529 if (DECL_P (TREE_OPERAND (t, 1))
26530 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26531 && !unknown_base_ref_p (t)
26532 && accessible_in_template_p (TREE_OPERAND (t, 0),
26533 TREE_OPERAND (t, 1)))
26534 return false;
26535 else
26536 return true;
26537 }
26538
26539 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26540 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26541 expression. */
26542
26543 /* Note that this predicate is not appropriate for general expressions;
26544 only constant expressions (that satisfy potential_constant_expression)
26545 can be tested for value dependence. */
26546
26547 bool
26548 value_dependent_expression_p (tree expression)
26549 {
26550 if (!processing_template_decl || expression == NULL_TREE)
26551 return false;
26552
26553 /* A type-dependent expression is also value-dependent. */
26554 if (type_dependent_expression_p (expression))
26555 return true;
26556
26557 switch (TREE_CODE (expression))
26558 {
26559 case BASELINK:
26560 /* A dependent member function of the current instantiation. */
26561 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26562
26563 case FUNCTION_DECL:
26564 /* A dependent member function of the current instantiation. */
26565 if (DECL_CLASS_SCOPE_P (expression)
26566 && dependent_type_p (DECL_CONTEXT (expression)))
26567 return true;
26568 break;
26569
26570 case IDENTIFIER_NODE:
26571 /* A name that has not been looked up -- must be dependent. */
26572 return true;
26573
26574 case TEMPLATE_PARM_INDEX:
26575 /* A non-type template parm. */
26576 return true;
26577
26578 case CONST_DECL:
26579 /* A non-type template parm. */
26580 if (DECL_TEMPLATE_PARM_P (expression))
26581 return true;
26582 return value_dependent_expression_p (DECL_INITIAL (expression));
26583
26584 case VAR_DECL:
26585 /* A constant with literal type and is initialized
26586 with an expression that is value-dependent. */
26587 if (DECL_DEPENDENT_INIT_P (expression)
26588 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26589 || TYPE_REF_P (TREE_TYPE (expression)))
26590 return true;
26591 if (DECL_HAS_VALUE_EXPR_P (expression))
26592 {
26593 tree value_expr = DECL_VALUE_EXPR (expression);
26594 if (value_dependent_expression_p (value_expr)
26595 /* __PRETTY_FUNCTION__ inside a template function is dependent
26596 on the name of the function. */
26597 || (DECL_PRETTY_FUNCTION_P (expression)
26598 /* It might be used in a template, but not a template
26599 function, in which case its DECL_VALUE_EXPR will be
26600 "top level". */
26601 && value_expr == error_mark_node))
26602 return true;
26603 }
26604 return false;
26605
26606 case DYNAMIC_CAST_EXPR:
26607 case STATIC_CAST_EXPR:
26608 case CONST_CAST_EXPR:
26609 case REINTERPRET_CAST_EXPR:
26610 case CAST_EXPR:
26611 case IMPLICIT_CONV_EXPR:
26612 /* These expressions are value-dependent if the type to which
26613 the cast occurs is dependent or the expression being casted
26614 is value-dependent. */
26615 {
26616 tree type = TREE_TYPE (expression);
26617
26618 if (dependent_type_p (type))
26619 return true;
26620
26621 /* A functional cast has a list of operands. */
26622 expression = TREE_OPERAND (expression, 0);
26623 if (!expression)
26624 {
26625 /* If there are no operands, it must be an expression such
26626 as "int()". This should not happen for aggregate types
26627 because it would form non-constant expressions. */
26628 gcc_assert (cxx_dialect >= cxx11
26629 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26630
26631 return false;
26632 }
26633
26634 if (TREE_CODE (expression) == TREE_LIST)
26635 return any_value_dependent_elements_p (expression);
26636
26637 return value_dependent_expression_p (expression);
26638 }
26639
26640 case SIZEOF_EXPR:
26641 if (SIZEOF_EXPR_TYPE_P (expression))
26642 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26643 /* FALLTHRU */
26644 case ALIGNOF_EXPR:
26645 case TYPEID_EXPR:
26646 /* A `sizeof' expression is value-dependent if the operand is
26647 type-dependent or is a pack expansion. */
26648 expression = TREE_OPERAND (expression, 0);
26649 if (PACK_EXPANSION_P (expression))
26650 return true;
26651 else if (TYPE_P (expression))
26652 return dependent_type_p (expression);
26653 return instantiation_dependent_uneval_expression_p (expression);
26654
26655 case AT_ENCODE_EXPR:
26656 /* An 'encode' expression is value-dependent if the operand is
26657 type-dependent. */
26658 expression = TREE_OPERAND (expression, 0);
26659 return dependent_type_p (expression);
26660
26661 case NOEXCEPT_EXPR:
26662 expression = TREE_OPERAND (expression, 0);
26663 return instantiation_dependent_uneval_expression_p (expression);
26664
26665 case SCOPE_REF:
26666 /* All instantiation-dependent expressions should also be considered
26667 value-dependent. */
26668 return instantiation_dependent_scope_ref_p (expression);
26669
26670 case COMPONENT_REF:
26671 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26672 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26673
26674 case NONTYPE_ARGUMENT_PACK:
26675 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26676 is value-dependent. */
26677 {
26678 tree values = ARGUMENT_PACK_ARGS (expression);
26679 int i, len = TREE_VEC_LENGTH (values);
26680
26681 for (i = 0; i < len; ++i)
26682 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26683 return true;
26684
26685 return false;
26686 }
26687
26688 case TRAIT_EXPR:
26689 {
26690 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26691
26692 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26693 return true;
26694
26695 if (!type2)
26696 return false;
26697
26698 if (TREE_CODE (type2) != TREE_LIST)
26699 return dependent_type_p (type2);
26700
26701 for (; type2; type2 = TREE_CHAIN (type2))
26702 if (dependent_type_p (TREE_VALUE (type2)))
26703 return true;
26704
26705 return false;
26706 }
26707
26708 case MODOP_EXPR:
26709 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26710 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26711
26712 case ARRAY_REF:
26713 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26714 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26715
26716 case ADDR_EXPR:
26717 {
26718 tree op = TREE_OPERAND (expression, 0);
26719 return (value_dependent_expression_p (op)
26720 || has_value_dependent_address (op));
26721 }
26722
26723 case REQUIRES_EXPR:
26724 /* Treat all requires-expressions as value-dependent so
26725 we don't try to fold them. */
26726 return true;
26727
26728 case TYPE_REQ:
26729 return dependent_type_p (TREE_OPERAND (expression, 0));
26730
26731 case CALL_EXPR:
26732 {
26733 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26734 return true;
26735 tree fn = get_callee_fndecl (expression);
26736 int i, nargs;
26737 nargs = call_expr_nargs (expression);
26738 for (i = 0; i < nargs; ++i)
26739 {
26740 tree op = CALL_EXPR_ARG (expression, i);
26741 /* In a call to a constexpr member function, look through the
26742 implicit ADDR_EXPR on the object argument so that it doesn't
26743 cause the call to be considered value-dependent. We also
26744 look through it in potential_constant_expression. */
26745 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26746 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26747 && TREE_CODE (op) == ADDR_EXPR)
26748 op = TREE_OPERAND (op, 0);
26749 if (value_dependent_expression_p (op))
26750 return true;
26751 }
26752 return false;
26753 }
26754
26755 case TEMPLATE_ID_EXPR:
26756 return concept_definition_p (TREE_OPERAND (expression, 0));
26757
26758 case CONSTRUCTOR:
26759 {
26760 unsigned ix;
26761 tree val;
26762 if (dependent_type_p (TREE_TYPE (expression)))
26763 return true;
26764 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26765 if (value_dependent_expression_p (val))
26766 return true;
26767 return false;
26768 }
26769
26770 case STMT_EXPR:
26771 /* Treat a GNU statement expression as dependent to avoid crashing
26772 under instantiate_non_dependent_expr; it can't be constant. */
26773 return true;
26774
26775 default:
26776 /* A constant expression is value-dependent if any subexpression is
26777 value-dependent. */
26778 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26779 {
26780 case tcc_reference:
26781 case tcc_unary:
26782 case tcc_comparison:
26783 case tcc_binary:
26784 case tcc_expression:
26785 case tcc_vl_exp:
26786 {
26787 int i, len = cp_tree_operand_length (expression);
26788
26789 for (i = 0; i < len; i++)
26790 {
26791 tree t = TREE_OPERAND (expression, i);
26792
26793 /* In some cases, some of the operands may be missing.
26794 (For example, in the case of PREDECREMENT_EXPR, the
26795 amount to increment by may be missing.) That doesn't
26796 make the expression dependent. */
26797 if (t && value_dependent_expression_p (t))
26798 return true;
26799 }
26800 }
26801 break;
26802 default:
26803 break;
26804 }
26805 break;
26806 }
26807
26808 /* The expression is not value-dependent. */
26809 return false;
26810 }
26811
26812 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26813 [temp.dep.expr]. Note that an expression with no type is
26814 considered dependent. Other parts of the compiler arrange for an
26815 expression with type-dependent subexpressions to have no type, so
26816 this function doesn't have to be fully recursive. */
26817
26818 bool
26819 type_dependent_expression_p (tree expression)
26820 {
26821 if (!processing_template_decl)
26822 return false;
26823
26824 if (expression == NULL_TREE || expression == error_mark_node)
26825 return false;
26826
26827 STRIP_ANY_LOCATION_WRAPPER (expression);
26828
26829 /* An unresolved name is always dependent. */
26830 if (identifier_p (expression)
26831 || TREE_CODE (expression) == USING_DECL
26832 || TREE_CODE (expression) == WILDCARD_DECL)
26833 return true;
26834
26835 /* A lambda-expression in template context is dependent. dependent_type_p is
26836 true for a lambda in the scope of a class or function template, but that
26837 doesn't cover all template contexts, like a default template argument. */
26838 if (TREE_CODE (expression) == LAMBDA_EXPR)
26839 return true;
26840
26841 /* A fold expression is type-dependent. */
26842 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26843 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26844 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26845 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26846 return true;
26847
26848 /* Some expression forms are never type-dependent. */
26849 if (TREE_CODE (expression) == SIZEOF_EXPR
26850 || TREE_CODE (expression) == ALIGNOF_EXPR
26851 || TREE_CODE (expression) == AT_ENCODE_EXPR
26852 || TREE_CODE (expression) == NOEXCEPT_EXPR
26853 || TREE_CODE (expression) == TRAIT_EXPR
26854 || TREE_CODE (expression) == TYPEID_EXPR
26855 || TREE_CODE (expression) == DELETE_EXPR
26856 || TREE_CODE (expression) == VEC_DELETE_EXPR
26857 || TREE_CODE (expression) == THROW_EXPR
26858 || TREE_CODE (expression) == REQUIRES_EXPR)
26859 return false;
26860
26861 /* The types of these expressions depends only on the type to which
26862 the cast occurs. */
26863 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26864 || TREE_CODE (expression) == STATIC_CAST_EXPR
26865 || TREE_CODE (expression) == CONST_CAST_EXPR
26866 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26867 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26868 || TREE_CODE (expression) == CAST_EXPR)
26869 return dependent_type_p (TREE_TYPE (expression));
26870
26871 /* The types of these expressions depends only on the type created
26872 by the expression. */
26873 if (TREE_CODE (expression) == NEW_EXPR
26874 || TREE_CODE (expression) == VEC_NEW_EXPR)
26875 {
26876 /* For NEW_EXPR tree nodes created inside a template, either
26877 the object type itself or a TREE_LIST may appear as the
26878 operand 1. */
26879 tree type = TREE_OPERAND (expression, 1);
26880 if (TREE_CODE (type) == TREE_LIST)
26881 /* This is an array type. We need to check array dimensions
26882 as well. */
26883 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26884 || value_dependent_expression_p
26885 (TREE_OPERAND (TREE_VALUE (type), 1));
26886 /* Array type whose dimension has to be deduced. */
26887 else if (TREE_CODE (type) == ARRAY_TYPE
26888 && TREE_OPERAND (expression, 2) == NULL_TREE)
26889 return true;
26890 else
26891 return dependent_type_p (type);
26892 }
26893
26894 if (TREE_CODE (expression) == SCOPE_REF)
26895 {
26896 tree scope = TREE_OPERAND (expression, 0);
26897 tree name = TREE_OPERAND (expression, 1);
26898
26899 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26900 contains an identifier associated by name lookup with one or more
26901 declarations declared with a dependent type, or...a
26902 nested-name-specifier or qualified-id that names a member of an
26903 unknown specialization. */
26904 return (type_dependent_expression_p (name)
26905 || dependent_scope_p (scope));
26906 }
26907
26908 if (TREE_CODE (expression) == TEMPLATE_DECL
26909 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26910 return uses_outer_template_parms (expression);
26911
26912 if (TREE_CODE (expression) == STMT_EXPR)
26913 expression = stmt_expr_value_expr (expression);
26914
26915 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26916 {
26917 tree elt;
26918 unsigned i;
26919
26920 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26921 {
26922 if (type_dependent_expression_p (elt))
26923 return true;
26924 }
26925 return false;
26926 }
26927
26928 /* A static data member of the current instantiation with incomplete
26929 array type is type-dependent, as the definition and specializations
26930 can have different bounds. */
26931 if (VAR_P (expression)
26932 && DECL_CLASS_SCOPE_P (expression)
26933 && dependent_type_p (DECL_CONTEXT (expression))
26934 && VAR_HAD_UNKNOWN_BOUND (expression))
26935 return true;
26936
26937 /* An array of unknown bound depending on a variadic parameter, eg:
26938
26939 template<typename... Args>
26940 void foo (Args... args)
26941 {
26942 int arr[] = { args... };
26943 }
26944
26945 template<int... vals>
26946 void bar ()
26947 {
26948 int arr[] = { vals... };
26949 }
26950
26951 If the array has no length and has an initializer, it must be that
26952 we couldn't determine its length in cp_complete_array_type because
26953 it is dependent. */
26954 if (VAR_P (expression)
26955 && TREE_TYPE (expression) != NULL_TREE
26956 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26957 && !TYPE_DOMAIN (TREE_TYPE (expression))
26958 && DECL_INITIAL (expression))
26959 return true;
26960
26961 /* A function or variable template-id is type-dependent if it has any
26962 dependent template arguments. */
26963 if (VAR_OR_FUNCTION_DECL_P (expression)
26964 && DECL_LANG_SPECIFIC (expression)
26965 && DECL_TEMPLATE_INFO (expression))
26966 {
26967 /* Consider the innermost template arguments, since those are the ones
26968 that come from the template-id; the template arguments for the
26969 enclosing class do not make it type-dependent unless they are used in
26970 the type of the decl. */
26971 if (instantiates_primary_template_p (expression)
26972 && (any_dependent_template_arguments_p
26973 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26974 return true;
26975 }
26976
26977 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26978 type-dependent. Checking this is important for functions with auto return
26979 type, which looks like a dependent type. */
26980 if (TREE_CODE (expression) == FUNCTION_DECL
26981 && !(DECL_CLASS_SCOPE_P (expression)
26982 && dependent_type_p (DECL_CONTEXT (expression)))
26983 && !(DECL_LANG_SPECIFIC (expression)
26984 && DECL_FRIEND_P (expression)
26985 && (!DECL_FRIEND_CONTEXT (expression)
26986 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26987 && !DECL_LOCAL_DECL_P (expression))
26988 {
26989 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26990 || undeduced_auto_decl (expression));
26991 return false;
26992 }
26993
26994 /* Always dependent, on the number of arguments if nothing else. */
26995 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26996 return true;
26997
26998 if (TREE_TYPE (expression) == unknown_type_node)
26999 {
27000 if (TREE_CODE (expression) == ADDR_EXPR)
27001 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27002 if (TREE_CODE (expression) == COMPONENT_REF
27003 || TREE_CODE (expression) == OFFSET_REF)
27004 {
27005 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27006 return true;
27007 expression = TREE_OPERAND (expression, 1);
27008 if (identifier_p (expression))
27009 return false;
27010 }
27011 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27012 if (TREE_CODE (expression) == SCOPE_REF)
27013 return false;
27014
27015 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27016 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27017 || TREE_CODE (expression) == CO_YIELD_EXPR)
27018 return true;
27019
27020 if (BASELINK_P (expression))
27021 {
27022 if (BASELINK_OPTYPE (expression)
27023 && dependent_type_p (BASELINK_OPTYPE (expression)))
27024 return true;
27025 expression = BASELINK_FUNCTIONS (expression);
27026 }
27027
27028 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27029 {
27030 if (any_dependent_template_arguments_p
27031 (TREE_OPERAND (expression, 1)))
27032 return true;
27033 expression = TREE_OPERAND (expression, 0);
27034 if (identifier_p (expression))
27035 return true;
27036 }
27037
27038 gcc_assert (OVL_P (expression));
27039
27040 for (lkp_iterator iter (expression); iter; ++iter)
27041 if (type_dependent_expression_p (*iter))
27042 return true;
27043
27044 return false;
27045 }
27046
27047 /* The type of a non-type template parm declared with a placeholder type
27048 depends on the corresponding template argument, even though
27049 placeholders are not normally considered dependent. */
27050 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27051 && is_auto (TREE_TYPE (expression)))
27052 return true;
27053
27054 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27055
27056 /* Dependent type attributes might not have made it from the decl to
27057 the type yet. */
27058 if (DECL_P (expression)
27059 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27060 return true;
27061
27062 return (dependent_type_p (TREE_TYPE (expression)));
27063 }
27064
27065 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27066 type-dependent if the expression refers to a member of the current
27067 instantiation and the type of the referenced member is dependent, or the
27068 class member access expression refers to a member of an unknown
27069 specialization.
27070
27071 This function returns true if the OBJECT in such a class member access
27072 expression is of an unknown specialization. */
27073
27074 bool
27075 type_dependent_object_expression_p (tree object)
27076 {
27077 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27078 dependent. */
27079 if (TREE_CODE (object) == IDENTIFIER_NODE)
27080 return true;
27081 tree scope = TREE_TYPE (object);
27082 return (!scope || dependent_scope_p (scope));
27083 }
27084
27085 /* walk_tree callback function for instantiation_dependent_expression_p,
27086 below. Returns non-zero if a dependent subexpression is found. */
27087
27088 static tree
27089 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27090 void * /*data*/)
27091 {
27092 if (TYPE_P (*tp))
27093 {
27094 /* We don't have to worry about decltype currently because decltype
27095 of an instantiation-dependent expr is a dependent type. This
27096 might change depending on the resolution of DR 1172. */
27097 *walk_subtrees = false;
27098 return NULL_TREE;
27099 }
27100 enum tree_code code = TREE_CODE (*tp);
27101 switch (code)
27102 {
27103 /* Don't treat an argument list as dependent just because it has no
27104 TREE_TYPE. */
27105 case TREE_LIST:
27106 case TREE_VEC:
27107 case NONTYPE_ARGUMENT_PACK:
27108 return NULL_TREE;
27109
27110 case TEMPLATE_PARM_INDEX:
27111 if (dependent_type_p (TREE_TYPE (*tp)))
27112 return *tp;
27113 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27114 return *tp;
27115 /* We'll check value-dependence separately. */
27116 return NULL_TREE;
27117
27118 /* Handle expressions with type operands. */
27119 case SIZEOF_EXPR:
27120 case ALIGNOF_EXPR:
27121 case TYPEID_EXPR:
27122 case AT_ENCODE_EXPR:
27123 {
27124 tree op = TREE_OPERAND (*tp, 0);
27125 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27126 op = TREE_TYPE (op);
27127 if (TYPE_P (op))
27128 {
27129 if (dependent_type_p (op))
27130 return *tp;
27131 else
27132 {
27133 *walk_subtrees = false;
27134 return NULL_TREE;
27135 }
27136 }
27137 break;
27138 }
27139
27140 case COMPONENT_REF:
27141 if (identifier_p (TREE_OPERAND (*tp, 1)))
27142 /* In a template, finish_class_member_access_expr creates a
27143 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27144 type-dependent, so that we can check access control at
27145 instantiation time (PR 42277). See also Core issue 1273. */
27146 return *tp;
27147 break;
27148
27149 case SCOPE_REF:
27150 if (instantiation_dependent_scope_ref_p (*tp))
27151 return *tp;
27152 else
27153 break;
27154
27155 /* Treat statement-expressions as dependent. */
27156 case BIND_EXPR:
27157 return *tp;
27158
27159 /* Treat requires-expressions as dependent. */
27160 case REQUIRES_EXPR:
27161 return *tp;
27162
27163 case CALL_EXPR:
27164 /* Treat concept checks as dependent. */
27165 if (concept_check_p (*tp))
27166 return *tp;
27167 break;
27168
27169 case TEMPLATE_ID_EXPR:
27170 /* Treat concept checks as dependent. */
27171 if (concept_check_p (*tp))
27172 return *tp;
27173 break;
27174
27175 case CONSTRUCTOR:
27176 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27177 return *tp;
27178 break;
27179
27180 default:
27181 break;
27182 }
27183
27184 if (type_dependent_expression_p (*tp))
27185 return *tp;
27186 else
27187 return NULL_TREE;
27188 }
27189
27190 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27191 sense defined by the ABI:
27192
27193 "An expression is instantiation-dependent if it is type-dependent
27194 or value-dependent, or it has a subexpression that is type-dependent
27195 or value-dependent."
27196
27197 Except don't actually check value-dependence for unevaluated expressions,
27198 because in sizeof(i) we don't care about the value of i. Checking
27199 type-dependence will in turn check value-dependence of array bounds/template
27200 arguments as needed. */
27201
27202 bool
27203 instantiation_dependent_uneval_expression_p (tree expression)
27204 {
27205 tree result;
27206
27207 if (!processing_template_decl)
27208 return false;
27209
27210 if (expression == error_mark_node)
27211 return false;
27212
27213 result = cp_walk_tree_without_duplicates (&expression,
27214 instantiation_dependent_r, NULL);
27215 return result != NULL_TREE;
27216 }
27217
27218 /* As above, but also check value-dependence of the expression as a whole. */
27219
27220 bool
27221 instantiation_dependent_expression_p (tree expression)
27222 {
27223 return (instantiation_dependent_uneval_expression_p (expression)
27224 || value_dependent_expression_p (expression));
27225 }
27226
27227 /* Like type_dependent_expression_p, but it also works while not processing
27228 a template definition, i.e. during substitution or mangling. */
27229
27230 bool
27231 type_dependent_expression_p_push (tree expr)
27232 {
27233 bool b;
27234 ++processing_template_decl;
27235 b = type_dependent_expression_p (expr);
27236 --processing_template_decl;
27237 return b;
27238 }
27239
27240 /* Returns TRUE if ARGS contains a type-dependent expression. */
27241
27242 bool
27243 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27244 {
27245 unsigned int i;
27246 tree arg;
27247
27248 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27249 {
27250 if (type_dependent_expression_p (arg))
27251 return true;
27252 }
27253 return false;
27254 }
27255
27256 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27257 expressions) contains any type-dependent expressions. */
27258
27259 bool
27260 any_type_dependent_elements_p (const_tree list)
27261 {
27262 for (; list; list = TREE_CHAIN (list))
27263 if (type_dependent_expression_p (TREE_VALUE (list)))
27264 return true;
27265
27266 return false;
27267 }
27268
27269 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27270 expressions) contains any value-dependent expressions. */
27271
27272 bool
27273 any_value_dependent_elements_p (const_tree list)
27274 {
27275 for (; list; list = TREE_CHAIN (list))
27276 if (value_dependent_expression_p (TREE_VALUE (list)))
27277 return true;
27278
27279 return false;
27280 }
27281
27282 /* Returns TRUE if the ARG (a template argument) is dependent. */
27283
27284 bool
27285 dependent_template_arg_p (tree arg)
27286 {
27287 if (!processing_template_decl)
27288 return false;
27289
27290 /* Assume a template argument that was wrongly written by the user
27291 is dependent. This is consistent with what
27292 any_dependent_template_arguments_p [that calls this function]
27293 does. */
27294 if (!arg || arg == error_mark_node)
27295 return true;
27296
27297 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27298 arg = argument_pack_select_arg (arg);
27299
27300 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27301 return true;
27302 if (TREE_CODE (arg) == TEMPLATE_DECL)
27303 {
27304 if (DECL_TEMPLATE_PARM_P (arg))
27305 return true;
27306 /* A member template of a dependent class is not necessarily
27307 type-dependent, but it is a dependent template argument because it
27308 will be a member of an unknown specialization to that template. */
27309 tree scope = CP_DECL_CONTEXT (arg);
27310 return TYPE_P (scope) && dependent_type_p (scope);
27311 }
27312 else if (ARGUMENT_PACK_P (arg))
27313 {
27314 tree args = ARGUMENT_PACK_ARGS (arg);
27315 int i, len = TREE_VEC_LENGTH (args);
27316 for (i = 0; i < len; ++i)
27317 {
27318 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27319 return true;
27320 }
27321
27322 return false;
27323 }
27324 else if (TYPE_P (arg))
27325 return dependent_type_p (arg);
27326 else
27327 return value_dependent_expression_p (arg);
27328 }
27329
27330 /* Returns true if ARGS (a collection of template arguments) contains
27331 any types that require structural equality testing. */
27332
27333 bool
27334 any_template_arguments_need_structural_equality_p (tree args)
27335 {
27336 int i;
27337 int j;
27338
27339 if (!args)
27340 return false;
27341 if (args == error_mark_node)
27342 return true;
27343
27344 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27345 {
27346 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27347 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27348 {
27349 tree arg = TREE_VEC_ELT (level, j);
27350 tree packed_args = NULL_TREE;
27351 int k, len = 1;
27352
27353 if (ARGUMENT_PACK_P (arg))
27354 {
27355 /* Look inside the argument pack. */
27356 packed_args = ARGUMENT_PACK_ARGS (arg);
27357 len = TREE_VEC_LENGTH (packed_args);
27358 }
27359
27360 for (k = 0; k < len; ++k)
27361 {
27362 if (packed_args)
27363 arg = TREE_VEC_ELT (packed_args, k);
27364
27365 if (error_operand_p (arg))
27366 return true;
27367 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27368 continue;
27369 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27370 return true;
27371 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27372 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27373 return true;
27374 }
27375 }
27376 }
27377
27378 return false;
27379 }
27380
27381 /* Returns true if ARGS (a collection of template arguments) contains
27382 any dependent arguments. */
27383
27384 bool
27385 any_dependent_template_arguments_p (const_tree args)
27386 {
27387 int i;
27388 int j;
27389
27390 if (!args)
27391 return false;
27392 if (args == error_mark_node)
27393 return true;
27394
27395 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27396 {
27397 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27398 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27399 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27400 return true;
27401 }
27402
27403 return false;
27404 }
27405
27406 /* Returns true if ARGS contains any errors. */
27407
27408 bool
27409 any_erroneous_template_args_p (const_tree args)
27410 {
27411 int i;
27412 int j;
27413
27414 if (args == error_mark_node)
27415 return true;
27416
27417 if (args && TREE_CODE (args) != TREE_VEC)
27418 {
27419 if (tree ti = get_template_info (args))
27420 args = TI_ARGS (ti);
27421 else
27422 args = NULL_TREE;
27423 }
27424
27425 if (!args)
27426 return false;
27427
27428 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27429 {
27430 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27431 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27432 if (error_operand_p (TREE_VEC_ELT (level, j)))
27433 return true;
27434 }
27435
27436 return false;
27437 }
27438
27439 /* Returns TRUE if the template TMPL is type-dependent. */
27440
27441 bool
27442 dependent_template_p (tree tmpl)
27443 {
27444 if (TREE_CODE (tmpl) == OVERLOAD)
27445 {
27446 for (lkp_iterator iter (tmpl); iter; ++iter)
27447 if (dependent_template_p (*iter))
27448 return true;
27449 return false;
27450 }
27451
27452 /* Template template parameters are dependent. */
27453 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27454 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27455 return true;
27456 /* So are names that have not been looked up. */
27457 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27458 return true;
27459 return false;
27460 }
27461
27462 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27463
27464 bool
27465 dependent_template_id_p (tree tmpl, tree args)
27466 {
27467 return (dependent_template_p (tmpl)
27468 || any_dependent_template_arguments_p (args));
27469 }
27470
27471 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27472 are dependent. */
27473
27474 bool
27475 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27476 {
27477 int i;
27478
27479 if (!processing_template_decl)
27480 return false;
27481
27482 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27483 {
27484 tree decl = TREE_VEC_ELT (declv, i);
27485 tree init = TREE_VEC_ELT (initv, i);
27486 tree cond = TREE_VEC_ELT (condv, i);
27487 tree incr = TREE_VEC_ELT (incrv, i);
27488
27489 if (type_dependent_expression_p (decl)
27490 || TREE_CODE (decl) == SCOPE_REF)
27491 return true;
27492
27493 if (init && type_dependent_expression_p (init))
27494 return true;
27495
27496 if (cond == global_namespace)
27497 return true;
27498
27499 if (type_dependent_expression_p (cond))
27500 return true;
27501
27502 if (COMPARISON_CLASS_P (cond)
27503 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27504 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27505 return true;
27506
27507 if (TREE_CODE (incr) == MODOP_EXPR)
27508 {
27509 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27510 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27511 return true;
27512 }
27513 else if (type_dependent_expression_p (incr))
27514 return true;
27515 else if (TREE_CODE (incr) == MODIFY_EXPR)
27516 {
27517 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27518 return true;
27519 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27520 {
27521 tree t = TREE_OPERAND (incr, 1);
27522 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27523 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27524 return true;
27525
27526 /* If this loop has a class iterator with != comparison
27527 with increment other than i++/++i/i--/--i, make sure the
27528 increment is constant. */
27529 if (CLASS_TYPE_P (TREE_TYPE (decl))
27530 && TREE_CODE (cond) == NE_EXPR)
27531 {
27532 if (TREE_OPERAND (t, 0) == decl)
27533 t = TREE_OPERAND (t, 1);
27534 else
27535 t = TREE_OPERAND (t, 0);
27536 if (TREE_CODE (t) != INTEGER_CST)
27537 return true;
27538 }
27539 }
27540 }
27541 }
27542
27543 return false;
27544 }
27545
27546 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27547 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27548 no such TYPE can be found. Note that this function peers inside
27549 uninstantiated templates and therefore should be used only in
27550 extremely limited situations. ONLY_CURRENT_P restricts this
27551 peering to the currently open classes hierarchy (which is required
27552 when comparing types). */
27553
27554 tree
27555 resolve_typename_type (tree type, bool only_current_p)
27556 {
27557 tree scope;
27558 tree name;
27559 tree decl;
27560 int quals;
27561 tree pushed_scope;
27562 tree result;
27563
27564 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27565
27566 scope = TYPE_CONTEXT (type);
27567 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27568 gcc_checking_assert (uses_template_parms (scope));
27569
27570 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27571 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27572 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27573 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27574 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27575 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27576 the TYPENAME_TYPE instead, we avoid messing up with a possible
27577 typedef variant case. */
27578 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27579
27580 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27581 it first before we can figure out what NAME refers to. */
27582 if (TREE_CODE (scope) == TYPENAME_TYPE)
27583 {
27584 if (TYPENAME_IS_RESOLVING_P (scope))
27585 /* Given a class template A with a dependent base with nested type C,
27586 typedef typename A::C::C C will land us here, as trying to resolve
27587 the initial A::C leads to the local C typedef, which leads back to
27588 A::C::C. So we break the recursion now. */
27589 return type;
27590 else
27591 scope = resolve_typename_type (scope, only_current_p);
27592 }
27593 /* If we don't know what SCOPE refers to, then we cannot resolve the
27594 TYPENAME_TYPE. */
27595 if (!CLASS_TYPE_P (scope))
27596 return type;
27597 /* If this is a typedef, we don't want to look inside (c++/11987). */
27598 if (typedef_variant_p (type))
27599 return type;
27600 /* If SCOPE isn't the template itself, it will not have a valid
27601 TYPE_FIELDS list. */
27602 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27603 /* scope is either the template itself or a compatible instantiation
27604 like X<T>, so look up the name in the original template. */
27605 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27606 /* If scope has no fields, it can't be a current instantiation. Check this
27607 before currently_open_class to avoid infinite recursion (71515). */
27608 if (!TYPE_FIELDS (scope))
27609 return type;
27610 /* If the SCOPE is not the current instantiation, there's no reason
27611 to look inside it. */
27612 if (only_current_p && !currently_open_class (scope))
27613 return type;
27614 /* Enter the SCOPE so that name lookup will be resolved as if we
27615 were in the class definition. In particular, SCOPE will no
27616 longer be considered a dependent type. */
27617 pushed_scope = push_scope (scope);
27618 /* Look up the declaration. */
27619 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27620 tf_warning_or_error);
27621
27622 result = NULL_TREE;
27623
27624 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27625 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27626 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27627 if (!decl)
27628 /*nop*/;
27629 else if (identifier_p (fullname)
27630 && TREE_CODE (decl) == TYPE_DECL)
27631 {
27632 result = TREE_TYPE (decl);
27633 if (result == error_mark_node)
27634 result = NULL_TREE;
27635 }
27636 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27637 && DECL_CLASS_TEMPLATE_P (decl))
27638 {
27639 /* Obtain the template and the arguments. */
27640 tree tmpl = TREE_OPERAND (fullname, 0);
27641 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27642 {
27643 /* We get here with a plain identifier because a previous tentative
27644 parse of the nested-name-specifier as part of a ptr-operator saw
27645 ::template X<A>. The use of ::template is necessary in a
27646 ptr-operator, but wrong in a declarator-id.
27647
27648 [temp.names]: In a qualified-id of a declarator-id, the keyword
27649 template shall not appear at the top level. */
27650 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27651 "keyword %<template%> not allowed in declarator-id");
27652 tmpl = decl;
27653 }
27654 tree args = TREE_OPERAND (fullname, 1);
27655 /* Instantiate the template. */
27656 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27657 /*entering_scope=*/true,
27658 tf_error | tf_user);
27659 if (result == error_mark_node)
27660 result = NULL_TREE;
27661 }
27662
27663 /* Leave the SCOPE. */
27664 if (pushed_scope)
27665 pop_scope (pushed_scope);
27666
27667 /* If we failed to resolve it, return the original typename. */
27668 if (!result)
27669 return type;
27670
27671 /* If lookup found a typename type, resolve that too. */
27672 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27673 {
27674 /* Ill-formed programs can cause infinite recursion here, so we
27675 must catch that. */
27676 TYPENAME_IS_RESOLVING_P (result) = 1;
27677 result = resolve_typename_type (result, only_current_p);
27678 TYPENAME_IS_RESOLVING_P (result) = 0;
27679 }
27680
27681 /* Qualify the resulting type. */
27682 quals = cp_type_quals (type);
27683 if (quals)
27684 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27685
27686 return result;
27687 }
27688
27689 /* EXPR is an expression which is not type-dependent. Return a proxy
27690 for EXPR that can be used to compute the types of larger
27691 expressions containing EXPR. */
27692
27693 tree
27694 build_non_dependent_expr (tree expr)
27695 {
27696 tree orig_expr = expr;
27697 tree inner_expr;
27698
27699 /* When checking, try to get a constant value for all non-dependent
27700 expressions in order to expose bugs in *_dependent_expression_p
27701 and constexpr. This can affect code generation, see PR70704, so
27702 only do this for -fchecking=2. */
27703 if (flag_checking > 1
27704 && cxx_dialect >= cxx11
27705 /* Don't do this during nsdmi parsing as it can lead to
27706 unexpected recursive instantiations. */
27707 && !parsing_nsdmi ()
27708 /* Don't do this during concept processing either and for
27709 the same reason. */
27710 && !processing_constraint_expression_p ())
27711 fold_non_dependent_expr (expr, tf_none);
27712
27713 STRIP_ANY_LOCATION_WRAPPER (expr);
27714
27715 /* Preserve OVERLOADs; the functions must be available to resolve
27716 types. */
27717 inner_expr = expr;
27718 if (TREE_CODE (inner_expr) == STMT_EXPR)
27719 inner_expr = stmt_expr_value_expr (inner_expr);
27720 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27721 inner_expr = TREE_OPERAND (inner_expr, 0);
27722 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27723 inner_expr = TREE_OPERAND (inner_expr, 1);
27724 if (is_overloaded_fn (inner_expr)
27725 || TREE_CODE (inner_expr) == OFFSET_REF)
27726 return orig_expr;
27727 /* There is no need to return a proxy for a variable or enumerator. */
27728 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27729 return orig_expr;
27730 /* Preserve string constants; conversions from string constants to
27731 "char *" are allowed, even though normally a "const char *"
27732 cannot be used to initialize a "char *". */
27733 if (TREE_CODE (expr) == STRING_CST)
27734 return orig_expr;
27735 /* Preserve void and arithmetic constants, as an optimization -- there is no
27736 reason to create a new node. */
27737 if (TREE_CODE (expr) == VOID_CST
27738 || TREE_CODE (expr) == INTEGER_CST
27739 || TREE_CODE (expr) == REAL_CST)
27740 return orig_expr;
27741 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27742 There is at least one place where we want to know that a
27743 particular expression is a throw-expression: when checking a ?:
27744 expression, there are special rules if the second or third
27745 argument is a throw-expression. */
27746 if (TREE_CODE (expr) == THROW_EXPR)
27747 return orig_expr;
27748
27749 /* Don't wrap an initializer list, we need to be able to look inside. */
27750 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27751 return orig_expr;
27752
27753 /* Don't wrap a dummy object, we need to be able to test for it. */
27754 if (is_dummy_object (expr))
27755 return orig_expr;
27756
27757 if (TREE_CODE (expr) == COND_EXPR)
27758 return build3 (COND_EXPR,
27759 TREE_TYPE (expr),
27760 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27761 (TREE_OPERAND (expr, 1)
27762 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27763 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27764 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27765 if (TREE_CODE (expr) == COMPOUND_EXPR
27766 && !COMPOUND_EXPR_OVERLOADED (expr))
27767 return build2 (COMPOUND_EXPR,
27768 TREE_TYPE (expr),
27769 TREE_OPERAND (expr, 0),
27770 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27771
27772 /* If the type is unknown, it can't really be non-dependent */
27773 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27774
27775 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27776 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27777 TREE_TYPE (expr), expr);
27778 }
27779
27780 /* ARGS is a vector of expressions as arguments to a function call.
27781 Replace the arguments with equivalent non-dependent expressions.
27782 This modifies ARGS in place. */
27783
27784 void
27785 make_args_non_dependent (vec<tree, va_gc> *args)
27786 {
27787 unsigned int ix;
27788 tree arg;
27789
27790 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27791 {
27792 tree newarg = build_non_dependent_expr (arg);
27793 if (newarg != arg)
27794 (*args)[ix] = newarg;
27795 }
27796 }
27797
27798 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27799 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27800 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27801
27802 static tree
27803 make_auto_1 (tree name, bool set_canonical)
27804 {
27805 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27806 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27807 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27808 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27809 (0, processing_template_decl + 1, processing_template_decl + 1,
27810 TYPE_NAME (au), NULL_TREE);
27811 if (set_canonical)
27812 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27813 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27814 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27815 if (name == decltype_auto_identifier)
27816 AUTO_IS_DECLTYPE (au) = true;
27817
27818 return au;
27819 }
27820
27821 tree
27822 make_decltype_auto (void)
27823 {
27824 return make_auto_1 (decltype_auto_identifier, true);
27825 }
27826
27827 tree
27828 make_auto (void)
27829 {
27830 return make_auto_1 (auto_identifier, true);
27831 }
27832
27833 /* Return a C++17 deduction placeholder for class template TMPL. */
27834
27835 tree
27836 make_template_placeholder (tree tmpl)
27837 {
27838 tree t = make_auto_1 (auto_identifier, false);
27839 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27840 /* Our canonical type depends on the placeholder. */
27841 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27842 return t;
27843 }
27844
27845 /* True iff T is a C++17 class template deduction placeholder. */
27846
27847 bool
27848 template_placeholder_p (tree t)
27849 {
27850 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27851 }
27852
27853 /* Make a "constrained auto" type-specifier. This is an auto or
27854 decltype(auto) type with constraints that must be associated after
27855 deduction. The constraint is formed from the given concept CON
27856 and its optional sequence of template arguments ARGS.
27857
27858 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27859
27860 static tree
27861 make_constrained_placeholder_type (tree type, tree con, tree args)
27862 {
27863 /* Build the constraint. */
27864 tree tmpl = DECL_TI_TEMPLATE (con);
27865 tree expr = tmpl;
27866 if (TREE_CODE (con) == FUNCTION_DECL)
27867 expr = ovl_make (tmpl);
27868 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27869
27870 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27871
27872 /* Our canonical type depends on the constraint. */
27873 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27874
27875 /* Attach the constraint to the type declaration. */
27876 return TYPE_NAME (type);
27877 }
27878
27879 /* Make a "constrained auto" type-specifier. */
27880
27881 tree
27882 make_constrained_auto (tree con, tree args)
27883 {
27884 tree type = make_auto_1 (auto_identifier, false);
27885 return make_constrained_placeholder_type (type, con, args);
27886 }
27887
27888 /* Make a "constrained decltype(auto)" type-specifier. */
27889
27890 tree
27891 make_constrained_decltype_auto (tree con, tree args)
27892 {
27893 tree type = make_auto_1 (decltype_auto_identifier, false);
27894 return make_constrained_placeholder_type (type, con, args);
27895 }
27896
27897 /* Build and return a concept definition. Like other templates, the
27898 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27899 the TEMPLATE_DECL. */
27900
27901 tree
27902 finish_concept_definition (cp_expr id, tree init)
27903 {
27904 gcc_assert (identifier_p (id));
27905 gcc_assert (processing_template_decl);
27906
27907 location_t loc = id.get_location();
27908
27909 /* A concept-definition shall not have associated constraints. */
27910 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27911 {
27912 error_at (loc, "a concept cannot be constrained");
27913 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27914 }
27915
27916 /* A concept-definition shall appear in namespace scope. Templates
27917 aren't allowed in block scope, so we only need to check for class
27918 scope. */
27919 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27920 {
27921 error_at (loc, "concept %qE not in namespace scope", *id);
27922 return error_mark_node;
27923 }
27924
27925 /* Initially build the concept declaration; its type is bool. */
27926 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27927 DECL_CONTEXT (decl) = current_scope ();
27928 DECL_INITIAL (decl) = init;
27929
27930 /* Push the enclosing template. */
27931 return push_template_decl (decl);
27932 }
27933
27934 /* Given type ARG, return std::initializer_list<ARG>. */
27935
27936 static tree
27937 listify (tree arg)
27938 {
27939 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27940
27941 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27942 {
27943 gcc_rich_location richloc (input_location);
27944 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27945 error_at (&richloc,
27946 "deducing from brace-enclosed initializer list"
27947 " requires %<#include <initializer_list>%>");
27948
27949 return error_mark_node;
27950 }
27951 tree argvec = make_tree_vec (1);
27952 TREE_VEC_ELT (argvec, 0) = arg;
27953
27954 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27955 NULL_TREE, 0, tf_warning_or_error);
27956 }
27957
27958 /* Replace auto in TYPE with std::initializer_list<auto>. */
27959
27960 static tree
27961 listify_autos (tree type, tree auto_node)
27962 {
27963 tree init_auto = listify (strip_top_quals (auto_node));
27964 tree argvec = make_tree_vec (1);
27965 TREE_VEC_ELT (argvec, 0) = init_auto;
27966 if (processing_template_decl)
27967 argvec = add_to_template_args (current_template_args (), argvec);
27968 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27969 }
27970
27971 /* Hash traits for hashing possibly constrained 'auto'
27972 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27973
27974 struct auto_hash : default_hash_traits<tree>
27975 {
27976 static inline hashval_t hash (tree);
27977 static inline bool equal (tree, tree);
27978 };
27979
27980 /* Hash the 'auto' T. */
27981
27982 inline hashval_t
27983 auto_hash::hash (tree t)
27984 {
27985 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27986 /* Matching constrained-type-specifiers denote the same template
27987 parameter, so hash the constraint. */
27988 return hash_placeholder_constraint (c);
27989 else
27990 /* But unconstrained autos are all separate, so just hash the pointer. */
27991 return iterative_hash_object (t, 0);
27992 }
27993
27994 /* Compare two 'auto's. */
27995
27996 inline bool
27997 auto_hash::equal (tree t1, tree t2)
27998 {
27999 if (t1 == t2)
28000 return true;
28001
28002 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28003 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28004
28005 /* Two unconstrained autos are distinct. */
28006 if (!c1 || !c2)
28007 return false;
28008
28009 return equivalent_placeholder_constraints (c1, c2);
28010 }
28011
28012 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28013 constrained) auto, add it to the vector. */
28014
28015 static int
28016 extract_autos_r (tree t, void *data)
28017 {
28018 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28019 if (is_auto (t))
28020 {
28021 /* All the autos were built with index 0; fix that up now. */
28022 tree *p = hash.find_slot (t, INSERT);
28023 unsigned idx;
28024 if (*p)
28025 /* If this is a repeated constrained-type-specifier, use the index we
28026 chose before. */
28027 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28028 else
28029 {
28030 /* Otherwise this is new, so use the current count. */
28031 *p = t;
28032 idx = hash.elements () - 1;
28033 }
28034 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28035 }
28036
28037 /* Always keep walking. */
28038 return 0;
28039 }
28040
28041 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28042 says they can appear anywhere in the type. */
28043
28044 static tree
28045 extract_autos (tree type)
28046 {
28047 hash_set<tree> visited;
28048 hash_table<auto_hash> hash (2);
28049
28050 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28051
28052 tree tree_vec = make_tree_vec (hash.elements());
28053 for (hash_table<auto_hash>::iterator iter = hash.begin();
28054 iter != hash.end(); ++iter)
28055 {
28056 tree elt = *iter;
28057 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28058 TREE_VEC_ELT (tree_vec, i)
28059 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28060 }
28061
28062 return tree_vec;
28063 }
28064
28065 /* The stem for deduction guide names. */
28066 const char *const dguide_base = "__dguide_";
28067
28068 /* Return the name for a deduction guide for class template TMPL. */
28069
28070 tree
28071 dguide_name (tree tmpl)
28072 {
28073 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28074 tree tname = TYPE_IDENTIFIER (type);
28075 char *buf = (char *) alloca (1 + strlen (dguide_base)
28076 + IDENTIFIER_LENGTH (tname));
28077 memcpy (buf, dguide_base, strlen (dguide_base));
28078 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28079 IDENTIFIER_LENGTH (tname) + 1);
28080 tree dname = get_identifier (buf);
28081 TREE_TYPE (dname) = type;
28082 return dname;
28083 }
28084
28085 /* True if NAME is the name of a deduction guide. */
28086
28087 bool
28088 dguide_name_p (tree name)
28089 {
28090 return (TREE_CODE (name) == IDENTIFIER_NODE
28091 && TREE_TYPE (name)
28092 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28093 strlen (dguide_base)));
28094 }
28095
28096 /* True if FN is a deduction guide. */
28097
28098 bool
28099 deduction_guide_p (const_tree fn)
28100 {
28101 if (DECL_P (fn))
28102 if (tree name = DECL_NAME (fn))
28103 return dguide_name_p (name);
28104 return false;
28105 }
28106
28107 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28108
28109 bool
28110 copy_guide_p (const_tree fn)
28111 {
28112 gcc_assert (deduction_guide_p (fn));
28113 if (!DECL_ARTIFICIAL (fn))
28114 return false;
28115 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28116 return (TREE_CHAIN (parms) == void_list_node
28117 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28118 }
28119
28120 /* True if FN is a guide generated from a constructor template. */
28121
28122 bool
28123 template_guide_p (const_tree fn)
28124 {
28125 gcc_assert (deduction_guide_p (fn));
28126 if (!DECL_ARTIFICIAL (fn))
28127 return false;
28128 tree tmpl = DECL_TI_TEMPLATE (fn);
28129 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28130 return PRIMARY_TEMPLATE_P (org);
28131 return false;
28132 }
28133
28134 /* True if FN is an aggregate initialization guide or the copy deduction
28135 guide. */
28136
28137 bool
28138 builtin_guide_p (const_tree fn)
28139 {
28140 if (!deduction_guide_p (fn))
28141 return false;
28142 if (!DECL_ARTIFICIAL (fn))
28143 /* Explicitly declared. */
28144 return false;
28145 if (DECL_ABSTRACT_ORIGIN (fn))
28146 /* Derived from a constructor. */
28147 return false;
28148 return true;
28149 }
28150
28151 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28152 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28153 template parameter types. Note that the handling of template template
28154 parameters relies on current_template_parms being set appropriately for the
28155 new template. */
28156
28157 static tree
28158 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28159 tree tsubst_args, tsubst_flags_t complain)
28160 {
28161 if (olddecl == error_mark_node)
28162 return error_mark_node;
28163
28164 tree oldidx = get_template_parm_index (olddecl);
28165
28166 tree newtype;
28167 if (TREE_CODE (olddecl) == TYPE_DECL
28168 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28169 {
28170 tree oldtype = TREE_TYPE (olddecl);
28171 newtype = cxx_make_type (TREE_CODE (oldtype));
28172 TYPE_MAIN_VARIANT (newtype) = newtype;
28173 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28174 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28175 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28176 }
28177 else
28178 {
28179 newtype = TREE_TYPE (olddecl);
28180 if (type_uses_auto (newtype))
28181 {
28182 // Substitute once to fix references to other template parameters.
28183 newtype = tsubst (newtype, tsubst_args,
28184 complain|tf_partial, NULL_TREE);
28185 // Now substitute again to reduce the level of the auto.
28186 newtype = tsubst (newtype, current_template_args (),
28187 complain, NULL_TREE);
28188 }
28189 else
28190 newtype = tsubst (newtype, tsubst_args,
28191 complain, NULL_TREE);
28192 }
28193
28194 tree newdecl
28195 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28196 DECL_NAME (olddecl), newtype);
28197 SET_DECL_TEMPLATE_PARM_P (newdecl);
28198
28199 tree newidx;
28200 if (TREE_CODE (olddecl) == TYPE_DECL
28201 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28202 {
28203 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28204 = build_template_parm_index (index, level, level,
28205 newdecl, newtype);
28206 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28207 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28208 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28209 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28210 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28211 else
28212 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28213
28214 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28215 {
28216 DECL_TEMPLATE_RESULT (newdecl)
28217 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28218 DECL_NAME (olddecl), newtype);
28219 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28220 // First create a copy (ttargs) of tsubst_args with an
28221 // additional level for the template template parameter's own
28222 // template parameters (ttparms).
28223 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28224 (DECL_TEMPLATE_PARMS (olddecl)));
28225 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28226 tree ttargs = make_tree_vec (depth + 1);
28227 for (int i = 0; i < depth; ++i)
28228 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28229 TREE_VEC_ELT (ttargs, depth)
28230 = template_parms_level_to_args (ttparms);
28231 // Substitute ttargs into ttparms to fix references to
28232 // other template parameters.
28233 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28234 complain|tf_partial);
28235 // Now substitute again with args based on tparms, to reduce
28236 // the level of the ttparms.
28237 ttargs = current_template_args ();
28238 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28239 complain);
28240 // Finally, tack the adjusted parms onto tparms.
28241 ttparms = tree_cons (size_int (depth), ttparms,
28242 current_template_parms);
28243 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28244 }
28245 }
28246 else
28247 {
28248 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28249 tree newconst
28250 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28251 TREE_CODE (oldconst),
28252 DECL_NAME (oldconst), newtype);
28253 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28254 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28255 SET_DECL_TEMPLATE_PARM_P (newconst);
28256 newidx = build_template_parm_index (index, level, level,
28257 newconst, newtype);
28258 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28259 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28260 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28261 }
28262
28263 return newdecl;
28264 }
28265
28266 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28267 template parameter. */
28268
28269 static tree
28270 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28271 tree targs, unsigned targs_index, tsubst_flags_t complain)
28272 {
28273 tree olddecl = TREE_VALUE (oldelt);
28274 tree newdecl = rewrite_template_parm (olddecl, index, level,
28275 targs, complain);
28276 if (newdecl == error_mark_node)
28277 return error_mark_node;
28278 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28279 targs, complain, NULL_TREE);
28280 tree list = build_tree_list (newdef, newdecl);
28281 TEMPLATE_PARM_CONSTRAINTS (list)
28282 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28283 targs, complain, NULL_TREE);
28284 int depth = TMPL_ARGS_DEPTH (targs);
28285 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28286 return list;
28287 }
28288
28289 /* Returns a C++17 class deduction guide template based on the constructor
28290 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28291 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28292 aggregate initialization guide. */
28293
28294 static tree
28295 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28296 {
28297 tree tparms, targs, fparms, fargs, ci;
28298 bool memtmpl = false;
28299 bool explicit_p;
28300 location_t loc;
28301 tree fn_tmpl = NULL_TREE;
28302
28303 if (outer_args)
28304 {
28305 ++processing_template_decl;
28306 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28307 --processing_template_decl;
28308 }
28309
28310 if (!DECL_DECLARES_FUNCTION_P (ctor))
28311 {
28312 if (TYPE_P (ctor))
28313 {
28314 bool copy_p = TYPE_REF_P (ctor);
28315 if (copy_p)
28316 fparms = tree_cons (NULL_TREE, type, void_list_node);
28317 else
28318 fparms = void_list_node;
28319 }
28320 else if (TREE_CODE (ctor) == TREE_LIST)
28321 fparms = ctor;
28322 else
28323 gcc_unreachable ();
28324
28325 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28326 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28327 targs = CLASSTYPE_TI_ARGS (type);
28328 ci = NULL_TREE;
28329 fargs = NULL_TREE;
28330 loc = DECL_SOURCE_LOCATION (ctmpl);
28331 explicit_p = false;
28332 }
28333 else
28334 {
28335 ++processing_template_decl;
28336 bool ok = true;
28337
28338 fn_tmpl
28339 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28340 : DECL_TI_TEMPLATE (ctor));
28341 if (outer_args)
28342 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28343 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28344
28345 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28346 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28347 fully specialized args for the enclosing class. Strip those off, as
28348 the deduction guide won't have those template parameters. */
28349 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28350 TMPL_PARMS_DEPTH (tparms));
28351 /* Discard the 'this' parameter. */
28352 fparms = FUNCTION_ARG_CHAIN (ctor);
28353 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28354 ci = get_constraints (ctor);
28355 loc = DECL_SOURCE_LOCATION (ctor);
28356 explicit_p = DECL_NONCONVERTING_P (ctor);
28357
28358 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28359 {
28360 memtmpl = true;
28361
28362 /* For a member template constructor, we need to flatten the two
28363 template parameter lists into one, and then adjust the function
28364 signature accordingly. This gets...complicated. */
28365 tree save_parms = current_template_parms;
28366
28367 /* For a member template we should have two levels of parms/args, one
28368 for the class and one for the constructor. We stripped
28369 specialized args for further enclosing classes above. */
28370 const int depth = 2;
28371 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28372
28373 /* Template args for translating references to the two-level template
28374 parameters into references to the one-level template parameters we
28375 are creating. */
28376 tree tsubst_args = copy_node (targs);
28377 TMPL_ARGS_LEVEL (tsubst_args, depth)
28378 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28379
28380 /* Template parms for the constructor template. */
28381 tree ftparms = TREE_VALUE (tparms);
28382 unsigned flen = TREE_VEC_LENGTH (ftparms);
28383 /* Template parms for the class template. */
28384 tparms = TREE_CHAIN (tparms);
28385 tree ctparms = TREE_VALUE (tparms);
28386 unsigned clen = TREE_VEC_LENGTH (ctparms);
28387 /* Template parms for the deduction guide start as a copy of the
28388 template parms for the class. We set current_template_parms for
28389 lookup_template_class_1. */
28390 current_template_parms = tparms = copy_node (tparms);
28391 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28392 for (unsigned i = 0; i < clen; ++i)
28393 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28394
28395 /* Now we need to rewrite the constructor parms to append them to the
28396 class parms. */
28397 for (unsigned i = 0; i < flen; ++i)
28398 {
28399 unsigned index = i + clen;
28400 unsigned level = 1;
28401 tree oldelt = TREE_VEC_ELT (ftparms, i);
28402 tree newelt
28403 = rewrite_tparm_list (oldelt, index, level,
28404 tsubst_args, i, complain);
28405 if (newelt == error_mark_node)
28406 ok = false;
28407 TREE_VEC_ELT (new_vec, index) = newelt;
28408 }
28409
28410 /* Now we have a final set of template parms to substitute into the
28411 function signature. */
28412 targs = template_parms_to_args (tparms);
28413 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28414 complain, ctor);
28415 if (fparms == error_mark_node)
28416 ok = false;
28417 if (ci)
28418 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28419
28420 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28421 cp_unevaluated_operand. */
28422 cp_evaluated ev;
28423 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28424 current_template_parms = save_parms;
28425 }
28426 else
28427 {
28428 /* Substitute in the same arguments to rewrite class members into
28429 references to members of an unknown specialization. */
28430 cp_evaluated ev;
28431 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28432 fargs = tsubst (fargs, targs, complain, ctor);
28433 if (ci)
28434 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28435 }
28436
28437 --processing_template_decl;
28438 if (!ok)
28439 return error_mark_node;
28440 }
28441
28442 if (!memtmpl)
28443 {
28444 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28445 tparms = copy_node (tparms);
28446 INNERMOST_TEMPLATE_PARMS (tparms)
28447 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28448 }
28449
28450 tree fntype = build_function_type (type, fparms);
28451 tree ded_fn = build_lang_decl_loc (loc,
28452 FUNCTION_DECL,
28453 dguide_name (type), fntype);
28454 DECL_ARGUMENTS (ded_fn) = fargs;
28455 DECL_ARTIFICIAL (ded_fn) = true;
28456 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28457 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28458 DECL_ARTIFICIAL (ded_tmpl) = true;
28459 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28460 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28461 if (DECL_P (ctor))
28462 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28463 if (ci)
28464 set_constraints (ded_tmpl, ci);
28465
28466 return ded_tmpl;
28467 }
28468
28469 /* Add to LIST the member types for the reshaped initializer CTOR. */
28470
28471 static tree
28472 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28473 {
28474 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28475 tree idx, val; unsigned i;
28476 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28477 {
28478 tree ftype = elt ? elt : TREE_TYPE (idx);
28479 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28480 && CONSTRUCTOR_NELTS (val)
28481 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28482 type gets a single initializer. */
28483 && CP_AGGREGATE_TYPE_P (ftype)
28484 && !(TREE_CODE (ftype) == ARRAY_TYPE
28485 && uses_template_parms (TYPE_DOMAIN (ftype))))
28486 {
28487 tree subelt = NULL_TREE;
28488 if (TREE_CODE (ftype) == ARRAY_TYPE)
28489 subelt = TREE_TYPE (ftype);
28490 list = collect_ctor_idx_types (val, list, subelt);
28491 continue;
28492 }
28493 tree arg = NULL_TREE;
28494 if (i == v->length() - 1
28495 && PACK_EXPANSION_P (ftype))
28496 /* Give the trailing pack expansion parameter a default argument to
28497 match aggregate initialization behavior, even if we deduce the
28498 length of the pack separately to more than we have initializers. */
28499 arg = build_constructor (init_list_type_node, NULL);
28500 /* if ei is of array type and xi is a braced-init-list or string literal,
28501 Ti is an rvalue reference to the declared type of ei */
28502 STRIP_ANY_LOCATION_WRAPPER (val);
28503 if (TREE_CODE (ftype) == ARRAY_TYPE
28504 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28505 || TREE_CODE (val) == STRING_CST))
28506 {
28507 if (TREE_CODE (val) == STRING_CST)
28508 ftype = cp_build_qualified_type
28509 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28510 ftype = (cp_build_reference_type
28511 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28512 }
28513 list = tree_cons (arg, ftype, list);
28514 }
28515
28516 return list;
28517 }
28518
28519 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28520
28521 static bool
28522 is_spec_or_derived (tree etype, tree tmpl)
28523 {
28524 if (!etype || !CLASS_TYPE_P (etype))
28525 return false;
28526
28527 tree type = TREE_TYPE (tmpl);
28528 tree tparms = (INNERMOST_TEMPLATE_PARMS
28529 (DECL_TEMPLATE_PARMS (tmpl)));
28530 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28531 int err = unify (tparms, targs, type, etype,
28532 UNIFY_ALLOW_DERIVED, /*explain*/false);
28533 ggc_free (targs);
28534 return !err;
28535 }
28536
28537 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28538 INIT. */
28539
28540 static tree
28541 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28542 {
28543 if (cxx_dialect < cxx20)
28544 return NULL_TREE;
28545
28546 if (init == NULL_TREE)
28547 return NULL_TREE;
28548
28549 tree type = TREE_TYPE (tmpl);
28550 if (!CP_AGGREGATE_TYPE_P (type))
28551 return NULL_TREE;
28552
28553 /* No aggregate candidate for copy-initialization. */
28554 if (args->length() == 1)
28555 {
28556 tree val = (*args)[0];
28557 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28558 return NULL_TREE;
28559 }
28560
28561 /* If we encounter a problem, we just won't add the candidate. */
28562 tsubst_flags_t complain = tf_none;
28563
28564 tree parms = NULL_TREE;
28565 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28566 {
28567 init = reshape_init (type, init, complain);
28568 if (init == error_mark_node)
28569 return NULL_TREE;
28570 parms = collect_ctor_idx_types (init, parms);
28571 }
28572 else if (TREE_CODE (init) == TREE_LIST)
28573 {
28574 int len = list_length (init);
28575 for (tree field = TYPE_FIELDS (type);
28576 len;
28577 --len, field = DECL_CHAIN (field))
28578 {
28579 field = next_initializable_field (field);
28580 if (!field)
28581 return NULL_TREE;
28582 tree ftype = finish_decltype_type (field, true, complain);
28583 parms = tree_cons (NULL_TREE, ftype, parms);
28584 }
28585 }
28586 else
28587 /* Aggregate initialization doesn't apply to an initializer expression. */
28588 return NULL_TREE;
28589
28590 if (parms)
28591 {
28592 tree last = parms;
28593 parms = nreverse (parms);
28594 TREE_CHAIN (last) = void_list_node;
28595 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28596 return guide;
28597 }
28598
28599 return NULL_TREE;
28600 }
28601
28602 /* UGUIDES are the deduction guides for the underlying template of alias
28603 template TMPL; adjust them to be deduction guides for TMPL. */
28604
28605 static tree
28606 alias_ctad_tweaks (tree tmpl, tree uguides)
28607 {
28608 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28609 class type (9.2.8.2) where the template-name names an alias template A,
28610 the defining-type-id of A must be of the form
28611
28612 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28613
28614 as specified in 9.2.8.2. The guides of A are the set of functions or
28615 function templates formed as follows. For each function or function
28616 template f in the guides of the template named by the simple-template-id
28617 of the defining-type-id, the template arguments of the return type of f
28618 are deduced from the defining-type-id of A according to the process in
28619 13.10.2.5 with the exception that deduction does not fail if not all
28620 template arguments are deduced. Let g denote the result of substituting
28621 these deductions into f. If substitution succeeds, form a function or
28622 function template f' with the following properties and add it to the set
28623 of guides of A:
28624
28625 * The function type of f' is the function type of g.
28626
28627 * If f is a function template, f' is a function template whose template
28628 parameter list consists of all the template parameters of A (including
28629 their default template arguments) that appear in the above deductions or
28630 (recursively) in their default template arguments, followed by the
28631 template parameters of f that were not deduced (including their default
28632 template arguments), otherwise f' is not a function template.
28633
28634 * The associated constraints (13.5.2) are the conjunction of the
28635 associated constraints of g and a constraint that is satisfied if and only
28636 if the arguments of A are deducible (see below) from the return type.
28637
28638 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28639 be so as well.
28640
28641 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28642 considered to be so as well.
28643
28644 * The explicit-specifier of f' is the explicit-specifier of g (if
28645 any). */
28646
28647 /* This implementation differs from the above in two significant ways:
28648
28649 1) We include all template parameters of A, not just some.
28650 2) The added constraint is same_type instead of deducible.
28651
28652 I believe that while it's probably possible to construct a testcase that
28653 behaves differently with this simplification, it should have the same
28654 effect for real uses. Including all template parameters means that we
28655 deduce all parameters of A when resolving the call, so when we're in the
28656 constraint we don't need to deduce them again, we can just check whether
28657 the deduction produced the desired result. */
28658
28659 tsubst_flags_t complain = tf_warning_or_error;
28660 tree atype = TREE_TYPE (tmpl);
28661 tree aguides = NULL_TREE;
28662 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28663 unsigned natparms = TREE_VEC_LENGTH (atparms);
28664 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28665 for (ovl_iterator iter (uguides); iter; ++iter)
28666 {
28667 tree f = *iter;
28668 tree in_decl = f;
28669 location_t loc = DECL_SOURCE_LOCATION (f);
28670 tree ret = TREE_TYPE (TREE_TYPE (f));
28671 tree fprime = f;
28672 if (TREE_CODE (f) == TEMPLATE_DECL)
28673 {
28674 processing_template_decl_sentinel ptds (/*reset*/false);
28675 ++processing_template_decl;
28676
28677 /* Deduce template arguments for f from the type-id of A. */
28678 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28679 unsigned len = TREE_VEC_LENGTH (ftparms);
28680 tree targs = make_tree_vec (len);
28681 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28682 gcc_assert (!err);
28683
28684 /* The number of parms for f' is the number of parms for A plus
28685 non-deduced parms of f. */
28686 unsigned ndlen = 0;
28687 unsigned j;
28688 for (unsigned i = 0; i < len; ++i)
28689 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28690 ++ndlen;
28691 tree gtparms = make_tree_vec (natparms + ndlen);
28692
28693 /* First copy over the parms of A. */
28694 for (j = 0; j < natparms; ++j)
28695 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28696 /* Now rewrite the non-deduced parms of f. */
28697 for (unsigned i = 0; ndlen && i < len; ++i)
28698 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28699 {
28700 --ndlen;
28701 unsigned index = j++;
28702 unsigned level = 1;
28703 tree oldlist = TREE_VEC_ELT (ftparms, i);
28704 tree list = rewrite_tparm_list (oldlist, index, level,
28705 targs, i, complain);
28706 TREE_VEC_ELT (gtparms, index) = list;
28707 }
28708 gtparms = build_tree_list (size_one_node, gtparms);
28709
28710 /* Substitute the deduced arguments plus the rewritten template
28711 parameters into f to get g. This covers the type, copyness,
28712 guideness, and explicit-specifier. */
28713 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28714 if (g == error_mark_node)
28715 return error_mark_node;
28716 DECL_USE_TEMPLATE (g) = 0;
28717 fprime = build_template_decl (g, gtparms, false);
28718 DECL_TEMPLATE_RESULT (fprime) = g;
28719 TREE_TYPE (fprime) = TREE_TYPE (g);
28720 tree gtargs = template_parms_to_args (gtparms);
28721 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28722 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28723
28724 /* Substitute the associated constraints. */
28725 tree ci = get_constraints (f);
28726 if (ci)
28727 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28728 if (ci == error_mark_node)
28729 return error_mark_node;
28730
28731 /* Add a constraint that the return type matches the instantiation of
28732 A with the same template arguments. */
28733 ret = TREE_TYPE (TREE_TYPE (fprime));
28734 if (!same_type_p (atype, ret)
28735 /* FIXME this should mean they don't compare as equivalent. */
28736 || dependent_alias_template_spec_p (atype, nt_opaque))
28737 {
28738 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28739 ci = append_constraint (ci, same);
28740 }
28741
28742 if (ci)
28743 {
28744 remove_constraints (fprime);
28745 set_constraints (fprime, ci);
28746 }
28747 }
28748 else
28749 {
28750 /* For a non-template deduction guide, if the arguments of A aren't
28751 deducible from the return type, don't add the candidate. */
28752 tree targs = make_tree_vec (natparms);
28753 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28754 for (unsigned i = 0; !err && i < natparms; ++i)
28755 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28756 err = true;
28757 if (err)
28758 continue;
28759 }
28760
28761 aguides = lookup_add (fprime, aguides);
28762 }
28763
28764 return aguides;
28765 }
28766
28767 /* Return artificial deduction guides built from the constructors of class
28768 template TMPL. */
28769
28770 static tree
28771 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28772 {
28773 tree type = TREE_TYPE (tmpl);
28774 tree outer_args = NULL_TREE;
28775 if (DECL_CLASS_SCOPE_P (tmpl)
28776 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28777 {
28778 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28779 type = TREE_TYPE (most_general_template (tmpl));
28780 }
28781
28782 tree cands = NULL_TREE;
28783
28784 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28785 {
28786 /* Skip inherited constructors. */
28787 if (iter.using_p ())
28788 continue;
28789
28790 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28791 cands = lookup_add (guide, cands);
28792 }
28793
28794 /* Add implicit default constructor deduction guide. */
28795 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28796 {
28797 tree guide = build_deduction_guide (type, type, outer_args,
28798 complain);
28799 cands = lookup_add (guide, cands);
28800 }
28801
28802 /* Add copy guide. */
28803 {
28804 tree gtype = build_reference_type (type);
28805 tree guide = build_deduction_guide (type, gtype, outer_args,
28806 complain);
28807 cands = lookup_add (guide, cands);
28808 }
28809
28810 return cands;
28811 }
28812
28813 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28814
28815 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28816 aggregate candidate is added separately because it depends on the
28817 initializer. */
28818
28819 static tree
28820 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28821 {
28822 tree guides = NULL_TREE;
28823 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28824 {
28825 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28826 tree tinfo = get_template_info (under);
28827 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28828 }
28829 else
28830 {
28831 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28832 dguide_name (tmpl),
28833 LOOK_want::NORMAL, /*complain*/false);
28834 if (guides == error_mark_node)
28835 guides = NULL_TREE;
28836 }
28837
28838 /* Cache the deduction guides for a template. We also remember the result of
28839 lookup, and rebuild everything if it changes; should be very rare. */
28840 tree_pair_p cache = NULL;
28841 if (tree_pair_p &r
28842 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28843 {
28844 cache = r;
28845 if (cache->purpose == guides)
28846 return cache->value;
28847 }
28848 else
28849 {
28850 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28851 cache->purpose = guides;
28852 }
28853
28854 tree cands = NULL_TREE;
28855 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28856 cands = alias_ctad_tweaks (tmpl, guides);
28857 else
28858 {
28859 cands = ctor_deduction_guides_for (tmpl, complain);
28860 for (ovl_iterator it (guides); it; ++it)
28861 cands = lookup_add (*it, cands);
28862 }
28863
28864 cache->value = cands;
28865 return cands;
28866 }
28867
28868 /* Return whether TMPL is a (class template argument-) deducible template. */
28869
28870 bool
28871 ctad_template_p (tree tmpl)
28872 {
28873 /* A deducible template is either a class template or is an alias template
28874 whose defining-type-id is of the form
28875
28876 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28877
28878 where the nested-name-specifier (if any) is non-dependent and the
28879 template-name of the simple-template-id names a deducible template. */
28880
28881 if (DECL_CLASS_TEMPLATE_P (tmpl)
28882 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28883 return true;
28884 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28885 return false;
28886 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28887 if (tree tinfo = get_template_info (orig))
28888 return ctad_template_p (TI_TEMPLATE (tinfo));
28889 return false;
28890 }
28891
28892 /* Deduce template arguments for the class template placeholder PTYPE for
28893 template TMPL based on the initializer INIT, and return the resulting
28894 type. */
28895
28896 static tree
28897 do_class_deduction (tree ptype, tree tmpl, tree init,
28898 int flags, tsubst_flags_t complain)
28899 {
28900 /* We should have handled this in the caller. */
28901 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28902 return ptype;
28903
28904 /* Look through alias templates that just rename another template. */
28905 tmpl = get_underlying_template (tmpl);
28906 if (!ctad_template_p (tmpl))
28907 {
28908 if (complain & tf_error)
28909 error ("non-deducible template %qT used without template arguments", tmpl);
28910 return error_mark_node;
28911 }
28912 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28913 {
28914 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28915 if (complain & tf_warning_or_error)
28916 pedwarn (input_location, 0, "alias template deduction only available "
28917 "with %<-std=c++20%> or %<-std=gnu++20%>");
28918 }
28919
28920 if (init && TREE_TYPE (init) == ptype)
28921 /* Using the template parm as its own argument. */
28922 return ptype;
28923
28924 tree type = TREE_TYPE (tmpl);
28925
28926 bool try_list_ctor = false;
28927
28928 releasing_vec rv_args = NULL;
28929 vec<tree,va_gc> *&args = *&rv_args;
28930 if (init == NULL_TREE)
28931 args = make_tree_vector ();
28932 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28933 {
28934 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28935 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28936 {
28937 /* As an exception, the first phase in 16.3.1.7 (considering the
28938 initializer list as a single argument) is omitted if the
28939 initializer list consists of a single expression of type cv U,
28940 where U is a specialization of C or a class derived from a
28941 specialization of C. */
28942 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28943 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28944 try_list_ctor = false;
28945 }
28946 if (try_list_ctor || is_std_init_list (type))
28947 args = make_tree_vector_single (init);
28948 else
28949 args = make_tree_vector_from_ctor (init);
28950 }
28951 else if (TREE_CODE (init) == TREE_LIST)
28952 args = make_tree_vector_from_list (init);
28953 else
28954 args = make_tree_vector_single (init);
28955
28956 /* Do this now to avoid problems with erroneous args later on. */
28957 args = resolve_args (args, complain);
28958 if (args == NULL)
28959 return error_mark_node;
28960
28961 tree cands = deduction_guides_for (tmpl, complain);
28962 if (cands == error_mark_node)
28963 return error_mark_node;
28964
28965 /* Prune explicit deduction guides in copy-initialization context. */
28966 bool elided = false;
28967 if (flags & LOOKUP_ONLYCONVERTING)
28968 {
28969 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28970 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28971 elided = true;
28972
28973 if (elided)
28974 {
28975 /* Found a nonconverting guide, prune the candidates. */
28976 tree pruned = NULL_TREE;
28977 for (lkp_iterator iter (cands); iter; ++iter)
28978 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28979 pruned = lookup_add (*iter, pruned);
28980
28981 cands = pruned;
28982 }
28983 }
28984
28985 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28986 cands = lookup_add (guide, cands);
28987
28988 tree call = error_mark_node;
28989
28990 /* If this is list-initialization and the class has a list constructor, first
28991 try deducing from the list as a single argument, as [over.match.list]. */
28992 tree list_cands = NULL_TREE;
28993 if (try_list_ctor && cands)
28994 for (lkp_iterator iter (cands); iter; ++iter)
28995 {
28996 tree dg = *iter;
28997 if (is_list_ctor (dg))
28998 list_cands = lookup_add (dg, list_cands);
28999 }
29000 if (list_cands)
29001 {
29002 ++cp_unevaluated_operand;
29003 call = build_new_function_call (list_cands, &args, tf_decltype);
29004 --cp_unevaluated_operand;
29005
29006 if (call == error_mark_node)
29007 {
29008 /* That didn't work, now try treating the list as a sequence of
29009 arguments. */
29010 release_tree_vector (args);
29011 args = make_tree_vector_from_ctor (init);
29012 }
29013 }
29014
29015 if (elided && !cands)
29016 {
29017 error ("cannot deduce template arguments for copy-initialization"
29018 " of %qT, as it has no non-explicit deduction guides or "
29019 "user-declared constructors", type);
29020 return error_mark_node;
29021 }
29022 else if (!cands && call == error_mark_node)
29023 {
29024 error ("cannot deduce template arguments of %qT, as it has no viable "
29025 "deduction guides", type);
29026 return error_mark_node;
29027 }
29028
29029 if (call == error_mark_node)
29030 {
29031 ++cp_unevaluated_operand;
29032 call = build_new_function_call (cands, &args, tf_decltype);
29033 --cp_unevaluated_operand;
29034 }
29035
29036 if (call == error_mark_node
29037 && (complain & tf_warning_or_error))
29038 {
29039 error ("class template argument deduction failed:");
29040
29041 ++cp_unevaluated_operand;
29042 call = build_new_function_call (cands, &args, complain | tf_decltype);
29043 --cp_unevaluated_operand;
29044
29045 if (elided)
29046 inform (input_location, "explicit deduction guides not considered "
29047 "for copy-initialization");
29048 }
29049
29050 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29051 }
29052
29053 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29054 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29055 The CONTEXT determines the context in which auto deduction is performed
29056 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29057 OUTER_TARGS are used during template argument deduction
29058 (context == adc_unify) to properly substitute the result, and is ignored
29059 in other contexts.
29060
29061 For partial-concept-ids, extra args may be appended to the list of deduced
29062 template arguments prior to determining constraint satisfaction. */
29063
29064 tree
29065 do_auto_deduction (tree type, tree init, tree auto_node,
29066 tsubst_flags_t complain, auto_deduction_context context,
29067 tree outer_targs, int flags)
29068 {
29069 tree targs;
29070
29071 if (init == error_mark_node)
29072 return error_mark_node;
29073
29074 if (init && type_dependent_expression_p (init)
29075 && context != adc_unify)
29076 /* Defining a subset of type-dependent expressions that we can deduce
29077 from ahead of time isn't worth the trouble. */
29078 return type;
29079
29080 /* Similarly, we can't deduce from another undeduced decl. */
29081 if (init && undeduced_auto_decl (init))
29082 return type;
29083
29084 /* We may be doing a partial substitution, but we still want to replace
29085 auto_node. */
29086 complain &= ~tf_partial;
29087
29088 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29089 /* C++17 class template argument deduction. */
29090 return do_class_deduction (type, tmpl, init, flags, complain);
29091
29092 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29093 /* Nothing we can do with this, even in deduction context. */
29094 return type;
29095
29096 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29097 with either a new invented type template parameter U or, if the
29098 initializer is a braced-init-list (8.5.4), with
29099 std::initializer_list<U>. */
29100 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29101 {
29102 if (!DIRECT_LIST_INIT_P (init))
29103 type = listify_autos (type, auto_node);
29104 else if (CONSTRUCTOR_NELTS (init) == 1)
29105 init = CONSTRUCTOR_ELT (init, 0)->value;
29106 else
29107 {
29108 if (complain & tf_warning_or_error)
29109 {
29110 if (permerror (input_location, "direct-list-initialization of "
29111 "%<auto%> requires exactly one element"))
29112 inform (input_location,
29113 "for deduction to %<std::initializer_list%>, use copy-"
29114 "list-initialization (i.e. add %<=%> before the %<{%>)");
29115 }
29116 type = listify_autos (type, auto_node);
29117 }
29118 }
29119
29120 if (type == error_mark_node)
29121 return error_mark_node;
29122
29123 init = resolve_nondeduced_context (init, complain);
29124
29125 if (context == adc_decomp_type
29126 && auto_node == type
29127 && init != error_mark_node
29128 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29129 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29130 and initializer has array type, deduce cv-qualified array type. */
29131 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29132 complain);
29133 else if (AUTO_IS_DECLTYPE (auto_node))
29134 {
29135 tree stripped_init = tree_strip_any_location_wrapper (init);
29136 bool id = (DECL_P (stripped_init)
29137 || ((TREE_CODE (init) == COMPONENT_REF
29138 || TREE_CODE (init) == SCOPE_REF)
29139 && !REF_PARENTHESIZED_P (init)));
29140 targs = make_tree_vec (1);
29141 TREE_VEC_ELT (targs, 0)
29142 = finish_decltype_type (init, id, tf_warning_or_error);
29143 if (type != auto_node)
29144 {
29145 if (complain & tf_error)
29146 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29147 return error_mark_node;
29148 }
29149 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29150 {
29151 if (complain & tf_error)
29152 error ("%<decltype(auto)%> cannot be cv-qualified");
29153 return error_mark_node;
29154 }
29155 }
29156 else
29157 {
29158 if (error_operand_p (init))
29159 return error_mark_node;
29160
29161 tree parms = build_tree_list (NULL_TREE, type);
29162 tree tparms;
29163
29164 if (flag_concepts)
29165 tparms = extract_autos (type);
29166 else
29167 {
29168 tparms = make_tree_vec (1);
29169 TREE_VEC_ELT (tparms, 0)
29170 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29171 }
29172
29173 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29174 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29175 DEDUCE_CALL,
29176 NULL, /*explain_p=*/false);
29177 if (val > 0)
29178 {
29179 if (processing_template_decl)
29180 /* Try again at instantiation time. */
29181 return type;
29182 if (type && type != error_mark_node
29183 && (complain & tf_error))
29184 /* If type is error_mark_node a diagnostic must have been
29185 emitted by now. Also, having a mention to '<type error>'
29186 in the diagnostic is not really useful to the user. */
29187 {
29188 if (cfun
29189 && FNDECL_USED_AUTO (current_function_decl)
29190 && (auto_node
29191 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29192 && LAMBDA_FUNCTION_P (current_function_decl))
29193 error ("unable to deduce lambda return type from %qE", init);
29194 else
29195 error ("unable to deduce %qT from %qE", type, init);
29196 type_unification_real (tparms, targs, parms, &init, 1, 0,
29197 DEDUCE_CALL,
29198 NULL, /*explain_p=*/true);
29199 }
29200 return error_mark_node;
29201 }
29202 }
29203
29204 /* Check any placeholder constraints against the deduced type. */
29205 if (flag_concepts && !processing_template_decl)
29206 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29207 {
29208 /* Use the deduced type to check the associated constraints. If we
29209 have a partial-concept-id, rebuild the argument list so that
29210 we check using the extra arguments. */
29211 check = unpack_concept_check (check);
29212 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29213 tree cdecl = TREE_OPERAND (check, 0);
29214 if (OVL_P (cdecl))
29215 cdecl = OVL_FIRST (cdecl);
29216 tree cargs = TREE_OPERAND (check, 1);
29217 if (TREE_VEC_LENGTH (cargs) > 1)
29218 {
29219 cargs = copy_node (cargs);
29220 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29221 }
29222 else
29223 cargs = targs;
29224
29225 /* Rebuild the check using the deduced arguments. */
29226 check = build_concept_check (cdecl, cargs, tf_none);
29227
29228 if (!constraints_satisfied_p (check))
29229 {
29230 if (complain & tf_warning_or_error)
29231 {
29232 auto_diagnostic_group d;
29233 switch (context)
29234 {
29235 case adc_unspecified:
29236 case adc_unify:
29237 error("placeholder constraints not satisfied");
29238 break;
29239 case adc_variable_type:
29240 case adc_decomp_type:
29241 error ("deduced initializer does not satisfy "
29242 "placeholder constraints");
29243 break;
29244 case adc_return_type:
29245 error ("deduced return type does not satisfy "
29246 "placeholder constraints");
29247 break;
29248 case adc_requirement:
29249 error ("deduced expression type does not satisfy "
29250 "placeholder constraints");
29251 break;
29252 }
29253 diagnose_constraints (input_location, check, targs);
29254 }
29255 return error_mark_node;
29256 }
29257 }
29258
29259 if (processing_template_decl && context != adc_unify)
29260 outer_targs = current_template_args ();
29261 targs = add_to_template_args (outer_targs, targs);
29262 return tsubst (type, targs, complain, NULL_TREE);
29263 }
29264
29265 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29266 result. */
29267
29268 tree
29269 splice_late_return_type (tree type, tree late_return_type)
29270 {
29271 if (late_return_type)
29272 {
29273 gcc_assert (is_auto (type) || seen_error ());
29274 return late_return_type;
29275 }
29276
29277 if (tree *auto_node = find_type_usage (&type, is_auto))
29278 {
29279 tree idx = get_template_parm_index (*auto_node);
29280 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29281 {
29282 /* In an abbreviated function template we didn't know we were dealing
29283 with a function template when we saw the auto return type, so update
29284 it to have the correct level. */
29285 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29286 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29287 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29288 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29289 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29290 *auto_node = new_auto;
29291 }
29292 }
29293 return type;
29294 }
29295
29296 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29297 'decltype(auto)' or a deduced class template. */
29298
29299 bool
29300 is_auto (const_tree type)
29301 {
29302 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29303 && (TYPE_IDENTIFIER (type) == auto_identifier
29304 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29305 return true;
29306 else
29307 return false;
29308 }
29309
29310 /* for_each_template_parm callback for type_uses_auto. */
29311
29312 int
29313 is_auto_r (tree tp, void */*data*/)
29314 {
29315 return is_auto (tp);
29316 }
29317
29318 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29319 a use of `auto'. Returns NULL_TREE otherwise. */
29320
29321 tree
29322 type_uses_auto (tree type)
29323 {
29324 if (type == NULL_TREE)
29325 return NULL_TREE;
29326 else if (flag_concepts)
29327 {
29328 /* The Concepts TS allows multiple autos in one type-specifier; just
29329 return the first one we find, do_auto_deduction will collect all of
29330 them. */
29331 if (uses_template_parms (type))
29332 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29333 /*visited*/NULL, /*nondeduced*/false);
29334 else
29335 return NULL_TREE;
29336 }
29337 else if (tree *tp = find_type_usage (&type, is_auto))
29338 return *tp;
29339 else
29340 return NULL_TREE;
29341 }
29342
29343 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29344 concepts are enabled, auto is acceptable in template arguments, but
29345 only when TEMPL identifies a template class. Return TRUE if any
29346 such errors were reported. */
29347
29348 bool
29349 check_auto_in_tmpl_args (tree tmpl, tree args)
29350 {
29351 /* If there were previous errors, nevermind. */
29352 if (!args || TREE_CODE (args) != TREE_VEC)
29353 return false;
29354
29355 /* If TMPL is an identifier, we're parsing and we can't tell yet
29356 whether TMPL is supposed to be a type, a function or a variable.
29357 We'll only be able to tell during template substitution, so we
29358 expect to be called again then. If concepts are enabled and we
29359 know we have a type, we're ok. */
29360 if (flag_concepts
29361 && (identifier_p (tmpl)
29362 || (DECL_P (tmpl)
29363 && (DECL_TYPE_TEMPLATE_P (tmpl)
29364 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29365 return false;
29366
29367 /* Quickly search for any occurrences of auto; usually there won't
29368 be any, and then we'll avoid allocating the vector. */
29369 if (!type_uses_auto (args))
29370 return false;
29371
29372 bool errors = false;
29373
29374 tree vec = extract_autos (args);
29375 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29376 {
29377 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29378 error_at (DECL_SOURCE_LOCATION (xauto),
29379 "invalid use of %qT in template argument", xauto);
29380 errors = true;
29381 }
29382
29383 return errors;
29384 }
29385
29386 /* Recursively walk over && expressions searching for EXPR. Return a reference
29387 to that expression. */
29388
29389 static tree *find_template_requirement (tree *t, tree key)
29390 {
29391 if (*t == key)
29392 return t;
29393 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29394 {
29395 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29396 return p;
29397 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29398 return p;
29399 }
29400 return 0;
29401 }
29402
29403 /* Convert the generic type parameters in PARM that match the types given in the
29404 range [START_IDX, END_IDX) from the current_template_parms into generic type
29405 packs. */
29406
29407 tree
29408 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29409 {
29410 tree current = current_template_parms;
29411 int depth = TMPL_PARMS_DEPTH (current);
29412 current = INNERMOST_TEMPLATE_PARMS (current);
29413 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29414
29415 for (int i = 0; i < start_idx; ++i)
29416 TREE_VEC_ELT (replacement, i)
29417 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29418
29419 for (int i = start_idx; i < end_idx; ++i)
29420 {
29421 /* Create a distinct parameter pack type from the current parm and add it
29422 to the replacement args to tsubst below into the generic function
29423 parameter. */
29424 tree node = TREE_VEC_ELT (current, i);
29425 tree o = TREE_TYPE (TREE_VALUE (node));
29426 tree t = copy_type (o);
29427 TEMPLATE_TYPE_PARM_INDEX (t)
29428 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29429 t, 0, 0, tf_none);
29430 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29431 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29432 TYPE_MAIN_VARIANT (t) = t;
29433 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29434 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29435 TREE_VEC_ELT (replacement, i) = t;
29436
29437 /* Replace the current template parameter with new pack. */
29438 TREE_VALUE (node) = TREE_CHAIN (t);
29439
29440 /* Surgically adjust the associated constraint of adjusted parameter
29441 and it's corresponding contribution to the current template
29442 requirements. */
29443 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29444 {
29445 tree id = unpack_concept_check (constr);
29446 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29447 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29448 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29449
29450 /* If there was a constraint, we also need to replace that in
29451 the template requirements, which we've already built. */
29452 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29453 reqs = find_template_requirement (reqs, constr);
29454 *reqs = fold;
29455 }
29456 }
29457
29458 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29459 TREE_VEC_ELT (replacement, i)
29460 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29461
29462 /* If there are more levels then build up the replacement with the outer
29463 template parms. */
29464 if (depth > 1)
29465 replacement = add_to_template_args (template_parms_to_args
29466 (TREE_CHAIN (current_template_parms)),
29467 replacement);
29468
29469 return tsubst (parm, replacement, tf_none, NULL_TREE);
29470 }
29471
29472 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29473 0..N-1. */
29474
29475 void
29476 declare_integer_pack (void)
29477 {
29478 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29479 build_function_type_list (integer_type_node,
29480 integer_type_node,
29481 NULL_TREE),
29482 NULL_TREE, ECF_CONST);
29483 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29484 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29485 CP_BUILT_IN_INTEGER_PACK);
29486 }
29487
29488 /* Set up the hash tables for template instantiations. */
29489
29490 void
29491 init_template_processing (void)
29492 {
29493 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29494 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29495
29496 if (cxx_dialect >= cxx11)
29497 declare_integer_pack ();
29498 }
29499
29500 /* Print stats about the template hash tables for -fstats. */
29501
29502 void
29503 print_template_statistics (void)
29504 {
29505 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29506 "%f collisions\n", (long) decl_specializations->size (),
29507 (long) decl_specializations->elements (),
29508 decl_specializations->collisions ());
29509 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29510 "%f collisions\n", (long) type_specializations->size (),
29511 (long) type_specializations->elements (),
29512 type_specializations->collisions ());
29513 }
29514
29515 #if CHECKING_P
29516
29517 namespace selftest {
29518
29519 /* Verify that build_non_dependent_expr () works, for various expressions,
29520 and that location wrappers don't affect the results. */
29521
29522 static void
29523 test_build_non_dependent_expr ()
29524 {
29525 location_t loc = BUILTINS_LOCATION;
29526
29527 /* Verify constants, without and with location wrappers. */
29528 tree int_cst = build_int_cst (integer_type_node, 42);
29529 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29530
29531 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29532 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29533 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29534
29535 tree string_lit = build_string (4, "foo");
29536 TREE_TYPE (string_lit) = char_array_type_node;
29537 string_lit = fix_string_type (string_lit);
29538 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29539
29540 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29541 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29542 ASSERT_EQ (wrapped_string_lit,
29543 build_non_dependent_expr (wrapped_string_lit));
29544 }
29545
29546 /* Verify that type_dependent_expression_p () works correctly, even
29547 in the presence of location wrapper nodes. */
29548
29549 static void
29550 test_type_dependent_expression_p ()
29551 {
29552 location_t loc = BUILTINS_LOCATION;
29553
29554 tree name = get_identifier ("foo");
29555
29556 /* If no templates are involved, nothing is type-dependent. */
29557 gcc_assert (!processing_template_decl);
29558 ASSERT_FALSE (type_dependent_expression_p (name));
29559
29560 ++processing_template_decl;
29561
29562 /* Within a template, an unresolved name is always type-dependent. */
29563 ASSERT_TRUE (type_dependent_expression_p (name));
29564
29565 /* Ensure it copes with NULL_TREE and errors. */
29566 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29567 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29568
29569 /* A USING_DECL in a template should be type-dependent, even if wrapped
29570 with a location wrapper (PR c++/83799). */
29571 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29572 TREE_TYPE (using_decl) = integer_type_node;
29573 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29574 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29575 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29576 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29577
29578 --processing_template_decl;
29579 }
29580
29581 /* Run all of the selftests within this file. */
29582
29583 void
29584 cp_pt_c_tests ()
29585 {
29586 test_build_non_dependent_expr ();
29587 test_type_dependent_expression_p ();
29588 }
29589
29590 } // namespace selftest
29591
29592 #endif /* #if CHECKING_P */
29593
29594 #include "gt-cp-pt.h"