]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
907ca879c7319be10c0e1873360178d814e6a920
[thirdparty/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_typedefs_access_check (tree tmpl, tree targs);
219 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
220 location_t);
221 static tree listify (tree);
222 static tree listify_autos (tree, tree);
223 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
224 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
225 static bool complex_alias_template_p (const_tree tmpl);
226 static tree get_underlying_template (tree);
227 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
228 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
229 static tree make_argument_pack (tree);
230 static void register_parameter_specializations (tree, tree);
231 static tree enclosing_instantiation_of (tree tctx);
232
233 /* Make the current scope suitable for access checking when we are
234 processing T. T can be FUNCTION_DECL for instantiated function
235 template, VAR_DECL for static member variable, or TYPE_DECL for
236 alias template (needed by instantiate_decl). */
237
238 void
239 push_access_scope (tree t)
240 {
241 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
242 || TREE_CODE (t) == TYPE_DECL);
243
244 if (DECL_FRIEND_CONTEXT (t))
245 push_nested_class (DECL_FRIEND_CONTEXT (t));
246 else if (DECL_CLASS_SCOPE_P (t))
247 push_nested_class (DECL_CONTEXT (t));
248 else
249 push_to_top_level ();
250
251 if (TREE_CODE (t) == FUNCTION_DECL)
252 {
253 vec_safe_push (saved_access_scope, current_function_decl);
254 current_function_decl = t;
255 }
256 }
257
258 /* Restore the scope set up by push_access_scope. T is the node we
259 are processing. */
260
261 void
262 pop_access_scope (tree t)
263 {
264 if (TREE_CODE (t) == FUNCTION_DECL)
265 current_function_decl = saved_access_scope->pop();
266
267 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
268 pop_nested_class ();
269 else
270 pop_from_top_level ();
271 }
272
273 /* Do any processing required when DECL (a member template
274 declaration) is finished. Returns the TEMPLATE_DECL corresponding
275 to DECL, unless it is a specialization, in which case the DECL
276 itself is returned. */
277
278 tree
279 finish_member_template_decl (tree decl)
280 {
281 if (decl == error_mark_node)
282 return error_mark_node;
283
284 gcc_assert (DECL_P (decl));
285
286 if (TREE_CODE (decl) == TYPE_DECL)
287 {
288 tree type;
289
290 type = TREE_TYPE (decl);
291 if (type == error_mark_node)
292 return error_mark_node;
293 if (MAYBE_CLASS_TYPE_P (type)
294 && CLASSTYPE_TEMPLATE_INFO (type)
295 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
296 {
297 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
298 check_member_template (tmpl);
299 return tmpl;
300 }
301 return NULL_TREE;
302 }
303 else if (TREE_CODE (decl) == FIELD_DECL)
304 error_at (DECL_SOURCE_LOCATION (decl),
305 "data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error_at (DECL_SOURCE_LOCATION (decl),
318 "invalid member template declaration %qD", decl);
319
320 return error_mark_node;
321 }
322
323 /* Create a template info node. */
324
325 tree
326 build_template_info (tree template_decl, tree template_args)
327 {
328 tree result = make_node (TEMPLATE_INFO);
329 TI_TEMPLATE (result) = template_decl;
330 TI_ARGS (result) = template_args;
331 return result;
332 }
333
334 /* Return the template info node corresponding to T, whatever T is. */
335
336 tree
337 get_template_info (const_tree t)
338 {
339 tree tinfo = NULL_TREE;
340
341 if (!t || t == error_mark_node)
342 return NULL;
343
344 if (TREE_CODE (t) == NAMESPACE_DECL
345 || TREE_CODE (t) == PARM_DECL)
346 return NULL;
347
348 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
349 tinfo = DECL_TEMPLATE_INFO (t);
350
351 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
352 t = TREE_TYPE (t);
353
354 if (OVERLOAD_TYPE_P (t))
355 tinfo = TYPE_TEMPLATE_INFO (t);
356 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
357 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
358
359 return tinfo;
360 }
361
362 /* Returns the template nesting level of the indicated class TYPE.
363
364 For example, in:
365 template <class T>
366 struct A
367 {
368 template <class U>
369 struct B {};
370 };
371
372 A<T>::B<U> has depth two, while A<T> has depth one.
373 Both A<T>::B<int> and A<int>::B<U> have depth one, if
374 they are instantiations, not specializations.
375
376 This function is guaranteed to return 0 if passed NULL_TREE so
377 that, for example, `template_class_depth (current_class_type)' is
378 always safe. */
379
380 int
381 template_class_depth (tree type)
382 {
383 int depth;
384
385 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
386 {
387 tree tinfo = get_template_info (type);
388
389 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
390 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
391 ++depth;
392
393 if (DECL_P (type))
394 {
395 if (tree fctx = DECL_FRIEND_CONTEXT (type))
396 type = fctx;
397 else
398 type = CP_DECL_CONTEXT (type);
399 }
400 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
401 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
402 else
403 type = CP_TYPE_CONTEXT (type);
404 }
405
406 return depth;
407 }
408
409 /* Return TRUE if NODE instantiates a template that has arguments of
410 its own, be it directly a primary template or indirectly through a
411 partial specializations. */
412 static bool
413 instantiates_primary_template_p (tree node)
414 {
415 tree tinfo = get_template_info (node);
416 if (!tinfo)
417 return false;
418
419 tree tmpl = TI_TEMPLATE (tinfo);
420 if (PRIMARY_TEMPLATE_P (tmpl))
421 return true;
422
423 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
424 return false;
425
426 /* So now we know we have a specialization, but it could be a full
427 or a partial specialization. To tell which, compare the depth of
428 its template arguments with those of its context. */
429
430 tree ctxt = DECL_CONTEXT (tmpl);
431 tree ctinfo = get_template_info (ctxt);
432 if (!ctinfo)
433 return true;
434
435 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
436 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
437 }
438
439 /* Subroutine of maybe_begin_member_template_processing.
440 Returns true if processing DECL needs us to push template parms. */
441
442 static bool
443 inline_needs_template_parms (tree decl, bool nsdmi)
444 {
445 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
446 return false;
447
448 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
449 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
450 }
451
452 /* Subroutine of maybe_begin_member_template_processing.
453 Push the template parms in PARMS, starting from LEVELS steps into the
454 chain, and ending at the beginning, since template parms are listed
455 innermost first. */
456
457 static void
458 push_inline_template_parms_recursive (tree parmlist, int levels)
459 {
460 tree parms = TREE_VALUE (parmlist);
461 int i;
462
463 if (levels > 1)
464 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
465
466 ++processing_template_decl;
467 current_template_parms
468 = tree_cons (size_int (processing_template_decl),
469 parms, current_template_parms);
470 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
471
472 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
473 NULL);
474 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
475 {
476 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
477
478 if (error_operand_p (parm))
479 continue;
480
481 gcc_assert (DECL_P (parm));
482
483 switch (TREE_CODE (parm))
484 {
485 case TYPE_DECL:
486 case TEMPLATE_DECL:
487 pushdecl (parm);
488 break;
489
490 case PARM_DECL:
491 /* Push the CONST_DECL. */
492 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
493 break;
494
495 default:
496 gcc_unreachable ();
497 }
498 }
499 }
500
501 /* Restore the template parameter context for a member template, a
502 friend template defined in a class definition, or a non-template
503 member of template class. */
504
505 void
506 maybe_begin_member_template_processing (tree decl)
507 {
508 tree parms;
509 int levels = 0;
510 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
511
512 if (nsdmi)
513 {
514 tree ctx = DECL_CONTEXT (decl);
515 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
516 /* Disregard full specializations (c++/60999). */
517 && uses_template_parms (ctx)
518 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
519 }
520
521 if (inline_needs_template_parms (decl, nsdmi))
522 {
523 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
524 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
525
526 if (DECL_TEMPLATE_SPECIALIZATION (decl))
527 {
528 --levels;
529 parms = TREE_CHAIN (parms);
530 }
531
532 push_inline_template_parms_recursive (parms, levels);
533 }
534
535 /* Remember how many levels of template parameters we pushed so that
536 we can pop them later. */
537 inline_parm_levels.safe_push (levels);
538 }
539
540 /* Undo the effects of maybe_begin_member_template_processing. */
541
542 void
543 maybe_end_member_template_processing (void)
544 {
545 int i;
546 int last;
547
548 if (inline_parm_levels.length () == 0)
549 return;
550
551 last = inline_parm_levels.pop ();
552 for (i = 0; i < last; ++i)
553 {
554 --processing_template_decl;
555 current_template_parms = TREE_CHAIN (current_template_parms);
556 poplevel (0, 0, 0);
557 }
558 }
559
560 /* Return a new template argument vector which contains all of ARGS,
561 but has as its innermost set of arguments the EXTRA_ARGS. */
562
563 static tree
564 add_to_template_args (tree args, tree extra_args)
565 {
566 tree new_args;
567 int extra_depth;
568 int i;
569 int j;
570
571 if (args == NULL_TREE || extra_args == error_mark_node)
572 return extra_args;
573
574 extra_depth = TMPL_ARGS_DEPTH (extra_args);
575 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
576
577 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
578 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
579
580 for (j = 1; j <= extra_depth; ++j, ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
582
583 return new_args;
584 }
585
586 /* Like add_to_template_args, but only the outermost ARGS are added to
587 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
588 (EXTRA_ARGS) levels are added. This function is used to combine
589 the template arguments from a partial instantiation with the
590 template arguments used to attain the full instantiation from the
591 partial instantiation. */
592
593 tree
594 add_outermost_template_args (tree args, tree extra_args)
595 {
596 tree new_args;
597
598 /* If there are more levels of EXTRA_ARGS than there are ARGS,
599 something very fishy is going on. */
600 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
601
602 /* If *all* the new arguments will be the EXTRA_ARGS, just return
603 them. */
604 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
605 return extra_args;
606
607 /* For the moment, we make ARGS look like it contains fewer levels. */
608 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
609
610 new_args = add_to_template_args (args, extra_args);
611
612 /* Now, we restore ARGS to its full dimensions. */
613 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
614
615 return new_args;
616 }
617
618 /* Return the N levels of innermost template arguments from the ARGS. */
619
620 tree
621 get_innermost_template_args (tree args, int n)
622 {
623 tree new_args;
624 int extra_levels;
625 int i;
626
627 gcc_assert (n >= 0);
628
629 /* If N is 1, just return the innermost set of template arguments. */
630 if (n == 1)
631 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
632
633 /* If we're not removing anything, just return the arguments we were
634 given. */
635 extra_levels = TMPL_ARGS_DEPTH (args) - n;
636 gcc_assert (extra_levels >= 0);
637 if (extra_levels == 0)
638 return args;
639
640 /* Make a new set of arguments, not containing the outer arguments. */
641 new_args = make_tree_vec (n);
642 for (i = 1; i <= n; ++i)
643 SET_TMPL_ARGS_LEVEL (new_args, i,
644 TMPL_ARGS_LEVEL (args, i + extra_levels));
645
646 return new_args;
647 }
648
649 /* The inverse of get_innermost_template_args: Return all but the innermost
650 EXTRA_LEVELS levels of template arguments from the ARGS. */
651
652 static tree
653 strip_innermost_template_args (tree args, int extra_levels)
654 {
655 tree new_args;
656 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
657 int i;
658
659 gcc_assert (n >= 0);
660
661 /* If N is 1, just return the outermost set of template arguments. */
662 if (n == 1)
663 return TMPL_ARGS_LEVEL (args, 1);
664
665 /* If we're not removing anything, just return the arguments we were
666 given. */
667 gcc_assert (extra_levels >= 0);
668 if (extra_levels == 0)
669 return args;
670
671 /* Make a new set of arguments, not containing the inner arguments. */
672 new_args = make_tree_vec (n);
673 for (i = 1; i <= n; ++i)
674 SET_TMPL_ARGS_LEVEL (new_args, i,
675 TMPL_ARGS_LEVEL (args, i));
676
677 return new_args;
678 }
679
680 /* We've got a template header coming up; push to a new level for storing
681 the parms. */
682
683 void
684 begin_template_parm_list (void)
685 {
686 /* We use a non-tag-transparent scope here, which causes pushtag to
687 put tags in this scope, rather than in the enclosing class or
688 namespace scope. This is the right thing, since we want
689 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
690 global template class, push_template_decl handles putting the
691 TEMPLATE_DECL into top-level scope. For a nested template class,
692 e.g.:
693
694 template <class T> struct S1 {
695 template <class T> struct S2 {};
696 };
697
698 pushtag contains special code to insert the TEMPLATE_DECL for S2
699 at the right scope. */
700 begin_scope (sk_template_parms, NULL);
701 ++processing_template_decl;
702 ++processing_template_parmlist;
703 note_template_header (0);
704
705 /* Add a dummy parameter level while we process the parameter list. */
706 current_template_parms
707 = tree_cons (size_int (processing_template_decl),
708 make_tree_vec (0),
709 current_template_parms);
710 }
711
712 /* This routine is called when a specialization is declared. If it is
713 invalid to declare a specialization here, an error is reported and
714 false is returned, otherwise this routine will return true. */
715
716 static bool
717 check_specialization_scope (void)
718 {
719 tree scope = current_scope ();
720
721 /* [temp.expl.spec]
722
723 An explicit specialization shall be declared in the namespace of
724 which the template is a member, or, for member templates, in the
725 namespace of which the enclosing class or enclosing class
726 template is a member. An explicit specialization of a member
727 function, member class or static data member of a class template
728 shall be declared in the namespace of which the class template
729 is a member. */
730 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
731 {
732 error ("explicit specialization in non-namespace scope %qD", scope);
733 return false;
734 }
735
736 /* [temp.expl.spec]
737
738 In an explicit specialization declaration for a member of a class
739 template or a member template that appears in namespace scope,
740 the member template and some of its enclosing class templates may
741 remain unspecialized, except that the declaration shall not
742 explicitly specialize a class member template if its enclosing
743 class templates are not explicitly specialized as well. */
744 if (current_template_parms)
745 {
746 error ("enclosing class templates are not explicitly specialized");
747 return false;
748 }
749
750 return true;
751 }
752
753 /* We've just seen template <>. */
754
755 bool
756 begin_specialization (void)
757 {
758 begin_scope (sk_template_spec, NULL);
759 note_template_header (1);
760 return check_specialization_scope ();
761 }
762
763 /* Called at then end of processing a declaration preceded by
764 template<>. */
765
766 void
767 end_specialization (void)
768 {
769 finish_scope ();
770 reset_specialization ();
771 }
772
773 /* Any template <>'s that we have seen thus far are not referring to a
774 function specialization. */
775
776 void
777 reset_specialization (void)
778 {
779 processing_specialization = 0;
780 template_header_count = 0;
781 }
782
783 /* We've just seen a template header. If SPECIALIZATION is nonzero,
784 it was of the form template <>. */
785
786 static void
787 note_template_header (int specialization)
788 {
789 processing_specialization = specialization;
790 template_header_count++;
791 }
792
793 /* We're beginning an explicit instantiation. */
794
795 void
796 begin_explicit_instantiation (void)
797 {
798 gcc_assert (!processing_explicit_instantiation);
799 processing_explicit_instantiation = true;
800 }
801
802
803 void
804 end_explicit_instantiation (void)
805 {
806 gcc_assert (processing_explicit_instantiation);
807 processing_explicit_instantiation = false;
808 }
809
810 /* An explicit specialization or partial specialization of TMPL is being
811 declared. Check that the namespace in which the specialization is
812 occurring is permissible. Returns false iff it is invalid to
813 specialize TMPL in the current namespace. */
814
815 static bool
816 check_specialization_namespace (tree tmpl)
817 {
818 tree tpl_ns = decl_namespace_context (tmpl);
819
820 /* [tmpl.expl.spec]
821
822 An explicit specialization shall be declared in a namespace enclosing the
823 specialized template. An explicit specialization whose declarator-id is
824 not qualified shall be declared in the nearest enclosing namespace of the
825 template, or, if the namespace is inline (7.3.1), any namespace from its
826 enclosing namespace set. */
827 if (current_scope() != DECL_CONTEXT (tmpl)
828 && !at_namespace_scope_p ())
829 {
830 error ("specialization of %qD must appear at namespace scope", tmpl);
831 return false;
832 }
833
834 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
835 /* Same or enclosing namespace. */
836 return true;
837 else
838 {
839 auto_diagnostic_group d;
840 if (permerror (input_location,
841 "specialization of %qD in different namespace", tmpl))
842 inform (DECL_SOURCE_LOCATION (tmpl),
843 " from definition of %q#D", tmpl);
844 return false;
845 }
846 }
847
848 /* SPEC is an explicit instantiation. Check that it is valid to
849 perform this explicit instantiation in the current namespace. */
850
851 static void
852 check_explicit_instantiation_namespace (tree spec)
853 {
854 tree ns;
855
856 /* DR 275: An explicit instantiation shall appear in an enclosing
857 namespace of its template. */
858 ns = decl_namespace_context (spec);
859 if (!is_nested_namespace (current_namespace, ns))
860 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
861 "(which does not enclose namespace %qD)",
862 spec, current_namespace, ns);
863 }
864
865 /* Returns the type of a template specialization only if that
866 specialization needs to be defined. Otherwise (e.g., if the type has
867 already been defined), the function returns NULL_TREE. */
868
869 static tree
870 maybe_new_partial_specialization (tree type)
871 {
872 /* An implicit instantiation of an incomplete type implies
873 the definition of a new class template.
874
875 template<typename T>
876 struct S;
877
878 template<typename T>
879 struct S<T*>;
880
881 Here, S<T*> is an implicit instantiation of S whose type
882 is incomplete. */
883 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
884 return type;
885
886 /* It can also be the case that TYPE is a completed specialization.
887 Continuing the previous example, suppose we also declare:
888
889 template<typename T>
890 requires Integral<T>
891 struct S<T*>;
892
893 Here, S<T*> refers to the specialization S<T*> defined
894 above. However, we need to differentiate definitions because
895 we intend to define a new partial specialization. In this case,
896 we rely on the fact that the constraints are different for
897 this declaration than that above.
898
899 Note that we also get here for injected class names and
900 late-parsed template definitions. We must ensure that we
901 do not create new type declarations for those cases. */
902 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
903 {
904 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
905 tree args = CLASSTYPE_TI_ARGS (type);
906
907 /* If there are no template parameters, this cannot be a new
908 partial template specialization? */
909 if (!current_template_parms)
910 return NULL_TREE;
911
912 /* The injected-class-name is not a new partial specialization. */
913 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
914 return NULL_TREE;
915
916 /* If the constraints are not the same as those of the primary
917 then, we can probably create a new specialization. */
918 tree type_constr = current_template_constraints ();
919
920 if (type == TREE_TYPE (tmpl))
921 {
922 tree main_constr = get_constraints (tmpl);
923 if (equivalent_constraints (type_constr, main_constr))
924 return NULL_TREE;
925 }
926
927 /* Also, if there's a pre-existing specialization with matching
928 constraints, then this also isn't new. */
929 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
930 while (specs)
931 {
932 tree spec_tmpl = TREE_VALUE (specs);
933 tree spec_args = TREE_PURPOSE (specs);
934 tree spec_constr = get_constraints (spec_tmpl);
935 if (comp_template_args (args, spec_args)
936 && equivalent_constraints (type_constr, spec_constr))
937 return NULL_TREE;
938 specs = TREE_CHAIN (specs);
939 }
940
941 /* Create a new type node (and corresponding type decl)
942 for the newly declared specialization. */
943 tree t = make_class_type (TREE_CODE (type));
944 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
945 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
946
947 /* We only need a separate type node for storing the definition of this
948 partial specialization; uses of S<T*> are unconstrained, so all are
949 equivalent. So keep TYPE_CANONICAL the same. */
950 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
951
952 /* Build the corresponding type decl. */
953 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
954 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
955 DECL_SOURCE_LOCATION (d) = input_location;
956 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
957 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
958
959 return t;
960 }
961
962 return NULL_TREE;
963 }
964
965 /* The TYPE is being declared. If it is a template type, that means it
966 is a partial specialization. Do appropriate error-checking. */
967
968 tree
969 maybe_process_partial_specialization (tree type)
970 {
971 tree context;
972
973 if (type == error_mark_node)
974 return error_mark_node;
975
976 /* A lambda that appears in specialization context is not itself a
977 specialization. */
978 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
979 return type;
980
981 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
982 {
983 error ("name of class shadows template template parameter %qD",
984 TYPE_NAME (type));
985 return error_mark_node;
986 }
987
988 context = TYPE_CONTEXT (type);
989
990 if (TYPE_ALIAS_P (type))
991 {
992 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
993
994 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
995 error ("specialization of alias template %qD",
996 TI_TEMPLATE (tinfo));
997 else
998 error ("explicit specialization of non-template %qT", type);
999 return error_mark_node;
1000 }
1001 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1002 {
1003 /* This is for ordinary explicit specialization and partial
1004 specialization of a template class such as:
1005
1006 template <> class C<int>;
1007
1008 or:
1009
1010 template <class T> class C<T*>;
1011
1012 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1013
1014 if (tree t = maybe_new_partial_specialization (type))
1015 {
1016 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1017 && !at_namespace_scope_p ())
1018 return error_mark_node;
1019 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1020 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1021 if (processing_template_decl)
1022 {
1023 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1024 if (decl == error_mark_node)
1025 return error_mark_node;
1026 return TREE_TYPE (decl);
1027 }
1028 }
1029 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1030 error ("specialization of %qT after instantiation", type);
1031 else if (errorcount && !processing_specialization
1032 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1033 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1034 /* Trying to define a specialization either without a template<> header
1035 or in an inappropriate place. We've already given an error, so just
1036 bail now so we don't actually define the specialization. */
1037 return error_mark_node;
1038 }
1039 else if (CLASS_TYPE_P (type)
1040 && !CLASSTYPE_USE_TEMPLATE (type)
1041 && CLASSTYPE_TEMPLATE_INFO (type)
1042 && context && CLASS_TYPE_P (context)
1043 && CLASSTYPE_TEMPLATE_INFO (context))
1044 {
1045 /* This is for an explicit specialization of member class
1046 template according to [temp.expl.spec/18]:
1047
1048 template <> template <class U> class C<int>::D;
1049
1050 The context `C<int>' must be an implicit instantiation.
1051 Otherwise this is just a member class template declared
1052 earlier like:
1053
1054 template <> class C<int> { template <class U> class D; };
1055 template <> template <class U> class C<int>::D;
1056
1057 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1058 while in the second case, `C<int>::D' is a primary template
1059 and `C<T>::D' may not exist. */
1060
1061 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1062 && !COMPLETE_TYPE_P (type))
1063 {
1064 tree t;
1065 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1066
1067 if (current_namespace
1068 != decl_namespace_context (tmpl))
1069 {
1070 if (permerror (input_location,
1071 "specialization of %qD in different namespace",
1072 type))
1073 inform (DECL_SOURCE_LOCATION (tmpl),
1074 "from definition of %q#D", tmpl);
1075 }
1076
1077 /* Check for invalid specialization after instantiation:
1078
1079 template <> template <> class C<int>::D<int>;
1080 template <> template <class U> class C<int>::D; */
1081
1082 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1083 t; t = TREE_CHAIN (t))
1084 {
1085 tree inst = TREE_VALUE (t);
1086 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1087 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1088 {
1089 /* We already have a full specialization of this partial
1090 instantiation, or a full specialization has been
1091 looked up but not instantiated. Reassign it to the
1092 new member specialization template. */
1093 spec_entry elt;
1094 spec_entry *entry;
1095
1096 elt.tmpl = most_general_template (tmpl);
1097 elt.args = CLASSTYPE_TI_ARGS (inst);
1098 elt.spec = inst;
1099
1100 type_specializations->remove_elt (&elt);
1101
1102 elt.tmpl = tmpl;
1103 CLASSTYPE_TI_ARGS (inst)
1104 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1105
1106 spec_entry **slot
1107 = type_specializations->find_slot (&elt, INSERT);
1108 entry = ggc_alloc<spec_entry> ();
1109 *entry = elt;
1110 *slot = entry;
1111 }
1112 else
1113 /* But if we've had an implicit instantiation, that's a
1114 problem ([temp.expl.spec]/6). */
1115 error ("specialization %qT after instantiation %qT",
1116 type, inst);
1117 }
1118
1119 /* Mark TYPE as a specialization. And as a result, we only
1120 have one level of template argument for the innermost
1121 class template. */
1122 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1123 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1124 CLASSTYPE_TI_ARGS (type)
1125 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1126 }
1127 }
1128 else if (processing_specialization)
1129 {
1130 /* Someday C++0x may allow for enum template specialization. */
1131 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1132 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1133 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1134 "of %qD not allowed by ISO C++", type);
1135 else
1136 {
1137 error ("explicit specialization of non-template %qT", type);
1138 return error_mark_node;
1139 }
1140 }
1141
1142 return type;
1143 }
1144
1145 /* Returns nonzero if we can optimize the retrieval of specializations
1146 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1147 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1148
1149 static inline bool
1150 optimize_specialization_lookup_p (tree tmpl)
1151 {
1152 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1153 && DECL_CLASS_SCOPE_P (tmpl)
1154 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1155 parameter. */
1156 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1157 /* The optimized lookup depends on the fact that the
1158 template arguments for the member function template apply
1159 purely to the containing class, which is not true if the
1160 containing class is an explicit or partial
1161 specialization. */
1162 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1163 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1164 && !DECL_CONV_FN_P (tmpl)
1165 /* It is possible to have a template that is not a member
1166 template and is not a member of a template class:
1167
1168 template <typename T>
1169 struct S { friend A::f(); };
1170
1171 Here, the friend function is a template, but the context does
1172 not have template information. The optimized lookup relies
1173 on having ARGS be the template arguments for both the class
1174 and the function template. */
1175 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1176 }
1177
1178 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1179 gone through coerce_template_parms by now. */
1180
1181 static void
1182 verify_unstripped_args_1 (tree inner)
1183 {
1184 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1185 {
1186 tree arg = TREE_VEC_ELT (inner, i);
1187 if (TREE_CODE (arg) == TEMPLATE_DECL)
1188 /* OK */;
1189 else if (TYPE_P (arg))
1190 gcc_assert (strip_typedefs (arg, NULL) == arg);
1191 else if (ARGUMENT_PACK_P (arg))
1192 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1193 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1194 /* Allow typedefs on the type of a non-type argument, since a
1195 parameter can have them. */;
1196 else
1197 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1198 }
1199 }
1200
1201 static void
1202 verify_unstripped_args (tree args)
1203 {
1204 ++processing_template_decl;
1205 if (!any_dependent_template_arguments_p (args))
1206 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1207 --processing_template_decl;
1208 }
1209
1210 /* Retrieve the specialization (in the sense of [temp.spec] - a
1211 specialization is either an instantiation or an explicit
1212 specialization) of TMPL for the given template ARGS. If there is
1213 no such specialization, return NULL_TREE. The ARGS are a vector of
1214 arguments, or a vector of vectors of arguments, in the case of
1215 templates with more than one level of parameters.
1216
1217 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1218 then we search for a partial specialization matching ARGS. This
1219 parameter is ignored if TMPL is not a class template.
1220
1221 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1222 result is a NONTYPE_ARGUMENT_PACK. */
1223
1224 static tree
1225 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1226 {
1227 if (tmpl == NULL_TREE)
1228 return NULL_TREE;
1229
1230 if (args == error_mark_node)
1231 return NULL_TREE;
1232
1233 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1234 || TREE_CODE (tmpl) == FIELD_DECL);
1235
1236 /* There should be as many levels of arguments as there are
1237 levels of parameters. */
1238 gcc_assert (TMPL_ARGS_DEPTH (args)
1239 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1240 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1241 : template_class_depth (DECL_CONTEXT (tmpl))));
1242
1243 if (flag_checking)
1244 verify_unstripped_args (args);
1245
1246 /* Lambda functions in templates aren't instantiated normally, but through
1247 tsubst_lambda_expr. */
1248 if (lambda_fn_in_template_p (tmpl))
1249 return NULL_TREE;
1250
1251 if (optimize_specialization_lookup_p (tmpl))
1252 {
1253 /* The template arguments actually apply to the containing
1254 class. Find the class specialization with those
1255 arguments. */
1256 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1257 tree class_specialization
1258 = retrieve_specialization (class_template, args, 0);
1259 if (!class_specialization)
1260 return NULL_TREE;
1261
1262 /* Find the instance of TMPL. */
1263 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1264 for (ovl_iterator iter (fns); iter; ++iter)
1265 {
1266 tree fn = *iter;
1267 if (tree ti = get_template_info (fn))
1268 if (TI_TEMPLATE (ti) == tmpl
1269 /* using-declarations can bring in a different
1270 instantiation of tmpl as a member of a different
1271 instantiation of tmpl's class. We don't want those
1272 here. */
1273 && DECL_CONTEXT (fn) == class_specialization)
1274 return fn;
1275 }
1276 return NULL_TREE;
1277 }
1278 else
1279 {
1280 spec_entry *found;
1281 spec_entry elt;
1282 spec_hash_table *specializations;
1283
1284 elt.tmpl = tmpl;
1285 elt.args = args;
1286 elt.spec = NULL_TREE;
1287
1288 if (DECL_CLASS_TEMPLATE_P (tmpl))
1289 specializations = type_specializations;
1290 else
1291 specializations = decl_specializations;
1292
1293 if (hash == 0)
1294 hash = spec_hasher::hash (&elt);
1295 found = specializations->find_with_hash (&elt, hash);
1296 if (found)
1297 return found->spec;
1298 }
1299
1300 return NULL_TREE;
1301 }
1302
1303 /* Like retrieve_specialization, but for local declarations. */
1304
1305 tree
1306 retrieve_local_specialization (tree tmpl)
1307 {
1308 if (local_specializations == NULL)
1309 return NULL_TREE;
1310
1311 tree *slot = local_specializations->get (tmpl);
1312 return slot ? *slot : NULL_TREE;
1313 }
1314
1315 /* Returns nonzero iff DECL is a specialization of TMPL. */
1316
1317 int
1318 is_specialization_of (tree decl, tree tmpl)
1319 {
1320 tree t;
1321
1322 if (TREE_CODE (decl) == FUNCTION_DECL)
1323 {
1324 for (t = decl;
1325 t != NULL_TREE;
1326 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1327 if (t == tmpl)
1328 return 1;
1329 }
1330 else
1331 {
1332 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1333
1334 for (t = TREE_TYPE (decl);
1335 t != NULL_TREE;
1336 t = CLASSTYPE_USE_TEMPLATE (t)
1337 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1338 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1339 return 1;
1340 }
1341
1342 return 0;
1343 }
1344
1345 /* Returns nonzero iff DECL is a specialization of friend declaration
1346 FRIEND_DECL according to [temp.friend]. */
1347
1348 bool
1349 is_specialization_of_friend (tree decl, tree friend_decl)
1350 {
1351 bool need_template = true;
1352 int template_depth;
1353
1354 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1355 || TREE_CODE (decl) == TYPE_DECL);
1356
1357 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1358 of a template class, we want to check if DECL is a specialization
1359 if this. */
1360 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1361 && DECL_TEMPLATE_INFO (friend_decl)
1362 && !DECL_USE_TEMPLATE (friend_decl))
1363 {
1364 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1365 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1366 need_template = false;
1367 }
1368 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1369 && !PRIMARY_TEMPLATE_P (friend_decl))
1370 need_template = false;
1371
1372 /* There is nothing to do if this is not a template friend. */
1373 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1374 return false;
1375
1376 if (is_specialization_of (decl, friend_decl))
1377 return true;
1378
1379 /* [temp.friend/6]
1380 A member of a class template may be declared to be a friend of a
1381 non-template class. In this case, the corresponding member of
1382 every specialization of the class template is a friend of the
1383 class granting friendship.
1384
1385 For example, given a template friend declaration
1386
1387 template <class T> friend void A<T>::f();
1388
1389 the member function below is considered a friend
1390
1391 template <> struct A<int> {
1392 void f();
1393 };
1394
1395 For this type of template friend, TEMPLATE_DEPTH below will be
1396 nonzero. To determine if DECL is a friend of FRIEND, we first
1397 check if the enclosing class is a specialization of another. */
1398
1399 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1400 if (template_depth
1401 && DECL_CLASS_SCOPE_P (decl)
1402 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1403 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1404 {
1405 /* Next, we check the members themselves. In order to handle
1406 a few tricky cases, such as when FRIEND_DECL's are
1407
1408 template <class T> friend void A<T>::g(T t);
1409 template <class T> template <T t> friend void A<T>::h();
1410
1411 and DECL's are
1412
1413 void A<int>::g(int);
1414 template <int> void A<int>::h();
1415
1416 we need to figure out ARGS, the template arguments from
1417 the context of DECL. This is required for template substitution
1418 of `T' in the function parameter of `g' and template parameter
1419 of `h' in the above examples. Here ARGS corresponds to `int'. */
1420
1421 tree context = DECL_CONTEXT (decl);
1422 tree args = NULL_TREE;
1423 int current_depth = 0;
1424
1425 while (current_depth < template_depth)
1426 {
1427 if (CLASSTYPE_TEMPLATE_INFO (context))
1428 {
1429 if (current_depth == 0)
1430 args = TYPE_TI_ARGS (context);
1431 else
1432 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1433 current_depth++;
1434 }
1435 context = TYPE_CONTEXT (context);
1436 }
1437
1438 if (TREE_CODE (decl) == FUNCTION_DECL)
1439 {
1440 bool is_template;
1441 tree friend_type;
1442 tree decl_type;
1443 tree friend_args_type;
1444 tree decl_args_type;
1445
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1447 non-templates. */
1448 is_template = DECL_TEMPLATE_INFO (decl)
1449 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1450 if (need_template ^ is_template)
1451 return false;
1452 else if (is_template)
1453 {
1454 /* If both are templates, check template parameter list. */
1455 tree friend_parms
1456 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1457 args, tf_none);
1458 if (!comp_template_parms
1459 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1460 friend_parms))
1461 return false;
1462
1463 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1464 }
1465 else
1466 decl_type = TREE_TYPE (decl);
1467
1468 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1469 tf_none, NULL_TREE);
1470 if (friend_type == error_mark_node)
1471 return false;
1472
1473 /* Check if return types match. */
1474 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1475 return false;
1476
1477 /* Check if function parameter types match, ignoring the
1478 `this' parameter. */
1479 friend_args_type = TYPE_ARG_TYPES (friend_type);
1480 decl_args_type = TYPE_ARG_TYPES (decl_type);
1481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1482 friend_args_type = TREE_CHAIN (friend_args_type);
1483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1484 decl_args_type = TREE_CHAIN (decl_args_type);
1485
1486 return compparms (decl_args_type, friend_args_type);
1487 }
1488 else
1489 {
1490 /* DECL is a TYPE_DECL */
1491 bool is_template;
1492 tree decl_type = TREE_TYPE (decl);
1493
1494 /* Make sure that both DECL and FRIEND_DECL are templates or
1495 non-templates. */
1496 is_template
1497 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1498 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1499
1500 if (need_template ^ is_template)
1501 return false;
1502 else if (is_template)
1503 {
1504 tree friend_parms;
1505 /* If both are templates, check the name of the two
1506 TEMPLATE_DECL's first because is_friend didn't. */
1507 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1508 != DECL_NAME (friend_decl))
1509 return false;
1510
1511 /* Now check template parameter list. */
1512 friend_parms
1513 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1514 args, tf_none);
1515 return comp_template_parms
1516 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1517 friend_parms);
1518 }
1519 else
1520 return (DECL_NAME (decl)
1521 == DECL_NAME (friend_decl));
1522 }
1523 }
1524 return false;
1525 }
1526
1527 /* Register the specialization SPEC as a specialization of TMPL with
1528 the indicated ARGS. IS_FRIEND indicates whether the specialization
1529 is actually just a friend declaration. ATTRLIST is the list of
1530 attributes that the specialization is declared with or NULL when
1531 it isn't. Returns SPEC, or an equivalent prior declaration, if
1532 available.
1533
1534 We also store instantiations of field packs in the hash table, even
1535 though they are not themselves templates, to make lookup easier. */
1536
1537 static tree
1538 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1539 hashval_t hash)
1540 {
1541 tree fn;
1542 spec_entry **slot = NULL;
1543 spec_entry elt;
1544
1545 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1546 || (TREE_CODE (tmpl) == FIELD_DECL
1547 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1548
1549 if (TREE_CODE (spec) == FUNCTION_DECL
1550 && uses_template_parms (DECL_TI_ARGS (spec)))
1551 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1552 register it; we want the corresponding TEMPLATE_DECL instead.
1553 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1554 the more obvious `uses_template_parms (spec)' to avoid problems
1555 with default function arguments. In particular, given
1556 something like this:
1557
1558 template <class T> void f(T t1, T t = T())
1559
1560 the default argument expression is not substituted for in an
1561 instantiation unless and until it is actually needed. */
1562 return spec;
1563
1564 if (optimize_specialization_lookup_p (tmpl))
1565 /* We don't put these specializations in the hash table, but we might
1566 want to give an error about a mismatch. */
1567 fn = retrieve_specialization (tmpl, args, 0);
1568 else
1569 {
1570 elt.tmpl = tmpl;
1571 elt.args = args;
1572 elt.spec = spec;
1573
1574 if (hash == 0)
1575 hash = spec_hasher::hash (&elt);
1576
1577 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1578 if (*slot)
1579 fn = (*slot)->spec;
1580 else
1581 fn = NULL_TREE;
1582 }
1583
1584 /* We can sometimes try to re-register a specialization that we've
1585 already got. In particular, regenerate_decl_from_template calls
1586 duplicate_decls which will update the specialization list. But,
1587 we'll still get called again here anyhow. It's more convenient
1588 to simply allow this than to try to prevent it. */
1589 if (fn == spec)
1590 return spec;
1591 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1592 {
1593 if (DECL_TEMPLATE_INSTANTIATION (fn))
1594 {
1595 if (DECL_ODR_USED (fn)
1596 || DECL_EXPLICIT_INSTANTIATION (fn))
1597 {
1598 error ("specialization of %qD after instantiation",
1599 fn);
1600 return error_mark_node;
1601 }
1602 else
1603 {
1604 tree clone;
1605 /* This situation should occur only if the first
1606 specialization is an implicit instantiation, the
1607 second is an explicit specialization, and the
1608 implicit instantiation has not yet been used. That
1609 situation can occur if we have implicitly
1610 instantiated a member function and then specialized
1611 it later.
1612
1613 We can also wind up here if a friend declaration that
1614 looked like an instantiation turns out to be a
1615 specialization:
1616
1617 template <class T> void foo(T);
1618 class S { friend void foo<>(int) };
1619 template <> void foo(int);
1620
1621 We transform the existing DECL in place so that any
1622 pointers to it become pointers to the updated
1623 declaration.
1624
1625 If there was a definition for the template, but not
1626 for the specialization, we want this to look as if
1627 there were no definition, and vice versa. */
1628 DECL_INITIAL (fn) = NULL_TREE;
1629 duplicate_decls (spec, fn, is_friend);
1630 /* The call to duplicate_decls will have applied
1631 [temp.expl.spec]:
1632
1633 An explicit specialization of a function template
1634 is inline only if it is explicitly declared to be,
1635 and independently of whether its function template
1636 is.
1637
1638 to the primary function; now copy the inline bits to
1639 the various clones. */
1640 FOR_EACH_CLONE (clone, fn)
1641 {
1642 DECL_DECLARED_INLINE_P (clone)
1643 = DECL_DECLARED_INLINE_P (fn);
1644 DECL_SOURCE_LOCATION (clone)
1645 = DECL_SOURCE_LOCATION (fn);
1646 DECL_DELETED_FN (clone)
1647 = DECL_DELETED_FN (fn);
1648 }
1649 check_specialization_namespace (tmpl);
1650
1651 return fn;
1652 }
1653 }
1654 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1655 {
1656 tree dd = duplicate_decls (spec, fn, is_friend);
1657 if (dd == error_mark_node)
1658 /* We've already complained in duplicate_decls. */
1659 return error_mark_node;
1660
1661 if (dd == NULL_TREE && DECL_INITIAL (spec))
1662 /* Dup decl failed, but this is a new definition. Set the
1663 line number so any errors match this new
1664 definition. */
1665 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1666
1667 return fn;
1668 }
1669 }
1670 else if (fn)
1671 return duplicate_decls (spec, fn, is_friend);
1672
1673 /* A specialization must be declared in the same namespace as the
1674 template it is specializing. */
1675 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1676 && !check_specialization_namespace (tmpl))
1677 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1678
1679 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1680 {
1681 spec_entry *entry = ggc_alloc<spec_entry> ();
1682 gcc_assert (tmpl && args && spec);
1683 *entry = elt;
1684 *slot = entry;
1685 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1686 && PRIMARY_TEMPLATE_P (tmpl)
1687 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1688 || variable_template_p (tmpl))
1689 /* If TMPL is a forward declaration of a template function, keep a list
1690 of all specializations in case we need to reassign them to a friend
1691 template later in tsubst_friend_function.
1692
1693 Also keep a list of all variable template instantiations so that
1694 process_partial_specialization can check whether a later partial
1695 specialization would have used it. */
1696 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1697 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1698 }
1699
1700 return spec;
1701 }
1702
1703 /* Returns true iff two spec_entry nodes are equivalent. */
1704
1705 int comparing_specializations;
1706
1707 bool
1708 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1709 {
1710 int equal;
1711
1712 ++comparing_specializations;
1713 equal = (e1->tmpl == e2->tmpl
1714 && comp_template_args (e1->args, e2->args));
1715 if (equal && flag_concepts
1716 /* tmpl could be a FIELD_DECL for a capture pack. */
1717 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1718 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1719 && uses_template_parms (e1->args))
1720 {
1721 /* Partial specializations of a variable template can be distinguished by
1722 constraints. */
1723 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1724 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1725 equal = equivalent_constraints (c1, c2);
1726 }
1727 --comparing_specializations;
1728
1729 return equal;
1730 }
1731
1732 /* Returns a hash for a template TMPL and template arguments ARGS. */
1733
1734 static hashval_t
1735 hash_tmpl_and_args (tree tmpl, tree args)
1736 {
1737 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1738 return iterative_hash_template_arg (args, val);
1739 }
1740
1741 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1742 ignoring SPEC. */
1743
1744 hashval_t
1745 spec_hasher::hash (spec_entry *e)
1746 {
1747 return hash_tmpl_and_args (e->tmpl, e->args);
1748 }
1749
1750 /* Recursively calculate a hash value for a template argument ARG, for use
1751 in the hash tables of template specializations. We must be
1752 careful to (at least) skip the same entities template_args_equal
1753 does. */
1754
1755 hashval_t
1756 iterative_hash_template_arg (tree arg, hashval_t val)
1757 {
1758 if (arg == NULL_TREE)
1759 return iterative_hash_object (arg, val);
1760
1761 if (!TYPE_P (arg))
1762 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1763 while (CONVERT_EXPR_P (arg)
1764 || TREE_CODE (arg) == NON_LVALUE_EXPR
1765 || class_nttp_const_wrapper_p (arg))
1766 arg = TREE_OPERAND (arg, 0);
1767
1768 enum tree_code code = TREE_CODE (arg);
1769
1770 val = iterative_hash_object (code, val);
1771
1772 switch (code)
1773 {
1774 case ARGUMENT_PACK_SELECT:
1775 gcc_unreachable ();
1776
1777 case ERROR_MARK:
1778 return val;
1779
1780 case IDENTIFIER_NODE:
1781 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1782
1783 case TREE_VEC:
1784 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1785 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1786 return val;
1787
1788 case TYPE_PACK_EXPANSION:
1789 case EXPR_PACK_EXPANSION:
1790 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1791 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1792
1793 case TYPE_ARGUMENT_PACK:
1794 case NONTYPE_ARGUMENT_PACK:
1795 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1796
1797 case TREE_LIST:
1798 for (; arg; arg = TREE_CHAIN (arg))
1799 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1800 return val;
1801
1802 case OVERLOAD:
1803 for (lkp_iterator iter (arg); iter; ++iter)
1804 val = iterative_hash_template_arg (*iter, val);
1805 return val;
1806
1807 case CONSTRUCTOR:
1808 {
1809 tree field, value;
1810 unsigned i;
1811 iterative_hash_template_arg (TREE_TYPE (arg), val);
1812 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1813 {
1814 val = iterative_hash_template_arg (field, val);
1815 val = iterative_hash_template_arg (value, val);
1816 }
1817 return val;
1818 }
1819
1820 case PARM_DECL:
1821 if (!DECL_ARTIFICIAL (arg))
1822 {
1823 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1824 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1825 }
1826 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1827
1828 case TARGET_EXPR:
1829 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1830
1831 case PTRMEM_CST:
1832 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1833 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1834
1835 case TEMPLATE_PARM_INDEX:
1836 val = iterative_hash_template_arg
1837 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1838 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1839 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1840
1841 case TRAIT_EXPR:
1842 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1843 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1844 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1845
1846 case BASELINK:
1847 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1848 val);
1849 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1850 val);
1851
1852 case MODOP_EXPR:
1853 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1854 code = TREE_CODE (TREE_OPERAND (arg, 1));
1855 val = iterative_hash_object (code, val);
1856 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1857
1858 case LAMBDA_EXPR:
1859 /* [temp.over.link] Two lambda-expressions are never considered
1860 equivalent.
1861
1862 So just hash the closure type. */
1863 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1864
1865 case CAST_EXPR:
1866 case IMPLICIT_CONV_EXPR:
1867 case STATIC_CAST_EXPR:
1868 case REINTERPRET_CAST_EXPR:
1869 case CONST_CAST_EXPR:
1870 case DYNAMIC_CAST_EXPR:
1871 case NEW_EXPR:
1872 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1873 /* Now hash operands as usual. */
1874 break;
1875
1876 case CALL_EXPR:
1877 {
1878 tree fn = CALL_EXPR_FN (arg);
1879 if (tree name = dependent_name (fn))
1880 {
1881 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1882 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1883 fn = name;
1884 }
1885 val = iterative_hash_template_arg (fn, val);
1886 call_expr_arg_iterator ai;
1887 for (tree x = first_call_expr_arg (arg, &ai); x;
1888 x = next_call_expr_arg (&ai))
1889 val = iterative_hash_template_arg (x, val);
1890 return val;
1891 }
1892
1893 default:
1894 break;
1895 }
1896
1897 char tclass = TREE_CODE_CLASS (code);
1898 switch (tclass)
1899 {
1900 case tcc_type:
1901 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1902 {
1903 // We want an alias specialization that survived strip_typedefs
1904 // to hash differently from its TYPE_CANONICAL, to avoid hash
1905 // collisions that compare as different in template_args_equal.
1906 // These could be dependent specializations that strip_typedefs
1907 // left alone, or untouched specializations because
1908 // coerce_template_parms returns the unconverted template
1909 // arguments if it sees incomplete argument packs.
1910 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1911 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1912 }
1913
1914 switch (TREE_CODE (arg))
1915 {
1916 case TEMPLATE_TEMPLATE_PARM:
1917 {
1918 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1919
1920 /* Do not recurse with TPI directly, as that is unbounded
1921 recursion. */
1922 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1923 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1924 }
1925 break;
1926
1927 case DECLTYPE_TYPE:
1928 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1929 break;
1930
1931 default:
1932 if (tree canonical = TYPE_CANONICAL (arg))
1933 val = iterative_hash_object (TYPE_HASH (canonical), val);
1934 break;
1935 }
1936
1937 return val;
1938
1939 case tcc_declaration:
1940 case tcc_constant:
1941 return iterative_hash_expr (arg, val);
1942
1943 default:
1944 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1945 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1946 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1947 return val;
1948 }
1949
1950 gcc_unreachable ();
1951 return 0;
1952 }
1953
1954 /* Unregister the specialization SPEC as a specialization of TMPL.
1955 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1956 if the SPEC was listed as a specialization of TMPL.
1957
1958 Note that SPEC has been ggc_freed, so we can't look inside it. */
1959
1960 bool
1961 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1962 {
1963 spec_entry *entry;
1964 spec_entry elt;
1965
1966 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1967 elt.args = TI_ARGS (tinfo);
1968 elt.spec = NULL_TREE;
1969
1970 entry = decl_specializations->find (&elt);
1971 if (entry != NULL)
1972 {
1973 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1974 gcc_assert (new_spec != NULL_TREE);
1975 entry->spec = new_spec;
1976 return 1;
1977 }
1978
1979 return 0;
1980 }
1981
1982 /* Like register_specialization, but for local declarations. We are
1983 registering SPEC, an instantiation of TMPL. */
1984
1985 void
1986 register_local_specialization (tree spec, tree tmpl)
1987 {
1988 gcc_assert (tmpl != spec);
1989 local_specializations->put (tmpl, spec);
1990 }
1991
1992 /* TYPE is a class type. Returns true if TYPE is an explicitly
1993 specialized class. */
1994
1995 bool
1996 explicit_class_specialization_p (tree type)
1997 {
1998 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1999 return false;
2000 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2001 }
2002
2003 /* Print the list of functions at FNS, going through all the overloads
2004 for each element of the list. Alternatively, FNS cannot be a
2005 TREE_LIST, in which case it will be printed together with all the
2006 overloads.
2007
2008 MORE and *STR should respectively be FALSE and NULL when the function
2009 is called from the outside. They are used internally on recursive
2010 calls. print_candidates manages the two parameters and leaves NULL
2011 in *STR when it ends. */
2012
2013 static void
2014 print_candidates_1 (tree fns, char **str, bool more = false)
2015 {
2016 if (TREE_CODE (fns) == TREE_LIST)
2017 for (; fns; fns = TREE_CHAIN (fns))
2018 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2019 else
2020 for (lkp_iterator iter (fns); iter;)
2021 {
2022 tree cand = *iter;
2023 ++iter;
2024
2025 const char *pfx = *str;
2026 if (!pfx)
2027 {
2028 if (more || iter)
2029 pfx = _("candidates are:");
2030 else
2031 pfx = _("candidate is:");
2032 *str = get_spaces (pfx);
2033 }
2034 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2035 }
2036 }
2037
2038 /* Print the list of candidate FNS in an error message. FNS can also
2039 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2040
2041 void
2042 print_candidates (tree fns)
2043 {
2044 char *str = NULL;
2045 print_candidates_1 (fns, &str);
2046 free (str);
2047 }
2048
2049 /* Get a (possibly) constrained template declaration for the
2050 purpose of ordering candidates. */
2051 static tree
2052 get_template_for_ordering (tree list)
2053 {
2054 gcc_assert (TREE_CODE (list) == TREE_LIST);
2055 tree f = TREE_VALUE (list);
2056 if (tree ti = DECL_TEMPLATE_INFO (f))
2057 return TI_TEMPLATE (ti);
2058 return f;
2059 }
2060
2061 /* Among candidates having the same signature, return the
2062 most constrained or NULL_TREE if there is no best candidate.
2063 If the signatures of candidates vary (e.g., template
2064 specialization vs. member function), then there can be no
2065 most constrained.
2066
2067 Note that we don't compare constraints on the functions
2068 themselves, but rather those of their templates. */
2069 static tree
2070 most_constrained_function (tree candidates)
2071 {
2072 // Try to find the best candidate in a first pass.
2073 tree champ = candidates;
2074 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2075 {
2076 int winner = more_constrained (get_template_for_ordering (champ),
2077 get_template_for_ordering (c));
2078 if (winner == -1)
2079 champ = c; // The candidate is more constrained
2080 else if (winner == 0)
2081 return NULL_TREE; // Neither is more constrained
2082 }
2083
2084 // Verify that the champ is better than previous candidates.
2085 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2086 if (!more_constrained (get_template_for_ordering (champ),
2087 get_template_for_ordering (c)))
2088 return NULL_TREE;
2089 }
2090
2091 return champ;
2092 }
2093
2094
2095 /* Returns the template (one of the functions given by TEMPLATE_ID)
2096 which can be specialized to match the indicated DECL with the
2097 explicit template args given in TEMPLATE_ID. The DECL may be
2098 NULL_TREE if none is available. In that case, the functions in
2099 TEMPLATE_ID are non-members.
2100
2101 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2102 specialization of a member template.
2103
2104 The TEMPLATE_COUNT is the number of references to qualifying
2105 template classes that appeared in the name of the function. See
2106 check_explicit_specialization for a more accurate description.
2107
2108 TSK indicates what kind of template declaration (if any) is being
2109 declared. TSK_TEMPLATE indicates that the declaration given by
2110 DECL, though a FUNCTION_DECL, has template parameters, and is
2111 therefore a template function.
2112
2113 The template args (those explicitly specified and those deduced)
2114 are output in a newly created vector *TARGS_OUT.
2115
2116 If it is impossible to determine the result, an error message is
2117 issued. The error_mark_node is returned to indicate failure. */
2118
2119 static tree
2120 determine_specialization (tree template_id,
2121 tree decl,
2122 tree* targs_out,
2123 int need_member_template,
2124 int template_count,
2125 tmpl_spec_kind tsk)
2126 {
2127 tree fns;
2128 tree targs;
2129 tree explicit_targs;
2130 tree candidates = NULL_TREE;
2131
2132 /* A TREE_LIST of templates of which DECL may be a specialization.
2133 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2134 corresponding TREE_PURPOSE is the set of template arguments that,
2135 when used to instantiate the template, would produce a function
2136 with the signature of DECL. */
2137 tree templates = NULL_TREE;
2138 int header_count;
2139 cp_binding_level *b;
2140
2141 *targs_out = NULL_TREE;
2142
2143 if (template_id == error_mark_node || decl == error_mark_node)
2144 return error_mark_node;
2145
2146 /* We shouldn't be specializing a member template of an
2147 unspecialized class template; we already gave an error in
2148 check_specialization_scope, now avoid crashing. */
2149 if (!VAR_P (decl)
2150 && template_count && DECL_CLASS_SCOPE_P (decl)
2151 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2152 {
2153 gcc_assert (errorcount);
2154 return error_mark_node;
2155 }
2156
2157 fns = TREE_OPERAND (template_id, 0);
2158 explicit_targs = TREE_OPERAND (template_id, 1);
2159
2160 if (fns == error_mark_node)
2161 return error_mark_node;
2162
2163 /* Check for baselinks. */
2164 if (BASELINK_P (fns))
2165 fns = BASELINK_FUNCTIONS (fns);
2166
2167 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2168 {
2169 error_at (DECL_SOURCE_LOCATION (decl),
2170 "%qD is not a function template", fns);
2171 return error_mark_node;
2172 }
2173 else if (VAR_P (decl) && !variable_template_p (fns))
2174 {
2175 error ("%qD is not a variable template", fns);
2176 return error_mark_node;
2177 }
2178
2179 /* Count the number of template headers specified for this
2180 specialization. */
2181 header_count = 0;
2182 for (b = current_binding_level;
2183 b->kind == sk_template_parms;
2184 b = b->level_chain)
2185 ++header_count;
2186
2187 tree orig_fns = fns;
2188
2189 if (variable_template_p (fns))
2190 {
2191 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2192 targs = coerce_template_parms (parms, explicit_targs, fns,
2193 tf_warning_or_error,
2194 /*req_all*/true, /*use_defarg*/true);
2195 if (targs != error_mark_node)
2196 templates = tree_cons (targs, fns, templates);
2197 }
2198 else for (lkp_iterator iter (fns); iter; ++iter)
2199 {
2200 tree fn = *iter;
2201
2202 if (TREE_CODE (fn) == TEMPLATE_DECL)
2203 {
2204 tree decl_arg_types;
2205 tree fn_arg_types;
2206 tree insttype;
2207
2208 /* In case of explicit specialization, we need to check if
2209 the number of template headers appearing in the specialization
2210 is correct. This is usually done in check_explicit_specialization,
2211 but the check done there cannot be exhaustive when specializing
2212 member functions. Consider the following code:
2213
2214 template <> void A<int>::f(int);
2215 template <> template <> void A<int>::f(int);
2216
2217 Assuming that A<int> is not itself an explicit specialization
2218 already, the first line specializes "f" which is a non-template
2219 member function, whilst the second line specializes "f" which
2220 is a template member function. So both lines are syntactically
2221 correct, and check_explicit_specialization does not reject
2222 them.
2223
2224 Here, we can do better, as we are matching the specialization
2225 against the declarations. We count the number of template
2226 headers, and we check if they match TEMPLATE_COUNT + 1
2227 (TEMPLATE_COUNT is the number of qualifying template classes,
2228 plus there must be another header for the member template
2229 itself).
2230
2231 Notice that if header_count is zero, this is not a
2232 specialization but rather a template instantiation, so there
2233 is no check we can perform here. */
2234 if (header_count && header_count != template_count + 1)
2235 continue;
2236
2237 /* Check that the number of template arguments at the
2238 innermost level for DECL is the same as for FN. */
2239 if (current_binding_level->kind == sk_template_parms
2240 && !current_binding_level->explicit_spec_p
2241 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2242 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2243 (current_template_parms))))
2244 continue;
2245
2246 /* DECL might be a specialization of FN. */
2247 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2248 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2249
2250 /* For a non-static member function, we need to make sure
2251 that the const qualification is the same. Since
2252 get_bindings does not try to merge the "this" parameter,
2253 we must do the comparison explicitly. */
2254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2255 {
2256 if (!same_type_p (TREE_VALUE (fn_arg_types),
2257 TREE_VALUE (decl_arg_types)))
2258 continue;
2259
2260 /* And the ref-qualification. */
2261 if (type_memfn_rqual (TREE_TYPE (decl))
2262 != type_memfn_rqual (TREE_TYPE (fn)))
2263 continue;
2264 }
2265
2266 /* Skip the "this" parameter and, for constructors of
2267 classes with virtual bases, the VTT parameter. A
2268 full specialization of a constructor will have a VTT
2269 parameter, but a template never will. */
2270 decl_arg_types
2271 = skip_artificial_parms_for (decl, decl_arg_types);
2272 fn_arg_types
2273 = skip_artificial_parms_for (fn, fn_arg_types);
2274
2275 /* Function templates cannot be specializations; there are
2276 no partial specializations of functions. Therefore, if
2277 the type of DECL does not match FN, there is no
2278 match.
2279
2280 Note that it should never be the case that we have both
2281 candidates added here, and for regular member functions
2282 below. */
2283 if (tsk == tsk_template)
2284 {
2285 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2286 current_template_parms))
2287 continue;
2288 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2289 TREE_TYPE (TREE_TYPE (fn))))
2290 continue;
2291 if (!compparms (fn_arg_types, decl_arg_types))
2292 continue;
2293
2294 tree freq = get_trailing_function_requirements (fn);
2295 tree dreq = get_trailing_function_requirements (decl);
2296 if (!freq != !dreq)
2297 continue;
2298 if (freq)
2299 {
2300 tree fargs = DECL_TI_ARGS (fn);
2301 tsubst_flags_t complain = tf_none;
2302 freq = tsubst_constraint (freq, fargs, complain, fn);
2303 if (!cp_tree_equal (freq, dreq))
2304 continue;
2305 }
2306
2307 candidates = tree_cons (NULL_TREE, fn, candidates);
2308 continue;
2309 }
2310
2311 /* See whether this function might be a specialization of this
2312 template. Suppress access control because we might be trying
2313 to make this specialization a friend, and we have already done
2314 access control for the declaration of the specialization. */
2315 push_deferring_access_checks (dk_no_check);
2316 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2317 pop_deferring_access_checks ();
2318
2319 if (!targs)
2320 /* We cannot deduce template arguments that when used to
2321 specialize TMPL will produce DECL. */
2322 continue;
2323
2324 if (uses_template_parms (targs))
2325 /* We deduced something involving 'auto', which isn't a valid
2326 template argument. */
2327 continue;
2328
2329 /* Remove, from the set of candidates, all those functions
2330 whose constraints are not satisfied. */
2331 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2332 continue;
2333
2334 // Then, try to form the new function type.
2335 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2336 if (insttype == error_mark_node)
2337 continue;
2338 fn_arg_types
2339 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2340 if (!compparms (fn_arg_types, decl_arg_types))
2341 continue;
2342
2343 /* Save this template, and the arguments deduced. */
2344 templates = tree_cons (targs, fn, templates);
2345 }
2346 else if (need_member_template)
2347 /* FN is an ordinary member function, and we need a
2348 specialization of a member template. */
2349 ;
2350 else if (TREE_CODE (fn) != FUNCTION_DECL)
2351 /* We can get IDENTIFIER_NODEs here in certain erroneous
2352 cases. */
2353 ;
2354 else if (!DECL_FUNCTION_MEMBER_P (fn))
2355 /* This is just an ordinary non-member function. Nothing can
2356 be a specialization of that. */
2357 ;
2358 else if (DECL_ARTIFICIAL (fn))
2359 /* Cannot specialize functions that are created implicitly. */
2360 ;
2361 else
2362 {
2363 tree decl_arg_types;
2364
2365 /* This is an ordinary member function. However, since
2366 we're here, we can assume its enclosing class is a
2367 template class. For example,
2368
2369 template <typename T> struct S { void f(); };
2370 template <> void S<int>::f() {}
2371
2372 Here, S<int>::f is a non-template, but S<int> is a
2373 template class. If FN has the same type as DECL, we
2374 might be in business. */
2375
2376 if (!DECL_TEMPLATE_INFO (fn))
2377 /* Its enclosing class is an explicit specialization
2378 of a template class. This is not a candidate. */
2379 continue;
2380
2381 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2382 TREE_TYPE (TREE_TYPE (fn))))
2383 /* The return types differ. */
2384 continue;
2385
2386 /* Adjust the type of DECL in case FN is a static member. */
2387 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2388 if (DECL_STATIC_FUNCTION_P (fn)
2389 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2390 decl_arg_types = TREE_CHAIN (decl_arg_types);
2391
2392 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2393 decl_arg_types))
2394 continue;
2395
2396 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2397 && (type_memfn_rqual (TREE_TYPE (decl))
2398 != type_memfn_rqual (TREE_TYPE (fn))))
2399 continue;
2400
2401 // If the deduced arguments do not satisfy the constraints,
2402 // this is not a candidate.
2403 if (flag_concepts && !constraints_satisfied_p (fn))
2404 continue;
2405
2406 // Add the candidate.
2407 candidates = tree_cons (NULL_TREE, fn, candidates);
2408 }
2409 }
2410
2411 if (templates && TREE_CHAIN (templates))
2412 {
2413 /* We have:
2414
2415 [temp.expl.spec]
2416
2417 It is possible for a specialization with a given function
2418 signature to be instantiated from more than one function
2419 template. In such cases, explicit specification of the
2420 template arguments must be used to uniquely identify the
2421 function template specialization being specialized.
2422
2423 Note that here, there's no suggestion that we're supposed to
2424 determine which of the candidate templates is most
2425 specialized. However, we, also have:
2426
2427 [temp.func.order]
2428
2429 Partial ordering of overloaded function template
2430 declarations is used in the following contexts to select
2431 the function template to which a function template
2432 specialization refers:
2433
2434 -- when an explicit specialization refers to a function
2435 template.
2436
2437 So, we do use the partial ordering rules, at least for now.
2438 This extension can only serve to make invalid programs valid,
2439 so it's safe. And, there is strong anecdotal evidence that
2440 the committee intended the partial ordering rules to apply;
2441 the EDG front end has that behavior, and John Spicer claims
2442 that the committee simply forgot to delete the wording in
2443 [temp.expl.spec]. */
2444 tree tmpl = most_specialized_instantiation (templates);
2445 if (tmpl != error_mark_node)
2446 {
2447 templates = tmpl;
2448 TREE_CHAIN (templates) = NULL_TREE;
2449 }
2450 }
2451
2452 // Concepts allows multiple declarations of member functions
2453 // with the same signature. Like above, we need to rely on
2454 // on the partial ordering of those candidates to determine which
2455 // is the best.
2456 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2457 {
2458 if (tree cand = most_constrained_function (candidates))
2459 {
2460 candidates = cand;
2461 TREE_CHAIN (cand) = NULL_TREE;
2462 }
2463 }
2464
2465 if (templates == NULL_TREE && candidates == NULL_TREE)
2466 {
2467 error ("template-id %qD for %q+D does not match any template "
2468 "declaration", template_id, decl);
2469 if (header_count && header_count != template_count + 1)
2470 inform (DECL_SOURCE_LOCATION (decl),
2471 "saw %d %<template<>%>, need %d for "
2472 "specializing a member function template",
2473 header_count, template_count + 1);
2474 else
2475 print_candidates (orig_fns);
2476 return error_mark_node;
2477 }
2478 else if ((templates && TREE_CHAIN (templates))
2479 || (candidates && TREE_CHAIN (candidates))
2480 || (templates && candidates))
2481 {
2482 error ("ambiguous template specialization %qD for %q+D",
2483 template_id, decl);
2484 candidates = chainon (candidates, templates);
2485 print_candidates (candidates);
2486 return error_mark_node;
2487 }
2488
2489 /* We have one, and exactly one, match. */
2490 if (candidates)
2491 {
2492 tree fn = TREE_VALUE (candidates);
2493 *targs_out = copy_node (DECL_TI_ARGS (fn));
2494
2495 /* Propagate the candidate's constraints to the declaration. */
2496 if (tsk != tsk_template)
2497 set_constraints (decl, get_constraints (fn));
2498
2499 /* DECL is a re-declaration or partial instantiation of a template
2500 function. */
2501 if (TREE_CODE (fn) == TEMPLATE_DECL)
2502 return fn;
2503 /* It was a specialization of an ordinary member function in a
2504 template class. */
2505 return DECL_TI_TEMPLATE (fn);
2506 }
2507
2508 /* It was a specialization of a template. */
2509 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2510 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2511 {
2512 *targs_out = copy_node (targs);
2513 SET_TMPL_ARGS_LEVEL (*targs_out,
2514 TMPL_ARGS_DEPTH (*targs_out),
2515 TREE_PURPOSE (templates));
2516 }
2517 else
2518 *targs_out = TREE_PURPOSE (templates);
2519 return TREE_VALUE (templates);
2520 }
2521
2522 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2523 but with the default argument values filled in from those in the
2524 TMPL_TYPES. */
2525
2526 static tree
2527 copy_default_args_to_explicit_spec_1 (tree spec_types,
2528 tree tmpl_types)
2529 {
2530 tree new_spec_types;
2531
2532 if (!spec_types)
2533 return NULL_TREE;
2534
2535 if (spec_types == void_list_node)
2536 return void_list_node;
2537
2538 /* Substitute into the rest of the list. */
2539 new_spec_types =
2540 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2541 TREE_CHAIN (tmpl_types));
2542
2543 /* Add the default argument for this parameter. */
2544 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2545 TREE_VALUE (spec_types),
2546 new_spec_types);
2547 }
2548
2549 /* DECL is an explicit specialization. Replicate default arguments
2550 from the template it specializes. (That way, code like:
2551
2552 template <class T> void f(T = 3);
2553 template <> void f(double);
2554 void g () { f (); }
2555
2556 works, as required.) An alternative approach would be to look up
2557 the correct default arguments at the call-site, but this approach
2558 is consistent with how implicit instantiations are handled. */
2559
2560 static void
2561 copy_default_args_to_explicit_spec (tree decl)
2562 {
2563 tree tmpl;
2564 tree spec_types;
2565 tree tmpl_types;
2566 tree new_spec_types;
2567 tree old_type;
2568 tree new_type;
2569 tree t;
2570 tree object_type = NULL_TREE;
2571 tree in_charge = NULL_TREE;
2572 tree vtt = NULL_TREE;
2573
2574 /* See if there's anything we need to do. */
2575 tmpl = DECL_TI_TEMPLATE (decl);
2576 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2577 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2578 if (TREE_PURPOSE (t))
2579 break;
2580 if (!t)
2581 return;
2582
2583 old_type = TREE_TYPE (decl);
2584 spec_types = TYPE_ARG_TYPES (old_type);
2585
2586 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2587 {
2588 /* Remove the this pointer, but remember the object's type for
2589 CV quals. */
2590 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2591 spec_types = TREE_CHAIN (spec_types);
2592 tmpl_types = TREE_CHAIN (tmpl_types);
2593
2594 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2595 {
2596 /* DECL may contain more parameters than TMPL due to the extra
2597 in-charge parameter in constructors and destructors. */
2598 in_charge = spec_types;
2599 spec_types = TREE_CHAIN (spec_types);
2600 }
2601 if (DECL_HAS_VTT_PARM_P (decl))
2602 {
2603 vtt = spec_types;
2604 spec_types = TREE_CHAIN (spec_types);
2605 }
2606 }
2607
2608 /* Compute the merged default arguments. */
2609 new_spec_types =
2610 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2611
2612 /* Compute the new FUNCTION_TYPE. */
2613 if (object_type)
2614 {
2615 if (vtt)
2616 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2617 TREE_VALUE (vtt),
2618 new_spec_types);
2619
2620 if (in_charge)
2621 /* Put the in-charge parameter back. */
2622 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2623 TREE_VALUE (in_charge),
2624 new_spec_types);
2625
2626 new_type = build_method_type_directly (object_type,
2627 TREE_TYPE (old_type),
2628 new_spec_types);
2629 }
2630 else
2631 new_type = build_function_type (TREE_TYPE (old_type),
2632 new_spec_types);
2633 new_type = cp_build_type_attribute_variant (new_type,
2634 TYPE_ATTRIBUTES (old_type));
2635 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2636
2637 TREE_TYPE (decl) = new_type;
2638 }
2639
2640 /* Return the number of template headers we expect to see for a definition
2641 or specialization of CTYPE or one of its non-template members. */
2642
2643 int
2644 num_template_headers_for_class (tree ctype)
2645 {
2646 int num_templates = 0;
2647
2648 while (ctype && CLASS_TYPE_P (ctype))
2649 {
2650 /* You're supposed to have one `template <...>' for every
2651 template class, but you don't need one for a full
2652 specialization. For example:
2653
2654 template <class T> struct S{};
2655 template <> struct S<int> { void f(); };
2656 void S<int>::f () {}
2657
2658 is correct; there shouldn't be a `template <>' for the
2659 definition of `S<int>::f'. */
2660 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2661 /* If CTYPE does not have template information of any
2662 kind, then it is not a template, nor is it nested
2663 within a template. */
2664 break;
2665 if (explicit_class_specialization_p (ctype))
2666 break;
2667 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2668 ++num_templates;
2669
2670 ctype = TYPE_CONTEXT (ctype);
2671 }
2672
2673 return num_templates;
2674 }
2675
2676 /* Do a simple sanity check on the template headers that precede the
2677 variable declaration DECL. */
2678
2679 void
2680 check_template_variable (tree decl)
2681 {
2682 tree ctx = CP_DECL_CONTEXT (decl);
2683 int wanted = num_template_headers_for_class (ctx);
2684 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2685 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2686 {
2687 if (cxx_dialect < cxx14)
2688 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2689 "variable templates only available with "
2690 "%<-std=c++14%> or %<-std=gnu++14%>");
2691
2692 // Namespace-scope variable templates should have a template header.
2693 ++wanted;
2694 }
2695 if (template_header_count > wanted)
2696 {
2697 auto_diagnostic_group d;
2698 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2699 "too many template headers for %qD "
2700 "(should be %d)",
2701 decl, wanted);
2702 if (warned && CLASS_TYPE_P (ctx)
2703 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2704 inform (DECL_SOURCE_LOCATION (decl),
2705 "members of an explicitly specialized class are defined "
2706 "without a template header");
2707 }
2708 }
2709
2710 /* An explicit specialization whose declarator-id or class-head-name is not
2711 qualified shall be declared in the nearest enclosing namespace of the
2712 template, or, if the namespace is inline (7.3.1), any namespace from its
2713 enclosing namespace set.
2714
2715 If the name declared in the explicit instantiation is an unqualified name,
2716 the explicit instantiation shall appear in the namespace where its template
2717 is declared or, if that namespace is inline (7.3.1), any namespace from its
2718 enclosing namespace set. */
2719
2720 void
2721 check_unqualified_spec_or_inst (tree t, location_t loc)
2722 {
2723 tree tmpl = most_general_template (t);
2724 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2725 && !is_nested_namespace (current_namespace,
2726 CP_DECL_CONTEXT (tmpl), true))
2727 {
2728 if (processing_specialization)
2729 permerror (loc, "explicit specialization of %qD outside its "
2730 "namespace must use a nested-name-specifier", tmpl);
2731 else if (processing_explicit_instantiation
2732 && cxx_dialect >= cxx11)
2733 /* This was allowed in C++98, so only pedwarn. */
2734 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2735 "outside its namespace must use a nested-name-"
2736 "specifier", tmpl);
2737 }
2738 }
2739
2740 /* Warn for a template specialization SPEC that is missing some of a set
2741 of function or type attributes that the template TEMPL is declared with.
2742 ATTRLIST is a list of additional attributes that SPEC should be taken
2743 to ultimately be declared with. */
2744
2745 static void
2746 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2747 {
2748 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2749 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2750
2751 /* Avoid warning if the difference between the primary and
2752 the specialization is not in one of the attributes below. */
2753 const char* const blacklist[] = {
2754 "alloc_align", "alloc_size", "assume_aligned", "format",
2755 "format_arg", "malloc", "nonnull", NULL
2756 };
2757
2758 /* Put together a list of the black listed attributes that the primary
2759 template is declared with that the specialization is not, in case
2760 it's not apparent from the most recent declaration of the primary. */
2761 pretty_printer str;
2762 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2763 blacklist, &str);
2764
2765 if (!nattrs)
2766 return;
2767
2768 auto_diagnostic_group d;
2769 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2770 "explicit specialization %q#D may be missing attributes",
2771 spec))
2772 inform (DECL_SOURCE_LOCATION (tmpl),
2773 nattrs > 1
2774 ? G_("missing primary template attributes %s")
2775 : G_("missing primary template attribute %s"),
2776 pp_formatted_text (&str));
2777 }
2778
2779 /* Check to see if the function just declared, as indicated in
2780 DECLARATOR, and in DECL, is a specialization of a function
2781 template. We may also discover that the declaration is an explicit
2782 instantiation at this point.
2783
2784 Returns DECL, or an equivalent declaration that should be used
2785 instead if all goes well. Issues an error message if something is
2786 amiss. Returns error_mark_node if the error is not easily
2787 recoverable.
2788
2789 FLAGS is a bitmask consisting of the following flags:
2790
2791 2: The function has a definition.
2792 4: The function is a friend.
2793
2794 The TEMPLATE_COUNT is the number of references to qualifying
2795 template classes that appeared in the name of the function. For
2796 example, in
2797
2798 template <class T> struct S { void f(); };
2799 void S<int>::f();
2800
2801 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2802 classes are not counted in the TEMPLATE_COUNT, so that in
2803
2804 template <class T> struct S {};
2805 template <> struct S<int> { void f(); }
2806 template <> void S<int>::f();
2807
2808 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2809 invalid; there should be no template <>.)
2810
2811 If the function is a specialization, it is marked as such via
2812 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2813 is set up correctly, and it is added to the list of specializations
2814 for that template. */
2815
2816 tree
2817 check_explicit_specialization (tree declarator,
2818 tree decl,
2819 int template_count,
2820 int flags,
2821 tree attrlist)
2822 {
2823 int have_def = flags & 2;
2824 int is_friend = flags & 4;
2825 bool is_concept = flags & 8;
2826 int specialization = 0;
2827 int explicit_instantiation = 0;
2828 int member_specialization = 0;
2829 tree ctype = DECL_CLASS_CONTEXT (decl);
2830 tree dname = DECL_NAME (decl);
2831 tmpl_spec_kind tsk;
2832
2833 if (is_friend)
2834 {
2835 if (!processing_specialization)
2836 tsk = tsk_none;
2837 else
2838 tsk = tsk_excessive_parms;
2839 }
2840 else
2841 tsk = current_tmpl_spec_kind (template_count);
2842
2843 switch (tsk)
2844 {
2845 case tsk_none:
2846 if (processing_specialization && !VAR_P (decl))
2847 {
2848 specialization = 1;
2849 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2850 }
2851 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2852 {
2853 if (is_friend)
2854 /* This could be something like:
2855
2856 template <class T> void f(T);
2857 class S { friend void f<>(int); } */
2858 specialization = 1;
2859 else
2860 {
2861 /* This case handles bogus declarations like template <>
2862 template <class T> void f<int>(); */
2863
2864 error_at (cp_expr_loc_or_input_loc (declarator),
2865 "template-id %qE in declaration of primary template",
2866 declarator);
2867 return decl;
2868 }
2869 }
2870 break;
2871
2872 case tsk_invalid_member_spec:
2873 /* The error has already been reported in
2874 check_specialization_scope. */
2875 return error_mark_node;
2876
2877 case tsk_invalid_expl_inst:
2878 error ("template parameter list used in explicit instantiation");
2879
2880 /* Fall through. */
2881
2882 case tsk_expl_inst:
2883 if (have_def)
2884 error ("definition provided for explicit instantiation");
2885
2886 explicit_instantiation = 1;
2887 break;
2888
2889 case tsk_excessive_parms:
2890 case tsk_insufficient_parms:
2891 if (tsk == tsk_excessive_parms)
2892 error ("too many template parameter lists in declaration of %qD",
2893 decl);
2894 else if (template_header_count)
2895 error("too few template parameter lists in declaration of %qD", decl);
2896 else
2897 error("explicit specialization of %qD must be introduced by "
2898 "%<template <>%>", decl);
2899
2900 /* Fall through. */
2901 case tsk_expl_spec:
2902 if (is_concept)
2903 error ("explicit specialization declared %<concept%>");
2904
2905 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2906 /* In cases like template<> constexpr bool v = true;
2907 We'll give an error in check_template_variable. */
2908 break;
2909
2910 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2911 if (ctype)
2912 member_specialization = 1;
2913 else
2914 specialization = 1;
2915 break;
2916
2917 case tsk_template:
2918 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2919 {
2920 /* This case handles bogus declarations like template <>
2921 template <class T> void f<int>(); */
2922
2923 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2924 error_at (cp_expr_loc_or_input_loc (declarator),
2925 "template-id %qE in declaration of primary template",
2926 declarator);
2927 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2928 {
2929 /* Partial specialization of variable template. */
2930 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2931 specialization = 1;
2932 goto ok;
2933 }
2934 else if (cxx_dialect < cxx14)
2935 error_at (cp_expr_loc_or_input_loc (declarator),
2936 "non-type partial specialization %qE "
2937 "is not allowed", declarator);
2938 else
2939 error_at (cp_expr_loc_or_input_loc (declarator),
2940 "non-class, non-variable partial specialization %qE "
2941 "is not allowed", declarator);
2942 return decl;
2943 ok:;
2944 }
2945
2946 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2947 /* This is a specialization of a member template, without
2948 specialization the containing class. Something like:
2949
2950 template <class T> struct S {
2951 template <class U> void f (U);
2952 };
2953 template <> template <class U> void S<int>::f(U) {}
2954
2955 That's a specialization -- but of the entire template. */
2956 specialization = 1;
2957 break;
2958
2959 default:
2960 gcc_unreachable ();
2961 }
2962
2963 if ((specialization || member_specialization)
2964 /* This doesn't apply to variable templates. */
2965 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2966 {
2967 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2968 for (; t; t = TREE_CHAIN (t))
2969 if (TREE_PURPOSE (t))
2970 {
2971 permerror (input_location,
2972 "default argument specified in explicit specialization");
2973 break;
2974 }
2975 }
2976
2977 if (specialization || member_specialization || explicit_instantiation)
2978 {
2979 tree tmpl = NULL_TREE;
2980 tree targs = NULL_TREE;
2981 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2982
2983 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2984 if (!was_template_id)
2985 {
2986 tree fns;
2987
2988 gcc_assert (identifier_p (declarator));
2989 if (ctype)
2990 fns = dname;
2991 else
2992 {
2993 /* If there is no class context, the explicit instantiation
2994 must be at namespace scope. */
2995 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2996
2997 /* Find the namespace binding, using the declaration
2998 context. */
2999 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3000 false, true);
3001 if (fns == error_mark_node)
3002 /* If lookup fails, look for a friend declaration so we can
3003 give a better diagnostic. */
3004 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3005 /*type*/false, /*complain*/true,
3006 /*hidden*/true);
3007
3008 if (fns == error_mark_node || !is_overloaded_fn (fns))
3009 {
3010 error ("%qD is not a template function", dname);
3011 fns = error_mark_node;
3012 }
3013 }
3014
3015 declarator = lookup_template_function (fns, NULL_TREE);
3016 }
3017
3018 if (declarator == error_mark_node)
3019 return error_mark_node;
3020
3021 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3022 {
3023 if (!explicit_instantiation)
3024 /* A specialization in class scope. This is invalid,
3025 but the error will already have been flagged by
3026 check_specialization_scope. */
3027 return error_mark_node;
3028 else
3029 {
3030 /* It's not valid to write an explicit instantiation in
3031 class scope, e.g.:
3032
3033 class C { template void f(); }
3034
3035 This case is caught by the parser. However, on
3036 something like:
3037
3038 template class C { void f(); };
3039
3040 (which is invalid) we can get here. The error will be
3041 issued later. */
3042 ;
3043 }
3044
3045 return decl;
3046 }
3047 else if (ctype != NULL_TREE
3048 && (identifier_p (TREE_OPERAND (declarator, 0))))
3049 {
3050 // We'll match variable templates in start_decl.
3051 if (VAR_P (decl))
3052 return decl;
3053
3054 /* Find the list of functions in ctype that have the same
3055 name as the declared function. */
3056 tree name = TREE_OPERAND (declarator, 0);
3057
3058 if (constructor_name_p (name, ctype))
3059 {
3060 if (DECL_CONSTRUCTOR_P (decl)
3061 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3062 : !CLASSTYPE_DESTRUCTOR (ctype))
3063 {
3064 /* From [temp.expl.spec]:
3065
3066 If such an explicit specialization for the member
3067 of a class template names an implicitly-declared
3068 special member function (clause _special_), the
3069 program is ill-formed.
3070
3071 Similar language is found in [temp.explicit]. */
3072 error ("specialization of implicitly-declared special member function");
3073 return error_mark_node;
3074 }
3075
3076 name = DECL_NAME (decl);
3077 }
3078
3079 /* For a type-conversion operator, We might be looking for
3080 `operator int' which will be a specialization of
3081 `operator T'. Grab all the conversion operators, and
3082 then select from them. */
3083 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3084 ? conv_op_identifier : name);
3085
3086 if (fns == NULL_TREE)
3087 {
3088 error ("no member function %qD declared in %qT", name, ctype);
3089 return error_mark_node;
3090 }
3091 else
3092 TREE_OPERAND (declarator, 0) = fns;
3093 }
3094
3095 /* Figure out what exactly is being specialized at this point.
3096 Note that for an explicit instantiation, even one for a
3097 member function, we cannot tell a priori whether the
3098 instantiation is for a member template, or just a member
3099 function of a template class. Even if a member template is
3100 being instantiated, the member template arguments may be
3101 elided if they can be deduced from the rest of the
3102 declaration. */
3103 tmpl = determine_specialization (declarator, decl,
3104 &targs,
3105 member_specialization,
3106 template_count,
3107 tsk);
3108
3109 if (!tmpl || tmpl == error_mark_node)
3110 /* We couldn't figure out what this declaration was
3111 specializing. */
3112 return error_mark_node;
3113 else
3114 {
3115 if (TREE_CODE (decl) == FUNCTION_DECL
3116 && DECL_HIDDEN_FRIEND_P (tmpl))
3117 {
3118 auto_diagnostic_group d;
3119 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3120 "friend declaration %qD is not visible to "
3121 "explicit specialization", tmpl))
3122 inform (DECL_SOURCE_LOCATION (tmpl),
3123 "friend declaration here");
3124 }
3125 else if (!ctype && !is_friend
3126 && CP_DECL_CONTEXT (decl) == current_namespace)
3127 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3128
3129 tree gen_tmpl = most_general_template (tmpl);
3130
3131 if (explicit_instantiation)
3132 {
3133 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3134 is done by do_decl_instantiation later. */
3135
3136 int arg_depth = TMPL_ARGS_DEPTH (targs);
3137 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3138
3139 if (arg_depth > parm_depth)
3140 {
3141 /* If TMPL is not the most general template (for
3142 example, if TMPL is a friend template that is
3143 injected into namespace scope), then there will
3144 be too many levels of TARGS. Remove some of them
3145 here. */
3146 int i;
3147 tree new_targs;
3148
3149 new_targs = make_tree_vec (parm_depth);
3150 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3151 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3152 = TREE_VEC_ELT (targs, i);
3153 targs = new_targs;
3154 }
3155
3156 return instantiate_template (tmpl, targs, tf_error);
3157 }
3158
3159 /* If we thought that the DECL was a member function, but it
3160 turns out to be specializing a static member function,
3161 make DECL a static member function as well. */
3162 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3163 && DECL_STATIC_FUNCTION_P (tmpl)
3164 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3165 revert_static_member_fn (decl);
3166
3167 /* If this is a specialization of a member template of a
3168 template class, we want to return the TEMPLATE_DECL, not
3169 the specialization of it. */
3170 if (tsk == tsk_template && !was_template_id)
3171 {
3172 tree result = DECL_TEMPLATE_RESULT (tmpl);
3173 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3174 DECL_INITIAL (result) = NULL_TREE;
3175 if (have_def)
3176 {
3177 tree parm;
3178 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3179 DECL_SOURCE_LOCATION (result)
3180 = DECL_SOURCE_LOCATION (decl);
3181 /* We want to use the argument list specified in the
3182 definition, not in the original declaration. */
3183 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3184 for (parm = DECL_ARGUMENTS (result); parm;
3185 parm = DECL_CHAIN (parm))
3186 DECL_CONTEXT (parm) = result;
3187 }
3188 return register_specialization (tmpl, gen_tmpl, targs,
3189 is_friend, 0);
3190 }
3191
3192 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3193 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3194
3195 if (was_template_id)
3196 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3197
3198 /* Inherit default function arguments from the template
3199 DECL is specializing. */
3200 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3201 copy_default_args_to_explicit_spec (decl);
3202
3203 /* This specialization has the same protection as the
3204 template it specializes. */
3205 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3206 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3207
3208 /* 7.1.1-1 [dcl.stc]
3209
3210 A storage-class-specifier shall not be specified in an
3211 explicit specialization...
3212
3213 The parser rejects these, so unless action is taken here,
3214 explicit function specializations will always appear with
3215 global linkage.
3216
3217 The action recommended by the C++ CWG in response to C++
3218 defect report 605 is to make the storage class and linkage
3219 of the explicit specialization match the templated function:
3220
3221 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3222 */
3223 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3224 {
3225 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3226 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3227
3228 /* A concept cannot be specialized. */
3229 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3230 {
3231 error ("explicit specialization of function concept %qD",
3232 gen_tmpl);
3233 return error_mark_node;
3234 }
3235
3236 /* This specialization has the same linkage and visibility as
3237 the function template it specializes. */
3238 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3239 if (! TREE_PUBLIC (decl))
3240 {
3241 DECL_INTERFACE_KNOWN (decl) = 1;
3242 DECL_NOT_REALLY_EXTERN (decl) = 1;
3243 }
3244 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3245 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3246 {
3247 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3248 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3249 }
3250 }
3251
3252 /* If DECL is a friend declaration, declared using an
3253 unqualified name, the namespace associated with DECL may
3254 have been set incorrectly. For example, in:
3255
3256 template <typename T> void f(T);
3257 namespace N {
3258 struct S { friend void f<int>(int); }
3259 }
3260
3261 we will have set the DECL_CONTEXT for the friend
3262 declaration to N, rather than to the global namespace. */
3263 if (DECL_NAMESPACE_SCOPE_P (decl))
3264 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3265
3266 if (is_friend && !have_def)
3267 /* This is not really a declaration of a specialization.
3268 It's just the name of an instantiation. But, it's not
3269 a request for an instantiation, either. */
3270 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3271 else if (TREE_CODE (decl) == FUNCTION_DECL)
3272 /* A specialization is not necessarily COMDAT. */
3273 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3274 && DECL_DECLARED_INLINE_P (decl));
3275 else if (VAR_P (decl))
3276 DECL_COMDAT (decl) = false;
3277
3278 /* If this is a full specialization, register it so that we can find
3279 it again. Partial specializations will be registered in
3280 process_partial_specialization. */
3281 if (!processing_template_decl)
3282 {
3283 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3284
3285 decl = register_specialization (decl, gen_tmpl, targs,
3286 is_friend, 0);
3287 }
3288
3289
3290 /* A 'structor should already have clones. */
3291 gcc_assert (decl == error_mark_node
3292 || variable_template_p (tmpl)
3293 || !(DECL_CONSTRUCTOR_P (decl)
3294 || DECL_DESTRUCTOR_P (decl))
3295 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3296 }
3297 }
3298
3299 return decl;
3300 }
3301
3302 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3303 parameters. These are represented in the same format used for
3304 DECL_TEMPLATE_PARMS. */
3305
3306 int
3307 comp_template_parms (const_tree parms1, const_tree parms2)
3308 {
3309 const_tree p1;
3310 const_tree p2;
3311
3312 if (parms1 == parms2)
3313 return 1;
3314
3315 for (p1 = parms1, p2 = parms2;
3316 p1 != NULL_TREE && p2 != NULL_TREE;
3317 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3318 {
3319 tree t1 = TREE_VALUE (p1);
3320 tree t2 = TREE_VALUE (p2);
3321 int i;
3322
3323 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3324 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3325
3326 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3327 return 0;
3328
3329 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3330 {
3331 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3332 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3333
3334 /* If either of the template parameters are invalid, assume
3335 they match for the sake of error recovery. */
3336 if (error_operand_p (parm1) || error_operand_p (parm2))
3337 return 1;
3338
3339 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3340 return 0;
3341
3342 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3343 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3344 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3345 continue;
3346 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3347 return 0;
3348 }
3349 }
3350
3351 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3352 /* One set of parameters has more parameters lists than the
3353 other. */
3354 return 0;
3355
3356 return 1;
3357 }
3358
3359 /* Returns true if two template parameters are declared with
3360 equivalent constraints. */
3361
3362 static bool
3363 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3364 {
3365 tree req1 = TREE_TYPE (parm1);
3366 tree req2 = TREE_TYPE (parm2);
3367 if (!req1 != !req2)
3368 return false;
3369 if (req1)
3370 return cp_tree_equal (req1, req2);
3371 return true;
3372 }
3373
3374 /* Returns true when two template parameters are equivalent. */
3375
3376 static bool
3377 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3378 {
3379 tree decl1 = TREE_VALUE (parm1);
3380 tree decl2 = TREE_VALUE (parm2);
3381
3382 /* If either of the template parameters are invalid, assume
3383 they match for the sake of error recovery. */
3384 if (error_operand_p (decl1) || error_operand_p (decl2))
3385 return true;
3386
3387 /* ... they declare parameters of the same kind. */
3388 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3389 return false;
3390
3391 /* ... one parameter was introduced by a parameter declaration, then
3392 both are. This case arises as a result of eagerly rewriting declarations
3393 during parsing. */
3394 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3395 return false;
3396
3397 /* ... if either declares a pack, they both do. */
3398 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3399 return false;
3400
3401 if (TREE_CODE (decl1) == PARM_DECL)
3402 {
3403 /* ... if they declare non-type parameters, the types are equivalent. */
3404 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3405 return false;
3406 }
3407 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3408 {
3409 /* ... if they declare template template parameters, their template
3410 parameter lists are equivalent. */
3411 if (!template_heads_equivalent_p (decl1, decl2))
3412 return false;
3413 }
3414
3415 /* ... if they are declared with a qualified-concept name, they both
3416 are, and those names are equivalent. */
3417 return template_parameter_constraints_equivalent_p (parm1, parm2);
3418 }
3419
3420 /* Returns true if two template parameters lists are equivalent.
3421 Two template parameter lists are equivalent if they have the
3422 same length and their corresponding parameters are equivalent.
3423
3424 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3425 data structure returned by DECL_TEMPLATE_PARMS.
3426
3427 This is generally the same implementation as comp_template_parms
3428 except that it also the concept names and arguments used to
3429 introduce parameters. */
3430
3431 static bool
3432 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3433 {
3434 if (parms1 == parms2)
3435 return true;
3436
3437 const_tree p1 = parms1;
3438 const_tree p2 = parms2;
3439 while (p1 != NULL_TREE && p2 != NULL_TREE)
3440 {
3441 tree list1 = TREE_VALUE (p1);
3442 tree list2 = TREE_VALUE (p2);
3443
3444 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3445 return 0;
3446
3447 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3448 {
3449 tree parm1 = TREE_VEC_ELT (list1, i);
3450 tree parm2 = TREE_VEC_ELT (list2, i);
3451 if (!template_parameters_equivalent_p (parm1, parm2))
3452 return false;
3453 }
3454
3455 p1 = TREE_CHAIN (p1);
3456 p2 = TREE_CHAIN (p2);
3457 }
3458
3459 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3460 return false;
3461
3462 return true;
3463 }
3464
3465 /* Return true if the requires-clause of the template parameter lists are
3466 equivalent and false otherwise. */
3467 static bool
3468 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3469 {
3470 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3471 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3472 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3473 return false;
3474 if (!cp_tree_equal (req1, req2))
3475 return false;
3476 return true;
3477 }
3478
3479 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3480 Two template heads are equivalent if their template parameter
3481 lists are equivalent and their requires clauses are equivalent.
3482
3483 In pre-C++20, this is equivalent to calling comp_template_parms
3484 for the template parameters of TMPL1 and TMPL2. */
3485
3486 bool
3487 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3488 {
3489 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3490 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3491
3492 /* Don't change the matching rules for pre-C++20. */
3493 if (cxx_dialect < cxx20)
3494 return comp_template_parms (parms1, parms2);
3495
3496 /* ... have the same number of template parameters, and their
3497 corresponding parameters are equivalent. */
3498 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3499 return false;
3500
3501 /* ... if either has a requires-clause, they both do and their
3502 corresponding constraint-expressions are equivalent. */
3503 return template_requirements_equivalent_p (parms1, parms2);
3504 }
3505
3506 /* Determine whether PARM is a parameter pack. */
3507
3508 bool
3509 template_parameter_pack_p (const_tree parm)
3510 {
3511 /* Determine if we have a non-type template parameter pack. */
3512 if (TREE_CODE (parm) == PARM_DECL)
3513 return (DECL_TEMPLATE_PARM_P (parm)
3514 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3515 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3516 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3517
3518 /* If this is a list of template parameters, we could get a
3519 TYPE_DECL or a TEMPLATE_DECL. */
3520 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3521 parm = TREE_TYPE (parm);
3522
3523 /* Otherwise it must be a type template parameter. */
3524 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3525 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3526 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3527 }
3528
3529 /* Determine if T is a function parameter pack. */
3530
3531 bool
3532 function_parameter_pack_p (const_tree t)
3533 {
3534 if (t && TREE_CODE (t) == PARM_DECL)
3535 return DECL_PACK_P (t);
3536 return false;
3537 }
3538
3539 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3540 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3541
3542 tree
3543 get_function_template_decl (const_tree primary_func_tmpl_inst)
3544 {
3545 if (! primary_func_tmpl_inst
3546 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3547 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3548 return NULL;
3549
3550 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3551 }
3552
3553 /* Return true iff the function parameter PARAM_DECL was expanded
3554 from the function parameter pack PACK. */
3555
3556 bool
3557 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3558 {
3559 if (DECL_ARTIFICIAL (param_decl)
3560 || !function_parameter_pack_p (pack))
3561 return false;
3562
3563 /* The parameter pack and its pack arguments have the same
3564 DECL_PARM_INDEX. */
3565 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3566 }
3567
3568 /* Determine whether ARGS describes a variadic template args list,
3569 i.e., one that is terminated by a template argument pack. */
3570
3571 static bool
3572 template_args_variadic_p (tree args)
3573 {
3574 int nargs;
3575 tree last_parm;
3576
3577 if (args == NULL_TREE)
3578 return false;
3579
3580 args = INNERMOST_TEMPLATE_ARGS (args);
3581 nargs = TREE_VEC_LENGTH (args);
3582
3583 if (nargs == 0)
3584 return false;
3585
3586 last_parm = TREE_VEC_ELT (args, nargs - 1);
3587
3588 return ARGUMENT_PACK_P (last_parm);
3589 }
3590
3591 /* Generate a new name for the parameter pack name NAME (an
3592 IDENTIFIER_NODE) that incorporates its */
3593
3594 static tree
3595 make_ith_pack_parameter_name (tree name, int i)
3596 {
3597 /* Munge the name to include the parameter index. */
3598 #define NUMBUF_LEN 128
3599 char numbuf[NUMBUF_LEN];
3600 char* newname;
3601 int newname_len;
3602
3603 if (name == NULL_TREE)
3604 return name;
3605 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3606 newname_len = IDENTIFIER_LENGTH (name)
3607 + strlen (numbuf) + 2;
3608 newname = (char*)alloca (newname_len);
3609 snprintf (newname, newname_len,
3610 "%s#%i", IDENTIFIER_POINTER (name), i);
3611 return get_identifier (newname);
3612 }
3613
3614 /* Return true if T is a primary function, class or alias template
3615 specialization, not including the template pattern. */
3616
3617 bool
3618 primary_template_specialization_p (const_tree t)
3619 {
3620 if (!t)
3621 return false;
3622
3623 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3624 return (DECL_LANG_SPECIFIC (t)
3625 && DECL_USE_TEMPLATE (t)
3626 && DECL_TEMPLATE_INFO (t)
3627 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3628 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3629 return (CLASSTYPE_TEMPLATE_INFO (t)
3630 && CLASSTYPE_USE_TEMPLATE (t)
3631 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3632 else if (alias_template_specialization_p (t, nt_transparent))
3633 return true;
3634 return false;
3635 }
3636
3637 /* Return true if PARM is a template template parameter. */
3638
3639 bool
3640 template_template_parameter_p (const_tree parm)
3641 {
3642 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3643 }
3644
3645 /* Return true iff PARM is a DECL representing a type template
3646 parameter. */
3647
3648 bool
3649 template_type_parameter_p (const_tree parm)
3650 {
3651 return (parm
3652 && (TREE_CODE (parm) == TYPE_DECL
3653 || TREE_CODE (parm) == TEMPLATE_DECL)
3654 && DECL_TEMPLATE_PARM_P (parm));
3655 }
3656
3657 /* Return the template parameters of T if T is a
3658 primary template instantiation, NULL otherwise. */
3659
3660 tree
3661 get_primary_template_innermost_parameters (const_tree t)
3662 {
3663 tree parms = NULL, template_info = NULL;
3664
3665 if ((template_info = get_template_info (t))
3666 && primary_template_specialization_p (t))
3667 parms = INNERMOST_TEMPLATE_PARMS
3668 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3669
3670 return parms;
3671 }
3672
3673 /* Return the template parameters of the LEVELth level from the full list
3674 of template parameters PARMS. */
3675
3676 tree
3677 get_template_parms_at_level (tree parms, int level)
3678 {
3679 tree p;
3680 if (!parms
3681 || TREE_CODE (parms) != TREE_LIST
3682 || level > TMPL_PARMS_DEPTH (parms))
3683 return NULL_TREE;
3684
3685 for (p = parms; p; p = TREE_CHAIN (p))
3686 if (TMPL_PARMS_DEPTH (p) == level)
3687 return p;
3688
3689 return NULL_TREE;
3690 }
3691
3692 /* Returns the template arguments of T if T is a template instantiation,
3693 NULL otherwise. */
3694
3695 tree
3696 get_template_innermost_arguments (const_tree t)
3697 {
3698 tree args = NULL, template_info = NULL;
3699
3700 if ((template_info = get_template_info (t))
3701 && TI_ARGS (template_info))
3702 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3703
3704 return args;
3705 }
3706
3707 /* Return the argument pack elements of T if T is a template argument pack,
3708 NULL otherwise. */
3709
3710 tree
3711 get_template_argument_pack_elems (const_tree t)
3712 {
3713 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3714 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3715 return NULL;
3716
3717 return ARGUMENT_PACK_ARGS (t);
3718 }
3719
3720 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3721 ARGUMENT_PACK_SELECT represents. */
3722
3723 static tree
3724 argument_pack_select_arg (tree t)
3725 {
3726 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3727 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3728
3729 /* If the selected argument is an expansion E, that most likely means we were
3730 called from gen_elem_of_pack_expansion_instantiation during the
3731 substituting of an argument pack (of which the Ith element is a pack
3732 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3733 In this case, the Ith element resulting from this substituting is going to
3734 be a pack expansion, which pattern is the pattern of E. Let's return the
3735 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3736 resulting pack expansion from it. */
3737 if (PACK_EXPANSION_P (arg))
3738 {
3739 /* Make sure we aren't throwing away arg info. */
3740 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3741 arg = PACK_EXPANSION_PATTERN (arg);
3742 }
3743
3744 return arg;
3745 }
3746
3747
3748 /* True iff FN is a function representing a built-in variadic parameter
3749 pack. */
3750
3751 bool
3752 builtin_pack_fn_p (tree fn)
3753 {
3754 if (!fn
3755 || TREE_CODE (fn) != FUNCTION_DECL
3756 || !DECL_IS_BUILTIN (fn))
3757 return false;
3758
3759 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3760 return true;
3761
3762 return false;
3763 }
3764
3765 /* True iff CALL is a call to a function representing a built-in variadic
3766 parameter pack. */
3767
3768 static bool
3769 builtin_pack_call_p (tree call)
3770 {
3771 if (TREE_CODE (call) != CALL_EXPR)
3772 return false;
3773 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3774 }
3775
3776 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3777
3778 static tree
3779 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3780 tree in_decl)
3781 {
3782 tree ohi = CALL_EXPR_ARG (call, 0);
3783 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3784 false/*fn*/, true/*int_cst*/);
3785
3786 if (value_dependent_expression_p (hi))
3787 {
3788 if (hi != ohi)
3789 {
3790 call = copy_node (call);
3791 CALL_EXPR_ARG (call, 0) = hi;
3792 }
3793 tree ex = make_pack_expansion (call, complain);
3794 tree vec = make_tree_vec (1);
3795 TREE_VEC_ELT (vec, 0) = ex;
3796 return vec;
3797 }
3798 else
3799 {
3800 hi = cxx_constant_value (hi);
3801 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3802
3803 /* Calculate the largest value of len that won't make the size of the vec
3804 overflow an int. The compiler will exceed resource limits long before
3805 this, but it seems a decent place to diagnose. */
3806 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3807
3808 if (len < 0 || len > max)
3809 {
3810 if ((complain & tf_error)
3811 && hi != error_mark_node)
3812 error ("argument to %<__integer_pack%> must be between 0 and %d",
3813 max);
3814 return error_mark_node;
3815 }
3816
3817 tree vec = make_tree_vec (len);
3818
3819 for (int i = 0; i < len; ++i)
3820 TREE_VEC_ELT (vec, i) = size_int (i);
3821
3822 return vec;
3823 }
3824 }
3825
3826 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3827 CALL. */
3828
3829 static tree
3830 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3831 tree in_decl)
3832 {
3833 if (!builtin_pack_call_p (call))
3834 return NULL_TREE;
3835
3836 tree fn = CALL_EXPR_FN (call);
3837
3838 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3839 return expand_integer_pack (call, args, complain, in_decl);
3840
3841 return NULL_TREE;
3842 }
3843
3844 /* Structure used to track the progress of find_parameter_packs_r. */
3845 struct find_parameter_pack_data
3846 {
3847 /* TREE_LIST that will contain all of the parameter packs found by
3848 the traversal. */
3849 tree* parameter_packs;
3850
3851 /* Set of AST nodes that have been visited by the traversal. */
3852 hash_set<tree> *visited;
3853
3854 /* True iff we're making a type pack expansion. */
3855 bool type_pack_expansion_p;
3856 };
3857
3858 /* Identifies all of the argument packs that occur in a template
3859 argument and appends them to the TREE_LIST inside DATA, which is a
3860 find_parameter_pack_data structure. This is a subroutine of
3861 make_pack_expansion and uses_parameter_packs. */
3862 static tree
3863 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3864 {
3865 tree t = *tp;
3866 struct find_parameter_pack_data* ppd =
3867 (struct find_parameter_pack_data*)data;
3868 bool parameter_pack_p = false;
3869
3870 /* Don't look through typedefs; we are interested in whether a
3871 parameter pack is actually written in the expression/type we're
3872 looking at, not the target type. */
3873 if (TYPE_P (t) && typedef_variant_p (t))
3874 {
3875 /* But do look at arguments for an alias template. */
3876 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3877 cp_walk_tree (&TI_ARGS (tinfo),
3878 &find_parameter_packs_r,
3879 ppd, ppd->visited);
3880 *walk_subtrees = 0;
3881 return NULL_TREE;
3882 }
3883
3884 /* Identify whether this is a parameter pack or not. */
3885 switch (TREE_CODE (t))
3886 {
3887 case TEMPLATE_PARM_INDEX:
3888 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3889 parameter_pack_p = true;
3890 break;
3891
3892 case TEMPLATE_TYPE_PARM:
3893 t = TYPE_MAIN_VARIANT (t);
3894 /* FALLTHRU */
3895 case TEMPLATE_TEMPLATE_PARM:
3896 /* If the placeholder appears in the decl-specifier-seq of a function
3897 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3898 is a pack expansion, the invented template parameter is a template
3899 parameter pack. */
3900 if (ppd->type_pack_expansion_p && is_auto (t))
3901 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3902 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3903 parameter_pack_p = true;
3904 break;
3905
3906 case FIELD_DECL:
3907 case PARM_DECL:
3908 if (DECL_PACK_P (t))
3909 {
3910 /* We don't want to walk into the type of a PARM_DECL,
3911 because we don't want to see the type parameter pack. */
3912 *walk_subtrees = 0;
3913 parameter_pack_p = true;
3914 }
3915 break;
3916
3917 case VAR_DECL:
3918 if (DECL_PACK_P (t))
3919 {
3920 /* We don't want to walk into the type of a variadic capture proxy,
3921 because we don't want to see the type parameter pack. */
3922 *walk_subtrees = 0;
3923 parameter_pack_p = true;
3924 }
3925 else if (variable_template_specialization_p (t))
3926 {
3927 cp_walk_tree (&DECL_TI_ARGS (t),
3928 find_parameter_packs_r,
3929 ppd, ppd->visited);
3930 *walk_subtrees = 0;
3931 }
3932 break;
3933
3934 case CALL_EXPR:
3935 if (builtin_pack_call_p (t))
3936 parameter_pack_p = true;
3937 break;
3938
3939 case BASES:
3940 parameter_pack_p = true;
3941 break;
3942 default:
3943 /* Not a parameter pack. */
3944 break;
3945 }
3946
3947 if (parameter_pack_p)
3948 {
3949 /* Add this parameter pack to the list. */
3950 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3951 }
3952
3953 if (TYPE_P (t))
3954 cp_walk_tree (&TYPE_CONTEXT (t),
3955 &find_parameter_packs_r, ppd, ppd->visited);
3956
3957 /* This switch statement will return immediately if we don't find a
3958 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3959 switch (TREE_CODE (t))
3960 {
3961 case BOUND_TEMPLATE_TEMPLATE_PARM:
3962 /* Check the template itself. */
3963 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3964 &find_parameter_packs_r, ppd, ppd->visited);
3965 return NULL_TREE;
3966
3967 case DECL_EXPR:
3968 {
3969 tree decl = DECL_EXPR_DECL (t);
3970 /* Ignore the declaration of a capture proxy for a parameter pack. */
3971 if (is_capture_proxy (decl))
3972 *walk_subtrees = 0;
3973 if (is_typedef_decl (decl))
3974 /* Since we stop at typedefs above, we need to look through them at
3975 the point of the DECL_EXPR. */
3976 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3977 &find_parameter_packs_r, ppd, ppd->visited);
3978 return NULL_TREE;
3979 }
3980
3981 case TEMPLATE_DECL:
3982 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3983 return NULL_TREE;
3984 cp_walk_tree (&TREE_TYPE (t),
3985 &find_parameter_packs_r, ppd, ppd->visited);
3986 return NULL_TREE;
3987
3988 case TYPE_PACK_EXPANSION:
3989 case EXPR_PACK_EXPANSION:
3990 *walk_subtrees = 0;
3991 return NULL_TREE;
3992
3993 case INTEGER_TYPE:
3994 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3995 ppd, ppd->visited);
3996 *walk_subtrees = 0;
3997 return NULL_TREE;
3998
3999 case IDENTIFIER_NODE:
4000 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4001 ppd->visited);
4002 *walk_subtrees = 0;
4003 return NULL_TREE;
4004
4005 case LAMBDA_EXPR:
4006 {
4007 /* Since we defer implicit capture, look in the parms and body. */
4008 tree fn = lambda_function (t);
4009 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4010 ppd->visited);
4011 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4012 ppd->visited);
4013 return NULL_TREE;
4014 }
4015
4016 case DECLTYPE_TYPE:
4017 {
4018 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4019 type_pack_expansion_p to false so that any placeholders
4020 within the expression don't get marked as parameter packs. */
4021 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4022 ppd->type_pack_expansion_p = false;
4023 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4024 ppd, ppd->visited);
4025 ppd->type_pack_expansion_p = type_pack_expansion_p;
4026 *walk_subtrees = 0;
4027 return NULL_TREE;
4028 }
4029
4030 case IF_STMT:
4031 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4032 ppd, ppd->visited);
4033 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4034 ppd, ppd->visited);
4035 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4036 ppd, ppd->visited);
4037 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4038 *walk_subtrees = 0;
4039 return NULL_TREE;
4040
4041 default:
4042 return NULL_TREE;
4043 }
4044
4045 return NULL_TREE;
4046 }
4047
4048 /* Determines if the expression or type T uses any parameter packs. */
4049 tree
4050 uses_parameter_packs (tree t)
4051 {
4052 tree parameter_packs = NULL_TREE;
4053 struct find_parameter_pack_data ppd;
4054 ppd.parameter_packs = &parameter_packs;
4055 ppd.visited = new hash_set<tree>;
4056 ppd.type_pack_expansion_p = false;
4057 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4058 delete ppd.visited;
4059 return parameter_packs;
4060 }
4061
4062 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4063 representation a base-class initializer into a parameter pack
4064 expansion. If all goes well, the resulting node will be an
4065 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4066 respectively. */
4067 tree
4068 make_pack_expansion (tree arg, tsubst_flags_t complain)
4069 {
4070 tree result;
4071 tree parameter_packs = NULL_TREE;
4072 bool for_types = false;
4073 struct find_parameter_pack_data ppd;
4074
4075 if (!arg || arg == error_mark_node)
4076 return arg;
4077
4078 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4079 {
4080 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4081 class initializer. In this case, the TREE_PURPOSE will be a
4082 _TYPE node (representing the base class expansion we're
4083 initializing) and the TREE_VALUE will be a TREE_LIST
4084 containing the initialization arguments.
4085
4086 The resulting expansion looks somewhat different from most
4087 expansions. Rather than returning just one _EXPANSION, we
4088 return a TREE_LIST whose TREE_PURPOSE is a
4089 TYPE_PACK_EXPANSION containing the bases that will be
4090 initialized. The TREE_VALUE will be identical to the
4091 original TREE_VALUE, which is a list of arguments that will
4092 be passed to each base. We do not introduce any new pack
4093 expansion nodes into the TREE_VALUE (although it is possible
4094 that some already exist), because the TREE_PURPOSE and
4095 TREE_VALUE all need to be expanded together with the same
4096 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4097 resulting TREE_PURPOSE will mention the parameter packs in
4098 both the bases and the arguments to the bases. */
4099 tree purpose;
4100 tree value;
4101 tree parameter_packs = NULL_TREE;
4102
4103 /* Determine which parameter packs will be used by the base
4104 class expansion. */
4105 ppd.visited = new hash_set<tree>;
4106 ppd.parameter_packs = &parameter_packs;
4107 ppd.type_pack_expansion_p = false;
4108 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4109 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4110 &ppd, ppd.visited);
4111
4112 if (parameter_packs == NULL_TREE)
4113 {
4114 if (complain & tf_error)
4115 error ("base initializer expansion %qT contains no parameter packs",
4116 arg);
4117 delete ppd.visited;
4118 return error_mark_node;
4119 }
4120
4121 if (TREE_VALUE (arg) != void_type_node)
4122 {
4123 /* Collect the sets of parameter packs used in each of the
4124 initialization arguments. */
4125 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4126 {
4127 /* Determine which parameter packs will be expanded in this
4128 argument. */
4129 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4130 &ppd, ppd.visited);
4131 }
4132 }
4133
4134 delete ppd.visited;
4135
4136 /* Create the pack expansion type for the base type. */
4137 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4138 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4139 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4140 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4141
4142 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4143 they will rarely be compared to anything. */
4144 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4145
4146 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4147 }
4148
4149 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4150 for_types = true;
4151
4152 /* Build the PACK_EXPANSION_* node. */
4153 result = for_types
4154 ? cxx_make_type (TYPE_PACK_EXPANSION)
4155 : make_node (EXPR_PACK_EXPANSION);
4156 SET_PACK_EXPANSION_PATTERN (result, arg);
4157 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4158 {
4159 /* Propagate type and const-expression information. */
4160 TREE_TYPE (result) = TREE_TYPE (arg);
4161 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4162 /* Mark this read now, since the expansion might be length 0. */
4163 mark_exp_read (arg);
4164 }
4165 else
4166 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4167 they will rarely be compared to anything. */
4168 SET_TYPE_STRUCTURAL_EQUALITY (result);
4169
4170 /* Determine which parameter packs will be expanded. */
4171 ppd.parameter_packs = &parameter_packs;
4172 ppd.visited = new hash_set<tree>;
4173 ppd.type_pack_expansion_p = TYPE_P (arg);
4174 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4175 delete ppd.visited;
4176
4177 /* Make sure we found some parameter packs. */
4178 if (parameter_packs == NULL_TREE)
4179 {
4180 if (complain & tf_error)
4181 {
4182 if (TYPE_P (arg))
4183 error ("expansion pattern %qT contains no parameter packs", arg);
4184 else
4185 error ("expansion pattern %qE contains no parameter packs", arg);
4186 }
4187 return error_mark_node;
4188 }
4189 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4190
4191 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4192
4193 return result;
4194 }
4195
4196 /* Checks T for any "bare" parameter packs, which have not yet been
4197 expanded, and issues an error if any are found. This operation can
4198 only be done on full expressions or types (e.g., an expression
4199 statement, "if" condition, etc.), because we could have expressions like:
4200
4201 foo(f(g(h(args)))...)
4202
4203 where "args" is a parameter pack. check_for_bare_parameter_packs
4204 should not be called for the subexpressions args, h(args),
4205 g(h(args)), or f(g(h(args))), because we would produce erroneous
4206 error messages.
4207
4208 Returns TRUE and emits an error if there were bare parameter packs,
4209 returns FALSE otherwise. */
4210 bool
4211 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4212 {
4213 tree parameter_packs = NULL_TREE;
4214 struct find_parameter_pack_data ppd;
4215
4216 if (!processing_template_decl || !t || t == error_mark_node)
4217 return false;
4218
4219 /* A lambda might use a parameter pack from the containing context. */
4220 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4221 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4222 return false;
4223
4224 if (TREE_CODE (t) == TYPE_DECL)
4225 t = TREE_TYPE (t);
4226
4227 ppd.parameter_packs = &parameter_packs;
4228 ppd.visited = new hash_set<tree>;
4229 ppd.type_pack_expansion_p = false;
4230 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4231 delete ppd.visited;
4232
4233 if (parameter_packs)
4234 {
4235 if (loc == UNKNOWN_LOCATION)
4236 loc = cp_expr_loc_or_input_loc (t);
4237 error_at (loc, "parameter packs not expanded with %<...%>:");
4238 while (parameter_packs)
4239 {
4240 tree pack = TREE_VALUE (parameter_packs);
4241 tree name = NULL_TREE;
4242
4243 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4244 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4245 name = TYPE_NAME (pack);
4246 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4247 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4248 else if (TREE_CODE (pack) == CALL_EXPR)
4249 name = DECL_NAME (CALL_EXPR_FN (pack));
4250 else
4251 name = DECL_NAME (pack);
4252
4253 if (name)
4254 inform (loc, " %qD", name);
4255 else
4256 inform (loc, " %s", "<anonymous>");
4257
4258 parameter_packs = TREE_CHAIN (parameter_packs);
4259 }
4260
4261 return true;
4262 }
4263
4264 return false;
4265 }
4266
4267 /* Expand any parameter packs that occur in the template arguments in
4268 ARGS. */
4269 tree
4270 expand_template_argument_pack (tree args)
4271 {
4272 if (args == error_mark_node)
4273 return error_mark_node;
4274
4275 tree result_args = NULL_TREE;
4276 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4277 int num_result_args = -1;
4278 int non_default_args_count = -1;
4279
4280 /* First, determine if we need to expand anything, and the number of
4281 slots we'll need. */
4282 for (in_arg = 0; in_arg < nargs; ++in_arg)
4283 {
4284 tree arg = TREE_VEC_ELT (args, in_arg);
4285 if (arg == NULL_TREE)
4286 return args;
4287 if (ARGUMENT_PACK_P (arg))
4288 {
4289 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4290 if (num_result_args < 0)
4291 num_result_args = in_arg + num_packed;
4292 else
4293 num_result_args += num_packed;
4294 }
4295 else
4296 {
4297 if (num_result_args >= 0)
4298 num_result_args++;
4299 }
4300 }
4301
4302 /* If no expansion is necessary, we're done. */
4303 if (num_result_args < 0)
4304 return args;
4305
4306 /* Expand arguments. */
4307 result_args = make_tree_vec (num_result_args);
4308 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4309 non_default_args_count =
4310 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4311 for (in_arg = 0; in_arg < nargs; ++in_arg)
4312 {
4313 tree arg = TREE_VEC_ELT (args, in_arg);
4314 if (ARGUMENT_PACK_P (arg))
4315 {
4316 tree packed = ARGUMENT_PACK_ARGS (arg);
4317 int i, num_packed = TREE_VEC_LENGTH (packed);
4318 for (i = 0; i < num_packed; ++i, ++out_arg)
4319 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4320 if (non_default_args_count > 0)
4321 non_default_args_count += num_packed - 1;
4322 }
4323 else
4324 {
4325 TREE_VEC_ELT (result_args, out_arg) = arg;
4326 ++out_arg;
4327 }
4328 }
4329 if (non_default_args_count >= 0)
4330 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4331 return result_args;
4332 }
4333
4334 /* Checks if DECL shadows a template parameter.
4335
4336 [temp.local]: A template-parameter shall not be redeclared within its
4337 scope (including nested scopes).
4338
4339 Emits an error and returns TRUE if the DECL shadows a parameter,
4340 returns FALSE otherwise. */
4341
4342 bool
4343 check_template_shadow (tree decl)
4344 {
4345 tree olddecl;
4346
4347 /* If we're not in a template, we can't possibly shadow a template
4348 parameter. */
4349 if (!current_template_parms)
4350 return true;
4351
4352 /* Figure out what we're shadowing. */
4353 decl = OVL_FIRST (decl);
4354 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4355
4356 /* If there's no previous binding for this name, we're not shadowing
4357 anything, let alone a template parameter. */
4358 if (!olddecl)
4359 return true;
4360
4361 /* If we're not shadowing a template parameter, we're done. Note
4362 that OLDDECL might be an OVERLOAD (or perhaps even an
4363 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4364 node. */
4365 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4366 return true;
4367
4368 /* We check for decl != olddecl to avoid bogus errors for using a
4369 name inside a class. We check TPFI to avoid duplicate errors for
4370 inline member templates. */
4371 if (decl == olddecl
4372 || (DECL_TEMPLATE_PARM_P (decl)
4373 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4374 return true;
4375
4376 /* Don't complain about the injected class name, as we've already
4377 complained about the class itself. */
4378 if (DECL_SELF_REFERENCE_P (decl))
4379 return false;
4380
4381 if (DECL_TEMPLATE_PARM_P (decl))
4382 error ("declaration of template parameter %q+D shadows "
4383 "template parameter", decl);
4384 else
4385 error ("declaration of %q+#D shadows template parameter", decl);
4386 inform (DECL_SOURCE_LOCATION (olddecl),
4387 "template parameter %qD declared here", olddecl);
4388 return false;
4389 }
4390
4391 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4392 ORIG_LEVEL, DECL, and TYPE. */
4393
4394 static tree
4395 build_template_parm_index (int index,
4396 int level,
4397 int orig_level,
4398 tree decl,
4399 tree type)
4400 {
4401 tree t = make_node (TEMPLATE_PARM_INDEX);
4402 TEMPLATE_PARM_IDX (t) = index;
4403 TEMPLATE_PARM_LEVEL (t) = level;
4404 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4405 TEMPLATE_PARM_DECL (t) = decl;
4406 TREE_TYPE (t) = type;
4407 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4408 TREE_READONLY (t) = TREE_READONLY (decl);
4409
4410 return t;
4411 }
4412
4413 /* Find the canonical type parameter for the given template type
4414 parameter. Returns the canonical type parameter, which may be TYPE
4415 if no such parameter existed. */
4416
4417 static tree
4418 canonical_type_parameter (tree type)
4419 {
4420 int idx = TEMPLATE_TYPE_IDX (type);
4421
4422 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4423
4424 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4425 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4426
4427 for (tree list = (*canonical_template_parms)[idx];
4428 list; list = TREE_CHAIN (list))
4429 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4430 return TREE_VALUE (list);
4431
4432 (*canonical_template_parms)[idx]
4433 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4434 return type;
4435 }
4436
4437 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4438 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4439 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4440 new one is created. */
4441
4442 static tree
4443 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4444 tsubst_flags_t complain)
4445 {
4446 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4447 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4448 != TEMPLATE_PARM_LEVEL (index) - levels)
4449 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4450 {
4451 tree orig_decl = TEMPLATE_PARM_DECL (index);
4452
4453 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4454 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4455 type);
4456 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4457 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4458 DECL_ARTIFICIAL (decl) = 1;
4459 SET_DECL_TEMPLATE_PARM_P (decl);
4460
4461 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4462 TEMPLATE_PARM_LEVEL (index) - levels,
4463 TEMPLATE_PARM_ORIG_LEVEL (index),
4464 decl, type);
4465 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4466 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4467 = TEMPLATE_PARM_PARAMETER_PACK (index);
4468
4469 /* Template template parameters need this. */
4470 tree inner = decl;
4471 if (TREE_CODE (decl) == TEMPLATE_DECL)
4472 {
4473 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4474 TYPE_DECL, DECL_NAME (decl), type);
4475 DECL_TEMPLATE_RESULT (decl) = inner;
4476 DECL_ARTIFICIAL (inner) = true;
4477 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4478 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4479 }
4480
4481 /* Attach the TPI to the decl. */
4482 if (TREE_CODE (inner) == TYPE_DECL)
4483 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4484 else
4485 DECL_INITIAL (decl) = tpi;
4486 }
4487
4488 return TEMPLATE_PARM_DESCENDANTS (index);
4489 }
4490
4491 /* Process information from new template parameter PARM and append it
4492 to the LIST being built. This new parameter is a non-type
4493 parameter iff IS_NON_TYPE is true. This new parameter is a
4494 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4495 is in PARM_LOC. */
4496
4497 tree
4498 process_template_parm (tree list, location_t parm_loc, tree parm,
4499 bool is_non_type, bool is_parameter_pack)
4500 {
4501 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4502 tree prev = NULL_TREE;
4503 int idx = 0;
4504
4505 if (list)
4506 {
4507 prev = tree_last (list);
4508
4509 tree p = TREE_VALUE (prev);
4510 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4511 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4512 else if (TREE_CODE (p) == PARM_DECL)
4513 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4514
4515 ++idx;
4516 }
4517
4518 tree decl = NULL_TREE;
4519 tree defval = TREE_PURPOSE (parm);
4520 tree constr = TREE_TYPE (parm);
4521
4522 if (is_non_type)
4523 {
4524 parm = TREE_VALUE (parm);
4525
4526 SET_DECL_TEMPLATE_PARM_P (parm);
4527
4528 if (TREE_TYPE (parm) != error_mark_node)
4529 {
4530 /* [temp.param]
4531
4532 The top-level cv-qualifiers on the template-parameter are
4533 ignored when determining its type. */
4534 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4535 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4536 TREE_TYPE (parm) = error_mark_node;
4537 else if (uses_parameter_packs (TREE_TYPE (parm))
4538 && !is_parameter_pack
4539 /* If we're in a nested template parameter list, the template
4540 template parameter could be a parameter pack. */
4541 && processing_template_parmlist == 1)
4542 {
4543 /* This template parameter is not a parameter pack, but it
4544 should be. Complain about "bare" parameter packs. */
4545 check_for_bare_parameter_packs (TREE_TYPE (parm));
4546
4547 /* Recover by calling this a parameter pack. */
4548 is_parameter_pack = true;
4549 }
4550 }
4551
4552 /* A template parameter is not modifiable. */
4553 TREE_CONSTANT (parm) = 1;
4554 TREE_READONLY (parm) = 1;
4555 decl = build_decl (parm_loc,
4556 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4557 TREE_CONSTANT (decl) = 1;
4558 TREE_READONLY (decl) = 1;
4559 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4560 = build_template_parm_index (idx, processing_template_decl,
4561 processing_template_decl,
4562 decl, TREE_TYPE (parm));
4563
4564 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4565 = is_parameter_pack;
4566 }
4567 else
4568 {
4569 tree t;
4570 parm = TREE_VALUE (TREE_VALUE (parm));
4571
4572 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4573 {
4574 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4575 /* This is for distinguishing between real templates and template
4576 template parameters */
4577 TREE_TYPE (parm) = t;
4578
4579 /* any_template_parm_r expects to be able to get the targs of a
4580 DECL_TEMPLATE_RESULT. */
4581 tree result = DECL_TEMPLATE_RESULT (parm);
4582 TREE_TYPE (result) = t;
4583 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4584 tree tinfo = build_template_info (parm, args);
4585 retrofit_lang_decl (result);
4586 DECL_TEMPLATE_INFO (result) = tinfo;
4587
4588 decl = parm;
4589 }
4590 else
4591 {
4592 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4593 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4594 decl = build_decl (parm_loc,
4595 TYPE_DECL, parm, t);
4596 }
4597
4598 TYPE_NAME (t) = decl;
4599 TYPE_STUB_DECL (t) = decl;
4600 parm = decl;
4601 TEMPLATE_TYPE_PARM_INDEX (t)
4602 = build_template_parm_index (idx, processing_template_decl,
4603 processing_template_decl,
4604 decl, TREE_TYPE (parm));
4605 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4606 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4607 SET_TYPE_STRUCTURAL_EQUALITY (t);
4608 else
4609 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4610 }
4611 DECL_ARTIFICIAL (decl) = 1;
4612 SET_DECL_TEMPLATE_PARM_P (decl);
4613
4614 /* Build requirements for the type/template parameter.
4615 This must be done after SET_DECL_TEMPLATE_PARM_P or
4616 process_template_parm could fail. */
4617 tree reqs = finish_shorthand_constraint (parm, constr);
4618
4619 decl = pushdecl (decl);
4620 if (!is_non_type)
4621 parm = decl;
4622
4623 /* Build the parameter node linking the parameter declaration,
4624 its default argument (if any), and its constraints (if any). */
4625 parm = build_tree_list (defval, parm);
4626 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4627
4628 if (prev)
4629 TREE_CHAIN (prev) = parm;
4630 else
4631 list = parm;
4632
4633 return list;
4634 }
4635
4636 /* The end of a template parameter list has been reached. Process the
4637 tree list into a parameter vector, converting each parameter into a more
4638 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4639 as PARM_DECLs. */
4640
4641 tree
4642 end_template_parm_list (tree parms)
4643 {
4644 tree saved_parmlist = make_tree_vec (list_length (parms));
4645
4646 /* Pop the dummy parameter level and add the real one. We do not
4647 morph the dummy parameter in place, as it might have been
4648 captured by a (nested) template-template-parm. */
4649 current_template_parms = TREE_CHAIN (current_template_parms);
4650
4651 current_template_parms
4652 = tree_cons (size_int (processing_template_decl),
4653 saved_parmlist, current_template_parms);
4654
4655 for (unsigned ix = 0; parms; ix++)
4656 {
4657 tree parm = parms;
4658 parms = TREE_CHAIN (parms);
4659 TREE_CHAIN (parm) = NULL_TREE;
4660
4661 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4662 }
4663
4664 --processing_template_parmlist;
4665
4666 return saved_parmlist;
4667 }
4668
4669 // Explicitly indicate the end of the template parameter list. We assume
4670 // that the current template parameters have been constructed and/or
4671 // managed explicitly, as when creating new template template parameters
4672 // from a shorthand constraint.
4673 void
4674 end_template_parm_list ()
4675 {
4676 --processing_template_parmlist;
4677 }
4678
4679 /* end_template_decl is called after a template declaration is seen. */
4680
4681 void
4682 end_template_decl (void)
4683 {
4684 reset_specialization ();
4685
4686 if (! processing_template_decl)
4687 return;
4688
4689 /* This matches the pushlevel in begin_template_parm_list. */
4690 finish_scope ();
4691
4692 --processing_template_decl;
4693 current_template_parms = TREE_CHAIN (current_template_parms);
4694 }
4695
4696 /* Takes a TREE_LIST representing a template parameter and convert it
4697 into an argument suitable to be passed to the type substitution
4698 functions. Note that If the TREE_LIST contains an error_mark
4699 node, the returned argument is error_mark_node. */
4700
4701 tree
4702 template_parm_to_arg (tree t)
4703 {
4704
4705 if (t == NULL_TREE
4706 || TREE_CODE (t) != TREE_LIST)
4707 return t;
4708
4709 if (error_operand_p (TREE_VALUE (t)))
4710 return error_mark_node;
4711
4712 t = TREE_VALUE (t);
4713
4714 if (TREE_CODE (t) == TYPE_DECL
4715 || TREE_CODE (t) == TEMPLATE_DECL)
4716 {
4717 t = TREE_TYPE (t);
4718
4719 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4720 {
4721 /* Turn this argument into a TYPE_ARGUMENT_PACK
4722 with a single element, which expands T. */
4723 tree vec = make_tree_vec (1);
4724 if (CHECKING_P)
4725 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4726
4727 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4728
4729 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4730 SET_ARGUMENT_PACK_ARGS (t, vec);
4731 }
4732 }
4733 else
4734 {
4735 t = DECL_INITIAL (t);
4736
4737 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4738 {
4739 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4740 with a single element, which expands T. */
4741 tree vec = make_tree_vec (1);
4742 if (CHECKING_P)
4743 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4744
4745 t = convert_from_reference (t);
4746 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4747
4748 t = make_node (NONTYPE_ARGUMENT_PACK);
4749 SET_ARGUMENT_PACK_ARGS (t, vec);
4750 }
4751 else
4752 t = convert_from_reference (t);
4753 }
4754 return t;
4755 }
4756
4757 /* Given a single level of template parameters (a TREE_VEC), return it
4758 as a set of template arguments. */
4759
4760 tree
4761 template_parms_level_to_args (tree parms)
4762 {
4763 tree a = copy_node (parms);
4764 TREE_TYPE (a) = NULL_TREE;
4765 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4766 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4767
4768 if (CHECKING_P)
4769 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4770
4771 return a;
4772 }
4773
4774 /* Given a set of template parameters, return them as a set of template
4775 arguments. The template parameters are represented as a TREE_VEC, in
4776 the form documented in cp-tree.h for template arguments. */
4777
4778 tree
4779 template_parms_to_args (tree parms)
4780 {
4781 tree header;
4782 tree args = NULL_TREE;
4783 int length = TMPL_PARMS_DEPTH (parms);
4784 int l = length;
4785
4786 /* If there is only one level of template parameters, we do not
4787 create a TREE_VEC of TREE_VECs. Instead, we return a single
4788 TREE_VEC containing the arguments. */
4789 if (length > 1)
4790 args = make_tree_vec (length);
4791
4792 for (header = parms; header; header = TREE_CHAIN (header))
4793 {
4794 tree a = template_parms_level_to_args (TREE_VALUE (header));
4795
4796 if (length > 1)
4797 TREE_VEC_ELT (args, --l) = a;
4798 else
4799 args = a;
4800 }
4801
4802 return args;
4803 }
4804
4805 /* Within the declaration of a template, return the currently active
4806 template parameters as an argument TREE_VEC. */
4807
4808 static tree
4809 current_template_args (void)
4810 {
4811 return template_parms_to_args (current_template_parms);
4812 }
4813
4814 /* Return the fully generic arguments for of TMPL, i.e. what
4815 current_template_args would be while parsing it. */
4816
4817 tree
4818 generic_targs_for (tree tmpl)
4819 {
4820 if (tmpl == NULL_TREE)
4821 return NULL_TREE;
4822 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4823 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4824 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4825 template parameter, it has no TEMPLATE_INFO; for a partial
4826 specialization, it has the arguments for the primary template, and we
4827 want the arguments for the partial specialization. */;
4828 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4829 if (tree ti = get_template_info (result))
4830 return TI_ARGS (ti);
4831 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4832 }
4833
4834 /* Update the declared TYPE by doing any lookups which were thought to be
4835 dependent, but are not now that we know the SCOPE of the declarator. */
4836
4837 tree
4838 maybe_update_decl_type (tree orig_type, tree scope)
4839 {
4840 tree type = orig_type;
4841
4842 if (type == NULL_TREE)
4843 return type;
4844
4845 if (TREE_CODE (orig_type) == TYPE_DECL)
4846 type = TREE_TYPE (type);
4847
4848 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4849 && dependent_type_p (type)
4850 /* Don't bother building up the args in this case. */
4851 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4852 {
4853 /* tsubst in the args corresponding to the template parameters,
4854 including auto if present. Most things will be unchanged, but
4855 make_typename_type and tsubst_qualified_id will resolve
4856 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4857 tree args = current_template_args ();
4858 tree auto_node = type_uses_auto (type);
4859 tree pushed;
4860 if (auto_node)
4861 {
4862 tree auto_vec = make_tree_vec (1);
4863 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4864 args = add_to_template_args (args, auto_vec);
4865 }
4866 pushed = push_scope (scope);
4867 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4868 if (pushed)
4869 pop_scope (scope);
4870 }
4871
4872 if (type == error_mark_node)
4873 return orig_type;
4874
4875 if (TREE_CODE (orig_type) == TYPE_DECL)
4876 {
4877 if (same_type_p (type, TREE_TYPE (orig_type)))
4878 type = orig_type;
4879 else
4880 type = TYPE_NAME (type);
4881 }
4882 return type;
4883 }
4884
4885 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4886 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4887 the new template is a member template. */
4888
4889 static tree
4890 build_template_decl (tree decl, tree parms, bool member_template_p)
4891 {
4892 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4893 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4894 DECL_TEMPLATE_PARMS (tmpl) = parms;
4895 DECL_TEMPLATE_RESULT (tmpl) = decl;
4896 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4897 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4898 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4899 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4900
4901 return tmpl;
4902 }
4903
4904 struct template_parm_data
4905 {
4906 /* The level of the template parameters we are currently
4907 processing. */
4908 int level;
4909
4910 /* The index of the specialization argument we are currently
4911 processing. */
4912 int current_arg;
4913
4914 /* An array whose size is the number of template parameters. The
4915 elements are nonzero if the parameter has been used in any one
4916 of the arguments processed so far. */
4917 int* parms;
4918
4919 /* An array whose size is the number of template arguments. The
4920 elements are nonzero if the argument makes use of template
4921 parameters of this level. */
4922 int* arg_uses_template_parms;
4923 };
4924
4925 /* Subroutine of push_template_decl used to see if each template
4926 parameter in a partial specialization is used in the explicit
4927 argument list. If T is of the LEVEL given in DATA (which is
4928 treated as a template_parm_data*), then DATA->PARMS is marked
4929 appropriately. */
4930
4931 static int
4932 mark_template_parm (tree t, void* data)
4933 {
4934 int level;
4935 int idx;
4936 struct template_parm_data* tpd = (struct template_parm_data*) data;
4937
4938 template_parm_level_and_index (t, &level, &idx);
4939
4940 if (level == tpd->level)
4941 {
4942 tpd->parms[idx] = 1;
4943 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4944 }
4945
4946 /* In C++17 the type of a non-type argument is a deduced context. */
4947 if (cxx_dialect >= cxx17
4948 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4949 for_each_template_parm (TREE_TYPE (t),
4950 &mark_template_parm,
4951 data,
4952 NULL,
4953 /*include_nondeduced_p=*/false);
4954
4955 /* Return zero so that for_each_template_parm will continue the
4956 traversal of the tree; we want to mark *every* template parm. */
4957 return 0;
4958 }
4959
4960 /* Process the partial specialization DECL. */
4961
4962 static tree
4963 process_partial_specialization (tree decl)
4964 {
4965 tree type = TREE_TYPE (decl);
4966 tree tinfo = get_template_info (decl);
4967 tree maintmpl = TI_TEMPLATE (tinfo);
4968 tree specargs = TI_ARGS (tinfo);
4969 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4970 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4971 tree inner_parms;
4972 tree inst;
4973 int nargs = TREE_VEC_LENGTH (inner_args);
4974 int ntparms;
4975 int i;
4976 bool did_error_intro = false;
4977 struct template_parm_data tpd;
4978 struct template_parm_data tpd2;
4979
4980 gcc_assert (current_template_parms);
4981
4982 /* A concept cannot be specialized. */
4983 if (flag_concepts && variable_concept_p (maintmpl))
4984 {
4985 error ("specialization of variable concept %q#D", maintmpl);
4986 return error_mark_node;
4987 }
4988
4989 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4990 ntparms = TREE_VEC_LENGTH (inner_parms);
4991
4992 /* We check that each of the template parameters given in the
4993 partial specialization is used in the argument list to the
4994 specialization. For example:
4995
4996 template <class T> struct S;
4997 template <class T> struct S<T*>;
4998
4999 The second declaration is OK because `T*' uses the template
5000 parameter T, whereas
5001
5002 template <class T> struct S<int>;
5003
5004 is no good. Even trickier is:
5005
5006 template <class T>
5007 struct S1
5008 {
5009 template <class U>
5010 struct S2;
5011 template <class U>
5012 struct S2<T>;
5013 };
5014
5015 The S2<T> declaration is actually invalid; it is a
5016 full-specialization. Of course,
5017
5018 template <class U>
5019 struct S2<T (*)(U)>;
5020
5021 or some such would have been OK. */
5022 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5023 tpd.parms = XALLOCAVEC (int, ntparms);
5024 memset (tpd.parms, 0, sizeof (int) * ntparms);
5025
5026 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5027 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5028 for (i = 0; i < nargs; ++i)
5029 {
5030 tpd.current_arg = i;
5031 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5032 &mark_template_parm,
5033 &tpd,
5034 NULL,
5035 /*include_nondeduced_p=*/false);
5036 }
5037 for (i = 0; i < ntparms; ++i)
5038 if (tpd.parms[i] == 0)
5039 {
5040 /* One of the template parms was not used in a deduced context in the
5041 specialization. */
5042 if (!did_error_intro)
5043 {
5044 error ("template parameters not deducible in "
5045 "partial specialization:");
5046 did_error_intro = true;
5047 }
5048
5049 inform (input_location, " %qD",
5050 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5051 }
5052
5053 if (did_error_intro)
5054 return error_mark_node;
5055
5056 /* [temp.class.spec]
5057
5058 The argument list of the specialization shall not be identical to
5059 the implicit argument list of the primary template. */
5060 tree main_args
5061 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5062 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5063 && (!flag_concepts
5064 || !strictly_subsumes (current_template_constraints (),
5065 inner_args, maintmpl)))
5066 {
5067 if (!flag_concepts)
5068 error ("partial specialization %q+D does not specialize "
5069 "any template arguments; to define the primary template, "
5070 "remove the template argument list", decl);
5071 else
5072 error ("partial specialization %q+D does not specialize any "
5073 "template arguments and is not more constrained than "
5074 "the primary template; to define the primary template, "
5075 "remove the template argument list", decl);
5076 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5077 }
5078
5079 /* A partial specialization that replaces multiple parameters of the
5080 primary template with a pack expansion is less specialized for those
5081 parameters. */
5082 if (nargs < DECL_NTPARMS (maintmpl))
5083 {
5084 error ("partial specialization is not more specialized than the "
5085 "primary template because it replaces multiple parameters "
5086 "with a pack expansion");
5087 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5088 /* Avoid crash in process_partial_specialization. */
5089 return decl;
5090 }
5091
5092 else if (nargs > DECL_NTPARMS (maintmpl))
5093 {
5094 error ("too many arguments for partial specialization %qT", type);
5095 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5096 /* Avoid crash below. */
5097 return decl;
5098 }
5099
5100 /* If we aren't in a dependent class, we can actually try deduction. */
5101 else if (tpd.level == 1
5102 /* FIXME we should be able to handle a partial specialization of a
5103 partial instantiation, but currently we can't (c++/41727). */
5104 && TMPL_ARGS_DEPTH (specargs) == 1
5105 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5106 {
5107 auto_diagnostic_group d;
5108 if (permerror (input_location, "partial specialization %qD is not "
5109 "more specialized than", decl))
5110 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5111 maintmpl);
5112 }
5113
5114 /* [temp.class.spec]
5115
5116 A partially specialized non-type argument expression shall not
5117 involve template parameters of the partial specialization except
5118 when the argument expression is a simple identifier.
5119
5120 The type of a template parameter corresponding to a specialized
5121 non-type argument shall not be dependent on a parameter of the
5122 specialization.
5123
5124 Also, we verify that pack expansions only occur at the
5125 end of the argument list. */
5126 tpd2.parms = 0;
5127 for (i = 0; i < nargs; ++i)
5128 {
5129 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5130 tree arg = TREE_VEC_ELT (inner_args, i);
5131 tree packed_args = NULL_TREE;
5132 int j, len = 1;
5133
5134 if (ARGUMENT_PACK_P (arg))
5135 {
5136 /* Extract the arguments from the argument pack. We'll be
5137 iterating over these in the following loop. */
5138 packed_args = ARGUMENT_PACK_ARGS (arg);
5139 len = TREE_VEC_LENGTH (packed_args);
5140 }
5141
5142 for (j = 0; j < len; j++)
5143 {
5144 if (packed_args)
5145 /* Get the Jth argument in the parameter pack. */
5146 arg = TREE_VEC_ELT (packed_args, j);
5147
5148 if (PACK_EXPANSION_P (arg))
5149 {
5150 /* Pack expansions must come at the end of the
5151 argument list. */
5152 if ((packed_args && j < len - 1)
5153 || (!packed_args && i < nargs - 1))
5154 {
5155 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5156 error ("parameter pack argument %qE must be at the "
5157 "end of the template argument list", arg);
5158 else
5159 error ("parameter pack argument %qT must be at the "
5160 "end of the template argument list", arg);
5161 }
5162 }
5163
5164 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5165 /* We only care about the pattern. */
5166 arg = PACK_EXPANSION_PATTERN (arg);
5167
5168 if (/* These first two lines are the `non-type' bit. */
5169 !TYPE_P (arg)
5170 && TREE_CODE (arg) != TEMPLATE_DECL
5171 /* This next two lines are the `argument expression is not just a
5172 simple identifier' condition and also the `specialized
5173 non-type argument' bit. */
5174 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5175 && !((REFERENCE_REF_P (arg)
5176 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5177 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5178 {
5179 if ((!packed_args && tpd.arg_uses_template_parms[i])
5180 || (packed_args && uses_template_parms (arg)))
5181 error_at (cp_expr_loc_or_input_loc (arg),
5182 "template argument %qE involves template "
5183 "parameter(s)", arg);
5184 else
5185 {
5186 /* Look at the corresponding template parameter,
5187 marking which template parameters its type depends
5188 upon. */
5189 tree type = TREE_TYPE (parm);
5190
5191 if (!tpd2.parms)
5192 {
5193 /* We haven't yet initialized TPD2. Do so now. */
5194 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5195 /* The number of parameters here is the number in the
5196 main template, which, as checked in the assertion
5197 above, is NARGS. */
5198 tpd2.parms = XALLOCAVEC (int, nargs);
5199 tpd2.level =
5200 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5201 }
5202
5203 /* Mark the template parameters. But this time, we're
5204 looking for the template parameters of the main
5205 template, not in the specialization. */
5206 tpd2.current_arg = i;
5207 tpd2.arg_uses_template_parms[i] = 0;
5208 memset (tpd2.parms, 0, sizeof (int) * nargs);
5209 for_each_template_parm (type,
5210 &mark_template_parm,
5211 &tpd2,
5212 NULL,
5213 /*include_nondeduced_p=*/false);
5214
5215 if (tpd2.arg_uses_template_parms [i])
5216 {
5217 /* The type depended on some template parameters.
5218 If they are fully specialized in the
5219 specialization, that's OK. */
5220 int j;
5221 int count = 0;
5222 for (j = 0; j < nargs; ++j)
5223 if (tpd2.parms[j] != 0
5224 && tpd.arg_uses_template_parms [j])
5225 ++count;
5226 if (count != 0)
5227 error_n (input_location, count,
5228 "type %qT of template argument %qE depends "
5229 "on a template parameter",
5230 "type %qT of template argument %qE depends "
5231 "on template parameters",
5232 type,
5233 arg);
5234 }
5235 }
5236 }
5237 }
5238 }
5239
5240 /* We should only get here once. */
5241 if (TREE_CODE (decl) == TYPE_DECL)
5242 gcc_assert (!COMPLETE_TYPE_P (type));
5243
5244 // Build the template decl.
5245 tree tmpl = build_template_decl (decl, current_template_parms,
5246 DECL_MEMBER_TEMPLATE_P (maintmpl));
5247 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5248 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5249 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5250
5251 /* Give template template parms a DECL_CONTEXT of the template
5252 for which they are a parameter. */
5253 for (i = 0; i < ntparms; ++i)
5254 {
5255 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5256 if (TREE_CODE (parm) == TEMPLATE_DECL)
5257 DECL_CONTEXT (parm) = tmpl;
5258 }
5259
5260 if (VAR_P (decl))
5261 /* We didn't register this in check_explicit_specialization so we could
5262 wait until the constraints were set. */
5263 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5264 else
5265 associate_classtype_constraints (type);
5266
5267 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5268 = tree_cons (specargs, tmpl,
5269 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5270 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5271
5272 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5273 inst = TREE_CHAIN (inst))
5274 {
5275 tree instance = TREE_VALUE (inst);
5276 if (TYPE_P (instance)
5277 ? (COMPLETE_TYPE_P (instance)
5278 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5279 : DECL_TEMPLATE_INSTANTIATION (instance))
5280 {
5281 tree spec = most_specialized_partial_spec (instance, tf_none);
5282 tree inst_decl = (DECL_P (instance)
5283 ? instance : TYPE_NAME (instance));
5284 if (!spec)
5285 /* OK */;
5286 else if (spec == error_mark_node)
5287 permerror (input_location,
5288 "declaration of %qD ambiguates earlier template "
5289 "instantiation for %qD", decl, inst_decl);
5290 else if (TREE_VALUE (spec) == tmpl)
5291 permerror (input_location,
5292 "partial specialization of %qD after instantiation "
5293 "of %qD", decl, inst_decl);
5294 }
5295 }
5296
5297 return decl;
5298 }
5299
5300 /* PARM is a template parameter of some form; return the corresponding
5301 TEMPLATE_PARM_INDEX. */
5302
5303 static tree
5304 get_template_parm_index (tree parm)
5305 {
5306 if (TREE_CODE (parm) == PARM_DECL
5307 || TREE_CODE (parm) == CONST_DECL)
5308 parm = DECL_INITIAL (parm);
5309 else if (TREE_CODE (parm) == TYPE_DECL
5310 || TREE_CODE (parm) == TEMPLATE_DECL)
5311 parm = TREE_TYPE (parm);
5312 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5313 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5314 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5315 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5316 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5317 return parm;
5318 }
5319
5320 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5321 parameter packs used by the template parameter PARM. */
5322
5323 static void
5324 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5325 {
5326 /* A type parm can't refer to another parm. */
5327 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5328 return;
5329 else if (TREE_CODE (parm) == PARM_DECL)
5330 {
5331 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5332 ppd, ppd->visited);
5333 return;
5334 }
5335
5336 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5337
5338 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5339 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5340 {
5341 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5342 if (template_parameter_pack_p (p))
5343 /* Any packs in the type are expanded by this parameter. */;
5344 else
5345 fixed_parameter_pack_p_1 (p, ppd);
5346 }
5347 }
5348
5349 /* PARM is a template parameter pack. Return any parameter packs used in
5350 its type or the type of any of its template parameters. If there are
5351 any such packs, it will be instantiated into a fixed template parameter
5352 list by partial instantiation rather than be fully deduced. */
5353
5354 tree
5355 fixed_parameter_pack_p (tree parm)
5356 {
5357 /* This can only be true in a member template. */
5358 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5359 return NULL_TREE;
5360 /* This can only be true for a parameter pack. */
5361 if (!template_parameter_pack_p (parm))
5362 return NULL_TREE;
5363 /* A type parm can't refer to another parm. */
5364 if (TREE_CODE (parm) == TYPE_DECL)
5365 return NULL_TREE;
5366
5367 tree parameter_packs = NULL_TREE;
5368 struct find_parameter_pack_data ppd;
5369 ppd.parameter_packs = &parameter_packs;
5370 ppd.visited = new hash_set<tree>;
5371 ppd.type_pack_expansion_p = false;
5372
5373 fixed_parameter_pack_p_1 (parm, &ppd);
5374
5375 delete ppd.visited;
5376 return parameter_packs;
5377 }
5378
5379 /* Check that a template declaration's use of default arguments and
5380 parameter packs is not invalid. Here, PARMS are the template
5381 parameters. IS_PRIMARY is true if DECL is the thing declared by
5382 a primary template. IS_PARTIAL is true if DECL is a partial
5383 specialization.
5384
5385 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5386 function template declaration or a friend class template
5387 declaration. In the function case, 1 indicates a declaration, 2
5388 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5389 emitted for extraneous default arguments.
5390
5391 Returns TRUE if there were no errors found, FALSE otherwise. */
5392
5393 bool
5394 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5395 bool is_partial, int is_friend_decl)
5396 {
5397 const char *msg;
5398 int last_level_to_check;
5399 tree parm_level;
5400 bool no_errors = true;
5401
5402 /* [temp.param]
5403
5404 A default template-argument shall not be specified in a
5405 function template declaration or a function template definition, nor
5406 in the template-parameter-list of the definition of a member of a
5407 class template. */
5408
5409 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5410 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5411 /* You can't have a function template declaration in a local
5412 scope, nor you can you define a member of a class template in a
5413 local scope. */
5414 return true;
5415
5416 if ((TREE_CODE (decl) == TYPE_DECL
5417 && TREE_TYPE (decl)
5418 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5419 || (TREE_CODE (decl) == FUNCTION_DECL
5420 && LAMBDA_FUNCTION_P (decl)))
5421 /* A lambda doesn't have an explicit declaration; don't complain
5422 about the parms of the enclosing class. */
5423 return true;
5424
5425 if (current_class_type
5426 && !TYPE_BEING_DEFINED (current_class_type)
5427 && DECL_LANG_SPECIFIC (decl)
5428 && DECL_DECLARES_FUNCTION_P (decl)
5429 /* If this is either a friend defined in the scope of the class
5430 or a member function. */
5431 && (DECL_FUNCTION_MEMBER_P (decl)
5432 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5433 : DECL_FRIEND_CONTEXT (decl)
5434 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5435 : false)
5436 /* And, if it was a member function, it really was defined in
5437 the scope of the class. */
5438 && (!DECL_FUNCTION_MEMBER_P (decl)
5439 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5440 /* We already checked these parameters when the template was
5441 declared, so there's no need to do it again now. This function
5442 was defined in class scope, but we're processing its body now
5443 that the class is complete. */
5444 return true;
5445
5446 /* Core issue 226 (C++0x only): the following only applies to class
5447 templates. */
5448 if (is_primary
5449 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5450 {
5451 /* [temp.param]
5452
5453 If a template-parameter has a default template-argument, all
5454 subsequent template-parameters shall have a default
5455 template-argument supplied. */
5456 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5457 {
5458 tree inner_parms = TREE_VALUE (parm_level);
5459 int ntparms = TREE_VEC_LENGTH (inner_parms);
5460 int seen_def_arg_p = 0;
5461 int i;
5462
5463 for (i = 0; i < ntparms; ++i)
5464 {
5465 tree parm = TREE_VEC_ELT (inner_parms, i);
5466
5467 if (parm == error_mark_node)
5468 continue;
5469
5470 if (TREE_PURPOSE (parm))
5471 seen_def_arg_p = 1;
5472 else if (seen_def_arg_p
5473 && !template_parameter_pack_p (TREE_VALUE (parm)))
5474 {
5475 error ("no default argument for %qD", TREE_VALUE (parm));
5476 /* For better subsequent error-recovery, we indicate that
5477 there should have been a default argument. */
5478 TREE_PURPOSE (parm) = error_mark_node;
5479 no_errors = false;
5480 }
5481 else if (!is_partial
5482 && !is_friend_decl
5483 /* Don't complain about an enclosing partial
5484 specialization. */
5485 && parm_level == parms
5486 && TREE_CODE (decl) == TYPE_DECL
5487 && i < ntparms - 1
5488 && template_parameter_pack_p (TREE_VALUE (parm))
5489 /* A fixed parameter pack will be partially
5490 instantiated into a fixed length list. */
5491 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5492 {
5493 /* A primary class template can only have one
5494 parameter pack, at the end of the template
5495 parameter list. */
5496
5497 error ("parameter pack %q+D must be at the end of the"
5498 " template parameter list", TREE_VALUE (parm));
5499
5500 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5501 = error_mark_node;
5502 no_errors = false;
5503 }
5504 }
5505 }
5506 }
5507
5508 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5509 || is_partial
5510 || !is_primary
5511 || is_friend_decl)
5512 /* For an ordinary class template, default template arguments are
5513 allowed at the innermost level, e.g.:
5514 template <class T = int>
5515 struct S {};
5516 but, in a partial specialization, they're not allowed even
5517 there, as we have in [temp.class.spec]:
5518
5519 The template parameter list of a specialization shall not
5520 contain default template argument values.
5521
5522 So, for a partial specialization, or for a function template
5523 (in C++98/C++03), we look at all of them. */
5524 ;
5525 else
5526 /* But, for a primary class template that is not a partial
5527 specialization we look at all template parameters except the
5528 innermost ones. */
5529 parms = TREE_CHAIN (parms);
5530
5531 /* Figure out what error message to issue. */
5532 if (is_friend_decl == 2)
5533 msg = G_("default template arguments may not be used in function template "
5534 "friend re-declaration");
5535 else if (is_friend_decl)
5536 msg = G_("default template arguments may not be used in template "
5537 "friend declarations");
5538 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5539 msg = G_("default template arguments may not be used in function templates "
5540 "without %<-std=c++11%> or %<-std=gnu++11%>");
5541 else if (is_partial)
5542 msg = G_("default template arguments may not be used in "
5543 "partial specializations");
5544 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5545 msg = G_("default argument for template parameter for class enclosing %qD");
5546 else
5547 /* Per [temp.param]/9, "A default template-argument shall not be
5548 specified in the template-parameter-lists of the definition of
5549 a member of a class template that appears outside of the member's
5550 class.", thus if we aren't handling a member of a class template
5551 there is no need to examine the parameters. */
5552 return true;
5553
5554 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5555 /* If we're inside a class definition, there's no need to
5556 examine the parameters to the class itself. On the one
5557 hand, they will be checked when the class is defined, and,
5558 on the other, default arguments are valid in things like:
5559 template <class T = double>
5560 struct S { template <class U> void f(U); };
5561 Here the default argument for `S' has no bearing on the
5562 declaration of `f'. */
5563 last_level_to_check = template_class_depth (current_class_type) + 1;
5564 else
5565 /* Check everything. */
5566 last_level_to_check = 0;
5567
5568 for (parm_level = parms;
5569 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5570 parm_level = TREE_CHAIN (parm_level))
5571 {
5572 tree inner_parms = TREE_VALUE (parm_level);
5573 int i;
5574 int ntparms;
5575
5576 ntparms = TREE_VEC_LENGTH (inner_parms);
5577 for (i = 0; i < ntparms; ++i)
5578 {
5579 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5580 continue;
5581
5582 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5583 {
5584 if (msg)
5585 {
5586 no_errors = false;
5587 if (is_friend_decl == 2)
5588 return no_errors;
5589
5590 error (msg, decl);
5591 msg = 0;
5592 }
5593
5594 /* Clear out the default argument so that we are not
5595 confused later. */
5596 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5597 }
5598 }
5599
5600 /* At this point, if we're still interested in issuing messages,
5601 they must apply to classes surrounding the object declared. */
5602 if (msg)
5603 msg = G_("default argument for template parameter for class "
5604 "enclosing %qD");
5605 }
5606
5607 return no_errors;
5608 }
5609
5610 /* Worker for push_template_decl_real, called via
5611 for_each_template_parm. DATA is really an int, indicating the
5612 level of the parameters we are interested in. If T is a template
5613 parameter of that level, return nonzero. */
5614
5615 static int
5616 template_parm_this_level_p (tree t, void* data)
5617 {
5618 int this_level = *(int *)data;
5619 int level;
5620
5621 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5622 level = TEMPLATE_PARM_LEVEL (t);
5623 else
5624 level = TEMPLATE_TYPE_LEVEL (t);
5625 return level == this_level;
5626 }
5627
5628 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5629 DATA is really an int, indicating the innermost outer level of parameters.
5630 If T is a template parameter of that level or further out, return
5631 nonzero. */
5632
5633 static int
5634 template_parm_outer_level (tree t, void *data)
5635 {
5636 int this_level = *(int *)data;
5637 int level;
5638
5639 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5640 level = TEMPLATE_PARM_LEVEL (t);
5641 else
5642 level = TEMPLATE_TYPE_LEVEL (t);
5643 return level <= this_level;
5644 }
5645
5646 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5647 parameters given by current_template_args, or reuses a
5648 previously existing one, if appropriate. Returns the DECL, or an
5649 equivalent one, if it is replaced via a call to duplicate_decls.
5650
5651 If IS_FRIEND is true, DECL is a friend declaration. */
5652
5653 tree
5654 push_template_decl_real (tree decl, bool is_friend)
5655 {
5656 tree tmpl;
5657 tree args;
5658 tree info;
5659 tree ctx;
5660 bool is_primary;
5661 bool is_partial;
5662 int new_template_p = 0;
5663 /* True if the template is a member template, in the sense of
5664 [temp.mem]. */
5665 bool member_template_p = false;
5666
5667 if (decl == error_mark_node || !current_template_parms)
5668 return error_mark_node;
5669
5670 /* See if this is a partial specialization. */
5671 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5672 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5673 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5674 || (VAR_P (decl)
5675 && DECL_LANG_SPECIFIC (decl)
5676 && DECL_TEMPLATE_SPECIALIZATION (decl)
5677 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5678
5679 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5680 is_friend = true;
5681
5682 if (is_friend)
5683 /* For a friend, we want the context of the friend, not
5684 the type of which it is a friend. */
5685 ctx = CP_DECL_CONTEXT (decl);
5686 else if (CP_DECL_CONTEXT (decl)
5687 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5688 /* In the case of a virtual function, we want the class in which
5689 it is defined. */
5690 ctx = CP_DECL_CONTEXT (decl);
5691 else
5692 /* Otherwise, if we're currently defining some class, the DECL
5693 is assumed to be a member of the class. */
5694 ctx = current_scope ();
5695
5696 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5697 ctx = NULL_TREE;
5698
5699 if (!DECL_CONTEXT (decl))
5700 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5701
5702 /* See if this is a primary template. */
5703 if (is_friend && ctx
5704 && uses_template_parms_level (ctx, processing_template_decl))
5705 /* A friend template that specifies a class context, i.e.
5706 template <typename T> friend void A<T>::f();
5707 is not primary. */
5708 is_primary = false;
5709 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5710 is_primary = false;
5711 else
5712 is_primary = template_parm_scope_p ();
5713
5714 if (is_primary)
5715 {
5716 warning (OPT_Wtemplates, "template %qD declared", decl);
5717
5718 if (DECL_CLASS_SCOPE_P (decl))
5719 member_template_p = true;
5720 if (TREE_CODE (decl) == TYPE_DECL
5721 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5722 {
5723 error ("template class without a name");
5724 return error_mark_node;
5725 }
5726 else if (TREE_CODE (decl) == FUNCTION_DECL)
5727 {
5728 if (member_template_p)
5729 {
5730 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5731 error ("member template %qD may not have virt-specifiers", decl);
5732 }
5733 if (DECL_DESTRUCTOR_P (decl))
5734 {
5735 /* [temp.mem]
5736
5737 A destructor shall not be a member template. */
5738 error_at (DECL_SOURCE_LOCATION (decl),
5739 "destructor %qD declared as member template", decl);
5740 return error_mark_node;
5741 }
5742 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5743 && (!prototype_p (TREE_TYPE (decl))
5744 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5745 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5746 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5747 == void_list_node)))
5748 {
5749 /* [basic.stc.dynamic.allocation]
5750
5751 An allocation function can be a function
5752 template. ... Template allocation functions shall
5753 have two or more parameters. */
5754 error ("invalid template declaration of %qD", decl);
5755 return error_mark_node;
5756 }
5757 }
5758 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5759 && CLASS_TYPE_P (TREE_TYPE (decl)))
5760 {
5761 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5762 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5763 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5764 {
5765 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5766 if (TREE_CODE (t) == TYPE_DECL)
5767 t = TREE_TYPE (t);
5768 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5769 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5770 }
5771 }
5772 else if (TREE_CODE (decl) == TYPE_DECL
5773 && TYPE_DECL_ALIAS_P (decl))
5774 /* alias-declaration */
5775 gcc_assert (!DECL_ARTIFICIAL (decl));
5776 else if (VAR_P (decl))
5777 /* C++14 variable template. */;
5778 else if (TREE_CODE (decl) == CONCEPT_DECL)
5779 /* C++20 concept definitions. */;
5780 else
5781 {
5782 error ("template declaration of %q#D", decl);
5783 return error_mark_node;
5784 }
5785 }
5786
5787 /* Check to see that the rules regarding the use of default
5788 arguments are not being violated. We check args for a friend
5789 functions when we know whether it's a definition, introducing
5790 declaration or re-declaration. */
5791 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5792 check_default_tmpl_args (decl, current_template_parms,
5793 is_primary, is_partial, is_friend);
5794
5795 /* Ensure that there are no parameter packs in the type of this
5796 declaration that have not been expanded. */
5797 if (TREE_CODE (decl) == FUNCTION_DECL)
5798 {
5799 /* Check each of the arguments individually to see if there are
5800 any bare parameter packs. */
5801 tree type = TREE_TYPE (decl);
5802 tree arg = DECL_ARGUMENTS (decl);
5803 tree argtype = TYPE_ARG_TYPES (type);
5804
5805 while (arg && argtype)
5806 {
5807 if (!DECL_PACK_P (arg)
5808 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5809 {
5810 /* This is a PARM_DECL that contains unexpanded parameter
5811 packs. We have already complained about this in the
5812 check_for_bare_parameter_packs call, so just replace
5813 these types with ERROR_MARK_NODE. */
5814 TREE_TYPE (arg) = error_mark_node;
5815 TREE_VALUE (argtype) = error_mark_node;
5816 }
5817
5818 arg = DECL_CHAIN (arg);
5819 argtype = TREE_CHAIN (argtype);
5820 }
5821
5822 /* Check for bare parameter packs in the return type and the
5823 exception specifiers. */
5824 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5825 /* Errors were already issued, set return type to int
5826 as the frontend doesn't expect error_mark_node as
5827 the return type. */
5828 TREE_TYPE (type) = integer_type_node;
5829 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5830 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5831 }
5832 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5833 ? DECL_ORIGINAL_TYPE (decl)
5834 : TREE_TYPE (decl)))
5835 {
5836 TREE_TYPE (decl) = error_mark_node;
5837 return error_mark_node;
5838 }
5839
5840 if (is_partial)
5841 return process_partial_specialization (decl);
5842
5843 args = current_template_args ();
5844
5845 if (!ctx
5846 || TREE_CODE (ctx) == FUNCTION_DECL
5847 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5848 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5849 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5850 {
5851 if (DECL_LANG_SPECIFIC (decl)
5852 && DECL_TEMPLATE_INFO (decl)
5853 && DECL_TI_TEMPLATE (decl))
5854 tmpl = DECL_TI_TEMPLATE (decl);
5855 /* If DECL is a TYPE_DECL for a class-template, then there won't
5856 be DECL_LANG_SPECIFIC. The information equivalent to
5857 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5858 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5859 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5860 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5861 {
5862 /* Since a template declaration already existed for this
5863 class-type, we must be redeclaring it here. Make sure
5864 that the redeclaration is valid. */
5865 redeclare_class_template (TREE_TYPE (decl),
5866 current_template_parms,
5867 current_template_constraints ());
5868 /* We don't need to create a new TEMPLATE_DECL; just use the
5869 one we already had. */
5870 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5871 }
5872 else
5873 {
5874 tmpl = build_template_decl (decl, current_template_parms,
5875 member_template_p);
5876 new_template_p = 1;
5877
5878 if (DECL_LANG_SPECIFIC (decl)
5879 && DECL_TEMPLATE_SPECIALIZATION (decl))
5880 {
5881 /* A specialization of a member template of a template
5882 class. */
5883 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5884 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5885 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5886 }
5887 }
5888 }
5889 else
5890 {
5891 tree a, t, current, parms;
5892 int i;
5893 tree tinfo = get_template_info (decl);
5894
5895 if (!tinfo)
5896 {
5897 error ("template definition of non-template %q#D", decl);
5898 return error_mark_node;
5899 }
5900
5901 tmpl = TI_TEMPLATE (tinfo);
5902
5903 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5904 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5905 && DECL_TEMPLATE_SPECIALIZATION (decl)
5906 && DECL_MEMBER_TEMPLATE_P (tmpl))
5907 {
5908 tree new_tmpl;
5909
5910 /* The declaration is a specialization of a member
5911 template, declared outside the class. Therefore, the
5912 innermost template arguments will be NULL, so we
5913 replace them with the arguments determined by the
5914 earlier call to check_explicit_specialization. */
5915 args = DECL_TI_ARGS (decl);
5916
5917 new_tmpl
5918 = build_template_decl (decl, current_template_parms,
5919 member_template_p);
5920 DECL_TI_TEMPLATE (decl) = new_tmpl;
5921 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5922 DECL_TEMPLATE_INFO (new_tmpl)
5923 = build_template_info (tmpl, args);
5924
5925 register_specialization (new_tmpl,
5926 most_general_template (tmpl),
5927 args,
5928 is_friend, 0);
5929 return decl;
5930 }
5931
5932 /* Make sure the template headers we got make sense. */
5933
5934 parms = DECL_TEMPLATE_PARMS (tmpl);
5935 i = TMPL_PARMS_DEPTH (parms);
5936 if (TMPL_ARGS_DEPTH (args) != i)
5937 {
5938 error ("expected %d levels of template parms for %q#D, got %d",
5939 i, decl, TMPL_ARGS_DEPTH (args));
5940 DECL_INTERFACE_KNOWN (decl) = 1;
5941 return error_mark_node;
5942 }
5943 else
5944 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5945 {
5946 a = TMPL_ARGS_LEVEL (args, i);
5947 t = INNERMOST_TEMPLATE_PARMS (parms);
5948
5949 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5950 {
5951 if (current == decl)
5952 error ("got %d template parameters for %q#D",
5953 TREE_VEC_LENGTH (a), decl);
5954 else
5955 error ("got %d template parameters for %q#T",
5956 TREE_VEC_LENGTH (a), current);
5957 error (" but %d required", TREE_VEC_LENGTH (t));
5958 /* Avoid crash in import_export_decl. */
5959 DECL_INTERFACE_KNOWN (decl) = 1;
5960 return error_mark_node;
5961 }
5962
5963 if (current == decl)
5964 current = ctx;
5965 else if (current == NULL_TREE)
5966 /* Can happen in erroneous input. */
5967 break;
5968 else
5969 current = get_containing_scope (current);
5970 }
5971
5972 /* Check that the parms are used in the appropriate qualifying scopes
5973 in the declarator. */
5974 if (!comp_template_args
5975 (TI_ARGS (tinfo),
5976 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5977 {
5978 error ("template arguments to %qD do not match original "
5979 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5980 if (!uses_template_parms (TI_ARGS (tinfo)))
5981 inform (input_location, "use %<template<>%> for"
5982 " an explicit specialization");
5983 /* Avoid crash in import_export_decl. */
5984 DECL_INTERFACE_KNOWN (decl) = 1;
5985 return error_mark_node;
5986 }
5987 }
5988
5989 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
5990
5991
5992 if (new_template_p)
5993 {
5994 /* Push template declarations for global functions and types.
5995 Note that we do not try to push a global template friend
5996 declared in a template class; such a thing may well depend on
5997 the template parameters of the class and we'll push it when
5998 instantiating the befriending class. */
5999 if (!ctx
6000 && !(is_friend && template_class_depth (current_class_type) > 0))
6001 {
6002 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6003 if (tmpl == error_mark_node)
6004 return error_mark_node;
6005
6006 /* Hide template friend classes that haven't been declared yet. */
6007 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6008 {
6009 DECL_ANTICIPATED (tmpl) = 1;
6010 DECL_FRIEND_P (tmpl) = 1;
6011 }
6012 }
6013 }
6014 else
6015 /* The type may have been completed, or (erroneously) changed. */
6016 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6017
6018 if (is_primary)
6019 {
6020 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6021
6022 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6023
6024 /* Give template template parms a DECL_CONTEXT of the template
6025 for which they are a parameter. */
6026 parms = INNERMOST_TEMPLATE_PARMS (parms);
6027 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6028 {
6029 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6030 if (TREE_CODE (parm) == TEMPLATE_DECL)
6031 DECL_CONTEXT (parm) = tmpl;
6032 }
6033
6034 if (TREE_CODE (decl) == TYPE_DECL
6035 && TYPE_DECL_ALIAS_P (decl))
6036 {
6037 if (tree constr
6038 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6039 {
6040 /* ??? Why don't we do this here for all templates? */
6041 constr = build_constraints (constr, NULL_TREE);
6042 set_constraints (decl, constr);
6043 }
6044 if (complex_alias_template_p (tmpl))
6045 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6046 }
6047 }
6048
6049 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6050 back to its most general template. If TMPL is a specialization,
6051 ARGS may only have the innermost set of arguments. Add the missing
6052 argument levels if necessary. */
6053 if (DECL_TEMPLATE_INFO (tmpl))
6054 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6055
6056 info = build_template_info (tmpl, args);
6057
6058 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6059 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6060 else
6061 {
6062 if (is_primary)
6063 retrofit_lang_decl (decl);
6064 if (DECL_LANG_SPECIFIC (decl))
6065 DECL_TEMPLATE_INFO (decl) = info;
6066 }
6067
6068 if (flag_implicit_templates
6069 && !is_friend
6070 && TREE_PUBLIC (decl)
6071 && VAR_OR_FUNCTION_DECL_P (decl))
6072 /* Set DECL_COMDAT on template instantiations; if we force
6073 them to be emitted by explicit instantiation,
6074 mark_needed will tell cgraph to do the right thing. */
6075 DECL_COMDAT (decl) = true;
6076
6077 return DECL_TEMPLATE_RESULT (tmpl);
6078 }
6079
6080 tree
6081 push_template_decl (tree decl)
6082 {
6083 return push_template_decl_real (decl, false);
6084 }
6085
6086 /* FN is an inheriting constructor that inherits from the constructor
6087 template INHERITED; turn FN into a constructor template with a matching
6088 template header. */
6089
6090 tree
6091 add_inherited_template_parms (tree fn, tree inherited)
6092 {
6093 tree inner_parms
6094 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6095 inner_parms = copy_node (inner_parms);
6096 tree parms
6097 = tree_cons (size_int (processing_template_decl + 1),
6098 inner_parms, current_template_parms);
6099 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6100 tree args = template_parms_to_args (parms);
6101 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6102 DECL_ARTIFICIAL (tmpl) = true;
6103 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6104 return tmpl;
6105 }
6106
6107 /* Called when a class template TYPE is redeclared with the indicated
6108 template PARMS, e.g.:
6109
6110 template <class T> struct S;
6111 template <class T> struct S {}; */
6112
6113 bool
6114 redeclare_class_template (tree type, tree parms, tree cons)
6115 {
6116 tree tmpl;
6117 tree tmpl_parms;
6118 int i;
6119
6120 if (!TYPE_TEMPLATE_INFO (type))
6121 {
6122 error ("%qT is not a template type", type);
6123 return false;
6124 }
6125
6126 tmpl = TYPE_TI_TEMPLATE (type);
6127 if (!PRIMARY_TEMPLATE_P (tmpl))
6128 /* The type is nested in some template class. Nothing to worry
6129 about here; there are no new template parameters for the nested
6130 type. */
6131 return true;
6132
6133 if (!parms)
6134 {
6135 error ("template specifiers not specified in declaration of %qD",
6136 tmpl);
6137 return false;
6138 }
6139
6140 parms = INNERMOST_TEMPLATE_PARMS (parms);
6141 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6142
6143 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6144 {
6145 error_n (input_location, TREE_VEC_LENGTH (parms),
6146 "redeclared with %d template parameter",
6147 "redeclared with %d template parameters",
6148 TREE_VEC_LENGTH (parms));
6149 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6150 "previous declaration %qD used %d template parameter",
6151 "previous declaration %qD used %d template parameters",
6152 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6153 return false;
6154 }
6155
6156 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6157 {
6158 tree tmpl_parm;
6159 tree parm;
6160 tree tmpl_default;
6161 tree parm_default;
6162
6163 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6164 || TREE_VEC_ELT (parms, i) == error_mark_node)
6165 continue;
6166
6167 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6168 if (error_operand_p (tmpl_parm))
6169 return false;
6170
6171 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6172 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6173 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6174
6175 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6176 TEMPLATE_DECL. */
6177 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6178 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6179 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6180 || (TREE_CODE (tmpl_parm) != PARM_DECL
6181 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6182 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6183 || (TREE_CODE (tmpl_parm) == PARM_DECL
6184 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6185 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6186 {
6187 auto_diagnostic_group d;
6188 error ("template parameter %q+#D", tmpl_parm);
6189 inform (input_location, "redeclared here as %q#D", parm);
6190 return false;
6191 }
6192
6193 /* The parameters can be declared to introduce different
6194 constraints. */
6195 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6196 tree p2 = TREE_VEC_ELT (parms, i);
6197 if (!template_parameter_constraints_equivalent_p (p1, p2))
6198 {
6199 auto_diagnostic_group d;
6200 error ("declaration of template parameter %q+#D with different "
6201 "constraints", parm);
6202 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6203 "original declaration appeared here");
6204 return false;
6205 }
6206
6207 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6208 {
6209 /* We have in [temp.param]:
6210
6211 A template-parameter may not be given default arguments
6212 by two different declarations in the same scope. */
6213 auto_diagnostic_group d;
6214 error_at (input_location, "redefinition of default argument for %q#D", parm);
6215 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6216 "original definition appeared here");
6217 return false;
6218 }
6219
6220 if (parm_default != NULL_TREE)
6221 /* Update the previous template parameters (which are the ones
6222 that will really count) with the new default value. */
6223 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6224 else if (tmpl_default != NULL_TREE)
6225 /* Update the new parameters, too; they'll be used as the
6226 parameters for any members. */
6227 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6228
6229 /* Give each template template parm in this redeclaration a
6230 DECL_CONTEXT of the template for which they are a parameter. */
6231 if (TREE_CODE (parm) == TEMPLATE_DECL)
6232 {
6233 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6234 DECL_CONTEXT (parm) = tmpl;
6235 }
6236
6237 if (TREE_CODE (parm) == TYPE_DECL)
6238 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6239 }
6240
6241 tree ci = get_constraints (tmpl);
6242 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6243 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6244
6245 /* Two classes with different constraints declare different entities. */
6246 if (!cp_tree_equal (req1, req2))
6247 {
6248 auto_diagnostic_group d;
6249 error_at (input_location, "redeclaration %q#D with different "
6250 "constraints", tmpl);
6251 inform (DECL_SOURCE_LOCATION (tmpl),
6252 "original declaration appeared here");
6253 return false;
6254 }
6255
6256 return true;
6257 }
6258
6259 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6260 to be used when the caller has already checked
6261 (processing_template_decl
6262 && !instantiation_dependent_expression_p (expr)
6263 && potential_constant_expression (expr))
6264 and cleared processing_template_decl. */
6265
6266 tree
6267 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6268 {
6269 return tsubst_copy_and_build (expr,
6270 /*args=*/NULL_TREE,
6271 complain,
6272 /*in_decl=*/NULL_TREE,
6273 /*function_p=*/false,
6274 /*integral_constant_expression_p=*/true);
6275 }
6276
6277 /* Simplify EXPR if it is a non-dependent expression. Returns the
6278 (possibly simplified) expression. */
6279
6280 tree
6281 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6282 {
6283 if (expr == NULL_TREE)
6284 return NULL_TREE;
6285
6286 /* If we're in a template, but EXPR isn't value dependent, simplify
6287 it. We're supposed to treat:
6288
6289 template <typename T> void f(T[1 + 1]);
6290 template <typename T> void f(T[2]);
6291
6292 as two declarations of the same function, for example. */
6293 if (processing_template_decl
6294 && is_nondependent_constant_expression (expr))
6295 {
6296 processing_template_decl_sentinel s;
6297 expr = instantiate_non_dependent_expr_internal (expr, complain);
6298 }
6299 return expr;
6300 }
6301
6302 tree
6303 instantiate_non_dependent_expr (tree expr)
6304 {
6305 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6306 }
6307
6308 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6309 an uninstantiated expression. */
6310
6311 tree
6312 instantiate_non_dependent_or_null (tree expr)
6313 {
6314 if (expr == NULL_TREE)
6315 return NULL_TREE;
6316 if (processing_template_decl)
6317 {
6318 if (!is_nondependent_constant_expression (expr))
6319 expr = NULL_TREE;
6320 else
6321 {
6322 processing_template_decl_sentinel s;
6323 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6324 }
6325 }
6326 return expr;
6327 }
6328
6329 /* True iff T is a specialization of a variable template. */
6330
6331 bool
6332 variable_template_specialization_p (tree t)
6333 {
6334 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6335 return false;
6336 tree tmpl = DECL_TI_TEMPLATE (t);
6337 return variable_template_p (tmpl);
6338 }
6339
6340 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6341 template declaration, or a TYPE_DECL for an alias declaration. */
6342
6343 bool
6344 alias_type_or_template_p (tree t)
6345 {
6346 if (t == NULL_TREE)
6347 return false;
6348 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6349 || (TYPE_P (t)
6350 && TYPE_NAME (t)
6351 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6352 || DECL_ALIAS_TEMPLATE_P (t));
6353 }
6354
6355 /* If T is a specialization of an alias template, return it; otherwise return
6356 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6357
6358 tree
6359 alias_template_specialization_p (const_tree t,
6360 bool transparent_typedefs)
6361 {
6362 if (!TYPE_P (t))
6363 return NULL_TREE;
6364
6365 /* It's an alias template specialization if it's an alias and its
6366 TYPE_NAME is a specialization of a primary template. */
6367 if (typedef_variant_p (t))
6368 {
6369 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6370 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6371 return CONST_CAST_TREE (t);
6372 if (transparent_typedefs)
6373 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6374 (TYPE_NAME (t)),
6375 transparent_typedefs);
6376 }
6377
6378 return NULL_TREE;
6379 }
6380
6381 /* An alias template is complex from a SFINAE perspective if a template-id
6382 using that alias can be ill-formed when the expansion is not, as with
6383 the void_t template. We determine this by checking whether the
6384 expansion for the alias template uses all its template parameters. */
6385
6386 struct uses_all_template_parms_data
6387 {
6388 int level;
6389 bool *seen;
6390 };
6391
6392 static int
6393 uses_all_template_parms_r (tree t, void *data_)
6394 {
6395 struct uses_all_template_parms_data &data
6396 = *(struct uses_all_template_parms_data*)data_;
6397 tree idx = get_template_parm_index (t);
6398
6399 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6400 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6401 return 0;
6402 }
6403
6404 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6405
6406 static int
6407 complex_pack_expansion_r (tree t, void *data_)
6408 {
6409 /* An alias template with a pack expansion that expands a pack from the
6410 enclosing class needs to be considered complex, to avoid confusion with
6411 the same pack being used as an argument to the alias's own template
6412 parameter (91966). */
6413 if (!PACK_EXPANSION_P (t))
6414 return 0;
6415 struct uses_all_template_parms_data &data
6416 = *(struct uses_all_template_parms_data*)data_;
6417 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6418 pack = TREE_CHAIN (pack))
6419 {
6420 tree parm_pack = TREE_VALUE (pack);
6421 if (!TEMPLATE_PARM_P (parm_pack))
6422 continue;
6423 int idx, level;
6424 template_parm_level_and_index (parm_pack, &level, &idx);
6425 if (level < data.level)
6426 return 1;
6427 }
6428 return 0;
6429 }
6430
6431 static bool
6432 complex_alias_template_p (const_tree tmpl)
6433 {
6434 /* A renaming alias isn't complex. */
6435 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6436 return false;
6437
6438 /* Any other constrained alias is complex. */
6439 if (get_constraints (tmpl))
6440 return true;
6441
6442 struct uses_all_template_parms_data data;
6443 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6444 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6445 data.level = TMPL_PARMS_DEPTH (parms);
6446 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6447 data.seen = XALLOCAVEC (bool, len);
6448 for (int i = 0; i < len; ++i)
6449 data.seen[i] = false;
6450
6451 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6452 NULL, true, complex_pack_expansion_r))
6453 return true;
6454 for (int i = 0; i < len; ++i)
6455 if (!data.seen[i])
6456 return true;
6457 return false;
6458 }
6459
6460 /* If T is a specialization of a complex alias template with dependent
6461 template-arguments, return it; otherwise return NULL_TREE. If T is a
6462 typedef to such a specialization, return the specialization. */
6463
6464 tree
6465 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6466 {
6467 if (!TYPE_P (t) || !typedef_variant_p (t))
6468 return NULL_TREE;
6469
6470 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6471 if (tinfo
6472 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6473 && (any_dependent_template_arguments_p
6474 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6475 return CONST_CAST_TREE (t);
6476
6477 if (transparent_typedefs)
6478 {
6479 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6480 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6481 }
6482
6483 return NULL_TREE;
6484 }
6485
6486 /* Return the number of innermost template parameters in TMPL. */
6487
6488 static int
6489 num_innermost_template_parms (const_tree tmpl)
6490 {
6491 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6492 return TREE_VEC_LENGTH (parms);
6493 }
6494
6495 /* Return either TMPL or another template that it is equivalent to under DR
6496 1286: An alias that just changes the name of a template is equivalent to
6497 the other template. */
6498
6499 static tree
6500 get_underlying_template (tree tmpl)
6501 {
6502 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6503 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6504 {
6505 /* Determine if the alias is equivalent to an underlying template. */
6506 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6507 /* The underlying type may have been ill-formed. Don't proceed. */
6508 if (!orig_type)
6509 break;
6510 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6511 if (!tinfo)
6512 break;
6513
6514 tree underlying = TI_TEMPLATE (tinfo);
6515 if (!PRIMARY_TEMPLATE_P (underlying)
6516 || (num_innermost_template_parms (tmpl)
6517 != num_innermost_template_parms (underlying)))
6518 break;
6519
6520 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6521 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6522 break;
6523
6524 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6525 it's appropriate to treat a less-constrained alias as equivalent. */
6526 if (!at_least_as_constrained (underlying, tmpl))
6527 break;
6528
6529 /* Alias is equivalent. Strip it and repeat. */
6530 tmpl = underlying;
6531 }
6532
6533 return tmpl;
6534 }
6535
6536 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6537 must be a reference-to-function or a pointer-to-function type, as specified
6538 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6539 and check that the resulting function has external linkage. */
6540
6541 static tree
6542 convert_nontype_argument_function (tree type, tree expr,
6543 tsubst_flags_t complain)
6544 {
6545 tree fns = expr;
6546 tree fn, fn_no_ptr;
6547 linkage_kind linkage;
6548
6549 fn = instantiate_type (type, fns, tf_none);
6550 if (fn == error_mark_node)
6551 return error_mark_node;
6552
6553 if (value_dependent_expression_p (fn))
6554 goto accept;
6555
6556 fn_no_ptr = strip_fnptr_conv (fn);
6557 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6558 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6559 if (BASELINK_P (fn_no_ptr))
6560 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6561
6562 /* [temp.arg.nontype]/1
6563
6564 A template-argument for a non-type, non-template template-parameter
6565 shall be one of:
6566 [...]
6567 -- the address of an object or function with external [C++11: or
6568 internal] linkage. */
6569
6570 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6571 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6572 {
6573 if (complain & tf_error)
6574 {
6575 location_t loc = cp_expr_loc_or_input_loc (expr);
6576 error_at (loc, "%qE is not a valid template argument for type %qT",
6577 expr, type);
6578 if (TYPE_PTR_P (type))
6579 inform (loc, "it must be the address of a function "
6580 "with external linkage");
6581 else
6582 inform (loc, "it must be the name of a function with "
6583 "external linkage");
6584 }
6585 return NULL_TREE;
6586 }
6587
6588 linkage = decl_linkage (fn_no_ptr);
6589 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6590 {
6591 if (complain & tf_error)
6592 {
6593 location_t loc = cp_expr_loc_or_input_loc (expr);
6594 if (cxx_dialect >= cxx11)
6595 error_at (loc, "%qE is not a valid template argument for type "
6596 "%qT because %qD has no linkage",
6597 expr, type, fn_no_ptr);
6598 else
6599 error_at (loc, "%qE is not a valid template argument for type "
6600 "%qT because %qD does not have external linkage",
6601 expr, type, fn_no_ptr);
6602 }
6603 return NULL_TREE;
6604 }
6605
6606 accept:
6607 if (TYPE_REF_P (type))
6608 {
6609 if (REFERENCE_REF_P (fn))
6610 fn = TREE_OPERAND (fn, 0);
6611 else
6612 fn = build_address (fn);
6613 }
6614 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6615 fn = build_nop (type, fn);
6616
6617 return fn;
6618 }
6619
6620 /* Subroutine of convert_nontype_argument.
6621 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6622 Emit an error otherwise. */
6623
6624 static bool
6625 check_valid_ptrmem_cst_expr (tree type, tree expr,
6626 tsubst_flags_t complain)
6627 {
6628 tree orig_expr = expr;
6629 STRIP_NOPS (expr);
6630 if (null_ptr_cst_p (expr))
6631 return true;
6632 if (TREE_CODE (expr) == PTRMEM_CST
6633 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6634 PTRMEM_CST_CLASS (expr)))
6635 return true;
6636 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6637 return true;
6638 if (processing_template_decl
6639 && TREE_CODE (expr) == ADDR_EXPR
6640 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6641 return true;
6642 if (complain & tf_error)
6643 {
6644 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6645 error_at (loc, "%qE is not a valid template argument for type %qT",
6646 orig_expr, type);
6647 if (TREE_CODE (expr) != PTRMEM_CST)
6648 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6649 else
6650 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6651 }
6652 return false;
6653 }
6654
6655 /* Returns TRUE iff the address of OP is value-dependent.
6656
6657 14.6.2.4 [temp.dep.temp]:
6658 A non-integral non-type template-argument is dependent if its type is
6659 dependent or it has either of the following forms
6660 qualified-id
6661 & qualified-id
6662 and contains a nested-name-specifier which specifies a class-name that
6663 names a dependent type.
6664
6665 We generalize this to just say that the address of a member of a
6666 dependent class is value-dependent; the above doesn't cover the
6667 address of a static data member named with an unqualified-id. */
6668
6669 static bool
6670 has_value_dependent_address (tree op)
6671 {
6672 STRIP_ANY_LOCATION_WRAPPER (op);
6673
6674 /* We could use get_inner_reference here, but there's no need;
6675 this is only relevant for template non-type arguments, which
6676 can only be expressed as &id-expression. */
6677 if (DECL_P (op))
6678 {
6679 tree ctx = CP_DECL_CONTEXT (op);
6680 if (TYPE_P (ctx) && dependent_type_p (ctx))
6681 return true;
6682 }
6683
6684 return false;
6685 }
6686
6687 /* The next set of functions are used for providing helpful explanatory
6688 diagnostics for failed overload resolution. Their messages should be
6689 indented by two spaces for consistency with the messages in
6690 call.c */
6691
6692 static int
6693 unify_success (bool /*explain_p*/)
6694 {
6695 return 0;
6696 }
6697
6698 /* Other failure functions should call this one, to provide a single function
6699 for setting a breakpoint on. */
6700
6701 static int
6702 unify_invalid (bool /*explain_p*/)
6703 {
6704 return 1;
6705 }
6706
6707 static int
6708 unify_parameter_deduction_failure (bool explain_p, tree parm)
6709 {
6710 if (explain_p)
6711 inform (input_location,
6712 " couldn%'t deduce template parameter %qD", parm);
6713 return unify_invalid (explain_p);
6714 }
6715
6716 static int
6717 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6718 {
6719 if (explain_p)
6720 inform (input_location,
6721 " types %qT and %qT have incompatible cv-qualifiers",
6722 parm, arg);
6723 return unify_invalid (explain_p);
6724 }
6725
6726 static int
6727 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6728 {
6729 if (explain_p)
6730 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6731 return unify_invalid (explain_p);
6732 }
6733
6734 static int
6735 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6736 {
6737 if (explain_p)
6738 inform (input_location,
6739 " template parameter %qD is not a parameter pack, but "
6740 "argument %qD is",
6741 parm, arg);
6742 return unify_invalid (explain_p);
6743 }
6744
6745 static int
6746 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6747 {
6748 if (explain_p)
6749 inform (input_location,
6750 " template argument %qE does not match "
6751 "pointer-to-member constant %qE",
6752 arg, parm);
6753 return unify_invalid (explain_p);
6754 }
6755
6756 static int
6757 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6758 {
6759 if (explain_p)
6760 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6761 return unify_invalid (explain_p);
6762 }
6763
6764 static int
6765 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6766 {
6767 if (explain_p)
6768 inform (input_location,
6769 " inconsistent parameter pack deduction with %qT and %qT",
6770 old_arg, new_arg);
6771 return unify_invalid (explain_p);
6772 }
6773
6774 static int
6775 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6776 {
6777 if (explain_p)
6778 {
6779 if (TYPE_P (parm))
6780 inform (input_location,
6781 " deduced conflicting types for parameter %qT (%qT and %qT)",
6782 parm, first, second);
6783 else
6784 inform (input_location,
6785 " deduced conflicting values for non-type parameter "
6786 "%qE (%qE and %qE)", parm, first, second);
6787 }
6788 return unify_invalid (explain_p);
6789 }
6790
6791 static int
6792 unify_vla_arg (bool explain_p, tree arg)
6793 {
6794 if (explain_p)
6795 inform (input_location,
6796 " variable-sized array type %qT is not "
6797 "a valid template argument",
6798 arg);
6799 return unify_invalid (explain_p);
6800 }
6801
6802 static int
6803 unify_method_type_error (bool explain_p, tree arg)
6804 {
6805 if (explain_p)
6806 inform (input_location,
6807 " member function type %qT is not a valid template argument",
6808 arg);
6809 return unify_invalid (explain_p);
6810 }
6811
6812 static int
6813 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6814 {
6815 if (explain_p)
6816 {
6817 if (least_p)
6818 inform_n (input_location, wanted,
6819 " candidate expects at least %d argument, %d provided",
6820 " candidate expects at least %d arguments, %d provided",
6821 wanted, have);
6822 else
6823 inform_n (input_location, wanted,
6824 " candidate expects %d argument, %d provided",
6825 " candidate expects %d arguments, %d provided",
6826 wanted, have);
6827 }
6828 return unify_invalid (explain_p);
6829 }
6830
6831 static int
6832 unify_too_many_arguments (bool explain_p, int have, int wanted)
6833 {
6834 return unify_arity (explain_p, have, wanted);
6835 }
6836
6837 static int
6838 unify_too_few_arguments (bool explain_p, int have, int wanted,
6839 bool least_p = false)
6840 {
6841 return unify_arity (explain_p, have, wanted, least_p);
6842 }
6843
6844 static int
6845 unify_arg_conversion (bool explain_p, tree to_type,
6846 tree from_type, tree arg)
6847 {
6848 if (explain_p)
6849 inform (cp_expr_loc_or_input_loc (arg),
6850 " cannot convert %qE (type %qT) to type %qT",
6851 arg, from_type, to_type);
6852 return unify_invalid (explain_p);
6853 }
6854
6855 static int
6856 unify_no_common_base (bool explain_p, enum template_base_result r,
6857 tree parm, tree arg)
6858 {
6859 if (explain_p)
6860 switch (r)
6861 {
6862 case tbr_ambiguous_baseclass:
6863 inform (input_location, " %qT is an ambiguous base class of %qT",
6864 parm, arg);
6865 break;
6866 default:
6867 inform (input_location, " %qT is not derived from %qT", arg, parm);
6868 break;
6869 }
6870 return unify_invalid (explain_p);
6871 }
6872
6873 static int
6874 unify_inconsistent_template_template_parameters (bool explain_p)
6875 {
6876 if (explain_p)
6877 inform (input_location,
6878 " template parameters of a template template argument are "
6879 "inconsistent with other deduced template arguments");
6880 return unify_invalid (explain_p);
6881 }
6882
6883 static int
6884 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6885 {
6886 if (explain_p)
6887 inform (input_location,
6888 " cannot deduce a template for %qT from non-template type %qT",
6889 parm, arg);
6890 return unify_invalid (explain_p);
6891 }
6892
6893 static int
6894 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6895 {
6896 if (explain_p)
6897 inform (input_location,
6898 " template argument %qE does not match %qE", arg, parm);
6899 return unify_invalid (explain_p);
6900 }
6901
6902 /* True if T is a C++20 template parameter object to store the argument for a
6903 template parameter of class type. */
6904
6905 bool
6906 template_parm_object_p (const_tree t)
6907 {
6908 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6909 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6910 }
6911
6912 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6913 argument for TYPE, points to an unsuitable object. */
6914
6915 static bool
6916 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6917 {
6918 switch (TREE_CODE (expr))
6919 {
6920 CASE_CONVERT:
6921 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6922 complain);
6923
6924 case TARGET_EXPR:
6925 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6926 complain);
6927
6928 case CONSTRUCTOR:
6929 {
6930 unsigned i; tree elt;
6931 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6932 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6933 return true;
6934 }
6935 break;
6936
6937 case ADDR_EXPR:
6938 {
6939 tree decl = TREE_OPERAND (expr, 0);
6940
6941 if (!VAR_P (decl))
6942 {
6943 if (complain & tf_error)
6944 error_at (cp_expr_loc_or_input_loc (expr),
6945 "%qE is not a valid template argument of type %qT "
6946 "because %qE is not a variable", expr, type, decl);
6947 return true;
6948 }
6949 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6950 {
6951 if (complain & tf_error)
6952 error_at (cp_expr_loc_or_input_loc (expr),
6953 "%qE is not a valid template argument of type %qT "
6954 "in C++98 because %qD does not have external linkage",
6955 expr, type, decl);
6956 return true;
6957 }
6958 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6959 && decl_linkage (decl) == lk_none)
6960 {
6961 if (complain & tf_error)
6962 error_at (cp_expr_loc_or_input_loc (expr),
6963 "%qE is not a valid template argument of type %qT "
6964 "because %qD has no linkage", expr, type, decl);
6965 return true;
6966 }
6967 /* C++17: For a non-type template-parameter of reference or pointer
6968 type, the value of the constant expression shall not refer to (or
6969 for a pointer type, shall not be the address of):
6970 * a subobject (4.5),
6971 * a temporary object (15.2),
6972 * a string literal (5.13.5),
6973 * the result of a typeid expression (8.2.8), or
6974 * a predefined __func__ variable (11.4.1). */
6975 else if (DECL_ARTIFICIAL (decl))
6976 {
6977 if (complain & tf_error)
6978 error ("the address of %qD is not a valid template argument",
6979 decl);
6980 return true;
6981 }
6982 else if (!same_type_ignoring_top_level_qualifiers_p
6983 (strip_array_types (TREE_TYPE (type)),
6984 strip_array_types (TREE_TYPE (decl))))
6985 {
6986 if (complain & tf_error)
6987 error ("the address of the %qT subobject of %qD is not a "
6988 "valid template argument", TREE_TYPE (type), decl);
6989 return true;
6990 }
6991 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6992 {
6993 if (complain & tf_error)
6994 error ("the address of %qD is not a valid template argument "
6995 "because it does not have static storage duration",
6996 decl);
6997 return true;
6998 }
6999 }
7000 break;
7001
7002 default:
7003 if (!INDIRECT_TYPE_P (type))
7004 /* We're only concerned about pointers and references here. */;
7005 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7006 /* Null pointer values are OK in C++11. */;
7007 else
7008 {
7009 if (VAR_P (expr))
7010 {
7011 if (complain & tf_error)
7012 error ("%qD is not a valid template argument "
7013 "because %qD is a variable, not the address of "
7014 "a variable", expr, expr);
7015 return true;
7016 }
7017 else
7018 {
7019 if (complain & tf_error)
7020 error ("%qE is not a valid template argument for %qT "
7021 "because it is not the address of a variable",
7022 expr, type);
7023 return true;
7024 }
7025 }
7026 }
7027 return false;
7028
7029 }
7030
7031 /* The template arguments corresponding to template parameter objects of types
7032 that contain pointers to members. */
7033
7034 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7035
7036 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7037 template argument EXPR. */
7038
7039 static tree
7040 get_template_parm_object (tree expr, tsubst_flags_t complain)
7041 {
7042 if (TREE_CODE (expr) == TARGET_EXPR)
7043 expr = TARGET_EXPR_INITIAL (expr);
7044
7045 if (!TREE_CONSTANT (expr))
7046 {
7047 if ((complain & tf_error)
7048 && require_rvalue_constant_expression (expr))
7049 cxx_constant_value (expr);
7050 return error_mark_node;
7051 }
7052 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7053 return error_mark_node;
7054
7055 tree name = mangle_template_parm_object (expr);
7056 tree decl = get_global_binding (name);
7057 if (decl)
7058 return decl;
7059
7060 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7061 decl = create_temporary_var (type);
7062 TREE_STATIC (decl) = true;
7063 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7064 TREE_READONLY (decl) = true;
7065 DECL_NAME (decl) = name;
7066 SET_DECL_ASSEMBLER_NAME (decl, name);
7067 DECL_CONTEXT (decl) = global_namespace;
7068 comdat_linkage (decl);
7069
7070 if (!zero_init_p (type))
7071 {
7072 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7073 lower_var_init before we're done mangling. So store the original
7074 value elsewhere. */
7075 tree copy = unshare_constructor (expr);
7076 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7077 }
7078
7079 pushdecl_top_level_and_finish (decl, expr);
7080
7081 return decl;
7082 }
7083
7084 /* Return the actual template argument corresponding to template parameter
7085 object VAR. */
7086
7087 tree
7088 tparm_object_argument (tree var)
7089 {
7090 if (zero_init_p (TREE_TYPE (var)))
7091 return DECL_INITIAL (var);
7092 return *(tparm_obj_values->get (var));
7093 }
7094
7095 /* Attempt to convert the non-type template parameter EXPR to the
7096 indicated TYPE. If the conversion is successful, return the
7097 converted value. If the conversion is unsuccessful, return
7098 NULL_TREE if we issued an error message, or error_mark_node if we
7099 did not. We issue error messages for out-and-out bad template
7100 parameters, but not simply because the conversion failed, since we
7101 might be just trying to do argument deduction. Both TYPE and EXPR
7102 must be non-dependent.
7103
7104 The conversion follows the special rules described in
7105 [temp.arg.nontype], and it is much more strict than an implicit
7106 conversion.
7107
7108 This function is called twice for each template argument (see
7109 lookup_template_class for a more accurate description of this
7110 problem). This means that we need to handle expressions which
7111 are not valid in a C++ source, but can be created from the
7112 first call (for instance, casts to perform conversions). These
7113 hacks can go away after we fix the double coercion problem. */
7114
7115 static tree
7116 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7117 {
7118 tree expr_type;
7119 location_t loc = cp_expr_loc_or_input_loc (expr);
7120
7121 /* Detect immediately string literals as invalid non-type argument.
7122 This special-case is not needed for correctness (we would easily
7123 catch this later), but only to provide better diagnostic for this
7124 common user mistake. As suggested by DR 100, we do not mention
7125 linkage issues in the diagnostic as this is not the point. */
7126 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7127 {
7128 if (complain & tf_error)
7129 error ("%qE is not a valid template argument for type %qT "
7130 "because string literals can never be used in this context",
7131 expr, type);
7132 return NULL_TREE;
7133 }
7134
7135 /* Add the ADDR_EXPR now for the benefit of
7136 value_dependent_expression_p. */
7137 if (TYPE_PTROBV_P (type)
7138 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7139 {
7140 expr = decay_conversion (expr, complain);
7141 if (expr == error_mark_node)
7142 return error_mark_node;
7143 }
7144
7145 /* If we are in a template, EXPR may be non-dependent, but still
7146 have a syntactic, rather than semantic, form. For example, EXPR
7147 might be a SCOPE_REF, rather than the VAR_DECL to which the
7148 SCOPE_REF refers. Preserving the qualifying scope is necessary
7149 so that access checking can be performed when the template is
7150 instantiated -- but here we need the resolved form so that we can
7151 convert the argument. */
7152 bool non_dep = false;
7153 if (TYPE_REF_OBJ_P (type)
7154 && has_value_dependent_address (expr))
7155 /* If we want the address and it's value-dependent, don't fold. */;
7156 else if (processing_template_decl
7157 && is_nondependent_constant_expression (expr))
7158 non_dep = true;
7159 if (error_operand_p (expr))
7160 return error_mark_node;
7161 expr_type = TREE_TYPE (expr);
7162
7163 /* If the argument is non-dependent, perform any conversions in
7164 non-dependent context as well. */
7165 processing_template_decl_sentinel s (non_dep);
7166 if (non_dep)
7167 expr = instantiate_non_dependent_expr_internal (expr, complain);
7168
7169 const bool val_dep_p = value_dependent_expression_p (expr);
7170 if (val_dep_p)
7171 expr = canonicalize_expr_argument (expr, complain);
7172
7173 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7174 to a non-type argument of "nullptr". */
7175 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7176 expr = fold_simple (convert (type, expr));
7177
7178 /* In C++11, integral or enumeration non-type template arguments can be
7179 arbitrary constant expressions. Pointer and pointer to
7180 member arguments can be general constant expressions that evaluate
7181 to a null value, but otherwise still need to be of a specific form. */
7182 if (cxx_dialect >= cxx11)
7183 {
7184 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7185 /* A PTRMEM_CST is already constant, and a valid template
7186 argument for a parameter of pointer to member type, we just want
7187 to leave it in that form rather than lower it to a
7188 CONSTRUCTOR. */;
7189 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7190 || cxx_dialect >= cxx17)
7191 {
7192 /* C++17: A template-argument for a non-type template-parameter shall
7193 be a converted constant expression (8.20) of the type of the
7194 template-parameter. */
7195 expr = build_converted_constant_expr (type, expr, complain);
7196 if (expr == error_mark_node)
7197 /* Make sure we return NULL_TREE only if we have really issued
7198 an error, as described above. */
7199 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7200 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7201 {
7202 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7203 return expr;
7204 }
7205 expr = maybe_constant_value (expr, NULL_TREE,
7206 /*manifestly_const_eval=*/true);
7207 expr = convert_from_reference (expr);
7208 }
7209 else if (TYPE_PTR_OR_PTRMEM_P (type))
7210 {
7211 tree folded = maybe_constant_value (expr, NULL_TREE,
7212 /*manifestly_const_eval=*/true);
7213 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7214 : null_member_pointer_value_p (folded))
7215 expr = folded;
7216 }
7217 }
7218
7219 if (TYPE_REF_P (type))
7220 expr = mark_lvalue_use (expr);
7221 else
7222 expr = mark_rvalue_use (expr);
7223
7224 /* HACK: Due to double coercion, we can get a
7225 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7226 which is the tree that we built on the first call (see
7227 below when coercing to reference to object or to reference to
7228 function). We just strip everything and get to the arg.
7229 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7230 for examples. */
7231 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7232 {
7233 tree probe_type, probe = expr;
7234 if (REFERENCE_REF_P (probe))
7235 probe = TREE_OPERAND (probe, 0);
7236 probe_type = TREE_TYPE (probe);
7237 if (TREE_CODE (probe) == NOP_EXPR)
7238 {
7239 /* ??? Maybe we could use convert_from_reference here, but we
7240 would need to relax its constraints because the NOP_EXPR
7241 could actually change the type to something more cv-qualified,
7242 and this is not folded by convert_from_reference. */
7243 tree addr = TREE_OPERAND (probe, 0);
7244 if (TYPE_REF_P (probe_type)
7245 && TREE_CODE (addr) == ADDR_EXPR
7246 && TYPE_PTR_P (TREE_TYPE (addr))
7247 && (same_type_ignoring_top_level_qualifiers_p
7248 (TREE_TYPE (probe_type),
7249 TREE_TYPE (TREE_TYPE (addr)))))
7250 {
7251 expr = TREE_OPERAND (addr, 0);
7252 expr_type = TREE_TYPE (probe_type);
7253 }
7254 }
7255 }
7256
7257 /* [temp.arg.nontype]/5, bullet 1
7258
7259 For a non-type template-parameter of integral or enumeration type,
7260 integral promotions (_conv.prom_) and integral conversions
7261 (_conv.integral_) are applied. */
7262 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7263 {
7264 if (cxx_dialect < cxx11)
7265 {
7266 tree t = build_converted_constant_expr (type, expr, complain);
7267 t = maybe_constant_value (t);
7268 if (t != error_mark_node)
7269 expr = t;
7270 }
7271
7272 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7273 return error_mark_node;
7274
7275 /* Notice that there are constant expressions like '4 % 0' which
7276 do not fold into integer constants. */
7277 if (TREE_CODE (expr) != INTEGER_CST && !val_dep_p)
7278 {
7279 if (complain & tf_error)
7280 {
7281 int errs = errorcount, warns = warningcount + werrorcount;
7282 if (!require_potential_constant_expression (expr))
7283 expr = error_mark_node;
7284 else
7285 expr = cxx_constant_value (expr);
7286 if (errorcount > errs || warningcount + werrorcount > warns)
7287 inform (loc, "in template argument for type %qT", type);
7288 if (expr == error_mark_node)
7289 return NULL_TREE;
7290 /* else cxx_constant_value complained but gave us
7291 a real constant, so go ahead. */
7292 if (TREE_CODE (expr) != INTEGER_CST)
7293 {
7294 /* Some assemble time constant expressions like
7295 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7296 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7297 as we can emit them into .rodata initializers of
7298 variables, yet they can't fold into an INTEGER_CST at
7299 compile time. Refuse them here. */
7300 gcc_checking_assert (reduced_constant_expression_p (expr));
7301 error_at (loc, "template argument %qE for type %qT not "
7302 "a constant integer", expr, type);
7303 return NULL_TREE;
7304 }
7305 }
7306 else
7307 return NULL_TREE;
7308 }
7309
7310 /* Avoid typedef problems. */
7311 if (TREE_TYPE (expr) != type)
7312 expr = fold_convert (type, expr);
7313 }
7314 /* [temp.arg.nontype]/5, bullet 2
7315
7316 For a non-type template-parameter of type pointer to object,
7317 qualification conversions (_conv.qual_) and the array-to-pointer
7318 conversion (_conv.array_) are applied. */
7319 else if (TYPE_PTROBV_P (type))
7320 {
7321 tree decayed = expr;
7322
7323 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7324 decay_conversion or an explicit cast. If it's a problematic cast,
7325 we'll complain about it below. */
7326 if (TREE_CODE (expr) == NOP_EXPR)
7327 {
7328 tree probe = expr;
7329 STRIP_NOPS (probe);
7330 if (TREE_CODE (probe) == ADDR_EXPR
7331 && TYPE_PTR_P (TREE_TYPE (probe)))
7332 {
7333 expr = probe;
7334 expr_type = TREE_TYPE (expr);
7335 }
7336 }
7337
7338 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7339
7340 A template-argument for a non-type, non-template template-parameter
7341 shall be one of: [...]
7342
7343 -- the name of a non-type template-parameter;
7344 -- the address of an object or function with external linkage, [...]
7345 expressed as "& id-expression" where the & is optional if the name
7346 refers to a function or array, or if the corresponding
7347 template-parameter is a reference.
7348
7349 Here, we do not care about functions, as they are invalid anyway
7350 for a parameter of type pointer-to-object. */
7351
7352 if (val_dep_p)
7353 /* Non-type template parameters are OK. */
7354 ;
7355 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7356 /* Null pointer values are OK in C++11. */;
7357 else if (TREE_CODE (expr) != ADDR_EXPR
7358 && !INDIRECT_TYPE_P (expr_type))
7359 /* Other values, like integer constants, might be valid
7360 non-type arguments of some other type. */
7361 return error_mark_node;
7362 else if (invalid_tparm_referent_p (type, expr, complain))
7363 return NULL_TREE;
7364
7365 expr = decayed;
7366
7367 expr = perform_qualification_conversions (type, expr);
7368 if (expr == error_mark_node)
7369 return error_mark_node;
7370 }
7371 /* [temp.arg.nontype]/5, bullet 3
7372
7373 For a non-type template-parameter of type reference to object, no
7374 conversions apply. The type referred to by the reference may be more
7375 cv-qualified than the (otherwise identical) type of the
7376 template-argument. The template-parameter is bound directly to the
7377 template-argument, which must be an lvalue. */
7378 else if (TYPE_REF_OBJ_P (type))
7379 {
7380 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7381 expr_type))
7382 return error_mark_node;
7383
7384 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7385 {
7386 if (complain & tf_error)
7387 error ("%qE is not a valid template argument for type %qT "
7388 "because of conflicts in cv-qualification", expr, type);
7389 return NULL_TREE;
7390 }
7391
7392 if (!lvalue_p (expr))
7393 {
7394 if (complain & tf_error)
7395 error ("%qE is not a valid template argument for type %qT "
7396 "because it is not an lvalue", expr, type);
7397 return NULL_TREE;
7398 }
7399
7400 /* [temp.arg.nontype]/1
7401
7402 A template-argument for a non-type, non-template template-parameter
7403 shall be one of: [...]
7404
7405 -- the address of an object or function with external linkage. */
7406 if (INDIRECT_REF_P (expr)
7407 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7408 {
7409 expr = TREE_OPERAND (expr, 0);
7410 if (DECL_P (expr))
7411 {
7412 if (complain & tf_error)
7413 error ("%q#D is not a valid template argument for type %qT "
7414 "because a reference variable does not have a constant "
7415 "address", expr, type);
7416 return NULL_TREE;
7417 }
7418 }
7419
7420 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7421 /* OK, dependent reference. We don't want to ask whether a DECL is
7422 itself value-dependent, since what we want here is its address. */;
7423 else
7424 {
7425 expr = build_address (expr);
7426
7427 if (invalid_tparm_referent_p (type, expr, complain))
7428 return NULL_TREE;
7429 }
7430
7431 if (!same_type_p (type, TREE_TYPE (expr)))
7432 expr = build_nop (type, expr);
7433 }
7434 /* [temp.arg.nontype]/5, bullet 4
7435
7436 For a non-type template-parameter of type pointer to function, only
7437 the function-to-pointer conversion (_conv.func_) is applied. If the
7438 template-argument represents a set of overloaded functions (or a
7439 pointer to such), the matching function is selected from the set
7440 (_over.over_). */
7441 else if (TYPE_PTRFN_P (type))
7442 {
7443 /* If the argument is a template-id, we might not have enough
7444 context information to decay the pointer. */
7445 if (!type_unknown_p (expr_type))
7446 {
7447 expr = decay_conversion (expr, complain);
7448 if (expr == error_mark_node)
7449 return error_mark_node;
7450 }
7451
7452 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7453 /* Null pointer values are OK in C++11. */
7454 return perform_qualification_conversions (type, expr);
7455
7456 expr = convert_nontype_argument_function (type, expr, complain);
7457 if (!expr || expr == error_mark_node)
7458 return expr;
7459 }
7460 /* [temp.arg.nontype]/5, bullet 5
7461
7462 For a non-type template-parameter of type reference to function, no
7463 conversions apply. If the template-argument represents a set of
7464 overloaded functions, the matching function is selected from the set
7465 (_over.over_). */
7466 else if (TYPE_REFFN_P (type))
7467 {
7468 if (TREE_CODE (expr) == ADDR_EXPR)
7469 {
7470 if (complain & tf_error)
7471 {
7472 error ("%qE is not a valid template argument for type %qT "
7473 "because it is a pointer", expr, type);
7474 inform (input_location, "try using %qE instead",
7475 TREE_OPERAND (expr, 0));
7476 }
7477 return NULL_TREE;
7478 }
7479
7480 expr = convert_nontype_argument_function (type, expr, complain);
7481 if (!expr || expr == error_mark_node)
7482 return expr;
7483 }
7484 /* [temp.arg.nontype]/5, bullet 6
7485
7486 For a non-type template-parameter of type pointer to member function,
7487 no conversions apply. If the template-argument represents a set of
7488 overloaded member functions, the matching member function is selected
7489 from the set (_over.over_). */
7490 else if (TYPE_PTRMEMFUNC_P (type))
7491 {
7492 expr = instantiate_type (type, expr, tf_none);
7493 if (expr == error_mark_node)
7494 return error_mark_node;
7495
7496 /* [temp.arg.nontype] bullet 1 says the pointer to member
7497 expression must be a pointer-to-member constant. */
7498 if (!val_dep_p
7499 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7500 return NULL_TREE;
7501
7502 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7503 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7504 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7505 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7506 }
7507 /* [temp.arg.nontype]/5, bullet 7
7508
7509 For a non-type template-parameter of type pointer to data member,
7510 qualification conversions (_conv.qual_) are applied. */
7511 else if (TYPE_PTRDATAMEM_P (type))
7512 {
7513 /* [temp.arg.nontype] bullet 1 says the pointer to member
7514 expression must be a pointer-to-member constant. */
7515 if (!val_dep_p
7516 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7517 return NULL_TREE;
7518
7519 expr = perform_qualification_conversions (type, expr);
7520 if (expr == error_mark_node)
7521 return expr;
7522 }
7523 else if (NULLPTR_TYPE_P (type))
7524 {
7525 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7526 {
7527 if (complain & tf_error)
7528 error ("%qE is not a valid template argument for type %qT "
7529 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7530 return NULL_TREE;
7531 }
7532 return expr;
7533 }
7534 else if (CLASS_TYPE_P (type))
7535 {
7536 /* Replace the argument with a reference to the corresponding template
7537 parameter object. */
7538 if (!val_dep_p)
7539 expr = get_template_parm_object (expr, complain);
7540 if (expr == error_mark_node)
7541 return NULL_TREE;
7542 }
7543 /* A template non-type parameter must be one of the above. */
7544 else
7545 gcc_unreachable ();
7546
7547 /* Sanity check: did we actually convert the argument to the
7548 right type? */
7549 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7550 (type, TREE_TYPE (expr)));
7551 return convert_from_reference (expr);
7552 }
7553
7554 /* Subroutine of coerce_template_template_parms, which returns 1 if
7555 PARM_PARM and ARG_PARM match using the rule for the template
7556 parameters of template template parameters. Both PARM and ARG are
7557 template parameters; the rest of the arguments are the same as for
7558 coerce_template_template_parms.
7559 */
7560 static int
7561 coerce_template_template_parm (tree parm,
7562 tree arg,
7563 tsubst_flags_t complain,
7564 tree in_decl,
7565 tree outer_args)
7566 {
7567 if (arg == NULL_TREE || error_operand_p (arg)
7568 || parm == NULL_TREE || error_operand_p (parm))
7569 return 0;
7570
7571 if (TREE_CODE (arg) != TREE_CODE (parm))
7572 return 0;
7573
7574 switch (TREE_CODE (parm))
7575 {
7576 case TEMPLATE_DECL:
7577 /* We encounter instantiations of templates like
7578 template <template <template <class> class> class TT>
7579 class C; */
7580 {
7581 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7582 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7583
7584 if (!coerce_template_template_parms
7585 (parmparm, argparm, complain, in_decl, outer_args))
7586 return 0;
7587 }
7588 /* Fall through. */
7589
7590 case TYPE_DECL:
7591 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7592 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7593 /* Argument is a parameter pack but parameter is not. */
7594 return 0;
7595 break;
7596
7597 case PARM_DECL:
7598 /* The tsubst call is used to handle cases such as
7599
7600 template <int> class C {};
7601 template <class T, template <T> class TT> class D {};
7602 D<int, C> d;
7603
7604 i.e. the parameter list of TT depends on earlier parameters. */
7605 if (!uses_template_parms (TREE_TYPE (arg)))
7606 {
7607 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7608 if (!uses_template_parms (t)
7609 && !same_type_p (t, TREE_TYPE (arg)))
7610 return 0;
7611 }
7612
7613 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7614 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7615 /* Argument is a parameter pack but parameter is not. */
7616 return 0;
7617
7618 break;
7619
7620 default:
7621 gcc_unreachable ();
7622 }
7623
7624 return 1;
7625 }
7626
7627 /* Coerce template argument list ARGLIST for use with template
7628 template-parameter TEMPL. */
7629
7630 static tree
7631 coerce_template_args_for_ttp (tree templ, tree arglist,
7632 tsubst_flags_t complain)
7633 {
7634 /* Consider an example where a template template parameter declared as
7635
7636 template <class T, class U = std::allocator<T> > class TT
7637
7638 The template parameter level of T and U are one level larger than
7639 of TT. To proper process the default argument of U, say when an
7640 instantiation `TT<int>' is seen, we need to build the full
7641 arguments containing {int} as the innermost level. Outer levels,
7642 available when not appearing as default template argument, can be
7643 obtained from the arguments of the enclosing template.
7644
7645 Suppose that TT is later substituted with std::vector. The above
7646 instantiation is `TT<int, std::allocator<T> >' with TT at
7647 level 1, and T at level 2, while the template arguments at level 1
7648 becomes {std::vector} and the inner level 2 is {int}. */
7649
7650 tree outer = DECL_CONTEXT (templ);
7651 if (outer)
7652 outer = generic_targs_for (outer);
7653 else if (current_template_parms)
7654 {
7655 /* This is an argument of the current template, so we haven't set
7656 DECL_CONTEXT yet. */
7657 tree relevant_template_parms;
7658
7659 /* Parameter levels that are greater than the level of the given
7660 template template parm are irrelevant. */
7661 relevant_template_parms = current_template_parms;
7662 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7663 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7664 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7665
7666 outer = template_parms_to_args (relevant_template_parms);
7667 }
7668
7669 if (outer)
7670 arglist = add_to_template_args (outer, arglist);
7671
7672 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7673 return coerce_template_parms (parmlist, arglist, templ,
7674 complain,
7675 /*require_all_args=*/true,
7676 /*use_default_args=*/true);
7677 }
7678
7679 /* A cache of template template parameters with match-all default
7680 arguments. */
7681 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7682
7683 /* T is a bound template template-parameter. Copy its arguments into default
7684 arguments of the template template-parameter's template parameters. */
7685
7686 static tree
7687 add_defaults_to_ttp (tree otmpl)
7688 {
7689 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7690 return *c;
7691
7692 tree ntmpl = copy_node (otmpl);
7693
7694 tree ntype = copy_node (TREE_TYPE (otmpl));
7695 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7696 TYPE_MAIN_VARIANT (ntype) = ntype;
7697 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7698 TYPE_NAME (ntype) = ntmpl;
7699 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7700
7701 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7702 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7703 TEMPLATE_PARM_DECL (idx) = ntmpl;
7704 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7705
7706 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7707 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7708 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7709 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7710 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7711 {
7712 tree o = TREE_VEC_ELT (vec, i);
7713 if (!template_parameter_pack_p (TREE_VALUE (o)))
7714 {
7715 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7716 TREE_PURPOSE (n) = any_targ_node;
7717 }
7718 }
7719
7720 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7721 return ntmpl;
7722 }
7723
7724 /* ARG is a bound potential template template-argument, and PARGS is a list
7725 of arguments for the corresponding template template-parameter. Adjust
7726 PARGS as appropriate for application to ARG's template, and if ARG is a
7727 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7728 arguments to the template template parameter. */
7729
7730 static tree
7731 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7732 {
7733 ++processing_template_decl;
7734 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7735 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7736 {
7737 /* When comparing two template template-parameters in partial ordering,
7738 rewrite the one currently being used as an argument to have default
7739 arguments for all parameters. */
7740 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7741 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7742 if (pargs != error_mark_node)
7743 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7744 TYPE_TI_ARGS (arg));
7745 }
7746 else
7747 {
7748 tree aparms
7749 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7750 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7751 /*require_all*/true,
7752 /*use_default*/true);
7753 }
7754 --processing_template_decl;
7755 return pargs;
7756 }
7757
7758 /* Subroutine of unify for the case when PARM is a
7759 BOUND_TEMPLATE_TEMPLATE_PARM. */
7760
7761 static int
7762 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7763 bool explain_p)
7764 {
7765 tree parmvec = TYPE_TI_ARGS (parm);
7766 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7767
7768 /* The template template parm might be variadic and the argument
7769 not, so flatten both argument lists. */
7770 parmvec = expand_template_argument_pack (parmvec);
7771 argvec = expand_template_argument_pack (argvec);
7772
7773 if (flag_new_ttp)
7774 {
7775 /* In keeping with P0522R0, adjust P's template arguments
7776 to apply to A's template; then flatten it again. */
7777 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7778 nparmvec = expand_template_argument_pack (nparmvec);
7779
7780 if (unify (tparms, targs, nparmvec, argvec,
7781 UNIFY_ALLOW_NONE, explain_p))
7782 return 1;
7783
7784 /* If the P0522 adjustment eliminated a pack expansion, deduce
7785 empty packs. */
7786 if (flag_new_ttp
7787 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7788 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7789 DEDUCE_EXACT, /*sub*/true, explain_p))
7790 return 1;
7791 }
7792 else
7793 {
7794 /* Deduce arguments T, i from TT<T> or TT<i>.
7795 We check each element of PARMVEC and ARGVEC individually
7796 rather than the whole TREE_VEC since they can have
7797 different number of elements, which is allowed under N2555. */
7798
7799 int len = TREE_VEC_LENGTH (parmvec);
7800
7801 /* Check if the parameters end in a pack, making them
7802 variadic. */
7803 int parm_variadic_p = 0;
7804 if (len > 0
7805 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7806 parm_variadic_p = 1;
7807
7808 for (int i = 0; i < len - parm_variadic_p; ++i)
7809 /* If the template argument list of P contains a pack
7810 expansion that is not the last template argument, the
7811 entire template argument list is a non-deduced
7812 context. */
7813 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7814 return unify_success (explain_p);
7815
7816 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7817 return unify_too_few_arguments (explain_p,
7818 TREE_VEC_LENGTH (argvec), len);
7819
7820 for (int i = 0; i < len - parm_variadic_p; ++i)
7821 if (unify (tparms, targs,
7822 TREE_VEC_ELT (parmvec, i),
7823 TREE_VEC_ELT (argvec, i),
7824 UNIFY_ALLOW_NONE, explain_p))
7825 return 1;
7826
7827 if (parm_variadic_p
7828 && unify_pack_expansion (tparms, targs,
7829 parmvec, argvec,
7830 DEDUCE_EXACT,
7831 /*subr=*/true, explain_p))
7832 return 1;
7833 }
7834
7835 return 0;
7836 }
7837
7838 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7839 template template parameters. Both PARM_PARMS and ARG_PARMS are
7840 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7841 or PARM_DECL.
7842
7843 Consider the example:
7844 template <class T> class A;
7845 template<template <class U> class TT> class B;
7846
7847 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7848 the parameters to A, and OUTER_ARGS contains A. */
7849
7850 static int
7851 coerce_template_template_parms (tree parm_parms,
7852 tree arg_parms,
7853 tsubst_flags_t complain,
7854 tree in_decl,
7855 tree outer_args)
7856 {
7857 int nparms, nargs, i;
7858 tree parm, arg;
7859 int variadic_p = 0;
7860
7861 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7862 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7863
7864 nparms = TREE_VEC_LENGTH (parm_parms);
7865 nargs = TREE_VEC_LENGTH (arg_parms);
7866
7867 if (flag_new_ttp)
7868 {
7869 /* P0522R0: A template template-parameter P is at least as specialized as
7870 a template template-argument A if, given the following rewrite to two
7871 function templates, the function template corresponding to P is at
7872 least as specialized as the function template corresponding to A
7873 according to the partial ordering rules for function templates
7874 ([temp.func.order]). Given an invented class template X with the
7875 template parameter list of A (including default arguments):
7876
7877 * Each of the two function templates has the same template parameters,
7878 respectively, as P or A.
7879
7880 * Each function template has a single function parameter whose type is
7881 a specialization of X with template arguments corresponding to the
7882 template parameters from the respective function template where, for
7883 each template parameter PP in the template parameter list of the
7884 function template, a corresponding template argument AA is formed. If
7885 PP declares a parameter pack, then AA is the pack expansion
7886 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7887
7888 If the rewrite produces an invalid type, then P is not at least as
7889 specialized as A. */
7890
7891 /* So coerce P's args to apply to A's parms, and then deduce between A's
7892 args and the converted args. If that succeeds, A is at least as
7893 specialized as P, so they match.*/
7894 tree pargs = template_parms_level_to_args (parm_parms);
7895 pargs = add_outermost_template_args (outer_args, pargs);
7896 ++processing_template_decl;
7897 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7898 /*require_all*/true, /*use_default*/true);
7899 --processing_template_decl;
7900 if (pargs != error_mark_node)
7901 {
7902 tree targs = make_tree_vec (nargs);
7903 tree aargs = template_parms_level_to_args (arg_parms);
7904 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7905 /*explain*/false))
7906 return 1;
7907 }
7908 }
7909
7910 /* Determine whether we have a parameter pack at the end of the
7911 template template parameter's template parameter list. */
7912 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7913 {
7914 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7915
7916 if (error_operand_p (parm))
7917 return 0;
7918
7919 switch (TREE_CODE (parm))
7920 {
7921 case TEMPLATE_DECL:
7922 case TYPE_DECL:
7923 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7924 variadic_p = 1;
7925 break;
7926
7927 case PARM_DECL:
7928 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7929 variadic_p = 1;
7930 break;
7931
7932 default:
7933 gcc_unreachable ();
7934 }
7935 }
7936
7937 if (nargs != nparms
7938 && !(variadic_p && nargs >= nparms - 1))
7939 return 0;
7940
7941 /* Check all of the template parameters except the parameter pack at
7942 the end (if any). */
7943 for (i = 0; i < nparms - variadic_p; ++i)
7944 {
7945 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7946 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7947 continue;
7948
7949 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7950 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7951
7952 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7953 outer_args))
7954 return 0;
7955
7956 }
7957
7958 if (variadic_p)
7959 {
7960 /* Check each of the template parameters in the template
7961 argument against the template parameter pack at the end of
7962 the template template parameter. */
7963 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7964 return 0;
7965
7966 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7967
7968 for (; i < nargs; ++i)
7969 {
7970 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7971 continue;
7972
7973 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7974
7975 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7976 outer_args))
7977 return 0;
7978 }
7979 }
7980
7981 return 1;
7982 }
7983
7984 /* Verifies that the deduced template arguments (in TARGS) for the
7985 template template parameters (in TPARMS) represent valid bindings,
7986 by comparing the template parameter list of each template argument
7987 to the template parameter list of its corresponding template
7988 template parameter, in accordance with DR150. This
7989 routine can only be called after all template arguments have been
7990 deduced. It will return TRUE if all of the template template
7991 parameter bindings are okay, FALSE otherwise. */
7992 bool
7993 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7994 {
7995 int i, ntparms = TREE_VEC_LENGTH (tparms);
7996 bool ret = true;
7997
7998 /* We're dealing with template parms in this process. */
7999 ++processing_template_decl;
8000
8001 targs = INNERMOST_TEMPLATE_ARGS (targs);
8002
8003 for (i = 0; i < ntparms; ++i)
8004 {
8005 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8006 tree targ = TREE_VEC_ELT (targs, i);
8007
8008 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8009 {
8010 tree packed_args = NULL_TREE;
8011 int idx, len = 1;
8012
8013 if (ARGUMENT_PACK_P (targ))
8014 {
8015 /* Look inside the argument pack. */
8016 packed_args = ARGUMENT_PACK_ARGS (targ);
8017 len = TREE_VEC_LENGTH (packed_args);
8018 }
8019
8020 for (idx = 0; idx < len; ++idx)
8021 {
8022 tree targ_parms = NULL_TREE;
8023
8024 if (packed_args)
8025 /* Extract the next argument from the argument
8026 pack. */
8027 targ = TREE_VEC_ELT (packed_args, idx);
8028
8029 if (PACK_EXPANSION_P (targ))
8030 /* Look at the pattern of the pack expansion. */
8031 targ = PACK_EXPANSION_PATTERN (targ);
8032
8033 /* Extract the template parameters from the template
8034 argument. */
8035 if (TREE_CODE (targ) == TEMPLATE_DECL)
8036 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8037 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8038 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8039
8040 /* Verify that we can coerce the template template
8041 parameters from the template argument to the template
8042 parameter. This requires an exact match. */
8043 if (targ_parms
8044 && !coerce_template_template_parms
8045 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8046 targ_parms,
8047 tf_none,
8048 tparm,
8049 targs))
8050 {
8051 ret = false;
8052 goto out;
8053 }
8054 }
8055 }
8056 }
8057
8058 out:
8059
8060 --processing_template_decl;
8061 return ret;
8062 }
8063
8064 /* Since type attributes aren't mangled, we need to strip them from
8065 template type arguments. */
8066
8067 tree
8068 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8069 {
8070 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8071 return arg;
8072 bool removed_attributes = false;
8073 tree canon = strip_typedefs (arg, &removed_attributes);
8074 if (removed_attributes
8075 && (complain & tf_warning))
8076 warning (OPT_Wignored_attributes,
8077 "ignoring attributes on template argument %qT", arg);
8078 return canon;
8079 }
8080
8081 /* And from inside dependent non-type arguments like sizeof(Type). */
8082
8083 static tree
8084 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8085 {
8086 if (!arg || arg == error_mark_node)
8087 return arg;
8088 bool removed_attributes = false;
8089 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8090 if (removed_attributes
8091 && (complain & tf_warning))
8092 warning (OPT_Wignored_attributes,
8093 "ignoring attributes in template argument %qE", arg);
8094 return canon;
8095 }
8096
8097 // A template declaration can be substituted for a constrained
8098 // template template parameter only when the argument is more
8099 // constrained than the parameter.
8100 static bool
8101 is_compatible_template_arg (tree parm, tree arg)
8102 {
8103 tree parm_cons = get_constraints (parm);
8104
8105 /* For now, allow constrained template template arguments
8106 and unconstrained template template parameters. */
8107 if (parm_cons == NULL_TREE)
8108 return true;
8109
8110 /* If the template parameter is constrained, we need to rewrite its
8111 constraints in terms of the ARG's template parameters. This ensures
8112 that all of the template parameter types will have the same depth.
8113
8114 Note that this is only valid when coerce_template_template_parm is
8115 true for the innermost template parameters of PARM and ARG. In other
8116 words, because coercion is successful, this conversion will be valid. */
8117 tree new_args = NULL_TREE;
8118 if (parm_cons)
8119 {
8120 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8121 new_args = template_parms_level_to_args (aparms);
8122 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8123 tf_none, NULL_TREE);
8124 if (parm_cons == error_mark_node)
8125 return false;
8126 }
8127
8128 return weakly_subsumes (parm_cons, new_args, arg);
8129 }
8130
8131 // Convert a placeholder argument into a binding to the original
8132 // parameter. The original parameter is saved as the TREE_TYPE of
8133 // ARG.
8134 static inline tree
8135 convert_wildcard_argument (tree parm, tree arg)
8136 {
8137 TREE_TYPE (arg) = parm;
8138 return arg;
8139 }
8140
8141 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8142 because one of them is dependent. But we need to represent the
8143 conversion for the benefit of cp_tree_equal. */
8144
8145 static tree
8146 maybe_convert_nontype_argument (tree type, tree arg)
8147 {
8148 /* Auto parms get no conversion. */
8149 if (type_uses_auto (type))
8150 return arg;
8151 /* We don't need or want to add this conversion now if we're going to use the
8152 argument for deduction. */
8153 if (value_dependent_expression_p (arg))
8154 return arg;
8155
8156 type = cv_unqualified (type);
8157 tree argtype = TREE_TYPE (arg);
8158 if (same_type_p (type, argtype))
8159 return arg;
8160
8161 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8162 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8163 return arg;
8164 }
8165
8166 /* Convert the indicated template ARG as necessary to match the
8167 indicated template PARM. Returns the converted ARG, or
8168 error_mark_node if the conversion was unsuccessful. Error and
8169 warning messages are issued under control of COMPLAIN. This
8170 conversion is for the Ith parameter in the parameter list. ARGS is
8171 the full set of template arguments deduced so far. */
8172
8173 static tree
8174 convert_template_argument (tree parm,
8175 tree arg,
8176 tree args,
8177 tsubst_flags_t complain,
8178 int i,
8179 tree in_decl)
8180 {
8181 tree orig_arg;
8182 tree val;
8183 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8184
8185 if (parm == error_mark_node || error_operand_p (arg))
8186 return error_mark_node;
8187
8188 /* Trivially convert placeholders. */
8189 if (TREE_CODE (arg) == WILDCARD_DECL)
8190 return convert_wildcard_argument (parm, arg);
8191
8192 if (arg == any_targ_node)
8193 return arg;
8194
8195 if (TREE_CODE (arg) == TREE_LIST
8196 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8197 {
8198 /* The template argument was the name of some
8199 member function. That's usually
8200 invalid, but static members are OK. In any
8201 case, grab the underlying fields/functions
8202 and issue an error later if required. */
8203 TREE_TYPE (arg) = unknown_type_node;
8204 }
8205
8206 orig_arg = arg;
8207
8208 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8209 requires_type = (TREE_CODE (parm) == TYPE_DECL
8210 || requires_tmpl_type);
8211
8212 /* When determining whether an argument pack expansion is a template,
8213 look at the pattern. */
8214 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8215 arg = PACK_EXPANSION_PATTERN (arg);
8216
8217 /* Deal with an injected-class-name used as a template template arg. */
8218 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8219 {
8220 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8221 if (TREE_CODE (t) == TEMPLATE_DECL)
8222 {
8223 if (cxx_dialect >= cxx11)
8224 /* OK under DR 1004. */;
8225 else if (complain & tf_warning_or_error)
8226 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8227 " used as template template argument", TYPE_NAME (arg));
8228 else if (flag_pedantic_errors)
8229 t = arg;
8230
8231 arg = t;
8232 }
8233 }
8234
8235 is_tmpl_type =
8236 ((TREE_CODE (arg) == TEMPLATE_DECL
8237 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8238 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8239 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8240 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8241
8242 if (is_tmpl_type
8243 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8244 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8245 arg = TYPE_STUB_DECL (arg);
8246
8247 is_type = TYPE_P (arg) || is_tmpl_type;
8248
8249 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8250 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8251 {
8252 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8253 {
8254 if (complain & tf_error)
8255 error ("invalid use of destructor %qE as a type", orig_arg);
8256 return error_mark_node;
8257 }
8258
8259 permerror (input_location,
8260 "to refer to a type member of a template parameter, "
8261 "use %<typename %E%>", orig_arg);
8262
8263 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8264 TREE_OPERAND (arg, 1),
8265 typename_type,
8266 complain);
8267 arg = orig_arg;
8268 is_type = 1;
8269 }
8270 if (is_type != requires_type)
8271 {
8272 if (in_decl)
8273 {
8274 if (complain & tf_error)
8275 {
8276 error ("type/value mismatch at argument %d in template "
8277 "parameter list for %qD",
8278 i + 1, in_decl);
8279 if (is_type)
8280 {
8281 /* The template argument is a type, but we're expecting
8282 an expression. */
8283 inform (input_location,
8284 " expected a constant of type %qT, got %qT",
8285 TREE_TYPE (parm),
8286 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8287 /* [temp.arg]/2: "In a template-argument, an ambiguity
8288 between a type-id and an expression is resolved to a
8289 type-id, regardless of the form of the corresponding
8290 template-parameter." So give the user a clue. */
8291 if (TREE_CODE (arg) == FUNCTION_TYPE)
8292 inform (input_location, " ambiguous template argument "
8293 "for non-type template parameter is treated as "
8294 "function type");
8295 }
8296 else if (requires_tmpl_type)
8297 inform (input_location,
8298 " expected a class template, got %qE", orig_arg);
8299 else
8300 inform (input_location,
8301 " expected a type, got %qE", orig_arg);
8302 }
8303 }
8304 return error_mark_node;
8305 }
8306 if (is_tmpl_type ^ requires_tmpl_type)
8307 {
8308 if (in_decl && (complain & tf_error))
8309 {
8310 error ("type/value mismatch at argument %d in template "
8311 "parameter list for %qD",
8312 i + 1, in_decl);
8313 if (is_tmpl_type)
8314 inform (input_location,
8315 " expected a type, got %qT", DECL_NAME (arg));
8316 else
8317 inform (input_location,
8318 " expected a class template, got %qT", orig_arg);
8319 }
8320 return error_mark_node;
8321 }
8322
8323 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8324 /* We already did the appropriate conversion when packing args. */
8325 val = orig_arg;
8326 else if (is_type)
8327 {
8328 if (requires_tmpl_type)
8329 {
8330 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8331 /* The number of argument required is not known yet.
8332 Just accept it for now. */
8333 val = orig_arg;
8334 else
8335 {
8336 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8337 tree argparm;
8338
8339 /* Strip alias templates that are equivalent to another
8340 template. */
8341 arg = get_underlying_template (arg);
8342 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8343
8344 if (coerce_template_template_parms (parmparm, argparm,
8345 complain, in_decl,
8346 args))
8347 {
8348 val = arg;
8349
8350 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8351 TEMPLATE_DECL. */
8352 if (val != error_mark_node)
8353 {
8354 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8355 val = TREE_TYPE (val);
8356 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8357 val = make_pack_expansion (val, complain);
8358 }
8359 }
8360 else
8361 {
8362 if (in_decl && (complain & tf_error))
8363 {
8364 error ("type/value mismatch at argument %d in "
8365 "template parameter list for %qD",
8366 i + 1, in_decl);
8367 inform (input_location,
8368 " expected a template of type %qD, got %qT",
8369 parm, orig_arg);
8370 }
8371
8372 val = error_mark_node;
8373 }
8374
8375 // Check that the constraints are compatible before allowing the
8376 // substitution.
8377 if (val != error_mark_node)
8378 if (!is_compatible_template_arg (parm, arg))
8379 {
8380 if (in_decl && (complain & tf_error))
8381 {
8382 error ("constraint mismatch at argument %d in "
8383 "template parameter list for %qD",
8384 i + 1, in_decl);
8385 inform (input_location, " expected %qD but got %qD",
8386 parm, arg);
8387 }
8388 val = error_mark_node;
8389 }
8390 }
8391 }
8392 else
8393 val = orig_arg;
8394 /* We only form one instance of each template specialization.
8395 Therefore, if we use a non-canonical variant (i.e., a
8396 typedef), any future messages referring to the type will use
8397 the typedef, which is confusing if those future uses do not
8398 themselves also use the typedef. */
8399 if (TYPE_P (val))
8400 val = canonicalize_type_argument (val, complain);
8401 }
8402 else
8403 {
8404 tree t = TREE_TYPE (parm);
8405
8406 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8407 > TMPL_ARGS_DEPTH (args))
8408 /* We don't have enough levels of args to do any substitution. This
8409 can happen in the context of -fnew-ttp-matching. */;
8410 else if (tree a = type_uses_auto (t))
8411 {
8412 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8413 if (t == error_mark_node)
8414 return error_mark_node;
8415 }
8416 else
8417 t = tsubst (t, args, complain, in_decl);
8418
8419 if (invalid_nontype_parm_type_p (t, complain))
8420 return error_mark_node;
8421
8422 if (t != TREE_TYPE (parm))
8423 t = canonicalize_type_argument (t, complain);
8424
8425 if (!type_dependent_expression_p (orig_arg)
8426 && !uses_template_parms (t))
8427 /* We used to call digest_init here. However, digest_init
8428 will report errors, which we don't want when complain
8429 is zero. More importantly, digest_init will try too
8430 hard to convert things: for example, `0' should not be
8431 converted to pointer type at this point according to
8432 the standard. Accepting this is not merely an
8433 extension, since deciding whether or not these
8434 conversions can occur is part of determining which
8435 function template to call, or whether a given explicit
8436 argument specification is valid. */
8437 val = convert_nontype_argument (t, orig_arg, complain);
8438 else
8439 {
8440 val = canonicalize_expr_argument (orig_arg, complain);
8441 val = maybe_convert_nontype_argument (t, val);
8442 }
8443
8444
8445 if (val == NULL_TREE)
8446 val = error_mark_node;
8447 else if (val == error_mark_node && (complain & tf_error))
8448 error_at (cp_expr_loc_or_input_loc (orig_arg),
8449 "could not convert template argument %qE from %qT to %qT",
8450 orig_arg, TREE_TYPE (orig_arg), t);
8451
8452 if (INDIRECT_REF_P (val))
8453 {
8454 /* Reject template arguments that are references to built-in
8455 functions with no library fallbacks. */
8456 const_tree inner = TREE_OPERAND (val, 0);
8457 const_tree innertype = TREE_TYPE (inner);
8458 if (innertype
8459 && TYPE_REF_P (innertype)
8460 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8461 && TREE_OPERAND_LENGTH (inner) > 0
8462 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8463 return error_mark_node;
8464 }
8465
8466 if (TREE_CODE (val) == SCOPE_REF)
8467 {
8468 /* Strip typedefs from the SCOPE_REF. */
8469 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8470 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8471 complain);
8472 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8473 QUALIFIED_NAME_IS_TEMPLATE (val));
8474 }
8475 }
8476
8477 return val;
8478 }
8479
8480 /* Coerces the remaining template arguments in INNER_ARGS (from
8481 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8482 Returns the coerced argument pack. PARM_IDX is the position of this
8483 parameter in the template parameter list. ARGS is the original
8484 template argument list. */
8485 static tree
8486 coerce_template_parameter_pack (tree parms,
8487 int parm_idx,
8488 tree args,
8489 tree inner_args,
8490 int arg_idx,
8491 tree new_args,
8492 int* lost,
8493 tree in_decl,
8494 tsubst_flags_t complain)
8495 {
8496 tree parm = TREE_VEC_ELT (parms, parm_idx);
8497 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8498 tree packed_args;
8499 tree argument_pack;
8500 tree packed_parms = NULL_TREE;
8501
8502 if (arg_idx > nargs)
8503 arg_idx = nargs;
8504
8505 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8506 {
8507 /* When the template parameter is a non-type template parameter pack
8508 or template template parameter pack whose type or template
8509 parameters use parameter packs, we know exactly how many arguments
8510 we are looking for. Build a vector of the instantiated decls for
8511 these template parameters in PACKED_PARMS. */
8512 /* We can't use make_pack_expansion here because it would interpret a
8513 _DECL as a use rather than a declaration. */
8514 tree decl = TREE_VALUE (parm);
8515 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8516 SET_PACK_EXPANSION_PATTERN (exp, decl);
8517 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8518 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8519
8520 TREE_VEC_LENGTH (args)--;
8521 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8522 TREE_VEC_LENGTH (args)++;
8523
8524 if (packed_parms == error_mark_node)
8525 return error_mark_node;
8526
8527 /* If we're doing a partial instantiation of a member template,
8528 verify that all of the types used for the non-type
8529 template parameter pack are, in fact, valid for non-type
8530 template parameters. */
8531 if (arg_idx < nargs
8532 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8533 {
8534 int j, len = TREE_VEC_LENGTH (packed_parms);
8535 for (j = 0; j < len; ++j)
8536 {
8537 tree t = TREE_VEC_ELT (packed_parms, j);
8538 if (TREE_CODE (t) == PARM_DECL
8539 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8540 return error_mark_node;
8541 }
8542 /* We don't know how many args we have yet, just
8543 use the unconverted ones for now. */
8544 return NULL_TREE;
8545 }
8546
8547 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8548 }
8549 /* Check if we have a placeholder pack, which indicates we're
8550 in the context of a introduction list. In that case we want
8551 to match this pack to the single placeholder. */
8552 else if (arg_idx < nargs
8553 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8554 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8555 {
8556 nargs = arg_idx + 1;
8557 packed_args = make_tree_vec (1);
8558 }
8559 else
8560 packed_args = make_tree_vec (nargs - arg_idx);
8561
8562 /* Convert the remaining arguments, which will be a part of the
8563 parameter pack "parm". */
8564 int first_pack_arg = arg_idx;
8565 for (; arg_idx < nargs; ++arg_idx)
8566 {
8567 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8568 tree actual_parm = TREE_VALUE (parm);
8569 int pack_idx = arg_idx - first_pack_arg;
8570
8571 if (packed_parms)
8572 {
8573 /* Once we've packed as many args as we have types, stop. */
8574 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8575 break;
8576 else if (PACK_EXPANSION_P (arg))
8577 /* We don't know how many args we have yet, just
8578 use the unconverted ones for now. */
8579 return NULL_TREE;
8580 else
8581 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8582 }
8583
8584 if (arg == error_mark_node)
8585 {
8586 if (complain & tf_error)
8587 error ("template argument %d is invalid", arg_idx + 1);
8588 }
8589 else
8590 arg = convert_template_argument (actual_parm,
8591 arg, new_args, complain, parm_idx,
8592 in_decl);
8593 if (arg == error_mark_node)
8594 (*lost)++;
8595 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8596 }
8597
8598 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8599 && TREE_VEC_LENGTH (packed_args) > 0)
8600 {
8601 if (complain & tf_error)
8602 error ("wrong number of template arguments (%d, should be %d)",
8603 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8604 return error_mark_node;
8605 }
8606
8607 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8608 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8609 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8610 else
8611 {
8612 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8613 TREE_CONSTANT (argument_pack) = 1;
8614 }
8615
8616 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8617 if (CHECKING_P)
8618 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8619 TREE_VEC_LENGTH (packed_args));
8620 return argument_pack;
8621 }
8622
8623 /* Returns the number of pack expansions in the template argument vector
8624 ARGS. */
8625
8626 static int
8627 pack_expansion_args_count (tree args)
8628 {
8629 int i;
8630 int count = 0;
8631 if (args)
8632 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8633 {
8634 tree elt = TREE_VEC_ELT (args, i);
8635 if (elt && PACK_EXPANSION_P (elt))
8636 ++count;
8637 }
8638 return count;
8639 }
8640
8641 /* Convert all template arguments to their appropriate types, and
8642 return a vector containing the innermost resulting template
8643 arguments. If any error occurs, return error_mark_node. Error and
8644 warning messages are issued under control of COMPLAIN.
8645
8646 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8647 for arguments not specified in ARGS. Otherwise, if
8648 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8649 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8650 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8651 ARGS. */
8652
8653 static tree
8654 coerce_template_parms (tree parms,
8655 tree args,
8656 tree in_decl,
8657 tsubst_flags_t complain,
8658 bool require_all_args,
8659 bool use_default_args)
8660 {
8661 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8662 tree orig_inner_args;
8663 tree inner_args;
8664 tree new_args;
8665 tree new_inner_args;
8666
8667 /* When used as a boolean value, indicates whether this is a
8668 variadic template parameter list. Since it's an int, we can also
8669 subtract it from nparms to get the number of non-variadic
8670 parameters. */
8671 int variadic_p = 0;
8672 int variadic_args_p = 0;
8673 int post_variadic_parms = 0;
8674
8675 /* Adjustment to nparms for fixed parameter packs. */
8676 int fixed_pack_adjust = 0;
8677 int fixed_packs = 0;
8678 int missing = 0;
8679
8680 /* Likewise for parameters with default arguments. */
8681 int default_p = 0;
8682
8683 if (args == error_mark_node)
8684 return error_mark_node;
8685
8686 nparms = TREE_VEC_LENGTH (parms);
8687
8688 /* Determine if there are any parameter packs or default arguments. */
8689 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8690 {
8691 tree parm = TREE_VEC_ELT (parms, parm_idx);
8692 if (variadic_p)
8693 ++post_variadic_parms;
8694 if (template_parameter_pack_p (TREE_VALUE (parm)))
8695 ++variadic_p;
8696 if (TREE_PURPOSE (parm))
8697 ++default_p;
8698 }
8699
8700 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8701 /* If there are no parameters that follow a parameter pack, we need to
8702 expand any argument packs so that we can deduce a parameter pack from
8703 some non-packed args followed by an argument pack, as in variadic85.C.
8704 If there are such parameters, we need to leave argument packs intact
8705 so the arguments are assigned properly. This can happen when dealing
8706 with a nested class inside a partial specialization of a class
8707 template, as in variadic92.C, or when deducing a template parameter pack
8708 from a sub-declarator, as in variadic114.C. */
8709 if (!post_variadic_parms)
8710 inner_args = expand_template_argument_pack (inner_args);
8711
8712 /* Count any pack expansion args. */
8713 variadic_args_p = pack_expansion_args_count (inner_args);
8714
8715 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8716 if ((nargs - variadic_args_p > nparms && !variadic_p)
8717 || (nargs < nparms - variadic_p
8718 && require_all_args
8719 && !variadic_args_p
8720 && (!use_default_args
8721 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8722 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8723 {
8724 bad_nargs:
8725 if (complain & tf_error)
8726 {
8727 if (variadic_p || default_p)
8728 {
8729 nparms -= variadic_p + default_p;
8730 error ("wrong number of template arguments "
8731 "(%d, should be at least %d)", nargs, nparms);
8732 }
8733 else
8734 error ("wrong number of template arguments "
8735 "(%d, should be %d)", nargs, nparms);
8736
8737 if (in_decl)
8738 inform (DECL_SOURCE_LOCATION (in_decl),
8739 "provided for %qD", in_decl);
8740 }
8741
8742 return error_mark_node;
8743 }
8744 /* We can't pass a pack expansion to a non-pack parameter of an alias
8745 template (DR 1430). */
8746 else if (in_decl
8747 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8748 || concept_definition_p (in_decl))
8749 && variadic_args_p
8750 && nargs - variadic_args_p < nparms - variadic_p)
8751 {
8752 if (complain & tf_error)
8753 {
8754 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8755 {
8756 tree arg = TREE_VEC_ELT (inner_args, i);
8757 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8758
8759 if (PACK_EXPANSION_P (arg)
8760 && !template_parameter_pack_p (parm))
8761 {
8762 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8763 error_at (location_of (arg),
8764 "pack expansion argument for non-pack parameter "
8765 "%qD of alias template %qD", parm, in_decl);
8766 else
8767 error_at (location_of (arg),
8768 "pack expansion argument for non-pack parameter "
8769 "%qD of concept %qD", parm, in_decl);
8770 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8771 goto found;
8772 }
8773 }
8774 gcc_unreachable ();
8775 found:;
8776 }
8777 return error_mark_node;
8778 }
8779
8780 /* We need to evaluate the template arguments, even though this
8781 template-id may be nested within a "sizeof". */
8782 cp_evaluated ev;
8783
8784 new_inner_args = make_tree_vec (nparms);
8785 new_args = add_outermost_template_args (args, new_inner_args);
8786 int pack_adjust = 0;
8787 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8788 {
8789 tree arg;
8790 tree parm;
8791
8792 /* Get the Ith template parameter. */
8793 parm = TREE_VEC_ELT (parms, parm_idx);
8794
8795 if (parm == error_mark_node)
8796 {
8797 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8798 continue;
8799 }
8800
8801 /* Calculate the next argument. */
8802 if (arg_idx < nargs)
8803 arg = TREE_VEC_ELT (inner_args, arg_idx);
8804 else
8805 arg = NULL_TREE;
8806
8807 if (template_parameter_pack_p (TREE_VALUE (parm))
8808 && (arg || require_all_args || !(complain & tf_partial))
8809 && !(arg && ARGUMENT_PACK_P (arg)))
8810 {
8811 /* Some arguments will be placed in the
8812 template parameter pack PARM. */
8813 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8814 inner_args, arg_idx,
8815 new_args, &lost,
8816 in_decl, complain);
8817
8818 if (arg == NULL_TREE)
8819 {
8820 /* We don't know how many args we have yet, just use the
8821 unconverted (and still packed) ones for now. */
8822 new_inner_args = orig_inner_args;
8823 arg_idx = nargs;
8824 break;
8825 }
8826
8827 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8828
8829 /* Store this argument. */
8830 if (arg == error_mark_node)
8831 {
8832 lost++;
8833 /* We are done with all of the arguments. */
8834 arg_idx = nargs;
8835 break;
8836 }
8837 else
8838 {
8839 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8840 arg_idx += pack_adjust;
8841 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8842 {
8843 ++fixed_packs;
8844 fixed_pack_adjust += pack_adjust;
8845 }
8846 }
8847
8848 continue;
8849 }
8850 else if (arg)
8851 {
8852 if (PACK_EXPANSION_P (arg))
8853 {
8854 /* "If every valid specialization of a variadic template
8855 requires an empty template parameter pack, the template is
8856 ill-formed, no diagnostic required." So check that the
8857 pattern works with this parameter. */
8858 tree pattern = PACK_EXPANSION_PATTERN (arg);
8859 tree conv = convert_template_argument (TREE_VALUE (parm),
8860 pattern, new_args,
8861 complain, parm_idx,
8862 in_decl);
8863 if (conv == error_mark_node)
8864 {
8865 if (complain & tf_error)
8866 inform (input_location, "so any instantiation with a "
8867 "non-empty parameter pack would be ill-formed");
8868 ++lost;
8869 }
8870 else if (TYPE_P (conv) && !TYPE_P (pattern))
8871 /* Recover from missing typename. */
8872 TREE_VEC_ELT (inner_args, arg_idx)
8873 = make_pack_expansion (conv, complain);
8874
8875 /* We don't know how many args we have yet, just
8876 use the unconverted ones for now. */
8877 new_inner_args = inner_args;
8878 arg_idx = nargs;
8879 break;
8880 }
8881 }
8882 else if (require_all_args)
8883 {
8884 /* There must be a default arg in this case. */
8885 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8886 complain, in_decl);
8887 /* The position of the first default template argument,
8888 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8889 Record that. */
8890 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8891 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8892 arg_idx - pack_adjust);
8893 }
8894 else
8895 break;
8896
8897 if (arg == error_mark_node)
8898 {
8899 if (complain & tf_error)
8900 error ("template argument %d is invalid", arg_idx + 1);
8901 }
8902 else if (!arg)
8903 {
8904 /* This can occur if there was an error in the template
8905 parameter list itself (which we would already have
8906 reported) that we are trying to recover from, e.g., a class
8907 template with a parameter list such as
8908 template<typename..., typename> (cpp0x/variadic150.C). */
8909 ++lost;
8910
8911 /* This can also happen with a fixed parameter pack (71834). */
8912 if (arg_idx >= nargs)
8913 ++missing;
8914 }
8915 else
8916 arg = convert_template_argument (TREE_VALUE (parm),
8917 arg, new_args, complain,
8918 parm_idx, in_decl);
8919
8920 if (arg == error_mark_node)
8921 lost++;
8922
8923 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8924 }
8925
8926 if (missing || arg_idx < nargs - variadic_args_p)
8927 {
8928 /* If we had fixed parameter packs, we didn't know how many arguments we
8929 actually needed earlier; now we do. */
8930 nparms += fixed_pack_adjust;
8931 variadic_p -= fixed_packs;
8932 goto bad_nargs;
8933 }
8934
8935 if (arg_idx < nargs)
8936 {
8937 /* We had some pack expansion arguments that will only work if the packs
8938 are empty, but wait until instantiation time to complain.
8939 See variadic-ttp3.C. */
8940
8941 /* Except that we can't provide empty packs to alias templates or
8942 concepts when there are no corresponding parameters. Basically,
8943 we can get here with this:
8944
8945 template<typename T> concept C = true;
8946
8947 template<typename... Args>
8948 requires C<Args...>
8949 void f();
8950
8951 When parsing C<Args...>, we try to form a concept check of
8952 C<?, Args...>. Without the extra check for substituting an empty
8953 pack past the last parameter, we can accept the check as valid.
8954
8955 FIXME: This may be valid for alias templates (but I doubt it).
8956
8957 FIXME: The error could be better also. */
8958 if (in_decl && concept_definition_p (in_decl))
8959 {
8960 if (complain & tf_error)
8961 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8962 "too many arguments");
8963 return error_mark_node;
8964 }
8965
8966 int len = nparms + (nargs - arg_idx);
8967 tree args = make_tree_vec (len);
8968 int i = 0;
8969 for (; i < nparms; ++i)
8970 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8971 for (; i < len; ++i, ++arg_idx)
8972 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8973 arg_idx - pack_adjust);
8974 new_inner_args = args;
8975 }
8976
8977 if (lost)
8978 {
8979 gcc_assert (!(complain & tf_error) || seen_error ());
8980 return error_mark_node;
8981 }
8982
8983 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8985 TREE_VEC_LENGTH (new_inner_args));
8986
8987 return new_inner_args;
8988 }
8989
8990 /* Convert all template arguments to their appropriate types, and
8991 return a vector containing the innermost resulting template
8992 arguments. If any error occurs, return error_mark_node. Error and
8993 warning messages are not issued.
8994
8995 Note that no function argument deduction is performed, and default
8996 arguments are used to fill in unspecified arguments. */
8997 tree
8998 coerce_template_parms (tree parms, tree args, tree in_decl)
8999 {
9000 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9001 }
9002
9003 /* Convert all template arguments to their appropriate type, and
9004 instantiate default arguments as needed. This returns a vector
9005 containing the innermost resulting template arguments, or
9006 error_mark_node if unsuccessful. */
9007 tree
9008 coerce_template_parms (tree parms, tree args, tree in_decl,
9009 tsubst_flags_t complain)
9010 {
9011 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9012 }
9013
9014 /* Like coerce_template_parms. If PARMS represents all template
9015 parameters levels, this function returns a vector of vectors
9016 representing all the resulting argument levels. Note that in this
9017 case, only the innermost arguments are coerced because the
9018 outermost ones are supposed to have been coerced already.
9019
9020 Otherwise, if PARMS represents only (the innermost) vector of
9021 parameters, this function returns a vector containing just the
9022 innermost resulting arguments. */
9023
9024 static tree
9025 coerce_innermost_template_parms (tree parms,
9026 tree args,
9027 tree in_decl,
9028 tsubst_flags_t complain,
9029 bool require_all_args,
9030 bool use_default_args)
9031 {
9032 int parms_depth = TMPL_PARMS_DEPTH (parms);
9033 int args_depth = TMPL_ARGS_DEPTH (args);
9034 tree coerced_args;
9035
9036 if (parms_depth > 1)
9037 {
9038 coerced_args = make_tree_vec (parms_depth);
9039 tree level;
9040 int cur_depth;
9041
9042 for (level = parms, cur_depth = parms_depth;
9043 parms_depth > 0 && level != NULL_TREE;
9044 level = TREE_CHAIN (level), --cur_depth)
9045 {
9046 tree l;
9047 if (cur_depth == args_depth)
9048 l = coerce_template_parms (TREE_VALUE (level),
9049 args, in_decl, complain,
9050 require_all_args,
9051 use_default_args);
9052 else
9053 l = TMPL_ARGS_LEVEL (args, cur_depth);
9054
9055 if (l == error_mark_node)
9056 return error_mark_node;
9057
9058 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9059 }
9060 }
9061 else
9062 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9063 args, in_decl, complain,
9064 require_all_args,
9065 use_default_args);
9066 return coerced_args;
9067 }
9068
9069 /* Returns true if T is a wrapper to make a C++20 template parameter
9070 object const. */
9071
9072 static bool
9073 class_nttp_const_wrapper_p (tree t)
9074 {
9075 if (cxx_dialect < cxx20)
9076 return false;
9077 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9078 && CP_TYPE_CONST_P (TREE_TYPE (t))
9079 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9080 }
9081
9082 /* Returns 1 if template args OT and NT are equivalent. */
9083
9084 int
9085 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9086 {
9087 if (nt == ot)
9088 return 1;
9089 if (nt == NULL_TREE || ot == NULL_TREE)
9090 return false;
9091 if (nt == any_targ_node || ot == any_targ_node)
9092 return true;
9093
9094 if (class_nttp_const_wrapper_p (nt))
9095 nt = TREE_OPERAND (nt, 0);
9096 if (class_nttp_const_wrapper_p (ot))
9097 ot = TREE_OPERAND (ot, 0);
9098
9099 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9100 /* For member templates */
9101 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9102 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9103 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9104 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9105 PACK_EXPANSION_PATTERN (nt))
9106 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9107 PACK_EXPANSION_EXTRA_ARGS (nt)));
9108 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9109 return cp_tree_equal (ot, nt);
9110 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9111 gcc_unreachable ();
9112 else if (TYPE_P (nt) || TYPE_P (ot))
9113 {
9114 if (!(TYPE_P (nt) && TYPE_P (ot)))
9115 return false;
9116 /* Don't treat an alias template specialization with dependent
9117 arguments as equivalent to its underlying type when used as a
9118 template argument; we need them to be distinct so that we
9119 substitute into the specialization arguments at instantiation
9120 time. And aliases can't be equivalent without being ==, so
9121 we don't need to look any deeper.
9122
9123 During partial ordering, however, we need to treat them normally so
9124 that we can order uses of the same alias with different
9125 cv-qualification (79960). */
9126 if (!partial_order
9127 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9128 return false;
9129 else
9130 return same_type_p (ot, nt);
9131 }
9132 else
9133 {
9134 /* Try to treat a template non-type argument that has been converted
9135 to the parameter type as equivalent to one that hasn't yet. */
9136 for (enum tree_code code1 = TREE_CODE (ot);
9137 CONVERT_EXPR_CODE_P (code1)
9138 || code1 == NON_LVALUE_EXPR;
9139 code1 = TREE_CODE (ot))
9140 ot = TREE_OPERAND (ot, 0);
9141
9142 for (enum tree_code code2 = TREE_CODE (nt);
9143 CONVERT_EXPR_CODE_P (code2)
9144 || code2 == NON_LVALUE_EXPR;
9145 code2 = TREE_CODE (nt))
9146 nt = TREE_OPERAND (nt, 0);
9147
9148 return cp_tree_equal (ot, nt);
9149 }
9150 }
9151
9152 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9153 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9154 NEWARG_PTR with the offending arguments if they are non-NULL. */
9155
9156 int
9157 comp_template_args (tree oldargs, tree newargs,
9158 tree *oldarg_ptr, tree *newarg_ptr,
9159 bool partial_order)
9160 {
9161 int i;
9162
9163 if (oldargs == newargs)
9164 return 1;
9165
9166 if (!oldargs || !newargs)
9167 return 0;
9168
9169 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9170 return 0;
9171
9172 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9173 {
9174 tree nt = TREE_VEC_ELT (newargs, i);
9175 tree ot = TREE_VEC_ELT (oldargs, i);
9176
9177 if (! template_args_equal (ot, nt, partial_order))
9178 {
9179 if (oldarg_ptr != NULL)
9180 *oldarg_ptr = ot;
9181 if (newarg_ptr != NULL)
9182 *newarg_ptr = nt;
9183 return 0;
9184 }
9185 }
9186 return 1;
9187 }
9188
9189 inline bool
9190 comp_template_args_porder (tree oargs, tree nargs)
9191 {
9192 return comp_template_args (oargs, nargs, NULL, NULL, true);
9193 }
9194
9195 /* Implement a freelist interface for objects of type T.
9196
9197 Head is a separate object, rather than a regular member, so that we
9198 can define it as a GTY deletable pointer, which is highly
9199 desirable. A data member could be declared that way, but then the
9200 containing object would implicitly get GTY((user)), which would
9201 prevent us from instantiating freelists as global objects.
9202 Although this way we can create freelist global objects, they're
9203 such thin wrappers that instantiating temporaries at every use
9204 loses nothing and saves permanent storage for the freelist object.
9205
9206 Member functions next, anew, poison and reinit have default
9207 implementations that work for most of the types we're interested
9208 in, but if they don't work for some type, they should be explicitly
9209 specialized. See the comments before them for requirements, and
9210 the example specializations for the tree_list_freelist. */
9211 template <typename T>
9212 class freelist
9213 {
9214 /* Return the next object in a chain. We could just do type
9215 punning, but if we access the object with its underlying type, we
9216 avoid strict-aliasing trouble. This needs only work between
9217 poison and reinit. */
9218 static T *&next (T *obj) { return obj->next; }
9219
9220 /* Return a newly allocated, uninitialized or minimally-initialized
9221 object of type T. Any initialization performed by anew should
9222 either remain across the life of the object and the execution of
9223 poison, or be redone by reinit. */
9224 static T *anew () { return ggc_alloc<T> (); }
9225
9226 /* Optionally scribble all over the bits holding the object, so that
9227 they become (mostly?) uninitialized memory. This is called while
9228 preparing to make the object part of the free list. */
9229 static void poison (T *obj) {
9230 T *p ATTRIBUTE_UNUSED = obj;
9231 T **q ATTRIBUTE_UNUSED = &next (obj);
9232
9233 #ifdef ENABLE_GC_CHECKING
9234 /* Poison the data, to indicate the data is garbage. */
9235 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9236 memset (p, 0xa5, sizeof (*p));
9237 #endif
9238 /* Let valgrind know the object is free. */
9239 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9240
9241 /* Let valgrind know the next portion of the object is available,
9242 but uninitialized. */
9243 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9244 }
9245
9246 /* Bring an object that underwent at least one lifecycle after anew
9247 and before the most recent free and poison, back to a usable
9248 state, reinitializing whatever is needed for it to be
9249 functionally equivalent to an object just allocated and returned
9250 by anew. This may poison or clear the next field, used by
9251 freelist housekeeping after poison was called. */
9252 static void reinit (T *obj) {
9253 T **q ATTRIBUTE_UNUSED = &next (obj);
9254
9255 #ifdef ENABLE_GC_CHECKING
9256 memset (q, 0xa5, sizeof (*q));
9257 #endif
9258 /* Let valgrind know the entire object is available, but
9259 uninitialized. */
9260 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9261 }
9262
9263 /* Reference a GTY-deletable pointer that points to the first object
9264 in the free list proper. */
9265 T *&head;
9266 public:
9267 /* Construct a freelist object chaining objects off of HEAD. */
9268 freelist (T *&head) : head(head) {}
9269
9270 /* Add OBJ to the free object list. The former head becomes OBJ's
9271 successor. */
9272 void free (T *obj)
9273 {
9274 poison (obj);
9275 next (obj) = head;
9276 head = obj;
9277 }
9278
9279 /* Take an object from the free list, if one is available, or
9280 allocate a new one. Objects taken from the free list should be
9281 regarded as filled with garbage, except for bits that are
9282 configured to be preserved across free and alloc. */
9283 T *alloc ()
9284 {
9285 if (head)
9286 {
9287 T *obj = head;
9288 head = next (head);
9289 reinit (obj);
9290 return obj;
9291 }
9292 else
9293 return anew ();
9294 }
9295 };
9296
9297 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9298 want to allocate a TREE_LIST using the usual interface, and ensure
9299 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9300 build_tree_list logic in reinit, so this could go out of sync. */
9301 template <>
9302 inline tree &
9303 freelist<tree_node>::next (tree obj)
9304 {
9305 return TREE_CHAIN (obj);
9306 }
9307 template <>
9308 inline tree
9309 freelist<tree_node>::anew ()
9310 {
9311 return build_tree_list (NULL, NULL);
9312 }
9313 template <>
9314 inline void
9315 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9316 {
9317 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9318 tree p ATTRIBUTE_UNUSED = obj;
9319 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9320 tree *q ATTRIBUTE_UNUSED = &next (obj);
9321
9322 #ifdef ENABLE_GC_CHECKING
9323 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9324
9325 /* Poison the data, to indicate the data is garbage. */
9326 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9327 memset (p, 0xa5, size);
9328 #endif
9329 /* Let valgrind know the object is free. */
9330 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9331 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9332 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9333 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9334
9335 #ifdef ENABLE_GC_CHECKING
9336 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9337 /* Keep TREE_CHAIN functional. */
9338 TREE_SET_CODE (obj, TREE_LIST);
9339 #else
9340 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9341 #endif
9342 }
9343 template <>
9344 inline void
9345 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9346 {
9347 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9348
9349 #ifdef ENABLE_GC_CHECKING
9350 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9351 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9352 memset (obj, 0, sizeof (tree_list));
9353 #endif
9354
9355 /* Let valgrind know the entire object is available, but
9356 uninitialized. */
9357 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9358
9359 #ifdef ENABLE_GC_CHECKING
9360 TREE_SET_CODE (obj, TREE_LIST);
9361 #else
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9363 #endif
9364 }
9365
9366 /* Point to the first object in the TREE_LIST freelist. */
9367 static GTY((deletable)) tree tree_list_freelist_head;
9368 /* Return the/an actual TREE_LIST freelist. */
9369 static inline freelist<tree_node>
9370 tree_list_freelist ()
9371 {
9372 return tree_list_freelist_head;
9373 }
9374
9375 /* Point to the first object in the tinst_level freelist. */
9376 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9377 /* Return the/an actual tinst_level freelist. */
9378 static inline freelist<tinst_level>
9379 tinst_level_freelist ()
9380 {
9381 return tinst_level_freelist_head;
9382 }
9383
9384 /* Point to the first object in the pending_template freelist. */
9385 static GTY((deletable)) pending_template *pending_template_freelist_head;
9386 /* Return the/an actual pending_template freelist. */
9387 static inline freelist<pending_template>
9388 pending_template_freelist ()
9389 {
9390 return pending_template_freelist_head;
9391 }
9392
9393 /* Build the TREE_LIST object out of a split list, store it
9394 permanently, and return it. */
9395 tree
9396 tinst_level::to_list ()
9397 {
9398 gcc_assert (split_list_p ());
9399 tree ret = tree_list_freelist ().alloc ();
9400 TREE_PURPOSE (ret) = tldcl;
9401 TREE_VALUE (ret) = targs;
9402 tldcl = ret;
9403 targs = NULL;
9404 gcc_assert (tree_list_p ());
9405 return ret;
9406 }
9407
9408 const unsigned short tinst_level::refcount_infinity;
9409
9410 /* Increment OBJ's refcount unless it is already infinite. */
9411 static tinst_level *
9412 inc_refcount_use (tinst_level *obj)
9413 {
9414 if (obj && obj->refcount != tinst_level::refcount_infinity)
9415 ++obj->refcount;
9416 return obj;
9417 }
9418
9419 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9420 void
9421 tinst_level::free (tinst_level *obj)
9422 {
9423 if (obj->tree_list_p ())
9424 tree_list_freelist ().free (obj->get_node ());
9425 tinst_level_freelist ().free (obj);
9426 }
9427
9428 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9429 OBJ's DECL and OBJ, and start over with the tinst_level object that
9430 used to be referenced by OBJ's NEXT. */
9431 static void
9432 dec_refcount_use (tinst_level *obj)
9433 {
9434 while (obj
9435 && obj->refcount != tinst_level::refcount_infinity
9436 && !--obj->refcount)
9437 {
9438 tinst_level *next = obj->next;
9439 tinst_level::free (obj);
9440 obj = next;
9441 }
9442 }
9443
9444 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9445 and of the former PTR. Omitting the second argument is equivalent
9446 to passing (T*)NULL; this is allowed because passing the
9447 zero-valued integral constant NULL confuses type deduction and/or
9448 overload resolution. */
9449 template <typename T>
9450 static void
9451 set_refcount_ptr (T *& ptr, T *obj = NULL)
9452 {
9453 T *save = ptr;
9454 ptr = inc_refcount_use (obj);
9455 dec_refcount_use (save);
9456 }
9457
9458 static void
9459 add_pending_template (tree d)
9460 {
9461 tree ti = (TYPE_P (d)
9462 ? CLASSTYPE_TEMPLATE_INFO (d)
9463 : DECL_TEMPLATE_INFO (d));
9464 struct pending_template *pt;
9465 int level;
9466
9467 if (TI_PENDING_TEMPLATE_FLAG (ti))
9468 return;
9469
9470 /* We are called both from instantiate_decl, where we've already had a
9471 tinst_level pushed, and instantiate_template, where we haven't.
9472 Compensate. */
9473 gcc_assert (TREE_CODE (d) != TREE_LIST);
9474 level = !current_tinst_level
9475 || current_tinst_level->maybe_get_node () != d;
9476
9477 if (level)
9478 push_tinst_level (d);
9479
9480 pt = pending_template_freelist ().alloc ();
9481 pt->next = NULL;
9482 pt->tinst = NULL;
9483 set_refcount_ptr (pt->tinst, current_tinst_level);
9484 if (last_pending_template)
9485 last_pending_template->next = pt;
9486 else
9487 pending_templates = pt;
9488
9489 last_pending_template = pt;
9490
9491 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9492
9493 if (level)
9494 pop_tinst_level ();
9495 }
9496
9497
9498 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9499 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9500 documentation for TEMPLATE_ID_EXPR. */
9501
9502 tree
9503 lookup_template_function (tree fns, tree arglist)
9504 {
9505 if (fns == error_mark_node || arglist == error_mark_node)
9506 return error_mark_node;
9507
9508 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9509
9510 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9511 {
9512 error ("%q#D is not a function template", fns);
9513 return error_mark_node;
9514 }
9515
9516 if (BASELINK_P (fns))
9517 {
9518 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9519 unknown_type_node,
9520 BASELINK_FUNCTIONS (fns),
9521 arglist);
9522 return fns;
9523 }
9524
9525 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9526 }
9527
9528 /* Within the scope of a template class S<T>, the name S gets bound
9529 (in build_self_reference) to a TYPE_DECL for the class, not a
9530 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9531 or one of its enclosing classes, and that type is a template,
9532 return the associated TEMPLATE_DECL. Otherwise, the original
9533 DECL is returned.
9534
9535 Also handle the case when DECL is a TREE_LIST of ambiguous
9536 injected-class-names from different bases. */
9537
9538 tree
9539 maybe_get_template_decl_from_type_decl (tree decl)
9540 {
9541 if (decl == NULL_TREE)
9542 return decl;
9543
9544 /* DR 176: A lookup that finds an injected-class-name (10.2
9545 [class.member.lookup]) can result in an ambiguity in certain cases
9546 (for example, if it is found in more than one base class). If all of
9547 the injected-class-names that are found refer to specializations of
9548 the same class template, and if the name is followed by a
9549 template-argument-list, the reference refers to the class template
9550 itself and not a specialization thereof, and is not ambiguous. */
9551 if (TREE_CODE (decl) == TREE_LIST)
9552 {
9553 tree t, tmpl = NULL_TREE;
9554 for (t = decl; t; t = TREE_CHAIN (t))
9555 {
9556 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9557 if (!tmpl)
9558 tmpl = elt;
9559 else if (tmpl != elt)
9560 break;
9561 }
9562 if (tmpl && t == NULL_TREE)
9563 return tmpl;
9564 else
9565 return decl;
9566 }
9567
9568 return (decl != NULL_TREE
9569 && DECL_SELF_REFERENCE_P (decl)
9570 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9571 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9572 }
9573
9574 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9575 parameters, find the desired type.
9576
9577 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9578
9579 IN_DECL, if non-NULL, is the template declaration we are trying to
9580 instantiate.
9581
9582 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9583 the class we are looking up.
9584
9585 Issue error and warning messages under control of COMPLAIN.
9586
9587 If the template class is really a local class in a template
9588 function, then the FUNCTION_CONTEXT is the function in which it is
9589 being instantiated.
9590
9591 ??? Note that this function is currently called *twice* for each
9592 template-id: the first time from the parser, while creating the
9593 incomplete type (finish_template_type), and the second type during the
9594 real instantiation (instantiate_template_class). This is surely something
9595 that we want to avoid. It also causes some problems with argument
9596 coercion (see convert_nontype_argument for more information on this). */
9597
9598 static tree
9599 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9600 int entering_scope, tsubst_flags_t complain)
9601 {
9602 tree templ = NULL_TREE, parmlist;
9603 tree t;
9604 spec_entry **slot;
9605 spec_entry *entry;
9606 spec_entry elt;
9607 hashval_t hash;
9608
9609 if (identifier_p (d1))
9610 {
9611 tree value = innermost_non_namespace_value (d1);
9612 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9613 templ = value;
9614 else
9615 {
9616 if (context)
9617 push_decl_namespace (context);
9618 templ = lookup_name (d1);
9619 templ = maybe_get_template_decl_from_type_decl (templ);
9620 if (context)
9621 pop_decl_namespace ();
9622 }
9623 if (templ)
9624 context = DECL_CONTEXT (templ);
9625 }
9626 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9627 {
9628 tree type = TREE_TYPE (d1);
9629
9630 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9631 an implicit typename for the second A. Deal with it. */
9632 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9633 type = TREE_TYPE (type);
9634
9635 if (CLASSTYPE_TEMPLATE_INFO (type))
9636 {
9637 templ = CLASSTYPE_TI_TEMPLATE (type);
9638 d1 = DECL_NAME (templ);
9639 }
9640 }
9641 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9642 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9643 {
9644 templ = TYPE_TI_TEMPLATE (d1);
9645 d1 = DECL_NAME (templ);
9646 }
9647 else if (DECL_TYPE_TEMPLATE_P (d1))
9648 {
9649 templ = d1;
9650 d1 = DECL_NAME (templ);
9651 context = DECL_CONTEXT (templ);
9652 }
9653 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9654 {
9655 templ = d1;
9656 d1 = DECL_NAME (templ);
9657 }
9658
9659 /* Issue an error message if we didn't find a template. */
9660 if (! templ)
9661 {
9662 if (complain & tf_error)
9663 error ("%qT is not a template", d1);
9664 return error_mark_node;
9665 }
9666
9667 if (TREE_CODE (templ) != TEMPLATE_DECL
9668 /* Make sure it's a user visible template, if it was named by
9669 the user. */
9670 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9671 && !PRIMARY_TEMPLATE_P (templ)))
9672 {
9673 if (complain & tf_error)
9674 {
9675 error ("non-template type %qT used as a template", d1);
9676 if (in_decl)
9677 error ("for template declaration %q+D", in_decl);
9678 }
9679 return error_mark_node;
9680 }
9681
9682 complain &= ~tf_user;
9683
9684 /* An alias that just changes the name of a template is equivalent to the
9685 other template, so if any of the arguments are pack expansions, strip
9686 the alias to avoid problems with a pack expansion passed to a non-pack
9687 alias template parameter (DR 1430). */
9688 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9689 templ = get_underlying_template (templ);
9690
9691 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9692 {
9693 tree parm;
9694 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9695 if (arglist2 == error_mark_node
9696 || (!uses_template_parms (arglist2)
9697 && check_instantiated_args (templ, arglist2, complain)))
9698 return error_mark_node;
9699
9700 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9701 return parm;
9702 }
9703 else
9704 {
9705 tree template_type = TREE_TYPE (templ);
9706 tree gen_tmpl;
9707 tree type_decl;
9708 tree found = NULL_TREE;
9709 int arg_depth;
9710 int parm_depth;
9711 int is_dependent_type;
9712 int use_partial_inst_tmpl = false;
9713
9714 if (template_type == error_mark_node)
9715 /* An error occurred while building the template TEMPL, and a
9716 diagnostic has most certainly been emitted for that
9717 already. Let's propagate that error. */
9718 return error_mark_node;
9719
9720 gen_tmpl = most_general_template (templ);
9721 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9722 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9723 arg_depth = TMPL_ARGS_DEPTH (arglist);
9724
9725 if (arg_depth == 1 && parm_depth > 1)
9726 {
9727 /* We've been given an incomplete set of template arguments.
9728 For example, given:
9729
9730 template <class T> struct S1 {
9731 template <class U> struct S2 {};
9732 template <class U> struct S2<U*> {};
9733 };
9734
9735 we will be called with an ARGLIST of `U*', but the
9736 TEMPLATE will be `template <class T> template
9737 <class U> struct S1<T>::S2'. We must fill in the missing
9738 arguments. */
9739 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9740 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9741 arg_depth = TMPL_ARGS_DEPTH (arglist);
9742 }
9743
9744 /* Now we should have enough arguments. */
9745 gcc_assert (parm_depth == arg_depth);
9746
9747 /* From here on, we're only interested in the most general
9748 template. */
9749
9750 /* Calculate the BOUND_ARGS. These will be the args that are
9751 actually tsubst'd into the definition to create the
9752 instantiation. */
9753 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9754 complain,
9755 /*require_all_args=*/true,
9756 /*use_default_args=*/true);
9757
9758 if (arglist == error_mark_node)
9759 /* We were unable to bind the arguments. */
9760 return error_mark_node;
9761
9762 /* In the scope of a template class, explicit references to the
9763 template class refer to the type of the template, not any
9764 instantiation of it. For example, in:
9765
9766 template <class T> class C { void f(C<T>); }
9767
9768 the `C<T>' is just the same as `C'. Outside of the
9769 class, however, such a reference is an instantiation. */
9770 if (entering_scope
9771 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9772 || currently_open_class (template_type))
9773 {
9774 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9775
9776 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9777 return template_type;
9778 }
9779
9780 /* If we already have this specialization, return it. */
9781 elt.tmpl = gen_tmpl;
9782 elt.args = arglist;
9783 elt.spec = NULL_TREE;
9784 hash = spec_hasher::hash (&elt);
9785 entry = type_specializations->find_with_hash (&elt, hash);
9786
9787 if (entry)
9788 return entry->spec;
9789
9790 /* If the template's constraints are not satisfied,
9791 then we cannot form a valid type.
9792
9793 Note that the check is deferred until after the hash
9794 lookup. This prevents redundant checks on previously
9795 instantiated specializations. */
9796 if (flag_concepts
9797 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9798 && !constraints_satisfied_p (gen_tmpl, arglist))
9799 {
9800 if (complain & tf_error)
9801 {
9802 auto_diagnostic_group d;
9803 error ("template constraint failure for %qD", gen_tmpl);
9804 diagnose_constraints (input_location, gen_tmpl, arglist);
9805 }
9806 return error_mark_node;
9807 }
9808
9809 is_dependent_type = uses_template_parms (arglist);
9810
9811 /* If the deduced arguments are invalid, then the binding
9812 failed. */
9813 if (!is_dependent_type
9814 && check_instantiated_args (gen_tmpl,
9815 INNERMOST_TEMPLATE_ARGS (arglist),
9816 complain))
9817 return error_mark_node;
9818
9819 if (!is_dependent_type
9820 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9821 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9822 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9823 {
9824 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9825 DECL_NAME (gen_tmpl),
9826 /*tag_scope=*/ts_global);
9827 return found;
9828 }
9829
9830 context = DECL_CONTEXT (gen_tmpl);
9831 if (context && TYPE_P (context))
9832 {
9833 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9834 context = complete_type (context);
9835 }
9836 else
9837 context = tsubst (context, arglist, complain, in_decl);
9838
9839 if (context == error_mark_node)
9840 return error_mark_node;
9841
9842 if (!context)
9843 context = global_namespace;
9844
9845 /* Create the type. */
9846 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9847 {
9848 /* The user referred to a specialization of an alias
9849 template represented by GEN_TMPL.
9850
9851 [temp.alias]/2 says:
9852
9853 When a template-id refers to the specialization of an
9854 alias template, it is equivalent to the associated
9855 type obtained by substitution of its
9856 template-arguments for the template-parameters in the
9857 type-id of the alias template. */
9858
9859 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9860 /* Note that the call above (by indirectly calling
9861 register_specialization in tsubst_decl) registers the
9862 TYPE_DECL representing the specialization of the alias
9863 template. So next time someone substitutes ARGLIST for
9864 the template parms into the alias template (GEN_TMPL),
9865 she'll get that TYPE_DECL back. */
9866
9867 if (t == error_mark_node)
9868 return t;
9869 }
9870 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9871 {
9872 if (!is_dependent_type)
9873 {
9874 set_current_access_from_decl (TYPE_NAME (template_type));
9875 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9876 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9877 arglist, complain, in_decl),
9878 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9879 arglist, complain, in_decl),
9880 SCOPED_ENUM_P (template_type), NULL);
9881
9882 if (t == error_mark_node)
9883 return t;
9884 }
9885 else
9886 {
9887 /* We don't want to call start_enum for this type, since
9888 the values for the enumeration constants may involve
9889 template parameters. And, no one should be interested
9890 in the enumeration constants for such a type. */
9891 t = cxx_make_type (ENUMERAL_TYPE);
9892 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9893 }
9894 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9895 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9896 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9897 }
9898 else if (CLASS_TYPE_P (template_type))
9899 {
9900 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9901 instantiated here. */
9902 gcc_assert (!LAMBDA_TYPE_P (template_type));
9903
9904 t = make_class_type (TREE_CODE (template_type));
9905 CLASSTYPE_DECLARED_CLASS (t)
9906 = CLASSTYPE_DECLARED_CLASS (template_type);
9907 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9908
9909 /* A local class. Make sure the decl gets registered properly. */
9910 if (context == current_function_decl)
9911 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9912 == error_mark_node)
9913 return error_mark_node;
9914
9915 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9916 /* This instantiation is another name for the primary
9917 template type. Set the TYPE_CANONICAL field
9918 appropriately. */
9919 TYPE_CANONICAL (t) = template_type;
9920 else if (any_template_arguments_need_structural_equality_p (arglist))
9921 /* Some of the template arguments require structural
9922 equality testing, so this template class requires
9923 structural equality testing. */
9924 SET_TYPE_STRUCTURAL_EQUALITY (t);
9925 }
9926 else
9927 gcc_unreachable ();
9928
9929 /* If we called start_enum or pushtag above, this information
9930 will already be set up. */
9931 type_decl = TYPE_NAME (t);
9932 if (!type_decl)
9933 {
9934 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9935
9936 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9937 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9938 DECL_SOURCE_LOCATION (type_decl)
9939 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9940 }
9941
9942 if (CLASS_TYPE_P (template_type))
9943 {
9944 TREE_PRIVATE (type_decl)
9945 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9946 TREE_PROTECTED (type_decl)
9947 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9948 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9949 {
9950 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9951 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9952 }
9953 }
9954
9955 if (OVERLOAD_TYPE_P (t)
9956 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9957 {
9958 static const char *tags[] = {"abi_tag", "may_alias"};
9959
9960 for (unsigned ix = 0; ix != 2; ix++)
9961 {
9962 tree attributes
9963 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9964
9965 if (attributes)
9966 TYPE_ATTRIBUTES (t)
9967 = tree_cons (TREE_PURPOSE (attributes),
9968 TREE_VALUE (attributes),
9969 TYPE_ATTRIBUTES (t));
9970 }
9971 }
9972
9973 /* Let's consider the explicit specialization of a member
9974 of a class template specialization that is implicitly instantiated,
9975 e.g.:
9976 template<class T>
9977 struct S
9978 {
9979 template<class U> struct M {}; //#0
9980 };
9981
9982 template<>
9983 template<>
9984 struct S<int>::M<char> //#1
9985 {
9986 int i;
9987 };
9988 [temp.expl.spec]/4 says this is valid.
9989
9990 In this case, when we write:
9991 S<int>::M<char> m;
9992
9993 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9994 the one of #0.
9995
9996 When we encounter #1, we want to store the partial instantiation
9997 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9998
9999 For all cases other than this "explicit specialization of member of a
10000 class template", we just want to store the most general template into
10001 the CLASSTYPE_TI_TEMPLATE of M.
10002
10003 This case of "explicit specialization of member of a class template"
10004 only happens when:
10005 1/ the enclosing class is an instantiation of, and therefore not
10006 the same as, the context of the most general template, and
10007 2/ we aren't looking at the partial instantiation itself, i.e.
10008 the innermost arguments are not the same as the innermost parms of
10009 the most general template.
10010
10011 So it's only when 1/ and 2/ happens that we want to use the partial
10012 instantiation of the member template in lieu of its most general
10013 template. */
10014
10015 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10016 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10017 /* the enclosing class must be an instantiation... */
10018 && CLASS_TYPE_P (context)
10019 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10020 {
10021 TREE_VEC_LENGTH (arglist)--;
10022 ++processing_template_decl;
10023 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10024 tree partial_inst_args =
10025 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10026 arglist, complain, NULL_TREE);
10027 --processing_template_decl;
10028 TREE_VEC_LENGTH (arglist)++;
10029 if (partial_inst_args == error_mark_node)
10030 return error_mark_node;
10031 use_partial_inst_tmpl =
10032 /*...and we must not be looking at the partial instantiation
10033 itself. */
10034 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10035 partial_inst_args);
10036 }
10037
10038 if (!use_partial_inst_tmpl)
10039 /* This case is easy; there are no member templates involved. */
10040 found = gen_tmpl;
10041 else
10042 {
10043 /* This is a full instantiation of a member template. Find
10044 the partial instantiation of which this is an instance. */
10045
10046 /* Temporarily reduce by one the number of levels in the ARGLIST
10047 so as to avoid comparing the last set of arguments. */
10048 TREE_VEC_LENGTH (arglist)--;
10049 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10050 TREE_VEC_LENGTH (arglist)++;
10051 /* FOUND is either a proper class type, or an alias
10052 template specialization. In the later case, it's a
10053 TYPE_DECL, resulting from the substituting of arguments
10054 for parameters in the TYPE_DECL of the alias template
10055 done earlier. So be careful while getting the template
10056 of FOUND. */
10057 found = (TREE_CODE (found) == TEMPLATE_DECL
10058 ? found
10059 : (TREE_CODE (found) == TYPE_DECL
10060 ? DECL_TI_TEMPLATE (found)
10061 : CLASSTYPE_TI_TEMPLATE (found)));
10062
10063 if (DECL_CLASS_TEMPLATE_P (found)
10064 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10065 {
10066 /* If this partial instantiation is specialized, we want to
10067 use it for hash table lookup. */
10068 elt.tmpl = found;
10069 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10070 hash = spec_hasher::hash (&elt);
10071 }
10072 }
10073
10074 // Build template info for the new specialization.
10075 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10076
10077 elt.spec = t;
10078 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10079 gcc_checking_assert (*slot == NULL);
10080 entry = ggc_alloc<spec_entry> ();
10081 *entry = elt;
10082 *slot = entry;
10083
10084 /* Note this use of the partial instantiation so we can check it
10085 later in maybe_process_partial_specialization. */
10086 DECL_TEMPLATE_INSTANTIATIONS (found)
10087 = tree_cons (arglist, t,
10088 DECL_TEMPLATE_INSTANTIATIONS (found));
10089
10090 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10091 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10092 /* Now that the type has been registered on the instantiations
10093 list, we set up the enumerators. Because the enumeration
10094 constants may involve the enumeration type itself, we make
10095 sure to register the type first, and then create the
10096 constants. That way, doing tsubst_expr for the enumeration
10097 constants won't result in recursive calls here; we'll find
10098 the instantiation and exit above. */
10099 tsubst_enum (template_type, t, arglist);
10100
10101 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10102 /* If the type makes use of template parameters, the
10103 code that generates debugging information will crash. */
10104 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10105
10106 /* Possibly limit visibility based on template args. */
10107 TREE_PUBLIC (type_decl) = 1;
10108 determine_visibility (type_decl);
10109
10110 inherit_targ_abi_tags (t);
10111
10112 return t;
10113 }
10114 }
10115
10116 /* Wrapper for lookup_template_class_1. */
10117
10118 tree
10119 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10120 int entering_scope, tsubst_flags_t complain)
10121 {
10122 tree ret;
10123 timevar_push (TV_TEMPLATE_INST);
10124 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10125 entering_scope, complain);
10126 timevar_pop (TV_TEMPLATE_INST);
10127 return ret;
10128 }
10129
10130 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10131
10132 tree
10133 lookup_template_variable (tree templ, tree arglist)
10134 {
10135 if (flag_concepts && variable_concept_p (templ))
10136 return build_concept_check (templ, arglist, tf_none);
10137
10138 /* The type of the expression is NULL_TREE since the template-id could refer
10139 to an explicit or partial specialization. */
10140 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10141 }
10142
10143 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10144
10145 tree
10146 finish_template_variable (tree var, tsubst_flags_t complain)
10147 {
10148 tree templ = TREE_OPERAND (var, 0);
10149 tree arglist = TREE_OPERAND (var, 1);
10150
10151 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10152 arglist = add_outermost_template_args (tmpl_args, arglist);
10153
10154 templ = most_general_template (templ);
10155 tree parms = DECL_TEMPLATE_PARMS (templ);
10156 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10157 /*req_all*/true,
10158 /*use_default*/true);
10159
10160 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10161 {
10162 if (complain & tf_error)
10163 {
10164 auto_diagnostic_group d;
10165 error ("use of invalid variable template %qE", var);
10166 diagnose_constraints (location_of (var), templ, arglist);
10167 }
10168 return error_mark_node;
10169 }
10170
10171 return instantiate_template (templ, arglist, complain);
10172 }
10173
10174 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10175 TARGS template args, and instantiate it if it's not dependent. */
10176
10177 tree
10178 lookup_and_finish_template_variable (tree templ, tree targs,
10179 tsubst_flags_t complain)
10180 {
10181 templ = lookup_template_variable (templ, targs);
10182 if (!any_dependent_template_arguments_p (targs))
10183 {
10184 templ = finish_template_variable (templ, complain);
10185 mark_used (templ);
10186 }
10187
10188 return convert_from_reference (templ);
10189 }
10190
10191 \f
10192 struct pair_fn_data
10193 {
10194 tree_fn_t fn;
10195 tree_fn_t any_fn;
10196 void *data;
10197 /* True when we should also visit template parameters that occur in
10198 non-deduced contexts. */
10199 bool include_nondeduced_p;
10200 hash_set<tree> *visited;
10201 };
10202
10203 /* Called from for_each_template_parm via walk_tree. */
10204
10205 static tree
10206 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10207 {
10208 tree t = *tp;
10209 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10210 tree_fn_t fn = pfd->fn;
10211 void *data = pfd->data;
10212 tree result = NULL_TREE;
10213
10214 #define WALK_SUBTREE(NODE) \
10215 do \
10216 { \
10217 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10218 pfd->include_nondeduced_p, \
10219 pfd->any_fn); \
10220 if (result) goto out; \
10221 } \
10222 while (0)
10223
10224 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10225 return t;
10226
10227 if (TYPE_P (t)
10228 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10229 WALK_SUBTREE (TYPE_CONTEXT (t));
10230
10231 switch (TREE_CODE (t))
10232 {
10233 case RECORD_TYPE:
10234 if (TYPE_PTRMEMFUNC_P (t))
10235 break;
10236 /* Fall through. */
10237
10238 case UNION_TYPE:
10239 case ENUMERAL_TYPE:
10240 if (!TYPE_TEMPLATE_INFO (t))
10241 *walk_subtrees = 0;
10242 else
10243 WALK_SUBTREE (TYPE_TI_ARGS (t));
10244 break;
10245
10246 case INTEGER_TYPE:
10247 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10248 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10249 break;
10250
10251 case METHOD_TYPE:
10252 /* Since we're not going to walk subtrees, we have to do this
10253 explicitly here. */
10254 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10255 /* Fall through. */
10256
10257 case FUNCTION_TYPE:
10258 /* Check the return type. */
10259 WALK_SUBTREE (TREE_TYPE (t));
10260
10261 /* Check the parameter types. Since default arguments are not
10262 instantiated until they are needed, the TYPE_ARG_TYPES may
10263 contain expressions that involve template parameters. But,
10264 no-one should be looking at them yet. And, once they're
10265 instantiated, they don't contain template parameters, so
10266 there's no point in looking at them then, either. */
10267 {
10268 tree parm;
10269
10270 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10271 WALK_SUBTREE (TREE_VALUE (parm));
10272
10273 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10274 want walk_tree walking into them itself. */
10275 *walk_subtrees = 0;
10276 }
10277
10278 if (flag_noexcept_type)
10279 {
10280 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10281 if (spec)
10282 WALK_SUBTREE (TREE_PURPOSE (spec));
10283 }
10284 break;
10285
10286 case TYPEOF_TYPE:
10287 case DECLTYPE_TYPE:
10288 case UNDERLYING_TYPE:
10289 if (pfd->include_nondeduced_p
10290 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10291 pfd->visited,
10292 pfd->include_nondeduced_p,
10293 pfd->any_fn))
10294 return error_mark_node;
10295 *walk_subtrees = false;
10296 break;
10297
10298 case FUNCTION_DECL:
10299 case VAR_DECL:
10300 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10301 WALK_SUBTREE (DECL_TI_ARGS (t));
10302 /* Fall through. */
10303
10304 case PARM_DECL:
10305 case CONST_DECL:
10306 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10307 WALK_SUBTREE (DECL_INITIAL (t));
10308 if (DECL_CONTEXT (t)
10309 && pfd->include_nondeduced_p)
10310 WALK_SUBTREE (DECL_CONTEXT (t));
10311 break;
10312
10313 case BOUND_TEMPLATE_TEMPLATE_PARM:
10314 /* Record template parameters such as `T' inside `TT<T>'. */
10315 WALK_SUBTREE (TYPE_TI_ARGS (t));
10316 /* Fall through. */
10317
10318 case TEMPLATE_TEMPLATE_PARM:
10319 case TEMPLATE_TYPE_PARM:
10320 case TEMPLATE_PARM_INDEX:
10321 if (fn && (*fn)(t, data))
10322 return t;
10323 else if (!fn)
10324 return t;
10325 break;
10326
10327 case TEMPLATE_DECL:
10328 /* A template template parameter is encountered. */
10329 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10330 WALK_SUBTREE (TREE_TYPE (t));
10331
10332 /* Already substituted template template parameter */
10333 *walk_subtrees = 0;
10334 break;
10335
10336 case TYPENAME_TYPE:
10337 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10338 partial instantiation. */
10339 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10340 *walk_subtrees = 0;
10341 break;
10342
10343 case CONSTRUCTOR:
10344 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10345 && pfd->include_nondeduced_p)
10346 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10347 break;
10348
10349 case INDIRECT_REF:
10350 case COMPONENT_REF:
10351 /* If there's no type, then this thing must be some expression
10352 involving template parameters. */
10353 if (!fn && !TREE_TYPE (t))
10354 return error_mark_node;
10355 break;
10356
10357 case MODOP_EXPR:
10358 case CAST_EXPR:
10359 case IMPLICIT_CONV_EXPR:
10360 case REINTERPRET_CAST_EXPR:
10361 case CONST_CAST_EXPR:
10362 case STATIC_CAST_EXPR:
10363 case DYNAMIC_CAST_EXPR:
10364 case ARROW_EXPR:
10365 case DOTSTAR_EXPR:
10366 case TYPEID_EXPR:
10367 case PSEUDO_DTOR_EXPR:
10368 if (!fn)
10369 return error_mark_node;
10370 break;
10371
10372 case SCOPE_REF:
10373 if (pfd->include_nondeduced_p)
10374 WALK_SUBTREE (TREE_OPERAND (t, 0));
10375 break;
10376
10377 case REQUIRES_EXPR:
10378 {
10379 if (!fn)
10380 return error_mark_node;
10381
10382 /* Recursively walk the type of each constraint variable. */
10383 tree p = TREE_OPERAND (t, 0);
10384 while (p)
10385 {
10386 WALK_SUBTREE (TREE_TYPE (p));
10387 p = TREE_CHAIN (p);
10388 }
10389 }
10390 break;
10391
10392 default:
10393 break;
10394 }
10395
10396 #undef WALK_SUBTREE
10397
10398 /* We didn't find any template parameters we liked. */
10399 out:
10400 return result;
10401 }
10402
10403 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10404 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10405 call FN with the parameter and the DATA.
10406 If FN returns nonzero, the iteration is terminated, and
10407 for_each_template_parm returns 1. Otherwise, the iteration
10408 continues. If FN never returns a nonzero value, the value
10409 returned by for_each_template_parm is 0. If FN is NULL, it is
10410 considered to be the function which always returns 1.
10411
10412 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10413 parameters that occur in non-deduced contexts. When false, only
10414 visits those template parameters that can be deduced. */
10415
10416 static tree
10417 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10418 hash_set<tree> *visited,
10419 bool include_nondeduced_p,
10420 tree_fn_t any_fn)
10421 {
10422 struct pair_fn_data pfd;
10423 tree result;
10424
10425 /* Set up. */
10426 pfd.fn = fn;
10427 pfd.any_fn = any_fn;
10428 pfd.data = data;
10429 pfd.include_nondeduced_p = include_nondeduced_p;
10430
10431 /* Walk the tree. (Conceptually, we would like to walk without
10432 duplicates, but for_each_template_parm_r recursively calls
10433 for_each_template_parm, so we would need to reorganize a fair
10434 bit to use walk_tree_without_duplicates, so we keep our own
10435 visited list.) */
10436 if (visited)
10437 pfd.visited = visited;
10438 else
10439 pfd.visited = new hash_set<tree>;
10440 result = cp_walk_tree (&t,
10441 for_each_template_parm_r,
10442 &pfd,
10443 pfd.visited);
10444
10445 /* Clean up. */
10446 if (!visited)
10447 {
10448 delete pfd.visited;
10449 pfd.visited = 0;
10450 }
10451
10452 return result;
10453 }
10454
10455 struct find_template_parameter_info
10456 {
10457 explicit find_template_parameter_info (tree ctx_parms)
10458 : parm_list (NULL_TREE),
10459 ctx_parms (ctx_parms),
10460 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10461 {}
10462
10463 hash_set<tree> visited;
10464 hash_set<tree> parms;
10465 tree parm_list;
10466 tree ctx_parms;
10467 int max_depth;
10468 };
10469
10470 /* Appends the declaration of T to the list in DATA. */
10471
10472 static int
10473 keep_template_parm (tree t, void* data)
10474 {
10475 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10476
10477 /* Template parameters declared within the expression are not part of
10478 the parameter mapping. For example, in this concept:
10479
10480 template<typename T>
10481 concept C = requires { <expr> } -> same_as<int>;
10482
10483 the return specifier same_as<int> declares a new decltype parameter
10484 that must not be part of the parameter mapping. The same is true
10485 for generic lambda parameters, lambda template parameters, etc. */
10486 int level;
10487 int index;
10488 template_parm_level_and_index (t, &level, &index);
10489 if (level > ftpi->max_depth)
10490 return 0;
10491
10492 /* Arguments like const T yield parameters like const T. This means that
10493 a template-id like X<T, const T> would yield two distinct parameters:
10494 T and const T. Adjust types to their unqualified versions. */
10495 if (TYPE_P (t))
10496 t = TYPE_MAIN_VARIANT (t);
10497 if (!ftpi->parms.add (t))
10498 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10499
10500 return 0;
10501 }
10502
10503 /* Ensure that we recursively examine certain terms that are not normally
10504 visited in for_each_template_parm_r. */
10505
10506 static int
10507 any_template_parm_r (tree t, void *data)
10508 {
10509 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10510
10511 #define WALK_SUBTREE(NODE) \
10512 do \
10513 { \
10514 for_each_template_parm (NODE, keep_template_parm, data, \
10515 &ftpi->visited, true, \
10516 any_template_parm_r); \
10517 } \
10518 while (0)
10519
10520 /* A mention of a member alias/typedef is a use of all of its template
10521 arguments, including those from the enclosing class, so we don't use
10522 alias_template_specialization_p here. */
10523 if (TYPE_P (t) && typedef_variant_p (t))
10524 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10525 WALK_SUBTREE (TI_ARGS (tinfo));
10526
10527 switch (TREE_CODE (t))
10528 {
10529 case TEMPLATE_TYPE_PARM:
10530 /* Type constraints of a placeholder type may contain parameters. */
10531 if (is_auto (t))
10532 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10533 WALK_SUBTREE (constr);
10534 break;
10535
10536 case TEMPLATE_ID_EXPR:
10537 /* Search through references to variable templates. */
10538 WALK_SUBTREE (TREE_OPERAND (t, 0));
10539 WALK_SUBTREE (TREE_OPERAND (t, 1));
10540 break;
10541
10542 case TEMPLATE_PARM_INDEX:
10543 case PARM_DECL:
10544 /* A parameter or constraint variable may also depend on a template
10545 parameter without explicitly naming it. */
10546 WALK_SUBTREE (TREE_TYPE (t));
10547 break;
10548
10549 case TEMPLATE_DECL:
10550 {
10551 /* If T is a member template that shares template parameters with
10552 ctx_parms, we need to mark all those parameters for mapping. */
10553 tree dparms = DECL_TEMPLATE_PARMS (t);
10554 tree cparms = ftpi->ctx_parms;
10555 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10556 dparms = TREE_CHAIN (dparms);
10557 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10558 cparms = TREE_CHAIN (cparms);
10559 while (dparms
10560 && (TREE_TYPE (TREE_VALUE (dparms))
10561 != TREE_TYPE (TREE_VALUE (cparms))))
10562 dparms = TREE_CHAIN (dparms),
10563 cparms = TREE_CHAIN (cparms);
10564 if (dparms)
10565 {
10566 int ddepth = TMPL_PARMS_DEPTH (dparms);
10567 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10568 for (int i = 0; i < ddepth; ++i)
10569 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10570 }
10571 }
10572 break;
10573
10574 case LAMBDA_EXPR:
10575 {
10576 /* Look in the parms and body. */
10577 tree fn = lambda_function (t);
10578 WALK_SUBTREE (TREE_TYPE (fn));
10579 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10580 }
10581 break;
10582
10583 case IDENTIFIER_NODE:
10584 if (IDENTIFIER_CONV_OP_P (t))
10585 /* The conversion-type-id of a conversion operator may be dependent. */
10586 WALK_SUBTREE (TREE_TYPE (t));
10587 break;
10588
10589 default:
10590 break;
10591 }
10592
10593 /* Keep walking. */
10594 return 0;
10595 }
10596
10597 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10598 are the template parameters in scope. */
10599
10600 tree
10601 find_template_parameters (tree t, tree ctx_parms)
10602 {
10603 if (!ctx_parms)
10604 return NULL_TREE;
10605
10606 find_template_parameter_info ftpi (ctx_parms);
10607 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10608 /*include_nondeduced*/true, any_template_parm_r);
10609 return ftpi.parm_list;
10610 }
10611
10612 /* Returns true if T depends on any template parameter. */
10613
10614 int
10615 uses_template_parms (tree t)
10616 {
10617 if (t == NULL_TREE)
10618 return false;
10619
10620 bool dependent_p;
10621 int saved_processing_template_decl;
10622
10623 saved_processing_template_decl = processing_template_decl;
10624 if (!saved_processing_template_decl)
10625 processing_template_decl = 1;
10626 if (TYPE_P (t))
10627 dependent_p = dependent_type_p (t);
10628 else if (TREE_CODE (t) == TREE_VEC)
10629 dependent_p = any_dependent_template_arguments_p (t);
10630 else if (TREE_CODE (t) == TREE_LIST)
10631 dependent_p = (uses_template_parms (TREE_VALUE (t))
10632 || uses_template_parms (TREE_CHAIN (t)));
10633 else if (TREE_CODE (t) == TYPE_DECL)
10634 dependent_p = dependent_type_p (TREE_TYPE (t));
10635 else if (t == error_mark_node)
10636 dependent_p = false;
10637 else
10638 dependent_p = value_dependent_expression_p (t);
10639
10640 processing_template_decl = saved_processing_template_decl;
10641
10642 return dependent_p;
10643 }
10644
10645 /* Returns true iff current_function_decl is an incompletely instantiated
10646 template. Useful instead of processing_template_decl because the latter
10647 is set to 0 during instantiate_non_dependent_expr. */
10648
10649 bool
10650 in_template_function (void)
10651 {
10652 tree fn = current_function_decl;
10653 bool ret;
10654 ++processing_template_decl;
10655 ret = (fn && DECL_LANG_SPECIFIC (fn)
10656 && DECL_TEMPLATE_INFO (fn)
10657 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10658 --processing_template_decl;
10659 return ret;
10660 }
10661
10662 /* Returns true if T depends on any template parameter with level LEVEL. */
10663
10664 bool
10665 uses_template_parms_level (tree t, int level)
10666 {
10667 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10668 /*include_nondeduced_p=*/true);
10669 }
10670
10671 /* Returns true if the signature of DECL depends on any template parameter from
10672 its enclosing class. */
10673
10674 bool
10675 uses_outer_template_parms (tree decl)
10676 {
10677 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10678 if (depth == 0)
10679 return false;
10680 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10681 &depth, NULL, /*include_nondeduced_p=*/true))
10682 return true;
10683 if (PRIMARY_TEMPLATE_P (decl)
10684 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10685 (DECL_TEMPLATE_PARMS (decl)),
10686 template_parm_outer_level,
10687 &depth, NULL, /*include_nondeduced_p=*/true))
10688 return true;
10689 tree ci = get_constraints (decl);
10690 if (ci)
10691 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10692 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10693 &depth, NULL, /*nondeduced*/true))
10694 return true;
10695 return false;
10696 }
10697
10698 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10699 ill-formed translation unit, i.e. a variable or function that isn't
10700 usable in a constant expression. */
10701
10702 static inline bool
10703 neglectable_inst_p (tree d)
10704 {
10705 return (d && DECL_P (d)
10706 && !undeduced_auto_decl (d)
10707 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10708 : decl_maybe_constant_var_p (d)));
10709 }
10710
10711 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10712 neglectable and instantiated from within an erroneous instantiation. */
10713
10714 static bool
10715 limit_bad_template_recursion (tree decl)
10716 {
10717 struct tinst_level *lev = current_tinst_level;
10718 int errs = errorcount + sorrycount;
10719 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10720 return false;
10721
10722 for (; lev; lev = lev->next)
10723 if (neglectable_inst_p (lev->maybe_get_node ()))
10724 break;
10725
10726 return (lev && errs > lev->errors);
10727 }
10728
10729 static int tinst_depth;
10730 extern int max_tinst_depth;
10731 int depth_reached;
10732
10733 static GTY(()) struct tinst_level *last_error_tinst_level;
10734
10735 /* We're starting to instantiate D; record the template instantiation context
10736 at LOC for diagnostics and to restore it later. */
10737
10738 static bool
10739 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10740 {
10741 struct tinst_level *new_level;
10742
10743 if (tinst_depth >= max_tinst_depth)
10744 {
10745 /* Tell error.c not to try to instantiate any templates. */
10746 at_eof = 2;
10747 fatal_error (input_location,
10748 "template instantiation depth exceeds maximum of %d"
10749 " (use %<-ftemplate-depth=%> to increase the maximum)",
10750 max_tinst_depth);
10751 return false;
10752 }
10753
10754 /* If the current instantiation caused problems, don't let it instantiate
10755 anything else. Do allow deduction substitution and decls usable in
10756 constant expressions. */
10757 if (!targs && limit_bad_template_recursion (tldcl))
10758 {
10759 /* Avoid no_linkage_errors and unused function warnings for this
10760 decl. */
10761 TREE_NO_WARNING (tldcl) = 1;
10762 return false;
10763 }
10764
10765 /* When not -quiet, dump template instantiations other than functions, since
10766 announce_function will take care of those. */
10767 if (!quiet_flag && !targs
10768 && TREE_CODE (tldcl) != TREE_LIST
10769 && TREE_CODE (tldcl) != FUNCTION_DECL)
10770 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10771
10772 new_level = tinst_level_freelist ().alloc ();
10773 new_level->tldcl = tldcl;
10774 new_level->targs = targs;
10775 new_level->locus = loc;
10776 new_level->errors = errorcount + sorrycount;
10777 new_level->next = NULL;
10778 new_level->refcount = 0;
10779 set_refcount_ptr (new_level->next, current_tinst_level);
10780 set_refcount_ptr (current_tinst_level, new_level);
10781
10782 ++tinst_depth;
10783 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10784 depth_reached = tinst_depth;
10785
10786 return true;
10787 }
10788
10789 /* We're starting substitution of TMPL<ARGS>; record the template
10790 substitution context for diagnostics and to restore it later. */
10791
10792 static bool
10793 push_tinst_level (tree tmpl, tree args)
10794 {
10795 return push_tinst_level_loc (tmpl, args, input_location);
10796 }
10797
10798 /* We're starting to instantiate D; record INPUT_LOCATION and the
10799 template instantiation context for diagnostics and to restore it
10800 later. */
10801
10802 bool
10803 push_tinst_level (tree d)
10804 {
10805 return push_tinst_level_loc (d, input_location);
10806 }
10807
10808 /* Likewise, but record LOC as the program location. */
10809
10810 bool
10811 push_tinst_level_loc (tree d, location_t loc)
10812 {
10813 gcc_assert (TREE_CODE (d) != TREE_LIST);
10814 return push_tinst_level_loc (d, NULL, loc);
10815 }
10816
10817 /* We're done instantiating this template; return to the instantiation
10818 context. */
10819
10820 void
10821 pop_tinst_level (void)
10822 {
10823 /* Restore the filename and line number stashed away when we started
10824 this instantiation. */
10825 input_location = current_tinst_level->locus;
10826 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10827 --tinst_depth;
10828 }
10829
10830 /* We're instantiating a deferred template; restore the template
10831 instantiation context in which the instantiation was requested, which
10832 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10833
10834 static tree
10835 reopen_tinst_level (struct tinst_level *level)
10836 {
10837 struct tinst_level *t;
10838
10839 tinst_depth = 0;
10840 for (t = level; t; t = t->next)
10841 ++tinst_depth;
10842
10843 set_refcount_ptr (current_tinst_level, level);
10844 pop_tinst_level ();
10845 if (current_tinst_level)
10846 current_tinst_level->errors = errorcount+sorrycount;
10847 return level->maybe_get_node ();
10848 }
10849
10850 /* Returns the TINST_LEVEL which gives the original instantiation
10851 context. */
10852
10853 struct tinst_level *
10854 outermost_tinst_level (void)
10855 {
10856 struct tinst_level *level = current_tinst_level;
10857 if (level)
10858 while (level->next)
10859 level = level->next;
10860 return level;
10861 }
10862
10863 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10864 vector of template arguments, as for tsubst.
10865
10866 Returns an appropriate tsubst'd friend declaration. */
10867
10868 static tree
10869 tsubst_friend_function (tree decl, tree args)
10870 {
10871 tree new_friend;
10872
10873 if (TREE_CODE (decl) == FUNCTION_DECL
10874 && DECL_TEMPLATE_INSTANTIATION (decl)
10875 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10876 /* This was a friend declared with an explicit template
10877 argument list, e.g.:
10878
10879 friend void f<>(T);
10880
10881 to indicate that f was a template instantiation, not a new
10882 function declaration. Now, we have to figure out what
10883 instantiation of what template. */
10884 {
10885 tree template_id, arglist, fns;
10886 tree new_args;
10887 tree tmpl;
10888 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10889
10890 /* Friend functions are looked up in the containing namespace scope.
10891 We must enter that scope, to avoid finding member functions of the
10892 current class with same name. */
10893 push_nested_namespace (ns);
10894 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10895 tf_warning_or_error, NULL_TREE,
10896 /*integral_constant_expression_p=*/false);
10897 pop_nested_namespace (ns);
10898 arglist = tsubst (DECL_TI_ARGS (decl), args,
10899 tf_warning_or_error, NULL_TREE);
10900 template_id = lookup_template_function (fns, arglist);
10901
10902 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10903 tmpl = determine_specialization (template_id, new_friend,
10904 &new_args,
10905 /*need_member_template=*/0,
10906 TREE_VEC_LENGTH (args),
10907 tsk_none);
10908 return instantiate_template (tmpl, new_args, tf_error);
10909 }
10910
10911 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10912 if (new_friend == error_mark_node)
10913 return error_mark_node;
10914
10915 /* The NEW_FRIEND will look like an instantiation, to the
10916 compiler, but is not an instantiation from the point of view of
10917 the language. For example, we might have had:
10918
10919 template <class T> struct S {
10920 template <class U> friend void f(T, U);
10921 };
10922
10923 Then, in S<int>, template <class U> void f(int, U) is not an
10924 instantiation of anything. */
10925
10926 DECL_USE_TEMPLATE (new_friend) = 0;
10927 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10928 {
10929 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10930 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10931 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10932
10933 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10934 match in decls_match. */
10935 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10936 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10937 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10938 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10939 }
10940
10941 /* The mangled name for the NEW_FRIEND is incorrect. The function
10942 is not a template instantiation and should not be mangled like
10943 one. Therefore, we forget the mangling here; we'll recompute it
10944 later if we need it. */
10945 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10946 {
10947 SET_DECL_RTL (new_friend, NULL);
10948 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10949 }
10950
10951 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10952 {
10953 tree old_decl;
10954 tree ns;
10955
10956 /* We must save some information from NEW_FRIEND before calling
10957 duplicate decls since that function will free NEW_FRIEND if
10958 possible. */
10959 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10960 tree new_friend_result_template_info = NULL_TREE;
10961 bool new_friend_is_defn =
10962 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10963 (template_for_substitution (new_friend)))
10964 != NULL_TREE);
10965 tree not_tmpl = new_friend;
10966
10967 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10968 {
10969 /* This declaration is a `primary' template. */
10970 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10971
10972 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
10973 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
10974 }
10975
10976 /* Inside pushdecl_namespace_level, we will push into the
10977 current namespace. However, the friend function should go
10978 into the namespace of the template. */
10979 ns = decl_namespace_context (new_friend);
10980 push_nested_namespace (ns);
10981 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10982 pop_nested_namespace (ns);
10983
10984 if (old_decl == error_mark_node)
10985 return error_mark_node;
10986
10987 if (old_decl != new_friend)
10988 {
10989 /* This new friend declaration matched an existing
10990 declaration. For example, given:
10991
10992 template <class T> void f(T);
10993 template <class U> class C {
10994 template <class T> friend void f(T) {}
10995 };
10996
10997 the friend declaration actually provides the definition
10998 of `f', once C has been instantiated for some type. So,
10999 old_decl will be the out-of-class template declaration,
11000 while new_friend is the in-class definition.
11001
11002 But, if `f' was called before this point, the
11003 instantiation of `f' will have DECL_TI_ARGS corresponding
11004 to `T' but not to `U', references to which might appear
11005 in the definition of `f'. Previously, the most general
11006 template for an instantiation of `f' was the out-of-class
11007 version; now it is the in-class version. Therefore, we
11008 run through all specialization of `f', adding to their
11009 DECL_TI_ARGS appropriately. In particular, they need a
11010 new set of outer arguments, corresponding to the
11011 arguments for this class instantiation.
11012
11013 The same situation can arise with something like this:
11014
11015 friend void f(int);
11016 template <class T> class C {
11017 friend void f(T) {}
11018 };
11019
11020 when `C<int>' is instantiated. Now, `f(int)' is defined
11021 in the class. */
11022
11023 if (!new_friend_is_defn)
11024 /* On the other hand, if the in-class declaration does
11025 *not* provide a definition, then we don't want to alter
11026 existing definitions. We can just leave everything
11027 alone. */
11028 ;
11029 else
11030 {
11031 tree new_template = TI_TEMPLATE (new_friend_template_info);
11032 tree new_args = TI_ARGS (new_friend_template_info);
11033
11034 /* Overwrite whatever template info was there before, if
11035 any, with the new template information pertaining to
11036 the declaration. */
11037 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11038
11039 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11040 {
11041 /* We should have called reregister_specialization in
11042 duplicate_decls. */
11043 gcc_assert (retrieve_specialization (new_template,
11044 new_args, 0)
11045 == old_decl);
11046
11047 /* Instantiate it if the global has already been used. */
11048 if (DECL_ODR_USED (old_decl))
11049 instantiate_decl (old_decl, /*defer_ok=*/true,
11050 /*expl_inst_class_mem_p=*/false);
11051 }
11052 else
11053 {
11054 tree t;
11055
11056 /* Indicate that the old function template is a partial
11057 instantiation. */
11058 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11059 = new_friend_result_template_info;
11060
11061 gcc_assert (new_template
11062 == most_general_template (new_template));
11063 gcc_assert (new_template != old_decl);
11064
11065 /* Reassign any specializations already in the hash table
11066 to the new more general template, and add the
11067 additional template args. */
11068 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11069 t != NULL_TREE;
11070 t = TREE_CHAIN (t))
11071 {
11072 tree spec = TREE_VALUE (t);
11073 spec_entry elt;
11074
11075 elt.tmpl = old_decl;
11076 elt.args = DECL_TI_ARGS (spec);
11077 elt.spec = NULL_TREE;
11078
11079 decl_specializations->remove_elt (&elt);
11080
11081 DECL_TI_ARGS (spec)
11082 = add_outermost_template_args (new_args,
11083 DECL_TI_ARGS (spec));
11084
11085 register_specialization
11086 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11087
11088 }
11089 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11090 }
11091 }
11092
11093 /* The information from NEW_FRIEND has been merged into OLD_DECL
11094 by duplicate_decls. */
11095 new_friend = old_decl;
11096 }
11097 }
11098 else
11099 {
11100 tree context = DECL_CONTEXT (new_friend);
11101 bool dependent_p;
11102
11103 /* In the code
11104 template <class T> class C {
11105 template <class U> friend void C1<U>::f (); // case 1
11106 friend void C2<T>::f (); // case 2
11107 };
11108 we only need to make sure CONTEXT is a complete type for
11109 case 2. To distinguish between the two cases, we note that
11110 CONTEXT of case 1 remains dependent type after tsubst while
11111 this isn't true for case 2. */
11112 ++processing_template_decl;
11113 dependent_p = dependent_type_p (context);
11114 --processing_template_decl;
11115
11116 if (!dependent_p
11117 && !complete_type_or_else (context, NULL_TREE))
11118 return error_mark_node;
11119
11120 if (COMPLETE_TYPE_P (context))
11121 {
11122 tree fn = new_friend;
11123 /* do_friend adds the TEMPLATE_DECL for any member friend
11124 template even if it isn't a member template, i.e.
11125 template <class T> friend A<T>::f();
11126 Look through it in that case. */
11127 if (TREE_CODE (fn) == TEMPLATE_DECL
11128 && !PRIMARY_TEMPLATE_P (fn))
11129 fn = DECL_TEMPLATE_RESULT (fn);
11130 /* Check to see that the declaration is really present, and,
11131 possibly obtain an improved declaration. */
11132 fn = check_classfn (context, fn, NULL_TREE);
11133
11134 if (fn)
11135 new_friend = fn;
11136 }
11137 }
11138
11139 return new_friend;
11140 }
11141
11142 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11143 template arguments, as for tsubst.
11144
11145 Returns an appropriate tsubst'd friend type or error_mark_node on
11146 failure. */
11147
11148 static tree
11149 tsubst_friend_class (tree friend_tmpl, tree args)
11150 {
11151 tree tmpl;
11152
11153 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11154 {
11155 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11156 return TREE_TYPE (tmpl);
11157 }
11158
11159 tree context = CP_DECL_CONTEXT (friend_tmpl);
11160 if (TREE_CODE (context) == NAMESPACE_DECL)
11161 push_nested_namespace (context);
11162 else
11163 {
11164 context = tsubst (context, args, tf_error, NULL_TREE);
11165 push_nested_class (context);
11166 }
11167
11168 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11169 /*non_class=*/false, /*block_p=*/false,
11170 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11171
11172 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11173 {
11174 /* The friend template has already been declared. Just
11175 check to see that the declarations match, and install any new
11176 default parameters. We must tsubst the default parameters,
11177 of course. We only need the innermost template parameters
11178 because that is all that redeclare_class_template will look
11179 at. */
11180 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11181 > TMPL_ARGS_DEPTH (args))
11182 {
11183 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11184 args, tf_warning_or_error);
11185 location_t saved_input_location = input_location;
11186 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11187 tree cons = get_constraints (tmpl);
11188 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11189 input_location = saved_input_location;
11190 }
11191 }
11192 else
11193 {
11194 /* The friend template has not already been declared. In this
11195 case, the instantiation of the template class will cause the
11196 injection of this template into the namespace scope. */
11197 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11198
11199 if (tmpl != error_mark_node)
11200 {
11201 /* The new TMPL is not an instantiation of anything, so we
11202 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11203 for the new type because that is supposed to be the
11204 corresponding template decl, i.e., TMPL. */
11205 DECL_USE_TEMPLATE (tmpl) = 0;
11206 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11207 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11208 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11209 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11210
11211 /* It is hidden. */
11212 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11213 DECL_ANTICIPATED (tmpl)
11214 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11215
11216 /* Inject this template into the enclosing namspace scope. */
11217 tmpl = pushdecl_namespace_level (tmpl, true);
11218 }
11219 }
11220
11221 if (TREE_CODE (context) == NAMESPACE_DECL)
11222 pop_nested_namespace (context);
11223 else
11224 pop_nested_class ();
11225
11226 return TREE_TYPE (tmpl);
11227 }
11228
11229 /* Returns zero if TYPE cannot be completed later due to circularity.
11230 Otherwise returns one. */
11231
11232 static int
11233 can_complete_type_without_circularity (tree type)
11234 {
11235 if (type == NULL_TREE || type == error_mark_node)
11236 return 0;
11237 else if (COMPLETE_TYPE_P (type))
11238 return 1;
11239 else if (TREE_CODE (type) == ARRAY_TYPE)
11240 return can_complete_type_without_circularity (TREE_TYPE (type));
11241 else if (CLASS_TYPE_P (type)
11242 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11243 return 0;
11244 else
11245 return 1;
11246 }
11247
11248 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11249 tsubst_flags_t, tree);
11250
11251 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11252 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11253
11254 static tree
11255 tsubst_attribute (tree t, tree *decl_p, tree args,
11256 tsubst_flags_t complain, tree in_decl)
11257 {
11258 gcc_assert (ATTR_IS_DEPENDENT (t));
11259
11260 tree val = TREE_VALUE (t);
11261 if (val == NULL_TREE)
11262 /* Nothing to do. */;
11263 else if ((flag_openmp || flag_openmp_simd)
11264 && is_attribute_p ("omp declare simd",
11265 get_attribute_name (t)))
11266 {
11267 tree clauses = TREE_VALUE (val);
11268 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11269 complain, in_decl);
11270 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11271 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11272 tree parms = DECL_ARGUMENTS (*decl_p);
11273 clauses
11274 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11275 if (clauses)
11276 val = build_tree_list (NULL_TREE, clauses);
11277 else
11278 val = NULL_TREE;
11279 }
11280 else if (flag_openmp
11281 && is_attribute_p ("omp declare variant base",
11282 get_attribute_name (t)))
11283 {
11284 ++cp_unevaluated_operand;
11285 tree varid
11286 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11287 in_decl, /*integral_constant_expression_p=*/false);
11288 --cp_unevaluated_operand;
11289 tree chain = TREE_CHAIN (val);
11290 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11291 tree ctx = copy_list (TREE_VALUE (val));
11292 tree simd = get_identifier ("simd");
11293 tree score = get_identifier (" score");
11294 tree condition = get_identifier ("condition");
11295 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11296 {
11297 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11298 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11299 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11300 {
11301 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11302 {
11303 tree clauses = TREE_VALUE (t2);
11304 clauses = tsubst_omp_clauses (clauses,
11305 C_ORT_OMP_DECLARE_SIMD, args,
11306 complain, in_decl);
11307 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11308 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11309 TREE_VALUE (t2) = clauses;
11310 }
11311 else
11312 {
11313 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11314 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11315 if (TREE_VALUE (t3))
11316 {
11317 bool allow_string
11318 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11319 && TREE_PURPOSE (t3) != score);
11320 tree v = TREE_VALUE (t3);
11321 if (TREE_CODE (v) == STRING_CST && allow_string)
11322 continue;
11323 v = tsubst_expr (v, args, complain, in_decl, true);
11324 v = fold_non_dependent_expr (v);
11325 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11326 || (TREE_PURPOSE (t3) == score
11327 ? TREE_CODE (v) != INTEGER_CST
11328 : !tree_fits_shwi_p (v)))
11329 {
11330 location_t loc
11331 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11332 match_loc);
11333 if (TREE_PURPOSE (t3) == score)
11334 error_at (loc, "score argument must be "
11335 "constant integer expression");
11336 else if (allow_string)
11337 error_at (loc, "property must be constant "
11338 "integer expression or string "
11339 "literal");
11340 else
11341 error_at (loc, "property must be constant "
11342 "integer expression");
11343 return NULL_TREE;
11344 }
11345 else if (TREE_PURPOSE (t3) == score
11346 && tree_int_cst_sgn (v) < 0)
11347 {
11348 location_t loc
11349 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11350 match_loc);
11351 error_at (loc, "score argument must be "
11352 "non-negative");
11353 return NULL_TREE;
11354 }
11355 TREE_VALUE (t3) = v;
11356 }
11357 }
11358 }
11359 }
11360 val = tree_cons (varid, ctx, chain);
11361 }
11362 /* If the first attribute argument is an identifier, don't
11363 pass it through tsubst. Attributes like mode, format,
11364 cleanup and several target specific attributes expect it
11365 unmodified. */
11366 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11367 {
11368 tree chain
11369 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11370 /*integral_constant_expression_p=*/false);
11371 if (chain != TREE_CHAIN (val))
11372 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11373 }
11374 else if (PACK_EXPANSION_P (val))
11375 {
11376 /* An attribute pack expansion. */
11377 tree purp = TREE_PURPOSE (t);
11378 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11379 if (pack == error_mark_node)
11380 return error_mark_node;
11381 int len = TREE_VEC_LENGTH (pack);
11382 tree list = NULL_TREE;
11383 tree *q = &list;
11384 for (int i = 0; i < len; ++i)
11385 {
11386 tree elt = TREE_VEC_ELT (pack, i);
11387 *q = build_tree_list (purp, elt);
11388 q = &TREE_CHAIN (*q);
11389 }
11390 return list;
11391 }
11392 else
11393 val = tsubst_expr (val, args, complain, in_decl,
11394 /*integral_constant_expression_p=*/false);
11395
11396 if (val != TREE_VALUE (t))
11397 return build_tree_list (TREE_PURPOSE (t), val);
11398 return t;
11399 }
11400
11401 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11402 unchanged or a new TREE_LIST chain. */
11403
11404 static tree
11405 tsubst_attributes (tree attributes, tree args,
11406 tsubst_flags_t complain, tree in_decl)
11407 {
11408 tree last_dep = NULL_TREE;
11409
11410 for (tree t = attributes; t; t = TREE_CHAIN (t))
11411 if (ATTR_IS_DEPENDENT (t))
11412 {
11413 last_dep = t;
11414 attributes = copy_list (attributes);
11415 break;
11416 }
11417
11418 if (last_dep)
11419 for (tree *p = &attributes; *p; )
11420 {
11421 tree t = *p;
11422 if (ATTR_IS_DEPENDENT (t))
11423 {
11424 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11425 if (subst != t)
11426 {
11427 *p = subst;
11428 while (*p)
11429 p = &TREE_CHAIN (*p);
11430 *p = TREE_CHAIN (t);
11431 continue;
11432 }
11433 }
11434 p = &TREE_CHAIN (*p);
11435 }
11436
11437 return attributes;
11438 }
11439
11440 /* Apply any attributes which had to be deferred until instantiation
11441 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11442 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11443
11444 static void
11445 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11446 tree args, tsubst_flags_t complain, tree in_decl)
11447 {
11448 tree last_dep = NULL_TREE;
11449 tree t;
11450 tree *p;
11451
11452 if (attributes == NULL_TREE)
11453 return;
11454
11455 if (DECL_P (*decl_p))
11456 {
11457 if (TREE_TYPE (*decl_p) == error_mark_node)
11458 return;
11459 p = &DECL_ATTRIBUTES (*decl_p);
11460 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11461 to our attributes parameter. */
11462 gcc_assert (*p == attributes);
11463 }
11464 else
11465 {
11466 p = &TYPE_ATTRIBUTES (*decl_p);
11467 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11468 lookup_template_class_1, and should be preserved. */
11469 gcc_assert (*p != attributes);
11470 while (*p)
11471 p = &TREE_CHAIN (*p);
11472 }
11473
11474 for (t = attributes; t; t = TREE_CHAIN (t))
11475 if (ATTR_IS_DEPENDENT (t))
11476 {
11477 last_dep = t;
11478 attributes = copy_list (attributes);
11479 break;
11480 }
11481
11482 *p = attributes;
11483 if (last_dep)
11484 {
11485 tree late_attrs = NULL_TREE;
11486 tree *q = &late_attrs;
11487
11488 for (; *p; )
11489 {
11490 t = *p;
11491 if (ATTR_IS_DEPENDENT (t))
11492 {
11493 *p = TREE_CHAIN (t);
11494 TREE_CHAIN (t) = NULL_TREE;
11495 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11496 while (*q)
11497 q = &TREE_CHAIN (*q);
11498 }
11499 else
11500 p = &TREE_CHAIN (t);
11501 }
11502
11503 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11504 }
11505 }
11506
11507 /* Perform (or defer) access check for typedefs that were referenced
11508 from within the template TMPL code.
11509 This is a subroutine of instantiate_decl and instantiate_class_template.
11510 TMPL is the template to consider and TARGS is the list of arguments of
11511 that template. */
11512
11513 static void
11514 perform_typedefs_access_check (tree tmpl, tree targs)
11515 {
11516 unsigned i;
11517 qualified_typedef_usage_t *iter;
11518
11519 if (!tmpl
11520 || (!CLASS_TYPE_P (tmpl)
11521 && TREE_CODE (tmpl) != FUNCTION_DECL))
11522 return;
11523
11524 if (vec<qualified_typedef_usage_t, va_gc> *tdefs
11525 = get_types_needing_access_check (tmpl))
11526 FOR_EACH_VEC_ELT (*tdefs, i, iter)
11527 {
11528 tree type_decl = iter->typedef_decl;
11529 tree type_scope = iter->context;
11530
11531 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11532 continue;
11533
11534 if (uses_template_parms (type_decl))
11535 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11536 if (uses_template_parms (type_scope))
11537 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11538
11539 /* Make access check error messages point to the location
11540 of the use of the typedef. */
11541 iloc_sentinel ils (iter->locus);
11542 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11543 type_decl, type_decl,
11544 tf_warning_or_error);
11545 }
11546 }
11547
11548 static tree
11549 instantiate_class_template_1 (tree type)
11550 {
11551 tree templ, args, pattern, t, member;
11552 tree typedecl;
11553 tree pbinfo;
11554 tree base_list;
11555 unsigned int saved_maximum_field_alignment;
11556 tree fn_context;
11557
11558 if (type == error_mark_node)
11559 return error_mark_node;
11560
11561 if (COMPLETE_OR_OPEN_TYPE_P (type)
11562 || uses_template_parms (type))
11563 return type;
11564
11565 /* Figure out which template is being instantiated. */
11566 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11567 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11568
11569 /* Mark the type as in the process of being defined. */
11570 TYPE_BEING_DEFINED (type) = 1;
11571
11572 /* We may be in the middle of deferred access check. Disable
11573 it now. */
11574 deferring_access_check_sentinel acs (dk_no_deferred);
11575
11576 /* Determine what specialization of the original template to
11577 instantiate. */
11578 t = most_specialized_partial_spec (type, tf_warning_or_error);
11579 if (t == error_mark_node)
11580 return error_mark_node;
11581 else if (t)
11582 {
11583 /* This TYPE is actually an instantiation of a partial
11584 specialization. We replace the innermost set of ARGS with
11585 the arguments appropriate for substitution. For example,
11586 given:
11587
11588 template <class T> struct S {};
11589 template <class T> struct S<T*> {};
11590
11591 and supposing that we are instantiating S<int*>, ARGS will
11592 presently be {int*} -- but we need {int}. */
11593 pattern = TREE_TYPE (t);
11594 args = TREE_PURPOSE (t);
11595 }
11596 else
11597 {
11598 pattern = TREE_TYPE (templ);
11599 args = CLASSTYPE_TI_ARGS (type);
11600 }
11601
11602 /* If the template we're instantiating is incomplete, then clearly
11603 there's nothing we can do. */
11604 if (!COMPLETE_TYPE_P (pattern))
11605 {
11606 /* We can try again later. */
11607 TYPE_BEING_DEFINED (type) = 0;
11608 return type;
11609 }
11610
11611 /* If we've recursively instantiated too many templates, stop. */
11612 if (! push_tinst_level (type))
11613 return type;
11614
11615 int saved_unevaluated_operand = cp_unevaluated_operand;
11616 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11617
11618 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11619 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11620 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11621 fn_context = error_mark_node;
11622 if (!fn_context)
11623 push_to_top_level ();
11624 else
11625 {
11626 cp_unevaluated_operand = 0;
11627 c_inhibit_evaluation_warnings = 0;
11628 }
11629 /* Use #pragma pack from the template context. */
11630 saved_maximum_field_alignment = maximum_field_alignment;
11631 maximum_field_alignment = TYPE_PRECISION (pattern);
11632
11633 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11634
11635 /* Set the input location to the most specialized template definition.
11636 This is needed if tsubsting causes an error. */
11637 typedecl = TYPE_MAIN_DECL (pattern);
11638 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11639 DECL_SOURCE_LOCATION (typedecl);
11640
11641 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11642 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11643 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11644 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11645 if (ANON_AGGR_TYPE_P (pattern))
11646 SET_ANON_AGGR_TYPE_P (type);
11647 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11648 {
11649 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11650 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11651 /* Adjust visibility for template arguments. */
11652 determine_visibility (TYPE_MAIN_DECL (type));
11653 }
11654 if (CLASS_TYPE_P (type))
11655 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11656
11657 pbinfo = TYPE_BINFO (pattern);
11658
11659 /* We should never instantiate a nested class before its enclosing
11660 class; we need to look up the nested class by name before we can
11661 instantiate it, and that lookup should instantiate the enclosing
11662 class. */
11663 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11664 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11665
11666 base_list = NULL_TREE;
11667 if (BINFO_N_BASE_BINFOS (pbinfo))
11668 {
11669 tree pbase_binfo;
11670 tree pushed_scope;
11671 int i;
11672
11673 /* We must enter the scope containing the type, as that is where
11674 the accessibility of types named in dependent bases are
11675 looked up from. */
11676 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11677
11678 /* Substitute into each of the bases to determine the actual
11679 basetypes. */
11680 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11681 {
11682 tree base;
11683 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11684 tree expanded_bases = NULL_TREE;
11685 int idx, len = 1;
11686
11687 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11688 {
11689 expanded_bases =
11690 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11691 args, tf_error, NULL_TREE);
11692 if (expanded_bases == error_mark_node)
11693 continue;
11694
11695 len = TREE_VEC_LENGTH (expanded_bases);
11696 }
11697
11698 for (idx = 0; idx < len; idx++)
11699 {
11700 if (expanded_bases)
11701 /* Extract the already-expanded base class. */
11702 base = TREE_VEC_ELT (expanded_bases, idx);
11703 else
11704 /* Substitute to figure out the base class. */
11705 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11706 NULL_TREE);
11707
11708 if (base == error_mark_node)
11709 continue;
11710
11711 base_list = tree_cons (access, base, base_list);
11712 if (BINFO_VIRTUAL_P (pbase_binfo))
11713 TREE_TYPE (base_list) = integer_type_node;
11714 }
11715 }
11716
11717 /* The list is now in reverse order; correct that. */
11718 base_list = nreverse (base_list);
11719
11720 if (pushed_scope)
11721 pop_scope (pushed_scope);
11722 }
11723 /* Now call xref_basetypes to set up all the base-class
11724 information. */
11725 xref_basetypes (type, base_list);
11726
11727 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11728 (int) ATTR_FLAG_TYPE_IN_PLACE,
11729 args, tf_error, NULL_TREE);
11730 fixup_attribute_variants (type);
11731
11732 /* Now that our base classes are set up, enter the scope of the
11733 class, so that name lookups into base classes, etc. will work
11734 correctly. This is precisely analogous to what we do in
11735 begin_class_definition when defining an ordinary non-template
11736 class, except we also need to push the enclosing classes. */
11737 push_nested_class (type);
11738
11739 /* Now members are processed in the order of declaration. */
11740 for (member = CLASSTYPE_DECL_LIST (pattern);
11741 member; member = TREE_CHAIN (member))
11742 {
11743 tree t = TREE_VALUE (member);
11744
11745 if (TREE_PURPOSE (member))
11746 {
11747 if (TYPE_P (t))
11748 {
11749 if (LAMBDA_TYPE_P (t))
11750 /* A closure type for a lambda in an NSDMI or default argument.
11751 Ignore it; it will be regenerated when needed. */
11752 continue;
11753
11754 /* Build new CLASSTYPE_NESTED_UTDS. */
11755 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11756 && TYPE_LANG_SPECIFIC (t)
11757 && CLASSTYPE_IS_TEMPLATE (t));
11758
11759 /* If the member is a class template, then -- even after
11760 substitution -- there may be dependent types in the
11761 template argument list for the class. We increment
11762 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11763 that function will assume that no types are dependent
11764 when outside of a template. */
11765 if (class_template_p)
11766 ++processing_template_decl;
11767 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11768 if (class_template_p)
11769 --processing_template_decl;
11770 if (newtag == error_mark_node)
11771 continue;
11772
11773 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11774 {
11775 tree name = TYPE_IDENTIFIER (t);
11776
11777 if (class_template_p)
11778 /* Unfortunately, lookup_template_class sets
11779 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11780 instantiation (i.e., for the type of a member
11781 template class nested within a template class.)
11782 This behavior is required for
11783 maybe_process_partial_specialization to work
11784 correctly, but is not accurate in this case;
11785 the TAG is not an instantiation of anything.
11786 (The corresponding TEMPLATE_DECL is an
11787 instantiation, but the TYPE is not.) */
11788 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11789
11790 /* Now, we call pushtag to put this NEWTAG into the scope of
11791 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11792 pushtag calling push_template_decl. We don't have to do
11793 this for enums because it will already have been done in
11794 tsubst_enum. */
11795 if (name)
11796 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11797 pushtag (name, newtag, /*tag_scope=*/ts_current);
11798 }
11799 }
11800 else if (DECL_DECLARES_FUNCTION_P (t))
11801 {
11802 tree r;
11803
11804 if (TREE_CODE (t) == TEMPLATE_DECL)
11805 ++processing_template_decl;
11806 r = tsubst (t, args, tf_error, NULL_TREE);
11807 if (TREE_CODE (t) == TEMPLATE_DECL)
11808 --processing_template_decl;
11809 set_current_access_from_decl (r);
11810 finish_member_declaration (r);
11811 /* Instantiate members marked with attribute used. */
11812 if (r != error_mark_node && DECL_PRESERVE_P (r))
11813 mark_used (r);
11814 if (TREE_CODE (r) == FUNCTION_DECL
11815 && DECL_OMP_DECLARE_REDUCTION_P (r))
11816 cp_check_omp_declare_reduction (r);
11817 }
11818 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11819 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11820 /* A closure type for a lambda in an NSDMI or default argument.
11821 Ignore it; it will be regenerated when needed. */;
11822 else
11823 {
11824 /* Build new TYPE_FIELDS. */
11825 if (TREE_CODE (t) == STATIC_ASSERT)
11826 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11827 /*integral_constant_expression_p=*/true);
11828 else if (TREE_CODE (t) != CONST_DECL)
11829 {
11830 tree r;
11831 tree vec = NULL_TREE;
11832 int len = 1;
11833
11834 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11835 /* The file and line for this declaration, to
11836 assist in error message reporting. Since we
11837 called push_tinst_level above, we don't need to
11838 restore these. */
11839 input_location = DECL_SOURCE_LOCATION (t);
11840
11841 if (TREE_CODE (t) == TEMPLATE_DECL)
11842 ++processing_template_decl;
11843 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11844 if (TREE_CODE (t) == TEMPLATE_DECL)
11845 --processing_template_decl;
11846
11847 if (TREE_CODE (r) == TREE_VEC)
11848 {
11849 /* A capture pack became multiple fields. */
11850 vec = r;
11851 len = TREE_VEC_LENGTH (vec);
11852 }
11853
11854 for (int i = 0; i < len; ++i)
11855 {
11856 if (vec)
11857 r = TREE_VEC_ELT (vec, i);
11858 if (VAR_P (r))
11859 {
11860 /* In [temp.inst]:
11861
11862 [t]he initialization (and any associated
11863 side-effects) of a static data member does
11864 not occur unless the static data member is
11865 itself used in a way that requires the
11866 definition of the static data member to
11867 exist.
11868
11869 Therefore, we do not substitute into the
11870 initialized for the static data member here. */
11871 finish_static_data_member_decl
11872 (r,
11873 /*init=*/NULL_TREE,
11874 /*init_const_expr_p=*/false,
11875 /*asmspec_tree=*/NULL_TREE,
11876 /*flags=*/0);
11877 /* Instantiate members marked with attribute used. */
11878 if (r != error_mark_node && DECL_PRESERVE_P (r))
11879 mark_used (r);
11880 }
11881 else if (TREE_CODE (r) == FIELD_DECL)
11882 {
11883 /* Determine whether R has a valid type and can be
11884 completed later. If R is invalid, then its type
11885 is replaced by error_mark_node. */
11886 tree rtype = TREE_TYPE (r);
11887 if (can_complete_type_without_circularity (rtype))
11888 complete_type (rtype);
11889
11890 if (!complete_or_array_type_p (rtype))
11891 {
11892 /* If R's type couldn't be completed and
11893 it isn't a flexible array member (whose
11894 type is incomplete by definition) give
11895 an error. */
11896 cxx_incomplete_type_error (r, rtype);
11897 TREE_TYPE (r) = error_mark_node;
11898 }
11899 else if (TREE_CODE (rtype) == ARRAY_TYPE
11900 && TYPE_DOMAIN (rtype) == NULL_TREE
11901 && (TREE_CODE (type) == UNION_TYPE
11902 || TREE_CODE (type) == QUAL_UNION_TYPE))
11903 {
11904 error ("flexible array member %qD in union", r);
11905 TREE_TYPE (r) = error_mark_node;
11906 }
11907 else if (!verify_type_context (input_location,
11908 TCTX_FIELD, rtype))
11909 TREE_TYPE (r) = error_mark_node;
11910 }
11911
11912 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11913 such a thing will already have been added to the field
11914 list by tsubst_enum in finish_member_declaration in the
11915 CLASSTYPE_NESTED_UTDS case above. */
11916 if (!(TREE_CODE (r) == TYPE_DECL
11917 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11918 && DECL_ARTIFICIAL (r)))
11919 {
11920 set_current_access_from_decl (r);
11921 finish_member_declaration (r);
11922 }
11923 }
11924 }
11925 }
11926 }
11927 else
11928 {
11929 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11930 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11931 {
11932 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11933
11934 tree friend_type = t;
11935 bool adjust_processing_template_decl = false;
11936
11937 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11938 {
11939 /* template <class T> friend class C; */
11940 friend_type = tsubst_friend_class (friend_type, args);
11941 adjust_processing_template_decl = true;
11942 }
11943 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11944 {
11945 /* template <class T> friend class C::D; */
11946 friend_type = tsubst (friend_type, args,
11947 tf_warning_or_error, NULL_TREE);
11948 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11949 friend_type = TREE_TYPE (friend_type);
11950 adjust_processing_template_decl = true;
11951 }
11952 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11953 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11954 {
11955 /* This could be either
11956
11957 friend class T::C;
11958
11959 when dependent_type_p is false or
11960
11961 template <class U> friend class T::C;
11962
11963 otherwise. */
11964 /* Bump processing_template_decl in case this is something like
11965 template <class T> friend struct A<T>::B. */
11966 ++processing_template_decl;
11967 friend_type = tsubst (friend_type, args,
11968 tf_warning_or_error, NULL_TREE);
11969 if (dependent_type_p (friend_type))
11970 adjust_processing_template_decl = true;
11971 --processing_template_decl;
11972 }
11973 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11974 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11975 && TYPE_HIDDEN_P (friend_type))
11976 {
11977 /* friend class C;
11978
11979 where C hasn't been declared yet. Let's lookup name
11980 from namespace scope directly, bypassing any name that
11981 come from dependent base class. */
11982 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11983
11984 /* The call to xref_tag_from_type does injection for friend
11985 classes. */
11986 push_nested_namespace (ns);
11987 friend_type =
11988 xref_tag_from_type (friend_type, NULL_TREE,
11989 /*tag_scope=*/ts_current);
11990 pop_nested_namespace (ns);
11991 }
11992 else if (uses_template_parms (friend_type))
11993 /* friend class C<T>; */
11994 friend_type = tsubst (friend_type, args,
11995 tf_warning_or_error, NULL_TREE);
11996 /* Otherwise it's
11997
11998 friend class C;
11999
12000 where C is already declared or
12001
12002 friend class C<int>;
12003
12004 We don't have to do anything in these cases. */
12005
12006 if (adjust_processing_template_decl)
12007 /* Trick make_friend_class into realizing that the friend
12008 we're adding is a template, not an ordinary class. It's
12009 important that we use make_friend_class since it will
12010 perform some error-checking and output cross-reference
12011 information. */
12012 ++processing_template_decl;
12013
12014 if (friend_type != error_mark_node)
12015 make_friend_class (type, friend_type, /*complain=*/false);
12016
12017 if (adjust_processing_template_decl)
12018 --processing_template_decl;
12019 }
12020 else
12021 {
12022 /* Build new DECL_FRIENDLIST. */
12023 tree r;
12024
12025 /* The file and line for this declaration, to
12026 assist in error message reporting. Since we
12027 called push_tinst_level above, we don't need to
12028 restore these. */
12029 input_location = DECL_SOURCE_LOCATION (t);
12030
12031 if (TREE_CODE (t) == TEMPLATE_DECL)
12032 {
12033 ++processing_template_decl;
12034 push_deferring_access_checks (dk_no_check);
12035 }
12036
12037 r = tsubst_friend_function (t, args);
12038 add_friend (type, r, /*complain=*/false);
12039 if (TREE_CODE (t) == TEMPLATE_DECL)
12040 {
12041 pop_deferring_access_checks ();
12042 --processing_template_decl;
12043 }
12044 }
12045 }
12046 }
12047
12048 if (fn_context)
12049 {
12050 /* Restore these before substituting into the lambda capture
12051 initializers. */
12052 cp_unevaluated_operand = saved_unevaluated_operand;
12053 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12054 }
12055
12056 /* Set the file and line number information to whatever is given for
12057 the class itself. This puts error messages involving generated
12058 implicit functions at a predictable point, and the same point
12059 that would be used for non-template classes. */
12060 input_location = DECL_SOURCE_LOCATION (typedecl);
12061
12062 unreverse_member_declarations (type);
12063 finish_struct_1 (type);
12064 TYPE_BEING_DEFINED (type) = 0;
12065
12066 /* We don't instantiate default arguments for member functions. 14.7.1:
12067
12068 The implicit instantiation of a class template specialization causes
12069 the implicit instantiation of the declarations, but not of the
12070 definitions or default arguments, of the class member functions,
12071 member classes, static data members and member templates.... */
12072
12073 /* Some typedefs referenced from within the template code need to be access
12074 checked at template instantiation time, i.e now. These types were
12075 added to the template at parsing time. Let's get those and perform
12076 the access checks then. */
12077 perform_typedefs_access_check (pattern, args);
12078 perform_deferred_access_checks (tf_warning_or_error);
12079 pop_nested_class ();
12080 maximum_field_alignment = saved_maximum_field_alignment;
12081 if (!fn_context)
12082 pop_from_top_level ();
12083 pop_tinst_level ();
12084
12085 /* The vtable for a template class can be emitted in any translation
12086 unit in which the class is instantiated. When there is no key
12087 method, however, finish_struct_1 will already have added TYPE to
12088 the keyed_classes. */
12089 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12090 vec_safe_push (keyed_classes, type);
12091
12092 return type;
12093 }
12094
12095 /* Wrapper for instantiate_class_template_1. */
12096
12097 tree
12098 instantiate_class_template (tree type)
12099 {
12100 tree ret;
12101 timevar_push (TV_TEMPLATE_INST);
12102 ret = instantiate_class_template_1 (type);
12103 timevar_pop (TV_TEMPLATE_INST);
12104 return ret;
12105 }
12106
12107 tree
12108 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12109 {
12110 tree r;
12111
12112 if (!t)
12113 r = t;
12114 else if (TYPE_P (t))
12115 r = tsubst (t, args, complain, in_decl);
12116 else
12117 {
12118 if (!(complain & tf_warning))
12119 ++c_inhibit_evaluation_warnings;
12120 r = tsubst_expr (t, args, complain, in_decl,
12121 /*integral_constant_expression_p=*/true);
12122 if (!(complain & tf_warning))
12123 --c_inhibit_evaluation_warnings;
12124 }
12125
12126 return r;
12127 }
12128
12129 /* Given a function parameter pack TMPL_PARM and some function parameters
12130 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12131 and set *SPEC_P to point at the next point in the list. */
12132
12133 tree
12134 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12135 {
12136 /* Collect all of the extra "packed" parameters into an
12137 argument pack. */
12138 tree parmvec;
12139 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12140 tree spec_parm = *spec_p;
12141 int i, len;
12142
12143 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12144 if (tmpl_parm
12145 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12146 break;
12147
12148 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12149 parmvec = make_tree_vec (len);
12150 spec_parm = *spec_p;
12151 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12152 {
12153 tree elt = spec_parm;
12154 if (DECL_PACK_P (elt))
12155 elt = make_pack_expansion (elt);
12156 TREE_VEC_ELT (parmvec, i) = elt;
12157 }
12158
12159 /* Build the argument packs. */
12160 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12161 *spec_p = spec_parm;
12162
12163 return argpack;
12164 }
12165
12166 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12167 NONTYPE_ARGUMENT_PACK. */
12168
12169 static tree
12170 make_fnparm_pack (tree spec_parm)
12171 {
12172 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12173 }
12174
12175 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12176 pack expansion with no extra args, 2 if it has extra args, or 0
12177 if it is not a pack expansion. */
12178
12179 static int
12180 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12181 {
12182 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12183 /* We're being called before this happens in tsubst_pack_expansion. */
12184 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12185 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12186 if (i >= TREE_VEC_LENGTH (vec))
12187 return 0;
12188 tree elt = TREE_VEC_ELT (vec, i);
12189 if (DECL_P (elt))
12190 /* A decl pack is itself an expansion. */
12191 elt = TREE_TYPE (elt);
12192 if (!PACK_EXPANSION_P (elt))
12193 return 0;
12194 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12195 return 2;
12196 return 1;
12197 }
12198
12199
12200 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12201
12202 static tree
12203 make_argument_pack_select (tree arg_pack, unsigned index)
12204 {
12205 tree aps = make_node (ARGUMENT_PACK_SELECT);
12206
12207 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12208 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12209
12210 return aps;
12211 }
12212
12213 /* This is a subroutine of tsubst_pack_expansion.
12214
12215 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12216 mechanism to store the (non complete list of) arguments of the
12217 substitution and return a non substituted pack expansion, in order
12218 to wait for when we have enough arguments to really perform the
12219 substitution. */
12220
12221 static bool
12222 use_pack_expansion_extra_args_p (tree parm_packs,
12223 int arg_pack_len,
12224 bool has_empty_arg)
12225 {
12226 /* If one pack has an expansion and another pack has a normal
12227 argument or if one pack has an empty argument and an another
12228 one hasn't then tsubst_pack_expansion cannot perform the
12229 substitution and need to fall back on the
12230 PACK_EXPANSION_EXTRA mechanism. */
12231 if (parm_packs == NULL_TREE)
12232 return false;
12233 else if (has_empty_arg)
12234 {
12235 /* If all the actual packs are pack expansions, we can still
12236 subsitute directly. */
12237 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12238 {
12239 tree a = TREE_VALUE (p);
12240 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12241 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12242 a = ARGUMENT_PACK_ARGS (a);
12243 if (TREE_VEC_LENGTH (a) == 1)
12244 a = TREE_VEC_ELT (a, 0);
12245 if (PACK_EXPANSION_P (a))
12246 continue;
12247 return true;
12248 }
12249 return false;
12250 }
12251
12252 bool has_expansion_arg = false;
12253 for (int i = 0 ; i < arg_pack_len; ++i)
12254 {
12255 bool has_non_expansion_arg = false;
12256 for (tree parm_pack = parm_packs;
12257 parm_pack;
12258 parm_pack = TREE_CHAIN (parm_pack))
12259 {
12260 tree arg = TREE_VALUE (parm_pack);
12261
12262 int exp = argument_pack_element_is_expansion_p (arg, i);
12263 if (exp == 2)
12264 /* We can't substitute a pack expansion with extra args into
12265 our pattern. */
12266 return true;
12267 else if (exp)
12268 has_expansion_arg = true;
12269 else
12270 has_non_expansion_arg = true;
12271 }
12272
12273 if (has_expansion_arg && has_non_expansion_arg)
12274 return true;
12275 }
12276 return false;
12277 }
12278
12279 /* [temp.variadic]/6 says that:
12280
12281 The instantiation of a pack expansion [...]
12282 produces a list E1,E2, ..., En, where N is the number of elements
12283 in the pack expansion parameters.
12284
12285 This subroutine of tsubst_pack_expansion produces one of these Ei.
12286
12287 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12288 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12289 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12290 INDEX is the index 'i' of the element Ei to produce. ARGS,
12291 COMPLAIN, and IN_DECL are the same parameters as for the
12292 tsubst_pack_expansion function.
12293
12294 The function returns the resulting Ei upon successful completion,
12295 or error_mark_node.
12296
12297 Note that this function possibly modifies the ARGS parameter, so
12298 it's the responsibility of the caller to restore it. */
12299
12300 static tree
12301 gen_elem_of_pack_expansion_instantiation (tree pattern,
12302 tree parm_packs,
12303 unsigned index,
12304 tree args /* This parm gets
12305 modified. */,
12306 tsubst_flags_t complain,
12307 tree in_decl)
12308 {
12309 tree t;
12310 bool ith_elem_is_expansion = false;
12311
12312 /* For each parameter pack, change the substitution of the parameter
12313 pack to the ith argument in its argument pack, then expand the
12314 pattern. */
12315 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12316 {
12317 tree parm = TREE_PURPOSE (pack);
12318 tree arg_pack = TREE_VALUE (pack);
12319 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12320
12321 ith_elem_is_expansion |=
12322 argument_pack_element_is_expansion_p (arg_pack, index);
12323
12324 /* Select the Ith argument from the pack. */
12325 if (TREE_CODE (parm) == PARM_DECL
12326 || VAR_P (parm)
12327 || TREE_CODE (parm) == FIELD_DECL)
12328 {
12329 if (index == 0)
12330 {
12331 aps = make_argument_pack_select (arg_pack, index);
12332 if (!mark_used (parm, complain) && !(complain & tf_error))
12333 return error_mark_node;
12334 register_local_specialization (aps, parm);
12335 }
12336 else
12337 aps = retrieve_local_specialization (parm);
12338 }
12339 else
12340 {
12341 int idx, level;
12342 template_parm_level_and_index (parm, &level, &idx);
12343
12344 if (index == 0)
12345 {
12346 aps = make_argument_pack_select (arg_pack, index);
12347 /* Update the corresponding argument. */
12348 TMPL_ARG (args, level, idx) = aps;
12349 }
12350 else
12351 /* Re-use the ARGUMENT_PACK_SELECT. */
12352 aps = TMPL_ARG (args, level, idx);
12353 }
12354 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12355 }
12356
12357 /* Substitute into the PATTERN with the (possibly altered)
12358 arguments. */
12359 if (pattern == in_decl)
12360 /* Expanding a fixed parameter pack from
12361 coerce_template_parameter_pack. */
12362 t = tsubst_decl (pattern, args, complain);
12363 else if (pattern == error_mark_node)
12364 t = error_mark_node;
12365 else if (!TYPE_P (pattern))
12366 t = tsubst_expr (pattern, args, complain, in_decl,
12367 /*integral_constant_expression_p=*/false);
12368 else
12369 t = tsubst (pattern, args, complain, in_decl);
12370
12371 /* If the Ith argument pack element is a pack expansion, then
12372 the Ith element resulting from the substituting is going to
12373 be a pack expansion as well. */
12374 if (ith_elem_is_expansion)
12375 t = make_pack_expansion (t, complain);
12376
12377 return t;
12378 }
12379
12380 /* When the unexpanded parameter pack in a fold expression expands to an empty
12381 sequence, the value of the expression is as follows; the program is
12382 ill-formed if the operator is not listed in this table.
12383
12384 && true
12385 || false
12386 , void() */
12387
12388 tree
12389 expand_empty_fold (tree t, tsubst_flags_t complain)
12390 {
12391 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12392 if (!FOLD_EXPR_MODIFY_P (t))
12393 switch (code)
12394 {
12395 case TRUTH_ANDIF_EXPR:
12396 return boolean_true_node;
12397 case TRUTH_ORIF_EXPR:
12398 return boolean_false_node;
12399 case COMPOUND_EXPR:
12400 return void_node;
12401 default:
12402 break;
12403 }
12404
12405 if (complain & tf_error)
12406 error_at (location_of (t),
12407 "fold of empty expansion over %O", code);
12408 return error_mark_node;
12409 }
12410
12411 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12412 form an expression that combines the two terms using the
12413 operator of T. */
12414
12415 static tree
12416 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12417 {
12418 tree op = FOLD_EXPR_OP (t);
12419 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12420
12421 // Handle compound assignment operators.
12422 if (FOLD_EXPR_MODIFY_P (t))
12423 return build_x_modify_expr (input_location, left, code, right, complain);
12424
12425 warning_sentinel s(warn_parentheses);
12426 switch (code)
12427 {
12428 case COMPOUND_EXPR:
12429 return build_x_compound_expr (input_location, left, right, complain);
12430 default:
12431 return build_x_binary_op (input_location, code,
12432 left, TREE_CODE (left),
12433 right, TREE_CODE (right),
12434 /*overload=*/NULL,
12435 complain);
12436 }
12437 }
12438
12439 /* Substitute ARGS into the pack of a fold expression T. */
12440
12441 static inline tree
12442 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12443 {
12444 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12445 }
12446
12447 /* Substitute ARGS into the pack of a fold expression T. */
12448
12449 static inline tree
12450 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12451 {
12452 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12453 }
12454
12455 /* Expand a PACK of arguments into a grouped as left fold.
12456 Given a pack containing elements A0, A1, ..., An and an
12457 operator @, this builds the expression:
12458
12459 ((A0 @ A1) @ A2) ... @ An
12460
12461 Note that PACK must not be empty.
12462
12463 The operator is defined by the original fold expression T. */
12464
12465 static tree
12466 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12467 {
12468 tree left = TREE_VEC_ELT (pack, 0);
12469 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12470 {
12471 tree right = TREE_VEC_ELT (pack, i);
12472 left = fold_expression (t, left, right, complain);
12473 }
12474 return left;
12475 }
12476
12477 /* Substitute into a unary left fold expression. */
12478
12479 static tree
12480 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12481 tree in_decl)
12482 {
12483 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12484 if (pack == error_mark_node)
12485 return error_mark_node;
12486 if (PACK_EXPANSION_P (pack))
12487 {
12488 tree r = copy_node (t);
12489 FOLD_EXPR_PACK (r) = pack;
12490 return r;
12491 }
12492 if (TREE_VEC_LENGTH (pack) == 0)
12493 return expand_empty_fold (t, complain);
12494 else
12495 return expand_left_fold (t, pack, complain);
12496 }
12497
12498 /* Substitute into a binary left fold expression.
12499
12500 Do ths by building a single (non-empty) vector of argumnts and
12501 building the expression from those elements. */
12502
12503 static tree
12504 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12505 tree in_decl)
12506 {
12507 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12508 if (pack == error_mark_node)
12509 return error_mark_node;
12510 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12511 if (init == error_mark_node)
12512 return error_mark_node;
12513
12514 if (PACK_EXPANSION_P (pack))
12515 {
12516 tree r = copy_node (t);
12517 FOLD_EXPR_PACK (r) = pack;
12518 FOLD_EXPR_INIT (r) = init;
12519 return r;
12520 }
12521
12522 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12523 TREE_VEC_ELT (vec, 0) = init;
12524 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12525 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12526
12527 return expand_left_fold (t, vec, complain);
12528 }
12529
12530 /* Expand a PACK of arguments into a grouped as right fold.
12531 Given a pack containing elementns A0, A1, ..., and an
12532 operator @, this builds the expression:
12533
12534 A0@ ... (An-2 @ (An-1 @ An))
12535
12536 Note that PACK must not be empty.
12537
12538 The operator is defined by the original fold expression T. */
12539
12540 tree
12541 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12542 {
12543 // Build the expression.
12544 int n = TREE_VEC_LENGTH (pack);
12545 tree right = TREE_VEC_ELT (pack, n - 1);
12546 for (--n; n != 0; --n)
12547 {
12548 tree left = TREE_VEC_ELT (pack, n - 1);
12549 right = fold_expression (t, left, right, complain);
12550 }
12551 return right;
12552 }
12553
12554 /* Substitute into a unary right fold expression. */
12555
12556 static tree
12557 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12558 tree in_decl)
12559 {
12560 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12561 if (pack == error_mark_node)
12562 return error_mark_node;
12563 if (PACK_EXPANSION_P (pack))
12564 {
12565 tree r = copy_node (t);
12566 FOLD_EXPR_PACK (r) = pack;
12567 return r;
12568 }
12569 if (TREE_VEC_LENGTH (pack) == 0)
12570 return expand_empty_fold (t, complain);
12571 else
12572 return expand_right_fold (t, pack, complain);
12573 }
12574
12575 /* Substitute into a binary right fold expression.
12576
12577 Do ths by building a single (non-empty) vector of arguments and
12578 building the expression from those elements. */
12579
12580 static tree
12581 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12582 tree in_decl)
12583 {
12584 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12585 if (pack == error_mark_node)
12586 return error_mark_node;
12587 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12588 if (init == error_mark_node)
12589 return error_mark_node;
12590
12591 if (PACK_EXPANSION_P (pack))
12592 {
12593 tree r = copy_node (t);
12594 FOLD_EXPR_PACK (r) = pack;
12595 FOLD_EXPR_INIT (r) = init;
12596 return r;
12597 }
12598
12599 int n = TREE_VEC_LENGTH (pack);
12600 tree vec = make_tree_vec (n + 1);
12601 for (int i = 0; i < n; ++i)
12602 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12603 TREE_VEC_ELT (vec, n) = init;
12604
12605 return expand_right_fold (t, vec, complain);
12606 }
12607
12608 /* Walk through the pattern of a pack expansion, adding everything in
12609 local_specializations to a list. */
12610
12611 class el_data
12612 {
12613 public:
12614 hash_set<tree> internal;
12615 tree extra;
12616 tsubst_flags_t complain;
12617
12618 el_data (tsubst_flags_t c)
12619 : extra (NULL_TREE), complain (c) {}
12620 };
12621 static tree
12622 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12623 {
12624 el_data &data = *reinterpret_cast<el_data*>(data_);
12625 tree *extra = &data.extra;
12626 tsubst_flags_t complain = data.complain;
12627
12628 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12629 /* Remember local typedefs (85214). */
12630 tp = &TYPE_NAME (*tp);
12631
12632 if (TREE_CODE (*tp) == DECL_EXPR)
12633 data.internal.add (DECL_EXPR_DECL (*tp));
12634 else if (tree spec = retrieve_local_specialization (*tp))
12635 {
12636 if (data.internal.contains (*tp))
12637 /* Don't mess with variables declared within the pattern. */
12638 return NULL_TREE;
12639 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12640 {
12641 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12642 tree args = ARGUMENT_PACK_ARGS (spec);
12643 if (TREE_VEC_LENGTH (args) == 1)
12644 {
12645 tree elt = TREE_VEC_ELT (args, 0);
12646 if (PACK_EXPANSION_P (elt))
12647 elt = PACK_EXPANSION_PATTERN (elt);
12648 if (DECL_PACK_P (elt))
12649 spec = elt;
12650 }
12651 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12652 {
12653 /* Handle lambda capture here, since we aren't doing any
12654 substitution now, and so tsubst_copy won't call
12655 process_outer_var_ref. */
12656 tree args = ARGUMENT_PACK_ARGS (spec);
12657 int len = TREE_VEC_LENGTH (args);
12658 for (int i = 0; i < len; ++i)
12659 {
12660 tree arg = TREE_VEC_ELT (args, i);
12661 tree carg = arg;
12662 if (outer_automatic_var_p (arg))
12663 carg = process_outer_var_ref (arg, complain);
12664 if (carg != arg)
12665 {
12666 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12667 proxies. */
12668 if (i == 0)
12669 {
12670 spec = copy_node (spec);
12671 args = copy_node (args);
12672 SET_ARGUMENT_PACK_ARGS (spec, args);
12673 register_local_specialization (spec, *tp);
12674 }
12675 TREE_VEC_ELT (args, i) = carg;
12676 }
12677 }
12678 }
12679 }
12680 if (outer_automatic_var_p (spec))
12681 spec = process_outer_var_ref (spec, complain);
12682 *extra = tree_cons (*tp, spec, *extra);
12683 }
12684 return NULL_TREE;
12685 }
12686 static tree
12687 extract_local_specs (tree pattern, tsubst_flags_t complain)
12688 {
12689 el_data data (complain);
12690 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12691 return data.extra;
12692 }
12693
12694 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12695 for use in PACK_EXPANSION_EXTRA_ARGS. */
12696
12697 tree
12698 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12699 {
12700 tree extra = args;
12701 if (local_specializations)
12702 if (tree locals = extract_local_specs (pattern, complain))
12703 extra = tree_cons (NULL_TREE, extra, locals);
12704 return extra;
12705 }
12706
12707 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12708 normal template args to ARGS. */
12709
12710 tree
12711 add_extra_args (tree extra, tree args)
12712 {
12713 if (extra && TREE_CODE (extra) == TREE_LIST)
12714 {
12715 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12716 {
12717 /* The partial instantiation involved local declarations collected in
12718 extract_local_specs; map from the general template to our local
12719 context. */
12720 tree gen = TREE_PURPOSE (elt);
12721 tree inst = TREE_VALUE (elt);
12722 if (DECL_P (inst))
12723 if (tree local = retrieve_local_specialization (inst))
12724 inst = local;
12725 /* else inst is already a full instantiation of the pack. */
12726 register_local_specialization (inst, gen);
12727 }
12728 gcc_assert (!TREE_PURPOSE (extra));
12729 extra = TREE_VALUE (extra);
12730 }
12731 #if 1
12732 /* I think we should always be able to substitute dependent args into the
12733 pattern. If that turns out to be incorrect in some cases, enable the
12734 alternate code (and add complain/in_decl parms to this function). */
12735 gcc_checking_assert (!uses_template_parms (extra));
12736 #else
12737 if (!uses_template_parms (extra))
12738 {
12739 gcc_unreachable ();
12740 extra = tsubst_template_args (extra, args, complain, in_decl);
12741 args = add_outermost_template_args (args, extra);
12742 }
12743 else
12744 #endif
12745 args = add_to_template_args (extra, args);
12746 return args;
12747 }
12748
12749 /* Substitute ARGS into T, which is an pack expansion
12750 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12751 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12752 (if only a partial substitution could be performed) or
12753 ERROR_MARK_NODE if there was an error. */
12754 tree
12755 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12756 tree in_decl)
12757 {
12758 tree pattern;
12759 tree pack, packs = NULL_TREE;
12760 bool unsubstituted_packs = false;
12761 int i, len = -1;
12762 tree result;
12763 bool need_local_specializations = false;
12764 int levels;
12765
12766 gcc_assert (PACK_EXPANSION_P (t));
12767 pattern = PACK_EXPANSION_PATTERN (t);
12768
12769 /* Add in any args remembered from an earlier partial instantiation. */
12770 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12771
12772 levels = TMPL_ARGS_DEPTH (args);
12773
12774 /* Determine the argument packs that will instantiate the parameter
12775 packs used in the expansion expression. While we're at it,
12776 compute the number of arguments to be expanded and make sure it
12777 is consistent. */
12778 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12779 pack = TREE_CHAIN (pack))
12780 {
12781 tree parm_pack = TREE_VALUE (pack);
12782 tree arg_pack = NULL_TREE;
12783 tree orig_arg = NULL_TREE;
12784 int level = 0;
12785
12786 if (TREE_CODE (parm_pack) == BASES)
12787 {
12788 gcc_assert (parm_pack == pattern);
12789 if (BASES_DIRECT (parm_pack))
12790 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12791 args, complain,
12792 in_decl, false),
12793 complain);
12794 else
12795 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12796 args, complain, in_decl,
12797 false), complain);
12798 }
12799 else if (builtin_pack_call_p (parm_pack))
12800 {
12801 if (parm_pack != pattern)
12802 {
12803 if (complain & tf_error)
12804 sorry ("%qE is not the entire pattern of the pack expansion",
12805 parm_pack);
12806 return error_mark_node;
12807 }
12808 return expand_builtin_pack_call (parm_pack, args,
12809 complain, in_decl);
12810 }
12811 else if (TREE_CODE (parm_pack) == PARM_DECL)
12812 {
12813 /* We know we have correct local_specializations if this
12814 expansion is at function scope, or if we're dealing with a
12815 local parameter in a requires expression; for the latter,
12816 tsubst_requires_expr set it up appropriately. */
12817 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12818 arg_pack = retrieve_local_specialization (parm_pack);
12819 else
12820 /* We can't rely on local_specializations for a parameter
12821 name used later in a function declaration (such as in a
12822 late-specified return type). Even if it exists, it might
12823 have the wrong value for a recursive call. */
12824 need_local_specializations = true;
12825
12826 if (!arg_pack)
12827 {
12828 /* This parameter pack was used in an unevaluated context. Just
12829 make a dummy decl, since it's only used for its type. */
12830 ++cp_unevaluated_operand;
12831 arg_pack = tsubst_decl (parm_pack, args, complain);
12832 --cp_unevaluated_operand;
12833 if (arg_pack && DECL_PACK_P (arg_pack))
12834 /* Partial instantiation of the parm_pack, we can't build
12835 up an argument pack yet. */
12836 arg_pack = NULL_TREE;
12837 else
12838 arg_pack = make_fnparm_pack (arg_pack);
12839 }
12840 else if (DECL_PACK_P (arg_pack))
12841 /* This argument pack isn't fully instantiated yet. */
12842 arg_pack = NULL_TREE;
12843 }
12844 else if (is_capture_proxy (parm_pack))
12845 {
12846 arg_pack = retrieve_local_specialization (parm_pack);
12847 if (DECL_PACK_P (arg_pack))
12848 arg_pack = NULL_TREE;
12849 }
12850 else
12851 {
12852 int idx;
12853 template_parm_level_and_index (parm_pack, &level, &idx);
12854 if (level <= levels)
12855 arg_pack = TMPL_ARG (args, level, idx);
12856 }
12857
12858 orig_arg = arg_pack;
12859 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12860 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12861
12862 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12863 /* This can only happen if we forget to expand an argument
12864 pack somewhere else. Just return an error, silently. */
12865 {
12866 result = make_tree_vec (1);
12867 TREE_VEC_ELT (result, 0) = error_mark_node;
12868 return result;
12869 }
12870
12871 if (arg_pack)
12872 {
12873 int my_len =
12874 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12875
12876 /* Don't bother trying to do a partial substitution with
12877 incomplete packs; we'll try again after deduction. */
12878 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12879 return t;
12880
12881 if (len < 0)
12882 len = my_len;
12883 else if (len != my_len)
12884 {
12885 if (!(complain & tf_error))
12886 /* Fail quietly. */;
12887 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12888 error ("mismatched argument pack lengths while expanding %qT",
12889 pattern);
12890 else
12891 error ("mismatched argument pack lengths while expanding %qE",
12892 pattern);
12893 return error_mark_node;
12894 }
12895
12896 /* Keep track of the parameter packs and their corresponding
12897 argument packs. */
12898 packs = tree_cons (parm_pack, arg_pack, packs);
12899 TREE_TYPE (packs) = orig_arg;
12900 }
12901 else
12902 {
12903 /* We can't substitute for this parameter pack. We use a flag as
12904 well as the missing_level counter because function parameter
12905 packs don't have a level. */
12906 gcc_assert (processing_template_decl || is_auto (parm_pack));
12907 unsubstituted_packs = true;
12908 }
12909 }
12910
12911 /* If the expansion is just T..., return the matching argument pack, unless
12912 we need to call convert_from_reference on all the elements. This is an
12913 important optimization; see c++/68422. */
12914 if (!unsubstituted_packs
12915 && TREE_PURPOSE (packs) == pattern)
12916 {
12917 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12918
12919 /* If the argument pack is a single pack expansion, pull it out. */
12920 if (TREE_VEC_LENGTH (args) == 1
12921 && pack_expansion_args_count (args))
12922 return TREE_VEC_ELT (args, 0);
12923
12924 /* Types need no adjustment, nor does sizeof..., and if we still have
12925 some pack expansion args we won't do anything yet. */
12926 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12927 || PACK_EXPANSION_SIZEOF_P (t)
12928 || pack_expansion_args_count (args))
12929 return args;
12930 /* Also optimize expression pack expansions if we can tell that the
12931 elements won't have reference type. */
12932 tree type = TREE_TYPE (pattern);
12933 if (type && !TYPE_REF_P (type)
12934 && !PACK_EXPANSION_P (type)
12935 && !WILDCARD_TYPE_P (type))
12936 return args;
12937 /* Otherwise use the normal path so we get convert_from_reference. */
12938 }
12939
12940 /* We cannot expand this expansion expression, because we don't have
12941 all of the argument packs we need. */
12942 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12943 {
12944 /* We got some full packs, but we can't substitute them in until we
12945 have values for all the packs. So remember these until then. */
12946
12947 t = make_pack_expansion (pattern, complain);
12948 PACK_EXPANSION_EXTRA_ARGS (t)
12949 = build_extra_args (pattern, args, complain);
12950 return t;
12951 }
12952
12953 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12954 type, so create our own local specializations map; the current map is
12955 either NULL or (in the case of recursive unification) might have
12956 bindings that we don't want to use or alter. */
12957 local_specialization_stack lss (need_local_specializations
12958 ? lss_blank : lss_nop);
12959
12960 if (unsubstituted_packs)
12961 {
12962 /* There were no real arguments, we're just replacing a parameter
12963 pack with another version of itself. Substitute into the
12964 pattern and return a PACK_EXPANSION_*. The caller will need to
12965 deal with that. */
12966 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12967 t = tsubst_expr (pattern, args, complain, in_decl,
12968 /*integral_constant_expression_p=*/false);
12969 else
12970 t = tsubst (pattern, args, complain, in_decl);
12971 t = make_pack_expansion (t, complain);
12972 return t;
12973 }
12974
12975 gcc_assert (len >= 0);
12976
12977 /* For each argument in each argument pack, substitute into the
12978 pattern. */
12979 result = make_tree_vec (len);
12980 tree elem_args = copy_template_args (args);
12981 for (i = 0; i < len; ++i)
12982 {
12983 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12984 i,
12985 elem_args, complain,
12986 in_decl);
12987 TREE_VEC_ELT (result, i) = t;
12988 if (t == error_mark_node)
12989 {
12990 result = error_mark_node;
12991 break;
12992 }
12993 }
12994
12995 /* Update ARGS to restore the substitution from parameter packs to
12996 their argument packs. */
12997 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12998 {
12999 tree parm = TREE_PURPOSE (pack);
13000
13001 if (TREE_CODE (parm) == PARM_DECL
13002 || VAR_P (parm)
13003 || TREE_CODE (parm) == FIELD_DECL)
13004 register_local_specialization (TREE_TYPE (pack), parm);
13005 else
13006 {
13007 int idx, level;
13008
13009 if (TREE_VALUE (pack) == NULL_TREE)
13010 continue;
13011
13012 template_parm_level_and_index (parm, &level, &idx);
13013
13014 /* Update the corresponding argument. */
13015 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13016 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13017 TREE_TYPE (pack);
13018 else
13019 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13020 }
13021 }
13022
13023 /* If the dependent pack arguments were such that we end up with only a
13024 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13025 if (len == 1 && TREE_CODE (result) == TREE_VEC
13026 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13027 return TREE_VEC_ELT (result, 0);
13028
13029 return result;
13030 }
13031
13032 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13033 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13034 parameter packs; all parms generated from a function parameter pack will
13035 have the same DECL_PARM_INDEX. */
13036
13037 tree
13038 get_pattern_parm (tree parm, tree tmpl)
13039 {
13040 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13041 tree patparm;
13042
13043 if (DECL_ARTIFICIAL (parm))
13044 {
13045 for (patparm = DECL_ARGUMENTS (pattern);
13046 patparm; patparm = DECL_CHAIN (patparm))
13047 if (DECL_ARTIFICIAL (patparm)
13048 && DECL_NAME (parm) == DECL_NAME (patparm))
13049 break;
13050 }
13051 else
13052 {
13053 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13054 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13055 gcc_assert (DECL_PARM_INDEX (patparm)
13056 == DECL_PARM_INDEX (parm));
13057 }
13058
13059 return patparm;
13060 }
13061
13062 /* Make an argument pack out of the TREE_VEC VEC. */
13063
13064 static tree
13065 make_argument_pack (tree vec)
13066 {
13067 tree pack;
13068
13069 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13070 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13071 else
13072 {
13073 pack = make_node (NONTYPE_ARGUMENT_PACK);
13074 TREE_CONSTANT (pack) = 1;
13075 }
13076 SET_ARGUMENT_PACK_ARGS (pack, vec);
13077 return pack;
13078 }
13079
13080 /* Return an exact copy of template args T that can be modified
13081 independently. */
13082
13083 static tree
13084 copy_template_args (tree t)
13085 {
13086 if (t == error_mark_node)
13087 return t;
13088
13089 int len = TREE_VEC_LENGTH (t);
13090 tree new_vec = make_tree_vec (len);
13091
13092 for (int i = 0; i < len; ++i)
13093 {
13094 tree elt = TREE_VEC_ELT (t, i);
13095 if (elt && TREE_CODE (elt) == TREE_VEC)
13096 elt = copy_template_args (elt);
13097 TREE_VEC_ELT (new_vec, i) = elt;
13098 }
13099
13100 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13101 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13102
13103 return new_vec;
13104 }
13105
13106 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13107
13108 tree
13109 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13110 tree in_decl)
13111 {
13112 /* Substitute into each of the arguments. */
13113 tree new_arg = TYPE_P (orig_arg)
13114 ? cxx_make_type (TREE_CODE (orig_arg))
13115 : make_node (TREE_CODE (orig_arg));
13116
13117 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13118 args, complain, in_decl);
13119 if (pack_args == error_mark_node)
13120 new_arg = error_mark_node;
13121 else
13122 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13123
13124 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13125 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13126
13127 return new_arg;
13128 }
13129
13130 /* Substitute ARGS into the vector or list of template arguments T. */
13131
13132 tree
13133 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13134 {
13135 tree orig_t = t;
13136 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13137 tree *elts;
13138
13139 if (t == error_mark_node)
13140 return error_mark_node;
13141
13142 len = TREE_VEC_LENGTH (t);
13143 elts = XALLOCAVEC (tree, len);
13144
13145 for (i = 0; i < len; i++)
13146 {
13147 tree orig_arg = TREE_VEC_ELT (t, i);
13148 tree new_arg;
13149
13150 if (TREE_CODE (orig_arg) == TREE_VEC)
13151 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13152 else if (PACK_EXPANSION_P (orig_arg))
13153 {
13154 /* Substitute into an expansion expression. */
13155 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13156
13157 if (TREE_CODE (new_arg) == TREE_VEC)
13158 /* Add to the expanded length adjustment the number of
13159 expanded arguments. We subtract one from this
13160 measurement, because the argument pack expression
13161 itself is already counted as 1 in
13162 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13163 the argument pack is empty. */
13164 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13165 }
13166 else if (ARGUMENT_PACK_P (orig_arg))
13167 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13168 else
13169 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13170
13171 if (new_arg == error_mark_node)
13172 return error_mark_node;
13173
13174 elts[i] = new_arg;
13175 if (new_arg != orig_arg)
13176 need_new = 1;
13177 }
13178
13179 if (!need_new)
13180 return t;
13181
13182 /* Make space for the expanded arguments coming from template
13183 argument packs. */
13184 t = make_tree_vec (len + expanded_len_adjust);
13185 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13186 arguments for a member template.
13187 In that case each TREE_VEC in ORIG_T represents a level of template
13188 arguments, and ORIG_T won't carry any non defaulted argument count.
13189 It will rather be the nested TREE_VECs that will carry one.
13190 In other words, ORIG_T carries a non defaulted argument count only
13191 if it doesn't contain any nested TREE_VEC. */
13192 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13193 {
13194 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13195 count += expanded_len_adjust;
13196 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13197 }
13198 for (i = 0, out = 0; i < len; i++)
13199 {
13200 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13201 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13202 && TREE_CODE (elts[i]) == TREE_VEC)
13203 {
13204 int idx;
13205
13206 /* Now expand the template argument pack "in place". */
13207 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13208 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13209 }
13210 else
13211 {
13212 TREE_VEC_ELT (t, out) = elts[i];
13213 out++;
13214 }
13215 }
13216
13217 return t;
13218 }
13219
13220 /* Substitute ARGS into one level PARMS of template parameters. */
13221
13222 static tree
13223 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13224 {
13225 if (parms == error_mark_node)
13226 return error_mark_node;
13227
13228 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13229
13230 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13231 {
13232 tree tuple = TREE_VEC_ELT (parms, i);
13233
13234 if (tuple == error_mark_node)
13235 continue;
13236
13237 TREE_VEC_ELT (new_vec, i) =
13238 tsubst_template_parm (tuple, args, complain);
13239 }
13240
13241 return new_vec;
13242 }
13243
13244 /* Return the result of substituting ARGS into the template parameters
13245 given by PARMS. If there are m levels of ARGS and m + n levels of
13246 PARMS, then the result will contain n levels of PARMS. For
13247 example, if PARMS is `template <class T> template <class U>
13248 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13249 result will be `template <int*, double, class V>'. */
13250
13251 static tree
13252 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13253 {
13254 tree r = NULL_TREE;
13255 tree* new_parms;
13256
13257 /* When substituting into a template, we must set
13258 PROCESSING_TEMPLATE_DECL as the template parameters may be
13259 dependent if they are based on one-another, and the dependency
13260 predicates are short-circuit outside of templates. */
13261 ++processing_template_decl;
13262
13263 for (new_parms = &r;
13264 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13265 new_parms = &(TREE_CHAIN (*new_parms)),
13266 parms = TREE_CHAIN (parms))
13267 {
13268 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13269 args, complain);
13270 *new_parms =
13271 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13272 - TMPL_ARGS_DEPTH (args)),
13273 new_vec, NULL_TREE);
13274 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13275 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13276 }
13277
13278 --processing_template_decl;
13279
13280 return r;
13281 }
13282
13283 /* Return the result of substituting ARGS into one template parameter
13284 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13285 parameter and which TREE_PURPOSE is the default argument of the
13286 template parameter. */
13287
13288 static tree
13289 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13290 {
13291 tree default_value, parm_decl;
13292
13293 if (args == NULL_TREE
13294 || t == NULL_TREE
13295 || t == error_mark_node)
13296 return t;
13297
13298 gcc_assert (TREE_CODE (t) == TREE_LIST);
13299
13300 default_value = TREE_PURPOSE (t);
13301 parm_decl = TREE_VALUE (t);
13302 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13303
13304 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13305 if (TREE_CODE (parm_decl) == PARM_DECL
13306 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13307 parm_decl = error_mark_node;
13308 default_value = tsubst_template_arg (default_value, args,
13309 complain, NULL_TREE);
13310 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13311
13312 tree r = build_tree_list (default_value, parm_decl);
13313 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13314 return r;
13315 }
13316
13317 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13318 type T. If T is not an aggregate or enumeration type, it is
13319 handled as if by tsubst. IN_DECL is as for tsubst. If
13320 ENTERING_SCOPE is nonzero, T is the context for a template which
13321 we are presently tsubst'ing. Return the substituted value. */
13322
13323 static tree
13324 tsubst_aggr_type (tree t,
13325 tree args,
13326 tsubst_flags_t complain,
13327 tree in_decl,
13328 int entering_scope)
13329 {
13330 if (t == NULL_TREE)
13331 return NULL_TREE;
13332
13333 switch (TREE_CODE (t))
13334 {
13335 case RECORD_TYPE:
13336 if (TYPE_PTRMEMFUNC_P (t))
13337 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13338
13339 /* Fall through. */
13340 case ENUMERAL_TYPE:
13341 case UNION_TYPE:
13342 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13343 {
13344 tree argvec;
13345 tree context;
13346 tree r;
13347
13348 /* In "sizeof(X<I>)" we need to evaluate "I". */
13349 cp_evaluated ev;
13350
13351 /* First, determine the context for the type we are looking
13352 up. */
13353 context = TYPE_CONTEXT (t);
13354 if (context && TYPE_P (context))
13355 {
13356 context = tsubst_aggr_type (context, args, complain,
13357 in_decl, /*entering_scope=*/1);
13358 /* If context is a nested class inside a class template,
13359 it may still need to be instantiated (c++/33959). */
13360 context = complete_type (context);
13361 }
13362
13363 /* Then, figure out what arguments are appropriate for the
13364 type we are trying to find. For example, given:
13365
13366 template <class T> struct S;
13367 template <class T, class U> void f(T, U) { S<U> su; }
13368
13369 and supposing that we are instantiating f<int, double>,
13370 then our ARGS will be {int, double}, but, when looking up
13371 S we only want {double}. */
13372 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13373 complain, in_decl);
13374 if (argvec == error_mark_node)
13375 r = error_mark_node;
13376 else
13377 {
13378 r = lookup_template_class (t, argvec, in_decl, context,
13379 entering_scope, complain);
13380 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13381 }
13382
13383 return r;
13384 }
13385 else
13386 /* This is not a template type, so there's nothing to do. */
13387 return t;
13388
13389 default:
13390 return tsubst (t, args, complain, in_decl);
13391 }
13392 }
13393
13394 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13395
13396 /* Substitute into the default argument ARG (a default argument for
13397 FN), which has the indicated TYPE. */
13398
13399 tree
13400 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13401 tsubst_flags_t complain)
13402 {
13403 int errs = errorcount + sorrycount;
13404
13405 /* This can happen in invalid code. */
13406 if (TREE_CODE (arg) == DEFERRED_PARSE)
13407 return arg;
13408
13409 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13410 parm = chain_index (parmnum, parm);
13411 tree parmtype = TREE_TYPE (parm);
13412 if (DECL_BY_REFERENCE (parm))
13413 parmtype = TREE_TYPE (parmtype);
13414 if (parmtype == error_mark_node)
13415 return error_mark_node;
13416
13417 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13418
13419 tree *slot;
13420 if (defarg_inst && (slot = defarg_inst->get (parm)))
13421 return *slot;
13422
13423 /* This default argument came from a template. Instantiate the
13424 default argument here, not in tsubst. In the case of
13425 something like:
13426
13427 template <class T>
13428 struct S {
13429 static T t();
13430 void f(T = t());
13431 };
13432
13433 we must be careful to do name lookup in the scope of S<T>,
13434 rather than in the current class. */
13435 push_to_top_level ();
13436 push_access_scope (fn);
13437 push_deferring_access_checks (dk_no_deferred);
13438 start_lambda_scope (parm);
13439
13440 /* The default argument expression may cause implicitly defined
13441 member functions to be synthesized, which will result in garbage
13442 collection. We must treat this situation as if we were within
13443 the body of function so as to avoid collecting live data on the
13444 stack. */
13445 ++function_depth;
13446 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13447 complain, NULL_TREE,
13448 /*integral_constant_expression_p=*/false);
13449 --function_depth;
13450
13451 finish_lambda_scope ();
13452
13453 /* Make sure the default argument is reasonable. */
13454 arg = check_default_argument (type, arg, complain);
13455
13456 if (errorcount+sorrycount > errs
13457 && (complain & tf_warning_or_error))
13458 inform (input_location,
13459 " when instantiating default argument for call to %qD", fn);
13460
13461 pop_deferring_access_checks ();
13462 pop_access_scope (fn);
13463 pop_from_top_level ();
13464
13465 if (arg != error_mark_node && !cp_unevaluated_operand)
13466 {
13467 if (!defarg_inst)
13468 defarg_inst = decl_tree_cache_map::create_ggc (37);
13469 defarg_inst->put (parm, arg);
13470 }
13471
13472 return arg;
13473 }
13474
13475 /* Substitute into all the default arguments for FN. */
13476
13477 static void
13478 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13479 {
13480 tree arg;
13481 tree tmpl_args;
13482
13483 tmpl_args = DECL_TI_ARGS (fn);
13484
13485 /* If this function is not yet instantiated, we certainly don't need
13486 its default arguments. */
13487 if (uses_template_parms (tmpl_args))
13488 return;
13489 /* Don't do this again for clones. */
13490 if (DECL_CLONED_FUNCTION_P (fn))
13491 return;
13492
13493 int i = 0;
13494 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13495 arg;
13496 arg = TREE_CHAIN (arg), ++i)
13497 if (TREE_PURPOSE (arg))
13498 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13499 TREE_VALUE (arg),
13500 TREE_PURPOSE (arg),
13501 complain);
13502 }
13503
13504 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13505 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13506
13507 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13508
13509 void
13510 store_explicit_specifier (tree v, tree t)
13511 {
13512 if (!explicit_specifier_map)
13513 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13514 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13515 explicit_specifier_map->put (v, t);
13516 }
13517
13518 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13519
13520 static tree
13521 lookup_explicit_specifier (tree v)
13522 {
13523 return *explicit_specifier_map->get (v);
13524 }
13525
13526 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13527 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13528 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13529 identical to T. */
13530
13531 static tree
13532 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13533 tree raises, tsubst_flags_t complain)
13534 {
13535 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13536
13537 tree new_type;
13538 if (TREE_CODE (t) == FUNCTION_TYPE)
13539 {
13540 new_type = build_function_type (return_type, arg_types);
13541 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13542 }
13543 else
13544 {
13545 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13546 /* Don't pick up extra function qualifiers from the basetype. */
13547 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13548 if (! MAYBE_CLASS_TYPE_P (r))
13549 {
13550 /* [temp.deduct]
13551
13552 Type deduction may fail for any of the following
13553 reasons:
13554
13555 -- Attempting to create "pointer to member of T" when T
13556 is not a class type. */
13557 if (complain & tf_error)
13558 error ("creating pointer to member function of non-class type %qT",
13559 r);
13560 return error_mark_node;
13561 }
13562
13563 new_type = build_method_type_directly (r, return_type,
13564 TREE_CHAIN (arg_types));
13565 }
13566 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13567
13568 cp_ref_qualifier rqual = type_memfn_rqual (t);
13569 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13570 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13571 }
13572
13573 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13574 each of its formal parameters. If there is a disagreement then rebuild
13575 DECL's function type according to its formal parameter types, as part of a
13576 resolution for Core issues 1001/1322. */
13577
13578 static void
13579 maybe_rebuild_function_decl_type (tree decl)
13580 {
13581 bool function_type_needs_rebuilding = false;
13582 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13583 {
13584 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13585 while (parm_type_list && parm_type_list != void_list_node)
13586 {
13587 tree parm_type = TREE_VALUE (parm_type_list);
13588 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13589 if (!same_type_p (parm_type, formal_parm_type_unqual))
13590 {
13591 function_type_needs_rebuilding = true;
13592 break;
13593 }
13594
13595 parm_list = DECL_CHAIN (parm_list);
13596 parm_type_list = TREE_CHAIN (parm_type_list);
13597 }
13598 }
13599
13600 if (!function_type_needs_rebuilding)
13601 return;
13602
13603 const tree fntype = TREE_TYPE (decl);
13604 tree parm_list = DECL_ARGUMENTS (decl);
13605 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13606 tree new_parm_type_list = NULL_TREE;
13607 tree *q = &new_parm_type_list;
13608 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13609 {
13610 *q = copy_node (old_parm_type_list);
13611 parm_list = DECL_CHAIN (parm_list);
13612 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13613 q = &TREE_CHAIN (*q);
13614 }
13615 while (old_parm_type_list && old_parm_type_list != void_list_node)
13616 {
13617 *q = copy_node (old_parm_type_list);
13618 tree *new_parm_type = &TREE_VALUE (*q);
13619 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13620 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13621 *new_parm_type = formal_parm_type_unqual;
13622
13623 parm_list = DECL_CHAIN (parm_list);
13624 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13625 q = &TREE_CHAIN (*q);
13626 }
13627 if (old_parm_type_list == void_list_node)
13628 *q = void_list_node;
13629
13630 TREE_TYPE (decl)
13631 = rebuild_function_or_method_type (fntype,
13632 TREE_TYPE (fntype), new_parm_type_list,
13633 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13634 }
13635
13636 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13637
13638 static tree
13639 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13640 tree lambda_fntype)
13641 {
13642 tree gen_tmpl, argvec;
13643 hashval_t hash = 0;
13644 tree in_decl = t;
13645
13646 /* Nobody should be tsubst'ing into non-template functions. */
13647 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13648
13649 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13650 {
13651 /* If T is not dependent, just return it. */
13652 if (!uses_template_parms (DECL_TI_ARGS (t))
13653 && !LAMBDA_FUNCTION_P (t))
13654 return t;
13655
13656 /* Calculate the most general template of which R is a
13657 specialization. */
13658 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13659
13660 /* We're substituting a lambda function under tsubst_lambda_expr but not
13661 directly from it; find the matching function we're already inside.
13662 But don't do this if T is a generic lambda with a single level of
13663 template parms, as in that case we're doing a normal instantiation. */
13664 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13665 && (!generic_lambda_fn_p (t)
13666 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13667 return enclosing_instantiation_of (t);
13668
13669 /* Calculate the complete set of arguments used to
13670 specialize R. */
13671 argvec = tsubst_template_args (DECL_TI_ARGS
13672 (DECL_TEMPLATE_RESULT
13673 (DECL_TI_TEMPLATE (t))),
13674 args, complain, in_decl);
13675 if (argvec == error_mark_node)
13676 return error_mark_node;
13677
13678 /* Check to see if we already have this specialization. */
13679 if (!lambda_fntype)
13680 {
13681 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13682 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13683 return spec;
13684 }
13685
13686 /* We can see more levels of arguments than parameters if
13687 there was a specialization of a member template, like
13688 this:
13689
13690 template <class T> struct S { template <class U> void f(); }
13691 template <> template <class U> void S<int>::f(U);
13692
13693 Here, we'll be substituting into the specialization,
13694 because that's where we can find the code we actually
13695 want to generate, but we'll have enough arguments for
13696 the most general template.
13697
13698 We also deal with the peculiar case:
13699
13700 template <class T> struct S {
13701 template <class U> friend void f();
13702 };
13703 template <class U> void f() {}
13704 template S<int>;
13705 template void f<double>();
13706
13707 Here, the ARGS for the instantiation of will be {int,
13708 double}. But, we only need as many ARGS as there are
13709 levels of template parameters in CODE_PATTERN. We are
13710 careful not to get fooled into reducing the ARGS in
13711 situations like:
13712
13713 template <class T> struct S { template <class U> void f(U); }
13714 template <class T> template <> void S<T>::f(int) {}
13715
13716 which we can spot because the pattern will be a
13717 specialization in this case. */
13718 int args_depth = TMPL_ARGS_DEPTH (args);
13719 int parms_depth =
13720 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13721
13722 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13723 args = get_innermost_template_args (args, parms_depth);
13724 }
13725 else
13726 {
13727 /* This special case arises when we have something like this:
13728
13729 template <class T> struct S {
13730 friend void f<int>(int, double);
13731 };
13732
13733 Here, the DECL_TI_TEMPLATE for the friend declaration
13734 will be an IDENTIFIER_NODE. We are being called from
13735 tsubst_friend_function, and we want only to create a
13736 new decl (R) with appropriate types so that we can call
13737 determine_specialization. */
13738 gen_tmpl = NULL_TREE;
13739 argvec = NULL_TREE;
13740 }
13741
13742 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13743 : NULL_TREE);
13744 tree ctx = closure ? closure : DECL_CONTEXT (t);
13745 bool member = ctx && TYPE_P (ctx);
13746
13747 if (member && !closure)
13748 ctx = tsubst_aggr_type (ctx, args,
13749 complain, t, /*entering_scope=*/1);
13750
13751 tree type = (lambda_fntype ? lambda_fntype
13752 : tsubst (TREE_TYPE (t), args,
13753 complain | tf_fndecl_type, in_decl));
13754 if (type == error_mark_node)
13755 return error_mark_node;
13756
13757 /* If we hit excessive deduction depth, the type is bogus even if
13758 it isn't error_mark_node, so don't build a decl. */
13759 if (excessive_deduction_depth)
13760 return error_mark_node;
13761
13762 /* We do NOT check for matching decls pushed separately at this
13763 point, as they may not represent instantiations of this
13764 template, and in any case are considered separate under the
13765 discrete model. */
13766 tree r = copy_decl (t);
13767 DECL_USE_TEMPLATE (r) = 0;
13768 TREE_TYPE (r) = type;
13769 /* Clear out the mangled name and RTL for the instantiation. */
13770 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13771 SET_DECL_RTL (r, NULL);
13772 /* Leave DECL_INITIAL set on deleted instantiations. */
13773 if (!DECL_DELETED_FN (r))
13774 DECL_INITIAL (r) = NULL_TREE;
13775 DECL_CONTEXT (r) = ctx;
13776
13777 /* Handle explicit(dependent-expr). */
13778 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13779 {
13780 tree spec = lookup_explicit_specifier (t);
13781 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13782 /*function_p=*/false,
13783 /*i_c_e_p=*/true);
13784 spec = build_explicit_specifier (spec, complain);
13785 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13786 }
13787
13788 /* OpenMP UDRs have the only argument a reference to the declared
13789 type. We want to diagnose if the declared type is a reference,
13790 which is invalid, but as references to references are usually
13791 quietly merged, diagnose it here. */
13792 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13793 {
13794 tree argtype
13795 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13796 argtype = tsubst (argtype, args, complain, in_decl);
13797 if (TYPE_REF_P (argtype))
13798 error_at (DECL_SOURCE_LOCATION (t),
13799 "reference type %qT in "
13800 "%<#pragma omp declare reduction%>", argtype);
13801 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13802 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13803 argtype);
13804 }
13805
13806 if (member && DECL_CONV_FN_P (r))
13807 /* Type-conversion operator. Reconstruct the name, in
13808 case it's the name of one of the template's parameters. */
13809 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13810
13811 tree parms = DECL_ARGUMENTS (t);
13812 if (closure)
13813 parms = DECL_CHAIN (parms);
13814 parms = tsubst (parms, args, complain, t);
13815 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13816 DECL_CONTEXT (parm) = r;
13817 if (closure)
13818 {
13819 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13820 DECL_NAME (tparm) = closure_identifier;
13821 DECL_CHAIN (tparm) = parms;
13822 parms = tparm;
13823 }
13824 DECL_ARGUMENTS (r) = parms;
13825 DECL_RESULT (r) = NULL_TREE;
13826
13827 maybe_rebuild_function_decl_type (r);
13828
13829 TREE_STATIC (r) = 0;
13830 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13831 DECL_EXTERNAL (r) = 1;
13832 /* If this is an instantiation of a function with internal
13833 linkage, we already know what object file linkage will be
13834 assigned to the instantiation. */
13835 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13836 DECL_DEFER_OUTPUT (r) = 0;
13837 DECL_CHAIN (r) = NULL_TREE;
13838 DECL_PENDING_INLINE_INFO (r) = 0;
13839 DECL_PENDING_INLINE_P (r) = 0;
13840 DECL_SAVED_TREE (r) = NULL_TREE;
13841 DECL_STRUCT_FUNCTION (r) = NULL;
13842 TREE_USED (r) = 0;
13843 /* We'll re-clone as appropriate in instantiate_template. */
13844 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13845
13846 /* If we aren't complaining now, return on error before we register
13847 the specialization so that we'll complain eventually. */
13848 if ((complain & tf_error) == 0
13849 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13850 && !grok_op_properties (r, /*complain=*/false))
13851 return error_mark_node;
13852
13853 /* Associate the constraints directly with the instantiation. We
13854 don't substitute through the constraints; that's only done when
13855 they are checked. */
13856 if (tree ci = get_constraints (t))
13857 /* Unless we're regenerating a lambda, in which case we'll set the
13858 lambda's constraints in tsubst_lambda_expr. */
13859 if (!lambda_fntype)
13860 set_constraints (r, ci);
13861
13862 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13863 SET_DECL_FRIEND_CONTEXT (r,
13864 tsubst (DECL_FRIEND_CONTEXT (t),
13865 args, complain, in_decl));
13866
13867 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13868 this in the special friend case mentioned above where
13869 GEN_TMPL is NULL. */
13870 if (gen_tmpl && !closure)
13871 {
13872 DECL_TEMPLATE_INFO (r)
13873 = build_template_info (gen_tmpl, argvec);
13874 SET_DECL_IMPLICIT_INSTANTIATION (r);
13875
13876 tree new_r
13877 = register_specialization (r, gen_tmpl, argvec, false, hash);
13878 if (new_r != r)
13879 /* We instantiated this while substituting into
13880 the type earlier (template/friend54.C). */
13881 return new_r;
13882
13883 /* We're not supposed to instantiate default arguments
13884 until they are called, for a template. But, for a
13885 declaration like:
13886
13887 template <class T> void f ()
13888 { extern void g(int i = T()); }
13889
13890 we should do the substitution when the template is
13891 instantiated. We handle the member function case in
13892 instantiate_class_template since the default arguments
13893 might refer to other members of the class. */
13894 if (!member
13895 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13896 && !uses_template_parms (argvec))
13897 tsubst_default_arguments (r, complain);
13898 }
13899 else
13900 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13901
13902 /* Copy the list of befriending classes. */
13903 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13904 *friends;
13905 friends = &TREE_CHAIN (*friends))
13906 {
13907 *friends = copy_node (*friends);
13908 TREE_VALUE (*friends)
13909 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13910 }
13911
13912 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13913 {
13914 maybe_retrofit_in_chrg (r);
13915 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13916 return error_mark_node;
13917 /* If this is an instantiation of a member template, clone it.
13918 If it isn't, that'll be handled by
13919 clone_constructors_and_destructors. */
13920 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13921 clone_function_decl (r, /*update_methods=*/false);
13922 }
13923 else if ((complain & tf_error) != 0
13924 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13925 && !grok_op_properties (r, /*complain=*/true))
13926 return error_mark_node;
13927
13928 /* Possibly limit visibility based on template args. */
13929 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13930 if (DECL_VISIBILITY_SPECIFIED (t))
13931 {
13932 DECL_VISIBILITY_SPECIFIED (r) = 0;
13933 DECL_ATTRIBUTES (r)
13934 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13935 }
13936 determine_visibility (r);
13937 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13938 && !processing_template_decl)
13939 defaulted_late_check (r);
13940
13941 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13942 args, complain, in_decl);
13943 if (flag_openmp)
13944 if (tree attr = lookup_attribute ("omp declare variant base",
13945 DECL_ATTRIBUTES (r)))
13946 omp_declare_variant_finalize (r, attr);
13947
13948 return r;
13949 }
13950
13951 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13952
13953 static tree
13954 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13955 tree lambda_fntype)
13956 {
13957 /* We can get here when processing a member function template,
13958 member class template, or template template parameter. */
13959 tree decl = DECL_TEMPLATE_RESULT (t);
13960 tree in_decl = t;
13961 tree spec;
13962 tree tmpl_args;
13963 tree full_args;
13964 tree r;
13965 hashval_t hash = 0;
13966
13967 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13968 {
13969 /* Template template parameter is treated here. */
13970 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13971 if (new_type == error_mark_node)
13972 r = error_mark_node;
13973 /* If we get a real template back, return it. This can happen in
13974 the context of most_specialized_partial_spec. */
13975 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13976 r = new_type;
13977 else
13978 /* The new TEMPLATE_DECL was built in
13979 reduce_template_parm_level. */
13980 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13981 return r;
13982 }
13983
13984 if (!lambda_fntype)
13985 {
13986 /* We might already have an instance of this template.
13987 The ARGS are for the surrounding class type, so the
13988 full args contain the tsubst'd args for the context,
13989 plus the innermost args from the template decl. */
13990 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13991 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13992 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13993 /* Because this is a template, the arguments will still be
13994 dependent, even after substitution. If
13995 PROCESSING_TEMPLATE_DECL is not set, the dependency
13996 predicates will short-circuit. */
13997 ++processing_template_decl;
13998 full_args = tsubst_template_args (tmpl_args, args,
13999 complain, in_decl);
14000 --processing_template_decl;
14001 if (full_args == error_mark_node)
14002 return error_mark_node;
14003
14004 /* If this is a default template template argument,
14005 tsubst might not have changed anything. */
14006 if (full_args == tmpl_args)
14007 return t;
14008
14009 hash = hash_tmpl_and_args (t, full_args);
14010 spec = retrieve_specialization (t, full_args, hash);
14011 if (spec != NULL_TREE)
14012 {
14013 if (TYPE_P (spec))
14014 /* Type partial instantiations are stored as the type by
14015 lookup_template_class_1, not here as the template. */
14016 spec = CLASSTYPE_TI_TEMPLATE (spec);
14017 return spec;
14018 }
14019 }
14020
14021 /* Make a new template decl. It will be similar to the
14022 original, but will record the current template arguments.
14023 We also create a new function declaration, which is just
14024 like the old one, but points to this new template, rather
14025 than the old one. */
14026 r = copy_decl (t);
14027 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14028 DECL_CHAIN (r) = NULL_TREE;
14029
14030 // Build new template info linking to the original template decl.
14031 if (!lambda_fntype)
14032 {
14033 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14034 SET_DECL_IMPLICIT_INSTANTIATION (r);
14035 }
14036 else
14037 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14038
14039 /* The template parameters for this new template are all the
14040 template parameters for the old template, except the
14041 outermost level of parameters. */
14042 DECL_TEMPLATE_PARMS (r)
14043 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14044 complain);
14045
14046 bool class_p = false;
14047 tree inner = decl;
14048 ++processing_template_decl;
14049 if (TREE_CODE (inner) == FUNCTION_DECL)
14050 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14051 else
14052 {
14053 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14054 {
14055 class_p = true;
14056 inner = TREE_TYPE (inner);
14057 }
14058 inner = tsubst (inner, args, complain, in_decl);
14059 }
14060 --processing_template_decl;
14061 if (inner == error_mark_node)
14062 return error_mark_node;
14063
14064 if (class_p)
14065 {
14066 /* For a partial specialization, we need to keep pointing to
14067 the primary template. */
14068 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14069 CLASSTYPE_TI_TEMPLATE (inner) = r;
14070
14071 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14072 inner = TYPE_MAIN_DECL (inner);
14073 }
14074 else if (lambda_fntype)
14075 {
14076 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14077 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14078 }
14079 else
14080 {
14081 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14082 DECL_TI_TEMPLATE (inner) = r;
14083 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14084 }
14085
14086 DECL_TEMPLATE_RESULT (r) = inner;
14087 TREE_TYPE (r) = TREE_TYPE (inner);
14088 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14089
14090 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14091 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14092
14093 if (PRIMARY_TEMPLATE_P (t))
14094 DECL_PRIMARY_TEMPLATE (r) = r;
14095
14096 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14097 && !lambda_fntype)
14098 /* Record this non-type partial instantiation. */
14099 register_specialization (r, t,
14100 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14101 false, hash);
14102
14103 return r;
14104 }
14105
14106 /* True if FN is the op() for a lambda in an uninstantiated template. */
14107
14108 bool
14109 lambda_fn_in_template_p (tree fn)
14110 {
14111 if (!fn || !LAMBDA_FUNCTION_P (fn))
14112 return false;
14113 tree closure = DECL_CONTEXT (fn);
14114 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14115 }
14116
14117 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14118 which the above is true. */
14119
14120 bool
14121 instantiated_lambda_fn_p (tree fn)
14122 {
14123 if (!fn || !LAMBDA_FUNCTION_P (fn))
14124 return false;
14125 tree closure = DECL_CONTEXT (fn);
14126 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14127 return LAMBDA_EXPR_INSTANTIATED (lam);
14128 }
14129
14130 /* We're instantiating a variable from template function TCTX. Return the
14131 corresponding current enclosing scope. This gets complicated because lambda
14132 functions in templates are regenerated rather than instantiated, but generic
14133 lambda functions are subsequently instantiated. */
14134
14135 static tree
14136 enclosing_instantiation_of (tree otctx)
14137 {
14138 tree tctx = otctx;
14139 tree fn = current_function_decl;
14140 int lambda_count = 0;
14141
14142 for (; tctx && (lambda_fn_in_template_p (tctx)
14143 || instantiated_lambda_fn_p (tctx));
14144 tctx = decl_function_context (tctx))
14145 ++lambda_count;
14146 for (; fn; fn = decl_function_context (fn))
14147 {
14148 tree ofn = fn;
14149 int flambda_count = 0;
14150 for (; fn && instantiated_lambda_fn_p (fn);
14151 fn = decl_function_context (fn))
14152 ++flambda_count;
14153 if ((fn && DECL_TEMPLATE_INFO (fn))
14154 ? most_general_template (fn) != most_general_template (tctx)
14155 : fn != tctx)
14156 continue;
14157 if (flambda_count != lambda_count)
14158 {
14159 gcc_assert (flambda_count > lambda_count);
14160 for (; flambda_count > lambda_count; --flambda_count)
14161 ofn = decl_function_context (ofn);
14162 }
14163 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14164 || DECL_CONV_FN_P (ofn));
14165 return ofn;
14166 }
14167 gcc_unreachable ();
14168 }
14169
14170 /* Substitute the ARGS into the T, which is a _DECL. Return the
14171 result of the substitution. Issue error and warning messages under
14172 control of COMPLAIN. */
14173
14174 static tree
14175 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14176 {
14177 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14178 location_t saved_loc;
14179 tree r = NULL_TREE;
14180 tree in_decl = t;
14181 hashval_t hash = 0;
14182
14183 /* Set the filename and linenumber to improve error-reporting. */
14184 saved_loc = input_location;
14185 input_location = DECL_SOURCE_LOCATION (t);
14186
14187 switch (TREE_CODE (t))
14188 {
14189 case TEMPLATE_DECL:
14190 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14191 break;
14192
14193 case FUNCTION_DECL:
14194 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14195 break;
14196
14197 case PARM_DECL:
14198 {
14199 tree type = NULL_TREE;
14200 int i, len = 1;
14201 tree expanded_types = NULL_TREE;
14202 tree prev_r = NULL_TREE;
14203 tree first_r = NULL_TREE;
14204
14205 if (DECL_PACK_P (t))
14206 {
14207 /* If there is a local specialization that isn't a
14208 parameter pack, it means that we're doing a "simple"
14209 substitution from inside tsubst_pack_expansion. Just
14210 return the local specialization (which will be a single
14211 parm). */
14212 tree spec = retrieve_local_specialization (t);
14213 if (spec
14214 && TREE_CODE (spec) == PARM_DECL
14215 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14216 RETURN (spec);
14217
14218 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14219 the parameters in this function parameter pack. */
14220 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14221 complain, in_decl);
14222 if (TREE_CODE (expanded_types) == TREE_VEC)
14223 {
14224 len = TREE_VEC_LENGTH (expanded_types);
14225
14226 /* Zero-length parameter packs are boring. Just substitute
14227 into the chain. */
14228 if (len == 0 && !cp_unevaluated_operand)
14229 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14230 TREE_CHAIN (t)));
14231 }
14232 else
14233 {
14234 /* All we did was update the type. Make a note of that. */
14235 type = expanded_types;
14236 expanded_types = NULL_TREE;
14237 }
14238 }
14239
14240 /* Loop through all of the parameters we'll build. When T is
14241 a function parameter pack, LEN is the number of expanded
14242 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14243 r = NULL_TREE;
14244 for (i = 0; i < len; ++i)
14245 {
14246 prev_r = r;
14247 r = copy_node (t);
14248 if (DECL_TEMPLATE_PARM_P (t))
14249 SET_DECL_TEMPLATE_PARM_P (r);
14250
14251 if (expanded_types)
14252 /* We're on the Ith parameter of the function parameter
14253 pack. */
14254 {
14255 /* Get the Ith type. */
14256 type = TREE_VEC_ELT (expanded_types, i);
14257
14258 /* Rename the parameter to include the index. */
14259 DECL_NAME (r)
14260 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14261 }
14262 else if (!type)
14263 /* We're dealing with a normal parameter. */
14264 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14265
14266 type = type_decays_to (type);
14267 TREE_TYPE (r) = type;
14268 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14269
14270 if (DECL_INITIAL (r))
14271 {
14272 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14273 DECL_INITIAL (r) = TREE_TYPE (r);
14274 else
14275 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14276 complain, in_decl);
14277 }
14278
14279 DECL_CONTEXT (r) = NULL_TREE;
14280
14281 if (!DECL_TEMPLATE_PARM_P (r))
14282 DECL_ARG_TYPE (r) = type_passed_as (type);
14283
14284 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14285 args, complain, in_decl);
14286
14287 /* Keep track of the first new parameter we
14288 generate. That's what will be returned to the
14289 caller. */
14290 if (!first_r)
14291 first_r = r;
14292
14293 /* Build a proper chain of parameters when substituting
14294 into a function parameter pack. */
14295 if (prev_r)
14296 DECL_CHAIN (prev_r) = r;
14297 }
14298
14299 /* If cp_unevaluated_operand is set, we're just looking for a
14300 single dummy parameter, so don't keep going. */
14301 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14302 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14303 complain, DECL_CHAIN (t));
14304
14305 /* FIRST_R contains the start of the chain we've built. */
14306 r = first_r;
14307 }
14308 break;
14309
14310 case FIELD_DECL:
14311 {
14312 tree type = NULL_TREE;
14313 tree vec = NULL_TREE;
14314 tree expanded_types = NULL_TREE;
14315 int len = 1;
14316
14317 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14318 {
14319 /* This field is a lambda capture pack. Return a TREE_VEC of
14320 the expanded fields to instantiate_class_template_1. */
14321 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14322 complain, in_decl);
14323 if (TREE_CODE (expanded_types) == TREE_VEC)
14324 {
14325 len = TREE_VEC_LENGTH (expanded_types);
14326 vec = make_tree_vec (len);
14327 }
14328 else
14329 {
14330 /* All we did was update the type. Make a note of that. */
14331 type = expanded_types;
14332 expanded_types = NULL_TREE;
14333 }
14334 }
14335
14336 for (int i = 0; i < len; ++i)
14337 {
14338 r = copy_decl (t);
14339 if (expanded_types)
14340 {
14341 type = TREE_VEC_ELT (expanded_types, i);
14342 DECL_NAME (r)
14343 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14344 }
14345 else if (!type)
14346 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14347
14348 if (type == error_mark_node)
14349 RETURN (error_mark_node);
14350 TREE_TYPE (r) = type;
14351 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14352
14353 if (DECL_C_BIT_FIELD (r))
14354 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14355 number of bits. */
14356 DECL_BIT_FIELD_REPRESENTATIVE (r)
14357 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14358 complain, in_decl,
14359 /*integral_constant_expression_p=*/true);
14360 if (DECL_INITIAL (t))
14361 {
14362 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14363 NSDMI in perform_member_init. Still set DECL_INITIAL
14364 so that we know there is one. */
14365 DECL_INITIAL (r) = void_node;
14366 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14367 retrofit_lang_decl (r);
14368 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14369 }
14370 /* We don't have to set DECL_CONTEXT here; it is set by
14371 finish_member_declaration. */
14372 DECL_CHAIN (r) = NULL_TREE;
14373
14374 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14375 args, complain, in_decl);
14376
14377 if (vec)
14378 TREE_VEC_ELT (vec, i) = r;
14379 }
14380
14381 if (vec)
14382 r = vec;
14383 }
14384 break;
14385
14386 case USING_DECL:
14387 /* We reach here only for member using decls. We also need to check
14388 uses_template_parms because DECL_DEPENDENT_P is not set for a
14389 using-declaration that designates a member of the current
14390 instantiation (c++/53549). */
14391 if (DECL_DEPENDENT_P (t)
14392 || uses_template_parms (USING_DECL_SCOPE (t)))
14393 {
14394 tree scope = USING_DECL_SCOPE (t);
14395 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14396 if (PACK_EXPANSION_P (scope))
14397 {
14398 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14399 int len = TREE_VEC_LENGTH (vec);
14400 r = make_tree_vec (len);
14401 for (int i = 0; i < len; ++i)
14402 {
14403 tree escope = TREE_VEC_ELT (vec, i);
14404 tree elt = do_class_using_decl (escope, name);
14405 if (!elt)
14406 {
14407 r = error_mark_node;
14408 break;
14409 }
14410 else
14411 {
14412 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14413 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14414 }
14415 TREE_VEC_ELT (r, i) = elt;
14416 }
14417 }
14418 else
14419 {
14420 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14421 complain, in_decl);
14422 r = do_class_using_decl (inst_scope, name);
14423 if (!r)
14424 r = error_mark_node;
14425 else
14426 {
14427 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14428 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14429 }
14430 }
14431 }
14432 else
14433 {
14434 r = copy_node (t);
14435 DECL_CHAIN (r) = NULL_TREE;
14436 }
14437 break;
14438
14439 case TYPE_DECL:
14440 case VAR_DECL:
14441 {
14442 tree argvec = NULL_TREE;
14443 tree gen_tmpl = NULL_TREE;
14444 tree spec;
14445 tree tmpl = NULL_TREE;
14446 tree ctx;
14447 tree type = NULL_TREE;
14448 bool local_p;
14449
14450 if (TREE_TYPE (t) == error_mark_node)
14451 RETURN (error_mark_node);
14452
14453 if (TREE_CODE (t) == TYPE_DECL
14454 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14455 {
14456 /* If this is the canonical decl, we don't have to
14457 mess with instantiations, and often we can't (for
14458 typename, template type parms and such). Note that
14459 TYPE_NAME is not correct for the above test if
14460 we've copied the type for a typedef. */
14461 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14462 if (type == error_mark_node)
14463 RETURN (error_mark_node);
14464 r = TYPE_NAME (type);
14465 break;
14466 }
14467
14468 /* Check to see if we already have the specialization we
14469 need. */
14470 spec = NULL_TREE;
14471 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14472 {
14473 /* T is a static data member or namespace-scope entity.
14474 We have to substitute into namespace-scope variables
14475 (not just variable templates) because of cases like:
14476
14477 template <class T> void f() { extern T t; }
14478
14479 where the entity referenced is not known until
14480 instantiation time. */
14481 local_p = false;
14482 ctx = DECL_CONTEXT (t);
14483 if (DECL_CLASS_SCOPE_P (t))
14484 {
14485 ctx = tsubst_aggr_type (ctx, args,
14486 complain,
14487 in_decl, /*entering_scope=*/1);
14488 /* If CTX is unchanged, then T is in fact the
14489 specialization we want. That situation occurs when
14490 referencing a static data member within in its own
14491 class. We can use pointer equality, rather than
14492 same_type_p, because DECL_CONTEXT is always
14493 canonical... */
14494 if (ctx == DECL_CONTEXT (t)
14495 /* ... unless T is a member template; in which
14496 case our caller can be willing to create a
14497 specialization of that template represented
14498 by T. */
14499 && !(DECL_TI_TEMPLATE (t)
14500 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14501 spec = t;
14502 }
14503
14504 if (!spec)
14505 {
14506 tmpl = DECL_TI_TEMPLATE (t);
14507 gen_tmpl = most_general_template (tmpl);
14508 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14509 if (argvec != error_mark_node)
14510 argvec = (coerce_innermost_template_parms
14511 (DECL_TEMPLATE_PARMS (gen_tmpl),
14512 argvec, t, complain,
14513 /*all*/true, /*defarg*/true));
14514 if (argvec == error_mark_node)
14515 RETURN (error_mark_node);
14516 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14517 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14518 }
14519 }
14520 else
14521 {
14522 /* A local variable. */
14523 local_p = true;
14524 /* Subsequent calls to pushdecl will fill this in. */
14525 ctx = NULL_TREE;
14526 /* Unless this is a reference to a static variable from an
14527 enclosing function, in which case we need to fill it in now. */
14528 if (TREE_STATIC (t))
14529 {
14530 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14531 if (fn != current_function_decl)
14532 ctx = fn;
14533 }
14534 spec = retrieve_local_specialization (t);
14535 }
14536 /* If we already have the specialization we need, there is
14537 nothing more to do. */
14538 if (spec)
14539 {
14540 r = spec;
14541 break;
14542 }
14543
14544 /* Create a new node for the specialization we need. */
14545 if (type == NULL_TREE)
14546 {
14547 if (is_typedef_decl (t))
14548 type = DECL_ORIGINAL_TYPE (t);
14549 else
14550 type = TREE_TYPE (t);
14551 if (VAR_P (t)
14552 && VAR_HAD_UNKNOWN_BOUND (t)
14553 && type != error_mark_node)
14554 type = strip_array_domain (type);
14555 tree sub_args = args;
14556 if (tree auto_node = type_uses_auto (type))
14557 {
14558 /* Mask off any template args past the variable's context so we
14559 don't replace the auto with an unrelated argument. */
14560 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14561 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14562 if (extra > 0)
14563 /* This should never happen with the new lambda instantiation
14564 model, but keep the handling just in case. */
14565 gcc_assert (!CHECKING_P),
14566 sub_args = strip_innermost_template_args (args, extra);
14567 }
14568 type = tsubst (type, sub_args, complain, in_decl);
14569 /* Substituting the type might have recursively instantiated this
14570 same alias (c++/86171). */
14571 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14572 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14573 {
14574 r = spec;
14575 break;
14576 }
14577 }
14578 r = copy_decl (t);
14579 if (VAR_P (r))
14580 {
14581 DECL_INITIALIZED_P (r) = 0;
14582 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14583 if (type == error_mark_node)
14584 RETURN (error_mark_node);
14585 if (TREE_CODE (type) == FUNCTION_TYPE)
14586 {
14587 /* It may seem that this case cannot occur, since:
14588
14589 typedef void f();
14590 void g() { f x; }
14591
14592 declares a function, not a variable. However:
14593
14594 typedef void f();
14595 template <typename T> void g() { T t; }
14596 template void g<f>();
14597
14598 is an attempt to declare a variable with function
14599 type. */
14600 error ("variable %qD has function type",
14601 /* R is not yet sufficiently initialized, so we
14602 just use its name. */
14603 DECL_NAME (r));
14604 RETURN (error_mark_node);
14605 }
14606 type = complete_type (type);
14607 /* Wait until cp_finish_decl to set this again, to handle
14608 circular dependency (template/instantiate6.C). */
14609 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14610 type = check_var_type (DECL_NAME (r), type,
14611 DECL_SOURCE_LOCATION (r));
14612 if (DECL_HAS_VALUE_EXPR_P (t))
14613 {
14614 tree ve = DECL_VALUE_EXPR (t);
14615 /* If the DECL_VALUE_EXPR is converted to the declared type,
14616 preserve the identity so that gimplify_type_sizes works. */
14617 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14618 if (nop)
14619 ve = TREE_OPERAND (ve, 0);
14620 ve = tsubst_expr (ve, args, complain, in_decl,
14621 /*constant_expression_p=*/false);
14622 if (REFERENCE_REF_P (ve))
14623 {
14624 gcc_assert (TYPE_REF_P (type));
14625 ve = TREE_OPERAND (ve, 0);
14626 }
14627 if (nop)
14628 ve = build_nop (type, ve);
14629 else if (DECL_LANG_SPECIFIC (t)
14630 && DECL_OMP_PRIVATIZED_MEMBER (t)
14631 && TREE_CODE (ve) == COMPONENT_REF
14632 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14633 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14634 type = TREE_TYPE (ve);
14635 else
14636 gcc_checking_assert (TREE_TYPE (ve) == type);
14637 SET_DECL_VALUE_EXPR (r, ve);
14638 }
14639 if (CP_DECL_THREAD_LOCAL_P (r)
14640 && !processing_template_decl)
14641 set_decl_tls_model (r, decl_default_tls_model (r));
14642 }
14643 else if (DECL_SELF_REFERENCE_P (t))
14644 SET_DECL_SELF_REFERENCE_P (r);
14645 TREE_TYPE (r) = type;
14646 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14647 DECL_CONTEXT (r) = ctx;
14648 /* Clear out the mangled name and RTL for the instantiation. */
14649 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14650 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14651 SET_DECL_RTL (r, NULL);
14652 /* The initializer must not be expanded until it is required;
14653 see [temp.inst]. */
14654 DECL_INITIAL (r) = NULL_TREE;
14655 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14656 if (VAR_P (r))
14657 {
14658 if (DECL_LANG_SPECIFIC (r))
14659 SET_DECL_DEPENDENT_INIT_P (r, false);
14660
14661 SET_DECL_MODE (r, VOIDmode);
14662
14663 /* Possibly limit visibility based on template args. */
14664 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14665 if (DECL_VISIBILITY_SPECIFIED (t))
14666 {
14667 DECL_VISIBILITY_SPECIFIED (r) = 0;
14668 DECL_ATTRIBUTES (r)
14669 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14670 }
14671 determine_visibility (r);
14672 }
14673
14674 if (!local_p)
14675 {
14676 /* A static data member declaration is always marked
14677 external when it is declared in-class, even if an
14678 initializer is present. We mimic the non-template
14679 processing here. */
14680 DECL_EXTERNAL (r) = 1;
14681 if (DECL_NAMESPACE_SCOPE_P (t))
14682 DECL_NOT_REALLY_EXTERN (r) = 1;
14683
14684 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14685 SET_DECL_IMPLICIT_INSTANTIATION (r);
14686 /* Remember whether we require constant initialization of
14687 a non-constant template variable. */
14688 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14689 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14690 if (!error_operand_p (r) || (complain & tf_error))
14691 register_specialization (r, gen_tmpl, argvec, false, hash);
14692 }
14693 else
14694 {
14695 if (DECL_LANG_SPECIFIC (r))
14696 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14697 if (!cp_unevaluated_operand)
14698 register_local_specialization (r, t);
14699 }
14700
14701 DECL_CHAIN (r) = NULL_TREE;
14702
14703 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14704 /*flags=*/0,
14705 args, complain, in_decl);
14706
14707 /* Preserve a typedef that names a type. */
14708 if (is_typedef_decl (r) && type != error_mark_node)
14709 {
14710 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14711 set_underlying_type (r);
14712 if (TYPE_DECL_ALIAS_P (r))
14713 /* An alias template specialization can be dependent
14714 even if its underlying type is not. */
14715 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14716 }
14717
14718 layout_decl (r, 0);
14719 }
14720 break;
14721
14722 default:
14723 gcc_unreachable ();
14724 }
14725 #undef RETURN
14726
14727 out:
14728 /* Restore the file and line information. */
14729 input_location = saved_loc;
14730
14731 return r;
14732 }
14733
14734 /* Substitute into the complete parameter type list PARMS. */
14735
14736 tree
14737 tsubst_function_parms (tree parms,
14738 tree args,
14739 tsubst_flags_t complain,
14740 tree in_decl)
14741 {
14742 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14743 }
14744
14745 /* Substitute into the ARG_TYPES of a function type.
14746 If END is a TREE_CHAIN, leave it and any following types
14747 un-substituted. */
14748
14749 static tree
14750 tsubst_arg_types (tree arg_types,
14751 tree args,
14752 tree end,
14753 tsubst_flags_t complain,
14754 tree in_decl)
14755 {
14756 tree remaining_arg_types;
14757 tree type = NULL_TREE;
14758 int i = 1;
14759 tree expanded_args = NULL_TREE;
14760 tree default_arg;
14761
14762 if (!arg_types || arg_types == void_list_node || arg_types == end)
14763 return arg_types;
14764
14765 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14766 args, end, complain, in_decl);
14767 if (remaining_arg_types == error_mark_node)
14768 return error_mark_node;
14769
14770 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14771 {
14772 /* For a pack expansion, perform substitution on the
14773 entire expression. Later on, we'll handle the arguments
14774 one-by-one. */
14775 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14776 args, complain, in_decl);
14777
14778 if (TREE_CODE (expanded_args) == TREE_VEC)
14779 /* So that we'll spin through the parameters, one by one. */
14780 i = TREE_VEC_LENGTH (expanded_args);
14781 else
14782 {
14783 /* We only partially substituted into the parameter
14784 pack. Our type is TYPE_PACK_EXPANSION. */
14785 type = expanded_args;
14786 expanded_args = NULL_TREE;
14787 }
14788 }
14789
14790 while (i > 0) {
14791 --i;
14792
14793 if (expanded_args)
14794 type = TREE_VEC_ELT (expanded_args, i);
14795 else if (!type)
14796 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14797
14798 if (type == error_mark_node)
14799 return error_mark_node;
14800 if (VOID_TYPE_P (type))
14801 {
14802 if (complain & tf_error)
14803 {
14804 error ("invalid parameter type %qT", type);
14805 if (in_decl)
14806 error ("in declaration %q+D", in_decl);
14807 }
14808 return error_mark_node;
14809 }
14810 /* DR 657. */
14811 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14812 return error_mark_node;
14813
14814 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14815 top-level qualifiers as required. */
14816 type = cv_unqualified (type_decays_to (type));
14817
14818 /* We do not substitute into default arguments here. The standard
14819 mandates that they be instantiated only when needed, which is
14820 done in build_over_call. */
14821 default_arg = TREE_PURPOSE (arg_types);
14822
14823 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14824 since the new op() won't have any associated template arguments for us
14825 to refer to later. */
14826 if (lambda_fn_in_template_p (in_decl))
14827 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14828 false/*fn*/, false/*constexpr*/);
14829
14830 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14831 {
14832 /* We've instantiated a template before its default arguments
14833 have been parsed. This can happen for a nested template
14834 class, and is not an error unless we require the default
14835 argument in a call of this function. */
14836 remaining_arg_types =
14837 tree_cons (default_arg, type, remaining_arg_types);
14838 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14839 remaining_arg_types);
14840 }
14841 else
14842 remaining_arg_types =
14843 hash_tree_cons (default_arg, type, remaining_arg_types);
14844 }
14845
14846 return remaining_arg_types;
14847 }
14848
14849 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14850 *not* handle the exception-specification for FNTYPE, because the
14851 initial substitution of explicitly provided template parameters
14852 during argument deduction forbids substitution into the
14853 exception-specification:
14854
14855 [temp.deduct]
14856
14857 All references in the function type of the function template to the
14858 corresponding template parameters are replaced by the specified tem-
14859 plate argument values. If a substitution in a template parameter or
14860 in the function type of the function template results in an invalid
14861 type, type deduction fails. [Note: The equivalent substitution in
14862 exception specifications is done only when the function is instanti-
14863 ated, at which point a program is ill-formed if the substitution
14864 results in an invalid type.] */
14865
14866 static tree
14867 tsubst_function_type (tree t,
14868 tree args,
14869 tsubst_flags_t complain,
14870 tree in_decl)
14871 {
14872 tree return_type;
14873 tree arg_types = NULL_TREE;
14874
14875 /* The TYPE_CONTEXT is not used for function/method types. */
14876 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14877
14878 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14879 failure. */
14880 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14881
14882 if (late_return_type_p)
14883 {
14884 /* Substitute the argument types. */
14885 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14886 complain, in_decl);
14887 if (arg_types == error_mark_node)
14888 return error_mark_node;
14889
14890 tree save_ccp = current_class_ptr;
14891 tree save_ccr = current_class_ref;
14892 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14893 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14894 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14895 if (do_inject)
14896 {
14897 /* DR 1207: 'this' is in scope in the trailing return type. */
14898 inject_this_parameter (this_type, cp_type_quals (this_type));
14899 }
14900
14901 /* Substitute the return type. */
14902 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14903
14904 if (do_inject)
14905 {
14906 current_class_ptr = save_ccp;
14907 current_class_ref = save_ccr;
14908 }
14909 }
14910 else
14911 /* Substitute the return type. */
14912 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14913
14914 if (return_type == error_mark_node)
14915 return error_mark_node;
14916 /* DR 486 clarifies that creation of a function type with an
14917 invalid return type is a deduction failure. */
14918 if (TREE_CODE (return_type) == ARRAY_TYPE
14919 || TREE_CODE (return_type) == FUNCTION_TYPE)
14920 {
14921 if (complain & tf_error)
14922 {
14923 if (TREE_CODE (return_type) == ARRAY_TYPE)
14924 error ("function returning an array");
14925 else
14926 error ("function returning a function");
14927 }
14928 return error_mark_node;
14929 }
14930 /* And DR 657. */
14931 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14932 return error_mark_node;
14933
14934 if (!late_return_type_p)
14935 {
14936 /* Substitute the argument types. */
14937 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14938 complain, in_decl);
14939 if (arg_types == error_mark_node)
14940 return error_mark_node;
14941 }
14942
14943 /* Construct a new type node and return it. */
14944 return rebuild_function_or_method_type (t, return_type, arg_types,
14945 /*raises=*/NULL_TREE, complain);
14946 }
14947
14948 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14949 ARGS into that specification, and return the substituted
14950 specification. If there is no specification, return NULL_TREE. */
14951
14952 static tree
14953 tsubst_exception_specification (tree fntype,
14954 tree args,
14955 tsubst_flags_t complain,
14956 tree in_decl,
14957 bool defer_ok)
14958 {
14959 tree specs;
14960 tree new_specs;
14961
14962 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14963 new_specs = NULL_TREE;
14964 if (specs && TREE_PURPOSE (specs))
14965 {
14966 /* A noexcept-specifier. */
14967 tree expr = TREE_PURPOSE (specs);
14968 if (TREE_CODE (expr) == INTEGER_CST)
14969 new_specs = expr;
14970 else if (defer_ok)
14971 {
14972 /* Defer instantiation of noexcept-specifiers to avoid
14973 excessive instantiations (c++/49107). */
14974 new_specs = make_node (DEFERRED_NOEXCEPT);
14975 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14976 {
14977 /* We already partially instantiated this member template,
14978 so combine the new args with the old. */
14979 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14980 = DEFERRED_NOEXCEPT_PATTERN (expr);
14981 DEFERRED_NOEXCEPT_ARGS (new_specs)
14982 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14983 }
14984 else
14985 {
14986 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14987 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14988 }
14989 }
14990 else
14991 {
14992 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14993 {
14994 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14995 args);
14996 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14997 }
14998 new_specs = tsubst_copy_and_build
14999 (expr, args, complain, in_decl, /*function_p=*/false,
15000 /*integral_constant_expression_p=*/true);
15001 }
15002 new_specs = build_noexcept_spec (new_specs, complain);
15003 }
15004 else if (specs)
15005 {
15006 if (! TREE_VALUE (specs))
15007 new_specs = specs;
15008 else
15009 while (specs)
15010 {
15011 tree spec;
15012 int i, len = 1;
15013 tree expanded_specs = NULL_TREE;
15014
15015 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15016 {
15017 /* Expand the pack expansion type. */
15018 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15019 args, complain,
15020 in_decl);
15021
15022 if (expanded_specs == error_mark_node)
15023 return error_mark_node;
15024 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15025 len = TREE_VEC_LENGTH (expanded_specs);
15026 else
15027 {
15028 /* We're substituting into a member template, so
15029 we got a TYPE_PACK_EXPANSION back. Add that
15030 expansion and move on. */
15031 gcc_assert (TREE_CODE (expanded_specs)
15032 == TYPE_PACK_EXPANSION);
15033 new_specs = add_exception_specifier (new_specs,
15034 expanded_specs,
15035 complain);
15036 specs = TREE_CHAIN (specs);
15037 continue;
15038 }
15039 }
15040
15041 for (i = 0; i < len; ++i)
15042 {
15043 if (expanded_specs)
15044 spec = TREE_VEC_ELT (expanded_specs, i);
15045 else
15046 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15047 if (spec == error_mark_node)
15048 return spec;
15049 new_specs = add_exception_specifier (new_specs, spec,
15050 complain);
15051 }
15052
15053 specs = TREE_CHAIN (specs);
15054 }
15055 }
15056 return new_specs;
15057 }
15058
15059 /* Substitute through a TREE_LIST of types or expressions, handling pack
15060 expansions. */
15061
15062 tree
15063 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15064 {
15065 if (t == void_list_node)
15066 return t;
15067
15068 tree purpose = TREE_PURPOSE (t);
15069 tree purposevec = NULL_TREE;
15070 if (!purpose)
15071 ;
15072 else if (PACK_EXPANSION_P (purpose))
15073 {
15074 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15075 if (TREE_CODE (purpose) == TREE_VEC)
15076 purposevec = purpose;
15077 }
15078 else if (TYPE_P (purpose))
15079 purpose = tsubst (purpose, args, complain, in_decl);
15080 else
15081 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15082 if (purpose == error_mark_node || purposevec == error_mark_node)
15083 return error_mark_node;
15084
15085 tree value = TREE_VALUE (t);
15086 tree valuevec = NULL_TREE;
15087 if (!value)
15088 ;
15089 else if (PACK_EXPANSION_P (value))
15090 {
15091 value = tsubst_pack_expansion (value, args, complain, in_decl);
15092 if (TREE_CODE (value) == TREE_VEC)
15093 valuevec = value;
15094 }
15095 else if (TYPE_P (value))
15096 value = tsubst (value, args, complain, in_decl);
15097 else
15098 value = tsubst_copy_and_build (value, args, complain, in_decl);
15099 if (value == error_mark_node || valuevec == error_mark_node)
15100 return error_mark_node;
15101
15102 tree chain = TREE_CHAIN (t);
15103 if (!chain)
15104 ;
15105 else if (TREE_CODE (chain) == TREE_LIST)
15106 chain = tsubst_tree_list (chain, args, complain, in_decl);
15107 else if (TYPE_P (chain))
15108 chain = tsubst (chain, args, complain, in_decl);
15109 else
15110 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15111 if (chain == error_mark_node)
15112 return error_mark_node;
15113
15114 if (purpose == TREE_PURPOSE (t)
15115 && value == TREE_VALUE (t)
15116 && chain == TREE_CHAIN (t))
15117 return t;
15118
15119 int len;
15120 /* Determine the number of arguments. */
15121 if (purposevec)
15122 {
15123 len = TREE_VEC_LENGTH (purposevec);
15124 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15125 }
15126 else if (valuevec)
15127 len = TREE_VEC_LENGTH (valuevec);
15128 else
15129 len = 1;
15130
15131 for (int i = len; i-- > 0; )
15132 {
15133 if (purposevec)
15134 purpose = TREE_VEC_ELT (purposevec, i);
15135 if (valuevec)
15136 value = TREE_VEC_ELT (valuevec, i);
15137
15138 if (value && TYPE_P (value))
15139 chain = hash_tree_cons (purpose, value, chain);
15140 else
15141 chain = tree_cons (purpose, value, chain);
15142 }
15143
15144 return chain;
15145 }
15146
15147 /* Take the tree structure T and replace template parameters used
15148 therein with the argument vector ARGS. IN_DECL is an associated
15149 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15150 Issue error and warning messages under control of COMPLAIN. Note
15151 that we must be relatively non-tolerant of extensions here, in
15152 order to preserve conformance; if we allow substitutions that
15153 should not be allowed, we may allow argument deductions that should
15154 not succeed, and therefore report ambiguous overload situations
15155 where there are none. In theory, we could allow the substitution,
15156 but indicate that it should have failed, and allow our caller to
15157 make sure that the right thing happens, but we don't try to do this
15158 yet.
15159
15160 This function is used for dealing with types, decls and the like;
15161 for expressions, use tsubst_expr or tsubst_copy. */
15162
15163 tree
15164 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15165 {
15166 enum tree_code code;
15167 tree type, r = NULL_TREE;
15168
15169 if (t == NULL_TREE || t == error_mark_node
15170 || t == integer_type_node
15171 || t == void_type_node
15172 || t == char_type_node
15173 || t == unknown_type_node
15174 || TREE_CODE (t) == NAMESPACE_DECL
15175 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15176 return t;
15177
15178 if (DECL_P (t))
15179 return tsubst_decl (t, args, complain);
15180
15181 if (args == NULL_TREE)
15182 return t;
15183
15184 code = TREE_CODE (t);
15185
15186 if (code == IDENTIFIER_NODE)
15187 type = IDENTIFIER_TYPE_VALUE (t);
15188 else
15189 type = TREE_TYPE (t);
15190
15191 gcc_assert (type != unknown_type_node);
15192
15193 /* Reuse typedefs. We need to do this to handle dependent attributes,
15194 such as attribute aligned. */
15195 if (TYPE_P (t)
15196 && typedef_variant_p (t))
15197 {
15198 tree decl = TYPE_NAME (t);
15199
15200 if (alias_template_specialization_p (t, nt_opaque))
15201 {
15202 /* DECL represents an alias template and we want to
15203 instantiate it. */
15204 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15205 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15206 r = instantiate_alias_template (tmpl, gen_args, complain);
15207 }
15208 else if (DECL_CLASS_SCOPE_P (decl)
15209 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15210 && uses_template_parms (DECL_CONTEXT (decl)))
15211 {
15212 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15213 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15214 r = retrieve_specialization (tmpl, gen_args, 0);
15215 }
15216 else if (DECL_FUNCTION_SCOPE_P (decl)
15217 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15218 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15219 r = retrieve_local_specialization (decl);
15220 else
15221 /* The typedef is from a non-template context. */
15222 return t;
15223
15224 if (r)
15225 {
15226 r = TREE_TYPE (r);
15227 r = cp_build_qualified_type_real
15228 (r, cp_type_quals (t) | cp_type_quals (r),
15229 complain | tf_ignore_bad_quals);
15230 return r;
15231 }
15232 else
15233 {
15234 /* We don't have an instantiation yet, so drop the typedef. */
15235 int quals = cp_type_quals (t);
15236 t = DECL_ORIGINAL_TYPE (decl);
15237 t = cp_build_qualified_type_real (t, quals,
15238 complain | tf_ignore_bad_quals);
15239 }
15240 }
15241
15242 bool fndecl_type = (complain & tf_fndecl_type);
15243 complain &= ~tf_fndecl_type;
15244
15245 if (type
15246 && code != TYPENAME_TYPE
15247 && code != TEMPLATE_TYPE_PARM
15248 && code != TEMPLATE_PARM_INDEX
15249 && code != IDENTIFIER_NODE
15250 && code != FUNCTION_TYPE
15251 && code != METHOD_TYPE)
15252 type = tsubst (type, args, complain, in_decl);
15253 if (type == error_mark_node)
15254 return error_mark_node;
15255
15256 switch (code)
15257 {
15258 case RECORD_TYPE:
15259 case UNION_TYPE:
15260 case ENUMERAL_TYPE:
15261 return tsubst_aggr_type (t, args, complain, in_decl,
15262 /*entering_scope=*/0);
15263
15264 case ERROR_MARK:
15265 case IDENTIFIER_NODE:
15266 case VOID_TYPE:
15267 case REAL_TYPE:
15268 case COMPLEX_TYPE:
15269 case VECTOR_TYPE:
15270 case BOOLEAN_TYPE:
15271 case NULLPTR_TYPE:
15272 case LANG_TYPE:
15273 return t;
15274
15275 case INTEGER_TYPE:
15276 if (t == integer_type_node)
15277 return t;
15278
15279 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15280 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15281 return t;
15282
15283 {
15284 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15285
15286 max = tsubst_expr (omax, args, complain, in_decl,
15287 /*integral_constant_expression_p=*/false);
15288
15289 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15290 needed. */
15291 if (TREE_CODE (max) == NOP_EXPR
15292 && TREE_SIDE_EFFECTS (omax)
15293 && !TREE_TYPE (max))
15294 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15295
15296 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15297 with TREE_SIDE_EFFECTS that indicates this is not an integral
15298 constant expression. */
15299 if (processing_template_decl
15300 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15301 {
15302 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15303 TREE_SIDE_EFFECTS (max) = 1;
15304 }
15305
15306 return compute_array_index_type (NULL_TREE, max, complain);
15307 }
15308
15309 case TEMPLATE_TYPE_PARM:
15310 case TEMPLATE_TEMPLATE_PARM:
15311 case BOUND_TEMPLATE_TEMPLATE_PARM:
15312 case TEMPLATE_PARM_INDEX:
15313 {
15314 int idx;
15315 int level;
15316 int levels;
15317 tree arg = NULL_TREE;
15318
15319 r = NULL_TREE;
15320
15321 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15322 template_parm_level_and_index (t, &level, &idx);
15323
15324 levels = TMPL_ARGS_DEPTH (args);
15325 if (level <= levels
15326 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15327 {
15328 arg = TMPL_ARG (args, level, idx);
15329
15330 /* See through ARGUMENT_PACK_SELECT arguments. */
15331 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15332 arg = argument_pack_select_arg (arg);
15333 }
15334
15335 if (arg == error_mark_node)
15336 return error_mark_node;
15337 else if (arg != NULL_TREE)
15338 {
15339 if (ARGUMENT_PACK_P (arg))
15340 /* If ARG is an argument pack, we don't actually want to
15341 perform a substitution here, because substitutions
15342 for argument packs are only done
15343 element-by-element. We can get to this point when
15344 substituting the type of a non-type template
15345 parameter pack, when that type actually contains
15346 template parameter packs from an outer template, e.g.,
15347
15348 template<typename... Types> struct A {
15349 template<Types... Values> struct B { };
15350 }; */
15351 return t;
15352
15353 if (code == TEMPLATE_TYPE_PARM)
15354 {
15355 int quals;
15356
15357 /* When building concept checks for the purpose of
15358 deducing placeholders, we can end up with wildcards
15359 where types are expected. Adjust this to the deduced
15360 value. */
15361 if (TREE_CODE (arg) == WILDCARD_DECL)
15362 arg = TREE_TYPE (TREE_TYPE (arg));
15363
15364 gcc_assert (TYPE_P (arg));
15365
15366 quals = cp_type_quals (arg) | cp_type_quals (t);
15367
15368 return cp_build_qualified_type_real
15369 (arg, quals, complain | tf_ignore_bad_quals);
15370 }
15371 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15372 {
15373 /* We are processing a type constructed from a
15374 template template parameter. */
15375 tree argvec = tsubst (TYPE_TI_ARGS (t),
15376 args, complain, in_decl);
15377 if (argvec == error_mark_node)
15378 return error_mark_node;
15379
15380 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15381 || TREE_CODE (arg) == TEMPLATE_DECL
15382 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15383
15384 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15385 /* Consider this code:
15386
15387 template <template <class> class Template>
15388 struct Internal {
15389 template <class Arg> using Bind = Template<Arg>;
15390 };
15391
15392 template <template <class> class Template, class Arg>
15393 using Instantiate = Template<Arg>; //#0
15394
15395 template <template <class> class Template,
15396 class Argument>
15397 using Bind =
15398 Instantiate<Internal<Template>::template Bind,
15399 Argument>; //#1
15400
15401 When #1 is parsed, the
15402 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15403 parameter `Template' in #0 matches the
15404 UNBOUND_CLASS_TEMPLATE representing the argument
15405 `Internal<Template>::template Bind'; We then want
15406 to assemble the type `Bind<Argument>' that can't
15407 be fully created right now, because
15408 `Internal<Template>' not being complete, the Bind
15409 template cannot be looked up in that context. So
15410 we need to "store" `Bind<Argument>' for later
15411 when the context of Bind becomes complete. Let's
15412 store that in a TYPENAME_TYPE. */
15413 return make_typename_type (TYPE_CONTEXT (arg),
15414 build_nt (TEMPLATE_ID_EXPR,
15415 TYPE_IDENTIFIER (arg),
15416 argvec),
15417 typename_type,
15418 complain);
15419
15420 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15421 are resolving nested-types in the signature of a
15422 member function templates. Otherwise ARG is a
15423 TEMPLATE_DECL and is the real template to be
15424 instantiated. */
15425 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15426 arg = TYPE_NAME (arg);
15427
15428 r = lookup_template_class (arg,
15429 argvec, in_decl,
15430 DECL_CONTEXT (arg),
15431 /*entering_scope=*/0,
15432 complain);
15433 return cp_build_qualified_type_real
15434 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15435 }
15436 else if (code == TEMPLATE_TEMPLATE_PARM)
15437 return arg;
15438 else
15439 /* TEMPLATE_PARM_INDEX. */
15440 return convert_from_reference (unshare_expr (arg));
15441 }
15442
15443 if (level == 1)
15444 /* This can happen during the attempted tsubst'ing in
15445 unify. This means that we don't yet have any information
15446 about the template parameter in question. */
15447 return t;
15448
15449 /* Early in template argument deduction substitution, we don't
15450 want to reduce the level of 'auto', or it will be confused
15451 with a normal template parm in subsequent deduction.
15452 Similarly, don't reduce the level of template parameters to
15453 avoid mismatches when deducing their types. */
15454 if (complain & tf_partial)
15455 return t;
15456
15457 /* If we get here, we must have been looking at a parm for a
15458 more deeply nested template. Make a new version of this
15459 template parameter, but with a lower level. */
15460 switch (code)
15461 {
15462 case TEMPLATE_TYPE_PARM:
15463 case TEMPLATE_TEMPLATE_PARM:
15464 case BOUND_TEMPLATE_TEMPLATE_PARM:
15465 if (cp_type_quals (t))
15466 {
15467 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15468 r = cp_build_qualified_type_real
15469 (r, cp_type_quals (t),
15470 complain | (code == TEMPLATE_TYPE_PARM
15471 ? tf_ignore_bad_quals : 0));
15472 }
15473 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15474 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15475 && (r = (TEMPLATE_PARM_DESCENDANTS
15476 (TEMPLATE_TYPE_PARM_INDEX (t))))
15477 && (r = TREE_TYPE (r))
15478 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15479 /* Break infinite recursion when substituting the constraints
15480 of a constrained placeholder. */;
15481 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15482 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15483 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15484 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15485 r = TEMPLATE_PARM_DESCENDANTS (arg))
15486 && (TEMPLATE_PARM_LEVEL (r)
15487 == TEMPLATE_PARM_LEVEL (arg) - levels))
15488 /* Cache the simple case of lowering a type parameter. */
15489 r = TREE_TYPE (r);
15490 else
15491 {
15492 r = copy_type (t);
15493 TEMPLATE_TYPE_PARM_INDEX (r)
15494 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15495 r, levels, args, complain);
15496 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15497 TYPE_MAIN_VARIANT (r) = r;
15498 TYPE_POINTER_TO (r) = NULL_TREE;
15499 TYPE_REFERENCE_TO (r) = NULL_TREE;
15500
15501 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15502 {
15503 /* Propagate constraints on placeholders since they are
15504 only instantiated during satisfaction. */
15505 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15506 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15507 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15508 {
15509 pl = tsubst_copy (pl, args, complain, in_decl);
15510 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15511 }
15512 }
15513
15514 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15515 /* We have reduced the level of the template
15516 template parameter, but not the levels of its
15517 template parameters, so canonical_type_parameter
15518 will not be able to find the canonical template
15519 template parameter for this level. Thus, we
15520 require structural equality checking to compare
15521 TEMPLATE_TEMPLATE_PARMs. */
15522 SET_TYPE_STRUCTURAL_EQUALITY (r);
15523 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15524 SET_TYPE_STRUCTURAL_EQUALITY (r);
15525 else
15526 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15527
15528 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15529 {
15530 tree tinfo = TYPE_TEMPLATE_INFO (t);
15531 /* We might need to substitute into the types of non-type
15532 template parameters. */
15533 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15534 complain, in_decl);
15535 if (tmpl == error_mark_node)
15536 return error_mark_node;
15537 tree argvec = tsubst (TI_ARGS (tinfo), args,
15538 complain, in_decl);
15539 if (argvec == error_mark_node)
15540 return error_mark_node;
15541
15542 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15543 = build_template_info (tmpl, argvec);
15544 }
15545 }
15546 break;
15547
15548 case TEMPLATE_PARM_INDEX:
15549 /* OK, now substitute the type of the non-type parameter. We
15550 couldn't do it earlier because it might be an auto parameter,
15551 and we wouldn't need to if we had an argument. */
15552 type = tsubst (type, args, complain, in_decl);
15553 if (type == error_mark_node)
15554 return error_mark_node;
15555 r = reduce_template_parm_level (t, type, levels, args, complain);
15556 break;
15557
15558 default:
15559 gcc_unreachable ();
15560 }
15561
15562 return r;
15563 }
15564
15565 case TREE_LIST:
15566 return tsubst_tree_list (t, args, complain, in_decl);
15567
15568 case TREE_BINFO:
15569 /* We should never be tsubsting a binfo. */
15570 gcc_unreachable ();
15571
15572 case TREE_VEC:
15573 /* A vector of template arguments. */
15574 gcc_assert (!type);
15575 return tsubst_template_args (t, args, complain, in_decl);
15576
15577 case POINTER_TYPE:
15578 case REFERENCE_TYPE:
15579 {
15580 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15581 return t;
15582
15583 /* [temp.deduct]
15584
15585 Type deduction may fail for any of the following
15586 reasons:
15587
15588 -- Attempting to create a pointer to reference type.
15589 -- Attempting to create a reference to a reference type or
15590 a reference to void.
15591
15592 Core issue 106 says that creating a reference to a reference
15593 during instantiation is no longer a cause for failure. We
15594 only enforce this check in strict C++98 mode. */
15595 if ((TYPE_REF_P (type)
15596 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15597 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15598 {
15599 static location_t last_loc;
15600
15601 /* We keep track of the last time we issued this error
15602 message to avoid spewing a ton of messages during a
15603 single bad template instantiation. */
15604 if (complain & tf_error
15605 && last_loc != input_location)
15606 {
15607 if (VOID_TYPE_P (type))
15608 error ("forming reference to void");
15609 else if (code == POINTER_TYPE)
15610 error ("forming pointer to reference type %qT", type);
15611 else
15612 error ("forming reference to reference type %qT", type);
15613 last_loc = input_location;
15614 }
15615
15616 return error_mark_node;
15617 }
15618 else if (TREE_CODE (type) == FUNCTION_TYPE
15619 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15620 || type_memfn_rqual (type) != REF_QUAL_NONE))
15621 {
15622 if (complain & tf_error)
15623 {
15624 if (code == POINTER_TYPE)
15625 error ("forming pointer to qualified function type %qT",
15626 type);
15627 else
15628 error ("forming reference to qualified function type %qT",
15629 type);
15630 }
15631 return error_mark_node;
15632 }
15633 else if (code == POINTER_TYPE)
15634 {
15635 r = build_pointer_type (type);
15636 if (TREE_CODE (type) == METHOD_TYPE)
15637 r = build_ptrmemfunc_type (r);
15638 }
15639 else if (TYPE_REF_P (type))
15640 /* In C++0x, during template argument substitution, when there is an
15641 attempt to create a reference to a reference type, reference
15642 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15643
15644 "If a template-argument for a template-parameter T names a type
15645 that is a reference to a type A, an attempt to create the type
15646 'lvalue reference to cv T' creates the type 'lvalue reference to
15647 A,' while an attempt to create the type type rvalue reference to
15648 cv T' creates the type T"
15649 */
15650 r = cp_build_reference_type
15651 (TREE_TYPE (type),
15652 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15653 else
15654 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15655 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15656
15657 if (r != error_mark_node)
15658 /* Will this ever be needed for TYPE_..._TO values? */
15659 layout_type (r);
15660
15661 return r;
15662 }
15663 case OFFSET_TYPE:
15664 {
15665 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15666 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15667 {
15668 /* [temp.deduct]
15669
15670 Type deduction may fail for any of the following
15671 reasons:
15672
15673 -- Attempting to create "pointer to member of T" when T
15674 is not a class type. */
15675 if (complain & tf_error)
15676 error ("creating pointer to member of non-class type %qT", r);
15677 return error_mark_node;
15678 }
15679 if (TYPE_REF_P (type))
15680 {
15681 if (complain & tf_error)
15682 error ("creating pointer to member reference type %qT", type);
15683 return error_mark_node;
15684 }
15685 if (VOID_TYPE_P (type))
15686 {
15687 if (complain & tf_error)
15688 error ("creating pointer to member of type void");
15689 return error_mark_node;
15690 }
15691 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15692 if (TREE_CODE (type) == FUNCTION_TYPE)
15693 {
15694 /* The type of the implicit object parameter gets its
15695 cv-qualifiers from the FUNCTION_TYPE. */
15696 tree memptr;
15697 tree method_type
15698 = build_memfn_type (type, r, type_memfn_quals (type),
15699 type_memfn_rqual (type));
15700 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15701 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15702 complain);
15703 }
15704 else
15705 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15706 cp_type_quals (t),
15707 complain);
15708 }
15709 case FUNCTION_TYPE:
15710 case METHOD_TYPE:
15711 {
15712 tree fntype;
15713 tree specs;
15714 fntype = tsubst_function_type (t, args, complain, in_decl);
15715 if (fntype == error_mark_node)
15716 return error_mark_node;
15717
15718 /* Substitute the exception specification. */
15719 specs = tsubst_exception_specification (t, args, complain, in_decl,
15720 /*defer_ok*/fndecl_type);
15721 if (specs == error_mark_node)
15722 return error_mark_node;
15723 if (specs)
15724 fntype = build_exception_variant (fntype, specs);
15725 return fntype;
15726 }
15727 case ARRAY_TYPE:
15728 {
15729 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15730 if (domain == error_mark_node)
15731 return error_mark_node;
15732
15733 /* As an optimization, we avoid regenerating the array type if
15734 it will obviously be the same as T. */
15735 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15736 return t;
15737
15738 /* These checks should match the ones in create_array_type_for_decl.
15739
15740 [temp.deduct]
15741
15742 The deduction may fail for any of the following reasons:
15743
15744 -- Attempting to create an array with an element type that
15745 is void, a function type, or a reference type, or [DR337]
15746 an abstract class type. */
15747 if (VOID_TYPE_P (type)
15748 || TREE_CODE (type) == FUNCTION_TYPE
15749 || (TREE_CODE (type) == ARRAY_TYPE
15750 && TYPE_DOMAIN (type) == NULL_TREE)
15751 || TYPE_REF_P (type))
15752 {
15753 if (complain & tf_error)
15754 error ("creating array of %qT", type);
15755 return error_mark_node;
15756 }
15757
15758 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15759 return error_mark_node;
15760
15761 r = build_cplus_array_type (type, domain);
15762
15763 if (!valid_array_size_p (input_location, r, in_decl,
15764 (complain & tf_error)))
15765 return error_mark_node;
15766
15767 if (TYPE_USER_ALIGN (t))
15768 {
15769 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15770 TYPE_USER_ALIGN (r) = 1;
15771 }
15772
15773 return r;
15774 }
15775
15776 case TYPENAME_TYPE:
15777 {
15778 tree ctx = TYPE_CONTEXT (t);
15779 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15780 {
15781 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15782 if (ctx == error_mark_node
15783 || TREE_VEC_LENGTH (ctx) > 1)
15784 return error_mark_node;
15785 if (TREE_VEC_LENGTH (ctx) == 0)
15786 {
15787 if (complain & tf_error)
15788 error ("%qD is instantiated for an empty pack",
15789 TYPENAME_TYPE_FULLNAME (t));
15790 return error_mark_node;
15791 }
15792 ctx = TREE_VEC_ELT (ctx, 0);
15793 }
15794 else
15795 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15796 /*entering_scope=*/1);
15797 if (ctx == error_mark_node)
15798 return error_mark_node;
15799
15800 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15801 complain, in_decl);
15802 if (f == error_mark_node)
15803 return error_mark_node;
15804
15805 if (!MAYBE_CLASS_TYPE_P (ctx))
15806 {
15807 if (complain & tf_error)
15808 error ("%qT is not a class, struct, or union type", ctx);
15809 return error_mark_node;
15810 }
15811 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15812 {
15813 /* Normally, make_typename_type does not require that the CTX
15814 have complete type in order to allow things like:
15815
15816 template <class T> struct S { typename S<T>::X Y; };
15817
15818 But, such constructs have already been resolved by this
15819 point, so here CTX really should have complete type, unless
15820 it's a partial instantiation. */
15821 ctx = complete_type (ctx);
15822 if (!COMPLETE_TYPE_P (ctx))
15823 {
15824 if (complain & tf_error)
15825 cxx_incomplete_type_error (NULL_TREE, ctx);
15826 return error_mark_node;
15827 }
15828 }
15829
15830 f = make_typename_type (ctx, f, typename_type,
15831 complain | tf_keep_type_decl);
15832 if (f == error_mark_node)
15833 return f;
15834 if (TREE_CODE (f) == TYPE_DECL)
15835 {
15836 complain |= tf_ignore_bad_quals;
15837 f = TREE_TYPE (f);
15838 }
15839
15840 if (TREE_CODE (f) != TYPENAME_TYPE)
15841 {
15842 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15843 {
15844 if (complain & tf_error)
15845 error ("%qT resolves to %qT, which is not an enumeration type",
15846 t, f);
15847 else
15848 return error_mark_node;
15849 }
15850 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15851 {
15852 if (complain & tf_error)
15853 error ("%qT resolves to %qT, which is not a class type",
15854 t, f);
15855 else
15856 return error_mark_node;
15857 }
15858 }
15859
15860 return cp_build_qualified_type_real
15861 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15862 }
15863
15864 case UNBOUND_CLASS_TEMPLATE:
15865 {
15866 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15867 in_decl, /*entering_scope=*/1);
15868 tree name = TYPE_IDENTIFIER (t);
15869 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15870
15871 if (ctx == error_mark_node || name == error_mark_node)
15872 return error_mark_node;
15873
15874 if (parm_list)
15875 parm_list = tsubst_template_parms (parm_list, args, complain);
15876 return make_unbound_class_template (ctx, name, parm_list, complain);
15877 }
15878
15879 case TYPEOF_TYPE:
15880 {
15881 tree type;
15882
15883 ++cp_unevaluated_operand;
15884 ++c_inhibit_evaluation_warnings;
15885
15886 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15887 complain, in_decl,
15888 /*integral_constant_expression_p=*/false);
15889
15890 --cp_unevaluated_operand;
15891 --c_inhibit_evaluation_warnings;
15892
15893 type = finish_typeof (type);
15894 return cp_build_qualified_type_real (type,
15895 cp_type_quals (t)
15896 | cp_type_quals (type),
15897 complain);
15898 }
15899
15900 case DECLTYPE_TYPE:
15901 {
15902 tree type;
15903
15904 ++cp_unevaluated_operand;
15905 ++c_inhibit_evaluation_warnings;
15906
15907 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15908 complain|tf_decltype, in_decl,
15909 /*function_p*/false,
15910 /*integral_constant_expression*/false);
15911
15912 --cp_unevaluated_operand;
15913 --c_inhibit_evaluation_warnings;
15914
15915 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15916 type = lambda_capture_field_type (type,
15917 false /*explicit_init*/,
15918 DECLTYPE_FOR_REF_CAPTURE (t));
15919 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15920 type = lambda_proxy_type (type);
15921 else
15922 {
15923 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15924 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15925 && EXPR_P (type))
15926 /* In a template ~id could be either a complement expression
15927 or an unqualified-id naming a destructor; if instantiating
15928 it produces an expression, it's not an id-expression or
15929 member access. */
15930 id = false;
15931 type = finish_decltype_type (type, id, complain);
15932 }
15933 return cp_build_qualified_type_real (type,
15934 cp_type_quals (t)
15935 | cp_type_quals (type),
15936 complain | tf_ignore_bad_quals);
15937 }
15938
15939 case UNDERLYING_TYPE:
15940 {
15941 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15942 complain, in_decl);
15943 return finish_underlying_type (type);
15944 }
15945
15946 case TYPE_ARGUMENT_PACK:
15947 case NONTYPE_ARGUMENT_PACK:
15948 {
15949 tree r;
15950
15951 if (code == NONTYPE_ARGUMENT_PACK)
15952 r = make_node (code);
15953 else
15954 r = cxx_make_type (code);
15955
15956 tree pack_args = ARGUMENT_PACK_ARGS (t);
15957 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15958 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15959
15960 return r;
15961 }
15962
15963 case VOID_CST:
15964 case INTEGER_CST:
15965 case REAL_CST:
15966 case STRING_CST:
15967 case PLUS_EXPR:
15968 case MINUS_EXPR:
15969 case NEGATE_EXPR:
15970 case NOP_EXPR:
15971 case INDIRECT_REF:
15972 case ADDR_EXPR:
15973 case CALL_EXPR:
15974 case ARRAY_REF:
15975 case SCOPE_REF:
15976 /* We should use one of the expression tsubsts for these codes. */
15977 gcc_unreachable ();
15978
15979 default:
15980 sorry ("use of %qs in template", get_tree_code_name (code));
15981 return error_mark_node;
15982 }
15983 }
15984
15985 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15986 expression on the left-hand side of the "." or "->" operator. We
15987 only do the lookup if we had a dependent BASELINK. Otherwise we
15988 adjust it onto the instantiated heirarchy. */
15989
15990 static tree
15991 tsubst_baselink (tree baselink, tree object_type,
15992 tree args, tsubst_flags_t complain, tree in_decl)
15993 {
15994 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15995 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15996 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15997
15998 tree optype = BASELINK_OPTYPE (baselink);
15999 optype = tsubst (optype, args, complain, in_decl);
16000
16001 tree template_args = NULL_TREE;
16002 bool template_id_p = false;
16003 tree fns = BASELINK_FUNCTIONS (baselink);
16004 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16005 {
16006 template_id_p = true;
16007 template_args = TREE_OPERAND (fns, 1);
16008 fns = TREE_OPERAND (fns, 0);
16009 if (template_args)
16010 template_args = tsubst_template_args (template_args, args,
16011 complain, in_decl);
16012 }
16013
16014 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16015 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16016 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16017
16018 if (dependent_p)
16019 {
16020 tree name = OVL_NAME (fns);
16021 if (IDENTIFIER_CONV_OP_P (name))
16022 name = make_conv_op_name (optype);
16023
16024 if (name == complete_dtor_identifier)
16025 /* Treat as-if non-dependent below. */
16026 dependent_p = false;
16027
16028 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16029 complain);
16030 if (!baselink)
16031 {
16032 if ((complain & tf_error)
16033 && constructor_name_p (name, qualifying_scope))
16034 error ("cannot call constructor %<%T::%D%> directly",
16035 qualifying_scope, name);
16036 return error_mark_node;
16037 }
16038
16039 if (BASELINK_P (baselink))
16040 fns = BASELINK_FUNCTIONS (baselink);
16041 }
16042 else
16043 /* We're going to overwrite pieces below, make a duplicate. */
16044 baselink = copy_node (baselink);
16045
16046 /* If lookup found a single function, mark it as used at this point.
16047 (If lookup found multiple functions the one selected later by
16048 overload resolution will be marked as used at that point.) */
16049 if (!template_id_p && !really_overloaded_fn (fns))
16050 {
16051 tree fn = OVL_FIRST (fns);
16052 bool ok = mark_used (fn, complain);
16053 if (!ok && !(complain & tf_error))
16054 return error_mark_node;
16055 if (ok && BASELINK_P (baselink))
16056 /* We might have instantiated an auto function. */
16057 TREE_TYPE (baselink) = TREE_TYPE (fn);
16058 }
16059
16060 if (BASELINK_P (baselink))
16061 {
16062 /* Add back the template arguments, if present. */
16063 if (template_id_p)
16064 BASELINK_FUNCTIONS (baselink)
16065 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16066
16067 /* Update the conversion operator type. */
16068 BASELINK_OPTYPE (baselink) = optype;
16069 }
16070
16071 if (!object_type)
16072 object_type = current_class_type;
16073
16074 if (qualified_p || !dependent_p)
16075 {
16076 baselink = adjust_result_of_qualified_name_lookup (baselink,
16077 qualifying_scope,
16078 object_type);
16079 if (!qualified_p)
16080 /* We need to call adjust_result_of_qualified_name_lookup in case the
16081 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16082 so that we still get virtual function binding. */
16083 BASELINK_QUALIFIED_P (baselink) = false;
16084 }
16085
16086 return baselink;
16087 }
16088
16089 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16090 true if the qualified-id will be a postfix-expression in-and-of
16091 itself; false if more of the postfix-expression follows the
16092 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16093 of "&". */
16094
16095 static tree
16096 tsubst_qualified_id (tree qualified_id, tree args,
16097 tsubst_flags_t complain, tree in_decl,
16098 bool done, bool address_p)
16099 {
16100 tree expr;
16101 tree scope;
16102 tree name;
16103 bool is_template;
16104 tree template_args;
16105 location_t loc = UNKNOWN_LOCATION;
16106
16107 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16108
16109 /* Figure out what name to look up. */
16110 name = TREE_OPERAND (qualified_id, 1);
16111 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16112 {
16113 is_template = true;
16114 loc = EXPR_LOCATION (name);
16115 template_args = TREE_OPERAND (name, 1);
16116 if (template_args)
16117 template_args = tsubst_template_args (template_args, args,
16118 complain, in_decl);
16119 if (template_args == error_mark_node)
16120 return error_mark_node;
16121 name = TREE_OPERAND (name, 0);
16122 }
16123 else
16124 {
16125 is_template = false;
16126 template_args = NULL_TREE;
16127 }
16128
16129 /* Substitute into the qualifying scope. When there are no ARGS, we
16130 are just trying to simplify a non-dependent expression. In that
16131 case the qualifying scope may be dependent, and, in any case,
16132 substituting will not help. */
16133 scope = TREE_OPERAND (qualified_id, 0);
16134 if (args)
16135 {
16136 scope = tsubst (scope, args, complain, in_decl);
16137 expr = tsubst_copy (name, args, complain, in_decl);
16138 }
16139 else
16140 expr = name;
16141
16142 if (dependent_scope_p (scope))
16143 {
16144 if (is_template)
16145 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16146 tree r = build_qualified_name (NULL_TREE, scope, expr,
16147 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16148 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16149 return r;
16150 }
16151
16152 if (!BASELINK_P (name) && !DECL_P (expr))
16153 {
16154 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16155 {
16156 /* A BIT_NOT_EXPR is used to represent a destructor. */
16157 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16158 {
16159 error ("qualifying type %qT does not match destructor name ~%qT",
16160 scope, TREE_OPERAND (expr, 0));
16161 expr = error_mark_node;
16162 }
16163 else
16164 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16165 /*is_type_p=*/0, false);
16166 }
16167 else
16168 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
16169 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16170 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16171 {
16172 if (complain & tf_error)
16173 {
16174 error ("dependent-name %qE is parsed as a non-type, but "
16175 "instantiation yields a type", qualified_id);
16176 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16177 }
16178 return error_mark_node;
16179 }
16180 }
16181
16182 if (DECL_P (expr))
16183 {
16184 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16185 scope, complain))
16186 return error_mark_node;
16187 /* Remember that there was a reference to this entity. */
16188 if (!mark_used (expr, complain) && !(complain & tf_error))
16189 return error_mark_node;
16190 }
16191
16192 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16193 {
16194 if (complain & tf_error)
16195 qualified_name_lookup_error (scope,
16196 TREE_OPERAND (qualified_id, 1),
16197 expr, input_location);
16198 return error_mark_node;
16199 }
16200
16201 if (is_template)
16202 {
16203 /* We may be repeating a check already done during parsing, but
16204 if it was well-formed and passed then, it will pass again
16205 now, and if it didn't, we wouldn't have got here. The case
16206 we want to catch is when we couldn't tell then, and can now,
16207 namely when templ prior to substitution was an
16208 identifier. */
16209 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16210 return error_mark_node;
16211
16212 if (variable_template_p (expr))
16213 expr = lookup_and_finish_template_variable (expr, template_args,
16214 complain);
16215 else
16216 expr = lookup_template_function (expr, template_args);
16217 }
16218
16219 if (expr == error_mark_node && complain & tf_error)
16220 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16221 expr, input_location);
16222 else if (TYPE_P (scope))
16223 {
16224 expr = (adjust_result_of_qualified_name_lookup
16225 (expr, scope, current_nonlambda_class_type ()));
16226 expr = (finish_qualified_id_expr
16227 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16228 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16229 /*template_arg_p=*/false, complain));
16230 }
16231
16232 /* Expressions do not generally have reference type. */
16233 if (TREE_CODE (expr) != SCOPE_REF
16234 /* However, if we're about to form a pointer-to-member, we just
16235 want the referenced member referenced. */
16236 && TREE_CODE (expr) != OFFSET_REF)
16237 expr = convert_from_reference (expr);
16238
16239 if (REF_PARENTHESIZED_P (qualified_id))
16240 expr = force_paren_expr (expr);
16241
16242 return expr;
16243 }
16244
16245 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16246 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16247 for tsubst. */
16248
16249 static tree
16250 tsubst_init (tree init, tree decl, tree args,
16251 tsubst_flags_t complain, tree in_decl)
16252 {
16253 if (!init)
16254 return NULL_TREE;
16255
16256 init = tsubst_expr (init, args, complain, in_decl, false);
16257
16258 tree type = TREE_TYPE (decl);
16259
16260 if (!init && type != error_mark_node)
16261 {
16262 if (tree auto_node = type_uses_auto (type))
16263 {
16264 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16265 {
16266 if (complain & tf_error)
16267 error ("initializer for %q#D expands to an empty list "
16268 "of expressions", decl);
16269 return error_mark_node;
16270 }
16271 }
16272 else if (!dependent_type_p (type))
16273 {
16274 /* If we had an initializer but it
16275 instantiated to nothing,
16276 value-initialize the object. This will
16277 only occur when the initializer was a
16278 pack expansion where the parameter packs
16279 used in that expansion were of length
16280 zero. */
16281 init = build_value_init (type, complain);
16282 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16283 init = get_target_expr_sfinae (init, complain);
16284 if (TREE_CODE (init) == TARGET_EXPR)
16285 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16286 }
16287 }
16288
16289 return init;
16290 }
16291
16292 /* Like tsubst, but deals with expressions. This function just replaces
16293 template parms; to finish processing the resultant expression, use
16294 tsubst_copy_and_build or tsubst_expr. */
16295
16296 static tree
16297 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16298 {
16299 enum tree_code code;
16300 tree r;
16301
16302 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16303 return t;
16304
16305 code = TREE_CODE (t);
16306
16307 switch (code)
16308 {
16309 case PARM_DECL:
16310 r = retrieve_local_specialization (t);
16311
16312 if (r == NULL_TREE)
16313 {
16314 /* We get here for a use of 'this' in an NSDMI. */
16315 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16316 return current_class_ptr;
16317
16318 /* This can happen for a parameter name used later in a function
16319 declaration (such as in a late-specified return type). Just
16320 make a dummy decl, since it's only used for its type. */
16321 gcc_assert (cp_unevaluated_operand != 0);
16322 r = tsubst_decl (t, args, complain);
16323 /* Give it the template pattern as its context; its true context
16324 hasn't been instantiated yet and this is good enough for
16325 mangling. */
16326 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16327 }
16328
16329 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16330 r = argument_pack_select_arg (r);
16331 if (!mark_used (r, complain) && !(complain & tf_error))
16332 return error_mark_node;
16333 return r;
16334
16335 case CONST_DECL:
16336 {
16337 tree enum_type;
16338 tree v;
16339
16340 if (DECL_TEMPLATE_PARM_P (t))
16341 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16342 /* There is no need to substitute into namespace-scope
16343 enumerators. */
16344 if (DECL_NAMESPACE_SCOPE_P (t))
16345 return t;
16346 /* If ARGS is NULL, then T is known to be non-dependent. */
16347 if (args == NULL_TREE)
16348 return scalar_constant_value (t);
16349
16350 /* Unfortunately, we cannot just call lookup_name here.
16351 Consider:
16352
16353 template <int I> int f() {
16354 enum E { a = I };
16355 struct S { void g() { E e = a; } };
16356 };
16357
16358 When we instantiate f<7>::S::g(), say, lookup_name is not
16359 clever enough to find f<7>::a. */
16360 enum_type
16361 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16362 /*entering_scope=*/0);
16363
16364 for (v = TYPE_VALUES (enum_type);
16365 v != NULL_TREE;
16366 v = TREE_CHAIN (v))
16367 if (TREE_PURPOSE (v) == DECL_NAME (t))
16368 return TREE_VALUE (v);
16369
16370 /* We didn't find the name. That should never happen; if
16371 name-lookup found it during preliminary parsing, we
16372 should find it again here during instantiation. */
16373 gcc_unreachable ();
16374 }
16375 return t;
16376
16377 case FIELD_DECL:
16378 if (DECL_CONTEXT (t))
16379 {
16380 tree ctx;
16381
16382 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16383 /*entering_scope=*/1);
16384 if (ctx != DECL_CONTEXT (t))
16385 {
16386 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16387 if (!r)
16388 {
16389 if (complain & tf_error)
16390 error ("using invalid field %qD", t);
16391 return error_mark_node;
16392 }
16393 return r;
16394 }
16395 }
16396
16397 return t;
16398
16399 case VAR_DECL:
16400 case FUNCTION_DECL:
16401 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16402 r = tsubst (t, args, complain, in_decl);
16403 else if (local_variable_p (t)
16404 && uses_template_parms (DECL_CONTEXT (t)))
16405 {
16406 r = retrieve_local_specialization (t);
16407 if (r == NULL_TREE)
16408 {
16409 /* First try name lookup to find the instantiation. */
16410 r = lookup_name (DECL_NAME (t));
16411 if (r)
16412 {
16413 if (!VAR_P (r))
16414 {
16415 /* During error-recovery we may find a non-variable,
16416 even an OVERLOAD: just bail out and avoid ICEs and
16417 duplicate diagnostics (c++/62207). */
16418 gcc_assert (seen_error ());
16419 return error_mark_node;
16420 }
16421 if (!is_capture_proxy (r))
16422 {
16423 /* Make sure the one we found is the one we want. */
16424 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16425 if (ctx != DECL_CONTEXT (r))
16426 r = NULL_TREE;
16427 }
16428 }
16429
16430 if (r)
16431 /* OK */;
16432 else
16433 {
16434 /* This can happen for a variable used in a
16435 late-specified return type of a local lambda, or for a
16436 local static or constant. Building a new VAR_DECL
16437 should be OK in all those cases. */
16438 r = tsubst_decl (t, args, complain);
16439 if (local_specializations)
16440 /* Avoid infinite recursion (79640). */
16441 register_local_specialization (r, t);
16442 if (decl_maybe_constant_var_p (r))
16443 {
16444 /* We can't call cp_finish_decl, so handle the
16445 initializer by hand. */
16446 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16447 complain, in_decl);
16448 if (!processing_template_decl)
16449 init = maybe_constant_init (init);
16450 if (processing_template_decl
16451 ? potential_constant_expression (init)
16452 : reduced_constant_expression_p (init))
16453 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16454 = TREE_CONSTANT (r) = true;
16455 DECL_INITIAL (r) = init;
16456 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16457 TREE_TYPE (r)
16458 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16459 complain, adc_variable_type);
16460 }
16461 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16462 || decl_constant_var_p (r)
16463 || seen_error ());
16464 if (!processing_template_decl
16465 && !TREE_STATIC (r))
16466 r = process_outer_var_ref (r, complain);
16467 }
16468 /* Remember this for subsequent uses. */
16469 if (local_specializations)
16470 register_local_specialization (r, t);
16471 }
16472 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16473 r = argument_pack_select_arg (r);
16474 }
16475 else
16476 r = t;
16477 if (!mark_used (r, complain))
16478 return error_mark_node;
16479 return r;
16480
16481 case NAMESPACE_DECL:
16482 return t;
16483
16484 case OVERLOAD:
16485 return t;
16486
16487 case BASELINK:
16488 return tsubst_baselink (t, current_nonlambda_class_type (),
16489 args, complain, in_decl);
16490
16491 case TEMPLATE_DECL:
16492 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16493 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16494 args, complain, in_decl);
16495 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16496 return tsubst (t, args, complain, in_decl);
16497 else if (DECL_CLASS_SCOPE_P (t)
16498 && uses_template_parms (DECL_CONTEXT (t)))
16499 {
16500 /* Template template argument like the following example need
16501 special treatment:
16502
16503 template <template <class> class TT> struct C {};
16504 template <class T> struct D {
16505 template <class U> struct E {};
16506 C<E> c; // #1
16507 };
16508 D<int> d; // #2
16509
16510 We are processing the template argument `E' in #1 for
16511 the template instantiation #2. Originally, `E' is a
16512 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16513 have to substitute this with one having context `D<int>'. */
16514
16515 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16516 if (dependent_scope_p (context))
16517 {
16518 /* When rewriting a constructor into a deduction guide, a
16519 non-dependent name can become dependent, so memtmpl<args>
16520 becomes context::template memtmpl<args>. */
16521 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16522 return build_qualified_name (type, context, DECL_NAME (t),
16523 /*template*/true);
16524 }
16525 return lookup_field (context, DECL_NAME(t), 0, false);
16526 }
16527 else
16528 /* Ordinary template template argument. */
16529 return t;
16530
16531 case NON_LVALUE_EXPR:
16532 case VIEW_CONVERT_EXPR:
16533 {
16534 /* Handle location wrappers by substituting the wrapped node
16535 first, *then* reusing the resulting type. Doing the type
16536 first ensures that we handle template parameters and
16537 parameter pack expansions. */
16538 if (location_wrapper_p (t))
16539 {
16540 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16541 complain, in_decl);
16542 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16543 }
16544 tree op = TREE_OPERAND (t, 0);
16545 if (code == VIEW_CONVERT_EXPR
16546 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16547 {
16548 /* Wrapper to make a C++20 template parameter object const. */
16549 op = tsubst_copy (op, args, complain, in_decl);
16550 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16551 {
16552 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16553 return build1 (code, type, op);
16554 }
16555 else
16556 {
16557 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16558 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16559 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16560 return op;
16561 }
16562 }
16563 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16564 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16565 {
16566 op = tsubst_copy (op, args, complain, in_decl);
16567 op = build1 (code, TREE_TYPE (op), op);
16568 REF_PARENTHESIZED_P (op) = true;
16569 return op;
16570 }
16571 /* We shouldn't see any other uses of these in templates. */
16572 gcc_unreachable ();
16573 }
16574
16575 case CAST_EXPR:
16576 case REINTERPRET_CAST_EXPR:
16577 case CONST_CAST_EXPR:
16578 case STATIC_CAST_EXPR:
16579 case DYNAMIC_CAST_EXPR:
16580 case IMPLICIT_CONV_EXPR:
16581 case CONVERT_EXPR:
16582 case NOP_EXPR:
16583 {
16584 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16585 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16586 return build1 (code, type, op0);
16587 }
16588
16589 case SIZEOF_EXPR:
16590 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16591 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16592 {
16593 tree expanded, op = TREE_OPERAND (t, 0);
16594 int len = 0;
16595
16596 if (SIZEOF_EXPR_TYPE_P (t))
16597 op = TREE_TYPE (op);
16598
16599 ++cp_unevaluated_operand;
16600 ++c_inhibit_evaluation_warnings;
16601 /* We only want to compute the number of arguments. */
16602 if (PACK_EXPANSION_P (op))
16603 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16604 else
16605 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16606 args, complain, in_decl);
16607 --cp_unevaluated_operand;
16608 --c_inhibit_evaluation_warnings;
16609
16610 if (TREE_CODE (expanded) == TREE_VEC)
16611 {
16612 len = TREE_VEC_LENGTH (expanded);
16613 /* Set TREE_USED for the benefit of -Wunused. */
16614 for (int i = 0; i < len; i++)
16615 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16616 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16617 }
16618
16619 if (expanded == error_mark_node)
16620 return error_mark_node;
16621 else if (PACK_EXPANSION_P (expanded)
16622 || (TREE_CODE (expanded) == TREE_VEC
16623 && pack_expansion_args_count (expanded)))
16624
16625 {
16626 if (PACK_EXPANSION_P (expanded))
16627 /* OK. */;
16628 else if (TREE_VEC_LENGTH (expanded) == 1)
16629 expanded = TREE_VEC_ELT (expanded, 0);
16630 else
16631 expanded = make_argument_pack (expanded);
16632
16633 if (TYPE_P (expanded))
16634 return cxx_sizeof_or_alignof_type (input_location,
16635 expanded, SIZEOF_EXPR,
16636 false,
16637 complain & tf_error);
16638 else
16639 return cxx_sizeof_or_alignof_expr (input_location,
16640 expanded, SIZEOF_EXPR,
16641 complain & tf_error);
16642 }
16643 else
16644 return build_int_cst (size_type_node, len);
16645 }
16646 if (SIZEOF_EXPR_TYPE_P (t))
16647 {
16648 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16649 args, complain, in_decl);
16650 r = build1 (NOP_EXPR, r, error_mark_node);
16651 r = build1 (SIZEOF_EXPR,
16652 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16653 SIZEOF_EXPR_TYPE_P (r) = 1;
16654 return r;
16655 }
16656 /* Fall through */
16657
16658 case INDIRECT_REF:
16659 case NEGATE_EXPR:
16660 case TRUTH_NOT_EXPR:
16661 case BIT_NOT_EXPR:
16662 case ADDR_EXPR:
16663 case UNARY_PLUS_EXPR: /* Unary + */
16664 case ALIGNOF_EXPR:
16665 case AT_ENCODE_EXPR:
16666 case ARROW_EXPR:
16667 case THROW_EXPR:
16668 case TYPEID_EXPR:
16669 case REALPART_EXPR:
16670 case IMAGPART_EXPR:
16671 case PAREN_EXPR:
16672 {
16673 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16674 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16675 r = build1 (code, type, op0);
16676 if (code == ALIGNOF_EXPR)
16677 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16678 return r;
16679 }
16680
16681 case COMPONENT_REF:
16682 {
16683 tree object;
16684 tree name;
16685
16686 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16687 name = TREE_OPERAND (t, 1);
16688 if (TREE_CODE (name) == BIT_NOT_EXPR)
16689 {
16690 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16691 complain, in_decl);
16692 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16693 }
16694 else if (TREE_CODE (name) == SCOPE_REF
16695 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16696 {
16697 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16698 complain, in_decl);
16699 name = TREE_OPERAND (name, 1);
16700 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16701 complain, in_decl);
16702 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16703 name = build_qualified_name (/*type=*/NULL_TREE,
16704 base, name,
16705 /*template_p=*/false);
16706 }
16707 else if (BASELINK_P (name))
16708 name = tsubst_baselink (name,
16709 non_reference (TREE_TYPE (object)),
16710 args, complain,
16711 in_decl);
16712 else
16713 name = tsubst_copy (name, args, complain, in_decl);
16714 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16715 }
16716
16717 case PLUS_EXPR:
16718 case MINUS_EXPR:
16719 case MULT_EXPR:
16720 case TRUNC_DIV_EXPR:
16721 case CEIL_DIV_EXPR:
16722 case FLOOR_DIV_EXPR:
16723 case ROUND_DIV_EXPR:
16724 case EXACT_DIV_EXPR:
16725 case BIT_AND_EXPR:
16726 case BIT_IOR_EXPR:
16727 case BIT_XOR_EXPR:
16728 case TRUNC_MOD_EXPR:
16729 case FLOOR_MOD_EXPR:
16730 case TRUTH_ANDIF_EXPR:
16731 case TRUTH_ORIF_EXPR:
16732 case TRUTH_AND_EXPR:
16733 case TRUTH_OR_EXPR:
16734 case RSHIFT_EXPR:
16735 case LSHIFT_EXPR:
16736 case EQ_EXPR:
16737 case NE_EXPR:
16738 case MAX_EXPR:
16739 case MIN_EXPR:
16740 case LE_EXPR:
16741 case GE_EXPR:
16742 case LT_EXPR:
16743 case GT_EXPR:
16744 case COMPOUND_EXPR:
16745 case DOTSTAR_EXPR:
16746 case MEMBER_REF:
16747 case PREDECREMENT_EXPR:
16748 case PREINCREMENT_EXPR:
16749 case POSTDECREMENT_EXPR:
16750 case POSTINCREMENT_EXPR:
16751 {
16752 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16753 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16754 return build_nt (code, op0, op1);
16755 }
16756
16757 case SCOPE_REF:
16758 {
16759 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16760 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16761 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16762 QUALIFIED_NAME_IS_TEMPLATE (t));
16763 }
16764
16765 case ARRAY_REF:
16766 {
16767 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16768 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16769 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16770 }
16771
16772 case CALL_EXPR:
16773 {
16774 int n = VL_EXP_OPERAND_LENGTH (t);
16775 tree result = build_vl_exp (CALL_EXPR, n);
16776 int i;
16777 for (i = 0; i < n; i++)
16778 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16779 complain, in_decl);
16780 return result;
16781 }
16782
16783 case COND_EXPR:
16784 case MODOP_EXPR:
16785 case PSEUDO_DTOR_EXPR:
16786 case VEC_PERM_EXPR:
16787 {
16788 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16789 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16790 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16791 r = build_nt (code, op0, op1, op2);
16792 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16793 return r;
16794 }
16795
16796 case NEW_EXPR:
16797 {
16798 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16799 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16800 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16801 r = build_nt (code, op0, op1, op2);
16802 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16803 return r;
16804 }
16805
16806 case DELETE_EXPR:
16807 {
16808 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16809 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16810 r = build_nt (code, op0, op1);
16811 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16812 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16813 return r;
16814 }
16815
16816 case TEMPLATE_ID_EXPR:
16817 {
16818 /* Substituted template arguments */
16819 tree fn = TREE_OPERAND (t, 0);
16820 tree targs = TREE_OPERAND (t, 1);
16821
16822 fn = tsubst_copy (fn, args, complain, in_decl);
16823 if (targs)
16824 targs = tsubst_template_args (targs, args, complain, in_decl);
16825
16826 return lookup_template_function (fn, targs);
16827 }
16828
16829 case TREE_LIST:
16830 {
16831 tree purpose, value, chain;
16832
16833 if (t == void_list_node)
16834 return t;
16835
16836 purpose = TREE_PURPOSE (t);
16837 if (purpose)
16838 purpose = tsubst_copy (purpose, args, complain, in_decl);
16839 value = TREE_VALUE (t);
16840 if (value)
16841 value = tsubst_copy (value, args, complain, in_decl);
16842 chain = TREE_CHAIN (t);
16843 if (chain && chain != void_type_node)
16844 chain = tsubst_copy (chain, args, complain, in_decl);
16845 if (purpose == TREE_PURPOSE (t)
16846 && value == TREE_VALUE (t)
16847 && chain == TREE_CHAIN (t))
16848 return t;
16849 return tree_cons (purpose, value, chain);
16850 }
16851
16852 case RECORD_TYPE:
16853 case UNION_TYPE:
16854 case ENUMERAL_TYPE:
16855 case INTEGER_TYPE:
16856 case TEMPLATE_TYPE_PARM:
16857 case TEMPLATE_TEMPLATE_PARM:
16858 case BOUND_TEMPLATE_TEMPLATE_PARM:
16859 case TEMPLATE_PARM_INDEX:
16860 case POINTER_TYPE:
16861 case REFERENCE_TYPE:
16862 case OFFSET_TYPE:
16863 case FUNCTION_TYPE:
16864 case METHOD_TYPE:
16865 case ARRAY_TYPE:
16866 case TYPENAME_TYPE:
16867 case UNBOUND_CLASS_TEMPLATE:
16868 case TYPEOF_TYPE:
16869 case DECLTYPE_TYPE:
16870 case TYPE_DECL:
16871 return tsubst (t, args, complain, in_decl);
16872
16873 case USING_DECL:
16874 t = DECL_NAME (t);
16875 /* Fall through. */
16876 case IDENTIFIER_NODE:
16877 if (IDENTIFIER_CONV_OP_P (t))
16878 {
16879 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16880 return make_conv_op_name (new_type);
16881 }
16882 else
16883 return t;
16884
16885 case CONSTRUCTOR:
16886 /* This is handled by tsubst_copy_and_build. */
16887 gcc_unreachable ();
16888
16889 case VA_ARG_EXPR:
16890 {
16891 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16892 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16893 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16894 }
16895
16896 case CLEANUP_POINT_EXPR:
16897 /* We shouldn't have built any of these during initial template
16898 generation. Instead, they should be built during instantiation
16899 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16900 gcc_unreachable ();
16901
16902 case OFFSET_REF:
16903 {
16904 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16905 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16906 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16907 r = build2 (code, type, op0, op1);
16908 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16909 if (!mark_used (TREE_OPERAND (r, 1), complain)
16910 && !(complain & tf_error))
16911 return error_mark_node;
16912 return r;
16913 }
16914
16915 case EXPR_PACK_EXPANSION:
16916 error ("invalid use of pack expansion expression");
16917 return error_mark_node;
16918
16919 case NONTYPE_ARGUMENT_PACK:
16920 error ("use %<...%> to expand argument pack");
16921 return error_mark_node;
16922
16923 case VOID_CST:
16924 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16925 return t;
16926
16927 case INTEGER_CST:
16928 case REAL_CST:
16929 case COMPLEX_CST:
16930 {
16931 /* Instantiate any typedefs in the type. */
16932 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16933 r = fold_convert (type, t);
16934 gcc_assert (TREE_CODE (r) == code);
16935 return r;
16936 }
16937
16938 case STRING_CST:
16939 {
16940 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16941 r = t;
16942 if (type != TREE_TYPE (t))
16943 {
16944 r = copy_node (t);
16945 TREE_TYPE (r) = type;
16946 }
16947 return r;
16948 }
16949
16950 case PTRMEM_CST:
16951 /* These can sometimes show up in a partial instantiation, but never
16952 involve template parms. */
16953 gcc_assert (!uses_template_parms (t));
16954 return t;
16955
16956 case UNARY_LEFT_FOLD_EXPR:
16957 return tsubst_unary_left_fold (t, args, complain, in_decl);
16958 case UNARY_RIGHT_FOLD_EXPR:
16959 return tsubst_unary_right_fold (t, args, complain, in_decl);
16960 case BINARY_LEFT_FOLD_EXPR:
16961 return tsubst_binary_left_fold (t, args, complain, in_decl);
16962 case BINARY_RIGHT_FOLD_EXPR:
16963 return tsubst_binary_right_fold (t, args, complain, in_decl);
16964 case PREDICT_EXPR:
16965 return t;
16966
16967 case DEBUG_BEGIN_STMT:
16968 /* ??? There's no point in copying it for now, but maybe some
16969 day it will contain more information, such as a pointer back
16970 to the containing function, inlined copy or so. */
16971 return t;
16972
16973 case CO_AWAIT_EXPR:
16974 return tsubst_expr (t, args, complain, in_decl,
16975 /*integral_constant_expression_p=*/false);
16976 break;
16977
16978 default:
16979 /* We shouldn't get here, but keep going if !flag_checking. */
16980 if (flag_checking)
16981 gcc_unreachable ();
16982 return t;
16983 }
16984 }
16985
16986 /* Helper function for tsubst_omp_clauses, used for instantiation of
16987 OMP_CLAUSE_DECL of clauses. */
16988
16989 static tree
16990 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16991 tree in_decl, tree *iterator_cache)
16992 {
16993 if (decl == NULL_TREE)
16994 return NULL_TREE;
16995
16996 /* Handle OpenMP iterators. */
16997 if (TREE_CODE (decl) == TREE_LIST
16998 && TREE_PURPOSE (decl)
16999 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17000 {
17001 tree ret;
17002 if (iterator_cache[0] == TREE_PURPOSE (decl))
17003 ret = iterator_cache[1];
17004 else
17005 {
17006 tree *tp = &ret;
17007 begin_scope (sk_omp, NULL);
17008 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17009 {
17010 *tp = copy_node (it);
17011 TREE_VEC_ELT (*tp, 0)
17012 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17013 TREE_VEC_ELT (*tp, 1)
17014 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17015 /*integral_constant_expression_p=*/false);
17016 TREE_VEC_ELT (*tp, 2)
17017 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17018 /*integral_constant_expression_p=*/false);
17019 TREE_VEC_ELT (*tp, 3)
17020 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17021 /*integral_constant_expression_p=*/false);
17022 TREE_CHAIN (*tp) = NULL_TREE;
17023 tp = &TREE_CHAIN (*tp);
17024 }
17025 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17026 iterator_cache[0] = TREE_PURPOSE (decl);
17027 iterator_cache[1] = ret;
17028 }
17029 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17030 args, complain,
17031 in_decl, NULL));
17032 }
17033
17034 /* Handle an OpenMP array section represented as a TREE_LIST (or
17035 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17036 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17037 TREE_LIST. We can handle it exactly the same as an array section
17038 (purpose, value, and a chain), even though the nomenclature
17039 (low_bound, length, etc) is different. */
17040 if (TREE_CODE (decl) == TREE_LIST)
17041 {
17042 tree low_bound
17043 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17044 /*integral_constant_expression_p=*/false);
17045 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17046 /*integral_constant_expression_p=*/false);
17047 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17048 in_decl, NULL);
17049 if (TREE_PURPOSE (decl) == low_bound
17050 && TREE_VALUE (decl) == length
17051 && TREE_CHAIN (decl) == chain)
17052 return decl;
17053 tree ret = tree_cons (low_bound, length, chain);
17054 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17055 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17056 return ret;
17057 }
17058 tree ret = tsubst_expr (decl, args, complain, in_decl,
17059 /*integral_constant_expression_p=*/false);
17060 /* Undo convert_from_reference tsubst_expr could have called. */
17061 if (decl
17062 && REFERENCE_REF_P (ret)
17063 && !REFERENCE_REF_P (decl))
17064 ret = TREE_OPERAND (ret, 0);
17065 return ret;
17066 }
17067
17068 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17069
17070 static tree
17071 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17072 tree args, tsubst_flags_t complain, tree in_decl)
17073 {
17074 tree new_clauses = NULL_TREE, nc, oc;
17075 tree linear_no_step = NULL_TREE;
17076 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17077
17078 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17079 {
17080 nc = copy_node (oc);
17081 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17082 new_clauses = nc;
17083
17084 switch (OMP_CLAUSE_CODE (nc))
17085 {
17086 case OMP_CLAUSE_LASTPRIVATE:
17087 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17088 {
17089 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17090 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17091 in_decl, /*integral_constant_expression_p=*/false);
17092 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17093 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17094 }
17095 /* FALLTHRU */
17096 case OMP_CLAUSE_PRIVATE:
17097 case OMP_CLAUSE_SHARED:
17098 case OMP_CLAUSE_FIRSTPRIVATE:
17099 case OMP_CLAUSE_COPYIN:
17100 case OMP_CLAUSE_COPYPRIVATE:
17101 case OMP_CLAUSE_UNIFORM:
17102 case OMP_CLAUSE_DEPEND:
17103 case OMP_CLAUSE_FROM:
17104 case OMP_CLAUSE_TO:
17105 case OMP_CLAUSE_MAP:
17106 case OMP_CLAUSE_NONTEMPORAL:
17107 case OMP_CLAUSE_USE_DEVICE_PTR:
17108 case OMP_CLAUSE_USE_DEVICE_ADDR:
17109 case OMP_CLAUSE_IS_DEVICE_PTR:
17110 case OMP_CLAUSE_INCLUSIVE:
17111 case OMP_CLAUSE_EXCLUSIVE:
17112 OMP_CLAUSE_DECL (nc)
17113 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17114 in_decl, iterator_cache);
17115 break;
17116 case OMP_CLAUSE_TILE:
17117 case OMP_CLAUSE_IF:
17118 case OMP_CLAUSE_NUM_THREADS:
17119 case OMP_CLAUSE_SCHEDULE:
17120 case OMP_CLAUSE_COLLAPSE:
17121 case OMP_CLAUSE_FINAL:
17122 case OMP_CLAUSE_DEVICE:
17123 case OMP_CLAUSE_DIST_SCHEDULE:
17124 case OMP_CLAUSE_NUM_TEAMS:
17125 case OMP_CLAUSE_THREAD_LIMIT:
17126 case OMP_CLAUSE_SAFELEN:
17127 case OMP_CLAUSE_SIMDLEN:
17128 case OMP_CLAUSE_NUM_TASKS:
17129 case OMP_CLAUSE_GRAINSIZE:
17130 case OMP_CLAUSE_PRIORITY:
17131 case OMP_CLAUSE_ORDERED:
17132 case OMP_CLAUSE_HINT:
17133 case OMP_CLAUSE_NUM_GANGS:
17134 case OMP_CLAUSE_NUM_WORKERS:
17135 case OMP_CLAUSE_VECTOR_LENGTH:
17136 case OMP_CLAUSE_WORKER:
17137 case OMP_CLAUSE_VECTOR:
17138 case OMP_CLAUSE_ASYNC:
17139 case OMP_CLAUSE_WAIT:
17140 OMP_CLAUSE_OPERAND (nc, 0)
17141 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17142 in_decl, /*integral_constant_expression_p=*/false);
17143 break;
17144 case OMP_CLAUSE_REDUCTION:
17145 case OMP_CLAUSE_IN_REDUCTION:
17146 case OMP_CLAUSE_TASK_REDUCTION:
17147 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17148 {
17149 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17150 if (TREE_CODE (placeholder) == SCOPE_REF)
17151 {
17152 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17153 complain, in_decl);
17154 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17155 = build_qualified_name (NULL_TREE, scope,
17156 TREE_OPERAND (placeholder, 1),
17157 false);
17158 }
17159 else
17160 gcc_assert (identifier_p (placeholder));
17161 }
17162 OMP_CLAUSE_DECL (nc)
17163 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17164 in_decl, NULL);
17165 break;
17166 case OMP_CLAUSE_GANG:
17167 case OMP_CLAUSE_ALIGNED:
17168 OMP_CLAUSE_DECL (nc)
17169 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17170 in_decl, NULL);
17171 OMP_CLAUSE_OPERAND (nc, 1)
17172 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17173 in_decl, /*integral_constant_expression_p=*/false);
17174 break;
17175 case OMP_CLAUSE_LINEAR:
17176 OMP_CLAUSE_DECL (nc)
17177 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17178 in_decl, NULL);
17179 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17180 {
17181 gcc_assert (!linear_no_step);
17182 linear_no_step = nc;
17183 }
17184 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17185 OMP_CLAUSE_LINEAR_STEP (nc)
17186 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17187 complain, in_decl, NULL);
17188 else
17189 OMP_CLAUSE_LINEAR_STEP (nc)
17190 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17191 in_decl,
17192 /*integral_constant_expression_p=*/false);
17193 break;
17194 case OMP_CLAUSE_NOWAIT:
17195 case OMP_CLAUSE_DEFAULT:
17196 case OMP_CLAUSE_UNTIED:
17197 case OMP_CLAUSE_MERGEABLE:
17198 case OMP_CLAUSE_INBRANCH:
17199 case OMP_CLAUSE_NOTINBRANCH:
17200 case OMP_CLAUSE_PROC_BIND:
17201 case OMP_CLAUSE_FOR:
17202 case OMP_CLAUSE_PARALLEL:
17203 case OMP_CLAUSE_SECTIONS:
17204 case OMP_CLAUSE_TASKGROUP:
17205 case OMP_CLAUSE_NOGROUP:
17206 case OMP_CLAUSE_THREADS:
17207 case OMP_CLAUSE_SIMD:
17208 case OMP_CLAUSE_DEFAULTMAP:
17209 case OMP_CLAUSE_ORDER:
17210 case OMP_CLAUSE_BIND:
17211 case OMP_CLAUSE_INDEPENDENT:
17212 case OMP_CLAUSE_AUTO:
17213 case OMP_CLAUSE_SEQ:
17214 case OMP_CLAUSE_IF_PRESENT:
17215 case OMP_CLAUSE_FINALIZE:
17216 break;
17217 default:
17218 gcc_unreachable ();
17219 }
17220 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17221 switch (OMP_CLAUSE_CODE (nc))
17222 {
17223 case OMP_CLAUSE_SHARED:
17224 case OMP_CLAUSE_PRIVATE:
17225 case OMP_CLAUSE_FIRSTPRIVATE:
17226 case OMP_CLAUSE_LASTPRIVATE:
17227 case OMP_CLAUSE_COPYPRIVATE:
17228 case OMP_CLAUSE_LINEAR:
17229 case OMP_CLAUSE_REDUCTION:
17230 case OMP_CLAUSE_IN_REDUCTION:
17231 case OMP_CLAUSE_TASK_REDUCTION:
17232 case OMP_CLAUSE_USE_DEVICE_PTR:
17233 case OMP_CLAUSE_USE_DEVICE_ADDR:
17234 case OMP_CLAUSE_IS_DEVICE_PTR:
17235 case OMP_CLAUSE_INCLUSIVE:
17236 case OMP_CLAUSE_EXCLUSIVE:
17237 /* tsubst_expr on SCOPE_REF results in returning
17238 finish_non_static_data_member result. Undo that here. */
17239 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17240 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17241 == IDENTIFIER_NODE))
17242 {
17243 tree t = OMP_CLAUSE_DECL (nc);
17244 tree v = t;
17245 while (v)
17246 switch (TREE_CODE (v))
17247 {
17248 case COMPONENT_REF:
17249 case MEM_REF:
17250 case INDIRECT_REF:
17251 CASE_CONVERT:
17252 case POINTER_PLUS_EXPR:
17253 v = TREE_OPERAND (v, 0);
17254 continue;
17255 case PARM_DECL:
17256 if (DECL_CONTEXT (v) == current_function_decl
17257 && DECL_ARTIFICIAL (v)
17258 && DECL_NAME (v) == this_identifier)
17259 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17260 /* FALLTHRU */
17261 default:
17262 v = NULL_TREE;
17263 break;
17264 }
17265 }
17266 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17267 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17268 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17269 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17270 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17271 {
17272 tree decl = OMP_CLAUSE_DECL (nc);
17273 if (VAR_P (decl))
17274 {
17275 retrofit_lang_decl (decl);
17276 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17277 }
17278 }
17279 break;
17280 default:
17281 break;
17282 }
17283 }
17284
17285 new_clauses = nreverse (new_clauses);
17286 if (ort != C_ORT_OMP_DECLARE_SIMD)
17287 {
17288 new_clauses = finish_omp_clauses (new_clauses, ort);
17289 if (linear_no_step)
17290 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17291 if (nc == linear_no_step)
17292 {
17293 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17294 break;
17295 }
17296 }
17297 return new_clauses;
17298 }
17299
17300 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17301
17302 static tree
17303 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17304 tree in_decl)
17305 {
17306 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17307
17308 tree purpose, value, chain;
17309
17310 if (t == NULL)
17311 return t;
17312
17313 if (TREE_CODE (t) != TREE_LIST)
17314 return tsubst_copy_and_build (t, args, complain, in_decl,
17315 /*function_p=*/false,
17316 /*integral_constant_expression_p=*/false);
17317
17318 if (t == void_list_node)
17319 return t;
17320
17321 purpose = TREE_PURPOSE (t);
17322 if (purpose)
17323 purpose = RECUR (purpose);
17324 value = TREE_VALUE (t);
17325 if (value)
17326 {
17327 if (TREE_CODE (value) != LABEL_DECL)
17328 value = RECUR (value);
17329 else
17330 {
17331 value = lookup_label (DECL_NAME (value));
17332 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17333 TREE_USED (value) = 1;
17334 }
17335 }
17336 chain = TREE_CHAIN (t);
17337 if (chain && chain != void_type_node)
17338 chain = RECUR (chain);
17339 return tree_cons (purpose, value, chain);
17340 #undef RECUR
17341 }
17342
17343 /* Used to temporarily communicate the list of #pragma omp parallel
17344 clauses to #pragma omp for instantiation if they are combined
17345 together. */
17346
17347 static tree *omp_parallel_combined_clauses;
17348
17349 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17350 tree *, unsigned int *);
17351
17352 /* Substitute one OMP_FOR iterator. */
17353
17354 static bool
17355 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17356 tree initv, tree condv, tree incrv, tree *clauses,
17357 tree args, tsubst_flags_t complain, tree in_decl,
17358 bool integral_constant_expression_p)
17359 {
17360 #define RECUR(NODE) \
17361 tsubst_expr ((NODE), args, complain, in_decl, \
17362 integral_constant_expression_p)
17363 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17364 bool ret = false;
17365
17366 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17367 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17368
17369 decl = TREE_OPERAND (init, 0);
17370 init = TREE_OPERAND (init, 1);
17371 tree decl_expr = NULL_TREE;
17372 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17373 if (range_for)
17374 {
17375 bool decomp = false;
17376 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17377 {
17378 tree v = DECL_VALUE_EXPR (decl);
17379 if (TREE_CODE (v) == ARRAY_REF
17380 && VAR_P (TREE_OPERAND (v, 0))
17381 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17382 {
17383 tree decomp_first = NULL_TREE;
17384 unsigned decomp_cnt = 0;
17385 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17386 maybe_push_decl (d);
17387 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17388 in_decl, &decomp_first, &decomp_cnt);
17389 decomp = true;
17390 if (d == error_mark_node)
17391 decl = error_mark_node;
17392 else
17393 for (unsigned int i = 0; i < decomp_cnt; i++)
17394 {
17395 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17396 {
17397 tree v = build_nt (ARRAY_REF, d,
17398 size_int (decomp_cnt - i - 1),
17399 NULL_TREE, NULL_TREE);
17400 SET_DECL_VALUE_EXPR (decomp_first, v);
17401 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17402 }
17403 fit_decomposition_lang_decl (decomp_first, d);
17404 decomp_first = DECL_CHAIN (decomp_first);
17405 }
17406 }
17407 }
17408 decl = tsubst_decl (decl, args, complain);
17409 if (!decomp)
17410 maybe_push_decl (decl);
17411 }
17412 else if (init && TREE_CODE (init) == DECL_EXPR)
17413 {
17414 /* We need to jump through some hoops to handle declarations in the
17415 init-statement, since we might need to handle auto deduction,
17416 but we need to keep control of initialization. */
17417 decl_expr = init;
17418 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17419 decl = tsubst_decl (decl, args, complain);
17420 }
17421 else
17422 {
17423 if (TREE_CODE (decl) == SCOPE_REF)
17424 {
17425 decl = RECUR (decl);
17426 if (TREE_CODE (decl) == COMPONENT_REF)
17427 {
17428 tree v = decl;
17429 while (v)
17430 switch (TREE_CODE (v))
17431 {
17432 case COMPONENT_REF:
17433 case MEM_REF:
17434 case INDIRECT_REF:
17435 CASE_CONVERT:
17436 case POINTER_PLUS_EXPR:
17437 v = TREE_OPERAND (v, 0);
17438 continue;
17439 case PARM_DECL:
17440 if (DECL_CONTEXT (v) == current_function_decl
17441 && DECL_ARTIFICIAL (v)
17442 && DECL_NAME (v) == this_identifier)
17443 {
17444 decl = TREE_OPERAND (decl, 1);
17445 decl = omp_privatize_field (decl, false);
17446 }
17447 /* FALLTHRU */
17448 default:
17449 v = NULL_TREE;
17450 break;
17451 }
17452 }
17453 }
17454 else
17455 decl = RECUR (decl);
17456 }
17457 init = RECUR (init);
17458
17459 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17460 {
17461 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17462 if (TREE_CODE (o) == TREE_LIST)
17463 TREE_VEC_ELT (orig_declv, i)
17464 = tree_cons (RECUR (TREE_PURPOSE (o)),
17465 RECUR (TREE_VALUE (o)),
17466 NULL_TREE);
17467 else
17468 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17469 }
17470
17471 if (range_for)
17472 {
17473 tree this_pre_body = NULL_TREE;
17474 tree orig_init = NULL_TREE;
17475 tree orig_decl = NULL_TREE;
17476 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17477 orig_init, cond, incr);
17478 if (orig_decl)
17479 {
17480 if (orig_declv == NULL_TREE)
17481 orig_declv = copy_node (declv);
17482 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17483 ret = true;
17484 }
17485 else if (orig_declv)
17486 TREE_VEC_ELT (orig_declv, i) = decl;
17487 }
17488
17489 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17490 if (!range_for && auto_node && init)
17491 TREE_TYPE (decl)
17492 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17493
17494 gcc_assert (!type_dependent_expression_p (decl));
17495
17496 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17497 {
17498 if (decl_expr)
17499 {
17500 /* Declare the variable, but don't let that initialize it. */
17501 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17502 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17503 RECUR (decl_expr);
17504 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17505 }
17506
17507 if (!range_for)
17508 {
17509 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17510 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17511 if (TREE_CODE (incr) == MODIFY_EXPR)
17512 {
17513 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17514 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17515 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17516 NOP_EXPR, rhs, complain);
17517 }
17518 else
17519 incr = RECUR (incr);
17520 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17521 TREE_VEC_ELT (orig_declv, i) = decl;
17522 }
17523 TREE_VEC_ELT (declv, i) = decl;
17524 TREE_VEC_ELT (initv, i) = init;
17525 TREE_VEC_ELT (condv, i) = cond;
17526 TREE_VEC_ELT (incrv, i) = incr;
17527 return ret;
17528 }
17529
17530 if (decl_expr)
17531 {
17532 /* Declare and initialize the variable. */
17533 RECUR (decl_expr);
17534 init = NULL_TREE;
17535 }
17536 else if (init)
17537 {
17538 tree *pc;
17539 int j;
17540 for (j = ((omp_parallel_combined_clauses == NULL
17541 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17542 {
17543 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17544 {
17545 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17546 && OMP_CLAUSE_DECL (*pc) == decl)
17547 break;
17548 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17549 && OMP_CLAUSE_DECL (*pc) == decl)
17550 {
17551 if (j)
17552 break;
17553 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17554 tree c = *pc;
17555 *pc = OMP_CLAUSE_CHAIN (c);
17556 OMP_CLAUSE_CHAIN (c) = *clauses;
17557 *clauses = c;
17558 }
17559 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17560 && OMP_CLAUSE_DECL (*pc) == decl)
17561 {
17562 error ("iteration variable %qD should not be firstprivate",
17563 decl);
17564 *pc = OMP_CLAUSE_CHAIN (*pc);
17565 }
17566 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17567 && OMP_CLAUSE_DECL (*pc) == decl)
17568 {
17569 error ("iteration variable %qD should not be reduction",
17570 decl);
17571 *pc = OMP_CLAUSE_CHAIN (*pc);
17572 }
17573 else
17574 pc = &OMP_CLAUSE_CHAIN (*pc);
17575 }
17576 if (*pc)
17577 break;
17578 }
17579 if (*pc == NULL_TREE)
17580 {
17581 tree c = build_omp_clause (input_location,
17582 TREE_CODE (t) == OMP_LOOP
17583 ? OMP_CLAUSE_LASTPRIVATE
17584 : OMP_CLAUSE_PRIVATE);
17585 OMP_CLAUSE_DECL (c) = decl;
17586 c = finish_omp_clauses (c, C_ORT_OMP);
17587 if (c)
17588 {
17589 OMP_CLAUSE_CHAIN (c) = *clauses;
17590 *clauses = c;
17591 }
17592 }
17593 }
17594 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17595 if (COMPARISON_CLASS_P (cond))
17596 {
17597 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17598 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17599 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17600 }
17601 else
17602 cond = RECUR (cond);
17603 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17604 switch (TREE_CODE (incr))
17605 {
17606 case PREINCREMENT_EXPR:
17607 case PREDECREMENT_EXPR:
17608 case POSTINCREMENT_EXPR:
17609 case POSTDECREMENT_EXPR:
17610 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17611 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17612 break;
17613 case MODIFY_EXPR:
17614 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17615 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17616 {
17617 tree rhs = TREE_OPERAND (incr, 1);
17618 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17619 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17620 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17621 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17622 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17623 rhs0, rhs1));
17624 }
17625 else
17626 incr = RECUR (incr);
17627 break;
17628 case MODOP_EXPR:
17629 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17630 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17631 {
17632 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17633 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17634 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17635 TREE_TYPE (decl), lhs,
17636 RECUR (TREE_OPERAND (incr, 2))));
17637 }
17638 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17639 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17640 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17641 {
17642 tree rhs = TREE_OPERAND (incr, 2);
17643 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17644 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17645 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17646 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17647 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17648 rhs0, rhs1));
17649 }
17650 else
17651 incr = RECUR (incr);
17652 break;
17653 default:
17654 incr = RECUR (incr);
17655 break;
17656 }
17657
17658 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17659 TREE_VEC_ELT (orig_declv, i) = decl;
17660 TREE_VEC_ELT (declv, i) = decl;
17661 TREE_VEC_ELT (initv, i) = init;
17662 TREE_VEC_ELT (condv, i) = cond;
17663 TREE_VEC_ELT (incrv, i) = incr;
17664 return false;
17665 #undef RECUR
17666 }
17667
17668 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17669 of OMP_TARGET's body. */
17670
17671 static tree
17672 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17673 {
17674 *walk_subtrees = 0;
17675 switch (TREE_CODE (*tp))
17676 {
17677 case OMP_TEAMS:
17678 return *tp;
17679 case BIND_EXPR:
17680 case STATEMENT_LIST:
17681 *walk_subtrees = 1;
17682 break;
17683 default:
17684 break;
17685 }
17686 return NULL_TREE;
17687 }
17688
17689 /* Helper function for tsubst_expr. For decomposition declaration
17690 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17691 also the corresponding decls representing the identifiers
17692 of the decomposition declaration. Return DECL if successful
17693 or error_mark_node otherwise, set *FIRST to the first decl
17694 in the list chained through DECL_CHAIN and *CNT to the number
17695 of such decls. */
17696
17697 static tree
17698 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17699 tsubst_flags_t complain, tree in_decl, tree *first,
17700 unsigned int *cnt)
17701 {
17702 tree decl2, decl3, prev = decl;
17703 *cnt = 0;
17704 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17705 for (decl2 = DECL_CHAIN (pattern_decl);
17706 decl2
17707 && VAR_P (decl2)
17708 && DECL_DECOMPOSITION_P (decl2)
17709 && DECL_NAME (decl2);
17710 decl2 = DECL_CHAIN (decl2))
17711 {
17712 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17713 {
17714 gcc_assert (errorcount);
17715 return error_mark_node;
17716 }
17717 (*cnt)++;
17718 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17719 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17720 tree v = DECL_VALUE_EXPR (decl2);
17721 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17722 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17723 decl3 = tsubst (decl2, args, complain, in_decl);
17724 SET_DECL_VALUE_EXPR (decl2, v);
17725 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17726 if (VAR_P (decl3))
17727 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17728 else
17729 {
17730 gcc_assert (errorcount);
17731 decl = error_mark_node;
17732 continue;
17733 }
17734 maybe_push_decl (decl3);
17735 if (error_operand_p (decl3))
17736 decl = error_mark_node;
17737 else if (decl != error_mark_node
17738 && DECL_CHAIN (decl3) != prev
17739 && decl != prev)
17740 {
17741 gcc_assert (errorcount);
17742 decl = error_mark_node;
17743 }
17744 else
17745 prev = decl3;
17746 }
17747 *first = prev;
17748 return decl;
17749 }
17750
17751 /* Return the proper local_specialization for init-capture pack DECL. */
17752
17753 static tree
17754 lookup_init_capture_pack (tree decl)
17755 {
17756 /* We handle normal pack captures by forwarding to the specialization of the
17757 captured parameter. We can't do that for pack init-captures; we need them
17758 to have their own local_specialization. We created the individual
17759 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17760 when we process the DECL_EXPR for the pack init-capture in the template.
17761 So, how do we find them? We don't know the capture proxy pack when
17762 building the individual resulting proxies, and we don't know the
17763 individual proxies when instantiating the pack. What we have in common is
17764 the FIELD_DECL.
17765
17766 So...when we instantiate the FIELD_DECL, we stick the result in
17767 local_specializations. Then at the DECL_EXPR we look up that result, see
17768 how many elements it has, synthesize the names, and look them up. */
17769
17770 tree cname = DECL_NAME (decl);
17771 tree val = DECL_VALUE_EXPR (decl);
17772 tree field = TREE_OPERAND (val, 1);
17773 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17774 tree fpack = retrieve_local_specialization (field);
17775 if (fpack == error_mark_node)
17776 return error_mark_node;
17777
17778 int len = 1;
17779 tree vec = NULL_TREE;
17780 tree r = NULL_TREE;
17781 if (TREE_CODE (fpack) == TREE_VEC)
17782 {
17783 len = TREE_VEC_LENGTH (fpack);
17784 vec = make_tree_vec (len);
17785 r = make_node (NONTYPE_ARGUMENT_PACK);
17786 SET_ARGUMENT_PACK_ARGS (r, vec);
17787 }
17788 for (int i = 0; i < len; ++i)
17789 {
17790 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17791 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17792 if (vec)
17793 TREE_VEC_ELT (vec, i) = elt;
17794 else
17795 r = elt;
17796 }
17797 return r;
17798 }
17799
17800 /* Like tsubst_copy for expressions, etc. but also does semantic
17801 processing. */
17802
17803 tree
17804 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17805 bool integral_constant_expression_p)
17806 {
17807 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17808 #define RECUR(NODE) \
17809 tsubst_expr ((NODE), args, complain, in_decl, \
17810 integral_constant_expression_p)
17811
17812 tree stmt, tmp;
17813 tree r;
17814 location_t loc;
17815
17816 if (t == NULL_TREE || t == error_mark_node)
17817 return t;
17818
17819 loc = input_location;
17820 if (location_t eloc = cp_expr_location (t))
17821 input_location = eloc;
17822 if (STATEMENT_CODE_P (TREE_CODE (t)))
17823 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17824
17825 switch (TREE_CODE (t))
17826 {
17827 case STATEMENT_LIST:
17828 {
17829 tree_stmt_iterator i;
17830 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17831 RECUR (tsi_stmt (i));
17832 break;
17833 }
17834
17835 case CTOR_INITIALIZER:
17836 finish_mem_initializers (tsubst_initializer_list
17837 (TREE_OPERAND (t, 0), args));
17838 break;
17839
17840 case RETURN_EXPR:
17841 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17842 break;
17843
17844 case CO_RETURN_EXPR:
17845 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17846 break;
17847
17848 case CO_YIELD_EXPR:
17849 stmt = finish_co_yield_expr (input_location,
17850 RECUR (TREE_OPERAND (t, 0)));
17851 RETURN (stmt);
17852 break;
17853
17854 case CO_AWAIT_EXPR:
17855 stmt = finish_co_await_expr (input_location,
17856 RECUR (TREE_OPERAND (t, 0)));
17857 RETURN (stmt);
17858 break;
17859
17860 case EXPR_STMT:
17861 tmp = RECUR (EXPR_STMT_EXPR (t));
17862 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17863 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17864 else
17865 finish_expr_stmt (tmp);
17866 break;
17867
17868 case USING_STMT:
17869 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17870 break;
17871
17872 case DECL_EXPR:
17873 {
17874 tree decl, pattern_decl;
17875 tree init;
17876
17877 pattern_decl = decl = DECL_EXPR_DECL (t);
17878 if (TREE_CODE (decl) == LABEL_DECL)
17879 finish_label_decl (DECL_NAME (decl));
17880 else if (TREE_CODE (decl) == USING_DECL)
17881 {
17882 tree scope = USING_DECL_SCOPE (decl);
17883 tree name = DECL_NAME (decl);
17884
17885 scope = tsubst (scope, args, complain, in_decl);
17886 finish_nonmember_using_decl (scope, name);
17887 }
17888 else if (is_capture_proxy (decl)
17889 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17890 {
17891 /* We're in tsubst_lambda_expr, we've already inserted a new
17892 capture proxy, so look it up and register it. */
17893 tree inst;
17894 if (!DECL_PACK_P (decl))
17895 {
17896 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17897 /*nonclass*/1, /*block_p=*/true,
17898 /*ns_only*/0, LOOKUP_HIDDEN);
17899 gcc_assert (inst != decl && is_capture_proxy (inst));
17900 }
17901 else if (is_normal_capture_proxy (decl))
17902 {
17903 inst = (retrieve_local_specialization
17904 (DECL_CAPTURED_VARIABLE (decl)));
17905 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
17906 || DECL_PACK_P (inst));
17907 }
17908 else
17909 inst = lookup_init_capture_pack (decl);
17910
17911 register_local_specialization (inst, decl);
17912 break;
17913 }
17914 else if (DECL_PRETTY_FUNCTION_P (decl))
17915 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17916 DECL_NAME (decl),
17917 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17918 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17919 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17920 /* Don't copy the old closure; we'll create a new one in
17921 tsubst_lambda_expr. */
17922 break;
17923 else
17924 {
17925 init = DECL_INITIAL (decl);
17926 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17927 for local variables, so save if DECL was declared constinit. */
17928 const bool constinit_p
17929 = (VAR_P (decl)
17930 && DECL_LANG_SPECIFIC (decl)
17931 && DECL_TEMPLATE_INFO (decl)
17932 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17933 decl = tsubst (decl, args, complain, in_decl);
17934 if (decl != error_mark_node)
17935 {
17936 /* By marking the declaration as instantiated, we avoid
17937 trying to instantiate it. Since instantiate_decl can't
17938 handle local variables, and since we've already done
17939 all that needs to be done, that's the right thing to
17940 do. */
17941 if (VAR_P (decl))
17942 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17943 if (VAR_P (decl) && !DECL_NAME (decl)
17944 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17945 /* Anonymous aggregates are a special case. */
17946 finish_anon_union (decl);
17947 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17948 {
17949 DECL_CONTEXT (decl) = current_function_decl;
17950 if (DECL_NAME (decl) == this_identifier)
17951 {
17952 tree lam = DECL_CONTEXT (current_function_decl);
17953 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17954 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17955 }
17956 insert_capture_proxy (decl);
17957 }
17958 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17959 /* We already did a pushtag. */;
17960 else if (TREE_CODE (decl) == FUNCTION_DECL
17961 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17962 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17963 {
17964 DECL_CONTEXT (decl) = NULL_TREE;
17965 pushdecl (decl);
17966 DECL_CONTEXT (decl) = current_function_decl;
17967 cp_check_omp_declare_reduction (decl);
17968 }
17969 else
17970 {
17971 bool const_init = false;
17972 unsigned int cnt = 0;
17973 tree first = NULL_TREE, ndecl = error_mark_node;
17974 maybe_push_decl (decl);
17975
17976 if (VAR_P (decl)
17977 && DECL_DECOMPOSITION_P (decl)
17978 && TREE_TYPE (pattern_decl) != error_mark_node)
17979 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17980 complain, in_decl, &first,
17981 &cnt);
17982
17983 init = tsubst_init (init, decl, args, complain, in_decl);
17984
17985 if (VAR_P (decl))
17986 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17987 (pattern_decl));
17988
17989 if (ndecl != error_mark_node)
17990 cp_maybe_mangle_decomp (ndecl, first, cnt);
17991
17992 cp_finish_decl (decl, init, const_init, NULL_TREE,
17993 constinit_p ? LOOKUP_CONSTINIT : 0);
17994
17995 if (ndecl != error_mark_node)
17996 cp_finish_decomp (ndecl, first, cnt);
17997 }
17998 }
17999 }
18000
18001 break;
18002 }
18003
18004 case FOR_STMT:
18005 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18006 RECUR (FOR_INIT_STMT (t));
18007 finish_init_stmt (stmt);
18008 tmp = RECUR (FOR_COND (t));
18009 finish_for_cond (tmp, stmt, false, 0);
18010 tmp = RECUR (FOR_EXPR (t));
18011 finish_for_expr (tmp, stmt);
18012 {
18013 bool prev = note_iteration_stmt_body_start ();
18014 RECUR (FOR_BODY (t));
18015 note_iteration_stmt_body_end (prev);
18016 }
18017 finish_for_stmt (stmt);
18018 break;
18019
18020 case RANGE_FOR_STMT:
18021 {
18022 /* Construct another range_for, if this is not a final
18023 substitution (for inside a generic lambda of a
18024 template). Otherwise convert to a regular for. */
18025 tree decl, expr;
18026 stmt = (processing_template_decl
18027 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18028 : begin_for_stmt (NULL_TREE, NULL_TREE));
18029 RECUR (RANGE_FOR_INIT_STMT (t));
18030 decl = RANGE_FOR_DECL (t);
18031 decl = tsubst (decl, args, complain, in_decl);
18032 maybe_push_decl (decl);
18033 expr = RECUR (RANGE_FOR_EXPR (t));
18034
18035 tree decomp_first = NULL_TREE;
18036 unsigned decomp_cnt = 0;
18037 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18038 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18039 complain, in_decl,
18040 &decomp_first, &decomp_cnt);
18041
18042 if (processing_template_decl)
18043 {
18044 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18045 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18046 finish_range_for_decl (stmt, decl, expr);
18047 if (decomp_first && decl != error_mark_node)
18048 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18049 }
18050 else
18051 {
18052 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18053 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18054 stmt = cp_convert_range_for (stmt, decl, expr,
18055 decomp_first, decomp_cnt,
18056 RANGE_FOR_IVDEP (t), unroll);
18057 }
18058
18059 bool prev = note_iteration_stmt_body_start ();
18060 RECUR (RANGE_FOR_BODY (t));
18061 note_iteration_stmt_body_end (prev);
18062 finish_for_stmt (stmt);
18063 }
18064 break;
18065
18066 case WHILE_STMT:
18067 stmt = begin_while_stmt ();
18068 tmp = RECUR (WHILE_COND (t));
18069 finish_while_stmt_cond (tmp, stmt, false, 0);
18070 {
18071 bool prev = note_iteration_stmt_body_start ();
18072 RECUR (WHILE_BODY (t));
18073 note_iteration_stmt_body_end (prev);
18074 }
18075 finish_while_stmt (stmt);
18076 break;
18077
18078 case DO_STMT:
18079 stmt = begin_do_stmt ();
18080 {
18081 bool prev = note_iteration_stmt_body_start ();
18082 RECUR (DO_BODY (t));
18083 note_iteration_stmt_body_end (prev);
18084 }
18085 finish_do_body (stmt);
18086 tmp = RECUR (DO_COND (t));
18087 finish_do_stmt (tmp, stmt, false, 0);
18088 break;
18089
18090 case IF_STMT:
18091 stmt = begin_if_stmt ();
18092 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18093 if (IF_STMT_CONSTEXPR_P (t))
18094 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18095 tmp = RECUR (IF_COND (t));
18096 tmp = finish_if_stmt_cond (tmp, stmt);
18097 if (IF_STMT_CONSTEXPR_P (t)
18098 && instantiation_dependent_expression_p (tmp))
18099 {
18100 /* We're partially instantiating a generic lambda, but the condition
18101 of the constexpr if is still dependent. Don't substitute into the
18102 branches now, just remember the template arguments. */
18103 do_poplevel (IF_SCOPE (stmt));
18104 IF_COND (stmt) = IF_COND (t);
18105 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18106 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18107 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18108 add_stmt (stmt);
18109 break;
18110 }
18111 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18112 /* Don't instantiate the THEN_CLAUSE. */;
18113 else
18114 {
18115 tree folded = fold_non_dependent_expr (tmp, complain);
18116 bool inhibit = integer_zerop (folded);
18117 if (inhibit)
18118 ++c_inhibit_evaluation_warnings;
18119 RECUR (THEN_CLAUSE (t));
18120 if (inhibit)
18121 --c_inhibit_evaluation_warnings;
18122 }
18123 finish_then_clause (stmt);
18124
18125 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18126 /* Don't instantiate the ELSE_CLAUSE. */;
18127 else if (ELSE_CLAUSE (t))
18128 {
18129 tree folded = fold_non_dependent_expr (tmp, complain);
18130 bool inhibit = integer_nonzerop (folded);
18131 begin_else_clause (stmt);
18132 if (inhibit)
18133 ++c_inhibit_evaluation_warnings;
18134 RECUR (ELSE_CLAUSE (t));
18135 if (inhibit)
18136 --c_inhibit_evaluation_warnings;
18137 finish_else_clause (stmt);
18138 }
18139
18140 finish_if_stmt (stmt);
18141 break;
18142
18143 case BIND_EXPR:
18144 if (BIND_EXPR_BODY_BLOCK (t))
18145 stmt = begin_function_body ();
18146 else
18147 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18148 ? BCS_TRY_BLOCK : 0);
18149
18150 RECUR (BIND_EXPR_BODY (t));
18151
18152 if (BIND_EXPR_BODY_BLOCK (t))
18153 finish_function_body (stmt);
18154 else
18155 finish_compound_stmt (stmt);
18156 break;
18157
18158 case BREAK_STMT:
18159 finish_break_stmt ();
18160 break;
18161
18162 case CONTINUE_STMT:
18163 finish_continue_stmt ();
18164 break;
18165
18166 case SWITCH_STMT:
18167 stmt = begin_switch_stmt ();
18168 tmp = RECUR (SWITCH_STMT_COND (t));
18169 finish_switch_cond (tmp, stmt);
18170 RECUR (SWITCH_STMT_BODY (t));
18171 finish_switch_stmt (stmt);
18172 break;
18173
18174 case CASE_LABEL_EXPR:
18175 {
18176 tree decl = CASE_LABEL (t);
18177 tree low = RECUR (CASE_LOW (t));
18178 tree high = RECUR (CASE_HIGH (t));
18179 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18180 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18181 {
18182 tree label = CASE_LABEL (l);
18183 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18184 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18185 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18186 }
18187 }
18188 break;
18189
18190 case LABEL_EXPR:
18191 {
18192 tree decl = LABEL_EXPR_LABEL (t);
18193 tree label;
18194
18195 label = finish_label_stmt (DECL_NAME (decl));
18196 if (TREE_CODE (label) == LABEL_DECL)
18197 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18198 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18199 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18200 }
18201 break;
18202
18203 case GOTO_EXPR:
18204 tmp = GOTO_DESTINATION (t);
18205 if (TREE_CODE (tmp) != LABEL_DECL)
18206 /* Computed goto's must be tsubst'd into. On the other hand,
18207 non-computed gotos must not be; the identifier in question
18208 will have no binding. */
18209 tmp = RECUR (tmp);
18210 else
18211 tmp = DECL_NAME (tmp);
18212 finish_goto_stmt (tmp);
18213 break;
18214
18215 case ASM_EXPR:
18216 {
18217 tree string = RECUR (ASM_STRING (t));
18218 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18219 complain, in_decl);
18220 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18221 complain, in_decl);
18222 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18223 complain, in_decl);
18224 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18225 complain, in_decl);
18226 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18227 outputs, inputs, clobbers, labels,
18228 ASM_INLINE_P (t));
18229 tree asm_expr = tmp;
18230 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18231 asm_expr = TREE_OPERAND (asm_expr, 0);
18232 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18233 }
18234 break;
18235
18236 case TRY_BLOCK:
18237 if (CLEANUP_P (t))
18238 {
18239 stmt = begin_try_block ();
18240 RECUR (TRY_STMTS (t));
18241 finish_cleanup_try_block (stmt);
18242 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18243 }
18244 else
18245 {
18246 tree compound_stmt = NULL_TREE;
18247
18248 if (FN_TRY_BLOCK_P (t))
18249 stmt = begin_function_try_block (&compound_stmt);
18250 else
18251 stmt = begin_try_block ();
18252
18253 RECUR (TRY_STMTS (t));
18254
18255 if (FN_TRY_BLOCK_P (t))
18256 finish_function_try_block (stmt);
18257 else
18258 finish_try_block (stmt);
18259
18260 RECUR (TRY_HANDLERS (t));
18261 if (FN_TRY_BLOCK_P (t))
18262 finish_function_handler_sequence (stmt, compound_stmt);
18263 else
18264 finish_handler_sequence (stmt);
18265 }
18266 break;
18267
18268 case HANDLER:
18269 {
18270 tree decl = HANDLER_PARMS (t);
18271
18272 if (decl)
18273 {
18274 decl = tsubst (decl, args, complain, in_decl);
18275 /* Prevent instantiate_decl from trying to instantiate
18276 this variable. We've already done all that needs to be
18277 done. */
18278 if (decl != error_mark_node)
18279 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18280 }
18281 stmt = begin_handler ();
18282 finish_handler_parms (decl, stmt);
18283 RECUR (HANDLER_BODY (t));
18284 finish_handler (stmt);
18285 }
18286 break;
18287
18288 case TAG_DEFN:
18289 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18290 if (CLASS_TYPE_P (tmp))
18291 {
18292 /* Local classes are not independent templates; they are
18293 instantiated along with their containing function. And this
18294 way we don't have to deal with pushing out of one local class
18295 to instantiate a member of another local class. */
18296 /* Closures are handled by the LAMBDA_EXPR. */
18297 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18298 complete_type (tmp);
18299 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18300 if ((VAR_P (fld)
18301 || (TREE_CODE (fld) == FUNCTION_DECL
18302 && !DECL_ARTIFICIAL (fld)))
18303 && DECL_TEMPLATE_INSTANTIATION (fld))
18304 instantiate_decl (fld, /*defer_ok=*/false,
18305 /*expl_inst_class=*/false);
18306 }
18307 break;
18308
18309 case STATIC_ASSERT:
18310 {
18311 tree condition;
18312
18313 ++c_inhibit_evaluation_warnings;
18314 condition =
18315 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18316 args,
18317 complain, in_decl,
18318 /*integral_constant_expression_p=*/true);
18319 --c_inhibit_evaluation_warnings;
18320
18321 finish_static_assert (condition,
18322 STATIC_ASSERT_MESSAGE (t),
18323 STATIC_ASSERT_SOURCE_LOCATION (t),
18324 /*member_p=*/false);
18325 }
18326 break;
18327
18328 case OACC_KERNELS:
18329 case OACC_PARALLEL:
18330 case OACC_SERIAL:
18331 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18332 in_decl);
18333 stmt = begin_omp_parallel ();
18334 RECUR (OMP_BODY (t));
18335 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18336 break;
18337
18338 case OMP_PARALLEL:
18339 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18340 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18341 complain, in_decl);
18342 if (OMP_PARALLEL_COMBINED (t))
18343 omp_parallel_combined_clauses = &tmp;
18344 stmt = begin_omp_parallel ();
18345 RECUR (OMP_PARALLEL_BODY (t));
18346 gcc_assert (omp_parallel_combined_clauses == NULL);
18347 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18348 = OMP_PARALLEL_COMBINED (t);
18349 pop_omp_privatization_clauses (r);
18350 break;
18351
18352 case OMP_TASK:
18353 if (OMP_TASK_BODY (t) == NULL_TREE)
18354 {
18355 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18356 complain, in_decl);
18357 t = copy_node (t);
18358 OMP_TASK_CLAUSES (t) = tmp;
18359 add_stmt (t);
18360 break;
18361 }
18362 r = push_omp_privatization_clauses (false);
18363 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18364 complain, in_decl);
18365 stmt = begin_omp_task ();
18366 RECUR (OMP_TASK_BODY (t));
18367 finish_omp_task (tmp, stmt);
18368 pop_omp_privatization_clauses (r);
18369 break;
18370
18371 case OMP_FOR:
18372 case OMP_LOOP:
18373 case OMP_SIMD:
18374 case OMP_DISTRIBUTE:
18375 case OMP_TASKLOOP:
18376 case OACC_LOOP:
18377 {
18378 tree clauses, body, pre_body;
18379 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18380 tree orig_declv = NULL_TREE;
18381 tree incrv = NULL_TREE;
18382 enum c_omp_region_type ort = C_ORT_OMP;
18383 bool any_range_for = false;
18384 int i;
18385
18386 if (TREE_CODE (t) == OACC_LOOP)
18387 ort = C_ORT_ACC;
18388
18389 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18390 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18391 in_decl);
18392 if (OMP_FOR_INIT (t) != NULL_TREE)
18393 {
18394 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18395 if (OMP_FOR_ORIG_DECLS (t))
18396 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18397 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18398 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18399 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18400 }
18401
18402 keep_next_level (true);
18403 stmt = begin_omp_structured_block ();
18404
18405 pre_body = push_stmt_list ();
18406 RECUR (OMP_FOR_PRE_BODY (t));
18407 pre_body = pop_stmt_list (pre_body);
18408
18409 if (OMP_FOR_INIT (t) != NULL_TREE)
18410 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18411 any_range_for
18412 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18413 condv, incrv, &clauses, args,
18414 complain, in_decl,
18415 integral_constant_expression_p);
18416 omp_parallel_combined_clauses = NULL;
18417
18418 if (any_range_for)
18419 {
18420 gcc_assert (orig_declv);
18421 body = begin_omp_structured_block ();
18422 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18423 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18424 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18425 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18426 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18427 TREE_VEC_ELT (declv, i));
18428 }
18429 else
18430 body = push_stmt_list ();
18431 RECUR (OMP_FOR_BODY (t));
18432 if (any_range_for)
18433 body = finish_omp_structured_block (body);
18434 else
18435 body = pop_stmt_list (body);
18436
18437 if (OMP_FOR_INIT (t) != NULL_TREE)
18438 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18439 orig_declv, initv, condv, incrv, body, pre_body,
18440 NULL, clauses);
18441 else
18442 {
18443 t = make_node (TREE_CODE (t));
18444 TREE_TYPE (t) = void_type_node;
18445 OMP_FOR_BODY (t) = body;
18446 OMP_FOR_PRE_BODY (t) = pre_body;
18447 OMP_FOR_CLAUSES (t) = clauses;
18448 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18449 add_stmt (t);
18450 }
18451
18452 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18453 t));
18454 pop_omp_privatization_clauses (r);
18455 }
18456 break;
18457
18458 case OMP_SECTIONS:
18459 omp_parallel_combined_clauses = NULL;
18460 /* FALLTHRU */
18461 case OMP_SINGLE:
18462 case OMP_TEAMS:
18463 case OMP_CRITICAL:
18464 case OMP_TASKGROUP:
18465 case OMP_SCAN:
18466 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18467 && OMP_TEAMS_COMBINED (t));
18468 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18469 in_decl);
18470 if (TREE_CODE (t) == OMP_TEAMS)
18471 {
18472 keep_next_level (true);
18473 stmt = begin_omp_structured_block ();
18474 RECUR (OMP_BODY (t));
18475 stmt = finish_omp_structured_block (stmt);
18476 }
18477 else
18478 {
18479 stmt = push_stmt_list ();
18480 RECUR (OMP_BODY (t));
18481 stmt = pop_stmt_list (stmt);
18482 }
18483
18484 t = copy_node (t);
18485 OMP_BODY (t) = stmt;
18486 OMP_CLAUSES (t) = tmp;
18487 add_stmt (t);
18488 pop_omp_privatization_clauses (r);
18489 break;
18490
18491 case OMP_DEPOBJ:
18492 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18493 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18494 {
18495 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18496 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18497 {
18498 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18499 args, complain, in_decl);
18500 if (tmp == NULL_TREE)
18501 tmp = error_mark_node;
18502 }
18503 else
18504 {
18505 kind = (enum omp_clause_depend_kind)
18506 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18507 tmp = NULL_TREE;
18508 }
18509 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18510 }
18511 else
18512 finish_omp_depobj (EXPR_LOCATION (t), r,
18513 OMP_CLAUSE_DEPEND_SOURCE,
18514 OMP_DEPOBJ_CLAUSES (t));
18515 break;
18516
18517 case OACC_DATA:
18518 case OMP_TARGET_DATA:
18519 case OMP_TARGET:
18520 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18521 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18522 in_decl);
18523 keep_next_level (true);
18524 stmt = begin_omp_structured_block ();
18525
18526 RECUR (OMP_BODY (t));
18527 stmt = finish_omp_structured_block (stmt);
18528
18529 t = copy_node (t);
18530 OMP_BODY (t) = stmt;
18531 OMP_CLAUSES (t) = tmp;
18532 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18533 {
18534 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18535 if (teams)
18536 {
18537 /* For combined target teams, ensure the num_teams and
18538 thread_limit clause expressions are evaluated on the host,
18539 before entering the target construct. */
18540 tree c;
18541 for (c = OMP_TEAMS_CLAUSES (teams);
18542 c; c = OMP_CLAUSE_CHAIN (c))
18543 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18544 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18545 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18546 {
18547 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18548 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18549 if (expr == error_mark_node)
18550 continue;
18551 tmp = TARGET_EXPR_SLOT (expr);
18552 add_stmt (expr);
18553 OMP_CLAUSE_OPERAND (c, 0) = expr;
18554 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18555 OMP_CLAUSE_FIRSTPRIVATE);
18556 OMP_CLAUSE_DECL (tc) = tmp;
18557 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18558 OMP_TARGET_CLAUSES (t) = tc;
18559 }
18560 }
18561 }
18562 add_stmt (t);
18563 break;
18564
18565 case OACC_DECLARE:
18566 t = copy_node (t);
18567 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18568 complain, in_decl);
18569 OACC_DECLARE_CLAUSES (t) = tmp;
18570 add_stmt (t);
18571 break;
18572
18573 case OMP_TARGET_UPDATE:
18574 case OMP_TARGET_ENTER_DATA:
18575 case OMP_TARGET_EXIT_DATA:
18576 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18577 complain, in_decl);
18578 t = copy_node (t);
18579 OMP_STANDALONE_CLAUSES (t) = tmp;
18580 add_stmt (t);
18581 break;
18582
18583 case OACC_ENTER_DATA:
18584 case OACC_EXIT_DATA:
18585 case OACC_UPDATE:
18586 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18587 complain, in_decl);
18588 t = copy_node (t);
18589 OMP_STANDALONE_CLAUSES (t) = tmp;
18590 add_stmt (t);
18591 break;
18592
18593 case OMP_ORDERED:
18594 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18595 complain, in_decl);
18596 stmt = push_stmt_list ();
18597 RECUR (OMP_BODY (t));
18598 stmt = pop_stmt_list (stmt);
18599
18600 t = copy_node (t);
18601 OMP_BODY (t) = stmt;
18602 OMP_ORDERED_CLAUSES (t) = tmp;
18603 add_stmt (t);
18604 break;
18605
18606 case OMP_MASTER:
18607 omp_parallel_combined_clauses = NULL;
18608 /* FALLTHRU */
18609 case OMP_SECTION:
18610 stmt = push_stmt_list ();
18611 RECUR (OMP_BODY (t));
18612 stmt = pop_stmt_list (stmt);
18613
18614 t = copy_node (t);
18615 OMP_BODY (t) = stmt;
18616 add_stmt (t);
18617 break;
18618
18619 case OMP_ATOMIC:
18620 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18621 tmp = NULL_TREE;
18622 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18623 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18624 complain, in_decl);
18625 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18626 {
18627 tree op1 = TREE_OPERAND (t, 1);
18628 tree rhs1 = NULL_TREE;
18629 tree lhs, rhs;
18630 if (TREE_CODE (op1) == COMPOUND_EXPR)
18631 {
18632 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18633 op1 = TREE_OPERAND (op1, 1);
18634 }
18635 lhs = RECUR (TREE_OPERAND (op1, 0));
18636 rhs = RECUR (TREE_OPERAND (op1, 1));
18637 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18638 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18639 OMP_ATOMIC_MEMORY_ORDER (t));
18640 }
18641 else
18642 {
18643 tree op1 = TREE_OPERAND (t, 1);
18644 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18645 tree rhs1 = NULL_TREE;
18646 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18647 enum tree_code opcode = NOP_EXPR;
18648 if (code == OMP_ATOMIC_READ)
18649 {
18650 v = RECUR (TREE_OPERAND (op1, 0));
18651 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18652 }
18653 else if (code == OMP_ATOMIC_CAPTURE_OLD
18654 || code == OMP_ATOMIC_CAPTURE_NEW)
18655 {
18656 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18657 v = RECUR (TREE_OPERAND (op1, 0));
18658 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18659 if (TREE_CODE (op11) == COMPOUND_EXPR)
18660 {
18661 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18662 op11 = TREE_OPERAND (op11, 1);
18663 }
18664 lhs = RECUR (TREE_OPERAND (op11, 0));
18665 rhs = RECUR (TREE_OPERAND (op11, 1));
18666 opcode = TREE_CODE (op11);
18667 if (opcode == MODIFY_EXPR)
18668 opcode = NOP_EXPR;
18669 }
18670 else
18671 {
18672 code = OMP_ATOMIC;
18673 lhs = RECUR (TREE_OPERAND (op1, 0));
18674 rhs = RECUR (TREE_OPERAND (op1, 1));
18675 }
18676 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18677 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18678 }
18679 break;
18680
18681 case TRANSACTION_EXPR:
18682 {
18683 int flags = 0;
18684 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18685 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18686
18687 if (TRANSACTION_EXPR_IS_STMT (t))
18688 {
18689 tree body = TRANSACTION_EXPR_BODY (t);
18690 tree noex = NULL_TREE;
18691 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18692 {
18693 noex = MUST_NOT_THROW_COND (body);
18694 if (noex == NULL_TREE)
18695 noex = boolean_true_node;
18696 body = TREE_OPERAND (body, 0);
18697 }
18698 stmt = begin_transaction_stmt (input_location, NULL, flags);
18699 RECUR (body);
18700 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18701 }
18702 else
18703 {
18704 stmt = build_transaction_expr (EXPR_LOCATION (t),
18705 RECUR (TRANSACTION_EXPR_BODY (t)),
18706 flags, NULL_TREE);
18707 RETURN (stmt);
18708 }
18709 }
18710 break;
18711
18712 case MUST_NOT_THROW_EXPR:
18713 {
18714 tree op0 = RECUR (TREE_OPERAND (t, 0));
18715 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18716 RETURN (build_must_not_throw_expr (op0, cond));
18717 }
18718
18719 case EXPR_PACK_EXPANSION:
18720 error ("invalid use of pack expansion expression");
18721 RETURN (error_mark_node);
18722
18723 case NONTYPE_ARGUMENT_PACK:
18724 error ("use %<...%> to expand argument pack");
18725 RETURN (error_mark_node);
18726
18727 case COMPOUND_EXPR:
18728 tmp = RECUR (TREE_OPERAND (t, 0));
18729 if (tmp == NULL_TREE)
18730 /* If the first operand was a statement, we're done with it. */
18731 RETURN (RECUR (TREE_OPERAND (t, 1)));
18732 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18733 RECUR (TREE_OPERAND (t, 1)),
18734 complain));
18735
18736 case ANNOTATE_EXPR:
18737 tmp = RECUR (TREE_OPERAND (t, 0));
18738 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18739 TREE_TYPE (tmp), tmp,
18740 RECUR (TREE_OPERAND (t, 1)),
18741 RECUR (TREE_OPERAND (t, 2))));
18742
18743 case PREDICT_EXPR:
18744 RETURN (add_stmt (copy_node (t)));
18745
18746 default:
18747 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18748
18749 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18750 /*function_p=*/false,
18751 integral_constant_expression_p));
18752 }
18753
18754 RETURN (NULL_TREE);
18755 out:
18756 input_location = loc;
18757 return r;
18758 #undef RECUR
18759 #undef RETURN
18760 }
18761
18762 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18763 function. For description of the body see comment above
18764 cp_parser_omp_declare_reduction_exprs. */
18765
18766 static void
18767 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18768 {
18769 if (t == NULL_TREE || t == error_mark_node)
18770 return;
18771
18772 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18773
18774 tree_stmt_iterator tsi;
18775 int i;
18776 tree stmts[7];
18777 memset (stmts, 0, sizeof stmts);
18778 for (i = 0, tsi = tsi_start (t);
18779 i < 7 && !tsi_end_p (tsi);
18780 i++, tsi_next (&tsi))
18781 stmts[i] = tsi_stmt (tsi);
18782 gcc_assert (tsi_end_p (tsi));
18783
18784 if (i >= 3)
18785 {
18786 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18787 && TREE_CODE (stmts[1]) == DECL_EXPR);
18788 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18789 args, complain, in_decl);
18790 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18791 args, complain, in_decl);
18792 DECL_CONTEXT (omp_out) = current_function_decl;
18793 DECL_CONTEXT (omp_in) = current_function_decl;
18794 keep_next_level (true);
18795 tree block = begin_omp_structured_block ();
18796 tsubst_expr (stmts[2], args, complain, in_decl, false);
18797 block = finish_omp_structured_block (block);
18798 block = maybe_cleanup_point_expr_void (block);
18799 add_decl_expr (omp_out);
18800 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18801 TREE_NO_WARNING (omp_out) = 1;
18802 add_decl_expr (omp_in);
18803 finish_expr_stmt (block);
18804 }
18805 if (i >= 6)
18806 {
18807 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18808 && TREE_CODE (stmts[4]) == DECL_EXPR);
18809 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18810 args, complain, in_decl);
18811 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18812 args, complain, in_decl);
18813 DECL_CONTEXT (omp_priv) = current_function_decl;
18814 DECL_CONTEXT (omp_orig) = current_function_decl;
18815 keep_next_level (true);
18816 tree block = begin_omp_structured_block ();
18817 tsubst_expr (stmts[5], args, complain, in_decl, false);
18818 block = finish_omp_structured_block (block);
18819 block = maybe_cleanup_point_expr_void (block);
18820 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18821 add_decl_expr (omp_priv);
18822 add_decl_expr (omp_orig);
18823 finish_expr_stmt (block);
18824 if (i == 7)
18825 add_decl_expr (omp_orig);
18826 }
18827 }
18828
18829 /* T is a postfix-expression that is not being used in a function
18830 call. Return the substituted version of T. */
18831
18832 static tree
18833 tsubst_non_call_postfix_expression (tree t, tree args,
18834 tsubst_flags_t complain,
18835 tree in_decl)
18836 {
18837 if (TREE_CODE (t) == SCOPE_REF)
18838 t = tsubst_qualified_id (t, args, complain, in_decl,
18839 /*done=*/false, /*address_p=*/false);
18840 else
18841 t = tsubst_copy_and_build (t, args, complain, in_decl,
18842 /*function_p=*/false,
18843 /*integral_constant_expression_p=*/false);
18844
18845 return t;
18846 }
18847
18848 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18849 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18850 dependent init-capture. */
18851
18852 static void
18853 prepend_one_capture (tree field, tree init, tree &list,
18854 tsubst_flags_t complain)
18855 {
18856 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18857 {
18858 tree type = NULL_TREE;
18859 if (!init)
18860 {
18861 if (complain & tf_error)
18862 error ("empty initializer in lambda init-capture");
18863 init = error_mark_node;
18864 }
18865 else if (TREE_CODE (init) == TREE_LIST)
18866 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18867 if (!type)
18868 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18869 TREE_TYPE (field) = type;
18870 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18871 }
18872 list = tree_cons (field, init, list);
18873 }
18874
18875 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18876 instantiation context. Instantiating a pack expansion containing a lambda
18877 might result in multiple lambdas all based on the same lambda in the
18878 template. */
18879
18880 tree
18881 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18882 {
18883 tree oldfn = lambda_function (t);
18884 in_decl = oldfn;
18885
18886 tree r = build_lambda_expr ();
18887
18888 LAMBDA_EXPR_LOCATION (r)
18889 = LAMBDA_EXPR_LOCATION (t);
18890 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18891 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18892 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18893 LAMBDA_EXPR_INSTANTIATED (r) = true;
18894
18895 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18896 /* A lambda in a default argument outside a class gets no
18897 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18898 tsubst_default_argument calls start_lambda_scope, so we need to
18899 specifically ignore it here, and use the global scope. */
18900 record_null_lambda_scope (r);
18901 else
18902 record_lambda_scope (r);
18903
18904 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18905 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18906
18907 vec<tree,va_gc>* field_packs = NULL;
18908
18909 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18910 cap = TREE_CHAIN (cap))
18911 {
18912 tree ofield = TREE_PURPOSE (cap);
18913 tree init = TREE_VALUE (cap);
18914 if (PACK_EXPANSION_P (init))
18915 init = tsubst_pack_expansion (init, args, complain, in_decl);
18916 else
18917 init = tsubst_copy_and_build (init, args, complain, in_decl,
18918 /*fn*/false, /*constexpr*/false);
18919
18920 if (init == error_mark_node)
18921 return error_mark_node;
18922
18923 if (init && TREE_CODE (init) == TREE_LIST)
18924 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18925
18926 if (!processing_template_decl
18927 && init && TREE_CODE (init) != TREE_VEC
18928 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
18929 {
18930 /* For a VLA, simply tsubsting the field type won't work, we need to
18931 go through add_capture again. XXX do we want to do this for all
18932 captures? */
18933 tree name = (get_identifier
18934 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
18935 tree ftype = TREE_TYPE (ofield);
18936 bool by_ref = (TYPE_REF_P (ftype)
18937 || (TREE_CODE (ftype) == DECLTYPE_TYPE
18938 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
18939 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
18940 continue;
18941 }
18942
18943 if (PACK_EXPANSION_P (ofield))
18944 ofield = PACK_EXPANSION_PATTERN (ofield);
18945 tree field = tsubst_decl (ofield, args, complain);
18946
18947 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18948 {
18949 /* Remember these for when we've pushed local_specializations. */
18950 vec_safe_push (field_packs, ofield);
18951 vec_safe_push (field_packs, field);
18952 }
18953
18954 if (field == error_mark_node)
18955 return error_mark_node;
18956
18957 if (TREE_CODE (field) == TREE_VEC)
18958 {
18959 int len = TREE_VEC_LENGTH (field);
18960 gcc_assert (TREE_CODE (init) == TREE_VEC
18961 && TREE_VEC_LENGTH (init) == len);
18962 for (int i = 0; i < len; ++i)
18963 prepend_one_capture (TREE_VEC_ELT (field, i),
18964 TREE_VEC_ELT (init, i),
18965 LAMBDA_EXPR_CAPTURE_LIST (r),
18966 complain);
18967 }
18968 else
18969 {
18970 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18971 complain);
18972
18973 if (id_equal (DECL_NAME (field), "__this"))
18974 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18975 }
18976 }
18977
18978 tree type = begin_lambda_type (r);
18979 if (type == error_mark_node)
18980 return error_mark_node;
18981
18982 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18983 determine_visibility (TYPE_NAME (type));
18984
18985 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18986
18987 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18988 ? DECL_TI_TEMPLATE (oldfn)
18989 : NULL_TREE);
18990
18991 tree fntype = static_fn_type (oldfn);
18992 if (oldtmpl)
18993 ++processing_template_decl;
18994 fntype = tsubst (fntype, args, complain, in_decl);
18995 if (oldtmpl)
18996 --processing_template_decl;
18997
18998 if (fntype == error_mark_node)
18999 r = error_mark_node;
19000 else
19001 {
19002 /* The body of a lambda-expression is not a subexpression of the
19003 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19004 which would be skipped if cp_unevaluated_operand. */
19005 cp_evaluated ev;
19006
19007 /* Fix the type of 'this'. */
19008 fntype = build_memfn_type (fntype, type,
19009 type_memfn_quals (fntype),
19010 type_memfn_rqual (fntype));
19011 tree fn, tmpl;
19012 if (oldtmpl)
19013 {
19014 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19015 if (tmpl == error_mark_node)
19016 {
19017 r = error_mark_node;
19018 goto out;
19019 }
19020 fn = DECL_TEMPLATE_RESULT (tmpl);
19021 finish_member_declaration (tmpl);
19022 }
19023 else
19024 {
19025 tmpl = NULL_TREE;
19026 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19027 if (fn == error_mark_node)
19028 {
19029 r = error_mark_node;
19030 goto out;
19031 }
19032 finish_member_declaration (fn);
19033 }
19034
19035 if (tree ci = get_constraints (oldfn))
19036 {
19037 /* Substitute into the lambda's constraints. */
19038 if (oldtmpl)
19039 ++processing_template_decl;
19040 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19041 if (oldtmpl)
19042 --processing_template_decl;
19043 set_constraints (fn, ci);
19044 }
19045
19046 /* Let finish_function set this. */
19047 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19048
19049 bool nested = cfun;
19050 if (nested)
19051 push_function_context ();
19052 else
19053 /* Still increment function_depth so that we don't GC in the
19054 middle of an expression. */
19055 ++function_depth;
19056
19057 local_specialization_stack s (lss_copy);
19058
19059 tree body = start_lambda_function (fn, r);
19060
19061 /* Now record them for lookup_init_capture_pack. */
19062 int fplen = vec_safe_length (field_packs);
19063 for (int i = 0; i < fplen; )
19064 {
19065 tree pack = (*field_packs)[i++];
19066 tree inst = (*field_packs)[i++];
19067 register_local_specialization (inst, pack);
19068 }
19069 release_tree_vector (field_packs);
19070
19071 register_parameter_specializations (oldfn, fn);
19072
19073 if (oldtmpl)
19074 {
19075 /* We might not partially instantiate some parts of the function, so
19076 copy these flags from the original template. */
19077 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19078 current_function_returns_value = ol->returns_value;
19079 current_function_returns_null = ol->returns_null;
19080 current_function_returns_abnormally = ol->returns_abnormally;
19081 current_function_infinite_loop = ol->infinite_loop;
19082 }
19083
19084 /* [temp.deduct] A lambda-expression appearing in a function type or a
19085 template parameter is not considered part of the immediate context for
19086 the purposes of template argument deduction. */
19087 complain = tf_warning_or_error;
19088
19089 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19090 /*constexpr*/false);
19091
19092 finish_lambda_function (body);
19093
19094 if (nested)
19095 pop_function_context ();
19096 else
19097 --function_depth;
19098
19099 /* The capture list was built up in reverse order; fix that now. */
19100 LAMBDA_EXPR_CAPTURE_LIST (r)
19101 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19102
19103 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19104
19105 maybe_add_lambda_conv_op (type);
19106 }
19107
19108 out:
19109 finish_struct (type, /*attr*/NULL_TREE);
19110
19111 insert_pending_capture_proxies ();
19112
19113 return r;
19114 }
19115
19116 /* Like tsubst but deals with expressions and performs semantic
19117 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19118
19119 tree
19120 tsubst_copy_and_build (tree t,
19121 tree args,
19122 tsubst_flags_t complain,
19123 tree in_decl,
19124 bool function_p,
19125 bool integral_constant_expression_p)
19126 {
19127 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19128 #define RECUR(NODE) \
19129 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19130 /*function_p=*/false, \
19131 integral_constant_expression_p)
19132
19133 tree retval, op1;
19134 location_t save_loc;
19135
19136 if (t == NULL_TREE || t == error_mark_node)
19137 return t;
19138
19139 save_loc = input_location;
19140 if (location_t eloc = cp_expr_location (t))
19141 input_location = eloc;
19142
19143 /* N3276 decltype magic only applies to calls at the top level or on the
19144 right side of a comma. */
19145 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19146 complain &= ~tf_decltype;
19147
19148 switch (TREE_CODE (t))
19149 {
19150 case USING_DECL:
19151 t = DECL_NAME (t);
19152 /* Fall through. */
19153 case IDENTIFIER_NODE:
19154 {
19155 tree decl;
19156 cp_id_kind idk;
19157 bool non_integral_constant_expression_p;
19158 const char *error_msg;
19159
19160 if (IDENTIFIER_CONV_OP_P (t))
19161 {
19162 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19163 t = make_conv_op_name (new_type);
19164 }
19165
19166 /* Look up the name. */
19167 decl = lookup_name (t);
19168
19169 /* By convention, expressions use ERROR_MARK_NODE to indicate
19170 failure, not NULL_TREE. */
19171 if (decl == NULL_TREE)
19172 decl = error_mark_node;
19173
19174 decl = finish_id_expression (t, decl, NULL_TREE,
19175 &idk,
19176 integral_constant_expression_p,
19177 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19178 &non_integral_constant_expression_p,
19179 /*template_p=*/false,
19180 /*done=*/true,
19181 /*address_p=*/false,
19182 /*template_arg_p=*/false,
19183 &error_msg,
19184 input_location);
19185 if (error_msg)
19186 error (error_msg);
19187 if (!function_p && identifier_p (decl))
19188 {
19189 if (complain & tf_error)
19190 unqualified_name_lookup_error (decl);
19191 decl = error_mark_node;
19192 }
19193 RETURN (decl);
19194 }
19195
19196 case TEMPLATE_ID_EXPR:
19197 {
19198 tree object;
19199 tree templ = RECUR (TREE_OPERAND (t, 0));
19200 tree targs = TREE_OPERAND (t, 1);
19201
19202 if (targs)
19203 targs = tsubst_template_args (targs, args, complain, in_decl);
19204 if (targs == error_mark_node)
19205 RETURN (error_mark_node);
19206
19207 if (TREE_CODE (templ) == SCOPE_REF)
19208 {
19209 tree name = TREE_OPERAND (templ, 1);
19210 tree tid = lookup_template_function (name, targs);
19211 TREE_OPERAND (templ, 1) = tid;
19212 RETURN (templ);
19213 }
19214
19215 if (concept_definition_p (templ))
19216 {
19217 tree check = build_concept_check (templ, targs, complain);
19218 if (check == error_mark_node)
19219 RETURN (error_mark_node);
19220
19221 tree id = unpack_concept_check (check);
19222
19223 /* If we built a function concept check, return the underlying
19224 template-id. So we can evaluate it as a function call. */
19225 if (function_concept_p (TREE_OPERAND (id, 0)))
19226 RETURN (id);
19227
19228 RETURN (check);
19229 }
19230
19231 if (variable_template_p (templ))
19232 {
19233 tree r = lookup_and_finish_template_variable (templ, targs,
19234 complain);
19235 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19236 RETURN (r);
19237 }
19238
19239 if (TREE_CODE (templ) == COMPONENT_REF)
19240 {
19241 object = TREE_OPERAND (templ, 0);
19242 templ = TREE_OPERAND (templ, 1);
19243 }
19244 else
19245 object = NULL_TREE;
19246 templ = lookup_template_function (templ, targs);
19247
19248 if (object)
19249 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19250 object, templ, NULL_TREE));
19251 else
19252 RETURN (baselink_for_fns (templ));
19253 }
19254
19255 case INDIRECT_REF:
19256 {
19257 tree r = RECUR (TREE_OPERAND (t, 0));
19258
19259 if (REFERENCE_REF_P (t))
19260 {
19261 /* A type conversion to reference type will be enclosed in
19262 such an indirect ref, but the substitution of the cast
19263 will have also added such an indirect ref. */
19264 r = convert_from_reference (r);
19265 }
19266 else
19267 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19268 complain|decltype_flag);
19269
19270 if (REF_PARENTHESIZED_P (t))
19271 r = force_paren_expr (r);
19272
19273 RETURN (r);
19274 }
19275
19276 case NOP_EXPR:
19277 {
19278 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19279 tree op0 = RECUR (TREE_OPERAND (t, 0));
19280 RETURN (build_nop (type, op0));
19281 }
19282
19283 case IMPLICIT_CONV_EXPR:
19284 {
19285 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19286 tree expr = RECUR (TREE_OPERAND (t, 0));
19287 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19288 {
19289 retval = copy_node (t);
19290 TREE_TYPE (retval) = type;
19291 TREE_OPERAND (retval, 0) = expr;
19292 RETURN (retval);
19293 }
19294 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19295 /* We'll pass this to convert_nontype_argument again, we don't need
19296 to actually perform any conversion here. */
19297 RETURN (expr);
19298 int flags = LOOKUP_IMPLICIT;
19299 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19300 flags = LOOKUP_NORMAL;
19301 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19302 flags |= LOOKUP_NO_NARROWING;
19303 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19304 flags));
19305 }
19306
19307 case CONVERT_EXPR:
19308 {
19309 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19310 tree op0 = RECUR (TREE_OPERAND (t, 0));
19311 if (op0 == error_mark_node)
19312 RETURN (error_mark_node);
19313 RETURN (build1 (CONVERT_EXPR, type, op0));
19314 }
19315
19316 case CAST_EXPR:
19317 case REINTERPRET_CAST_EXPR:
19318 case CONST_CAST_EXPR:
19319 case DYNAMIC_CAST_EXPR:
19320 case STATIC_CAST_EXPR:
19321 {
19322 tree type;
19323 tree op, r = NULL_TREE;
19324
19325 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19326 if (integral_constant_expression_p
19327 && !cast_valid_in_integral_constant_expression_p (type))
19328 {
19329 if (complain & tf_error)
19330 error ("a cast to a type other than an integral or "
19331 "enumeration type cannot appear in a constant-expression");
19332 RETURN (error_mark_node);
19333 }
19334
19335 op = RECUR (TREE_OPERAND (t, 0));
19336
19337 warning_sentinel s(warn_useless_cast);
19338 warning_sentinel s2(warn_ignored_qualifiers);
19339 switch (TREE_CODE (t))
19340 {
19341 case CAST_EXPR:
19342 r = build_functional_cast (input_location, type, op, complain);
19343 break;
19344 case REINTERPRET_CAST_EXPR:
19345 r = build_reinterpret_cast (input_location, type, op, complain);
19346 break;
19347 case CONST_CAST_EXPR:
19348 r = build_const_cast (input_location, type, op, complain);
19349 break;
19350 case DYNAMIC_CAST_EXPR:
19351 r = build_dynamic_cast (input_location, type, op, complain);
19352 break;
19353 case STATIC_CAST_EXPR:
19354 r = build_static_cast (input_location, type, op, complain);
19355 break;
19356 default:
19357 gcc_unreachable ();
19358 }
19359
19360 RETURN (r);
19361 }
19362
19363 case POSTDECREMENT_EXPR:
19364 case POSTINCREMENT_EXPR:
19365 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19366 args, complain, in_decl);
19367 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19368 complain|decltype_flag));
19369
19370 case PREDECREMENT_EXPR:
19371 case PREINCREMENT_EXPR:
19372 case NEGATE_EXPR:
19373 case BIT_NOT_EXPR:
19374 case ABS_EXPR:
19375 case TRUTH_NOT_EXPR:
19376 case UNARY_PLUS_EXPR: /* Unary + */
19377 case REALPART_EXPR:
19378 case IMAGPART_EXPR:
19379 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19380 RECUR (TREE_OPERAND (t, 0)),
19381 complain|decltype_flag));
19382
19383 case FIX_TRUNC_EXPR:
19384 gcc_unreachable ();
19385
19386 case ADDR_EXPR:
19387 op1 = TREE_OPERAND (t, 0);
19388 if (TREE_CODE (op1) == LABEL_DECL)
19389 RETURN (finish_label_address_expr (DECL_NAME (op1),
19390 EXPR_LOCATION (op1)));
19391 if (TREE_CODE (op1) == SCOPE_REF)
19392 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19393 /*done=*/true, /*address_p=*/true);
19394 else
19395 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19396 in_decl);
19397 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19398 complain|decltype_flag));
19399
19400 case PLUS_EXPR:
19401 case MINUS_EXPR:
19402 case MULT_EXPR:
19403 case TRUNC_DIV_EXPR:
19404 case CEIL_DIV_EXPR:
19405 case FLOOR_DIV_EXPR:
19406 case ROUND_DIV_EXPR:
19407 case EXACT_DIV_EXPR:
19408 case BIT_AND_EXPR:
19409 case BIT_IOR_EXPR:
19410 case BIT_XOR_EXPR:
19411 case TRUNC_MOD_EXPR:
19412 case FLOOR_MOD_EXPR:
19413 case TRUTH_ANDIF_EXPR:
19414 case TRUTH_ORIF_EXPR:
19415 case TRUTH_AND_EXPR:
19416 case TRUTH_OR_EXPR:
19417 case RSHIFT_EXPR:
19418 case LSHIFT_EXPR:
19419 case EQ_EXPR:
19420 case NE_EXPR:
19421 case MAX_EXPR:
19422 case MIN_EXPR:
19423 case LE_EXPR:
19424 case GE_EXPR:
19425 case LT_EXPR:
19426 case GT_EXPR:
19427 case SPACESHIP_EXPR:
19428 case MEMBER_REF:
19429 case DOTSTAR_EXPR:
19430 {
19431 /* If T was type-dependent, suppress warnings that depend on the range
19432 of the types involved. */
19433 bool was_dep = type_dependent_expression_p_push (t);
19434
19435 tree op0 = RECUR (TREE_OPERAND (t, 0));
19436 tree op1 = RECUR (TREE_OPERAND (t, 1));
19437
19438 warning_sentinel s1(warn_type_limits, was_dep);
19439 warning_sentinel s2(warn_div_by_zero, was_dep);
19440 warning_sentinel s3(warn_logical_op, was_dep);
19441 warning_sentinel s4(warn_tautological_compare, was_dep);
19442
19443 tree r = build_x_binary_op
19444 (input_location, TREE_CODE (t),
19445 op0,
19446 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19447 ? ERROR_MARK
19448 : TREE_CODE (TREE_OPERAND (t, 0))),
19449 op1,
19450 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19451 ? ERROR_MARK
19452 : TREE_CODE (TREE_OPERAND (t, 1))),
19453 /*overload=*/NULL,
19454 complain|decltype_flag);
19455 if (EXPR_P (r) && TREE_NO_WARNING (t))
19456 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19457
19458 RETURN (r);
19459 }
19460
19461 case POINTER_PLUS_EXPR:
19462 {
19463 tree op0 = RECUR (TREE_OPERAND (t, 0));
19464 if (op0 == error_mark_node)
19465 RETURN (error_mark_node);
19466 tree op1 = RECUR (TREE_OPERAND (t, 1));
19467 if (op1 == error_mark_node)
19468 RETURN (error_mark_node);
19469 RETURN (fold_build_pointer_plus (op0, op1));
19470 }
19471
19472 case SCOPE_REF:
19473 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19474 /*address_p=*/false));
19475 case ARRAY_REF:
19476 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19477 args, complain, in_decl);
19478 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19479 RECUR (TREE_OPERAND (t, 1)),
19480 complain|decltype_flag));
19481
19482 case SIZEOF_EXPR:
19483 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19484 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19485 RETURN (tsubst_copy (t, args, complain, in_decl));
19486 /* Fall through */
19487
19488 case ALIGNOF_EXPR:
19489 {
19490 tree r;
19491
19492 op1 = TREE_OPERAND (t, 0);
19493 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19494 op1 = TREE_TYPE (op1);
19495 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19496 && ALIGNOF_EXPR_STD_P (t));
19497 if (!args)
19498 {
19499 /* When there are no ARGS, we are trying to evaluate a
19500 non-dependent expression from the parser. Trying to do
19501 the substitutions may not work. */
19502 if (!TYPE_P (op1))
19503 op1 = TREE_TYPE (op1);
19504 }
19505 else
19506 {
19507 ++cp_unevaluated_operand;
19508 ++c_inhibit_evaluation_warnings;
19509 if (TYPE_P (op1))
19510 op1 = tsubst (op1, args, complain, in_decl);
19511 else
19512 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19513 /*function_p=*/false,
19514 /*integral_constant_expression_p=*/
19515 false);
19516 --cp_unevaluated_operand;
19517 --c_inhibit_evaluation_warnings;
19518 }
19519 if (TYPE_P (op1))
19520 r = cxx_sizeof_or_alignof_type (input_location,
19521 op1, TREE_CODE (t), std_alignof,
19522 complain & tf_error);
19523 else
19524 r = cxx_sizeof_or_alignof_expr (input_location,
19525 op1, TREE_CODE (t),
19526 complain & tf_error);
19527 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19528 {
19529 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19530 {
19531 if (!processing_template_decl && TYPE_P (op1))
19532 {
19533 r = build_min (SIZEOF_EXPR, size_type_node,
19534 build1 (NOP_EXPR, op1, error_mark_node));
19535 SIZEOF_EXPR_TYPE_P (r) = 1;
19536 }
19537 else
19538 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19539 TREE_SIDE_EFFECTS (r) = 0;
19540 TREE_READONLY (r) = 1;
19541 }
19542 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19543 }
19544 RETURN (r);
19545 }
19546
19547 case AT_ENCODE_EXPR:
19548 {
19549 op1 = TREE_OPERAND (t, 0);
19550 ++cp_unevaluated_operand;
19551 ++c_inhibit_evaluation_warnings;
19552 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19553 /*function_p=*/false,
19554 /*integral_constant_expression_p=*/false);
19555 --cp_unevaluated_operand;
19556 --c_inhibit_evaluation_warnings;
19557 RETURN (objc_build_encode_expr (op1));
19558 }
19559
19560 case NOEXCEPT_EXPR:
19561 op1 = TREE_OPERAND (t, 0);
19562 ++cp_unevaluated_operand;
19563 ++c_inhibit_evaluation_warnings;
19564 ++cp_noexcept_operand;
19565 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19566 /*function_p=*/false,
19567 /*integral_constant_expression_p=*/false);
19568 --cp_unevaluated_operand;
19569 --c_inhibit_evaluation_warnings;
19570 --cp_noexcept_operand;
19571 RETURN (finish_noexcept_expr (op1, complain));
19572
19573 case MODOP_EXPR:
19574 {
19575 warning_sentinel s(warn_div_by_zero);
19576 tree lhs = RECUR (TREE_OPERAND (t, 0));
19577 tree rhs = RECUR (TREE_OPERAND (t, 2));
19578 tree r = build_x_modify_expr
19579 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19580 complain|decltype_flag);
19581 /* TREE_NO_WARNING must be set if either the expression was
19582 parenthesized or it uses an operator such as >>= rather
19583 than plain assignment. In the former case, it was already
19584 set and must be copied. In the latter case,
19585 build_x_modify_expr sets it and it must not be reset
19586 here. */
19587 if (TREE_NO_WARNING (t))
19588 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19589
19590 RETURN (r);
19591 }
19592
19593 case ARROW_EXPR:
19594 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19595 args, complain, in_decl);
19596 /* Remember that there was a reference to this entity. */
19597 if (DECL_P (op1)
19598 && !mark_used (op1, complain) && !(complain & tf_error))
19599 RETURN (error_mark_node);
19600 RETURN (build_x_arrow (input_location, op1, complain));
19601
19602 case NEW_EXPR:
19603 {
19604 tree placement = RECUR (TREE_OPERAND (t, 0));
19605 tree init = RECUR (TREE_OPERAND (t, 3));
19606 vec<tree, va_gc> *placement_vec;
19607 vec<tree, va_gc> *init_vec;
19608 tree ret;
19609 location_t loc = EXPR_LOCATION (t);
19610
19611 if (placement == NULL_TREE)
19612 placement_vec = NULL;
19613 else
19614 {
19615 placement_vec = make_tree_vector ();
19616 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19617 vec_safe_push (placement_vec, TREE_VALUE (placement));
19618 }
19619
19620 /* If there was an initializer in the original tree, but it
19621 instantiated to an empty list, then we should pass a
19622 non-NULL empty vector to tell build_new that it was an
19623 empty initializer() rather than no initializer. This can
19624 only happen when the initializer is a pack expansion whose
19625 parameter packs are of length zero. */
19626 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19627 init_vec = NULL;
19628 else
19629 {
19630 init_vec = make_tree_vector ();
19631 if (init == void_node)
19632 gcc_assert (init_vec != NULL);
19633 else
19634 {
19635 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19636 vec_safe_push (init_vec, TREE_VALUE (init));
19637 }
19638 }
19639
19640 /* Avoid passing an enclosing decl to valid_array_size_p. */
19641 in_decl = NULL_TREE;
19642
19643 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19644 tree op2 = RECUR (TREE_OPERAND (t, 2));
19645 ret = build_new (loc, &placement_vec, op1, op2,
19646 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19647 complain);
19648
19649 if (placement_vec != NULL)
19650 release_tree_vector (placement_vec);
19651 if (init_vec != NULL)
19652 release_tree_vector (init_vec);
19653
19654 RETURN (ret);
19655 }
19656
19657 case DELETE_EXPR:
19658 {
19659 tree op0 = RECUR (TREE_OPERAND (t, 0));
19660 tree op1 = RECUR (TREE_OPERAND (t, 1));
19661 RETURN (delete_sanity (input_location, op0, op1,
19662 DELETE_EXPR_USE_VEC (t),
19663 DELETE_EXPR_USE_GLOBAL (t),
19664 complain));
19665 }
19666
19667 case COMPOUND_EXPR:
19668 {
19669 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19670 complain & ~tf_decltype, in_decl,
19671 /*function_p=*/false,
19672 integral_constant_expression_p);
19673 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19674 op0,
19675 RECUR (TREE_OPERAND (t, 1)),
19676 complain|decltype_flag));
19677 }
19678
19679 case CALL_EXPR:
19680 {
19681 tree function;
19682 unsigned int nargs, i;
19683 bool qualified_p;
19684 bool koenig_p;
19685 tree ret;
19686
19687 function = CALL_EXPR_FN (t);
19688 /* Internal function with no arguments. */
19689 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19690 RETURN (t);
19691
19692 /* When we parsed the expression, we determined whether or
19693 not Koenig lookup should be performed. */
19694 koenig_p = KOENIG_LOOKUP_P (t);
19695 if (function == NULL_TREE)
19696 {
19697 koenig_p = false;
19698 qualified_p = false;
19699 }
19700 else if (TREE_CODE (function) == SCOPE_REF)
19701 {
19702 qualified_p = true;
19703 function = tsubst_qualified_id (function, args, complain, in_decl,
19704 /*done=*/false,
19705 /*address_p=*/false);
19706 }
19707 else if (koenig_p && identifier_p (function))
19708 {
19709 /* Do nothing; calling tsubst_copy_and_build on an identifier
19710 would incorrectly perform unqualified lookup again.
19711
19712 Note that we can also have an IDENTIFIER_NODE if the earlier
19713 unqualified lookup found a member function; in that case
19714 koenig_p will be false and we do want to do the lookup
19715 again to find the instantiated member function.
19716
19717 FIXME but doing that causes c++/15272, so we need to stop
19718 using IDENTIFIER_NODE in that situation. */
19719 qualified_p = false;
19720 }
19721 else
19722 {
19723 if (TREE_CODE (function) == COMPONENT_REF)
19724 {
19725 tree op = TREE_OPERAND (function, 1);
19726
19727 qualified_p = (TREE_CODE (op) == SCOPE_REF
19728 || (BASELINK_P (op)
19729 && BASELINK_QUALIFIED_P (op)));
19730 }
19731 else
19732 qualified_p = false;
19733
19734 if (TREE_CODE (function) == ADDR_EXPR
19735 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19736 /* Avoid error about taking the address of a constructor. */
19737 function = TREE_OPERAND (function, 0);
19738
19739 function = tsubst_copy_and_build (function, args, complain,
19740 in_decl,
19741 !qualified_p,
19742 integral_constant_expression_p);
19743
19744 if (BASELINK_P (function))
19745 qualified_p = true;
19746 }
19747
19748 nargs = call_expr_nargs (t);
19749 releasing_vec call_args;
19750 for (i = 0; i < nargs; ++i)
19751 {
19752 tree arg = CALL_EXPR_ARG (t, i);
19753
19754 if (!PACK_EXPANSION_P (arg))
19755 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19756 else
19757 {
19758 /* Expand the pack expansion and push each entry onto
19759 CALL_ARGS. */
19760 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19761 if (TREE_CODE (arg) == TREE_VEC)
19762 {
19763 unsigned int len, j;
19764
19765 len = TREE_VEC_LENGTH (arg);
19766 for (j = 0; j < len; ++j)
19767 {
19768 tree value = TREE_VEC_ELT (arg, j);
19769 if (value != NULL_TREE)
19770 value = convert_from_reference (value);
19771 vec_safe_push (call_args, value);
19772 }
19773 }
19774 else
19775 {
19776 /* A partial substitution. Add one entry. */
19777 vec_safe_push (call_args, arg);
19778 }
19779 }
19780 }
19781
19782 /* Stripped-down processing for a call in a thunk. Specifically, in
19783 the thunk template for a generic lambda. */
19784 if (CALL_FROM_THUNK_P (t))
19785 {
19786 /* Now that we've expanded any packs, the number of call args
19787 might be different. */
19788 unsigned int cargs = call_args->length ();
19789 tree thisarg = NULL_TREE;
19790 if (TREE_CODE (function) == COMPONENT_REF)
19791 {
19792 thisarg = TREE_OPERAND (function, 0);
19793 if (TREE_CODE (thisarg) == INDIRECT_REF)
19794 thisarg = TREE_OPERAND (thisarg, 0);
19795 function = TREE_OPERAND (function, 1);
19796 if (TREE_CODE (function) == BASELINK)
19797 function = BASELINK_FUNCTIONS (function);
19798 }
19799 /* We aren't going to do normal overload resolution, so force the
19800 template-id to resolve. */
19801 function = resolve_nondeduced_context (function, complain);
19802 for (unsigned i = 0; i < cargs; ++i)
19803 {
19804 /* In a thunk, pass through args directly, without any
19805 conversions. */
19806 tree arg = (*call_args)[i];
19807 while (TREE_CODE (arg) != PARM_DECL)
19808 arg = TREE_OPERAND (arg, 0);
19809 (*call_args)[i] = arg;
19810 }
19811 if (thisarg)
19812 {
19813 /* If there are no other args, just push 'this'. */
19814 if (cargs == 0)
19815 vec_safe_push (call_args, thisarg);
19816 else
19817 {
19818 /* Otherwise, shift the other args over to make room. */
19819 tree last = (*call_args)[cargs - 1];
19820 vec_safe_push (call_args, last);
19821 for (int i = cargs - 1; i > 0; --i)
19822 (*call_args)[i] = (*call_args)[i - 1];
19823 (*call_args)[0] = thisarg;
19824 }
19825 }
19826 ret = build_call_a (function, call_args->length (),
19827 call_args->address ());
19828 /* The thunk location is not interesting. */
19829 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19830 CALL_FROM_THUNK_P (ret) = true;
19831 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19832 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19833
19834 RETURN (ret);
19835 }
19836
19837 /* We do not perform argument-dependent lookup if normal
19838 lookup finds a non-function, in accordance with the
19839 expected resolution of DR 218. */
19840 if (koenig_p
19841 && ((is_overloaded_fn (function)
19842 /* If lookup found a member function, the Koenig lookup is
19843 not appropriate, even if an unqualified-name was used
19844 to denote the function. */
19845 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19846 || identifier_p (function))
19847 /* Only do this when substitution turns a dependent call
19848 into a non-dependent call. */
19849 && type_dependent_expression_p_push (t)
19850 && !any_type_dependent_arguments_p (call_args))
19851 function = perform_koenig_lookup (function, call_args, tf_none);
19852
19853 if (function != NULL_TREE
19854 && identifier_p (function)
19855 && !any_type_dependent_arguments_p (call_args))
19856 {
19857 if (koenig_p && (complain & tf_warning_or_error))
19858 {
19859 /* For backwards compatibility and good diagnostics, try
19860 the unqualified lookup again if we aren't in SFINAE
19861 context. */
19862 tree unq = (tsubst_copy_and_build
19863 (function, args, complain, in_decl, true,
19864 integral_constant_expression_p));
19865 if (unq == error_mark_node)
19866 RETURN (error_mark_node);
19867
19868 if (unq != function)
19869 {
19870 /* In a lambda fn, we have to be careful to not
19871 introduce new this captures. Legacy code can't
19872 be using lambdas anyway, so it's ok to be
19873 stricter. */
19874 bool in_lambda = (current_class_type
19875 && LAMBDA_TYPE_P (current_class_type));
19876 char const *const msg
19877 = G_("%qD was not declared in this scope, "
19878 "and no declarations were found by "
19879 "argument-dependent lookup at the point "
19880 "of instantiation");
19881
19882 bool diag = true;
19883 if (in_lambda)
19884 error_at (cp_expr_loc_or_input_loc (t),
19885 msg, function);
19886 else
19887 diag = permerror (cp_expr_loc_or_input_loc (t),
19888 msg, function);
19889 if (diag)
19890 {
19891 tree fn = unq;
19892
19893 if (INDIRECT_REF_P (fn))
19894 fn = TREE_OPERAND (fn, 0);
19895 if (is_overloaded_fn (fn))
19896 fn = get_first_fn (fn);
19897
19898 if (!DECL_P (fn))
19899 /* Can't say anything more. */;
19900 else if (DECL_CLASS_SCOPE_P (fn))
19901 {
19902 location_t loc = cp_expr_loc_or_input_loc (t);
19903 inform (loc,
19904 "declarations in dependent base %qT are "
19905 "not found by unqualified lookup",
19906 DECL_CLASS_CONTEXT (fn));
19907 if (current_class_ptr)
19908 inform (loc,
19909 "use %<this->%D%> instead", function);
19910 else
19911 inform (loc,
19912 "use %<%T::%D%> instead",
19913 current_class_name, function);
19914 }
19915 else
19916 inform (DECL_SOURCE_LOCATION (fn),
19917 "%qD declared here, later in the "
19918 "translation unit", fn);
19919 if (in_lambda)
19920 RETURN (error_mark_node);
19921 }
19922
19923 function = unq;
19924 }
19925 }
19926 if (identifier_p (function))
19927 {
19928 if (complain & tf_error)
19929 unqualified_name_lookup_error (function);
19930 RETURN (error_mark_node);
19931 }
19932 }
19933
19934 /* Remember that there was a reference to this entity. */
19935 if (function != NULL_TREE
19936 && DECL_P (function)
19937 && !mark_used (function, complain) && !(complain & tf_error))
19938 RETURN (error_mark_node);
19939
19940 /* Put back tf_decltype for the actual call. */
19941 complain |= decltype_flag;
19942
19943 if (function == NULL_TREE)
19944 switch (CALL_EXPR_IFN (t))
19945 {
19946 case IFN_LAUNDER:
19947 gcc_assert (nargs == 1);
19948 if (vec_safe_length (call_args) != 1)
19949 {
19950 error_at (cp_expr_loc_or_input_loc (t),
19951 "wrong number of arguments to "
19952 "%<__builtin_launder%>");
19953 ret = error_mark_node;
19954 }
19955 else
19956 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19957 (*call_args)[0], complain);
19958 break;
19959
19960 case IFN_VEC_CONVERT:
19961 gcc_assert (nargs == 1);
19962 if (vec_safe_length (call_args) != 1)
19963 {
19964 error_at (cp_expr_loc_or_input_loc (t),
19965 "wrong number of arguments to "
19966 "%<__builtin_convertvector%>");
19967 ret = error_mark_node;
19968 break;
19969 }
19970 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19971 tsubst (TREE_TYPE (t), args,
19972 complain, in_decl),
19973 complain);
19974 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19975 RETURN (ret);
19976 break;
19977
19978 default:
19979 /* Unsupported internal function with arguments. */
19980 gcc_unreachable ();
19981 }
19982 else if (TREE_CODE (function) == OFFSET_REF
19983 || TREE_CODE (function) == DOTSTAR_EXPR
19984 || TREE_CODE (function) == MEMBER_REF)
19985 ret = build_offset_ref_call_from_tree (function, &call_args,
19986 complain);
19987 else if (TREE_CODE (function) == COMPONENT_REF)
19988 {
19989 tree instance = TREE_OPERAND (function, 0);
19990 tree fn = TREE_OPERAND (function, 1);
19991
19992 if (processing_template_decl
19993 && (type_dependent_expression_p (instance)
19994 || (!BASELINK_P (fn)
19995 && TREE_CODE (fn) != FIELD_DECL)
19996 || type_dependent_expression_p (fn)
19997 || any_type_dependent_arguments_p (call_args)))
19998 ret = build_min_nt_call_vec (function, call_args);
19999 else if (!BASELINK_P (fn))
20000 ret = finish_call_expr (function, &call_args,
20001 /*disallow_virtual=*/false,
20002 /*koenig_p=*/false,
20003 complain);
20004 else
20005 ret = (build_new_method_call
20006 (instance, fn,
20007 &call_args, NULL_TREE,
20008 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20009 /*fn_p=*/NULL,
20010 complain));
20011 }
20012 else if (concept_check_p (function))
20013 {
20014 /* FUNCTION is a template-id referring to a concept definition. */
20015 tree id = unpack_concept_check (function);
20016 tree tmpl = TREE_OPERAND (id, 0);
20017 tree args = TREE_OPERAND (id, 1);
20018
20019 /* Calls to standard and variable concepts should have been
20020 previously diagnosed. */
20021 gcc_assert (function_concept_p (tmpl));
20022
20023 /* Ensure the result is wrapped as a call expression. */
20024 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20025 }
20026 else
20027 ret = finish_call_expr (function, &call_args,
20028 /*disallow_virtual=*/qualified_p,
20029 koenig_p,
20030 complain);
20031
20032 if (ret != error_mark_node)
20033 {
20034 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20035 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20036 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20037 if (op || ord || rev)
20038 {
20039 function = extract_call_expr (ret);
20040 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20041 CALL_EXPR_ORDERED_ARGS (function) = ord;
20042 CALL_EXPR_REVERSE_ARGS (function) = rev;
20043 }
20044 }
20045
20046 RETURN (ret);
20047 }
20048
20049 case COND_EXPR:
20050 {
20051 tree cond = RECUR (TREE_OPERAND (t, 0));
20052 cond = mark_rvalue_use (cond);
20053 tree folded_cond = fold_non_dependent_expr (cond, complain);
20054 tree exp1, exp2;
20055
20056 if (TREE_CODE (folded_cond) == INTEGER_CST)
20057 {
20058 if (integer_zerop (folded_cond))
20059 {
20060 ++c_inhibit_evaluation_warnings;
20061 exp1 = RECUR (TREE_OPERAND (t, 1));
20062 --c_inhibit_evaluation_warnings;
20063 exp2 = RECUR (TREE_OPERAND (t, 2));
20064 }
20065 else
20066 {
20067 exp1 = RECUR (TREE_OPERAND (t, 1));
20068 ++c_inhibit_evaluation_warnings;
20069 exp2 = RECUR (TREE_OPERAND (t, 2));
20070 --c_inhibit_evaluation_warnings;
20071 }
20072 cond = folded_cond;
20073 }
20074 else
20075 {
20076 exp1 = RECUR (TREE_OPERAND (t, 1));
20077 exp2 = RECUR (TREE_OPERAND (t, 2));
20078 }
20079
20080 warning_sentinel s(warn_duplicated_branches);
20081 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20082 cond, exp1, exp2, complain));
20083 }
20084
20085 case PSEUDO_DTOR_EXPR:
20086 {
20087 tree op0 = RECUR (TREE_OPERAND (t, 0));
20088 tree op1 = RECUR (TREE_OPERAND (t, 1));
20089 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20090 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20091 input_location));
20092 }
20093
20094 case TREE_LIST:
20095 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20096
20097 case COMPONENT_REF:
20098 {
20099 tree object;
20100 tree object_type;
20101 tree member;
20102 tree r;
20103
20104 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20105 args, complain, in_decl);
20106 /* Remember that there was a reference to this entity. */
20107 if (DECL_P (object)
20108 && !mark_used (object, complain) && !(complain & tf_error))
20109 RETURN (error_mark_node);
20110 object_type = TREE_TYPE (object);
20111
20112 member = TREE_OPERAND (t, 1);
20113 if (BASELINK_P (member))
20114 member = tsubst_baselink (member,
20115 non_reference (TREE_TYPE (object)),
20116 args, complain, in_decl);
20117 else
20118 member = tsubst_copy (member, args, complain, in_decl);
20119 if (member == error_mark_node)
20120 RETURN (error_mark_node);
20121
20122 if (TREE_CODE (member) == FIELD_DECL)
20123 {
20124 r = finish_non_static_data_member (member, object, NULL_TREE);
20125 if (TREE_CODE (r) == COMPONENT_REF)
20126 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20127 RETURN (r);
20128 }
20129 else if (type_dependent_expression_p (object))
20130 /* We can't do much here. */;
20131 else if (!CLASS_TYPE_P (object_type))
20132 {
20133 if (scalarish_type_p (object_type))
20134 {
20135 tree s = NULL_TREE;
20136 tree dtor = member;
20137
20138 if (TREE_CODE (dtor) == SCOPE_REF)
20139 {
20140 s = TREE_OPERAND (dtor, 0);
20141 dtor = TREE_OPERAND (dtor, 1);
20142 }
20143 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20144 {
20145 dtor = TREE_OPERAND (dtor, 0);
20146 if (TYPE_P (dtor))
20147 RETURN (finish_pseudo_destructor_expr
20148 (object, s, dtor, input_location));
20149 }
20150 }
20151 }
20152 else if (TREE_CODE (member) == SCOPE_REF
20153 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20154 {
20155 /* Lookup the template functions now that we know what the
20156 scope is. */
20157 tree scope = TREE_OPERAND (member, 0);
20158 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20159 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20160 member = lookup_qualified_name (scope, tmpl,
20161 /*is_type_p=*/false,
20162 /*complain=*/false);
20163 if (BASELINK_P (member))
20164 {
20165 BASELINK_FUNCTIONS (member)
20166 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20167 args);
20168 member = (adjust_result_of_qualified_name_lookup
20169 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20170 object_type));
20171 }
20172 else
20173 {
20174 qualified_name_lookup_error (scope, tmpl, member,
20175 input_location);
20176 RETURN (error_mark_node);
20177 }
20178 }
20179 else if (TREE_CODE (member) == SCOPE_REF
20180 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20181 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20182 {
20183 if (complain & tf_error)
20184 {
20185 if (TYPE_P (TREE_OPERAND (member, 0)))
20186 error ("%qT is not a class or namespace",
20187 TREE_OPERAND (member, 0));
20188 else
20189 error ("%qD is not a class or namespace",
20190 TREE_OPERAND (member, 0));
20191 }
20192 RETURN (error_mark_node);
20193 }
20194
20195 r = finish_class_member_access_expr (object, member,
20196 /*template_p=*/false,
20197 complain);
20198 if (TREE_CODE (r) == COMPONENT_REF)
20199 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20200 RETURN (r);
20201 }
20202
20203 case THROW_EXPR:
20204 RETURN (build_throw
20205 (input_location, RECUR (TREE_OPERAND (t, 0))));
20206
20207 case CONSTRUCTOR:
20208 {
20209 vec<constructor_elt, va_gc> *n;
20210 constructor_elt *ce;
20211 unsigned HOST_WIDE_INT idx;
20212 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20213 bool process_index_p;
20214 int newlen;
20215 bool need_copy_p = false;
20216 tree r;
20217
20218 if (type == error_mark_node)
20219 RETURN (error_mark_node);
20220
20221 /* We do not want to process the index of aggregate
20222 initializers as they are identifier nodes which will be
20223 looked up by digest_init. */
20224 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20225
20226 if (null_member_pointer_value_p (t))
20227 {
20228 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20229 RETURN (t);
20230 }
20231
20232 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20233 newlen = vec_safe_length (n);
20234 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20235 {
20236 if (ce->index && process_index_p
20237 /* An identifier index is looked up in the type
20238 being initialized, not the current scope. */
20239 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20240 ce->index = RECUR (ce->index);
20241
20242 if (PACK_EXPANSION_P (ce->value))
20243 {
20244 /* Substitute into the pack expansion. */
20245 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20246 in_decl);
20247
20248 if (ce->value == error_mark_node
20249 || PACK_EXPANSION_P (ce->value))
20250 ;
20251 else if (TREE_VEC_LENGTH (ce->value) == 1)
20252 /* Just move the argument into place. */
20253 ce->value = TREE_VEC_ELT (ce->value, 0);
20254 else
20255 {
20256 /* Update the length of the final CONSTRUCTOR
20257 arguments vector, and note that we will need to
20258 copy.*/
20259 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20260 need_copy_p = true;
20261 }
20262 }
20263 else
20264 ce->value = RECUR (ce->value);
20265 }
20266
20267 if (need_copy_p)
20268 {
20269 vec<constructor_elt, va_gc> *old_n = n;
20270
20271 vec_alloc (n, newlen);
20272 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20273 {
20274 if (TREE_CODE (ce->value) == TREE_VEC)
20275 {
20276 int i, len = TREE_VEC_LENGTH (ce->value);
20277 for (i = 0; i < len; ++i)
20278 CONSTRUCTOR_APPEND_ELT (n, 0,
20279 TREE_VEC_ELT (ce->value, i));
20280 }
20281 else
20282 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20283 }
20284 }
20285
20286 r = build_constructor (init_list_type_node, n);
20287 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20288 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20289 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20290
20291 if (TREE_HAS_CONSTRUCTOR (t))
20292 {
20293 fcl_t cl = fcl_functional;
20294 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20295 cl = fcl_c99;
20296 RETURN (finish_compound_literal (type, r, complain, cl));
20297 }
20298
20299 TREE_TYPE (r) = type;
20300 RETURN (r);
20301 }
20302
20303 case TYPEID_EXPR:
20304 {
20305 tree operand_0 = TREE_OPERAND (t, 0);
20306 if (TYPE_P (operand_0))
20307 {
20308 operand_0 = tsubst (operand_0, args, complain, in_decl);
20309 RETURN (get_typeid (operand_0, complain));
20310 }
20311 else
20312 {
20313 operand_0 = RECUR (operand_0);
20314 RETURN (build_typeid (operand_0, complain));
20315 }
20316 }
20317
20318 case VAR_DECL:
20319 if (!args)
20320 RETURN (t);
20321 /* Fall through */
20322
20323 case PARM_DECL:
20324 {
20325 tree r = tsubst_copy (t, args, complain, in_decl);
20326 /* ??? We're doing a subset of finish_id_expression here. */
20327 if (tree wrap = maybe_get_tls_wrapper_call (r))
20328 /* Replace an evaluated use of the thread_local variable with
20329 a call to its wrapper. */
20330 r = wrap;
20331 else if (outer_automatic_var_p (r))
20332 r = process_outer_var_ref (r, complain);
20333
20334 if (!TYPE_REF_P (TREE_TYPE (t)))
20335 /* If the original type was a reference, we'll be wrapped in
20336 the appropriate INDIRECT_REF. */
20337 r = convert_from_reference (r);
20338 RETURN (r);
20339 }
20340
20341 case VA_ARG_EXPR:
20342 {
20343 tree op0 = RECUR (TREE_OPERAND (t, 0));
20344 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20345 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20346 }
20347
20348 case OFFSETOF_EXPR:
20349 {
20350 tree object_ptr
20351 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20352 in_decl, /*function_p=*/false,
20353 /*integral_constant_expression_p=*/false);
20354 RETURN (finish_offsetof (object_ptr,
20355 RECUR (TREE_OPERAND (t, 0)),
20356 EXPR_LOCATION (t)));
20357 }
20358
20359 case ADDRESSOF_EXPR:
20360 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20361 RECUR (TREE_OPERAND (t, 0)), complain));
20362
20363 case TRAIT_EXPR:
20364 {
20365 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20366 complain, in_decl);
20367 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20368 complain, in_decl);
20369 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20370 TRAIT_EXPR_KIND (t), type1, type2));
20371 }
20372
20373 case STMT_EXPR:
20374 {
20375 tree old_stmt_expr = cur_stmt_expr;
20376 tree stmt_expr = begin_stmt_expr ();
20377
20378 cur_stmt_expr = stmt_expr;
20379 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20380 integral_constant_expression_p);
20381 stmt_expr = finish_stmt_expr (stmt_expr, false);
20382 cur_stmt_expr = old_stmt_expr;
20383
20384 /* If the resulting list of expression statement is empty,
20385 fold it further into void_node. */
20386 if (empty_expr_stmt_p (stmt_expr))
20387 stmt_expr = void_node;
20388
20389 RETURN (stmt_expr);
20390 }
20391
20392 case LAMBDA_EXPR:
20393 {
20394 if (complain & tf_partial)
20395 {
20396 /* We don't have a full set of template arguments yet; don't touch
20397 the lambda at all. */
20398 gcc_assert (processing_template_decl);
20399 return t;
20400 }
20401 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20402
20403 RETURN (build_lambda_object (r));
20404 }
20405
20406 case TARGET_EXPR:
20407 /* We can get here for a constant initializer of non-dependent type.
20408 FIXME stop folding in cp_parser_initializer_clause. */
20409 {
20410 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20411 complain);
20412 RETURN (r);
20413 }
20414
20415 case TRANSACTION_EXPR:
20416 RETURN (tsubst_expr(t, args, complain, in_decl,
20417 integral_constant_expression_p));
20418
20419 case PAREN_EXPR:
20420 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20421
20422 case VEC_PERM_EXPR:
20423 {
20424 tree op0 = RECUR (TREE_OPERAND (t, 0));
20425 tree op1 = RECUR (TREE_OPERAND (t, 1));
20426 tree op2 = RECUR (TREE_OPERAND (t, 2));
20427 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20428 complain));
20429 }
20430
20431 case REQUIRES_EXPR:
20432 {
20433 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20434 RETURN (r);
20435 }
20436
20437 case RANGE_EXPR:
20438 /* No need to substitute further, a RANGE_EXPR will always be built
20439 with constant operands. */
20440 RETURN (t);
20441
20442 case NON_LVALUE_EXPR:
20443 case VIEW_CONVERT_EXPR:
20444 if (location_wrapper_p (t))
20445 /* We need to do this here as well as in tsubst_copy so we get the
20446 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20447 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20448 EXPR_LOCATION (t)));
20449 /* fallthrough. */
20450
20451 default:
20452 /* Handle Objective-C++ constructs, if appropriate. */
20453 {
20454 tree subst
20455 = objcp_tsubst_copy_and_build (t, args, complain,
20456 in_decl, /*function_p=*/false);
20457 if (subst)
20458 RETURN (subst);
20459 }
20460 RETURN (tsubst_copy (t, args, complain, in_decl));
20461 }
20462
20463 #undef RECUR
20464 #undef RETURN
20465 out:
20466 input_location = save_loc;
20467 return retval;
20468 }
20469
20470 /* Verify that the instantiated ARGS are valid. For type arguments,
20471 make sure that the type's linkage is ok. For non-type arguments,
20472 make sure they are constants if they are integral or enumerations.
20473 Emit an error under control of COMPLAIN, and return TRUE on error. */
20474
20475 static bool
20476 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20477 {
20478 if (dependent_template_arg_p (t))
20479 return false;
20480 if (ARGUMENT_PACK_P (t))
20481 {
20482 tree vec = ARGUMENT_PACK_ARGS (t);
20483 int len = TREE_VEC_LENGTH (vec);
20484 bool result = false;
20485 int i;
20486
20487 for (i = 0; i < len; ++i)
20488 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20489 result = true;
20490 return result;
20491 }
20492 else if (TYPE_P (t))
20493 {
20494 /* [basic.link]: A name with no linkage (notably, the name
20495 of a class or enumeration declared in a local scope)
20496 shall not be used to declare an entity with linkage.
20497 This implies that names with no linkage cannot be used as
20498 template arguments
20499
20500 DR 757 relaxes this restriction for C++0x. */
20501 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20502 : no_linkage_check (t, /*relaxed_p=*/false));
20503
20504 if (nt)
20505 {
20506 /* DR 488 makes use of a type with no linkage cause
20507 type deduction to fail. */
20508 if (complain & tf_error)
20509 {
20510 if (TYPE_UNNAMED_P (nt))
20511 error ("%qT is/uses unnamed type", t);
20512 else
20513 error ("template argument for %qD uses local type %qT",
20514 tmpl, t);
20515 }
20516 return true;
20517 }
20518 /* In order to avoid all sorts of complications, we do not
20519 allow variably-modified types as template arguments. */
20520 else if (variably_modified_type_p (t, NULL_TREE))
20521 {
20522 if (complain & tf_error)
20523 error ("%qT is a variably modified type", t);
20524 return true;
20525 }
20526 }
20527 /* Class template and alias template arguments should be OK. */
20528 else if (DECL_TYPE_TEMPLATE_P (t))
20529 ;
20530 /* A non-type argument of integral or enumerated type must be a
20531 constant. */
20532 else if (TREE_TYPE (t)
20533 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20534 && !REFERENCE_REF_P (t)
20535 && !TREE_CONSTANT (t))
20536 {
20537 if (complain & tf_error)
20538 error ("integral expression %qE is not constant", t);
20539 return true;
20540 }
20541 return false;
20542 }
20543
20544 static bool
20545 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20546 {
20547 int ix, len = DECL_NTPARMS (tmpl);
20548 bool result = false;
20549
20550 for (ix = 0; ix != len; ix++)
20551 {
20552 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20553 result = true;
20554 }
20555 if (result && (complain & tf_error))
20556 error (" trying to instantiate %qD", tmpl);
20557 return result;
20558 }
20559
20560 /* We're out of SFINAE context now, so generate diagnostics for the access
20561 errors we saw earlier when instantiating D from TMPL and ARGS. */
20562
20563 static void
20564 recheck_decl_substitution (tree d, tree tmpl, tree args)
20565 {
20566 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20567 tree type = TREE_TYPE (pattern);
20568 location_t loc = input_location;
20569
20570 push_access_scope (d);
20571 push_deferring_access_checks (dk_no_deferred);
20572 input_location = DECL_SOURCE_LOCATION (pattern);
20573 tsubst (type, args, tf_warning_or_error, d);
20574 input_location = loc;
20575 pop_deferring_access_checks ();
20576 pop_access_scope (d);
20577 }
20578
20579 /* Instantiate the indicated variable, function, or alias template TMPL with
20580 the template arguments in TARG_PTR. */
20581
20582 static tree
20583 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20584 {
20585 tree targ_ptr = orig_args;
20586 tree fndecl;
20587 tree gen_tmpl;
20588 tree spec;
20589 bool access_ok = true;
20590
20591 if (tmpl == error_mark_node)
20592 return error_mark_node;
20593
20594 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20595
20596 /* If this function is a clone, handle it specially. */
20597 if (DECL_CLONED_FUNCTION_P (tmpl))
20598 {
20599 tree spec;
20600 tree clone;
20601
20602 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20603 DECL_CLONED_FUNCTION. */
20604 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20605 targ_ptr, complain);
20606 if (spec == error_mark_node)
20607 return error_mark_node;
20608
20609 /* Look for the clone. */
20610 FOR_EACH_CLONE (clone, spec)
20611 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20612 return clone;
20613 /* We should always have found the clone by now. */
20614 gcc_unreachable ();
20615 return NULL_TREE;
20616 }
20617
20618 if (targ_ptr == error_mark_node)
20619 return error_mark_node;
20620
20621 /* Check to see if we already have this specialization. */
20622 gen_tmpl = most_general_template (tmpl);
20623 if (TMPL_ARGS_DEPTH (targ_ptr)
20624 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20625 /* targ_ptr only has the innermost template args, so add the outer ones
20626 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20627 the case of a non-dependent call within a template definition). */
20628 targ_ptr = (add_outermost_template_args
20629 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20630 targ_ptr));
20631
20632 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20633 but it doesn't seem to be on the hot path. */
20634 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20635
20636 gcc_checking_assert (tmpl == gen_tmpl
20637 || ((fndecl
20638 = retrieve_specialization (tmpl, orig_args, 0))
20639 == spec)
20640 || fndecl == NULL_TREE);
20641
20642 if (spec != NULL_TREE)
20643 {
20644 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20645 {
20646 if (complain & tf_error)
20647 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20648 return error_mark_node;
20649 }
20650 return spec;
20651 }
20652
20653 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20654 complain))
20655 return error_mark_node;
20656
20657 /* We are building a FUNCTION_DECL, during which the access of its
20658 parameters and return types have to be checked. However this
20659 FUNCTION_DECL which is the desired context for access checking
20660 is not built yet. We solve this chicken-and-egg problem by
20661 deferring all checks until we have the FUNCTION_DECL. */
20662 push_deferring_access_checks (dk_deferred);
20663
20664 /* Instantiation of the function happens in the context of the function
20665 template, not the context of the overload resolution we're doing. */
20666 push_to_top_level ();
20667 /* If there are dependent arguments, e.g. because we're doing partial
20668 ordering, make sure processing_template_decl stays set. */
20669 if (uses_template_parms (targ_ptr))
20670 ++processing_template_decl;
20671 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20672 {
20673 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20674 complain, gen_tmpl, true);
20675 push_nested_class (ctx);
20676 }
20677
20678 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20679
20680 fndecl = NULL_TREE;
20681 if (VAR_P (pattern))
20682 {
20683 /* We need to determine if we're using a partial or explicit
20684 specialization now, because the type of the variable could be
20685 different. */
20686 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20687 tree elt = most_specialized_partial_spec (tid, complain);
20688 if (elt == error_mark_node)
20689 pattern = error_mark_node;
20690 else if (elt)
20691 {
20692 tree partial_tmpl = TREE_VALUE (elt);
20693 tree partial_args = TREE_PURPOSE (elt);
20694 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20695 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20696 }
20697 }
20698
20699 /* Substitute template parameters to obtain the specialization. */
20700 if (fndecl == NULL_TREE)
20701 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20702 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20703 pop_nested_class ();
20704 pop_from_top_level ();
20705
20706 if (fndecl == error_mark_node)
20707 {
20708 pop_deferring_access_checks ();
20709 return error_mark_node;
20710 }
20711
20712 /* The DECL_TI_TEMPLATE should always be the immediate parent
20713 template, not the most general template. */
20714 DECL_TI_TEMPLATE (fndecl) = tmpl;
20715 DECL_TI_ARGS (fndecl) = targ_ptr;
20716
20717 /* Now we know the specialization, compute access previously
20718 deferred. Do no access control for inheriting constructors,
20719 as we already checked access for the inherited constructor. */
20720 if (!(flag_new_inheriting_ctors
20721 && DECL_INHERITED_CTOR (fndecl)))
20722 {
20723 push_access_scope (fndecl);
20724 if (!perform_deferred_access_checks (complain))
20725 access_ok = false;
20726 pop_access_scope (fndecl);
20727 }
20728 pop_deferring_access_checks ();
20729
20730 /* If we've just instantiated the main entry point for a function,
20731 instantiate all the alternate entry points as well. We do this
20732 by cloning the instantiation of the main entry point, not by
20733 instantiating the template clones. */
20734 if (tree chain = DECL_CHAIN (gen_tmpl))
20735 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20736 clone_function_decl (fndecl, /*update_methods=*/false);
20737
20738 if (!access_ok)
20739 {
20740 if (!(complain & tf_error))
20741 {
20742 /* Remember to reinstantiate when we're out of SFINAE so the user
20743 can see the errors. */
20744 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20745 }
20746 return error_mark_node;
20747 }
20748 return fndecl;
20749 }
20750
20751 /* Wrapper for instantiate_template_1. */
20752
20753 tree
20754 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20755 {
20756 tree ret;
20757 timevar_push (TV_TEMPLATE_INST);
20758 ret = instantiate_template_1 (tmpl, orig_args, complain);
20759 timevar_pop (TV_TEMPLATE_INST);
20760 return ret;
20761 }
20762
20763 /* Instantiate the alias template TMPL with ARGS. Also push a template
20764 instantiation level, which instantiate_template doesn't do because
20765 functions and variables have sufficient context established by the
20766 callers. */
20767
20768 static tree
20769 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20770 {
20771 if (tmpl == error_mark_node || args == error_mark_node)
20772 return error_mark_node;
20773
20774 args =
20775 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20776 args, tmpl, complain,
20777 /*require_all_args=*/true,
20778 /*use_default_args=*/true);
20779
20780 /* FIXME check for satisfaction in check_instantiated_args. */
20781 if (flag_concepts
20782 && !any_dependent_template_arguments_p (args)
20783 && !constraints_satisfied_p (tmpl, args))
20784 {
20785 if (complain & tf_error)
20786 {
20787 auto_diagnostic_group d;
20788 error ("template constraint failure for %qD", tmpl);
20789 diagnose_constraints (input_location, tmpl, args);
20790 }
20791 return error_mark_node;
20792 }
20793
20794 if (!push_tinst_level (tmpl, args))
20795 return error_mark_node;
20796 tree r = instantiate_template (tmpl, args, complain);
20797 pop_tinst_level ();
20798
20799 return r;
20800 }
20801
20802 /* PARM is a template parameter pack for FN. Returns true iff
20803 PARM is used in a deducible way in the argument list of FN. */
20804
20805 static bool
20806 pack_deducible_p (tree parm, tree fn)
20807 {
20808 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20809 for (; t; t = TREE_CHAIN (t))
20810 {
20811 tree type = TREE_VALUE (t);
20812 tree packs;
20813 if (!PACK_EXPANSION_P (type))
20814 continue;
20815 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20816 packs; packs = TREE_CHAIN (packs))
20817 if (template_args_equal (TREE_VALUE (packs), parm))
20818 {
20819 /* The template parameter pack is used in a function parameter
20820 pack. If this is the end of the parameter list, the
20821 template parameter pack is deducible. */
20822 if (TREE_CHAIN (t) == void_list_node)
20823 return true;
20824 else
20825 /* Otherwise, not. Well, it could be deduced from
20826 a non-pack parameter, but doing so would end up with
20827 a deduction mismatch, so don't bother. */
20828 return false;
20829 }
20830 }
20831 /* The template parameter pack isn't used in any function parameter
20832 packs, but it might be used deeper, e.g. tuple<Args...>. */
20833 return true;
20834 }
20835
20836 /* Subroutine of fn_type_unification: check non-dependent parms for
20837 convertibility. */
20838
20839 static int
20840 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20841 tree fn, unification_kind_t strict, int flags,
20842 struct conversion **convs, bool explain_p)
20843 {
20844 /* Non-constructor methods need to leave a conversion for 'this', which
20845 isn't included in nargs here. */
20846 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20847 && !DECL_CONSTRUCTOR_P (fn));
20848
20849 for (unsigned ia = 0;
20850 parms && parms != void_list_node && ia < nargs; )
20851 {
20852 tree parm = TREE_VALUE (parms);
20853
20854 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20855 && (!TREE_CHAIN (parms)
20856 || TREE_CHAIN (parms) == void_list_node))
20857 /* For a function parameter pack that occurs at the end of the
20858 parameter-declaration-list, the type A of each remaining
20859 argument of the call is compared with the type P of the
20860 declarator-id of the function parameter pack. */
20861 break;
20862
20863 parms = TREE_CHAIN (parms);
20864
20865 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20866 /* For a function parameter pack that does not occur at the
20867 end of the parameter-declaration-list, the type of the
20868 parameter pack is a non-deduced context. */
20869 continue;
20870
20871 if (!uses_template_parms (parm))
20872 {
20873 tree arg = args[ia];
20874 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20875 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20876
20877 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20878 conv_p, explain_p))
20879 return 1;
20880 }
20881
20882 ++ia;
20883 }
20884
20885 return 0;
20886 }
20887
20888 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20889 NARGS elements of the arguments that are being used when calling
20890 it. TARGS is a vector into which the deduced template arguments
20891 are placed.
20892
20893 Returns either a FUNCTION_DECL for the matching specialization of FN or
20894 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20895 true, diagnostics will be printed to explain why it failed.
20896
20897 If FN is a conversion operator, or we are trying to produce a specific
20898 specialization, RETURN_TYPE is the return type desired.
20899
20900 The EXPLICIT_TARGS are explicit template arguments provided via a
20901 template-id.
20902
20903 The parameter STRICT is one of:
20904
20905 DEDUCE_CALL:
20906 We are deducing arguments for a function call, as in
20907 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20908 deducing arguments for a call to the result of a conversion
20909 function template, as in [over.call.object].
20910
20911 DEDUCE_CONV:
20912 We are deducing arguments for a conversion function, as in
20913 [temp.deduct.conv].
20914
20915 DEDUCE_EXACT:
20916 We are deducing arguments when doing an explicit instantiation
20917 as in [temp.explicit], when determining an explicit specialization
20918 as in [temp.expl.spec], or when taking the address of a function
20919 template, as in [temp.deduct.funcaddr]. */
20920
20921 tree
20922 fn_type_unification (tree fn,
20923 tree explicit_targs,
20924 tree targs,
20925 const tree *args,
20926 unsigned int nargs,
20927 tree return_type,
20928 unification_kind_t strict,
20929 int flags,
20930 struct conversion **convs,
20931 bool explain_p,
20932 bool decltype_p)
20933 {
20934 tree parms;
20935 tree fntype;
20936 tree decl = NULL_TREE;
20937 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20938 bool ok;
20939 static int deduction_depth;
20940 /* type_unification_real will pass back any access checks from default
20941 template argument substitution. */
20942 vec<deferred_access_check, va_gc> *checks = NULL;
20943 /* We don't have all the template args yet. */
20944 bool incomplete = true;
20945
20946 tree orig_fn = fn;
20947 if (flag_new_inheriting_ctors)
20948 fn = strip_inheriting_ctors (fn);
20949
20950 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20951 tree r = error_mark_node;
20952
20953 tree full_targs = targs;
20954 if (TMPL_ARGS_DEPTH (targs)
20955 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20956 full_targs = (add_outermost_template_args
20957 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20958 targs));
20959
20960 if (decltype_p)
20961 complain |= tf_decltype;
20962
20963 /* In C++0x, it's possible to have a function template whose type depends
20964 on itself recursively. This is most obvious with decltype, but can also
20965 occur with enumeration scope (c++/48969). So we need to catch infinite
20966 recursion and reject the substitution at deduction time; this function
20967 will return error_mark_node for any repeated substitution.
20968
20969 This also catches excessive recursion such as when f<N> depends on
20970 f<N-1> across all integers, and returns error_mark_node for all the
20971 substitutions back up to the initial one.
20972
20973 This is, of course, not reentrant. */
20974 if (excessive_deduction_depth)
20975 return error_mark_node;
20976 ++deduction_depth;
20977
20978 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20979
20980 fntype = TREE_TYPE (fn);
20981 if (explicit_targs)
20982 {
20983 /* [temp.deduct]
20984
20985 The specified template arguments must match the template
20986 parameters in kind (i.e., type, nontype, template), and there
20987 must not be more arguments than there are parameters;
20988 otherwise type deduction fails.
20989
20990 Nontype arguments must match the types of the corresponding
20991 nontype template parameters, or must be convertible to the
20992 types of the corresponding nontype parameters as specified in
20993 _temp.arg.nontype_, otherwise type deduction fails.
20994
20995 All references in the function type of the function template
20996 to the corresponding template parameters are replaced by the
20997 specified template argument values. If a substitution in a
20998 template parameter or in the function type of the function
20999 template results in an invalid type, type deduction fails. */
21000 int i, len = TREE_VEC_LENGTH (tparms);
21001 location_t loc = input_location;
21002 incomplete = false;
21003
21004 if (explicit_targs == error_mark_node)
21005 goto fail;
21006
21007 if (TMPL_ARGS_DEPTH (explicit_targs)
21008 < TMPL_ARGS_DEPTH (full_targs))
21009 explicit_targs = add_outermost_template_args (full_targs,
21010 explicit_targs);
21011
21012 /* Adjust any explicit template arguments before entering the
21013 substitution context. */
21014 explicit_targs
21015 = (coerce_template_parms (tparms, explicit_targs, fn,
21016 complain|tf_partial,
21017 /*require_all_args=*/false,
21018 /*use_default_args=*/false));
21019 if (explicit_targs == error_mark_node)
21020 goto fail;
21021
21022 /* Substitute the explicit args into the function type. This is
21023 necessary so that, for instance, explicitly declared function
21024 arguments can match null pointed constants. If we were given
21025 an incomplete set of explicit args, we must not do semantic
21026 processing during substitution as we could create partial
21027 instantiations. */
21028 for (i = 0; i < len; i++)
21029 {
21030 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21031 bool parameter_pack = false;
21032 tree targ = TREE_VEC_ELT (explicit_targs, i);
21033
21034 /* Dig out the actual parm. */
21035 if (TREE_CODE (parm) == TYPE_DECL
21036 || TREE_CODE (parm) == TEMPLATE_DECL)
21037 {
21038 parm = TREE_TYPE (parm);
21039 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21040 }
21041 else if (TREE_CODE (parm) == PARM_DECL)
21042 {
21043 parm = DECL_INITIAL (parm);
21044 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21045 }
21046
21047 if (targ == NULL_TREE)
21048 /* No explicit argument for this template parameter. */
21049 incomplete = true;
21050 else if (parameter_pack && pack_deducible_p (parm, fn))
21051 {
21052 /* Mark the argument pack as "incomplete". We could
21053 still deduce more arguments during unification.
21054 We remove this mark in type_unification_real. */
21055 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21056 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21057 = ARGUMENT_PACK_ARGS (targ);
21058
21059 /* We have some incomplete argument packs. */
21060 incomplete = true;
21061 }
21062 }
21063
21064 if (incomplete)
21065 {
21066 if (!push_tinst_level (fn, explicit_targs))
21067 {
21068 excessive_deduction_depth = true;
21069 goto fail;
21070 }
21071 ++processing_template_decl;
21072 input_location = DECL_SOURCE_LOCATION (fn);
21073 /* Ignore any access checks; we'll see them again in
21074 instantiate_template and they might have the wrong
21075 access path at this point. */
21076 push_deferring_access_checks (dk_deferred);
21077 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21078 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21079 pop_deferring_access_checks ();
21080 input_location = loc;
21081 --processing_template_decl;
21082 pop_tinst_level ();
21083
21084 if (fntype == error_mark_node)
21085 goto fail;
21086 }
21087
21088 /* Place the explicitly specified arguments in TARGS. */
21089 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21090 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21091 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21092 if (!incomplete && CHECKING_P
21093 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21094 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21095 (targs, NUM_TMPL_ARGS (explicit_targs));
21096 }
21097
21098 if (return_type && strict != DEDUCE_CALL)
21099 {
21100 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21101 new_args[0] = return_type;
21102 memcpy (new_args + 1, args, nargs * sizeof (tree));
21103 args = new_args;
21104 ++nargs;
21105 }
21106
21107 if (!incomplete)
21108 goto deduced;
21109
21110 /* Never do unification on the 'this' parameter. */
21111 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21112
21113 if (return_type && strict == DEDUCE_CALL)
21114 {
21115 /* We're deducing for a call to the result of a template conversion
21116 function. The parms we really want are in return_type. */
21117 if (INDIRECT_TYPE_P (return_type))
21118 return_type = TREE_TYPE (return_type);
21119 parms = TYPE_ARG_TYPES (return_type);
21120 }
21121 else if (return_type)
21122 {
21123 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21124 }
21125
21126 /* We allow incomplete unification without an error message here
21127 because the standard doesn't seem to explicitly prohibit it. Our
21128 callers must be ready to deal with unification failures in any
21129 event. */
21130
21131 /* If we aren't explaining yet, push tinst context so we can see where
21132 any errors (e.g. from class instantiations triggered by instantiation
21133 of default template arguments) come from. If we are explaining, this
21134 context is redundant. */
21135 if (!explain_p && !push_tinst_level (fn, targs))
21136 {
21137 excessive_deduction_depth = true;
21138 goto fail;
21139 }
21140
21141 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21142 full_targs, parms, args, nargs, /*subr=*/0,
21143 strict, &checks, explain_p);
21144 if (!explain_p)
21145 pop_tinst_level ();
21146 if (!ok)
21147 goto fail;
21148
21149 /* Now that we have bindings for all of the template arguments,
21150 ensure that the arguments deduced for the template template
21151 parameters have compatible template parameter lists. We cannot
21152 check this property before we have deduced all template
21153 arguments, because the template parameter types of a template
21154 template parameter might depend on prior template parameters
21155 deduced after the template template parameter. The following
21156 ill-formed example illustrates this issue:
21157
21158 template<typename T, template<T> class C> void f(C<5>, T);
21159
21160 template<int N> struct X {};
21161
21162 void g() {
21163 f(X<5>(), 5l); // error: template argument deduction fails
21164 }
21165
21166 The template parameter list of 'C' depends on the template type
21167 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21168 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21169 time that we deduce 'C'. */
21170 if (!template_template_parm_bindings_ok_p
21171 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21172 {
21173 unify_inconsistent_template_template_parameters (explain_p);
21174 goto fail;
21175 }
21176
21177 /* DR 1391: All parameters have args, now check non-dependent parms for
21178 convertibility. */
21179 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21180 convs, explain_p))
21181 goto fail;
21182
21183 deduced:
21184 /* All is well so far. Now, check:
21185
21186 [temp.deduct]
21187
21188 When all template arguments have been deduced, all uses of
21189 template parameters in nondeduced contexts are replaced with
21190 the corresponding deduced argument values. If the
21191 substitution results in an invalid type, as described above,
21192 type deduction fails. */
21193 if (!push_tinst_level (fn, targs))
21194 {
21195 excessive_deduction_depth = true;
21196 goto fail;
21197 }
21198
21199 /* Also collect access checks from the instantiation. */
21200 reopen_deferring_access_checks (checks);
21201
21202 decl = instantiate_template (fn, targs, complain);
21203
21204 checks = get_deferred_access_checks ();
21205 pop_deferring_access_checks ();
21206
21207 pop_tinst_level ();
21208
21209 if (decl == error_mark_node)
21210 goto fail;
21211
21212 /* Now perform any access checks encountered during substitution. */
21213 push_access_scope (decl);
21214 ok = perform_access_checks (checks, complain);
21215 pop_access_scope (decl);
21216 if (!ok)
21217 goto fail;
21218
21219 /* If we're looking for an exact match, check that what we got
21220 is indeed an exact match. It might not be if some template
21221 parameters are used in non-deduced contexts. But don't check
21222 for an exact match if we have dependent template arguments;
21223 in that case we're doing partial ordering, and we already know
21224 that we have two candidates that will provide the actual type. */
21225 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21226 {
21227 tree substed = TREE_TYPE (decl);
21228 unsigned int i;
21229
21230 tree sarg
21231 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21232 if (return_type)
21233 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21234 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21235 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21236 {
21237 unify_type_mismatch (explain_p, args[i],
21238 TREE_VALUE (sarg));
21239 goto fail;
21240 }
21241 }
21242
21243 /* After doing deduction with the inherited constructor, actually return an
21244 instantiation of the inheriting constructor. */
21245 if (orig_fn != fn)
21246 decl = instantiate_template (orig_fn, targs, complain);
21247
21248 r = decl;
21249
21250 fail:
21251 --deduction_depth;
21252 if (excessive_deduction_depth)
21253 {
21254 if (deduction_depth == 0)
21255 /* Reset once we're all the way out. */
21256 excessive_deduction_depth = false;
21257 }
21258
21259 return r;
21260 }
21261
21262 /* Adjust types before performing type deduction, as described in
21263 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21264 sections are symmetric. PARM is the type of a function parameter
21265 or the return type of the conversion function. ARG is the type of
21266 the argument passed to the call, or the type of the value
21267 initialized with the result of the conversion function.
21268 ARG_EXPR is the original argument expression, which may be null. */
21269
21270 static int
21271 maybe_adjust_types_for_deduction (unification_kind_t strict,
21272 tree* parm,
21273 tree* arg,
21274 tree arg_expr)
21275 {
21276 int result = 0;
21277
21278 switch (strict)
21279 {
21280 case DEDUCE_CALL:
21281 break;
21282
21283 case DEDUCE_CONV:
21284 /* Swap PARM and ARG throughout the remainder of this
21285 function; the handling is precisely symmetric since PARM
21286 will initialize ARG rather than vice versa. */
21287 std::swap (parm, arg);
21288 break;
21289
21290 case DEDUCE_EXACT:
21291 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21292 too, but here handle it by stripping the reference from PARM
21293 rather than by adding it to ARG. */
21294 if (TYPE_REF_P (*parm)
21295 && TYPE_REF_IS_RVALUE (*parm)
21296 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21297 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21298 && TYPE_REF_P (*arg)
21299 && !TYPE_REF_IS_RVALUE (*arg))
21300 *parm = TREE_TYPE (*parm);
21301 /* Nothing else to do in this case. */
21302 return 0;
21303
21304 default:
21305 gcc_unreachable ();
21306 }
21307
21308 if (!TYPE_REF_P (*parm))
21309 {
21310 /* [temp.deduct.call]
21311
21312 If P is not a reference type:
21313
21314 --If A is an array type, the pointer type produced by the
21315 array-to-pointer standard conversion (_conv.array_) is
21316 used in place of A for type deduction; otherwise,
21317
21318 --If A is a function type, the pointer type produced by
21319 the function-to-pointer standard conversion
21320 (_conv.func_) is used in place of A for type deduction;
21321 otherwise,
21322
21323 --If A is a cv-qualified type, the top level
21324 cv-qualifiers of A's type are ignored for type
21325 deduction. */
21326 if (TREE_CODE (*arg) == ARRAY_TYPE)
21327 *arg = build_pointer_type (TREE_TYPE (*arg));
21328 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21329 *arg = build_pointer_type (*arg);
21330 else
21331 *arg = TYPE_MAIN_VARIANT (*arg);
21332 }
21333
21334 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21335 reference to a cv-unqualified template parameter that does not represent a
21336 template parameter of a class template (during class template argument
21337 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21338 an lvalue, the type "lvalue reference to A" is used in place of A for type
21339 deduction. */
21340 if (TYPE_REF_P (*parm)
21341 && TYPE_REF_IS_RVALUE (*parm)
21342 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21343 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21344 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21345 && (arg_expr ? lvalue_p (arg_expr)
21346 /* try_one_overload doesn't provide an arg_expr, but
21347 functions are always lvalues. */
21348 : TREE_CODE (*arg) == FUNCTION_TYPE))
21349 *arg = build_reference_type (*arg);
21350
21351 /* [temp.deduct.call]
21352
21353 If P is a cv-qualified type, the top level cv-qualifiers
21354 of P's type are ignored for type deduction. If P is a
21355 reference type, the type referred to by P is used for
21356 type deduction. */
21357 *parm = TYPE_MAIN_VARIANT (*parm);
21358 if (TYPE_REF_P (*parm))
21359 {
21360 *parm = TREE_TYPE (*parm);
21361 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21362 }
21363
21364 /* DR 322. For conversion deduction, remove a reference type on parm
21365 too (which has been swapped into ARG). */
21366 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21367 *arg = TREE_TYPE (*arg);
21368
21369 return result;
21370 }
21371
21372 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21373 template which doesn't contain any deducible template parameters; check if
21374 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21375 unify_one_argument. */
21376
21377 static int
21378 check_non_deducible_conversion (tree parm, tree arg, int strict,
21379 int flags, struct conversion **conv_p,
21380 bool explain_p)
21381 {
21382 tree type;
21383
21384 if (!TYPE_P (arg))
21385 type = TREE_TYPE (arg);
21386 else
21387 type = arg;
21388
21389 if (same_type_p (parm, type))
21390 return unify_success (explain_p);
21391
21392 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21393 if (strict == DEDUCE_CONV)
21394 {
21395 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21396 return unify_success (explain_p);
21397 }
21398 else if (strict != DEDUCE_EXACT)
21399 {
21400 bool ok = false;
21401 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21402 if (conv_p)
21403 /* Avoid recalculating this in add_function_candidate. */
21404 ok = (*conv_p
21405 = good_conversion (parm, type, conv_arg, flags, complain));
21406 else
21407 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21408 if (ok)
21409 return unify_success (explain_p);
21410 }
21411
21412 if (strict == DEDUCE_EXACT)
21413 return unify_type_mismatch (explain_p, parm, arg);
21414 else
21415 return unify_arg_conversion (explain_p, parm, type, arg);
21416 }
21417
21418 static bool uses_deducible_template_parms (tree type);
21419
21420 /* Returns true iff the expression EXPR is one from which a template
21421 argument can be deduced. In other words, if it's an undecorated
21422 use of a template non-type parameter. */
21423
21424 static bool
21425 deducible_expression (tree expr)
21426 {
21427 /* Strip implicit conversions. */
21428 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21429 expr = TREE_OPERAND (expr, 0);
21430 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21431 }
21432
21433 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21434 deducible way; that is, if it has a max value of <PARM> - 1. */
21435
21436 static bool
21437 deducible_array_bound (tree domain)
21438 {
21439 if (domain == NULL_TREE)
21440 return false;
21441
21442 tree max = TYPE_MAX_VALUE (domain);
21443 if (TREE_CODE (max) != MINUS_EXPR)
21444 return false;
21445
21446 return deducible_expression (TREE_OPERAND (max, 0));
21447 }
21448
21449 /* Returns true iff the template arguments ARGS use a template parameter
21450 in a deducible way. */
21451
21452 static bool
21453 deducible_template_args (tree args)
21454 {
21455 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21456 {
21457 bool deducible;
21458 tree elt = TREE_VEC_ELT (args, i);
21459 if (ARGUMENT_PACK_P (elt))
21460 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21461 else
21462 {
21463 if (PACK_EXPANSION_P (elt))
21464 elt = PACK_EXPANSION_PATTERN (elt);
21465 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21466 deducible = true;
21467 else if (TYPE_P (elt))
21468 deducible = uses_deducible_template_parms (elt);
21469 else
21470 deducible = deducible_expression (elt);
21471 }
21472 if (deducible)
21473 return true;
21474 }
21475 return false;
21476 }
21477
21478 /* Returns true iff TYPE contains any deducible references to template
21479 parameters, as per 14.8.2.5. */
21480
21481 static bool
21482 uses_deducible_template_parms (tree type)
21483 {
21484 if (PACK_EXPANSION_P (type))
21485 type = PACK_EXPANSION_PATTERN (type);
21486
21487 /* T
21488 cv-list T
21489 TT<T>
21490 TT<i>
21491 TT<> */
21492 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21493 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21494 return true;
21495
21496 /* T*
21497 T&
21498 T&& */
21499 if (INDIRECT_TYPE_P (type))
21500 return uses_deducible_template_parms (TREE_TYPE (type));
21501
21502 /* T[integer-constant ]
21503 type [i] */
21504 if (TREE_CODE (type) == ARRAY_TYPE)
21505 return (uses_deducible_template_parms (TREE_TYPE (type))
21506 || deducible_array_bound (TYPE_DOMAIN (type)));
21507
21508 /* T type ::*
21509 type T::*
21510 T T::*
21511 T (type ::*)()
21512 type (T::*)()
21513 type (type ::*)(T)
21514 type (T::*)(T)
21515 T (type ::*)(T)
21516 T (T::*)()
21517 T (T::*)(T) */
21518 if (TYPE_PTRMEM_P (type))
21519 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21520 || (uses_deducible_template_parms
21521 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21522
21523 /* template-name <T> (where template-name refers to a class template)
21524 template-name <i> (where template-name refers to a class template) */
21525 if (CLASS_TYPE_P (type)
21526 && CLASSTYPE_TEMPLATE_INFO (type)
21527 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21528 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21529 (CLASSTYPE_TI_ARGS (type)));
21530
21531 /* type (T)
21532 T()
21533 T(T) */
21534 if (FUNC_OR_METHOD_TYPE_P (type))
21535 {
21536 if (uses_deducible_template_parms (TREE_TYPE (type)))
21537 return true;
21538 tree parm = TYPE_ARG_TYPES (type);
21539 if (TREE_CODE (type) == METHOD_TYPE)
21540 parm = TREE_CHAIN (parm);
21541 for (; parm; parm = TREE_CHAIN (parm))
21542 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21543 return true;
21544 }
21545
21546 return false;
21547 }
21548
21549 /* Subroutine of type_unification_real and unify_pack_expansion to
21550 handle unification of a single P/A pair. Parameters are as
21551 for those functions. */
21552
21553 static int
21554 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21555 int subr, unification_kind_t strict,
21556 bool explain_p)
21557 {
21558 tree arg_expr = NULL_TREE;
21559 int arg_strict;
21560
21561 if (arg == error_mark_node || parm == error_mark_node)
21562 return unify_invalid (explain_p);
21563 if (arg == unknown_type_node)
21564 /* We can't deduce anything from this, but we might get all the
21565 template args from other function args. */
21566 return unify_success (explain_p);
21567
21568 /* Implicit conversions (Clause 4) will be performed on a function
21569 argument to convert it to the type of the corresponding function
21570 parameter if the parameter type contains no template-parameters that
21571 participate in template argument deduction. */
21572 if (strict != DEDUCE_EXACT
21573 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21574 /* For function parameters with no deducible template parameters,
21575 just return. We'll check non-dependent conversions later. */
21576 return unify_success (explain_p);
21577
21578 switch (strict)
21579 {
21580 case DEDUCE_CALL:
21581 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21582 | UNIFY_ALLOW_MORE_CV_QUAL
21583 | UNIFY_ALLOW_DERIVED);
21584 break;
21585
21586 case DEDUCE_CONV:
21587 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21588 break;
21589
21590 case DEDUCE_EXACT:
21591 arg_strict = UNIFY_ALLOW_NONE;
21592 break;
21593
21594 default:
21595 gcc_unreachable ();
21596 }
21597
21598 /* We only do these transformations if this is the top-level
21599 parameter_type_list in a call or declaration matching; in other
21600 situations (nested function declarators, template argument lists) we
21601 won't be comparing a type to an expression, and we don't do any type
21602 adjustments. */
21603 if (!subr)
21604 {
21605 if (!TYPE_P (arg))
21606 {
21607 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21608 if (type_unknown_p (arg))
21609 {
21610 /* [temp.deduct.type] A template-argument can be
21611 deduced from a pointer to function or pointer
21612 to member function argument if the set of
21613 overloaded functions does not contain function
21614 templates and at most one of a set of
21615 overloaded functions provides a unique
21616 match. */
21617 resolve_overloaded_unification (tparms, targs, parm,
21618 arg, strict,
21619 arg_strict, explain_p);
21620 /* If a unique match was not found, this is a
21621 non-deduced context, so we still succeed. */
21622 return unify_success (explain_p);
21623 }
21624
21625 arg_expr = arg;
21626 arg = unlowered_expr_type (arg);
21627 if (arg == error_mark_node)
21628 return unify_invalid (explain_p);
21629 }
21630
21631 arg_strict |=
21632 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21633 }
21634 else
21635 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21636 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21637 return unify_template_argument_mismatch (explain_p, parm, arg);
21638
21639 /* For deduction from an init-list we need the actual list. */
21640 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21641 arg = arg_expr;
21642 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21643 }
21644
21645 /* for_each_template_parm callback that always returns 0. */
21646
21647 static int
21648 zero_r (tree, void *)
21649 {
21650 return 0;
21651 }
21652
21653 /* for_each_template_parm any_fn callback to handle deduction of a template
21654 type argument from the type of an array bound. */
21655
21656 static int
21657 array_deduction_r (tree t, void *data)
21658 {
21659 tree_pair_p d = (tree_pair_p)data;
21660 tree &tparms = d->purpose;
21661 tree &targs = d->value;
21662
21663 if (TREE_CODE (t) == ARRAY_TYPE)
21664 if (tree dom = TYPE_DOMAIN (t))
21665 if (tree max = TYPE_MAX_VALUE (dom))
21666 {
21667 if (TREE_CODE (max) == MINUS_EXPR)
21668 max = TREE_OPERAND (max, 0);
21669 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21670 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21671 UNIFY_ALLOW_NONE, /*explain*/false);
21672 }
21673
21674 /* Keep walking. */
21675 return 0;
21676 }
21677
21678 /* Try to deduce any not-yet-deduced template type arguments from the type of
21679 an array bound. This is handled separately from unify because 14.8.2.5 says
21680 "The type of a type parameter is only deduced from an array bound if it is
21681 not otherwise deduced." */
21682
21683 static void
21684 try_array_deduction (tree tparms, tree targs, tree parm)
21685 {
21686 tree_pair_s data = { tparms, targs };
21687 hash_set<tree> visited;
21688 for_each_template_parm (parm, zero_r, &data, &visited,
21689 /*nondeduced*/false, array_deduction_r);
21690 }
21691
21692 /* Most parms like fn_type_unification.
21693
21694 If SUBR is 1, we're being called recursively (to unify the
21695 arguments of a function or method parameter of a function
21696 template).
21697
21698 CHECKS is a pointer to a vector of access checks encountered while
21699 substituting default template arguments. */
21700
21701 static int
21702 type_unification_real (tree tparms,
21703 tree full_targs,
21704 tree xparms,
21705 const tree *xargs,
21706 unsigned int xnargs,
21707 int subr,
21708 unification_kind_t strict,
21709 vec<deferred_access_check, va_gc> **checks,
21710 bool explain_p)
21711 {
21712 tree parm, arg;
21713 int i;
21714 int ntparms = TREE_VEC_LENGTH (tparms);
21715 int saw_undeduced = 0;
21716 tree parms;
21717 const tree *args;
21718 unsigned int nargs;
21719 unsigned int ia;
21720
21721 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21722 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21723 gcc_assert (ntparms > 0);
21724
21725 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21726
21727 /* Reset the number of non-defaulted template arguments contained
21728 in TARGS. */
21729 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21730
21731 again:
21732 parms = xparms;
21733 args = xargs;
21734 nargs = xnargs;
21735
21736 ia = 0;
21737 while (parms && parms != void_list_node
21738 && ia < nargs)
21739 {
21740 parm = TREE_VALUE (parms);
21741
21742 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21743 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21744 /* For a function parameter pack that occurs at the end of the
21745 parameter-declaration-list, the type A of each remaining
21746 argument of the call is compared with the type P of the
21747 declarator-id of the function parameter pack. */
21748 break;
21749
21750 parms = TREE_CHAIN (parms);
21751
21752 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21753 /* For a function parameter pack that does not occur at the
21754 end of the parameter-declaration-list, the type of the
21755 parameter pack is a non-deduced context. */
21756 continue;
21757
21758 arg = args[ia];
21759 ++ia;
21760
21761 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21762 explain_p))
21763 return 1;
21764 }
21765
21766 if (parms
21767 && parms != void_list_node
21768 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21769 {
21770 /* Unify the remaining arguments with the pack expansion type. */
21771 tree argvec;
21772 tree parmvec = make_tree_vec (1);
21773
21774 /* Allocate a TREE_VEC and copy in all of the arguments */
21775 argvec = make_tree_vec (nargs - ia);
21776 for (i = 0; ia < nargs; ++ia, ++i)
21777 TREE_VEC_ELT (argvec, i) = args[ia];
21778
21779 /* Copy the parameter into parmvec. */
21780 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21781 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21782 /*subr=*/subr, explain_p))
21783 return 1;
21784
21785 /* Advance to the end of the list of parameters. */
21786 parms = TREE_CHAIN (parms);
21787 }
21788
21789 /* Fail if we've reached the end of the parm list, and more args
21790 are present, and the parm list isn't variadic. */
21791 if (ia < nargs && parms == void_list_node)
21792 return unify_too_many_arguments (explain_p, nargs, ia);
21793 /* Fail if parms are left and they don't have default values and
21794 they aren't all deduced as empty packs (c++/57397). This is
21795 consistent with sufficient_parms_p. */
21796 if (parms && parms != void_list_node
21797 && TREE_PURPOSE (parms) == NULL_TREE)
21798 {
21799 unsigned int count = nargs;
21800 tree p = parms;
21801 bool type_pack_p;
21802 do
21803 {
21804 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21805 if (!type_pack_p)
21806 count++;
21807 p = TREE_CHAIN (p);
21808 }
21809 while (p && p != void_list_node);
21810 if (count != nargs)
21811 return unify_too_few_arguments (explain_p, ia, count,
21812 type_pack_p);
21813 }
21814
21815 if (!subr)
21816 {
21817 tsubst_flags_t complain = (explain_p
21818 ? tf_warning_or_error
21819 : tf_none);
21820 bool tried_array_deduction = (cxx_dialect < cxx17);
21821
21822 for (i = 0; i < ntparms; i++)
21823 {
21824 tree targ = TREE_VEC_ELT (targs, i);
21825 tree tparm = TREE_VEC_ELT (tparms, i);
21826
21827 /* Clear the "incomplete" flags on all argument packs now so that
21828 substituting them into later default arguments works. */
21829 if (targ && ARGUMENT_PACK_P (targ))
21830 {
21831 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21832 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21833 }
21834
21835 if (targ || tparm == error_mark_node)
21836 continue;
21837 tparm = TREE_VALUE (tparm);
21838
21839 if (TREE_CODE (tparm) == TYPE_DECL
21840 && !tried_array_deduction)
21841 {
21842 try_array_deduction (tparms, targs, xparms);
21843 tried_array_deduction = true;
21844 if (TREE_VEC_ELT (targs, i))
21845 continue;
21846 }
21847
21848 /* If this is an undeduced nontype parameter that depends on
21849 a type parameter, try another pass; its type may have been
21850 deduced from a later argument than the one from which
21851 this parameter can be deduced. */
21852 if (TREE_CODE (tparm) == PARM_DECL
21853 && uses_template_parms (TREE_TYPE (tparm))
21854 && saw_undeduced < 2)
21855 {
21856 saw_undeduced = 1;
21857 continue;
21858 }
21859
21860 /* Core issue #226 (C++0x) [temp.deduct]:
21861
21862 If a template argument has not been deduced, its
21863 default template argument, if any, is used.
21864
21865 When we are in C++98 mode, TREE_PURPOSE will either
21866 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21867 to explicitly check cxx_dialect here. */
21868 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21869 /* OK, there is a default argument. Wait until after the
21870 conversion check to do substitution. */
21871 continue;
21872
21873 /* If the type parameter is a parameter pack, then it will
21874 be deduced to an empty parameter pack. */
21875 if (template_parameter_pack_p (tparm))
21876 {
21877 tree arg;
21878
21879 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21880 {
21881 arg = make_node (NONTYPE_ARGUMENT_PACK);
21882 TREE_CONSTANT (arg) = 1;
21883 }
21884 else
21885 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21886
21887 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21888
21889 TREE_VEC_ELT (targs, i) = arg;
21890 continue;
21891 }
21892
21893 return unify_parameter_deduction_failure (explain_p, tparm);
21894 }
21895
21896 /* Now substitute into the default template arguments. */
21897 for (i = 0; i < ntparms; i++)
21898 {
21899 tree targ = TREE_VEC_ELT (targs, i);
21900 tree tparm = TREE_VEC_ELT (tparms, i);
21901
21902 if (targ || tparm == error_mark_node)
21903 continue;
21904 tree parm = TREE_VALUE (tparm);
21905 tree arg = TREE_PURPOSE (tparm);
21906 reopen_deferring_access_checks (*checks);
21907 location_t save_loc = input_location;
21908 if (DECL_P (parm))
21909 input_location = DECL_SOURCE_LOCATION (parm);
21910
21911 if (saw_undeduced == 1
21912 && TREE_CODE (parm) == PARM_DECL
21913 && uses_template_parms (TREE_TYPE (parm)))
21914 {
21915 /* The type of this non-type parameter depends on undeduced
21916 parameters. Don't try to use its default argument yet,
21917 since we might deduce an argument for it on the next pass,
21918 but do check whether the arguments we already have cause
21919 substitution failure, so that that happens before we try
21920 later default arguments (78489). */
21921 ++processing_template_decl;
21922 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21923 NULL_TREE);
21924 --processing_template_decl;
21925 if (type == error_mark_node)
21926 arg = error_mark_node;
21927 else
21928 arg = NULL_TREE;
21929 }
21930 else
21931 {
21932 /* Even if the call is happening in template context, getting
21933 here means it's non-dependent, and a default argument is
21934 considered a separate definition under [temp.decls], so we can
21935 do this substitution without processing_template_decl. This
21936 is important if the default argument contains something that
21937 might be instantiation-dependent like access (87480). */
21938 processing_template_decl_sentinel s;
21939 tree substed = NULL_TREE;
21940 if (saw_undeduced == 1)
21941 {
21942 /* First instatiate in template context, in case we still
21943 depend on undeduced template parameters. */
21944 ++processing_template_decl;
21945 substed = tsubst_template_arg (arg, full_targs, complain,
21946 NULL_TREE);
21947 --processing_template_decl;
21948 if (substed != error_mark_node
21949 && !uses_template_parms (substed))
21950 /* We replaced all the tparms, substitute again out of
21951 template context. */
21952 substed = NULL_TREE;
21953 }
21954 if (!substed)
21955 substed = tsubst_template_arg (arg, full_targs, complain,
21956 NULL_TREE);
21957
21958 if (!uses_template_parms (substed))
21959 arg = convert_template_argument (parm, substed, full_targs,
21960 complain, i, NULL_TREE);
21961 else if (saw_undeduced == 1)
21962 arg = NULL_TREE;
21963 else
21964 arg = error_mark_node;
21965 }
21966
21967 input_location = save_loc;
21968 *checks = get_deferred_access_checks ();
21969 pop_deferring_access_checks ();
21970
21971 if (arg == error_mark_node)
21972 return 1;
21973 else if (arg)
21974 {
21975 TREE_VEC_ELT (targs, i) = arg;
21976 /* The position of the first default template argument,
21977 is also the number of non-defaulted arguments in TARGS.
21978 Record that. */
21979 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21980 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21981 }
21982 }
21983
21984 if (saw_undeduced++ == 1)
21985 goto again;
21986 }
21987
21988 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21990
21991 return unify_success (explain_p);
21992 }
21993
21994 /* Subroutine of type_unification_real. Args are like the variables
21995 at the call site. ARG is an overloaded function (or template-id);
21996 we try deducing template args from each of the overloads, and if
21997 only one succeeds, we go with that. Modifies TARGS and returns
21998 true on success. */
21999
22000 static bool
22001 resolve_overloaded_unification (tree tparms,
22002 tree targs,
22003 tree parm,
22004 tree arg,
22005 unification_kind_t strict,
22006 int sub_strict,
22007 bool explain_p)
22008 {
22009 tree tempargs = copy_node (targs);
22010 int good = 0;
22011 tree goodfn = NULL_TREE;
22012 bool addr_p;
22013
22014 if (TREE_CODE (arg) == ADDR_EXPR)
22015 {
22016 arg = TREE_OPERAND (arg, 0);
22017 addr_p = true;
22018 }
22019 else
22020 addr_p = false;
22021
22022 if (TREE_CODE (arg) == COMPONENT_REF)
22023 /* Handle `&x' where `x' is some static or non-static member
22024 function name. */
22025 arg = TREE_OPERAND (arg, 1);
22026
22027 if (TREE_CODE (arg) == OFFSET_REF)
22028 arg = TREE_OPERAND (arg, 1);
22029
22030 /* Strip baselink information. */
22031 if (BASELINK_P (arg))
22032 arg = BASELINK_FUNCTIONS (arg);
22033
22034 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22035 {
22036 /* If we got some explicit template args, we need to plug them into
22037 the affected templates before we try to unify, in case the
22038 explicit args will completely resolve the templates in question. */
22039
22040 int ok = 0;
22041 tree expl_subargs = TREE_OPERAND (arg, 1);
22042 arg = TREE_OPERAND (arg, 0);
22043
22044 for (lkp_iterator iter (arg); iter; ++iter)
22045 {
22046 tree fn = *iter;
22047 tree subargs, elem;
22048
22049 if (TREE_CODE (fn) != TEMPLATE_DECL)
22050 continue;
22051
22052 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22053 expl_subargs, NULL_TREE, tf_none,
22054 /*require_all_args=*/true,
22055 /*use_default_args=*/true);
22056 if (subargs != error_mark_node
22057 && !any_dependent_template_arguments_p (subargs))
22058 {
22059 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
22060 if (try_one_overload (tparms, targs, tempargs, parm,
22061 elem, strict, sub_strict, addr_p, explain_p)
22062 && (!goodfn || !same_type_p (goodfn, elem)))
22063 {
22064 goodfn = elem;
22065 ++good;
22066 }
22067 }
22068 else if (subargs)
22069 ++ok;
22070 }
22071 /* If no templates (or more than one) are fully resolved by the
22072 explicit arguments, this template-id is a non-deduced context; it
22073 could still be OK if we deduce all template arguments for the
22074 enclosing call through other arguments. */
22075 if (good != 1)
22076 good = ok;
22077 }
22078 else if (!OVL_P (arg))
22079 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22080 -- but the deduction does not succeed because the expression is
22081 not just the function on its own. */
22082 return false;
22083 else
22084 for (lkp_iterator iter (arg); iter; ++iter)
22085 {
22086 tree fn = *iter;
22087 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22088 strict, sub_strict, addr_p, explain_p)
22089 && (!goodfn || !decls_match (goodfn, fn)))
22090 {
22091 goodfn = fn;
22092 ++good;
22093 }
22094 }
22095
22096 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22097 to function or pointer to member function argument if the set of
22098 overloaded functions does not contain function templates and at most
22099 one of a set of overloaded functions provides a unique match.
22100
22101 So if we found multiple possibilities, we return success but don't
22102 deduce anything. */
22103
22104 if (good == 1)
22105 {
22106 int i = TREE_VEC_LENGTH (targs);
22107 for (; i--; )
22108 if (TREE_VEC_ELT (tempargs, i))
22109 {
22110 tree old = TREE_VEC_ELT (targs, i);
22111 tree new_ = TREE_VEC_ELT (tempargs, i);
22112 if (new_ && old && ARGUMENT_PACK_P (old)
22113 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22114 /* Don't forget explicit template arguments in a pack. */
22115 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22116 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22117 TREE_VEC_ELT (targs, i) = new_;
22118 }
22119 }
22120 if (good)
22121 return true;
22122
22123 return false;
22124 }
22125
22126 /* Core DR 115: In contexts where deduction is done and fails, or in
22127 contexts where deduction is not done, if a template argument list is
22128 specified and it, along with any default template arguments, identifies
22129 a single function template specialization, then the template-id is an
22130 lvalue for the function template specialization. */
22131
22132 tree
22133 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22134 {
22135 tree expr, offset, baselink;
22136 bool addr;
22137
22138 if (!type_unknown_p (orig_expr))
22139 return orig_expr;
22140
22141 expr = orig_expr;
22142 addr = false;
22143 offset = NULL_TREE;
22144 baselink = NULL_TREE;
22145
22146 if (TREE_CODE (expr) == ADDR_EXPR)
22147 {
22148 expr = TREE_OPERAND (expr, 0);
22149 addr = true;
22150 }
22151 if (TREE_CODE (expr) == OFFSET_REF)
22152 {
22153 offset = expr;
22154 expr = TREE_OPERAND (expr, 1);
22155 }
22156 if (BASELINK_P (expr))
22157 {
22158 baselink = expr;
22159 expr = BASELINK_FUNCTIONS (expr);
22160 }
22161
22162 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22163 {
22164 int good = 0;
22165 tree goodfn = NULL_TREE;
22166
22167 /* If we got some explicit template args, we need to plug them into
22168 the affected templates before we try to unify, in case the
22169 explicit args will completely resolve the templates in question. */
22170
22171 tree expl_subargs = TREE_OPERAND (expr, 1);
22172 tree arg = TREE_OPERAND (expr, 0);
22173 tree badfn = NULL_TREE;
22174 tree badargs = NULL_TREE;
22175
22176 for (lkp_iterator iter (arg); iter; ++iter)
22177 {
22178 tree fn = *iter;
22179 tree subargs, elem;
22180
22181 if (TREE_CODE (fn) != TEMPLATE_DECL)
22182 continue;
22183
22184 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22185 expl_subargs, NULL_TREE, tf_none,
22186 /*require_all_args=*/true,
22187 /*use_default_args=*/true);
22188 if (subargs != error_mark_node
22189 && !any_dependent_template_arguments_p (subargs))
22190 {
22191 elem = instantiate_template (fn, subargs, tf_none);
22192 if (elem == error_mark_node)
22193 {
22194 badfn = fn;
22195 badargs = subargs;
22196 }
22197 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
22198 {
22199 goodfn = elem;
22200 ++good;
22201 }
22202 }
22203 }
22204 if (good == 1)
22205 {
22206 mark_used (goodfn);
22207 expr = goodfn;
22208 if (baselink)
22209 expr = build_baselink (BASELINK_BINFO (baselink),
22210 BASELINK_ACCESS_BINFO (baselink),
22211 expr, BASELINK_OPTYPE (baselink));
22212 if (offset)
22213 {
22214 tree base
22215 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22216 expr = build_offset_ref (base, expr, addr, complain);
22217 }
22218 if (addr)
22219 expr = cp_build_addr_expr (expr, complain);
22220 return expr;
22221 }
22222 else if (good == 0 && badargs && (complain & tf_error))
22223 /* There were no good options and at least one bad one, so let the
22224 user know what the problem is. */
22225 instantiate_template (badfn, badargs, complain);
22226 }
22227 return orig_expr;
22228 }
22229
22230 /* As above, but error out if the expression remains overloaded. */
22231
22232 tree
22233 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22234 {
22235 exp = resolve_nondeduced_context (exp, complain);
22236 if (type_unknown_p (exp))
22237 {
22238 if (complain & tf_error)
22239 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22240 return error_mark_node;
22241 }
22242 return exp;
22243 }
22244
22245 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22246 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22247 different overloads deduce different arguments for a given parm.
22248 ADDR_P is true if the expression for which deduction is being
22249 performed was of the form "& fn" rather than simply "fn".
22250
22251 Returns 1 on success. */
22252
22253 static int
22254 try_one_overload (tree tparms,
22255 tree orig_targs,
22256 tree targs,
22257 tree parm,
22258 tree arg,
22259 unification_kind_t strict,
22260 int sub_strict,
22261 bool addr_p,
22262 bool explain_p)
22263 {
22264 int nargs;
22265 tree tempargs;
22266 int i;
22267
22268 if (arg == error_mark_node)
22269 return 0;
22270
22271 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22272 to function or pointer to member function argument if the set of
22273 overloaded functions does not contain function templates and at most
22274 one of a set of overloaded functions provides a unique match.
22275
22276 So if this is a template, just return success. */
22277
22278 if (uses_template_parms (arg))
22279 return 1;
22280
22281 if (TREE_CODE (arg) == METHOD_TYPE)
22282 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22283 else if (addr_p)
22284 arg = build_pointer_type (arg);
22285
22286 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22287
22288 /* We don't copy orig_targs for this because if we have already deduced
22289 some template args from previous args, unify would complain when we
22290 try to deduce a template parameter for the same argument, even though
22291 there isn't really a conflict. */
22292 nargs = TREE_VEC_LENGTH (targs);
22293 tempargs = make_tree_vec (nargs);
22294
22295 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22296 return 0;
22297
22298 /* First make sure we didn't deduce anything that conflicts with
22299 explicitly specified args. */
22300 for (i = nargs; i--; )
22301 {
22302 tree elt = TREE_VEC_ELT (tempargs, i);
22303 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22304
22305 if (!elt)
22306 /*NOP*/;
22307 else if (uses_template_parms (elt))
22308 /* Since we're unifying against ourselves, we will fill in
22309 template args used in the function parm list with our own
22310 template parms. Discard them. */
22311 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22312 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22313 {
22314 /* Check that the argument at each index of the deduced argument pack
22315 is equivalent to the corresponding explicitly specified argument.
22316 We may have deduced more arguments than were explicitly specified,
22317 and that's OK. */
22318
22319 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22320 that's wrong if we deduce the same argument pack from multiple
22321 function arguments: it's only incomplete the first time. */
22322
22323 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22324 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22325
22326 if (TREE_VEC_LENGTH (deduced_pack)
22327 < TREE_VEC_LENGTH (explicit_pack))
22328 return 0;
22329
22330 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22331 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22332 TREE_VEC_ELT (deduced_pack, j)))
22333 return 0;
22334 }
22335 else if (oldelt && !template_args_equal (oldelt, elt))
22336 return 0;
22337 }
22338
22339 for (i = nargs; i--; )
22340 {
22341 tree elt = TREE_VEC_ELT (tempargs, i);
22342
22343 if (elt)
22344 TREE_VEC_ELT (targs, i) = elt;
22345 }
22346
22347 return 1;
22348 }
22349
22350 /* PARM is a template class (perhaps with unbound template
22351 parameters). ARG is a fully instantiated type. If ARG can be
22352 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22353 TARGS are as for unify. */
22354
22355 static tree
22356 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22357 bool explain_p)
22358 {
22359 tree copy_of_targs;
22360
22361 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22362 return NULL_TREE;
22363 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22364 /* Matches anything. */;
22365 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22366 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22367 return NULL_TREE;
22368
22369 /* We need to make a new template argument vector for the call to
22370 unify. If we used TARGS, we'd clutter it up with the result of
22371 the attempted unification, even if this class didn't work out.
22372 We also don't want to commit ourselves to all the unifications
22373 we've already done, since unification is supposed to be done on
22374 an argument-by-argument basis. In other words, consider the
22375 following pathological case:
22376
22377 template <int I, int J, int K>
22378 struct S {};
22379
22380 template <int I, int J>
22381 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22382
22383 template <int I, int J, int K>
22384 void f(S<I, J, K>, S<I, I, I>);
22385
22386 void g() {
22387 S<0, 0, 0> s0;
22388 S<0, 1, 2> s2;
22389
22390 f(s0, s2);
22391 }
22392
22393 Now, by the time we consider the unification involving `s2', we
22394 already know that we must have `f<0, 0, 0>'. But, even though
22395 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22396 because there are two ways to unify base classes of S<0, 1, 2>
22397 with S<I, I, I>. If we kept the already deduced knowledge, we
22398 would reject the possibility I=1. */
22399 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22400
22401 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22402 {
22403 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22404 return NULL_TREE;
22405 return arg;
22406 }
22407
22408 /* If unification failed, we're done. */
22409 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22410 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22411 return NULL_TREE;
22412
22413 return arg;
22414 }
22415
22416 /* Given a template type PARM and a class type ARG, find the unique
22417 base type in ARG that is an instance of PARM. We do not examine
22418 ARG itself; only its base-classes. If there is not exactly one
22419 appropriate base class, return NULL_TREE. PARM may be the type of
22420 a partial specialization, as well as a plain template type. Used
22421 by unify. */
22422
22423 static enum template_base_result
22424 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22425 bool explain_p, tree *result)
22426 {
22427 tree rval = NULL_TREE;
22428 tree binfo;
22429
22430 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22431
22432 binfo = TYPE_BINFO (complete_type (arg));
22433 if (!binfo)
22434 {
22435 /* The type could not be completed. */
22436 *result = NULL_TREE;
22437 return tbr_incomplete_type;
22438 }
22439
22440 /* Walk in inheritance graph order. The search order is not
22441 important, and this avoids multiple walks of virtual bases. */
22442 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22443 {
22444 tree r = try_class_unification (tparms, targs, parm,
22445 BINFO_TYPE (binfo), explain_p);
22446
22447 if (r)
22448 {
22449 /* If there is more than one satisfactory baseclass, then:
22450
22451 [temp.deduct.call]
22452
22453 If they yield more than one possible deduced A, the type
22454 deduction fails.
22455
22456 applies. */
22457 if (rval && !same_type_p (r, rval))
22458 {
22459 *result = NULL_TREE;
22460 return tbr_ambiguous_baseclass;
22461 }
22462
22463 rval = r;
22464 }
22465 }
22466
22467 *result = rval;
22468 return tbr_success;
22469 }
22470
22471 /* Returns the level of DECL, which declares a template parameter. */
22472
22473 static int
22474 template_decl_level (tree decl)
22475 {
22476 switch (TREE_CODE (decl))
22477 {
22478 case TYPE_DECL:
22479 case TEMPLATE_DECL:
22480 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22481
22482 case PARM_DECL:
22483 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22484
22485 default:
22486 gcc_unreachable ();
22487 }
22488 return 0;
22489 }
22490
22491 /* Decide whether ARG can be unified with PARM, considering only the
22492 cv-qualifiers of each type, given STRICT as documented for unify.
22493 Returns nonzero iff the unification is OK on that basis. */
22494
22495 static int
22496 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22497 {
22498 int arg_quals = cp_type_quals (arg);
22499 int parm_quals = cp_type_quals (parm);
22500
22501 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22502 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22503 {
22504 /* Although a CVR qualifier is ignored when being applied to a
22505 substituted template parameter ([8.3.2]/1 for example), that
22506 does not allow us to unify "const T" with "int&" because both
22507 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22508 It is ok when we're allowing additional CV qualifiers
22509 at the outer level [14.8.2.1]/3,1st bullet. */
22510 if ((TYPE_REF_P (arg)
22511 || FUNC_OR_METHOD_TYPE_P (arg))
22512 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22513 return 0;
22514
22515 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22516 && (parm_quals & TYPE_QUAL_RESTRICT))
22517 return 0;
22518 }
22519
22520 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22521 && (arg_quals & parm_quals) != parm_quals)
22522 return 0;
22523
22524 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22525 && (parm_quals & arg_quals) != arg_quals)
22526 return 0;
22527
22528 return 1;
22529 }
22530
22531 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22532 void
22533 template_parm_level_and_index (tree parm, int* level, int* index)
22534 {
22535 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22536 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22537 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22538 {
22539 *index = TEMPLATE_TYPE_IDX (parm);
22540 *level = TEMPLATE_TYPE_LEVEL (parm);
22541 }
22542 else
22543 {
22544 *index = TEMPLATE_PARM_IDX (parm);
22545 *level = TEMPLATE_PARM_LEVEL (parm);
22546 }
22547 }
22548
22549 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22550 do { \
22551 if (unify (TP, TA, P, A, S, EP)) \
22552 return 1; \
22553 } while (0)
22554
22555 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22556 expansion at the end of PACKED_PARMS. Returns 0 if the type
22557 deduction succeeds, 1 otherwise. STRICT is the same as in
22558 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22559 function call argument list. We'll need to adjust the arguments to make them
22560 types. SUBR tells us if this is from a recursive call to
22561 type_unification_real, or for comparing two template argument
22562 lists. */
22563
22564 static int
22565 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22566 tree packed_args, unification_kind_t strict,
22567 bool subr, bool explain_p)
22568 {
22569 tree parm
22570 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22571 tree pattern = PACK_EXPANSION_PATTERN (parm);
22572 tree pack, packs = NULL_TREE;
22573 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22574
22575 /* Add in any args remembered from an earlier partial instantiation. */
22576 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22577 int levels = TMPL_ARGS_DEPTH (targs);
22578
22579 packed_args = expand_template_argument_pack (packed_args);
22580
22581 int len = TREE_VEC_LENGTH (packed_args);
22582
22583 /* Determine the parameter packs we will be deducing from the
22584 pattern, and record their current deductions. */
22585 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22586 pack; pack = TREE_CHAIN (pack))
22587 {
22588 tree parm_pack = TREE_VALUE (pack);
22589 int idx, level;
22590
22591 /* Only template parameter packs can be deduced, not e.g. function
22592 parameter packs or __bases or __integer_pack. */
22593 if (!TEMPLATE_PARM_P (parm_pack))
22594 continue;
22595
22596 /* Determine the index and level of this parameter pack. */
22597 template_parm_level_and_index (parm_pack, &level, &idx);
22598 if (level < levels)
22599 continue;
22600
22601 /* Keep track of the parameter packs and their corresponding
22602 argument packs. */
22603 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22604 TREE_TYPE (packs) = make_tree_vec (len - start);
22605 }
22606
22607 /* Loop through all of the arguments that have not yet been
22608 unified and unify each with the pattern. */
22609 for (i = start; i < len; i++)
22610 {
22611 tree parm;
22612 bool any_explicit = false;
22613 tree arg = TREE_VEC_ELT (packed_args, i);
22614
22615 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22616 or the element of its argument pack at the current index if
22617 this argument was explicitly specified. */
22618 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22619 {
22620 int idx, level;
22621 tree arg, pargs;
22622 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22623
22624 arg = NULL_TREE;
22625 if (TREE_VALUE (pack)
22626 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22627 && (i - start < TREE_VEC_LENGTH (pargs)))
22628 {
22629 any_explicit = true;
22630 arg = TREE_VEC_ELT (pargs, i - start);
22631 }
22632 TMPL_ARG (targs, level, idx) = arg;
22633 }
22634
22635 /* If we had explicit template arguments, substitute them into the
22636 pattern before deduction. */
22637 if (any_explicit)
22638 {
22639 /* Some arguments might still be unspecified or dependent. */
22640 bool dependent;
22641 ++processing_template_decl;
22642 dependent = any_dependent_template_arguments_p (targs);
22643 if (!dependent)
22644 --processing_template_decl;
22645 parm = tsubst (pattern, targs,
22646 explain_p ? tf_warning_or_error : tf_none,
22647 NULL_TREE);
22648 if (dependent)
22649 --processing_template_decl;
22650 if (parm == error_mark_node)
22651 return 1;
22652 }
22653 else
22654 parm = pattern;
22655
22656 /* Unify the pattern with the current argument. */
22657 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22658 explain_p))
22659 return 1;
22660
22661 /* For each parameter pack, collect the deduced value. */
22662 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22663 {
22664 int idx, level;
22665 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22666
22667 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22668 TMPL_ARG (targs, level, idx);
22669 }
22670 }
22671
22672 /* Verify that the results of unification with the parameter packs
22673 produce results consistent with what we've seen before, and make
22674 the deduced argument packs available. */
22675 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22676 {
22677 tree old_pack = TREE_VALUE (pack);
22678 tree new_args = TREE_TYPE (pack);
22679 int i, len = TREE_VEC_LENGTH (new_args);
22680 int idx, level;
22681 bool nondeduced_p = false;
22682
22683 /* By default keep the original deduced argument pack.
22684 If necessary, more specific code is going to update the
22685 resulting deduced argument later down in this function. */
22686 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22687 TMPL_ARG (targs, level, idx) = old_pack;
22688
22689 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22690 actually deduce anything. */
22691 for (i = 0; i < len && !nondeduced_p; ++i)
22692 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22693 nondeduced_p = true;
22694 if (nondeduced_p)
22695 continue;
22696
22697 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22698 {
22699 /* If we had fewer function args than explicit template args,
22700 just use the explicits. */
22701 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22702 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22703 if (len < explicit_len)
22704 new_args = explicit_args;
22705 }
22706
22707 if (!old_pack)
22708 {
22709 tree result;
22710 /* Build the deduced *_ARGUMENT_PACK. */
22711 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22712 {
22713 result = make_node (NONTYPE_ARGUMENT_PACK);
22714 TREE_CONSTANT (result) = 1;
22715 }
22716 else
22717 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22718
22719 SET_ARGUMENT_PACK_ARGS (result, new_args);
22720
22721 /* Note the deduced argument packs for this parameter
22722 pack. */
22723 TMPL_ARG (targs, level, idx) = result;
22724 }
22725 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22726 && (ARGUMENT_PACK_ARGS (old_pack)
22727 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22728 {
22729 /* We only had the explicitly-provided arguments before, but
22730 now we have a complete set of arguments. */
22731 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22732
22733 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22734 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22735 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22736 }
22737 else
22738 {
22739 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22740 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22741
22742 if (!comp_template_args (old_args, new_args,
22743 &bad_old_arg, &bad_new_arg))
22744 /* Inconsistent unification of this parameter pack. */
22745 return unify_parameter_pack_inconsistent (explain_p,
22746 bad_old_arg,
22747 bad_new_arg);
22748 }
22749 }
22750
22751 return unify_success (explain_p);
22752 }
22753
22754 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22755 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22756 parameters and return value are as for unify. */
22757
22758 static int
22759 unify_array_domain (tree tparms, tree targs,
22760 tree parm_dom, tree arg_dom,
22761 bool explain_p)
22762 {
22763 tree parm_max;
22764 tree arg_max;
22765 bool parm_cst;
22766 bool arg_cst;
22767
22768 /* Our representation of array types uses "N - 1" as the
22769 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22770 not an integer constant. We cannot unify arbitrarily
22771 complex expressions, so we eliminate the MINUS_EXPRs
22772 here. */
22773 parm_max = TYPE_MAX_VALUE (parm_dom);
22774 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22775 if (!parm_cst)
22776 {
22777 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22778 parm_max = TREE_OPERAND (parm_max, 0);
22779 }
22780 arg_max = TYPE_MAX_VALUE (arg_dom);
22781 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22782 if (!arg_cst)
22783 {
22784 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22785 trying to unify the type of a variable with the type
22786 of a template parameter. For example:
22787
22788 template <unsigned int N>
22789 void f (char (&) [N]);
22790 int g();
22791 void h(int i) {
22792 char a[g(i)];
22793 f(a);
22794 }
22795
22796 Here, the type of the ARG will be "int [g(i)]", and
22797 may be a SAVE_EXPR, etc. */
22798 if (TREE_CODE (arg_max) != MINUS_EXPR)
22799 return unify_vla_arg (explain_p, arg_dom);
22800 arg_max = TREE_OPERAND (arg_max, 0);
22801 }
22802
22803 /* If only one of the bounds used a MINUS_EXPR, compensate
22804 by adding one to the other bound. */
22805 if (parm_cst && !arg_cst)
22806 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22807 integer_type_node,
22808 parm_max,
22809 integer_one_node);
22810 else if (arg_cst && !parm_cst)
22811 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22812 integer_type_node,
22813 arg_max,
22814 integer_one_node);
22815
22816 return unify (tparms, targs, parm_max, arg_max,
22817 UNIFY_ALLOW_INTEGER, explain_p);
22818 }
22819
22820 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22821
22822 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22823
22824 static pa_kind_t
22825 pa_kind (tree t)
22826 {
22827 if (PACK_EXPANSION_P (t))
22828 t = PACK_EXPANSION_PATTERN (t);
22829 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22830 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22831 || DECL_TYPE_TEMPLATE_P (t))
22832 return pa_tmpl;
22833 else if (TYPE_P (t))
22834 return pa_type;
22835 else
22836 return pa_expr;
22837 }
22838
22839 /* Deduce the value of template parameters. TPARMS is the (innermost)
22840 set of template parameters to a template. TARGS is the bindings
22841 for those template parameters, as determined thus far; TARGS may
22842 include template arguments for outer levels of template parameters
22843 as well. PARM is a parameter to a template function, or a
22844 subcomponent of that parameter; ARG is the corresponding argument.
22845 This function attempts to match PARM with ARG in a manner
22846 consistent with the existing assignments in TARGS. If more values
22847 are deduced, then TARGS is updated.
22848
22849 Returns 0 if the type deduction succeeds, 1 otherwise. The
22850 parameter STRICT is a bitwise or of the following flags:
22851
22852 UNIFY_ALLOW_NONE:
22853 Require an exact match between PARM and ARG.
22854 UNIFY_ALLOW_MORE_CV_QUAL:
22855 Allow the deduced ARG to be more cv-qualified (by qualification
22856 conversion) than ARG.
22857 UNIFY_ALLOW_LESS_CV_QUAL:
22858 Allow the deduced ARG to be less cv-qualified than ARG.
22859 UNIFY_ALLOW_DERIVED:
22860 Allow the deduced ARG to be a template base class of ARG,
22861 or a pointer to a template base class of the type pointed to by
22862 ARG.
22863 UNIFY_ALLOW_INTEGER:
22864 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22865 case for more information.
22866 UNIFY_ALLOW_OUTER_LEVEL:
22867 This is the outermost level of a deduction. Used to determine validity
22868 of qualification conversions. A valid qualification conversion must
22869 have const qualified pointers leading up to the inner type which
22870 requires additional CV quals, except at the outer level, where const
22871 is not required [conv.qual]. It would be normal to set this flag in
22872 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22873 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22874 This is the outermost level of a deduction, and PARM can be more CV
22875 qualified at this point.
22876 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22877 This is the outermost level of a deduction, and PARM can be less CV
22878 qualified at this point. */
22879
22880 static int
22881 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22882 bool explain_p)
22883 {
22884 int idx;
22885 tree targ;
22886 tree tparm;
22887 int strict_in = strict;
22888 tsubst_flags_t complain = (explain_p
22889 ? tf_warning_or_error
22890 : tf_none);
22891
22892 /* I don't think this will do the right thing with respect to types.
22893 But the only case I've seen it in so far has been array bounds, where
22894 signedness is the only information lost, and I think that will be
22895 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22896 finish_id_expression_1, and are also OK. */
22897 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22898 parm = TREE_OPERAND (parm, 0);
22899
22900 if (arg == error_mark_node)
22901 return unify_invalid (explain_p);
22902 if (arg == unknown_type_node
22903 || arg == init_list_type_node)
22904 /* We can't deduce anything from this, but we might get all the
22905 template args from other function args. */
22906 return unify_success (explain_p);
22907
22908 if (parm == any_targ_node || arg == any_targ_node)
22909 return unify_success (explain_p);
22910
22911 /* If PARM uses template parameters, then we can't bail out here,
22912 even if ARG == PARM, since we won't record unifications for the
22913 template parameters. We might need them if we're trying to
22914 figure out which of two things is more specialized. */
22915 if (arg == parm && !uses_template_parms (parm))
22916 return unify_success (explain_p);
22917
22918 /* Handle init lists early, so the rest of the function can assume
22919 we're dealing with a type. */
22920 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22921 {
22922 tree elt, elttype;
22923 unsigned i;
22924 tree orig_parm = parm;
22925
22926 if (!is_std_init_list (parm)
22927 && TREE_CODE (parm) != ARRAY_TYPE)
22928 /* We can only deduce from an initializer list argument if the
22929 parameter is std::initializer_list or an array; otherwise this
22930 is a non-deduced context. */
22931 return unify_success (explain_p);
22932
22933 if (TREE_CODE (parm) == ARRAY_TYPE)
22934 elttype = TREE_TYPE (parm);
22935 else
22936 {
22937 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22938 /* Deduction is defined in terms of a single type, so just punt
22939 on the (bizarre) std::initializer_list<T...>. */
22940 if (PACK_EXPANSION_P (elttype))
22941 return unify_success (explain_p);
22942 }
22943
22944 if (strict != DEDUCE_EXACT
22945 && TYPE_P (elttype)
22946 && !uses_deducible_template_parms (elttype))
22947 /* If ELTTYPE has no deducible template parms, skip deduction from
22948 the list elements. */;
22949 else
22950 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22951 {
22952 int elt_strict = strict;
22953
22954 if (elt == error_mark_node)
22955 return unify_invalid (explain_p);
22956
22957 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22958 {
22959 tree type = TREE_TYPE (elt);
22960 if (type == error_mark_node)
22961 return unify_invalid (explain_p);
22962 /* It should only be possible to get here for a call. */
22963 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22964 elt_strict |= maybe_adjust_types_for_deduction
22965 (DEDUCE_CALL, &elttype, &type, elt);
22966 elt = type;
22967 }
22968
22969 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22970 explain_p);
22971 }
22972
22973 if (TREE_CODE (parm) == ARRAY_TYPE
22974 && deducible_array_bound (TYPE_DOMAIN (parm)))
22975 {
22976 /* Also deduce from the length of the initializer list. */
22977 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22978 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22979 if (idx == error_mark_node)
22980 return unify_invalid (explain_p);
22981 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22982 idx, explain_p);
22983 }
22984
22985 /* If the std::initializer_list<T> deduction worked, replace the
22986 deduced A with std::initializer_list<A>. */
22987 if (orig_parm != parm)
22988 {
22989 idx = TEMPLATE_TYPE_IDX (orig_parm);
22990 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22991 targ = listify (targ);
22992 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22993 }
22994 return unify_success (explain_p);
22995 }
22996
22997 /* If parm and arg aren't the same kind of thing (template, type, or
22998 expression), fail early. */
22999 if (pa_kind (parm) != pa_kind (arg))
23000 return unify_invalid (explain_p);
23001
23002 /* Immediately reject some pairs that won't unify because of
23003 cv-qualification mismatches. */
23004 if (TREE_CODE (arg) == TREE_CODE (parm)
23005 && TYPE_P (arg)
23006 /* It is the elements of the array which hold the cv quals of an array
23007 type, and the elements might be template type parms. We'll check
23008 when we recurse. */
23009 && TREE_CODE (arg) != ARRAY_TYPE
23010 /* We check the cv-qualifiers when unifying with template type
23011 parameters below. We want to allow ARG `const T' to unify with
23012 PARM `T' for example, when computing which of two templates
23013 is more specialized, for example. */
23014 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23015 && !check_cv_quals_for_unify (strict_in, arg, parm))
23016 return unify_cv_qual_mismatch (explain_p, parm, arg);
23017
23018 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23019 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23020 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23021 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23022 strict &= ~UNIFY_ALLOW_DERIVED;
23023 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23024 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23025
23026 switch (TREE_CODE (parm))
23027 {
23028 case TYPENAME_TYPE:
23029 case SCOPE_REF:
23030 case UNBOUND_CLASS_TEMPLATE:
23031 /* In a type which contains a nested-name-specifier, template
23032 argument values cannot be deduced for template parameters used
23033 within the nested-name-specifier. */
23034 return unify_success (explain_p);
23035
23036 case TEMPLATE_TYPE_PARM:
23037 case TEMPLATE_TEMPLATE_PARM:
23038 case BOUND_TEMPLATE_TEMPLATE_PARM:
23039 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23040 if (error_operand_p (tparm))
23041 return unify_invalid (explain_p);
23042
23043 if (TEMPLATE_TYPE_LEVEL (parm)
23044 != template_decl_level (tparm))
23045 /* The PARM is not one we're trying to unify. Just check
23046 to see if it matches ARG. */
23047 {
23048 if (TREE_CODE (arg) == TREE_CODE (parm)
23049 && (is_auto (parm) ? is_auto (arg)
23050 : same_type_p (parm, arg)))
23051 return unify_success (explain_p);
23052 else
23053 return unify_type_mismatch (explain_p, parm, arg);
23054 }
23055 idx = TEMPLATE_TYPE_IDX (parm);
23056 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23057 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23058 if (error_operand_p (tparm))
23059 return unify_invalid (explain_p);
23060
23061 /* Check for mixed types and values. */
23062 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23063 && TREE_CODE (tparm) != TYPE_DECL)
23064 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23065 && TREE_CODE (tparm) != TEMPLATE_DECL))
23066 gcc_unreachable ();
23067
23068 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23069 {
23070 if ((strict_in & UNIFY_ALLOW_DERIVED)
23071 && CLASS_TYPE_P (arg))
23072 {
23073 /* First try to match ARG directly. */
23074 tree t = try_class_unification (tparms, targs, parm, arg,
23075 explain_p);
23076 if (!t)
23077 {
23078 /* Otherwise, look for a suitable base of ARG, as below. */
23079 enum template_base_result r;
23080 r = get_template_base (tparms, targs, parm, arg,
23081 explain_p, &t);
23082 if (!t)
23083 return unify_no_common_base (explain_p, r, parm, arg);
23084 arg = t;
23085 }
23086 }
23087 /* ARG must be constructed from a template class or a template
23088 template parameter. */
23089 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23090 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23091 return unify_template_deduction_failure (explain_p, parm, arg);
23092
23093 /* Deduce arguments T, i from TT<T> or TT<i>. */
23094 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23095 return 1;
23096
23097 arg = TYPE_TI_TEMPLATE (arg);
23098
23099 /* Fall through to deduce template name. */
23100 }
23101
23102 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23103 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23104 {
23105 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23106
23107 /* Simple cases: Value already set, does match or doesn't. */
23108 if (targ != NULL_TREE && template_args_equal (targ, arg))
23109 return unify_success (explain_p);
23110 else if (targ)
23111 return unify_inconsistency (explain_p, parm, targ, arg);
23112 }
23113 else
23114 {
23115 /* If PARM is `const T' and ARG is only `int', we don't have
23116 a match unless we are allowing additional qualification.
23117 If ARG is `const int' and PARM is just `T' that's OK;
23118 that binds `const int' to `T'. */
23119 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23120 arg, parm))
23121 return unify_cv_qual_mismatch (explain_p, parm, arg);
23122
23123 /* Consider the case where ARG is `const volatile int' and
23124 PARM is `const T'. Then, T should be `volatile int'. */
23125 arg = cp_build_qualified_type_real
23126 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23127 if (arg == error_mark_node)
23128 return unify_invalid (explain_p);
23129
23130 /* Simple cases: Value already set, does match or doesn't. */
23131 if (targ != NULL_TREE && same_type_p (targ, arg))
23132 return unify_success (explain_p);
23133 else if (targ)
23134 return unify_inconsistency (explain_p, parm, targ, arg);
23135
23136 /* Make sure that ARG is not a variable-sized array. (Note
23137 that were talking about variable-sized arrays (like
23138 `int[n]'), rather than arrays of unknown size (like
23139 `int[]').) We'll get very confused by such a type since
23140 the bound of the array is not constant, and therefore
23141 not mangleable. Besides, such types are not allowed in
23142 ISO C++, so we can do as we please here. We do allow
23143 them for 'auto' deduction, since that isn't ABI-exposed. */
23144 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23145 return unify_vla_arg (explain_p, arg);
23146
23147 /* Strip typedefs as in convert_template_argument. */
23148 arg = canonicalize_type_argument (arg, tf_none);
23149 }
23150
23151 /* If ARG is a parameter pack or an expansion, we cannot unify
23152 against it unless PARM is also a parameter pack. */
23153 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23154 && !template_parameter_pack_p (parm))
23155 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23156
23157 /* If the argument deduction results is a METHOD_TYPE,
23158 then there is a problem.
23159 METHOD_TYPE doesn't map to any real C++ type the result of
23160 the deduction cannot be of that type. */
23161 if (TREE_CODE (arg) == METHOD_TYPE)
23162 return unify_method_type_error (explain_p, arg);
23163
23164 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23165 return unify_success (explain_p);
23166
23167 case TEMPLATE_PARM_INDEX:
23168 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23169 if (error_operand_p (tparm))
23170 return unify_invalid (explain_p);
23171
23172 if (TEMPLATE_PARM_LEVEL (parm)
23173 != template_decl_level (tparm))
23174 {
23175 /* The PARM is not one we're trying to unify. Just check
23176 to see if it matches ARG. */
23177 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23178 && cp_tree_equal (parm, arg));
23179 if (result)
23180 unify_expression_unequal (explain_p, parm, arg);
23181 return result;
23182 }
23183
23184 idx = TEMPLATE_PARM_IDX (parm);
23185 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23186
23187 if (targ)
23188 {
23189 if ((strict & UNIFY_ALLOW_INTEGER)
23190 && TREE_TYPE (targ) && TREE_TYPE (arg)
23191 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23192 /* We're deducing from an array bound, the type doesn't matter. */
23193 arg = fold_convert (TREE_TYPE (targ), arg);
23194 int x = !cp_tree_equal (targ, arg);
23195 if (x)
23196 unify_inconsistency (explain_p, parm, targ, arg);
23197 return x;
23198 }
23199
23200 /* [temp.deduct.type] If, in the declaration of a function template
23201 with a non-type template-parameter, the non-type
23202 template-parameter is used in an expression in the function
23203 parameter-list and, if the corresponding template-argument is
23204 deduced, the template-argument type shall match the type of the
23205 template-parameter exactly, except that a template-argument
23206 deduced from an array bound may be of any integral type.
23207 The non-type parameter might use already deduced type parameters. */
23208 tparm = TREE_TYPE (parm);
23209 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23210 /* We don't have enough levels of args to do any substitution. This
23211 can happen in the context of -fnew-ttp-matching. */;
23212 else
23213 {
23214 ++processing_template_decl;
23215 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23216 --processing_template_decl;
23217
23218 if (tree a = type_uses_auto (tparm))
23219 {
23220 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23221 if (tparm == error_mark_node)
23222 return 1;
23223 }
23224 }
23225
23226 if (!TREE_TYPE (arg))
23227 /* Template-parameter dependent expression. Just accept it for now.
23228 It will later be processed in convert_template_argument. */
23229 ;
23230 else if (same_type_ignoring_top_level_qualifiers_p
23231 (non_reference (TREE_TYPE (arg)),
23232 non_reference (tparm)))
23233 /* OK. Ignore top-level quals here because a class-type template
23234 parameter object is const. */;
23235 else if ((strict & UNIFY_ALLOW_INTEGER)
23236 && CP_INTEGRAL_TYPE_P (tparm))
23237 /* Convert the ARG to the type of PARM; the deduced non-type
23238 template argument must exactly match the types of the
23239 corresponding parameter. */
23240 arg = fold (build_nop (tparm, arg));
23241 else if (uses_template_parms (tparm))
23242 {
23243 /* We haven't deduced the type of this parameter yet. */
23244 if (cxx_dialect >= cxx17
23245 /* We deduce from array bounds in try_array_deduction. */
23246 && !(strict & UNIFY_ALLOW_INTEGER))
23247 {
23248 /* Deduce it from the non-type argument. */
23249 tree atype = TREE_TYPE (arg);
23250 RECUR_AND_CHECK_FAILURE (tparms, targs,
23251 tparm, atype,
23252 UNIFY_ALLOW_NONE, explain_p);
23253 }
23254 else
23255 /* Try again later. */
23256 return unify_success (explain_p);
23257 }
23258 else
23259 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23260
23261 /* If ARG is a parameter pack or an expansion, we cannot unify
23262 against it unless PARM is also a parameter pack. */
23263 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23264 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23265 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23266
23267 {
23268 bool removed_attr = false;
23269 arg = strip_typedefs_expr (arg, &removed_attr);
23270 }
23271 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23272 return unify_success (explain_p);
23273
23274 case PTRMEM_CST:
23275 {
23276 /* A pointer-to-member constant can be unified only with
23277 another constant. */
23278 if (TREE_CODE (arg) != PTRMEM_CST)
23279 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23280
23281 /* Just unify the class member. It would be useless (and possibly
23282 wrong, depending on the strict flags) to unify also
23283 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23284 arg refer to the same variable, even if through different
23285 classes. For instance:
23286
23287 struct A { int x; };
23288 struct B : A { };
23289
23290 Unification of &A::x and &B::x must succeed. */
23291 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23292 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23293 }
23294
23295 case POINTER_TYPE:
23296 {
23297 if (!TYPE_PTR_P (arg))
23298 return unify_type_mismatch (explain_p, parm, arg);
23299
23300 /* [temp.deduct.call]
23301
23302 A can be another pointer or pointer to member type that can
23303 be converted to the deduced A via a qualification
23304 conversion (_conv.qual_).
23305
23306 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23307 This will allow for additional cv-qualification of the
23308 pointed-to types if appropriate. */
23309
23310 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23311 /* The derived-to-base conversion only persists through one
23312 level of pointers. */
23313 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23314
23315 return unify (tparms, targs, TREE_TYPE (parm),
23316 TREE_TYPE (arg), strict, explain_p);
23317 }
23318
23319 case REFERENCE_TYPE:
23320 if (!TYPE_REF_P (arg))
23321 return unify_type_mismatch (explain_p, parm, arg);
23322 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23323 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23324
23325 case ARRAY_TYPE:
23326 if (TREE_CODE (arg) != ARRAY_TYPE)
23327 return unify_type_mismatch (explain_p, parm, arg);
23328 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23329 != (TYPE_DOMAIN (arg) == NULL_TREE))
23330 return unify_type_mismatch (explain_p, parm, arg);
23331 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23332 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23333 if (TYPE_DOMAIN (parm) != NULL_TREE)
23334 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23335 TYPE_DOMAIN (arg), explain_p);
23336 return unify_success (explain_p);
23337
23338 case REAL_TYPE:
23339 case COMPLEX_TYPE:
23340 case VECTOR_TYPE:
23341 case INTEGER_TYPE:
23342 case BOOLEAN_TYPE:
23343 case ENUMERAL_TYPE:
23344 case VOID_TYPE:
23345 case NULLPTR_TYPE:
23346 if (TREE_CODE (arg) != TREE_CODE (parm))
23347 return unify_type_mismatch (explain_p, parm, arg);
23348
23349 /* We have already checked cv-qualification at the top of the
23350 function. */
23351 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23352 return unify_type_mismatch (explain_p, parm, arg);
23353
23354 /* As far as unification is concerned, this wins. Later checks
23355 will invalidate it if necessary. */
23356 return unify_success (explain_p);
23357
23358 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23359 /* Type INTEGER_CST can come from ordinary constant template args. */
23360 case INTEGER_CST:
23361 while (CONVERT_EXPR_P (arg))
23362 arg = TREE_OPERAND (arg, 0);
23363
23364 if (TREE_CODE (arg) != INTEGER_CST)
23365 return unify_template_argument_mismatch (explain_p, parm, arg);
23366 return (tree_int_cst_equal (parm, arg)
23367 ? unify_success (explain_p)
23368 : unify_template_argument_mismatch (explain_p, parm, arg));
23369
23370 case TREE_VEC:
23371 {
23372 int i, len, argslen;
23373 int parm_variadic_p = 0;
23374
23375 if (TREE_CODE (arg) != TREE_VEC)
23376 return unify_template_argument_mismatch (explain_p, parm, arg);
23377
23378 len = TREE_VEC_LENGTH (parm);
23379 argslen = TREE_VEC_LENGTH (arg);
23380
23381 /* Check for pack expansions in the parameters. */
23382 for (i = 0; i < len; ++i)
23383 {
23384 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23385 {
23386 if (i == len - 1)
23387 /* We can unify against something with a trailing
23388 parameter pack. */
23389 parm_variadic_p = 1;
23390 else
23391 /* [temp.deduct.type]/9: If the template argument list of
23392 P contains a pack expansion that is not the last
23393 template argument, the entire template argument list
23394 is a non-deduced context. */
23395 return unify_success (explain_p);
23396 }
23397 }
23398
23399 /* If we don't have enough arguments to satisfy the parameters
23400 (not counting the pack expression at the end), or we have
23401 too many arguments for a parameter list that doesn't end in
23402 a pack expression, we can't unify. */
23403 if (parm_variadic_p
23404 ? argslen < len - parm_variadic_p
23405 : argslen != len)
23406 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23407
23408 /* Unify all of the parameters that precede the (optional)
23409 pack expression. */
23410 for (i = 0; i < len - parm_variadic_p; ++i)
23411 {
23412 RECUR_AND_CHECK_FAILURE (tparms, targs,
23413 TREE_VEC_ELT (parm, i),
23414 TREE_VEC_ELT (arg, i),
23415 UNIFY_ALLOW_NONE, explain_p);
23416 }
23417 if (parm_variadic_p)
23418 return unify_pack_expansion (tparms, targs, parm, arg,
23419 DEDUCE_EXACT,
23420 /*subr=*/true, explain_p);
23421 return unify_success (explain_p);
23422 }
23423
23424 case RECORD_TYPE:
23425 case UNION_TYPE:
23426 if (TREE_CODE (arg) != TREE_CODE (parm))
23427 return unify_type_mismatch (explain_p, parm, arg);
23428
23429 if (TYPE_PTRMEMFUNC_P (parm))
23430 {
23431 if (!TYPE_PTRMEMFUNC_P (arg))
23432 return unify_type_mismatch (explain_p, parm, arg);
23433
23434 return unify (tparms, targs,
23435 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23436 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23437 strict, explain_p);
23438 }
23439 else if (TYPE_PTRMEMFUNC_P (arg))
23440 return unify_type_mismatch (explain_p, parm, arg);
23441
23442 if (CLASSTYPE_TEMPLATE_INFO (parm))
23443 {
23444 tree t = NULL_TREE;
23445
23446 if (strict_in & UNIFY_ALLOW_DERIVED)
23447 {
23448 /* First, we try to unify the PARM and ARG directly. */
23449 t = try_class_unification (tparms, targs,
23450 parm, arg, explain_p);
23451
23452 if (!t)
23453 {
23454 /* Fallback to the special case allowed in
23455 [temp.deduct.call]:
23456
23457 If P is a class, and P has the form
23458 template-id, then A can be a derived class of
23459 the deduced A. Likewise, if P is a pointer to
23460 a class of the form template-id, A can be a
23461 pointer to a derived class pointed to by the
23462 deduced A. */
23463 enum template_base_result r;
23464 r = get_template_base (tparms, targs, parm, arg,
23465 explain_p, &t);
23466
23467 if (!t)
23468 {
23469 /* Don't give the derived diagnostic if we're
23470 already dealing with the same template. */
23471 bool same_template
23472 = (CLASSTYPE_TEMPLATE_INFO (arg)
23473 && (CLASSTYPE_TI_TEMPLATE (parm)
23474 == CLASSTYPE_TI_TEMPLATE (arg)));
23475 return unify_no_common_base (explain_p && !same_template,
23476 r, parm, arg);
23477 }
23478 }
23479 }
23480 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23481 && (CLASSTYPE_TI_TEMPLATE (parm)
23482 == CLASSTYPE_TI_TEMPLATE (arg)))
23483 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23484 Then, we should unify `int' and `U'. */
23485 t = arg;
23486 else
23487 /* There's no chance of unification succeeding. */
23488 return unify_type_mismatch (explain_p, parm, arg);
23489
23490 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23491 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23492 }
23493 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23494 return unify_type_mismatch (explain_p, parm, arg);
23495 return unify_success (explain_p);
23496
23497 case METHOD_TYPE:
23498 case FUNCTION_TYPE:
23499 {
23500 unsigned int nargs;
23501 tree *args;
23502 tree a;
23503 unsigned int i;
23504
23505 if (TREE_CODE (arg) != TREE_CODE (parm))
23506 return unify_type_mismatch (explain_p, parm, arg);
23507
23508 /* CV qualifications for methods can never be deduced, they must
23509 match exactly. We need to check them explicitly here,
23510 because type_unification_real treats them as any other
23511 cv-qualified parameter. */
23512 if (TREE_CODE (parm) == METHOD_TYPE
23513 && (!check_cv_quals_for_unify
23514 (UNIFY_ALLOW_NONE,
23515 class_of_this_parm (arg),
23516 class_of_this_parm (parm))))
23517 return unify_cv_qual_mismatch (explain_p, parm, arg);
23518 if (TREE_CODE (arg) == FUNCTION_TYPE
23519 && type_memfn_quals (parm) != type_memfn_quals (arg))
23520 return unify_cv_qual_mismatch (explain_p, parm, arg);
23521 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23522 return unify_type_mismatch (explain_p, parm, arg);
23523
23524 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23525 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23526
23527 nargs = list_length (TYPE_ARG_TYPES (arg));
23528 args = XALLOCAVEC (tree, nargs);
23529 for (a = TYPE_ARG_TYPES (arg), i = 0;
23530 a != NULL_TREE && a != void_list_node;
23531 a = TREE_CHAIN (a), ++i)
23532 args[i] = TREE_VALUE (a);
23533 nargs = i;
23534
23535 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23536 args, nargs, 1, DEDUCE_EXACT,
23537 NULL, explain_p))
23538 return 1;
23539
23540 if (flag_noexcept_type)
23541 {
23542 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23543 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23544 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23545 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23546 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23547 && uses_template_parms (TREE_PURPOSE (pspec)))
23548 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23549 TREE_PURPOSE (aspec),
23550 UNIFY_ALLOW_NONE, explain_p);
23551 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23552 return unify_type_mismatch (explain_p, parm, arg);
23553 }
23554
23555 return 0;
23556 }
23557
23558 case OFFSET_TYPE:
23559 /* Unify a pointer to member with a pointer to member function, which
23560 deduces the type of the member as a function type. */
23561 if (TYPE_PTRMEMFUNC_P (arg))
23562 {
23563 /* Check top-level cv qualifiers */
23564 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23565 return unify_cv_qual_mismatch (explain_p, parm, arg);
23566
23567 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23568 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23569 UNIFY_ALLOW_NONE, explain_p);
23570
23571 /* Determine the type of the function we are unifying against. */
23572 tree fntype = static_fn_type (arg);
23573
23574 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23575 }
23576
23577 if (TREE_CODE (arg) != OFFSET_TYPE)
23578 return unify_type_mismatch (explain_p, parm, arg);
23579 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23580 TYPE_OFFSET_BASETYPE (arg),
23581 UNIFY_ALLOW_NONE, explain_p);
23582 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23583 strict, explain_p);
23584
23585 case CONST_DECL:
23586 if (DECL_TEMPLATE_PARM_P (parm))
23587 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23588 if (arg != scalar_constant_value (parm))
23589 return unify_template_argument_mismatch (explain_p, parm, arg);
23590 return unify_success (explain_p);
23591
23592 case FIELD_DECL:
23593 case TEMPLATE_DECL:
23594 /* Matched cases are handled by the ARG == PARM test above. */
23595 return unify_template_argument_mismatch (explain_p, parm, arg);
23596
23597 case VAR_DECL:
23598 /* We might get a variable as a non-type template argument in parm if the
23599 corresponding parameter is type-dependent. Make any necessary
23600 adjustments based on whether arg is a reference. */
23601 if (CONSTANT_CLASS_P (arg))
23602 parm = fold_non_dependent_expr (parm, complain);
23603 else if (REFERENCE_REF_P (arg))
23604 {
23605 tree sub = TREE_OPERAND (arg, 0);
23606 STRIP_NOPS (sub);
23607 if (TREE_CODE (sub) == ADDR_EXPR)
23608 arg = TREE_OPERAND (sub, 0);
23609 }
23610 /* Now use the normal expression code to check whether they match. */
23611 goto expr;
23612
23613 case TYPE_ARGUMENT_PACK:
23614 case NONTYPE_ARGUMENT_PACK:
23615 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23616 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23617
23618 case TYPEOF_TYPE:
23619 case DECLTYPE_TYPE:
23620 case UNDERLYING_TYPE:
23621 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23622 or UNDERLYING_TYPE nodes. */
23623 return unify_success (explain_p);
23624
23625 case ERROR_MARK:
23626 /* Unification fails if we hit an error node. */
23627 return unify_invalid (explain_p);
23628
23629 case INDIRECT_REF:
23630 if (REFERENCE_REF_P (parm))
23631 {
23632 bool pexp = PACK_EXPANSION_P (arg);
23633 if (pexp)
23634 arg = PACK_EXPANSION_PATTERN (arg);
23635 if (REFERENCE_REF_P (arg))
23636 arg = TREE_OPERAND (arg, 0);
23637 if (pexp)
23638 arg = make_pack_expansion (arg, complain);
23639 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23640 strict, explain_p);
23641 }
23642 /* FALLTHRU */
23643
23644 default:
23645 /* An unresolved overload is a nondeduced context. */
23646 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23647 return unify_success (explain_p);
23648 gcc_assert (EXPR_P (parm)
23649 || COMPOUND_LITERAL_P (parm)
23650 || TREE_CODE (parm) == TRAIT_EXPR);
23651 expr:
23652 /* We must be looking at an expression. This can happen with
23653 something like:
23654
23655 template <int I>
23656 void foo(S<I>, S<I + 2>);
23657
23658 or
23659
23660 template<typename T>
23661 void foo(A<T, T{}>);
23662
23663 This is a "non-deduced context":
23664
23665 [deduct.type]
23666
23667 The non-deduced contexts are:
23668
23669 --A non-type template argument or an array bound in which
23670 a subexpression references a template parameter.
23671
23672 In these cases, we assume deduction succeeded, but don't
23673 actually infer any unifications. */
23674
23675 if (!uses_template_parms (parm)
23676 && !template_args_equal (parm, arg))
23677 return unify_expression_unequal (explain_p, parm, arg);
23678 else
23679 return unify_success (explain_p);
23680 }
23681 }
23682 #undef RECUR_AND_CHECK_FAILURE
23683 \f
23684 /* Note that DECL can be defined in this translation unit, if
23685 required. */
23686
23687 static void
23688 mark_definable (tree decl)
23689 {
23690 tree clone;
23691 DECL_NOT_REALLY_EXTERN (decl) = 1;
23692 FOR_EACH_CLONE (clone, decl)
23693 DECL_NOT_REALLY_EXTERN (clone) = 1;
23694 }
23695
23696 /* Called if RESULT is explicitly instantiated, or is a member of an
23697 explicitly instantiated class. */
23698
23699 void
23700 mark_decl_instantiated (tree result, int extern_p)
23701 {
23702 SET_DECL_EXPLICIT_INSTANTIATION (result);
23703
23704 /* If this entity has already been written out, it's too late to
23705 make any modifications. */
23706 if (TREE_ASM_WRITTEN (result))
23707 return;
23708
23709 /* For anonymous namespace we don't need to do anything. */
23710 if (decl_anon_ns_mem_p (result))
23711 {
23712 gcc_assert (!TREE_PUBLIC (result));
23713 return;
23714 }
23715
23716 if (TREE_CODE (result) != FUNCTION_DECL)
23717 /* The TREE_PUBLIC flag for function declarations will have been
23718 set correctly by tsubst. */
23719 TREE_PUBLIC (result) = 1;
23720
23721 /* This might have been set by an earlier implicit instantiation. */
23722 DECL_COMDAT (result) = 0;
23723
23724 if (extern_p)
23725 DECL_NOT_REALLY_EXTERN (result) = 0;
23726 else
23727 {
23728 mark_definable (result);
23729 mark_needed (result);
23730 /* Always make artificials weak. */
23731 if (DECL_ARTIFICIAL (result) && flag_weak)
23732 comdat_linkage (result);
23733 /* For WIN32 we also want to put explicit instantiations in
23734 linkonce sections. */
23735 else if (TREE_PUBLIC (result))
23736 maybe_make_one_only (result);
23737 if (TREE_CODE (result) == FUNCTION_DECL
23738 && DECL_TEMPLATE_INSTANTIATED (result))
23739 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23740 since start_preparsed_function wouldn't have if we had an earlier
23741 extern explicit instantiation. */
23742 DECL_EXTERNAL (result) = 0;
23743 }
23744
23745 /* If EXTERN_P, then this function will not be emitted -- unless
23746 followed by an explicit instantiation, at which point its linkage
23747 will be adjusted. If !EXTERN_P, then this function will be
23748 emitted here. In neither circumstance do we want
23749 import_export_decl to adjust the linkage. */
23750 DECL_INTERFACE_KNOWN (result) = 1;
23751 }
23752
23753 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23754 important template arguments. If any are missing, we check whether
23755 they're important by using error_mark_node for substituting into any
23756 args that were used for partial ordering (the ones between ARGS and END)
23757 and seeing if it bubbles up. */
23758
23759 static bool
23760 check_undeduced_parms (tree targs, tree args, tree end)
23761 {
23762 bool found = false;
23763 int i;
23764 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23765 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23766 {
23767 found = true;
23768 TREE_VEC_ELT (targs, i) = error_mark_node;
23769 }
23770 if (found)
23771 {
23772 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23773 if (substed == error_mark_node)
23774 return true;
23775 }
23776 return false;
23777 }
23778
23779 /* Given two function templates PAT1 and PAT2, return:
23780
23781 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23782 -1 if PAT2 is more specialized than PAT1.
23783 0 if neither is more specialized.
23784
23785 LEN indicates the number of parameters we should consider
23786 (defaulted parameters should not be considered).
23787
23788 The 1998 std underspecified function template partial ordering, and
23789 DR214 addresses the issue. We take pairs of arguments, one from
23790 each of the templates, and deduce them against each other. One of
23791 the templates will be more specialized if all the *other*
23792 template's arguments deduce against its arguments and at least one
23793 of its arguments *does* *not* deduce against the other template's
23794 corresponding argument. Deduction is done as for class templates.
23795 The arguments used in deduction have reference and top level cv
23796 qualifiers removed. Iff both arguments were originally reference
23797 types *and* deduction succeeds in both directions, an lvalue reference
23798 wins against an rvalue reference and otherwise the template
23799 with the more cv-qualified argument wins for that pairing (if
23800 neither is more cv-qualified, they both are equal). Unlike regular
23801 deduction, after all the arguments have been deduced in this way,
23802 we do *not* verify the deduced template argument values can be
23803 substituted into non-deduced contexts.
23804
23805 The logic can be a bit confusing here, because we look at deduce1 and
23806 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23807 can find template arguments for pat1 to make arg1 look like arg2, that
23808 means that arg2 is at least as specialized as arg1. */
23809
23810 int
23811 more_specialized_fn (tree pat1, tree pat2, int len)
23812 {
23813 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23814 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23815 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23816 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23817 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23818 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23819 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23820 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23821 tree origs1, origs2;
23822 bool lose1 = false;
23823 bool lose2 = false;
23824
23825 /* Remove the this parameter from non-static member functions. If
23826 one is a non-static member function and the other is not a static
23827 member function, remove the first parameter from that function
23828 also. This situation occurs for operator functions where we
23829 locate both a member function (with this pointer) and non-member
23830 operator (with explicit first operand). */
23831 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23832 {
23833 len--; /* LEN is the number of significant arguments for DECL1 */
23834 args1 = TREE_CHAIN (args1);
23835 if (!DECL_STATIC_FUNCTION_P (decl2))
23836 args2 = TREE_CHAIN (args2);
23837 }
23838 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23839 {
23840 args2 = TREE_CHAIN (args2);
23841 if (!DECL_STATIC_FUNCTION_P (decl1))
23842 {
23843 len--;
23844 args1 = TREE_CHAIN (args1);
23845 }
23846 }
23847
23848 /* If only one is a conversion operator, they are unordered. */
23849 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23850 return 0;
23851
23852 /* Consider the return type for a conversion function */
23853 if (DECL_CONV_FN_P (decl1))
23854 {
23855 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23856 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23857 len++;
23858 }
23859
23860 processing_template_decl++;
23861
23862 origs1 = args1;
23863 origs2 = args2;
23864
23865 while (len--
23866 /* Stop when an ellipsis is seen. */
23867 && args1 != NULL_TREE && args2 != NULL_TREE)
23868 {
23869 tree arg1 = TREE_VALUE (args1);
23870 tree arg2 = TREE_VALUE (args2);
23871 int deduce1, deduce2;
23872 int quals1 = -1;
23873 int quals2 = -1;
23874 int ref1 = 0;
23875 int ref2 = 0;
23876
23877 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23878 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23879 {
23880 /* When both arguments are pack expansions, we need only
23881 unify the patterns themselves. */
23882 arg1 = PACK_EXPANSION_PATTERN (arg1);
23883 arg2 = PACK_EXPANSION_PATTERN (arg2);
23884
23885 /* This is the last comparison we need to do. */
23886 len = 0;
23887 }
23888
23889 if (TYPE_REF_P (arg1))
23890 {
23891 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23892 arg1 = TREE_TYPE (arg1);
23893 quals1 = cp_type_quals (arg1);
23894 }
23895
23896 if (TYPE_REF_P (arg2))
23897 {
23898 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23899 arg2 = TREE_TYPE (arg2);
23900 quals2 = cp_type_quals (arg2);
23901 }
23902
23903 arg1 = TYPE_MAIN_VARIANT (arg1);
23904 arg2 = TYPE_MAIN_VARIANT (arg2);
23905
23906 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23907 {
23908 int i, len2 = remaining_arguments (args2);
23909 tree parmvec = make_tree_vec (1);
23910 tree argvec = make_tree_vec (len2);
23911 tree ta = args2;
23912
23913 /* Setup the parameter vector, which contains only ARG1. */
23914 TREE_VEC_ELT (parmvec, 0) = arg1;
23915
23916 /* Setup the argument vector, which contains the remaining
23917 arguments. */
23918 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23919 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23920
23921 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23922 argvec, DEDUCE_EXACT,
23923 /*subr=*/true, /*explain_p=*/false)
23924 == 0);
23925
23926 /* We cannot deduce in the other direction, because ARG1 is
23927 a pack expansion but ARG2 is not. */
23928 deduce2 = 0;
23929 }
23930 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23931 {
23932 int i, len1 = remaining_arguments (args1);
23933 tree parmvec = make_tree_vec (1);
23934 tree argvec = make_tree_vec (len1);
23935 tree ta = args1;
23936
23937 /* Setup the parameter vector, which contains only ARG1. */
23938 TREE_VEC_ELT (parmvec, 0) = arg2;
23939
23940 /* Setup the argument vector, which contains the remaining
23941 arguments. */
23942 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23943 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23944
23945 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23946 argvec, DEDUCE_EXACT,
23947 /*subr=*/true, /*explain_p=*/false)
23948 == 0);
23949
23950 /* We cannot deduce in the other direction, because ARG2 is
23951 a pack expansion but ARG1 is not.*/
23952 deduce1 = 0;
23953 }
23954
23955 else
23956 {
23957 /* The normal case, where neither argument is a pack
23958 expansion. */
23959 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23960 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23961 == 0);
23962 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23963 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23964 == 0);
23965 }
23966
23967 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23968 arg2, then arg2 is not as specialized as arg1. */
23969 if (!deduce1)
23970 lose2 = true;
23971 if (!deduce2)
23972 lose1 = true;
23973
23974 /* "If, for a given type, deduction succeeds in both directions
23975 (i.e., the types are identical after the transformations above)
23976 and both P and A were reference types (before being replaced with
23977 the type referred to above):
23978 - if the type from the argument template was an lvalue reference and
23979 the type from the parameter template was not, the argument type is
23980 considered to be more specialized than the other; otherwise,
23981 - if the type from the argument template is more cv-qualified
23982 than the type from the parameter template (as described above),
23983 the argument type is considered to be more specialized than the other;
23984 otherwise,
23985 - neither type is more specialized than the other." */
23986
23987 if (deduce1 && deduce2)
23988 {
23989 if (ref1 && ref2 && ref1 != ref2)
23990 {
23991 if (ref1 > ref2)
23992 lose1 = true;
23993 else
23994 lose2 = true;
23995 }
23996 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23997 {
23998 if ((quals1 & quals2) == quals2)
23999 lose2 = true;
24000 if ((quals1 & quals2) == quals1)
24001 lose1 = true;
24002 }
24003 }
24004
24005 if (lose1 && lose2)
24006 /* We've failed to deduce something in either direction.
24007 These must be unordered. */
24008 break;
24009
24010 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24011 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24012 /* We have already processed all of the arguments in our
24013 handing of the pack expansion type. */
24014 len = 0;
24015
24016 args1 = TREE_CHAIN (args1);
24017 args2 = TREE_CHAIN (args2);
24018 }
24019
24020 /* "In most cases, all template parameters must have values in order for
24021 deduction to succeed, but for partial ordering purposes a template
24022 parameter may remain without a value provided it is not used in the
24023 types being used for partial ordering."
24024
24025 Thus, if we are missing any of the targs1 we need to substitute into
24026 origs1, then pat2 is not as specialized as pat1. This can happen when
24027 there is a nondeduced context. */
24028 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24029 lose2 = true;
24030 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24031 lose1 = true;
24032
24033 processing_template_decl--;
24034
24035 /* If both deductions succeed, the partial ordering selects the more
24036 constrained template. */
24037 if (!lose1 && !lose2)
24038 {
24039 int winner = more_constrained (decl1, decl2);
24040 if (winner > 0)
24041 lose2 = true;
24042 else if (winner < 0)
24043 lose1 = true;
24044 }
24045
24046 /* All things being equal, if the next argument is a pack expansion
24047 for one function but not for the other, prefer the
24048 non-variadic function. FIXME this is bogus; see c++/41958. */
24049 if (lose1 == lose2
24050 && args1 && TREE_VALUE (args1)
24051 && args2 && TREE_VALUE (args2))
24052 {
24053 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24054 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24055 }
24056
24057 if (lose1 == lose2)
24058 return 0;
24059 else if (!lose1)
24060 return 1;
24061 else
24062 return -1;
24063 }
24064
24065 /* Determine which of two partial specializations of TMPL is more
24066 specialized.
24067
24068 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24069 to the first partial specialization. The TREE_PURPOSE is the
24070 innermost set of template parameters for the partial
24071 specialization. PAT2 is similar, but for the second template.
24072
24073 Return 1 if the first partial specialization is more specialized;
24074 -1 if the second is more specialized; 0 if neither is more
24075 specialized.
24076
24077 See [temp.class.order] for information about determining which of
24078 two templates is more specialized. */
24079
24080 static int
24081 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24082 {
24083 tree targs;
24084 int winner = 0;
24085 bool any_deductions = false;
24086
24087 tree tmpl1 = TREE_VALUE (pat1);
24088 tree tmpl2 = TREE_VALUE (pat2);
24089 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24090 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24091
24092 /* Just like what happens for functions, if we are ordering between
24093 different template specializations, we may encounter dependent
24094 types in the arguments, and we need our dependency check functions
24095 to behave correctly. */
24096 ++processing_template_decl;
24097 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24098 if (targs)
24099 {
24100 --winner;
24101 any_deductions = true;
24102 }
24103
24104 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24105 if (targs)
24106 {
24107 ++winner;
24108 any_deductions = true;
24109 }
24110 --processing_template_decl;
24111
24112 /* If both deductions succeed, the partial ordering selects the more
24113 constrained template. */
24114 if (!winner && any_deductions)
24115 winner = more_constrained (tmpl1, tmpl2);
24116
24117 /* In the case of a tie where at least one of the templates
24118 has a parameter pack at the end, the template with the most
24119 non-packed parameters wins. */
24120 if (winner == 0
24121 && any_deductions
24122 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24123 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24124 {
24125 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24126 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24127 int len1 = TREE_VEC_LENGTH (args1);
24128 int len2 = TREE_VEC_LENGTH (args2);
24129
24130 /* We don't count the pack expansion at the end. */
24131 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24132 --len1;
24133 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24134 --len2;
24135
24136 if (len1 > len2)
24137 return 1;
24138 else if (len1 < len2)
24139 return -1;
24140 }
24141
24142 return winner;
24143 }
24144
24145 /* Return the template arguments that will produce the function signature
24146 DECL from the function template FN, with the explicit template
24147 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24148 also match. Return NULL_TREE if no satisfactory arguments could be
24149 found. */
24150
24151 static tree
24152 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24153 {
24154 int ntparms = DECL_NTPARMS (fn);
24155 tree targs = make_tree_vec (ntparms);
24156 tree decl_type = TREE_TYPE (decl);
24157 tree decl_arg_types;
24158 tree *args;
24159 unsigned int nargs, ix;
24160 tree arg;
24161
24162 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24163
24164 /* Never do unification on the 'this' parameter. */
24165 decl_arg_types = skip_artificial_parms_for (decl,
24166 TYPE_ARG_TYPES (decl_type));
24167
24168 nargs = list_length (decl_arg_types);
24169 args = XALLOCAVEC (tree, nargs);
24170 for (arg = decl_arg_types, ix = 0;
24171 arg != NULL_TREE && arg != void_list_node;
24172 arg = TREE_CHAIN (arg), ++ix)
24173 args[ix] = TREE_VALUE (arg);
24174
24175 if (fn_type_unification (fn, explicit_args, targs,
24176 args, ix,
24177 (check_rettype || DECL_CONV_FN_P (fn)
24178 ? TREE_TYPE (decl_type) : NULL_TREE),
24179 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24180 /*explain_p=*/false,
24181 /*decltype*/false)
24182 == error_mark_node)
24183 return NULL_TREE;
24184
24185 return targs;
24186 }
24187
24188 /* Return the innermost template arguments that, when applied to a partial
24189 specialization SPEC_TMPL of TMPL, yield the ARGS.
24190
24191 For example, suppose we have:
24192
24193 template <class T, class U> struct S {};
24194 template <class T> struct S<T*, int> {};
24195
24196 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24197 partial specialization and the ARGS will be {double*, int}. The resulting
24198 vector will be {double}, indicating that `T' is bound to `double'. */
24199
24200 static tree
24201 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24202 {
24203 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24204 tree spec_args
24205 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24206 int i, ntparms = TREE_VEC_LENGTH (tparms);
24207 tree deduced_args;
24208 tree innermost_deduced_args;
24209
24210 innermost_deduced_args = make_tree_vec (ntparms);
24211 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24212 {
24213 deduced_args = copy_node (args);
24214 SET_TMPL_ARGS_LEVEL (deduced_args,
24215 TMPL_ARGS_DEPTH (deduced_args),
24216 innermost_deduced_args);
24217 }
24218 else
24219 deduced_args = innermost_deduced_args;
24220
24221 bool tried_array_deduction = (cxx_dialect < cxx17);
24222 again:
24223 if (unify (tparms, deduced_args,
24224 INNERMOST_TEMPLATE_ARGS (spec_args),
24225 INNERMOST_TEMPLATE_ARGS (args),
24226 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24227 return NULL_TREE;
24228
24229 for (i = 0; i < ntparms; ++i)
24230 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24231 {
24232 if (!tried_array_deduction)
24233 {
24234 try_array_deduction (tparms, innermost_deduced_args,
24235 INNERMOST_TEMPLATE_ARGS (spec_args));
24236 tried_array_deduction = true;
24237 if (TREE_VEC_ELT (innermost_deduced_args, i))
24238 goto again;
24239 }
24240 return NULL_TREE;
24241 }
24242
24243 if (!push_tinst_level (spec_tmpl, deduced_args))
24244 {
24245 excessive_deduction_depth = true;
24246 return NULL_TREE;
24247 }
24248
24249 /* Verify that nondeduced template arguments agree with the type
24250 obtained from argument deduction.
24251
24252 For example:
24253
24254 struct A { typedef int X; };
24255 template <class T, class U> struct C {};
24256 template <class T> struct C<T, typename T::X> {};
24257
24258 Then with the instantiation `C<A, int>', we can deduce that
24259 `T' is `A' but unify () does not check whether `typename T::X'
24260 is `int'. */
24261 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24262
24263 if (spec_args != error_mark_node)
24264 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24265 INNERMOST_TEMPLATE_ARGS (spec_args),
24266 tmpl, tf_none, false, false);
24267
24268 pop_tinst_level ();
24269
24270 if (spec_args == error_mark_node
24271 /* We only need to check the innermost arguments; the other
24272 arguments will always agree. */
24273 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24274 INNERMOST_TEMPLATE_ARGS (args)))
24275 return NULL_TREE;
24276
24277 /* Now that we have bindings for all of the template arguments,
24278 ensure that the arguments deduced for the template template
24279 parameters have compatible template parameter lists. See the use
24280 of template_template_parm_bindings_ok_p in fn_type_unification
24281 for more information. */
24282 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24283 return NULL_TREE;
24284
24285 return deduced_args;
24286 }
24287
24288 // Compare two function templates T1 and T2 by deducing bindings
24289 // from one against the other. If both deductions succeed, compare
24290 // constraints to see which is more constrained.
24291 static int
24292 more_specialized_inst (tree t1, tree t2)
24293 {
24294 int fate = 0;
24295 int count = 0;
24296
24297 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24298 {
24299 --fate;
24300 ++count;
24301 }
24302
24303 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24304 {
24305 ++fate;
24306 ++count;
24307 }
24308
24309 // If both deductions succeed, then one may be more constrained.
24310 if (count == 2 && fate == 0)
24311 fate = more_constrained (t1, t2);
24312
24313 return fate;
24314 }
24315
24316 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24317 Return the TREE_LIST node with the most specialized template, if
24318 any. If there is no most specialized template, the error_mark_node
24319 is returned.
24320
24321 Note that this function does not look at, or modify, the
24322 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24323 returned is one of the elements of INSTANTIATIONS, callers may
24324 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24325 and retrieve it from the value returned. */
24326
24327 tree
24328 most_specialized_instantiation (tree templates)
24329 {
24330 tree fn, champ;
24331
24332 ++processing_template_decl;
24333
24334 champ = templates;
24335 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24336 {
24337 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24338 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24339 if (fate == -1)
24340 champ = fn;
24341 else if (!fate)
24342 {
24343 /* Equally specialized, move to next function. If there
24344 is no next function, nothing's most specialized. */
24345 fn = TREE_CHAIN (fn);
24346 champ = fn;
24347 if (!fn)
24348 break;
24349 }
24350 }
24351
24352 if (champ)
24353 /* Now verify that champ is better than everything earlier in the
24354 instantiation list. */
24355 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24356 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24357 {
24358 champ = NULL_TREE;
24359 break;
24360 }
24361 }
24362
24363 processing_template_decl--;
24364
24365 if (!champ)
24366 return error_mark_node;
24367
24368 return champ;
24369 }
24370
24371 /* If DECL is a specialization of some template, return the most
24372 general such template. Otherwise, returns NULL_TREE.
24373
24374 For example, given:
24375
24376 template <class T> struct S { template <class U> void f(U); };
24377
24378 if TMPL is `template <class U> void S<int>::f(U)' this will return
24379 the full template. This function will not trace past partial
24380 specializations, however. For example, given in addition:
24381
24382 template <class T> struct S<T*> { template <class U> void f(U); };
24383
24384 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24385 `template <class T> template <class U> S<T*>::f(U)'. */
24386
24387 tree
24388 most_general_template (tree decl)
24389 {
24390 if (TREE_CODE (decl) != TEMPLATE_DECL)
24391 {
24392 if (tree tinfo = get_template_info (decl))
24393 decl = TI_TEMPLATE (tinfo);
24394 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24395 template friend, or a FIELD_DECL for a capture pack. */
24396 if (TREE_CODE (decl) != TEMPLATE_DECL)
24397 return NULL_TREE;
24398 }
24399
24400 /* Look for more and more general templates. */
24401 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24402 {
24403 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24404 (See cp-tree.h for details.) */
24405 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24406 break;
24407
24408 if (CLASS_TYPE_P (TREE_TYPE (decl))
24409 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24410 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24411 break;
24412
24413 /* Stop if we run into an explicitly specialized class template. */
24414 if (!DECL_NAMESPACE_SCOPE_P (decl)
24415 && DECL_CONTEXT (decl)
24416 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24417 break;
24418
24419 decl = DECL_TI_TEMPLATE (decl);
24420 }
24421
24422 return decl;
24423 }
24424
24425 /* Return the most specialized of the template partial specializations
24426 which can produce TARGET, a specialization of some class or variable
24427 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24428 a TEMPLATE_DECL node corresponding to the partial specialization, while
24429 the TREE_PURPOSE is the set of template arguments that must be
24430 substituted into the template pattern in order to generate TARGET.
24431
24432 If the choice of partial specialization is ambiguous, a diagnostic
24433 is issued, and the error_mark_node is returned. If there are no
24434 partial specializations matching TARGET, then NULL_TREE is
24435 returned, indicating that the primary template should be used. */
24436
24437 tree
24438 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24439 {
24440 tree list = NULL_TREE;
24441 tree t;
24442 tree champ;
24443 int fate;
24444 bool ambiguous_p;
24445 tree outer_args = NULL_TREE;
24446 tree tmpl, args;
24447
24448 if (TYPE_P (target))
24449 {
24450 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24451 tmpl = TI_TEMPLATE (tinfo);
24452 args = TI_ARGS (tinfo);
24453 }
24454 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24455 {
24456 tmpl = TREE_OPERAND (target, 0);
24457 args = TREE_OPERAND (target, 1);
24458 }
24459 else if (VAR_P (target))
24460 {
24461 tree tinfo = DECL_TEMPLATE_INFO (target);
24462 tmpl = TI_TEMPLATE (tinfo);
24463 args = TI_ARGS (tinfo);
24464 }
24465 else
24466 gcc_unreachable ();
24467
24468 tree main_tmpl = most_general_template (tmpl);
24469
24470 /* For determining which partial specialization to use, only the
24471 innermost args are interesting. */
24472 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24473 {
24474 outer_args = strip_innermost_template_args (args, 1);
24475 args = INNERMOST_TEMPLATE_ARGS (args);
24476 }
24477
24478 /* The caller hasn't called push_to_top_level yet, but we need
24479 get_partial_spec_bindings to be done in non-template context so that we'll
24480 fully resolve everything. */
24481 processing_template_decl_sentinel ptds;
24482
24483 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24484 {
24485 tree spec_args;
24486 tree spec_tmpl = TREE_VALUE (t);
24487
24488 if (outer_args)
24489 {
24490 /* Substitute in the template args from the enclosing class. */
24491 ++processing_template_decl;
24492 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24493 --processing_template_decl;
24494 }
24495
24496 if (spec_tmpl == error_mark_node)
24497 return error_mark_node;
24498
24499 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24500 if (spec_args)
24501 {
24502 if (outer_args)
24503 spec_args = add_to_template_args (outer_args, spec_args);
24504
24505 /* Keep the candidate only if the constraints are satisfied,
24506 or if we're not compiling with concepts. */
24507 if (!flag_concepts
24508 || constraints_satisfied_p (spec_tmpl, spec_args))
24509 {
24510 list = tree_cons (spec_args, TREE_VALUE (t), list);
24511 TREE_TYPE (list) = TREE_TYPE (t);
24512 }
24513 }
24514 }
24515
24516 if (! list)
24517 return NULL_TREE;
24518
24519 ambiguous_p = false;
24520 t = list;
24521 champ = t;
24522 t = TREE_CHAIN (t);
24523 for (; t; t = TREE_CHAIN (t))
24524 {
24525 fate = more_specialized_partial_spec (tmpl, champ, t);
24526 if (fate == 1)
24527 ;
24528 else
24529 {
24530 if (fate == 0)
24531 {
24532 t = TREE_CHAIN (t);
24533 if (! t)
24534 {
24535 ambiguous_p = true;
24536 break;
24537 }
24538 }
24539 champ = t;
24540 }
24541 }
24542
24543 if (!ambiguous_p)
24544 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24545 {
24546 fate = more_specialized_partial_spec (tmpl, champ, t);
24547 if (fate != 1)
24548 {
24549 ambiguous_p = true;
24550 break;
24551 }
24552 }
24553
24554 if (ambiguous_p)
24555 {
24556 const char *str;
24557 char *spaces = NULL;
24558 if (!(complain & tf_error))
24559 return error_mark_node;
24560 if (TYPE_P (target))
24561 error ("ambiguous template instantiation for %q#T", target);
24562 else
24563 error ("ambiguous template instantiation for %q#D", target);
24564 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24565 for (t = list; t; t = TREE_CHAIN (t))
24566 {
24567 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24568 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24569 "%s %#qS", spaces ? spaces : str, subst);
24570 spaces = spaces ? spaces : get_spaces (str);
24571 }
24572 free (spaces);
24573 return error_mark_node;
24574 }
24575
24576 return champ;
24577 }
24578
24579 /* Explicitly instantiate DECL. */
24580
24581 void
24582 do_decl_instantiation (tree decl, tree storage)
24583 {
24584 tree result = NULL_TREE;
24585 int extern_p = 0;
24586
24587 if (!decl || decl == error_mark_node)
24588 /* An error occurred, for which grokdeclarator has already issued
24589 an appropriate message. */
24590 return;
24591 else if (! DECL_LANG_SPECIFIC (decl))
24592 {
24593 error ("explicit instantiation of non-template %q#D", decl);
24594 return;
24595 }
24596 else if (DECL_DECLARED_CONCEPT_P (decl))
24597 {
24598 if (VAR_P (decl))
24599 error ("explicit instantiation of variable concept %q#D", decl);
24600 else
24601 error ("explicit instantiation of function concept %q#D", decl);
24602 return;
24603 }
24604
24605 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24606 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24607
24608 if (VAR_P (decl) && !var_templ)
24609 {
24610 /* There is an asymmetry here in the way VAR_DECLs and
24611 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24612 the latter, the DECL we get back will be marked as a
24613 template instantiation, and the appropriate
24614 DECL_TEMPLATE_INFO will be set up. This does not happen for
24615 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24616 should handle VAR_DECLs as it currently handles
24617 FUNCTION_DECLs. */
24618 if (!DECL_CLASS_SCOPE_P (decl))
24619 {
24620 error ("%qD is not a static data member of a class template", decl);
24621 return;
24622 }
24623 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24624 if (!result || !VAR_P (result))
24625 {
24626 error ("no matching template for %qD found", decl);
24627 return;
24628 }
24629 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24630 {
24631 error ("type %qT for explicit instantiation %qD does not match "
24632 "declared type %qT", TREE_TYPE (result), decl,
24633 TREE_TYPE (decl));
24634 return;
24635 }
24636 }
24637 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24638 {
24639 error ("explicit instantiation of %q#D", decl);
24640 return;
24641 }
24642 else
24643 result = decl;
24644
24645 /* Check for various error cases. Note that if the explicit
24646 instantiation is valid the RESULT will currently be marked as an
24647 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24648 until we get here. */
24649
24650 if (DECL_TEMPLATE_SPECIALIZATION (result))
24651 {
24652 /* DR 259 [temp.spec].
24653
24654 Both an explicit instantiation and a declaration of an explicit
24655 specialization shall not appear in a program unless the explicit
24656 instantiation follows a declaration of the explicit specialization.
24657
24658 For a given set of template parameters, if an explicit
24659 instantiation of a template appears after a declaration of an
24660 explicit specialization for that template, the explicit
24661 instantiation has no effect. */
24662 return;
24663 }
24664 else if (DECL_EXPLICIT_INSTANTIATION (result))
24665 {
24666 /* [temp.spec]
24667
24668 No program shall explicitly instantiate any template more
24669 than once.
24670
24671 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24672 the first instantiation was `extern' and the second is not,
24673 and EXTERN_P for the opposite case. */
24674 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24675 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24676 /* If an "extern" explicit instantiation follows an ordinary
24677 explicit instantiation, the template is instantiated. */
24678 if (extern_p)
24679 return;
24680 }
24681 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24682 {
24683 error ("no matching template for %qD found", result);
24684 return;
24685 }
24686 else if (!DECL_TEMPLATE_INFO (result))
24687 {
24688 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24689 return;
24690 }
24691
24692 if (storage == NULL_TREE)
24693 ;
24694 else if (storage == ridpointers[(int) RID_EXTERN])
24695 {
24696 if (cxx_dialect == cxx98)
24697 pedwarn (input_location, OPT_Wpedantic,
24698 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24699 "instantiations");
24700 extern_p = 1;
24701 }
24702 else
24703 error ("storage class %qD applied to template instantiation", storage);
24704
24705 check_explicit_instantiation_namespace (result);
24706 mark_decl_instantiated (result, extern_p);
24707 if (! extern_p)
24708 instantiate_decl (result, /*defer_ok=*/true,
24709 /*expl_inst_class_mem_p=*/false);
24710 }
24711
24712 static void
24713 mark_class_instantiated (tree t, int extern_p)
24714 {
24715 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24716 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24717 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24718 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24719 if (! extern_p)
24720 {
24721 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24722 rest_of_type_compilation (t, 1);
24723 }
24724 }
24725
24726 /* Called from do_type_instantiation through binding_table_foreach to
24727 do recursive instantiation for the type bound in ENTRY. */
24728 static void
24729 bt_instantiate_type_proc (binding_entry entry, void *data)
24730 {
24731 tree storage = *(tree *) data;
24732
24733 if (MAYBE_CLASS_TYPE_P (entry->type)
24734 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24735 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24736 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24737 }
24738
24739 /* Perform an explicit instantiation of template class T. STORAGE, if
24740 non-null, is the RID for extern, inline or static. COMPLAIN is
24741 nonzero if this is called from the parser, zero if called recursively,
24742 since the standard is unclear (as detailed below). */
24743
24744 void
24745 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24746 {
24747 int extern_p = 0;
24748 int nomem_p = 0;
24749 int static_p = 0;
24750 int previous_instantiation_extern_p = 0;
24751
24752 if (TREE_CODE (t) == TYPE_DECL)
24753 t = TREE_TYPE (t);
24754
24755 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24756 {
24757 tree tmpl =
24758 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24759 if (tmpl)
24760 error ("explicit instantiation of non-class template %qD", tmpl);
24761 else
24762 error ("explicit instantiation of non-template type %qT", t);
24763 return;
24764 }
24765
24766 complete_type (t);
24767
24768 if (!COMPLETE_TYPE_P (t))
24769 {
24770 if (complain & tf_error)
24771 error ("explicit instantiation of %q#T before definition of template",
24772 t);
24773 return;
24774 }
24775
24776 if (storage != NULL_TREE)
24777 {
24778 if (storage == ridpointers[(int) RID_EXTERN])
24779 {
24780 if (cxx_dialect == cxx98)
24781 pedwarn (input_location, OPT_Wpedantic,
24782 "ISO C++ 1998 forbids the use of %<extern%> on "
24783 "explicit instantiations");
24784 }
24785 else
24786 pedwarn (input_location, OPT_Wpedantic,
24787 "ISO C++ forbids the use of %qE"
24788 " on explicit instantiations", storage);
24789
24790 if (storage == ridpointers[(int) RID_INLINE])
24791 nomem_p = 1;
24792 else if (storage == ridpointers[(int) RID_EXTERN])
24793 extern_p = 1;
24794 else if (storage == ridpointers[(int) RID_STATIC])
24795 static_p = 1;
24796 else
24797 {
24798 error ("storage class %qD applied to template instantiation",
24799 storage);
24800 extern_p = 0;
24801 }
24802 }
24803
24804 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24805 {
24806 /* DR 259 [temp.spec].
24807
24808 Both an explicit instantiation and a declaration of an explicit
24809 specialization shall not appear in a program unless the explicit
24810 instantiation follows a declaration of the explicit specialization.
24811
24812 For a given set of template parameters, if an explicit
24813 instantiation of a template appears after a declaration of an
24814 explicit specialization for that template, the explicit
24815 instantiation has no effect. */
24816 return;
24817 }
24818 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24819 {
24820 /* [temp.spec]
24821
24822 No program shall explicitly instantiate any template more
24823 than once.
24824
24825 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24826 instantiation was `extern'. If EXTERN_P then the second is.
24827 These cases are OK. */
24828 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24829
24830 if (!previous_instantiation_extern_p && !extern_p
24831 && (complain & tf_error))
24832 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24833
24834 /* If we've already instantiated the template, just return now. */
24835 if (!CLASSTYPE_INTERFACE_ONLY (t))
24836 return;
24837 }
24838
24839 check_explicit_instantiation_namespace (TYPE_NAME (t));
24840 mark_class_instantiated (t, extern_p);
24841
24842 if (nomem_p)
24843 return;
24844
24845 /* In contrast to implicit instantiation, where only the
24846 declarations, and not the definitions, of members are
24847 instantiated, we have here:
24848
24849 [temp.explicit]
24850
24851 The explicit instantiation of a class template specialization
24852 implies the instantiation of all of its members not
24853 previously explicitly specialized in the translation unit
24854 containing the explicit instantiation.
24855
24856 Of course, we can't instantiate member template classes, since we
24857 don't have any arguments for them. Note that the standard is
24858 unclear on whether the instantiation of the members are
24859 *explicit* instantiations or not. However, the most natural
24860 interpretation is that it should be an explicit
24861 instantiation. */
24862 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24863 if ((VAR_P (fld)
24864 || (TREE_CODE (fld) == FUNCTION_DECL
24865 && !static_p
24866 && user_provided_p (fld)))
24867 && DECL_TEMPLATE_INSTANTIATION (fld))
24868 {
24869 mark_decl_instantiated (fld, extern_p);
24870 if (! extern_p)
24871 instantiate_decl (fld, /*defer_ok=*/true,
24872 /*expl_inst_class_mem_p=*/true);
24873 }
24874
24875 if (CLASSTYPE_NESTED_UTDS (t))
24876 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24877 bt_instantiate_type_proc, &storage);
24878 }
24879
24880 /* Given a function DECL, which is a specialization of TMPL, modify
24881 DECL to be a re-instantiation of TMPL with the same template
24882 arguments. TMPL should be the template into which tsubst'ing
24883 should occur for DECL, not the most general template.
24884
24885 One reason for doing this is a scenario like this:
24886
24887 template <class T>
24888 void f(const T&, int i);
24889
24890 void g() { f(3, 7); }
24891
24892 template <class T>
24893 void f(const T& t, const int i) { }
24894
24895 Note that when the template is first instantiated, with
24896 instantiate_template, the resulting DECL will have no name for the
24897 first parameter, and the wrong type for the second. So, when we go
24898 to instantiate the DECL, we regenerate it. */
24899
24900 static void
24901 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24902 {
24903 /* The arguments used to instantiate DECL, from the most general
24904 template. */
24905 tree code_pattern;
24906
24907 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24908
24909 /* Make sure that we can see identifiers, and compute access
24910 correctly. */
24911 push_access_scope (decl);
24912
24913 if (TREE_CODE (decl) == FUNCTION_DECL)
24914 {
24915 tree decl_parm;
24916 tree pattern_parm;
24917 tree specs;
24918 int args_depth;
24919 int parms_depth;
24920
24921 args_depth = TMPL_ARGS_DEPTH (args);
24922 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24923 if (args_depth > parms_depth)
24924 args = get_innermost_template_args (args, parms_depth);
24925
24926 /* Instantiate a dynamic exception-specification. noexcept will be
24927 handled below. */
24928 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24929 if (TREE_VALUE (raises))
24930 {
24931 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24932 args, tf_error, NULL_TREE,
24933 /*defer_ok*/false);
24934 if (specs && specs != error_mark_node)
24935 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24936 specs);
24937 }
24938
24939 /* Merge parameter declarations. */
24940 decl_parm = skip_artificial_parms_for (decl,
24941 DECL_ARGUMENTS (decl));
24942 pattern_parm
24943 = skip_artificial_parms_for (code_pattern,
24944 DECL_ARGUMENTS (code_pattern));
24945 while (decl_parm && !DECL_PACK_P (pattern_parm))
24946 {
24947 tree parm_type;
24948 tree attributes;
24949
24950 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24951 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24952 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24953 NULL_TREE);
24954 parm_type = type_decays_to (parm_type);
24955 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24956 TREE_TYPE (decl_parm) = parm_type;
24957 attributes = DECL_ATTRIBUTES (pattern_parm);
24958 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24959 {
24960 DECL_ATTRIBUTES (decl_parm) = attributes;
24961 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24962 }
24963 decl_parm = DECL_CHAIN (decl_parm);
24964 pattern_parm = DECL_CHAIN (pattern_parm);
24965 }
24966 /* Merge any parameters that match with the function parameter
24967 pack. */
24968 if (pattern_parm && DECL_PACK_P (pattern_parm))
24969 {
24970 int i, len;
24971 tree expanded_types;
24972 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24973 the parameters in this function parameter pack. */
24974 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24975 args, tf_error, NULL_TREE);
24976 len = TREE_VEC_LENGTH (expanded_types);
24977 for (i = 0; i < len; i++)
24978 {
24979 tree parm_type;
24980 tree attributes;
24981
24982 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24983 /* Rename the parameter to include the index. */
24984 DECL_NAME (decl_parm) =
24985 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24986 parm_type = TREE_VEC_ELT (expanded_types, i);
24987 parm_type = type_decays_to (parm_type);
24988 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24989 TREE_TYPE (decl_parm) = parm_type;
24990 attributes = DECL_ATTRIBUTES (pattern_parm);
24991 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24992 {
24993 DECL_ATTRIBUTES (decl_parm) = attributes;
24994 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24995 }
24996 decl_parm = DECL_CHAIN (decl_parm);
24997 }
24998 }
24999 /* Merge additional specifiers from the CODE_PATTERN. */
25000 if (DECL_DECLARED_INLINE_P (code_pattern)
25001 && !DECL_DECLARED_INLINE_P (decl))
25002 DECL_DECLARED_INLINE_P (decl) = 1;
25003
25004 maybe_instantiate_noexcept (decl, tf_error);
25005 }
25006 else if (VAR_P (decl))
25007 {
25008 start_lambda_scope (decl);
25009 DECL_INITIAL (decl) =
25010 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25011 tf_error, DECL_TI_TEMPLATE (decl));
25012 finish_lambda_scope ();
25013 if (VAR_HAD_UNKNOWN_BOUND (decl))
25014 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25015 tf_error, DECL_TI_TEMPLATE (decl));
25016 }
25017 else
25018 gcc_unreachable ();
25019
25020 pop_access_scope (decl);
25021 }
25022
25023 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25024 substituted to get DECL. */
25025
25026 tree
25027 template_for_substitution (tree decl)
25028 {
25029 tree tmpl = DECL_TI_TEMPLATE (decl);
25030
25031 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25032 for the instantiation. This is not always the most general
25033 template. Consider, for example:
25034
25035 template <class T>
25036 struct S { template <class U> void f();
25037 template <> void f<int>(); };
25038
25039 and an instantiation of S<double>::f<int>. We want TD to be the
25040 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25041 while (/* An instantiation cannot have a definition, so we need a
25042 more general template. */
25043 DECL_TEMPLATE_INSTANTIATION (tmpl)
25044 /* We must also deal with friend templates. Given:
25045
25046 template <class T> struct S {
25047 template <class U> friend void f() {};
25048 };
25049
25050 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25051 so far as the language is concerned, but that's still
25052 where we get the pattern for the instantiation from. On
25053 other hand, if the definition comes outside the class, say:
25054
25055 template <class T> struct S {
25056 template <class U> friend void f();
25057 };
25058 template <class U> friend void f() {}
25059
25060 we don't need to look any further. That's what the check for
25061 DECL_INITIAL is for. */
25062 || (TREE_CODE (decl) == FUNCTION_DECL
25063 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25064 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25065 {
25066 /* The present template, TD, should not be a definition. If it
25067 were a definition, we should be using it! Note that we
25068 cannot restructure the loop to just keep going until we find
25069 a template with a definition, since that might go too far if
25070 a specialization was declared, but not defined. */
25071
25072 /* Fetch the more general template. */
25073 tmpl = DECL_TI_TEMPLATE (tmpl);
25074 }
25075
25076 return tmpl;
25077 }
25078
25079 /* Returns true if we need to instantiate this template instance even if we
25080 know we aren't going to emit it. */
25081
25082 bool
25083 always_instantiate_p (tree decl)
25084 {
25085 /* We always instantiate inline functions so that we can inline them. An
25086 explicit instantiation declaration prohibits implicit instantiation of
25087 non-inline functions. With high levels of optimization, we would
25088 normally inline non-inline functions -- but we're not allowed to do
25089 that for "extern template" functions. Therefore, we check
25090 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25091 return ((TREE_CODE (decl) == FUNCTION_DECL
25092 && (DECL_DECLARED_INLINE_P (decl)
25093 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25094 /* And we need to instantiate static data members so that
25095 their initializers are available in integral constant
25096 expressions. */
25097 || (VAR_P (decl)
25098 && decl_maybe_constant_var_p (decl)));
25099 }
25100
25101 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25102 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25103 error, true otherwise. */
25104
25105 bool
25106 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25107 {
25108 tree fntype, spec, noex;
25109
25110 /* Don't instantiate a noexcept-specification from template context. */
25111 if (processing_template_decl
25112 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25113 return true;
25114
25115 if (DECL_MAYBE_DELETED (fn))
25116 {
25117 if (fn == current_function_decl)
25118 /* We're in start_preparsed_function, keep going. */
25119 return true;
25120
25121 ++function_depth;
25122 synthesize_method (fn);
25123 --function_depth;
25124 return !DECL_MAYBE_DELETED (fn);
25125 }
25126
25127 fntype = TREE_TYPE (fn);
25128 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25129
25130 if (!spec || !TREE_PURPOSE (spec))
25131 return true;
25132
25133 noex = TREE_PURPOSE (spec);
25134 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25135 && TREE_CODE (noex) != DEFERRED_PARSE)
25136 return true;
25137
25138 tree orig_fn = NULL_TREE;
25139 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25140 its FUNCTION_DECL for the rest of this function -- push_access_scope
25141 doesn't accept TEMPLATE_DECLs. */
25142 if (DECL_FUNCTION_TEMPLATE_P (fn))
25143 {
25144 orig_fn = fn;
25145 fn = DECL_TEMPLATE_RESULT (fn);
25146 }
25147
25148 if (DECL_CLONED_FUNCTION_P (fn))
25149 {
25150 tree prime = DECL_CLONED_FUNCTION (fn);
25151 if (!maybe_instantiate_noexcept (prime, complain))
25152 return false;
25153 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25154 }
25155 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25156 {
25157 static hash_set<tree>* fns = new hash_set<tree>;
25158 bool added = false;
25159 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25160 {
25161 spec = get_defaulted_eh_spec (fn, complain);
25162 if (spec == error_mark_node)
25163 /* This might have failed because of an unparsed DMI, so
25164 let's try again later. */
25165 return false;
25166 }
25167 else if (!(added = !fns->add (fn)))
25168 {
25169 /* If hash_set::add returns true, the element was already there. */
25170 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25171 DECL_SOURCE_LOCATION (fn));
25172 error_at (loc,
25173 "exception specification of %qD depends on itself",
25174 fn);
25175 spec = noexcept_false_spec;
25176 }
25177 else if (push_tinst_level (fn))
25178 {
25179 push_to_top_level ();
25180 push_access_scope (fn);
25181 push_deferring_access_checks (dk_no_deferred);
25182 input_location = DECL_SOURCE_LOCATION (fn);
25183
25184 /* If needed, set current_class_ptr for the benefit of
25185 tsubst_copy/PARM_DECL. */
25186 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25187 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25188 {
25189 tree this_parm = DECL_ARGUMENTS (tdecl);
25190 current_class_ptr = NULL_TREE;
25191 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25192 current_class_ptr = this_parm;
25193 }
25194
25195 /* If this function is represented by a TEMPLATE_DECL, then
25196 the deferred noexcept-specification might still contain
25197 dependent types, even after substitution. And we need the
25198 dependency check functions to work in build_noexcept_spec. */
25199 if (orig_fn)
25200 ++processing_template_decl;
25201
25202 /* Do deferred instantiation of the noexcept-specifier. */
25203 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25204 DEFERRED_NOEXCEPT_ARGS (noex),
25205 tf_warning_or_error, fn,
25206 /*function_p=*/false,
25207 /*i_c_e_p=*/true);
25208
25209 /* Build up the noexcept-specification. */
25210 spec = build_noexcept_spec (noex, tf_warning_or_error);
25211
25212 if (orig_fn)
25213 --processing_template_decl;
25214
25215 pop_deferring_access_checks ();
25216 pop_access_scope (fn);
25217 pop_tinst_level ();
25218 pop_from_top_level ();
25219 }
25220 else
25221 spec = noexcept_false_spec;
25222
25223 if (added)
25224 fns->remove (fn);
25225 }
25226
25227 if (spec == error_mark_node)
25228 {
25229 /* This failed with a hard error, so let's go with false. */
25230 gcc_assert (seen_error ());
25231 spec = noexcept_false_spec;
25232 }
25233
25234 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25235 if (orig_fn)
25236 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25237
25238 return true;
25239 }
25240
25241 /* We're starting to process the function INST, an instantiation of PATTERN;
25242 add their parameters to local_specializations. */
25243
25244 static void
25245 register_parameter_specializations (tree pattern, tree inst)
25246 {
25247 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25248 tree spec_parm = DECL_ARGUMENTS (inst);
25249 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25250 {
25251 register_local_specialization (spec_parm, tmpl_parm);
25252 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25253 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25254 }
25255 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25256 {
25257 if (!DECL_PACK_P (tmpl_parm)
25258 || (spec_parm && DECL_PACK_P (spec_parm)))
25259 {
25260 register_local_specialization (spec_parm, tmpl_parm);
25261 spec_parm = DECL_CHAIN (spec_parm);
25262 }
25263 else
25264 {
25265 /* Register the (value) argument pack as a specialization of
25266 TMPL_PARM, then move on. */
25267 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25268 register_local_specialization (argpack, tmpl_parm);
25269 }
25270 }
25271 gcc_assert (!spec_parm);
25272 }
25273
25274 /* Produce the definition of D, a _DECL generated from a template. If
25275 DEFER_OK is true, then we don't have to actually do the
25276 instantiation now; we just have to do it sometime. Normally it is
25277 an error if this is an explicit instantiation but D is undefined.
25278 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25279 instantiated class template. */
25280
25281 tree
25282 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25283 {
25284 tree tmpl = DECL_TI_TEMPLATE (d);
25285 tree gen_args;
25286 tree args;
25287 tree td;
25288 tree code_pattern;
25289 tree spec;
25290 tree gen_tmpl;
25291 bool pattern_defined;
25292 location_t saved_loc = input_location;
25293 int saved_unevaluated_operand = cp_unevaluated_operand;
25294 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25295 bool external_p;
25296 bool deleted_p;
25297
25298 /* This function should only be used to instantiate templates for
25299 functions and static member variables. */
25300 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25301
25302 /* A concept is never instantiated. */
25303 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25304
25305 /* Variables are never deferred; if instantiation is required, they
25306 are instantiated right away. That allows for better code in the
25307 case that an expression refers to the value of the variable --
25308 if the variable has a constant value the referring expression can
25309 take advantage of that fact. */
25310 if (VAR_P (d))
25311 defer_ok = false;
25312
25313 /* Don't instantiate cloned functions. Instead, instantiate the
25314 functions they cloned. */
25315 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25316 d = DECL_CLONED_FUNCTION (d);
25317
25318 if (DECL_TEMPLATE_INSTANTIATED (d)
25319 || TREE_TYPE (d) == error_mark_node
25320 || (TREE_CODE (d) == FUNCTION_DECL
25321 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25322 || DECL_TEMPLATE_SPECIALIZATION (d))
25323 /* D has already been instantiated or explicitly specialized, so
25324 there's nothing for us to do here.
25325
25326 It might seem reasonable to check whether or not D is an explicit
25327 instantiation, and, if so, stop here. But when an explicit
25328 instantiation is deferred until the end of the compilation,
25329 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25330 the instantiation. */
25331 return d;
25332
25333 /* Check to see whether we know that this template will be
25334 instantiated in some other file, as with "extern template"
25335 extension. */
25336 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25337
25338 /* In general, we do not instantiate such templates. */
25339 if (external_p && !always_instantiate_p (d))
25340 return d;
25341
25342 gen_tmpl = most_general_template (tmpl);
25343 gen_args = DECL_TI_ARGS (d);
25344
25345 /* We should already have the extra args. */
25346 gcc_checking_assert (tmpl == gen_tmpl
25347 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25348 == TMPL_ARGS_DEPTH (gen_args)));
25349 /* And what's in the hash table should match D. */
25350 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25351 == d
25352 || spec == NULL_TREE);
25353
25354 /* This needs to happen before any tsubsting. */
25355 if (! push_tinst_level (d))
25356 return d;
25357
25358 timevar_push (TV_TEMPLATE_INST);
25359
25360 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25361 for the instantiation. */
25362 td = template_for_substitution (d);
25363 args = gen_args;
25364
25365 if (VAR_P (d))
25366 {
25367 /* Look up an explicit specialization, if any. */
25368 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25369 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25370 if (elt && elt != error_mark_node)
25371 {
25372 td = TREE_VALUE (elt);
25373 args = TREE_PURPOSE (elt);
25374 }
25375 }
25376
25377 code_pattern = DECL_TEMPLATE_RESULT (td);
25378
25379 /* We should never be trying to instantiate a member of a class
25380 template or partial specialization. */
25381 gcc_assert (d != code_pattern);
25382
25383 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25384 || DECL_TEMPLATE_SPECIALIZATION (td))
25385 /* In the case of a friend template whose definition is provided
25386 outside the class, we may have too many arguments. Drop the
25387 ones we don't need. The same is true for specializations. */
25388 args = get_innermost_template_args
25389 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25390
25391 if (TREE_CODE (d) == FUNCTION_DECL)
25392 {
25393 deleted_p = DECL_DELETED_FN (code_pattern);
25394 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25395 && DECL_INITIAL (code_pattern) != error_mark_node)
25396 || DECL_DEFAULTED_FN (code_pattern)
25397 || deleted_p);
25398 }
25399 else
25400 {
25401 deleted_p = false;
25402 if (DECL_CLASS_SCOPE_P (code_pattern))
25403 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25404 else
25405 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25406 }
25407
25408 /* We may be in the middle of deferred access check. Disable it now. */
25409 push_deferring_access_checks (dk_no_deferred);
25410
25411 /* Unless an explicit instantiation directive has already determined
25412 the linkage of D, remember that a definition is available for
25413 this entity. */
25414 if (pattern_defined
25415 && !DECL_INTERFACE_KNOWN (d)
25416 && !DECL_NOT_REALLY_EXTERN (d))
25417 mark_definable (d);
25418
25419 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25420 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25421 input_location = DECL_SOURCE_LOCATION (d);
25422
25423 /* If D is a member of an explicitly instantiated class template,
25424 and no definition is available, treat it like an implicit
25425 instantiation. */
25426 if (!pattern_defined && expl_inst_class_mem_p
25427 && DECL_EXPLICIT_INSTANTIATION (d))
25428 {
25429 /* Leave linkage flags alone on instantiations with anonymous
25430 visibility. */
25431 if (TREE_PUBLIC (d))
25432 {
25433 DECL_NOT_REALLY_EXTERN (d) = 0;
25434 DECL_INTERFACE_KNOWN (d) = 0;
25435 }
25436 SET_DECL_IMPLICIT_INSTANTIATION (d);
25437 }
25438
25439 /* Defer all other templates, unless we have been explicitly
25440 forbidden from doing so. */
25441 if (/* If there is no definition, we cannot instantiate the
25442 template. */
25443 ! pattern_defined
25444 /* If it's OK to postpone instantiation, do so. */
25445 || defer_ok
25446 /* If this is a static data member that will be defined
25447 elsewhere, we don't want to instantiate the entire data
25448 member, but we do want to instantiate the initializer so that
25449 we can substitute that elsewhere. */
25450 || (external_p && VAR_P (d))
25451 /* Handle here a deleted function too, avoid generating
25452 its body (c++/61080). */
25453 || deleted_p)
25454 {
25455 /* The definition of the static data member is now required so
25456 we must substitute the initializer. */
25457 if (VAR_P (d)
25458 && !DECL_INITIAL (d)
25459 && DECL_INITIAL (code_pattern))
25460 {
25461 tree ns;
25462 tree init;
25463 bool const_init = false;
25464 bool enter_context = DECL_CLASS_SCOPE_P (d);
25465
25466 ns = decl_namespace_context (d);
25467 push_nested_namespace (ns);
25468 if (enter_context)
25469 push_nested_class (DECL_CONTEXT (d));
25470 init = tsubst_expr (DECL_INITIAL (code_pattern),
25471 args,
25472 tf_warning_or_error, NULL_TREE,
25473 /*integral_constant_expression_p=*/false);
25474 /* If instantiating the initializer involved instantiating this
25475 again, don't call cp_finish_decl twice. */
25476 if (!DECL_INITIAL (d))
25477 {
25478 /* Make sure the initializer is still constant, in case of
25479 circular dependency (template/instantiate6.C). */
25480 const_init
25481 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25482 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25483 /*asmspec_tree=*/NULL_TREE,
25484 LOOKUP_ONLYCONVERTING);
25485 }
25486 if (enter_context)
25487 pop_nested_class ();
25488 pop_nested_namespace (ns);
25489 }
25490
25491 /* We restore the source position here because it's used by
25492 add_pending_template. */
25493 input_location = saved_loc;
25494
25495 if (at_eof && !pattern_defined
25496 && DECL_EXPLICIT_INSTANTIATION (d)
25497 && DECL_NOT_REALLY_EXTERN (d))
25498 /* [temp.explicit]
25499
25500 The definition of a non-exported function template, a
25501 non-exported member function template, or a non-exported
25502 member function or static data member of a class template
25503 shall be present in every translation unit in which it is
25504 explicitly instantiated. */
25505 permerror (input_location, "explicit instantiation of %qD "
25506 "but no definition available", d);
25507
25508 /* If we're in unevaluated context, we just wanted to get the
25509 constant value; this isn't an odr use, so don't queue
25510 a full instantiation. */
25511 if (cp_unevaluated_operand != 0)
25512 goto out;
25513 /* ??? Historically, we have instantiated inline functions, even
25514 when marked as "extern template". */
25515 if (!(external_p && VAR_P (d)))
25516 add_pending_template (d);
25517 goto out;
25518 }
25519
25520 bool push_to_top, nested;
25521 tree fn_context;
25522 fn_context = decl_function_context (d);
25523 if (LAMBDA_FUNCTION_P (d))
25524 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25525 fn_context = NULL_TREE;
25526 nested = current_function_decl != NULL_TREE;
25527 push_to_top = !(nested && fn_context == current_function_decl);
25528
25529 vec<tree> omp_privatization_save;
25530 if (nested)
25531 save_omp_privatization_clauses (omp_privatization_save);
25532
25533 if (push_to_top)
25534 push_to_top_level ();
25535 else
25536 {
25537 gcc_assert (!processing_template_decl);
25538 push_function_context ();
25539 cp_unevaluated_operand = 0;
25540 c_inhibit_evaluation_warnings = 0;
25541 }
25542
25543 if (VAR_P (d))
25544 {
25545 /* The variable might be a lambda's extra scope, and that
25546 lambda's visibility depends on D's. */
25547 maybe_commonize_var (d);
25548 determine_visibility (d);
25549 }
25550
25551 /* Mark D as instantiated so that recursive calls to
25552 instantiate_decl do not try to instantiate it again. */
25553 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25554
25555 /* Regenerate the declaration in case the template has been modified
25556 by a subsequent redeclaration. */
25557 regenerate_decl_from_template (d, td, args);
25558
25559 /* We already set the file and line above. Reset them now in case
25560 they changed as a result of calling regenerate_decl_from_template. */
25561 input_location = DECL_SOURCE_LOCATION (d);
25562
25563 if (VAR_P (d))
25564 {
25565 tree init;
25566 bool const_init = false;
25567
25568 /* Clear out DECL_RTL; whatever was there before may not be right
25569 since we've reset the type of the declaration. */
25570 SET_DECL_RTL (d, NULL);
25571 DECL_IN_AGGR_P (d) = 0;
25572
25573 /* The initializer is placed in DECL_INITIAL by
25574 regenerate_decl_from_template so we don't need to
25575 push/pop_access_scope again here. Pull it out so that
25576 cp_finish_decl can process it. */
25577 init = DECL_INITIAL (d);
25578 DECL_INITIAL (d) = NULL_TREE;
25579 DECL_INITIALIZED_P (d) = 0;
25580
25581 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25582 initializer. That function will defer actual emission until
25583 we have a chance to determine linkage. */
25584 DECL_EXTERNAL (d) = 0;
25585
25586 /* Enter the scope of D so that access-checking works correctly. */
25587 bool enter_context = DECL_CLASS_SCOPE_P (d);
25588 if (enter_context)
25589 push_nested_class (DECL_CONTEXT (d));
25590
25591 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25592 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25593 ? LOOKUP_CONSTINIT : 0);
25594 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25595
25596 if (enter_context)
25597 pop_nested_class ();
25598
25599 if (variable_template_p (gen_tmpl))
25600 note_variable_template_instantiation (d);
25601 }
25602 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25603 synthesize_method (d);
25604 else if (TREE_CODE (d) == FUNCTION_DECL)
25605 {
25606 /* Set up the list of local specializations. */
25607 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25608 tree block = NULL_TREE;
25609
25610 /* Set up context. */
25611 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25612 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25613 block = push_stmt_list ();
25614 else
25615 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25616
25617 /* Some typedefs referenced from within the template code need to be
25618 access checked at template instantiation time, i.e now. These
25619 types were added to the template at parsing time. Let's get those
25620 and perform the access checks then. */
25621 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25622 args);
25623
25624 /* Create substitution entries for the parameters. */
25625 register_parameter_specializations (code_pattern, d);
25626
25627 /* Substitute into the body of the function. */
25628 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25629 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25630 tf_warning_or_error, tmpl);
25631 else
25632 {
25633 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25634 tf_warning_or_error, tmpl,
25635 /*integral_constant_expression_p=*/false);
25636
25637 /* Set the current input_location to the end of the function
25638 so that finish_function knows where we are. */
25639 input_location
25640 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25641
25642 /* Remember if we saw an infinite loop in the template. */
25643 current_function_infinite_loop
25644 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25645 }
25646
25647 /* Finish the function. */
25648 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25649 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25650 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25651 else
25652 {
25653 d = finish_function (/*inline_p=*/false);
25654 expand_or_defer_fn (d);
25655 }
25656
25657 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25658 cp_check_omp_declare_reduction (d);
25659 }
25660
25661 /* We're not deferring instantiation any more. */
25662 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25663
25664 if (push_to_top)
25665 pop_from_top_level ();
25666 else
25667 pop_function_context ();
25668
25669 if (nested)
25670 restore_omp_privatization_clauses (omp_privatization_save);
25671
25672 out:
25673 pop_deferring_access_checks ();
25674 timevar_pop (TV_TEMPLATE_INST);
25675 pop_tinst_level ();
25676 input_location = saved_loc;
25677 cp_unevaluated_operand = saved_unevaluated_operand;
25678 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25679
25680 return d;
25681 }
25682
25683 /* Run through the list of templates that we wish we could
25684 instantiate, and instantiate any we can. RETRIES is the
25685 number of times we retry pending template instantiation. */
25686
25687 void
25688 instantiate_pending_templates (int retries)
25689 {
25690 int reconsider;
25691 location_t saved_loc = input_location;
25692
25693 /* Instantiating templates may trigger vtable generation. This in turn
25694 may require further template instantiations. We place a limit here
25695 to avoid infinite loop. */
25696 if (pending_templates && retries >= max_tinst_depth)
25697 {
25698 tree decl = pending_templates->tinst->maybe_get_node ();
25699
25700 fatal_error (input_location,
25701 "template instantiation depth exceeds maximum of %d"
25702 " instantiating %q+D, possibly from virtual table generation"
25703 " (use %<-ftemplate-depth=%> to increase the maximum)",
25704 max_tinst_depth, decl);
25705 if (TREE_CODE (decl) == FUNCTION_DECL)
25706 /* Pretend that we defined it. */
25707 DECL_INITIAL (decl) = error_mark_node;
25708 return;
25709 }
25710
25711 do
25712 {
25713 struct pending_template **t = &pending_templates;
25714 struct pending_template *last = NULL;
25715 reconsider = 0;
25716 while (*t)
25717 {
25718 tree instantiation = reopen_tinst_level ((*t)->tinst);
25719 bool complete = false;
25720
25721 if (TYPE_P (instantiation))
25722 {
25723 if (!COMPLETE_TYPE_P (instantiation))
25724 {
25725 instantiate_class_template (instantiation);
25726 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25727 for (tree fld = TYPE_FIELDS (instantiation);
25728 fld; fld = TREE_CHAIN (fld))
25729 if ((VAR_P (fld)
25730 || (TREE_CODE (fld) == FUNCTION_DECL
25731 && !DECL_ARTIFICIAL (fld)))
25732 && DECL_TEMPLATE_INSTANTIATION (fld))
25733 instantiate_decl (fld,
25734 /*defer_ok=*/false,
25735 /*expl_inst_class_mem_p=*/false);
25736
25737 if (COMPLETE_TYPE_P (instantiation))
25738 reconsider = 1;
25739 }
25740
25741 complete = COMPLETE_TYPE_P (instantiation);
25742 }
25743 else
25744 {
25745 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25746 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25747 {
25748 instantiation
25749 = instantiate_decl (instantiation,
25750 /*defer_ok=*/false,
25751 /*expl_inst_class_mem_p=*/false);
25752 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25753 reconsider = 1;
25754 }
25755
25756 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25757 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25758 }
25759
25760 if (complete)
25761 {
25762 /* If INSTANTIATION has been instantiated, then we don't
25763 need to consider it again in the future. */
25764 struct pending_template *drop = *t;
25765 *t = (*t)->next;
25766 set_refcount_ptr (drop->tinst);
25767 pending_template_freelist ().free (drop);
25768 }
25769 else
25770 {
25771 last = *t;
25772 t = &(*t)->next;
25773 }
25774 tinst_depth = 0;
25775 set_refcount_ptr (current_tinst_level);
25776 }
25777 last_pending_template = last;
25778 }
25779 while (reconsider);
25780
25781 input_location = saved_loc;
25782 }
25783
25784 /* Substitute ARGVEC into T, which is a list of initializers for
25785 either base class or a non-static data member. The TREE_PURPOSEs
25786 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25787 instantiate_decl. */
25788
25789 static tree
25790 tsubst_initializer_list (tree t, tree argvec)
25791 {
25792 tree inits = NULL_TREE;
25793 tree target_ctor = error_mark_node;
25794
25795 for (; t; t = TREE_CHAIN (t))
25796 {
25797 tree decl;
25798 tree init;
25799 tree expanded_bases = NULL_TREE;
25800 tree expanded_arguments = NULL_TREE;
25801 int i, len = 1;
25802
25803 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25804 {
25805 tree expr;
25806 tree arg;
25807
25808 /* Expand the base class expansion type into separate base
25809 classes. */
25810 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25811 tf_warning_or_error,
25812 NULL_TREE);
25813 if (expanded_bases == error_mark_node)
25814 continue;
25815
25816 /* We'll be building separate TREE_LISTs of arguments for
25817 each base. */
25818 len = TREE_VEC_LENGTH (expanded_bases);
25819 expanded_arguments = make_tree_vec (len);
25820 for (i = 0; i < len; i++)
25821 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25822
25823 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25824 expand each argument in the TREE_VALUE of t. */
25825 expr = make_node (EXPR_PACK_EXPANSION);
25826 PACK_EXPANSION_LOCAL_P (expr) = true;
25827 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25828 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25829
25830 if (TREE_VALUE (t) == void_type_node)
25831 /* VOID_TYPE_NODE is used to indicate
25832 value-initialization. */
25833 {
25834 for (i = 0; i < len; i++)
25835 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25836 }
25837 else
25838 {
25839 /* Substitute parameter packs into each argument in the
25840 TREE_LIST. */
25841 in_base_initializer = 1;
25842 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25843 {
25844 tree expanded_exprs;
25845
25846 /* Expand the argument. */
25847 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25848 expanded_exprs
25849 = tsubst_pack_expansion (expr, argvec,
25850 tf_warning_or_error,
25851 NULL_TREE);
25852 if (expanded_exprs == error_mark_node)
25853 continue;
25854
25855 /* Prepend each of the expanded expressions to the
25856 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25857 for (i = 0; i < len; i++)
25858 {
25859 TREE_VEC_ELT (expanded_arguments, i) =
25860 tree_cons (NULL_TREE,
25861 TREE_VEC_ELT (expanded_exprs, i),
25862 TREE_VEC_ELT (expanded_arguments, i));
25863 }
25864 }
25865 in_base_initializer = 0;
25866
25867 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25868 since we built them backwards. */
25869 for (i = 0; i < len; i++)
25870 {
25871 TREE_VEC_ELT (expanded_arguments, i) =
25872 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25873 }
25874 }
25875 }
25876
25877 for (i = 0; i < len; ++i)
25878 {
25879 if (expanded_bases)
25880 {
25881 decl = TREE_VEC_ELT (expanded_bases, i);
25882 decl = expand_member_init (decl);
25883 init = TREE_VEC_ELT (expanded_arguments, i);
25884 }
25885 else
25886 {
25887 tree tmp;
25888 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25889 tf_warning_or_error, NULL_TREE);
25890
25891 decl = expand_member_init (decl);
25892 if (decl && !DECL_P (decl))
25893 in_base_initializer = 1;
25894
25895 init = TREE_VALUE (t);
25896 tmp = init;
25897 if (init != void_type_node)
25898 init = tsubst_expr (init, argvec,
25899 tf_warning_or_error, NULL_TREE,
25900 /*integral_constant_expression_p=*/false);
25901 if (init == NULL_TREE && tmp != NULL_TREE)
25902 /* If we had an initializer but it instantiated to nothing,
25903 value-initialize the object. This will only occur when
25904 the initializer was a pack expansion where the parameter
25905 packs used in that expansion were of length zero. */
25906 init = void_type_node;
25907 in_base_initializer = 0;
25908 }
25909
25910 if (target_ctor != error_mark_node
25911 && init != error_mark_node)
25912 {
25913 error ("mem-initializer for %qD follows constructor delegation",
25914 decl);
25915 return inits;
25916 }
25917 /* Look for a target constructor. */
25918 if (init != error_mark_node
25919 && decl && CLASS_TYPE_P (decl)
25920 && same_type_p (decl, current_class_type))
25921 {
25922 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25923 if (inits)
25924 {
25925 error ("constructor delegation follows mem-initializer for %qD",
25926 TREE_PURPOSE (inits));
25927 continue;
25928 }
25929 target_ctor = init;
25930 }
25931
25932 if (decl)
25933 {
25934 init = build_tree_list (decl, init);
25935 TREE_CHAIN (init) = inits;
25936 inits = init;
25937 }
25938 }
25939 }
25940 return inits;
25941 }
25942
25943 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25944
25945 static void
25946 set_current_access_from_decl (tree decl)
25947 {
25948 if (TREE_PRIVATE (decl))
25949 current_access_specifier = access_private_node;
25950 else if (TREE_PROTECTED (decl))
25951 current_access_specifier = access_protected_node;
25952 else
25953 current_access_specifier = access_public_node;
25954 }
25955
25956 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25957 is the instantiation (which should have been created with
25958 start_enum) and ARGS are the template arguments to use. */
25959
25960 static void
25961 tsubst_enum (tree tag, tree newtag, tree args)
25962 {
25963 tree e;
25964
25965 if (SCOPED_ENUM_P (newtag))
25966 begin_scope (sk_scoped_enum, newtag);
25967
25968 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25969 {
25970 tree value;
25971 tree decl;
25972
25973 decl = TREE_VALUE (e);
25974 /* Note that in a template enum, the TREE_VALUE is the
25975 CONST_DECL, not the corresponding INTEGER_CST. */
25976 value = tsubst_expr (DECL_INITIAL (decl),
25977 args, tf_warning_or_error, NULL_TREE,
25978 /*integral_constant_expression_p=*/true);
25979
25980 /* Give this enumeration constant the correct access. */
25981 set_current_access_from_decl (decl);
25982
25983 /* Actually build the enumerator itself. Here we're assuming that
25984 enumerators can't have dependent attributes. */
25985 build_enumerator (DECL_NAME (decl), value, newtag,
25986 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25987 }
25988
25989 if (SCOPED_ENUM_P (newtag))
25990 finish_scope ();
25991
25992 finish_enum_value_list (newtag);
25993 finish_enum (newtag);
25994
25995 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25996 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25997 }
25998
25999 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26000 its type -- but without substituting the innermost set of template
26001 arguments. So, innermost set of template parameters will appear in
26002 the type. */
26003
26004 tree
26005 get_mostly_instantiated_function_type (tree decl)
26006 {
26007 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26008 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26009 }
26010
26011 /* Return truthvalue if we're processing a template different from
26012 the last one involved in diagnostics. */
26013 bool
26014 problematic_instantiation_changed (void)
26015 {
26016 return current_tinst_level != last_error_tinst_level;
26017 }
26018
26019 /* Remember current template involved in diagnostics. */
26020 void
26021 record_last_problematic_instantiation (void)
26022 {
26023 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26024 }
26025
26026 struct tinst_level *
26027 current_instantiation (void)
26028 {
26029 return current_tinst_level;
26030 }
26031
26032 /* Return TRUE if current_function_decl is being instantiated, false
26033 otherwise. */
26034
26035 bool
26036 instantiating_current_function_p (void)
26037 {
26038 return (current_instantiation ()
26039 && (current_instantiation ()->maybe_get_node ()
26040 == current_function_decl));
26041 }
26042
26043 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26044 type. Return false for ok, true for disallowed. Issue error and
26045 inform messages under control of COMPLAIN. */
26046
26047 static bool
26048 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26049 {
26050 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26051 return false;
26052 else if (TYPE_PTR_P (type))
26053 return false;
26054 else if (TYPE_REF_P (type)
26055 && !TYPE_REF_IS_RVALUE (type))
26056 return false;
26057 else if (TYPE_PTRMEM_P (type))
26058 return false;
26059 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26060 {
26061 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26062 {
26063 if (complain & tf_error)
26064 error ("non-type template parameters of deduced class type only "
26065 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26066 return true;
26067 }
26068 return false;
26069 }
26070 else if (TREE_CODE (type) == TYPENAME_TYPE)
26071 return false;
26072 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26073 return false;
26074 else if (TREE_CODE (type) == NULLPTR_TYPE)
26075 return false;
26076 /* A bound template template parm could later be instantiated to have a valid
26077 nontype parm type via an alias template. */
26078 else if (cxx_dialect >= cxx11
26079 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26080 return false;
26081 else if (CLASS_TYPE_P (type))
26082 {
26083 if (cxx_dialect < cxx20)
26084 {
26085 if (complain & tf_error)
26086 error ("non-type template parameters of class type only available "
26087 "with %<-std=c++20%> or %<-std=gnu++20%>");
26088 return true;
26089 }
26090 if (dependent_type_p (type))
26091 return false;
26092 if (!complete_type_or_else (type, NULL_TREE))
26093 return true;
26094 if (!structural_type_p (type))
26095 {
26096 if (complain & tf_error)
26097 {
26098 auto_diagnostic_group d;
26099 error ("%qT is not a valid type for a template non-type "
26100 "parameter because it is not structural", type);
26101 structural_type_p (type, true);
26102 }
26103 return true;
26104 }
26105 return false;
26106 }
26107
26108 if (complain & tf_error)
26109 {
26110 if (type == error_mark_node)
26111 inform (input_location, "invalid template non-type parameter");
26112 else
26113 error ("%q#T is not a valid type for a template non-type parameter",
26114 type);
26115 }
26116 return true;
26117 }
26118
26119 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26120 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26121
26122 static bool
26123 dependent_type_p_r (tree type)
26124 {
26125 tree scope;
26126
26127 /* [temp.dep.type]
26128
26129 A type is dependent if it is:
26130
26131 -- a template parameter. Template template parameters are types
26132 for us (since TYPE_P holds true for them) so we handle
26133 them here. */
26134 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26135 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26136 return true;
26137 /* -- a qualified-id with a nested-name-specifier which contains a
26138 class-name that names a dependent type or whose unqualified-id
26139 names a dependent type. */
26140 if (TREE_CODE (type) == TYPENAME_TYPE)
26141 return true;
26142
26143 /* An alias template specialization can be dependent even if the
26144 resulting type is not. */
26145 if (dependent_alias_template_spec_p (type, nt_transparent))
26146 return true;
26147
26148 /* -- a cv-qualified type where the cv-unqualified type is
26149 dependent.
26150 No code is necessary for this bullet; the code below handles
26151 cv-qualified types, and we don't want to strip aliases with
26152 TYPE_MAIN_VARIANT because of DR 1558. */
26153 /* -- a compound type constructed from any dependent type. */
26154 if (TYPE_PTRMEM_P (type))
26155 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26156 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26157 (type)));
26158 else if (INDIRECT_TYPE_P (type))
26159 return dependent_type_p (TREE_TYPE (type));
26160 else if (FUNC_OR_METHOD_TYPE_P (type))
26161 {
26162 tree arg_type;
26163
26164 if (dependent_type_p (TREE_TYPE (type)))
26165 return true;
26166 for (arg_type = TYPE_ARG_TYPES (type);
26167 arg_type;
26168 arg_type = TREE_CHAIN (arg_type))
26169 if (dependent_type_p (TREE_VALUE (arg_type)))
26170 return true;
26171 if (cxx_dialect >= cxx17)
26172 /* A value-dependent noexcept-specifier makes the type dependent. */
26173 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26174 if (tree noex = TREE_PURPOSE (spec))
26175 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26176 affect overload resolution and treating it as dependent breaks
26177 things. Same for an unparsed noexcept expression. */
26178 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26179 && TREE_CODE (noex) != DEFERRED_PARSE
26180 && value_dependent_expression_p (noex))
26181 return true;
26182 return false;
26183 }
26184 /* -- an array type constructed from any dependent type or whose
26185 size is specified by a constant expression that is
26186 value-dependent.
26187
26188 We checked for type- and value-dependence of the bounds in
26189 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26190 if (TREE_CODE (type) == ARRAY_TYPE)
26191 {
26192 if (TYPE_DOMAIN (type)
26193 && dependent_type_p (TYPE_DOMAIN (type)))
26194 return true;
26195 return dependent_type_p (TREE_TYPE (type));
26196 }
26197
26198 /* -- a template-id in which either the template name is a template
26199 parameter ... */
26200 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26201 return true;
26202 /* ... or any of the template arguments is a dependent type or
26203 an expression that is type-dependent or value-dependent. */
26204 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26205 && (any_dependent_template_arguments_p
26206 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26207 return true;
26208
26209 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26210 dependent; if the argument of the `typeof' expression is not
26211 type-dependent, then it should already been have resolved. */
26212 if (TREE_CODE (type) == TYPEOF_TYPE
26213 || TREE_CODE (type) == DECLTYPE_TYPE
26214 || TREE_CODE (type) == UNDERLYING_TYPE)
26215 return true;
26216
26217 /* A template argument pack is dependent if any of its packed
26218 arguments are. */
26219 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26220 {
26221 tree args = ARGUMENT_PACK_ARGS (type);
26222 int i, len = TREE_VEC_LENGTH (args);
26223 for (i = 0; i < len; ++i)
26224 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26225 return true;
26226 }
26227
26228 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26229 be template parameters. */
26230 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26231 return true;
26232
26233 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26234 return true;
26235
26236 /* The standard does not specifically mention types that are local
26237 to template functions or local classes, but they should be
26238 considered dependent too. For example:
26239
26240 template <int I> void f() {
26241 enum E { a = I };
26242 S<sizeof (E)> s;
26243 }
26244
26245 The size of `E' cannot be known until the value of `I' has been
26246 determined. Therefore, `E' must be considered dependent. */
26247 scope = TYPE_CONTEXT (type);
26248 if (scope && TYPE_P (scope))
26249 return dependent_type_p (scope);
26250 /* Don't use type_dependent_expression_p here, as it can lead
26251 to infinite recursion trying to determine whether a lambda
26252 nested in a lambda is dependent (c++/47687). */
26253 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26254 && DECL_LANG_SPECIFIC (scope)
26255 && DECL_TEMPLATE_INFO (scope)
26256 && (any_dependent_template_arguments_p
26257 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26258 return true;
26259
26260 /* Other types are non-dependent. */
26261 return false;
26262 }
26263
26264 /* Returns TRUE if TYPE is dependent, in the sense of
26265 [temp.dep.type]. Note that a NULL type is considered dependent. */
26266
26267 bool
26268 dependent_type_p (tree type)
26269 {
26270 /* If there are no template parameters in scope, then there can't be
26271 any dependent types. */
26272 if (!processing_template_decl)
26273 {
26274 /* If we are not processing a template, then nobody should be
26275 providing us with a dependent type. */
26276 gcc_assert (type);
26277 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26278 return false;
26279 }
26280
26281 /* If the type is NULL, we have not computed a type for the entity
26282 in question; in that case, the type is dependent. */
26283 if (!type)
26284 return true;
26285
26286 /* Erroneous types can be considered non-dependent. */
26287 if (type == error_mark_node)
26288 return false;
26289
26290 /* Getting here with global_type_node means we improperly called this
26291 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26292 gcc_checking_assert (type != global_type_node);
26293
26294 /* If we have not already computed the appropriate value for TYPE,
26295 do so now. */
26296 if (!TYPE_DEPENDENT_P_VALID (type))
26297 {
26298 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26299 TYPE_DEPENDENT_P_VALID (type) = 1;
26300 }
26301
26302 return TYPE_DEPENDENT_P (type);
26303 }
26304
26305 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26306 lookup. In other words, a dependent type that is not the current
26307 instantiation. */
26308
26309 bool
26310 dependent_scope_p (tree scope)
26311 {
26312 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26313 && !currently_open_class (scope));
26314 }
26315
26316 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26317 an unknown base of 'this' (and is therefore instantiation-dependent). */
26318
26319 static bool
26320 unknown_base_ref_p (tree t)
26321 {
26322 if (!current_class_ptr)
26323 return false;
26324
26325 tree mem = TREE_OPERAND (t, 1);
26326 if (shared_member_p (mem))
26327 return false;
26328
26329 tree cur = current_nonlambda_class_type ();
26330 if (!any_dependent_bases_p (cur))
26331 return false;
26332
26333 tree ctx = TREE_OPERAND (t, 0);
26334 if (DERIVED_FROM_P (ctx, cur))
26335 return false;
26336
26337 return true;
26338 }
26339
26340 /* T is a SCOPE_REF; return whether we need to consider it
26341 instantiation-dependent so that we can check access at instantiation
26342 time even though we know which member it resolves to. */
26343
26344 static bool
26345 instantiation_dependent_scope_ref_p (tree t)
26346 {
26347 if (DECL_P (TREE_OPERAND (t, 1))
26348 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26349 && !unknown_base_ref_p (t)
26350 && accessible_in_template_p (TREE_OPERAND (t, 0),
26351 TREE_OPERAND (t, 1)))
26352 return false;
26353 else
26354 return true;
26355 }
26356
26357 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26358 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26359 expression. */
26360
26361 /* Note that this predicate is not appropriate for general expressions;
26362 only constant expressions (that satisfy potential_constant_expression)
26363 can be tested for value dependence. */
26364
26365 bool
26366 value_dependent_expression_p (tree expression)
26367 {
26368 if (!processing_template_decl || expression == NULL_TREE)
26369 return false;
26370
26371 /* A type-dependent expression is also value-dependent. */
26372 if (type_dependent_expression_p (expression))
26373 return true;
26374
26375 switch (TREE_CODE (expression))
26376 {
26377 case BASELINK:
26378 /* A dependent member function of the current instantiation. */
26379 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26380
26381 case FUNCTION_DECL:
26382 /* A dependent member function of the current instantiation. */
26383 if (DECL_CLASS_SCOPE_P (expression)
26384 && dependent_type_p (DECL_CONTEXT (expression)))
26385 return true;
26386 break;
26387
26388 case IDENTIFIER_NODE:
26389 /* A name that has not been looked up -- must be dependent. */
26390 return true;
26391
26392 case TEMPLATE_PARM_INDEX:
26393 /* A non-type template parm. */
26394 return true;
26395
26396 case CONST_DECL:
26397 /* A non-type template parm. */
26398 if (DECL_TEMPLATE_PARM_P (expression))
26399 return true;
26400 return value_dependent_expression_p (DECL_INITIAL (expression));
26401
26402 case VAR_DECL:
26403 /* A constant with literal type and is initialized
26404 with an expression that is value-dependent. */
26405 if (DECL_DEPENDENT_INIT_P (expression)
26406 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26407 || TYPE_REF_P (TREE_TYPE (expression)))
26408 return true;
26409 if (DECL_HAS_VALUE_EXPR_P (expression))
26410 {
26411 tree value_expr = DECL_VALUE_EXPR (expression);
26412 if (value_dependent_expression_p (value_expr)
26413 /* __PRETTY_FUNCTION__ inside a template function is dependent
26414 on the name of the function. */
26415 || (DECL_PRETTY_FUNCTION_P (expression)
26416 /* It might be used in a template, but not a template
26417 function, in which case its DECL_VALUE_EXPR will be
26418 "top level". */
26419 && value_expr == error_mark_node))
26420 return true;
26421 }
26422 return false;
26423
26424 case DYNAMIC_CAST_EXPR:
26425 case STATIC_CAST_EXPR:
26426 case CONST_CAST_EXPR:
26427 case REINTERPRET_CAST_EXPR:
26428 case CAST_EXPR:
26429 case IMPLICIT_CONV_EXPR:
26430 /* These expressions are value-dependent if the type to which
26431 the cast occurs is dependent or the expression being casted
26432 is value-dependent. */
26433 {
26434 tree type = TREE_TYPE (expression);
26435
26436 if (dependent_type_p (type))
26437 return true;
26438
26439 /* A functional cast has a list of operands. */
26440 expression = TREE_OPERAND (expression, 0);
26441 if (!expression)
26442 {
26443 /* If there are no operands, it must be an expression such
26444 as "int()". This should not happen for aggregate types
26445 because it would form non-constant expressions. */
26446 gcc_assert (cxx_dialect >= cxx11
26447 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26448
26449 return false;
26450 }
26451
26452 if (TREE_CODE (expression) == TREE_LIST)
26453 return any_value_dependent_elements_p (expression);
26454
26455 return value_dependent_expression_p (expression);
26456 }
26457
26458 case SIZEOF_EXPR:
26459 if (SIZEOF_EXPR_TYPE_P (expression))
26460 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26461 /* FALLTHRU */
26462 case ALIGNOF_EXPR:
26463 case TYPEID_EXPR:
26464 /* A `sizeof' expression is value-dependent if the operand is
26465 type-dependent or is a pack expansion. */
26466 expression = TREE_OPERAND (expression, 0);
26467 if (PACK_EXPANSION_P (expression))
26468 return true;
26469 else if (TYPE_P (expression))
26470 return dependent_type_p (expression);
26471 return instantiation_dependent_uneval_expression_p (expression);
26472
26473 case AT_ENCODE_EXPR:
26474 /* An 'encode' expression is value-dependent if the operand is
26475 type-dependent. */
26476 expression = TREE_OPERAND (expression, 0);
26477 return dependent_type_p (expression);
26478
26479 case NOEXCEPT_EXPR:
26480 expression = TREE_OPERAND (expression, 0);
26481 return instantiation_dependent_uneval_expression_p (expression);
26482
26483 case SCOPE_REF:
26484 /* All instantiation-dependent expressions should also be considered
26485 value-dependent. */
26486 return instantiation_dependent_scope_ref_p (expression);
26487
26488 case COMPONENT_REF:
26489 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26490 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26491
26492 case NONTYPE_ARGUMENT_PACK:
26493 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26494 is value-dependent. */
26495 {
26496 tree values = ARGUMENT_PACK_ARGS (expression);
26497 int i, len = TREE_VEC_LENGTH (values);
26498
26499 for (i = 0; i < len; ++i)
26500 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26501 return true;
26502
26503 return false;
26504 }
26505
26506 case TRAIT_EXPR:
26507 {
26508 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26509
26510 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26511 return true;
26512
26513 if (!type2)
26514 return false;
26515
26516 if (TREE_CODE (type2) != TREE_LIST)
26517 return dependent_type_p (type2);
26518
26519 for (; type2; type2 = TREE_CHAIN (type2))
26520 if (dependent_type_p (TREE_VALUE (type2)))
26521 return true;
26522
26523 return false;
26524 }
26525
26526 case MODOP_EXPR:
26527 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26528 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26529
26530 case ARRAY_REF:
26531 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26532 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26533
26534 case ADDR_EXPR:
26535 {
26536 tree op = TREE_OPERAND (expression, 0);
26537 return (value_dependent_expression_p (op)
26538 || has_value_dependent_address (op));
26539 }
26540
26541 case REQUIRES_EXPR:
26542 /* Treat all requires-expressions as value-dependent so
26543 we don't try to fold them. */
26544 return true;
26545
26546 case TYPE_REQ:
26547 return dependent_type_p (TREE_OPERAND (expression, 0));
26548
26549 case CALL_EXPR:
26550 {
26551 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26552 return true;
26553 tree fn = get_callee_fndecl (expression);
26554 int i, nargs;
26555 nargs = call_expr_nargs (expression);
26556 for (i = 0; i < nargs; ++i)
26557 {
26558 tree op = CALL_EXPR_ARG (expression, i);
26559 /* In a call to a constexpr member function, look through the
26560 implicit ADDR_EXPR on the object argument so that it doesn't
26561 cause the call to be considered value-dependent. We also
26562 look through it in potential_constant_expression. */
26563 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26564 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26565 && TREE_CODE (op) == ADDR_EXPR)
26566 op = TREE_OPERAND (op, 0);
26567 if (value_dependent_expression_p (op))
26568 return true;
26569 }
26570 return false;
26571 }
26572
26573 case TEMPLATE_ID_EXPR:
26574 return concept_definition_p (TREE_OPERAND (expression, 0));
26575
26576 case CONSTRUCTOR:
26577 {
26578 unsigned ix;
26579 tree val;
26580 if (dependent_type_p (TREE_TYPE (expression)))
26581 return true;
26582 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26583 if (value_dependent_expression_p (val))
26584 return true;
26585 return false;
26586 }
26587
26588 case STMT_EXPR:
26589 /* Treat a GNU statement expression as dependent to avoid crashing
26590 under instantiate_non_dependent_expr; it can't be constant. */
26591 return true;
26592
26593 default:
26594 /* A constant expression is value-dependent if any subexpression is
26595 value-dependent. */
26596 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26597 {
26598 case tcc_reference:
26599 case tcc_unary:
26600 case tcc_comparison:
26601 case tcc_binary:
26602 case tcc_expression:
26603 case tcc_vl_exp:
26604 {
26605 int i, len = cp_tree_operand_length (expression);
26606
26607 for (i = 0; i < len; i++)
26608 {
26609 tree t = TREE_OPERAND (expression, i);
26610
26611 /* In some cases, some of the operands may be missing.
26612 (For example, in the case of PREDECREMENT_EXPR, the
26613 amount to increment by may be missing.) That doesn't
26614 make the expression dependent. */
26615 if (t && value_dependent_expression_p (t))
26616 return true;
26617 }
26618 }
26619 break;
26620 default:
26621 break;
26622 }
26623 break;
26624 }
26625
26626 /* The expression is not value-dependent. */
26627 return false;
26628 }
26629
26630 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26631 [temp.dep.expr]. Note that an expression with no type is
26632 considered dependent. Other parts of the compiler arrange for an
26633 expression with type-dependent subexpressions to have no type, so
26634 this function doesn't have to be fully recursive. */
26635
26636 bool
26637 type_dependent_expression_p (tree expression)
26638 {
26639 if (!processing_template_decl)
26640 return false;
26641
26642 if (expression == NULL_TREE || expression == error_mark_node)
26643 return false;
26644
26645 STRIP_ANY_LOCATION_WRAPPER (expression);
26646
26647 /* An unresolved name is always dependent. */
26648 if (identifier_p (expression)
26649 || TREE_CODE (expression) == USING_DECL
26650 || TREE_CODE (expression) == WILDCARD_DECL)
26651 return true;
26652
26653 /* A lambda-expression in template context is dependent. dependent_type_p is
26654 true for a lambda in the scope of a class or function template, but that
26655 doesn't cover all template contexts, like a default template argument. */
26656 if (TREE_CODE (expression) == LAMBDA_EXPR)
26657 return true;
26658
26659 /* A fold expression is type-dependent. */
26660 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26661 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26662 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26663 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26664 return true;
26665
26666 /* Some expression forms are never type-dependent. */
26667 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26668 || TREE_CODE (expression) == SIZEOF_EXPR
26669 || TREE_CODE (expression) == ALIGNOF_EXPR
26670 || TREE_CODE (expression) == AT_ENCODE_EXPR
26671 || TREE_CODE (expression) == NOEXCEPT_EXPR
26672 || TREE_CODE (expression) == TRAIT_EXPR
26673 || TREE_CODE (expression) == TYPEID_EXPR
26674 || TREE_CODE (expression) == DELETE_EXPR
26675 || TREE_CODE (expression) == VEC_DELETE_EXPR
26676 || TREE_CODE (expression) == THROW_EXPR
26677 || TREE_CODE (expression) == REQUIRES_EXPR)
26678 return false;
26679
26680 /* The types of these expressions depends only on the type to which
26681 the cast occurs. */
26682 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26683 || TREE_CODE (expression) == STATIC_CAST_EXPR
26684 || TREE_CODE (expression) == CONST_CAST_EXPR
26685 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26686 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26687 || TREE_CODE (expression) == CAST_EXPR)
26688 return dependent_type_p (TREE_TYPE (expression));
26689
26690 /* The types of these expressions depends only on the type created
26691 by the expression. */
26692 if (TREE_CODE (expression) == NEW_EXPR
26693 || TREE_CODE (expression) == VEC_NEW_EXPR)
26694 {
26695 /* For NEW_EXPR tree nodes created inside a template, either
26696 the object type itself or a TREE_LIST may appear as the
26697 operand 1. */
26698 tree type = TREE_OPERAND (expression, 1);
26699 if (TREE_CODE (type) == TREE_LIST)
26700 /* This is an array type. We need to check array dimensions
26701 as well. */
26702 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26703 || value_dependent_expression_p
26704 (TREE_OPERAND (TREE_VALUE (type), 1));
26705 else
26706 return dependent_type_p (type);
26707 }
26708
26709 if (TREE_CODE (expression) == SCOPE_REF)
26710 {
26711 tree scope = TREE_OPERAND (expression, 0);
26712 tree name = TREE_OPERAND (expression, 1);
26713
26714 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26715 contains an identifier associated by name lookup with one or more
26716 declarations declared with a dependent type, or...a
26717 nested-name-specifier or qualified-id that names a member of an
26718 unknown specialization. */
26719 return (type_dependent_expression_p (name)
26720 || dependent_scope_p (scope));
26721 }
26722
26723 if (TREE_CODE (expression) == TEMPLATE_DECL
26724 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26725 return uses_outer_template_parms (expression);
26726
26727 if (TREE_CODE (expression) == STMT_EXPR)
26728 expression = stmt_expr_value_expr (expression);
26729
26730 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26731 {
26732 tree elt;
26733 unsigned i;
26734
26735 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26736 {
26737 if (type_dependent_expression_p (elt))
26738 return true;
26739 }
26740 return false;
26741 }
26742
26743 /* A static data member of the current instantiation with incomplete
26744 array type is type-dependent, as the definition and specializations
26745 can have different bounds. */
26746 if (VAR_P (expression)
26747 && DECL_CLASS_SCOPE_P (expression)
26748 && dependent_type_p (DECL_CONTEXT (expression))
26749 && VAR_HAD_UNKNOWN_BOUND (expression))
26750 return true;
26751
26752 /* An array of unknown bound depending on a variadic parameter, eg:
26753
26754 template<typename... Args>
26755 void foo (Args... args)
26756 {
26757 int arr[] = { args... };
26758 }
26759
26760 template<int... vals>
26761 void bar ()
26762 {
26763 int arr[] = { vals... };
26764 }
26765
26766 If the array has no length and has an initializer, it must be that
26767 we couldn't determine its length in cp_complete_array_type because
26768 it is dependent. */
26769 if (VAR_P (expression)
26770 && TREE_TYPE (expression) != NULL_TREE
26771 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26772 && !TYPE_DOMAIN (TREE_TYPE (expression))
26773 && DECL_INITIAL (expression))
26774 return true;
26775
26776 /* A function or variable template-id is type-dependent if it has any
26777 dependent template arguments. */
26778 if (VAR_OR_FUNCTION_DECL_P (expression)
26779 && DECL_LANG_SPECIFIC (expression)
26780 && DECL_TEMPLATE_INFO (expression))
26781 {
26782 /* Consider the innermost template arguments, since those are the ones
26783 that come from the template-id; the template arguments for the
26784 enclosing class do not make it type-dependent unless they are used in
26785 the type of the decl. */
26786 if (instantiates_primary_template_p (expression)
26787 && (any_dependent_template_arguments_p
26788 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26789 return true;
26790 }
26791
26792 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26793 type-dependent. Checking this is important for functions with auto return
26794 type, which looks like a dependent type. */
26795 if (TREE_CODE (expression) == FUNCTION_DECL
26796 && !(DECL_CLASS_SCOPE_P (expression)
26797 && dependent_type_p (DECL_CONTEXT (expression)))
26798 && !(DECL_LANG_SPECIFIC (expression)
26799 && DECL_FRIEND_P (expression)
26800 && (!DECL_FRIEND_CONTEXT (expression)
26801 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26802 && !DECL_LOCAL_FUNCTION_P (expression))
26803 {
26804 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26805 || undeduced_auto_decl (expression));
26806 return false;
26807 }
26808
26809 /* Always dependent, on the number of arguments if nothing else. */
26810 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26811 return true;
26812
26813 if (TREE_TYPE (expression) == unknown_type_node)
26814 {
26815 if (TREE_CODE (expression) == ADDR_EXPR)
26816 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26817 if (TREE_CODE (expression) == COMPONENT_REF
26818 || TREE_CODE (expression) == OFFSET_REF)
26819 {
26820 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26821 return true;
26822 expression = TREE_OPERAND (expression, 1);
26823 if (identifier_p (expression))
26824 return false;
26825 }
26826 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26827 if (TREE_CODE (expression) == SCOPE_REF)
26828 return false;
26829
26830 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26831 if (TREE_CODE (expression) == CO_AWAIT_EXPR
26832 || TREE_CODE (expression) == CO_YIELD_EXPR)
26833 return true;
26834
26835 if (BASELINK_P (expression))
26836 {
26837 if (BASELINK_OPTYPE (expression)
26838 && dependent_type_p (BASELINK_OPTYPE (expression)))
26839 return true;
26840 expression = BASELINK_FUNCTIONS (expression);
26841 }
26842
26843 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26844 {
26845 if (any_dependent_template_arguments_p
26846 (TREE_OPERAND (expression, 1)))
26847 return true;
26848 expression = TREE_OPERAND (expression, 0);
26849 if (identifier_p (expression))
26850 return true;
26851 }
26852
26853 gcc_assert (OVL_P (expression));
26854
26855 for (lkp_iterator iter (expression); iter; ++iter)
26856 if (type_dependent_expression_p (*iter))
26857 return true;
26858
26859 return false;
26860 }
26861
26862 /* The type of a non-type template parm declared with a placeholder type
26863 depends on the corresponding template argument, even though
26864 placeholders are not normally considered dependent. */
26865 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26866 && is_auto (TREE_TYPE (expression)))
26867 return true;
26868
26869 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26870
26871 /* Dependent type attributes might not have made it from the decl to
26872 the type yet. */
26873 if (DECL_P (expression)
26874 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26875 return true;
26876
26877 return (dependent_type_p (TREE_TYPE (expression)));
26878 }
26879
26880 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26881 type-dependent if the expression refers to a member of the current
26882 instantiation and the type of the referenced member is dependent, or the
26883 class member access expression refers to a member of an unknown
26884 specialization.
26885
26886 This function returns true if the OBJECT in such a class member access
26887 expression is of an unknown specialization. */
26888
26889 bool
26890 type_dependent_object_expression_p (tree object)
26891 {
26892 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26893 dependent. */
26894 if (TREE_CODE (object) == IDENTIFIER_NODE)
26895 return true;
26896 tree scope = TREE_TYPE (object);
26897 return (!scope || dependent_scope_p (scope));
26898 }
26899
26900 /* walk_tree callback function for instantiation_dependent_expression_p,
26901 below. Returns non-zero if a dependent subexpression is found. */
26902
26903 static tree
26904 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26905 void * /*data*/)
26906 {
26907 if (TYPE_P (*tp))
26908 {
26909 /* We don't have to worry about decltype currently because decltype
26910 of an instantiation-dependent expr is a dependent type. This
26911 might change depending on the resolution of DR 1172. */
26912 *walk_subtrees = false;
26913 return NULL_TREE;
26914 }
26915 enum tree_code code = TREE_CODE (*tp);
26916 switch (code)
26917 {
26918 /* Don't treat an argument list as dependent just because it has no
26919 TREE_TYPE. */
26920 case TREE_LIST:
26921 case TREE_VEC:
26922 case NONTYPE_ARGUMENT_PACK:
26923 return NULL_TREE;
26924
26925 case TEMPLATE_PARM_INDEX:
26926 if (dependent_type_p (TREE_TYPE (*tp)))
26927 return *tp;
26928 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26929 return *tp;
26930 /* We'll check value-dependence separately. */
26931 return NULL_TREE;
26932
26933 /* Handle expressions with type operands. */
26934 case SIZEOF_EXPR:
26935 case ALIGNOF_EXPR:
26936 case TYPEID_EXPR:
26937 case AT_ENCODE_EXPR:
26938 {
26939 tree op = TREE_OPERAND (*tp, 0);
26940 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26941 op = TREE_TYPE (op);
26942 if (TYPE_P (op))
26943 {
26944 if (dependent_type_p (op))
26945 return *tp;
26946 else
26947 {
26948 *walk_subtrees = false;
26949 return NULL_TREE;
26950 }
26951 }
26952 break;
26953 }
26954
26955 case COMPONENT_REF:
26956 if (identifier_p (TREE_OPERAND (*tp, 1)))
26957 /* In a template, finish_class_member_access_expr creates a
26958 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26959 type-dependent, so that we can check access control at
26960 instantiation time (PR 42277). See also Core issue 1273. */
26961 return *tp;
26962 break;
26963
26964 case SCOPE_REF:
26965 if (instantiation_dependent_scope_ref_p (*tp))
26966 return *tp;
26967 else
26968 break;
26969
26970 /* Treat statement-expressions as dependent. */
26971 case BIND_EXPR:
26972 return *tp;
26973
26974 /* Treat requires-expressions as dependent. */
26975 case REQUIRES_EXPR:
26976 return *tp;
26977
26978 case CALL_EXPR:
26979 /* Treat concept checks as dependent. */
26980 if (concept_check_p (*tp))
26981 return *tp;
26982 break;
26983
26984 case TEMPLATE_ID_EXPR:
26985 /* Treat concept checks as dependent. */
26986 if (concept_check_p (*tp))
26987 return *tp;
26988 break;
26989
26990 case CONSTRUCTOR:
26991 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26992 return *tp;
26993 break;
26994
26995 default:
26996 break;
26997 }
26998
26999 if (type_dependent_expression_p (*tp))
27000 return *tp;
27001 else
27002 return NULL_TREE;
27003 }
27004
27005 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27006 sense defined by the ABI:
27007
27008 "An expression is instantiation-dependent if it is type-dependent
27009 or value-dependent, or it has a subexpression that is type-dependent
27010 or value-dependent."
27011
27012 Except don't actually check value-dependence for unevaluated expressions,
27013 because in sizeof(i) we don't care about the value of i. Checking
27014 type-dependence will in turn check value-dependence of array bounds/template
27015 arguments as needed. */
27016
27017 bool
27018 instantiation_dependent_uneval_expression_p (tree expression)
27019 {
27020 tree result;
27021
27022 if (!processing_template_decl)
27023 return false;
27024
27025 if (expression == error_mark_node)
27026 return false;
27027
27028 result = cp_walk_tree_without_duplicates (&expression,
27029 instantiation_dependent_r, NULL);
27030 return result != NULL_TREE;
27031 }
27032
27033 /* As above, but also check value-dependence of the expression as a whole. */
27034
27035 bool
27036 instantiation_dependent_expression_p (tree expression)
27037 {
27038 return (instantiation_dependent_uneval_expression_p (expression)
27039 || value_dependent_expression_p (expression));
27040 }
27041
27042 /* Like type_dependent_expression_p, but it also works while not processing
27043 a template definition, i.e. during substitution or mangling. */
27044
27045 bool
27046 type_dependent_expression_p_push (tree expr)
27047 {
27048 bool b;
27049 ++processing_template_decl;
27050 b = type_dependent_expression_p (expr);
27051 --processing_template_decl;
27052 return b;
27053 }
27054
27055 /* Returns TRUE if ARGS contains a type-dependent expression. */
27056
27057 bool
27058 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27059 {
27060 unsigned int i;
27061 tree arg;
27062
27063 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27064 {
27065 if (type_dependent_expression_p (arg))
27066 return true;
27067 }
27068 return false;
27069 }
27070
27071 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27072 expressions) contains any type-dependent expressions. */
27073
27074 bool
27075 any_type_dependent_elements_p (const_tree list)
27076 {
27077 for (; list; list = TREE_CHAIN (list))
27078 if (type_dependent_expression_p (TREE_VALUE (list)))
27079 return true;
27080
27081 return false;
27082 }
27083
27084 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27085 expressions) contains any value-dependent expressions. */
27086
27087 bool
27088 any_value_dependent_elements_p (const_tree list)
27089 {
27090 for (; list; list = TREE_CHAIN (list))
27091 if (value_dependent_expression_p (TREE_VALUE (list)))
27092 return true;
27093
27094 return false;
27095 }
27096
27097 /* Returns TRUE if the ARG (a template argument) is dependent. */
27098
27099 bool
27100 dependent_template_arg_p (tree arg)
27101 {
27102 if (!processing_template_decl)
27103 return false;
27104
27105 /* Assume a template argument that was wrongly written by the user
27106 is dependent. This is consistent with what
27107 any_dependent_template_arguments_p [that calls this function]
27108 does. */
27109 if (!arg || arg == error_mark_node)
27110 return true;
27111
27112 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27113 arg = argument_pack_select_arg (arg);
27114
27115 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27116 return true;
27117 if (TREE_CODE (arg) == TEMPLATE_DECL)
27118 {
27119 if (DECL_TEMPLATE_PARM_P (arg))
27120 return true;
27121 /* A member template of a dependent class is not necessarily
27122 type-dependent, but it is a dependent template argument because it
27123 will be a member of an unknown specialization to that template. */
27124 tree scope = CP_DECL_CONTEXT (arg);
27125 return TYPE_P (scope) && dependent_type_p (scope);
27126 }
27127 else if (ARGUMENT_PACK_P (arg))
27128 {
27129 tree args = ARGUMENT_PACK_ARGS (arg);
27130 int i, len = TREE_VEC_LENGTH (args);
27131 for (i = 0; i < len; ++i)
27132 {
27133 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27134 return true;
27135 }
27136
27137 return false;
27138 }
27139 else if (TYPE_P (arg))
27140 return dependent_type_p (arg);
27141 else
27142 return value_dependent_expression_p (arg);
27143 }
27144
27145 /* Returns true if ARGS (a collection of template arguments) contains
27146 any types that require structural equality testing. */
27147
27148 bool
27149 any_template_arguments_need_structural_equality_p (tree args)
27150 {
27151 int i;
27152 int j;
27153
27154 if (!args)
27155 return false;
27156 if (args == error_mark_node)
27157 return true;
27158
27159 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27160 {
27161 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27162 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27163 {
27164 tree arg = TREE_VEC_ELT (level, j);
27165 tree packed_args = NULL_TREE;
27166 int k, len = 1;
27167
27168 if (ARGUMENT_PACK_P (arg))
27169 {
27170 /* Look inside the argument pack. */
27171 packed_args = ARGUMENT_PACK_ARGS (arg);
27172 len = TREE_VEC_LENGTH (packed_args);
27173 }
27174
27175 for (k = 0; k < len; ++k)
27176 {
27177 if (packed_args)
27178 arg = TREE_VEC_ELT (packed_args, k);
27179
27180 if (error_operand_p (arg))
27181 return true;
27182 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27183 continue;
27184 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27185 return true;
27186 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27187 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27188 return true;
27189 }
27190 }
27191 }
27192
27193 return false;
27194 }
27195
27196 /* Returns true if ARGS (a collection of template arguments) contains
27197 any dependent arguments. */
27198
27199 bool
27200 any_dependent_template_arguments_p (const_tree args)
27201 {
27202 int i;
27203 int j;
27204
27205 if (!args)
27206 return false;
27207 if (args == error_mark_node)
27208 return true;
27209
27210 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27211 {
27212 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27213 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27214 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27215 return true;
27216 }
27217
27218 return false;
27219 }
27220
27221 /* Returns true if ARGS contains any errors. */
27222
27223 bool
27224 any_erroneous_template_args_p (const_tree args)
27225 {
27226 int i;
27227 int j;
27228
27229 if (args == error_mark_node)
27230 return true;
27231
27232 if (args && TREE_CODE (args) != TREE_VEC)
27233 {
27234 if (tree ti = get_template_info (args))
27235 args = TI_ARGS (ti);
27236 else
27237 args = NULL_TREE;
27238 }
27239
27240 if (!args)
27241 return false;
27242
27243 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27244 {
27245 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27246 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27247 if (error_operand_p (TREE_VEC_ELT (level, j)))
27248 return true;
27249 }
27250
27251 return false;
27252 }
27253
27254 /* Returns TRUE if the template TMPL is type-dependent. */
27255
27256 bool
27257 dependent_template_p (tree tmpl)
27258 {
27259 if (TREE_CODE (tmpl) == OVERLOAD)
27260 {
27261 for (lkp_iterator iter (tmpl); iter; ++iter)
27262 if (dependent_template_p (*iter))
27263 return true;
27264 return false;
27265 }
27266
27267 /* Template template parameters are dependent. */
27268 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27269 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27270 return true;
27271 /* So are names that have not been looked up. */
27272 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27273 return true;
27274 return false;
27275 }
27276
27277 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27278
27279 bool
27280 dependent_template_id_p (tree tmpl, tree args)
27281 {
27282 return (dependent_template_p (tmpl)
27283 || any_dependent_template_arguments_p (args));
27284 }
27285
27286 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27287 are dependent. */
27288
27289 bool
27290 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27291 {
27292 int i;
27293
27294 if (!processing_template_decl)
27295 return false;
27296
27297 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27298 {
27299 tree decl = TREE_VEC_ELT (declv, i);
27300 tree init = TREE_VEC_ELT (initv, i);
27301 tree cond = TREE_VEC_ELT (condv, i);
27302 tree incr = TREE_VEC_ELT (incrv, i);
27303
27304 if (type_dependent_expression_p (decl)
27305 || TREE_CODE (decl) == SCOPE_REF)
27306 return true;
27307
27308 if (init && type_dependent_expression_p (init))
27309 return true;
27310
27311 if (cond == global_namespace)
27312 return true;
27313
27314 if (type_dependent_expression_p (cond))
27315 return true;
27316
27317 if (COMPARISON_CLASS_P (cond)
27318 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27319 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27320 return true;
27321
27322 if (TREE_CODE (incr) == MODOP_EXPR)
27323 {
27324 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27325 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27326 return true;
27327 }
27328 else if (type_dependent_expression_p (incr))
27329 return true;
27330 else if (TREE_CODE (incr) == MODIFY_EXPR)
27331 {
27332 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27333 return true;
27334 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27335 {
27336 tree t = TREE_OPERAND (incr, 1);
27337 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27338 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27339 return true;
27340
27341 /* If this loop has a class iterator with != comparison
27342 with increment other than i++/++i/i--/--i, make sure the
27343 increment is constant. */
27344 if (CLASS_TYPE_P (TREE_TYPE (decl))
27345 && TREE_CODE (cond) == NE_EXPR)
27346 {
27347 if (TREE_OPERAND (t, 0) == decl)
27348 t = TREE_OPERAND (t, 1);
27349 else
27350 t = TREE_OPERAND (t, 0);
27351 if (TREE_CODE (t) != INTEGER_CST)
27352 return true;
27353 }
27354 }
27355 }
27356 }
27357
27358 return false;
27359 }
27360
27361 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27362 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27363 no such TYPE can be found. Note that this function peers inside
27364 uninstantiated templates and therefore should be used only in
27365 extremely limited situations. ONLY_CURRENT_P restricts this
27366 peering to the currently open classes hierarchy (which is required
27367 when comparing types). */
27368
27369 tree
27370 resolve_typename_type (tree type, bool only_current_p)
27371 {
27372 tree scope;
27373 tree name;
27374 tree decl;
27375 int quals;
27376 tree pushed_scope;
27377 tree result;
27378
27379 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27380
27381 scope = TYPE_CONTEXT (type);
27382 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27383 gcc_checking_assert (uses_template_parms (scope));
27384
27385 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27386 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27387 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27388 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27389 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27390 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27391 the TYPENAME_TYPE instead, we avoid messing up with a possible
27392 typedef variant case. */
27393 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27394
27395 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27396 it first before we can figure out what NAME refers to. */
27397 if (TREE_CODE (scope) == TYPENAME_TYPE)
27398 {
27399 if (TYPENAME_IS_RESOLVING_P (scope))
27400 /* Given a class template A with a dependent base with nested type C,
27401 typedef typename A::C::C C will land us here, as trying to resolve
27402 the initial A::C leads to the local C typedef, which leads back to
27403 A::C::C. So we break the recursion now. */
27404 return type;
27405 else
27406 scope = resolve_typename_type (scope, only_current_p);
27407 }
27408 /* If we don't know what SCOPE refers to, then we cannot resolve the
27409 TYPENAME_TYPE. */
27410 if (!CLASS_TYPE_P (scope))
27411 return type;
27412 /* If this is a typedef, we don't want to look inside (c++/11987). */
27413 if (typedef_variant_p (type))
27414 return type;
27415 /* If SCOPE isn't the template itself, it will not have a valid
27416 TYPE_FIELDS list. */
27417 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27418 /* scope is either the template itself or a compatible instantiation
27419 like X<T>, so look up the name in the original template. */
27420 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27421 /* If scope has no fields, it can't be a current instantiation. Check this
27422 before currently_open_class to avoid infinite recursion (71515). */
27423 if (!TYPE_FIELDS (scope))
27424 return type;
27425 /* If the SCOPE is not the current instantiation, there's no reason
27426 to look inside it. */
27427 if (only_current_p && !currently_open_class (scope))
27428 return type;
27429 /* Enter the SCOPE so that name lookup will be resolved as if we
27430 were in the class definition. In particular, SCOPE will no
27431 longer be considered a dependent type. */
27432 pushed_scope = push_scope (scope);
27433 /* Look up the declaration. */
27434 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27435 tf_warning_or_error);
27436
27437 result = NULL_TREE;
27438
27439 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27440 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27441 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27442 if (!decl)
27443 /*nop*/;
27444 else if (identifier_p (fullname)
27445 && TREE_CODE (decl) == TYPE_DECL)
27446 {
27447 result = TREE_TYPE (decl);
27448 if (result == error_mark_node)
27449 result = NULL_TREE;
27450 }
27451 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27452 && DECL_CLASS_TEMPLATE_P (decl))
27453 {
27454 /* Obtain the template and the arguments. */
27455 tree tmpl = TREE_OPERAND (fullname, 0);
27456 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27457 {
27458 /* We get here with a plain identifier because a previous tentative
27459 parse of the nested-name-specifier as part of a ptr-operator saw
27460 ::template X<A>. The use of ::template is necessary in a
27461 ptr-operator, but wrong in a declarator-id.
27462
27463 [temp.names]: In a qualified-id of a declarator-id, the keyword
27464 template shall not appear at the top level. */
27465 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27466 "keyword %<template%> not allowed in declarator-id");
27467 tmpl = decl;
27468 }
27469 tree args = TREE_OPERAND (fullname, 1);
27470 /* Instantiate the template. */
27471 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27472 /*entering_scope=*/true,
27473 tf_error | tf_user);
27474 if (result == error_mark_node)
27475 result = NULL_TREE;
27476 }
27477
27478 /* Leave the SCOPE. */
27479 if (pushed_scope)
27480 pop_scope (pushed_scope);
27481
27482 /* If we failed to resolve it, return the original typename. */
27483 if (!result)
27484 return type;
27485
27486 /* If lookup found a typename type, resolve that too. */
27487 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27488 {
27489 /* Ill-formed programs can cause infinite recursion here, so we
27490 must catch that. */
27491 TYPENAME_IS_RESOLVING_P (result) = 1;
27492 result = resolve_typename_type (result, only_current_p);
27493 TYPENAME_IS_RESOLVING_P (result) = 0;
27494 }
27495
27496 /* Qualify the resulting type. */
27497 quals = cp_type_quals (type);
27498 if (quals)
27499 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27500
27501 return result;
27502 }
27503
27504 /* EXPR is an expression which is not type-dependent. Return a proxy
27505 for EXPR that can be used to compute the types of larger
27506 expressions containing EXPR. */
27507
27508 tree
27509 build_non_dependent_expr (tree expr)
27510 {
27511 tree orig_expr = expr;
27512 tree inner_expr;
27513
27514 /* When checking, try to get a constant value for all non-dependent
27515 expressions in order to expose bugs in *_dependent_expression_p
27516 and constexpr. This can affect code generation, see PR70704, so
27517 only do this for -fchecking=2. */
27518 if (flag_checking > 1
27519 && cxx_dialect >= cxx11
27520 /* Don't do this during nsdmi parsing as it can lead to
27521 unexpected recursive instantiations. */
27522 && !parsing_nsdmi ()
27523 /* Don't do this during concept processing either and for
27524 the same reason. */
27525 && !processing_constraint_expression_p ())
27526 fold_non_dependent_expr (expr, tf_none);
27527
27528 STRIP_ANY_LOCATION_WRAPPER (expr);
27529
27530 /* Preserve OVERLOADs; the functions must be available to resolve
27531 types. */
27532 inner_expr = expr;
27533 if (TREE_CODE (inner_expr) == STMT_EXPR)
27534 inner_expr = stmt_expr_value_expr (inner_expr);
27535 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27536 inner_expr = TREE_OPERAND (inner_expr, 0);
27537 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27538 inner_expr = TREE_OPERAND (inner_expr, 1);
27539 if (is_overloaded_fn (inner_expr)
27540 || TREE_CODE (inner_expr) == OFFSET_REF)
27541 return orig_expr;
27542 /* There is no need to return a proxy for a variable or enumerator. */
27543 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27544 return orig_expr;
27545 /* Preserve string constants; conversions from string constants to
27546 "char *" are allowed, even though normally a "const char *"
27547 cannot be used to initialize a "char *". */
27548 if (TREE_CODE (expr) == STRING_CST)
27549 return orig_expr;
27550 /* Preserve void and arithmetic constants, as an optimization -- there is no
27551 reason to create a new node. */
27552 if (TREE_CODE (expr) == VOID_CST
27553 || TREE_CODE (expr) == INTEGER_CST
27554 || TREE_CODE (expr) == REAL_CST)
27555 return orig_expr;
27556 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27557 There is at least one place where we want to know that a
27558 particular expression is a throw-expression: when checking a ?:
27559 expression, there are special rules if the second or third
27560 argument is a throw-expression. */
27561 if (TREE_CODE (expr) == THROW_EXPR)
27562 return orig_expr;
27563
27564 /* Don't wrap an initializer list, we need to be able to look inside. */
27565 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27566 return orig_expr;
27567
27568 /* Don't wrap a dummy object, we need to be able to test for it. */
27569 if (is_dummy_object (expr))
27570 return orig_expr;
27571
27572 if (TREE_CODE (expr) == COND_EXPR)
27573 return build3 (COND_EXPR,
27574 TREE_TYPE (expr),
27575 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27576 (TREE_OPERAND (expr, 1)
27577 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27578 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27579 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27580 if (TREE_CODE (expr) == COMPOUND_EXPR
27581 && !COMPOUND_EXPR_OVERLOADED (expr))
27582 return build2 (COMPOUND_EXPR,
27583 TREE_TYPE (expr),
27584 TREE_OPERAND (expr, 0),
27585 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27586
27587 /* If the type is unknown, it can't really be non-dependent */
27588 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27589
27590 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27591 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27592 TREE_TYPE (expr), expr);
27593 }
27594
27595 /* ARGS is a vector of expressions as arguments to a function call.
27596 Replace the arguments with equivalent non-dependent expressions.
27597 This modifies ARGS in place. */
27598
27599 void
27600 make_args_non_dependent (vec<tree, va_gc> *args)
27601 {
27602 unsigned int ix;
27603 tree arg;
27604
27605 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27606 {
27607 tree newarg = build_non_dependent_expr (arg);
27608 if (newarg != arg)
27609 (*args)[ix] = newarg;
27610 }
27611 }
27612
27613 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27614 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27615 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27616
27617 static tree
27618 make_auto_1 (tree name, bool set_canonical)
27619 {
27620 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27621 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27622 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27623 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27624 (0, processing_template_decl + 1, processing_template_decl + 1,
27625 TYPE_NAME (au), NULL_TREE);
27626 if (set_canonical)
27627 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27628 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27629 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27630 if (name == decltype_auto_identifier)
27631 AUTO_IS_DECLTYPE (au) = true;
27632
27633 return au;
27634 }
27635
27636 tree
27637 make_decltype_auto (void)
27638 {
27639 return make_auto_1 (decltype_auto_identifier, true);
27640 }
27641
27642 tree
27643 make_auto (void)
27644 {
27645 return make_auto_1 (auto_identifier, true);
27646 }
27647
27648 /* Return a C++17 deduction placeholder for class template TMPL. */
27649
27650 tree
27651 make_template_placeholder (tree tmpl)
27652 {
27653 tree t = make_auto_1 (auto_identifier, false);
27654 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27655 /* Our canonical type depends on the placeholder. */
27656 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27657 return t;
27658 }
27659
27660 /* True iff T is a C++17 class template deduction placeholder. */
27661
27662 bool
27663 template_placeholder_p (tree t)
27664 {
27665 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27666 }
27667
27668 /* Make a "constrained auto" type-specifier. This is an auto or
27669 decltype(auto) type with constraints that must be associated after
27670 deduction. The constraint is formed from the given concept CON
27671 and its optional sequence of template arguments ARGS.
27672
27673 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27674
27675 static tree
27676 make_constrained_placeholder_type (tree type, tree con, tree args)
27677 {
27678 /* Build the constraint. */
27679 tree tmpl = DECL_TI_TEMPLATE (con);
27680 tree expr = tmpl;
27681 if (TREE_CODE (con) == FUNCTION_DECL)
27682 expr = ovl_make (tmpl);
27683 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27684
27685 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27686
27687 /* Our canonical type depends on the constraint. */
27688 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27689
27690 /* Attach the constraint to the type declaration. */
27691 return TYPE_NAME (type);
27692 }
27693
27694 /* Make a "constrained auto" type-specifier. */
27695
27696 tree
27697 make_constrained_auto (tree con, tree args)
27698 {
27699 tree type = make_auto_1 (auto_identifier, false);
27700 return make_constrained_placeholder_type (type, con, args);
27701 }
27702
27703 /* Make a "constrained decltype(auto)" type-specifier. */
27704
27705 tree
27706 make_constrained_decltype_auto (tree con, tree args)
27707 {
27708 tree type = make_auto_1 (decltype_auto_identifier, false);
27709 return make_constrained_placeholder_type (type, con, args);
27710 }
27711
27712 /* Build and return a concept definition. Like other templates, the
27713 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27714 the TEMPLATE_DECL. */
27715
27716 tree
27717 finish_concept_definition (cp_expr id, tree init)
27718 {
27719 gcc_assert (identifier_p (id));
27720 gcc_assert (processing_template_decl);
27721
27722 location_t loc = id.get_location();
27723
27724 /* A concept-definition shall not have associated constraints. */
27725 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27726 {
27727 error_at (loc, "a concept cannot be constrained");
27728 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27729 }
27730
27731 /* A concept-definition shall appear in namespace scope. Templates
27732 aren't allowed in block scope, so we only need to check for class
27733 scope. */
27734 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27735 {
27736 error_at (loc, "concept %qE not in namespace scope", *id);
27737 return error_mark_node;
27738 }
27739
27740 /* Initially build the concept declaration; its type is bool. */
27741 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27742 DECL_CONTEXT (decl) = current_scope ();
27743 DECL_INITIAL (decl) = init;
27744
27745 /* Push the enclosing template. */
27746 return push_template_decl (decl);
27747 }
27748
27749 /* Given type ARG, return std::initializer_list<ARG>. */
27750
27751 static tree
27752 listify (tree arg)
27753 {
27754 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27755
27756 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27757 {
27758 gcc_rich_location richloc (input_location);
27759 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27760 error_at (&richloc,
27761 "deducing from brace-enclosed initializer list"
27762 " requires %<#include <initializer_list>%>");
27763
27764 return error_mark_node;
27765 }
27766 tree argvec = make_tree_vec (1);
27767 TREE_VEC_ELT (argvec, 0) = arg;
27768
27769 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27770 NULL_TREE, 0, tf_warning_or_error);
27771 }
27772
27773 /* Replace auto in TYPE with std::initializer_list<auto>. */
27774
27775 static tree
27776 listify_autos (tree type, tree auto_node)
27777 {
27778 tree init_auto = listify (strip_top_quals (auto_node));
27779 tree argvec = make_tree_vec (1);
27780 TREE_VEC_ELT (argvec, 0) = init_auto;
27781 if (processing_template_decl)
27782 argvec = add_to_template_args (current_template_args (), argvec);
27783 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27784 }
27785
27786 /* Hash traits for hashing possibly constrained 'auto'
27787 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27788
27789 struct auto_hash : default_hash_traits<tree>
27790 {
27791 static inline hashval_t hash (tree);
27792 static inline bool equal (tree, tree);
27793 };
27794
27795 /* Hash the 'auto' T. */
27796
27797 inline hashval_t
27798 auto_hash::hash (tree t)
27799 {
27800 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27801 /* Matching constrained-type-specifiers denote the same template
27802 parameter, so hash the constraint. */
27803 return hash_placeholder_constraint (c);
27804 else
27805 /* But unconstrained autos are all separate, so just hash the pointer. */
27806 return iterative_hash_object (t, 0);
27807 }
27808
27809 /* Compare two 'auto's. */
27810
27811 inline bool
27812 auto_hash::equal (tree t1, tree t2)
27813 {
27814 if (t1 == t2)
27815 return true;
27816
27817 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27818 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27819
27820 /* Two unconstrained autos are distinct. */
27821 if (!c1 || !c2)
27822 return false;
27823
27824 return equivalent_placeholder_constraints (c1, c2);
27825 }
27826
27827 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27828 constrained) auto, add it to the vector. */
27829
27830 static int
27831 extract_autos_r (tree t, void *data)
27832 {
27833 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27834 if (is_auto (t))
27835 {
27836 /* All the autos were built with index 0; fix that up now. */
27837 tree *p = hash.find_slot (t, INSERT);
27838 unsigned idx;
27839 if (*p)
27840 /* If this is a repeated constrained-type-specifier, use the index we
27841 chose before. */
27842 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27843 else
27844 {
27845 /* Otherwise this is new, so use the current count. */
27846 *p = t;
27847 idx = hash.elements () - 1;
27848 }
27849 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27850 }
27851
27852 /* Always keep walking. */
27853 return 0;
27854 }
27855
27856 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27857 says they can appear anywhere in the type. */
27858
27859 static tree
27860 extract_autos (tree type)
27861 {
27862 hash_set<tree> visited;
27863 hash_table<auto_hash> hash (2);
27864
27865 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27866
27867 tree tree_vec = make_tree_vec (hash.elements());
27868 for (hash_table<auto_hash>::iterator iter = hash.begin();
27869 iter != hash.end(); ++iter)
27870 {
27871 tree elt = *iter;
27872 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27873 TREE_VEC_ELT (tree_vec, i)
27874 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27875 }
27876
27877 return tree_vec;
27878 }
27879
27880 /* The stem for deduction guide names. */
27881 const char *const dguide_base = "__dguide_";
27882
27883 /* Return the name for a deduction guide for class template TMPL. */
27884
27885 tree
27886 dguide_name (tree tmpl)
27887 {
27888 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27889 tree tname = TYPE_IDENTIFIER (type);
27890 char *buf = (char *) alloca (1 + strlen (dguide_base)
27891 + IDENTIFIER_LENGTH (tname));
27892 memcpy (buf, dguide_base, strlen (dguide_base));
27893 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27894 IDENTIFIER_LENGTH (tname) + 1);
27895 tree dname = get_identifier (buf);
27896 TREE_TYPE (dname) = type;
27897 return dname;
27898 }
27899
27900 /* True if NAME is the name of a deduction guide. */
27901
27902 bool
27903 dguide_name_p (tree name)
27904 {
27905 return (TREE_CODE (name) == IDENTIFIER_NODE
27906 && TREE_TYPE (name)
27907 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27908 strlen (dguide_base)));
27909 }
27910
27911 /* True if FN is a deduction guide. */
27912
27913 bool
27914 deduction_guide_p (const_tree fn)
27915 {
27916 if (DECL_P (fn))
27917 if (tree name = DECL_NAME (fn))
27918 return dguide_name_p (name);
27919 return false;
27920 }
27921
27922 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27923
27924 bool
27925 copy_guide_p (const_tree fn)
27926 {
27927 gcc_assert (deduction_guide_p (fn));
27928 if (!DECL_ARTIFICIAL (fn))
27929 return false;
27930 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27931 return (TREE_CHAIN (parms) == void_list_node
27932 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27933 }
27934
27935 /* True if FN is a guide generated from a constructor template. */
27936
27937 bool
27938 template_guide_p (const_tree fn)
27939 {
27940 gcc_assert (deduction_guide_p (fn));
27941 if (!DECL_ARTIFICIAL (fn))
27942 return false;
27943 tree tmpl = DECL_TI_TEMPLATE (fn);
27944 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27945 return PRIMARY_TEMPLATE_P (org);
27946 return false;
27947 }
27948
27949 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27950 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27951 template parameter types. Note that the handling of template template
27952 parameters relies on current_template_parms being set appropriately for the
27953 new template. */
27954
27955 static tree
27956 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27957 tree tsubst_args, tsubst_flags_t complain)
27958 {
27959 if (olddecl == error_mark_node)
27960 return error_mark_node;
27961
27962 tree oldidx = get_template_parm_index (olddecl);
27963
27964 tree newtype;
27965 if (TREE_CODE (olddecl) == TYPE_DECL
27966 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27967 {
27968 tree oldtype = TREE_TYPE (olddecl);
27969 newtype = cxx_make_type (TREE_CODE (oldtype));
27970 TYPE_MAIN_VARIANT (newtype) = newtype;
27971 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27972 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27973 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27974 }
27975 else
27976 {
27977 newtype = TREE_TYPE (olddecl);
27978 if (type_uses_auto (newtype))
27979 {
27980 // Substitute once to fix references to other template parameters.
27981 newtype = tsubst (newtype, tsubst_args,
27982 complain|tf_partial, NULL_TREE);
27983 // Now substitute again to reduce the level of the auto.
27984 newtype = tsubst (newtype, current_template_args (),
27985 complain, NULL_TREE);
27986 }
27987 else
27988 newtype = tsubst (newtype, tsubst_args,
27989 complain, NULL_TREE);
27990 }
27991
27992 tree newdecl
27993 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27994 DECL_NAME (olddecl), newtype);
27995 SET_DECL_TEMPLATE_PARM_P (newdecl);
27996
27997 tree newidx;
27998 if (TREE_CODE (olddecl) == TYPE_DECL
27999 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28000 {
28001 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28002 = build_template_parm_index (index, level, level,
28003 newdecl, newtype);
28004 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28005 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28006 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28007 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28008 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28009 else
28010 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28011
28012 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28013 {
28014 DECL_TEMPLATE_RESULT (newdecl)
28015 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28016 DECL_NAME (olddecl), newtype);
28017 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28018 // First create a copy (ttargs) of tsubst_args with an
28019 // additional level for the template template parameter's own
28020 // template parameters (ttparms).
28021 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28022 (DECL_TEMPLATE_PARMS (olddecl)));
28023 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28024 tree ttargs = make_tree_vec (depth + 1);
28025 for (int i = 0; i < depth; ++i)
28026 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28027 TREE_VEC_ELT (ttargs, depth)
28028 = template_parms_level_to_args (ttparms);
28029 // Substitute ttargs into ttparms to fix references to
28030 // other template parameters.
28031 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28032 complain|tf_partial);
28033 // Now substitute again with args based on tparms, to reduce
28034 // the level of the ttparms.
28035 ttargs = current_template_args ();
28036 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28037 complain);
28038 // Finally, tack the adjusted parms onto tparms.
28039 ttparms = tree_cons (size_int (depth), ttparms,
28040 current_template_parms);
28041 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28042 }
28043 }
28044 else
28045 {
28046 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28047 tree newconst
28048 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28049 TREE_CODE (oldconst),
28050 DECL_NAME (oldconst), newtype);
28051 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28052 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28053 SET_DECL_TEMPLATE_PARM_P (newconst);
28054 newidx = build_template_parm_index (index, level, level,
28055 newconst, newtype);
28056 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28057 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28058 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28059 }
28060
28061 return newdecl;
28062 }
28063
28064 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28065 template parameter. */
28066
28067 static tree
28068 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28069 tree targs, unsigned targs_index, tsubst_flags_t complain)
28070 {
28071 tree olddecl = TREE_VALUE (oldelt);
28072 tree newdecl = rewrite_template_parm (olddecl, index, level,
28073 targs, complain);
28074 if (newdecl == error_mark_node)
28075 return error_mark_node;
28076 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28077 targs, complain, NULL_TREE);
28078 tree list = build_tree_list (newdef, newdecl);
28079 TEMPLATE_PARM_CONSTRAINTS (list)
28080 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28081 targs, complain, NULL_TREE);
28082 int depth = TMPL_ARGS_DEPTH (targs);
28083 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28084 return list;
28085 }
28086
28087 /* Returns a C++17 class deduction guide template based on the constructor
28088 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28089 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28090 aggregate initialization guide. */
28091
28092 static tree
28093 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28094 {
28095 tree tparms, targs, fparms, fargs, ci;
28096 bool memtmpl = false;
28097 bool explicit_p;
28098 location_t loc;
28099 tree fn_tmpl = NULL_TREE;
28100
28101 if (outer_args)
28102 {
28103 ++processing_template_decl;
28104 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28105 --processing_template_decl;
28106 }
28107
28108 if (!DECL_DECLARES_FUNCTION_P (ctor))
28109 {
28110 if (TYPE_P (ctor))
28111 {
28112 bool copy_p = TYPE_REF_P (ctor);
28113 if (copy_p)
28114 fparms = tree_cons (NULL_TREE, type, void_list_node);
28115 else
28116 fparms = void_list_node;
28117 }
28118 else if (TREE_CODE (ctor) == TREE_LIST)
28119 fparms = ctor;
28120 else
28121 gcc_unreachable ();
28122
28123 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28124 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28125 targs = CLASSTYPE_TI_ARGS (type);
28126 ci = NULL_TREE;
28127 fargs = NULL_TREE;
28128 loc = DECL_SOURCE_LOCATION (ctmpl);
28129 explicit_p = false;
28130 }
28131 else
28132 {
28133 ++processing_template_decl;
28134 bool ok = true;
28135
28136 fn_tmpl
28137 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28138 : DECL_TI_TEMPLATE (ctor));
28139 if (outer_args)
28140 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28141 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28142
28143 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28144 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28145 fully specialized args for the enclosing class. Strip those off, as
28146 the deduction guide won't have those template parameters. */
28147 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28148 TMPL_PARMS_DEPTH (tparms));
28149 /* Discard the 'this' parameter. */
28150 fparms = FUNCTION_ARG_CHAIN (ctor);
28151 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28152 ci = get_constraints (ctor);
28153 loc = DECL_SOURCE_LOCATION (ctor);
28154 explicit_p = DECL_NONCONVERTING_P (ctor);
28155
28156 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28157 {
28158 memtmpl = true;
28159
28160 /* For a member template constructor, we need to flatten the two
28161 template parameter lists into one, and then adjust the function
28162 signature accordingly. This gets...complicated. */
28163 tree save_parms = current_template_parms;
28164
28165 /* For a member template we should have two levels of parms/args, one
28166 for the class and one for the constructor. We stripped
28167 specialized args for further enclosing classes above. */
28168 const int depth = 2;
28169 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28170
28171 /* Template args for translating references to the two-level template
28172 parameters into references to the one-level template parameters we
28173 are creating. */
28174 tree tsubst_args = copy_node (targs);
28175 TMPL_ARGS_LEVEL (tsubst_args, depth)
28176 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28177
28178 /* Template parms for the constructor template. */
28179 tree ftparms = TREE_VALUE (tparms);
28180 unsigned flen = TREE_VEC_LENGTH (ftparms);
28181 /* Template parms for the class template. */
28182 tparms = TREE_CHAIN (tparms);
28183 tree ctparms = TREE_VALUE (tparms);
28184 unsigned clen = TREE_VEC_LENGTH (ctparms);
28185 /* Template parms for the deduction guide start as a copy of the
28186 template parms for the class. We set current_template_parms for
28187 lookup_template_class_1. */
28188 current_template_parms = tparms = copy_node (tparms);
28189 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28190 for (unsigned i = 0; i < clen; ++i)
28191 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28192
28193 /* Now we need to rewrite the constructor parms to append them to the
28194 class parms. */
28195 for (unsigned i = 0; i < flen; ++i)
28196 {
28197 unsigned index = i + clen;
28198 unsigned level = 1;
28199 tree oldelt = TREE_VEC_ELT (ftparms, i);
28200 tree newelt
28201 = rewrite_tparm_list (oldelt, index, level,
28202 tsubst_args, i, complain);
28203 if (newelt == error_mark_node)
28204 ok = false;
28205 TREE_VEC_ELT (new_vec, index) = newelt;
28206 }
28207
28208 /* Now we have a final set of template parms to substitute into the
28209 function signature. */
28210 targs = template_parms_to_args (tparms);
28211 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28212 complain, ctor);
28213 if (fparms == error_mark_node)
28214 ok = false;
28215 if (ci)
28216 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28217
28218 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28219 cp_unevaluated_operand. */
28220 cp_evaluated ev;
28221 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28222 current_template_parms = save_parms;
28223 }
28224
28225 --processing_template_decl;
28226 if (!ok)
28227 return error_mark_node;
28228 }
28229
28230 if (!memtmpl)
28231 {
28232 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28233 tparms = copy_node (tparms);
28234 INNERMOST_TEMPLATE_PARMS (tparms)
28235 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28236 }
28237
28238 tree fntype = build_function_type (type, fparms);
28239 tree ded_fn = build_lang_decl_loc (loc,
28240 FUNCTION_DECL,
28241 dguide_name (type), fntype);
28242 DECL_ARGUMENTS (ded_fn) = fargs;
28243 DECL_ARTIFICIAL (ded_fn) = true;
28244 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28245 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28246 DECL_ARTIFICIAL (ded_tmpl) = true;
28247 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28248 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28249 if (DECL_P (ctor))
28250 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28251 if (ci)
28252 set_constraints (ded_tmpl, ci);
28253
28254 return ded_tmpl;
28255 }
28256
28257 /* Add to LIST the member types for the reshaped initializer CTOR. */
28258
28259 static tree
28260 collect_ctor_idx_types (tree ctor, tree list)
28261 {
28262 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28263 tree idx, val; unsigned i;
28264 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28265 {
28266 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28267 && CONSTRUCTOR_NELTS (val))
28268 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
28269 if (TREE_CODE (subidx) == FIELD_DECL)
28270 {
28271 list = collect_ctor_idx_types (val, list);
28272 continue;
28273 }
28274 tree ftype = finish_decltype_type (idx, true, tf_none);
28275 list = tree_cons (NULL_TREE, ftype, list);
28276 }
28277
28278 return list;
28279 }
28280
28281 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28282
28283 static bool
28284 is_spec_or_derived (tree etype, tree tmpl)
28285 {
28286 if (!etype || !CLASS_TYPE_P (etype))
28287 return false;
28288
28289 tree type = TREE_TYPE (tmpl);
28290 tree tparms = (INNERMOST_TEMPLATE_PARMS
28291 (DECL_TEMPLATE_PARMS (tmpl)));
28292 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28293 int err = unify (tparms, targs, type, etype,
28294 UNIFY_ALLOW_DERIVED, /*explain*/false);
28295 ggc_free (targs);
28296 return !err;
28297 }
28298
28299 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28300 INIT. */
28301
28302 static tree
28303 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28304 {
28305 if (cxx_dialect < cxx20)
28306 return NULL_TREE;
28307
28308 if (init == NULL_TREE)
28309 return NULL_TREE;
28310
28311 tree type = TREE_TYPE (tmpl);
28312 if (!CP_AGGREGATE_TYPE_P (type))
28313 return NULL_TREE;
28314
28315 /* No aggregate candidate for copy-initialization. */
28316 if (args->length() == 1)
28317 {
28318 tree val = (*args)[0];
28319 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28320 return NULL_TREE;
28321 }
28322
28323 /* If we encounter a problem, we just won't add the candidate. */
28324 tsubst_flags_t complain = tf_none;
28325
28326 tree parms = NULL_TREE;
28327 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28328 {
28329 init = reshape_init (type, init, complain);
28330 if (init == error_mark_node)
28331 return NULL_TREE;
28332 parms = collect_ctor_idx_types (init, parms);
28333 }
28334 else if (TREE_CODE (init) == TREE_LIST)
28335 {
28336 int len = list_length (init);
28337 for (tree field = TYPE_FIELDS (type);
28338 len;
28339 --len, field = DECL_CHAIN (field))
28340 {
28341 field = next_initializable_field (field);
28342 if (!field)
28343 return NULL_TREE;
28344 tree ftype = finish_decltype_type (field, true, complain);
28345 parms = tree_cons (NULL_TREE, ftype, parms);
28346 }
28347 }
28348 else
28349 /* Aggregate initialization doesn't apply to an initializer expression. */
28350 return NULL_TREE;
28351
28352 if (parms)
28353 {
28354 tree last = parms;
28355 parms = nreverse (parms);
28356 TREE_CHAIN (last) = void_list_node;
28357 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28358 return guide;
28359 }
28360
28361 return NULL_TREE;
28362 }
28363
28364 /* UGUIDES are the deduction guides for the underlying template of alias
28365 template TMPL; adjust them to be deduction guides for TMPL. */
28366
28367 static tree
28368 alias_ctad_tweaks (tree tmpl, tree uguides)
28369 {
28370 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28371 class type (9.2.8.2) where the template-name names an alias template A,
28372 the defining-type-id of A must be of the form
28373
28374 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28375
28376 as specified in 9.2.8.2. The guides of A are the set of functions or
28377 function templates formed as follows. For each function or function
28378 template f in the guides of the template named by the simple-template-id
28379 of the defining-type-id, the template arguments of the return type of f
28380 are deduced from the defining-type-id of A according to the process in
28381 13.10.2.5 with the exception that deduction does not fail if not all
28382 template arguments are deduced. Let g denote the result of substituting
28383 these deductions into f. If substitution succeeds, form a function or
28384 function template f' with the following properties and add it to the set
28385 of guides of A:
28386
28387 * The function type of f' is the function type of g.
28388
28389 * If f is a function template, f' is a function template whose template
28390 parameter list consists of all the template parameters of A (including
28391 their default template arguments) that appear in the above deductions or
28392 (recursively) in their default template arguments, followed by the
28393 template parameters of f that were not deduced (including their default
28394 template arguments), otherwise f' is not a function template.
28395
28396 * The associated constraints (13.5.2) are the conjunction of the
28397 associated constraints of g and a constraint that is satisfied if and only
28398 if the arguments of A are deducible (see below) from the return type.
28399
28400 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28401 be so as well.
28402
28403 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28404 considered to be so as well.
28405
28406 * The explicit-specifier of f' is the explicit-specifier of g (if
28407 any). */
28408
28409 /* This implementation differs from the above in two significant ways:
28410
28411 1) We include all template parameters of A, not just some.
28412 2) The added constraint is same_type instead of deducible.
28413
28414 I believe that while it's probably possible to construct a testcase that
28415 behaves differently with this simplification, it should have the same
28416 effect for real uses. Including all template parameters means that we
28417 deduce all parameters of A when resolving the call, so when we're in the
28418 constraint we don't need to deduce them again, we can just check whether
28419 the deduction produced the desired result. */
28420
28421 tsubst_flags_t complain = tf_warning_or_error;
28422 tree atype = TREE_TYPE (tmpl);
28423 tree aguides = NULL_TREE;
28424 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28425 unsigned natparms = TREE_VEC_LENGTH (atparms);
28426 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28427 for (ovl_iterator iter (uguides); iter; ++iter)
28428 {
28429 tree f = *iter;
28430 tree in_decl = f;
28431 location_t loc = DECL_SOURCE_LOCATION (f);
28432 tree ret = TREE_TYPE (TREE_TYPE (f));
28433 tree fprime = f;
28434 if (TREE_CODE (f) == TEMPLATE_DECL)
28435 {
28436 processing_template_decl_sentinel ptds (/*reset*/false);
28437 ++processing_template_decl;
28438
28439 /* Deduce template arguments for f from the type-id of A. */
28440 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28441 unsigned len = TREE_VEC_LENGTH (ftparms);
28442 tree targs = make_tree_vec (len);
28443 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28444 gcc_assert (!err);
28445
28446 /* The number of parms for f' is the number of parms for A plus
28447 non-deduced parms of f. */
28448 unsigned ndlen = 0;
28449 unsigned j;
28450 for (unsigned i = 0; i < len; ++i)
28451 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28452 ++ndlen;
28453 tree gtparms = make_tree_vec (natparms + ndlen);
28454
28455 /* First copy over the parms of A. */
28456 for (j = 0; j < natparms; ++j)
28457 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28458 /* Now rewrite the non-deduced parms of f. */
28459 for (unsigned i = 0; ndlen && i < len; ++i)
28460 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28461 {
28462 --ndlen;
28463 unsigned index = j++;
28464 unsigned level = 1;
28465 tree oldlist = TREE_VEC_ELT (ftparms, i);
28466 tree list = rewrite_tparm_list (oldlist, index, level,
28467 targs, i, complain);
28468 TREE_VEC_ELT (gtparms, index) = list;
28469 }
28470 gtparms = build_tree_list (size_one_node, gtparms);
28471
28472 /* Substitute the deduced arguments plus the rewritten template
28473 parameters into f to get g. This covers the type, copyness,
28474 guideness, and explicit-specifier. */
28475 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28476 if (g == error_mark_node)
28477 return error_mark_node;
28478 DECL_USE_TEMPLATE (g) = 0;
28479 fprime = build_template_decl (g, gtparms, false);
28480 DECL_TEMPLATE_RESULT (fprime) = g;
28481 TREE_TYPE (fprime) = TREE_TYPE (g);
28482 tree gtargs = template_parms_to_args (gtparms);
28483 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28484 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28485
28486 /* Substitute the associated constraints. */
28487 tree ci = get_constraints (f);
28488 if (ci)
28489 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28490 if (ci == error_mark_node)
28491 return error_mark_node;
28492
28493 /* Add a constraint that the return type matches the instantiation of
28494 A with the same template arguments. */
28495 ret = TREE_TYPE (TREE_TYPE (fprime));
28496 if (!same_type_p (atype, ret)
28497 /* FIXME this should mean they don't compare as equivalent. */
28498 || dependent_alias_template_spec_p (atype, nt_opaque))
28499 {
28500 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28501 ci = append_constraint (ci, same);
28502 }
28503
28504 if (ci)
28505 set_constraints (fprime, ci);
28506 }
28507 else
28508 {
28509 /* For a non-template deduction guide, if the arguments of A aren't
28510 deducible from the return type, don't add the candidate. */
28511 tree targs = make_tree_vec (natparms);
28512 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28513 for (unsigned i = 0; !err && i < natparms; ++i)
28514 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28515 err = true;
28516 if (err)
28517 continue;
28518 }
28519
28520 aguides = lookup_add (fprime, aguides);
28521 }
28522
28523 return aguides;
28524 }
28525
28526 /* Return artificial deduction guides built from the constructors of class
28527 template TMPL. */
28528
28529 static tree
28530 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28531 {
28532 tree type = TREE_TYPE (tmpl);
28533 tree outer_args = NULL_TREE;
28534 if (DECL_CLASS_SCOPE_P (tmpl)
28535 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28536 {
28537 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28538 type = TREE_TYPE (most_general_template (tmpl));
28539 }
28540
28541 tree cands = NULL_TREE;
28542
28543 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28544 {
28545 /* Skip inherited constructors. */
28546 if (iter.using_p ())
28547 continue;
28548
28549 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28550 cands = lookup_add (guide, cands);
28551 }
28552
28553 /* Add implicit default constructor deduction guide. */
28554 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28555 {
28556 tree guide = build_deduction_guide (type, type, outer_args,
28557 complain);
28558 cands = lookup_add (guide, cands);
28559 }
28560
28561 /* Add copy guide. */
28562 {
28563 tree gtype = build_reference_type (type);
28564 tree guide = build_deduction_guide (type, gtype, outer_args,
28565 complain);
28566 cands = lookup_add (guide, cands);
28567 }
28568
28569 return cands;
28570 }
28571
28572 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28573
28574 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28575 aggregate candidate is added separately because it depends on the
28576 initializer. */
28577
28578 static tree
28579 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28580 {
28581 tree guides = NULL_TREE;
28582 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28583 {
28584 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28585 tree tinfo = get_template_info (under);
28586 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28587 }
28588 else
28589 {
28590 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28591 dguide_name (tmpl),
28592 /*type*/false, /*complain*/false,
28593 /*hidden*/false);
28594 if (guides == error_mark_node)
28595 guides = NULL_TREE;
28596 }
28597
28598 /* Cache the deduction guides for a template. We also remember the result of
28599 lookup, and rebuild everything if it changes; should be very rare. */
28600 tree_pair_p cache = NULL;
28601 if (tree_pair_p &r
28602 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28603 {
28604 cache = r;
28605 if (cache->purpose == guides)
28606 return cache->value;
28607 }
28608 else
28609 {
28610 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28611 cache->purpose = guides;
28612 }
28613
28614 tree cands = NULL_TREE;
28615 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28616 cands = alias_ctad_tweaks (tmpl, guides);
28617 else
28618 {
28619 cands = ctor_deduction_guides_for (tmpl, complain);
28620 for (ovl_iterator it (guides); it; ++it)
28621 cands = lookup_add (*it, cands);
28622 }
28623
28624 cache->value = cands;
28625 return cands;
28626 }
28627
28628 /* Return whether TMPL is a (class template argument-) deducible template. */
28629
28630 bool
28631 ctad_template_p (tree tmpl)
28632 {
28633 /* A deducible template is either a class template or is an alias template
28634 whose defining-type-id is of the form
28635
28636 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28637
28638 where the nested-name-specifier (if any) is non-dependent and the
28639 template-name of the simple-template-id names a deducible template. */
28640
28641 if (DECL_CLASS_TEMPLATE_P (tmpl)
28642 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28643 return true;
28644 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28645 return false;
28646 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28647 if (tree tinfo = get_template_info (orig))
28648 return ctad_template_p (TI_TEMPLATE (tinfo));
28649 return false;
28650 }
28651
28652 /* Deduce template arguments for the class template placeholder PTYPE for
28653 template TMPL based on the initializer INIT, and return the resulting
28654 type. */
28655
28656 static tree
28657 do_class_deduction (tree ptype, tree tmpl, tree init,
28658 int flags, tsubst_flags_t complain)
28659 {
28660 /* We should have handled this in the caller. */
28661 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28662 return ptype;
28663
28664 /* Look through alias templates that just rename another template. */
28665 tmpl = get_underlying_template (tmpl);
28666 if (!ctad_template_p (tmpl))
28667 {
28668 if (complain & tf_error)
28669 error ("non-deducible template %qT used without template arguments", tmpl);
28670 return error_mark_node;
28671 }
28672 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28673 {
28674 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28675 if (complain & tf_warning_or_error)
28676 pedwarn (input_location, 0, "alias template deduction only available "
28677 "with %<-std=c++20%> or %<-std=gnu++20%>");
28678 }
28679
28680 if (init && TREE_TYPE (init) == ptype)
28681 /* Using the template parm as its own argument. */
28682 return ptype;
28683
28684 tree type = TREE_TYPE (tmpl);
28685
28686 bool try_list_ctor = false;
28687
28688 releasing_vec rv_args = NULL;
28689 vec<tree,va_gc> *&args = *&rv_args;
28690 if (init == NULL_TREE)
28691 args = make_tree_vector ();
28692 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28693 {
28694 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28695 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28696 {
28697 /* As an exception, the first phase in 16.3.1.7 (considering the
28698 initializer list as a single argument) is omitted if the
28699 initializer list consists of a single expression of type cv U,
28700 where U is a specialization of C or a class derived from a
28701 specialization of C. */
28702 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28703 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28704 try_list_ctor = false;
28705 }
28706 if (try_list_ctor || is_std_init_list (type))
28707 args = make_tree_vector_single (init);
28708 else
28709 args = make_tree_vector_from_ctor (init);
28710 }
28711 else if (TREE_CODE (init) == TREE_LIST)
28712 args = make_tree_vector_from_list (init);
28713 else
28714 args = make_tree_vector_single (init);
28715
28716 /* Do this now to avoid problems with erroneous args later on. */
28717 args = resolve_args (args, complain);
28718 if (args == NULL)
28719 return error_mark_node;
28720
28721 tree cands = deduction_guides_for (tmpl, complain);
28722 if (cands == error_mark_node)
28723 return error_mark_node;
28724
28725 /* Prune explicit deduction guides in copy-initialization context. */
28726 bool elided = false;
28727 if (flags & LOOKUP_ONLYCONVERTING)
28728 {
28729 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28730 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28731 elided = true;
28732
28733 if (elided)
28734 {
28735 /* Found a nonconverting guide, prune the candidates. */
28736 tree pruned = NULL_TREE;
28737 for (lkp_iterator iter (cands); iter; ++iter)
28738 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28739 pruned = lookup_add (*iter, pruned);
28740
28741 cands = pruned;
28742 }
28743 }
28744
28745 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28746 cands = lookup_add (guide, cands);
28747
28748 tree call = error_mark_node;
28749
28750 /* If this is list-initialization and the class has a list constructor, first
28751 try deducing from the list as a single argument, as [over.match.list]. */
28752 tree list_cands = NULL_TREE;
28753 if (try_list_ctor && cands)
28754 for (lkp_iterator iter (cands); iter; ++iter)
28755 {
28756 tree dg = *iter;
28757 if (is_list_ctor (dg))
28758 list_cands = lookup_add (dg, list_cands);
28759 }
28760 if (list_cands)
28761 {
28762 ++cp_unevaluated_operand;
28763 call = build_new_function_call (list_cands, &args, tf_decltype);
28764 --cp_unevaluated_operand;
28765
28766 if (call == error_mark_node)
28767 {
28768 /* That didn't work, now try treating the list as a sequence of
28769 arguments. */
28770 release_tree_vector (args);
28771 args = make_tree_vector_from_ctor (init);
28772 }
28773 }
28774
28775 if (elided && !cands)
28776 {
28777 error ("cannot deduce template arguments for copy-initialization"
28778 " of %qT, as it has no non-explicit deduction guides or "
28779 "user-declared constructors", type);
28780 return error_mark_node;
28781 }
28782 else if (!cands && call == error_mark_node)
28783 {
28784 error ("cannot deduce template arguments of %qT, as it has no viable "
28785 "deduction guides", type);
28786 return error_mark_node;
28787 }
28788
28789 if (call == error_mark_node)
28790 {
28791 ++cp_unevaluated_operand;
28792 call = build_new_function_call (cands, &args, tf_decltype);
28793 --cp_unevaluated_operand;
28794 }
28795
28796 if (call == error_mark_node
28797 && (complain & tf_warning_or_error))
28798 {
28799 error ("class template argument deduction failed:");
28800
28801 ++cp_unevaluated_operand;
28802 call = build_new_function_call (cands, &args, complain | tf_decltype);
28803 --cp_unevaluated_operand;
28804
28805 if (elided)
28806 inform (input_location, "explicit deduction guides not considered "
28807 "for copy-initialization");
28808 }
28809
28810 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28811 }
28812
28813 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28814 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28815 The CONTEXT determines the context in which auto deduction is performed
28816 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28817 OUTER_TARGS are used during template argument deduction
28818 (context == adc_unify) to properly substitute the result, and is ignored
28819 in other contexts.
28820
28821 For partial-concept-ids, extra args may be appended to the list of deduced
28822 template arguments prior to determining constraint satisfaction. */
28823
28824 tree
28825 do_auto_deduction (tree type, tree init, tree auto_node,
28826 tsubst_flags_t complain, auto_deduction_context context,
28827 tree outer_targs, int flags)
28828 {
28829 tree targs;
28830
28831 if (init == error_mark_node)
28832 return error_mark_node;
28833
28834 if (init && type_dependent_expression_p (init)
28835 && context != adc_unify)
28836 /* Defining a subset of type-dependent expressions that we can deduce
28837 from ahead of time isn't worth the trouble. */
28838 return type;
28839
28840 /* Similarly, we can't deduce from another undeduced decl. */
28841 if (init && undeduced_auto_decl (init))
28842 return type;
28843
28844 /* We may be doing a partial substitution, but we still want to replace
28845 auto_node. */
28846 complain &= ~tf_partial;
28847
28848 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28849 /* C++17 class template argument deduction. */
28850 return do_class_deduction (type, tmpl, init, flags, complain);
28851
28852 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28853 /* Nothing we can do with this, even in deduction context. */
28854 return type;
28855
28856 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28857 with either a new invented type template parameter U or, if the
28858 initializer is a braced-init-list (8.5.4), with
28859 std::initializer_list<U>. */
28860 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28861 {
28862 if (!DIRECT_LIST_INIT_P (init))
28863 type = listify_autos (type, auto_node);
28864 else if (CONSTRUCTOR_NELTS (init) == 1)
28865 init = CONSTRUCTOR_ELT (init, 0)->value;
28866 else
28867 {
28868 if (complain & tf_warning_or_error)
28869 {
28870 if (permerror (input_location, "direct-list-initialization of "
28871 "%<auto%> requires exactly one element"))
28872 inform (input_location,
28873 "for deduction to %<std::initializer_list%>, use copy-"
28874 "list-initialization (i.e. add %<=%> before the %<{%>)");
28875 }
28876 type = listify_autos (type, auto_node);
28877 }
28878 }
28879
28880 if (type == error_mark_node)
28881 return error_mark_node;
28882
28883 init = resolve_nondeduced_context (init, complain);
28884
28885 if (context == adc_decomp_type
28886 && auto_node == type
28887 && init != error_mark_node
28888 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28889 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28890 and initializer has array type, deduce cv-qualified array type. */
28891 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28892 complain);
28893 else if (AUTO_IS_DECLTYPE (auto_node))
28894 {
28895 tree stripped_init = tree_strip_any_location_wrapper (init);
28896 bool id = (DECL_P (stripped_init)
28897 || ((TREE_CODE (init) == COMPONENT_REF
28898 || TREE_CODE (init) == SCOPE_REF)
28899 && !REF_PARENTHESIZED_P (init)));
28900 targs = make_tree_vec (1);
28901 TREE_VEC_ELT (targs, 0)
28902 = finish_decltype_type (init, id, tf_warning_or_error);
28903 if (type != auto_node)
28904 {
28905 if (complain & tf_error)
28906 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28907 return error_mark_node;
28908 }
28909 }
28910 else
28911 {
28912 if (error_operand_p (init))
28913 return error_mark_node;
28914
28915 tree parms = build_tree_list (NULL_TREE, type);
28916 tree tparms;
28917
28918 if (flag_concepts)
28919 tparms = extract_autos (type);
28920 else
28921 {
28922 tparms = make_tree_vec (1);
28923 TREE_VEC_ELT (tparms, 0)
28924 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28925 }
28926
28927 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28928 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28929 DEDUCE_CALL,
28930 NULL, /*explain_p=*/false);
28931 if (val > 0)
28932 {
28933 if (processing_template_decl)
28934 /* Try again at instantiation time. */
28935 return type;
28936 if (type && type != error_mark_node
28937 && (complain & tf_error))
28938 /* If type is error_mark_node a diagnostic must have been
28939 emitted by now. Also, having a mention to '<type error>'
28940 in the diagnostic is not really useful to the user. */
28941 {
28942 if (cfun
28943 && FNDECL_USED_AUTO (current_function_decl)
28944 && (auto_node
28945 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28946 && LAMBDA_FUNCTION_P (current_function_decl))
28947 error ("unable to deduce lambda return type from %qE", init);
28948 else
28949 error ("unable to deduce %qT from %qE", type, init);
28950 type_unification_real (tparms, targs, parms, &init, 1, 0,
28951 DEDUCE_CALL,
28952 NULL, /*explain_p=*/true);
28953 }
28954 return error_mark_node;
28955 }
28956 }
28957
28958 /* Check any placeholder constraints against the deduced type. */
28959 if (flag_concepts && !processing_template_decl)
28960 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28961 {
28962 /* Use the deduced type to check the associated constraints. If we
28963 have a partial-concept-id, rebuild the argument list so that
28964 we check using the extra arguments. */
28965 check = unpack_concept_check (check);
28966 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28967 tree cdecl = TREE_OPERAND (check, 0);
28968 if (OVL_P (cdecl))
28969 cdecl = OVL_FIRST (cdecl);
28970 tree cargs = TREE_OPERAND (check, 1);
28971 if (TREE_VEC_LENGTH (cargs) > 1)
28972 {
28973 cargs = copy_node (cargs);
28974 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28975 }
28976 else
28977 cargs = targs;
28978
28979 /* Rebuild the check using the deduced arguments. */
28980 check = build_concept_check (cdecl, cargs, tf_none);
28981
28982 if (!constraints_satisfied_p (check))
28983 {
28984 if (complain & tf_warning_or_error)
28985 {
28986 auto_diagnostic_group d;
28987 switch (context)
28988 {
28989 case adc_unspecified:
28990 case adc_unify:
28991 error("placeholder constraints not satisfied");
28992 break;
28993 case adc_variable_type:
28994 case adc_decomp_type:
28995 error ("deduced initializer does not satisfy "
28996 "placeholder constraints");
28997 break;
28998 case adc_return_type:
28999 error ("deduced return type does not satisfy "
29000 "placeholder constraints");
29001 break;
29002 case adc_requirement:
29003 error ("deduced expression type does not satisfy "
29004 "placeholder constraints");
29005 break;
29006 }
29007 diagnose_constraints (input_location, check, targs);
29008 }
29009 return error_mark_node;
29010 }
29011 }
29012
29013 if (processing_template_decl && context != adc_unify)
29014 outer_targs = current_template_args ();
29015 targs = add_to_template_args (outer_targs, targs);
29016 return tsubst (type, targs, complain, NULL_TREE);
29017 }
29018
29019 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29020 result. */
29021
29022 tree
29023 splice_late_return_type (tree type, tree late_return_type)
29024 {
29025 if (late_return_type)
29026 {
29027 gcc_assert (is_auto (type) || seen_error ());
29028 return late_return_type;
29029 }
29030
29031 if (tree *auto_node = find_type_usage (&type, is_auto))
29032 {
29033 tree idx = get_template_parm_index (*auto_node);
29034 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29035 {
29036 /* In an abbreviated function template we didn't know we were dealing
29037 with a function template when we saw the auto return type, so update
29038 it to have the correct level. */
29039 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29040 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29041 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29042 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29043 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29044 *auto_node = new_auto;
29045 }
29046 }
29047 return type;
29048 }
29049
29050 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29051 'decltype(auto)' or a deduced class template. */
29052
29053 bool
29054 is_auto (const_tree type)
29055 {
29056 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29057 && (TYPE_IDENTIFIER (type) == auto_identifier
29058 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29059 return true;
29060 else
29061 return false;
29062 }
29063
29064 /* for_each_template_parm callback for type_uses_auto. */
29065
29066 int
29067 is_auto_r (tree tp, void */*data*/)
29068 {
29069 return is_auto (tp);
29070 }
29071
29072 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29073 a use of `auto'. Returns NULL_TREE otherwise. */
29074
29075 tree
29076 type_uses_auto (tree type)
29077 {
29078 if (type == NULL_TREE)
29079 return NULL_TREE;
29080 else if (flag_concepts)
29081 {
29082 /* The Concepts TS allows multiple autos in one type-specifier; just
29083 return the first one we find, do_auto_deduction will collect all of
29084 them. */
29085 if (uses_template_parms (type))
29086 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29087 /*visited*/NULL, /*nondeduced*/false);
29088 else
29089 return NULL_TREE;
29090 }
29091 else if (tree *tp = find_type_usage (&type, is_auto))
29092 return *tp;
29093 else
29094 return NULL_TREE;
29095 }
29096
29097 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29098 concepts are enabled, auto is acceptable in template arguments, but
29099 only when TEMPL identifies a template class. Return TRUE if any
29100 such errors were reported. */
29101
29102 bool
29103 check_auto_in_tmpl_args (tree tmpl, tree args)
29104 {
29105 /* If there were previous errors, nevermind. */
29106 if (!args || TREE_CODE (args) != TREE_VEC)
29107 return false;
29108
29109 /* If TMPL is an identifier, we're parsing and we can't tell yet
29110 whether TMPL is supposed to be a type, a function or a variable.
29111 We'll only be able to tell during template substitution, so we
29112 expect to be called again then. If concepts are enabled and we
29113 know we have a type, we're ok. */
29114 if (flag_concepts
29115 && (identifier_p (tmpl)
29116 || (DECL_P (tmpl)
29117 && (DECL_TYPE_TEMPLATE_P (tmpl)
29118 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29119 return false;
29120
29121 /* Quickly search for any occurrences of auto; usually there won't
29122 be any, and then we'll avoid allocating the vector. */
29123 if (!type_uses_auto (args))
29124 return false;
29125
29126 bool errors = false;
29127
29128 tree vec = extract_autos (args);
29129 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29130 {
29131 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29132 error_at (DECL_SOURCE_LOCATION (xauto),
29133 "invalid use of %qT in template argument", xauto);
29134 errors = true;
29135 }
29136
29137 return errors;
29138 }
29139
29140 /* For a given template T, return the vector of typedefs referenced
29141 in T for which access check is needed at T instantiation time.
29142 T is either a FUNCTION_DECL or a RECORD_TYPE.
29143 Those typedefs were added to T by the function
29144 append_type_to_template_for_access_check. */
29145
29146 vec<qualified_typedef_usage_t, va_gc> *
29147 get_types_needing_access_check (tree t)
29148 {
29149 gcc_checking_assert ((CLASS_TYPE_P (t) || TREE_CODE (t) == FUNCTION_DECL));
29150
29151 if (tree ti = get_template_info (t))
29152 if (TI_TEMPLATE (ti))
29153 return TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
29154
29155 return NULL;
29156 }
29157
29158 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29159 tied to T. That list of typedefs will be access checked at
29160 T instantiation time.
29161 T is either a FUNCTION_DECL or a RECORD_TYPE.
29162 TYPE_DECL is a TYPE_DECL node representing a typedef.
29163 SCOPE is the scope through which TYPE_DECL is accessed.
29164 LOCATION is the location of the usage point of TYPE_DECL.
29165
29166 This function is a subroutine of
29167 append_type_to_template_for_access_check. */
29168
29169 static void
29170 append_type_to_template_for_access_check_1 (tree t,
29171 tree type_decl,
29172 tree scope,
29173 location_t location)
29174 {
29175 qualified_typedef_usage_t typedef_usage;
29176 tree ti;
29177
29178 if (!t || t == error_mark_node)
29179 return;
29180
29181 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
29182 || CLASS_TYPE_P (t))
29183 && type_decl
29184 && TREE_CODE (type_decl) == TYPE_DECL
29185 && scope);
29186
29187 if (!(ti = get_template_info (t)))
29188 return;
29189
29190 gcc_assert (TI_TEMPLATE (ti));
29191
29192 typedef_usage.typedef_decl = type_decl;
29193 typedef_usage.context = scope;
29194 typedef_usage.locus = location;
29195
29196 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
29197 }
29198
29199 /* Append TYPE_DECL to the template TEMPL.
29200 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29201 At TEMPL instanciation time, TYPE_DECL will be checked to see
29202 if it can be accessed through SCOPE.
29203 LOCATION is the location of the usage point of TYPE_DECL.
29204
29205 e.g. consider the following code snippet:
29206
29207 class C
29208 {
29209 typedef int myint;
29210 };
29211
29212 template<class U> struct S
29213 {
29214 C::myint mi; // <-- usage point of the typedef C::myint
29215 };
29216
29217 S<char> s;
29218
29219 At S<char> instantiation time, we need to check the access of C::myint
29220 In other words, we need to check the access of the myint typedef through
29221 the C scope. For that purpose, this function will add the myint typedef
29222 and the scope C through which its being accessed to a list of typedefs
29223 tied to the template S. That list will be walked at template instantiation
29224 time and access check performed on each typedefs it contains.
29225 Note that this particular code snippet should yield an error because
29226 myint is private to C. */
29227
29228 void
29229 append_type_to_template_for_access_check (tree templ,
29230 tree type_decl,
29231 tree scope,
29232 location_t location)
29233 {
29234 qualified_typedef_usage_t *iter;
29235 unsigned i;
29236
29237 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
29238
29239 /* Make sure we don't append the type to the template twice. */
29240 if (vec<qualified_typedef_usage_t, va_gc> *tdefs
29241 = get_types_needing_access_check (templ))
29242 FOR_EACH_VEC_ELT (*tdefs, i, iter)
29243 if (iter->typedef_decl == type_decl && scope == iter->context)
29244 return;
29245
29246 append_type_to_template_for_access_check_1 (templ, type_decl,
29247 scope, location);
29248 }
29249
29250 /* Recursively walk over && expressions searching for EXPR. Return a reference
29251 to that expression. */
29252
29253 static tree *find_template_requirement (tree *t, tree key)
29254 {
29255 if (*t == key)
29256 return t;
29257 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29258 {
29259 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29260 return p;
29261 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29262 return p;
29263 }
29264 return 0;
29265 }
29266
29267 /* Convert the generic type parameters in PARM that match the types given in the
29268 range [START_IDX, END_IDX) from the current_template_parms into generic type
29269 packs. */
29270
29271 tree
29272 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29273 {
29274 tree current = current_template_parms;
29275 int depth = TMPL_PARMS_DEPTH (current);
29276 current = INNERMOST_TEMPLATE_PARMS (current);
29277 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29278
29279 for (int i = 0; i < start_idx; ++i)
29280 TREE_VEC_ELT (replacement, i)
29281 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29282
29283 for (int i = start_idx; i < end_idx; ++i)
29284 {
29285 /* Create a distinct parameter pack type from the current parm and add it
29286 to the replacement args to tsubst below into the generic function
29287 parameter. */
29288 tree node = TREE_VEC_ELT (current, i);
29289 tree o = TREE_TYPE (TREE_VALUE (node));
29290 tree t = copy_type (o);
29291 TEMPLATE_TYPE_PARM_INDEX (t)
29292 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29293 t, 0, 0, tf_none);
29294 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29295 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29296 TYPE_MAIN_VARIANT (t) = t;
29297 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29298 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29299 TREE_VEC_ELT (replacement, i) = t;
29300
29301 /* Replace the current template parameter with new pack. */
29302 TREE_VALUE (node) = TREE_CHAIN (t);
29303
29304 /* Surgically adjust the associated constraint of adjusted parameter
29305 and it's corresponding contribution to the current template
29306 requirements. */
29307 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29308 {
29309 tree id = unpack_concept_check (constr);
29310 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29311 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29312 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29313
29314 /* If there was a constraint, we also need to replace that in
29315 the template requirements, which we've already built. */
29316 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29317 reqs = find_template_requirement (reqs, constr);
29318 *reqs = fold;
29319 }
29320 }
29321
29322 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29323 TREE_VEC_ELT (replacement, i)
29324 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29325
29326 /* If there are more levels then build up the replacement with the outer
29327 template parms. */
29328 if (depth > 1)
29329 replacement = add_to_template_args (template_parms_to_args
29330 (TREE_CHAIN (current_template_parms)),
29331 replacement);
29332
29333 return tsubst (parm, replacement, tf_none, NULL_TREE);
29334 }
29335
29336 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29337 0..N-1. */
29338
29339 void
29340 declare_integer_pack (void)
29341 {
29342 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29343 build_function_type_list (integer_type_node,
29344 integer_type_node,
29345 NULL_TREE),
29346 NULL_TREE, ECF_CONST);
29347 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29348 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29349 CP_BUILT_IN_INTEGER_PACK);
29350 }
29351
29352 /* Set up the hash tables for template instantiations. */
29353
29354 void
29355 init_template_processing (void)
29356 {
29357 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29358 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29359
29360 if (cxx_dialect >= cxx11)
29361 declare_integer_pack ();
29362 }
29363
29364 /* Print stats about the template hash tables for -fstats. */
29365
29366 void
29367 print_template_statistics (void)
29368 {
29369 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29370 "%f collisions\n", (long) decl_specializations->size (),
29371 (long) decl_specializations->elements (),
29372 decl_specializations->collisions ());
29373 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29374 "%f collisions\n", (long) type_specializations->size (),
29375 (long) type_specializations->elements (),
29376 type_specializations->collisions ());
29377 }
29378
29379 #if CHECKING_P
29380
29381 namespace selftest {
29382
29383 /* Verify that build_non_dependent_expr () works, for various expressions,
29384 and that location wrappers don't affect the results. */
29385
29386 static void
29387 test_build_non_dependent_expr ()
29388 {
29389 location_t loc = BUILTINS_LOCATION;
29390
29391 /* Verify constants, without and with location wrappers. */
29392 tree int_cst = build_int_cst (integer_type_node, 42);
29393 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29394
29395 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29396 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29397 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29398
29399 tree string_lit = build_string (4, "foo");
29400 TREE_TYPE (string_lit) = char_array_type_node;
29401 string_lit = fix_string_type (string_lit);
29402 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29403
29404 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29405 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29406 ASSERT_EQ (wrapped_string_lit,
29407 build_non_dependent_expr (wrapped_string_lit));
29408 }
29409
29410 /* Verify that type_dependent_expression_p () works correctly, even
29411 in the presence of location wrapper nodes. */
29412
29413 static void
29414 test_type_dependent_expression_p ()
29415 {
29416 location_t loc = BUILTINS_LOCATION;
29417
29418 tree name = get_identifier ("foo");
29419
29420 /* If no templates are involved, nothing is type-dependent. */
29421 gcc_assert (!processing_template_decl);
29422 ASSERT_FALSE (type_dependent_expression_p (name));
29423
29424 ++processing_template_decl;
29425
29426 /* Within a template, an unresolved name is always type-dependent. */
29427 ASSERT_TRUE (type_dependent_expression_p (name));
29428
29429 /* Ensure it copes with NULL_TREE and errors. */
29430 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29431 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29432
29433 /* A USING_DECL in a template should be type-dependent, even if wrapped
29434 with a location wrapper (PR c++/83799). */
29435 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29436 TREE_TYPE (using_decl) = integer_type_node;
29437 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29438 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29439 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29440 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29441
29442 --processing_template_decl;
29443 }
29444
29445 /* Run all of the selftests within this file. */
29446
29447 void
29448 cp_pt_c_tests ()
29449 {
29450 test_build_non_dependent_expr ();
29451 test_type_dependent_expression_p ();
29452 }
29453
29454 } // namespace selftest
29455
29456 #endif /* #if CHECKING_P */
29457
29458 #include "gt-cp-pt.h"