]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.c
c++: alias template and parameter packs (PR91966).
[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 TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet 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_blank || !saved)
87 local_specializations = new hash_map<tree, tree>;
88 else
89 local_specializations = new hash_map<tree, tree>(*saved);
90 }
91
92 local_specialization_stack::~local_specialization_stack ()
93 {
94 delete local_specializations;
95 local_specializations = saved;
96 }
97
98 /* True if we've recursed into fn_type_unification too many times. */
99 static bool excessive_deduction_depth;
100
101 struct GTY((for_user)) spec_entry
102 {
103 tree tmpl;
104 tree args;
105 tree spec;
106 };
107
108 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 {
110 static hashval_t hash (spec_entry *);
111 static bool equal (spec_entry *, spec_entry *);
112 };
113
114 static GTY (()) hash_table<spec_hasher> *decl_specializations;
115
116 static GTY (()) hash_table<spec_hasher> *type_specializations;
117
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
123
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132
133 enum template_base_result {
134 tbr_incomplete_type,
135 tbr_ambiguous_baseclass,
136 tbr_success
137 };
138
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int check_non_deducible_conversion (tree, tree, int, int,
157 struct conversion **, bool);
158 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
159 tree);
160 static int type_unification_real (tree, tree, tree, const tree *,
161 unsigned int, int, unification_kind_t,
162 vec<deferred_access_check, va_gc> **,
163 bool);
164 static void note_template_header (int);
165 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
166 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
167 static tree convert_template_argument (tree, tree, tree,
168 tsubst_flags_t, int, tree);
169 static tree for_each_template_parm (tree, tree_fn_t, void*,
170 hash_set<tree> *, bool, tree_fn_t = NULL);
171 static tree expand_template_argument_pack (tree);
172 static tree build_template_parm_index (int, int, int, tree, tree);
173 static bool inline_needs_template_parms (tree, bool);
174 static void push_inline_template_parms_recursive (tree, int);
175 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
176 static int mark_template_parm (tree, void *);
177 static int template_parm_this_level_p (tree, void *);
178 static tree tsubst_friend_function (tree, tree);
179 static tree tsubst_friend_class (tree, tree);
180 static int can_complete_type_without_circularity (tree);
181 static tree get_bindings (tree, tree, tree, bool);
182 static int template_decl_level (tree);
183 static int check_cv_quals_for_unify (int, tree, tree);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
188 tree most_specialized_partial_spec (tree, tsubst_flags_t);
189 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
190 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
192 static bool check_specialization_scope (void);
193 static tree process_partial_specialization (tree);
194 static void set_current_access_from_decl (tree);
195 static enum template_base_result get_template_base (tree, tree, tree, tree,
196 bool , tree *);
197 static tree try_class_unification (tree, tree, tree, tree, bool);
198 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
199 tree, tree);
200 static bool template_template_parm_bindings_ok_p (tree, tree);
201 static void tsubst_default_arguments (tree, tsubst_flags_t);
202 static tree for_each_template_parm_r (tree *, int *, void *);
203 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
204 static void copy_default_args_to_explicit_spec (tree);
205 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
206 static bool dependent_template_arg_p (tree);
207 static bool any_template_arguments_need_structural_equality_p (tree);
208 static bool dependent_type_p_r (tree);
209 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
210 static tree tsubst_decl (tree, tree, tsubst_flags_t);
211 static void perform_typedefs_access_check (tree tmpl, tree targs);
212 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
213 location_t);
214 static tree listify (tree);
215 static tree listify_autos (tree, tree);
216 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
217 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
218 static bool complex_alias_template_p (const_tree tmpl);
219 static tree get_underlying_template (tree);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
224 static tree enclosing_instantiation_of (tree tctx);
225
226 /* Make the current scope suitable for access checking when we are
227 processing T. T can be FUNCTION_DECL for instantiated function
228 template, VAR_DECL for static member variable, or TYPE_DECL for
229 alias template (needed by instantiate_decl). */
230
231 void
232 push_access_scope (tree t)
233 {
234 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
235 || TREE_CODE (t) == TYPE_DECL);
236
237 if (DECL_FRIEND_CONTEXT (t))
238 push_nested_class (DECL_FRIEND_CONTEXT (t));
239 else if (DECL_CLASS_SCOPE_P (t))
240 push_nested_class (DECL_CONTEXT (t));
241 else
242 push_to_top_level ();
243
244 if (TREE_CODE (t) == FUNCTION_DECL)
245 {
246 vec_safe_push (saved_access_scope, current_function_decl);
247 current_function_decl = t;
248 }
249 }
250
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
253
254 void
255 pop_access_scope (tree t)
256 {
257 if (TREE_CODE (t) == FUNCTION_DECL)
258 current_function_decl = saved_access_scope->pop();
259
260 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
261 pop_nested_class ();
262 else
263 pop_from_top_level ();
264 }
265
266 /* Do any processing required when DECL (a member template
267 declaration) is finished. Returns the TEMPLATE_DECL corresponding
268 to DECL, unless it is a specialization, in which case the DECL
269 itself is returned. */
270
271 tree
272 finish_member_template_decl (tree decl)
273 {
274 if (decl == error_mark_node)
275 return error_mark_node;
276
277 gcc_assert (DECL_P (decl));
278
279 if (TREE_CODE (decl) == TYPE_DECL)
280 {
281 tree type;
282
283 type = TREE_TYPE (decl);
284 if (type == error_mark_node)
285 return error_mark_node;
286 if (MAYBE_CLASS_TYPE_P (type)
287 && CLASSTYPE_TEMPLATE_INFO (type)
288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
289 {
290 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
291 check_member_template (tmpl);
292 return tmpl;
293 }
294 return NULL_TREE;
295 }
296 else if (TREE_CODE (decl) == FIELD_DECL)
297 error_at (DECL_SOURCE_LOCATION (decl),
298 "data member %qD cannot be a member template", decl);
299 else if (DECL_TEMPLATE_INFO (decl))
300 {
301 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
302 {
303 check_member_template (DECL_TI_TEMPLATE (decl));
304 return DECL_TI_TEMPLATE (decl);
305 }
306 else
307 return decl;
308 }
309 else
310 error_at (DECL_SOURCE_LOCATION (decl),
311 "invalid member template declaration %qD", decl);
312
313 return error_mark_node;
314 }
315
316 /* Create a template info node. */
317
318 tree
319 build_template_info (tree template_decl, tree template_args)
320 {
321 tree result = make_node (TEMPLATE_INFO);
322 TI_TEMPLATE (result) = template_decl;
323 TI_ARGS (result) = template_args;
324 return result;
325 }
326
327 /* Return the template info node corresponding to T, whatever T is. */
328
329 tree
330 get_template_info (const_tree t)
331 {
332 tree tinfo = NULL_TREE;
333
334 if (!t || t == error_mark_node)
335 return NULL;
336
337 if (TREE_CODE (t) == NAMESPACE_DECL
338 || TREE_CODE (t) == PARM_DECL)
339 return NULL;
340
341 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
342 tinfo = DECL_TEMPLATE_INFO (t);
343
344 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
345 t = TREE_TYPE (t);
346
347 if (OVERLOAD_TYPE_P (t))
348 tinfo = TYPE_TEMPLATE_INFO (t);
349 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
350 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
351
352 return tinfo;
353 }
354
355 /* Returns the template nesting level of the indicated class TYPE.
356
357 For example, in:
358 template <class T>
359 struct A
360 {
361 template <class U>
362 struct B {};
363 };
364
365 A<T>::B<U> has depth two, while A<T> has depth one.
366 Both A<T>::B<int> and A<int>::B<U> have depth one, if
367 they are instantiations, not specializations.
368
369 This function is guaranteed to return 0 if passed NULL_TREE so
370 that, for example, `template_class_depth (current_class_type)' is
371 always safe. */
372
373 int
374 template_class_depth (tree type)
375 {
376 int depth;
377
378 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
379 {
380 tree tinfo = get_template_info (type);
381
382 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
383 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
384 ++depth;
385
386 if (DECL_P (type))
387 type = CP_DECL_CONTEXT (type);
388 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
389 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
390 else
391 type = CP_TYPE_CONTEXT (type);
392 }
393
394 return depth;
395 }
396
397 /* Return TRUE if NODE instantiates a template that has arguments of
398 its own, be it directly a primary template or indirectly through a
399 partial specializations. */
400 static bool
401 instantiates_primary_template_p (tree node)
402 {
403 tree tinfo = get_template_info (node);
404 if (!tinfo)
405 return false;
406
407 tree tmpl = TI_TEMPLATE (tinfo);
408 if (PRIMARY_TEMPLATE_P (tmpl))
409 return true;
410
411 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
412 return false;
413
414 /* So now we know we have a specialization, but it could be a full
415 or a partial specialization. To tell which, compare the depth of
416 its template arguments with those of its context. */
417
418 tree ctxt = DECL_CONTEXT (tmpl);
419 tree ctinfo = get_template_info (ctxt);
420 if (!ctinfo)
421 return true;
422
423 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
424 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
425 }
426
427 /* Subroutine of maybe_begin_member_template_processing.
428 Returns true if processing DECL needs us to push template parms. */
429
430 static bool
431 inline_needs_template_parms (tree decl, bool nsdmi)
432 {
433 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
434 return false;
435
436 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
437 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
438 }
439
440 /* Subroutine of maybe_begin_member_template_processing.
441 Push the template parms in PARMS, starting from LEVELS steps into the
442 chain, and ending at the beginning, since template parms are listed
443 innermost first. */
444
445 static void
446 push_inline_template_parms_recursive (tree parmlist, int levels)
447 {
448 tree parms = TREE_VALUE (parmlist);
449 int i;
450
451 if (levels > 1)
452 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
453
454 ++processing_template_decl;
455 current_template_parms
456 = tree_cons (size_int (processing_template_decl),
457 parms, current_template_parms);
458 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
459
460 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
461 NULL);
462 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
463 {
464 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
465
466 if (error_operand_p (parm))
467 continue;
468
469 gcc_assert (DECL_P (parm));
470
471 switch (TREE_CODE (parm))
472 {
473 case TYPE_DECL:
474 case TEMPLATE_DECL:
475 pushdecl (parm);
476 break;
477
478 case PARM_DECL:
479 /* Push the CONST_DECL. */
480 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
481 break;
482
483 default:
484 gcc_unreachable ();
485 }
486 }
487 }
488
489 /* Restore the template parameter context for a member template, a
490 friend template defined in a class definition, or a non-template
491 member of template class. */
492
493 void
494 maybe_begin_member_template_processing (tree decl)
495 {
496 tree parms;
497 int levels = 0;
498 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
499
500 if (nsdmi)
501 {
502 tree ctx = DECL_CONTEXT (decl);
503 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
504 /* Disregard full specializations (c++/60999). */
505 && uses_template_parms (ctx)
506 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
507 }
508
509 if (inline_needs_template_parms (decl, nsdmi))
510 {
511 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
512 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
513
514 if (DECL_TEMPLATE_SPECIALIZATION (decl))
515 {
516 --levels;
517 parms = TREE_CHAIN (parms);
518 }
519
520 push_inline_template_parms_recursive (parms, levels);
521 }
522
523 /* Remember how many levels of template parameters we pushed so that
524 we can pop them later. */
525 inline_parm_levels.safe_push (levels);
526 }
527
528 /* Undo the effects of maybe_begin_member_template_processing. */
529
530 void
531 maybe_end_member_template_processing (void)
532 {
533 int i;
534 int last;
535
536 if (inline_parm_levels.length () == 0)
537 return;
538
539 last = inline_parm_levels.pop ();
540 for (i = 0; i < last; ++i)
541 {
542 --processing_template_decl;
543 current_template_parms = TREE_CHAIN (current_template_parms);
544 poplevel (0, 0, 0);
545 }
546 }
547
548 /* Return a new template argument vector which contains all of ARGS,
549 but has as its innermost set of arguments the EXTRA_ARGS. */
550
551 static tree
552 add_to_template_args (tree args, tree extra_args)
553 {
554 tree new_args;
555 int extra_depth;
556 int i;
557 int j;
558
559 if (args == NULL_TREE || extra_args == error_mark_node)
560 return extra_args;
561
562 extra_depth = TMPL_ARGS_DEPTH (extra_args);
563 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
564
565 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
566 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
567
568 for (j = 1; j <= extra_depth; ++j, ++i)
569 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
570
571 return new_args;
572 }
573
574 /* Like add_to_template_args, but only the outermost ARGS are added to
575 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
576 (EXTRA_ARGS) levels are added. This function is used to combine
577 the template arguments from a partial instantiation with the
578 template arguments used to attain the full instantiation from the
579 partial instantiation. */
580
581 tree
582 add_outermost_template_args (tree args, tree extra_args)
583 {
584 tree new_args;
585
586 /* If there are more levels of EXTRA_ARGS than there are ARGS,
587 something very fishy is going on. */
588 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
589
590 /* If *all* the new arguments will be the EXTRA_ARGS, just return
591 them. */
592 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
593 return extra_args;
594
595 /* For the moment, we make ARGS look like it contains fewer levels. */
596 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
597
598 new_args = add_to_template_args (args, extra_args);
599
600 /* Now, we restore ARGS to its full dimensions. */
601 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
602
603 return new_args;
604 }
605
606 /* Return the N levels of innermost template arguments from the ARGS. */
607
608 tree
609 get_innermost_template_args (tree args, int n)
610 {
611 tree new_args;
612 int extra_levels;
613 int i;
614
615 gcc_assert (n >= 0);
616
617 /* If N is 1, just return the innermost set of template arguments. */
618 if (n == 1)
619 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
620
621 /* If we're not removing anything, just return the arguments we were
622 given. */
623 extra_levels = TMPL_ARGS_DEPTH (args) - n;
624 gcc_assert (extra_levels >= 0);
625 if (extra_levels == 0)
626 return args;
627
628 /* Make a new set of arguments, not containing the outer arguments. */
629 new_args = make_tree_vec (n);
630 for (i = 1; i <= n; ++i)
631 SET_TMPL_ARGS_LEVEL (new_args, i,
632 TMPL_ARGS_LEVEL (args, i + extra_levels));
633
634 return new_args;
635 }
636
637 /* The inverse of get_innermost_template_args: Return all but the innermost
638 EXTRA_LEVELS levels of template arguments from the ARGS. */
639
640 static tree
641 strip_innermost_template_args (tree args, int extra_levels)
642 {
643 tree new_args;
644 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
645 int i;
646
647 gcc_assert (n >= 0);
648
649 /* If N is 1, just return the outermost set of template arguments. */
650 if (n == 1)
651 return TMPL_ARGS_LEVEL (args, 1);
652
653 /* If we're not removing anything, just return the arguments we were
654 given. */
655 gcc_assert (extra_levels >= 0);
656 if (extra_levels == 0)
657 return args;
658
659 /* Make a new set of arguments, not containing the inner arguments. */
660 new_args = make_tree_vec (n);
661 for (i = 1; i <= n; ++i)
662 SET_TMPL_ARGS_LEVEL (new_args, i,
663 TMPL_ARGS_LEVEL (args, i));
664
665 return new_args;
666 }
667
668 /* We've got a template header coming up; push to a new level for storing
669 the parms. */
670
671 void
672 begin_template_parm_list (void)
673 {
674 /* We use a non-tag-transparent scope here, which causes pushtag to
675 put tags in this scope, rather than in the enclosing class or
676 namespace scope. This is the right thing, since we want
677 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
678 global template class, push_template_decl handles putting the
679 TEMPLATE_DECL into top-level scope. For a nested template class,
680 e.g.:
681
682 template <class T> struct S1 {
683 template <class T> struct S2 {};
684 };
685
686 pushtag contains special code to insert the TEMPLATE_DECL for S2
687 at the right scope. */
688 begin_scope (sk_template_parms, NULL);
689 ++processing_template_decl;
690 ++processing_template_parmlist;
691 note_template_header (0);
692
693 /* Add a dummy parameter level while we process the parameter list. */
694 current_template_parms
695 = tree_cons (size_int (processing_template_decl),
696 make_tree_vec (0),
697 current_template_parms);
698 }
699
700 /* This routine is called when a specialization is declared. If it is
701 invalid to declare a specialization here, an error is reported and
702 false is returned, otherwise this routine will return true. */
703
704 static bool
705 check_specialization_scope (void)
706 {
707 tree scope = current_scope ();
708
709 /* [temp.expl.spec]
710
711 An explicit specialization shall be declared in the namespace of
712 which the template is a member, or, for member templates, in the
713 namespace of which the enclosing class or enclosing class
714 template is a member. An explicit specialization of a member
715 function, member class or static data member of a class template
716 shall be declared in the namespace of which the class template
717 is a member. */
718 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
719 {
720 error ("explicit specialization in non-namespace scope %qD", scope);
721 return false;
722 }
723
724 /* [temp.expl.spec]
725
726 In an explicit specialization declaration for a member of a class
727 template or a member template that appears in namespace scope,
728 the member template and some of its enclosing class templates may
729 remain unspecialized, except that the declaration shall not
730 explicitly specialize a class member template if its enclosing
731 class templates are not explicitly specialized as well. */
732 if (current_template_parms)
733 {
734 error ("enclosing class templates are not explicitly specialized");
735 return false;
736 }
737
738 return true;
739 }
740
741 /* We've just seen template <>. */
742
743 bool
744 begin_specialization (void)
745 {
746 begin_scope (sk_template_spec, NULL);
747 note_template_header (1);
748 return check_specialization_scope ();
749 }
750
751 /* Called at then end of processing a declaration preceded by
752 template<>. */
753
754 void
755 end_specialization (void)
756 {
757 finish_scope ();
758 reset_specialization ();
759 }
760
761 /* Any template <>'s that we have seen thus far are not referring to a
762 function specialization. */
763
764 void
765 reset_specialization (void)
766 {
767 processing_specialization = 0;
768 template_header_count = 0;
769 }
770
771 /* We've just seen a template header. If SPECIALIZATION is nonzero,
772 it was of the form template <>. */
773
774 static void
775 note_template_header (int specialization)
776 {
777 processing_specialization = specialization;
778 template_header_count++;
779 }
780
781 /* We're beginning an explicit instantiation. */
782
783 void
784 begin_explicit_instantiation (void)
785 {
786 gcc_assert (!processing_explicit_instantiation);
787 processing_explicit_instantiation = true;
788 }
789
790
791 void
792 end_explicit_instantiation (void)
793 {
794 gcc_assert (processing_explicit_instantiation);
795 processing_explicit_instantiation = false;
796 }
797
798 /* An explicit specialization or partial specialization of TMPL is being
799 declared. Check that the namespace in which the specialization is
800 occurring is permissible. Returns false iff it is invalid to
801 specialize TMPL in the current namespace. */
802
803 static bool
804 check_specialization_namespace (tree tmpl)
805 {
806 tree tpl_ns = decl_namespace_context (tmpl);
807
808 /* [tmpl.expl.spec]
809
810 An explicit specialization shall be declared in a namespace enclosing the
811 specialized template. An explicit specialization whose declarator-id is
812 not qualified shall be declared in the nearest enclosing namespace of the
813 template, or, if the namespace is inline (7.3.1), any namespace from its
814 enclosing namespace set. */
815 if (current_scope() != DECL_CONTEXT (tmpl)
816 && !at_namespace_scope_p ())
817 {
818 error ("specialization of %qD must appear at namespace scope", tmpl);
819 return false;
820 }
821
822 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
823 /* Same or enclosing namespace. */
824 return true;
825 else
826 {
827 auto_diagnostic_group d;
828 if (permerror (input_location,
829 "specialization of %qD in different namespace", tmpl))
830 inform (DECL_SOURCE_LOCATION (tmpl),
831 " from definition of %q#D", tmpl);
832 return false;
833 }
834 }
835
836 /* SPEC is an explicit instantiation. Check that it is valid to
837 perform this explicit instantiation in the current namespace. */
838
839 static void
840 check_explicit_instantiation_namespace (tree spec)
841 {
842 tree ns;
843
844 /* DR 275: An explicit instantiation shall appear in an enclosing
845 namespace of its template. */
846 ns = decl_namespace_context (spec);
847 if (!is_nested_namespace (current_namespace, ns))
848 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
849 "(which does not enclose namespace %qD)",
850 spec, current_namespace, ns);
851 }
852
853 /* Returns the type of a template specialization only if that
854 specialization needs to be defined. Otherwise (e.g., if the type has
855 already been defined), the function returns NULL_TREE. */
856
857 static tree
858 maybe_new_partial_specialization (tree type)
859 {
860 /* An implicit instantiation of an incomplete type implies
861 the definition of a new class template.
862
863 template<typename T>
864 struct S;
865
866 template<typename T>
867 struct S<T*>;
868
869 Here, S<T*> is an implicit instantiation of S whose type
870 is incomplete. */
871 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
872 return type;
873
874 /* It can also be the case that TYPE is a completed specialization.
875 Continuing the previous example, suppose we also declare:
876
877 template<typename T>
878 requires Integral<T>
879 struct S<T*>;
880
881 Here, S<T*> refers to the specialization S<T*> defined
882 above. However, we need to differentiate definitions because
883 we intend to define a new partial specialization. In this case,
884 we rely on the fact that the constraints are different for
885 this declaration than that above.
886
887 Note that we also get here for injected class names and
888 late-parsed template definitions. We must ensure that we
889 do not create new type declarations for those cases. */
890 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
891 {
892 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
893 tree args = CLASSTYPE_TI_ARGS (type);
894
895 /* If there are no template parameters, this cannot be a new
896 partial template specialization? */
897 if (!current_template_parms)
898 return NULL_TREE;
899
900 /* The injected-class-name is not a new partial specialization. */
901 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
902 return NULL_TREE;
903
904 /* If the constraints are not the same as those of the primary
905 then, we can probably create a new specialization. */
906 tree type_constr = current_template_constraints ();
907
908 if (type == TREE_TYPE (tmpl))
909 {
910 tree main_constr = get_constraints (tmpl);
911 if (equivalent_constraints (type_constr, main_constr))
912 return NULL_TREE;
913 }
914
915 /* Also, if there's a pre-existing specialization with matching
916 constraints, then this also isn't new. */
917 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
918 while (specs)
919 {
920 tree spec_tmpl = TREE_VALUE (specs);
921 tree spec_args = TREE_PURPOSE (specs);
922 tree spec_constr = get_constraints (spec_tmpl);
923 if (comp_template_args (args, spec_args)
924 && equivalent_constraints (type_constr, spec_constr))
925 return NULL_TREE;
926 specs = TREE_CHAIN (specs);
927 }
928
929 /* Create a new type node (and corresponding type decl)
930 for the newly declared specialization. */
931 tree t = make_class_type (TREE_CODE (type));
932 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
933 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
934
935 /* We only need a separate type node for storing the definition of this
936 partial specialization; uses of S<T*> are unconstrained, so all are
937 equivalent. So keep TYPE_CANONICAL the same. */
938 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
939
940 /* Build the corresponding type decl. */
941 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
942 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
943 DECL_SOURCE_LOCATION (d) = input_location;
944 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
945 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
946
947 return t;
948 }
949
950 return NULL_TREE;
951 }
952
953 /* The TYPE is being declared. If it is a template type, that means it
954 is a partial specialization. Do appropriate error-checking. */
955
956 tree
957 maybe_process_partial_specialization (tree type)
958 {
959 tree context;
960
961 if (type == error_mark_node)
962 return error_mark_node;
963
964 /* A lambda that appears in specialization context is not itself a
965 specialization. */
966 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
967 return type;
968
969 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
970 {
971 error ("name of class shadows template template parameter %qD",
972 TYPE_NAME (type));
973 return error_mark_node;
974 }
975
976 context = TYPE_CONTEXT (type);
977
978 if (TYPE_ALIAS_P (type))
979 {
980 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
981
982 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
983 error ("specialization of alias template %qD",
984 TI_TEMPLATE (tinfo));
985 else
986 error ("explicit specialization of non-template %qT", type);
987 return error_mark_node;
988 }
989 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
990 {
991 /* This is for ordinary explicit specialization and partial
992 specialization of a template class such as:
993
994 template <> class C<int>;
995
996 or:
997
998 template <class T> class C<T*>;
999
1000 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1001
1002 if (tree t = maybe_new_partial_specialization (type))
1003 {
1004 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1005 && !at_namespace_scope_p ())
1006 return error_mark_node;
1007 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1008 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1009 if (processing_template_decl)
1010 {
1011 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1012 if (decl == error_mark_node)
1013 return error_mark_node;
1014 return TREE_TYPE (decl);
1015 }
1016 }
1017 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1018 error ("specialization of %qT after instantiation", type);
1019 else if (errorcount && !processing_specialization
1020 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1021 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1022 /* Trying to define a specialization either without a template<> header
1023 or in an inappropriate place. We've already given an error, so just
1024 bail now so we don't actually define the specialization. */
1025 return error_mark_node;
1026 }
1027 else if (CLASS_TYPE_P (type)
1028 && !CLASSTYPE_USE_TEMPLATE (type)
1029 && CLASSTYPE_TEMPLATE_INFO (type)
1030 && context && CLASS_TYPE_P (context)
1031 && CLASSTYPE_TEMPLATE_INFO (context))
1032 {
1033 /* This is for an explicit specialization of member class
1034 template according to [temp.expl.spec/18]:
1035
1036 template <> template <class U> class C<int>::D;
1037
1038 The context `C<int>' must be an implicit instantiation.
1039 Otherwise this is just a member class template declared
1040 earlier like:
1041
1042 template <> class C<int> { template <class U> class D; };
1043 template <> template <class U> class C<int>::D;
1044
1045 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1046 while in the second case, `C<int>::D' is a primary template
1047 and `C<T>::D' may not exist. */
1048
1049 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1050 && !COMPLETE_TYPE_P (type))
1051 {
1052 tree t;
1053 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1054
1055 if (current_namespace
1056 != decl_namespace_context (tmpl))
1057 {
1058 if (permerror (input_location,
1059 "specialization of %qD in different namespace",
1060 type))
1061 inform (DECL_SOURCE_LOCATION (tmpl),
1062 "from definition of %q#D", tmpl);
1063 }
1064
1065 /* Check for invalid specialization after instantiation:
1066
1067 template <> template <> class C<int>::D<int>;
1068 template <> template <class U> class C<int>::D; */
1069
1070 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1071 t; t = TREE_CHAIN (t))
1072 {
1073 tree inst = TREE_VALUE (t);
1074 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1075 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1076 {
1077 /* We already have a full specialization of this partial
1078 instantiation, or a full specialization has been
1079 looked up but not instantiated. Reassign it to the
1080 new member specialization template. */
1081 spec_entry elt;
1082 spec_entry *entry;
1083
1084 elt.tmpl = most_general_template (tmpl);
1085 elt.args = CLASSTYPE_TI_ARGS (inst);
1086 elt.spec = inst;
1087
1088 type_specializations->remove_elt (&elt);
1089
1090 elt.tmpl = tmpl;
1091 CLASSTYPE_TI_ARGS (inst)
1092 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1093
1094 spec_entry **slot
1095 = type_specializations->find_slot (&elt, INSERT);
1096 entry = ggc_alloc<spec_entry> ();
1097 *entry = elt;
1098 *slot = entry;
1099 }
1100 else
1101 /* But if we've had an implicit instantiation, that's a
1102 problem ([temp.expl.spec]/6). */
1103 error ("specialization %qT after instantiation %qT",
1104 type, inst);
1105 }
1106
1107 /* Mark TYPE as a specialization. And as a result, we only
1108 have one level of template argument for the innermost
1109 class template. */
1110 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1111 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1112 CLASSTYPE_TI_ARGS (type)
1113 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1114 }
1115 }
1116 else if (processing_specialization)
1117 {
1118 /* Someday C++0x may allow for enum template specialization. */
1119 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1120 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1121 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1122 "of %qD not allowed by ISO C++", type);
1123 else
1124 {
1125 error ("explicit specialization of non-template %qT", type);
1126 return error_mark_node;
1127 }
1128 }
1129
1130 return type;
1131 }
1132
1133 /* Returns nonzero if we can optimize the retrieval of specializations
1134 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1135 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1136
1137 static inline bool
1138 optimize_specialization_lookup_p (tree tmpl)
1139 {
1140 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1141 && DECL_CLASS_SCOPE_P (tmpl)
1142 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1143 parameter. */
1144 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1145 /* The optimized lookup depends on the fact that the
1146 template arguments for the member function template apply
1147 purely to the containing class, which is not true if the
1148 containing class is an explicit or partial
1149 specialization. */
1150 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1151 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1152 && !DECL_CONV_FN_P (tmpl)
1153 /* It is possible to have a template that is not a member
1154 template and is not a member of a template class:
1155
1156 template <typename T>
1157 struct S { friend A::f(); };
1158
1159 Here, the friend function is a template, but the context does
1160 not have template information. The optimized lookup relies
1161 on having ARGS be the template arguments for both the class
1162 and the function template. */
1163 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1164 }
1165
1166 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1167 gone through coerce_template_parms by now. */
1168
1169 static void
1170 verify_unstripped_args_1 (tree inner)
1171 {
1172 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1173 {
1174 tree arg = TREE_VEC_ELT (inner, i);
1175 if (TREE_CODE (arg) == TEMPLATE_DECL)
1176 /* OK */;
1177 else if (TYPE_P (arg))
1178 gcc_assert (strip_typedefs (arg, NULL) == arg);
1179 else if (ARGUMENT_PACK_P (arg))
1180 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1181 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1182 /* Allow typedefs on the type of a non-type argument, since a
1183 parameter can have them. */;
1184 else
1185 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1186 }
1187 }
1188
1189 static void
1190 verify_unstripped_args (tree args)
1191 {
1192 ++processing_template_decl;
1193 if (!any_dependent_template_arguments_p (args))
1194 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1195 --processing_template_decl;
1196 }
1197
1198 /* Retrieve the specialization (in the sense of [temp.spec] - a
1199 specialization is either an instantiation or an explicit
1200 specialization) of TMPL for the given template ARGS. If there is
1201 no such specialization, return NULL_TREE. The ARGS are a vector of
1202 arguments, or a vector of vectors of arguments, in the case of
1203 templates with more than one level of parameters.
1204
1205 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1206 then we search for a partial specialization matching ARGS. This
1207 parameter is ignored if TMPL is not a class template.
1208
1209 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1210 result is a NONTYPE_ARGUMENT_PACK. */
1211
1212 static tree
1213 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1214 {
1215 if (tmpl == NULL_TREE)
1216 return NULL_TREE;
1217
1218 if (args == error_mark_node)
1219 return NULL_TREE;
1220
1221 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1222 || TREE_CODE (tmpl) == FIELD_DECL);
1223
1224 /* There should be as many levels of arguments as there are
1225 levels of parameters. */
1226 gcc_assert (TMPL_ARGS_DEPTH (args)
1227 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1228 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1229 : template_class_depth (DECL_CONTEXT (tmpl))));
1230
1231 if (flag_checking)
1232 verify_unstripped_args (args);
1233
1234 /* Lambda functions in templates aren't instantiated normally, but through
1235 tsubst_lambda_expr. */
1236 if (lambda_fn_in_template_p (tmpl))
1237 return NULL_TREE;
1238
1239 if (optimize_specialization_lookup_p (tmpl))
1240 {
1241 /* The template arguments actually apply to the containing
1242 class. Find the class specialization with those
1243 arguments. */
1244 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1245 tree class_specialization
1246 = retrieve_specialization (class_template, args, 0);
1247 if (!class_specialization)
1248 return NULL_TREE;
1249
1250 /* Find the instance of TMPL. */
1251 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1252 for (ovl_iterator iter (fns); iter; ++iter)
1253 {
1254 tree fn = *iter;
1255 if (tree ti = get_template_info (fn))
1256 if (TI_TEMPLATE (ti) == tmpl
1257 /* using-declarations can bring in a different
1258 instantiation of tmpl as a member of a different
1259 instantiation of tmpl's class. We don't want those
1260 here. */
1261 && DECL_CONTEXT (fn) == class_specialization)
1262 return fn;
1263 }
1264 return NULL_TREE;
1265 }
1266 else
1267 {
1268 spec_entry *found;
1269 spec_entry elt;
1270 hash_table<spec_hasher> *specializations;
1271
1272 elt.tmpl = tmpl;
1273 elt.args = args;
1274 elt.spec = NULL_TREE;
1275
1276 if (DECL_CLASS_TEMPLATE_P (tmpl))
1277 specializations = type_specializations;
1278 else
1279 specializations = decl_specializations;
1280
1281 if (hash == 0)
1282 hash = spec_hasher::hash (&elt);
1283 found = specializations->find_with_hash (&elt, hash);
1284 if (found)
1285 return found->spec;
1286 }
1287
1288 return NULL_TREE;
1289 }
1290
1291 /* Like retrieve_specialization, but for local declarations. */
1292
1293 tree
1294 retrieve_local_specialization (tree tmpl)
1295 {
1296 if (local_specializations == NULL)
1297 return NULL_TREE;
1298
1299 tree *slot = local_specializations->get (tmpl);
1300 return slot ? *slot : NULL_TREE;
1301 }
1302
1303 /* Returns nonzero iff DECL is a specialization of TMPL. */
1304
1305 int
1306 is_specialization_of (tree decl, tree tmpl)
1307 {
1308 tree t;
1309
1310 if (TREE_CODE (decl) == FUNCTION_DECL)
1311 {
1312 for (t = decl;
1313 t != NULL_TREE;
1314 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1315 if (t == tmpl)
1316 return 1;
1317 }
1318 else
1319 {
1320 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1321
1322 for (t = TREE_TYPE (decl);
1323 t != NULL_TREE;
1324 t = CLASSTYPE_USE_TEMPLATE (t)
1325 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1326 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1327 return 1;
1328 }
1329
1330 return 0;
1331 }
1332
1333 /* Returns nonzero iff DECL is a specialization of friend declaration
1334 FRIEND_DECL according to [temp.friend]. */
1335
1336 bool
1337 is_specialization_of_friend (tree decl, tree friend_decl)
1338 {
1339 bool need_template = true;
1340 int template_depth;
1341
1342 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1343 || TREE_CODE (decl) == TYPE_DECL);
1344
1345 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1346 of a template class, we want to check if DECL is a specialization
1347 if this. */
1348 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1349 && DECL_TEMPLATE_INFO (friend_decl)
1350 && !DECL_USE_TEMPLATE (friend_decl))
1351 {
1352 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1353 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1354 need_template = false;
1355 }
1356 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1357 && !PRIMARY_TEMPLATE_P (friend_decl))
1358 need_template = false;
1359
1360 /* There is nothing to do if this is not a template friend. */
1361 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1362 return false;
1363
1364 if (is_specialization_of (decl, friend_decl))
1365 return true;
1366
1367 /* [temp.friend/6]
1368 A member of a class template may be declared to be a friend of a
1369 non-template class. In this case, the corresponding member of
1370 every specialization of the class template is a friend of the
1371 class granting friendship.
1372
1373 For example, given a template friend declaration
1374
1375 template <class T> friend void A<T>::f();
1376
1377 the member function below is considered a friend
1378
1379 template <> struct A<int> {
1380 void f();
1381 };
1382
1383 For this type of template friend, TEMPLATE_DEPTH below will be
1384 nonzero. To determine if DECL is a friend of FRIEND, we first
1385 check if the enclosing class is a specialization of another. */
1386
1387 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1388 if (template_depth
1389 && DECL_CLASS_SCOPE_P (decl)
1390 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1391 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1392 {
1393 /* Next, we check the members themselves. In order to handle
1394 a few tricky cases, such as when FRIEND_DECL's are
1395
1396 template <class T> friend void A<T>::g(T t);
1397 template <class T> template <T t> friend void A<T>::h();
1398
1399 and DECL's are
1400
1401 void A<int>::g(int);
1402 template <int> void A<int>::h();
1403
1404 we need to figure out ARGS, the template arguments from
1405 the context of DECL. This is required for template substitution
1406 of `T' in the function parameter of `g' and template parameter
1407 of `h' in the above examples. Here ARGS corresponds to `int'. */
1408
1409 tree context = DECL_CONTEXT (decl);
1410 tree args = NULL_TREE;
1411 int current_depth = 0;
1412
1413 while (current_depth < template_depth)
1414 {
1415 if (CLASSTYPE_TEMPLATE_INFO (context))
1416 {
1417 if (current_depth == 0)
1418 args = TYPE_TI_ARGS (context);
1419 else
1420 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1421 current_depth++;
1422 }
1423 context = TYPE_CONTEXT (context);
1424 }
1425
1426 if (TREE_CODE (decl) == FUNCTION_DECL)
1427 {
1428 bool is_template;
1429 tree friend_type;
1430 tree decl_type;
1431 tree friend_args_type;
1432 tree decl_args_type;
1433
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1435 non-templates. */
1436 is_template = DECL_TEMPLATE_INFO (decl)
1437 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1438 if (need_template ^ is_template)
1439 return false;
1440 else if (is_template)
1441 {
1442 /* If both are templates, check template parameter list. */
1443 tree friend_parms
1444 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1445 args, tf_none);
1446 if (!comp_template_parms
1447 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1448 friend_parms))
1449 return false;
1450
1451 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1452 }
1453 else
1454 decl_type = TREE_TYPE (decl);
1455
1456 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1457 tf_none, NULL_TREE);
1458 if (friend_type == error_mark_node)
1459 return false;
1460
1461 /* Check if return types match. */
1462 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1463 return false;
1464
1465 /* Check if function parameter types match, ignoring the
1466 `this' parameter. */
1467 friend_args_type = TYPE_ARG_TYPES (friend_type);
1468 decl_args_type = TYPE_ARG_TYPES (decl_type);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1470 friend_args_type = TREE_CHAIN (friend_args_type);
1471 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1472 decl_args_type = TREE_CHAIN (decl_args_type);
1473
1474 return compparms (decl_args_type, friend_args_type);
1475 }
1476 else
1477 {
1478 /* DECL is a TYPE_DECL */
1479 bool is_template;
1480 tree decl_type = TREE_TYPE (decl);
1481
1482 /* Make sure that both DECL and FRIEND_DECL are templates or
1483 non-templates. */
1484 is_template
1485 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1486 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1487
1488 if (need_template ^ is_template)
1489 return false;
1490 else if (is_template)
1491 {
1492 tree friend_parms;
1493 /* If both are templates, check the name of the two
1494 TEMPLATE_DECL's first because is_friend didn't. */
1495 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1496 != DECL_NAME (friend_decl))
1497 return false;
1498
1499 /* Now check template parameter list. */
1500 friend_parms
1501 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1502 args, tf_none);
1503 return comp_template_parms
1504 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1505 friend_parms);
1506 }
1507 else
1508 return (DECL_NAME (decl)
1509 == DECL_NAME (friend_decl));
1510 }
1511 }
1512 return false;
1513 }
1514
1515 /* Register the specialization SPEC as a specialization of TMPL with
1516 the indicated ARGS. IS_FRIEND indicates whether the specialization
1517 is actually just a friend declaration. ATTRLIST is the list of
1518 attributes that the specialization is declared with or NULL when
1519 it isn't. Returns SPEC, or an equivalent prior declaration, if
1520 available.
1521
1522 We also store instantiations of field packs in the hash table, even
1523 though they are not themselves templates, to make lookup easier. */
1524
1525 static tree
1526 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1527 hashval_t hash)
1528 {
1529 tree fn;
1530 spec_entry **slot = NULL;
1531 spec_entry elt;
1532
1533 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1534 || (TREE_CODE (tmpl) == FIELD_DECL
1535 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1536
1537 if (TREE_CODE (spec) == FUNCTION_DECL
1538 && uses_template_parms (DECL_TI_ARGS (spec)))
1539 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1540 register it; we want the corresponding TEMPLATE_DECL instead.
1541 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1542 the more obvious `uses_template_parms (spec)' to avoid problems
1543 with default function arguments. In particular, given
1544 something like this:
1545
1546 template <class T> void f(T t1, T t = T())
1547
1548 the default argument expression is not substituted for in an
1549 instantiation unless and until it is actually needed. */
1550 return spec;
1551
1552 if (optimize_specialization_lookup_p (tmpl))
1553 /* We don't put these specializations in the hash table, but we might
1554 want to give an error about a mismatch. */
1555 fn = retrieve_specialization (tmpl, args, 0);
1556 else
1557 {
1558 elt.tmpl = tmpl;
1559 elt.args = args;
1560 elt.spec = spec;
1561
1562 if (hash == 0)
1563 hash = spec_hasher::hash (&elt);
1564
1565 slot =
1566 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1567 if (*slot)
1568 fn = ((spec_entry *) *slot)->spec;
1569 else
1570 fn = NULL_TREE;
1571 }
1572
1573 /* We can sometimes try to re-register a specialization that we've
1574 already got. In particular, regenerate_decl_from_template calls
1575 duplicate_decls which will update the specialization list. But,
1576 we'll still get called again here anyhow. It's more convenient
1577 to simply allow this than to try to prevent it. */
1578 if (fn == spec)
1579 return spec;
1580 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1581 {
1582 if (DECL_TEMPLATE_INSTANTIATION (fn))
1583 {
1584 if (DECL_ODR_USED (fn)
1585 || DECL_EXPLICIT_INSTANTIATION (fn))
1586 {
1587 error ("specialization of %qD after instantiation",
1588 fn);
1589 return error_mark_node;
1590 }
1591 else
1592 {
1593 tree clone;
1594 /* This situation should occur only if the first
1595 specialization is an implicit instantiation, the
1596 second is an explicit specialization, and the
1597 implicit instantiation has not yet been used. That
1598 situation can occur if we have implicitly
1599 instantiated a member function and then specialized
1600 it later.
1601
1602 We can also wind up here if a friend declaration that
1603 looked like an instantiation turns out to be a
1604 specialization:
1605
1606 template <class T> void foo(T);
1607 class S { friend void foo<>(int) };
1608 template <> void foo(int);
1609
1610 We transform the existing DECL in place so that any
1611 pointers to it become pointers to the updated
1612 declaration.
1613
1614 If there was a definition for the template, but not
1615 for the specialization, we want this to look as if
1616 there were no definition, and vice versa. */
1617 DECL_INITIAL (fn) = NULL_TREE;
1618 duplicate_decls (spec, fn, is_friend);
1619 /* The call to duplicate_decls will have applied
1620 [temp.expl.spec]:
1621
1622 An explicit specialization of a function template
1623 is inline only if it is explicitly declared to be,
1624 and independently of whether its function template
1625 is.
1626
1627 to the primary function; now copy the inline bits to
1628 the various clones. */
1629 FOR_EACH_CLONE (clone, fn)
1630 {
1631 DECL_DECLARED_INLINE_P (clone)
1632 = DECL_DECLARED_INLINE_P (fn);
1633 DECL_SOURCE_LOCATION (clone)
1634 = DECL_SOURCE_LOCATION (fn);
1635 DECL_DELETED_FN (clone)
1636 = DECL_DELETED_FN (fn);
1637 }
1638 check_specialization_namespace (tmpl);
1639
1640 return fn;
1641 }
1642 }
1643 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1644 {
1645 tree dd = duplicate_decls (spec, fn, is_friend);
1646 if (dd == error_mark_node)
1647 /* We've already complained in duplicate_decls. */
1648 return error_mark_node;
1649
1650 if (dd == NULL_TREE && DECL_INITIAL (spec))
1651 /* Dup decl failed, but this is a new definition. Set the
1652 line number so any errors match this new
1653 definition. */
1654 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1655
1656 return fn;
1657 }
1658 }
1659 else if (fn)
1660 return duplicate_decls (spec, fn, is_friend);
1661
1662 /* A specialization must be declared in the same namespace as the
1663 template it is specializing. */
1664 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1665 && !check_specialization_namespace (tmpl))
1666 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1667
1668 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1669 {
1670 spec_entry *entry = ggc_alloc<spec_entry> ();
1671 gcc_assert (tmpl && args && spec);
1672 *entry = elt;
1673 *slot = entry;
1674 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1675 && PRIMARY_TEMPLATE_P (tmpl)
1676 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1677 || variable_template_p (tmpl))
1678 /* If TMPL is a forward declaration of a template function, keep a list
1679 of all specializations in case we need to reassign them to a friend
1680 template later in tsubst_friend_function.
1681
1682 Also keep a list of all variable template instantiations so that
1683 process_partial_specialization can check whether a later partial
1684 specialization would have used it. */
1685 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1686 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1687 }
1688
1689 return spec;
1690 }
1691
1692 /* Returns true iff two spec_entry nodes are equivalent. */
1693
1694 int comparing_specializations;
1695
1696 bool
1697 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1698 {
1699 int equal;
1700
1701 ++comparing_specializations;
1702 equal = (e1->tmpl == e2->tmpl
1703 && comp_template_args (e1->args, e2->args));
1704 if (equal && flag_concepts
1705 /* tmpl could be a FIELD_DECL for a capture pack. */
1706 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1707 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1708 && uses_template_parms (e1->args))
1709 {
1710 /* Partial specializations of a variable template can be distinguished by
1711 constraints. */
1712 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1713 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1714 equal = equivalent_constraints (c1, c2);
1715 }
1716 --comparing_specializations;
1717
1718 return equal;
1719 }
1720
1721 /* Returns a hash for a template TMPL and template arguments ARGS. */
1722
1723 static hashval_t
1724 hash_tmpl_and_args (tree tmpl, tree args)
1725 {
1726 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1727 return iterative_hash_template_arg (args, val);
1728 }
1729
1730 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1731 ignoring SPEC. */
1732
1733 hashval_t
1734 spec_hasher::hash (spec_entry *e)
1735 {
1736 return hash_tmpl_and_args (e->tmpl, e->args);
1737 }
1738
1739 /* Recursively calculate a hash value for a template argument ARG, for use
1740 in the hash tables of template specializations. */
1741
1742 hashval_t
1743 iterative_hash_template_arg (tree arg, hashval_t val)
1744 {
1745 unsigned HOST_WIDE_INT i;
1746 enum tree_code code;
1747 char tclass;
1748
1749 if (arg == NULL_TREE)
1750 return iterative_hash_object (arg, val);
1751
1752 if (!TYPE_P (arg))
1753 STRIP_NOPS (arg);
1754
1755 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1756 gcc_unreachable ();
1757
1758 code = TREE_CODE (arg);
1759 tclass = TREE_CODE_CLASS (code);
1760
1761 val = iterative_hash_object (code, val);
1762
1763 switch (code)
1764 {
1765 case ERROR_MARK:
1766 return val;
1767
1768 case IDENTIFIER_NODE:
1769 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1770
1771 case TREE_VEC:
1772 {
1773 int i, len = TREE_VEC_LENGTH (arg);
1774 for (i = 0; i < len; ++i)
1775 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1776 return val;
1777 }
1778
1779 case TYPE_PACK_EXPANSION:
1780 case EXPR_PACK_EXPANSION:
1781 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1782 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1783
1784 case TYPE_ARGUMENT_PACK:
1785 case NONTYPE_ARGUMENT_PACK:
1786 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1787
1788 case TREE_LIST:
1789 for (; arg; arg = TREE_CHAIN (arg))
1790 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1791 return val;
1792
1793 case OVERLOAD:
1794 for (lkp_iterator iter (arg); iter; ++iter)
1795 val = iterative_hash_template_arg (*iter, val);
1796 return val;
1797
1798 case CONSTRUCTOR:
1799 {
1800 tree field, value;
1801 iterative_hash_template_arg (TREE_TYPE (arg), val);
1802 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1803 {
1804 val = iterative_hash_template_arg (field, val);
1805 val = iterative_hash_template_arg (value, val);
1806 }
1807 return val;
1808 }
1809
1810 case PARM_DECL:
1811 if (!DECL_ARTIFICIAL (arg))
1812 {
1813 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1814 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1815 }
1816 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1817
1818 case TARGET_EXPR:
1819 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1820
1821 case PTRMEM_CST:
1822 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1823 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1824
1825 case TEMPLATE_PARM_INDEX:
1826 val = iterative_hash_template_arg
1827 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1828 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1829 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1830
1831 case TRAIT_EXPR:
1832 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1833 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1834 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1835
1836 case BASELINK:
1837 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1838 val);
1839 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1840 val);
1841
1842 case MODOP_EXPR:
1843 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1844 code = TREE_CODE (TREE_OPERAND (arg, 1));
1845 val = iterative_hash_object (code, val);
1846 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1847
1848 case LAMBDA_EXPR:
1849 /* [temp.over.link] Two lambda-expressions are never considered
1850 equivalent.
1851
1852 So just hash the closure type. */
1853 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1854
1855 case CAST_EXPR:
1856 case IMPLICIT_CONV_EXPR:
1857 case STATIC_CAST_EXPR:
1858 case REINTERPRET_CAST_EXPR:
1859 case CONST_CAST_EXPR:
1860 case DYNAMIC_CAST_EXPR:
1861 case NEW_EXPR:
1862 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1863 /* Now hash operands as usual. */
1864 break;
1865
1866 case CALL_EXPR:
1867 {
1868 tree fn = CALL_EXPR_FN (arg);
1869 if (tree name = dependent_name (fn))
1870 {
1871 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1872 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1873 fn = name;
1874 }
1875 val = iterative_hash_template_arg (fn, val);
1876 call_expr_arg_iterator ai;
1877 for (tree x = first_call_expr_arg (arg, &ai); x;
1878 x = next_call_expr_arg (&ai))
1879 val = iterative_hash_template_arg (x, val);
1880 return val;
1881 }
1882
1883 default:
1884 break;
1885 }
1886
1887 switch (tclass)
1888 {
1889 case tcc_type:
1890 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1891 {
1892 // We want an alias specialization that survived strip_typedefs
1893 // to hash differently from its TYPE_CANONICAL, to avoid hash
1894 // collisions that compare as different in template_args_equal.
1895 // These could be dependent specializations that strip_typedefs
1896 // left alone, or untouched specializations because
1897 // coerce_template_parms returns the unconverted template
1898 // arguments if it sees incomplete argument packs.
1899 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1900 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1901 }
1902 if (TYPE_CANONICAL (arg))
1903 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1904 val);
1905 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1906 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1907 /* Otherwise just compare the types during lookup. */
1908 return val;
1909
1910 case tcc_declaration:
1911 case tcc_constant:
1912 return iterative_hash_expr (arg, val);
1913
1914 default:
1915 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1916 {
1917 unsigned n = cp_tree_operand_length (arg);
1918 for (i = 0; i < n; ++i)
1919 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1920 return val;
1921 }
1922 }
1923 gcc_unreachable ();
1924 return 0;
1925 }
1926
1927 /* Unregister the specialization SPEC as a specialization of TMPL.
1928 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1929 if the SPEC was listed as a specialization of TMPL.
1930
1931 Note that SPEC has been ggc_freed, so we can't look inside it. */
1932
1933 bool
1934 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1935 {
1936 spec_entry *entry;
1937 spec_entry elt;
1938
1939 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1940 elt.args = TI_ARGS (tinfo);
1941 elt.spec = NULL_TREE;
1942
1943 entry = decl_specializations->find (&elt);
1944 if (entry != NULL)
1945 {
1946 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1947 gcc_assert (new_spec != NULL_TREE);
1948 entry->spec = new_spec;
1949 return 1;
1950 }
1951
1952 return 0;
1953 }
1954
1955 /* Like register_specialization, but for local declarations. We are
1956 registering SPEC, an instantiation of TMPL. */
1957
1958 void
1959 register_local_specialization (tree spec, tree tmpl)
1960 {
1961 gcc_assert (tmpl != spec);
1962 local_specializations->put (tmpl, spec);
1963 }
1964
1965 /* TYPE is a class type. Returns true if TYPE is an explicitly
1966 specialized class. */
1967
1968 bool
1969 explicit_class_specialization_p (tree type)
1970 {
1971 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1972 return false;
1973 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1974 }
1975
1976 /* Print the list of functions at FNS, going through all the overloads
1977 for each element of the list. Alternatively, FNS cannot be a
1978 TREE_LIST, in which case it will be printed together with all the
1979 overloads.
1980
1981 MORE and *STR should respectively be FALSE and NULL when the function
1982 is called from the outside. They are used internally on recursive
1983 calls. print_candidates manages the two parameters and leaves NULL
1984 in *STR when it ends. */
1985
1986 static void
1987 print_candidates_1 (tree fns, char **str, bool more = false)
1988 {
1989 if (TREE_CODE (fns) == TREE_LIST)
1990 for (; fns; fns = TREE_CHAIN (fns))
1991 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1992 else
1993 for (lkp_iterator iter (fns); iter;)
1994 {
1995 tree cand = *iter;
1996 ++iter;
1997
1998 const char *pfx = *str;
1999 if (!pfx)
2000 {
2001 if (more || iter)
2002 pfx = _("candidates are:");
2003 else
2004 pfx = _("candidate is:");
2005 *str = get_spaces (pfx);
2006 }
2007 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2008 }
2009 }
2010
2011 /* Print the list of candidate FNS in an error message. FNS can also
2012 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2013
2014 void
2015 print_candidates (tree fns)
2016 {
2017 char *str = NULL;
2018 print_candidates_1 (fns, &str);
2019 free (str);
2020 }
2021
2022 /* Get a (possibly) constrained template declaration for the
2023 purpose of ordering candidates. */
2024 static tree
2025 get_template_for_ordering (tree list)
2026 {
2027 gcc_assert (TREE_CODE (list) == TREE_LIST);
2028 tree f = TREE_VALUE (list);
2029 if (tree ti = DECL_TEMPLATE_INFO (f))
2030 return TI_TEMPLATE (ti);
2031 return f;
2032 }
2033
2034 /* Among candidates having the same signature, return the
2035 most constrained or NULL_TREE if there is no best candidate.
2036 If the signatures of candidates vary (e.g., template
2037 specialization vs. member function), then there can be no
2038 most constrained.
2039
2040 Note that we don't compare constraints on the functions
2041 themselves, but rather those of their templates. */
2042 static tree
2043 most_constrained_function (tree candidates)
2044 {
2045 // Try to find the best candidate in a first pass.
2046 tree champ = candidates;
2047 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2048 {
2049 int winner = more_constrained (get_template_for_ordering (champ),
2050 get_template_for_ordering (c));
2051 if (winner == -1)
2052 champ = c; // The candidate is more constrained
2053 else if (winner == 0)
2054 return NULL_TREE; // Neither is more constrained
2055 }
2056
2057 // Verify that the champ is better than previous candidates.
2058 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2059 if (!more_constrained (get_template_for_ordering (champ),
2060 get_template_for_ordering (c)))
2061 return NULL_TREE;
2062 }
2063
2064 return champ;
2065 }
2066
2067
2068 /* Returns the template (one of the functions given by TEMPLATE_ID)
2069 which can be specialized to match the indicated DECL with the
2070 explicit template args given in TEMPLATE_ID. The DECL may be
2071 NULL_TREE if none is available. In that case, the functions in
2072 TEMPLATE_ID are non-members.
2073
2074 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2075 specialization of a member template.
2076
2077 The TEMPLATE_COUNT is the number of references to qualifying
2078 template classes that appeared in the name of the function. See
2079 check_explicit_specialization for a more accurate description.
2080
2081 TSK indicates what kind of template declaration (if any) is being
2082 declared. TSK_TEMPLATE indicates that the declaration given by
2083 DECL, though a FUNCTION_DECL, has template parameters, and is
2084 therefore a template function.
2085
2086 The template args (those explicitly specified and those deduced)
2087 are output in a newly created vector *TARGS_OUT.
2088
2089 If it is impossible to determine the result, an error message is
2090 issued. The error_mark_node is returned to indicate failure. */
2091
2092 static tree
2093 determine_specialization (tree template_id,
2094 tree decl,
2095 tree* targs_out,
2096 int need_member_template,
2097 int template_count,
2098 tmpl_spec_kind tsk)
2099 {
2100 tree fns;
2101 tree targs;
2102 tree explicit_targs;
2103 tree candidates = NULL_TREE;
2104
2105 /* A TREE_LIST of templates of which DECL may be a specialization.
2106 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2107 corresponding TREE_PURPOSE is the set of template arguments that,
2108 when used to instantiate the template, would produce a function
2109 with the signature of DECL. */
2110 tree templates = NULL_TREE;
2111 int header_count;
2112 cp_binding_level *b;
2113
2114 *targs_out = NULL_TREE;
2115
2116 if (template_id == error_mark_node || decl == error_mark_node)
2117 return error_mark_node;
2118
2119 /* We shouldn't be specializing a member template of an
2120 unspecialized class template; we already gave an error in
2121 check_specialization_scope, now avoid crashing. */
2122 if (!VAR_P (decl)
2123 && template_count && DECL_CLASS_SCOPE_P (decl)
2124 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2125 {
2126 gcc_assert (errorcount);
2127 return error_mark_node;
2128 }
2129
2130 fns = TREE_OPERAND (template_id, 0);
2131 explicit_targs = TREE_OPERAND (template_id, 1);
2132
2133 if (fns == error_mark_node)
2134 return error_mark_node;
2135
2136 /* Check for baselinks. */
2137 if (BASELINK_P (fns))
2138 fns = BASELINK_FUNCTIONS (fns);
2139
2140 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2141 {
2142 error_at (DECL_SOURCE_LOCATION (decl),
2143 "%qD is not a function template", fns);
2144 return error_mark_node;
2145 }
2146 else if (VAR_P (decl) && !variable_template_p (fns))
2147 {
2148 error ("%qD is not a variable template", fns);
2149 return error_mark_node;
2150 }
2151
2152 /* Count the number of template headers specified for this
2153 specialization. */
2154 header_count = 0;
2155 for (b = current_binding_level;
2156 b->kind == sk_template_parms;
2157 b = b->level_chain)
2158 ++header_count;
2159
2160 tree orig_fns = fns;
2161
2162 if (variable_template_p (fns))
2163 {
2164 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2165 targs = coerce_template_parms (parms, explicit_targs, fns,
2166 tf_warning_or_error,
2167 /*req_all*/true, /*use_defarg*/true);
2168 if (targs != error_mark_node)
2169 templates = tree_cons (targs, fns, templates);
2170 }
2171 else for (lkp_iterator iter (fns); iter; ++iter)
2172 {
2173 tree fn = *iter;
2174
2175 if (TREE_CODE (fn) == TEMPLATE_DECL)
2176 {
2177 tree decl_arg_types;
2178 tree fn_arg_types;
2179 tree insttype;
2180
2181 /* In case of explicit specialization, we need to check if
2182 the number of template headers appearing in the specialization
2183 is correct. This is usually done in check_explicit_specialization,
2184 but the check done there cannot be exhaustive when specializing
2185 member functions. Consider the following code:
2186
2187 template <> void A<int>::f(int);
2188 template <> template <> void A<int>::f(int);
2189
2190 Assuming that A<int> is not itself an explicit specialization
2191 already, the first line specializes "f" which is a non-template
2192 member function, whilst the second line specializes "f" which
2193 is a template member function. So both lines are syntactically
2194 correct, and check_explicit_specialization does not reject
2195 them.
2196
2197 Here, we can do better, as we are matching the specialization
2198 against the declarations. We count the number of template
2199 headers, and we check if they match TEMPLATE_COUNT + 1
2200 (TEMPLATE_COUNT is the number of qualifying template classes,
2201 plus there must be another header for the member template
2202 itself).
2203
2204 Notice that if header_count is zero, this is not a
2205 specialization but rather a template instantiation, so there
2206 is no check we can perform here. */
2207 if (header_count && header_count != template_count + 1)
2208 continue;
2209
2210 /* Check that the number of template arguments at the
2211 innermost level for DECL is the same as for FN. */
2212 if (current_binding_level->kind == sk_template_parms
2213 && !current_binding_level->explicit_spec_p
2214 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2215 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2216 (current_template_parms))))
2217 continue;
2218
2219 /* DECL might be a specialization of FN. */
2220 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2221 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2222
2223 /* For a non-static member function, we need to make sure
2224 that the const qualification is the same. Since
2225 get_bindings does not try to merge the "this" parameter,
2226 we must do the comparison explicitly. */
2227 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2228 {
2229 if (!same_type_p (TREE_VALUE (fn_arg_types),
2230 TREE_VALUE (decl_arg_types)))
2231 continue;
2232
2233 /* And the ref-qualification. */
2234 if (type_memfn_rqual (TREE_TYPE (decl))
2235 != type_memfn_rqual (TREE_TYPE (fn)))
2236 continue;
2237 }
2238
2239 /* Skip the "this" parameter and, for constructors of
2240 classes with virtual bases, the VTT parameter. A
2241 full specialization of a constructor will have a VTT
2242 parameter, but a template never will. */
2243 decl_arg_types
2244 = skip_artificial_parms_for (decl, decl_arg_types);
2245 fn_arg_types
2246 = skip_artificial_parms_for (fn, fn_arg_types);
2247
2248 /* Function templates cannot be specializations; there are
2249 no partial specializations of functions. Therefore, if
2250 the type of DECL does not match FN, there is no
2251 match.
2252
2253 Note that it should never be the case that we have both
2254 candidates added here, and for regular member functions
2255 below. */
2256 if (tsk == tsk_template)
2257 {
2258 if (compparms (fn_arg_types, decl_arg_types))
2259 candidates = tree_cons (NULL_TREE, fn, candidates);
2260 continue;
2261 }
2262
2263 /* See whether this function might be a specialization of this
2264 template. Suppress access control because we might be trying
2265 to make this specialization a friend, and we have already done
2266 access control for the declaration of the specialization. */
2267 push_deferring_access_checks (dk_no_check);
2268 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2269 pop_deferring_access_checks ();
2270
2271 if (!targs)
2272 /* We cannot deduce template arguments that when used to
2273 specialize TMPL will produce DECL. */
2274 continue;
2275
2276 if (uses_template_parms (targs))
2277 /* We deduced something involving 'auto', which isn't a valid
2278 template argument. */
2279 continue;
2280
2281 /* Remove, from the set of candidates, all those functions
2282 whose constraints are not satisfied. */
2283 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2284 continue;
2285
2286 // Then, try to form the new function type.
2287 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2288 if (insttype == error_mark_node)
2289 continue;
2290 fn_arg_types
2291 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2292 if (!compparms (fn_arg_types, decl_arg_types))
2293 continue;
2294
2295 /* Save this template, and the arguments deduced. */
2296 templates = tree_cons (targs, fn, templates);
2297 }
2298 else if (need_member_template)
2299 /* FN is an ordinary member function, and we need a
2300 specialization of a member template. */
2301 ;
2302 else if (TREE_CODE (fn) != FUNCTION_DECL)
2303 /* We can get IDENTIFIER_NODEs here in certain erroneous
2304 cases. */
2305 ;
2306 else if (!DECL_FUNCTION_MEMBER_P (fn))
2307 /* This is just an ordinary non-member function. Nothing can
2308 be a specialization of that. */
2309 ;
2310 else if (DECL_ARTIFICIAL (fn))
2311 /* Cannot specialize functions that are created implicitly. */
2312 ;
2313 else
2314 {
2315 tree decl_arg_types;
2316
2317 /* This is an ordinary member function. However, since
2318 we're here, we can assume its enclosing class is a
2319 template class. For example,
2320
2321 template <typename T> struct S { void f(); };
2322 template <> void S<int>::f() {}
2323
2324 Here, S<int>::f is a non-template, but S<int> is a
2325 template class. If FN has the same type as DECL, we
2326 might be in business. */
2327
2328 if (!DECL_TEMPLATE_INFO (fn))
2329 /* Its enclosing class is an explicit specialization
2330 of a template class. This is not a candidate. */
2331 continue;
2332
2333 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2334 TREE_TYPE (TREE_TYPE (fn))))
2335 /* The return types differ. */
2336 continue;
2337
2338 /* Adjust the type of DECL in case FN is a static member. */
2339 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2340 if (DECL_STATIC_FUNCTION_P (fn)
2341 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2342 decl_arg_types = TREE_CHAIN (decl_arg_types);
2343
2344 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2345 decl_arg_types))
2346 continue;
2347
2348 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2349 && (type_memfn_rqual (TREE_TYPE (decl))
2350 != type_memfn_rqual (TREE_TYPE (fn))))
2351 continue;
2352
2353 // If the deduced arguments do not satisfy the constraints,
2354 // this is not a candidate.
2355 if (flag_concepts && !constraints_satisfied_p (fn))
2356 continue;
2357
2358 // Add the candidate.
2359 candidates = tree_cons (NULL_TREE, fn, candidates);
2360 }
2361 }
2362
2363 if (templates && TREE_CHAIN (templates))
2364 {
2365 /* We have:
2366
2367 [temp.expl.spec]
2368
2369 It is possible for a specialization with a given function
2370 signature to be instantiated from more than one function
2371 template. In such cases, explicit specification of the
2372 template arguments must be used to uniquely identify the
2373 function template specialization being specialized.
2374
2375 Note that here, there's no suggestion that we're supposed to
2376 determine which of the candidate templates is most
2377 specialized. However, we, also have:
2378
2379 [temp.func.order]
2380
2381 Partial ordering of overloaded function template
2382 declarations is used in the following contexts to select
2383 the function template to which a function template
2384 specialization refers:
2385
2386 -- when an explicit specialization refers to a function
2387 template.
2388
2389 So, we do use the partial ordering rules, at least for now.
2390 This extension can only serve to make invalid programs valid,
2391 so it's safe. And, there is strong anecdotal evidence that
2392 the committee intended the partial ordering rules to apply;
2393 the EDG front end has that behavior, and John Spicer claims
2394 that the committee simply forgot to delete the wording in
2395 [temp.expl.spec]. */
2396 tree tmpl = most_specialized_instantiation (templates);
2397 if (tmpl != error_mark_node)
2398 {
2399 templates = tmpl;
2400 TREE_CHAIN (templates) = NULL_TREE;
2401 }
2402 }
2403
2404 // Concepts allows multiple declarations of member functions
2405 // with the same signature. Like above, we need to rely on
2406 // on the partial ordering of those candidates to determine which
2407 // is the best.
2408 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2409 {
2410 if (tree cand = most_constrained_function (candidates))
2411 {
2412 candidates = cand;
2413 TREE_CHAIN (cand) = NULL_TREE;
2414 }
2415 }
2416
2417 if (templates == NULL_TREE && candidates == NULL_TREE)
2418 {
2419 error ("template-id %qD for %q+D does not match any template "
2420 "declaration", template_id, decl);
2421 if (header_count && header_count != template_count + 1)
2422 inform (DECL_SOURCE_LOCATION (decl),
2423 "saw %d %<template<>%>, need %d for "
2424 "specializing a member function template",
2425 header_count, template_count + 1);
2426 else
2427 print_candidates (orig_fns);
2428 return error_mark_node;
2429 }
2430 else if ((templates && TREE_CHAIN (templates))
2431 || (candidates && TREE_CHAIN (candidates))
2432 || (templates && candidates))
2433 {
2434 error ("ambiguous template specialization %qD for %q+D",
2435 template_id, decl);
2436 candidates = chainon (candidates, templates);
2437 print_candidates (candidates);
2438 return error_mark_node;
2439 }
2440
2441 /* We have one, and exactly one, match. */
2442 if (candidates)
2443 {
2444 tree fn = TREE_VALUE (candidates);
2445 *targs_out = copy_node (DECL_TI_ARGS (fn));
2446
2447 /* Propagate the candidate's constraints to the declaration. */
2448 set_constraints (decl, get_constraints (fn));
2449
2450 /* DECL is a re-declaration or partial instantiation of a template
2451 function. */
2452 if (TREE_CODE (fn) == TEMPLATE_DECL)
2453 return fn;
2454 /* It was a specialization of an ordinary member function in a
2455 template class. */
2456 return DECL_TI_TEMPLATE (fn);
2457 }
2458
2459 /* It was a specialization of a template. */
2460 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2461 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2462 {
2463 *targs_out = copy_node (targs);
2464 SET_TMPL_ARGS_LEVEL (*targs_out,
2465 TMPL_ARGS_DEPTH (*targs_out),
2466 TREE_PURPOSE (templates));
2467 }
2468 else
2469 *targs_out = TREE_PURPOSE (templates);
2470 return TREE_VALUE (templates);
2471 }
2472
2473 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2474 but with the default argument values filled in from those in the
2475 TMPL_TYPES. */
2476
2477 static tree
2478 copy_default_args_to_explicit_spec_1 (tree spec_types,
2479 tree tmpl_types)
2480 {
2481 tree new_spec_types;
2482
2483 if (!spec_types)
2484 return NULL_TREE;
2485
2486 if (spec_types == void_list_node)
2487 return void_list_node;
2488
2489 /* Substitute into the rest of the list. */
2490 new_spec_types =
2491 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2492 TREE_CHAIN (tmpl_types));
2493
2494 /* Add the default argument for this parameter. */
2495 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2496 TREE_VALUE (spec_types),
2497 new_spec_types);
2498 }
2499
2500 /* DECL is an explicit specialization. Replicate default arguments
2501 from the template it specializes. (That way, code like:
2502
2503 template <class T> void f(T = 3);
2504 template <> void f(double);
2505 void g () { f (); }
2506
2507 works, as required.) An alternative approach would be to look up
2508 the correct default arguments at the call-site, but this approach
2509 is consistent with how implicit instantiations are handled. */
2510
2511 static void
2512 copy_default_args_to_explicit_spec (tree decl)
2513 {
2514 tree tmpl;
2515 tree spec_types;
2516 tree tmpl_types;
2517 tree new_spec_types;
2518 tree old_type;
2519 tree new_type;
2520 tree t;
2521 tree object_type = NULL_TREE;
2522 tree in_charge = NULL_TREE;
2523 tree vtt = NULL_TREE;
2524
2525 /* See if there's anything we need to do. */
2526 tmpl = DECL_TI_TEMPLATE (decl);
2527 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2528 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2529 if (TREE_PURPOSE (t))
2530 break;
2531 if (!t)
2532 return;
2533
2534 old_type = TREE_TYPE (decl);
2535 spec_types = TYPE_ARG_TYPES (old_type);
2536
2537 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2538 {
2539 /* Remove the this pointer, but remember the object's type for
2540 CV quals. */
2541 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2542 spec_types = TREE_CHAIN (spec_types);
2543 tmpl_types = TREE_CHAIN (tmpl_types);
2544
2545 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2546 {
2547 /* DECL may contain more parameters than TMPL due to the extra
2548 in-charge parameter in constructors and destructors. */
2549 in_charge = spec_types;
2550 spec_types = TREE_CHAIN (spec_types);
2551 }
2552 if (DECL_HAS_VTT_PARM_P (decl))
2553 {
2554 vtt = spec_types;
2555 spec_types = TREE_CHAIN (spec_types);
2556 }
2557 }
2558
2559 /* Compute the merged default arguments. */
2560 new_spec_types =
2561 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2562
2563 /* Compute the new FUNCTION_TYPE. */
2564 if (object_type)
2565 {
2566 if (vtt)
2567 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2568 TREE_VALUE (vtt),
2569 new_spec_types);
2570
2571 if (in_charge)
2572 /* Put the in-charge parameter back. */
2573 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2574 TREE_VALUE (in_charge),
2575 new_spec_types);
2576
2577 new_type = build_method_type_directly (object_type,
2578 TREE_TYPE (old_type),
2579 new_spec_types);
2580 }
2581 else
2582 new_type = build_function_type (TREE_TYPE (old_type),
2583 new_spec_types);
2584 new_type = cp_build_type_attribute_variant (new_type,
2585 TYPE_ATTRIBUTES (old_type));
2586 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2587
2588 TREE_TYPE (decl) = new_type;
2589 }
2590
2591 /* Return the number of template headers we expect to see for a definition
2592 or specialization of CTYPE or one of its non-template members. */
2593
2594 int
2595 num_template_headers_for_class (tree ctype)
2596 {
2597 int num_templates = 0;
2598
2599 while (ctype && CLASS_TYPE_P (ctype))
2600 {
2601 /* You're supposed to have one `template <...>' for every
2602 template class, but you don't need one for a full
2603 specialization. For example:
2604
2605 template <class T> struct S{};
2606 template <> struct S<int> { void f(); };
2607 void S<int>::f () {}
2608
2609 is correct; there shouldn't be a `template <>' for the
2610 definition of `S<int>::f'. */
2611 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2612 /* If CTYPE does not have template information of any
2613 kind, then it is not a template, nor is it nested
2614 within a template. */
2615 break;
2616 if (explicit_class_specialization_p (ctype))
2617 break;
2618 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2619 ++num_templates;
2620
2621 ctype = TYPE_CONTEXT (ctype);
2622 }
2623
2624 return num_templates;
2625 }
2626
2627 /* Do a simple sanity check on the template headers that precede the
2628 variable declaration DECL. */
2629
2630 void
2631 check_template_variable (tree decl)
2632 {
2633 tree ctx = CP_DECL_CONTEXT (decl);
2634 int wanted = num_template_headers_for_class (ctx);
2635 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2636 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2637 {
2638 if (cxx_dialect < cxx14)
2639 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2640 "variable templates only available with "
2641 "%<-std=c++14%> or %<-std=gnu++14%>");
2642
2643 // Namespace-scope variable templates should have a template header.
2644 ++wanted;
2645 }
2646 if (template_header_count > wanted)
2647 {
2648 auto_diagnostic_group d;
2649 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2650 "too many template headers for %qD "
2651 "(should be %d)",
2652 decl, wanted);
2653 if (warned && CLASS_TYPE_P (ctx)
2654 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2655 inform (DECL_SOURCE_LOCATION (decl),
2656 "members of an explicitly specialized class are defined "
2657 "without a template header");
2658 }
2659 }
2660
2661 /* An explicit specialization whose declarator-id or class-head-name is not
2662 qualified shall be declared in the nearest enclosing namespace of the
2663 template, or, if the namespace is inline (7.3.1), any namespace from its
2664 enclosing namespace set.
2665
2666 If the name declared in the explicit instantiation is an unqualified name,
2667 the explicit instantiation shall appear in the namespace where its template
2668 is declared or, if that namespace is inline (7.3.1), any namespace from its
2669 enclosing namespace set. */
2670
2671 void
2672 check_unqualified_spec_or_inst (tree t, location_t loc)
2673 {
2674 tree tmpl = most_general_template (t);
2675 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2676 && !is_nested_namespace (current_namespace,
2677 CP_DECL_CONTEXT (tmpl), true))
2678 {
2679 if (processing_specialization)
2680 permerror (loc, "explicit specialization of %qD outside its "
2681 "namespace must use a nested-name-specifier", tmpl);
2682 else if (processing_explicit_instantiation
2683 && cxx_dialect >= cxx11)
2684 /* This was allowed in C++98, so only pedwarn. */
2685 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2686 "outside its namespace must use a nested-name-"
2687 "specifier", tmpl);
2688 }
2689 }
2690
2691 /* Warn for a template specialization SPEC that is missing some of a set
2692 of function or type attributes that the template TEMPL is declared with.
2693 ATTRLIST is a list of additional attributes that SPEC should be taken
2694 to ultimately be declared with. */
2695
2696 static void
2697 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2698 {
2699 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2700 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2701
2702 /* Avoid warning if the difference between the primary and
2703 the specialization is not in one of the attributes below. */
2704 const char* const blacklist[] = {
2705 "alloc_align", "alloc_size", "assume_aligned", "format",
2706 "format_arg", "malloc", "nonnull", NULL
2707 };
2708
2709 /* Put together a list of the black listed attributes that the primary
2710 template is declared with that the specialization is not, in case
2711 it's not apparent from the most recent declaration of the primary. */
2712 pretty_printer str;
2713 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2714 blacklist, &str);
2715
2716 if (!nattrs)
2717 return;
2718
2719 auto_diagnostic_group d;
2720 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2721 "explicit specialization %q#D may be missing attributes",
2722 spec))
2723 inform (DECL_SOURCE_LOCATION (tmpl),
2724 nattrs > 1
2725 ? G_("missing primary template attributes %s")
2726 : G_("missing primary template attribute %s"),
2727 pp_formatted_text (&str));
2728 }
2729
2730 /* Check to see if the function just declared, as indicated in
2731 DECLARATOR, and in DECL, is a specialization of a function
2732 template. We may also discover that the declaration is an explicit
2733 instantiation at this point.
2734
2735 Returns DECL, or an equivalent declaration that should be used
2736 instead if all goes well. Issues an error message if something is
2737 amiss. Returns error_mark_node if the error is not easily
2738 recoverable.
2739
2740 FLAGS is a bitmask consisting of the following flags:
2741
2742 2: The function has a definition.
2743 4: The function is a friend.
2744
2745 The TEMPLATE_COUNT is the number of references to qualifying
2746 template classes that appeared in the name of the function. For
2747 example, in
2748
2749 template <class T> struct S { void f(); };
2750 void S<int>::f();
2751
2752 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2753 classes are not counted in the TEMPLATE_COUNT, so that in
2754
2755 template <class T> struct S {};
2756 template <> struct S<int> { void f(); }
2757 template <> void S<int>::f();
2758
2759 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2760 invalid; there should be no template <>.)
2761
2762 If the function is a specialization, it is marked as such via
2763 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2764 is set up correctly, and it is added to the list of specializations
2765 for that template. */
2766
2767 tree
2768 check_explicit_specialization (tree declarator,
2769 tree decl,
2770 int template_count,
2771 int flags,
2772 tree attrlist)
2773 {
2774 int have_def = flags & 2;
2775 int is_friend = flags & 4;
2776 bool is_concept = flags & 8;
2777 int specialization = 0;
2778 int explicit_instantiation = 0;
2779 int member_specialization = 0;
2780 tree ctype = DECL_CLASS_CONTEXT (decl);
2781 tree dname = DECL_NAME (decl);
2782 tmpl_spec_kind tsk;
2783
2784 if (is_friend)
2785 {
2786 if (!processing_specialization)
2787 tsk = tsk_none;
2788 else
2789 tsk = tsk_excessive_parms;
2790 }
2791 else
2792 tsk = current_tmpl_spec_kind (template_count);
2793
2794 switch (tsk)
2795 {
2796 case tsk_none:
2797 if (processing_specialization && !VAR_P (decl))
2798 {
2799 specialization = 1;
2800 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2801 }
2802 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2803 {
2804 if (is_friend)
2805 /* This could be something like:
2806
2807 template <class T> void f(T);
2808 class S { friend void f<>(int); } */
2809 specialization = 1;
2810 else
2811 {
2812 /* This case handles bogus declarations like template <>
2813 template <class T> void f<int>(); */
2814
2815 error_at (cp_expr_loc_or_input_loc (declarator),
2816 "template-id %qE in declaration of primary template",
2817 declarator);
2818 return decl;
2819 }
2820 }
2821 break;
2822
2823 case tsk_invalid_member_spec:
2824 /* The error has already been reported in
2825 check_specialization_scope. */
2826 return error_mark_node;
2827
2828 case tsk_invalid_expl_inst:
2829 error ("template parameter list used in explicit instantiation");
2830
2831 /* Fall through. */
2832
2833 case tsk_expl_inst:
2834 if (have_def)
2835 error ("definition provided for explicit instantiation");
2836
2837 explicit_instantiation = 1;
2838 break;
2839
2840 case tsk_excessive_parms:
2841 case tsk_insufficient_parms:
2842 if (tsk == tsk_excessive_parms)
2843 error ("too many template parameter lists in declaration of %qD",
2844 decl);
2845 else if (template_header_count)
2846 error("too few template parameter lists in declaration of %qD", decl);
2847 else
2848 error("explicit specialization of %qD must be introduced by "
2849 "%<template <>%>", decl);
2850
2851 /* Fall through. */
2852 case tsk_expl_spec:
2853 if (is_concept)
2854 error ("explicit specialization declared %<concept%>");
2855
2856 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2857 /* In cases like template<> constexpr bool v = true;
2858 We'll give an error in check_template_variable. */
2859 break;
2860
2861 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2862 if (ctype)
2863 member_specialization = 1;
2864 else
2865 specialization = 1;
2866 break;
2867
2868 case tsk_template:
2869 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2870 {
2871 /* This case handles bogus declarations like template <>
2872 template <class T> void f<int>(); */
2873
2874 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2875 error_at (cp_expr_loc_or_input_loc (declarator),
2876 "template-id %qE in declaration of primary template",
2877 declarator);
2878 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2879 {
2880 /* Partial specialization of variable template. */
2881 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2882 specialization = 1;
2883 goto ok;
2884 }
2885 else if (cxx_dialect < cxx14)
2886 error_at (cp_expr_loc_or_input_loc (declarator),
2887 "non-type partial specialization %qE "
2888 "is not allowed", declarator);
2889 else
2890 error_at (cp_expr_loc_or_input_loc (declarator),
2891 "non-class, non-variable partial specialization %qE "
2892 "is not allowed", declarator);
2893 return decl;
2894 ok:;
2895 }
2896
2897 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2898 /* This is a specialization of a member template, without
2899 specialization the containing class. Something like:
2900
2901 template <class T> struct S {
2902 template <class U> void f (U);
2903 };
2904 template <> template <class U> void S<int>::f(U) {}
2905
2906 That's a specialization -- but of the entire template. */
2907 specialization = 1;
2908 break;
2909
2910 default:
2911 gcc_unreachable ();
2912 }
2913
2914 if ((specialization || member_specialization)
2915 /* This doesn't apply to variable templates. */
2916 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2917 {
2918 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2919 for (; t; t = TREE_CHAIN (t))
2920 if (TREE_PURPOSE (t))
2921 {
2922 permerror (input_location,
2923 "default argument specified in explicit specialization");
2924 break;
2925 }
2926 }
2927
2928 if (specialization || member_specialization || explicit_instantiation)
2929 {
2930 tree tmpl = NULL_TREE;
2931 tree targs = NULL_TREE;
2932 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2933
2934 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2935 if (!was_template_id)
2936 {
2937 tree fns;
2938
2939 gcc_assert (identifier_p (declarator));
2940 if (ctype)
2941 fns = dname;
2942 else
2943 {
2944 /* If there is no class context, the explicit instantiation
2945 must be at namespace scope. */
2946 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2947
2948 /* Find the namespace binding, using the declaration
2949 context. */
2950 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2951 false, true);
2952 if (fns == error_mark_node)
2953 /* If lookup fails, look for a friend declaration so we can
2954 give a better diagnostic. */
2955 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2956 /*type*/false, /*complain*/true,
2957 /*hidden*/true);
2958
2959 if (fns == error_mark_node || !is_overloaded_fn (fns))
2960 {
2961 error ("%qD is not a template function", dname);
2962 fns = error_mark_node;
2963 }
2964 }
2965
2966 declarator = lookup_template_function (fns, NULL_TREE);
2967 }
2968
2969 if (declarator == error_mark_node)
2970 return error_mark_node;
2971
2972 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2973 {
2974 if (!explicit_instantiation)
2975 /* A specialization in class scope. This is invalid,
2976 but the error will already have been flagged by
2977 check_specialization_scope. */
2978 return error_mark_node;
2979 else
2980 {
2981 /* It's not valid to write an explicit instantiation in
2982 class scope, e.g.:
2983
2984 class C { template void f(); }
2985
2986 This case is caught by the parser. However, on
2987 something like:
2988
2989 template class C { void f(); };
2990
2991 (which is invalid) we can get here. The error will be
2992 issued later. */
2993 ;
2994 }
2995
2996 return decl;
2997 }
2998 else if (ctype != NULL_TREE
2999 && (identifier_p (TREE_OPERAND (declarator, 0))))
3000 {
3001 // We'll match variable templates in start_decl.
3002 if (VAR_P (decl))
3003 return decl;
3004
3005 /* Find the list of functions in ctype that have the same
3006 name as the declared function. */
3007 tree name = TREE_OPERAND (declarator, 0);
3008
3009 if (constructor_name_p (name, ctype))
3010 {
3011 if (DECL_CONSTRUCTOR_P (decl)
3012 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3013 : !CLASSTYPE_DESTRUCTOR (ctype))
3014 {
3015 /* From [temp.expl.spec]:
3016
3017 If such an explicit specialization for the member
3018 of a class template names an implicitly-declared
3019 special member function (clause _special_), the
3020 program is ill-formed.
3021
3022 Similar language is found in [temp.explicit]. */
3023 error ("specialization of implicitly-declared special member function");
3024 return error_mark_node;
3025 }
3026
3027 name = DECL_NAME (decl);
3028 }
3029
3030 /* For a type-conversion operator, We might be looking for
3031 `operator int' which will be a specialization of
3032 `operator T'. Grab all the conversion operators, and
3033 then select from them. */
3034 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3035 ? conv_op_identifier : name);
3036
3037 if (fns == NULL_TREE)
3038 {
3039 error ("no member function %qD declared in %qT", name, ctype);
3040 return error_mark_node;
3041 }
3042 else
3043 TREE_OPERAND (declarator, 0) = fns;
3044 }
3045
3046 /* Figure out what exactly is being specialized at this point.
3047 Note that for an explicit instantiation, even one for a
3048 member function, we cannot tell a priori whether the
3049 instantiation is for a member template, or just a member
3050 function of a template class. Even if a member template is
3051 being instantiated, the member template arguments may be
3052 elided if they can be deduced from the rest of the
3053 declaration. */
3054 tmpl = determine_specialization (declarator, decl,
3055 &targs,
3056 member_specialization,
3057 template_count,
3058 tsk);
3059
3060 if (!tmpl || tmpl == error_mark_node)
3061 /* We couldn't figure out what this declaration was
3062 specializing. */
3063 return error_mark_node;
3064 else
3065 {
3066 if (TREE_CODE (decl) == FUNCTION_DECL
3067 && DECL_HIDDEN_FRIEND_P (tmpl))
3068 {
3069 auto_diagnostic_group d;
3070 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3071 "friend declaration %qD is not visible to "
3072 "explicit specialization", tmpl))
3073 inform (DECL_SOURCE_LOCATION (tmpl),
3074 "friend declaration here");
3075 }
3076 else if (!ctype && !is_friend
3077 && CP_DECL_CONTEXT (decl) == current_namespace)
3078 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3079
3080 tree gen_tmpl = most_general_template (tmpl);
3081
3082 if (explicit_instantiation)
3083 {
3084 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3085 is done by do_decl_instantiation later. */
3086
3087 int arg_depth = TMPL_ARGS_DEPTH (targs);
3088 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3089
3090 if (arg_depth > parm_depth)
3091 {
3092 /* If TMPL is not the most general template (for
3093 example, if TMPL is a friend template that is
3094 injected into namespace scope), then there will
3095 be too many levels of TARGS. Remove some of them
3096 here. */
3097 int i;
3098 tree new_targs;
3099
3100 new_targs = make_tree_vec (parm_depth);
3101 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3102 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3103 = TREE_VEC_ELT (targs, i);
3104 targs = new_targs;
3105 }
3106
3107 return instantiate_template (tmpl, targs, tf_error);
3108 }
3109
3110 /* If we thought that the DECL was a member function, but it
3111 turns out to be specializing a static member function,
3112 make DECL a static member function as well. */
3113 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3114 && DECL_STATIC_FUNCTION_P (tmpl)
3115 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3116 revert_static_member_fn (decl);
3117
3118 /* If this is a specialization of a member template of a
3119 template class, we want to return the TEMPLATE_DECL, not
3120 the specialization of it. */
3121 if (tsk == tsk_template && !was_template_id)
3122 {
3123 tree result = DECL_TEMPLATE_RESULT (tmpl);
3124 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3125 DECL_INITIAL (result) = NULL_TREE;
3126 if (have_def)
3127 {
3128 tree parm;
3129 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3130 DECL_SOURCE_LOCATION (result)
3131 = DECL_SOURCE_LOCATION (decl);
3132 /* We want to use the argument list specified in the
3133 definition, not in the original declaration. */
3134 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3135 for (parm = DECL_ARGUMENTS (result); parm;
3136 parm = DECL_CHAIN (parm))
3137 DECL_CONTEXT (parm) = result;
3138 }
3139 return register_specialization (tmpl, gen_tmpl, targs,
3140 is_friend, 0);
3141 }
3142
3143 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3144 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3145
3146 if (was_template_id)
3147 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3148
3149 /* Inherit default function arguments from the template
3150 DECL is specializing. */
3151 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3152 copy_default_args_to_explicit_spec (decl);
3153
3154 /* This specialization has the same protection as the
3155 template it specializes. */
3156 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3157 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3158
3159 /* 7.1.1-1 [dcl.stc]
3160
3161 A storage-class-specifier shall not be specified in an
3162 explicit specialization...
3163
3164 The parser rejects these, so unless action is taken here,
3165 explicit function specializations will always appear with
3166 global linkage.
3167
3168 The action recommended by the C++ CWG in response to C++
3169 defect report 605 is to make the storage class and linkage
3170 of the explicit specialization match the templated function:
3171
3172 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3173 */
3174 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3175 {
3176 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3177 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3178
3179 /* A concept cannot be specialized. */
3180 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3181 {
3182 error ("explicit specialization of function concept %qD",
3183 gen_tmpl);
3184 return error_mark_node;
3185 }
3186
3187 /* This specialization has the same linkage and visibility as
3188 the function template it specializes. */
3189 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3190 if (! TREE_PUBLIC (decl))
3191 {
3192 DECL_INTERFACE_KNOWN (decl) = 1;
3193 DECL_NOT_REALLY_EXTERN (decl) = 1;
3194 }
3195 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3196 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3197 {
3198 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3199 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3200 }
3201 }
3202
3203 /* If DECL is a friend declaration, declared using an
3204 unqualified name, the namespace associated with DECL may
3205 have been set incorrectly. For example, in:
3206
3207 template <typename T> void f(T);
3208 namespace N {
3209 struct S { friend void f<int>(int); }
3210 }
3211
3212 we will have set the DECL_CONTEXT for the friend
3213 declaration to N, rather than to the global namespace. */
3214 if (DECL_NAMESPACE_SCOPE_P (decl))
3215 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3216
3217 if (is_friend && !have_def)
3218 /* This is not really a declaration of a specialization.
3219 It's just the name of an instantiation. But, it's not
3220 a request for an instantiation, either. */
3221 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3222 else if (TREE_CODE (decl) == FUNCTION_DECL)
3223 /* A specialization is not necessarily COMDAT. */
3224 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3225 && DECL_DECLARED_INLINE_P (decl));
3226 else if (VAR_P (decl))
3227 DECL_COMDAT (decl) = false;
3228
3229 /* If this is a full specialization, register it so that we can find
3230 it again. Partial specializations will be registered in
3231 process_partial_specialization. */
3232 if (!processing_template_decl)
3233 {
3234 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3235
3236 decl = register_specialization (decl, gen_tmpl, targs,
3237 is_friend, 0);
3238 }
3239
3240
3241 /* A 'structor should already have clones. */
3242 gcc_assert (decl == error_mark_node
3243 || variable_template_p (tmpl)
3244 || !(DECL_CONSTRUCTOR_P (decl)
3245 || DECL_DESTRUCTOR_P (decl))
3246 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3247 }
3248 }
3249
3250 return decl;
3251 }
3252
3253 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3254 parameters. These are represented in the same format used for
3255 DECL_TEMPLATE_PARMS. */
3256
3257 int
3258 comp_template_parms (const_tree parms1, const_tree parms2)
3259 {
3260 const_tree p1;
3261 const_tree p2;
3262
3263 if (parms1 == parms2)
3264 return 1;
3265
3266 for (p1 = parms1, p2 = parms2;
3267 p1 != NULL_TREE && p2 != NULL_TREE;
3268 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3269 {
3270 tree t1 = TREE_VALUE (p1);
3271 tree t2 = TREE_VALUE (p2);
3272 int i;
3273
3274 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3275 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3276
3277 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3278 return 0;
3279
3280 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3281 {
3282 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3283 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3284
3285 /* If either of the template parameters are invalid, assume
3286 they match for the sake of error recovery. */
3287 if (error_operand_p (parm1) || error_operand_p (parm2))
3288 return 1;
3289
3290 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3291 return 0;
3292
3293 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3294 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3295 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3296 continue;
3297 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3298 return 0;
3299 }
3300 }
3301
3302 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3303 /* One set of parameters has more parameters lists than the
3304 other. */
3305 return 0;
3306
3307 return 1;
3308 }
3309
3310 /* Returns true if two template parameters are declared with
3311 equivalent constraints. */
3312
3313 static bool
3314 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3315 {
3316 tree req1 = TREE_TYPE (parm1);
3317 tree req2 = TREE_TYPE (parm2);
3318 if (!req1 != !req2)
3319 return false;
3320 if (req1)
3321 return cp_tree_equal (req1, req2);
3322 return true;
3323 }
3324
3325 /* Returns true when two template parameters are equivalent. */
3326
3327 static bool
3328 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3329 {
3330 tree decl1 = TREE_VALUE (parm1);
3331 tree decl2 = TREE_VALUE (parm2);
3332
3333 /* If either of the template parameters are invalid, assume
3334 they match for the sake of error recovery. */
3335 if (error_operand_p (decl1) || error_operand_p (decl2))
3336 return true;
3337
3338 /* ... they declare parameters of the same kind. */
3339 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3340 return false;
3341
3342 /* ... one parameter was introduced by a parameter declaration, then
3343 both are. This case arises as a result of eagerly rewriting declarations
3344 during parsing. */
3345 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3346 return false;
3347
3348 /* ... if either declares a pack, they both do. */
3349 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3350 return false;
3351
3352 if (TREE_CODE (decl1) == PARM_DECL)
3353 {
3354 /* ... if they declare non-type parameters, the types are equivalent. */
3355 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3356 return false;
3357 }
3358 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3359 {
3360 /* ... if they declare template template parameters, their template
3361 parameter lists are equivalent. */
3362 if (!template_heads_equivalent_p (decl1, decl2))
3363 return false;
3364 }
3365
3366 /* ... if they are declared with a qualified-concept name, they both
3367 are, and those names are equivalent. */
3368 return template_parameter_constraints_equivalent_p (parm1, parm2);
3369 }
3370
3371 /* Returns true if two template parameters lists are equivalent.
3372 Two template parameter lists are equivalent if they have the
3373 same length and their corresponding parameters are equivalent.
3374
3375 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3376 data structure returned by DECL_TEMPLATE_PARMS.
3377
3378 This is generally the same implementation as comp_template_parms
3379 except that it also the concept names and arguments used to
3380 introduce parameters. */
3381
3382 static bool
3383 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3384 {
3385 if (parms1 == parms2)
3386 return true;
3387
3388 const_tree p1 = parms1;
3389 const_tree p2 = parms2;
3390 while (p1 != NULL_TREE && p2 != NULL_TREE)
3391 {
3392 tree list1 = TREE_VALUE (p1);
3393 tree list2 = TREE_VALUE (p2);
3394
3395 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3396 return 0;
3397
3398 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3399 {
3400 tree parm1 = TREE_VEC_ELT (list1, i);
3401 tree parm2 = TREE_VEC_ELT (list2, i);
3402 if (!template_parameters_equivalent_p (parm1, parm2))
3403 return false;
3404 }
3405
3406 p1 = TREE_CHAIN (p1);
3407 p2 = TREE_CHAIN (p2);
3408 }
3409
3410 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3411 return false;
3412
3413 return true;
3414 }
3415
3416 /* Return true if the requires-clause of the template parameter lists are
3417 equivalent and false otherwise. */
3418 static bool
3419 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3420 {
3421 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3422 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3423 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3424 return false;
3425 if (!cp_tree_equal (req1, req2))
3426 return false;
3427 return true;
3428 }
3429
3430 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3431 Two template heads are equivalent if their template parameter
3432 lists are equivalent and their requires clauses are equivalent.
3433
3434 In pre-C++20, this is equivalent to calling comp_template_parms
3435 for the template parameters of TMPL1 and TMPL2. */
3436
3437 bool
3438 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3439 {
3440 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3441 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3442
3443 /* Don't change the matching rules for pre-C++20. */
3444 if (cxx_dialect < cxx2a)
3445 return comp_template_parms (parms1, parms2);
3446
3447 /* ... have the same number of template parameters, and their
3448 corresponding parameters are equivalent. */
3449 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3450 return false;
3451
3452 /* ... if either has a requires-clause, they both do and their
3453 corresponding constraint-expressions are equivalent. */
3454 return template_requirements_equivalent_p (parms1, parms2);
3455 }
3456
3457 /* Determine whether PARM is a parameter pack. */
3458
3459 bool
3460 template_parameter_pack_p (const_tree parm)
3461 {
3462 /* Determine if we have a non-type template parameter pack. */
3463 if (TREE_CODE (parm) == PARM_DECL)
3464 return (DECL_TEMPLATE_PARM_P (parm)
3465 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3466 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3467 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3468
3469 /* If this is a list of template parameters, we could get a
3470 TYPE_DECL or a TEMPLATE_DECL. */
3471 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3472 parm = TREE_TYPE (parm);
3473
3474 /* Otherwise it must be a type template parameter. */
3475 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3476 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3477 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3478 }
3479
3480 /* Determine if T is a function parameter pack. */
3481
3482 bool
3483 function_parameter_pack_p (const_tree t)
3484 {
3485 if (t && TREE_CODE (t) == PARM_DECL)
3486 return DECL_PACK_P (t);
3487 return false;
3488 }
3489
3490 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3491 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3492
3493 tree
3494 get_function_template_decl (const_tree primary_func_tmpl_inst)
3495 {
3496 if (! primary_func_tmpl_inst
3497 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3498 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3499 return NULL;
3500
3501 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3502 }
3503
3504 /* Return true iff the function parameter PARAM_DECL was expanded
3505 from the function parameter pack PACK. */
3506
3507 bool
3508 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3509 {
3510 if (DECL_ARTIFICIAL (param_decl)
3511 || !function_parameter_pack_p (pack))
3512 return false;
3513
3514 /* The parameter pack and its pack arguments have the same
3515 DECL_PARM_INDEX. */
3516 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3517 }
3518
3519 /* Determine whether ARGS describes a variadic template args list,
3520 i.e., one that is terminated by a template argument pack. */
3521
3522 static bool
3523 template_args_variadic_p (tree args)
3524 {
3525 int nargs;
3526 tree last_parm;
3527
3528 if (args == NULL_TREE)
3529 return false;
3530
3531 args = INNERMOST_TEMPLATE_ARGS (args);
3532 nargs = TREE_VEC_LENGTH (args);
3533
3534 if (nargs == 0)
3535 return false;
3536
3537 last_parm = TREE_VEC_ELT (args, nargs - 1);
3538
3539 return ARGUMENT_PACK_P (last_parm);
3540 }
3541
3542 /* Generate a new name for the parameter pack name NAME (an
3543 IDENTIFIER_NODE) that incorporates its */
3544
3545 static tree
3546 make_ith_pack_parameter_name (tree name, int i)
3547 {
3548 /* Munge the name to include the parameter index. */
3549 #define NUMBUF_LEN 128
3550 char numbuf[NUMBUF_LEN];
3551 char* newname;
3552 int newname_len;
3553
3554 if (name == NULL_TREE)
3555 return name;
3556 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3557 newname_len = IDENTIFIER_LENGTH (name)
3558 + strlen (numbuf) + 2;
3559 newname = (char*)alloca (newname_len);
3560 snprintf (newname, newname_len,
3561 "%s#%i", IDENTIFIER_POINTER (name), i);
3562 return get_identifier (newname);
3563 }
3564
3565 /* Return true if T is a primary function, class or alias template
3566 specialization, not including the template pattern. */
3567
3568 bool
3569 primary_template_specialization_p (const_tree t)
3570 {
3571 if (!t)
3572 return false;
3573
3574 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3575 return (DECL_LANG_SPECIFIC (t)
3576 && DECL_USE_TEMPLATE (t)
3577 && DECL_TEMPLATE_INFO (t)
3578 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3579 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3580 return (CLASSTYPE_TEMPLATE_INFO (t)
3581 && CLASSTYPE_USE_TEMPLATE (t)
3582 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3583 else if (alias_template_specialization_p (t, nt_transparent))
3584 return true;
3585 return false;
3586 }
3587
3588 /* Return true if PARM is a template template parameter. */
3589
3590 bool
3591 template_template_parameter_p (const_tree parm)
3592 {
3593 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3594 }
3595
3596 /* Return true iff PARM is a DECL representing a type template
3597 parameter. */
3598
3599 bool
3600 template_type_parameter_p (const_tree parm)
3601 {
3602 return (parm
3603 && (TREE_CODE (parm) == TYPE_DECL
3604 || TREE_CODE (parm) == TEMPLATE_DECL)
3605 && DECL_TEMPLATE_PARM_P (parm));
3606 }
3607
3608 /* Return the template parameters of T if T is a
3609 primary template instantiation, NULL otherwise. */
3610
3611 tree
3612 get_primary_template_innermost_parameters (const_tree t)
3613 {
3614 tree parms = NULL, template_info = NULL;
3615
3616 if ((template_info = get_template_info (t))
3617 && primary_template_specialization_p (t))
3618 parms = INNERMOST_TEMPLATE_PARMS
3619 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3620
3621 return parms;
3622 }
3623
3624 /* Return the template parameters of the LEVELth level from the full list
3625 of template parameters PARMS. */
3626
3627 tree
3628 get_template_parms_at_level (tree parms, int level)
3629 {
3630 tree p;
3631 if (!parms
3632 || TREE_CODE (parms) != TREE_LIST
3633 || level > TMPL_PARMS_DEPTH (parms))
3634 return NULL_TREE;
3635
3636 for (p = parms; p; p = TREE_CHAIN (p))
3637 if (TMPL_PARMS_DEPTH (p) == level)
3638 return p;
3639
3640 return NULL_TREE;
3641 }
3642
3643 /* Returns the template arguments of T if T is a template instantiation,
3644 NULL otherwise. */
3645
3646 tree
3647 get_template_innermost_arguments (const_tree t)
3648 {
3649 tree args = NULL, template_info = NULL;
3650
3651 if ((template_info = get_template_info (t))
3652 && TI_ARGS (template_info))
3653 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3654
3655 return args;
3656 }
3657
3658 /* Return the argument pack elements of T if T is a template argument pack,
3659 NULL otherwise. */
3660
3661 tree
3662 get_template_argument_pack_elems (const_tree t)
3663 {
3664 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3665 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3666 return NULL;
3667
3668 return ARGUMENT_PACK_ARGS (t);
3669 }
3670
3671 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3672 ARGUMENT_PACK_SELECT represents. */
3673
3674 static tree
3675 argument_pack_select_arg (tree t)
3676 {
3677 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3678 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3679
3680 /* If the selected argument is an expansion E, that most likely means we were
3681 called from gen_elem_of_pack_expansion_instantiation during the
3682 substituting of an argument pack (of which the Ith element is a pack
3683 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3684 In this case, the Ith element resulting from this substituting is going to
3685 be a pack expansion, which pattern is the pattern of E. Let's return the
3686 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3687 resulting pack expansion from it. */
3688 if (PACK_EXPANSION_P (arg))
3689 {
3690 /* Make sure we aren't throwing away arg info. */
3691 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3692 arg = PACK_EXPANSION_PATTERN (arg);
3693 }
3694
3695 return arg;
3696 }
3697
3698
3699 /* True iff FN is a function representing a built-in variadic parameter
3700 pack. */
3701
3702 bool
3703 builtin_pack_fn_p (tree fn)
3704 {
3705 if (!fn
3706 || TREE_CODE (fn) != FUNCTION_DECL
3707 || !DECL_IS_BUILTIN (fn))
3708 return false;
3709
3710 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3711 return true;
3712
3713 return false;
3714 }
3715
3716 /* True iff CALL is a call to a function representing a built-in variadic
3717 parameter pack. */
3718
3719 static bool
3720 builtin_pack_call_p (tree call)
3721 {
3722 if (TREE_CODE (call) != CALL_EXPR)
3723 return false;
3724 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3725 }
3726
3727 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3728
3729 static tree
3730 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3731 tree in_decl)
3732 {
3733 tree ohi = CALL_EXPR_ARG (call, 0);
3734 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3735 false/*fn*/, true/*int_cst*/);
3736
3737 if (value_dependent_expression_p (hi))
3738 {
3739 if (hi != ohi)
3740 {
3741 call = copy_node (call);
3742 CALL_EXPR_ARG (call, 0) = hi;
3743 }
3744 tree ex = make_pack_expansion (call, complain);
3745 tree vec = make_tree_vec (1);
3746 TREE_VEC_ELT (vec, 0) = ex;
3747 return vec;
3748 }
3749 else
3750 {
3751 hi = cxx_constant_value (hi);
3752 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3753
3754 /* Calculate the largest value of len that won't make the size of the vec
3755 overflow an int. The compiler will exceed resource limits long before
3756 this, but it seems a decent place to diagnose. */
3757 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3758
3759 if (len < 0 || len > max)
3760 {
3761 if ((complain & tf_error)
3762 && hi != error_mark_node)
3763 error ("argument to %<__integer_pack%> must be between 0 and %d",
3764 max);
3765 return error_mark_node;
3766 }
3767
3768 tree vec = make_tree_vec (len);
3769
3770 for (int i = 0; i < len; ++i)
3771 TREE_VEC_ELT (vec, i) = size_int (i);
3772
3773 return vec;
3774 }
3775 }
3776
3777 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3778 CALL. */
3779
3780 static tree
3781 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3782 tree in_decl)
3783 {
3784 if (!builtin_pack_call_p (call))
3785 return NULL_TREE;
3786
3787 tree fn = CALL_EXPR_FN (call);
3788
3789 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3790 return expand_integer_pack (call, args, complain, in_decl);
3791
3792 return NULL_TREE;
3793 }
3794
3795 /* Structure used to track the progress of find_parameter_packs_r. */
3796 struct find_parameter_pack_data
3797 {
3798 /* TREE_LIST that will contain all of the parameter packs found by
3799 the traversal. */
3800 tree* parameter_packs;
3801
3802 /* Set of AST nodes that have been visited by the traversal. */
3803 hash_set<tree> *visited;
3804
3805 /* True iff we're making a type pack expansion. */
3806 bool type_pack_expansion_p;
3807 };
3808
3809 /* Identifies all of the argument packs that occur in a template
3810 argument and appends them to the TREE_LIST inside DATA, which is a
3811 find_parameter_pack_data structure. This is a subroutine of
3812 make_pack_expansion and uses_parameter_packs. */
3813 static tree
3814 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3815 {
3816 tree t = *tp;
3817 struct find_parameter_pack_data* ppd =
3818 (struct find_parameter_pack_data*)data;
3819 bool parameter_pack_p = false;
3820
3821 /* Don't look through typedefs; we are interested in whether a
3822 parameter pack is actually written in the expression/type we're
3823 looking at, not the target type. */
3824 if (TYPE_P (t) && typedef_variant_p (t))
3825 {
3826 /* But do look at arguments for an alias template. */
3827 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3828 cp_walk_tree (&TI_ARGS (tinfo),
3829 &find_parameter_packs_r,
3830 ppd, ppd->visited);
3831 *walk_subtrees = 0;
3832 return NULL_TREE;
3833 }
3834
3835 /* Identify whether this is a parameter pack or not. */
3836 switch (TREE_CODE (t))
3837 {
3838 case TEMPLATE_PARM_INDEX:
3839 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3840 parameter_pack_p = true;
3841 break;
3842
3843 case TEMPLATE_TYPE_PARM:
3844 t = TYPE_MAIN_VARIANT (t);
3845 /* FALLTHRU */
3846 case TEMPLATE_TEMPLATE_PARM:
3847 /* If the placeholder appears in the decl-specifier-seq of a function
3848 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3849 is a pack expansion, the invented template parameter is a template
3850 parameter pack. */
3851 if (ppd->type_pack_expansion_p && is_auto (t))
3852 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3853 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3854 parameter_pack_p = true;
3855 break;
3856
3857 case FIELD_DECL:
3858 case PARM_DECL:
3859 if (DECL_PACK_P (t))
3860 {
3861 /* We don't want to walk into the type of a PARM_DECL,
3862 because we don't want to see the type parameter pack. */
3863 *walk_subtrees = 0;
3864 parameter_pack_p = true;
3865 }
3866 break;
3867
3868 case VAR_DECL:
3869 if (DECL_PACK_P (t))
3870 {
3871 /* We don't want to walk into the type of a variadic capture proxy,
3872 because we don't want to see the type parameter pack. */
3873 *walk_subtrees = 0;
3874 parameter_pack_p = true;
3875 }
3876 else if (variable_template_specialization_p (t))
3877 {
3878 cp_walk_tree (&DECL_TI_ARGS (t),
3879 find_parameter_packs_r,
3880 ppd, ppd->visited);
3881 *walk_subtrees = 0;
3882 }
3883 break;
3884
3885 case CALL_EXPR:
3886 if (builtin_pack_call_p (t))
3887 parameter_pack_p = true;
3888 break;
3889
3890 case BASES:
3891 parameter_pack_p = true;
3892 break;
3893 default:
3894 /* Not a parameter pack. */
3895 break;
3896 }
3897
3898 if (parameter_pack_p)
3899 {
3900 /* Add this parameter pack to the list. */
3901 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3902 }
3903
3904 if (TYPE_P (t))
3905 cp_walk_tree (&TYPE_CONTEXT (t),
3906 &find_parameter_packs_r, ppd, ppd->visited);
3907
3908 /* This switch statement will return immediately if we don't find a
3909 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3910 switch (TREE_CODE (t))
3911 {
3912 case BOUND_TEMPLATE_TEMPLATE_PARM:
3913 /* Check the template itself. */
3914 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3915 &find_parameter_packs_r, ppd, ppd->visited);
3916 return NULL_TREE;
3917
3918 case DECL_EXPR:
3919 {
3920 tree decl = DECL_EXPR_DECL (t);
3921 /* Ignore the declaration of a capture proxy for a parameter pack. */
3922 if (is_capture_proxy (decl))
3923 *walk_subtrees = 0;
3924 if (is_typedef_decl (decl))
3925 /* Since we stop at typedefs above, we need to look through them at
3926 the point of the DECL_EXPR. */
3927 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3928 &find_parameter_packs_r, ppd, ppd->visited);
3929 return NULL_TREE;
3930 }
3931
3932 case TEMPLATE_DECL:
3933 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3934 return NULL_TREE;
3935 cp_walk_tree (&TREE_TYPE (t),
3936 &find_parameter_packs_r, ppd, ppd->visited);
3937 return NULL_TREE;
3938
3939 case TYPENAME_TYPE:
3940 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3941 ppd, ppd->visited);
3942 *walk_subtrees = 0;
3943 return NULL_TREE;
3944
3945 case TYPE_PACK_EXPANSION:
3946 case EXPR_PACK_EXPANSION:
3947 *walk_subtrees = 0;
3948 return NULL_TREE;
3949
3950 case INTEGER_TYPE:
3951 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3952 ppd, ppd->visited);
3953 *walk_subtrees = 0;
3954 return NULL_TREE;
3955
3956 case IDENTIFIER_NODE:
3957 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3958 ppd->visited);
3959 *walk_subtrees = 0;
3960 return NULL_TREE;
3961
3962 case LAMBDA_EXPR:
3963 {
3964 /* Look at explicit captures. */
3965 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3966 cap; cap = TREE_CHAIN (cap))
3967 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3968 ppd->visited);
3969 /* Since we defer implicit capture, look in the parms and body. */
3970 tree fn = lambda_function (t);
3971 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3972 ppd->visited);
3973 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3974 ppd->visited);
3975 *walk_subtrees = 0;
3976 return NULL_TREE;
3977 }
3978
3979 case DECLTYPE_TYPE:
3980 {
3981 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3982 type_pack_expansion_p to false so that any placeholders
3983 within the expression don't get marked as parameter packs. */
3984 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3985 ppd->type_pack_expansion_p = false;
3986 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3987 ppd, ppd->visited);
3988 ppd->type_pack_expansion_p = type_pack_expansion_p;
3989 *walk_subtrees = 0;
3990 return NULL_TREE;
3991 }
3992
3993 case IF_STMT:
3994 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3995 ppd, ppd->visited);
3996 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3997 ppd, ppd->visited);
3998 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3999 ppd, ppd->visited);
4000 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4001 *walk_subtrees = 0;
4002 return NULL_TREE;
4003
4004 default:
4005 return NULL_TREE;
4006 }
4007
4008 return NULL_TREE;
4009 }
4010
4011 /* Determines if the expression or type T uses any parameter packs. */
4012 tree
4013 uses_parameter_packs (tree t)
4014 {
4015 tree parameter_packs = NULL_TREE;
4016 struct find_parameter_pack_data ppd;
4017 ppd.parameter_packs = &parameter_packs;
4018 ppd.visited = new hash_set<tree>;
4019 ppd.type_pack_expansion_p = false;
4020 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4021 delete ppd.visited;
4022 return parameter_packs;
4023 }
4024
4025 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4026 representation a base-class initializer into a parameter pack
4027 expansion. If all goes well, the resulting node will be an
4028 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4029 respectively. */
4030 tree
4031 make_pack_expansion (tree arg, tsubst_flags_t complain)
4032 {
4033 tree result;
4034 tree parameter_packs = NULL_TREE;
4035 bool for_types = false;
4036 struct find_parameter_pack_data ppd;
4037
4038 if (!arg || arg == error_mark_node)
4039 return arg;
4040
4041 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4042 {
4043 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4044 class initializer. In this case, the TREE_PURPOSE will be a
4045 _TYPE node (representing the base class expansion we're
4046 initializing) and the TREE_VALUE will be a TREE_LIST
4047 containing the initialization arguments.
4048
4049 The resulting expansion looks somewhat different from most
4050 expansions. Rather than returning just one _EXPANSION, we
4051 return a TREE_LIST whose TREE_PURPOSE is a
4052 TYPE_PACK_EXPANSION containing the bases that will be
4053 initialized. The TREE_VALUE will be identical to the
4054 original TREE_VALUE, which is a list of arguments that will
4055 be passed to each base. We do not introduce any new pack
4056 expansion nodes into the TREE_VALUE (although it is possible
4057 that some already exist), because the TREE_PURPOSE and
4058 TREE_VALUE all need to be expanded together with the same
4059 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4060 resulting TREE_PURPOSE will mention the parameter packs in
4061 both the bases and the arguments to the bases. */
4062 tree purpose;
4063 tree value;
4064 tree parameter_packs = NULL_TREE;
4065
4066 /* Determine which parameter packs will be used by the base
4067 class expansion. */
4068 ppd.visited = new hash_set<tree>;
4069 ppd.parameter_packs = &parameter_packs;
4070 ppd.type_pack_expansion_p = false;
4071 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4072 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4073 &ppd, ppd.visited);
4074
4075 if (parameter_packs == NULL_TREE)
4076 {
4077 if (complain & tf_error)
4078 error ("base initializer expansion %qT contains no parameter packs",
4079 arg);
4080 delete ppd.visited;
4081 return error_mark_node;
4082 }
4083
4084 if (TREE_VALUE (arg) != void_type_node)
4085 {
4086 /* Collect the sets of parameter packs used in each of the
4087 initialization arguments. */
4088 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4089 {
4090 /* Determine which parameter packs will be expanded in this
4091 argument. */
4092 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4093 &ppd, ppd.visited);
4094 }
4095 }
4096
4097 delete ppd.visited;
4098
4099 /* Create the pack expansion type for the base type. */
4100 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4101 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4102 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4103 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4104
4105 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4106 they will rarely be compared to anything. */
4107 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4108
4109 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4110 }
4111
4112 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4113 for_types = true;
4114
4115 /* Build the PACK_EXPANSION_* node. */
4116 result = for_types
4117 ? cxx_make_type (TYPE_PACK_EXPANSION)
4118 : make_node (EXPR_PACK_EXPANSION);
4119 SET_PACK_EXPANSION_PATTERN (result, arg);
4120 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4121 {
4122 /* Propagate type and const-expression information. */
4123 TREE_TYPE (result) = TREE_TYPE (arg);
4124 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4125 /* Mark this read now, since the expansion might be length 0. */
4126 mark_exp_read (arg);
4127 }
4128 else
4129 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4130 they will rarely be compared to anything. */
4131 SET_TYPE_STRUCTURAL_EQUALITY (result);
4132
4133 /* Determine which parameter packs will be expanded. */
4134 ppd.parameter_packs = &parameter_packs;
4135 ppd.visited = new hash_set<tree>;
4136 ppd.type_pack_expansion_p = TYPE_P (arg);
4137 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4138 delete ppd.visited;
4139
4140 /* Make sure we found some parameter packs. */
4141 if (parameter_packs == NULL_TREE)
4142 {
4143 if (complain & tf_error)
4144 {
4145 if (TYPE_P (arg))
4146 error ("expansion pattern %qT contains no parameter packs", arg);
4147 else
4148 error ("expansion pattern %qE contains no parameter packs", arg);
4149 }
4150 return error_mark_node;
4151 }
4152 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4153
4154 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4155
4156 return result;
4157 }
4158
4159 /* Checks T for any "bare" parameter packs, which have not yet been
4160 expanded, and issues an error if any are found. This operation can
4161 only be done on full expressions or types (e.g., an expression
4162 statement, "if" condition, etc.), because we could have expressions like:
4163
4164 foo(f(g(h(args)))...)
4165
4166 where "args" is a parameter pack. check_for_bare_parameter_packs
4167 should not be called for the subexpressions args, h(args),
4168 g(h(args)), or f(g(h(args))), because we would produce erroneous
4169 error messages.
4170
4171 Returns TRUE and emits an error if there were bare parameter packs,
4172 returns FALSE otherwise. */
4173 bool
4174 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4175 {
4176 tree parameter_packs = NULL_TREE;
4177 struct find_parameter_pack_data ppd;
4178
4179 if (!processing_template_decl || !t || t == error_mark_node)
4180 return false;
4181
4182 /* A lambda might use a parameter pack from the containing context. */
4183 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4184 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4185 return false;
4186
4187 if (TREE_CODE (t) == TYPE_DECL)
4188 t = TREE_TYPE (t);
4189
4190 ppd.parameter_packs = &parameter_packs;
4191 ppd.visited = new hash_set<tree>;
4192 ppd.type_pack_expansion_p = false;
4193 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4194 delete ppd.visited;
4195
4196 if (parameter_packs)
4197 {
4198 if (loc == UNKNOWN_LOCATION)
4199 loc = cp_expr_loc_or_input_loc (t);
4200 error_at (loc, "parameter packs not expanded with %<...%>:");
4201 while (parameter_packs)
4202 {
4203 tree pack = TREE_VALUE (parameter_packs);
4204 tree name = NULL_TREE;
4205
4206 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4207 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4208 name = TYPE_NAME (pack);
4209 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4210 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4211 else if (TREE_CODE (pack) == CALL_EXPR)
4212 name = DECL_NAME (CALL_EXPR_FN (pack));
4213 else
4214 name = DECL_NAME (pack);
4215
4216 if (name)
4217 inform (loc, " %qD", name);
4218 else
4219 inform (loc, " %s", "<anonymous>");
4220
4221 parameter_packs = TREE_CHAIN (parameter_packs);
4222 }
4223
4224 return true;
4225 }
4226
4227 return false;
4228 }
4229
4230 /* Expand any parameter packs that occur in the template arguments in
4231 ARGS. */
4232 tree
4233 expand_template_argument_pack (tree args)
4234 {
4235 if (args == error_mark_node)
4236 return error_mark_node;
4237
4238 tree result_args = NULL_TREE;
4239 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4240 int num_result_args = -1;
4241 int non_default_args_count = -1;
4242
4243 /* First, determine if we need to expand anything, and the number of
4244 slots we'll need. */
4245 for (in_arg = 0; in_arg < nargs; ++in_arg)
4246 {
4247 tree arg = TREE_VEC_ELT (args, in_arg);
4248 if (arg == NULL_TREE)
4249 return args;
4250 if (ARGUMENT_PACK_P (arg))
4251 {
4252 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4253 if (num_result_args < 0)
4254 num_result_args = in_arg + num_packed;
4255 else
4256 num_result_args += num_packed;
4257 }
4258 else
4259 {
4260 if (num_result_args >= 0)
4261 num_result_args++;
4262 }
4263 }
4264
4265 /* If no expansion is necessary, we're done. */
4266 if (num_result_args < 0)
4267 return args;
4268
4269 /* Expand arguments. */
4270 result_args = make_tree_vec (num_result_args);
4271 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4272 non_default_args_count =
4273 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4274 for (in_arg = 0; in_arg < nargs; ++in_arg)
4275 {
4276 tree arg = TREE_VEC_ELT (args, in_arg);
4277 if (ARGUMENT_PACK_P (arg))
4278 {
4279 tree packed = ARGUMENT_PACK_ARGS (arg);
4280 int i, num_packed = TREE_VEC_LENGTH (packed);
4281 for (i = 0; i < num_packed; ++i, ++out_arg)
4282 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4283 if (non_default_args_count > 0)
4284 non_default_args_count += num_packed - 1;
4285 }
4286 else
4287 {
4288 TREE_VEC_ELT (result_args, out_arg) = arg;
4289 ++out_arg;
4290 }
4291 }
4292 if (non_default_args_count >= 0)
4293 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4294 return result_args;
4295 }
4296
4297 /* Checks if DECL shadows a template parameter.
4298
4299 [temp.local]: A template-parameter shall not be redeclared within its
4300 scope (including nested scopes).
4301
4302 Emits an error and returns TRUE if the DECL shadows a parameter,
4303 returns FALSE otherwise. */
4304
4305 bool
4306 check_template_shadow (tree decl)
4307 {
4308 tree olddecl;
4309
4310 /* If we're not in a template, we can't possibly shadow a template
4311 parameter. */
4312 if (!current_template_parms)
4313 return true;
4314
4315 /* Figure out what we're shadowing. */
4316 decl = OVL_FIRST (decl);
4317 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4318
4319 /* If there's no previous binding for this name, we're not shadowing
4320 anything, let alone a template parameter. */
4321 if (!olddecl)
4322 return true;
4323
4324 /* If we're not shadowing a template parameter, we're done. Note
4325 that OLDDECL might be an OVERLOAD (or perhaps even an
4326 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4327 node. */
4328 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4329 return true;
4330
4331 /* We check for decl != olddecl to avoid bogus errors for using a
4332 name inside a class. We check TPFI to avoid duplicate errors for
4333 inline member templates. */
4334 if (decl == olddecl
4335 || (DECL_TEMPLATE_PARM_P (decl)
4336 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4337 return true;
4338
4339 /* Don't complain about the injected class name, as we've already
4340 complained about the class itself. */
4341 if (DECL_SELF_REFERENCE_P (decl))
4342 return false;
4343
4344 if (DECL_TEMPLATE_PARM_P (decl))
4345 error ("declaration of template parameter %q+D shadows "
4346 "template parameter", decl);
4347 else
4348 error ("declaration of %q+#D shadows template parameter", decl);
4349 inform (DECL_SOURCE_LOCATION (olddecl),
4350 "template parameter %qD declared here", olddecl);
4351 return false;
4352 }
4353
4354 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4355 ORIG_LEVEL, DECL, and TYPE. */
4356
4357 static tree
4358 build_template_parm_index (int index,
4359 int level,
4360 int orig_level,
4361 tree decl,
4362 tree type)
4363 {
4364 tree t = make_node (TEMPLATE_PARM_INDEX);
4365 TEMPLATE_PARM_IDX (t) = index;
4366 TEMPLATE_PARM_LEVEL (t) = level;
4367 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4368 TEMPLATE_PARM_DECL (t) = decl;
4369 TREE_TYPE (t) = type;
4370 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4371 TREE_READONLY (t) = TREE_READONLY (decl);
4372
4373 return t;
4374 }
4375
4376 /* Find the canonical type parameter for the given template type
4377 parameter. Returns the canonical type parameter, which may be TYPE
4378 if no such parameter existed. */
4379
4380 static tree
4381 canonical_type_parameter (tree type)
4382 {
4383 tree list;
4384 int idx = TEMPLATE_TYPE_IDX (type);
4385 if (!canonical_template_parms)
4386 vec_alloc (canonical_template_parms, idx + 1);
4387
4388 if (canonical_template_parms->length () <= (unsigned) idx)
4389 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4390
4391 list = (*canonical_template_parms)[idx];
4392 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4393 list = TREE_CHAIN (list);
4394
4395 if (list)
4396 return TREE_VALUE (list);
4397 else
4398 {
4399 (*canonical_template_parms)[idx]
4400 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4401 return type;
4402 }
4403 }
4404
4405 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4406 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4407 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4408 new one is created. */
4409
4410 static tree
4411 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4412 tsubst_flags_t complain)
4413 {
4414 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4415 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4416 != TEMPLATE_PARM_LEVEL (index) - levels)
4417 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4418 {
4419 tree orig_decl = TEMPLATE_PARM_DECL (index);
4420
4421 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4422 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4423 type);
4424 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4425 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4426 DECL_ARTIFICIAL (decl) = 1;
4427 SET_DECL_TEMPLATE_PARM_P (decl);
4428
4429 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4430 TEMPLATE_PARM_LEVEL (index) - levels,
4431 TEMPLATE_PARM_ORIG_LEVEL (index),
4432 decl, type);
4433 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4434 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4435 = TEMPLATE_PARM_PARAMETER_PACK (index);
4436
4437 /* Template template parameters need this. */
4438 tree inner = decl;
4439 if (TREE_CODE (decl) == TEMPLATE_DECL)
4440 {
4441 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4442 TYPE_DECL, DECL_NAME (decl), type);
4443 DECL_TEMPLATE_RESULT (decl) = inner;
4444 DECL_ARTIFICIAL (inner) = true;
4445 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4446 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4447 }
4448
4449 /* Attach the TPI to the decl. */
4450 if (TREE_CODE (inner) == TYPE_DECL)
4451 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4452 else
4453 DECL_INITIAL (decl) = tpi;
4454 }
4455
4456 return TEMPLATE_PARM_DESCENDANTS (index);
4457 }
4458
4459 /* Process information from new template parameter PARM and append it
4460 to the LIST being built. This new parameter is a non-type
4461 parameter iff IS_NON_TYPE is true. This new parameter is a
4462 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4463 is in PARM_LOC. */
4464
4465 tree
4466 process_template_parm (tree list, location_t parm_loc, tree parm,
4467 bool is_non_type, bool is_parameter_pack)
4468 {
4469 tree decl = 0;
4470 int idx = 0;
4471
4472 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4473 tree defval = TREE_PURPOSE (parm);
4474 tree constr = TREE_TYPE (parm);
4475
4476 if (list)
4477 {
4478 tree p = tree_last (list);
4479
4480 if (p && TREE_VALUE (p) != error_mark_node)
4481 {
4482 p = TREE_VALUE (p);
4483 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4484 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4485 else
4486 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4487 }
4488
4489 ++idx;
4490 }
4491
4492 if (is_non_type)
4493 {
4494 parm = TREE_VALUE (parm);
4495
4496 SET_DECL_TEMPLATE_PARM_P (parm);
4497
4498 if (TREE_TYPE (parm) != error_mark_node)
4499 {
4500 /* [temp.param]
4501
4502 The top-level cv-qualifiers on the template-parameter are
4503 ignored when determining its type. */
4504 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4505 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4506 TREE_TYPE (parm) = error_mark_node;
4507 else if (uses_parameter_packs (TREE_TYPE (parm))
4508 && !is_parameter_pack
4509 /* If we're in a nested template parameter list, the template
4510 template parameter could be a parameter pack. */
4511 && processing_template_parmlist == 1)
4512 {
4513 /* This template parameter is not a parameter pack, but it
4514 should be. Complain about "bare" parameter packs. */
4515 check_for_bare_parameter_packs (TREE_TYPE (parm));
4516
4517 /* Recover by calling this a parameter pack. */
4518 is_parameter_pack = true;
4519 }
4520 }
4521
4522 /* A template parameter is not modifiable. */
4523 TREE_CONSTANT (parm) = 1;
4524 TREE_READONLY (parm) = 1;
4525 decl = build_decl (parm_loc,
4526 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4527 TREE_CONSTANT (decl) = 1;
4528 TREE_READONLY (decl) = 1;
4529 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4530 = build_template_parm_index (idx, processing_template_decl,
4531 processing_template_decl,
4532 decl, TREE_TYPE (parm));
4533
4534 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4535 = is_parameter_pack;
4536 }
4537 else
4538 {
4539 tree t;
4540 parm = TREE_VALUE (TREE_VALUE (parm));
4541
4542 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4543 {
4544 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4545 /* This is for distinguishing between real templates and template
4546 template parameters */
4547 TREE_TYPE (parm) = t;
4548 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4549 decl = parm;
4550 }
4551 else
4552 {
4553 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4554 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4555 decl = build_decl (parm_loc,
4556 TYPE_DECL, parm, t);
4557 }
4558
4559 TYPE_NAME (t) = decl;
4560 TYPE_STUB_DECL (t) = decl;
4561 parm = decl;
4562 TEMPLATE_TYPE_PARM_INDEX (t)
4563 = build_template_parm_index (idx, processing_template_decl,
4564 processing_template_decl,
4565 decl, TREE_TYPE (parm));
4566 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4567 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4568 }
4569 DECL_ARTIFICIAL (decl) = 1;
4570 SET_DECL_TEMPLATE_PARM_P (decl);
4571
4572 /* Build requirements for the type/template parameter.
4573 This must be done after SET_DECL_TEMPLATE_PARM_P or
4574 process_template_parm could fail. */
4575 tree reqs = finish_shorthand_constraint (parm, constr);
4576
4577 decl = pushdecl (decl);
4578 if (!is_non_type)
4579 parm = decl;
4580
4581 /* Build the parameter node linking the parameter declaration,
4582 its default argument (if any), and its constraints (if any). */
4583 parm = build_tree_list (defval, parm);
4584 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4585
4586 return chainon (list, parm);
4587 }
4588
4589 /* The end of a template parameter list has been reached. Process the
4590 tree list into a parameter vector, converting each parameter into a more
4591 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4592 as PARM_DECLs. */
4593
4594 tree
4595 end_template_parm_list (tree parms)
4596 {
4597 int nparms;
4598 tree parm, next;
4599 tree saved_parmlist = make_tree_vec (list_length (parms));
4600
4601 /* Pop the dummy parameter level and add the real one. */
4602 current_template_parms = TREE_CHAIN (current_template_parms);
4603
4604 current_template_parms
4605 = tree_cons (size_int (processing_template_decl),
4606 saved_parmlist, current_template_parms);
4607
4608 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4609 {
4610 next = TREE_CHAIN (parm);
4611 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4612 TREE_CHAIN (parm) = NULL_TREE;
4613 }
4614
4615 --processing_template_parmlist;
4616
4617 return saved_parmlist;
4618 }
4619
4620 // Explicitly indicate the end of the template parameter list. We assume
4621 // that the current template parameters have been constructed and/or
4622 // managed explicitly, as when creating new template template parameters
4623 // from a shorthand constraint.
4624 void
4625 end_template_parm_list ()
4626 {
4627 --processing_template_parmlist;
4628 }
4629
4630 /* end_template_decl is called after a template declaration is seen. */
4631
4632 void
4633 end_template_decl (void)
4634 {
4635 reset_specialization ();
4636
4637 if (! processing_template_decl)
4638 return;
4639
4640 /* This matches the pushlevel in begin_template_parm_list. */
4641 finish_scope ();
4642
4643 --processing_template_decl;
4644 current_template_parms = TREE_CHAIN (current_template_parms);
4645 }
4646
4647 /* Takes a TREE_LIST representing a template parameter and convert it
4648 into an argument suitable to be passed to the type substitution
4649 functions. Note that If the TREE_LIST contains an error_mark
4650 node, the returned argument is error_mark_node. */
4651
4652 tree
4653 template_parm_to_arg (tree t)
4654 {
4655
4656 if (t == NULL_TREE
4657 || TREE_CODE (t) != TREE_LIST)
4658 return t;
4659
4660 if (error_operand_p (TREE_VALUE (t)))
4661 return error_mark_node;
4662
4663 t = TREE_VALUE (t);
4664
4665 if (TREE_CODE (t) == TYPE_DECL
4666 || TREE_CODE (t) == TEMPLATE_DECL)
4667 {
4668 t = TREE_TYPE (t);
4669
4670 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4671 {
4672 /* Turn this argument into a TYPE_ARGUMENT_PACK
4673 with a single element, which expands T. */
4674 tree vec = make_tree_vec (1);
4675 if (CHECKING_P)
4676 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4677
4678 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4679
4680 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4681 SET_ARGUMENT_PACK_ARGS (t, vec);
4682 }
4683 }
4684 else
4685 {
4686 t = DECL_INITIAL (t);
4687
4688 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4689 {
4690 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4691 with a single element, which expands T. */
4692 tree vec = make_tree_vec (1);
4693 if (CHECKING_P)
4694 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4695
4696 t = convert_from_reference (t);
4697 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4698
4699 t = make_node (NONTYPE_ARGUMENT_PACK);
4700 SET_ARGUMENT_PACK_ARGS (t, vec);
4701 }
4702 else
4703 t = convert_from_reference (t);
4704 }
4705 return t;
4706 }
4707
4708 /* Given a single level of template parameters (a TREE_VEC), return it
4709 as a set of template arguments. */
4710
4711 tree
4712 template_parms_level_to_args (tree parms)
4713 {
4714 tree a = copy_node (parms);
4715 TREE_TYPE (a) = NULL_TREE;
4716 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4717 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4718
4719 if (CHECKING_P)
4720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4721
4722 return a;
4723 }
4724
4725 /* Given a set of template parameters, return them as a set of template
4726 arguments. The template parameters are represented as a TREE_VEC, in
4727 the form documented in cp-tree.h for template arguments. */
4728
4729 tree
4730 template_parms_to_args (tree parms)
4731 {
4732 tree header;
4733 tree args = NULL_TREE;
4734 int length = TMPL_PARMS_DEPTH (parms);
4735 int l = length;
4736
4737 /* If there is only one level of template parameters, we do not
4738 create a TREE_VEC of TREE_VECs. Instead, we return a single
4739 TREE_VEC containing the arguments. */
4740 if (length > 1)
4741 args = make_tree_vec (length);
4742
4743 for (header = parms; header; header = TREE_CHAIN (header))
4744 {
4745 tree a = template_parms_level_to_args (TREE_VALUE (header));
4746
4747 if (length > 1)
4748 TREE_VEC_ELT (args, --l) = a;
4749 else
4750 args = a;
4751 }
4752
4753 return args;
4754 }
4755
4756 /* Within the declaration of a template, return the currently active
4757 template parameters as an argument TREE_VEC. */
4758
4759 static tree
4760 current_template_args (void)
4761 {
4762 return template_parms_to_args (current_template_parms);
4763 }
4764
4765 /* Return the fully generic arguments for of TMPL, i.e. what
4766 current_template_args would be while parsing it. */
4767
4768 tree
4769 generic_targs_for (tree tmpl)
4770 {
4771 if (tmpl == NULL_TREE)
4772 return NULL_TREE;
4773 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4774 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4775 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4776 template parameter, it has no TEMPLATE_INFO; for a partial
4777 specialization, it has the arguments for the primary template, and we
4778 want the arguments for the partial specialization. */;
4779 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4780 if (tree ti = get_template_info (result))
4781 return TI_ARGS (ti);
4782 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4783 }
4784
4785 /* Update the declared TYPE by doing any lookups which were thought to be
4786 dependent, but are not now that we know the SCOPE of the declarator. */
4787
4788 tree
4789 maybe_update_decl_type (tree orig_type, tree scope)
4790 {
4791 tree type = orig_type;
4792
4793 if (type == NULL_TREE)
4794 return type;
4795
4796 if (TREE_CODE (orig_type) == TYPE_DECL)
4797 type = TREE_TYPE (type);
4798
4799 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4800 && dependent_type_p (type)
4801 /* Don't bother building up the args in this case. */
4802 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4803 {
4804 /* tsubst in the args corresponding to the template parameters,
4805 including auto if present. Most things will be unchanged, but
4806 make_typename_type and tsubst_qualified_id will resolve
4807 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4808 tree args = current_template_args ();
4809 tree auto_node = type_uses_auto (type);
4810 tree pushed;
4811 if (auto_node)
4812 {
4813 tree auto_vec = make_tree_vec (1);
4814 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4815 args = add_to_template_args (args, auto_vec);
4816 }
4817 pushed = push_scope (scope);
4818 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4819 if (pushed)
4820 pop_scope (scope);
4821 }
4822
4823 if (type == error_mark_node)
4824 return orig_type;
4825
4826 if (TREE_CODE (orig_type) == TYPE_DECL)
4827 {
4828 if (same_type_p (type, TREE_TYPE (orig_type)))
4829 type = orig_type;
4830 else
4831 type = TYPE_NAME (type);
4832 }
4833 return type;
4834 }
4835
4836 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4837 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4838 the new template is a member template. */
4839
4840 static tree
4841 build_template_decl (tree decl, tree parms, bool member_template_p)
4842 {
4843 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4844 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4845 DECL_TEMPLATE_PARMS (tmpl) = parms;
4846 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4847 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4848 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4849
4850 return tmpl;
4851 }
4852
4853 struct template_parm_data
4854 {
4855 /* The level of the template parameters we are currently
4856 processing. */
4857 int level;
4858
4859 /* The index of the specialization argument we are currently
4860 processing. */
4861 int current_arg;
4862
4863 /* An array whose size is the number of template parameters. The
4864 elements are nonzero if the parameter has been used in any one
4865 of the arguments processed so far. */
4866 int* parms;
4867
4868 /* An array whose size is the number of template arguments. The
4869 elements are nonzero if the argument makes use of template
4870 parameters of this level. */
4871 int* arg_uses_template_parms;
4872 };
4873
4874 /* Subroutine of push_template_decl used to see if each template
4875 parameter in a partial specialization is used in the explicit
4876 argument list. If T is of the LEVEL given in DATA (which is
4877 treated as a template_parm_data*), then DATA->PARMS is marked
4878 appropriately. */
4879
4880 static int
4881 mark_template_parm (tree t, void* data)
4882 {
4883 int level;
4884 int idx;
4885 struct template_parm_data* tpd = (struct template_parm_data*) data;
4886
4887 template_parm_level_and_index (t, &level, &idx);
4888
4889 if (level == tpd->level)
4890 {
4891 tpd->parms[idx] = 1;
4892 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4893 }
4894
4895 /* In C++17 the type of a non-type argument is a deduced context. */
4896 if (cxx_dialect >= cxx17
4897 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4898 for_each_template_parm (TREE_TYPE (t),
4899 &mark_template_parm,
4900 data,
4901 NULL,
4902 /*include_nondeduced_p=*/false);
4903
4904 /* Return zero so that for_each_template_parm will continue the
4905 traversal of the tree; we want to mark *every* template parm. */
4906 return 0;
4907 }
4908
4909 /* Process the partial specialization DECL. */
4910
4911 static tree
4912 process_partial_specialization (tree decl)
4913 {
4914 tree type = TREE_TYPE (decl);
4915 tree tinfo = get_template_info (decl);
4916 tree maintmpl = TI_TEMPLATE (tinfo);
4917 tree specargs = TI_ARGS (tinfo);
4918 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4919 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4920 tree inner_parms;
4921 tree inst;
4922 int nargs = TREE_VEC_LENGTH (inner_args);
4923 int ntparms;
4924 int i;
4925 bool did_error_intro = false;
4926 struct template_parm_data tpd;
4927 struct template_parm_data tpd2;
4928
4929 gcc_assert (current_template_parms);
4930
4931 /* A concept cannot be specialized. */
4932 if (flag_concepts && variable_concept_p (maintmpl))
4933 {
4934 error ("specialization of variable concept %q#D", maintmpl);
4935 return error_mark_node;
4936 }
4937
4938 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4939 ntparms = TREE_VEC_LENGTH (inner_parms);
4940
4941 /* We check that each of the template parameters given in the
4942 partial specialization is used in the argument list to the
4943 specialization. For example:
4944
4945 template <class T> struct S;
4946 template <class T> struct S<T*>;
4947
4948 The second declaration is OK because `T*' uses the template
4949 parameter T, whereas
4950
4951 template <class T> struct S<int>;
4952
4953 is no good. Even trickier is:
4954
4955 template <class T>
4956 struct S1
4957 {
4958 template <class U>
4959 struct S2;
4960 template <class U>
4961 struct S2<T>;
4962 };
4963
4964 The S2<T> declaration is actually invalid; it is a
4965 full-specialization. Of course,
4966
4967 template <class U>
4968 struct S2<T (*)(U)>;
4969
4970 or some such would have been OK. */
4971 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4972 tpd.parms = XALLOCAVEC (int, ntparms);
4973 memset (tpd.parms, 0, sizeof (int) * ntparms);
4974
4975 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4976 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4977 for (i = 0; i < nargs; ++i)
4978 {
4979 tpd.current_arg = i;
4980 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4981 &mark_template_parm,
4982 &tpd,
4983 NULL,
4984 /*include_nondeduced_p=*/false);
4985 }
4986 for (i = 0; i < ntparms; ++i)
4987 if (tpd.parms[i] == 0)
4988 {
4989 /* One of the template parms was not used in a deduced context in the
4990 specialization. */
4991 if (!did_error_intro)
4992 {
4993 error ("template parameters not deducible in "
4994 "partial specialization:");
4995 did_error_intro = true;
4996 }
4997
4998 inform (input_location, " %qD",
4999 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5000 }
5001
5002 if (did_error_intro)
5003 return error_mark_node;
5004
5005 /* [temp.class.spec]
5006
5007 The argument list of the specialization shall not be identical to
5008 the implicit argument list of the primary template. */
5009 tree main_args
5010 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5011 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5012 && (!flag_concepts
5013 || !strictly_subsumes (current_template_constraints (),
5014 inner_args, maintmpl)))
5015 {
5016 if (!flag_concepts)
5017 error ("partial specialization %q+D does not specialize "
5018 "any template arguments; to define the primary template, "
5019 "remove the template argument list", decl);
5020 else
5021 error ("partial specialization %q+D does not specialize any "
5022 "template arguments and is not more constrained than "
5023 "the primary template; to define the primary template, "
5024 "remove the template argument list", decl);
5025 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5026 }
5027
5028 /* A partial specialization that replaces multiple parameters of the
5029 primary template with a pack expansion is less specialized for those
5030 parameters. */
5031 if (nargs < DECL_NTPARMS (maintmpl))
5032 {
5033 error ("partial specialization is not more specialized than the "
5034 "primary template because it replaces multiple parameters "
5035 "with a pack expansion");
5036 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5037 /* Avoid crash in process_partial_specialization. */
5038 return decl;
5039 }
5040
5041 else if (nargs > DECL_NTPARMS (maintmpl))
5042 {
5043 error ("too many arguments for partial specialization %qT", type);
5044 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5045 /* Avoid crash below. */
5046 return decl;
5047 }
5048
5049 /* If we aren't in a dependent class, we can actually try deduction. */
5050 else if (tpd.level == 1
5051 /* FIXME we should be able to handle a partial specialization of a
5052 partial instantiation, but currently we can't (c++/41727). */
5053 && TMPL_ARGS_DEPTH (specargs) == 1
5054 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5055 {
5056 auto_diagnostic_group d;
5057 if (permerror (input_location, "partial specialization %qD is not "
5058 "more specialized than", decl))
5059 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5060 maintmpl);
5061 }
5062
5063 /* [temp.class.spec]
5064
5065 A partially specialized non-type argument expression shall not
5066 involve template parameters of the partial specialization except
5067 when the argument expression is a simple identifier.
5068
5069 The type of a template parameter corresponding to a specialized
5070 non-type argument shall not be dependent on a parameter of the
5071 specialization.
5072
5073 Also, we verify that pack expansions only occur at the
5074 end of the argument list. */
5075 tpd2.parms = 0;
5076 for (i = 0; i < nargs; ++i)
5077 {
5078 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5079 tree arg = TREE_VEC_ELT (inner_args, i);
5080 tree packed_args = NULL_TREE;
5081 int j, len = 1;
5082
5083 if (ARGUMENT_PACK_P (arg))
5084 {
5085 /* Extract the arguments from the argument pack. We'll be
5086 iterating over these in the following loop. */
5087 packed_args = ARGUMENT_PACK_ARGS (arg);
5088 len = TREE_VEC_LENGTH (packed_args);
5089 }
5090
5091 for (j = 0; j < len; j++)
5092 {
5093 if (packed_args)
5094 /* Get the Jth argument in the parameter pack. */
5095 arg = TREE_VEC_ELT (packed_args, j);
5096
5097 if (PACK_EXPANSION_P (arg))
5098 {
5099 /* Pack expansions must come at the end of the
5100 argument list. */
5101 if ((packed_args && j < len - 1)
5102 || (!packed_args && i < nargs - 1))
5103 {
5104 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5105 error ("parameter pack argument %qE must be at the "
5106 "end of the template argument list", arg);
5107 else
5108 error ("parameter pack argument %qT must be at the "
5109 "end of the template argument list", arg);
5110 }
5111 }
5112
5113 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5114 /* We only care about the pattern. */
5115 arg = PACK_EXPANSION_PATTERN (arg);
5116
5117 if (/* These first two lines are the `non-type' bit. */
5118 !TYPE_P (arg)
5119 && TREE_CODE (arg) != TEMPLATE_DECL
5120 /* This next two lines are the `argument expression is not just a
5121 simple identifier' condition and also the `specialized
5122 non-type argument' bit. */
5123 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5124 && !((REFERENCE_REF_P (arg)
5125 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5126 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5127 {
5128 if ((!packed_args && tpd.arg_uses_template_parms[i])
5129 || (packed_args && uses_template_parms (arg)))
5130 error_at (cp_expr_loc_or_input_loc (arg),
5131 "template argument %qE involves template "
5132 "parameter(s)", arg);
5133 else
5134 {
5135 /* Look at the corresponding template parameter,
5136 marking which template parameters its type depends
5137 upon. */
5138 tree type = TREE_TYPE (parm);
5139
5140 if (!tpd2.parms)
5141 {
5142 /* We haven't yet initialized TPD2. Do so now. */
5143 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5144 /* The number of parameters here is the number in the
5145 main template, which, as checked in the assertion
5146 above, is NARGS. */
5147 tpd2.parms = XALLOCAVEC (int, nargs);
5148 tpd2.level =
5149 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5150 }
5151
5152 /* Mark the template parameters. But this time, we're
5153 looking for the template parameters of the main
5154 template, not in the specialization. */
5155 tpd2.current_arg = i;
5156 tpd2.arg_uses_template_parms[i] = 0;
5157 memset (tpd2.parms, 0, sizeof (int) * nargs);
5158 for_each_template_parm (type,
5159 &mark_template_parm,
5160 &tpd2,
5161 NULL,
5162 /*include_nondeduced_p=*/false);
5163
5164 if (tpd2.arg_uses_template_parms [i])
5165 {
5166 /* The type depended on some template parameters.
5167 If they are fully specialized in the
5168 specialization, that's OK. */
5169 int j;
5170 int count = 0;
5171 for (j = 0; j < nargs; ++j)
5172 if (tpd2.parms[j] != 0
5173 && tpd.arg_uses_template_parms [j])
5174 ++count;
5175 if (count != 0)
5176 error_n (input_location, count,
5177 "type %qT of template argument %qE depends "
5178 "on a template parameter",
5179 "type %qT of template argument %qE depends "
5180 "on template parameters",
5181 type,
5182 arg);
5183 }
5184 }
5185 }
5186 }
5187 }
5188
5189 /* We should only get here once. */
5190 if (TREE_CODE (decl) == TYPE_DECL)
5191 gcc_assert (!COMPLETE_TYPE_P (type));
5192
5193 // Build the template decl.
5194 tree tmpl = build_template_decl (decl, current_template_parms,
5195 DECL_MEMBER_TEMPLATE_P (maintmpl));
5196 TREE_TYPE (tmpl) = type;
5197 DECL_TEMPLATE_RESULT (tmpl) = decl;
5198 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5199 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5200 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5201
5202 /* Give template template parms a DECL_CONTEXT of the template
5203 for which they are a parameter. */
5204 for (i = 0; i < ntparms; ++i)
5205 {
5206 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5207 if (TREE_CODE (parm) == TEMPLATE_DECL)
5208 DECL_CONTEXT (parm) = tmpl;
5209 }
5210
5211 if (VAR_P (decl))
5212 /* We didn't register this in check_explicit_specialization so we could
5213 wait until the constraints were set. */
5214 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5215 else
5216 associate_classtype_constraints (type);
5217
5218 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5219 = tree_cons (specargs, tmpl,
5220 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5221 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5222
5223 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5224 inst = TREE_CHAIN (inst))
5225 {
5226 tree instance = TREE_VALUE (inst);
5227 if (TYPE_P (instance)
5228 ? (COMPLETE_TYPE_P (instance)
5229 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5230 : DECL_TEMPLATE_INSTANTIATION (instance))
5231 {
5232 tree spec = most_specialized_partial_spec (instance, tf_none);
5233 tree inst_decl = (DECL_P (instance)
5234 ? instance : TYPE_NAME (instance));
5235 if (!spec)
5236 /* OK */;
5237 else if (spec == error_mark_node)
5238 permerror (input_location,
5239 "declaration of %qD ambiguates earlier template "
5240 "instantiation for %qD", decl, inst_decl);
5241 else if (TREE_VALUE (spec) == tmpl)
5242 permerror (input_location,
5243 "partial specialization of %qD after instantiation "
5244 "of %qD", decl, inst_decl);
5245 }
5246 }
5247
5248 return decl;
5249 }
5250
5251 /* PARM is a template parameter of some form; return the corresponding
5252 TEMPLATE_PARM_INDEX. */
5253
5254 static tree
5255 get_template_parm_index (tree parm)
5256 {
5257 if (TREE_CODE (parm) == PARM_DECL
5258 || TREE_CODE (parm) == CONST_DECL)
5259 parm = DECL_INITIAL (parm);
5260 else if (TREE_CODE (parm) == TYPE_DECL
5261 || TREE_CODE (parm) == TEMPLATE_DECL)
5262 parm = TREE_TYPE (parm);
5263 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5264 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5265 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5266 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5267 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5268 return parm;
5269 }
5270
5271 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5272 parameter packs used by the template parameter PARM. */
5273
5274 static void
5275 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5276 {
5277 /* A type parm can't refer to another parm. */
5278 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5279 return;
5280 else if (TREE_CODE (parm) == PARM_DECL)
5281 {
5282 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5283 ppd, ppd->visited);
5284 return;
5285 }
5286
5287 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5288
5289 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5290 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5291 {
5292 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5293 if (template_parameter_pack_p (p))
5294 /* Any packs in the type are expanded by this parameter. */;
5295 else
5296 fixed_parameter_pack_p_1 (p, ppd);
5297 }
5298 }
5299
5300 /* PARM is a template parameter pack. Return any parameter packs used in
5301 its type or the type of any of its template parameters. If there are
5302 any such packs, it will be instantiated into a fixed template parameter
5303 list by partial instantiation rather than be fully deduced. */
5304
5305 tree
5306 fixed_parameter_pack_p (tree parm)
5307 {
5308 /* This can only be true in a member template. */
5309 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5310 return NULL_TREE;
5311 /* This can only be true for a parameter pack. */
5312 if (!template_parameter_pack_p (parm))
5313 return NULL_TREE;
5314 /* A type parm can't refer to another parm. */
5315 if (TREE_CODE (parm) == TYPE_DECL)
5316 return NULL_TREE;
5317
5318 tree parameter_packs = NULL_TREE;
5319 struct find_parameter_pack_data ppd;
5320 ppd.parameter_packs = &parameter_packs;
5321 ppd.visited = new hash_set<tree>;
5322 ppd.type_pack_expansion_p = false;
5323
5324 fixed_parameter_pack_p_1 (parm, &ppd);
5325
5326 delete ppd.visited;
5327 return parameter_packs;
5328 }
5329
5330 /* Check that a template declaration's use of default arguments and
5331 parameter packs is not invalid. Here, PARMS are the template
5332 parameters. IS_PRIMARY is true if DECL is the thing declared by
5333 a primary template. IS_PARTIAL is true if DECL is a partial
5334 specialization.
5335
5336 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5337 function template declaration or a friend class template
5338 declaration. In the function case, 1 indicates a declaration, 2
5339 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5340 emitted for extraneous default arguments.
5341
5342 Returns TRUE if there were no errors found, FALSE otherwise. */
5343
5344 bool
5345 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5346 bool is_partial, int is_friend_decl)
5347 {
5348 const char *msg;
5349 int last_level_to_check;
5350 tree parm_level;
5351 bool no_errors = true;
5352
5353 /* [temp.param]
5354
5355 A default template-argument shall not be specified in a
5356 function template declaration or a function template definition, nor
5357 in the template-parameter-list of the definition of a member of a
5358 class template. */
5359
5360 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5361 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5362 /* You can't have a function template declaration in a local
5363 scope, nor you can you define a member of a class template in a
5364 local scope. */
5365 return true;
5366
5367 if ((TREE_CODE (decl) == TYPE_DECL
5368 && TREE_TYPE (decl)
5369 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5370 || (TREE_CODE (decl) == FUNCTION_DECL
5371 && LAMBDA_FUNCTION_P (decl)))
5372 /* A lambda doesn't have an explicit declaration; don't complain
5373 about the parms of the enclosing class. */
5374 return true;
5375
5376 if (current_class_type
5377 && !TYPE_BEING_DEFINED (current_class_type)
5378 && DECL_LANG_SPECIFIC (decl)
5379 && DECL_DECLARES_FUNCTION_P (decl)
5380 /* If this is either a friend defined in the scope of the class
5381 or a member function. */
5382 && (DECL_FUNCTION_MEMBER_P (decl)
5383 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5384 : DECL_FRIEND_CONTEXT (decl)
5385 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5386 : false)
5387 /* And, if it was a member function, it really was defined in
5388 the scope of the class. */
5389 && (!DECL_FUNCTION_MEMBER_P (decl)
5390 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5391 /* We already checked these parameters when the template was
5392 declared, so there's no need to do it again now. This function
5393 was defined in class scope, but we're processing its body now
5394 that the class is complete. */
5395 return true;
5396
5397 /* Core issue 226 (C++0x only): the following only applies to class
5398 templates. */
5399 if (is_primary
5400 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5401 {
5402 /* [temp.param]
5403
5404 If a template-parameter has a default template-argument, all
5405 subsequent template-parameters shall have a default
5406 template-argument supplied. */
5407 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5408 {
5409 tree inner_parms = TREE_VALUE (parm_level);
5410 int ntparms = TREE_VEC_LENGTH (inner_parms);
5411 int seen_def_arg_p = 0;
5412 int i;
5413
5414 for (i = 0; i < ntparms; ++i)
5415 {
5416 tree parm = TREE_VEC_ELT (inner_parms, i);
5417
5418 if (parm == error_mark_node)
5419 continue;
5420
5421 if (TREE_PURPOSE (parm))
5422 seen_def_arg_p = 1;
5423 else if (seen_def_arg_p
5424 && !template_parameter_pack_p (TREE_VALUE (parm)))
5425 {
5426 error ("no default argument for %qD", TREE_VALUE (parm));
5427 /* For better subsequent error-recovery, we indicate that
5428 there should have been a default argument. */
5429 TREE_PURPOSE (parm) = error_mark_node;
5430 no_errors = false;
5431 }
5432 else if (!is_partial
5433 && !is_friend_decl
5434 /* Don't complain about an enclosing partial
5435 specialization. */
5436 && parm_level == parms
5437 && TREE_CODE (decl) == TYPE_DECL
5438 && i < ntparms - 1
5439 && template_parameter_pack_p (TREE_VALUE (parm))
5440 /* A fixed parameter pack will be partially
5441 instantiated into a fixed length list. */
5442 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5443 {
5444 /* A primary class template can only have one
5445 parameter pack, at the end of the template
5446 parameter list. */
5447
5448 error ("parameter pack %q+D must be at the end of the"
5449 " template parameter list", TREE_VALUE (parm));
5450
5451 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5452 = error_mark_node;
5453 no_errors = false;
5454 }
5455 }
5456 }
5457 }
5458
5459 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5460 || is_partial
5461 || !is_primary
5462 || is_friend_decl)
5463 /* For an ordinary class template, default template arguments are
5464 allowed at the innermost level, e.g.:
5465 template <class T = int>
5466 struct S {};
5467 but, in a partial specialization, they're not allowed even
5468 there, as we have in [temp.class.spec]:
5469
5470 The template parameter list of a specialization shall not
5471 contain default template argument values.
5472
5473 So, for a partial specialization, or for a function template
5474 (in C++98/C++03), we look at all of them. */
5475 ;
5476 else
5477 /* But, for a primary class template that is not a partial
5478 specialization we look at all template parameters except the
5479 innermost ones. */
5480 parms = TREE_CHAIN (parms);
5481
5482 /* Figure out what error message to issue. */
5483 if (is_friend_decl == 2)
5484 msg = G_("default template arguments may not be used in function template "
5485 "friend re-declaration");
5486 else if (is_friend_decl)
5487 msg = G_("default template arguments may not be used in template "
5488 "friend declarations");
5489 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5490 msg = G_("default template arguments may not be used in function templates "
5491 "without %<-std=c++11%> or %<-std=gnu++11%>");
5492 else if (is_partial)
5493 msg = G_("default template arguments may not be used in "
5494 "partial specializations");
5495 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5496 msg = G_("default argument for template parameter for class enclosing %qD");
5497 else
5498 /* Per [temp.param]/9, "A default template-argument shall not be
5499 specified in the template-parameter-lists of the definition of
5500 a member of a class template that appears outside of the member's
5501 class.", thus if we aren't handling a member of a class template
5502 there is no need to examine the parameters. */
5503 return true;
5504
5505 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5506 /* If we're inside a class definition, there's no need to
5507 examine the parameters to the class itself. On the one
5508 hand, they will be checked when the class is defined, and,
5509 on the other, default arguments are valid in things like:
5510 template <class T = double>
5511 struct S { template <class U> void f(U); };
5512 Here the default argument for `S' has no bearing on the
5513 declaration of `f'. */
5514 last_level_to_check = template_class_depth (current_class_type) + 1;
5515 else
5516 /* Check everything. */
5517 last_level_to_check = 0;
5518
5519 for (parm_level = parms;
5520 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5521 parm_level = TREE_CHAIN (parm_level))
5522 {
5523 tree inner_parms = TREE_VALUE (parm_level);
5524 int i;
5525 int ntparms;
5526
5527 ntparms = TREE_VEC_LENGTH (inner_parms);
5528 for (i = 0; i < ntparms; ++i)
5529 {
5530 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5531 continue;
5532
5533 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5534 {
5535 if (msg)
5536 {
5537 no_errors = false;
5538 if (is_friend_decl == 2)
5539 return no_errors;
5540
5541 error (msg, decl);
5542 msg = 0;
5543 }
5544
5545 /* Clear out the default argument so that we are not
5546 confused later. */
5547 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5548 }
5549 }
5550
5551 /* At this point, if we're still interested in issuing messages,
5552 they must apply to classes surrounding the object declared. */
5553 if (msg)
5554 msg = G_("default argument for template parameter for class "
5555 "enclosing %qD");
5556 }
5557
5558 return no_errors;
5559 }
5560
5561 /* Worker for push_template_decl_real, called via
5562 for_each_template_parm. DATA is really an int, indicating the
5563 level of the parameters we are interested in. If T is a template
5564 parameter of that level, return nonzero. */
5565
5566 static int
5567 template_parm_this_level_p (tree t, void* data)
5568 {
5569 int this_level = *(int *)data;
5570 int level;
5571
5572 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5573 level = TEMPLATE_PARM_LEVEL (t);
5574 else
5575 level = TEMPLATE_TYPE_LEVEL (t);
5576 return level == this_level;
5577 }
5578
5579 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5580 DATA is really an int, indicating the innermost outer level of parameters.
5581 If T is a template parameter of that level or further out, return
5582 nonzero. */
5583
5584 static int
5585 template_parm_outer_level (tree t, void *data)
5586 {
5587 int this_level = *(int *)data;
5588 int level;
5589
5590 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5591 level = TEMPLATE_PARM_LEVEL (t);
5592 else
5593 level = TEMPLATE_TYPE_LEVEL (t);
5594 return level <= this_level;
5595 }
5596
5597 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5598 parameters given by current_template_args, or reuses a
5599 previously existing one, if appropriate. Returns the DECL, or an
5600 equivalent one, if it is replaced via a call to duplicate_decls.
5601
5602 If IS_FRIEND is true, DECL is a friend declaration. */
5603
5604 tree
5605 push_template_decl_real (tree decl, bool is_friend)
5606 {
5607 tree tmpl;
5608 tree args;
5609 tree info;
5610 tree ctx;
5611 bool is_primary;
5612 bool is_partial;
5613 int new_template_p = 0;
5614 /* True if the template is a member template, in the sense of
5615 [temp.mem]. */
5616 bool member_template_p = false;
5617
5618 if (decl == error_mark_node || !current_template_parms)
5619 return error_mark_node;
5620
5621 /* See if this is a partial specialization. */
5622 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5623 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5624 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5625 || (VAR_P (decl)
5626 && DECL_LANG_SPECIFIC (decl)
5627 && DECL_TEMPLATE_SPECIALIZATION (decl)
5628 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5629
5630 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5631 is_friend = true;
5632
5633 if (is_friend)
5634 /* For a friend, we want the context of the friend, not
5635 the type of which it is a friend. */
5636 ctx = CP_DECL_CONTEXT (decl);
5637 else if (CP_DECL_CONTEXT (decl)
5638 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5639 /* In the case of a virtual function, we want the class in which
5640 it is defined. */
5641 ctx = CP_DECL_CONTEXT (decl);
5642 else
5643 /* Otherwise, if we're currently defining some class, the DECL
5644 is assumed to be a member of the class. */
5645 ctx = current_scope ();
5646
5647 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5648 ctx = NULL_TREE;
5649
5650 if (!DECL_CONTEXT (decl))
5651 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5652
5653 /* See if this is a primary template. */
5654 if (is_friend && ctx
5655 && uses_template_parms_level (ctx, processing_template_decl))
5656 /* A friend template that specifies a class context, i.e.
5657 template <typename T> friend void A<T>::f();
5658 is not primary. */
5659 is_primary = false;
5660 else if (TREE_CODE (decl) == TYPE_DECL
5661 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5662 is_primary = false;
5663 else
5664 is_primary = template_parm_scope_p ();
5665
5666 if (is_primary)
5667 {
5668 warning (OPT_Wtemplates, "template %qD declared", decl);
5669
5670 if (DECL_CLASS_SCOPE_P (decl))
5671 member_template_p = true;
5672 if (TREE_CODE (decl) == TYPE_DECL
5673 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5674 {
5675 error ("template class without a name");
5676 return error_mark_node;
5677 }
5678 else if (TREE_CODE (decl) == FUNCTION_DECL)
5679 {
5680 if (member_template_p)
5681 {
5682 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5683 error ("member template %qD may not have virt-specifiers", decl);
5684 }
5685 if (DECL_DESTRUCTOR_P (decl))
5686 {
5687 /* [temp.mem]
5688
5689 A destructor shall not be a member template. */
5690 error_at (DECL_SOURCE_LOCATION (decl),
5691 "destructor %qD declared as member template", decl);
5692 return error_mark_node;
5693 }
5694 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5695 && (!prototype_p (TREE_TYPE (decl))
5696 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5697 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5698 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5699 == void_list_node)))
5700 {
5701 /* [basic.stc.dynamic.allocation]
5702
5703 An allocation function can be a function
5704 template. ... Template allocation functions shall
5705 have two or more parameters. */
5706 error ("invalid template declaration of %qD", decl);
5707 return error_mark_node;
5708 }
5709 }
5710 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5711 && CLASS_TYPE_P (TREE_TYPE (decl)))
5712 {
5713 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5714 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5715 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5716 {
5717 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5718 if (TREE_CODE (t) == TYPE_DECL)
5719 t = TREE_TYPE (t);
5720 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5721 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5722 }
5723 }
5724 else if (TREE_CODE (decl) == TYPE_DECL
5725 && TYPE_DECL_ALIAS_P (decl))
5726 /* alias-declaration */
5727 gcc_assert (!DECL_ARTIFICIAL (decl));
5728 else if (VAR_P (decl))
5729 /* C++14 variable template. */;
5730 else if (TREE_CODE (decl) == CONCEPT_DECL)
5731 /* C++2a concept definitions. */;
5732 else
5733 {
5734 error ("template declaration of %q#D", decl);
5735 return error_mark_node;
5736 }
5737 }
5738
5739 /* Check to see that the rules regarding the use of default
5740 arguments are not being violated. We check args for a friend
5741 functions when we know whether it's a definition, introducing
5742 declaration or re-declaration. */
5743 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5744 check_default_tmpl_args (decl, current_template_parms,
5745 is_primary, is_partial, is_friend);
5746
5747 /* Ensure that there are no parameter packs in the type of this
5748 declaration that have not been expanded. */
5749 if (TREE_CODE (decl) == FUNCTION_DECL)
5750 {
5751 /* Check each of the arguments individually to see if there are
5752 any bare parameter packs. */
5753 tree type = TREE_TYPE (decl);
5754 tree arg = DECL_ARGUMENTS (decl);
5755 tree argtype = TYPE_ARG_TYPES (type);
5756
5757 while (arg && argtype)
5758 {
5759 if (!DECL_PACK_P (arg)
5760 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5761 {
5762 /* This is a PARM_DECL that contains unexpanded parameter
5763 packs. We have already complained about this in the
5764 check_for_bare_parameter_packs call, so just replace
5765 these types with ERROR_MARK_NODE. */
5766 TREE_TYPE (arg) = error_mark_node;
5767 TREE_VALUE (argtype) = error_mark_node;
5768 }
5769
5770 arg = DECL_CHAIN (arg);
5771 argtype = TREE_CHAIN (argtype);
5772 }
5773
5774 /* Check for bare parameter packs in the return type and the
5775 exception specifiers. */
5776 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5777 /* Errors were already issued, set return type to int
5778 as the frontend doesn't expect error_mark_node as
5779 the return type. */
5780 TREE_TYPE (type) = integer_type_node;
5781 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5782 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5783 }
5784 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5785 ? DECL_ORIGINAL_TYPE (decl)
5786 : TREE_TYPE (decl)))
5787 {
5788 TREE_TYPE (decl) = error_mark_node;
5789 return error_mark_node;
5790 }
5791
5792 if (is_partial)
5793 return process_partial_specialization (decl);
5794
5795 args = current_template_args ();
5796
5797 if (!ctx
5798 || TREE_CODE (ctx) == FUNCTION_DECL
5799 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5800 || (TREE_CODE (decl) == TYPE_DECL
5801 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5802 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5803 {
5804 if (DECL_LANG_SPECIFIC (decl)
5805 && DECL_TEMPLATE_INFO (decl)
5806 && DECL_TI_TEMPLATE (decl))
5807 tmpl = DECL_TI_TEMPLATE (decl);
5808 /* If DECL is a TYPE_DECL for a class-template, then there won't
5809 be DECL_LANG_SPECIFIC. The information equivalent to
5810 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5811 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5812 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5813 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5814 {
5815 /* Since a template declaration already existed for this
5816 class-type, we must be redeclaring it here. Make sure
5817 that the redeclaration is valid. */
5818 redeclare_class_template (TREE_TYPE (decl),
5819 current_template_parms,
5820 current_template_constraints ());
5821 /* We don't need to create a new TEMPLATE_DECL; just use the
5822 one we already had. */
5823 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5824 }
5825 else
5826 {
5827 tmpl = build_template_decl (decl, current_template_parms,
5828 member_template_p);
5829 new_template_p = 1;
5830
5831 if (DECL_LANG_SPECIFIC (decl)
5832 && DECL_TEMPLATE_SPECIALIZATION (decl))
5833 {
5834 /* A specialization of a member template of a template
5835 class. */
5836 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5837 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5838 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5839 }
5840 }
5841 }
5842 else
5843 {
5844 tree a, t, current, parms;
5845 int i;
5846 tree tinfo = get_template_info (decl);
5847
5848 if (!tinfo)
5849 {
5850 error ("template definition of non-template %q#D", decl);
5851 return error_mark_node;
5852 }
5853
5854 tmpl = TI_TEMPLATE (tinfo);
5855
5856 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5857 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5858 && DECL_TEMPLATE_SPECIALIZATION (decl)
5859 && DECL_MEMBER_TEMPLATE_P (tmpl))
5860 {
5861 tree new_tmpl;
5862
5863 /* The declaration is a specialization of a member
5864 template, declared outside the class. Therefore, the
5865 innermost template arguments will be NULL, so we
5866 replace them with the arguments determined by the
5867 earlier call to check_explicit_specialization. */
5868 args = DECL_TI_ARGS (decl);
5869
5870 new_tmpl
5871 = build_template_decl (decl, current_template_parms,
5872 member_template_p);
5873 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5874 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5875 DECL_TI_TEMPLATE (decl) = new_tmpl;
5876 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5877 DECL_TEMPLATE_INFO (new_tmpl)
5878 = build_template_info (tmpl, args);
5879
5880 register_specialization (new_tmpl,
5881 most_general_template (tmpl),
5882 args,
5883 is_friend, 0);
5884 return decl;
5885 }
5886
5887 /* Make sure the template headers we got make sense. */
5888
5889 parms = DECL_TEMPLATE_PARMS (tmpl);
5890 i = TMPL_PARMS_DEPTH (parms);
5891 if (TMPL_ARGS_DEPTH (args) != i)
5892 {
5893 error ("expected %d levels of template parms for %q#D, got %d",
5894 i, decl, TMPL_ARGS_DEPTH (args));
5895 DECL_INTERFACE_KNOWN (decl) = 1;
5896 return error_mark_node;
5897 }
5898 else
5899 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5900 {
5901 a = TMPL_ARGS_LEVEL (args, i);
5902 t = INNERMOST_TEMPLATE_PARMS (parms);
5903
5904 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5905 {
5906 if (current == decl)
5907 error ("got %d template parameters for %q#D",
5908 TREE_VEC_LENGTH (a), decl);
5909 else
5910 error ("got %d template parameters for %q#T",
5911 TREE_VEC_LENGTH (a), current);
5912 error (" but %d required", TREE_VEC_LENGTH (t));
5913 /* Avoid crash in import_export_decl. */
5914 DECL_INTERFACE_KNOWN (decl) = 1;
5915 return error_mark_node;
5916 }
5917
5918 if (current == decl)
5919 current = ctx;
5920 else if (current == NULL_TREE)
5921 /* Can happen in erroneous input. */
5922 break;
5923 else
5924 current = get_containing_scope (current);
5925 }
5926
5927 /* Check that the parms are used in the appropriate qualifying scopes
5928 in the declarator. */
5929 if (!comp_template_args
5930 (TI_ARGS (tinfo),
5931 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5932 {
5933 error ("template arguments to %qD do not match original "
5934 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5935 if (!uses_template_parms (TI_ARGS (tinfo)))
5936 inform (input_location, "use %<template<>%> for"
5937 " an explicit specialization");
5938 /* Avoid crash in import_export_decl. */
5939 DECL_INTERFACE_KNOWN (decl) = 1;
5940 return error_mark_node;
5941 }
5942 }
5943
5944 DECL_TEMPLATE_RESULT (tmpl) = decl;
5945 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5946
5947 /* Push template declarations for global functions and types. Note
5948 that we do not try to push a global template friend declared in a
5949 template class; such a thing may well depend on the template
5950 parameters of the class. */
5951 if (new_template_p && !ctx
5952 && !(is_friend && template_class_depth (current_class_type) > 0))
5953 {
5954 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5955 if (tmpl == error_mark_node)
5956 return error_mark_node;
5957
5958 /* Hide template friend classes that haven't been declared yet. */
5959 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5960 {
5961 DECL_ANTICIPATED (tmpl) = 1;
5962 DECL_FRIEND_P (tmpl) = 1;
5963 }
5964 }
5965
5966 if (is_primary)
5967 {
5968 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5969
5970 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5971
5972 /* Give template template parms a DECL_CONTEXT of the template
5973 for which they are a parameter. */
5974 parms = INNERMOST_TEMPLATE_PARMS (parms);
5975 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5976 {
5977 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5978 if (TREE_CODE (parm) == TEMPLATE_DECL)
5979 DECL_CONTEXT (parm) = tmpl;
5980 }
5981
5982 if (TREE_CODE (decl) == TYPE_DECL
5983 && TYPE_DECL_ALIAS_P (decl))
5984 {
5985 if (tree constr
5986 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
5987 {
5988 /* ??? Why don't we do this here for all templates? */
5989 constr = build_constraints (constr, NULL_TREE);
5990 set_constraints (decl, constr);
5991 }
5992 if (complex_alias_template_p (tmpl))
5993 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5994 }
5995 }
5996
5997 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5998 back to its most general template. If TMPL is a specialization,
5999 ARGS may only have the innermost set of arguments. Add the missing
6000 argument levels if necessary. */
6001 if (DECL_TEMPLATE_INFO (tmpl))
6002 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6003
6004 info = build_template_info (tmpl, args);
6005
6006 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6007 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6008 else
6009 {
6010 if (is_primary)
6011 retrofit_lang_decl (decl);
6012 if (DECL_LANG_SPECIFIC (decl))
6013 DECL_TEMPLATE_INFO (decl) = info;
6014 }
6015
6016 if (flag_implicit_templates
6017 && !is_friend
6018 && TREE_PUBLIC (decl)
6019 && VAR_OR_FUNCTION_DECL_P (decl))
6020 /* Set DECL_COMDAT on template instantiations; if we force
6021 them to be emitted by explicit instantiation,
6022 mark_needed will tell cgraph to do the right thing. */
6023 DECL_COMDAT (decl) = true;
6024
6025 return DECL_TEMPLATE_RESULT (tmpl);
6026 }
6027
6028 tree
6029 push_template_decl (tree decl)
6030 {
6031 return push_template_decl_real (decl, false);
6032 }
6033
6034 /* FN is an inheriting constructor that inherits from the constructor
6035 template INHERITED; turn FN into a constructor template with a matching
6036 template header. */
6037
6038 tree
6039 add_inherited_template_parms (tree fn, tree inherited)
6040 {
6041 tree inner_parms
6042 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6043 inner_parms = copy_node (inner_parms);
6044 tree parms
6045 = tree_cons (size_int (processing_template_decl + 1),
6046 inner_parms, current_template_parms);
6047 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6048 tree args = template_parms_to_args (parms);
6049 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6050 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6051 DECL_TEMPLATE_RESULT (tmpl) = fn;
6052 DECL_ARTIFICIAL (tmpl) = true;
6053 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6054 return tmpl;
6055 }
6056
6057 /* Called when a class template TYPE is redeclared with the indicated
6058 template PARMS, e.g.:
6059
6060 template <class T> struct S;
6061 template <class T> struct S {}; */
6062
6063 bool
6064 redeclare_class_template (tree type, tree parms, tree cons)
6065 {
6066 tree tmpl;
6067 tree tmpl_parms;
6068 int i;
6069
6070 if (!TYPE_TEMPLATE_INFO (type))
6071 {
6072 error ("%qT is not a template type", type);
6073 return false;
6074 }
6075
6076 tmpl = TYPE_TI_TEMPLATE (type);
6077 if (!PRIMARY_TEMPLATE_P (tmpl))
6078 /* The type is nested in some template class. Nothing to worry
6079 about here; there are no new template parameters for the nested
6080 type. */
6081 return true;
6082
6083 if (!parms)
6084 {
6085 error ("template specifiers not specified in declaration of %qD",
6086 tmpl);
6087 return false;
6088 }
6089
6090 parms = INNERMOST_TEMPLATE_PARMS (parms);
6091 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6092
6093 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6094 {
6095 error_n (input_location, TREE_VEC_LENGTH (parms),
6096 "redeclared with %d template parameter",
6097 "redeclared with %d template parameters",
6098 TREE_VEC_LENGTH (parms));
6099 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6100 "previous declaration %qD used %d template parameter",
6101 "previous declaration %qD used %d template parameters",
6102 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6103 return false;
6104 }
6105
6106 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6107 {
6108 tree tmpl_parm;
6109 tree parm;
6110 tree tmpl_default;
6111 tree parm_default;
6112
6113 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6114 || TREE_VEC_ELT (parms, i) == error_mark_node)
6115 continue;
6116
6117 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6118 if (error_operand_p (tmpl_parm))
6119 return false;
6120
6121 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6122 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6123 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6124
6125 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6126 TEMPLATE_DECL. */
6127 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6128 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6129 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6130 || (TREE_CODE (tmpl_parm) != PARM_DECL
6131 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6132 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6133 || (TREE_CODE (tmpl_parm) == PARM_DECL
6134 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6135 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6136 {
6137 auto_diagnostic_group d;
6138 error ("template parameter %q+#D", tmpl_parm);
6139 inform (input_location, "redeclared here as %q#D", parm);
6140 return false;
6141 }
6142
6143 /* The parameters can be declared to introduce different
6144 constraints. */
6145 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6146 tree p2 = TREE_VEC_ELT (parms, i);
6147 if (!template_parameter_constraints_equivalent_p (p1, p2))
6148 {
6149 auto_diagnostic_group d;
6150 error ("declaration of template parameter %q+#D with different "
6151 "constraints", parm);
6152 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6153 "original declaration appeared here");
6154 return false;
6155 }
6156
6157 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6158 {
6159 /* We have in [temp.param]:
6160
6161 A template-parameter may not be given default arguments
6162 by two different declarations in the same scope. */
6163 auto_diagnostic_group d;
6164 error_at (input_location, "redefinition of default argument for %q#D", parm);
6165 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6166 "original definition appeared here");
6167 return false;
6168 }
6169
6170 if (parm_default != NULL_TREE)
6171 /* Update the previous template parameters (which are the ones
6172 that will really count) with the new default value. */
6173 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6174 else if (tmpl_default != NULL_TREE)
6175 /* Update the new parameters, too; they'll be used as the
6176 parameters for any members. */
6177 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6178
6179 /* Give each template template parm in this redeclaration a
6180 DECL_CONTEXT of the template for which they are a parameter. */
6181 if (TREE_CODE (parm) == TEMPLATE_DECL)
6182 {
6183 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6184 DECL_CONTEXT (parm) = tmpl;
6185 }
6186
6187 if (TREE_CODE (parm) == TYPE_DECL)
6188 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6189 }
6190
6191 tree ci = get_constraints (tmpl);
6192 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6193 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6194
6195 /* Two classes with different constraints declare different entities. */
6196 if (!cp_tree_equal (req1, req2))
6197 {
6198 auto_diagnostic_group d;
6199 error_at (input_location, "redeclaration %q#D with different "
6200 "constraints", tmpl);
6201 inform (DECL_SOURCE_LOCATION (tmpl),
6202 "original declaration appeared here");
6203 return false;
6204 }
6205
6206 return true;
6207 }
6208
6209 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6210 to be used when the caller has already checked
6211 (processing_template_decl
6212 && !instantiation_dependent_expression_p (expr)
6213 && potential_constant_expression (expr))
6214 and cleared processing_template_decl. */
6215
6216 tree
6217 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6218 {
6219 return tsubst_copy_and_build (expr,
6220 /*args=*/NULL_TREE,
6221 complain,
6222 /*in_decl=*/NULL_TREE,
6223 /*function_p=*/false,
6224 /*integral_constant_expression_p=*/true);
6225 }
6226
6227 /* Simplify EXPR if it is a non-dependent expression. Returns the
6228 (possibly simplified) expression. */
6229
6230 tree
6231 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6232 {
6233 if (expr == NULL_TREE)
6234 return NULL_TREE;
6235
6236 /* If we're in a template, but EXPR isn't value dependent, simplify
6237 it. We're supposed to treat:
6238
6239 template <typename T> void f(T[1 + 1]);
6240 template <typename T> void f(T[2]);
6241
6242 as two declarations of the same function, for example. */
6243 if (processing_template_decl
6244 && is_nondependent_constant_expression (expr))
6245 {
6246 processing_template_decl_sentinel s;
6247 expr = instantiate_non_dependent_expr_internal (expr, complain);
6248 }
6249 return expr;
6250 }
6251
6252 tree
6253 instantiate_non_dependent_expr (tree expr)
6254 {
6255 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6256 }
6257
6258 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6259 an uninstantiated expression. */
6260
6261 tree
6262 instantiate_non_dependent_or_null (tree expr)
6263 {
6264 if (expr == NULL_TREE)
6265 return NULL_TREE;
6266 if (processing_template_decl)
6267 {
6268 if (!is_nondependent_constant_expression (expr))
6269 expr = NULL_TREE;
6270 else
6271 {
6272 processing_template_decl_sentinel s;
6273 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6274 }
6275 }
6276 return expr;
6277 }
6278
6279 /* True iff T is a specialization of a variable template. */
6280
6281 bool
6282 variable_template_specialization_p (tree t)
6283 {
6284 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6285 return false;
6286 tree tmpl = DECL_TI_TEMPLATE (t);
6287 return variable_template_p (tmpl);
6288 }
6289
6290 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6291 template declaration, or a TYPE_DECL for an alias declaration. */
6292
6293 bool
6294 alias_type_or_template_p (tree t)
6295 {
6296 if (t == NULL_TREE)
6297 return false;
6298 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6299 || (TYPE_P (t)
6300 && TYPE_NAME (t)
6301 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6302 || DECL_ALIAS_TEMPLATE_P (t));
6303 }
6304
6305 /* If T is a specialization of an alias template, return it; otherwise return
6306 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6307
6308 tree
6309 alias_template_specialization_p (const_tree t,
6310 bool transparent_typedefs)
6311 {
6312 if (!TYPE_P (t))
6313 return NULL_TREE;
6314
6315 /* It's an alias template specialization if it's an alias and its
6316 TYPE_NAME is a specialization of a primary template. */
6317 if (typedef_variant_p (t))
6318 {
6319 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6320 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6321 return CONST_CAST_TREE (t);
6322 if (transparent_typedefs)
6323 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6324 (TYPE_NAME (t)),
6325 transparent_typedefs);
6326 }
6327
6328 return NULL_TREE;
6329 }
6330
6331 /* An alias template is complex from a SFINAE perspective if a template-id
6332 using that alias can be ill-formed when the expansion is not, as with
6333 the void_t template. We determine this by checking whether the
6334 expansion for the alias template uses all its template parameters. */
6335
6336 struct uses_all_template_parms_data
6337 {
6338 int level;
6339 bool *seen;
6340 };
6341
6342 static int
6343 uses_all_template_parms_r (tree t, void *data_)
6344 {
6345 struct uses_all_template_parms_data &data
6346 = *(struct uses_all_template_parms_data*)data_;
6347 tree idx = get_template_parm_index (t);
6348
6349 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6350 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6351 return 0;
6352 }
6353
6354 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6355
6356 static int
6357 complex_pack_expansion_r (tree t, void *data_)
6358 {
6359 /* An alias template with a pack expansion that expands a pack from the
6360 enclosing class needs to be considered complex, to avoid confusion with
6361 the same pack being used as an argument to the alias's own template
6362 parameter (91966). */
6363 if (!PACK_EXPANSION_P (t))
6364 return 0;
6365 struct uses_all_template_parms_data &data
6366 = *(struct uses_all_template_parms_data*)data_;
6367 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6368 pack = TREE_CHAIN (pack))
6369 {
6370 tree parm_pack = TREE_VALUE (pack);
6371 if (!TEMPLATE_PARM_P (parm_pack))
6372 continue;
6373 int idx, level;
6374 template_parm_level_and_index (parm_pack, &level, &idx);
6375 if (level < data.level)
6376 return 1;
6377 }
6378 return 0;
6379 }
6380
6381 static bool
6382 complex_alias_template_p (const_tree tmpl)
6383 {
6384 /* A renaming alias isn't complex. */
6385 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6386 return false;
6387
6388 /* Any other constrained alias is complex. */
6389 if (get_constraints (tmpl))
6390 return true;
6391
6392 struct uses_all_template_parms_data data;
6393 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6394 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6395 data.level = TMPL_PARMS_DEPTH (parms);
6396 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6397 data.seen = XALLOCAVEC (bool, len);
6398 for (int i = 0; i < len; ++i)
6399 data.seen[i] = false;
6400
6401 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6402 NULL, true, complex_pack_expansion_r))
6403 return true;
6404 for (int i = 0; i < len; ++i)
6405 if (!data.seen[i])
6406 return true;
6407 return false;
6408 }
6409
6410 /* If T is a specialization of a complex alias template with dependent
6411 template-arguments, return it; otherwise return NULL_TREE. If T is a
6412 typedef to such a specialization, return the specialization. */
6413
6414 tree
6415 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6416 {
6417 if (!TYPE_P (t) || !typedef_variant_p (t))
6418 return NULL_TREE;
6419
6420 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6421 if (tinfo
6422 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6423 && (any_dependent_template_arguments_p
6424 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6425 return CONST_CAST_TREE (t);
6426
6427 if (transparent_typedefs)
6428 {
6429 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6430 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6431 }
6432
6433 return NULL_TREE;
6434 }
6435
6436 /* Return the number of innermost template parameters in TMPL. */
6437
6438 static int
6439 num_innermost_template_parms (const_tree tmpl)
6440 {
6441 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6442 return TREE_VEC_LENGTH (parms);
6443 }
6444
6445 /* Return either TMPL or another template that it is equivalent to under DR
6446 1286: An alias that just changes the name of a template is equivalent to
6447 the other template. */
6448
6449 static tree
6450 get_underlying_template (tree tmpl)
6451 {
6452 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6453 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6454 {
6455 /* Determine if the alias is equivalent to an underlying template. */
6456 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6457 /* The underlying type may have been ill-formed. Don't proceed. */
6458 if (!orig_type)
6459 break;
6460 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6461 if (!tinfo)
6462 break;
6463
6464 tree underlying = TI_TEMPLATE (tinfo);
6465 if (!PRIMARY_TEMPLATE_P (underlying)
6466 || (num_innermost_template_parms (tmpl)
6467 != num_innermost_template_parms (underlying)))
6468 break;
6469
6470 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6471 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6472 break;
6473
6474 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6475 it's appropriate to treat a less-constrained alias as equivalent. */
6476 if (!at_least_as_constrained (underlying, tmpl))
6477 break;
6478
6479 /* Alias is equivalent. Strip it and repeat. */
6480 tmpl = underlying;
6481 }
6482
6483 return tmpl;
6484 }
6485
6486 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6487 must be a reference-to-function or a pointer-to-function type, as specified
6488 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6489 and check that the resulting function has external linkage. */
6490
6491 static tree
6492 convert_nontype_argument_function (tree type, tree expr,
6493 tsubst_flags_t complain)
6494 {
6495 tree fns = expr;
6496 tree fn, fn_no_ptr;
6497 linkage_kind linkage;
6498
6499 fn = instantiate_type (type, fns, tf_none);
6500 if (fn == error_mark_node)
6501 return error_mark_node;
6502
6503 if (value_dependent_expression_p (fn))
6504 goto accept;
6505
6506 fn_no_ptr = strip_fnptr_conv (fn);
6507 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6508 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6509 if (BASELINK_P (fn_no_ptr))
6510 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6511
6512 /* [temp.arg.nontype]/1
6513
6514 A template-argument for a non-type, non-template template-parameter
6515 shall be one of:
6516 [...]
6517 -- the address of an object or function with external [C++11: or
6518 internal] linkage. */
6519
6520 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6521 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6522 {
6523 if (complain & tf_error)
6524 {
6525 location_t loc = cp_expr_loc_or_input_loc (expr);
6526 error_at (loc, "%qE is not a valid template argument for type %qT",
6527 expr, type);
6528 if (TYPE_PTR_P (type))
6529 inform (loc, "it must be the address of a function "
6530 "with external linkage");
6531 else
6532 inform (loc, "it must be the name of a function with "
6533 "external linkage");
6534 }
6535 return NULL_TREE;
6536 }
6537
6538 linkage = decl_linkage (fn_no_ptr);
6539 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6540 {
6541 if (complain & tf_error)
6542 {
6543 location_t loc = cp_expr_loc_or_input_loc (expr);
6544 if (cxx_dialect >= cxx11)
6545 error_at (loc, "%qE is not a valid template argument for type "
6546 "%qT because %qD has no linkage",
6547 expr, type, fn_no_ptr);
6548 else
6549 error_at (loc, "%qE is not a valid template argument for type "
6550 "%qT because %qD does not have external linkage",
6551 expr, type, fn_no_ptr);
6552 }
6553 return NULL_TREE;
6554 }
6555
6556 accept:
6557 if (TYPE_REF_P (type))
6558 {
6559 if (REFERENCE_REF_P (fn))
6560 fn = TREE_OPERAND (fn, 0);
6561 else
6562 fn = build_address (fn);
6563 }
6564 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6565 fn = build_nop (type, fn);
6566
6567 return fn;
6568 }
6569
6570 /* Subroutine of convert_nontype_argument.
6571 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6572 Emit an error otherwise. */
6573
6574 static bool
6575 check_valid_ptrmem_cst_expr (tree type, tree expr,
6576 tsubst_flags_t complain)
6577 {
6578 tree orig_expr = expr;
6579 STRIP_NOPS (expr);
6580 if (null_ptr_cst_p (expr))
6581 return true;
6582 if (TREE_CODE (expr) == PTRMEM_CST
6583 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6584 PTRMEM_CST_CLASS (expr)))
6585 return true;
6586 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6587 return true;
6588 if (processing_template_decl
6589 && TREE_CODE (expr) == ADDR_EXPR
6590 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6591 return true;
6592 if (complain & tf_error)
6593 {
6594 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6595 error_at (loc, "%qE is not a valid template argument for type %qT",
6596 orig_expr, type);
6597 if (TREE_CODE (expr) != PTRMEM_CST)
6598 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6599 else
6600 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6601 }
6602 return false;
6603 }
6604
6605 /* Returns TRUE iff the address of OP is value-dependent.
6606
6607 14.6.2.4 [temp.dep.temp]:
6608 A non-integral non-type template-argument is dependent if its type is
6609 dependent or it has either of the following forms
6610 qualified-id
6611 & qualified-id
6612 and contains a nested-name-specifier which specifies a class-name that
6613 names a dependent type.
6614
6615 We generalize this to just say that the address of a member of a
6616 dependent class is value-dependent; the above doesn't cover the
6617 address of a static data member named with an unqualified-id. */
6618
6619 static bool
6620 has_value_dependent_address (tree op)
6621 {
6622 STRIP_ANY_LOCATION_WRAPPER (op);
6623
6624 /* We could use get_inner_reference here, but there's no need;
6625 this is only relevant for template non-type arguments, which
6626 can only be expressed as &id-expression. */
6627 if (DECL_P (op))
6628 {
6629 tree ctx = CP_DECL_CONTEXT (op);
6630 if (TYPE_P (ctx) && dependent_type_p (ctx))
6631 return true;
6632 }
6633
6634 return false;
6635 }
6636
6637 /* The next set of functions are used for providing helpful explanatory
6638 diagnostics for failed overload resolution. Their messages should be
6639 indented by two spaces for consistency with the messages in
6640 call.c */
6641
6642 static int
6643 unify_success (bool /*explain_p*/)
6644 {
6645 return 0;
6646 }
6647
6648 /* Other failure functions should call this one, to provide a single function
6649 for setting a breakpoint on. */
6650
6651 static int
6652 unify_invalid (bool /*explain_p*/)
6653 {
6654 return 1;
6655 }
6656
6657 static int
6658 unify_parameter_deduction_failure (bool explain_p, tree parm)
6659 {
6660 if (explain_p)
6661 inform (input_location,
6662 " couldn%'t deduce template parameter %qD", parm);
6663 return unify_invalid (explain_p);
6664 }
6665
6666 static int
6667 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6668 {
6669 if (explain_p)
6670 inform (input_location,
6671 " types %qT and %qT have incompatible cv-qualifiers",
6672 parm, arg);
6673 return unify_invalid (explain_p);
6674 }
6675
6676 static int
6677 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6678 {
6679 if (explain_p)
6680 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6681 return unify_invalid (explain_p);
6682 }
6683
6684 static int
6685 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6686 {
6687 if (explain_p)
6688 inform (input_location,
6689 " template parameter %qD is not a parameter pack, but "
6690 "argument %qD is",
6691 parm, arg);
6692 return unify_invalid (explain_p);
6693 }
6694
6695 static int
6696 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6697 {
6698 if (explain_p)
6699 inform (input_location,
6700 " template argument %qE does not match "
6701 "pointer-to-member constant %qE",
6702 arg, parm);
6703 return unify_invalid (explain_p);
6704 }
6705
6706 static int
6707 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6708 {
6709 if (explain_p)
6710 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6711 return unify_invalid (explain_p);
6712 }
6713
6714 static int
6715 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6716 {
6717 if (explain_p)
6718 inform (input_location,
6719 " inconsistent parameter pack deduction with %qT and %qT",
6720 old_arg, new_arg);
6721 return unify_invalid (explain_p);
6722 }
6723
6724 static int
6725 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6726 {
6727 if (explain_p)
6728 {
6729 if (TYPE_P (parm))
6730 inform (input_location,
6731 " deduced conflicting types for parameter %qT (%qT and %qT)",
6732 parm, first, second);
6733 else
6734 inform (input_location,
6735 " deduced conflicting values for non-type parameter "
6736 "%qE (%qE and %qE)", parm, first, second);
6737 }
6738 return unify_invalid (explain_p);
6739 }
6740
6741 static int
6742 unify_vla_arg (bool explain_p, tree arg)
6743 {
6744 if (explain_p)
6745 inform (input_location,
6746 " variable-sized array type %qT is not "
6747 "a valid template argument",
6748 arg);
6749 return unify_invalid (explain_p);
6750 }
6751
6752 static int
6753 unify_method_type_error (bool explain_p, tree arg)
6754 {
6755 if (explain_p)
6756 inform (input_location,
6757 " member function type %qT is not a valid template argument",
6758 arg);
6759 return unify_invalid (explain_p);
6760 }
6761
6762 static int
6763 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6764 {
6765 if (explain_p)
6766 {
6767 if (least_p)
6768 inform_n (input_location, wanted,
6769 " candidate expects at least %d argument, %d provided",
6770 " candidate expects at least %d arguments, %d provided",
6771 wanted, have);
6772 else
6773 inform_n (input_location, wanted,
6774 " candidate expects %d argument, %d provided",
6775 " candidate expects %d arguments, %d provided",
6776 wanted, have);
6777 }
6778 return unify_invalid (explain_p);
6779 }
6780
6781 static int
6782 unify_too_many_arguments (bool explain_p, int have, int wanted)
6783 {
6784 return unify_arity (explain_p, have, wanted);
6785 }
6786
6787 static int
6788 unify_too_few_arguments (bool explain_p, int have, int wanted,
6789 bool least_p = false)
6790 {
6791 return unify_arity (explain_p, have, wanted, least_p);
6792 }
6793
6794 static int
6795 unify_arg_conversion (bool explain_p, tree to_type,
6796 tree from_type, tree arg)
6797 {
6798 if (explain_p)
6799 inform (cp_expr_loc_or_input_loc (arg),
6800 " cannot convert %qE (type %qT) to type %qT",
6801 arg, from_type, to_type);
6802 return unify_invalid (explain_p);
6803 }
6804
6805 static int
6806 unify_no_common_base (bool explain_p, enum template_base_result r,
6807 tree parm, tree arg)
6808 {
6809 if (explain_p)
6810 switch (r)
6811 {
6812 case tbr_ambiguous_baseclass:
6813 inform (input_location, " %qT is an ambiguous base class of %qT",
6814 parm, arg);
6815 break;
6816 default:
6817 inform (input_location, " %qT is not derived from %qT", arg, parm);
6818 break;
6819 }
6820 return unify_invalid (explain_p);
6821 }
6822
6823 static int
6824 unify_inconsistent_template_template_parameters (bool explain_p)
6825 {
6826 if (explain_p)
6827 inform (input_location,
6828 " template parameters of a template template argument are "
6829 "inconsistent with other deduced template arguments");
6830 return unify_invalid (explain_p);
6831 }
6832
6833 static int
6834 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6835 {
6836 if (explain_p)
6837 inform (input_location,
6838 " cannot deduce a template for %qT from non-template type %qT",
6839 parm, arg);
6840 return unify_invalid (explain_p);
6841 }
6842
6843 static int
6844 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6845 {
6846 if (explain_p)
6847 inform (input_location,
6848 " template argument %qE does not match %qE", arg, parm);
6849 return unify_invalid (explain_p);
6850 }
6851
6852 /* True if T is a C++20 template parameter object to store the argument for a
6853 template parameter of class type. */
6854
6855 bool
6856 template_parm_object_p (const_tree t)
6857 {
6858 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6859 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6860 }
6861
6862 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6863 argument for TYPE, points to an unsuitable object. */
6864
6865 static bool
6866 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6867 {
6868 switch (TREE_CODE (expr))
6869 {
6870 CASE_CONVERT:
6871 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6872 complain);
6873
6874 case TARGET_EXPR:
6875 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6876 complain);
6877
6878 case CONSTRUCTOR:
6879 {
6880 unsigned i; tree elt;
6881 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6882 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6883 return true;
6884 }
6885 break;
6886
6887 case ADDR_EXPR:
6888 {
6889 tree decl = TREE_OPERAND (expr, 0);
6890
6891 if (!VAR_P (decl))
6892 {
6893 if (complain & tf_error)
6894 error_at (cp_expr_loc_or_input_loc (expr),
6895 "%qE is not a valid template argument of type %qT "
6896 "because %qE is not a variable", expr, type, decl);
6897 return true;
6898 }
6899 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6900 {
6901 if (complain & tf_error)
6902 error_at (cp_expr_loc_or_input_loc (expr),
6903 "%qE is not a valid template argument of type %qT "
6904 "in C++98 because %qD does not have external linkage",
6905 expr, type, decl);
6906 return true;
6907 }
6908 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6909 && decl_linkage (decl) == lk_none)
6910 {
6911 if (complain & tf_error)
6912 error_at (cp_expr_loc_or_input_loc (expr),
6913 "%qE is not a valid template argument of type %qT "
6914 "because %qD has no linkage", expr, type, decl);
6915 return true;
6916 }
6917 /* C++17: For a non-type template-parameter of reference or pointer
6918 type, the value of the constant expression shall not refer to (or
6919 for a pointer type, shall not be the address of):
6920 * a subobject (4.5),
6921 * a temporary object (15.2),
6922 * a string literal (5.13.5),
6923 * the result of a typeid expression (8.2.8), or
6924 * a predefined __func__ variable (11.4.1). */
6925 else if (DECL_ARTIFICIAL (decl))
6926 {
6927 if (complain & tf_error)
6928 error ("the address of %qD is not a valid template argument",
6929 decl);
6930 return true;
6931 }
6932 else if (!same_type_ignoring_top_level_qualifiers_p
6933 (strip_array_types (TREE_TYPE (type)),
6934 strip_array_types (TREE_TYPE (decl))))
6935 {
6936 if (complain & tf_error)
6937 error ("the address of the %qT subobject of %qD is not a "
6938 "valid template argument", TREE_TYPE (type), decl);
6939 return true;
6940 }
6941 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6942 {
6943 if (complain & tf_error)
6944 error ("the address of %qD is not a valid template argument "
6945 "because it does not have static storage duration",
6946 decl);
6947 return true;
6948 }
6949 }
6950 break;
6951
6952 default:
6953 if (!INDIRECT_TYPE_P (type))
6954 /* We're only concerned about pointers and references here. */;
6955 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6956 /* Null pointer values are OK in C++11. */;
6957 else
6958 {
6959 if (VAR_P (expr))
6960 {
6961 if (complain & tf_error)
6962 error ("%qD is not a valid template argument "
6963 "because %qD is a variable, not the address of "
6964 "a variable", expr, expr);
6965 return true;
6966 }
6967 else
6968 {
6969 if (complain & tf_error)
6970 error ("%qE is not a valid template argument for %qT "
6971 "because it is not the address of a variable",
6972 expr, type);
6973 return true;
6974 }
6975 }
6976 }
6977 return false;
6978
6979 }
6980
6981 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6982 template argument EXPR. */
6983
6984 static tree
6985 get_template_parm_object (tree expr, tsubst_flags_t complain)
6986 {
6987 if (TREE_CODE (expr) == TARGET_EXPR)
6988 expr = TARGET_EXPR_INITIAL (expr);
6989
6990 if (!TREE_CONSTANT (expr))
6991 {
6992 if ((complain & tf_error)
6993 && require_rvalue_constant_expression (expr))
6994 cxx_constant_value (expr);
6995 return error_mark_node;
6996 }
6997 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6998 return error_mark_node;
6999
7000 tree name = mangle_template_parm_object (expr);
7001 tree decl = get_global_binding (name);
7002 if (decl)
7003 return decl;
7004
7005 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7006 decl = create_temporary_var (type);
7007 TREE_STATIC (decl) = true;
7008 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7009 TREE_READONLY (decl) = true;
7010 DECL_NAME (decl) = name;
7011 SET_DECL_ASSEMBLER_NAME (decl, name);
7012 DECL_CONTEXT (decl) = global_namespace;
7013 comdat_linkage (decl);
7014 pushdecl_top_level_and_finish (decl, expr);
7015 return decl;
7016 }
7017
7018 /* Attempt to convert the non-type template parameter EXPR to the
7019 indicated TYPE. If the conversion is successful, return the
7020 converted value. If the conversion is unsuccessful, return
7021 NULL_TREE if we issued an error message, or error_mark_node if we
7022 did not. We issue error messages for out-and-out bad template
7023 parameters, but not simply because the conversion failed, since we
7024 might be just trying to do argument deduction. Both TYPE and EXPR
7025 must be non-dependent.
7026
7027 The conversion follows the special rules described in
7028 [temp.arg.nontype], and it is much more strict than an implicit
7029 conversion.
7030
7031 This function is called twice for each template argument (see
7032 lookup_template_class for a more accurate description of this
7033 problem). This means that we need to handle expressions which
7034 are not valid in a C++ source, but can be created from the
7035 first call (for instance, casts to perform conversions). These
7036 hacks can go away after we fix the double coercion problem. */
7037
7038 static tree
7039 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7040 {
7041 tree expr_type;
7042 location_t loc = cp_expr_loc_or_input_loc (expr);
7043
7044 /* Detect immediately string literals as invalid non-type argument.
7045 This special-case is not needed for correctness (we would easily
7046 catch this later), but only to provide better diagnostic for this
7047 common user mistake. As suggested by DR 100, we do not mention
7048 linkage issues in the diagnostic as this is not the point. */
7049 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7050 {
7051 if (complain & tf_error)
7052 error ("%qE is not a valid template argument for type %qT "
7053 "because string literals can never be used in this context",
7054 expr, type);
7055 return NULL_TREE;
7056 }
7057
7058 /* Add the ADDR_EXPR now for the benefit of
7059 value_dependent_expression_p. */
7060 if (TYPE_PTROBV_P (type)
7061 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7062 {
7063 expr = decay_conversion (expr, complain);
7064 if (expr == error_mark_node)
7065 return error_mark_node;
7066 }
7067
7068 /* If we are in a template, EXPR may be non-dependent, but still
7069 have a syntactic, rather than semantic, form. For example, EXPR
7070 might be a SCOPE_REF, rather than the VAR_DECL to which the
7071 SCOPE_REF refers. Preserving the qualifying scope is necessary
7072 so that access checking can be performed when the template is
7073 instantiated -- but here we need the resolved form so that we can
7074 convert the argument. */
7075 bool non_dep = false;
7076 if (TYPE_REF_OBJ_P (type)
7077 && has_value_dependent_address (expr))
7078 /* If we want the address and it's value-dependent, don't fold. */;
7079 else if (processing_template_decl
7080 && is_nondependent_constant_expression (expr))
7081 non_dep = true;
7082 if (error_operand_p (expr))
7083 return error_mark_node;
7084 expr_type = TREE_TYPE (expr);
7085
7086 /* If the argument is non-dependent, perform any conversions in
7087 non-dependent context as well. */
7088 processing_template_decl_sentinel s (non_dep);
7089 if (non_dep)
7090 expr = instantiate_non_dependent_expr_internal (expr, complain);
7091
7092 const bool val_dep_p = value_dependent_expression_p (expr);
7093 if (val_dep_p)
7094 expr = canonicalize_expr_argument (expr, complain);
7095
7096 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7097 to a non-type argument of "nullptr". */
7098 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7099 expr = fold_simple (convert (type, expr));
7100
7101 /* In C++11, integral or enumeration non-type template arguments can be
7102 arbitrary constant expressions. Pointer and pointer to
7103 member arguments can be general constant expressions that evaluate
7104 to a null value, but otherwise still need to be of a specific form. */
7105 if (cxx_dialect >= cxx11)
7106 {
7107 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7108 /* A PTRMEM_CST is already constant, and a valid template
7109 argument for a parameter of pointer to member type, we just want
7110 to leave it in that form rather than lower it to a
7111 CONSTRUCTOR. */;
7112 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7113 || cxx_dialect >= cxx17)
7114 {
7115 /* C++17: A template-argument for a non-type template-parameter shall
7116 be a converted constant expression (8.20) of the type of the
7117 template-parameter. */
7118 expr = build_converted_constant_expr (type, expr, complain);
7119 if (expr == error_mark_node)
7120 /* Make sure we return NULL_TREE only if we have really issued
7121 an error, as described above. */
7122 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7123 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7124 {
7125 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7126 return expr;
7127 }
7128 expr = maybe_constant_value (expr, NULL_TREE,
7129 /*manifestly_const_eval=*/true);
7130 expr = convert_from_reference (expr);
7131 }
7132 else if (TYPE_PTR_OR_PTRMEM_P (type))
7133 {
7134 tree folded = maybe_constant_value (expr, NULL_TREE,
7135 /*manifestly_const_eval=*/true);
7136 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7137 : null_member_pointer_value_p (folded))
7138 expr = folded;
7139 }
7140 }
7141
7142 if (TYPE_REF_P (type))
7143 expr = mark_lvalue_use (expr);
7144 else
7145 expr = mark_rvalue_use (expr);
7146
7147 /* HACK: Due to double coercion, we can get a
7148 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7149 which is the tree that we built on the first call (see
7150 below when coercing to reference to object or to reference to
7151 function). We just strip everything and get to the arg.
7152 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7153 for examples. */
7154 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7155 {
7156 tree probe_type, probe = expr;
7157 if (REFERENCE_REF_P (probe))
7158 probe = TREE_OPERAND (probe, 0);
7159 probe_type = TREE_TYPE (probe);
7160 if (TREE_CODE (probe) == NOP_EXPR)
7161 {
7162 /* ??? Maybe we could use convert_from_reference here, but we
7163 would need to relax its constraints because the NOP_EXPR
7164 could actually change the type to something more cv-qualified,
7165 and this is not folded by convert_from_reference. */
7166 tree addr = TREE_OPERAND (probe, 0);
7167 if (TYPE_REF_P (probe_type)
7168 && TREE_CODE (addr) == ADDR_EXPR
7169 && TYPE_PTR_P (TREE_TYPE (addr))
7170 && (same_type_ignoring_top_level_qualifiers_p
7171 (TREE_TYPE (probe_type),
7172 TREE_TYPE (TREE_TYPE (addr)))))
7173 {
7174 expr = TREE_OPERAND (addr, 0);
7175 expr_type = TREE_TYPE (probe_type);
7176 }
7177 }
7178 }
7179
7180 /* [temp.arg.nontype]/5, bullet 1
7181
7182 For a non-type template-parameter of integral or enumeration type,
7183 integral promotions (_conv.prom_) and integral conversions
7184 (_conv.integral_) are applied. */
7185 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7186 {
7187 if (cxx_dialect < cxx11)
7188 {
7189 tree t = build_converted_constant_expr (type, expr, complain);
7190 t = maybe_constant_value (t);
7191 if (t != error_mark_node)
7192 expr = t;
7193 }
7194
7195 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7196 return error_mark_node;
7197
7198 /* Notice that there are constant expressions like '4 % 0' which
7199 do not fold into integer constants. */
7200 if (TREE_CODE (expr) != INTEGER_CST && !val_dep_p)
7201 {
7202 if (complain & tf_error)
7203 {
7204 int errs = errorcount, warns = warningcount + werrorcount;
7205 if (!require_potential_constant_expression (expr))
7206 expr = error_mark_node;
7207 else
7208 expr = cxx_constant_value (expr);
7209 if (errorcount > errs || warningcount + werrorcount > warns)
7210 inform (loc, "in template argument for type %qT", type);
7211 if (expr == error_mark_node)
7212 return NULL_TREE;
7213 /* else cxx_constant_value complained but gave us
7214 a real constant, so go ahead. */
7215 if (TREE_CODE (expr) != INTEGER_CST)
7216 {
7217 /* Some assemble time constant expressions like
7218 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7219 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7220 as we can emit them into .rodata initializers of
7221 variables, yet they can't fold into an INTEGER_CST at
7222 compile time. Refuse them here. */
7223 gcc_checking_assert (reduced_constant_expression_p (expr));
7224 error_at (loc, "template argument %qE for type %qT not "
7225 "a constant integer", expr, type);
7226 return NULL_TREE;
7227 }
7228 }
7229 else
7230 return NULL_TREE;
7231 }
7232
7233 /* Avoid typedef problems. */
7234 if (TREE_TYPE (expr) != type)
7235 expr = fold_convert (type, expr);
7236 }
7237 /* [temp.arg.nontype]/5, bullet 2
7238
7239 For a non-type template-parameter of type pointer to object,
7240 qualification conversions (_conv.qual_) and the array-to-pointer
7241 conversion (_conv.array_) are applied. */
7242 else if (TYPE_PTROBV_P (type))
7243 {
7244 tree decayed = expr;
7245
7246 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7247 decay_conversion or an explicit cast. If it's a problematic cast,
7248 we'll complain about it below. */
7249 if (TREE_CODE (expr) == NOP_EXPR)
7250 {
7251 tree probe = expr;
7252 STRIP_NOPS (probe);
7253 if (TREE_CODE (probe) == ADDR_EXPR
7254 && TYPE_PTR_P (TREE_TYPE (probe)))
7255 {
7256 expr = probe;
7257 expr_type = TREE_TYPE (expr);
7258 }
7259 }
7260
7261 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7262
7263 A template-argument for a non-type, non-template template-parameter
7264 shall be one of: [...]
7265
7266 -- the name of a non-type template-parameter;
7267 -- the address of an object or function with external linkage, [...]
7268 expressed as "& id-expression" where the & is optional if the name
7269 refers to a function or array, or if the corresponding
7270 template-parameter is a reference.
7271
7272 Here, we do not care about functions, as they are invalid anyway
7273 for a parameter of type pointer-to-object. */
7274
7275 if (val_dep_p)
7276 /* Non-type template parameters are OK. */
7277 ;
7278 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7279 /* Null pointer values are OK in C++11. */;
7280 else if (TREE_CODE (expr) != ADDR_EXPR
7281 && !INDIRECT_TYPE_P (expr_type))
7282 /* Other values, like integer constants, might be valid
7283 non-type arguments of some other type. */
7284 return error_mark_node;
7285 else if (invalid_tparm_referent_p (type, expr, complain))
7286 return NULL_TREE;
7287
7288 expr = decayed;
7289
7290 expr = perform_qualification_conversions (type, expr);
7291 if (expr == error_mark_node)
7292 return error_mark_node;
7293 }
7294 /* [temp.arg.nontype]/5, bullet 3
7295
7296 For a non-type template-parameter of type reference to object, no
7297 conversions apply. The type referred to by the reference may be more
7298 cv-qualified than the (otherwise identical) type of the
7299 template-argument. The template-parameter is bound directly to the
7300 template-argument, which must be an lvalue. */
7301 else if (TYPE_REF_OBJ_P (type))
7302 {
7303 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7304 expr_type))
7305 return error_mark_node;
7306
7307 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7308 {
7309 if (complain & tf_error)
7310 error ("%qE is not a valid template argument for type %qT "
7311 "because of conflicts in cv-qualification", expr, type);
7312 return NULL_TREE;
7313 }
7314
7315 if (!lvalue_p (expr))
7316 {
7317 if (complain & tf_error)
7318 error ("%qE is not a valid template argument for type %qT "
7319 "because it is not an lvalue", expr, type);
7320 return NULL_TREE;
7321 }
7322
7323 /* [temp.arg.nontype]/1
7324
7325 A template-argument for a non-type, non-template template-parameter
7326 shall be one of: [...]
7327
7328 -- the address of an object or function with external linkage. */
7329 if (INDIRECT_REF_P (expr)
7330 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7331 {
7332 expr = TREE_OPERAND (expr, 0);
7333 if (DECL_P (expr))
7334 {
7335 if (complain & tf_error)
7336 error ("%q#D is not a valid template argument for type %qT "
7337 "because a reference variable does not have a constant "
7338 "address", expr, type);
7339 return NULL_TREE;
7340 }
7341 }
7342
7343 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7344 /* OK, dependent reference. We don't want to ask whether a DECL is
7345 itself value-dependent, since what we want here is its address. */;
7346 else
7347 {
7348 expr = build_address (expr);
7349
7350 if (invalid_tparm_referent_p (type, expr, complain))
7351 return NULL_TREE;
7352 }
7353
7354 if (!same_type_p (type, TREE_TYPE (expr)))
7355 expr = build_nop (type, expr);
7356 }
7357 /* [temp.arg.nontype]/5, bullet 4
7358
7359 For a non-type template-parameter of type pointer to function, only
7360 the function-to-pointer conversion (_conv.func_) is applied. If the
7361 template-argument represents a set of overloaded functions (or a
7362 pointer to such), the matching function is selected from the set
7363 (_over.over_). */
7364 else if (TYPE_PTRFN_P (type))
7365 {
7366 /* If the argument is a template-id, we might not have enough
7367 context information to decay the pointer. */
7368 if (!type_unknown_p (expr_type))
7369 {
7370 expr = decay_conversion (expr, complain);
7371 if (expr == error_mark_node)
7372 return error_mark_node;
7373 }
7374
7375 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7376 /* Null pointer values are OK in C++11. */
7377 return perform_qualification_conversions (type, expr);
7378
7379 expr = convert_nontype_argument_function (type, expr, complain);
7380 if (!expr || expr == error_mark_node)
7381 return expr;
7382 }
7383 /* [temp.arg.nontype]/5, bullet 5
7384
7385 For a non-type template-parameter of type reference to function, no
7386 conversions apply. If the template-argument represents a set of
7387 overloaded functions, the matching function is selected from the set
7388 (_over.over_). */
7389 else if (TYPE_REFFN_P (type))
7390 {
7391 if (TREE_CODE (expr) == ADDR_EXPR)
7392 {
7393 if (complain & tf_error)
7394 {
7395 error ("%qE is not a valid template argument for type %qT "
7396 "because it is a pointer", expr, type);
7397 inform (input_location, "try using %qE instead",
7398 TREE_OPERAND (expr, 0));
7399 }
7400 return NULL_TREE;
7401 }
7402
7403 expr = convert_nontype_argument_function (type, expr, complain);
7404 if (!expr || expr == error_mark_node)
7405 return expr;
7406 }
7407 /* [temp.arg.nontype]/5, bullet 6
7408
7409 For a non-type template-parameter of type pointer to member function,
7410 no conversions apply. If the template-argument represents a set of
7411 overloaded member functions, the matching member function is selected
7412 from the set (_over.over_). */
7413 else if (TYPE_PTRMEMFUNC_P (type))
7414 {
7415 expr = instantiate_type (type, expr, tf_none);
7416 if (expr == error_mark_node)
7417 return error_mark_node;
7418
7419 /* [temp.arg.nontype] bullet 1 says the pointer to member
7420 expression must be a pointer-to-member constant. */
7421 if (!val_dep_p
7422 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7423 return NULL_TREE;
7424
7425 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7426 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7427 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7428 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7429 }
7430 /* [temp.arg.nontype]/5, bullet 7
7431
7432 For a non-type template-parameter of type pointer to data member,
7433 qualification conversions (_conv.qual_) are applied. */
7434 else if (TYPE_PTRDATAMEM_P (type))
7435 {
7436 /* [temp.arg.nontype] bullet 1 says the pointer to member
7437 expression must be a pointer-to-member constant. */
7438 if (!val_dep_p
7439 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7440 return NULL_TREE;
7441
7442 expr = perform_qualification_conversions (type, expr);
7443 if (expr == error_mark_node)
7444 return expr;
7445 }
7446 else if (NULLPTR_TYPE_P (type))
7447 {
7448 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7449 {
7450 if (complain & tf_error)
7451 error ("%qE is not a valid template argument for type %qT "
7452 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7453 return NULL_TREE;
7454 }
7455 return expr;
7456 }
7457 else if (CLASS_TYPE_P (type))
7458 {
7459 /* Replace the argument with a reference to the corresponding template
7460 parameter object. */
7461 if (!val_dep_p)
7462 expr = get_template_parm_object (expr, complain);
7463 if (expr == error_mark_node)
7464 return NULL_TREE;
7465 }
7466 /* A template non-type parameter must be one of the above. */
7467 else
7468 gcc_unreachable ();
7469
7470 /* Sanity check: did we actually convert the argument to the
7471 right type? */
7472 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7473 (type, TREE_TYPE (expr)));
7474 return convert_from_reference (expr);
7475 }
7476
7477 /* Subroutine of coerce_template_template_parms, which returns 1 if
7478 PARM_PARM and ARG_PARM match using the rule for the template
7479 parameters of template template parameters. Both PARM and ARG are
7480 template parameters; the rest of the arguments are the same as for
7481 coerce_template_template_parms.
7482 */
7483 static int
7484 coerce_template_template_parm (tree parm,
7485 tree arg,
7486 tsubst_flags_t complain,
7487 tree in_decl,
7488 tree outer_args)
7489 {
7490 if (arg == NULL_TREE || error_operand_p (arg)
7491 || parm == NULL_TREE || error_operand_p (parm))
7492 return 0;
7493
7494 if (TREE_CODE (arg) != TREE_CODE (parm))
7495 return 0;
7496
7497 switch (TREE_CODE (parm))
7498 {
7499 case TEMPLATE_DECL:
7500 /* We encounter instantiations of templates like
7501 template <template <template <class> class> class TT>
7502 class C; */
7503 {
7504 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7505 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7506
7507 if (!coerce_template_template_parms
7508 (parmparm, argparm, complain, in_decl, outer_args))
7509 return 0;
7510 }
7511 /* Fall through. */
7512
7513 case TYPE_DECL:
7514 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7515 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7516 /* Argument is a parameter pack but parameter is not. */
7517 return 0;
7518 break;
7519
7520 case PARM_DECL:
7521 /* The tsubst call is used to handle cases such as
7522
7523 template <int> class C {};
7524 template <class T, template <T> class TT> class D {};
7525 D<int, C> d;
7526
7527 i.e. the parameter list of TT depends on earlier parameters. */
7528 if (!uses_template_parms (TREE_TYPE (arg)))
7529 {
7530 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7531 if (!uses_template_parms (t)
7532 && !same_type_p (t, TREE_TYPE (arg)))
7533 return 0;
7534 }
7535
7536 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7537 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7538 /* Argument is a parameter pack but parameter is not. */
7539 return 0;
7540
7541 break;
7542
7543 default:
7544 gcc_unreachable ();
7545 }
7546
7547 return 1;
7548 }
7549
7550 /* Coerce template argument list ARGLIST for use with template
7551 template-parameter TEMPL. */
7552
7553 static tree
7554 coerce_template_args_for_ttp (tree templ, tree arglist,
7555 tsubst_flags_t complain)
7556 {
7557 /* Consider an example where a template template parameter declared as
7558
7559 template <class T, class U = std::allocator<T> > class TT
7560
7561 The template parameter level of T and U are one level larger than
7562 of TT. To proper process the default argument of U, say when an
7563 instantiation `TT<int>' is seen, we need to build the full
7564 arguments containing {int} as the innermost level. Outer levels,
7565 available when not appearing as default template argument, can be
7566 obtained from the arguments of the enclosing template.
7567
7568 Suppose that TT is later substituted with std::vector. The above
7569 instantiation is `TT<int, std::allocator<T> >' with TT at
7570 level 1, and T at level 2, while the template arguments at level 1
7571 becomes {std::vector} and the inner level 2 is {int}. */
7572
7573 tree outer = DECL_CONTEXT (templ);
7574 if (outer)
7575 outer = generic_targs_for (outer);
7576 else if (current_template_parms)
7577 {
7578 /* This is an argument of the current template, so we haven't set
7579 DECL_CONTEXT yet. */
7580 tree relevant_template_parms;
7581
7582 /* Parameter levels that are greater than the level of the given
7583 template template parm are irrelevant. */
7584 relevant_template_parms = current_template_parms;
7585 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7586 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7587 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7588
7589 outer = template_parms_to_args (relevant_template_parms);
7590 }
7591
7592 if (outer)
7593 arglist = add_to_template_args (outer, arglist);
7594
7595 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7596 return coerce_template_parms (parmlist, arglist, templ,
7597 complain,
7598 /*require_all_args=*/true,
7599 /*use_default_args=*/true);
7600 }
7601
7602 /* A cache of template template parameters with match-all default
7603 arguments. */
7604 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7605
7606 /* T is a bound template template-parameter. Copy its arguments into default
7607 arguments of the template template-parameter's template parameters. */
7608
7609 static tree
7610 add_defaults_to_ttp (tree otmpl)
7611 {
7612 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7613 return *c;
7614
7615 tree ntmpl = copy_node (otmpl);
7616
7617 tree ntype = copy_node (TREE_TYPE (otmpl));
7618 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7619 TYPE_MAIN_VARIANT (ntype) = ntype;
7620 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7621 TYPE_NAME (ntype) = ntmpl;
7622 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7623
7624 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7625 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7626 TEMPLATE_PARM_DECL (idx) = ntmpl;
7627 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7628
7629 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7630 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7631 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7632 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7633 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7634 {
7635 tree o = TREE_VEC_ELT (vec, i);
7636 if (!template_parameter_pack_p (TREE_VALUE (o)))
7637 {
7638 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7639 TREE_PURPOSE (n) = any_targ_node;
7640 }
7641 }
7642
7643 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7644 return ntmpl;
7645 }
7646
7647 /* ARG is a bound potential template template-argument, and PARGS is a list
7648 of arguments for the corresponding template template-parameter. Adjust
7649 PARGS as appropriate for application to ARG's template, and if ARG is a
7650 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7651 arguments to the template template parameter. */
7652
7653 static tree
7654 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7655 {
7656 ++processing_template_decl;
7657 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7658 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7659 {
7660 /* When comparing two template template-parameters in partial ordering,
7661 rewrite the one currently being used as an argument to have default
7662 arguments for all parameters. */
7663 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7664 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7665 if (pargs != error_mark_node)
7666 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7667 TYPE_TI_ARGS (arg));
7668 }
7669 else
7670 {
7671 tree aparms
7672 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7673 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7674 /*require_all*/true,
7675 /*use_default*/true);
7676 }
7677 --processing_template_decl;
7678 return pargs;
7679 }
7680
7681 /* Subroutine of unify for the case when PARM is a
7682 BOUND_TEMPLATE_TEMPLATE_PARM. */
7683
7684 static int
7685 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7686 bool explain_p)
7687 {
7688 tree parmvec = TYPE_TI_ARGS (parm);
7689 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7690
7691 /* The template template parm might be variadic and the argument
7692 not, so flatten both argument lists. */
7693 parmvec = expand_template_argument_pack (parmvec);
7694 argvec = expand_template_argument_pack (argvec);
7695
7696 if (flag_new_ttp)
7697 {
7698 /* In keeping with P0522R0, adjust P's template arguments
7699 to apply to A's template; then flatten it again. */
7700 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7701 nparmvec = expand_template_argument_pack (nparmvec);
7702
7703 if (unify (tparms, targs, nparmvec, argvec,
7704 UNIFY_ALLOW_NONE, explain_p))
7705 return 1;
7706
7707 /* If the P0522 adjustment eliminated a pack expansion, deduce
7708 empty packs. */
7709 if (flag_new_ttp
7710 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7711 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7712 DEDUCE_EXACT, /*sub*/true, explain_p))
7713 return 1;
7714 }
7715 else
7716 {
7717 /* Deduce arguments T, i from TT<T> or TT<i>.
7718 We check each element of PARMVEC and ARGVEC individually
7719 rather than the whole TREE_VEC since they can have
7720 different number of elements, which is allowed under N2555. */
7721
7722 int len = TREE_VEC_LENGTH (parmvec);
7723
7724 /* Check if the parameters end in a pack, making them
7725 variadic. */
7726 int parm_variadic_p = 0;
7727 if (len > 0
7728 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7729 parm_variadic_p = 1;
7730
7731 for (int i = 0; i < len - parm_variadic_p; ++i)
7732 /* If the template argument list of P contains a pack
7733 expansion that is not the last template argument, the
7734 entire template argument list is a non-deduced
7735 context. */
7736 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7737 return unify_success (explain_p);
7738
7739 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7740 return unify_too_few_arguments (explain_p,
7741 TREE_VEC_LENGTH (argvec), len);
7742
7743 for (int i = 0; i < len - parm_variadic_p; ++i)
7744 if (unify (tparms, targs,
7745 TREE_VEC_ELT (parmvec, i),
7746 TREE_VEC_ELT (argvec, i),
7747 UNIFY_ALLOW_NONE, explain_p))
7748 return 1;
7749
7750 if (parm_variadic_p
7751 && unify_pack_expansion (tparms, targs,
7752 parmvec, argvec,
7753 DEDUCE_EXACT,
7754 /*subr=*/true, explain_p))
7755 return 1;
7756 }
7757
7758 return 0;
7759 }
7760
7761 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7762 template template parameters. Both PARM_PARMS and ARG_PARMS are
7763 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7764 or PARM_DECL.
7765
7766 Consider the example:
7767 template <class T> class A;
7768 template<template <class U> class TT> class B;
7769
7770 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7771 the parameters to A, and OUTER_ARGS contains A. */
7772
7773 static int
7774 coerce_template_template_parms (tree parm_parms,
7775 tree arg_parms,
7776 tsubst_flags_t complain,
7777 tree in_decl,
7778 tree outer_args)
7779 {
7780 int nparms, nargs, i;
7781 tree parm, arg;
7782 int variadic_p = 0;
7783
7784 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7785 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7786
7787 nparms = TREE_VEC_LENGTH (parm_parms);
7788 nargs = TREE_VEC_LENGTH (arg_parms);
7789
7790 if (flag_new_ttp)
7791 {
7792 /* P0522R0: A template template-parameter P is at least as specialized as
7793 a template template-argument A if, given the following rewrite to two
7794 function templates, the function template corresponding to P is at
7795 least as specialized as the function template corresponding to A
7796 according to the partial ordering rules for function templates
7797 ([temp.func.order]). Given an invented class template X with the
7798 template parameter list of A (including default arguments):
7799
7800 * Each of the two function templates has the same template parameters,
7801 respectively, as P or A.
7802
7803 * Each function template has a single function parameter whose type is
7804 a specialization of X with template arguments corresponding to the
7805 template parameters from the respective function template where, for
7806 each template parameter PP in the template parameter list of the
7807 function template, a corresponding template argument AA is formed. If
7808 PP declares a parameter pack, then AA is the pack expansion
7809 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7810
7811 If the rewrite produces an invalid type, then P is not at least as
7812 specialized as A. */
7813
7814 /* So coerce P's args to apply to A's parms, and then deduce between A's
7815 args and the converted args. If that succeeds, A is at least as
7816 specialized as P, so they match.*/
7817 tree pargs = template_parms_level_to_args (parm_parms);
7818 pargs = add_outermost_template_args (outer_args, pargs);
7819 ++processing_template_decl;
7820 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7821 /*require_all*/true, /*use_default*/true);
7822 --processing_template_decl;
7823 if (pargs != error_mark_node)
7824 {
7825 tree targs = make_tree_vec (nargs);
7826 tree aargs = template_parms_level_to_args (arg_parms);
7827 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7828 /*explain*/false))
7829 return 1;
7830 }
7831 }
7832
7833 /* Determine whether we have a parameter pack at the end of the
7834 template template parameter's template parameter list. */
7835 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7836 {
7837 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7838
7839 if (error_operand_p (parm))
7840 return 0;
7841
7842 switch (TREE_CODE (parm))
7843 {
7844 case TEMPLATE_DECL:
7845 case TYPE_DECL:
7846 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7847 variadic_p = 1;
7848 break;
7849
7850 case PARM_DECL:
7851 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7852 variadic_p = 1;
7853 break;
7854
7855 default:
7856 gcc_unreachable ();
7857 }
7858 }
7859
7860 if (nargs != nparms
7861 && !(variadic_p && nargs >= nparms - 1))
7862 return 0;
7863
7864 /* Check all of the template parameters except the parameter pack at
7865 the end (if any). */
7866 for (i = 0; i < nparms - variadic_p; ++i)
7867 {
7868 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7869 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7870 continue;
7871
7872 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7873 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7874
7875 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7876 outer_args))
7877 return 0;
7878
7879 }
7880
7881 if (variadic_p)
7882 {
7883 /* Check each of the template parameters in the template
7884 argument against the template parameter pack at the end of
7885 the template template parameter. */
7886 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7887 return 0;
7888
7889 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7890
7891 for (; i < nargs; ++i)
7892 {
7893 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7894 continue;
7895
7896 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7897
7898 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7899 outer_args))
7900 return 0;
7901 }
7902 }
7903
7904 return 1;
7905 }
7906
7907 /* Verifies that the deduced template arguments (in TARGS) for the
7908 template template parameters (in TPARMS) represent valid bindings,
7909 by comparing the template parameter list of each template argument
7910 to the template parameter list of its corresponding template
7911 template parameter, in accordance with DR150. This
7912 routine can only be called after all template arguments have been
7913 deduced. It will return TRUE if all of the template template
7914 parameter bindings are okay, FALSE otherwise. */
7915 bool
7916 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7917 {
7918 int i, ntparms = TREE_VEC_LENGTH (tparms);
7919 bool ret = true;
7920
7921 /* We're dealing with template parms in this process. */
7922 ++processing_template_decl;
7923
7924 targs = INNERMOST_TEMPLATE_ARGS (targs);
7925
7926 for (i = 0; i < ntparms; ++i)
7927 {
7928 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7929 tree targ = TREE_VEC_ELT (targs, i);
7930
7931 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7932 {
7933 tree packed_args = NULL_TREE;
7934 int idx, len = 1;
7935
7936 if (ARGUMENT_PACK_P (targ))
7937 {
7938 /* Look inside the argument pack. */
7939 packed_args = ARGUMENT_PACK_ARGS (targ);
7940 len = TREE_VEC_LENGTH (packed_args);
7941 }
7942
7943 for (idx = 0; idx < len; ++idx)
7944 {
7945 tree targ_parms = NULL_TREE;
7946
7947 if (packed_args)
7948 /* Extract the next argument from the argument
7949 pack. */
7950 targ = TREE_VEC_ELT (packed_args, idx);
7951
7952 if (PACK_EXPANSION_P (targ))
7953 /* Look at the pattern of the pack expansion. */
7954 targ = PACK_EXPANSION_PATTERN (targ);
7955
7956 /* Extract the template parameters from the template
7957 argument. */
7958 if (TREE_CODE (targ) == TEMPLATE_DECL)
7959 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7960 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7961 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7962
7963 /* Verify that we can coerce the template template
7964 parameters from the template argument to the template
7965 parameter. This requires an exact match. */
7966 if (targ_parms
7967 && !coerce_template_template_parms
7968 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7969 targ_parms,
7970 tf_none,
7971 tparm,
7972 targs))
7973 {
7974 ret = false;
7975 goto out;
7976 }
7977 }
7978 }
7979 }
7980
7981 out:
7982
7983 --processing_template_decl;
7984 return ret;
7985 }
7986
7987 /* Since type attributes aren't mangled, we need to strip them from
7988 template type arguments. */
7989
7990 tree
7991 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7992 {
7993 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7994 return arg;
7995 bool removed_attributes = false;
7996 tree canon = strip_typedefs (arg, &removed_attributes);
7997 if (removed_attributes
7998 && (complain & tf_warning))
7999 warning (OPT_Wignored_attributes,
8000 "ignoring attributes on template argument %qT", arg);
8001 return canon;
8002 }
8003
8004 /* And from inside dependent non-type arguments like sizeof(Type). */
8005
8006 static tree
8007 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8008 {
8009 if (!arg || arg == error_mark_node)
8010 return arg;
8011 bool removed_attributes = false;
8012 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8013 if (removed_attributes
8014 && (complain & tf_warning))
8015 warning (OPT_Wignored_attributes,
8016 "ignoring attributes in template argument %qE", arg);
8017 return canon;
8018 }
8019
8020 // A template declaration can be substituted for a constrained
8021 // template template parameter only when the argument is more
8022 // constrained than the parameter.
8023 static bool
8024 is_compatible_template_arg (tree parm, tree arg)
8025 {
8026 tree parm_cons = get_constraints (parm);
8027
8028 /* For now, allow constrained template template arguments
8029 and unconstrained template template parameters. */
8030 if (parm_cons == NULL_TREE)
8031 return true;
8032
8033 /* If the template parameter is constrained, we need to rewrite its
8034 constraints in terms of the ARG's template parameters. This ensures
8035 that all of the template parameter types will have the same depth.
8036
8037 Note that this is only valid when coerce_template_template_parm is
8038 true for the innermost template parameters of PARM and ARG. In other
8039 words, because coercion is successful, this conversion will be valid. */
8040 tree new_args = NULL_TREE;
8041 if (parm_cons)
8042 {
8043 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8044 new_args = template_parms_level_to_args (aparms);
8045 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8046 tf_none, NULL_TREE);
8047 if (parm_cons == error_mark_node)
8048 return false;
8049 }
8050
8051 return weakly_subsumes (parm_cons, new_args, arg);
8052 }
8053
8054 // Convert a placeholder argument into a binding to the original
8055 // parameter. The original parameter is saved as the TREE_TYPE of
8056 // ARG.
8057 static inline tree
8058 convert_wildcard_argument (tree parm, tree arg)
8059 {
8060 TREE_TYPE (arg) = parm;
8061 return arg;
8062 }
8063
8064 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8065 because one of them is dependent. But we need to represent the
8066 conversion for the benefit of cp_tree_equal. */
8067
8068 static tree
8069 maybe_convert_nontype_argument (tree type, tree arg)
8070 {
8071 /* Auto parms get no conversion. */
8072 if (type_uses_auto (type))
8073 return arg;
8074 /* We don't need or want to add this conversion now if we're going to use the
8075 argument for deduction. */
8076 if (value_dependent_expression_p (arg))
8077 return arg;
8078
8079 type = cv_unqualified (type);
8080 tree argtype = TREE_TYPE (arg);
8081 if (same_type_p (type, argtype))
8082 return arg;
8083
8084 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8085 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8086 return arg;
8087 }
8088
8089 /* Convert the indicated template ARG as necessary to match the
8090 indicated template PARM. Returns the converted ARG, or
8091 error_mark_node if the conversion was unsuccessful. Error and
8092 warning messages are issued under control of COMPLAIN. This
8093 conversion is for the Ith parameter in the parameter list. ARGS is
8094 the full set of template arguments deduced so far. */
8095
8096 static tree
8097 convert_template_argument (tree parm,
8098 tree arg,
8099 tree args,
8100 tsubst_flags_t complain,
8101 int i,
8102 tree in_decl)
8103 {
8104 tree orig_arg;
8105 tree val;
8106 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8107
8108 if (parm == error_mark_node || error_operand_p (arg))
8109 return error_mark_node;
8110
8111 /* Trivially convert placeholders. */
8112 if (TREE_CODE (arg) == WILDCARD_DECL)
8113 return convert_wildcard_argument (parm, arg);
8114
8115 if (arg == any_targ_node)
8116 return arg;
8117
8118 if (TREE_CODE (arg) == TREE_LIST
8119 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8120 {
8121 /* The template argument was the name of some
8122 member function. That's usually
8123 invalid, but static members are OK. In any
8124 case, grab the underlying fields/functions
8125 and issue an error later if required. */
8126 TREE_TYPE (arg) = unknown_type_node;
8127 }
8128
8129 orig_arg = arg;
8130
8131 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8132 requires_type = (TREE_CODE (parm) == TYPE_DECL
8133 || requires_tmpl_type);
8134
8135 /* When determining whether an argument pack expansion is a template,
8136 look at the pattern. */
8137 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8138 arg = PACK_EXPANSION_PATTERN (arg);
8139
8140 /* Deal with an injected-class-name used as a template template arg. */
8141 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8142 {
8143 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8144 if (TREE_CODE (t) == TEMPLATE_DECL)
8145 {
8146 if (cxx_dialect >= cxx11)
8147 /* OK under DR 1004. */;
8148 else if (complain & tf_warning_or_error)
8149 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8150 " used as template template argument", TYPE_NAME (arg));
8151 else if (flag_pedantic_errors)
8152 t = arg;
8153
8154 arg = t;
8155 }
8156 }
8157
8158 is_tmpl_type =
8159 ((TREE_CODE (arg) == TEMPLATE_DECL
8160 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8161 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8162 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8163 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8164
8165 if (is_tmpl_type
8166 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8167 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8168 arg = TYPE_STUB_DECL (arg);
8169
8170 is_type = TYPE_P (arg) || is_tmpl_type;
8171
8172 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8173 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8174 {
8175 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8176 {
8177 if (complain & tf_error)
8178 error ("invalid use of destructor %qE as a type", orig_arg);
8179 return error_mark_node;
8180 }
8181
8182 permerror (input_location,
8183 "to refer to a type member of a template parameter, "
8184 "use %<typename %E%>", orig_arg);
8185
8186 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8187 TREE_OPERAND (arg, 1),
8188 typename_type,
8189 complain);
8190 arg = orig_arg;
8191 is_type = 1;
8192 }
8193 if (is_type != requires_type)
8194 {
8195 if (in_decl)
8196 {
8197 if (complain & tf_error)
8198 {
8199 error ("type/value mismatch at argument %d in template "
8200 "parameter list for %qD",
8201 i + 1, in_decl);
8202 if (is_type)
8203 {
8204 /* The template argument is a type, but we're expecting
8205 an expression. */
8206 inform (input_location,
8207 " expected a constant of type %qT, got %qT",
8208 TREE_TYPE (parm),
8209 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8210 /* [temp.arg]/2: "In a template-argument, an ambiguity
8211 between a type-id and an expression is resolved to a
8212 type-id, regardless of the form of the corresponding
8213 template-parameter." So give the user a clue. */
8214 if (TREE_CODE (arg) == FUNCTION_TYPE)
8215 inform (input_location, " ambiguous template argument "
8216 "for non-type template parameter is treated as "
8217 "function type");
8218 }
8219 else if (requires_tmpl_type)
8220 inform (input_location,
8221 " expected a class template, got %qE", orig_arg);
8222 else
8223 inform (input_location,
8224 " expected a type, got %qE", orig_arg);
8225 }
8226 }
8227 return error_mark_node;
8228 }
8229 if (is_tmpl_type ^ requires_tmpl_type)
8230 {
8231 if (in_decl && (complain & tf_error))
8232 {
8233 error ("type/value mismatch at argument %d in template "
8234 "parameter list for %qD",
8235 i + 1, in_decl);
8236 if (is_tmpl_type)
8237 inform (input_location,
8238 " expected a type, got %qT", DECL_NAME (arg));
8239 else
8240 inform (input_location,
8241 " expected a class template, got %qT", orig_arg);
8242 }
8243 return error_mark_node;
8244 }
8245
8246 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8247 /* We already did the appropriate conversion when packing args. */
8248 val = orig_arg;
8249 else if (is_type)
8250 {
8251 if (requires_tmpl_type)
8252 {
8253 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8254 /* The number of argument required is not known yet.
8255 Just accept it for now. */
8256 val = orig_arg;
8257 else
8258 {
8259 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8260 tree argparm;
8261
8262 /* Strip alias templates that are equivalent to another
8263 template. */
8264 arg = get_underlying_template (arg);
8265 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8266
8267 if (coerce_template_template_parms (parmparm, argparm,
8268 complain, in_decl,
8269 args))
8270 {
8271 val = arg;
8272
8273 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8274 TEMPLATE_DECL. */
8275 if (val != error_mark_node)
8276 {
8277 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8278 val = TREE_TYPE (val);
8279 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8280 val = make_pack_expansion (val, complain);
8281 }
8282 }
8283 else
8284 {
8285 if (in_decl && (complain & tf_error))
8286 {
8287 error ("type/value mismatch at argument %d in "
8288 "template parameter list for %qD",
8289 i + 1, in_decl);
8290 inform (input_location,
8291 " expected a template of type %qD, got %qT",
8292 parm, orig_arg);
8293 }
8294
8295 val = error_mark_node;
8296 }
8297
8298 // Check that the constraints are compatible before allowing the
8299 // substitution.
8300 if (val != error_mark_node)
8301 if (!is_compatible_template_arg (parm, arg))
8302 {
8303 if (in_decl && (complain & tf_error))
8304 {
8305 error ("constraint mismatch at argument %d in "
8306 "template parameter list for %qD",
8307 i + 1, in_decl);
8308 inform (input_location, " expected %qD but got %qD",
8309 parm, arg);
8310 }
8311 val = error_mark_node;
8312 }
8313 }
8314 }
8315 else
8316 val = orig_arg;
8317 /* We only form one instance of each template specialization.
8318 Therefore, if we use a non-canonical variant (i.e., a
8319 typedef), any future messages referring to the type will use
8320 the typedef, which is confusing if those future uses do not
8321 themselves also use the typedef. */
8322 if (TYPE_P (val))
8323 val = canonicalize_type_argument (val, complain);
8324 }
8325 else
8326 {
8327 tree t = TREE_TYPE (parm);
8328
8329 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8330 > TMPL_ARGS_DEPTH (args))
8331 /* We don't have enough levels of args to do any substitution. This
8332 can happen in the context of -fnew-ttp-matching. */;
8333 else if (tree a = type_uses_auto (t))
8334 {
8335 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8336 if (t == error_mark_node)
8337 return error_mark_node;
8338 }
8339 else
8340 t = tsubst (t, args, complain, in_decl);
8341
8342 if (invalid_nontype_parm_type_p (t, complain))
8343 return error_mark_node;
8344
8345 if (t != TREE_TYPE (parm))
8346 t = canonicalize_type_argument (t, complain);
8347
8348 if (!type_dependent_expression_p (orig_arg)
8349 && !uses_template_parms (t))
8350 /* We used to call digest_init here. However, digest_init
8351 will report errors, which we don't want when complain
8352 is zero. More importantly, digest_init will try too
8353 hard to convert things: for example, `0' should not be
8354 converted to pointer type at this point according to
8355 the standard. Accepting this is not merely an
8356 extension, since deciding whether or not these
8357 conversions can occur is part of determining which
8358 function template to call, or whether a given explicit
8359 argument specification is valid. */
8360 val = convert_nontype_argument (t, orig_arg, complain);
8361 else
8362 {
8363 val = canonicalize_expr_argument (orig_arg, complain);
8364 val = maybe_convert_nontype_argument (t, val);
8365 }
8366
8367
8368 if (val == NULL_TREE)
8369 val = error_mark_node;
8370 else if (val == error_mark_node && (complain & tf_error))
8371 error_at (cp_expr_loc_or_input_loc (orig_arg),
8372 "could not convert template argument %qE from %qT to %qT",
8373 orig_arg, TREE_TYPE (orig_arg), t);
8374
8375 if (INDIRECT_REF_P (val))
8376 {
8377 /* Reject template arguments that are references to built-in
8378 functions with no library fallbacks. */
8379 const_tree inner = TREE_OPERAND (val, 0);
8380 const_tree innertype = TREE_TYPE (inner);
8381 if (innertype
8382 && TYPE_REF_P (innertype)
8383 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8384 && TREE_OPERAND_LENGTH (inner) > 0
8385 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8386 return error_mark_node;
8387 }
8388
8389 if (TREE_CODE (val) == SCOPE_REF)
8390 {
8391 /* Strip typedefs from the SCOPE_REF. */
8392 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8393 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8394 complain);
8395 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8396 QUALIFIED_NAME_IS_TEMPLATE (val));
8397 }
8398 }
8399
8400 return val;
8401 }
8402
8403 /* Coerces the remaining template arguments in INNER_ARGS (from
8404 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8405 Returns the coerced argument pack. PARM_IDX is the position of this
8406 parameter in the template parameter list. ARGS is the original
8407 template argument list. */
8408 static tree
8409 coerce_template_parameter_pack (tree parms,
8410 int parm_idx,
8411 tree args,
8412 tree inner_args,
8413 int arg_idx,
8414 tree new_args,
8415 int* lost,
8416 tree in_decl,
8417 tsubst_flags_t complain)
8418 {
8419 tree parm = TREE_VEC_ELT (parms, parm_idx);
8420 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8421 tree packed_args;
8422 tree argument_pack;
8423 tree packed_parms = NULL_TREE;
8424
8425 if (arg_idx > nargs)
8426 arg_idx = nargs;
8427
8428 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8429 {
8430 /* When the template parameter is a non-type template parameter pack
8431 or template template parameter pack whose type or template
8432 parameters use parameter packs, we know exactly how many arguments
8433 we are looking for. Build a vector of the instantiated decls for
8434 these template parameters in PACKED_PARMS. */
8435 /* We can't use make_pack_expansion here because it would interpret a
8436 _DECL as a use rather than a declaration. */
8437 tree decl = TREE_VALUE (parm);
8438 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8439 SET_PACK_EXPANSION_PATTERN (exp, decl);
8440 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8441 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8442
8443 TREE_VEC_LENGTH (args)--;
8444 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8445 TREE_VEC_LENGTH (args)++;
8446
8447 if (packed_parms == error_mark_node)
8448 return error_mark_node;
8449
8450 /* If we're doing a partial instantiation of a member template,
8451 verify that all of the types used for the non-type
8452 template parameter pack are, in fact, valid for non-type
8453 template parameters. */
8454 if (arg_idx < nargs
8455 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8456 {
8457 int j, len = TREE_VEC_LENGTH (packed_parms);
8458 for (j = 0; j < len; ++j)
8459 {
8460 tree t = TREE_VEC_ELT (packed_parms, j);
8461 if (TREE_CODE (t) == PARM_DECL
8462 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8463 return error_mark_node;
8464 }
8465 /* We don't know how many args we have yet, just
8466 use the unconverted ones for now. */
8467 return NULL_TREE;
8468 }
8469
8470 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8471 }
8472 /* Check if we have a placeholder pack, which indicates we're
8473 in the context of a introduction list. In that case we want
8474 to match this pack to the single placeholder. */
8475 else if (arg_idx < nargs
8476 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8477 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8478 {
8479 nargs = arg_idx + 1;
8480 packed_args = make_tree_vec (1);
8481 }
8482 else
8483 packed_args = make_tree_vec (nargs - arg_idx);
8484
8485 /* Convert the remaining arguments, which will be a part of the
8486 parameter pack "parm". */
8487 int first_pack_arg = arg_idx;
8488 for (; arg_idx < nargs; ++arg_idx)
8489 {
8490 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8491 tree actual_parm = TREE_VALUE (parm);
8492 int pack_idx = arg_idx - first_pack_arg;
8493
8494 if (packed_parms)
8495 {
8496 /* Once we've packed as many args as we have types, stop. */
8497 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8498 break;
8499 else if (PACK_EXPANSION_P (arg))
8500 /* We don't know how many args we have yet, just
8501 use the unconverted ones for now. */
8502 return NULL_TREE;
8503 else
8504 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8505 }
8506
8507 if (arg == error_mark_node)
8508 {
8509 if (complain & tf_error)
8510 error ("template argument %d is invalid", arg_idx + 1);
8511 }
8512 else
8513 arg = convert_template_argument (actual_parm,
8514 arg, new_args, complain, parm_idx,
8515 in_decl);
8516 if (arg == error_mark_node)
8517 (*lost)++;
8518 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8519 }
8520
8521 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8522 && TREE_VEC_LENGTH (packed_args) > 0)
8523 {
8524 if (complain & tf_error)
8525 error ("wrong number of template arguments (%d, should be %d)",
8526 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8527 return error_mark_node;
8528 }
8529
8530 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8531 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8532 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8533 else
8534 {
8535 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8536 TREE_CONSTANT (argument_pack) = 1;
8537 }
8538
8539 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8540 if (CHECKING_P)
8541 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8542 TREE_VEC_LENGTH (packed_args));
8543 return argument_pack;
8544 }
8545
8546 /* Returns the number of pack expansions in the template argument vector
8547 ARGS. */
8548
8549 static int
8550 pack_expansion_args_count (tree args)
8551 {
8552 int i;
8553 int count = 0;
8554 if (args)
8555 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8556 {
8557 tree elt = TREE_VEC_ELT (args, i);
8558 if (elt && PACK_EXPANSION_P (elt))
8559 ++count;
8560 }
8561 return count;
8562 }
8563
8564 /* Convert all template arguments to their appropriate types, and
8565 return a vector containing the innermost resulting template
8566 arguments. If any error occurs, return error_mark_node. Error and
8567 warning messages are issued under control of COMPLAIN.
8568
8569 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8570 for arguments not specified in ARGS. Otherwise, if
8571 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8572 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8573 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8574 ARGS. */
8575
8576 static tree
8577 coerce_template_parms (tree parms,
8578 tree args,
8579 tree in_decl,
8580 tsubst_flags_t complain,
8581 bool require_all_args,
8582 bool use_default_args)
8583 {
8584 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8585 tree orig_inner_args;
8586 tree inner_args;
8587 tree new_args;
8588 tree new_inner_args;
8589
8590 /* When used as a boolean value, indicates whether this is a
8591 variadic template parameter list. Since it's an int, we can also
8592 subtract it from nparms to get the number of non-variadic
8593 parameters. */
8594 int variadic_p = 0;
8595 int variadic_args_p = 0;
8596 int post_variadic_parms = 0;
8597
8598 /* Adjustment to nparms for fixed parameter packs. */
8599 int fixed_pack_adjust = 0;
8600 int fixed_packs = 0;
8601 int missing = 0;
8602
8603 /* Likewise for parameters with default arguments. */
8604 int default_p = 0;
8605
8606 if (args == error_mark_node)
8607 return error_mark_node;
8608
8609 nparms = TREE_VEC_LENGTH (parms);
8610
8611 /* Determine if there are any parameter packs or default arguments. */
8612 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8613 {
8614 tree parm = TREE_VEC_ELT (parms, parm_idx);
8615 if (variadic_p)
8616 ++post_variadic_parms;
8617 if (template_parameter_pack_p (TREE_VALUE (parm)))
8618 ++variadic_p;
8619 if (TREE_PURPOSE (parm))
8620 ++default_p;
8621 }
8622
8623 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8624 /* If there are no parameters that follow a parameter pack, we need to
8625 expand any argument packs so that we can deduce a parameter pack from
8626 some non-packed args followed by an argument pack, as in variadic85.C.
8627 If there are such parameters, we need to leave argument packs intact
8628 so the arguments are assigned properly. This can happen when dealing
8629 with a nested class inside a partial specialization of a class
8630 template, as in variadic92.C, or when deducing a template parameter pack
8631 from a sub-declarator, as in variadic114.C. */
8632 if (!post_variadic_parms)
8633 inner_args = expand_template_argument_pack (inner_args);
8634
8635 /* Count any pack expansion args. */
8636 variadic_args_p = pack_expansion_args_count (inner_args);
8637
8638 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8639 if ((nargs - variadic_args_p > nparms && !variadic_p)
8640 || (nargs < nparms - variadic_p
8641 && require_all_args
8642 && !variadic_args_p
8643 && (!use_default_args
8644 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8645 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8646 {
8647 bad_nargs:
8648 if (complain & tf_error)
8649 {
8650 if (variadic_p || default_p)
8651 {
8652 nparms -= variadic_p + default_p;
8653 error ("wrong number of template arguments "
8654 "(%d, should be at least %d)", nargs, nparms);
8655 }
8656 else
8657 error ("wrong number of template arguments "
8658 "(%d, should be %d)", nargs, nparms);
8659
8660 if (in_decl)
8661 inform (DECL_SOURCE_LOCATION (in_decl),
8662 "provided for %qD", in_decl);
8663 }
8664
8665 return error_mark_node;
8666 }
8667 /* We can't pass a pack expansion to a non-pack parameter of an alias
8668 template (DR 1430). */
8669 else if (in_decl
8670 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8671 || concept_definition_p (in_decl))
8672 && variadic_args_p
8673 && nargs - variadic_args_p < nparms - variadic_p)
8674 {
8675 if (complain & tf_error)
8676 {
8677 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8678 {
8679 tree arg = TREE_VEC_ELT (inner_args, i);
8680 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8681
8682 if (PACK_EXPANSION_P (arg)
8683 && !template_parameter_pack_p (parm))
8684 {
8685 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8686 error_at (location_of (arg),
8687 "pack expansion argument for non-pack parameter "
8688 "%qD of alias template %qD", parm, in_decl);
8689 else
8690 error_at (location_of (arg),
8691 "pack expansion argument for non-pack parameter "
8692 "%qD of concept %qD", parm, in_decl);
8693 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8694 goto found;
8695 }
8696 }
8697 gcc_unreachable ();
8698 found:;
8699 }
8700 return error_mark_node;
8701 }
8702
8703 /* We need to evaluate the template arguments, even though this
8704 template-id may be nested within a "sizeof". */
8705 cp_evaluated ev;
8706
8707 new_inner_args = make_tree_vec (nparms);
8708 new_args = add_outermost_template_args (args, new_inner_args);
8709 int pack_adjust = 0;
8710 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8711 {
8712 tree arg;
8713 tree parm;
8714
8715 /* Get the Ith template parameter. */
8716 parm = TREE_VEC_ELT (parms, parm_idx);
8717
8718 if (parm == error_mark_node)
8719 {
8720 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8721 continue;
8722 }
8723
8724 /* Calculate the next argument. */
8725 if (arg_idx < nargs)
8726 arg = TREE_VEC_ELT (inner_args, arg_idx);
8727 else
8728 arg = NULL_TREE;
8729
8730 if (template_parameter_pack_p (TREE_VALUE (parm))
8731 && (arg || require_all_args || !(complain & tf_partial))
8732 && !(arg && ARGUMENT_PACK_P (arg)))
8733 {
8734 /* Some arguments will be placed in the
8735 template parameter pack PARM. */
8736 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8737 inner_args, arg_idx,
8738 new_args, &lost,
8739 in_decl, complain);
8740
8741 if (arg == NULL_TREE)
8742 {
8743 /* We don't know how many args we have yet, just use the
8744 unconverted (and still packed) ones for now. */
8745 new_inner_args = orig_inner_args;
8746 arg_idx = nargs;
8747 break;
8748 }
8749
8750 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8751
8752 /* Store this argument. */
8753 if (arg == error_mark_node)
8754 {
8755 lost++;
8756 /* We are done with all of the arguments. */
8757 arg_idx = nargs;
8758 break;
8759 }
8760 else
8761 {
8762 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8763 arg_idx += pack_adjust;
8764 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8765 {
8766 ++fixed_packs;
8767 fixed_pack_adjust += pack_adjust;
8768 }
8769 }
8770
8771 continue;
8772 }
8773 else if (arg)
8774 {
8775 if (PACK_EXPANSION_P (arg))
8776 {
8777 /* "If every valid specialization of a variadic template
8778 requires an empty template parameter pack, the template is
8779 ill-formed, no diagnostic required." So check that the
8780 pattern works with this parameter. */
8781 tree pattern = PACK_EXPANSION_PATTERN (arg);
8782 tree conv = convert_template_argument (TREE_VALUE (parm),
8783 pattern, new_args,
8784 complain, parm_idx,
8785 in_decl);
8786 if (conv == error_mark_node)
8787 {
8788 if (complain & tf_error)
8789 inform (input_location, "so any instantiation with a "
8790 "non-empty parameter pack would be ill-formed");
8791 ++lost;
8792 }
8793 else if (TYPE_P (conv) && !TYPE_P (pattern))
8794 /* Recover from missing typename. */
8795 TREE_VEC_ELT (inner_args, arg_idx)
8796 = make_pack_expansion (conv, complain);
8797
8798 /* We don't know how many args we have yet, just
8799 use the unconverted ones for now. */
8800 new_inner_args = inner_args;
8801 arg_idx = nargs;
8802 break;
8803 }
8804 }
8805 else if (require_all_args)
8806 {
8807 /* There must be a default arg in this case. */
8808 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8809 complain, in_decl);
8810 /* The position of the first default template argument,
8811 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8812 Record that. */
8813 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8815 arg_idx - pack_adjust);
8816 }
8817 else
8818 break;
8819
8820 if (arg == error_mark_node)
8821 {
8822 if (complain & tf_error)
8823 error ("template argument %d is invalid", arg_idx + 1);
8824 }
8825 else if (!arg)
8826 {
8827 /* This can occur if there was an error in the template
8828 parameter list itself (which we would already have
8829 reported) that we are trying to recover from, e.g., a class
8830 template with a parameter list such as
8831 template<typename..., typename> (cpp0x/variadic150.C). */
8832 ++lost;
8833
8834 /* This can also happen with a fixed parameter pack (71834). */
8835 if (arg_idx >= nargs)
8836 ++missing;
8837 }
8838 else
8839 arg = convert_template_argument (TREE_VALUE (parm),
8840 arg, new_args, complain,
8841 parm_idx, in_decl);
8842
8843 if (arg == error_mark_node)
8844 lost++;
8845
8846 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8847 }
8848
8849 if (missing || arg_idx < nargs - variadic_args_p)
8850 {
8851 /* If we had fixed parameter packs, we didn't know how many arguments we
8852 actually needed earlier; now we do. */
8853 nparms += fixed_pack_adjust;
8854 variadic_p -= fixed_packs;
8855 goto bad_nargs;
8856 }
8857
8858 if (arg_idx < nargs)
8859 {
8860 /* We had some pack expansion arguments that will only work if the packs
8861 are empty, but wait until instantiation time to complain.
8862 See variadic-ttp3.C. */
8863
8864 /* Except that we can't provide empty packs to alias templates or
8865 concepts when there are no corresponding parameters. Basically,
8866 we can get here with this:
8867
8868 template<typename T> concept C = true;
8869
8870 template<typename... Args>
8871 requires C<Args...>
8872 void f();
8873
8874 When parsing C<Args...>, we try to form a concept check of
8875 C<?, Args...>. Without the extra check for substituting an empty
8876 pack past the last parameter, we can accept the check as valid.
8877
8878 FIXME: This may be valid for alias templates (but I doubt it).
8879
8880 FIXME: The error could be better also. */
8881 if (in_decl && concept_definition_p (in_decl))
8882 {
8883 if (complain & tf_error)
8884 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8885 "too many arguments");
8886 return error_mark_node;
8887 }
8888
8889 int len = nparms + (nargs - arg_idx);
8890 tree args = make_tree_vec (len);
8891 int i = 0;
8892 for (; i < nparms; ++i)
8893 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8894 for (; i < len; ++i, ++arg_idx)
8895 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8896 arg_idx - pack_adjust);
8897 new_inner_args = args;
8898 }
8899
8900 if (lost)
8901 {
8902 gcc_assert (!(complain & tf_error) || seen_error ());
8903 return error_mark_node;
8904 }
8905
8906 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8907 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8908 TREE_VEC_LENGTH (new_inner_args));
8909
8910 return new_inner_args;
8911 }
8912
8913 /* Convert all template arguments to their appropriate types, and
8914 return a vector containing the innermost resulting template
8915 arguments. If any error occurs, return error_mark_node. Error and
8916 warning messages are not issued.
8917
8918 Note that no function argument deduction is performed, and default
8919 arguments are used to fill in unspecified arguments. */
8920 tree
8921 coerce_template_parms (tree parms, tree args, tree in_decl)
8922 {
8923 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8924 }
8925
8926 /* Convert all template arguments to their appropriate type, and
8927 instantiate default arguments as needed. This returns a vector
8928 containing the innermost resulting template arguments, or
8929 error_mark_node if unsuccessful. */
8930 tree
8931 coerce_template_parms (tree parms, tree args, tree in_decl,
8932 tsubst_flags_t complain)
8933 {
8934 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8935 }
8936
8937 /* Like coerce_template_parms. If PARMS represents all template
8938 parameters levels, this function returns a vector of vectors
8939 representing all the resulting argument levels. Note that in this
8940 case, only the innermost arguments are coerced because the
8941 outermost ones are supposed to have been coerced already.
8942
8943 Otherwise, if PARMS represents only (the innermost) vector of
8944 parameters, this function returns a vector containing just the
8945 innermost resulting arguments. */
8946
8947 static tree
8948 coerce_innermost_template_parms (tree parms,
8949 tree args,
8950 tree in_decl,
8951 tsubst_flags_t complain,
8952 bool require_all_args,
8953 bool use_default_args)
8954 {
8955 int parms_depth = TMPL_PARMS_DEPTH (parms);
8956 int args_depth = TMPL_ARGS_DEPTH (args);
8957 tree coerced_args;
8958
8959 if (parms_depth > 1)
8960 {
8961 coerced_args = make_tree_vec (parms_depth);
8962 tree level;
8963 int cur_depth;
8964
8965 for (level = parms, cur_depth = parms_depth;
8966 parms_depth > 0 && level != NULL_TREE;
8967 level = TREE_CHAIN (level), --cur_depth)
8968 {
8969 tree l;
8970 if (cur_depth == args_depth)
8971 l = coerce_template_parms (TREE_VALUE (level),
8972 args, in_decl, complain,
8973 require_all_args,
8974 use_default_args);
8975 else
8976 l = TMPL_ARGS_LEVEL (args, cur_depth);
8977
8978 if (l == error_mark_node)
8979 return error_mark_node;
8980
8981 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8982 }
8983 }
8984 else
8985 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8986 args, in_decl, complain,
8987 require_all_args,
8988 use_default_args);
8989 return coerced_args;
8990 }
8991
8992 /* Returns true if T is a wrapper to make a C++20 template parameter
8993 object const. */
8994
8995 static bool
8996 class_nttp_const_wrapper_p (tree t)
8997 {
8998 if (cxx_dialect < cxx2a)
8999 return false;
9000 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9001 && CP_TYPE_CONST_P (TREE_TYPE (t))
9002 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9003 }
9004
9005 /* Returns 1 if template args OT and NT are equivalent. */
9006
9007 int
9008 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9009 {
9010 if (nt == ot)
9011 return 1;
9012 if (nt == NULL_TREE || ot == NULL_TREE)
9013 return false;
9014 if (nt == any_targ_node || ot == any_targ_node)
9015 return true;
9016
9017 if (class_nttp_const_wrapper_p (nt))
9018 nt = TREE_OPERAND (nt, 0);
9019 if (class_nttp_const_wrapper_p (ot))
9020 ot = TREE_OPERAND (ot, 0);
9021
9022 if (TREE_CODE (nt) == TREE_VEC)
9023 /* For member templates */
9024 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
9025 else if (PACK_EXPANSION_P (ot))
9026 return (PACK_EXPANSION_P (nt)
9027 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9028 PACK_EXPANSION_PATTERN (nt))
9029 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9030 PACK_EXPANSION_EXTRA_ARGS (nt)));
9031 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9032 return cp_tree_equal (ot, nt);
9033 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9034 gcc_unreachable ();
9035 else if (TYPE_P (nt))
9036 {
9037 if (!TYPE_P (ot))
9038 return false;
9039 /* Don't treat an alias template specialization with dependent
9040 arguments as equivalent to its underlying type when used as a
9041 template argument; we need them to be distinct so that we
9042 substitute into the specialization arguments at instantiation
9043 time. And aliases can't be equivalent without being ==, so
9044 we don't need to look any deeper.
9045
9046 During partial ordering, however, we need to treat them normally so
9047 that we can order uses of the same alias with different
9048 cv-qualification (79960). */
9049 if (!partial_order
9050 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9051 return false;
9052 else
9053 return same_type_p (ot, nt);
9054 }
9055 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
9056 return 0;
9057 else
9058 {
9059 /* Try to treat a template non-type argument that has been converted
9060 to the parameter type as equivalent to one that hasn't yet. */
9061 for (enum tree_code code1 = TREE_CODE (ot);
9062 CONVERT_EXPR_CODE_P (code1)
9063 || code1 == NON_LVALUE_EXPR;
9064 code1 = TREE_CODE (ot))
9065 ot = TREE_OPERAND (ot, 0);
9066 for (enum tree_code code2 = TREE_CODE (nt);
9067 CONVERT_EXPR_CODE_P (code2)
9068 || code2 == NON_LVALUE_EXPR;
9069 code2 = TREE_CODE (nt))
9070 nt = TREE_OPERAND (nt, 0);
9071
9072 return cp_tree_equal (ot, nt);
9073 }
9074 }
9075
9076 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9077 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9078 NEWARG_PTR with the offending arguments if they are non-NULL. */
9079
9080 int
9081 comp_template_args (tree oldargs, tree newargs,
9082 tree *oldarg_ptr, tree *newarg_ptr,
9083 bool partial_order)
9084 {
9085 int i;
9086
9087 if (oldargs == newargs)
9088 return 1;
9089
9090 if (!oldargs || !newargs)
9091 return 0;
9092
9093 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9094 return 0;
9095
9096 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9097 {
9098 tree nt = TREE_VEC_ELT (newargs, i);
9099 tree ot = TREE_VEC_ELT (oldargs, i);
9100
9101 if (! template_args_equal (ot, nt, partial_order))
9102 {
9103 if (oldarg_ptr != NULL)
9104 *oldarg_ptr = ot;
9105 if (newarg_ptr != NULL)
9106 *newarg_ptr = nt;
9107 return 0;
9108 }
9109 }
9110 return 1;
9111 }
9112
9113 inline bool
9114 comp_template_args_porder (tree oargs, tree nargs)
9115 {
9116 return comp_template_args (oargs, nargs, NULL, NULL, true);
9117 }
9118
9119 /* Implement a freelist interface for objects of type T.
9120
9121 Head is a separate object, rather than a regular member, so that we
9122 can define it as a GTY deletable pointer, which is highly
9123 desirable. A data member could be declared that way, but then the
9124 containing object would implicitly get GTY((user)), which would
9125 prevent us from instantiating freelists as global objects.
9126 Although this way we can create freelist global objects, they're
9127 such thin wrappers that instantiating temporaries at every use
9128 loses nothing and saves permanent storage for the freelist object.
9129
9130 Member functions next, anew, poison and reinit have default
9131 implementations that work for most of the types we're interested
9132 in, but if they don't work for some type, they should be explicitly
9133 specialized. See the comments before them for requirements, and
9134 the example specializations for the tree_list_freelist. */
9135 template <typename T>
9136 class freelist
9137 {
9138 /* Return the next object in a chain. We could just do type
9139 punning, but if we access the object with its underlying type, we
9140 avoid strict-aliasing trouble. This needs only work between
9141 poison and reinit. */
9142 static T *&next (T *obj) { return obj->next; }
9143
9144 /* Return a newly allocated, uninitialized or minimally-initialized
9145 object of type T. Any initialization performed by anew should
9146 either remain across the life of the object and the execution of
9147 poison, or be redone by reinit. */
9148 static T *anew () { return ggc_alloc<T> (); }
9149
9150 /* Optionally scribble all over the bits holding the object, so that
9151 they become (mostly?) uninitialized memory. This is called while
9152 preparing to make the object part of the free list. */
9153 static void poison (T *obj) {
9154 T *p ATTRIBUTE_UNUSED = obj;
9155 T **q ATTRIBUTE_UNUSED = &next (obj);
9156
9157 #ifdef ENABLE_GC_CHECKING
9158 /* Poison the data, to indicate the data is garbage. */
9159 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9160 memset (p, 0xa5, sizeof (*p));
9161 #endif
9162 /* Let valgrind know the object is free. */
9163 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9164
9165 /* Let valgrind know the next portion of the object is available,
9166 but uninitialized. */
9167 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9168 }
9169
9170 /* Bring an object that underwent at least one lifecycle after anew
9171 and before the most recent free and poison, back to a usable
9172 state, reinitializing whatever is needed for it to be
9173 functionally equivalent to an object just allocated and returned
9174 by anew. This may poison or clear the next field, used by
9175 freelist housekeeping after poison was called. */
9176 static void reinit (T *obj) {
9177 T **q ATTRIBUTE_UNUSED = &next (obj);
9178
9179 #ifdef ENABLE_GC_CHECKING
9180 memset (q, 0xa5, sizeof (*q));
9181 #endif
9182 /* Let valgrind know the entire object is available, but
9183 uninitialized. */
9184 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9185 }
9186
9187 /* Reference a GTY-deletable pointer that points to the first object
9188 in the free list proper. */
9189 T *&head;
9190 public:
9191 /* Construct a freelist object chaining objects off of HEAD. */
9192 freelist (T *&head) : head(head) {}
9193
9194 /* Add OBJ to the free object list. The former head becomes OBJ's
9195 successor. */
9196 void free (T *obj)
9197 {
9198 poison (obj);
9199 next (obj) = head;
9200 head = obj;
9201 }
9202
9203 /* Take an object from the free list, if one is available, or
9204 allocate a new one. Objects taken from the free list should be
9205 regarded as filled with garbage, except for bits that are
9206 configured to be preserved across free and alloc. */
9207 T *alloc ()
9208 {
9209 if (head)
9210 {
9211 T *obj = head;
9212 head = next (head);
9213 reinit (obj);
9214 return obj;
9215 }
9216 else
9217 return anew ();
9218 }
9219 };
9220
9221 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9222 want to allocate a TREE_LIST using the usual interface, and ensure
9223 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9224 build_tree_list logic in reinit, so this could go out of sync. */
9225 template <>
9226 inline tree &
9227 freelist<tree_node>::next (tree obj)
9228 {
9229 return TREE_CHAIN (obj);
9230 }
9231 template <>
9232 inline tree
9233 freelist<tree_node>::anew ()
9234 {
9235 return build_tree_list (NULL, NULL);
9236 }
9237 template <>
9238 inline void
9239 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9240 {
9241 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9242 tree p ATTRIBUTE_UNUSED = obj;
9243 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9244 tree *q ATTRIBUTE_UNUSED = &next (obj);
9245
9246 #ifdef ENABLE_GC_CHECKING
9247 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9248
9249 /* Poison the data, to indicate the data is garbage. */
9250 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9251 memset (p, 0xa5, size);
9252 #endif
9253 /* Let valgrind know the object is free. */
9254 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9255 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9256 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9257 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9258
9259 #ifdef ENABLE_GC_CHECKING
9260 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9261 /* Keep TREE_CHAIN functional. */
9262 TREE_SET_CODE (obj, TREE_LIST);
9263 #else
9264 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9265 #endif
9266 }
9267 template <>
9268 inline void
9269 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9270 {
9271 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9272
9273 #ifdef ENABLE_GC_CHECKING
9274 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9276 memset (obj, 0, sizeof (tree_list));
9277 #endif
9278
9279 /* Let valgrind know the entire object is available, but
9280 uninitialized. */
9281 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9282
9283 #ifdef ENABLE_GC_CHECKING
9284 TREE_SET_CODE (obj, TREE_LIST);
9285 #else
9286 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9287 #endif
9288 }
9289
9290 /* Point to the first object in the TREE_LIST freelist. */
9291 static GTY((deletable)) tree tree_list_freelist_head;
9292 /* Return the/an actual TREE_LIST freelist. */
9293 static inline freelist<tree_node>
9294 tree_list_freelist ()
9295 {
9296 return tree_list_freelist_head;
9297 }
9298
9299 /* Point to the first object in the tinst_level freelist. */
9300 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9301 /* Return the/an actual tinst_level freelist. */
9302 static inline freelist<tinst_level>
9303 tinst_level_freelist ()
9304 {
9305 return tinst_level_freelist_head;
9306 }
9307
9308 /* Point to the first object in the pending_template freelist. */
9309 static GTY((deletable)) pending_template *pending_template_freelist_head;
9310 /* Return the/an actual pending_template freelist. */
9311 static inline freelist<pending_template>
9312 pending_template_freelist ()
9313 {
9314 return pending_template_freelist_head;
9315 }
9316
9317 /* Build the TREE_LIST object out of a split list, store it
9318 permanently, and return it. */
9319 tree
9320 tinst_level::to_list ()
9321 {
9322 gcc_assert (split_list_p ());
9323 tree ret = tree_list_freelist ().alloc ();
9324 TREE_PURPOSE (ret) = tldcl;
9325 TREE_VALUE (ret) = targs;
9326 tldcl = ret;
9327 targs = NULL;
9328 gcc_assert (tree_list_p ());
9329 return ret;
9330 }
9331
9332 const unsigned short tinst_level::refcount_infinity;
9333
9334 /* Increment OBJ's refcount unless it is already infinite. */
9335 static tinst_level *
9336 inc_refcount_use (tinst_level *obj)
9337 {
9338 if (obj && obj->refcount != tinst_level::refcount_infinity)
9339 ++obj->refcount;
9340 return obj;
9341 }
9342
9343 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9344 void
9345 tinst_level::free (tinst_level *obj)
9346 {
9347 if (obj->tree_list_p ())
9348 tree_list_freelist ().free (obj->get_node ());
9349 tinst_level_freelist ().free (obj);
9350 }
9351
9352 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9353 OBJ's DECL and OBJ, and start over with the tinst_level object that
9354 used to be referenced by OBJ's NEXT. */
9355 static void
9356 dec_refcount_use (tinst_level *obj)
9357 {
9358 while (obj
9359 && obj->refcount != tinst_level::refcount_infinity
9360 && !--obj->refcount)
9361 {
9362 tinst_level *next = obj->next;
9363 tinst_level::free (obj);
9364 obj = next;
9365 }
9366 }
9367
9368 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9369 and of the former PTR. Omitting the second argument is equivalent
9370 to passing (T*)NULL; this is allowed because passing the
9371 zero-valued integral constant NULL confuses type deduction and/or
9372 overload resolution. */
9373 template <typename T>
9374 static void
9375 set_refcount_ptr (T *& ptr, T *obj = NULL)
9376 {
9377 T *save = ptr;
9378 ptr = inc_refcount_use (obj);
9379 dec_refcount_use (save);
9380 }
9381
9382 static void
9383 add_pending_template (tree d)
9384 {
9385 tree ti = (TYPE_P (d)
9386 ? CLASSTYPE_TEMPLATE_INFO (d)
9387 : DECL_TEMPLATE_INFO (d));
9388 struct pending_template *pt;
9389 int level;
9390
9391 if (TI_PENDING_TEMPLATE_FLAG (ti))
9392 return;
9393
9394 /* We are called both from instantiate_decl, where we've already had a
9395 tinst_level pushed, and instantiate_template, where we haven't.
9396 Compensate. */
9397 gcc_assert (TREE_CODE (d) != TREE_LIST);
9398 level = !current_tinst_level
9399 || current_tinst_level->maybe_get_node () != d;
9400
9401 if (level)
9402 push_tinst_level (d);
9403
9404 pt = pending_template_freelist ().alloc ();
9405 pt->next = NULL;
9406 pt->tinst = NULL;
9407 set_refcount_ptr (pt->tinst, current_tinst_level);
9408 if (last_pending_template)
9409 last_pending_template->next = pt;
9410 else
9411 pending_templates = pt;
9412
9413 last_pending_template = pt;
9414
9415 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9416
9417 if (level)
9418 pop_tinst_level ();
9419 }
9420
9421
9422 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9423 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9424 documentation for TEMPLATE_ID_EXPR. */
9425
9426 tree
9427 lookup_template_function (tree fns, tree arglist)
9428 {
9429 if (fns == error_mark_node || arglist == error_mark_node)
9430 return error_mark_node;
9431
9432 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9433
9434 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9435 {
9436 error ("%q#D is not a function template", fns);
9437 return error_mark_node;
9438 }
9439
9440 if (BASELINK_P (fns))
9441 {
9442 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9443 unknown_type_node,
9444 BASELINK_FUNCTIONS (fns),
9445 arglist);
9446 return fns;
9447 }
9448
9449 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9450 }
9451
9452 /* Within the scope of a template class S<T>, the name S gets bound
9453 (in build_self_reference) to a TYPE_DECL for the class, not a
9454 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9455 or one of its enclosing classes, and that type is a template,
9456 return the associated TEMPLATE_DECL. Otherwise, the original
9457 DECL is returned.
9458
9459 Also handle the case when DECL is a TREE_LIST of ambiguous
9460 injected-class-names from different bases. */
9461
9462 tree
9463 maybe_get_template_decl_from_type_decl (tree decl)
9464 {
9465 if (decl == NULL_TREE)
9466 return decl;
9467
9468 /* DR 176: A lookup that finds an injected-class-name (10.2
9469 [class.member.lookup]) can result in an ambiguity in certain cases
9470 (for example, if it is found in more than one base class). If all of
9471 the injected-class-names that are found refer to specializations of
9472 the same class template, and if the name is followed by a
9473 template-argument-list, the reference refers to the class template
9474 itself and not a specialization thereof, and is not ambiguous. */
9475 if (TREE_CODE (decl) == TREE_LIST)
9476 {
9477 tree t, tmpl = NULL_TREE;
9478 for (t = decl; t; t = TREE_CHAIN (t))
9479 {
9480 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9481 if (!tmpl)
9482 tmpl = elt;
9483 else if (tmpl != elt)
9484 break;
9485 }
9486 if (tmpl && t == NULL_TREE)
9487 return tmpl;
9488 else
9489 return decl;
9490 }
9491
9492 return (decl != NULL_TREE
9493 && DECL_SELF_REFERENCE_P (decl)
9494 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9495 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9496 }
9497
9498 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9499 parameters, find the desired type.
9500
9501 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9502
9503 IN_DECL, if non-NULL, is the template declaration we are trying to
9504 instantiate.
9505
9506 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9507 the class we are looking up.
9508
9509 Issue error and warning messages under control of COMPLAIN.
9510
9511 If the template class is really a local class in a template
9512 function, then the FUNCTION_CONTEXT is the function in which it is
9513 being instantiated.
9514
9515 ??? Note that this function is currently called *twice* for each
9516 template-id: the first time from the parser, while creating the
9517 incomplete type (finish_template_type), and the second type during the
9518 real instantiation (instantiate_template_class). This is surely something
9519 that we want to avoid. It also causes some problems with argument
9520 coercion (see convert_nontype_argument for more information on this). */
9521
9522 static tree
9523 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9524 int entering_scope, tsubst_flags_t complain)
9525 {
9526 tree templ = NULL_TREE, parmlist;
9527 tree t;
9528 spec_entry **slot;
9529 spec_entry *entry;
9530 spec_entry elt;
9531 hashval_t hash;
9532
9533 if (identifier_p (d1))
9534 {
9535 tree value = innermost_non_namespace_value (d1);
9536 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9537 templ = value;
9538 else
9539 {
9540 if (context)
9541 push_decl_namespace (context);
9542 templ = lookup_name (d1);
9543 templ = maybe_get_template_decl_from_type_decl (templ);
9544 if (context)
9545 pop_decl_namespace ();
9546 }
9547 if (templ)
9548 context = DECL_CONTEXT (templ);
9549 }
9550 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9551 {
9552 tree type = TREE_TYPE (d1);
9553
9554 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9555 an implicit typename for the second A. Deal with it. */
9556 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9557 type = TREE_TYPE (type);
9558
9559 if (CLASSTYPE_TEMPLATE_INFO (type))
9560 {
9561 templ = CLASSTYPE_TI_TEMPLATE (type);
9562 d1 = DECL_NAME (templ);
9563 }
9564 }
9565 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9566 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9567 {
9568 templ = TYPE_TI_TEMPLATE (d1);
9569 d1 = DECL_NAME (templ);
9570 }
9571 else if (DECL_TYPE_TEMPLATE_P (d1))
9572 {
9573 templ = d1;
9574 d1 = DECL_NAME (templ);
9575 context = DECL_CONTEXT (templ);
9576 }
9577 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9578 {
9579 templ = d1;
9580 d1 = DECL_NAME (templ);
9581 }
9582
9583 /* Issue an error message if we didn't find a template. */
9584 if (! templ)
9585 {
9586 if (complain & tf_error)
9587 error ("%qT is not a template", d1);
9588 return error_mark_node;
9589 }
9590
9591 if (TREE_CODE (templ) != TEMPLATE_DECL
9592 /* Make sure it's a user visible template, if it was named by
9593 the user. */
9594 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9595 && !PRIMARY_TEMPLATE_P (templ)))
9596 {
9597 if (complain & tf_error)
9598 {
9599 error ("non-template type %qT used as a template", d1);
9600 if (in_decl)
9601 error ("for template declaration %q+D", in_decl);
9602 }
9603 return error_mark_node;
9604 }
9605
9606 complain &= ~tf_user;
9607
9608 /* An alias that just changes the name of a template is equivalent to the
9609 other template, so if any of the arguments are pack expansions, strip
9610 the alias to avoid problems with a pack expansion passed to a non-pack
9611 alias template parameter (DR 1430). */
9612 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9613 templ = get_underlying_template (templ);
9614
9615 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9616 {
9617 tree parm;
9618 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9619 if (arglist2 == error_mark_node
9620 || (!uses_template_parms (arglist2)
9621 && check_instantiated_args (templ, arglist2, complain)))
9622 return error_mark_node;
9623
9624 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9625 return parm;
9626 }
9627 else
9628 {
9629 tree template_type = TREE_TYPE (templ);
9630 tree gen_tmpl;
9631 tree type_decl;
9632 tree found = NULL_TREE;
9633 int arg_depth;
9634 int parm_depth;
9635 int is_dependent_type;
9636 int use_partial_inst_tmpl = false;
9637
9638 if (template_type == error_mark_node)
9639 /* An error occurred while building the template TEMPL, and a
9640 diagnostic has most certainly been emitted for that
9641 already. Let's propagate that error. */
9642 return error_mark_node;
9643
9644 gen_tmpl = most_general_template (templ);
9645 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9646 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9647 arg_depth = TMPL_ARGS_DEPTH (arglist);
9648
9649 if (arg_depth == 1 && parm_depth > 1)
9650 {
9651 /* We've been given an incomplete set of template arguments.
9652 For example, given:
9653
9654 template <class T> struct S1 {
9655 template <class U> struct S2 {};
9656 template <class U> struct S2<U*> {};
9657 };
9658
9659 we will be called with an ARGLIST of `U*', but the
9660 TEMPLATE will be `template <class T> template
9661 <class U> struct S1<T>::S2'. We must fill in the missing
9662 arguments. */
9663 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9664 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9665 arg_depth = TMPL_ARGS_DEPTH (arglist);
9666 }
9667
9668 /* Now we should have enough arguments. */
9669 gcc_assert (parm_depth == arg_depth);
9670
9671 /* From here on, we're only interested in the most general
9672 template. */
9673
9674 /* Calculate the BOUND_ARGS. These will be the args that are
9675 actually tsubst'd into the definition to create the
9676 instantiation. */
9677 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9678 complain,
9679 /*require_all_args=*/true,
9680 /*use_default_args=*/true);
9681
9682 if (arglist == error_mark_node)
9683 /* We were unable to bind the arguments. */
9684 return error_mark_node;
9685
9686 /* In the scope of a template class, explicit references to the
9687 template class refer to the type of the template, not any
9688 instantiation of it. For example, in:
9689
9690 template <class T> class C { void f(C<T>); }
9691
9692 the `C<T>' is just the same as `C'. Outside of the
9693 class, however, such a reference is an instantiation. */
9694 if (entering_scope
9695 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9696 || currently_open_class (template_type))
9697 {
9698 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9699
9700 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9701 return template_type;
9702 }
9703
9704 /* If we already have this specialization, return it. */
9705 elt.tmpl = gen_tmpl;
9706 elt.args = arglist;
9707 elt.spec = NULL_TREE;
9708 hash = spec_hasher::hash (&elt);
9709 entry = type_specializations->find_with_hash (&elt, hash);
9710
9711 if (entry)
9712 return entry->spec;
9713
9714 /* If the template's constraints are not satisfied,
9715 then we cannot form a valid type.
9716
9717 Note that the check is deferred until after the hash
9718 lookup. This prevents redundant checks on previously
9719 instantiated specializations. */
9720 if (flag_concepts
9721 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9722 && !constraints_satisfied_p (gen_tmpl, arglist))
9723 {
9724 if (complain & tf_error)
9725 {
9726 auto_diagnostic_group d;
9727 error ("template constraint failure for %qD", gen_tmpl);
9728 diagnose_constraints (input_location, gen_tmpl, arglist);
9729 }
9730 return error_mark_node;
9731 }
9732
9733 is_dependent_type = uses_template_parms (arglist);
9734
9735 /* If the deduced arguments are invalid, then the binding
9736 failed. */
9737 if (!is_dependent_type
9738 && check_instantiated_args (gen_tmpl,
9739 INNERMOST_TEMPLATE_ARGS (arglist),
9740 complain))
9741 return error_mark_node;
9742
9743 if (!is_dependent_type
9744 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9745 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9746 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9747 {
9748 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9749 DECL_NAME (gen_tmpl),
9750 /*tag_scope=*/ts_global);
9751 return found;
9752 }
9753
9754 context = DECL_CONTEXT (gen_tmpl);
9755 if (context && TYPE_P (context))
9756 {
9757 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9758 context = complete_type (context);
9759 }
9760 else
9761 context = tsubst (context, arglist, complain, in_decl);
9762
9763 if (context == error_mark_node)
9764 return error_mark_node;
9765
9766 if (!context)
9767 context = global_namespace;
9768
9769 /* Create the type. */
9770 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9771 {
9772 /* The user referred to a specialization of an alias
9773 template represented by GEN_TMPL.
9774
9775 [temp.alias]/2 says:
9776
9777 When a template-id refers to the specialization of an
9778 alias template, it is equivalent to the associated
9779 type obtained by substitution of its
9780 template-arguments for the template-parameters in the
9781 type-id of the alias template. */
9782
9783 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9784 /* Note that the call above (by indirectly calling
9785 register_specialization in tsubst_decl) registers the
9786 TYPE_DECL representing the specialization of the alias
9787 template. So next time someone substitutes ARGLIST for
9788 the template parms into the alias template (GEN_TMPL),
9789 she'll get that TYPE_DECL back. */
9790
9791 if (t == error_mark_node)
9792 return t;
9793 }
9794 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9795 {
9796 if (!is_dependent_type)
9797 {
9798 set_current_access_from_decl (TYPE_NAME (template_type));
9799 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9800 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9801 arglist, complain, in_decl),
9802 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9803 arglist, complain, in_decl),
9804 SCOPED_ENUM_P (template_type), NULL);
9805
9806 if (t == error_mark_node)
9807 return t;
9808 }
9809 else
9810 {
9811 /* We don't want to call start_enum for this type, since
9812 the values for the enumeration constants may involve
9813 template parameters. And, no one should be interested
9814 in the enumeration constants for such a type. */
9815 t = cxx_make_type (ENUMERAL_TYPE);
9816 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9817 }
9818 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9819 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9820 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9821 }
9822 else if (CLASS_TYPE_P (template_type))
9823 {
9824 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9825 instantiated here. */
9826 gcc_assert (!LAMBDA_TYPE_P (template_type));
9827
9828 t = make_class_type (TREE_CODE (template_type));
9829 CLASSTYPE_DECLARED_CLASS (t)
9830 = CLASSTYPE_DECLARED_CLASS (template_type);
9831 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9832
9833 /* A local class. Make sure the decl gets registered properly. */
9834 if (context == current_function_decl)
9835 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9836 == error_mark_node)
9837 return error_mark_node;
9838
9839 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9840 /* This instantiation is another name for the primary
9841 template type. Set the TYPE_CANONICAL field
9842 appropriately. */
9843 TYPE_CANONICAL (t) = template_type;
9844 else if (any_template_arguments_need_structural_equality_p (arglist))
9845 /* Some of the template arguments require structural
9846 equality testing, so this template class requires
9847 structural equality testing. */
9848 SET_TYPE_STRUCTURAL_EQUALITY (t);
9849 }
9850 else
9851 gcc_unreachable ();
9852
9853 /* If we called start_enum or pushtag above, this information
9854 will already be set up. */
9855 if (!TYPE_NAME (t))
9856 {
9857 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9858
9859 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9860 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9861 DECL_SOURCE_LOCATION (type_decl)
9862 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9863 }
9864 else
9865 type_decl = TYPE_NAME (t);
9866
9867 if (CLASS_TYPE_P (template_type))
9868 {
9869 TREE_PRIVATE (type_decl)
9870 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9871 TREE_PROTECTED (type_decl)
9872 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9873 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9874 {
9875 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9876 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9877 }
9878 }
9879
9880 if (OVERLOAD_TYPE_P (t)
9881 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9882 {
9883 static const char *tags[] = {"abi_tag", "may_alias"};
9884
9885 for (unsigned ix = 0; ix != 2; ix++)
9886 {
9887 tree attributes
9888 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9889
9890 if (attributes)
9891 TYPE_ATTRIBUTES (t)
9892 = tree_cons (TREE_PURPOSE (attributes),
9893 TREE_VALUE (attributes),
9894 TYPE_ATTRIBUTES (t));
9895 }
9896 }
9897
9898 /* Let's consider the explicit specialization of a member
9899 of a class template specialization that is implicitly instantiated,
9900 e.g.:
9901 template<class T>
9902 struct S
9903 {
9904 template<class U> struct M {}; //#0
9905 };
9906
9907 template<>
9908 template<>
9909 struct S<int>::M<char> //#1
9910 {
9911 int i;
9912 };
9913 [temp.expl.spec]/4 says this is valid.
9914
9915 In this case, when we write:
9916 S<int>::M<char> m;
9917
9918 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9919 the one of #0.
9920
9921 When we encounter #1, we want to store the partial instantiation
9922 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9923
9924 For all cases other than this "explicit specialization of member of a
9925 class template", we just want to store the most general template into
9926 the CLASSTYPE_TI_TEMPLATE of M.
9927
9928 This case of "explicit specialization of member of a class template"
9929 only happens when:
9930 1/ the enclosing class is an instantiation of, and therefore not
9931 the same as, the context of the most general template, and
9932 2/ we aren't looking at the partial instantiation itself, i.e.
9933 the innermost arguments are not the same as the innermost parms of
9934 the most general template.
9935
9936 So it's only when 1/ and 2/ happens that we want to use the partial
9937 instantiation of the member template in lieu of its most general
9938 template. */
9939
9940 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9941 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9942 /* the enclosing class must be an instantiation... */
9943 && CLASS_TYPE_P (context)
9944 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9945 {
9946 TREE_VEC_LENGTH (arglist)--;
9947 ++processing_template_decl;
9948 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9949 tree partial_inst_args =
9950 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9951 arglist, complain, NULL_TREE);
9952 --processing_template_decl;
9953 TREE_VEC_LENGTH (arglist)++;
9954 if (partial_inst_args == error_mark_node)
9955 return error_mark_node;
9956 use_partial_inst_tmpl =
9957 /*...and we must not be looking at the partial instantiation
9958 itself. */
9959 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9960 partial_inst_args);
9961 }
9962
9963 if (!use_partial_inst_tmpl)
9964 /* This case is easy; there are no member templates involved. */
9965 found = gen_tmpl;
9966 else
9967 {
9968 /* This is a full instantiation of a member template. Find
9969 the partial instantiation of which this is an instance. */
9970
9971 /* Temporarily reduce by one the number of levels in the ARGLIST
9972 so as to avoid comparing the last set of arguments. */
9973 TREE_VEC_LENGTH (arglist)--;
9974 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9975 TREE_VEC_LENGTH (arglist)++;
9976 /* FOUND is either a proper class type, or an alias
9977 template specialization. In the later case, it's a
9978 TYPE_DECL, resulting from the substituting of arguments
9979 for parameters in the TYPE_DECL of the alias template
9980 done earlier. So be careful while getting the template
9981 of FOUND. */
9982 found = (TREE_CODE (found) == TEMPLATE_DECL
9983 ? found
9984 : (TREE_CODE (found) == TYPE_DECL
9985 ? DECL_TI_TEMPLATE (found)
9986 : CLASSTYPE_TI_TEMPLATE (found)));
9987
9988 if (DECL_CLASS_TEMPLATE_P (found)
9989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9990 {
9991 /* If this partial instantiation is specialized, we want to
9992 use it for hash table lookup. */
9993 elt.tmpl = found;
9994 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9995 hash = spec_hasher::hash (&elt);
9996 }
9997 }
9998
9999 // Build template info for the new specialization.
10000 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10001
10002 elt.spec = t;
10003 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10004 gcc_checking_assert (*slot == NULL);
10005 entry = ggc_alloc<spec_entry> ();
10006 *entry = elt;
10007 *slot = entry;
10008
10009 /* Note this use of the partial instantiation so we can check it
10010 later in maybe_process_partial_specialization. */
10011 DECL_TEMPLATE_INSTANTIATIONS (found)
10012 = tree_cons (arglist, t,
10013 DECL_TEMPLATE_INSTANTIATIONS (found));
10014
10015 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10016 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10017 /* Now that the type has been registered on the instantiations
10018 list, we set up the enumerators. Because the enumeration
10019 constants may involve the enumeration type itself, we make
10020 sure to register the type first, and then create the
10021 constants. That way, doing tsubst_expr for the enumeration
10022 constants won't result in recursive calls here; we'll find
10023 the instantiation and exit above. */
10024 tsubst_enum (template_type, t, arglist);
10025
10026 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10027 /* If the type makes use of template parameters, the
10028 code that generates debugging information will crash. */
10029 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10030
10031 /* Possibly limit visibility based on template args. */
10032 TREE_PUBLIC (type_decl) = 1;
10033 determine_visibility (type_decl);
10034
10035 inherit_targ_abi_tags (t);
10036
10037 return t;
10038 }
10039 }
10040
10041 /* Wrapper for lookup_template_class_1. */
10042
10043 tree
10044 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10045 int entering_scope, tsubst_flags_t complain)
10046 {
10047 tree ret;
10048 timevar_push (TV_TEMPLATE_INST);
10049 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10050 entering_scope, complain);
10051 timevar_pop (TV_TEMPLATE_INST);
10052 return ret;
10053 }
10054
10055 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10056
10057 tree
10058 lookup_template_variable (tree templ, tree arglist)
10059 {
10060 if (flag_concepts && variable_concept_p (templ))
10061 return build_concept_check (templ, arglist, tf_none);
10062
10063 /* The type of the expression is NULL_TREE since the template-id could refer
10064 to an explicit or partial specialization. */
10065 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10066 }
10067
10068 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10069
10070 tree
10071 finish_template_variable (tree var, tsubst_flags_t complain)
10072 {
10073 tree templ = TREE_OPERAND (var, 0);
10074 tree arglist = TREE_OPERAND (var, 1);
10075
10076 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10077 arglist = add_outermost_template_args (tmpl_args, arglist);
10078
10079 templ = most_general_template (templ);
10080 tree parms = DECL_TEMPLATE_PARMS (templ);
10081 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10082 /*req_all*/true,
10083 /*use_default*/true);
10084
10085 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10086 {
10087 if (complain & tf_error)
10088 {
10089 auto_diagnostic_group d;
10090 error ("use of invalid variable template %qE", var);
10091 diagnose_constraints (location_of (var), templ, arglist);
10092 }
10093 return error_mark_node;
10094 }
10095
10096 return instantiate_template (templ, arglist, complain);
10097 }
10098
10099 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10100 TARGS template args, and instantiate it if it's not dependent. */
10101
10102 tree
10103 lookup_and_finish_template_variable (tree templ, tree targs,
10104 tsubst_flags_t complain)
10105 {
10106 templ = lookup_template_variable (templ, targs);
10107 if (!any_dependent_template_arguments_p (targs))
10108 {
10109 templ = finish_template_variable (templ, complain);
10110 mark_used (templ);
10111 }
10112
10113 return convert_from_reference (templ);
10114 }
10115
10116 \f
10117 struct pair_fn_data
10118 {
10119 tree_fn_t fn;
10120 tree_fn_t any_fn;
10121 void *data;
10122 /* True when we should also visit template parameters that occur in
10123 non-deduced contexts. */
10124 bool include_nondeduced_p;
10125 hash_set<tree> *visited;
10126 };
10127
10128 /* Called from for_each_template_parm via walk_tree. */
10129
10130 static tree
10131 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10132 {
10133 tree t = *tp;
10134 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10135 tree_fn_t fn = pfd->fn;
10136 void *data = pfd->data;
10137 tree result = NULL_TREE;
10138
10139 #define WALK_SUBTREE(NODE) \
10140 do \
10141 { \
10142 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10143 pfd->include_nondeduced_p, \
10144 pfd->any_fn); \
10145 if (result) goto out; \
10146 } \
10147 while (0)
10148
10149 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10150 return t;
10151
10152 if (TYPE_P (t)
10153 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10154 WALK_SUBTREE (TYPE_CONTEXT (t));
10155
10156 switch (TREE_CODE (t))
10157 {
10158 case RECORD_TYPE:
10159 if (TYPE_PTRMEMFUNC_P (t))
10160 break;
10161 /* Fall through. */
10162
10163 case UNION_TYPE:
10164 case ENUMERAL_TYPE:
10165 if (!TYPE_TEMPLATE_INFO (t))
10166 *walk_subtrees = 0;
10167 else
10168 WALK_SUBTREE (TYPE_TI_ARGS (t));
10169 break;
10170
10171 case INTEGER_TYPE:
10172 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10173 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10174 break;
10175
10176 case METHOD_TYPE:
10177 /* Since we're not going to walk subtrees, we have to do this
10178 explicitly here. */
10179 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10180 /* Fall through. */
10181
10182 case FUNCTION_TYPE:
10183 /* Check the return type. */
10184 WALK_SUBTREE (TREE_TYPE (t));
10185
10186 /* Check the parameter types. Since default arguments are not
10187 instantiated until they are needed, the TYPE_ARG_TYPES may
10188 contain expressions that involve template parameters. But,
10189 no-one should be looking at them yet. And, once they're
10190 instantiated, they don't contain template parameters, so
10191 there's no point in looking at them then, either. */
10192 {
10193 tree parm;
10194
10195 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10196 WALK_SUBTREE (TREE_VALUE (parm));
10197
10198 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10199 want walk_tree walking into them itself. */
10200 *walk_subtrees = 0;
10201 }
10202
10203 if (flag_noexcept_type)
10204 {
10205 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10206 if (spec)
10207 WALK_SUBTREE (TREE_PURPOSE (spec));
10208 }
10209 break;
10210
10211 case TYPEOF_TYPE:
10212 case DECLTYPE_TYPE:
10213 case UNDERLYING_TYPE:
10214 if (pfd->include_nondeduced_p
10215 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10216 pfd->visited,
10217 pfd->include_nondeduced_p,
10218 pfd->any_fn))
10219 return error_mark_node;
10220 *walk_subtrees = false;
10221 break;
10222
10223 case FUNCTION_DECL:
10224 case VAR_DECL:
10225 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10226 WALK_SUBTREE (DECL_TI_ARGS (t));
10227 /* Fall through. */
10228
10229 case PARM_DECL:
10230 case CONST_DECL:
10231 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10232 WALK_SUBTREE (DECL_INITIAL (t));
10233 if (DECL_CONTEXT (t)
10234 && pfd->include_nondeduced_p)
10235 WALK_SUBTREE (DECL_CONTEXT (t));
10236 break;
10237
10238 case BOUND_TEMPLATE_TEMPLATE_PARM:
10239 /* Record template parameters such as `T' inside `TT<T>'. */
10240 WALK_SUBTREE (TYPE_TI_ARGS (t));
10241 /* Fall through. */
10242
10243 case TEMPLATE_TEMPLATE_PARM:
10244 case TEMPLATE_TYPE_PARM:
10245 case TEMPLATE_PARM_INDEX:
10246 if (fn && (*fn)(t, data))
10247 return t;
10248 else if (!fn)
10249 return t;
10250 break;
10251
10252 case TEMPLATE_DECL:
10253 /* A template template parameter is encountered. */
10254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10255 WALK_SUBTREE (TREE_TYPE (t));
10256
10257 /* Already substituted template template parameter */
10258 *walk_subtrees = 0;
10259 break;
10260
10261 case TYPENAME_TYPE:
10262 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10263 partial instantiation. */
10264 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10265 break;
10266
10267 case CONSTRUCTOR:
10268 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10269 && pfd->include_nondeduced_p)
10270 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10271 break;
10272
10273 case INDIRECT_REF:
10274 case COMPONENT_REF:
10275 /* If there's no type, then this thing must be some expression
10276 involving template parameters. */
10277 if (!fn && !TREE_TYPE (t))
10278 return error_mark_node;
10279 break;
10280
10281 case MODOP_EXPR:
10282 case CAST_EXPR:
10283 case IMPLICIT_CONV_EXPR:
10284 case REINTERPRET_CAST_EXPR:
10285 case CONST_CAST_EXPR:
10286 case STATIC_CAST_EXPR:
10287 case DYNAMIC_CAST_EXPR:
10288 case ARROW_EXPR:
10289 case DOTSTAR_EXPR:
10290 case TYPEID_EXPR:
10291 case PSEUDO_DTOR_EXPR:
10292 if (!fn)
10293 return error_mark_node;
10294 break;
10295
10296 case SCOPE_REF:
10297 if (pfd->include_nondeduced_p)
10298 WALK_SUBTREE (TREE_OPERAND (t, 0));
10299 break;
10300
10301 case REQUIRES_EXPR:
10302 {
10303 if (!fn)
10304 return error_mark_node;
10305
10306 /* Recursively walk the type of each constraint variable. */
10307 tree p = TREE_OPERAND (t, 0);
10308 while (p)
10309 {
10310 WALK_SUBTREE (TREE_TYPE (p));
10311 p = TREE_CHAIN (p);
10312 }
10313 }
10314 break;
10315
10316 default:
10317 break;
10318 }
10319
10320 #undef WALK_SUBTREE
10321
10322 /* We didn't find any template parameters we liked. */
10323 out:
10324 return result;
10325 }
10326
10327 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10328 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10329 call FN with the parameter and the DATA.
10330 If FN returns nonzero, the iteration is terminated, and
10331 for_each_template_parm returns 1. Otherwise, the iteration
10332 continues. If FN never returns a nonzero value, the value
10333 returned by for_each_template_parm is 0. If FN is NULL, it is
10334 considered to be the function which always returns 1.
10335
10336 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10337 parameters that occur in non-deduced contexts. When false, only
10338 visits those template parameters that can be deduced. */
10339
10340 static tree
10341 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10342 hash_set<tree> *visited,
10343 bool include_nondeduced_p,
10344 tree_fn_t any_fn)
10345 {
10346 struct pair_fn_data pfd;
10347 tree result;
10348
10349 /* Set up. */
10350 pfd.fn = fn;
10351 pfd.any_fn = any_fn;
10352 pfd.data = data;
10353 pfd.include_nondeduced_p = include_nondeduced_p;
10354
10355 /* Walk the tree. (Conceptually, we would like to walk without
10356 duplicates, but for_each_template_parm_r recursively calls
10357 for_each_template_parm, so we would need to reorganize a fair
10358 bit to use walk_tree_without_duplicates, so we keep our own
10359 visited list.) */
10360 if (visited)
10361 pfd.visited = visited;
10362 else
10363 pfd.visited = new hash_set<tree>;
10364 result = cp_walk_tree (&t,
10365 for_each_template_parm_r,
10366 &pfd,
10367 pfd.visited);
10368
10369 /* Clean up. */
10370 if (!visited)
10371 {
10372 delete pfd.visited;
10373 pfd.visited = 0;
10374 }
10375
10376 return result;
10377 }
10378
10379 struct find_template_parameter_info
10380 {
10381 explicit find_template_parameter_info (tree ctx_parms)
10382 : ctx_parms (ctx_parms),
10383 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10384 {}
10385
10386 hash_set<tree> visited;
10387 hash_set<tree> parms;
10388 tree ctx_parms;
10389 int max_depth;
10390 };
10391
10392 /* Appends the declaration of T to the list in DATA. */
10393
10394 static int
10395 keep_template_parm (tree t, void* data)
10396 {
10397 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10398
10399 /* Template parameters declared within the expression are not part of
10400 the parameter mapping. For example, in this concept:
10401
10402 template<typename T>
10403 concept C = requires { <expr> } -> same_as<int>;
10404
10405 the return specifier same_as<int> declares a new decltype parameter
10406 that must not be part of the parameter mapping. The same is true
10407 for generic lambda parameters, lambda template parameters, etc. */
10408 int level;
10409 int index;
10410 template_parm_level_and_index (t, &level, &index);
10411 if (level > ftpi->max_depth)
10412 return 0;
10413
10414 /* Arguments like const T yield parameters like const T. This means that
10415 a template-id like X<T, const T> would yield two distinct parameters:
10416 T and const T. Adjust types to their unqualified versions. */
10417 if (TYPE_P (t))
10418 t = TYPE_MAIN_VARIANT (t);
10419 ftpi->parms.add (t);
10420
10421 return 0;
10422 }
10423
10424 /* Ensure that we recursively examine certain terms that are not normally
10425 visited in for_each_template_parm_r. */
10426
10427 static int
10428 any_template_parm_r (tree t, void *data)
10429 {
10430 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10431
10432 #define WALK_SUBTREE(NODE) \
10433 do \
10434 { \
10435 for_each_template_parm (NODE, keep_template_parm, data, \
10436 &ftpi->visited, true, \
10437 any_template_parm_r); \
10438 } \
10439 while (0)
10440
10441 /* A mention of a member alias/typedef is a use of all of its template
10442 arguments, including those from the enclosing class, so we don't use
10443 alias_template_specialization_p here. */
10444 if (TYPE_P (t) && typedef_variant_p (t))
10445 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10446 WALK_SUBTREE (TI_ARGS (tinfo));
10447
10448 switch (TREE_CODE (t))
10449 {
10450 case TEMPLATE_TYPE_PARM:
10451 /* Type constraints of a placeholder type may contain parameters. */
10452 if (is_auto (t))
10453 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10454 WALK_SUBTREE (constr);
10455 break;
10456
10457 case TEMPLATE_ID_EXPR:
10458 /* Search through references to variable templates. */
10459 WALK_SUBTREE (TREE_OPERAND (t, 0));
10460 WALK_SUBTREE (TREE_OPERAND (t, 1));
10461 break;
10462
10463 case TEMPLATE_PARM_INDEX:
10464 case PARM_DECL:
10465 /* A parameter or constraint variable may also depend on a template
10466 parameter without explicitly naming it. */
10467 WALK_SUBTREE (TREE_TYPE (t));
10468 break;
10469
10470 case TEMPLATE_DECL:
10471 {
10472 /* If T is a member template that shares template parameters with
10473 ctx_parms, we need to mark all those parameters for mapping. */
10474 tree dparms = DECL_TEMPLATE_PARMS (t);
10475 tree cparms = ftpi->ctx_parms;
10476 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10477 dparms = TREE_CHAIN (dparms);
10478 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10479 cparms = TREE_CHAIN (cparms);
10480 while (dparms
10481 && (TREE_TYPE (TREE_VALUE (dparms))
10482 != TREE_TYPE (TREE_VALUE (cparms))))
10483 dparms = TREE_CHAIN (dparms),
10484 cparms = TREE_CHAIN (cparms);
10485 if (dparms)
10486 {
10487 int ddepth = TMPL_PARMS_DEPTH (dparms);
10488 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10489 for (int i = 0; i < ddepth; ++i)
10490 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10491 }
10492 }
10493 break;
10494
10495 case LAMBDA_EXPR:
10496 {
10497 /* Look in the parms and body. */
10498 tree fn = lambda_function (t);
10499 WALK_SUBTREE (TREE_TYPE (fn));
10500 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10501 }
10502 break;
10503
10504 default:
10505 break;
10506 }
10507
10508 /* Keep walking. */
10509 return 0;
10510 }
10511
10512 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10513 are the template parameters in scope. */
10514
10515 tree
10516 find_template_parameters (tree t, tree ctx_parms)
10517 {
10518 if (!ctx_parms)
10519 return NULL_TREE;
10520
10521 find_template_parameter_info ftpi (ctx_parms);
10522 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10523 /*include_nondeduced*/true, any_template_parm_r);
10524 tree list = NULL_TREE;
10525 for (hash_set<tree>::iterator iter = ftpi.parms.begin();
10526 iter != ftpi.parms.end(); ++iter)
10527 list = tree_cons (NULL_TREE, *iter, list);
10528 return list;
10529 }
10530
10531 /* Returns true if T depends on any template parameter. */
10532
10533 int
10534 uses_template_parms (tree t)
10535 {
10536 if (t == NULL_TREE)
10537 return false;
10538
10539 bool dependent_p;
10540 int saved_processing_template_decl;
10541
10542 saved_processing_template_decl = processing_template_decl;
10543 if (!saved_processing_template_decl)
10544 processing_template_decl = 1;
10545 if (TYPE_P (t))
10546 dependent_p = dependent_type_p (t);
10547 else if (TREE_CODE (t) == TREE_VEC)
10548 dependent_p = any_dependent_template_arguments_p (t);
10549 else if (TREE_CODE (t) == TREE_LIST)
10550 dependent_p = (uses_template_parms (TREE_VALUE (t))
10551 || uses_template_parms (TREE_CHAIN (t)));
10552 else if (TREE_CODE (t) == TYPE_DECL)
10553 dependent_p = dependent_type_p (TREE_TYPE (t));
10554 else if (t == error_mark_node)
10555 dependent_p = false;
10556 else
10557 dependent_p = value_dependent_expression_p (t);
10558
10559 processing_template_decl = saved_processing_template_decl;
10560
10561 return dependent_p;
10562 }
10563
10564 /* Returns true iff current_function_decl is an incompletely instantiated
10565 template. Useful instead of processing_template_decl because the latter
10566 is set to 0 during instantiate_non_dependent_expr. */
10567
10568 bool
10569 in_template_function (void)
10570 {
10571 tree fn = current_function_decl;
10572 bool ret;
10573 ++processing_template_decl;
10574 ret = (fn && DECL_LANG_SPECIFIC (fn)
10575 && DECL_TEMPLATE_INFO (fn)
10576 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10577 --processing_template_decl;
10578 return ret;
10579 }
10580
10581 /* Returns true if T depends on any template parameter with level LEVEL. */
10582
10583 bool
10584 uses_template_parms_level (tree t, int level)
10585 {
10586 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10587 /*include_nondeduced_p=*/true);
10588 }
10589
10590 /* Returns true if the signature of DECL depends on any template parameter from
10591 its enclosing class. */
10592
10593 bool
10594 uses_outer_template_parms (tree decl)
10595 {
10596 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10597 if (depth == 0)
10598 return false;
10599 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10600 &depth, NULL, /*include_nondeduced_p=*/true))
10601 return true;
10602 if (PRIMARY_TEMPLATE_P (decl)
10603 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10604 (DECL_TEMPLATE_PARMS (decl)),
10605 template_parm_outer_level,
10606 &depth, NULL, /*include_nondeduced_p=*/true))
10607 return true;
10608 tree ci = get_constraints (decl);
10609 if (ci)
10610 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10611 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10612 &depth, NULL, /*nondeduced*/true))
10613 return true;
10614 return false;
10615 }
10616
10617 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10618 ill-formed translation unit, i.e. a variable or function that isn't
10619 usable in a constant expression. */
10620
10621 static inline bool
10622 neglectable_inst_p (tree d)
10623 {
10624 return (d && DECL_P (d)
10625 && !undeduced_auto_decl (d)
10626 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10627 : decl_maybe_constant_var_p (d)));
10628 }
10629
10630 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10631 neglectable and instantiated from within an erroneous instantiation. */
10632
10633 static bool
10634 limit_bad_template_recursion (tree decl)
10635 {
10636 struct tinst_level *lev = current_tinst_level;
10637 int errs = errorcount + sorrycount;
10638 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10639 return false;
10640
10641 for (; lev; lev = lev->next)
10642 if (neglectable_inst_p (lev->maybe_get_node ()))
10643 break;
10644
10645 return (lev && errs > lev->errors);
10646 }
10647
10648 static int tinst_depth;
10649 extern int max_tinst_depth;
10650 int depth_reached;
10651
10652 static GTY(()) struct tinst_level *last_error_tinst_level;
10653
10654 /* We're starting to instantiate D; record the template instantiation context
10655 at LOC for diagnostics and to restore it later. */
10656
10657 static bool
10658 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10659 {
10660 struct tinst_level *new_level;
10661
10662 if (tinst_depth >= max_tinst_depth)
10663 {
10664 /* Tell error.c not to try to instantiate any templates. */
10665 at_eof = 2;
10666 fatal_error (input_location,
10667 "template instantiation depth exceeds maximum of %d"
10668 " (use %<-ftemplate-depth=%> to increase the maximum)",
10669 max_tinst_depth);
10670 return false;
10671 }
10672
10673 /* If the current instantiation caused problems, don't let it instantiate
10674 anything else. Do allow deduction substitution and decls usable in
10675 constant expressions. */
10676 if (!targs && limit_bad_template_recursion (tldcl))
10677 {
10678 /* Avoid no_linkage_errors and unused function warnings for this
10679 decl. */
10680 TREE_NO_WARNING (tldcl) = 1;
10681 return false;
10682 }
10683
10684 /* When not -quiet, dump template instantiations other than functions, since
10685 announce_function will take care of those. */
10686 if (!quiet_flag && !targs
10687 && TREE_CODE (tldcl) != TREE_LIST
10688 && TREE_CODE (tldcl) != FUNCTION_DECL)
10689 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10690
10691 new_level = tinst_level_freelist ().alloc ();
10692 new_level->tldcl = tldcl;
10693 new_level->targs = targs;
10694 new_level->locus = loc;
10695 new_level->errors = errorcount + sorrycount;
10696 new_level->next = NULL;
10697 new_level->refcount = 0;
10698 set_refcount_ptr (new_level->next, current_tinst_level);
10699 set_refcount_ptr (current_tinst_level, new_level);
10700
10701 ++tinst_depth;
10702 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10703 depth_reached = tinst_depth;
10704
10705 return true;
10706 }
10707
10708 /* We're starting substitution of TMPL<ARGS>; record the template
10709 substitution context for diagnostics and to restore it later. */
10710
10711 static bool
10712 push_tinst_level (tree tmpl, tree args)
10713 {
10714 return push_tinst_level_loc (tmpl, args, input_location);
10715 }
10716
10717 /* We're starting to instantiate D; record INPUT_LOCATION and the
10718 template instantiation context for diagnostics and to restore it
10719 later. */
10720
10721 bool
10722 push_tinst_level (tree d)
10723 {
10724 return push_tinst_level_loc (d, input_location);
10725 }
10726
10727 /* Likewise, but record LOC as the program location. */
10728
10729 bool
10730 push_tinst_level_loc (tree d, location_t loc)
10731 {
10732 gcc_assert (TREE_CODE (d) != TREE_LIST);
10733 return push_tinst_level_loc (d, NULL, loc);
10734 }
10735
10736 /* We're done instantiating this template; return to the instantiation
10737 context. */
10738
10739 void
10740 pop_tinst_level (void)
10741 {
10742 /* Restore the filename and line number stashed away when we started
10743 this instantiation. */
10744 input_location = current_tinst_level->locus;
10745 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10746 --tinst_depth;
10747 }
10748
10749 /* We're instantiating a deferred template; restore the template
10750 instantiation context in which the instantiation was requested, which
10751 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10752
10753 static tree
10754 reopen_tinst_level (struct tinst_level *level)
10755 {
10756 struct tinst_level *t;
10757
10758 tinst_depth = 0;
10759 for (t = level; t; t = t->next)
10760 ++tinst_depth;
10761
10762 set_refcount_ptr (current_tinst_level, level);
10763 pop_tinst_level ();
10764 if (current_tinst_level)
10765 current_tinst_level->errors = errorcount+sorrycount;
10766 return level->maybe_get_node ();
10767 }
10768
10769 /* Returns the TINST_LEVEL which gives the original instantiation
10770 context. */
10771
10772 struct tinst_level *
10773 outermost_tinst_level (void)
10774 {
10775 struct tinst_level *level = current_tinst_level;
10776 if (level)
10777 while (level->next)
10778 level = level->next;
10779 return level;
10780 }
10781
10782 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10783 vector of template arguments, as for tsubst.
10784
10785 Returns an appropriate tsubst'd friend declaration. */
10786
10787 static tree
10788 tsubst_friend_function (tree decl, tree args)
10789 {
10790 tree new_friend;
10791
10792 if (TREE_CODE (decl) == FUNCTION_DECL
10793 && DECL_TEMPLATE_INSTANTIATION (decl)
10794 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10795 /* This was a friend declared with an explicit template
10796 argument list, e.g.:
10797
10798 friend void f<>(T);
10799
10800 to indicate that f was a template instantiation, not a new
10801 function declaration. Now, we have to figure out what
10802 instantiation of what template. */
10803 {
10804 tree template_id, arglist, fns;
10805 tree new_args;
10806 tree tmpl;
10807 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10808
10809 /* Friend functions are looked up in the containing namespace scope.
10810 We must enter that scope, to avoid finding member functions of the
10811 current class with same name. */
10812 push_nested_namespace (ns);
10813 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10814 tf_warning_or_error, NULL_TREE,
10815 /*integral_constant_expression_p=*/false);
10816 pop_nested_namespace (ns);
10817 arglist = tsubst (DECL_TI_ARGS (decl), args,
10818 tf_warning_or_error, NULL_TREE);
10819 template_id = lookup_template_function (fns, arglist);
10820
10821 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10822 tmpl = determine_specialization (template_id, new_friend,
10823 &new_args,
10824 /*need_member_template=*/0,
10825 TREE_VEC_LENGTH (args),
10826 tsk_none);
10827 return instantiate_template (tmpl, new_args, tf_error);
10828 }
10829
10830 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10831
10832 /* The NEW_FRIEND will look like an instantiation, to the
10833 compiler, but is not an instantiation from the point of view of
10834 the language. For example, we might have had:
10835
10836 template <class T> struct S {
10837 template <class U> friend void f(T, U);
10838 };
10839
10840 Then, in S<int>, template <class U> void f(int, U) is not an
10841 instantiation of anything. */
10842 if (new_friend == error_mark_node)
10843 return error_mark_node;
10844
10845 DECL_USE_TEMPLATE (new_friend) = 0;
10846 if (TREE_CODE (decl) == TEMPLATE_DECL)
10847 {
10848 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10849 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10850 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10851
10852 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10853 match in decls_match. */
10854 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10855 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10856 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10857 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10858 }
10859
10860 /* The mangled name for the NEW_FRIEND is incorrect. The function
10861 is not a template instantiation and should not be mangled like
10862 one. Therefore, we forget the mangling here; we'll recompute it
10863 later if we need it. */
10864 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10865 {
10866 SET_DECL_RTL (new_friend, NULL);
10867 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10868 }
10869
10870 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10871 {
10872 tree old_decl;
10873 tree new_friend_template_info;
10874 tree new_friend_result_template_info;
10875 tree ns;
10876 int new_friend_is_defn;
10877
10878 /* We must save some information from NEW_FRIEND before calling
10879 duplicate decls since that function will free NEW_FRIEND if
10880 possible. */
10881 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10882 new_friend_is_defn =
10883 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10884 (template_for_substitution (new_friend)))
10885 != NULL_TREE);
10886 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10887 {
10888 /* This declaration is a `primary' template. */
10889 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10890
10891 new_friend_result_template_info
10892 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10893 }
10894 else
10895 new_friend_result_template_info = NULL_TREE;
10896
10897 /* Inside pushdecl_namespace_level, we will push into the
10898 current namespace. However, the friend function should go
10899 into the namespace of the template. */
10900 ns = decl_namespace_context (new_friend);
10901 push_nested_namespace (ns);
10902 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10903 pop_nested_namespace (ns);
10904
10905 if (old_decl == error_mark_node)
10906 return error_mark_node;
10907
10908 if (old_decl != new_friend)
10909 {
10910 /* This new friend declaration matched an existing
10911 declaration. For example, given:
10912
10913 template <class T> void f(T);
10914 template <class U> class C {
10915 template <class T> friend void f(T) {}
10916 };
10917
10918 the friend declaration actually provides the definition
10919 of `f', once C has been instantiated for some type. So,
10920 old_decl will be the out-of-class template declaration,
10921 while new_friend is the in-class definition.
10922
10923 But, if `f' was called before this point, the
10924 instantiation of `f' will have DECL_TI_ARGS corresponding
10925 to `T' but not to `U', references to which might appear
10926 in the definition of `f'. Previously, the most general
10927 template for an instantiation of `f' was the out-of-class
10928 version; now it is the in-class version. Therefore, we
10929 run through all specialization of `f', adding to their
10930 DECL_TI_ARGS appropriately. In particular, they need a
10931 new set of outer arguments, corresponding to the
10932 arguments for this class instantiation.
10933
10934 The same situation can arise with something like this:
10935
10936 friend void f(int);
10937 template <class T> class C {
10938 friend void f(T) {}
10939 };
10940
10941 when `C<int>' is instantiated. Now, `f(int)' is defined
10942 in the class. */
10943
10944 if (!new_friend_is_defn)
10945 /* On the other hand, if the in-class declaration does
10946 *not* provide a definition, then we don't want to alter
10947 existing definitions. We can just leave everything
10948 alone. */
10949 ;
10950 else
10951 {
10952 tree new_template = TI_TEMPLATE (new_friend_template_info);
10953 tree new_args = TI_ARGS (new_friend_template_info);
10954
10955 /* Overwrite whatever template info was there before, if
10956 any, with the new template information pertaining to
10957 the declaration. */
10958 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10959
10960 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10961 {
10962 /* We should have called reregister_specialization in
10963 duplicate_decls. */
10964 gcc_assert (retrieve_specialization (new_template,
10965 new_args, 0)
10966 == old_decl);
10967
10968 /* Instantiate it if the global has already been used. */
10969 if (DECL_ODR_USED (old_decl))
10970 instantiate_decl (old_decl, /*defer_ok=*/true,
10971 /*expl_inst_class_mem_p=*/false);
10972 }
10973 else
10974 {
10975 tree t;
10976
10977 /* Indicate that the old function template is a partial
10978 instantiation. */
10979 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10980 = new_friend_result_template_info;
10981
10982 gcc_assert (new_template
10983 == most_general_template (new_template));
10984 gcc_assert (new_template != old_decl);
10985
10986 /* Reassign any specializations already in the hash table
10987 to the new more general template, and add the
10988 additional template args. */
10989 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10990 t != NULL_TREE;
10991 t = TREE_CHAIN (t))
10992 {
10993 tree spec = TREE_VALUE (t);
10994 spec_entry elt;
10995
10996 elt.tmpl = old_decl;
10997 elt.args = DECL_TI_ARGS (spec);
10998 elt.spec = NULL_TREE;
10999
11000 decl_specializations->remove_elt (&elt);
11001
11002 DECL_TI_ARGS (spec)
11003 = add_outermost_template_args (new_args,
11004 DECL_TI_ARGS (spec));
11005
11006 register_specialization
11007 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11008
11009 }
11010 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11011 }
11012 }
11013
11014 /* The information from NEW_FRIEND has been merged into OLD_DECL
11015 by duplicate_decls. */
11016 new_friend = old_decl;
11017 }
11018 }
11019 else
11020 {
11021 tree context = DECL_CONTEXT (new_friend);
11022 bool dependent_p;
11023
11024 /* In the code
11025 template <class T> class C {
11026 template <class U> friend void C1<U>::f (); // case 1
11027 friend void C2<T>::f (); // case 2
11028 };
11029 we only need to make sure CONTEXT is a complete type for
11030 case 2. To distinguish between the two cases, we note that
11031 CONTEXT of case 1 remains dependent type after tsubst while
11032 this isn't true for case 2. */
11033 ++processing_template_decl;
11034 dependent_p = dependent_type_p (context);
11035 --processing_template_decl;
11036
11037 if (!dependent_p
11038 && !complete_type_or_else (context, NULL_TREE))
11039 return error_mark_node;
11040
11041 if (COMPLETE_TYPE_P (context))
11042 {
11043 tree fn = new_friend;
11044 /* do_friend adds the TEMPLATE_DECL for any member friend
11045 template even if it isn't a member template, i.e.
11046 template <class T> friend A<T>::f();
11047 Look through it in that case. */
11048 if (TREE_CODE (fn) == TEMPLATE_DECL
11049 && !PRIMARY_TEMPLATE_P (fn))
11050 fn = DECL_TEMPLATE_RESULT (fn);
11051 /* Check to see that the declaration is really present, and,
11052 possibly obtain an improved declaration. */
11053 fn = check_classfn (context, fn, NULL_TREE);
11054
11055 if (fn)
11056 new_friend = fn;
11057 }
11058 }
11059
11060 return new_friend;
11061 }
11062
11063 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11064 template arguments, as for tsubst.
11065
11066 Returns an appropriate tsubst'd friend type or error_mark_node on
11067 failure. */
11068
11069 static tree
11070 tsubst_friend_class (tree friend_tmpl, tree args)
11071 {
11072 tree tmpl;
11073
11074 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11075 {
11076 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11077 return TREE_TYPE (tmpl);
11078 }
11079
11080 tree context = CP_DECL_CONTEXT (friend_tmpl);
11081 if (TREE_CODE (context) == NAMESPACE_DECL)
11082 push_nested_namespace (context);
11083 else
11084 {
11085 context = tsubst (context, args, tf_error, NULL_TREE);
11086 push_nested_class (context);
11087 }
11088
11089 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11090 /*non_class=*/false, /*block_p=*/false,
11091 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11092
11093 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11094 {
11095 /* The friend template has already been declared. Just
11096 check to see that the declarations match, and install any new
11097 default parameters. We must tsubst the default parameters,
11098 of course. We only need the innermost template parameters
11099 because that is all that redeclare_class_template will look
11100 at. */
11101 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11102 > TMPL_ARGS_DEPTH (args))
11103 {
11104 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11105 args, tf_warning_or_error);
11106 location_t saved_input_location = input_location;
11107 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11108 tree cons = get_constraints (tmpl);
11109 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11110 input_location = saved_input_location;
11111 }
11112 }
11113 else
11114 {
11115 /* The friend template has not already been declared. In this
11116 case, the instantiation of the template class will cause the
11117 injection of this template into the namespace scope. */
11118 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11119
11120 if (tmpl != error_mark_node)
11121 {
11122 /* The new TMPL is not an instantiation of anything, so we
11123 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11124 for the new type because that is supposed to be the
11125 corresponding template decl, i.e., TMPL. */
11126 DECL_USE_TEMPLATE (tmpl) = 0;
11127 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11128 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11129 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11130 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11131
11132 /* It is hidden. */
11133 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11134 DECL_ANTICIPATED (tmpl)
11135 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11136
11137 /* Inject this template into the enclosing namspace scope. */
11138 tmpl = pushdecl_namespace_level (tmpl, true);
11139 }
11140 }
11141
11142 if (TREE_CODE (context) == NAMESPACE_DECL)
11143 pop_nested_namespace (context);
11144 else
11145 pop_nested_class ();
11146
11147 return TREE_TYPE (tmpl);
11148 }
11149
11150 /* Returns zero if TYPE cannot be completed later due to circularity.
11151 Otherwise returns one. */
11152
11153 static int
11154 can_complete_type_without_circularity (tree type)
11155 {
11156 if (type == NULL_TREE || type == error_mark_node)
11157 return 0;
11158 else if (COMPLETE_TYPE_P (type))
11159 return 1;
11160 else if (TREE_CODE (type) == ARRAY_TYPE)
11161 return can_complete_type_without_circularity (TREE_TYPE (type));
11162 else if (CLASS_TYPE_P (type)
11163 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11164 return 0;
11165 else
11166 return 1;
11167 }
11168
11169 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11170 tsubst_flags_t, tree);
11171
11172 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11173 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11174
11175 static tree
11176 tsubst_attribute (tree t, tree *decl_p, tree args,
11177 tsubst_flags_t complain, tree in_decl)
11178 {
11179 gcc_assert (ATTR_IS_DEPENDENT (t));
11180
11181 tree val = TREE_VALUE (t);
11182 if (val == NULL_TREE)
11183 /* Nothing to do. */;
11184 else if ((flag_openmp || flag_openmp_simd)
11185 && is_attribute_p ("omp declare simd",
11186 get_attribute_name (t)))
11187 {
11188 tree clauses = TREE_VALUE (val);
11189 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11190 complain, in_decl);
11191 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11192 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11193 tree parms = DECL_ARGUMENTS (*decl_p);
11194 clauses
11195 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11196 if (clauses)
11197 val = build_tree_list (NULL_TREE, clauses);
11198 else
11199 val = NULL_TREE;
11200 }
11201 else if (flag_openmp
11202 && is_attribute_p ("omp declare variant base",
11203 get_attribute_name (t)))
11204 {
11205 ++cp_unevaluated_operand;
11206 tree varid
11207 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11208 in_decl, /*integral_constant_expression_p=*/false);
11209 --cp_unevaluated_operand;
11210 tree chain = TREE_CHAIN (val);
11211 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11212 tree ctx = copy_list (TREE_VALUE (val));
11213 tree simd = get_identifier ("simd");
11214 tree score = get_identifier (" score");
11215 tree condition = get_identifier ("condition");
11216 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11217 {
11218 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11219 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11220 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11221 {
11222 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11223 {
11224 tree clauses = TREE_VALUE (t2);
11225 clauses = tsubst_omp_clauses (clauses,
11226 C_ORT_OMP_DECLARE_SIMD, args,
11227 complain, in_decl);
11228 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11229 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11230 TREE_VALUE (t2) = clauses;
11231 }
11232 else
11233 {
11234 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11235 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11236 if (TREE_VALUE (t3))
11237 {
11238 bool allow_string
11239 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11240 && TREE_PURPOSE (t3) != score);
11241 tree v = TREE_VALUE (t3);
11242 if (TREE_CODE (v) == STRING_CST && allow_string)
11243 continue;
11244 v = tsubst_expr (v, args, complain, in_decl, true);
11245 v = fold_non_dependent_expr (v);
11246 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11247 || (TREE_PURPOSE (t3) == score
11248 ? TREE_CODE (v) != INTEGER_CST
11249 : !tree_fits_shwi_p (v)))
11250 {
11251 location_t loc
11252 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11253 match_loc);
11254 if (TREE_PURPOSE (t3) == score)
11255 error_at (loc, "score argument must be "
11256 "constant integer expression");
11257 else if (allow_string)
11258 error_at (loc, "property must be constant "
11259 "integer expression or string "
11260 "literal");
11261 else
11262 error_at (loc, "property must be constant "
11263 "integer expression");
11264 return NULL_TREE;
11265 }
11266 else if (TREE_PURPOSE (t3) == score
11267 && tree_int_cst_sgn (v) < 0)
11268 {
11269 location_t loc
11270 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11271 match_loc);
11272 error_at (loc, "score argument must be "
11273 "non-negative");
11274 return NULL_TREE;
11275 }
11276 TREE_VALUE (t3) = v;
11277 }
11278 }
11279 }
11280 }
11281 val = tree_cons (varid, ctx, chain);
11282 }
11283 /* If the first attribute argument is an identifier, don't
11284 pass it through tsubst. Attributes like mode, format,
11285 cleanup and several target specific attributes expect it
11286 unmodified. */
11287 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11288 {
11289 tree chain
11290 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11291 /*integral_constant_expression_p=*/false);
11292 if (chain != TREE_CHAIN (val))
11293 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11294 }
11295 else if (PACK_EXPANSION_P (val))
11296 {
11297 /* An attribute pack expansion. */
11298 tree purp = TREE_PURPOSE (t);
11299 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11300 if (pack == error_mark_node)
11301 return error_mark_node;
11302 int len = TREE_VEC_LENGTH (pack);
11303 tree list = NULL_TREE;
11304 tree *q = &list;
11305 for (int i = 0; i < len; ++i)
11306 {
11307 tree elt = TREE_VEC_ELT (pack, i);
11308 *q = build_tree_list (purp, elt);
11309 q = &TREE_CHAIN (*q);
11310 }
11311 return list;
11312 }
11313 else
11314 val = tsubst_expr (val, args, complain, in_decl,
11315 /*integral_constant_expression_p=*/false);
11316
11317 if (val != TREE_VALUE (t))
11318 return build_tree_list (TREE_PURPOSE (t), val);
11319 return t;
11320 }
11321
11322 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11323 unchanged or a new TREE_LIST chain. */
11324
11325 static tree
11326 tsubst_attributes (tree attributes, tree args,
11327 tsubst_flags_t complain, tree in_decl)
11328 {
11329 tree last_dep = NULL_TREE;
11330
11331 for (tree t = attributes; t; t = TREE_CHAIN (t))
11332 if (ATTR_IS_DEPENDENT (t))
11333 {
11334 last_dep = t;
11335 attributes = copy_list (attributes);
11336 break;
11337 }
11338
11339 if (last_dep)
11340 for (tree *p = &attributes; *p; )
11341 {
11342 tree t = *p;
11343 if (ATTR_IS_DEPENDENT (t))
11344 {
11345 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11346 if (subst != t)
11347 {
11348 *p = subst;
11349 while (*p)
11350 p = &TREE_CHAIN (*p);
11351 *p = TREE_CHAIN (t);
11352 continue;
11353 }
11354 }
11355 p = &TREE_CHAIN (*p);
11356 }
11357
11358 return attributes;
11359 }
11360
11361 /* Apply any attributes which had to be deferred until instantiation
11362 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11363 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11364
11365 static void
11366 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11367 tree args, tsubst_flags_t complain, tree in_decl)
11368 {
11369 tree last_dep = NULL_TREE;
11370 tree t;
11371 tree *p;
11372
11373 if (attributes == NULL_TREE)
11374 return;
11375
11376 if (DECL_P (*decl_p))
11377 {
11378 if (TREE_TYPE (*decl_p) == error_mark_node)
11379 return;
11380 p = &DECL_ATTRIBUTES (*decl_p);
11381 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11382 to our attributes parameter. */
11383 gcc_assert (*p == attributes);
11384 }
11385 else
11386 {
11387 p = &TYPE_ATTRIBUTES (*decl_p);
11388 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11389 lookup_template_class_1, and should be preserved. */
11390 gcc_assert (*p != attributes);
11391 while (*p)
11392 p = &TREE_CHAIN (*p);
11393 }
11394
11395 for (t = attributes; t; t = TREE_CHAIN (t))
11396 if (ATTR_IS_DEPENDENT (t))
11397 {
11398 last_dep = t;
11399 attributes = copy_list (attributes);
11400 break;
11401 }
11402
11403 *p = attributes;
11404 if (last_dep)
11405 {
11406 tree late_attrs = NULL_TREE;
11407 tree *q = &late_attrs;
11408
11409 for (; *p; )
11410 {
11411 t = *p;
11412 if (ATTR_IS_DEPENDENT (t))
11413 {
11414 *p = TREE_CHAIN (t);
11415 TREE_CHAIN (t) = NULL_TREE;
11416 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11417 while (*q)
11418 q = &TREE_CHAIN (*q);
11419 }
11420 else
11421 p = &TREE_CHAIN (t);
11422 }
11423
11424 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11425 }
11426 }
11427
11428 /* Perform (or defer) access check for typedefs that were referenced
11429 from within the template TMPL code.
11430 This is a subroutine of instantiate_decl and instantiate_class_template.
11431 TMPL is the template to consider and TARGS is the list of arguments of
11432 that template. */
11433
11434 static void
11435 perform_typedefs_access_check (tree tmpl, tree targs)
11436 {
11437 unsigned i;
11438 qualified_typedef_usage_t *iter;
11439
11440 if (!tmpl
11441 || (!CLASS_TYPE_P (tmpl)
11442 && TREE_CODE (tmpl) != FUNCTION_DECL))
11443 return;
11444
11445 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11446 {
11447 tree type_decl = iter->typedef_decl;
11448 tree type_scope = iter->context;
11449
11450 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11451 continue;
11452
11453 if (uses_template_parms (type_decl))
11454 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11455 if (uses_template_parms (type_scope))
11456 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11457
11458 /* Make access check error messages point to the location
11459 of the use of the typedef. */
11460 iloc_sentinel ils (iter->locus);
11461 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11462 type_decl, type_decl,
11463 tf_warning_or_error);
11464 }
11465 }
11466
11467 static tree
11468 instantiate_class_template_1 (tree type)
11469 {
11470 tree templ, args, pattern, t, member;
11471 tree typedecl;
11472 tree pbinfo;
11473 tree base_list;
11474 unsigned int saved_maximum_field_alignment;
11475 tree fn_context;
11476
11477 if (type == error_mark_node)
11478 return error_mark_node;
11479
11480 if (COMPLETE_OR_OPEN_TYPE_P (type)
11481 || uses_template_parms (type))
11482 return type;
11483
11484 /* Figure out which template is being instantiated. */
11485 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11486 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11487
11488 /* Mark the type as in the process of being defined. */
11489 TYPE_BEING_DEFINED (type) = 1;
11490
11491 /* We may be in the middle of deferred access check. Disable
11492 it now. */
11493 deferring_access_check_sentinel acs (dk_no_deferred);
11494
11495 /* Determine what specialization of the original template to
11496 instantiate. */
11497 t = most_specialized_partial_spec (type, tf_warning_or_error);
11498 if (t == error_mark_node)
11499 return error_mark_node;
11500 else if (t)
11501 {
11502 /* This TYPE is actually an instantiation of a partial
11503 specialization. We replace the innermost set of ARGS with
11504 the arguments appropriate for substitution. For example,
11505 given:
11506
11507 template <class T> struct S {};
11508 template <class T> struct S<T*> {};
11509
11510 and supposing that we are instantiating S<int*>, ARGS will
11511 presently be {int*} -- but we need {int}. */
11512 pattern = TREE_TYPE (t);
11513 args = TREE_PURPOSE (t);
11514 }
11515 else
11516 {
11517 pattern = TREE_TYPE (templ);
11518 args = CLASSTYPE_TI_ARGS (type);
11519 }
11520
11521 /* If the template we're instantiating is incomplete, then clearly
11522 there's nothing we can do. */
11523 if (!COMPLETE_TYPE_P (pattern))
11524 {
11525 /* We can try again later. */
11526 TYPE_BEING_DEFINED (type) = 0;
11527 return type;
11528 }
11529
11530 /* If we've recursively instantiated too many templates, stop. */
11531 if (! push_tinst_level (type))
11532 return type;
11533
11534 int saved_unevaluated_operand = cp_unevaluated_operand;
11535 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11536
11537 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11538 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11539 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11540 fn_context = error_mark_node;
11541 if (!fn_context)
11542 push_to_top_level ();
11543 else
11544 {
11545 cp_unevaluated_operand = 0;
11546 c_inhibit_evaluation_warnings = 0;
11547 }
11548 /* Use #pragma pack from the template context. */
11549 saved_maximum_field_alignment = maximum_field_alignment;
11550 maximum_field_alignment = TYPE_PRECISION (pattern);
11551
11552 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11553
11554 /* Set the input location to the most specialized template definition.
11555 This is needed if tsubsting causes an error. */
11556 typedecl = TYPE_MAIN_DECL (pattern);
11557 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11558 DECL_SOURCE_LOCATION (typedecl);
11559
11560 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11561 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11562 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11563 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11564 if (ANON_AGGR_TYPE_P (pattern))
11565 SET_ANON_AGGR_TYPE_P (type);
11566 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11567 {
11568 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11569 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11570 /* Adjust visibility for template arguments. */
11571 determine_visibility (TYPE_MAIN_DECL (type));
11572 }
11573 if (CLASS_TYPE_P (type))
11574 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11575
11576 pbinfo = TYPE_BINFO (pattern);
11577
11578 /* We should never instantiate a nested class before its enclosing
11579 class; we need to look up the nested class by name before we can
11580 instantiate it, and that lookup should instantiate the enclosing
11581 class. */
11582 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11583 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11584
11585 base_list = NULL_TREE;
11586 if (BINFO_N_BASE_BINFOS (pbinfo))
11587 {
11588 tree pbase_binfo;
11589 tree pushed_scope;
11590 int i;
11591
11592 /* We must enter the scope containing the type, as that is where
11593 the accessibility of types named in dependent bases are
11594 looked up from. */
11595 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11596
11597 /* Substitute into each of the bases to determine the actual
11598 basetypes. */
11599 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11600 {
11601 tree base;
11602 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11603 tree expanded_bases = NULL_TREE;
11604 int idx, len = 1;
11605
11606 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11607 {
11608 expanded_bases =
11609 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11610 args, tf_error, NULL_TREE);
11611 if (expanded_bases == error_mark_node)
11612 continue;
11613
11614 len = TREE_VEC_LENGTH (expanded_bases);
11615 }
11616
11617 for (idx = 0; idx < len; idx++)
11618 {
11619 if (expanded_bases)
11620 /* Extract the already-expanded base class. */
11621 base = TREE_VEC_ELT (expanded_bases, idx);
11622 else
11623 /* Substitute to figure out the base class. */
11624 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11625 NULL_TREE);
11626
11627 if (base == error_mark_node)
11628 continue;
11629
11630 base_list = tree_cons (access, base, base_list);
11631 if (BINFO_VIRTUAL_P (pbase_binfo))
11632 TREE_TYPE (base_list) = integer_type_node;
11633 }
11634 }
11635
11636 /* The list is now in reverse order; correct that. */
11637 base_list = nreverse (base_list);
11638
11639 if (pushed_scope)
11640 pop_scope (pushed_scope);
11641 }
11642 /* Now call xref_basetypes to set up all the base-class
11643 information. */
11644 xref_basetypes (type, base_list);
11645
11646 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11647 (int) ATTR_FLAG_TYPE_IN_PLACE,
11648 args, tf_error, NULL_TREE);
11649 fixup_attribute_variants (type);
11650
11651 /* Now that our base classes are set up, enter the scope of the
11652 class, so that name lookups into base classes, etc. will work
11653 correctly. This is precisely analogous to what we do in
11654 begin_class_definition when defining an ordinary non-template
11655 class, except we also need to push the enclosing classes. */
11656 push_nested_class (type);
11657
11658 /* Now members are processed in the order of declaration. */
11659 for (member = CLASSTYPE_DECL_LIST (pattern);
11660 member; member = TREE_CHAIN (member))
11661 {
11662 tree t = TREE_VALUE (member);
11663
11664 if (TREE_PURPOSE (member))
11665 {
11666 if (TYPE_P (t))
11667 {
11668 if (LAMBDA_TYPE_P (t))
11669 /* A closure type for a lambda in an NSDMI or default argument.
11670 Ignore it; it will be regenerated when needed. */
11671 continue;
11672
11673 /* Build new CLASSTYPE_NESTED_UTDS. */
11674
11675 tree newtag;
11676 bool class_template_p;
11677
11678 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11679 && TYPE_LANG_SPECIFIC (t)
11680 && CLASSTYPE_IS_TEMPLATE (t));
11681 /* If the member is a class template, then -- even after
11682 substitution -- there may be dependent types in the
11683 template argument list for the class. We increment
11684 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11685 that function will assume that no types are dependent
11686 when outside of a template. */
11687 if (class_template_p)
11688 ++processing_template_decl;
11689 newtag = tsubst (t, args, tf_error, NULL_TREE);
11690 if (class_template_p)
11691 --processing_template_decl;
11692 if (newtag == error_mark_node)
11693 continue;
11694
11695 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11696 {
11697 tree name = TYPE_IDENTIFIER (t);
11698
11699 if (class_template_p)
11700 /* Unfortunately, lookup_template_class sets
11701 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11702 instantiation (i.e., for the type of a member
11703 template class nested within a template class.)
11704 This behavior is required for
11705 maybe_process_partial_specialization to work
11706 correctly, but is not accurate in this case;
11707 the TAG is not an instantiation of anything.
11708 (The corresponding TEMPLATE_DECL is an
11709 instantiation, but the TYPE is not.) */
11710 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11711
11712 /* Now, we call pushtag to put this NEWTAG into the scope of
11713 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11714 pushtag calling push_template_decl. We don't have to do
11715 this for enums because it will already have been done in
11716 tsubst_enum. */
11717 if (name)
11718 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11719 pushtag (name, newtag, /*tag_scope=*/ts_current);
11720 }
11721 }
11722 else if (DECL_DECLARES_FUNCTION_P (t))
11723 {
11724 tree r;
11725
11726 if (TREE_CODE (t) == TEMPLATE_DECL)
11727 ++processing_template_decl;
11728 r = tsubst (t, args, tf_error, NULL_TREE);
11729 if (TREE_CODE (t) == TEMPLATE_DECL)
11730 --processing_template_decl;
11731 set_current_access_from_decl (r);
11732 finish_member_declaration (r);
11733 /* Instantiate members marked with attribute used. */
11734 if (r != error_mark_node && DECL_PRESERVE_P (r))
11735 mark_used (r);
11736 if (TREE_CODE (r) == FUNCTION_DECL
11737 && DECL_OMP_DECLARE_REDUCTION_P (r))
11738 cp_check_omp_declare_reduction (r);
11739 }
11740 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11741 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11742 /* A closure type for a lambda in an NSDMI or default argument.
11743 Ignore it; it will be regenerated when needed. */;
11744 else
11745 {
11746 /* Build new TYPE_FIELDS. */
11747 if (TREE_CODE (t) == STATIC_ASSERT)
11748 {
11749 tree condition;
11750
11751 ++c_inhibit_evaluation_warnings;
11752 condition =
11753 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11754 tf_warning_or_error, NULL_TREE,
11755 /*integral_constant_expression_p=*/true);
11756 --c_inhibit_evaluation_warnings;
11757
11758 finish_static_assert (condition,
11759 STATIC_ASSERT_MESSAGE (t),
11760 STATIC_ASSERT_SOURCE_LOCATION (t),
11761 /*member_p=*/true);
11762 }
11763 else if (TREE_CODE (t) != CONST_DECL)
11764 {
11765 tree r;
11766 tree vec = NULL_TREE;
11767 int len = 1;
11768
11769 /* The file and line for this declaration, to
11770 assist in error message reporting. Since we
11771 called push_tinst_level above, we don't need to
11772 restore these. */
11773 input_location = DECL_SOURCE_LOCATION (t);
11774
11775 if (TREE_CODE (t) == TEMPLATE_DECL)
11776 ++processing_template_decl;
11777 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11778 if (TREE_CODE (t) == TEMPLATE_DECL)
11779 --processing_template_decl;
11780
11781 if (TREE_CODE (r) == TREE_VEC)
11782 {
11783 /* A capture pack became multiple fields. */
11784 vec = r;
11785 len = TREE_VEC_LENGTH (vec);
11786 }
11787
11788 for (int i = 0; i < len; ++i)
11789 {
11790 if (vec)
11791 r = TREE_VEC_ELT (vec, i);
11792 if (VAR_P (r))
11793 {
11794 /* In [temp.inst]:
11795
11796 [t]he initialization (and any associated
11797 side-effects) of a static data member does
11798 not occur unless the static data member is
11799 itself used in a way that requires the
11800 definition of the static data member to
11801 exist.
11802
11803 Therefore, we do not substitute into the
11804 initialized for the static data member here. */
11805 finish_static_data_member_decl
11806 (r,
11807 /*init=*/NULL_TREE,
11808 /*init_const_expr_p=*/false,
11809 /*asmspec_tree=*/NULL_TREE,
11810 /*flags=*/0);
11811 /* Instantiate members marked with attribute used. */
11812 if (r != error_mark_node && DECL_PRESERVE_P (r))
11813 mark_used (r);
11814 }
11815 else if (TREE_CODE (r) == FIELD_DECL)
11816 {
11817 /* Determine whether R has a valid type and can be
11818 completed later. If R is invalid, then its type
11819 is replaced by error_mark_node. */
11820 tree rtype = TREE_TYPE (r);
11821 if (can_complete_type_without_circularity (rtype))
11822 complete_type (rtype);
11823
11824 if (!complete_or_array_type_p (rtype))
11825 {
11826 /* If R's type couldn't be completed and
11827 it isn't a flexible array member (whose
11828 type is incomplete by definition) give
11829 an error. */
11830 cxx_incomplete_type_error (r, rtype);
11831 TREE_TYPE (r) = error_mark_node;
11832 }
11833 else if (TREE_CODE (rtype) == ARRAY_TYPE
11834 && TYPE_DOMAIN (rtype) == NULL_TREE
11835 && (TREE_CODE (type) == UNION_TYPE
11836 || TREE_CODE (type) == QUAL_UNION_TYPE))
11837 {
11838 error ("flexible array member %qD in union", r);
11839 TREE_TYPE (r) = error_mark_node;
11840 }
11841 else if (!verify_type_context (input_location,
11842 TCTX_FIELD, rtype))
11843 TREE_TYPE (r) = error_mark_node;
11844 }
11845
11846 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11847 such a thing will already have been added to the field
11848 list by tsubst_enum in finish_member_declaration in the
11849 CLASSTYPE_NESTED_UTDS case above. */
11850 if (!(TREE_CODE (r) == TYPE_DECL
11851 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11852 && DECL_ARTIFICIAL (r)))
11853 {
11854 set_current_access_from_decl (r);
11855 finish_member_declaration (r);
11856 }
11857 }
11858 }
11859 }
11860 }
11861 else
11862 {
11863 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11864 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11865 {
11866 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11867
11868 tree friend_type = t;
11869 bool adjust_processing_template_decl = false;
11870
11871 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11872 {
11873 /* template <class T> friend class C; */
11874 friend_type = tsubst_friend_class (friend_type, args);
11875 adjust_processing_template_decl = true;
11876 }
11877 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11878 {
11879 /* template <class T> friend class C::D; */
11880 friend_type = tsubst (friend_type, args,
11881 tf_warning_or_error, NULL_TREE);
11882 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11883 friend_type = TREE_TYPE (friend_type);
11884 adjust_processing_template_decl = true;
11885 }
11886 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11887 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11888 {
11889 /* This could be either
11890
11891 friend class T::C;
11892
11893 when dependent_type_p is false or
11894
11895 template <class U> friend class T::C;
11896
11897 otherwise. */
11898 /* Bump processing_template_decl in case this is something like
11899 template <class T> friend struct A<T>::B. */
11900 ++processing_template_decl;
11901 friend_type = tsubst (friend_type, args,
11902 tf_warning_or_error, NULL_TREE);
11903 if (dependent_type_p (friend_type))
11904 adjust_processing_template_decl = true;
11905 --processing_template_decl;
11906 }
11907 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11908 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11909 && TYPE_HIDDEN_P (friend_type))
11910 {
11911 /* friend class C;
11912
11913 where C hasn't been declared yet. Let's lookup name
11914 from namespace scope directly, bypassing any name that
11915 come from dependent base class. */
11916 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11917
11918 /* The call to xref_tag_from_type does injection for friend
11919 classes. */
11920 push_nested_namespace (ns);
11921 friend_type =
11922 xref_tag_from_type (friend_type, NULL_TREE,
11923 /*tag_scope=*/ts_current);
11924 pop_nested_namespace (ns);
11925 }
11926 else if (uses_template_parms (friend_type))
11927 /* friend class C<T>; */
11928 friend_type = tsubst (friend_type, args,
11929 tf_warning_or_error, NULL_TREE);
11930 /* Otherwise it's
11931
11932 friend class C;
11933
11934 where C is already declared or
11935
11936 friend class C<int>;
11937
11938 We don't have to do anything in these cases. */
11939
11940 if (adjust_processing_template_decl)
11941 /* Trick make_friend_class into realizing that the friend
11942 we're adding is a template, not an ordinary class. It's
11943 important that we use make_friend_class since it will
11944 perform some error-checking and output cross-reference
11945 information. */
11946 ++processing_template_decl;
11947
11948 if (friend_type != error_mark_node)
11949 make_friend_class (type, friend_type, /*complain=*/false);
11950
11951 if (adjust_processing_template_decl)
11952 --processing_template_decl;
11953 }
11954 else
11955 {
11956 /* Build new DECL_FRIENDLIST. */
11957 tree r;
11958
11959 /* The file and line for this declaration, to
11960 assist in error message reporting. Since we
11961 called push_tinst_level above, we don't need to
11962 restore these. */
11963 input_location = DECL_SOURCE_LOCATION (t);
11964
11965 if (TREE_CODE (t) == TEMPLATE_DECL)
11966 {
11967 ++processing_template_decl;
11968 push_deferring_access_checks (dk_no_check);
11969 }
11970
11971 r = tsubst_friend_function (t, args);
11972 add_friend (type, r, /*complain=*/false);
11973 if (TREE_CODE (t) == TEMPLATE_DECL)
11974 {
11975 pop_deferring_access_checks ();
11976 --processing_template_decl;
11977 }
11978 }
11979 }
11980 }
11981
11982 if (fn_context)
11983 {
11984 /* Restore these before substituting into the lambda capture
11985 initializers. */
11986 cp_unevaluated_operand = saved_unevaluated_operand;
11987 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11988 }
11989
11990 /* Set the file and line number information to whatever is given for
11991 the class itself. This puts error messages involving generated
11992 implicit functions at a predictable point, and the same point
11993 that would be used for non-template classes. */
11994 input_location = DECL_SOURCE_LOCATION (typedecl);
11995
11996 unreverse_member_declarations (type);
11997 finish_struct_1 (type);
11998 TYPE_BEING_DEFINED (type) = 0;
11999
12000 /* We don't instantiate default arguments for member functions. 14.7.1:
12001
12002 The implicit instantiation of a class template specialization causes
12003 the implicit instantiation of the declarations, but not of the
12004 definitions or default arguments, of the class member functions,
12005 member classes, static data members and member templates.... */
12006
12007 /* Some typedefs referenced from within the template code need to be access
12008 checked at template instantiation time, i.e now. These types were
12009 added to the template at parsing time. Let's get those and perform
12010 the access checks then. */
12011 perform_typedefs_access_check (pattern, args);
12012 perform_deferred_access_checks (tf_warning_or_error);
12013 pop_nested_class ();
12014 maximum_field_alignment = saved_maximum_field_alignment;
12015 if (!fn_context)
12016 pop_from_top_level ();
12017 pop_tinst_level ();
12018
12019 /* The vtable for a template class can be emitted in any translation
12020 unit in which the class is instantiated. When there is no key
12021 method, however, finish_struct_1 will already have added TYPE to
12022 the keyed_classes. */
12023 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12024 vec_safe_push (keyed_classes, type);
12025
12026 return type;
12027 }
12028
12029 /* Wrapper for instantiate_class_template_1. */
12030
12031 tree
12032 instantiate_class_template (tree type)
12033 {
12034 tree ret;
12035 timevar_push (TV_TEMPLATE_INST);
12036 ret = instantiate_class_template_1 (type);
12037 timevar_pop (TV_TEMPLATE_INST);
12038 return ret;
12039 }
12040
12041 tree
12042 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12043 {
12044 tree r;
12045
12046 if (!t)
12047 r = t;
12048 else if (TYPE_P (t))
12049 r = tsubst (t, args, complain, in_decl);
12050 else
12051 {
12052 if (!(complain & tf_warning))
12053 ++c_inhibit_evaluation_warnings;
12054 r = tsubst_expr (t, args, complain, in_decl,
12055 /*integral_constant_expression_p=*/true);
12056 if (!(complain & tf_warning))
12057 --c_inhibit_evaluation_warnings;
12058 }
12059
12060 return r;
12061 }
12062
12063 /* Given a function parameter pack TMPL_PARM and some function parameters
12064 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12065 and set *SPEC_P to point at the next point in the list. */
12066
12067 tree
12068 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12069 {
12070 /* Collect all of the extra "packed" parameters into an
12071 argument pack. */
12072 tree parmvec;
12073 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12074 tree spec_parm = *spec_p;
12075 int i, len;
12076
12077 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12078 if (tmpl_parm
12079 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12080 break;
12081
12082 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12083 parmvec = make_tree_vec (len);
12084 spec_parm = *spec_p;
12085 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12086 {
12087 tree elt = spec_parm;
12088 if (DECL_PACK_P (elt))
12089 elt = make_pack_expansion (elt);
12090 TREE_VEC_ELT (parmvec, i) = elt;
12091 }
12092
12093 /* Build the argument packs. */
12094 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12095 *spec_p = spec_parm;
12096
12097 return argpack;
12098 }
12099
12100 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12101 NONTYPE_ARGUMENT_PACK. */
12102
12103 static tree
12104 make_fnparm_pack (tree spec_parm)
12105 {
12106 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12107 }
12108
12109 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12110 pack expansion with no extra args, 2 if it has extra args, or 0
12111 if it is not a pack expansion. */
12112
12113 static int
12114 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12115 {
12116 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12117 /* We're being called before this happens in tsubst_pack_expansion. */
12118 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12119 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12120 if (i >= TREE_VEC_LENGTH (vec))
12121 return 0;
12122 tree elt = TREE_VEC_ELT (vec, i);
12123 if (DECL_P (elt))
12124 /* A decl pack is itself an expansion. */
12125 elt = TREE_TYPE (elt);
12126 if (!PACK_EXPANSION_P (elt))
12127 return 0;
12128 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12129 return 2;
12130 return 1;
12131 }
12132
12133
12134 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12135
12136 static tree
12137 make_argument_pack_select (tree arg_pack, unsigned index)
12138 {
12139 tree aps = make_node (ARGUMENT_PACK_SELECT);
12140
12141 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12142 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12143
12144 return aps;
12145 }
12146
12147 /* This is a subroutine of tsubst_pack_expansion.
12148
12149 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12150 mechanism to store the (non complete list of) arguments of the
12151 substitution and return a non substituted pack expansion, in order
12152 to wait for when we have enough arguments to really perform the
12153 substitution. */
12154
12155 static bool
12156 use_pack_expansion_extra_args_p (tree parm_packs,
12157 int arg_pack_len,
12158 bool has_empty_arg)
12159 {
12160 /* If one pack has an expansion and another pack has a normal
12161 argument or if one pack has an empty argument and an another
12162 one hasn't then tsubst_pack_expansion cannot perform the
12163 substitution and need to fall back on the
12164 PACK_EXPANSION_EXTRA mechanism. */
12165 if (parm_packs == NULL_TREE)
12166 return false;
12167 else if (has_empty_arg)
12168 {
12169 /* If all the actual packs are pack expansions, we can still
12170 subsitute directly. */
12171 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12172 {
12173 tree a = TREE_VALUE (p);
12174 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12175 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12176 a = ARGUMENT_PACK_ARGS (a);
12177 if (TREE_VEC_LENGTH (a) == 1)
12178 a = TREE_VEC_ELT (a, 0);
12179 if (PACK_EXPANSION_P (a))
12180 continue;
12181 return true;
12182 }
12183 return false;
12184 }
12185
12186 bool has_expansion_arg = false;
12187 for (int i = 0 ; i < arg_pack_len; ++i)
12188 {
12189 bool has_non_expansion_arg = false;
12190 for (tree parm_pack = parm_packs;
12191 parm_pack;
12192 parm_pack = TREE_CHAIN (parm_pack))
12193 {
12194 tree arg = TREE_VALUE (parm_pack);
12195
12196 int exp = argument_pack_element_is_expansion_p (arg, i);
12197 if (exp == 2)
12198 /* We can't substitute a pack expansion with extra args into
12199 our pattern. */
12200 return true;
12201 else if (exp)
12202 has_expansion_arg = true;
12203 else
12204 has_non_expansion_arg = true;
12205 }
12206
12207 if (has_expansion_arg && has_non_expansion_arg)
12208 return true;
12209 }
12210 return false;
12211 }
12212
12213 /* [temp.variadic]/6 says that:
12214
12215 The instantiation of a pack expansion [...]
12216 produces a list E1,E2, ..., En, where N is the number of elements
12217 in the pack expansion parameters.
12218
12219 This subroutine of tsubst_pack_expansion produces one of these Ei.
12220
12221 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12222 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12223 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12224 INDEX is the index 'i' of the element Ei to produce. ARGS,
12225 COMPLAIN, and IN_DECL are the same parameters as for the
12226 tsubst_pack_expansion function.
12227
12228 The function returns the resulting Ei upon successful completion,
12229 or error_mark_node.
12230
12231 Note that this function possibly modifies the ARGS parameter, so
12232 it's the responsibility of the caller to restore it. */
12233
12234 static tree
12235 gen_elem_of_pack_expansion_instantiation (tree pattern,
12236 tree parm_packs,
12237 unsigned index,
12238 tree args /* This parm gets
12239 modified. */,
12240 tsubst_flags_t complain,
12241 tree in_decl)
12242 {
12243 tree t;
12244 bool ith_elem_is_expansion = false;
12245
12246 /* For each parameter pack, change the substitution of the parameter
12247 pack to the ith argument in its argument pack, then expand the
12248 pattern. */
12249 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12250 {
12251 tree parm = TREE_PURPOSE (pack);
12252 tree arg_pack = TREE_VALUE (pack);
12253 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12254
12255 ith_elem_is_expansion |=
12256 argument_pack_element_is_expansion_p (arg_pack, index);
12257
12258 /* Select the Ith argument from the pack. */
12259 if (TREE_CODE (parm) == PARM_DECL
12260 || VAR_P (parm)
12261 || TREE_CODE (parm) == FIELD_DECL)
12262 {
12263 if (index == 0)
12264 {
12265 aps = make_argument_pack_select (arg_pack, index);
12266 if (!mark_used (parm, complain) && !(complain & tf_error))
12267 return error_mark_node;
12268 register_local_specialization (aps, parm);
12269 }
12270 else
12271 aps = retrieve_local_specialization (parm);
12272 }
12273 else
12274 {
12275 int idx, level;
12276 template_parm_level_and_index (parm, &level, &idx);
12277
12278 if (index == 0)
12279 {
12280 aps = make_argument_pack_select (arg_pack, index);
12281 /* Update the corresponding argument. */
12282 TMPL_ARG (args, level, idx) = aps;
12283 }
12284 else
12285 /* Re-use the ARGUMENT_PACK_SELECT. */
12286 aps = TMPL_ARG (args, level, idx);
12287 }
12288 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12289 }
12290
12291 /* Substitute into the PATTERN with the (possibly altered)
12292 arguments. */
12293 if (pattern == in_decl)
12294 /* Expanding a fixed parameter pack from
12295 coerce_template_parameter_pack. */
12296 t = tsubst_decl (pattern, args, complain);
12297 else if (pattern == error_mark_node)
12298 t = error_mark_node;
12299 else if (!TYPE_P (pattern))
12300 t = tsubst_expr (pattern, args, complain, in_decl,
12301 /*integral_constant_expression_p=*/false);
12302 else
12303 t = tsubst (pattern, args, complain, in_decl);
12304
12305 /* If the Ith argument pack element is a pack expansion, then
12306 the Ith element resulting from the substituting is going to
12307 be a pack expansion as well. */
12308 if (ith_elem_is_expansion)
12309 t = make_pack_expansion (t, complain);
12310
12311 return t;
12312 }
12313
12314 /* When the unexpanded parameter pack in a fold expression expands to an empty
12315 sequence, the value of the expression is as follows; the program is
12316 ill-formed if the operator is not listed in this table.
12317
12318 && true
12319 || false
12320 , void() */
12321
12322 tree
12323 expand_empty_fold (tree t, tsubst_flags_t complain)
12324 {
12325 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12326 if (!FOLD_EXPR_MODIFY_P (t))
12327 switch (code)
12328 {
12329 case TRUTH_ANDIF_EXPR:
12330 return boolean_true_node;
12331 case TRUTH_ORIF_EXPR:
12332 return boolean_false_node;
12333 case COMPOUND_EXPR:
12334 return void_node;
12335 default:
12336 break;
12337 }
12338
12339 if (complain & tf_error)
12340 error_at (location_of (t),
12341 "fold of empty expansion over %O", code);
12342 return error_mark_node;
12343 }
12344
12345 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12346 form an expression that combines the two terms using the
12347 operator of T. */
12348
12349 static tree
12350 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12351 {
12352 tree op = FOLD_EXPR_OP (t);
12353 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12354
12355 // Handle compound assignment operators.
12356 if (FOLD_EXPR_MODIFY_P (t))
12357 return build_x_modify_expr (input_location, left, code, right, complain);
12358
12359 switch (code)
12360 {
12361 case COMPOUND_EXPR:
12362 return build_x_compound_expr (input_location, left, right, complain);
12363 default:
12364 return build_x_binary_op (input_location, code,
12365 left, TREE_CODE (left),
12366 right, TREE_CODE (right),
12367 /*overload=*/NULL,
12368 complain);
12369 }
12370 }
12371
12372 /* Substitute ARGS into the pack of a fold expression T. */
12373
12374 static inline tree
12375 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12376 {
12377 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12378 }
12379
12380 /* Substitute ARGS into the pack of a fold expression T. */
12381
12382 static inline tree
12383 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12384 {
12385 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12386 }
12387
12388 /* Expand a PACK of arguments into a grouped as left fold.
12389 Given a pack containing elements A0, A1, ..., An and an
12390 operator @, this builds the expression:
12391
12392 ((A0 @ A1) @ A2) ... @ An
12393
12394 Note that PACK must not be empty.
12395
12396 The operator is defined by the original fold expression T. */
12397
12398 static tree
12399 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12400 {
12401 tree left = TREE_VEC_ELT (pack, 0);
12402 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12403 {
12404 tree right = TREE_VEC_ELT (pack, i);
12405 left = fold_expression (t, left, right, complain);
12406 }
12407 return left;
12408 }
12409
12410 /* Substitute into a unary left fold expression. */
12411
12412 static tree
12413 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12414 tree in_decl)
12415 {
12416 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12417 if (pack == error_mark_node)
12418 return error_mark_node;
12419 if (PACK_EXPANSION_P (pack))
12420 {
12421 tree r = copy_node (t);
12422 FOLD_EXPR_PACK (r) = pack;
12423 return r;
12424 }
12425 if (TREE_VEC_LENGTH (pack) == 0)
12426 return expand_empty_fold (t, complain);
12427 else
12428 return expand_left_fold (t, pack, complain);
12429 }
12430
12431 /* Substitute into a binary left fold expression.
12432
12433 Do ths by building a single (non-empty) vector of argumnts and
12434 building the expression from those elements. */
12435
12436 static tree
12437 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12438 tree in_decl)
12439 {
12440 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12441 if (pack == error_mark_node)
12442 return error_mark_node;
12443 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12444 if (init == error_mark_node)
12445 return error_mark_node;
12446
12447 if (PACK_EXPANSION_P (pack))
12448 {
12449 tree r = copy_node (t);
12450 FOLD_EXPR_PACK (r) = pack;
12451 FOLD_EXPR_INIT (r) = init;
12452 return r;
12453 }
12454
12455 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12456 TREE_VEC_ELT (vec, 0) = init;
12457 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12458 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12459
12460 return expand_left_fold (t, vec, complain);
12461 }
12462
12463 /* Expand a PACK of arguments into a grouped as right fold.
12464 Given a pack containing elementns A0, A1, ..., and an
12465 operator @, this builds the expression:
12466
12467 A0@ ... (An-2 @ (An-1 @ An))
12468
12469 Note that PACK must not be empty.
12470
12471 The operator is defined by the original fold expression T. */
12472
12473 tree
12474 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12475 {
12476 // Build the expression.
12477 int n = TREE_VEC_LENGTH (pack);
12478 tree right = TREE_VEC_ELT (pack, n - 1);
12479 for (--n; n != 0; --n)
12480 {
12481 tree left = TREE_VEC_ELT (pack, n - 1);
12482 right = fold_expression (t, left, right, complain);
12483 }
12484 return right;
12485 }
12486
12487 /* Substitute into a unary right fold expression. */
12488
12489 static tree
12490 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12491 tree in_decl)
12492 {
12493 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12494 if (pack == error_mark_node)
12495 return error_mark_node;
12496 if (PACK_EXPANSION_P (pack))
12497 {
12498 tree r = copy_node (t);
12499 FOLD_EXPR_PACK (r) = pack;
12500 return r;
12501 }
12502 if (TREE_VEC_LENGTH (pack) == 0)
12503 return expand_empty_fold (t, complain);
12504 else
12505 return expand_right_fold (t, pack, complain);
12506 }
12507
12508 /* Substitute into a binary right fold expression.
12509
12510 Do ths by building a single (non-empty) vector of arguments and
12511 building the expression from those elements. */
12512
12513 static tree
12514 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12515 tree in_decl)
12516 {
12517 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12518 if (pack == error_mark_node)
12519 return error_mark_node;
12520 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12521 if (init == error_mark_node)
12522 return error_mark_node;
12523
12524 if (PACK_EXPANSION_P (pack))
12525 {
12526 tree r = copy_node (t);
12527 FOLD_EXPR_PACK (r) = pack;
12528 FOLD_EXPR_INIT (r) = init;
12529 return r;
12530 }
12531
12532 int n = TREE_VEC_LENGTH (pack);
12533 tree vec = make_tree_vec (n + 1);
12534 for (int i = 0; i < n; ++i)
12535 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12536 TREE_VEC_ELT (vec, n) = init;
12537
12538 return expand_right_fold (t, vec, complain);
12539 }
12540
12541 /* Walk through the pattern of a pack expansion, adding everything in
12542 local_specializations to a list. */
12543
12544 class el_data
12545 {
12546 public:
12547 hash_set<tree> internal;
12548 tree extra;
12549 tsubst_flags_t complain;
12550
12551 el_data (tsubst_flags_t c)
12552 : extra (NULL_TREE), complain (c) {}
12553 };
12554 static tree
12555 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12556 {
12557 el_data &data = *reinterpret_cast<el_data*>(data_);
12558 tree *extra = &data.extra;
12559 tsubst_flags_t complain = data.complain;
12560
12561 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12562 /* Remember local typedefs (85214). */
12563 tp = &TYPE_NAME (*tp);
12564
12565 if (TREE_CODE (*tp) == DECL_EXPR)
12566 data.internal.add (DECL_EXPR_DECL (*tp));
12567 else if (tree spec = retrieve_local_specialization (*tp))
12568 {
12569 if (data.internal.contains (*tp))
12570 /* Don't mess with variables declared within the pattern. */
12571 return NULL_TREE;
12572 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12573 {
12574 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12575 tree args = ARGUMENT_PACK_ARGS (spec);
12576 if (TREE_VEC_LENGTH (args) == 1)
12577 {
12578 tree elt = TREE_VEC_ELT (args, 0);
12579 if (PACK_EXPANSION_P (elt))
12580 elt = PACK_EXPANSION_PATTERN (elt);
12581 if (DECL_PACK_P (elt))
12582 spec = elt;
12583 }
12584 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12585 {
12586 /* Handle lambda capture here, since we aren't doing any
12587 substitution now, and so tsubst_copy won't call
12588 process_outer_var_ref. */
12589 tree args = ARGUMENT_PACK_ARGS (spec);
12590 int len = TREE_VEC_LENGTH (args);
12591 for (int i = 0; i < len; ++i)
12592 {
12593 tree arg = TREE_VEC_ELT (args, i);
12594 tree carg = arg;
12595 if (outer_automatic_var_p (arg))
12596 carg = process_outer_var_ref (arg, complain);
12597 if (carg != arg)
12598 {
12599 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12600 proxies. */
12601 if (i == 0)
12602 {
12603 spec = copy_node (spec);
12604 args = copy_node (args);
12605 SET_ARGUMENT_PACK_ARGS (spec, args);
12606 register_local_specialization (spec, *tp);
12607 }
12608 TREE_VEC_ELT (args, i) = carg;
12609 }
12610 }
12611 }
12612 }
12613 if (outer_automatic_var_p (spec))
12614 spec = process_outer_var_ref (spec, complain);
12615 *extra = tree_cons (*tp, spec, *extra);
12616 }
12617 return NULL_TREE;
12618 }
12619 static tree
12620 extract_local_specs (tree pattern, tsubst_flags_t complain)
12621 {
12622 el_data data (complain);
12623 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12624 return data.extra;
12625 }
12626
12627 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12628 for use in PACK_EXPANSION_EXTRA_ARGS. */
12629
12630 tree
12631 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12632 {
12633 tree extra = args;
12634 if (local_specializations)
12635 if (tree locals = extract_local_specs (pattern, complain))
12636 extra = tree_cons (NULL_TREE, extra, locals);
12637 return extra;
12638 }
12639
12640 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12641 normal template args to ARGS. */
12642
12643 tree
12644 add_extra_args (tree extra, tree args)
12645 {
12646 if (extra && TREE_CODE (extra) == TREE_LIST)
12647 {
12648 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12649 {
12650 /* The partial instantiation involved local declarations collected in
12651 extract_local_specs; map from the general template to our local
12652 context. */
12653 tree gen = TREE_PURPOSE (elt);
12654 tree inst = TREE_VALUE (elt);
12655 if (DECL_P (inst))
12656 if (tree local = retrieve_local_specialization (inst))
12657 inst = local;
12658 /* else inst is already a full instantiation of the pack. */
12659 register_local_specialization (inst, gen);
12660 }
12661 gcc_assert (!TREE_PURPOSE (extra));
12662 extra = TREE_VALUE (extra);
12663 }
12664 #if 1
12665 /* I think we should always be able to substitute dependent args into the
12666 pattern. If that turns out to be incorrect in some cases, enable the
12667 alternate code (and add complain/in_decl parms to this function). */
12668 gcc_checking_assert (!uses_template_parms (extra));
12669 #else
12670 if (!uses_template_parms (extra))
12671 {
12672 gcc_unreachable ();
12673 extra = tsubst_template_args (extra, args, complain, in_decl);
12674 args = add_outermost_template_args (args, extra);
12675 }
12676 else
12677 #endif
12678 args = add_to_template_args (extra, args);
12679 return args;
12680 }
12681
12682 /* Substitute ARGS into T, which is an pack expansion
12683 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12684 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12685 (if only a partial substitution could be performed) or
12686 ERROR_MARK_NODE if there was an error. */
12687 tree
12688 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12689 tree in_decl)
12690 {
12691 tree pattern;
12692 tree pack, packs = NULL_TREE;
12693 bool unsubstituted_packs = false;
12694 bool unsubstituted_fn_pack = false;
12695 int i, len = -1;
12696 tree result;
12697 hash_map<tree, tree> *saved_local_specializations = NULL;
12698 bool need_local_specializations = false;
12699 int levels;
12700
12701 gcc_assert (PACK_EXPANSION_P (t));
12702 pattern = PACK_EXPANSION_PATTERN (t);
12703
12704 /* Add in any args remembered from an earlier partial instantiation. */
12705 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12706
12707 levels = TMPL_ARGS_DEPTH (args);
12708
12709 /* Determine the argument packs that will instantiate the parameter
12710 packs used in the expansion expression. While we're at it,
12711 compute the number of arguments to be expanded and make sure it
12712 is consistent. */
12713 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12714 pack = TREE_CHAIN (pack))
12715 {
12716 tree parm_pack = TREE_VALUE (pack);
12717 tree arg_pack = NULL_TREE;
12718 tree orig_arg = NULL_TREE;
12719 int level = 0;
12720
12721 if (TREE_CODE (parm_pack) == BASES)
12722 {
12723 gcc_assert (parm_pack == pattern);
12724 if (BASES_DIRECT (parm_pack))
12725 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12726 args, complain,
12727 in_decl, false),
12728 complain);
12729 else
12730 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12731 args, complain, in_decl,
12732 false), complain);
12733 }
12734 else if (builtin_pack_call_p (parm_pack))
12735 {
12736 if (parm_pack != pattern)
12737 {
12738 if (complain & tf_error)
12739 sorry ("%qE is not the entire pattern of the pack expansion",
12740 parm_pack);
12741 return error_mark_node;
12742 }
12743 return expand_builtin_pack_call (parm_pack, args,
12744 complain, in_decl);
12745 }
12746 else if (TREE_CODE (parm_pack) == PARM_DECL)
12747 {
12748 /* We know we have correct local_specializations if this
12749 expansion is at function scope, or if we're dealing with a
12750 local parameter in a requires expression; for the latter,
12751 tsubst_requires_expr set it up appropriately. */
12752 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12753 arg_pack = retrieve_local_specialization (parm_pack);
12754 else
12755 /* We can't rely on local_specializations for a parameter
12756 name used later in a function declaration (such as in a
12757 late-specified return type). Even if it exists, it might
12758 have the wrong value for a recursive call. */
12759 need_local_specializations = true;
12760
12761 if (!arg_pack)
12762 {
12763 /* This parameter pack was used in an unevaluated context. Just
12764 make a dummy decl, since it's only used for its type. */
12765 ++cp_unevaluated_operand;
12766 arg_pack = tsubst_decl (parm_pack, args, complain);
12767 --cp_unevaluated_operand;
12768 if (arg_pack && DECL_PACK_P (arg_pack))
12769 /* Partial instantiation of the parm_pack, we can't build
12770 up an argument pack yet. */
12771 arg_pack = NULL_TREE;
12772 else
12773 arg_pack = make_fnparm_pack (arg_pack);
12774 }
12775 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12776 /* This argument pack isn't fully instantiated yet. We set this
12777 flag rather than clear arg_pack because we do want to do the
12778 optimization below, and we don't want to substitute directly
12779 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12780 where it isn't expected). */
12781 unsubstituted_fn_pack = true;
12782 }
12783 else if (is_capture_proxy (parm_pack))
12784 {
12785 arg_pack = retrieve_local_specialization (parm_pack);
12786 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12787 unsubstituted_fn_pack = true;
12788 }
12789 else
12790 {
12791 int idx;
12792 template_parm_level_and_index (parm_pack, &level, &idx);
12793 if (level <= levels)
12794 arg_pack = TMPL_ARG (args, level, idx);
12795 }
12796
12797 orig_arg = arg_pack;
12798 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12799 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12800
12801 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12802 /* This can only happen if we forget to expand an argument
12803 pack somewhere else. Just return an error, silently. */
12804 {
12805 result = make_tree_vec (1);
12806 TREE_VEC_ELT (result, 0) = error_mark_node;
12807 return result;
12808 }
12809
12810 if (arg_pack)
12811 {
12812 int my_len =
12813 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12814
12815 /* Don't bother trying to do a partial substitution with
12816 incomplete packs; we'll try again after deduction. */
12817 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12818 return t;
12819
12820 if (len < 0)
12821 len = my_len;
12822 else if (len != my_len
12823 && !unsubstituted_fn_pack)
12824 {
12825 if (!(complain & tf_error))
12826 /* Fail quietly. */;
12827 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12828 error ("mismatched argument pack lengths while expanding %qT",
12829 pattern);
12830 else
12831 error ("mismatched argument pack lengths while expanding %qE",
12832 pattern);
12833 return error_mark_node;
12834 }
12835
12836 /* Keep track of the parameter packs and their corresponding
12837 argument packs. */
12838 packs = tree_cons (parm_pack, arg_pack, packs);
12839 TREE_TYPE (packs) = orig_arg;
12840 }
12841 else
12842 {
12843 /* We can't substitute for this parameter pack. We use a flag as
12844 well as the missing_level counter because function parameter
12845 packs don't have a level. */
12846 if (!(processing_template_decl || is_auto (parm_pack)))
12847 {
12848 gcc_unreachable ();
12849 }
12850 gcc_assert (processing_template_decl || is_auto (parm_pack));
12851 unsubstituted_packs = true;
12852 }
12853 }
12854
12855 /* If the expansion is just T..., return the matching argument pack, unless
12856 we need to call convert_from_reference on all the elements. This is an
12857 important optimization; see c++/68422. */
12858 if (!unsubstituted_packs
12859 && TREE_PURPOSE (packs) == pattern)
12860 {
12861 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12862
12863 /* If the argument pack is a single pack expansion, pull it out. */
12864 if (TREE_VEC_LENGTH (args) == 1
12865 && pack_expansion_args_count (args))
12866 return TREE_VEC_ELT (args, 0);
12867
12868 /* Types need no adjustment, nor does sizeof..., and if we still have
12869 some pack expansion args we won't do anything yet. */
12870 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12871 || PACK_EXPANSION_SIZEOF_P (t)
12872 || pack_expansion_args_count (args))
12873 return args;
12874 /* Also optimize expression pack expansions if we can tell that the
12875 elements won't have reference type. */
12876 tree type = TREE_TYPE (pattern);
12877 if (type && !TYPE_REF_P (type)
12878 && !PACK_EXPANSION_P (type)
12879 && !WILDCARD_TYPE_P (type))
12880 return args;
12881 /* Otherwise use the normal path so we get convert_from_reference. */
12882 }
12883
12884 /* We cannot expand this expansion expression, because we don't have
12885 all of the argument packs we need. */
12886 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12887 {
12888 /* We got some full packs, but we can't substitute them in until we
12889 have values for all the packs. So remember these until then. */
12890
12891 t = make_pack_expansion (pattern, complain);
12892 PACK_EXPANSION_EXTRA_ARGS (t)
12893 = build_extra_args (pattern, args, complain);
12894 return t;
12895 }
12896 else if (unsubstituted_packs)
12897 {
12898 /* There were no real arguments, we're just replacing a parameter
12899 pack with another version of itself. Substitute into the
12900 pattern and return a PACK_EXPANSION_*. The caller will need to
12901 deal with that. */
12902 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12903 t = tsubst_expr (pattern, args, complain, in_decl,
12904 /*integral_constant_expression_p=*/false);
12905 else
12906 t = tsubst (pattern, args, complain, in_decl);
12907 t = make_pack_expansion (t, complain);
12908 return t;
12909 }
12910
12911 gcc_assert (len >= 0);
12912
12913 if (need_local_specializations)
12914 {
12915 /* We're in a late-specified return type, so create our own local
12916 specializations map; the current map is either NULL or (in the
12917 case of recursive unification) might have bindings that we don't
12918 want to use or alter. */
12919 saved_local_specializations = local_specializations;
12920 local_specializations = new hash_map<tree, tree>;
12921 }
12922
12923 /* For each argument in each argument pack, substitute into the
12924 pattern. */
12925 result = make_tree_vec (len);
12926 tree elem_args = copy_template_args (args);
12927 for (i = 0; i < len; ++i)
12928 {
12929 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12930 i,
12931 elem_args, complain,
12932 in_decl);
12933 TREE_VEC_ELT (result, i) = t;
12934 if (t == error_mark_node)
12935 {
12936 result = error_mark_node;
12937 break;
12938 }
12939 }
12940
12941 /* Update ARGS to restore the substitution from parameter packs to
12942 their argument packs. */
12943 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12944 {
12945 tree parm = TREE_PURPOSE (pack);
12946
12947 if (TREE_CODE (parm) == PARM_DECL
12948 || VAR_P (parm)
12949 || TREE_CODE (parm) == FIELD_DECL)
12950 register_local_specialization (TREE_TYPE (pack), parm);
12951 else
12952 {
12953 int idx, level;
12954
12955 if (TREE_VALUE (pack) == NULL_TREE)
12956 continue;
12957
12958 template_parm_level_and_index (parm, &level, &idx);
12959
12960 /* Update the corresponding argument. */
12961 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12962 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12963 TREE_TYPE (pack);
12964 else
12965 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12966 }
12967 }
12968
12969 if (need_local_specializations)
12970 {
12971 delete local_specializations;
12972 local_specializations = saved_local_specializations;
12973 }
12974
12975 /* If the dependent pack arguments were such that we end up with only a
12976 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12977 if (len == 1 && TREE_CODE (result) == TREE_VEC
12978 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12979 return TREE_VEC_ELT (result, 0);
12980
12981 return result;
12982 }
12983
12984 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12985 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12986 parameter packs; all parms generated from a function parameter pack will
12987 have the same DECL_PARM_INDEX. */
12988
12989 tree
12990 get_pattern_parm (tree parm, tree tmpl)
12991 {
12992 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12993 tree patparm;
12994
12995 if (DECL_ARTIFICIAL (parm))
12996 {
12997 for (patparm = DECL_ARGUMENTS (pattern);
12998 patparm; patparm = DECL_CHAIN (patparm))
12999 if (DECL_ARTIFICIAL (patparm)
13000 && DECL_NAME (parm) == DECL_NAME (patparm))
13001 break;
13002 }
13003 else
13004 {
13005 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13006 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13007 gcc_assert (DECL_PARM_INDEX (patparm)
13008 == DECL_PARM_INDEX (parm));
13009 }
13010
13011 return patparm;
13012 }
13013
13014 /* Make an argument pack out of the TREE_VEC VEC. */
13015
13016 static tree
13017 make_argument_pack (tree vec)
13018 {
13019 tree pack;
13020 tree elt = TREE_VEC_ELT (vec, 0);
13021 if (TYPE_P (elt))
13022 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13023 else
13024 {
13025 pack = make_node (NONTYPE_ARGUMENT_PACK);
13026 TREE_CONSTANT (pack) = 1;
13027 }
13028 SET_ARGUMENT_PACK_ARGS (pack, vec);
13029 return pack;
13030 }
13031
13032 /* Return an exact copy of template args T that can be modified
13033 independently. */
13034
13035 static tree
13036 copy_template_args (tree t)
13037 {
13038 if (t == error_mark_node)
13039 return t;
13040
13041 int len = TREE_VEC_LENGTH (t);
13042 tree new_vec = make_tree_vec (len);
13043
13044 for (int i = 0; i < len; ++i)
13045 {
13046 tree elt = TREE_VEC_ELT (t, i);
13047 if (elt && TREE_CODE (elt) == TREE_VEC)
13048 elt = copy_template_args (elt);
13049 TREE_VEC_ELT (new_vec, i) = elt;
13050 }
13051
13052 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13053 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13054
13055 return new_vec;
13056 }
13057
13058 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13059
13060 tree
13061 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13062 tree in_decl)
13063 {
13064 /* Substitute into each of the arguments. */
13065 tree new_arg = TYPE_P (orig_arg)
13066 ? cxx_make_type (TREE_CODE (orig_arg))
13067 : make_node (TREE_CODE (orig_arg));
13068
13069 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13070 args, complain, in_decl);
13071 if (pack_args == error_mark_node)
13072 new_arg = error_mark_node;
13073 else
13074 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13075
13076 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13077 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13078
13079 return new_arg;
13080 }
13081
13082 /* Substitute ARGS into the vector or list of template arguments T. */
13083
13084 tree
13085 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13086 {
13087 tree orig_t = t;
13088 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13089 tree *elts;
13090
13091 if (t == error_mark_node)
13092 return error_mark_node;
13093
13094 len = TREE_VEC_LENGTH (t);
13095 elts = XALLOCAVEC (tree, len);
13096
13097 for (i = 0; i < len; i++)
13098 {
13099 tree orig_arg = TREE_VEC_ELT (t, i);
13100 tree new_arg;
13101
13102 if (TREE_CODE (orig_arg) == TREE_VEC)
13103 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13104 else if (PACK_EXPANSION_P (orig_arg))
13105 {
13106 /* Substitute into an expansion expression. */
13107 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13108
13109 if (TREE_CODE (new_arg) == TREE_VEC)
13110 /* Add to the expanded length adjustment the number of
13111 expanded arguments. We subtract one from this
13112 measurement, because the argument pack expression
13113 itself is already counted as 1 in
13114 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13115 the argument pack is empty. */
13116 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13117 }
13118 else if (ARGUMENT_PACK_P (orig_arg))
13119 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13120 else
13121 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13122
13123 if (new_arg == error_mark_node)
13124 return error_mark_node;
13125
13126 elts[i] = new_arg;
13127 if (new_arg != orig_arg)
13128 need_new = 1;
13129 }
13130
13131 if (!need_new)
13132 return t;
13133
13134 /* Make space for the expanded arguments coming from template
13135 argument packs. */
13136 t = make_tree_vec (len + expanded_len_adjust);
13137 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13138 arguments for a member template.
13139 In that case each TREE_VEC in ORIG_T represents a level of template
13140 arguments, and ORIG_T won't carry any non defaulted argument count.
13141 It will rather be the nested TREE_VECs that will carry one.
13142 In other words, ORIG_T carries a non defaulted argument count only
13143 if it doesn't contain any nested TREE_VEC. */
13144 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13145 {
13146 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13147 count += expanded_len_adjust;
13148 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13149 }
13150 for (i = 0, out = 0; i < len; i++)
13151 {
13152 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13153 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13154 && TREE_CODE (elts[i]) == TREE_VEC)
13155 {
13156 int idx;
13157
13158 /* Now expand the template argument pack "in place". */
13159 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13160 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13161 }
13162 else
13163 {
13164 TREE_VEC_ELT (t, out) = elts[i];
13165 out++;
13166 }
13167 }
13168
13169 return t;
13170 }
13171
13172 /* Substitute ARGS into one level PARMS of template parameters. */
13173
13174 static tree
13175 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13176 {
13177 if (parms == error_mark_node)
13178 return error_mark_node;
13179
13180 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13181
13182 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13183 {
13184 tree tuple = TREE_VEC_ELT (parms, i);
13185
13186 if (tuple == error_mark_node)
13187 continue;
13188
13189 TREE_VEC_ELT (new_vec, i) =
13190 tsubst_template_parm (tuple, args, complain);
13191 }
13192
13193 return new_vec;
13194 }
13195
13196 /* Return the result of substituting ARGS into the template parameters
13197 given by PARMS. If there are m levels of ARGS and m + n levels of
13198 PARMS, then the result will contain n levels of PARMS. For
13199 example, if PARMS is `template <class T> template <class U>
13200 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13201 result will be `template <int*, double, class V>'. */
13202
13203 static tree
13204 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13205 {
13206 tree r = NULL_TREE;
13207 tree* new_parms;
13208
13209 /* When substituting into a template, we must set
13210 PROCESSING_TEMPLATE_DECL as the template parameters may be
13211 dependent if they are based on one-another, and the dependency
13212 predicates are short-circuit outside of templates. */
13213 ++processing_template_decl;
13214
13215 for (new_parms = &r;
13216 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13217 new_parms = &(TREE_CHAIN (*new_parms)),
13218 parms = TREE_CHAIN (parms))
13219 {
13220 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13221 args, complain);
13222 *new_parms =
13223 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13224 - TMPL_ARGS_DEPTH (args)),
13225 new_vec, NULL_TREE);
13226 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13227 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13228 }
13229
13230 --processing_template_decl;
13231
13232 return r;
13233 }
13234
13235 /* Return the result of substituting ARGS into one template parameter
13236 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13237 parameter and which TREE_PURPOSE is the default argument of the
13238 template parameter. */
13239
13240 static tree
13241 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13242 {
13243 tree default_value, parm_decl;
13244
13245 if (args == NULL_TREE
13246 || t == NULL_TREE
13247 || t == error_mark_node)
13248 return t;
13249
13250 gcc_assert (TREE_CODE (t) == TREE_LIST);
13251
13252 default_value = TREE_PURPOSE (t);
13253 parm_decl = TREE_VALUE (t);
13254 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13255
13256 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13257 if (TREE_CODE (parm_decl) == PARM_DECL
13258 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13259 parm_decl = error_mark_node;
13260 default_value = tsubst_template_arg (default_value, args,
13261 complain, NULL_TREE);
13262 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13263
13264 tree r = build_tree_list (default_value, parm_decl);
13265 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13266 return r;
13267 }
13268
13269 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13270 type T. If T is not an aggregate or enumeration type, it is
13271 handled as if by tsubst. IN_DECL is as for tsubst. If
13272 ENTERING_SCOPE is nonzero, T is the context for a template which
13273 we are presently tsubst'ing. Return the substituted value. */
13274
13275 static tree
13276 tsubst_aggr_type (tree t,
13277 tree args,
13278 tsubst_flags_t complain,
13279 tree in_decl,
13280 int entering_scope)
13281 {
13282 if (t == NULL_TREE)
13283 return NULL_TREE;
13284
13285 switch (TREE_CODE (t))
13286 {
13287 case RECORD_TYPE:
13288 if (TYPE_PTRMEMFUNC_P (t))
13289 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13290
13291 /* Fall through. */
13292 case ENUMERAL_TYPE:
13293 case UNION_TYPE:
13294 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13295 {
13296 tree argvec;
13297 tree context;
13298 tree r;
13299
13300 /* In "sizeof(X<I>)" we need to evaluate "I". */
13301 cp_evaluated ev;
13302
13303 /* First, determine the context for the type we are looking
13304 up. */
13305 context = TYPE_CONTEXT (t);
13306 if (context && TYPE_P (context))
13307 {
13308 context = tsubst_aggr_type (context, args, complain,
13309 in_decl, /*entering_scope=*/1);
13310 /* If context is a nested class inside a class template,
13311 it may still need to be instantiated (c++/33959). */
13312 context = complete_type (context);
13313 }
13314
13315 /* Then, figure out what arguments are appropriate for the
13316 type we are trying to find. For example, given:
13317
13318 template <class T> struct S;
13319 template <class T, class U> void f(T, U) { S<U> su; }
13320
13321 and supposing that we are instantiating f<int, double>,
13322 then our ARGS will be {int, double}, but, when looking up
13323 S we only want {double}. */
13324 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13325 complain, in_decl);
13326 if (argvec == error_mark_node)
13327 r = error_mark_node;
13328 else
13329 {
13330 r = lookup_template_class (t, argvec, in_decl, context,
13331 entering_scope, complain);
13332 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13333 }
13334
13335 return r;
13336 }
13337 else
13338 /* This is not a template type, so there's nothing to do. */
13339 return t;
13340
13341 default:
13342 return tsubst (t, args, complain, in_decl);
13343 }
13344 }
13345
13346 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13347
13348 /* Substitute into the default argument ARG (a default argument for
13349 FN), which has the indicated TYPE. */
13350
13351 tree
13352 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13353 tsubst_flags_t complain)
13354 {
13355 int errs = errorcount + sorrycount;
13356
13357 /* This can happen in invalid code. */
13358 if (TREE_CODE (arg) == DEFERRED_PARSE)
13359 return arg;
13360
13361 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13362 parm = chain_index (parmnum, parm);
13363 tree parmtype = TREE_TYPE (parm);
13364 if (DECL_BY_REFERENCE (parm))
13365 parmtype = TREE_TYPE (parmtype);
13366 if (parmtype == error_mark_node)
13367 return error_mark_node;
13368
13369 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13370
13371 tree *slot;
13372 if (defarg_inst && (slot = defarg_inst->get (parm)))
13373 return *slot;
13374
13375 /* This default argument came from a template. Instantiate the
13376 default argument here, not in tsubst. In the case of
13377 something like:
13378
13379 template <class T>
13380 struct S {
13381 static T t();
13382 void f(T = t());
13383 };
13384
13385 we must be careful to do name lookup in the scope of S<T>,
13386 rather than in the current class. */
13387 push_to_top_level ();
13388 push_access_scope (fn);
13389 push_deferring_access_checks (dk_no_deferred);
13390 start_lambda_scope (parm);
13391
13392 /* The default argument expression may cause implicitly defined
13393 member functions to be synthesized, which will result in garbage
13394 collection. We must treat this situation as if we were within
13395 the body of function so as to avoid collecting live data on the
13396 stack. */
13397 ++function_depth;
13398 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13399 complain, NULL_TREE,
13400 /*integral_constant_expression_p=*/false);
13401 --function_depth;
13402
13403 finish_lambda_scope ();
13404
13405 /* Make sure the default argument is reasonable. */
13406 arg = check_default_argument (type, arg, complain);
13407
13408 if (errorcount+sorrycount > errs
13409 && (complain & tf_warning_or_error))
13410 inform (input_location,
13411 " when instantiating default argument for call to %qD", fn);
13412
13413 pop_deferring_access_checks ();
13414 pop_access_scope (fn);
13415 pop_from_top_level ();
13416
13417 if (arg != error_mark_node && !cp_unevaluated_operand)
13418 {
13419 if (!defarg_inst)
13420 defarg_inst = decl_tree_cache_map::create_ggc (37);
13421 defarg_inst->put (parm, arg);
13422 }
13423
13424 return arg;
13425 }
13426
13427 /* Substitute into all the default arguments for FN. */
13428
13429 static void
13430 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13431 {
13432 tree arg;
13433 tree tmpl_args;
13434
13435 tmpl_args = DECL_TI_ARGS (fn);
13436
13437 /* If this function is not yet instantiated, we certainly don't need
13438 its default arguments. */
13439 if (uses_template_parms (tmpl_args))
13440 return;
13441 /* Don't do this again for clones. */
13442 if (DECL_CLONED_FUNCTION_P (fn))
13443 return;
13444
13445 int i = 0;
13446 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13447 arg;
13448 arg = TREE_CHAIN (arg), ++i)
13449 if (TREE_PURPOSE (arg))
13450 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13451 TREE_VALUE (arg),
13452 TREE_PURPOSE (arg),
13453 complain);
13454 }
13455
13456 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13457 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13458
13459 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13460
13461 void
13462 store_explicit_specifier (tree v, tree t)
13463 {
13464 if (!explicit_specifier_map)
13465 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13466 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13467 explicit_specifier_map->put (v, t);
13468 }
13469
13470 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13471
13472 static tree
13473 lookup_explicit_specifier (tree v)
13474 {
13475 return *explicit_specifier_map->get (v);
13476 }
13477
13478 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13479
13480 static tree
13481 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13482 tree lambda_fntype)
13483 {
13484 tree gen_tmpl, argvec;
13485 hashval_t hash = 0;
13486 tree in_decl = t;
13487
13488 /* Nobody should be tsubst'ing into non-template functions. */
13489 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13490
13491 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13492 {
13493 /* If T is not dependent, just return it. */
13494 if (!uses_template_parms (DECL_TI_ARGS (t))
13495 && !LAMBDA_FUNCTION_P (t))
13496 return t;
13497
13498 /* Calculate the most general template of which R is a
13499 specialization. */
13500 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13501
13502 /* We're substituting a lambda function under tsubst_lambda_expr but not
13503 directly from it; find the matching function we're already inside.
13504 But don't do this if T is a generic lambda with a single level of
13505 template parms, as in that case we're doing a normal instantiation. */
13506 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13507 && (!generic_lambda_fn_p (t)
13508 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13509 return enclosing_instantiation_of (t);
13510
13511 /* Calculate the complete set of arguments used to
13512 specialize R. */
13513 argvec = tsubst_template_args (DECL_TI_ARGS
13514 (DECL_TEMPLATE_RESULT
13515 (DECL_TI_TEMPLATE (t))),
13516 args, complain, in_decl);
13517 if (argvec == error_mark_node)
13518 return error_mark_node;
13519
13520 /* Check to see if we already have this specialization. */
13521 if (!lambda_fntype)
13522 {
13523 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13524 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13525 return spec;
13526 }
13527
13528 /* We can see more levels of arguments than parameters if
13529 there was a specialization of a member template, like
13530 this:
13531
13532 template <class T> struct S { template <class U> void f(); }
13533 template <> template <class U> void S<int>::f(U);
13534
13535 Here, we'll be substituting into the specialization,
13536 because that's where we can find the code we actually
13537 want to generate, but we'll have enough arguments for
13538 the most general template.
13539
13540 We also deal with the peculiar case:
13541
13542 template <class T> struct S {
13543 template <class U> friend void f();
13544 };
13545 template <class U> void f() {}
13546 template S<int>;
13547 template void f<double>();
13548
13549 Here, the ARGS for the instantiation of will be {int,
13550 double}. But, we only need as many ARGS as there are
13551 levels of template parameters in CODE_PATTERN. We are
13552 careful not to get fooled into reducing the ARGS in
13553 situations like:
13554
13555 template <class T> struct S { template <class U> void f(U); }
13556 template <class T> template <> void S<T>::f(int) {}
13557
13558 which we can spot because the pattern will be a
13559 specialization in this case. */
13560 int args_depth = TMPL_ARGS_DEPTH (args);
13561 int parms_depth =
13562 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13563
13564 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13565 args = get_innermost_template_args (args, parms_depth);
13566 }
13567 else
13568 {
13569 /* This special case arises when we have something like this:
13570
13571 template <class T> struct S {
13572 friend void f<int>(int, double);
13573 };
13574
13575 Here, the DECL_TI_TEMPLATE for the friend declaration
13576 will be an IDENTIFIER_NODE. We are being called from
13577 tsubst_friend_function, and we want only to create a
13578 new decl (R) with appropriate types so that we can call
13579 determine_specialization. */
13580 gen_tmpl = NULL_TREE;
13581 argvec = NULL_TREE;
13582 }
13583
13584 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13585 : NULL_TREE);
13586 tree ctx = closure ? closure : DECL_CONTEXT (t);
13587 bool member = ctx && TYPE_P (ctx);
13588
13589 if (member && !closure)
13590 ctx = tsubst_aggr_type (ctx, args,
13591 complain, t, /*entering_scope=*/1);
13592
13593 tree type = (lambda_fntype ? lambda_fntype
13594 : tsubst (TREE_TYPE (t), args,
13595 complain | tf_fndecl_type, in_decl));
13596 if (type == error_mark_node)
13597 return error_mark_node;
13598
13599 /* If we hit excessive deduction depth, the type is bogus even if
13600 it isn't error_mark_node, so don't build a decl. */
13601 if (excessive_deduction_depth)
13602 return error_mark_node;
13603
13604 /* We do NOT check for matching decls pushed separately at this
13605 point, as they may not represent instantiations of this
13606 template, and in any case are considered separate under the
13607 discrete model. */
13608 tree r = copy_decl (t);
13609 DECL_USE_TEMPLATE (r) = 0;
13610 TREE_TYPE (r) = type;
13611 /* Clear out the mangled name and RTL for the instantiation. */
13612 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13613 SET_DECL_RTL (r, NULL);
13614 /* Leave DECL_INITIAL set on deleted instantiations. */
13615 if (!DECL_DELETED_FN (r))
13616 DECL_INITIAL (r) = NULL_TREE;
13617 DECL_CONTEXT (r) = ctx;
13618
13619 /* Handle explicit(dependent-expr). */
13620 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13621 {
13622 tree spec = lookup_explicit_specifier (t);
13623 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13624 /*function_p=*/false,
13625 /*i_c_e_p=*/true);
13626 spec = build_explicit_specifier (spec, complain);
13627 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13628 }
13629
13630 /* OpenMP UDRs have the only argument a reference to the declared
13631 type. We want to diagnose if the declared type is a reference,
13632 which is invalid, but as references to references are usually
13633 quietly merged, diagnose it here. */
13634 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13635 {
13636 tree argtype
13637 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13638 argtype = tsubst (argtype, args, complain, in_decl);
13639 if (TYPE_REF_P (argtype))
13640 error_at (DECL_SOURCE_LOCATION (t),
13641 "reference type %qT in "
13642 "%<#pragma omp declare reduction%>", argtype);
13643 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13644 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13645 argtype);
13646 }
13647
13648 if (member && DECL_CONV_FN_P (r))
13649 /* Type-conversion operator. Reconstruct the name, in
13650 case it's the name of one of the template's parameters. */
13651 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13652
13653 tree parms = DECL_ARGUMENTS (t);
13654 if (closure)
13655 parms = DECL_CHAIN (parms);
13656 parms = tsubst (parms, args, complain, t);
13657 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13658 DECL_CONTEXT (parm) = r;
13659 if (closure)
13660 {
13661 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13662 DECL_CHAIN (tparm) = parms;
13663 parms = tparm;
13664 }
13665 DECL_ARGUMENTS (r) = parms;
13666 DECL_RESULT (r) = NULL_TREE;
13667
13668 TREE_STATIC (r) = 0;
13669 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13670 DECL_EXTERNAL (r) = 1;
13671 /* If this is an instantiation of a function with internal
13672 linkage, we already know what object file linkage will be
13673 assigned to the instantiation. */
13674 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13675 DECL_DEFER_OUTPUT (r) = 0;
13676 DECL_CHAIN (r) = NULL_TREE;
13677 DECL_PENDING_INLINE_INFO (r) = 0;
13678 DECL_PENDING_INLINE_P (r) = 0;
13679 DECL_SAVED_TREE (r) = NULL_TREE;
13680 DECL_STRUCT_FUNCTION (r) = NULL;
13681 TREE_USED (r) = 0;
13682 /* We'll re-clone as appropriate in instantiate_template. */
13683 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13684
13685 /* If we aren't complaining now, return on error before we register
13686 the specialization so that we'll complain eventually. */
13687 if ((complain & tf_error) == 0
13688 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13689 && !grok_op_properties (r, /*complain=*/false))
13690 return error_mark_node;
13691
13692 /* Associate the constraints directly with the instantiation. We
13693 don't substitute through the constraints; that's only done when
13694 they are checked. */
13695 if (tree ci = get_constraints (t))
13696 set_constraints (r, ci);
13697
13698 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13699 SET_DECL_FRIEND_CONTEXT (r,
13700 tsubst (DECL_FRIEND_CONTEXT (t),
13701 args, complain, in_decl));
13702
13703 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13704 this in the special friend case mentioned above where
13705 GEN_TMPL is NULL. */
13706 if (gen_tmpl && !closure)
13707 {
13708 DECL_TEMPLATE_INFO (r)
13709 = build_template_info (gen_tmpl, argvec);
13710 SET_DECL_IMPLICIT_INSTANTIATION (r);
13711
13712 tree new_r
13713 = register_specialization (r, gen_tmpl, argvec, false, hash);
13714 if (new_r != r)
13715 /* We instantiated this while substituting into
13716 the type earlier (template/friend54.C). */
13717 return new_r;
13718
13719 /* We're not supposed to instantiate default arguments
13720 until they are called, for a template. But, for a
13721 declaration like:
13722
13723 template <class T> void f ()
13724 { extern void g(int i = T()); }
13725
13726 we should do the substitution when the template is
13727 instantiated. We handle the member function case in
13728 instantiate_class_template since the default arguments
13729 might refer to other members of the class. */
13730 if (!member
13731 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13732 && !uses_template_parms (argvec))
13733 tsubst_default_arguments (r, complain);
13734 }
13735 else
13736 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13737
13738 /* Copy the list of befriending classes. */
13739 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13740 *friends;
13741 friends = &TREE_CHAIN (*friends))
13742 {
13743 *friends = copy_node (*friends);
13744 TREE_VALUE (*friends)
13745 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13746 }
13747
13748 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13749 {
13750 maybe_retrofit_in_chrg (r);
13751 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13752 return error_mark_node;
13753 /* If this is an instantiation of a member template, clone it.
13754 If it isn't, that'll be handled by
13755 clone_constructors_and_destructors. */
13756 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13757 clone_function_decl (r, /*update_methods=*/false);
13758 }
13759 else if ((complain & tf_error) != 0
13760 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13761 && !grok_op_properties (r, /*complain=*/true))
13762 return error_mark_node;
13763
13764 /* Possibly limit visibility based on template args. */
13765 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13766 if (DECL_VISIBILITY_SPECIFIED (t))
13767 {
13768 DECL_VISIBILITY_SPECIFIED (r) = 0;
13769 DECL_ATTRIBUTES (r)
13770 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13771 }
13772 determine_visibility (r);
13773 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13774 && !processing_template_decl)
13775 defaulted_late_check (r);
13776
13777 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13778 args, complain, in_decl);
13779 if (flag_openmp)
13780 if (tree attr = lookup_attribute ("omp declare variant base",
13781 DECL_ATTRIBUTES (r)))
13782 omp_declare_variant_finalize (r, attr);
13783
13784 return r;
13785 }
13786
13787 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13788
13789 static tree
13790 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13791 tree lambda_fntype)
13792 {
13793 /* We can get here when processing a member function template,
13794 member class template, or template template parameter. */
13795 tree decl = DECL_TEMPLATE_RESULT (t);
13796 tree in_decl = t;
13797 tree spec;
13798 tree tmpl_args;
13799 tree full_args;
13800 tree r;
13801 hashval_t hash = 0;
13802
13803 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13804 {
13805 /* Template template parameter is treated here. */
13806 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13807 if (new_type == error_mark_node)
13808 r = error_mark_node;
13809 /* If we get a real template back, return it. This can happen in
13810 the context of most_specialized_partial_spec. */
13811 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13812 r = new_type;
13813 else
13814 /* The new TEMPLATE_DECL was built in
13815 reduce_template_parm_level. */
13816 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13817 return r;
13818 }
13819
13820 if (!lambda_fntype)
13821 {
13822 /* We might already have an instance of this template.
13823 The ARGS are for the surrounding class type, so the
13824 full args contain the tsubst'd args for the context,
13825 plus the innermost args from the template decl. */
13826 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13827 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13828 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13829 /* Because this is a template, the arguments will still be
13830 dependent, even after substitution. If
13831 PROCESSING_TEMPLATE_DECL is not set, the dependency
13832 predicates will short-circuit. */
13833 ++processing_template_decl;
13834 full_args = tsubst_template_args (tmpl_args, args,
13835 complain, in_decl);
13836 --processing_template_decl;
13837 if (full_args == error_mark_node)
13838 return error_mark_node;
13839
13840 /* If this is a default template template argument,
13841 tsubst might not have changed anything. */
13842 if (full_args == tmpl_args)
13843 return t;
13844
13845 hash = hash_tmpl_and_args (t, full_args);
13846 spec = retrieve_specialization (t, full_args, hash);
13847 if (spec != NULL_TREE)
13848 {
13849 if (TYPE_P (spec))
13850 /* Type partial instantiations are stored as the type by
13851 lookup_template_class_1, not here as the template. */
13852 spec = CLASSTYPE_TI_TEMPLATE (spec);
13853 return spec;
13854 }
13855 }
13856
13857 /* Make a new template decl. It will be similar to the
13858 original, but will record the current template arguments.
13859 We also create a new function declaration, which is just
13860 like the old one, but points to this new template, rather
13861 than the old one. */
13862 r = copy_decl (t);
13863 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13864 DECL_CHAIN (r) = NULL_TREE;
13865
13866 // Build new template info linking to the original template decl.
13867 if (!lambda_fntype)
13868 {
13869 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13870 SET_DECL_IMPLICIT_INSTANTIATION (r);
13871 }
13872 else
13873 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13874
13875 /* The template parameters for this new template are all the
13876 template parameters for the old template, except the
13877 outermost level of parameters. */
13878 DECL_TEMPLATE_PARMS (r)
13879 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13880 complain);
13881
13882 if (TREE_CODE (decl) == TYPE_DECL
13883 && !TYPE_DECL_ALIAS_P (decl))
13884 {
13885 tree new_type;
13886 ++processing_template_decl;
13887 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13888 --processing_template_decl;
13889 if (new_type == error_mark_node)
13890 return error_mark_node;
13891
13892 TREE_TYPE (r) = new_type;
13893 /* For a partial specialization, we need to keep pointing to
13894 the primary template. */
13895 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13896 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13897 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13898 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13899 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13900 }
13901 else
13902 {
13903 tree new_decl;
13904 ++processing_template_decl;
13905 if (TREE_CODE (decl) == FUNCTION_DECL)
13906 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13907 else
13908 new_decl = tsubst (decl, args, complain, in_decl);
13909 --processing_template_decl;
13910 if (new_decl == error_mark_node)
13911 return error_mark_node;
13912
13913 DECL_TEMPLATE_RESULT (r) = new_decl;
13914 TREE_TYPE (r) = TREE_TYPE (new_decl);
13915 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13916 if (lambda_fntype)
13917 {
13918 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13919 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13920 }
13921 else
13922 {
13923 DECL_TI_TEMPLATE (new_decl) = r;
13924 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13925 }
13926 }
13927
13928 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13929 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13930
13931 if (PRIMARY_TEMPLATE_P (t))
13932 DECL_PRIMARY_TEMPLATE (r) = r;
13933
13934 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13935 && !lambda_fntype)
13936 /* Record this non-type partial instantiation. */
13937 register_specialization (r, t,
13938 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13939 false, hash);
13940
13941 return r;
13942 }
13943
13944 /* True if FN is the op() for a lambda in an uninstantiated template. */
13945
13946 bool
13947 lambda_fn_in_template_p (tree fn)
13948 {
13949 if (!fn || !LAMBDA_FUNCTION_P (fn))
13950 return false;
13951 tree closure = DECL_CONTEXT (fn);
13952 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13953 }
13954
13955 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13956 which the above is true. */
13957
13958 bool
13959 instantiated_lambda_fn_p (tree fn)
13960 {
13961 if (!fn || !LAMBDA_FUNCTION_P (fn))
13962 return false;
13963 tree closure = DECL_CONTEXT (fn);
13964 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13965 return LAMBDA_EXPR_INSTANTIATED (lam);
13966 }
13967
13968 /* We're instantiating a variable from template function TCTX. Return the
13969 corresponding current enclosing scope. This gets complicated because lambda
13970 functions in templates are regenerated rather than instantiated, but generic
13971 lambda functions are subsequently instantiated. */
13972
13973 static tree
13974 enclosing_instantiation_of (tree otctx)
13975 {
13976 tree tctx = otctx;
13977 tree fn = current_function_decl;
13978 int lambda_count = 0;
13979
13980 for (; tctx && (lambda_fn_in_template_p (tctx)
13981 || instantiated_lambda_fn_p (tctx));
13982 tctx = decl_function_context (tctx))
13983 ++lambda_count;
13984 for (; fn; fn = decl_function_context (fn))
13985 {
13986 tree ofn = fn;
13987 int flambda_count = 0;
13988 for (; fn && instantiated_lambda_fn_p (fn);
13989 fn = decl_function_context (fn))
13990 ++flambda_count;
13991 if ((fn && DECL_TEMPLATE_INFO (fn))
13992 ? most_general_template (fn) != most_general_template (tctx)
13993 : fn != tctx)
13994 continue;
13995 if (flambda_count != lambda_count)
13996 {
13997 gcc_assert (flambda_count > lambda_count);
13998 for (; flambda_count > lambda_count; --flambda_count)
13999 ofn = decl_function_context (ofn);
14000 }
14001 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14002 || DECL_CONV_FN_P (ofn));
14003 return ofn;
14004 }
14005 gcc_unreachable ();
14006 }
14007
14008 /* Substitute the ARGS into the T, which is a _DECL. Return the
14009 result of the substitution. Issue error and warning messages under
14010 control of COMPLAIN. */
14011
14012 static tree
14013 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14014 {
14015 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14016 location_t saved_loc;
14017 tree r = NULL_TREE;
14018 tree in_decl = t;
14019 hashval_t hash = 0;
14020
14021 /* Set the filename and linenumber to improve error-reporting. */
14022 saved_loc = input_location;
14023 input_location = DECL_SOURCE_LOCATION (t);
14024
14025 switch (TREE_CODE (t))
14026 {
14027 case TEMPLATE_DECL:
14028 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14029 break;
14030
14031 case FUNCTION_DECL:
14032 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14033 break;
14034
14035 case PARM_DECL:
14036 {
14037 tree type = NULL_TREE;
14038 int i, len = 1;
14039 tree expanded_types = NULL_TREE;
14040 tree prev_r = NULL_TREE;
14041 tree first_r = NULL_TREE;
14042
14043 if (DECL_PACK_P (t))
14044 {
14045 /* If there is a local specialization that isn't a
14046 parameter pack, it means that we're doing a "simple"
14047 substitution from inside tsubst_pack_expansion. Just
14048 return the local specialization (which will be a single
14049 parm). */
14050 tree spec = retrieve_local_specialization (t);
14051 if (spec
14052 && TREE_CODE (spec) == PARM_DECL
14053 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14054 RETURN (spec);
14055
14056 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14057 the parameters in this function parameter pack. */
14058 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14059 complain, in_decl);
14060 if (TREE_CODE (expanded_types) == TREE_VEC)
14061 {
14062 len = TREE_VEC_LENGTH (expanded_types);
14063
14064 /* Zero-length parameter packs are boring. Just substitute
14065 into the chain. */
14066 if (len == 0 && !cp_unevaluated_operand)
14067 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14068 TREE_CHAIN (t)));
14069 }
14070 else
14071 {
14072 /* All we did was update the type. Make a note of that. */
14073 type = expanded_types;
14074 expanded_types = NULL_TREE;
14075 }
14076 }
14077
14078 /* Loop through all of the parameters we'll build. When T is
14079 a function parameter pack, LEN is the number of expanded
14080 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14081 r = NULL_TREE;
14082 for (i = 0; i < len; ++i)
14083 {
14084 prev_r = r;
14085 r = copy_node (t);
14086 if (DECL_TEMPLATE_PARM_P (t))
14087 SET_DECL_TEMPLATE_PARM_P (r);
14088
14089 if (expanded_types)
14090 /* We're on the Ith parameter of the function parameter
14091 pack. */
14092 {
14093 /* Get the Ith type. */
14094 type = TREE_VEC_ELT (expanded_types, i);
14095
14096 /* Rename the parameter to include the index. */
14097 DECL_NAME (r)
14098 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14099 }
14100 else if (!type)
14101 /* We're dealing with a normal parameter. */
14102 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14103
14104 type = type_decays_to (type);
14105 TREE_TYPE (r) = type;
14106 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14107
14108 if (DECL_INITIAL (r))
14109 {
14110 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14111 DECL_INITIAL (r) = TREE_TYPE (r);
14112 else
14113 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14114 complain, in_decl);
14115 }
14116
14117 DECL_CONTEXT (r) = NULL_TREE;
14118
14119 if (!DECL_TEMPLATE_PARM_P (r))
14120 DECL_ARG_TYPE (r) = type_passed_as (type);
14121
14122 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14123 args, complain, in_decl);
14124
14125 /* Keep track of the first new parameter we
14126 generate. That's what will be returned to the
14127 caller. */
14128 if (!first_r)
14129 first_r = r;
14130
14131 /* Build a proper chain of parameters when substituting
14132 into a function parameter pack. */
14133 if (prev_r)
14134 DECL_CHAIN (prev_r) = r;
14135 }
14136
14137 /* If cp_unevaluated_operand is set, we're just looking for a
14138 single dummy parameter, so don't keep going. */
14139 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14140 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14141 complain, DECL_CHAIN (t));
14142
14143 /* FIRST_R contains the start of the chain we've built. */
14144 r = first_r;
14145 }
14146 break;
14147
14148 case FIELD_DECL:
14149 {
14150 tree type = NULL_TREE;
14151 tree vec = NULL_TREE;
14152 tree expanded_types = NULL_TREE;
14153 int len = 1;
14154
14155 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14156 {
14157 /* This field is a lambda capture pack. Return a TREE_VEC of
14158 the expanded fields to instantiate_class_template_1. */
14159 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14160 complain, in_decl);
14161 if (TREE_CODE (expanded_types) == TREE_VEC)
14162 {
14163 len = TREE_VEC_LENGTH (expanded_types);
14164 vec = make_tree_vec (len);
14165 }
14166 else
14167 {
14168 /* All we did was update the type. Make a note of that. */
14169 type = expanded_types;
14170 expanded_types = NULL_TREE;
14171 }
14172 }
14173
14174 for (int i = 0; i < len; ++i)
14175 {
14176 r = copy_decl (t);
14177 if (expanded_types)
14178 {
14179 type = TREE_VEC_ELT (expanded_types, i);
14180 DECL_NAME (r)
14181 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14182 }
14183 else if (!type)
14184 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14185
14186 if (type == error_mark_node)
14187 RETURN (error_mark_node);
14188 TREE_TYPE (r) = type;
14189 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14190
14191 if (DECL_C_BIT_FIELD (r))
14192 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14193 number of bits. */
14194 DECL_BIT_FIELD_REPRESENTATIVE (r)
14195 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14196 complain, in_decl,
14197 /*integral_constant_expression_p=*/true);
14198 if (DECL_INITIAL (t))
14199 {
14200 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14201 NSDMI in perform_member_init. Still set DECL_INITIAL
14202 so that we know there is one. */
14203 DECL_INITIAL (r) = void_node;
14204 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14205 retrofit_lang_decl (r);
14206 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14207 }
14208 /* We don't have to set DECL_CONTEXT here; it is set by
14209 finish_member_declaration. */
14210 DECL_CHAIN (r) = NULL_TREE;
14211
14212 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14213 args, complain, in_decl);
14214
14215 if (vec)
14216 TREE_VEC_ELT (vec, i) = r;
14217 }
14218
14219 if (vec)
14220 r = vec;
14221 }
14222 break;
14223
14224 case USING_DECL:
14225 /* We reach here only for member using decls. We also need to check
14226 uses_template_parms because DECL_DEPENDENT_P is not set for a
14227 using-declaration that designates a member of the current
14228 instantiation (c++/53549). */
14229 if (DECL_DEPENDENT_P (t)
14230 || uses_template_parms (USING_DECL_SCOPE (t)))
14231 {
14232 tree scope = USING_DECL_SCOPE (t);
14233 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14234 if (PACK_EXPANSION_P (scope))
14235 {
14236 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14237 int len = TREE_VEC_LENGTH (vec);
14238 r = make_tree_vec (len);
14239 for (int i = 0; i < len; ++i)
14240 {
14241 tree escope = TREE_VEC_ELT (vec, i);
14242 tree elt = do_class_using_decl (escope, name);
14243 if (!elt)
14244 {
14245 r = error_mark_node;
14246 break;
14247 }
14248 else
14249 {
14250 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14251 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14252 }
14253 TREE_VEC_ELT (r, i) = elt;
14254 }
14255 }
14256 else
14257 {
14258 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14259 complain, in_decl);
14260 r = do_class_using_decl (inst_scope, name);
14261 if (!r)
14262 r = error_mark_node;
14263 else
14264 {
14265 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14266 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14267 }
14268 }
14269 }
14270 else
14271 {
14272 r = copy_node (t);
14273 DECL_CHAIN (r) = NULL_TREE;
14274 }
14275 break;
14276
14277 case TYPE_DECL:
14278 case VAR_DECL:
14279 {
14280 tree argvec = NULL_TREE;
14281 tree gen_tmpl = NULL_TREE;
14282 tree spec;
14283 tree tmpl = NULL_TREE;
14284 tree ctx;
14285 tree type = NULL_TREE;
14286 bool local_p;
14287
14288 if (TREE_TYPE (t) == error_mark_node)
14289 RETURN (error_mark_node);
14290
14291 if (TREE_CODE (t) == TYPE_DECL
14292 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14293 {
14294 /* If this is the canonical decl, we don't have to
14295 mess with instantiations, and often we can't (for
14296 typename, template type parms and such). Note that
14297 TYPE_NAME is not correct for the above test if
14298 we've copied the type for a typedef. */
14299 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14300 if (type == error_mark_node)
14301 RETURN (error_mark_node);
14302 r = TYPE_NAME (type);
14303 break;
14304 }
14305
14306 /* Check to see if we already have the specialization we
14307 need. */
14308 spec = NULL_TREE;
14309 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14310 {
14311 /* T is a static data member or namespace-scope entity.
14312 We have to substitute into namespace-scope variables
14313 (not just variable templates) because of cases like:
14314
14315 template <class T> void f() { extern T t; }
14316
14317 where the entity referenced is not known until
14318 instantiation time. */
14319 local_p = false;
14320 ctx = DECL_CONTEXT (t);
14321 if (DECL_CLASS_SCOPE_P (t))
14322 {
14323 ctx = tsubst_aggr_type (ctx, args,
14324 complain,
14325 in_decl, /*entering_scope=*/1);
14326 /* If CTX is unchanged, then T is in fact the
14327 specialization we want. That situation occurs when
14328 referencing a static data member within in its own
14329 class. We can use pointer equality, rather than
14330 same_type_p, because DECL_CONTEXT is always
14331 canonical... */
14332 if (ctx == DECL_CONTEXT (t)
14333 /* ... unless T is a member template; in which
14334 case our caller can be willing to create a
14335 specialization of that template represented
14336 by T. */
14337 && !(DECL_TI_TEMPLATE (t)
14338 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14339 spec = t;
14340 }
14341
14342 if (!spec)
14343 {
14344 tmpl = DECL_TI_TEMPLATE (t);
14345 gen_tmpl = most_general_template (tmpl);
14346 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14347 if (argvec != error_mark_node)
14348 argvec = (coerce_innermost_template_parms
14349 (DECL_TEMPLATE_PARMS (gen_tmpl),
14350 argvec, t, complain,
14351 /*all*/true, /*defarg*/true));
14352 if (argvec == error_mark_node)
14353 RETURN (error_mark_node);
14354 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14355 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14356 }
14357 }
14358 else
14359 {
14360 /* A local variable. */
14361 local_p = true;
14362 /* Subsequent calls to pushdecl will fill this in. */
14363 ctx = NULL_TREE;
14364 /* Unless this is a reference to a static variable from an
14365 enclosing function, in which case we need to fill it in now. */
14366 if (TREE_STATIC (t))
14367 {
14368 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14369 if (fn != current_function_decl)
14370 ctx = fn;
14371 }
14372 spec = retrieve_local_specialization (t);
14373 }
14374 /* If we already have the specialization we need, there is
14375 nothing more to do. */
14376 if (spec)
14377 {
14378 r = spec;
14379 break;
14380 }
14381
14382 /* Create a new node for the specialization we need. */
14383 if (type == NULL_TREE)
14384 {
14385 if (is_typedef_decl (t))
14386 type = DECL_ORIGINAL_TYPE (t);
14387 else
14388 type = TREE_TYPE (t);
14389 if (VAR_P (t)
14390 && VAR_HAD_UNKNOWN_BOUND (t)
14391 && type != error_mark_node)
14392 type = strip_array_domain (type);
14393 tree sub_args = args;
14394 if (tree auto_node = type_uses_auto (type))
14395 {
14396 /* Mask off any template args past the variable's context so we
14397 don't replace the auto with an unrelated argument. */
14398 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14399 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14400 if (extra > 0)
14401 /* This should never happen with the new lambda instantiation
14402 model, but keep the handling just in case. */
14403 gcc_assert (!CHECKING_P),
14404 sub_args = strip_innermost_template_args (args, extra);
14405 }
14406 type = tsubst (type, sub_args, complain, in_decl);
14407 /* Substituting the type might have recursively instantiated this
14408 same alias (c++/86171). */
14409 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14410 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14411 {
14412 r = spec;
14413 break;
14414 }
14415 }
14416 r = copy_decl (t);
14417 if (VAR_P (r))
14418 {
14419 DECL_INITIALIZED_P (r) = 0;
14420 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14421 if (type == error_mark_node)
14422 RETURN (error_mark_node);
14423 if (TREE_CODE (type) == FUNCTION_TYPE)
14424 {
14425 /* It may seem that this case cannot occur, since:
14426
14427 typedef void f();
14428 void g() { f x; }
14429
14430 declares a function, not a variable. However:
14431
14432 typedef void f();
14433 template <typename T> void g() { T t; }
14434 template void g<f>();
14435
14436 is an attempt to declare a variable with function
14437 type. */
14438 error ("variable %qD has function type",
14439 /* R is not yet sufficiently initialized, so we
14440 just use its name. */
14441 DECL_NAME (r));
14442 RETURN (error_mark_node);
14443 }
14444 type = complete_type (type);
14445 /* Wait until cp_finish_decl to set this again, to handle
14446 circular dependency (template/instantiate6.C). */
14447 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14448 type = check_var_type (DECL_NAME (r), type,
14449 DECL_SOURCE_LOCATION (r));
14450 if (DECL_HAS_VALUE_EXPR_P (t))
14451 {
14452 tree ve = DECL_VALUE_EXPR (t);
14453 ve = tsubst_expr (ve, args, complain, in_decl,
14454 /*constant_expression_p=*/false);
14455 if (REFERENCE_REF_P (ve))
14456 {
14457 gcc_assert (TYPE_REF_P (type));
14458 ve = TREE_OPERAND (ve, 0);
14459 }
14460 SET_DECL_VALUE_EXPR (r, ve);
14461 }
14462 if (CP_DECL_THREAD_LOCAL_P (r)
14463 && !processing_template_decl)
14464 set_decl_tls_model (r, decl_default_tls_model (r));
14465 }
14466 else if (DECL_SELF_REFERENCE_P (t))
14467 SET_DECL_SELF_REFERENCE_P (r);
14468 TREE_TYPE (r) = type;
14469 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14470 DECL_CONTEXT (r) = ctx;
14471 /* Clear out the mangled name and RTL for the instantiation. */
14472 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14473 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14474 SET_DECL_RTL (r, NULL);
14475 /* The initializer must not be expanded until it is required;
14476 see [temp.inst]. */
14477 DECL_INITIAL (r) = NULL_TREE;
14478 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14479 if (VAR_P (r))
14480 {
14481 if (DECL_LANG_SPECIFIC (r))
14482 SET_DECL_DEPENDENT_INIT_P (r, false);
14483
14484 SET_DECL_MODE (r, VOIDmode);
14485
14486 /* Possibly limit visibility based on template args. */
14487 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14488 if (DECL_VISIBILITY_SPECIFIED (t))
14489 {
14490 DECL_VISIBILITY_SPECIFIED (r) = 0;
14491 DECL_ATTRIBUTES (r)
14492 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14493 }
14494 determine_visibility (r);
14495 }
14496
14497 if (!local_p)
14498 {
14499 /* A static data member declaration is always marked
14500 external when it is declared in-class, even if an
14501 initializer is present. We mimic the non-template
14502 processing here. */
14503 DECL_EXTERNAL (r) = 1;
14504 if (DECL_NAMESPACE_SCOPE_P (t))
14505 DECL_NOT_REALLY_EXTERN (r) = 1;
14506
14507 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14508 SET_DECL_IMPLICIT_INSTANTIATION (r);
14509 /* Remember whether we require constant initialization of
14510 a non-constant template variable. */
14511 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14512 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14513 if (!error_operand_p (r) || (complain & tf_error))
14514 register_specialization (r, gen_tmpl, argvec, false, hash);
14515 }
14516 else
14517 {
14518 if (DECL_LANG_SPECIFIC (r))
14519 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14520 if (!cp_unevaluated_operand)
14521 register_local_specialization (r, t);
14522 }
14523
14524 DECL_CHAIN (r) = NULL_TREE;
14525
14526 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14527 /*flags=*/0,
14528 args, complain, in_decl);
14529
14530 /* Preserve a typedef that names a type. */
14531 if (is_typedef_decl (r) && type != error_mark_node)
14532 {
14533 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14534 set_underlying_type (r);
14535 if (TYPE_DECL_ALIAS_P (r))
14536 /* An alias template specialization can be dependent
14537 even if its underlying type is not. */
14538 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14539 }
14540
14541 layout_decl (r, 0);
14542 }
14543 break;
14544
14545 default:
14546 gcc_unreachable ();
14547 }
14548 #undef RETURN
14549
14550 out:
14551 /* Restore the file and line information. */
14552 input_location = saved_loc;
14553
14554 return r;
14555 }
14556
14557 /* Substitute into the complete parameter type list PARMS. */
14558
14559 tree
14560 tsubst_function_parms (tree parms,
14561 tree args,
14562 tsubst_flags_t complain,
14563 tree in_decl)
14564 {
14565 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14566 }
14567
14568 /* Substitute into the ARG_TYPES of a function type.
14569 If END is a TREE_CHAIN, leave it and any following types
14570 un-substituted. */
14571
14572 static tree
14573 tsubst_arg_types (tree arg_types,
14574 tree args,
14575 tree end,
14576 tsubst_flags_t complain,
14577 tree in_decl)
14578 {
14579 tree remaining_arg_types;
14580 tree type = NULL_TREE;
14581 int i = 1;
14582 tree expanded_args = NULL_TREE;
14583 tree default_arg;
14584
14585 if (!arg_types || arg_types == void_list_node || arg_types == end)
14586 return arg_types;
14587
14588 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14589 args, end, complain, in_decl);
14590 if (remaining_arg_types == error_mark_node)
14591 return error_mark_node;
14592
14593 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14594 {
14595 /* For a pack expansion, perform substitution on the
14596 entire expression. Later on, we'll handle the arguments
14597 one-by-one. */
14598 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14599 args, complain, in_decl);
14600
14601 if (TREE_CODE (expanded_args) == TREE_VEC)
14602 /* So that we'll spin through the parameters, one by one. */
14603 i = TREE_VEC_LENGTH (expanded_args);
14604 else
14605 {
14606 /* We only partially substituted into the parameter
14607 pack. Our type is TYPE_PACK_EXPANSION. */
14608 type = expanded_args;
14609 expanded_args = NULL_TREE;
14610 }
14611 }
14612
14613 while (i > 0) {
14614 --i;
14615
14616 if (expanded_args)
14617 type = TREE_VEC_ELT (expanded_args, i);
14618 else if (!type)
14619 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14620
14621 if (type == error_mark_node)
14622 return error_mark_node;
14623 if (VOID_TYPE_P (type))
14624 {
14625 if (complain & tf_error)
14626 {
14627 error ("invalid parameter type %qT", type);
14628 if (in_decl)
14629 error ("in declaration %q+D", in_decl);
14630 }
14631 return error_mark_node;
14632 }
14633 /* DR 657. */
14634 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14635 return error_mark_node;
14636
14637 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14638 top-level qualifiers as required. */
14639 type = cv_unqualified (type_decays_to (type));
14640
14641 /* We do not substitute into default arguments here. The standard
14642 mandates that they be instantiated only when needed, which is
14643 done in build_over_call. */
14644 default_arg = TREE_PURPOSE (arg_types);
14645
14646 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14647 since the new op() won't have any associated template arguments for us
14648 to refer to later. */
14649 if (lambda_fn_in_template_p (in_decl))
14650 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14651 false/*fn*/, false/*constexpr*/);
14652
14653 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14654 {
14655 /* We've instantiated a template before its default arguments
14656 have been parsed. This can happen for a nested template
14657 class, and is not an error unless we require the default
14658 argument in a call of this function. */
14659 remaining_arg_types =
14660 tree_cons (default_arg, type, remaining_arg_types);
14661 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14662 remaining_arg_types);
14663 }
14664 else
14665 remaining_arg_types =
14666 hash_tree_cons (default_arg, type, remaining_arg_types);
14667 }
14668
14669 return remaining_arg_types;
14670 }
14671
14672 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14673 *not* handle the exception-specification for FNTYPE, because the
14674 initial substitution of explicitly provided template parameters
14675 during argument deduction forbids substitution into the
14676 exception-specification:
14677
14678 [temp.deduct]
14679
14680 All references in the function type of the function template to the
14681 corresponding template parameters are replaced by the specified tem-
14682 plate argument values. If a substitution in a template parameter or
14683 in the function type of the function template results in an invalid
14684 type, type deduction fails. [Note: The equivalent substitution in
14685 exception specifications is done only when the function is instanti-
14686 ated, at which point a program is ill-formed if the substitution
14687 results in an invalid type.] */
14688
14689 static tree
14690 tsubst_function_type (tree t,
14691 tree args,
14692 tsubst_flags_t complain,
14693 tree in_decl)
14694 {
14695 tree return_type;
14696 tree arg_types = NULL_TREE;
14697 tree fntype;
14698
14699 /* The TYPE_CONTEXT is not used for function/method types. */
14700 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14701
14702 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14703 failure. */
14704 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14705
14706 if (late_return_type_p)
14707 {
14708 /* Substitute the argument types. */
14709 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14710 complain, in_decl);
14711 if (arg_types == error_mark_node)
14712 return error_mark_node;
14713
14714 tree save_ccp = current_class_ptr;
14715 tree save_ccr = current_class_ref;
14716 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14717 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14718 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14719 if (do_inject)
14720 {
14721 /* DR 1207: 'this' is in scope in the trailing return type. */
14722 inject_this_parameter (this_type, cp_type_quals (this_type));
14723 }
14724
14725 /* Substitute the return type. */
14726 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14727
14728 if (do_inject)
14729 {
14730 current_class_ptr = save_ccp;
14731 current_class_ref = save_ccr;
14732 }
14733 }
14734 else
14735 /* Substitute the return type. */
14736 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14737
14738 if (return_type == error_mark_node)
14739 return error_mark_node;
14740 /* DR 486 clarifies that creation of a function type with an
14741 invalid return type is a deduction failure. */
14742 if (TREE_CODE (return_type) == ARRAY_TYPE
14743 || TREE_CODE (return_type) == FUNCTION_TYPE)
14744 {
14745 if (complain & tf_error)
14746 {
14747 if (TREE_CODE (return_type) == ARRAY_TYPE)
14748 error ("function returning an array");
14749 else
14750 error ("function returning a function");
14751 }
14752 return error_mark_node;
14753 }
14754 /* And DR 657. */
14755 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14756 return error_mark_node;
14757
14758 if (!late_return_type_p)
14759 {
14760 /* Substitute the argument types. */
14761 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14762 complain, in_decl);
14763 if (arg_types == error_mark_node)
14764 return error_mark_node;
14765 }
14766
14767 /* Construct a new type node and return it. */
14768 if (TREE_CODE (t) == FUNCTION_TYPE)
14769 {
14770 fntype = build_function_type (return_type, arg_types);
14771 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14772 }
14773 else
14774 {
14775 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14776 /* Don't pick up extra function qualifiers from the basetype. */
14777 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14778 if (! MAYBE_CLASS_TYPE_P (r))
14779 {
14780 /* [temp.deduct]
14781
14782 Type deduction may fail for any of the following
14783 reasons:
14784
14785 -- Attempting to create "pointer to member of T" when T
14786 is not a class type. */
14787 if (complain & tf_error)
14788 error ("creating pointer to member function of non-class type %qT",
14789 r);
14790 return error_mark_node;
14791 }
14792
14793 fntype = build_method_type_directly (r, return_type,
14794 TREE_CHAIN (arg_types));
14795 }
14796 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14797
14798 /* See comment above. */
14799 tree raises = NULL_TREE;
14800 cp_ref_qualifier rqual = type_memfn_rqual (t);
14801 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14802
14803 return fntype;
14804 }
14805
14806 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14807 ARGS into that specification, and return the substituted
14808 specification. If there is no specification, return NULL_TREE. */
14809
14810 static tree
14811 tsubst_exception_specification (tree fntype,
14812 tree args,
14813 tsubst_flags_t complain,
14814 tree in_decl,
14815 bool defer_ok)
14816 {
14817 tree specs;
14818 tree new_specs;
14819
14820 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14821 new_specs = NULL_TREE;
14822 if (specs && TREE_PURPOSE (specs))
14823 {
14824 /* A noexcept-specifier. */
14825 tree expr = TREE_PURPOSE (specs);
14826 if (TREE_CODE (expr) == INTEGER_CST)
14827 new_specs = expr;
14828 else if (defer_ok)
14829 {
14830 /* Defer instantiation of noexcept-specifiers to avoid
14831 excessive instantiations (c++/49107). */
14832 new_specs = make_node (DEFERRED_NOEXCEPT);
14833 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14834 {
14835 /* We already partially instantiated this member template,
14836 so combine the new args with the old. */
14837 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14838 = DEFERRED_NOEXCEPT_PATTERN (expr);
14839 DEFERRED_NOEXCEPT_ARGS (new_specs)
14840 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14841 }
14842 else
14843 {
14844 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14845 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14846 }
14847 }
14848 else
14849 {
14850 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14851 {
14852 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14853 args);
14854 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14855 }
14856 new_specs = tsubst_copy_and_build
14857 (expr, args, complain, in_decl, /*function_p=*/false,
14858 /*integral_constant_expression_p=*/true);
14859 }
14860 new_specs = build_noexcept_spec (new_specs, complain);
14861 }
14862 else if (specs)
14863 {
14864 if (! TREE_VALUE (specs))
14865 new_specs = specs;
14866 else
14867 while (specs)
14868 {
14869 tree spec;
14870 int i, len = 1;
14871 tree expanded_specs = NULL_TREE;
14872
14873 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14874 {
14875 /* Expand the pack expansion type. */
14876 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14877 args, complain,
14878 in_decl);
14879
14880 if (expanded_specs == error_mark_node)
14881 return error_mark_node;
14882 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14883 len = TREE_VEC_LENGTH (expanded_specs);
14884 else
14885 {
14886 /* We're substituting into a member template, so
14887 we got a TYPE_PACK_EXPANSION back. Add that
14888 expansion and move on. */
14889 gcc_assert (TREE_CODE (expanded_specs)
14890 == TYPE_PACK_EXPANSION);
14891 new_specs = add_exception_specifier (new_specs,
14892 expanded_specs,
14893 complain);
14894 specs = TREE_CHAIN (specs);
14895 continue;
14896 }
14897 }
14898
14899 for (i = 0; i < len; ++i)
14900 {
14901 if (expanded_specs)
14902 spec = TREE_VEC_ELT (expanded_specs, i);
14903 else
14904 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14905 if (spec == error_mark_node)
14906 return spec;
14907 new_specs = add_exception_specifier (new_specs, spec,
14908 complain);
14909 }
14910
14911 specs = TREE_CHAIN (specs);
14912 }
14913 }
14914 return new_specs;
14915 }
14916
14917 /* Take the tree structure T and replace template parameters used
14918 therein with the argument vector ARGS. IN_DECL is an associated
14919 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14920 Issue error and warning messages under control of COMPLAIN. Note
14921 that we must be relatively non-tolerant of extensions here, in
14922 order to preserve conformance; if we allow substitutions that
14923 should not be allowed, we may allow argument deductions that should
14924 not succeed, and therefore report ambiguous overload situations
14925 where there are none. In theory, we could allow the substitution,
14926 but indicate that it should have failed, and allow our caller to
14927 make sure that the right thing happens, but we don't try to do this
14928 yet.
14929
14930 This function is used for dealing with types, decls and the like;
14931 for expressions, use tsubst_expr or tsubst_copy. */
14932
14933 tree
14934 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14935 {
14936 enum tree_code code;
14937 tree type, r = NULL_TREE;
14938
14939 if (t == NULL_TREE || t == error_mark_node
14940 || t == integer_type_node
14941 || t == void_type_node
14942 || t == char_type_node
14943 || t == unknown_type_node
14944 || TREE_CODE (t) == NAMESPACE_DECL
14945 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14946 return t;
14947
14948 if (DECL_P (t))
14949 return tsubst_decl (t, args, complain);
14950
14951 if (args == NULL_TREE)
14952 return t;
14953
14954 code = TREE_CODE (t);
14955
14956 if (code == IDENTIFIER_NODE)
14957 type = IDENTIFIER_TYPE_VALUE (t);
14958 else
14959 type = TREE_TYPE (t);
14960
14961 gcc_assert (type != unknown_type_node);
14962
14963 /* Reuse typedefs. We need to do this to handle dependent attributes,
14964 such as attribute aligned. */
14965 if (TYPE_P (t)
14966 && typedef_variant_p (t))
14967 {
14968 tree decl = TYPE_NAME (t);
14969
14970 if (alias_template_specialization_p (t, nt_opaque))
14971 {
14972 /* DECL represents an alias template and we want to
14973 instantiate it. */
14974 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14975 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14976 r = instantiate_alias_template (tmpl, gen_args, complain);
14977 }
14978 else if (DECL_CLASS_SCOPE_P (decl)
14979 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14980 && uses_template_parms (DECL_CONTEXT (decl)))
14981 {
14982 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14983 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14984 r = retrieve_specialization (tmpl, gen_args, 0);
14985 }
14986 else if (DECL_FUNCTION_SCOPE_P (decl)
14987 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14988 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14989 r = retrieve_local_specialization (decl);
14990 else
14991 /* The typedef is from a non-template context. */
14992 return t;
14993
14994 if (r)
14995 {
14996 r = TREE_TYPE (r);
14997 r = cp_build_qualified_type_real
14998 (r, cp_type_quals (t) | cp_type_quals (r),
14999 complain | tf_ignore_bad_quals);
15000 return r;
15001 }
15002 else
15003 {
15004 /* We don't have an instantiation yet, so drop the typedef. */
15005 int quals = cp_type_quals (t);
15006 t = DECL_ORIGINAL_TYPE (decl);
15007 t = cp_build_qualified_type_real (t, quals,
15008 complain | tf_ignore_bad_quals);
15009 }
15010 }
15011
15012 bool fndecl_type = (complain & tf_fndecl_type);
15013 complain &= ~tf_fndecl_type;
15014
15015 if (type
15016 && code != TYPENAME_TYPE
15017 && code != TEMPLATE_TYPE_PARM
15018 && code != TEMPLATE_PARM_INDEX
15019 && code != IDENTIFIER_NODE
15020 && code != FUNCTION_TYPE
15021 && code != METHOD_TYPE)
15022 type = tsubst (type, args, complain, in_decl);
15023 if (type == error_mark_node)
15024 return error_mark_node;
15025
15026 switch (code)
15027 {
15028 case RECORD_TYPE:
15029 case UNION_TYPE:
15030 case ENUMERAL_TYPE:
15031 return tsubst_aggr_type (t, args, complain, in_decl,
15032 /*entering_scope=*/0);
15033
15034 case ERROR_MARK:
15035 case IDENTIFIER_NODE:
15036 case VOID_TYPE:
15037 case REAL_TYPE:
15038 case COMPLEX_TYPE:
15039 case VECTOR_TYPE:
15040 case BOOLEAN_TYPE:
15041 case NULLPTR_TYPE:
15042 case LANG_TYPE:
15043 return t;
15044
15045 case INTEGER_TYPE:
15046 if (t == integer_type_node)
15047 return t;
15048
15049 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15050 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15051 return t;
15052
15053 {
15054 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15055
15056 max = tsubst_expr (omax, args, complain, in_decl,
15057 /*integral_constant_expression_p=*/false);
15058
15059 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15060 needed. */
15061 if (TREE_CODE (max) == NOP_EXPR
15062 && TREE_SIDE_EFFECTS (omax)
15063 && !TREE_TYPE (max))
15064 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15065
15066 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15067 with TREE_SIDE_EFFECTS that indicates this is not an integral
15068 constant expression. */
15069 if (processing_template_decl
15070 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15071 {
15072 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15073 TREE_SIDE_EFFECTS (max) = 1;
15074 }
15075
15076 return compute_array_index_type (NULL_TREE, max, complain);
15077 }
15078
15079 case TEMPLATE_TYPE_PARM:
15080 case TEMPLATE_TEMPLATE_PARM:
15081 case BOUND_TEMPLATE_TEMPLATE_PARM:
15082 case TEMPLATE_PARM_INDEX:
15083 {
15084 int idx;
15085 int level;
15086 int levels;
15087 tree arg = NULL_TREE;
15088
15089 r = NULL_TREE;
15090
15091 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15092 template_parm_level_and_index (t, &level, &idx);
15093
15094 levels = TMPL_ARGS_DEPTH (args);
15095 if (level <= levels
15096 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15097 {
15098 arg = TMPL_ARG (args, level, idx);
15099
15100 /* See through ARGUMENT_PACK_SELECT arguments. */
15101 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15102 arg = argument_pack_select_arg (arg);
15103 }
15104
15105 if (arg == error_mark_node)
15106 return error_mark_node;
15107 else if (arg != NULL_TREE)
15108 {
15109 if (ARGUMENT_PACK_P (arg))
15110 /* If ARG is an argument pack, we don't actually want to
15111 perform a substitution here, because substitutions
15112 for argument packs are only done
15113 element-by-element. We can get to this point when
15114 substituting the type of a non-type template
15115 parameter pack, when that type actually contains
15116 template parameter packs from an outer template, e.g.,
15117
15118 template<typename... Types> struct A {
15119 template<Types... Values> struct B { };
15120 }; */
15121 return t;
15122
15123 if (code == TEMPLATE_TYPE_PARM)
15124 {
15125 int quals;
15126
15127 /* When building concept checks for the purpose of
15128 deducing placeholders, we can end up with wildcards
15129 where types are expected. Adjust this to the deduced
15130 value. */
15131 if (TREE_CODE (arg) == WILDCARD_DECL)
15132 arg = TREE_TYPE (TREE_TYPE (arg));
15133
15134 gcc_assert (TYPE_P (arg));
15135
15136 quals = cp_type_quals (arg) | cp_type_quals (t);
15137
15138 return cp_build_qualified_type_real
15139 (arg, quals, complain | tf_ignore_bad_quals);
15140 }
15141 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15142 {
15143 /* We are processing a type constructed from a
15144 template template parameter. */
15145 tree argvec = tsubst (TYPE_TI_ARGS (t),
15146 args, complain, in_decl);
15147 if (argvec == error_mark_node)
15148 return error_mark_node;
15149
15150 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15151 || TREE_CODE (arg) == TEMPLATE_DECL
15152 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15153
15154 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15155 /* Consider this code:
15156
15157 template <template <class> class Template>
15158 struct Internal {
15159 template <class Arg> using Bind = Template<Arg>;
15160 };
15161
15162 template <template <class> class Template, class Arg>
15163 using Instantiate = Template<Arg>; //#0
15164
15165 template <template <class> class Template,
15166 class Argument>
15167 using Bind =
15168 Instantiate<Internal<Template>::template Bind,
15169 Argument>; //#1
15170
15171 When #1 is parsed, the
15172 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15173 parameter `Template' in #0 matches the
15174 UNBOUND_CLASS_TEMPLATE representing the argument
15175 `Internal<Template>::template Bind'; We then want
15176 to assemble the type `Bind<Argument>' that can't
15177 be fully created right now, because
15178 `Internal<Template>' not being complete, the Bind
15179 template cannot be looked up in that context. So
15180 we need to "store" `Bind<Argument>' for later
15181 when the context of Bind becomes complete. Let's
15182 store that in a TYPENAME_TYPE. */
15183 return make_typename_type (TYPE_CONTEXT (arg),
15184 build_nt (TEMPLATE_ID_EXPR,
15185 TYPE_IDENTIFIER (arg),
15186 argvec),
15187 typename_type,
15188 complain);
15189
15190 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15191 are resolving nested-types in the signature of a
15192 member function templates. Otherwise ARG is a
15193 TEMPLATE_DECL and is the real template to be
15194 instantiated. */
15195 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15196 arg = TYPE_NAME (arg);
15197
15198 r = lookup_template_class (arg,
15199 argvec, in_decl,
15200 DECL_CONTEXT (arg),
15201 /*entering_scope=*/0,
15202 complain);
15203 return cp_build_qualified_type_real
15204 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15205 }
15206 else if (code == TEMPLATE_TEMPLATE_PARM)
15207 return arg;
15208 else
15209 /* TEMPLATE_PARM_INDEX. */
15210 return convert_from_reference (unshare_expr (arg));
15211 }
15212
15213 if (level == 1)
15214 /* This can happen during the attempted tsubst'ing in
15215 unify. This means that we don't yet have any information
15216 about the template parameter in question. */
15217 return t;
15218
15219 /* Early in template argument deduction substitution, we don't
15220 want to reduce the level of 'auto', or it will be confused
15221 with a normal template parm in subsequent deduction.
15222 Similarly, don't reduce the level of template parameters to
15223 avoid mismatches when deducing their types. */
15224 if (complain & tf_partial)
15225 return t;
15226
15227 /* If we get here, we must have been looking at a parm for a
15228 more deeply nested template. Make a new version of this
15229 template parameter, but with a lower level. */
15230 switch (code)
15231 {
15232 case TEMPLATE_TYPE_PARM:
15233 case TEMPLATE_TEMPLATE_PARM:
15234 case BOUND_TEMPLATE_TEMPLATE_PARM:
15235 if (cp_type_quals (t))
15236 {
15237 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15238 r = cp_build_qualified_type_real
15239 (r, cp_type_quals (t),
15240 complain | (code == TEMPLATE_TYPE_PARM
15241 ? tf_ignore_bad_quals : 0));
15242 }
15243 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15244 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15245 && (r = (TEMPLATE_PARM_DESCENDANTS
15246 (TEMPLATE_TYPE_PARM_INDEX (t))))
15247 && (r = TREE_TYPE (r))
15248 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15249 /* Break infinite recursion when substituting the constraints
15250 of a constrained placeholder. */;
15251 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15252 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15253 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15254 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15255 r = TEMPLATE_PARM_DESCENDANTS (arg))
15256 && (TEMPLATE_PARM_LEVEL (r)
15257 == TEMPLATE_PARM_LEVEL (arg) - levels))
15258 /* Cache the simple case of lowering a type parameter. */
15259 r = TREE_TYPE (r);
15260 else
15261 {
15262 r = copy_type (t);
15263 TEMPLATE_TYPE_PARM_INDEX (r)
15264 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15265 r, levels, args, complain);
15266 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15267 TYPE_MAIN_VARIANT (r) = r;
15268 TYPE_POINTER_TO (r) = NULL_TREE;
15269 TYPE_REFERENCE_TO (r) = NULL_TREE;
15270
15271 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15272 {
15273 /* Propagate constraints on placeholders since they are
15274 only instantiated during satisfaction. */
15275 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15276 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15277 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15278 {
15279 pl = tsubst_copy (pl, args, complain, in_decl);
15280 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15281 }
15282 }
15283
15284 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15285 /* We have reduced the level of the template
15286 template parameter, but not the levels of its
15287 template parameters, so canonical_type_parameter
15288 will not be able to find the canonical template
15289 template parameter for this level. Thus, we
15290 require structural equality checking to compare
15291 TEMPLATE_TEMPLATE_PARMs. */
15292 SET_TYPE_STRUCTURAL_EQUALITY (r);
15293 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15294 SET_TYPE_STRUCTURAL_EQUALITY (r);
15295 else
15296 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15297
15298 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15299 {
15300 tree tinfo = TYPE_TEMPLATE_INFO (t);
15301 /* We might need to substitute into the types of non-type
15302 template parameters. */
15303 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15304 complain, in_decl);
15305 if (tmpl == error_mark_node)
15306 return error_mark_node;
15307 tree argvec = tsubst (TI_ARGS (tinfo), args,
15308 complain, in_decl);
15309 if (argvec == error_mark_node)
15310 return error_mark_node;
15311
15312 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15313 = build_template_info (tmpl, argvec);
15314 }
15315 }
15316 break;
15317
15318 case TEMPLATE_PARM_INDEX:
15319 /* OK, now substitute the type of the non-type parameter. We
15320 couldn't do it earlier because it might be an auto parameter,
15321 and we wouldn't need to if we had an argument. */
15322 type = tsubst (type, args, complain, in_decl);
15323 if (type == error_mark_node)
15324 return error_mark_node;
15325 r = reduce_template_parm_level (t, type, levels, args, complain);
15326 break;
15327
15328 default:
15329 gcc_unreachable ();
15330 }
15331
15332 return r;
15333 }
15334
15335 case TREE_LIST:
15336 {
15337 tree purpose, value, chain;
15338
15339 if (t == void_list_node)
15340 return t;
15341
15342 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15343 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15344 {
15345 /* We have pack expansions, so expand those and
15346 create a new list out of it. */
15347
15348 /* Expand the argument expressions. */
15349 tree purposevec = NULL_TREE;
15350 if (TREE_PURPOSE (t))
15351 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15352 complain, in_decl);
15353 if (purposevec == error_mark_node)
15354 return error_mark_node;
15355
15356 tree valuevec = NULL_TREE;
15357 if (TREE_VALUE (t))
15358 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15359 complain, in_decl);
15360 if (valuevec == error_mark_node)
15361 return error_mark_node;
15362
15363 /* Build the rest of the list. */
15364 tree chain = TREE_CHAIN (t);
15365 if (chain && chain != void_type_node)
15366 chain = tsubst (chain, args, complain, in_decl);
15367 if (chain == error_mark_node)
15368 return error_mark_node;
15369
15370 /* Determine the number of arguments. */
15371 int len = -1;
15372 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15373 {
15374 len = TREE_VEC_LENGTH (purposevec);
15375 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15376 }
15377 else if (TREE_CODE (valuevec) == TREE_VEC)
15378 len = TREE_VEC_LENGTH (valuevec);
15379 else
15380 {
15381 /* Since we only performed a partial substitution into
15382 the argument pack, we only RETURN (a single list
15383 node. */
15384 if (purposevec == TREE_PURPOSE (t)
15385 && valuevec == TREE_VALUE (t)
15386 && chain == TREE_CHAIN (t))
15387 return t;
15388
15389 return tree_cons (purposevec, valuevec, chain);
15390 }
15391
15392 /* Convert the argument vectors into a TREE_LIST. */
15393 for (int i = len; i-- > 0; )
15394 {
15395 purpose = (purposevec ? TREE_VEC_ELT (purposevec, i)
15396 : NULL_TREE);
15397 value = (valuevec ? TREE_VEC_ELT (valuevec, i)
15398 : NULL_TREE);
15399
15400 /* Build the list (backwards). */
15401 chain = hash_tree_cons (purpose, value, chain);
15402 }
15403
15404 return chain;
15405 }
15406
15407 purpose = TREE_PURPOSE (t);
15408 if (purpose)
15409 {
15410 purpose = tsubst (purpose, args, complain, in_decl);
15411 if (purpose == error_mark_node)
15412 return error_mark_node;
15413 }
15414 value = TREE_VALUE (t);
15415 if (value)
15416 {
15417 value = tsubst (value, args, complain, in_decl);
15418 if (value == error_mark_node)
15419 return error_mark_node;
15420 }
15421 chain = TREE_CHAIN (t);
15422 if (chain && chain != void_type_node)
15423 {
15424 chain = tsubst (chain, args, complain, in_decl);
15425 if (chain == error_mark_node)
15426 return error_mark_node;
15427 }
15428 if (purpose == TREE_PURPOSE (t)
15429 && value == TREE_VALUE (t)
15430 && chain == TREE_CHAIN (t))
15431 return t;
15432 return hash_tree_cons (purpose, value, chain);
15433 }
15434
15435 case TREE_BINFO:
15436 /* We should never be tsubsting a binfo. */
15437 gcc_unreachable ();
15438
15439 case TREE_VEC:
15440 /* A vector of template arguments. */
15441 gcc_assert (!type);
15442 return tsubst_template_args (t, args, complain, in_decl);
15443
15444 case POINTER_TYPE:
15445 case REFERENCE_TYPE:
15446 {
15447 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15448 return t;
15449
15450 /* [temp.deduct]
15451
15452 Type deduction may fail for any of the following
15453 reasons:
15454
15455 -- Attempting to create a pointer to reference type.
15456 -- Attempting to create a reference to a reference type or
15457 a reference to void.
15458
15459 Core issue 106 says that creating a reference to a reference
15460 during instantiation is no longer a cause for failure. We
15461 only enforce this check in strict C++98 mode. */
15462 if ((TYPE_REF_P (type)
15463 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15464 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15465 {
15466 static location_t last_loc;
15467
15468 /* We keep track of the last time we issued this error
15469 message to avoid spewing a ton of messages during a
15470 single bad template instantiation. */
15471 if (complain & tf_error
15472 && last_loc != input_location)
15473 {
15474 if (VOID_TYPE_P (type))
15475 error ("forming reference to void");
15476 else if (code == POINTER_TYPE)
15477 error ("forming pointer to reference type %qT", type);
15478 else
15479 error ("forming reference to reference type %qT", type);
15480 last_loc = input_location;
15481 }
15482
15483 return error_mark_node;
15484 }
15485 else if (TREE_CODE (type) == FUNCTION_TYPE
15486 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15487 || type_memfn_rqual (type) != REF_QUAL_NONE))
15488 {
15489 if (complain & tf_error)
15490 {
15491 if (code == POINTER_TYPE)
15492 error ("forming pointer to qualified function type %qT",
15493 type);
15494 else
15495 error ("forming reference to qualified function type %qT",
15496 type);
15497 }
15498 return error_mark_node;
15499 }
15500 else if (code == POINTER_TYPE)
15501 {
15502 r = build_pointer_type (type);
15503 if (TREE_CODE (type) == METHOD_TYPE)
15504 r = build_ptrmemfunc_type (r);
15505 }
15506 else if (TYPE_REF_P (type))
15507 /* In C++0x, during template argument substitution, when there is an
15508 attempt to create a reference to a reference type, reference
15509 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15510
15511 "If a template-argument for a template-parameter T names a type
15512 that is a reference to a type A, an attempt to create the type
15513 'lvalue reference to cv T' creates the type 'lvalue reference to
15514 A,' while an attempt to create the type type rvalue reference to
15515 cv T' creates the type T"
15516 */
15517 r = cp_build_reference_type
15518 (TREE_TYPE (type),
15519 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15520 else
15521 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15522 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15523
15524 if (r != error_mark_node)
15525 /* Will this ever be needed for TYPE_..._TO values? */
15526 layout_type (r);
15527
15528 return r;
15529 }
15530 case OFFSET_TYPE:
15531 {
15532 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15533 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15534 {
15535 /* [temp.deduct]
15536
15537 Type deduction may fail for any of the following
15538 reasons:
15539
15540 -- Attempting to create "pointer to member of T" when T
15541 is not a class type. */
15542 if (complain & tf_error)
15543 error ("creating pointer to member of non-class type %qT", r);
15544 return error_mark_node;
15545 }
15546 if (TYPE_REF_P (type))
15547 {
15548 if (complain & tf_error)
15549 error ("creating pointer to member reference type %qT", type);
15550 return error_mark_node;
15551 }
15552 if (VOID_TYPE_P (type))
15553 {
15554 if (complain & tf_error)
15555 error ("creating pointer to member of type void");
15556 return error_mark_node;
15557 }
15558 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15559 if (TREE_CODE (type) == FUNCTION_TYPE)
15560 {
15561 /* The type of the implicit object parameter gets its
15562 cv-qualifiers from the FUNCTION_TYPE. */
15563 tree memptr;
15564 tree method_type
15565 = build_memfn_type (type, r, type_memfn_quals (type),
15566 type_memfn_rqual (type));
15567 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15568 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15569 complain);
15570 }
15571 else
15572 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15573 cp_type_quals (t),
15574 complain);
15575 }
15576 case FUNCTION_TYPE:
15577 case METHOD_TYPE:
15578 {
15579 tree fntype;
15580 tree specs;
15581 fntype = tsubst_function_type (t, args, complain, in_decl);
15582 if (fntype == error_mark_node)
15583 return error_mark_node;
15584
15585 /* Substitute the exception specification. */
15586 specs = tsubst_exception_specification (t, args, complain, in_decl,
15587 /*defer_ok*/fndecl_type);
15588 if (specs == error_mark_node)
15589 return error_mark_node;
15590 if (specs)
15591 fntype = build_exception_variant (fntype, specs);
15592 return fntype;
15593 }
15594 case ARRAY_TYPE:
15595 {
15596 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15597 if (domain == error_mark_node)
15598 return error_mark_node;
15599
15600 /* As an optimization, we avoid regenerating the array type if
15601 it will obviously be the same as T. */
15602 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15603 return t;
15604
15605 /* These checks should match the ones in create_array_type_for_decl.
15606
15607 [temp.deduct]
15608
15609 The deduction may fail for any of the following reasons:
15610
15611 -- Attempting to create an array with an element type that
15612 is void, a function type, or a reference type, or [DR337]
15613 an abstract class type. */
15614 if (VOID_TYPE_P (type)
15615 || TREE_CODE (type) == FUNCTION_TYPE
15616 || (TREE_CODE (type) == ARRAY_TYPE
15617 && TYPE_DOMAIN (type) == NULL_TREE)
15618 || TYPE_REF_P (type))
15619 {
15620 if (complain & tf_error)
15621 error ("creating array of %qT", type);
15622 return error_mark_node;
15623 }
15624
15625 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15626 return error_mark_node;
15627
15628 r = build_cplus_array_type (type, domain);
15629
15630 if (!valid_array_size_p (input_location, r, in_decl,
15631 (complain & tf_error)))
15632 return error_mark_node;
15633
15634 if (TYPE_USER_ALIGN (t))
15635 {
15636 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15637 TYPE_USER_ALIGN (r) = 1;
15638 }
15639
15640 return r;
15641 }
15642
15643 case TYPENAME_TYPE:
15644 {
15645 tree ctx = TYPE_CONTEXT (t);
15646 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15647 {
15648 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15649 if (ctx == error_mark_node
15650 || TREE_VEC_LENGTH (ctx) > 1)
15651 return error_mark_node;
15652 if (TREE_VEC_LENGTH (ctx) == 0)
15653 {
15654 if (complain & tf_error)
15655 error ("%qD is instantiated for an empty pack",
15656 TYPENAME_TYPE_FULLNAME (t));
15657 return error_mark_node;
15658 }
15659 ctx = TREE_VEC_ELT (ctx, 0);
15660 }
15661 else
15662 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15663 /*entering_scope=*/1);
15664 if (ctx == error_mark_node)
15665 return error_mark_node;
15666
15667 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15668 complain, in_decl);
15669 if (f == error_mark_node)
15670 return error_mark_node;
15671
15672 if (!MAYBE_CLASS_TYPE_P (ctx))
15673 {
15674 if (complain & tf_error)
15675 error ("%qT is not a class, struct, or union type", ctx);
15676 return error_mark_node;
15677 }
15678 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15679 {
15680 /* Normally, make_typename_type does not require that the CTX
15681 have complete type in order to allow things like:
15682
15683 template <class T> struct S { typename S<T>::X Y; };
15684
15685 But, such constructs have already been resolved by this
15686 point, so here CTX really should have complete type, unless
15687 it's a partial instantiation. */
15688 ctx = complete_type (ctx);
15689 if (!COMPLETE_TYPE_P (ctx))
15690 {
15691 if (complain & tf_error)
15692 cxx_incomplete_type_error (NULL_TREE, ctx);
15693 return error_mark_node;
15694 }
15695 }
15696
15697 f = make_typename_type (ctx, f, typename_type,
15698 complain | tf_keep_type_decl);
15699 if (f == error_mark_node)
15700 return f;
15701 if (TREE_CODE (f) == TYPE_DECL)
15702 {
15703 complain |= tf_ignore_bad_quals;
15704 f = TREE_TYPE (f);
15705 }
15706
15707 if (TREE_CODE (f) != TYPENAME_TYPE)
15708 {
15709 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15710 {
15711 if (complain & tf_error)
15712 error ("%qT resolves to %qT, which is not an enumeration type",
15713 t, f);
15714 else
15715 return error_mark_node;
15716 }
15717 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15718 {
15719 if (complain & tf_error)
15720 error ("%qT resolves to %qT, which is not a class type",
15721 t, f);
15722 else
15723 return error_mark_node;
15724 }
15725 }
15726
15727 return cp_build_qualified_type_real
15728 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15729 }
15730
15731 case UNBOUND_CLASS_TEMPLATE:
15732 {
15733 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15734 in_decl, /*entering_scope=*/1);
15735 tree name = TYPE_IDENTIFIER (t);
15736 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15737
15738 if (ctx == error_mark_node || name == error_mark_node)
15739 return error_mark_node;
15740
15741 if (parm_list)
15742 parm_list = tsubst_template_parms (parm_list, args, complain);
15743 return make_unbound_class_template (ctx, name, parm_list, complain);
15744 }
15745
15746 case TYPEOF_TYPE:
15747 {
15748 tree type;
15749
15750 ++cp_unevaluated_operand;
15751 ++c_inhibit_evaluation_warnings;
15752
15753 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15754 complain, in_decl,
15755 /*integral_constant_expression_p=*/false);
15756
15757 --cp_unevaluated_operand;
15758 --c_inhibit_evaluation_warnings;
15759
15760 type = finish_typeof (type);
15761 return cp_build_qualified_type_real (type,
15762 cp_type_quals (t)
15763 | cp_type_quals (type),
15764 complain);
15765 }
15766
15767 case DECLTYPE_TYPE:
15768 {
15769 tree type;
15770
15771 ++cp_unevaluated_operand;
15772 ++c_inhibit_evaluation_warnings;
15773
15774 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15775 complain|tf_decltype, in_decl,
15776 /*function_p*/false,
15777 /*integral_constant_expression*/false);
15778
15779 --cp_unevaluated_operand;
15780 --c_inhibit_evaluation_warnings;
15781
15782 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15783 type = lambda_capture_field_type (type,
15784 false /*explicit_init*/,
15785 DECLTYPE_FOR_REF_CAPTURE (t));
15786 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15787 type = lambda_proxy_type (type);
15788 else
15789 {
15790 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15791 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15792 && EXPR_P (type))
15793 /* In a template ~id could be either a complement expression
15794 or an unqualified-id naming a destructor; if instantiating
15795 it produces an expression, it's not an id-expression or
15796 member access. */
15797 id = false;
15798 type = finish_decltype_type (type, id, complain);
15799 }
15800 return cp_build_qualified_type_real (type,
15801 cp_type_quals (t)
15802 | cp_type_quals (type),
15803 complain | tf_ignore_bad_quals);
15804 }
15805
15806 case UNDERLYING_TYPE:
15807 {
15808 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15809 complain, in_decl);
15810 return finish_underlying_type (type);
15811 }
15812
15813 case TYPE_ARGUMENT_PACK:
15814 case NONTYPE_ARGUMENT_PACK:
15815 {
15816 tree r;
15817
15818 if (code == NONTYPE_ARGUMENT_PACK)
15819 r = make_node (code);
15820 else
15821 r = cxx_make_type (code);
15822
15823 tree pack_args = ARGUMENT_PACK_ARGS (t);
15824 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15825 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15826
15827 return r;
15828 }
15829
15830 case VOID_CST:
15831 case INTEGER_CST:
15832 case REAL_CST:
15833 case STRING_CST:
15834 case PLUS_EXPR:
15835 case MINUS_EXPR:
15836 case NEGATE_EXPR:
15837 case NOP_EXPR:
15838 case INDIRECT_REF:
15839 case ADDR_EXPR:
15840 case CALL_EXPR:
15841 case ARRAY_REF:
15842 case SCOPE_REF:
15843 /* We should use one of the expression tsubsts for these codes. */
15844 gcc_unreachable ();
15845
15846 default:
15847 sorry ("use of %qs in template", get_tree_code_name (code));
15848 return error_mark_node;
15849 }
15850 }
15851
15852 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15853 expression on the left-hand side of the "." or "->" operator. We
15854 only do the lookup if we had a dependent BASELINK. Otherwise we
15855 adjust it onto the instantiated heirarchy. */
15856
15857 static tree
15858 tsubst_baselink (tree baselink, tree object_type,
15859 tree args, tsubst_flags_t complain, tree in_decl)
15860 {
15861 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15862 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15863 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15864
15865 tree optype = BASELINK_OPTYPE (baselink);
15866 optype = tsubst (optype, args, complain, in_decl);
15867
15868 tree template_args = NULL_TREE;
15869 bool template_id_p = false;
15870 tree fns = BASELINK_FUNCTIONS (baselink);
15871 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15872 {
15873 template_id_p = true;
15874 template_args = TREE_OPERAND (fns, 1);
15875 fns = TREE_OPERAND (fns, 0);
15876 if (template_args)
15877 template_args = tsubst_template_args (template_args, args,
15878 complain, in_decl);
15879 }
15880
15881 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15882 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15883 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15884
15885 if (dependent_p)
15886 {
15887 tree name = OVL_NAME (fns);
15888 if (IDENTIFIER_CONV_OP_P (name))
15889 name = make_conv_op_name (optype);
15890
15891 if (name == complete_dtor_identifier)
15892 /* Treat as-if non-dependent below. */
15893 dependent_p = false;
15894
15895 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15896 if (!baselink)
15897 {
15898 if ((complain & tf_error)
15899 && constructor_name_p (name, qualifying_scope))
15900 error ("cannot call constructor %<%T::%D%> directly",
15901 qualifying_scope, name);
15902 return error_mark_node;
15903 }
15904
15905 if (BASELINK_P (baselink))
15906 fns = BASELINK_FUNCTIONS (baselink);
15907 }
15908 else
15909 /* We're going to overwrite pieces below, make a duplicate. */
15910 baselink = copy_node (baselink);
15911
15912 /* If lookup found a single function, mark it as used at this point.
15913 (If lookup found multiple functions the one selected later by
15914 overload resolution will be marked as used at that point.) */
15915 if (!template_id_p && !really_overloaded_fn (fns))
15916 {
15917 tree fn = OVL_FIRST (fns);
15918 bool ok = mark_used (fn, complain);
15919 if (!ok && !(complain & tf_error))
15920 return error_mark_node;
15921 if (ok && BASELINK_P (baselink))
15922 /* We might have instantiated an auto function. */
15923 TREE_TYPE (baselink) = TREE_TYPE (fn);
15924 }
15925
15926 if (BASELINK_P (baselink))
15927 {
15928 /* Add back the template arguments, if present. */
15929 if (template_id_p)
15930 BASELINK_FUNCTIONS (baselink)
15931 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15932
15933 /* Update the conversion operator type. */
15934 BASELINK_OPTYPE (baselink) = optype;
15935 }
15936
15937 if (!object_type)
15938 object_type = current_class_type;
15939
15940 if (qualified_p || !dependent_p)
15941 {
15942 baselink = adjust_result_of_qualified_name_lookup (baselink,
15943 qualifying_scope,
15944 object_type);
15945 if (!qualified_p)
15946 /* We need to call adjust_result_of_qualified_name_lookup in case the
15947 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15948 so that we still get virtual function binding. */
15949 BASELINK_QUALIFIED_P (baselink) = false;
15950 }
15951
15952 return baselink;
15953 }
15954
15955 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15956 true if the qualified-id will be a postfix-expression in-and-of
15957 itself; false if more of the postfix-expression follows the
15958 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15959 of "&". */
15960
15961 static tree
15962 tsubst_qualified_id (tree qualified_id, tree args,
15963 tsubst_flags_t complain, tree in_decl,
15964 bool done, bool address_p)
15965 {
15966 tree expr;
15967 tree scope;
15968 tree name;
15969 bool is_template;
15970 tree template_args;
15971 location_t loc = UNKNOWN_LOCATION;
15972
15973 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15974
15975 /* Figure out what name to look up. */
15976 name = TREE_OPERAND (qualified_id, 1);
15977 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15978 {
15979 is_template = true;
15980 loc = EXPR_LOCATION (name);
15981 template_args = TREE_OPERAND (name, 1);
15982 if (template_args)
15983 template_args = tsubst_template_args (template_args, args,
15984 complain, in_decl);
15985 if (template_args == error_mark_node)
15986 return error_mark_node;
15987 name = TREE_OPERAND (name, 0);
15988 }
15989 else
15990 {
15991 is_template = false;
15992 template_args = NULL_TREE;
15993 }
15994
15995 /* Substitute into the qualifying scope. When there are no ARGS, we
15996 are just trying to simplify a non-dependent expression. In that
15997 case the qualifying scope may be dependent, and, in any case,
15998 substituting will not help. */
15999 scope = TREE_OPERAND (qualified_id, 0);
16000 if (args)
16001 {
16002 scope = tsubst (scope, args, complain, in_decl);
16003 expr = tsubst_copy (name, args, complain, in_decl);
16004 }
16005 else
16006 expr = name;
16007
16008 if (dependent_scope_p (scope))
16009 {
16010 if (is_template)
16011 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16012 tree r = build_qualified_name (NULL_TREE, scope, expr,
16013 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16014 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16015 return r;
16016 }
16017
16018 if (!BASELINK_P (name) && !DECL_P (expr))
16019 {
16020 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16021 {
16022 /* A BIT_NOT_EXPR is used to represent a destructor. */
16023 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16024 {
16025 error ("qualifying type %qT does not match destructor name ~%qT",
16026 scope, TREE_OPERAND (expr, 0));
16027 expr = error_mark_node;
16028 }
16029 else
16030 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16031 /*is_type_p=*/0, false);
16032 }
16033 else
16034 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
16035 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16036 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16037 {
16038 if (complain & tf_error)
16039 {
16040 error ("dependent-name %qE is parsed as a non-type, but "
16041 "instantiation yields a type", qualified_id);
16042 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16043 }
16044 return error_mark_node;
16045 }
16046 }
16047
16048 if (DECL_P (expr))
16049 {
16050 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16051 scope);
16052 /* Remember that there was a reference to this entity. */
16053 if (!mark_used (expr, complain) && !(complain & tf_error))
16054 return error_mark_node;
16055 }
16056
16057 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16058 {
16059 if (complain & tf_error)
16060 qualified_name_lookup_error (scope,
16061 TREE_OPERAND (qualified_id, 1),
16062 expr, input_location);
16063 return error_mark_node;
16064 }
16065
16066 if (is_template)
16067 {
16068 /* We may be repeating a check already done during parsing, but
16069 if it was well-formed and passed then, it will pass again
16070 now, and if it didn't, we wouldn't have got here. The case
16071 we want to catch is when we couldn't tell then, and can now,
16072 namely when templ prior to substitution was an
16073 identifier. */
16074 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16075 return error_mark_node;
16076
16077 if (variable_template_p (expr))
16078 expr = lookup_and_finish_template_variable (expr, template_args,
16079 complain);
16080 else
16081 expr = lookup_template_function (expr, template_args);
16082 }
16083
16084 if (expr == error_mark_node && complain & tf_error)
16085 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16086 expr, input_location);
16087 else if (TYPE_P (scope))
16088 {
16089 expr = (adjust_result_of_qualified_name_lookup
16090 (expr, scope, current_nonlambda_class_type ()));
16091 expr = (finish_qualified_id_expr
16092 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16093 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16094 /*template_arg_p=*/false, complain));
16095 }
16096
16097 /* Expressions do not generally have reference type. */
16098 if (TREE_CODE (expr) != SCOPE_REF
16099 /* However, if we're about to form a pointer-to-member, we just
16100 want the referenced member referenced. */
16101 && TREE_CODE (expr) != OFFSET_REF)
16102 expr = convert_from_reference (expr);
16103
16104 if (REF_PARENTHESIZED_P (qualified_id))
16105 expr = force_paren_expr (expr);
16106
16107 return expr;
16108 }
16109
16110 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16111 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16112 for tsubst. */
16113
16114 static tree
16115 tsubst_init (tree init, tree decl, tree args,
16116 tsubst_flags_t complain, tree in_decl)
16117 {
16118 if (!init)
16119 return NULL_TREE;
16120
16121 init = tsubst_expr (init, args, complain, in_decl, false);
16122
16123 tree type = TREE_TYPE (decl);
16124
16125 if (!init && type != error_mark_node)
16126 {
16127 if (tree auto_node = type_uses_auto (type))
16128 {
16129 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16130 {
16131 if (complain & tf_error)
16132 error ("initializer for %q#D expands to an empty list "
16133 "of expressions", decl);
16134 return error_mark_node;
16135 }
16136 }
16137 else if (!dependent_type_p (type))
16138 {
16139 /* If we had an initializer but it
16140 instantiated to nothing,
16141 value-initialize the object. This will
16142 only occur when the initializer was a
16143 pack expansion where the parameter packs
16144 used in that expansion were of length
16145 zero. */
16146 init = build_value_init (type, complain);
16147 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16148 init = get_target_expr_sfinae (init, complain);
16149 if (TREE_CODE (init) == TARGET_EXPR)
16150 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16151 }
16152 }
16153
16154 return init;
16155 }
16156
16157 /* Like tsubst, but deals with expressions. This function just replaces
16158 template parms; to finish processing the resultant expression, use
16159 tsubst_copy_and_build or tsubst_expr. */
16160
16161 static tree
16162 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16163 {
16164 enum tree_code code;
16165 tree r;
16166
16167 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16168 return t;
16169
16170 code = TREE_CODE (t);
16171
16172 switch (code)
16173 {
16174 case PARM_DECL:
16175 r = retrieve_local_specialization (t);
16176
16177 if (r == NULL_TREE)
16178 {
16179 /* We get here for a use of 'this' in an NSDMI. */
16180 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16181 return current_class_ptr;
16182
16183 /* This can happen for a parameter name used later in a function
16184 declaration (such as in a late-specified return type). Just
16185 make a dummy decl, since it's only used for its type. */
16186 gcc_assert (cp_unevaluated_operand != 0);
16187 r = tsubst_decl (t, args, complain);
16188 /* Give it the template pattern as its context; its true context
16189 hasn't been instantiated yet and this is good enough for
16190 mangling. */
16191 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16192 }
16193
16194 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16195 r = argument_pack_select_arg (r);
16196 if (!mark_used (r, complain) && !(complain & tf_error))
16197 return error_mark_node;
16198 return r;
16199
16200 case CONST_DECL:
16201 {
16202 tree enum_type;
16203 tree v;
16204
16205 if (DECL_TEMPLATE_PARM_P (t))
16206 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16207 /* There is no need to substitute into namespace-scope
16208 enumerators. */
16209 if (DECL_NAMESPACE_SCOPE_P (t))
16210 return t;
16211 /* If ARGS is NULL, then T is known to be non-dependent. */
16212 if (args == NULL_TREE)
16213 return scalar_constant_value (t);
16214
16215 /* Unfortunately, we cannot just call lookup_name here.
16216 Consider:
16217
16218 template <int I> int f() {
16219 enum E { a = I };
16220 struct S { void g() { E e = a; } };
16221 };
16222
16223 When we instantiate f<7>::S::g(), say, lookup_name is not
16224 clever enough to find f<7>::a. */
16225 enum_type
16226 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16227 /*entering_scope=*/0);
16228
16229 for (v = TYPE_VALUES (enum_type);
16230 v != NULL_TREE;
16231 v = TREE_CHAIN (v))
16232 if (TREE_PURPOSE (v) == DECL_NAME (t))
16233 return TREE_VALUE (v);
16234
16235 /* We didn't find the name. That should never happen; if
16236 name-lookup found it during preliminary parsing, we
16237 should find it again here during instantiation. */
16238 gcc_unreachable ();
16239 }
16240 return t;
16241
16242 case FIELD_DECL:
16243 if (DECL_CONTEXT (t))
16244 {
16245 tree ctx;
16246
16247 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16248 /*entering_scope=*/1);
16249 if (ctx != DECL_CONTEXT (t))
16250 {
16251 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16252 if (!r)
16253 {
16254 if (complain & tf_error)
16255 error ("using invalid field %qD", t);
16256 return error_mark_node;
16257 }
16258 return r;
16259 }
16260 }
16261
16262 return t;
16263
16264 case VAR_DECL:
16265 case FUNCTION_DECL:
16266 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16267 r = tsubst (t, args, complain, in_decl);
16268 else if (local_variable_p (t)
16269 && uses_template_parms (DECL_CONTEXT (t)))
16270 {
16271 r = retrieve_local_specialization (t);
16272 if (r == NULL_TREE)
16273 {
16274 /* First try name lookup to find the instantiation. */
16275 r = lookup_name (DECL_NAME (t));
16276 if (r)
16277 {
16278 if (!VAR_P (r))
16279 {
16280 /* During error-recovery we may find a non-variable,
16281 even an OVERLOAD: just bail out and avoid ICEs and
16282 duplicate diagnostics (c++/62207). */
16283 gcc_assert (seen_error ());
16284 return error_mark_node;
16285 }
16286 if (!is_capture_proxy (r))
16287 {
16288 /* Make sure the one we found is the one we want. */
16289 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16290 if (ctx != DECL_CONTEXT (r))
16291 r = NULL_TREE;
16292 }
16293 }
16294
16295 if (r)
16296 /* OK */;
16297 else
16298 {
16299 /* This can happen for a variable used in a
16300 late-specified return type of a local lambda, or for a
16301 local static or constant. Building a new VAR_DECL
16302 should be OK in all those cases. */
16303 r = tsubst_decl (t, args, complain);
16304 if (local_specializations)
16305 /* Avoid infinite recursion (79640). */
16306 register_local_specialization (r, t);
16307 if (decl_maybe_constant_var_p (r))
16308 {
16309 /* We can't call cp_finish_decl, so handle the
16310 initializer by hand. */
16311 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16312 complain, in_decl);
16313 if (!processing_template_decl)
16314 init = maybe_constant_init (init);
16315 if (processing_template_decl
16316 ? potential_constant_expression (init)
16317 : reduced_constant_expression_p (init))
16318 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16319 = TREE_CONSTANT (r) = true;
16320 DECL_INITIAL (r) = init;
16321 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16322 TREE_TYPE (r)
16323 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16324 complain, adc_variable_type);
16325 }
16326 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16327 || decl_constant_var_p (r)
16328 || seen_error ());
16329 if (!processing_template_decl
16330 && !TREE_STATIC (r))
16331 r = process_outer_var_ref (r, complain);
16332 }
16333 /* Remember this for subsequent uses. */
16334 if (local_specializations)
16335 register_local_specialization (r, t);
16336 }
16337 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16338 r = argument_pack_select_arg (r);
16339 }
16340 else
16341 r = t;
16342 if (!mark_used (r, complain))
16343 return error_mark_node;
16344 return r;
16345
16346 case NAMESPACE_DECL:
16347 return t;
16348
16349 case OVERLOAD:
16350 return t;
16351
16352 case BASELINK:
16353 return tsubst_baselink (t, current_nonlambda_class_type (),
16354 args, complain, in_decl);
16355
16356 case TEMPLATE_DECL:
16357 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16358 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16359 args, complain, in_decl);
16360 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16361 return tsubst (t, args, complain, in_decl);
16362 else if (DECL_CLASS_SCOPE_P (t)
16363 && uses_template_parms (DECL_CONTEXT (t)))
16364 {
16365 /* Template template argument like the following example need
16366 special treatment:
16367
16368 template <template <class> class TT> struct C {};
16369 template <class T> struct D {
16370 template <class U> struct E {};
16371 C<E> c; // #1
16372 };
16373 D<int> d; // #2
16374
16375 We are processing the template argument `E' in #1 for
16376 the template instantiation #2. Originally, `E' is a
16377 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16378 have to substitute this with one having context `D<int>'. */
16379
16380 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16381 if (dependent_scope_p (context))
16382 {
16383 /* When rewriting a constructor into a deduction guide, a
16384 non-dependent name can become dependent, so memtmpl<args>
16385 becomes context::template memtmpl<args>. */
16386 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16387 return build_qualified_name (type, context, DECL_NAME (t),
16388 /*template*/true);
16389 }
16390 return lookup_field (context, DECL_NAME(t), 0, false);
16391 }
16392 else
16393 /* Ordinary template template argument. */
16394 return t;
16395
16396 case NON_LVALUE_EXPR:
16397 case VIEW_CONVERT_EXPR:
16398 {
16399 /* Handle location wrappers by substituting the wrapped node
16400 first, *then* reusing the resulting type. Doing the type
16401 first ensures that we handle template parameters and
16402 parameter pack expansions. */
16403 if (location_wrapper_p (t))
16404 {
16405 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16406 complain, in_decl);
16407 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16408 }
16409 tree op = TREE_OPERAND (t, 0);
16410 if (code == VIEW_CONVERT_EXPR
16411 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16412 {
16413 /* Wrapper to make a C++20 template parameter object const. */
16414 op = tsubst_copy (op, args, complain, in_decl);
16415 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16416 {
16417 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16418 return build1 (code, type, op);
16419 }
16420 else
16421 {
16422 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16423 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16424 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16425 return op;
16426 }
16427 }
16428 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16429 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16430 {
16431 op = tsubst_copy (op, args, complain, in_decl);
16432 op = build1 (code, TREE_TYPE (op), op);
16433 REF_PARENTHESIZED_P (op) = true;
16434 return op;
16435 }
16436 /* We shouldn't see any other uses of these in templates. */
16437 gcc_unreachable ();
16438 }
16439
16440 case CAST_EXPR:
16441 case REINTERPRET_CAST_EXPR:
16442 case CONST_CAST_EXPR:
16443 case STATIC_CAST_EXPR:
16444 case DYNAMIC_CAST_EXPR:
16445 case IMPLICIT_CONV_EXPR:
16446 case CONVERT_EXPR:
16447 case NOP_EXPR:
16448 {
16449 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16450 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16451 return build1 (code, type, op0);
16452 }
16453
16454 case SIZEOF_EXPR:
16455 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16456 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16457 {
16458 tree expanded, op = TREE_OPERAND (t, 0);
16459 int len = 0;
16460
16461 if (SIZEOF_EXPR_TYPE_P (t))
16462 op = TREE_TYPE (op);
16463
16464 ++cp_unevaluated_operand;
16465 ++c_inhibit_evaluation_warnings;
16466 /* We only want to compute the number of arguments. */
16467 if (PACK_EXPANSION_P (op))
16468 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16469 else
16470 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16471 args, complain, in_decl);
16472 --cp_unevaluated_operand;
16473 --c_inhibit_evaluation_warnings;
16474
16475 if (TREE_CODE (expanded) == TREE_VEC)
16476 {
16477 len = TREE_VEC_LENGTH (expanded);
16478 /* Set TREE_USED for the benefit of -Wunused. */
16479 for (int i = 0; i < len; i++)
16480 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16481 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16482 }
16483
16484 if (expanded == error_mark_node)
16485 return error_mark_node;
16486 else if (PACK_EXPANSION_P (expanded)
16487 || (TREE_CODE (expanded) == TREE_VEC
16488 && pack_expansion_args_count (expanded)))
16489
16490 {
16491 if (PACK_EXPANSION_P (expanded))
16492 /* OK. */;
16493 else if (TREE_VEC_LENGTH (expanded) == 1)
16494 expanded = TREE_VEC_ELT (expanded, 0);
16495 else
16496 expanded = make_argument_pack (expanded);
16497
16498 if (TYPE_P (expanded))
16499 return cxx_sizeof_or_alignof_type (input_location,
16500 expanded, SIZEOF_EXPR,
16501 false,
16502 complain & tf_error);
16503 else
16504 return cxx_sizeof_or_alignof_expr (input_location,
16505 expanded, SIZEOF_EXPR,
16506 complain & tf_error);
16507 }
16508 else
16509 return build_int_cst (size_type_node, len);
16510 }
16511 if (SIZEOF_EXPR_TYPE_P (t))
16512 {
16513 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16514 args, complain, in_decl);
16515 r = build1 (NOP_EXPR, r, error_mark_node);
16516 r = build1 (SIZEOF_EXPR,
16517 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16518 SIZEOF_EXPR_TYPE_P (r) = 1;
16519 return r;
16520 }
16521 /* Fall through */
16522
16523 case INDIRECT_REF:
16524 case NEGATE_EXPR:
16525 case TRUTH_NOT_EXPR:
16526 case BIT_NOT_EXPR:
16527 case ADDR_EXPR:
16528 case UNARY_PLUS_EXPR: /* Unary + */
16529 case ALIGNOF_EXPR:
16530 case AT_ENCODE_EXPR:
16531 case ARROW_EXPR:
16532 case THROW_EXPR:
16533 case TYPEID_EXPR:
16534 case REALPART_EXPR:
16535 case IMAGPART_EXPR:
16536 case PAREN_EXPR:
16537 {
16538 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16539 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16540 r = build1 (code, type, op0);
16541 if (code == ALIGNOF_EXPR)
16542 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16543 return r;
16544 }
16545
16546 case COMPONENT_REF:
16547 {
16548 tree object;
16549 tree name;
16550
16551 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16552 name = TREE_OPERAND (t, 1);
16553 if (TREE_CODE (name) == BIT_NOT_EXPR)
16554 {
16555 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16556 complain, in_decl);
16557 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16558 }
16559 else if (TREE_CODE (name) == SCOPE_REF
16560 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16561 {
16562 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16563 complain, in_decl);
16564 name = TREE_OPERAND (name, 1);
16565 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16566 complain, in_decl);
16567 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16568 name = build_qualified_name (/*type=*/NULL_TREE,
16569 base, name,
16570 /*template_p=*/false);
16571 }
16572 else if (BASELINK_P (name))
16573 name = tsubst_baselink (name,
16574 non_reference (TREE_TYPE (object)),
16575 args, complain,
16576 in_decl);
16577 else
16578 name = tsubst_copy (name, args, complain, in_decl);
16579 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16580 }
16581
16582 case PLUS_EXPR:
16583 case MINUS_EXPR:
16584 case MULT_EXPR:
16585 case TRUNC_DIV_EXPR:
16586 case CEIL_DIV_EXPR:
16587 case FLOOR_DIV_EXPR:
16588 case ROUND_DIV_EXPR:
16589 case EXACT_DIV_EXPR:
16590 case BIT_AND_EXPR:
16591 case BIT_IOR_EXPR:
16592 case BIT_XOR_EXPR:
16593 case TRUNC_MOD_EXPR:
16594 case FLOOR_MOD_EXPR:
16595 case TRUTH_ANDIF_EXPR:
16596 case TRUTH_ORIF_EXPR:
16597 case TRUTH_AND_EXPR:
16598 case TRUTH_OR_EXPR:
16599 case RSHIFT_EXPR:
16600 case LSHIFT_EXPR:
16601 case EQ_EXPR:
16602 case NE_EXPR:
16603 case MAX_EXPR:
16604 case MIN_EXPR:
16605 case LE_EXPR:
16606 case GE_EXPR:
16607 case LT_EXPR:
16608 case GT_EXPR:
16609 case COMPOUND_EXPR:
16610 case DOTSTAR_EXPR:
16611 case MEMBER_REF:
16612 case PREDECREMENT_EXPR:
16613 case PREINCREMENT_EXPR:
16614 case POSTDECREMENT_EXPR:
16615 case POSTINCREMENT_EXPR:
16616 {
16617 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16618 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16619 return build_nt (code, op0, op1);
16620 }
16621
16622 case SCOPE_REF:
16623 {
16624 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16625 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16626 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16627 QUALIFIED_NAME_IS_TEMPLATE (t));
16628 }
16629
16630 case ARRAY_REF:
16631 {
16632 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16633 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16634 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16635 }
16636
16637 case CALL_EXPR:
16638 {
16639 int n = VL_EXP_OPERAND_LENGTH (t);
16640 tree result = build_vl_exp (CALL_EXPR, n);
16641 int i;
16642 for (i = 0; i < n; i++)
16643 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16644 complain, in_decl);
16645 return result;
16646 }
16647
16648 case COND_EXPR:
16649 case MODOP_EXPR:
16650 case PSEUDO_DTOR_EXPR:
16651 case VEC_PERM_EXPR:
16652 {
16653 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16654 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16655 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16656 r = build_nt (code, op0, op1, op2);
16657 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16658 return r;
16659 }
16660
16661 case NEW_EXPR:
16662 {
16663 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16664 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16665 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16666 r = build_nt (code, op0, op1, op2);
16667 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16668 return r;
16669 }
16670
16671 case DELETE_EXPR:
16672 {
16673 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16674 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16675 r = build_nt (code, op0, op1);
16676 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16677 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16678 return r;
16679 }
16680
16681 case TEMPLATE_ID_EXPR:
16682 {
16683 /* Substituted template arguments */
16684 tree fn = TREE_OPERAND (t, 0);
16685 tree targs = TREE_OPERAND (t, 1);
16686
16687 fn = tsubst_copy (fn, args, complain, in_decl);
16688 if (targs)
16689 targs = tsubst_template_args (targs, args, complain, in_decl);
16690
16691 return lookup_template_function (fn, targs);
16692 }
16693
16694 case TREE_LIST:
16695 {
16696 tree purpose, value, chain;
16697
16698 if (t == void_list_node)
16699 return t;
16700
16701 purpose = TREE_PURPOSE (t);
16702 if (purpose)
16703 purpose = tsubst_copy (purpose, args, complain, in_decl);
16704 value = TREE_VALUE (t);
16705 if (value)
16706 value = tsubst_copy (value, args, complain, in_decl);
16707 chain = TREE_CHAIN (t);
16708 if (chain && chain != void_type_node)
16709 chain = tsubst_copy (chain, args, complain, in_decl);
16710 if (purpose == TREE_PURPOSE (t)
16711 && value == TREE_VALUE (t)
16712 && chain == TREE_CHAIN (t))
16713 return t;
16714 return tree_cons (purpose, value, chain);
16715 }
16716
16717 case RECORD_TYPE:
16718 case UNION_TYPE:
16719 case ENUMERAL_TYPE:
16720 case INTEGER_TYPE:
16721 case TEMPLATE_TYPE_PARM:
16722 case TEMPLATE_TEMPLATE_PARM:
16723 case BOUND_TEMPLATE_TEMPLATE_PARM:
16724 case TEMPLATE_PARM_INDEX:
16725 case POINTER_TYPE:
16726 case REFERENCE_TYPE:
16727 case OFFSET_TYPE:
16728 case FUNCTION_TYPE:
16729 case METHOD_TYPE:
16730 case ARRAY_TYPE:
16731 case TYPENAME_TYPE:
16732 case UNBOUND_CLASS_TEMPLATE:
16733 case TYPEOF_TYPE:
16734 case DECLTYPE_TYPE:
16735 case TYPE_DECL:
16736 return tsubst (t, args, complain, in_decl);
16737
16738 case USING_DECL:
16739 t = DECL_NAME (t);
16740 /* Fall through. */
16741 case IDENTIFIER_NODE:
16742 if (IDENTIFIER_CONV_OP_P (t))
16743 {
16744 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16745 return make_conv_op_name (new_type);
16746 }
16747 else
16748 return t;
16749
16750 case CONSTRUCTOR:
16751 /* This is handled by tsubst_copy_and_build. */
16752 gcc_unreachable ();
16753
16754 case VA_ARG_EXPR:
16755 {
16756 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16757 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16758 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16759 }
16760
16761 case CLEANUP_POINT_EXPR:
16762 /* We shouldn't have built any of these during initial template
16763 generation. Instead, they should be built during instantiation
16764 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16765 gcc_unreachable ();
16766
16767 case OFFSET_REF:
16768 {
16769 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16770 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16771 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16772 r = build2 (code, type, op0, op1);
16773 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16774 if (!mark_used (TREE_OPERAND (r, 1), complain)
16775 && !(complain & tf_error))
16776 return error_mark_node;
16777 return r;
16778 }
16779
16780 case EXPR_PACK_EXPANSION:
16781 error ("invalid use of pack expansion expression");
16782 return error_mark_node;
16783
16784 case NONTYPE_ARGUMENT_PACK:
16785 error ("use %<...%> to expand argument pack");
16786 return error_mark_node;
16787
16788 case VOID_CST:
16789 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16790 return t;
16791
16792 case INTEGER_CST:
16793 case REAL_CST:
16794 case COMPLEX_CST:
16795 {
16796 /* Instantiate any typedefs in the type. */
16797 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16798 r = fold_convert (type, t);
16799 gcc_assert (TREE_CODE (r) == code);
16800 return r;
16801 }
16802
16803 case STRING_CST:
16804 {
16805 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16806 r = t;
16807 if (type != TREE_TYPE (t))
16808 {
16809 r = copy_node (t);
16810 TREE_TYPE (r) = type;
16811 }
16812 return r;
16813 }
16814
16815 case PTRMEM_CST:
16816 /* These can sometimes show up in a partial instantiation, but never
16817 involve template parms. */
16818 gcc_assert (!uses_template_parms (t));
16819 return t;
16820
16821 case UNARY_LEFT_FOLD_EXPR:
16822 return tsubst_unary_left_fold (t, args, complain, in_decl);
16823 case UNARY_RIGHT_FOLD_EXPR:
16824 return tsubst_unary_right_fold (t, args, complain, in_decl);
16825 case BINARY_LEFT_FOLD_EXPR:
16826 return tsubst_binary_left_fold (t, args, complain, in_decl);
16827 case BINARY_RIGHT_FOLD_EXPR:
16828 return tsubst_binary_right_fold (t, args, complain, in_decl);
16829 case PREDICT_EXPR:
16830 return t;
16831
16832 case DEBUG_BEGIN_STMT:
16833 /* ??? There's no point in copying it for now, but maybe some
16834 day it will contain more information, such as a pointer back
16835 to the containing function, inlined copy or so. */
16836 return t;
16837
16838 case CO_AWAIT_EXPR:
16839 return tsubst_expr (t, args, complain, in_decl,
16840 /*integral_constant_expression_p=*/false);
16841 break;
16842
16843 default:
16844 /* We shouldn't get here, but keep going if !flag_checking. */
16845 if (flag_checking)
16846 gcc_unreachable ();
16847 return t;
16848 }
16849 }
16850
16851 /* Helper function for tsubst_omp_clauses, used for instantiation of
16852 OMP_CLAUSE_DECL of clauses. */
16853
16854 static tree
16855 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16856 tree in_decl, tree *iterator_cache)
16857 {
16858 if (decl == NULL_TREE)
16859 return NULL_TREE;
16860
16861 /* Handle OpenMP iterators. */
16862 if (TREE_CODE (decl) == TREE_LIST
16863 && TREE_PURPOSE (decl)
16864 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16865 {
16866 tree ret;
16867 if (iterator_cache[0] == TREE_PURPOSE (decl))
16868 ret = iterator_cache[1];
16869 else
16870 {
16871 tree *tp = &ret;
16872 begin_scope (sk_omp, NULL);
16873 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16874 {
16875 *tp = copy_node (it);
16876 TREE_VEC_ELT (*tp, 0)
16877 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16878 TREE_VEC_ELT (*tp, 1)
16879 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16880 /*integral_constant_expression_p=*/false);
16881 TREE_VEC_ELT (*tp, 2)
16882 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16883 /*integral_constant_expression_p=*/false);
16884 TREE_VEC_ELT (*tp, 3)
16885 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16886 /*integral_constant_expression_p=*/false);
16887 TREE_CHAIN (*tp) = NULL_TREE;
16888 tp = &TREE_CHAIN (*tp);
16889 }
16890 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16891 iterator_cache[0] = TREE_PURPOSE (decl);
16892 iterator_cache[1] = ret;
16893 }
16894 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16895 args, complain,
16896 in_decl, NULL));
16897 }
16898
16899 /* Handle an OpenMP array section represented as a TREE_LIST (or
16900 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16901 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16902 TREE_LIST. We can handle it exactly the same as an array section
16903 (purpose, value, and a chain), even though the nomenclature
16904 (low_bound, length, etc) is different. */
16905 if (TREE_CODE (decl) == TREE_LIST)
16906 {
16907 tree low_bound
16908 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16909 /*integral_constant_expression_p=*/false);
16910 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16911 /*integral_constant_expression_p=*/false);
16912 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16913 in_decl, NULL);
16914 if (TREE_PURPOSE (decl) == low_bound
16915 && TREE_VALUE (decl) == length
16916 && TREE_CHAIN (decl) == chain)
16917 return decl;
16918 tree ret = tree_cons (low_bound, length, chain);
16919 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16920 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16921 return ret;
16922 }
16923 tree ret = tsubst_expr (decl, args, complain, in_decl,
16924 /*integral_constant_expression_p=*/false);
16925 /* Undo convert_from_reference tsubst_expr could have called. */
16926 if (decl
16927 && REFERENCE_REF_P (ret)
16928 && !REFERENCE_REF_P (decl))
16929 ret = TREE_OPERAND (ret, 0);
16930 return ret;
16931 }
16932
16933 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16934
16935 static tree
16936 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16937 tree args, tsubst_flags_t complain, tree in_decl)
16938 {
16939 tree new_clauses = NULL_TREE, nc, oc;
16940 tree linear_no_step = NULL_TREE;
16941 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16942
16943 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16944 {
16945 nc = copy_node (oc);
16946 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16947 new_clauses = nc;
16948
16949 switch (OMP_CLAUSE_CODE (nc))
16950 {
16951 case OMP_CLAUSE_LASTPRIVATE:
16952 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16953 {
16954 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16955 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16956 in_decl, /*integral_constant_expression_p=*/false);
16957 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16958 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16959 }
16960 /* FALLTHRU */
16961 case OMP_CLAUSE_PRIVATE:
16962 case OMP_CLAUSE_SHARED:
16963 case OMP_CLAUSE_FIRSTPRIVATE:
16964 case OMP_CLAUSE_COPYIN:
16965 case OMP_CLAUSE_COPYPRIVATE:
16966 case OMP_CLAUSE_UNIFORM:
16967 case OMP_CLAUSE_DEPEND:
16968 case OMP_CLAUSE_FROM:
16969 case OMP_CLAUSE_TO:
16970 case OMP_CLAUSE_MAP:
16971 case OMP_CLAUSE_NONTEMPORAL:
16972 case OMP_CLAUSE_USE_DEVICE_PTR:
16973 case OMP_CLAUSE_USE_DEVICE_ADDR:
16974 case OMP_CLAUSE_IS_DEVICE_PTR:
16975 case OMP_CLAUSE_INCLUSIVE:
16976 case OMP_CLAUSE_EXCLUSIVE:
16977 OMP_CLAUSE_DECL (nc)
16978 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16979 in_decl, iterator_cache);
16980 break;
16981 case OMP_CLAUSE_TILE:
16982 case OMP_CLAUSE_IF:
16983 case OMP_CLAUSE_NUM_THREADS:
16984 case OMP_CLAUSE_SCHEDULE:
16985 case OMP_CLAUSE_COLLAPSE:
16986 case OMP_CLAUSE_FINAL:
16987 case OMP_CLAUSE_DEVICE:
16988 case OMP_CLAUSE_DIST_SCHEDULE:
16989 case OMP_CLAUSE_NUM_TEAMS:
16990 case OMP_CLAUSE_THREAD_LIMIT:
16991 case OMP_CLAUSE_SAFELEN:
16992 case OMP_CLAUSE_SIMDLEN:
16993 case OMP_CLAUSE_NUM_TASKS:
16994 case OMP_CLAUSE_GRAINSIZE:
16995 case OMP_CLAUSE_PRIORITY:
16996 case OMP_CLAUSE_ORDERED:
16997 case OMP_CLAUSE_HINT:
16998 case OMP_CLAUSE_NUM_GANGS:
16999 case OMP_CLAUSE_NUM_WORKERS:
17000 case OMP_CLAUSE_VECTOR_LENGTH:
17001 case OMP_CLAUSE_WORKER:
17002 case OMP_CLAUSE_VECTOR:
17003 case OMP_CLAUSE_ASYNC:
17004 case OMP_CLAUSE_WAIT:
17005 OMP_CLAUSE_OPERAND (nc, 0)
17006 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17007 in_decl, /*integral_constant_expression_p=*/false);
17008 break;
17009 case OMP_CLAUSE_REDUCTION:
17010 case OMP_CLAUSE_IN_REDUCTION:
17011 case OMP_CLAUSE_TASK_REDUCTION:
17012 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17013 {
17014 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17015 if (TREE_CODE (placeholder) == SCOPE_REF)
17016 {
17017 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17018 complain, in_decl);
17019 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17020 = build_qualified_name (NULL_TREE, scope,
17021 TREE_OPERAND (placeholder, 1),
17022 false);
17023 }
17024 else
17025 gcc_assert (identifier_p (placeholder));
17026 }
17027 OMP_CLAUSE_DECL (nc)
17028 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17029 in_decl, NULL);
17030 break;
17031 case OMP_CLAUSE_GANG:
17032 case OMP_CLAUSE_ALIGNED:
17033 OMP_CLAUSE_DECL (nc)
17034 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17035 in_decl, NULL);
17036 OMP_CLAUSE_OPERAND (nc, 1)
17037 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17038 in_decl, /*integral_constant_expression_p=*/false);
17039 break;
17040 case OMP_CLAUSE_LINEAR:
17041 OMP_CLAUSE_DECL (nc)
17042 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17043 in_decl, NULL);
17044 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17045 {
17046 gcc_assert (!linear_no_step);
17047 linear_no_step = nc;
17048 }
17049 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17050 OMP_CLAUSE_LINEAR_STEP (nc)
17051 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17052 complain, in_decl, NULL);
17053 else
17054 OMP_CLAUSE_LINEAR_STEP (nc)
17055 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17056 in_decl,
17057 /*integral_constant_expression_p=*/false);
17058 break;
17059 case OMP_CLAUSE_NOWAIT:
17060 case OMP_CLAUSE_DEFAULT:
17061 case OMP_CLAUSE_UNTIED:
17062 case OMP_CLAUSE_MERGEABLE:
17063 case OMP_CLAUSE_INBRANCH:
17064 case OMP_CLAUSE_NOTINBRANCH:
17065 case OMP_CLAUSE_PROC_BIND:
17066 case OMP_CLAUSE_FOR:
17067 case OMP_CLAUSE_PARALLEL:
17068 case OMP_CLAUSE_SECTIONS:
17069 case OMP_CLAUSE_TASKGROUP:
17070 case OMP_CLAUSE_NOGROUP:
17071 case OMP_CLAUSE_THREADS:
17072 case OMP_CLAUSE_SIMD:
17073 case OMP_CLAUSE_DEFAULTMAP:
17074 case OMP_CLAUSE_ORDER:
17075 case OMP_CLAUSE_BIND:
17076 case OMP_CLAUSE_INDEPENDENT:
17077 case OMP_CLAUSE_AUTO:
17078 case OMP_CLAUSE_SEQ:
17079 case OMP_CLAUSE_IF_PRESENT:
17080 case OMP_CLAUSE_FINALIZE:
17081 break;
17082 default:
17083 gcc_unreachable ();
17084 }
17085 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17086 switch (OMP_CLAUSE_CODE (nc))
17087 {
17088 case OMP_CLAUSE_SHARED:
17089 case OMP_CLAUSE_PRIVATE:
17090 case OMP_CLAUSE_FIRSTPRIVATE:
17091 case OMP_CLAUSE_LASTPRIVATE:
17092 case OMP_CLAUSE_COPYPRIVATE:
17093 case OMP_CLAUSE_LINEAR:
17094 case OMP_CLAUSE_REDUCTION:
17095 case OMP_CLAUSE_IN_REDUCTION:
17096 case OMP_CLAUSE_TASK_REDUCTION:
17097 case OMP_CLAUSE_USE_DEVICE_PTR:
17098 case OMP_CLAUSE_USE_DEVICE_ADDR:
17099 case OMP_CLAUSE_IS_DEVICE_PTR:
17100 case OMP_CLAUSE_INCLUSIVE:
17101 case OMP_CLAUSE_EXCLUSIVE:
17102 /* tsubst_expr on SCOPE_REF results in returning
17103 finish_non_static_data_member result. Undo that here. */
17104 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17105 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17106 == IDENTIFIER_NODE))
17107 {
17108 tree t = OMP_CLAUSE_DECL (nc);
17109 tree v = t;
17110 while (v)
17111 switch (TREE_CODE (v))
17112 {
17113 case COMPONENT_REF:
17114 case MEM_REF:
17115 case INDIRECT_REF:
17116 CASE_CONVERT:
17117 case POINTER_PLUS_EXPR:
17118 v = TREE_OPERAND (v, 0);
17119 continue;
17120 case PARM_DECL:
17121 if (DECL_CONTEXT (v) == current_function_decl
17122 && DECL_ARTIFICIAL (v)
17123 && DECL_NAME (v) == this_identifier)
17124 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17125 /* FALLTHRU */
17126 default:
17127 v = NULL_TREE;
17128 break;
17129 }
17130 }
17131 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17132 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17133 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17134 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17135 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17136 {
17137 tree decl = OMP_CLAUSE_DECL (nc);
17138 if (VAR_P (decl))
17139 {
17140 retrofit_lang_decl (decl);
17141 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17142 }
17143 }
17144 break;
17145 default:
17146 break;
17147 }
17148 }
17149
17150 new_clauses = nreverse (new_clauses);
17151 if (ort != C_ORT_OMP_DECLARE_SIMD)
17152 {
17153 new_clauses = finish_omp_clauses (new_clauses, ort);
17154 if (linear_no_step)
17155 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17156 if (nc == linear_no_step)
17157 {
17158 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17159 break;
17160 }
17161 }
17162 return new_clauses;
17163 }
17164
17165 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17166
17167 static tree
17168 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17169 tree in_decl)
17170 {
17171 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17172
17173 tree purpose, value, chain;
17174
17175 if (t == NULL)
17176 return t;
17177
17178 if (TREE_CODE (t) != TREE_LIST)
17179 return tsubst_copy_and_build (t, args, complain, in_decl,
17180 /*function_p=*/false,
17181 /*integral_constant_expression_p=*/false);
17182
17183 if (t == void_list_node)
17184 return t;
17185
17186 purpose = TREE_PURPOSE (t);
17187 if (purpose)
17188 purpose = RECUR (purpose);
17189 value = TREE_VALUE (t);
17190 if (value)
17191 {
17192 if (TREE_CODE (value) != LABEL_DECL)
17193 value = RECUR (value);
17194 else
17195 {
17196 value = lookup_label (DECL_NAME (value));
17197 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17198 TREE_USED (value) = 1;
17199 }
17200 }
17201 chain = TREE_CHAIN (t);
17202 if (chain && chain != void_type_node)
17203 chain = RECUR (chain);
17204 return tree_cons (purpose, value, chain);
17205 #undef RECUR
17206 }
17207
17208 /* Used to temporarily communicate the list of #pragma omp parallel
17209 clauses to #pragma omp for instantiation if they are combined
17210 together. */
17211
17212 static tree *omp_parallel_combined_clauses;
17213
17214 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17215 tree *, unsigned int *);
17216
17217 /* Substitute one OMP_FOR iterator. */
17218
17219 static bool
17220 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17221 tree initv, tree condv, tree incrv, tree *clauses,
17222 tree args, tsubst_flags_t complain, tree in_decl,
17223 bool integral_constant_expression_p)
17224 {
17225 #define RECUR(NODE) \
17226 tsubst_expr ((NODE), args, complain, in_decl, \
17227 integral_constant_expression_p)
17228 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17229 bool ret = false;
17230
17231 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17232 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17233
17234 decl = TREE_OPERAND (init, 0);
17235 init = TREE_OPERAND (init, 1);
17236 tree decl_expr = NULL_TREE;
17237 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17238 if (range_for)
17239 {
17240 bool decomp = false;
17241 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17242 {
17243 tree v = DECL_VALUE_EXPR (decl);
17244 if (TREE_CODE (v) == ARRAY_REF
17245 && VAR_P (TREE_OPERAND (v, 0))
17246 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17247 {
17248 tree decomp_first = NULL_TREE;
17249 unsigned decomp_cnt = 0;
17250 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17251 maybe_push_decl (d);
17252 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17253 in_decl, &decomp_first, &decomp_cnt);
17254 decomp = true;
17255 if (d == error_mark_node)
17256 decl = error_mark_node;
17257 else
17258 for (unsigned int i = 0; i < decomp_cnt; i++)
17259 {
17260 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17261 {
17262 tree v = build_nt (ARRAY_REF, d,
17263 size_int (decomp_cnt - i - 1),
17264 NULL_TREE, NULL_TREE);
17265 SET_DECL_VALUE_EXPR (decomp_first, v);
17266 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17267 }
17268 fit_decomposition_lang_decl (decomp_first, d);
17269 decomp_first = DECL_CHAIN (decomp_first);
17270 }
17271 }
17272 }
17273 decl = tsubst_decl (decl, args, complain);
17274 if (!decomp)
17275 maybe_push_decl (decl);
17276 }
17277 else if (init && TREE_CODE (init) == DECL_EXPR)
17278 {
17279 /* We need to jump through some hoops to handle declarations in the
17280 init-statement, since we might need to handle auto deduction,
17281 but we need to keep control of initialization. */
17282 decl_expr = init;
17283 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17284 decl = tsubst_decl (decl, args, complain);
17285 }
17286 else
17287 {
17288 if (TREE_CODE (decl) == SCOPE_REF)
17289 {
17290 decl = RECUR (decl);
17291 if (TREE_CODE (decl) == COMPONENT_REF)
17292 {
17293 tree v = decl;
17294 while (v)
17295 switch (TREE_CODE (v))
17296 {
17297 case COMPONENT_REF:
17298 case MEM_REF:
17299 case INDIRECT_REF:
17300 CASE_CONVERT:
17301 case POINTER_PLUS_EXPR:
17302 v = TREE_OPERAND (v, 0);
17303 continue;
17304 case PARM_DECL:
17305 if (DECL_CONTEXT (v) == current_function_decl
17306 && DECL_ARTIFICIAL (v)
17307 && DECL_NAME (v) == this_identifier)
17308 {
17309 decl = TREE_OPERAND (decl, 1);
17310 decl = omp_privatize_field (decl, false);
17311 }
17312 /* FALLTHRU */
17313 default:
17314 v = NULL_TREE;
17315 break;
17316 }
17317 }
17318 }
17319 else
17320 decl = RECUR (decl);
17321 }
17322 init = RECUR (init);
17323
17324 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17325 {
17326 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17327 if (TREE_CODE (o) == TREE_LIST)
17328 TREE_VEC_ELT (orig_declv, i)
17329 = tree_cons (RECUR (TREE_PURPOSE (o)),
17330 RECUR (TREE_VALUE (o)),
17331 NULL_TREE);
17332 else
17333 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17334 }
17335
17336 if (range_for)
17337 {
17338 tree this_pre_body = NULL_TREE;
17339 tree orig_init = NULL_TREE;
17340 tree orig_decl = NULL_TREE;
17341 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17342 orig_init, cond, incr);
17343 if (orig_decl)
17344 {
17345 if (orig_declv == NULL_TREE)
17346 orig_declv = copy_node (declv);
17347 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17348 ret = true;
17349 }
17350 else if (orig_declv)
17351 TREE_VEC_ELT (orig_declv, i) = decl;
17352 }
17353
17354 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17355 if (!range_for && auto_node && init)
17356 TREE_TYPE (decl)
17357 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17358
17359 gcc_assert (!type_dependent_expression_p (decl));
17360
17361 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17362 {
17363 if (decl_expr)
17364 {
17365 /* Declare the variable, but don't let that initialize it. */
17366 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17367 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17368 RECUR (decl_expr);
17369 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17370 }
17371
17372 if (!range_for)
17373 {
17374 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17375 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17376 if (TREE_CODE (incr) == MODIFY_EXPR)
17377 {
17378 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17379 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17380 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17381 NOP_EXPR, rhs, complain);
17382 }
17383 else
17384 incr = RECUR (incr);
17385 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17386 TREE_VEC_ELT (orig_declv, i) = decl;
17387 }
17388 TREE_VEC_ELT (declv, i) = decl;
17389 TREE_VEC_ELT (initv, i) = init;
17390 TREE_VEC_ELT (condv, i) = cond;
17391 TREE_VEC_ELT (incrv, i) = incr;
17392 return ret;
17393 }
17394
17395 if (decl_expr)
17396 {
17397 /* Declare and initialize the variable. */
17398 RECUR (decl_expr);
17399 init = NULL_TREE;
17400 }
17401 else if (init)
17402 {
17403 tree *pc;
17404 int j;
17405 for (j = ((omp_parallel_combined_clauses == NULL
17406 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17407 {
17408 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17409 {
17410 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17411 && OMP_CLAUSE_DECL (*pc) == decl)
17412 break;
17413 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17414 && OMP_CLAUSE_DECL (*pc) == decl)
17415 {
17416 if (j)
17417 break;
17418 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17419 tree c = *pc;
17420 *pc = OMP_CLAUSE_CHAIN (c);
17421 OMP_CLAUSE_CHAIN (c) = *clauses;
17422 *clauses = c;
17423 }
17424 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17425 && OMP_CLAUSE_DECL (*pc) == decl)
17426 {
17427 error ("iteration variable %qD should not be firstprivate",
17428 decl);
17429 *pc = OMP_CLAUSE_CHAIN (*pc);
17430 }
17431 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17432 && OMP_CLAUSE_DECL (*pc) == decl)
17433 {
17434 error ("iteration variable %qD should not be reduction",
17435 decl);
17436 *pc = OMP_CLAUSE_CHAIN (*pc);
17437 }
17438 else
17439 pc = &OMP_CLAUSE_CHAIN (*pc);
17440 }
17441 if (*pc)
17442 break;
17443 }
17444 if (*pc == NULL_TREE)
17445 {
17446 tree c = build_omp_clause (input_location,
17447 TREE_CODE (t) == OMP_LOOP
17448 ? OMP_CLAUSE_LASTPRIVATE
17449 : OMP_CLAUSE_PRIVATE);
17450 OMP_CLAUSE_DECL (c) = decl;
17451 c = finish_omp_clauses (c, C_ORT_OMP);
17452 if (c)
17453 {
17454 OMP_CLAUSE_CHAIN (c) = *clauses;
17455 *clauses = c;
17456 }
17457 }
17458 }
17459 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17460 if (COMPARISON_CLASS_P (cond))
17461 {
17462 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17463 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17464 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17465 }
17466 else
17467 cond = RECUR (cond);
17468 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17469 switch (TREE_CODE (incr))
17470 {
17471 case PREINCREMENT_EXPR:
17472 case PREDECREMENT_EXPR:
17473 case POSTINCREMENT_EXPR:
17474 case POSTDECREMENT_EXPR:
17475 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17476 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17477 break;
17478 case MODIFY_EXPR:
17479 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17480 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17481 {
17482 tree rhs = TREE_OPERAND (incr, 1);
17483 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17484 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17485 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17486 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17487 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17488 rhs0, rhs1));
17489 }
17490 else
17491 incr = RECUR (incr);
17492 break;
17493 case MODOP_EXPR:
17494 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17495 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17496 {
17497 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17498 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17499 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17500 TREE_TYPE (decl), lhs,
17501 RECUR (TREE_OPERAND (incr, 2))));
17502 }
17503 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17504 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17505 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17506 {
17507 tree rhs = TREE_OPERAND (incr, 2);
17508 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17509 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17510 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17511 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17512 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17513 rhs0, rhs1));
17514 }
17515 else
17516 incr = RECUR (incr);
17517 break;
17518 default:
17519 incr = RECUR (incr);
17520 break;
17521 }
17522
17523 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17524 TREE_VEC_ELT (orig_declv, i) = decl;
17525 TREE_VEC_ELT (declv, i) = decl;
17526 TREE_VEC_ELT (initv, i) = init;
17527 TREE_VEC_ELT (condv, i) = cond;
17528 TREE_VEC_ELT (incrv, i) = incr;
17529 return false;
17530 #undef RECUR
17531 }
17532
17533 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17534 of OMP_TARGET's body. */
17535
17536 static tree
17537 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17538 {
17539 *walk_subtrees = 0;
17540 switch (TREE_CODE (*tp))
17541 {
17542 case OMP_TEAMS:
17543 return *tp;
17544 case BIND_EXPR:
17545 case STATEMENT_LIST:
17546 *walk_subtrees = 1;
17547 break;
17548 default:
17549 break;
17550 }
17551 return NULL_TREE;
17552 }
17553
17554 /* Helper function for tsubst_expr. For decomposition declaration
17555 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17556 also the corresponding decls representing the identifiers
17557 of the decomposition declaration. Return DECL if successful
17558 or error_mark_node otherwise, set *FIRST to the first decl
17559 in the list chained through DECL_CHAIN and *CNT to the number
17560 of such decls. */
17561
17562 static tree
17563 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17564 tsubst_flags_t complain, tree in_decl, tree *first,
17565 unsigned int *cnt)
17566 {
17567 tree decl2, decl3, prev = decl;
17568 *cnt = 0;
17569 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17570 for (decl2 = DECL_CHAIN (pattern_decl);
17571 decl2
17572 && VAR_P (decl2)
17573 && DECL_DECOMPOSITION_P (decl2)
17574 && DECL_NAME (decl2);
17575 decl2 = DECL_CHAIN (decl2))
17576 {
17577 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17578 {
17579 gcc_assert (errorcount);
17580 return error_mark_node;
17581 }
17582 (*cnt)++;
17583 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17584 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17585 tree v = DECL_VALUE_EXPR (decl2);
17586 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17587 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17588 decl3 = tsubst (decl2, args, complain, in_decl);
17589 SET_DECL_VALUE_EXPR (decl2, v);
17590 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17591 if (VAR_P (decl3))
17592 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17593 else
17594 {
17595 gcc_assert (errorcount);
17596 decl = error_mark_node;
17597 continue;
17598 }
17599 maybe_push_decl (decl3);
17600 if (error_operand_p (decl3))
17601 decl = error_mark_node;
17602 else if (decl != error_mark_node
17603 && DECL_CHAIN (decl3) != prev
17604 && decl != prev)
17605 {
17606 gcc_assert (errorcount);
17607 decl = error_mark_node;
17608 }
17609 else
17610 prev = decl3;
17611 }
17612 *first = prev;
17613 return decl;
17614 }
17615
17616 /* Return the proper local_specialization for init-capture pack DECL. */
17617
17618 static tree
17619 lookup_init_capture_pack (tree decl)
17620 {
17621 /* We handle normal pack captures by forwarding to the specialization of the
17622 captured parameter. We can't do that for pack init-captures; we need them
17623 to have their own local_specialization. We created the individual
17624 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17625 when we process the DECL_EXPR for the pack init-capture in the template.
17626 So, how do we find them? We don't know the capture proxy pack when
17627 building the individual resulting proxies, and we don't know the
17628 individual proxies when instantiating the pack. What we have in common is
17629 the FIELD_DECL.
17630
17631 So...when we instantiate the FIELD_DECL, we stick the result in
17632 local_specializations. Then at the DECL_EXPR we look up that result, see
17633 how many elements it has, synthesize the names, and look them up. */
17634
17635 tree cname = DECL_NAME (decl);
17636 tree val = DECL_VALUE_EXPR (decl);
17637 tree field = TREE_OPERAND (val, 1);
17638 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17639 tree fpack = retrieve_local_specialization (field);
17640 if (fpack == error_mark_node)
17641 return error_mark_node;
17642
17643 int len = 1;
17644 tree vec = NULL_TREE;
17645 tree r = NULL_TREE;
17646 if (TREE_CODE (fpack) == TREE_VEC)
17647 {
17648 len = TREE_VEC_LENGTH (fpack);
17649 vec = make_tree_vec (len);
17650 r = make_node (NONTYPE_ARGUMENT_PACK);
17651 SET_ARGUMENT_PACK_ARGS (r, vec);
17652 }
17653 for (int i = 0; i < len; ++i)
17654 {
17655 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17656 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17657 if (vec)
17658 TREE_VEC_ELT (vec, i) = elt;
17659 else
17660 r = elt;
17661 }
17662 return r;
17663 }
17664
17665 /* Like tsubst_copy for expressions, etc. but also does semantic
17666 processing. */
17667
17668 tree
17669 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17670 bool integral_constant_expression_p)
17671 {
17672 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17673 #define RECUR(NODE) \
17674 tsubst_expr ((NODE), args, complain, in_decl, \
17675 integral_constant_expression_p)
17676
17677 tree stmt, tmp;
17678 tree r;
17679 location_t loc;
17680
17681 if (t == NULL_TREE || t == error_mark_node)
17682 return t;
17683
17684 loc = input_location;
17685 if (location_t eloc = cp_expr_location (t))
17686 input_location = eloc;
17687 if (STATEMENT_CODE_P (TREE_CODE (t)))
17688 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17689
17690 switch (TREE_CODE (t))
17691 {
17692 case STATEMENT_LIST:
17693 {
17694 tree_stmt_iterator i;
17695 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17696 RECUR (tsi_stmt (i));
17697 break;
17698 }
17699
17700 case CTOR_INITIALIZER:
17701 finish_mem_initializers (tsubst_initializer_list
17702 (TREE_OPERAND (t, 0), args));
17703 break;
17704
17705 case RETURN_EXPR:
17706 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17707 break;
17708
17709 case CO_RETURN_EXPR:
17710 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17711 break;
17712
17713 case CO_YIELD_EXPR:
17714 stmt = finish_co_yield_expr (input_location,
17715 RECUR (TREE_OPERAND (t, 0)));
17716 RETURN (stmt);
17717 break;
17718
17719 case CO_AWAIT_EXPR:
17720 stmt = finish_co_await_expr (input_location,
17721 RECUR (TREE_OPERAND (t, 0)));
17722 RETURN (stmt);
17723 break;
17724
17725 case EXPR_STMT:
17726 tmp = RECUR (EXPR_STMT_EXPR (t));
17727 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17728 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17729 else
17730 finish_expr_stmt (tmp);
17731 break;
17732
17733 case USING_STMT:
17734 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17735 break;
17736
17737 case DECL_EXPR:
17738 {
17739 tree decl, pattern_decl;
17740 tree init;
17741
17742 pattern_decl = decl = DECL_EXPR_DECL (t);
17743 if (TREE_CODE (decl) == LABEL_DECL)
17744 finish_label_decl (DECL_NAME (decl));
17745 else if (TREE_CODE (decl) == USING_DECL)
17746 {
17747 tree scope = USING_DECL_SCOPE (decl);
17748 tree name = DECL_NAME (decl);
17749
17750 scope = tsubst (scope, args, complain, in_decl);
17751 finish_nonmember_using_decl (scope, name);
17752 }
17753 else if (is_capture_proxy (decl)
17754 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17755 {
17756 /* We're in tsubst_lambda_expr, we've already inserted a new
17757 capture proxy, so look it up and register it. */
17758 tree inst;
17759 if (!DECL_PACK_P (decl))
17760 {
17761 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17762 /*nonclass*/1, /*block_p=*/true,
17763 /*ns_only*/0, LOOKUP_HIDDEN);
17764 gcc_assert (inst != decl && is_capture_proxy (inst));
17765 }
17766 else if (is_normal_capture_proxy (decl))
17767 {
17768 inst = (retrieve_local_specialization
17769 (DECL_CAPTURED_VARIABLE (decl)));
17770 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17771 }
17772 else
17773 inst = lookup_init_capture_pack (decl);
17774
17775 register_local_specialization (inst, decl);
17776 break;
17777 }
17778 else if (DECL_PRETTY_FUNCTION_P (decl))
17779 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17780 DECL_NAME (decl),
17781 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17782 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17783 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17784 /* Don't copy the old closure; we'll create a new one in
17785 tsubst_lambda_expr. */
17786 break;
17787 else
17788 {
17789 init = DECL_INITIAL (decl);
17790 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17791 for local variables, so save if DECL was declared constinit. */
17792 const bool constinit_p
17793 = (VAR_P (decl)
17794 && DECL_LANG_SPECIFIC (decl)
17795 && DECL_TEMPLATE_INFO (decl)
17796 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17797 decl = tsubst (decl, args, complain, in_decl);
17798 if (decl != error_mark_node)
17799 {
17800 /* By marking the declaration as instantiated, we avoid
17801 trying to instantiate it. Since instantiate_decl can't
17802 handle local variables, and since we've already done
17803 all that needs to be done, that's the right thing to
17804 do. */
17805 if (VAR_P (decl))
17806 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17807 if (VAR_P (decl) && !DECL_NAME (decl)
17808 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17809 /* Anonymous aggregates are a special case. */
17810 finish_anon_union (decl);
17811 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17812 {
17813 DECL_CONTEXT (decl) = current_function_decl;
17814 if (DECL_NAME (decl) == this_identifier)
17815 {
17816 tree lam = DECL_CONTEXT (current_function_decl);
17817 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17818 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17819 }
17820 insert_capture_proxy (decl);
17821 }
17822 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17823 /* We already did a pushtag. */;
17824 else if (TREE_CODE (decl) == FUNCTION_DECL
17825 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17826 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17827 {
17828 DECL_CONTEXT (decl) = NULL_TREE;
17829 pushdecl (decl);
17830 DECL_CONTEXT (decl) = current_function_decl;
17831 cp_check_omp_declare_reduction (decl);
17832 }
17833 else
17834 {
17835 bool const_init = false;
17836 unsigned int cnt = 0;
17837 tree first = NULL_TREE, ndecl = error_mark_node;
17838 maybe_push_decl (decl);
17839
17840 if (VAR_P (decl)
17841 && DECL_DECOMPOSITION_P (decl)
17842 && TREE_TYPE (pattern_decl) != error_mark_node)
17843 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17844 complain, in_decl, &first,
17845 &cnt);
17846
17847 init = tsubst_init (init, decl, args, complain, in_decl);
17848
17849 if (VAR_P (decl))
17850 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17851 (pattern_decl));
17852
17853 if (ndecl != error_mark_node)
17854 cp_maybe_mangle_decomp (ndecl, first, cnt);
17855
17856 cp_finish_decl (decl, init, const_init, NULL_TREE,
17857 constinit_p ? LOOKUP_CONSTINIT : 0);
17858
17859 if (ndecl != error_mark_node)
17860 cp_finish_decomp (ndecl, first, cnt);
17861 }
17862 }
17863 }
17864
17865 break;
17866 }
17867
17868 case FOR_STMT:
17869 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17870 RECUR (FOR_INIT_STMT (t));
17871 finish_init_stmt (stmt);
17872 tmp = RECUR (FOR_COND (t));
17873 finish_for_cond (tmp, stmt, false, 0);
17874 tmp = RECUR (FOR_EXPR (t));
17875 finish_for_expr (tmp, stmt);
17876 {
17877 bool prev = note_iteration_stmt_body_start ();
17878 RECUR (FOR_BODY (t));
17879 note_iteration_stmt_body_end (prev);
17880 }
17881 finish_for_stmt (stmt);
17882 break;
17883
17884 case RANGE_FOR_STMT:
17885 {
17886 /* Construct another range_for, if this is not a final
17887 substitution (for inside a generic lambda of a
17888 template). Otherwise convert to a regular for. */
17889 tree decl, expr;
17890 stmt = (processing_template_decl
17891 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17892 : begin_for_stmt (NULL_TREE, NULL_TREE));
17893 RECUR (RANGE_FOR_INIT_STMT (t));
17894 decl = RANGE_FOR_DECL (t);
17895 decl = tsubst (decl, args, complain, in_decl);
17896 maybe_push_decl (decl);
17897 expr = RECUR (RANGE_FOR_EXPR (t));
17898
17899 tree decomp_first = NULL_TREE;
17900 unsigned decomp_cnt = 0;
17901 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17902 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17903 complain, in_decl,
17904 &decomp_first, &decomp_cnt);
17905
17906 if (processing_template_decl)
17907 {
17908 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17909 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17910 finish_range_for_decl (stmt, decl, expr);
17911 if (decomp_first && decl != error_mark_node)
17912 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17913 }
17914 else
17915 {
17916 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17917 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17918 stmt = cp_convert_range_for (stmt, decl, expr,
17919 decomp_first, decomp_cnt,
17920 RANGE_FOR_IVDEP (t), unroll);
17921 }
17922
17923 bool prev = note_iteration_stmt_body_start ();
17924 RECUR (RANGE_FOR_BODY (t));
17925 note_iteration_stmt_body_end (prev);
17926 finish_for_stmt (stmt);
17927 }
17928 break;
17929
17930 case WHILE_STMT:
17931 stmt = begin_while_stmt ();
17932 tmp = RECUR (WHILE_COND (t));
17933 finish_while_stmt_cond (tmp, stmt, false, 0);
17934 {
17935 bool prev = note_iteration_stmt_body_start ();
17936 RECUR (WHILE_BODY (t));
17937 note_iteration_stmt_body_end (prev);
17938 }
17939 finish_while_stmt (stmt);
17940 break;
17941
17942 case DO_STMT:
17943 stmt = begin_do_stmt ();
17944 {
17945 bool prev = note_iteration_stmt_body_start ();
17946 RECUR (DO_BODY (t));
17947 note_iteration_stmt_body_end (prev);
17948 }
17949 finish_do_body (stmt);
17950 tmp = RECUR (DO_COND (t));
17951 finish_do_stmt (tmp, stmt, false, 0);
17952 break;
17953
17954 case IF_STMT:
17955 stmt = begin_if_stmt ();
17956 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17957 if (IF_STMT_CONSTEXPR_P (t))
17958 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17959 tmp = RECUR (IF_COND (t));
17960 tmp = finish_if_stmt_cond (tmp, stmt);
17961 if (IF_STMT_CONSTEXPR_P (t)
17962 && instantiation_dependent_expression_p (tmp))
17963 {
17964 /* We're partially instantiating a generic lambda, but the condition
17965 of the constexpr if is still dependent. Don't substitute into the
17966 branches now, just remember the template arguments. */
17967 do_poplevel (IF_SCOPE (stmt));
17968 IF_COND (stmt) = IF_COND (t);
17969 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17970 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17971 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17972 add_stmt (stmt);
17973 break;
17974 }
17975 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17976 /* Don't instantiate the THEN_CLAUSE. */;
17977 else
17978 {
17979 tree folded = fold_non_dependent_expr (tmp, complain);
17980 bool inhibit = integer_zerop (folded);
17981 if (inhibit)
17982 ++c_inhibit_evaluation_warnings;
17983 RECUR (THEN_CLAUSE (t));
17984 if (inhibit)
17985 --c_inhibit_evaluation_warnings;
17986 }
17987 finish_then_clause (stmt);
17988
17989 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17990 /* Don't instantiate the ELSE_CLAUSE. */;
17991 else if (ELSE_CLAUSE (t))
17992 {
17993 tree folded = fold_non_dependent_expr (tmp, complain);
17994 bool inhibit = integer_nonzerop (folded);
17995 begin_else_clause (stmt);
17996 if (inhibit)
17997 ++c_inhibit_evaluation_warnings;
17998 RECUR (ELSE_CLAUSE (t));
17999 if (inhibit)
18000 --c_inhibit_evaluation_warnings;
18001 finish_else_clause (stmt);
18002 }
18003
18004 finish_if_stmt (stmt);
18005 break;
18006
18007 case BIND_EXPR:
18008 if (BIND_EXPR_BODY_BLOCK (t))
18009 stmt = begin_function_body ();
18010 else
18011 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18012 ? BCS_TRY_BLOCK : 0);
18013
18014 RECUR (BIND_EXPR_BODY (t));
18015
18016 if (BIND_EXPR_BODY_BLOCK (t))
18017 finish_function_body (stmt);
18018 else
18019 finish_compound_stmt (stmt);
18020 break;
18021
18022 case BREAK_STMT:
18023 finish_break_stmt ();
18024 break;
18025
18026 case CONTINUE_STMT:
18027 finish_continue_stmt ();
18028 break;
18029
18030 case SWITCH_STMT:
18031 stmt = begin_switch_stmt ();
18032 tmp = RECUR (SWITCH_STMT_COND (t));
18033 finish_switch_cond (tmp, stmt);
18034 RECUR (SWITCH_STMT_BODY (t));
18035 finish_switch_stmt (stmt);
18036 break;
18037
18038 case CASE_LABEL_EXPR:
18039 {
18040 tree decl = CASE_LABEL (t);
18041 tree low = RECUR (CASE_LOW (t));
18042 tree high = RECUR (CASE_HIGH (t));
18043 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18044 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18045 {
18046 tree label = CASE_LABEL (l);
18047 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18048 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18049 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18050 }
18051 }
18052 break;
18053
18054 case LABEL_EXPR:
18055 {
18056 tree decl = LABEL_EXPR_LABEL (t);
18057 tree label;
18058
18059 label = finish_label_stmt (DECL_NAME (decl));
18060 if (TREE_CODE (label) == LABEL_DECL)
18061 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18062 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18063 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18064 }
18065 break;
18066
18067 case GOTO_EXPR:
18068 tmp = GOTO_DESTINATION (t);
18069 if (TREE_CODE (tmp) != LABEL_DECL)
18070 /* Computed goto's must be tsubst'd into. On the other hand,
18071 non-computed gotos must not be; the identifier in question
18072 will have no binding. */
18073 tmp = RECUR (tmp);
18074 else
18075 tmp = DECL_NAME (tmp);
18076 finish_goto_stmt (tmp);
18077 break;
18078
18079 case ASM_EXPR:
18080 {
18081 tree string = RECUR (ASM_STRING (t));
18082 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18083 complain, in_decl);
18084 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18085 complain, in_decl);
18086 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18087 complain, in_decl);
18088 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18089 complain, in_decl);
18090 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18091 outputs, inputs, clobbers, labels,
18092 ASM_INLINE_P (t));
18093 tree asm_expr = tmp;
18094 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18095 asm_expr = TREE_OPERAND (asm_expr, 0);
18096 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18097 }
18098 break;
18099
18100 case TRY_BLOCK:
18101 if (CLEANUP_P (t))
18102 {
18103 stmt = begin_try_block ();
18104 RECUR (TRY_STMTS (t));
18105 finish_cleanup_try_block (stmt);
18106 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18107 }
18108 else
18109 {
18110 tree compound_stmt = NULL_TREE;
18111
18112 if (FN_TRY_BLOCK_P (t))
18113 stmt = begin_function_try_block (&compound_stmt);
18114 else
18115 stmt = begin_try_block ();
18116
18117 RECUR (TRY_STMTS (t));
18118
18119 if (FN_TRY_BLOCK_P (t))
18120 finish_function_try_block (stmt);
18121 else
18122 finish_try_block (stmt);
18123
18124 RECUR (TRY_HANDLERS (t));
18125 if (FN_TRY_BLOCK_P (t))
18126 finish_function_handler_sequence (stmt, compound_stmt);
18127 else
18128 finish_handler_sequence (stmt);
18129 }
18130 break;
18131
18132 case HANDLER:
18133 {
18134 tree decl = HANDLER_PARMS (t);
18135
18136 if (decl)
18137 {
18138 decl = tsubst (decl, args, complain, in_decl);
18139 /* Prevent instantiate_decl from trying to instantiate
18140 this variable. We've already done all that needs to be
18141 done. */
18142 if (decl != error_mark_node)
18143 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18144 }
18145 stmt = begin_handler ();
18146 finish_handler_parms (decl, stmt);
18147 RECUR (HANDLER_BODY (t));
18148 finish_handler (stmt);
18149 }
18150 break;
18151
18152 case TAG_DEFN:
18153 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18154 if (CLASS_TYPE_P (tmp))
18155 {
18156 /* Local classes are not independent templates; they are
18157 instantiated along with their containing function. And this
18158 way we don't have to deal with pushing out of one local class
18159 to instantiate a member of another local class. */
18160 /* Closures are handled by the LAMBDA_EXPR. */
18161 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18162 complete_type (tmp);
18163 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18164 if ((VAR_P (fld)
18165 || (TREE_CODE (fld) == FUNCTION_DECL
18166 && !DECL_ARTIFICIAL (fld)))
18167 && DECL_TEMPLATE_INSTANTIATION (fld))
18168 instantiate_decl (fld, /*defer_ok=*/false,
18169 /*expl_inst_class=*/false);
18170 }
18171 break;
18172
18173 case STATIC_ASSERT:
18174 {
18175 tree condition;
18176
18177 ++c_inhibit_evaluation_warnings;
18178 condition =
18179 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18180 args,
18181 complain, in_decl,
18182 /*integral_constant_expression_p=*/true);
18183 --c_inhibit_evaluation_warnings;
18184
18185 finish_static_assert (condition,
18186 STATIC_ASSERT_MESSAGE (t),
18187 STATIC_ASSERT_SOURCE_LOCATION (t),
18188 /*member_p=*/false);
18189 }
18190 break;
18191
18192 case OACC_KERNELS:
18193 case OACC_PARALLEL:
18194 case OACC_SERIAL:
18195 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18196 in_decl);
18197 stmt = begin_omp_parallel ();
18198 RECUR (OMP_BODY (t));
18199 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18200 break;
18201
18202 case OMP_PARALLEL:
18203 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18204 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18205 complain, in_decl);
18206 if (OMP_PARALLEL_COMBINED (t))
18207 omp_parallel_combined_clauses = &tmp;
18208 stmt = begin_omp_parallel ();
18209 RECUR (OMP_PARALLEL_BODY (t));
18210 gcc_assert (omp_parallel_combined_clauses == NULL);
18211 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18212 = OMP_PARALLEL_COMBINED (t);
18213 pop_omp_privatization_clauses (r);
18214 break;
18215
18216 case OMP_TASK:
18217 if (OMP_TASK_BODY (t) == NULL_TREE)
18218 {
18219 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18220 complain, in_decl);
18221 t = copy_node (t);
18222 OMP_TASK_CLAUSES (t) = tmp;
18223 add_stmt (t);
18224 break;
18225 }
18226 r = push_omp_privatization_clauses (false);
18227 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18228 complain, in_decl);
18229 stmt = begin_omp_task ();
18230 RECUR (OMP_TASK_BODY (t));
18231 finish_omp_task (tmp, stmt);
18232 pop_omp_privatization_clauses (r);
18233 break;
18234
18235 case OMP_FOR:
18236 case OMP_LOOP:
18237 case OMP_SIMD:
18238 case OMP_DISTRIBUTE:
18239 case OMP_TASKLOOP:
18240 case OACC_LOOP:
18241 {
18242 tree clauses, body, pre_body;
18243 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18244 tree orig_declv = NULL_TREE;
18245 tree incrv = NULL_TREE;
18246 enum c_omp_region_type ort = C_ORT_OMP;
18247 bool any_range_for = false;
18248 int i;
18249
18250 if (TREE_CODE (t) == OACC_LOOP)
18251 ort = C_ORT_ACC;
18252
18253 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18254 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18255 in_decl);
18256 if (OMP_FOR_INIT (t) != NULL_TREE)
18257 {
18258 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18259 if (OMP_FOR_ORIG_DECLS (t))
18260 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18261 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18262 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18263 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18264 }
18265
18266 keep_next_level (true);
18267 stmt = begin_omp_structured_block ();
18268
18269 pre_body = push_stmt_list ();
18270 RECUR (OMP_FOR_PRE_BODY (t));
18271 pre_body = pop_stmt_list (pre_body);
18272
18273 if (OMP_FOR_INIT (t) != NULL_TREE)
18274 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18275 any_range_for
18276 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18277 condv, incrv, &clauses, args,
18278 complain, in_decl,
18279 integral_constant_expression_p);
18280 omp_parallel_combined_clauses = NULL;
18281
18282 if (any_range_for)
18283 {
18284 gcc_assert (orig_declv);
18285 body = begin_omp_structured_block ();
18286 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18287 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18288 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18289 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18290 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18291 TREE_VEC_ELT (declv, i));
18292 }
18293 else
18294 body = push_stmt_list ();
18295 RECUR (OMP_FOR_BODY (t));
18296 if (any_range_for)
18297 body = finish_omp_structured_block (body);
18298 else
18299 body = pop_stmt_list (body);
18300
18301 if (OMP_FOR_INIT (t) != NULL_TREE)
18302 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18303 orig_declv, initv, condv, incrv, body, pre_body,
18304 NULL, clauses);
18305 else
18306 {
18307 t = make_node (TREE_CODE (t));
18308 TREE_TYPE (t) = void_type_node;
18309 OMP_FOR_BODY (t) = body;
18310 OMP_FOR_PRE_BODY (t) = pre_body;
18311 OMP_FOR_CLAUSES (t) = clauses;
18312 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18313 add_stmt (t);
18314 }
18315
18316 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18317 t));
18318 pop_omp_privatization_clauses (r);
18319 }
18320 break;
18321
18322 case OMP_SECTIONS:
18323 omp_parallel_combined_clauses = NULL;
18324 /* FALLTHRU */
18325 case OMP_SINGLE:
18326 case OMP_TEAMS:
18327 case OMP_CRITICAL:
18328 case OMP_TASKGROUP:
18329 case OMP_SCAN:
18330 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18331 && OMP_TEAMS_COMBINED (t));
18332 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18333 in_decl);
18334 if (TREE_CODE (t) == OMP_TEAMS)
18335 {
18336 keep_next_level (true);
18337 stmt = begin_omp_structured_block ();
18338 RECUR (OMP_BODY (t));
18339 stmt = finish_omp_structured_block (stmt);
18340 }
18341 else
18342 {
18343 stmt = push_stmt_list ();
18344 RECUR (OMP_BODY (t));
18345 stmt = pop_stmt_list (stmt);
18346 }
18347
18348 t = copy_node (t);
18349 OMP_BODY (t) = stmt;
18350 OMP_CLAUSES (t) = tmp;
18351 add_stmt (t);
18352 pop_omp_privatization_clauses (r);
18353 break;
18354
18355 case OMP_DEPOBJ:
18356 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18357 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18358 {
18359 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18360 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18361 {
18362 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18363 args, complain, in_decl);
18364 if (tmp == NULL_TREE)
18365 tmp = error_mark_node;
18366 }
18367 else
18368 {
18369 kind = (enum omp_clause_depend_kind)
18370 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18371 tmp = NULL_TREE;
18372 }
18373 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18374 }
18375 else
18376 finish_omp_depobj (EXPR_LOCATION (t), r,
18377 OMP_CLAUSE_DEPEND_SOURCE,
18378 OMP_DEPOBJ_CLAUSES (t));
18379 break;
18380
18381 case OACC_DATA:
18382 case OMP_TARGET_DATA:
18383 case OMP_TARGET:
18384 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18385 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18386 in_decl);
18387 keep_next_level (true);
18388 stmt = begin_omp_structured_block ();
18389
18390 RECUR (OMP_BODY (t));
18391 stmt = finish_omp_structured_block (stmt);
18392
18393 t = copy_node (t);
18394 OMP_BODY (t) = stmt;
18395 OMP_CLAUSES (t) = tmp;
18396 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18397 {
18398 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18399 if (teams)
18400 {
18401 /* For combined target teams, ensure the num_teams and
18402 thread_limit clause expressions are evaluated on the host,
18403 before entering the target construct. */
18404 tree c;
18405 for (c = OMP_TEAMS_CLAUSES (teams);
18406 c; c = OMP_CLAUSE_CHAIN (c))
18407 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18408 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18409 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18410 {
18411 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18412 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18413 if (expr == error_mark_node)
18414 continue;
18415 tmp = TARGET_EXPR_SLOT (expr);
18416 add_stmt (expr);
18417 OMP_CLAUSE_OPERAND (c, 0) = expr;
18418 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18419 OMP_CLAUSE_FIRSTPRIVATE);
18420 OMP_CLAUSE_DECL (tc) = tmp;
18421 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18422 OMP_TARGET_CLAUSES (t) = tc;
18423 }
18424 }
18425 }
18426 add_stmt (t);
18427 break;
18428
18429 case OACC_DECLARE:
18430 t = copy_node (t);
18431 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18432 complain, in_decl);
18433 OACC_DECLARE_CLAUSES (t) = tmp;
18434 add_stmt (t);
18435 break;
18436
18437 case OMP_TARGET_UPDATE:
18438 case OMP_TARGET_ENTER_DATA:
18439 case OMP_TARGET_EXIT_DATA:
18440 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18441 complain, in_decl);
18442 t = copy_node (t);
18443 OMP_STANDALONE_CLAUSES (t) = tmp;
18444 add_stmt (t);
18445 break;
18446
18447 case OACC_ENTER_DATA:
18448 case OACC_EXIT_DATA:
18449 case OACC_UPDATE:
18450 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18451 complain, in_decl);
18452 t = copy_node (t);
18453 OMP_STANDALONE_CLAUSES (t) = tmp;
18454 add_stmt (t);
18455 break;
18456
18457 case OMP_ORDERED:
18458 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18459 complain, in_decl);
18460 stmt = push_stmt_list ();
18461 RECUR (OMP_BODY (t));
18462 stmt = pop_stmt_list (stmt);
18463
18464 t = copy_node (t);
18465 OMP_BODY (t) = stmt;
18466 OMP_ORDERED_CLAUSES (t) = tmp;
18467 add_stmt (t);
18468 break;
18469
18470 case OMP_SECTION:
18471 case OMP_MASTER:
18472 stmt = push_stmt_list ();
18473 RECUR (OMP_BODY (t));
18474 stmt = pop_stmt_list (stmt);
18475
18476 t = copy_node (t);
18477 OMP_BODY (t) = stmt;
18478 add_stmt (t);
18479 break;
18480
18481 case OMP_ATOMIC:
18482 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18483 tmp = NULL_TREE;
18484 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18485 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18486 complain, in_decl);
18487 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18488 {
18489 tree op1 = TREE_OPERAND (t, 1);
18490 tree rhs1 = NULL_TREE;
18491 tree lhs, rhs;
18492 if (TREE_CODE (op1) == COMPOUND_EXPR)
18493 {
18494 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18495 op1 = TREE_OPERAND (op1, 1);
18496 }
18497 lhs = RECUR (TREE_OPERAND (op1, 0));
18498 rhs = RECUR (TREE_OPERAND (op1, 1));
18499 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18500 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18501 OMP_ATOMIC_MEMORY_ORDER (t));
18502 }
18503 else
18504 {
18505 tree op1 = TREE_OPERAND (t, 1);
18506 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18507 tree rhs1 = NULL_TREE;
18508 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18509 enum tree_code opcode = NOP_EXPR;
18510 if (code == OMP_ATOMIC_READ)
18511 {
18512 v = RECUR (TREE_OPERAND (op1, 0));
18513 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18514 }
18515 else if (code == OMP_ATOMIC_CAPTURE_OLD
18516 || code == OMP_ATOMIC_CAPTURE_NEW)
18517 {
18518 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18519 v = RECUR (TREE_OPERAND (op1, 0));
18520 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18521 if (TREE_CODE (op11) == COMPOUND_EXPR)
18522 {
18523 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18524 op11 = TREE_OPERAND (op11, 1);
18525 }
18526 lhs = RECUR (TREE_OPERAND (op11, 0));
18527 rhs = RECUR (TREE_OPERAND (op11, 1));
18528 opcode = TREE_CODE (op11);
18529 if (opcode == MODIFY_EXPR)
18530 opcode = NOP_EXPR;
18531 }
18532 else
18533 {
18534 code = OMP_ATOMIC;
18535 lhs = RECUR (TREE_OPERAND (op1, 0));
18536 rhs = RECUR (TREE_OPERAND (op1, 1));
18537 }
18538 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18539 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18540 }
18541 break;
18542
18543 case TRANSACTION_EXPR:
18544 {
18545 int flags = 0;
18546 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18547 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18548
18549 if (TRANSACTION_EXPR_IS_STMT (t))
18550 {
18551 tree body = TRANSACTION_EXPR_BODY (t);
18552 tree noex = NULL_TREE;
18553 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18554 {
18555 noex = MUST_NOT_THROW_COND (body);
18556 if (noex == NULL_TREE)
18557 noex = boolean_true_node;
18558 body = TREE_OPERAND (body, 0);
18559 }
18560 stmt = begin_transaction_stmt (input_location, NULL, flags);
18561 RECUR (body);
18562 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18563 }
18564 else
18565 {
18566 stmt = build_transaction_expr (EXPR_LOCATION (t),
18567 RECUR (TRANSACTION_EXPR_BODY (t)),
18568 flags, NULL_TREE);
18569 RETURN (stmt);
18570 }
18571 }
18572 break;
18573
18574 case MUST_NOT_THROW_EXPR:
18575 {
18576 tree op0 = RECUR (TREE_OPERAND (t, 0));
18577 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18578 RETURN (build_must_not_throw_expr (op0, cond));
18579 }
18580
18581 case EXPR_PACK_EXPANSION:
18582 error ("invalid use of pack expansion expression");
18583 RETURN (error_mark_node);
18584
18585 case NONTYPE_ARGUMENT_PACK:
18586 error ("use %<...%> to expand argument pack");
18587 RETURN (error_mark_node);
18588
18589 case COMPOUND_EXPR:
18590 tmp = RECUR (TREE_OPERAND (t, 0));
18591 if (tmp == NULL_TREE)
18592 /* If the first operand was a statement, we're done with it. */
18593 RETURN (RECUR (TREE_OPERAND (t, 1)));
18594 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18595 RECUR (TREE_OPERAND (t, 1)),
18596 complain));
18597
18598 case ANNOTATE_EXPR:
18599 tmp = RECUR (TREE_OPERAND (t, 0));
18600 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18601 TREE_TYPE (tmp), tmp,
18602 RECUR (TREE_OPERAND (t, 1)),
18603 RECUR (TREE_OPERAND (t, 2))));
18604
18605 case PREDICT_EXPR:
18606 RETURN (add_stmt (copy_node (t)));
18607
18608 default:
18609 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18610
18611 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18612 /*function_p=*/false,
18613 integral_constant_expression_p));
18614 }
18615
18616 RETURN (NULL_TREE);
18617 out:
18618 input_location = loc;
18619 return r;
18620 #undef RECUR
18621 #undef RETURN
18622 }
18623
18624 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18625 function. For description of the body see comment above
18626 cp_parser_omp_declare_reduction_exprs. */
18627
18628 static void
18629 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18630 {
18631 if (t == NULL_TREE || t == error_mark_node)
18632 return;
18633
18634 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18635
18636 tree_stmt_iterator tsi;
18637 int i;
18638 tree stmts[7];
18639 memset (stmts, 0, sizeof stmts);
18640 for (i = 0, tsi = tsi_start (t);
18641 i < 7 && !tsi_end_p (tsi);
18642 i++, tsi_next (&tsi))
18643 stmts[i] = tsi_stmt (tsi);
18644 gcc_assert (tsi_end_p (tsi));
18645
18646 if (i >= 3)
18647 {
18648 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18649 && TREE_CODE (stmts[1]) == DECL_EXPR);
18650 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18651 args, complain, in_decl);
18652 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18653 args, complain, in_decl);
18654 DECL_CONTEXT (omp_out) = current_function_decl;
18655 DECL_CONTEXT (omp_in) = current_function_decl;
18656 keep_next_level (true);
18657 tree block = begin_omp_structured_block ();
18658 tsubst_expr (stmts[2], args, complain, in_decl, false);
18659 block = finish_omp_structured_block (block);
18660 block = maybe_cleanup_point_expr_void (block);
18661 add_decl_expr (omp_out);
18662 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18663 TREE_NO_WARNING (omp_out) = 1;
18664 add_decl_expr (omp_in);
18665 finish_expr_stmt (block);
18666 }
18667 if (i >= 6)
18668 {
18669 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18670 && TREE_CODE (stmts[4]) == DECL_EXPR);
18671 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18672 args, complain, in_decl);
18673 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18674 args, complain, in_decl);
18675 DECL_CONTEXT (omp_priv) = current_function_decl;
18676 DECL_CONTEXT (omp_orig) = current_function_decl;
18677 keep_next_level (true);
18678 tree block = begin_omp_structured_block ();
18679 tsubst_expr (stmts[5], args, complain, in_decl, false);
18680 block = finish_omp_structured_block (block);
18681 block = maybe_cleanup_point_expr_void (block);
18682 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18683 add_decl_expr (omp_priv);
18684 add_decl_expr (omp_orig);
18685 finish_expr_stmt (block);
18686 if (i == 7)
18687 add_decl_expr (omp_orig);
18688 }
18689 }
18690
18691 /* T is a postfix-expression that is not being used in a function
18692 call. Return the substituted version of T. */
18693
18694 static tree
18695 tsubst_non_call_postfix_expression (tree t, tree args,
18696 tsubst_flags_t complain,
18697 tree in_decl)
18698 {
18699 if (TREE_CODE (t) == SCOPE_REF)
18700 t = tsubst_qualified_id (t, args, complain, in_decl,
18701 /*done=*/false, /*address_p=*/false);
18702 else
18703 t = tsubst_copy_and_build (t, args, complain, in_decl,
18704 /*function_p=*/false,
18705 /*integral_constant_expression_p=*/false);
18706
18707 return t;
18708 }
18709
18710 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18711 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18712 dependent init-capture. */
18713
18714 static void
18715 prepend_one_capture (tree field, tree init, tree &list,
18716 tsubst_flags_t complain)
18717 {
18718 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18719 {
18720 tree type = NULL_TREE;
18721 if (!init)
18722 {
18723 if (complain & tf_error)
18724 error ("empty initializer in lambda init-capture");
18725 init = error_mark_node;
18726 }
18727 else if (TREE_CODE (init) == TREE_LIST)
18728 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18729 if (!type)
18730 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18731 TREE_TYPE (field) = type;
18732 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18733 }
18734 list = tree_cons (field, init, list);
18735 }
18736
18737 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18738 instantiation context. Instantiating a pack expansion containing a lambda
18739 might result in multiple lambdas all based on the same lambda in the
18740 template. */
18741
18742 tree
18743 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18744 {
18745 tree oldfn = lambda_function (t);
18746 in_decl = oldfn;
18747
18748 tree r = build_lambda_expr ();
18749
18750 LAMBDA_EXPR_LOCATION (r)
18751 = LAMBDA_EXPR_LOCATION (t);
18752 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18753 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18754 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18755 LAMBDA_EXPR_INSTANTIATED (r) = true;
18756
18757 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18758 /* A lambda in a default argument outside a class gets no
18759 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18760 tsubst_default_argument calls start_lambda_scope, so we need to
18761 specifically ignore it here, and use the global scope. */
18762 record_null_lambda_scope (r);
18763 else
18764 record_lambda_scope (r);
18765
18766 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18767 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18768
18769 vec<tree,va_gc>* field_packs = NULL;
18770
18771 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18772 cap = TREE_CHAIN (cap))
18773 {
18774 tree ofield = TREE_PURPOSE (cap);
18775 tree init = TREE_VALUE (cap);
18776 if (PACK_EXPANSION_P (init))
18777 init = tsubst_pack_expansion (init, args, complain, in_decl);
18778 else
18779 init = tsubst_copy_and_build (init, args, complain, in_decl,
18780 /*fn*/false, /*constexpr*/false);
18781
18782 if (init == error_mark_node)
18783 return error_mark_node;
18784
18785 if (init && TREE_CODE (init) == TREE_LIST)
18786 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18787
18788 if (!processing_template_decl
18789 && init && TREE_CODE (init) != TREE_VEC
18790 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
18791 {
18792 /* For a VLA, simply tsubsting the field type won't work, we need to
18793 go through add_capture again. XXX do we want to do this for all
18794 captures? */
18795 tree name = (get_identifier
18796 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
18797 tree ftype = TREE_TYPE (ofield);
18798 bool by_ref = (TYPE_REF_P (ftype)
18799 || (TREE_CODE (ftype) == DECLTYPE_TYPE
18800 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
18801 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
18802 continue;
18803 }
18804
18805 if (PACK_EXPANSION_P (ofield))
18806 ofield = PACK_EXPANSION_PATTERN (ofield);
18807 tree field = tsubst_decl (ofield, args, complain);
18808
18809 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18810 {
18811 /* Remember these for when we've pushed local_specializations. */
18812 vec_safe_push (field_packs, ofield);
18813 vec_safe_push (field_packs, field);
18814 }
18815
18816 if (field == error_mark_node)
18817 return error_mark_node;
18818
18819 if (TREE_CODE (field) == TREE_VEC)
18820 {
18821 int len = TREE_VEC_LENGTH (field);
18822 gcc_assert (TREE_CODE (init) == TREE_VEC
18823 && TREE_VEC_LENGTH (init) == len);
18824 for (int i = 0; i < len; ++i)
18825 prepend_one_capture (TREE_VEC_ELT (field, i),
18826 TREE_VEC_ELT (init, i),
18827 LAMBDA_EXPR_CAPTURE_LIST (r),
18828 complain);
18829 }
18830 else
18831 {
18832 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18833 complain);
18834
18835 if (id_equal (DECL_NAME (field), "__this"))
18836 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18837 }
18838 }
18839
18840 tree type = begin_lambda_type (r);
18841 if (type == error_mark_node)
18842 return error_mark_node;
18843
18844 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18845 determine_visibility (TYPE_NAME (type));
18846
18847 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18848
18849 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18850 ? DECL_TI_TEMPLATE (oldfn)
18851 : NULL_TREE);
18852
18853 tree fntype = static_fn_type (oldfn);
18854 if (oldtmpl)
18855 ++processing_template_decl;
18856 fntype = tsubst (fntype, args, complain, in_decl);
18857 if (oldtmpl)
18858 --processing_template_decl;
18859
18860 if (fntype == error_mark_node)
18861 r = error_mark_node;
18862 else
18863 {
18864 /* The body of a lambda-expression is not a subexpression of the
18865 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18866 which would be skipped if cp_unevaluated_operand. */
18867 cp_evaluated ev;
18868
18869 /* Fix the type of 'this'. */
18870 fntype = build_memfn_type (fntype, type,
18871 type_memfn_quals (fntype),
18872 type_memfn_rqual (fntype));
18873 tree fn, tmpl;
18874 if (oldtmpl)
18875 {
18876 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18877 fn = DECL_TEMPLATE_RESULT (tmpl);
18878 finish_member_declaration (tmpl);
18879 }
18880 else
18881 {
18882 tmpl = NULL_TREE;
18883 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18884 finish_member_declaration (fn);
18885 }
18886
18887 /* Let finish_function set this. */
18888 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18889
18890 bool nested = cfun;
18891 if (nested)
18892 push_function_context ();
18893 else
18894 /* Still increment function_depth so that we don't GC in the
18895 middle of an expression. */
18896 ++function_depth;
18897
18898 local_specialization_stack s (lss_copy);
18899
18900 tree body = start_lambda_function (fn, r);
18901
18902 /* Now record them for lookup_init_capture_pack. */
18903 int fplen = vec_safe_length (field_packs);
18904 for (int i = 0; i < fplen; )
18905 {
18906 tree pack = (*field_packs)[i++];
18907 tree inst = (*field_packs)[i++];
18908 register_local_specialization (inst, pack);
18909 }
18910 release_tree_vector (field_packs);
18911
18912 register_parameter_specializations (oldfn, fn);
18913
18914 if (oldtmpl)
18915 {
18916 /* We might not partially instantiate some parts of the function, so
18917 copy these flags from the original template. */
18918 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18919 current_function_returns_value = ol->returns_value;
18920 current_function_returns_null = ol->returns_null;
18921 current_function_returns_abnormally = ol->returns_abnormally;
18922 current_function_infinite_loop = ol->infinite_loop;
18923 }
18924
18925 /* [temp.deduct] A lambda-expression appearing in a function type or a
18926 template parameter is not considered part of the immediate context for
18927 the purposes of template argument deduction. */
18928 complain = tf_warning_or_error;
18929
18930 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18931 /*constexpr*/false);
18932
18933 finish_lambda_function (body);
18934
18935 if (nested)
18936 pop_function_context ();
18937 else
18938 --function_depth;
18939
18940 /* The capture list was built up in reverse order; fix that now. */
18941 LAMBDA_EXPR_CAPTURE_LIST (r)
18942 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18943
18944 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18945
18946 maybe_add_lambda_conv_op (type);
18947 }
18948
18949 finish_struct (type, /*attr*/NULL_TREE);
18950
18951 insert_pending_capture_proxies ();
18952
18953 return r;
18954 }
18955
18956 /* Like tsubst but deals with expressions and performs semantic
18957 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18958
18959 tree
18960 tsubst_copy_and_build (tree t,
18961 tree args,
18962 tsubst_flags_t complain,
18963 tree in_decl,
18964 bool function_p,
18965 bool integral_constant_expression_p)
18966 {
18967 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18968 #define RECUR(NODE) \
18969 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18970 /*function_p=*/false, \
18971 integral_constant_expression_p)
18972
18973 tree retval, op1;
18974 location_t save_loc;
18975
18976 if (t == NULL_TREE || t == error_mark_node)
18977 return t;
18978
18979 save_loc = input_location;
18980 if (location_t eloc = cp_expr_location (t))
18981 input_location = eloc;
18982
18983 /* N3276 decltype magic only applies to calls at the top level or on the
18984 right side of a comma. */
18985 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18986 complain &= ~tf_decltype;
18987
18988 switch (TREE_CODE (t))
18989 {
18990 case USING_DECL:
18991 t = DECL_NAME (t);
18992 /* Fall through. */
18993 case IDENTIFIER_NODE:
18994 {
18995 tree decl;
18996 cp_id_kind idk;
18997 bool non_integral_constant_expression_p;
18998 const char *error_msg;
18999
19000 if (IDENTIFIER_CONV_OP_P (t))
19001 {
19002 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19003 t = make_conv_op_name (new_type);
19004 }
19005
19006 /* Look up the name. */
19007 decl = lookup_name (t);
19008
19009 /* By convention, expressions use ERROR_MARK_NODE to indicate
19010 failure, not NULL_TREE. */
19011 if (decl == NULL_TREE)
19012 decl = error_mark_node;
19013
19014 decl = finish_id_expression (t, decl, NULL_TREE,
19015 &idk,
19016 integral_constant_expression_p,
19017 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19018 &non_integral_constant_expression_p,
19019 /*template_p=*/false,
19020 /*done=*/true,
19021 /*address_p=*/false,
19022 /*template_arg_p=*/false,
19023 &error_msg,
19024 input_location);
19025 if (error_msg)
19026 error (error_msg);
19027 if (!function_p && identifier_p (decl))
19028 {
19029 if (complain & tf_error)
19030 unqualified_name_lookup_error (decl);
19031 decl = error_mark_node;
19032 }
19033 RETURN (decl);
19034 }
19035
19036 case TEMPLATE_ID_EXPR:
19037 {
19038 tree object;
19039 tree templ = RECUR (TREE_OPERAND (t, 0));
19040 tree targs = TREE_OPERAND (t, 1);
19041
19042 if (targs)
19043 targs = tsubst_template_args (targs, args, complain, in_decl);
19044 if (targs == error_mark_node)
19045 RETURN (error_mark_node);
19046
19047 if (TREE_CODE (templ) == SCOPE_REF)
19048 {
19049 tree name = TREE_OPERAND (templ, 1);
19050 tree tid = lookup_template_function (name, targs);
19051 TREE_OPERAND (templ, 1) = tid;
19052 RETURN (templ);
19053 }
19054
19055 if (concept_definition_p (templ))
19056 {
19057 tree check = build_concept_check (templ, targs, complain);
19058 if (check == error_mark_node)
19059 RETURN (error_mark_node);
19060
19061 tree id = unpack_concept_check (check);
19062
19063 /* If we built a function concept check, return the underlying
19064 template-id. So we can evaluate it as a function call. */
19065 if (function_concept_p (TREE_OPERAND (id, 0)))
19066 RETURN (id);
19067
19068 RETURN (check);
19069 }
19070
19071 if (variable_template_p (templ))
19072 {
19073 tree r = lookup_and_finish_template_variable (templ, targs,
19074 complain);
19075 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19076 RETURN (r);
19077 }
19078
19079 if (TREE_CODE (templ) == COMPONENT_REF)
19080 {
19081 object = TREE_OPERAND (templ, 0);
19082 templ = TREE_OPERAND (templ, 1);
19083 }
19084 else
19085 object = NULL_TREE;
19086 templ = lookup_template_function (templ, targs);
19087
19088 if (object)
19089 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19090 object, templ, NULL_TREE));
19091 else
19092 RETURN (baselink_for_fns (templ));
19093 }
19094
19095 case INDIRECT_REF:
19096 {
19097 tree r = RECUR (TREE_OPERAND (t, 0));
19098
19099 if (REFERENCE_REF_P (t))
19100 {
19101 /* A type conversion to reference type will be enclosed in
19102 such an indirect ref, but the substitution of the cast
19103 will have also added such an indirect ref. */
19104 r = convert_from_reference (r);
19105 }
19106 else
19107 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19108 complain|decltype_flag);
19109
19110 if (REF_PARENTHESIZED_P (t))
19111 r = force_paren_expr (r);
19112
19113 RETURN (r);
19114 }
19115
19116 case NOP_EXPR:
19117 {
19118 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19119 tree op0 = RECUR (TREE_OPERAND (t, 0));
19120 RETURN (build_nop (type, op0));
19121 }
19122
19123 case IMPLICIT_CONV_EXPR:
19124 {
19125 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19126 tree expr = RECUR (TREE_OPERAND (t, 0));
19127 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19128 {
19129 retval = copy_node (t);
19130 TREE_TYPE (retval) = type;
19131 TREE_OPERAND (retval, 0) = expr;
19132 RETURN (retval);
19133 }
19134 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19135 /* We'll pass this to convert_nontype_argument again, we don't need
19136 to actually perform any conversion here. */
19137 RETURN (expr);
19138 int flags = LOOKUP_IMPLICIT;
19139 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19140 flags = LOOKUP_NORMAL;
19141 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19142 flags |= LOOKUP_NO_NARROWING;
19143 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19144 flags));
19145 }
19146
19147 case CONVERT_EXPR:
19148 {
19149 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19150 tree op0 = RECUR (TREE_OPERAND (t, 0));
19151 if (op0 == error_mark_node)
19152 RETURN (error_mark_node);
19153 RETURN (build1 (CONVERT_EXPR, type, op0));
19154 }
19155
19156 case CAST_EXPR:
19157 case REINTERPRET_CAST_EXPR:
19158 case CONST_CAST_EXPR:
19159 case DYNAMIC_CAST_EXPR:
19160 case STATIC_CAST_EXPR:
19161 {
19162 tree type;
19163 tree op, r = NULL_TREE;
19164
19165 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19166 if (integral_constant_expression_p
19167 && !cast_valid_in_integral_constant_expression_p (type))
19168 {
19169 if (complain & tf_error)
19170 error ("a cast to a type other than an integral or "
19171 "enumeration type cannot appear in a constant-expression");
19172 RETURN (error_mark_node);
19173 }
19174
19175 op = RECUR (TREE_OPERAND (t, 0));
19176
19177 warning_sentinel s(warn_useless_cast);
19178 warning_sentinel s2(warn_ignored_qualifiers);
19179 switch (TREE_CODE (t))
19180 {
19181 case CAST_EXPR:
19182 r = build_functional_cast (input_location, type, op, complain);
19183 break;
19184 case REINTERPRET_CAST_EXPR:
19185 r = build_reinterpret_cast (input_location, type, op, complain);
19186 break;
19187 case CONST_CAST_EXPR:
19188 r = build_const_cast (input_location, type, op, complain);
19189 break;
19190 case DYNAMIC_CAST_EXPR:
19191 r = build_dynamic_cast (input_location, type, op, complain);
19192 break;
19193 case STATIC_CAST_EXPR:
19194 r = build_static_cast (input_location, type, op, complain);
19195 break;
19196 default:
19197 gcc_unreachable ();
19198 }
19199
19200 RETURN (r);
19201 }
19202
19203 case POSTDECREMENT_EXPR:
19204 case POSTINCREMENT_EXPR:
19205 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19206 args, complain, in_decl);
19207 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19208 complain|decltype_flag));
19209
19210 case PREDECREMENT_EXPR:
19211 case PREINCREMENT_EXPR:
19212 case NEGATE_EXPR:
19213 case BIT_NOT_EXPR:
19214 case ABS_EXPR:
19215 case TRUTH_NOT_EXPR:
19216 case UNARY_PLUS_EXPR: /* Unary + */
19217 case REALPART_EXPR:
19218 case IMAGPART_EXPR:
19219 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19220 RECUR (TREE_OPERAND (t, 0)),
19221 complain|decltype_flag));
19222
19223 case FIX_TRUNC_EXPR:
19224 gcc_unreachable ();
19225
19226 case ADDR_EXPR:
19227 op1 = TREE_OPERAND (t, 0);
19228 if (TREE_CODE (op1) == LABEL_DECL)
19229 RETURN (finish_label_address_expr (DECL_NAME (op1),
19230 EXPR_LOCATION (op1)));
19231 if (TREE_CODE (op1) == SCOPE_REF)
19232 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19233 /*done=*/true, /*address_p=*/true);
19234 else
19235 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19236 in_decl);
19237 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19238 complain|decltype_flag));
19239
19240 case PLUS_EXPR:
19241 case MINUS_EXPR:
19242 case MULT_EXPR:
19243 case TRUNC_DIV_EXPR:
19244 case CEIL_DIV_EXPR:
19245 case FLOOR_DIV_EXPR:
19246 case ROUND_DIV_EXPR:
19247 case EXACT_DIV_EXPR:
19248 case BIT_AND_EXPR:
19249 case BIT_IOR_EXPR:
19250 case BIT_XOR_EXPR:
19251 case TRUNC_MOD_EXPR:
19252 case FLOOR_MOD_EXPR:
19253 case TRUTH_ANDIF_EXPR:
19254 case TRUTH_ORIF_EXPR:
19255 case TRUTH_AND_EXPR:
19256 case TRUTH_OR_EXPR:
19257 case RSHIFT_EXPR:
19258 case LSHIFT_EXPR:
19259 case EQ_EXPR:
19260 case NE_EXPR:
19261 case MAX_EXPR:
19262 case MIN_EXPR:
19263 case LE_EXPR:
19264 case GE_EXPR:
19265 case LT_EXPR:
19266 case GT_EXPR:
19267 case SPACESHIP_EXPR:
19268 case MEMBER_REF:
19269 case DOTSTAR_EXPR:
19270 {
19271 /* If T was type-dependent, suppress warnings that depend on the range
19272 of the types involved. */
19273 bool was_dep = uses_template_parms (t);
19274 warning_sentinel s1(warn_type_limits, was_dep);
19275 warning_sentinel s2(warn_div_by_zero, was_dep);
19276 warning_sentinel s3(warn_logical_op, was_dep);
19277 warning_sentinel s4(warn_tautological_compare, was_dep);
19278
19279 tree op0 = RECUR (TREE_OPERAND (t, 0));
19280 tree op1 = RECUR (TREE_OPERAND (t, 1));
19281 tree r = build_x_binary_op
19282 (input_location, TREE_CODE (t),
19283 op0,
19284 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19285 ? ERROR_MARK
19286 : TREE_CODE (TREE_OPERAND (t, 0))),
19287 op1,
19288 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19289 ? ERROR_MARK
19290 : TREE_CODE (TREE_OPERAND (t, 1))),
19291 /*overload=*/NULL,
19292 complain|decltype_flag);
19293 if (EXPR_P (r) && TREE_NO_WARNING (t))
19294 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19295
19296 RETURN (r);
19297 }
19298
19299 case POINTER_PLUS_EXPR:
19300 {
19301 tree op0 = RECUR (TREE_OPERAND (t, 0));
19302 tree op1 = RECUR (TREE_OPERAND (t, 1));
19303 RETURN (fold_build_pointer_plus (op0, op1));
19304 }
19305
19306 case SCOPE_REF:
19307 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19308 /*address_p=*/false));
19309 case ARRAY_REF:
19310 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19311 args, complain, in_decl);
19312 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19313 RECUR (TREE_OPERAND (t, 1)),
19314 complain|decltype_flag));
19315
19316 case SIZEOF_EXPR:
19317 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19318 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19319 RETURN (tsubst_copy (t, args, complain, in_decl));
19320 /* Fall through */
19321
19322 case ALIGNOF_EXPR:
19323 {
19324 tree r;
19325
19326 op1 = TREE_OPERAND (t, 0);
19327 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19328 op1 = TREE_TYPE (op1);
19329 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19330 && ALIGNOF_EXPR_STD_P (t));
19331 if (!args)
19332 {
19333 /* When there are no ARGS, we are trying to evaluate a
19334 non-dependent expression from the parser. Trying to do
19335 the substitutions may not work. */
19336 if (!TYPE_P (op1))
19337 op1 = TREE_TYPE (op1);
19338 }
19339 else
19340 {
19341 ++cp_unevaluated_operand;
19342 ++c_inhibit_evaluation_warnings;
19343 if (TYPE_P (op1))
19344 op1 = tsubst (op1, args, complain, in_decl);
19345 else
19346 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19347 /*function_p=*/false,
19348 /*integral_constant_expression_p=*/
19349 false);
19350 --cp_unevaluated_operand;
19351 --c_inhibit_evaluation_warnings;
19352 }
19353 if (TYPE_P (op1))
19354 r = cxx_sizeof_or_alignof_type (input_location,
19355 op1, TREE_CODE (t), std_alignof,
19356 complain & tf_error);
19357 else
19358 r = cxx_sizeof_or_alignof_expr (input_location,
19359 op1, TREE_CODE (t),
19360 complain & tf_error);
19361 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19362 {
19363 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19364 {
19365 if (!processing_template_decl && TYPE_P (op1))
19366 {
19367 r = build_min (SIZEOF_EXPR, size_type_node,
19368 build1 (NOP_EXPR, op1, error_mark_node));
19369 SIZEOF_EXPR_TYPE_P (r) = 1;
19370 }
19371 else
19372 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19373 TREE_SIDE_EFFECTS (r) = 0;
19374 TREE_READONLY (r) = 1;
19375 }
19376 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19377 }
19378 RETURN (r);
19379 }
19380
19381 case AT_ENCODE_EXPR:
19382 {
19383 op1 = TREE_OPERAND (t, 0);
19384 ++cp_unevaluated_operand;
19385 ++c_inhibit_evaluation_warnings;
19386 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19387 /*function_p=*/false,
19388 /*integral_constant_expression_p=*/false);
19389 --cp_unevaluated_operand;
19390 --c_inhibit_evaluation_warnings;
19391 RETURN (objc_build_encode_expr (op1));
19392 }
19393
19394 case NOEXCEPT_EXPR:
19395 op1 = TREE_OPERAND (t, 0);
19396 ++cp_unevaluated_operand;
19397 ++c_inhibit_evaluation_warnings;
19398 ++cp_noexcept_operand;
19399 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19400 /*function_p=*/false,
19401 /*integral_constant_expression_p=*/false);
19402 --cp_unevaluated_operand;
19403 --c_inhibit_evaluation_warnings;
19404 --cp_noexcept_operand;
19405 RETURN (finish_noexcept_expr (op1, complain));
19406
19407 case MODOP_EXPR:
19408 {
19409 warning_sentinel s(warn_div_by_zero);
19410 tree lhs = RECUR (TREE_OPERAND (t, 0));
19411 tree rhs = RECUR (TREE_OPERAND (t, 2));
19412 tree r = build_x_modify_expr
19413 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19414 complain|decltype_flag);
19415 /* TREE_NO_WARNING must be set if either the expression was
19416 parenthesized or it uses an operator such as >>= rather
19417 than plain assignment. In the former case, it was already
19418 set and must be copied. In the latter case,
19419 build_x_modify_expr sets it and it must not be reset
19420 here. */
19421 if (TREE_NO_WARNING (t))
19422 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19423
19424 RETURN (r);
19425 }
19426
19427 case ARROW_EXPR:
19428 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19429 args, complain, in_decl);
19430 /* Remember that there was a reference to this entity. */
19431 if (DECL_P (op1)
19432 && !mark_used (op1, complain) && !(complain & tf_error))
19433 RETURN (error_mark_node);
19434 RETURN (build_x_arrow (input_location, op1, complain));
19435
19436 case NEW_EXPR:
19437 {
19438 tree placement = RECUR (TREE_OPERAND (t, 0));
19439 tree init = RECUR (TREE_OPERAND (t, 3));
19440 vec<tree, va_gc> *placement_vec;
19441 vec<tree, va_gc> *init_vec;
19442 tree ret;
19443 location_t loc = EXPR_LOCATION (t);
19444
19445 if (placement == NULL_TREE)
19446 placement_vec = NULL;
19447 else
19448 {
19449 placement_vec = make_tree_vector ();
19450 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19451 vec_safe_push (placement_vec, TREE_VALUE (placement));
19452 }
19453
19454 /* If there was an initializer in the original tree, but it
19455 instantiated to an empty list, then we should pass a
19456 non-NULL empty vector to tell build_new that it was an
19457 empty initializer() rather than no initializer. This can
19458 only happen when the initializer is a pack expansion whose
19459 parameter packs are of length zero. */
19460 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19461 init_vec = NULL;
19462 else
19463 {
19464 init_vec = make_tree_vector ();
19465 if (init == void_node)
19466 gcc_assert (init_vec != NULL);
19467 else
19468 {
19469 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19470 vec_safe_push (init_vec, TREE_VALUE (init));
19471 }
19472 }
19473
19474 /* Avoid passing an enclosing decl to valid_array_size_p. */
19475 in_decl = NULL_TREE;
19476
19477 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19478 tree op2 = RECUR (TREE_OPERAND (t, 2));
19479 ret = build_new (loc, &placement_vec, op1, op2,
19480 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19481 complain);
19482
19483 if (placement_vec != NULL)
19484 release_tree_vector (placement_vec);
19485 if (init_vec != NULL)
19486 release_tree_vector (init_vec);
19487
19488 RETURN (ret);
19489 }
19490
19491 case DELETE_EXPR:
19492 {
19493 tree op0 = RECUR (TREE_OPERAND (t, 0));
19494 tree op1 = RECUR (TREE_OPERAND (t, 1));
19495 RETURN (delete_sanity (input_location, op0, op1,
19496 DELETE_EXPR_USE_VEC (t),
19497 DELETE_EXPR_USE_GLOBAL (t),
19498 complain));
19499 }
19500
19501 case COMPOUND_EXPR:
19502 {
19503 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19504 complain & ~tf_decltype, in_decl,
19505 /*function_p=*/false,
19506 integral_constant_expression_p);
19507 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19508 op0,
19509 RECUR (TREE_OPERAND (t, 1)),
19510 complain|decltype_flag));
19511 }
19512
19513 case CALL_EXPR:
19514 {
19515 tree function;
19516 unsigned int nargs, i;
19517 bool qualified_p;
19518 bool koenig_p;
19519 tree ret;
19520
19521 function = CALL_EXPR_FN (t);
19522 /* Internal function with no arguments. */
19523 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19524 RETURN (t);
19525
19526 /* When we parsed the expression, we determined whether or
19527 not Koenig lookup should be performed. */
19528 koenig_p = KOENIG_LOOKUP_P (t);
19529 if (function == NULL_TREE)
19530 {
19531 koenig_p = false;
19532 qualified_p = false;
19533 }
19534 else if (TREE_CODE (function) == SCOPE_REF)
19535 {
19536 qualified_p = true;
19537 function = tsubst_qualified_id (function, args, complain, in_decl,
19538 /*done=*/false,
19539 /*address_p=*/false);
19540 }
19541 else if (koenig_p && identifier_p (function))
19542 {
19543 /* Do nothing; calling tsubst_copy_and_build on an identifier
19544 would incorrectly perform unqualified lookup again.
19545
19546 Note that we can also have an IDENTIFIER_NODE if the earlier
19547 unqualified lookup found a member function; in that case
19548 koenig_p will be false and we do want to do the lookup
19549 again to find the instantiated member function.
19550
19551 FIXME but doing that causes c++/15272, so we need to stop
19552 using IDENTIFIER_NODE in that situation. */
19553 qualified_p = false;
19554 }
19555 else
19556 {
19557 if (TREE_CODE (function) == COMPONENT_REF)
19558 {
19559 tree op = TREE_OPERAND (function, 1);
19560
19561 qualified_p = (TREE_CODE (op) == SCOPE_REF
19562 || (BASELINK_P (op)
19563 && BASELINK_QUALIFIED_P (op)));
19564 }
19565 else
19566 qualified_p = false;
19567
19568 if (TREE_CODE (function) == ADDR_EXPR
19569 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19570 /* Avoid error about taking the address of a constructor. */
19571 function = TREE_OPERAND (function, 0);
19572
19573 function = tsubst_copy_and_build (function, args, complain,
19574 in_decl,
19575 !qualified_p,
19576 integral_constant_expression_p);
19577
19578 if (BASELINK_P (function))
19579 qualified_p = true;
19580 }
19581
19582 nargs = call_expr_nargs (t);
19583 releasing_vec call_args;
19584 for (i = 0; i < nargs; ++i)
19585 {
19586 tree arg = CALL_EXPR_ARG (t, i);
19587
19588 if (!PACK_EXPANSION_P (arg))
19589 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19590 else
19591 {
19592 /* Expand the pack expansion and push each entry onto
19593 CALL_ARGS. */
19594 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19595 if (TREE_CODE (arg) == TREE_VEC)
19596 {
19597 unsigned int len, j;
19598
19599 len = TREE_VEC_LENGTH (arg);
19600 for (j = 0; j < len; ++j)
19601 {
19602 tree value = TREE_VEC_ELT (arg, j);
19603 if (value != NULL_TREE)
19604 value = convert_from_reference (value);
19605 vec_safe_push (call_args, value);
19606 }
19607 }
19608 else
19609 {
19610 /* A partial substitution. Add one entry. */
19611 vec_safe_push (call_args, arg);
19612 }
19613 }
19614 }
19615
19616 /* Stripped-down processing for a call in a thunk. Specifically, in
19617 the thunk template for a generic lambda. */
19618 if (CALL_FROM_THUNK_P (t))
19619 {
19620 /* Now that we've expanded any packs, the number of call args
19621 might be different. */
19622 unsigned int cargs = call_args->length ();
19623 tree thisarg = NULL_TREE;
19624 if (TREE_CODE (function) == COMPONENT_REF)
19625 {
19626 thisarg = TREE_OPERAND (function, 0);
19627 if (TREE_CODE (thisarg) == INDIRECT_REF)
19628 thisarg = TREE_OPERAND (thisarg, 0);
19629 function = TREE_OPERAND (function, 1);
19630 if (TREE_CODE (function) == BASELINK)
19631 function = BASELINK_FUNCTIONS (function);
19632 }
19633 /* We aren't going to do normal overload resolution, so force the
19634 template-id to resolve. */
19635 function = resolve_nondeduced_context (function, complain);
19636 for (unsigned i = 0; i < cargs; ++i)
19637 {
19638 /* In a thunk, pass through args directly, without any
19639 conversions. */
19640 tree arg = (*call_args)[i];
19641 while (TREE_CODE (arg) != PARM_DECL)
19642 arg = TREE_OPERAND (arg, 0);
19643 (*call_args)[i] = arg;
19644 }
19645 if (thisarg)
19646 {
19647 /* If there are no other args, just push 'this'. */
19648 if (cargs == 0)
19649 vec_safe_push (call_args, thisarg);
19650 else
19651 {
19652 /* Otherwise, shift the other args over to make room. */
19653 tree last = (*call_args)[cargs - 1];
19654 vec_safe_push (call_args, last);
19655 for (int i = cargs - 1; i > 0; --i)
19656 (*call_args)[i] = (*call_args)[i - 1];
19657 (*call_args)[0] = thisarg;
19658 }
19659 }
19660 ret = build_call_a (function, call_args->length (),
19661 call_args->address ());
19662 /* The thunk location is not interesting. */
19663 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19664 CALL_FROM_THUNK_P (ret) = true;
19665 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19666 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19667
19668 RETURN (ret);
19669 }
19670
19671 /* We do not perform argument-dependent lookup if normal
19672 lookup finds a non-function, in accordance with the
19673 expected resolution of DR 218. */
19674 if (koenig_p
19675 && ((is_overloaded_fn (function)
19676 /* If lookup found a member function, the Koenig lookup is
19677 not appropriate, even if an unqualified-name was used
19678 to denote the function. */
19679 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19680 || identifier_p (function))
19681 /* Only do this when substitution turns a dependent call
19682 into a non-dependent call. */
19683 && type_dependent_expression_p_push (t)
19684 && !any_type_dependent_arguments_p (call_args))
19685 function = perform_koenig_lookup (function, call_args, tf_none);
19686
19687 if (function != NULL_TREE
19688 && identifier_p (function)
19689 && !any_type_dependent_arguments_p (call_args))
19690 {
19691 if (koenig_p && (complain & tf_warning_or_error))
19692 {
19693 /* For backwards compatibility and good diagnostics, try
19694 the unqualified lookup again if we aren't in SFINAE
19695 context. */
19696 tree unq = (tsubst_copy_and_build
19697 (function, args, complain, in_decl, true,
19698 integral_constant_expression_p));
19699 if (unq == error_mark_node)
19700 RETURN (error_mark_node);
19701
19702 if (unq != function)
19703 {
19704 /* In a lambda fn, we have to be careful to not
19705 introduce new this captures. Legacy code can't
19706 be using lambdas anyway, so it's ok to be
19707 stricter. */
19708 bool in_lambda = (current_class_type
19709 && LAMBDA_TYPE_P (current_class_type));
19710 char const *const msg
19711 = G_("%qD was not declared in this scope, "
19712 "and no declarations were found by "
19713 "argument-dependent lookup at the point "
19714 "of instantiation");
19715
19716 bool diag = true;
19717 if (in_lambda)
19718 error_at (cp_expr_loc_or_input_loc (t),
19719 msg, function);
19720 else
19721 diag = permerror (cp_expr_loc_or_input_loc (t),
19722 msg, function);
19723 if (diag)
19724 {
19725 tree fn = unq;
19726
19727 if (INDIRECT_REF_P (fn))
19728 fn = TREE_OPERAND (fn, 0);
19729 if (is_overloaded_fn (fn))
19730 fn = get_first_fn (fn);
19731
19732 if (!DECL_P (fn))
19733 /* Can't say anything more. */;
19734 else if (DECL_CLASS_SCOPE_P (fn))
19735 {
19736 location_t loc = cp_expr_loc_or_input_loc (t);
19737 inform (loc,
19738 "declarations in dependent base %qT are "
19739 "not found by unqualified lookup",
19740 DECL_CLASS_CONTEXT (fn));
19741 if (current_class_ptr)
19742 inform (loc,
19743 "use %<this->%D%> instead", function);
19744 else
19745 inform (loc,
19746 "use %<%T::%D%> instead",
19747 current_class_name, function);
19748 }
19749 else
19750 inform (DECL_SOURCE_LOCATION (fn),
19751 "%qD declared here, later in the "
19752 "translation unit", fn);
19753 if (in_lambda)
19754 RETURN (error_mark_node);
19755 }
19756
19757 function = unq;
19758 }
19759 }
19760 if (identifier_p (function))
19761 {
19762 if (complain & tf_error)
19763 unqualified_name_lookup_error (function);
19764 RETURN (error_mark_node);
19765 }
19766 }
19767
19768 /* Remember that there was a reference to this entity. */
19769 if (function != NULL_TREE
19770 && DECL_P (function)
19771 && !mark_used (function, complain) && !(complain & tf_error))
19772 RETURN (error_mark_node);
19773
19774 /* Put back tf_decltype for the actual call. */
19775 complain |= decltype_flag;
19776
19777 if (function == NULL_TREE)
19778 switch (CALL_EXPR_IFN (t))
19779 {
19780 case IFN_LAUNDER:
19781 gcc_assert (nargs == 1);
19782 if (vec_safe_length (call_args) != 1)
19783 {
19784 error_at (cp_expr_loc_or_input_loc (t),
19785 "wrong number of arguments to "
19786 "%<__builtin_launder%>");
19787 ret = error_mark_node;
19788 }
19789 else
19790 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19791 (*call_args)[0], complain);
19792 break;
19793
19794 case IFN_VEC_CONVERT:
19795 gcc_assert (nargs == 1);
19796 if (vec_safe_length (call_args) != 1)
19797 {
19798 error_at (cp_expr_loc_or_input_loc (t),
19799 "wrong number of arguments to "
19800 "%<__builtin_convertvector%>");
19801 ret = error_mark_node;
19802 break;
19803 }
19804 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19805 tsubst (TREE_TYPE (t), args,
19806 complain, in_decl),
19807 complain);
19808 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19809 RETURN (ret);
19810 break;
19811
19812 default:
19813 /* Unsupported internal function with arguments. */
19814 gcc_unreachable ();
19815 }
19816 else if (TREE_CODE (function) == OFFSET_REF
19817 || TREE_CODE (function) == DOTSTAR_EXPR
19818 || TREE_CODE (function) == MEMBER_REF)
19819 ret = build_offset_ref_call_from_tree (function, &call_args,
19820 complain);
19821 else if (TREE_CODE (function) == COMPONENT_REF)
19822 {
19823 tree instance = TREE_OPERAND (function, 0);
19824 tree fn = TREE_OPERAND (function, 1);
19825
19826 if (processing_template_decl
19827 && (type_dependent_expression_p (instance)
19828 || (!BASELINK_P (fn)
19829 && TREE_CODE (fn) != FIELD_DECL)
19830 || type_dependent_expression_p (fn)
19831 || any_type_dependent_arguments_p (call_args)))
19832 ret = build_min_nt_call_vec (function, call_args);
19833 else if (!BASELINK_P (fn))
19834 ret = finish_call_expr (function, &call_args,
19835 /*disallow_virtual=*/false,
19836 /*koenig_p=*/false,
19837 complain);
19838 else
19839 ret = (build_new_method_call
19840 (instance, fn,
19841 &call_args, NULL_TREE,
19842 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19843 /*fn_p=*/NULL,
19844 complain));
19845 }
19846 else if (concept_check_p (function))
19847 {
19848 /* FUNCTION is a template-id referring to a concept definition. */
19849 tree id = unpack_concept_check (function);
19850 tree tmpl = TREE_OPERAND (id, 0);
19851 tree args = TREE_OPERAND (id, 1);
19852
19853 /* Calls to standard and variable concepts should have been
19854 previously diagnosed. */
19855 gcc_assert (function_concept_p (tmpl));
19856
19857 /* Ensure the result is wrapped as a call expression. */
19858 ret = build_concept_check (tmpl, args, tf_warning_or_error);
19859 }
19860 else
19861 ret = finish_call_expr (function, &call_args,
19862 /*disallow_virtual=*/qualified_p,
19863 koenig_p,
19864 complain);
19865
19866 if (ret != error_mark_node)
19867 {
19868 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19869 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19870 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19871 if (op || ord || rev)
19872 {
19873 function = extract_call_expr (ret);
19874 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19875 CALL_EXPR_ORDERED_ARGS (function) = ord;
19876 CALL_EXPR_REVERSE_ARGS (function) = rev;
19877 }
19878 }
19879
19880 RETURN (ret);
19881 }
19882
19883 case COND_EXPR:
19884 {
19885 tree cond = RECUR (TREE_OPERAND (t, 0));
19886 cond = mark_rvalue_use (cond);
19887 tree folded_cond = fold_non_dependent_expr (cond, complain);
19888 tree exp1, exp2;
19889
19890 if (TREE_CODE (folded_cond) == INTEGER_CST)
19891 {
19892 if (integer_zerop (folded_cond))
19893 {
19894 ++c_inhibit_evaluation_warnings;
19895 exp1 = RECUR (TREE_OPERAND (t, 1));
19896 --c_inhibit_evaluation_warnings;
19897 exp2 = RECUR (TREE_OPERAND (t, 2));
19898 }
19899 else
19900 {
19901 exp1 = RECUR (TREE_OPERAND (t, 1));
19902 ++c_inhibit_evaluation_warnings;
19903 exp2 = RECUR (TREE_OPERAND (t, 2));
19904 --c_inhibit_evaluation_warnings;
19905 }
19906 cond = folded_cond;
19907 }
19908 else
19909 {
19910 exp1 = RECUR (TREE_OPERAND (t, 1));
19911 exp2 = RECUR (TREE_OPERAND (t, 2));
19912 }
19913
19914 warning_sentinel s(warn_duplicated_branches);
19915 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19916 cond, exp1, exp2, complain));
19917 }
19918
19919 case PSEUDO_DTOR_EXPR:
19920 {
19921 tree op0 = RECUR (TREE_OPERAND (t, 0));
19922 tree op1 = RECUR (TREE_OPERAND (t, 1));
19923 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19924 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19925 input_location));
19926 }
19927
19928 case TREE_LIST:
19929 {
19930 tree purpose, value, chain;
19931
19932 if (t == void_list_node)
19933 RETURN (t);
19934
19935 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19936 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19937 {
19938 /* We have pack expansions, so expand those and
19939 create a new list out of it. */
19940 tree purposevec = NULL_TREE;
19941 tree valuevec = NULL_TREE;
19942 tree chain;
19943 int i, len = -1;
19944
19945 /* Expand the argument expressions. */
19946 if (TREE_PURPOSE (t))
19947 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19948 complain, in_decl);
19949 if (TREE_VALUE (t))
19950 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19951 complain, in_decl);
19952
19953 /* Build the rest of the list. */
19954 chain = TREE_CHAIN (t);
19955 if (chain && chain != void_type_node)
19956 chain = RECUR (chain);
19957
19958 /* Determine the number of arguments. */
19959 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19960 {
19961 len = TREE_VEC_LENGTH (purposevec);
19962 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19963 }
19964 else if (TREE_CODE (valuevec) == TREE_VEC)
19965 len = TREE_VEC_LENGTH (valuevec);
19966 else
19967 {
19968 /* Since we only performed a partial substitution into
19969 the argument pack, we only RETURN (a single list
19970 node. */
19971 if (purposevec == TREE_PURPOSE (t)
19972 && valuevec == TREE_VALUE (t)
19973 && chain == TREE_CHAIN (t))
19974 RETURN (t);
19975
19976 RETURN (tree_cons (purposevec, valuevec, chain));
19977 }
19978
19979 /* Convert the argument vectors into a TREE_LIST */
19980 i = len;
19981 while (i > 0)
19982 {
19983 /* Grab the Ith values. */
19984 i--;
19985 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19986 : NULL_TREE;
19987 value
19988 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19989 : NULL_TREE;
19990
19991 /* Build the list (backwards). */
19992 chain = tree_cons (purpose, value, chain);
19993 }
19994
19995 RETURN (chain);
19996 }
19997
19998 purpose = TREE_PURPOSE (t);
19999 if (purpose)
20000 purpose = RECUR (purpose);
20001 value = TREE_VALUE (t);
20002 if (value)
20003 value = RECUR (value);
20004 chain = TREE_CHAIN (t);
20005 if (chain && chain != void_type_node)
20006 chain = RECUR (chain);
20007 if (purpose == TREE_PURPOSE (t)
20008 && value == TREE_VALUE (t)
20009 && chain == TREE_CHAIN (t))
20010 RETURN (t);
20011 RETURN (tree_cons (purpose, value, chain));
20012 }
20013
20014 case COMPONENT_REF:
20015 {
20016 tree object;
20017 tree object_type;
20018 tree member;
20019 tree r;
20020
20021 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20022 args, complain, in_decl);
20023 /* Remember that there was a reference to this entity. */
20024 if (DECL_P (object)
20025 && !mark_used (object, complain) && !(complain & tf_error))
20026 RETURN (error_mark_node);
20027 object_type = TREE_TYPE (object);
20028
20029 member = TREE_OPERAND (t, 1);
20030 if (BASELINK_P (member))
20031 member = tsubst_baselink (member,
20032 non_reference (TREE_TYPE (object)),
20033 args, complain, in_decl);
20034 else
20035 member = tsubst_copy (member, args, complain, in_decl);
20036 if (member == error_mark_node)
20037 RETURN (error_mark_node);
20038
20039 if (TREE_CODE (member) == FIELD_DECL)
20040 {
20041 r = finish_non_static_data_member (member, object, NULL_TREE);
20042 if (TREE_CODE (r) == COMPONENT_REF)
20043 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20044 RETURN (r);
20045 }
20046 else if (type_dependent_expression_p (object))
20047 /* We can't do much here. */;
20048 else if (!CLASS_TYPE_P (object_type))
20049 {
20050 if (scalarish_type_p (object_type))
20051 {
20052 tree s = NULL_TREE;
20053 tree dtor = member;
20054
20055 if (TREE_CODE (dtor) == SCOPE_REF)
20056 {
20057 s = TREE_OPERAND (dtor, 0);
20058 dtor = TREE_OPERAND (dtor, 1);
20059 }
20060 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20061 {
20062 dtor = TREE_OPERAND (dtor, 0);
20063 if (TYPE_P (dtor))
20064 RETURN (finish_pseudo_destructor_expr
20065 (object, s, dtor, input_location));
20066 }
20067 }
20068 }
20069 else if (TREE_CODE (member) == SCOPE_REF
20070 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20071 {
20072 /* Lookup the template functions now that we know what the
20073 scope is. */
20074 tree scope = TREE_OPERAND (member, 0);
20075 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20076 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20077 member = lookup_qualified_name (scope, tmpl,
20078 /*is_type_p=*/false,
20079 /*complain=*/false);
20080 if (BASELINK_P (member))
20081 {
20082 BASELINK_FUNCTIONS (member)
20083 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20084 args);
20085 member = (adjust_result_of_qualified_name_lookup
20086 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20087 object_type));
20088 }
20089 else
20090 {
20091 qualified_name_lookup_error (scope, tmpl, member,
20092 input_location);
20093 RETURN (error_mark_node);
20094 }
20095 }
20096 else if (TREE_CODE (member) == SCOPE_REF
20097 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20098 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20099 {
20100 if (complain & tf_error)
20101 {
20102 if (TYPE_P (TREE_OPERAND (member, 0)))
20103 error ("%qT is not a class or namespace",
20104 TREE_OPERAND (member, 0));
20105 else
20106 error ("%qD is not a class or namespace",
20107 TREE_OPERAND (member, 0));
20108 }
20109 RETURN (error_mark_node);
20110 }
20111
20112 r = finish_class_member_access_expr (object, member,
20113 /*template_p=*/false,
20114 complain);
20115 if (TREE_CODE (r) == COMPONENT_REF)
20116 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20117 RETURN (r);
20118 }
20119
20120 case THROW_EXPR:
20121 RETURN (build_throw
20122 (input_location, RECUR (TREE_OPERAND (t, 0))));
20123
20124 case CONSTRUCTOR:
20125 {
20126 vec<constructor_elt, va_gc> *n;
20127 constructor_elt *ce;
20128 unsigned HOST_WIDE_INT idx;
20129 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20130 bool process_index_p;
20131 int newlen;
20132 bool need_copy_p = false;
20133 tree r;
20134
20135 if (type == error_mark_node)
20136 RETURN (error_mark_node);
20137
20138 /* We do not want to process the index of aggregate
20139 initializers as they are identifier nodes which will be
20140 looked up by digest_init. */
20141 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20142
20143 if (null_member_pointer_value_p (t))
20144 {
20145 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20146 RETURN (t);
20147 }
20148
20149 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20150 newlen = vec_safe_length (n);
20151 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20152 {
20153 if (ce->index && process_index_p
20154 /* An identifier index is looked up in the type
20155 being initialized, not the current scope. */
20156 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20157 ce->index = RECUR (ce->index);
20158
20159 if (PACK_EXPANSION_P (ce->value))
20160 {
20161 /* Substitute into the pack expansion. */
20162 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20163 in_decl);
20164
20165 if (ce->value == error_mark_node
20166 || PACK_EXPANSION_P (ce->value))
20167 ;
20168 else if (TREE_VEC_LENGTH (ce->value) == 1)
20169 /* Just move the argument into place. */
20170 ce->value = TREE_VEC_ELT (ce->value, 0);
20171 else
20172 {
20173 /* Update the length of the final CONSTRUCTOR
20174 arguments vector, and note that we will need to
20175 copy.*/
20176 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20177 need_copy_p = true;
20178 }
20179 }
20180 else
20181 ce->value = RECUR (ce->value);
20182 }
20183
20184 if (need_copy_p)
20185 {
20186 vec<constructor_elt, va_gc> *old_n = n;
20187
20188 vec_alloc (n, newlen);
20189 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20190 {
20191 if (TREE_CODE (ce->value) == TREE_VEC)
20192 {
20193 int i, len = TREE_VEC_LENGTH (ce->value);
20194 for (i = 0; i < len; ++i)
20195 CONSTRUCTOR_APPEND_ELT (n, 0,
20196 TREE_VEC_ELT (ce->value, i));
20197 }
20198 else
20199 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20200 }
20201 }
20202
20203 r = build_constructor (init_list_type_node, n);
20204 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20205 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20206 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20207
20208 if (TREE_HAS_CONSTRUCTOR (t))
20209 {
20210 fcl_t cl = fcl_functional;
20211 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20212 cl = fcl_c99;
20213 RETURN (finish_compound_literal (type, r, complain, cl));
20214 }
20215
20216 TREE_TYPE (r) = type;
20217 RETURN (r);
20218 }
20219
20220 case TYPEID_EXPR:
20221 {
20222 tree operand_0 = TREE_OPERAND (t, 0);
20223 if (TYPE_P (operand_0))
20224 {
20225 operand_0 = tsubst (operand_0, args, complain, in_decl);
20226 RETURN (get_typeid (operand_0, complain));
20227 }
20228 else
20229 {
20230 operand_0 = RECUR (operand_0);
20231 RETURN (build_typeid (operand_0, complain));
20232 }
20233 }
20234
20235 case VAR_DECL:
20236 if (!args)
20237 RETURN (t);
20238 /* Fall through */
20239
20240 case PARM_DECL:
20241 {
20242 tree r = tsubst_copy (t, args, complain, in_decl);
20243 /* ??? We're doing a subset of finish_id_expression here. */
20244 if (tree wrap = maybe_get_tls_wrapper_call (r))
20245 /* Replace an evaluated use of the thread_local variable with
20246 a call to its wrapper. */
20247 r = wrap;
20248 else if (outer_automatic_var_p (r))
20249 r = process_outer_var_ref (r, complain);
20250
20251 if (!TYPE_REF_P (TREE_TYPE (t)))
20252 /* If the original type was a reference, we'll be wrapped in
20253 the appropriate INDIRECT_REF. */
20254 r = convert_from_reference (r);
20255 RETURN (r);
20256 }
20257
20258 case VA_ARG_EXPR:
20259 {
20260 tree op0 = RECUR (TREE_OPERAND (t, 0));
20261 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20262 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20263 }
20264
20265 case OFFSETOF_EXPR:
20266 {
20267 tree object_ptr
20268 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20269 in_decl, /*function_p=*/false,
20270 /*integral_constant_expression_p=*/false);
20271 RETURN (finish_offsetof (object_ptr,
20272 RECUR (TREE_OPERAND (t, 0)),
20273 EXPR_LOCATION (t)));
20274 }
20275
20276 case ADDRESSOF_EXPR:
20277 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20278 RECUR (TREE_OPERAND (t, 0)), complain));
20279
20280 case TRAIT_EXPR:
20281 {
20282 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20283 complain, in_decl);
20284 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20285 complain, in_decl);
20286 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20287 TRAIT_EXPR_KIND (t), type1, type2));
20288 }
20289
20290 case STMT_EXPR:
20291 {
20292 tree old_stmt_expr = cur_stmt_expr;
20293 tree stmt_expr = begin_stmt_expr ();
20294
20295 cur_stmt_expr = stmt_expr;
20296 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20297 integral_constant_expression_p);
20298 stmt_expr = finish_stmt_expr (stmt_expr, false);
20299 cur_stmt_expr = old_stmt_expr;
20300
20301 /* If the resulting list of expression statement is empty,
20302 fold it further into void_node. */
20303 if (empty_expr_stmt_p (stmt_expr))
20304 stmt_expr = void_node;
20305
20306 RETURN (stmt_expr);
20307 }
20308
20309 case LAMBDA_EXPR:
20310 {
20311 if (complain & tf_partial)
20312 {
20313 /* We don't have a full set of template arguments yet; don't touch
20314 the lambda at all. */
20315 gcc_assert (processing_template_decl);
20316 return t;
20317 }
20318 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20319
20320 RETURN (build_lambda_object (r));
20321 }
20322
20323 case TARGET_EXPR:
20324 /* We can get here for a constant initializer of non-dependent type.
20325 FIXME stop folding in cp_parser_initializer_clause. */
20326 {
20327 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20328 complain);
20329 RETURN (r);
20330 }
20331
20332 case TRANSACTION_EXPR:
20333 RETURN (tsubst_expr(t, args, complain, in_decl,
20334 integral_constant_expression_p));
20335
20336 case PAREN_EXPR:
20337 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20338
20339 case VEC_PERM_EXPR:
20340 {
20341 tree op0 = RECUR (TREE_OPERAND (t, 0));
20342 tree op1 = RECUR (TREE_OPERAND (t, 1));
20343 tree op2 = RECUR (TREE_OPERAND (t, 2));
20344 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20345 complain));
20346 }
20347
20348 case REQUIRES_EXPR:
20349 {
20350 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20351 RETURN (r);
20352 }
20353
20354 case RANGE_EXPR:
20355 /* No need to substitute further, a RANGE_EXPR will always be built
20356 with constant operands. */
20357 RETURN (t);
20358
20359 case NON_LVALUE_EXPR:
20360 case VIEW_CONVERT_EXPR:
20361 if (location_wrapper_p (t))
20362 /* We need to do this here as well as in tsubst_copy so we get the
20363 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20364 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20365 EXPR_LOCATION (t)));
20366 /* fallthrough. */
20367
20368 default:
20369 /* Handle Objective-C++ constructs, if appropriate. */
20370 {
20371 tree subst
20372 = objcp_tsubst_copy_and_build (t, args, complain,
20373 in_decl, /*function_p=*/false);
20374 if (subst)
20375 RETURN (subst);
20376 }
20377 RETURN (tsubst_copy (t, args, complain, in_decl));
20378 }
20379
20380 #undef RECUR
20381 #undef RETURN
20382 out:
20383 input_location = save_loc;
20384 return retval;
20385 }
20386
20387 /* Verify that the instantiated ARGS are valid. For type arguments,
20388 make sure that the type's linkage is ok. For non-type arguments,
20389 make sure they are constants if they are integral or enumerations.
20390 Emit an error under control of COMPLAIN, and return TRUE on error. */
20391
20392 static bool
20393 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20394 {
20395 if (dependent_template_arg_p (t))
20396 return false;
20397 if (ARGUMENT_PACK_P (t))
20398 {
20399 tree vec = ARGUMENT_PACK_ARGS (t);
20400 int len = TREE_VEC_LENGTH (vec);
20401 bool result = false;
20402 int i;
20403
20404 for (i = 0; i < len; ++i)
20405 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20406 result = true;
20407 return result;
20408 }
20409 else if (TYPE_P (t))
20410 {
20411 /* [basic.link]: A name with no linkage (notably, the name
20412 of a class or enumeration declared in a local scope)
20413 shall not be used to declare an entity with linkage.
20414 This implies that names with no linkage cannot be used as
20415 template arguments
20416
20417 DR 757 relaxes this restriction for C++0x. */
20418 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20419 : no_linkage_check (t, /*relaxed_p=*/false));
20420
20421 if (nt)
20422 {
20423 /* DR 488 makes use of a type with no linkage cause
20424 type deduction to fail. */
20425 if (complain & tf_error)
20426 {
20427 if (TYPE_UNNAMED_P (nt))
20428 error ("%qT is/uses unnamed type", t);
20429 else
20430 error ("template argument for %qD uses local type %qT",
20431 tmpl, t);
20432 }
20433 return true;
20434 }
20435 /* In order to avoid all sorts of complications, we do not
20436 allow variably-modified types as template arguments. */
20437 else if (variably_modified_type_p (t, NULL_TREE))
20438 {
20439 if (complain & tf_error)
20440 error ("%qT is a variably modified type", t);
20441 return true;
20442 }
20443 }
20444 /* Class template and alias template arguments should be OK. */
20445 else if (DECL_TYPE_TEMPLATE_P (t))
20446 ;
20447 /* A non-type argument of integral or enumerated type must be a
20448 constant. */
20449 else if (TREE_TYPE (t)
20450 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20451 && !REFERENCE_REF_P (t)
20452 && !TREE_CONSTANT (t))
20453 {
20454 if (complain & tf_error)
20455 error ("integral expression %qE is not constant", t);
20456 return true;
20457 }
20458 return false;
20459 }
20460
20461 static bool
20462 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20463 {
20464 int ix, len = DECL_NTPARMS (tmpl);
20465 bool result = false;
20466
20467 for (ix = 0; ix != len; ix++)
20468 {
20469 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20470 result = true;
20471 }
20472 if (result && (complain & tf_error))
20473 error (" trying to instantiate %qD", tmpl);
20474 return result;
20475 }
20476
20477 /* We're out of SFINAE context now, so generate diagnostics for the access
20478 errors we saw earlier when instantiating D from TMPL and ARGS. */
20479
20480 static void
20481 recheck_decl_substitution (tree d, tree tmpl, tree args)
20482 {
20483 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20484 tree type = TREE_TYPE (pattern);
20485 location_t loc = input_location;
20486
20487 push_access_scope (d);
20488 push_deferring_access_checks (dk_no_deferred);
20489 input_location = DECL_SOURCE_LOCATION (pattern);
20490 tsubst (type, args, tf_warning_or_error, d);
20491 input_location = loc;
20492 pop_deferring_access_checks ();
20493 pop_access_scope (d);
20494 }
20495
20496 /* Instantiate the indicated variable, function, or alias template TMPL with
20497 the template arguments in TARG_PTR. */
20498
20499 static tree
20500 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20501 {
20502 tree targ_ptr = orig_args;
20503 tree fndecl;
20504 tree gen_tmpl;
20505 tree spec;
20506 bool access_ok = true;
20507
20508 if (tmpl == error_mark_node)
20509 return error_mark_node;
20510
20511 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20512
20513 /* If this function is a clone, handle it specially. */
20514 if (DECL_CLONED_FUNCTION_P (tmpl))
20515 {
20516 tree spec;
20517 tree clone;
20518
20519 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20520 DECL_CLONED_FUNCTION. */
20521 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20522 targ_ptr, complain);
20523 if (spec == error_mark_node)
20524 return error_mark_node;
20525
20526 /* Look for the clone. */
20527 FOR_EACH_CLONE (clone, spec)
20528 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20529 return clone;
20530 /* We should always have found the clone by now. */
20531 gcc_unreachable ();
20532 return NULL_TREE;
20533 }
20534
20535 if (targ_ptr == error_mark_node)
20536 return error_mark_node;
20537
20538 /* Check to see if we already have this specialization. */
20539 gen_tmpl = most_general_template (tmpl);
20540 if (TMPL_ARGS_DEPTH (targ_ptr)
20541 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20542 /* targ_ptr only has the innermost template args, so add the outer ones
20543 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20544 the case of a non-dependent call within a template definition). */
20545 targ_ptr = (add_outermost_template_args
20546 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20547 targ_ptr));
20548
20549 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20550 but it doesn't seem to be on the hot path. */
20551 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20552
20553 gcc_assert (tmpl == gen_tmpl
20554 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20555 == spec)
20556 || fndecl == NULL_TREE);
20557
20558 if (spec != NULL_TREE)
20559 {
20560 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20561 {
20562 if (complain & tf_error)
20563 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20564 return error_mark_node;
20565 }
20566 return spec;
20567 }
20568
20569 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20570 complain))
20571 return error_mark_node;
20572
20573 /* We are building a FUNCTION_DECL, during which the access of its
20574 parameters and return types have to be checked. However this
20575 FUNCTION_DECL which is the desired context for access checking
20576 is not built yet. We solve this chicken-and-egg problem by
20577 deferring all checks until we have the FUNCTION_DECL. */
20578 push_deferring_access_checks (dk_deferred);
20579
20580 /* Instantiation of the function happens in the context of the function
20581 template, not the context of the overload resolution we're doing. */
20582 push_to_top_level ();
20583 /* If there are dependent arguments, e.g. because we're doing partial
20584 ordering, make sure processing_template_decl stays set. */
20585 if (uses_template_parms (targ_ptr))
20586 ++processing_template_decl;
20587 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20588 {
20589 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20590 complain, gen_tmpl, true);
20591 push_nested_class (ctx);
20592 }
20593
20594 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20595
20596 fndecl = NULL_TREE;
20597 if (VAR_P (pattern))
20598 {
20599 /* We need to determine if we're using a partial or explicit
20600 specialization now, because the type of the variable could be
20601 different. */
20602 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20603 tree elt = most_specialized_partial_spec (tid, complain);
20604 if (elt == error_mark_node)
20605 pattern = error_mark_node;
20606 else if (elt)
20607 {
20608 tree partial_tmpl = TREE_VALUE (elt);
20609 tree partial_args = TREE_PURPOSE (elt);
20610 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20611 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20612 }
20613 }
20614
20615 /* Substitute template parameters to obtain the specialization. */
20616 if (fndecl == NULL_TREE)
20617 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20618 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20619 pop_nested_class ();
20620 pop_from_top_level ();
20621
20622 if (fndecl == error_mark_node)
20623 {
20624 pop_deferring_access_checks ();
20625 return error_mark_node;
20626 }
20627
20628 /* The DECL_TI_TEMPLATE should always be the immediate parent
20629 template, not the most general template. */
20630 DECL_TI_TEMPLATE (fndecl) = tmpl;
20631 DECL_TI_ARGS (fndecl) = targ_ptr;
20632
20633 /* Now we know the specialization, compute access previously
20634 deferred. Do no access control for inheriting constructors,
20635 as we already checked access for the inherited constructor. */
20636 if (!(flag_new_inheriting_ctors
20637 && DECL_INHERITED_CTOR (fndecl)))
20638 {
20639 push_access_scope (fndecl);
20640 if (!perform_deferred_access_checks (complain))
20641 access_ok = false;
20642 pop_access_scope (fndecl);
20643 }
20644 pop_deferring_access_checks ();
20645
20646 /* If we've just instantiated the main entry point for a function,
20647 instantiate all the alternate entry points as well. We do this
20648 by cloning the instantiation of the main entry point, not by
20649 instantiating the template clones. */
20650 if (tree chain = DECL_CHAIN (gen_tmpl))
20651 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20652 clone_function_decl (fndecl, /*update_methods=*/false);
20653
20654 if (!access_ok)
20655 {
20656 if (!(complain & tf_error))
20657 {
20658 /* Remember to reinstantiate when we're out of SFINAE so the user
20659 can see the errors. */
20660 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20661 }
20662 return error_mark_node;
20663 }
20664 return fndecl;
20665 }
20666
20667 /* Wrapper for instantiate_template_1. */
20668
20669 tree
20670 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20671 {
20672 tree ret;
20673 timevar_push (TV_TEMPLATE_INST);
20674 ret = instantiate_template_1 (tmpl, orig_args, complain);
20675 timevar_pop (TV_TEMPLATE_INST);
20676 return ret;
20677 }
20678
20679 /* Instantiate the alias template TMPL with ARGS. Also push a template
20680 instantiation level, which instantiate_template doesn't do because
20681 functions and variables have sufficient context established by the
20682 callers. */
20683
20684 static tree
20685 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20686 {
20687 if (tmpl == error_mark_node || args == error_mark_node)
20688 return error_mark_node;
20689
20690 args =
20691 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20692 args, tmpl, complain,
20693 /*require_all_args=*/true,
20694 /*use_default_args=*/true);
20695
20696 /* FIXME check for satisfaction in check_instantiated_args. */
20697 if (flag_concepts
20698 && !any_dependent_template_arguments_p (args)
20699 && !constraints_satisfied_p (tmpl, args))
20700 {
20701 if (complain & tf_error)
20702 {
20703 auto_diagnostic_group d;
20704 error ("template constraint failure for %qD", tmpl);
20705 diagnose_constraints (input_location, tmpl, args);
20706 }
20707 return error_mark_node;
20708 }
20709
20710 if (!push_tinst_level (tmpl, args))
20711 return error_mark_node;
20712 tree r = instantiate_template (tmpl, args, complain);
20713 pop_tinst_level ();
20714
20715 return r;
20716 }
20717
20718 /* PARM is a template parameter pack for FN. Returns true iff
20719 PARM is used in a deducible way in the argument list of FN. */
20720
20721 static bool
20722 pack_deducible_p (tree parm, tree fn)
20723 {
20724 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20725 for (; t; t = TREE_CHAIN (t))
20726 {
20727 tree type = TREE_VALUE (t);
20728 tree packs;
20729 if (!PACK_EXPANSION_P (type))
20730 continue;
20731 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20732 packs; packs = TREE_CHAIN (packs))
20733 if (template_args_equal (TREE_VALUE (packs), parm))
20734 {
20735 /* The template parameter pack is used in a function parameter
20736 pack. If this is the end of the parameter list, the
20737 template parameter pack is deducible. */
20738 if (TREE_CHAIN (t) == void_list_node)
20739 return true;
20740 else
20741 /* Otherwise, not. Well, it could be deduced from
20742 a non-pack parameter, but doing so would end up with
20743 a deduction mismatch, so don't bother. */
20744 return false;
20745 }
20746 }
20747 /* The template parameter pack isn't used in any function parameter
20748 packs, but it might be used deeper, e.g. tuple<Args...>. */
20749 return true;
20750 }
20751
20752 /* Subroutine of fn_type_unification: check non-dependent parms for
20753 convertibility. */
20754
20755 static int
20756 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20757 tree fn, unification_kind_t strict, int flags,
20758 struct conversion **convs, bool explain_p)
20759 {
20760 /* Non-constructor methods need to leave a conversion for 'this', which
20761 isn't included in nargs here. */
20762 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20763 && !DECL_CONSTRUCTOR_P (fn));
20764
20765 for (unsigned ia = 0;
20766 parms && parms != void_list_node && ia < nargs; )
20767 {
20768 tree parm = TREE_VALUE (parms);
20769
20770 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20771 && (!TREE_CHAIN (parms)
20772 || TREE_CHAIN (parms) == void_list_node))
20773 /* For a function parameter pack that occurs at the end of the
20774 parameter-declaration-list, the type A of each remaining
20775 argument of the call is compared with the type P of the
20776 declarator-id of the function parameter pack. */
20777 break;
20778
20779 parms = TREE_CHAIN (parms);
20780
20781 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20782 /* For a function parameter pack that does not occur at the
20783 end of the parameter-declaration-list, the type of the
20784 parameter pack is a non-deduced context. */
20785 continue;
20786
20787 if (!uses_template_parms (parm))
20788 {
20789 tree arg = args[ia];
20790 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20791 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20792
20793 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20794 conv_p, explain_p))
20795 return 1;
20796 }
20797
20798 ++ia;
20799 }
20800
20801 return 0;
20802 }
20803
20804 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20805 NARGS elements of the arguments that are being used when calling
20806 it. TARGS is a vector into which the deduced template arguments
20807 are placed.
20808
20809 Returns either a FUNCTION_DECL for the matching specialization of FN or
20810 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20811 true, diagnostics will be printed to explain why it failed.
20812
20813 If FN is a conversion operator, or we are trying to produce a specific
20814 specialization, RETURN_TYPE is the return type desired.
20815
20816 The EXPLICIT_TARGS are explicit template arguments provided via a
20817 template-id.
20818
20819 The parameter STRICT is one of:
20820
20821 DEDUCE_CALL:
20822 We are deducing arguments for a function call, as in
20823 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20824 deducing arguments for a call to the result of a conversion
20825 function template, as in [over.call.object].
20826
20827 DEDUCE_CONV:
20828 We are deducing arguments for a conversion function, as in
20829 [temp.deduct.conv].
20830
20831 DEDUCE_EXACT:
20832 We are deducing arguments when doing an explicit instantiation
20833 as in [temp.explicit], when determining an explicit specialization
20834 as in [temp.expl.spec], or when taking the address of a function
20835 template, as in [temp.deduct.funcaddr]. */
20836
20837 tree
20838 fn_type_unification (tree fn,
20839 tree explicit_targs,
20840 tree targs,
20841 const tree *args,
20842 unsigned int nargs,
20843 tree return_type,
20844 unification_kind_t strict,
20845 int flags,
20846 struct conversion **convs,
20847 bool explain_p,
20848 bool decltype_p)
20849 {
20850 tree parms;
20851 tree fntype;
20852 tree decl = NULL_TREE;
20853 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20854 bool ok;
20855 static int deduction_depth;
20856 /* type_unification_real will pass back any access checks from default
20857 template argument substitution. */
20858 vec<deferred_access_check, va_gc> *checks = NULL;
20859 /* We don't have all the template args yet. */
20860 bool incomplete = true;
20861
20862 tree orig_fn = fn;
20863 if (flag_new_inheriting_ctors)
20864 fn = strip_inheriting_ctors (fn);
20865
20866 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20867 tree r = error_mark_node;
20868
20869 tree full_targs = targs;
20870 if (TMPL_ARGS_DEPTH (targs)
20871 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20872 full_targs = (add_outermost_template_args
20873 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20874 targs));
20875
20876 if (decltype_p)
20877 complain |= tf_decltype;
20878
20879 /* In C++0x, it's possible to have a function template whose type depends
20880 on itself recursively. This is most obvious with decltype, but can also
20881 occur with enumeration scope (c++/48969). So we need to catch infinite
20882 recursion and reject the substitution at deduction time; this function
20883 will return error_mark_node for any repeated substitution.
20884
20885 This also catches excessive recursion such as when f<N> depends on
20886 f<N-1> across all integers, and returns error_mark_node for all the
20887 substitutions back up to the initial one.
20888
20889 This is, of course, not reentrant. */
20890 if (excessive_deduction_depth)
20891 return error_mark_node;
20892 ++deduction_depth;
20893
20894 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20895
20896 fntype = TREE_TYPE (fn);
20897 if (explicit_targs)
20898 {
20899 /* [temp.deduct]
20900
20901 The specified template arguments must match the template
20902 parameters in kind (i.e., type, nontype, template), and there
20903 must not be more arguments than there are parameters;
20904 otherwise type deduction fails.
20905
20906 Nontype arguments must match the types of the corresponding
20907 nontype template parameters, or must be convertible to the
20908 types of the corresponding nontype parameters as specified in
20909 _temp.arg.nontype_, otherwise type deduction fails.
20910
20911 All references in the function type of the function template
20912 to the corresponding template parameters are replaced by the
20913 specified template argument values. If a substitution in a
20914 template parameter or in the function type of the function
20915 template results in an invalid type, type deduction fails. */
20916 int i, len = TREE_VEC_LENGTH (tparms);
20917 location_t loc = input_location;
20918 incomplete = false;
20919
20920 if (explicit_targs == error_mark_node)
20921 goto fail;
20922
20923 if (TMPL_ARGS_DEPTH (explicit_targs)
20924 < TMPL_ARGS_DEPTH (full_targs))
20925 explicit_targs = add_outermost_template_args (full_targs,
20926 explicit_targs);
20927
20928 /* Adjust any explicit template arguments before entering the
20929 substitution context. */
20930 explicit_targs
20931 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20932 complain|tf_partial,
20933 /*require_all_args=*/false,
20934 /*use_default_args=*/false));
20935 if (explicit_targs == error_mark_node)
20936 goto fail;
20937
20938 /* Substitute the explicit args into the function type. This is
20939 necessary so that, for instance, explicitly declared function
20940 arguments can match null pointed constants. If we were given
20941 an incomplete set of explicit args, we must not do semantic
20942 processing during substitution as we could create partial
20943 instantiations. */
20944 for (i = 0; i < len; i++)
20945 {
20946 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20947 bool parameter_pack = false;
20948 tree targ = TREE_VEC_ELT (explicit_targs, i);
20949
20950 /* Dig out the actual parm. */
20951 if (TREE_CODE (parm) == TYPE_DECL
20952 || TREE_CODE (parm) == TEMPLATE_DECL)
20953 {
20954 parm = TREE_TYPE (parm);
20955 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20956 }
20957 else if (TREE_CODE (parm) == PARM_DECL)
20958 {
20959 parm = DECL_INITIAL (parm);
20960 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20961 }
20962
20963 if (targ == NULL_TREE)
20964 /* No explicit argument for this template parameter. */
20965 incomplete = true;
20966 else if (parameter_pack && pack_deducible_p (parm, fn))
20967 {
20968 /* Mark the argument pack as "incomplete". We could
20969 still deduce more arguments during unification.
20970 We remove this mark in type_unification_real. */
20971 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20972 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20973 = ARGUMENT_PACK_ARGS (targ);
20974
20975 /* We have some incomplete argument packs. */
20976 incomplete = true;
20977 }
20978 }
20979
20980 if (incomplete)
20981 {
20982 if (!push_tinst_level (fn, explicit_targs))
20983 {
20984 excessive_deduction_depth = true;
20985 goto fail;
20986 }
20987 ++processing_template_decl;
20988 input_location = DECL_SOURCE_LOCATION (fn);
20989 /* Ignore any access checks; we'll see them again in
20990 instantiate_template and they might have the wrong
20991 access path at this point. */
20992 push_deferring_access_checks (dk_deferred);
20993 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20994 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20995 pop_deferring_access_checks ();
20996 input_location = loc;
20997 --processing_template_decl;
20998 pop_tinst_level ();
20999
21000 if (fntype == error_mark_node)
21001 goto fail;
21002 }
21003
21004 /* Place the explicitly specified arguments in TARGS. */
21005 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21006 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21007 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21008 if (!incomplete && CHECKING_P
21009 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21010 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21011 (targs, NUM_TMPL_ARGS (explicit_targs));
21012 }
21013
21014 if (return_type && strict != DEDUCE_CALL)
21015 {
21016 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21017 new_args[0] = return_type;
21018 memcpy (new_args + 1, args, nargs * sizeof (tree));
21019 args = new_args;
21020 ++nargs;
21021 }
21022
21023 if (!incomplete)
21024 goto deduced;
21025
21026 /* Never do unification on the 'this' parameter. */
21027 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21028
21029 if (return_type && strict == DEDUCE_CALL)
21030 {
21031 /* We're deducing for a call to the result of a template conversion
21032 function. The parms we really want are in return_type. */
21033 if (INDIRECT_TYPE_P (return_type))
21034 return_type = TREE_TYPE (return_type);
21035 parms = TYPE_ARG_TYPES (return_type);
21036 }
21037 else if (return_type)
21038 {
21039 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21040 }
21041
21042 /* We allow incomplete unification without an error message here
21043 because the standard doesn't seem to explicitly prohibit it. Our
21044 callers must be ready to deal with unification failures in any
21045 event. */
21046
21047 /* If we aren't explaining yet, push tinst context so we can see where
21048 any errors (e.g. from class instantiations triggered by instantiation
21049 of default template arguments) come from. If we are explaining, this
21050 context is redundant. */
21051 if (!explain_p && !push_tinst_level (fn, targs))
21052 {
21053 excessive_deduction_depth = true;
21054 goto fail;
21055 }
21056
21057 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21058 full_targs, parms, args, nargs, /*subr=*/0,
21059 strict, &checks, explain_p);
21060 if (!explain_p)
21061 pop_tinst_level ();
21062 if (!ok)
21063 goto fail;
21064
21065 /* Now that we have bindings for all of the template arguments,
21066 ensure that the arguments deduced for the template template
21067 parameters have compatible template parameter lists. We cannot
21068 check this property before we have deduced all template
21069 arguments, because the template parameter types of a template
21070 template parameter might depend on prior template parameters
21071 deduced after the template template parameter. The following
21072 ill-formed example illustrates this issue:
21073
21074 template<typename T, template<T> class C> void f(C<5>, T);
21075
21076 template<int N> struct X {};
21077
21078 void g() {
21079 f(X<5>(), 5l); // error: template argument deduction fails
21080 }
21081
21082 The template parameter list of 'C' depends on the template type
21083 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21084 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21085 time that we deduce 'C'. */
21086 if (!template_template_parm_bindings_ok_p
21087 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21088 {
21089 unify_inconsistent_template_template_parameters (explain_p);
21090 goto fail;
21091 }
21092
21093 /* DR 1391: All parameters have args, now check non-dependent parms for
21094 convertibility. */
21095 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21096 convs, explain_p))
21097 goto fail;
21098
21099 deduced:
21100 /* All is well so far. Now, check:
21101
21102 [temp.deduct]
21103
21104 When all template arguments have been deduced, all uses of
21105 template parameters in nondeduced contexts are replaced with
21106 the corresponding deduced argument values. If the
21107 substitution results in an invalid type, as described above,
21108 type deduction fails. */
21109 if (!push_tinst_level (fn, targs))
21110 {
21111 excessive_deduction_depth = true;
21112 goto fail;
21113 }
21114
21115 /* Also collect access checks from the instantiation. */
21116 reopen_deferring_access_checks (checks);
21117
21118 decl = instantiate_template (fn, targs, complain);
21119
21120 checks = get_deferred_access_checks ();
21121 pop_deferring_access_checks ();
21122
21123 pop_tinst_level ();
21124
21125 if (decl == error_mark_node)
21126 goto fail;
21127
21128 /* Now perform any access checks encountered during substitution. */
21129 push_access_scope (decl);
21130 ok = perform_access_checks (checks, complain);
21131 pop_access_scope (decl);
21132 if (!ok)
21133 goto fail;
21134
21135 /* If we're looking for an exact match, check that what we got
21136 is indeed an exact match. It might not be if some template
21137 parameters are used in non-deduced contexts. But don't check
21138 for an exact match if we have dependent template arguments;
21139 in that case we're doing partial ordering, and we already know
21140 that we have two candidates that will provide the actual type. */
21141 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21142 {
21143 tree substed = TREE_TYPE (decl);
21144 unsigned int i;
21145
21146 tree sarg
21147 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21148 if (return_type)
21149 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21150 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21151 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21152 {
21153 unify_type_mismatch (explain_p, args[i],
21154 TREE_VALUE (sarg));
21155 goto fail;
21156 }
21157 }
21158
21159 /* After doing deduction with the inherited constructor, actually return an
21160 instantiation of the inheriting constructor. */
21161 if (orig_fn != fn)
21162 decl = instantiate_template (orig_fn, targs, complain);
21163
21164 r = decl;
21165
21166 fail:
21167 --deduction_depth;
21168 if (excessive_deduction_depth)
21169 {
21170 if (deduction_depth == 0)
21171 /* Reset once we're all the way out. */
21172 excessive_deduction_depth = false;
21173 }
21174
21175 return r;
21176 }
21177
21178 /* Adjust types before performing type deduction, as described in
21179 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21180 sections are symmetric. PARM is the type of a function parameter
21181 or the return type of the conversion function. ARG is the type of
21182 the argument passed to the call, or the type of the value
21183 initialized with the result of the conversion function.
21184 ARG_EXPR is the original argument expression, which may be null. */
21185
21186 static int
21187 maybe_adjust_types_for_deduction (unification_kind_t strict,
21188 tree* parm,
21189 tree* arg,
21190 tree arg_expr)
21191 {
21192 int result = 0;
21193
21194 switch (strict)
21195 {
21196 case DEDUCE_CALL:
21197 break;
21198
21199 case DEDUCE_CONV:
21200 /* Swap PARM and ARG throughout the remainder of this
21201 function; the handling is precisely symmetric since PARM
21202 will initialize ARG rather than vice versa. */
21203 std::swap (parm, arg);
21204 break;
21205
21206 case DEDUCE_EXACT:
21207 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21208 too, but here handle it by stripping the reference from PARM
21209 rather than by adding it to ARG. */
21210 if (TYPE_REF_P (*parm)
21211 && TYPE_REF_IS_RVALUE (*parm)
21212 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21213 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21214 && TYPE_REF_P (*arg)
21215 && !TYPE_REF_IS_RVALUE (*arg))
21216 *parm = TREE_TYPE (*parm);
21217 /* Nothing else to do in this case. */
21218 return 0;
21219
21220 default:
21221 gcc_unreachable ();
21222 }
21223
21224 if (!TYPE_REF_P (*parm))
21225 {
21226 /* [temp.deduct.call]
21227
21228 If P is not a reference type:
21229
21230 --If A is an array type, the pointer type produced by the
21231 array-to-pointer standard conversion (_conv.array_) is
21232 used in place of A for type deduction; otherwise,
21233
21234 --If A is a function type, the pointer type produced by
21235 the function-to-pointer standard conversion
21236 (_conv.func_) is used in place of A for type deduction;
21237 otherwise,
21238
21239 --If A is a cv-qualified type, the top level
21240 cv-qualifiers of A's type are ignored for type
21241 deduction. */
21242 if (TREE_CODE (*arg) == ARRAY_TYPE)
21243 *arg = build_pointer_type (TREE_TYPE (*arg));
21244 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21245 *arg = build_pointer_type (*arg);
21246 else
21247 *arg = TYPE_MAIN_VARIANT (*arg);
21248 }
21249
21250 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21251 reference to a cv-unqualified template parameter that does not represent a
21252 template parameter of a class template (during class template argument
21253 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21254 an lvalue, the type "lvalue reference to A" is used in place of A for type
21255 deduction. */
21256 if (TYPE_REF_P (*parm)
21257 && TYPE_REF_IS_RVALUE (*parm)
21258 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21259 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21260 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21261 && (arg_expr ? lvalue_p (arg_expr)
21262 /* try_one_overload doesn't provide an arg_expr, but
21263 functions are always lvalues. */
21264 : TREE_CODE (*arg) == FUNCTION_TYPE))
21265 *arg = build_reference_type (*arg);
21266
21267 /* [temp.deduct.call]
21268
21269 If P is a cv-qualified type, the top level cv-qualifiers
21270 of P's type are ignored for type deduction. If P is a
21271 reference type, the type referred to by P is used for
21272 type deduction. */
21273 *parm = TYPE_MAIN_VARIANT (*parm);
21274 if (TYPE_REF_P (*parm))
21275 {
21276 *parm = TREE_TYPE (*parm);
21277 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21278 }
21279
21280 /* DR 322. For conversion deduction, remove a reference type on parm
21281 too (which has been swapped into ARG). */
21282 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21283 *arg = TREE_TYPE (*arg);
21284
21285 return result;
21286 }
21287
21288 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21289 template which doesn't contain any deducible template parameters; check if
21290 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21291 unify_one_argument. */
21292
21293 static int
21294 check_non_deducible_conversion (tree parm, tree arg, int strict,
21295 int flags, struct conversion **conv_p,
21296 bool explain_p)
21297 {
21298 tree type;
21299
21300 if (!TYPE_P (arg))
21301 type = TREE_TYPE (arg);
21302 else
21303 type = arg;
21304
21305 if (same_type_p (parm, type))
21306 return unify_success (explain_p);
21307
21308 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21309 if (strict == DEDUCE_CONV)
21310 {
21311 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21312 return unify_success (explain_p);
21313 }
21314 else if (strict != DEDUCE_EXACT)
21315 {
21316 bool ok = false;
21317 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21318 if (conv_p)
21319 /* Avoid recalculating this in add_function_candidate. */
21320 ok = (*conv_p
21321 = good_conversion (parm, type, conv_arg, flags, complain));
21322 else
21323 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21324 if (ok)
21325 return unify_success (explain_p);
21326 }
21327
21328 if (strict == DEDUCE_EXACT)
21329 return unify_type_mismatch (explain_p, parm, arg);
21330 else
21331 return unify_arg_conversion (explain_p, parm, type, arg);
21332 }
21333
21334 static bool uses_deducible_template_parms (tree type);
21335
21336 /* Returns true iff the expression EXPR is one from which a template
21337 argument can be deduced. In other words, if it's an undecorated
21338 use of a template non-type parameter. */
21339
21340 static bool
21341 deducible_expression (tree expr)
21342 {
21343 /* Strip implicit conversions. */
21344 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21345 expr = TREE_OPERAND (expr, 0);
21346 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21347 }
21348
21349 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21350 deducible way; that is, if it has a max value of <PARM> - 1. */
21351
21352 static bool
21353 deducible_array_bound (tree domain)
21354 {
21355 if (domain == NULL_TREE)
21356 return false;
21357
21358 tree max = TYPE_MAX_VALUE (domain);
21359 if (TREE_CODE (max) != MINUS_EXPR)
21360 return false;
21361
21362 return deducible_expression (TREE_OPERAND (max, 0));
21363 }
21364
21365 /* Returns true iff the template arguments ARGS use a template parameter
21366 in a deducible way. */
21367
21368 static bool
21369 deducible_template_args (tree args)
21370 {
21371 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21372 {
21373 bool deducible;
21374 tree elt = TREE_VEC_ELT (args, i);
21375 if (ARGUMENT_PACK_P (elt))
21376 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21377 else
21378 {
21379 if (PACK_EXPANSION_P (elt))
21380 elt = PACK_EXPANSION_PATTERN (elt);
21381 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21382 deducible = true;
21383 else if (TYPE_P (elt))
21384 deducible = uses_deducible_template_parms (elt);
21385 else
21386 deducible = deducible_expression (elt);
21387 }
21388 if (deducible)
21389 return true;
21390 }
21391 return false;
21392 }
21393
21394 /* Returns true iff TYPE contains any deducible references to template
21395 parameters, as per 14.8.2.5. */
21396
21397 static bool
21398 uses_deducible_template_parms (tree type)
21399 {
21400 if (PACK_EXPANSION_P (type))
21401 type = PACK_EXPANSION_PATTERN (type);
21402
21403 /* T
21404 cv-list T
21405 TT<T>
21406 TT<i>
21407 TT<> */
21408 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21409 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21410 return true;
21411
21412 /* T*
21413 T&
21414 T&& */
21415 if (INDIRECT_TYPE_P (type))
21416 return uses_deducible_template_parms (TREE_TYPE (type));
21417
21418 /* T[integer-constant ]
21419 type [i] */
21420 if (TREE_CODE (type) == ARRAY_TYPE)
21421 return (uses_deducible_template_parms (TREE_TYPE (type))
21422 || deducible_array_bound (TYPE_DOMAIN (type)));
21423
21424 /* T type ::*
21425 type T::*
21426 T T::*
21427 T (type ::*)()
21428 type (T::*)()
21429 type (type ::*)(T)
21430 type (T::*)(T)
21431 T (type ::*)(T)
21432 T (T::*)()
21433 T (T::*)(T) */
21434 if (TYPE_PTRMEM_P (type))
21435 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21436 || (uses_deducible_template_parms
21437 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21438
21439 /* template-name <T> (where template-name refers to a class template)
21440 template-name <i> (where template-name refers to a class template) */
21441 if (CLASS_TYPE_P (type)
21442 && CLASSTYPE_TEMPLATE_INFO (type)
21443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21444 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21445 (CLASSTYPE_TI_ARGS (type)));
21446
21447 /* type (T)
21448 T()
21449 T(T) */
21450 if (FUNC_OR_METHOD_TYPE_P (type))
21451 {
21452 if (uses_deducible_template_parms (TREE_TYPE (type)))
21453 return true;
21454 tree parm = TYPE_ARG_TYPES (type);
21455 if (TREE_CODE (type) == METHOD_TYPE)
21456 parm = TREE_CHAIN (parm);
21457 for (; parm; parm = TREE_CHAIN (parm))
21458 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21459 return true;
21460 }
21461
21462 return false;
21463 }
21464
21465 /* Subroutine of type_unification_real and unify_pack_expansion to
21466 handle unification of a single P/A pair. Parameters are as
21467 for those functions. */
21468
21469 static int
21470 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21471 int subr, unification_kind_t strict,
21472 bool explain_p)
21473 {
21474 tree arg_expr = NULL_TREE;
21475 int arg_strict;
21476
21477 if (arg == error_mark_node || parm == error_mark_node)
21478 return unify_invalid (explain_p);
21479 if (arg == unknown_type_node)
21480 /* We can't deduce anything from this, but we might get all the
21481 template args from other function args. */
21482 return unify_success (explain_p);
21483
21484 /* Implicit conversions (Clause 4) will be performed on a function
21485 argument to convert it to the type of the corresponding function
21486 parameter if the parameter type contains no template-parameters that
21487 participate in template argument deduction. */
21488 if (strict != DEDUCE_EXACT
21489 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21490 /* For function parameters with no deducible template parameters,
21491 just return. We'll check non-dependent conversions later. */
21492 return unify_success (explain_p);
21493
21494 switch (strict)
21495 {
21496 case DEDUCE_CALL:
21497 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21498 | UNIFY_ALLOW_MORE_CV_QUAL
21499 | UNIFY_ALLOW_DERIVED);
21500 break;
21501
21502 case DEDUCE_CONV:
21503 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21504 break;
21505
21506 case DEDUCE_EXACT:
21507 arg_strict = UNIFY_ALLOW_NONE;
21508 break;
21509
21510 default:
21511 gcc_unreachable ();
21512 }
21513
21514 /* We only do these transformations if this is the top-level
21515 parameter_type_list in a call or declaration matching; in other
21516 situations (nested function declarators, template argument lists) we
21517 won't be comparing a type to an expression, and we don't do any type
21518 adjustments. */
21519 if (!subr)
21520 {
21521 if (!TYPE_P (arg))
21522 {
21523 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21524 if (type_unknown_p (arg))
21525 {
21526 /* [temp.deduct.type] A template-argument can be
21527 deduced from a pointer to function or pointer
21528 to member function argument if the set of
21529 overloaded functions does not contain function
21530 templates and at most one of a set of
21531 overloaded functions provides a unique
21532 match. */
21533 resolve_overloaded_unification (tparms, targs, parm,
21534 arg, strict,
21535 arg_strict, explain_p);
21536 /* If a unique match was not found, this is a
21537 non-deduced context, so we still succeed. */
21538 return unify_success (explain_p);
21539 }
21540
21541 arg_expr = arg;
21542 arg = unlowered_expr_type (arg);
21543 if (arg == error_mark_node)
21544 return unify_invalid (explain_p);
21545 }
21546
21547 arg_strict |=
21548 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21549 }
21550 else
21551 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21552 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21553 return unify_template_argument_mismatch (explain_p, parm, arg);
21554
21555 /* For deduction from an init-list we need the actual list. */
21556 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21557 arg = arg_expr;
21558 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21559 }
21560
21561 /* for_each_template_parm callback that always returns 0. */
21562
21563 static int
21564 zero_r (tree, void *)
21565 {
21566 return 0;
21567 }
21568
21569 /* for_each_template_parm any_fn callback to handle deduction of a template
21570 type argument from the type of an array bound. */
21571
21572 static int
21573 array_deduction_r (tree t, void *data)
21574 {
21575 tree_pair_p d = (tree_pair_p)data;
21576 tree &tparms = d->purpose;
21577 tree &targs = d->value;
21578
21579 if (TREE_CODE (t) == ARRAY_TYPE)
21580 if (tree dom = TYPE_DOMAIN (t))
21581 if (tree max = TYPE_MAX_VALUE (dom))
21582 {
21583 if (TREE_CODE (max) == MINUS_EXPR)
21584 max = TREE_OPERAND (max, 0);
21585 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21586 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21587 UNIFY_ALLOW_NONE, /*explain*/false);
21588 }
21589
21590 /* Keep walking. */
21591 return 0;
21592 }
21593
21594 /* Try to deduce any not-yet-deduced template type arguments from the type of
21595 an array bound. This is handled separately from unify because 14.8.2.5 says
21596 "The type of a type parameter is only deduced from an array bound if it is
21597 not otherwise deduced." */
21598
21599 static void
21600 try_array_deduction (tree tparms, tree targs, tree parm)
21601 {
21602 tree_pair_s data = { tparms, targs };
21603 hash_set<tree> visited;
21604 for_each_template_parm (parm, zero_r, &data, &visited,
21605 /*nondeduced*/false, array_deduction_r);
21606 }
21607
21608 /* Most parms like fn_type_unification.
21609
21610 If SUBR is 1, we're being called recursively (to unify the
21611 arguments of a function or method parameter of a function
21612 template).
21613
21614 CHECKS is a pointer to a vector of access checks encountered while
21615 substituting default template arguments. */
21616
21617 static int
21618 type_unification_real (tree tparms,
21619 tree full_targs,
21620 tree xparms,
21621 const tree *xargs,
21622 unsigned int xnargs,
21623 int subr,
21624 unification_kind_t strict,
21625 vec<deferred_access_check, va_gc> **checks,
21626 bool explain_p)
21627 {
21628 tree parm, arg;
21629 int i;
21630 int ntparms = TREE_VEC_LENGTH (tparms);
21631 int saw_undeduced = 0;
21632 tree parms;
21633 const tree *args;
21634 unsigned int nargs;
21635 unsigned int ia;
21636
21637 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21638 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21639 gcc_assert (ntparms > 0);
21640
21641 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21642
21643 /* Reset the number of non-defaulted template arguments contained
21644 in TARGS. */
21645 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21646
21647 again:
21648 parms = xparms;
21649 args = xargs;
21650 nargs = xnargs;
21651
21652 ia = 0;
21653 while (parms && parms != void_list_node
21654 && ia < nargs)
21655 {
21656 parm = TREE_VALUE (parms);
21657
21658 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21659 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21660 /* For a function parameter pack that occurs at the end of the
21661 parameter-declaration-list, the type A of each remaining
21662 argument of the call is compared with the type P of the
21663 declarator-id of the function parameter pack. */
21664 break;
21665
21666 parms = TREE_CHAIN (parms);
21667
21668 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21669 /* For a function parameter pack that does not occur at the
21670 end of the parameter-declaration-list, the type of the
21671 parameter pack is a non-deduced context. */
21672 continue;
21673
21674 arg = args[ia];
21675 ++ia;
21676
21677 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21678 explain_p))
21679 return 1;
21680 }
21681
21682 if (parms
21683 && parms != void_list_node
21684 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21685 {
21686 /* Unify the remaining arguments with the pack expansion type. */
21687 tree argvec;
21688 tree parmvec = make_tree_vec (1);
21689
21690 /* Allocate a TREE_VEC and copy in all of the arguments */
21691 argvec = make_tree_vec (nargs - ia);
21692 for (i = 0; ia < nargs; ++ia, ++i)
21693 TREE_VEC_ELT (argvec, i) = args[ia];
21694
21695 /* Copy the parameter into parmvec. */
21696 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21697 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21698 /*subr=*/subr, explain_p))
21699 return 1;
21700
21701 /* Advance to the end of the list of parameters. */
21702 parms = TREE_CHAIN (parms);
21703 }
21704
21705 /* Fail if we've reached the end of the parm list, and more args
21706 are present, and the parm list isn't variadic. */
21707 if (ia < nargs && parms == void_list_node)
21708 return unify_too_many_arguments (explain_p, nargs, ia);
21709 /* Fail if parms are left and they don't have default values and
21710 they aren't all deduced as empty packs (c++/57397). This is
21711 consistent with sufficient_parms_p. */
21712 if (parms && parms != void_list_node
21713 && TREE_PURPOSE (parms) == NULL_TREE)
21714 {
21715 unsigned int count = nargs;
21716 tree p = parms;
21717 bool type_pack_p;
21718 do
21719 {
21720 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21721 if (!type_pack_p)
21722 count++;
21723 p = TREE_CHAIN (p);
21724 }
21725 while (p && p != void_list_node);
21726 if (count != nargs)
21727 return unify_too_few_arguments (explain_p, ia, count,
21728 type_pack_p);
21729 }
21730
21731 if (!subr)
21732 {
21733 tsubst_flags_t complain = (explain_p
21734 ? tf_warning_or_error
21735 : tf_none);
21736 bool tried_array_deduction = (cxx_dialect < cxx17);
21737
21738 for (i = 0; i < ntparms; i++)
21739 {
21740 tree targ = TREE_VEC_ELT (targs, i);
21741 tree tparm = TREE_VEC_ELT (tparms, i);
21742
21743 /* Clear the "incomplete" flags on all argument packs now so that
21744 substituting them into later default arguments works. */
21745 if (targ && ARGUMENT_PACK_P (targ))
21746 {
21747 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21748 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21749 }
21750
21751 if (targ || tparm == error_mark_node)
21752 continue;
21753 tparm = TREE_VALUE (tparm);
21754
21755 if (TREE_CODE (tparm) == TYPE_DECL
21756 && !tried_array_deduction)
21757 {
21758 try_array_deduction (tparms, targs, xparms);
21759 tried_array_deduction = true;
21760 if (TREE_VEC_ELT (targs, i))
21761 continue;
21762 }
21763
21764 /* If this is an undeduced nontype parameter that depends on
21765 a type parameter, try another pass; its type may have been
21766 deduced from a later argument than the one from which
21767 this parameter can be deduced. */
21768 if (TREE_CODE (tparm) == PARM_DECL
21769 && uses_template_parms (TREE_TYPE (tparm))
21770 && saw_undeduced < 2)
21771 {
21772 saw_undeduced = 1;
21773 continue;
21774 }
21775
21776 /* Core issue #226 (C++0x) [temp.deduct]:
21777
21778 If a template argument has not been deduced, its
21779 default template argument, if any, is used.
21780
21781 When we are in C++98 mode, TREE_PURPOSE will either
21782 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21783 to explicitly check cxx_dialect here. */
21784 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21785 /* OK, there is a default argument. Wait until after the
21786 conversion check to do substitution. */
21787 continue;
21788
21789 /* If the type parameter is a parameter pack, then it will
21790 be deduced to an empty parameter pack. */
21791 if (template_parameter_pack_p (tparm))
21792 {
21793 tree arg;
21794
21795 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21796 {
21797 arg = make_node (NONTYPE_ARGUMENT_PACK);
21798 TREE_CONSTANT (arg) = 1;
21799 }
21800 else
21801 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21802
21803 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21804
21805 TREE_VEC_ELT (targs, i) = arg;
21806 continue;
21807 }
21808
21809 return unify_parameter_deduction_failure (explain_p, tparm);
21810 }
21811
21812 /* Now substitute into the default template arguments. */
21813 for (i = 0; i < ntparms; i++)
21814 {
21815 tree targ = TREE_VEC_ELT (targs, i);
21816 tree tparm = TREE_VEC_ELT (tparms, i);
21817
21818 if (targ || tparm == error_mark_node)
21819 continue;
21820 tree parm = TREE_VALUE (tparm);
21821 tree arg = TREE_PURPOSE (tparm);
21822 reopen_deferring_access_checks (*checks);
21823 location_t save_loc = input_location;
21824 if (DECL_P (parm))
21825 input_location = DECL_SOURCE_LOCATION (parm);
21826
21827 if (saw_undeduced == 1
21828 && TREE_CODE (parm) == PARM_DECL
21829 && uses_template_parms (TREE_TYPE (parm)))
21830 {
21831 /* The type of this non-type parameter depends on undeduced
21832 parameters. Don't try to use its default argument yet,
21833 since we might deduce an argument for it on the next pass,
21834 but do check whether the arguments we already have cause
21835 substitution failure, so that that happens before we try
21836 later default arguments (78489). */
21837 ++processing_template_decl;
21838 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21839 NULL_TREE);
21840 --processing_template_decl;
21841 if (type == error_mark_node)
21842 arg = error_mark_node;
21843 else
21844 arg = NULL_TREE;
21845 }
21846 else
21847 {
21848 /* Even if the call is happening in template context, getting
21849 here means it's non-dependent, and a default argument is
21850 considered a separate definition under [temp.decls], so we can
21851 do this substitution without processing_template_decl. This
21852 is important if the default argument contains something that
21853 might be instantiation-dependent like access (87480). */
21854 processing_template_decl_sentinel s;
21855 tree substed = NULL_TREE;
21856 if (saw_undeduced == 1)
21857 {
21858 /* First instatiate in template context, in case we still
21859 depend on undeduced template parameters. */
21860 ++processing_template_decl;
21861 substed = tsubst_template_arg (arg, full_targs, complain,
21862 NULL_TREE);
21863 --processing_template_decl;
21864 if (substed != error_mark_node
21865 && !uses_template_parms (substed))
21866 /* We replaced all the tparms, substitute again out of
21867 template context. */
21868 substed = NULL_TREE;
21869 }
21870 if (!substed)
21871 substed = tsubst_template_arg (arg, full_targs, complain,
21872 NULL_TREE);
21873
21874 if (!uses_template_parms (substed))
21875 arg = convert_template_argument (parm, substed, full_targs,
21876 complain, i, NULL_TREE);
21877 else if (saw_undeduced == 1)
21878 arg = NULL_TREE;
21879 else
21880 arg = error_mark_node;
21881 }
21882
21883 input_location = save_loc;
21884 *checks = get_deferred_access_checks ();
21885 pop_deferring_access_checks ();
21886
21887 if (arg == error_mark_node)
21888 return 1;
21889 else if (arg)
21890 {
21891 TREE_VEC_ELT (targs, i) = arg;
21892 /* The position of the first default template argument,
21893 is also the number of non-defaulted arguments in TARGS.
21894 Record that. */
21895 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21897 }
21898 }
21899
21900 if (saw_undeduced++ == 1)
21901 goto again;
21902 }
21903
21904 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21905 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21906
21907 return unify_success (explain_p);
21908 }
21909
21910 /* Subroutine of type_unification_real. Args are like the variables
21911 at the call site. ARG is an overloaded function (or template-id);
21912 we try deducing template args from each of the overloads, and if
21913 only one succeeds, we go with that. Modifies TARGS and returns
21914 true on success. */
21915
21916 static bool
21917 resolve_overloaded_unification (tree tparms,
21918 tree targs,
21919 tree parm,
21920 tree arg,
21921 unification_kind_t strict,
21922 int sub_strict,
21923 bool explain_p)
21924 {
21925 tree tempargs = copy_node (targs);
21926 int good = 0;
21927 tree goodfn = NULL_TREE;
21928 bool addr_p;
21929
21930 if (TREE_CODE (arg) == ADDR_EXPR)
21931 {
21932 arg = TREE_OPERAND (arg, 0);
21933 addr_p = true;
21934 }
21935 else
21936 addr_p = false;
21937
21938 if (TREE_CODE (arg) == COMPONENT_REF)
21939 /* Handle `&x' where `x' is some static or non-static member
21940 function name. */
21941 arg = TREE_OPERAND (arg, 1);
21942
21943 if (TREE_CODE (arg) == OFFSET_REF)
21944 arg = TREE_OPERAND (arg, 1);
21945
21946 /* Strip baselink information. */
21947 if (BASELINK_P (arg))
21948 arg = BASELINK_FUNCTIONS (arg);
21949
21950 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21951 {
21952 /* If we got some explicit template args, we need to plug them into
21953 the affected templates before we try to unify, in case the
21954 explicit args will completely resolve the templates in question. */
21955
21956 int ok = 0;
21957 tree expl_subargs = TREE_OPERAND (arg, 1);
21958 arg = TREE_OPERAND (arg, 0);
21959
21960 for (lkp_iterator iter (arg); iter; ++iter)
21961 {
21962 tree fn = *iter;
21963 tree subargs, elem;
21964
21965 if (TREE_CODE (fn) != TEMPLATE_DECL)
21966 continue;
21967
21968 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21969 expl_subargs, NULL_TREE, tf_none,
21970 /*require_all_args=*/true,
21971 /*use_default_args=*/true);
21972 if (subargs != error_mark_node
21973 && !any_dependent_template_arguments_p (subargs))
21974 {
21975 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21976 if (try_one_overload (tparms, targs, tempargs, parm,
21977 elem, strict, sub_strict, addr_p, explain_p)
21978 && (!goodfn || !same_type_p (goodfn, elem)))
21979 {
21980 goodfn = elem;
21981 ++good;
21982 }
21983 }
21984 else if (subargs)
21985 ++ok;
21986 }
21987 /* If no templates (or more than one) are fully resolved by the
21988 explicit arguments, this template-id is a non-deduced context; it
21989 could still be OK if we deduce all template arguments for the
21990 enclosing call through other arguments. */
21991 if (good != 1)
21992 good = ok;
21993 }
21994 else if (!OVL_P (arg))
21995 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21996 -- but the deduction does not succeed because the expression is
21997 not just the function on its own. */
21998 return false;
21999 else
22000 for (lkp_iterator iter (arg); iter; ++iter)
22001 {
22002 tree fn = *iter;
22003 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22004 strict, sub_strict, addr_p, explain_p)
22005 && (!goodfn || !decls_match (goodfn, fn)))
22006 {
22007 goodfn = fn;
22008 ++good;
22009 }
22010 }
22011
22012 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22013 to function or pointer to member function argument if the set of
22014 overloaded functions does not contain function templates and at most
22015 one of a set of overloaded functions provides a unique match.
22016
22017 So if we found multiple possibilities, we return success but don't
22018 deduce anything. */
22019
22020 if (good == 1)
22021 {
22022 int i = TREE_VEC_LENGTH (targs);
22023 for (; i--; )
22024 if (TREE_VEC_ELT (tempargs, i))
22025 {
22026 tree old = TREE_VEC_ELT (targs, i);
22027 tree new_ = TREE_VEC_ELT (tempargs, i);
22028 if (new_ && old && ARGUMENT_PACK_P (old)
22029 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22030 /* Don't forget explicit template arguments in a pack. */
22031 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22032 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22033 TREE_VEC_ELT (targs, i) = new_;
22034 }
22035 }
22036 if (good)
22037 return true;
22038
22039 return false;
22040 }
22041
22042 /* Core DR 115: In contexts where deduction is done and fails, or in
22043 contexts where deduction is not done, if a template argument list is
22044 specified and it, along with any default template arguments, identifies
22045 a single function template specialization, then the template-id is an
22046 lvalue for the function template specialization. */
22047
22048 tree
22049 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22050 {
22051 tree expr, offset, baselink;
22052 bool addr;
22053
22054 if (!type_unknown_p (orig_expr))
22055 return orig_expr;
22056
22057 expr = orig_expr;
22058 addr = false;
22059 offset = NULL_TREE;
22060 baselink = NULL_TREE;
22061
22062 if (TREE_CODE (expr) == ADDR_EXPR)
22063 {
22064 expr = TREE_OPERAND (expr, 0);
22065 addr = true;
22066 }
22067 if (TREE_CODE (expr) == OFFSET_REF)
22068 {
22069 offset = expr;
22070 expr = TREE_OPERAND (expr, 1);
22071 }
22072 if (BASELINK_P (expr))
22073 {
22074 baselink = expr;
22075 expr = BASELINK_FUNCTIONS (expr);
22076 }
22077
22078 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22079 {
22080 int good = 0;
22081 tree goodfn = NULL_TREE;
22082
22083 /* If we got some explicit template args, we need to plug them into
22084 the affected templates before we try to unify, in case the
22085 explicit args will completely resolve the templates in question. */
22086
22087 tree expl_subargs = TREE_OPERAND (expr, 1);
22088 tree arg = TREE_OPERAND (expr, 0);
22089 tree badfn = NULL_TREE;
22090 tree badargs = NULL_TREE;
22091
22092 for (lkp_iterator iter (arg); iter; ++iter)
22093 {
22094 tree fn = *iter;
22095 tree subargs, elem;
22096
22097 if (TREE_CODE (fn) != TEMPLATE_DECL)
22098 continue;
22099
22100 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22101 expl_subargs, NULL_TREE, tf_none,
22102 /*require_all_args=*/true,
22103 /*use_default_args=*/true);
22104 if (subargs != error_mark_node
22105 && !any_dependent_template_arguments_p (subargs))
22106 {
22107 elem = instantiate_template (fn, subargs, tf_none);
22108 if (elem == error_mark_node)
22109 {
22110 badfn = fn;
22111 badargs = subargs;
22112 }
22113 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
22114 {
22115 goodfn = elem;
22116 ++good;
22117 }
22118 }
22119 }
22120 if (good == 1)
22121 {
22122 mark_used (goodfn);
22123 expr = goodfn;
22124 if (baselink)
22125 expr = build_baselink (BASELINK_BINFO (baselink),
22126 BASELINK_ACCESS_BINFO (baselink),
22127 expr, BASELINK_OPTYPE (baselink));
22128 if (offset)
22129 {
22130 tree base
22131 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22132 expr = build_offset_ref (base, expr, addr, complain);
22133 }
22134 if (addr)
22135 expr = cp_build_addr_expr (expr, complain);
22136 return expr;
22137 }
22138 else if (good == 0 && badargs && (complain & tf_error))
22139 /* There were no good options and at least one bad one, so let the
22140 user know what the problem is. */
22141 instantiate_template (badfn, badargs, complain);
22142 }
22143 return orig_expr;
22144 }
22145
22146 /* As above, but error out if the expression remains overloaded. */
22147
22148 tree
22149 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22150 {
22151 exp = resolve_nondeduced_context (exp, complain);
22152 if (type_unknown_p (exp))
22153 {
22154 if (complain & tf_error)
22155 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22156 return error_mark_node;
22157 }
22158 return exp;
22159 }
22160
22161 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22162 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22163 different overloads deduce different arguments for a given parm.
22164 ADDR_P is true if the expression for which deduction is being
22165 performed was of the form "& fn" rather than simply "fn".
22166
22167 Returns 1 on success. */
22168
22169 static int
22170 try_one_overload (tree tparms,
22171 tree orig_targs,
22172 tree targs,
22173 tree parm,
22174 tree arg,
22175 unification_kind_t strict,
22176 int sub_strict,
22177 bool addr_p,
22178 bool explain_p)
22179 {
22180 int nargs;
22181 tree tempargs;
22182 int i;
22183
22184 if (arg == error_mark_node)
22185 return 0;
22186
22187 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22188 to function or pointer to member function argument if the set of
22189 overloaded functions does not contain function templates and at most
22190 one of a set of overloaded functions provides a unique match.
22191
22192 So if this is a template, just return success. */
22193
22194 if (uses_template_parms (arg))
22195 return 1;
22196
22197 if (TREE_CODE (arg) == METHOD_TYPE)
22198 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22199 else if (addr_p)
22200 arg = build_pointer_type (arg);
22201
22202 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22203
22204 /* We don't copy orig_targs for this because if we have already deduced
22205 some template args from previous args, unify would complain when we
22206 try to deduce a template parameter for the same argument, even though
22207 there isn't really a conflict. */
22208 nargs = TREE_VEC_LENGTH (targs);
22209 tempargs = make_tree_vec (nargs);
22210
22211 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22212 return 0;
22213
22214 /* First make sure we didn't deduce anything that conflicts with
22215 explicitly specified args. */
22216 for (i = nargs; i--; )
22217 {
22218 tree elt = TREE_VEC_ELT (tempargs, i);
22219 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22220
22221 if (!elt)
22222 /*NOP*/;
22223 else if (uses_template_parms (elt))
22224 /* Since we're unifying against ourselves, we will fill in
22225 template args used in the function parm list with our own
22226 template parms. Discard them. */
22227 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22228 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22229 {
22230 /* Check that the argument at each index of the deduced argument pack
22231 is equivalent to the corresponding explicitly specified argument.
22232 We may have deduced more arguments than were explicitly specified,
22233 and that's OK. */
22234
22235 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22236 that's wrong if we deduce the same argument pack from multiple
22237 function arguments: it's only incomplete the first time. */
22238
22239 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22240 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22241
22242 if (TREE_VEC_LENGTH (deduced_pack)
22243 < TREE_VEC_LENGTH (explicit_pack))
22244 return 0;
22245
22246 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22247 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22248 TREE_VEC_ELT (deduced_pack, j)))
22249 return 0;
22250 }
22251 else if (oldelt && !template_args_equal (oldelt, elt))
22252 return 0;
22253 }
22254
22255 for (i = nargs; i--; )
22256 {
22257 tree elt = TREE_VEC_ELT (tempargs, i);
22258
22259 if (elt)
22260 TREE_VEC_ELT (targs, i) = elt;
22261 }
22262
22263 return 1;
22264 }
22265
22266 /* PARM is a template class (perhaps with unbound template
22267 parameters). ARG is a fully instantiated type. If ARG can be
22268 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22269 TARGS are as for unify. */
22270
22271 static tree
22272 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22273 bool explain_p)
22274 {
22275 tree copy_of_targs;
22276
22277 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22278 return NULL_TREE;
22279 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22280 /* Matches anything. */;
22281 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22282 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22283 return NULL_TREE;
22284
22285 /* We need to make a new template argument vector for the call to
22286 unify. If we used TARGS, we'd clutter it up with the result of
22287 the attempted unification, even if this class didn't work out.
22288 We also don't want to commit ourselves to all the unifications
22289 we've already done, since unification is supposed to be done on
22290 an argument-by-argument basis. In other words, consider the
22291 following pathological case:
22292
22293 template <int I, int J, int K>
22294 struct S {};
22295
22296 template <int I, int J>
22297 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22298
22299 template <int I, int J, int K>
22300 void f(S<I, J, K>, S<I, I, I>);
22301
22302 void g() {
22303 S<0, 0, 0> s0;
22304 S<0, 1, 2> s2;
22305
22306 f(s0, s2);
22307 }
22308
22309 Now, by the time we consider the unification involving `s2', we
22310 already know that we must have `f<0, 0, 0>'. But, even though
22311 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22312 because there are two ways to unify base classes of S<0, 1, 2>
22313 with S<I, I, I>. If we kept the already deduced knowledge, we
22314 would reject the possibility I=1. */
22315 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22316
22317 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22318 {
22319 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22320 return NULL_TREE;
22321 return arg;
22322 }
22323
22324 /* If unification failed, we're done. */
22325 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22326 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22327 return NULL_TREE;
22328
22329 return arg;
22330 }
22331
22332 /* Given a template type PARM and a class type ARG, find the unique
22333 base type in ARG that is an instance of PARM. We do not examine
22334 ARG itself; only its base-classes. If there is not exactly one
22335 appropriate base class, return NULL_TREE. PARM may be the type of
22336 a partial specialization, as well as a plain template type. Used
22337 by unify. */
22338
22339 static enum template_base_result
22340 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22341 bool explain_p, tree *result)
22342 {
22343 tree rval = NULL_TREE;
22344 tree binfo;
22345
22346 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22347
22348 binfo = TYPE_BINFO (complete_type (arg));
22349 if (!binfo)
22350 {
22351 /* The type could not be completed. */
22352 *result = NULL_TREE;
22353 return tbr_incomplete_type;
22354 }
22355
22356 /* Walk in inheritance graph order. The search order is not
22357 important, and this avoids multiple walks of virtual bases. */
22358 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22359 {
22360 tree r = try_class_unification (tparms, targs, parm,
22361 BINFO_TYPE (binfo), explain_p);
22362
22363 if (r)
22364 {
22365 /* If there is more than one satisfactory baseclass, then:
22366
22367 [temp.deduct.call]
22368
22369 If they yield more than one possible deduced A, the type
22370 deduction fails.
22371
22372 applies. */
22373 if (rval && !same_type_p (r, rval))
22374 {
22375 *result = NULL_TREE;
22376 return tbr_ambiguous_baseclass;
22377 }
22378
22379 rval = r;
22380 }
22381 }
22382
22383 *result = rval;
22384 return tbr_success;
22385 }
22386
22387 /* Returns the level of DECL, which declares a template parameter. */
22388
22389 static int
22390 template_decl_level (tree decl)
22391 {
22392 switch (TREE_CODE (decl))
22393 {
22394 case TYPE_DECL:
22395 case TEMPLATE_DECL:
22396 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22397
22398 case PARM_DECL:
22399 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22400
22401 default:
22402 gcc_unreachable ();
22403 }
22404 return 0;
22405 }
22406
22407 /* Decide whether ARG can be unified with PARM, considering only the
22408 cv-qualifiers of each type, given STRICT as documented for unify.
22409 Returns nonzero iff the unification is OK on that basis. */
22410
22411 static int
22412 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22413 {
22414 int arg_quals = cp_type_quals (arg);
22415 int parm_quals = cp_type_quals (parm);
22416
22417 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22418 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22419 {
22420 /* Although a CVR qualifier is ignored when being applied to a
22421 substituted template parameter ([8.3.2]/1 for example), that
22422 does not allow us to unify "const T" with "int&" because both
22423 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22424 It is ok when we're allowing additional CV qualifiers
22425 at the outer level [14.8.2.1]/3,1st bullet. */
22426 if ((TYPE_REF_P (arg)
22427 || FUNC_OR_METHOD_TYPE_P (arg))
22428 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22429 return 0;
22430
22431 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22432 && (parm_quals & TYPE_QUAL_RESTRICT))
22433 return 0;
22434 }
22435
22436 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22437 && (arg_quals & parm_quals) != parm_quals)
22438 return 0;
22439
22440 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22441 && (parm_quals & arg_quals) != arg_quals)
22442 return 0;
22443
22444 return 1;
22445 }
22446
22447 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22448 void
22449 template_parm_level_and_index (tree parm, int* level, int* index)
22450 {
22451 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22452 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22453 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22454 {
22455 *index = TEMPLATE_TYPE_IDX (parm);
22456 *level = TEMPLATE_TYPE_LEVEL (parm);
22457 }
22458 else
22459 {
22460 *index = TEMPLATE_PARM_IDX (parm);
22461 *level = TEMPLATE_PARM_LEVEL (parm);
22462 }
22463 }
22464
22465 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22466 do { \
22467 if (unify (TP, TA, P, A, S, EP)) \
22468 return 1; \
22469 } while (0)
22470
22471 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22472 expansion at the end of PACKED_PARMS. Returns 0 if the type
22473 deduction succeeds, 1 otherwise. STRICT is the same as in
22474 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22475 function call argument list. We'll need to adjust the arguments to make them
22476 types. SUBR tells us if this is from a recursive call to
22477 type_unification_real, or for comparing two template argument
22478 lists. */
22479
22480 static int
22481 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22482 tree packed_args, unification_kind_t strict,
22483 bool subr, bool explain_p)
22484 {
22485 tree parm
22486 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22487 tree pattern = PACK_EXPANSION_PATTERN (parm);
22488 tree pack, packs = NULL_TREE;
22489 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22490
22491 /* Add in any args remembered from an earlier partial instantiation. */
22492 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22493 int levels = TMPL_ARGS_DEPTH (targs);
22494
22495 packed_args = expand_template_argument_pack (packed_args);
22496
22497 int len = TREE_VEC_LENGTH (packed_args);
22498
22499 /* Determine the parameter packs we will be deducing from the
22500 pattern, and record their current deductions. */
22501 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22502 pack; pack = TREE_CHAIN (pack))
22503 {
22504 tree parm_pack = TREE_VALUE (pack);
22505 int idx, level;
22506
22507 /* Only template parameter packs can be deduced, not e.g. function
22508 parameter packs or __bases or __integer_pack. */
22509 if (!TEMPLATE_PARM_P (parm_pack))
22510 continue;
22511
22512 /* Determine the index and level of this parameter pack. */
22513 template_parm_level_and_index (parm_pack, &level, &idx);
22514 if (level < levels)
22515 continue;
22516
22517 /* Keep track of the parameter packs and their corresponding
22518 argument packs. */
22519 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22520 TREE_TYPE (packs) = make_tree_vec (len - start);
22521 }
22522
22523 /* Loop through all of the arguments that have not yet been
22524 unified and unify each with the pattern. */
22525 for (i = start; i < len; i++)
22526 {
22527 tree parm;
22528 bool any_explicit = false;
22529 tree arg = TREE_VEC_ELT (packed_args, i);
22530
22531 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22532 or the element of its argument pack at the current index if
22533 this argument was explicitly specified. */
22534 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22535 {
22536 int idx, level;
22537 tree arg, pargs;
22538 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22539
22540 arg = NULL_TREE;
22541 if (TREE_VALUE (pack)
22542 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22543 && (i - start < TREE_VEC_LENGTH (pargs)))
22544 {
22545 any_explicit = true;
22546 arg = TREE_VEC_ELT (pargs, i - start);
22547 }
22548 TMPL_ARG (targs, level, idx) = arg;
22549 }
22550
22551 /* If we had explicit template arguments, substitute them into the
22552 pattern before deduction. */
22553 if (any_explicit)
22554 {
22555 /* Some arguments might still be unspecified or dependent. */
22556 bool dependent;
22557 ++processing_template_decl;
22558 dependent = any_dependent_template_arguments_p (targs);
22559 if (!dependent)
22560 --processing_template_decl;
22561 parm = tsubst (pattern, targs,
22562 explain_p ? tf_warning_or_error : tf_none,
22563 NULL_TREE);
22564 if (dependent)
22565 --processing_template_decl;
22566 if (parm == error_mark_node)
22567 return 1;
22568 }
22569 else
22570 parm = pattern;
22571
22572 /* Unify the pattern with the current argument. */
22573 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22574 explain_p))
22575 return 1;
22576
22577 /* For each parameter pack, collect the deduced value. */
22578 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22579 {
22580 int idx, level;
22581 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22582
22583 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22584 TMPL_ARG (targs, level, idx);
22585 }
22586 }
22587
22588 /* Verify that the results of unification with the parameter packs
22589 produce results consistent with what we've seen before, and make
22590 the deduced argument packs available. */
22591 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22592 {
22593 tree old_pack = TREE_VALUE (pack);
22594 tree new_args = TREE_TYPE (pack);
22595 int i, len = TREE_VEC_LENGTH (new_args);
22596 int idx, level;
22597 bool nondeduced_p = false;
22598
22599 /* By default keep the original deduced argument pack.
22600 If necessary, more specific code is going to update the
22601 resulting deduced argument later down in this function. */
22602 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22603 TMPL_ARG (targs, level, idx) = old_pack;
22604
22605 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22606 actually deduce anything. */
22607 for (i = 0; i < len && !nondeduced_p; ++i)
22608 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22609 nondeduced_p = true;
22610 if (nondeduced_p)
22611 continue;
22612
22613 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22614 {
22615 /* If we had fewer function args than explicit template args,
22616 just use the explicits. */
22617 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22618 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22619 if (len < explicit_len)
22620 new_args = explicit_args;
22621 }
22622
22623 if (!old_pack)
22624 {
22625 tree result;
22626 /* Build the deduced *_ARGUMENT_PACK. */
22627 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22628 {
22629 result = make_node (NONTYPE_ARGUMENT_PACK);
22630 TREE_CONSTANT (result) = 1;
22631 }
22632 else
22633 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22634
22635 SET_ARGUMENT_PACK_ARGS (result, new_args);
22636
22637 /* Note the deduced argument packs for this parameter
22638 pack. */
22639 TMPL_ARG (targs, level, idx) = result;
22640 }
22641 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22642 && (ARGUMENT_PACK_ARGS (old_pack)
22643 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22644 {
22645 /* We only had the explicitly-provided arguments before, but
22646 now we have a complete set of arguments. */
22647 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22648
22649 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22650 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22651 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22652 }
22653 else
22654 {
22655 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22656 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22657
22658 if (!comp_template_args (old_args, new_args,
22659 &bad_old_arg, &bad_new_arg))
22660 /* Inconsistent unification of this parameter pack. */
22661 return unify_parameter_pack_inconsistent (explain_p,
22662 bad_old_arg,
22663 bad_new_arg);
22664 }
22665 }
22666
22667 return unify_success (explain_p);
22668 }
22669
22670 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22671 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22672 parameters and return value are as for unify. */
22673
22674 static int
22675 unify_array_domain (tree tparms, tree targs,
22676 tree parm_dom, tree arg_dom,
22677 bool explain_p)
22678 {
22679 tree parm_max;
22680 tree arg_max;
22681 bool parm_cst;
22682 bool arg_cst;
22683
22684 /* Our representation of array types uses "N - 1" as the
22685 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22686 not an integer constant. We cannot unify arbitrarily
22687 complex expressions, so we eliminate the MINUS_EXPRs
22688 here. */
22689 parm_max = TYPE_MAX_VALUE (parm_dom);
22690 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22691 if (!parm_cst)
22692 {
22693 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22694 parm_max = TREE_OPERAND (parm_max, 0);
22695 }
22696 arg_max = TYPE_MAX_VALUE (arg_dom);
22697 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22698 if (!arg_cst)
22699 {
22700 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22701 trying to unify the type of a variable with the type
22702 of a template parameter. For example:
22703
22704 template <unsigned int N>
22705 void f (char (&) [N]);
22706 int g();
22707 void h(int i) {
22708 char a[g(i)];
22709 f(a);
22710 }
22711
22712 Here, the type of the ARG will be "int [g(i)]", and
22713 may be a SAVE_EXPR, etc. */
22714 if (TREE_CODE (arg_max) != MINUS_EXPR)
22715 return unify_vla_arg (explain_p, arg_dom);
22716 arg_max = TREE_OPERAND (arg_max, 0);
22717 }
22718
22719 /* If only one of the bounds used a MINUS_EXPR, compensate
22720 by adding one to the other bound. */
22721 if (parm_cst && !arg_cst)
22722 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22723 integer_type_node,
22724 parm_max,
22725 integer_one_node);
22726 else if (arg_cst && !parm_cst)
22727 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22728 integer_type_node,
22729 arg_max,
22730 integer_one_node);
22731
22732 return unify (tparms, targs, parm_max, arg_max,
22733 UNIFY_ALLOW_INTEGER, explain_p);
22734 }
22735
22736 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22737
22738 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22739
22740 static pa_kind_t
22741 pa_kind (tree t)
22742 {
22743 if (PACK_EXPANSION_P (t))
22744 t = PACK_EXPANSION_PATTERN (t);
22745 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22746 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22747 || DECL_TYPE_TEMPLATE_P (t))
22748 return pa_tmpl;
22749 else if (TYPE_P (t))
22750 return pa_type;
22751 else
22752 return pa_expr;
22753 }
22754
22755 /* Deduce the value of template parameters. TPARMS is the (innermost)
22756 set of template parameters to a template. TARGS is the bindings
22757 for those template parameters, as determined thus far; TARGS may
22758 include template arguments for outer levels of template parameters
22759 as well. PARM is a parameter to a template function, or a
22760 subcomponent of that parameter; ARG is the corresponding argument.
22761 This function attempts to match PARM with ARG in a manner
22762 consistent with the existing assignments in TARGS. If more values
22763 are deduced, then TARGS is updated.
22764
22765 Returns 0 if the type deduction succeeds, 1 otherwise. The
22766 parameter STRICT is a bitwise or of the following flags:
22767
22768 UNIFY_ALLOW_NONE:
22769 Require an exact match between PARM and ARG.
22770 UNIFY_ALLOW_MORE_CV_QUAL:
22771 Allow the deduced ARG to be more cv-qualified (by qualification
22772 conversion) than ARG.
22773 UNIFY_ALLOW_LESS_CV_QUAL:
22774 Allow the deduced ARG to be less cv-qualified than ARG.
22775 UNIFY_ALLOW_DERIVED:
22776 Allow the deduced ARG to be a template base class of ARG,
22777 or a pointer to a template base class of the type pointed to by
22778 ARG.
22779 UNIFY_ALLOW_INTEGER:
22780 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22781 case for more information.
22782 UNIFY_ALLOW_OUTER_LEVEL:
22783 This is the outermost level of a deduction. Used to determine validity
22784 of qualification conversions. A valid qualification conversion must
22785 have const qualified pointers leading up to the inner type which
22786 requires additional CV quals, except at the outer level, where const
22787 is not required [conv.qual]. It would be normal to set this flag in
22788 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22789 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22790 This is the outermost level of a deduction, and PARM can be more CV
22791 qualified at this point.
22792 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22793 This is the outermost level of a deduction, and PARM can be less CV
22794 qualified at this point. */
22795
22796 static int
22797 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22798 bool explain_p)
22799 {
22800 int idx;
22801 tree targ;
22802 tree tparm;
22803 int strict_in = strict;
22804 tsubst_flags_t complain = (explain_p
22805 ? tf_warning_or_error
22806 : tf_none);
22807
22808 /* I don't think this will do the right thing with respect to types.
22809 But the only case I've seen it in so far has been array bounds, where
22810 signedness is the only information lost, and I think that will be
22811 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22812 finish_id_expression_1, and are also OK. */
22813 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22814 parm = TREE_OPERAND (parm, 0);
22815
22816 if (arg == error_mark_node)
22817 return unify_invalid (explain_p);
22818 if (arg == unknown_type_node
22819 || arg == init_list_type_node)
22820 /* We can't deduce anything from this, but we might get all the
22821 template args from other function args. */
22822 return unify_success (explain_p);
22823
22824 if (parm == any_targ_node || arg == any_targ_node)
22825 return unify_success (explain_p);
22826
22827 /* If PARM uses template parameters, then we can't bail out here,
22828 even if ARG == PARM, since we won't record unifications for the
22829 template parameters. We might need them if we're trying to
22830 figure out which of two things is more specialized. */
22831 if (arg == parm && !uses_template_parms (parm))
22832 return unify_success (explain_p);
22833
22834 /* Handle init lists early, so the rest of the function can assume
22835 we're dealing with a type. */
22836 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22837 {
22838 tree elt, elttype;
22839 unsigned i;
22840 tree orig_parm = parm;
22841
22842 if (!is_std_init_list (parm)
22843 && TREE_CODE (parm) != ARRAY_TYPE)
22844 /* We can only deduce from an initializer list argument if the
22845 parameter is std::initializer_list or an array; otherwise this
22846 is a non-deduced context. */
22847 return unify_success (explain_p);
22848
22849 if (TREE_CODE (parm) == ARRAY_TYPE)
22850 elttype = TREE_TYPE (parm);
22851 else
22852 {
22853 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22854 /* Deduction is defined in terms of a single type, so just punt
22855 on the (bizarre) std::initializer_list<T...>. */
22856 if (PACK_EXPANSION_P (elttype))
22857 return unify_success (explain_p);
22858 }
22859
22860 if (strict != DEDUCE_EXACT
22861 && TYPE_P (elttype)
22862 && !uses_deducible_template_parms (elttype))
22863 /* If ELTTYPE has no deducible template parms, skip deduction from
22864 the list elements. */;
22865 else
22866 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22867 {
22868 int elt_strict = strict;
22869
22870 if (elt == error_mark_node)
22871 return unify_invalid (explain_p);
22872
22873 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22874 {
22875 tree type = TREE_TYPE (elt);
22876 if (type == error_mark_node)
22877 return unify_invalid (explain_p);
22878 /* It should only be possible to get here for a call. */
22879 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22880 elt_strict |= maybe_adjust_types_for_deduction
22881 (DEDUCE_CALL, &elttype, &type, elt);
22882 elt = type;
22883 }
22884
22885 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22886 explain_p);
22887 }
22888
22889 if (TREE_CODE (parm) == ARRAY_TYPE
22890 && deducible_array_bound (TYPE_DOMAIN (parm)))
22891 {
22892 /* Also deduce from the length of the initializer list. */
22893 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22894 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22895 if (idx == error_mark_node)
22896 return unify_invalid (explain_p);
22897 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22898 idx, explain_p);
22899 }
22900
22901 /* If the std::initializer_list<T> deduction worked, replace the
22902 deduced A with std::initializer_list<A>. */
22903 if (orig_parm != parm)
22904 {
22905 idx = TEMPLATE_TYPE_IDX (orig_parm);
22906 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22907 targ = listify (targ);
22908 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22909 }
22910 return unify_success (explain_p);
22911 }
22912
22913 /* If parm and arg aren't the same kind of thing (template, type, or
22914 expression), fail early. */
22915 if (pa_kind (parm) != pa_kind (arg))
22916 return unify_invalid (explain_p);
22917
22918 /* Immediately reject some pairs that won't unify because of
22919 cv-qualification mismatches. */
22920 if (TREE_CODE (arg) == TREE_CODE (parm)
22921 && TYPE_P (arg)
22922 /* It is the elements of the array which hold the cv quals of an array
22923 type, and the elements might be template type parms. We'll check
22924 when we recurse. */
22925 && TREE_CODE (arg) != ARRAY_TYPE
22926 /* We check the cv-qualifiers when unifying with template type
22927 parameters below. We want to allow ARG `const T' to unify with
22928 PARM `T' for example, when computing which of two templates
22929 is more specialized, for example. */
22930 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22931 && !check_cv_quals_for_unify (strict_in, arg, parm))
22932 return unify_cv_qual_mismatch (explain_p, parm, arg);
22933
22934 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22935 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22936 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22937 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22938 strict &= ~UNIFY_ALLOW_DERIVED;
22939 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22940 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22941
22942 switch (TREE_CODE (parm))
22943 {
22944 case TYPENAME_TYPE:
22945 case SCOPE_REF:
22946 case UNBOUND_CLASS_TEMPLATE:
22947 /* In a type which contains a nested-name-specifier, template
22948 argument values cannot be deduced for template parameters used
22949 within the nested-name-specifier. */
22950 return unify_success (explain_p);
22951
22952 case TEMPLATE_TYPE_PARM:
22953 case TEMPLATE_TEMPLATE_PARM:
22954 case BOUND_TEMPLATE_TEMPLATE_PARM:
22955 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22956 if (error_operand_p (tparm))
22957 return unify_invalid (explain_p);
22958
22959 if (TEMPLATE_TYPE_LEVEL (parm)
22960 != template_decl_level (tparm))
22961 /* The PARM is not one we're trying to unify. Just check
22962 to see if it matches ARG. */
22963 {
22964 if (TREE_CODE (arg) == TREE_CODE (parm)
22965 && (is_auto (parm) ? is_auto (arg)
22966 : same_type_p (parm, arg)))
22967 return unify_success (explain_p);
22968 else
22969 return unify_type_mismatch (explain_p, parm, arg);
22970 }
22971 idx = TEMPLATE_TYPE_IDX (parm);
22972 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22973 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22974 if (error_operand_p (tparm))
22975 return unify_invalid (explain_p);
22976
22977 /* Check for mixed types and values. */
22978 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22979 && TREE_CODE (tparm) != TYPE_DECL)
22980 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22981 && TREE_CODE (tparm) != TEMPLATE_DECL))
22982 gcc_unreachable ();
22983
22984 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22985 {
22986 if ((strict_in & UNIFY_ALLOW_DERIVED)
22987 && CLASS_TYPE_P (arg))
22988 {
22989 /* First try to match ARG directly. */
22990 tree t = try_class_unification (tparms, targs, parm, arg,
22991 explain_p);
22992 if (!t)
22993 {
22994 /* Otherwise, look for a suitable base of ARG, as below. */
22995 enum template_base_result r;
22996 r = get_template_base (tparms, targs, parm, arg,
22997 explain_p, &t);
22998 if (!t)
22999 return unify_no_common_base (explain_p, r, parm, arg);
23000 arg = t;
23001 }
23002 }
23003 /* ARG must be constructed from a template class or a template
23004 template parameter. */
23005 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23006 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23007 return unify_template_deduction_failure (explain_p, parm, arg);
23008
23009 /* Deduce arguments T, i from TT<T> or TT<i>. */
23010 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23011 return 1;
23012
23013 arg = TYPE_TI_TEMPLATE (arg);
23014
23015 /* Fall through to deduce template name. */
23016 }
23017
23018 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23019 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23020 {
23021 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23022
23023 /* Simple cases: Value already set, does match or doesn't. */
23024 if (targ != NULL_TREE && template_args_equal (targ, arg))
23025 return unify_success (explain_p);
23026 else if (targ)
23027 return unify_inconsistency (explain_p, parm, targ, arg);
23028 }
23029 else
23030 {
23031 /* If PARM is `const T' and ARG is only `int', we don't have
23032 a match unless we are allowing additional qualification.
23033 If ARG is `const int' and PARM is just `T' that's OK;
23034 that binds `const int' to `T'. */
23035 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23036 arg, parm))
23037 return unify_cv_qual_mismatch (explain_p, parm, arg);
23038
23039 /* Consider the case where ARG is `const volatile int' and
23040 PARM is `const T'. Then, T should be `volatile int'. */
23041 arg = cp_build_qualified_type_real
23042 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23043 if (arg == error_mark_node)
23044 return unify_invalid (explain_p);
23045
23046 /* Simple cases: Value already set, does match or doesn't. */
23047 if (targ != NULL_TREE && same_type_p (targ, arg))
23048 return unify_success (explain_p);
23049 else if (targ)
23050 return unify_inconsistency (explain_p, parm, targ, arg);
23051
23052 /* Make sure that ARG is not a variable-sized array. (Note
23053 that were talking about variable-sized arrays (like
23054 `int[n]'), rather than arrays of unknown size (like
23055 `int[]').) We'll get very confused by such a type since
23056 the bound of the array is not constant, and therefore
23057 not mangleable. Besides, such types are not allowed in
23058 ISO C++, so we can do as we please here. We do allow
23059 them for 'auto' deduction, since that isn't ABI-exposed. */
23060 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23061 return unify_vla_arg (explain_p, arg);
23062
23063 /* Strip typedefs as in convert_template_argument. */
23064 arg = canonicalize_type_argument (arg, tf_none);
23065 }
23066
23067 /* If ARG is a parameter pack or an expansion, we cannot unify
23068 against it unless PARM is also a parameter pack. */
23069 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23070 && !template_parameter_pack_p (parm))
23071 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23072
23073 /* If the argument deduction results is a METHOD_TYPE,
23074 then there is a problem.
23075 METHOD_TYPE doesn't map to any real C++ type the result of
23076 the deduction cannot be of that type. */
23077 if (TREE_CODE (arg) == METHOD_TYPE)
23078 return unify_method_type_error (explain_p, arg);
23079
23080 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23081 return unify_success (explain_p);
23082
23083 case TEMPLATE_PARM_INDEX:
23084 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23085 if (error_operand_p (tparm))
23086 return unify_invalid (explain_p);
23087
23088 if (TEMPLATE_PARM_LEVEL (parm)
23089 != template_decl_level (tparm))
23090 {
23091 /* The PARM is not one we're trying to unify. Just check
23092 to see if it matches ARG. */
23093 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23094 && cp_tree_equal (parm, arg));
23095 if (result)
23096 unify_expression_unequal (explain_p, parm, arg);
23097 return result;
23098 }
23099
23100 idx = TEMPLATE_PARM_IDX (parm);
23101 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23102
23103 if (targ)
23104 {
23105 if ((strict & UNIFY_ALLOW_INTEGER)
23106 && TREE_TYPE (targ) && TREE_TYPE (arg)
23107 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23108 /* We're deducing from an array bound, the type doesn't matter. */
23109 arg = fold_convert (TREE_TYPE (targ), arg);
23110 int x = !cp_tree_equal (targ, arg);
23111 if (x)
23112 unify_inconsistency (explain_p, parm, targ, arg);
23113 return x;
23114 }
23115
23116 /* [temp.deduct.type] If, in the declaration of a function template
23117 with a non-type template-parameter, the non-type
23118 template-parameter is used in an expression in the function
23119 parameter-list and, if the corresponding template-argument is
23120 deduced, the template-argument type shall match the type of the
23121 template-parameter exactly, except that a template-argument
23122 deduced from an array bound may be of any integral type.
23123 The non-type parameter might use already deduced type parameters. */
23124 tparm = TREE_TYPE (parm);
23125 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23126 /* We don't have enough levels of args to do any substitution. This
23127 can happen in the context of -fnew-ttp-matching. */;
23128 else
23129 {
23130 ++processing_template_decl;
23131 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23132 --processing_template_decl;
23133
23134 if (tree a = type_uses_auto (tparm))
23135 {
23136 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23137 if (tparm == error_mark_node)
23138 return 1;
23139 }
23140 }
23141
23142 if (!TREE_TYPE (arg))
23143 /* Template-parameter dependent expression. Just accept it for now.
23144 It will later be processed in convert_template_argument. */
23145 ;
23146 else if (same_type_ignoring_top_level_qualifiers_p
23147 (non_reference (TREE_TYPE (arg)),
23148 non_reference (tparm)))
23149 /* OK. Ignore top-level quals here because a class-type template
23150 parameter object is const. */;
23151 else if ((strict & UNIFY_ALLOW_INTEGER)
23152 && CP_INTEGRAL_TYPE_P (tparm))
23153 /* Convert the ARG to the type of PARM; the deduced non-type
23154 template argument must exactly match the types of the
23155 corresponding parameter. */
23156 arg = fold (build_nop (tparm, arg));
23157 else if (uses_template_parms (tparm))
23158 {
23159 /* We haven't deduced the type of this parameter yet. */
23160 if (cxx_dialect >= cxx17
23161 /* We deduce from array bounds in try_array_deduction. */
23162 && !(strict & UNIFY_ALLOW_INTEGER))
23163 {
23164 /* Deduce it from the non-type argument. */
23165 tree atype = TREE_TYPE (arg);
23166 RECUR_AND_CHECK_FAILURE (tparms, targs,
23167 tparm, atype,
23168 UNIFY_ALLOW_NONE, explain_p);
23169 }
23170 else
23171 /* Try again later. */
23172 return unify_success (explain_p);
23173 }
23174 else
23175 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23176
23177 /* If ARG is a parameter pack or an expansion, we cannot unify
23178 against it unless PARM is also a parameter pack. */
23179 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23180 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23181 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23182
23183 {
23184 bool removed_attr = false;
23185 arg = strip_typedefs_expr (arg, &removed_attr);
23186 }
23187 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23188 return unify_success (explain_p);
23189
23190 case PTRMEM_CST:
23191 {
23192 /* A pointer-to-member constant can be unified only with
23193 another constant. */
23194 if (TREE_CODE (arg) != PTRMEM_CST)
23195 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23196
23197 /* Just unify the class member. It would be useless (and possibly
23198 wrong, depending on the strict flags) to unify also
23199 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23200 arg refer to the same variable, even if through different
23201 classes. For instance:
23202
23203 struct A { int x; };
23204 struct B : A { };
23205
23206 Unification of &A::x and &B::x must succeed. */
23207 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23208 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23209 }
23210
23211 case POINTER_TYPE:
23212 {
23213 if (!TYPE_PTR_P (arg))
23214 return unify_type_mismatch (explain_p, parm, arg);
23215
23216 /* [temp.deduct.call]
23217
23218 A can be another pointer or pointer to member type that can
23219 be converted to the deduced A via a qualification
23220 conversion (_conv.qual_).
23221
23222 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23223 This will allow for additional cv-qualification of the
23224 pointed-to types if appropriate. */
23225
23226 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23227 /* The derived-to-base conversion only persists through one
23228 level of pointers. */
23229 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23230
23231 return unify (tparms, targs, TREE_TYPE (parm),
23232 TREE_TYPE (arg), strict, explain_p);
23233 }
23234
23235 case REFERENCE_TYPE:
23236 if (!TYPE_REF_P (arg))
23237 return unify_type_mismatch (explain_p, parm, arg);
23238 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23239 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23240
23241 case ARRAY_TYPE:
23242 if (TREE_CODE (arg) != ARRAY_TYPE)
23243 return unify_type_mismatch (explain_p, parm, arg);
23244 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23245 != (TYPE_DOMAIN (arg) == NULL_TREE))
23246 return unify_type_mismatch (explain_p, parm, arg);
23247 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23248 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23249 if (TYPE_DOMAIN (parm) != NULL_TREE)
23250 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23251 TYPE_DOMAIN (arg), explain_p);
23252 return unify_success (explain_p);
23253
23254 case REAL_TYPE:
23255 case COMPLEX_TYPE:
23256 case VECTOR_TYPE:
23257 case INTEGER_TYPE:
23258 case BOOLEAN_TYPE:
23259 case ENUMERAL_TYPE:
23260 case VOID_TYPE:
23261 case NULLPTR_TYPE:
23262 if (TREE_CODE (arg) != TREE_CODE (parm))
23263 return unify_type_mismatch (explain_p, parm, arg);
23264
23265 /* We have already checked cv-qualification at the top of the
23266 function. */
23267 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23268 return unify_type_mismatch (explain_p, parm, arg);
23269
23270 /* As far as unification is concerned, this wins. Later checks
23271 will invalidate it if necessary. */
23272 return unify_success (explain_p);
23273
23274 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23275 /* Type INTEGER_CST can come from ordinary constant template args. */
23276 case INTEGER_CST:
23277 while (CONVERT_EXPR_P (arg))
23278 arg = TREE_OPERAND (arg, 0);
23279
23280 if (TREE_CODE (arg) != INTEGER_CST)
23281 return unify_template_argument_mismatch (explain_p, parm, arg);
23282 return (tree_int_cst_equal (parm, arg)
23283 ? unify_success (explain_p)
23284 : unify_template_argument_mismatch (explain_p, parm, arg));
23285
23286 case TREE_VEC:
23287 {
23288 int i, len, argslen;
23289 int parm_variadic_p = 0;
23290
23291 if (TREE_CODE (arg) != TREE_VEC)
23292 return unify_template_argument_mismatch (explain_p, parm, arg);
23293
23294 len = TREE_VEC_LENGTH (parm);
23295 argslen = TREE_VEC_LENGTH (arg);
23296
23297 /* Check for pack expansions in the parameters. */
23298 for (i = 0; i < len; ++i)
23299 {
23300 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23301 {
23302 if (i == len - 1)
23303 /* We can unify against something with a trailing
23304 parameter pack. */
23305 parm_variadic_p = 1;
23306 else
23307 /* [temp.deduct.type]/9: If the template argument list of
23308 P contains a pack expansion that is not the last
23309 template argument, the entire template argument list
23310 is a non-deduced context. */
23311 return unify_success (explain_p);
23312 }
23313 }
23314
23315 /* If we don't have enough arguments to satisfy the parameters
23316 (not counting the pack expression at the end), or we have
23317 too many arguments for a parameter list that doesn't end in
23318 a pack expression, we can't unify. */
23319 if (parm_variadic_p
23320 ? argslen < len - parm_variadic_p
23321 : argslen != len)
23322 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23323
23324 /* Unify all of the parameters that precede the (optional)
23325 pack expression. */
23326 for (i = 0; i < len - parm_variadic_p; ++i)
23327 {
23328 RECUR_AND_CHECK_FAILURE (tparms, targs,
23329 TREE_VEC_ELT (parm, i),
23330 TREE_VEC_ELT (arg, i),
23331 UNIFY_ALLOW_NONE, explain_p);
23332 }
23333 if (parm_variadic_p)
23334 return unify_pack_expansion (tparms, targs, parm, arg,
23335 DEDUCE_EXACT,
23336 /*subr=*/true, explain_p);
23337 return unify_success (explain_p);
23338 }
23339
23340 case RECORD_TYPE:
23341 case UNION_TYPE:
23342 if (TREE_CODE (arg) != TREE_CODE (parm))
23343 return unify_type_mismatch (explain_p, parm, arg);
23344
23345 if (TYPE_PTRMEMFUNC_P (parm))
23346 {
23347 if (!TYPE_PTRMEMFUNC_P (arg))
23348 return unify_type_mismatch (explain_p, parm, arg);
23349
23350 return unify (tparms, targs,
23351 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23352 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23353 strict, explain_p);
23354 }
23355 else if (TYPE_PTRMEMFUNC_P (arg))
23356 return unify_type_mismatch (explain_p, parm, arg);
23357
23358 if (CLASSTYPE_TEMPLATE_INFO (parm))
23359 {
23360 tree t = NULL_TREE;
23361
23362 if (strict_in & UNIFY_ALLOW_DERIVED)
23363 {
23364 /* First, we try to unify the PARM and ARG directly. */
23365 t = try_class_unification (tparms, targs,
23366 parm, arg, explain_p);
23367
23368 if (!t)
23369 {
23370 /* Fallback to the special case allowed in
23371 [temp.deduct.call]:
23372
23373 If P is a class, and P has the form
23374 template-id, then A can be a derived class of
23375 the deduced A. Likewise, if P is a pointer to
23376 a class of the form template-id, A can be a
23377 pointer to a derived class pointed to by the
23378 deduced A. */
23379 enum template_base_result r;
23380 r = get_template_base (tparms, targs, parm, arg,
23381 explain_p, &t);
23382
23383 if (!t)
23384 {
23385 /* Don't give the derived diagnostic if we're
23386 already dealing with the same template. */
23387 bool same_template
23388 = (CLASSTYPE_TEMPLATE_INFO (arg)
23389 && (CLASSTYPE_TI_TEMPLATE (parm)
23390 == CLASSTYPE_TI_TEMPLATE (arg)));
23391 return unify_no_common_base (explain_p && !same_template,
23392 r, parm, arg);
23393 }
23394 }
23395 }
23396 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23397 && (CLASSTYPE_TI_TEMPLATE (parm)
23398 == CLASSTYPE_TI_TEMPLATE (arg)))
23399 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23400 Then, we should unify `int' and `U'. */
23401 t = arg;
23402 else
23403 /* There's no chance of unification succeeding. */
23404 return unify_type_mismatch (explain_p, parm, arg);
23405
23406 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23407 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23408 }
23409 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23410 return unify_type_mismatch (explain_p, parm, arg);
23411 return unify_success (explain_p);
23412
23413 case METHOD_TYPE:
23414 case FUNCTION_TYPE:
23415 {
23416 unsigned int nargs;
23417 tree *args;
23418 tree a;
23419 unsigned int i;
23420
23421 if (TREE_CODE (arg) != TREE_CODE (parm))
23422 return unify_type_mismatch (explain_p, parm, arg);
23423
23424 /* CV qualifications for methods can never be deduced, they must
23425 match exactly. We need to check them explicitly here,
23426 because type_unification_real treats them as any other
23427 cv-qualified parameter. */
23428 if (TREE_CODE (parm) == METHOD_TYPE
23429 && (!check_cv_quals_for_unify
23430 (UNIFY_ALLOW_NONE,
23431 class_of_this_parm (arg),
23432 class_of_this_parm (parm))))
23433 return unify_cv_qual_mismatch (explain_p, parm, arg);
23434 if (TREE_CODE (arg) == FUNCTION_TYPE
23435 && type_memfn_quals (parm) != type_memfn_quals (arg))
23436 return unify_cv_qual_mismatch (explain_p, parm, arg);
23437 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23438 return unify_type_mismatch (explain_p, parm, arg);
23439
23440 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23441 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23442
23443 nargs = list_length (TYPE_ARG_TYPES (arg));
23444 args = XALLOCAVEC (tree, nargs);
23445 for (a = TYPE_ARG_TYPES (arg), i = 0;
23446 a != NULL_TREE && a != void_list_node;
23447 a = TREE_CHAIN (a), ++i)
23448 args[i] = TREE_VALUE (a);
23449 nargs = i;
23450
23451 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23452 args, nargs, 1, DEDUCE_EXACT,
23453 NULL, explain_p))
23454 return 1;
23455
23456 if (flag_noexcept_type)
23457 {
23458 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23459 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23460 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23461 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23462 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23463 && uses_template_parms (TREE_PURPOSE (pspec)))
23464 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23465 TREE_PURPOSE (aspec),
23466 UNIFY_ALLOW_NONE, explain_p);
23467 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23468 return unify_type_mismatch (explain_p, parm, arg);
23469 }
23470
23471 return 0;
23472 }
23473
23474 case OFFSET_TYPE:
23475 /* Unify a pointer to member with a pointer to member function, which
23476 deduces the type of the member as a function type. */
23477 if (TYPE_PTRMEMFUNC_P (arg))
23478 {
23479 /* Check top-level cv qualifiers */
23480 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23481 return unify_cv_qual_mismatch (explain_p, parm, arg);
23482
23483 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23484 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23485 UNIFY_ALLOW_NONE, explain_p);
23486
23487 /* Determine the type of the function we are unifying against. */
23488 tree fntype = static_fn_type (arg);
23489
23490 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23491 }
23492
23493 if (TREE_CODE (arg) != OFFSET_TYPE)
23494 return unify_type_mismatch (explain_p, parm, arg);
23495 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23496 TYPE_OFFSET_BASETYPE (arg),
23497 UNIFY_ALLOW_NONE, explain_p);
23498 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23499 strict, explain_p);
23500
23501 case CONST_DECL:
23502 if (DECL_TEMPLATE_PARM_P (parm))
23503 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23504 if (arg != scalar_constant_value (parm))
23505 return unify_template_argument_mismatch (explain_p, parm, arg);
23506 return unify_success (explain_p);
23507
23508 case FIELD_DECL:
23509 case TEMPLATE_DECL:
23510 /* Matched cases are handled by the ARG == PARM test above. */
23511 return unify_template_argument_mismatch (explain_p, parm, arg);
23512
23513 case VAR_DECL:
23514 /* We might get a variable as a non-type template argument in parm if the
23515 corresponding parameter is type-dependent. Make any necessary
23516 adjustments based on whether arg is a reference. */
23517 if (CONSTANT_CLASS_P (arg))
23518 parm = fold_non_dependent_expr (parm, complain);
23519 else if (REFERENCE_REF_P (arg))
23520 {
23521 tree sub = TREE_OPERAND (arg, 0);
23522 STRIP_NOPS (sub);
23523 if (TREE_CODE (sub) == ADDR_EXPR)
23524 arg = TREE_OPERAND (sub, 0);
23525 }
23526 /* Now use the normal expression code to check whether they match. */
23527 goto expr;
23528
23529 case TYPE_ARGUMENT_PACK:
23530 case NONTYPE_ARGUMENT_PACK:
23531 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23532 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23533
23534 case TYPEOF_TYPE:
23535 case DECLTYPE_TYPE:
23536 case UNDERLYING_TYPE:
23537 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23538 or UNDERLYING_TYPE nodes. */
23539 return unify_success (explain_p);
23540
23541 case ERROR_MARK:
23542 /* Unification fails if we hit an error node. */
23543 return unify_invalid (explain_p);
23544
23545 case INDIRECT_REF:
23546 if (REFERENCE_REF_P (parm))
23547 {
23548 bool pexp = PACK_EXPANSION_P (arg);
23549 if (pexp)
23550 arg = PACK_EXPANSION_PATTERN (arg);
23551 if (REFERENCE_REF_P (arg))
23552 arg = TREE_OPERAND (arg, 0);
23553 if (pexp)
23554 arg = make_pack_expansion (arg, complain);
23555 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23556 strict, explain_p);
23557 }
23558 /* FALLTHRU */
23559
23560 default:
23561 /* An unresolved overload is a nondeduced context. */
23562 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23563 return unify_success (explain_p);
23564 gcc_assert (EXPR_P (parm)
23565 || COMPOUND_LITERAL_P (parm)
23566 || TREE_CODE (parm) == TRAIT_EXPR);
23567 expr:
23568 /* We must be looking at an expression. This can happen with
23569 something like:
23570
23571 template <int I>
23572 void foo(S<I>, S<I + 2>);
23573
23574 or
23575
23576 template<typename T>
23577 void foo(A<T, T{}>);
23578
23579 This is a "non-deduced context":
23580
23581 [deduct.type]
23582
23583 The non-deduced contexts are:
23584
23585 --A non-type template argument or an array bound in which
23586 a subexpression references a template parameter.
23587
23588 In these cases, we assume deduction succeeded, but don't
23589 actually infer any unifications. */
23590
23591 if (!uses_template_parms (parm)
23592 && !template_args_equal (parm, arg))
23593 return unify_expression_unequal (explain_p, parm, arg);
23594 else
23595 return unify_success (explain_p);
23596 }
23597 }
23598 #undef RECUR_AND_CHECK_FAILURE
23599 \f
23600 /* Note that DECL can be defined in this translation unit, if
23601 required. */
23602
23603 static void
23604 mark_definable (tree decl)
23605 {
23606 tree clone;
23607 DECL_NOT_REALLY_EXTERN (decl) = 1;
23608 FOR_EACH_CLONE (clone, decl)
23609 DECL_NOT_REALLY_EXTERN (clone) = 1;
23610 }
23611
23612 /* Called if RESULT is explicitly instantiated, or is a member of an
23613 explicitly instantiated class. */
23614
23615 void
23616 mark_decl_instantiated (tree result, int extern_p)
23617 {
23618 SET_DECL_EXPLICIT_INSTANTIATION (result);
23619
23620 /* If this entity has already been written out, it's too late to
23621 make any modifications. */
23622 if (TREE_ASM_WRITTEN (result))
23623 return;
23624
23625 /* For anonymous namespace we don't need to do anything. */
23626 if (decl_anon_ns_mem_p (result))
23627 {
23628 gcc_assert (!TREE_PUBLIC (result));
23629 return;
23630 }
23631
23632 if (TREE_CODE (result) != FUNCTION_DECL)
23633 /* The TREE_PUBLIC flag for function declarations will have been
23634 set correctly by tsubst. */
23635 TREE_PUBLIC (result) = 1;
23636
23637 /* This might have been set by an earlier implicit instantiation. */
23638 DECL_COMDAT (result) = 0;
23639
23640 if (extern_p)
23641 DECL_NOT_REALLY_EXTERN (result) = 0;
23642 else
23643 {
23644 mark_definable (result);
23645 mark_needed (result);
23646 /* Always make artificials weak. */
23647 if (DECL_ARTIFICIAL (result) && flag_weak)
23648 comdat_linkage (result);
23649 /* For WIN32 we also want to put explicit instantiations in
23650 linkonce sections. */
23651 else if (TREE_PUBLIC (result))
23652 maybe_make_one_only (result);
23653 if (TREE_CODE (result) == FUNCTION_DECL
23654 && DECL_TEMPLATE_INSTANTIATED (result))
23655 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23656 since start_preparsed_function wouldn't have if we had an earlier
23657 extern explicit instantiation. */
23658 DECL_EXTERNAL (result) = 0;
23659 }
23660
23661 /* If EXTERN_P, then this function will not be emitted -- unless
23662 followed by an explicit instantiation, at which point its linkage
23663 will be adjusted. If !EXTERN_P, then this function will be
23664 emitted here. In neither circumstance do we want
23665 import_export_decl to adjust the linkage. */
23666 DECL_INTERFACE_KNOWN (result) = 1;
23667 }
23668
23669 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23670 important template arguments. If any are missing, we check whether
23671 they're important by using error_mark_node for substituting into any
23672 args that were used for partial ordering (the ones between ARGS and END)
23673 and seeing if it bubbles up. */
23674
23675 static bool
23676 check_undeduced_parms (tree targs, tree args, tree end)
23677 {
23678 bool found = false;
23679 int i;
23680 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23681 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23682 {
23683 found = true;
23684 TREE_VEC_ELT (targs, i) = error_mark_node;
23685 }
23686 if (found)
23687 {
23688 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23689 if (substed == error_mark_node)
23690 return true;
23691 }
23692 return false;
23693 }
23694
23695 /* Given two function templates PAT1 and PAT2, return:
23696
23697 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23698 -1 if PAT2 is more specialized than PAT1.
23699 0 if neither is more specialized.
23700
23701 LEN indicates the number of parameters we should consider
23702 (defaulted parameters should not be considered).
23703
23704 The 1998 std underspecified function template partial ordering, and
23705 DR214 addresses the issue. We take pairs of arguments, one from
23706 each of the templates, and deduce them against each other. One of
23707 the templates will be more specialized if all the *other*
23708 template's arguments deduce against its arguments and at least one
23709 of its arguments *does* *not* deduce against the other template's
23710 corresponding argument. Deduction is done as for class templates.
23711 The arguments used in deduction have reference and top level cv
23712 qualifiers removed. Iff both arguments were originally reference
23713 types *and* deduction succeeds in both directions, an lvalue reference
23714 wins against an rvalue reference and otherwise the template
23715 with the more cv-qualified argument wins for that pairing (if
23716 neither is more cv-qualified, they both are equal). Unlike regular
23717 deduction, after all the arguments have been deduced in this way,
23718 we do *not* verify the deduced template argument values can be
23719 substituted into non-deduced contexts.
23720
23721 The logic can be a bit confusing here, because we look at deduce1 and
23722 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23723 can find template arguments for pat1 to make arg1 look like arg2, that
23724 means that arg2 is at least as specialized as arg1. */
23725
23726 int
23727 more_specialized_fn (tree pat1, tree pat2, int len)
23728 {
23729 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23730 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23731 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23732 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23733 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23734 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23735 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23736 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23737 tree origs1, origs2;
23738 bool lose1 = false;
23739 bool lose2 = false;
23740
23741 /* Remove the this parameter from non-static member functions. If
23742 one is a non-static member function and the other is not a static
23743 member function, remove the first parameter from that function
23744 also. This situation occurs for operator functions where we
23745 locate both a member function (with this pointer) and non-member
23746 operator (with explicit first operand). */
23747 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23748 {
23749 len--; /* LEN is the number of significant arguments for DECL1 */
23750 args1 = TREE_CHAIN (args1);
23751 if (!DECL_STATIC_FUNCTION_P (decl2))
23752 args2 = TREE_CHAIN (args2);
23753 }
23754 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23755 {
23756 args2 = TREE_CHAIN (args2);
23757 if (!DECL_STATIC_FUNCTION_P (decl1))
23758 {
23759 len--;
23760 args1 = TREE_CHAIN (args1);
23761 }
23762 }
23763
23764 /* If only one is a conversion operator, they are unordered. */
23765 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23766 return 0;
23767
23768 /* Consider the return type for a conversion function */
23769 if (DECL_CONV_FN_P (decl1))
23770 {
23771 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23772 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23773 len++;
23774 }
23775
23776 processing_template_decl++;
23777
23778 origs1 = args1;
23779 origs2 = args2;
23780
23781 while (len--
23782 /* Stop when an ellipsis is seen. */
23783 && args1 != NULL_TREE && args2 != NULL_TREE)
23784 {
23785 tree arg1 = TREE_VALUE (args1);
23786 tree arg2 = TREE_VALUE (args2);
23787 int deduce1, deduce2;
23788 int quals1 = -1;
23789 int quals2 = -1;
23790 int ref1 = 0;
23791 int ref2 = 0;
23792
23793 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23794 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23795 {
23796 /* When both arguments are pack expansions, we need only
23797 unify the patterns themselves. */
23798 arg1 = PACK_EXPANSION_PATTERN (arg1);
23799 arg2 = PACK_EXPANSION_PATTERN (arg2);
23800
23801 /* This is the last comparison we need to do. */
23802 len = 0;
23803 }
23804
23805 /* DR 1847: If a particular P contains no template-parameters that
23806 participate in template argument deduction, that P is not used to
23807 determine the ordering. */
23808 if (!uses_deducible_template_parms (arg1)
23809 && !uses_deducible_template_parms (arg2))
23810 goto next;
23811
23812 if (TYPE_REF_P (arg1))
23813 {
23814 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23815 arg1 = TREE_TYPE (arg1);
23816 quals1 = cp_type_quals (arg1);
23817 }
23818
23819 if (TYPE_REF_P (arg2))
23820 {
23821 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23822 arg2 = TREE_TYPE (arg2);
23823 quals2 = cp_type_quals (arg2);
23824 }
23825
23826 arg1 = TYPE_MAIN_VARIANT (arg1);
23827 arg2 = TYPE_MAIN_VARIANT (arg2);
23828
23829 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23830 {
23831 int i, len2 = remaining_arguments (args2);
23832 tree parmvec = make_tree_vec (1);
23833 tree argvec = make_tree_vec (len2);
23834 tree ta = args2;
23835
23836 /* Setup the parameter vector, which contains only ARG1. */
23837 TREE_VEC_ELT (parmvec, 0) = arg1;
23838
23839 /* Setup the argument vector, which contains the remaining
23840 arguments. */
23841 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23842 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23843
23844 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23845 argvec, DEDUCE_EXACT,
23846 /*subr=*/true, /*explain_p=*/false)
23847 == 0);
23848
23849 /* We cannot deduce in the other direction, because ARG1 is
23850 a pack expansion but ARG2 is not. */
23851 deduce2 = 0;
23852 }
23853 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23854 {
23855 int i, len1 = remaining_arguments (args1);
23856 tree parmvec = make_tree_vec (1);
23857 tree argvec = make_tree_vec (len1);
23858 tree ta = args1;
23859
23860 /* Setup the parameter vector, which contains only ARG1. */
23861 TREE_VEC_ELT (parmvec, 0) = arg2;
23862
23863 /* Setup the argument vector, which contains the remaining
23864 arguments. */
23865 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23866 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23867
23868 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23869 argvec, DEDUCE_EXACT,
23870 /*subr=*/true, /*explain_p=*/false)
23871 == 0);
23872
23873 /* We cannot deduce in the other direction, because ARG2 is
23874 a pack expansion but ARG1 is not.*/
23875 deduce1 = 0;
23876 }
23877
23878 else
23879 {
23880 /* The normal case, where neither argument is a pack
23881 expansion. */
23882 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23883 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23884 == 0);
23885 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23886 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23887 == 0);
23888 }
23889
23890 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23891 arg2, then arg2 is not as specialized as arg1. */
23892 if (!deduce1)
23893 lose2 = true;
23894 if (!deduce2)
23895 lose1 = true;
23896
23897 /* "If, for a given type, deduction succeeds in both directions
23898 (i.e., the types are identical after the transformations above)
23899 and both P and A were reference types (before being replaced with
23900 the type referred to above):
23901 - if the type from the argument template was an lvalue reference and
23902 the type from the parameter template was not, the argument type is
23903 considered to be more specialized than the other; otherwise,
23904 - if the type from the argument template is more cv-qualified
23905 than the type from the parameter template (as described above),
23906 the argument type is considered to be more specialized than the other;
23907 otherwise,
23908 - neither type is more specialized than the other." */
23909
23910 if (deduce1 && deduce2)
23911 {
23912 if (ref1 && ref2 && ref1 != ref2)
23913 {
23914 if (ref1 > ref2)
23915 lose1 = true;
23916 else
23917 lose2 = true;
23918 }
23919 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23920 {
23921 if ((quals1 & quals2) == quals2)
23922 lose2 = true;
23923 if ((quals1 & quals2) == quals1)
23924 lose1 = true;
23925 }
23926 }
23927
23928 if (lose1 && lose2)
23929 /* We've failed to deduce something in either direction.
23930 These must be unordered. */
23931 break;
23932
23933 next:
23934
23935 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23936 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23937 /* We have already processed all of the arguments in our
23938 handing of the pack expansion type. */
23939 len = 0;
23940
23941 args1 = TREE_CHAIN (args1);
23942 args2 = TREE_CHAIN (args2);
23943 }
23944
23945 /* "In most cases, all template parameters must have values in order for
23946 deduction to succeed, but for partial ordering purposes a template
23947 parameter may remain without a value provided it is not used in the
23948 types being used for partial ordering."
23949
23950 Thus, if we are missing any of the targs1 we need to substitute into
23951 origs1, then pat2 is not as specialized as pat1. This can happen when
23952 there is a nondeduced context. */
23953 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23954 lose2 = true;
23955 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23956 lose1 = true;
23957
23958 processing_template_decl--;
23959
23960 /* If both deductions succeed, the partial ordering selects the more
23961 constrained template. */
23962 if (!lose1 && !lose2)
23963 {
23964 int winner = more_constrained (decl1, decl2);
23965 if (winner > 0)
23966 lose2 = true;
23967 else if (winner < 0)
23968 lose1 = true;
23969 }
23970
23971 /* All things being equal, if the next argument is a pack expansion
23972 for one function but not for the other, prefer the
23973 non-variadic function. FIXME this is bogus; see c++/41958. */
23974 if (lose1 == lose2
23975 && args1 && TREE_VALUE (args1)
23976 && args2 && TREE_VALUE (args2))
23977 {
23978 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23979 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23980 }
23981
23982 if (lose1 == lose2)
23983 return 0;
23984 else if (!lose1)
23985 return 1;
23986 else
23987 return -1;
23988 }
23989
23990 /* Determine which of two partial specializations of TMPL is more
23991 specialized.
23992
23993 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23994 to the first partial specialization. The TREE_PURPOSE is the
23995 innermost set of template parameters for the partial
23996 specialization. PAT2 is similar, but for the second template.
23997
23998 Return 1 if the first partial specialization is more specialized;
23999 -1 if the second is more specialized; 0 if neither is more
24000 specialized.
24001
24002 See [temp.class.order] for information about determining which of
24003 two templates is more specialized. */
24004
24005 static int
24006 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24007 {
24008 tree targs;
24009 int winner = 0;
24010 bool any_deductions = false;
24011
24012 tree tmpl1 = TREE_VALUE (pat1);
24013 tree tmpl2 = TREE_VALUE (pat2);
24014 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24015 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24016
24017 /* Just like what happens for functions, if we are ordering between
24018 different template specializations, we may encounter dependent
24019 types in the arguments, and we need our dependency check functions
24020 to behave correctly. */
24021 ++processing_template_decl;
24022 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24023 if (targs)
24024 {
24025 --winner;
24026 any_deductions = true;
24027 }
24028
24029 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24030 if (targs)
24031 {
24032 ++winner;
24033 any_deductions = true;
24034 }
24035 --processing_template_decl;
24036
24037 /* If both deductions succeed, the partial ordering selects the more
24038 constrained template. */
24039 if (!winner && any_deductions)
24040 winner = more_constrained (tmpl1, tmpl2);
24041
24042 /* In the case of a tie where at least one of the templates
24043 has a parameter pack at the end, the template with the most
24044 non-packed parameters wins. */
24045 if (winner == 0
24046 && any_deductions
24047 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24048 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24049 {
24050 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24051 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24052 int len1 = TREE_VEC_LENGTH (args1);
24053 int len2 = TREE_VEC_LENGTH (args2);
24054
24055 /* We don't count the pack expansion at the end. */
24056 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24057 --len1;
24058 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24059 --len2;
24060
24061 if (len1 > len2)
24062 return 1;
24063 else if (len1 < len2)
24064 return -1;
24065 }
24066
24067 return winner;
24068 }
24069
24070 /* Return the template arguments that will produce the function signature
24071 DECL from the function template FN, with the explicit template
24072 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24073 also match. Return NULL_TREE if no satisfactory arguments could be
24074 found. */
24075
24076 static tree
24077 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24078 {
24079 int ntparms = DECL_NTPARMS (fn);
24080 tree targs = make_tree_vec (ntparms);
24081 tree decl_type = TREE_TYPE (decl);
24082 tree decl_arg_types;
24083 tree *args;
24084 unsigned int nargs, ix;
24085 tree arg;
24086
24087 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24088
24089 /* Never do unification on the 'this' parameter. */
24090 decl_arg_types = skip_artificial_parms_for (decl,
24091 TYPE_ARG_TYPES (decl_type));
24092
24093 nargs = list_length (decl_arg_types);
24094 args = XALLOCAVEC (tree, nargs);
24095 for (arg = decl_arg_types, ix = 0;
24096 arg != NULL_TREE && arg != void_list_node;
24097 arg = TREE_CHAIN (arg), ++ix)
24098 args[ix] = TREE_VALUE (arg);
24099
24100 if (fn_type_unification (fn, explicit_args, targs,
24101 args, ix,
24102 (check_rettype || DECL_CONV_FN_P (fn)
24103 ? TREE_TYPE (decl_type) : NULL_TREE),
24104 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24105 /*explain_p=*/false,
24106 /*decltype*/false)
24107 == error_mark_node)
24108 return NULL_TREE;
24109
24110 return targs;
24111 }
24112
24113 /* Return the innermost template arguments that, when applied to a partial
24114 specialization SPEC_TMPL of TMPL, yield the ARGS.
24115
24116 For example, suppose we have:
24117
24118 template <class T, class U> struct S {};
24119 template <class T> struct S<T*, int> {};
24120
24121 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24122 partial specialization and the ARGS will be {double*, int}. The resulting
24123 vector will be {double}, indicating that `T' is bound to `double'. */
24124
24125 static tree
24126 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24127 {
24128 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24129 tree spec_args
24130 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24131 int i, ntparms = TREE_VEC_LENGTH (tparms);
24132 tree deduced_args;
24133 tree innermost_deduced_args;
24134
24135 innermost_deduced_args = make_tree_vec (ntparms);
24136 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24137 {
24138 deduced_args = copy_node (args);
24139 SET_TMPL_ARGS_LEVEL (deduced_args,
24140 TMPL_ARGS_DEPTH (deduced_args),
24141 innermost_deduced_args);
24142 }
24143 else
24144 deduced_args = innermost_deduced_args;
24145
24146 bool tried_array_deduction = (cxx_dialect < cxx17);
24147 again:
24148 if (unify (tparms, deduced_args,
24149 INNERMOST_TEMPLATE_ARGS (spec_args),
24150 INNERMOST_TEMPLATE_ARGS (args),
24151 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24152 return NULL_TREE;
24153
24154 for (i = 0; i < ntparms; ++i)
24155 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24156 {
24157 if (!tried_array_deduction)
24158 {
24159 try_array_deduction (tparms, innermost_deduced_args,
24160 INNERMOST_TEMPLATE_ARGS (spec_args));
24161 tried_array_deduction = true;
24162 if (TREE_VEC_ELT (innermost_deduced_args, i))
24163 goto again;
24164 }
24165 return NULL_TREE;
24166 }
24167
24168 if (!push_tinst_level (spec_tmpl, deduced_args))
24169 {
24170 excessive_deduction_depth = true;
24171 return NULL_TREE;
24172 }
24173
24174 /* Verify that nondeduced template arguments agree with the type
24175 obtained from argument deduction.
24176
24177 For example:
24178
24179 struct A { typedef int X; };
24180 template <class T, class U> struct C {};
24181 template <class T> struct C<T, typename T::X> {};
24182
24183 Then with the instantiation `C<A, int>', we can deduce that
24184 `T' is `A' but unify () does not check whether `typename T::X'
24185 is `int'. */
24186 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24187
24188 if (spec_args != error_mark_node)
24189 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24190 INNERMOST_TEMPLATE_ARGS (spec_args),
24191 tmpl, tf_none, false, false);
24192
24193 pop_tinst_level ();
24194
24195 if (spec_args == error_mark_node
24196 /* We only need to check the innermost arguments; the other
24197 arguments will always agree. */
24198 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24199 INNERMOST_TEMPLATE_ARGS (args)))
24200 return NULL_TREE;
24201
24202 /* Now that we have bindings for all of the template arguments,
24203 ensure that the arguments deduced for the template template
24204 parameters have compatible template parameter lists. See the use
24205 of template_template_parm_bindings_ok_p in fn_type_unification
24206 for more information. */
24207 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24208 return NULL_TREE;
24209
24210 return deduced_args;
24211 }
24212
24213 // Compare two function templates T1 and T2 by deducing bindings
24214 // from one against the other. If both deductions succeed, compare
24215 // constraints to see which is more constrained.
24216 static int
24217 more_specialized_inst (tree t1, tree t2)
24218 {
24219 int fate = 0;
24220 int count = 0;
24221
24222 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24223 {
24224 --fate;
24225 ++count;
24226 }
24227
24228 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24229 {
24230 ++fate;
24231 ++count;
24232 }
24233
24234 // If both deductions succeed, then one may be more constrained.
24235 if (count == 2 && fate == 0)
24236 fate = more_constrained (t1, t2);
24237
24238 return fate;
24239 }
24240
24241 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24242 Return the TREE_LIST node with the most specialized template, if
24243 any. If there is no most specialized template, the error_mark_node
24244 is returned.
24245
24246 Note that this function does not look at, or modify, the
24247 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24248 returned is one of the elements of INSTANTIATIONS, callers may
24249 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24250 and retrieve it from the value returned. */
24251
24252 tree
24253 most_specialized_instantiation (tree templates)
24254 {
24255 tree fn, champ;
24256
24257 ++processing_template_decl;
24258
24259 champ = templates;
24260 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24261 {
24262 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24263 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24264 if (fate == -1)
24265 champ = fn;
24266 else if (!fate)
24267 {
24268 /* Equally specialized, move to next function. If there
24269 is no next function, nothing's most specialized. */
24270 fn = TREE_CHAIN (fn);
24271 champ = fn;
24272 if (!fn)
24273 break;
24274 }
24275 }
24276
24277 if (champ)
24278 /* Now verify that champ is better than everything earlier in the
24279 instantiation list. */
24280 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24281 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24282 {
24283 champ = NULL_TREE;
24284 break;
24285 }
24286 }
24287
24288 processing_template_decl--;
24289
24290 if (!champ)
24291 return error_mark_node;
24292
24293 return champ;
24294 }
24295
24296 /* If DECL is a specialization of some template, return the most
24297 general such template. Otherwise, returns NULL_TREE.
24298
24299 For example, given:
24300
24301 template <class T> struct S { template <class U> void f(U); };
24302
24303 if TMPL is `template <class U> void S<int>::f(U)' this will return
24304 the full template. This function will not trace past partial
24305 specializations, however. For example, given in addition:
24306
24307 template <class T> struct S<T*> { template <class U> void f(U); };
24308
24309 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24310 `template <class T> template <class U> S<T*>::f(U)'. */
24311
24312 tree
24313 most_general_template (tree decl)
24314 {
24315 if (TREE_CODE (decl) != TEMPLATE_DECL)
24316 {
24317 if (tree tinfo = get_template_info (decl))
24318 decl = TI_TEMPLATE (tinfo);
24319 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24320 template friend, or a FIELD_DECL for a capture pack. */
24321 if (TREE_CODE (decl) != TEMPLATE_DECL)
24322 return NULL_TREE;
24323 }
24324
24325 /* Look for more and more general templates. */
24326 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24327 {
24328 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24329 (See cp-tree.h for details.) */
24330 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24331 break;
24332
24333 if (CLASS_TYPE_P (TREE_TYPE (decl))
24334 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24335 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24336 break;
24337
24338 /* Stop if we run into an explicitly specialized class template. */
24339 if (!DECL_NAMESPACE_SCOPE_P (decl)
24340 && DECL_CONTEXT (decl)
24341 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24342 break;
24343
24344 decl = DECL_TI_TEMPLATE (decl);
24345 }
24346
24347 return decl;
24348 }
24349
24350 /* Return the most specialized of the template partial specializations
24351 which can produce TARGET, a specialization of some class or variable
24352 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24353 a TEMPLATE_DECL node corresponding to the partial specialization, while
24354 the TREE_PURPOSE is the set of template arguments that must be
24355 substituted into the template pattern in order to generate TARGET.
24356
24357 If the choice of partial specialization is ambiguous, a diagnostic
24358 is issued, and the error_mark_node is returned. If there are no
24359 partial specializations matching TARGET, then NULL_TREE is
24360 returned, indicating that the primary template should be used. */
24361
24362 tree
24363 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24364 {
24365 tree list = NULL_TREE;
24366 tree t;
24367 tree champ;
24368 int fate;
24369 bool ambiguous_p;
24370 tree outer_args = NULL_TREE;
24371 tree tmpl, args;
24372
24373 if (TYPE_P (target))
24374 {
24375 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24376 tmpl = TI_TEMPLATE (tinfo);
24377 args = TI_ARGS (tinfo);
24378 }
24379 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24380 {
24381 tmpl = TREE_OPERAND (target, 0);
24382 args = TREE_OPERAND (target, 1);
24383 }
24384 else if (VAR_P (target))
24385 {
24386 tree tinfo = DECL_TEMPLATE_INFO (target);
24387 tmpl = TI_TEMPLATE (tinfo);
24388 args = TI_ARGS (tinfo);
24389 }
24390 else
24391 gcc_unreachable ();
24392
24393 tree main_tmpl = most_general_template (tmpl);
24394
24395 /* For determining which partial specialization to use, only the
24396 innermost args are interesting. */
24397 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24398 {
24399 outer_args = strip_innermost_template_args (args, 1);
24400 args = INNERMOST_TEMPLATE_ARGS (args);
24401 }
24402
24403 /* The caller hasn't called push_to_top_level yet, but we need
24404 get_partial_spec_bindings to be done in non-template context so that we'll
24405 fully resolve everything. */
24406 processing_template_decl_sentinel ptds;
24407
24408 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24409 {
24410 tree spec_args;
24411 tree spec_tmpl = TREE_VALUE (t);
24412
24413 if (outer_args)
24414 {
24415 /* Substitute in the template args from the enclosing class. */
24416 ++processing_template_decl;
24417 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24418 --processing_template_decl;
24419 }
24420
24421 if (spec_tmpl == error_mark_node)
24422 return error_mark_node;
24423
24424 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24425 if (spec_args)
24426 {
24427 if (outer_args)
24428 spec_args = add_to_template_args (outer_args, spec_args);
24429
24430 /* Keep the candidate only if the constraints are satisfied,
24431 or if we're not compiling with concepts. */
24432 if (!flag_concepts
24433 || constraints_satisfied_p (spec_tmpl, spec_args))
24434 {
24435 list = tree_cons (spec_args, TREE_VALUE (t), list);
24436 TREE_TYPE (list) = TREE_TYPE (t);
24437 }
24438 }
24439 }
24440
24441 if (! list)
24442 return NULL_TREE;
24443
24444 ambiguous_p = false;
24445 t = list;
24446 champ = t;
24447 t = TREE_CHAIN (t);
24448 for (; t; t = TREE_CHAIN (t))
24449 {
24450 fate = more_specialized_partial_spec (tmpl, champ, t);
24451 if (fate == 1)
24452 ;
24453 else
24454 {
24455 if (fate == 0)
24456 {
24457 t = TREE_CHAIN (t);
24458 if (! t)
24459 {
24460 ambiguous_p = true;
24461 break;
24462 }
24463 }
24464 champ = t;
24465 }
24466 }
24467
24468 if (!ambiguous_p)
24469 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24470 {
24471 fate = more_specialized_partial_spec (tmpl, champ, t);
24472 if (fate != 1)
24473 {
24474 ambiguous_p = true;
24475 break;
24476 }
24477 }
24478
24479 if (ambiguous_p)
24480 {
24481 const char *str;
24482 char *spaces = NULL;
24483 if (!(complain & tf_error))
24484 return error_mark_node;
24485 if (TYPE_P (target))
24486 error ("ambiguous template instantiation for %q#T", target);
24487 else
24488 error ("ambiguous template instantiation for %q#D", target);
24489 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24490 for (t = list; t; t = TREE_CHAIN (t))
24491 {
24492 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24493 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24494 "%s %#qS", spaces ? spaces : str, subst);
24495 spaces = spaces ? spaces : get_spaces (str);
24496 }
24497 free (spaces);
24498 return error_mark_node;
24499 }
24500
24501 return champ;
24502 }
24503
24504 /* Explicitly instantiate DECL. */
24505
24506 void
24507 do_decl_instantiation (tree decl, tree storage)
24508 {
24509 tree result = NULL_TREE;
24510 int extern_p = 0;
24511
24512 if (!decl || decl == error_mark_node)
24513 /* An error occurred, for which grokdeclarator has already issued
24514 an appropriate message. */
24515 return;
24516 else if (! DECL_LANG_SPECIFIC (decl))
24517 {
24518 error ("explicit instantiation of non-template %q#D", decl);
24519 return;
24520 }
24521 else if (DECL_DECLARED_CONCEPT_P (decl))
24522 {
24523 if (VAR_P (decl))
24524 error ("explicit instantiation of variable concept %q#D", decl);
24525 else
24526 error ("explicit instantiation of function concept %q#D", decl);
24527 return;
24528 }
24529
24530 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24531 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24532
24533 if (VAR_P (decl) && !var_templ)
24534 {
24535 /* There is an asymmetry here in the way VAR_DECLs and
24536 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24537 the latter, the DECL we get back will be marked as a
24538 template instantiation, and the appropriate
24539 DECL_TEMPLATE_INFO will be set up. This does not happen for
24540 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24541 should handle VAR_DECLs as it currently handles
24542 FUNCTION_DECLs. */
24543 if (!DECL_CLASS_SCOPE_P (decl))
24544 {
24545 error ("%qD is not a static data member of a class template", decl);
24546 return;
24547 }
24548 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24549 if (!result || !VAR_P (result))
24550 {
24551 error ("no matching template for %qD found", decl);
24552 return;
24553 }
24554 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24555 {
24556 error ("type %qT for explicit instantiation %qD does not match "
24557 "declared type %qT", TREE_TYPE (result), decl,
24558 TREE_TYPE (decl));
24559 return;
24560 }
24561 }
24562 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24563 {
24564 error ("explicit instantiation of %q#D", decl);
24565 return;
24566 }
24567 else
24568 result = decl;
24569
24570 /* Check for various error cases. Note that if the explicit
24571 instantiation is valid the RESULT will currently be marked as an
24572 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24573 until we get here. */
24574
24575 if (DECL_TEMPLATE_SPECIALIZATION (result))
24576 {
24577 /* DR 259 [temp.spec].
24578
24579 Both an explicit instantiation and a declaration of an explicit
24580 specialization shall not appear in a program unless the explicit
24581 instantiation follows a declaration of the explicit specialization.
24582
24583 For a given set of template parameters, if an explicit
24584 instantiation of a template appears after a declaration of an
24585 explicit specialization for that template, the explicit
24586 instantiation has no effect. */
24587 return;
24588 }
24589 else if (DECL_EXPLICIT_INSTANTIATION (result))
24590 {
24591 /* [temp.spec]
24592
24593 No program shall explicitly instantiate any template more
24594 than once.
24595
24596 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24597 the first instantiation was `extern' and the second is not,
24598 and EXTERN_P for the opposite case. */
24599 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24600 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24601 /* If an "extern" explicit instantiation follows an ordinary
24602 explicit instantiation, the template is instantiated. */
24603 if (extern_p)
24604 return;
24605 }
24606 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24607 {
24608 error ("no matching template for %qD found", result);
24609 return;
24610 }
24611 else if (!DECL_TEMPLATE_INFO (result))
24612 {
24613 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24614 return;
24615 }
24616
24617 if (storage == NULL_TREE)
24618 ;
24619 else if (storage == ridpointers[(int) RID_EXTERN])
24620 {
24621 if (cxx_dialect == cxx98)
24622 pedwarn (input_location, OPT_Wpedantic,
24623 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24624 "instantiations");
24625 extern_p = 1;
24626 }
24627 else
24628 error ("storage class %qD applied to template instantiation", storage);
24629
24630 check_explicit_instantiation_namespace (result);
24631 mark_decl_instantiated (result, extern_p);
24632 if (! extern_p)
24633 instantiate_decl (result, /*defer_ok=*/true,
24634 /*expl_inst_class_mem_p=*/false);
24635 }
24636
24637 static void
24638 mark_class_instantiated (tree t, int extern_p)
24639 {
24640 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24641 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24642 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24643 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24644 if (! extern_p)
24645 {
24646 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24647 rest_of_type_compilation (t, 1);
24648 }
24649 }
24650
24651 /* Called from do_type_instantiation through binding_table_foreach to
24652 do recursive instantiation for the type bound in ENTRY. */
24653 static void
24654 bt_instantiate_type_proc (binding_entry entry, void *data)
24655 {
24656 tree storage = *(tree *) data;
24657
24658 if (MAYBE_CLASS_TYPE_P (entry->type)
24659 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24660 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24661 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24662 }
24663
24664 /* Perform an explicit instantiation of template class T. STORAGE, if
24665 non-null, is the RID for extern, inline or static. COMPLAIN is
24666 nonzero if this is called from the parser, zero if called recursively,
24667 since the standard is unclear (as detailed below). */
24668
24669 void
24670 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24671 {
24672 int extern_p = 0;
24673 int nomem_p = 0;
24674 int static_p = 0;
24675 int previous_instantiation_extern_p = 0;
24676
24677 if (TREE_CODE (t) == TYPE_DECL)
24678 t = TREE_TYPE (t);
24679
24680 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24681 {
24682 tree tmpl =
24683 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24684 if (tmpl)
24685 error ("explicit instantiation of non-class template %qD", tmpl);
24686 else
24687 error ("explicit instantiation of non-template type %qT", t);
24688 return;
24689 }
24690
24691 complete_type (t);
24692
24693 if (!COMPLETE_TYPE_P (t))
24694 {
24695 if (complain & tf_error)
24696 error ("explicit instantiation of %q#T before definition of template",
24697 t);
24698 return;
24699 }
24700
24701 if (storage != NULL_TREE)
24702 {
24703 if (storage == ridpointers[(int) RID_EXTERN])
24704 {
24705 if (cxx_dialect == cxx98)
24706 pedwarn (input_location, OPT_Wpedantic,
24707 "ISO C++ 1998 forbids the use of %<extern%> on "
24708 "explicit instantiations");
24709 }
24710 else
24711 pedwarn (input_location, OPT_Wpedantic,
24712 "ISO C++ forbids the use of %qE"
24713 " on explicit instantiations", storage);
24714
24715 if (storage == ridpointers[(int) RID_INLINE])
24716 nomem_p = 1;
24717 else if (storage == ridpointers[(int) RID_EXTERN])
24718 extern_p = 1;
24719 else if (storage == ridpointers[(int) RID_STATIC])
24720 static_p = 1;
24721 else
24722 {
24723 error ("storage class %qD applied to template instantiation",
24724 storage);
24725 extern_p = 0;
24726 }
24727 }
24728
24729 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24730 {
24731 /* DR 259 [temp.spec].
24732
24733 Both an explicit instantiation and a declaration of an explicit
24734 specialization shall not appear in a program unless the explicit
24735 instantiation follows a declaration of the explicit specialization.
24736
24737 For a given set of template parameters, if an explicit
24738 instantiation of a template appears after a declaration of an
24739 explicit specialization for that template, the explicit
24740 instantiation has no effect. */
24741 return;
24742 }
24743 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24744 {
24745 /* [temp.spec]
24746
24747 No program shall explicitly instantiate any template more
24748 than once.
24749
24750 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24751 instantiation was `extern'. If EXTERN_P then the second is.
24752 These cases are OK. */
24753 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24754
24755 if (!previous_instantiation_extern_p && !extern_p
24756 && (complain & tf_error))
24757 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24758
24759 /* If we've already instantiated the template, just return now. */
24760 if (!CLASSTYPE_INTERFACE_ONLY (t))
24761 return;
24762 }
24763
24764 check_explicit_instantiation_namespace (TYPE_NAME (t));
24765 mark_class_instantiated (t, extern_p);
24766
24767 if (nomem_p)
24768 return;
24769
24770 /* In contrast to implicit instantiation, where only the
24771 declarations, and not the definitions, of members are
24772 instantiated, we have here:
24773
24774 [temp.explicit]
24775
24776 The explicit instantiation of a class template specialization
24777 implies the instantiation of all of its members not
24778 previously explicitly specialized in the translation unit
24779 containing the explicit instantiation.
24780
24781 Of course, we can't instantiate member template classes, since we
24782 don't have any arguments for them. Note that the standard is
24783 unclear on whether the instantiation of the members are
24784 *explicit* instantiations or not. However, the most natural
24785 interpretation is that it should be an explicit
24786 instantiation. */
24787 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24788 if ((VAR_P (fld)
24789 || (TREE_CODE (fld) == FUNCTION_DECL
24790 && !static_p
24791 && user_provided_p (fld)))
24792 && DECL_TEMPLATE_INSTANTIATION (fld))
24793 {
24794 mark_decl_instantiated (fld, extern_p);
24795 if (! extern_p)
24796 instantiate_decl (fld, /*defer_ok=*/true,
24797 /*expl_inst_class_mem_p=*/true);
24798 }
24799
24800 if (CLASSTYPE_NESTED_UTDS (t))
24801 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24802 bt_instantiate_type_proc, &storage);
24803 }
24804
24805 /* Given a function DECL, which is a specialization of TMPL, modify
24806 DECL to be a re-instantiation of TMPL with the same template
24807 arguments. TMPL should be the template into which tsubst'ing
24808 should occur for DECL, not the most general template.
24809
24810 One reason for doing this is a scenario like this:
24811
24812 template <class T>
24813 void f(const T&, int i);
24814
24815 void g() { f(3, 7); }
24816
24817 template <class T>
24818 void f(const T& t, const int i) { }
24819
24820 Note that when the template is first instantiated, with
24821 instantiate_template, the resulting DECL will have no name for the
24822 first parameter, and the wrong type for the second. So, when we go
24823 to instantiate the DECL, we regenerate it. */
24824
24825 static void
24826 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24827 {
24828 /* The arguments used to instantiate DECL, from the most general
24829 template. */
24830 tree code_pattern;
24831
24832 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24833
24834 /* Make sure that we can see identifiers, and compute access
24835 correctly. */
24836 push_access_scope (decl);
24837
24838 if (TREE_CODE (decl) == FUNCTION_DECL)
24839 {
24840 tree decl_parm;
24841 tree pattern_parm;
24842 tree specs;
24843 int args_depth;
24844 int parms_depth;
24845
24846 args_depth = TMPL_ARGS_DEPTH (args);
24847 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24848 if (args_depth > parms_depth)
24849 args = get_innermost_template_args (args, parms_depth);
24850
24851 /* Instantiate a dynamic exception-specification. noexcept will be
24852 handled below. */
24853 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24854 if (TREE_VALUE (raises))
24855 {
24856 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24857 args, tf_error, NULL_TREE,
24858 /*defer_ok*/false);
24859 if (specs && specs != error_mark_node)
24860 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24861 specs);
24862 }
24863
24864 /* Merge parameter declarations. */
24865 decl_parm = skip_artificial_parms_for (decl,
24866 DECL_ARGUMENTS (decl));
24867 pattern_parm
24868 = skip_artificial_parms_for (code_pattern,
24869 DECL_ARGUMENTS (code_pattern));
24870 while (decl_parm && !DECL_PACK_P (pattern_parm))
24871 {
24872 tree parm_type;
24873 tree attributes;
24874
24875 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24876 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24877 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24878 NULL_TREE);
24879 parm_type = type_decays_to (parm_type);
24880 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24881 TREE_TYPE (decl_parm) = parm_type;
24882 attributes = DECL_ATTRIBUTES (pattern_parm);
24883 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24884 {
24885 DECL_ATTRIBUTES (decl_parm) = attributes;
24886 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24887 }
24888 decl_parm = DECL_CHAIN (decl_parm);
24889 pattern_parm = DECL_CHAIN (pattern_parm);
24890 }
24891 /* Merge any parameters that match with the function parameter
24892 pack. */
24893 if (pattern_parm && DECL_PACK_P (pattern_parm))
24894 {
24895 int i, len;
24896 tree expanded_types;
24897 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24898 the parameters in this function parameter pack. */
24899 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24900 args, tf_error, NULL_TREE);
24901 len = TREE_VEC_LENGTH (expanded_types);
24902 for (i = 0; i < len; i++)
24903 {
24904 tree parm_type;
24905 tree attributes;
24906
24907 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24908 /* Rename the parameter to include the index. */
24909 DECL_NAME (decl_parm) =
24910 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24911 parm_type = TREE_VEC_ELT (expanded_types, i);
24912 parm_type = type_decays_to (parm_type);
24913 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24914 TREE_TYPE (decl_parm) = parm_type;
24915 attributes = DECL_ATTRIBUTES (pattern_parm);
24916 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24917 {
24918 DECL_ATTRIBUTES (decl_parm) = attributes;
24919 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24920 }
24921 decl_parm = DECL_CHAIN (decl_parm);
24922 }
24923 }
24924 /* Merge additional specifiers from the CODE_PATTERN. */
24925 if (DECL_DECLARED_INLINE_P (code_pattern)
24926 && !DECL_DECLARED_INLINE_P (decl))
24927 DECL_DECLARED_INLINE_P (decl) = 1;
24928
24929 maybe_instantiate_noexcept (decl, tf_error);
24930 }
24931 else if (VAR_P (decl))
24932 {
24933 start_lambda_scope (decl);
24934 DECL_INITIAL (decl) =
24935 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24936 tf_error, DECL_TI_TEMPLATE (decl));
24937 finish_lambda_scope ();
24938 if (VAR_HAD_UNKNOWN_BOUND (decl))
24939 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24940 tf_error, DECL_TI_TEMPLATE (decl));
24941 }
24942 else
24943 gcc_unreachable ();
24944
24945 pop_access_scope (decl);
24946 }
24947
24948 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24949 substituted to get DECL. */
24950
24951 tree
24952 template_for_substitution (tree decl)
24953 {
24954 tree tmpl = DECL_TI_TEMPLATE (decl);
24955
24956 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24957 for the instantiation. This is not always the most general
24958 template. Consider, for example:
24959
24960 template <class T>
24961 struct S { template <class U> void f();
24962 template <> void f<int>(); };
24963
24964 and an instantiation of S<double>::f<int>. We want TD to be the
24965 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24966 while (/* An instantiation cannot have a definition, so we need a
24967 more general template. */
24968 DECL_TEMPLATE_INSTANTIATION (tmpl)
24969 /* We must also deal with friend templates. Given:
24970
24971 template <class T> struct S {
24972 template <class U> friend void f() {};
24973 };
24974
24975 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24976 so far as the language is concerned, but that's still
24977 where we get the pattern for the instantiation from. On
24978 other hand, if the definition comes outside the class, say:
24979
24980 template <class T> struct S {
24981 template <class U> friend void f();
24982 };
24983 template <class U> friend void f() {}
24984
24985 we don't need to look any further. That's what the check for
24986 DECL_INITIAL is for. */
24987 || (TREE_CODE (decl) == FUNCTION_DECL
24988 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24989 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24990 {
24991 /* The present template, TD, should not be a definition. If it
24992 were a definition, we should be using it! Note that we
24993 cannot restructure the loop to just keep going until we find
24994 a template with a definition, since that might go too far if
24995 a specialization was declared, but not defined. */
24996
24997 /* Fetch the more general template. */
24998 tmpl = DECL_TI_TEMPLATE (tmpl);
24999 }
25000
25001 return tmpl;
25002 }
25003
25004 /* Returns true if we need to instantiate this template instance even if we
25005 know we aren't going to emit it. */
25006
25007 bool
25008 always_instantiate_p (tree decl)
25009 {
25010 /* We always instantiate inline functions so that we can inline them. An
25011 explicit instantiation declaration prohibits implicit instantiation of
25012 non-inline functions. With high levels of optimization, we would
25013 normally inline non-inline functions -- but we're not allowed to do
25014 that for "extern template" functions. Therefore, we check
25015 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25016 return ((TREE_CODE (decl) == FUNCTION_DECL
25017 && (DECL_DECLARED_INLINE_P (decl)
25018 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25019 /* And we need to instantiate static data members so that
25020 their initializers are available in integral constant
25021 expressions. */
25022 || (VAR_P (decl)
25023 && decl_maybe_constant_var_p (decl)));
25024 }
25025
25026 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25027 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25028 error, true otherwise. */
25029
25030 bool
25031 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25032 {
25033 tree fntype, spec, noex, clone;
25034
25035 /* Don't instantiate a noexcept-specification from template context. */
25036 if (processing_template_decl
25037 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25038 return true;
25039
25040 if (DECL_CLONED_FUNCTION_P (fn))
25041 fn = DECL_CLONED_FUNCTION (fn);
25042
25043 tree orig_fn = NULL_TREE;
25044 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25045 its FUNCTION_DECL for the rest of this function -- push_access_scope
25046 doesn't accept TEMPLATE_DECLs. */
25047 if (DECL_FUNCTION_TEMPLATE_P (fn))
25048 {
25049 orig_fn = fn;
25050 fn = DECL_TEMPLATE_RESULT (fn);
25051 }
25052
25053 fntype = TREE_TYPE (fn);
25054 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25055
25056 if (!spec || !TREE_PURPOSE (spec))
25057 return true;
25058
25059 noex = TREE_PURPOSE (spec);
25060
25061 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25062 {
25063 static hash_set<tree>* fns = new hash_set<tree>;
25064 bool added = false;
25065 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25066 {
25067 spec = get_defaulted_eh_spec (fn, complain);
25068 if (spec == error_mark_node)
25069 /* This might have failed because of an unparsed DMI, so
25070 let's try again later. */
25071 return false;
25072 }
25073 else if (!(added = !fns->add (fn)))
25074 {
25075 /* If hash_set::add returns true, the element was already there. */
25076 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25077 DECL_SOURCE_LOCATION (fn));
25078 error_at (loc,
25079 "exception specification of %qD depends on itself",
25080 fn);
25081 spec = noexcept_false_spec;
25082 }
25083 else if (push_tinst_level (fn))
25084 {
25085 push_to_top_level ();
25086 push_access_scope (fn);
25087 push_deferring_access_checks (dk_no_deferred);
25088 input_location = DECL_SOURCE_LOCATION (fn);
25089
25090 /* If needed, set current_class_ptr for the benefit of
25091 tsubst_copy/PARM_DECL. */
25092 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25093 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25094 {
25095 tree this_parm = DECL_ARGUMENTS (tdecl);
25096 current_class_ptr = NULL_TREE;
25097 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25098 current_class_ptr = this_parm;
25099 }
25100
25101 /* If this function is represented by a TEMPLATE_DECL, then
25102 the deferred noexcept-specification might still contain
25103 dependent types, even after substitution. And we need the
25104 dependency check functions to work in build_noexcept_spec. */
25105 if (orig_fn)
25106 ++processing_template_decl;
25107
25108 /* Do deferred instantiation of the noexcept-specifier. */
25109 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25110 DEFERRED_NOEXCEPT_ARGS (noex),
25111 tf_warning_or_error, fn,
25112 /*function_p=*/false,
25113 /*i_c_e_p=*/true);
25114
25115 /* Build up the noexcept-specification. */
25116 spec = build_noexcept_spec (noex, tf_warning_or_error);
25117
25118 if (orig_fn)
25119 --processing_template_decl;
25120
25121 pop_deferring_access_checks ();
25122 pop_access_scope (fn);
25123 pop_tinst_level ();
25124 pop_from_top_level ();
25125 }
25126 else
25127 spec = noexcept_false_spec;
25128
25129 if (added)
25130 fns->remove (fn);
25131
25132 if (spec == error_mark_node)
25133 {
25134 /* This failed with a hard error, so let's go with false. */
25135 gcc_assert (seen_error ());
25136 spec = noexcept_false_spec;
25137 }
25138
25139 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25140 if (orig_fn)
25141 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25142 }
25143
25144 FOR_EACH_CLONE (clone, fn)
25145 {
25146 if (TREE_TYPE (clone) == fntype)
25147 TREE_TYPE (clone) = TREE_TYPE (fn);
25148 else
25149 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
25150 }
25151
25152 return true;
25153 }
25154
25155 /* We're starting to process the function INST, an instantiation of PATTERN;
25156 add their parameters to local_specializations. */
25157
25158 static void
25159 register_parameter_specializations (tree pattern, tree inst)
25160 {
25161 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25162 tree spec_parm = DECL_ARGUMENTS (inst);
25163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25164 {
25165 register_local_specialization (spec_parm, tmpl_parm);
25166 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25167 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25168 }
25169 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25170 {
25171 if (!DECL_PACK_P (tmpl_parm))
25172 {
25173 register_local_specialization (spec_parm, tmpl_parm);
25174 spec_parm = DECL_CHAIN (spec_parm);
25175 }
25176 else
25177 {
25178 /* Register the (value) argument pack as a specialization of
25179 TMPL_PARM, then move on. */
25180 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25181 register_local_specialization (argpack, tmpl_parm);
25182 }
25183 }
25184 gcc_assert (!spec_parm);
25185 }
25186
25187 /* Produce the definition of D, a _DECL generated from a template. If
25188 DEFER_OK is true, then we don't have to actually do the
25189 instantiation now; we just have to do it sometime. Normally it is
25190 an error if this is an explicit instantiation but D is undefined.
25191 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25192 instantiated class template. */
25193
25194 tree
25195 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25196 {
25197 tree tmpl = DECL_TI_TEMPLATE (d);
25198 tree gen_args;
25199 tree args;
25200 tree td;
25201 tree code_pattern;
25202 tree spec;
25203 tree gen_tmpl;
25204 bool pattern_defined;
25205 location_t saved_loc = input_location;
25206 int saved_unevaluated_operand = cp_unevaluated_operand;
25207 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25208 bool external_p;
25209 bool deleted_p;
25210
25211 /* This function should only be used to instantiate templates for
25212 functions and static member variables. */
25213 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25214
25215 /* A concept is never instantiated. */
25216 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25217
25218 /* Variables are never deferred; if instantiation is required, they
25219 are instantiated right away. That allows for better code in the
25220 case that an expression refers to the value of the variable --
25221 if the variable has a constant value the referring expression can
25222 take advantage of that fact. */
25223 if (VAR_P (d))
25224 defer_ok = false;
25225
25226 /* Don't instantiate cloned functions. Instead, instantiate the
25227 functions they cloned. */
25228 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25229 d = DECL_CLONED_FUNCTION (d);
25230
25231 if (DECL_TEMPLATE_INSTANTIATED (d)
25232 || (TREE_CODE (d) == FUNCTION_DECL
25233 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25234 || DECL_TEMPLATE_SPECIALIZATION (d))
25235 /* D has already been instantiated or explicitly specialized, so
25236 there's nothing for us to do here.
25237
25238 It might seem reasonable to check whether or not D is an explicit
25239 instantiation, and, if so, stop here. But when an explicit
25240 instantiation is deferred until the end of the compilation,
25241 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25242 the instantiation. */
25243 return d;
25244
25245 /* Check to see whether we know that this template will be
25246 instantiated in some other file, as with "extern template"
25247 extension. */
25248 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25249
25250 /* In general, we do not instantiate such templates. */
25251 if (external_p && !always_instantiate_p (d))
25252 return d;
25253
25254 gen_tmpl = most_general_template (tmpl);
25255 gen_args = DECL_TI_ARGS (d);
25256
25257 if (tmpl != gen_tmpl)
25258 /* We should already have the extra args. */
25259 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25260 == TMPL_ARGS_DEPTH (gen_args));
25261 /* And what's in the hash table should match D. */
25262 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25263 || spec == NULL_TREE);
25264
25265 /* This needs to happen before any tsubsting. */
25266 if (! push_tinst_level (d))
25267 return d;
25268
25269 timevar_push (TV_TEMPLATE_INST);
25270
25271 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25272 for the instantiation. */
25273 td = template_for_substitution (d);
25274 args = gen_args;
25275
25276 if (VAR_P (d))
25277 {
25278 /* Look up an explicit specialization, if any. */
25279 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25280 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25281 if (elt && elt != error_mark_node)
25282 {
25283 td = TREE_VALUE (elt);
25284 args = TREE_PURPOSE (elt);
25285 }
25286 }
25287
25288 code_pattern = DECL_TEMPLATE_RESULT (td);
25289
25290 /* We should never be trying to instantiate a member of a class
25291 template or partial specialization. */
25292 gcc_assert (d != code_pattern);
25293
25294 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25295 || DECL_TEMPLATE_SPECIALIZATION (td))
25296 /* In the case of a friend template whose definition is provided
25297 outside the class, we may have too many arguments. Drop the
25298 ones we don't need. The same is true for specializations. */
25299 args = get_innermost_template_args
25300 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25301
25302 if (TREE_CODE (d) == FUNCTION_DECL)
25303 {
25304 deleted_p = DECL_DELETED_FN (code_pattern);
25305 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25306 && DECL_INITIAL (code_pattern) != error_mark_node)
25307 || DECL_DEFAULTED_FN (code_pattern)
25308 || deleted_p);
25309 }
25310 else
25311 {
25312 deleted_p = false;
25313 if (DECL_CLASS_SCOPE_P (code_pattern))
25314 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25315 else
25316 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25317 }
25318
25319 /* We may be in the middle of deferred access check. Disable it now. */
25320 push_deferring_access_checks (dk_no_deferred);
25321
25322 /* Unless an explicit instantiation directive has already determined
25323 the linkage of D, remember that a definition is available for
25324 this entity. */
25325 if (pattern_defined
25326 && !DECL_INTERFACE_KNOWN (d)
25327 && !DECL_NOT_REALLY_EXTERN (d))
25328 mark_definable (d);
25329
25330 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25331 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25332 input_location = DECL_SOURCE_LOCATION (d);
25333
25334 /* If D is a member of an explicitly instantiated class template,
25335 and no definition is available, treat it like an implicit
25336 instantiation. */
25337 if (!pattern_defined && expl_inst_class_mem_p
25338 && DECL_EXPLICIT_INSTANTIATION (d))
25339 {
25340 /* Leave linkage flags alone on instantiations with anonymous
25341 visibility. */
25342 if (TREE_PUBLIC (d))
25343 {
25344 DECL_NOT_REALLY_EXTERN (d) = 0;
25345 DECL_INTERFACE_KNOWN (d) = 0;
25346 }
25347 SET_DECL_IMPLICIT_INSTANTIATION (d);
25348 }
25349
25350 /* Defer all other templates, unless we have been explicitly
25351 forbidden from doing so. */
25352 if (/* If there is no definition, we cannot instantiate the
25353 template. */
25354 ! pattern_defined
25355 /* If it's OK to postpone instantiation, do so. */
25356 || defer_ok
25357 /* If this is a static data member that will be defined
25358 elsewhere, we don't want to instantiate the entire data
25359 member, but we do want to instantiate the initializer so that
25360 we can substitute that elsewhere. */
25361 || (external_p && VAR_P (d))
25362 /* Handle here a deleted function too, avoid generating
25363 its body (c++/61080). */
25364 || deleted_p)
25365 {
25366 /* The definition of the static data member is now required so
25367 we must substitute the initializer. */
25368 if (VAR_P (d)
25369 && !DECL_INITIAL (d)
25370 && DECL_INITIAL (code_pattern))
25371 {
25372 tree ns;
25373 tree init;
25374 bool const_init = false;
25375 bool enter_context = DECL_CLASS_SCOPE_P (d);
25376
25377 ns = decl_namespace_context (d);
25378 push_nested_namespace (ns);
25379 if (enter_context)
25380 push_nested_class (DECL_CONTEXT (d));
25381 init = tsubst_expr (DECL_INITIAL (code_pattern),
25382 args,
25383 tf_warning_or_error, NULL_TREE,
25384 /*integral_constant_expression_p=*/false);
25385 /* If instantiating the initializer involved instantiating this
25386 again, don't call cp_finish_decl twice. */
25387 if (!DECL_INITIAL (d))
25388 {
25389 /* Make sure the initializer is still constant, in case of
25390 circular dependency (template/instantiate6.C). */
25391 const_init
25392 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25393 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25394 /*asmspec_tree=*/NULL_TREE,
25395 LOOKUP_ONLYCONVERTING);
25396 }
25397 if (enter_context)
25398 pop_nested_class ();
25399 pop_nested_namespace (ns);
25400 }
25401
25402 /* We restore the source position here because it's used by
25403 add_pending_template. */
25404 input_location = saved_loc;
25405
25406 if (at_eof && !pattern_defined
25407 && DECL_EXPLICIT_INSTANTIATION (d)
25408 && DECL_NOT_REALLY_EXTERN (d))
25409 /* [temp.explicit]
25410
25411 The definition of a non-exported function template, a
25412 non-exported member function template, or a non-exported
25413 member function or static data member of a class template
25414 shall be present in every translation unit in which it is
25415 explicitly instantiated. */
25416 permerror (input_location, "explicit instantiation of %qD "
25417 "but no definition available", d);
25418
25419 /* If we're in unevaluated context, we just wanted to get the
25420 constant value; this isn't an odr use, so don't queue
25421 a full instantiation. */
25422 if (cp_unevaluated_operand != 0)
25423 goto out;
25424 /* ??? Historically, we have instantiated inline functions, even
25425 when marked as "extern template". */
25426 if (!(external_p && VAR_P (d)))
25427 add_pending_template (d);
25428 goto out;
25429 }
25430
25431 bool push_to_top, nested;
25432 tree fn_context;
25433 fn_context = decl_function_context (d);
25434 if (LAMBDA_FUNCTION_P (d))
25435 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25436 fn_context = NULL_TREE;
25437 nested = current_function_decl != NULL_TREE;
25438 push_to_top = !(nested && fn_context == current_function_decl);
25439
25440 vec<tree> omp_privatization_save;
25441 if (nested)
25442 save_omp_privatization_clauses (omp_privatization_save);
25443
25444 if (push_to_top)
25445 push_to_top_level ();
25446 else
25447 {
25448 gcc_assert (!processing_template_decl);
25449 push_function_context ();
25450 cp_unevaluated_operand = 0;
25451 c_inhibit_evaluation_warnings = 0;
25452 }
25453
25454 /* Mark D as instantiated so that recursive calls to
25455 instantiate_decl do not try to instantiate it again. */
25456 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25457
25458 /* Regenerate the declaration in case the template has been modified
25459 by a subsequent redeclaration. */
25460 regenerate_decl_from_template (d, td, args);
25461
25462 /* We already set the file and line above. Reset them now in case
25463 they changed as a result of calling regenerate_decl_from_template. */
25464 input_location = DECL_SOURCE_LOCATION (d);
25465
25466 if (VAR_P (d))
25467 {
25468 tree init;
25469 bool const_init = false;
25470
25471 /* Clear out DECL_RTL; whatever was there before may not be right
25472 since we've reset the type of the declaration. */
25473 SET_DECL_RTL (d, NULL);
25474 DECL_IN_AGGR_P (d) = 0;
25475
25476 /* The initializer is placed in DECL_INITIAL by
25477 regenerate_decl_from_template so we don't need to
25478 push/pop_access_scope again here. Pull it out so that
25479 cp_finish_decl can process it. */
25480 init = DECL_INITIAL (d);
25481 DECL_INITIAL (d) = NULL_TREE;
25482 DECL_INITIALIZED_P (d) = 0;
25483
25484 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25485 initializer. That function will defer actual emission until
25486 we have a chance to determine linkage. */
25487 DECL_EXTERNAL (d) = 0;
25488
25489 /* Enter the scope of D so that access-checking works correctly. */
25490 bool enter_context = DECL_CLASS_SCOPE_P (d);
25491 if (enter_context)
25492 push_nested_class (DECL_CONTEXT (d));
25493
25494 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25495 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25496 ? LOOKUP_CONSTINIT : 0);
25497 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25498
25499 if (enter_context)
25500 pop_nested_class ();
25501
25502 if (variable_template_p (gen_tmpl))
25503 note_variable_template_instantiation (d);
25504 }
25505 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25506 synthesize_method (d);
25507 else if (TREE_CODE (d) == FUNCTION_DECL)
25508 {
25509 /* Set up the list of local specializations. */
25510 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25511 tree block = NULL_TREE;
25512
25513 /* Set up context. */
25514 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25515 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25516 block = push_stmt_list ();
25517 else
25518 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25519
25520 /* Some typedefs referenced from within the template code need to be
25521 access checked at template instantiation time, i.e now. These
25522 types were added to the template at parsing time. Let's get those
25523 and perform the access checks then. */
25524 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25525 args);
25526
25527 /* Create substitution entries for the parameters. */
25528 register_parameter_specializations (code_pattern, d);
25529
25530 /* Substitute into the body of the function. */
25531 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25532 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25533 tf_warning_or_error, tmpl);
25534 else
25535 {
25536 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25537 tf_warning_or_error, tmpl,
25538 /*integral_constant_expression_p=*/false);
25539
25540 /* Set the current input_location to the end of the function
25541 so that finish_function knows where we are. */
25542 input_location
25543 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25544
25545 /* Remember if we saw an infinite loop in the template. */
25546 current_function_infinite_loop
25547 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25548 }
25549
25550 /* Finish the function. */
25551 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25552 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25553 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25554 else
25555 {
25556 d = finish_function (/*inline_p=*/false);
25557 expand_or_defer_fn (d);
25558 }
25559
25560 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25561 cp_check_omp_declare_reduction (d);
25562 }
25563
25564 /* We're not deferring instantiation any more. */
25565 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25566
25567 if (push_to_top)
25568 pop_from_top_level ();
25569 else
25570 pop_function_context ();
25571
25572 if (nested)
25573 restore_omp_privatization_clauses (omp_privatization_save);
25574
25575 out:
25576 pop_deferring_access_checks ();
25577 timevar_pop (TV_TEMPLATE_INST);
25578 pop_tinst_level ();
25579 input_location = saved_loc;
25580 cp_unevaluated_operand = saved_unevaluated_operand;
25581 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25582
25583 return d;
25584 }
25585
25586 /* Run through the list of templates that we wish we could
25587 instantiate, and instantiate any we can. RETRIES is the
25588 number of times we retry pending template instantiation. */
25589
25590 void
25591 instantiate_pending_templates (int retries)
25592 {
25593 int reconsider;
25594 location_t saved_loc = input_location;
25595
25596 /* Instantiating templates may trigger vtable generation. This in turn
25597 may require further template instantiations. We place a limit here
25598 to avoid infinite loop. */
25599 if (pending_templates && retries >= max_tinst_depth)
25600 {
25601 tree decl = pending_templates->tinst->maybe_get_node ();
25602
25603 fatal_error (input_location,
25604 "template instantiation depth exceeds maximum of %d"
25605 " instantiating %q+D, possibly from virtual table generation"
25606 " (use %<-ftemplate-depth=%> to increase the maximum)",
25607 max_tinst_depth, decl);
25608 if (TREE_CODE (decl) == FUNCTION_DECL)
25609 /* Pretend that we defined it. */
25610 DECL_INITIAL (decl) = error_mark_node;
25611 return;
25612 }
25613
25614 do
25615 {
25616 struct pending_template **t = &pending_templates;
25617 struct pending_template *last = NULL;
25618 reconsider = 0;
25619 while (*t)
25620 {
25621 tree instantiation = reopen_tinst_level ((*t)->tinst);
25622 bool complete = false;
25623
25624 if (TYPE_P (instantiation))
25625 {
25626 if (!COMPLETE_TYPE_P (instantiation))
25627 {
25628 instantiate_class_template (instantiation);
25629 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25630 for (tree fld = TYPE_FIELDS (instantiation);
25631 fld; fld = TREE_CHAIN (fld))
25632 if ((VAR_P (fld)
25633 || (TREE_CODE (fld) == FUNCTION_DECL
25634 && !DECL_ARTIFICIAL (fld)))
25635 && DECL_TEMPLATE_INSTANTIATION (fld))
25636 instantiate_decl (fld,
25637 /*defer_ok=*/false,
25638 /*expl_inst_class_mem_p=*/false);
25639
25640 if (COMPLETE_TYPE_P (instantiation))
25641 reconsider = 1;
25642 }
25643
25644 complete = COMPLETE_TYPE_P (instantiation);
25645 }
25646 else
25647 {
25648 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25649 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25650 {
25651 instantiation
25652 = instantiate_decl (instantiation,
25653 /*defer_ok=*/false,
25654 /*expl_inst_class_mem_p=*/false);
25655 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25656 reconsider = 1;
25657 }
25658
25659 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25660 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25661 }
25662
25663 if (complete)
25664 {
25665 /* If INSTANTIATION has been instantiated, then we don't
25666 need to consider it again in the future. */
25667 struct pending_template *drop = *t;
25668 *t = (*t)->next;
25669 set_refcount_ptr (drop->tinst);
25670 pending_template_freelist ().free (drop);
25671 }
25672 else
25673 {
25674 last = *t;
25675 t = &(*t)->next;
25676 }
25677 tinst_depth = 0;
25678 set_refcount_ptr (current_tinst_level);
25679 }
25680 last_pending_template = last;
25681 }
25682 while (reconsider);
25683
25684 input_location = saved_loc;
25685 }
25686
25687 /* Substitute ARGVEC into T, which is a list of initializers for
25688 either base class or a non-static data member. The TREE_PURPOSEs
25689 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25690 instantiate_decl. */
25691
25692 static tree
25693 tsubst_initializer_list (tree t, tree argvec)
25694 {
25695 tree inits = NULL_TREE;
25696 tree target_ctor = error_mark_node;
25697
25698 for (; t; t = TREE_CHAIN (t))
25699 {
25700 tree decl;
25701 tree init;
25702 tree expanded_bases = NULL_TREE;
25703 tree expanded_arguments = NULL_TREE;
25704 int i, len = 1;
25705
25706 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25707 {
25708 tree expr;
25709 tree arg;
25710
25711 /* Expand the base class expansion type into separate base
25712 classes. */
25713 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25714 tf_warning_or_error,
25715 NULL_TREE);
25716 if (expanded_bases == error_mark_node)
25717 continue;
25718
25719 /* We'll be building separate TREE_LISTs of arguments for
25720 each base. */
25721 len = TREE_VEC_LENGTH (expanded_bases);
25722 expanded_arguments = make_tree_vec (len);
25723 for (i = 0; i < len; i++)
25724 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25725
25726 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25727 expand each argument in the TREE_VALUE of t. */
25728 expr = make_node (EXPR_PACK_EXPANSION);
25729 PACK_EXPANSION_LOCAL_P (expr) = true;
25730 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25731 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25732
25733 if (TREE_VALUE (t) == void_type_node)
25734 /* VOID_TYPE_NODE is used to indicate
25735 value-initialization. */
25736 {
25737 for (i = 0; i < len; i++)
25738 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25739 }
25740 else
25741 {
25742 /* Substitute parameter packs into each argument in the
25743 TREE_LIST. */
25744 in_base_initializer = 1;
25745 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25746 {
25747 tree expanded_exprs;
25748
25749 /* Expand the argument. */
25750 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25751 expanded_exprs
25752 = tsubst_pack_expansion (expr, argvec,
25753 tf_warning_or_error,
25754 NULL_TREE);
25755 if (expanded_exprs == error_mark_node)
25756 continue;
25757
25758 /* Prepend each of the expanded expressions to the
25759 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25760 for (i = 0; i < len; i++)
25761 {
25762 TREE_VEC_ELT (expanded_arguments, i) =
25763 tree_cons (NULL_TREE,
25764 TREE_VEC_ELT (expanded_exprs, i),
25765 TREE_VEC_ELT (expanded_arguments, i));
25766 }
25767 }
25768 in_base_initializer = 0;
25769
25770 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25771 since we built them backwards. */
25772 for (i = 0; i < len; i++)
25773 {
25774 TREE_VEC_ELT (expanded_arguments, i) =
25775 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25776 }
25777 }
25778 }
25779
25780 for (i = 0; i < len; ++i)
25781 {
25782 if (expanded_bases)
25783 {
25784 decl = TREE_VEC_ELT (expanded_bases, i);
25785 decl = expand_member_init (decl);
25786 init = TREE_VEC_ELT (expanded_arguments, i);
25787 }
25788 else
25789 {
25790 tree tmp;
25791 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25792 tf_warning_or_error, NULL_TREE);
25793
25794 decl = expand_member_init (decl);
25795 if (decl && !DECL_P (decl))
25796 in_base_initializer = 1;
25797
25798 init = TREE_VALUE (t);
25799 tmp = init;
25800 if (init != void_type_node)
25801 init = tsubst_expr (init, argvec,
25802 tf_warning_or_error, NULL_TREE,
25803 /*integral_constant_expression_p=*/false);
25804 if (init == NULL_TREE && tmp != NULL_TREE)
25805 /* If we had an initializer but it instantiated to nothing,
25806 value-initialize the object. This will only occur when
25807 the initializer was a pack expansion where the parameter
25808 packs used in that expansion were of length zero. */
25809 init = void_type_node;
25810 in_base_initializer = 0;
25811 }
25812
25813 if (target_ctor != error_mark_node
25814 && init != error_mark_node)
25815 {
25816 error ("mem-initializer for %qD follows constructor delegation",
25817 decl);
25818 return inits;
25819 }
25820 /* Look for a target constructor. */
25821 if (init != error_mark_node
25822 && decl && CLASS_TYPE_P (decl)
25823 && same_type_p (decl, current_class_type))
25824 {
25825 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25826 if (inits)
25827 {
25828 error ("constructor delegation follows mem-initializer for %qD",
25829 TREE_PURPOSE (inits));
25830 continue;
25831 }
25832 target_ctor = init;
25833 }
25834
25835 if (decl)
25836 {
25837 init = build_tree_list (decl, init);
25838 TREE_CHAIN (init) = inits;
25839 inits = init;
25840 }
25841 }
25842 }
25843 return inits;
25844 }
25845
25846 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25847
25848 static void
25849 set_current_access_from_decl (tree decl)
25850 {
25851 if (TREE_PRIVATE (decl))
25852 current_access_specifier = access_private_node;
25853 else if (TREE_PROTECTED (decl))
25854 current_access_specifier = access_protected_node;
25855 else
25856 current_access_specifier = access_public_node;
25857 }
25858
25859 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25860 is the instantiation (which should have been created with
25861 start_enum) and ARGS are the template arguments to use. */
25862
25863 static void
25864 tsubst_enum (tree tag, tree newtag, tree args)
25865 {
25866 tree e;
25867
25868 if (SCOPED_ENUM_P (newtag))
25869 begin_scope (sk_scoped_enum, newtag);
25870
25871 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25872 {
25873 tree value;
25874 tree decl;
25875
25876 decl = TREE_VALUE (e);
25877 /* Note that in a template enum, the TREE_VALUE is the
25878 CONST_DECL, not the corresponding INTEGER_CST. */
25879 value = tsubst_expr (DECL_INITIAL (decl),
25880 args, tf_warning_or_error, NULL_TREE,
25881 /*integral_constant_expression_p=*/true);
25882
25883 /* Give this enumeration constant the correct access. */
25884 set_current_access_from_decl (decl);
25885
25886 /* Actually build the enumerator itself. Here we're assuming that
25887 enumerators can't have dependent attributes. */
25888 build_enumerator (DECL_NAME (decl), value, newtag,
25889 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25890 }
25891
25892 if (SCOPED_ENUM_P (newtag))
25893 finish_scope ();
25894
25895 finish_enum_value_list (newtag);
25896 finish_enum (newtag);
25897
25898 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25899 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25900 }
25901
25902 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25903 its type -- but without substituting the innermost set of template
25904 arguments. So, innermost set of template parameters will appear in
25905 the type. */
25906
25907 tree
25908 get_mostly_instantiated_function_type (tree decl)
25909 {
25910 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25911 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25912 }
25913
25914 /* Return truthvalue if we're processing a template different from
25915 the last one involved in diagnostics. */
25916 bool
25917 problematic_instantiation_changed (void)
25918 {
25919 return current_tinst_level != last_error_tinst_level;
25920 }
25921
25922 /* Remember current template involved in diagnostics. */
25923 void
25924 record_last_problematic_instantiation (void)
25925 {
25926 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25927 }
25928
25929 struct tinst_level *
25930 current_instantiation (void)
25931 {
25932 return current_tinst_level;
25933 }
25934
25935 /* Return TRUE if current_function_decl is being instantiated, false
25936 otherwise. */
25937
25938 bool
25939 instantiating_current_function_p (void)
25940 {
25941 return (current_instantiation ()
25942 && (current_instantiation ()->maybe_get_node ()
25943 == current_function_decl));
25944 }
25945
25946 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25947 type. Return false for ok, true for disallowed. Issue error and
25948 inform messages under control of COMPLAIN. */
25949
25950 static bool
25951 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25952 {
25953 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25954 return false;
25955 else if (TYPE_PTR_P (type))
25956 return false;
25957 else if (TYPE_REF_P (type)
25958 && !TYPE_REF_IS_RVALUE (type))
25959 return false;
25960 else if (TYPE_PTRMEM_P (type))
25961 return false;
25962 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25963 {
25964 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx2a)
25965 {
25966 if (complain & tf_error)
25967 error ("non-type template parameters of deduced class type only "
25968 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25969 return true;
25970 }
25971 return false;
25972 }
25973 else if (TREE_CODE (type) == TYPENAME_TYPE)
25974 return false;
25975 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25976 return false;
25977 else if (TREE_CODE (type) == NULLPTR_TYPE)
25978 return false;
25979 /* A bound template template parm could later be instantiated to have a valid
25980 nontype parm type via an alias template. */
25981 else if (cxx_dialect >= cxx11
25982 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25983 return false;
25984 else if (CLASS_TYPE_P (type))
25985 {
25986 if (cxx_dialect < cxx2a)
25987 {
25988 if (complain & tf_error)
25989 error ("non-type template parameters of class type only available "
25990 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25991 return true;
25992 }
25993 if (dependent_type_p (type))
25994 return false;
25995 if (!complete_type_or_else (type, NULL_TREE))
25996 return true;
25997 if (!structural_type_p (type))
25998 {
25999 if (complain & tf_error)
26000 {
26001 auto_diagnostic_group d;
26002 error ("%qT is not a valid type for a template non-type "
26003 "parameter because it is not structural", type);
26004 structural_type_p (type, true);
26005 }
26006 return true;
26007 }
26008 return false;
26009 }
26010
26011 if (complain & tf_error)
26012 {
26013 if (type == error_mark_node)
26014 inform (input_location, "invalid template non-type parameter");
26015 else
26016 error ("%q#T is not a valid type for a template non-type parameter",
26017 type);
26018 }
26019 return true;
26020 }
26021
26022 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26023 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26024
26025 static bool
26026 dependent_type_p_r (tree type)
26027 {
26028 tree scope;
26029
26030 /* [temp.dep.type]
26031
26032 A type is dependent if it is:
26033
26034 -- a template parameter. Template template parameters are types
26035 for us (since TYPE_P holds true for them) so we handle
26036 them here. */
26037 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26038 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26039 return true;
26040 /* -- a qualified-id with a nested-name-specifier which contains a
26041 class-name that names a dependent type or whose unqualified-id
26042 names a dependent type. */
26043 if (TREE_CODE (type) == TYPENAME_TYPE)
26044 return true;
26045
26046 /* An alias template specialization can be dependent even if the
26047 resulting type is not. */
26048 if (dependent_alias_template_spec_p (type, nt_transparent))
26049 return true;
26050
26051 /* -- a cv-qualified type where the cv-unqualified type is
26052 dependent.
26053 No code is necessary for this bullet; the code below handles
26054 cv-qualified types, and we don't want to strip aliases with
26055 TYPE_MAIN_VARIANT because of DR 1558. */
26056 /* -- a compound type constructed from any dependent type. */
26057 if (TYPE_PTRMEM_P (type))
26058 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26059 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26060 (type)));
26061 else if (INDIRECT_TYPE_P (type))
26062 return dependent_type_p (TREE_TYPE (type));
26063 else if (FUNC_OR_METHOD_TYPE_P (type))
26064 {
26065 tree arg_type;
26066
26067 if (dependent_type_p (TREE_TYPE (type)))
26068 return true;
26069 for (arg_type = TYPE_ARG_TYPES (type);
26070 arg_type;
26071 arg_type = TREE_CHAIN (arg_type))
26072 if (dependent_type_p (TREE_VALUE (arg_type)))
26073 return true;
26074 if (cxx_dialect >= cxx17)
26075 /* A value-dependent noexcept-specifier makes the type dependent. */
26076 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26077 if (tree noex = TREE_PURPOSE (spec))
26078 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26079 affect overload resolution and treating it as dependent breaks
26080 things. Same for an unparsed noexcept expression. */
26081 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26082 && TREE_CODE (noex) != DEFERRED_PARSE
26083 && value_dependent_expression_p (noex))
26084 return true;
26085 return false;
26086 }
26087 /* -- an array type constructed from any dependent type or whose
26088 size is specified by a constant expression that is
26089 value-dependent.
26090
26091 We checked for type- and value-dependence of the bounds in
26092 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26093 if (TREE_CODE (type) == ARRAY_TYPE)
26094 {
26095 if (TYPE_DOMAIN (type)
26096 && dependent_type_p (TYPE_DOMAIN (type)))
26097 return true;
26098 return dependent_type_p (TREE_TYPE (type));
26099 }
26100
26101 /* -- a template-id in which either the template name is a template
26102 parameter ... */
26103 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26104 return true;
26105 /* ... or any of the template arguments is a dependent type or
26106 an expression that is type-dependent or value-dependent. */
26107 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26108 && (any_dependent_template_arguments_p
26109 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26110 return true;
26111
26112 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26113 dependent; if the argument of the `typeof' expression is not
26114 type-dependent, then it should already been have resolved. */
26115 if (TREE_CODE (type) == TYPEOF_TYPE
26116 || TREE_CODE (type) == DECLTYPE_TYPE
26117 || TREE_CODE (type) == UNDERLYING_TYPE)
26118 return true;
26119
26120 /* A template argument pack is dependent if any of its packed
26121 arguments are. */
26122 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26123 {
26124 tree args = ARGUMENT_PACK_ARGS (type);
26125 int i, len = TREE_VEC_LENGTH (args);
26126 for (i = 0; i < len; ++i)
26127 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26128 return true;
26129 }
26130
26131 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26132 be template parameters. */
26133 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26134 return true;
26135
26136 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26137 return true;
26138
26139 /* The standard does not specifically mention types that are local
26140 to template functions or local classes, but they should be
26141 considered dependent too. For example:
26142
26143 template <int I> void f() {
26144 enum E { a = I };
26145 S<sizeof (E)> s;
26146 }
26147
26148 The size of `E' cannot be known until the value of `I' has been
26149 determined. Therefore, `E' must be considered dependent. */
26150 scope = TYPE_CONTEXT (type);
26151 if (scope && TYPE_P (scope))
26152 return dependent_type_p (scope);
26153 /* Don't use type_dependent_expression_p here, as it can lead
26154 to infinite recursion trying to determine whether a lambda
26155 nested in a lambda is dependent (c++/47687). */
26156 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26157 && DECL_LANG_SPECIFIC (scope)
26158 && DECL_TEMPLATE_INFO (scope)
26159 && (any_dependent_template_arguments_p
26160 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26161 return true;
26162
26163 /* Other types are non-dependent. */
26164 return false;
26165 }
26166
26167 /* Returns TRUE if TYPE is dependent, in the sense of
26168 [temp.dep.type]. Note that a NULL type is considered dependent. */
26169
26170 bool
26171 dependent_type_p (tree type)
26172 {
26173 /* If there are no template parameters in scope, then there can't be
26174 any dependent types. */
26175 if (!processing_template_decl)
26176 {
26177 /* If we are not processing a template, then nobody should be
26178 providing us with a dependent type. */
26179 gcc_assert (type);
26180 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26181 return false;
26182 }
26183
26184 /* If the type is NULL, we have not computed a type for the entity
26185 in question; in that case, the type is dependent. */
26186 if (!type)
26187 return true;
26188
26189 /* Erroneous types can be considered non-dependent. */
26190 if (type == error_mark_node)
26191 return false;
26192
26193 /* Getting here with global_type_node means we improperly called this
26194 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26195 gcc_checking_assert (type != global_type_node);
26196
26197 /* If we have not already computed the appropriate value for TYPE,
26198 do so now. */
26199 if (!TYPE_DEPENDENT_P_VALID (type))
26200 {
26201 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26202 TYPE_DEPENDENT_P_VALID (type) = 1;
26203 }
26204
26205 return TYPE_DEPENDENT_P (type);
26206 }
26207
26208 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26209 lookup. In other words, a dependent type that is not the current
26210 instantiation. */
26211
26212 bool
26213 dependent_scope_p (tree scope)
26214 {
26215 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26216 && !currently_open_class (scope));
26217 }
26218
26219 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26220 an unknown base of 'this' (and is therefore instantiation-dependent). */
26221
26222 static bool
26223 unknown_base_ref_p (tree t)
26224 {
26225 if (!current_class_ptr)
26226 return false;
26227
26228 tree mem = TREE_OPERAND (t, 1);
26229 if (shared_member_p (mem))
26230 return false;
26231
26232 tree cur = current_nonlambda_class_type ();
26233 if (!any_dependent_bases_p (cur))
26234 return false;
26235
26236 tree ctx = TREE_OPERAND (t, 0);
26237 if (DERIVED_FROM_P (ctx, cur))
26238 return false;
26239
26240 return true;
26241 }
26242
26243 /* T is a SCOPE_REF; return whether we need to consider it
26244 instantiation-dependent so that we can check access at instantiation
26245 time even though we know which member it resolves to. */
26246
26247 static bool
26248 instantiation_dependent_scope_ref_p (tree t)
26249 {
26250 if (DECL_P (TREE_OPERAND (t, 1))
26251 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26252 && !unknown_base_ref_p (t)
26253 && accessible_in_template_p (TREE_OPERAND (t, 0),
26254 TREE_OPERAND (t, 1)))
26255 return false;
26256 else
26257 return true;
26258 }
26259
26260 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26261 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26262 expression. */
26263
26264 /* Note that this predicate is not appropriate for general expressions;
26265 only constant expressions (that satisfy potential_constant_expression)
26266 can be tested for value dependence. */
26267
26268 bool
26269 value_dependent_expression_p (tree expression)
26270 {
26271 if (!processing_template_decl || expression == NULL_TREE)
26272 return false;
26273
26274 /* A type-dependent expression is also value-dependent. */
26275 if (type_dependent_expression_p (expression))
26276 return true;
26277
26278 switch (TREE_CODE (expression))
26279 {
26280 case BASELINK:
26281 /* A dependent member function of the current instantiation. */
26282 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26283
26284 case FUNCTION_DECL:
26285 /* A dependent member function of the current instantiation. */
26286 if (DECL_CLASS_SCOPE_P (expression)
26287 && dependent_type_p (DECL_CONTEXT (expression)))
26288 return true;
26289 break;
26290
26291 case IDENTIFIER_NODE:
26292 /* A name that has not been looked up -- must be dependent. */
26293 return true;
26294
26295 case TEMPLATE_PARM_INDEX:
26296 /* A non-type template parm. */
26297 return true;
26298
26299 case CONST_DECL:
26300 /* A non-type template parm. */
26301 if (DECL_TEMPLATE_PARM_P (expression))
26302 return true;
26303 return value_dependent_expression_p (DECL_INITIAL (expression));
26304
26305 case VAR_DECL:
26306 /* A constant with literal type and is initialized
26307 with an expression that is value-dependent. */
26308 if (DECL_DEPENDENT_INIT_P (expression)
26309 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26310 || TYPE_REF_P (TREE_TYPE (expression)))
26311 return true;
26312 if (DECL_HAS_VALUE_EXPR_P (expression))
26313 {
26314 tree value_expr = DECL_VALUE_EXPR (expression);
26315 if (value_dependent_expression_p (value_expr)
26316 /* __PRETTY_FUNCTION__ inside a template function is dependent
26317 on the name of the function. */
26318 || (DECL_PRETTY_FUNCTION_P (expression)
26319 /* It might be used in a template, but not a template
26320 function, in which case its DECL_VALUE_EXPR will be
26321 "top level". */
26322 && value_expr == error_mark_node))
26323 return true;
26324 }
26325 return false;
26326
26327 case DYNAMIC_CAST_EXPR:
26328 case STATIC_CAST_EXPR:
26329 case CONST_CAST_EXPR:
26330 case REINTERPRET_CAST_EXPR:
26331 case CAST_EXPR:
26332 case IMPLICIT_CONV_EXPR:
26333 /* These expressions are value-dependent if the type to which
26334 the cast occurs is dependent or the expression being casted
26335 is value-dependent. */
26336 {
26337 tree type = TREE_TYPE (expression);
26338
26339 if (dependent_type_p (type))
26340 return true;
26341
26342 /* A functional cast has a list of operands. */
26343 expression = TREE_OPERAND (expression, 0);
26344 if (!expression)
26345 {
26346 /* If there are no operands, it must be an expression such
26347 as "int()". This should not happen for aggregate types
26348 because it would form non-constant expressions. */
26349 gcc_assert (cxx_dialect >= cxx11
26350 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26351
26352 return false;
26353 }
26354
26355 if (TREE_CODE (expression) == TREE_LIST)
26356 return any_value_dependent_elements_p (expression);
26357
26358 return value_dependent_expression_p (expression);
26359 }
26360
26361 case SIZEOF_EXPR:
26362 if (SIZEOF_EXPR_TYPE_P (expression))
26363 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26364 /* FALLTHRU */
26365 case ALIGNOF_EXPR:
26366 case TYPEID_EXPR:
26367 /* A `sizeof' expression is value-dependent if the operand is
26368 type-dependent or is a pack expansion. */
26369 expression = TREE_OPERAND (expression, 0);
26370 if (PACK_EXPANSION_P (expression))
26371 return true;
26372 else if (TYPE_P (expression))
26373 return dependent_type_p (expression);
26374 return instantiation_dependent_uneval_expression_p (expression);
26375
26376 case AT_ENCODE_EXPR:
26377 /* An 'encode' expression is value-dependent if the operand is
26378 type-dependent. */
26379 expression = TREE_OPERAND (expression, 0);
26380 return dependent_type_p (expression);
26381
26382 case NOEXCEPT_EXPR:
26383 expression = TREE_OPERAND (expression, 0);
26384 return instantiation_dependent_uneval_expression_p (expression);
26385
26386 case SCOPE_REF:
26387 /* All instantiation-dependent expressions should also be considered
26388 value-dependent. */
26389 return instantiation_dependent_scope_ref_p (expression);
26390
26391 case COMPONENT_REF:
26392 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26393 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26394
26395 case NONTYPE_ARGUMENT_PACK:
26396 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26397 is value-dependent. */
26398 {
26399 tree values = ARGUMENT_PACK_ARGS (expression);
26400 int i, len = TREE_VEC_LENGTH (values);
26401
26402 for (i = 0; i < len; ++i)
26403 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26404 return true;
26405
26406 return false;
26407 }
26408
26409 case TRAIT_EXPR:
26410 {
26411 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26412
26413 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26414 return true;
26415
26416 if (!type2)
26417 return false;
26418
26419 if (TREE_CODE (type2) != TREE_LIST)
26420 return dependent_type_p (type2);
26421
26422 for (; type2; type2 = TREE_CHAIN (type2))
26423 if (dependent_type_p (TREE_VALUE (type2)))
26424 return true;
26425
26426 return false;
26427 }
26428
26429 case MODOP_EXPR:
26430 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26431 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26432
26433 case ARRAY_REF:
26434 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26435 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26436
26437 case ADDR_EXPR:
26438 {
26439 tree op = TREE_OPERAND (expression, 0);
26440 return (value_dependent_expression_p (op)
26441 || has_value_dependent_address (op));
26442 }
26443
26444 case REQUIRES_EXPR:
26445 /* Treat all requires-expressions as value-dependent so
26446 we don't try to fold them. */
26447 return true;
26448
26449 case TYPE_REQ:
26450 return dependent_type_p (TREE_OPERAND (expression, 0));
26451
26452 case CALL_EXPR:
26453 {
26454 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26455 return true;
26456 tree fn = get_callee_fndecl (expression);
26457 int i, nargs;
26458 nargs = call_expr_nargs (expression);
26459 for (i = 0; i < nargs; ++i)
26460 {
26461 tree op = CALL_EXPR_ARG (expression, i);
26462 /* In a call to a constexpr member function, look through the
26463 implicit ADDR_EXPR on the object argument so that it doesn't
26464 cause the call to be considered value-dependent. We also
26465 look through it in potential_constant_expression. */
26466 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26467 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26468 && TREE_CODE (op) == ADDR_EXPR)
26469 op = TREE_OPERAND (op, 0);
26470 if (value_dependent_expression_p (op))
26471 return true;
26472 }
26473 return false;
26474 }
26475
26476 case TEMPLATE_ID_EXPR:
26477 return concept_definition_p (TREE_OPERAND (expression, 0));
26478
26479 case CONSTRUCTOR:
26480 {
26481 unsigned ix;
26482 tree val;
26483 if (dependent_type_p (TREE_TYPE (expression)))
26484 return true;
26485 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26486 if (value_dependent_expression_p (val))
26487 return true;
26488 return false;
26489 }
26490
26491 case STMT_EXPR:
26492 /* Treat a GNU statement expression as dependent to avoid crashing
26493 under instantiate_non_dependent_expr; it can't be constant. */
26494 return true;
26495
26496 default:
26497 /* A constant expression is value-dependent if any subexpression is
26498 value-dependent. */
26499 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26500 {
26501 case tcc_reference:
26502 case tcc_unary:
26503 case tcc_comparison:
26504 case tcc_binary:
26505 case tcc_expression:
26506 case tcc_vl_exp:
26507 {
26508 int i, len = cp_tree_operand_length (expression);
26509
26510 for (i = 0; i < len; i++)
26511 {
26512 tree t = TREE_OPERAND (expression, i);
26513
26514 /* In some cases, some of the operands may be missing.
26515 (For example, in the case of PREDECREMENT_EXPR, the
26516 amount to increment by may be missing.) That doesn't
26517 make the expression dependent. */
26518 if (t && value_dependent_expression_p (t))
26519 return true;
26520 }
26521 }
26522 break;
26523 default:
26524 break;
26525 }
26526 break;
26527 }
26528
26529 /* The expression is not value-dependent. */
26530 return false;
26531 }
26532
26533 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26534 [temp.dep.expr]. Note that an expression with no type is
26535 considered dependent. Other parts of the compiler arrange for an
26536 expression with type-dependent subexpressions to have no type, so
26537 this function doesn't have to be fully recursive. */
26538
26539 bool
26540 type_dependent_expression_p (tree expression)
26541 {
26542 if (!processing_template_decl)
26543 return false;
26544
26545 if (expression == NULL_TREE || expression == error_mark_node)
26546 return false;
26547
26548 STRIP_ANY_LOCATION_WRAPPER (expression);
26549
26550 /* An unresolved name is always dependent. */
26551 if (identifier_p (expression)
26552 || TREE_CODE (expression) == USING_DECL
26553 || TREE_CODE (expression) == WILDCARD_DECL)
26554 return true;
26555
26556 /* A lambda-expression in template context is dependent. dependent_type_p is
26557 true for a lambda in the scope of a class or function template, but that
26558 doesn't cover all template contexts, like a default template argument. */
26559 if (TREE_CODE (expression) == LAMBDA_EXPR)
26560 return true;
26561
26562 /* A fold expression is type-dependent. */
26563 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26564 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26565 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26566 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26567 return true;
26568
26569 /* Some expression forms are never type-dependent. */
26570 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26571 || TREE_CODE (expression) == SIZEOF_EXPR
26572 || TREE_CODE (expression) == ALIGNOF_EXPR
26573 || TREE_CODE (expression) == AT_ENCODE_EXPR
26574 || TREE_CODE (expression) == NOEXCEPT_EXPR
26575 || TREE_CODE (expression) == TRAIT_EXPR
26576 || TREE_CODE (expression) == TYPEID_EXPR
26577 || TREE_CODE (expression) == DELETE_EXPR
26578 || TREE_CODE (expression) == VEC_DELETE_EXPR
26579 || TREE_CODE (expression) == THROW_EXPR
26580 || TREE_CODE (expression) == REQUIRES_EXPR)
26581 return false;
26582
26583 /* The types of these expressions depends only on the type to which
26584 the cast occurs. */
26585 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26586 || TREE_CODE (expression) == STATIC_CAST_EXPR
26587 || TREE_CODE (expression) == CONST_CAST_EXPR
26588 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26589 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26590 || TREE_CODE (expression) == CAST_EXPR)
26591 return dependent_type_p (TREE_TYPE (expression));
26592
26593 /* The types of these expressions depends only on the type created
26594 by the expression. */
26595 if (TREE_CODE (expression) == NEW_EXPR
26596 || TREE_CODE (expression) == VEC_NEW_EXPR)
26597 {
26598 /* For NEW_EXPR tree nodes created inside a template, either
26599 the object type itself or a TREE_LIST may appear as the
26600 operand 1. */
26601 tree type = TREE_OPERAND (expression, 1);
26602 if (TREE_CODE (type) == TREE_LIST)
26603 /* This is an array type. We need to check array dimensions
26604 as well. */
26605 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26606 || value_dependent_expression_p
26607 (TREE_OPERAND (TREE_VALUE (type), 1));
26608 else
26609 return dependent_type_p (type);
26610 }
26611
26612 if (TREE_CODE (expression) == SCOPE_REF)
26613 {
26614 tree scope = TREE_OPERAND (expression, 0);
26615 tree name = TREE_OPERAND (expression, 1);
26616
26617 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26618 contains an identifier associated by name lookup with one or more
26619 declarations declared with a dependent type, or...a
26620 nested-name-specifier or qualified-id that names a member of an
26621 unknown specialization. */
26622 return (type_dependent_expression_p (name)
26623 || dependent_scope_p (scope));
26624 }
26625
26626 if (TREE_CODE (expression) == TEMPLATE_DECL
26627 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26628 return uses_outer_template_parms (expression);
26629
26630 if (TREE_CODE (expression) == STMT_EXPR)
26631 expression = stmt_expr_value_expr (expression);
26632
26633 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26634 {
26635 tree elt;
26636 unsigned i;
26637
26638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26639 {
26640 if (type_dependent_expression_p (elt))
26641 return true;
26642 }
26643 return false;
26644 }
26645
26646 /* A static data member of the current instantiation with incomplete
26647 array type is type-dependent, as the definition and specializations
26648 can have different bounds. */
26649 if (VAR_P (expression)
26650 && DECL_CLASS_SCOPE_P (expression)
26651 && dependent_type_p (DECL_CONTEXT (expression))
26652 && VAR_HAD_UNKNOWN_BOUND (expression))
26653 return true;
26654
26655 /* An array of unknown bound depending on a variadic parameter, eg:
26656
26657 template<typename... Args>
26658 void foo (Args... args)
26659 {
26660 int arr[] = { args... };
26661 }
26662
26663 template<int... vals>
26664 void bar ()
26665 {
26666 int arr[] = { vals... };
26667 }
26668
26669 If the array has no length and has an initializer, it must be that
26670 we couldn't determine its length in cp_complete_array_type because
26671 it is dependent. */
26672 if (VAR_P (expression)
26673 && TREE_TYPE (expression) != NULL_TREE
26674 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26675 && !TYPE_DOMAIN (TREE_TYPE (expression))
26676 && DECL_INITIAL (expression))
26677 return true;
26678
26679 /* A function or variable template-id is type-dependent if it has any
26680 dependent template arguments. */
26681 if (VAR_OR_FUNCTION_DECL_P (expression)
26682 && DECL_LANG_SPECIFIC (expression)
26683 && DECL_TEMPLATE_INFO (expression))
26684 {
26685 /* Consider the innermost template arguments, since those are the ones
26686 that come from the template-id; the template arguments for the
26687 enclosing class do not make it type-dependent unless they are used in
26688 the type of the decl. */
26689 if (instantiates_primary_template_p (expression)
26690 && (any_dependent_template_arguments_p
26691 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26692 return true;
26693 }
26694
26695 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26696 type-dependent. Checking this is important for functions with auto return
26697 type, which looks like a dependent type. */
26698 if (TREE_CODE (expression) == FUNCTION_DECL
26699 && !(DECL_CLASS_SCOPE_P (expression)
26700 && dependent_type_p (DECL_CONTEXT (expression)))
26701 && !(DECL_LANG_SPECIFIC (expression)
26702 && DECL_FRIEND_P (expression)
26703 && (!DECL_FRIEND_CONTEXT (expression)
26704 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26705 && !DECL_LOCAL_FUNCTION_P (expression))
26706 {
26707 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26708 || undeduced_auto_decl (expression));
26709 return false;
26710 }
26711
26712 /* Always dependent, on the number of arguments if nothing else. */
26713 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26714 return true;
26715
26716 if (TREE_TYPE (expression) == unknown_type_node)
26717 {
26718 if (TREE_CODE (expression) == ADDR_EXPR)
26719 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26720 if (TREE_CODE (expression) == COMPONENT_REF
26721 || TREE_CODE (expression) == OFFSET_REF)
26722 {
26723 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26724 return true;
26725 expression = TREE_OPERAND (expression, 1);
26726 if (identifier_p (expression))
26727 return false;
26728 }
26729 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26730 if (TREE_CODE (expression) == SCOPE_REF)
26731 return false;
26732
26733 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26734 if (TREE_CODE (expression) == CO_AWAIT_EXPR
26735 || TREE_CODE (expression) == CO_YIELD_EXPR)
26736 return true;
26737
26738 if (BASELINK_P (expression))
26739 {
26740 if (BASELINK_OPTYPE (expression)
26741 && dependent_type_p (BASELINK_OPTYPE (expression)))
26742 return true;
26743 expression = BASELINK_FUNCTIONS (expression);
26744 }
26745
26746 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26747 {
26748 if (any_dependent_template_arguments_p
26749 (TREE_OPERAND (expression, 1)))
26750 return true;
26751 expression = TREE_OPERAND (expression, 0);
26752 if (identifier_p (expression))
26753 return true;
26754 }
26755
26756 gcc_assert (OVL_P (expression));
26757
26758 for (lkp_iterator iter (expression); iter; ++iter)
26759 if (type_dependent_expression_p (*iter))
26760 return true;
26761
26762 return false;
26763 }
26764
26765 /* The type of a non-type template parm declared with a placeholder type
26766 depends on the corresponding template argument, even though
26767 placeholders are not normally considered dependent. */
26768 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26769 && is_auto (TREE_TYPE (expression)))
26770 return true;
26771
26772 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26773
26774 /* Dependent type attributes might not have made it from the decl to
26775 the type yet. */
26776 if (DECL_P (expression)
26777 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26778 return true;
26779
26780 return (dependent_type_p (TREE_TYPE (expression)));
26781 }
26782
26783 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26784 type-dependent if the expression refers to a member of the current
26785 instantiation and the type of the referenced member is dependent, or the
26786 class member access expression refers to a member of an unknown
26787 specialization.
26788
26789 This function returns true if the OBJECT in such a class member access
26790 expression is of an unknown specialization. */
26791
26792 bool
26793 type_dependent_object_expression_p (tree object)
26794 {
26795 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26796 dependent. */
26797 if (TREE_CODE (object) == IDENTIFIER_NODE)
26798 return true;
26799 tree scope = TREE_TYPE (object);
26800 return (!scope || dependent_scope_p (scope));
26801 }
26802
26803 /* walk_tree callback function for instantiation_dependent_expression_p,
26804 below. Returns non-zero if a dependent subexpression is found. */
26805
26806 static tree
26807 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26808 void * /*data*/)
26809 {
26810 if (TYPE_P (*tp))
26811 {
26812 /* We don't have to worry about decltype currently because decltype
26813 of an instantiation-dependent expr is a dependent type. This
26814 might change depending on the resolution of DR 1172. */
26815 *walk_subtrees = false;
26816 return NULL_TREE;
26817 }
26818 enum tree_code code = TREE_CODE (*tp);
26819 switch (code)
26820 {
26821 /* Don't treat an argument list as dependent just because it has no
26822 TREE_TYPE. */
26823 case TREE_LIST:
26824 case TREE_VEC:
26825 case NONTYPE_ARGUMENT_PACK:
26826 return NULL_TREE;
26827
26828 case TEMPLATE_PARM_INDEX:
26829 if (dependent_type_p (TREE_TYPE (*tp)))
26830 return *tp;
26831 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26832 return *tp;
26833 /* We'll check value-dependence separately. */
26834 return NULL_TREE;
26835
26836 /* Handle expressions with type operands. */
26837 case SIZEOF_EXPR:
26838 case ALIGNOF_EXPR:
26839 case TYPEID_EXPR:
26840 case AT_ENCODE_EXPR:
26841 {
26842 tree op = TREE_OPERAND (*tp, 0);
26843 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26844 op = TREE_TYPE (op);
26845 if (TYPE_P (op))
26846 {
26847 if (dependent_type_p (op))
26848 return *tp;
26849 else
26850 {
26851 *walk_subtrees = false;
26852 return NULL_TREE;
26853 }
26854 }
26855 break;
26856 }
26857
26858 case COMPONENT_REF:
26859 if (identifier_p (TREE_OPERAND (*tp, 1)))
26860 /* In a template, finish_class_member_access_expr creates a
26861 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26862 type-dependent, so that we can check access control at
26863 instantiation time (PR 42277). See also Core issue 1273. */
26864 return *tp;
26865 break;
26866
26867 case SCOPE_REF:
26868 if (instantiation_dependent_scope_ref_p (*tp))
26869 return *tp;
26870 else
26871 break;
26872
26873 /* Treat statement-expressions as dependent. */
26874 case BIND_EXPR:
26875 return *tp;
26876
26877 /* Treat requires-expressions as dependent. */
26878 case REQUIRES_EXPR:
26879 return *tp;
26880
26881 case CALL_EXPR:
26882 /* Treat concept checks as dependent. */
26883 if (concept_check_p (*tp))
26884 return *tp;
26885 break;
26886
26887 case TEMPLATE_ID_EXPR:
26888 /* Treat concept checks as dependent. */
26889 if (concept_check_p (*tp))
26890 return *tp;
26891 break;
26892
26893 case CONSTRUCTOR:
26894 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26895 return *tp;
26896 break;
26897
26898 default:
26899 break;
26900 }
26901
26902 if (type_dependent_expression_p (*tp))
26903 return *tp;
26904 else
26905 return NULL_TREE;
26906 }
26907
26908 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26909 sense defined by the ABI:
26910
26911 "An expression is instantiation-dependent if it is type-dependent
26912 or value-dependent, or it has a subexpression that is type-dependent
26913 or value-dependent."
26914
26915 Except don't actually check value-dependence for unevaluated expressions,
26916 because in sizeof(i) we don't care about the value of i. Checking
26917 type-dependence will in turn check value-dependence of array bounds/template
26918 arguments as needed. */
26919
26920 bool
26921 instantiation_dependent_uneval_expression_p (tree expression)
26922 {
26923 tree result;
26924
26925 if (!processing_template_decl)
26926 return false;
26927
26928 if (expression == error_mark_node)
26929 return false;
26930
26931 result = cp_walk_tree_without_duplicates (&expression,
26932 instantiation_dependent_r, NULL);
26933 return result != NULL_TREE;
26934 }
26935
26936 /* As above, but also check value-dependence of the expression as a whole. */
26937
26938 bool
26939 instantiation_dependent_expression_p (tree expression)
26940 {
26941 return (instantiation_dependent_uneval_expression_p (expression)
26942 || value_dependent_expression_p (expression));
26943 }
26944
26945 /* Like type_dependent_expression_p, but it also works while not processing
26946 a template definition, i.e. during substitution or mangling. */
26947
26948 bool
26949 type_dependent_expression_p_push (tree expr)
26950 {
26951 bool b;
26952 ++processing_template_decl;
26953 b = type_dependent_expression_p (expr);
26954 --processing_template_decl;
26955 return b;
26956 }
26957
26958 /* Returns TRUE if ARGS contains a type-dependent expression. */
26959
26960 bool
26961 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26962 {
26963 unsigned int i;
26964 tree arg;
26965
26966 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26967 {
26968 if (type_dependent_expression_p (arg))
26969 return true;
26970 }
26971 return false;
26972 }
26973
26974 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26975 expressions) contains any type-dependent expressions. */
26976
26977 bool
26978 any_type_dependent_elements_p (const_tree list)
26979 {
26980 for (; list; list = TREE_CHAIN (list))
26981 if (type_dependent_expression_p (TREE_VALUE (list)))
26982 return true;
26983
26984 return false;
26985 }
26986
26987 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26988 expressions) contains any value-dependent expressions. */
26989
26990 bool
26991 any_value_dependent_elements_p (const_tree list)
26992 {
26993 for (; list; list = TREE_CHAIN (list))
26994 if (value_dependent_expression_p (TREE_VALUE (list)))
26995 return true;
26996
26997 return false;
26998 }
26999
27000 /* Returns TRUE if the ARG (a template argument) is dependent. */
27001
27002 bool
27003 dependent_template_arg_p (tree arg)
27004 {
27005 if (!processing_template_decl)
27006 return false;
27007
27008 /* Assume a template argument that was wrongly written by the user
27009 is dependent. This is consistent with what
27010 any_dependent_template_arguments_p [that calls this function]
27011 does. */
27012 if (!arg || arg == error_mark_node)
27013 return true;
27014
27015 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27016 arg = argument_pack_select_arg (arg);
27017
27018 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27019 return true;
27020 if (TREE_CODE (arg) == TEMPLATE_DECL)
27021 {
27022 if (DECL_TEMPLATE_PARM_P (arg))
27023 return true;
27024 /* A member template of a dependent class is not necessarily
27025 type-dependent, but it is a dependent template argument because it
27026 will be a member of an unknown specialization to that template. */
27027 tree scope = CP_DECL_CONTEXT (arg);
27028 return TYPE_P (scope) && dependent_type_p (scope);
27029 }
27030 else if (ARGUMENT_PACK_P (arg))
27031 {
27032 tree args = ARGUMENT_PACK_ARGS (arg);
27033 int i, len = TREE_VEC_LENGTH (args);
27034 for (i = 0; i < len; ++i)
27035 {
27036 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27037 return true;
27038 }
27039
27040 return false;
27041 }
27042 else if (TYPE_P (arg))
27043 return dependent_type_p (arg);
27044 else
27045 return value_dependent_expression_p (arg);
27046 }
27047
27048 /* Returns true if ARGS (a collection of template arguments) contains
27049 any types that require structural equality testing. */
27050
27051 bool
27052 any_template_arguments_need_structural_equality_p (tree args)
27053 {
27054 int i;
27055 int j;
27056
27057 if (!args)
27058 return false;
27059 if (args == error_mark_node)
27060 return true;
27061
27062 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27063 {
27064 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27065 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27066 {
27067 tree arg = TREE_VEC_ELT (level, j);
27068 tree packed_args = NULL_TREE;
27069 int k, len = 1;
27070
27071 if (ARGUMENT_PACK_P (arg))
27072 {
27073 /* Look inside the argument pack. */
27074 packed_args = ARGUMENT_PACK_ARGS (arg);
27075 len = TREE_VEC_LENGTH (packed_args);
27076 }
27077
27078 for (k = 0; k < len; ++k)
27079 {
27080 if (packed_args)
27081 arg = TREE_VEC_ELT (packed_args, k);
27082
27083 if (error_operand_p (arg))
27084 return true;
27085 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27086 continue;
27087 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27088 return true;
27089 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27090 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27091 return true;
27092 }
27093 }
27094 }
27095
27096 return false;
27097 }
27098
27099 /* Returns true if ARGS (a collection of template arguments) contains
27100 any dependent arguments. */
27101
27102 bool
27103 any_dependent_template_arguments_p (const_tree args)
27104 {
27105 int i;
27106 int j;
27107
27108 if (!args)
27109 return false;
27110 if (args == error_mark_node)
27111 return true;
27112
27113 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27114 {
27115 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27116 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27117 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27118 return true;
27119 }
27120
27121 return false;
27122 }
27123
27124 /* Returns true if ARGS contains any errors. */
27125
27126 bool
27127 any_erroneous_template_args_p (const_tree args)
27128 {
27129 int i;
27130 int j;
27131
27132 if (args == error_mark_node)
27133 return true;
27134
27135 if (args && TREE_CODE (args) != TREE_VEC)
27136 {
27137 if (tree ti = get_template_info (args))
27138 args = TI_ARGS (ti);
27139 else
27140 args = NULL_TREE;
27141 }
27142
27143 if (!args)
27144 return false;
27145
27146 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27147 {
27148 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27149 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27150 if (error_operand_p (TREE_VEC_ELT (level, j)))
27151 return true;
27152 }
27153
27154 return false;
27155 }
27156
27157 /* Returns TRUE if the template TMPL is type-dependent. */
27158
27159 bool
27160 dependent_template_p (tree tmpl)
27161 {
27162 if (TREE_CODE (tmpl) == OVERLOAD)
27163 {
27164 for (lkp_iterator iter (tmpl); iter; ++iter)
27165 if (dependent_template_p (*iter))
27166 return true;
27167 return false;
27168 }
27169
27170 /* Template template parameters are dependent. */
27171 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27172 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27173 return true;
27174 /* So are names that have not been looked up. */
27175 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27176 return true;
27177 return false;
27178 }
27179
27180 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27181
27182 bool
27183 dependent_template_id_p (tree tmpl, tree args)
27184 {
27185 return (dependent_template_p (tmpl)
27186 || any_dependent_template_arguments_p (args));
27187 }
27188
27189 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27190 are dependent. */
27191
27192 bool
27193 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27194 {
27195 int i;
27196
27197 if (!processing_template_decl)
27198 return false;
27199
27200 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27201 {
27202 tree decl = TREE_VEC_ELT (declv, i);
27203 tree init = TREE_VEC_ELT (initv, i);
27204 tree cond = TREE_VEC_ELT (condv, i);
27205 tree incr = TREE_VEC_ELT (incrv, i);
27206
27207 if (type_dependent_expression_p (decl)
27208 || TREE_CODE (decl) == SCOPE_REF)
27209 return true;
27210
27211 if (init && type_dependent_expression_p (init))
27212 return true;
27213
27214 if (cond == global_namespace)
27215 return true;
27216
27217 if (type_dependent_expression_p (cond))
27218 return true;
27219
27220 if (COMPARISON_CLASS_P (cond)
27221 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27222 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27223 return true;
27224
27225 if (TREE_CODE (incr) == MODOP_EXPR)
27226 {
27227 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27228 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27229 return true;
27230 }
27231 else if (type_dependent_expression_p (incr))
27232 return true;
27233 else if (TREE_CODE (incr) == MODIFY_EXPR)
27234 {
27235 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27236 return true;
27237 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27238 {
27239 tree t = TREE_OPERAND (incr, 1);
27240 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27241 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27242 return true;
27243
27244 /* If this loop has a class iterator with != comparison
27245 with increment other than i++/++i/i--/--i, make sure the
27246 increment is constant. */
27247 if (CLASS_TYPE_P (TREE_TYPE (decl))
27248 && TREE_CODE (cond) == NE_EXPR)
27249 {
27250 if (TREE_OPERAND (t, 0) == decl)
27251 t = TREE_OPERAND (t, 1);
27252 else
27253 t = TREE_OPERAND (t, 0);
27254 if (TREE_CODE (t) != INTEGER_CST)
27255 return true;
27256 }
27257 }
27258 }
27259 }
27260
27261 return false;
27262 }
27263
27264 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27265 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27266 no such TYPE can be found. Note that this function peers inside
27267 uninstantiated templates and therefore should be used only in
27268 extremely limited situations. ONLY_CURRENT_P restricts this
27269 peering to the currently open classes hierarchy (which is required
27270 when comparing types). */
27271
27272 tree
27273 resolve_typename_type (tree type, bool only_current_p)
27274 {
27275 tree scope;
27276 tree name;
27277 tree decl;
27278 int quals;
27279 tree pushed_scope;
27280 tree result;
27281
27282 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27283
27284 scope = TYPE_CONTEXT (type);
27285 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27286 gcc_checking_assert (uses_template_parms (scope));
27287
27288 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27289 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27290 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27291 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27292 identifier of the TYPENAME_TYPE anymore.
27293 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27294 TYPENAME_TYPE instead, we avoid messing up with a possible
27295 typedef variant case. */
27296 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27297
27298 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27299 it first before we can figure out what NAME refers to. */
27300 if (TREE_CODE (scope) == TYPENAME_TYPE)
27301 {
27302 if (TYPENAME_IS_RESOLVING_P (scope))
27303 /* Given a class template A with a dependent base with nested type C,
27304 typedef typename A::C::C C will land us here, as trying to resolve
27305 the initial A::C leads to the local C typedef, which leads back to
27306 A::C::C. So we break the recursion now. */
27307 return type;
27308 else
27309 scope = resolve_typename_type (scope, only_current_p);
27310 }
27311 /* If we don't know what SCOPE refers to, then we cannot resolve the
27312 TYPENAME_TYPE. */
27313 if (!CLASS_TYPE_P (scope))
27314 return type;
27315 /* If this is a typedef, we don't want to look inside (c++/11987). */
27316 if (typedef_variant_p (type))
27317 return type;
27318 /* If SCOPE isn't the template itself, it will not have a valid
27319 TYPE_FIELDS list. */
27320 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27321 /* scope is either the template itself or a compatible instantiation
27322 like X<T>, so look up the name in the original template. */
27323 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27324 /* If scope has no fields, it can't be a current instantiation. Check this
27325 before currently_open_class to avoid infinite recursion (71515). */
27326 if (!TYPE_FIELDS (scope))
27327 return type;
27328 /* If the SCOPE is not the current instantiation, there's no reason
27329 to look inside it. */
27330 if (only_current_p && !currently_open_class (scope))
27331 return type;
27332 /* Enter the SCOPE so that name lookup will be resolved as if we
27333 were in the class definition. In particular, SCOPE will no
27334 longer be considered a dependent type. */
27335 pushed_scope = push_scope (scope);
27336 /* Look up the declaration. */
27337 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27338 tf_warning_or_error);
27339
27340 result = NULL_TREE;
27341
27342 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27343 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27344 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27345 if (!decl)
27346 /*nop*/;
27347 else if (identifier_p (fullname)
27348 && TREE_CODE (decl) == TYPE_DECL)
27349 {
27350 result = TREE_TYPE (decl);
27351 if (result == error_mark_node)
27352 result = NULL_TREE;
27353 }
27354 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27355 && DECL_CLASS_TEMPLATE_P (decl))
27356 {
27357 /* Obtain the template and the arguments. */
27358 tree tmpl = TREE_OPERAND (fullname, 0);
27359 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27360 {
27361 /* We get here with a plain identifier because a previous tentative
27362 parse of the nested-name-specifier as part of a ptr-operator saw
27363 ::template X<A>. The use of ::template is necessary in a
27364 ptr-operator, but wrong in a declarator-id.
27365
27366 [temp.names]: In a qualified-id of a declarator-id, the keyword
27367 template shall not appear at the top level. */
27368 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27369 "keyword %<template%> not allowed in declarator-id");
27370 tmpl = decl;
27371 }
27372 tree args = TREE_OPERAND (fullname, 1);
27373 /* Instantiate the template. */
27374 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27375 /*entering_scope=*/true,
27376 tf_error | tf_user);
27377 if (result == error_mark_node)
27378 result = NULL_TREE;
27379 }
27380
27381 /* Leave the SCOPE. */
27382 if (pushed_scope)
27383 pop_scope (pushed_scope);
27384
27385 /* If we failed to resolve it, return the original typename. */
27386 if (!result)
27387 return type;
27388
27389 /* If lookup found a typename type, resolve that too. */
27390 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27391 {
27392 /* Ill-formed programs can cause infinite recursion here, so we
27393 must catch that. */
27394 TYPENAME_IS_RESOLVING_P (result) = 1;
27395 result = resolve_typename_type (result, only_current_p);
27396 TYPENAME_IS_RESOLVING_P (result) = 0;
27397 }
27398
27399 /* Qualify the resulting type. */
27400 quals = cp_type_quals (type);
27401 if (quals)
27402 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27403
27404 return result;
27405 }
27406
27407 /* EXPR is an expression which is not type-dependent. Return a proxy
27408 for EXPR that can be used to compute the types of larger
27409 expressions containing EXPR. */
27410
27411 tree
27412 build_non_dependent_expr (tree expr)
27413 {
27414 tree orig_expr = expr;
27415 tree inner_expr;
27416
27417 /* When checking, try to get a constant value for all non-dependent
27418 expressions in order to expose bugs in *_dependent_expression_p
27419 and constexpr. This can affect code generation, see PR70704, so
27420 only do this for -fchecking=2. */
27421 if (flag_checking > 1
27422 && cxx_dialect >= cxx11
27423 /* Don't do this during nsdmi parsing as it can lead to
27424 unexpected recursive instantiations. */
27425 && !parsing_nsdmi ()
27426 /* Don't do this during concept processing either and for
27427 the same reason. */
27428 && !processing_constraint_expression_p ())
27429 fold_non_dependent_expr (expr, tf_none);
27430
27431 STRIP_ANY_LOCATION_WRAPPER (expr);
27432
27433 /* Preserve OVERLOADs; the functions must be available to resolve
27434 types. */
27435 inner_expr = expr;
27436 if (TREE_CODE (inner_expr) == STMT_EXPR)
27437 inner_expr = stmt_expr_value_expr (inner_expr);
27438 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27439 inner_expr = TREE_OPERAND (inner_expr, 0);
27440 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27441 inner_expr = TREE_OPERAND (inner_expr, 1);
27442 if (is_overloaded_fn (inner_expr)
27443 || TREE_CODE (inner_expr) == OFFSET_REF)
27444 return orig_expr;
27445 /* There is no need to return a proxy for a variable or enumerator. */
27446 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27447 return orig_expr;
27448 /* Preserve string constants; conversions from string constants to
27449 "char *" are allowed, even though normally a "const char *"
27450 cannot be used to initialize a "char *". */
27451 if (TREE_CODE (expr) == STRING_CST)
27452 return orig_expr;
27453 /* Preserve void and arithmetic constants, as an optimization -- there is no
27454 reason to create a new node. */
27455 if (TREE_CODE (expr) == VOID_CST
27456 || TREE_CODE (expr) == INTEGER_CST
27457 || TREE_CODE (expr) == REAL_CST)
27458 return orig_expr;
27459 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27460 There is at least one place where we want to know that a
27461 particular expression is a throw-expression: when checking a ?:
27462 expression, there are special rules if the second or third
27463 argument is a throw-expression. */
27464 if (TREE_CODE (expr) == THROW_EXPR)
27465 return orig_expr;
27466
27467 /* Don't wrap an initializer list, we need to be able to look inside. */
27468 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27469 return orig_expr;
27470
27471 /* Don't wrap a dummy object, we need to be able to test for it. */
27472 if (is_dummy_object (expr))
27473 return orig_expr;
27474
27475 if (TREE_CODE (expr) == COND_EXPR)
27476 return build3 (COND_EXPR,
27477 TREE_TYPE (expr),
27478 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27479 (TREE_OPERAND (expr, 1)
27480 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27481 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27482 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27483 if (TREE_CODE (expr) == COMPOUND_EXPR
27484 && !COMPOUND_EXPR_OVERLOADED (expr))
27485 return build2 (COMPOUND_EXPR,
27486 TREE_TYPE (expr),
27487 TREE_OPERAND (expr, 0),
27488 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27489
27490 /* If the type is unknown, it can't really be non-dependent */
27491 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27492
27493 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27494 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27495 TREE_TYPE (expr), expr);
27496 }
27497
27498 /* ARGS is a vector of expressions as arguments to a function call.
27499 Replace the arguments with equivalent non-dependent expressions.
27500 This modifies ARGS in place. */
27501
27502 void
27503 make_args_non_dependent (vec<tree, va_gc> *args)
27504 {
27505 unsigned int ix;
27506 tree arg;
27507
27508 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27509 {
27510 tree newarg = build_non_dependent_expr (arg);
27511 if (newarg != arg)
27512 (*args)[ix] = newarg;
27513 }
27514 }
27515
27516 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27517 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27518 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27519
27520 static tree
27521 make_auto_1 (tree name, bool set_canonical)
27522 {
27523 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27524 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27525 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27526 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27527 (0, processing_template_decl + 1, processing_template_decl + 1,
27528 TYPE_NAME (au), NULL_TREE);
27529 if (set_canonical)
27530 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27531 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27532 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27533 if (name == decltype_auto_identifier)
27534 AUTO_IS_DECLTYPE (au) = true;
27535
27536 return au;
27537 }
27538
27539 tree
27540 make_decltype_auto (void)
27541 {
27542 return make_auto_1 (decltype_auto_identifier, true);
27543 }
27544
27545 tree
27546 make_auto (void)
27547 {
27548 return make_auto_1 (auto_identifier, true);
27549 }
27550
27551 /* Return a C++17 deduction placeholder for class template TMPL. */
27552
27553 tree
27554 make_template_placeholder (tree tmpl)
27555 {
27556 tree t = make_auto_1 (auto_identifier, false);
27557 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27558 /* Our canonical type depends on the placeholder. */
27559 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27560 return t;
27561 }
27562
27563 /* True iff T is a C++17 class template deduction placeholder. */
27564
27565 bool
27566 template_placeholder_p (tree t)
27567 {
27568 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27569 }
27570
27571 /* Make a "constrained auto" type-specifier. This is an auto or
27572 decltype(auto) type with constraints that must be associated after
27573 deduction. The constraint is formed from the given concept CON
27574 and its optional sequence of template arguments ARGS.
27575
27576 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27577
27578 static tree
27579 make_constrained_placeholder_type (tree type, tree con, tree args)
27580 {
27581 /* Build the constraint. */
27582 tree tmpl = DECL_TI_TEMPLATE (con);
27583 tree expr = tmpl;
27584 if (TREE_CODE (con) == FUNCTION_DECL)
27585 expr = ovl_make (tmpl);
27586 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27587
27588 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27589
27590 /* Our canonical type depends on the constraint. */
27591 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27592
27593 /* Attach the constraint to the type declaration. */
27594 return TYPE_NAME (type);
27595 }
27596
27597 /* Make a "constrained auto" type-specifier. */
27598
27599 tree
27600 make_constrained_auto (tree con, tree args)
27601 {
27602 tree type = make_auto_1 (auto_identifier, false);
27603 return make_constrained_placeholder_type (type, con, args);
27604 }
27605
27606 /* Make a "constrained decltype(auto)" type-specifier. */
27607
27608 tree
27609 make_constrained_decltype_auto (tree con, tree args)
27610 {
27611 tree type = make_auto_1 (decltype_auto_identifier, false);
27612 return make_constrained_placeholder_type (type, con, args);
27613 }
27614
27615 /* Build and return a concept definition. Like other templates, the
27616 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27617 the TEMPLATE_DECL. */
27618
27619 tree
27620 finish_concept_definition (cp_expr id, tree init)
27621 {
27622 gcc_assert (identifier_p (id));
27623 gcc_assert (processing_template_decl);
27624
27625 location_t loc = id.get_location();
27626
27627 /* A concept-definition shall not have associated constraints. */
27628 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27629 {
27630 error_at (loc, "a concept cannot be constrained");
27631 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27632 }
27633
27634 /* A concept-definition shall appear in namespace scope. Templates
27635 aren't allowed in block scope, so we only need to check for class
27636 scope. */
27637 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27638 {
27639 error_at (loc, "concept %qE not in namespace scope", *id);
27640 return error_mark_node;
27641 }
27642
27643 /* Initially build the concept declaration; it's type is bool. */
27644 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27645 DECL_CONTEXT (decl) = current_scope ();
27646 DECL_INITIAL (decl) = init;
27647
27648 /* Push the enclosing template. */
27649 return push_template_decl (decl);
27650 }
27651
27652 /* Given type ARG, return std::initializer_list<ARG>. */
27653
27654 static tree
27655 listify (tree arg)
27656 {
27657 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27658
27659 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27660 {
27661 gcc_rich_location richloc (input_location);
27662 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27663 error_at (&richloc,
27664 "deducing from brace-enclosed initializer list"
27665 " requires %<#include <initializer_list>%>");
27666
27667 return error_mark_node;
27668 }
27669 tree argvec = make_tree_vec (1);
27670 TREE_VEC_ELT (argvec, 0) = arg;
27671
27672 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27673 NULL_TREE, 0, tf_warning_or_error);
27674 }
27675
27676 /* Replace auto in TYPE with std::initializer_list<auto>. */
27677
27678 static tree
27679 listify_autos (tree type, tree auto_node)
27680 {
27681 tree init_auto = listify (strip_top_quals (auto_node));
27682 tree argvec = make_tree_vec (1);
27683 TREE_VEC_ELT (argvec, 0) = init_auto;
27684 if (processing_template_decl)
27685 argvec = add_to_template_args (current_template_args (), argvec);
27686 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27687 }
27688
27689 /* Hash traits for hashing possibly constrained 'auto'
27690 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27691
27692 struct auto_hash : default_hash_traits<tree>
27693 {
27694 static inline hashval_t hash (tree);
27695 static inline bool equal (tree, tree);
27696 };
27697
27698 /* Hash the 'auto' T. */
27699
27700 inline hashval_t
27701 auto_hash::hash (tree t)
27702 {
27703 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27704 /* Matching constrained-type-specifiers denote the same template
27705 parameter, so hash the constraint. */
27706 return hash_placeholder_constraint (c);
27707 else
27708 /* But unconstrained autos are all separate, so just hash the pointer. */
27709 return iterative_hash_object (t, 0);
27710 }
27711
27712 /* Compare two 'auto's. */
27713
27714 inline bool
27715 auto_hash::equal (tree t1, tree t2)
27716 {
27717 if (t1 == t2)
27718 return true;
27719
27720 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27721 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27722
27723 /* Two unconstrained autos are distinct. */
27724 if (!c1 || !c2)
27725 return false;
27726
27727 return equivalent_placeholder_constraints (c1, c2);
27728 }
27729
27730 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27731 constrained) auto, add it to the vector. */
27732
27733 static int
27734 extract_autos_r (tree t, void *data)
27735 {
27736 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27737 if (is_auto (t))
27738 {
27739 /* All the autos were built with index 0; fix that up now. */
27740 tree *p = hash.find_slot (t, INSERT);
27741 unsigned idx;
27742 if (*p)
27743 /* If this is a repeated constrained-type-specifier, use the index we
27744 chose before. */
27745 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27746 else
27747 {
27748 /* Otherwise this is new, so use the current count. */
27749 *p = t;
27750 idx = hash.elements () - 1;
27751 }
27752 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27753 }
27754
27755 /* Always keep walking. */
27756 return 0;
27757 }
27758
27759 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27760 says they can appear anywhere in the type. */
27761
27762 static tree
27763 extract_autos (tree type)
27764 {
27765 hash_set<tree> visited;
27766 hash_table<auto_hash> hash (2);
27767
27768 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27769
27770 tree tree_vec = make_tree_vec (hash.elements());
27771 for (hash_table<auto_hash>::iterator iter = hash.begin();
27772 iter != hash.end(); ++iter)
27773 {
27774 tree elt = *iter;
27775 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27776 TREE_VEC_ELT (tree_vec, i)
27777 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27778 }
27779
27780 return tree_vec;
27781 }
27782
27783 /* The stem for deduction guide names. */
27784 const char *const dguide_base = "__dguide_";
27785
27786 /* Return the name for a deduction guide for class template TMPL. */
27787
27788 tree
27789 dguide_name (tree tmpl)
27790 {
27791 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27792 tree tname = TYPE_IDENTIFIER (type);
27793 char *buf = (char *) alloca (1 + strlen (dguide_base)
27794 + IDENTIFIER_LENGTH (tname));
27795 memcpy (buf, dguide_base, strlen (dguide_base));
27796 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27797 IDENTIFIER_LENGTH (tname) + 1);
27798 tree dname = get_identifier (buf);
27799 TREE_TYPE (dname) = type;
27800 return dname;
27801 }
27802
27803 /* True if NAME is the name of a deduction guide. */
27804
27805 bool
27806 dguide_name_p (tree name)
27807 {
27808 return (TREE_CODE (name) == IDENTIFIER_NODE
27809 && TREE_TYPE (name)
27810 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27811 strlen (dguide_base)));
27812 }
27813
27814 /* True if FN is a deduction guide. */
27815
27816 bool
27817 deduction_guide_p (const_tree fn)
27818 {
27819 if (DECL_P (fn))
27820 if (tree name = DECL_NAME (fn))
27821 return dguide_name_p (name);
27822 return false;
27823 }
27824
27825 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27826
27827 bool
27828 copy_guide_p (const_tree fn)
27829 {
27830 gcc_assert (deduction_guide_p (fn));
27831 if (!DECL_ARTIFICIAL (fn))
27832 return false;
27833 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27834 return (TREE_CHAIN (parms) == void_list_node
27835 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27836 }
27837
27838 /* True if FN is a guide generated from a constructor template. */
27839
27840 bool
27841 template_guide_p (const_tree fn)
27842 {
27843 gcc_assert (deduction_guide_p (fn));
27844 if (!DECL_ARTIFICIAL (fn))
27845 return false;
27846 tree tmpl = DECL_TI_TEMPLATE (fn);
27847 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27848 return PRIMARY_TEMPLATE_P (org);
27849 return false;
27850 }
27851
27852 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27853 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27854 template parameter types. Note that the handling of template template
27855 parameters relies on current_template_parms being set appropriately for the
27856 new template. */
27857
27858 static tree
27859 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27860 tree tsubst_args, tsubst_flags_t complain)
27861 {
27862 if (olddecl == error_mark_node)
27863 return error_mark_node;
27864
27865 tree oldidx = get_template_parm_index (olddecl);
27866
27867 tree newtype;
27868 if (TREE_CODE (olddecl) == TYPE_DECL
27869 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27870 {
27871 tree oldtype = TREE_TYPE (olddecl);
27872 newtype = cxx_make_type (TREE_CODE (oldtype));
27873 TYPE_MAIN_VARIANT (newtype) = newtype;
27874 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27875 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27876 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27877 }
27878 else
27879 {
27880 newtype = TREE_TYPE (olddecl);
27881 if (type_uses_auto (newtype))
27882 {
27883 // Substitute once to fix references to other template parameters.
27884 newtype = tsubst (newtype, tsubst_args,
27885 complain|tf_partial, NULL_TREE);
27886 // Now substitute again to reduce the level of the auto.
27887 newtype = tsubst (newtype, current_template_args (),
27888 complain, NULL_TREE);
27889 }
27890 else
27891 newtype = tsubst (newtype, tsubst_args,
27892 complain, NULL_TREE);
27893 }
27894
27895 tree newdecl
27896 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27897 DECL_NAME (olddecl), newtype);
27898 SET_DECL_TEMPLATE_PARM_P (newdecl);
27899
27900 tree newidx;
27901 if (TREE_CODE (olddecl) == TYPE_DECL
27902 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27903 {
27904 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27905 = build_template_parm_index (index, level, level,
27906 newdecl, newtype);
27907 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27908 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27909 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27910 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27911
27912 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27913 {
27914 DECL_TEMPLATE_RESULT (newdecl)
27915 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27916 DECL_NAME (olddecl), newtype);
27917 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27918 // First create a copy (ttargs) of tsubst_args with an
27919 // additional level for the template template parameter's own
27920 // template parameters (ttparms).
27921 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27922 (DECL_TEMPLATE_PARMS (olddecl)));
27923 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27924 tree ttargs = make_tree_vec (depth + 1);
27925 for (int i = 0; i < depth; ++i)
27926 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27927 TREE_VEC_ELT (ttargs, depth)
27928 = template_parms_level_to_args (ttparms);
27929 // Substitute ttargs into ttparms to fix references to
27930 // other template parameters.
27931 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27932 complain|tf_partial);
27933 // Now substitute again with args based on tparms, to reduce
27934 // the level of the ttparms.
27935 ttargs = current_template_args ();
27936 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27937 complain);
27938 // Finally, tack the adjusted parms onto tparms.
27939 ttparms = tree_cons (size_int (depth), ttparms,
27940 current_template_parms);
27941 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27942 }
27943 }
27944 else
27945 {
27946 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27947 tree newconst
27948 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27949 TREE_CODE (oldconst),
27950 DECL_NAME (oldconst), newtype);
27951 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27952 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27953 SET_DECL_TEMPLATE_PARM_P (newconst);
27954 newidx = build_template_parm_index (index, level, level,
27955 newconst, newtype);
27956 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27957 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27958 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27959 }
27960
27961 return newdecl;
27962 }
27963
27964 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
27965 template parameter. */
27966
27967 static tree
27968 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
27969 tree targs, unsigned targs_index, tsubst_flags_t complain)
27970 {
27971 tree olddecl = TREE_VALUE (oldelt);
27972 tree newdecl = rewrite_template_parm (olddecl, index, level,
27973 targs, complain);
27974 if (newdecl == error_mark_node)
27975 return error_mark_node;
27976 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27977 targs, complain, NULL_TREE);
27978 tree list = build_tree_list (newdef, newdecl);
27979 TEMPLATE_PARM_CONSTRAINTS (list)
27980 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27981 targs, complain, NULL_TREE);
27982 int depth = TMPL_ARGS_DEPTH (targs);
27983 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
27984 return list;
27985 }
27986
27987 /* Returns a C++17 class deduction guide template based on the constructor
27988 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27989 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27990 aggregate initialization guide. */
27991
27992 static tree
27993 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
27994 {
27995 tree tparms, targs, fparms, fargs, ci;
27996 bool memtmpl = false;
27997 bool explicit_p;
27998 location_t loc;
27999 tree fn_tmpl = NULL_TREE;
28000
28001 if (outer_args)
28002 {
28003 ++processing_template_decl;
28004 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28005 --processing_template_decl;
28006 }
28007
28008 if (!DECL_DECLARES_FUNCTION_P (ctor))
28009 {
28010 if (TYPE_P (ctor))
28011 {
28012 bool copy_p = TYPE_REF_P (ctor);
28013 if (copy_p)
28014 fparms = tree_cons (NULL_TREE, type, void_list_node);
28015 else
28016 fparms = void_list_node;
28017 }
28018 else if (TREE_CODE (ctor) == TREE_LIST)
28019 fparms = ctor;
28020 else
28021 gcc_unreachable ();
28022
28023 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28024 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28025 targs = CLASSTYPE_TI_ARGS (type);
28026 ci = NULL_TREE;
28027 fargs = NULL_TREE;
28028 loc = DECL_SOURCE_LOCATION (ctmpl);
28029 explicit_p = false;
28030 }
28031 else
28032 {
28033 ++processing_template_decl;
28034 bool ok = true;
28035
28036 fn_tmpl
28037 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28038 : DECL_TI_TEMPLATE (ctor));
28039 if (outer_args)
28040 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28041 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28042
28043 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28044 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28045 fully specialized args for the enclosing class. Strip those off, as
28046 the deduction guide won't have those template parameters. */
28047 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28048 TMPL_PARMS_DEPTH (tparms));
28049 /* Discard the 'this' parameter. */
28050 fparms = FUNCTION_ARG_CHAIN (ctor);
28051 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28052 ci = get_constraints (ctor);
28053 loc = DECL_SOURCE_LOCATION (ctor);
28054 explicit_p = DECL_NONCONVERTING_P (ctor);
28055
28056 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28057 {
28058 memtmpl = true;
28059
28060 /* For a member template constructor, we need to flatten the two
28061 template parameter lists into one, and then adjust the function
28062 signature accordingly. This gets...complicated. */
28063 tree save_parms = current_template_parms;
28064
28065 /* For a member template we should have two levels of parms/args, one
28066 for the class and one for the constructor. We stripped
28067 specialized args for further enclosing classes above. */
28068 const int depth = 2;
28069 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28070
28071 /* Template args for translating references to the two-level template
28072 parameters into references to the one-level template parameters we
28073 are creating. */
28074 tree tsubst_args = copy_node (targs);
28075 TMPL_ARGS_LEVEL (tsubst_args, depth)
28076 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28077
28078 /* Template parms for the constructor template. */
28079 tree ftparms = TREE_VALUE (tparms);
28080 unsigned flen = TREE_VEC_LENGTH (ftparms);
28081 /* Template parms for the class template. */
28082 tparms = TREE_CHAIN (tparms);
28083 tree ctparms = TREE_VALUE (tparms);
28084 unsigned clen = TREE_VEC_LENGTH (ctparms);
28085 /* Template parms for the deduction guide start as a copy of the
28086 template parms for the class. We set current_template_parms for
28087 lookup_template_class_1. */
28088 current_template_parms = tparms = copy_node (tparms);
28089 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28090 for (unsigned i = 0; i < clen; ++i)
28091 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28092
28093 /* Now we need to rewrite the constructor parms to append them to the
28094 class parms. */
28095 for (unsigned i = 0; i < flen; ++i)
28096 {
28097 unsigned index = i + clen;
28098 unsigned level = 1;
28099 tree oldelt = TREE_VEC_ELT (ftparms, i);
28100 tree newelt
28101 = rewrite_tparm_list (oldelt, index, level,
28102 tsubst_args, i, complain);
28103 if (newelt == error_mark_node)
28104 ok = false;
28105 TREE_VEC_ELT (new_vec, index) = newelt;
28106 }
28107
28108 /* Now we have a final set of template parms to substitute into the
28109 function signature. */
28110 targs = template_parms_to_args (tparms);
28111 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28112 complain, ctor);
28113 if (fparms == error_mark_node)
28114 ok = false;
28115 if (ci)
28116 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28117
28118 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28119 cp_unevaluated_operand. */
28120 cp_evaluated ev;
28121 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28122 current_template_parms = save_parms;
28123 }
28124
28125 --processing_template_decl;
28126 if (!ok)
28127 return error_mark_node;
28128 }
28129
28130 if (!memtmpl)
28131 {
28132 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28133 tparms = copy_node (tparms);
28134 INNERMOST_TEMPLATE_PARMS (tparms)
28135 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28136 }
28137
28138 tree fntype = build_function_type (type, fparms);
28139 tree ded_fn = build_lang_decl_loc (loc,
28140 FUNCTION_DECL,
28141 dguide_name (type), fntype);
28142 DECL_ARGUMENTS (ded_fn) = fargs;
28143 DECL_ARTIFICIAL (ded_fn) = true;
28144 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28145 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28146 DECL_ARTIFICIAL (ded_tmpl) = true;
28147 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
28148 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
28149 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28150 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28151 if (DECL_P (ctor))
28152 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28153 if (ci)
28154 set_constraints (ded_tmpl, ci);
28155
28156 return ded_tmpl;
28157 }
28158
28159 /* Add to LIST the member types for the reshaped initializer CTOR. */
28160
28161 static tree
28162 collect_ctor_idx_types (tree ctor, tree list)
28163 {
28164 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28165 tree idx, val; unsigned i;
28166 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28167 {
28168 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28169 && CONSTRUCTOR_NELTS (val))
28170 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
28171 if (TREE_CODE (subidx) == FIELD_DECL)
28172 {
28173 list = collect_ctor_idx_types (val, list);
28174 continue;
28175 }
28176 tree ftype = finish_decltype_type (idx, true, tf_none);
28177 list = tree_cons (NULL_TREE, ftype, list);
28178 }
28179
28180 return list;
28181 }
28182
28183 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28184
28185 static bool
28186 is_spec_or_derived (tree etype, tree tmpl)
28187 {
28188 if (!etype || !CLASS_TYPE_P (etype))
28189 return false;
28190
28191 tree type = TREE_TYPE (tmpl);
28192 tree tparms = (INNERMOST_TEMPLATE_PARMS
28193 (DECL_TEMPLATE_PARMS (tmpl)));
28194 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28195 int err = unify (tparms, targs, type, etype,
28196 UNIFY_ALLOW_DERIVED, /*explain*/false);
28197 ggc_free (targs);
28198 return !err;
28199 }
28200
28201 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28202 INIT. */
28203
28204 static tree
28205 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28206 {
28207 if (cxx_dialect < cxx2a)
28208 return NULL_TREE;
28209
28210 if (init == NULL_TREE)
28211 return NULL_TREE;
28212
28213 tree type = TREE_TYPE (tmpl);
28214 if (!CP_AGGREGATE_TYPE_P (type))
28215 return NULL_TREE;
28216
28217 /* No aggregate candidate for copy-initialization. */
28218 if (args->length() == 1)
28219 {
28220 tree val = (*args)[0];
28221 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28222 return NULL_TREE;
28223 }
28224
28225 /* If we encounter a problem, we just won't add the candidate. */
28226 tsubst_flags_t complain = tf_none;
28227
28228 tree parms = NULL_TREE;
28229 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28230 {
28231 init = reshape_init (type, init, complain);
28232 if (init == error_mark_node)
28233 return NULL_TREE;
28234 parms = collect_ctor_idx_types (init, parms);
28235 }
28236 else if (TREE_CODE (init) == TREE_LIST)
28237 {
28238 int len = list_length (init);
28239 for (tree field = TYPE_FIELDS (type);
28240 len;
28241 --len, field = DECL_CHAIN (field))
28242 {
28243 field = next_initializable_field (field);
28244 if (!field)
28245 return NULL_TREE;
28246 tree ftype = finish_decltype_type (field, true, complain);
28247 parms = tree_cons (NULL_TREE, ftype, parms);
28248 }
28249 }
28250 else
28251 /* Aggregate initialization doesn't apply to an initializer expression. */
28252 return NULL_TREE;
28253
28254 if (parms)
28255 {
28256 tree last = parms;
28257 parms = nreverse (parms);
28258 TREE_CHAIN (last) = void_list_node;
28259 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28260 return guide;
28261 }
28262
28263 return NULL_TREE;
28264 }
28265
28266 /* UGUIDES are the deduction guides for the underlying template of alias
28267 template TMPL; adjust them to be deduction guides for TMPL. */
28268
28269 static tree
28270 alias_ctad_tweaks (tree tmpl, tree uguides)
28271 {
28272 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28273 class type (9.2.8.2) where the template-name names an alias template A,
28274 the defining-type-id of A must be of the form
28275
28276 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28277
28278 as specified in 9.2.8.2. The guides of A are the set of functions or
28279 function templates formed as follows. For each function or function
28280 template f in the guides of the template named by the simple-template-id
28281 of the defining-type-id, the template arguments of the return type of f
28282 are deduced from the defining-type-id of A according to the process in
28283 13.10.2.5 with the exception that deduction does not fail if not all
28284 template arguments are deduced. Let g denote the result of substituting
28285 these deductions into f. If substitution succeeds, form a function or
28286 function template f' with the following properties and add it to the set
28287 of guides of A:
28288
28289 * The function type of f' is the function type of g.
28290
28291 * If f is a function template, f' is a function template whose template
28292 parameter list consists of all the template parameters of A (including
28293 their default template arguments) that appear in the above deductions or
28294 (recursively) in their default template arguments, followed by the
28295 template parameters of f that were not deduced (including their default
28296 template arguments), otherwise f' is not a function template.
28297
28298 * The associated constraints (13.5.2) are the conjunction of the
28299 associated constraints of g and a constraint that is satisfied if and only
28300 if the arguments of A are deducible (see below) from the return type.
28301
28302 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28303 be so as well.
28304
28305 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28306 considered to be so as well.
28307
28308 * The explicit-specifier of f' is the explicit-specifier of g (if
28309 any). */
28310
28311 /* This implementation differs from the above in two significant ways:
28312
28313 1) We include all template parameters of A, not just some.
28314 2) The added constraint is same_type instead of deducible.
28315
28316 I believe that while it's probably possible to construct a testcase that
28317 behaves differently with this simplification, it should have the same
28318 effect for real uses. Including all template parameters means that we
28319 deduce all parameters of A when resolving the call, so when we're in the
28320 constraint we don't need to deduce them again, we can just check whether
28321 the deduction produced the desired result. */
28322
28323 tsubst_flags_t complain = tf_warning_or_error;
28324 tree atype = TREE_TYPE (tmpl);
28325 tree aguides = NULL_TREE;
28326 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28327 unsigned natparms = TREE_VEC_LENGTH (atparms);
28328 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28329 for (ovl_iterator iter (uguides); iter; ++iter)
28330 {
28331 tree f = *iter;
28332 tree in_decl = f;
28333 location_t loc = DECL_SOURCE_LOCATION (f);
28334 tree ret = TREE_TYPE (TREE_TYPE (f));
28335 tree fprime = f;
28336 if (TREE_CODE (f) == TEMPLATE_DECL)
28337 {
28338 processing_template_decl_sentinel ptds (/*reset*/false);
28339 ++processing_template_decl;
28340
28341 /* Deduce template arguments for f from the type-id of A. */
28342 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28343 unsigned len = TREE_VEC_LENGTH (ftparms);
28344 tree targs = make_tree_vec (len);
28345 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28346 gcc_assert (!err);
28347
28348 /* The number of parms for f' is the number of parms for A plus
28349 non-deduced parms of f. */
28350 unsigned ndlen = 0;
28351 unsigned j;
28352 for (unsigned i = 0; i < len; ++i)
28353 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28354 ++ndlen;
28355 tree gtparms = make_tree_vec (natparms + ndlen);
28356
28357 /* First copy over the parms of A. */
28358 for (j = 0; j < natparms; ++j)
28359 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28360 /* Now rewrite the non-deduced parms of f. */
28361 for (unsigned i = 0; ndlen && i < len; ++i)
28362 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28363 {
28364 --ndlen;
28365 unsigned index = j++;
28366 unsigned level = 1;
28367 tree oldlist = TREE_VEC_ELT (ftparms, i);
28368 tree list = rewrite_tparm_list (oldlist, index, level,
28369 targs, i, complain);
28370 TREE_VEC_ELT (gtparms, index) = list;
28371 }
28372 gtparms = build_tree_list (size_one_node, gtparms);
28373
28374 /* Substitute the deduced arguments plus the rewritten template
28375 parameters into f to get g. This covers the type, copyness,
28376 guideness, and explicit-specifier. */
28377 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28378 if (g == error_mark_node)
28379 return error_mark_node;
28380 DECL_USE_TEMPLATE (g) = 0;
28381 fprime = build_template_decl (g, gtparms, false);
28382 DECL_TEMPLATE_RESULT (fprime) = g;
28383 TREE_TYPE (fprime) = TREE_TYPE (g);
28384 tree gtargs = template_parms_to_args (gtparms);
28385 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28386 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28387
28388 /* Substitute the associated constraints. */
28389 tree ci = get_constraints (f);
28390 if (ci)
28391 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28392 if (ci == error_mark_node)
28393 return error_mark_node;
28394
28395 /* Add a constraint that the return type matches the instantiation of
28396 A with the same template arguments. */
28397 ret = TREE_TYPE (TREE_TYPE (fprime));
28398 if (!same_type_p (atype, ret)
28399 /* FIXME this should mean they don't compare as equivalent. */
28400 || dependent_alias_template_spec_p (atype, nt_opaque))
28401 {
28402 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28403 ci = append_constraint (ci, same);
28404 }
28405
28406 if (ci)
28407 set_constraints (fprime, ci);
28408 }
28409 else
28410 {
28411 /* For a non-template deduction guide, if the arguments of A aren't
28412 deducible from the return type, don't add the candidate. */
28413 tree targs = make_tree_vec (natparms);
28414 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28415 for (unsigned i = 0; !err && i < natparms; ++i)
28416 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28417 err = true;
28418 if (err)
28419 continue;
28420 }
28421
28422 aguides = lookup_add (fprime, aguides);
28423 }
28424
28425 return aguides;
28426 }
28427
28428 /* Return artificial deduction guides built from the constructors of class
28429 template TMPL. */
28430
28431 static tree
28432 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28433 {
28434 tree type = TREE_TYPE (tmpl);
28435 tree outer_args = NULL_TREE;
28436 if (DECL_CLASS_SCOPE_P (tmpl)
28437 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28438 {
28439 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28440 type = TREE_TYPE (most_general_template (tmpl));
28441 }
28442
28443 tree cands = NULL_TREE;
28444
28445 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28446 {
28447 /* Skip inherited constructors. */
28448 if (iter.using_p ())
28449 continue;
28450
28451 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28452 cands = lookup_add (guide, cands);
28453 }
28454
28455 /* Add implicit default constructor deduction guide. */
28456 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28457 {
28458 tree guide = build_deduction_guide (type, type, outer_args,
28459 complain);
28460 cands = lookup_add (guide, cands);
28461 }
28462
28463 /* Add copy guide. */
28464 {
28465 tree gtype = build_reference_type (type);
28466 tree guide = build_deduction_guide (type, gtype, outer_args,
28467 complain);
28468 cands = lookup_add (guide, cands);
28469 }
28470
28471 return cands;
28472 }
28473
28474 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28475
28476 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28477 aggregate candidate is added separately because it depends on the
28478 initializer. */
28479
28480 static tree
28481 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28482 {
28483 tree guides = NULL_TREE;
28484 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28485 {
28486 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28487 tree tinfo = get_template_info (under);
28488 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28489 }
28490 else
28491 {
28492 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28493 dguide_name (tmpl),
28494 /*type*/false, /*complain*/false,
28495 /*hidden*/false);
28496 if (guides == error_mark_node)
28497 guides = NULL_TREE;
28498 }
28499
28500 /* Cache the deduction guides for a template. We also remember the result of
28501 lookup, and rebuild everything if it changes; should be very rare. */
28502 tree_pair_p cache = NULL;
28503 if (tree_pair_p &r
28504 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28505 {
28506 cache = r;
28507 if (cache->purpose == guides)
28508 return cache->value;
28509 }
28510 else
28511 {
28512 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28513 cache->purpose = guides;
28514 }
28515
28516 tree cands = NULL_TREE;
28517 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28518 cands = alias_ctad_tweaks (tmpl, guides);
28519 else
28520 {
28521 cands = ctor_deduction_guides_for (tmpl, complain);
28522 for (ovl_iterator it (guides); it; ++it)
28523 cands = lookup_add (*it, cands);
28524 }
28525
28526 cache->value = cands;
28527 return cands;
28528 }
28529
28530 /* Return whether TMPL is a (class template argument-) deducible template. */
28531
28532 bool
28533 ctad_template_p (tree tmpl)
28534 {
28535 /* A deducible template is either a class template or is an alias template
28536 whose defining-type-id is of the form
28537
28538 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28539
28540 where the nested-name-specifier (if any) is non-dependent and the
28541 template-name of the simple-template-id names a deducible template. */
28542
28543 if (DECL_CLASS_TEMPLATE_P (tmpl)
28544 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28545 return true;
28546 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28547 return false;
28548 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28549 if (tree tinfo = get_template_info (orig))
28550 return ctad_template_p (TI_TEMPLATE (tinfo));
28551 return false;
28552 }
28553
28554 /* Deduce template arguments for the class template placeholder PTYPE for
28555 template TMPL based on the initializer INIT, and return the resulting
28556 type. */
28557
28558 static tree
28559 do_class_deduction (tree ptype, tree tmpl, tree init,
28560 int flags, tsubst_flags_t complain)
28561 {
28562 /* We should have handled this in the caller. */
28563 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28564 return ptype;
28565
28566 /* Look through alias templates that just rename another template. */
28567 tmpl = get_underlying_template (tmpl);
28568 if (!ctad_template_p (tmpl))
28569 {
28570 if (complain & tf_error)
28571 error ("non-deducible template %qT used without template arguments", tmpl);
28572 return error_mark_node;
28573 }
28574 else if (cxx_dialect < cxx2a && DECL_ALIAS_TEMPLATE_P (tmpl))
28575 {
28576 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28577 if (complain & tf_warning_or_error)
28578 pedwarn (input_location, 0, "alias template deduction only available "
28579 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28580 }
28581
28582 if (init && TREE_TYPE (init) == ptype)
28583 /* Using the template parm as its own argument. */
28584 return ptype;
28585
28586 tree type = TREE_TYPE (tmpl);
28587
28588 bool try_list_ctor = false;
28589
28590 releasing_vec rv_args = NULL;
28591 vec<tree,va_gc> *&args = *&rv_args;
28592 if (init == NULL_TREE)
28593 args = make_tree_vector ();
28594 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28595 {
28596 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28597 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28598 {
28599 /* As an exception, the first phase in 16.3.1.7 (considering the
28600 initializer list as a single argument) is omitted if the
28601 initializer list consists of a single expression of type cv U,
28602 where U is a specialization of C or a class derived from a
28603 specialization of C. */
28604 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28605 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28606 try_list_ctor = false;
28607 }
28608 if (try_list_ctor || is_std_init_list (type))
28609 args = make_tree_vector_single (init);
28610 else
28611 args = make_tree_vector_from_ctor (init);
28612 }
28613 else if (TREE_CODE (init) == TREE_LIST)
28614 args = make_tree_vector_from_list (init);
28615 else
28616 args = make_tree_vector_single (init);
28617
28618 /* Do this now to avoid problems with erroneous args later on. */
28619 args = resolve_args (args, complain);
28620 if (args == NULL)
28621 return error_mark_node;
28622
28623 tree cands = deduction_guides_for (tmpl, complain);
28624 if (cands == error_mark_node)
28625 return error_mark_node;
28626
28627 /* Prune explicit deduction guides in copy-initialization context. */
28628 bool elided = false;
28629 if (flags & LOOKUP_ONLYCONVERTING)
28630 {
28631 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28632 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28633 elided = true;
28634
28635 if (elided)
28636 {
28637 /* Found a nonconverting guide, prune the candidates. */
28638 tree pruned = NULL_TREE;
28639 for (lkp_iterator iter (cands); iter; ++iter)
28640 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28641 pruned = lookup_add (*iter, pruned);
28642
28643 cands = pruned;
28644 }
28645 }
28646
28647 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28648 cands = lookup_add (guide, cands);
28649
28650 tree call = error_mark_node;
28651
28652 /* If this is list-initialization and the class has a list constructor, first
28653 try deducing from the list as a single argument, as [over.match.list]. */
28654 tree list_cands = NULL_TREE;
28655 if (try_list_ctor && cands)
28656 for (lkp_iterator iter (cands); iter; ++iter)
28657 {
28658 tree dg = *iter;
28659 if (is_list_ctor (dg))
28660 list_cands = lookup_add (dg, list_cands);
28661 }
28662 if (list_cands)
28663 {
28664 ++cp_unevaluated_operand;
28665 call = build_new_function_call (list_cands, &args, tf_decltype);
28666 --cp_unevaluated_operand;
28667
28668 if (call == error_mark_node)
28669 {
28670 /* That didn't work, now try treating the list as a sequence of
28671 arguments. */
28672 release_tree_vector (args);
28673 args = make_tree_vector_from_ctor (init);
28674 }
28675 }
28676
28677 if (elided && !cands)
28678 {
28679 error ("cannot deduce template arguments for copy-initialization"
28680 " of %qT, as it has no non-explicit deduction guides or "
28681 "user-declared constructors", type);
28682 return error_mark_node;
28683 }
28684 else if (!cands && call == error_mark_node)
28685 {
28686 error ("cannot deduce template arguments of %qT, as it has no viable "
28687 "deduction guides", type);
28688 return error_mark_node;
28689 }
28690
28691 if (call == error_mark_node)
28692 {
28693 ++cp_unevaluated_operand;
28694 call = build_new_function_call (cands, &args, tf_decltype);
28695 --cp_unevaluated_operand;
28696 }
28697
28698 if (call == error_mark_node
28699 && (complain & tf_warning_or_error))
28700 {
28701 error ("class template argument deduction failed:");
28702
28703 ++cp_unevaluated_operand;
28704 call = build_new_function_call (cands, &args, complain | tf_decltype);
28705 --cp_unevaluated_operand;
28706
28707 if (elided)
28708 inform (input_location, "explicit deduction guides not considered "
28709 "for copy-initialization");
28710 }
28711
28712 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28713 }
28714
28715 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28716 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28717 The CONTEXT determines the context in which auto deduction is performed
28718 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28719 OUTER_TARGS are used during template argument deduction
28720 (context == adc_unify) to properly substitute the result, and is ignored
28721 in other contexts.
28722
28723 For partial-concept-ids, extra args may be appended to the list of deduced
28724 template arguments prior to determining constraint satisfaction. */
28725
28726 tree
28727 do_auto_deduction (tree type, tree init, tree auto_node,
28728 tsubst_flags_t complain, auto_deduction_context context,
28729 tree outer_targs, int flags)
28730 {
28731 tree targs;
28732
28733 if (init == error_mark_node)
28734 return error_mark_node;
28735
28736 if (init && type_dependent_expression_p (init)
28737 && context != adc_unify)
28738 /* Defining a subset of type-dependent expressions that we can deduce
28739 from ahead of time isn't worth the trouble. */
28740 return type;
28741
28742 /* Similarly, we can't deduce from another undeduced decl. */
28743 if (init && undeduced_auto_decl (init))
28744 return type;
28745
28746 /* We may be doing a partial substitution, but we still want to replace
28747 auto_node. */
28748 complain &= ~tf_partial;
28749
28750 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28751 /* C++17 class template argument deduction. */
28752 return do_class_deduction (type, tmpl, init, flags, complain);
28753
28754 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28755 /* Nothing we can do with this, even in deduction context. */
28756 return type;
28757
28758 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28759 with either a new invented type template parameter U or, if the
28760 initializer is a braced-init-list (8.5.4), with
28761 std::initializer_list<U>. */
28762 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28763 {
28764 if (!DIRECT_LIST_INIT_P (init))
28765 type = listify_autos (type, auto_node);
28766 else if (CONSTRUCTOR_NELTS (init) == 1)
28767 init = CONSTRUCTOR_ELT (init, 0)->value;
28768 else
28769 {
28770 if (complain & tf_warning_or_error)
28771 {
28772 if (permerror (input_location, "direct-list-initialization of "
28773 "%<auto%> requires exactly one element"))
28774 inform (input_location,
28775 "for deduction to %<std::initializer_list%>, use copy-"
28776 "list-initialization (i.e. add %<=%> before the %<{%>)");
28777 }
28778 type = listify_autos (type, auto_node);
28779 }
28780 }
28781
28782 if (type == error_mark_node)
28783 return error_mark_node;
28784
28785 init = resolve_nondeduced_context (init, complain);
28786
28787 if (context == adc_decomp_type
28788 && auto_node == type
28789 && init != error_mark_node
28790 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28791 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28792 and initializer has array type, deduce cv-qualified array type. */
28793 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28794 complain);
28795 else if (AUTO_IS_DECLTYPE (auto_node))
28796 {
28797 tree stripped_init = tree_strip_any_location_wrapper (init);
28798 bool id = (DECL_P (stripped_init)
28799 || ((TREE_CODE (init) == COMPONENT_REF
28800 || TREE_CODE (init) == SCOPE_REF)
28801 && !REF_PARENTHESIZED_P (init)));
28802 targs = make_tree_vec (1);
28803 TREE_VEC_ELT (targs, 0)
28804 = finish_decltype_type (init, id, tf_warning_or_error);
28805 if (type != auto_node)
28806 {
28807 if (complain & tf_error)
28808 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28809 return error_mark_node;
28810 }
28811 }
28812 else
28813 {
28814 if (error_operand_p (init))
28815 return error_mark_node;
28816
28817 tree parms = build_tree_list (NULL_TREE, type);
28818 tree tparms;
28819
28820 if (flag_concepts)
28821 tparms = extract_autos (type);
28822 else
28823 {
28824 tparms = make_tree_vec (1);
28825 TREE_VEC_ELT (tparms, 0)
28826 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28827 }
28828
28829 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28830 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28831 DEDUCE_CALL,
28832 NULL, /*explain_p=*/false);
28833 if (val > 0)
28834 {
28835 if (processing_template_decl)
28836 /* Try again at instantiation time. */
28837 return type;
28838 if (type && type != error_mark_node
28839 && (complain & tf_error))
28840 /* If type is error_mark_node a diagnostic must have been
28841 emitted by now. Also, having a mention to '<type error>'
28842 in the diagnostic is not really useful to the user. */
28843 {
28844 if (cfun
28845 && FNDECL_USED_AUTO (current_function_decl)
28846 && (auto_node
28847 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28848 && LAMBDA_FUNCTION_P (current_function_decl))
28849 error ("unable to deduce lambda return type from %qE", init);
28850 else
28851 error ("unable to deduce %qT from %qE", type, init);
28852 type_unification_real (tparms, targs, parms, &init, 1, 0,
28853 DEDUCE_CALL,
28854 NULL, /*explain_p=*/true);
28855 }
28856 return error_mark_node;
28857 }
28858 }
28859
28860 /* Check any placeholder constraints against the deduced type. */
28861 if (flag_concepts && !processing_template_decl)
28862 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28863 {
28864 /* Use the deduced type to check the associated constraints. If we
28865 have a partial-concept-id, rebuild the argument list so that
28866 we check using the extra arguments. */
28867 check = unpack_concept_check (check);
28868 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28869 tree cdecl = TREE_OPERAND (check, 0);
28870 if (OVL_P (cdecl))
28871 cdecl = OVL_FIRST (cdecl);
28872 tree cargs = TREE_OPERAND (check, 1);
28873 if (TREE_VEC_LENGTH (cargs) > 1)
28874 {
28875 cargs = copy_node (cargs);
28876 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28877 }
28878 else
28879 cargs = targs;
28880
28881 /* Rebuild the check using the deduced arguments. */
28882 check = build_concept_check (cdecl, cargs, tf_none);
28883
28884 if (!constraints_satisfied_p (check))
28885 {
28886 if (complain & tf_warning_or_error)
28887 {
28888 auto_diagnostic_group d;
28889 switch (context)
28890 {
28891 case adc_unspecified:
28892 case adc_unify:
28893 error("placeholder constraints not satisfied");
28894 break;
28895 case adc_variable_type:
28896 case adc_decomp_type:
28897 error ("deduced initializer does not satisfy "
28898 "placeholder constraints");
28899 break;
28900 case adc_return_type:
28901 error ("deduced return type does not satisfy "
28902 "placeholder constraints");
28903 break;
28904 case adc_requirement:
28905 error ("deduced expression type does not satisfy "
28906 "placeholder constraints");
28907 break;
28908 }
28909 diagnose_constraints (input_location, check, targs);
28910 }
28911 return error_mark_node;
28912 }
28913 }
28914
28915 if (processing_template_decl && context != adc_unify)
28916 outer_targs = current_template_args ();
28917 targs = add_to_template_args (outer_targs, targs);
28918 return tsubst (type, targs, complain, NULL_TREE);
28919 }
28920
28921 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28922 result. */
28923
28924 tree
28925 splice_late_return_type (tree type, tree late_return_type)
28926 {
28927 if (late_return_type)
28928 {
28929 gcc_assert (is_auto (type) || seen_error ());
28930 return late_return_type;
28931 }
28932
28933 if (tree *auto_node = find_type_usage (&type, is_auto))
28934 {
28935 tree idx = get_template_parm_index (*auto_node);
28936 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
28937 /* In an abbreviated function template we didn't know we were dealing
28938 with a function template when we saw the auto return type, so update
28939 it to have the correct level. */
28940 *auto_node = make_auto_1 (TYPE_IDENTIFIER (*auto_node), true);
28941 }
28942 return type;
28943 }
28944
28945 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28946 'decltype(auto)' or a deduced class template. */
28947
28948 bool
28949 is_auto (const_tree type)
28950 {
28951 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28952 && (TYPE_IDENTIFIER (type) == auto_identifier
28953 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
28954 return true;
28955 else
28956 return false;
28957 }
28958
28959 /* for_each_template_parm callback for type_uses_auto. */
28960
28961 int
28962 is_auto_r (tree tp, void */*data*/)
28963 {
28964 return is_auto (tp);
28965 }
28966
28967 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28968 a use of `auto'. Returns NULL_TREE otherwise. */
28969
28970 tree
28971 type_uses_auto (tree type)
28972 {
28973 if (type == NULL_TREE)
28974 return NULL_TREE;
28975 else if (flag_concepts)
28976 {
28977 /* The Concepts TS allows multiple autos in one type-specifier; just
28978 return the first one we find, do_auto_deduction will collect all of
28979 them. */
28980 if (uses_template_parms (type))
28981 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
28982 /*visited*/NULL, /*nondeduced*/false);
28983 else
28984 return NULL_TREE;
28985 }
28986 else if (tree *tp = find_type_usage (&type, is_auto))
28987 return *tp;
28988 else
28989 return NULL_TREE;
28990 }
28991
28992 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28993 concepts are enabled, auto is acceptable in template arguments, but
28994 only when TEMPL identifies a template class. Return TRUE if any
28995 such errors were reported. */
28996
28997 bool
28998 check_auto_in_tmpl_args (tree tmpl, tree args)
28999 {
29000 /* If there were previous errors, nevermind. */
29001 if (!args || TREE_CODE (args) != TREE_VEC)
29002 return false;
29003
29004 /* If TMPL is an identifier, we're parsing and we can't tell yet
29005 whether TMPL is supposed to be a type, a function or a variable.
29006 We'll only be able to tell during template substitution, so we
29007 expect to be called again then. If concepts are enabled and we
29008 know we have a type, we're ok. */
29009 if (flag_concepts
29010 && (identifier_p (tmpl)
29011 || (DECL_P (tmpl)
29012 && (DECL_TYPE_TEMPLATE_P (tmpl)
29013 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29014 return false;
29015
29016 /* Quickly search for any occurrences of auto; usually there won't
29017 be any, and then we'll avoid allocating the vector. */
29018 if (!type_uses_auto (args))
29019 return false;
29020
29021 bool errors = false;
29022
29023 tree vec = extract_autos (args);
29024 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29025 {
29026 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29027 error_at (DECL_SOURCE_LOCATION (xauto),
29028 "invalid use of %qT in template argument", xauto);
29029 errors = true;
29030 }
29031
29032 return errors;
29033 }
29034
29035 /* For a given template T, return the vector of typedefs referenced
29036 in T for which access check is needed at T instantiation time.
29037 T is either a FUNCTION_DECL or a RECORD_TYPE.
29038 Those typedefs were added to T by the function
29039 append_type_to_template_for_access_check. */
29040
29041 vec<qualified_typedef_usage_t, va_gc> *
29042 get_types_needing_access_check (tree t)
29043 {
29044 tree ti;
29045 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
29046
29047 if (!t || t == error_mark_node)
29048 return NULL;
29049
29050 if (!(ti = get_template_info (t)))
29051 return NULL;
29052
29053 if (CLASS_TYPE_P (t)
29054 || TREE_CODE (t) == FUNCTION_DECL)
29055 {
29056 if (!TI_TEMPLATE (ti))
29057 return NULL;
29058
29059 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
29060 }
29061
29062 return result;
29063 }
29064
29065 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29066 tied to T. That list of typedefs will be access checked at
29067 T instantiation time.
29068 T is either a FUNCTION_DECL or a RECORD_TYPE.
29069 TYPE_DECL is a TYPE_DECL node representing a typedef.
29070 SCOPE is the scope through which TYPE_DECL is accessed.
29071 LOCATION is the location of the usage point of TYPE_DECL.
29072
29073 This function is a subroutine of
29074 append_type_to_template_for_access_check. */
29075
29076 static void
29077 append_type_to_template_for_access_check_1 (tree t,
29078 tree type_decl,
29079 tree scope,
29080 location_t location)
29081 {
29082 qualified_typedef_usage_t typedef_usage;
29083 tree ti;
29084
29085 if (!t || t == error_mark_node)
29086 return;
29087
29088 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
29089 || CLASS_TYPE_P (t))
29090 && type_decl
29091 && TREE_CODE (type_decl) == TYPE_DECL
29092 && scope);
29093
29094 if (!(ti = get_template_info (t)))
29095 return;
29096
29097 gcc_assert (TI_TEMPLATE (ti));
29098
29099 typedef_usage.typedef_decl = type_decl;
29100 typedef_usage.context = scope;
29101 typedef_usage.locus = location;
29102
29103 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
29104 }
29105
29106 /* Append TYPE_DECL to the template TEMPL.
29107 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29108 At TEMPL instanciation time, TYPE_DECL will be checked to see
29109 if it can be accessed through SCOPE.
29110 LOCATION is the location of the usage point of TYPE_DECL.
29111
29112 e.g. consider the following code snippet:
29113
29114 class C
29115 {
29116 typedef int myint;
29117 };
29118
29119 template<class U> struct S
29120 {
29121 C::myint mi; // <-- usage point of the typedef C::myint
29122 };
29123
29124 S<char> s;
29125
29126 At S<char> instantiation time, we need to check the access of C::myint
29127 In other words, we need to check the access of the myint typedef through
29128 the C scope. For that purpose, this function will add the myint typedef
29129 and the scope C through which its being accessed to a list of typedefs
29130 tied to the template S. That list will be walked at template instantiation
29131 time and access check performed on each typedefs it contains.
29132 Note that this particular code snippet should yield an error because
29133 myint is private to C. */
29134
29135 void
29136 append_type_to_template_for_access_check (tree templ,
29137 tree type_decl,
29138 tree scope,
29139 location_t location)
29140 {
29141 qualified_typedef_usage_t *iter;
29142 unsigned i;
29143
29144 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
29145
29146 /* Make sure we don't append the type to the template twice. */
29147 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
29148 if (iter->typedef_decl == type_decl && scope == iter->context)
29149 return;
29150
29151 append_type_to_template_for_access_check_1 (templ, type_decl,
29152 scope, location);
29153 }
29154
29155 /* Recursively walk over && expressions searching for EXPR. Return a reference
29156 to that expression. */
29157
29158 static tree *find_template_requirement (tree *t, tree key)
29159 {
29160 if (*t == key)
29161 return t;
29162 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29163 {
29164 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29165 return p;
29166 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29167 return p;
29168 }
29169 return 0;
29170 }
29171
29172 /* Convert the generic type parameters in PARM that match the types given in the
29173 range [START_IDX, END_IDX) from the current_template_parms into generic type
29174 packs. */
29175
29176 tree
29177 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29178 {
29179 tree current = current_template_parms;
29180 int depth = TMPL_PARMS_DEPTH (current);
29181 current = INNERMOST_TEMPLATE_PARMS (current);
29182 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29183
29184 for (int i = 0; i < start_idx; ++i)
29185 TREE_VEC_ELT (replacement, i)
29186 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29187
29188 for (int i = start_idx; i < end_idx; ++i)
29189 {
29190 /* Create a distinct parameter pack type from the current parm and add it
29191 to the replacement args to tsubst below into the generic function
29192 parameter. */
29193 tree node = TREE_VEC_ELT (current, i);
29194 tree o = TREE_TYPE (TREE_VALUE (node));
29195 tree t = copy_type (o);
29196 TEMPLATE_TYPE_PARM_INDEX (t)
29197 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29198 t, 0, 0, tf_none);
29199 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29200 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29201 TYPE_MAIN_VARIANT (t) = t;
29202 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29203 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29204 TREE_VEC_ELT (replacement, i) = t;
29205
29206 /* Replace the current template parameter with new pack. */
29207 TREE_VALUE (node) = TREE_CHAIN (t);
29208
29209 /* Surgically adjust the associated constraint of adjusted parameter
29210 and it's corresponding contribution to the current template
29211 requirements. */
29212 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29213 {
29214 tree id = unpack_concept_check (constr);
29215 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29216 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29217 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29218
29219 /* If there was a constraint, we also need to replace that in
29220 the template requirements, which we've already built. */
29221 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29222 reqs = find_template_requirement (reqs, constr);
29223 *reqs = fold;
29224 }
29225 }
29226
29227 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29228 TREE_VEC_ELT (replacement, i)
29229 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29230
29231 /* If there are more levels then build up the replacement with the outer
29232 template parms. */
29233 if (depth > 1)
29234 replacement = add_to_template_args (template_parms_to_args
29235 (TREE_CHAIN (current_template_parms)),
29236 replacement);
29237
29238 return tsubst (parm, replacement, tf_none, NULL_TREE);
29239 }
29240
29241 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29242 0..N-1. */
29243
29244 void
29245 declare_integer_pack (void)
29246 {
29247 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29248 build_function_type_list (integer_type_node,
29249 integer_type_node,
29250 NULL_TREE),
29251 NULL_TREE, ECF_CONST);
29252 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29253 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29254 CP_BUILT_IN_INTEGER_PACK);
29255 }
29256
29257 /* Set up the hash tables for template instantiations. */
29258
29259 void
29260 init_template_processing (void)
29261 {
29262 /* FIXME: enable sanitization (PR87847) */
29263 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29264 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29265
29266 if (cxx_dialect >= cxx11)
29267 declare_integer_pack ();
29268 }
29269
29270 /* Print stats about the template hash tables for -fstats. */
29271
29272 void
29273 print_template_statistics (void)
29274 {
29275 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29276 "%f collisions\n", (long) decl_specializations->size (),
29277 (long) decl_specializations->elements (),
29278 decl_specializations->collisions ());
29279 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29280 "%f collisions\n", (long) type_specializations->size (),
29281 (long) type_specializations->elements (),
29282 type_specializations->collisions ());
29283 }
29284
29285 #if CHECKING_P
29286
29287 namespace selftest {
29288
29289 /* Verify that build_non_dependent_expr () works, for various expressions,
29290 and that location wrappers don't affect the results. */
29291
29292 static void
29293 test_build_non_dependent_expr ()
29294 {
29295 location_t loc = BUILTINS_LOCATION;
29296
29297 /* Verify constants, without and with location wrappers. */
29298 tree int_cst = build_int_cst (integer_type_node, 42);
29299 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29300
29301 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29302 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29303 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29304
29305 tree string_lit = build_string (4, "foo");
29306 TREE_TYPE (string_lit) = char_array_type_node;
29307 string_lit = fix_string_type (string_lit);
29308 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29309
29310 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29311 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29312 ASSERT_EQ (wrapped_string_lit,
29313 build_non_dependent_expr (wrapped_string_lit));
29314 }
29315
29316 /* Verify that type_dependent_expression_p () works correctly, even
29317 in the presence of location wrapper nodes. */
29318
29319 static void
29320 test_type_dependent_expression_p ()
29321 {
29322 location_t loc = BUILTINS_LOCATION;
29323
29324 tree name = get_identifier ("foo");
29325
29326 /* If no templates are involved, nothing is type-dependent. */
29327 gcc_assert (!processing_template_decl);
29328 ASSERT_FALSE (type_dependent_expression_p (name));
29329
29330 ++processing_template_decl;
29331
29332 /* Within a template, an unresolved name is always type-dependent. */
29333 ASSERT_TRUE (type_dependent_expression_p (name));
29334
29335 /* Ensure it copes with NULL_TREE and errors. */
29336 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29337 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29338
29339 /* A USING_DECL in a template should be type-dependent, even if wrapped
29340 with a location wrapper (PR c++/83799). */
29341 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29342 TREE_TYPE (using_decl) = integer_type_node;
29343 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29344 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29345 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29346 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29347
29348 --processing_template_decl;
29349 }
29350
29351 /* Run all of the selftests within this file. */
29352
29353 void
29354 cp_pt_c_tests ()
29355 {
29356 test_build_non_dependent_expr ();
29357 test_type_dependent_expression_p ();
29358 }
29359
29360 } // namespace selftest
29361
29362 #endif /* #if CHECKING_P */
29363
29364 #include "gt-cp-pt.h"
This page took 1.252245 seconds and 6 git commands to generate.